common/btrfs: source module file and remove duplicates
[xfstests-dev.git] / common / btrfs
1 #
2 # Common btrfs specific functions
3 #
4
5 . common/module
6
7 _btrfs_get_subvolid()
8 {
9         mnt=$1
10         name=$2
11
12         $BTRFS_UTIL_PROG sub list $mnt | egrep "\s$name$" | awk '{ print $2 }'
13 }
14
15 # _require_btrfs_command <command> [<subcommand>|<option>]
16 # We check for btrfs and (optionally) features of the btrfs command
17 # This function support both subfunction like "inspect-internal dump-tree" and
18 # options like "check --qgroup-report", and also subfunction options like
19 # "subvolume delete --subvolid"
20 _require_btrfs_command()
21 {
22         local cmd=$1
23         local param=$2
24         local param_arg=$3
25         local safe_param
26
27         _require_command "$BTRFS_UTIL_PROG" btrfs
28         if [ -z "$1" ]; then
29                 return 1;
30         fi
31         $BTRFS_UTIL_PROG $cmd --help &> /dev/null
32         [ $? -eq 0 ] || _notrun "$BTRFS_UTIL_PROG too old (must support $cmd)"
33
34         test -z "$param" && return
35
36         # If $param is an option, replace leading "-"s for grep
37         if [ ${param:0:1} == "-" ]; then
38                 safe_param=$(echo $param | sed 's/^-*//')
39                 $BTRFS_UTIL_PROG $cmd --help | grep -wq $safe_param || \
40                         _notrun "$BTRFS_UTIL_PROG too old (must support $cmd $param)"
41                 return
42         fi
43
44         $BTRFS_UTIL_PROG $cmd $param --help &> /dev/null
45         [ $? -eq 0 ] || _notrun "$BTRFS_UTIL_PROG too old (must support $cmd $param)"
46
47         test -z "$param_arg" && return
48
49         # replace leading "-"s for grep
50         safe_param=$(echo $param_arg | sed 's/^-*//')
51         $BTRFS_UTIL_PROG $cmd $param --help | grep -wq $safe_param || \
52                 _notrun "$BTRFS_UTIL_PROG too old (must support $cmd $param $param_arg)"
53 }
54
55 # Require extra check on btrfs qgroup numbers
56 _require_btrfs_qgroup_report()
57 {
58         _require_btrfs_command check --qgroup-report
59         touch ${RESULT_DIR}/require_scratch.require_qgroup_report
60 }
61
62 _require_btrfs_dump_super()
63 {
64         if [ ! -x "$BTRFS_SHOW_SUPER_PROG" ]; then
65                 _require_command "$BTRFS_UTIL_PROG" btrfs
66                 if ! $BTRFS_UTIL_PROG inspect-internal dump-super --help >& /dev/null; then
67                         _notrun "Missing btrfs-show-super or inspect-internal dump-super"
68                 fi
69                 BTRFS_SHOW_SUPER_PROG="$BTRFS_UTIL_PROG inspect-internal dump-super"
70         fi
71 }
72
73 _run_btrfs_util_prog()
74 {
75         run_check $BTRFS_UTIL_PROG $*
76 }
77
78 _require_btrfs_mkfs_feature()
79 {
80         if [ -z $1 ]; then
81                 echo "Missing feature name argument for _require_btrfs_mkfs_feature"
82                 exit 1
83         fi
84         feat=$1
85         $MKFS_BTRFS_PROG -O list-all 2>&1 | \
86                 grep '^[ \t]*'"$feat"'\b' > /dev/null 2>&1
87         [ $? -eq 0 ] || \
88                 _notrun "Feature $feat not supported in the available version of mkfs.btrfs"
89 }
90
91 _require_btrfs_fs_feature()
92 {
93         if [ -z $1 ]; then
94                 echo "Missing feature name argument for _require_btrfs_fs_feature"
95                 exit 1
96         fi
97         feat=$1
98         modprobe btrfs > /dev/null 2>&1
99         [ -e /sys/fs/btrfs/features/$feat ] || \
100                 _notrun "Feature $feat not supported by the available btrfs version"
101
102         if [ $feat = "raid56" ]; then
103                 # Zoned btrfs only supports SINGLE profile
104                 _require_non_zoned_device "${SCRATCH_DEV}"
105         fi
106 }
107
108 _require_btrfs_fs_sysfs()
109 {
110         modprobe btrfs > /dev/null 2>&1
111         [ -e /sys/fs/btrfs/features ] || \
112                 _notrun "Sysfs not supported by the available btrfs version"
113
114 }
115
116 _check_btrfs_filesystem()
117 {
118         device=$1
119
120         # If type is set, we're mounted
121         type=`_fs_type $device`
122         ok=1
123
124         if [ "$type" = "$FSTYP" ]; then
125                 # mounted ...
126                 mountpoint=`_umount_or_remount_ro $device`
127         fi
128
129         if [ -f ${RESULT_DIR}/require_scratch.require_qgroup_report ]; then
130                 $BTRFS_UTIL_PROG check $device --qgroup-report > $tmp.qgroup_report 2>&1
131                 if grep -qE "Counts for qgroup.*are different" $tmp.qgroup_report ; then
132                         _log_err "_check_btrfs_filesystem: filesystem on $device has wrong qgroup numbers"
133                         echo "*** qgroup_report.$FSTYP output ***"      >>$seqres.full
134                         cat $tmp.qgroup_report                          >>$seqres.full
135                         echo "*** qgroup_report.$FSTYP output ***"      >>$seqres.full
136                 fi
137                 rm -f $tmp.qgroup_report
138         fi
139
140         $BTRFS_UTIL_PROG check $device >$tmp.fsck 2>&1
141         if [ $? -ne 0 ]; then
142                 _log_err "_check_btrfs_filesystem: filesystem on $device is inconsistent"
143                 echo "*** fsck.$FSTYP output ***"       >>$seqres.full
144                 cat $tmp.fsck                           >>$seqres.full
145                 echo "*** end fsck.$FSTYP output"       >>$seqres.full
146
147                 ok=0
148         fi
149         rm -f $tmp.fsck
150
151         if [ $ok -eq 0 ]; then
152                 echo "*** mount output ***"             >>$seqres.full
153                 _mount                                  >>$seqres.full
154                 echo "*** end mount output"             >>$seqres.full
155         elif [ "$type" = "$FSTYP" ]; then
156                 # was mounted ...
157                 _mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
158                 ok=$?
159         fi
160
161         if [ $ok -eq 0 ]; then
162                 status=1
163                 if [ "$iam" != "check" ]; then
164                         exit 1
165                 fi
166                 return 1
167         fi
168
169         return 0
170 }
171
172 _require_btrfs_dev_del_by_devid()
173 {
174         $BTRFS_UTIL_PROG device delete --help | egrep devid > /dev/null 2>&1
175         [ $? -eq 0 ] || _notrun "$BTRFS_UTIL_PROG too old "\
176                         "(must support 'btrfs device delete <devid> /<mnt>')"
177 }
178
179 # get btrfs profile configs being tested
180 #
181 # A set of pre-set profile configs are exported via _btrfs_profile_configs
182 # array. Default configs can be overridden by setting BTRFS_PROFILE_CONFIGS
183 # var in the format "metadata_profile:data_profile", multiple configs can be
184 # seperated by space, e.g.
185 # export BTRFS_PROFILE_CONFIGS="raid0:raid0 raid1:raid1 dup:single"
186 _btrfs_get_profile_configs()
187 {
188         if [ "$FSTYP" != "btrfs" ]; then
189                 return
190         fi
191
192         if [ -z "$BTRFS_PROFILE_CONFIGS" ]; then
193                 # Default configurations to test.
194                 local configs=(
195                         "single:single"
196                         "dup:single"
197                         "raid0:raid0"
198                         "raid1:raid0"
199                         "raid1:raid1"
200                         "raid10:raid10"
201                         "raid5:raid5"
202                         "raid6:raid6"
203                 )
204         else
205                 # User-provided configurations.
206                 local configs=(${BTRFS_PROFILE_CONFIGS[@]})
207         fi
208
209         _btrfs_profile_configs=()
210         for cfg in "${configs[@]}"; do
211                 local supported=true
212                 local profiles=(${cfg/:/ })
213                 if [ "$1" == "replace" ]; then
214                         # We can't do replace with these profiles because they
215                         # imply only one device ($SCRATCH_DEV), and we need to
216                         # keep $SCRATCH_DEV around for _scratch_mount
217                         # and _check_scratch_fs.
218                         local unsupported=(
219                                 "dup"
220                         )
221                 elif [ "$1" == "replace-missing" ]; then
222                         # We can't replace missing devices with these profiles
223                         # because there isn't enough redundancy.
224                         local unsupported=(
225                                 "single"
226                                 "dup"
227                                 "raid0"
228                         )
229                 else
230                         local unsupported=()
231                 fi
232
233                 if _scratch_btrfs_is_zoned; then
234                         # Zoned btrfs only supports SINGLE profile
235                         unsupported+=(
236                                 "dup"
237                                 "raid0"
238                                 "raid1"
239                                 "raid1c3"
240                                 "raid1c4"
241                                 "raid10"
242                                 "raid5"
243                                 "raid6"
244                         )
245                 fi
246
247                 for unsupp in "${unsupported[@]}"; do
248                         if [ "${profiles[0]}" == "$unsupp" -o "${profiles[1]}" == "$unsupp" ]; then
249                              if [ -z "$BTRFS_PROFILE_CONFIGS" ]; then
250                                      # For the default config, just omit it.
251                                      supported=false
252                              else
253                                      # For user-provided config, don't run the test.
254                                      _notrun "Profile $unsupp not supported for $1"
255                              fi
256                         fi
257                 done
258                 if "$supported"; then
259                         _btrfs_profile_configs+=("-m ${profiles[0]} -d ${profiles[1]}")
260                 fi
261         done
262         export _btrfs_profile_configs
263 }
264
265 # stress btrfs by running balance operation in a loop
266 _btrfs_stress_balance()
267 {
268         local options=$@
269         while true; do
270                 _run_btrfs_balance_start $options >> $seqres.full
271         done
272 }
273
274 # stress btrfs by creating/mounting/umounting/deleting subvolume in a loop
275 _btrfs_stress_subvolume()
276 {
277         local btrfs_dev=$1
278         local btrfs_mnt=$2
279         local subvol_name=$3
280         local subvol_mnt=$4
281         local stop_file=$5
282
283         mkdir -p $subvol_mnt
284         while [ ! -e $stop_file ]; do
285                 $BTRFS_UTIL_PROG subvolume create $btrfs_mnt/$subvol_name
286                 $MOUNT_PROG -o subvol=$subvol_name $btrfs_dev $subvol_mnt
287                 $UMOUNT_PROG $subvol_mnt
288                 $BTRFS_UTIL_PROG subvolume delete $btrfs_mnt/$subvol_name
289         done
290 }
291
292 # stress btrfs by running scrub in a loop
293 _btrfs_stress_scrub()
294 {
295         local btrfs_mnt=$1
296         while true; do
297                 $BTRFS_UTIL_PROG scrub start -B $btrfs_mnt
298         done
299 }
300
301 # stress btrfs by defragmenting every file/dir in a loop and compress file
302 # contents while defragmenting if second argument is not "nocompress"
303 _btrfs_stress_defrag()
304 {
305         local btrfs_mnt=$1
306         local compress=$2
307
308         while true; do
309                 if [ "$compress" == "nocompress" ]; then
310                         find $btrfs_mnt \( -type f -o -type d \) -exec \
311                         $BTRFS_UTIL_PROG filesystem defrag {} \;
312                 else
313                         find $btrfs_mnt \( -type f -o -type d \) -exec \
314                         $BTRFS_UTIL_PROG filesystem defrag -clzo {} \;
315                         find $btrfs_mnt \( -type f -o -type d \) -exec \
316                         $BTRFS_UTIL_PROG filesystem defrag -czlib {} \;
317                 fi
318         done
319 }
320
321 # stress btrfs by remounting it with different compression algorithms in a loop
322 # run this with fsstress running at background could exercise the compression
323 # code path and ensure no race when switching compression algorithm with constant
324 # I/O activity.
325 _btrfs_stress_remount_compress()
326 {
327         local btrfs_mnt=$1
328         while true; do
329                 for algo in no zlib lzo; do
330                         $MOUNT_PROG -o remount,compress=$algo $btrfs_mnt
331                 done
332         done
333 }
334
335 # stress btrfs by replacing devices in a loop
336 # Note that at least 3 devices are needed in SCRATCH_DEV_POOL and the last
337 # device should be free(not used by btrfs)
338 _btrfs_stress_replace()
339 {
340         local btrfs_mnt=$1
341
342         # The device number in SCRATCH_DEV_POOL should be at least 3,
343         # one is SCRATCH_DEV, one is to be replaced, one is free device
344         # we won't replace SCRATCH_DEV, see below for reason
345         if [ "`echo $SCRATCH_DEV_POOL | wc -w`" -lt 3 ]; then
346                 echo "_btrfs_stress_replace requires at least 3 devices in SCRATCH_DEV_POOL"
347                 return
348         fi
349
350         # take the last device as the first free_dev
351         local free_dev="`echo $SCRATCH_DEV_POOL | $AWK_PROG '{print $NF}'`"
352
353         # free_dev should be really free
354         if $BTRFS_UTIL_PROG filesystem show $btrfs_mnt | grep -q "$free_dev"; then
355                 echo "_btrfs_stress_replace: $free_dev is used by btrfs"
356                 return
357         fi
358
359         # dev_pool is device list being currently used by btrfs (excluding SCRATCH_DEV)
360         # and can be replaced. We don't replace SCRATCH_DEV because it will be used in
361         # _scratch_mount and _check_scratch_fs etc.
362         local dev_pool=`echo $SCRATCH_DEV_POOL | sed -e "s# *$SCRATCH_DEV *##" \
363                         -e "s# *$free_dev *##"`
364
365         # set the first device in dev_pool as the first src_dev to be replaced
366         local src_dev=`echo $dev_pool | $AWK_PROG '{print $1}'`
367
368         echo "dev_pool=$dev_pool"
369         echo "free_dev=$free_dev, src_dev=$src_dev"
370         while true; do
371                 echo "Replacing $src_dev with $free_dev"
372                 $BTRFS_UTIL_PROG replace start -fB $src_dev $free_dev $btrfs_mnt
373                 if [ $? -ne 0 ]; then
374                         # don't update src_dev and free_dev if replace failed
375                         continue
376                 fi
377                 dev_pool="$dev_pool $free_dev"
378                 dev_pool=`echo $dev_pool | sed -e "s# *$src_dev *##"`
379                 free_dev=$src_dev
380                 src_dev=`echo $dev_pool | $AWK_PROG '{print $1}'`
381         done
382 }
383
384 # find the right option to force output in bytes, older versions of btrfs-progs
385 # print that by default, newer print human readable numbers with unit suffix
386 _btrfs_qgroup_units()
387 {
388         $BTRFS_UTIL_PROG qgroup show --help 2>&1 | grep -q -- --raw && echo "--raw"
389 }
390
391 _btrfs_compression_algos()
392 {
393         echo zlib
394         for feature in /sys/fs/btrfs/features/compress_*; do
395                 echo "${feature#/sys/fs/btrfs/features/compress_}"
396         done
397 }
398
399 # run btrfs balance start with required --full-balance if available.
400 _run_btrfs_balance_start()
401 {
402         local bal_opt=""
403
404         $BTRFS_UTIL_PROG balance start --help | grep -q "full-balance"
405         (( $? == 0 )) && bal_opt="--full-balance"
406
407         $BTRFS_UTIL_PROG balance start $bal_opt $*
408 }
409
410 #return the sector size of the btrfs scratch fs
411 _scratch_btrfs_sectorsize()
412 {
413         $BTRFS_UTIL_PROG inspect-internal dump-super $SCRATCH_DEV |\
414                 grep sectorsize | awk '{print $2}'
415 }
416
417 _btrfs_supports_forget()
418 {
419         $BTRFS_UTIL_PROG device scan --help | grep -wq forget && \
420                 $BTRFS_UTIL_PROG device scan --forget > /dev/null 2>&1
421 }
422
423 _require_btrfs_forget_or_module_loadable()
424 {
425         _btrfs_supports_forget && return
426
427         _require_loadable_fs_module "btrfs"
428 }
429
430 _btrfs_forget_or_module_reload()
431 {
432         _btrfs_supports_forget && return
433
434         _reload_fs_module "btrfs"
435 }
436
437 # Test cases which utilized _btrfs_forget_or_module_reload() must call this
438 # to make sure TEST_DEV can still be mounted. As TEST_DEV can be part of a
439 # multi-device btrfs.
440 _btrfs_rescan_devices()
441 {
442         $BTRFS_UTIL_PROG device scan &> /dev/null
443 }
444
445 _scratch_btrfs_is_zoned()
446 {
447         [ `_zone_type ${SCRATCH_DEV}` != "none" ] && return 0
448         return 1
449 }
450
451 _require_btrfs_sysfs_fsid()
452 {
453         local fsid
454
455         fsid=$($BTRFS_UTIL_PROG filesystem show $TEST_DIR |grep uuid: |\
456                awk '{print $NF}')
457
458         # Check if the kernel has sysfs fsid support.
459         # Following kernel patch adds it:
460         #   btrfs: sysfs add devinfo/fsid to retrieve fsid from the device
461         test -f /sys/fs/btrfs/$fsid/devinfo/1/fsid ||\
462                 _notrun "Need btrfs sysfs fsid support"
463 }