Ejemplo n.º 1
0
/*
 * Iterate over root datasets, calling the given function for each.  The zfs
 * handle passed each time must be explicitly closed by the callback.
 */
int
zfs_iter_root(libzfs_handle_t *hdl, zfs_iter_f func, void *data)
{
	config_node_t *cn;
	zfs_handle_t *zhp;
	int ret;

	if (namespace_reload(hdl) != 0)
		return (-1);

	for (cn = uu_avl_first(hdl->libzfs_ns_avl); cn != NULL;
	    cn = uu_avl_next(hdl->libzfs_ns_avl, cn)) {

		if (check_restricted(cn->cn_name))
			continue;

		if ((zhp = make_dataset_handle(hdl, cn->cn_name)) == NULL)
			continue;

		if ((ret = func(zhp, data)) != 0)
			return (ret);
	}

	return (0);
}
Ejemplo n.º 2
0
/**
 * Iterate over root datasets
 * @param p_libzfshd: the libzfs handle
 * @param func: the function to call for each zfs
 * @param data: anonymous data to pass along to the callback function
 * @param ppsz_error: the error message if any
 * @return 0 in case of success, the error code overwise
 */
int libzfs_zfs_iter(libzfs_handle_t *p_libzfshd, zfs_iter_f func, void *data, const char **ppsz_error)
{
        config_node_t *p_cn;
        zfs_handle_t *p_zfs;
        int i_error;

        if(namespace_reload(p_libzfshd))
        {
                *ppsz_error = "Unable to reload the namespace";
                return 1;
        }

        for(p_cn = uu_avl_first(p_libzfshd->libzfs_ns_avl); p_cn;
            p_cn = uu_avl_next(p_libzfshd->libzfs_ns_avl, p_cn))
        {
                if(!(p_zfs = libzfs_make_dataset_handle(p_libzfshd, p_cn->cn_name)))
                {
                        *ppsz_error = "Unable to create the zfs_handle for the zfs object";
                        return 1;
                }
                if((i_error = func(p_zfs, data)))
                {
                        *ppsz_error = "Error in the callback function";
                        libzfs_zfs_close(p_zfs);
                        return i_error;
                }
                libzfs_zfs_close(p_zfs);
        }

        return 0;
}
Ejemplo n.º 3
0
/**
 * Iterate over the zpools
 * @param p_libzfshd: the libzfs handle
 * @param func: the function to call for each zpool
 * @param data: anonymous data to pass along to the callback function
 * @param ppsz_error: the error message if any
 * @return 0 in case of success, the error code overwise
 */
int libzfs_zpool_iter(libzfs_handle_t *p_libzfshd, zpool_iter_f func, void *data, const char **ppsz_error)
{
        config_node_t *p_config_node;

        /*
         * If someone makes a recursive call to zpool_iter(), we want to avoid
         * refreshing the namespace because that will invalidate the parent
         * context.  We allow recursive calls, but simply re-use the same
         * namespace AVL tree.
         */
        if(!p_libzfshd->libzfs_pool_iter && namespace_reload(p_libzfshd))
        {
                *ppsz_error = "unable to reload the namespace";
                return -1;
        }

        p_libzfshd->libzfs_pool_iter++;
        for(p_config_node = uu_avl_first(p_libzfshd->libzfs_ns_avl);
            p_config_node;
            p_config_node = uu_avl_next(p_libzfshd->libzfs_ns_avl, p_config_node))
        {
                zpool_handle_t *p_zpool = libzfs_zpool_open_canfail(p_libzfshd,
                                                p_config_node->cn_name, ppsz_error);

                if(!p_zpool)
                        continue;

                /* Call the callback function: it might return 0 */
                int i_ret = func(p_zpool, data);
                libzfs_zpool_close(p_zpool);
                if(i_ret)
                {
                        *ppsz_error = "error when calling the callback function";
                        p_libzfshd->libzfs_pool_iter--;
                        return i_ret;
                }
        }
        p_libzfshd->libzfs_pool_iter--;

        return 0;
}
Ejemplo n.º 4
0
/*
 * Iterate over all pools in the system.
 */
int
zpool_iter(libzfs_handle_t *hdl, zpool_iter_f func, void *data)
{
	config_node_t *cn;
	zpool_handle_t *zhp;
	int ret;

	/*
	 * If someone makes a recursive call to zpool_iter(), we want to avoid
	 * refreshing the namespace because that will invalidate the parent
	 * context.  We allow recursive calls, but simply re-use the same
	 * namespace AVL tree.
	 */
	if (!hdl->libzfs_pool_iter && namespace_reload(hdl) != 0)
		return (-1);

	hdl->libzfs_pool_iter++;
	for (cn = uu_avl_first(hdl->libzfs_ns_avl); cn != NULL;
	    cn = uu_avl_next(hdl->libzfs_ns_avl, cn)) {

		if (check_restricted(cn->cn_name))
			continue;

		if (zpool_open_silent(hdl, cn->cn_name, &zhp) != 0) {
			hdl->libzfs_pool_iter--;
			return (-1);
		}

		if (zhp == NULL)
			continue;

		if ((ret = func(zhp, data)) != 0) {
			hdl->libzfs_pool_iter--;
			return (ret);
		}
	}
	hdl->libzfs_pool_iter--;

	return (0);
}