xref: /freebsd/sys/contrib/openzfs/lib/libzpool/util.c (revision be181ee2a28aa2b4b0e76684bce9f673ef668874)
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) 2016 by Delphix. All rights reserved.
24  * Copyright 2017 Jason King
25  * Copyright (c) 2017, Intel Corporation.
26  */
27 
28 #include <assert.h>
29 #include <sys/zfs_context.h>
30 #include <sys/avl.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/spa.h>
35 #include <sys/fs/zfs.h>
36 #include <sys/zfs_refcount.h>
37 #include <sys/zfs_ioctl.h>
38 #include <dlfcn.h>
39 #include <libzutil.h>
40 
41 /*
42  * Routines needed by more than one client of libzpool.
43  */
44 
45 static void
46 show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent)
47 {
48 	vdev_stat_t *vs;
49 	vdev_stat_t *v0 = { 0 };
50 	uint64_t sec;
51 	uint64_t is_log = 0;
52 	nvlist_t **child;
53 	uint_t c, children;
54 	char used[6], avail[6];
55 	char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];
56 
57 	v0 = umem_zalloc(sizeof (*v0), UMEM_NOFAIL);
58 
59 	if (indent == 0 && desc != NULL) {
60 		(void) printf("                           "
61 		    " capacity   operations   bandwidth  ---- errors ----\n");
62 		(void) printf("description                "
63 		    "used avail  read write  read write  read write cksum\n");
64 	}
65 
66 	if (desc != NULL) {
67 		const char *suffix = "";
68 		char *bias = NULL;
69 		char bias_suffix[32];
70 
71 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
72 		(void) nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
73 		    &bias);
74 		if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
75 		    (uint64_t **)&vs, &c) != 0)
76 			vs = v0;
77 
78 		if (bias != NULL) {
79 			(void) snprintf(bias_suffix, sizeof (bias_suffix),
80 			    " (%s)", bias);
81 			suffix = bias_suffix;
82 		} else if (is_log) {
83 			suffix = " (log)";
84 		}
85 
86 		sec = MAX(1, vs->vs_timestamp / NANOSEC);
87 
88 		nicenum(vs->vs_alloc, used, sizeof (used));
89 		nicenum(vs->vs_space - vs->vs_alloc, avail, sizeof (avail));
90 		nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops, sizeof (rops));
91 		nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops, sizeof (wops));
92 		nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes,
93 		    sizeof (rbytes));
94 		nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes,
95 		    sizeof (wbytes));
96 		nicenum(vs->vs_read_errors, rerr, sizeof (rerr));
97 		nicenum(vs->vs_write_errors, werr, sizeof (werr));
98 		nicenum(vs->vs_checksum_errors, cerr, sizeof (cerr));
99 
100 		(void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
101 		    indent, "",
102 		    desc,
103 		    (int)(indent+strlen(desc)-25-(vs->vs_space ? 0 : 12)),
104 		    suffix,
105 		    vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
106 		    vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
107 		    rops, wops, rbytes, wbytes, rerr, werr, cerr);
108 	}
109 	umem_free(v0, sizeof (*v0));
110 
111 	if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0)
112 		return;
113 
114 	for (c = 0; c < children; c++) {
115 		nvlist_t *cnv = child[c];
116 		char *cname = NULL, *tname;
117 		uint64_t np;
118 		int len;
119 		if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
120 		    nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
121 			cname = (char *)"<unknown>";
122 		len = strlen(cname) + 2;
123 		tname = umem_zalloc(len, UMEM_NOFAIL);
124 		(void) strlcpy(tname, cname, len);
125 		if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0)
126 			tname[strlen(tname)] = '0' + np;
127 		show_vdev_stats(tname, ctype, cnv, indent + 2);
128 		umem_free(tname, len);
129 	}
130 }
131 
132 void
133 show_pool_stats(spa_t *spa)
134 {
135 	nvlist_t *config, *nvroot;
136 	char *name;
137 
138 	VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0);
139 
140 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
141 	    &nvroot) == 0);
142 	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
143 	    &name) == 0);
144 
145 	show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0);
146 	show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0);
147 	show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0);
148 
149 	nvlist_free(config);
150 }
151 
152 /* *k_out must be freed by the caller */
153 static int
154 set_global_var_parse_kv(const char *arg, char **k_out, u_longlong_t *v_out)
155 {
156 	int err;
157 	VERIFY(arg);
158 	char *d = strdup(arg);
159 
160 	char *save = NULL;
161 	char *k = strtok_r(d, "=", &save);
162 	char *v_str = strtok_r(NULL, "=", &save);
163 	char *follow = strtok_r(NULL, "=", &save);
164 	if (k == NULL || v_str == NULL || follow != NULL) {
165 		err = EINVAL;
166 		goto err_free;
167 	}
168 
169 	u_longlong_t val = strtoull(v_str, NULL, 0);
170 	if (val > UINT32_MAX) {
171 		fprintf(stderr, "Value for global variable '%s' must "
172 		    "be a 32-bit unsigned integer, got '%s'\n", k, v_str);
173 		err = EOVERFLOW;
174 		goto err_free;
175 	}
176 
177 	*k_out = strdup(k);
178 	*v_out = val;
179 	free(d);
180 	return (0);
181 
182 err_free:
183 	free(d);
184 
185 	return (err);
186 }
187 
188 /*
189  * Sets given global variable in libzpool to given unsigned 32-bit value.
190  * arg: "<variable>=<value>"
191  */
192 int
193 set_global_var(char const *arg)
194 {
195 	void *zpoolhdl;
196 	char *varname;
197 	u_longlong_t val;
198 	int ret;
199 
200 #ifndef _ZFS_LITTLE_ENDIAN
201 	/*
202 	 * On big endian systems changing a 64-bit variable would set the high
203 	 * 32 bits instead of the low 32 bits, which could cause unexpected
204 	 * results.
205 	 */
206 	fprintf(stderr, "Setting global variables is only supported on "
207 	    "little-endian systems\n");
208 	ret = ENOTSUP;
209 	goto out_ret;
210 #endif
211 
212 	if ((ret = set_global_var_parse_kv(arg, &varname, &val)) != 0) {
213 		goto out_ret;
214 	}
215 
216 	zpoolhdl = dlopen("libzpool.so", RTLD_LAZY);
217 	if (zpoolhdl != NULL) {
218 		uint32_t *var;
219 		var = dlsym(zpoolhdl, varname);
220 		if (var == NULL) {
221 			fprintf(stderr, "Global variable '%s' does not exist "
222 			    "in libzpool.so\n", varname);
223 			ret = EINVAL;
224 			goto out_dlclose;
225 		}
226 		*var = (uint32_t)val;
227 
228 	} else {
229 		fprintf(stderr, "Failed to open libzpool.so to set global "
230 		    "variable\n");
231 		ret = EIO;
232 		goto out_dlclose;
233 	}
234 
235 	ret = 0;
236 
237 out_dlclose:
238 	dlclose(zpoolhdl);
239 	free(varname);
240 out_ret:
241 	return (ret);
242 }
243 
244 static nvlist_t *
245 refresh_config(void *unused, nvlist_t *tryconfig)
246 {
247 	(void) unused;
248 	return (spa_tryimport(tryconfig));
249 }
250 
251 #if defined(__FreeBSD__)
252 
253 #include <sys/param.h>
254 #include <sys/sysctl.h>
255 #include <os/freebsd/zfs/sys/zfs_ioctl_compat.h>
256 
257 static int
258 pool_active(void *unused, const char *name, uint64_t guid, boolean_t *isactive)
259 {
260 	(void) unused, (void) guid;
261 	zfs_iocparm_t zp;
262 	zfs_cmd_t *zc = NULL;
263 	zfs_cmd_legacy_t *zcl = NULL;
264 	unsigned long request;
265 	int ret;
266 
267 	int fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);
268 	if (fd < 0)
269 		return (-1);
270 
271 	/*
272 	 * Use ZFS_IOC_POOL_STATS to check if the pool is active.  We want to
273 	 * avoid adding a dependency on libzfs_core solely for this ioctl(),
274 	 * therefore we manually craft the stats command.  Note that the command
275 	 * ID is identical between the openzfs and legacy ioctl() formats.
276 	 */
277 	int ver = ZFS_IOCVER_NONE;
278 	size_t ver_size = sizeof (ver);
279 
280 	sysctlbyname("vfs.zfs.version.ioctl", &ver, &ver_size, NULL, 0);
281 
282 	switch (ver) {
283 	case ZFS_IOCVER_OZFS:
284 		zc = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL);
285 
286 		(void) strlcpy(zc->zc_name, name, sizeof (zc->zc_name));
287 		zp.zfs_cmd = (uint64_t)(uintptr_t)zc;
288 		zp.zfs_cmd_size = sizeof (zfs_cmd_t);
289 		zp.zfs_ioctl_version = ZFS_IOCVER_OZFS;
290 
291 		request = _IOWR('Z', ZFS_IOC_POOL_STATS, zfs_iocparm_t);
292 		ret = ioctl(fd, request, &zp);
293 
294 		free((void *)(uintptr_t)zc->zc_nvlist_dst);
295 		umem_free(zc, sizeof (zfs_cmd_t));
296 
297 		break;
298 	case ZFS_IOCVER_LEGACY:
299 		zcl = umem_zalloc(sizeof (zfs_cmd_legacy_t), UMEM_NOFAIL);
300 
301 		(void) strlcpy(zcl->zc_name, name, sizeof (zcl->zc_name));
302 		zp.zfs_cmd = (uint64_t)(uintptr_t)zcl;
303 		zp.zfs_cmd_size = sizeof (zfs_cmd_legacy_t);
304 		zp.zfs_ioctl_version = ZFS_IOCVER_LEGACY;
305 
306 		request = _IOWR('Z', ZFS_IOC_POOL_STATS, zfs_iocparm_t);
307 		ret = ioctl(fd, request, &zp);
308 
309 		free((void *)(uintptr_t)zcl->zc_nvlist_dst);
310 		umem_free(zcl, sizeof (zfs_cmd_legacy_t));
311 
312 		break;
313 	default:
314 		fprintf(stderr, "unrecognized zfs ioctl version %d", ver);
315 		exit(1);
316 	}
317 
318 	(void) close(fd);
319 
320 	*isactive = (ret == 0);
321 
322 	return (0);
323 }
324 #else
325 static int
326 pool_active(void *unused, const char *name, uint64_t guid,
327     boolean_t *isactive)
328 {
329 	(void) unused, (void) guid;
330 	int fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);
331 	if (fd < 0)
332 		return (-1);
333 
334 	/*
335 	 * Use ZFS_IOC_POOL_STATS to check if a pool is active.
336 	 */
337 	zfs_cmd_t *zcp = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL);
338 	(void) strlcpy(zcp->zc_name, name, sizeof (zcp->zc_name));
339 
340 	int ret = ioctl(fd, ZFS_IOC_POOL_STATS, zcp);
341 
342 	free((void *)(uintptr_t)zcp->zc_nvlist_dst);
343 	umem_free(zcp, sizeof (zfs_cmd_t));
344 
345 	(void) close(fd);
346 
347 	*isactive = (ret == 0);
348 
349 	return (0);
350 }
351 #endif
352 
353 pool_config_ops_t libzpool_config_ops = {
354 	.pco_refresh_config = refresh_config,
355 	.pco_pool_active = pool_active,
356 };
357