Path: blob/main/sys/contrib/openzfs/cmd/zpool/zpool_util.c
48288 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2009 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/2526#include <errno.h>27#include <libgen.h>28#include <libintl.h>29#include <stdio.h>30#include <stdlib.h>31#include <string.h>32#include <ctype.h>3334#include "zpool_util.h"3536/*37* Utility function to guarantee malloc() success.38*/39void *40safe_malloc(size_t size)41{42void *data;4344if ((data = calloc(1, size)) == NULL) {45(void) fprintf(stderr, "internal error: out of memory\n");46exit(1);47}4849return (data);50}5152/*53* Utility function to guarantee realloc() success.54*/55void *56safe_realloc(void *from, size_t size)57{58void *data;5960if ((data = realloc(from, size)) == NULL) {61(void) fprintf(stderr, "internal error: out of memory\n");62exit(1);63}6465return (data);66}6768/*69* Display an out of memory error message and abort the current program.70*/71void72zpool_no_memory(void)73{74assert(errno == ENOMEM);75(void) fprintf(stderr,76gettext("internal error: out of memory\n"));77exit(1);78}7980/*81* Return the number of logs in supplied nvlist82*/83uint_t84num_logs(nvlist_t *nv)85{86uint_t nlogs = 0;87uint_t c, children;88nvlist_t **child;8990if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,91&child, &children) != 0)92return (0);9394for (c = 0; c < children; c++) {95uint64_t is_log = B_FALSE;9697(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,98&is_log);99if (is_log)100nlogs++;101}102return (nlogs);103}104105/* Find the max element in an array of uint64_t values */106uint64_t107array64_max(uint64_t array[], unsigned int len)108{109uint64_t max = 0;110int i;111for (i = 0; i < len; i++)112max = MAX(max, array[i]);113114return (max);115}116117/*118* Find highest one bit set.119* Returns bit number + 1 of highest bit that is set, otherwise returns 0.120*/121int122highbit64(uint64_t i)123{124if (i == 0)125return (0);126127return (NBBY * sizeof (uint64_t) - __builtin_clzll(i));128}129130/*131* Find lowest one bit set.132* Returns bit number + 1 of lowest bit that is set, otherwise returns 0.133*/134int135lowbit64(uint64_t i)136{137if (i == 0)138return (0);139140return (__builtin_ffsll(i));141}142143144