Path: blob/main/sys/contrib/openzfs/lib/libzpool/util.c
105380 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>40#include <libzpool.h>4142/*43* Routines needed by more than one client of libzpool.44*/4546static void47show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent)48{49vdev_stat_t *vs;50vdev_stat_t *v0 = { 0 };51uint64_t sec;52uint64_t is_log = 0;53nvlist_t **child;54uint_t c, children;55char used[6], avail[6];56char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];5758v0 = umem_zalloc(sizeof (*v0), UMEM_NOFAIL);5960if (indent == 0 && desc != NULL) {61(void) printf(" "62" capacity operations bandwidth ---- errors ----\n");63(void) printf("description "64"used avail read write read write read write cksum\n");65}6667if (desc != NULL) {68const char *suffix = "";69const char *bias = NULL;70char bias_suffix[32];7172(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);73(void) nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,74&bias);75if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,76(uint64_t **)&vs, &c) != 0)77vs = v0;7879if (bias != NULL) {80(void) snprintf(bias_suffix, sizeof (bias_suffix),81" (%s)", bias);82suffix = bias_suffix;83} else if (is_log) {84suffix = " (log)";85}8687sec = MAX(1, vs->vs_timestamp / NANOSEC);8889nicenum(vs->vs_alloc, used, sizeof (used));90nicenum(vs->vs_space - vs->vs_alloc, avail, sizeof (avail));91nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops, sizeof (rops));92nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops, sizeof (wops));93nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes,94sizeof (rbytes));95nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes,96sizeof (wbytes));97nicenum(vs->vs_read_errors, rerr, sizeof (rerr));98nicenum(vs->vs_write_errors, werr, sizeof (werr));99nicenum(vs->vs_checksum_errors, cerr, sizeof (cerr));100101(void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",102indent, "",103desc,104(int)(indent+strlen(desc)-25-(vs->vs_space ? 0 : 12)),105suffix,106vs->vs_space ? 6 : 0, vs->vs_space ? used : "",107vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",108rops, wops, rbytes, wbytes, rerr, werr, cerr);109}110umem_free(v0, sizeof (*v0));111112if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0)113return;114115for (c = 0; c < children; c++) {116nvlist_t *cnv = child[c];117const char *cname = NULL;118char *tname;119uint64_t np;120int len;121if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&122nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))123cname = "<unknown>";124len = strlen(cname) + 2;125tname = umem_zalloc(len, UMEM_NOFAIL);126(void) strlcpy(tname, cname, len);127if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0)128tname[strlen(tname)] = '0' + np;129show_vdev_stats(tname, ctype, cnv, indent + 2);130umem_free(tname, len);131}132}133134void135show_pool_stats(spa_t *spa)136{137nvlist_t *config, *nvroot;138const char *name;139140VERIFY0(spa_get_stats(spa_name(spa), &config, NULL, 0));141142VERIFY0(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot));143VERIFY0(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, &name));144145show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0);146show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0);147show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0);148149nvlist_free(config);150}151152/*153* Common helper for working with libzpool tunables from the command line.154*155* Valid inputs:156*157* <name> show named tunable and value158* <name>=<value> set tunable value159*160* show show all tunables and values161* show=<name> show named tunable and value162* info show info about all tunables163* info=<name> show info about named tunable164*/165166typedef enum { SHOW, INFO, SET } tunable_mode_t;167168static int169list_tunables_cb(const zfs_tunable_t *tunable, void *arg)170{171const tunable_mode_t *mode = arg;172173static const char *type[] = {174"int", "uint", "ulong", "u64", "str",175};176static const char *perm[] = {177"rw", "rd",178};179180if (*mode == SHOW) {181char val[64];182int err = zfs_tunable_get(tunable, val, sizeof (val));183if (err == 0)184printf("%s: %s\n", tunable->zt_name, val);185else186printf("%s: [error getting tunable value: %s]\n",187tunable->zt_name, strerror(err));188} else {189printf("%s [%s %s]: %s\n", tunable->zt_name,190type[tunable->zt_type], perm[tunable->zt_perm],191tunable->zt_desc);192}193194return (0);195}196int197handle_tunable_option(const char *_arg, boolean_t quiet)198{199int err = 0;200char *arg = strdup(_arg);201char *k, *v;202203v = arg;204k = strsep(&v, "=");205206tunable_mode_t mode;207208if (strcmp(k, "show") == 0) {209mode = SHOW;210k = v;211} else if (strcmp(k, "info") == 0) {212mode = INFO;213k = v;214} else if (v == NULL) {215mode = SHOW;216} else {217mode = SET;218}219220if (quiet && mode != SET) {221err = EINVAL;222goto out;223}224225if (mode == SET) {226const zfs_tunable_t *tunable = zfs_tunable_lookup(k);227if (tunable == NULL) {228err = ENOENT;229goto out;230}231232char vold[256], vnew[256];233if (zfs_tunable_get(tunable, vold, sizeof (vold)) != 0)234strcpy(vold, "???");235err = zfs_tunable_set(tunable, v);236if (err != 0)237goto out;238if (zfs_tunable_get(tunable, vnew, sizeof (vnew)) != 0)239strcpy(vnew, "???");240241if (!quiet)242printf("%s: %s -> %s\n", k, vold, vnew);243} else if (k != NULL) {244const zfs_tunable_t *tunable = zfs_tunable_lookup(k);245if (tunable == NULL) {246err = ENOENT;247goto out;248}249list_tunables_cb(tunable, &mode);250} else {251zfs_tunable_iter(list_tunables_cb, &mode);252}253254out:255if (!quiet) {256if (err == ENOENT)257fprintf(stderr, "no such tunable: %s\n", k);258else if (err != 0)259fprintf(stderr, "couldn't set tunable '%s': %s\n",260k, strerror(err));261}262263free(arg);264return (err);265}266267static nvlist_t *268refresh_config(void *unused, nvlist_t *tryconfig)269{270(void) unused;271return (spa_tryimport(tryconfig));272}273274#if defined(__FreeBSD__)275276#include <sys/param.h>277#include <sys/sysctl.h>278#include <os/freebsd/zfs/sys/zfs_ioctl_compat.h>279280static int281pool_active(void *unused, const char *name, uint64_t guid, boolean_t *isactive)282{283(void) unused, (void) guid;284zfs_iocparm_t zp;285zfs_cmd_t *zc = NULL;286#ifdef ZFS_LEGACY_SUPPORT287zfs_cmd_legacy_t *zcl = NULL;288#endif289unsigned long request;290int ret;291292int fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);293if (fd < 0)294return (-1);295296/*297* Use ZFS_IOC_POOL_STATS to check if the pool is active. We want to298* avoid adding a dependency on libzfs_core solely for this ioctl(),299* therefore we manually craft the stats command. Note that the command300* ID is identical between the openzfs and legacy ioctl() formats.301*/302int ver = ZFS_IOCVER_NONE;303size_t ver_size = sizeof (ver);304305sysctlbyname("vfs.zfs.version.ioctl", &ver, &ver_size, NULL, 0);306307switch (ver) {308case ZFS_IOCVER_OZFS:309zc = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL);310311(void) strlcpy(zc->zc_name, name, sizeof (zc->zc_name));312zp.zfs_cmd = (uint64_t)(uintptr_t)zc;313zp.zfs_cmd_size = sizeof (zfs_cmd_t);314zp.zfs_ioctl_version = ZFS_IOCVER_OZFS;315316request = _IOWR('Z', ZFS_IOC_POOL_STATS, zfs_iocparm_t);317ret = ioctl(fd, request, &zp);318319free((void *)(uintptr_t)zc->zc_nvlist_dst);320umem_free(zc, sizeof (zfs_cmd_t));321322break;323#ifdef ZFS_LEGACY_SUPPORT324case ZFS_IOCVER_LEGACY:325zcl = umem_zalloc(sizeof (zfs_cmd_legacy_t), UMEM_NOFAIL);326327(void) strlcpy(zcl->zc_name, name, sizeof (zcl->zc_name));328zp.zfs_cmd = (uint64_t)(uintptr_t)zcl;329zp.zfs_cmd_size = sizeof (zfs_cmd_legacy_t);330zp.zfs_ioctl_version = ZFS_IOCVER_LEGACY;331332request = _IOWR('Z', ZFS_IOC_POOL_STATS, zfs_iocparm_t);333ret = ioctl(fd, request, &zp);334335free((void *)(uintptr_t)zcl->zc_nvlist_dst);336umem_free(zcl, sizeof (zfs_cmd_legacy_t));337338break;339#endif340default:341fprintf(stderr, "unrecognized zfs ioctl version %d", ver);342exit(1);343}344345(void) close(fd);346347*isactive = (ret == 0);348349return (0);350}351#else352static int353pool_active(void *unused, const char *name, uint64_t guid,354boolean_t *isactive)355{356(void) unused, (void) guid;357int fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);358if (fd < 0)359return (-1);360361/*362* Use ZFS_IOC_POOL_STATS to check if a pool is active.363*/364zfs_cmd_t *zcp = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL);365(void) strlcpy(zcp->zc_name, name, sizeof (zcp->zc_name));366367int ret = ioctl(fd, ZFS_IOC_POOL_STATS, zcp);368369free((void *)(uintptr_t)zcp->zc_nvlist_dst);370umem_free(zcp, sizeof (zfs_cmd_t));371372(void) close(fd);373374*isactive = (ret == 0);375376return (0);377}378#endif379380pool_config_ops_t libzpool_config_ops = {381.pco_refresh_config = refresh_config,382.pco_pool_active = pool_active,383};384385386