2 # Common btrfs specific functions
10 $BTRFS_UTIL_PROG sub list $mnt | grep $name | awk '{ print $2 }'
13 # _require_btrfs_command <command> [<subcommand>|<option>]
14 # We check for btrfs and (optionally) features of the btrfs command
15 # It can both subfunction like "inspect-internal dump-tree" and
16 # options like "check --qgroup-report"
17 _require_btrfs_command()
23 _require_command "$BTRFS_UTIL_PROG" btrfs
27 $BTRFS_UTIL_PROG $cmd --help &> /dev/null
28 [ $? -eq 0 ] || _notrun "$BTRFS_UTIL_PROG too old (must support $cmd)"
30 test -z "$param" && return
32 # If $param is an option, replace leading "-"s for grep
33 if [ ${param:0:1} == "-" ]; then
34 safe_param=$(echo $param | sed 's/^-*//')
35 $BTRFS_UTIL_PROG $cmd --help | grep -wq $safe_param || \
36 _notrun "$BTRFS_UTIL_PROG too old (must support $cmd $param)"
40 $BTRFS_UTIL_PROG $cmd $param --help &> /dev/null
41 [ $? -eq 0 ] || _notrun "$BTRFS_UTIL_PROG too old (must support $cmd $param)"
44 # Require extra check on btrfs qgroup numbers
45 _require_btrfs_qgroup_report()
47 _require_btrfs_command check --qgroup-report
48 touch ${RESULT_DIR}/require_scratch.require_qgroup_report
51 _require_btrfs_dump_super()
53 if [ ! -x "$BTRFS_SHOW_SUPER_PROG" ]; then
54 _require_command "$BTRFS_UTIL_PROG" btrfs
55 if ! $BTRFS_UTIL_PROG inspect-internal dump-super --help >& /dev/null; then
56 _notrun "Missing btrfs-show-super or inspect-internal dump-super"
58 BTRFS_SHOW_SUPER_PROG="$BTRFS_UTIL_PROG inspect-internal dump-super"
62 _run_btrfs_util_prog()
64 run_check $BTRFS_UTIL_PROG $*
67 _require_btrfs_mkfs_feature()
70 echo "Missing feature name argument for _require_btrfs_mkfs_feature"
74 $MKFS_BTRFS_PROG -O list-all 2>&1 | \
75 grep '^[ \t]*'"$feat"'\b' > /dev/null 2>&1
77 _notrun "Feature $feat not supported in the available version of mkfs.btrfs"
80 _require_btrfs_fs_feature()
83 echo "Missing feature name argument for _require_btrfs_fs_feature"
87 modprobe btrfs > /dev/null 2>&1
88 [ -e /sys/fs/btrfs/features/$feat ] || \
89 _notrun "Feature $feat not supported by the available btrfs version"
92 _check_btrfs_filesystem()
96 # If type is set, we're mounted
97 type=`_fs_type $device`
100 if [ "$type" = "$FSTYP" ]; then
102 mountpoint=`_umount_or_remount_ro $device`
105 if [ -f ${RESULT_DIR}/require_scratch.require_qgroup_report ]; then
106 $BTRFS_UTIL_PROG check $device --qgroup-report > $tmp.qgroup_report 2>&1
107 if grep -qE "Counts for qgroup.*are different" $tmp.qgroup_report ; then
108 _log_err "_check_btrfs_filesystem: filesystem on $device has wrong qgroup numbers"
109 echo "*** qgroup_report.$FSTYP output ***" >>$seqres.full
110 cat $tmp.qgroup_report >>$seqres.full
111 echo "*** qgroup_report.$FSTYP output ***" >>$seqres.full
113 rm -f $tmp.qgroup_report
116 $BTRFS_UTIL_PROG check $device >$tmp.fsck 2>&1
117 if [ $? -ne 0 ]; then
118 _log_err "_check_btrfs_filesystem: filesystem on $device is inconsistent"
119 echo "*** fsck.$FSTYP output ***" >>$seqres.full
120 cat $tmp.fsck >>$seqres.full
121 echo "*** end fsck.$FSTYP output" >>$seqres.full
127 if [ $ok -eq 0 ]; then
128 echo "*** mount output ***" >>$seqres.full
129 _mount >>$seqres.full
130 echo "*** end mount output" >>$seqres.full
131 elif [ "$type" = "$FSTYP" ]; then
133 _mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
137 if [ $ok -eq 0 ]; then
139 if [ "$iam" != "check" ]; then
148 _require_btrfs_dev_del_by_devid()
150 $BTRFS_UTIL_PROG device delete --help | egrep devid > /dev/null 2>&1
151 [ $? -eq 0 ] || _notrun "$BTRFS_UTIL_PROG too old "\
152 "(must support 'btrfs device delete <devid> /<mnt>')"
155 # get btrfs profile configs being tested
157 # A set of pre-set profile configs are exported via _btrfs_profile_configs
158 # array. Default configs can be overridden by setting BTRFS_PROFILE_CONFIGS
159 # var in the format "metadata_profile:data_profile", multiple configs can be
160 # seperated by space, e.g.
161 # export BTRFS_PROFILE_CONFIGS="raid0:raid0 raid1:raid1 dup:single"
162 _btrfs_get_profile_configs()
164 if [ "$FSTYP" != "btrfs" ]; then
168 if [ -z "$BTRFS_PROFILE_CONFIGS" ]; then
169 # Default configurations to test.
181 # User-provided configurations.
182 local configs=(${BTRFS_PROFILE_CONFIGS[@]})
185 _btrfs_profile_configs=()
186 for cfg in "${configs[@]}"; do
188 local profiles=(${cfg/:/ })
189 if [ "$1" == "replace" ]; then
190 # We can't do replace with these profiles because they
191 # imply only one device ($SCRATCH_DEV), and we need to
192 # keep $SCRATCH_DEV around for _scratch_mount
193 # and _check_scratch_fs.
197 elif [ "$1" == "replace-missing" ]; then
198 # We can't replace missing devices with these profiles
199 # because there isn't enough redundancy.
208 for unsupp in "${unsupported[@]}"; do
209 if [ "${profiles[0]}" == "$unsupp" -o "${profiles[1]}" == "$unsupp" ]; then
210 if [ -z "$BTRFS_PROFILE_CONFIGS" ]; then
211 # For the default config, just omit it.
214 # For user-provided config, don't run the test.
215 _notrun "Profile $unsupp not supported for $1"
219 if "$supported"; then
220 _btrfs_profile_configs+=("-m ${profiles[0]} -d ${profiles[1]}")
223 export _btrfs_profile_configs
226 # stress btrfs by running balance operation in a loop
227 _btrfs_stress_balance()
231 $BTRFS_UTIL_PROG balance start $options
235 # stress btrfs by creating/mounting/umounting/deleting subvolume in a loop
236 _btrfs_stress_subvolume()
245 while [ ! -e $stop_file ]; do
246 $BTRFS_UTIL_PROG subvolume create $btrfs_mnt/$subvol_name
247 $MOUNT_PROG -o subvol=$subvol_name $btrfs_dev $subvol_mnt
248 $UMOUNT_PROG $subvol_mnt
249 $BTRFS_UTIL_PROG subvolume delete $btrfs_mnt/$subvol_name
253 # stress btrfs by running scrub in a loop
254 _btrfs_stress_scrub()
258 $BTRFS_UTIL_PROG scrub start -B $btrfs_mnt
262 # stress btrfs by defragmenting every file/dir in a loop and compress file
263 # contents while defragmenting if second argument is not "nocompress"
264 _btrfs_stress_defrag()
270 if [ "$compress" == "nocompress" ]; then
271 find $btrfs_mnt \( -type f -o -type d \) -exec \
272 $BTRFS_UTIL_PROG filesystem defrag {} \;
274 find $btrfs_mnt \( -type f -o -type d \) -exec \
275 $BTRFS_UTIL_PROG filesystem defrag -clzo {} \;
276 find $btrfs_mnt \( -type f -o -type d \) -exec \
277 $BTRFS_UTIL_PROG filesystem defrag -czlib {} \;
282 # stress btrfs by remounting it with different compression algorithms in a loop
283 # run this with fsstress running at background could exercise the compression
284 # code path and ensure no race when switching compression algorithm with constant
286 _btrfs_stress_remount_compress()
290 for algo in no zlib lzo; do
291 $MOUNT_PROG -o remount,compress=$algo $btrfs_mnt
296 # stress btrfs by replacing devices in a loop
297 # Note that at least 3 devices are needed in SCRATCH_DEV_POOL and the last
298 # device should be free(not used by btrfs)
299 _btrfs_stress_replace()
303 # The device number in SCRATCH_DEV_POOL should be at least 3,
304 # one is SCRATCH_DEV, one is to be replaced, one is free device
305 # we won't replace SCRATCH_DEV, see below for reason
306 if [ "`echo $SCRATCH_DEV_POOL | wc -w`" -lt 3 ]; then
307 echo "_btrfs_stress_replace requires at least 3 devices in SCRATCH_DEV_POOL"
311 # take the last device as the first free_dev
312 local free_dev="`echo $SCRATCH_DEV_POOL | $AWK_PROG '{print $NF}'`"
314 # free_dev should be really free
315 if $BTRFS_UTIL_PROG filesystem show $btrfs_mnt | grep -q "$free_dev"; then
316 echo "_btrfs_stress_replace: $free_dev is used by btrfs"
320 # dev_pool is device list being currently used by btrfs (excluding SCRATCH_DEV)
321 # and can be replaced. We don't replace SCRATCH_DEV because it will be used in
322 # _scratch_mount and _check_scratch_fs etc.
323 local dev_pool=`echo $SCRATCH_DEV_POOL | sed -e "s# *$SCRATCH_DEV *##" \
324 -e "s# *$free_dev *##"`
326 # set the first device in dev_pool as the first src_dev to be replaced
327 local src_dev=`echo $dev_pool | $AWK_PROG '{print $1}'`
329 echo "dev_pool=$dev_pool"
330 echo "free_dev=$free_dev, src_dev=$src_dev"
332 echo "Replacing $src_dev with $free_dev"
333 $BTRFS_UTIL_PROG replace start -fB $src_dev $free_dev $btrfs_mnt
334 if [ $? -ne 0 ]; then
335 # don't update src_dev and free_dev if replace failed
338 dev_pool="$dev_pool $free_dev"
339 dev_pool=`echo $dev_pool | sed -e "s# *$src_dev *##"`
341 src_dev=`echo $dev_pool | $AWK_PROG '{print $1}'`
345 # find the right option to force output in bytes, older versions of btrfs-progs
346 # print that by default, newer print human readable numbers with unit suffix
347 _btrfs_qgroup_units()
349 $BTRFS_UTIL_PROG qgroup show --help 2>&1 | grep -q -- --raw && echo "--raw"
352 _btrfs_compression_algos()
355 for feature in /sys/fs/btrfs/features/compress_*; do
356 echo "${feature#/sys/fs/btrfs/features/compress_}"
360 # run btrfs balance start with required --full-balance if available.
361 _run_btrfs_balance_start()
365 $BTRFS_UTIL_PROG balance start --help | grep -q "full-balance"
366 (( $? == 0 )) && bal_opt="--full-balance"
368 run_check $BTRFS_UTIL_PROG balance start $bal_opt $*