xref: /titanic_51/usr/src/common/zfs/zfs_prop.c (revision 275c9da86e89f8abf71135cf63d9fc23671b2e60)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/zio.h>
29 #include <sys/spa.h>
30 #include <sys/u8_textprep.h>
31 #include <sys/zfs_acl.h>
32 #include <sys/zfs_ioctl.h>
33 #include <sys/zfs_znode.h>
34 
35 #include "zfs_prop.h"
36 #include "zfs_deleg.h"
37 
38 #if defined(_KERNEL)
39 #include <sys/systm.h>
40 #else
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44 #endif
45 
46 static zprop_desc_t zfs_prop_table[ZFS_NUM_PROPS];
47 
48 zprop_desc_t *
49 zfs_prop_get_table(void)
50 {
51 	return (zfs_prop_table);
52 }
53 
54 void
55 zfs_prop_init(void)
56 {
57 	static zprop_index_t checksum_table[] = {
58 		{ "on",		ZIO_CHECKSUM_ON },
59 		{ "off",	ZIO_CHECKSUM_OFF },
60 		{ "fletcher2",	ZIO_CHECKSUM_FLETCHER_2 },
61 		{ "fletcher4",	ZIO_CHECKSUM_FLETCHER_4 },
62 		{ "sha256",	ZIO_CHECKSUM_SHA256 },
63 		{ NULL }
64 	};
65 
66 	static zprop_index_t compress_table[] = {
67 		{ "on",		ZIO_COMPRESS_ON },
68 		{ "off",	ZIO_COMPRESS_OFF },
69 		{ "lzjb",	ZIO_COMPRESS_LZJB },
70 		{ "gzip",	ZIO_COMPRESS_GZIP_6 },	/* gzip default */
71 		{ "gzip-1",	ZIO_COMPRESS_GZIP_1 },
72 		{ "gzip-2",	ZIO_COMPRESS_GZIP_2 },
73 		{ "gzip-3",	ZIO_COMPRESS_GZIP_3 },
74 		{ "gzip-4",	ZIO_COMPRESS_GZIP_4 },
75 		{ "gzip-5",	ZIO_COMPRESS_GZIP_5 },
76 		{ "gzip-6",	ZIO_COMPRESS_GZIP_6 },
77 		{ "gzip-7",	ZIO_COMPRESS_GZIP_7 },
78 		{ "gzip-8",	ZIO_COMPRESS_GZIP_8 },
79 		{ "gzip-9",	ZIO_COMPRESS_GZIP_9 },
80 		{ NULL }
81 	};
82 
83 	static zprop_index_t snapdir_table[] = {
84 		{ "hidden",	ZFS_SNAPDIR_HIDDEN },
85 		{ "visible",	ZFS_SNAPDIR_VISIBLE },
86 		{ NULL }
87 	};
88 
89 	static zprop_index_t acl_mode_table[] = {
90 		{ "discard",	ZFS_ACL_DISCARD },
91 		{ "groupmask",	ZFS_ACL_GROUPMASK },
92 		{ "passthrough", ZFS_ACL_PASSTHROUGH },
93 		{ NULL }
94 	};
95 
96 	static zprop_index_t acl_inherit_table[] = {
97 		{ "discard",	ZFS_ACL_DISCARD },
98 		{ "noallow",	ZFS_ACL_NOALLOW },
99 		{ "secure",	ZFS_ACL_SECURE },
100 		{ "passthrough", ZFS_ACL_PASSTHROUGH },
101 		{ NULL }
102 	};
103 
104 	static zprop_index_t case_table[] = {
105 		{ "sensitive",		ZFS_CASE_SENSITIVE },
106 		{ "insensitive",	ZFS_CASE_INSENSITIVE },
107 		{ "mixed",		ZFS_CASE_MIXED },
108 		{ NULL }
109 	};
110 
111 	static zprop_index_t copies_table[] = {
112 		{ "1",		1 },
113 		{ "2",		2 },
114 		{ "3",		3 },
115 		{ NULL }
116 	};
117 
118 	/*
119 	 * Use the unique flags we have to send to u8_strcmp() and/or
120 	 * u8_textprep() to represent the various normalization property
121 	 * values.
122 	 */
123 	static zprop_index_t normalize_table[] = {
124 		{ "none",	0 },
125 		{ "formD",	U8_TEXTPREP_NFD },
126 		{ "formKC",	U8_TEXTPREP_NFKC },
127 		{ "formC",	U8_TEXTPREP_NFC },
128 		{ "formKD",	U8_TEXTPREP_NFKD },
129 		{ NULL }
130 	};
131 
132 	static zprop_index_t version_table[] = {
133 		{ "1",		1 },
134 		{ "2",		2 },
135 		{ "3",		3 },
136 		{ "current",	ZPL_VERSION },
137 		{ NULL }
138 	};
139 
140 	static zprop_index_t boolean_table[] = {
141 		{ "off",	0 },
142 		{ "on",		1 },
143 		{ NULL }
144 	};
145 
146 	static zprop_index_t canmount_table[] = {
147 		{ "off",	ZFS_CANMOUNT_OFF },
148 		{ "on",		ZFS_CANMOUNT_ON },
149 		{ "noauto",	ZFS_CANMOUNT_NOAUTO },
150 		{ NULL }
151 	};
152 
153 	/* inherit index properties */
154 	register_index(ZFS_PROP_CHECKSUM, "checksum", ZIO_CHECKSUM_DEFAULT,
155 	    PROP_INHERIT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
156 	    "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM",
157 	    checksum_table);
158 	register_index(ZFS_PROP_COMPRESSION, "compression",
159 	    ZIO_COMPRESS_DEFAULT, PROP_INHERIT,
160 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
161 	    "on | off | lzjb | gzip | gzip-[1-9]", "COMPRESS", compress_table);
162 	register_index(ZFS_PROP_SNAPDIR, "snapdir", ZFS_SNAPDIR_HIDDEN,
163 	    PROP_INHERIT, ZFS_TYPE_FILESYSTEM,
164 	    "hidden | visible", "SNAPDIR", snapdir_table);
165 	register_index(ZFS_PROP_ACLMODE, "aclmode", ZFS_ACL_GROUPMASK,
166 	    PROP_INHERIT, ZFS_TYPE_FILESYSTEM,
167 	    "discard | groupmask | passthrough", "ACLMODE", acl_mode_table);
168 	register_index(ZFS_PROP_ACLINHERIT, "aclinherit", ZFS_ACL_SECURE,
169 	    PROP_INHERIT, ZFS_TYPE_FILESYSTEM,
170 	    "discard | noallow | secure | passthrough", "ACLINHERIT",
171 	    acl_inherit_table);
172 	register_index(ZFS_PROP_COPIES, "copies", 1,
173 	    PROP_INHERIT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
174 	    "1 | 2 | 3", "COPIES", copies_table);
175 
176 	/* inherit index (boolean) properties */
177 	register_index(ZFS_PROP_ATIME, "atime", 1, PROP_INHERIT,
178 	    ZFS_TYPE_FILESYSTEM, "on | off", "ATIME", boolean_table);
179 	register_index(ZFS_PROP_DEVICES, "devices", 1, PROP_INHERIT,
180 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "DEVICES",
181 	    boolean_table);
182 	register_index(ZFS_PROP_EXEC, "exec", 1, PROP_INHERIT,
183 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "EXEC",
184 	    boolean_table);
185 	register_index(ZFS_PROP_SETUID, "setuid", 1, PROP_INHERIT,
186 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "SETUID",
187 	    boolean_table);
188 	register_index(ZFS_PROP_READONLY, "readonly", 0, PROP_INHERIT,
189 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "on | off", "RDONLY",
190 	    boolean_table);
191 	register_index(ZFS_PROP_ZONED, "zoned", 0, PROP_INHERIT,
192 	    ZFS_TYPE_FILESYSTEM, "on | off", "ZONED", boolean_table);
193 	register_index(ZFS_PROP_XATTR, "xattr", 1, PROP_INHERIT,
194 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "XATTR",
195 	    boolean_table);
196 	register_index(ZFS_PROP_VSCAN, "vscan", 0, PROP_INHERIT,
197 	    ZFS_TYPE_FILESYSTEM, "on | off", "VSCAN",
198 	    boolean_table);
199 	register_index(ZFS_PROP_NBMAND, "nbmand", 0, PROP_INHERIT,
200 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "NBMAND",
201 	    boolean_table);
202 
203 	/* default index properties */
204 	register_index(ZFS_PROP_VERSION, "version", 0, PROP_DEFAULT,
205 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
206 	    "1 | 2 | 3 | current", "VERSION", version_table);
207 	register_index(ZFS_PROP_CANMOUNT, "canmount", ZFS_CANMOUNT_ON,
208 	    PROP_DEFAULT, ZFS_TYPE_FILESYSTEM, "on | off | noauto",
209 	    "CANMOUNT", canmount_table);
210 
211 	/* readonly index (boolean) properties */
212 	register_index(ZFS_PROP_MOUNTED, "mounted", 0, PROP_READONLY,
213 	    ZFS_TYPE_FILESYSTEM, "yes | no", "MOUNTED", boolean_table);
214 
215 	/* set once index properties */
216 	register_index(ZFS_PROP_NORMALIZE, "normalization", 0,
217 	    PROP_ONETIME, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
218 	    "none | formC | formD | formKC | formKD", "NORMALIZATION",
219 	    normalize_table);
220 	register_index(ZFS_PROP_CASE, "casesensitivity", ZFS_CASE_SENSITIVE,
221 	    PROP_ONETIME, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
222 	    "sensitive | insensitive | mixed", "CASE", case_table);
223 
224 	/* set once index (boolean) properties */
225 	register_index(ZFS_PROP_UTF8ONLY, "utf8only", 0, PROP_ONETIME,
226 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
227 	    "on | off", "UTF8ONLY", boolean_table);
228 
229 	/* string properties */
230 	register_string(ZFS_PROP_ORIGIN, "origin", NULL, PROP_READONLY,
231 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<snapshot>", "ORIGIN");
232 	register_string(ZFS_PROP_MOUNTPOINT, "mountpoint", "/", PROP_INHERIT,
233 	    ZFS_TYPE_FILESYSTEM, "<path> | legacy | none", "MOUNTPOINT");
234 	register_string(ZFS_PROP_SHARENFS, "sharenfs", "off", PROP_INHERIT,
235 	    ZFS_TYPE_FILESYSTEM, "on | off | share(1M) options", "SHARENFS");
236 	register_string(ZFS_PROP_SHAREISCSI, "shareiscsi", "off", PROP_INHERIT,
237 	    ZFS_TYPE_DATASET, "on | off | type=<type>", "SHAREISCSI");
238 	register_string(ZFS_PROP_TYPE, "type", NULL, PROP_READONLY,
239 	    ZFS_TYPE_DATASET, "filesystem | volume | snapshot", "TYPE");
240 	register_string(ZFS_PROP_SHARESMB, "sharesmb", "off", PROP_INHERIT,
241 	    ZFS_TYPE_FILESYSTEM, "on | off | sharemgr(1M) options", "SHARESMB");
242 
243 	/* readonly number properties */
244 	register_number(ZFS_PROP_USED, "used", 0, PROP_READONLY,
245 	    ZFS_TYPE_DATASET, "<size>", "USED");
246 	register_number(ZFS_PROP_AVAILABLE, "available", 0, PROP_READONLY,
247 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "AVAIL");
248 	register_number(ZFS_PROP_REFERENCED, "referenced", 0, PROP_READONLY,
249 	    ZFS_TYPE_DATASET, "<size>", "REFER");
250 	register_number(ZFS_PROP_COMPRESSRATIO, "compressratio", 0,
251 	    PROP_READONLY, ZFS_TYPE_DATASET,
252 	    "<1.00x or higher if compressed>", "RATIO");
253 	register_number(ZFS_PROP_VOLBLOCKSIZE, "volblocksize", 8192,
254 	    PROP_ONETIME,
255 	    ZFS_TYPE_VOLUME, "512 to 128k, power of 2",	"VOLBLOCK");
256 
257 	/* default number properties */
258 	register_number(ZFS_PROP_QUOTA, "quota", 0, PROP_DEFAULT,
259 	    ZFS_TYPE_FILESYSTEM, "<size> | none", "QUOTA");
260 	register_number(ZFS_PROP_RESERVATION, "reservation", 0, PROP_DEFAULT,
261 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size> | none", "RESERV");
262 	register_number(ZFS_PROP_VOLSIZE, "volsize", 0, PROP_DEFAULT,
263 	    ZFS_TYPE_VOLUME, "<size>", "VOLSIZE");
264 	register_number(ZFS_PROP_REFQUOTA, "refquota", 0, PROP_DEFAULT,
265 	    ZFS_TYPE_FILESYSTEM, "<size> | none", "REFQUOTA");
266 	register_number(ZFS_PROP_REFRESERVATION, "refreservation", 0,
267 	    PROP_DEFAULT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
268 	    "<size> | none", "REFRESERV");
269 
270 	/* inherit number properties */
271 	register_number(ZFS_PROP_RECORDSIZE, "recordsize", SPA_MAXBLOCKSIZE,
272 	    PROP_INHERIT,
273 	    ZFS_TYPE_FILESYSTEM, "512 to 128k, power of 2", "RECSIZE");
274 
275 	/* hidden properties */
276 	register_hidden(ZFS_PROP_CREATETXG, "createtxg", PROP_TYPE_NUMBER,
277 	    PROP_READONLY, ZFS_TYPE_DATASET, NULL);
278 	register_hidden(ZFS_PROP_NUMCLONES, "numclones", PROP_TYPE_NUMBER,
279 	    PROP_READONLY, ZFS_TYPE_SNAPSHOT, NULL);
280 	register_hidden(ZFS_PROP_NAME, "name", PROP_TYPE_STRING,
281 	    PROP_READONLY, ZFS_TYPE_DATASET, "NAME");
282 	register_hidden(ZFS_PROP_ISCSIOPTIONS, "iscsioptions", PROP_TYPE_STRING,
283 	    PROP_INHERIT, ZFS_TYPE_VOLUME, "ISCSIOPTIONS");
284 
285 	/* oddball properties */
286 	register_impl(ZFS_PROP_CREATION, "creation", PROP_TYPE_NUMBER, 0, NULL,
287 	    PROP_READONLY, ZFS_TYPE_DATASET,
288 	    "<date>", "CREATION", B_FALSE, B_TRUE, NULL);
289 }
290 
291 boolean_t
292 zfs_prop_delegatable(zfs_prop_t prop)
293 {
294 	zprop_desc_t *pd = &zfs_prop_table[prop];
295 	return (pd->pd_attr != PROP_READONLY);
296 }
297 
298 /*
299  * Given a zfs dataset property name, returns the corresponding property ID.
300  */
301 zfs_prop_t
302 zfs_name_to_prop(const char *propname)
303 {
304 	return (zprop_name_to_prop(propname, ZFS_TYPE_DATASET));
305 }
306 
307 
308 /*
309  * For user property names, we allow all lowercase alphanumeric characters, plus
310  * a few useful punctuation characters.
311  */
312 static int
313 valid_char(char c)
314 {
315 	return ((c >= 'a' && c <= 'z') ||
316 	    (c >= '0' && c <= '9') ||
317 	    c == '-' || c == '_' || c == '.' || c == ':');
318 }
319 
320 /*
321  * Returns true if this is a valid user-defined property (one with a ':').
322  */
323 boolean_t
324 zfs_prop_user(const char *name)
325 {
326 	int i;
327 	char c;
328 	boolean_t foundsep = B_FALSE;
329 
330 	for (i = 0; i < strlen(name); i++) {
331 		c = name[i];
332 		if (!valid_char(c))
333 			return (B_FALSE);
334 		if (c == ':')
335 			foundsep = B_TRUE;
336 	}
337 
338 	if (!foundsep)
339 		return (B_FALSE);
340 
341 	return (B_TRUE);
342 }
343 
344 /*
345  * Tables of index types, plus functions to convert between the user view
346  * (strings) and internal representation (uint64_t).
347  */
348 int
349 zfs_prop_string_to_index(zfs_prop_t prop, const char *string, uint64_t *index)
350 {
351 	return (zprop_string_to_index(prop, string, index, ZFS_TYPE_DATASET));
352 }
353 
354 int
355 zfs_prop_index_to_string(zfs_prop_t prop, uint64_t index, const char **string)
356 {
357 	return (zprop_index_to_string(prop, index, string, ZFS_TYPE_DATASET));
358 }
359 
360 /*
361  * Returns TRUE if the property applies to any of the given dataset types.
362  */
363 int
364 zfs_prop_valid_for_type(int prop, zfs_type_t types)
365 {
366 	return (zprop_valid_for_type(prop, types));
367 }
368 
369 zprop_type_t
370 zfs_prop_get_type(zfs_prop_t prop)
371 {
372 	return (zfs_prop_table[prop].pd_proptype);
373 }
374 
375 /*
376  * Returns TRUE if the property is readonly.
377  */
378 boolean_t
379 zfs_prop_readonly(zfs_prop_t prop)
380 {
381 	return (zfs_prop_table[prop].pd_attr == PROP_READONLY ||
382 	    zfs_prop_table[prop].pd_attr == PROP_ONETIME);
383 }
384 
385 /*
386  * Returns TRUE if the property is only allowed to be set once.
387  */
388 boolean_t
389 zfs_prop_setonce(zfs_prop_t prop)
390 {
391 	return (zfs_prop_table[prop].pd_attr == PROP_ONETIME);
392 }
393 
394 const char *
395 zfs_prop_default_string(zfs_prop_t prop)
396 {
397 	return (zfs_prop_table[prop].pd_strdefault);
398 }
399 
400 uint64_t
401 zfs_prop_default_numeric(zfs_prop_t prop)
402 {
403 	return (zfs_prop_table[prop].pd_numdefault);
404 }
405 
406 /*
407  * Given a dataset property ID, returns the corresponding name.
408  * Assuming the zfs dataset property ID is valid.
409  */
410 const char *
411 zfs_prop_to_name(zfs_prop_t prop)
412 {
413 	return (zfs_prop_table[prop].pd_name);
414 }
415 
416 /*
417  * Returns TRUE if the property is inheritable.
418  */
419 boolean_t
420 zfs_prop_inheritable(zfs_prop_t prop)
421 {
422 	return (zfs_prop_table[prop].pd_attr == PROP_INHERIT ||
423 	    zfs_prop_table[prop].pd_attr == PROP_ONETIME);
424 }
425 
426 #ifndef _KERNEL
427 
428 /*
429  * Returns a string describing the set of acceptable values for the given
430  * zfs property, or NULL if it cannot be set.
431  */
432 const char *
433 zfs_prop_values(zfs_prop_t prop)
434 {
435 	return (zfs_prop_table[prop].pd_values);
436 }
437 
438 /*
439  * Returns TRUE if this property is a string type.  Note that index types
440  * (compression, checksum) are treated as strings in userland, even though they
441  * are stored numerically on disk.
442  */
443 int
444 zfs_prop_is_string(zfs_prop_t prop)
445 {
446 	return (zfs_prop_table[prop].pd_proptype == PROP_TYPE_STRING ||
447 	    zfs_prop_table[prop].pd_proptype == PROP_TYPE_INDEX);
448 }
449 
450 /*
451  * Returns the column header for the given property.  Used only in
452  * 'zfs list -o', but centralized here with the other property information.
453  */
454 const char *
455 zfs_prop_column_name(zfs_prop_t prop)
456 {
457 	return (zfs_prop_table[prop].pd_colname);
458 }
459 
460 /*
461  * Returns whether the given property should be displayed right-justified for
462  * 'zfs list'.
463  */
464 boolean_t
465 zfs_prop_align_right(zfs_prop_t prop)
466 {
467 	return (zfs_prop_table[prop].pd_rightalign);
468 }
469 
470 #endif
471