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 */ 92*2474Seschrock EZFS_RECURSIVE, /* recursive dependency */ 932082Seschrock EZFS_UNKNOWN /* unknown error */ 942082Seschrock }; 952082Seschrock 962082Seschrock /* 97789Sahrens * Basic handle types 98789Sahrens */ 99789Sahrens typedef struct zfs_handle zfs_handle_t; 100789Sahrens typedef struct zpool_handle zpool_handle_t; 1012082Seschrock typedef struct libzfs_handle libzfs_handle_t; 1022082Seschrock 1032082Seschrock /* 1042082Seschrock * Library initialization 1052082Seschrock */ 1062082Seschrock extern libzfs_handle_t *libzfs_init(void); 1072082Seschrock extern void libzfs_fini(libzfs_handle_t *); 1082082Seschrock 1092082Seschrock extern libzfs_handle_t *zpool_get_handle(zpool_handle_t *); 1102082Seschrock extern libzfs_handle_t *zfs_get_handle(zfs_handle_t *); 1112082Seschrock 1122082Seschrock extern void libzfs_print_on_error(libzfs_handle_t *, boolean_t); 1132082Seschrock 1142082Seschrock extern int libzfs_errno(libzfs_handle_t *); 1152082Seschrock extern const char *libzfs_error_action(libzfs_handle_t *); 1162082Seschrock extern const char *libzfs_error_description(libzfs_handle_t *); 117789Sahrens 118789Sahrens /* 119789Sahrens * Basic handle functions 120789Sahrens */ 1212082Seschrock extern zpool_handle_t *zpool_open(libzfs_handle_t *, const char *); 1222082Seschrock extern zpool_handle_t *zpool_open_canfail(libzfs_handle_t *, const char *); 123789Sahrens extern void zpool_close(zpool_handle_t *); 124789Sahrens extern const char *zpool_get_name(zpool_handle_t *); 125789Sahrens extern uint64_t zpool_get_guid(zpool_handle_t *); 126789Sahrens extern uint64_t zpool_get_space_used(zpool_handle_t *); 127789Sahrens extern uint64_t zpool_get_space_total(zpool_handle_t *); 128789Sahrens extern int zpool_get_root(zpool_handle_t *, char *, size_t); 129789Sahrens extern int zpool_get_state(zpool_handle_t *); 1302082Seschrock extern uint64_t zpool_get_version(zpool_handle_t *); 131789Sahrens 132789Sahrens /* 133789Sahrens * Iterate over all active pools in the system. 134789Sahrens */ 135789Sahrens typedef int (*zpool_iter_f)(zpool_handle_t *, void *); 1362082Seschrock extern int zpool_iter(libzfs_handle_t *, zpool_iter_f, void *); 137789Sahrens 138789Sahrens /* 139789Sahrens * Functions to create and destroy pools 140789Sahrens */ 1412082Seschrock extern int zpool_create(libzfs_handle_t *, const char *, nvlist_t *, 1422082Seschrock const char *); 143789Sahrens extern int zpool_destroy(zpool_handle_t *); 144789Sahrens extern int zpool_add(zpool_handle_t *, nvlist_t *); 145789Sahrens 146789Sahrens /* 147789Sahrens * Functions to manipulate pool and vdev state 148789Sahrens */ 149789Sahrens extern int zpool_scrub(zpool_handle_t *, pool_scrub_type_t); 150789Sahrens 151789Sahrens extern int zpool_vdev_online(zpool_handle_t *, const char *); 1521485Slling extern int zpool_vdev_offline(zpool_handle_t *, const char *, int); 153789Sahrens extern int zpool_vdev_attach(zpool_handle_t *, const char *, const char *, 154789Sahrens nvlist_t *, int); 155789Sahrens extern int zpool_vdev_detach(zpool_handle_t *, const char *); 1562082Seschrock extern int zpool_vdev_remove(zpool_handle_t *, const char *); 1571544Seschrock extern int zpool_clear(zpool_handle_t *, const char *); 1582082Seschrock extern nvlist_t *zpool_find_vdev(zpool_handle_t *, const char *, boolean_t *); 159789Sahrens 160789Sahrens /* 161789Sahrens * Pool health statistics. 162789Sahrens */ 163789Sahrens typedef enum { 164789Sahrens /* 165789Sahrens * The following correspond to faults as defined in the (fault.fs.zfs.*) 1661003Slling * event namespace. Each is associated with a corresponding message ID. 167789Sahrens */ 168789Sahrens ZPOOL_STATUS_CORRUPT_CACHE, /* corrupt /kernel/drv/zpool.cache */ 169789Sahrens ZPOOL_STATUS_MISSING_DEV_R, /* missing device with replicas */ 170789Sahrens ZPOOL_STATUS_MISSING_DEV_NR, /* missing device with no replicas */ 171789Sahrens ZPOOL_STATUS_CORRUPT_LABEL_R, /* bad device label with replicas */ 1721003Slling ZPOOL_STATUS_CORRUPT_LABEL_NR, /* bad device label with no replicas */ 173789Sahrens ZPOOL_STATUS_BAD_GUID_SUM, /* sum of device guids didn't match */ 174789Sahrens ZPOOL_STATUS_CORRUPT_POOL, /* pool metadata is corrupted */ 175789Sahrens ZPOOL_STATUS_CORRUPT_DATA, /* data errors in user (meta)data */ 176789Sahrens ZPOOL_STATUS_FAILING_DEV, /* device experiencing errors */ 1771760Seschrock ZPOOL_STATUS_VERSION_NEWER, /* newer on-disk version */ 178789Sahrens 179789Sahrens /* 180789Sahrens * The following are not faults per se, but still an error possibly 1811003Slling * requiring administrative attention. There is no corresponding 182789Sahrens * message ID. 183789Sahrens */ 1841760Seschrock ZPOOL_STATUS_VERSION_OLDER, /* older on-disk version */ 185789Sahrens ZPOOL_STATUS_RESILVERING, /* device being resilvered */ 186789Sahrens ZPOOL_STATUS_OFFLINE_DEV, /* device online */ 187789Sahrens 188789Sahrens /* 189789Sahrens * Finally, the following indicates a healthy pool. 190789Sahrens */ 191789Sahrens ZPOOL_STATUS_OK 192789Sahrens } zpool_status_t; 193789Sahrens 1941544Seschrock extern zpool_status_t zpool_get_status(zpool_handle_t *, char **); 1951544Seschrock extern zpool_status_t zpool_import_status(nvlist_t *, char **); 196789Sahrens 197789Sahrens /* 198789Sahrens * Statistics and configuration functions. 199789Sahrens */ 2001544Seschrock extern nvlist_t *zpool_get_config(zpool_handle_t *, nvlist_t **); 2012142Seschrock extern int zpool_refresh_stats(zpool_handle_t *, boolean_t *); 2021544Seschrock extern int zpool_get_errlog(zpool_handle_t *, nvlist_t ***, size_t *); 2031544Seschrock 2041544Seschrock #define ZPOOL_ERR_DATASET "dataset" 2051544Seschrock #define ZPOOL_ERR_OBJECT "object" 2061544Seschrock #define ZPOOL_ERR_RANGE "range" 207789Sahrens 208789Sahrens /* 209789Sahrens * Import and export functions 210789Sahrens */ 211789Sahrens extern int zpool_export(zpool_handle_t *); 2122082Seschrock extern int zpool_import(libzfs_handle_t *, nvlist_t *, const char *, 2132082Seschrock const char *); 214789Sahrens 215789Sahrens /* 216789Sahrens * Search for pools to import 217789Sahrens */ 2182082Seschrock extern nvlist_t *zpool_find_import(libzfs_handle_t *, int, char **); 219789Sahrens 220789Sahrens /* 2211354Seschrock * Miscellaneous pool functions 2221354Seschrock */ 2232082Seschrock extern char *zpool_vdev_name(libzfs_handle_t *, zpool_handle_t *, nvlist_t *); 2241760Seschrock extern int zpool_upgrade(zpool_handle_t *); 2251354Seschrock 2261354Seschrock /* 227789Sahrens * Basic handle manipulations. These functions do not create or destroy the 228789Sahrens * underlying datasets, only the references to them. 229789Sahrens */ 2302082Seschrock extern zfs_handle_t *zfs_open(libzfs_handle_t *, const char *, int); 231789Sahrens extern void zfs_close(zfs_handle_t *); 232789Sahrens extern zfs_type_t zfs_get_type(const zfs_handle_t *); 233789Sahrens extern const char *zfs_get_name(const zfs_handle_t *); 234789Sahrens 235789Sahrens typedef enum { 236789Sahrens ZFS_SRC_NONE = 0x1, 237789Sahrens ZFS_SRC_DEFAULT = 0x2, 238789Sahrens ZFS_SRC_TEMPORARY = 0x4, 239789Sahrens ZFS_SRC_LOCAL = 0x8, 240789Sahrens ZFS_SRC_INHERITED = 0x10 241789Sahrens } zfs_source_t; 242789Sahrens 243789Sahrens #define ZFS_SRC_ALL 0x1f 244789Sahrens 245789Sahrens /* 246789Sahrens * Property management functions. Some functions are shared with the kernel, 2471003Slling * and are found in sys/fs/zfs.h. 248789Sahrens */ 249789Sahrens const char *zfs_prop_to_name(zfs_prop_t); 250789Sahrens int zfs_prop_set(zfs_handle_t *, zfs_prop_t, const char *); 251789Sahrens int zfs_prop_get(zfs_handle_t *, zfs_prop_t, char *, size_t, zfs_source_t *, 2522082Seschrock char *, size_t, boolean_t); 253789Sahrens int zfs_prop_get_numeric(zfs_handle_t *, zfs_prop_t, uint64_t *, zfs_source_t *, 254789Sahrens char *, size_t); 255789Sahrens uint64_t zfs_prop_get_int(zfs_handle_t *, zfs_prop_t); 2562082Seschrock int zfs_prop_validate(libzfs_handle_t *, zfs_prop_t, const char *, uint64_t *); 257789Sahrens int zfs_prop_inheritable(zfs_prop_t); 258789Sahrens int zfs_prop_inherit(zfs_handle_t *, zfs_prop_t); 259789Sahrens const char *zfs_prop_values(zfs_prop_t); 260789Sahrens int zfs_prop_valid_for_type(zfs_prop_t, int); 2611356Seschrock const char *zfs_prop_default_string(zfs_prop_t prop); 262789Sahrens uint64_t zfs_prop_default_numeric(zfs_prop_t); 263789Sahrens int zfs_prop_is_string(zfs_prop_t prop); 264789Sahrens const char *zfs_prop_column_name(zfs_prop_t); 265789Sahrens const char *zfs_prop_column_format(zfs_prop_t); 266866Seschrock int zfs_get_proplist(char *fields, zfs_prop_t *proplist, int max, int *count, 267866Seschrock char **badopt); 268789Sahrens 269789Sahrens #define ZFS_MOUNTPOINT_NONE "none" 270789Sahrens #define ZFS_MOUNTPOINT_LEGACY "legacy" 271789Sahrens 272789Sahrens /* 273789Sahrens * Iterator functions. 274789Sahrens */ 275789Sahrens typedef int (*zfs_iter_f)(zfs_handle_t *, void *); 2762082Seschrock extern int zfs_iter_root(libzfs_handle_t *, zfs_iter_f, void *); 277789Sahrens extern int zfs_iter_children(zfs_handle_t *, zfs_iter_f, void *); 278*2474Seschrock extern int zfs_iter_dependents(zfs_handle_t *, boolean_t, zfs_iter_f, void *); 2791356Seschrock extern int zfs_iter_filesystems(zfs_handle_t *, zfs_iter_f, void *); 2801356Seschrock extern int zfs_iter_snapshots(zfs_handle_t *, zfs_iter_f, void *); 281789Sahrens 282789Sahrens /* 283789Sahrens * Functions to create and destroy datasets. 284789Sahrens */ 2852082Seschrock extern int zfs_create(libzfs_handle_t *, const char *, zfs_type_t, 2862082Seschrock const char *, const char *); 287789Sahrens extern int zfs_destroy(zfs_handle_t *); 2882199Sahrens extern int zfs_destroy_snaps(zfs_handle_t *, char *); 289789Sahrens extern int zfs_clone(zfs_handle_t *, const char *); 2902199Sahrens extern int zfs_snapshot(libzfs_handle_t *, const char *, boolean_t); 2911294Slling extern int zfs_rollback(zfs_handle_t *, zfs_handle_t *, int); 292789Sahrens extern int zfs_rename(zfs_handle_t *, const char *); 2931749Sahrens extern int zfs_send(zfs_handle_t *, zfs_handle_t *); 2942082Seschrock extern int zfs_receive(libzfs_handle_t *, const char *, int, int, int); 2952082Seschrock extern int zfs_promote(zfs_handle_t *); 296789Sahrens 297789Sahrens /* 298789Sahrens * Miscellaneous functions. 299789Sahrens */ 300789Sahrens extern const char *zfs_type_to_name(zfs_type_t); 301789Sahrens extern void zfs_refresh_properties(zfs_handle_t *); 302789Sahrens extern int zfs_name_valid(const char *, zfs_type_t); 303789Sahrens 304789Sahrens /* 305789Sahrens * Mount support functions. 306789Sahrens */ 3072082Seschrock extern boolean_t zfs_is_mounted(zfs_handle_t *, char **); 308789Sahrens extern int zfs_mount(zfs_handle_t *, const char *, int); 309789Sahrens extern int zfs_unmount(zfs_handle_t *, const char *, int); 310789Sahrens extern int zfs_unmountall(zfs_handle_t *, int); 311789Sahrens 312789Sahrens /* 313789Sahrens * Share support functions. 314789Sahrens */ 3152082Seschrock extern boolean_t zfs_is_shared(zfs_handle_t *, char **); 316789Sahrens extern int zfs_share(zfs_handle_t *); 317789Sahrens extern int zfs_unshare(zfs_handle_t *, const char *); 318789Sahrens extern int zfs_unshareall(zfs_handle_t *); 319789Sahrens 320789Sahrens /* 321789Sahrens * When dealing with nvlists, verify() is extremely useful 322789Sahrens */ 323789Sahrens #ifdef NDEBUG 324789Sahrens #define verify(EX) ((void)(EX)) 325789Sahrens #else 326789Sahrens #define verify(EX) assert(EX) 327789Sahrens #endif 328789Sahrens 329789Sahrens /* 330789Sahrens * Utility function to convert a number to a human-readable form. 331789Sahrens */ 332789Sahrens extern void zfs_nicenum(uint64_t, char *, size_t); 333789Sahrens extern int zfs_nicestrtonum(const char *, uint64_t *); 334789Sahrens 335789Sahrens /* 336789Sahrens * Pool destroy special. Remove the device information without destroying 337789Sahrens * the underlying dataset. 338789Sahrens */ 339789Sahrens extern int zfs_remove_link(zfs_handle_t *); 340789Sahrens 341789Sahrens /* 342789Sahrens * Given a device or file, determine if it is part of a pool. 343789Sahrens */ 3442082Seschrock extern int zpool_in_use(libzfs_handle_t *, int, pool_state_t *, char **, 3452082Seschrock boolean_t *); 346789Sahrens 347789Sahrens /* 348789Sahrens * ftyp special. Read the label from a given device. 349789Sahrens */ 3502082Seschrock extern int zpool_read_label(int, nvlist_t **); 351789Sahrens 352789Sahrens /* 353789Sahrens * Create and remove zvol /dev links 354789Sahrens */ 355789Sahrens extern int zpool_create_zvol_links(zpool_handle_t *); 356789Sahrens extern int zpool_remove_zvol_links(zpool_handle_t *); 357789Sahrens 358*2474Seschrock /* 359*2474Seschrock * Mount and unmount datasets within a pool 360*2474Seschrock */ 361*2474Seschrock extern int zpool_mount_datasets(zpool_handle_t *, const char *); 362*2474Seschrock extern int zpool_unmount_datasets(zpool_handle_t *, boolean_t); 363*2474Seschrock 364789Sahrens #ifdef __cplusplus 365789Sahrens } 366789Sahrens #endif 367789Sahrens 368789Sahrens #endif /* _LIBZFS_H */ 369