1#!/bin/ksh
2#
3# CDDL HEADER START
4#
5# This file and its contents are supplied under the terms of the
6# Common Development and Distribution License ("CDDL"), version 1.0.
7# You may only use this file in accordance with the terms of version
8# 1.0 of the CDDL.
9#
10# A full copy of the text of the CDDL should have accompanied this
11# source.  A copy of the CDDL is also available via the Internet at
12# http://www.illumos.org/license/CDDL.
13#
14# CDDL HEADER END
15#
16
17#
18# Copyright (c) 2015, 2016 by Delphix. All rights reserved.
19#
20
21. $STF_SUITE/include/libtest.shlib
22#
23# DESCRIPTION:
24#	Verify 'zfs send' drills holes appropriately when files are replaced
25#
26# STRATEGY:
27#	1. Create dataset
28#	2. Write block 0 in a bunch of files
29#	3. Snapshot the dataset
30#	4. Remove all the files and rewrite some files with just block 1
31#	5. Snapshot the dataset
32#	6. Send both snapshots and receive them locally
33#	7. diff the received dataset and the old datasets.
34#	8. Repeat steps 1-7 above with pool that never had hole birth enabled.
35#
36
37verify_runnable "both"
38
39function cleanup
40{
41	zfs destroy -rf $TESTPOOL/fs
42	zfs destroy -rf $TESTPOOL/recvfs
43	rm $streamfile
44	rm $vdev
45	zpool destroy tmp_pool
46}
47
48
49log_assert "Verify that 'zfs send' drills appropriate holes"
50log_onexit cleanup
51streamfile=$(mktemp $TESTDIR/file.XXXXXX)
52vdev=$(mktemp $TEST_BASE_DIR/file.XXXXXX)
53
54
55function test_pool
56{
57	POOL=$1
58	log_must zfs create -o recordsize=512 $POOL/fs
59	mntpnt=$(get_prop mountpoint "$POOL/fs")
60	log_must eval "dd if=/dev/urandom of=${mntpnt}/file bs=512 count=1 2>/dev/null"
61	object=$(ls -i $mntpnt | awk '{print $1}')
62	log_must zfs snapshot $POOL/fs@a
63	while true; do
64		log_must find $mntpnt/ -type f -delete
65		sync_all_pools
66		log_must mkfiles "$mntpnt/" 4000
67		sync_all_pools
68		# check if we started reusing objects
69		object=$(ls -i $mntpnt | sort -n | awk -v object=$object \
70		    '{if ($1 <= object) {exit 1}} END {print $1}') || break
71	done
72	dd if=/dev/urandom of=${mntpnt}/$FILE bs=512 count=1 seek=1 2>/dev/null
73
74	log_must zfs snapshot $POOL/fs@b
75
76	log_must eval "zfs send $POOL/fs@a > $streamfile"
77	cat $streamfile | log_must zfs receive $POOL/recvfs
78
79	log_must eval "zfs send -i @a $POOL/fs@b > $streamfile"
80	cat $streamfile | log_must zfs receive $POOL/recvfs
81
82	recv_mntpnt=$(get_prop mountpoint "$POOL/recvfs")
83	log_must directory_diff $mntpnt $recv_mntpnt
84	log_must zfs destroy -rf $POOL/fs
85	log_must zfs destroy -rf $POOL/recvfs
86}
87
88test_pool $TESTPOOL
89log_must truncate -s 1G $vdev
90log_must zpool create -o version=1 tmp_pool $vdev
91test_pool tmp_pool
92log_must zpool destroy tmp_pool
93log_must zpool create -d tmp_pool $vdev
94test_pool tmp_pool
95log_must zpool destroy tmp_pool
96
97log_pass "'zfs send' drills appropriate holes"
98