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