1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 5789Sahrens * Common Development and Distribution License, Version 1.0 only 6789Sahrens * (the "License"). You may not use this file except in compliance 7789Sahrens * with the License. 8789Sahrens * 9789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10789Sahrens * or http://www.opensolaris.org/os/licensing. 11789Sahrens * See the License for the specific language governing permissions 12789Sahrens * and limitations under the License. 13789Sahrens * 14789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 15789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16789Sahrens * If applicable, add the following below this CDDL HEADER, with the 17789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 18789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 19789Sahrens * 20789Sahrens * CDDL HEADER END 21789Sahrens */ 22789Sahrens /* 23*1294Slling * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 27789Sahrens #ifndef _LIBZFS_H 28789Sahrens #define _LIBZFS_H 29789Sahrens 30789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 31789Sahrens 32789Sahrens #include <assert.h> 33789Sahrens #include <libnvpair.h> 34789Sahrens #include <sys/param.h> 35789Sahrens #include <sys/types.h> 36789Sahrens #include <sys/varargs.h> 37789Sahrens #include <sys/fs/zfs.h> 38789Sahrens 39789Sahrens #ifdef __cplusplus 40789Sahrens extern "C" { 41789Sahrens #endif 42789Sahrens 43789Sahrens /* 44789Sahrens * Miscellaneous ZFS constants 45789Sahrens */ 46789Sahrens #define ZFS_MAXNAMELEN MAXNAMELEN 47789Sahrens #define ZPOOL_MAXNAMELEN MAXNAMELEN 48789Sahrens #define ZFS_MAXPROPLEN MAXPATHLEN 49789Sahrens 50789Sahrens /* 51789Sahrens * Basic handle types 52789Sahrens */ 53789Sahrens typedef struct zfs_handle zfs_handle_t; 54789Sahrens typedef struct zpool_handle zpool_handle_t; 55789Sahrens 56789Sahrens /* 57789Sahrens * Basic handle functions 58789Sahrens */ 59789Sahrens extern zpool_handle_t *zpool_open(const char *); 60789Sahrens extern zpool_handle_t *zpool_open_canfail(const char *); 61789Sahrens extern void zpool_close(zpool_handle_t *); 62789Sahrens extern const char *zpool_get_name(zpool_handle_t *); 63789Sahrens extern uint64_t zpool_get_guid(zpool_handle_t *); 64789Sahrens extern uint64_t zpool_get_space_used(zpool_handle_t *); 65789Sahrens extern uint64_t zpool_get_space_total(zpool_handle_t *); 66789Sahrens extern int zpool_get_root(zpool_handle_t *, char *, size_t); 67789Sahrens extern int zpool_get_state(zpool_handle_t *); 68789Sahrens 69789Sahrens /* 70789Sahrens * Iterate over all active pools in the system. 71789Sahrens */ 72789Sahrens typedef int (*zpool_iter_f)(zpool_handle_t *, void *); 73789Sahrens extern int zpool_iter(zpool_iter_f, void *); 74789Sahrens 75789Sahrens /* 76789Sahrens * Functions to create and destroy pools 77789Sahrens */ 78789Sahrens extern int zpool_create(const char *, nvlist_t *, const char *); 79789Sahrens extern int zpool_destroy(zpool_handle_t *); 80789Sahrens extern int zpool_add(zpool_handle_t *, nvlist_t *); 81789Sahrens 82789Sahrens /* 83789Sahrens * Functions to manipulate pool and vdev state 84789Sahrens */ 85789Sahrens extern int zpool_scrub(zpool_handle_t *, pool_scrub_type_t); 86789Sahrens 87789Sahrens extern int zpool_vdev_online(zpool_handle_t *, const char *); 88789Sahrens extern int zpool_vdev_offline(zpool_handle_t *, const char *); 89789Sahrens extern int zpool_vdev_attach(zpool_handle_t *, const char *, const char *, 90789Sahrens nvlist_t *, int); 91789Sahrens extern int zpool_vdev_detach(zpool_handle_t *, const char *); 92789Sahrens 93789Sahrens /* 94789Sahrens * Pool health statistics. 95789Sahrens */ 96789Sahrens typedef enum { 97789Sahrens /* 98789Sahrens * The following correspond to faults as defined in the (fault.fs.zfs.*) 991003Slling * event namespace. Each is associated with a corresponding message ID. 100789Sahrens */ 101789Sahrens ZPOOL_STATUS_CORRUPT_CACHE, /* corrupt /kernel/drv/zpool.cache */ 102789Sahrens ZPOOL_STATUS_MISSING_DEV_R, /* missing device with replicas */ 103789Sahrens ZPOOL_STATUS_MISSING_DEV_NR, /* missing device with no replicas */ 104789Sahrens ZPOOL_STATUS_CORRUPT_LABEL_R, /* bad device label with replicas */ 1051003Slling ZPOOL_STATUS_CORRUPT_LABEL_NR, /* bad device label with no replicas */ 106789Sahrens ZPOOL_STATUS_BAD_GUID_SUM, /* sum of device guids didn't match */ 107789Sahrens ZPOOL_STATUS_CORRUPT_POOL, /* pool metadata is corrupted */ 108789Sahrens ZPOOL_STATUS_CORRUPT_DATA, /* data errors in user (meta)data */ 109789Sahrens ZPOOL_STATUS_FAILING_DEV, /* device experiencing errors */ 110789Sahrens ZPOOL_STATUS_VERSION_MISMATCH, /* bad on-disk version */ 111789Sahrens 112789Sahrens /* 113789Sahrens * The following are not faults per se, but still an error possibly 1141003Slling * requiring administrative attention. There is no corresponding 115789Sahrens * message ID. 116789Sahrens */ 117789Sahrens ZPOOL_STATUS_RESILVERING, /* device being resilvered */ 118789Sahrens ZPOOL_STATUS_OFFLINE_DEV, /* device online */ 119789Sahrens 120789Sahrens /* 121789Sahrens * Finally, the following indicates a healthy pool. 122789Sahrens */ 123789Sahrens ZPOOL_STATUS_OK 124789Sahrens } zpool_status_t; 125789Sahrens 126789Sahrens extern zpool_status_t zpool_get_status(zpool_handle_t *, char **msgid); 127789Sahrens extern zpool_status_t zpool_import_status(nvlist_t *, char **msgid); 128789Sahrens 129789Sahrens /* 130789Sahrens * Statistics and configuration functions. 131789Sahrens */ 132952Seschrock extern nvlist_t *zpool_get_config(zpool_handle_t *, nvlist_t **oldconfig); 133952Seschrock extern int zpool_refresh_stats(zpool_handle_t *); 134789Sahrens 135789Sahrens /* 136789Sahrens * Import and export functions 137789Sahrens */ 138789Sahrens extern int zpool_export(zpool_handle_t *); 139789Sahrens extern int zpool_import(nvlist_t *, const char *, const char *); 140789Sahrens 141789Sahrens /* 142789Sahrens * Search for pools to import 143789Sahrens */ 144789Sahrens extern nvlist_t *zpool_find_import(int argc, char **argv); 145789Sahrens 146789Sahrens /* 147789Sahrens * Basic handle manipulations. These functions do not create or destroy the 148789Sahrens * underlying datasets, only the references to them. 149789Sahrens */ 150789Sahrens extern zfs_handle_t *zfs_open(const char *, int); 151789Sahrens extern void zfs_close(zfs_handle_t *); 152789Sahrens extern zfs_type_t zfs_get_type(const zfs_handle_t *); 153789Sahrens extern const char *zfs_get_name(const zfs_handle_t *); 154789Sahrens 155789Sahrens typedef enum { 156789Sahrens ZFS_SRC_NONE = 0x1, 157789Sahrens ZFS_SRC_DEFAULT = 0x2, 158789Sahrens ZFS_SRC_TEMPORARY = 0x4, 159789Sahrens ZFS_SRC_LOCAL = 0x8, 160789Sahrens ZFS_SRC_INHERITED = 0x10 161789Sahrens } zfs_source_t; 162789Sahrens 163789Sahrens #define ZFS_SRC_ALL 0x1f 164789Sahrens 165789Sahrens /* 166789Sahrens * Property management functions. Some functions are shared with the kernel, 1671003Slling * and are found in sys/fs/zfs.h. 168789Sahrens */ 169789Sahrens const char *zfs_prop_to_name(zfs_prop_t); 170789Sahrens int zfs_prop_set(zfs_handle_t *, zfs_prop_t, const char *); 171789Sahrens int zfs_prop_get(zfs_handle_t *, zfs_prop_t, char *, size_t, zfs_source_t *, 172789Sahrens char *, size_t, int); 173789Sahrens int zfs_prop_get_numeric(zfs_handle_t *, zfs_prop_t, uint64_t *, zfs_source_t *, 174789Sahrens char *, size_t); 175789Sahrens uint64_t zfs_prop_get_int(zfs_handle_t *, zfs_prop_t); 176789Sahrens int zfs_prop_validate(zfs_prop_t, const char *, uint64_t *); 177789Sahrens int zfs_prop_inheritable(zfs_prop_t); 178789Sahrens int zfs_prop_inherit(zfs_handle_t *, zfs_prop_t); 179789Sahrens const char *zfs_prop_values(zfs_prop_t); 180789Sahrens int zfs_prop_valid_for_type(zfs_prop_t, int); 181789Sahrens void zfs_prop_default_string(zfs_prop_t prop, char *buf, size_t buflen); 182789Sahrens uint64_t zfs_prop_default_numeric(zfs_prop_t); 183789Sahrens int zfs_prop_is_string(zfs_prop_t prop); 184789Sahrens const char *zfs_prop_column_name(zfs_prop_t); 185789Sahrens const char *zfs_prop_column_format(zfs_prop_t); 186866Seschrock int zfs_get_proplist(char *fields, zfs_prop_t *proplist, int max, int *count, 187866Seschrock char **badopt); 188789Sahrens 189789Sahrens #define ZFS_MOUNTPOINT_NONE "none" 190789Sahrens #define ZFS_MOUNTPOINT_LEGACY "legacy" 191789Sahrens 192789Sahrens /* 193789Sahrens * Iterator functions. 194789Sahrens */ 195789Sahrens typedef int (*zfs_iter_f)(zfs_handle_t *, void *); 196789Sahrens extern int zfs_iter_root(zfs_iter_f, void *); 197789Sahrens extern int zfs_iter_children(zfs_handle_t *, zfs_iter_f, void *); 198789Sahrens extern int zfs_iter_dependents(zfs_handle_t *, zfs_iter_f, void *); 199789Sahrens 200789Sahrens /* 201789Sahrens * Functions to create and destroy datasets. 202789Sahrens */ 203789Sahrens extern int zfs_create(const char *, zfs_type_t, const char *, const char *); 204789Sahrens extern int zfs_destroy(zfs_handle_t *); 205789Sahrens extern int zfs_clone(zfs_handle_t *, const char *); 206789Sahrens extern int zfs_snapshot(const char *); 207*1294Slling extern int zfs_rollback(zfs_handle_t *, zfs_handle_t *, int); 208789Sahrens extern int zfs_rename(zfs_handle_t *, const char *); 209789Sahrens extern int zfs_backup(zfs_handle_t *, zfs_handle_t *); 210789Sahrens extern int zfs_restore(const char *, int, int, int); 211789Sahrens 212789Sahrens /* 213789Sahrens * Miscellaneous functions. 214789Sahrens */ 215789Sahrens extern const char *zfs_type_to_name(zfs_type_t); 216789Sahrens extern void zfs_refresh_properties(zfs_handle_t *); 217789Sahrens extern int zfs_name_valid(const char *, zfs_type_t); 218789Sahrens 219789Sahrens /* 220789Sahrens * Mount support functions. 221789Sahrens */ 222789Sahrens extern int zfs_is_mounted(zfs_handle_t *, char **); 223789Sahrens extern int zfs_mount(zfs_handle_t *, const char *, int); 224789Sahrens extern int zfs_unmount(zfs_handle_t *, const char *, int); 225789Sahrens extern int zfs_unmountall(zfs_handle_t *, int); 226789Sahrens 227789Sahrens /* 228789Sahrens * Share support functions. 229789Sahrens */ 230789Sahrens extern int zfs_is_shared(zfs_handle_t *, char **); 231789Sahrens extern int zfs_share(zfs_handle_t *); 232789Sahrens extern int zfs_unshare(zfs_handle_t *, const char *); 233789Sahrens extern int zfs_unshareall(zfs_handle_t *); 234789Sahrens 235789Sahrens /* 236789Sahrens * For clients that need to capture error output. 237789Sahrens */ 238789Sahrens extern void zfs_set_error_handler(void (*)(const char *, va_list)); 239789Sahrens 240789Sahrens /* 241789Sahrens * When dealing with nvlists, verify() is extremely useful 242789Sahrens */ 243789Sahrens #ifdef NDEBUG 244789Sahrens #define verify(EX) ((void)(EX)) 245789Sahrens #else 246789Sahrens #define verify(EX) assert(EX) 247789Sahrens #endif 248789Sahrens 249789Sahrens /* 250789Sahrens * Utility function to convert a number to a human-readable form. 251789Sahrens */ 252789Sahrens extern void zfs_nicenum(uint64_t, char *, size_t); 253789Sahrens extern int zfs_nicestrtonum(const char *, uint64_t *); 254789Sahrens 255789Sahrens /* 256789Sahrens * Pool destroy special. Remove the device information without destroying 257789Sahrens * the underlying dataset. 258789Sahrens */ 259789Sahrens extern int zfs_remove_link(zfs_handle_t *); 260789Sahrens 261789Sahrens /* 262789Sahrens * Given a device or file, determine if it is part of a pool. 263789Sahrens */ 264789Sahrens extern int zpool_in_use(int fd, char **state, 265789Sahrens char **name); 266789Sahrens 267789Sahrens /* 268789Sahrens * ftyp special. Read the label from a given device. 269789Sahrens */ 270789Sahrens extern nvlist_t *zpool_read_label(int fd); 271789Sahrens 272789Sahrens /* 273789Sahrens * Create and remove zvol /dev links 274789Sahrens */ 275789Sahrens extern int zpool_create_zvol_links(zpool_handle_t *); 276789Sahrens extern int zpool_remove_zvol_links(zpool_handle_t *); 277789Sahrens 278789Sahrens /* 279789Sahrens * zoneadmd hack 280789Sahrens */ 281789Sahrens extern void zfs_init(void); 282789Sahrens 283789Sahrens /* 284789Sahrens * Useful defines 285789Sahrens */ 286789Sahrens #ifndef TRUE 287789Sahrens #define TRUE 1 288789Sahrens #endif 289789Sahrens #ifndef FALSE 290789Sahrens #define FALSE 0 291789Sahrens #endif 292789Sahrens 293789Sahrens #ifdef __cplusplus 294789Sahrens } 295789Sahrens #endif 296789Sahrens 297789Sahrens #endif /* _LIBZFS_H */ 298