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'"
88 _scratch_do_mkfs "$mkfs_cmd" "$mkfs_filter" $* 2>$tmp.mkfserr 1>$tmp.mkfsstd
92 if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
93 # manually parse the mkfs output to get the fs size in bytes
95 fs_size=`cat $tmp.mkfsstd | perl -ne '
96 if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+)/) {
98 print STDOUT "$size\n";
100 _setup_large_xfs_fs $fs_size
104 # output mkfs stdout and stderr
107 rm -f $tmp.mkfserr $tmp.mkfsstd
112 # xfs_check script is planned to be deprecated. But, we want to
113 # be able to invoke "xfs_check" behavior in xfstests in order to
114 # maintain the current verification levels.
119 USAGE="Usage: xfs_check [-fsvV] [-l logdev] [-i ino]... [-b bno]... special"
121 while getopts "b:fi:l:stvV" c; do
126 i) OPTS=$OPTS"-i "$OPTARG" ";;
127 b) OPTS=$OPTS"-b "$OPTARG" ";;
128 f) DBOPTS=$DBOPTS" -f";;
129 l) DBOPTS=$DBOPTS" -l "$OPTARG" ";;
130 V) $XFS_DB_PROG -p xfs_check -V
138 1) ${XFS_DB_PROG}${DBOPTS} -F -i -p xfs_check -c "check$OPTS" $1
148 _scratch_xfs_db_options()
151 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
152 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
153 echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
158 $XFS_DB_PROG "$@" $(_scratch_xfs_db_options)
161 _scratch_xfs_logprint()
164 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
165 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
166 $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
172 [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
173 TEST_OPTIONS="-l$TEST_LOGDEV"
174 $XFS_LOGPRINT_PROG $TEST_OPTIONS $* $TEST_DEV
180 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
181 SCRATCH_OPTIONS="-l $SCRATCH_LOGDEV"
182 [ "$LARGE_SCRATCH_DEV" = yes ] && \
183 SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
184 _xfs_check $SCRATCH_OPTIONS $* $SCRATCH_DEV
187 _scratch_xfs_repair()
190 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
191 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
192 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
193 SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
194 [ "$LARGE_SCRATCH_DEV" = yes ] && SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
195 $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
198 # this test requires the projid32bit feature to be available in mkfs.xfs.
200 _require_projid32bit()
202 _scratch_mkfs_xfs_supported -i projid32bit=1 >/dev/null 2>&1 \
203 || _notrun "mkfs.xfs doesn't have projid32bit feature"
206 _require_projid16bit()
208 _scratch_mkfs_xfs_supported -i projid32bit=0 >/dev/null 2>&1 \
209 || _notrun "16 bit project IDs not supported on $SCRATCH_DEV"
212 # this test requires the crc feature to be available in mkfs.xfs
214 _require_xfs_mkfs_crc()
216 _scratch_mkfs_xfs_supported -m crc=1 >/dev/null 2>&1 \
217 || _notrun "mkfs.xfs doesn't have crc feature"
220 # this test requires the xfs kernel support crc feature
224 _scratch_mkfs_xfs -m crc=1 >/dev/null 2>&1
225 _scratch_mount >/dev/null 2>&1 \
226 || _notrun "Kernel doesn't support crc feature"
230 # this test requires the xfs kernel support crc feature on scratch device
232 _require_scratch_xfs_crc()
234 _scratch_mkfs_xfs >/dev/null 2>&1
235 _scratch_mount >/dev/null 2>&1 \
236 || _notrun "Kernel doesn't support crc feature"
237 xfs_info $SCRATCH_MNT | grep -q 'crc=1' || _notrun "crc feature not supported by this filesystem"
241 # this test requires the finobt feature to be available in mkfs.xfs
243 _require_xfs_mkfs_finobt()
245 _scratch_mkfs_xfs_supported -m crc=1,finobt=1 >/dev/null 2>&1 \
246 || _notrun "mkfs.xfs doesn't have finobt feature"
249 # this test requires the xfs kernel support finobt feature
251 _require_xfs_finobt()
253 _scratch_mkfs_xfs -m crc=1,finobt=1 >/dev/null 2>&1
254 _scratch_mount >/dev/null 2>&1 \
255 || _notrun "Kernel doesn't support finobt feature"
259 # this test requires xfs sysfs attribute support
266 if [ ! -e $sysfsdir ]; then
267 _notrun "no kernel support for XFS sysfs attributes"
270 if [ ! -z $1 ] && [ ! -e $sysfsdir/$attr ]; then
271 _notrun "sysfs attribute '$attr' is not supported"
275 # this test requires the xfs sparse inode feature
277 _require_xfs_sparse_inodes()
279 _scratch_mkfs_xfs_supported -m crc=1 -i sparse > /dev/null 2>&1 \
280 || _notrun "mkfs.xfs does not support sparse inodes"
281 _scratch_mkfs_xfs -m crc=1 -i sparse > /dev/null 2>&1
282 _scratch_mount >/dev/null 2>&1 \
283 || _notrun "kernel does not support sparse inodes"
287 # check that xfs_db supports a specific command
288 _require_xfs_db_command()
290 if [ $# -ne 1 ]; then
291 echo "Usage: _require_xfs_db_command command" 1>&2
296 _scratch_xfs_db -x -c "help" | grep $command > /dev/null || \
297 _notrun "xfs_db $command support is missing"
300 # run xfs_check and friends on a FS.
301 _check_xfs_filesystem()
303 if [ $# -ne 3 ]; then
304 echo "Usage: _check_xfs_filesystem device <logdev>|none <rtdev>|none" 1>&2
308 extra_mount_options=""
312 if [ -f $device ]; then
316 if [ "$2" != "none" ]; then
317 extra_log_options="-l$2"
318 extra_mount_options="-ologdev=$2"
321 if [ "$3" != "none" ]; then
322 extra_rt_options="-r$3"
323 extra_mount_options=$extra_mount_options" -ortdev=$3"
325 extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"
327 [ "$FSTYP" != xfs ] && return 0
329 type=`_fs_type $device`
332 if [ "$type" = "xfs" ]; then
333 if [ -n "$TEST_XFS_SCRUB" ] && [ -x "$XFS_SCRUB_PROG" ]; then
334 "$XFS_SCRUB_PROG" $scrubflag -v -d -n $device >>$seqres.full
335 if [ $? -ne 0 ]; then
336 _log_err "filesystem on $device failed scrub"
341 mountpoint=`_umount_or_remount_ro $device`
344 $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
345 | tee $tmp.logprint | grep -q "<CLEAN>"
346 if [ $? -ne 0 -a "$HOSTOS" = "Linux" ]; then
347 _log_err "_check_xfs_filesystem: filesystem on $device has dirty log"
348 echo "*** xfs_logprint -t output ***" >>$seqres.full
349 cat $tmp.logprint >>$seqres.full
350 echo "*** end xfs_logprint output" >>$seqres.full
355 # xfs_check runs out of memory on large files, so even providing the test
356 # option (-t) to avoid indexing the free space trees doesn't make it pass on
357 # large filesystems. Avoid it.
358 if [ "$LARGE_SCRATCH_DEV" != yes ]; then
359 _xfs_check $extra_log_options $device 2>&1 |\
360 _fix_malloc >$tmp.fs_check
362 if [ -s $tmp.fs_check ]; then
363 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (c)"
364 echo "*** xfs_check output ***" >>$seqres.full
365 cat $tmp.fs_check >>$seqres.full
366 echo "*** end xfs_check output" >>$seqres.full
371 $XFS_REPAIR_PROG -n $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
372 if [ $? -ne 0 ]; then
373 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (r)"
374 echo "*** xfs_repair -n output ***" >>$seqres.full
375 cat $tmp.repair | _fix_malloc >>$seqres.full
376 echo "*** end xfs_repair output" >>$seqres.full
380 rm -f $tmp.fs_check $tmp.logprint $tmp.repair
382 # Optionally test the index rebuilding behavior.
383 if [ -n "$TEST_XFS_REPAIR_REBUILD" ]; then
384 $XFS_REPAIR_PROG $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
385 if [ $? -ne 0 ]; then
386 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild)"
387 echo "*** xfs_repair output ***" >>$seqres.full
388 cat $tmp.repair | _fix_malloc >>$seqres.full
389 echo "*** end xfs_repair output" >>$seqres.full
395 $XFS_REPAIR_PROG -n $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
396 if [ $? -ne 0 ]; then
397 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild-reverify)"
398 echo "*** xfs_repair -n output ***" >>$seqres.full
399 cat $tmp.repair | _fix_malloc >>$seqres.full
400 echo "*** end xfs_repair output" >>$seqres.full
407 if [ $ok -eq 0 ]; then
408 echo "*** mount output ***" >>$seqres.full
409 _mount >>$seqres.full
410 echo "*** end mount output" >>$seqres.full
411 elif [ "$type" = "xfs" ]; then
412 _mount_or_remount_rw "$extra_mount_options" $device $mountpoint
415 if [ $ok -eq 0 ]; then
417 if [ "$iam" != "check" ]; then
430 [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
431 TEST_LOG="$TEST_LOGDEV"
433 [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
434 TEST_RT="$TEST_RTDEV"
436 _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT
438 # check for ipath consistency
439 if $XFS_GROWFS_PROG -n $TEST_DIR | grep -q 'inode-paths=1'; then
440 # errors go to stderr
441 xfs_check_ipaths $TEST_DIR >/dev/null
442 xfs_repair_ipaths -n $TEST_DIR >/dev/null
446 _require_xfs_test_rmapbt()
450 if [ "$(xfs_info "$TEST_DIR" | grep -c "rmapbt=1")" -ne 1 ]; then
451 _notrun "rmapbt not supported by test filesystem type: $FSTYP"
455 _require_xfs_scratch_rmapbt()
459 _scratch_mkfs > /dev/null
461 if [ "$(xfs_info "$SCRATCH_MNT" | grep -c "rmapbt=1")" -ne 1 ]; then
463 _notrun "rmapbt not supported by scratch filesystem type: $FSTYP"
485 $XFS_IO_PROG -c "bmap -${param}lpv" "$file" | grep -c "$@"
488 # Reset all xfs error handling attributes, set them to original
491 # Only one argument, and it's mandatory:
492 # - dev: device name, e.g. $SCRATCH_DEV
494 # Note: this function only works for XFS
495 _reset_xfs_sysfs_error_handling()
499 if [ ! -b "$dev" -o "$FSTYP" != "xfs" ]; then
500 _fail "Usage: reset_xfs_sysfs_error_handling <device>"
503 _set_fs_sysfs_attr $dev error/fail_at_unmount 1
504 echo -n "error/fail_at_unmount="
505 _get_fs_sysfs_attr $dev error/fail_at_unmount
507 # Make sure all will be configured to retry forever by default, except
508 # for ENODEV, which is an unrecoverable error, so it will be configured
509 # to not retry on error by default.
510 for e in default EIO ENOSPC; do
511 _set_fs_sysfs_attr $dev \
512 error/metadata/${e}/max_retries -1
513 echo -n "error/metadata/${e}/max_retries="
514 _get_fs_sysfs_attr $dev error/metadata/${e}/max_retries
516 _set_fs_sysfs_attr $dev \
517 error/metadata/${e}/retry_timeout_seconds 0
518 echo -n "error/metadata/${e}/retry_timeout_seconds="
519 _get_fs_sysfs_attr $dev \
520 error/metadata/${e}/retry_timeout_seconds
524 # Skip if we are running an older binary without the stricter input checks.
525 # Make multiple checks to be sure that there is no regression on the one
526 # selected feature check, which would skew the result.
528 # At first, make a common function that runs the tests and returns
529 # number of failed cases.
530 _xfs_mkfs_validation_check()
532 local tmpfile=`mktemp`
533 local cmd="$MKFS_XFS_PROG -f -N -d file,name=$tmpfile,size=1g"
535 $cmd -s size=8s >/dev/null 2>&1
538 $cmd -l version=2,su=260k >/dev/null 2>&1
545 # Skip the test if all calls passed - mkfs accepts invalid input
546 _require_xfs_mkfs_validation()
548 _xfs_mkfs_validation_check
549 if [ "$?" -eq 0 ]; then
550 _notrun "Requires newer mkfs with stricter input checks: the oldest supported version of xfsprogs is 4.7."
554 # The opposite of _require_xfs_mkfs_validation.
555 _require_xfs_mkfs_without_validation()
557 _xfs_mkfs_validation_check
558 if [ "$?" -ne 0 ]; then
559 _notrun "Requires older mkfs without strict input checks: the last supported version of xfsprogs is 4.5."
563 # XFS ability to change UUIDs on V5/CRC filesystems
567 # This will create a crc fs on $SCRATCH_DEV
570 _scratch_xfs_db -x -c "uuid restore" 2>&1 \
571 | grep -q "invalid UUID\|supported on V5 fs" \
572 && _notrun "Userspace doesn't support meta_uuid feature"
574 _scratch_xfs_db -x -c "uuid generate" >/dev/null 2>&1
576 _scratch_mount >/dev/null 2>&1 \
577 || _notrun "Kernel doesn't support meta_uuid feature"
581 # this test requires mkfs.xfs have case-insensitive naming support
582 _require_xfs_mkfs_ciname()
584 _scratch_mkfs_xfs_supported -n version=ci >/dev/null 2>&1 \
585 || _notrun "need case-insensitive naming support in mkfs.xfs"
588 # xfs generates WARNINGs on purpose when applications mix buffered/mmap IO with
589 # direct IO on the same file. This is a helper for _check_dmesg() to filter out
593 local warn1="WARNING:.*fs/xfs/xfs_file\.c:.*xfs_file_dio_aio_write.*"
594 local warn2="WARNING:.*fs/xfs/xfs_file\.c:.*xfs_file_dio_aio_read.*"
595 local warn3="WARNING:.*fs/xfs/xfs_file\.c:.*xfs_file_read_iter.*"
596 local warn4="WARNING:.*fs/xfs/xfs_file\.c:.*xfs_file_aio_read.*"
597 local warn5="WARNING:.*fs/iomap\.c:.*iomap_dio_rw.*"
598 local warn6="WARNING:.*fs/xfs/xfs_aops\.c:.*__xfs_get_blocks.*"
599 local warn7="WARNING:.*fs/iomap\.c:.*iomap_dio_actor.*"
600 sed -e "s#$warn1#Intentional warnings in xfs_file_dio_aio_write#" \
601 -e "s#$warn2#Intentional warnings in xfs_file_dio_aio_read#" \
602 -e "s#$warn3#Intentional warnings in xfs_file_read_iter#" \
603 -e "s#$warn4#Intentional warnings in xfs_file_aio_read#" \
604 -e "s#$warn5#Intentional warnings in iomap_dio_rw#" \
605 -e "s#$warn6#Intentional warnings in __xfs_get_blocks#" \
606 -e "s#$warn7#Intentional warnings in iomap_dio_actor#"