1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 599653d4eSeschrock * Common Development and Distribution License (the "License"). 699653d4eSeschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 22b1b8ab34Slling * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25b327cd3fSIgor Kozhukhov /* 26b327cd3fSIgor Kozhukhov * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>. 27b327cd3fSIgor Kozhukhov */ 28fa9e4066Sahrens 29fa9e4066Sahrens #include <libintl.h> 30fa9e4066Sahrens #include <libuutil.h> 31fa9e4066Sahrens #include <stddef.h> 32fa9e4066Sahrens #include <stdio.h> 33fa9e4066Sahrens #include <stdlib.h> 34fa9e4066Sahrens #include <strings.h> 35*dd50e0ccSTony Hutter #include <sys/sysmacros.h> 36fa9e4066Sahrens 37fa9e4066Sahrens #include <libzfs.h> 38d8ab6e12SDon Brady #include <libzutil.h> 39fa9e4066Sahrens 40fa9e4066Sahrens #include "zpool_util.h" 41fa9e4066Sahrens 42fa9e4066Sahrens /* 43fa9e4066Sahrens * Private interface for iterating over pools specified on the command line. 44fa9e4066Sahrens * Most consumers will call for_each_pool, but in order to support iostat, we 45fa9e4066Sahrens * allow fined grained control through the zpool_list_t interface. 46fa9e4066Sahrens */ 47fa9e4066Sahrens 48fa9e4066Sahrens typedef struct zpool_node { 49fa9e4066Sahrens zpool_handle_t *zn_handle; 50fa9e4066Sahrens uu_avl_node_t zn_avlnode; 51fa9e4066Sahrens int zn_mark; 52fa9e4066Sahrens } zpool_node_t; 53fa9e4066Sahrens 54fa9e4066Sahrens struct zpool_list { 5599653d4eSeschrock boolean_t zl_findall; 56fa9e4066Sahrens uu_avl_t *zl_avl; 57fa9e4066Sahrens uu_avl_pool_t *zl_pool; 58990b4856Slling zprop_list_t **zl_proplist; 59fa9e4066Sahrens }; 60fa9e4066Sahrens 61fa9e4066Sahrens /* ARGSUSED */ 62fa9e4066Sahrens static int 63fa9e4066Sahrens zpool_compare(const void *larg, const void *rarg, void *unused) 64fa9e4066Sahrens { 65fa9e4066Sahrens zpool_handle_t *l = ((zpool_node_t *)larg)->zn_handle; 66fa9e4066Sahrens zpool_handle_t *r = ((zpool_node_t *)rarg)->zn_handle; 67fa9e4066Sahrens const char *lname = zpool_get_name(l); 68fa9e4066Sahrens const char *rname = zpool_get_name(r); 69fa9e4066Sahrens 70fa9e4066Sahrens return (strcmp(lname, rname)); 71fa9e4066Sahrens } 72fa9e4066Sahrens 73fa9e4066Sahrens /* 74fa9e4066Sahrens * Callback function for pool_list_get(). Adds the given pool to the AVL tree 75fa9e4066Sahrens * of known pools. 76fa9e4066Sahrens */ 77fa9e4066Sahrens static int 78fa9e4066Sahrens add_pool(zpool_handle_t *zhp, void *data) 79fa9e4066Sahrens { 80fa9e4066Sahrens zpool_list_t *zlp = data; 81fa9e4066Sahrens zpool_node_t *node = safe_malloc(sizeof (zpool_node_t)); 82fa9e4066Sahrens uu_avl_index_t idx; 83fa9e4066Sahrens 84fa9e4066Sahrens node->zn_handle = zhp; 85fa9e4066Sahrens uu_avl_node_init(node, &node->zn_avlnode, zlp->zl_pool); 86fa9e4066Sahrens if (uu_avl_find(zlp->zl_avl, node, NULL, &idx) == NULL) { 87990b4856Slling if (zlp->zl_proplist && 88990b4856Slling zpool_expand_proplist(zhp, zlp->zl_proplist) != 0) { 89990b4856Slling zpool_close(zhp); 90990b4856Slling free(node); 91990b4856Slling return (-1); 92990b4856Slling } 93fa9e4066Sahrens uu_avl_insert(zlp->zl_avl, node, idx); 94fa9e4066Sahrens } else { 95fa9e4066Sahrens zpool_close(zhp); 96fa9e4066Sahrens free(node); 97b1b8ab34Slling return (-1); 98fa9e4066Sahrens } 99fa9e4066Sahrens 100fa9e4066Sahrens return (0); 101fa9e4066Sahrens } 102fa9e4066Sahrens 103fa9e4066Sahrens /* 104fa9e4066Sahrens * Create a list of pools based on the given arguments. If we're given no 105fa9e4066Sahrens * arguments, then iterate over all pools in the system and add them to the AVL 106fa9e4066Sahrens * tree. Otherwise, add only those pool explicitly specified on the command 107fa9e4066Sahrens * line. 108fa9e4066Sahrens */ 109fa9e4066Sahrens zpool_list_t * 110990b4856Slling pool_list_get(int argc, char **argv, zprop_list_t **proplist, int *err) 111fa9e4066Sahrens { 112fa9e4066Sahrens zpool_list_t *zlp; 113fa9e4066Sahrens 114fa9e4066Sahrens zlp = safe_malloc(sizeof (zpool_list_t)); 115fa9e4066Sahrens 116fa9e4066Sahrens zlp->zl_pool = uu_avl_pool_create("zfs_pool", sizeof (zpool_node_t), 117fa9e4066Sahrens offsetof(zpool_node_t, zn_avlnode), zpool_compare, UU_DEFAULT); 118fa9e4066Sahrens 119fa9e4066Sahrens if (zlp->zl_pool == NULL) 1205ad82045Snd zpool_no_memory(); 121fa9e4066Sahrens 122fa9e4066Sahrens if ((zlp->zl_avl = uu_avl_create(zlp->zl_pool, NULL, 123fa9e4066Sahrens UU_DEFAULT)) == NULL) 1245ad82045Snd zpool_no_memory(); 125fa9e4066Sahrens 126990b4856Slling zlp->zl_proplist = proplist; 127990b4856Slling 128fa9e4066Sahrens if (argc == 0) { 12999653d4eSeschrock (void) zpool_iter(g_zfs, add_pool, zlp); 13099653d4eSeschrock zlp->zl_findall = B_TRUE; 131fa9e4066Sahrens } else { 132fa9e4066Sahrens int i; 133fa9e4066Sahrens 134fa9e4066Sahrens for (i = 0; i < argc; i++) { 135fa9e4066Sahrens zpool_handle_t *zhp; 136fa9e4066Sahrens 137b327cd3fSIgor Kozhukhov if ((zhp = zpool_open_canfail(g_zfs, argv[i])) != 138b327cd3fSIgor Kozhukhov NULL) { 139990b4856Slling if (add_pool(zhp, zlp) != 0) 140b1b8ab34Slling *err = B_TRUE; 141990b4856Slling } else { 14299653d4eSeschrock *err = B_TRUE; 143990b4856Slling } 144fa9e4066Sahrens } 145fa9e4066Sahrens } 146fa9e4066Sahrens 147fa9e4066Sahrens return (zlp); 148fa9e4066Sahrens } 149fa9e4066Sahrens 150fa9e4066Sahrens /* 151fa9e4066Sahrens * Search for any new pools, adding them to the list. We only add pools when no 152fa9e4066Sahrens * options were given on the command line. Otherwise, we keep the list fixed as 153fa9e4066Sahrens * those that were explicitly specified. 154fa9e4066Sahrens */ 155fa9e4066Sahrens void 156fa9e4066Sahrens pool_list_update(zpool_list_t *zlp) 157fa9e4066Sahrens { 158fa9e4066Sahrens if (zlp->zl_findall) 15999653d4eSeschrock (void) zpool_iter(g_zfs, add_pool, zlp); 160fa9e4066Sahrens } 161fa9e4066Sahrens 162fa9e4066Sahrens /* 163fa9e4066Sahrens * Iterate over all pools in the list, executing the callback for each 164fa9e4066Sahrens */ 165fa9e4066Sahrens int 166fa9e4066Sahrens pool_list_iter(zpool_list_t *zlp, int unavail, zpool_iter_f func, 167fa9e4066Sahrens void *data) 168fa9e4066Sahrens { 169fa9e4066Sahrens zpool_node_t *node, *next_node; 170fa9e4066Sahrens int ret = 0; 171fa9e4066Sahrens 172fa9e4066Sahrens for (node = uu_avl_first(zlp->zl_avl); node != NULL; node = next_node) { 173fa9e4066Sahrens next_node = uu_avl_next(zlp->zl_avl, node); 174fa9e4066Sahrens if (zpool_get_state(node->zn_handle) != POOL_STATE_UNAVAIL || 175fa9e4066Sahrens unavail) 176fa9e4066Sahrens ret |= func(node->zn_handle, data); 177fa9e4066Sahrens } 178fa9e4066Sahrens 179fa9e4066Sahrens return (ret); 180fa9e4066Sahrens } 181fa9e4066Sahrens 182fa9e4066Sahrens /* 183fa9e4066Sahrens * Remove the given pool from the list. When running iostat, we want to remove 184fa9e4066Sahrens * those pools that no longer exist. 185fa9e4066Sahrens */ 186fa9e4066Sahrens void 187fa9e4066Sahrens pool_list_remove(zpool_list_t *zlp, zpool_handle_t *zhp) 188fa9e4066Sahrens { 189fa9e4066Sahrens zpool_node_t search, *node; 190fa9e4066Sahrens 191fa9e4066Sahrens search.zn_handle = zhp; 192fa9e4066Sahrens if ((node = uu_avl_find(zlp->zl_avl, &search, NULL, NULL)) != NULL) { 193fa9e4066Sahrens uu_avl_remove(zlp->zl_avl, node); 194fa9e4066Sahrens zpool_close(node->zn_handle); 195fa9e4066Sahrens free(node); 196fa9e4066Sahrens } 197fa9e4066Sahrens } 198fa9e4066Sahrens 199fa9e4066Sahrens /* 200fa9e4066Sahrens * Free all the handles associated with this list. 201fa9e4066Sahrens */ 202fa9e4066Sahrens void 203fa9e4066Sahrens pool_list_free(zpool_list_t *zlp) 204fa9e4066Sahrens { 205fa9e4066Sahrens uu_avl_walk_t *walk; 206fa9e4066Sahrens zpool_node_t *node; 207fa9e4066Sahrens 208fa9e4066Sahrens if ((walk = uu_avl_walk_start(zlp->zl_avl, UU_WALK_ROBUST)) == NULL) { 209fa9e4066Sahrens (void) fprintf(stderr, 210fa9e4066Sahrens gettext("internal error: out of memory")); 211fa9e4066Sahrens exit(1); 212fa9e4066Sahrens } 213fa9e4066Sahrens 214fa9e4066Sahrens while ((node = uu_avl_walk_next(walk)) != NULL) { 215fa9e4066Sahrens uu_avl_remove(zlp->zl_avl, node); 216fa9e4066Sahrens zpool_close(node->zn_handle); 217fa9e4066Sahrens free(node); 218fa9e4066Sahrens } 219fa9e4066Sahrens 220fa9e4066Sahrens uu_avl_walk_end(walk); 221fa9e4066Sahrens uu_avl_destroy(zlp->zl_avl); 222fa9e4066Sahrens uu_avl_pool_destroy(zlp->zl_pool); 223fa9e4066Sahrens 224fa9e4066Sahrens free(zlp); 225fa9e4066Sahrens } 226fa9e4066Sahrens 227fa9e4066Sahrens /* 228fa9e4066Sahrens * Returns the number of elements in the pool list. 229fa9e4066Sahrens */ 230fa9e4066Sahrens int 231fa9e4066Sahrens pool_list_count(zpool_list_t *zlp) 232fa9e4066Sahrens { 233fa9e4066Sahrens return (uu_avl_numnodes(zlp->zl_avl)); 234fa9e4066Sahrens } 235fa9e4066Sahrens 236fa9e4066Sahrens /* 237fa9e4066Sahrens * High level function which iterates over all pools given on the command line, 238fa9e4066Sahrens * using the pool_list_* interfaces. 239fa9e4066Sahrens */ 240fa9e4066Sahrens int 241b1b8ab34Slling for_each_pool(int argc, char **argv, boolean_t unavail, 242990b4856Slling zprop_list_t **proplist, zpool_iter_f func, void *data) 243fa9e4066Sahrens { 244fa9e4066Sahrens zpool_list_t *list; 245fa9e4066Sahrens int ret = 0; 246fa9e4066Sahrens 247b1b8ab34Slling if ((list = pool_list_get(argc, argv, proplist, &ret)) == NULL) 248fa9e4066Sahrens return (1); 249fa9e4066Sahrens 250fa9e4066Sahrens if (pool_list_iter(list, unavail, func, data) != 0) 251fa9e4066Sahrens ret = 1; 252fa9e4066Sahrens 253fa9e4066Sahrens pool_list_free(list); 254fa9e4066Sahrens 255fa9e4066Sahrens return (ret); 256fa9e4066Sahrens } 257*dd50e0ccSTony Hutter 258*dd50e0ccSTony Hutter static int 259*dd50e0ccSTony Hutter for_each_vdev_cb(zpool_handle_t *zhp, nvlist_t *nv, pool_vdev_iter_f func, 260*dd50e0ccSTony Hutter void *data) 261*dd50e0ccSTony Hutter { 262*dd50e0ccSTony Hutter nvlist_t **child; 263*dd50e0ccSTony Hutter uint_t c, children; 264*dd50e0ccSTony Hutter int ret = 0; 265*dd50e0ccSTony Hutter int i; 266*dd50e0ccSTony Hutter char *type; 267*dd50e0ccSTony Hutter 268*dd50e0ccSTony Hutter const char *list[] = { 269*dd50e0ccSTony Hutter ZPOOL_CONFIG_SPARES, 270*dd50e0ccSTony Hutter ZPOOL_CONFIG_L2CACHE, 271*dd50e0ccSTony Hutter ZPOOL_CONFIG_CHILDREN 272*dd50e0ccSTony Hutter }; 273*dd50e0ccSTony Hutter 274*dd50e0ccSTony Hutter for (i = 0; i < ARRAY_SIZE(list); i++) { 275*dd50e0ccSTony Hutter if (nvlist_lookup_nvlist_array(nv, list[i], &child, 276*dd50e0ccSTony Hutter &children) == 0) { 277*dd50e0ccSTony Hutter for (c = 0; c < children; c++) { 278*dd50e0ccSTony Hutter uint64_t ishole = 0; 279*dd50e0ccSTony Hutter 280*dd50e0ccSTony Hutter (void) nvlist_lookup_uint64(child[c], 281*dd50e0ccSTony Hutter ZPOOL_CONFIG_IS_HOLE, &ishole); 282*dd50e0ccSTony Hutter 283*dd50e0ccSTony Hutter if (ishole) 284*dd50e0ccSTony Hutter continue; 285*dd50e0ccSTony Hutter 286*dd50e0ccSTony Hutter ret |= for_each_vdev_cb(zhp, child[c], func, 287*dd50e0ccSTony Hutter data); 288*dd50e0ccSTony Hutter } 289*dd50e0ccSTony Hutter } 290*dd50e0ccSTony Hutter } 291*dd50e0ccSTony Hutter 292*dd50e0ccSTony Hutter if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 293*dd50e0ccSTony Hutter return (ret); 294*dd50e0ccSTony Hutter 295*dd50e0ccSTony Hutter /* Don't run our function on root vdevs */ 296*dd50e0ccSTony Hutter if (strcmp(type, VDEV_TYPE_ROOT) != 0) { 297*dd50e0ccSTony Hutter ret |= func(zhp, nv, data); 298*dd50e0ccSTony Hutter } 299*dd50e0ccSTony Hutter 300*dd50e0ccSTony Hutter return (ret); 301*dd50e0ccSTony Hutter } 302*dd50e0ccSTony Hutter 303*dd50e0ccSTony Hutter /* 304*dd50e0ccSTony Hutter * This is the equivalent of for_each_pool() for vdevs. It iterates thorough 305*dd50e0ccSTony Hutter * all vdevs in the pool, ignoring root vdevs and holes, calling func() on 306*dd50e0ccSTony Hutter * each one. 307*dd50e0ccSTony Hutter * 308*dd50e0ccSTony Hutter * @zhp: Zpool handle 309*dd50e0ccSTony Hutter * @func: Function to call on each vdev 310*dd50e0ccSTony Hutter * @data: Custom data to pass to the function 311*dd50e0ccSTony Hutter */ 312*dd50e0ccSTony Hutter int 313*dd50e0ccSTony Hutter for_each_vdev(zpool_handle_t *zhp, pool_vdev_iter_f func, void *data) 314*dd50e0ccSTony Hutter { 315*dd50e0ccSTony Hutter nvlist_t *config, *nvroot; 316*dd50e0ccSTony Hutter 317*dd50e0ccSTony Hutter if ((config = zpool_get_config(zhp, NULL)) != NULL) { 318*dd50e0ccSTony Hutter verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 319*dd50e0ccSTony Hutter &nvroot) == 0); 320*dd50e0ccSTony Hutter } 321*dd50e0ccSTony Hutter return (for_each_vdev_cb(zhp, nvroot, func, data)); 322*dd50e0ccSTony Hutter } 323