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 (c) 2017, Intel Corporation.
25 */
26
27#include <assert.h>
28#include <sys/zfs_context.h>
29#include <sys/avl.h>
30#include <string.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <sys/spa.h>
34#include <sys/fs/zfs.h>
35#include <sys/refcount.h>
36#include <dlfcn.h>
37
38/*
39 * Routines needed by more than one client of libzpool.
40 */
41
42static void
43show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent)
44{
45	vdev_stat_t *vs;
46	vdev_stat_t v0 = { 0 };
47	uint64_t sec;
48	uint64_t is_log = 0;
49	nvlist_t **child;
50	uint_t c, children;
51	char used[6], avail[6];
52	char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];
53
54	if (indent == 0 && desc != NULL) {
55		(void) printf("                           "
56		    " capacity   operations   bandwidth  ---- errors ----\n");
57		(void) printf("description                "
58		    "used avail  read write  read write  read write cksum\n");
59	}
60
61	if (desc != NULL) {
62		char *suffix = "", *bias = NULL;
63		char bias_suffix[32];
64
65		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
66		(void) nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
67		    &bias);
68		if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
69		    (uint64_t **)&vs, &c) != 0)
70			vs = &v0;
71
72		if (bias != NULL) {
73			(void) snprintf(bias_suffix, sizeof (bias_suffix),
74			    " (%s)", bias);
75			suffix = bias_suffix;
76		} else if (is_log) {
77			suffix = " (log)";
78		}
79
80		sec = MAX(1, vs->vs_timestamp / NANOSEC);
81
82		nicenum(vs->vs_alloc, used, sizeof (used));
83		nicenum(vs->vs_space - vs->vs_alloc, avail, sizeof (avail));
84		nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops, sizeof (rops));
85		nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops, sizeof (wops));
86		nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes,
87		    sizeof (rbytes));
88		nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes,
89		    sizeof (wbytes));
90		nicenum(vs->vs_read_errors, rerr, sizeof (rerr));
91		nicenum(vs->vs_write_errors, werr, sizeof (werr));
92		nicenum(vs->vs_checksum_errors, cerr, sizeof (cerr));
93
94		(void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
95		    indent, "",
96		    desc,
97		    (int)(indent+strlen(desc)-25-(vs->vs_space ? 0 : 12)),
98		    suffix,
99		    vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
100		    vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
101		    rops, wops, rbytes, wbytes, rerr, werr, cerr);
102	}
103
104	if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0)
105		return;
106
107	for (c = 0; c < children; c++) {
108		nvlist_t *cnv = child[c];
109		char *cname, *tname;
110		uint64_t np;
111		if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
112		    nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
113			cname = "<unknown>";
114		tname = calloc(1, strlen(cname) + 2);
115		(void) strcpy(tname, cname);
116		if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0)
117			tname[strlen(tname)] = '0' + np;
118		show_vdev_stats(tname, ctype, cnv, indent + 2);
119		free(tname);
120	}
121}
122
123void
124show_pool_stats(spa_t *spa)
125{
126	nvlist_t *config, *nvroot;
127	char *name;
128
129	VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0);
130
131	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
132	    &nvroot) == 0);
133	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
134	    &name) == 0);
135
136	show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0);
137	show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0);
138	show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0);
139
140	nvlist_free(config);
141}
142
143/*
144 * Sets given global variable in libzpool to given unsigned 32-bit value.
145 * arg: "<variable>=<value>"
146 */
147int
148set_global_var(char *arg)
149{
150	void *zpoolhdl;
151	char *varname = arg, *varval;
152	u_longlong_t val;
153
154#ifndef _LITTLE_ENDIAN
155	/*
156	 * On big endian systems changing a 64-bit variable would set the high
157	 * 32 bits instead of the low 32 bits, which could cause unexpected
158	 * results.
159	 */
160	fprintf(stderr, "Setting global variables is only supported on "
161	    "little-endian systems\n", varname);
162	return (ENOTSUP);
163#endif
164	if ((varval = strchr(arg, '=')) != NULL) {
165		*varval = '\0';
166		varval++;
167		val = strtoull(varval, NULL, 0);
168		if (val > UINT32_MAX) {
169			fprintf(stderr, "Value for global variable '%s' must "
170			    "be a 32-bit unsigned integer\n", varname);
171			return (EOVERFLOW);
172		}
173	} else {
174		return (EINVAL);
175	}
176
177	zpoolhdl = dlopen("libzpool.so", RTLD_LAZY);
178	if (zpoolhdl != NULL) {
179		uint32_t *var;
180		var = dlsym(zpoolhdl, varname);
181		if (var == NULL) {
182			fprintf(stderr, "Global variable '%s' does not exist "
183			    "in libzpool.so\n", varname);
184			return (EINVAL);
185		}
186		*var = (uint32_t)val;
187
188		dlclose(zpoolhdl);
189	} else {
190		fprintf(stderr, "Failed to open libzpool.so to set global "
191		    "variable\n");
192		return (EIO);
193	}
194
195	return (0);
196}
197