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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25  * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
26  */
27 
28 #include <sys/zio.h>
29 #include <sys/spa.h>
30 #include <sys/zfs_acl.h>
31 #include <sys/zfs_ioctl.h>
32 #include <sys/fs/zfs.h>
33 
34 #include "zfs_prop.h"
35 
36 #if !defined(_KERNEL)
37 #include <stdlib.h>
38 #include <string.h>
39 #include <ctype.h>
40 #endif
41 
42 static zprop_desc_t zpool_prop_table[ZPOOL_NUM_PROPS];
43 
44 zprop_desc_t *
45 zpool_prop_get_table(void)
46 {
47 	return (zpool_prop_table);
48 }
49 
50 void
51 zpool_prop_init(void)
52 {
53 	static zprop_index_t boolean_table[] = {
54 		{ "off",	0},
55 		{ "on",		1},
56 		{ NULL }
57 	};
58 
59 	static zprop_index_t failuremode_table[] = {
60 		{ "wait",	ZIO_FAILURE_MODE_WAIT },
61 		{ "continue",	ZIO_FAILURE_MODE_CONTINUE },
62 		{ "panic",	ZIO_FAILURE_MODE_PANIC },
63 		{ NULL }
64 	};
65 
66 	/* string properties */
67 	zprop_register_string(ZPOOL_PROP_ALTROOT, "altroot", NULL, PROP_DEFAULT,
68 	    ZFS_TYPE_POOL, "<path>", "ALTROOT");
69 	zprop_register_string(ZPOOL_PROP_BOOTFS, "bootfs", NULL, PROP_DEFAULT,
70 	    ZFS_TYPE_POOL, "<filesystem>", "BOOTFS");
71 	zprop_register_string(ZPOOL_PROP_CACHEFILE, "cachefile", NULL,
72 	    PROP_DEFAULT, ZFS_TYPE_POOL, "<file> | none", "CACHEFILE");
73 	zprop_register_string(ZPOOL_PROP_COMMENT, "comment", NULL,
74 	    PROP_DEFAULT, ZFS_TYPE_POOL, "<comment-string>", "COMMENT");
75 	zprop_register_string(ZPOOL_PROP_COMPATIBILITY, "compatibility",
76 	    "off", PROP_DEFAULT, ZFS_TYPE_POOL,
77 	    "<file[,file...]> | off | legacy", "COMPATIBILITY");
78 
79 	/* readonly number properties */
80 	zprop_register_number(ZPOOL_PROP_SIZE, "size", 0, PROP_READONLY,
81 	    ZFS_TYPE_POOL, "<size>", "SIZE");
82 	zprop_register_number(ZPOOL_PROP_FREE, "free", 0, PROP_READONLY,
83 	    ZFS_TYPE_POOL, "<size>", "FREE");
84 	zprop_register_number(ZPOOL_PROP_FREEING, "freeing", 0, PROP_READONLY,
85 	    ZFS_TYPE_POOL, "<size>", "FREEING");
86 	zprop_register_number(ZPOOL_PROP_CHECKPOINT, "checkpoint", 0,
87 	    PROP_READONLY, ZFS_TYPE_POOL, "<size>", "CKPOINT");
88 	zprop_register_number(ZPOOL_PROP_LEAKED, "leaked", 0, PROP_READONLY,
89 	    ZFS_TYPE_POOL, "<size>", "LEAKED");
90 	zprop_register_number(ZPOOL_PROP_ALLOCATED, "allocated", 0,
91 	    PROP_READONLY, ZFS_TYPE_POOL, "<size>", "ALLOC");
92 	zprop_register_number(ZPOOL_PROP_EXPANDSZ, "expandsize", 0,
93 	    PROP_READONLY, ZFS_TYPE_POOL, "<size>", "EXPANDSZ");
94 	zprop_register_number(ZPOOL_PROP_FRAGMENTATION, "fragmentation", 0,
95 	    PROP_READONLY, ZFS_TYPE_POOL, "<percent>", "FRAG");
96 	zprop_register_number(ZPOOL_PROP_CAPACITY, "capacity", 0, PROP_READONLY,
97 	    ZFS_TYPE_POOL, "<size>", "CAP");
98 	zprop_register_number(ZPOOL_PROP_GUID, "guid", 0, PROP_READONLY,
99 	    ZFS_TYPE_POOL, "<guid>", "GUID");
100 	zprop_register_number(ZPOOL_PROP_LOAD_GUID, "load_guid", 0,
101 	    PROP_READONLY, ZFS_TYPE_POOL, "<load_guid>", "LOAD_GUID");
102 	zprop_register_number(ZPOOL_PROP_HEALTH, "health", 0, PROP_READONLY,
103 	    ZFS_TYPE_POOL, "<state>", "HEALTH");
104 	zprop_register_number(ZPOOL_PROP_DEDUPRATIO, "dedupratio", 0,
105 	    PROP_READONLY, ZFS_TYPE_POOL, "<1.00x or higher if deduped>",
106 	    "DEDUP");
107 
108 	/* default number properties */
109 	zprop_register_number(ZPOOL_PROP_VERSION, "version", SPA_VERSION,
110 	    PROP_DEFAULT, ZFS_TYPE_POOL, "<version>", "VERSION");
111 	zprop_register_number(ZPOOL_PROP_ASHIFT, "ashift", 0, PROP_DEFAULT,
112 	    ZFS_TYPE_POOL, "<ashift, 9-16, or 0=default>", "ASHIFT");
113 
114 	/* default index (boolean) properties */
115 	zprop_register_index(ZPOOL_PROP_DELEGATION, "delegation", 1,
116 	    PROP_DEFAULT, ZFS_TYPE_POOL, "on | off", "DELEGATION",
117 	    boolean_table);
118 	zprop_register_index(ZPOOL_PROP_AUTOREPLACE, "autoreplace", 0,
119 	    PROP_DEFAULT, ZFS_TYPE_POOL, "on | off", "REPLACE", boolean_table);
120 	zprop_register_index(ZPOOL_PROP_LISTSNAPS, "listsnapshots", 0,
121 	    PROP_DEFAULT, ZFS_TYPE_POOL, "on | off", "LISTSNAPS",
122 	    boolean_table);
123 	zprop_register_index(ZPOOL_PROP_AUTOEXPAND, "autoexpand", 0,
124 	    PROP_DEFAULT, ZFS_TYPE_POOL, "on | off", "EXPAND", boolean_table);
125 	zprop_register_index(ZPOOL_PROP_READONLY, "readonly", 0,
126 	    PROP_DEFAULT, ZFS_TYPE_POOL, "on | off", "RDONLY", boolean_table);
127 	zprop_register_index(ZPOOL_PROP_MULTIHOST, "multihost", 0,
128 	    PROP_DEFAULT, ZFS_TYPE_POOL, "on | off", "MULTIHOST",
129 	    boolean_table);
130 
131 	/* default index properties */
132 	zprop_register_index(ZPOOL_PROP_FAILUREMODE, "failmode",
133 	    ZIO_FAILURE_MODE_WAIT, PROP_DEFAULT, ZFS_TYPE_POOL,
134 	    "wait | continue | panic", "FAILMODE", failuremode_table);
135 	zprop_register_index(ZPOOL_PROP_AUTOTRIM, "autotrim",
136 	    SPA_AUTOTRIM_DEFAULT, PROP_DEFAULT, ZFS_TYPE_POOL,
137 	    "on | off", "AUTOTRIM", boolean_table);
138 
139 	/* hidden properties */
140 	zprop_register_hidden(ZPOOL_PROP_NAME, "name", PROP_TYPE_STRING,
141 	    PROP_READONLY, ZFS_TYPE_POOL, "NAME");
142 	zprop_register_hidden(ZPOOL_PROP_MAXBLOCKSIZE, "maxblocksize",
143 	    PROP_TYPE_NUMBER, PROP_READONLY, ZFS_TYPE_POOL, "MAXBLOCKSIZE");
144 	zprop_register_hidden(ZPOOL_PROP_TNAME, "tname", PROP_TYPE_STRING,
145 	    PROP_ONETIME, ZFS_TYPE_POOL, "TNAME");
146 	zprop_register_hidden(ZPOOL_PROP_MAXDNODESIZE, "maxdnodesize",
147 	    PROP_TYPE_NUMBER, PROP_READONLY, ZFS_TYPE_POOL, "MAXDNODESIZE");
148 	zprop_register_hidden(ZPOOL_PROP_DEDUPDITTO, "dedupditto",
149 	    PROP_TYPE_NUMBER, PROP_DEFAULT, ZFS_TYPE_POOL, "DEDUPDITTO");
150 }
151 
152 /*
153  * Given a property name and its type, returns the corresponding property ID.
154  */
155 zpool_prop_t
156 zpool_name_to_prop(const char *propname)
157 {
158 	return (zprop_name_to_prop(propname, ZFS_TYPE_POOL));
159 }
160 
161 /*
162  * Given a pool property ID, returns the corresponding name.
163  * Assuming the pool property ID is valid.
164  */
165 const char *
166 zpool_prop_to_name(zpool_prop_t prop)
167 {
168 	return (zpool_prop_table[prop].pd_name);
169 }
170 
171 zprop_type_t
172 zpool_prop_get_type(zpool_prop_t prop)
173 {
174 	return (zpool_prop_table[prop].pd_proptype);
175 }
176 
177 boolean_t
178 zpool_prop_readonly(zpool_prop_t prop)
179 {
180 	return (zpool_prop_table[prop].pd_attr == PROP_READONLY);
181 }
182 
183 boolean_t
184 zpool_prop_setonce(zpool_prop_t prop)
185 {
186 	return (zpool_prop_table[prop].pd_attr == PROP_ONETIME);
187 }
188 
189 const char *
190 zpool_prop_default_string(zpool_prop_t prop)
191 {
192 	return (zpool_prop_table[prop].pd_strdefault);
193 }
194 
195 uint64_t
196 zpool_prop_default_numeric(zpool_prop_t prop)
197 {
198 	return (zpool_prop_table[prop].pd_numdefault);
199 }
200 
201 /*
202  * Returns true if this is a valid feature@ property.
203  */
204 boolean_t
205 zpool_prop_feature(const char *name)
206 {
207 	static const char *prefix = "feature@";
208 	return (strncmp(name, prefix, strlen(prefix)) == 0);
209 }
210 
211 /*
212  * Returns true if this is a valid unsupported@ property.
213  */
214 boolean_t
215 zpool_prop_unsupported(const char *name)
216 {
217 	static const char *prefix = "unsupported@";
218 	return (strncmp(name, prefix, strlen(prefix)) == 0);
219 }
220 
221 int
222 zpool_prop_string_to_index(zpool_prop_t prop, const char *string,
223     uint64_t *index)
224 {
225 	return (zprop_string_to_index(prop, string, index, ZFS_TYPE_POOL));
226 }
227 
228 int
229 zpool_prop_index_to_string(zpool_prop_t prop, uint64_t index,
230     const char **string)
231 {
232 	return (zprop_index_to_string(prop, index, string, ZFS_TYPE_POOL));
233 }
234 
235 uint64_t
236 zpool_prop_random_value(zpool_prop_t prop, uint64_t seed)
237 {
238 	return (zprop_random_value(prop, seed, ZFS_TYPE_POOL));
239 }
240 
241 #ifndef _KERNEL
242 #include <libzfs.h>
243 
244 const char *
245 zpool_prop_values(zpool_prop_t prop)
246 {
247 	return (zpool_prop_table[prop].pd_values);
248 }
249 
250 const char *
251 zpool_prop_column_name(zpool_prop_t prop)
252 {
253 	return (zpool_prop_table[prop].pd_colname);
254 }
255 
256 boolean_t
257 zpool_prop_align_right(zpool_prop_t prop)
258 {
259 	return (zpool_prop_table[prop].pd_rightalign);
260 }
261 #endif
262 
263 #if defined(_KERNEL)
264 /* zpool property functions */
265 EXPORT_SYMBOL(zpool_prop_init);
266 EXPORT_SYMBOL(zpool_prop_get_type);
267 EXPORT_SYMBOL(zpool_prop_get_table);
268 
269 /* Pool property functions shared between libzfs and kernel. */
270 EXPORT_SYMBOL(zpool_name_to_prop);
271 EXPORT_SYMBOL(zpool_prop_to_name);
272 EXPORT_SYMBOL(zpool_prop_default_string);
273 EXPORT_SYMBOL(zpool_prop_default_numeric);
274 EXPORT_SYMBOL(zpool_prop_readonly);
275 EXPORT_SYMBOL(zpool_prop_feature);
276 EXPORT_SYMBOL(zpool_prop_unsupported);
277 EXPORT_SYMBOL(zpool_prop_index_to_string);
278 EXPORT_SYMBOL(zpool_prop_string_to_index);
279 #endif
280