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  * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
24  */
25 
26 /*
27  * This file is intended for functions that ought to be common between user
28  * land (libzfs) and the kernel. When many common routines need to be shared
29  * then a separate file should be created.
30  */
31 
32 #if !defined(_KERNEL)
33 #include <string.h>
34 #endif
35 
36 #include <sys/types.h>
37 #include <sys/fs/zfs.h>
38 #include <sys/nvpair.h>
39 #include "zfs_comutil.h"
40 #include <sys/zfs_ratelimit.h>
41 
42 /*
43  * Are there allocatable vdevs?
44  */
45 boolean_t
46 zfs_allocatable_devs(nvlist_t *nv)
47 {
48 	uint64_t is_log;
49 	uint_t c;
50 	nvlist_t **child;
51 	uint_t children;
52 
53 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
54 	    &child, &children) != 0) {
55 		return (B_FALSE);
56 	}
57 	for (c = 0; c < children; c++) {
58 		is_log = 0;
59 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
60 		    &is_log);
61 		if (!is_log)
62 			return (B_TRUE);
63 	}
64 	return (B_FALSE);
65 }
66 
67 /*
68  * Are there special vdevs?
69  */
70 boolean_t
71 zfs_special_devs(nvlist_t *nv, char *type)
72 {
73 	char *bias;
74 	uint_t c;
75 	nvlist_t **child;
76 	uint_t children;
77 
78 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
79 	    &child, &children) != 0) {
80 		return (B_FALSE);
81 	}
82 	for (c = 0; c < children; c++) {
83 		if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_ALLOCATION_BIAS,
84 		    &bias) == 0) {
85 			if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0 ||
86 			    strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0) {
87 				if (type != NULL && strcmp(bias, type) == 0) {
88 					return (B_TRUE);
89 				} else if (type == NULL) {
90 					return (B_TRUE);
91 				}
92 			}
93 		}
94 	}
95 	return (B_FALSE);
96 }
97 
98 void
99 zpool_get_load_policy(nvlist_t *nvl, zpool_load_policy_t *zlpp)
100 {
101 	nvlist_t *policy;
102 	nvpair_t *elem;
103 	char *nm;
104 
105 	/* Defaults */
106 	zlpp->zlp_rewind = ZPOOL_NO_REWIND;
107 	zlpp->zlp_maxmeta = 0;
108 	zlpp->zlp_maxdata = UINT64_MAX;
109 	zlpp->zlp_txg = UINT64_MAX;
110 
111 	if (nvl == NULL)
112 		return;
113 
114 	elem = NULL;
115 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
116 		nm = nvpair_name(elem);
117 		if (strcmp(nm, ZPOOL_LOAD_POLICY) == 0) {
118 			if (nvpair_value_nvlist(elem, &policy) == 0)
119 				zpool_get_load_policy(policy, zlpp);
120 			return;
121 		} else if (strcmp(nm, ZPOOL_LOAD_REWIND_POLICY) == 0) {
122 			if (nvpair_value_uint32(elem, &zlpp->zlp_rewind) == 0)
123 				if (zlpp->zlp_rewind & ~ZPOOL_REWIND_POLICIES)
124 					zlpp->zlp_rewind = ZPOOL_NO_REWIND;
125 		} else if (strcmp(nm, ZPOOL_LOAD_REQUEST_TXG) == 0) {
126 			(void) nvpair_value_uint64(elem, &zlpp->zlp_txg);
127 		} else if (strcmp(nm, ZPOOL_LOAD_META_THRESH) == 0) {
128 			(void) nvpair_value_uint64(elem, &zlpp->zlp_maxmeta);
129 		} else if (strcmp(nm, ZPOOL_LOAD_DATA_THRESH) == 0) {
130 			(void) nvpair_value_uint64(elem, &zlpp->zlp_maxdata);
131 		}
132 	}
133 	if (zlpp->zlp_rewind == 0)
134 		zlpp->zlp_rewind = ZPOOL_NO_REWIND;
135 }
136 
137 typedef struct zfs_version_spa_map {
138 	int	version_zpl;
139 	int	version_spa;
140 } zfs_version_spa_map_t;
141 
142 /*
143  * Keep this table in monotonically increasing version number order.
144  */
145 static zfs_version_spa_map_t zfs_version_table[] = {
146 	{ZPL_VERSION_INITIAL, SPA_VERSION_INITIAL},
147 	{ZPL_VERSION_DIRENT_TYPE, SPA_VERSION_INITIAL},
148 	{ZPL_VERSION_FUID, SPA_VERSION_FUID},
149 	{ZPL_VERSION_USERSPACE, SPA_VERSION_USERSPACE},
150 	{ZPL_VERSION_SA, SPA_VERSION_SA},
151 	{0, 0}
152 };
153 
154 /*
155  * Return the max zpl version for a corresponding spa version
156  * -1 is returned if no mapping exists.
157  */
158 int
159 zfs_zpl_version_map(int spa_version)
160 {
161 	int version = -1;
162 
163 	for (int i = 0; zfs_version_table[i].version_spa; i++)
164 		if (spa_version >= zfs_version_table[i].version_spa)
165 			version = zfs_version_table[i].version_zpl;
166 
167 	return (version);
168 }
169 
170 /*
171  * Return the min spa version for a corresponding spa version
172  * -1 is returned if no mapping exists.
173  */
174 int
175 zfs_spa_version_map(int zpl_version)
176 {
177 	for (int i = 0; zfs_version_table[i].version_zpl; i++)
178 		if (zfs_version_table[i].version_zpl >= zpl_version)
179 			return (zfs_version_table[i].version_spa);
180 
181 	return (-1);
182 }
183 
184 /*
185  * This is the table of legacy internal event names; it should not be modified.
186  * The internal events are now stored in the history log as strings.
187  */
188 const char *const zfs_history_event_names[ZFS_NUM_LEGACY_HISTORY_EVENTS] = {
189 	"invalid event",
190 	"pool create",
191 	"vdev add",
192 	"pool remove",
193 	"pool destroy",
194 	"pool export",
195 	"pool import",
196 	"vdev attach",
197 	"vdev replace",
198 	"vdev detach",
199 	"vdev online",
200 	"vdev offline",
201 	"vdev upgrade",
202 	"pool clear",
203 	"pool scrub",
204 	"pool property set",
205 	"create",
206 	"clone",
207 	"destroy",
208 	"destroy_begin_sync",
209 	"inherit",
210 	"property set",
211 	"quota set",
212 	"permission update",
213 	"permission remove",
214 	"permission who remove",
215 	"promote",
216 	"receive",
217 	"rename",
218 	"reservation set",
219 	"replay_inc_sync",
220 	"replay_full_sync",
221 	"rollback",
222 	"snapshot",
223 	"filesystem version upgrade",
224 	"refquota set",
225 	"refreservation set",
226 	"pool scrub done",
227 	"user hold",
228 	"user release",
229 	"pool split",
230 };
231 
232 boolean_t
233 zfs_dataset_name_hidden(const char *name)
234 {
235 	/*
236 	 * Skip over datasets that are not visible in this zone,
237 	 * internal datasets (which have a $ in their name), and
238 	 * temporary datasets (which have a % in their name).
239 	 */
240 	if (strpbrk(name, "$%") != NULL)
241 		return (B_TRUE);
242 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
243 		return (B_TRUE);
244 	return (B_FALSE);
245 }
246 
247 #if defined(_KERNEL)
248 EXPORT_SYMBOL(zfs_allocatable_devs);
249 EXPORT_SYMBOL(zfs_special_devs);
250 EXPORT_SYMBOL(zpool_get_load_policy);
251 EXPORT_SYMBOL(zfs_zpl_version_map);
252 EXPORT_SYMBOL(zfs_spa_version_map);
253 EXPORT_SYMBOL(zfs_history_event_names);
254 EXPORT_SYMBOL(zfs_dataset_name_hidden);
255 #endif
256