2 # XFS specific common functions.
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
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))
20 # mount the filesystem, create the file, unmount it
21 _try_scratch_mount 2>&1 >$tmp_dir/mnt.err
23 if [ $status -ne 0 ]; then
25 cat $tmp_dir/mnt.err >&2
26 rm -f $tmp_dir/mnt.err
29 rm -f $tmp_dir/mnt.err
32 -c "truncate $file_size" \
33 -c "falloc -k 0 $file_size" \
35 $SCRATCH_MNT/.use_space 2>&1 > /dev/null
36 export NUM_SPACE_FILES=1
39 if [ $status -ne 0 ]; then
40 echo "large file prealloc failed"
41 cat $tmp_dir/mnt.err >&2
47 _scratch_mkfs_xfs_opts()
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"`
58 echo "$MKFS_XFS_PROG $SCRATCH_OPTIONS $mkfs_opts"
62 _scratch_mkfs_xfs_supported()
68 $MKFS_XFS_PROG -N $MKFS_OPTIONS $SCRATCH_OPTIONS $mkfs_opts $SCRATCH_DEV
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
82 local mkfs_cmd="`_scratch_mkfs_xfs_opts`"
83 local mkfs_filter="sed -e '/less than device physical sector/d' \
84 -e '/switching to logical sector/d' \
85 -e '/Default configuration/d'"
89 _scratch_do_mkfs "$mkfs_cmd" "$mkfs_filter" $* 2>$tmp.mkfserr 1>$tmp.mkfsstd
93 if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
94 # manually parse the mkfs output to get the fs size in bytes
96 fs_size=`cat $tmp.mkfsstd | perl -ne '
97 if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+)/) {
99 print STDOUT "$size\n";
101 _setup_large_xfs_fs $fs_size
105 # output mkfs stdout and stderr
108 rm -f $tmp.mkfserr $tmp.mkfsstd
113 # xfs_check script is planned to be deprecated. But, we want to
114 # be able to invoke "xfs_check" behavior in xfstests in order to
115 # maintain the current verification levels.
120 USAGE="Usage: xfs_check [-fsvV] [-l logdev] [-i ino]... [-b bno]... special"
123 while getopts "b:fi:l:stvV" c; do
128 i) OPTS=$OPTS"-i "$OPTARG" ";;
129 b) OPTS=$OPTS"-b "$OPTARG" ";;
130 f) DBOPTS=$DBOPTS" -f";;
131 l) DBOPTS=$DBOPTS" -l "$OPTARG" ";;
132 V) $XFS_DB_PROG -p xfs_check -V
140 1) ${XFS_DB_PROG}${DBOPTS} -F -i -p xfs_check -c "check$OPTS" $1
150 _scratch_xfs_db_options()
153 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
154 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
155 echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
160 $XFS_DB_PROG "$@" $(_scratch_xfs_db_options)
163 _scratch_xfs_logprint()
166 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
167 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
168 $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
174 [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
175 TEST_OPTIONS="-l$TEST_LOGDEV"
176 $XFS_LOGPRINT_PROG $TEST_OPTIONS $* $TEST_DEV
182 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
183 SCRATCH_OPTIONS="-l $SCRATCH_LOGDEV"
184 [ "$LARGE_SCRATCH_DEV" = yes ] && \
185 SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
186 _xfs_check $SCRATCH_OPTIONS $* $SCRATCH_DEV
189 _scratch_xfs_repair()
192 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
193 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
194 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
195 SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
196 [ "$LARGE_SCRATCH_DEV" = yes ] && SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
197 $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
200 # this test requires the projid32bit feature to be available in mkfs.xfs.
202 _require_projid32bit()
204 _scratch_mkfs_xfs_supported -i projid32bit=1 >/dev/null 2>&1 \
205 || _notrun "mkfs.xfs doesn't have projid32bit feature"
208 _require_projid16bit()
210 _scratch_mkfs_xfs_supported -i projid32bit=0 >/dev/null 2>&1 \
211 || _notrun "16 bit project IDs not supported on $SCRATCH_DEV"
214 # this test requires the crc feature to be available in mkfs.xfs
216 _require_xfs_mkfs_crc()
218 _scratch_mkfs_xfs_supported -m crc=1 >/dev/null 2>&1 \
219 || _notrun "mkfs.xfs doesn't have crc feature"
222 # this test requires the xfs kernel support crc feature
226 _scratch_mkfs_xfs -m crc=1 >/dev/null 2>&1
227 _try_scratch_mount >/dev/null 2>&1 \
228 || _notrun "Kernel doesn't support crc feature"
232 # this test requires the xfs kernel support crc feature on scratch device
234 _require_scratch_xfs_crc()
236 _scratch_mkfs_xfs >/dev/null 2>&1
237 _try_scratch_mount >/dev/null 2>&1 \
238 || _notrun "Kernel doesn't support crc feature"
239 xfs_info $SCRATCH_MNT | grep -q 'crc=1' || _notrun "crc feature not supported by this filesystem"
243 # this test requires the finobt feature to be available in mkfs.xfs
245 _require_xfs_mkfs_finobt()
247 _scratch_mkfs_xfs_supported -m crc=1,finobt=1 >/dev/null 2>&1 \
248 || _notrun "mkfs.xfs doesn't have finobt feature"
251 # this test requires the xfs kernel support finobt feature
253 _require_xfs_finobt()
255 _scratch_mkfs_xfs -m crc=1,finobt=1 >/dev/null 2>&1
256 _try_scratch_mount >/dev/null 2>&1 \
257 || _notrun "Kernel doesn't support finobt feature"
261 # this test requires xfs sysfs attribute support
268 if [ ! -e $sysfsdir ]; then
269 _notrun "no kernel support for XFS sysfs attributes"
272 if [ ! -z $1 ] && [ ! -e $sysfsdir/$attr ]; then
273 _notrun "sysfs attribute '$attr' is not supported"
277 # this test requires the xfs sparse inode feature
279 _require_xfs_sparse_inodes()
281 _scratch_mkfs_xfs_supported -m crc=1 -i sparse > /dev/null 2>&1 \
282 || _notrun "mkfs.xfs does not support sparse inodes"
283 _scratch_mkfs_xfs -m crc=1 -i sparse > /dev/null 2>&1
284 _try_scratch_mount >/dev/null 2>&1 \
285 || _notrun "kernel does not support sparse inodes"
289 # check that xfs_db supports a specific command
290 _require_xfs_db_command()
292 if [ $# -ne 1 ]; then
293 echo "Usage: _require_xfs_db_command command" 1>&2
298 _scratch_xfs_db -x -c "help" | grep $command > /dev/null || \
299 _notrun "xfs_db $command support is missing"
302 # Does the filesystem mounted from a particular device support scrub?
303 _supports_xfs_scrub()
305 local mountpoint="$1"
308 if [ ! -b "$device" ] || [ ! -e "$mountpoint" ]; then
309 echo "Usage: _supports_xfs_scrub mountpoint device"
313 test "$FSTYP" = "xfs" || return 1
314 test -x "$XFS_SCRUB_PROG" || return 1
316 # Probe for kernel support...
317 $XFS_IO_PROG -c 'help scrub' 2>&1 | grep -q 'types are:.*probe' || return 1
318 $XFS_IO_PROG -c "scrub probe" "$mountpoint" 2>&1 | grep -q "Inappropriate ioctl" && return 1
320 # Scrub can't run on norecovery mounts
321 _fs_options "$device" | grep -q "norecovery" && return 1
326 # run xfs_check and friends on a FS.
327 _check_xfs_filesystem()
329 if [ $# -ne 3 ]; then
330 echo "Usage: _check_xfs_filesystem device <logdev>|none <rtdev>|none" 1>&2
334 extra_mount_options=""
338 if [ -f $device ]; then
342 if [ "$2" != "none" ]; then
343 extra_log_options="-l$2"
344 extra_mount_options="-ologdev=$2"
347 if [ "$3" != "none" ]; then
348 extra_rt_options="-r$3"
349 extra_mount_options=$extra_mount_options" -ortdev=$3"
351 extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"
353 [ "$FSTYP" != xfs ] && return 0
355 type=`_fs_type $device`
358 # Run online scrub if we can.
359 mntpt="$(_is_mounted $device)"
360 if [ -n "$mntpt" ] && _supports_xfs_scrub "$mntpt" "$device"; then
361 "$XFS_SCRUB_PROG" $scrubflag -v -d -n $device > $tmp.scrub 2>&1
362 if [ $? -ne 0 ]; then
363 _log_err "_check_xfs_filesystem: filesystem on $device failed scrub"
364 echo "*** xfs_scrub $scrubflag -v -d -n output ***" >> $seqres.full
365 cat $tmp.scrub >> $seqres.full
366 echo "*** end xfs_scrub output" >> $serqres.full
372 if [ "$type" = "xfs" ]; then
374 mountpoint=`_umount_or_remount_ro $device`
377 $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
378 | tee $tmp.logprint | grep -q "<CLEAN>"
379 if [ $? -ne 0 -a "$HOSTOS" = "Linux" ]; then
380 _log_err "_check_xfs_filesystem: filesystem on $device has dirty log"
381 echo "*** xfs_logprint -t output ***" >>$seqres.full
382 cat $tmp.logprint >>$seqres.full
383 echo "*** end xfs_logprint output" >>$seqres.full
388 # xfs_check runs out of memory on large files, so even providing the test
389 # option (-t) to avoid indexing the free space trees doesn't make it pass on
390 # large filesystems. Avoid it.
391 if [ "$LARGE_SCRATCH_DEV" != yes ]; then
392 _xfs_check $extra_log_options $device 2>&1 |\
393 _fix_malloc >$tmp.fs_check
395 if [ -s $tmp.fs_check ]; then
396 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (c)"
397 echo "*** xfs_check output ***" >>$seqres.full
398 cat $tmp.fs_check >>$seqres.full
399 echo "*** end xfs_check output" >>$seqres.full
404 $XFS_REPAIR_PROG -n $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
405 if [ $? -ne 0 ]; then
406 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (r)"
407 echo "*** xfs_repair -n output ***" >>$seqres.full
408 cat $tmp.repair | _fix_malloc >>$seqres.full
409 echo "*** end xfs_repair output" >>$seqres.full
413 rm -f $tmp.fs_check $tmp.logprint $tmp.repair
415 # Optionally test the index rebuilding behavior.
416 if [ -n "$TEST_XFS_REPAIR_REBUILD" ]; then
417 $XFS_REPAIR_PROG $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
418 if [ $? -ne 0 ]; then
419 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild)"
420 echo "*** xfs_repair output ***" >>$seqres.full
421 cat $tmp.repair | _fix_malloc >>$seqres.full
422 echo "*** end xfs_repair output" >>$seqres.full
428 $XFS_REPAIR_PROG -n $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
429 if [ $? -ne 0 ]; then
430 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild-reverify)"
431 echo "*** xfs_repair -n output ***" >>$seqres.full
432 cat $tmp.repair | _fix_malloc >>$seqres.full
433 echo "*** end xfs_repair output" >>$seqres.full
440 if [ $ok -eq 0 ]; then
441 echo "*** mount output ***" >>$seqres.full
442 _mount >>$seqres.full
443 echo "*** end mount output" >>$seqres.full
444 elif [ "$type" = "xfs" ]; then
445 _mount_or_remount_rw "$extra_mount_options" $device $mountpoint
448 if [ $ok -eq 0 ]; then
450 if [ "$iam" != "check" ]; then
463 [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
464 TEST_LOG="$TEST_LOGDEV"
466 [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
467 TEST_RT="$TEST_RTDEV"
469 _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT
473 _require_xfs_test_rmapbt()
477 if [ "$(xfs_info "$TEST_DIR" | grep -c "rmapbt=1")" -ne 1 ]; then
478 _notrun "rmapbt not supported by test filesystem type: $FSTYP"
482 _require_xfs_scratch_rmapbt()
486 _scratch_mkfs > /dev/null
488 if [ "$(xfs_info "$SCRATCH_MNT" | grep -c "rmapbt=1")" -ne 1 ]; then
490 _notrun "rmapbt not supported by scratch filesystem type: $FSTYP"
512 $XFS_IO_PROG -c "bmap -${param}lpv" "$file" | grep -c "$@"
515 # Reset all xfs error handling attributes, set them to original
518 # Only one argument, and it's mandatory:
519 # - dev: device name, e.g. $SCRATCH_DEV
521 # Note: this function only works for XFS
522 _reset_xfs_sysfs_error_handling()
526 if [ ! -b "$dev" -o "$FSTYP" != "xfs" ]; then
527 _fail "Usage: reset_xfs_sysfs_error_handling <device>"
530 _set_fs_sysfs_attr $dev error/fail_at_unmount 1
531 echo -n "error/fail_at_unmount="
532 _get_fs_sysfs_attr $dev error/fail_at_unmount
534 # Make sure all will be configured to retry forever by default, except
535 # for ENODEV, which is an unrecoverable error, so it will be configured
536 # to not retry on error by default.
537 for e in default EIO ENOSPC; do
538 _set_fs_sysfs_attr $dev \
539 error/metadata/${e}/max_retries -1
540 echo -n "error/metadata/${e}/max_retries="
541 _get_fs_sysfs_attr $dev error/metadata/${e}/max_retries
543 _set_fs_sysfs_attr $dev \
544 error/metadata/${e}/retry_timeout_seconds 0
545 echo -n "error/metadata/${e}/retry_timeout_seconds="
546 _get_fs_sysfs_attr $dev \
547 error/metadata/${e}/retry_timeout_seconds
551 # Skip if we are running an older binary without the stricter input checks.
552 # Make multiple checks to be sure that there is no regression on the one
553 # selected feature check, which would skew the result.
555 # At first, make a common function that runs the tests and returns
556 # number of failed cases.
557 _xfs_mkfs_validation_check()
559 local tmpfile=`mktemp`
560 local cmd="$MKFS_XFS_PROG -f -N -d file,name=$tmpfile,size=1g"
562 $cmd -s size=8s >/dev/null 2>&1
565 $cmd -l version=2,su=260k >/dev/null 2>&1
572 # Skip the test if all calls passed - mkfs accepts invalid input
573 _require_xfs_mkfs_validation()
575 _xfs_mkfs_validation_check
576 if [ "$?" -eq 0 ]; then
577 _notrun "Requires newer mkfs with stricter input checks: the oldest supported version of xfsprogs is 4.7."
581 # The opposite of _require_xfs_mkfs_validation.
582 _require_xfs_mkfs_without_validation()
584 _xfs_mkfs_validation_check
585 if [ "$?" -ne 0 ]; then
586 _notrun "Requires older mkfs without strict input checks: the last supported version of xfsprogs is 4.5."
590 # XFS ability to change UUIDs on V5/CRC filesystems
594 # This will create a crc fs on $SCRATCH_DEV
597 _scratch_xfs_db -x -c "uuid restore" 2>&1 \
598 | grep -q "invalid UUID\|supported on V5 fs" \
599 && _notrun "Userspace doesn't support meta_uuid feature"
601 _scratch_xfs_db -x -c "uuid generate" >/dev/null 2>&1
603 _try_scratch_mount >/dev/null 2>&1 \
604 || _notrun "Kernel doesn't support meta_uuid feature"
608 # this test requires mkfs.xfs have case-insensitive naming support
609 _require_xfs_mkfs_ciname()
611 _scratch_mkfs_xfs_supported -n version=ci >/dev/null 2>&1 \
612 || _notrun "need case-insensitive naming support in mkfs.xfs"
615 # XFS_DEBUG requirements
618 if grep -q "debug 0" /proc/fs/xfs/stat; then
619 _notrun "Require XFS built with CONFIG_XFS_DEBUG"
622 _require_no_xfs_debug()
624 if grep -q "debug 1" /proc/fs/xfs/stat; then
625 _notrun "Require XFS built without CONFIG_XFS_DEBUG"
629 # Require that assertions will not crash the system.
631 # Assertions would always crash the system if XFS assert fatal was enabled
632 # (CONFIG_XFS_ASSERT_FATAL=y). If a test is designed to trigger an assertion,
633 # skip the test on a CONFIG_XFS_ASSERT_FATAL built XFS by default. Note:
634 # CONFIG_XFS_ASSERT_FATAL can be disabled by setting bug_on_assert to zero if
635 # we want test to run.
636 _require_no_xfs_bug_on_assert()
638 if [ -f /sys/fs/xfs/debug/bug_on_assert ]; then
639 grep -q "1" /sys/fs/xfs/debug/bug_on_assert && \
640 _notrun "test requires XFS bug_on_assert to be off, turn it off to run the test"
642 # Note: Prior to the creation of CONFIG_XFS_ASSERT_FATAL (and
643 # the sysfs knob bug_on_assert), assertions would always crash
644 # the system if XFS debug was enabled (CONFIG_XFS_DEBUG=y). If
645 # a test is designed to trigger an assertion and the test
646 # designer does not want to hang fstests, skip the test.
647 _require_no_xfs_debug
651 # Get a metadata field
652 # The first arg is the field name
653 # The rest of the arguments are xfs_db commands to find the metadata.
654 _scratch_xfs_get_metadata_field()
659 local grep_key="$(echo "${key}" | tr '[]()' '....')"
663 cmds+=("-c" "${arg}")
665 _scratch_xfs_db "${cmds[@]}" -c "print ${key}" | grep "^${grep_key}" | \
669 # Set a metadata field
670 # The first arg is the field name
671 # The second arg is the new value
672 # The rest of the arguments are xfs_db commands to find the metadata.
673 _scratch_xfs_set_metadata_field()
682 cmds+=("-c" "${arg}")
686 _scratch_xfs_db -x -c "help write" | egrep -q "(-c|-d)" && value="-- ${value}"
687 _scratch_xfs_db -x -c "help write" | egrep -q "(-d)" && wr_cmd="${wr_cmd} -d"
688 _scratch_xfs_db -x "${cmds[@]}" -c "${wr_cmd} ${key} ${value}"
691 _scratch_xfs_get_sb_field()
693 _scratch_xfs_get_metadata_field "$1" "sb 0"
696 _scratch_xfs_set_sb_field()
698 _scratch_xfs_set_metadata_field "$1" "$2" "sb 0"