1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * 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.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 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 RackTop Systems. 25 * Copyright (c) 2017, Intel Corporation. 26 */ 27 28 #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/refcount.h> 37 #include <sys/zfs_ioctl.h> 38 #include <dlfcn.h> 39 #include <libzutil.h> 40 41 extern void nicenum(uint64_t num, char *buf, size_t); 42 43 /* 44 * Routines needed by more than one client of libzpool. 45 */ 46 47 static void 48 show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent) 49 { 50 vdev_stat_t *vs; 51 vdev_stat_t v0 = { 0 }; 52 uint64_t sec; 53 uint64_t is_log = 0; 54 nvlist_t **child; 55 uint_t c, children; 56 char used[6], avail[6]; 57 char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6]; 58 59 if (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 } 65 66 if (desc != NULL) { 67 char *suffix = "", *bias = NULL; 68 char bias_suffix[32]; 69 70 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log); 71 (void) nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS, 72 &bias); 73 if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 74 (uint64_t **)&vs, &c) != 0) 75 vs = &v0; 76 77 if (bias != NULL) { 78 (void) snprintf(bias_suffix, sizeof (bias_suffix), 79 " (%s)", bias); 80 suffix = bias_suffix; 81 } else if (is_log) { 82 suffix = " (log)"; 83 } 84 85 sec = MAX(1, vs->vs_timestamp / NANOSEC); 86 87 nicenum(vs->vs_alloc, used, sizeof (used)); 88 nicenum(vs->vs_space - vs->vs_alloc, avail, sizeof (avail)); 89 nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops, sizeof (rops)); 90 nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops, sizeof (wops)); 91 nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes, 92 sizeof (rbytes)); 93 nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes, 94 sizeof (wbytes)); 95 nicenum(vs->vs_read_errors, rerr, sizeof (rerr)); 96 nicenum(vs->vs_write_errors, werr, sizeof (werr)); 97 nicenum(vs->vs_checksum_errors, cerr, sizeof (cerr)); 98 99 (void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n", 100 indent, "", 101 desc, 102 (int)(indent+strlen(desc)-25-(vs->vs_space ? 0 : 12)), 103 suffix, 104 vs->vs_space ? 6 : 0, vs->vs_space ? used : "", 105 vs->vs_space ? 6 : 0, vs->vs_space ? avail : "", 106 rops, wops, rbytes, wbytes, rerr, werr, cerr); 107 } 108 109 if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0) 110 return; 111 112 for (c = 0; c < children; c++) { 113 nvlist_t *cnv = child[c]; 114 char *cname, *tname; 115 uint64_t np; 116 if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) && 117 nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname)) 118 cname = "<unknown>"; 119 tname = calloc(1, strlen(cname) + 2); 120 (void) strcpy(tname, cname); 121 if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0) 122 tname[strlen(tname)] = '0' + np; 123 show_vdev_stats(tname, ctype, cnv, indent + 2); 124 free(tname); 125 } 126 } 127 128 void 129 show_pool_stats(spa_t *spa) 130 { 131 nvlist_t *config, *nvroot; 132 char *name; 133 134 VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0); 135 136 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 137 &nvroot) == 0); 138 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 139 &name) == 0); 140 141 show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0); 142 show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0); 143 show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0); 144 145 nvlist_free(config); 146 } 147 148 /* 149 * Sets given global variable in libzpool to given unsigned 32-bit value. 150 * arg: "<variable>=<value>" 151 */ 152 int 153 set_global_var(char *arg) 154 { 155 void *zpoolhdl; 156 char *varname = arg, *varval; 157 u_longlong_t val; 158 159 #ifndef _LITTLE_ENDIAN 160 /* 161 * On big endian systems changing a 64-bit variable would set the high 162 * 32 bits instead of the low 32 bits, which could cause unexpected 163 * results. 164 */ 165 fprintf(stderr, "Setting global variables is only supported on " 166 "little-endian systems\n", varname); 167 return (ENOTSUP); 168 #endif 169 if ((varval = strchr(arg, '=')) != NULL) { 170 *varval = '\0'; 171 varval++; 172 val = strtoull(varval, NULL, 0); 173 if (val > UINT32_MAX) { 174 fprintf(stderr, "Value for global variable '%s' must " 175 "be a 32-bit unsigned integer\n", varname); 176 return (EOVERFLOW); 177 } 178 } else { 179 return (EINVAL); 180 } 181 182 zpoolhdl = dlopen("libzpool.so", RTLD_LAZY); 183 if (zpoolhdl != NULL) { 184 uint32_t *var; 185 var = dlsym(zpoolhdl, varname); 186 if (var == NULL) { 187 fprintf(stderr, "Global variable '%s' does not exist " 188 "in libzpool.so\n", varname); 189 return (EINVAL); 190 } 191 *var = (uint32_t)val; 192 193 dlclose(zpoolhdl); 194 } else { 195 fprintf(stderr, "Failed to open libzpool.so to set global " 196 "variable\n"); 197 return (EIO); 198 } 199 200 return (0); 201 } 202 203 static nvlist_t * 204 refresh_config(void *unused, nvlist_t *tryconfig) 205 { 206 return (spa_tryimport(tryconfig)); 207 } 208 209 static int 210 pool_active(void *unused, const char *name, uint64_t guid, 211 boolean_t *isactive) 212 { 213 zfs_cmd_t *zcp; 214 nvlist_t *innvl; 215 char *packed = NULL; 216 size_t size = 0; 217 int fd, ret; 218 219 /* 220 * Use ZFS_IOC_POOL_SYNC to confirm if a pool is active 221 */ 222 223 fd = open("/dev/zfs", O_RDWR); 224 if (fd < 0) 225 return (-1); 226 227 zcp = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL); 228 229 innvl = fnvlist_alloc(); 230 fnvlist_add_boolean_value(innvl, "force", B_FALSE); 231 232 (void) strlcpy(zcp->zc_name, name, sizeof (zcp->zc_name)); 233 packed = fnvlist_pack(innvl, &size); 234 zcp->zc_nvlist_src = (uint64_t)(uintptr_t)packed; 235 zcp->zc_nvlist_src_size = size; 236 237 ret = ioctl(fd, ZFS_IOC_POOL_SYNC, zcp); 238 239 fnvlist_pack_free(packed, size); 240 free((void *)(uintptr_t)zcp->zc_nvlist_dst); 241 nvlist_free(innvl); 242 umem_free(zcp, sizeof (zfs_cmd_t)); 243 244 (void) close(fd); 245 246 *isactive = (ret == 0); 247 248 return (0); 249 } 250 251 const pool_config_ops_t libzpool_config_ops = { 252 .pco_refresh_config = refresh_config, 253 .pco_pool_active = pool_active, 254 }; 255