1*eda14cbcSMatt Macy /* 2*eda14cbcSMatt Macy * CDDL HEADER START 3*eda14cbcSMatt Macy * 4*eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 5*eda14cbcSMatt Macy * Common Development and Distribution License (the "License"). 6*eda14cbcSMatt Macy * You may not use this file except in compliance with the License. 7*eda14cbcSMatt Macy * 8*eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*eda14cbcSMatt Macy * or http://www.opensolaris.org/os/licensing. 10*eda14cbcSMatt Macy * See the License for the specific language governing permissions 11*eda14cbcSMatt Macy * and limitations under the License. 12*eda14cbcSMatt Macy * 13*eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each 14*eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the 16*eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying 17*eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner] 18*eda14cbcSMatt Macy * 19*eda14cbcSMatt Macy * CDDL HEADER END 20*eda14cbcSMatt Macy */ 21*eda14cbcSMatt Macy /* 22*eda14cbcSMatt Macy * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23*eda14cbcSMatt Macy * Use is subject to license terms. 24*eda14cbcSMatt Macy */ 25*eda14cbcSMatt Macy 26*eda14cbcSMatt Macy #include <errno.h> 27*eda14cbcSMatt Macy #include <libgen.h> 28*eda14cbcSMatt Macy #include <libintl.h> 29*eda14cbcSMatt Macy #include <stdio.h> 30*eda14cbcSMatt Macy #include <stdlib.h> 31*eda14cbcSMatt Macy #include <strings.h> 32*eda14cbcSMatt Macy #include <ctype.h> 33*eda14cbcSMatt Macy 34*eda14cbcSMatt Macy #include "zpool_util.h" 35*eda14cbcSMatt Macy 36*eda14cbcSMatt Macy /* 37*eda14cbcSMatt Macy * Utility function to guarantee malloc() success. 38*eda14cbcSMatt Macy */ 39*eda14cbcSMatt Macy void * 40*eda14cbcSMatt Macy safe_malloc(size_t size) 41*eda14cbcSMatt Macy { 42*eda14cbcSMatt Macy void *data; 43*eda14cbcSMatt Macy 44*eda14cbcSMatt Macy if ((data = calloc(1, size)) == NULL) { 45*eda14cbcSMatt Macy (void) fprintf(stderr, "internal error: out of memory\n"); 46*eda14cbcSMatt Macy exit(1); 47*eda14cbcSMatt Macy } 48*eda14cbcSMatt Macy 49*eda14cbcSMatt Macy return (data); 50*eda14cbcSMatt Macy } 51*eda14cbcSMatt Macy 52*eda14cbcSMatt Macy /* 53*eda14cbcSMatt Macy * Display an out of memory error message and abort the current program. 54*eda14cbcSMatt Macy */ 55*eda14cbcSMatt Macy void 56*eda14cbcSMatt Macy zpool_no_memory(void) 57*eda14cbcSMatt Macy { 58*eda14cbcSMatt Macy assert(errno == ENOMEM); 59*eda14cbcSMatt Macy (void) fprintf(stderr, 60*eda14cbcSMatt Macy gettext("internal error: out of memory\n")); 61*eda14cbcSMatt Macy exit(1); 62*eda14cbcSMatt Macy } 63*eda14cbcSMatt Macy 64*eda14cbcSMatt Macy /* 65*eda14cbcSMatt Macy * Return the number of logs in supplied nvlist 66*eda14cbcSMatt Macy */ 67*eda14cbcSMatt Macy uint_t 68*eda14cbcSMatt Macy num_logs(nvlist_t *nv) 69*eda14cbcSMatt Macy { 70*eda14cbcSMatt Macy uint_t nlogs = 0; 71*eda14cbcSMatt Macy uint_t c, children; 72*eda14cbcSMatt Macy nvlist_t **child; 73*eda14cbcSMatt Macy 74*eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 75*eda14cbcSMatt Macy &child, &children) != 0) 76*eda14cbcSMatt Macy return (0); 77*eda14cbcSMatt Macy 78*eda14cbcSMatt Macy for (c = 0; c < children; c++) { 79*eda14cbcSMatt Macy uint64_t is_log = B_FALSE; 80*eda14cbcSMatt Macy 81*eda14cbcSMatt Macy (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 82*eda14cbcSMatt Macy &is_log); 83*eda14cbcSMatt Macy if (is_log) 84*eda14cbcSMatt Macy nlogs++; 85*eda14cbcSMatt Macy } 86*eda14cbcSMatt Macy return (nlogs); 87*eda14cbcSMatt Macy } 88*eda14cbcSMatt Macy 89*eda14cbcSMatt Macy /* Find the max element in an array of uint64_t values */ 90*eda14cbcSMatt Macy uint64_t 91*eda14cbcSMatt Macy array64_max(uint64_t array[], unsigned int len) 92*eda14cbcSMatt Macy { 93*eda14cbcSMatt Macy uint64_t max = 0; 94*eda14cbcSMatt Macy int i; 95*eda14cbcSMatt Macy for (i = 0; i < len; i++) 96*eda14cbcSMatt Macy max = MAX(max, array[i]); 97*eda14cbcSMatt Macy 98*eda14cbcSMatt Macy return (max); 99*eda14cbcSMatt Macy } 100*eda14cbcSMatt Macy 101*eda14cbcSMatt Macy /* 102*eda14cbcSMatt Macy * Find highest one bit set. 103*eda14cbcSMatt Macy * Returns bit number + 1 of highest bit that is set, otherwise returns 0. 104*eda14cbcSMatt Macy */ 105*eda14cbcSMatt Macy int 106*eda14cbcSMatt Macy highbit64(uint64_t i) 107*eda14cbcSMatt Macy { 108*eda14cbcSMatt Macy if (i == 0) 109*eda14cbcSMatt Macy return (0); 110*eda14cbcSMatt Macy 111*eda14cbcSMatt Macy return (NBBY * sizeof (uint64_t) - __builtin_clzll(i)); 112*eda14cbcSMatt Macy } 113*eda14cbcSMatt Macy 114*eda14cbcSMatt Macy /* 115*eda14cbcSMatt Macy * Find lowest one bit set. 116*eda14cbcSMatt Macy * Returns bit number + 1 of lowest bit that is set, otherwise returns 0. 117*eda14cbcSMatt Macy */ 118*eda14cbcSMatt Macy int 119*eda14cbcSMatt Macy lowbit64(uint64_t i) 120*eda14cbcSMatt Macy { 121*eda14cbcSMatt Macy if (i == 0) 122*eda14cbcSMatt Macy return (0); 123*eda14cbcSMatt Macy 124*eda14cbcSMatt Macy return (__builtin_ffsll(i)); 125*eda14cbcSMatt Macy } 126