Current File : //usr/include/libzfs.h
/*
 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
 */

#ifndef	_LIBZFS_H
#define	_LIBZFS_H

#include <assert.h>
#include <libnvpair.h>
#include <sys/mnttab.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/varargs.h>
#include <sys/fs/zfs.h>
#include <sys/avl.h>
#include <ucred.h>
#include <sys/sa_share.h>

#ifdef	__cplusplus
extern "C" {
#endif

/*
 * Miscellaneous ZFS constants
 */
#define	ZFS_MAXNAMELEN		MAXNAMELEN
#define	ZPOOL_MAXNAMELEN	MAXNAMELEN
#define	ZFS_MAXPROPLEN		ZAP_MAXVALUELEN
#define	ZPOOL_MAXPROPLEN	MAXPATHLEN

#define	ZFS_MOUNTPOINT_NONE	"none"
#define	ZFS_MOUNTPOINT_LEGACY	"legacy"

/*
 * libzfs errors
 */

#define	EZFS_MINVAL	2000

enum {
	EZFS_NOMEM = EZFS_MINVAL,	/* out of memory */
	EZFS_BADPROP,		/* invalid property value */
	EZFS_PROPREADONLY,	/* cannot set readonly property */
	EZFS_PROPTYPE,		/* property does not apply to dataset type */
	EZFS_PROPNONINHERIT,	/* property is not inheritable */
	EZFS_PROPSPACE,		/* bad quota or reservation */
	EZFS_BADTYPE,		/* dataset is not of appropriate type */
	EZFS_BUSY,		/* pool or dataset is busy */
	EZFS_EXISTS,		/* pool or dataset already exists */
	EZFS_NOENT,		/* no such pool or dataset */
	EZFS_BADSTREAM,		/* bad backup stream */
	EZFS_DSREADONLY,	/* dataset is readonly */
	EZFS_VOLTOOBIG,		/* volume is too large for 32-bit system */
	EZFS_INVALIDNAME,	/* invalid dataset name */
	EZFS_BADRESTORE,	/* unable to restore to destination */
	EZFS_BADBACKUP,		/* backup failed */
	EZFS_BADTARGET,		/* bad attach/detach/replace target */
	EZFS_NODEVICE,		/* no such device in pool */
	EZFS_BADDEV,		/* invalid device to add */
	EZFS_NOREPLICAS,	/* no valid replicas */
	EZFS_RESILVERING,	/* currently resilvering */
	EZFS_BADVERSION,	/* unsupported version */
	EZFS_POOLUNAVAIL,	/* pool is currently unavailable */
	EZFS_DEVOVERFLOW,	/* too many devices in one vdev */
	EZFS_BADPATH,		/* must be an absolute path */
	EZFS_CROSSTARGET,	/* rename or clone across pool or dataset */
	EZFS_ZONED,		/* used improperly in local zone */
	EZFS_MOUNTFAILED,	/* failed to mount dataset */
	EZFS_MKDIRFAILED,	/* failed to create directory for mount */
	EZFS_BADSHADOW,		/* invalid shadow property */
	EZFS_SHADOWMOUNTFAILED,	/* failed to mount shadow filesysem */
	EZFS_UMOUNTFAILED,	/* failed to unmount dataset */
	EZFS_UNSHAREFAILED,	/* unshare failed */
	EZFS_SHAREFAILED,	/* share failed */
	EZFS_PERM,		/* permission denied */
	EZFS_NOSPC,		/* out of space */
	EZFS_FAULT,		/* bad address */
	EZFS_IO,		/* I/O error */
	EZFS_INTR,		/* signal received */
	EZFS_ISSPARE,		/* device is a hot spare */
	EZFS_INVALCONFIG,	/* invalid vdev configuration */
	EZFS_RECURSIVE,		/* recursive dependency */
	EZFS_NOHISTORY,		/* no history object */
	EZFS_POOLPROPS,		/* couldn't retrieve pool props */
	EZFS_POOL_NOTSUP,	/* ops not supported for this type of pool */
	EZFS_POOL_INVALARG,	/* invalid argument for this pool operation */
	EZFS_NAMETOOLONG,	/* dataset name is too long */
	EZFS_OPENFAILED,	/* open of device failed */
	EZFS_NOCAP,		/* couldn't get capacity */
	EZFS_LABELFAILED,	/* write of label failed */
	EZFS_BADWHO,		/* invalid permission who */
	EZFS_BADPERM,		/* invalid permission */
	EZFS_BADPERMSET,	/* invalid permission set name */
	EZFS_NODELEGATION,	/* delegated administration is disabled */
	EZFS_BADCACHE,		/* bad cache file */
	EZFS_ISL2CACHE,		/* device is for the level 2 ARC */
	EZFS_VDEVNOTSUP,	/* unsupported vdev type */
	EZFS_NOTSUP,		/* ops not supported on this dataset */
	EZFS_ACTIVE_SPARE,	/* pool has active shared spare devices */
	EZFS_UNPLAYED_LOGS,	/* log device has unplayed logs */
	EZFS_REFTAG_RELE,	/* snapshot release: tag not found */
	EZFS_REFTAG_HOLD,	/* snapshot hold: tag already exists */
	EZFS_TAGTOOLONG,	/* snapshot hold/rele: tag too long */
	EZFS_PIPEFAILED,	/* pipe create failed */
	EZFS_THREADCREATEFAILED, /* thread create failed */
	EZFS_POSTSPLIT_ONLINE,	/* onlining a disk after splitting it */
	EZFS_SCRUBBING,		/* currently scrubbing */
	EZFS_NO_SCRUB,		/* no active scrub */
	EZFS_DIFF,		/* general failure of zfs diff */
	EZFS_DIFFDATA,		/* bad zfs diff data */
	EZFS_POOLREADONLY,	/* pool is in read-only mode */
	EZFS_KEYERR,		/* crypto key config or general failure */
	EZFS_POOL_INUSE,	/* pool already in use */
	EZFS_DEV_INUSE,		/* device already in use */
	EZFS_INVALID_PROPNAME,	/* invalid property name */
	EZFS_BADSHAREPATH,	/* not relative to mountpoint */
	EZFS_NOTMOUNTED,	/* filesystem not mounted */
	EZFS_VOLONLY,		/* applies only to zvol */
	EZFS_KEY_MISSING,	/* could not find the key */
	EZFS_KEY_INCORRECT,	/* key doesn't decrypt the keychain */
	EZFS_KEY_UNUSABLE,	/* key found but in an unusable state */
	EZFS_UNKNOWN
};

enum zfs_aux_error {
	EXZFS_NO_AUX_ERROR,
	EXZFS_DEST_EXISTS,
	EXZFS_NAME_EXISTS,
	EXZFS_DEST_HAS_SNAPS,
	EXZFS_CANT_OVERWRITE_CLONE,
	EXZFS_INCR_SRCSNAP_NOT_FOUND,
	EXZFS_SEND_SRC_NOT_FOUND,
	EXZFS_ZERO_LENGTH_NAME,
	EXZFS_CANT_GET_VERSION_INFO,
	EXZFS_CLONE_ORIGIN_NOT_FOUND,
	EXZFS_DESTDS_NOT_FOUND,
	EXZFS_PREFIX_DS_NOT_FOUND,
	EXZFS_UNKNOWN
};

/*
 * definitions used to control whether a required partition for booting
 * of the disk should be created when (EFI/GPT) labeling a disk.
 */

/* do not create any required boot partitions */
#define	ZPOOL_LABEL_NOREQ_PART		0

/* create a required boot partition based on system requirement */
#define	ZPOOL_LABEL_REQ_PART		1

/*
 * create required boot partition based on original pool's content,
 * e.g zpool for attach
 */
#define	ZPOOL_LABEL_MATCH_REQ_PART	2


/*
 * The following data structures are all part
 * of the zfs_allow_t data structure which is
 * used for printing 'allow' permissions.
 * It is a linked list of zfs_allow_t's which
 * then contain avl tree's for user/group/sets/...
 * and each one of the entries in those trees have
 * avl tree's for the permissions they belong to and
 * whether they are local,descendent or local+descendent
 * permissions.  The AVL trees are used primarily for
 * sorting purposes, but also so that we can quickly find
 * a given user and or permission.
 */
typedef struct zfs_perm_node {
	avl_node_t z_node;
	char z_pname[MAXPATHLEN];
} zfs_perm_node_t;

typedef struct zfs_allow_node {
	avl_node_t z_node;
	char z_key[MAXPATHLEN];		/* name, such as joe */
	avl_tree_t z_localdescend;	/* local+descendent perms */
	avl_tree_t z_local;		/* local permissions */
	avl_tree_t z_descend;		/* descendent permissions */
} zfs_allow_node_t;

typedef struct zfs_allow {
	struct zfs_allow *z_next;
	char z_setpoint[MAXPATHLEN];
	avl_tree_t z_sets;
	avl_tree_t z_crperms;
	avl_tree_t z_user;
	avl_tree_t z_group;
	avl_tree_t z_everyone;
} zfs_allow_t;

/*
 * Basic handle types
 */
typedef struct zfs_handle zfs_handle_t;
typedef struct zpool_handle zpool_handle_t;
typedef struct libzfs_handle libzfs_handle_t;
typedef struct libzfs_cro_handle libzfs_cro_handle_t;

/*
 * Library initialization
 */
extern libzfs_handle_t *libzfs_init(void);
extern void libzfs_fini(libzfs_handle_t *);

extern libzfs_handle_t *zpool_get_handle(zpool_handle_t *);
extern libzfs_handle_t *zfs_get_handle(zfs_handle_t *);

extern void libzfs_print_on_error(libzfs_handle_t *, boolean_t);
extern void libzfs_abort_on_error(libzfs_handle_t *, int, boolean_t);

extern int libzfs_errno(libzfs_handle_t *);
extern int libzfs_auxerrno(libzfs_handle_t *);
extern const char *libzfs_error_action(libzfs_handle_t *);
extern const char *libzfs_error_description(libzfs_handle_t *);
extern void libzfs_mnttab_init(libzfs_handle_t *);
extern void libzfs_mnttab_fini(libzfs_handle_t *);
extern void libzfs_mnttab_cache(libzfs_handle_t *, boolean_t);
extern int libzfs_mnttab_find(libzfs_handle_t *, const char *,
    struct extmnttab *, boolean_t);
extern void libzfs_mnttab_add(libzfs_handle_t *, const char *,
    const char *, const char *);
extern void libzfs_mnttab_remove(libzfs_handle_t *, const char *);
extern void libzfs_mnttab_reset(libzfs_handle_t *);
extern int libzfs_mnttab_scan(libzfs_handle_t *, dev_t, struct extmnttab *);
extern int libzfs_mnttab_scan_path(libzfs_handle_t *, const char *,
    struct extmnttab *, boolean_t);
extern libzfs_cro_handle_t *libzfs_get_cro_hdl(libzfs_handle_t *);

extern void libzfs_set_automount(libzfs_handle_t *, boolean_t);
typedef int (*libzfs_spawn_f)(const char *, char **, void *);
extern void libzfs_set_spawn(libzfs_handle_t *, libzfs_spawn_f, void *);

/*
 * Basic handle functions
 */
extern zpool_handle_t *zpool_open(libzfs_handle_t *, const char *);
extern zpool_handle_t *zpool_open_canfail(libzfs_handle_t *, const char *);
extern int zpool_open_quiet(libzfs_handle_t *, const char *,
    zpool_handle_t **);
extern void zpool_close(zpool_handle_t *);
extern const char *zpool_get_name(zpool_handle_t *);
extern int zpool_get_state(zpool_handle_t *);
extern char *zpool_state_to_name(vdev_state_t, vdev_aux_t);
extern void zpool_free_handles(libzfs_handle_t *);

/*
 * Iterate over all active pools in the system.
 */
typedef int (*zpool_iter_f)(zpool_handle_t *, void *);
extern int zpool_iter(libzfs_handle_t *, zpool_iter_f, void *);

/*
 * Functions to create and destroy pools
 */
extern int zpool_create(libzfs_handle_t *, const char *, nvlist_t *,
    nvlist_t *, nvlist_t *);
extern int zpool_destroy(zpool_handle_t *);
extern int zpool_add(zpool_handle_t *, nvlist_t *);

typedef struct splitflags {
	/* do not split, but return the config that would be split off */
	int dryrun : 1;

	/* after splitting, import the pool */
	int import : 1;
} splitflags_t;

/*
 * Functions to manipulate pool and vdev state
 */
extern int zpool_scan(zpool_handle_t *, pool_scan_func_t);
extern int zpool_clear(zpool_handle_t *, const char *, nvlist_t *, boolean_t);

extern int zpool_vdev_online(zpool_handle_t *, const char *, int,
    vdev_state_t *);
extern int zpool_vdev_offline(zpool_handle_t *, const char *, boolean_t);
extern int zpool_vdev_attach(zpool_handle_t *, const char *,
    const char *, nvlist_t *, int);
extern int zpool_vdev_detach(zpool_handle_t *, const char *);
extern int zpool_vdev_remove(zpool_handle_t *, const char *);
extern int zpool_vdev_split(zpool_handle_t *, char *, nvlist_t **, nvlist_t *,
    splitflags_t);

extern int zpool_vdev_fault(zpool_handle_t *, uint64_t, void *);
extern int zpool_vdev_degrade(zpool_handle_t *, uint64_t, void *);
extern int zpool_vdev_clear(zpool_handle_t *, uint64_t);

extern nvlist_t *zpool_find_vdev(zpool_handle_t *, const char *, boolean_t *,
    boolean_t *, boolean_t *, uint64_t *ashift);
extern int zpool_label_disk(libzfs_handle_t *, zpool_handle_t *, char *, int,
    int *);
extern nvlist_t *zpool_make_root_vdev(libzfs_handle_t *, zpool_handle_t *,
    boolean_t, int, boolean_t, boolean_t, int, int, char **);
extern nvlist_t *zpool_make_root_vdev_checkboot(libzfs_handle_t *,
    zpool_handle_t *, boolean_t, int, boolean_t, boolean_t, int, int, char **);
extern nvlist_t *zpool_split_root_vdev(zpool_handle_t *, char *, nvlist_t *,
    splitflags_t, int, char **);

/*
 * Property management.
 */

#define	ZFS_GET_NCOLS	5
#define	ZFS_MAX_HELP_COL 80

typedef enum {
	GET_COL_NONE,
	GET_COL_NAME,
	GET_COL_PROPERTY,
	GET_COL_VALUE,
	GET_COL_RECVD,
	GET_COL_SOURCE
} zfs_get_column_t;

typedef enum {
	ZPROP_SHOWTYPE_NONE,	/* properties are not displayed */
	ZPROP_SHOWTYPE_LIST,	/* each property is its own value column */
	ZPROP_SHOWTYPE_GET	/* properties share a single value column */
} zprop_showtype_t;

/*
 * This property description supports built-in ZFS properties and registered
 * external properties defined outside of ZFS.
 */
typedef struct zpropdef {
	const char *zpd_name;		/* property name */
	const char *zpd_owner;		/* property owner */
	void *zpd_opaque;		/* property description */
	const char *zpd_deleg;		/* delegated permission */
	int zpd_prop_id;		/* -1 if not a property */
	int zpd_sublist_id;		/* -1 if not a sublist */
	/*
	 * NULL if not in a sublist
	 */
	const struct zpropdef *zpd_parent_sublist;
	/*
	 * Bitmask of zfs_type_t values {poo||fs|snap|vol|share} for which this
	 * property is valid. If this entry is a sublist, the bitmask is a
	 * summary (OR'ing together) of the valid types across all properties in
	 * the sublist to any depth.
	 */
	int zpd_zfs_types;

	const char *const *zpd_values_list; /* for error message */
	int zpd_nvalues;		/* zpd_values_list count */
} zpropdef_t;

typedef struct zprop_list {
	const zpropdef_t *pl_prop;
	char		*pl_user_prop;
	struct zprop_list *pl_next;
	struct zprop_list *pl_alias;	/* resolution depends on zfs_handle_t */
	boolean_t	pl_all;
	boolean_t	pl_group;	/* requested as part of a sublist */
	size_t		pl_width;
	size_t		pl_recvd_width;
	boolean_t	pl_fixed;	/* fixed width */
	zprop_source_t	pl_source;
	int		pl_ntimes;	/* needed by how many datasets? */
} zprop_list_t;

typedef struct zprop_get_cbdata {
	libzfs_handle_t *cb_hdl;
	int cb_sources;
	zfs_get_column_t cb_columns[ZFS_GET_NCOLS];
	int cb_colwidths[ZFS_GET_NCOLS + 1];
	boolean_t cb_scripted;
	boolean_t cb_literal;
	boolean_t cb_first;
	boolean_t cb_goterror;
	zprop_list_t *cb_proplist;
	zfs_type_t cb_type;
	zfs_type_t cb_visited_types;
	const char *cb_prefix;
	int cb_prefixlen;
	boolean_t cb_expand_sublists;
} zprop_get_cbdata_t;

typedef enum zprop_spec {
	ZPROP_PROPERTY,
	ZPROP_SUBLIST,
	ZPROP_PROPERTY_OR_SUBLIST,
	ZPROP_INTERNAL,
	ZPROP_EXTERNAL
} zprop_spec_t;

#define	ZPROP_IS_SUBLIST(_xpd)	((_xpd) != NULL && (_xpd)->zpd_sublist_id > 0)
#define	ZPROP_IS_PROPERTY(_xpd)	((_xpd) != NULL && (_xpd)->zpd_prop_id >= 0)
/* This is a registered property defined outside of ZFS */
#define	ZPROP_IS_EXTERNAL(_xpd)	(ZPROP_IS_PROPERTY(_xpd) && \
    zprop_propnum(_xpd) == ZPROP_INVAL)
/* This is a built-in ZFS property */
#define	ZPROP_IS_INTERNAL(_xpd)	(ZPROP_IS_PROPERTY(_xpd) && \
    zprop_propnum(_xpd) != ZPROP_INVAL)

/*
 * Returned by zprop_func in zfs_prop_iter().
 *
 * ZPROP_STOP:	we don't need to iterate any further
 * ZPROP_ERR:	iteration should stop because of an error
 * ZPROP_CONT:	iteration should continue
 */
#define	ZPROP_STOP	0
#define	ZPROP_ERR	ZPROP_INVAL

typedef int zprop_func(const zpropdef_t *, void *);
typedef boolean_t zprop_filter_f(zfs_prop_t, const char *, zprop_source_t);

/*
 * These property management functions are layered on top of old style
 * index-based functions to add support for external properties.
 *
 * Property functions specific to zfs.
 */
extern int zfs_zprop_lookup(libzfs_handle_t *, const char *, zprop_spec_t,
    const zpropdef_t **);
extern int zfs_zprop_iter(libzfs_handle_t *, const char *, boolean_t,
    zprop_func *, void *);
extern int zfs_zprop_count(libzfs_handle_t *, zprop_spec_t);
extern int zprop_count(libzfs_handle_t *, zfs_type_t type, zprop_spec_t);
extern const zpropdef_t *zfs_zprop(libzfs_handle_t *, int);
extern int zfs_zprop_name_to_id(libzfs_handle_t *, const char *name);
extern const char *zfs_zprop_id_to_name(libzfs_handle_t *, int);
extern int zfs_register_default_sublist(libzfs_handle_t *, const char *);
extern void zfs_unregister_default_sublist(libzfs_handle_t *, const char *);
extern boolean_t zfs_prop_remap_is_valid(zfs_handle_t *, int);
extern int zfs_prop_get_unaliased(zfs_handle_t *, zfs_prop_t, char *, size_t,
    zprop_source_t *, char *, size_t);
extern int zfs_prop_set_unaliased(zfs_handle_t *, const char *, const char *,
    zprop_setflags_t);

extern int zfs_zprop_get(zfs_handle_t *, const zpropdef_t *, char *, size_t,
    zprop_source_t *, char *, size_t, boolean_t);
extern void zfs_get_collapsed_sublist(zfs_handle_t *, const zpropdef_t *,
    char *, size_t, zprop_source_t *, char *, size_t, boolean_t);
extern void zfs_get_collapsed_source(zfs_handle_t *, zfs_prop_t *,
    zprop_source_t *, char *, size_t, zprop_filter_f *);
extern void zfs_share_protocols_source(zfs_handle_t *, zprop_source_t *,
    char *, size_t);

/*
 * Property functions specific to zpool.
 */
extern const zpropdef_t *zpool_zprop(libzfs_handle_t *, int);

/*
 * Property functions common to zfs and zpool.
 */
extern int zprop_lookup(libzfs_handle_t *, zfs_type_t, const char *,
    zprop_spec_t, const zpropdef_t **);
extern int zprop_iter(libzfs_handle_t *, zfs_type_t, const char *, boolean_t,
    zprop_func *, void *);
extern const char *zprop_name(const zpropdef_t *);
extern int zprop_id(const zpropdef_t *);
extern int zprop_propnum(const zpropdef_t *);
extern boolean_t zprop_valid_for_type(const zpropdef_t *, zfs_type_t);
extern zprop_type_t zprop_type(const zpropdef_t *);
extern boolean_t zprop_is_boolean(const zpropdef_t *);
extern boolean_t zprop_is_string(const zpropdef_t *);
extern const char *zprop_strdefault(const zpropdef_t *);
extern uint64_t zprop_numdefault(const zpropdef_t *);
extern boolean_t zprop_readonly(const zpropdef_t *);
extern boolean_t zprop_inheritable(const zpropdef_t *);
extern boolean_t zprop_setonce(const zpropdef_t *);
extern boolean_t zprop_readonly(const zpropdef_t *);
extern const char *zprop_values(const zpropdef_t *);
extern int zprop_nvalues(const zpropdef_t *);
extern const char *zprop_value(const zpropdef_t *, int);
extern boolean_t zprop_is_size(const zpropdef_t *);
extern const char *zprop_deleg(const zpropdef_t *);
extern boolean_t zprop_visible(const zpropdef_t *);
extern boolean_t zprop_align_right(const zpropdef_t *);
extern const char *zprop_column_name(const zpropdef_t *);
extern int zprop_source_cmp(zprop_source_t, zprop_source_t);

/*
 * Property list functions.
 */
extern int zprop_get_list(libzfs_handle_t *, char *, zprop_list_t **,
    zfs_type_t, zprop_showtype_t, boolean_t);
extern void zprop_free_list(zprop_list_t *);
extern int zfs_expand_proplist(zfs_handle_t *, zprop_list_t **, int, boolean_t,
    boolean_t, zprop_showtype_t);
extern void zfs_prune_proplist(zfs_handle_t *, uint8_t *);
extern zprop_list_t *zfs_proplist_first(zprop_list_t *);
extern zprop_list_t *zfs_proplist_next(zprop_list_t *);
extern int zfs_proplist_resolve_alias(zfs_handle_t *, zprop_list_t *,
    zprop_list_t **);

typedef size_t (*zfs_proplist_width_f)(zprop_list_t *, void *);

extern size_t zfs_proplist_width(zprop_list_t *, zfs_proplist_width_f, void *);

/*
 * Property list functions specific to zpool.
 */
extern int zpool_expand_proplist(zpool_handle_t *, zprop_list_t **,
    zprop_showtype_t);

/*
 * Property help functions.
 */
extern void zprop_print_tall(libzfs_handle_t *, zfs_type_t type, FILE *fp);
extern void zprop_print_list(libzfs_handle_t *, zfs_type_t type, FILE *fp,
    boolean_t deleg_only);
extern void zprop_print_quota(libzfs_handle_t *, FILE *fp);
int zprop_parse_value(libzfs_handle_t *, nvpair_t *, int, zfs_type_t,
    nvlist_t *, char **, uint64_t *, const char *);
extern void zprop_print_one_property(const char *, zprop_get_cbdata_t *,
    const char *, const char *, zprop_source_t, const char *,
    const char *);
extern void zprop_describe_prop(FILE *, int, zfs_type_t);
extern void zprop_describe(FILE *fp, const zpropdef_t *zpd);
extern void zprop_describe_quota(FILE *, const char *);

/*
 * zfs_valid_proplist() flags
 */
typedef enum {
	ZFS_PROPLIST_NOVALIDATE = 0x1,
	ZFS_PROPLIST_UNMAPPED   = 0x2
} zfs_proplist_flags_t;

/*
 * Property nvlist functions.
 */
extern int zfs_prop_nvlist_add(libzfs_handle_t *, nvlist_t *, const char *,
    void *, zprop_source_t, zprop_setflags_t);

/*
 * Old style property management functions use a property index (zfs_prop_t)
 * into an array of built-in ZFS properties. Some functions are shared with the
 * kernel and are found in sys/fs/zfs.h.
 *
 * Functions that take a string (char *) instead of an index (zfs_prop_t) are
 * updated to handle external properties and don't need a zfs_zprop_* function
 * overlay.
 */

/*
 * zfs dataset property management
 */
extern const char *zfs_prop_default_string(zfs_prop_t);
extern uint64_t zfs_prop_default_numeric(zfs_prop_t);
extern const char *zfs_prop_column_name(zfs_prop_t);
extern boolean_t zfs_prop_align_right(zfs_prop_t);

extern const char *zfs_prop_to_name(zfs_prop_t);
extern int zfs_prop_set(zfs_handle_t *, const char *, const char *);
extern int zfs_prop_set_extended(zfs_handle_t *, const char *, const char *,
    zprop_setflags_t);
extern int zfs_prop_set_int(zfs_handle_t *, zfs_prop_t, uint64_t);
extern int zfs_prop_get(zfs_handle_t *, zfs_prop_t, char *, size_t,
    zprop_source_t *, char *, size_t, boolean_t);

extern int zfs_prop_get_persistent(zfs_handle_t *, zfs_prop_t, char *, size_t,
    zprop_source_t *, char *, size_t, boolean_t, boolean_t *);
extern int zfs_prop_get_recvd(zfs_handle_t *, const char *, char *, size_t,
    boolean_t);
extern int zfs_prop_get_numeric(zfs_handle_t *, zfs_prop_t, uint64_t *,
    zprop_source_t *, char *, size_t);
extern int zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
    uint64_t *propvalue);
extern int zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
    char *propbuf, int proplen, boolean_t literal, zprop_source_t *sourcetype);
extern uint64_t zfs_prop_get_int(zfs_handle_t *, zfs_prop_t);
extern boolean_t zfs_prop_is_error(zfs_handle_t *, zfs_prop_t);
extern boolean_t zfs_prop_get_error(zfs_handle_t *, zfs_prop_t, int *);
extern int zfs_prop_inherit(zfs_handle_t *, const char *, boolean_t);
extern int zfs_prop_predict(zfs_handle_t *, const char *, void *,
    zprop_source_t, zprop_setflags_t, nvlist_t **);
extern int zfs_props_predict(zfs_handle_t *, nvlist_t *,
    zprop_setflags_t, nvlist_t **);
extern const char *zfs_prop_values(zfs_prop_t);
extern int zfs_prop_is_string(zfs_prop_t prop);
extern nvlist_t *zfs_get_user_props(zfs_handle_t *);
extern nvlist_t *zfs_get_recvd_props(zfs_handle_t *);

/*
 * zpool property management
 */
extern const char *zpool_prop_default_string(zpool_prop_t);
extern uint64_t zpool_prop_default_numeric(zpool_prop_t);
extern const char *zpool_prop_column_name(zpool_prop_t);
extern boolean_t zpool_prop_align_right(zpool_prop_t);

extern int zpool_set_prop(zpool_handle_t *, const char *, const char *);
extern int zpool_get_prop(zpool_handle_t *, zpool_prop_t, char *,
    size_t proplen, zprop_source_t *);
extern boolean_t zpool_is_bootable(zpool_handle_t *);
extern uint64_t zpool_get_prop_int(zpool_handle_t *, zpool_prop_t,
    zprop_source_t *);

extern const char *zpool_prop_to_name(zpool_prop_t);
extern const char *zpool_prop_values(zpool_prop_t);

/*
 * Functions shared by zfs and zpool property management.
 */
extern int zc_prop_iter(zc_prop_func func, void *cb, boolean_t show_all,
    boolean_t ordered, zfs_type_t type);

/*
 * Pool health statistics.
 */
typedef enum {
	/*
	 * The following correspond to faults as defined in the (fault.fs.zfs.*)
	 * event namespace.  Each is associated with a corresponding message ID
	 * as defined in zfs_msgid_table[] in libzfs_status.c.
	 * The following are pool level failures.
	 */
	ZPOOL_STATUS_CORRUPT_CACHE,	/* corrupt or outdated cachefile */
	ZPOOL_STATUS_BAD_GUID_SUM,	/* sum of device guids didn't match */
	ZPOOL_STATUS_CORRUPT_POOL,	/* pool metadata is corrupted */
	ZPOOL_STATUS_CORRUPT_DATA,	/* data errors in user (meta)data */
	ZPOOL_STATUS_VERSION_NEWER,	/* newer on-disk version */
	ZPOOL_STATUS_HOSTID_MISMATCH,	/* last accessed by another system */
	ZPOOL_STATUS_IO_FAILURE_WAIT,	/* failed I/O, failmode 'wait' */
	ZPOOL_STATUS_IO_FAILURE_CONTINUE, /* failed I/O, failmode 'continue' */
	ZPOOL_STATUS_BAD_LOG,		/* cannot read log chain(s) */
	ZPOOL_STATUS_POOL_GUID_MISMATCH, /* uberblocks overwritten */

	/*
	 * The following are device level failures. The original reason for
	 * the failure is available through FMA msgid.
	 */
	ZPOOL_STATUS_FAILING_DEV,	/* degraded device */
	ZPOOL_STATUS_UNUSABLE_DEV_R,	/* faulted device with replicas */
	ZPOOL_STATUS_UNUSABLE_DEV_NR,	/* faulted device with no replicas */
	ZPOOL_STATUS_DEGRADED_DEV_DTL,	/* degraded device with dtl */

	/*
	 * The following are not faults per se, but still an error possibly
	 * requiring administrative attention.  There is no corresponding
	 * message ID.
	 */
	ZPOOL_STATUS_VERSION_OLDER,	/* older on-disk version */
	ZPOOL_STATUS_RESILVERING,	/* device being resilvered */
	ZPOOL_STATUS_OFFLINE_DEV,	/* device online */
	ZPOOL_STATUS_REMOVED_DEV,	/* removed device */
	ZPOOL_STATUS_TEMP_IMPORT,	/* imported using a temporary name */

	/*
	 * Finally, the following indicates a healthy pool.
	 */
	ZPOOL_STATUS_OK
} zpool_status_t;

extern zpool_status_t zpool_get_status(zpool_handle_t *, char **);
extern zpool_status_t zpool_import_status(nvlist_t *, char **);
extern void zpool_dump_ddt(const ddt_stat_t *dds, const ddt_histogram_t *ddh);

/*
 * Statistics and configuration functions.
 */
extern nvlist_t *zpool_get_config(zpool_handle_t *, nvlist_t **);
extern int zpool_refresh_stats(zpool_handle_t *, boolean_t *);
extern int zpool_get_errlog(zpool_handle_t *, nvlist_t **);

extern int zpool_get_monitor_data(zpool_handle_t *, char *, nvlist_t **);
/*
 * Import and export functions
 */
extern int zpool_export(zpool_handle_t *, boolean_t);
extern int zpool_export_force(zpool_handle_t *);
extern int zpool_import(libzfs_handle_t *, nvlist_t *, const char *,
    char *altroot);
extern int zpool_import_props(libzfs_handle_t *, nvlist_t *, const char *,
    nvlist_t *, int);

/*
 * Search for pools to import
 */

typedef struct importargs {
	char **path;		/* a list of paths to search		*/
	int paths;		/* number of paths to search		*/
	char *poolname;		/* name of a pool to find		*/
	uint64_t guid;		/* guid of a pool to find		*/
	char *cachefile;	/* cachefile to use for import		*/
	int can_be_active : 1;	/* can the pool be active?		*/
	int unique : 1;		/* does 'poolname' already exist?	*/
	int exists : 1;		/* set on return if pool already exists	*/
	int trust_cache : 1;	/* can the cachefile be trusted?	*/
	int no_validation : 1;	/* do not validate pool config		*/
} importargs_t;

extern nvlist_t *zpool_search_import(libzfs_handle_t *, importargs_t *);

/* legacy pool search routines and contracted interfaces */
extern nvlist_t *zpool_find_import(libzfs_handle_t *, int, char **);
extern nvlist_t *zpool_find_import_cached(libzfs_handle_t *, const char *,
    char *, uint64_t);
extern nvlist_t *zpool_find_import_cluster(libzfs_handle_t *, char *,
    uint64_t, const char *, boolean_t, char **, int);

/*
 * Miscellaneous pool functions
 */
struct zfs_cmd;

extern const char *zfs_history_event_names[LOG_END];

extern char *zpool_vdev_name(libzfs_handle_t *, zpool_handle_t *, nvlist_t *,
    boolean_t verbose, boolean_t location);
extern int zpool_validate_path(zpool_handle_t *zhp, nvlist_t *nv, char **pathp);
extern int zpool_upgrade(zpool_handle_t *, uint64_t);
extern int zpool_get_history(zpool_handle_t *, nvlist_t **);
extern int zpool_history_unpack(char *, uint64_t, uint64_t *,
    nvlist_t ***, uint_t *);
extern void zpool_set_history_str(const char *subcommand, int argc,
    char **argv, char *history_str);
extern int zpool_stage_history(libzfs_handle_t *, const char *);
extern void zpool_obj_to_path(zpool_handle_t *, uint64_t, uint64_t, char *,
    size_t len);
extern int zfs_ioctl(libzfs_handle_t *, int, struct zfs_cmd *);
extern int zpool_get_physpath(zpool_handle_t *, char *, size_t);
extern void zpool_explain_recover(libzfs_handle_t *, const char *, int,
    nvlist_t *);
extern nvlist_t *zpool_read_cachefile(libzfs_handle_t *, const char *);

/* Crypto related functions */
typedef enum {
	ZFS_CRYPTO_CREATE,
	ZFS_CRYPTO_PCREATE,
	ZFS_CRYPTO_CLONE,
	ZFS_CRYPTO_RECV,
	ZFS_CRYPTO_KEY_LOAD,
	ZFS_CRYPTO_KEY_CHANGE
} zfs_crypto_zckey_t;

extern int zfs_crypto_zckey(libzfs_handle_t *, zfs_crypto_zckey_t,
    nvlist_t *, struct zfs_cmd *);
extern int zfs_crypto_rename_check(zfs_handle_t *, struct zfs_cmd *);
extern boolean_t zfs_valid_keysource(char *);
extern boolean_t zfs_valid_set_keysource_change(zfs_handle_t *, char *);
extern boolean_t zfs_mount_crypto_check(zfs_handle_t *);
extern void libzfs_crypto_set_key(libzfs_handle_t *, char *, size_t);
extern void zfs_crypto_set_key(zfs_handle_t *, char *, size_t);
extern void zfs_crypto_set_clone_newkey(zfs_handle_t *);

/*
 * Basic handle manipulations.  These functions do not create or destroy the
 * underlying datasets, only the references to them.
 */
extern zfs_handle_t *zfs_open(libzfs_handle_t *, const char *, int);
extern zfs_handle_t *zfs_open_quiet(libzfs_handle_t *hdl, const char *path,
    int types);
extern void zfs_close(zfs_handle_t *);
extern zfs_type_t zfs_get_type(const zfs_handle_t *);
extern const char *zfs_get_name(const zfs_handle_t *);
extern boolean_t zfs_is_share(const zfs_handle_t *);
extern boolean_t zfs_is_auto_share(const zfs_handle_t *);
extern zfs_handle_t *zfs_share_to_fs(const zfs_handle_t *);
extern zfs_handle_t *zfs_fs_to_auto_share(const zfs_handle_t *);
extern zpool_handle_t *zfs_get_pool_handle(const zfs_handle_t *);

struct zfs_handle_cache; /* opaque zfs_handle_t cache */
typedef struct zfs_handle_cache zfs_handle_cache_t;
zfs_handle_cache_t *libzfs_get_cache(libzfs_handle_t *);

/*
 * zfs_handle_t cache functions
 */
extern zfs_handle_cache_t *zfs_cache_open(libzfs_handle_t *);
extern void zfs_cache_close(libzfs_handle_t *);
extern void zfs_cache_clear(zfs_handle_cache_t *);
extern zfs_handle_t *zfs_cache_get(libzfs_handle_t *, const char *,
    zfs_type_t);
extern zfs_handle_t *zfs_cache_get_quiet(libzfs_handle_t *, const char *,
    zfs_type_t);
extern boolean_t zfs_cache_add(zfs_handle_cache_t *, zfs_handle_t *);
extern void zfs_cache_remove(zfs_handle_cache_t *, zfs_handle_t *);
extern boolean_t zfs_cache_contains(zfs_handle_cache_t *, const char *,
    zfs_type_t, zfs_handle_t **);

extern zfs_handle_cache_t *zfs_get_cache(zfs_handle_t *);
extern zfs_handle_cache_t *libzfs_get_cache(libzfs_handle_t *);
extern boolean_t libzfs_cache_enable(libzfs_handle_t *);
extern void libzfs_cache_disable(libzfs_handle_t *);
extern void libzfs_cache_clear(libzfs_handle_t *);
extern boolean_t libzfs_is_cache_enabled(libzfs_handle_t *);
extern boolean_t zfs_is_cached(zfs_handle_t *);
extern void zfs_uncache_and_close(zfs_handle_t *);
extern void zfs_close_uncached(zfs_handle_t *);

/*
 * Iterator functions.
 */
typedef int (*zfs_iter_f)(zfs_handle_t *, void *);
extern int zfs_iter_root(libzfs_handle_t *, zfs_iter_f, void *);
extern int zfs_iter_children(zfs_handle_t *, zfs_iter_f, void *);
extern int zfs_iter_dependents(zfs_handle_t *, boolean_t, zfs_iter_f, void *);
extern int zfs_iter_filesystems(zfs_handle_t *, zfs_iter_f, void *);
extern int zfs_iter_snapshots(zfs_handle_t *, zfs_iter_f, void *);
extern int zfs_iter_snapshots_sorted(zfs_handle_t *, zfs_iter_f, void *);
extern int zfs_iter_shares(zfs_handle_t *, zfs_iter_f, void *);

extern int zfs_cache_iter_filesystems(libzfs_handle_t *, zfs_handle_t *,
    zfs_iter_f, void *);
extern int zfs_cache_iter_shares(libzfs_handle_t *, zfs_handle_t *,
    zfs_iter_f, void *);
extern int zfs_cache_iter_children(libzfs_handle_t *, zfs_handle_t *,
    zfs_iter_f, void *);
extern int zfs_cache_iter_dependents(zfs_handle_t *, boolean_t,
    zfs_iter_f func, void *data);
extern int zfs_cache_iter_root(libzfs_handle_t *, zfs_iter_f, void *);

typedef struct get_all_cb {
	zfs_handle_t	**cb_handles;
	size_t		cb_alloc;
	size_t		cb_used;
	boolean_t	cb_verbose;
	int		(*cb_getone)(zfs_handle_t *, void *);
} get_all_cb_t;

void libzfs_add_handle(get_all_cb_t *, zfs_handle_t *);
int libzfs_dataset_cmp(const void *, const void *);

typedef int (*zfs_hold_iter_f)(zfs_handle_t *, const char *, void *);
extern int zfs_iter_holds(zfs_handle_t *, zfs_hold_iter_f, void *);

/*
 * Functions to create and destroy datasets.
 */
extern int zfs_create(libzfs_handle_t *, const char *, zfs_type_t,
    nvlist_t *);
extern int zfs_check_parents(libzfs_handle_t *, const char *, uint64_t *,
    boolean_t, int *);
extern int zfs_set_fsid(libzfs_handle_t *, const char *, uint64_t);
extern int zfs_reset_fsid(libzfs_handle_t *, const char *, uint64_t);
extern int zfs_get_fsid(libzfs_handle_t *, const char *, uint64_t *);
extern int zfs_remove_fsid(libzfs_handle_t *, const char *, uint64_t *);
extern int zfs_create_ancestors(libzfs_handle_t *, const char *);
extern int zfs_destroy(zfs_handle_t *, boolean_t);
extern int zfs_destroy_snaps(zfs_handle_t *, char *, boolean_t);
extern int zfs_clone(zfs_handle_t *, const char *, nvlist_t *);

typedef enum snapfilter_action {
    SNAP_FILTER_EXCLUDE,
    SNAP_FILTER_INCLUDE,
    SNAP_FILTER_INCLUDE_DESCENDANTS
} snapfilter_action_t;

typedef snapfilter_action_t (*snap_filter_f)(libzfs_handle_t *hdl,
    void *cb_arg, const char *dsname);

extern snapfilter_action_t zfs_snap_recursive_all(libzfs_handle_t *, void *,
    const char *);
extern snapfilter_action_t zfs_snap_not_recursive(libzfs_handle_t *, void *,
    const char *);
extern int zfs_snapshot(libzfs_handle_t *, const char *path, nvlist_t *,
    snap_filter_f, void *);

#define	SNAP_RECURSIVE_ALL	zfs_snap_recursive_all
#define	SNAP_NOT_RECURSIVE	zfs_snap_not_recursive

extern int zfs_rollback(zfs_handle_t *, zfs_handle_t *, boolean_t);
extern int zfs_rename(zfs_handle_t *, const char *, boolean_t);

typedef struct sendflags {
	/* print informational messages (ie, -v was specified) */
	int verbose : 1;

	/* recursive or replication send  (ie, -r or -R) */
	int replicate : 1;

	/* for incrementals, do all intermediate snapshots */
	int doall : 1; /* (ie, -I) */

	/* if dataset is a clone, do incremental from its origin */
	int fromorigin : 1;

	/* do deduplication */
	int dedup : 1;

	/* send properties (ie, -p) */
	int props : 1;

	/* send received properties (ie, -b) */
	int received : 1;

	/* self-contained (ie, -c) */
	int selfcont : 1;

	/* don't send stream (ie, -n) */
	int dryrun : 1;

	/* which output goes to stderr (ie, -s) */
	int streamsize : 1;

	/* suppress warnings about missing snapshots during gather */
	int warn_missing_snaps : 1;
} sendflags_t;

typedef boolean_t (snapfilter_cb_t)(zfs_handle_t *, void *);

typedef struct zfs_send_args {
	const char	*zsa_fromsnap;
	const char	*zsa_tosnap;
	sendflags_t	zsa_flags;
	int 		zsa_outfd;
	snapfilter_cb_t *zsa_filter_func;
	void		*zsa_cb_arg;
	nvlist_t	**zsa_debugnvp;
	nvlist_t	*zsa_exclusions;
	const char	*zsa_send_tag;
	uint64_t	zsa_estimated_size;
} zfs_send_args_t;

extern int zfs_send(zfs_handle_t *, zfs_send_args_t *);
extern int zfs_promote(zfs_handle_t *);
extern int zfs_hold(zfs_handle_t *, const char *, const char *, boolean_t,
    boolean_t, boolean_t, int, uint64_t, uint64_t);
extern int zfs_release(zfs_handle_t *, const char *, const char *, boolean_t);
extern uint64_t zvol_volsize_to_reservation(uint64_t, nvlist_t *);

typedef int (*zfs_userspace_cb_t)(void *arg, const char *domain,
    uid_t rid, uint64_t space);

extern int zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
    zfs_userspace_cb_t func, void *arg);

typedef struct recvflags {
	/* print informational messages (ie, -v was specified) */
	int verbose : 1;

	/* the destination is a prefix, not the exact fs (ie, -d) */
	int isprefix : 1;

	/*
	 * Only the tail of the sent snapshot path is appended to the
	 * destination to determine the received snapshot name (ie, -e).
	 */
	int istail : 1;

	/* do not actually do the recv, just check if it would work (ie, -n) */
	int dryrun : 1;

	/* rollback/destroy filesystems as necessary (eg, -F) */
	int force : 1;

	/* set "canmount=off" on all modified filesystems */
	int canmountoff : 1;

	/* byteswap flag is used internally; callers need not specify */
	int byteswap : 1;

	/* do not mount file systems as they are extracted (private) */
	int nomount : 1;

	/* do not rename snapshots set for deferred destroy */
	int nosnaprename : 1;
} recvflags_t;

typedef enum recv_op {
	ZFS_RECV_SNAPDESTROY = 0x01,
	ZFS_RECV_SNAPRENAME,
	ZFS_RECV_START,
	ZFS_RECV_FINISH
} recv_op_t;

typedef boolean_t (*recv_cb_t)(libzfs_handle_t *, void *, recv_op_t, void *);

typedef struct zfs_recv_args {
	const char	*zsa_tosnap;
	recvflags_t	zsa_flags;
	nvlist_t	*zsa_props;
	int		zsa_infd;
	avl_tree_t	*zsa_stream_avl;
	recv_cb_t	zsa_recv_cb;
	void		*zsa_recv_cb_arg;
	const char	*zsa_recv_tag;
} zfs_recv_args_t;

extern int zfs_receive(libzfs_handle_t *, zfs_recv_args_t *);

typedef enum diff_flags {
	ZFS_DIFF_PARSEABLE = 0x01,
	ZFS_DIFF_TIMESTAMP = 0x02,
	ZFS_DIFF_CLASSIFY  = 0x04,
	ZFS_DIFF_ENUMERATE = 0x08,
	ZFS_DIFF_BASE	   = 0x10,
	ZFS_DIFF_NONAME    = 0x20,
	ZFS_DIFF_NOOLDNAME = 0x40,
	ZFS_DIFF_RECURSIVE = 0x80,
	ZFS_DIFF_ADDNEWDS  = 0x100,
	ZFS_DIFF_QUIET	   = 0x200
} diff_flags_t;

#define	ZDIFF_ADDED	'+'
#define	ZDIFF_MODIFIED	'M'
#define	ZDIFF_REMOVED	'-'
#define	ZDIFF_RENAMED	'R'
#define	ZDIFF_DATASET	'D'

struct zfs_stat;		/* forward declaration */

typedef void (diff_scan_cb_t)(char *dsmnt, char *ds, uint64_t guid,
    uint64_t obj, char type, char *fobjname, char *tobjname,
    struct zfs_stat *fsp, struct zfs_stat *tsp, int delta, void *arg);

extern int zfs_scan_diffs(zfs_handle_t *zhp, const char *fromsnap,
    const char *tosnap, int flags, diff_scan_cb_t *cb_func, void *cb_arg);

extern int zfs_show_diffs(zfs_handle_t *zhp, int outfd, const char *fromsnap,
    const char *tosnap, struct nvlist *fields, int flags);

/*
 * Command processing and help
 */
typedef struct zfs_command {
	const char	*name;
	int		(*func)(int argc, char **argv);
	int		usage;
} zfs_command_t;

extern void zfs_command_help(const char *, const char *);
extern void zfs_command_list(FILE *, const char *, zfs_command_t *);
extern int zfs_command_find(const char *, zfs_command_t *, int *);

/*
 * Miscellaneous functions.
 */
extern const char *zfs_type_to_name(zfs_type_t);
extern void zfs_refresh_properties(zfs_handle_t *);
extern int zfs_name_valid(const char *, zfs_type_t);
extern zfs_handle_t *zfs_path_to_zhandle(libzfs_handle_t *, const char *,
    zfs_type_t);
extern zfs_handle_t *zfs_path_to_zhandle_quiet(libzfs_handle_t *, const char *,
    zfs_type_t);
extern int zfs_path_to_mntpnt(libzfs_handle_t *, const char *, char *, size_t,
    char *, size_t, char *, size_t);
extern boolean_t zfs_dataset_exists(libzfs_handle_t *, const char *,
    zfs_type_t);
extern int zfs_spa_version(zfs_handle_t *, int *);
extern int zfs_parent_name(const char *, char *, size_t);

/*
 * Mount support functions.
 */
extern boolean_t is_mounted(libzfs_handle_t *, const char *special, char **);
extern boolean_t zfs_is_mounted(zfs_handle_t *, char **);
extern int zfs_mount(zfs_handle_t *, const char *, int);
extern int zfs_mountall(zfs_handle_t *, int);
extern int zfs_unmount(zfs_handle_t *, const char *, int);
extern int zfs_unmountall(zfs_handle_t *, int);

typedef struct shareflags {
	int share : 1;		/* publish the share */
	int validate : 1;	/* validate the share */
	int received : 1;	/* share is received from a ZFS send stream */
} shareflags_t;

/*
 * Share management support functions
 * These are direct interfaces to corresponding libshare routines.
 */
extern int zfs_share_validate(zfs_handle_t *, nvlist_t *, uint32_t, char *,
    int);
extern int zfs_share_remove(zfs_handle_t *, const char *);
extern int zfs_share_read(zfs_handle_t *, const char *, nvlist_t **);
extern int zfs_share_read_init(zfs_handle_t *, boolean_t, void **);
extern int zfs_share_read_next(zfs_handle_t *, void *, nvlist_t **);
extern void zfs_share_read_fini(zfs_handle_t *, void *);
extern int zfs_share_write(zfs_handle_t *, nvlist_t *, boolean_t, boolean_t);
extern int zfs_share_parse(zfs_handle_t *, const char *, int, nvlist_t **);
extern int zfs_share_merge(zfs_handle_t *, nvlist_t *, nvlist_t *, int);

extern libshare_handle_t *libzfs_get_libshare(libzfs_handle_t *);
extern void libzfs_set_libshare(libzfs_handle_t *, libshare_handle_t *);
extern libshare_handle_t *zfs_get_libshare(zfs_handle_t *);

/*
 * Miscellaneous share routines.
 */
extern int libzfs_setup_share_handling(libzfs_handle_t *);
extern boolean_t zfs_supports_zfs_shares(zfs_handle_t *, int);

/*
 * Share support functions.
 * zfs_main calls these
 */
extern boolean_t zfs_is_shared(zfs_handle_t *);
extern int zfs_share_create(libzfs_handle_t *, const char *, nvlist_t *,
    shareflags_t);
extern int zfs_share(zfs_handle_t *);
extern int zfs_unshare(zfs_handle_t *);
extern int zfs_share_one(zfs_handle_t *);
extern int zfs_unshare_one(zfs_handle_t *);
extern boolean_t zfs_is_sharing_requested(zfs_handle_t *);
extern int zfs_get_mountpoint(zfs_handle_t *, char *, size_t);
extern int zfs_get_sharepoint(zfs_handle_t *, char *, size_t);
extern void zfs_disable_auto_share(zfs_handle_t *);

/*
 * Protocol-specific share support functions.
 */
boolean_t zfs_is_sharing_enabled_mntpnt(zfs_handle_t *, boolean_t *,
    sa_proto_t *);
extern boolean_t zfs_is_shared_nfs(zfs_handle_t *, char **);
extern boolean_t zfs_is_shared_smb(zfs_handle_t *, char **);
extern int zfs_share_nfs(zfs_handle_t *);
extern int zfs_share_smb(zfs_handle_t *);
extern int zfs_shareall(zfs_handle_t *);
extern int zfs_unshare_nfs(zfs_handle_t *, const char *);
extern int zfs_unshare_smb(zfs_handle_t *, const char *);
extern int zfs_unshareall_nfs(zfs_handle_t *);
extern int zfs_unshareall_smb(zfs_handle_t *);
extern int zfs_unshareall_bypath(zfs_handle_t *, const char *);
extern int zfs_unshareall(zfs_handle_t *);

/*
 * Share persistence support functions
 * The libshare_zfs plugin calls these.
 */
extern int zfssa_share_storage_write(libzfs_handle_t *, nvlist_t *, boolean_t);
extern int zfssa_share_storage_read(libzfs_handle_t *, const char *,
    const char *, nvlist_t **);
extern int zfssa_share_storage_read_init(libzfs_handle_t *, const char *,
    boolean_t, void **);
extern int zfssa_share_storage_read_next(void *, nvlist_t **);
extern int zfssa_share_storage_read_fini(void *);
extern void zfssa_share_notify(zfs_handle_t *, nvlist_t *);
extern int zfssa_add_share_notification(zfs_handle_t *, nvlist_t *,
    const char *, zfs_share_state_t);
extern int zfssa_is_sharing_enabled_share(libzfs_handle_t *, const char *,
    const char *, sa_proto_t *);
extern int zfssa_sharing_get_prop(libzfs_handle_t *, const char *,
    const char *, sa_proto_t, char **);
extern int zfssa_sharing_set_prop(libzfs_handle_t *, const char *,
    const char *, sa_proto_t, const char *);

/*
 * Crypto key functions for datasets
 */
extern boolean_t zfs_is_encrypted(zfs_handle_t *);
extern boolean_t zfs_changing_key(zfs_handle_t *);

extern int zfs_key_load(zfs_handle_t *, boolean_t, boolean_t, boolean_t);
extern int zfs_key_unload(zfs_handle_t *, boolean_t);
extern int zfs_key_change(zfs_handle_t *, boolean_t, nvlist_t *);
extern int zfs_key_new(zfs_handle_t *);

/*
 * When dealing with nvlists, verify() is extremely useful
 */
#ifdef NDEBUG
#define	verify(EX)	((void)(EX))
#else
#define	verify(EX)	assert(EX)
#endif

/*
 * Utility function to convert a number to a human-readable form.
 */
extern void zfs_nicenum(uint64_t, char *, size_t);
extern int zfs_nicestrtonum(libzfs_handle_t *, const char *, uint64_t *);
extern void zfs_nicettc(uint64_t, char *, size_t);
extern void zfs_nicetime(uint64_t, char *, size_t);

/*
 * Given a device or file, determine if it is part of a pool.
 */
extern int zpool_in_use(libzfs_handle_t *, int, pool_state_t *, char **,
    boolean_t *);

/*
 * Label manipulation.
 */
extern int zpool_read_label(int, nvlist_t **);
extern int zpool_clear_label(int);

/* is this zvol valid for use as a dump device? */
extern int zvol_check_dump_config(char *);

/*
 * Management interfaces for SMB/NFS share resource files
 */
int zfs_share_resource_add(libzfs_handle_t *, char *, char *, char *);
int zfs_share_resource_remove(libzfs_handle_t *, char *, char *, char *,
    boolean_t);
int zfs_share_resource_purge(libzfs_handle_t *, char *, char *);
int zfs_share_resource_rename(libzfs_handle_t *, char *, char *, char *,
    char *);
int zfs_share_resource_read(libzfs_handle_t *, char *, char *, char *, char *,
    int);
int zfs_share_resource_write(libzfs_handle_t *, char *, char *, char *, char *,
    int);
char *make_share_filename(const char *, const char *);

/*
 * Enable and disable datasets within a pool by mounting/unmounting and
 * sharing/unsharing them.
 */
extern int zpool_enable_datasets(zpool_handle_t *, const char *, int);
extern int zpool_disable_datasets(zpool_handle_t *, boolean_t);

/*
 * FRU location management.
 */
extern int zpool_location_set(zpool_handle_t *, uint64_t, const char *,
    const char *, const char *);

/*
 * Error handling related fucntions.
 */
extern boolean_t libzfs_get_ignore_errs(libzfs_handle_t *);
extern void libzfs_set_ignore_errs(libzfs_handle_t *, boolean_t);

#ifdef	__cplusplus
}
#endif

#endif	/* _LIBZFS_H */