8e1fdb4e501d960ff26b7b3a261ad273e7778db6
[xfstests-dev.git] / common / filter
1 ##/bin/bash
2 #
3 # Copyright (c) 2000-2001 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 # standard filters
20 #
21
22 # Checks that given_value is in range of correct_value +/- tolerance.
23 # Tolerance can be an absolute value or a percentage of the correct value
24 # (see examples with tolerances below).
25 # Outputs suitable message to stdout if it's not in range.
26 #
27 # A verbose option, -v, may be used as the LAST argument
28
29 # e.g. 
30 # foo: 0.0298 = 0.03 +/- 5%
31 # _within_tolerance "foo" 0.0298 0.03 5%  
32
33 # foo: 0.0298 = 0.03 +/- 0.01
34 # _within_tolerance "foo" 0.0298 0.03 0.01
35 #
36 # foo: 0.0298 = 0.03 -0.01 +0.002
37 # _within_tolerance "foo" 0.0298 0.03 0.01 0.002
38 #
39 # foo: verbose output of 0.0298 = 0.03 +/- 5% 
40 # _within_tolerance "foo" 0.0298 0.03 5% -v 
41 _within_tolerance()
42 {
43   _name=$1
44   _given_val=$2
45   _correct_val=$3
46   _mintol=$4
47   _maxtol=$_mintol
48   _verbose=0
49   _debug=false
50
51   # maxtol arg is optional
52   # verbose arg is optional
53   if [ $# -ge 5 ]
54   then 
55      if [ "$5" = "-v" ]
56      then
57         _verbose=1
58      else
59         _maxtol=$5
60      fi
61   fi
62   if [ $# -ge 6 ]
63   then
64      [ "$6" = "-v" ] && _verbose=1
65   fi
66
67   # find min with or without %
68   _mintolerance=`echo $_mintol | sed -e 's/%//'` 
69   if [ $_mintol = $_mintolerance ]
70   then 
71       _min=`echo "scale=5; $_correct_val-$_mintolerance" | bc`
72   else
73       _min=`echo "scale=5; $_correct_val-$_mintolerance*0.01*$_correct_val" | bc`
74   fi
75
76   # find max with or without %
77   _maxtolerance=`echo $_maxtol | sed -e 's/%//'` 
78   if [ $_maxtol = $_maxtolerance ]
79   then 
80       _max=`echo "scale=5; $_correct_val+$_maxtolerance" | bc`
81   else
82       _max=`echo "scale=5; $_correct_val+$_maxtolerance*0.01*$_correct_val" | bc`
83   fi
84
85   $_debug && echo "min = $_min"
86   $_debug && echo "max = $_max"
87
88   cat <<EOF >$tmp.bc.1
89 scale=5;
90 if ($_min <= $_given_val) 1;
91 if ($_min > $_given_val) 0; 
92 EOF
93
94   cat <<EOF >$tmp.bc.2
95 scale=5;
96 if ($_given_val <= $_max) 1;
97 if ($_given_val > $_max) 0;
98 EOF
99
100   _above_min=`bc <$tmp.bc.1`
101   _below_max=`bc <$tmp.bc.2`
102
103   rm -f $tmp.bc.[12]
104
105   _in_range=`expr $_above_min \& $_below_max` 
106
107   # fix up min, max precision for output
108   # can vary for 5.3, 6.2
109
110   # remove any trailing zeroes from min, max if they have fractional parts
111   _min=`echo $_min | sed -e '/\./s/0*$//' -e 's/\.$//'`
112   _max=`echo $_max | sed -e '/\./s/0*$//' -e 's/\.$//'`
113
114   if [ $_in_range -eq 1 ] 
115   then
116         [ $_verbose -eq 1 ] && echo $_name is in range
117         return 0
118   else
119         [ $_verbose -eq 1 ] && echo $_name has value of $_given_val
120         [ $_verbose -eq 1 ] && echo $_name is NOT in range $_min .. $_max       
121         return 1
122   fi
123 }
124
125 # ctime(3) dates
126 #
127 _filter_date()
128 {
129     sed \
130         -e 's/[A-Z][a-z][a-z] [A-z][a-z][a-z]  *[0-9][0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]$/DATE/'
131 }
132
133 # prints filtered output on stdout, values (use eval) on stderr
134 # Non XFS filesystems always return a 4k block size and a 256 byte inode.
135 _filter_mkfs()
136 {
137     case $FSTYP in
138     xfs)
139         ;;
140     *)
141         cat - >/dev/null
142         perl -e 'print STDERR "dbsize=4096\nisize=256\n"'
143         return ;;
144     esac
145
146     echo "_fs_has_crcs=0" >&2
147     set -
148     perl -ne '
149     if (/^meta-data=([\w,|\/.-]+)\s+isize=(\d+)\s+agcount=(\d+), agsize=(\d+) blks/) {
150         print STDERR "ddev=$1\nisize=$2\nagcount=$3\nagsize=$4\n";
151         print STDOUT "meta-data=DDEV isize=XXX agcount=N, agsize=XXX blks\n";
152     }
153     if (/^\s+=\s+sectsz=(\d+)\s+attr=(\d+)/) {
154         print STDERR "sectsz=$1\nattr=$2\n";
155     }
156     if (/^\s+=\s+crc=(\d)/) {
157         print STDERR "_fs_has_crcs=$1\n";
158     }
159     if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+), imaxpct=(\d+)/) {
160         print STDERR "dbsize=$1\ndblocks=$2\nimaxpct=$3\n";
161         print STDOUT "data     = bsize=XXX blocks=XXX, imaxpct=PCT\n";
162     }
163     if (/^\s+=\s+sunit=(\d+)\s+swidth=(\d+) blks/) {
164         print STDERR "sunit=$1\nswidth=$2\nunwritten=1\n";
165         print STDOUT "         = sunit=XXX swidth=XXX, unwritten=X\n";
166     }
167     if (/^naming\s+=version\s+(\d+)\s+bsize=(\d+)/) {
168         print STDERR "dirversion=$1\ndirbsize=$2\n";
169         print STDOUT "naming   =VERN bsize=XXX\n";
170     }
171     if (/^log\s+=(internal log|[\w|\/.-]+)\s+bsize=(\d+)\s+blocks=(\d+),\s+version=(\d+)/ ||
172         /^log\s+=(internal log|[\w|\/.-]+)\s+bsize=(\d+)\s+blocks=(\d+)/) {
173         print STDERR "ldev=\"$1\"\nlbsize=$2\nlblocks=$3\nlversion=$4\n";
174         print STDOUT "log      =LDEV bsize=XXX blocks=XXX\n";
175     }
176     if (/^\s+=\s+sectsz=(\d+)\s+sunit=(\d+) blks/) {
177         print STDERR "logsectsz=$1\nlogsunit=$2\n\n";
178     }
179     if (/^realtime\s+=([\w|\/.-]+)\s+extsz=(\d+)\s+blocks=(\d+), rtextents=(\d+)/) {
180         print STDERR "rtdev=$1\nrtextsz=$2\nrtblocks=$3\nrtextents=$4\n";
181         print STDOUT "realtime =RDEV extsz=XXX blocks=XXX, rtextents=XXX\n";
182     }'
183 }
184
185
186 # prints the bits we care about in growfs
187
188 _filter_growfs()
189 {
190     perl -ne '
191     if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+), imaxpct=(\d+)/) {
192         print "xfs_growfs --BlockSize=$1 --Blocks=$2\n";
193     }
194     elsif (/^data/) {
195         print;
196     }'
197 }
198
199 _filter_dd()
200 {
201     $AWK_PROG '
202         /records in/                { next }
203         /records out/               { next }
204         /No space left on device/   { print "   !!! disk full (expected)" 
205                                       next }
206                                     { print "   *** " $0 }
207     '
208 }
209
210 common_line_filter()
211 {
212     perl -ne 'if (/.*:(.*)/) {
213         if ( "$last_line" ne "$1" ) { print "$_"; $first_match=1; }
214         elsif ( $first_match==1 ) { print "*\n"; $first_match=0; }
215         $last_line="$1";
216     }
217     else {
218         print $_; $last_line=$_;
219     }'
220 }
221
222 _filter_xfs_io()
223 {
224     # Apart from standard numeric values, we also filter out 'inf' and 'nan'
225     # which can result from division in some cases
226     sed -e "s/[0-9/.]* [GMKiBbytes]*, [0-9]* ops\; [0-9/:. sec]* ([infa0-9/.]* [EPGMKiBbytes]*\/sec and [infa0-9/.]* ops\/sec)/XXX Bytes, X ops\; XX:XX:XX.X (XXX YYY\/sec and XXX ops\/sec)/"
227 }
228
229 # Also filter out the offset part of xfs_io output
230 # Some test cases may be affected by underlaying extent/chunk layout change,
231 # so wipe out this part to avoid golden output difference
232 _filter_xfs_io_offset()
233 {
234     # filter out " at offset XXX" and offset of "pread -v"
235     _filter_xfs_io | sed -e "s/ at offset [0-9]*$//" -e "s/^[0-9a-f]\+:/XXXXXXXX:/"
236 }
237
238 # stderr filter for xfs_io to handle change of error output format (e.g.
239 # pwrite64 -> pwrite).
240 _filter_xfs_io_error()
241 {
242         sed -e "s/^\(.*\)64\(: .*$\)/\1\2/"
243 }
244
245 _filter_xfs_io_unique()
246 {
247     common_line_filter | _filter_xfs_io
248 }
249
250 _filter_xfs_io_units_modified()
251 {
252         UNIT=$1
253         UNIT_SIZE=$2
254
255         $AWK_PROG -v unit="$UNIT" -v unit_size=$UNIT_SIZE '
256                 /wrote/ {
257                         split($2, bytes, "/")
258
259                         bytes_written = strtonum(bytes[1])
260
261                         offset = strtonum($NF)
262
263                         unit_start = offset / unit_size
264                         unit_start = int(unit_start)
265                         unit_end = (offset + bytes_written - 1) / unit_size
266                         unit_end = int(unit_end)
267
268                         printf("%ss modified: [%d - %d]\n", unit, unit_start, unit_end)
269
270                         next
271                 }
272         '
273 }
274
275 _filter_xfs_io_blocks_modified()
276 {
277         BLOCK_SIZE=$(_get_block_size $SCRATCH_MNT)
278
279         _filter_xfs_io_units_modified "Block" $BLOCK_SIZE
280 }
281
282 _filter_xfs_io_pages_modified()
283 {
284         PAGE_SIZE=$(get_page_size)
285
286         _filter_xfs_io_units_modified "Page" $PAGE_SIZE
287 }
288
289 _filter_test_dir()
290 {
291         # TEST_DEV may be a prefix of TEST_DIR (e.g. /mnt, /mnt/ovl-mnt)
292         # so substitute TEST_DIR first
293         sed -e "s,\B$TEST_DIR,TEST_DIR,g" \
294             -e "s,\B$TEST_DEV,TEST_DEV,g"
295 }
296
297 _filter_scratch()
298 {
299         # SCRATCH_DEV may be a prefix of SCRATCH_MNT (e.g. /mnt, /mnt/ovl-mnt)
300         # so substitute SCRATCH_MNT first
301         sed -e "s,\B$SCRATCH_MNT,SCRATCH_MNT,g" \
302             -e "s,\B$SCRATCH_DEV,SCRATCH_DEV,g" \
303             -e "/.use_space/d"
304 }
305
306 _filter_testdir_and_scratch()
307 {
308         # filter both $TEST_DIR and $SCRATCH_MNT, but always filter the longer
309         # string first if the other string is a substring of the first one
310         if echo "$TEST_DIR" | grep -q "$SCRATCH_MNT"; then
311                 _filter_test_dir | _filter_scratch
312         else
313                 _filter_scratch | _filter_test_dir
314         fi
315 }
316
317 # Turn any device in the scratch pool into SCRATCH_DEV
318 _filter_scratch_pool()
319 {
320         FILTER_STRINGS=`echo $SCRATCH_DEV_POOL | sed -e 's/\s\+/\\\|/g'`
321         sed -e "s,$FILTER_STRINGS,SCRATCH_DEV,g"
322 }
323
324 _filter_spaces()
325 {
326         sed -e "s/\s\+/ /g"
327 }
328
329 _filter_quota()
330 {
331         # Long dev name might be split onto its own line; last
332         # seds remove that newline if present
333         _filter_testdir_and_scratch | _filter_spaces | \
334         sed -e 'N;s/SCRATCH_DEV\n/SCRATCH_DEV/g' | \
335         sed -e 'N;s/TEST_DEV\n/TEST_DEV/g'
336 }
337
338 _filter_project_quota()
339 {
340         # Project ID 0 is always present on disk but was not reported
341         # until the GETNEXTQUOTA ioctl came into use.  Filter it out.
342         # But if you specify a name for ID 0, that means you want to
343         # deal with it by yourself, this function won't filter it out.
344         _filter_quota | grep -v "^\#0 \|^(null) "
345 }
346
347 # Account for different "ln" failure messages
348 _filter_ln()
349 {
350         sed -e "s,\(creating symbolic link .*\) to .*: ,\1: ," \
351             -e "s,failed to create,creating,"
352 }
353
354 # If given an arg, filter *that* UUID string
355 # Otherwise look for something that looks like a generic UUID
356 _filter_uuid()
357 {
358         if [ ! -z $1 ]; then
359                 UUID=$1
360                 sed -e "s/\(uuid[ :=]\+\) $UUID/\1 <EXACTUUID>/i"
361         else
362                 sed -e "s/\(uuid[ :=]\+\) [0-9a-f-][0-9a-f-]*/\1 <UUID>/ig"
363         fi
364 }
365
366 # In mixed group the added disks may have zero used size
367 _filter_zero_size()
368 {
369         sed -e "s/0\.00/<SIZE>/g"
370 }
371
372 # Filter out sizes like 6.14MB etc
373 _filter_size()
374 {
375         sed -e "s/[0-9\.]\+\s\?[b|k|m|g|t][i]\?[b]\?/<SIZE>/ig"
376 }
377
378 # Convert string read from stdin like 128K to bytes and print it to stdout
379 _filter_size_to_bytes()
380 {
381         read size
382         suffix=${size:${#size}-1}
383         mul=1
384         case $suffix in
385                 k|K) mul=1024 ;;
386                 m|M) mul=$((1024*1024)) ;;
387                 g|G) mul=$((1024*1024*1024)) ;;
388                 t|T) mul=$((1024*1024*1024*1024)) ;;
389         esac
390         echo $((${size:0:${#size}-1}*$mul))
391 }
392
393 # Print trimmed bytes of fstrim
394 # Starting from util-linux v2.23 fstrim usees human readable sizes in
395 # verbose output
396 _filter_fstrim()
397 {
398         egrep -o "[0-9]+ bytes" | $AWK_PROG '{print $1}'
399 }
400
401 # Remove the ending dot appended to mount error message, util-linux 2.30
402 # starts to do so.
403 _filter_ending_dot()
404 {
405         sed -e "s/\.$//"
406 }
407
408 # Older mount output referred to "block device" when mounting RO devices. It's
409 # gone in newer versions. v2.30 changed the output again. This filter is to
410 # unify all read-only mount messages across all util-linux versions.
411 #
412 # for a successful ro mount:
413 # ancient:         mount: block device <device> is write-protected, mounting read-only
414 # prior to v2.30:  mount: <device> is write-protected, mounting read-only
415 # v2.30 and later: mount: <mountpoint>: WARNING: device write-protected, mounted read-only.
416 #
417 # a failed ro mount:
418 # ancient (two-line message):
419 # mount: block device <device> is write-protected, mounting read-only
420 # mount: cannot mount block device <device> read-only
421 # prior to v2.30 (two-line message):
422 # mount: <device> is write-protected, mounting read-only
423 # mount: cannot mount <device> read-only
424 # v2.30 and later (single-line message):
425 # mount: <mountpoint>: cannot mount <device> read-only.
426 #
427 # a failed rw remount:
428 # ancient:         mount: cannot remount block device <device> read-write, is write-protected
429 # prior to v2.30:  mount: cannot remount <device> read-write, is write-protected
430 # v2.30 and later: mount: <mountpoint>: cannot remount <device> read-write, is write-protected.
431 #
432 # Now use _filter_ro_mount to unify all these differences across old & new
433 # util-linux versions. So the filtered format would be:
434 #
435 # successful ro mount:
436 # mount: device write-protected, mounting read-only
437 #
438 # failed ro mount:
439 # mount: device write-protected, mounting read-only
440 # mount: cannot mount device read-only
441 #
442 # failed rw remount:
443 # mount: cannot remount device read-write, is write-protected
444 _filter_ro_mount() {
445         perl -ne '
446         if (/write-protected, mount.*read-only/) {
447                 # filter successful ro mount, and first line of prior to v2.30
448                 # format failed ro mount
449                 print "mount: device write-protected, mounting read-only\n";
450         } elsif (/mount: .*: cannot mount.*read-only/) {
451                 # filter v2.30 format failed ro mount, convert single-line
452                 # message to two-line message
453                 print "mount: device write-protected, mounting read-only\n";
454                 print "mount: cannot mount device read-only\n";
455         } elsif (/^mount: cannot mount .* read-only$/) {
456                 # filter prior to v2.30 format failed ro mount
457                 print "mount: cannot mount device read-only\n";
458         } elsif (/mount:.* cannot remount .* read-write.*/) {
459                 # filter failed rw remount
460                 print "mount: cannot remount device read-write, is write-protected\n";
461         } else {
462                 print "$_";
463         }' | _filter_ending_dot
464 }
465
466 # Filter a failed mount output due to EUCLEAN and USTALE, util-linux changed
467 # the message several times.
468 #
469 # prior to v2.21:
470 # mount: Structure needs cleaning
471 # v2.21 to v2.29:
472 # mount: mount <device> on <mountpoint> failed: Structure needs cleaning
473 # v2.30 and later:
474 # mount: <mountpoint>: mount(2) system call failed: Structure needs cleaning.
475 #
476 # This is also true for ESTALE error. So let's remove all the changing parts
477 # and keep the 'prior to v2.21' format:
478 # mount: Structure needs cleaning
479 # mount: Stale file handle
480 _filter_error_mount()
481 {
482         sed -e "s/mount:\(.*failed:\)/mount:/" | _filter_ending_dot
483 }
484
485 # Similar to _filter_error_mount, filter a busy mount output.
486 # Turn both old (prior to util-linux v2.30) and new (v2.30 and later) format to
487 # a simple one. e.g.
488 # old: mount: <device> is already mounted or <mountpoint> busy
489 # new: mount: <mountpoint>: <device> already mounted or mount point busy.
490 # filtered: mount: device already mounted or mount point busy
491 _filter_busy_mount()
492 {
493         sed -e "s/.*: .* already mounted or .* busy/mount: device already mounted or mount point busy/" | \
494                 _filter_ending_dot
495 }
496
497 _filter_od()
498 {
499         BLOCK_SIZE=$(_get_block_size $SCRATCH_MNT)
500         $AWK_PROG -v block_size=$BLOCK_SIZE '
501                 /^[0-9]+/ {
502                         offset = strtonum("0"$1);
503                         $1 = sprintf("%o", offset / block_size);
504                         print $0;
505                 }
506                 /\*/
507         '
508 }
509
510 # Remove quotes from failed mknod calls. Starting with Coreutils v8.25,
511 # mknod errors print unquoted filenames
512 _filter_mknod()
513 {
514         sed -e "s/mknod: [\`']\(.*\)': File exists/mknod: \1: File exists/"
515 }
516
517 _filter_lostfound()
518 {
519         sed -e '/^lost+found$/d'
520 }
521
522 _filter_ovl_dirs()
523 {
524         sed -e "s,$OVL_LOWER,OVL_LOWER,g" \
525             -e "s,$OVL_UPPER,OVL_UPPER,g" \
526             -e "s,$OVL_WORK,OVL_WORK,g"
527 }
528
529 # interpret filefrag output,
530 # eg. "physical 1234, length 10, logical 5678" -> "1234#10#5678"
531 _filter_filefrag()
532 {
533         perl -ne '
534         if (/blocks? of (\d+) bytes/) {
535                 $blocksize = $1;
536                 next
537         }
538         ($ext, $logical, $physical, $length) =
539                 (/^\s*(\d+):\s+(\d+)..\s+\d+:\s+(\d+)..\s+\d+:\s+(\d+):/)
540         or next;
541         ($flags) = /.*:\s*(\S*)$/;
542         print $physical * $blocksize, "#",
543               $length * $blocksize, "#",
544               $logical * $blocksize, "#",
545               $flags, "\n"'
546 }
547
548 # We generate WARNINGs on purpose when applications mix buffered/mmap IO with
549 # direct IO on the same file. This is a helper for _check_dmesg() to filter out
550 # such warnings.
551 _filter_aiodio_dmesg()
552 {
553         local warn1="WARNING:.*fs/xfs/xfs_file\.c:.*xfs_file_dio_aio_write.*"
554         local warn2="WARNING:.*fs/xfs/xfs_file\.c:.*xfs_file_dio_aio_read.*"
555         local warn3="WARNING:.*fs/xfs/xfs_file\.c:.*xfs_file_read_iter.*"
556         local warn4="WARNING:.*fs/xfs/xfs_file\.c:.*xfs_file_aio_read.*"
557         local warn5="WARNING:.*fs/iomap\.c:.*iomap_dio_rw.*"
558         local warn6="WARNING:.*fs/xfs/xfs_aops\.c:.*__xfs_get_blocks.*"
559         local warn7="WARNING:.*fs/iomap\.c:.*iomap_dio_actor.*"
560         local warn8="WARNING:.*fs/iomap\.c:.*iomap_dio_complete.*"
561         local warn9="WARNING:.*fs/direct-io\.c:.*dio_complete.*"
562         sed -e "s#$warn1#Intentional warnings in xfs_file_dio_aio_write#" \
563             -e "s#$warn2#Intentional warnings in xfs_file_dio_aio_read#" \
564             -e "s#$warn3#Intentional warnings in xfs_file_read_iter#" \
565             -e "s#$warn4#Intentional warnings in xfs_file_aio_read#" \
566             -e "s#$warn5#Intentional warnings in iomap_dio_rw#" \
567             -e "s#$warn6#Intentional warnings in __xfs_get_blocks#" \
568             -e "s#$warn7#Intentional warnings in iomap_dio_actor#" \
569             -e "s#$warn8#Intentional warnings in iomap_dio_complete#" \
570             -e "s#$warn9#Intentional warnings in dio_complete#"
571 }
572
573 # make sure this script returns success
574 /bin/true