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 $MKFS_XFS_PROG $SCRATCH_OPTIONS $mkfs_opts $SCRATCH_DEV
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 # extra mkfs options can be added by tests
83 local extra_mkfs_options=$*
87 # save mkfs output in case conflict means we need to run again.
88 # only the output for the mkfs that applies should be shown
89 _scratch_mkfs_xfs_opts $MKFS_OPTIONS $extra_mkfs_options \
90 2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
94 # a mkfs failure may be caused by conflicts between
95 # $MKFS_OPTIONS and $extra_mkfs_options
96 if [ $mkfs_status -ne 0 -a ! -z "$extra_mkfs_options" ]; then
98 echo -n "** mkfs failed with extra mkfs options "
99 echo "added to \"$MKFS_OPTIONS\" by test $seq **"
100 echo -n "** attempting to mkfs using only test $seq "
101 echo "options: $extra_mkfs_options **"
104 # running mkfs again. overwrite previous mkfs output files
105 _scratch_mkfs_xfs_opts $extra_mkfs_options \
106 2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
110 if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
111 # manually parse the mkfs output to get the fs size in bytes
113 fs_size=`cat $tmp_dir.mkfsstd | perl -ne '
114 if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+)/) {
116 print STDOUT "$size\n";
118 _setup_large_xfs_fs $fs_size
122 # output stored mkfs output, filtering unnecessary warnings from stderr
124 cat $tmp_dir.mkfserr | sed \
125 -e '/less than device physical sector/d' \
126 -e '/switching to logical sector/d' \
128 rm -f $tmp_dir.mkfserr $tmp_dir.mkfsstd
133 # xfs_check script is planned to be deprecated. But, we want to
134 # be able to invoke "xfs_check" behavior in xfstests in order to
135 # maintain the current verification levels.
140 USAGE="Usage: xfs_check [-fsvV] [-l logdev] [-i ino]... [-b bno]... special"
142 while getopts "b:fi:l:stvV" c; do
147 i) OPTS=$OPTS"-i "$OPTARG" ";;
148 b) OPTS=$OPTS"-b "$OPTARG" ";;
149 f) DBOPTS=$DBOPTS" -f";;
150 l) DBOPTS=$DBOPTS" -l "$OPTARG" ";;
151 V) $XFS_DB_PROG -p xfs_check -V
159 1) ${XFS_DB_PROG}${DBOPTS} -F -i -p xfs_check -c "check$OPTS" $1
169 _scratch_xfs_db_options()
172 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
173 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
174 echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
179 $XFS_DB_PROG "$@" $(_scratch_xfs_db_options)
182 _scratch_xfs_logprint()
185 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
186 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
187 $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
193 [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
194 TEST_OPTIONS="-l$TEST_LOGDEV"
195 $XFS_LOGPRINT_PROG $TEST_OPTIONS $* $TEST_DEV
201 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
202 SCRATCH_OPTIONS="-l $SCRATCH_LOGDEV"
203 [ "$LARGE_SCRATCH_DEV" = yes ] && \
204 SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
205 _xfs_check $SCRATCH_OPTIONS $* $SCRATCH_DEV
208 _scratch_xfs_repair()
211 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
212 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
213 [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
214 SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
215 [ "$LARGE_SCRATCH_DEV" = yes ] && SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
216 $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
219 # this test requires the projid32bit feature to be available in mkfs.xfs.
221 _require_projid32bit()
223 _scratch_mkfs_xfs_supported -i projid32bit=1 >/dev/null 2>&1 \
224 || _notrun "mkfs.xfs doesn't have projid32bit feature"
227 _require_projid16bit()
229 _scratch_mkfs_xfs_supported -i projid32bit=0 >/dev/null 2>&1 \
230 || _notrun "16 bit project IDs not supported on $SCRATCH_DEV"
233 # this test requires the crc feature to be available in mkfs.xfs
235 _require_xfs_mkfs_crc()
237 _scratch_mkfs_xfs_supported -m crc=1 >/dev/null 2>&1 \
238 || _notrun "mkfs.xfs doesn't have crc feature"
241 # this test requires the xfs kernel support crc feature
245 _scratch_mkfs_xfs -m crc=1 >/dev/null 2>&1
246 _scratch_mount >/dev/null 2>&1 \
247 || _notrun "Kernel doesn't support crc feature"
251 # this test requires the xfs kernel support crc feature on scratch device
253 _require_scratch_xfs_crc()
255 _scratch_mkfs_xfs >/dev/null 2>&1
256 _scratch_mount >/dev/null 2>&1 \
257 || _notrun "Kernel doesn't support crc feature"
258 xfs_info $SCRATCH_MNT | grep -q 'crc=1' || _notrun "crc feature not supported by this filesystem"
262 # this test requires the finobt feature to be available in mkfs.xfs
264 _require_xfs_mkfs_finobt()
266 _scratch_mkfs_xfs_supported -m crc=1,finobt=1 >/dev/null 2>&1 \
267 || _notrun "mkfs.xfs doesn't have finobt feature"
270 # this test requires the xfs kernel support finobt feature
272 _require_xfs_finobt()
274 _scratch_mkfs_xfs -m crc=1,finobt=1 >/dev/null 2>&1
275 _scratch_mount >/dev/null 2>&1 \
276 || _notrun "Kernel doesn't support finobt feature"
280 # this test requires xfs sysfs attribute support
287 if [ ! -e $sysfsdir ]; then
288 _notrun "no kernel support for XFS sysfs attributes"
291 if [ ! -z $1 ] && [ ! -e $sysfsdir/$attr ]; then
292 _notrun "sysfs attribute '$attr' is not supported"
296 # this test requires the xfs sparse inode feature
298 _require_xfs_sparse_inodes()
300 _scratch_mkfs_xfs_supported -m crc=1 -i sparse > /dev/null 2>&1 \
301 || _notrun "mkfs.xfs does not support sparse inodes"
302 _scratch_mkfs_xfs -m crc=1 -i sparse > /dev/null 2>&1
303 _scratch_mount >/dev/null 2>&1 \
304 || _notrun "kernel does not support sparse inodes"
308 # check that xfs_db supports a specific command
309 _require_xfs_db_command()
311 if [ $# -ne 1 ]; then
312 echo "Usage: _require_xfs_db_command command" 1>&2
317 _scratch_xfs_db -x -c "help" | grep $command > /dev/null || \
318 _notrun "xfs_db $command support is missing"
321 # run xfs_check and friends on a FS.
322 _check_xfs_filesystem()
324 if [ $# -ne 3 ]; then
325 echo "Usage: _check_xfs_filesystem device <logdev>|none <rtdev>|none" 1>&2
329 extra_mount_options=""
333 if [ -f $device ]; then
337 if [ "$2" != "none" ]; then
338 extra_log_options="-l$2"
339 extra_mount_options="-ologdev=$2"
342 if [ "$3" != "none" ]; then
343 extra_rt_options="-r$3"
344 extra_mount_options=$extra_mount_options" -ortdev=$3"
346 extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"
348 [ "$FSTYP" != xfs ] && return 0
350 type=`_fs_type $device`
353 if [ "$type" = "xfs" ]; then
354 if [ -n "$TEST_XFS_SCRUB" ] && [ -x "$XFS_SCRUB_PROG" ]; then
355 "$XFS_SCRUB_PROG" $scrubflag -vd $device >>$seqres.full
356 if [ $? -ne 0 ]; then
357 echo "filesystem on $device failed scrub (see $seqres.full)"
362 mountpoint=`_umount_or_remount_ro $device`
365 $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
366 | tee $tmp.logprint | grep -q "<CLEAN>"
367 if [ $? -ne 0 -a "$HOSTOS" = "Linux" ]; then
368 echo "_check_xfs_filesystem: filesystem on $device has dirty log (see $seqres.full)"
370 echo "_check_xfs_filesystem: filesystem on $device has dirty log" >>$seqres.full
371 echo "*** xfs_logprint -t output ***" >>$seqres.full
372 cat $tmp.logprint >>$seqres.full
373 echo "*** end xfs_logprint output" >>$seqres.full
378 # xfs_check runs out of memory on large files, so even providing the test
379 # option (-t) to avoid indexing the free space trees doesn't make it pass on
380 # large filesystems. Avoid it.
381 if [ "$LARGE_SCRATCH_DEV" != yes ]; then
382 _xfs_check $extra_log_options $device 2>&1 |\
383 _fix_malloc >$tmp.fs_check
385 if [ -s $tmp.fs_check ]; then
386 echo "_check_xfs_filesystem: filesystem on $device is inconsistent (c) (see $seqres.full)"
388 echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
389 echo "*** xfs_check output ***" >>$seqres.full
390 cat $tmp.fs_check >>$seqres.full
391 echo "*** end xfs_check output" >>$seqres.full
396 $XFS_REPAIR_PROG -n $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
397 if [ $? -ne 0 ]; then
398 echo "_check_xfs_filesystem: filesystem on $device is inconsistent (r) (see $seqres.full)"
400 echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
401 echo "*** xfs_repair -n output ***" >>$seqres.full
402 cat $tmp.repair | _fix_malloc >>$seqres.full
403 echo "*** end xfs_repair output" >>$seqres.full
407 rm -f $tmp.fs_check $tmp.logprint $tmp.repair
409 # Optionally test the index rebuilding behavior.
410 if [ -n "$TEST_XFS_REPAIR_REBUILD" ]; then
411 $XFS_REPAIR_PROG $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
412 if [ $? -ne 0 ]; then
413 echo "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild) (see $seqres.full)"
415 echo "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild)" >>$seqres.full
416 echo "*** xfs_repair output ***" >>$seqres.full
417 cat $tmp.repair | _fix_malloc >>$seqres.full
418 echo "*** end xfs_repair output" >>$seqres.full
424 $XFS_REPAIR_PROG -n $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
425 if [ $? -ne 0 ]; then
426 echo "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild-reverify) (see $seqres.full)"
428 echo "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild-reverify)" >>$seqres.full
429 echo "*** xfs_repair -n output ***" >>$seqres.full
430 cat $tmp.repair | _fix_malloc >>$seqres.full
431 echo "*** end xfs_repair output" >>$seqres.full
438 if [ $ok -eq 0 ]; then
439 echo "*** mount output ***" >>$seqres.full
440 _mount >>$seqres.full
441 echo "*** end mount output" >>$seqres.full
442 elif [ "$type" = "xfs" ]; then
443 _mount_or_remount_rw "$extra_mount_options" $device $mountpoint
446 if [ $ok -eq 0 ]; then
448 if [ "$iam" != "check" ]; then
461 [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
462 TEST_LOG="$TEST_LOGDEV"
464 [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
465 TEST_RT="$TEST_RTDEV"
467 _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT
469 # check for ipath consistency
470 if $XFS_GROWFS_PROG -n $TEST_DIR | grep -q 'inode-paths=1'; then
471 # errors go to stderr
472 xfs_check_ipaths $TEST_DIR >/dev/null
473 xfs_repair_ipaths -n $TEST_DIR >/dev/null
477 _require_xfs_test_rmapbt()
481 if [ "$(xfs_info "$TEST_DIR" | grep -c "rmapbt=1")" -ne 1 ]; then
482 _notrun "rmapbt not supported by test filesystem type: $FSTYP"
486 _require_xfs_scratch_rmapbt()
490 _scratch_mkfs > /dev/null
492 if [ "$(xfs_info "$SCRATCH_MNT" | grep -c "rmapbt=1")" -ne 1 ]; then
494 _notrun "rmapbt not supported by scratch filesystem type: $FSTYP"
516 $XFS_IO_PROG -c "bmap -${param}lpv" "$file" | grep -c "$@"
519 # Reset all xfs error handling attributes, set them to original
522 # Only one argument, and it's mandatory:
523 # - dev: device name, e.g. $SCRATCH_DEV
525 # Note: this function only works for XFS
526 _reset_xfs_sysfs_error_handling()
530 if [ ! -b "$dev" -o "$FSTYP" != "xfs" ]; then
531 _fail "Usage: reset_xfs_sysfs_error_handling <device>"
534 _set_fs_sysfs_attr $dev error/fail_at_unmount 1
535 echo -n "error/fail_at_unmount="
536 _get_fs_sysfs_attr $dev error/fail_at_unmount
538 # Make sure all will be configured to retry forever by default, except
539 # for ENODEV, which is an unrecoverable error, so it will be configured
540 # to not retry on error by default.
541 for e in default EIO ENOSPC; do
542 _set_fs_sysfs_attr $dev \
543 error/metadata/${e}/max_retries -1
544 echo -n "error/metadata/${e}/max_retries="
545 _get_fs_sysfs_attr $dev error/metadata/${e}/max_retries
547 _set_fs_sysfs_attr $dev \
548 error/metadata/${e}/retry_timeout_seconds 0
549 echo -n "error/metadata/${e}/retry_timeout_seconds="
550 _get_fs_sysfs_attr $dev \
551 error/metadata/${e}/retry_timeout_seconds
555 # Skip if we are running an older binary without the stricter input checks.
556 # Make multiple checks to be sure that there is no regression on the one
557 # selected feature check, which would skew the result.
559 # At first, make a common function that runs the tests and returns
560 # number of failed cases.
561 _xfs_mkfs_validation_check()
563 local tmpfile=`mktemp`
564 local cmd="$MKFS_XFS_PROG -f -N -d file,name=$tmpfile,size=1g"
566 $cmd -s size=2s >/dev/null 2>&1
569 $cmd -l version=2,su=260k >/dev/null 2>&1
576 # Skip the test if all calls passed - mkfs accepts invalid input
577 _require_xfs_mkfs_validation()
579 _xfs_mkfs_validation_check
580 if [ "$?" -eq 0 ]; then
581 _notrun "Requires newer mkfs with stricter input checks: the oldest supported version of xfsprogs is 4.7."
585 # The opposite of _require_xfs_mkfs_validation.
586 _require_xfs_mkfs_without_validation()
588 _xfs_mkfs_validation_check
589 if [ "$?" -ne 0 ]; then
590 _notrun "Requires older mkfs without strict input checks: the last supported version of xfsprogs is 4.5."
594 # XFS ability to change UUIDs on V5/CRC filesystems
598 # This will create a crc fs on $SCRATCH_DEV
601 _scratch_xfs_db -x -c "uuid restore" 2>&1 \
602 | grep -q "invalid UUID\|supported on V5 fs" \
603 && _notrun "Userspace doesn't support meta_uuid feature"
605 _scratch_xfs_db -x -c "uuid generate" >/dev/null 2>&1
607 _scratch_mount >/dev/null 2>&1 \
608 || _notrun "Kernel doesn't support meta_uuid feature"