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