1#!/bin/ksh -p
2#
3# This file and its contents are supplied under the terms of the
4# Common Development and Distribution License ("CDDL"), version 1.0.
5# You may only use this file in accordance with the terms of version
6# 1.0 of the CDDL.
7#
8# A full copy of the text of the CDDL should have accompanied this
9# source.  A copy of the CDDL is also available via the Internet at
10# http://www.illumos.org/license/CDDL.
11#
12
13#
14# Copyright 2017, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
15#
16
17. $STF_SUITE/include/libtest.shlib
18
19#
20# DESCRIPTION:
21# 'zfs send' should be able to send (big) sparse files correctly.
22#
23# STRATEGY:
24# 1. Create sparse files of various size
25# 2. Snapshot and send these sparse files
26# 3. Verify these files are received correctly and we don't trigger any issue
27#    like the one described in https://github.com/openzfs/zfs/pull/6760
28#
29
30verify_runnable "both"
31
32function cleanup
33{
34        datasetexists $SENDFS && log_must zfs destroy -r $SENDFS
35        datasetexists $RECVFS && log_must zfs destroy -r $RECVFS
36}
37
38#
39# Write 1 random byte at $offset of "source" file in $sendfs dataset
40# Snapshot and send $sendfs dataset to $recvfs
41# Compare the received file with its source
42#
43function write_compare_files # <sendfs> <recvfs> <offset>
44{
45	typeset sendfs="$1"
46	typeset recvfs="$2"
47	typeset offset="$3"
48
49	# create source filesystem
50	log_must zfs create $sendfs
51	# write sparse file
52	sendfile="$(get_prop mountpoint $sendfs)/data.bin"
53	log_must dd if=/dev/urandom of=$sendfile bs=1 count=1 seek=$offset
54	# send/receive the file
55	log_must zfs snapshot $sendfs@snap
56	log_must eval "zfs send $sendfs@snap | zfs receive $recvfs"
57	# compare sparse files
58	recvfile="$(get_prop mountpoint $recvfs)/data.bin"
59	log_must cmp $sendfile $recvfile $offset $offset
60	sendsz=$(stat_size $sendfile)
61	recvsz=$(stat_size $recvfile)
62	if [[ $sendsz -ne $recvsz ]]; then
63		log_fail "$sendfile ($sendsz) and $recvfile ($recvsz) differ."
64	fi
65	# cleanup
66	log_must zfs destroy -r $sendfs
67	log_must zfs destroy -r $recvfs
68}
69
70log_assert "'zfs send' should be able to send (big) sparse files correctly."
71log_onexit cleanup
72
73SENDFS="$TESTPOOL/sendfs"
74RECVFS="$TESTPOOL/recvfs"
75OFF_T_MAX="$(echo '2 ^ 40 * 8 - 1' | bc)"
76
77for i in {1..60}; do
78	offset=$(echo "2 ^ $i" | bc)
79	[[ is_32bit ]] && [[ $offset -ge $OFF_T_MAX ]] && continue;
80	write_compare_files $SENDFS $RECVFS $offset
81done
82
83log_pass "'zfs send' sends (big) sparse files correctly."
84