fstests: drop check.log and check.time into section specific results dir
[xfstests-dev.git] / common / xfs
1 #
2 # XFS specific common functions.
3 #
4
5 _setup_large_xfs_fs()
6 {
7         fs_size=$1
8         local tmp_dir=/tmp/
9
10         [ "$LARGE_SCRATCH_DEV" != yes ] && return 0
11         [ -z "$SCRATCH_DEV_EMPTY_SPACE" ] && SCRATCH_DEV_EMPTY_SPACE=0
12         [ $SCRATCH_DEV_EMPTY_SPACE -ge $fs_size ] && return 0
13
14         # calculate the size of the file we need to allocate.
15         # Default free space in the FS is 50GB, but you can specify more via
16         # SCRATCH_DEV_EMPTY_SPACE
17         file_size=$(($fs_size - 50*1024*1024*1024))
18         file_size=$(($file_size - $SCRATCH_DEV_EMPTY_SPACE))
19
20         # mount the filesystem, create the file, unmount it
21         _try_scratch_mount 2>&1 >$tmp_dir/mnt.err
22         local status=$?
23         if [ $status -ne 0 ]; then
24                 echo "mount failed"
25                 cat $tmp_dir/mnt.err >&2
26                 rm -f $tmp_dir/mnt.err
27                 return $status
28         fi
29         rm -f $tmp_dir/mnt.err
30
31         xfs_io -F -f \
32                 -c "truncate $file_size" \
33                 -c "falloc -k 0 $file_size" \
34                 -c "chattr +d" \
35                 $SCRATCH_MNT/.use_space 2>&1 > /dev/null
36         export NUM_SPACE_FILES=1
37         status=$?
38         _scratch_unmount
39         if [ $status -ne 0 ]; then
40                 echo "large file prealloc failed"
41                 cat $tmp_dir/mnt.err >&2
42                 return $status
43         fi
44         return 0
45 }
46
47 _scratch_mkfs_xfs_opts()
48 {
49         mkfs_opts=$*
50
51         # remove metadata related mkfs options if mkfs.xfs doesn't them
52         if [ -n "$XFS_MKFS_HAS_NO_META_SUPPORT" ]; then
53                 mkfs_opts=`echo $mkfs_opts | sed "s/-m\s\+\S\+//g"`
54         fi
55
56         _scratch_options mkfs
57
58         echo "$MKFS_XFS_PROG $SCRATCH_OPTIONS $mkfs_opts"
59 }
60
61
62 _scratch_mkfs_xfs_supported()
63 {
64         local mkfs_opts=$*
65
66         _scratch_options mkfs
67
68         $MKFS_XFS_PROG -N $MKFS_OPTIONS $SCRATCH_OPTIONS $mkfs_opts $SCRATCH_DEV
69         local mkfs_status=$?
70
71         # a mkfs failure may be caused by conflicts between $MKFS_OPTIONS and
72         # $mkfs_opts, try again without $MKFS_OPTIONS
73         if [ $mkfs_status -ne 0 -a -n "$mkfs_opts" ]; then
74                 $MKFS_XFS_PROG -N $SCRATCH_OPTIONS $mkfs_opts $SCRATCH_DEV
75                 mkfs_status=$?
76         fi
77         return $mkfs_status
78 }
79
80 # Returns the minimum XFS log size, in units of log blocks.
81 _scratch_find_xfs_min_logblocks()
82 {
83         local mkfs_cmd="`_scratch_mkfs_xfs_opts`"
84
85         # The smallest log size we can specify is 2M (XFS_MIN_LOG_BYTES) so
86         # pass that in and see if mkfs succeeds or tells us what is the
87         # minimum log size.
88         local XFS_MIN_LOG_BYTES=2097152
89
90         # Try formatting the filesystem with all the options given and the
91         # minimum log size.  We hope either that this succeeds or that mkfs
92         # tells us the required minimum log size for the feature set.
93         #
94         # We cannot use _scratch_do_mkfs because it will retry /any/ failed
95         # mkfs with MKFS_OPTIONS removed even if the only "failure" was that
96         # the log was too small.
97         local extra_mkfs_options="$* -N -l size=$XFS_MIN_LOG_BYTES"
98         eval "$mkfs_cmd $MKFS_OPTIONS $extra_mkfs_options $SCRATCH_DEV" \
99                 2>$tmp.mkfserr 1>$tmp.mkfsstd
100         local mkfs_status=$?
101
102         # If the format fails for a reason other than the log being too small,
103         # try again without MKFS_OPTIONS because that's what _scratch_do_mkfs
104         # will do if we pass in the log size option.
105         if [ $mkfs_status -ne 0 ] &&
106            ! egrep -q '(log size.*too small, minimum|external log device.*too small, must be)' $tmp.mkfserr; then
107                 eval "$mkfs_cmd $extra_mkfs_options $SCRATCH_DEV" \
108                         2>$tmp.mkfserr 1>$tmp.mkfsstd
109                 mkfs_status=$?
110         fi
111
112         # mkfs suceeded, so we must pick out the log block size to do the
113         # unit conversion
114         if [ $mkfs_status -eq 0 ]; then
115                 blksz="$(grep '^log.*bsize' $tmp.mkfsstd | \
116                         sed -e 's/log.*bsize=\([0-9]*\).*$/\1/g')"
117                 echo $((XFS_MIN_LOG_BYTES / blksz))
118                 rm -f $tmp.mkfsstd $tmp.mkfserr
119                 return
120         fi
121
122         # Usually mkfs will tell us the minimum log size...
123         if grep -q 'minimum size is' $tmp.mkfserr; then
124                 grep 'minimum size is' $tmp.mkfserr | \
125                         sed -e 's/^.*minimum size is \([0-9]*\) blocks/\1/g'
126                 rm -f $tmp.mkfsstd $tmp.mkfserr
127                 return
128         fi
129         if grep -q 'external log device.*too small, must be' $tmp.mkfserr; then
130                 grep 'external log device.*too small, must be' $tmp.mkfserr | \
131                         sed -e 's/^.*must be at least \([0-9]*\) blocks/\1/g'
132                 rm -f $tmp.mkfsstd $tmp.mkfserr
133                 return
134         fi
135
136         # Don't know what to do, so fail
137         echo "Cannot determine minimum log size" >&2
138         cat $tmp.mkfsstd >> $seqres.full
139         cat $tmp.mkfserr >> $seqres.full
140         rm -f $tmp.mkfsstd $tmp.mkfserr
141 }
142
143 _scratch_mkfs_xfs()
144 {
145         local mkfs_cmd="`_scratch_mkfs_xfs_opts`"
146         local mkfs_filter="sed -e '/less than device physical sector/d' \
147                                -e '/switching to logical sector/d' \
148                                -e '/Default configuration/d'"
149         local tmp=`mktemp -u`
150         local mkfs_status
151
152         _scratch_do_mkfs "$mkfs_cmd" "$mkfs_filter" $* 2>$tmp.mkfserr 1>$tmp.mkfsstd
153         mkfs_status=$?
154
155         grep -q crc=0 $tmp.mkfsstd && _force_xfsv4_mount_options
156
157         if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
158                 # manually parse the mkfs output to get the fs size in bytes
159                 local fs_size
160                 fs_size=`cat $tmp.mkfsstd | perl -ne '
161                         if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+)/) {
162                                 my $size = $1 * $2;
163                                 print STDOUT "$size\n";
164                         }'`
165                 _setup_large_xfs_fs $fs_size
166                 mkfs_status=$?
167         fi
168
169         # output mkfs stdout and stderr
170         cat $tmp.mkfsstd
171         cat $tmp.mkfserr >&2
172         rm -f $tmp.mkfserr $tmp.mkfsstd
173
174         return $mkfs_status
175 }
176
177 # xfs_check script is planned to be deprecated. But, we want to
178 # be able to invoke "xfs_check" behavior in xfstests in order to
179 # maintain the current verification levels.
180 _xfs_check()
181 {
182         OPTS=" "
183         DBOPTS=" "
184         USAGE="Usage: xfs_check [-fsvV] [-l logdev] [-i ino]... [-b bno]... special"
185
186         OPTIND=1
187         while getopts "b:fi:l:stvV" c; do
188                 case $c in
189                         s) OPTS=$OPTS"-s ";;
190                         t) OPTS=$OPTS"-t ";;
191                         v) OPTS=$OPTS"-v ";;
192                         i) OPTS=$OPTS"-i "$OPTARG" ";;
193                         b) OPTS=$OPTS"-b "$OPTARG" ";;
194                         f) DBOPTS=$DBOPTS" -f";;
195                         l) DBOPTS=$DBOPTS" -l "$OPTARG" ";;
196                         V) $XFS_DB_PROG -p xfs_check -V
197                            return $?
198                            ;;
199                 esac
200         done
201         set -- extra $@
202         shift $OPTIND
203         case $# in
204                 1) ${XFS_DB_PROG}${DBOPTS} -F -i -p xfs_check -c "check$OPTS" $1
205                    status=$?
206                    ;;
207                 2) echo $USAGE 1>&1
208                    status=2
209                    ;;
210         esac
211         return $status
212 }
213
214 _scratch_xfs_db_options()
215 {
216         SCRATCH_OPTIONS=""
217         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
218                 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
219         echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
220 }
221
222 _scratch_xfs_db()
223 {
224         $XFS_DB_PROG "$@" $(_scratch_xfs_db_options)
225 }
226
227 _test_xfs_db_options()
228 {
229         TEST_OPTIONS=""
230         [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
231                 TEST_OPTIONS="-l$TEST_LOGDEV"
232         echo $TEST_OPTIONS $* $TEST_DEV
233 }
234
235 _test_xfs_db()
236 {
237         $XFS_DB_PROG "$@" $(_test_xfs_db_options)
238 }
239
240 _scratch_xfs_admin()
241 {
242         local options=("$SCRATCH_DEV")
243         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
244                 options+=("$SCRATCH_LOGDEV")
245         $XFS_ADMIN_PROG "$@" "${options[@]}"
246 }
247
248 _scratch_xfs_logprint()
249 {
250         SCRATCH_OPTIONS=""
251         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
252                 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
253         $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
254 }
255
256 _test_xfs_logprint()
257 {
258         TEST_OPTIONS=""
259         [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
260                 TEST_OPTIONS="-l$TEST_LOGDEV"
261         $XFS_LOGPRINT_PROG $TEST_OPTIONS $* $TEST_DEV
262 }
263
264 _scratch_xfs_check()
265 {
266         SCRATCH_OPTIONS=""
267         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
268                 SCRATCH_OPTIONS="-l $SCRATCH_LOGDEV"
269         [ "$LARGE_SCRATCH_DEV" = yes ] && \
270                 SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
271         _xfs_check $SCRATCH_OPTIONS $* $SCRATCH_DEV
272 }
273
274 _scratch_xfs_repair()
275 {
276         SCRATCH_OPTIONS=""
277         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
278                 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
279         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
280                 SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
281         $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
282 }
283
284 # this test requires the projid32bit feature to be available in mkfs.xfs.
285 #
286 _require_projid32bit()
287 {
288        _scratch_mkfs_xfs_supported -i projid32bit=1 >/dev/null 2>&1 \
289            || _notrun "mkfs.xfs doesn't have projid32bit feature"
290 }
291
292 _require_projid16bit()
293 {
294         _scratch_mkfs_xfs_supported -i projid32bit=0 >/dev/null 2>&1 \
295            || _notrun "16 bit project IDs not supported on $SCRATCH_DEV"
296 }
297
298 # this test requires the crc feature to be available in mkfs.xfs
299 #
300 _require_xfs_mkfs_crc()
301 {
302         _scratch_mkfs_xfs_supported -m crc=1 >/dev/null 2>&1 \
303            || _notrun "mkfs.xfs doesn't have crc feature"
304 }
305
306 # this test requires the xfs kernel support crc feature
307 #
308 _require_xfs_crc()
309 {
310         _scratch_mkfs_xfs -m crc=1 >/dev/null 2>&1
311         _try_scratch_mount >/dev/null 2>&1 \
312            || _notrun "Kernel doesn't support crc feature"
313         _scratch_unmount
314 }
315
316 # this test requires the xfs kernel support crc feature on scratch device
317 #
318 _require_scratch_xfs_crc()
319 {
320         _scratch_mkfs_xfs >/dev/null 2>&1
321         _try_scratch_mount >/dev/null 2>&1 \
322            || _notrun "Kernel doesn't support crc feature"
323         $XFS_INFO_PROG $SCRATCH_MNT | grep -q 'crc=1' || _notrun "crc feature not supported by this filesystem"
324         _scratch_unmount
325 }
326
327 # this test requires the finobt feature to be available in mkfs.xfs
328 #
329 _require_xfs_mkfs_finobt()
330 {
331         _scratch_mkfs_xfs_supported -m crc=1,finobt=1 >/dev/null 2>&1 \
332            || _notrun "mkfs.xfs doesn't have finobt feature"
333 }
334
335 # this test requires the xfs kernel support finobt feature
336 #
337 _require_xfs_finobt()
338 {
339         _scratch_mkfs_xfs -m crc=1,finobt=1 >/dev/null 2>&1
340         _try_scratch_mount >/dev/null 2>&1 \
341            || _notrun "Kernel doesn't support finobt feature"
342         _scratch_unmount
343 }
344
345 # this test requires xfs sysfs attribute support
346 #
347 _require_xfs_sysfs()
348 {
349         attr=$1
350         sysfsdir=/sys/fs/xfs
351
352         if [ ! -e $sysfsdir ]; then
353                 _notrun "no kernel support for XFS sysfs attributes"
354         fi
355
356         if [ ! -z $1 ] && [ ! -e $sysfsdir/$attr ]; then
357                 _notrun "sysfs attribute '$attr' is not supported"
358         fi
359 }
360
361 # this test requires the xfs sparse inode feature
362 #
363 _require_xfs_sparse_inodes()
364 {
365         _scratch_mkfs_xfs_supported -m crc=1 -i sparse > /dev/null 2>&1 \
366                 || _notrun "mkfs.xfs does not support sparse inodes"
367         _scratch_mkfs_xfs -m crc=1 -i sparse > /dev/null 2>&1
368         _try_scratch_mount >/dev/null 2>&1 \
369                 || _notrun "kernel does not support sparse inodes"
370         _scratch_unmount
371 }
372
373 # check that xfs_db supports a specific command
374 _require_xfs_db_command()
375 {
376         if [ $# -ne 1 ]; then
377                 echo "Usage: _require_xfs_db_command command" 1>&2
378                 exit 1
379         fi
380         command=$1
381
382         _scratch_mkfs_xfs >/dev/null 2>&1
383         _scratch_xfs_db -x -c "help" | grep $command > /dev/null || \
384                 _notrun "xfs_db $command support is missing"
385 }
386
387 # Does the filesystem mounted from a particular device support scrub?
388 _supports_xfs_scrub()
389 {
390         local mountpoint="$1"
391         local device="$2"
392
393         if [ -z "$device" ] || [ -z "$mountpoint" ]; then
394                 echo "Usage: _supports_xfs_scrub mountpoint device"
395                 return 1
396         fi
397
398         if [ ! -b "$device" ] || [ ! -e "$mountpoint" ]; then
399                 return 1
400         fi
401
402         test "$FSTYP" = "xfs" || return 1
403         test -x "$XFS_SCRUB_PROG" || return 1
404
405         # Probe for kernel support...
406         $XFS_IO_PROG -c 'help scrub' 2>&1 | grep -q 'types are:.*probe' || return 1
407         $XFS_IO_PROG -c "scrub probe" "$mountpoint" 2>&1 | grep -q "Inappropriate ioctl" && return 1
408
409         # Scrub can't run on norecovery mounts
410         _fs_options "$device" | grep -q "norecovery" && return 1
411
412         return 0
413 }
414
415 # run xfs_check and friends on a FS.
416 _check_xfs_filesystem()
417 {
418         if [ $# -ne 3 ]; then
419                 echo "Usage: _check_xfs_filesystem device <logdev>|none <rtdev>|none" 1>&2
420                 exit 1
421         fi
422
423         extra_mount_options=""
424         extra_log_options=""
425         extra_options=""
426         device=$1
427         if [ -f $device ]; then
428                 extra_options="-f"
429         fi
430
431         if [ "$2" != "none" ]; then
432                 extra_log_options="-l$2"
433                 extra_mount_options="-ologdev=$2"
434         fi
435
436         if [ "$3" != "none" ]; then
437                 extra_rt_options="-r$3"
438                 extra_mount_options=$extra_mount_options" -ortdev=$3"
439         fi
440         extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"
441
442         [ "$FSTYP" != xfs ] && return 0
443
444         type=`_fs_type $device`
445         ok=1
446
447         # Run online scrub if we can.
448         mntpt="$(_is_dev_mounted $device)"
449         if [ -n "$mntpt" ] && _supports_xfs_scrub "$mntpt" "$device"; then
450                 "$XFS_SCRUB_PROG" $scrubflag -v -d -n $mntpt > $tmp.scrub 2>&1
451                 if [ $? -ne 0 ]; then
452                         _log_err "_check_xfs_filesystem: filesystem on $device failed scrub"
453                         echo "*** xfs_scrub $scrubflag -v -d -n output ***" >> $seqres.full
454                         cat $tmp.scrub >> $seqres.full
455                         echo "*** end xfs_scrub output" >> $serqres.full
456                         ok=0
457                 fi
458                 rm -f $tmp.scrub
459         fi
460
461         if [ "$type" = "xfs" ]; then
462                 # mounted ...
463                 mountpoint=`_umount_or_remount_ro $device`
464         fi
465
466         $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
467                 | tee $tmp.logprint | grep -q "<CLEAN>"
468         if [ $? -ne 0 ]; then
469                 _log_err "_check_xfs_filesystem: filesystem on $device has dirty log"
470                 echo "*** xfs_logprint -t output ***"   >>$seqres.full
471                 cat $tmp.logprint                       >>$seqres.full
472                 echo "*** end xfs_logprint output"      >>$seqres.full
473
474                 ok=0
475         fi
476
477         # xfs_check runs out of memory on large files, so even providing the test
478         # option (-t) to avoid indexing the free space trees doesn't make it pass on
479         # large filesystems. Avoid it.
480         if [ "$LARGE_SCRATCH_DEV" != yes ]; then
481                 _xfs_check $extra_log_options $device 2>&1 > $tmp.fs_check
482         fi
483         if [ -s $tmp.fs_check ]; then
484                 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (c)"
485                 echo "*** xfs_check output ***"         >>$seqres.full
486                 cat $tmp.fs_check                       >>$seqres.full
487                 echo "*** end xfs_check output"         >>$seqres.full
488
489                 ok=0
490         fi
491
492         $XFS_REPAIR_PROG -n $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
493         if [ $? -ne 0 ]; then
494                 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (r)"
495                 echo "*** xfs_repair -n output ***"     >>$seqres.full
496                 cat $tmp.repair                         >>$seqres.full
497                 echo "*** end xfs_repair output"        >>$seqres.full
498
499                 ok=0
500         fi
501         rm -f $tmp.fs_check $tmp.logprint $tmp.repair
502
503         # Optionally test the index rebuilding behavior.
504         if [ -n "$TEST_XFS_REPAIR_REBUILD" ]; then
505                 $XFS_REPAIR_PROG $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
506                 if [ $? -ne 0 ]; then
507                         _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild)"
508                         echo "*** xfs_repair output ***"        >>$seqres.full
509                         cat $tmp.repair                         >>$seqres.full
510                         echo "*** end xfs_repair output"        >>$seqres.full
511
512                         ok=0
513                 fi
514                 rm -f $tmp.repair
515
516                 $XFS_REPAIR_PROG -n $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
517                 if [ $? -ne 0 ]; then
518                         _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild-reverify)"
519                         echo "*** xfs_repair -n output ***"     >>$seqres.full
520                         cat $tmp.repair                         >>$seqres.full
521                         echo "*** end xfs_repair output"        >>$seqres.full
522
523                         ok=0
524                 fi
525                 rm -f $tmp.repair
526         fi
527
528         if [ $ok -eq 0 ]; then
529                 echo "*** mount output ***"             >>$seqres.full
530                 _mount                                  >>$seqres.full
531                 echo "*** end mount output"             >>$seqres.full
532         elif [ "$type" = "xfs" ]; then
533                 _mount_or_remount_rw "$extra_mount_options" $device $mountpoint
534         fi
535
536         if [ $ok -eq 0 ]; then
537                 status=1
538                 if [ "$iam" != "check" ]; then
539                         exit 1
540                 fi
541                 return 1
542         fi
543
544         return 0
545 }
546
547 _check_xfs_test_fs()
548 {
549         TEST_LOG="none"
550         TEST_RT="none"
551         [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
552                 TEST_LOG="$TEST_LOGDEV"
553
554         [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
555                 TEST_RT="$TEST_RTDEV"
556
557         _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT
558         return $?
559 }
560
561 _require_xfs_test_rmapbt()
562 {
563         _require_test
564
565         if [ "$($XFS_INFO_PROG "$TEST_DIR" | grep -c "rmapbt=1")" -ne 1 ]; then
566                 _notrun "rmapbt not supported by test filesystem type: $FSTYP"
567         fi
568 }
569
570 _require_xfs_scratch_rmapbt()
571 {
572         _require_scratch
573
574         _scratch_mkfs > /dev/null
575         _scratch_mount
576         if [ "$($XFS_INFO_PROG "$SCRATCH_MNT" | grep -c "rmapbt=1")" -ne 1 ]; then
577                 _scratch_unmount
578                 _notrun "rmapbt not supported by scratch filesystem type: $FSTYP"
579         fi
580         _scratch_unmount
581 }
582
583 _xfs_bmapx_find()
584 {
585         case "$1" in
586         "attr")
587                 param="a"
588                 ;;
589         "cow")
590                 param="c"
591                 ;;
592         *)
593                 param="e"
594                 ;;
595         esac
596         shift
597         file="$1"
598         shift
599
600         $XFS_IO_PROG -c "bmap -${param}lpv" "$file" | grep -c "$@"
601 }
602
603 # Reset all xfs error handling attributes, set them to original
604 # status.
605 #
606 # Only one argument, and it's mandatory:
607 #  - dev: device name, e.g. $SCRATCH_DEV
608 #
609 # Note: this function only works for XFS
610 _reset_xfs_sysfs_error_handling()
611 {
612         local dev=$1
613
614         if [ ! -b "$dev" -o "$FSTYP" != "xfs" ]; then
615                 _fail "Usage: reset_xfs_sysfs_error_handling <device>"
616         fi
617
618         _set_fs_sysfs_attr $dev error/fail_at_unmount 1
619         echo -n "error/fail_at_unmount="
620         _get_fs_sysfs_attr $dev error/fail_at_unmount
621
622         # Make sure all will be configured to retry forever by default, except
623         # for ENODEV, which is an unrecoverable error, so it will be configured
624         # to not retry on error by default.
625         for e in default EIO ENOSPC; do
626                 _set_fs_sysfs_attr $dev \
627                                    error/metadata/${e}/max_retries -1
628                 echo -n "error/metadata/${e}/max_retries="
629                 _get_fs_sysfs_attr $dev error/metadata/${e}/max_retries
630
631                 _set_fs_sysfs_attr $dev \
632                                    error/metadata/${e}/retry_timeout_seconds 0
633                 echo -n "error/metadata/${e}/retry_timeout_seconds="
634                 _get_fs_sysfs_attr $dev \
635                                    error/metadata/${e}/retry_timeout_seconds
636         done
637 }
638
639 # Skip if we are running an older binary without the stricter input checks.
640 # Make multiple checks to be sure that there is no regression on the one
641 # selected feature check, which would skew the result.
642 #
643 # At first, make a common function that runs the tests and returns
644 # number of failed cases.
645 _xfs_mkfs_validation_check()
646 {
647         local tmpfile=`mktemp`
648         local cmd="$MKFS_XFS_PROG -f -N -d file,name=$tmpfile,size=1g"
649
650         $cmd -s size=8s >/dev/null 2>&1
651         local sum=$?
652
653         $cmd -l version=2,su=260k >/dev/null 2>&1
654         sum=`expr $sum + $?`
655
656         rm -f $tmpfile
657         return $sum
658 }
659
660 # Skip the test if all calls passed - mkfs accepts invalid input
661 _require_xfs_mkfs_validation()
662 {
663         _xfs_mkfs_validation_check
664         if [ "$?" -eq 0 ]; then
665                 _notrun "Requires newer mkfs with stricter input checks: the oldest supported version of xfsprogs is 4.7."
666         fi
667 }
668
669 # The opposite of _require_xfs_mkfs_validation.
670 _require_xfs_mkfs_without_validation()
671 {
672         _xfs_mkfs_validation_check
673         if [ "$?" -ne 0 ]; then
674                 _notrun "Requires older mkfs without strict input checks: the last supported version of xfsprogs is 4.5."
675         fi
676 }
677
678 # XFS ability to change UUIDs on V5/CRC filesystems
679 #
680 _require_meta_uuid()
681 {
682         # This will create a crc fs on $SCRATCH_DEV
683         _require_xfs_crc
684
685         _scratch_xfs_db -x -c "uuid restore" 2>&1 \
686            | grep -q "invalid UUID\|supported on V5 fs" \
687            && _notrun "Userspace doesn't support meta_uuid feature"
688
689         _scratch_xfs_db -x -c "uuid generate" >/dev/null 2>&1
690
691         _try_scratch_mount >/dev/null 2>&1 \
692            || _notrun "Kernel doesn't support meta_uuid feature"
693         _scratch_unmount
694 }
695
696 # this test requires mkfs.xfs have case-insensitive naming support
697 _require_xfs_mkfs_ciname()
698 {
699         _scratch_mkfs_xfs_supported -n version=ci >/dev/null 2>&1 \
700                 || _notrun "need case-insensitive naming support in mkfs.xfs"
701 }
702
703 # XFS_DEBUG requirements
704 _require_xfs_debug()
705 {
706         if grep -q "debug 0" /proc/fs/xfs/stat; then
707                 _notrun "Require XFS built with CONFIG_XFS_DEBUG"
708         fi
709 }
710 _require_no_xfs_debug()
711 {
712         if grep -q "debug 1" /proc/fs/xfs/stat; then
713                 _notrun "Require XFS built without CONFIG_XFS_DEBUG"
714         fi
715 }
716
717 # Require that assertions will not crash the system.
718 #
719 # Assertions would always crash the system if XFS assert fatal was enabled
720 # (CONFIG_XFS_ASSERT_FATAL=y).  If a test is designed to trigger an assertion,
721 # skip the test on a CONFIG_XFS_ASSERT_FATAL built XFS by default.  Note:
722 # CONFIG_XFS_ASSERT_FATAL can be disabled by setting bug_on_assert to zero if
723 # we want test to run.
724 _require_no_xfs_bug_on_assert()
725 {
726         if [ -f /sys/fs/xfs/debug/bug_on_assert ]; then
727                 grep -q "1" /sys/fs/xfs/debug/bug_on_assert && \
728                    _notrun "test requires XFS bug_on_assert to be off, turn it off to run the test"
729         else
730                 # Note: Prior to the creation of CONFIG_XFS_ASSERT_FATAL (and
731                 # the sysfs knob bug_on_assert), assertions would always crash
732                 # the system if XFS debug was enabled (CONFIG_XFS_DEBUG=y).  If
733                 # a test is designed to trigger an assertion and the test
734                 # designer does not want to hang fstests, skip the test.
735                 _require_no_xfs_debug
736         fi
737 }
738
739 # Get a metadata field
740 # The first arg is the field name
741 # The rest of the arguments are xfs_db commands to find the metadata.
742 _scratch_xfs_get_metadata_field()
743 {
744         local key="$1"
745         shift
746
747         local grep_key="$(echo "${key}" | tr '[]()' '....')"
748         local cmds=()
749         local arg
750         for arg in "$@"; do
751                 cmds+=("-c" "${arg}")
752         done
753         _scratch_xfs_db "${cmds[@]}" -c "print ${key}" | grep "^${grep_key}" | \
754                 sed -e 's/^.* = //g'
755 }
756
757 # Set a metadata field
758 # The first arg is the field name
759 # The second arg is the new value
760 # The rest of the arguments are xfs_db commands to find the metadata.
761 _scratch_xfs_set_metadata_field()
762 {
763         local key="$1"
764         local value="$2"
765         shift; shift
766
767         local cmds=()
768         local arg
769         for arg in "$@"; do
770                 cmds+=("-c" "${arg}")
771         done
772
773         local wr_cmd="write"
774         _scratch_xfs_db -x -c "help write" | egrep -q "(-c|-d)" && value="-- ${value}"
775         _scratch_xfs_db -x -c "help write" | egrep -q "(-d)" && wr_cmd="${wr_cmd} -d"
776         _scratch_xfs_db -x "${cmds[@]}" -c "${wr_cmd} ${key} ${value}"
777 }
778
779 _scratch_xfs_get_sb_field()
780 {
781         _scratch_xfs_get_metadata_field "$1" "sb 0"
782 }
783
784 _scratch_xfs_set_sb_field()
785 {
786         _scratch_xfs_set_metadata_field "$1" "$2" "sb 0"
787 }
788
789 # Before xfsprogs commit 4222d000ed("db: write via array indexing doesn't
790 # work"), xfs_db command to write a specific AGFL index doesn't work. It's a
791 # bug in a diagnostic tool that is only used by XFS developers as a test
792 # infrastructure, so it's fine to treat it as a infrastructure dependency as
793 # all other _require rules.
794 _require_xfs_db_write_array()
795 {
796         local supported=0
797
798         _require_test
799         touch $TEST_DIR/$seq.img
800         $MKFS_XFS_PROG -d file,name=$TEST_DIR/$seq.img,size=512m >/dev/null 2>&1
801         $XFS_DB_PROG -x -c "agfl 0" -c "write bno[32] 78" $TEST_DIR/$seq.img \
802                 >/dev/null 2>&1
803         $XFS_DB_PROG -x -c "agfl 0" -c "print bno[32]" $TEST_DIR/$seq.img \
804                 | grep -q "bno\[32\] = 78" && supported=1
805         rm -f $TEST_DIR/$seq.img
806         [ $supported -eq 0 ] && _notrun "xfs_db write can't support array"
807 }
808
809 _require_xfs_spaceman_command()
810 {
811         if [ -z "$1" ]; then
812                 echo "Usage: _require_xfs_spaceman_command command [switch]" 1>&2
813                 exit 1
814         fi
815         local command=$1
816         shift
817         local param="$*"
818         local param_checked=0
819         local opts=""
820
821         _require_command "$XFS_SPACEMAN_PROG" "xfs_spaceman"
822
823         testfile=$TEST_DIR/$$.xfs_spaceman
824         touch $testfile
825         case $command in
826         "health")
827                 testio=`$XFS_SPACEMAN_PROG -c "health $param" $TEST_DIR 2>&1`
828                 param_checked=1
829                 ;;
830         *)
831                 testio=`$XFS_SPACEMAN_PROG -c "help $command" $TEST_DIR 2>&1`
832         esac
833
834         rm -f $testfile 2>&1 > /dev/null
835         echo $testio | grep -q "not found" && \
836                 _notrun "xfs_spaceman $command support is missing"
837         echo $testio | grep -q "Operation not supported" && \
838                 _notrun "xfs_spaceman $command failed (old kernel/wrong fs?)"
839         echo $testio | grep -q "Invalid" && \
840                 _notrun "xfs_spaceman $command failed (old kernel/wrong fs/bad args?)"
841         echo $testio | grep -q "foreign file active" && \
842                 _notrun "xfs_spaceman $command not supported on $FSTYP"
843         echo $testio | grep -q "Inappropriate ioctl for device" && \
844                 _notrun "xfs_spaceman $command support is missing (missing ioctl?)"
845         echo $testio | grep -q "Function not implemented" && \
846                 _notrun "xfs_spaceman $command support is missing (missing syscall?)"
847
848         [ -n "$param" ] || return
849
850         if [ $param_checked -eq 0 ]; then
851                 $XFS_SPACEMAN_PROG -c "help $command" | grep -q "^ $param --" || \
852                         _notrun "xfs_spaceman $command doesn't support $param"
853         fi
854 }
855
856 _scratch_get_sfdir_prefix() {
857         local dir_ino="$1"
858
859         for prefix in "u.sfdir3" "u.sfdir2" "u3.sfdir3"; do
860                 if [ -n "$(_scratch_xfs_get_metadata_field \
861                                 "${prefix}.hdr.parent.i4" \
862                                 "inode ${dir_ino}")" ]; then
863                         echo "${prefix}"
864                         return 0
865                 fi
866         done
867         _scratch_xfs_db -c "inode ${dir_ino}" -c 'p' >> $seqres.full
868         return 1
869 }
870
871 _scratch_get_bmx_prefix() {
872         local ino="$1"
873
874         for prefix in "u3.bmx" "u.bmx"; do
875                 if [ -n "$(_scratch_xfs_get_metadata_field \
876                                 "${prefix}[0].startblock" \
877                                 "inode ${ino}")" ]; then
878                         echo "${prefix}"
879                         return 0
880                 fi
881         done
882         _scratch_xfs_db -c "inode ${ino}" -c 'p' >> $seqres.full
883         return 1
884 }
885
886 #
887 # Ensures that we don't pass any mount options incompatible with XFS v4
888 #
889 _force_xfsv4_mount_options()
890 {
891         local gquota=0
892         local pquota=0
893
894         # Can't have group and project quotas in XFS v4
895         echo "$MOUNT_OPTIONS" | egrep -q "(gquota|grpquota|grpjquota=|gqnoenforce)" && gquota=1
896         echo "$MOUNT_OPTIONS" | egrep -q "(\bpquota|prjquota|pqnoenforce)" && pquota=1
897
898         if [ $gquota -gt 0 ] && [ $pquota -gt 0 ]; then
899                 export MOUNT_OPTIONS=$(echo $MOUNT_OPTIONS \
900                         | sed   -e 's/gquota/QUOTA/g'      \
901                                 -e 's/grpquota/QUOTA/g'    \
902                                 -e 's/grpjquota=[^, ]/QUOTA/g' \
903                                 -e 's/gqnoenforce/QUOTA/g' \
904                                 -e "s/QUOTA/defaults/g")
905         fi
906         echo "MOUNT_OPTIONS = $MOUNT_OPTIONS" >>$seqres.full
907 }
908
909 # Find AG count of mounted filesystem
910 _xfs_mount_agcount()
911 {
912         $XFS_INFO_PROG "$1" | grep agcount= | sed -e 's/^.*agcount=\([0-9]*\),.*$/\1/g'
913 }
914
915 # Wipe the superblock of each XFS AGs
916 _try_wipe_scratch_xfs()
917 {
918         local num='^[0-9]+$'
919         local agcount
920         local agsize
921         local dbsize
922
923         # Try to wipe each SB if there's an existed XFS
924         agcount=`_scratch_xfs_get_sb_field agcount 2>/dev/null`
925         agsize=`_scratch_xfs_get_sb_field agblocks 2>/dev/null`
926         dbsize=`_scratch_xfs_get_sb_field blocksize 2>/dev/null`
927         if [[ $agcount =~ $num && $agsize =~ $num && $dbsize =~ $num ]];then
928                 for ((i = 0; i < agcount; i++)); do
929                         $XFS_IO_PROG -c "pwrite $((i * dbsize * agsize)) $dbsize" \
930                                 $SCRATCH_DEV >/dev/null;
931                 done
932         fi
933
934         # Try to wipe each SB by default mkfs.xfs geometry
935         local tmp=`mktemp -u`
936         unset agcount agsize dbsize
937         _scratch_mkfs_xfs -N 2>/dev/null | perl -ne '
938                 if (/^meta-data=.*\s+agcount=(\d+), agsize=(\d+) blks/) {
939                         print STDOUT "agcount=$1\nagsize=$2\n";
940                 }
941                 if (/^data\s+=\s+bsize=(\d+)\s/) {
942                         print STDOUT "dbsize=$1\n";
943                 }' > $tmp.mkfs
944
945         . $tmp.mkfs
946         if [[ $agcount =~ $num && $agsize =~ $num && $dbsize =~ $num ]];then
947                 for ((i = 0; i < agcount; i++)); do
948                         $XFS_IO_PROG -c "pwrite $((i * dbsize * agsize)) $dbsize" \
949                                 $SCRATCH_DEV >/dev/null;
950                 done
951         fi
952         rm -f $tmp.mkfs
953 }
954
955 _require_xfs_copy()
956 {
957         [ -n "$XFS_COPY_PROG" ] || _notrun "xfs_copy binary not yet installed"
958         [ "$USE_EXTERNAL" = yes ] && \
959                 _notrun "Cannot xfs_copy with external devices"
960 }