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