xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs.h (revision f2a3c691e1fab4dee486fd83642311ec59dc3732)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_LIBZFS_H
27 #define	_LIBZFS_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <assert.h>
32 #include <libnvpair.h>
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/varargs.h>
36 #include <sys/fs/zfs.h>
37 
38 #ifdef	__cplusplus
39 extern "C" {
40 #endif
41 
42 /*
43  * Miscellaneous ZFS constants
44  */
45 #define	ZFS_MAXNAMELEN		MAXNAMELEN
46 #define	ZPOOL_MAXNAMELEN	MAXNAMELEN
47 #define	ZFS_MAXPROPLEN		MAXPATHLEN
48 
49 /*
50  * Basic handle types
51  */
52 typedef struct zfs_handle zfs_handle_t;
53 typedef struct zpool_handle zpool_handle_t;
54 
55 /*
56  * Basic handle functions
57  */
58 extern zpool_handle_t *zpool_open(const char *);
59 extern zpool_handle_t *zpool_open_canfail(const char *);
60 extern void zpool_close(zpool_handle_t *);
61 extern const char *zpool_get_name(zpool_handle_t *);
62 extern uint64_t zpool_get_guid(zpool_handle_t *);
63 extern uint64_t zpool_get_space_used(zpool_handle_t *);
64 extern uint64_t zpool_get_space_total(zpool_handle_t *);
65 extern int zpool_get_root(zpool_handle_t *, char *, size_t);
66 extern int zpool_get_state(zpool_handle_t *);
67 
68 /*
69  * Iterate over all active pools in the system.
70  */
71 typedef int (*zpool_iter_f)(zpool_handle_t *, void *);
72 extern int zpool_iter(zpool_iter_f, void *);
73 
74 /*
75  * Functions to create and destroy pools
76  */
77 extern int zpool_create(const char *, nvlist_t *, const char *);
78 extern int zpool_destroy(zpool_handle_t *);
79 extern int zpool_add(zpool_handle_t *, nvlist_t *);
80 
81 /*
82  * Functions to manipulate pool and vdev state
83  */
84 extern int zpool_scrub(zpool_handle_t *, pool_scrub_type_t);
85 
86 extern int zpool_vdev_online(zpool_handle_t *, const char *);
87 extern int zpool_vdev_offline(zpool_handle_t *, const char *, int);
88 extern int zpool_vdev_attach(zpool_handle_t *, const char *, const char *,
89     nvlist_t *, int);
90 extern int zpool_vdev_detach(zpool_handle_t *, const char *);
91 extern int zpool_clear(zpool_handle_t *, const char *);
92 extern uint64_t zpool_vdev_to_guid(zpool_handle_t *, const char *);
93 
94 /*
95  * Pool health statistics.
96  */
97 typedef enum {
98 	/*
99 	 * The following correspond to faults as defined in the (fault.fs.zfs.*)
100 	 * event namespace.  Each is associated with a corresponding message ID.
101 	 */
102 	ZPOOL_STATUS_CORRUPT_CACHE,	/* corrupt /kernel/drv/zpool.cache */
103 	ZPOOL_STATUS_MISSING_DEV_R,	/* missing device with replicas */
104 	ZPOOL_STATUS_MISSING_DEV_NR,	/* missing device with no replicas */
105 	ZPOOL_STATUS_CORRUPT_LABEL_R,	/* bad device label with replicas */
106 	ZPOOL_STATUS_CORRUPT_LABEL_NR,	/* bad device label with no replicas */
107 	ZPOOL_STATUS_BAD_GUID_SUM,	/* sum of device guids didn't match */
108 	ZPOOL_STATUS_CORRUPT_POOL,	/* pool metadata is corrupted */
109 	ZPOOL_STATUS_CORRUPT_DATA,	/* data errors in user (meta)data */
110 	ZPOOL_STATUS_FAILING_DEV,	/* device experiencing errors */
111 	ZPOOL_STATUS_VERSION_MISMATCH,	/* bad on-disk version */
112 
113 	/*
114 	 * The following are not faults per se, but still an error possibly
115 	 * requiring administrative attention.  There is no corresponding
116 	 * message ID.
117 	 */
118 	ZPOOL_STATUS_RESILVERING,	/* device being resilvered */
119 	ZPOOL_STATUS_OFFLINE_DEV,	/* device online */
120 
121 	/*
122 	 * Finally, the following indicates a healthy pool.
123 	 */
124 	ZPOOL_STATUS_OK
125 } zpool_status_t;
126 
127 extern zpool_status_t zpool_get_status(zpool_handle_t *, char **);
128 extern zpool_status_t zpool_import_status(nvlist_t *, char **);
129 
130 /*
131  * Statistics and configuration functions.
132  */
133 extern nvlist_t *zpool_get_config(zpool_handle_t *, nvlist_t **);
134 extern int zpool_refresh_stats(zpool_handle_t *);
135 extern int zpool_get_errlog(zpool_handle_t *, nvlist_t ***, size_t *);
136 
137 #define	ZPOOL_ERR_DATASET	"dataset"
138 #define	ZPOOL_ERR_OBJECT	"object"
139 #define	ZPOOL_ERR_RANGE		"range"
140 
141 /*
142  * Import and export functions
143  */
144 extern int zpool_export(zpool_handle_t *);
145 extern int zpool_import(nvlist_t *, const char *, const char *);
146 
147 /*
148  * Search for pools to import
149  */
150 extern nvlist_t *zpool_find_import(int, char **);
151 
152 /*
153  * Miscellaneous pool functions
154  */
155 extern char *zpool_vdev_name(zpool_handle_t *, nvlist_t *);
156 
157 /*
158  * Basic handle manipulations.  These functions do not create or destroy the
159  * underlying datasets, only the references to them.
160  */
161 extern zfs_handle_t *zfs_open(const char *, int);
162 extern void zfs_close(zfs_handle_t *);
163 extern zfs_type_t zfs_get_type(const zfs_handle_t *);
164 extern const char *zfs_get_name(const zfs_handle_t *);
165 
166 typedef enum {
167 	ZFS_SRC_NONE = 0x1,
168 	ZFS_SRC_DEFAULT = 0x2,
169 	ZFS_SRC_TEMPORARY = 0x4,
170 	ZFS_SRC_LOCAL = 0x8,
171 	ZFS_SRC_INHERITED = 0x10
172 } zfs_source_t;
173 
174 #define	ZFS_SRC_ALL	0x1f
175 
176 /*
177  * Property management functions.  Some functions are shared with the kernel,
178  * and are found in sys/fs/zfs.h.
179  */
180 const char *zfs_prop_to_name(zfs_prop_t);
181 int zfs_prop_set(zfs_handle_t *, zfs_prop_t, const char *);
182 int zfs_prop_get(zfs_handle_t *, zfs_prop_t, char *, size_t, zfs_source_t *,
183     char *, size_t, int);
184 int zfs_prop_get_numeric(zfs_handle_t *, zfs_prop_t, uint64_t *, zfs_source_t *,
185     char *, size_t);
186 uint64_t zfs_prop_get_int(zfs_handle_t *, zfs_prop_t);
187 int zfs_prop_validate(zfs_prop_t, const char *, uint64_t *);
188 int zfs_prop_inheritable(zfs_prop_t);
189 int zfs_prop_inherit(zfs_handle_t *, zfs_prop_t);
190 const char *zfs_prop_values(zfs_prop_t);
191 int zfs_prop_valid_for_type(zfs_prop_t, int);
192 const char *zfs_prop_default_string(zfs_prop_t prop);
193 uint64_t zfs_prop_default_numeric(zfs_prop_t);
194 int zfs_prop_is_string(zfs_prop_t prop);
195 const char *zfs_prop_column_name(zfs_prop_t);
196 const char *zfs_prop_column_format(zfs_prop_t);
197 int zfs_get_proplist(char *fields, zfs_prop_t *proplist, int max, int *count,
198     char **badopt);
199 
200 #define	ZFS_MOUNTPOINT_NONE	"none"
201 #define	ZFS_MOUNTPOINT_LEGACY	"legacy"
202 
203 /*
204  * Iterator functions.
205  */
206 typedef int (*zfs_iter_f)(zfs_handle_t *, void *);
207 extern int zfs_iter_root(zfs_iter_f, void *);
208 extern int zfs_iter_children(zfs_handle_t *, zfs_iter_f, void *);
209 extern int zfs_iter_dependents(zfs_handle_t *, zfs_iter_f, void *);
210 extern int zfs_iter_filesystems(zfs_handle_t *, zfs_iter_f, void *);
211 extern int zfs_iter_snapshots(zfs_handle_t *, zfs_iter_f, void *);
212 
213 /*
214  * Functions to create and destroy datasets.
215  */
216 extern int zfs_create(const char *, zfs_type_t, const char *, const char *);
217 extern int zfs_destroy(zfs_handle_t *);
218 extern int zfs_clone(zfs_handle_t *, const char *);
219 extern int zfs_snapshot(const char *);
220 extern int zfs_rollback(zfs_handle_t *, zfs_handle_t *, int);
221 extern int zfs_rename(zfs_handle_t *, const char *);
222 extern int zfs_send(zfs_handle_t *, zfs_handle_t *);
223 extern int zfs_receive(const char *, int, int, int);
224 
225 /*
226  * Miscellaneous functions.
227  */
228 extern const char *zfs_type_to_name(zfs_type_t);
229 extern void zfs_refresh_properties(zfs_handle_t *);
230 extern int zfs_name_valid(const char *, zfs_type_t);
231 
232 /*
233  * Mount support functions.
234  */
235 extern int zfs_is_mounted(zfs_handle_t *, char **);
236 extern int zfs_mount(zfs_handle_t *, const char *, int);
237 extern int zfs_unmount(zfs_handle_t *, const char *, int);
238 extern int zfs_unmountall(zfs_handle_t *, int);
239 
240 /*
241  * Share support functions.
242  */
243 extern int zfs_is_shared(zfs_handle_t *, char **);
244 extern int zfs_share(zfs_handle_t *);
245 extern int zfs_unshare(zfs_handle_t *, const char *);
246 extern int zfs_unshareall(zfs_handle_t *);
247 
248 /*
249  * For clients that need to capture error output.
250  */
251 extern void zfs_set_error_handler(void (*)(const char *, va_list));
252 
253 /*
254  * When dealing with nvlists, verify() is extremely useful
255  */
256 #ifdef NDEBUG
257 #define	verify(EX)	((void)(EX))
258 #else
259 #define	verify(EX)	assert(EX)
260 #endif
261 
262 /*
263  * Utility function to convert a number to a human-readable form.
264  */
265 extern void zfs_nicenum(uint64_t, char *, size_t);
266 extern int zfs_nicestrtonum(const char *, uint64_t *);
267 
268 /*
269  * Pool destroy special.  Remove the device information without destroying
270  * the underlying dataset.
271  */
272 extern int zfs_remove_link(zfs_handle_t *);
273 
274 /*
275  * Given a device or file, determine if it is part of a pool.
276  */
277 extern int zpool_in_use(int fd, pool_state_t *state, char **name);
278 
279 /*
280  * ftyp special.  Read the label from a given device.
281  */
282 extern nvlist_t *zpool_read_label(int fd);
283 
284 /*
285  * Create and remove zvol /dev links
286  */
287 extern int zpool_create_zvol_links(zpool_handle_t *);
288 extern int zpool_remove_zvol_links(zpool_handle_t *);
289 
290 /*
291  * zoneadmd hack
292  */
293 extern void zfs_init(void);
294 
295 /*
296  * Useful defines
297  */
298 #ifndef TRUE
299 #define	TRUE	1
300 #endif
301 #ifndef FALSE
302 #define	FALSE	0
303 #endif
304 
305 #ifdef	__cplusplus
306 }
307 #endif
308 
309 #endif	/* _LIBZFS_H */
310