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 _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"
122 while getopts "b:fi:l:stvV" c; do
127 i) OPTS=$OPTS"-i "$OPTARG" ";;
128 b) OPTS=$OPTS"-b "$OPTARG" ";;
129 f) DBOPTS=$DBOPTS" -f";;
130 l) DBOPTS=$DBOPTS" -l "$OPTARG" ";;
131 V) $XFS_DB_PROG -p xfs_check -V
139 1) ${XFS_DB_PROG}${DBOPTS} -F -i -p xfs_check -c "check$OPTS" $1
149 _scratch_xfs_db_options()
152 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
153 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
154 echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
159 $XFS_DB_PROG "$@" $(_scratch_xfs_db_options)
162 _scratch_xfs_logprint()
165 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
166 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
167 $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
173 [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
174 TEST_OPTIONS="-l$TEST_LOGDEV"
175 $XFS_LOGPRINT_PROG $TEST_OPTIONS $* $TEST_DEV
181 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
182 SCRATCH_OPTIONS="-l $SCRATCH_LOGDEV"
183 [ "$LARGE_SCRATCH_DEV" = yes ] && \
184 SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
185 _xfs_check $SCRATCH_OPTIONS $* $SCRATCH_DEV
188 _scratch_xfs_repair()
191 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
192 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
193 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
194 SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
195 [ "$LARGE_SCRATCH_DEV" = yes ] && SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
196 $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
199 # this test requires the projid32bit feature to be available in mkfs.xfs.
201 _require_projid32bit()
203 _scratch_mkfs_xfs_supported -i projid32bit=1 >/dev/null 2>&1 \
204 || _notrun "mkfs.xfs doesn't have projid32bit feature"
207 _require_projid16bit()
209 _scratch_mkfs_xfs_supported -i projid32bit=0 >/dev/null 2>&1 \
210 || _notrun "16 bit project IDs not supported on $SCRATCH_DEV"
213 # this test requires the crc feature to be available in mkfs.xfs
215 _require_xfs_mkfs_crc()
217 _scratch_mkfs_xfs_supported -m crc=1 >/dev/null 2>&1 \
218 || _notrun "mkfs.xfs doesn't have crc feature"
221 # this test requires the xfs kernel support crc feature
225 _scratch_mkfs_xfs -m crc=1 >/dev/null 2>&1
226 _scratch_mount >/dev/null 2>&1 \
227 || _notrun "Kernel doesn't support crc feature"
231 # this test requires the xfs kernel support crc feature on scratch device
233 _require_scratch_xfs_crc()
235 _scratch_mkfs_xfs >/dev/null 2>&1
236 _scratch_mount >/dev/null 2>&1 \
237 || _notrun "Kernel doesn't support crc feature"
238 xfs_info $SCRATCH_MNT | grep -q 'crc=1' || _notrun "crc feature not supported by this filesystem"
242 # this test requires the finobt feature to be available in mkfs.xfs
244 _require_xfs_mkfs_finobt()
246 _scratch_mkfs_xfs_supported -m crc=1,finobt=1 >/dev/null 2>&1 \
247 || _notrun "mkfs.xfs doesn't have finobt feature"
250 # this test requires the xfs kernel support finobt feature
252 _require_xfs_finobt()
254 _scratch_mkfs_xfs -m crc=1,finobt=1 >/dev/null 2>&1
255 _scratch_mount >/dev/null 2>&1 \
256 || _notrun "Kernel doesn't support finobt feature"
260 # this test requires xfs sysfs attribute support
267 if [ ! -e $sysfsdir ]; then
268 _notrun "no kernel support for XFS sysfs attributes"
271 if [ ! -z $1 ] && [ ! -e $sysfsdir/$attr ]; then
272 _notrun "sysfs attribute '$attr' is not supported"
276 # this test requires the xfs sparse inode feature
278 _require_xfs_sparse_inodes()
280 _scratch_mkfs_xfs_supported -m crc=1 -i sparse > /dev/null 2>&1 \
281 || _notrun "mkfs.xfs does not support sparse inodes"
282 _scratch_mkfs_xfs -m crc=1 -i sparse > /dev/null 2>&1
283 _scratch_mount >/dev/null 2>&1 \
284 || _notrun "kernel does not support sparse inodes"
288 # check that xfs_db supports a specific command
289 _require_xfs_db_command()
291 if [ $# -ne 1 ]; then
292 echo "Usage: _require_xfs_db_command command" 1>&2
297 _scratch_xfs_db -x -c "help" | grep $command > /dev/null || \
298 _notrun "xfs_db $command support is missing"
301 # Does the filesystem mounted from a particular device support scrub?
302 _supports_xfs_scrub()
304 local mountpoint="$1"
307 if [ ! -b "$device" ] || [ ! -e "$mountpoint" ]; then
308 echo "Usage: _supports_xfs_scrub mountpoint device"
312 test "$FSTYP" = "xfs" || return 1
313 test -x "$XFS_SCRUB_PROG" || return 1
315 # Probe for kernel support...
316 $XFS_IO_PROG -c 'help scrub' 2>&1 | grep -q 'types are:.*probe' || return 1
317 $XFS_IO_PROG -c "scrub probe 0" "$mountpoint" 2>&1 | grep -q "Inappropriate ioctl" && return 1
319 # Scrub can't run on norecovery mounts
320 _fs_options "$device" | grep -q "norecovery" && return 1
325 # run xfs_check and friends on a FS.
326 _check_xfs_filesystem()
328 if [ $# -ne 3 ]; then
329 echo "Usage: _check_xfs_filesystem device <logdev>|none <rtdev>|none" 1>&2
333 extra_mount_options=""
337 if [ -f $device ]; then
341 if [ "$2" != "none" ]; then
342 extra_log_options="-l$2"
343 extra_mount_options="-ologdev=$2"
346 if [ "$3" != "none" ]; then
347 extra_rt_options="-r$3"
348 extra_mount_options=$extra_mount_options" -ortdev=$3"
350 extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"
352 [ "$FSTYP" != xfs ] && return 0
354 type=`_fs_type $device`
357 # Run online scrub if we can.
358 mntpt="$(_is_mounted $device)"
359 if [ -n "$mntpt" ] && _supports_xfs_scrub "$mntpt" "$device"; then
360 "$XFS_SCRUB_PROG" $scrubflag -v -d -n $device > $tmp.scrub 2>&1
361 if [ $? -ne 0 ]; then
362 _log_err "_check_xfs_filesystem: filesystem on $device failed scrub"
363 echo "*** xfs_scrub $scrubflag -v -d -n output ***" >> $seqres.full
364 cat $tmp.scrub >> $seqres.full
365 echo "*** end xfs_scrub output" >> $serqres.full
371 if [ "$type" = "xfs" ]; then
373 mountpoint=`_umount_or_remount_ro $device`
376 $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
377 | tee $tmp.logprint | grep -q "<CLEAN>"
378 if [ $? -ne 0 -a "$HOSTOS" = "Linux" ]; then
379 _log_err "_check_xfs_filesystem: filesystem on $device has dirty log"
380 echo "*** xfs_logprint -t output ***" >>$seqres.full
381 cat $tmp.logprint >>$seqres.full
382 echo "*** end xfs_logprint output" >>$seqres.full
387 # xfs_check runs out of memory on large files, so even providing the test
388 # option (-t) to avoid indexing the free space trees doesn't make it pass on
389 # large filesystems. Avoid it.
390 if [ "$LARGE_SCRATCH_DEV" != yes ]; then
391 _xfs_check $extra_log_options $device 2>&1 |\
392 _fix_malloc >$tmp.fs_check
394 if [ -s $tmp.fs_check ]; then
395 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (c)"
396 echo "*** xfs_check output ***" >>$seqres.full
397 cat $tmp.fs_check >>$seqres.full
398 echo "*** end xfs_check output" >>$seqres.full
403 $XFS_REPAIR_PROG -n $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
404 if [ $? -ne 0 ]; then
405 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (r)"
406 echo "*** xfs_repair -n output ***" >>$seqres.full
407 cat $tmp.repair | _fix_malloc >>$seqres.full
408 echo "*** end xfs_repair output" >>$seqres.full
412 rm -f $tmp.fs_check $tmp.logprint $tmp.repair
414 # Optionally test the index rebuilding behavior.
415 if [ -n "$TEST_XFS_REPAIR_REBUILD" ]; then
416 $XFS_REPAIR_PROG $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
417 if [ $? -ne 0 ]; then
418 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild)"
419 echo "*** xfs_repair output ***" >>$seqres.full
420 cat $tmp.repair | _fix_malloc >>$seqres.full
421 echo "*** end xfs_repair output" >>$seqres.full
427 $XFS_REPAIR_PROG -n $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
428 if [ $? -ne 0 ]; then
429 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild-reverify)"
430 echo "*** xfs_repair -n output ***" >>$seqres.full
431 cat $tmp.repair | _fix_malloc >>$seqres.full
432 echo "*** end xfs_repair output" >>$seqres.full
439 if [ $ok -eq 0 ]; then
440 echo "*** mount output ***" >>$seqres.full
441 _mount >>$seqres.full
442 echo "*** end mount output" >>$seqres.full
443 elif [ "$type" = "xfs" ]; then
444 _mount_or_remount_rw "$extra_mount_options" $device $mountpoint
447 if [ $ok -eq 0 ]; then
449 if [ "$iam" != "check" ]; then
462 [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
463 TEST_LOG="$TEST_LOGDEV"
465 [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
466 TEST_RT="$TEST_RTDEV"
468 _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT
470 # check for ipath consistency
471 if $XFS_GROWFS_PROG -n $TEST_DIR | grep -q 'inode-paths=1'; then
472 # errors go to stderr
473 xfs_check_ipaths $TEST_DIR >/dev/null
474 xfs_repair_ipaths -n $TEST_DIR >/dev/null
478 _require_xfs_test_rmapbt()
482 if [ "$(xfs_info "$TEST_DIR" | grep -c "rmapbt=1")" -ne 1 ]; then
483 _notrun "rmapbt not supported by test filesystem type: $FSTYP"
487 _require_xfs_scratch_rmapbt()
491 _scratch_mkfs > /dev/null
493 if [ "$(xfs_info "$SCRATCH_MNT" | grep -c "rmapbt=1")" -ne 1 ]; then
495 _notrun "rmapbt not supported by scratch filesystem type: $FSTYP"
517 $XFS_IO_PROG -c "bmap -${param}lpv" "$file" | grep -c "$@"
520 # Reset all xfs error handling attributes, set them to original
523 # Only one argument, and it's mandatory:
524 # - dev: device name, e.g. $SCRATCH_DEV
526 # Note: this function only works for XFS
527 _reset_xfs_sysfs_error_handling()
531 if [ ! -b "$dev" -o "$FSTYP" != "xfs" ]; then
532 _fail "Usage: reset_xfs_sysfs_error_handling <device>"
535 _set_fs_sysfs_attr $dev error/fail_at_unmount 1
536 echo -n "error/fail_at_unmount="
537 _get_fs_sysfs_attr $dev error/fail_at_unmount
539 # Make sure all will be configured to retry forever by default, except
540 # for ENODEV, which is an unrecoverable error, so it will be configured
541 # to not retry on error by default.
542 for e in default EIO ENOSPC; do
543 _set_fs_sysfs_attr $dev \
544 error/metadata/${e}/max_retries -1
545 echo -n "error/metadata/${e}/max_retries="
546 _get_fs_sysfs_attr $dev error/metadata/${e}/max_retries
548 _set_fs_sysfs_attr $dev \
549 error/metadata/${e}/retry_timeout_seconds 0
550 echo -n "error/metadata/${e}/retry_timeout_seconds="
551 _get_fs_sysfs_attr $dev \
552 error/metadata/${e}/retry_timeout_seconds
556 # Skip if we are running an older binary without the stricter input checks.
557 # Make multiple checks to be sure that there is no regression on the one
558 # selected feature check, which would skew the result.
560 # At first, make a common function that runs the tests and returns
561 # number of failed cases.
562 _xfs_mkfs_validation_check()
564 local tmpfile=`mktemp`
565 local cmd="$MKFS_XFS_PROG -f -N -d file,name=$tmpfile,size=1g"
567 $cmd -s size=8s >/dev/null 2>&1
570 $cmd -l version=2,su=260k >/dev/null 2>&1
577 # Skip the test if all calls passed - mkfs accepts invalid input
578 _require_xfs_mkfs_validation()
580 _xfs_mkfs_validation_check
581 if [ "$?" -eq 0 ]; then
582 _notrun "Requires newer mkfs with stricter input checks: the oldest supported version of xfsprogs is 4.7."
586 # The opposite of _require_xfs_mkfs_validation.
587 _require_xfs_mkfs_without_validation()
589 _xfs_mkfs_validation_check
590 if [ "$?" -ne 0 ]; then
591 _notrun "Requires older mkfs without strict input checks: the last supported version of xfsprogs is 4.5."
595 # XFS ability to change UUIDs on V5/CRC filesystems
599 # This will create a crc fs on $SCRATCH_DEV
602 _scratch_xfs_db -x -c "uuid restore" 2>&1 \
603 | grep -q "invalid UUID\|supported on V5 fs" \
604 && _notrun "Userspace doesn't support meta_uuid feature"
606 _scratch_xfs_db -x -c "uuid generate" >/dev/null 2>&1
608 _scratch_mount >/dev/null 2>&1 \
609 || _notrun "Kernel doesn't support meta_uuid feature"
613 # this test requires mkfs.xfs have case-insensitive naming support
614 _require_xfs_mkfs_ciname()
616 _scratch_mkfs_xfs_supported -n version=ci >/dev/null 2>&1 \
617 || _notrun "need case-insensitive naming support in mkfs.xfs"
620 # XFS_DEBUG requirements
623 if grep -q "debug 0" /proc/fs/xfs/stat; then
624 _notrun "Require XFS built with CONFIG_XFS_DEBUG"
627 _require_no_xfs_debug()
629 if grep -q "debug 1" /proc/fs/xfs/stat; then
630 _notrun "Require XFS built without CONFIG_XFS_DEBUG"