1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or https://opensource.org/licenses/CDDL-1.0. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2016 by Delphix. All rights reserved. 25 * Copyright 2017 Jason King 26 * Copyright (c) 2017, Intel Corporation. 27 */ 28 29 #include <assert.h> 30 #include <sys/zfs_context.h> 31 #include <sys/avl.h> 32 #include <string.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <sys/spa.h> 36 #include <sys/fs/zfs.h> 37 #include <sys/zfs_refcount.h> 38 #include <sys/zfs_ioctl.h> 39 #include <sys/tunables.h> 40 #include <libzutil.h> 41 #include <libzpool.h> 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 v0 = umem_zalloc(sizeof (*v0), UMEM_NOFAIL); 60 61 if (indent == 0 && desc != NULL) { 62 (void) printf(" " 63 " capacity operations bandwidth ---- errors ----\n"); 64 (void) printf("description " 65 "used avail read write read write read write cksum\n"); 66 } 67 68 if (desc != NULL) { 69 const char *suffix = ""; 70 const char *bias = NULL; 71 char bias_suffix[32]; 72 73 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log); 74 (void) nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS, 75 &bias); 76 if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 77 (uint64_t **)&vs, &c) != 0) 78 vs = v0; 79 80 if (bias != NULL) { 81 (void) snprintf(bias_suffix, sizeof (bias_suffix), 82 " (%s)", bias); 83 suffix = bias_suffix; 84 } else if (is_log) { 85 suffix = " (log)"; 86 } 87 88 sec = MAX(1, vs->vs_timestamp / NANOSEC); 89 90 nicenum(vs->vs_alloc, used, sizeof (used)); 91 nicenum(vs->vs_space - vs->vs_alloc, avail, sizeof (avail)); 92 nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops, sizeof (rops)); 93 nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops, sizeof (wops)); 94 nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes, 95 sizeof (rbytes)); 96 nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes, 97 sizeof (wbytes)); 98 nicenum(vs->vs_read_errors, rerr, sizeof (rerr)); 99 nicenum(vs->vs_write_errors, werr, sizeof (werr)); 100 nicenum(vs->vs_checksum_errors, cerr, sizeof (cerr)); 101 102 (void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n", 103 indent, "", 104 desc, 105 (int)(indent+strlen(desc)-25-(vs->vs_space ? 0 : 12)), 106 suffix, 107 vs->vs_space ? 6 : 0, vs->vs_space ? used : "", 108 vs->vs_space ? 6 : 0, vs->vs_space ? avail : "", 109 rops, wops, rbytes, wbytes, rerr, werr, cerr); 110 } 111 umem_free(v0, sizeof (*v0)); 112 113 if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0) 114 return; 115 116 for (c = 0; c < children; c++) { 117 nvlist_t *cnv = child[c]; 118 const char *cname = NULL; 119 char *tname; 120 uint64_t np; 121 int len; 122 if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) && 123 nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname)) 124 cname = "<unknown>"; 125 len = strlen(cname) + 2; 126 tname = umem_zalloc(len, UMEM_NOFAIL); 127 (void) strlcpy(tname, cname, len); 128 if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0) 129 tname[strlen(tname)] = '0' + np; 130 show_vdev_stats(tname, ctype, cnv, indent + 2); 131 umem_free(tname, len); 132 } 133 } 134 135 void 136 show_pool_stats(spa_t *spa) 137 { 138 nvlist_t *config, *nvroot; 139 const char *name; 140 141 VERIFY0(spa_get_stats(spa_name(spa), &config, NULL, 0)); 142 143 VERIFY0(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot)); 144 VERIFY0(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, &name)); 145 146 show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0); 147 show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0); 148 show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0); 149 150 nvlist_free(config); 151 } 152 153 /* 154 * Common helper for working with libzpool tunables from the command line. 155 * 156 * Valid inputs: 157 * 158 * <name> show named tunable and value 159 * <name>=<value> set tunable value 160 * 161 * show show all tunables and values 162 * show=<name> show named tunable and value 163 * info show info about all tunables 164 * info=<name> show info about named tunable 165 */ 166 167 typedef enum { SHOW, INFO, SET } tunable_mode_t; 168 169 static int 170 list_tunables_cb(const zfs_tunable_t *tunable, void *arg) 171 { 172 const tunable_mode_t *mode = arg; 173 174 static const char *type[] = { 175 "int", "uint", "ulong", "u64", "str", 176 }; 177 static const char *perm[] = { 178 "rw", "rd", 179 }; 180 181 if (*mode == SHOW) { 182 char val[64]; 183 int err = zfs_tunable_get(tunable, val, sizeof (val)); 184 if (err == 0) 185 printf("%s: %s\n", tunable->zt_name, val); 186 else 187 printf("%s: [error getting tunable value: %s]\n", 188 tunable->zt_name, strerror(err)); 189 } else { 190 printf("%s [%s %s]: %s\n", tunable->zt_name, 191 type[tunable->zt_type], perm[tunable->zt_perm], 192 tunable->zt_desc); 193 } 194 195 return (0); 196 } 197 int 198 handle_tunable_option(const char *_arg, boolean_t quiet) 199 { 200 int err = 0; 201 char *arg = strdup(_arg); 202 char *k, *v; 203 204 v = arg; 205 k = strsep(&v, "="); 206 207 tunable_mode_t mode; 208 209 if (strcmp(k, "show") == 0) { 210 mode = SHOW; 211 k = v; 212 } else if (strcmp(k, "info") == 0) { 213 mode = INFO; 214 k = v; 215 } else if (v == NULL) { 216 mode = SHOW; 217 } else { 218 mode = SET; 219 } 220 221 if (quiet && mode != SET) { 222 err = EINVAL; 223 goto out; 224 } 225 226 if (mode == SET) { 227 const zfs_tunable_t *tunable = zfs_tunable_lookup(k); 228 if (tunable == NULL) { 229 err = ENOENT; 230 goto out; 231 } 232 233 char vold[256], vnew[256]; 234 if (zfs_tunable_get(tunable, vold, sizeof (vold)) != 0) 235 strcpy(vold, "???"); 236 err = zfs_tunable_set(tunable, v); 237 if (err != 0) 238 goto out; 239 if (zfs_tunable_get(tunable, vnew, sizeof (vnew)) != 0) 240 strcpy(vnew, "???"); 241 242 if (!quiet) 243 printf("%s: %s -> %s\n", k, vold, vnew); 244 } else if (k != NULL) { 245 const zfs_tunable_t *tunable = zfs_tunable_lookup(k); 246 if (tunable == NULL) { 247 err = ENOENT; 248 goto out; 249 } 250 list_tunables_cb(tunable, &mode); 251 } else { 252 zfs_tunable_iter(list_tunables_cb, &mode); 253 } 254 255 out: 256 if (!quiet) { 257 if (err == ENOENT) 258 fprintf(stderr, "no such tunable: %s\n", k); 259 else if (err != 0) 260 fprintf(stderr, "couldn't set tunable '%s': %s\n", 261 k, strerror(err)); 262 } 263 264 free(arg); 265 return (err); 266 } 267 268 static nvlist_t * 269 refresh_config(void *unused, nvlist_t *tryconfig) 270 { 271 (void) unused; 272 return (spa_tryimport(tryconfig)); 273 } 274 275 #if defined(__FreeBSD__) 276 277 #include <sys/param.h> 278 #include <sys/sysctl.h> 279 #include <os/freebsd/zfs/sys/zfs_ioctl_compat.h> 280 281 static int 282 pool_active(void *unused, const char *name, uint64_t guid, boolean_t *isactive) 283 { 284 (void) unused, (void) guid; 285 zfs_iocparm_t zp; 286 zfs_cmd_t *zc = NULL; 287 #ifdef ZFS_LEGACY_SUPPORT 288 zfs_cmd_legacy_t *zcl = NULL; 289 #endif 290 unsigned long request; 291 int ret; 292 293 int fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC); 294 if (fd < 0) 295 return (-1); 296 297 /* 298 * Use ZFS_IOC_POOL_STATS to check if the pool is active. We want to 299 * avoid adding a dependency on libzfs_core solely for this ioctl(), 300 * therefore we manually craft the stats command. Note that the command 301 * ID is identical between the openzfs and legacy ioctl() formats. 302 */ 303 int ver = ZFS_IOCVER_NONE; 304 size_t ver_size = sizeof (ver); 305 306 sysctlbyname("vfs.zfs.version.ioctl", &ver, &ver_size, NULL, 0); 307 308 switch (ver) { 309 case ZFS_IOCVER_OZFS: 310 zc = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL); 311 312 (void) strlcpy(zc->zc_name, name, sizeof (zc->zc_name)); 313 zp.zfs_cmd = (uint64_t)(uintptr_t)zc; 314 zp.zfs_cmd_size = sizeof (zfs_cmd_t); 315 zp.zfs_ioctl_version = ZFS_IOCVER_OZFS; 316 317 request = _IOWR('Z', ZFS_IOC_POOL_STATS, zfs_iocparm_t); 318 ret = ioctl(fd, request, &zp); 319 320 free((void *)(uintptr_t)zc->zc_nvlist_dst); 321 umem_free(zc, sizeof (zfs_cmd_t)); 322 323 break; 324 #ifdef ZFS_LEGACY_SUPPORT 325 case ZFS_IOCVER_LEGACY: 326 zcl = umem_zalloc(sizeof (zfs_cmd_legacy_t), UMEM_NOFAIL); 327 328 (void) strlcpy(zcl->zc_name, name, sizeof (zcl->zc_name)); 329 zp.zfs_cmd = (uint64_t)(uintptr_t)zcl; 330 zp.zfs_cmd_size = sizeof (zfs_cmd_legacy_t); 331 zp.zfs_ioctl_version = ZFS_IOCVER_LEGACY; 332 333 request = _IOWR('Z', ZFS_IOC_POOL_STATS, zfs_iocparm_t); 334 ret = ioctl(fd, request, &zp); 335 336 free((void *)(uintptr_t)zcl->zc_nvlist_dst); 337 umem_free(zcl, sizeof (zfs_cmd_legacy_t)); 338 339 break; 340 #endif 341 default: 342 fprintf(stderr, "unrecognized zfs ioctl version %d", ver); 343 exit(1); 344 } 345 346 (void) close(fd); 347 348 *isactive = (ret == 0); 349 350 return (0); 351 } 352 #else 353 static int 354 pool_active(void *unused, const char *name, uint64_t guid, 355 boolean_t *isactive) 356 { 357 (void) unused, (void) guid; 358 int fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC); 359 if (fd < 0) 360 return (-1); 361 362 /* 363 * Use ZFS_IOC_POOL_STATS to check if a pool is active. 364 */ 365 zfs_cmd_t *zcp = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL); 366 (void) strlcpy(zcp->zc_name, name, sizeof (zcp->zc_name)); 367 368 int ret = ioctl(fd, ZFS_IOC_POOL_STATS, zcp); 369 370 free((void *)(uintptr_t)zcp->zc_nvlist_dst); 371 umem_free(zcp, sizeof (zfs_cmd_t)); 372 373 (void) close(fd); 374 375 *isactive = (ret == 0); 376 377 return (0); 378 } 379 #endif 380 381 pool_config_ops_t libzpool_config_ops = { 382 .pco_refresh_config = refresh_config, 383 .pco_pool_active = pool_active, 384 }; 385