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 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 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
157 # Detect if the given two filesystems have same sub-datasets
159 # $1 source filesystem
160 # $2 destination filesystem
167 zfs list -r -H -t all -o name $src_fs > $BACKDIR/src1
168 zfs list -r -H -t all -o name $dst_fs > $BACKDIR/dst1
170 eval sed -e 's:^$src_fs:PREFIX:g' < $BACKDIR/src1 > $BACKDIR/src
171 eval sed -e 's:^$dst_fs:PREFIX:g' < $BACKDIR/dst1 > $BACKDIR/dst
173 diff $BACKDIR/src $BACKDIR/dst
176 rm -f $BACKDIR/src $BACKDIR/dst $BACKDIR/src1 $BACKDIR/dst1
182 # Compare all the directories and files in two filesystems
184 # $1 source filesystem
185 # $2 destination filesystem
192 typeset srcdir dstdir
193 srcdir=$(get_prop mountpoint $src_fs)
194 dstdir=$(get_prop mountpoint $dst_fs)
196 diff -r $srcdir $dstdir > /dev/null 2>&1
201 # Compare the given two dataset properties
211 for item in "type" "origin" "volblocksize" "acltype" "dnodesize" \
212 "atime" "canmount" "checksum" "compression" "copies" "devices" \
213 "exec" "quota" "readonly" "recordsize" "reservation" "setuid" \
214 "snapdir" "version" "volsize" "xattr" "zoned" "mountpoint";
216 zfs get -H -o property,value,source $item $dtst1 >> \
218 zfs get -H -o property,value,source $item $dtst2 >> \
222 eval sed -e 's:$dtst1:PREFIX:g' < $BACKDIR/dtst1 > $BACKDIR/dtst1
223 eval sed -e 's:$dtst2:PREFIX:g' < $BACKDIR/dtst2 > $BACKDIR/dtst2
225 diff $BACKDIR/dtst1 $BACKDIR/dtst2
228 rm -f $BACKDIR/dtst1 $BACKDIR/dtst2
235 # Random create directories and files
243 if [[ -d $dir ]]; then
250 ((nl = RANDOM % 6 + 1))
253 mktree -b $dir -l $nl -d $nd -f $nf
260 # Put data in filesystem and take snapshot
264 function snapshot_tree
267 typeset ds=${snap%%@*}
268 typeset type=$(get_prop "type" $ds)
271 if [[ $type == "filesystem" ]]; then
272 typeset mntpnt=$(get_prop mountpoint $ds)
275 if ((ret == 0)) ; then
276 eval random_tree $mntpnt/${snap##$ds}
281 if ((ret == 0)) ; then
290 # Destroy the given snapshot and stuff
294 function destroy_tree
298 for snap in "$@" ; do
302 typeset ds=${snap%%@*}
303 typeset type=$(get_prop "type" $ds)
304 if [[ $type == "filesystem" ]]; then
305 typeset mntpnt=$(get_prop mountpoint $ds)
308 if ((ret != 0)); then
314 if ((ret != 0)); then
323 # Get all the sub-datasets of give dataset with specific suffix
328 function getds_with_suffix
333 typeset list=$(zfs list -r -H -t all -o name $ds | grep "$suffix$")
339 # Output inherited properties whitch is edited for file system
341 function fs_inherit_prop
344 if is_global_zone ; then
345 fs_prop=$(zfs inherit 2>&1 | \
346 awk '$2=="YES" && $3=="YES" {print $1}')
347 if ! is_te_enabled ; then
348 fs_prop=$(echo $fs_prop | grep -v "mlslabel")
351 fs_prop=$(zfs inherit 2>&1 | \
352 awk '$2=="YES" && $3=="YES" {print $1}'|
353 egrep -v "devices|mlslabel|sharenfs|sharesmb|zoned")
360 # Output inherited properties for volume
362 function vol_inherit_prop
364 echo "checksum readonly"
368 # Get the destination dataset to compare
376 # If the srcfs is not pool
378 if ! zpool list $srcfs > /dev/null 2>&1 ; then
379 eval dstfs="$dstfs/${srcfs#*/}"
388 # $1 Number of files to create
389 # $2 Maximum file size
391 # $4 File system to create the files on
400 for ((i=0; i<$nfiles; i=i+1)); do
402 of=/$fs/file-$maxsize-$((i+$file_id_offset)) \
403 bs=$((($RANDOM * $RANDOM % ($maxsize - 1)) + 1)) \
404 count=1 >/dev/null 2>&1 || log_fail \
405 "Failed to create /$fs/file-$maxsize-$((i+$file_id_offset))"
407 echo Created $nfiles files of random sizes up to $maxsize bytes
413 # $1 Number of files to remove
414 # $2 Maximum file size
416 # $4 File system to remove the files from
425 for ((i=0; i<$nfiles; i=i+1)); do
426 rm -f /$fs/file-$maxsize-$((i+$file_id_offset))
428 echo Removed $nfiles files of random sizes up to $maxsize bytes
432 # Mess up file contents
440 filesize=$(stat -c '%s' $file)
441 offset=$(($RANDOM * $RANDOM % $filesize))
442 if (($RANDOM % 7 <= 1)); then
444 # We corrupt 2 bytes to minimize the chance that we
445 # write the same value that's already there.
447 log_must eval "dd if=/dev/urandom of=$file conv=notrunc " \
448 "bs=1 count=2 seek=$offset >/dev/null 2>&1"
450 log_must truncate -s $offset $file
455 # Diff the send/receive filesystems
457 # $1 The sent filesystem
458 # $2 The received filesystem
465 if [[ -d /$recvfs/.zfs/snapshot/a && -d \
466 /$sendfs/.zfs/snapshot/a ]]; then
467 diff -r /$recvfs/.zfs/snapshot/a /$sendfs/.zfs/snapshot/a
468 [[ $? -eq 0 ]] || log_fail "Differences found in snap a"
470 if [[ -d /$recvfs/.zfs/snapshot/b && -d \
471 /$sendfs/.zfs/snapshot/b ]]; then
472 diff -r /$recvfs/.zfs/snapshot/b /$sendfs/.zfs/snapshot/b
473 [[ $? -eq 0 ]] || log_fail "Differences found in snap b"
480 # $1 The ZFS send command
481 # $2 The filesystem where the streams are sent
482 # $3 The receive filesystem
491 log_must eval "$sendcmd >/$streamfs/$stream_num"
493 for ((i=0; i<2; i=i+1)); do
494 mess_file /$streamfs/$stream_num
495 log_mustnot zfs recv -suv $recvfs </$streamfs/$stream_num
496 stream_num=$((stream_num+1))
498 token=$(zfs get -Hp -o value receive_resume_token $recvfs)
499 log_must eval "zfs send -v -t $token >/$streamfs/$stream_num"
500 [[ -f /$streamfs/$stream_num ]] || \
501 log_fail "NO FILE /$streamfs/$stream_num"
503 log_must zfs recv -suv $recvfs </$streamfs/$stream_num
507 # Setup filesystems for the resumable send/receive tests
509 # $1 The "send" filesystem
510 # $2 The "recv" filesystem
512 function test_fs_setup
516 typeset sendpool=${sendfs%%/*}
517 typeset recvpool=${recvfs%%/*}
519 datasetexists $sendfs && log_must zfs destroy -r $sendpool
520 datasetexists $recvfs && log_must zfs destroy -r $recvpool
522 if $(datasetexists $sendfs || zfs create -o compress=lz4 $sendfs); then
523 mk_files 1000 256 0 $sendfs &
524 mk_files 1000 131072 0 $sendfs &
525 mk_files 100 1048576 0 $sendfs &
526 mk_files 10 10485760 0 $sendfs &
527 mk_files 1 104857600 0 $sendfs &
529 log_must zfs snapshot $sendfs@a
531 rm_files 200 256 0 $sendfs &
532 rm_files 200 131072 0 $sendfs &
533 rm_files 20 1048576 0 $sendfs &
534 rm_files 2 10485760 0 $sendfs &
537 mk_files 400 256 0 $sendfs &
538 mk_files 400 131072 0 $sendfs &
539 mk_files 40 1048576 0 $sendfs &
540 mk_files 4 10485760 0 $sendfs &
543 log_must zfs snapshot $sendfs@b
544 log_must eval "zfs send -v $sendfs@a >/$sendpool/initial.zsend"
545 log_must eval "zfs send -v -i @a $sendfs@b " \
546 ">/$sendpool/incremental.zsend"
549 if datasetexists $streamfs; then
550 log_must zfs destroy -r $streamfs
552 log_must zfs create -o compress=lz4 $sendpool/stream
556 # Check to see if the specified features are set in a send stream.
557 # The values for these features are found in include/sys/zfs_ioctl.h
560 # $2-$n The flags expected in the stream
562 function stream_has_features
567 [[ -f $file ]] || log_fail "Couldn't find file: $file"
568 typeset flags=$(cat $file | zstreamdump | \
569 awk '/features =/ {features = $3} END {print features}')
572 feature[dedupprops]="2"
573 feature[sa_spill]="4"
574 feature[embed_data]="10000"
576 feature[mooch_byteswap]="40000"
577 feature[large_blocks]="80000"
578 feature[resuming]="100000"
579 feature[redacted]="200000"
580 feature[compressed]="400000"
582 typeset flag known derived=0
584 known=${feature[$flag]}
585 [[ -z $known ]] && log_fail "Unknown feature: $flag"
587 derived=$(printf "%x" $((0x${flags} & 0x${feature[$flag]})))
588 [[ $derived = $known ]] || return 1
595 # Parse zstreamdump -v output. The output varies for each kind of record:
596 # BEGIN records are simply output as "BEGIN"
597 # END records are output as "END"
598 # OBJECT records become "OBJECT <object num>"
599 # FREEOBJECTS records become "FREEOBJECTS <startobj> <numobjs>"
600 # FREE records become "<record type> <start> <length>"
601 # WRITE records become:
602 # "<record type> <compression type> <start> <logical size> <compressed size>
607 sed '/^WRITE/{N;s/\n/ /;}' | grep "^[A-Z]" | awk '{
608 if ($1 == "BEGIN" || $1 == "END") print $1
609 if ($1 == "OBJECT") print $1" "$4
610 if ($1 == "FREEOBJECTS") print $1" "$4" "$7
611 if ($1 == "FREE") print $1" "$7" "$10
612 if ($1 == "WRITE") print $1" "$15" "$18" "$21" "$24" "$27}'
616 # Given a send stream, verify that the size of the stream matches what's
617 # expected based on the source or target dataset. If the stream is an
618 # incremental stream, subtract the size of the source snapshot before
619 # comparing. This function does not currently handle incremental streams
622 # $1 The zstreamdump output file
623 # $2 The dataset to compare against
624 # This can be a source of a send or recv target (fs, not snapshot)
625 # $3 The percentage below which verification is deemed a failure
626 # $4 The source snapshot of an incremental send
629 function verify_stream_size
633 typeset percent=${3:-90}
636 [[ -f $stream ]] || log_fail "No such file: $stream"
637 datasetexists $ds || log_fail "No such dataset: $ds"
639 typeset stream_size=$(cat $stream | zstreamdump | sed -n \
640 's/ Total write size = \(.*\) (0x.*)/\1/p')
643 if [[ -n $inc_src ]]; then
644 inc_size=$(get_prop lrefer $inc_src)
645 if stream_has_features $stream compressed; then
646 inc_size=$(get_prop refer $inc_src)
650 if stream_has_features $stream compressed; then
651 ds_size=$(get_prop refer $ds)
653 ds_size=$(get_prop lrefer $ds)
655 ds_size=$((ds_size - inc_size))
657 within_percent $stream_size $ds_size $percent || log_fail \
658 "$stream_size $ds_size differed by too much"
661 # Cleanup function for tests involving resumable send
662 function resume_cleanup
667 datasetexists $sendfs && log_must zfs destroy -r $sendfs
668 datasetexists $streamfs && log_must zfs destroy -r $streamfs
670 rm -f /$POOL/initial.zsend /$POOL/incremental.zsend