xref: /freebsd-src/sys/contrib/openzfs/cmd/zstream/zstream_redup.c (revision 9db44a8e5da9bf1ce6dd1c0f1468ddafed6d6c91)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * This file and its contents are supplied under the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License ("CDDL"), version 1.0.
6eda14cbcSMatt Macy  * You may only use this file in accordance with the terms of version
7eda14cbcSMatt Macy  * 1.0 of the CDDL.
8eda14cbcSMatt Macy  *
9eda14cbcSMatt Macy  * A full copy of the text of the CDDL should have accompanied this
10eda14cbcSMatt Macy  * source.  A copy of the CDDL is also available via the Internet at
11eda14cbcSMatt Macy  * http://www.illumos.org/license/CDDL.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * CDDL HEADER END
14eda14cbcSMatt Macy  */
15eda14cbcSMatt Macy 
16eda14cbcSMatt Macy /*
17eda14cbcSMatt Macy  * Copyright (c) 2020 by Delphix. All rights reserved.
18eda14cbcSMatt Macy  */
19eda14cbcSMatt Macy 
20eda14cbcSMatt Macy #include <assert.h>
21eda14cbcSMatt Macy #include <cityhash.h>
22eda14cbcSMatt Macy #include <ctype.h>
23eda14cbcSMatt Macy #include <errno.h>
24eda14cbcSMatt Macy #include <fcntl.h>
25eda14cbcSMatt Macy #include <libzfs_impl.h>
26eda14cbcSMatt Macy #include <libzfs.h>
27eda14cbcSMatt Macy #include <libzutil.h>
28eda14cbcSMatt Macy #include <stddef.h>
29eda14cbcSMatt Macy #include <stdio.h>
30eda14cbcSMatt Macy #include <stdlib.h>
31eda14cbcSMatt Macy #include <strings.h>
32eda14cbcSMatt Macy #include <umem.h>
33eda14cbcSMatt Macy #include <unistd.h>
34eda14cbcSMatt Macy #include <sys/debug.h>
35eda14cbcSMatt Macy #include <sys/stat.h>
36eda14cbcSMatt Macy #include <sys/zfs_ioctl.h>
37eda14cbcSMatt Macy #include <sys/zio_checksum.h>
38eda14cbcSMatt Macy #include "zfs_fletcher.h"
39eda14cbcSMatt Macy #include "zstream.h"
40eda14cbcSMatt Macy 
41eda14cbcSMatt Macy 
42eda14cbcSMatt Macy #define	MAX_RDT_PHYSMEM_PERCENT		20
43eda14cbcSMatt Macy #define	SMALLEST_POSSIBLE_MAX_RDT_MB		128
44eda14cbcSMatt Macy 
45eda14cbcSMatt Macy typedef struct redup_entry {
46eda14cbcSMatt Macy 	struct redup_entry	*rde_next;
47eda14cbcSMatt Macy 	uint64_t rde_guid;
48eda14cbcSMatt Macy 	uint64_t rde_object;
49eda14cbcSMatt Macy 	uint64_t rde_offset;
50eda14cbcSMatt Macy 	uint64_t rde_stream_offset;
51eda14cbcSMatt Macy } redup_entry_t;
52eda14cbcSMatt Macy 
53eda14cbcSMatt Macy typedef struct redup_table {
54eda14cbcSMatt Macy 	redup_entry_t	**redup_hash_array;
55eda14cbcSMatt Macy 	umem_cache_t	*ddecache;
56eda14cbcSMatt Macy 	uint64_t	ddt_count;
57eda14cbcSMatt Macy 	int		numhashbits;
58eda14cbcSMatt Macy } redup_table_t;
59eda14cbcSMatt Macy 
60eda14cbcSMatt Macy int
61eda14cbcSMatt Macy highbit64(uint64_t i)
62eda14cbcSMatt Macy {
63eda14cbcSMatt Macy 	if (i == 0)
64eda14cbcSMatt Macy 		return (0);
65eda14cbcSMatt Macy 
66eda14cbcSMatt Macy 	return (NBBY * sizeof (uint64_t) - __builtin_clzll(i));
67eda14cbcSMatt Macy }
68eda14cbcSMatt Macy 
69eda14cbcSMatt Macy static void *
70eda14cbcSMatt Macy safe_calloc(size_t n)
71eda14cbcSMatt Macy {
72eda14cbcSMatt Macy 	void *rv = calloc(1, n);
73eda14cbcSMatt Macy 	if (rv == NULL) {
74eda14cbcSMatt Macy 		fprintf(stderr,
75eda14cbcSMatt Macy 		    "Error: could not allocate %u bytes of memory\n",
76eda14cbcSMatt Macy 		    (int)n);
77eda14cbcSMatt Macy 		exit(1);
78eda14cbcSMatt Macy 	}
79eda14cbcSMatt Macy 	return (rv);
80eda14cbcSMatt Macy }
81eda14cbcSMatt Macy 
82eda14cbcSMatt Macy /*
83eda14cbcSMatt Macy  * Safe version of fread(), exits on error.
84eda14cbcSMatt Macy  */
85eda14cbcSMatt Macy static int
86eda14cbcSMatt Macy sfread(void *buf, size_t size, FILE *fp)
87eda14cbcSMatt Macy {
88eda14cbcSMatt Macy 	int rv = fread(buf, size, 1, fp);
89eda14cbcSMatt Macy 	if (rv == 0 && ferror(fp)) {
90eda14cbcSMatt Macy 		(void) fprintf(stderr, "Error while reading file: %s\n",
91eda14cbcSMatt Macy 		    strerror(errno));
92eda14cbcSMatt Macy 		exit(1);
93eda14cbcSMatt Macy 	}
94eda14cbcSMatt Macy 	return (rv);
95eda14cbcSMatt Macy }
96eda14cbcSMatt Macy 
97eda14cbcSMatt Macy /*
98eda14cbcSMatt Macy  * Safe version of pread(), exits on error.
99eda14cbcSMatt Macy  */
100eda14cbcSMatt Macy static void
101eda14cbcSMatt Macy spread(int fd, void *buf, size_t count, off_t offset)
102eda14cbcSMatt Macy {
103eda14cbcSMatt Macy 	ssize_t err = pread(fd, buf, count, offset);
104eda14cbcSMatt Macy 	if (err == -1) {
105eda14cbcSMatt Macy 		(void) fprintf(stderr,
106eda14cbcSMatt Macy 		    "Error while reading file: %s\n",
107eda14cbcSMatt Macy 		    strerror(errno));
108eda14cbcSMatt Macy 		exit(1);
109eda14cbcSMatt Macy 	} else if (err != count) {
110eda14cbcSMatt Macy 		(void) fprintf(stderr,
111eda14cbcSMatt Macy 		    "Error while reading file: short read\n");
112eda14cbcSMatt Macy 		exit(1);
113eda14cbcSMatt Macy 	}
114eda14cbcSMatt Macy }
115eda14cbcSMatt Macy 
116eda14cbcSMatt Macy static int
117eda14cbcSMatt Macy dump_record(dmu_replay_record_t *drr, void *payload, int payload_len,
118eda14cbcSMatt Macy     zio_cksum_t *zc, int outfd)
119eda14cbcSMatt Macy {
120eda14cbcSMatt Macy 	assert(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum)
121eda14cbcSMatt Macy 	    == sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
122eda14cbcSMatt Macy 	fletcher_4_incremental_native(drr,
123eda14cbcSMatt Macy 	    offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
124eda14cbcSMatt Macy 	if (drr->drr_type != DRR_BEGIN) {
125eda14cbcSMatt Macy 		assert(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
126eda14cbcSMatt Macy 		    drr_checksum.drr_checksum));
127eda14cbcSMatt Macy 		drr->drr_u.drr_checksum.drr_checksum = *zc;
128eda14cbcSMatt Macy 	}
129eda14cbcSMatt Macy 	fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum,
130eda14cbcSMatt Macy 	    sizeof (zio_cksum_t), zc);
131eda14cbcSMatt Macy 	if (write(outfd, drr, sizeof (*drr)) == -1)
132eda14cbcSMatt Macy 		return (errno);
133eda14cbcSMatt Macy 	if (payload_len != 0) {
134eda14cbcSMatt Macy 		fletcher_4_incremental_native(payload, payload_len, zc);
135eda14cbcSMatt Macy 		if (write(outfd, payload, payload_len) == -1)
136eda14cbcSMatt Macy 			return (errno);
137eda14cbcSMatt Macy 	}
138eda14cbcSMatt Macy 	return (0);
139eda14cbcSMatt Macy }
140eda14cbcSMatt Macy 
141eda14cbcSMatt Macy static void
142eda14cbcSMatt Macy rdt_insert(redup_table_t *rdt,
143eda14cbcSMatt Macy     uint64_t guid, uint64_t object, uint64_t offset, uint64_t stream_offset)
144eda14cbcSMatt Macy {
145eda14cbcSMatt Macy 	uint64_t ch = cityhash4(guid, object, offset, 0);
146eda14cbcSMatt Macy 	uint64_t hashcode = BF64_GET(ch, 0, rdt->numhashbits);
147eda14cbcSMatt Macy 	redup_entry_t **rdepp;
148eda14cbcSMatt Macy 
149eda14cbcSMatt Macy 	rdepp = &(rdt->redup_hash_array[hashcode]);
150eda14cbcSMatt Macy 	redup_entry_t *rde = umem_cache_alloc(rdt->ddecache, UMEM_NOFAIL);
151eda14cbcSMatt Macy 	rde->rde_next = *rdepp;
152eda14cbcSMatt Macy 	rde->rde_guid = guid;
153eda14cbcSMatt Macy 	rde->rde_object = object;
154eda14cbcSMatt Macy 	rde->rde_offset = offset;
155eda14cbcSMatt Macy 	rde->rde_stream_offset = stream_offset;
156eda14cbcSMatt Macy 	*rdepp = rde;
157eda14cbcSMatt Macy 	rdt->ddt_count++;
158eda14cbcSMatt Macy }
159eda14cbcSMatt Macy 
160eda14cbcSMatt Macy static void
161eda14cbcSMatt Macy rdt_lookup(redup_table_t *rdt,
162eda14cbcSMatt Macy     uint64_t guid, uint64_t object, uint64_t offset,
163eda14cbcSMatt Macy     uint64_t *stream_offsetp)
164eda14cbcSMatt Macy {
165eda14cbcSMatt Macy 	uint64_t ch = cityhash4(guid, object, offset, 0);
166eda14cbcSMatt Macy 	uint64_t hashcode = BF64_GET(ch, 0, rdt->numhashbits);
167eda14cbcSMatt Macy 
168eda14cbcSMatt Macy 	for (redup_entry_t *rde = rdt->redup_hash_array[hashcode];
169eda14cbcSMatt Macy 	    rde != NULL; rde = rde->rde_next) {
170eda14cbcSMatt Macy 		if (rde->rde_guid == guid &&
171eda14cbcSMatt Macy 		    rde->rde_object == object &&
172eda14cbcSMatt Macy 		    rde->rde_offset == offset) {
173eda14cbcSMatt Macy 			*stream_offsetp = rde->rde_stream_offset;
174eda14cbcSMatt Macy 			return;
175eda14cbcSMatt Macy 		}
176eda14cbcSMatt Macy 	}
177eda14cbcSMatt Macy 	assert(!"could not find expected redup table entry");
178eda14cbcSMatt Macy }
179eda14cbcSMatt Macy 
180eda14cbcSMatt Macy /*
181eda14cbcSMatt Macy  * Convert a dedup stream (generated by "zfs send -D") to a
182eda14cbcSMatt Macy  * non-deduplicated stream.  The entire infd will be converted, including
183eda14cbcSMatt Macy  * any substreams in a stream package (generated by "zfs send -RD"). The
184eda14cbcSMatt Macy  * infd must be seekable.
185eda14cbcSMatt Macy  */
186eda14cbcSMatt Macy static void
187eda14cbcSMatt Macy zfs_redup_stream(int infd, int outfd, boolean_t verbose)
188eda14cbcSMatt Macy {
189eda14cbcSMatt Macy 	int bufsz = SPA_MAXBLOCKSIZE;
190eda14cbcSMatt Macy 	dmu_replay_record_t thedrr = { 0 };
191eda14cbcSMatt Macy 	dmu_replay_record_t *drr = &thedrr;
192eda14cbcSMatt Macy 	redup_table_t rdt;
193eda14cbcSMatt Macy 	zio_cksum_t stream_cksum;
194eda14cbcSMatt Macy 	uint64_t numbuckets;
195eda14cbcSMatt Macy 	uint64_t num_records = 0;
196eda14cbcSMatt Macy 	uint64_t num_write_byref_records = 0;
197eda14cbcSMatt Macy 
198eda14cbcSMatt Macy #ifdef _ILP32
199eda14cbcSMatt Macy 	uint64_t max_rde_size = SMALLEST_POSSIBLE_MAX_RDT_MB << 20;
200eda14cbcSMatt Macy #else
201eda14cbcSMatt Macy 	uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
202eda14cbcSMatt Macy 	uint64_t max_rde_size =
203eda14cbcSMatt Macy 	    MAX((physmem * MAX_RDT_PHYSMEM_PERCENT) / 100,
204eda14cbcSMatt Macy 	    SMALLEST_POSSIBLE_MAX_RDT_MB << 20);
205eda14cbcSMatt Macy #endif
206eda14cbcSMatt Macy 
207eda14cbcSMatt Macy 	numbuckets = max_rde_size / (sizeof (redup_entry_t));
208eda14cbcSMatt Macy 
209eda14cbcSMatt Macy 	/*
210eda14cbcSMatt Macy 	 * numbuckets must be a power of 2.  Increase number to
211eda14cbcSMatt Macy 	 * a power of 2 if necessary.
212eda14cbcSMatt Macy 	 */
213eda14cbcSMatt Macy 	if (!ISP2(numbuckets))
214eda14cbcSMatt Macy 		numbuckets = 1ULL << highbit64(numbuckets);
215eda14cbcSMatt Macy 
216eda14cbcSMatt Macy 	rdt.redup_hash_array =
217eda14cbcSMatt Macy 	    safe_calloc(numbuckets * sizeof (redup_entry_t *));
218eda14cbcSMatt Macy 	rdt.ddecache = umem_cache_create("rde", sizeof (redup_entry_t), 0,
219eda14cbcSMatt Macy 	    NULL, NULL, NULL, NULL, NULL, 0);
220eda14cbcSMatt Macy 	rdt.numhashbits = highbit64(numbuckets) - 1;
221eda14cbcSMatt Macy 	rdt.ddt_count = 0;
222eda14cbcSMatt Macy 
223eda14cbcSMatt Macy 	char *buf = safe_calloc(bufsz);
224eda14cbcSMatt Macy 	FILE *ofp = fdopen(infd, "r");
225eda14cbcSMatt Macy 	long offset = ftell(ofp);
226eda14cbcSMatt Macy 	while (sfread(drr, sizeof (*drr), ofp) != 0) {
227eda14cbcSMatt Macy 		num_records++;
228eda14cbcSMatt Macy 
229eda14cbcSMatt Macy 		/*
230eda14cbcSMatt Macy 		 * We need to regenerate the checksum.
231eda14cbcSMatt Macy 		 */
232eda14cbcSMatt Macy 		if (drr->drr_type != DRR_BEGIN) {
233eda14cbcSMatt Macy 			bzero(&drr->drr_u.drr_checksum.drr_checksum,
234eda14cbcSMatt Macy 			    sizeof (drr->drr_u.drr_checksum.drr_checksum));
235eda14cbcSMatt Macy 		}
236eda14cbcSMatt Macy 
237eda14cbcSMatt Macy 		uint64_t payload_size = 0;
238eda14cbcSMatt Macy 		switch (drr->drr_type) {
239eda14cbcSMatt Macy 		case DRR_BEGIN:
240eda14cbcSMatt Macy 		{
241eda14cbcSMatt Macy 			struct drr_begin *drrb = &drr->drr_u.drr_begin;
242eda14cbcSMatt Macy 			int fflags;
243eda14cbcSMatt Macy 			ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0);
244eda14cbcSMatt Macy 
245eda14cbcSMatt Macy 			assert(drrb->drr_magic == DMU_BACKUP_MAGIC);
246eda14cbcSMatt Macy 
247eda14cbcSMatt Macy 			/* clear the DEDUP feature flag for this stream */
248eda14cbcSMatt Macy 			fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
249eda14cbcSMatt Macy 			fflags &= ~(DMU_BACKUP_FEATURE_DEDUP |
250eda14cbcSMatt Macy 			    DMU_BACKUP_FEATURE_DEDUPPROPS);
251*9db44a8eSMartin Matuska 			/* cppcheck-suppress syntaxError */
252eda14cbcSMatt Macy 			DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags);
253eda14cbcSMatt Macy 
254eda14cbcSMatt Macy 			int sz = drr->drr_payloadlen;
255eda14cbcSMatt Macy 			if (sz != 0) {
256eda14cbcSMatt Macy 				if (sz > bufsz) {
257eda14cbcSMatt Macy 					free(buf);
258eda14cbcSMatt Macy 					buf = safe_calloc(sz);
259eda14cbcSMatt Macy 					bufsz = sz;
260eda14cbcSMatt Macy 				}
261eda14cbcSMatt Macy 				(void) sfread(buf, sz, ofp);
262eda14cbcSMatt Macy 			}
263eda14cbcSMatt Macy 			payload_size = sz;
264eda14cbcSMatt Macy 			break;
265eda14cbcSMatt Macy 		}
266eda14cbcSMatt Macy 
267eda14cbcSMatt Macy 		case DRR_END:
268eda14cbcSMatt Macy 		{
269eda14cbcSMatt Macy 			struct drr_end *drre = &drr->drr_u.drr_end;
270eda14cbcSMatt Macy 			/*
271eda14cbcSMatt Macy 			 * Use the recalculated checksum, unless this is
272eda14cbcSMatt Macy 			 * the END record of a stream package, which has
273eda14cbcSMatt Macy 			 * no checksum.
274eda14cbcSMatt Macy 			 */
275eda14cbcSMatt Macy 			if (!ZIO_CHECKSUM_IS_ZERO(&drre->drr_checksum))
276eda14cbcSMatt Macy 				drre->drr_checksum = stream_cksum;
277eda14cbcSMatt Macy 			break;
278eda14cbcSMatt Macy 		}
279eda14cbcSMatt Macy 
280eda14cbcSMatt Macy 		case DRR_OBJECT:
281eda14cbcSMatt Macy 		{
282eda14cbcSMatt Macy 			struct drr_object *drro = &drr->drr_u.drr_object;
283eda14cbcSMatt Macy 
284eda14cbcSMatt Macy 			if (drro->drr_bonuslen > 0) {
285eda14cbcSMatt Macy 				payload_size = DRR_OBJECT_PAYLOAD_SIZE(drro);
286eda14cbcSMatt Macy 				(void) sfread(buf, payload_size, ofp);
287eda14cbcSMatt Macy 			}
288eda14cbcSMatt Macy 			break;
289eda14cbcSMatt Macy 		}
290eda14cbcSMatt Macy 
291eda14cbcSMatt Macy 		case DRR_SPILL:
292eda14cbcSMatt Macy 		{
293eda14cbcSMatt Macy 			struct drr_spill *drrs = &drr->drr_u.drr_spill;
294eda14cbcSMatt Macy 			payload_size = DRR_SPILL_PAYLOAD_SIZE(drrs);
295eda14cbcSMatt Macy 			(void) sfread(buf, payload_size, ofp);
296eda14cbcSMatt Macy 			break;
297eda14cbcSMatt Macy 		}
298eda14cbcSMatt Macy 
299eda14cbcSMatt Macy 		case DRR_WRITE_BYREF:
300eda14cbcSMatt Macy 		{
301eda14cbcSMatt Macy 			struct drr_write_byref drrwb =
302eda14cbcSMatt Macy 			    drr->drr_u.drr_write_byref;
303eda14cbcSMatt Macy 
304eda14cbcSMatt Macy 			num_write_byref_records++;
305eda14cbcSMatt Macy 
306eda14cbcSMatt Macy 			/*
307eda14cbcSMatt Macy 			 * Look up in hash table by drrwb->drr_refguid,
308eda14cbcSMatt Macy 			 * drr_refobject, drr_refoffset.  Replace this
309eda14cbcSMatt Macy 			 * record with the found WRITE record, but with
310eda14cbcSMatt Macy 			 * drr_object,drr_offset,drr_toguid replaced with ours.
311eda14cbcSMatt Macy 			 */
312eda14cbcSMatt Macy 			uint64_t stream_offset = 0;
313eda14cbcSMatt Macy 			rdt_lookup(&rdt, drrwb.drr_refguid,
314eda14cbcSMatt Macy 			    drrwb.drr_refobject, drrwb.drr_refoffset,
315eda14cbcSMatt Macy 			    &stream_offset);
316eda14cbcSMatt Macy 
317eda14cbcSMatt Macy 			spread(infd, drr, sizeof (*drr), stream_offset);
318eda14cbcSMatt Macy 
319eda14cbcSMatt Macy 			assert(drr->drr_type == DRR_WRITE);
320eda14cbcSMatt Macy 			struct drr_write *drrw = &drr->drr_u.drr_write;
321eda14cbcSMatt Macy 			assert(drrw->drr_toguid == drrwb.drr_refguid);
322eda14cbcSMatt Macy 			assert(drrw->drr_object == drrwb.drr_refobject);
323eda14cbcSMatt Macy 			assert(drrw->drr_offset == drrwb.drr_refoffset);
324eda14cbcSMatt Macy 
325eda14cbcSMatt Macy 			payload_size = DRR_WRITE_PAYLOAD_SIZE(drrw);
326eda14cbcSMatt Macy 			spread(infd, buf, payload_size,
327eda14cbcSMatt Macy 			    stream_offset + sizeof (*drr));
328eda14cbcSMatt Macy 
329eda14cbcSMatt Macy 			drrw->drr_toguid = drrwb.drr_toguid;
330eda14cbcSMatt Macy 			drrw->drr_object = drrwb.drr_object;
331eda14cbcSMatt Macy 			drrw->drr_offset = drrwb.drr_offset;
332eda14cbcSMatt Macy 			break;
333eda14cbcSMatt Macy 		}
334eda14cbcSMatt Macy 
335eda14cbcSMatt Macy 		case DRR_WRITE:
336eda14cbcSMatt Macy 		{
337eda14cbcSMatt Macy 			struct drr_write *drrw = &drr->drr_u.drr_write;
338eda14cbcSMatt Macy 			payload_size = DRR_WRITE_PAYLOAD_SIZE(drrw);
339eda14cbcSMatt Macy 			(void) sfread(buf, payload_size, ofp);
340eda14cbcSMatt Macy 
341eda14cbcSMatt Macy 			rdt_insert(&rdt, drrw->drr_toguid,
342eda14cbcSMatt Macy 			    drrw->drr_object, drrw->drr_offset, offset);
343eda14cbcSMatt Macy 			break;
344eda14cbcSMatt Macy 		}
345eda14cbcSMatt Macy 
346eda14cbcSMatt Macy 		case DRR_WRITE_EMBEDDED:
347eda14cbcSMatt Macy 		{
348eda14cbcSMatt Macy 			struct drr_write_embedded *drrwe =
349eda14cbcSMatt Macy 			    &drr->drr_u.drr_write_embedded;
350eda14cbcSMatt Macy 			payload_size =
351eda14cbcSMatt Macy 			    P2ROUNDUP((uint64_t)drrwe->drr_psize, 8);
352eda14cbcSMatt Macy 			(void) sfread(buf, payload_size, ofp);
353eda14cbcSMatt Macy 			break;
354eda14cbcSMatt Macy 		}
355eda14cbcSMatt Macy 
356eda14cbcSMatt Macy 		case DRR_FREEOBJECTS:
357eda14cbcSMatt Macy 		case DRR_FREE:
358eda14cbcSMatt Macy 		case DRR_OBJECT_RANGE:
359eda14cbcSMatt Macy 			break;
360eda14cbcSMatt Macy 
361eda14cbcSMatt Macy 		default:
362eda14cbcSMatt Macy 			(void) fprintf(stderr, "INVALID record type 0x%x\n",
363eda14cbcSMatt Macy 			    drr->drr_type);
364eda14cbcSMatt Macy 			/* should never happen, so assert */
365eda14cbcSMatt Macy 			assert(B_FALSE);
366eda14cbcSMatt Macy 		}
367eda14cbcSMatt Macy 
368eda14cbcSMatt Macy 		if (feof(ofp)) {
369eda14cbcSMatt Macy 			fprintf(stderr, "Error: unexpected end-of-file\n");
370eda14cbcSMatt Macy 			exit(1);
371eda14cbcSMatt Macy 		}
372eda14cbcSMatt Macy 		if (ferror(ofp)) {
373eda14cbcSMatt Macy 			fprintf(stderr, "Error while reading file: %s\n",
374eda14cbcSMatt Macy 			    strerror(errno));
375eda14cbcSMatt Macy 			exit(1);
376eda14cbcSMatt Macy 		}
377eda14cbcSMatt Macy 
378eda14cbcSMatt Macy 		/*
379eda14cbcSMatt Macy 		 * We need to recalculate the checksum, and it needs to be
380eda14cbcSMatt Macy 		 * initially zero to do that.  BEGIN records don't have
381eda14cbcSMatt Macy 		 * a checksum.
382eda14cbcSMatt Macy 		 */
383eda14cbcSMatt Macy 		if (drr->drr_type != DRR_BEGIN) {
384eda14cbcSMatt Macy 			bzero(&drr->drr_u.drr_checksum.drr_checksum,
385eda14cbcSMatt Macy 			    sizeof (drr->drr_u.drr_checksum.drr_checksum));
386eda14cbcSMatt Macy 		}
387eda14cbcSMatt Macy 		if (dump_record(drr, buf, payload_size,
388eda14cbcSMatt Macy 		    &stream_cksum, outfd) != 0)
389eda14cbcSMatt Macy 			break;
390eda14cbcSMatt Macy 		if (drr->drr_type == DRR_END) {
391eda14cbcSMatt Macy 			/*
392eda14cbcSMatt Macy 			 * Typically the END record is either the last
393eda14cbcSMatt Macy 			 * thing in the stream, or it is followed
394eda14cbcSMatt Macy 			 * by a BEGIN record (which also zeros the checksum).
395eda14cbcSMatt Macy 			 * However, a stream package ends with two END
396eda14cbcSMatt Macy 			 * records.  The last END record's checksum starts
397eda14cbcSMatt Macy 			 * from zero.
398eda14cbcSMatt Macy 			 */
399eda14cbcSMatt Macy 			ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0);
400eda14cbcSMatt Macy 		}
401eda14cbcSMatt Macy 		offset = ftell(ofp);
402eda14cbcSMatt Macy 	}
403eda14cbcSMatt Macy 
404eda14cbcSMatt Macy 	if (verbose) {
405eda14cbcSMatt Macy 		char mem_str[16];
406eda14cbcSMatt Macy 		zfs_nicenum(rdt.ddt_count * sizeof (redup_entry_t),
407eda14cbcSMatt Macy 		    mem_str, sizeof (mem_str));
408eda14cbcSMatt Macy 		fprintf(stderr, "converted stream with %llu total records, "
409eda14cbcSMatt Macy 		    "including %llu dedup records, using %sB memory.\n",
410eda14cbcSMatt Macy 		    (long long)num_records,
411eda14cbcSMatt Macy 		    (long long)num_write_byref_records,
412eda14cbcSMatt Macy 		    mem_str);
413eda14cbcSMatt Macy 	}
414eda14cbcSMatt Macy 
415eda14cbcSMatt Macy 	umem_cache_destroy(rdt.ddecache);
416eda14cbcSMatt Macy 	free(rdt.redup_hash_array);
417eda14cbcSMatt Macy 	free(buf);
418eda14cbcSMatt Macy 	(void) fclose(ofp);
419eda14cbcSMatt Macy }
420eda14cbcSMatt Macy 
421eda14cbcSMatt Macy int
422eda14cbcSMatt Macy zstream_do_redup(int argc, char *argv[])
423eda14cbcSMatt Macy {
424eda14cbcSMatt Macy 	boolean_t verbose = B_FALSE;
4257877fdebSMatt Macy 	int c;
426eda14cbcSMatt Macy 
427eda14cbcSMatt Macy 	while ((c = getopt(argc, argv, "v")) != -1) {
428eda14cbcSMatt Macy 		switch (c) {
429eda14cbcSMatt Macy 		case 'v':
430eda14cbcSMatt Macy 			verbose = B_TRUE;
431eda14cbcSMatt Macy 			break;
432eda14cbcSMatt Macy 		case '?':
433eda14cbcSMatt Macy 			(void) fprintf(stderr, "invalid option '%c'\n",
434eda14cbcSMatt Macy 			    optopt);
435eda14cbcSMatt Macy 			zstream_usage();
436eda14cbcSMatt Macy 			break;
437eda14cbcSMatt Macy 		}
438eda14cbcSMatt Macy 	}
439eda14cbcSMatt Macy 
440eda14cbcSMatt Macy 	argc -= optind;
441eda14cbcSMatt Macy 	argv += optind;
442eda14cbcSMatt Macy 
443eda14cbcSMatt Macy 	if (argc != 1)
444eda14cbcSMatt Macy 		zstream_usage();
445eda14cbcSMatt Macy 
446eda14cbcSMatt Macy 	const char *filename = argv[0];
447eda14cbcSMatt Macy 
448eda14cbcSMatt Macy 	if (isatty(STDOUT_FILENO)) {
449eda14cbcSMatt Macy 		(void) fprintf(stderr,
450eda14cbcSMatt Macy 		    "Error: Stream can not be written to a terminal.\n"
451eda14cbcSMatt Macy 		    "You must redirect standard output.\n");
452eda14cbcSMatt Macy 		return (1);
453eda14cbcSMatt Macy 	}
454eda14cbcSMatt Macy 
455eda14cbcSMatt Macy 	int fd = open(filename, O_RDONLY);
456eda14cbcSMatt Macy 	if (fd == -1) {
457eda14cbcSMatt Macy 		(void) fprintf(stderr,
458eda14cbcSMatt Macy 		    "Error while opening file '%s': %s\n",
459eda14cbcSMatt Macy 		    filename, strerror(errno));
460eda14cbcSMatt Macy 		exit(1);
461eda14cbcSMatt Macy 	}
462eda14cbcSMatt Macy 
463eda14cbcSMatt Macy 	fletcher_4_init();
464eda14cbcSMatt Macy 	zfs_redup_stream(fd, STDOUT_FILENO, verbose);
465eda14cbcSMatt Macy 	fletcher_4_fini();
466eda14cbcSMatt Macy 
467eda14cbcSMatt Macy 	close(fd);
468eda14cbcSMatt Macy 
469eda14cbcSMatt Macy 	return (0);
470eda14cbcSMatt Macy }
471