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
80 # Returns the minimum XFS log size, in units of log blocks.
81 _scratch_find_xfs_min_logblocks()
83 local mkfs_cmd="`_scratch_mkfs_xfs_opts`"
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
88 local XFS_MIN_LOG_BYTES=2097152
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.
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
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
112 # mkfs suceeded, so we must pick out the log block size to do the
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
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
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
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
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`
152 _scratch_do_mkfs "$mkfs_cmd" "$mkfs_filter" $* 2>$tmp.mkfserr 1>$tmp.mkfsstd
155 grep -q crc=0 $tmp.mkfsstd && _force_xfsv4_mount_options
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
160 fs_size=`cat $tmp.mkfsstd | perl -ne '
161 if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+)/) {
163 print STDOUT "$size\n";
165 _setup_large_xfs_fs $fs_size
169 # output mkfs stdout and stderr
172 rm -f $tmp.mkfserr $tmp.mkfsstd
177 # Get the size of an allocation unit of a file. Normally this is just the
178 # block size of the file, but for realtime files, this is the realtime extent
180 _xfs_get_file_block_size()
184 if ! ($XFS_IO_PROG -c "stat -v" "$path" 2>&1 | egrep -q '(rt-inherit|realtime)'); then
185 _get_block_size "$path"
189 # Otherwise, call xfs_info until we find a mount point or the root.
190 path="$(readlink -m "$path")"
191 while ! $XFS_INFO_PROG "$path" &>/dev/null && [ "$path" != "/" ]; do
192 path="$(dirname "$path")"
194 $XFS_INFO_PROG "$path" | grep realtime | sed -e 's/^.*extsz=\([0-9]*\).*$/\1/g'
202 local value=$($XFS_IO_PROG -c "stat" "$path" | grep -w "$field")
203 echo ${value##fsxattr.${field} = }
206 # xfs_check script is planned to be deprecated. But, we want to
207 # be able to invoke "xfs_check" behavior in xfstests in order to
208 # maintain the current verification levels.
213 USAGE="Usage: xfs_check [-fsvV] [-l logdev] [-i ino]... [-b bno]... special"
216 while getopts "b:fi:l:stvV" c; do
221 i) OPTS=$OPTS"-i "$OPTARG" ";;
222 b) OPTS=$OPTS"-b "$OPTARG" ";;
223 f) DBOPTS=$DBOPTS" -f";;
224 l) DBOPTS=$DBOPTS" -l "$OPTARG" ";;
225 V) $XFS_DB_PROG -p xfs_check -V
233 1) ${XFS_DB_PROG}${DBOPTS} -F -i -p xfs_check -c "check$OPTS" $1
243 _scratch_xfs_db_options()
246 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
247 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
248 echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
253 $XFS_DB_PROG "$@" $(_scratch_xfs_db_options)
256 _test_xfs_db_options()
259 [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
260 TEST_OPTIONS="-l$TEST_LOGDEV"
261 echo $TEST_OPTIONS $* $TEST_DEV
266 $XFS_DB_PROG "$@" $(_test_xfs_db_options)
271 local options=("$SCRATCH_DEV")
272 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
273 options+=("$SCRATCH_LOGDEV")
274 $XFS_ADMIN_PROG "$@" "${options[@]}"
277 _scratch_xfs_logprint()
280 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
281 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
282 $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
288 [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
289 TEST_OPTIONS="-l$TEST_LOGDEV"
290 $XFS_LOGPRINT_PROG $TEST_OPTIONS $* $TEST_DEV
296 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
297 SCRATCH_OPTIONS="-l $SCRATCH_LOGDEV"
298 [ "$LARGE_SCRATCH_DEV" = yes ] && \
299 SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
300 _xfs_check $SCRATCH_OPTIONS $* $SCRATCH_DEV
303 _scratch_xfs_repair()
306 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
307 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
308 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
309 SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
310 $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
313 # this test requires the projid32bit feature to be available in mkfs.xfs.
315 _require_projid32bit()
317 _scratch_mkfs_xfs_supported -i projid32bit=1 >/dev/null 2>&1 \
318 || _notrun "mkfs.xfs doesn't have projid32bit feature"
321 _require_projid16bit()
323 _scratch_mkfs_xfs_supported -i projid32bit=0 >/dev/null 2>&1 \
324 || _notrun "16 bit project IDs not supported on $SCRATCH_DEV"
327 # this test requires the crc feature to be available in mkfs.xfs
329 _require_xfs_mkfs_crc()
331 _scratch_mkfs_xfs_supported -m crc=1 >/dev/null 2>&1 \
332 || _notrun "mkfs.xfs doesn't have crc feature"
335 # this test requires the xfs kernel support crc feature
339 _scratch_mkfs_xfs -m crc=1 >/dev/null 2>&1
340 _try_scratch_mount >/dev/null 2>&1 \
341 || _notrun "Kernel doesn't support crc feature"
345 # this test requires the xfs kernel support crc feature on scratch device
347 _require_scratch_xfs_crc()
349 _scratch_mkfs_xfs >/dev/null 2>&1
350 _try_scratch_mount >/dev/null 2>&1 \
351 || _notrun "Kernel doesn't support crc feature"
352 $XFS_INFO_PROG $SCRATCH_MNT | grep -q 'crc=1' || _notrun "crc feature not supported by this filesystem"
356 # this test requires the finobt feature to be available in mkfs.xfs
358 _require_xfs_mkfs_finobt()
360 _scratch_mkfs_xfs_supported -m crc=1,finobt=1 >/dev/null 2>&1 \
361 || _notrun "mkfs.xfs doesn't have finobt feature"
364 # this test requires the xfs kernel support finobt feature
366 _require_xfs_finobt()
368 _scratch_mkfs_xfs -m crc=1,finobt=1 >/dev/null 2>&1
369 _try_scratch_mount >/dev/null 2>&1 \
370 || _notrun "Kernel doesn't support finobt feature"
374 # this test requires xfs sysfs attribute support
381 if [ ! -e $sysfsdir ]; then
382 _notrun "no kernel support for XFS sysfs attributes"
385 if [ ! -z $1 ] && [ ! -e $sysfsdir/$attr ]; then
386 _notrun "sysfs attribute '$attr' is not supported"
390 # this test requires the xfs sparse inode feature
392 _require_xfs_sparse_inodes()
394 _scratch_mkfs_xfs_supported -m crc=1 -i sparse > /dev/null 2>&1 \
395 || _notrun "mkfs.xfs does not support sparse inodes"
396 _scratch_mkfs_xfs -m crc=1 -i sparse > /dev/null 2>&1
397 _try_scratch_mount >/dev/null 2>&1 \
398 || _notrun "kernel does not support sparse inodes"
402 # check that xfs_db supports a specific command
403 _require_xfs_db_command()
405 if [ $# -ne 1 ]; then
406 echo "Usage: _require_xfs_db_command command" 1>&2
411 _scratch_mkfs_xfs >/dev/null 2>&1
412 _scratch_xfs_db -x -c "help" | grep $command > /dev/null || \
413 _notrun "xfs_db $command support is missing"
416 # Does the filesystem mounted from a particular device support scrub?
417 _supports_xfs_scrub()
419 local mountpoint="$1"
422 if [ -z "$device" ] || [ -z "$mountpoint" ]; then
423 echo "Usage: _supports_xfs_scrub mountpoint device"
427 if [ ! -b "$device" ] || [ ! -e "$mountpoint" ]; then
431 test "$FSTYP" = "xfs" || return 1
432 test -x "$XFS_SCRUB_PROG" || return 1
434 # Probe for kernel support...
435 $XFS_IO_PROG -c 'help scrub' 2>&1 | grep -q 'types are:.*probe' || return 1
436 $XFS_IO_PROG -c "scrub probe" "$mountpoint" 2>&1 | grep -q "Inappropriate ioctl" && return 1
438 # Scrub can't run on norecovery mounts
439 _fs_options "$device" | grep -q "norecovery" && return 1
444 # Save a snapshot of a corrupt xfs filesystem for later debugging.
449 local compressopt="$4"
450 shift; shift; shift; shift
452 test -z "$options" && options="-a -o"
454 if [ "$logdev" != "none" ]; then
455 options="$options -l $logdev"
458 $XFS_METADUMP_PROG $options "$device" "$metadump"
460 [ "$compressopt" = "compress" ] && [ -n "$DUMP_COMPRESSOR" ] &&
461 $DUMP_COMPRESSOR "$metadump" &> /dev/null
465 # Snapshot the metadata on the scratch device
466 _scratch_xfs_metadump()
472 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
473 logdev=$SCRATCH_LOGDEV
475 _xfs_metadump "$metadump" "$SCRATCH_DEV" "$logdev" nocompress "$@"
478 # run xfs_check and friends on a FS.
479 _check_xfs_filesystem()
481 if [ $# -ne 3 ]; then
482 echo "Usage: _check_xfs_filesystem device <logdev>|none <rtdev>|none" 1>&2
486 extra_mount_options=""
490 if [ -f $device ]; then
495 if [ "$logdev" != "none" ]; then
496 extra_log_options="-l$logdev"
497 extra_mount_options="-ologdev=$logdev"
501 if [ "$rtdev" != "none" ]; then
502 extra_rt_options="-r$rtdev"
503 extra_mount_options=$extra_mount_options" -ortdev=$rtdev"
505 extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"
507 [ "$FSTYP" != xfs ] && return 0
509 type=`_fs_type $device`
512 # Run online scrub if we can.
513 mntpt="$(_is_dev_mounted $device)"
514 if [ -n "$mntpt" ] && _supports_xfs_scrub "$mntpt" "$device"; then
515 # Tests can create a scenario in which a call to syncfs() issued
516 # at the end of the execution of the test script would return an
517 # error code. xfs_scrub internally calls syncfs() before
518 # starting the actual online consistency check operation. Since
519 # such a call to syncfs() fails, xfs_scrub ends up returning
520 # without performing consistency checks on the test
521 # filesystem. This can mask a possible on-disk data structure
522 # corruption. Hence consume such a possible syncfs() failure
523 # before executing a scrub operation.
524 $XFS_IO_PROG -c syncfs $mntpt >> $seqres.full 2>&1
526 "$XFS_SCRUB_PROG" $scrubflag -v -d -n $mntpt > $tmp.scrub 2>&1
527 if [ $? -ne 0 ]; then
528 _log_err "_check_xfs_filesystem: filesystem on $device failed scrub"
529 echo "*** xfs_scrub $scrubflag -v -d -n output ***" >> $seqres.full
530 cat $tmp.scrub >> $seqres.full
531 echo "*** end xfs_scrub output" >> $serqres.full
537 if [ "$type" = "xfs" ]; then
539 mountpoint=`_umount_or_remount_ro $device`
542 $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
543 | tee $tmp.logprint | grep -q "<CLEAN>"
544 if [ $? -ne 0 ]; then
545 _log_err "_check_xfs_filesystem: filesystem on $device has dirty log"
546 echo "*** xfs_logprint -t output ***" >>$seqres.full
547 cat $tmp.logprint >>$seqres.full
548 echo "*** end xfs_logprint output" >>$seqres.full
553 # xfs_check runs out of memory on large files, so even providing the test
554 # option (-t) to avoid indexing the free space trees doesn't make it pass on
555 # large filesystems. Avoid it.
556 if [ "$LARGE_SCRATCH_DEV" != yes ]; then
557 _xfs_check $extra_log_options $device 2>&1 > $tmp.fs_check
559 if [ -s $tmp.fs_check ]; then
560 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (c)"
561 echo "*** xfs_check output ***" >>$seqres.full
562 cat $tmp.fs_check >>$seqres.full
563 echo "*** end xfs_check output" >>$seqres.full
568 $XFS_REPAIR_PROG -n $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
569 if [ $? -ne 0 ]; then
570 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (r)"
571 echo "*** xfs_repair -n output ***" >>$seqres.full
572 cat $tmp.repair >>$seqres.full
573 echo "*** end xfs_repair output" >>$seqres.full
577 rm -f $tmp.fs_check $tmp.logprint $tmp.repair
579 if [ "$ok" -ne 1 ] && [ "$DUMP_CORRUPT_FS" = "1" ]; then
580 local flatdev="$(basename "$device")"
581 _xfs_metadump "$seqres.$flatdev.check.md" "$device" "$logdev" \
582 compress >> $seqres.full
585 # Optionally test the index rebuilding behavior.
586 if [ -n "$TEST_XFS_REPAIR_REBUILD" ]; then
588 $XFS_REPAIR_PROG $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
589 if [ $? -ne 0 ]; then
590 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild)"
591 echo "*** xfs_repair output ***" >>$seqres.full
592 cat $tmp.repair >>$seqres.full
593 echo "*** end xfs_repair output" >>$seqres.full
600 $XFS_REPAIR_PROG -n $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
601 if [ $? -ne 0 ]; then
602 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild-reverify)"
603 echo "*** xfs_repair -n output ***" >>$seqres.full
604 cat $tmp.repair >>$seqres.full
605 echo "*** end xfs_repair output" >>$seqres.full
612 if [ "$rebuild_ok" -ne 1 ] && [ "$DUMP_CORRUPT_FS" = "1" ]; then
613 local flatdev="$(basename "$device")"
614 _xfs_metadump "$seqres.$flatdev.rebuild.md" "$device" \
615 "$logdev" compress >> $seqres.full
619 if [ $ok -eq 0 ]; then
620 echo "*** mount output ***" >>$seqres.full
621 _mount >>$seqres.full
622 echo "*** end mount output" >>$seqres.full
623 elif [ "$type" = "xfs" ]; then
624 _mount_or_remount_rw "$extra_mount_options" $device $mountpoint
627 if [ $ok -eq 0 ]; then
629 if [ "$iam" != "check" ]; then
642 [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
643 TEST_LOG="$TEST_LOGDEV"
645 [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
646 TEST_RT="$TEST_RTDEV"
648 _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT
652 _require_xfs_test_rmapbt()
656 if [ "$($XFS_INFO_PROG "$TEST_DIR" | grep -c "rmapbt=1")" -ne 1 ]; then
657 _notrun "rmapbt not supported by test filesystem type: $FSTYP"
661 _require_xfs_scratch_rmapbt()
665 _scratch_mkfs > /dev/null
667 if [ "$($XFS_INFO_PROG "$SCRATCH_MNT" | grep -c "rmapbt=1")" -ne 1 ]; then
669 _notrun "rmapbt not supported by scratch filesystem type: $FSTYP"
691 $XFS_IO_PROG -c "bmap -${param}lpv" "$file" | grep -c "$@"
694 # Reset all xfs error handling attributes, set them to original
697 # Only one argument, and it's mandatory:
698 # - dev: device name, e.g. $SCRATCH_DEV
700 # Note: this function only works for XFS
701 _reset_xfs_sysfs_error_handling()
705 if [ ! -b "$dev" -o "$FSTYP" != "xfs" ]; then
706 _fail "Usage: reset_xfs_sysfs_error_handling <device>"
709 _set_fs_sysfs_attr $dev error/fail_at_unmount 1
710 echo -n "error/fail_at_unmount="
711 _get_fs_sysfs_attr $dev error/fail_at_unmount
713 # Make sure all will be configured to retry forever by default, except
714 # for ENODEV, which is an unrecoverable error, so it will be configured
715 # to not retry on error by default.
716 for e in default EIO ENOSPC; do
717 _set_fs_sysfs_attr $dev \
718 error/metadata/${e}/max_retries -1
719 echo -n "error/metadata/${e}/max_retries="
720 _get_fs_sysfs_attr $dev error/metadata/${e}/max_retries
722 _set_fs_sysfs_attr $dev \
723 error/metadata/${e}/retry_timeout_seconds 0
724 echo -n "error/metadata/${e}/retry_timeout_seconds="
725 _get_fs_sysfs_attr $dev \
726 error/metadata/${e}/retry_timeout_seconds
730 # Skip if we are running an older binary without the stricter input checks.
731 # Make multiple checks to be sure that there is no regression on the one
732 # selected feature check, which would skew the result.
734 # At first, make a common function that runs the tests and returns
735 # number of failed cases.
736 _xfs_mkfs_validation_check()
738 local tmpfile=`mktemp`
739 local cmd="$MKFS_XFS_PROG -f -N -d file,name=$tmpfile,size=1g"
741 $cmd -s size=8s >/dev/null 2>&1
744 $cmd -l version=2,su=260k >/dev/null 2>&1
751 # Skip the test if all calls passed - mkfs accepts invalid input
752 _require_xfs_mkfs_validation()
754 _xfs_mkfs_validation_check
755 if [ "$?" -eq 0 ]; then
756 _notrun "Requires newer mkfs with stricter input checks: the oldest supported version of xfsprogs is 4.7."
760 # The opposite of _require_xfs_mkfs_validation.
761 _require_xfs_mkfs_without_validation()
763 _xfs_mkfs_validation_check
764 if [ "$?" -ne 0 ]; then
765 _notrun "Requires older mkfs without strict input checks: the last supported version of xfsprogs is 4.5."
769 # XFS ability to change UUIDs on V5/CRC filesystems
773 # This will create a crc fs on $SCRATCH_DEV
776 _scratch_xfs_db -x -c "uuid restore" 2>&1 \
777 | grep -q "invalid UUID\|supported on V5 fs" \
778 && _notrun "Userspace doesn't support meta_uuid feature"
780 _scratch_xfs_db -x -c "uuid generate" >/dev/null 2>&1
782 _try_scratch_mount >/dev/null 2>&1 \
783 || _notrun "Kernel doesn't support meta_uuid feature"
787 # this test requires mkfs.xfs have case-insensitive naming support
788 _require_xfs_mkfs_ciname()
790 _scratch_mkfs_xfs_supported -n version=ci >/dev/null 2>&1 \
791 || _notrun "need case-insensitive naming support in mkfs.xfs"
794 # this test requires mkfs.xfs have configuration file support
795 _require_xfs_mkfs_cfgfile()
798 _scratch_mkfs_xfs_supported -c options=/tmp/a >/dev/null 2>&1
801 test $res -eq 0 || _notrun "need configuration file support in mkfs.xfs"
804 # XFS_DEBUG requirements
807 if grep -q "debug 0" /proc/fs/xfs/stat; then
808 _notrun "Require XFS built with CONFIG_XFS_DEBUG"
811 _require_no_xfs_debug()
813 if grep -q "debug 1" /proc/fs/xfs/stat; then
814 _notrun "Require XFS built without CONFIG_XFS_DEBUG"
818 # Require that assertions will not crash the system.
820 # Assertions would always crash the system if XFS assert fatal was enabled
821 # (CONFIG_XFS_ASSERT_FATAL=y). If a test is designed to trigger an assertion,
822 # skip the test on a CONFIG_XFS_ASSERT_FATAL built XFS by default. Note:
823 # CONFIG_XFS_ASSERT_FATAL can be disabled by setting bug_on_assert to zero if
824 # we want test to run.
825 _require_no_xfs_bug_on_assert()
827 if [ -f /sys/fs/xfs/debug/bug_on_assert ]; then
828 grep -q "1" /sys/fs/xfs/debug/bug_on_assert && \
829 _notrun "test requires XFS bug_on_assert to be off, turn it off to run the test"
831 # Note: Prior to the creation of CONFIG_XFS_ASSERT_FATAL (and
832 # the sysfs knob bug_on_assert), assertions would always crash
833 # the system if XFS debug was enabled (CONFIG_XFS_DEBUG=y). If
834 # a test is designed to trigger an assertion and the test
835 # designer does not want to hang fstests, skip the test.
836 _require_no_xfs_debug
840 # Get a metadata field
841 # The first arg is the field name
842 # The rest of the arguments are xfs_db commands to find the metadata.
843 _scratch_xfs_get_metadata_field()
848 local grep_key="$(echo "${key}" | tr '[]()' '....')"
852 cmds+=("-c" "${arg}")
854 _scratch_xfs_db "${cmds[@]}" -c "print ${key}" | grep "^${grep_key}" | \
858 # Set a metadata field
859 # The first arg is the field name
860 # The second arg is the new value
861 # The rest of the arguments are xfs_db commands to find the metadata.
862 _scratch_xfs_set_metadata_field()
871 cmds+=("-c" "${arg}")
875 _scratch_xfs_db -x -c "help write" | egrep -q "(-c|-d)" && value="-- ${value}"
876 _scratch_xfs_db -x -c "help write" | egrep -q "(-d)" && wr_cmd="${wr_cmd} -d"
877 _scratch_xfs_db -x "${cmds[@]}" -c "${wr_cmd} ${key} ${value}"
880 _scratch_xfs_get_sb_field()
882 _scratch_xfs_get_metadata_field "$1" "sb 0"
885 _scratch_xfs_set_sb_field()
887 _scratch_xfs_set_metadata_field "$1" "$2" "sb 0"
890 # Before xfsprogs commit 4222d000ed("db: write via array indexing doesn't
891 # work"), xfs_db command to write a specific AGFL index doesn't work. It's a
892 # bug in a diagnostic tool that is only used by XFS developers as a test
893 # infrastructure, so it's fine to treat it as a infrastructure dependency as
894 # all other _require rules.
895 _require_xfs_db_write_array()
900 touch $TEST_DIR/$seq.img
901 $MKFS_XFS_PROG -d file,name=$TEST_DIR/$seq.img,size=512m >/dev/null 2>&1
902 $XFS_DB_PROG -x -c "agfl 0" -c "write bno[32] 78" $TEST_DIR/$seq.img \
904 $XFS_DB_PROG -x -c "agfl 0" -c "print bno[32]" $TEST_DIR/$seq.img \
905 | grep -q "bno\[32\] = 78" && supported=1
906 rm -f $TEST_DIR/$seq.img
907 [ $supported -eq 0 ] && _notrun "xfs_db write can't support array"
910 _require_xfs_spaceman_command()
913 echo "Usage: _require_xfs_spaceman_command command [switch]" 1>&2
919 local param_checked=0
922 _require_command "$XFS_SPACEMAN_PROG" "xfs_spaceman"
924 testfile=$TEST_DIR/$$.xfs_spaceman
928 testio=`$XFS_SPACEMAN_PROG -c "health $param" $TEST_DIR 2>&1`
932 testio=`$XFS_SPACEMAN_PROG -c "help $command" $TEST_DIR 2>&1`
935 rm -f $testfile 2>&1 > /dev/null
936 echo $testio | grep -q "not found" && \
937 _notrun "xfs_spaceman $command support is missing"
938 echo $testio | grep -q "Operation not supported" && \
939 _notrun "xfs_spaceman $command failed (old kernel/wrong fs?)"
940 echo $testio | grep -q "Invalid" && \
941 _notrun "xfs_spaceman $command failed (old kernel/wrong fs/bad args?)"
942 echo $testio | grep -q "foreign file active" && \
943 _notrun "xfs_spaceman $command not supported on $FSTYP"
944 echo $testio | grep -q "Inappropriate ioctl for device" && \
945 _notrun "xfs_spaceman $command support is missing (missing ioctl?)"
946 echo $testio | grep -q "Function not implemented" && \
947 _notrun "xfs_spaceman $command support is missing (missing syscall?)"
949 [ -n "$param" ] || return
951 if [ $param_checked -eq 0 ]; then
952 $XFS_SPACEMAN_PROG -c "help $command" | grep -q "^ $param --" || \
953 _notrun "xfs_spaceman $command doesn't support $param"
957 _scratch_get_sfdir_prefix() {
960 for prefix in "u.sfdir3" "u.sfdir2" "u3.sfdir3"; do
961 if [ -n "$(_scratch_xfs_get_metadata_field \
962 "${prefix}.hdr.parent.i4" \
963 "inode ${dir_ino}")" ]; then
968 _scratch_xfs_db -c "inode ${dir_ino}" -c 'p' >> $seqres.full
972 _scratch_get_bmx_prefix() {
975 for prefix in "u3.bmx" "u.bmx"; do
976 if [ -n "$(_scratch_xfs_get_metadata_field \
977 "${prefix}[0].startblock" \
978 "inode ${ino}")" ]; then
983 _scratch_xfs_db -c "inode ${ino}" -c 'p' >> $seqres.full
987 _scratch_get_iext_count()
1004 _scratch_xfs_get_metadata_field $field "inode $ino"
1008 # Ensures that we don't pass any mount options incompatible with XFS v4
1010 _force_xfsv4_mount_options()
1015 # Can't have group and project quotas in XFS v4
1016 echo "$MOUNT_OPTIONS" | egrep -q "(gquota|grpquota|grpjquota=|gqnoenforce)" && gquota=1
1017 echo "$MOUNT_OPTIONS" | egrep -q "(\bpquota|prjquota|pqnoenforce)" && pquota=1
1019 if [ $gquota -gt 0 ] && [ $pquota -gt 0 ]; then
1020 export MOUNT_OPTIONS=$(echo $MOUNT_OPTIONS \
1021 | sed -e 's/gquota/QUOTA/g' \
1022 -e 's/grpquota/QUOTA/g' \
1023 -e 's/grpjquota=[^, ]/QUOTA/g' \
1024 -e 's/gqnoenforce/QUOTA/g' \
1025 -e "s/QUOTA/defaults/g")
1027 echo "MOUNT_OPTIONS = $MOUNT_OPTIONS" >>$seqres.full
1030 # Find AG count of mounted filesystem
1031 _xfs_mount_agcount()
1033 $XFS_INFO_PROG "$1" | grep agcount= | sed -e 's/^.*agcount=\([0-9]*\),.*$/\1/g'
1036 # Wipe the superblock of each XFS AGs
1037 _try_wipe_scratch_xfs()
1039 local num='^[0-9]+$'
1044 # Try to wipe each SB if there's an existed XFS
1045 agcount=`_scratch_xfs_get_sb_field agcount 2>/dev/null`
1046 agsize=`_scratch_xfs_get_sb_field agblocks 2>/dev/null`
1047 dbsize=`_scratch_xfs_get_sb_field blocksize 2>/dev/null`
1048 if [[ $agcount =~ $num && $agsize =~ $num && $dbsize =~ $num ]];then
1049 for ((i = 0; i < agcount; i++)); do
1050 $XFS_IO_PROG -c "pwrite $((i * dbsize * agsize)) $dbsize" \
1051 $SCRATCH_DEV >/dev/null;
1055 # Try to wipe each SB by default mkfs.xfs geometry
1056 local tmp=`mktemp -u`
1057 unset agcount agsize dbsize
1058 _scratch_mkfs_xfs -N 2>/dev/null | perl -ne '
1059 if (/^meta-data=.*\s+agcount=(\d+), agsize=(\d+) blks/) {
1060 print STDOUT "agcount=$1\nagsize=$2\n";
1062 if (/^data\s+=\s+bsize=(\d+)\s/) {
1063 print STDOUT "dbsize=$1\n";
1067 if [[ $agcount =~ $num && $agsize =~ $num && $dbsize =~ $num ]];then
1068 for ((i = 0; i < agcount; i++)); do
1069 $XFS_IO_PROG -c "pwrite $((i * dbsize * agsize)) $dbsize" \
1070 $SCRATCH_DEV >/dev/null;
1078 [ -n "$XFS_COPY_PROG" ] || _notrun "xfs_copy binary not yet installed"
1079 [ "$USE_EXTERNAL" = yes ] && \
1080 _notrun "Cannot xfs_copy with external devices"