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 /* 22*b1b8ab34Slling * Copyright 2007 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 { 5299653d4eSeschrock 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); 87*b1b8ab34Slling return (-1); 88fa9e4066Sahrens } 89fa9e4066Sahrens 90fa9e4066Sahrens return (0); 91fa9e4066Sahrens } 92fa9e4066Sahrens 93fa9e4066Sahrens /* 94fa9e4066Sahrens * Create a list of pools based on the given arguments. If we're given no 95fa9e4066Sahrens * arguments, then iterate over all pools in the system and add them to the AVL 96fa9e4066Sahrens * tree. Otherwise, add only those pool explicitly specified on the command 97fa9e4066Sahrens * line. 98fa9e4066Sahrens */ 99fa9e4066Sahrens zpool_list_t * 100*b1b8ab34Slling pool_list_get(int argc, char **argv, zpool_proplist_t **proplist, int *err) 101fa9e4066Sahrens { 102fa9e4066Sahrens zpool_list_t *zlp; 103fa9e4066Sahrens 104fa9e4066Sahrens zlp = safe_malloc(sizeof (zpool_list_t)); 105fa9e4066Sahrens 106fa9e4066Sahrens zlp->zl_pool = uu_avl_pool_create("zfs_pool", sizeof (zpool_node_t), 107fa9e4066Sahrens offsetof(zpool_node_t, zn_avlnode), zpool_compare, UU_DEFAULT); 108fa9e4066Sahrens 109fa9e4066Sahrens if (zlp->zl_pool == NULL) 1105ad82045Snd150628 zpool_no_memory(); 111fa9e4066Sahrens 112fa9e4066Sahrens if ((zlp->zl_avl = uu_avl_create(zlp->zl_pool, NULL, 113fa9e4066Sahrens UU_DEFAULT)) == NULL) 1145ad82045Snd150628 zpool_no_memory(); 115fa9e4066Sahrens 116fa9e4066Sahrens if (argc == 0) { 11799653d4eSeschrock (void) zpool_iter(g_zfs, add_pool, zlp); 11899653d4eSeschrock zlp->zl_findall = B_TRUE; 119fa9e4066Sahrens } else { 120fa9e4066Sahrens int i; 121fa9e4066Sahrens 122fa9e4066Sahrens for (i = 0; i < argc; i++) { 123fa9e4066Sahrens zpool_handle_t *zhp; 124fa9e4066Sahrens 125*b1b8ab34Slling if ((zhp = zpool_open_canfail(g_zfs, 126*b1b8ab34Slling argv[i])) != NULL && add_pool(zhp, zlp) == 0) { 127*b1b8ab34Slling if (proplist && 128*b1b8ab34Slling zpool_expand_proplist(zhp, proplist) != 0) 129*b1b8ab34Slling *err = B_TRUE; 130*b1b8ab34Slling } else 13199653d4eSeschrock *err = B_TRUE; 132fa9e4066Sahrens } 133fa9e4066Sahrens } 134fa9e4066Sahrens 135fa9e4066Sahrens return (zlp); 136fa9e4066Sahrens } 137fa9e4066Sahrens 138fa9e4066Sahrens /* 139fa9e4066Sahrens * Search for any new pools, adding them to the list. We only add pools when no 140fa9e4066Sahrens * options were given on the command line. Otherwise, we keep the list fixed as 141fa9e4066Sahrens * those that were explicitly specified. 142fa9e4066Sahrens */ 143fa9e4066Sahrens void 144fa9e4066Sahrens pool_list_update(zpool_list_t *zlp) 145fa9e4066Sahrens { 146fa9e4066Sahrens if (zlp->zl_findall) 14799653d4eSeschrock (void) zpool_iter(g_zfs, add_pool, zlp); 148fa9e4066Sahrens } 149fa9e4066Sahrens 150fa9e4066Sahrens /* 151fa9e4066Sahrens * Iterate over all pools in the list, executing the callback for each 152fa9e4066Sahrens */ 153fa9e4066Sahrens int 154fa9e4066Sahrens pool_list_iter(zpool_list_t *zlp, int unavail, zpool_iter_f func, 155fa9e4066Sahrens void *data) 156fa9e4066Sahrens { 157fa9e4066Sahrens zpool_node_t *node, *next_node; 158fa9e4066Sahrens int ret = 0; 159fa9e4066Sahrens 160fa9e4066Sahrens for (node = uu_avl_first(zlp->zl_avl); node != NULL; node = next_node) { 161fa9e4066Sahrens next_node = uu_avl_next(zlp->zl_avl, node); 162fa9e4066Sahrens if (zpool_get_state(node->zn_handle) != POOL_STATE_UNAVAIL || 163fa9e4066Sahrens unavail) 164fa9e4066Sahrens ret |= func(node->zn_handle, data); 165fa9e4066Sahrens } 166fa9e4066Sahrens 167fa9e4066Sahrens return (ret); 168fa9e4066Sahrens } 169fa9e4066Sahrens 170fa9e4066Sahrens /* 171fa9e4066Sahrens * Remove the given pool from the list. When running iostat, we want to remove 172fa9e4066Sahrens * those pools that no longer exist. 173fa9e4066Sahrens */ 174fa9e4066Sahrens void 175fa9e4066Sahrens pool_list_remove(zpool_list_t *zlp, zpool_handle_t *zhp) 176fa9e4066Sahrens { 177fa9e4066Sahrens zpool_node_t search, *node; 178fa9e4066Sahrens 179fa9e4066Sahrens search.zn_handle = zhp; 180fa9e4066Sahrens if ((node = uu_avl_find(zlp->zl_avl, &search, NULL, NULL)) != NULL) { 181fa9e4066Sahrens uu_avl_remove(zlp->zl_avl, node); 182fa9e4066Sahrens zpool_close(node->zn_handle); 183fa9e4066Sahrens free(node); 184fa9e4066Sahrens } 185fa9e4066Sahrens } 186fa9e4066Sahrens 187fa9e4066Sahrens /* 188fa9e4066Sahrens * Free all the handles associated with this list. 189fa9e4066Sahrens */ 190fa9e4066Sahrens void 191fa9e4066Sahrens pool_list_free(zpool_list_t *zlp) 192fa9e4066Sahrens { 193fa9e4066Sahrens uu_avl_walk_t *walk; 194fa9e4066Sahrens zpool_node_t *node; 195fa9e4066Sahrens 196fa9e4066Sahrens if ((walk = uu_avl_walk_start(zlp->zl_avl, UU_WALK_ROBUST)) == NULL) { 197fa9e4066Sahrens (void) fprintf(stderr, 198fa9e4066Sahrens gettext("internal error: out of memory")); 199fa9e4066Sahrens exit(1); 200fa9e4066Sahrens } 201fa9e4066Sahrens 202fa9e4066Sahrens while ((node = uu_avl_walk_next(walk)) != NULL) { 203fa9e4066Sahrens uu_avl_remove(zlp->zl_avl, node); 204fa9e4066Sahrens zpool_close(node->zn_handle); 205fa9e4066Sahrens free(node); 206fa9e4066Sahrens } 207fa9e4066Sahrens 208fa9e4066Sahrens uu_avl_walk_end(walk); 209fa9e4066Sahrens uu_avl_destroy(zlp->zl_avl); 210fa9e4066Sahrens uu_avl_pool_destroy(zlp->zl_pool); 211fa9e4066Sahrens 212fa9e4066Sahrens free(zlp); 213fa9e4066Sahrens } 214fa9e4066Sahrens 215fa9e4066Sahrens /* 216fa9e4066Sahrens * Returns the number of elements in the pool list. 217fa9e4066Sahrens */ 218fa9e4066Sahrens int 219fa9e4066Sahrens pool_list_count(zpool_list_t *zlp) 220fa9e4066Sahrens { 221fa9e4066Sahrens return (uu_avl_numnodes(zlp->zl_avl)); 222fa9e4066Sahrens } 223fa9e4066Sahrens 224fa9e4066Sahrens /* 225fa9e4066Sahrens * High level function which iterates over all pools given on the command line, 226fa9e4066Sahrens * using the pool_list_* interfaces. 227fa9e4066Sahrens */ 228fa9e4066Sahrens int 229*b1b8ab34Slling for_each_pool(int argc, char **argv, boolean_t unavail, 230*b1b8ab34Slling zpool_proplist_t **proplist, zpool_iter_f func, void *data) 231fa9e4066Sahrens { 232fa9e4066Sahrens zpool_list_t *list; 233fa9e4066Sahrens int ret = 0; 234fa9e4066Sahrens 235*b1b8ab34Slling if ((list = pool_list_get(argc, argv, proplist, &ret)) == NULL) 236fa9e4066Sahrens return (1); 237fa9e4066Sahrens 238fa9e4066Sahrens if (pool_list_iter(list, unavail, func, data) != 0) 239fa9e4066Sahrens ret = 1; 240fa9e4066Sahrens 241fa9e4066Sahrens pool_list_free(list); 242fa9e4066Sahrens 243fa9e4066Sahrens return (ret); 244fa9e4066Sahrens } 245