Add zfs_disable_dup_eviction module option
[zfs.git] / module / zcommon / zfs_prop.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) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2011 by Delphix. All rights reserved.
24  */
25
26 /* Portions Copyright 2010 Robert Milkowski */
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 /* Note this is indexed by zfs_userquota_prop_t, keep the order the same */
49 const char *zfs_userquota_prop_prefixes[] = {
50         "userused@",
51         "userquota@",
52         "groupused@",
53         "groupquota@"
54 };
55
56 zprop_desc_t *
57 zfs_prop_get_table(void)
58 {
59         return (zfs_prop_table);
60 }
61
62 void
63 zfs_prop_init(void)
64 {
65         static zprop_index_t checksum_table[] = {
66                 { "on",         ZIO_CHECKSUM_ON },
67                 { "off",        ZIO_CHECKSUM_OFF },
68                 { "fletcher2",  ZIO_CHECKSUM_FLETCHER_2 },
69                 { "fletcher4",  ZIO_CHECKSUM_FLETCHER_4 },
70                 { "sha256",     ZIO_CHECKSUM_SHA256 },
71                 { NULL }
72         };
73
74         static zprop_index_t dedup_table[] = {
75                 { "on",         ZIO_CHECKSUM_ON },
76                 { "off",        ZIO_CHECKSUM_OFF },
77                 { "verify",     ZIO_CHECKSUM_ON | ZIO_CHECKSUM_VERIFY },
78                 { "sha256",     ZIO_CHECKSUM_SHA256 },
79                 { "sha256,verify",
80                                 ZIO_CHECKSUM_SHA256 | ZIO_CHECKSUM_VERIFY },
81                 { NULL }
82         };
83
84         static zprop_index_t compress_table[] = {
85                 { "on",         ZIO_COMPRESS_ON },
86                 { "off",        ZIO_COMPRESS_OFF },
87                 { "lzjb",       ZIO_COMPRESS_LZJB },
88                 { "gzip",       ZIO_COMPRESS_GZIP_6 },  /* gzip default */
89                 { "gzip-1",     ZIO_COMPRESS_GZIP_1 },
90                 { "gzip-2",     ZIO_COMPRESS_GZIP_2 },
91                 { "gzip-3",     ZIO_COMPRESS_GZIP_3 },
92                 { "gzip-4",     ZIO_COMPRESS_GZIP_4 },
93                 { "gzip-5",     ZIO_COMPRESS_GZIP_5 },
94                 { "gzip-6",     ZIO_COMPRESS_GZIP_6 },
95                 { "gzip-7",     ZIO_COMPRESS_GZIP_7 },
96                 { "gzip-8",     ZIO_COMPRESS_GZIP_8 },
97                 { "gzip-9",     ZIO_COMPRESS_GZIP_9 },
98                 { "zle",        ZIO_COMPRESS_ZLE },
99                 { "lz4",        ZIO_COMPRESS_LZ4 },
100                 { NULL }
101         };
102
103         static zprop_index_t snapdir_table[] = {
104                 { "hidden",     ZFS_SNAPDIR_HIDDEN },
105                 { "visible",    ZFS_SNAPDIR_VISIBLE },
106                 { NULL }
107         };
108
109         static zprop_index_t acl_inherit_table[] = {
110                 { "discard",    ZFS_ACL_DISCARD },
111                 { "noallow",    ZFS_ACL_NOALLOW },
112                 { "restricted", ZFS_ACL_RESTRICTED },
113                 { "passthrough", ZFS_ACL_PASSTHROUGH },
114                 { "secure",     ZFS_ACL_RESTRICTED }, /* bkwrd compatability */
115                 { "passthrough-x", ZFS_ACL_PASSTHROUGH_X },
116                 { NULL }
117         };
118
119         static zprop_index_t case_table[] = {
120                 { "sensitive",          ZFS_CASE_SENSITIVE },
121                 { "insensitive",        ZFS_CASE_INSENSITIVE },
122                 { "mixed",              ZFS_CASE_MIXED },
123                 { NULL }
124         };
125
126         static zprop_index_t copies_table[] = {
127                 { "1",          1 },
128                 { "2",          2 },
129                 { "3",          3 },
130                 { NULL }
131         };
132
133         /*
134          * Use the unique flags we have to send to u8_strcmp() and/or
135          * u8_textprep() to represent the various normalization property
136          * values.
137          */
138         static zprop_index_t normalize_table[] = {
139                 { "none",       0 },
140                 { "formD",      U8_TEXTPREP_NFD },
141                 { "formKC",     U8_TEXTPREP_NFKC },
142                 { "formC",      U8_TEXTPREP_NFC },
143                 { "formKD",     U8_TEXTPREP_NFKD },
144                 { NULL }
145         };
146
147         static zprop_index_t version_table[] = {
148                 { "1",          1 },
149                 { "2",          2 },
150                 { "3",          3 },
151                 { "4",          4 },
152                 { "5",          5 },
153                 { "current",    ZPL_VERSION },
154                 { NULL }
155         };
156
157         static zprop_index_t boolean_table[] = {
158                 { "off",        0 },
159                 { "on",         1 },
160                 { NULL }
161         };
162
163         static zprop_index_t logbias_table[] = {
164                 { "latency",    ZFS_LOGBIAS_LATENCY },
165                 { "throughput", ZFS_LOGBIAS_THROUGHPUT },
166                 { NULL }
167         };
168
169         static zprop_index_t canmount_table[] = {
170                 { "off",        ZFS_CANMOUNT_OFF },
171                 { "on",         ZFS_CANMOUNT_ON },
172                 { "noauto",     ZFS_CANMOUNT_NOAUTO },
173                 { NULL }
174         };
175
176         static zprop_index_t cache_table[] = {
177                 { "none",       ZFS_CACHE_NONE },
178                 { "metadata",   ZFS_CACHE_METADATA },
179                 { "all",        ZFS_CACHE_ALL },
180                 { NULL }
181         };
182
183         static zprop_index_t sync_table[] = {
184                 { "standard",   ZFS_SYNC_STANDARD },
185                 { "always",     ZFS_SYNC_ALWAYS },
186                 { "disabled",   ZFS_SYNC_DISABLED },
187                 { NULL }
188         };
189
190         static zprop_index_t xattr_table[] = {
191                 { "off",        ZFS_XATTR_OFF },
192                 { "on",         ZFS_XATTR_DIR },
193                 { "sa",         ZFS_XATTR_SA },
194                 { "dir",        ZFS_XATTR_DIR },
195                 { NULL }
196         };
197
198         /* inherit index properties */
199         zprop_register_index(ZFS_PROP_SYNC, "sync", ZFS_SYNC_STANDARD,
200             PROP_INHERIT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
201             "standard | always | disabled", "SYNC",
202             sync_table);
203         zprop_register_index(ZFS_PROP_CHECKSUM, "checksum",
204             ZIO_CHECKSUM_DEFAULT, PROP_INHERIT, ZFS_TYPE_FILESYSTEM |
205             ZFS_TYPE_VOLUME,
206             "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM",
207             checksum_table);
208         zprop_register_index(ZFS_PROP_DEDUP, "dedup", ZIO_CHECKSUM_OFF,
209             PROP_INHERIT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
210             "on | off | verify | sha256[,verify]", "DEDUP",
211             dedup_table);
212         zprop_register_index(ZFS_PROP_COMPRESSION, "compression",
213             ZIO_COMPRESS_DEFAULT, PROP_INHERIT,
214             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
215             "on | off | lzjb | gzip | gzip-[1-9] | zle | lz4", "COMPRESS",
216             compress_table);
217         zprop_register_index(ZFS_PROP_SNAPDIR, "snapdir", ZFS_SNAPDIR_HIDDEN,
218             PROP_INHERIT, ZFS_TYPE_FILESYSTEM,
219             "hidden | visible", "SNAPDIR", snapdir_table);
220         zprop_register_index(ZFS_PROP_ACLINHERIT, "aclinherit",
221             ZFS_ACL_RESTRICTED, PROP_INHERIT, ZFS_TYPE_FILESYSTEM,
222             "discard | noallow | restricted | passthrough | passthrough-x",
223             "ACLINHERIT", acl_inherit_table);
224         zprop_register_index(ZFS_PROP_COPIES, "copies", 1, PROP_INHERIT,
225             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
226             "1 | 2 | 3", "COPIES", copies_table);
227         zprop_register_index(ZFS_PROP_PRIMARYCACHE, "primarycache",
228             ZFS_CACHE_ALL, PROP_INHERIT,
229             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT | ZFS_TYPE_VOLUME,
230             "all | none | metadata", "PRIMARYCACHE", cache_table);
231         zprop_register_index(ZFS_PROP_SECONDARYCACHE, "secondarycache",
232             ZFS_CACHE_ALL, PROP_INHERIT,
233             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT | ZFS_TYPE_VOLUME,
234             "all | none | metadata", "SECONDARYCACHE", cache_table);
235         zprop_register_index(ZFS_PROP_LOGBIAS, "logbias", ZFS_LOGBIAS_LATENCY,
236             PROP_INHERIT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
237             "latency | throughput", "LOGBIAS", logbias_table);
238         zprop_register_index(ZFS_PROP_XATTR, "xattr", ZFS_XATTR_DIR,
239             PROP_INHERIT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
240             "on | off | dir | sa", "XATTR", xattr_table);
241
242         /* inherit index (boolean) properties */
243         zprop_register_index(ZFS_PROP_ATIME, "atime", 1, PROP_INHERIT,
244             ZFS_TYPE_FILESYSTEM, "on | off", "ATIME", boolean_table);
245         zprop_register_index(ZFS_PROP_DEVICES, "devices", 1, PROP_INHERIT,
246             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "DEVICES",
247             boolean_table);
248         zprop_register_index(ZFS_PROP_EXEC, "exec", 1, PROP_INHERIT,
249             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "EXEC",
250             boolean_table);
251         zprop_register_index(ZFS_PROP_SETUID, "setuid", 1, PROP_INHERIT,
252             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "SETUID",
253             boolean_table);
254         zprop_register_index(ZFS_PROP_READONLY, "readonly", 0, PROP_INHERIT,
255             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "on | off", "RDONLY",
256             boolean_table);
257         zprop_register_index(ZFS_PROP_ZONED, "zoned", 0, PROP_INHERIT,
258             ZFS_TYPE_FILESYSTEM, "on | off", "ZONED", boolean_table);
259         zprop_register_index(ZFS_PROP_VSCAN, "vscan", 0, PROP_INHERIT,
260             ZFS_TYPE_FILESYSTEM, "on | off", "VSCAN", boolean_table);
261         zprop_register_index(ZFS_PROP_NBMAND, "nbmand", 0, PROP_INHERIT,
262             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "NBMAND",
263             boolean_table);
264
265         /* default index properties */
266         zprop_register_index(ZFS_PROP_VERSION, "version", 0, PROP_DEFAULT,
267             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
268             "1 | 2 | 3 | 4 | 5 | current", "VERSION", version_table);
269         zprop_register_index(ZFS_PROP_CANMOUNT, "canmount", ZFS_CANMOUNT_ON,
270             PROP_DEFAULT, ZFS_TYPE_FILESYSTEM, "on | off | noauto",
271             "CANMOUNT", canmount_table);
272
273         /* readonly index (boolean) properties */
274         zprop_register_index(ZFS_PROP_MOUNTED, "mounted", 0, PROP_READONLY,
275             ZFS_TYPE_FILESYSTEM, "yes | no", "MOUNTED", boolean_table);
276         zprop_register_index(ZFS_PROP_DEFER_DESTROY, "defer_destroy", 0,
277             PROP_READONLY, ZFS_TYPE_SNAPSHOT, "yes | no", "DEFER_DESTROY",
278             boolean_table);
279
280         /* set once index properties */
281         zprop_register_index(ZFS_PROP_NORMALIZE, "normalization", 0,
282             PROP_ONETIME, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
283             "none | formC | formD | formKC | formKD", "NORMALIZATION",
284             normalize_table);
285         zprop_register_index(ZFS_PROP_CASE, "casesensitivity",
286             ZFS_CASE_SENSITIVE, PROP_ONETIME, ZFS_TYPE_FILESYSTEM |
287             ZFS_TYPE_SNAPSHOT,
288             "sensitive | insensitive | mixed", "CASE", case_table);
289
290         /* set once index (boolean) properties */
291         zprop_register_index(ZFS_PROP_UTF8ONLY, "utf8only", 0, PROP_ONETIME,
292             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
293             "on | off", "UTF8ONLY", boolean_table);
294
295         /* string properties */
296         zprop_register_string(ZFS_PROP_ORIGIN, "origin", NULL, PROP_READONLY,
297             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<snapshot>", "ORIGIN");
298         zprop_register_string(ZFS_PROP_CLONES, "clones", NULL, PROP_READONLY,
299             ZFS_TYPE_SNAPSHOT, "<dataset>[,...]", "CLONES");
300         zprop_register_string(ZFS_PROP_MOUNTPOINT, "mountpoint", "/",
301             PROP_INHERIT, ZFS_TYPE_FILESYSTEM, "<path> | legacy | none",
302             "MOUNTPOINT");
303         zprop_register_string(ZFS_PROP_SHARENFS, "sharenfs", "off",
304             PROP_INHERIT, ZFS_TYPE_FILESYSTEM, "on | off | share(1M) options",
305             "SHARENFS");
306         zprop_register_string(ZFS_PROP_TYPE, "type", NULL, PROP_READONLY,
307             ZFS_TYPE_DATASET, "filesystem | volume | snapshot", "TYPE");
308         zprop_register_string(ZFS_PROP_SHARESMB, "sharesmb", "off",
309             PROP_INHERIT, ZFS_TYPE_FILESYSTEM,
310             "on | off | sharemgr(1M) options", "SHARESMB");
311         zprop_register_string(ZFS_PROP_MLSLABEL, "mlslabel",
312             ZFS_MLSLABEL_DEFAULT, PROP_INHERIT, ZFS_TYPE_DATASET,
313             "<sensitivity label>", "MLSLABEL");
314
315         /* readonly number properties */
316         zprop_register_number(ZFS_PROP_USED, "used", 0, PROP_READONLY,
317             ZFS_TYPE_DATASET, "<size>", "USED");
318         zprop_register_number(ZFS_PROP_AVAILABLE, "available", 0, PROP_READONLY,
319             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "AVAIL");
320         zprop_register_number(ZFS_PROP_REFERENCED, "referenced", 0,
321             PROP_READONLY, ZFS_TYPE_DATASET, "<size>", "REFER");
322         zprop_register_number(ZFS_PROP_COMPRESSRATIO, "compressratio", 0,
323             PROP_READONLY, ZFS_TYPE_DATASET,
324             "<1.00x or higher if compressed>", "RATIO");
325         zprop_register_number(ZFS_PROP_REFRATIO, "refcompressratio", 0,
326             PROP_READONLY, ZFS_TYPE_DATASET,
327             "<1.00x or higher if compressed>", "REFRATIO");
328         zprop_register_number(ZFS_PROP_VOLBLOCKSIZE, "volblocksize",
329             ZVOL_DEFAULT_BLOCKSIZE, PROP_ONETIME,
330             ZFS_TYPE_VOLUME, "512 to 128k, power of 2", "VOLBLOCK");
331         zprop_register_number(ZFS_PROP_USEDSNAP, "usedbysnapshots", 0,
332             PROP_READONLY, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>",
333             "USEDSNAP");
334         zprop_register_number(ZFS_PROP_USEDDS, "usedbydataset", 0,
335             PROP_READONLY, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>",
336             "USEDDS");
337         zprop_register_number(ZFS_PROP_USEDCHILD, "usedbychildren", 0,
338             PROP_READONLY, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>",
339             "USEDCHILD");
340         zprop_register_number(ZFS_PROP_USEDREFRESERV, "usedbyrefreservation", 0,
341             PROP_READONLY,
342             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "USEDREFRESERV");
343         zprop_register_number(ZFS_PROP_USERREFS, "userrefs", 0, PROP_READONLY,
344             ZFS_TYPE_SNAPSHOT, "<count>", "USERREFS");
345         zprop_register_number(ZFS_PROP_WRITTEN, "written", 0, PROP_READONLY,
346             ZFS_TYPE_DATASET, "<size>", "WRITTEN");
347
348         /* default number properties */
349         zprop_register_number(ZFS_PROP_QUOTA, "quota", 0, PROP_DEFAULT,
350             ZFS_TYPE_FILESYSTEM, "<size> | none", "QUOTA");
351         zprop_register_number(ZFS_PROP_RESERVATION, "reservation", 0,
352             PROP_DEFAULT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
353             "<size> | none", "RESERV");
354         zprop_register_number(ZFS_PROP_VOLSIZE, "volsize", 0, PROP_DEFAULT,
355             ZFS_TYPE_VOLUME, "<size>", "VOLSIZE");
356         zprop_register_number(ZFS_PROP_REFQUOTA, "refquota", 0, PROP_DEFAULT,
357             ZFS_TYPE_FILESYSTEM, "<size> | none", "REFQUOTA");
358         zprop_register_number(ZFS_PROP_REFRESERVATION, "refreservation", 0,
359             PROP_DEFAULT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
360             "<size> | none", "REFRESERV");
361
362         /* inherit number properties */
363         zprop_register_number(ZFS_PROP_RECORDSIZE, "recordsize",
364             SPA_MAXBLOCKSIZE, PROP_INHERIT,
365             ZFS_TYPE_FILESYSTEM, "512 to 128k, power of 2", "RECSIZE");
366
367         /* hidden properties */
368         zprop_register_hidden(ZFS_PROP_CREATETXG, "createtxg", PROP_TYPE_NUMBER,
369             PROP_READONLY, ZFS_TYPE_DATASET, "CREATETXG");
370         zprop_register_hidden(ZFS_PROP_NUMCLONES, "numclones", PROP_TYPE_NUMBER,
371             PROP_READONLY, ZFS_TYPE_SNAPSHOT, "NUMCLONES");
372         zprop_register_hidden(ZFS_PROP_NAME, "name", PROP_TYPE_STRING,
373             PROP_READONLY, ZFS_TYPE_DATASET, "NAME");
374         zprop_register_hidden(ZFS_PROP_ISCSIOPTIONS, "iscsioptions",
375             PROP_TYPE_STRING, PROP_INHERIT, ZFS_TYPE_VOLUME, "ISCSIOPTIONS");
376         zprop_register_hidden(ZFS_PROP_STMF_SHAREINFO, "stmf_sbd_lu",
377             PROP_TYPE_STRING, PROP_INHERIT, ZFS_TYPE_VOLUME,
378             "STMF_SBD_LU");
379         zprop_register_hidden(ZFS_PROP_GUID, "guid", PROP_TYPE_NUMBER,
380             PROP_READONLY, ZFS_TYPE_DATASET, "GUID");
381         zprop_register_hidden(ZFS_PROP_USERACCOUNTING, "useraccounting",
382             PROP_TYPE_NUMBER, PROP_READONLY, ZFS_TYPE_DATASET,
383             "USERACCOUNTING");
384         zprop_register_hidden(ZFS_PROP_UNIQUE, "unique", PROP_TYPE_NUMBER,
385             PROP_READONLY, ZFS_TYPE_DATASET, "UNIQUE");
386         zprop_register_hidden(ZFS_PROP_OBJSETID, "objsetid", PROP_TYPE_NUMBER,
387             PROP_READONLY, ZFS_TYPE_DATASET, "OBJSETID");
388
389         /*
390          * Property to be removed once libbe is integrated
391          */
392         zprop_register_hidden(ZFS_PROP_PRIVATE, "priv_prop",
393             PROP_TYPE_NUMBER, PROP_READONLY, ZFS_TYPE_FILESYSTEM,
394             "PRIV_PROP");
395
396         /* oddball properties */
397         zprop_register_impl(ZFS_PROP_CREATION, "creation", PROP_TYPE_NUMBER, 0,
398             NULL, PROP_READONLY, ZFS_TYPE_DATASET,
399             "<date>", "CREATION", B_FALSE, B_TRUE, NULL);
400 }
401
402 boolean_t
403 zfs_prop_delegatable(zfs_prop_t prop)
404 {
405         zprop_desc_t *pd = &zfs_prop_table[prop];
406
407         /* The mlslabel property is never delegatable. */
408         if (prop == ZFS_PROP_MLSLABEL)
409                 return (B_FALSE);
410
411         return (pd->pd_attr != PROP_READONLY);
412 }
413
414 /*
415  * Given a zfs dataset property name, returns the corresponding property ID.
416  */
417 zfs_prop_t
418 zfs_name_to_prop(const char *propname)
419 {
420         return (zprop_name_to_prop(propname, ZFS_TYPE_DATASET));
421 }
422
423 /*
424  * For user property names, we allow all lowercase alphanumeric characters, plus
425  * a few useful punctuation characters.
426  */
427 static int
428 valid_char(char c)
429 {
430         return ((c >= 'a' && c <= 'z') ||
431             (c >= '0' && c <= '9') ||
432             c == '-' || c == '_' || c == '.' || c == ':');
433 }
434
435 /*
436  * Returns true if this is a valid user-defined property (one with a ':').
437  */
438 boolean_t
439 zfs_prop_user(const char *name)
440 {
441         int i;
442         char c;
443         boolean_t foundsep = B_FALSE;
444
445         for (i = 0; i < strlen(name); i++) {
446                 c = name[i];
447                 if (!valid_char(c))
448                         return (B_FALSE);
449                 if (c == ':')
450                         foundsep = B_TRUE;
451         }
452
453         if (!foundsep)
454                 return (B_FALSE);
455
456         return (B_TRUE);
457 }
458
459 /*
460  * Returns true if this is a valid userspace-type property (one with a '@').
461  * Note that after the @, any character is valid (eg, another @, for SID
462  * user@domain).
463  */
464 boolean_t
465 zfs_prop_userquota(const char *name)
466 {
467         zfs_userquota_prop_t prop;
468
469         for (prop = 0; prop < ZFS_NUM_USERQUOTA_PROPS; prop++) {
470                 if (strncmp(name, zfs_userquota_prop_prefixes[prop],
471                     strlen(zfs_userquota_prop_prefixes[prop])) == 0) {
472                         return (B_TRUE);
473                 }
474         }
475
476         return (B_FALSE);
477 }
478
479 /*
480  * Returns true if this is a valid written@ property.
481  * Note that after the @, any character is valid (eg, another @, for
482  * written@pool/fs@origin).
483  */
484 boolean_t
485 zfs_prop_written(const char *name)
486 {
487         static const char *prefix = "written@";
488         return (strncmp(name, prefix, strlen(prefix)) == 0);
489 }
490
491 /*
492  * Tables of index types, plus functions to convert between the user view
493  * (strings) and internal representation (uint64_t).
494  */
495 int
496 zfs_prop_string_to_index(zfs_prop_t prop, const char *string, uint64_t *index)
497 {
498         return (zprop_string_to_index(prop, string, index, ZFS_TYPE_DATASET));
499 }
500
501 int
502 zfs_prop_index_to_string(zfs_prop_t prop, uint64_t index, const char **string)
503 {
504         return (zprop_index_to_string(prop, index, string, ZFS_TYPE_DATASET));
505 }
506
507 uint64_t
508 zfs_prop_random_value(zfs_prop_t prop, uint64_t seed)
509 {
510         return (zprop_random_value(prop, seed, ZFS_TYPE_DATASET));
511 }
512
513 /*
514  * Returns TRUE if the property applies to any of the given dataset types.
515  */
516 boolean_t
517 zfs_prop_valid_for_type(int prop, zfs_type_t types)
518 {
519         return (zprop_valid_for_type(prop, types));
520 }
521
522 zprop_type_t
523 zfs_prop_get_type(zfs_prop_t prop)
524 {
525         return (zfs_prop_table[prop].pd_proptype);
526 }
527
528 /*
529  * Returns TRUE if the property is readonly.
530  */
531 boolean_t
532 zfs_prop_readonly(zfs_prop_t prop)
533 {
534         return (zfs_prop_table[prop].pd_attr == PROP_READONLY ||
535             zfs_prop_table[prop].pd_attr == PROP_ONETIME);
536 }
537
538 /*
539  * Returns TRUE if the property is only allowed to be set once.
540  */
541 boolean_t
542 zfs_prop_setonce(zfs_prop_t prop)
543 {
544         return (zfs_prop_table[prop].pd_attr == PROP_ONETIME);
545 }
546
547 const char *
548 zfs_prop_default_string(zfs_prop_t prop)
549 {
550         return (zfs_prop_table[prop].pd_strdefault);
551 }
552
553 uint64_t
554 zfs_prop_default_numeric(zfs_prop_t prop)
555 {
556         return (zfs_prop_table[prop].pd_numdefault);
557 }
558
559 /*
560  * Given a dataset property ID, returns the corresponding name.
561  * Assuming the zfs dataset property ID is valid.
562  */
563 const char *
564 zfs_prop_to_name(zfs_prop_t prop)
565 {
566         return (zfs_prop_table[prop].pd_name);
567 }
568
569 /*
570  * Returns TRUE if the property is inheritable.
571  */
572 boolean_t
573 zfs_prop_inheritable(zfs_prop_t prop)
574 {
575         return (zfs_prop_table[prop].pd_attr == PROP_INHERIT ||
576             zfs_prop_table[prop].pd_attr == PROP_ONETIME);
577 }
578
579 #ifndef _KERNEL
580
581 /*
582  * Returns a string describing the set of acceptable values for the given
583  * zfs property, or NULL if it cannot be set.
584  */
585 const char *
586 zfs_prop_values(zfs_prop_t prop)
587 {
588         return (zfs_prop_table[prop].pd_values);
589 }
590
591 /*
592  * Returns TRUE if this property is a string type.  Note that index types
593  * (compression, checksum) are treated as strings in userland, even though they
594  * are stored numerically on disk.
595  */
596 int
597 zfs_prop_is_string(zfs_prop_t prop)
598 {
599         return (zfs_prop_table[prop].pd_proptype == PROP_TYPE_STRING ||
600             zfs_prop_table[prop].pd_proptype == PROP_TYPE_INDEX);
601 }
602
603 /*
604  * Returns the column header for the given property.  Used only in
605  * 'zfs list -o', but centralized here with the other property information.
606  */
607 const char *
608 zfs_prop_column_name(zfs_prop_t prop)
609 {
610         return (zfs_prop_table[prop].pd_colname);
611 }
612
613 /*
614  * Returns whether the given property should be displayed right-justified for
615  * 'zfs list'.
616  */
617 boolean_t
618 zfs_prop_align_right(zfs_prop_t prop)
619 {
620         return (zfs_prop_table[prop].pd_rightalign);
621 }
622
623 #endif
624
625 #if defined(_KERNEL) && defined(HAVE_SPL)
626
627 static int zcommon_init(void) { return 0; }
628 static int zcommon_fini(void) { return 0; }
629
630 spl_module_init(zcommon_init);
631 spl_module_exit(zcommon_fini);
632
633 MODULE_DESCRIPTION("Generic ZFS support");
634 MODULE_AUTHOR(ZFS_META_AUTHOR);
635 MODULE_LICENSE(ZFS_META_LICENSE);
636
637 /* zfs dataset property functions */
638 EXPORT_SYMBOL(zfs_userquota_prop_prefixes);
639 EXPORT_SYMBOL(zfs_prop_init);
640 EXPORT_SYMBOL(zfs_prop_get_type);
641 EXPORT_SYMBOL(zfs_prop_get_table);
642 EXPORT_SYMBOL(zfs_prop_delegatable);
643
644 /* Dataset property functions shared between libzfs and kernel. */
645 EXPORT_SYMBOL(zfs_prop_default_string);
646 EXPORT_SYMBOL(zfs_prop_default_numeric);
647 EXPORT_SYMBOL(zfs_prop_readonly);
648 EXPORT_SYMBOL(zfs_prop_inheritable);
649 EXPORT_SYMBOL(zfs_prop_setonce);
650 EXPORT_SYMBOL(zfs_prop_to_name);
651 EXPORT_SYMBOL(zfs_name_to_prop);
652 EXPORT_SYMBOL(zfs_prop_user);
653 EXPORT_SYMBOL(zfs_prop_userquota);
654 EXPORT_SYMBOL(zfs_prop_index_to_string);
655 EXPORT_SYMBOL(zfs_prop_string_to_index);
656 EXPORT_SYMBOL(zfs_prop_valid_for_type);
657
658 #endif