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 /* 50*2082Seschrock * libzfs errors 51*2082Seschrock */ 52*2082Seschrock enum { 53*2082Seschrock EZFS_NOMEM = 2000, /* out of memory */ 54*2082Seschrock EZFS_BADPROP, /* invalid property value */ 55*2082Seschrock EZFS_PROPREADONLY, /* cannot set readonly property */ 56*2082Seschrock EZFS_PROPTYPE, /* property does not apply to dataset type */ 57*2082Seschrock EZFS_PROPNONINHERIT, /* property is not inheritable */ 58*2082Seschrock EZFS_PROPSPACE, /* bad quota or reservation */ 59*2082Seschrock EZFS_BADTYPE, /* dataset is not of appropriate type */ 60*2082Seschrock EZFS_BUSY, /* pool or dataset is busy */ 61*2082Seschrock EZFS_EXISTS, /* pool or dataset already exists */ 62*2082Seschrock EZFS_NOENT, /* no such pool or dataset */ 63*2082Seschrock EZFS_BADSTREAM, /* bad backup stream */ 64*2082Seschrock EZFS_DSREADONLY, /* dataset is readonly */ 65*2082Seschrock EZFS_VOLTOOBIG, /* volume is too large for 32-bit system */ 66*2082Seschrock EZFS_VOLHASDATA, /* volume already contains data */ 67*2082Seschrock EZFS_INVALIDNAME, /* invalid dataset name */ 68*2082Seschrock EZFS_BADRESTORE, /* unable to restore to destination */ 69*2082Seschrock EZFS_BADBACKUP, /* backup failed */ 70*2082Seschrock EZFS_BADTARGET, /* bad attach/detach/replace target */ 71*2082Seschrock EZFS_NODEVICE, /* no such device in pool */ 72*2082Seschrock EZFS_BADDEV, /* invalid device to add */ 73*2082Seschrock EZFS_NOREPLICAS, /* no valid replicas */ 74*2082Seschrock EZFS_RESILVERING, /* currently resilvering */ 75*2082Seschrock EZFS_BADVERSION, /* unsupported version */ 76*2082Seschrock EZFS_POOLUNAVAIL, /* pool is currently unavailable */ 77*2082Seschrock EZFS_DEVOVERFLOW, /* too many devices in one vdev */ 78*2082Seschrock EZFS_BADPATH, /* must be an absolute path */ 79*2082Seschrock EZFS_CROSSTARGET, /* rename or clone across pool or dataset */ 80*2082Seschrock EZFS_ZONED, /* used improperly in local zone */ 81*2082Seschrock EZFS_MOUNTFAILED, /* failed to mount dataset */ 82*2082Seschrock EZFS_UMOUNTFAILED, /* failed to unmount dataset */ 83*2082Seschrock EZFS_UNSHAREFAILED, /* unshare(1M) failed */ 84*2082Seschrock EZFS_SHAREFAILED, /* share(1M) failed */ 85*2082Seschrock EZFS_DEVLINKS, /* failed to create zvol links */ 86*2082Seschrock EZFS_PERM, /* permission denied */ 87*2082Seschrock EZFS_NOSPC, /* out of space */ 88*2082Seschrock EZFS_IO, /* I/O error */ 89*2082Seschrock EZFS_INTR, /* signal received */ 90*2082Seschrock EZFS_ISSPARE, /* device is a hot spare */ 91*2082Seschrock EZFS_INVALCONFIG, /* invalid vdev configuration */ 92*2082Seschrock EZFS_UNKNOWN /* unknown error */ 93*2082Seschrock }; 94*2082Seschrock 95*2082Seschrock /* 96789Sahrens * Basic handle types 97789Sahrens */ 98789Sahrens typedef struct zfs_handle zfs_handle_t; 99789Sahrens typedef struct zpool_handle zpool_handle_t; 100*2082Seschrock typedef struct libzfs_handle libzfs_handle_t; 101*2082Seschrock 102*2082Seschrock /* 103*2082Seschrock * Library initialization 104*2082Seschrock */ 105*2082Seschrock extern libzfs_handle_t *libzfs_init(void); 106*2082Seschrock extern void libzfs_fini(libzfs_handle_t *); 107*2082Seschrock 108*2082Seschrock extern libzfs_handle_t *zpool_get_handle(zpool_handle_t *); 109*2082Seschrock extern libzfs_handle_t *zfs_get_handle(zfs_handle_t *); 110*2082Seschrock 111*2082Seschrock extern void libzfs_print_on_error(libzfs_handle_t *, boolean_t); 112*2082Seschrock 113*2082Seschrock extern int libzfs_errno(libzfs_handle_t *); 114*2082Seschrock extern const char *libzfs_error_action(libzfs_handle_t *); 115*2082Seschrock extern const char *libzfs_error_description(libzfs_handle_t *); 116789Sahrens 117789Sahrens /* 118789Sahrens * Basic handle functions 119789Sahrens */ 120*2082Seschrock extern zpool_handle_t *zpool_open(libzfs_handle_t *, const char *); 121*2082Seschrock 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 *); 129*2082Seschrock 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 *); 135*2082Seschrock extern int zpool_iter(libzfs_handle_t *, zpool_iter_f, void *); 136789Sahrens 137789Sahrens /* 138789Sahrens * Functions to create and destroy pools 139789Sahrens */ 140*2082Seschrock extern int zpool_create(libzfs_handle_t *, const char *, nvlist_t *, 141*2082Seschrock 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 *); 155*2082Seschrock extern int zpool_vdev_remove(zpool_handle_t *, const char *); 1561544Seschrock extern int zpool_clear(zpool_handle_t *, const char *); 157*2082Seschrock 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 **); 200952Seschrock extern int zpool_refresh_stats(zpool_handle_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 *); 211*2082Seschrock extern int zpool_import(libzfs_handle_t *, nvlist_t *, const char *, 212*2082Seschrock const char *); 213789Sahrens 214789Sahrens /* 215789Sahrens * Search for pools to import 216789Sahrens */ 217*2082Seschrock extern nvlist_t *zpool_find_import(libzfs_handle_t *, int, char **); 218789Sahrens 219789Sahrens /* 2201354Seschrock * Miscellaneous pool functions 2211354Seschrock */ 222*2082Seschrock 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 */ 229*2082Seschrock 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 *, 251*2082Seschrock 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); 255*2082Seschrock 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 *); 275*2082Seschrock 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 */ 284*2082Seschrock extern int zfs_create(libzfs_handle_t *, const char *, zfs_type_t, 285*2082Seschrock const char *, const char *); 286789Sahrens extern int zfs_destroy(zfs_handle_t *); 287789Sahrens extern int zfs_clone(zfs_handle_t *, const char *); 288*2082Seschrock extern int zfs_snapshot(libzfs_handle_t *, const char *); 2891294Slling extern int zfs_rollback(zfs_handle_t *, zfs_handle_t *, int); 290789Sahrens extern int zfs_rename(zfs_handle_t *, const char *); 2911749Sahrens extern int zfs_send(zfs_handle_t *, zfs_handle_t *); 292*2082Seschrock extern int zfs_receive(libzfs_handle_t *, const char *, int, int, int); 293*2082Seschrock extern int zfs_promote(zfs_handle_t *); 294789Sahrens 295789Sahrens /* 296789Sahrens * Miscellaneous functions. 297789Sahrens */ 298789Sahrens extern const char *zfs_type_to_name(zfs_type_t); 299789Sahrens extern void zfs_refresh_properties(zfs_handle_t *); 300789Sahrens extern int zfs_name_valid(const char *, zfs_type_t); 301789Sahrens 302789Sahrens /* 303789Sahrens * Mount support functions. 304789Sahrens */ 305*2082Seschrock extern boolean_t zfs_is_mounted(zfs_handle_t *, char **); 306789Sahrens extern int zfs_mount(zfs_handle_t *, const char *, int); 307789Sahrens extern int zfs_unmount(zfs_handle_t *, const char *, int); 308789Sahrens extern int zfs_unmountall(zfs_handle_t *, int); 309789Sahrens 310789Sahrens /* 311789Sahrens * Share support functions. 312789Sahrens */ 313*2082Seschrock extern boolean_t zfs_is_shared(zfs_handle_t *, char **); 314789Sahrens extern int zfs_share(zfs_handle_t *); 315789Sahrens extern int zfs_unshare(zfs_handle_t *, const char *); 316789Sahrens extern int zfs_unshareall(zfs_handle_t *); 317789Sahrens 318789Sahrens /* 319789Sahrens * When dealing with nvlists, verify() is extremely useful 320789Sahrens */ 321789Sahrens #ifdef NDEBUG 322789Sahrens #define verify(EX) ((void)(EX)) 323789Sahrens #else 324789Sahrens #define verify(EX) assert(EX) 325789Sahrens #endif 326789Sahrens 327789Sahrens /* 328789Sahrens * Utility function to convert a number to a human-readable form. 329789Sahrens */ 330789Sahrens extern void zfs_nicenum(uint64_t, char *, size_t); 331789Sahrens extern int zfs_nicestrtonum(const char *, uint64_t *); 332789Sahrens 333789Sahrens /* 334789Sahrens * Pool destroy special. Remove the device information without destroying 335789Sahrens * the underlying dataset. 336789Sahrens */ 337789Sahrens extern int zfs_remove_link(zfs_handle_t *); 338789Sahrens 339789Sahrens /* 340789Sahrens * Given a device or file, determine if it is part of a pool. 341789Sahrens */ 342*2082Seschrock extern int zpool_in_use(libzfs_handle_t *, int, pool_state_t *, char **, 343*2082Seschrock boolean_t *); 344789Sahrens 345789Sahrens /* 346789Sahrens * ftyp special. Read the label from a given device. 347789Sahrens */ 348*2082Seschrock extern int zpool_read_label(int, nvlist_t **); 349789Sahrens 350789Sahrens /* 351789Sahrens * Create and remove zvol /dev links 352789Sahrens */ 353789Sahrens extern int zpool_create_zvol_links(zpool_handle_t *); 354789Sahrens extern int zpool_remove_zvol_links(zpool_handle_t *); 355789Sahrens 356789Sahrens #ifdef __cplusplus 357789Sahrens } 358789Sahrens #endif 359789Sahrens 360789Sahrens #endif /* _LIBZFS_H */ 361