xref: /freebsd-src/sys/contrib/openzfs/cmd/zpool/zpool_util.c (revision 271171e0d97b88ba2a7c3bf750c9672b484c1c13)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23eda14cbcSMatt Macy  * Use is subject to license terms.
24eda14cbcSMatt Macy  */
25eda14cbcSMatt Macy 
26eda14cbcSMatt Macy #include <errno.h>
27eda14cbcSMatt Macy #include <libgen.h>
28eda14cbcSMatt Macy #include <libintl.h>
29eda14cbcSMatt Macy #include <stdio.h>
30eda14cbcSMatt Macy #include <stdlib.h>
31da5137abSMartin Matuska #include <string.h>
32eda14cbcSMatt Macy #include <ctype.h>
33eda14cbcSMatt Macy 
34eda14cbcSMatt Macy #include "zpool_util.h"
35eda14cbcSMatt Macy 
36eda14cbcSMatt Macy /*
37eda14cbcSMatt Macy  * Utility function to guarantee malloc() success.
38eda14cbcSMatt Macy  */
39eda14cbcSMatt Macy void *
safe_malloc(size_t size)40eda14cbcSMatt Macy safe_malloc(size_t size)
41eda14cbcSMatt Macy {
42eda14cbcSMatt Macy 	void *data;
43eda14cbcSMatt Macy 
44eda14cbcSMatt Macy 	if ((data = calloc(1, size)) == NULL) {
45eda14cbcSMatt Macy 		(void) fprintf(stderr, "internal error: out of memory\n");
46eda14cbcSMatt Macy 		exit(1);
47eda14cbcSMatt Macy 	}
48eda14cbcSMatt Macy 
49eda14cbcSMatt Macy 	return (data);
50eda14cbcSMatt Macy }
51eda14cbcSMatt Macy 
52eda14cbcSMatt Macy /*
5316038816SMartin Matuska  * Utility function to guarantee realloc() success.
5416038816SMartin Matuska  */
5516038816SMartin Matuska void *
safe_realloc(void * from,size_t size)5616038816SMartin Matuska safe_realloc(void *from, size_t size)
5716038816SMartin Matuska {
5816038816SMartin Matuska 	void *data;
5916038816SMartin Matuska 
6016038816SMartin Matuska 	if ((data = realloc(from, size)) == NULL) {
6116038816SMartin Matuska 		(void) fprintf(stderr, "internal error: out of memory\n");
6216038816SMartin Matuska 		exit(1);
6316038816SMartin Matuska 	}
6416038816SMartin Matuska 
6516038816SMartin Matuska 	return (data);
6616038816SMartin Matuska }
6716038816SMartin Matuska 
6816038816SMartin Matuska /*
69eda14cbcSMatt Macy  * Display an out of memory error message and abort the current program.
70eda14cbcSMatt Macy  */
71eda14cbcSMatt Macy void
zpool_no_memory(void)72eda14cbcSMatt Macy zpool_no_memory(void)
73eda14cbcSMatt Macy {
74eda14cbcSMatt Macy 	assert(errno == ENOMEM);
75eda14cbcSMatt Macy 	(void) fprintf(stderr,
76eda14cbcSMatt Macy 	    gettext("internal error: out of memory\n"));
77eda14cbcSMatt Macy 	exit(1);
78eda14cbcSMatt Macy }
79eda14cbcSMatt Macy 
80eda14cbcSMatt Macy /*
81eda14cbcSMatt Macy  * Return the number of logs in supplied nvlist
82eda14cbcSMatt Macy  */
83eda14cbcSMatt Macy uint_t
num_logs(nvlist_t * nv)84eda14cbcSMatt Macy num_logs(nvlist_t *nv)
85eda14cbcSMatt Macy {
86eda14cbcSMatt Macy 	uint_t nlogs = 0;
87eda14cbcSMatt Macy 	uint_t c, children;
88eda14cbcSMatt Macy 	nvlist_t **child;
89eda14cbcSMatt Macy 
90eda14cbcSMatt Macy 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
91eda14cbcSMatt Macy 	    &child, &children) != 0)
92eda14cbcSMatt Macy 		return (0);
93eda14cbcSMatt Macy 
94eda14cbcSMatt Macy 	for (c = 0; c < children; c++) {
95eda14cbcSMatt Macy 		uint64_t is_log = B_FALSE;
96eda14cbcSMatt Macy 
97eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
98eda14cbcSMatt Macy 		    &is_log);
99eda14cbcSMatt Macy 		if (is_log)
100eda14cbcSMatt Macy 			nlogs++;
101eda14cbcSMatt Macy 	}
102eda14cbcSMatt Macy 	return (nlogs);
103eda14cbcSMatt Macy }
104eda14cbcSMatt Macy 
105eda14cbcSMatt Macy /* Find the max element in an array of uint64_t values */
106eda14cbcSMatt Macy uint64_t
array64_max(uint64_t array[],unsigned int len)107eda14cbcSMatt Macy array64_max(uint64_t array[], unsigned int len)
108eda14cbcSMatt Macy {
109eda14cbcSMatt Macy 	uint64_t max = 0;
110eda14cbcSMatt Macy 	int i;
111eda14cbcSMatt Macy 	for (i = 0; i < len; i++)
112eda14cbcSMatt Macy 		max = MAX(max, array[i]);
113eda14cbcSMatt Macy 
114eda14cbcSMatt Macy 	return (max);
115eda14cbcSMatt Macy }
116eda14cbcSMatt Macy 
117eda14cbcSMatt Macy /*
118eda14cbcSMatt Macy  * Find highest one bit set.
119eda14cbcSMatt Macy  * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
120eda14cbcSMatt Macy  */
121eda14cbcSMatt Macy int
highbit64(uint64_t i)122eda14cbcSMatt Macy highbit64(uint64_t i)
123eda14cbcSMatt Macy {
124eda14cbcSMatt Macy 	if (i == 0)
125eda14cbcSMatt Macy 		return (0);
126eda14cbcSMatt Macy 
127eda14cbcSMatt Macy 	return (NBBY * sizeof (uint64_t) - __builtin_clzll(i));
128eda14cbcSMatt Macy }
129eda14cbcSMatt Macy 
130eda14cbcSMatt Macy /*
131eda14cbcSMatt Macy  * Find lowest one bit set.
132eda14cbcSMatt Macy  * Returns bit number + 1 of lowest bit that is set, otherwise returns 0.
133eda14cbcSMatt Macy  */
134eda14cbcSMatt Macy int
lowbit64(uint64_t i)135eda14cbcSMatt Macy lowbit64(uint64_t i)
136eda14cbcSMatt Macy {
137eda14cbcSMatt Macy 	if (i == 0)
138eda14cbcSMatt Macy 		return (0);
139eda14cbcSMatt Macy 
140eda14cbcSMatt Macy 	return (__builtin_ffsll(i));
141eda14cbcSMatt Macy }
142