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