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