xfstests: randholes: only allocate write buffer when needed
[xfstests-dev.git] / common.repair
1 ##/bin/bash
2 #
3 # Copyright (c) 2000-2002 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 xfs_repair tests
20 #
21
22 _zero_position()
23 {
24         value=$1
25         struct="$2"
26
27         # set values for off/len variables provided by db
28         eval `xfs_db -r -c "$struct" -c stack $SCRATCH_DEV | perl -ne '
29                 if (/byte offset (\d+), length (\d+)/) {
30                         print "offset=$1\nlength=$2\n"; exit
31                 }'`
32         if [ -z "$offset" -o -z "$length" ]; then
33                 echo "cannot calculate offset ($offset) or length ($length)"
34                 exit
35         fi
36         length=`expr $length / 512`
37         src/devzero -v $value -b 1 -n $length -o $offset $SCRATCH_DEV \
38                 | perl -npe 's/\d\.\d\dKb/X.XXKb/g'
39 }
40
41 _filter_repair()
42 {
43         perl -ne '
44 # for sb
45 /- agno = / && next;    # remove each AG line (variable number)
46 s/(pointer to) (\d+)/\1 INO/;
47 s/(sb root inode value) (\d+)( \(NULLFSINO\))?/\1 INO/;
48 s/(realtime bitmap inode) (\d+)( \(NULLFSINO\))?/\1 INO/;
49 s/(realtime summary inode) (\d+)( \(NULLFSINO\))?/\1 INO/;
50 s/(inconsistent with calculated value) (\d+)/\1 INO/;
51 s/\.+(found)/\1/g;      # remove "searching" output
52 # for agf + agi
53 s/(bad length -{0,1}\d+ for ag. 0, should be) (\d+)/\1 LENGTH/;
54 s/(bad length # -{0,1}\d+ for ag. 0, should be) (\d+)/\1 LENGTH/;
55 s/(bad agbno) (\d+)/\1 AGBNO/g;
56 s/(max =) (\d+)/\1 MAX/g;
57 # for root inos
58 s/(on inode) (\d+)/\1 INO/g;
59 s/(imap claims a free inode) (\d+)/\1 INO/;
60 s/(imap claims in-use inode) (\d+)/\1 INO/;
61 s/(cleared root inode) (\d+)/\1 INO/;
62 s/(resetting inode) (\d+)/\1 INO/;
63 s/(disconnected dir inode) (\d+)/\1 INO/;
64 # for log
65 s/internal log/<TYPEOF> log/g;
66 s/external log on \S+/<TYPEOF> log/g;
67 # realtime subvol - remove this whole line if it appears
68 s/        - generate realtime summary info and bitmap...\n//g;
69 #
70 # new xfs repair output filters
71 #
72 s/\s+- creating \d+ worker thread\(s\)\n//g;
73 s/\s+- reporting progress in intervals of \d+ minutes\n//g;
74 s/\s+- \d+:\d\d:\d\d:.*\n//g;
75 # 3.1.0 extra accounting output
76 /^agf_/ && next; # remove agf counts
77 /^agi_/ && next; # remove agi counts
78 /^sb_/ && next; # remove sb counts
79 /^agi unlinked/ && next; # remove agi unlinked bucket warning
80         print;'
81 }
82
83 _filter_dd()
84 {
85         fgrep -v records        # lose records in/out lines
86 }
87
88 # do some controlled corrupting & ensure repair recovers us
89
90 _check_repair()
91 {
92         value=$1
93         structure="$2"
94
95         #ensure the filesystem has been dirtied since last repair
96         _scratch_mount
97         POSIXLY_CORRECT=yes \
98         dd if=/bin/bash of=$SCRATCH_MNT/sh 2>&1 |_filter_dd
99         sync
100         rm -f $SCRATCH_MNT/sh
101         umount $SCRATCH_MNT
102
103         _zero_position $value "$structure"
104         _scratch_xfs_repair 2>&1 | _filter_repair
105
106         # some basic sanity checks...
107         _check_scratch_fs
108         _scratch_mount                                      #mount
109         POSIXLY_CORRECT=yes \
110         dd if=/bin/bash of=$SCRATCH_MNT/sh 2>&1 |_filter_dd   #open,write
111         POSIXLY_CORRECT=yes \
112         dd if=$SCRATCH_MNT/sh of=/dev/null 2>&1 |_filter_dd #read
113         rm -f $SCRATCH_MNT/sh                               #unlink
114         umount $SCRATCH_MNT                                 #umount
115 }
116
117 # make sure this script returns success
118 /bin/true