Rework last big-sized-inums fix, fix a couple of warnings on 64bit platforms
[xfstests-dev.git] / common.rc
1 ##/bin/sh
2
3 #
4 # Copyright (c) 2000-2003 Silicon Graphics, Inc.  All Rights Reserved.
5
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of version 2 of the GNU General Public License as
8 # published by the Free Software Foundation.
9
10 # This program is distributed in the hope that it would be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 # Further, this software is distributed without any warranty that it is
15 # free of the rightful claim of any third person regarding infringement
16 # or the like.  Any license provided herein, whether implied or
17 # otherwise, applies only to this software file.  Patent licenses, if
18 # any, provided herein do not apply to combinations of this program with
19 # other software, or any other product whatsoever.
20
21 # You should have received a copy of the GNU General Public License along
22 # with this program; if not, write the Free Software Foundation, Inc., 59
23 # Temple Place - Suite 330, Boston MA 02111-1307, USA.
24
25 # Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
26 # Mountain View, CA  94043, or:
27
28 # http://www.sgi.com 
29
30 # For further information regarding this notice, see: 
31
32 # http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 #
34
35 # we need common.config
36 if ! . ./common.config
37 then
38     echo "$iam: failed to source common.config"
39     exit 1
40 fi
41
42 # make sure we have a standard umask
43 umask 022
44
45 # awk
46 AWK_PROG=awk
47 export AWK_PROG
48
49 # we override mount and mkfs.xfs so we can specify extra options
50
51 mount()
52 {
53     case "$*"
54     in
55         *remount*)
56             /bin/mount $*
57             ;;
58         *ext2*)
59             /bin/mount $*
60             ;;
61         *xfs*)
62             /bin/mount $* $MOUNT_OPTIONS
63             ;;
64         *)
65             /bin/mount $*
66             ;;
67     esac
68 }
69
70 _test_mount()
71 {
72     TEST_OPTIONS=""
73     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
74         TEST_OPTIONS="$TEST_OPTIONS -ortdev=$TEST_RTDEV"
75     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
76         TEST_OPTIONS="$TEST_OPTIONS -ologdev=$TEST_LOGDEV"
77     [ -z "$FSTYP" ] && FSTYP=xfs
78     mount -t $FSTYP $TEST_OPTIONS $MOUNT_OPTIONS $* $TEST_DEV $TEST_DIR
79 }
80
81 _scratch_mount_options()
82 {
83     SCRATCH_OPTIONS=""
84     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
85         SCRATCH_OPTIONS="$SCRATCH_OPTIONS -ortdev=$SCRATCH_RTDEV"
86     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
87         SCRATCH_OPTIONS="$SCRATCH_OPTIONS -ologdev=$SCRATCH_LOGDEV"
88     echo $SCRATCH_OPTIONS $MOUNT_OPTIONS $* $SCRATCH_DEV $SCRATCH_MNT
89 }
90
91 _scratch_mount()
92 {
93     SCRATCH_OPTIONS=""
94     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
95         SCRATCH_OPTIONS="$SCRATCH_OPTIONS -ortdev=$SCRATCH_RTDEV"
96     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
97         SCRATCH_OPTIONS="$SCRATCH_OPTIONS -ologdev=$SCRATCH_LOGDEV"
98     [ -z "$FSTYP" ] && FSTYP=xfs
99     mount -t $FSTYP $SCRATCH_OPTIONS $MOUNT_OPTIONS $* $SCRATCH_DEV $SCRATCH_MNT
100 }
101
102 _scratch_mkfs_options()
103 {
104     SCRATCH_OPTIONS=""
105     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
106         SCRATCH_OPTIONS="$SCRATCH_OPTIONS -rrtdev=$SCRATCH_RTDEV"
107     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
108         SCRATCH_OPTIONS="$SCRATCH_OPTIONS -llogdev=$SCRATCH_LOGDEV"
109     echo $SCRATCH_OPTIONS $MKFS_OPTIONS $* $SCRATCH_DEV
110 }
111
112 _scratch_mkfs()
113 {
114     if [ -z "$FSTYP" -o "$FSTYP" = xfs ]; then
115         _scratch_mkfs_xfs $*
116     else
117         /sbin/mkfs.$FSTYP $MKFS_OPTIONS $* $SCRATCH_DEV
118     fi
119 }
120
121 _scratch_mkfs_xfs()
122 {
123     SCRATCH_OPTIONS=""
124     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
125         SCRATCH_OPTIONS="$SCRATCH_OPTIONS -rrtdev=$SCRATCH_RTDEV"
126     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
127         SCRATCH_OPTIONS="$SCRATCH_OPTIONS -llogdev=$SCRATCH_LOGDEV"
128     /sbin/mkfs.xfs -f $SCRATCH_OPTIONS $MKFS_OPTIONS $* $SCRATCH_DEV
129     mkfs_status=$?
130     [ "$USE_BIG_LOOPFS" = yes ] && \
131         ./tools/ag-wipe -c $SCRATCH_DEV >/dev/null
132     return $mkfs_status
133 }
134
135 _scratch_xfs_db_options()
136 {
137     SCRATCH_OPTIONS=""
138     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
139         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
140     echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
141 }
142
143 _scratch_xfs_logprint()
144 {
145     SCRATCH_OPTIONS=""
146     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
147         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
148     /usr/sbin/xfs_logprint $SCRATCH_OPTIONS $* $SCRATCH_DEV
149 }
150
151 _scratch_xfs_repair()
152 {
153     SCRATCH_OPTIONS=""
154     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
155         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
156     [ "$USE_BIG_LOOPFS" = yes ] && SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
157     /sbin/xfs_repair $SCRATCH_OPTIONS $* $SCRATCH_DEV
158 }
159
160 _get_pids_by_name()
161 {
162     if [ $# -ne 1 ]
163     then
164         echo "Usage: _get_pids_by_name process-name" 1>&2
165         exit 1
166     fi
167
168     # Algorithm ... all ps(1) variants have a time of the form MM:SS or
169     # HH:MM:SS before the psargs field, use this as the search anchor.
170     #
171     # Matches with $1 (process-name) occur if the first psarg is $1
172     # or ends in /$1 ... the matching uses sed's regular expressions,
173     # so passing a regex into $1 will work.
174
175     ps $PS_ALL_FLAGS \
176     | sed -n \
177         -e 's/$/ /' \
178         -e 's/[         ][      ]*/ /g' \
179         -e 's/^ //' \
180         -e 's/^[^ ]* //' \
181         -e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
182         -e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
183 }
184
185 # fqdn for localhost
186 #
187 _get_fqdn()
188 {
189     host=`hostname`
190     nslookup $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
191 }
192
193 # fix malloc libs output
194 #
195 _fix_malloc()
196 {
197     # filter out the Electric Fence notice
198     perl -e '
199         while (<>) {
200             if (defined $o && /^\s+Electric Fence/) {
201                 chomp($o);
202                 print "$o";
203                 undef $o;
204                 next;
205             }
206             print $o if (defined $o);
207
208             $o=$_;
209         }
210         print $o if (defined $o);
211     '
212 }
213
214 # check if run as root
215 #
216 _need_to_be_root()
217 {
218     id=`id | sed -e 's/(.*//' -e 's/.*=//'`
219     if [ "$id" -ne 0 ]
220     then
221         echo "Arrgh ... you need to be root (not uid=$id) to run this test"
222         exit 1
223     fi
224 }
225
226
227 #
228 # _df_device : get an IRIX style df line for a given device 
229 #
230 #       - returns "" if not mounted
231 #       - returns fs type in field two (ala IRIX)
232 #       - joins line together if split by fancy df formatting
233 #       - strips header etc
234 #
235
236 _df_device()
237 {
238     if [ $# -ne 1 ]
239     then
240         echo "Usage: _df_device device" 1>&2
241         exit 1
242     fi
243     
244     df -T 2>/dev/null | $AWK_PROG -v what=$1 '
245         match($1,what) && NF==1 { 
246             v=$1
247             getline
248             print v, $0
249             exit
250         }
251         match($1,what) {
252             print
253             exit
254         }
255     '
256 }
257
258 #
259 # _df_dir : get an IRIX style df line for device where a directory resides
260 #
261 #       - returns fs type in field two (ala IRIX)
262 #       - joins line together if split by fancy df formatting
263 #       - strips header etc
264 #
265
266 _df_dir()
267 {
268     if [ $# -ne 1 ]
269     then
270         echo "Usage: _df_dir device" 1>&2
271         exit 1
272     fi
273     
274     df -T $1 2>/dev/null | $AWK_PROG -v what=$1 '
275         NR == 2 && NF==1 { 
276             v=$1
277             getline 
278             print v, $0;
279             exit 0
280         }
281         NR == 2 {
282             print;
283             exit 0
284         }
285         {}
286     '
287     # otherwise, nada
288 }
289
290 # return percentage used disk space for mounted device
291
292 _used()
293 {
294     if [ $# -ne 1 ]
295     then
296         echo "Usage: _used device" 1>&2
297         exit 1
298     fi
299     
300     _df_device $1 | $AWK_PROG '{ sub("%", "") ; print $6 }'
301 }
302
303 # return the FS type of a mounted device
304 #
305 _fs_type()
306 {
307     if [ $# -ne 1 ]
308     then
309         echo "Usage: _fs_type device" 1>&2
310         exit 1
311     fi
312     
313     _df_device $1 | $AWK_PROG '{ print $2 }'
314 }
315
316 # return the FS mount options of a mounted device
317 #
318 _fs_options()
319 {
320     if [ $# -ne 1 ]
321     then
322         echo "Usage: _fs_options device" 1>&2
323         exit 1
324     fi
325     
326     $AWK_PROG -v dev=$1 '
327         match($1,dev) { print $4 }
328     ' </proc/mounts
329     
330 }
331
332 # returns device number if a file is a block device
333 #
334 _is_block_dev()
335 {
336     if [ $# -ne 1 ]
337     then
338         echo "Usage: _is_block_dev dev" 1>&2
339         exit 1
340     fi
341     
342     [ -b $1 ] && src/lstat64 $1 | $AWK_PROG '/Device type:/ { print $9 }'
343 }
344
345 # Do a command, log it to $seq.full, optionally test return status
346 # and die if command fails. If called with one argument _do executes the
347 # command, logs it, and returns its exit status. With two arguments _do
348 # first prints the message passed in the first argument, and then "done"
349 # or "fail" depending on the return status of the command passed in the
350 # second argument. If the command fails and the variable _do_die_on_error
351 # is set to "always" or the two argument form is used and _do_die_on_error
352 # is set to "message_only" _do will print an error message to
353 # $seq.out and exit.
354
355 _do()
356 {
357   if [ $# -eq 1 ]; then
358     _cmd=$1
359   elif [ $# -eq 2 ]; then
360     _note=$1
361     _cmd=$2
362     echo -n "$_note... "
363   else
364     echo "Usage: _do [note] cmd" 1>&2
365     status=1; exit
366   fi
367
368   (eval "echo '---' \"$_cmd\"") >>$seq.full
369   (eval "$_cmd") >$tmp._out 2>&1; ret=$?
370   cat $tmp._out | _fix_malloc >>$seq.full
371   if [ $# -eq 2 ]; then
372     if [ $ret -eq 0 ]; then
373       echo "done"
374     else
375       echo "fail"
376     fi
377   fi
378   if [ $ret -ne 0  ] \
379      && [ "$_do_die_on_error" = "always" \
380          -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
381   then
382       [ $# -ne 2 ] && echo
383       eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
384       status=1; exit
385   fi
386
387   return $ret
388 }
389
390 # bail out, setting up .notrun file
391
392 _notrun()
393 {
394     echo "$*" >$seq.notrun
395     echo "$seq not run: $*"
396     status=0
397     exit
398 }
399
400 # just plain bail out
401
402 _fail()
403 {
404     echo "$*" | tee -a $seq.full
405     echo "(see $seq.full for details)"
406     status=1
407     exit 1
408 }
409
410 # this test needs a scratch partition - check we're ok & unmount it
411
412 _require_scratch()
413 {
414     if [ -z "$SCRATCH_DEV" -o "`_is_block_dev $SCRATCH_DEV`" = "" ]
415     then
416         _notrun "this test requires a valid \$SCRATCH_DEV"
417     fi
418     
419     if [ "`_is_block_dev $SCRATCH_DEV`" = "`_is_block_dev $TEST_DEV`" ]
420     then
421         _notrun "this test requires a valid \$SCRATCH_DEV"
422     fi
423     
424     # mounted?
425     if mount | grep -q $SCRATCH_DEV
426     then 
427         # if it's mounted, make sure its on $SCRATCH_MNT
428         if ! mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
429         then 
430             echo "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
431             exit 1
432         fi
433         # and then unmount it
434         if ! umount $SCRATCH_DEV
435         then
436             echo "failed to unmount $SCRATCH_DEV"
437             exit 1
438         fi
439     fi
440 }
441
442 # this test needs a logdev 
443
444 _require_logdev()
445 {
446     [ -z "$SCRATCH_LOGDEV" -o ! -b "$SCRATCH_LOGDEV" ] && \
447         _notrun "This test requires a valid \$SCRATCH_LOGDEV" 
448     [ "$USE_EXTERNAL" != yes ] && \
449         _notrun "This test requires USE_EXTERNAL to be enabled"
450
451     # ensure its not mounted
452     umount $SCRATCH_LOGDEV 2>/dev/null
453 }
454
455 # this test requires loopback device support
456
457 _require_loop()
458 {
459     modprobe loop >/dev/null 2>&1
460     if grep loop /proc/devices >/dev/null 2>&1
461     then
462         :
463     else
464         _notrun "This test requires loopback device support"
465     fi
466 }
467
468 # this test requires that (large) loopback device files are not in use
469
470 _require_nobigloopfs()
471 {
472     [ "$USE_BIG_LOOPFS" = yes ] && \
473         _notrun "Large filesystem testing in progress, skipped this test"
474 }
475
476 # this test requires that external log/realtime devices are not in use
477
478 _require_nonexternal()
479 {
480     [ "$USE_EXTERNAL" = yes ] && \
481         _notrun "External device testing in progress, skipped this test"
482 }
483
484 # check that a FS is mounted as XFS. if so, return mount point
485 #
486 _xfs_mounted()
487 {
488     if [ $# -ne 1 ]
489     then
490         echo "Usage: _xfs_mounted device" 1>&2
491         exit 1
492     fi
493     
494     device=$1
495     
496     if mount | grep $device | $AWK_PROG '
497         /type xfs/  { print $3 ; exit 0 }
498         END         { exit 1 }
499     '
500     then
501         echo "_xfs_mounted: $device is not a mounted XFS FS"
502         exit 1
503     fi
504 }
505
506
507 # remount a FS to a new mode (ro or rw)
508 #
509 _remount()
510 {
511     if [ $# -ne 2 ]
512     then
513         echo "Usage: _remount device ro/rw" 1>&2
514         exit 1
515     fi
516     device=$1
517     mode=$2
518     
519     if ! mount -o remount,$mode $device
520     then
521         echo "_remount: failed to remount filesystem on $device as $mode"
522         exit 1
523     fi
524     
525     # we might like to check here later
526     #options=`_fs_options $device`
527
528 }
529
530 # run xfs_check and friends on a FS. 
531 #
532 # if the filesystem is mounted, it's either remounted ro before being
533 # checked or it's unmounted and then remounted
534 #
535
536 USE_REMOUNT=0
537
538 _check_filesystem()
539 {
540     if [ $# -ne 1 -a $# -ne 2 ]
541     then
542         echo "Usage: _check_fs device [logdevice]" 1>&2
543         exit 1
544     fi
545     
546     device=$1
547     if [ $# -eq 2 -a "$2" != "" ]; then
548         extra_log_options="-l$2"
549         extra_mount_options="-ologdev=$2"
550     fi
551
552     [ "$FSTYP" != xfs ] && return 0
553     testoption=""
554     [ "$USE_BIG_LOOPFS" = yes ] && testoption=-t
555
556     type=`_fs_type $device`
557     ok=1
558     
559     if [ "$type" = "xfs" ]
560     then
561         # mounted... 
562         
563         if [ $USE_REMOUNT -eq 0 ]
564         then
565             mountpoint=`_xfs_mounted $device`
566             umount $device
567         else
568             _remount $device ro
569         fi
570     fi
571
572     /usr/sbin/xfs_logprint -t $device $extra_log_options 2>&1 \
573                 | tee $tmp.fs_check | grep -q "<CLEAN>"
574     if [ $? -ne 0 ]
575     then
576         echo "_check_fs: filesystem on $device has dirty log (see $seq.full)"
577         
578         echo "_check_fs: filesystem on $device has dirty log"   >>$seq.full
579         echo "*** xfs_logprint -t output ***"                   >>$seq.full
580         cat $tmp.fs_check                                       >>$seq.full
581         echo "*** end xfs_logprint output"                      >>$seq.full
582         
583         ok=0
584     fi
585
586     /usr/sbin/xfs_check $testoption $device 2>&1 | _fix_malloc >$tmp.fs_check 
587     if [ -s $tmp.fs_check ]
588     then
589         echo "_check_fs: filesystem on $device is inconsistent (c) (see $seq.full)"
590         
591         echo "_check_fs: filesystem on $device is inconsistent" >>$seq.full
592         echo "*** xfs_check output ***"                         >>$seq.full
593         cat $tmp.fs_check                                       >>$seq.full
594         echo "*** end xfs_check output"                         >>$seq.full
595         
596         ok=0
597     fi
598     
599     if ! /sbin/xfs_repair -n $device $extra_log_options >$tmp.fs_check 2>&1
600     then
601         echo "_check_fs: filesystem on $device is inconsistent (r) (see $seq.full)"
602         
603         echo "_check_fs: filesystem on $device is inconsistent" >>$seq.full
604         echo "*** xfs_repair -n output ***"                     >>$seq.full
605         cat $tmp.fs_check | _fix_malloc                         >>$seq.full
606         echo "*** end xfs_repair output"                        >>$seq.full
607         
608         ok=0
609     fi
610     rm -f $tmp.fs_check
611     
612     if [ $ok -eq 0 ]
613     then
614         echo "*** mount output ***"                             >>$seq.full
615         mount                                                   >>$seq.full
616         echo "*** end mount output"                             >>$seq.full
617     elif [ "$type" = "xfs" ]
618     then
619         # mounted... 
620         if [ $USE_REMOUNT -eq 0 ]
621         then
622             if ! mount -t xfs $extra_mount_options $device $mountpoint
623             then
624                 echo "!!! failed to remount $device on $mountpoint"
625                 ok=0
626             fi
627         else
628             _remount $device rw
629         fi
630     fi
631     
632     [ $ok -eq 0 ] && exit 1
633     return 0    
634 }
635
636 _check_test_fs()
637 {
638     TEST_LOG=""
639     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
640         TEST_LOG="$TEST_LOGDEV"
641     _check_filesystem $TEST_DEV $TEST_LOG
642 }
643
644 _check_scratch_fs()
645 {
646     SCRATCH_LOG=""
647     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
648         SCRATCH_LOG="$SCRATCH_LOGDEV"
649     _check_filesystem $SCRATCH_DEV $SCRATCH_LOG
650 }
651
652 ################################################################################
653
654 [ -d /usr/bsd ] && PATH=$PATH:/usr/bsd
655 [ -d /usr/freeware/bin ] && PATH=$PATH:/usr/freeware/bin
656
657 if [ "$iam" != new -a "$iam" != bench ]
658 then
659
660     # make some further configuration checks here
661     
662     if [ "$TEST_DEV" = ""  ]
663     then
664         echo "common.rc: Error: \$TEST_DEV is not set"
665         exit 1
666     fi
667     
668     # if $TEST_DEV is not mounted, mount it now as XFS
669     if [ -z "`_fs_type $TEST_DEV`" ]
670     then
671         # $TEST_DEV is not mounted
672         if ! _test_mount
673         then
674             echo "common.rc: retrying test device mount with external set"
675             [ "$USE_EXTERNAL" != "yes" ] && export USE_EXTERNAL=yes
676             if ! _test_mount
677             then
678                 echo "common.rc: could not mount $TEST_DEV on $TEST_DIR"
679                 exit 1
680             fi
681         fi
682     fi
683     
684     if [ "`_fs_type $TEST_DEV`" != "xfs" ]
685     then
686         echo "common.rc: Error: \$TEST_DEV ($TEST_DEV) is not a MOUNTED XFS filesystem"
687         df -T $TEST_DEV
688         exit 1
689     fi
690
691 fi
692
693 # check for some required binaries on our $PATH
694 #
695 for exec in mkfs.xfs xfs_logprint xfs_check xfs_repair xfs_db
696 do
697     if which $exec >/dev/null 2>&1 
698     then
699         :
700     else
701         echo "common.rc: cannot find $exec on \$PATH=$PATH"
702         exit 1
703     fi
704 done
705
706 # make sure this script returns success
707 /bin/true