Path: blob/main/sys/contrib/openzfs/lib/libzpool/util.c
48375 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* 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.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/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 King25* Copyright (c) 2017, Intel Corporation.26*/2728#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 <sys/tunables.h>39#include <libzutil.h>4041/*42* Routines needed by more than one client of libzpool.43*/4445static void46show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent)47{48vdev_stat_t *vs;49vdev_stat_t *v0 = { 0 };50uint64_t sec;51uint64_t is_log = 0;52nvlist_t **child;53uint_t c, children;54char used[6], avail[6];55char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];5657v0 = umem_zalloc(sizeof (*v0), UMEM_NOFAIL);5859if (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}6566if (desc != NULL) {67const char *suffix = "";68const char *bias = NULL;69char bias_suffix[32];7071(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);72(void) nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,73&bias);74if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,75(uint64_t **)&vs, &c) != 0)76vs = v0;7778if (bias != NULL) {79(void) snprintf(bias_suffix, sizeof (bias_suffix),80" (%s)", bias);81suffix = bias_suffix;82} else if (is_log) {83suffix = " (log)";84}8586sec = MAX(1, vs->vs_timestamp / NANOSEC);8788nicenum(vs->vs_alloc, used, sizeof (used));89nicenum(vs->vs_space - vs->vs_alloc, avail, sizeof (avail));90nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops, sizeof (rops));91nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops, sizeof (wops));92nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes,93sizeof (rbytes));94nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes,95sizeof (wbytes));96nicenum(vs->vs_read_errors, rerr, sizeof (rerr));97nicenum(vs->vs_write_errors, werr, sizeof (werr));98nicenum(vs->vs_checksum_errors, cerr, sizeof (cerr));99100(void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",101indent, "",102desc,103(int)(indent+strlen(desc)-25-(vs->vs_space ? 0 : 12)),104suffix,105vs->vs_space ? 6 : 0, vs->vs_space ? used : "",106vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",107rops, wops, rbytes, wbytes, rerr, werr, cerr);108}109umem_free(v0, sizeof (*v0));110111if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0)112return;113114for (c = 0; c < children; c++) {115nvlist_t *cnv = child[c];116const char *cname = NULL;117char *tname;118uint64_t np;119int len;120if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&121nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))122cname = "<unknown>";123len = strlen(cname) + 2;124tname = umem_zalloc(len, UMEM_NOFAIL);125(void) strlcpy(tname, cname, len);126if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0)127tname[strlen(tname)] = '0' + np;128show_vdev_stats(tname, ctype, cnv, indent + 2);129umem_free(tname, len);130}131}132133void134show_pool_stats(spa_t *spa)135{136nvlist_t *config, *nvroot;137const char *name;138139VERIFY0(spa_get_stats(spa_name(spa), &config, NULL, 0));140141VERIFY0(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot));142VERIFY0(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, &name));143144show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0);145show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0);146show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0);147148nvlist_free(config);149}150151/*152* Common helper for working with libzpool tunables from the command line.153*154* Valid inputs:155*156* <name> show named tunable and value157* <name>=<value> set tunable value158*159* show show all tunables and values160* show=<name> show named tunable and value161* info show info about all tunables162* info=<name> show info about named tunable163*/164165typedef enum { SHOW, INFO, SET } tunable_mode_t;166167static int168list_tunables_cb(const zfs_tunable_t *tunable, void *arg)169{170const tunable_mode_t *mode = arg;171172static const char *type[] = {173"int", "uint", "ulong", "u64", "str",174};175static const char *perm[] = {176"rw", "rd",177};178179if (*mode == SHOW) {180char val[64];181int err = zfs_tunable_get(tunable, val, sizeof (val));182if (err == 0)183printf("%s: %s\n", tunable->zt_name, val);184else185printf("%s: [error getting tunable value: %s]\n",186tunable->zt_name, strerror(err));187} else {188printf("%s [%s %s]: %s\n", tunable->zt_name,189type[tunable->zt_type], perm[tunable->zt_perm],190tunable->zt_desc);191}192193return (0);194}195int196handle_tunable_option(const char *_arg, boolean_t quiet)197{198int err = 0;199char *arg = strdup(_arg);200char *k, *v;201202v = arg;203k = strsep(&v, "=");204205tunable_mode_t mode;206207if (strcmp(k, "show") == 0) {208mode = SHOW;209k = v;210} else if (strcmp(k, "info") == 0) {211mode = INFO;212k = v;213} else if (v == NULL) {214mode = SHOW;215} else {216mode = SET;217}218219if (quiet && mode != SET) {220err = EINVAL;221goto out;222}223224if (mode == SET) {225const zfs_tunable_t *tunable = zfs_tunable_lookup(k);226if (tunable == NULL) {227err = ENOENT;228goto out;229}230231char vold[256], vnew[256];232if (zfs_tunable_get(tunable, vold, sizeof (vold)) != 0)233strcpy(vold, "???");234err = zfs_tunable_set(tunable, v);235if (err != 0)236goto out;237if (zfs_tunable_get(tunable, vnew, sizeof (vnew)) != 0)238strcpy(vnew, "???");239240if (!quiet)241printf("%s: %s -> %s\n", k, vold, vnew);242} else if (k != NULL) {243const zfs_tunable_t *tunable = zfs_tunable_lookup(k);244if (tunable == NULL) {245err = ENOENT;246goto out;247}248list_tunables_cb(tunable, &mode);249} else {250zfs_tunable_iter(list_tunables_cb, &mode);251}252253out:254if (!quiet) {255if (err == ENOENT)256fprintf(stderr, "no such tunable: %s\n", k);257else if (err != 0)258fprintf(stderr, "couldn't set tunable '%s': %s\n",259k, strerror(err));260}261262free(arg);263return (err);264}265266static nvlist_t *267refresh_config(void *unused, nvlist_t *tryconfig)268{269(void) unused;270return (spa_tryimport(tryconfig));271}272273#if defined(__FreeBSD__)274275#include <sys/param.h>276#include <sys/sysctl.h>277#include <os/freebsd/zfs/sys/zfs_ioctl_compat.h>278279static int280pool_active(void *unused, const char *name, uint64_t guid, boolean_t *isactive)281{282(void) unused, (void) guid;283zfs_iocparm_t zp;284zfs_cmd_t *zc = NULL;285#ifdef ZFS_LEGACY_SUPPORT286zfs_cmd_legacy_t *zcl = NULL;287#endif288unsigned long request;289int ret;290291int fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);292if (fd < 0)293return (-1);294295/*296* Use ZFS_IOC_POOL_STATS to check if the pool is active. We want to297* avoid adding a dependency on libzfs_core solely for this ioctl(),298* therefore we manually craft the stats command. Note that the command299* ID is identical between the openzfs and legacy ioctl() formats.300*/301int ver = ZFS_IOCVER_NONE;302size_t ver_size = sizeof (ver);303304sysctlbyname("vfs.zfs.version.ioctl", &ver, &ver_size, NULL, 0);305306switch (ver) {307case ZFS_IOCVER_OZFS:308zc = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL);309310(void) strlcpy(zc->zc_name, name, sizeof (zc->zc_name));311zp.zfs_cmd = (uint64_t)(uintptr_t)zc;312zp.zfs_cmd_size = sizeof (zfs_cmd_t);313zp.zfs_ioctl_version = ZFS_IOCVER_OZFS;314315request = _IOWR('Z', ZFS_IOC_POOL_STATS, zfs_iocparm_t);316ret = ioctl(fd, request, &zp);317318free((void *)(uintptr_t)zc->zc_nvlist_dst);319umem_free(zc, sizeof (zfs_cmd_t));320321break;322#ifdef ZFS_LEGACY_SUPPORT323case ZFS_IOCVER_LEGACY:324zcl = umem_zalloc(sizeof (zfs_cmd_legacy_t), UMEM_NOFAIL);325326(void) strlcpy(zcl->zc_name, name, sizeof (zcl->zc_name));327zp.zfs_cmd = (uint64_t)(uintptr_t)zcl;328zp.zfs_cmd_size = sizeof (zfs_cmd_legacy_t);329zp.zfs_ioctl_version = ZFS_IOCVER_LEGACY;330331request = _IOWR('Z', ZFS_IOC_POOL_STATS, zfs_iocparm_t);332ret = ioctl(fd, request, &zp);333334free((void *)(uintptr_t)zcl->zc_nvlist_dst);335umem_free(zcl, sizeof (zfs_cmd_legacy_t));336337break;338#endif339default:340fprintf(stderr, "unrecognized zfs ioctl version %d", ver);341exit(1);342}343344(void) close(fd);345346*isactive = (ret == 0);347348return (0);349}350#else351static int352pool_active(void *unused, const char *name, uint64_t guid,353boolean_t *isactive)354{355(void) unused, (void) guid;356int fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);357if (fd < 0)358return (-1);359360/*361* Use ZFS_IOC_POOL_STATS to check if a pool is active.362*/363zfs_cmd_t *zcp = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL);364(void) strlcpy(zcp->zc_name, name, sizeof (zcp->zc_name));365366int ret = ioctl(fd, ZFS_IOC_POOL_STATS, zcp);367368free((void *)(uintptr_t)zcp->zc_nvlist_dst);369umem_free(zcp, sizeof (zfs_cmd_t));370371(void) close(fd);372373*isactive = (ret == 0);374375return (0);376}377#endif378379pool_config_ops_t libzpool_config_ops = {380.pco_refresh_config = refresh_config,381.pco_pool_active = pool_active,382};383384385