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