1 // SPDX-License-Identifier: CDDL-1.0
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 * https://opensource.org/license/CDDL-1.0.
11 */
12
13 /*
14 * Copyright (c) 2026 by Garth Snyder. All rights reserved.
15 */
16
17 #ifndef _ZSTREAM_UTIL_H
18 #define _ZSTREAM_UTIL_H
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 #include <assert.h>
25 #include <pthread.h>
26 #include <signal.h>
27 #include <stddef.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <sys/types.h>
31 #include <sys/stdtypes.h>
32 #include <sys/spa_checksum.h>
33 #include <sys/zfs_ioctl.h>
34 #include <sys/zio_checksum.h>
35 #include <sys/zio_compress.h>
36
37 typedef struct {
38 enum zio_compress cs_type;
39 int cs_level;
40 } compression_spec_t;
41
42 typedef void *
43 thread_f(void *);
44
45 /*
46 * The safe_ versions of the functions below terminate the process if the
47 * operation doesn't succeed instead of returning an error.
48 */
49 void *
50 safe_malloc(size_t size);
51
52 void *
53 safe_calloc(size_t n);
54
55 void
56 safe_pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset);
57
58 pthread_t
59 safe_create_thread(thread_f *body, void *body_arg, const char *name,
60 boolean_t detach);
61
62 char *
63 checksum_str(zio_cksum_t *cksum, char *buff, size_t buff_size);
64
65 /*
66 * Prints an error message if checksums don't match. Returns B_TRUE for
67 * a match, B_FALSE otherwise.
68 */
69 boolean_t
70 validate_checksum(zio_cksum_t *expect, zio_cksum_t *actual, boolean_t swap,
71 const char *where, off_t stream_offset);
72
73 static inline void
validate_or_exit(zio_cksum_t * expect,zio_cksum_t * actual,boolean_t swap,const char * where,off_t stream_offset)74 validate_or_exit(zio_cksum_t *expect, zio_cksum_t *actual, boolean_t swap,
75 const char *where, off_t stream_offset)
76 {
77 if (!validate_checksum(expect, actual, swap, where, stream_offset)) {
78 exit(1);
79 }
80 }
81
82 /*
83 * Determine whether a compression type indicates no compression
84 */
85 static inline boolean_t
ctype_is_uncompressed(enum zio_compress ct)86 ctype_is_uncompressed(enum zio_compress ct)
87 {
88 VERIFY3U((int)ct, <, (int)ZIO_COMPRESS_FUNCTIONS);
89 return (zio_compress_table[(int)(ct)].ci_compress == NULL);
90 }
91
92 boolean_t
93 write_is_encrypted(struct drr_write *drrw);
94
95 uint8_t *
96 decompress_buffer(uint8_t *inbuff, size_t inbuff_size, size_t logical_size,
97 enum zio_compress compress_type);
98
99 uint8_t *
100 compress_buffer(uint8_t *inbuff, size_t inbuff_size,
101 compression_spec_t compress_type, size_t *compressed_size);
102
103 #ifdef __cplusplus
104 }
105 #endif
106
107 #endif /* _ZSTREAM_UTIL_H */
108