xfs: add a _require_xfs_copy helper
[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            ! grep -q 'log size.*too small, minimum' $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
130         # Don't know what to do, so fail
131         echo "Cannot determine minimum log size" >&2
132         cat $tmp.mkfsstd >> $seqres.full
133         cat $tmp.mkfserr >> $seqres.full
134         rm -f $tmp.mkfsstd $tmp.mkfserr
135 }
136
137 _scratch_mkfs_xfs()
138 {
139         local mkfs_cmd="`_scratch_mkfs_xfs_opts`"
140         local mkfs_filter="sed -e '/less than device physical sector/d' \
141                                -e '/switching to logical sector/d' \
142                                -e '/Default configuration/d'"
143         local tmp=`mktemp -u`
144         local mkfs_status
145
146         _scratch_do_mkfs "$mkfs_cmd" "$mkfs_filter" $* 2>$tmp.mkfserr 1>$tmp.mkfsstd
147         mkfs_status=$?
148
149         grep -q crc=0 $tmp.mkfsstd && _force_xfsv4_mount_options
150
151         if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
152                 # manually parse the mkfs output to get the fs size in bytes
153                 local fs_size
154                 fs_size=`cat $tmp.mkfsstd | perl -ne '
155                         if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+)/) {
156                                 my $size = $1 * $2;
157                                 print STDOUT "$size\n";
158                         }'`
159                 _setup_large_xfs_fs $fs_size
160                 mkfs_status=$?
161         fi
162
163         # output mkfs stdout and stderr
164         cat $tmp.mkfsstd
165         cat $tmp.mkfserr >&2
166         rm -f $tmp.mkfserr $tmp.mkfsstd
167
168         return $mkfs_status
169 }
170
171 # xfs_check script is planned to be deprecated. But, we want to
172 # be able to invoke "xfs_check" behavior in xfstests in order to
173 # maintain the current verification levels.
174 _xfs_check()
175 {
176         OPTS=" "
177         DBOPTS=" "
178         USAGE="Usage: xfs_check [-fsvV] [-l logdev] [-i ino]... [-b bno]... special"
179
180         OPTIND=1
181         while getopts "b:fi:l:stvV" c; do
182                 case $c in
183                         s) OPTS=$OPTS"-s ";;
184                         t) OPTS=$OPTS"-t ";;
185                         v) OPTS=$OPTS"-v ";;
186                         i) OPTS=$OPTS"-i "$OPTARG" ";;
187                         b) OPTS=$OPTS"-b "$OPTARG" ";;
188                         f) DBOPTS=$DBOPTS" -f";;
189                         l) DBOPTS=$DBOPTS" -l "$OPTARG" ";;
190                         V) $XFS_DB_PROG -p xfs_check -V
191                            return $?
192                            ;;
193                 esac
194         done
195         set -- extra $@
196         shift $OPTIND
197         case $# in
198                 1) ${XFS_DB_PROG}${DBOPTS} -F -i -p xfs_check -c "check$OPTS" $1
199                    status=$?
200                    ;;
201                 2) echo $USAGE 1>&1
202                    status=2
203                    ;;
204         esac
205         return $status
206 }
207
208 _scratch_xfs_db_options()
209 {
210         SCRATCH_OPTIONS=""
211         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
212                 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
213         echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
214 }
215
216 _scratch_xfs_db()
217 {
218         $XFS_DB_PROG "$@" $(_scratch_xfs_db_options)
219 }
220
221 _test_xfs_db_options()
222 {
223         TEST_OPTIONS=""
224         [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
225                 TEST_OPTIONS="-l$TEST_LOGDEV"
226         echo $TEST_OPTIONS $* $TEST_DEV
227 }
228
229 _test_xfs_db()
230 {
231         $XFS_DB_PROG "$@" $(_test_xfs_db_options)
232 }
233
234 _scratch_xfs_admin()
235 {
236         local options=("$SCRATCH_DEV")
237         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
238                 options+=("$SCRATCH_LOGDEV")
239         $XFS_ADMIN_PROG "$@" "${options[@]}"
240 }
241
242 _scratch_xfs_logprint()
243 {
244         SCRATCH_OPTIONS=""
245         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
246                 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
247         $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
248 }
249
250 _test_xfs_logprint()
251 {
252         TEST_OPTIONS=""
253         [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
254                 TEST_OPTIONS="-l$TEST_LOGDEV"
255         $XFS_LOGPRINT_PROG $TEST_OPTIONS $* $TEST_DEV
256 }
257
258 _scratch_xfs_check()
259 {
260         SCRATCH_OPTIONS=""
261         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
262                 SCRATCH_OPTIONS="-l $SCRATCH_LOGDEV"
263         [ "$LARGE_SCRATCH_DEV" = yes ] && \
264                 SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
265         _xfs_check $SCRATCH_OPTIONS $* $SCRATCH_DEV
266 }
267
268 _scratch_xfs_repair()
269 {
270         SCRATCH_OPTIONS=""
271         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
272                 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
273         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
274                 SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
275         $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
276 }
277
278 # this test requires the projid32bit feature to be available in mkfs.xfs.
279 #
280 _require_projid32bit()
281 {
282        _scratch_mkfs_xfs_supported -i projid32bit=1 >/dev/null 2>&1 \
283            || _notrun "mkfs.xfs doesn't have projid32bit feature"
284 }
285
286 _require_projid16bit()
287 {
288         _scratch_mkfs_xfs_supported -i projid32bit=0 >/dev/null 2>&1 \
289            || _notrun "16 bit project IDs not supported on $SCRATCH_DEV"
290 }
291
292 # this test requires the crc feature to be available in mkfs.xfs
293 #
294 _require_xfs_mkfs_crc()
295 {
296         _scratch_mkfs_xfs_supported -m crc=1 >/dev/null 2>&1 \
297            || _notrun "mkfs.xfs doesn't have crc feature"
298 }
299
300 # this test requires the xfs kernel support crc feature
301 #
302 _require_xfs_crc()
303 {
304         _scratch_mkfs_xfs -m crc=1 >/dev/null 2>&1
305         _try_scratch_mount >/dev/null 2>&1 \
306            || _notrun "Kernel doesn't support crc feature"
307         _scratch_unmount
308 }
309
310 # this test requires the xfs kernel support crc feature on scratch device
311 #
312 _require_scratch_xfs_crc()
313 {
314         _scratch_mkfs_xfs >/dev/null 2>&1
315         _try_scratch_mount >/dev/null 2>&1 \
316            || _notrun "Kernel doesn't support crc feature"
317         $XFS_INFO_PROG $SCRATCH_MNT | grep -q 'crc=1' || _notrun "crc feature not supported by this filesystem"
318         _scratch_unmount
319 }
320
321 # this test requires the finobt feature to be available in mkfs.xfs
322 #
323 _require_xfs_mkfs_finobt()
324 {
325         _scratch_mkfs_xfs_supported -m crc=1,finobt=1 >/dev/null 2>&1 \
326            || _notrun "mkfs.xfs doesn't have finobt feature"
327 }
328
329 # this test requires the xfs kernel support finobt feature
330 #
331 _require_xfs_finobt()
332 {
333         _scratch_mkfs_xfs -m crc=1,finobt=1 >/dev/null 2>&1
334         _try_scratch_mount >/dev/null 2>&1 \
335            || _notrun "Kernel doesn't support finobt feature"
336         _scratch_unmount
337 }
338
339 # this test requires xfs sysfs attribute support
340 #
341 _require_xfs_sysfs()
342 {
343         attr=$1
344         sysfsdir=/sys/fs/xfs
345
346         if [ ! -e $sysfsdir ]; then
347                 _notrun "no kernel support for XFS sysfs attributes"
348         fi
349
350         if [ ! -z $1 ] && [ ! -e $sysfsdir/$attr ]; then
351                 _notrun "sysfs attribute '$attr' is not supported"
352         fi
353 }
354
355 # this test requires the xfs sparse inode feature
356 #
357 _require_xfs_sparse_inodes()
358 {
359         _scratch_mkfs_xfs_supported -m crc=1 -i sparse > /dev/null 2>&1 \
360                 || _notrun "mkfs.xfs does not support sparse inodes"
361         _scratch_mkfs_xfs -m crc=1 -i sparse > /dev/null 2>&1
362         _try_scratch_mount >/dev/null 2>&1 \
363                 || _notrun "kernel does not support sparse inodes"
364         _scratch_unmount
365 }
366
367 # check that xfs_db supports a specific command
368 _require_xfs_db_command()
369 {
370         if [ $# -ne 1 ]; then
371                 echo "Usage: _require_xfs_db_command command" 1>&2
372                 exit 1
373         fi
374         command=$1
375
376         _scratch_mkfs_xfs >/dev/null 2>&1
377         _scratch_xfs_db -x -c "help" | grep $command > /dev/null || \
378                 _notrun "xfs_db $command support is missing"
379 }
380
381 # Does the filesystem mounted from a particular device support scrub?
382 _supports_xfs_scrub()
383 {
384         local mountpoint="$1"
385         local device="$2"
386
387         if [ -z "$device" ] || [ -z "$mountpoint" ]; then
388                 echo "Usage: _supports_xfs_scrub mountpoint device"
389                 return 1
390         fi
391
392         if [ ! -b "$device" ] || [ ! -e "$mountpoint" ]; then
393                 return 1
394         fi
395
396         test "$FSTYP" = "xfs" || return 1
397         test -x "$XFS_SCRUB_PROG" || return 1
398
399         # Probe for kernel support...
400         $XFS_IO_PROG -c 'help scrub' 2>&1 | grep -q 'types are:.*probe' || return 1
401         $XFS_IO_PROG -c "scrub probe" "$mountpoint" 2>&1 | grep -q "Inappropriate ioctl" && return 1
402
403         # Scrub can't run on norecovery mounts
404         _fs_options "$device" | grep -q "norecovery" && return 1
405
406         return 0
407 }
408
409 # run xfs_check and friends on a FS.
410 _check_xfs_filesystem()
411 {
412         if [ $# -ne 3 ]; then
413                 echo "Usage: _check_xfs_filesystem device <logdev>|none <rtdev>|none" 1>&2
414                 exit 1
415         fi
416
417         extra_mount_options=""
418         extra_log_options=""
419         extra_options=""
420         device=$1
421         if [ -f $device ]; then
422                 extra_options="-f"
423         fi
424
425         if [ "$2" != "none" ]; then
426                 extra_log_options="-l$2"
427                 extra_mount_options="-ologdev=$2"
428         fi
429
430         if [ "$3" != "none" ]; then
431                 extra_rt_options="-r$3"
432                 extra_mount_options=$extra_mount_options" -ortdev=$3"
433         fi
434         extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"
435
436         [ "$FSTYP" != xfs ] && return 0
437
438         type=`_fs_type $device`
439         ok=1
440
441         # Run online scrub if we can.
442         mntpt="$(_is_dev_mounted $device)"
443         if [ -n "$mntpt" ] && _supports_xfs_scrub "$mntpt" "$device"; then
444                 "$XFS_SCRUB_PROG" $scrubflag -v -d -n $mntpt > $tmp.scrub 2>&1
445                 if [ $? -ne 0 ]; then
446                         _log_err "_check_xfs_filesystem: filesystem on $device failed scrub"
447                         echo "*** xfs_scrub $scrubflag -v -d -n output ***" >> $seqres.full
448                         cat $tmp.scrub >> $seqres.full
449                         echo "*** end xfs_scrub output" >> $serqres.full
450                         ok=0
451                 fi
452                 rm -f $tmp.scrub
453         fi
454
455         if [ "$type" = "xfs" ]; then
456                 # mounted ...
457                 mountpoint=`_umount_or_remount_ro $device`
458         fi
459
460         $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
461                 | tee $tmp.logprint | grep -q "<CLEAN>"
462         if [ $? -ne 0 -a "$HOSTOS" = "Linux" ]; then
463                 _log_err "_check_xfs_filesystem: filesystem on $device has dirty log"
464                 echo "*** xfs_logprint -t output ***"   >>$seqres.full
465                 cat $tmp.logprint                       >>$seqres.full
466                 echo "*** end xfs_logprint output"      >>$seqres.full
467
468                 ok=0
469         fi
470
471         # xfs_check runs out of memory on large files, so even providing the test
472         # option (-t) to avoid indexing the free space trees doesn't make it pass on
473         # large filesystems. Avoid it.
474         if [ "$LARGE_SCRATCH_DEV" != yes ]; then
475                 _xfs_check $extra_log_options $device 2>&1 > $tmp.fs_check
476         fi
477         if [ -s $tmp.fs_check ]; then
478                 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (c)"
479                 echo "*** xfs_check output ***"         >>$seqres.full
480                 cat $tmp.fs_check                       >>$seqres.full
481                 echo "*** end xfs_check output"         >>$seqres.full
482
483                 ok=0
484         fi
485
486         $XFS_REPAIR_PROG -n $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
487         if [ $? -ne 0 ]; then
488                 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (r)"
489                 echo "*** xfs_repair -n output ***"     >>$seqres.full
490                 cat $tmp.repair                         >>$seqres.full
491                 echo "*** end xfs_repair output"        >>$seqres.full
492
493                 ok=0
494         fi
495         rm -f $tmp.fs_check $tmp.logprint $tmp.repair
496
497         # Optionally test the index rebuilding behavior.
498         if [ -n "$TEST_XFS_REPAIR_REBUILD" ]; then
499                 $XFS_REPAIR_PROG $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
500                 if [ $? -ne 0 ]; then
501                         _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild)"
502                         echo "*** xfs_repair output ***"        >>$seqres.full
503                         cat $tmp.repair                         >>$seqres.full
504                         echo "*** end xfs_repair output"        >>$seqres.full
505
506                         ok=0
507                 fi
508                 rm -f $tmp.repair
509
510                 $XFS_REPAIR_PROG -n $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
511                 if [ $? -ne 0 ]; then
512                         _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild-reverify)"
513                         echo "*** xfs_repair -n output ***"     >>$seqres.full
514                         cat $tmp.repair                         >>$seqres.full
515                         echo "*** end xfs_repair output"        >>$seqres.full
516
517                         ok=0
518                 fi
519                 rm -f $tmp.repair
520         fi
521
522         if [ $ok -eq 0 ]; then
523                 echo "*** mount output ***"             >>$seqres.full
524                 _mount                                  >>$seqres.full
525                 echo "*** end mount output"             >>$seqres.full
526         elif [ "$type" = "xfs" ]; then
527                 _mount_or_remount_rw "$extra_mount_options" $device $mountpoint
528         fi
529
530         if [ $ok -eq 0 ]; then
531                 status=1
532                 if [ "$iam" != "check" ]; then
533                         exit 1
534                 fi
535                 return 1
536         fi
537
538         return 0
539 }
540
541 _check_xfs_test_fs()
542 {
543         TEST_LOG="none"
544         TEST_RT="none"
545         [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
546                 TEST_LOG="$TEST_LOGDEV"
547
548         [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
549                 TEST_RT="$TEST_RTDEV"
550
551         _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT
552         return $?
553 }
554
555 _require_xfs_test_rmapbt()
556 {
557         _require_test
558
559         if [ "$($XFS_INFO_PROG "$TEST_DIR" | grep -c "rmapbt=1")" -ne 1 ]; then
560                 _notrun "rmapbt not supported by test filesystem type: $FSTYP"
561         fi
562 }
563
564 _require_xfs_scratch_rmapbt()
565 {
566         _require_scratch
567
568         _scratch_mkfs > /dev/null
569         _scratch_mount
570         if [ "$($XFS_INFO_PROG "$SCRATCH_MNT" | grep -c "rmapbt=1")" -ne 1 ]; then
571                 _scratch_unmount
572                 _notrun "rmapbt not supported by scratch filesystem type: $FSTYP"
573         fi
574         _scratch_unmount
575 }
576
577 _xfs_bmapx_find()
578 {
579         case "$1" in
580         "attr")
581                 param="a"
582                 ;;
583         "cow")
584                 param="c"
585                 ;;
586         *)
587                 param="e"
588                 ;;
589         esac
590         shift
591         file="$1"
592         shift
593
594         $XFS_IO_PROG -c "bmap -${param}lpv" "$file" | grep -c "$@"
595 }
596
597 # Reset all xfs error handling attributes, set them to original
598 # status.
599 #
600 # Only one argument, and it's mandatory:
601 #  - dev: device name, e.g. $SCRATCH_DEV
602 #
603 # Note: this function only works for XFS
604 _reset_xfs_sysfs_error_handling()
605 {
606         local dev=$1
607
608         if [ ! -b "$dev" -o "$FSTYP" != "xfs" ]; then
609                 _fail "Usage: reset_xfs_sysfs_error_handling <device>"
610         fi
611
612         _set_fs_sysfs_attr $dev error/fail_at_unmount 1
613         echo -n "error/fail_at_unmount="
614         _get_fs_sysfs_attr $dev error/fail_at_unmount
615
616         # Make sure all will be configured to retry forever by default, except
617         # for ENODEV, which is an unrecoverable error, so it will be configured
618         # to not retry on error by default.
619         for e in default EIO ENOSPC; do
620                 _set_fs_sysfs_attr $dev \
621                                    error/metadata/${e}/max_retries -1
622                 echo -n "error/metadata/${e}/max_retries="
623                 _get_fs_sysfs_attr $dev error/metadata/${e}/max_retries
624
625                 _set_fs_sysfs_attr $dev \
626                                    error/metadata/${e}/retry_timeout_seconds 0
627                 echo -n "error/metadata/${e}/retry_timeout_seconds="
628                 _get_fs_sysfs_attr $dev \
629                                    error/metadata/${e}/retry_timeout_seconds
630         done
631 }
632
633 # Skip if we are running an older binary without the stricter input checks.
634 # Make multiple checks to be sure that there is no regression on the one
635 # selected feature check, which would skew the result.
636 #
637 # At first, make a common function that runs the tests and returns
638 # number of failed cases.
639 _xfs_mkfs_validation_check()
640 {
641         local tmpfile=`mktemp`
642         local cmd="$MKFS_XFS_PROG -f -N -d file,name=$tmpfile,size=1g"
643
644         $cmd -s size=8s >/dev/null 2>&1
645         local sum=$?
646
647         $cmd -l version=2,su=260k >/dev/null 2>&1
648         sum=`expr $sum + $?`
649
650         rm -f $tmpfile
651         return $sum
652 }
653
654 # Skip the test if all calls passed - mkfs accepts invalid input
655 _require_xfs_mkfs_validation()
656 {
657         _xfs_mkfs_validation_check
658         if [ "$?" -eq 0 ]; then
659                 _notrun "Requires newer mkfs with stricter input checks: the oldest supported version of xfsprogs is 4.7."
660         fi
661 }
662
663 # The opposite of _require_xfs_mkfs_validation.
664 _require_xfs_mkfs_without_validation()
665 {
666         _xfs_mkfs_validation_check
667         if [ "$?" -ne 0 ]; then
668                 _notrun "Requires older mkfs without strict input checks: the last supported version of xfsprogs is 4.5."
669         fi
670 }
671
672 # XFS ability to change UUIDs on V5/CRC filesystems
673 #
674 _require_meta_uuid()
675 {
676         # This will create a crc fs on $SCRATCH_DEV
677         _require_xfs_crc
678
679         _scratch_xfs_db -x -c "uuid restore" 2>&1 \
680            | grep -q "invalid UUID\|supported on V5 fs" \
681            && _notrun "Userspace doesn't support meta_uuid feature"
682
683         _scratch_xfs_db -x -c "uuid generate" >/dev/null 2>&1
684
685         _try_scratch_mount >/dev/null 2>&1 \
686            || _notrun "Kernel doesn't support meta_uuid feature"
687         _scratch_unmount
688 }
689
690 # this test requires mkfs.xfs have case-insensitive naming support
691 _require_xfs_mkfs_ciname()
692 {
693         _scratch_mkfs_xfs_supported -n version=ci >/dev/null 2>&1 \
694                 || _notrun "need case-insensitive naming support in mkfs.xfs"
695 }
696
697 # XFS_DEBUG requirements
698 _require_xfs_debug()
699 {
700         if grep -q "debug 0" /proc/fs/xfs/stat; then
701                 _notrun "Require XFS built with CONFIG_XFS_DEBUG"
702         fi
703 }
704 _require_no_xfs_debug()
705 {
706         if grep -q "debug 1" /proc/fs/xfs/stat; then
707                 _notrun "Require XFS built without CONFIG_XFS_DEBUG"
708         fi
709 }
710
711 # Require that assertions will not crash the system.
712 #
713 # Assertions would always crash the system if XFS assert fatal was enabled
714 # (CONFIG_XFS_ASSERT_FATAL=y).  If a test is designed to trigger an assertion,
715 # skip the test on a CONFIG_XFS_ASSERT_FATAL built XFS by default.  Note:
716 # CONFIG_XFS_ASSERT_FATAL can be disabled by setting bug_on_assert to zero if
717 # we want test to run.
718 _require_no_xfs_bug_on_assert()
719 {
720         if [ -f /sys/fs/xfs/debug/bug_on_assert ]; then
721                 grep -q "1" /sys/fs/xfs/debug/bug_on_assert && \
722                    _notrun "test requires XFS bug_on_assert to be off, turn it off to run the test"
723         else
724                 # Note: Prior to the creation of CONFIG_XFS_ASSERT_FATAL (and
725                 # the sysfs knob bug_on_assert), assertions would always crash
726                 # the system if XFS debug was enabled (CONFIG_XFS_DEBUG=y).  If
727                 # a test is designed to trigger an assertion and the test
728                 # designer does not want to hang fstests, skip the test.
729                 _require_no_xfs_debug
730         fi
731 }
732
733 # Get a metadata field
734 # The first arg is the field name
735 # The rest of the arguments are xfs_db commands to find the metadata.
736 _scratch_xfs_get_metadata_field()
737 {
738         local key="$1"
739         shift
740
741         local grep_key="$(echo "${key}" | tr '[]()' '....')"
742         local cmds=()
743         local arg
744         for arg in "$@"; do
745                 cmds+=("-c" "${arg}")
746         done
747         _scratch_xfs_db "${cmds[@]}" -c "print ${key}" | grep "^${grep_key}" | \
748                 sed -e 's/^.* = //g'
749 }
750
751 # Set a metadata field
752 # The first arg is the field name
753 # The second arg is the new value
754 # The rest of the arguments are xfs_db commands to find the metadata.
755 _scratch_xfs_set_metadata_field()
756 {
757         local key="$1"
758         local value="$2"
759         shift; shift
760
761         local cmds=()
762         local arg
763         for arg in "$@"; do
764                 cmds+=("-c" "${arg}")
765         done
766
767         local wr_cmd="write"
768         _scratch_xfs_db -x -c "help write" | egrep -q "(-c|-d)" && value="-- ${value}"
769         _scratch_xfs_db -x -c "help write" | egrep -q "(-d)" && wr_cmd="${wr_cmd} -d"
770         _scratch_xfs_db -x "${cmds[@]}" -c "${wr_cmd} ${key} ${value}"
771 }
772
773 _scratch_xfs_get_sb_field()
774 {
775         _scratch_xfs_get_metadata_field "$1" "sb 0"
776 }
777
778 _scratch_xfs_set_sb_field()
779 {
780         _scratch_xfs_set_metadata_field "$1" "$2" "sb 0"
781 }
782
783 # Before xfsprogs commit 4222d000ed("db: write via array indexing doesn't
784 # work"), xfs_db command to write a specific AGFL index doesn't work. It's a
785 # bug in a diagnostic tool that is only used by XFS developers as a test
786 # infrastructure, so it's fine to treat it as a infrastructure dependency as
787 # all other _require rules.
788 _require_xfs_db_write_array()
789 {
790         local supported=0
791
792         _require_test
793         touch $TEST_DIR/$seq.img
794         $MKFS_XFS_PROG -d file,name=$TEST_DIR/$seq.img,size=512m >/dev/null 2>&1
795         $XFS_DB_PROG -x -c "agfl 0" -c "write bno[32] 78" $TEST_DIR/$seq.img \
796                 >/dev/null 2>&1
797         $XFS_DB_PROG -x -c "agfl 0" -c "print bno[32]" $TEST_DIR/$seq.img \
798                 | grep -q "bno\[32\] = 78" && supported=1
799         rm -f $TEST_DIR/$seq.img
800         [ $supported -eq 0 ] && _notrun "xfs_db write can't support array"
801 }
802
803 _require_xfs_spaceman_command()
804 {
805         if [ -z "$1" ]; then
806                 echo "Usage: _require_xfs_spaceman_command command [switch]" 1>&2
807                 exit 1
808         fi
809         local command=$1
810         shift
811         local param="$*"
812         local param_checked=0
813         local opts=""
814
815         _require_command "$XFS_SPACEMAN_PROG" "xfs_spaceman"
816
817         testfile=$TEST_DIR/$$.xfs_spaceman
818         touch $testfile
819         case $command in
820         "health")
821                 testio=`$XFS_SPACEMAN_PROG -c "health $param" $TEST_DIR 2>&1`
822                 param_checked=1
823                 ;;
824         *)
825                 testio=`$XFS_SPACEMAN_PROG -c "help $command" $TEST_DIR 2>&1`
826         esac
827
828         rm -f $testfile 2>&1 > /dev/null
829         echo $testio | grep -q "not found" && \
830                 _notrun "xfs_spaceman $command support is missing"
831         echo $testio | grep -q "Operation not supported" && \
832                 _notrun "xfs_spaceman $command failed (old kernel/wrong fs?)"
833         echo $testio | grep -q "Invalid" && \
834                 _notrun "xfs_spaceman $command failed (old kernel/wrong fs/bad args?)"
835         echo $testio | grep -q "foreign file active" && \
836                 _notrun "xfs_spaceman $command not supported on $FSTYP"
837         echo $testio | grep -q "Inappropriate ioctl for device" && \
838                 _notrun "xfs_spaceman $command support is missing (missing ioctl?)"
839         echo $testio | grep -q "Function not implemented" && \
840                 _notrun "xfs_spaceman $command support is missing (missing syscall?)"
841
842         [ -n "$param" ] || return
843
844         if [ $param_checked -eq 0 ]; then
845                 $XFS_SPACEMAN_PROG -c "help $command" | grep -q "^ $param --" || \
846                         _notrun "xfs_spaceman $command doesn't support $param"
847         fi
848 }
849
850 _scratch_get_sfdir_prefix() {
851         local dir_ino="$1"
852
853         for prefix in "u.sfdir3" "u.sfdir2" "u3.sfdir3"; do
854                 if [ -n "$(_scratch_xfs_get_metadata_field \
855                                 "${prefix}.hdr.parent.i4" \
856                                 "inode ${dir_ino}")" ]; then
857                         echo "${prefix}"
858                         return 0
859                 fi
860         done
861         _scratch_xfs_db -c "inode ${dir_ino}" -c 'p' >> $seqres.full
862         return 1
863 }
864
865 _scratch_get_bmx_prefix() {
866         local ino="$1"
867
868         for prefix in "u3.bmx" "u.bmx"; do
869                 if [ -n "$(_scratch_xfs_get_metadata_field \
870                                 "${prefix}[0].startblock" \
871                                 "inode ${ino}")" ]; then
872                         echo "${prefix}"
873                         return 0
874                 fi
875         done
876         _scratch_xfs_db -c "inode ${ino}" -c 'p' >> $seqres.full
877         return 1
878 }
879
880 #
881 # Ensures that we don't pass any mount options incompatible with XFS v4
882 #
883 _force_xfsv4_mount_options()
884 {
885         local gquota=0
886         local pquota=0
887
888         # Can't have group and project quotas in XFS v4
889         echo "$MOUNT_OPTIONS" | egrep -q "(gquota|grpquota|grpjquota=|gqnoenforce)" && gquota=1
890         echo "$MOUNT_OPTIONS" | egrep -q "(\bpquota|prjquota|pqnoenforce)" && pquota=1
891
892         if [ $gquota -gt 0 ] && [ $pquota -gt 0 ]; then
893                 export MOUNT_OPTIONS=$(echo $MOUNT_OPTIONS \
894                         | sed   -e 's/gquota/QUOTA/g'      \
895                                 -e 's/grpquota/QUOTA/g'    \
896                                 -e 's/grpjquota=[^, ]/QUOTA/g' \
897                                 -e 's/gqnoenforce/QUOTA/g' \
898                                 -e "s/QUOTA/defaults/g")
899         fi
900         echo "MOUNT_OPTIONS = $MOUNT_OPTIONS" >>$seqres.full
901 }
902
903 # Find AG count of mounted filesystem
904 _xfs_mount_agcount()
905 {
906         $XFS_INFO_PROG "$1" | grep agcount= | sed -e 's/^.*agcount=\([0-9]*\),.*$/\1/g'
907 }
908
909 # Wipe the superblock of each XFS AGs
910 _try_wipe_scratch_xfs()
911 {
912         local num='^[0-9]+$'
913         local agcount
914         local agsize
915         local dbsize
916
917         # Try to wipe each SB if there's an existed XFS
918         agcount=`_scratch_xfs_get_sb_field agcount 2>/dev/null`
919         agsize=`_scratch_xfs_get_sb_field agblocks 2>/dev/null`
920         dbsize=`_scratch_xfs_get_sb_field blocksize 2>/dev/null`
921         if [[ $agcount =~ $num && $agsize =~ $num && $dbsize =~ $num ]];then
922                 for ((i = 0; i < agcount; i++)); do
923                         $XFS_IO_PROG -c "pwrite $((i * dbsize * agsize)) $dbsize" \
924                                 $SCRATCH_DEV >/dev/null;
925                 done
926         fi
927
928         # Try to wipe each SB by default mkfs.xfs geometry
929         local tmp=`mktemp -u`
930         unset agcount agsize dbsize
931         _scratch_mkfs_xfs -N 2>/dev/null | perl -ne '
932                 if (/^meta-data=.*\s+agcount=(\d+), agsize=(\d+) blks/) {
933                         print STDOUT "agcount=$1\nagsize=$2\n";
934                 }
935                 if (/^data\s+=\s+bsize=(\d+)\s/) {
936                         print STDOUT "dbsize=$1\n";
937                 }' > $tmp.mkfs
938
939         . $tmp.mkfs
940         if [[ $agcount =~ $num && $agsize =~ $num && $dbsize =~ $num ]];then
941                 for ((i = 0; i < agcount; i++)); do
942                         $XFS_IO_PROG -c "pwrite $((i * dbsize * agsize)) $dbsize" \
943                                 $SCRATCH_DEV >/dev/null;
944                 done
945         fi
946         rm -f $tmp.mkfs
947 }
948
949 _require_xfs_copy()
950 {
951         [ -n "$XFS_COPY_PROG" ] || _notrun "xfs_copy binary not yet installed"
952         [ "$USE_EXTERNAL" = yes ] && \
953                 _notrun "Cannot xfs_copy with external devices"
954 }