xref: /illumos-gate/usr/src/lib/libzpool/common/util.c (revision dd50e0cc)
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 http://www.opensolaris.org/os/licensing.
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 RackTop Systems.
25  * Copyright (c) 2017, Intel Corporation.
26  * Copyright 2020 Joyent, Inc.
27  */
28 
29 #include <assert.h>
30 #include <sys/zfs_context.h>
31 #include <sys/avl.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <sys/spa.h>
36 #include <sys/fs/zfs.h>
37 #include <sys/refcount.h>
38 #include <sys/zfs_ioctl.h>
39 #include <dlfcn.h>
40 #include <libzutil.h>
41 
42 extern void nicenum(uint64_t num, char *buf, size_t);
43 
44 /*
45  * Routines needed by more than one client of libzpool.
46  */
47 
48 static void
show_vdev_stats(const char * desc,const char * ctype,nvlist_t * nv,int indent)49 show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent)
50 {
51 	vdev_stat_t *vs;
52 	vdev_stat_t *v0 = { 0 };
53 	uint64_t sec;
54 	uint64_t is_log = 0;
55 	nvlist_t **child;
56 	uint_t c, children;
57 	char used[6], avail[6];
58 	char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];
59 
60 	v0 = umem_zalloc(sizeof (*v0), UMEM_NOFAIL);
61 
62 	if (indent == 0 && desc != NULL) {
63 		(void) printf("                           "
64 		    " capacity   operations   bandwidth  ---- errors ----\n");
65 		(void) printf("description                "
66 		    "used avail  read write  read write  read write cksum\n");
67 	}
68 
69 	if (desc != NULL) {
70 		char *suffix = "", *bias = NULL;
71 		char bias_suffix[32];
72 
73 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
74 		(void) nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
75 		    &bias);
76 		if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
77 		    (uint64_t **)&vs, &c) != 0)
78 			vs = v0;
79 
80 		if (bias != NULL) {
81 			(void) snprintf(bias_suffix, sizeof (bias_suffix),
82 			    " (%s)", bias);
83 			suffix = bias_suffix;
84 		} else if (is_log) {
85 			suffix = " (log)";
86 		}
87 
88 		sec = MAX(1, vs->vs_timestamp / NANOSEC);
89 
90 		nicenum(vs->vs_alloc, used, sizeof (used));
91 		nicenum(vs->vs_space - vs->vs_alloc, avail, sizeof (avail));
92 		nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops, sizeof (rops));
93 		nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops, sizeof (wops));
94 		nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes,
95 		    sizeof (rbytes));
96 		nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes,
97 		    sizeof (wbytes));
98 		nicenum(vs->vs_read_errors, rerr, sizeof (rerr));
99 		nicenum(vs->vs_write_errors, werr, sizeof (werr));
100 		nicenum(vs->vs_checksum_errors, cerr, sizeof (cerr));
101 
102 		(void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
103 		    indent, "",
104 		    desc,
105 		    (int)(indent+strlen(desc)-25-(vs->vs_space ? 0 : 12)),
106 		    suffix,
107 		    vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
108 		    vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
109 		    rops, wops, rbytes, wbytes, rerr, werr, cerr);
110 	}
111 	umem_free(v0, sizeof (*v0));
112 
113 	if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0)
114 		return;
115 
116 	for (c = 0; c < children; c++) {
117 		nvlist_t *cnv = child[c];
118 		char *cname, *tname;
119 		uint64_t np;
120 		if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
121 		    nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
122 			cname = "<unknown>";
123 		tname = calloc(1, strlen(cname) + 2);
124 		(void) strcpy(tname, cname);
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 		free(tname);
129 	}
130 }
131 
132 void
show_pool_stats(spa_t * spa)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 /*
153  * Sets given global variable in libzpool to given unsigned 32-bit value.
154  * arg: "<variable>=<value>"
155  */
156 int
set_global_var(char * arg)157 set_global_var(char *arg)
158 {
159 	void *zpoolhdl;
160 	char *varname = arg, *varval;
161 	u_longlong_t val;
162 
163 #ifndef _LITTLE_ENDIAN
164 	/*
165 	 * On big endian systems changing a 64-bit variable would set the high
166 	 * 32 bits instead of the low 32 bits, which could cause unexpected
167 	 * results.
168 	 */
169 	fprintf(stderr, "Setting global variables is only supported on "
170 	    "little-endian systems\n", varname);
171 	return (ENOTSUP);
172 #endif
173 	if ((varval = strchr(arg, '=')) != NULL) {
174 		*varval = '\0';
175 		varval++;
176 		val = strtoull(varval, NULL, 0);
177 		if (val > UINT32_MAX) {
178 			fprintf(stderr, "Value for global variable '%s' must "
179 			    "be a 32-bit unsigned integer\n", varname);
180 			return (EOVERFLOW);
181 		}
182 	} else {
183 		return (EINVAL);
184 	}
185 
186 	zpoolhdl = dlopen("libzpool.so", RTLD_LAZY);
187 	if (zpoolhdl != NULL) {
188 		uint32_t *var;
189 		var = dlsym(zpoolhdl, varname);
190 		if (var == NULL) {
191 			fprintf(stderr, "Global variable '%s' does not exist "
192 			    "in libzpool.so\n", varname);
193 			return (EINVAL);
194 		}
195 		*var = (uint32_t)val;
196 
197 		dlclose(zpoolhdl);
198 	} else {
199 		fprintf(stderr, "Failed to open libzpool.so to set global "
200 		    "variable\n");
201 		return (EIO);
202 	}
203 
204 	return (0);
205 }
206 
207 static nvlist_t *
refresh_config(void * unused,nvlist_t * tryconfig)208 refresh_config(void *unused, nvlist_t *tryconfig)
209 {
210 	return (spa_tryimport(tryconfig));
211 }
212 
213 static int
pool_active(void * unused,const char * name,uint64_t guid,boolean_t * isactive)214 pool_active(void *unused, const char *name, uint64_t guid,
215     boolean_t *isactive)
216 {
217 	zfs_cmd_t *zcp;
218 	nvlist_t *innvl;
219 	char *packed = NULL;
220 	size_t size = 0;
221 	int fd, ret;
222 
223 	/*
224 	 * Use ZFS_IOC_POOL_SYNC to confirm if a pool is active
225 	 */
226 
227 	fd = open("/dev/zfs", O_RDWR);
228 	if (fd < 0)
229 		return (-1);
230 
231 	zcp = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL);
232 
233 	innvl = fnvlist_alloc();
234 	fnvlist_add_boolean_value(innvl, "force", B_FALSE);
235 
236 	(void) strlcpy(zcp->zc_name, name, sizeof (zcp->zc_name));
237 	packed = fnvlist_pack(innvl, &size);
238 	zcp->zc_nvlist_src = (uint64_t)(uintptr_t)packed;
239 	zcp->zc_nvlist_src_size = size;
240 
241 	ret = ioctl(fd, ZFS_IOC_POOL_SYNC, zcp);
242 
243 	fnvlist_pack_free(packed, size);
244 	free((void *)(uintptr_t)zcp->zc_nvlist_dst);
245 	nvlist_free(innvl);
246 	umem_free(zcp, sizeof (zfs_cmd_t));
247 
248 	(void) close(fd);
249 
250 	*isactive = (ret == 0);
251 
252 	return (0);
253 }
254 
255 const pool_config_ops_t libzpool_config_ops = {
256 	.pco_refresh_config = refresh_config,
257 	.pco_pool_active = pool_active,
258 };
259