2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
223f9d6adLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
230e60744Pavel Zakharov * Copyright (c) 2016 by Delphix. All rights reserved.
24f06dce2Andrew Stormont * Copyright 2017 RackTop Systems.
25663207aDon Brady * Copyright (c) 2017, Intel Corporation.
26dd50e0cTony Hutter * Copyright 2020 Joyent, Inc.
27fa9e406ahrens */
29fa9e406ahrens#include <assert.h>
30fa9e406ahrens#include <sys/zfs_context.h>
31fa9e406ahrens#include <sys/avl.h>
32fa9e406ahrens#include <string.h>
33fa9e406ahrens#include <stdio.h>
34fa9e406ahrens#include <stdlib.h>
35fa9e406ahrens#include <sys/spa.h>
36fa9e406ahrens#include <sys/fs/zfs.h>
37ea8dc4beschrock#include <sys/refcount.h>
38d8ab6e1Don Brady#include <sys/zfs_ioctl.h>
390e60744Pavel Zakharov#include <dlfcn.h>
40d8ab6e1Don Brady#include <libzutil.h>
42f06dce2Andrew Stormontextern void nicenum(uint64_t num, char *buf, size_t);
43f06dce2Andrew Stormont
45fa9e406ahrens * Routines needed by more than one client of libzpool.
46fa9e406ahrens */
48fa9e406ahrensstatic void
49e14bb32Jeff Bonwickshow_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent)
51fa9e406ahrens	vdev_stat_t *vs;
52dd50e0cTony Hutter	vdev_stat_t *v0 = { 0 };
53fa9e406ahrens	uint64_t sec;
548654d02perrin	uint64_t is_log = 0;
55e14bb32Jeff Bonwick	nvlist_t **child;
56e14bb32Jeff Bonwick	uint_t c, children;
57fa9e406ahrens	char used[6], avail[6];
58fa9e406ahrens	char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];
60dd50e0cTony Hutter	v0 = umem_zalloc(sizeof (*v0), UMEM_NOFAIL);
61dd50e0cTony Hutter
62e14bb32Jeff Bonwick	if (indent == 0 && desc != NULL) {
63e14bb32Jeff Bonwick		(void) printf("                           "
64fa9e406ahrens		    " capacity   operations   bandwidth  ---- errors ----\n");
65e14bb32Jeff Bonwick		(void) printf("description                "
66fa9e406ahrens		    "used avail  read write  read write  read write cksum\n");
67fa9e406ahrens	}
69e14bb32Jeff Bonwick	if (desc != NULL) {
70663207aDon Brady		char *suffix = "", *bias = NULL;
71663207aDon Brady		char bias_suffix[32];
72e14bb32Jeff Bonwick
73663207aDon Brady		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
74663207aDon Brady		(void) nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
75663207aDon Brady		    &bias);
763f9d6adLin Ling		if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
77e14bb32Jeff Bonwick		    (uint64_t **)&vs, &c) != 0)
78dd50e0cTony Hutter			vs = v0;
79e14bb32Jeff Bonwick
80663207aDon Brady		if (bias != NULL) {
81663207aDon Brady			(void) snprintf(bias_suffix, sizeof (bias_suffix),
82663207aDon Brady			    " (%s)", bias);
83663207aDon Brady			suffix = bias_suffix;
84663207aDon Brady		} else if (is_log) {
85663207aDon Brady			suffix = " (log)";
86663207aDon Brady		}
87663207aDon Brady
88e14bb32Jeff Bonwick		sec = MAX(1, vs->vs_timestamp / NANOSEC);
89e14bb32Jeff Bonwick
900a05512Jason King		nicenum(vs->vs_alloc, used, sizeof (used));
910a05512Jason King		nicenum(vs->vs_space - vs->vs_alloc, avail, sizeof (avail));
920a05512Jason King		nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops, sizeof (rops));
930a05512Jason King		nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops, sizeof (wops));
940a05512Jason King		nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes,
950a05512Jason King		    sizeof (rbytes));
960a05512Jason King		nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes,
970a05512Jason King		    sizeof (wbytes));
980a05512Jason King		nicenum(vs->vs_read_errors, rerr, sizeof (rerr));
990a05512Jason King		nicenum(vs->vs_write_errors, werr, sizeof (werr));
1000a05512Jason King		nicenum(vs->vs_checksum_errors, cerr, sizeof (cerr));
101e14bb32Jeff Bonwick
102e14bb32Jeff Bonwick		(void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
103e14bb32Jeff Bonwick		    indent, "",
104e14bb32Jeff Bonwick		    desc,
105663207aDon Brady		    (int)(indent+strlen(desc)-25-(vs->vs_space ? 0 : 12)),
106663207aDon Brady		    suffix,
107e14bb32Jeff Bonwick		    vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
108e14bb32Jeff Bonwick		    vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
109e14bb32Jeff Bonwick		    rops, wops, rbytes, wbytes, rerr, werr, cerr);
110e14bb32Jeff Bonwick	}
111dd50e0cTony Hutter	umem_free(v0, sizeof (*v0));
112e14bb32Jeff Bonwick
113e14bb32Jeff Bonwick	if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0)
114fa9e406ahrens		return;
116fa9e406ahrens	for (c = 0; c < children; c++) {
117fa9e406ahrens		nvlist_t *cnv = child[c];
11899653d4eschrock		char *cname, *tname;
11999653d4eschrock		uint64_t np;
120fa9e406ahrens		if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
121fa9e406ahrens		    nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
122fa9e406ahrens			cname = "<unknown>";
12399653d4eschrock		tname = calloc(1, strlen(cname) + 2);
12499653d4eschrock		(void) strcpy(tname, cname);
12599653d4eschrock		if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0)
12699653d4eschrock			tname[strlen(tname)] = '0' + np;
127e14bb32Jeff Bonwick		show_vdev_stats(tname, ctype, cnv, indent + 2);
12899653d4eschrock		free(tname);
129fa9e406ahrens	}
133fa9e406ahrensshow_pool_stats(spa_t *spa)
1350373e76bonwick	nvlist_t *config, *nvroot;
1360373e76bonwick	char *name;
138e14bb32Jeff Bonwick	VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0);
140fa9e406ahrens	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
141fa9e406ahrens	    &nvroot) == 0);
1420373e76bonwick	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1430373e76bonwick	    &name) == 0);
145e14bb32Jeff Bonwick	show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0);
146e14bb32Jeff Bonwick	show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0);
147e14bb32Jeff Bonwick	show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0);
148e14bb32Jeff Bonwick
149e14bb32Jeff Bonwick	nvlist_free(config);
1510e60744Pavel Zakharov
1520e60744Pavel Zakharov/*
1530e60744Pavel Zakharov * Sets given global variable in libzpool to given unsigned 32-bit value.
1540e60744Pavel Zakharov * arg: "<variable>=<value>"
1550e60744Pavel Zakharov */
1560e60744Pavel Zakharovint
1570e60744Pavel Zakharovset_global_var(char *arg)
1580e60744Pavel Zakharov{
1590e60744Pavel Zakharov	void *zpoolhdl;
1600e60744Pavel Zakharov	char *varname = arg, *varval;
1610e60744Pavel Zakharov	u_longlong_t val;
1620e60744Pavel Zakharov
1630e60744Pavel Zakharov#ifndef _LITTLE_ENDIAN
1640e60744Pavel Zakharov	/*
1650e60744Pavel Zakharov	 * On big endian systems changing a 64-bit variable would set the high
1660e60744Pavel Zakharov	 * 32 bits instead of the low 32 bits, which could cause unexpected
1670e60744Pavel Zakharov	 * results.
1680e60744Pavel Zakharov	 */
1690e60744Pavel Zakharov	fprintf(stderr, "Setting global variables is only supported on "
1700e60744Pavel Zakharov	    "little-endian systems\n", varname);
1710e60744Pavel Zakharov	return (ENOTSUP);
1720e60744Pavel Zakharov#endif
1730e60744Pavel Zakharov	if ((varval = strchr(arg, '=')) != NULL) {
1740e60744Pavel Zakharov		*varval = '\0';
1750e60744Pavel Zakharov		varval++;
1760e60744Pavel Zakharov		val = strtoull(varval, NULL, 0);
1770e60744Pavel Zakharov		if (val > UINT32_MAX) {
1780e60744Pavel Zakharov			fprintf(stderr, "Value for global variable '%s' must "
1790e60744Pavel Zakharov			    "be a 32-bit unsigned integer\n", varname);
1800e60744Pavel Zakharov			return (EOVERFLOW);
1810e60744Pavel Zakharov		}
1820e60744Pavel Zakharov	} else {
1830e60744Pavel Zakharov		return (EINVAL);
1840e60744Pavel Zakharov	}
1850e60744Pavel Zakharov
1860e60744Pavel Zakharov	zpoolhdl = dlopen("libzpool.so", RTLD_LAZY);
1870e60744Pavel Zakharov	if (zpoolhdl != NULL) {
1880e60744Pavel Zakharov		uint32_t *var;
1890e60744Pavel Zakharov		var = dlsym(zpoolhdl, varname);
1900e60744Pavel Zakharov		if (var == NULL) {
1910e60744Pavel Zakharov			fprintf(stderr, "Global variable '%s' does not exist "
1920e60744Pavel Zakharov			    "in libzpool.so\n", varname);
1930e60744Pavel Zakharov			return (EINVAL);
1940e60744Pavel Zakharov		}
1950e60744Pavel Zakharov		*var = (uint32_t)val;
1960e60744Pavel Zakharov
1970e60744Pavel Zakharov		dlclose(zpoolhdl);
1980e60744Pavel Zakharov	} else {
1990e60744Pavel Zakharov		fprintf(stderr, "Failed to open libzpool.so to set global "
2000e60744Pavel Zakharov		    "variable\n");
2010e60744Pavel Zakharov		return (EIO);
2020e60744Pavel Zakharov	}
2030e60744Pavel Zakharov
2040e60744Pavel Zakharov	return (0);
2050e60744Pavel Zakharov}
206d8ab6e1Don Brady
207d8ab6e1Don Bradystatic nvlist_t *
208d8ab6e1Don Bradyrefresh_config(void *unused, nvlist_t *tryconfig)
209d8ab6e1Don Brady{
210d8ab6e1Don Brady	return (spa_tryimport(tryconfig));
211d8ab6e1Don Brady}
212d8ab6e1Don Brady
213d8ab6e1Don Bradystatic int
214d8ab6e1Don Bradypool_active(void *unused, const char *name, uint64_t guid,
215d8ab6e1Don Brady    boolean_t *isactive)
216d8ab6e1Don Brady{
217d8ab6e1Don Brady	zfs_cmd_t *zcp;
218d8ab6e1Don Brady	nvlist_t *innvl;
219d8ab6e1Don Brady	char *packed = NULL;
220d8ab6e1Don Brady	size_t size = 0;
221d8ab6e1Don Brady	int fd, ret;
222d8ab6e1Don Brady
223d8ab6e1Don Brady	/*
224d8ab6e1Don Brady	 * Use ZFS_IOC_POOL_SYNC to confirm if a pool is active
225d8ab6e1Don Brady	 */
226d8ab6e1Don Brady
227d8ab6e1Don Brady	fd = open("/dev/zfs", O_RDWR);
228d8ab6e1Don Brady	if (fd < 0)
229d8ab6e1Don Brady		return (-1);
230d8ab6e1Don Brady
231d8ab6e1Don Brady	zcp = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL);
232d8ab6e1Don Brady
233d8ab6e1Don Brady	innvl = fnvlist_alloc();
234d8ab6e1Don Brady	fnvlist_add_boolean_value(innvl, "force", B_FALSE);
235d8ab6e1Don Brady
236d8ab6e1Don Brady	(void) strlcpy(zcp->zc_name, name, sizeof (zcp->zc_name));
237d8ab6e1Don Brady	packed = fnvlist_pack(innvl, &size);
238d8ab6e1Don Brady	zcp->zc_nvlist_src = (uint64_t)(uintptr_t)packed;
239d8ab6e1Don Brady	zcp->zc_nvlist_src_size = size;
240d8ab6e1Don Brady
241d8ab6e1Don Brady	ret = ioctl(fd, ZFS_IOC_POOL_SYNC, zcp);
242d8ab6e1Don Brady
243d8ab6e1Don Brady	fnvlist_pack_free(packed, size);
244d8ab6e1Don Brady	free((void *)(uintptr_t)zcp->zc_nvlist_dst);
245d8ab6e1Don Brady	nvlist_free(innvl);
246d8ab6e1Don Brady	umem_free(zcp, sizeof (zfs_cmd_t));
247d8ab6e1Don Brady
248d8ab6e1Don Brady	(void) close(fd);
249d8ab6e1Don Brady
250d8ab6e1Don Brady	*isactive = (ret == 0);
251d8ab6e1Don Brady
252d8ab6e1Don Brady	return (0);
253d8ab6e1Don Brady}
254d8ab6e1Don Brady
255d8ab6e1Don Bradyconst pool_config_ops_t libzpool_config_ops = {
256d8ab6e1Don Brady	.pco_refresh_config = refresh_config,
257d8ab6e1Don Brady	.pco_pool_active = pool_active,
258d8ab6e1Don Brady};