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 VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0); 141 142 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 143 &nvroot) == 0); 144 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 145 &name) == 0); 146 147 show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0); 148 show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0); 149 show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0); 150 151 nvlist_free(config); 152 } 153 154 /* 155 * Common helper for working with libzpool tunables from the command line. 156 * 157 * Valid inputs: 158 * 159 * <name> show named tunable and value 160 * <name>=<value> set tunable value 161 * 162 * show show all tunables and values 163 * show=<name> show named tunable and value 164 * info show info about all tunables 165 * info=<name> show info about named tunable 166 */ 167 168 typedef enum { SHOW, INFO, SET } tunable_mode_t; 169 170 static int 171 list_tunables_cb(const zfs_tunable_t *tunable, void *arg) 172 { 173 const tunable_mode_t *mode = arg; 174 175 static const char *type[] = { 176 "int", "uint", "ulong", "u64", "str", 177 }; 178 static const char *perm[] = { 179 "rw", "rd", 180 }; 181 182 if (*mode == SHOW) { 183 char val[64]; 184 int err = zfs_tunable_get(tunable, val, sizeof (val)); 185 if (err == 0) 186 printf("%s: %s\n", tunable->zt_name, val); 187 else 188 printf("%s: [error getting tunable value: %s]\n", 189 tunable->zt_name, strerror(err)); 190 } else { 191 printf("%s [%s %s]: %s\n", tunable->zt_name, 192 type[tunable->zt_type], perm[tunable->zt_perm], 193 tunable->zt_desc); 194 } 195 196 return (0); 197 } 198 int 199 handle_tunable_option(const char *_arg, boolean_t quiet) 200 { 201 int err = 0; 202 char *arg = strdup(_arg); 203 char *k, *v; 204 205 v = arg; 206 k = strsep(&v, "="); 207 208 tunable_mode_t mode; 209 210 if (strcmp(k, "show") == 0) { 211 mode = SHOW; 212 k = v; 213 } else if (strcmp(k, "info") == 0) { 214 mode = INFO; 215 k = v; 216 } else if (v == NULL) { 217 mode = SHOW; 218 } else { 219 mode = SET; 220 } 221 222 if (quiet && mode != SET) { 223 err = EINVAL; 224 goto out; 225 } 226 227 if (mode == SET) { 228 const zfs_tunable_t *tunable = zfs_tunable_lookup(k); 229 if (tunable == NULL) { 230 err = ENOENT; 231 goto out; 232 } 233 234 char vold[256], vnew[256]; 235 if (zfs_tunable_get(tunable, vold, sizeof (vold)) != 0) 236 strcpy(vold, "???"); 237 err = zfs_tunable_set(tunable, v); 238 if (err != 0) 239 goto out; 240 if (zfs_tunable_get(tunable, vnew, sizeof (vnew)) != 0) 241 strcpy(vnew, "???"); 242 243 if (!quiet) 244 printf("%s: %s -> %s\n", k, vold, vnew); 245 } else if (k != NULL) { 246 const zfs_tunable_t *tunable = zfs_tunable_lookup(k); 247 if (tunable == NULL) { 248 err = ENOENT; 249 goto out; 250 } 251 list_tunables_cb(tunable, &mode); 252 } else { 253 zfs_tunable_iter(list_tunables_cb, &mode); 254 } 255 256 out: 257 if (!quiet) { 258 if (err == ENOENT) 259 fprintf(stderr, "no such tunable: %s\n", k); 260 else if (err != 0) 261 fprintf(stderr, "couldn't set tunable '%s': %s\n", 262 k, strerror(err)); 263 } 264 265 free(arg); 266 return (err); 267 } 268 269 static nvlist_t * 270 refresh_config(void *unused, nvlist_t *tryconfig) 271 { 272 (void) unused; 273 return (spa_tryimport(tryconfig)); 274 } 275 276 #if defined(__FreeBSD__) 277 278 #include <sys/param.h> 279 #include <sys/sysctl.h> 280 #include <os/freebsd/zfs/sys/zfs_ioctl_compat.h> 281 282 static int 283 pool_active(void *unused, const char *name, uint64_t guid, boolean_t *isactive) 284 { 285 (void) unused, (void) guid; 286 zfs_iocparm_t zp; 287 zfs_cmd_t *zc = NULL; 288 #ifdef ZFS_LEGACY_SUPPORT 289 zfs_cmd_legacy_t *zcl = NULL; 290 #endif 291 unsigned long request; 292 int ret; 293 294 int fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC); 295 if (fd < 0) 296 return (-1); 297 298 /* 299 * Use ZFS_IOC_POOL_STATS to check if the pool is active. We want to 300 * avoid adding a dependency on libzfs_core solely for this ioctl(), 301 * therefore we manually craft the stats command. Note that the command 302 * ID is identical between the openzfs and legacy ioctl() formats. 303 */ 304 int ver = ZFS_IOCVER_NONE; 305 size_t ver_size = sizeof (ver); 306 307 sysctlbyname("vfs.zfs.version.ioctl", &ver, &ver_size, NULL, 0); 308 309 switch (ver) { 310 case ZFS_IOCVER_OZFS: 311 zc = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL); 312 313 (void) strlcpy(zc->zc_name, name, sizeof (zc->zc_name)); 314 zp.zfs_cmd = (uint64_t)(uintptr_t)zc; 315 zp.zfs_cmd_size = sizeof (zfs_cmd_t); 316 zp.zfs_ioctl_version = ZFS_IOCVER_OZFS; 317 318 request = _IOWR('Z', ZFS_IOC_POOL_STATS, zfs_iocparm_t); 319 ret = ioctl(fd, request, &zp); 320 321 free((void *)(uintptr_t)zc->zc_nvlist_dst); 322 umem_free(zc, sizeof (zfs_cmd_t)); 323 324 break; 325 #ifdef ZFS_LEGACY_SUPPORT 326 case ZFS_IOCVER_LEGACY: 327 zcl = umem_zalloc(sizeof (zfs_cmd_legacy_t), UMEM_NOFAIL); 328 329 (void) strlcpy(zcl->zc_name, name, sizeof (zcl->zc_name)); 330 zp.zfs_cmd = (uint64_t)(uintptr_t)zcl; 331 zp.zfs_cmd_size = sizeof (zfs_cmd_legacy_t); 332 zp.zfs_ioctl_version = ZFS_IOCVER_LEGACY; 333 334 request = _IOWR('Z', ZFS_IOC_POOL_STATS, zfs_iocparm_t); 335 ret = ioctl(fd, request, &zp); 336 337 free((void *)(uintptr_t)zcl->zc_nvlist_dst); 338 umem_free(zcl, sizeof (zfs_cmd_legacy_t)); 339 340 break; 341 #endif 342 default: 343 fprintf(stderr, "unrecognized zfs ioctl version %d", ver); 344 exit(1); 345 } 346 347 (void) close(fd); 348 349 *isactive = (ret == 0); 350 351 return (0); 352 } 353 #else 354 static int 355 pool_active(void *unused, const char *name, uint64_t guid, 356 boolean_t *isactive) 357 { 358 (void) unused, (void) guid; 359 int fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC); 360 if (fd < 0) 361 return (-1); 362 363 /* 364 * Use ZFS_IOC_POOL_STATS to check if a pool is active. 365 */ 366 zfs_cmd_t *zcp = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL); 367 (void) strlcpy(zcp->zc_name, name, sizeof (zcp->zc_name)); 368 369 int ret = ioctl(fd, ZFS_IOC_POOL_STATS, zcp); 370 371 free((void *)(uintptr_t)zcp->zc_nvlist_dst); 372 umem_free(zcp, sizeof (zfs_cmd_t)); 373 374 (void) close(fd); 375 376 *isactive = (ret == 0); 377 378 return (0); 379 } 380 #endif 381 382 pool_config_ops_t libzpool_config_ops = { 383 .pco_refresh_config = refresh_config, 384 .pco_pool_active = pool_active, 385 }; 386