xfs: detect time limits from filesystem
[xfstests-dev.git] / common / xfs
1 #
2 # XFS specific common functions.
3 #
4
5 _setup_large_xfs_fs()
6 {
7         fs_size=$1
8         local tmp_dir=/tmp/
9
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
13
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))
19
20         # mount the filesystem, create the file, unmount it
21         _try_scratch_mount 2>&1 >$tmp_dir/mnt.err
22         local status=$?
23         if [ $status -ne 0 ]; then
24                 echo "mount failed"
25                 cat $tmp_dir/mnt.err >&2
26                 rm -f $tmp_dir/mnt.err
27                 return $status
28         fi
29         rm -f $tmp_dir/mnt.err
30
31         xfs_io -F -f \
32                 -c "truncate $file_size" \
33                 -c "falloc -k 0 $file_size" \
34                 -c "chattr +d" \
35                 $SCRATCH_MNT/.use_space 2>&1 > /dev/null
36         export NUM_SPACE_FILES=1
37         status=$?
38         _scratch_unmount
39         if [ $status -ne 0 ]; then
40                 echo "large file prealloc failed"
41                 cat $tmp_dir/mnt.err >&2
42                 return $status
43         fi
44         return 0
45 }
46
47 _scratch_mkfs_xfs_opts()
48 {
49         mkfs_opts=$*
50
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"`
54         fi
55
56         _scratch_options mkfs
57
58         echo "$MKFS_XFS_PROG $SCRATCH_OPTIONS $mkfs_opts"
59 }
60
61
62 _scratch_mkfs_xfs_supported()
63 {
64         local mkfs_opts=$*
65
66         _scratch_options mkfs
67
68         $MKFS_XFS_PROG -N $MKFS_OPTIONS $SCRATCH_OPTIONS $mkfs_opts $SCRATCH_DEV
69         local mkfs_status=$?
70
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
75                 mkfs_status=$?
76         fi
77         return $mkfs_status
78 }
79
80 # Returns the minimum XFS log size, in units of log blocks.
81 _scratch_find_xfs_min_logblocks()
82 {
83         local mkfs_cmd="`_scratch_mkfs_xfs_opts`"
84
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
87         # minimum log size.
88         local XFS_MIN_LOG_BYTES=2097152
89
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.
93         #
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
100         local mkfs_status=$?
101
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
109                 mkfs_status=$?
110         fi
111
112         # mkfs suceeded, so we must pick out the log block size to do the
113         # unit conversion
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
119                 return
120         fi
121
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
127                 return
128         fi
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
133                 return
134         fi
135
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
141 }
142
143 _scratch_mkfs_xfs()
144 {
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`
150         local mkfs_status
151
152         _scratch_do_mkfs "$mkfs_cmd" "$mkfs_filter" $* 2>$tmp.mkfserr 1>$tmp.mkfsstd
153         mkfs_status=$?
154
155         grep -q crc=0 $tmp.mkfsstd && _force_xfsv4_mount_options
156
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
159                 local fs_size
160                 fs_size=`cat $tmp.mkfsstd | perl -ne '
161                         if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+)/) {
162                                 my $size = $1 * $2;
163                                 print STDOUT "$size\n";
164                         }'`
165                 _setup_large_xfs_fs $fs_size
166                 mkfs_status=$?
167         fi
168
169         # output mkfs stdout and stderr
170         cat $tmp.mkfsstd
171         cat $tmp.mkfserr >&2
172         rm -f $tmp.mkfserr $tmp.mkfsstd
173
174         return $mkfs_status
175 }
176
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
179 # size.
180 _xfs_get_file_block_size()
181 {
182         local path="$1"
183
184         if ! ($XFS_IO_PROG -c "stat -v" "$path" 2>&1 | egrep -q '(rt-inherit|realtime)'); then
185                 _get_block_size "$path"
186                 return
187         fi
188
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")"
193         done
194         $XFS_INFO_PROG "$path" | grep realtime | sed -e 's/^.*extsz=\([0-9]*\).*$/\1/g'
195 }
196
197 _xfs_get_fsxattr()
198 {
199         local field="$1"
200         local path="$2"
201
202         local value=$($XFS_IO_PROG -c "stat" "$path" | grep -w "$field")
203         echo ${value##fsxattr.${field} = }
204 }
205
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.
209 _xfs_check()
210 {
211         OPTS=" "
212         DBOPTS=" "
213         USAGE="Usage: xfs_check [-fsvV] [-l logdev] [-i ino]... [-b bno]... special"
214
215         OPTIND=1
216         while getopts "b:fi:l:stvV" c; do
217                 case $c in
218                         s) OPTS=$OPTS"-s ";;
219                         t) OPTS=$OPTS"-t ";;
220                         v) OPTS=$OPTS"-v ";;
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
226                            return $?
227                            ;;
228                 esac
229         done
230         set -- extra $@
231         shift $OPTIND
232         case $# in
233                 1) ${XFS_DB_PROG}${DBOPTS} -F -i -p xfs_check -c "check$OPTS" $1
234                    status=$?
235                    ;;
236                 2) echo $USAGE 1>&1
237                    status=2
238                    ;;
239         esac
240         return $status
241 }
242
243 _scratch_xfs_db_options()
244 {
245         SCRATCH_OPTIONS=""
246         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
247                 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
248         echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
249 }
250
251 _scratch_xfs_db()
252 {
253         $XFS_DB_PROG "$@" $(_scratch_xfs_db_options)
254 }
255
256 _test_xfs_db_options()
257 {
258         TEST_OPTIONS=""
259         [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
260                 TEST_OPTIONS="-l$TEST_LOGDEV"
261         echo $TEST_OPTIONS $* $TEST_DEV
262 }
263
264 _test_xfs_db()
265 {
266         $XFS_DB_PROG "$@" $(_test_xfs_db_options)
267 }
268
269 _scratch_xfs_admin()
270 {
271         local options=("$SCRATCH_DEV")
272         local rt_opts=()
273         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
274                 options+=("$SCRATCH_LOGDEV")
275         if [ "$USE_EXTERNAL" = yes ] && [ -n "$SCRATCH_RTDEV" ]; then
276                 $XFS_ADMIN_PROG --help 2>&1 | grep -q 'rtdev' || \
277                         _notrun 'xfs_admin does not support rt devices'
278                 rt_opts+=(-r "$SCRATCH_RTDEV")
279         fi
280
281         # xfs_admin in xfsprogs 5.11 has a bug where an external log device
282         # forces xfs_db to be invoked, potentially with zero command arguments.
283         # When this happens, xfs_db will wait for input on stdin, which causes
284         # fstests to hang.  Since xfs_admin is not an interactive tool, we
285         # can redirect stdin from /dev/null to prevent this issue.
286         $XFS_ADMIN_PROG "${rt_opts[@]}" "$@" "${options[@]}" < /dev/null
287 }
288
289 _scratch_xfs_logprint()
290 {
291         SCRATCH_OPTIONS=""
292         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
293                 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
294         $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
295 }
296
297 _test_xfs_logprint()
298 {
299         TEST_OPTIONS=""
300         [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
301                 TEST_OPTIONS="-l$TEST_LOGDEV"
302         $XFS_LOGPRINT_PROG $TEST_OPTIONS $* $TEST_DEV
303 }
304
305 _scratch_xfs_check()
306 {
307         SCRATCH_OPTIONS=""
308         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
309                 SCRATCH_OPTIONS="-l $SCRATCH_LOGDEV"
310         [ "$LARGE_SCRATCH_DEV" = yes ] && \
311                 SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
312         _xfs_check $SCRATCH_OPTIONS $* $SCRATCH_DEV
313 }
314
315 # Check for secret debugging hooks in xfs_repair
316 _require_libxfs_debug_flag() {
317         local hook="$1"
318
319         grep -q "$hook" "$(type -P xfs_repair)" || \
320                 _notrun "libxfs debug hook $hook not detected?"
321 }
322
323 _scratch_xfs_repair()
324 {
325         SCRATCH_OPTIONS=""
326         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
327                 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
328         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
329                 SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
330         $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
331 }
332
333 # this test requires the projid32bit feature to be available in mkfs.xfs.
334 #
335 _require_projid32bit()
336 {
337        _scratch_mkfs_xfs_supported -i projid32bit=1 >/dev/null 2>&1 \
338            || _notrun "mkfs.xfs doesn't have projid32bit feature"
339 }
340
341 _require_projid16bit()
342 {
343         _scratch_mkfs_xfs_supported -i projid32bit=0 >/dev/null 2>&1 \
344            || _notrun "16 bit project IDs not supported on $SCRATCH_DEV"
345 }
346
347 # this test requires the crc feature to be available in mkfs.xfs
348 #
349 _require_xfs_mkfs_crc()
350 {
351         _scratch_mkfs_xfs_supported -m crc=1 >/dev/null 2>&1 \
352            || _notrun "mkfs.xfs doesn't have crc feature"
353 }
354
355 # this test requires the xfs kernel support crc feature
356 #
357 _require_xfs_crc()
358 {
359         _scratch_mkfs_xfs -m crc=1 >/dev/null 2>&1
360         _try_scratch_mount >/dev/null 2>&1 \
361            || _notrun "Kernel doesn't support crc feature"
362         _scratch_unmount
363 }
364
365 # this test requires the xfs kernel support crc feature on scratch device
366 #
367 _require_scratch_xfs_crc()
368 {
369         _scratch_mkfs_xfs >/dev/null 2>&1
370         _try_scratch_mount >/dev/null 2>&1 \
371            || _notrun "Kernel doesn't support crc feature"
372         $XFS_INFO_PROG $SCRATCH_MNT | grep -q 'crc=1' || _notrun "crc feature not supported by this filesystem"
373         _scratch_unmount
374 }
375
376 # this test requires the finobt feature to be available in mkfs.xfs
377 #
378 _require_xfs_mkfs_finobt()
379 {
380         _scratch_mkfs_xfs_supported -m crc=1,finobt=1 >/dev/null 2>&1 \
381            || _notrun "mkfs.xfs doesn't have finobt feature"
382 }
383
384 # this test requires the xfs kernel support finobt feature
385 #
386 _require_xfs_finobt()
387 {
388         _scratch_mkfs_xfs -m crc=1,finobt=1 >/dev/null 2>&1
389         _try_scratch_mount >/dev/null 2>&1 \
390            || _notrun "Kernel doesn't support finobt feature"
391         _scratch_unmount
392 }
393
394 # this test requires xfs sysfs attribute support
395 #
396 _require_xfs_sysfs()
397 {
398         attr=$1
399         sysfsdir=/sys/fs/xfs
400
401         if [ ! -e $sysfsdir ]; then
402                 _notrun "no kernel support for XFS sysfs attributes"
403         fi
404
405         if [ ! -z $1 ] && [ ! -e $sysfsdir/$attr ]; then
406                 _notrun "sysfs attribute '$attr' is not supported"
407         fi
408 }
409
410 # this test requires the xfs sparse inode feature
411 #
412 _require_xfs_sparse_inodes()
413 {
414         _scratch_mkfs_xfs_supported -m crc=1 -i sparse > /dev/null 2>&1 \
415                 || _notrun "mkfs.xfs does not support sparse inodes"
416         _scratch_mkfs_xfs -m crc=1 -i sparse > /dev/null 2>&1
417         _try_scratch_mount >/dev/null 2>&1 \
418                 || _notrun "kernel does not support sparse inodes"
419         _scratch_unmount
420 }
421
422 # check that xfs_db supports a specific command
423 _require_xfs_db_command()
424 {
425         if [ $# -ne 1 ]; then
426                 echo "Usage: _require_xfs_db_command command" 1>&2
427                 exit 1
428         fi
429         command=$1
430
431         _scratch_mkfs_xfs >/dev/null 2>&1
432         _scratch_xfs_db -x -c "help" | grep $command > /dev/null || \
433                 _notrun "xfs_db $command support is missing"
434 }
435
436 # Does the filesystem mounted from a particular device support scrub?
437 _supports_xfs_scrub()
438 {
439         local mountpoint="$1"
440         local device="$2"
441
442         if [ -z "$device" ] || [ -z "$mountpoint" ]; then
443                 echo "Usage: _supports_xfs_scrub mountpoint device"
444                 return 1
445         fi
446
447         if [ ! -b "$device" ] || [ ! -e "$mountpoint" ]; then
448                 return 1
449         fi
450
451         test "$FSTYP" = "xfs" || return 1
452         test -x "$XFS_SCRUB_PROG" || return 1
453
454         # Probe for kernel support...
455         $XFS_IO_PROG -c 'help scrub' 2>&1 | grep -q 'types are:.*probe' || return 1
456         $XFS_IO_PROG -c "scrub probe" "$mountpoint" 2>&1 | grep -q "Inappropriate ioctl" && return 1
457
458         # Scrub can't run on norecovery mounts
459         _fs_options "$device" | grep -q "norecovery" && return 1
460
461         return 0
462 }
463
464 # Save a snapshot of a corrupt xfs filesystem for later debugging.
465 _xfs_metadump() {
466         local metadump="$1"
467         local device="$2"
468         local logdev="$3"
469         local compressopt="$4"
470         shift; shift; shift; shift
471         local options="$@"
472         test -z "$options" && options="-a -o"
473
474         if [ "$logdev" != "none" ]; then
475                 options="$options -l $logdev"
476         fi
477
478         $XFS_METADUMP_PROG $options "$device" "$metadump"
479         res=$?
480         [ "$compressopt" = "compress" ] && [ -n "$DUMP_COMPRESSOR" ] &&
481                 $DUMP_COMPRESSOR "$metadump" &> /dev/null
482         return $res
483 }
484
485 # Snapshot the metadata on the scratch device
486 _scratch_xfs_metadump()
487 {
488         local metadump=$1
489         shift
490         local logdev=none
491
492         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
493                 logdev=$SCRATCH_LOGDEV
494
495         _xfs_metadump "$metadump" "$SCRATCH_DEV" "$logdev" nocompress "$@"
496 }
497
498 # run xfs_check and friends on a FS.
499 _check_xfs_filesystem()
500 {
501         if [ $# -ne 3 ]; then
502                 echo "Usage: _check_xfs_filesystem device <logdev>|none <rtdev>|none" 1>&2
503                 exit 1
504         fi
505
506         extra_mount_options=""
507         extra_log_options=""
508         extra_options=""
509         device=$1
510         if [ -f $device ]; then
511                 extra_options="-f"
512         fi
513
514         local logdev="$2"
515         if [ "$logdev" != "none" ]; then
516                 extra_log_options="-l$logdev"
517                 extra_mount_options="-ologdev=$logdev"
518         fi
519
520         local rtdev="$3"
521         if [ "$rtdev" != "none" ]; then
522                 extra_rt_options="-r$rtdev"
523                 extra_mount_options=$extra_mount_options" -ortdev=$rtdev"
524         fi
525         extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"
526
527         [ "$FSTYP" != xfs ] && return 0
528
529         type=`_fs_type $device`
530         ok=1
531
532         # Run online scrub if we can.
533         mntpt="$(_is_dev_mounted $device)"
534         if [ -n "$mntpt" ] && _supports_xfs_scrub "$mntpt" "$device"; then
535                 # Tests can create a scenario in which a call to syncfs() issued
536                 # at the end of the execution of the test script would return an
537                 # error code. xfs_scrub internally calls syncfs() before
538                 # starting the actual online consistency check operation. Since
539                 # such a call to syncfs() fails, xfs_scrub ends up returning
540                 # without performing consistency checks on the test
541                 # filesystem. This can mask a possible on-disk data structure
542                 # corruption. Hence consume such a possible syncfs() failure
543                 # before executing a scrub operation.
544                 $XFS_IO_PROG -c syncfs $mntpt >> $seqres.full 2>&1
545
546                 "$XFS_SCRUB_PROG" $scrubflag -v -d -n $mntpt > $tmp.scrub 2>&1
547                 if [ $? -ne 0 ]; then
548                         _log_err "_check_xfs_filesystem: filesystem on $device failed scrub"
549                         echo "*** xfs_scrub $scrubflag -v -d -n output ***" >> $seqres.full
550                         cat $tmp.scrub >> $seqres.full
551                         echo "*** end xfs_scrub output" >> $serqres.full
552                         ok=0
553                 fi
554                 rm -f $tmp.scrub
555         fi
556
557         if [ "$type" = "xfs" ]; then
558                 # mounted ...
559                 mountpoint=`_umount_or_remount_ro $device`
560         fi
561
562         $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
563                 | tee $tmp.logprint | grep -q "<CLEAN>"
564         if [ $? -ne 0 ]; then
565                 _log_err "_check_xfs_filesystem: filesystem on $device has dirty log"
566                 echo "*** xfs_logprint -t output ***"   >>$seqres.full
567                 cat $tmp.logprint                       >>$seqres.full
568                 echo "*** end xfs_logprint output"      >>$seqres.full
569
570                 ok=0
571         fi
572
573         # xfs_check runs out of memory on large files, so even providing the test
574         # option (-t) to avoid indexing the free space trees doesn't make it pass on
575         # large filesystems. Avoid it.
576         if [ "$LARGE_SCRATCH_DEV" != yes ]; then
577                 _xfs_check $extra_log_options $device 2>&1 > $tmp.fs_check
578         fi
579         if [ -s $tmp.fs_check ]; then
580                 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (c)"
581                 echo "*** xfs_check output ***"         >>$seqres.full
582                 cat $tmp.fs_check                       >>$seqres.full
583                 echo "*** end xfs_check output"         >>$seqres.full
584
585                 ok=0
586         fi
587
588         $XFS_REPAIR_PROG -n $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 (r)"
591                 echo "*** xfs_repair -n output ***"     >>$seqres.full
592                 cat $tmp.repair                         >>$seqres.full
593                 echo "*** end xfs_repair output"        >>$seqres.full
594
595                 ok=0
596         fi
597         rm -f $tmp.fs_check $tmp.logprint $tmp.repair
598
599         if [ "$ok" -ne 1 ] && [ "$DUMP_CORRUPT_FS" = "1" ]; then
600                 local flatdev="$(basename "$device")"
601                 _xfs_metadump "$seqres.$flatdev.check.md" "$device" "$logdev" \
602                         compress >> $seqres.full
603         fi
604
605         # Optionally test the index rebuilding behavior.
606         if [ -n "$TEST_XFS_REPAIR_REBUILD" ]; then
607                 rebuild_ok=1
608                 $XFS_REPAIR_PROG $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
609                 if [ $? -ne 0 ]; then
610                         _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild)"
611                         echo "*** xfs_repair output ***"        >>$seqres.full
612                         cat $tmp.repair                         >>$seqres.full
613                         echo "*** end xfs_repair output"        >>$seqres.full
614
615                         ok=0
616                         rebuild_ok=0
617                 fi
618                 rm -f $tmp.repair
619
620                 $XFS_REPAIR_PROG -n $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
621                 if [ $? -ne 0 ]; then
622                         _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild-reverify)"
623                         echo "*** xfs_repair -n output ***"     >>$seqres.full
624                         cat $tmp.repair                         >>$seqres.full
625                         echo "*** end xfs_repair output"        >>$seqres.full
626
627                         ok=0
628                         rebuild_ok=0
629                 fi
630                 rm -f $tmp.repair
631
632                 if [ "$rebuild_ok" -ne 1 ] && [ "$DUMP_CORRUPT_FS" = "1" ]; then
633                         local flatdev="$(basename "$device")"
634                         _xfs_metadump "$seqres.$flatdev.rebuild.md" "$device" \
635                                 "$logdev" compress >> $seqres.full
636                 fi
637         fi
638
639         if [ $ok -eq 0 ]; then
640                 echo "*** mount output ***"             >>$seqres.full
641                 _mount                                  >>$seqres.full
642                 echo "*** end mount output"             >>$seqres.full
643         elif [ "$type" = "xfs" ]; then
644                 _mount_or_remount_rw "$extra_mount_options" $device $mountpoint
645         fi
646
647         if [ $ok -eq 0 ]; then
648                 status=1
649                 if [ "$iam" != "check" ]; then
650                         exit 1
651                 fi
652                 return 1
653         fi
654
655         return 0
656 }
657
658 _check_xfs_test_fs()
659 {
660         TEST_LOG="none"
661         TEST_RT="none"
662         [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
663                 TEST_LOG="$TEST_LOGDEV"
664
665         [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
666                 TEST_RT="$TEST_RTDEV"
667
668         _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT
669         return $?
670 }
671
672 _require_xfs_test_rmapbt()
673 {
674         _require_test
675
676         if [ "$($XFS_INFO_PROG "$TEST_DIR" | grep -c "rmapbt=1")" -ne 1 ]; then
677                 _notrun "rmapbt not supported by test filesystem type: $FSTYP"
678         fi
679 }
680
681 _require_xfs_scratch_rmapbt()
682 {
683         _require_scratch
684
685         _scratch_mkfs > /dev/null
686         _scratch_mount
687         if [ "$($XFS_INFO_PROG "$SCRATCH_MNT" | grep -c "rmapbt=1")" -ne 1 ]; then
688                 _scratch_unmount
689                 _notrun "rmapbt not supported by scratch filesystem type: $FSTYP"
690         fi
691         _scratch_unmount
692 }
693
694 _xfs_bmapx_find()
695 {
696         case "$1" in
697         "attr")
698                 param="a"
699                 ;;
700         "cow")
701                 param="c"
702                 ;;
703         *)
704                 param="e"
705                 ;;
706         esac
707         shift
708         file="$1"
709         shift
710
711         $XFS_IO_PROG -c "bmap -${param}lpv" "$file" | grep -c "$@"
712 }
713
714 # Reset all xfs error handling attributes, set them to original
715 # status.
716 #
717 # Only one argument, and it's mandatory:
718 #  - dev: device name, e.g. $SCRATCH_DEV
719 #
720 # Note: this function only works for XFS
721 _reset_xfs_sysfs_error_handling()
722 {
723         local dev=$1
724
725         if [ ! -b "$dev" -o "$FSTYP" != "xfs" ]; then
726                 _fail "Usage: reset_xfs_sysfs_error_handling <device>"
727         fi
728
729         _set_fs_sysfs_attr $dev error/fail_at_unmount 1
730         echo -n "error/fail_at_unmount="
731         _get_fs_sysfs_attr $dev error/fail_at_unmount
732
733         # Make sure all will be configured to retry forever by default, except
734         # for ENODEV, which is an unrecoverable error, so it will be configured
735         # to not retry on error by default.
736         for e in default EIO ENOSPC; do
737                 _set_fs_sysfs_attr $dev \
738                                    error/metadata/${e}/max_retries -1
739                 echo -n "error/metadata/${e}/max_retries="
740                 _get_fs_sysfs_attr $dev error/metadata/${e}/max_retries
741
742                 _set_fs_sysfs_attr $dev \
743                                    error/metadata/${e}/retry_timeout_seconds 0
744                 echo -n "error/metadata/${e}/retry_timeout_seconds="
745                 _get_fs_sysfs_attr $dev \
746                                    error/metadata/${e}/retry_timeout_seconds
747         done
748 }
749
750 # Skip if we are running an older binary without the stricter input checks.
751 # Make multiple checks to be sure that there is no regression on the one
752 # selected feature check, which would skew the result.
753 #
754 # At first, make a common function that runs the tests and returns
755 # number of failed cases.
756 _xfs_mkfs_validation_check()
757 {
758         local tmpfile=`mktemp`
759         local cmd="$MKFS_XFS_PROG -f -N -d file,name=$tmpfile,size=1g"
760
761         $cmd -s size=8s >/dev/null 2>&1
762         local sum=$?
763
764         $cmd -l version=2,su=260k >/dev/null 2>&1
765         sum=`expr $sum + $?`
766
767         rm -f $tmpfile
768         return $sum
769 }
770
771 # Skip the test if all calls passed - mkfs accepts invalid input
772 _require_xfs_mkfs_validation()
773 {
774         _xfs_mkfs_validation_check
775         if [ "$?" -eq 0 ]; then
776                 _notrun "Requires newer mkfs with stricter input checks: the oldest supported version of xfsprogs is 4.7."
777         fi
778 }
779
780 # The opposite of _require_xfs_mkfs_validation.
781 _require_xfs_mkfs_without_validation()
782 {
783         _xfs_mkfs_validation_check
784         if [ "$?" -ne 0 ]; then
785                 _notrun "Requires older mkfs without strict input checks: the last supported version of xfsprogs is 4.5."
786         fi
787 }
788
789 # XFS ability to change UUIDs on V5/CRC filesystems
790 #
791 _require_meta_uuid()
792 {
793         # This will create a crc fs on $SCRATCH_DEV
794         _require_xfs_crc
795
796         _scratch_xfs_db -x -c "uuid restore" 2>&1 \
797            | grep -q "invalid UUID\|supported on V5 fs" \
798            && _notrun "Userspace doesn't support meta_uuid feature"
799
800         _scratch_xfs_db -x -c "uuid generate" >/dev/null 2>&1
801
802         _try_scratch_mount >/dev/null 2>&1 \
803            || _notrun "Kernel doesn't support meta_uuid feature"
804         _scratch_unmount
805 }
806
807 # this test requires mkfs.xfs have case-insensitive naming support
808 _require_xfs_mkfs_ciname()
809 {
810         _scratch_mkfs_xfs_supported -n version=ci >/dev/null 2>&1 \
811                 || _notrun "need case-insensitive naming support in mkfs.xfs"
812 }
813
814 # this test requires mkfs.xfs have configuration file support
815 _require_xfs_mkfs_cfgfile()
816 {
817         echo > /tmp/a
818         _scratch_mkfs_xfs_supported -c options=/tmp/a >/dev/null 2>&1
819         res=$?
820         rm -rf /tmp/a
821         test $res -eq 0 || _notrun "need configuration file support in mkfs.xfs"
822 }
823
824 # XFS_DEBUG requirements
825 _require_xfs_debug()
826 {
827         if grep -q "debug 0" /proc/fs/xfs/stat; then
828                 _notrun "Require XFS built with CONFIG_XFS_DEBUG"
829         fi
830 }
831 _require_no_xfs_debug()
832 {
833         if grep -q "debug 1" /proc/fs/xfs/stat; then
834                 _notrun "Require XFS built without CONFIG_XFS_DEBUG"
835         fi
836 }
837
838 # Require that assertions will not crash the system.
839 #
840 # Assertions would always crash the system if XFS assert fatal was enabled
841 # (CONFIG_XFS_ASSERT_FATAL=y).  If a test is designed to trigger an assertion,
842 # skip the test on a CONFIG_XFS_ASSERT_FATAL built XFS by default.  Note:
843 # CONFIG_XFS_ASSERT_FATAL can be disabled by setting bug_on_assert to zero if
844 # we want test to run.
845 _require_no_xfs_bug_on_assert()
846 {
847         if [ -f /sys/fs/xfs/debug/bug_on_assert ]; then
848                 grep -q "1" /sys/fs/xfs/debug/bug_on_assert && \
849                    _notrun "test requires XFS bug_on_assert to be off, turn it off to run the test"
850         else
851                 # Note: Prior to the creation of CONFIG_XFS_ASSERT_FATAL (and
852                 # the sysfs knob bug_on_assert), assertions would always crash
853                 # the system if XFS debug was enabled (CONFIG_XFS_DEBUG=y).  If
854                 # a test is designed to trigger an assertion and the test
855                 # designer does not want to hang fstests, skip the test.
856                 _require_no_xfs_debug
857         fi
858 }
859
860 # Get a metadata field
861 # The first arg is the field name
862 # The rest of the arguments are xfs_db commands to find the metadata.
863 _scratch_xfs_get_metadata_field()
864 {
865         local key="$1"
866         shift
867
868         local grep_key="$(echo "${key}" | tr '[]()' '....')"
869         local cmds=()
870         local arg
871         for arg in "$@"; do
872                 cmds+=("-c" "${arg}")
873         done
874         _scratch_xfs_db "${cmds[@]}" -c "print ${key}" | grep "^${grep_key}" | \
875                 sed -e 's/^.* = //g'
876 }
877
878 # Set a metadata field
879 # The first arg is the field name
880 # The second arg is the new value
881 # The rest of the arguments are xfs_db commands to find the metadata.
882 _scratch_xfs_set_metadata_field()
883 {
884         local key="$1"
885         local value="$2"
886         shift; shift
887
888         local cmds=()
889         local arg
890         for arg in "$@"; do
891                 cmds+=("-c" "${arg}")
892         done
893
894         local wr_cmd="write"
895         _scratch_xfs_db -x -c "help write" | egrep -q "(-c|-d)" && value="-- ${value}"
896         _scratch_xfs_db -x -c "help write" | egrep -q "(-d)" && wr_cmd="${wr_cmd} -d"
897         _scratch_xfs_db -x "${cmds[@]}" -c "${wr_cmd} ${key} ${value}"
898 }
899
900 _scratch_xfs_get_sb_field()
901 {
902         _scratch_xfs_get_metadata_field "$1" "sb 0"
903 }
904
905 _scratch_xfs_set_sb_field()
906 {
907         _scratch_xfs_set_metadata_field "$1" "$2" "sb 0"
908 }
909
910 # Before xfsprogs commit 4222d000ed("db: write via array indexing doesn't
911 # work"), xfs_db command to write a specific AGFL index doesn't work. It's a
912 # bug in a diagnostic tool that is only used by XFS developers as a test
913 # infrastructure, so it's fine to treat it as a infrastructure dependency as
914 # all other _require rules.
915 _require_xfs_db_write_array()
916 {
917         local supported=0
918
919         _require_test
920         touch $TEST_DIR/$seq.img
921         $MKFS_XFS_PROG -d file,name=$TEST_DIR/$seq.img,size=512m >/dev/null 2>&1
922         $XFS_DB_PROG -x -c "agfl 0" -c "write bno[32] 78" $TEST_DIR/$seq.img \
923                 >/dev/null 2>&1
924         $XFS_DB_PROG -x -c "agfl 0" -c "print bno[32]" $TEST_DIR/$seq.img \
925                 | grep -q "bno\[32\] = 78" && supported=1
926         rm -f $TEST_DIR/$seq.img
927         [ $supported -eq 0 ] && _notrun "xfs_db write can't support array"
928 }
929
930 _require_xfs_spaceman_command()
931 {
932         if [ -z "$1" ]; then
933                 echo "Usage: _require_xfs_spaceman_command command [switch]" 1>&2
934                 exit 1
935         fi
936         local command=$1
937         shift
938         local param="$*"
939         local param_checked=0
940         local opts=""
941
942         _require_command "$XFS_SPACEMAN_PROG" "xfs_spaceman"
943
944         testfile=$TEST_DIR/$$.xfs_spaceman
945         touch $testfile
946         case $command in
947         "health")
948                 testio=`$XFS_SPACEMAN_PROG -c "health $param" $TEST_DIR 2>&1`
949                 param_checked=1
950                 ;;
951         *)
952                 testio=`$XFS_SPACEMAN_PROG -c "help $command" $TEST_DIR 2>&1`
953         esac
954
955         rm -f $testfile 2>&1 > /dev/null
956         echo $testio | grep -q "not found" && \
957                 _notrun "xfs_spaceman $command support is missing"
958         echo $testio | grep -q "Operation not supported" && \
959                 _notrun "xfs_spaceman $command failed (old kernel/wrong fs?)"
960         echo $testio | grep -q "Invalid" && \
961                 _notrun "xfs_spaceman $command failed (old kernel/wrong fs/bad args?)"
962         echo $testio | grep -q "foreign file active" && \
963                 _notrun "xfs_spaceman $command not supported on $FSTYP"
964         echo $testio | grep -q "Inappropriate ioctl for device" && \
965                 _notrun "xfs_spaceman $command support is missing (missing ioctl?)"
966         echo $testio | grep -q "Function not implemented" && \
967                 _notrun "xfs_spaceman $command support is missing (missing syscall?)"
968
969         [ -n "$param" ] || return
970
971         if [ $param_checked -eq 0 ]; then
972                 $XFS_SPACEMAN_PROG -c "help $command" | grep -q "^ $param --" || \
973                         _notrun "xfs_spaceman $command doesn't support $param"
974         fi
975 }
976
977 _scratch_get_sfdir_prefix() {
978         local dir_ino="$1"
979
980         for prefix in "u.sfdir3" "u.sfdir2" "u3.sfdir3"; do
981                 if [ -n "$(_scratch_xfs_get_metadata_field \
982                                 "${prefix}.hdr.parent.i4" \
983                                 "inode ${dir_ino}")" ]; then
984                         echo "${prefix}"
985                         return 0
986                 fi
987         done
988         _scratch_xfs_db -c "inode ${dir_ino}" -c 'p' >> $seqres.full
989         return 1
990 }
991
992 _scratch_get_bmx_prefix() {
993         local ino="$1"
994
995         for prefix in "u3.bmx" "u.bmx"; do
996                 if [ -n "$(_scratch_xfs_get_metadata_field \
997                                 "${prefix}[0].startblock" \
998                                 "inode ${ino}")" ]; then
999                         echo "${prefix}"
1000                         return 0
1001                 fi
1002         done
1003         _scratch_xfs_db -c "inode ${ino}" -c 'p' >> $seqres.full
1004         return 1
1005 }
1006
1007 _scratch_get_iext_count()
1008 {
1009         local ino=$1
1010         local whichfork=$2
1011         local field=""
1012
1013         case $whichfork in
1014                 "attr")
1015                         field=core.naextents
1016                         ;;
1017                 "data")
1018                         field=core.nextents
1019                         ;;
1020                 *)
1021                         return 1
1022         esac
1023
1024         _scratch_xfs_get_metadata_field $field "inode $ino"
1025 }
1026
1027 #
1028 # Ensures that we don't pass any mount options incompatible with XFS v4
1029 #
1030 _force_xfsv4_mount_options()
1031 {
1032         local gquota=0
1033         local pquota=0
1034
1035         # Can't have group and project quotas in XFS v4
1036         echo "$MOUNT_OPTIONS" | egrep -q "(gquota|grpquota|grpjquota=|gqnoenforce)" && gquota=1
1037         echo "$MOUNT_OPTIONS" | egrep -q "(\bpquota|prjquota|pqnoenforce)" && pquota=1
1038
1039         if [ $gquota -gt 0 ] && [ $pquota -gt 0 ]; then
1040                 export MOUNT_OPTIONS=$(echo $MOUNT_OPTIONS \
1041                         | sed   -e 's/gquota/QUOTA/g'      \
1042                                 -e 's/grpquota/QUOTA/g'    \
1043                                 -e 's/grpjquota=[^, ]/QUOTA/g' \
1044                                 -e 's/gqnoenforce/QUOTA/g' \
1045                                 -e "s/QUOTA/defaults/g")
1046         fi
1047         echo "MOUNT_OPTIONS = $MOUNT_OPTIONS" >>$seqres.full
1048 }
1049
1050 # Find AG count of mounted filesystem
1051 _xfs_mount_agcount()
1052 {
1053         $XFS_INFO_PROG "$1" | grep agcount= | sed -e 's/^.*agcount=\([0-9]*\),.*$/\1/g'
1054 }
1055
1056 # Wipe the superblock of each XFS AGs
1057 _try_wipe_scratch_xfs()
1058 {
1059         local num='^[0-9]+$'
1060         local agcount
1061         local agsize
1062         local dbsize
1063
1064         # Try to wipe each SB if there's an existed XFS
1065         agcount=`_scratch_xfs_get_sb_field agcount 2>/dev/null`
1066         agsize=`_scratch_xfs_get_sb_field agblocks 2>/dev/null`
1067         dbsize=`_scratch_xfs_get_sb_field blocksize 2>/dev/null`
1068         if [[ $agcount =~ $num && $agsize =~ $num && $dbsize =~ $num ]];then
1069                 for ((i = 0; i < agcount; i++)); do
1070                         $XFS_IO_PROG -c "pwrite $((i * dbsize * agsize)) $dbsize" \
1071                                 $SCRATCH_DEV >/dev/null;
1072                 done
1073         fi
1074
1075         # Try to wipe each SB by default mkfs.xfs geometry
1076         local tmp=`mktemp -u`
1077         unset agcount agsize dbsize
1078         _scratch_mkfs_xfs -N 2>/dev/null | perl -ne '
1079                 if (/^meta-data=.*\s+agcount=(\d+), agsize=(\d+) blks/) {
1080                         print STDOUT "agcount=$1\nagsize=$2\n";
1081                 }
1082                 if (/^data\s+=\s+bsize=(\d+)\s/) {
1083                         print STDOUT "dbsize=$1\n";
1084                 }' > $tmp.mkfs
1085
1086         . $tmp.mkfs
1087         if [[ $agcount =~ $num && $agsize =~ $num && $dbsize =~ $num ]];then
1088                 for ((i = 0; i < agcount; i++)); do
1089                         $XFS_IO_PROG -c "pwrite $((i * dbsize * agsize)) $dbsize" \
1090                                 $SCRATCH_DEV >/dev/null;
1091                 done
1092         fi
1093         rm -f $tmp.mkfs
1094 }
1095
1096 _require_xfs_copy()
1097 {
1098         [ -n "$XFS_COPY_PROG" ] || _notrun "xfs_copy binary not yet installed"
1099         [ "$USE_EXTERNAL" = yes ] && \
1100                 _notrun "Cannot xfs_copy with external devices"
1101 }
1102
1103 __xfs_cowgc_interval_knob1="/proc/sys/fs/xfs/speculative_cow_prealloc_lifetime"
1104 __xfs_cowgc_interval_knob2="/proc/sys/fs/xfs/speculative_prealloc_lifetime"
1105
1106 _xfs_set_cowgc_interval() {
1107         if [ -w $__xfs_cowgc_interval_knob1 ]; then
1108                 echo "$@" > $__xfs_cowgc_interval_knob1
1109         elif [ -w $__xfs_cowgc_interval_knob2 ]; then
1110                 echo "$@" > $__xfs_cowgc_interval_knob2
1111         else
1112                 _fail "Can't find cowgc interval procfs knob?"
1113         fi
1114 }
1115
1116 _xfs_get_cowgc_interval() {
1117         if [ -w $__xfs_cowgc_interval_knob1 ]; then
1118                 cat $__xfs_cowgc_interval_knob1
1119         elif [ -w $__xfs_cowgc_interval_knob2 ]; then
1120                 cat $__xfs_cowgc_interval_knob2
1121         else
1122                 _fail "Can't find cowgc interval procfs knob?"
1123         fi
1124 }
1125
1126 # Print the status of the given features on the scratch filesystem.
1127 # Returns 0 if all features are found, 1 otherwise.
1128 _check_scratch_xfs_features()
1129 {
1130         local features="$(_scratch_xfs_db -c 'version')"
1131         local output=("FEATURES:")
1132         local found=0
1133
1134         for feature in "$@"; do
1135                 local status="NO"
1136                 if echo "${features}" | grep -q -w "${feature}"; then
1137                         status="YES"
1138                         found=$((found + 1))
1139                 fi
1140                 output+=("${feature}:${status}")
1141         done
1142
1143         echo "${output[@]}"
1144         test "${found}" -eq "$#"
1145 }
1146
1147 # Decide if xfs_repair knows how to set (or clear) a filesystem feature.
1148 _require_xfs_repair_upgrade()
1149 {
1150         local type="$1"
1151
1152         $XFS_REPAIR_PROG -c "$type=garbagevalue" 2>&1 | \
1153                 grep -q 'unknown option' && \
1154                 _notrun "xfs_repair does not support upgrading fs with $type"
1155 }
1156
1157 # Require that the scratch device exists, that mkfs can format with inobtcount
1158 # enabled, and that the kernel can mount such a filesystem.
1159 _require_scratch_xfs_inobtcount()
1160 {
1161         _require_scratch
1162
1163         _scratch_mkfs -m inobtcount=1 &> /dev/null || \
1164                 _notrun "mkfs.xfs doesn't support inobtcount feature"
1165         _try_scratch_mount || \
1166                 _notrun "kernel doesn't support xfs inobtcount feature"
1167         _scratch_unmount
1168 }
1169
1170 _xfs_timestamp_range()
1171 {
1172         local device="$1"
1173         local use_db=0
1174         local dbprog="$XFS_DB_PROG $device"
1175         test "$device" = "$SCRATCH_DEV" && dbprog=_scratch_xfs_db
1176
1177         $dbprog -f -c 'help timelimit' | grep -v -q 'not found' && use_db=1
1178         if [ $use_db -eq 0 ]; then
1179                 # The "timelimit" command was added to xfs_db at the same time
1180                 # that bigtime was added to xfsprogs.  Therefore, we can assume
1181                 # the old timestamp range if the command isn't present.
1182                 echo "-$((1<<31)) $(((1<<31)-1))"
1183         else
1184                 $dbprog -f -c 'timelimit --compact' | \
1185                         awk '{printf("%s %s", $1, $2);}'
1186         fi
1187 }