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