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) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
24 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25 * Copyright (c) 2014 Integros [integros.com]
26 * Copyright 2016, Joyent, Inc.
27 */
28
29/* Portions Copyright 2010 Robert Milkowski */
30
31#include <sys/zio.h>
32#include <sys/spa.h>
33#include <sys/u8_textprep.h>
34#include <sys/zfs_acl.h>
35#include <sys/zfs_ioctl.h>
36#include <sys/zfs_znode.h>
37#include <sys/dsl_crypt.h>
38
39#include "zfs_prop.h"
40#include "zfs_deleg.h"
41
42#if defined(_KERNEL)
43#include <sys/systm.h>
44#else
45#include <stdlib.h>
46#include <string.h>
47#include <ctype.h>
48#endif
49
50static zprop_desc_t zfs_prop_table[ZFS_NUM_PROPS];
51
52/* Note this is indexed by zfs_userquota_prop_t, keep the order the same */
53const char *zfs_userquota_prop_prefixes[] = {
54	"userused@",
55	"userquota@",
56	"groupused@",
57	"groupquota@",
58	"userobjused@",
59	"userobjquota@",
60	"groupobjused@",
61	"groupobjquota@",
62	"projectused@",
63	"projectquota@",
64	"projectobjused@",
65	"projectobjquota@"
66};
67
68zprop_desc_t *
69zfs_prop_get_table(void)
70{
71	return (zfs_prop_table);
72}
73
74void
75zfs_prop_init(void)
76{
77	static zprop_index_t checksum_table[] = {
78		{ "on",		ZIO_CHECKSUM_ON },
79		{ "off",	ZIO_CHECKSUM_OFF },
80		{ "fletcher2",	ZIO_CHECKSUM_FLETCHER_2 },
81		{ "fletcher4",	ZIO_CHECKSUM_FLETCHER_4 },
82		{ "sha256",	ZIO_CHECKSUM_SHA256 },
83		{ "noparity",	ZIO_CHECKSUM_NOPARITY },
84		{ "sha512",	ZIO_CHECKSUM_SHA512 },
85		{ "skein",	ZIO_CHECKSUM_SKEIN },
86		{ "edonr",	ZIO_CHECKSUM_EDONR },
87		{ NULL }
88	};
89
90	static zprop_index_t dedup_table[] = {
91		{ "on",		ZIO_CHECKSUM_ON },
92		{ "off",	ZIO_CHECKSUM_OFF },
93		{ "verify",	ZIO_CHECKSUM_ON | ZIO_CHECKSUM_VERIFY },
94		{ "sha256",	ZIO_CHECKSUM_SHA256 },
95		{ "sha256,verify",
96				ZIO_CHECKSUM_SHA256 | ZIO_CHECKSUM_VERIFY },
97		{ "sha512",	ZIO_CHECKSUM_SHA512 },
98		{ "sha512,verify",
99				ZIO_CHECKSUM_SHA512 | ZIO_CHECKSUM_VERIFY },
100		{ "skein",	ZIO_CHECKSUM_SKEIN },
101		{ "skein,verify",
102				ZIO_CHECKSUM_SKEIN | ZIO_CHECKSUM_VERIFY },
103		{ "edonr,verify",
104				ZIO_CHECKSUM_EDONR | ZIO_CHECKSUM_VERIFY },
105		{ NULL }
106	};
107
108	static zprop_index_t compress_table[] = {
109		{ "on",		ZIO_COMPRESS_ON },
110		{ "off",	ZIO_COMPRESS_OFF },
111		{ "lzjb",	ZIO_COMPRESS_LZJB },
112		{ "gzip",	ZIO_COMPRESS_GZIP_6 },	/* gzip default */
113		{ "gzip-1",	ZIO_COMPRESS_GZIP_1 },
114		{ "gzip-2",	ZIO_COMPRESS_GZIP_2 },
115		{ "gzip-3",	ZIO_COMPRESS_GZIP_3 },
116		{ "gzip-4",	ZIO_COMPRESS_GZIP_4 },
117		{ "gzip-5",	ZIO_COMPRESS_GZIP_5 },
118		{ "gzip-6",	ZIO_COMPRESS_GZIP_6 },
119		{ "gzip-7",	ZIO_COMPRESS_GZIP_7 },
120		{ "gzip-8",	ZIO_COMPRESS_GZIP_8 },
121		{ "gzip-9",	ZIO_COMPRESS_GZIP_9 },
122		{ "zle",	ZIO_COMPRESS_ZLE },
123		{ "lz4",	ZIO_COMPRESS_LZ4 },
124		{ NULL }
125	};
126
127	static zprop_index_t crypto_table[] = {
128		{ "on",			ZIO_CRYPT_ON },
129		{ "off",		ZIO_CRYPT_OFF },
130		{ "aes-128-ccm",	ZIO_CRYPT_AES_128_CCM },
131		{ "aes-192-ccm",	ZIO_CRYPT_AES_192_CCM },
132		{ "aes-256-ccm",	ZIO_CRYPT_AES_256_CCM },
133		{ "aes-128-gcm",	ZIO_CRYPT_AES_128_GCM },
134		{ "aes-192-gcm",	ZIO_CRYPT_AES_192_GCM },
135		{ "aes-256-gcm",	ZIO_CRYPT_AES_256_GCM },
136		{ NULL }
137	};
138
139	static zprop_index_t keyformat_table[] = {
140		{ "none",		ZFS_KEYFORMAT_NONE },
141		{ "raw",		ZFS_KEYFORMAT_RAW },
142		{ "hex",		ZFS_KEYFORMAT_HEX },
143		{ "passphrase",		ZFS_KEYFORMAT_PASSPHRASE },
144		{ NULL }
145	};
146
147	static zprop_index_t snapdir_table[] = {
148		{ "hidden",	ZFS_SNAPDIR_HIDDEN },
149		{ "visible",	ZFS_SNAPDIR_VISIBLE },
150		{ NULL }
151	};
152
153	static zprop_index_t acl_mode_table[] = {
154		{ "discard",	ZFS_ACL_DISCARD },
155		{ "groupmask",	ZFS_ACL_GROUPMASK },
156		{ "passthrough", ZFS_ACL_PASSTHROUGH },
157		{ "restricted", ZFS_ACL_RESTRICTED },
158		{ NULL }
159	};
160
161	static zprop_index_t acl_inherit_table[] = {
162		{ "discard",	ZFS_ACL_DISCARD },
163		{ "noallow",	ZFS_ACL_NOALLOW },
164		{ "restricted",	ZFS_ACL_RESTRICTED },
165		{ "passthrough", ZFS_ACL_PASSTHROUGH },
166		{ "secure",	ZFS_ACL_RESTRICTED }, /* bkwrd compatability */
167		{ "passthrough-x", ZFS_ACL_PASSTHROUGH_X },
168		{ NULL }
169	};
170
171	static zprop_index_t case_table[] = {
172		{ "sensitive",		ZFS_CASE_SENSITIVE },
173		{ "insensitive",	ZFS_CASE_INSENSITIVE },
174		{ "mixed",		ZFS_CASE_MIXED },
175		{ NULL }
176	};
177
178	static zprop_index_t copies_table[] = {
179		{ "1",		1 },
180		{ "2",		2 },
181		{ "3",		3 },
182		{ NULL }
183	};
184
185	/*
186	 * Use the unique flags we have to send to u8_strcmp() and/or
187	 * u8_textprep() to represent the various normalization property
188	 * values.
189	 */
190	static zprop_index_t normalize_table[] = {
191		{ "none",	0 },
192		{ "formD",	U8_TEXTPREP_NFD },
193		{ "formKC",	U8_TEXTPREP_NFKC },
194		{ "formC",	U8_TEXTPREP_NFC },
195		{ "formKD",	U8_TEXTPREP_NFKD },
196		{ NULL }
197	};
198
199	static zprop_index_t version_table[] = {
200		{ "1",		1 },
201		{ "2",		2 },
202		{ "3",		3 },
203		{ "4",		4 },
204		{ "5",		5 },
205		{ "current",	ZPL_VERSION },
206		{ NULL }
207	};
208
209	static zprop_index_t boolean_table[] = {
210		{ "off",	0 },
211		{ "on",		1 },
212		{ NULL }
213	};
214
215	static zprop_index_t keystatus_table[] = {
216		{ "none",		ZFS_KEYSTATUS_NONE },
217		{ "unavailable",	ZFS_KEYSTATUS_UNAVAILABLE },
218		{ "available",		ZFS_KEYSTATUS_AVAILABLE },
219		{ NULL }
220	};
221
222	static zprop_index_t logbias_table[] = {
223		{ "latency",	ZFS_LOGBIAS_LATENCY },
224		{ "throughput",	ZFS_LOGBIAS_THROUGHPUT },
225		{ NULL }
226	};
227
228	static zprop_index_t canmount_table[] = {
229		{ "off",	ZFS_CANMOUNT_OFF },
230		{ "on",		ZFS_CANMOUNT_ON },
231		{ "noauto",	ZFS_CANMOUNT_NOAUTO },
232		{ NULL }
233	};
234
235	static zprop_index_t cache_table[] = {
236		{ "none",	ZFS_CACHE_NONE },
237		{ "metadata",	ZFS_CACHE_METADATA },
238		{ "all",	ZFS_CACHE_ALL },
239		{ NULL }
240	};
241
242	static zprop_index_t sync_table[] = {
243		{ "standard",	ZFS_SYNC_STANDARD },
244		{ "always",	ZFS_SYNC_ALWAYS },
245		{ "disabled",	ZFS_SYNC_DISABLED },
246		{ NULL }
247	};
248
249	static zprop_index_t dnsize_table[] = {
250		{ "legacy",	ZFS_DNSIZE_LEGACY },
251		{ "auto",	ZFS_DNSIZE_AUTO },
252		{ "1k",		ZFS_DNSIZE_1K },
253		{ "2k",		ZFS_DNSIZE_2K },
254		{ "4k",		ZFS_DNSIZE_4K },
255		{ "8k",		ZFS_DNSIZE_8K },
256		{ "16k",	ZFS_DNSIZE_16K },
257		{ NULL }
258	};
259
260	static zprop_index_t redundant_metadata_table[] = {
261		{ "all",	ZFS_REDUNDANT_METADATA_ALL },
262		{ "most",	ZFS_REDUNDANT_METADATA_MOST },
263		{ NULL }
264	};
265
266	/* inherit index properties */
267	zprop_register_index(ZFS_PROP_REDUNDANT_METADATA, "redundant_metadata",
268	    ZFS_REDUNDANT_METADATA_ALL,
269	    PROP_INHERIT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
270	    "all | most", "REDUND_MD",
271	    redundant_metadata_table);
272	zprop_register_index(ZFS_PROP_SYNC, "sync", ZFS_SYNC_STANDARD,
273	    PROP_INHERIT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
274	    "standard | always | disabled", "SYNC",
275	    sync_table);
276	zprop_register_index(ZFS_PROP_CHECKSUM, "checksum",
277	    ZIO_CHECKSUM_DEFAULT, PROP_INHERIT, ZFS_TYPE_FILESYSTEM |
278	    ZFS_TYPE_VOLUME,
279	    "on | off | fletcher2 | fletcher4 | sha256 | sha512 | "
280	    "skein | edonr", "CHECKSUM", checksum_table);
281	zprop_register_index(ZFS_PROP_DEDUP, "dedup", ZIO_CHECKSUM_OFF,
282	    PROP_INHERIT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
283	    "on | off | verify | sha256[,verify], sha512[,verify], "
284	    "skein[,verify], edonr,verify", "DEDUP", dedup_table);
285	zprop_register_index(ZFS_PROP_COMPRESSION, "compression",
286	    ZIO_COMPRESS_DEFAULT, PROP_INHERIT,
287	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
288	    "on | off | lzjb | gzip | gzip-[1-9] | zle | lz4",
289	    "COMPRESS", compress_table);
290	zprop_register_index(ZFS_PROP_SNAPDIR, "snapdir", ZFS_SNAPDIR_HIDDEN,
291	    PROP_INHERIT, ZFS_TYPE_FILESYSTEM,
292	    "hidden | visible", "SNAPDIR", snapdir_table);
293	zprop_register_index(ZFS_PROP_ACLMODE, "aclmode", ZFS_ACL_DISCARD,
294	    PROP_INHERIT, ZFS_TYPE_FILESYSTEM,
295	    "discard | groupmask | passthrough | restricted", "ACLMODE",
296	    acl_mode_table);
297	zprop_register_index(ZFS_PROP_ACLINHERIT, "aclinherit",
298	    ZFS_ACL_RESTRICTED, PROP_INHERIT, ZFS_TYPE_FILESYSTEM,
299	    "discard | noallow | restricted | passthrough | passthrough-x",
300	    "ACLINHERIT", acl_inherit_table);
301	zprop_register_index(ZFS_PROP_COPIES, "copies", 1, PROP_INHERIT,
302	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
303	    "1 | 2 | 3", "COPIES", copies_table);
304	zprop_register_index(ZFS_PROP_PRIMARYCACHE, "primarycache",
305	    ZFS_CACHE_ALL, PROP_INHERIT,
306	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT | ZFS_TYPE_VOLUME,
307	    "all | none | metadata", "PRIMARYCACHE", cache_table);
308	zprop_register_index(ZFS_PROP_SECONDARYCACHE, "secondarycache",
309	    ZFS_CACHE_ALL, PROP_INHERIT,
310	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT | ZFS_TYPE_VOLUME,
311	    "all | none | metadata", "SECONDARYCACHE", cache_table);
312	zprop_register_index(ZFS_PROP_LOGBIAS, "logbias", ZFS_LOGBIAS_LATENCY,
313	    PROP_INHERIT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
314	    "latency | throughput", "LOGBIAS", logbias_table);
315
316	zprop_register_index(ZFS_PROP_DNODESIZE, "dnodesize",
317	    ZFS_DNSIZE_LEGACY, PROP_INHERIT, ZFS_TYPE_FILESYSTEM,
318	    "legacy | auto | 1k | 2k | 4k | 8k | 16k", "DNSIZE", dnsize_table);
319
320	/* inherit index (boolean) properties */
321	zprop_register_index(ZFS_PROP_ATIME, "atime", 1, PROP_INHERIT,
322	    ZFS_TYPE_FILESYSTEM, "on | off", "ATIME", boolean_table);
323	zprop_register_index(ZFS_PROP_DEVICES, "devices", 1, PROP_INHERIT,
324	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "DEVICES",
325	    boolean_table);
326	zprop_register_index(ZFS_PROP_EXEC, "exec", 1, PROP_INHERIT,
327	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "EXEC",
328	    boolean_table);
329	zprop_register_index(ZFS_PROP_SETUID, "setuid", 1, PROP_INHERIT,
330	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "SETUID",
331	    boolean_table);
332	zprop_register_index(ZFS_PROP_READONLY, "readonly", 0, PROP_INHERIT,
333	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "on | off", "RDONLY",
334	    boolean_table);
335	zprop_register_index(ZFS_PROP_ZONED, "zoned", 0, PROP_INHERIT,
336	    ZFS_TYPE_FILESYSTEM, "on | off", "ZONED", boolean_table);
337	zprop_register_index(ZFS_PROP_XATTR, "xattr", 1, PROP_INHERIT,
338	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "XATTR",
339	    boolean_table);
340	zprop_register_index(ZFS_PROP_VSCAN, "vscan", 0, PROP_INHERIT,
341	    ZFS_TYPE_FILESYSTEM, "on | off", "VSCAN",
342	    boolean_table);
343	zprop_register_index(ZFS_PROP_NBMAND, "nbmand", 0, PROP_INHERIT,
344	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "NBMAND",
345	    boolean_table);
346
347	/* default index properties */
348	zprop_register_index(ZFS_PROP_VERSION, "version", 0, PROP_DEFAULT,
349	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
350	    "1 | 2 | 3 | 4 | 5 | current", "VERSION", version_table);
351	zprop_register_index(ZFS_PROP_CANMOUNT, "canmount", ZFS_CANMOUNT_ON,
352	    PROP_DEFAULT, ZFS_TYPE_FILESYSTEM, "on | off | noauto",
353	    "CANMOUNT", canmount_table);
354
355	/* readonly index properties */
356	zprop_register_index(ZFS_PROP_MOUNTED, "mounted", 0, PROP_READONLY,
357	    ZFS_TYPE_FILESYSTEM, "yes | no", "MOUNTED", boolean_table);
358	zprop_register_index(ZFS_PROP_DEFER_DESTROY, "defer_destroy", 0,
359	    PROP_READONLY, ZFS_TYPE_SNAPSHOT, "yes | no", "DEFER_DESTROY",
360	    boolean_table);
361	zprop_register_index(ZFS_PROP_KEYSTATUS, "keystatus",
362	    ZFS_KEYSTATUS_NONE, PROP_READONLY, ZFS_TYPE_DATASET,
363	    "none | unavailable | available",
364	    "KEYSTATUS", keystatus_table);
365
366	/* set once index properties */
367	zprop_register_index(ZFS_PROP_NORMALIZE, "normalization", 0,
368	    PROP_ONETIME, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
369	    "none | formC | formD | formKC | formKD", "NORMALIZATION",
370	    normalize_table);
371	zprop_register_index(ZFS_PROP_CASE, "casesensitivity",
372	    ZFS_CASE_SENSITIVE, PROP_ONETIME, ZFS_TYPE_FILESYSTEM |
373	    ZFS_TYPE_SNAPSHOT,
374	    "sensitive | insensitive | mixed", "CASE", case_table);
375	zprop_register_index(ZFS_PROP_KEYFORMAT, "keyformat",
376	    ZFS_KEYFORMAT_NONE, PROP_ONETIME_DEFAULT,
377	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
378	    "none | raw | hex | passphrase", "KEYFORMAT", keyformat_table);
379	zprop_register_index(ZFS_PROP_ENCRYPTION, "encryption",
380	    ZIO_CRYPT_DEFAULT, PROP_ONETIME, ZFS_TYPE_DATASET,
381	    "on | off | aes-128-ccm | aes-192-ccm | aes-256-ccm | "
382	    "aes-128-gcm | aes-192-gcm | aes-256-gcm", "ENCRYPTION",
383	    crypto_table);
384
385	/* set once index (boolean) properties */
386	zprop_register_index(ZFS_PROP_UTF8ONLY, "utf8only", 0, PROP_ONETIME,
387	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
388	    "on | off", "UTF8ONLY", boolean_table);
389
390	/* string properties */
391	zprop_register_string(ZFS_PROP_ORIGIN, "origin", NULL, PROP_READONLY,
392	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<snapshot>", "ORIGIN");
393	zprop_register_string(ZFS_PROP_CLONES, "clones", NULL, PROP_READONLY,
394	    ZFS_TYPE_SNAPSHOT, "<dataset>[,...]", "CLONES");
395	zprop_register_string(ZFS_PROP_MOUNTPOINT, "mountpoint", "/",
396	    PROP_INHERIT, ZFS_TYPE_FILESYSTEM, "<path> | legacy | none",
397	    "MOUNTPOINT");
398	zprop_register_string(ZFS_PROP_SHARENFS, "sharenfs", "off",
399	    PROP_INHERIT, ZFS_TYPE_FILESYSTEM, "on | off | share(1M) options",
400	    "SHARENFS");
401	zprop_register_string(ZFS_PROP_TYPE, "type", NULL, PROP_READONLY,
402	    ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK,
403	    "filesystem | volume | snapshot | bookmark", "TYPE");
404	zprop_register_string(ZFS_PROP_SHARESMB, "sharesmb", "off",
405	    PROP_INHERIT, ZFS_TYPE_FILESYSTEM,
406	    "on | off | sharemgr(1M) options", "SHARESMB");
407	zprop_register_string(ZFS_PROP_MLSLABEL, "mlslabel",
408	    ZFS_MLSLABEL_DEFAULT, PROP_INHERIT, ZFS_TYPE_DATASET,
409	    "<sensitivity label>", "MLSLABEL");
410	zprop_register_string(ZFS_PROP_RECEIVE_RESUME_TOKEN,
411	    "receive_resume_token",
412	    NULL, PROP_READONLY, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
413	    "<string token>", "RESUMETOK");
414	zprop_register_string(ZFS_PROP_ENCRYPTION_ROOT, "encryptionroot", NULL,
415	    PROP_READONLY, ZFS_TYPE_DATASET, "<filesystem | volume>",
416	    "ENCROOT");
417	zprop_register_string(ZFS_PROP_KEYLOCATION, "keylocation",
418	    "none", PROP_DEFAULT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
419	    "prompt | <file URI>", "KEYLOCATION");
420
421	/* readonly number properties */
422	zprop_register_number(ZFS_PROP_USED, "used", 0, PROP_READONLY,
423	    ZFS_TYPE_DATASET, "<size>", "USED");
424	zprop_register_number(ZFS_PROP_AVAILABLE, "available", 0, PROP_READONLY,
425	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "AVAIL");
426	zprop_register_number(ZFS_PROP_REFERENCED, "referenced", 0,
427	    PROP_READONLY, ZFS_TYPE_DATASET, "<size>", "REFER");
428	zprop_register_number(ZFS_PROP_COMPRESSRATIO, "compressratio", 0,
429	    PROP_READONLY, ZFS_TYPE_DATASET,
430	    "<1.00x or higher if compressed>", "RATIO");
431	zprop_register_number(ZFS_PROP_REFRATIO, "refcompressratio", 0,
432	    PROP_READONLY, ZFS_TYPE_DATASET,
433	    "<1.00x or higher if compressed>", "REFRATIO");
434	zprop_register_number(ZFS_PROP_VOLBLOCKSIZE, "volblocksize",
435	    ZVOL_DEFAULT_BLOCKSIZE, PROP_ONETIME,
436	    ZFS_TYPE_VOLUME, "512 to 128k, power of 2",	"VOLBLOCK");
437	zprop_register_number(ZFS_PROP_USEDSNAP, "usedbysnapshots", 0,
438	    PROP_READONLY, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>",
439	    "USEDSNAP");
440	zprop_register_number(ZFS_PROP_USEDDS, "usedbydataset", 0,
441	    PROP_READONLY, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>",
442	    "USEDDS");
443	zprop_register_number(ZFS_PROP_USEDCHILD, "usedbychildren", 0,
444	    PROP_READONLY, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>",
445	    "USEDCHILD");
446	zprop_register_number(ZFS_PROP_USEDREFRESERV, "usedbyrefreservation", 0,
447	    PROP_READONLY,
448	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "USEDREFRESERV");
449	zprop_register_number(ZFS_PROP_USERREFS, "userrefs", 0, PROP_READONLY,
450	    ZFS_TYPE_SNAPSHOT, "<count>", "USERREFS");
451	zprop_register_number(ZFS_PROP_WRITTEN, "written", 0, PROP_READONLY,
452	    ZFS_TYPE_DATASET, "<size>", "WRITTEN");
453	zprop_register_number(ZFS_PROP_LOGICALUSED, "logicalused", 0,
454	    PROP_READONLY, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>",
455	    "LUSED");
456	zprop_register_number(ZFS_PROP_LOGICALREFERENCED, "logicalreferenced",
457	    0, PROP_READONLY, ZFS_TYPE_DATASET, "<size>", "LREFER");
458	zprop_register_number(ZFS_PROP_FILESYSTEM_COUNT, "filesystem_count",
459	    UINT64_MAX, PROP_READONLY, ZFS_TYPE_FILESYSTEM,
460	    "<count>", "FSCOUNT");
461	zprop_register_number(ZFS_PROP_SNAPSHOT_COUNT, "snapshot_count",
462	    UINT64_MAX, PROP_READONLY, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
463	    "<count>", "SSCOUNT");
464	zprop_register_number(ZFS_PROP_GUID, "guid", 0, PROP_READONLY,
465	    ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK, "<uint64>", "GUID");
466	zprop_register_number(ZFS_PROP_CREATETXG, "createtxg", 0, PROP_READONLY,
467	    ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK, "<uint64>", "CREATETXG");
468	zprop_register_number(ZFS_PROP_PBKDF2_ITERS, "pbkdf2iters",
469	    0, PROP_ONETIME_DEFAULT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
470	    "<iters>", "PBKDF2ITERS");
471
472	/* default number properties */
473	zprop_register_number(ZFS_PROP_QUOTA, "quota", 0, PROP_DEFAULT,
474	    ZFS_TYPE_FILESYSTEM, "<size> | none", "QUOTA");
475	zprop_register_number(ZFS_PROP_RESERVATION, "reservation", 0,
476	    PROP_DEFAULT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
477	    "<size> | none", "RESERV");
478	zprop_register_number(ZFS_PROP_VOLSIZE, "volsize", 0, PROP_DEFAULT,
479	    ZFS_TYPE_VOLUME, "<size>", "VOLSIZE");
480	zprop_register_number(ZFS_PROP_REFQUOTA, "refquota", 0, PROP_DEFAULT,
481	    ZFS_TYPE_FILESYSTEM, "<size> | none", "REFQUOTA");
482	zprop_register_number(ZFS_PROP_REFRESERVATION, "refreservation", 0,
483	    PROP_DEFAULT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
484	    "<size> | none", "REFRESERV");
485	zprop_register_number(ZFS_PROP_FILESYSTEM_LIMIT, "filesystem_limit",
486	    UINT64_MAX, PROP_DEFAULT, ZFS_TYPE_FILESYSTEM,
487	    "<count> | none", "FSLIMIT");
488	zprop_register_number(ZFS_PROP_SNAPSHOT_LIMIT, "snapshot_limit",
489	    UINT64_MAX, PROP_DEFAULT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
490	    "<count> | none", "SSLIMIT");
491
492	/* inherit number properties */
493	zprop_register_number(ZFS_PROP_RECORDSIZE, "recordsize",
494	    SPA_OLD_MAXBLOCKSIZE, PROP_INHERIT,
495	    ZFS_TYPE_FILESYSTEM, "512 to 1M, power of 2", "RECSIZE");
496	zprop_register_number(ZFS_PROP_SPECIAL_SMALL_BLOCKS,
497	    "special_small_blocks", 0, PROP_INHERIT, ZFS_TYPE_FILESYSTEM,
498	    "zero or 512 to 128K, power of 2", "SPECIAL_SMALL_BLOCKS");
499
500	/* hidden properties */
501	zprop_register_hidden(ZFS_PROP_REMAPTXG, "remaptxg", PROP_TYPE_NUMBER,
502	    PROP_READONLY, ZFS_TYPE_DATASET, "REMAPTXG");
503	zprop_register_hidden(ZFS_PROP_NUMCLONES, "numclones", PROP_TYPE_NUMBER,
504	    PROP_READONLY, ZFS_TYPE_SNAPSHOT, "NUMCLONES");
505	zprop_register_hidden(ZFS_PROP_NAME, "name", PROP_TYPE_STRING,
506	    PROP_READONLY, ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK, "NAME");
507	zprop_register_hidden(ZFS_PROP_ISCSIOPTIONS, "iscsioptions",
508	    PROP_TYPE_STRING, PROP_INHERIT, ZFS_TYPE_VOLUME, "ISCSIOPTIONS");
509	zprop_register_hidden(ZFS_PROP_STMF_SHAREINFO, "stmf_sbd_lu",
510	    PROP_TYPE_STRING, PROP_INHERIT, ZFS_TYPE_VOLUME,
511	    "STMF_SBD_LU");
512	zprop_register_hidden(ZFS_PROP_USERACCOUNTING, "useraccounting",
513	    PROP_TYPE_NUMBER, PROP_READONLY, ZFS_TYPE_DATASET,
514	    "USERACCOUNTING");
515	zprop_register_hidden(ZFS_PROP_UNIQUE, "unique", PROP_TYPE_NUMBER,
516	    PROP_READONLY, ZFS_TYPE_DATASET, "UNIQUE");
517	zprop_register_hidden(ZFS_PROP_OBJSETID, "objsetid", PROP_TYPE_NUMBER,
518	    PROP_READONLY, ZFS_TYPE_DATASET, "OBJSETID");
519	zprop_register_hidden(ZFS_PROP_INCONSISTENT, "inconsistent",
520	    PROP_TYPE_NUMBER, PROP_READONLY, ZFS_TYPE_DATASET, "INCONSISTENT");
521	zprop_register_hidden(ZFS_PROP_IVSET_GUID, "ivsetguid",
522	    PROP_TYPE_NUMBER, PROP_READONLY,
523	    ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK, "IVSETGUID");
524	zprop_register_hidden(ZFS_PROP_PREV_SNAP, "prevsnap", PROP_TYPE_STRING,
525	    PROP_READONLY, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "PREVSNAP");
526	zprop_register_hidden(ZFS_PROP_PBKDF2_SALT, "pbkdf2salt",
527	    PROP_TYPE_NUMBER, PROP_ONETIME_DEFAULT,
528	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "PBKDF2SALT");
529	zprop_register_hidden(ZFS_PROP_KEY_GUID, "keyguid", PROP_TYPE_NUMBER,
530	    PROP_READONLY, ZFS_TYPE_DATASET, "KEYGUID");
531
532	/* oddball properties */
533	zprop_register_impl(ZFS_PROP_CREATION, "creation", PROP_TYPE_NUMBER, 0,
534	    NULL, PROP_READONLY, ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK,
535	    "<date>", "CREATION", B_FALSE, B_TRUE, NULL);
536}
537
538boolean_t
539zfs_prop_delegatable(zfs_prop_t prop)
540{
541	zprop_desc_t *pd = &zfs_prop_table[prop];
542
543	/* The mlslabel property is never delegatable. */
544	if (prop == ZFS_PROP_MLSLABEL)
545		return (B_FALSE);
546
547	return (pd->pd_attr != PROP_READONLY);
548}
549
550/*
551 * Given a zfs dataset property name, returns the corresponding property ID.
552 */
553zfs_prop_t
554zfs_name_to_prop(const char *propname)
555{
556	return (zprop_name_to_prop(propname, ZFS_TYPE_DATASET));
557}
558
559/*
560 * For user property names, we allow all lowercase alphanumeric characters, plus
561 * a few useful punctuation characters.
562 */
563static int
564valid_char(char c)
565{
566	return ((c >= 'a' && c <= 'z') ||
567	    (c >= '0' && c <= '9') ||
568	    c == '-' || c == '_' || c == '.' || c == ':');
569}
570
571/*
572 * Returns true if this is a valid user-defined property (one with a ':').
573 */
574boolean_t
575zfs_prop_user(const char *name)
576{
577	int i;
578	char c;
579	boolean_t foundsep = B_FALSE;
580
581	for (i = 0; i < strlen(name); i++) {
582		c = name[i];
583		if (!valid_char(c))
584			return (B_FALSE);
585		if (c == ':')
586			foundsep = B_TRUE;
587	}
588
589	if (!foundsep)
590		return (B_FALSE);
591
592	return (B_TRUE);
593}
594
595/*
596 * Returns true if this is a valid userspace-type property (one with a '@').
597 * Note that after the @, any character is valid (eg, another @, for SID
598 * user@domain).
599 */
600boolean_t
601zfs_prop_userquota(const char *name)
602{
603	zfs_userquota_prop_t prop;
604
605	for (prop = 0; prop < ZFS_NUM_USERQUOTA_PROPS; prop++) {
606		if (strncmp(name, zfs_userquota_prop_prefixes[prop],
607		    strlen(zfs_userquota_prop_prefixes[prop])) == 0) {
608			return (B_TRUE);
609		}
610	}
611
612	return (B_FALSE);
613}
614
615/*
616 * Returns true if this is a valid written@ property.
617 * Note that after the @, any character is valid (eg, another @, for
618 * written@pool/fs@origin).
619 */
620boolean_t
621zfs_prop_written(const char *name)
622{
623	static const char *prefix = "written@";
624	return (strncmp(name, prefix, strlen(prefix)) == 0);
625}
626
627/*
628 * Tables of index types, plus functions to convert between the user view
629 * (strings) and internal representation (uint64_t).
630 */
631int
632zfs_prop_string_to_index(zfs_prop_t prop, const char *string, uint64_t *index)
633{
634	return (zprop_string_to_index(prop, string, index, ZFS_TYPE_DATASET));
635}
636
637int
638zfs_prop_index_to_string(zfs_prop_t prop, uint64_t index, const char **string)
639{
640	return (zprop_index_to_string(prop, index, string, ZFS_TYPE_DATASET));
641}
642
643uint64_t
644zfs_prop_random_value(zfs_prop_t prop, uint64_t seed)
645{
646	return (zprop_random_value(prop, seed, ZFS_TYPE_DATASET));
647}
648
649/*
650 * Returns TRUE if the property applies to any of the given dataset types.
651 */
652boolean_t
653zfs_prop_valid_for_type(int prop, zfs_type_t types)
654{
655	return (zprop_valid_for_type(prop, types));
656}
657
658zprop_type_t
659zfs_prop_get_type(zfs_prop_t prop)
660{
661	return (zfs_prop_table[prop].pd_proptype);
662}
663
664/*
665 * Returns TRUE if the property is readonly.
666 */
667boolean_t
668zfs_prop_readonly(zfs_prop_t prop)
669{
670	return (zfs_prop_table[prop].pd_attr == PROP_READONLY ||
671	    zfs_prop_table[prop].pd_attr == PROP_ONETIME ||
672	    zfs_prop_table[prop].pd_attr == PROP_ONETIME_DEFAULT);
673}
674
675/*
676 * Returns TRUE if the property is visible (not hidden).
677 */
678boolean_t
679zfs_prop_visible(zfs_prop_t prop)
680{
681	return (zfs_prop_table[prop].pd_visible);
682}
683
684/*
685 * Returns TRUE if the property is only allowed to be set once.
686 */
687boolean_t
688zfs_prop_setonce(zfs_prop_t prop)
689{
690	return (zfs_prop_table[prop].pd_attr == PROP_ONETIME ||
691	    zfs_prop_table[prop].pd_attr == PROP_ONETIME_DEFAULT);
692}
693
694const char *
695zfs_prop_default_string(zfs_prop_t prop)
696{
697	return (zfs_prop_table[prop].pd_strdefault);
698}
699
700uint64_t
701zfs_prop_default_numeric(zfs_prop_t prop)
702{
703	return (zfs_prop_table[prop].pd_numdefault);
704}
705
706/*
707 * Given a dataset property ID, returns the corresponding name.
708 * Assuming the zfs dataset property ID is valid.
709 */
710const char *
711zfs_prop_to_name(zfs_prop_t prop)
712{
713	return (zfs_prop_table[prop].pd_name);
714}
715
716/*
717 * Returns TRUE if the property is inheritable.
718 */
719boolean_t
720zfs_prop_inheritable(zfs_prop_t prop)
721{
722	return (zfs_prop_table[prop].pd_attr == PROP_INHERIT ||
723	    zfs_prop_table[prop].pd_attr == PROP_ONETIME);
724}
725
726/*
727 * Returns TRUE if property is one of the encryption properties that requires
728 * a loaded encryption key to modify.
729 */
730boolean_t
731zfs_prop_encryption_key_param(zfs_prop_t prop)
732{
733	/*
734	 * keylocation does not count as an encryption property. It can be
735	 * changed at will without needing the master keys.
736	 */
737	return (prop == ZFS_PROP_PBKDF2_SALT || prop == ZFS_PROP_PBKDF2_ITERS ||
738	    prop == ZFS_PROP_KEYFORMAT);
739}
740
741/*
742 * Helper function used by both kernelspace and userspace to check the
743 * keylocation property. If encrypted is set, the keylocation must be valid
744 * for an encrypted dataset.
745 */
746boolean_t
747zfs_prop_valid_keylocation(const char *str, boolean_t encrypted)
748{
749	if (strcmp("none", str) == 0)
750		return (!encrypted);
751	else if (strcmp("prompt", str) == 0)
752		return (B_TRUE);
753	else if (strlen(str) > 8 && strncmp("file:///", str, 8) == 0)
754		return (B_TRUE);
755
756	return (B_FALSE);
757}
758
759
760#ifndef _KERNEL
761
762/*
763 * Returns a string describing the set of acceptable values for the given
764 * zfs property, or NULL if it cannot be set.
765 */
766const char *
767zfs_prop_values(zfs_prop_t prop)
768{
769	return (zfs_prop_table[prop].pd_values);
770}
771
772/*
773 * Returns TRUE if this property is a string type.  Note that index types
774 * (compression, checksum) are treated as strings in userland, even though they
775 * are stored numerically on disk.
776 */
777int
778zfs_prop_is_string(zfs_prop_t prop)
779{
780	return (zfs_prop_table[prop].pd_proptype == PROP_TYPE_STRING ||
781	    zfs_prop_table[prop].pd_proptype == PROP_TYPE_INDEX);
782}
783
784/*
785 * Returns the column header for the given property.  Used only in
786 * 'zfs list -o', but centralized here with the other property information.
787 */
788const char *
789zfs_prop_column_name(zfs_prop_t prop)
790{
791	return (zfs_prop_table[prop].pd_colname);
792}
793
794/*
795 * Returns whether the given property should be displayed right-justified for
796 * 'zfs list'.
797 */
798boolean_t
799zfs_prop_align_right(zfs_prop_t prop)
800{
801	return (zfs_prop_table[prop].pd_rightalign);
802}
803
804#endif
805