1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or https://opensource.org/licenses/CDDL-1.0. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2018, 2024 by Delphix. All rights reserved. 25 */ 26 27 #ifndef _LIBZUTIL_H 28 #define _LIBZUTIL_H extern __attribute__((visibility("default"))) 29 30 #include <string.h> 31 #include <pthread.h> 32 #include <sys/nvpair.h> 33 #include <sys/fs/zfs.h> 34 35 #ifdef __cplusplus 36 extern "C" { 37 #endif 38 39 /* 40 * Default wait time in milliseconds for a device name to be created. 41 */ 42 #define DISK_LABEL_WAIT (30 * 1000) /* 30 seconds */ 43 44 45 /* 46 * Pool Config Operations 47 * 48 * These are specific to the library libzfs or libzpool instance. 49 */ 50 typedef nvlist_t *refresh_config_func_t(void *, nvlist_t *); 51 52 typedef int pool_active_func_t(void *, const char *, uint64_t, boolean_t *); 53 54 typedef const struct pool_config_ops { 55 refresh_config_func_t *pco_refresh_config; 56 pool_active_func_t *pco_pool_active; 57 } pool_config_ops_t; 58 59 /* 60 * An instance of pool_config_ops_t is expected in the caller's binary. 61 */ 62 _LIBZUTIL_H pool_config_ops_t libzfs_config_ops; 63 _LIBZUTIL_H pool_config_ops_t libzpool_config_ops; 64 65 typedef enum lpc_error { 66 LPC_SUCCESS = 0, /* no error -- success */ 67 LPC_BADCACHE = 2000, /* out of memory */ 68 LPC_BADPATH, /* must be an absolute path */ 69 LPC_NOMEM, /* out of memory */ 70 LPC_EACCESS, /* some devices require root privileges */ 71 LPC_UNKNOWN 72 } lpc_error_t; 73 74 typedef struct importargs { 75 char **path; /* a list of paths to search */ 76 int paths; /* number of paths to search */ 77 const char *poolname; /* name of a pool to find */ 78 uint64_t guid; /* guid of a pool to find */ 79 const char *cachefile; /* cachefile to use for import */ 80 boolean_t can_be_active; /* can the pool be active? */ 81 boolean_t scan; /* prefer scanning to libblkid cache */ 82 nvlist_t *policy; /* load policy (max txg, rewind, etc.) */ 83 boolean_t do_destroyed; 84 boolean_t do_all; 85 } importargs_t; 86 87 typedef struct libpc_handle { 88 int lpc_error; 89 boolean_t lpc_printerr; 90 boolean_t lpc_open_access_error; 91 boolean_t lpc_desc_active; 92 char lpc_desc[1024]; 93 pool_config_ops_t *lpc_ops; 94 void *lpc_lib_handle; 95 } libpc_handle_t; 96 97 _LIBZUTIL_H const char *libpc_error_description(libpc_handle_t *); 98 _LIBZUTIL_H nvlist_t *zpool_search_import(libpc_handle_t *, importargs_t *); 99 _LIBZUTIL_H int zpool_find_config(libpc_handle_t *, const char *, nvlist_t **, 100 importargs_t *); 101 102 _LIBZUTIL_H const char * const * zpool_default_search_paths(size_t *count); 103 _LIBZUTIL_H int zpool_read_label(int, nvlist_t **, int *); 104 _LIBZUTIL_H int zpool_label_disk_wait(const char *, int); 105 _LIBZUTIL_H int zpool_disk_wait(const char *); 106 107 struct udev_device; 108 109 _LIBZUTIL_H int zfs_device_get_devid(struct udev_device *, char *, size_t); 110 _LIBZUTIL_H int zfs_device_get_physical(struct udev_device *, char *, size_t); 111 112 _LIBZUTIL_H void update_vdev_config_dev_strs(nvlist_t *); 113 114 /* 115 * Default device paths 116 */ 117 #define DISK_ROOT "/dev" 118 #define UDISK_ROOT "/dev/disk" 119 #define ZVOL_ROOT "/dev/zvol" 120 121 _LIBZUTIL_H int zfs_append_partition(char *path, size_t max_len); 122 _LIBZUTIL_H int zfs_resolve_shortname(const char *name, char *path, 123 size_t pathlen); 124 125 _LIBZUTIL_H char *zfs_strip_partition(const char *); 126 _LIBZUTIL_H const char *zfs_strip_path(const char *); 127 128 _LIBZUTIL_H int zfs_strcmp_pathname(const char *, const char *, int); 129 130 _LIBZUTIL_H boolean_t zfs_dev_is_dm(const char *); 131 _LIBZUTIL_H boolean_t zfs_dev_is_whole_disk(const char *); 132 _LIBZUTIL_H int zfs_dev_flush(int); 133 _LIBZUTIL_H char *zfs_get_underlying_path(const char *); 134 _LIBZUTIL_H char *zfs_get_enclosure_sysfs_path(const char *); 135 136 _LIBZUTIL_H boolean_t is_mpath_whole_disk(const char *); 137 138 _LIBZUTIL_H boolean_t zfs_isnumber(const char *); 139 140 /* 141 * Formats for iostat numbers. Examples: "12K", "30ms", "4B", "2321234", "-". 142 * 143 * ZFS_NICENUM_1024: Print kilo, mega, tera, peta, exa.. 144 * ZFS_NICENUM_BYTES: Print single bytes ("13B"), kilo, mega, tera... 145 * ZFS_NICENUM_TIME: Print nanosecs, microsecs, millisecs, seconds... 146 * ZFS_NICENUM_RAW: Print the raw number without any formatting 147 * ZFS_NICENUM_RAWTIME: Same as RAW, but print dashes ('-') for zero. 148 */ 149 enum zfs_nicenum_format { 150 ZFS_NICENUM_1024 = 0, 151 ZFS_NICENUM_BYTES = 1, 152 ZFS_NICENUM_TIME = 2, 153 ZFS_NICENUM_RAW = 3, 154 ZFS_NICENUM_RAWTIME = 4 155 }; 156 157 /* 158 * Convert a number to a human-readable form. 159 */ 160 _LIBZUTIL_H void zfs_nicebytes(uint64_t, char *, size_t); 161 _LIBZUTIL_H void zfs_nicenum(uint64_t, char *, size_t); 162 _LIBZUTIL_H void zfs_nicenum_format(uint64_t, char *, size_t, 163 enum zfs_nicenum_format); 164 _LIBZUTIL_H void zfs_nicetime(uint64_t, char *, size_t); 165 _LIBZUTIL_H void zfs_niceraw(uint64_t, char *, size_t); 166 167 #define nicenum(num, buf, size) zfs_nicenum(num, buf, size) 168 #define NN_NUMBUF_SZ (6) 169 170 _LIBZUTIL_H void zpool_dump_ddt(const ddt_stat_t *, const ddt_histogram_t *, 171 boolean_t parsable); 172 _LIBZUTIL_H int zpool_history_unpack(char *, uint64_t, uint64_t *, nvlist_t ***, 173 uint_t *); 174 _LIBZUTIL_H void fsleep(float sec); 175 _LIBZUTIL_H int zpool_getenv_int(const char *env, int default_val); 176 177 struct zfs_cmd; 178 179 /* 180 * List of colors to use 181 */ 182 #define ANSI_BLACK "\033[0;30m" 183 #define ANSI_RED "\033[0;31m" 184 #define ANSI_GREEN "\033[0;32m" 185 #define ANSI_YELLOW "\033[0;33m" 186 #define ANSI_BLUE "\033[0;34m" 187 #define ANSI_BOLD_BLUE "\033[1;34m" /* light blue */ 188 #define ANSI_MAGENTA "\033[0;35m" 189 #define ANSI_CYAN "\033[0;36m" 190 #define ANSI_GRAY "\033[0;37m" 191 192 #define ANSI_RESET "\033[0m" 193 #define ANSI_BOLD "\033[1m" 194 195 _LIBZUTIL_H int use_color(void); 196 _LIBZUTIL_H void color_start(const char *color); 197 _LIBZUTIL_H void color_end(void); 198 _LIBZUTIL_H int printf_color(const char *color, const char *format, ...); 199 200 _LIBZUTIL_H const char *zfs_basename(const char *path); 201 _LIBZUTIL_H ssize_t zfs_dirnamelen(const char *path); 202 #ifdef __linux__ 203 extern char **environ; 204 _LIBZUTIL_H void zfs_setproctitle_init(int argc, char *argv[], char *envp[]); 205 _LIBZUTIL_H void zfs_setproctitle(const char *fmt, ...); 206 #else 207 #define zfs_setproctitle(fmt, ...) setproctitle(fmt, ##__VA_ARGS__) 208 #define zfs_setproctitle_init(x, y, z) ((void)0) 209 #endif 210 211 /* 212 * These functions are used by the ZFS libraries and cmd/zpool code, but are 213 * not exported in the ABI. 214 */ 215 typedef int (*pool_vdev_iter_f)(void *, nvlist_t *, void *); 216 int for_each_vdev_cb(void *zhp, nvlist_t *nv, pool_vdev_iter_f func, 217 void *data); 218 int for_each_vdev_macro_helper_func(void *zhp_data, nvlist_t *nv, void *data); 219 int for_each_real_leaf_vdev_macro_helper_func(void *zhp_data, nvlist_t *nv, 220 void *data); 221 /* 222 * Often you'll want to iterate over all the vdevs in the pool, but don't want 223 * to use for_each_vdev() since it requires a callback function. 224 * 225 * Instead you can use FOR_EACH_VDEV(): 226 * 227 * zpool_handle_t *zhp // Assume this is initialized 228 * nvlist_t *nv 229 * ... 230 * FOR_EACH_VDEV(zhp, nv) { 231 * const char *path = NULL; 232 * nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path); 233 * printf("Looking at vdev %s\n", path); 234 * } 235 * 236 * Note: FOR_EACH_VDEV runs in O(n^2) time where n = number of vdevs. However, 237 * there's an upper limit of 256 vdevs per dRAID top-level vdevs (TLDs), 255 for 238 * raidz2 TLDs, a real world limit of ~500 vdevs for mirrors, so this shouldn't 239 * really be an issue. 240 * 241 * Here are some micro-benchmarks of a complete FOR_EACH_VDEV loop on a RAID0 242 * pool: 243 * 244 * 100 vdevs = 0.7ms 245 * 500 vdevs = 17ms 246 * 750 vdevs = 40ms 247 * 1000 vdevs = 82ms 248 * 249 * The '__nv += 0' at the end of the for() loop gets around a "comma or 250 * semicolon followed by non-blank" checkstyle error. Note on most compliers 251 * the '__nv += 0' can just be replaced with 'NULL', but gcc on Centos 7 252 * will give a 'warning: statement with no effect' error if you do that. 253 */ 254 #define __FOR_EACH_VDEV(__zhp, __nv, __func) { \ 255 __nv = zpool_get_config(__zhp, NULL); \ 256 VERIFY0(nvlist_lookup_nvlist(__nv, ZPOOL_CONFIG_VDEV_TREE, &__nv)); \ 257 } \ 258 for (nvlist_t *__root_nv = __nv, *__state = (nvlist_t *)0; \ 259 for_each_vdev_cb(&__state, __root_nv, __func, &__nv) == 1; \ 260 __nv += 0) 261 262 #define FOR_EACH_VDEV(__zhp, __nv) \ 263 __FOR_EACH_VDEV(__zhp, __nv, for_each_vdev_macro_helper_func) 264 265 /* 266 * "real leaf" vdevs are leaf vdevs that are real devices (disks or files). 267 * This excludes leaf vdevs like like draid spares. 268 */ 269 #define FOR_EACH_REAL_LEAF_VDEV(__zhp, __nv) \ 270 __FOR_EACH_VDEV(__zhp, __nv, for_each_real_leaf_vdev_macro_helper_func) 271 272 int for_each_vdev_in_nvlist(nvlist_t *nvroot, pool_vdev_iter_f func, 273 void *data); 274 void update_vdevs_config_dev_sysfs_path(nvlist_t *config); 275 _LIBZUTIL_H void update_vdev_config_dev_sysfs_path(nvlist_t *nv, 276 const char *path, const char *key); 277 278 /* 279 * Thread-safe strerror() for use in ZFS libraries 280 */ 281 static inline char *zfs_strerror(int errnum) { 282 static __thread char errbuf[512]; 283 static pthread_mutex_t zfs_strerror_lock = PTHREAD_MUTEX_INITIALIZER; 284 285 (void) pthread_mutex_lock(&zfs_strerror_lock); 286 (void) strlcpy(errbuf, strerror(errnum), sizeof (errbuf)); 287 (void) pthread_mutex_unlock(&zfs_strerror_lock); 288 289 return (errbuf); 290 } 291 292 #ifdef __cplusplus 293 } 294 #endif 295 296 #endif /* _LIBZUTIL_H */ 297