xfstests: RESULTS_DIR needs to be an absolute path
[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 | grep noxfer > /dev/null 2>&1
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 # ls -l w/ selinux sometimes puts a dot at the end:
61 # -rwxrw-r--. id1 id2 file1
62
63 _ls_l()
64 {
65         ls -l $* | sed "s/\(^[-rwxdlbcpsStT]*\)\. /\1 /"
66 }
67
68 _mount_opts()
69 {
70     # SELinux adds extra xattrs which can mess up our expected output.
71     # So, mount with a context, and they won't be created
72     # nfs_t is a "liberal" context so we can use it.
73     if [ -x /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled; then
74         SELINUX_MOUNT_OPTIONS="-o context=system_u:object_r:nfs_t:s0"
75         export SELINUX_MOUNT_OPTIONS
76     fi
77
78     case $FSTYP in
79     xfs)
80         export MOUNT_OPTIONS=$XFS_MOUNT_OPTIONS
81         ;;
82     udf)
83         export MOUNT_OPTIONS=$UDF_MOUNT_OPTIONS
84         ;;
85     nfs)
86         export MOUNT_OPTIONS=$NFS_MOUNT_OPTIONS
87         ;;
88     ext2|ext3|ext4|ext4dev)
89         # acls & xattrs aren't turned on by default on ext$FOO
90         export MOUNT_OPTIONS="-o acl,user_xattr $EXT_MOUNT_OPTIONS"
91         ;;
92     reiserfs)
93         # acls & xattrs aren't turned on by default on reiserfs
94         export MOUNT_OPTIONS="-o acl,user_xattr $REISERFS_MOUNT_OPTIONS"
95         ;;
96     gfs2)
97         # acls aren't turned on by default on gfs2
98         export MOUNT_OPTIONS="-o acl $GFS2_MOUNT_OPTIONS"
99         ;;
100     *)
101         ;;
102     esac
103 }
104
105 _mkfs_opts()
106 {
107     case $FSTYP in
108     xfs)
109         export MKFS_OPTIONS=$XFS_MKFS_OPTIONS
110         ;;
111     udf)
112         [ ! -z "$udf_fsize" ] && \
113             UDF_MKFS_OPTIONS="$UDF_MKFS_OPTIONS -s $udf_fsize"
114         export MKFS_OPTIONS=$UDF_MKFS_OPTIONS
115         ;;
116     nfs)
117         export MKFS_OPTIONS=$NFS_MKFS_OPTIONS
118         ;;
119     reiserfs)
120         export MKFS_OPTIONS="$REISERFS_MKFS_OPTIONS -q"
121         ;;
122     gfs2)
123         export MKFS_OPTIONS="$GFS2_MKFS_OPTIONS -O -p lock_nolock"
124         ;;
125     jfs)
126         export MKFS_OPTIONS="$JFS_MKFS_OPTIONS -q"
127         ;;
128     *)
129         ;;
130     esac
131 }
132
133 _fsck_opts()
134 {
135     case $FSTYP in
136     ext2|ext3|ext4|ext4dev)
137         export FSCK_OPTIONS="-nf"
138         ;;
139     reiserfs)
140         export FSCK_OPTIONS="--yes"
141         ;;
142     *)
143         export FSCK_OPTIONS="-n"
144         ;;
145     esac
146 }
147
148 [ -z "$FSTYP" ] && FSTYP=xfs
149 [ -z "$MOUNT_OPTIONS" ] && _mount_opts
150 [ -z "$MKFS_OPTIONS" ] && _mkfs_opts
151 [ -z "$FSCK_OPTIONS" ] && _fsck_opts
152
153
154 # we need common.config
155 if [ "$iam" != "check" ]
156 then
157     if ! . ./common/config
158         then
159         echo "$iam: failed to source common.config"
160         exit 1
161     fi
162 fi
163
164 # check for correct setup
165 case "$FSTYP" in
166     xfs)
167          [ "$XFS_LOGPRINT_PROG" = "" ] && _fatal "xfs_logprint not found"
168          [ "$XFS_REPAIR_PROG" = "" ] && _fatal "xfs_repair not found"
169          [ "$XFS_CHECK_PROG" = "" ] && _fatal "xfs_check not found"
170          [ "$XFS_DB_PROG" = "" ] && _fatal "xfs_db not found"
171          [ "$MKFS_XFS_PROG" = "" ] && _fatal "mkfs_xfs not found"
172          ;;
173     udf)
174          [ "$MKFS_UDF_PROG" = "" ] && _fatal "mkfs_udf/mkudffs not found"
175          ;;
176     btrfs)
177          [ "$MKFS_BTRFS_PROG" = "" ] && _fatal "mkfs.btrfs not found"
178          ;;
179     nfs)
180          ;;
181 esac
182
183 # make sure we have a standard umask
184 umask 022
185
186 _mount()
187 {
188     $MOUNT_PROG `_mount_ops_filter $*`
189 }
190
191 _scratch_options()
192 {
193     type=$1
194     SCRATCH_OPTIONS=""
195
196     if [ "$FSTYP" != "xfs" ]; then
197         return
198     fi
199
200     case $type in
201     mkfs)
202         [ "$HOSTOS" != "IRIX" ] && SCRATCH_OPTIONS="$SCRATCH_OPTIONS -f"
203         rt_opt="-r"
204         log_opt="-l"
205         ;;
206     mount)
207         rt_opt="-o"
208         log_opt="-o"
209         ;;
210     esac
211     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
212         SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${rt_opt}rtdev=$SCRATCH_RTDEV"
213     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
214         SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${log_opt}logdev=$SCRATCH_LOGDEV"
215 }
216
217 _test_options()
218 {
219     type=$1
220     TEST_OPTIONS=""
221
222     if [ "$FSTYP" != "xfs" ]; then
223         return
224     fi
225
226     case $type in
227     mkfs)
228         rt_opt="-r"
229         log_opt="-l"
230         ;;
231     mount)
232         rt_opt="-o"
233         log_opt="-o"
234         ;;
235     esac
236     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
237         TEST_OPTIONS="$TEST_OPTIONS ${rt_opt}rtdev=$TEST_RTDEV"
238     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
239         TEST_OPTIONS="$TEST_OPTIONS ${log_opt}logdev=$TEST_LOGDEV"
240 }
241
242 _mount_ops_filter()
243 {
244     params="$*"
245     
246     #get mount point to handle dmapi mtpt option correctly
247     let last_index=$#-1
248     [ $last_index -gt 0 ] && shift $last_index
249     FS_ESCAPED=$1
250     
251     # irix is fussy about how it is fed its mount options
252     # - multiple -o's are not allowed
253     # - no spaces between comma delimitered options
254     # the sed script replaces all -o's (except the first) with a comma
255     # not required for linux, but won't hurt
256     
257     echo $params | sed -e 's/[[:space:]]*-o[[:space:]]*/UnIqUe/1; s/[[:space:]]*-o[[:space:]]*/,/g; s/UnIqUe/ -o /1' \
258         | sed -e 's/dmapi/dmi/' \
259         | $PERL_PROG -ne "s#mtpt=[^,|^\n|^\s]*#mtpt=$FS_ESCAPED\1\2#; print;"
260
261 }
262
263 _scratch_mount_options()
264 {
265     _scratch_options mount
266
267     echo $SCRATCH_OPTIONS $MOUNT_OPTIONS $SELINUX_MOUNT_OPTIONS $* $SCRATCH_DEV $SCRATCH_MNT
268 }
269
270 _scratch_mount()
271 {
272     _mount -t $FSTYP `_scratch_mount_options $*`
273 }
274
275 _scratch_unmount()
276 {
277     $UMOUNT_PROG $SCRATCH_DEV
278 }
279
280 _scratch_remount()
281 {
282     _scratch_unmount
283     _scratch_mount
284 }
285
286 _test_mount()
287 {
288     _test_options mount
289     _mount -t $FSTYP $TEST_OPTIONS $TEST_FS_MOUNT_OPTS $SELINUX_MOUNT_OPTIONS $* $TEST_DEV $TEST_DIR
290 }
291
292 _scratch_mkfs_options()
293 {
294     _scratch_options mkfs
295     echo $SCRATCH_OPTIONS $MKFS_OPTIONS $* $SCRATCH_DEV
296 }
297
298
299 _setup_large_xfs_fs()
300 {
301         fs_size=$1
302         local tmp_dir=/tmp/
303
304         [ "$LARGE_SCRATCH_DEV" != yes ] && return 0
305         [ -z "$SCRATCH_DEV_EMPTY_SPACE" ] && SCRATCH_DEV_EMPTY_SPACE=0
306         [ $SCRATCH_DEV_EMPTY_SPACE -ge $fs_size ] && return 0
307
308         # calculate the size of the file we need to allocate.
309         # Default free space in the FS is 50GB, but you can specify more via
310         # SCRATCH_DEV_EMPTY_SPACE
311         file_size=$(($fs_size - 50*1024*1024*1024))
312         file_size=$(($file_size - $SCRATCH_DEV_EMPTY_SPACE))
313
314         # mount the filesystem, create the file, unmount it
315         _scratch_mount 2>&1 >$tmp_dir/mnt.err
316         local status=$?
317         if [ $status -ne 0 ]; then
318                 echo "mount failed"
319                 cat $tmp_dir/mnt.err >&2
320                 rm -f $tmp_dir/mnt.err
321                 return $status
322         fi
323         rm -f $tmp_dir/mnt.err
324
325         xfs_io -F -f \
326                 -c "truncate $file_size" \
327                 -c "falloc -k 0 $file_size" \
328                 -c "chattr +d" \
329                 $SCRATCH_MNT/.use_space 2>&1 > /dev/null
330         export NUM_SPACE_FILES=1
331         status=$?
332         umount $SCRATCH_MNT
333         if [ $status -ne 0 ]; then
334                 echo "large file prealloc failed"
335                 cat $tmp_dir/mnt.err >&2
336                 return $status
337         fi
338         return 0
339 }
340
341 _scratch_mkfs_xfs()
342 {
343     # extra mkfs options can be added by tests
344     local extra_mkfs_options=$*
345
346     local tmp_dir=/tmp/
347
348     _scratch_options mkfs
349
350     # save mkfs output in case conflict means we need to run again.
351     # only the output for the mkfs that applies should be shown
352     $MKFS_XFS_PROG $SCRATCH_OPTIONS $MKFS_OPTIONS $extra_mkfs_options $SCRATCH_DEV \
353         2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
354     local mkfs_status=$?
355
356     # a mkfs failure may be caused by conflicts between
357     # $MKFS_OPTIONS and $extra_mkfs_options
358
359     if [ $mkfs_status -ne 0 -a ! -z "$extra_mkfs_options" ]; then
360         echo "** mkfs failed with extra mkfs options added to \"$MKFS_OPTIONS\" by test $seq **" \
361             >>$seqres.full
362         echo "** attempting to mkfs using only test $seq options: $extra_mkfs_options **" \
363             >>$seqres.full
364         # running mkfs again. overwrite previous mkfs output files
365         $MKFS_XFS_PROG $SCRATCH_OPTIONS $extra_mkfs_options $SCRATCH_DEV \
366             2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
367         mkfs_status=$?
368     fi
369
370     if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
371         # manually parse the mkfs output to get the fs size in bytes
372         local fs_size
373         fs_size=`cat $tmp_dir.mkfsstd | perl -ne '
374             if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+)/) {
375                 my $size = $1 * $2;
376                 print STDOUT "$size\n";
377             }'`
378         _setup_large_xfs_fs $fs_size
379         mkfs_status=$?
380     fi
381
382     # output stored mkfs output
383     cat $tmp_dir.mkfserr >&2
384     cat $tmp_dir.mkfsstd
385     rm -f $tmp_dir.mkfserr $tmp_dir.mkfsstd
386
387     return $mkfs_status
388 }
389
390 _setup_large_ext4_fs()
391 {
392         fs_size=$1
393         local tmp_dir=/tmp/
394
395         [ "$LARGE_SCRATCH_DEV" != yes ] && return 0
396         [ -z "$SCRATCH_DEV_EMPTY_SPACE" ] && SCRATCH_DEV_EMPTY_SPACE=0
397         [ $SCRATCH_DEV_EMPTY_SPACE -ge $fs_size ] && return 0
398
399         # Default free space in the FS is 50GB, but you can specify more via
400         # SCRATCH_DEV_EMPTY_SPACE
401         space_to_consume=$(($fs_size - 50*1024*1024*1024 - $SCRATCH_DEV_EMPTY_SPACE))
402
403         # mount the filesystem and create 16TB - 4KB files until we consume
404         # all the necessary space.
405         _scratch_mount 2>&1 >$tmp_dir/mnt.err
406         local status=$?
407         if [ $status -ne 0 ]; then
408                 echo "mount failed"
409                 cat $tmp_dir/mnt.err >&2
410                 rm -f $tmp_dir/mnt.err
411                 return $status
412         fi
413         rm -f $tmp_dir/mnt.err
414
415         file_size=$((16*1024*1024*1024*1024 - 4096))
416         nfiles=0
417         while [ $space_to_consume -gt $file_size ]; do
418
419                 xfs_io -F -f \
420                         -c "truncate $file_size" \
421                         -c "falloc -k 0 $file_size" \
422                         $SCRATCH_MNT/.use_space.$nfiles 2>&1
423                 status=$?
424                 if [ $status -ne 0 ]; then
425                         break;
426                 fi
427
428                 space_to_consume=$(( $space_to_consume - $file_size ))
429                 nfiles=$(($nfiles + 1))
430         done
431
432         # consume the remaining space.
433         if [ $space_to_consume -gt 0 ]; then
434                 xfs_io -F -f \
435                         -c "truncate $space_to_consume" \
436                         -c "falloc -k 0 $space_to_consume" \
437                         $SCRATCH_MNT/.use_space.$nfiles 2>&1
438                 status=$?
439         fi
440         export NUM_SPACE_FILES=$nfiles
441
442         umount $SCRATCH_MNT
443         if [ $status -ne 0 ]; then
444                 echo "large file prealloc failed"
445                 cat $tmp_dir/mnt.err >&2
446                 return $status
447         fi
448         return 0
449 }
450 _scratch_mkfs_ext4()
451 {
452         local tmp_dir=/tmp/
453
454         /sbin/mkfs -t $FSTYP -- $MKFS_OPTIONS $* $SCRATCH_DEV \
455                         2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
456         local mkfs_status=$?
457
458         if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
459                 # manually parse the mkfs output to get the fs size in bytes
460                 fs_size=`cat $tmp_dir.mkfsstd | awk ' \
461                         /^Block size/ { split($2, a, "="); bs = a[2] ; } \
462                         / inodes, / { blks = $3 } \
463                         /reserved for the super user/ { resv = $1 } \
464                         END { fssize = bs * blks - resv; print fssize }'`
465
466                 _setup_large_ext4_fs $fs_size
467                 mkfs_status=$?
468         fi
469
470         # output stored mkfs output
471         cat $tmp_dir.mkfserr >&2
472         cat $tmp_dir.mkfsstd
473         rm -f $tmp_dir.mkfserr $tmp_dir.mkfsstd
474
475         return $mkfs_status
476 }
477
478 _scratch_mkfs()
479 {
480     case $FSTYP in
481     xfs)
482         _scratch_mkfs_xfs $*
483         ;;
484     nfs*)
485         # do nothing for nfs
486         ;;
487     udf)
488         $MKFS_UDF_PROG $MKFS_OPTIONS $* $SCRATCH_DEV > /dev/null
489         ;;
490     btrfs)
491         $MKFS_BTRFS_PROG $MKFS_OPTIONS $* $SCRATCH_DEV > /dev/null
492         ;;
493     ext4)
494         _scratch_mkfs_ext4 $*
495         ;;
496     *)
497         yes | $MKFS_PROG -t $FSTYP -- $MKFS_OPTIONS $* $SCRATCH_DEV
498         ;;
499     esac
500 }
501
502 # Create fs of certain size on scratch device
503 # _scratch_mkfs_sized <size in bytes> [optional blocksize]
504 _scratch_mkfs_sized()
505 {
506     fssize=$1
507     blocksize=$2
508     [ -z "$blocksize" ] && blocksize=4096
509     blocks=`expr $fssize / $blocksize`
510
511     if [ "$HOSTOS" == "Linux" ]; then
512         devsize=`blockdev --getsize64 $SCRATCH_DEV`
513         [ "$fssize" -gt "$devsize" ] && _notrun "Scratch device too small"
514     fi
515
516     case $FSTYP in
517     xfs)
518         _scratch_mkfs_xfs -d size=$fssize -b size=$blocksize
519         ;;
520     ext2|ext3|ext4|ext4dev)
521         yes | ${MKFS_PROG}.$FSTYP $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV $blocks
522         ;;
523     btrfs)
524         $MKFS_BTRFS_PROG $MKFS_OPTIONS -b $fssize $SCRATCH_DEV
525         ;;
526     *)
527         _notrun "Filesystem $FSTYP not supported in _scratch_mkfs_sized"
528         ;;
529     esac
530 }
531
532 # Emulate an N-data-disk stripe w/ various stripe units
533 # _scratch_mkfs_geom <sunit bytes> <swidth multiplier> [optional blocksize]
534 _scratch_mkfs_geom()
535 {
536     sunit_bytes=$1
537     swidth_mult=$2
538     blocksize=$3
539     [ -z "$blocksize" ] && blocksize=4096
540
541     let sunit_blocks=$sunit_bytes/$blocksize
542     let swidth_blocks=$sunit_blocks*$swidth_mult
543
544     case $FSTYP in
545     xfs)
546         MKFS_OPTIONS+=" -b size=$blocksize, -d su=$sunit_bytes,sw=$swidth_mult"
547         ;;
548     ext4|ext4dev)
549         MKFS_OPTIONS+=" -b $blocksize -E stride=$sunit_blocks,stripe_width=$swidth_blocks"
550         ;;
551     *)
552         _notrun "can't mkfs $FSTYP with geometry"
553         ;;
554     esac
555     _scratch_mkfs
556 }
557
558 _scratch_resvblks()
559 {
560         case $FSTYP in
561         xfs)
562                 xfs_io -x -c "resblks $1" $SCRATCH_MNT
563                 ;;
564         *)
565                 ;;
566         esac
567 }
568
569 _scratch_xfs_db_options()
570 {
571     SCRATCH_OPTIONS=""
572     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
573         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
574     echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
575 }
576
577 _scratch_xfs_logprint()
578 {
579     SCRATCH_OPTIONS=""
580     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
581         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
582     $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
583 }
584
585 _scratch_xfs_check()
586 {
587     SCRATCH_OPTIONS=""
588     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
589         SCRATCH_OPTIONS="-l $SCRATCH_LOGDEV"
590     [ "$LARGE_SCRATCH_DEV" = yes ] && \
591         SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
592     $XFS_CHECK_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
593 }
594
595 _scratch_xfs_repair()
596 {
597     SCRATCH_OPTIONS=""
598     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
599         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
600     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
601         SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
602     [ "$LARGE_SCRATCH_DEV" = yes ] && SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
603     $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
604 }
605
606 _get_pids_by_name()
607 {
608     if [ $# -ne 1 ]
609     then
610         echo "Usage: _get_pids_by_name process-name" 1>&2
611         exit 1
612     fi
613
614     # Algorithm ... all ps(1) variants have a time of the form MM:SS or
615     # HH:MM:SS before the psargs field, use this as the search anchor.
616     #
617     # Matches with $1 (process-name) occur if the first psarg is $1
618     # or ends in /$1 ... the matching uses sed's regular expressions,
619     # so passing a regex into $1 will work.
620
621     ps $PS_ALL_FLAGS \
622     | sed -n \
623         -e 's/$/ /' \
624         -e 's/[         ][      ]*/ /g' \
625         -e 's/^ //' \
626         -e 's/^[^ ]* //' \
627         -e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
628         -e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
629 }
630
631 # fix malloc libs output
632 #
633 _fix_malloc()
634 {
635     # filter out the Electric Fence notice
636     $PERL_PROG -e '
637         while (<>) {
638             if (defined $o && /^\s+Electric Fence/) {
639                 chomp($o);
640                 print "$o";
641                 undef $o;
642                 next;
643             }
644             print $o if (defined $o);
645
646             $o=$_;
647         }
648         print $o if (defined $o);
649     '
650 }
651
652 # check if run as root
653 #
654 _need_to_be_root()
655 {
656     id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
657     if [ "$id" -ne 0 ]
658     then
659         echo "Arrgh ... you need to be root (not uid=$id) to run this test"
660         exit 1
661     fi
662 }
663
664
665 #
666 # _df_device : get an IRIX style df line for a given device
667 #
668 #       - returns "" if not mounted
669 #       - returns fs type in field two (ala IRIX)
670 #       - joins line together if split by fancy df formatting
671 #       - strips header etc
672 #
673
674 _df_device()
675 {
676     if [ $# -ne 1 ]
677     then
678         echo "Usage: _df_device device" 1>&2
679         exit 1
680     fi
681
682     $DF_PROG 2>/dev/null | $AWK_PROG -v what=$1 '
683         match($1,what) && NF==1 {
684             v=$1
685             getline
686             print v, $0
687             exit
688         }
689         match($1,what) {
690             print
691             exit
692         }
693     '
694 }
695
696 #
697 # _df_dir : get an IRIX style df line for device where a directory resides
698 #
699 #       - returns fs type in field two (ala IRIX)
700 #       - joins line together if split by fancy df formatting
701 #       - strips header etc
702 #
703
704 _df_dir()
705 {
706     if [ $# -ne 1 ]
707     then
708         echo "Usage: _df_dir device" 1>&2
709         exit 1
710     fi
711
712     $DF_PROG $1 2>/dev/null | $AWK_PROG -v what=$1 '
713         NR == 2 && NF==1 {
714             v=$1
715             getline
716             print v, $0;
717             exit 0
718         }
719         NR == 2 {
720             print;
721             exit 0
722         }
723         {}
724     '
725     # otherwise, nada
726 }
727
728 # return percentage used disk space for mounted device
729
730 _used()
731 {
732     if [ $# -ne 1 ]
733     then
734         echo "Usage: _used device" 1>&2
735         exit 1
736     fi
737
738     _df_device $1 | $AWK_PROG '{ sub("%", "") ; print $6 }'
739 }
740
741 # return the FS type of a mounted device
742 #
743 _fs_type()
744 {
745     if [ $# -ne 1 ]
746     then
747         echo "Usage: _fs_type device" 1>&2
748         exit 1
749     fi
750
751     #
752     # The Linux kernel shows NFSv4 filesystems in df output as
753     # filesystem type nfs4, although we mounted it as nfs earlier.
754     # Fix the filesystem type up here so that the callers don't
755     # have to bother with this quirk.
756     #
757     _df_device $1 | $AWK_PROG '{ print $2 }' | sed -e 's/nfs4/nfs/'
758 }
759
760 # return the FS mount options of a mounted device
761 #
762 # should write a version which just parses the output of mount for IRIX
763 # compatibility, but since this isn't used at all, at the moment I'll leave
764 # this for now
765 #
766 _fs_options()
767 {
768     if [ $# -ne 1 ]
769     then
770         echo "Usage: _fs_options device" 1>&2
771         exit 1
772     fi
773
774     $AWK_PROG -v dev=$1 '
775         match($1,dev) { print $4 }
776     ' </proc/mounts
777 }
778
779 # returns device number if a file is a block device
780 #
781 _is_block_dev()
782 {
783     if [ $# -ne 1 ]
784     then
785         echo "Usage: _is_block_dev dev" 1>&2
786         exit 1
787     fi
788
789     _dev=$1
790     if [ -L "${_dev}" ]; then
791         _dev=`readlink -f ${_dev}`
792     fi
793
794     if [ -b "${_dev}" ]; then
795         src/lstat64 ${_dev} | $AWK_PROG '/Device type:/ { print $9 }'
796     fi
797 }
798
799 # Do a command, log it to $seqres.full, optionally test return status
800 # and die if command fails. If called with one argument _do executes the
801 # command, logs it, and returns its exit status. With two arguments _do
802 # first prints the message passed in the first argument, and then "done"
803 # or "fail" depending on the return status of the command passed in the
804 # second argument. If the command fails and the variable _do_die_on_error
805 # is set to "always" or the two argument form is used and _do_die_on_error
806 # is set to "message_only" _do will print an error message to
807 # $seqres.out and exit.
808
809 _do()
810 {
811     if [ $# -eq 1 ]; then
812         _cmd=$1
813     elif [ $# -eq 2 ]; then
814         _note=$1
815         _cmd=$2
816         echo -n "$_note... "
817     else
818         echo "Usage: _do [note] cmd" 1>&2
819         status=1; exit
820     fi
821
822     (eval "echo '---' \"$_cmd\"") >>$seqres.full
823     (eval "$_cmd") >$tmp._out 2>&1; ret=$?
824     cat $tmp._out | _fix_malloc >>$seqres.full
825     if [ $# -eq 2 ]; then
826         if [ $ret -eq 0 ]; then
827             echo "done"
828         else
829             echo "fail"
830         fi
831     fi
832     if [ $ret -ne 0  ] \
833         && [ "$_do_die_on_error" = "always" \
834             -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
835     then
836         [ $# -ne 2 ] && echo
837         eval "echo \"$_cmd\" failed \(returned $ret\): see $seqres.full"
838         status=1; exit
839     fi
840
841     return $ret
842 }
843
844 # bail out, setting up .notrun file
845 #
846 _notrun()
847 {
848     echo "$*" > $seqres.notrun
849     echo "$seq not run: $*"
850     status=0
851     exit
852 }
853
854 # just plain bail out
855 #
856 _fail()
857 {
858     echo "$*" | tee -a $seqres.full
859     echo "(see $seqres.full for details)"
860     status=1
861     exit 1
862 }
863
864 # tests whether $FSTYP is one of the supported filesystems for a test
865 #
866 _supported_fs()
867 {
868     for f
869     do
870         if [ "$f" = "$FSTYP" -o "$f" = "generic" ]
871         then
872             return
873         fi
874     done
875
876     _notrun "not suitable for this filesystem type: $FSTYP"
877 }
878
879 # tests whether $FSTYP is one of the supported OSes for a test
880 #
881 _supported_os()
882 {
883     for h
884     do
885         if [ "$h" = "$HOSTOS" ]
886         then
887             return
888         fi
889     done
890
891     _notrun "not suitable for this OS: $HOSTOS"
892 }
893
894 # this test needs a scratch partition - check we're ok & unmount it
895 #
896 _require_scratch()
897 {
898     case "$FSTYP" in
899         nfs*)
900                  echo $SCRATCH_DEV | grep -q ":" > /dev/null 2>&1
901                  if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]
902                  then
903                      _notrun "this test requires a valid \$SCRATCH_DEV"
904                  fi
905                  ;;
906         *)
907                  if [ -z "$SCRATCH_DEV" -o "`_is_block_dev $SCRATCH_DEV`" = "" ]
908                  then
909                      _notrun "this test requires a valid \$SCRATCH_DEV"
910                  fi
911                  if [ "`_is_block_dev $SCRATCH_DEV`" = "`_is_block_dev $TEST_DEV`" ]
912                  then
913                      _notrun "this test requires a valid \$SCRATCH_DEV"
914                  fi
915                 if [ ! -d "$SCRATCH_MNT" ]
916                 then
917                      _notrun "this test requires a valid \$SCRATCH_MNT"
918                 fi
919                  ;;
920     esac
921
922     # mounted?
923     if _mount | grep -q $SCRATCH_DEV
924     then
925         # if it's mounted, make sure its on $SCRATCH_MNT
926         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
927         then
928             echo "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
929             exit 1
930         fi
931         # and then unmount it
932         if ! $UMOUNT_PROG $SCRATCH_DEV
933         then
934             echo "failed to unmount $SCRATCH_DEV"
935             exit 1
936         fi
937     fi
938 }
939
940 # this test needs a logdev
941 #
942 _require_logdev()
943 {
944     [ -z "$SCRATCH_LOGDEV" -o ! -b "$SCRATCH_LOGDEV" ] && \
945         _notrun "This test requires a valid \$SCRATCH_LOGDEV"
946     [ "$USE_EXTERNAL" != yes ] && \
947         _notrun "This test requires USE_EXTERNAL to be enabled"
948
949     # ensure its not mounted
950     $UMOUNT_PROG $SCRATCH_LOGDEV 2>/dev/null
951 }
952
953 # this test requires loopback device support
954 #
955 _require_loop()
956 {
957     if [ "$HOSTOS" != "Linux" ]
958     then
959         _notrun "This test requires linux for loopback device support"
960     fi
961
962     modprobe loop >/dev/null 2>&1
963     if grep loop /proc/devices >/dev/null 2>&1
964     then
965         :
966     else
967         _notrun "This test requires loopback device support"
968     fi
969 }
970
971 # this test requires ext2 filesystem support
972 #
973 _require_ext2()
974 {
975     if [ "$HOSTOS" != "Linux" ]
976     then
977         _notrun "This test requires linux for ext2 filesystem support"
978     fi
979
980     modprobe ext2 >/dev/null 2>&1
981     if grep ext2 /proc/filesystems >/dev/null 2>&1
982     then
983         :
984     else
985         _notrun "This test requires ext2 filesystem support"
986     fi
987 }
988
989 # this test requires that (large) loopback device files are not in use
990 #
991 _require_no_large_scratch_dev()
992 {
993     [ "$LARGE_SCRATCH_DEV" = yes ] && \
994         _notrun "Large filesystem testing in progress, skipped this test"
995 }
996
997 # this test requires that a realtime subvolume is in use, and
998 # that the kernel supports realtime as well.
999 #
1000 _require_realtime()
1001 {
1002     [ "$USE_EXTERNAL" = yes ] || \
1003         _notrun "External volumes not in use, skipped this test"
1004     [ "$SCRATCH_RTDEV" = "" ] && \
1005         _notrun "Realtime device required, skipped this test"
1006 }
1007
1008 # this test requires that a specified command (executable) exists
1009 # $1 - command, $2 - name for error message
1010 #
1011 _require_command()
1012 {
1013     [ -n "$1" ] && _cmd="$1" || _cmd="$2"
1014     [ -n "$1" -a -x "$1" ] || _notrun "$_cmd utility required, skipped this test"
1015 }
1016
1017 # this test requires the projid32bit feature to be available in
1018 # mkfs.xfs
1019 #
1020 _require_projid32bit()
1021 {
1022         _scratch_mkfs_xfs -f -i projid32bit=0 2>&1 >/dev/null \
1023            || _notrun "mkfs.xfs doesn't have projid32bit feature"
1024 }
1025
1026 # this test requires that external log/realtime devices are not in use
1027 #
1028 _require_nonexternal()
1029 {
1030     [ "$USE_EXTERNAL" = yes ] && \
1031         _notrun "External device testing in progress, skipped this test"
1032 }
1033
1034 # this test requires that a (specified) aio-dio executable exists
1035 # $1 - command (optional)
1036 #
1037 _require_aiodio()
1038 {
1039     if [ -z "$1" ]
1040     then
1041         AIO_TEST=src/aio-dio-regress/aiodio_sparse2
1042         [ -x $AIO_TEST ] || _notrun "aio-dio utilities required"
1043     else
1044         AIO_TEST=src/aio-dio-regress/$1
1045         [ -x $AIO_TEST ] || _notrun "$AIO_TEST not built"
1046     fi
1047 }
1048
1049 # run an aio-dio program
1050 # $1 - command
1051 _run_aiodio()
1052 {
1053     if [ -z "$1" ]
1054     then
1055         echo "usage: _run_aiodio command_name" 2>&1
1056         status=1; exit 1
1057     fi
1058
1059     _require_aiodio $1
1060
1061     local testtemp=$TEST_DIR/aio-testfile
1062     rm -f $testtemp
1063     $AIO_TEST $testtemp 2>&1
1064     status=$?
1065     rm -f $testtemp
1066
1067     return $status
1068 }
1069
1070 # indicate whether YP/NIS is active or not
1071 #
1072 _yp_active()
1073 {
1074         local dn
1075         dn=$(domainname 2>/dev/null)
1076         test -n "${dn}" -a "${dn}" != "(none)"
1077         echo $?
1078 }
1079
1080 # cat the password file
1081 #
1082 _cat_passwd()
1083 {
1084         [ $(_yp_active) -eq 0 ] && ypcat passwd
1085         cat /etc/passwd
1086 }
1087
1088 # cat the group file
1089 #
1090 _cat_group()
1091 {
1092         [ $(_yp_active) -eq 0 ] && ypcat group
1093         cat /etc/group
1094 }
1095
1096 # check for the fsgqa user on the machine
1097 #
1098 _require_user()
1099 {
1100     qa_user=fsgqa
1101     _cat_passwd | grep -q $qa_user
1102     [ "$?" == "0" ] || _notrun "$qa_user user not defined."
1103     echo /bin/true | su $qa_user
1104     [ "$?" == "0" ] || _notrun "$qa_user cannot execute commands."
1105 }
1106
1107 # check for the fsgqa group on the machine
1108 #
1109 _require_group()
1110 {
1111     qa_group=fsgqa
1112     _cat_group | grep -q $qa_group
1113     [ "$?" == "0" ] || _notrun "$qa_group user not defined."
1114 }
1115
1116 _filter_user_do()
1117 {
1118         perl -ne "
1119 s,.*Permission\sdenied.*,Permission denied,;
1120 s,.*no\saccess\sto\stty.*,,;
1121 s,.*no\sjob\scontrol\sin\sthis\sshell.*,,;
1122 s,^\s*$,,;
1123         print;"
1124 }
1125
1126 _user_do()
1127 {
1128     if [ "$HOSTOS" == "IRIX" ]
1129         then
1130         echo $1 | /bin/bash "su $qa_user 2>&1" | _filter_user_do
1131     else
1132         echo $1 | su $qa_user 2>&1 | _filter_user_do
1133     fi
1134 }
1135
1136 # check that xfs_io, kernel, and filesystem all support zero
1137 _require_xfs_io_zero()
1138 {
1139         testio=`$XFS_IO_PROG -c "zero help" 2>&1`
1140         echo $testio | grep -q 'command "zero" not found' && \
1141                 _notrun "zero command not supported"
1142 }
1143
1144 # check that xfs_io, glibc, kernel, and filesystem all (!) support
1145 # fallocate
1146 #
1147 _require_xfs_io_falloc()
1148 {
1149         testfile=$TEST_DIR/$$.falloc
1150         testio=`$XFS_IO_PROG -F -f -c "falloc 0 1m" $testfile 2>&1`
1151         rm -f $testfile 2>&1 > /dev/null
1152         echo $testio | grep -q "not found" && \
1153                 _notrun "xfs_io fallocate support is missing"
1154         echo $testio | grep -q "Operation not supported" && \
1155                 _notrun "xfs_io fallocate command failed (old kernel/wrong fs?)"
1156 }
1157
1158 # check that xfs_io, kernel and filesystem all support fallocate with hole
1159 # punching
1160 _require_xfs_io_falloc_punch()
1161 {
1162         testfile=$TEST_DIR/$$.falloc
1163         testio=`$XFS_IO_PROG -F -f -c "pwrite 0 20k" -c "fsync" \
1164                 -c "fpunch 4k 8k" $testfile 2>&1`
1165         rm -f $testfile 2>&1 > /dev/null
1166         echo $testio | grep -q "not found" && \
1167                 _notrun "xfs_io fallocate punch support is missing"
1168         echo $testio | grep -q "Operation not supported" && \
1169                 _notrun "xfs_io fallocate punch command failed (no fs support?)"
1170 }
1171
1172 # check that xfs_io, kernel and filesystem support fiemap
1173 _require_xfs_io_fiemap()
1174 {
1175         testfile=$TEST_DIR/$$.fiemap
1176         testio=`$XFS_IO_PROG -F -f -c "pwrite 0 20k" -c "fsync" \
1177                 -c "fiemap -v" $testfile 2>&1`
1178         rm -f $testfile 2>&1 > /dev/null
1179         echo $testio | grep -q "not found" && \
1180                 _notrun "xfs_io fiemap support is missing"
1181         echo $testio | grep -q "Operation not supported" && \
1182                 _notrun "xfs_io fiemap command failed (no fs support?)"
1183 }
1184
1185 # Check that a fs has enough free space (in 1024b blocks)
1186 #
1187 _require_fs_space()
1188 {
1189         MNT=$1
1190         BLOCKS=$2       # in units of 1024
1191         let GB=$BLOCKS/1024/1024
1192
1193         FREE_BLOCKS=`df -klP $MNT | grep -v Filesystem | awk '{print $4}'`
1194         [ $FREE_BLOCKS -lt $BLOCKS ] && \
1195                 _notrun "This test requires at least ${GB}GB free on $MNT to run"
1196 }
1197
1198 #
1199 # Check if the filesystem supports sparse files.
1200 #
1201 # Unfortunately there is no better way to do this than a manual black list.
1202 #
1203 _require_sparse_files()
1204 {
1205     case $FSTYP in
1206     hfsplus)
1207         _notrun "Sparse files not supported by this filesystem type: $FSTYP"
1208         ;;
1209     *)
1210         ;;
1211     esac
1212 }
1213
1214 _require_debugfs()
1215 {
1216     #boot_params always present in debugfs
1217     [ -d "$DEBUGFS_MNT/boot_params" ] || _notrun "Debugfs not mounted"
1218 }
1219
1220 _require_fail_make_request()
1221 {
1222     [ -f "$DEBUGFS_MNT/fail_make_request/probability" ] \
1223         || _notrun "$DEBUGFS_MNT/fail_make_request \
1224  not found. Seems that CONFIG_FAIL_MAKE_REQUEST kernel config option not enabled"
1225 }
1226
1227 # check that a FS on a device is mounted
1228 # if so, return mount point
1229 #
1230 _is_mounted()
1231 {
1232     if [ $# -ne 1 ]
1233     then
1234         echo "Usage: _is_mounted device" 1>&2
1235         exit 1
1236     fi
1237
1238     device=$1
1239
1240     if _mount | grep "$device " | $AWK_PROG -v pattern="type $FSTYP" '
1241         pattern        { print $3 ; exit 0 }
1242         END            { exit 1 }
1243     '
1244     then
1245         echo "_is_mounted: $device is not a mounted $FSTYP FS"
1246         exit 1
1247     fi
1248 }
1249
1250 # remount a FS to a new mode (ro or rw)
1251 #
1252 _remount()
1253 {
1254     if [ $# -ne 2 ]
1255     then
1256         echo "Usage: _remount device ro/rw" 1>&2
1257         exit 1
1258     fi
1259     device=$1
1260     mode=$2
1261
1262     if ! mount -o remount,$mode $device
1263     then
1264         echo "_remount: failed to remount filesystem on $device as $mode"
1265         exit 1
1266     fi
1267 }
1268
1269 # Run the appropriate repair/check on a filesystem
1270 #
1271 # if the filesystem is mounted, it's either remounted ro before being
1272 # checked or it's unmounted and then remounted
1273 #
1274
1275 # If set, we remount ro instead of unmounting for fsck
1276 USE_REMOUNT=0
1277
1278 _umount_or_remount_ro()
1279 {
1280     if [ $# -ne 1 ]
1281     then
1282         echo "Usage: _umount_or_remount_ro <device>" 1>&2
1283         exit 1
1284     fi
1285
1286     device=$1
1287     mountpoint=`_is_mounted $device`
1288
1289     if [ $USE_REMOUNT -eq 0 ]; then
1290         $UMOUNT_PROG $device
1291     else
1292         _remount $device ro
1293     fi
1294     echo "$mountpoint"
1295 }
1296
1297 _mount_or_remount_rw()
1298 {
1299     if [ $# -ne 3 ]
1300     then
1301         echo "Usage: _mount_or_remount_rw <opts> <device> <mountpoint>" 1>&2
1302         exit 1
1303     fi
1304     mount_opts=$1
1305     device=$2
1306     mountpoint=$3
1307
1308     if [ $USE_REMOUNT -eq 0 ]
1309     then
1310         if ! _mount -t $FSTYP $mount_opts $device $mountpoint
1311         then
1312             echo "!!! failed to remount $device on $mountpoint"
1313             return 0 # ok=0
1314         fi
1315     else
1316         _remount $device rw
1317     fi
1318
1319     return 1 # ok=1
1320 }
1321
1322 # Check a generic filesystem in no-op mode; this assumes that the
1323 # underlying fsck program accepts "-n" for a no-op (check-only) run,
1324 # and that it will still return an errno for corruption in this mode.
1325 #
1326 # Filesystems which don't support this will need to define their
1327 # own check routine.
1328 #
1329 _check_generic_filesystem()
1330 {
1331     device=$1
1332
1333     # If type is set, we're mounted
1334     type=`_fs_type $device`
1335     ok=1
1336
1337     if [ "$type" = "$FSTYP" ]
1338     then
1339         # mounted ...
1340         mountpoint=`_umount_or_remount_ro $device`
1341     fi
1342
1343     fsck -t $FSTYP $FSCK_OPTIONS $device >$tmp.fsck 2>&1
1344     if [ $? -ne 0 ]
1345     then
1346         echo "_check_generic_filesystem: filesystem on $device is inconsistent (see $seqres.full)"
1347
1348         echo "_check_generic filesystem: filesystem on $device is inconsistent" >>$seqres.full
1349         echo "*** fsck.$FSTYP output ***"       >>$seqres.full
1350         cat $tmp.fsck                           >>$seqres.full
1351         echo "*** end fsck.$FSTYP output"       >>$seqres.full
1352
1353         ok=0
1354     fi
1355     rm -f $tmp.fsck
1356
1357     if [ $ok -eq 0 ]
1358     then
1359         echo "*** mount output ***"             >>$seqres.full
1360         _mount                                  >>$seqres.full
1361         echo "*** end mount output"             >>$seqres.full
1362     elif [ "$type" = "$FSTYP" ]
1363     then
1364         # was mounted ...
1365         _mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
1366         ok=$?
1367     fi
1368
1369     if [ $ok -eq 0 ]; then
1370         status=1
1371         exit 1
1372     fi
1373
1374     return 0
1375 }
1376
1377 # run xfs_check and friends on a FS.
1378
1379 _check_xfs_filesystem()
1380 {
1381     if [ $# -ne 3 ]
1382     then
1383         echo "Usage: _check_xfs_filesystem device <logdev>|none <rtdev>|none" 1>&2
1384         exit 1
1385     fi
1386
1387     extra_mount_options=""
1388     device=$1
1389     if [ "$2" != "none" ]; then
1390         extra_log_options="-l$2"
1391         extra_mount_options="-ologdev=$2"
1392     fi
1393
1394     if [ "$3" != "none" ]; then
1395         extra_rt_options="-r$3"
1396         extra_mount_options=$extra_mount_options" -ortdev=$3"
1397     fi
1398     extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"
1399
1400     [ "$FSTYP" != xfs ] && return 0
1401
1402     type=`_fs_type $device`
1403     ok=1
1404
1405     if [ "$type" = "xfs" ]
1406     then
1407         # mounted ...
1408         mountpoint=`_umount_or_remount_ro $device`
1409     fi
1410
1411     $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
1412                 | tee $tmp.logprint | grep -q "<CLEAN>"
1413     if [ $? -ne 0 -a "$HOSTOS" = "Linux" ]
1414     then
1415         echo "_check_xfs_filesystem: filesystem on $device has dirty log (see $seqres.full)"
1416
1417         echo "_check_xfs_filesystem: filesystem on $device has dirty log"   >>$seqres.full
1418         echo "*** xfs_logprint -t output ***"   >>$seqres.full
1419         cat $tmp.logprint                       >>$seqres.full
1420         echo "*** end xfs_logprint output"      >>$seqres.full
1421
1422         ok=0
1423     fi
1424
1425     # xfs_check runs out of memory on large files, so even providing the test
1426     # option (-t) to avoid indexing the free space trees doesn't make it pass on
1427     # large filesystems. Avoid it.
1428     if [ "$LARGE_SCRATCH_DEV" != yes ]; then
1429             $XFS_CHECK_PROG $extra_log_options $device 2>&1 |\
1430                  _fix_malloc >$tmp.fs_check
1431     fi
1432     if [ -s $tmp.fs_check ]
1433     then
1434         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (c) (see $seqres.full)"
1435
1436         echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1437         echo "*** xfs_check output ***"         >>$seqres.full
1438         cat $tmp.fs_check                       >>$seqres.full
1439         echo "*** end xfs_check output"         >>$seqres.full
1440
1441         ok=0
1442     fi
1443
1444     $XFS_REPAIR_PROG -n $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
1445     if [ $? -ne 0 ]
1446     then
1447         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (r) (see $seqres.full)"
1448
1449         echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1450         echo "*** xfs_repair -n output ***"     >>$seqres.full
1451         cat $tmp.repair | _fix_malloc           >>$seqres.full
1452         echo "*** end xfs_repair output"        >>$seqres.full
1453
1454         ok=0
1455     fi
1456     rm -f $tmp.fs_check $tmp.logprint $tmp.repair
1457
1458     if [ $ok -eq 0 ]
1459     then
1460         echo "*** mount output ***"             >>$seqres.full
1461         _mount                                  >>$seqres.full
1462         echo "*** end mount output"             >>$seqres.full
1463     elif [ "$type" = "xfs" ]
1464     then
1465         _mount_or_remount_rw "$extra_mount_options" $device $mountpoint
1466     fi
1467
1468     if [ $ok -eq 0 ]; then
1469         status=1
1470         exit 1
1471     fi
1472
1473     return 0
1474 }
1475
1476 # Filter the knowen errors the UDF Verifier reports.
1477 _udf_test_known_error_filter()
1478 {
1479         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."
1480
1481 }
1482
1483 _check_udf_filesystem()
1484 {
1485     [ "$DISABLE_UDF_TEST" == "1" ] && return
1486
1487     if [ $# -ne 1 -a $# -ne 2 ]
1488     then
1489         echo "Usage: _check_udf_filesystem device [last_block]" 1>&2
1490         exit 1
1491     fi
1492
1493     if [ ! -x $here/src/udf_test ]
1494     then
1495         echo "udf_test not installed, please download and build the Philips"
1496         echo "UDF Verification Software from http://www.extra.research.philips.com/udf/."
1497         echo "Then copy the udf_test binary to $here/src/."
1498         echo "If you do not wish to run udf_test then set environment variable DISABLE_UDF_TEST"
1499         echo "to 1."
1500         return
1501     fi
1502
1503     device=$1
1504     if [ $# -eq 2 ];
1505     then
1506         LAST_BLOCK=`expr \( $2 - 1 \)`
1507         OPT_ARG="-lastvalidblock $LAST_BLOCK"
1508     fi
1509
1510     rm -f $seqres.checkfs
1511     sleep 1 # Due to a problem with time stamps in udf_test
1512     $here/src/udf_test $OPT_ARG $device | tee $seqres.checkfs | egrep "Error|Warning" | \
1513         _udf_test_known_error_filter | \
1514         egrep -iv "Error count:.*[0-9]+.*total occurrences:.*[0-9]+|Warning count:.*[0-9]+.*total occurrences:.*[0-9]+" | \
1515         sed "s/^.*$/Warning UDF Verifier reported errors see $seqres.checkfs./g"
1516
1517 }
1518
1519 _check_xfs_test_fs()
1520 {
1521     TEST_LOG="none"
1522     TEST_RT="none"
1523     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
1524         TEST_LOG="$TEST_LOGDEV"
1525
1526     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
1527         TEST_RT="$TEST_RTDEV"
1528
1529     _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT
1530
1531     # check for ipath consistency
1532     if $XFS_GROWFS_PROG -n $TEST_DIR | grep -q 'inode-paths=1'; then
1533         # errors go to stderr
1534         xfs_check_ipaths $TEST_DIR >/dev/null
1535         xfs_repair_ipaths -n $TEST_DIR >/dev/null
1536     fi
1537 }
1538
1539 _check_btrfs_filesystem()
1540 {
1541     device=$1
1542
1543     # If type is set, we're mounted
1544     type=`_fs_type $device`
1545     ok=1
1546
1547     if [ "$type" = "$FSTYP" ]
1548     then
1549         # mounted ...
1550         mountpoint=`_umount_or_remount_ro $device`
1551     fi
1552
1553     btrfsck $device >$tmp.fsck 2>&1
1554     if [ $? -ne 0 ]
1555     then
1556         echo "_check_btrfs_filesystem: filesystem on $device is inconsistent (see $seqres.full)"
1557
1558         echo "_check_btrfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1559         echo "*** fsck.$FSTYP output ***"       >>$seqres.full
1560         cat $tmp.fsck                           >>$seqres.full
1561         echo "*** end fsck.$FSTYP output"       >>$seqres.full
1562
1563         ok=0
1564     fi
1565     rm -f $tmp.fsck
1566
1567     if [ $ok -eq 0 ]
1568     then
1569         echo "*** mount output ***"             >>$seqres.full
1570         _mount                                  >>$seqres.full
1571         echo "*** end mount output"             >>$seqres.full
1572     elif [ "$type" = "$FSTYP" ]
1573     then
1574         # was mounted ...
1575         _mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
1576         ok=$?
1577     fi
1578
1579     if [ $ok -eq 0 ]; then
1580         status=1
1581         exit 1
1582     fi
1583
1584     return 0
1585 }
1586
1587 _check_test_fs()
1588 {
1589     case $FSTYP in
1590     xfs)
1591         _check_xfs_test_fs
1592         ;;
1593     nfs)
1594         # no way to check consistency for nfs
1595         ;;
1596     udf)
1597         # do nothing for now
1598         ;;
1599     btrfs)
1600         _check_btrfs_filesystem $TEST_DEV
1601         ;;
1602     *)
1603         _check_generic_filesystem $TEST_DEV
1604         ;;
1605     esac
1606 }
1607
1608 _check_scratch_fs()
1609 {
1610     case $FSTYP in
1611     xfs)
1612         SCRATCH_LOG="none"
1613         SCRATCH_RT="none"
1614         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
1615             SCRATCH_LOG="$SCRATCH_LOGDEV"
1616
1617         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
1618             SCRATCH_RT="$SCRATCH_RTDEV"
1619
1620         _check_xfs_filesystem $SCRATCH_DEV $SCRATCH_LOG $SCRATCH_RT
1621         ;;
1622     udf)
1623         _check_udf_filesystem $SCRATCH_DEV $udf_fsize
1624         ;;
1625     nfs*)
1626         # Don't know how to check an NFS filesystem, yet.
1627         ;;
1628     btrfs)
1629         _check_btrfs_filesystem $SCRATCH_DEV
1630         ;;
1631     *)
1632         _check_generic_filesystem $SCRATCH_DEV
1633         ;;
1634     esac
1635 }
1636
1637 _full_fstyp_details()
1638 {
1639      [ -z "$FSTYP" ] && FSTYP=xfs
1640      if [ $FSTYP = xfs ]; then
1641         if [ -d /proc/fs/xfs ]; then
1642             if grep -q 'debug 0' /proc/fs/xfs/stat; then
1643                 FSTYP="$FSTYP (non-debug)"
1644             elif grep -q 'debug 1' /proc/fs/xfs/stat; then
1645                 FSTYP="$FSTYP (debug)"
1646             fi
1647         else
1648             if uname -a | grep -qi 'debug'; then
1649                 FSTYP="$FSTYP (debug)"
1650             else
1651                 FSTYP="$FSTYP (non-debug)"
1652             fi
1653         fi
1654      fi
1655      echo $FSTYP
1656 }
1657
1658 _full_platform_details()
1659 {
1660      os=`uname -s`
1661      host=`hostname -s`
1662      kernel=`uname -r`
1663      platform=`uname -m`
1664      echo "$os/$platform $host $kernel"
1665 }
1666
1667 _setup_udf_scratchdir()
1668 {
1669     [ "$FSTYP" != "udf" ] \
1670         && _fail "setup_udf_testdir: \$FSTYP is not udf"
1671     [ -z "$SCRATCH_DEV" -o ! -b "$SCRATCH_DEV" ] \
1672         && _notrun "this test requires a valid \$SCRATCH_DEV"
1673     [ -z "$SCRATCH_MNT" ] \
1674         && _notrun "this test requires a valid \$SCRATCH_MNT"
1675
1676     # mounted?
1677     if _mount | grep -q $SCRATCH_DEV
1678     then
1679         # if it's mounted, make sure its on $TEST_RW_DIR
1680         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
1681         then
1682             _fail "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
1683         fi
1684         $UMOUNT_PROG $SCRATCH_DEV
1685     fi
1686
1687     _scratch_mkfs
1688     _scratch_mount
1689
1690     testdir=$SCRATCH_MNT
1691 }
1692
1693 _setup_nfs_scratchdir()
1694 {
1695     [ "$FSTYP" != "nfs" ] \
1696         && _fail "setup_nfs_testdir: \$FSTYP is not nfs"
1697     [ -z "$SCRATCH_DEV" ] \
1698         && _notrun "this test requires a valid host fs for \$SCRATCH_DEV"
1699     [ -z "$SCRATCH_MNT" ] \
1700         && _notrun "this test requires a valid \$SCRATCH_MNT"
1701
1702     # mounted?
1703     if _mount | grep -q $SCRATCH_DEV
1704     then
1705         # if it's mounted, make sure its on $SCRATCH_MNT
1706         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
1707         then
1708             _fail "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
1709         fi
1710         $UMOUNT_PROG $SCRATCH_DEV
1711     fi
1712
1713     _scratch_mkfs
1714     _scratch_mount
1715
1716     testdir=$SCRATCH_MNT
1717 }
1718
1719 #
1720 # Warning for UDF and NFS:
1721 # this function calls _setup_udf_scratchdir and _setup_udf_scratchdir
1722 # which actually uses the scratch dir for the test dir.
1723 #
1724 # This was done because testdir was intended to be a persistent
1725 # XFS only partition.  This should eventually change, and treat
1726 # at least local filesystems all the same.
1727 #
1728 _setup_testdir()
1729 {
1730     case $FSTYP in
1731     udf)
1732         _setup_udf_scratchdir
1733         ;;
1734     nfs*)
1735         _setup_nfs_scratchdir
1736         ;;
1737     *)
1738         testdir=$TEST_DIR
1739         ;;
1740     esac
1741 }
1742
1743 _cleanup_testdir()
1744 {
1745     case $FSTYP in
1746     udf)
1747         # umount testdir as it is $SCRATCH_MNT which could be used by xfs next
1748         [ -n "$testdir" ] && $UMOUNT_PROG $testdir
1749         ;;
1750     nfs*)
1751         # umount testdir as it is $SCRATCH_MNT which could be used by xfs next
1752         [ -n "$testdir" ] && $UMOUNT_PROG $testdir
1753         ;;
1754     *)
1755         # do nothing, testdir is $TEST_DIR
1756         :
1757         ;;
1758     esac
1759 }
1760
1761 _link_out_file()
1762 {
1763         if [ -z "$1" -o -z "$2" ]; then
1764                 echo Error must pass src and dst.
1765                 exit
1766         fi
1767         rm -f $2
1768         if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
1769                 ln -s $1.irix $2
1770         elif [ "`uname`" == "Linux" ]; then
1771                 ln -s $1.linux $2
1772         else
1773                 echo Error test $seq does not run on the operating system: `uname`
1774                 exit
1775         fi
1776 }
1777
1778 _die()
1779 {
1780         echo $@
1781         exit 1
1782 }
1783
1784 #takes files, randomdata
1785 _nfiles()
1786 {
1787         f=0
1788         while [ $f -lt $1 ]
1789         do
1790                 file=f$f
1791                 echo > $file
1792                 if [ $size -gt 0 ]; then
1793                     if [ "$2" == "false" ]; then
1794                         dd if=/dev/zero of=$file bs=1024 count=$size 2>&1 | _filter_dd
1795                     else
1796                         dd if=/dev/urandom of=$file bs=1024 count=$size 2>&1 | _filter_dd
1797                     fi
1798                 fi
1799                 let f=$f+1
1800         done
1801 }
1802
1803 # takes dirname, depth, randomdata
1804 _descend()
1805 {
1806         dirname=$1; depth=$2; randomdata=$3
1807         mkdir $dirname  || die "mkdir $dirname failed"
1808         cd $dirname
1809
1810         _nfiles $files $randomdata          # files for this dir and data type
1811
1812         [ $depth -eq 0 ] && return
1813         let deep=$depth-1 # go 1 down
1814
1815         [ $verbose = true ] && echo "descending, depth from leaves = $deep"
1816
1817         d=0
1818         while [ $d -lt $dirs ]
1819         do
1820                 _descend d$d $deep &
1821                 let d=$d+1
1822                 wait
1823         done
1824 }
1825
1826 # Populate a filesystem with inodes for performance experiments
1827 #
1828 # usage: populate [-v] [-n ndirs] [-f nfiles] [-d depth] [-r root] [-s size] [-x]
1829 #
1830 _populate_fs()
1831 {
1832     here=`pwd`
1833     dirs=5          # ndirs in each subdir till leaves
1834     size=0          # sizeof files in K
1835     files=100       # num files in _each_ subdir
1836     depth=2         # depth of tree from root to leaves
1837     verbose=false
1838     root=root       # path of initial root of directory tree
1839     randomdata=false # -x data type urandom or zero
1840
1841     OPTIND=1
1842     while getopts "d:f:n:r:s:v:x" c
1843     do
1844         case $c in
1845         d)      depth=$OPTARG;;
1846         n)      dirs=$OPTARG;;
1847         f)      files=$OPTARG;;
1848         s)      size=$OPTARG;;
1849         v)      verbose=true;;
1850         r)      root=$OPTARG;;
1851         x)      randomdata=true;;
1852         esac
1853     done
1854
1855     _descend $root $depth $randomdata
1856     wait
1857
1858     cd $here
1859
1860     [ $verbose = true ] && echo done
1861 }
1862
1863 # query whether the given file has the given inode flag set
1864 #
1865 _test_inode_flag()
1866 {
1867     flag=$1
1868     file=$2
1869
1870     if which $XFS_IO_PROG >/dev/null; then
1871         if $XFS_IO_PROG -r -c 'lsattr -v' "$file" | grep -q "$flag" ; then
1872             return 0
1873         fi
1874     fi
1875     return 1
1876 }
1877
1878 # query the given files extsize allocator hint in bytes (if any)
1879 #
1880 _test_inode_extsz()
1881 {
1882     file=$1
1883     blocks=""
1884
1885     if which $XFS_IO_PROG >/dev/null; then
1886         blocks=`$XFS_IO_PROG -r -c 'stat' "$file" | \
1887                 awk '/^xattr.extsize =/ { print $3 }'`
1888     fi
1889     [ -z "$blocks" ] && blocks="0"
1890     echo $blocks
1891 }
1892
1893 # scratch_dev_pool should contain the disks pool for the btrfs raid
1894 _require_scratch_dev_pool()
1895 {
1896         local i
1897         if [ -z "$SCRATCH_DEV_POOL" ]; then
1898                 _notrun "this test requires a valid \$SCRATCH_DEV_POOL"
1899         fi
1900
1901         # btrfs test case needs 2 or more scratch_dev_pool; other FS not sure
1902         # so fail it
1903         case $FSTYP in
1904         btrfs)
1905                 if [ "`echo $SCRATCH_DEV_POOL|wc -w`" -lt 2 ]; then
1906                         _notrun "btrfs and this test needs 2 or more disks in SCRATCH_DEV_POOL"
1907                 fi
1908         ;;
1909         *)
1910                 _notrun "dev_pool is not supported by fstype \"$FSTYP\""
1911         ;;
1912         esac
1913
1914         for i in $SCRATCH_DEV_POOL; do
1915                 if [ "`_is_block_dev $i`" = "" ]; then
1916                         _notrun "this test requires valid block disk $i"
1917                 fi
1918                 if [ "`_is_block_dev $i`" = "`_is_block_dev $TEST_DEV`" ]; then
1919                         _notrun "$i is part of TEST_DEV, this test requires unique disks"
1920                 fi
1921                 if _mount | grep -q $i; then
1922                         if ! $UMOUNT_PROG $i; then
1923                             echo "failed to unmount $i - aborting"
1924                             exit 1
1925                         fi
1926                 fi
1927                 # to help better debug when something fails, we remove
1928                 # traces of previous btrfs FS on the dev.
1929                 dd if=/dev/zero of=$i bs=4096 count=100 > /dev/null 2>&1
1930         done
1931 }
1932
1933 # We will check if the device is virtual (eg: loop device) since it does not
1934 # have the delete entry-point. Otherwise SCSI and USB devices are fine. 
1935 _require_deletable_scratch_dev_pool()
1936 {
1937         local i
1938         local x
1939         for i in $SCRATCH_DEV_POOL; do
1940                 x=`echo $i | cut -d"/" -f 3`
1941                 ls -l /sys/class/block/${x} | grep -q "virtual" 
1942                 if [ $? == "0" ]; then
1943                         _notrun "$i is a virtual device which is not deletable"
1944                 fi
1945         done
1946 }
1947
1948 # We check for btrfs and (optionally) features of the btrfs command
1949 _require_btrfs()
1950 {
1951         cmd=$1
1952         _require_command $BTRFS_UTIL_PROG btrfs
1953         if [ -z "$1" ]; then
1954                 return 1;
1955         fi
1956         $BTRFS_UTIL_PROG $cmd --help >/dev/null 2>&1
1957         [ $? -eq 0 ] || _notrun "$BTRFS_UTIL_PROG too old (must support $cmd)"
1958 }
1959
1960 # Check that fio is present, and it is able to execute given jobfile
1961 _require_fio()
1962 {
1963         job=$1
1964
1965         _require_command $FIO_PROG
1966         if [ -z "$1" ]; then
1967                 return 1;
1968         fi
1969
1970         $FIO_PROG --warnings-fatal --showcmd $job >/dev/null 2>&1
1971         [ $? -eq 0 ] || _notrun "$FIO_PROG too old"
1972 }
1973
1974 # Does freeze work on this fs?
1975 _require_freeze()
1976 {
1977         xfs_freeze -f "$TEST_DIR" >/dev/null 2>&1
1978         result=$? 
1979         xfs_freeze -u "$TEST_DIR" >/dev/null 2>&1
1980         [ $result -eq 0 ] || _notrun "$FSTYP does not support freezing"
1981 }
1982
1983 # arg 1 is dev to remove and is output of the below eg.
1984 # ls -l /sys/class/block/sdd | rev | cut -d "/" -f 3 | rev
1985 _devmgt_remove()
1986 {
1987         echo 1 > /sys/class/scsi_device/${1}/device/delete || _fail "Remove disk failed"
1988 }
1989
1990 # arg 1 is dev to add and is output of the below eg.
1991 # ls -l /sys/class/block/sdd | rev | cut -d "/" -f 3 | rev
1992 _devmgt_add()
1993 {
1994         local h
1995         local tdl
1996         # arg 1 will be in h:t:d:l format now in the h and "t d l" format
1997         h=`echo ${1} | cut -d":" -f 1`
1998         tdl=`echo ${1} | cut -d":" -f 2-|sed 's/:/ /g'`
1999
2000         echo ${tdl} >  /sys/class/scsi_host/host${h}/scan || _fail "Add disk failed"
2001 }
2002
2003 _require_fstrim()
2004 {
2005         if [ -z "$FSTRIM_PROG" ]; then
2006                 _notrun "This test requires fstrim utility."
2007         fi
2008 }
2009
2010 _test_batched_discard()
2011 {
2012         if [ $# -ne 1 ]; then
2013                 echo "Usage: _test_batched_discard mnt_point" 1>&2
2014                 exit 1
2015         fi
2016         _require_fstrim
2017         $FSTRIM_PROG ${1} &>/dev/null
2018 }
2019
2020 _require_dumpe2fs()
2021 {
2022         if [ -z "$DUMPE2FS_PROG" ]; then
2023                 _notrun "This test requires dumpe2fs utility."
2024         fi
2025 }
2026
2027 _create_loop_device()
2028 {
2029         file=$1
2030         dev=`losetup -f --show $file` || _fail "Cannot assign $file to a loop device"
2031         echo $dev
2032 }
2033
2034 _destroy_loop_device()
2035 {
2036         dev=$1
2037         losetup -d $dev || _fail "Cannot destroy loop device $dev"
2038 }
2039
2040 _scale_fsstress_args()
2041 {
2042     args=""
2043     while [ $# -gt 0 ]; do
2044         case "$1" in
2045             -n) args="$args $1 $(($2 * $TIME_FACTOR))"; shift ;;
2046             -p) args="$args $1 $(($2 * $LOAD_FACTOR))"; shift ;;
2047             *) args="$args $1" ;;
2048         esac
2049         shift
2050     done
2051     echo $args
2052 }
2053
2054 run_check()
2055 {
2056         echo "# $@" >> $seqres.full 2>&1
2057         "$@" >> $seqres.full 2>&1 || _fail "failed: '$@'"
2058 }
2059
2060 ################################################################################
2061
2062 if [ "$iam" != new ]
2063 then
2064     # make some further configuration checks here
2065
2066     if [ "$TEST_DEV" = ""  ]
2067     then
2068         echo "common.rc: Error: \$TEST_DEV is not set"
2069         exit 1
2070     fi
2071
2072     # if $TEST_DEV is not mounted, mount it now as XFS
2073     if [ -z "`_fs_type $TEST_DEV`" ]
2074     then
2075         # $TEST_DEV is not mounted
2076         if ! _test_mount
2077         then
2078             echo "common.rc: retrying test device mount with external set"
2079             [ "$USE_EXTERNAL" != "yes" ] && export USE_EXTERNAL=yes
2080             if ! _test_mount
2081             then
2082                 echo "common.rc: could not mount $TEST_DEV on $TEST_DIR"
2083                 exit 1
2084             fi
2085         fi
2086     fi
2087
2088     if [ "`_fs_type $TEST_DEV`" != "$FSTYP" ]
2089     then
2090         echo "common.rc: Error: \$TEST_DEV ($TEST_DEV) is not a MOUNTED $FSTYP filesystem"
2091         $DF_PROG $TEST_DEV
2092         exit 1
2093     fi
2094 fi
2095
2096 # make sure this script returns success
2097 /bin/true