common: split XFS functions from common/rc
[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         _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         $MKFS_XFS_PROG $SCRATCH_OPTIONS $mkfs_opts $SCRATCH_DEV
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         # extra mkfs options can be added by tests
83         local extra_mkfs_options=$*
84
85         local tmp_dir=/tmp/
86
87         # save mkfs output in case conflict means we need to run again.
88         # only the output for the mkfs that applies should be shown
89         _scratch_mkfs_xfs_opts $MKFS_OPTIONS $extra_mkfs_options \
90                 2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
91         local mkfs_status=$?
92
93
94         # a mkfs failure may be caused by conflicts between
95         # $MKFS_OPTIONS and $extra_mkfs_options
96         if [ $mkfs_status -ne 0 -a ! -z "$extra_mkfs_options" ]; then
97                 (
98                 echo -n "** mkfs failed with extra mkfs options "
99                 echo "added to \"$MKFS_OPTIONS\" by test $seq **"
100                 echo -n "** attempting to mkfs using only test $seq "
101                 echo "options: $extra_mkfs_options **"
102                 ) >> $seqres.full
103
104                 # running mkfs again. overwrite previous mkfs output files
105                 _scratch_mkfs_xfs_opts $extra_mkfs_options \
106                         2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
107                 local mkfs_status=$?
108         fi
109
110         if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
111                 # manually parse the mkfs output to get the fs size in bytes
112                 local fs_size
113                 fs_size=`cat $tmp_dir.mkfsstd | perl -ne '
114                         if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+)/) {
115                                 my $size = $1 * $2;
116                                 print STDOUT "$size\n";
117                         }'`
118                 _setup_large_xfs_fs $fs_size
119                 mkfs_status=$?
120         fi
121
122         # output stored mkfs output, filtering unnecessary warnings from stderr
123         cat $tmp_dir.mkfsstd
124         cat $tmp_dir.mkfserr | sed \
125                 -e '/less than device physical sector/d' \
126                 -e '/switching to logical sector/d' \
127                 >&2
128         rm -f $tmp_dir.mkfserr $tmp_dir.mkfsstd
129
130         return $mkfs_status
131 }
132
133 # xfs_check script is planned to be deprecated. But, we want to
134 # be able to invoke "xfs_check" behavior in xfstests in order to
135 # maintain the current verification levels.
136 _xfs_check()
137 {
138         OPTS=" "
139         DBOPTS=" "
140         USAGE="Usage: xfs_check [-fsvV] [-l logdev] [-i ino]... [-b bno]... special"
141
142         while getopts "b:fi:l:stvV" c; do
143                 case $c in
144                         s) OPTS=$OPTS"-s ";;
145                         t) OPTS=$OPTS"-t ";;
146                         v) OPTS=$OPTS"-v ";;
147                         i) OPTS=$OPTS"-i "$OPTARG" ";;
148                         b) OPTS=$OPTS"-b "$OPTARG" ";;
149                         f) DBOPTS=$DBOPTS" -f";;
150                         l) DBOPTS=$DBOPTS" -l "$OPTARG" ";;
151                         V) $XFS_DB_PROG -p xfs_check -V
152                            return $?
153                            ;;
154                 esac
155         done
156         set -- extra $@
157         shift $OPTIND
158         case $# in
159                 1) ${XFS_DB_PROG}${DBOPTS} -F -i -p xfs_check -c "check$OPTS" $1
160                    status=$?
161                    ;;
162                 2) echo $USAGE 1>&1
163                    status=2
164                    ;;
165         esac
166         return $status
167 }
168
169 _scratch_xfs_db_options()
170 {
171         SCRATCH_OPTIONS=""
172         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
173                 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
174         echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
175 }
176
177 _scratch_xfs_db()
178 {
179         $XFS_DB_PROG "$@" $(_scratch_xfs_db_options)
180 }
181
182 _scratch_xfs_logprint()
183 {
184         SCRATCH_OPTIONS=""
185         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
186                 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
187         $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
188 }
189
190 _test_xfs_logprint()
191 {
192         TEST_OPTIONS=""
193         [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
194                 TEST_OPTIONS="-l$TEST_LOGDEV"
195         $XFS_LOGPRINT_PROG $TEST_OPTIONS $* $TEST_DEV
196 }
197
198 _scratch_xfs_check()
199 {
200         SCRATCH_OPTIONS=""
201         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
202                 SCRATCH_OPTIONS="-l $SCRATCH_LOGDEV"
203         [ "$LARGE_SCRATCH_DEV" = yes ] && \
204                 SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
205         _xfs_check $SCRATCH_OPTIONS $* $SCRATCH_DEV
206 }
207
208 _scratch_xfs_repair()
209 {
210         SCRATCH_OPTIONS=""
211         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
212                 SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
213         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
214                 SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
215         [ "$LARGE_SCRATCH_DEV" = yes ] && SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
216         $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
217 }
218
219 # this test requires the projid32bit feature to be available in mkfs.xfs.
220 #
221 _require_projid32bit()
222 {
223        _scratch_mkfs_xfs_supported -i projid32bit=1 >/dev/null 2>&1 \
224            || _notrun "mkfs.xfs doesn't have projid32bit feature"
225 }
226
227 _require_projid16bit()
228 {
229         _scratch_mkfs_xfs_supported -i projid32bit=0 >/dev/null 2>&1 \
230            || _notrun "16 bit project IDs not supported on $SCRATCH_DEV"
231 }
232
233 # this test requires the crc feature to be available in mkfs.xfs
234 #
235 _require_xfs_mkfs_crc()
236 {
237         _scratch_mkfs_xfs_supported -m crc=1 >/dev/null 2>&1 \
238            || _notrun "mkfs.xfs doesn't have crc feature"
239 }
240
241 # this test requires the xfs kernel support crc feature
242 #
243 _require_xfs_crc()
244 {
245         _scratch_mkfs_xfs -m crc=1 >/dev/null 2>&1
246         _scratch_mount >/dev/null 2>&1 \
247            || _notrun "Kernel doesn't support crc feature"
248         _scratch_unmount
249 }
250
251 # this test requires the xfs kernel support crc feature on scratch device
252 #
253 _require_scratch_xfs_crc()
254 {
255         _scratch_mkfs_xfs >/dev/null 2>&1
256         _scratch_mount >/dev/null 2>&1 \
257            || _notrun "Kernel doesn't support crc feature"
258         xfs_info $SCRATCH_MNT | grep -q 'crc=1' || _notrun "crc feature not supported by this filesystem"
259         _scratch_unmount
260 }
261
262 # this test requires the finobt feature to be available in mkfs.xfs
263 #
264 _require_xfs_mkfs_finobt()
265 {
266         _scratch_mkfs_xfs_supported -m crc=1,finobt=1 >/dev/null 2>&1 \
267            || _notrun "mkfs.xfs doesn't have finobt feature"
268 }
269
270 # this test requires the xfs kernel support finobt feature
271 #
272 _require_xfs_finobt()
273 {
274         _scratch_mkfs_xfs -m crc=1,finobt=1 >/dev/null 2>&1
275         _scratch_mount >/dev/null 2>&1 \
276            || _notrun "Kernel doesn't support finobt feature"
277         _scratch_unmount
278 }
279
280 # this test requires xfs sysfs attribute support
281 #
282 _require_xfs_sysfs()
283 {
284         attr=$1
285         sysfsdir=/sys/fs/xfs
286
287         if [ ! -e $sysfsdir ]; then
288                 _notrun "no kernel support for XFS sysfs attributes"
289         fi
290
291         if [ ! -z $1 ] && [ ! -e $sysfsdir/$attr ]; then
292                 _notrun "sysfs attribute '$attr' is not supported"
293         fi
294 }
295
296 # this test requires the xfs sparse inode feature
297 #
298 _require_xfs_sparse_inodes()
299 {
300         _scratch_mkfs_xfs_supported -m crc=1 -i sparse > /dev/null 2>&1 \
301                 || _notrun "mkfs.xfs does not support sparse inodes"
302         _scratch_mkfs_xfs -m crc=1 -i sparse > /dev/null 2>&1
303         _scratch_mount >/dev/null 2>&1 \
304                 || _notrun "kernel does not support sparse inodes"
305         _scratch_unmount
306 }
307
308 # check that xfs_db supports a specific command
309 _require_xfs_db_command()
310 {
311         if [ $# -ne 1 ]; then
312                 echo "Usage: _require_xfs_db_command command" 1>&2
313                 exit 1
314         fi
315         command=$1
316
317         _scratch_xfs_db -x -c "help" | grep $command > /dev/null || \
318                 _notrun "xfs_db $command support is missing"
319 }
320
321 # run xfs_check and friends on a FS.
322 _check_xfs_filesystem()
323 {
324         if [ $# -ne 3 ]; then
325                 echo "Usage: _check_xfs_filesystem device <logdev>|none <rtdev>|none" 1>&2
326                 exit 1
327         fi
328
329         extra_mount_options=""
330         extra_log_options=""
331         extra_options=""
332         device=$1
333         if [ -f $device ]; then
334                 extra_options="-f"
335         fi
336
337         if [ "$2" != "none" ]; then
338                 extra_log_options="-l$2"
339                 extra_mount_options="-ologdev=$2"
340         fi
341
342         if [ "$3" != "none" ]; then
343                 extra_rt_options="-r$3"
344                 extra_mount_options=$extra_mount_options" -ortdev=$3"
345         fi
346         extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"
347
348         [ "$FSTYP" != xfs ] && return 0
349
350         type=`_fs_type $device`
351         ok=1
352
353         if [ "$type" = "xfs" ]; then
354                 if [ -n "$TEST_XFS_SCRUB" ] && [ -x "$XFS_SCRUB_PROG" ]; then
355                         "$XFS_SCRUB_PROG" $scrubflag -vd $device >>$seqres.full
356                         if [ $? -ne 0 ]; then
357                                 echo "filesystem on $device failed scrub (see $seqres.full)"
358                                 ok=0
359                         fi
360                 fi
361                 # mounted ...
362                 mountpoint=`_umount_or_remount_ro $device`
363         fi
364
365         $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
366                 | tee $tmp.logprint | grep -q "<CLEAN>"
367         if [ $? -ne 0 -a "$HOSTOS" = "Linux" ]; then
368                 echo "_check_xfs_filesystem: filesystem on $device has dirty log (see $seqres.full)"
369
370                 echo "_check_xfs_filesystem: filesystem on $device has dirty log"   >>$seqres.full
371                 echo "*** xfs_logprint -t output ***"   >>$seqres.full
372                 cat $tmp.logprint                       >>$seqres.full
373                 echo "*** end xfs_logprint output"      >>$seqres.full
374
375                 ok=0
376         fi
377
378         # xfs_check runs out of memory on large files, so even providing the test
379         # option (-t) to avoid indexing the free space trees doesn't make it pass on
380         # large filesystems. Avoid it.
381         if [ "$LARGE_SCRATCH_DEV" != yes ]; then
382                 _xfs_check $extra_log_options $device 2>&1 |\
383                         _fix_malloc >$tmp.fs_check
384         fi
385         if [ -s $tmp.fs_check ]; then
386                 echo "_check_xfs_filesystem: filesystem on $device is inconsistent (c) (see $seqres.full)"
387
388                 echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
389                 echo "*** xfs_check output ***"         >>$seqres.full
390                 cat $tmp.fs_check                       >>$seqres.full
391                 echo "*** end xfs_check output"         >>$seqres.full
392
393                 ok=0
394         fi
395
396         $XFS_REPAIR_PROG -n $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
397         if [ $? -ne 0 ]; then
398                 echo "_check_xfs_filesystem: filesystem on $device is inconsistent (r) (see $seqres.full)"
399
400                 echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
401                 echo "*** xfs_repair -n output ***"     >>$seqres.full
402                 cat $tmp.repair | _fix_malloc           >>$seqres.full
403                 echo "*** end xfs_repair output"        >>$seqres.full
404
405                 ok=0
406         fi
407         rm -f $tmp.fs_check $tmp.logprint $tmp.repair
408
409         # Optionally test the index rebuilding behavior.
410         if [ -n "$TEST_XFS_REPAIR_REBUILD" ]; then
411                 $XFS_REPAIR_PROG $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
412                 if [ $? -ne 0 ]; then
413                         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild) (see $seqres.full)"
414
415                         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild)" >>$seqres.full
416                         echo "*** xfs_repair output ***"        >>$seqres.full
417                         cat $tmp.repair | _fix_malloc           >>$seqres.full
418                         echo "*** end xfs_repair output"        >>$seqres.full
419
420                         ok=0
421                 fi
422                 rm -f $tmp.repair
423
424                 $XFS_REPAIR_PROG -n $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
425                 if [ $? -ne 0 ]; then
426                         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild-reverify) (see $seqres.full)"
427
428                         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild-reverify)" >>$seqres.full
429                         echo "*** xfs_repair -n output ***"     >>$seqres.full
430                         cat $tmp.repair | _fix_malloc           >>$seqres.full
431                         echo "*** end xfs_repair output"        >>$seqres.full
432
433                         ok=0
434                 fi
435                 rm -f $tmp.repair
436         fi
437
438         if [ $ok -eq 0 ]; then
439                 echo "*** mount output ***"             >>$seqres.full
440                 _mount                                  >>$seqres.full
441                 echo "*** end mount output"             >>$seqres.full
442         elif [ "$type" = "xfs" ]; then
443                 _mount_or_remount_rw "$extra_mount_options" $device $mountpoint
444         fi
445
446         if [ $ok -eq 0 ]; then
447                 status=1
448                 if [ "$iam" != "check" ]; then
449                         exit 1
450                 fi
451                 return 1
452         fi
453
454         return 0
455 }
456
457 _check_xfs_test_fs()
458 {
459         TEST_LOG="none"
460         TEST_RT="none"
461         [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
462                 TEST_LOG="$TEST_LOGDEV"
463
464         [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
465                 TEST_RT="$TEST_RTDEV"
466
467         _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT
468
469         # check for ipath consistency
470         if $XFS_GROWFS_PROG -n $TEST_DIR | grep -q 'inode-paths=1'; then
471                 # errors go to stderr
472                 xfs_check_ipaths $TEST_DIR >/dev/null
473                 xfs_repair_ipaths -n $TEST_DIR >/dev/null
474         fi
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=2s >/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         _scratch_mount >/dev/null 2>&1 \
608            || _notrun "Kernel doesn't support meta_uuid feature"
609         _scratch_unmount
610 }