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