xref: /freebsd/stand/libsa/zfs/libzfs.h (revision 52fba9a943cca518d2978f7588e99a35da37e5f2)
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  * $FreeBSD$
27  */
28 
29 #ifndef _BOOT_LIBZFS_H_
30 #define _BOOT_LIBZFS_H_
31 
32 #include <zfsimpl.h>
33 
34 #ifdef LOADER_GELI_SUPPORT
35 #include <crypto/intake.h>
36 #endif
37 
38 #define	ZFS_MAXNAMELEN	256
39 
40 /*
41  * ZFS fully-qualified device descriptor.
42  */
43 struct zfs_devdesc {
44 	struct devdesc	dd;		/* Must be first. */
45 	uint64_t	pool_guid;
46 	uint64_t	root_guid;
47 };
48 
49 /* nvp implementation version */
50 #define	NV_VERSION		0
51 
52 /* nvlist persistent unique name flags, stored in nvl_nvflags */
53 #define	NV_UNIQUE_NAME		0x1
54 #define	NV_UNIQUE_NAME_TYPE	0x2
55 
56 #define	NV_ALIGN4(x)		(((x) + 3) & ~3)
57 #define	NV_ALIGN(x)		(((x) + 7) & ~7)
58 
59 /*
60  * nvlist header.
61  * nvlist has 4 bytes header followed by version and flags, then nvpairs
62  * and the list is terminated by double zero.
63  */
64 typedef struct {
65 	char nvh_encoding;
66 	char nvh_endian;
67 	char nvh_reserved1;
68 	char nvh_reserved2;
69 } nvs_header_t;
70 
71 typedef struct {
72 	nvs_header_t nv_header;
73 	size_t nv_asize;
74 	size_t nv_size;
75 	uint8_t *nv_data;
76 	uint8_t *nv_idx;
77 } nvlist_t;
78 
79 /*
80  * nvpair header.
81  * nvpair has encoded and decoded size
82  * name string (size and data)
83  * data type and number of elements
84  * data
85  */
86 typedef struct {
87 	unsigned encoded_size;
88 	unsigned decoded_size;
89 } nvp_header_t;
90 
91 /*
92  * nvlist stream head.
93  */
94 typedef struct {
95 	unsigned nvl_version;
96 	unsigned nvl_nvflag;
97 	nvp_header_t nvl_pair;
98 } nvs_data_t;
99 
100 typedef struct {
101 	unsigned nv_size;
102 	uint8_t nv_data[];	/* NV_ALIGN4(string) */
103 } nv_string_t;
104 
105 typedef struct {
106 	unsigned nv_type;	/* data_type_t */
107 	unsigned nv_nelem;	/* number of elements */
108 	uint8_t nv_data[];	/* data stream */
109 } nv_pair_data_t;
110 
111 nvlist_t *nvlist_create(int);
112 void nvlist_destroy(nvlist_t *);
113 nvlist_t *nvlist_import(const char *, size_t);
114 int nvlist_export(nvlist_t *);
115 int nvlist_remove(nvlist_t *, const char *, data_type_t);
116 int nvpair_type_from_name(const char *);
117 nvp_header_t *nvpair_find(nvlist_t *, const char *);
118 void nvpair_print(nvp_header_t *, unsigned int);
119 void nvlist_print(const nvlist_t *, unsigned int);
120 char *nvstring_get(nv_string_t *);
121 int nvlist_find(const nvlist_t *, const char *, data_type_t,
122     int *, void *, int *);
123 nvp_header_t *nvlist_next_nvpair(nvlist_t *, nvp_header_t *);
124 
125 int nvlist_add_boolean_value(nvlist_t *, const char *, boolean_t);
126 int nvlist_add_byte(nvlist_t *, const char *, uint8_t);
127 int nvlist_add_int8(nvlist_t *, const char *, int8_t);
128 int nvlist_add_uint8(nvlist_t *, const char *, uint8_t);
129 int nvlist_add_int16(nvlist_t *, const char *, int16_t);
130 int nvlist_add_uint16(nvlist_t *, const char *, uint16_t);
131 int nvlist_add_int32(nvlist_t *, const char *, int32_t);
132 int nvlist_add_uint32(nvlist_t *, const char *, uint32_t);
133 int nvlist_add_int64(nvlist_t *, const char *, int64_t);
134 int nvlist_add_uint64(nvlist_t *, const char *, uint64_t);
135 int nvlist_add_string(nvlist_t *, const char *, const char *);
136 int nvlist_add_boolean_array(nvlist_t *, const char *, boolean_t *, uint32_t);
137 int nvlist_add_byte_array(nvlist_t *, const char *, uint8_t *, uint32_t);
138 int nvlist_add_int8_array(nvlist_t *, const char *, int8_t *, uint32_t);
139 int nvlist_add_uint8_array(nvlist_t *, const char *, uint8_t *, uint32_t);
140 int nvlist_add_int16_array(nvlist_t *, const char *, int16_t *, uint32_t);
141 int nvlist_add_uint16_array(nvlist_t *, const char *, uint16_t *, uint32_t);
142 int nvlist_add_int32_array(nvlist_t *, const char *, int32_t *, uint32_t);
143 int nvlist_add_uint32_array(nvlist_t *, const char *, uint32_t *, uint32_t);
144 int nvlist_add_int64_array(nvlist_t *, const char *, int64_t *, uint32_t);
145 int nvlist_add_uint64_array(nvlist_t *, const char *, uint64_t *, uint32_t);
146 int nvlist_add_string_array(nvlist_t *, const char *, char * const *, uint32_t);
147 int nvlist_add_nvlist(nvlist_t *, const char *, nvlist_t *);
148 int nvlist_add_nvlist_array(nvlist_t *, const char *, nvlist_t **, uint32_t);
149 
150 int	zfs_parsedev(struct zfs_devdesc *dev, const char *devspec,
151 		     const char **path);
152 char	*zfs_fmtdev(void *vdev);
153 int	zfs_probe_dev(const char *devname, uint64_t *pool_guid);
154 int	zfs_list(const char *name);
155 int	zfs_get_bootonce(void *, const char *, char *, size_t);
156 int	zfs_get_bootenv(void *, nvlist_t **);
157 int	zfs_set_bootenv(void *, nvlist_t *);
158 int	zfs_attach_nvstore(void *);
159 uint64_t ldi_get_size(void *);
160 void	init_zfs_boot_options(const char *currdev);
161 
162 int	zfs_bootenv(const char *name);
163 int	zfs_attach_nvstore(void *);
164 int	zfs_belist_add(const char *name, uint64_t __unused);
165 int	zfs_set_env(void);
166 
167 nvlist_t *vdev_read_bootenv(vdev_t *);
168 
169 extern struct devsw zfs_dev;
170 extern struct fs_ops zfs_fsops;
171 
172 #endif /*_BOOT_LIBZFS_H_*/
173