xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs.h (revision 228975cc44c2290cc190960e5894ac6ce0863855)
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 /*
23  * Copyright 2007 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 #include <sys/avl.h>
39 #include <libuutil.h>
40 #include <ucred.h>
41 
42 #ifdef	__cplusplus
43 extern "C" {
44 #endif
45 
46 /*
47  * Miscellaneous ZFS constants
48  */
49 #define	ZFS_MAXNAMELEN		MAXNAMELEN
50 #define	ZPOOL_MAXNAMELEN	MAXNAMELEN
51 #define	ZFS_MAXPROPLEN		MAXPATHLEN
52 
53 /*
54  * libzfs errors
55  */
56 enum {
57 	EZFS_NOMEM = 2000,	/* out of memory */
58 	EZFS_BADPROP,		/* invalid property value */
59 	EZFS_PROPREADONLY,	/* cannot set readonly property */
60 	EZFS_PROPTYPE,		/* property does not apply to dataset type */
61 	EZFS_PROPNONINHERIT,	/* property is not inheritable */
62 	EZFS_PROPSPACE,		/* bad quota or reservation */
63 	EZFS_BADTYPE,		/* dataset is not of appropriate type */
64 	EZFS_BUSY,		/* pool or dataset is busy */
65 	EZFS_EXISTS,		/* pool or dataset already exists */
66 	EZFS_NOENT,		/* no such pool or dataset */
67 	EZFS_BADSTREAM,		/* bad backup stream */
68 	EZFS_DSREADONLY,	/* dataset is readonly */
69 	EZFS_VOLTOOBIG,		/* volume is too large for 32-bit system */
70 	EZFS_VOLHASDATA,	/* volume already contains data */
71 	EZFS_INVALIDNAME,	/* invalid dataset name */
72 	EZFS_BADRESTORE,	/* unable to restore to destination */
73 	EZFS_BADBACKUP,		/* backup failed */
74 	EZFS_BADTARGET,		/* bad attach/detach/replace target */
75 	EZFS_NODEVICE,		/* no such device in pool */
76 	EZFS_BADDEV,		/* invalid device to add */
77 	EZFS_NOREPLICAS,	/* no valid replicas */
78 	EZFS_RESILVERING,	/* currently resilvering */
79 	EZFS_BADVERSION,	/* unsupported version */
80 	EZFS_POOLUNAVAIL,	/* pool is currently unavailable */
81 	EZFS_DEVOVERFLOW,	/* too many devices in one vdev */
82 	EZFS_BADPATH,		/* must be an absolute path */
83 	EZFS_CROSSTARGET,	/* rename or clone across pool or dataset */
84 	EZFS_ZONED,		/* used improperly in local zone */
85 	EZFS_MOUNTFAILED,	/* failed to mount dataset */
86 	EZFS_UMOUNTFAILED,	/* failed to unmount dataset */
87 	EZFS_UNSHARENFSFAILED,	/* unshare(1M) failed */
88 	EZFS_SHARENFSFAILED,	/* share(1M) failed */
89 	EZFS_DEVLINKS,		/* failed to create zvol links */
90 	EZFS_PERM,		/* permission denied */
91 	EZFS_NOSPC,		/* out of space */
92 	EZFS_IO,		/* I/O error */
93 	EZFS_INTR,		/* signal received */
94 	EZFS_ISSPARE,		/* device is a hot spare */
95 	EZFS_INVALCONFIG,	/* invalid vdev configuration */
96 	EZFS_RECURSIVE,		/* recursive dependency */
97 	EZFS_NOHISTORY,		/* no history object */
98 	EZFS_UNSHAREISCSIFAILED, /* iscsitgtd failed request to unshare */
99 	EZFS_SHAREISCSIFAILED,	/* iscsitgtd failed request to share */
100 	EZFS_POOLPROPS,		/* couldn't retrieve pool props */
101 	EZFS_POOL_NOTSUP,	/* ops not supported for this type of pool */
102 	EZFS_POOL_INVALARG,	/* invalid argument for this pool operation */
103 	EZFS_NAMETOOLONG,	/* dataset name is too long */
104 	EZFS_OPENFAILED,	/* open of device failed */
105 	EZFS_NOCAP,		/* couldn't get capacity */
106 	EZFS_LABELFAILED,	/* write of label failed */
107 	EZFS_ISCSISVCUNAVAIL,	/* iscsi service unavailable */
108 	EZFS_BADWHO,		/* invalid permission who */
109 	EZFS_BADPERM,		/* invalid permission */
110 	EZFS_BADPERMSET,	/* invalid permission set name */
111 	EZFS_PERMSET_CIRCULAR,	/* circular dependency on permset */
112 	EZFS_NODELEGATION,	/* delegated administration is disabled */
113 	EZFS_PERMRDONLY,	/* pemissions are readonly */
114 	EZFS_UNKNOWN
115 };
116 
117 /*
118  * The following data structures are all part
119  * of the zfs_allow_t data structure which is
120  * used for printing 'allow' permissions.
121  * It is a linked list of zfs_allow_t's which
122  * then contain avl tree's for user/group/sets/...
123  * and each one of the entries in those trees have
124  * avl tree's for the permissions they belong to and
125  * whether they are local,descendent or local+descendent
126  * permissions.  The AVL trees are used primarily for
127  * sorting purposes, but also so that we can quickly find
128  * a given user and or permission.
129  */
130 typedef struct zfs_perm_node {
131 	avl_node_t z_node;
132 	char z_pname[MAXPATHLEN];
133 } zfs_perm_node_t;
134 
135 typedef struct zfs_allow_node {
136 	avl_node_t z_node;
137 	char z_key[MAXPATHLEN];		/* name, such as joe */
138 	avl_tree_t z_localdescend;	/* local+descendent perms */
139 	avl_tree_t z_local;		/* local permissions */
140 	avl_tree_t z_descend;		/* descendent permissions */
141 } zfs_allow_node_t;
142 
143 typedef struct zfs_allow {
144 	struct zfs_allow *z_next;
145 	char z_setpoint[MAXPATHLEN];
146 	avl_tree_t z_sets;
147 	avl_tree_t z_crperms;
148 	avl_tree_t z_user;
149 	avl_tree_t z_group;
150 	avl_tree_t z_everyone;
151 } zfs_allow_t;
152 
153 /*
154  * Basic handle types
155  */
156 typedef struct zfs_handle zfs_handle_t;
157 typedef struct zpool_handle zpool_handle_t;
158 typedef struct libzfs_handle libzfs_handle_t;
159 
160 /*
161  * Library initialization
162  */
163 extern libzfs_handle_t *libzfs_init(void);
164 extern void libzfs_fini(libzfs_handle_t *);
165 
166 extern libzfs_handle_t *zpool_get_handle(zpool_handle_t *);
167 extern libzfs_handle_t *zfs_get_handle(zfs_handle_t *);
168 
169 extern void libzfs_print_on_error(libzfs_handle_t *, boolean_t);
170 
171 extern int libzfs_errno(libzfs_handle_t *);
172 extern const char *libzfs_error_action(libzfs_handle_t *);
173 extern const char *libzfs_error_description(libzfs_handle_t *);
174 
175 /*
176  * Basic handle functions
177  */
178 extern zpool_handle_t *zpool_open(libzfs_handle_t *, const char *);
179 extern zpool_handle_t *zpool_open_canfail(libzfs_handle_t *, const char *);
180 extern void zpool_close(zpool_handle_t *);
181 extern const char *zpool_get_name(zpool_handle_t *);
182 extern uint64_t zpool_get_guid(zpool_handle_t *);
183 extern uint64_t zpool_get_space_used(zpool_handle_t *);
184 extern uint64_t zpool_get_space_total(zpool_handle_t *);
185 extern int zpool_get_root(zpool_handle_t *, char *, size_t);
186 extern int zpool_get_state(zpool_handle_t *);
187 extern uint64_t zpool_get_version(zpool_handle_t *);
188 
189 /*
190  * Iterate over all active pools in the system.
191  */
192 typedef int (*zpool_iter_f)(zpool_handle_t *, void *);
193 extern int zpool_iter(libzfs_handle_t *, zpool_iter_f, void *);
194 
195 /*
196  * Functions to create and destroy pools
197  */
198 extern int zpool_create(libzfs_handle_t *, const char *, nvlist_t *,
199     const char *);
200 extern int zpool_destroy(zpool_handle_t *);
201 extern int zpool_add(zpool_handle_t *, nvlist_t *);
202 
203 /*
204  * Functions to manipulate pool and vdev state
205  */
206 extern int zpool_scrub(zpool_handle_t *, pool_scrub_type_t);
207 extern int zpool_clear(zpool_handle_t *, const char *);
208 
209 extern int zpool_vdev_online(zpool_handle_t *, const char *, int,
210     vdev_state_t *);
211 extern int zpool_vdev_offline(zpool_handle_t *, const char *, boolean_t);
212 extern int zpool_vdev_attach(zpool_handle_t *, const char *,
213     const char *, nvlist_t *, int);
214 extern int zpool_vdev_detach(zpool_handle_t *, const char *);
215 extern int zpool_vdev_remove(zpool_handle_t *, const char *);
216 
217 extern int zpool_vdev_fault(zpool_handle_t *, uint64_t);
218 extern int zpool_vdev_degrade(zpool_handle_t *, uint64_t);
219 extern int zpool_vdev_clear(zpool_handle_t *, uint64_t);
220 
221 extern nvlist_t *zpool_find_vdev(zpool_handle_t *, const char *, boolean_t *);
222 extern int zpool_label_disk(libzfs_handle_t *, zpool_handle_t *, char *);
223 
224 /*
225  * Functions to manage pool properties
226  */
227 extern int zpool_set_prop(zpool_handle_t *, const char *, const char *);
228 extern int zpool_get_prop(zpool_handle_t *, zpool_prop_t, char *,
229 	size_t proplen, zfs_source_t *);
230 extern uint64_t zpool_get_prop_int(zpool_handle_t *, zpool_prop_t);
231 extern const char *zpool_prop_to_name(zpool_prop_t);
232 extern const char *zpool_prop_values(zpool_prop_t);
233 
234 /*
235  * Pool health statistics.
236  */
237 typedef enum {
238 	/*
239 	 * The following correspond to faults as defined in the (fault.fs.zfs.*)
240 	 * event namespace.  Each is associated with a corresponding message ID.
241 	 */
242 	ZPOOL_STATUS_CORRUPT_CACHE,	/* corrupt /kernel/drv/zpool.cache */
243 	ZPOOL_STATUS_MISSING_DEV_R,	/* missing device with replicas */
244 	ZPOOL_STATUS_MISSING_DEV_NR,	/* missing device with no replicas */
245 	ZPOOL_STATUS_CORRUPT_LABEL_R,	/* bad device label with replicas */
246 	ZPOOL_STATUS_CORRUPT_LABEL_NR,	/* bad device label with no replicas */
247 	ZPOOL_STATUS_BAD_GUID_SUM,	/* sum of device guids didn't match */
248 	ZPOOL_STATUS_CORRUPT_POOL,	/* pool metadata is corrupted */
249 	ZPOOL_STATUS_CORRUPT_DATA,	/* data errors in user (meta)data */
250 	ZPOOL_STATUS_FAILING_DEV,	/* device experiencing errors */
251 	ZPOOL_STATUS_VERSION_NEWER,	/* newer on-disk version */
252 	ZPOOL_STATUS_HOSTID_MISMATCH,	/* last accessed by another system */
253 	ZPOOL_STATUS_FAULTED_DEV_R,	/* faulted device with replicas */
254 	ZPOOL_STATUS_FAULTED_DEV_NR,	/* faulted device with no replicas */
255 
256 	/*
257 	 * The following are not faults per se, but still an error possibly
258 	 * requiring administrative attention.  There is no corresponding
259 	 * message ID.
260 	 */
261 	ZPOOL_STATUS_VERSION_OLDER,	/* older on-disk version */
262 	ZPOOL_STATUS_RESILVERING,	/* device being resilvered */
263 	ZPOOL_STATUS_OFFLINE_DEV,	/* device online */
264 
265 	/*
266 	 * Finally, the following indicates a healthy pool.
267 	 */
268 	ZPOOL_STATUS_OK
269 } zpool_status_t;
270 
271 extern zpool_status_t zpool_get_status(zpool_handle_t *, char **);
272 extern zpool_status_t zpool_import_status(nvlist_t *, char **);
273 
274 /*
275  * Statistics and configuration functions.
276  */
277 extern nvlist_t *zpool_get_config(zpool_handle_t *, nvlist_t **);
278 extern int zpool_refresh_stats(zpool_handle_t *, boolean_t *);
279 extern int zpool_get_errlog(zpool_handle_t *, nvlist_t **);
280 
281 /*
282  * Import and export functions
283  */
284 extern int zpool_export(zpool_handle_t *);
285 extern int zpool_import(libzfs_handle_t *, nvlist_t *, const char *,
286     const char *);
287 
288 /*
289  * Search for pools to import
290  */
291 extern nvlist_t *zpool_find_import(libzfs_handle_t *, int, char **);
292 
293 /*
294  * Miscellaneous pool functions
295  */
296 struct zfs_cmd;
297 
298 extern char *zpool_vdev_name(libzfs_handle_t *, zpool_handle_t *, nvlist_t *);
299 extern int zpool_upgrade(zpool_handle_t *);
300 extern int zpool_get_history(zpool_handle_t *, nvlist_t **);
301 extern void zpool_stage_history(libzfs_handle_t *, int, char **,
302     boolean_t zfs_cmd);
303 extern void zpool_obj_to_path(zpool_handle_t *, uint64_t, uint64_t, char *,
304     size_t len);
305 extern int zfs_ioctl(libzfs_handle_t *, int, struct zfs_cmd *);
306 /*
307  * Basic handle manipulations.  These functions do not create or destroy the
308  * underlying datasets, only the references to them.
309  */
310 extern zfs_handle_t *zfs_open(libzfs_handle_t *, const char *, int);
311 extern void zfs_close(zfs_handle_t *);
312 extern zfs_type_t zfs_get_type(const zfs_handle_t *);
313 extern const char *zfs_get_name(const zfs_handle_t *);
314 
315 /*
316  * Property management functions.  Some functions are shared with the kernel,
317  * and are found in sys/fs/zfs.h.
318  */
319 extern const char *zfs_prop_to_name(zfs_prop_t);
320 extern int zfs_prop_set(zfs_handle_t *, const char *, const char *);
321 extern int zfs_prop_get(zfs_handle_t *, zfs_prop_t, char *, size_t,
322     zfs_source_t *, char *, size_t, boolean_t);
323 extern int zfs_prop_get_numeric(zfs_handle_t *, zfs_prop_t, uint64_t *,
324     zfs_source_t *, char *, size_t);
325 extern uint64_t zfs_prop_get_int(zfs_handle_t *, zfs_prop_t);
326 extern const char *zfs_prop_get_string(zfs_handle_t *, zfs_prop_t);
327 extern int zfs_prop_inherit(zfs_handle_t *, const char *);
328 extern const char *zfs_prop_values(zfs_prop_t);
329 extern int zfs_prop_valid_for_type(zfs_prop_t, int);
330 extern const char *zfs_prop_default_string(zfs_prop_t prop);
331 extern uint64_t zfs_prop_default_numeric(zfs_prop_t);
332 extern int zfs_prop_is_string(zfs_prop_t prop);
333 extern const char *zfs_prop_column_name(zfs_prop_t);
334 extern boolean_t zfs_prop_align_right(zfs_prop_t);
335 extern void nicebool(int value, char *buf, size_t buflen);
336 
337 typedef struct zfs_proplist {
338 	zfs_prop_t	pl_prop;
339 	char		*pl_user_prop;
340 	struct zfs_proplist *pl_next;
341 	boolean_t	pl_all;
342 	size_t		pl_width;
343 	boolean_t	pl_fixed;
344 } zfs_proplist_t;
345 
346 typedef zfs_proplist_t zpool_proplist_t;
347 
348 extern int zfs_get_proplist(libzfs_handle_t *, char *, zfs_proplist_t **);
349 extern int zpool_get_proplist(libzfs_handle_t *, char *, zpool_proplist_t **);
350 extern int zfs_expand_proplist(zfs_handle_t *, zfs_proplist_t **);
351 extern int zpool_expand_proplist(zpool_handle_t *, zpool_proplist_t **);
352 extern void zfs_free_proplist(zfs_proplist_t *);
353 extern nvlist_t *zfs_get_user_props(zfs_handle_t *);
354 
355 #define	ZFS_MOUNTPOINT_NONE	"none"
356 #define	ZFS_MOUNTPOINT_LEGACY	"legacy"
357 
358 /*
359  * Functions for printing properties from zfs/zpool
360  */
361 typedef struct libzfs_get_cbdata {
362 	int cb_sources;
363 	int cb_columns[4];
364 	int cb_colwidths[5];
365 	boolean_t cb_scripted;
366 	boolean_t cb_literal;
367 	boolean_t cb_first;
368 	zfs_proplist_t *cb_proplist;
369 } libzfs_get_cbdata_t;
370 
371 void libzfs_print_one_property(const char *, libzfs_get_cbdata_t *,
372     const char *, const char *, zfs_source_t, const char *);
373 
374 #define	GET_COL_NAME		1
375 #define	GET_COL_PROPERTY	2
376 #define	GET_COL_VALUE		3
377 #define	GET_COL_SOURCE		4
378 
379 /*
380  * Iterator functions.
381  */
382 typedef int (*zfs_iter_f)(zfs_handle_t *, void *);
383 extern int zfs_iter_root(libzfs_handle_t *, zfs_iter_f, void *);
384 extern int zfs_iter_children(zfs_handle_t *, zfs_iter_f, void *);
385 extern int zfs_iter_dependents(zfs_handle_t *, boolean_t, zfs_iter_f, void *);
386 extern int zfs_iter_filesystems(zfs_handle_t *, zfs_iter_f, void *);
387 extern int zfs_iter_snapshots(zfs_handle_t *, zfs_iter_f, void *);
388 
389 /*
390  * Functions to create and destroy datasets.
391  */
392 extern int zfs_create(libzfs_handle_t *, const char *, zfs_type_t,
393     nvlist_t *);
394 extern int zfs_create_ancestors(libzfs_handle_t *, const char *);
395 extern int zfs_destroy(zfs_handle_t *);
396 extern int zfs_destroy_snaps(zfs_handle_t *, char *);
397 extern int zfs_clone(zfs_handle_t *, const char *, nvlist_t *);
398 extern int zfs_snapshot(libzfs_handle_t *, const char *, boolean_t);
399 extern int zfs_rollback(zfs_handle_t *, zfs_handle_t *, int);
400 extern int zfs_rename(zfs_handle_t *, const char *, boolean_t);
401 extern int zfs_send(zfs_handle_t *, const char *, int);
402 extern int zfs_receive(libzfs_handle_t *, const char *, int, int, int,
403     boolean_t, int);
404 extern int zfs_promote(zfs_handle_t *);
405 
406 /*
407  * Miscellaneous functions.
408  */
409 extern const char *zfs_type_to_name(zfs_type_t);
410 extern void zfs_refresh_properties(zfs_handle_t *);
411 extern int zfs_name_valid(const char *, zfs_type_t);
412 extern zfs_handle_t *zfs_path_to_zhandle(libzfs_handle_t *, char *, zfs_type_t);
413 extern boolean_t zfs_dataset_exists(libzfs_handle_t *, const char *,
414     zfs_type_t);
415 
416 /*
417  * dataset permission functions.
418  */
419 extern int zfs_perm_set(zfs_handle_t *, nvlist_t *);
420 extern int zfs_perm_remove(zfs_handle_t *, nvlist_t *);
421 extern int zfs_build_perms(zfs_handle_t *, char *, char *,
422     zfs_deleg_who_type_t, zfs_deleg_inherit_t, nvlist_t **nvlist_t);
423 extern int zfs_perm_get(zfs_handle_t *, zfs_allow_t **);
424 extern void zfs_free_allows(zfs_allow_t *);
425 
426 /*
427  * Mount support functions.
428  */
429 extern boolean_t is_mounted(libzfs_handle_t *, const char *special, char **);
430 extern boolean_t zfs_is_mounted(zfs_handle_t *, char **);
431 extern int zfs_mount(zfs_handle_t *, const char *, int);
432 extern int zfs_unmount(zfs_handle_t *, const char *, int);
433 extern int zfs_unmountall(zfs_handle_t *, int);
434 
435 /*
436  * Share support functions.
437  */
438 extern boolean_t zfs_is_shared(zfs_handle_t *);
439 extern int zfs_share(zfs_handle_t *);
440 extern int zfs_unshare(zfs_handle_t *);
441 
442 /*
443  * Protocol-specific share support functions.
444  */
445 extern boolean_t zfs_is_shared_nfs(zfs_handle_t *, char **);
446 extern int zfs_share_nfs(zfs_handle_t *);
447 extern int zfs_unshare_nfs(zfs_handle_t *, const char *);
448 extern int zfs_unshareall_nfs(zfs_handle_t *);
449 extern boolean_t zfs_is_shared_iscsi(zfs_handle_t *);
450 extern int zfs_share_iscsi(zfs_handle_t *);
451 extern int zfs_unshare_iscsi(zfs_handle_t *);
452 extern int zfs_iscsi_perm_check(libzfs_handle_t *, char *, ucred_t *);
453 extern int zfs_deleg_share_nfs(libzfs_handle_t *, char *, char *,
454     void *, void *, int, boolean_t);
455 
456 /*
457  * When dealing with nvlists, verify() is extremely useful
458  */
459 #ifdef NDEBUG
460 #define	verify(EX)	((void)(EX))
461 #else
462 #define	verify(EX)	assert(EX)
463 #endif
464 
465 /*
466  * Utility function to convert a number to a human-readable form.
467  */
468 extern void zfs_nicenum(uint64_t, char *, size_t);
469 extern int zfs_nicestrtonum(libzfs_handle_t *, const char *, uint64_t *);
470 
471 /*
472  * Pool destroy special.  Remove the device information without destroying
473  * the underlying dataset.
474  */
475 extern int zfs_remove_link(zfs_handle_t *);
476 
477 /*
478  * Given a device or file, determine if it is part of a pool.
479  */
480 extern int zpool_in_use(libzfs_handle_t *, int, pool_state_t *, char **,
481     boolean_t *);
482 
483 /*
484  * ftyp special.  Read the label from a given device.
485  */
486 extern int zpool_read_label(int, nvlist_t **);
487 
488 /*
489  * Create and remove zvol /dev links.
490  */
491 extern int zpool_create_zvol_links(zpool_handle_t *);
492 extern int zpool_remove_zvol_links(zpool_handle_t *);
493 
494 /*
495  * Enable and disable datasets within a pool by mounting/unmounting and
496  * sharing/unsharing them.
497  */
498 extern int zpool_enable_datasets(zpool_handle_t *, const char *, int);
499 extern int zpool_disable_datasets(zpool_handle_t *, boolean_t);
500 
501 #ifdef	__cplusplus
502 }
503 #endif
504 
505 #endif	/* _LIBZFS_H */
506