1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51485Slling  * Common Development and Distribution License (the "License").
61485Slling  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
221294Slling  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #ifndef	_LIBZFS_H
27789Sahrens #define	_LIBZFS_H
28789Sahrens 
29789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
30789Sahrens 
31789Sahrens #include <assert.h>
32789Sahrens #include <libnvpair.h>
33789Sahrens #include <sys/param.h>
34789Sahrens #include <sys/types.h>
35789Sahrens #include <sys/varargs.h>
36789Sahrens #include <sys/fs/zfs.h>
37789Sahrens 
38789Sahrens #ifdef	__cplusplus
39789Sahrens extern "C" {
40789Sahrens #endif
41789Sahrens 
42789Sahrens /*
43789Sahrens  * Miscellaneous ZFS constants
44789Sahrens  */
45789Sahrens #define	ZFS_MAXNAMELEN		MAXNAMELEN
46789Sahrens #define	ZPOOL_MAXNAMELEN	MAXNAMELEN
47789Sahrens #define	ZFS_MAXPROPLEN		MAXPATHLEN
48789Sahrens 
49789Sahrens /*
502082Seschrock  * libzfs errors
512082Seschrock  */
522082Seschrock enum {
532082Seschrock 	EZFS_NOMEM = 2000,	/* out of memory */
542082Seschrock 	EZFS_BADPROP,		/* invalid property value */
552082Seschrock 	EZFS_PROPREADONLY,	/* cannot set readonly property */
562082Seschrock 	EZFS_PROPTYPE,		/* property does not apply to dataset type */
572082Seschrock 	EZFS_PROPNONINHERIT,	/* property is not inheritable */
582082Seschrock 	EZFS_PROPSPACE,		/* bad quota or reservation */
592082Seschrock 	EZFS_BADTYPE,		/* dataset is not of appropriate type */
602082Seschrock 	EZFS_BUSY,		/* pool or dataset is busy */
612082Seschrock 	EZFS_EXISTS,		/* pool or dataset already exists */
622082Seschrock 	EZFS_NOENT,		/* no such pool or dataset */
632082Seschrock 	EZFS_BADSTREAM,		/* bad backup stream */
642082Seschrock 	EZFS_DSREADONLY,	/* dataset is readonly */
652082Seschrock 	EZFS_VOLTOOBIG,		/* volume is too large for 32-bit system */
662082Seschrock 	EZFS_VOLHASDATA,	/* volume already contains data */
672082Seschrock 	EZFS_INVALIDNAME,	/* invalid dataset name */
682082Seschrock 	EZFS_BADRESTORE,	/* unable to restore to destination */
692082Seschrock 	EZFS_BADBACKUP,		/* backup failed */
702082Seschrock 	EZFS_BADTARGET,		/* bad attach/detach/replace target */
712082Seschrock 	EZFS_NODEVICE,		/* no such device in pool */
722082Seschrock 	EZFS_BADDEV,		/* invalid device to add */
732082Seschrock 	EZFS_NOREPLICAS,	/* no valid replicas */
742082Seschrock 	EZFS_RESILVERING,	/* currently resilvering */
752082Seschrock 	EZFS_BADVERSION,	/* unsupported version */
762082Seschrock 	EZFS_POOLUNAVAIL,	/* pool is currently unavailable */
772082Seschrock 	EZFS_DEVOVERFLOW,	/* too many devices in one vdev */
782082Seschrock 	EZFS_BADPATH,		/* must be an absolute path */
792082Seschrock 	EZFS_CROSSTARGET,	/* rename or clone across pool or dataset */
802082Seschrock 	EZFS_ZONED,		/* used improperly in local zone */
812082Seschrock 	EZFS_MOUNTFAILED,	/* failed to mount dataset */
822082Seschrock 	EZFS_UMOUNTFAILED,	/* failed to unmount dataset */
832082Seschrock 	EZFS_UNSHAREFAILED,	/* unshare(1M) failed */
842082Seschrock 	EZFS_SHAREFAILED,	/* share(1M) failed */
852082Seschrock 	EZFS_DEVLINKS,		/* failed to create zvol links */
862082Seschrock 	EZFS_PERM,		/* permission denied */
872082Seschrock 	EZFS_NOSPC,		/* out of space */
882082Seschrock 	EZFS_IO,		/* I/O error */
892082Seschrock 	EZFS_INTR,		/* signal received */
902082Seschrock 	EZFS_ISSPARE,		/* device is a hot spare */
912082Seschrock 	EZFS_INVALCONFIG,	/* invalid vdev configuration */
922082Seschrock 	EZFS_UNKNOWN		/* unknown error */
932082Seschrock };
942082Seschrock 
952082Seschrock /*
96789Sahrens  * Basic handle types
97789Sahrens  */
98789Sahrens typedef struct zfs_handle zfs_handle_t;
99789Sahrens typedef struct zpool_handle zpool_handle_t;
1002082Seschrock typedef struct libzfs_handle libzfs_handle_t;
1012082Seschrock 
1022082Seschrock /*
1032082Seschrock  * Library initialization
1042082Seschrock  */
1052082Seschrock extern libzfs_handle_t *libzfs_init(void);
1062082Seschrock extern void libzfs_fini(libzfs_handle_t *);
1072082Seschrock 
1082082Seschrock extern libzfs_handle_t *zpool_get_handle(zpool_handle_t *);
1092082Seschrock extern libzfs_handle_t *zfs_get_handle(zfs_handle_t *);
1102082Seschrock 
1112082Seschrock extern void libzfs_print_on_error(libzfs_handle_t *, boolean_t);
1122082Seschrock 
1132082Seschrock extern int libzfs_errno(libzfs_handle_t *);
1142082Seschrock extern const char *libzfs_error_action(libzfs_handle_t *);
1152082Seschrock extern const char *libzfs_error_description(libzfs_handle_t *);
116789Sahrens 
117789Sahrens /*
118789Sahrens  * Basic handle functions
119789Sahrens  */
1202082Seschrock extern zpool_handle_t *zpool_open(libzfs_handle_t *, const char *);
1212082Seschrock extern zpool_handle_t *zpool_open_canfail(libzfs_handle_t *, const char *);
122789Sahrens extern void zpool_close(zpool_handle_t *);
123789Sahrens extern const char *zpool_get_name(zpool_handle_t *);
124789Sahrens extern uint64_t zpool_get_guid(zpool_handle_t *);
125789Sahrens extern uint64_t zpool_get_space_used(zpool_handle_t *);
126789Sahrens extern uint64_t zpool_get_space_total(zpool_handle_t *);
127789Sahrens extern int zpool_get_root(zpool_handle_t *, char *, size_t);
128789Sahrens extern int zpool_get_state(zpool_handle_t *);
1292082Seschrock extern uint64_t zpool_get_version(zpool_handle_t *);
130789Sahrens 
131789Sahrens /*
132789Sahrens  * Iterate over all active pools in the system.
133789Sahrens  */
134789Sahrens typedef int (*zpool_iter_f)(zpool_handle_t *, void *);
1352082Seschrock extern int zpool_iter(libzfs_handle_t *, zpool_iter_f, void *);
136789Sahrens 
137789Sahrens /*
138789Sahrens  * Functions to create and destroy pools
139789Sahrens  */
1402082Seschrock extern int zpool_create(libzfs_handle_t *, const char *, nvlist_t *,
1412082Seschrock     const char *);
142789Sahrens extern int zpool_destroy(zpool_handle_t *);
143789Sahrens extern int zpool_add(zpool_handle_t *, nvlist_t *);
144789Sahrens 
145789Sahrens /*
146789Sahrens  * Functions to manipulate pool and vdev state
147789Sahrens  */
148789Sahrens extern int zpool_scrub(zpool_handle_t *, pool_scrub_type_t);
149789Sahrens 
150789Sahrens extern int zpool_vdev_online(zpool_handle_t *, const char *);
1511485Slling extern int zpool_vdev_offline(zpool_handle_t *, const char *, int);
152789Sahrens extern int zpool_vdev_attach(zpool_handle_t *, const char *, const char *,
153789Sahrens     nvlist_t *, int);
154789Sahrens extern int zpool_vdev_detach(zpool_handle_t *, const char *);
1552082Seschrock extern int zpool_vdev_remove(zpool_handle_t *, const char *);
1561544Seschrock extern int zpool_clear(zpool_handle_t *, const char *);
1572082Seschrock extern nvlist_t *zpool_find_vdev(zpool_handle_t *, const char *, boolean_t *);
158789Sahrens 
159789Sahrens /*
160789Sahrens  * Pool health statistics.
161789Sahrens  */
162789Sahrens typedef enum {
163789Sahrens 	/*
164789Sahrens 	 * The following correspond to faults as defined in the (fault.fs.zfs.*)
1651003Slling 	 * event namespace.  Each is associated with a corresponding message ID.
166789Sahrens 	 */
167789Sahrens 	ZPOOL_STATUS_CORRUPT_CACHE,	/* corrupt /kernel/drv/zpool.cache */
168789Sahrens 	ZPOOL_STATUS_MISSING_DEV_R,	/* missing device with replicas */
169789Sahrens 	ZPOOL_STATUS_MISSING_DEV_NR,	/* missing device with no replicas */
170789Sahrens 	ZPOOL_STATUS_CORRUPT_LABEL_R,	/* bad device label with replicas */
1711003Slling 	ZPOOL_STATUS_CORRUPT_LABEL_NR,	/* bad device label with no replicas */
172789Sahrens 	ZPOOL_STATUS_BAD_GUID_SUM,	/* sum of device guids didn't match */
173789Sahrens 	ZPOOL_STATUS_CORRUPT_POOL,	/* pool metadata is corrupted */
174789Sahrens 	ZPOOL_STATUS_CORRUPT_DATA,	/* data errors in user (meta)data */
175789Sahrens 	ZPOOL_STATUS_FAILING_DEV,	/* device experiencing errors */
1761760Seschrock 	ZPOOL_STATUS_VERSION_NEWER,	/* newer on-disk version */
177789Sahrens 
178789Sahrens 	/*
179789Sahrens 	 * The following are not faults per se, but still an error possibly
1801003Slling 	 * requiring administrative attention.  There is no corresponding
181789Sahrens 	 * message ID.
182789Sahrens 	 */
1831760Seschrock 	ZPOOL_STATUS_VERSION_OLDER,	/* older on-disk version */
184789Sahrens 	ZPOOL_STATUS_RESILVERING,	/* device being resilvered */
185789Sahrens 	ZPOOL_STATUS_OFFLINE_DEV,	/* device online */
186789Sahrens 
187789Sahrens 	/*
188789Sahrens 	 * Finally, the following indicates a healthy pool.
189789Sahrens 	 */
190789Sahrens 	ZPOOL_STATUS_OK
191789Sahrens } zpool_status_t;
192789Sahrens 
1931544Seschrock extern zpool_status_t zpool_get_status(zpool_handle_t *, char **);
1941544Seschrock extern zpool_status_t zpool_import_status(nvlist_t *, char **);
195789Sahrens 
196789Sahrens /*
197789Sahrens  * Statistics and configuration functions.
198789Sahrens  */
1991544Seschrock extern nvlist_t *zpool_get_config(zpool_handle_t *, nvlist_t **);
2002142Seschrock extern int zpool_refresh_stats(zpool_handle_t *, boolean_t *);
2011544Seschrock extern int zpool_get_errlog(zpool_handle_t *, nvlist_t ***, size_t *);
2021544Seschrock 
2031544Seschrock #define	ZPOOL_ERR_DATASET	"dataset"
2041544Seschrock #define	ZPOOL_ERR_OBJECT	"object"
2051544Seschrock #define	ZPOOL_ERR_RANGE		"range"
206789Sahrens 
207789Sahrens /*
208789Sahrens  * Import and export functions
209789Sahrens  */
210789Sahrens extern int zpool_export(zpool_handle_t *);
2112082Seschrock extern int zpool_import(libzfs_handle_t *, nvlist_t *, const char *,
2122082Seschrock     const char *);
213789Sahrens 
214789Sahrens /*
215789Sahrens  * Search for pools to import
216789Sahrens  */
2172082Seschrock extern nvlist_t *zpool_find_import(libzfs_handle_t *, int, char **);
218789Sahrens 
219789Sahrens /*
2201354Seschrock  * Miscellaneous pool functions
2211354Seschrock  */
2222082Seschrock extern char *zpool_vdev_name(libzfs_handle_t *, zpool_handle_t *, nvlist_t *);
2231760Seschrock extern int zpool_upgrade(zpool_handle_t *);
2241354Seschrock 
2251354Seschrock /*
226789Sahrens  * Basic handle manipulations.  These functions do not create or destroy the
227789Sahrens  * underlying datasets, only the references to them.
228789Sahrens  */
2292082Seschrock extern zfs_handle_t *zfs_open(libzfs_handle_t *, const char *, int);
230789Sahrens extern void zfs_close(zfs_handle_t *);
231789Sahrens extern zfs_type_t zfs_get_type(const zfs_handle_t *);
232789Sahrens extern const char *zfs_get_name(const zfs_handle_t *);
233789Sahrens 
234789Sahrens typedef enum {
235789Sahrens 	ZFS_SRC_NONE = 0x1,
236789Sahrens 	ZFS_SRC_DEFAULT = 0x2,
237789Sahrens 	ZFS_SRC_TEMPORARY = 0x4,
238789Sahrens 	ZFS_SRC_LOCAL = 0x8,
239789Sahrens 	ZFS_SRC_INHERITED = 0x10
240789Sahrens } zfs_source_t;
241789Sahrens 
242789Sahrens #define	ZFS_SRC_ALL	0x1f
243789Sahrens 
244789Sahrens /*
245789Sahrens  * Property management functions.  Some functions are shared with the kernel,
2461003Slling  * and are found in sys/fs/zfs.h.
247789Sahrens  */
248789Sahrens const char *zfs_prop_to_name(zfs_prop_t);
249789Sahrens int zfs_prop_set(zfs_handle_t *, zfs_prop_t, const char *);
250789Sahrens int zfs_prop_get(zfs_handle_t *, zfs_prop_t, char *, size_t, zfs_source_t *,
2512082Seschrock     char *, size_t, boolean_t);
252789Sahrens int zfs_prop_get_numeric(zfs_handle_t *, zfs_prop_t, uint64_t *, zfs_source_t *,
253789Sahrens     char *, size_t);
254789Sahrens uint64_t zfs_prop_get_int(zfs_handle_t *, zfs_prop_t);
2552082Seschrock int zfs_prop_validate(libzfs_handle_t *, zfs_prop_t, const char *, uint64_t *);
256789Sahrens int zfs_prop_inheritable(zfs_prop_t);
257789Sahrens int zfs_prop_inherit(zfs_handle_t *, zfs_prop_t);
258789Sahrens const char *zfs_prop_values(zfs_prop_t);
259789Sahrens int zfs_prop_valid_for_type(zfs_prop_t, int);
2601356Seschrock const char *zfs_prop_default_string(zfs_prop_t prop);
261789Sahrens uint64_t zfs_prop_default_numeric(zfs_prop_t);
262789Sahrens int zfs_prop_is_string(zfs_prop_t prop);
263789Sahrens const char *zfs_prop_column_name(zfs_prop_t);
264789Sahrens const char *zfs_prop_column_format(zfs_prop_t);
265866Seschrock int zfs_get_proplist(char *fields, zfs_prop_t *proplist, int max, int *count,
266866Seschrock     char **badopt);
267789Sahrens 
268789Sahrens #define	ZFS_MOUNTPOINT_NONE	"none"
269789Sahrens #define	ZFS_MOUNTPOINT_LEGACY	"legacy"
270789Sahrens 
271789Sahrens /*
272789Sahrens  * Iterator functions.
273789Sahrens  */
274789Sahrens typedef int (*zfs_iter_f)(zfs_handle_t *, void *);
2752082Seschrock extern int zfs_iter_root(libzfs_handle_t *, zfs_iter_f, void *);
276789Sahrens extern int zfs_iter_children(zfs_handle_t *, zfs_iter_f, void *);
277789Sahrens extern int zfs_iter_dependents(zfs_handle_t *, zfs_iter_f, void *);
2781356Seschrock extern int zfs_iter_filesystems(zfs_handle_t *, zfs_iter_f, void *);
2791356Seschrock extern int zfs_iter_snapshots(zfs_handle_t *, zfs_iter_f, void *);
280789Sahrens 
281789Sahrens /*
282789Sahrens  * Functions to create and destroy datasets.
283789Sahrens  */
2842082Seschrock extern int zfs_create(libzfs_handle_t *, const char *, zfs_type_t,
2852082Seschrock     const char *, const char *);
286789Sahrens extern int zfs_destroy(zfs_handle_t *);
287*2199Sahrens extern int zfs_destroy_snaps(zfs_handle_t *, char *);
288789Sahrens extern int zfs_clone(zfs_handle_t *, const char *);
289*2199Sahrens extern int zfs_snapshot(libzfs_handle_t *, const char *, boolean_t);
2901294Slling extern int zfs_rollback(zfs_handle_t *, zfs_handle_t *, int);
291789Sahrens extern int zfs_rename(zfs_handle_t *, const char *);
2921749Sahrens extern int zfs_send(zfs_handle_t *, zfs_handle_t *);
2932082Seschrock extern int zfs_receive(libzfs_handle_t *, const char *, int, int, int);
2942082Seschrock extern int zfs_promote(zfs_handle_t *);
295789Sahrens 
296789Sahrens /*
297789Sahrens  * Miscellaneous functions.
298789Sahrens  */
299789Sahrens extern const char *zfs_type_to_name(zfs_type_t);
300789Sahrens extern void zfs_refresh_properties(zfs_handle_t *);
301789Sahrens extern int zfs_name_valid(const char *, zfs_type_t);
302789Sahrens 
303789Sahrens /*
304789Sahrens  * Mount support functions.
305789Sahrens  */
3062082Seschrock extern boolean_t zfs_is_mounted(zfs_handle_t *, char **);
307789Sahrens extern int zfs_mount(zfs_handle_t *, const char *, int);
308789Sahrens extern int zfs_unmount(zfs_handle_t *, const char *, int);
309789Sahrens extern int zfs_unmountall(zfs_handle_t *, int);
310789Sahrens 
311789Sahrens /*
312789Sahrens  * Share support functions.
313789Sahrens  */
3142082Seschrock extern boolean_t zfs_is_shared(zfs_handle_t *, char **);
315789Sahrens extern int zfs_share(zfs_handle_t *);
316789Sahrens extern int zfs_unshare(zfs_handle_t *, const char *);
317789Sahrens extern int zfs_unshareall(zfs_handle_t *);
318789Sahrens 
319789Sahrens /*
320789Sahrens  * When dealing with nvlists, verify() is extremely useful
321789Sahrens  */
322789Sahrens #ifdef NDEBUG
323789Sahrens #define	verify(EX)	((void)(EX))
324789Sahrens #else
325789Sahrens #define	verify(EX)	assert(EX)
326789Sahrens #endif
327789Sahrens 
328789Sahrens /*
329789Sahrens  * Utility function to convert a number to a human-readable form.
330789Sahrens  */
331789Sahrens extern void zfs_nicenum(uint64_t, char *, size_t);
332789Sahrens extern int zfs_nicestrtonum(const char *, uint64_t *);
333789Sahrens 
334789Sahrens /*
335789Sahrens  * Pool destroy special.  Remove the device information without destroying
336789Sahrens  * the underlying dataset.
337789Sahrens  */
338789Sahrens extern int zfs_remove_link(zfs_handle_t *);
339789Sahrens 
340789Sahrens /*
341789Sahrens  * Given a device or file, determine if it is part of a pool.
342789Sahrens  */
3432082Seschrock extern int zpool_in_use(libzfs_handle_t *, int, pool_state_t *, char **,
3442082Seschrock     boolean_t *);
345789Sahrens 
346789Sahrens /*
347789Sahrens  * ftyp special.  Read the label from a given device.
348789Sahrens  */
3492082Seschrock extern int zpool_read_label(int, nvlist_t **);
350789Sahrens 
351789Sahrens /*
352789Sahrens  * Create and remove zvol /dev links
353789Sahrens  */
354789Sahrens extern int zpool_create_zvol_links(zpool_handle_t *);
355789Sahrens extern int zpool_remove_zvol_links(zpool_handle_t *);
356789Sahrens 
357789Sahrens #ifdef	__cplusplus
358789Sahrens }
359789Sahrens #endif
360789Sahrens 
361789Sahrens #endif	/* _LIBZFS_H */
362