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 Jason King 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/zfs_refcount.h> 37 #include <sys/zfs_ioctl.h> 38 #include <dlfcn.h> 39 #include <libzutil.h> 40 41 /* 42 * Routines needed by more than one client of libzpool. 43 */ 44 45 static void 46 show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent) 47 { 48 vdev_stat_t *vs; 49 vdev_stat_t *v0 = { 0 }; 50 uint64_t sec; 51 uint64_t is_log = 0; 52 nvlist_t **child; 53 uint_t c, children; 54 char used[6], avail[6]; 55 char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6]; 56 57 v0 = umem_zalloc(sizeof (*v0), UMEM_NOFAIL); 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 umem_free(v0, sizeof (*v0)); 109 110 if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0) 111 return; 112 113 for (c = 0; c < children; c++) { 114 nvlist_t *cnv = child[c]; 115 char *cname = NULL, *tname; 116 uint64_t np; 117 int len; 118 if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) && 119 nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname)) 120 cname = "<unknown>"; 121 len = strlen(cname) + 2; 122 tname = umem_zalloc(len, UMEM_NOFAIL); 123 (void) strlcpy(tname, cname, len); 124 if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0) 125 tname[strlen(tname)] = '0' + np; 126 show_vdev_stats(tname, ctype, cnv, indent + 2); 127 umem_free(tname, len); 128 } 129 } 130 131 void 132 show_pool_stats(spa_t *spa) 133 { 134 nvlist_t *config, *nvroot; 135 char *name; 136 137 VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0); 138 139 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 140 &nvroot) == 0); 141 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 142 &name) == 0); 143 144 show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0); 145 show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0); 146 show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0); 147 148 nvlist_free(config); 149 } 150 151 /* *k_out must be freed by the caller */ 152 static int 153 set_global_var_parse_kv(const char *arg, char **k_out, u_longlong_t *v_out) 154 { 155 int err; 156 VERIFY(arg); 157 char *d = strdup(arg); 158 159 char *save = NULL; 160 char *k = strtok_r(d, "=", &save); 161 char *v_str = strtok_r(NULL, "=", &save); 162 char *follow = strtok_r(NULL, "=", &save); 163 if (k == NULL || v_str == NULL || follow != NULL) { 164 err = EINVAL; 165 goto err_free; 166 } 167 168 u_longlong_t val = strtoull(v_str, NULL, 0); 169 if (val > UINT32_MAX) { 170 fprintf(stderr, "Value for global variable '%s' must " 171 "be a 32-bit unsigned integer, got '%s'\n", k, v_str); 172 err = EOVERFLOW; 173 goto err_free; 174 } 175 176 *k_out = k; 177 *v_out = val; 178 return (0); 179 180 err_free: 181 free(k); 182 183 return (err); 184 } 185 186 /* 187 * Sets given global variable in libzpool to given unsigned 32-bit value. 188 * arg: "<variable>=<value>" 189 */ 190 int 191 set_global_var(char const *arg) 192 { 193 void *zpoolhdl; 194 char *varname; 195 u_longlong_t val; 196 int ret; 197 198 #ifndef _ZFS_LITTLE_ENDIAN 199 /* 200 * On big endian systems changing a 64-bit variable would set the high 201 * 32 bits instead of the low 32 bits, which could cause unexpected 202 * results. 203 */ 204 fprintf(stderr, "Setting global variables is only supported on " 205 "little-endian systems\n"); 206 ret = ENOTSUP; 207 goto out_ret; 208 #endif 209 210 if ((ret = set_global_var_parse_kv(arg, &varname, &val)) != 0) { 211 goto out_ret; 212 } 213 214 zpoolhdl = dlopen("libzpool.so", RTLD_LAZY); 215 if (zpoolhdl != NULL) { 216 uint32_t *var; 217 var = dlsym(zpoolhdl, varname); 218 if (var == NULL) { 219 fprintf(stderr, "Global variable '%s' does not exist " 220 "in libzpool.so\n", varname); 221 ret = EINVAL; 222 goto out_dlclose; 223 } 224 *var = (uint32_t)val; 225 226 } else { 227 fprintf(stderr, "Failed to open libzpool.so to set global " 228 "variable\n"); 229 ret = EIO; 230 goto out_dlclose; 231 } 232 233 ret = 0; 234 235 out_dlclose: 236 dlclose(zpoolhdl); 237 free(varname); 238 out_ret: 239 return (ret); 240 } 241 242 static nvlist_t * 243 refresh_config(void *unused, nvlist_t *tryconfig) 244 { 245 return (spa_tryimport(tryconfig)); 246 } 247 248 #if defined(__FreeBSD__) 249 250 #include <sys/param.h> 251 #include <sys/sysctl.h> 252 #include <os/freebsd/zfs/sys/zfs_ioctl_compat.h> 253 254 static int 255 pool_active(void *unused, const char *name, uint64_t guid, boolean_t *isactive) 256 { 257 zfs_iocparm_t zp; 258 zfs_cmd_t *zc = NULL; 259 zfs_cmd_legacy_t *zcl = NULL; 260 unsigned long request; 261 int ret; 262 263 int fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC); 264 if (fd < 0) 265 return (-1); 266 267 /* 268 * Use ZFS_IOC_POOL_STATS to check if the pool is active. We want to 269 * avoid adding a dependency on libzfs_core solely for this ioctl(), 270 * therefore we manually craft the stats command. Note that the command 271 * ID is identical between the openzfs and legacy ioctl() formats. 272 */ 273 int ver = ZFS_IOCVER_NONE; 274 size_t ver_size = sizeof (ver); 275 276 sysctlbyname("vfs.zfs.version.ioctl", &ver, &ver_size, NULL, 0); 277 278 switch (ver) { 279 case ZFS_IOCVER_OZFS: 280 zc = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL); 281 282 (void) strlcpy(zc->zc_name, name, sizeof (zc->zc_name)); 283 zp.zfs_cmd = (uint64_t)(uintptr_t)zc; 284 zp.zfs_cmd_size = sizeof (zfs_cmd_t); 285 zp.zfs_ioctl_version = ZFS_IOCVER_OZFS; 286 287 request = _IOWR('Z', ZFS_IOC_POOL_STATS, zfs_iocparm_t); 288 ret = ioctl(fd, request, &zp); 289 290 free((void *)(uintptr_t)zc->zc_nvlist_dst); 291 umem_free(zc, sizeof (zfs_cmd_t)); 292 293 break; 294 case ZFS_IOCVER_LEGACY: 295 zcl = umem_zalloc(sizeof (zfs_cmd_legacy_t), UMEM_NOFAIL); 296 297 (void) strlcpy(zcl->zc_name, name, sizeof (zcl->zc_name)); 298 zp.zfs_cmd = (uint64_t)(uintptr_t)zcl; 299 zp.zfs_cmd_size = sizeof (zfs_cmd_legacy_t); 300 zp.zfs_ioctl_version = ZFS_IOCVER_LEGACY; 301 302 request = _IOWR('Z', ZFS_IOC_POOL_STATS, zfs_iocparm_t); 303 ret = ioctl(fd, request, &zp); 304 305 free((void *)(uintptr_t)zcl->zc_nvlist_dst); 306 umem_free(zcl, sizeof (zfs_cmd_legacy_t)); 307 308 break; 309 default: 310 fprintf(stderr, "unrecognized zfs ioctl version %d", ver); 311 exit(1); 312 } 313 314 (void) close(fd); 315 316 *isactive = (ret == 0); 317 318 return (0); 319 } 320 #else 321 static int 322 pool_active(void *unused, const char *name, uint64_t guid, 323 boolean_t *isactive) 324 { 325 int fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC); 326 if (fd < 0) 327 return (-1); 328 329 /* 330 * Use ZFS_IOC_POOL_STATS to check if a pool is active. 331 */ 332 zfs_cmd_t *zcp = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL); 333 (void) strlcpy(zcp->zc_name, name, sizeof (zcp->zc_name)); 334 335 int ret = ioctl(fd, ZFS_IOC_POOL_STATS, zcp); 336 337 free((void *)(uintptr_t)zcp->zc_nvlist_dst); 338 umem_free(zcp, sizeof (zfs_cmd_t)); 339 340 (void) close(fd); 341 342 *isactive = (ret == 0); 343 344 return (0); 345 } 346 #endif 347 348 const pool_config_ops_t libzpool_config_ops = { 349 .pco_refresh_config = refresh_config, 350 .pco_pool_active = pool_active, 351 }; 352