]> git.proxmox.com Git - mirror_zfs.git/blob - module/zcommon/zfs_comutil.c
Add CODE_OF_CONDUCT.md
[mirror_zfs.git] / module / zcommon / zfs_comutil.c
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 to 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 void
68 zpool_get_load_policy(nvlist_t *nvl, zpool_load_policy_t *zlpp)
69 {
70 nvlist_t *policy;
71 nvpair_t *elem;
72 char *nm;
73
74 /* Defaults */
75 zlpp->zlp_rewind = ZPOOL_NO_REWIND;
76 zlpp->zlp_maxmeta = 0;
77 zlpp->zlp_maxdata = UINT64_MAX;
78 zlpp->zlp_txg = UINT64_MAX;
79
80 if (nvl == NULL)
81 return;
82
83 elem = NULL;
84 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
85 nm = nvpair_name(elem);
86 if (strcmp(nm, ZPOOL_LOAD_POLICY) == 0) {
87 if (nvpair_value_nvlist(elem, &policy) == 0)
88 zpool_get_load_policy(policy, zlpp);
89 return;
90 } else if (strcmp(nm, ZPOOL_LOAD_REWIND_POLICY) == 0) {
91 if (nvpair_value_uint32(elem, &zlpp->zlp_rewind) == 0)
92 if (zlpp->zlp_rewind & ~ZPOOL_REWIND_POLICIES)
93 zlpp->zlp_rewind = ZPOOL_NO_REWIND;
94 } else if (strcmp(nm, ZPOOL_LOAD_REQUEST_TXG) == 0) {
95 (void) nvpair_value_uint64(elem, &zlpp->zlp_txg);
96 } else if (strcmp(nm, ZPOOL_LOAD_META_THRESH) == 0) {
97 (void) nvpair_value_uint64(elem, &zlpp->zlp_maxmeta);
98 } else if (strcmp(nm, ZPOOL_LOAD_DATA_THRESH) == 0) {
99 (void) nvpair_value_uint64(elem, &zlpp->zlp_maxdata);
100 }
101 }
102 if (zlpp->zlp_rewind == 0)
103 zlpp->zlp_rewind = ZPOOL_NO_REWIND;
104 }
105
106 typedef struct zfs_version_spa_map {
107 int version_zpl;
108 int version_spa;
109 } zfs_version_spa_map_t;
110
111 /*
112 * Keep this table in monotonically increasing version number order.
113 */
114 static zfs_version_spa_map_t zfs_version_table[] = {
115 {ZPL_VERSION_INITIAL, SPA_VERSION_INITIAL},
116 {ZPL_VERSION_DIRENT_TYPE, SPA_VERSION_INITIAL},
117 {ZPL_VERSION_FUID, SPA_VERSION_FUID},
118 {ZPL_VERSION_USERSPACE, SPA_VERSION_USERSPACE},
119 {ZPL_VERSION_SA, SPA_VERSION_SA},
120 {0, 0}
121 };
122
123 /*
124 * Return the max zpl version for a corresponding spa version
125 * -1 is returned if no mapping exists.
126 */
127 int
128 zfs_zpl_version_map(int spa_version)
129 {
130 int i;
131 int version = -1;
132
133 for (i = 0; zfs_version_table[i].version_spa; i++) {
134 if (spa_version >= zfs_version_table[i].version_spa)
135 version = zfs_version_table[i].version_zpl;
136 }
137
138 return (version);
139 }
140
141 /*
142 * Return the min spa version for a corresponding spa version
143 * -1 is returned if no mapping exists.
144 */
145 int
146 zfs_spa_version_map(int zpl_version)
147 {
148 int i;
149 int version = -1;
150
151 for (i = 0; zfs_version_table[i].version_zpl; i++) {
152 if (zfs_version_table[i].version_zpl >= zpl_version)
153 return (zfs_version_table[i].version_spa);
154 }
155
156 return (version);
157 }
158
159 /*
160 * This is the table of legacy internal event names; it should not be modified.
161 * The internal events are now stored in the history log as strings.
162 */
163 const char *zfs_history_event_names[ZFS_NUM_LEGACY_HISTORY_EVENTS] = {
164 "invalid event",
165 "pool create",
166 "vdev add",
167 "pool remove",
168 "pool destroy",
169 "pool export",
170 "pool import",
171 "vdev attach",
172 "vdev replace",
173 "vdev detach",
174 "vdev online",
175 "vdev offline",
176 "vdev upgrade",
177 "pool clear",
178 "pool scrub",
179 "pool property set",
180 "create",
181 "clone",
182 "destroy",
183 "destroy_begin_sync",
184 "inherit",
185 "property set",
186 "quota set",
187 "permission update",
188 "permission remove",
189 "permission who remove",
190 "promote",
191 "receive",
192 "rename",
193 "reservation set",
194 "replay_inc_sync",
195 "replay_full_sync",
196 "rollback",
197 "snapshot",
198 "filesystem version upgrade",
199 "refquota set",
200 "refreservation set",
201 "pool scrub done",
202 "user hold",
203 "user release",
204 "pool split",
205 };
206
207 boolean_t
208 zfs_dataset_name_hidden(const char *name)
209 {
210 /*
211 * Skip over datasets that are not visible in this zone,
212 * internal datasets (which have a $ in their name), and
213 * temporary datasets (which have a % in their name).
214 */
215 if (strchr(name, '$') != NULL)
216 return (B_TRUE);
217 if (strchr(name, '%') != NULL)
218 return (B_TRUE);
219 if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
220 return (B_TRUE);
221 return (B_FALSE);
222 }
223
224 #if defined(_KERNEL)
225 EXPORT_SYMBOL(zfs_allocatable_devs);
226 EXPORT_SYMBOL(zpool_get_load_policy);
227 EXPORT_SYMBOL(zfs_zpl_version_map);
228 EXPORT_SYMBOL(zfs_spa_version_map);
229 EXPORT_SYMBOL(zfs_history_event_names);
230 EXPORT_SYMBOL(zfs_dataset_name_hidden);
231 #endif