2 # Common btrfs specific functions
10 $BTRFS_UTIL_PROG sub list $mnt | egrep "\s$name$" | awk '{ print $2 }'
13 # _require_btrfs_command <command> [<subcommand>|<option>]
14 # We check for btrfs and (optionally) features of the btrfs command
15 # This function support both subfunction like "inspect-internal dump-tree" and
16 # options like "check --qgroup-report", and also subfunction options like
17 # "subvolume delete --subvolid"
18 _require_btrfs_command()
25 _require_command "$BTRFS_UTIL_PROG" btrfs
29 $BTRFS_UTIL_PROG $cmd --help &> /dev/null
30 [ $? -eq 0 ] || _notrun "$BTRFS_UTIL_PROG too old (must support $cmd)"
32 test -z "$param" && return
34 # If $param is an option, replace leading "-"s for grep
35 if [ ${param:0:1} == "-" ]; then
36 safe_param=$(echo $param | sed 's/^-*//')
37 $BTRFS_UTIL_PROG $cmd --help | grep -wq $safe_param || \
38 _notrun "$BTRFS_UTIL_PROG too old (must support $cmd $param)"
42 $BTRFS_UTIL_PROG $cmd $param --help &> /dev/null
43 [ $? -eq 0 ] || _notrun "$BTRFS_UTIL_PROG too old (must support $cmd $param)"
45 test -z "$param_arg" && return
47 # replace leading "-"s for grep
48 safe_param=$(echo $param_arg | sed 's/^-*//')
49 $BTRFS_UTIL_PROG $cmd $param --help | grep -wq $safe_param || \
50 _notrun "$BTRFS_UTIL_PROG too old (must support $cmd $param $param_arg)"
53 # Require extra check on btrfs qgroup numbers
54 _require_btrfs_qgroup_report()
56 _require_btrfs_command check --qgroup-report
57 touch ${RESULT_DIR}/require_scratch.require_qgroup_report
60 _require_btrfs_dump_super()
62 if [ ! -x "$BTRFS_SHOW_SUPER_PROG" ]; then
63 _require_command "$BTRFS_UTIL_PROG" btrfs
64 if ! $BTRFS_UTIL_PROG inspect-internal dump-super --help >& /dev/null; then
65 _notrun "Missing btrfs-show-super or inspect-internal dump-super"
67 BTRFS_SHOW_SUPER_PROG="$BTRFS_UTIL_PROG inspect-internal dump-super"
71 _run_btrfs_util_prog()
73 run_check $BTRFS_UTIL_PROG $*
76 _require_btrfs_mkfs_feature()
79 echo "Missing feature name argument for _require_btrfs_mkfs_feature"
83 $MKFS_BTRFS_PROG -O list-all 2>&1 | \
84 grep '^[ \t]*'"$feat"'\b' > /dev/null 2>&1
86 _notrun "Feature $feat not supported in the available version of mkfs.btrfs"
89 _require_btrfs_fs_feature()
92 echo "Missing feature name argument for _require_btrfs_fs_feature"
96 modprobe btrfs > /dev/null 2>&1
97 [ -e /sys/fs/btrfs/features/$feat ] || \
98 _notrun "Feature $feat not supported by the available btrfs version"
101 _require_btrfs_fs_sysfs()
103 modprobe btrfs > /dev/null 2>&1
104 [ -e /sys/fs/btrfs/features ] || \
105 _notrun "Sysfs not supported by the available btrfs version"
109 _check_btrfs_filesystem()
113 # If type is set, we're mounted
114 type=`_fs_type $device`
117 if [ "$type" = "$FSTYP" ]; then
119 mountpoint=`_umount_or_remount_ro $device`
122 if [ -f ${RESULT_DIR}/require_scratch.require_qgroup_report ]; then
123 $BTRFS_UTIL_PROG check $device --qgroup-report > $tmp.qgroup_report 2>&1
124 if grep -qE "Counts for qgroup.*are different" $tmp.qgroup_report ; then
125 _log_err "_check_btrfs_filesystem: filesystem on $device has wrong qgroup numbers"
126 echo "*** qgroup_report.$FSTYP output ***" >>$seqres.full
127 cat $tmp.qgroup_report >>$seqres.full
128 echo "*** qgroup_report.$FSTYP output ***" >>$seqres.full
130 rm -f $tmp.qgroup_report
133 $BTRFS_UTIL_PROG check $device >$tmp.fsck 2>&1
134 if [ $? -ne 0 ]; then
135 _log_err "_check_btrfs_filesystem: filesystem on $device is inconsistent"
136 echo "*** fsck.$FSTYP output ***" >>$seqres.full
137 cat $tmp.fsck >>$seqres.full
138 echo "*** end fsck.$FSTYP output" >>$seqres.full
144 if [ $ok -eq 0 ]; then
145 echo "*** mount output ***" >>$seqres.full
146 _mount >>$seqres.full
147 echo "*** end mount output" >>$seqres.full
148 elif [ "$type" = "$FSTYP" ]; then
150 _mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
154 if [ $ok -eq 0 ]; then
156 if [ "$iam" != "check" ]; then
165 _require_btrfs_dev_del_by_devid()
167 $BTRFS_UTIL_PROG device delete --help | egrep devid > /dev/null 2>&1
168 [ $? -eq 0 ] || _notrun "$BTRFS_UTIL_PROG too old "\
169 "(must support 'btrfs device delete <devid> /<mnt>')"
172 # get btrfs profile configs being tested
174 # A set of pre-set profile configs are exported via _btrfs_profile_configs
175 # array. Default configs can be overridden by setting BTRFS_PROFILE_CONFIGS
176 # var in the format "metadata_profile:data_profile", multiple configs can be
177 # seperated by space, e.g.
178 # export BTRFS_PROFILE_CONFIGS="raid0:raid0 raid1:raid1 dup:single"
179 _btrfs_get_profile_configs()
181 if [ "$FSTYP" != "btrfs" ]; then
185 if [ -z "$BTRFS_PROFILE_CONFIGS" ]; then
186 # Default configurations to test.
198 # User-provided configurations.
199 local configs=(${BTRFS_PROFILE_CONFIGS[@]})
202 _btrfs_profile_configs=()
203 for cfg in "${configs[@]}"; do
205 local profiles=(${cfg/:/ })
206 if [ "$1" == "replace" ]; then
207 # We can't do replace with these profiles because they
208 # imply only one device ($SCRATCH_DEV), and we need to
209 # keep $SCRATCH_DEV around for _scratch_mount
210 # and _check_scratch_fs.
214 elif [ "$1" == "replace-missing" ]; then
215 # We can't replace missing devices with these profiles
216 # because there isn't enough redundancy.
225 for unsupp in "${unsupported[@]}"; do
226 if [ "${profiles[0]}" == "$unsupp" -o "${profiles[1]}" == "$unsupp" ]; then
227 if [ -z "$BTRFS_PROFILE_CONFIGS" ]; then
228 # For the default config, just omit it.
231 # For user-provided config, don't run the test.
232 _notrun "Profile $unsupp not supported for $1"
236 if "$supported"; then
237 _btrfs_profile_configs+=("-m ${profiles[0]} -d ${profiles[1]}")
240 export _btrfs_profile_configs
243 # stress btrfs by running balance operation in a loop
244 _btrfs_stress_balance()
248 _run_btrfs_balance_start $options >> $seqres.full
252 # stress btrfs by creating/mounting/umounting/deleting subvolume in a loop
253 _btrfs_stress_subvolume()
262 while [ ! -e $stop_file ]; do
263 $BTRFS_UTIL_PROG subvolume create $btrfs_mnt/$subvol_name
264 $MOUNT_PROG -o subvol=$subvol_name $btrfs_dev $subvol_mnt
265 $UMOUNT_PROG $subvol_mnt
266 $BTRFS_UTIL_PROG subvolume delete $btrfs_mnt/$subvol_name
270 # stress btrfs by running scrub in a loop
271 _btrfs_stress_scrub()
275 $BTRFS_UTIL_PROG scrub start -B $btrfs_mnt
279 # stress btrfs by defragmenting every file/dir in a loop and compress file
280 # contents while defragmenting if second argument is not "nocompress"
281 _btrfs_stress_defrag()
287 if [ "$compress" == "nocompress" ]; then
288 find $btrfs_mnt \( -type f -o -type d \) -exec \
289 $BTRFS_UTIL_PROG filesystem defrag {} \;
291 find $btrfs_mnt \( -type f -o -type d \) -exec \
292 $BTRFS_UTIL_PROG filesystem defrag -clzo {} \;
293 find $btrfs_mnt \( -type f -o -type d \) -exec \
294 $BTRFS_UTIL_PROG filesystem defrag -czlib {} \;
299 # stress btrfs by remounting it with different compression algorithms in a loop
300 # run this with fsstress running at background could exercise the compression
301 # code path and ensure no race when switching compression algorithm with constant
303 _btrfs_stress_remount_compress()
307 for algo in no zlib lzo; do
308 $MOUNT_PROG -o remount,compress=$algo $btrfs_mnt
313 # stress btrfs by replacing devices in a loop
314 # Note that at least 3 devices are needed in SCRATCH_DEV_POOL and the last
315 # device should be free(not used by btrfs)
316 _btrfs_stress_replace()
320 # The device number in SCRATCH_DEV_POOL should be at least 3,
321 # one is SCRATCH_DEV, one is to be replaced, one is free device
322 # we won't replace SCRATCH_DEV, see below for reason
323 if [ "`echo $SCRATCH_DEV_POOL | wc -w`" -lt 3 ]; then
324 echo "_btrfs_stress_replace requires at least 3 devices in SCRATCH_DEV_POOL"
328 # take the last device as the first free_dev
329 local free_dev="`echo $SCRATCH_DEV_POOL | $AWK_PROG '{print $NF}'`"
331 # free_dev should be really free
332 if $BTRFS_UTIL_PROG filesystem show $btrfs_mnt | grep -q "$free_dev"; then
333 echo "_btrfs_stress_replace: $free_dev is used by btrfs"
337 # dev_pool is device list being currently used by btrfs (excluding SCRATCH_DEV)
338 # and can be replaced. We don't replace SCRATCH_DEV because it will be used in
339 # _scratch_mount and _check_scratch_fs etc.
340 local dev_pool=`echo $SCRATCH_DEV_POOL | sed -e "s# *$SCRATCH_DEV *##" \
341 -e "s# *$free_dev *##"`
343 # set the first device in dev_pool as the first src_dev to be replaced
344 local src_dev=`echo $dev_pool | $AWK_PROG '{print $1}'`
346 echo "dev_pool=$dev_pool"
347 echo "free_dev=$free_dev, src_dev=$src_dev"
349 echo "Replacing $src_dev with $free_dev"
350 $BTRFS_UTIL_PROG replace start -fB $src_dev $free_dev $btrfs_mnt
351 if [ $? -ne 0 ]; then
352 # don't update src_dev and free_dev if replace failed
355 dev_pool="$dev_pool $free_dev"
356 dev_pool=`echo $dev_pool | sed -e "s# *$src_dev *##"`
358 src_dev=`echo $dev_pool | $AWK_PROG '{print $1}'`
362 # find the right option to force output in bytes, older versions of btrfs-progs
363 # print that by default, newer print human readable numbers with unit suffix
364 _btrfs_qgroup_units()
366 $BTRFS_UTIL_PROG qgroup show --help 2>&1 | grep -q -- --raw && echo "--raw"
369 _btrfs_compression_algos()
372 for feature in /sys/fs/btrfs/features/compress_*; do
373 echo "${feature#/sys/fs/btrfs/features/compress_}"
377 # run btrfs balance start with required --full-balance if available.
378 _run_btrfs_balance_start()
382 $BTRFS_UTIL_PROG balance start --help | grep -q "full-balance"
383 (( $? == 0 )) && bal_opt="--full-balance"
385 $BTRFS_UTIL_PROG balance start $bal_opt $*
388 #return the sector size of the btrfs scratch fs
389 _scratch_btrfs_sectorsize()
391 $BTRFS_UTIL_PROG inspect-internal dump-super $SCRATCH_DEV |\
392 grep sectorsize | awk '{print $2}'
395 _btrfs_supports_forget()
397 $BTRFS_UTIL_PROG device scan --help | grep -wq forget && \
398 $BTRFS_UTIL_PROG device scan --forget > /dev/null 2>&1
401 _require_btrfs_forget_or_module_loadable()
403 _btrfs_supports_forget && return
405 _require_loadable_fs_module "btrfs"
408 _btrfs_forget_or_module_reload()
410 _btrfs_supports_forget && return
412 _reload_fs_module "btrfs"
415 # Test cases which utilized _btrfs_forget_or_module_reload() must call this
416 # to make sure TEST_DEV can still be mounted. As TEST_DEV can be part of a
417 # multi-device btrfs.
418 _btrfs_rescan_devices()
420 $BTRFS_UTIL_PROG device scan &> /dev/null