fsx: Replace use of bzero() with memset()
[xfstests-dev.git] / common.dump
1 #/bin/sh
2 #
3 # Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # Functions useful for xfsdump/xfsrestore tests
6 #
7
8 # --- initializations ---
9 rm -f $here/$seq.full
10
11 if [ -n "$DEBUGDUMP" ]; then
12         _dump_debug=-v4
13         _restore_debug=-v4
14         _invutil_debug=-d
15
16         # Use dump/restore in qa directory (copy them here) for debugging
17         export PATH="$here:$PATH"
18         export XFSDUMP_PROG="`set_prog_path xfsdump`"
19         export XFSRESTORE_PROG="`set_prog_path xfsrestore`"
20         export XFSINVUTIL_PROG="`set_prog_path xfsinvutil`"
21         [ -x $here/xfsdump ]    && echo "Using xfstests' xfsdump for debug"
22         [ -x $here/xfsrestore ] && echo "Using xfstests' xfsrestore for debug"
23         [ -x $here/xfsinvutil ] && echo "Using xfstests' xfsinvutil for debug"
24 fi
25
26 [ "$XFSDUMP_PROG" = "" ]    && _notrun "xfsdump not found"
27 [ "$XFSRESTORE_PROG" = "" ] && _notrun "xfsrestore not found"
28 [ "$XFSINVUTIL_PROG" = "" ] && _notrun "xfsinvutil not found"
29
30 # status returned for not run tests
31 NOTRUNSTS=2
32
33 # name those directories
34 dump_file=$tmp.dumpfile
35 # dump_file=$here/dumpfile #TEMP OVERRIDE DUMP FILE
36 dump_sdir=dumpdir
37 dump_dir=$SCRATCH_MNT/$dump_sdir
38 restore_sdir=restoredir
39 restore_dir=$SCRATCH_MNT/$restore_sdir
40 multi=3
41 dumptape=$TAPE_DEV
42 media_label="stress_tape_media"
43 session_label="stress_$seq"
44
45 nobody=4 # define this uid/gid as a number
46 do_quota_check=true # do quota check if quotas enabled
47
48 _need_to_be_root
49
50 # install our cleaner
51 trap "_cleanup; exit \$status" 0 1 2 3 15
52
53 # start inventory from a known base - move it aside for test
54 for dir in /var/xfsdump/inventory /var/lib/xfsdump/inventory; do
55     if [ -d $dir ]; then
56         [ -d $dir.$seq ] && rm -rf $dir.$seq
57         mv $dir $dir.$seq
58     fi
59 done
60
61 have_mtvariable=false
62 [ `uname` = "Linux" ] && have_mtvariable=true
63
64 #
65 # do a remote/local mt
66 #
67 _mt()
68 {
69     op=$1
70     if _isrmt; then
71         # REMOTE
72         _rmtdev=`echo $dumptape | $AWK_PROG -F: '{print $2}'`
73
74         if echo $dumptape | grep '@' >/dev/null; then
75             _spec=`echo $dumptape | $AWK_PROG -F: '{print $1}'`
76             _rmtuser=`echo $_spec | $AWK_PROG -F@ '{print $1}'`
77             _rmthost=`echo $_spec | $AWK_PROG -F@ '{print $2}'`
78             rsh -n -l $_rmtuser $_rmthost "mt -t $_rmtdev $op"
79         else
80             _rmthost=`echo $dumptape | $AWK_PROG -F: '{print $1}'`
81             rsh -n $_rmthost "mt -t $_rmtdev $op"
82         fi
83     else
84         #LOCAL
85         mt -t $dumptape $op
86     fi
87 }
88
89 _check_onl()
90 {
91     _limit=10
92     i=0
93     while [ $i -lt $_limit ]; do
94         echo "Checking online..." >>$here/$seq.full
95         if _mt status >$tmp.status 2>&1; then
96             break;
97         else
98             sleep 1
99         fi
100         let i=$i+1
101     done
102
103
104     if [ $i -eq $_limit ]; then
105         echo "ERROR: mt -f $dumptape failed"
106         cat $tmp.status
107
108         echo "mt -f $dumptape failed" >$seq.notrun
109         status=$NOTRUNSTS
110         exit
111     fi
112
113
114     if egrep -i 'onl|ready' $tmp.status | grep -iv 'not ready' >/dev/null; then
115         :
116     else
117         echo "ERROR: $dumptape is not online"
118         cat $tmp.status
119
120         echo "dumptape, $dumptape, is not online" >$seq.notrun
121         status=$NOTRUNSTS
122         exit
123     fi
124 }
125
126 _wait_tape()
127 {
128     echo "Wait for tape, $dumptape, ..." >>$here/$seq.full
129
130     i=0
131     while [ $i -lt 20 ]; do
132         echo "Checking status..." >>$here/$seq.full
133         if _mt status 2>&1 | tee -a $here/$seq.full | egrep -i "onl|ready" >/dev/null; then
134             break;
135         else
136             sleep 1
137         fi
138         let i=$i+1
139     done
140 }
141
142 #
143 # Keep trying so we know we really have rewound
144 #
145 _rewind()
146 {
147     echo "Initiate rewind..." >>$here/$seq.full
148     _wait_tape
149     _mt rewind >/dev/null
150     _wait_tape
151 }
152
153 #
154 # Do a custom erase because:
155 # (i) some machines don't support it
156 # (ii) some machines take forever to do it
157 #
158 _erase_soft()
159 {
160     echo "Erasing tape" | tee -a $here/$seq.full
161     _rewind
162     _mt weof 3
163     _rewind
164 }
165
166 _erase_hard()
167 {
168     echo "Erasing tape" | tee -a $here/$seq.full
169     _mt erase
170 }
171
172 _isrmt()
173 {
174     echo $dumptape | grep ':' >/dev/null
175 }
176
177 #
178 # Get tape ready
179 #
180 _set_variable()
181 {
182     $have_mtvariable || return
183
184     if _isrmt; then
185         :
186     else
187         # LOCAL
188         echo "Put scsi tape driver into variable block size mode"
189         mt -f $dumptape setblk 0
190     fi
191 }
192
193 _require_tape()
194 {
195     dumptape=$1
196
197     if [ -z "$dumptape" -o "@" == "$dumptape" ]; then
198         echo "This test requires a dump tape - none was specified"
199         echo "No dump tape specified" >$seq.notrun
200         status=$NOTRUNSTS
201         exit
202     fi
203
204     _check_onl
205     _set_variable
206 }
207
208 _wipe_fs()
209 {
210     _require_scratch
211
212     _scratch_mkfs_xfs >>$here/$seq.full || _fail "mkfs failed"
213     _scratch_mount >>$here/$seq.full || _fail "mount failed"
214 }
215
216 #
217 # Cleanup created dirs and files
218 # Called by trap
219 #
220 _cleanup()
221 {
222     cd $here
223     rm -f $tmp.*
224
225     if [ -n "$DEBUGDUMP" ]; then
226         # save it for inspection
227         for dir in /var/xfsdump/inventory /var/lib/xfsdump/inventory; do
228             [ -d $dir ] || continue
229             tar -cvf $seq.inventory.tar $dir
230             ls -nR $dir >$seq.inventory.ls
231         done
232     fi
233
234     # put inventory dir back
235     for dir in /var/xfsdump/inventory /var/lib/xfsdump/inventory; do
236         [ -d $dir.$seq ] || continue
237         rm -rf $dir             # get rid of new one
238         mv $dir.$seq $dir
239     done
240
241     if [ $status -ne $NOTRUNSTS ]; then
242         # Sleep added to stop _check_scratch_fs from complaining that the
243         # scratch_dev is still busy
244         sleep 10
245
246         _check_scratch_fs
247     fi
248 }
249
250 #
251 # ensure that bulkstat data will
252 # match with incore data
253 # by forcing disk data to be written out
254 #
255 _stable_fs()
256 {
257     _saveddir=`pwd`; cd /
258     umount $SCRATCH_MNT >>$here/$seq.full || _fail "unmount failed"
259     _scratch_mount >>$here/$seq.full || _fail "mount failed"
260     cd $_saveddir
261 }
262
263 #
264 # Run fsstress to create a mixture of
265 # files,dirs,links,symlinks
266 #
267 # Pinched from test 013.
268 #
269 _create_dumpdir_stress()
270 {
271     echo "Creating directory system to dump using fsstress."
272
273     _wipe_fs
274
275     _param="-f link=10 -f creat=10 -f mkdir=10 -f truncate=5 -f symlink=10"
276     _count=200
277     rm -rf $dump_dir
278     if ! mkdir $dump_dir; then
279         echo "    failed to mkdir $dump_dir"
280         status=1
281         exit
282     fi
283     echo ""
284     echo "-----------------------------------------------"
285     echo "fsstress : $_param"
286     echo "-----------------------------------------------"
287     if ! $here/ltp/fsstress $_param -s 1 $FSSTRESS_AVOID -n $_count -d $dump_dir >$tmp.out 2>&1
288     then
289         echo "    fsstress (count=$_count) returned $? - see $here/$seq.full"
290
291         echo "--------------------------------------"       >>$here/$seq.full
292         echo "output from fsstress:"                        >>$here/$seq.full
293         echo "--------------------------------------"       >>$here/$seq.full
294         cat $tmp.out                                        >>$here/$seq.full
295         status=1
296     fi
297
298     _stable_fs
299 }
300
301 _mk_fillconfig1()
302 {
303     cat <<End-of-File >$tmp.config
304 # pathname      size in bytes   owner   group
305 #
306 small           10      $nobody $nobody
307 big             102400  daemon  sys
308 sub/small       10      bin     bin
309 sub/big         102400  $nobody sys
310 #
311 sub/a           1       $nobody $nobody
312 sub/b           2       $nobody $nobody
313 sub/c           4       $nobody $nobody
314 sub/d           8       $nobody $nobody
315 sub/e           16      $nobody $nobody
316 sub/f           32      $nobody $nobody
317 sub/g           64      $nobody $nobody
318 sub/h           128     $nobody $nobody
319 sub/i           256     $nobody $nobody
320 sub/j           512     $nobody $nobody
321 sub/k           1024    $nobody $nobody
322 sub/l           2048    $nobody $nobody
323 sub/m           4096    $nobody $nobody
324 sub/n           8192    $nobody $nobody
325 #
326 sub/a00         100     $nobody $nobody
327 sub/b00         200     $nobody $nobody
328 sub/c00         400     $nobody $nobody
329 sub/d00         800     $nobody $nobody
330 sub/e00         1600    $nobody $nobody
331 sub/f00         3200    $nobody $nobody
332 sub/g00         6400    $nobody $nobody
333 sub/h00         12800   $nobody $nobody
334 sub/i00         25600   $nobody $nobody
335 sub/j00         51200   $nobody $nobody
336 sub/k00         102400  $nobody $nobody
337 sub/l00         204800  $nobody $nobody
338 sub/m00         409600  $nobody $nobody
339 sub/n00         819200  $nobody $nobody
340 #
341 sub/a000        1000    $nobody $nobody
342 sub/e000        16000   $nobody $nobody
343 sub/h000        128000  $nobody $nobody
344 sub/k000        1024000 $nobody $nobody
345 End-of-File
346 }
347
348 _mk_fillconfig2()
349 {
350     cat <<End-of-File >$tmp.config
351 # pathname      size in bytes
352 #
353 smalll          10      $nobody $nobody
354 biggg           102400  $nobody $nobody
355 sub/smalll      10      $nobody $nobody
356 sub/biggg       102400  $nobody $nobody
357 End-of-File
358 }
359
360 _mk_fillconfig_perm()
361 {
362     # dir_guid: ugo=rwx,g+s on dir is for IRIX chmod(1)
363
364     cat <<End-of-File >$tmp.config
365 # pathname      size/dir  user group mode
366 #
367 file_suid       10      $nobody $nobody 04777
368 file_guid       10      $nobody $nobody 02777
369 file_sticky     10      $nobody $nobody 01777
370 file_mix1       10      $nobody $nobody 761
371 file_mix2       10      $nobody $nobody 642
372 dir_suid        d       $nobody $nobody 04777
373 dir_guid        d       $nobody $nobody ugo=rwx,g+s
374 dir_sticky      d       $nobody $nobody 01777
375 dir_mix1        d       $nobody $nobody 761
376 dir_mix2        d       $nobody $nobody 642
377 End-of-File
378 }
379
380 _mk_fillconfig_ea()
381 {
382     cat <<End-of-File >$tmp.config
383 # pathname      size    user    group    perm   name value namespace
384 #
385 smalll          10      $nobody $nobody  777    attr1 some_text   user
386 biggg           102400  $nobody $nobody  777    attr2 some_text2  root
387 sub/smalll      10      $nobody $nobody  777    attr3 some_text3  user
388 sub/biggg       102400  $nobody $nobody  777    attr4 some_text4  root
389 dir             d       $nobody $nobody  777    attr5 dir_text    user
390 #
391 # Add more files so that there are more than the number
392 # of streams.
393 # There are bugs in dump/restore for # non-dir files < # streams
394 # It can be tested in another configuration.
395 # It is a pathalogical case.
396 #
397 sub/a           1       $nobody $nobody
398 sub/b           2       $nobody $nobody
399 sub/c           4       $nobody $nobody
400 sub/d           8       $nobody $nobody
401 sub/e           16      $nobody $nobody
402 sub/f           32      $nobody $nobody
403 sub/g           64      $nobody $nobody
404 sub/h           128     $nobody $nobody
405 sub/i           256     $nobody $nobody
406 sub/j           512     $nobody $nobody
407 sub/k           1024    $nobody $nobody
408 sub/l           2048    $nobody $nobody
409 sub/m           4096    $nobody $nobody
410 sub/n           8192    $nobody $nobody
411 End-of-File
412 }
413
414 #
415 # extended file attribute flags
416 #
417 _mk_fillconfig_xattr()
418 {
419     cat <<End-of-File >$tmp.config
420 # pathname      size    user    group    perm   name
421 #
422 xflag_realtime  10      $nobody $nobody  777    XFS_XFLAG_REALTIME
423 xflag_prealloc  10      $nobody $nobody  777    XFS_XFLAG_PREALLOC
424 xflag_immutable 10      $nobody $nobody  777    XFS_XFLAG_IMMUTABLE
425 xflag_append    10      $nobody $nobody  777    XFS_XFLAG_APPEND
426 xflag_sync      10      $nobody $nobody  777    XFS_XFLAG_SYNC
427 xflag_noatime   10      $nobody $nobody  777    XFS_XFLAG_NOATIME
428 xflag_nodump    10      $nobody $nobody  777    XFS_XFLAG_NODUMP
429 xflag_hasattr   10      $nobody $nobody  777    XFS_XFLAG_HASATTR
430 End-of-File
431 }
432
433 #
434 # Create a bunch of directories/files of different sizes
435 # filled with data.
436 #
437 # Pinched from test 001.
438 #
439 _do_create_dumpdir_fill()
440 {
441     echo "Creating directory system to dump using src/fill."
442
443     mkdir -p $dump_dir || _fail "cannot mkdir \"$dump_dir\""
444     cd $dump_dir
445
446     $verbose && echo -n "Setup "
447     sed -e '/^#/d' $tmp.config \
448     | while read file nbytes owner group perms ea_name ea_value namespace
449     do
450         if [ $nbytes = "d" ]; then
451             # create a directory
452             dir=$file
453             if [ ! -d $dir ]
454             then
455                 if mkdir $dir
456                 then
457                     :
458                 else
459                     $verbose && echo
460                     echo "Error: cannot mkdir \"$dir\""
461                     exit 1
462                 fi
463             fi
464         else
465             # create a directory/file
466             dir=`dirname $file`
467             if [ "$dir" != "." ]
468             then
469                 if [ ! -d $dir ]
470                 then
471                     if mkdir $dir
472                     then
473                         :
474                     else
475                         $verbose && echo
476                         echo "Error: cannot mkdir \"$dir\""
477                         exit 1
478                     fi
479                 fi
480             fi
481             rm -f $file
482             if $here/src/fill $file $file $nbytes
483             then
484                 :
485             else
486                 $verbose && echo
487                 echo "Error: cannot create \"$file\""
488                 exit 1
489             fi
490         fi
491         if [ -n "$owner" -a -n "$group" ]; then
492             chown $owner.$group $file
493         fi
494         if [ -n "$perms" ]; then
495             chmod $perms $file
496         fi
497
498         # extended attributes (EA)
499         if [ -n "$ea_name" -a -n "$ea_value" ]; then
500             if [ "X$namespace" = "Xroot" ]; then
501                 attr -R -s $ea_name -V $ea_value $file
502             else
503                 attr -s $ea_name -V $ea_value $file
504             fi
505         # extended file attribute flags - no value - NOT EAs
506         elif [ -n "$ea_name" -a -z "$ea_value" ]; then
507             # set the flag
508             # TODO XXX
509             # use xfs_io to send the ioctl
510             :
511         fi
512         $verbose && echo -n "."
513     done
514     $verbose && echo
515
516     cd $here
517 }
518
519 _create_dumpdir_largefile()
520 {
521     _wipe_fs
522     mkdir -p $dump_dir || _fail "cannot mkdir \"$dump_dir\""
523     _largesize=4294967297
524     _largefile=$dump_dir/largefile
525     echo "dd a largefile at offset $_largesize"
526     POSIXLY_CORRECT=yes \
527     dd if=/dev/zero of=$_largefile bs=1 seek=$_largesize count=10 2>&1
528     _stable_fs
529 }
530
531 _create_dumpdir_fill()
532 {
533     _wipe_fs
534     _mk_fillconfig1
535     _do_create_dumpdir_fill
536     _stable_fs
537 }
538
539 _create_dumpdir_fill2()
540 {
541     _wipe_fs
542     _mk_fillconfig2
543     _do_create_dumpdir_fill
544     _stable_fs
545 }
546
547 _create_dumpdir_fill_perm()
548 {
549     _wipe_fs
550     _mk_fillconfig_perm
551     _do_create_dumpdir_fill
552     _stable_fs
553 }
554
555 _create_dumpdir_fill_ea()
556 {
557     _wipe_fs
558     _mk_fillconfig_ea
559     _do_create_dumpdir_fill
560     _stable_fs
561 }
562
563
564 #
565 # Append a subset of the fill'ed files
566 # So we can see if just these get dumped on an incremental
567 #
568 _append_dumpdir_fill()
569 {
570     cd $dump_dir
571     cat <<End-of-File >$tmp.config
572 # pathname
573 #
574 small
575 sub/big
576 #
577 sub/a
578 sub/c
579 sub/e
580 End-of-File
581     sed -e '/^#/d' $tmp.config \
582     | while read file
583     do
584         echo 'Extra text' >>$file
585     done
586
587     cd $here
588     _stable_fs
589 }
590
591 _do_create_dump_symlinks()
592 {
593     echo "Creating directory system of symlinks to dump."
594
595     mkdir -p $dump_dir || _fail "cannot mkdir \"$dump_dir\""
596     cd $dump_dir
597
598     $verbose && echo -n "Setup "
599     sed -e '/^#/d' $tmp.config \
600     | while read file nbytes owner group owner2 group2 perms perms2
601     do
602         dir=`dirname $file`
603         if [ "$dir" != "." ]
604         then
605             if [ ! -d $dir ]
606             then
607                 if mkdir $dir
608                 then
609                     :
610                 else
611                     $verbose && echo
612                     echo "Error: cannot mkdir \"$dir\""
613                     exit 1
614                 fi
615             fi
616         fi
617         rm -f $file
618         touch $file
619
620         # Do chmod on symlink using umask.
621         # This won't do the right thing as it subtracts permissions.
622         # However, I don't care, as long as I get some different perms
623         # for testing.
624         if [ -n "$perms2" ]; then
625             omask=`umask`
626             umask $perms2
627         fi
628         ln -s $file $file-link
629         if [ -n "$perms2" ]; then
630             umask $omask
631         fi
632
633         if [ -n "$owner" -a -n "$group" ]; then
634             chown $owner.$group $file
635         fi
636         if [ -n "$owner" -a -n "$group" ]; then
637             chown -h $owner.$group $file-link
638         fi
639         if [ -n "$perms" ]; then
640             chmod $perms $file
641         fi
642         $verbose && echo -n "."
643     done
644     $verbose && echo
645
646     cd $here
647 }
648
649 _mk_symlink_config()
650 {
651     cat <<End-of-File >$tmp.config
652 # path  size    owner1  group1  owner2  group2  perm1   perm2
653 #
654 a       0       $nobody $nobody daemon  sys     124     421
655 b       0       daemon  sys     bin     bin     347     743
656 sub/a   0       bin     bin     $nobody sys     777     777
657 sub/b   0       $nobody sys     $nobody $nobody 367     763
658 End-of-File
659 }
660
661 _create_dumpdir_symlinks()
662 {
663     _wipe_fs
664     _mk_symlink_config
665     _do_create_dump_symlinks
666     _stable_fs
667 }
668
669 #
670 # create hardlinks of form $_fname, $_fname_h1 $_fname_h2 ...
671 #
672 _create_hardlinks()
673 {
674     _fname=$1
675     _numlinks=$2
676
677     touch $_fname
678     _j=1
679     while [ $_j -le $_numlinks ]; do
680         _suffix=_h$_j
681         _hardlink=$_fname$_suffix
682         echo "creating hardlink $_hardlink to $_fname"
683         ln $_fname $_hardlink
684         let _j=$_j+1
685     done
686 }
687
688 #
689 # create a set of hardlinks
690 # create hardlinks of form file1, file1_h1 file1_h2 ...
691 # create hardlinks of form file2, file2_h1 file2_h2 ...
692 # create hardlinks of form file3, file3_h1 file3_h2 ...
693 #
694 _create_hardset()
695 {
696     _numsets=$1
697     _i=1
698     while [ $_i -le $_numsets ]; do
699         _create_hardlinks file$_i 5
700         let _i=$_i+1
701     done
702 }
703
704
705 _modify_level()
706 {
707     _level=$1
708     echo "mod level $_level" >$dump_dir/file$_level
709 }
710
711 _create_dumpdir_hardlinks()
712 {
713     _numsets=$1
714     _wipe_fs
715     echo "Creating directory system of hardlinks to incrementally dump."
716
717     mkdir -p $dump_dir || _fail "cannot mkdir \"$dump_dir\""
718     cd $dump_dir
719
720     _create_hardset $_numsets
721
722     cd $here
723     _stable_fs
724 }
725
726 #
727 # Filter for ls
728 # Filter out times and dates on symlinks and char devices.
729 # Filter out size on directories because this can differ
730 # when transitioning to long inode numbers (ie. 64 bits).
731 #
732 _ls_filter()
733 {
734   $AWK_PROG '
735         /^l/ { date = $8; time = $7; sub(date,"DATE"); sub(time,"TIME"); print}
736         /^c/ { date = $9; time = $7; sub(date,"DATE"); sub(time,"TIME"); print}
737         /^d/ { size = $5; sub(size,"SIZE"); print}
738         {print}' \
739   | sed -e 's/total [0-9][0-9]*/total TOTAL/'
740 }
741
742 #
743 # Filtering of Irix character hwgraph device names
744 # e.g.
745 # chardev: /hw/node/xtalk/15/pci/0/scsi_ctlr/0/target/1/lun/0/disk/partition/4/char
746 # blkdev:  /dev/dsk/dks0d1s4
747 #
748 _filter_devchar()
749 {
750     $AWK_PROG '
751         /\/hw\/node/ {
752             sub(/\/hw.*scsi_ctlr\//,"/dev/dsk/dks")  # blah blah /dev/dsk/dks0/target/1/....
753             sub(/\/target\//,"d")                    # blah blah /dev/dsk/dks0d1/lun/0/disk.....
754             sub(/\/lun.*partition\//,"s")            # blah blah /dev/dsk/dks0d1s4/char
755             sub(/\/char/,"")                         # blah blah /dev/dsk/dks0d1s4
756         }
757         { print }
758     '
759 }
760
761
762 #
763 # Filter out the non-deterministic dump msgs from
764 # xfsdump and xfsrestore
765 #
766 _dump_filter_main()
767 {
768   _filter_devchar |\
769   sed \
770       -e "s#$XFSDUMP_PROG#xfsdump#"                     \
771       -e "s#$XFSRESTORE_PROG#xfsrestore#"               \
772       -e "s#$XFSINVUTIL_PROG#xfsinvutil#"               \
773       -e "s/`hostname`/HOSTNAME/"                       \
774       -e "s#$SCRATCH_DEV#SCRATCH_DEV#"                  \
775       -e "s#$SCRATCH_RAWDEV#SCRATCH_DEV#"               \
776       -e "s#$dumptape#TAPE_DEV#"                        \
777       -e "s#$SCRATCH_MNT#SCRATCH_MNT#"                  \
778       -e "s#$dump_file#DUMP_FILE#"                      \
779       -e 's#/var/lib/xfsdump#/var/xfsdump#'             \
780       -e 's/session id:[        ]*[0-9a-f-]*/session id: ID/'  \
781       -e '/filesystem id:[      ]*[0-9a-f-]*/d'         \
782       -e 's/time:[      ].*/time: TIME/'                \
783       -e 's/date:[      ].*/date: DATE/'                \
784       -e 's/dump begun .*/dump begun DATE/'             \
785       -e 's/[0-9][0-9]* seconds/SECS seconds/'          \
786       -e 's/restore.[0-9][0-9]*/restore.PID/'           \
787       -e 's/ino [0-9][0-9]*/ino INO/'                   \
788       -e '/: dump size/s/[0-9][0-9]*/NUM/'              \
789       -e '/dump size:/s/[0-9][0-9]*/NUM/'               \
790       -e '/dump size per stream:/s/[0-9][0-9]*/NUM/'    \
791       -e 's/\(media file size[   ]*\)[0-9][0-9]*/\1NUM/' \
792       -e 's/\(mfile size:[       ]*\)[0-9][0-9]*/\1NUM/' \
793       -e '/drive[        ]*[0-9][0-9]*:/d'              \
794       -e '/\/dev\/tty/d'                                \
795       -e '/inventory session uuid/d'                    \
796       -e '/ - Running single-threaded/d'                \
797       -e '/^.*I\/O metrics: .*$/d'                      \
798       -e 's/1048576/BLOCKSZ/'                           \
799       -e 's/2097152/BLOCKSZ/'                           \
800       -e 's/(pid[        ]*[1-9][0-9]*)/\(pid PID\)/'   \
801       -e '/version 3\.0/d'                              \
802       -e 's/\/hw\/module.*$/SCRATCH_DEV/'               \
803       -e 's/xfsdump: ino map phase 1: .*/xfsdump: ino map <PHASES>/' \
804       -e '/xfsdump: ino map phase [2]/,1d'              \
805       -e '/xfsdump: ino map phase [3]/,1d'              \
806       -e '/xfsdump: ino map phase [4]/,1d'              \
807       -e '/xfsdump: ino map phase [5]/,1d'              \
808       -e 's/id:[[:space:]]*[0-9a-f]\{8\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{12\}/ID: ID/'                                              \
809       -e 's/\[y\/n\][- ]----------------------*/\[y\/n\]/'              \
810   | perl -ne '
811       if ($_ =~ /(?:Dump|Restore) Summary/) {
812         $skip = 1;
813       } elsif ($_ =~ /(?:Dump|Restore) Status/) {
814         $skip = 0;
815       }
816       print if (! $skip);'
817 }
818
819 _dump_filter()
820 {
821    if $do_quota_check
822    then
823        _dump_filter_main | _check_quota_dumprestore | _check_quota_entries
824    else
825        _dump_filter_main
826    fi
827 }
828
829 _invutil_filter()
830 {
831   _dump_filter_main \
832   | sed \
833         -e 's/UUID[     ]*:[    ][0-9a-f-]*/UUID                :       ID/' \
834         -e 's/TIME OF DUMP[     ]*:.*/TIME OF DUMP      :       TIME/' \
835         -e 's/HOSTNAME:SCRATCH_MNT.*/HOSTNAME:SCRATCH_MNT/' \
836         -e 's#inventory/[0-9a-f-]*#inventory/UUID#' \
837
838 }
839
840
841 _dir_filter()
842 {
843   sed \
844     -e "s#$dump_file#DUMP_FILE#"      \
845     -e "s#$SCRATCH_DEV#SCRATCH_DEV#"        \
846     -e "s#$SCRATCH_RAWDEV#SCRATCH_DEV#"    \
847     -e "s#$dumptape#TAPE_DEV#"         \
848     -e "s#$dump_dir#DUMP_DIR#g"       \
849     -e "s#$restore_dir#RESTORE_DIR#g" \
850     -e "s#$SCRATCH_MNT#SCRATCH_MNT#g"       \
851     -e "s#$dump_sdir#DUMP_SUBDIR#g"   \
852     -e "s#$restore_sdir#RESTORE_SUBDIR#g" \
853     -e "s#$$#PID#g" \
854
855 }
856
857 #
858 # Note: requires a space between option letter and argument
859 #
860 _parse_args()
861 {
862     OPTIND=0
863     dump_args=""
864     while [ $# -gt 0 ]
865     do
866         case $1
867         in
868         -f)
869             [ -z "$2" ] && _fail "missing argument for -f"
870             dumptape=$2
871             shift
872             ;;
873         -L)
874             [ -z "$2" ] && _fail "missing argument for -L"
875             session_label=$2
876             shift
877             ;;
878         -o)
879             dump_args="$dump_args -o"
880             ;;
881         -F)
882             dump_args="$dump_args -F"
883             ;;
884         --multi)
885             multi=$2
886             shift
887             ;;
888         -q)
889             do_quota_check=true
890             ;;
891         -Q)
892             do_quota_check=false
893             ;;
894         -l)
895             [ -z "$2" ] && _fail "missing argument for -l"
896             dump_args="$dump_args -l$2"
897             shift
898             ;;
899         *)
900             _fail "invalid argument to common.dump function: $1"
901             ;;
902         esac
903         shift
904     done
905 }
906
907
908 #
909 # Dump a subdir
910 #
911 _do_dump_sub()
912 {
913     _parse_args $*
914
915     echo "Dumping to tape..."
916     opts="$_dump_debug$dump_args -s $dump_sdir -f $dumptape -M $media_label -L $session_label $SCRATCH_MNT"
917     echo "xfsdump $opts" | _dir_filter
918     $XFSDUMP_PROG $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
919 }
920
921 #
922 # Do dump to tape
923 #
924 _do_dump()
925 {
926     _parse_args $*
927
928     echo "Dumping to tape..."
929     opts="$_dump_debug$dump_args -f $dumptape -M $media_label -L $session_label $SCRATCH_MNT"
930     echo "xfsdump $opts" | _dir_filter
931     $XFSDUMP_PROG $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
932 }
933
934
935 #
936 # Do full dump with -m
937 #
938 _do_dump_min()
939 {
940     _parse_args $*
941
942     echo "Dumping to tape..."
943     onemeg=1048576
944     opts="$_dump_debug$dump_args -m -b $onemeg -l0 -f $dumptape -M $media_label -L $session_label $SCRATCH_MNT"
945     echo "xfsdump $opts" | _dir_filter
946     $XFSDUMP_PROG $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
947 }
948
949
950 #
951 # Do full dump to file
952 #
953 _do_dump_file()
954 {
955     _parse_args $*
956
957     echo "Dumping to file..."
958     opts="$_dump_debug$dump_args -f $dump_file -M $media_label -L $session_label $SCRATCH_MNT"
959     echo "xfsdump $opts" | _dir_filter
960     $XFSDUMP_PROG $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
961 }
962
963 #
964 # Do full dump to multiple files
965 #
966 _do_dump_multi_file()
967 {
968     _parse_args "$@"
969
970     multi_args=""
971
972     i=0
973     while [ $i -lt $multi ]
974     do
975         multi_args="$multi_args -f $dump_file.$i -M $media_label.$i"
976         let i=$i+1
977     done
978
979     echo "Dumping to files..."
980     opts="$_dump_debug$dump_args $multi_args -L $session_label $SCRATCH_MNT"
981     echo "xfsdump $opts" | _dir_filter
982     $XFSDUMP_PROG $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
983 }
984
985
986 _prepare_restore_dir()
987 {
988     rm -rf $restore_dir
989     mkdir $restore_dir || _fail "failed to mkdir $restore_dir"
990 }
991
992
993 #
994 # Get tape ready and restore dir
995 #
996 _prepare_restore()
997 {
998     _prepare_restore_dir
999
1000     echo "Rewinding tape"
1001     _rewind
1002 }
1003
1004 #
1005 # Restore the tape into $restore_dir
1006 #
1007 _do_restore()
1008 {
1009     _parse_args $*
1010     _prepare_restore
1011
1012
1013     echo "Restoring from tape..."
1014     opts="$_restore_debug -f $dumptape  -L $session_label $restore_dir"
1015     echo "xfsrestore $opts" | _dir_filter
1016     $XFSRESTORE_PROG $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1017 }
1018
1019 #
1020 # Restore the tape into $restore_dir using -m
1021 #
1022 _do_restore_min()
1023 {
1024     _parse_args $*
1025     _prepare_restore
1026
1027     echo "Restoring from tape..."
1028     onemeg=1048576
1029     opts="$_restore_debug -m -b $onemeg -f $dumptape  -L $session_label $restore_dir"
1030     echo "xfsrestore $opts" | _dir_filter
1031     $XFSRESTORE_PROG $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1032 }
1033
1034 #
1035 # Restore the tape from a dump file
1036 #
1037 _do_restore_file()
1038 {
1039     _parse_args $*
1040     _prepare_restore_dir
1041
1042     echo "Restoring from file..."
1043     opts="$_restore_debug -f $dump_file  -L $session_label $restore_dir"
1044     echo "xfsrestore $opts" | _dir_filter
1045     $XFSRESTORE_PROG $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1046 }
1047
1048 #
1049 # Cumulative restore from a file
1050 # Need to specify the dump level e.g. "-l 0"
1051 #
1052 _do_restore_file_cum()
1053 {
1054     _parse_args $*
1055     if echo $dump_args | grep '\-l0' >/dev/null; then
1056         _prepare_restore_dir
1057     fi
1058
1059     echo "Restoring cumumlative from file..."
1060     opts="$_restore_debug -f $dump_file -r $restore_dir"
1061     echo "xfsrestore $opts" | _dir_filter
1062     $XFSRESTORE_PROG $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1063 }
1064
1065 _do_restore_toc()
1066 {
1067     echo "Contents of dump ..."
1068     opts="$_restore_debug -f $dump_file -t"
1069     echo "xfsrestore $opts" | _dir_filter
1070     cd $SCRATCH_MNT # for IRIX which needs xfs cwd
1071     $XFSRESTORE_PROG $opts 2>&1 | tee -a $here/$seq.full | _dump_filter_main |\
1072     _check_quota_file |\
1073     _check_quota_entries |\
1074     $AWK_PROG 'NF != 1 { print; next }
1075                {files = sprintf("%s\n%s", files, $1)}
1076                 END { print files | "sort" } '
1077     # the above awk code is to alpha sort only the output
1078     # of files (and not the verbose restore msgs)
1079     cd $here # put back
1080 }
1081
1082 #
1083 # Restore the tape from multiple dump files
1084 #
1085 _do_restore_multi_file()
1086 {
1087     _parse_args "$@"
1088     _prepare_restore_dir
1089
1090     multi_args=""
1091
1092     i=0
1093     while [ $i -lt $multi ]
1094     do
1095         multi_args="$multi_args -f $dump_file.$i"
1096         let i=$i+1
1097     done
1098
1099     echo "Restoring from file..."
1100     opts="$_restore_debug $multi_args -L $session_label $restore_dir"
1101     echo "xfsrestore $opts" | _dir_filter
1102     $XFSRESTORE_PROG $opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1103 }
1104
1105 #
1106 # Do xfsdump piped into xfsrestore - xfsdump | xfsrestore
1107 #
1108 # Use -s as we want to dump and restore to the same xfs partition
1109 #
1110 _do_dump_restore()
1111 {
1112     _parse_args $*
1113     _prepare_restore_dir
1114     echo "xfsdump|xfsrestore ..."
1115     restore_opts="$_restore_debug - $restore_dir"
1116     dump_opts="$_dump_debug$dump_args -s $dump_sdir - $SCRATCH_MNT"
1117     echo "xfsdump $dump_opts | xfsrestore $restore_opts" | _dir_filter
1118     $XFSDUMP_PROG $dump_opts 2>$tmp.dump.mlog | $XFSRESTORE_PROG $restore_opts 2>&1 | tee -a $here/$seq.full | _dump_filter
1119     _dump_filter <$tmp.dump.mlog
1120 }
1121
1122 #
1123 # Compare dumped subdirectory with restored dir
1124 # using ls -nR.
1125 # Thus no contents are compared but permissions, sizes,
1126 # owners, etc... are.
1127 #
1128 _ls_compare_sub()
1129 {
1130     #
1131     # verify we got back what we dumped
1132     #
1133     echo "Comparing listing of dump directory with restore directory"
1134     ls -nR $dump_dir | tee -a $here/$seq.full | _ls_filter >$tmp.dump_dir
1135     ls -nR $restore_dir/$dump_sdir | tee -a $here/$seq.full | _ls_filter \
1136     | sed -e "s#$restore_sdir\/##" >$tmp.restore_dir
1137
1138     diff -bcs $tmp.dump_dir $tmp.restore_dir | sed -e "s#$tmp#TMP#g"
1139 }
1140
1141 #
1142 # filter out the date fields
1143 #
1144 _ls_nodate_filter()
1145 {
1146     $AWK_PROG 'NF == 9 { print $1, $2, $3, $4, $9 }'
1147 }
1148
1149 #
1150 # _ls_compare_sub but don't compare dates
1151 _ls_nodate_compare_sub()
1152 {
1153     #
1154     # verify we got back what we dumped
1155     #
1156     echo "Comparing listing of dump directory with restore directory"
1157     ls -nR $dump_dir | tee -a $here/$seq.full | _ls_filter | _ls_nodate_filter >$tmp.dump_dir
1158     ls -nR $restore_dir/$dump_sdir | tee -a $here/$seq.full | _ls_filter \
1159     | _ls_nodate_filter | sed -e "s#$restore_sdir\/##" >$tmp.restore_dir
1160
1161     diff -bcs $tmp.dump_dir $tmp.restore_dir | sed -e "s#$tmp#TMP#g"
1162 }
1163
1164 #
1165 # Compare using recursive diff the files of the dumped
1166 # subdirectory.
1167 # This one will compare the contents.
1168 #
1169 _diff_compare_sub()
1170 {
1171     echo "Comparing dump directory with restore directory"
1172     diff -rs $dump_dir $restore_dir/$dump_sdir | _dir_filter
1173 }
1174
1175 _get_eas_on_path()
1176 {
1177     _path=$1
1178
1179 # Tim - this is the IRIX way...
1180     # find $_path -exec attr -l {} \; |\
1181     # awk '{print $9, $2}' |\
1182     # sed 's/["]//g' |\
1183     # sort |\
1184 # and this is now the Linux way...
1185     echo "User names"
1186     getfattr --absolute-names -Rh -m user $_path |\
1187     perl -wn -e '
1188         if (m/^# file: (\S+)/) { $file = $1 }
1189         elsif (m/^user\.(\w+)/) { print $file, " ",$1,"\n" }' |\
1190     sort |\
1191     while read file ea_name; do
1192         attr -g $ea_name $file
1193     done
1194
1195     if [ "$USE_ATTR_SECURE" = yes ]; then
1196         echo "Security names"
1197         getfattr --absolute-names -Rh -m security $_path |\
1198         perl -wn -e '
1199             if (m/^# file: (\S+)/) { $file = $1 }
1200             elsif (m/^security\.(\w+)/) { print $file, " ",$1,"\n" }' |\
1201         sort |\
1202         while read file ea_name; do
1203             attr -g $ea_name $file
1204         done
1205     fi
1206
1207     echo "Root names"
1208     getfattr --absolute-names -Rh -m trusted $_path |\
1209     perl -wn -e '
1210         if (m/^# file: (\S+)/) { $file = $1 }
1211         elsif (m/^trusted\.(\w+)/) { print $file, " ",$1,"\n" }' |\
1212     sort |\
1213     while read file ea_name; do
1214         attr -R -g $ea_name $file
1215     done
1216 }
1217
1218 #
1219 # Compare the extended attributes of the files/dirs
1220 # b/w the dumped and restore dirs.
1221 #
1222 #
1223 # Attribute "attr5" had a 8 byte value for /spare1/dump.5460/dir:
1224 # Attribute "attr5" had a 8 byte value for /spare1/restore.5460/dump.5460/dir:
1225 #
1226 _diff_compare_eas()
1227 {
1228     echo "Comparing dump directory with restore directory"
1229     echo "Looking at the extended attributes (EAs)"
1230     echo "EAs on dump"
1231     _get_eas_on_path $dump_dir | tee $seq.ea1 | _dir_filter
1232     echo "EAs on restore"
1233     _get_eas_on_path $restore_dir/$dump_sdir \
1234     | sed -e "s#$restore_sdir\/##" \
1235     | tee $seq.ea2 \
1236     | _dir_filter
1237     diff -s $seq.ea1 $seq.ea2
1238 }
1239
1240
1241 #
1242 # Compare using recursive diff the files of the dumped
1243 # filesystem
1244 #
1245 _diff_compare()
1246 {
1247     echo "Comparing dump directory with restore directory"
1248     diff -rs $SCRATCH_MNT $restore_dir | _dir_filter | _check_quota_diff
1249 }
1250
1251 #
1252 # Check out the dump inventory
1253 #
1254 _dump_inventory()
1255 {
1256     $XFSDUMP_PROG $_dump_debug -I | tee -a $here/$seq.full | _dump_filter_main
1257 }
1258
1259 #
1260 # Do the xfsinvutil cmd with debug and filters
1261 # Need to set variable: "$middate" to the invutil date
1262 #
1263 _do_invutil()
1264 {
1265     host=`hostname`
1266     echo "xfsinvutil $_invutil_debug -M $host:$SCRATCH_MNT \"$middate\" $*" >$here/$seq.full
1267     $XFSINVUTIL_PROG $_invutil_debug $* -M $host:$SCRATCH_MNT "$middate" \
1268     | tee -a $here/$seq.full | _invutil_filter
1269 }
1270
1271 #
1272 # ensure we can find the user quota msg if user quotas are on
1273 # ensure we can find the group quota msg if group quotas are on
1274 #
1275 _check_quota()
1276 {
1277     usermsg=$1
1278     groupmsg=$2
1279     projectmsg=$3
1280     uquota=0
1281     gquota=0
1282     pquota=0
1283     $here/src/feature -U $SCRATCH_DEV && uquota=1
1284     $here/src/feature -G $SCRATCH_DEV && gquota=1
1285     $here/src/feature -P $SCRATCH_DEV && pquota=1
1286
1287     $AWK_PROG -v uquota=$uquota -v gquota=$gquota -v pquota=$pquota \
1288               -v full=$here/$seq.full -v usermsg="$usermsg" \
1289               -v groupmsg="$groupmsg" -v projectmsg="$projectmsg" '
1290         $0 ~ projectmsg {
1291                         print "Found project quota:", $0 >>full
1292                         found_pquota = 1
1293                         if (!pquota) {
1294                             print "Found extra:", $0
1295                         }
1296                         next
1297         }
1298         $0 ~ groupmsg {
1299                         print "Found group quota:", $0 >>full
1300                         found_gquota = 1
1301                         if (!gquota) {
1302                             print "Found extra:", $0
1303                         }
1304                         next
1305         }
1306         $0 ~ usermsg {
1307                         print "Found user quota:", $0 >>full
1308                         found_uquota = 1
1309                         if (!uquota) {
1310                             print "Found extra:", $0
1311                         }
1312                         next
1313         }
1314                         { print }
1315         END {
1316                 if (uquota && !found_uquota) {
1317                     print "Missing user quota msg:", usermsg
1318                 }
1319                 if (gquota && !found_gquota) {
1320                     print "Missing group quota msg:", groupmsg
1321                 }
1322                 if (pquota && !found_pquota) {
1323                     print "Missing project quota msg:", projectmsg
1324                 }
1325         }
1326     '
1327 }
1328
1329 #
1330 # xfsrestore: 3 directories and 40 entries processed
1331 #   $5 = 40
1332 #   num entries needs to be reduced by num quota file(s)
1333 #
1334 _check_quota_entries()
1335 {
1336     uquota=0
1337     gquota=0
1338     pquota=0
1339     $here/src/feature -U $SCRATCH_DEV && uquota=1
1340     $here/src/feature -G $SCRATCH_DEV && gquota=1
1341     $here/src/feature -P $SCRATCH_DEV && pquota=1
1342     $AWK_PROG -v uquota=$uquota -v gquota=$gquota -v pquota=$pquota '
1343         /entries processed/ {
1344                 if (uquota) $5--
1345                 if (gquota) $5--
1346                 if (pquota) $5--
1347         }
1348         {print}'
1349 }
1350
1351 #
1352 # Look for:
1353 # xfsdump: saving user quota information for: SCRATCH_MNT
1354 # xfsdump: saving group quota information for: SCRATCH_MNT
1355 # xfsdump: saving project quota information for: SCRATCH_MNT
1356 # xfsrestore: user quota information written to ...'
1357 # xfsrestore: group quota information written to ...'
1358 # xfsrestore: project quota information written to ...'
1359 #
1360 # If on IRIX then look for:
1361 # xfsrestore: use 'edquota' to restore quotas
1362 # Else look for:
1363 # xfsrestore: use 'xfs_quota' to restore quotas
1364 #
1365 _check_quota_dumprestore()
1366 {
1367     if [ "$HOSTOS" == "IRIX" ]; then
1368         _check_quota 'user quota information' \
1369                      'group quota information' \
1370                      'project quota information' | \
1371         sed "/xfsrestore:.*use 'edquota' to restore quotas/d"
1372     else
1373         _check_quota 'user quota information' \
1374                      'group quota information' \
1375                      'project quota information' | \
1376         sed "/xfsrestore:.*use 'xfs_quota' to restore quotas/d"
1377     fi
1378 }
1379
1380 #
1381 # Look for:
1382 # Only in RESTORE_DIR: xfsdump_quotas
1383 # Only in RESTORE_DIR: xfsdump_quotas_group
1384 # Only in RESTORE_DIR: xfsdump_quotas_project
1385 #
1386 _check_quota_diff()
1387 {
1388    _check_quota 'Only in RESTORE_DIR: xfsdump_quotas' \
1389         'Only in RESTORE_DIR: xfsdump_quotas_group' \
1390         'Only in RESTORE_DIR: xfsdump_quotas_proj'
1391 }
1392
1393 #
1394 # Look for the quota file in the output
1395 # Ensure that it is there if it should be
1396 # Filter it out so that the output is always the same
1397 # even with no quotas
1398 #
1399 _check_quota_file()
1400 {
1401    _check_quota 'xfsdump_quotas' 'xfsdump_quotas_group' 'xfsdump_quotas_proj'
1402 }
1403
1404
1405 # make sure this script returns success
1406 /bin/true