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