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. 26fa9e4066Sahrens */ 27fa9e4066Sahrens 28fa9e4066Sahrens #include <assert.h> 29fa9e4066Sahrens #include <sys/zfs_context.h> 30fa9e4066Sahrens #include <sys/avl.h> 31fa9e4066Sahrens #include <string.h> 32fa9e4066Sahrens #include <stdio.h> 33fa9e4066Sahrens #include <stdlib.h> 34fa9e4066Sahrens #include <sys/spa.h> 35fa9e4066Sahrens #include <sys/fs/zfs.h> 36ea8dc4b6Seschrock #include <sys/refcount.h> 37*d8ab6e12SDon Brady #include <sys/zfs_ioctl.h> 380e60744cSPavel Zakharov #include <dlfcn.h> 39*d8ab6e12SDon Brady #include <libzutil.h> 40fa9e4066Sahrens 41f06dce2cSAndrew Stormont extern void nicenum(uint64_t num, char *buf, size_t); 42f06dce2cSAndrew Stormont 43fa9e4066Sahrens /* 44fa9e4066Sahrens * Routines needed by more than one client of libzpool. 45fa9e4066Sahrens */ 46fa9e4066Sahrens 47fa9e4066Sahrens static void 48e14bb325SJeff Bonwick show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent) 49fa9e4066Sahrens { 50fa9e4066Sahrens vdev_stat_t *vs; 51e14bb325SJeff Bonwick vdev_stat_t v0 = { 0 }; 52fa9e4066Sahrens uint64_t sec; 538654d025Sperrin uint64_t is_log = 0; 54e14bb325SJeff Bonwick nvlist_t **child; 55e14bb325SJeff Bonwick uint_t c, children; 56fa9e4066Sahrens char used[6], avail[6]; 57fa9e4066Sahrens char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6]; 58fa9e4066Sahrens 59e14bb325SJeff Bonwick if (indent == 0 && desc != NULL) { 60e14bb325SJeff Bonwick (void) printf(" " 61fa9e4066Sahrens " capacity operations bandwidth ---- errors ----\n"); 62e14bb325SJeff Bonwick (void) printf("description " 63fa9e4066Sahrens "used avail read write read write read write cksum\n"); 64fa9e4066Sahrens } 65fa9e4066Sahrens 66e14bb325SJeff Bonwick if (desc != NULL) { 67663207adSDon Brady char *suffix = "", *bias = NULL; 68663207adSDon Brady char bias_suffix[32]; 69e14bb325SJeff Bonwick 70663207adSDon Brady (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log); 71663207adSDon Brady (void) nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS, 72663207adSDon Brady &bias); 733f9d6ad7SLin Ling if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 74e14bb325SJeff Bonwick (uint64_t **)&vs, &c) != 0) 75e14bb325SJeff Bonwick vs = &v0; 76e14bb325SJeff Bonwick 77663207adSDon Brady if (bias != NULL) { 78663207adSDon Brady (void) snprintf(bias_suffix, sizeof (bias_suffix), 79663207adSDon Brady " (%s)", bias); 80663207adSDon Brady suffix = bias_suffix; 81663207adSDon Brady } else if (is_log) { 82663207adSDon Brady suffix = " (log)"; 83663207adSDon Brady } 84663207adSDon Brady 85e14bb325SJeff Bonwick sec = MAX(1, vs->vs_timestamp / NANOSEC); 86e14bb325SJeff Bonwick 870a055120SJason King nicenum(vs->vs_alloc, used, sizeof (used)); 880a055120SJason King nicenum(vs->vs_space - vs->vs_alloc, avail, sizeof (avail)); 890a055120SJason King nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops, sizeof (rops)); 900a055120SJason King nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops, sizeof (wops)); 910a055120SJason King nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes, 920a055120SJason King sizeof (rbytes)); 930a055120SJason King nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes, 940a055120SJason King sizeof (wbytes)); 950a055120SJason King nicenum(vs->vs_read_errors, rerr, sizeof (rerr)); 960a055120SJason King nicenum(vs->vs_write_errors, werr, sizeof (werr)); 970a055120SJason King nicenum(vs->vs_checksum_errors, cerr, sizeof (cerr)); 98e14bb325SJeff Bonwick 99e14bb325SJeff Bonwick (void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n", 100e14bb325SJeff Bonwick indent, "", 101e14bb325SJeff Bonwick desc, 102663207adSDon Brady (int)(indent+strlen(desc)-25-(vs->vs_space ? 0 : 12)), 103663207adSDon Brady suffix, 104e14bb325SJeff Bonwick vs->vs_space ? 6 : 0, vs->vs_space ? used : "", 105e14bb325SJeff Bonwick vs->vs_space ? 6 : 0, vs->vs_space ? avail : "", 106e14bb325SJeff Bonwick rops, wops, rbytes, wbytes, rerr, werr, cerr); 107e14bb325SJeff Bonwick } 108e14bb325SJeff Bonwick 109e14bb325SJeff Bonwick if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0) 110fa9e4066Sahrens return; 111fa9e4066Sahrens 112fa9e4066Sahrens for (c = 0; c < children; c++) { 113fa9e4066Sahrens nvlist_t *cnv = child[c]; 11499653d4eSeschrock char *cname, *tname; 11599653d4eSeschrock uint64_t np; 116fa9e4066Sahrens if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) && 117fa9e4066Sahrens nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname)) 118fa9e4066Sahrens cname = "<unknown>"; 11999653d4eSeschrock tname = calloc(1, strlen(cname) + 2); 12099653d4eSeschrock (void) strcpy(tname, cname); 12199653d4eSeschrock if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0) 12299653d4eSeschrock tname[strlen(tname)] = '0' + np; 123e14bb325SJeff Bonwick show_vdev_stats(tname, ctype, cnv, indent + 2); 12499653d4eSeschrock free(tname); 125fa9e4066Sahrens } 126fa9e4066Sahrens } 127fa9e4066Sahrens 128fa9e4066Sahrens void 129fa9e4066Sahrens show_pool_stats(spa_t *spa) 130fa9e4066Sahrens { 1310373e76bSbonwick nvlist_t *config, *nvroot; 1320373e76bSbonwick char *name; 133fa9e4066Sahrens 134e14bb325SJeff Bonwick VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0); 1350373e76bSbonwick 136fa9e4066Sahrens VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 137fa9e4066Sahrens &nvroot) == 0); 1380373e76bSbonwick VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1390373e76bSbonwick &name) == 0); 140fa9e4066Sahrens 141e14bb325SJeff Bonwick show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0); 142e14bb325SJeff Bonwick show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0); 143e14bb325SJeff Bonwick show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0); 144e14bb325SJeff Bonwick 145e14bb325SJeff Bonwick nvlist_free(config); 146fa9e4066Sahrens } 1470e60744cSPavel Zakharov 1480e60744cSPavel Zakharov /* 1490e60744cSPavel Zakharov * Sets given global variable in libzpool to given unsigned 32-bit value. 1500e60744cSPavel Zakharov * arg: "<variable>=<value>" 1510e60744cSPavel Zakharov */ 1520e60744cSPavel Zakharov int 1530e60744cSPavel Zakharov set_global_var(char *arg) 1540e60744cSPavel Zakharov { 1550e60744cSPavel Zakharov void *zpoolhdl; 1560e60744cSPavel Zakharov char *varname = arg, *varval; 1570e60744cSPavel Zakharov u_longlong_t val; 1580e60744cSPavel Zakharov 1590e60744cSPavel Zakharov #ifndef _LITTLE_ENDIAN 1600e60744cSPavel Zakharov /* 1610e60744cSPavel Zakharov * On big endian systems changing a 64-bit variable would set the high 1620e60744cSPavel Zakharov * 32 bits instead of the low 32 bits, which could cause unexpected 1630e60744cSPavel Zakharov * results. 1640e60744cSPavel Zakharov */ 1650e60744cSPavel Zakharov fprintf(stderr, "Setting global variables is only supported on " 1660e60744cSPavel Zakharov "little-endian systems\n", varname); 1670e60744cSPavel Zakharov return (ENOTSUP); 1680e60744cSPavel Zakharov #endif 1690e60744cSPavel Zakharov if ((varval = strchr(arg, '=')) != NULL) { 1700e60744cSPavel Zakharov *varval = '\0'; 1710e60744cSPavel Zakharov varval++; 1720e60744cSPavel Zakharov val = strtoull(varval, NULL, 0); 1730e60744cSPavel Zakharov if (val > UINT32_MAX) { 1740e60744cSPavel Zakharov fprintf(stderr, "Value for global variable '%s' must " 1750e60744cSPavel Zakharov "be a 32-bit unsigned integer\n", varname); 1760e60744cSPavel Zakharov return (EOVERFLOW); 1770e60744cSPavel Zakharov } 1780e60744cSPavel Zakharov } else { 1790e60744cSPavel Zakharov return (EINVAL); 1800e60744cSPavel Zakharov } 1810e60744cSPavel Zakharov 1820e60744cSPavel Zakharov zpoolhdl = dlopen("libzpool.so", RTLD_LAZY); 1830e60744cSPavel Zakharov if (zpoolhdl != NULL) { 1840e60744cSPavel Zakharov uint32_t *var; 1850e60744cSPavel Zakharov var = dlsym(zpoolhdl, varname); 1860e60744cSPavel Zakharov if (var == NULL) { 1870e60744cSPavel Zakharov fprintf(stderr, "Global variable '%s' does not exist " 1880e60744cSPavel Zakharov "in libzpool.so\n", varname); 1890e60744cSPavel Zakharov return (EINVAL); 1900e60744cSPavel Zakharov } 1910e60744cSPavel Zakharov *var = (uint32_t)val; 1920e60744cSPavel Zakharov 1930e60744cSPavel Zakharov dlclose(zpoolhdl); 1940e60744cSPavel Zakharov } else { 1950e60744cSPavel Zakharov fprintf(stderr, "Failed to open libzpool.so to set global " 1960e60744cSPavel Zakharov "variable\n"); 1970e60744cSPavel Zakharov return (EIO); 1980e60744cSPavel Zakharov } 1990e60744cSPavel Zakharov 2000e60744cSPavel Zakharov return (0); 2010e60744cSPavel Zakharov } 202*d8ab6e12SDon Brady 203*d8ab6e12SDon Brady static nvlist_t * 204*d8ab6e12SDon Brady refresh_config(void *unused, nvlist_t *tryconfig) 205*d8ab6e12SDon Brady { 206*d8ab6e12SDon Brady return (spa_tryimport(tryconfig)); 207*d8ab6e12SDon Brady } 208*d8ab6e12SDon Brady 209*d8ab6e12SDon Brady static int 210*d8ab6e12SDon Brady pool_active(void *unused, const char *name, uint64_t guid, 211*d8ab6e12SDon Brady boolean_t *isactive) 212*d8ab6e12SDon Brady { 213*d8ab6e12SDon Brady zfs_cmd_t *zcp; 214*d8ab6e12SDon Brady nvlist_t *innvl; 215*d8ab6e12SDon Brady char *packed = NULL; 216*d8ab6e12SDon Brady size_t size = 0; 217*d8ab6e12SDon Brady int fd, ret; 218*d8ab6e12SDon Brady 219*d8ab6e12SDon Brady /* 220*d8ab6e12SDon Brady * Use ZFS_IOC_POOL_SYNC to confirm if a pool is active 221*d8ab6e12SDon Brady */ 222*d8ab6e12SDon Brady 223*d8ab6e12SDon Brady fd = open("/dev/zfs", O_RDWR); 224*d8ab6e12SDon Brady if (fd < 0) 225*d8ab6e12SDon Brady return (-1); 226*d8ab6e12SDon Brady 227*d8ab6e12SDon Brady zcp = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL); 228*d8ab6e12SDon Brady 229*d8ab6e12SDon Brady innvl = fnvlist_alloc(); 230*d8ab6e12SDon Brady fnvlist_add_boolean_value(innvl, "force", B_FALSE); 231*d8ab6e12SDon Brady 232*d8ab6e12SDon Brady (void) strlcpy(zcp->zc_name, name, sizeof (zcp->zc_name)); 233*d8ab6e12SDon Brady packed = fnvlist_pack(innvl, &size); 234*d8ab6e12SDon Brady zcp->zc_nvlist_src = (uint64_t)(uintptr_t)packed; 235*d8ab6e12SDon Brady zcp->zc_nvlist_src_size = size; 236*d8ab6e12SDon Brady 237*d8ab6e12SDon Brady ret = ioctl(fd, ZFS_IOC_POOL_SYNC, zcp); 238*d8ab6e12SDon Brady 239*d8ab6e12SDon Brady fnvlist_pack_free(packed, size); 240*d8ab6e12SDon Brady free((void *)(uintptr_t)zcp->zc_nvlist_dst); 241*d8ab6e12SDon Brady nvlist_free(innvl); 242*d8ab6e12SDon Brady umem_free(zcp, sizeof (zfs_cmd_t)); 243*d8ab6e12SDon Brady 244*d8ab6e12SDon Brady (void) close(fd); 245*d8ab6e12SDon Brady 246*d8ab6e12SDon Brady *isactive = (ret == 0); 247*d8ab6e12SDon Brady 248*d8ab6e12SDon Brady return (0); 249*d8ab6e12SDon Brady } 250*d8ab6e12SDon Brady 251*d8ab6e12SDon Brady const pool_config_ops_t libzpool_config_ops = { 252*d8ab6e12SDon Brady .pco_refresh_config = refresh_config, 253*d8ab6e12SDon Brady .pco_pool_active = pool_active, 254*d8ab6e12SDon Brady }; 255