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