1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5*99653d4eSeschrock * Common Development and Distribution License (the "License"). 6*99653d4eSeschrock * 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 /* 22*99653d4eSeschrock * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27fa9e4066Sahrens 28fa9e4066Sahrens #include <libintl.h> 29fa9e4066Sahrens #include <libuutil.h> 30fa9e4066Sahrens #include <stddef.h> 31fa9e4066Sahrens #include <stdio.h> 32fa9e4066Sahrens #include <stdlib.h> 33fa9e4066Sahrens #include <strings.h> 34fa9e4066Sahrens 35fa9e4066Sahrens #include <libzfs.h> 36fa9e4066Sahrens 37fa9e4066Sahrens #include "zpool_util.h" 38fa9e4066Sahrens 39fa9e4066Sahrens /* 40fa9e4066Sahrens * Private interface for iterating over pools specified on the command line. 41fa9e4066Sahrens * Most consumers will call for_each_pool, but in order to support iostat, we 42fa9e4066Sahrens * allow fined grained control through the zpool_list_t interface. 43fa9e4066Sahrens */ 44fa9e4066Sahrens 45fa9e4066Sahrens typedef struct zpool_node { 46fa9e4066Sahrens zpool_handle_t *zn_handle; 47fa9e4066Sahrens uu_avl_node_t zn_avlnode; 48fa9e4066Sahrens int zn_mark; 49fa9e4066Sahrens } zpool_node_t; 50fa9e4066Sahrens 51fa9e4066Sahrens struct zpool_list { 52*99653d4eSeschrock boolean_t zl_findall; 53fa9e4066Sahrens uu_avl_t *zl_avl; 54fa9e4066Sahrens uu_avl_pool_t *zl_pool; 55fa9e4066Sahrens }; 56fa9e4066Sahrens 57fa9e4066Sahrens /* ARGSUSED */ 58fa9e4066Sahrens static int 59fa9e4066Sahrens zpool_compare(const void *larg, const void *rarg, void *unused) 60fa9e4066Sahrens { 61fa9e4066Sahrens zpool_handle_t *l = ((zpool_node_t *)larg)->zn_handle; 62fa9e4066Sahrens zpool_handle_t *r = ((zpool_node_t *)rarg)->zn_handle; 63fa9e4066Sahrens const char *lname = zpool_get_name(l); 64fa9e4066Sahrens const char *rname = zpool_get_name(r); 65fa9e4066Sahrens 66fa9e4066Sahrens return (strcmp(lname, rname)); 67fa9e4066Sahrens } 68fa9e4066Sahrens 69fa9e4066Sahrens /* 70fa9e4066Sahrens * Callback function for pool_list_get(). Adds the given pool to the AVL tree 71fa9e4066Sahrens * of known pools. 72fa9e4066Sahrens */ 73fa9e4066Sahrens static int 74fa9e4066Sahrens add_pool(zpool_handle_t *zhp, void *data) 75fa9e4066Sahrens { 76fa9e4066Sahrens zpool_list_t *zlp = data; 77fa9e4066Sahrens zpool_node_t *node = safe_malloc(sizeof (zpool_node_t)); 78fa9e4066Sahrens uu_avl_index_t idx; 79fa9e4066Sahrens 80fa9e4066Sahrens node->zn_handle = zhp; 81fa9e4066Sahrens uu_avl_node_init(node, &node->zn_avlnode, zlp->zl_pool); 82fa9e4066Sahrens if (uu_avl_find(zlp->zl_avl, node, NULL, &idx) == NULL) { 83fa9e4066Sahrens uu_avl_insert(zlp->zl_avl, node, idx); 84fa9e4066Sahrens } else { 85fa9e4066Sahrens zpool_close(zhp); 86fa9e4066Sahrens free(node); 87fa9e4066Sahrens } 88fa9e4066Sahrens 89fa9e4066Sahrens return (0); 90fa9e4066Sahrens } 91fa9e4066Sahrens 92fa9e4066Sahrens /* 93fa9e4066Sahrens * Create a list of pools based on the given arguments. If we're given no 94fa9e4066Sahrens * arguments, then iterate over all pools in the system and add them to the AVL 95fa9e4066Sahrens * tree. Otherwise, add only those pool explicitly specified on the command 96fa9e4066Sahrens * line. 97fa9e4066Sahrens */ 98fa9e4066Sahrens zpool_list_t * 99fa9e4066Sahrens pool_list_get(int argc, char **argv, int *err) 100fa9e4066Sahrens { 101fa9e4066Sahrens zpool_list_t *zlp; 102fa9e4066Sahrens 103fa9e4066Sahrens zlp = safe_malloc(sizeof (zpool_list_t)); 104fa9e4066Sahrens 105fa9e4066Sahrens zlp->zl_pool = uu_avl_pool_create("zfs_pool", sizeof (zpool_node_t), 106fa9e4066Sahrens offsetof(zpool_node_t, zn_avlnode), zpool_compare, UU_DEFAULT); 107fa9e4066Sahrens 108fa9e4066Sahrens if (zlp->zl_pool == NULL) 109fa9e4066Sahrens no_memory(); 110fa9e4066Sahrens 111fa9e4066Sahrens if ((zlp->zl_avl = uu_avl_create(zlp->zl_pool, NULL, 112fa9e4066Sahrens UU_DEFAULT)) == NULL) 113fa9e4066Sahrens no_memory(); 114fa9e4066Sahrens 115fa9e4066Sahrens if (argc == 0) { 116*99653d4eSeschrock (void) zpool_iter(g_zfs, add_pool, zlp); 117*99653d4eSeschrock zlp->zl_findall = B_TRUE; 118fa9e4066Sahrens } else { 119fa9e4066Sahrens int i; 120fa9e4066Sahrens 121fa9e4066Sahrens for (i = 0; i < argc; i++) { 122fa9e4066Sahrens zpool_handle_t *zhp; 123fa9e4066Sahrens 124*99653d4eSeschrock if ((zhp = zpool_open_canfail(g_zfs, argv[i])) != NULL) 125fa9e4066Sahrens (void) add_pool(zhp, zlp); 126fa9e4066Sahrens else 127*99653d4eSeschrock *err = B_TRUE; 128fa9e4066Sahrens } 129fa9e4066Sahrens } 130fa9e4066Sahrens 131fa9e4066Sahrens return (zlp); 132fa9e4066Sahrens } 133fa9e4066Sahrens 134fa9e4066Sahrens /* 135fa9e4066Sahrens * Search for any new pools, adding them to the list. We only add pools when no 136fa9e4066Sahrens * options were given on the command line. Otherwise, we keep the list fixed as 137fa9e4066Sahrens * those that were explicitly specified. 138fa9e4066Sahrens */ 139fa9e4066Sahrens void 140fa9e4066Sahrens pool_list_update(zpool_list_t *zlp) 141fa9e4066Sahrens { 142fa9e4066Sahrens if (zlp->zl_findall) 143*99653d4eSeschrock (void) zpool_iter(g_zfs, add_pool, zlp); 144fa9e4066Sahrens } 145fa9e4066Sahrens 146fa9e4066Sahrens /* 147fa9e4066Sahrens * Iterate over all pools in the list, executing the callback for each 148fa9e4066Sahrens */ 149fa9e4066Sahrens int 150fa9e4066Sahrens pool_list_iter(zpool_list_t *zlp, int unavail, zpool_iter_f func, 151fa9e4066Sahrens void *data) 152fa9e4066Sahrens { 153fa9e4066Sahrens zpool_node_t *node, *next_node; 154fa9e4066Sahrens int ret = 0; 155fa9e4066Sahrens 156fa9e4066Sahrens for (node = uu_avl_first(zlp->zl_avl); node != NULL; node = next_node) { 157fa9e4066Sahrens next_node = uu_avl_next(zlp->zl_avl, node); 158fa9e4066Sahrens if (zpool_get_state(node->zn_handle) != POOL_STATE_UNAVAIL || 159fa9e4066Sahrens unavail) 160fa9e4066Sahrens ret |= func(node->zn_handle, data); 161fa9e4066Sahrens } 162fa9e4066Sahrens 163fa9e4066Sahrens return (ret); 164fa9e4066Sahrens } 165fa9e4066Sahrens 166fa9e4066Sahrens /* 167fa9e4066Sahrens * Remove the given pool from the list. When running iostat, we want to remove 168fa9e4066Sahrens * those pools that no longer exist. 169fa9e4066Sahrens */ 170fa9e4066Sahrens void 171fa9e4066Sahrens pool_list_remove(zpool_list_t *zlp, zpool_handle_t *zhp) 172fa9e4066Sahrens { 173fa9e4066Sahrens zpool_node_t search, *node; 174fa9e4066Sahrens 175fa9e4066Sahrens search.zn_handle = zhp; 176fa9e4066Sahrens if ((node = uu_avl_find(zlp->zl_avl, &search, NULL, NULL)) != NULL) { 177fa9e4066Sahrens uu_avl_remove(zlp->zl_avl, node); 178fa9e4066Sahrens zpool_close(node->zn_handle); 179fa9e4066Sahrens free(node); 180fa9e4066Sahrens } 181fa9e4066Sahrens } 182fa9e4066Sahrens 183fa9e4066Sahrens /* 184fa9e4066Sahrens * Free all the handles associated with this list. 185fa9e4066Sahrens */ 186fa9e4066Sahrens void 187fa9e4066Sahrens pool_list_free(zpool_list_t *zlp) 188fa9e4066Sahrens { 189fa9e4066Sahrens uu_avl_walk_t *walk; 190fa9e4066Sahrens zpool_node_t *node; 191fa9e4066Sahrens 192fa9e4066Sahrens if ((walk = uu_avl_walk_start(zlp->zl_avl, UU_WALK_ROBUST)) == NULL) { 193fa9e4066Sahrens (void) fprintf(stderr, 194fa9e4066Sahrens gettext("internal error: out of memory")); 195fa9e4066Sahrens exit(1); 196fa9e4066Sahrens } 197fa9e4066Sahrens 198fa9e4066Sahrens while ((node = uu_avl_walk_next(walk)) != NULL) { 199fa9e4066Sahrens uu_avl_remove(zlp->zl_avl, node); 200fa9e4066Sahrens zpool_close(node->zn_handle); 201fa9e4066Sahrens free(node); 202fa9e4066Sahrens } 203fa9e4066Sahrens 204fa9e4066Sahrens uu_avl_walk_end(walk); 205fa9e4066Sahrens uu_avl_destroy(zlp->zl_avl); 206fa9e4066Sahrens uu_avl_pool_destroy(zlp->zl_pool); 207fa9e4066Sahrens 208fa9e4066Sahrens free(zlp); 209fa9e4066Sahrens } 210fa9e4066Sahrens 211fa9e4066Sahrens /* 212fa9e4066Sahrens * Returns the number of elements in the pool list. 213fa9e4066Sahrens */ 214fa9e4066Sahrens int 215fa9e4066Sahrens pool_list_count(zpool_list_t *zlp) 216fa9e4066Sahrens { 217fa9e4066Sahrens return (uu_avl_numnodes(zlp->zl_avl)); 218fa9e4066Sahrens } 219fa9e4066Sahrens 220fa9e4066Sahrens /* 221fa9e4066Sahrens * High level function which iterates over all pools given on the command line, 222fa9e4066Sahrens * using the pool_list_* interfaces. 223fa9e4066Sahrens */ 224fa9e4066Sahrens int 225*99653d4eSeschrock for_each_pool(int argc, char **argv, boolean_t unavail, zpool_iter_f func, 226fa9e4066Sahrens void *data) 227fa9e4066Sahrens { 228fa9e4066Sahrens zpool_list_t *list; 229fa9e4066Sahrens int ret = 0; 230fa9e4066Sahrens 231fa9e4066Sahrens if ((list = pool_list_get(argc, argv, &ret)) == NULL) 232fa9e4066Sahrens return (1); 233fa9e4066Sahrens 234fa9e4066Sahrens if (pool_list_iter(list, unavail, func, data) != 0) 235fa9e4066Sahrens ret = 1; 236fa9e4066Sahrens 237fa9e4066Sahrens pool_list_free(list); 238fa9e4066Sahrens 239fa9e4066Sahrens return (ret); 240fa9e4066Sahrens } 241