xref: /freebsd/sys/contrib/openzfs/cmd/zstream/zstream_fletcher4.h (revision d0b3ecdc274930e190ea233b6b69ff03782eaf8d)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * This file and its contents are supplied under the terms of the Common
6  * Development and Distribution License ("CDDL"), version 1.0. You may only use
7  * this file in accordance with the terms of version 1.0 of the CDDL.
8  *
9  * A full copy of the text of the CDDL should have accompanied this source. A
10  * copy of the CDDL is also available via the Internet at
11  * http://www.illumos.org/license/CDDL.
12  *
13  * CDDL HEADER END
14  */
15 
16 /*
17  * Copyright (c) 2026 by Garth Snyder. All rights reserved.
18  */
19 
20 #ifndef _ZSTREAM_FLETCHER4_H
21 #define	_ZSTREAM_FLETCHER4_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #include "zstream_io.h"
28 
29 /*
30  * zstream_chain module for calculating, validating, and inscribing
31  * Fletcher4 checksums.
32  *
33  * serial_validate_fletcher4() validates record checksums against the
34  * running stream checksum and fails loudly on any mismatch.
35  *
36  * serial_add_fletcher4() inscribes record checksums from the running
37  * stream checksum, in theory replacing whatever was there before. But
38  * see note in zstream_fletcher4.c regarding zero checksums generated
39  * by send_conclusion_record(), which are preserved.
40  */
41 
42 /*
43  * DRR_END records normally do have end-record checksums. However, records
44  * emitted by send_conclusion_record() in libzfs_sendrecv.c have the
45  * checksum set to zero. zfs receive ignores those checksums. DRR_END
46  * records also have an internal checksum that applies to the stream-to-date
47  * since the most recent DRR_BEGIN.
48  *
49  * Ideally, null zstream transformations should be idempotent. E.g., a
50  * zstream redup that does not redup anything should yield a stream that is
51  * identical to the original stream. So, it's helpful to emulate zfs send's
52  * checksumming practices just to minimize spurious differences between
53  * input and output streams.
54  *
55  * The IS_CONCLUSION macro recognizes the records generated by
56  * send_conclusion_record() so that they can be treated specially.
57  */
58 #define	IS_CONCLUSION(drr, type)					       \
59 	    ((type) == DRR_END &&					       \
60 	    (drr)->drr_u.drr_end.drr_toguid == 0 &&			       \
61 	    ZIO_CHECKSUM_IS_ZERO(&(drr)->drr_u.drr_checksum.drr_checksum))
62 
63 /*
64  * Maximum number of checksum operations in one chain
65  */
66 #define	MAX_FLETCHER_4 8
67 
68 chain_step_t
69 serial_validate_fletcher4(void);
70 
71 chain_step_t
72 serial_add_fletcher4(void);
73 
74 #ifdef __cplusplus
75 }
76 #endif
77 
78 #endif  /* _ZSTREAM_FLETCHER4_H */
79