xref: /freebsd/sys/contrib/openzfs/module/zcommon/zfs_comutil.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25  */
26 
27 /*
28  * This file is intended for functions that ought to be common between user
29  * land (libzfs) and the kernel. When many common routines need to be shared
30  * then a separate file should be created.
31  */
32 
33 #if !defined(_KERNEL)
34 #include <string.h>
35 #endif
36 
37 #include <sys/types.h>
38 #include <sys/fs/zfs.h>
39 #include <sys/nvpair.h>
40 #include "zfs_comutil.h"
41 #include <sys/zfs_ratelimit.h>
42 
43 /*
44  * Are there allocatable vdevs?
45  */
46 boolean_t
zfs_allocatable_devs(nvlist_t * nv)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 /*
69  * Are there special vdevs?
70  */
71 boolean_t
zfs_special_devs(nvlist_t * nv,const char * type)72 zfs_special_devs(nvlist_t *nv, const char *type)
73 {
74 	const char *bias;
75 	uint_t c;
76 	nvlist_t **child;
77 	uint_t children;
78 
79 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
80 	    &child, &children) != 0) {
81 		return (B_FALSE);
82 	}
83 	for (c = 0; c < children; c++) {
84 		if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS,
85 		    &bias) == 0) {
86 			if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0 ||
87 			    strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0) {
88 				if (type == NULL ||
89 				    (type != NULL && strcmp(bias, type) == 0))
90 					return (B_TRUE);
91 			}
92 		}
93 	}
94 	return (B_FALSE);
95 }
96 
97 void
zpool_get_load_policy(nvlist_t * nvl,zpool_load_policy_t * zlpp)98 zpool_get_load_policy(nvlist_t *nvl, zpool_load_policy_t *zlpp)
99 {
100 	nvlist_t *policy;
101 	nvpair_t *elem;
102 	const char *nm;
103 
104 	/* Defaults */
105 	zlpp->zlp_rewind = ZPOOL_NO_REWIND;
106 	zlpp->zlp_maxmeta = 0;
107 	zlpp->zlp_maxdata = UINT64_MAX;
108 	zlpp->zlp_txg = UINT64_MAX;
109 
110 	if (nvl == NULL)
111 		return;
112 
113 	elem = NULL;
114 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
115 		nm = nvpair_name(elem);
116 		if (strcmp(nm, ZPOOL_LOAD_POLICY) == 0) {
117 			if (nvpair_value_nvlist(elem, &policy) == 0)
118 				zpool_get_load_policy(policy, zlpp);
119 			return;
120 		} else if (strcmp(nm, ZPOOL_LOAD_REWIND_POLICY) == 0) {
121 			if (nvpair_value_uint32(elem, &zlpp->zlp_rewind) == 0)
122 				if (zlpp->zlp_rewind & ~ZPOOL_REWIND_POLICIES)
123 					zlpp->zlp_rewind = ZPOOL_NO_REWIND;
124 		} else if (strcmp(nm, ZPOOL_LOAD_REQUEST_TXG) == 0) {
125 			(void) nvpair_value_uint64(elem, &zlpp->zlp_txg);
126 		} else if (strcmp(nm, ZPOOL_LOAD_META_THRESH) == 0) {
127 			(void) nvpair_value_uint64(elem, &zlpp->zlp_maxmeta);
128 		} else if (strcmp(nm, ZPOOL_LOAD_DATA_THRESH) == 0) {
129 			(void) nvpair_value_uint64(elem, &zlpp->zlp_maxdata);
130 		}
131 	}
132 	if (zlpp->zlp_rewind == 0)
133 		zlpp->zlp_rewind = ZPOOL_NO_REWIND;
134 }
135 
136 typedef struct zfs_version_spa_map {
137 	int	version_zpl;
138 	int	version_spa;
139 } zfs_version_spa_map_t;
140 
141 /*
142  * Keep this table in monotonically increasing version number order.
143  */
144 static zfs_version_spa_map_t zfs_version_table[] = {
145 	{ZPL_VERSION_INITIAL, SPA_VERSION_INITIAL},
146 	{ZPL_VERSION_DIRENT_TYPE, SPA_VERSION_INITIAL},
147 	{ZPL_VERSION_FUID, SPA_VERSION_FUID},
148 	{ZPL_VERSION_USERSPACE, SPA_VERSION_USERSPACE},
149 	{ZPL_VERSION_SA, SPA_VERSION_SA},
150 	{0, 0}
151 };
152 
153 /*
154  * Return the max zpl version for a corresponding spa version
155  * -1 is returned if no mapping exists.
156  */
157 int
zfs_zpl_version_map(int spa_version)158 zfs_zpl_version_map(int spa_version)
159 {
160 	int version = -1;
161 
162 	for (int i = 0; zfs_version_table[i].version_spa; i++)
163 		if (spa_version >= zfs_version_table[i].version_spa)
164 			version = zfs_version_table[i].version_zpl;
165 
166 	return (version);
167 }
168 
169 /*
170  * Return the min spa version for a corresponding spa version
171  * -1 is returned if no mapping exists.
172  */
173 int
zfs_spa_version_map(int zpl_version)174 zfs_spa_version_map(int zpl_version)
175 {
176 	for (int i = 0; zfs_version_table[i].version_zpl; i++)
177 		if (zfs_version_table[i].version_zpl >= zpl_version)
178 			return (zfs_version_table[i].version_spa);
179 
180 	return (-1);
181 }
182 
183 /*
184  * This is the table of legacy internal event names; it should not be modified.
185  * The internal events are now stored in the history log as strings.
186  */
187 const char *const zfs_history_event_names[ZFS_NUM_LEGACY_HISTORY_EVENTS] = {
188 	"invalid event",
189 	"pool create",
190 	"vdev add",
191 	"pool remove",
192 	"pool destroy",
193 	"pool export",
194 	"pool import",
195 	"vdev attach",
196 	"vdev replace",
197 	"vdev detach",
198 	"vdev online",
199 	"vdev offline",
200 	"vdev upgrade",
201 	"pool clear",
202 	"pool scrub",
203 	"pool property set",
204 	"create",
205 	"clone",
206 	"destroy",
207 	"destroy_begin_sync",
208 	"inherit",
209 	"property set",
210 	"quota set",
211 	"permission update",
212 	"permission remove",
213 	"permission who remove",
214 	"promote",
215 	"receive",
216 	"rename",
217 	"reservation set",
218 	"replay_inc_sync",
219 	"replay_full_sync",
220 	"rollback",
221 	"snapshot",
222 	"filesystem version upgrade",
223 	"refquota set",
224 	"refreservation set",
225 	"pool scrub done",
226 	"user hold",
227 	"user release",
228 	"pool split",
229 };
230 
231 boolean_t
zfs_dataset_name_hidden(const char * name)232 zfs_dataset_name_hidden(const char *name)
233 {
234 	/*
235 	 * Skip over datasets that are not visible in this zone,
236 	 * internal datasets (which have a $ in their name), and
237 	 * temporary datasets (which have a % in their name).
238 	 */
239 	if (strpbrk(name, "$%") != NULL)
240 		return (B_TRUE);
241 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
242 		return (B_TRUE);
243 	return (B_FALSE);
244 }
245 
246 #if defined(_KERNEL)
247 EXPORT_SYMBOL(zfs_allocatable_devs);
248 EXPORT_SYMBOL(zfs_special_devs);
249 EXPORT_SYMBOL(zpool_get_load_policy);
250 EXPORT_SYMBOL(zfs_zpl_version_map);
251 EXPORT_SYMBOL(zfs_spa_version_map);
252 EXPORT_SYMBOL(zfs_history_event_names);
253 EXPORT_SYMBOL(zfs_dataset_name_hidden);
254 #endif
255