common/xfs: Add require_xfs_db_write_array function
[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 _scratch_mkfs_xfs()
81 {
82         local mkfs_cmd="`_scratch_mkfs_xfs_opts`"
83         local mkfs_filter="sed -e '/less than device physical sector/d' \
84                                -e '/switching to logical sector/d' \
85                                -e '/Default configuration/d'"
86         local tmp=`mktemp -u`
87         local mkfs_status
88
89         _scratch_do_mkfs "$mkfs_cmd" "$mkfs_filter" $* 2>$tmp.mkfserr 1>$tmp.mkfsstd
90         mkfs_status=$?
91
92
93         if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
94                 # manually parse the mkfs output to get the fs size in bytes
95                 local fs_size
96                 fs_size=`cat $tmp.mkfsstd | perl -ne '
97                         if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+)/) {
98                                 my $size = $1 * $2;
99                                 print STDOUT "$size\n";
100                         }'`
101                 _setup_large_xfs_fs $fs_size
102                 mkfs_status=$?
103         fi
104
105         # output mkfs stdout and stderr
106         cat $tmp.mkfsstd
107         cat $tmp.mkfserr >&2
108         rm -f $tmp.mkfserr $tmp.mkfsstd
109
110         return $mkfs_status
111 }
112
113 # xfs_check script is planned to be deprecated. But, we want to
114 # be able to invoke "xfs_check" behavior in xfstests in order to
115 # maintain the current verification levels.
116 _xfs_check()
117 {
118         OPTS=" "
119         DBOPTS=" "
120         USAGE="Usage: xfs_check [-fsvV] [-l logdev] [-i ino]... [-b bno]... special"
121
122         OPTIND=1
123         while getopts "b:fi:l:stvV" c; do
124                 case $c in
125                         s) OPTS=$OPTS"-s ";;
126                         t) OPTS=$OPTS"-t ";;
127                         v) OPTS=$OPTS"-v ";;
128                         i) OPTS=$OPTS"-i "$OPTARG" ";;
129                         b) OPTS=$OPTS"-b "$OPTARG" ";;
130                         f) DBOPTS=$DBOPTS" -f";;
131                         l) DBOPTS=$DBOPTS" -l "$OPTARG" ";;
132                         V) $XFS_DB_PROG -p xfs_check -V
133                            return $?
134                            ;;
135                 esac
136         done
137         set -- extra $@
138         shift $OPTIND
139         case $# in
140                 1) ${XFS_DB_PROG}${DBOPTS} -F -i -p xfs_check -c "check$OPTS" $1
141                    status=$?
142                    ;;
143                 2) echo $USAGE 1>&1
144                    status=2
145                    ;;
146         esac
147         return $status
148 }
149
150 _scratch_xfs_db_options()
151 {
152         SCRATCH_OPTIONS=""
153         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
154                 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
155         echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
156 }
157
158 _scratch_xfs_db()
159 {
160         $XFS_DB_PROG "$@" $(_scratch_xfs_db_options)
161 }
162
163 _scratch_xfs_logprint()
164 {
165         SCRATCH_OPTIONS=""
166         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
167                 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
168         $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
169 }
170
171 _test_xfs_logprint()
172 {
173         TEST_OPTIONS=""
174         [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
175                 TEST_OPTIONS="-l$TEST_LOGDEV"
176         $XFS_LOGPRINT_PROG $TEST_OPTIONS $* $TEST_DEV
177 }
178
179 _scratch_xfs_check()
180 {
181         SCRATCH_OPTIONS=""
182         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
183                 SCRATCH_OPTIONS="-l $SCRATCH_LOGDEV"
184         [ "$LARGE_SCRATCH_DEV" = yes ] && \
185                 SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
186         _xfs_check $SCRATCH_OPTIONS $* $SCRATCH_DEV
187 }
188
189 _scratch_xfs_repair()
190 {
191         SCRATCH_OPTIONS=""
192         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
193                 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
194         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
195                 SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
196         [ "$LARGE_SCRATCH_DEV" = yes ] && SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
197         $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
198 }
199
200 # this test requires the projid32bit feature to be available in mkfs.xfs.
201 #
202 _require_projid32bit()
203 {
204        _scratch_mkfs_xfs_supported -i projid32bit=1 >/dev/null 2>&1 \
205            || _notrun "mkfs.xfs doesn't have projid32bit feature"
206 }
207
208 _require_projid16bit()
209 {
210         _scratch_mkfs_xfs_supported -i projid32bit=0 >/dev/null 2>&1 \
211            || _notrun "16 bit project IDs not supported on $SCRATCH_DEV"
212 }
213
214 # this test requires the crc feature to be available in mkfs.xfs
215 #
216 _require_xfs_mkfs_crc()
217 {
218         _scratch_mkfs_xfs_supported -m crc=1 >/dev/null 2>&1 \
219            || _notrun "mkfs.xfs doesn't have crc feature"
220 }
221
222 # this test requires the xfs kernel support crc feature
223 #
224 _require_xfs_crc()
225 {
226         _scratch_mkfs_xfs -m crc=1 >/dev/null 2>&1
227         _try_scratch_mount >/dev/null 2>&1 \
228            || _notrun "Kernel doesn't support crc feature"
229         _scratch_unmount
230 }
231
232 # this test requires the xfs kernel support crc feature on scratch device
233 #
234 _require_scratch_xfs_crc()
235 {
236         _scratch_mkfs_xfs >/dev/null 2>&1
237         _try_scratch_mount >/dev/null 2>&1 \
238            || _notrun "Kernel doesn't support crc feature"
239         xfs_info $SCRATCH_MNT | grep -q 'crc=1' || _notrun "crc feature not supported by this filesystem"
240         _scratch_unmount
241 }
242
243 # this test requires the finobt feature to be available in mkfs.xfs
244 #
245 _require_xfs_mkfs_finobt()
246 {
247         _scratch_mkfs_xfs_supported -m crc=1,finobt=1 >/dev/null 2>&1 \
248            || _notrun "mkfs.xfs doesn't have finobt feature"
249 }
250
251 # this test requires the xfs kernel support finobt feature
252 #
253 _require_xfs_finobt()
254 {
255         _scratch_mkfs_xfs -m crc=1,finobt=1 >/dev/null 2>&1
256         _try_scratch_mount >/dev/null 2>&1 \
257            || _notrun "Kernel doesn't support finobt feature"
258         _scratch_unmount
259 }
260
261 # this test requires xfs sysfs attribute support
262 #
263 _require_xfs_sysfs()
264 {
265         attr=$1
266         sysfsdir=/sys/fs/xfs
267
268         if [ ! -e $sysfsdir ]; then
269                 _notrun "no kernel support for XFS sysfs attributes"
270         fi
271
272         if [ ! -z $1 ] && [ ! -e $sysfsdir/$attr ]; then
273                 _notrun "sysfs attribute '$attr' is not supported"
274         fi
275 }
276
277 # this test requires the xfs sparse inode feature
278 #
279 _require_xfs_sparse_inodes()
280 {
281         _scratch_mkfs_xfs_supported -m crc=1 -i sparse > /dev/null 2>&1 \
282                 || _notrun "mkfs.xfs does not support sparse inodes"
283         _scratch_mkfs_xfs -m crc=1 -i sparse > /dev/null 2>&1
284         _try_scratch_mount >/dev/null 2>&1 \
285                 || _notrun "kernel does not support sparse inodes"
286         _scratch_unmount
287 }
288
289 # check that xfs_db supports a specific command
290 _require_xfs_db_command()
291 {
292         if [ $# -ne 1 ]; then
293                 echo "Usage: _require_xfs_db_command command" 1>&2
294                 exit 1
295         fi
296         command=$1
297
298         _scratch_xfs_db -x -c "help" | grep $command > /dev/null || \
299                 _notrun "xfs_db $command support is missing"
300 }
301
302 # Does the filesystem mounted from a particular device support scrub?
303 _supports_xfs_scrub()
304 {
305         local mountpoint="$1"
306         local device="$2"
307
308         if [ -z "$device" ] || [ -z "$mountpoint" ]; then
309                 echo "Usage: _supports_xfs_scrub mountpoint device"
310                 return 1
311         fi
312
313         if [ ! -b "$device" ] || [ ! -e "$mountpoint" ]; then
314                 return 1
315         fi
316
317         test "$FSTYP" = "xfs" || return 1
318         test -x "$XFS_SCRUB_PROG" || return 1
319
320         # Probe for kernel support...
321         $XFS_IO_PROG -c 'help scrub' 2>&1 | grep -q 'types are:.*probe' || return 1
322         $XFS_IO_PROG -c "scrub probe" "$mountpoint" 2>&1 | grep -q "Inappropriate ioctl" && return 1
323
324         # Scrub can't run on norecovery mounts
325         _fs_options "$device" | grep -q "norecovery" && return 1
326
327         return 0
328 }
329
330 # run xfs_check and friends on a FS.
331 _check_xfs_filesystem()
332 {
333         if [ $# -ne 3 ]; then
334                 echo "Usage: _check_xfs_filesystem device <logdev>|none <rtdev>|none" 1>&2
335                 exit 1
336         fi
337
338         extra_mount_options=""
339         extra_log_options=""
340         extra_options=""
341         device=$1
342         if [ -f $device ]; then
343                 extra_options="-f"
344         fi
345
346         if [ "$2" != "none" ]; then
347                 extra_log_options="-l$2"
348                 extra_mount_options="-ologdev=$2"
349         fi
350
351         if [ "$3" != "none" ]; then
352                 extra_rt_options="-r$3"
353                 extra_mount_options=$extra_mount_options" -ortdev=$3"
354         fi
355         extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"
356
357         [ "$FSTYP" != xfs ] && return 0
358
359         type=`_fs_type $device`
360         ok=1
361
362         # Run online scrub if we can.
363         mntpt="$(_is_dev_mounted $device)"
364         if [ -n "$mntpt" ] && _supports_xfs_scrub "$mntpt" "$device"; then
365                 "$XFS_SCRUB_PROG" $scrubflag -v -d -n $mntpt > $tmp.scrub 2>&1
366                 if [ $? -ne 0 ]; then
367                         _log_err "_check_xfs_filesystem: filesystem on $device failed scrub"
368                         echo "*** xfs_scrub $scrubflag -v -d -n output ***" >> $seqres.full
369                         cat $tmp.scrub >> $seqres.full
370                         echo "*** end xfs_scrub output" >> $serqres.full
371                         ok=0
372                 fi
373                 rm -f $tmp.scrub
374         fi
375
376         if [ "$type" = "xfs" ]; then
377                 # mounted ...
378                 mountpoint=`_umount_or_remount_ro $device`
379         fi
380
381         $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
382                 | tee $tmp.logprint | grep -q "<CLEAN>"
383         if [ $? -ne 0 -a "$HOSTOS" = "Linux" ]; then
384                 _log_err "_check_xfs_filesystem: filesystem on $device has dirty log"
385                 echo "*** xfs_logprint -t output ***"   >>$seqres.full
386                 cat $tmp.logprint                       >>$seqres.full
387                 echo "*** end xfs_logprint output"      >>$seqres.full
388
389                 ok=0
390         fi
391
392         # xfs_check runs out of memory on large files, so even providing the test
393         # option (-t) to avoid indexing the free space trees doesn't make it pass on
394         # large filesystems. Avoid it.
395         if [ "$LARGE_SCRATCH_DEV" != yes ]; then
396                 _xfs_check $extra_log_options $device 2>&1 |\
397                         _fix_malloc >$tmp.fs_check
398         fi
399         if [ -s $tmp.fs_check ]; then
400                 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (c)"
401                 echo "*** xfs_check output ***"         >>$seqres.full
402                 cat $tmp.fs_check                       >>$seqres.full
403                 echo "*** end xfs_check output"         >>$seqres.full
404
405                 ok=0
406         fi
407
408         $XFS_REPAIR_PROG -n $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
409         if [ $? -ne 0 ]; then
410                 _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (r)"
411                 echo "*** xfs_repair -n output ***"     >>$seqres.full
412                 cat $tmp.repair | _fix_malloc           >>$seqres.full
413                 echo "*** end xfs_repair output"        >>$seqres.full
414
415                 ok=0
416         fi
417         rm -f $tmp.fs_check $tmp.logprint $tmp.repair
418
419         # Optionally test the index rebuilding behavior.
420         if [ -n "$TEST_XFS_REPAIR_REBUILD" ]; then
421                 $XFS_REPAIR_PROG $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
422                 if [ $? -ne 0 ]; then
423                         _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild)"
424                         echo "*** xfs_repair output ***"        >>$seqres.full
425                         cat $tmp.repair | _fix_malloc           >>$seqres.full
426                         echo "*** end xfs_repair output"        >>$seqres.full
427
428                         ok=0
429                 fi
430                 rm -f $tmp.repair
431
432                 $XFS_REPAIR_PROG -n $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
433                 if [ $? -ne 0 ]; then
434                         _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild-reverify)"
435                         echo "*** xfs_repair -n output ***"     >>$seqres.full
436                         cat $tmp.repair | _fix_malloc           >>$seqres.full
437                         echo "*** end xfs_repair output"        >>$seqres.full
438
439                         ok=0
440                 fi
441                 rm -f $tmp.repair
442         fi
443
444         if [ $ok -eq 0 ]; then
445                 echo "*** mount output ***"             >>$seqres.full
446                 _mount                                  >>$seqres.full
447                 echo "*** end mount output"             >>$seqres.full
448         elif [ "$type" = "xfs" ]; then
449                 _mount_or_remount_rw "$extra_mount_options" $device $mountpoint
450         fi
451
452         if [ $ok -eq 0 ]; then
453                 status=1
454                 if [ "$iam" != "check" ]; then
455                         exit 1
456                 fi
457                 return 1
458         fi
459
460         return 0
461 }
462
463 _check_xfs_test_fs()
464 {
465         TEST_LOG="none"
466         TEST_RT="none"
467         [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
468                 TEST_LOG="$TEST_LOGDEV"
469
470         [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
471                 TEST_RT="$TEST_RTDEV"
472
473         _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT
474         return $?
475 }
476
477 _require_xfs_test_rmapbt()
478 {
479         _require_test
480
481         if [ "$(xfs_info "$TEST_DIR" | grep -c "rmapbt=1")" -ne 1 ]; then
482                 _notrun "rmapbt not supported by test filesystem type: $FSTYP"
483         fi
484 }
485
486 _require_xfs_scratch_rmapbt()
487 {
488         _require_scratch
489
490         _scratch_mkfs > /dev/null
491         _scratch_mount
492         if [ "$(xfs_info "$SCRATCH_MNT" | grep -c "rmapbt=1")" -ne 1 ]; then
493                 _scratch_unmount
494                 _notrun "rmapbt not supported by scratch filesystem type: $FSTYP"
495         fi
496         _scratch_unmount
497 }
498
499 _xfs_bmapx_find()
500 {
501         case "$1" in
502         "attr")
503                 param="a"
504                 ;;
505         "cow")
506                 param="c"
507                 ;;
508         *)
509                 param="e"
510                 ;;
511         esac
512         shift
513         file="$1"
514         shift
515
516         $XFS_IO_PROG -c "bmap -${param}lpv" "$file" | grep -c "$@"
517 }
518
519 # Reset all xfs error handling attributes, set them to original
520 # status.
521 #
522 # Only one argument, and it's mandatory:
523 #  - dev: device name, e.g. $SCRATCH_DEV
524 #
525 # Note: this function only works for XFS
526 _reset_xfs_sysfs_error_handling()
527 {
528         local dev=$1
529
530         if [ ! -b "$dev" -o "$FSTYP" != "xfs" ]; then
531                 _fail "Usage: reset_xfs_sysfs_error_handling <device>"
532         fi
533
534         _set_fs_sysfs_attr $dev error/fail_at_unmount 1
535         echo -n "error/fail_at_unmount="
536         _get_fs_sysfs_attr $dev error/fail_at_unmount
537
538         # Make sure all will be configured to retry forever by default, except
539         # for ENODEV, which is an unrecoverable error, so it will be configured
540         # to not retry on error by default.
541         for e in default EIO ENOSPC; do
542                 _set_fs_sysfs_attr $dev \
543                                    error/metadata/${e}/max_retries -1
544                 echo -n "error/metadata/${e}/max_retries="
545                 _get_fs_sysfs_attr $dev error/metadata/${e}/max_retries
546
547                 _set_fs_sysfs_attr $dev \
548                                    error/metadata/${e}/retry_timeout_seconds 0
549                 echo -n "error/metadata/${e}/retry_timeout_seconds="
550                 _get_fs_sysfs_attr $dev \
551                                    error/metadata/${e}/retry_timeout_seconds
552         done
553 }
554
555 # Skip if we are running an older binary without the stricter input checks.
556 # Make multiple checks to be sure that there is no regression on the one
557 # selected feature check, which would skew the result.
558 #
559 # At first, make a common function that runs the tests and returns
560 # number of failed cases.
561 _xfs_mkfs_validation_check()
562 {
563         local tmpfile=`mktemp`
564         local cmd="$MKFS_XFS_PROG -f -N -d file,name=$tmpfile,size=1g"
565
566         $cmd -s size=8s >/dev/null 2>&1
567         local sum=$?
568
569         $cmd -l version=2,su=260k >/dev/null 2>&1
570         sum=`expr $sum + $?`
571
572         rm -f $tmpfile
573         return $sum
574 }
575
576 # Skip the test if all calls passed - mkfs accepts invalid input
577 _require_xfs_mkfs_validation()
578 {
579         _xfs_mkfs_validation_check
580         if [ "$?" -eq 0 ]; then
581                 _notrun "Requires newer mkfs with stricter input checks: the oldest supported version of xfsprogs is 4.7."
582         fi
583 }
584
585 # The opposite of _require_xfs_mkfs_validation.
586 _require_xfs_mkfs_without_validation()
587 {
588         _xfs_mkfs_validation_check
589         if [ "$?" -ne 0 ]; then
590                 _notrun "Requires older mkfs without strict input checks: the last supported version of xfsprogs is 4.5."
591         fi
592 }
593
594 # XFS ability to change UUIDs on V5/CRC filesystems
595 #
596 _require_meta_uuid()
597 {
598         # This will create a crc fs on $SCRATCH_DEV
599         _require_xfs_crc
600
601         _scratch_xfs_db -x -c "uuid restore" 2>&1 \
602            | grep -q "invalid UUID\|supported on V5 fs" \
603            && _notrun "Userspace doesn't support meta_uuid feature"
604
605         _scratch_xfs_db -x -c "uuid generate" >/dev/null 2>&1
606
607         _try_scratch_mount >/dev/null 2>&1 \
608            || _notrun "Kernel doesn't support meta_uuid feature"
609         _scratch_unmount
610 }
611
612 # this test requires mkfs.xfs have case-insensitive naming support
613 _require_xfs_mkfs_ciname()
614 {
615         _scratch_mkfs_xfs_supported -n version=ci >/dev/null 2>&1 \
616                 || _notrun "need case-insensitive naming support in mkfs.xfs"
617 }
618
619 # XFS_DEBUG requirements
620 _require_xfs_debug()
621 {
622         if grep -q "debug 0" /proc/fs/xfs/stat; then
623                 _notrun "Require XFS built with CONFIG_XFS_DEBUG"
624         fi
625 }
626 _require_no_xfs_debug()
627 {
628         if grep -q "debug 1" /proc/fs/xfs/stat; then
629                 _notrun "Require XFS built without CONFIG_XFS_DEBUG"
630         fi
631 }
632
633 # Require that assertions will not crash the system.
634 #
635 # Assertions would always crash the system if XFS assert fatal was enabled
636 # (CONFIG_XFS_ASSERT_FATAL=y).  If a test is designed to trigger an assertion,
637 # skip the test on a CONFIG_XFS_ASSERT_FATAL built XFS by default.  Note:
638 # CONFIG_XFS_ASSERT_FATAL can be disabled by setting bug_on_assert to zero if
639 # we want test to run.
640 _require_no_xfs_bug_on_assert()
641 {
642         if [ -f /sys/fs/xfs/debug/bug_on_assert ]; then
643                 grep -q "1" /sys/fs/xfs/debug/bug_on_assert && \
644                    _notrun "test requires XFS bug_on_assert to be off, turn it off to run the test"
645         else
646                 # Note: Prior to the creation of CONFIG_XFS_ASSERT_FATAL (and
647                 # the sysfs knob bug_on_assert), assertions would always crash
648                 # the system if XFS debug was enabled (CONFIG_XFS_DEBUG=y).  If
649                 # a test is designed to trigger an assertion and the test
650                 # designer does not want to hang fstests, skip the test.
651                 _require_no_xfs_debug
652         fi
653 }
654
655 # Get a metadata field
656 # The first arg is the field name
657 # The rest of the arguments are xfs_db commands to find the metadata.
658 _scratch_xfs_get_metadata_field()
659 {
660         local key="$1"
661         shift
662
663         local grep_key="$(echo "${key}" | tr '[]()' '....')"
664         local cmds=()
665         local arg
666         for arg in "$@"; do
667                 cmds+=("-c" "${arg}")
668         done
669         _scratch_xfs_db "${cmds[@]}" -c "print ${key}" | grep "^${grep_key}" | \
670                 sed -e 's/^.* = //g'
671 }
672
673 # Set a metadata field
674 # The first arg is the field name
675 # The second arg is the new value
676 # The rest of the arguments are xfs_db commands to find the metadata.
677 _scratch_xfs_set_metadata_field()
678 {
679         local key="$1"
680         local value="$2"
681         shift; shift
682
683         local cmds=()
684         local arg
685         for arg in "$@"; do
686                 cmds+=("-c" "${arg}")
687         done
688
689         local wr_cmd="write"
690         _scratch_xfs_db -x -c "help write" | egrep -q "(-c|-d)" && value="-- ${value}"
691         _scratch_xfs_db -x -c "help write" | egrep -q "(-d)" && wr_cmd="${wr_cmd} -d"
692         _scratch_xfs_db -x "${cmds[@]}" -c "${wr_cmd} ${key} ${value}"
693 }
694
695 _scratch_xfs_get_sb_field()
696 {
697         _scratch_xfs_get_metadata_field "$1" "sb 0"
698 }
699
700 _scratch_xfs_set_sb_field()
701 {
702         _scratch_xfs_set_metadata_field "$1" "$2" "sb 0"
703 }
704
705 # Before xfsprogs commit 4222d000ed("db: write via array indexing doesn't
706 # work"), xfs_db command to write a specific AGFL index doesn't work. It's a
707 # bug in a diagnostic tool that is only used by XFS developers as a test
708 # infrastructure, so it's fine to treat it as a infrastructure dependency as
709 # all other _require rules.
710 _require_xfs_db_write_array()
711 {
712         local supported=0
713
714         _require_test
715         touch $TEST_DIR/$seq.img
716         $MKFS_XFS_PROG -d file,name=$TEST_DIR/$seq.img,size=512m >/dev/null 2>&1
717         $XFS_DB_PROG -x -c "agfl 0" -c "write bno[32] 78" $TEST_DIR/$seq.img \
718                 >/dev/null 2>&1
719         $XFS_DB_PROG -x -c "agfl 0" -c "print bno[32]" $TEST_DIR/$seq.img \
720                 | grep -q "bno\[32\] = 78" && supported=1
721         rm -f $TEST_DIR/$seq.img
722         [ $supported -eq 0 ] && _notrun "xfs_db write can't support array"
723 }