xref: /titanic_41/usr/src/common/zfs/zfs_comutil.c (revision 3fb517f786391b507780c78aabb8d98bfea9efe9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * 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.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 /*
26  * This file is intended for functions that ought to be common between user
27  * land (libzfs) and the kernel. When many common routines need to be shared
28  * then a separate file should to be created.
29  */
30 
31 #if defined(_KERNEL)
32 #include <sys/systm.h>
33 #else
34 #include <string.h>
35 #endif
36 
37 #include <sys/types.h>
38 #include <sys/fs/zfs.h>
39 #include <sys/int_limits.h>
40 #include <sys/nvpair.h>
41 #include "zfs_comutil.h"
42 
43 /*
44  * Are there allocatable vdevs?
45  */
46 boolean_t
47 zfs_allocatable_devs(nvlist_t *nv)
48 {
49 	uint64_t is_log;
50 	uint_t c;
51 	nvlist_t **child;
52 	uint_t children;
53 
54 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
55 	    &child, &children) != 0) {
56 		return (B_FALSE);
57 	}
58 	for (c = 0; c < children; c++) {
59 		is_log = 0;
60 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
61 		    &is_log);
62 		if (!is_log)
63 			return (B_TRUE);
64 	}
65 	return (B_FALSE);
66 }
67 
68 void
69 zpool_get_rewind_policy(nvlist_t *nvl, zpool_rewind_policy_t *zrpp)
70 {
71 	nvlist_t *policy;
72 	nvpair_t *elem;
73 	char *nm;
74 
75 	/* Defaults */
76 	zrpp->zrp_request = ZPOOL_NO_REWIND;
77 	zrpp->zrp_maxmeta = 0;
78 	zrpp->zrp_maxdata = UINT64_MAX;
79 	zrpp->zrp_txg = UINT64_MAX;
80 
81 	if (nvl == NULL)
82 		return;
83 
84 	elem = NULL;
85 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
86 		nm = nvpair_name(elem);
87 		if (strcmp(nm, ZPOOL_REWIND_POLICY) == 0) {
88 			if (nvpair_value_nvlist(elem, &policy) == 0)
89 				zpool_get_rewind_policy(policy, zrpp);
90 			return;
91 		} else if (strcmp(nm, ZPOOL_REWIND_REQUEST) == 0) {
92 			if (nvpair_value_uint32(elem, &zrpp->zrp_request) == 0)
93 				if (zrpp->zrp_request & ~ZPOOL_REWIND_POLICIES)
94 					zrpp->zrp_request = ZPOOL_NO_REWIND;
95 		} else if (strcmp(nm, ZPOOL_REWIND_REQUEST_TXG) == 0) {
96 			(void) nvpair_value_uint64(elem, &zrpp->zrp_txg);
97 		} else if (strcmp(nm, ZPOOL_REWIND_META_THRESH) == 0) {
98 			(void) nvpair_value_uint64(elem, &zrpp->zrp_maxmeta);
99 		} else if (strcmp(nm, ZPOOL_REWIND_DATA_THRESH) == 0) {
100 			(void) nvpair_value_uint64(elem, &zrpp->zrp_maxdata);
101 		}
102 	}
103 	if (zrpp->zrp_request == 0)
104 		zrpp->zrp_request = ZPOOL_NO_REWIND;
105 }
106 
107 typedef struct zfs_version_spa_map {
108 	int	version_zpl;
109 	int	version_spa;
110 } zfs_version_spa_map_t;
111 
112 /*
113  * Keep this table in monotonically increasing version number order.
114  */
115 static zfs_version_spa_map_t zfs_version_table[] = {
116 	{ZPL_VERSION_INITIAL, SPA_VERSION_INITIAL},
117 	{ZPL_VERSION_DIRENT_TYPE, SPA_VERSION_INITIAL},
118 	{ZPL_VERSION_FUID, SPA_VERSION_FUID},
119 	{ZPL_VERSION_USERSPACE, SPA_VERSION_USERSPACE},
120 	{ZPL_VERSION_SA, SPA_VERSION_SA},
121 	{0, 0}
122 };
123 
124 /*
125  * Return the max zpl version for a corresponding spa version
126  * -1 is returned if no mapping exists.
127  */
128 int
129 zfs_zpl_version_map(int spa_version)
130 {
131 	int i;
132 	int version = -1;
133 
134 	for (i = 0; zfs_version_table[i].version_spa; i++) {
135 		if (spa_version >= zfs_version_table[i].version_spa)
136 			version = zfs_version_table[i].version_zpl;
137 	}
138 
139 	return (version);
140 }
141 
142 /*
143  * Return the min spa version for a corresponding spa version
144  * -1 is returned if no mapping exists.
145  */
146 int
147 zfs_spa_version_map(int zpl_version)
148 {
149 	int i;
150 	int version = -1;
151 
152 	for (i = 0; zfs_version_table[i].version_zpl; i++) {
153 		if (zfs_version_table[i].version_zpl >= zpl_version)
154 			return (zfs_version_table[i].version_spa);
155 	}
156 
157 	return (version);
158 }
159 
160 const char *zfs_history_event_names[LOG_END] = {
161 	"invalid event",
162 	"pool create",
163 	"vdev add",
164 	"pool remove",
165 	"pool destroy",
166 	"pool export",
167 	"pool import",
168 	"vdev attach",
169 	"vdev replace",
170 	"vdev detach",
171 	"vdev online",
172 	"vdev offline",
173 	"vdev upgrade",
174 	"pool clear",
175 	"pool scrub",
176 	"pool property set",
177 	"create",
178 	"clone",
179 	"destroy",
180 	"destroy_begin_sync",
181 	"inherit",
182 	"property set",
183 	"quota set",
184 	"permission update",
185 	"permission remove",
186 	"permission who remove",
187 	"promote",
188 	"receive",
189 	"rename",
190 	"reservation set",
191 	"replay_inc_sync",
192 	"replay_full_sync",
193 	"rollback",
194 	"snapshot",
195 	"filesystem version upgrade",
196 	"refquota set",
197 	"refreservation set",
198 	"pool scrub done",
199 	"user hold",
200 	"user release",
201 	"pool split",
202 };
203