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 42 /* 43 * Routines needed by more than one client of libzpool. 44 */ 45 46 static void 47 show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent) 48 { 49 vdev_stat_t *vs; 50 vdev_stat_t *v0 = { 0 }; 51 uint64_t sec; 52 uint64_t is_log = 0; 53 nvlist_t **child; 54 uint_t c, children; 55 char used[6], avail[6]; 56 char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6]; 57 58 v0 = umem_zalloc(sizeof (*v0), UMEM_NOFAIL); 59 60 if (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 } 66 67 if (desc != NULL) { 68 const char *suffix = ""; 69 const char *bias = NULL; 70 char bias_suffix[32]; 71 72 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log); 73 (void) nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS, 74 &bias); 75 if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 76 (uint64_t **)&vs, &c) != 0) 77 vs = v0; 78 79 if (bias != NULL) { 80 (void) snprintf(bias_suffix, sizeof (bias_suffix), 81 " (%s)", bias); 82 suffix = bias_suffix; 83 } else if (is_log) { 84 suffix = " (log)"; 85 } 86 87 sec = MAX(1, vs->vs_timestamp / NANOSEC); 88 89 nicenum(vs->vs_alloc, used, sizeof (used)); 90 nicenum(vs->vs_space - vs->vs_alloc, avail, sizeof (avail)); 91 nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops, sizeof (rops)); 92 nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops, sizeof (wops)); 93 nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes, 94 sizeof (rbytes)); 95 nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes, 96 sizeof (wbytes)); 97 nicenum(vs->vs_read_errors, rerr, sizeof (rerr)); 98 nicenum(vs->vs_write_errors, werr, sizeof (werr)); 99 nicenum(vs->vs_checksum_errors, cerr, sizeof (cerr)); 100 101 (void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n", 102 indent, "", 103 desc, 104 (int)(indent+strlen(desc)-25-(vs->vs_space ? 0 : 12)), 105 suffix, 106 vs->vs_space ? 6 : 0, vs->vs_space ? used : "", 107 vs->vs_space ? 6 : 0, vs->vs_space ? avail : "", 108 rops, wops, rbytes, wbytes, rerr, werr, cerr); 109 } 110 umem_free(v0, sizeof (*v0)); 111 112 if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0) 113 return; 114 115 for (c = 0; c < children; c++) { 116 nvlist_t *cnv = child[c]; 117 const char *cname = NULL; 118 char *tname; 119 uint64_t np; 120 int len; 121 if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) && 122 nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname)) 123 cname = "<unknown>"; 124 len = strlen(cname) + 2; 125 tname = umem_zalloc(len, UMEM_NOFAIL); 126 (void) strlcpy(tname, cname, len); 127 if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0) 128 tname[strlen(tname)] = '0' + np; 129 show_vdev_stats(tname, ctype, cnv, indent + 2); 130 umem_free(tname, len); 131 } 132 } 133 134 void 135 show_pool_stats(spa_t *spa) 136 { 137 nvlist_t *config, *nvroot; 138 const char *name; 139 140 VERIFY0(spa_get_stats(spa_name(spa), &config, NULL, 0)); 141 142 VERIFY0(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot)); 143 VERIFY0(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, &name)); 144 145 show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0); 146 show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0); 147 show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0); 148 149 nvlist_free(config); 150 } 151 152 /* 153 * Common helper for working with libzpool tunables from the command line. 154 * 155 * Valid inputs: 156 * 157 * <name> show named tunable and value 158 * <name>=<value> set tunable value 159 * 160 * show show all tunables and values 161 * show=<name> show named tunable and value 162 * info show info about all tunables 163 * info=<name> show info about named tunable 164 */ 165 166 typedef enum { SHOW, INFO, SET } tunable_mode_t; 167 168 static int 169 list_tunables_cb(const zfs_tunable_t *tunable, void *arg) 170 { 171 const tunable_mode_t *mode = arg; 172 173 static const char *type[] = { 174 "int", "uint", "ulong", "u64", "str", 175 }; 176 static const char *perm[] = { 177 "rw", "rd", 178 }; 179 180 if (*mode == SHOW) { 181 char val[64]; 182 int err = zfs_tunable_get(tunable, val, sizeof (val)); 183 if (err == 0) 184 printf("%s: %s\n", tunable->zt_name, val); 185 else 186 printf("%s: [error getting tunable value: %s]\n", 187 tunable->zt_name, strerror(err)); 188 } else { 189 printf("%s [%s %s]: %s\n", tunable->zt_name, 190 type[tunable->zt_type], perm[tunable->zt_perm], 191 tunable->zt_desc); 192 } 193 194 return (0); 195 } 196 int 197 handle_tunable_option(const char *_arg, boolean_t quiet) 198 { 199 int err = 0; 200 char *arg = strdup(_arg); 201 char *k, *v; 202 203 v = arg; 204 k = strsep(&v, "="); 205 206 tunable_mode_t mode; 207 208 if (strcmp(k, "show") == 0) { 209 mode = SHOW; 210 k = v; 211 } else if (strcmp(k, "info") == 0) { 212 mode = INFO; 213 k = v; 214 } else if (v == NULL) { 215 mode = SHOW; 216 } else { 217 mode = SET; 218 } 219 220 if (quiet && mode != SET) { 221 err = EINVAL; 222 goto out; 223 } 224 225 if (mode == SET) { 226 const zfs_tunable_t *tunable = zfs_tunable_lookup(k); 227 if (tunable == NULL) { 228 err = ENOENT; 229 goto out; 230 } 231 232 char vold[256], vnew[256]; 233 if (zfs_tunable_get(tunable, vold, sizeof (vold)) != 0) 234 strcpy(vold, "???"); 235 err = zfs_tunable_set(tunable, v); 236 if (err != 0) 237 goto out; 238 if (zfs_tunable_get(tunable, vnew, sizeof (vnew)) != 0) 239 strcpy(vnew, "???"); 240 241 if (!quiet) 242 printf("%s: %s -> %s\n", k, vold, vnew); 243 } else if (k != NULL) { 244 const zfs_tunable_t *tunable = zfs_tunable_lookup(k); 245 if (tunable == NULL) { 246 err = ENOENT; 247 goto out; 248 } 249 list_tunables_cb(tunable, &mode); 250 } else { 251 zfs_tunable_iter(list_tunables_cb, &mode); 252 } 253 254 out: 255 if (!quiet) { 256 if (err == ENOENT) 257 fprintf(stderr, "no such tunable: %s\n", k); 258 else if (err != 0) 259 fprintf(stderr, "couldn't set tunable '%s': %s\n", 260 k, strerror(err)); 261 } 262 263 free(arg); 264 return (err); 265 } 266 267 static nvlist_t * 268 refresh_config(void *unused, nvlist_t *tryconfig) 269 { 270 (void) unused; 271 return (spa_tryimport(tryconfig)); 272 } 273 274 #if defined(__FreeBSD__) 275 276 #include <sys/param.h> 277 #include <sys/sysctl.h> 278 #include <os/freebsd/zfs/sys/zfs_ioctl_compat.h> 279 280 static int 281 pool_active(void *unused, const char *name, uint64_t guid, boolean_t *isactive) 282 { 283 (void) unused, (void) guid; 284 zfs_iocparm_t zp; 285 zfs_cmd_t *zc = NULL; 286 #ifdef ZFS_LEGACY_SUPPORT 287 zfs_cmd_legacy_t *zcl = NULL; 288 #endif 289 unsigned long request; 290 int ret; 291 292 int fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC); 293 if (fd < 0) 294 return (-1); 295 296 /* 297 * Use ZFS_IOC_POOL_STATS to check if the pool is active. We want to 298 * avoid adding a dependency on libzfs_core solely for this ioctl(), 299 * therefore we manually craft the stats command. Note that the command 300 * ID is identical between the openzfs and legacy ioctl() formats. 301 */ 302 int ver = ZFS_IOCVER_NONE; 303 size_t ver_size = sizeof (ver); 304 305 sysctlbyname("vfs.zfs.version.ioctl", &ver, &ver_size, NULL, 0); 306 307 switch (ver) { 308 case ZFS_IOCVER_OZFS: 309 zc = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL); 310 311 (void) strlcpy(zc->zc_name, name, sizeof (zc->zc_name)); 312 zp.zfs_cmd = (uint64_t)(uintptr_t)zc; 313 zp.zfs_cmd_size = sizeof (zfs_cmd_t); 314 zp.zfs_ioctl_version = ZFS_IOCVER_OZFS; 315 316 request = _IOWR('Z', ZFS_IOC_POOL_STATS, zfs_iocparm_t); 317 ret = ioctl(fd, request, &zp); 318 319 free((void *)(uintptr_t)zc->zc_nvlist_dst); 320 umem_free(zc, sizeof (zfs_cmd_t)); 321 322 break; 323 #ifdef ZFS_LEGACY_SUPPORT 324 case ZFS_IOCVER_LEGACY: 325 zcl = umem_zalloc(sizeof (zfs_cmd_legacy_t), UMEM_NOFAIL); 326 327 (void) strlcpy(zcl->zc_name, name, sizeof (zcl->zc_name)); 328 zp.zfs_cmd = (uint64_t)(uintptr_t)zcl; 329 zp.zfs_cmd_size = sizeof (zfs_cmd_legacy_t); 330 zp.zfs_ioctl_version = ZFS_IOCVER_LEGACY; 331 332 request = _IOWR('Z', ZFS_IOC_POOL_STATS, zfs_iocparm_t); 333 ret = ioctl(fd, request, &zp); 334 335 free((void *)(uintptr_t)zcl->zc_nvlist_dst); 336 umem_free(zcl, sizeof (zfs_cmd_legacy_t)); 337 338 break; 339 #endif 340 default: 341 fprintf(stderr, "unrecognized zfs ioctl version %d", ver); 342 exit(1); 343 } 344 345 (void) close(fd); 346 347 *isactive = (ret == 0); 348 349 return (0); 350 } 351 #else 352 static int 353 pool_active(void *unused, const char *name, uint64_t guid, 354 boolean_t *isactive) 355 { 356 (void) unused, (void) guid; 357 int fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC); 358 if (fd < 0) 359 return (-1); 360 361 /* 362 * Use ZFS_IOC_POOL_STATS to check if a pool is active. 363 */ 364 zfs_cmd_t *zcp = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL); 365 (void) strlcpy(zcp->zc_name, name, sizeof (zcp->zc_name)); 366 367 int ret = ioctl(fd, ZFS_IOC_POOL_STATS, zcp); 368 369 free((void *)(uintptr_t)zcp->zc_nvlist_dst); 370 umem_free(zcp, sizeof (zfs_cmd_t)); 371 372 (void) close(fd); 373 374 *isactive = (ret == 0); 375 376 return (0); 377 } 378 #endif 379 380 pool_config_ops_t libzpool_config_ops = { 381 .pco_refresh_config = refresh_config, 382 .pco_pool_active = pool_active, 383 }; 384