xref: /freebsd/sys/contrib/openzfs/include/libzutil.h (revision eda14cbc264d6969b02f2b1994cef11148e914f1)
1*eda14cbcSMatt Macy /*
2*eda14cbcSMatt Macy  * CDDL HEADER START
3*eda14cbcSMatt Macy  *
4*eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5*eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6*eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7*eda14cbcSMatt Macy  *
8*eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
10*eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11*eda14cbcSMatt Macy  * and limitations under the License.
12*eda14cbcSMatt Macy  *
13*eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14*eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16*eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17*eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18*eda14cbcSMatt Macy  *
19*eda14cbcSMatt Macy  * CDDL HEADER END
20*eda14cbcSMatt Macy  */
21*eda14cbcSMatt Macy /*
22*eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23*eda14cbcSMatt Macy  * Copyright (c) 2018 by Delphix. All rights reserved.
24*eda14cbcSMatt Macy  */
25*eda14cbcSMatt Macy 
26*eda14cbcSMatt Macy #ifndef	_LIBZUTIL_H
27*eda14cbcSMatt Macy #define	_LIBZUTIL_H
28*eda14cbcSMatt Macy 
29*eda14cbcSMatt Macy #include <sys/nvpair.h>
30*eda14cbcSMatt Macy #include <sys/fs/zfs.h>
31*eda14cbcSMatt Macy 
32*eda14cbcSMatt Macy #ifdef	__cplusplus
33*eda14cbcSMatt Macy extern "C" {
34*eda14cbcSMatt Macy #endif
35*eda14cbcSMatt Macy 
36*eda14cbcSMatt Macy /*
37*eda14cbcSMatt Macy  * Default wait time for a device name to be created.
38*eda14cbcSMatt Macy  */
39*eda14cbcSMatt Macy #define	DISK_LABEL_WAIT		(30 * 1000)  /* 30 seconds */
40*eda14cbcSMatt Macy 
41*eda14cbcSMatt Macy 
42*eda14cbcSMatt Macy /*
43*eda14cbcSMatt Macy  * Pool Config Operations
44*eda14cbcSMatt Macy  *
45*eda14cbcSMatt Macy  * These are specific to the library libzfs or libzpool instance.
46*eda14cbcSMatt Macy  */
47*eda14cbcSMatt Macy typedef nvlist_t *refresh_config_func_t(void *, nvlist_t *);
48*eda14cbcSMatt Macy 
49*eda14cbcSMatt Macy typedef int pool_active_func_t(void *, const char *, uint64_t, boolean_t *);
50*eda14cbcSMatt Macy 
51*eda14cbcSMatt Macy typedef const struct pool_config_ops {
52*eda14cbcSMatt Macy 	refresh_config_func_t	*pco_refresh_config;
53*eda14cbcSMatt Macy 	pool_active_func_t	*pco_pool_active;
54*eda14cbcSMatt Macy } pool_config_ops_t;
55*eda14cbcSMatt Macy 
56*eda14cbcSMatt Macy /*
57*eda14cbcSMatt Macy  * An instance of pool_config_ops_t is expected in the caller's binary.
58*eda14cbcSMatt Macy  */
59*eda14cbcSMatt Macy extern const pool_config_ops_t libzfs_config_ops;
60*eda14cbcSMatt Macy extern const pool_config_ops_t libzpool_config_ops;
61*eda14cbcSMatt Macy 
62*eda14cbcSMatt Macy typedef struct importargs {
63*eda14cbcSMatt Macy 	char **path;		/* a list of paths to search		*/
64*eda14cbcSMatt Macy 	int paths;		/* number of paths to search		*/
65*eda14cbcSMatt Macy 	const char *poolname;	/* name of a pool to find		*/
66*eda14cbcSMatt Macy 	uint64_t guid;		/* guid of a pool to find		*/
67*eda14cbcSMatt Macy 	const char *cachefile;	/* cachefile to use for import		*/
68*eda14cbcSMatt Macy 	boolean_t can_be_active; /* can the pool be active?		*/
69*eda14cbcSMatt Macy 	boolean_t scan;		/* prefer scanning to libblkid cache    */
70*eda14cbcSMatt Macy 	nvlist_t *policy;	/* load policy (max txg, rewind, etc.)	*/
71*eda14cbcSMatt Macy } importargs_t;
72*eda14cbcSMatt Macy 
73*eda14cbcSMatt Macy extern nvlist_t *zpool_search_import(void *, importargs_t *,
74*eda14cbcSMatt Macy     const pool_config_ops_t *);
75*eda14cbcSMatt Macy extern int zpool_find_config(void *, const char *, nvlist_t **, importargs_t *,
76*eda14cbcSMatt Macy     const pool_config_ops_t *);
77*eda14cbcSMatt Macy 
78*eda14cbcSMatt Macy extern const char * const * zpool_default_search_paths(size_t *count);
79*eda14cbcSMatt Macy extern int zpool_read_label(int, nvlist_t **, int *);
80*eda14cbcSMatt Macy extern int zpool_label_disk_wait(const char *, int);
81*eda14cbcSMatt Macy 
82*eda14cbcSMatt Macy struct udev_device;
83*eda14cbcSMatt Macy 
84*eda14cbcSMatt Macy extern int zfs_device_get_devid(struct udev_device *, char *, size_t);
85*eda14cbcSMatt Macy extern int zfs_device_get_physical(struct udev_device *, char *, size_t);
86*eda14cbcSMatt Macy 
87*eda14cbcSMatt Macy extern void update_vdev_config_dev_strs(nvlist_t *);
88*eda14cbcSMatt Macy 
89*eda14cbcSMatt Macy /*
90*eda14cbcSMatt Macy  * Default device paths
91*eda14cbcSMatt Macy  */
92*eda14cbcSMatt Macy #define	DISK_ROOT	"/dev"
93*eda14cbcSMatt Macy #define	UDISK_ROOT	"/dev/disk"
94*eda14cbcSMatt Macy #define	ZVOL_ROOT	"/dev/zvol"
95*eda14cbcSMatt Macy 
96*eda14cbcSMatt Macy extern int zfs_append_partition(char *path, size_t max_len);
97*eda14cbcSMatt Macy extern int zfs_resolve_shortname(const char *name, char *path, size_t pathlen);
98*eda14cbcSMatt Macy 
99*eda14cbcSMatt Macy extern char *zfs_strip_partition(char *);
100*eda14cbcSMatt Macy extern char *zfs_strip_path(char *);
101*eda14cbcSMatt Macy 
102*eda14cbcSMatt Macy extern int zfs_strcmp_pathname(const char *, const char *, int);
103*eda14cbcSMatt Macy 
104*eda14cbcSMatt Macy extern boolean_t zfs_dev_is_dm(const char *);
105*eda14cbcSMatt Macy extern boolean_t zfs_dev_is_whole_disk(const char *);
106*eda14cbcSMatt Macy extern int zfs_dev_flush(int);
107*eda14cbcSMatt Macy extern char *zfs_get_underlying_path(const char *);
108*eda14cbcSMatt Macy extern char *zfs_get_enclosure_sysfs_path(const char *);
109*eda14cbcSMatt Macy 
110*eda14cbcSMatt Macy extern boolean_t is_mpath_whole_disk(const char *);
111*eda14cbcSMatt Macy 
112*eda14cbcSMatt Macy extern boolean_t zfs_isnumber(const char *);
113*eda14cbcSMatt Macy 
114*eda14cbcSMatt Macy /*
115*eda14cbcSMatt Macy  * Formats for iostat numbers.  Examples: "12K", "30ms", "4B", "2321234", "-".
116*eda14cbcSMatt Macy  *
117*eda14cbcSMatt Macy  * ZFS_NICENUM_1024:	Print kilo, mega, tera, peta, exa..
118*eda14cbcSMatt Macy  * ZFS_NICENUM_BYTES:	Print single bytes ("13B"), kilo, mega, tera...
119*eda14cbcSMatt Macy  * ZFS_NICENUM_TIME:	Print nanosecs, microsecs, millisecs, seconds...
120*eda14cbcSMatt Macy  * ZFS_NICENUM_RAW:	Print the raw number without any formatting
121*eda14cbcSMatt Macy  * ZFS_NICENUM_RAWTIME:	Same as RAW, but print dashes ('-') for zero.
122*eda14cbcSMatt Macy  */
123*eda14cbcSMatt Macy enum zfs_nicenum_format {
124*eda14cbcSMatt Macy 	ZFS_NICENUM_1024 = 0,
125*eda14cbcSMatt Macy 	ZFS_NICENUM_BYTES = 1,
126*eda14cbcSMatt Macy 	ZFS_NICENUM_TIME = 2,
127*eda14cbcSMatt Macy 	ZFS_NICENUM_RAW = 3,
128*eda14cbcSMatt Macy 	ZFS_NICENUM_RAWTIME = 4
129*eda14cbcSMatt Macy };
130*eda14cbcSMatt Macy 
131*eda14cbcSMatt Macy /*
132*eda14cbcSMatt Macy  * Convert a number to a human-readable form.
133*eda14cbcSMatt Macy  */
134*eda14cbcSMatt Macy extern void zfs_nicebytes(uint64_t, char *, size_t);
135*eda14cbcSMatt Macy extern void zfs_nicenum(uint64_t, char *, size_t);
136*eda14cbcSMatt Macy extern void zfs_nicenum_format(uint64_t, char *, size_t,
137*eda14cbcSMatt Macy     enum zfs_nicenum_format);
138*eda14cbcSMatt Macy extern void zfs_nicetime(uint64_t, char *, size_t);
139*eda14cbcSMatt Macy extern void zfs_niceraw(uint64_t, char *, size_t);
140*eda14cbcSMatt Macy 
141*eda14cbcSMatt Macy #define	nicenum(num, buf, size)	zfs_nicenum(num, buf, size)
142*eda14cbcSMatt Macy 
143*eda14cbcSMatt Macy extern void zpool_dump_ddt(const ddt_stat_t *, const ddt_histogram_t *);
144*eda14cbcSMatt Macy extern int zpool_history_unpack(char *, uint64_t, uint64_t *, nvlist_t ***,
145*eda14cbcSMatt Macy     uint_t *);
146*eda14cbcSMatt Macy 
147*eda14cbcSMatt Macy struct zfs_cmd;
148*eda14cbcSMatt Macy int zfs_ioctl_fd(int fd, unsigned long request, struct zfs_cmd *zc);
149*eda14cbcSMatt Macy 
150*eda14cbcSMatt Macy /*
151*eda14cbcSMatt Macy  * List of colors to use
152*eda14cbcSMatt Macy  */
153*eda14cbcSMatt Macy #define	ANSI_RED	"\033[0;31m"
154*eda14cbcSMatt Macy #define	ANSI_YELLOW	"\033[0;33m"
155*eda14cbcSMatt Macy #define	ANSI_RESET	"\033[0m"
156*eda14cbcSMatt Macy #define	ANSI_BOLD	"\033[1m"
157*eda14cbcSMatt Macy 
158*eda14cbcSMatt Macy void color_start(char *color);
159*eda14cbcSMatt Macy void color_end(void);
160*eda14cbcSMatt Macy int printf_color(char *color, char *format, ...);
161*eda14cbcSMatt Macy 
162*eda14cbcSMatt Macy #ifdef	__cplusplus
163*eda14cbcSMatt Macy }
164*eda14cbcSMatt Macy #endif
165*eda14cbcSMatt Macy 
166*eda14cbcSMatt Macy #endif	/* _LIBZUTIL_H */
167