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