4 # The contents of this file are subject to the terms of the
5 # Common Development and Distribution License (the "License").
6 # You may not use this file except in compliance with the License.
8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 # or http://www.opensolaris.org/os/licensing.
10 # See the License for the specific language governing permissions
11 # and limitations under the License.
13 # When distributing Covered Code, include this CDDL HEADER in each
14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 # If applicable, add the following below this CDDL HEADER, with the
16 # fields enclosed by brackets "[]" replaced with your own identifying
17 # information: Portions Copyright [yyyy] [name of copyright owner]
23 # Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 # Use is subject to license terms.
28 # Copyright (c) 2013, 2016 by Delphix. All rights reserved.
31 . $STF_SUITE/include/libtest.shlib
32 . $STF_SUITE/include/math.shlib
33 . $STF_SUITE/tests/functional/rsend/rsend.cfg
36 # Set up test model which includes various datasets
46 # ||@final @final @snapC
47 # ||@snapC @snapC @snapB
48 # ||@snapA @snapB @snapA
51 # $pool -------- $FS ------- fs1 ------- fs2
53 # vol vol \____ \ @fsnap
55 # @init @vsnap | ------------ fclone
57 # @final @snapB \ | @init
58 # @snapC vclone @snapA
66 function setup_test_model
70 log_must zfs create -p $pool/$FS/fs1/fs2
72 log_must zfs snapshot $pool@psnap
73 log_must zfs clone $pool@psnap $pool/pclone
75 if is_global_zone ; then
76 log_must zfs create -V 16M $pool/vol
77 log_must zfs create -V 16M $pool/$FS/vol
80 log_must zfs snapshot $pool/$FS/vol@vsnap
81 log_must zfs clone $pool/$FS/vol@vsnap $pool/$FS/vclone
85 log_must snapshot_tree $pool/$FS/fs1/fs2@fsnap
86 log_must zfs clone $pool/$FS/fs1/fs2@fsnap $pool/$FS/fs1/fclone
87 log_must zfs snapshot -r $pool@init
89 log_must snapshot_tree $pool@snapA
90 log_must snapshot_tree $pool@snapC
91 log_must snapshot_tree $pool/pclone@snapB
92 log_must snapshot_tree $pool/$FS@snapB
93 log_must snapshot_tree $pool/$FS@snapC
94 log_must snapshot_tree $pool/$FS/fs1@snapA
95 log_must snapshot_tree $pool/$FS/fs1@snapB
96 log_must snapshot_tree $pool/$FS/fs1@snapC
97 log_must snapshot_tree $pool/$FS/fs1/fclone@snapA
99 if is_global_zone ; then
100 log_must zfs snapshot $pool/vol@snapA
101 log_must zfs snapshot $pool/$FS/vol@snapB
102 log_must zfs snapshot $pool/$FS/vol@snapC
103 log_must zfs snapshot $pool/$FS/vclone@snapC
106 log_must zfs snapshot -r $pool@final
112 # Cleanup the BACKDIR and given pool content and all the sub datasets
116 function cleanup_pool
119 log_must rm -rf $BACKDIR/*
121 if is_global_zone ; then
122 log_must_busy zfs destroy -Rf $pool
124 typeset list=$(zfs list -H -r -t all -o name $pool)
126 if [[ $ds != $pool ]] ; then
127 if datasetexists $ds ; then
128 log_must_busy zfs destroy -Rf $ds
134 typeset mntpnt=$(get_prop mountpoint $pool)
135 if ! ismounted $pool ; then
136 # Make sure mountpoint directory is empty
137 if [[ -d $mntpnt ]]; then
138 log_must rm -rf $mntpnt/*
141 log_must zfs mount $pool
143 if [[ -d $mntpnt ]]; then
150 function cleanup_pools
160 eval md5sum $file1 | awk '{ print $1 }' > $BACKDIR/md5_file1
161 eval md5sum $file2 | awk '{ print $1 }' > $BACKDIR/md5_file2
162 diff $BACKDIR/md5_file1 $BACKDIR/md5_file2
165 rm -f $BACKDIR/md5_file1 $BACKDIR/md5_file2
171 # Detect if the given two filesystems have same sub-datasets
173 # $1 source filesystem
174 # $2 destination filesystem
181 zfs list -r -H -t all -o name $src_fs > $BACKDIR/src1
182 zfs list -r -H -t all -o name $dst_fs > $BACKDIR/dst1
184 eval sed -e 's:^$src_fs:PREFIX:g' < $BACKDIR/src1 > $BACKDIR/src
185 eval sed -e 's:^$dst_fs:PREFIX:g' < $BACKDIR/dst1 > $BACKDIR/dst
187 diff $BACKDIR/src $BACKDIR/dst
190 rm -f $BACKDIR/src $BACKDIR/dst $BACKDIR/src1 $BACKDIR/dst1
196 # Compare all the directories and files in two filesystems
198 # $1 source filesystem
199 # $2 destination filesystem
206 typeset srcdir dstdir
207 srcdir=$(get_prop mountpoint $src_fs)
208 dstdir=$(get_prop mountpoint $dst_fs)
210 diff -r $srcdir $dstdir > /dev/null 2>&1
215 # Compare the given two dataset properties
225 for item in "type" "origin" "volblocksize" "acltype" "dnodesize" \
226 "atime" "canmount" "checksum" "compression" "copies" "devices" \
227 "exec" "quota" "readonly" "recordsize" "reservation" "setuid" \
228 "snapdir" "version" "volsize" "xattr" "zoned" "mountpoint";
230 zfs get -H -o property,value,source $item $dtst1 >> \
232 zfs get -H -o property,value,source $item $dtst2 >> \
236 eval sed -e 's:$dtst1:PREFIX:g' < $BACKDIR/dtst1 > $BACKDIR/dtst1
237 eval sed -e 's:$dtst2:PREFIX:g' < $BACKDIR/dtst2 > $BACKDIR/dtst2
239 diff $BACKDIR/dtst1 $BACKDIR/dtst2
242 rm -f $BACKDIR/dtst1 $BACKDIR/dtst2
249 # Random create directories and files
257 if [[ -d $dir ]]; then
264 ((nl = RANDOM % 6 + 1))
267 mktree -b $dir -l $nl -d $nd -f $nf
274 # Put data in filesystem and take snapshot
278 function snapshot_tree
281 typeset ds=${snap%%@*}
282 typeset type=$(get_prop "type" $ds)
285 if [[ $type == "filesystem" ]]; then
286 typeset mntpnt=$(get_prop mountpoint $ds)
289 if ((ret == 0)) ; then
290 eval random_tree $mntpnt/${snap##$ds}
295 if ((ret == 0)) ; then
304 # Destroy the given snapshot and stuff
308 function destroy_tree
312 for snap in "$@" ; do
313 log_must_busy zfs destroy $snap
315 typeset ds=${snap%%@*}
316 typeset type=$(get_prop "type" $ds)
317 if [[ $type == "filesystem" ]]; then
318 typeset mntpnt=$(get_prop mountpoint $ds)
319 if [[ -n $mntpnt ]]; then
329 # Get all the sub-datasets of give dataset with specific suffix
334 function getds_with_suffix
339 typeset list=$(zfs list -r -H -t all -o name $ds | grep "$suffix$")
345 # Output inherited properties whitch is edited for file system
347 function fs_inherit_prop
350 if is_global_zone ; then
351 fs_prop=$(zfs inherit 2>&1 | \
352 awk '$2=="YES" && $3=="YES" {print $1}')
353 if ! is_te_enabled ; then
354 fs_prop=$(echo $fs_prop | grep -v "mlslabel")
357 fs_prop=$(zfs inherit 2>&1 | \
358 awk '$2=="YES" && $3=="YES" {print $1}'|
359 egrep -v "devices|mlslabel|sharenfs|sharesmb|zoned")
366 # Output inherited properties for volume
368 function vol_inherit_prop
370 echo "checksum readonly"
374 # Get the destination dataset to compare
382 # If the srcfs is not pool
384 if ! zpool list $srcfs > /dev/null 2>&1 ; then
385 eval dstfs="$dstfs/${srcfs#*/}"
394 # $1 Number of files to create
395 # $2 Maximum file size
397 # $4 File system to create the files on
407 for ((i=0; i<$nfiles; i=i+1)); do
408 file_name="/$fs/file-$maxsize-$((i+$file_id_offset))"
409 file_size=$((($RANDOM * $RANDOM % ($maxsize - 1)) + 1))
412 # Create an interesting mix of files which contain both
413 # data blocks and holes for more realistic test coverage.
414 # Half the files are created as sparse then partially filled,
415 # the other half is dense then a hole is punched in the file.
417 if [ $((RANDOM % 2)) -eq 0 ]; then
418 truncate -s $file_size $file_name || \
419 log_fail "Failed to create $file_name"
420 dd if=/dev/urandom of=$file_name \
421 bs=$bs count=$(($file_size / 2 / $bs)) \
422 seek=$(($RANDOM % (($file_size / 2 / $bs) + 1))) \
423 conv=notrunc >/dev/null 2>&1 || \
424 log_fail "Failed to create $file_name"
426 dd if=/dev/urandom of=$file_name \
427 bs=$file_size count=1 >/dev/null 2>&1 || \
428 log_fail "Failed to create $file_name"
429 dd if=/dev/zero of=$file_name \
430 bs=$bs count=$(($file_size / 2 / $bs)) \
431 seek=$(($RANDOM % (($file_size / 2 / $bs) + 1))) \
432 conv=notrunc >/dev/null 2>&1 || \
433 log_fail "Failed to create $file_name"
436 echo Created $nfiles files of random sizes up to $maxsize bytes
442 # $1 Number of files to remove
443 # $2 Maximum file size
445 # $4 File system to remove the files from
454 for ((i=0; i<$nfiles; i=i+1)); do
455 rm -f /$fs/file-$maxsize-$((i+$file_id_offset))
457 echo Removed $nfiles files of random sizes up to $maxsize bytes
461 # Simulate a random set of operations which could be reasonably expected
462 # to occur on an average filesystem.
464 # $1 Number of files to modify
465 # $2 Maximum file size
466 # $3 File system to modify the file on
467 # $4 Enabled xattrs (optional)
477 # Remove roughly half of the files in order to make it more
478 # likely that a dnode will be reallocated.
480 for ((i=0; i<$nfiles; i=i+1)); do
481 file_name="/$fs/file-$i"
483 if [[ -e $file_name ]]; then
484 if [ $((RANDOM % 2)) -eq 0 ]; then
486 log_fail "Failed to remove $file_name"
492 # Remount the filesystem to simulate normal usage. This resets
493 # the last allocated object id allowing for new objects to be
494 # reallocated in the locations of previously freed objects.
496 log_must zfs unmount $fs
497 log_must zfs mount $fs
499 for i in {0..$nfiles}; do
500 file_name="/$fs/file-$i"
501 file_size=$((($RANDOM * $RANDOM % ($maxsize - 1)) + 1))
504 # When the file exists modify it in one of five ways to
505 # simulate normal usage:
506 # - (20%) Remove and set and extended attribute on the file
507 # - (20%) Overwrite the existing file
508 # - (20%) Truncate the existing file to a random length
509 # - (20%) Truncate the existing file to zero length
510 # - (20%) Remove the file
512 # Otherwise create the missing file. 20% of the created
513 # files will be small and use embedded block pointers, the
514 # remainder with have random sizes up to the maximum size.
515 # Three extended attributes are attached to all of the files.
517 if [[ -e $file_name ]]; then
518 value=$((RANDOM % 5))
519 if [ $value -eq 0 -a $xattrs -ne 0 ]; then
520 attrname="testattr$((RANDOM % 3))"
521 attr -qr $attrname $file_name || \
522 log_fail "Failed to remove $attrname"
523 attr -qs $attrname -V TestValue $file_name || \
524 log_fail "Failed to set $attrname"
525 elif [ $value -eq 1 ]; then
526 dd if=/dev/urandom of=$file_name \
527 bs=$file_size count=1 >/dev/null 2>&1 || \
528 log_fail "Failed to overwrite $file_name"
529 elif [ $value -eq 2 ]; then
530 truncate -s $file_size $file_name || \
531 log_fail "Failed to truncate $file_name"
532 elif [ $value -eq 3 ]; then
533 truncate -s 0 $file_name || \
534 log_fail "Failed to truncate $file_name"
537 log_fail "Failed to remove $file_name"
540 if [ $((RANDOM % 5)) -eq 0 ]; then
541 file_size=$((($RANDOM % 64) + 1))
544 dd if=/dev/urandom of=$file_name \
545 bs=$file_size count=1 >/dev/null 2>&1 || \
546 log_fail "Failed to create $file_name"
548 if [ $xattrs -ne 0 ]; then
550 attrname="testattr$j"
551 attr -qs $attrname -V TestValue \
552 $file_name || log_fail \
553 "Failed to set $attrname"
563 # Mess up file contents
571 filesize=$(stat -c '%s' $file)
572 offset=$(($RANDOM * $RANDOM % $filesize))
573 if (($RANDOM % 7 <= 1)); then
575 # We corrupt 2 bytes to minimize the chance that we
576 # write the same value that's already there.
578 log_must eval "dd if=/dev/urandom of=$file conv=notrunc " \
579 "bs=1 count=2 seek=$offset >/dev/null 2>&1"
581 log_must truncate -s $offset $file
586 # Diff the send/receive filesystems
588 # $1 The sent filesystem
589 # $2 The received filesystem
596 if [[ -d /$recvfs/.zfs/snapshot/a && -d \
597 /$sendfs/.zfs/snapshot/a ]]; then
598 diff -r /$recvfs/.zfs/snapshot/a /$sendfs/.zfs/snapshot/a
599 [[ $? -eq 0 ]] || log_fail "Differences found in snap a"
601 if [[ -d /$recvfs/.zfs/snapshot/b && -d \
602 /$sendfs/.zfs/snapshot/b ]]; then
603 diff -r /$recvfs/.zfs/snapshot/b /$sendfs/.zfs/snapshot/b
604 [[ $? -eq 0 ]] || log_fail "Differences found in snap b"
611 # $1 The ZFS send command
612 # $2 The filesystem where the streams are sent
613 # $3 The receive filesystem
622 log_must eval "$sendcmd >/$streamfs/$stream_num"
624 for ((i=0; i<2; i=i+1)); do
625 mess_file /$streamfs/$stream_num
626 log_mustnot zfs recv -suv $recvfs </$streamfs/$stream_num
627 stream_num=$((stream_num+1))
629 token=$(zfs get -Hp -o value receive_resume_token $recvfs)
630 log_must eval "zfs send -v -t $token >/$streamfs/$stream_num"
631 [[ -f /$streamfs/$stream_num ]] || \
632 log_fail "NO FILE /$streamfs/$stream_num"
634 log_must zfs recv -suv $recvfs </$streamfs/$stream_num
638 # Setup filesystems for the resumable send/receive tests
640 # $1 The "send" filesystem
641 # $2 The "recv" filesystem
643 function test_fs_setup
648 typeset sendpool=${sendfs%%/*}
649 typeset recvpool=${recvfs%%/*}
651 datasetexists $sendfs && log_must_busy zfs destroy -r $sendpool
652 datasetexists $recvfs && log_must_busy zfs destroy -r $recvpool
653 datasetexists $streamfs && log_must_busy zfs destroy -r $streamfs
655 if $(datasetexists $sendfs || zfs create -o compress=lz4 $sendfs); then
656 mk_files 1000 256 0 $sendfs &
657 mk_files 1000 131072 0 $sendfs &
658 mk_files 100 1048576 0 $sendfs &
659 mk_files 10 10485760 0 $sendfs &
660 mk_files 1 104857600 0 $sendfs &
662 log_must zfs snapshot $sendfs@a
664 rm_files 200 256 0 $sendfs &
665 rm_files 200 131072 0 $sendfs &
666 rm_files 20 1048576 0 $sendfs &
667 rm_files 2 10485760 0 $sendfs &
670 mk_files 400 256 0 $sendfs &
671 mk_files 400 131072 0 $sendfs &
672 mk_files 40 1048576 0 $sendfs &
673 mk_files 4 10485760 0 $sendfs &
676 log_must zfs snapshot $sendfs@b
677 log_must eval "zfs send -v $sendfs@a >/$sendpool/initial.zsend"
678 log_must eval "zfs send -v -i @a $sendfs@b " \
679 ">/$sendpool/incremental.zsend"
682 log_must zfs create -o compress=lz4 $streamfs
686 # Check to see if the specified features are set in a send stream.
687 # The values for these features are found in include/sys/zfs_ioctl.h
690 # $2-$n The flags expected in the stream
692 function stream_has_features
697 [[ -f $file ]] || log_fail "Couldn't find file: $file"
698 typeset flags=$(cat $file | zstreamdump | \
699 awk '/features =/ {features = $3} END {print features}')
702 feature[dedupprops]="2"
703 feature[sa_spill]="4"
704 feature[embed_data]="10000"
706 feature[mooch_byteswap]="40000"
707 feature[large_blocks]="80000"
708 feature[resuming]="100000"
709 feature[redacted]="200000"
710 feature[compressed]="400000"
712 typeset flag known derived=0
714 known=${feature[$flag]}
715 [[ -z $known ]] && log_fail "Unknown feature: $flag"
717 derived=$(printf "%x" $((0x${flags} & 0x${feature[$flag]})))
718 [[ $derived = $known ]] || return 1
725 # Given a send stream, verify that the size of the stream matches what's
726 # expected based on the source or target dataset. If the stream is an
727 # incremental stream, subtract the size of the source snapshot before
728 # comparing. This function does not currently handle incremental streams
731 # $1 The zstreamdump output file
732 # $2 The dataset to compare against
733 # This can be a source of a send or recv target (fs, not snapshot)
734 # $3 The percentage below which verification is deemed a failure
735 # $4 The source snapshot of an incremental send
738 function verify_stream_size
742 typeset percent=${3:-90}
745 [[ -f $stream ]] || log_fail "No such file: $stream"
746 datasetexists $ds || log_fail "No such dataset: $ds"
748 typeset stream_size=$(cat $stream | zstreamdump | sed -n \
749 's/ Total write size = \(.*\) (0x.*)/\1/p')
752 if [[ -n $inc_src ]]; then
753 inc_size=$(get_prop lrefer $inc_src)
754 if stream_has_features $stream compressed; then
755 inc_size=$(get_prop refer $inc_src)
759 if stream_has_features $stream compressed; then
760 ds_size=$(get_prop refer $ds)
762 ds_size=$(get_prop lrefer $ds)
764 ds_size=$((ds_size - inc_size))
766 within_percent $stream_size $ds_size $percent || log_fail \
767 "$stream_size $ds_size differed by too much"
770 # Cleanup function for tests involving resumable send
771 function resume_cleanup
775 typeset sendpool=${sendfs%%/*}
777 datasetexists $sendfs && log_must_busy zfs destroy -r $sendfs
778 datasetexists $streamfs && log_must_busy zfs destroy -r $streamfs
780 rm -f /$sendpool/initial.zsend /$sendpool/incremental.zsend
783 # Randomly set the property to one of the enumerated values.
784 function rand_set_prop
789 typeset value=$(random_get $@)
791 log_must eval "zfs set $prop='$value' $dtst"
794 # Generate a recursive checksum of a filesystems contents. Only file
795 # data is included in the checksum (no meta data, or xattrs).
796 function recursive_cksum
798 find $1 -type f -exec sha256sum {} \; | \
799 sort -k 2 | awk '{ print $1 }' | sha256sum