xref: /freebsd/stand/libsa/zfs/nvlist.h (revision 9f44a47fd07924afc035991af15d84e6585dea4f)
1 /*-
2  * Copyright 2020 Toomas Soome <tsoome@me.com>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #ifndef _BOOT_NVLIST_H_
27 #define	_BOOT_NVLIST_H_
28 
29 typedef enum {
30 	DATA_TYPE_UNKNOWN = 0,
31 	DATA_TYPE_BOOLEAN,
32 	DATA_TYPE_BYTE,
33 	DATA_TYPE_INT16,
34 	DATA_TYPE_UINT16,
35 	DATA_TYPE_INT32,
36 	DATA_TYPE_UINT32,
37 	DATA_TYPE_INT64,
38 	DATA_TYPE_UINT64,
39 	DATA_TYPE_STRING,
40 	DATA_TYPE_BYTE_ARRAY,
41 	DATA_TYPE_INT16_ARRAY,
42 	DATA_TYPE_UINT16_ARRAY,
43 	DATA_TYPE_INT32_ARRAY,
44 	DATA_TYPE_UINT32_ARRAY,
45 	DATA_TYPE_INT64_ARRAY,
46 	DATA_TYPE_UINT64_ARRAY,
47 	DATA_TYPE_STRING_ARRAY,
48 	DATA_TYPE_HRTIME,
49 	DATA_TYPE_NVLIST,
50 	DATA_TYPE_NVLIST_ARRAY,
51 	DATA_TYPE_BOOLEAN_VALUE,
52 	DATA_TYPE_INT8,
53 	DATA_TYPE_UINT8,
54 	DATA_TYPE_BOOLEAN_ARRAY,
55 	DATA_TYPE_INT8_ARRAY,
56 	DATA_TYPE_UINT8_ARRAY
57 } data_type_t;
58 
59 /* nvp implementation version */
60 #define	NV_VERSION		0
61 
62 /* nvlist pack encoding */
63 #define	NV_ENCODE_NATIVE	0
64 #define	NV_ENCODE_XDR		1
65 
66 /* nvlist persistent unique name flags, stored in nvl_nvflags */
67 #define	NV_UNIQUE_NAME		0x1
68 #define	NV_UNIQUE_NAME_TYPE	0x2
69 
70 #define	NV_ALIGN4(x)		(((x) + 3) & ~3)
71 #define	NV_ALIGN(x)		(((x) + 7) & ~7)
72 
73 /*
74  * nvlist header.
75  * nvlist has 4 bytes header followed by version and flags, then nvpairs
76  * and the list is terminated by double zero.
77  */
78 typedef struct {
79 	char nvh_encoding;
80 	char nvh_endian;
81 	char nvh_reserved1;
82 	char nvh_reserved2;
83 } nvs_header_t;
84 
85 typedef struct {
86 	nvs_header_t nv_header;
87 	size_t nv_asize;
88 	size_t nv_size;
89 	uint8_t *nv_data;
90 	uint8_t *nv_idx;
91 } nvlist_t;
92 
93 /*
94  * nvpair header.
95  * nvpair has encoded and decoded size
96  * name string (size and data)
97  * data type and number of elements
98  * data
99  */
100 typedef struct {
101 	unsigned encoded_size;
102 	unsigned decoded_size;
103 } nvp_header_t;
104 
105 /*
106  * nvlist stream head.
107  */
108 typedef struct {
109 	unsigned nvl_version;
110 	unsigned nvl_nvflag;
111 	nvp_header_t nvl_pair;
112 } nvs_data_t;
113 
114 typedef struct {
115 	unsigned nv_size;
116 	uint8_t nv_data[];	/* NV_ALIGN4(string) */
117 } nv_string_t;
118 
119 typedef struct {
120 	unsigned nv_type;	/* data_type_t */
121 	unsigned nv_nelem;	/* number of elements */
122 	uint8_t nv_data[];	/* data stream */
123 } nv_pair_data_t;
124 
125 nvlist_t *nvlist_create(int);
126 void nvlist_destroy(nvlist_t *);
127 nvlist_t *nvlist_import(const char *, size_t);
128 int nvlist_export(nvlist_t *);
129 int nvlist_remove(nvlist_t *, const char *, data_type_t);
130 int nvpair_type_from_name(const char *);
131 nvp_header_t *nvpair_find(nvlist_t *, const char *);
132 void nvpair_print(nvp_header_t *, unsigned int);
133 void nvlist_print(const nvlist_t *, unsigned int);
134 char *nvstring_get(nv_string_t *);
135 int nvlist_find(const nvlist_t *, const char *, data_type_t,
136     int *, void *, int *);
137 nvp_header_t *nvlist_next_nvpair(nvlist_t *, nvp_header_t *);
138 
139 int nvlist_add_boolean_value(nvlist_t *, const char *, int);
140 int nvlist_add_byte(nvlist_t *, const char *, uint8_t);
141 int nvlist_add_int8(nvlist_t *, const char *, int8_t);
142 int nvlist_add_uint8(nvlist_t *, const char *, uint8_t);
143 int nvlist_add_int16(nvlist_t *, const char *, int16_t);
144 int nvlist_add_uint16(nvlist_t *, const char *, uint16_t);
145 int nvlist_add_int32(nvlist_t *, const char *, int32_t);
146 int nvlist_add_uint32(nvlist_t *, const char *, uint32_t);
147 int nvlist_add_int64(nvlist_t *, const char *, int64_t);
148 int nvlist_add_uint64(nvlist_t *, const char *, uint64_t);
149 int nvlist_add_string(nvlist_t *, const char *, const char *);
150 int nvlist_add_boolean_array(nvlist_t *, const char *, int *, uint32_t);
151 int nvlist_add_byte_array(nvlist_t *, const char *, uint8_t *, uint32_t);
152 int nvlist_add_int8_array(nvlist_t *, const char *, int8_t *, uint32_t);
153 int nvlist_add_uint8_array(nvlist_t *, const char *, uint8_t *, uint32_t);
154 int nvlist_add_int16_array(nvlist_t *, const char *, int16_t *, uint32_t);
155 int nvlist_add_uint16_array(nvlist_t *, const char *, uint16_t *, uint32_t);
156 int nvlist_add_int32_array(nvlist_t *, const char *, int32_t *, uint32_t);
157 int nvlist_add_uint32_array(nvlist_t *, const char *, uint32_t *, uint32_t);
158 int nvlist_add_int64_array(nvlist_t *, const char *, int64_t *, uint32_t);
159 int nvlist_add_uint64_array(nvlist_t *, const char *, uint64_t *, uint32_t);
160 int nvlist_add_string_array(nvlist_t *, const char *, char * const *, uint32_t);
161 int nvlist_add_nvlist(nvlist_t *, const char *, nvlist_t *);
162 int nvlist_add_nvlist_array(nvlist_t *, const char *, nvlist_t **, uint32_t);
163 
164 #endif /* !_BOOT_NVLIST_H_ */
165