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  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
  25  */
  26 
  27 /* Portions Copyright 2010 Robert Milkowski */
  28 
  29 #include <sys/zio.h>
  30 #include <sys/spa.h>
  31 #include <sys/u8_textprep.h>
  32 #include <sys/zfs_acl.h>
  33 #include <sys/zfs_ioctl.h>
  34 #include <sys/zfs_znode.h>
  35 
  36 #include "zfs_prop.h"
  37 #include "zfs_deleg.h"
  38 
  39 #if defined(_KERNEL)
  40 #include <sys/systm.h>
  41 #else
  42 #include <stdlib.h>
  43 #include <string.h>
  44 #include <ctype.h>
  45 #endif
  46 
  47 static zprop_desc_t zfs_prop_table[ZFS_NUM_PROPS];
  48 
  49 /* Note this is indexed by zfs_userquota_prop_t, keep the order the same */
  50 const char *zfs_userquota_prop_prefixes[] = {
  51         "userused@",
  52         "userquota@",
  53         "groupused@",
  54         "groupquota@"
  55 };
  56 
  57 zprop_desc_t *
  58 zfs_prop_get_table(void)
  59 {
  60         return (zfs_prop_table);
  61 }
  62 
  63 void
  64 zfs_prop_init(void)
  65 {
  66         static zprop_index_t checksum_table[] = {
  67                 { "on",         ZIO_CHECKSUM_ON },
  68                 { "off",        ZIO_CHECKSUM_OFF },
  69                 { "fletcher2",  ZIO_CHECKSUM_FLETCHER_2 },
  70                 { "fletcher4",  ZIO_CHECKSUM_FLETCHER_4 },
  71                 { "sha256",     ZIO_CHECKSUM_SHA256 },
  72                 { NULL }
  73         };
  74 
  75         static zprop_index_t dedup_table[] = {
  76                 { "on",         ZIO_CHECKSUM_ON },
  77                 { "off",        ZIO_CHECKSUM_OFF },
  78                 { "verify",     ZIO_CHECKSUM_ON | ZIO_CHECKSUM_VERIFY },
  79                 { "sha256",     ZIO_CHECKSUM_SHA256 },
  80                 { "sha256,verify",
  81                                 ZIO_CHECKSUM_SHA256 | ZIO_CHECKSUM_VERIFY },
  82                 { NULL }
  83         };
  84 
  85         static zprop_index_t compress_table[] = {
  86                 { "on",         ZIO_COMPRESS_ON },
  87                 { "off",        ZIO_COMPRESS_OFF },
  88                 { "lzjb",       ZIO_COMPRESS_LZJB },
  89                 { "gzip",       ZIO_COMPRESS_GZIP_6 },  /* gzip default */
  90                 { "gzip-1",     ZIO_COMPRESS_GZIP_1 },
  91                 { "gzip-2",     ZIO_COMPRESS_GZIP_2 },
  92                 { "gzip-3",     ZIO_COMPRESS_GZIP_3 },
  93                 { "gzip-4",     ZIO_COMPRESS_GZIP_4 },
  94                 { "gzip-5",     ZIO_COMPRESS_GZIP_5 },
  95                 { "gzip-6",     ZIO_COMPRESS_GZIP_6 },
  96                 { "gzip-7",     ZIO_COMPRESS_GZIP_7 },
  97                 { "gzip-8",     ZIO_COMPRESS_GZIP_8 },
  98                 { "gzip-9",     ZIO_COMPRESS_GZIP_9 },
  99                 { "zle",        ZIO_COMPRESS_ZLE },
 100                 { "lz4",        ZIO_COMPRESS_LZ4 },
 101                 { "lz4hc",      ZIO_COMPRESS_LZ4HC },
 102                 { NULL }
 103         };
 104 
 105         static zprop_index_t snapdir_table[] = {
 106                 { "hidden",     ZFS_SNAPDIR_HIDDEN },
 107                 { "visible",    ZFS_SNAPDIR_VISIBLE },
 108                 { NULL }
 109         };
 110 
 111         static zprop_index_t acl_mode_table[] = {
 112                 { "discard",    ZFS_ACL_DISCARD },
 113                 { "groupmask",  ZFS_ACL_GROUPMASK },
 114                 { "passthrough", ZFS_ACL_PASSTHROUGH },
 115                 { "restricted", ZFS_ACL_RESTRICTED },
 116                 { NULL }
 117         };
 118 
 119         static zprop_index_t acl_inherit_table[] = {
 120                 { "discard",    ZFS_ACL_DISCARD },
 121                 { "noallow",    ZFS_ACL_NOALLOW },
 122                 { "restricted", ZFS_ACL_RESTRICTED },
 123                 { "passthrough", ZFS_ACL_PASSTHROUGH },
 124                 { "secure",     ZFS_ACL_RESTRICTED }, /* bkwrd compatability */
 125                 { "passthrough-x", ZFS_ACL_PASSTHROUGH_X },
 126                 { NULL }
 127         };
 128 
 129         static zprop_index_t case_table[] = {
 130                 { "sensitive",          ZFS_CASE_SENSITIVE },
 131                 { "insensitive",        ZFS_CASE_INSENSITIVE },
 132                 { "mixed",              ZFS_CASE_MIXED },
 133                 { NULL }
 134         };
 135 
 136         static zprop_index_t copies_table[] = {
 137                 { "1",          1 },
 138                 { "2",          2 },
 139                 { "3",          3 },
 140                 { NULL }
 141         };
 142 
 143         /*
 144          * Use the unique flags we have to send to u8_strcmp() and/or
 145          * u8_textprep() to represent the various normalization property
 146          * values.
 147          */
 148         static zprop_index_t normalize_table[] = {
 149                 { "none",       0 },
 150                 { "formD",      U8_TEXTPREP_NFD },
 151                 { "formKC",     U8_TEXTPREP_NFKC },
 152                 { "formC",      U8_TEXTPREP_NFC },
 153                 { "formKD",     U8_TEXTPREP_NFKD },
 154                 { NULL }
 155         };
 156 
 157         static zprop_index_t version_table[] = {
 158                 { "1",          1 },
 159                 { "2",          2 },
 160                 { "3",          3 },
 161                 { "4",          4 },
 162                 { "5",          5 },
 163                 { "current",    ZPL_VERSION },
 164                 { NULL }
 165         };
 166 
 167         static zprop_index_t boolean_table[] = {
 168                 { "off",        0 },
 169                 { "on",         1 },
 170                 { NULL }
 171         };
 172 
 173         static zprop_index_t logbias_table[] = {
 174                 { "latency",    ZFS_LOGBIAS_LATENCY },
 175                 { "throughput", ZFS_LOGBIAS_THROUGHPUT },
 176                 { NULL }
 177         };
 178 
 179         static zprop_index_t canmount_table[] = {
 180                 { "off",        ZFS_CANMOUNT_OFF },
 181                 { "on",         ZFS_CANMOUNT_ON },
 182                 { "noauto",     ZFS_CANMOUNT_NOAUTO },
 183                 { NULL }
 184         };
 185 
 186         static zprop_index_t cache_table[] = {
 187                 { "none",       ZFS_CACHE_NONE },
 188                 { "metadata",   ZFS_CACHE_METADATA },
 189                 { "all",        ZFS_CACHE_ALL },
 190                 { NULL }
 191         };
 192 
 193         static zprop_index_t sync_table[] = {
 194                 { "standard",   ZFS_SYNC_STANDARD },
 195                 { "always",     ZFS_SYNC_ALWAYS },
 196                 { "disabled",   ZFS_SYNC_DISABLED },
 197                 { NULL }
 198         };
 199 
 200         /* inherit index properties */
 201         zprop_register_index(ZFS_PROP_SYNC, "sync", ZFS_SYNC_STANDARD,
 202             PROP_INHERIT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
 203             "standard | always | disabled", "SYNC",
 204             sync_table);
 205         zprop_register_index(ZFS_PROP_CHECKSUM, "checksum",
 206             ZIO_CHECKSUM_DEFAULT, PROP_INHERIT, ZFS_TYPE_FILESYSTEM |
 207             ZFS_TYPE_VOLUME,
 208             "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM",
 209             checksum_table);
 210         zprop_register_index(ZFS_PROP_DEDUP, "dedup", ZIO_CHECKSUM_OFF,
 211             PROP_INHERIT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
 212             "on | off | verify | sha256[,verify]", "DEDUP",
 213             dedup_table);
 214         zprop_register_index(ZFS_PROP_COMPRESSION, "compression",
 215             ZIO_COMPRESS_DEFAULT, PROP_INHERIT,
 216             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
 217             "on | off | lzjb | gzip | gzip-[1-9] | zle | lz4 | lz4hc",
 218             "COMPRESS", compress_table);
 219         zprop_register_index(ZFS_PROP_SNAPDIR, "snapdir", ZFS_SNAPDIR_HIDDEN,
 220             PROP_INHERIT, ZFS_TYPE_FILESYSTEM,
 221             "hidden | visible", "SNAPDIR", snapdir_table);
 222         zprop_register_index(ZFS_PROP_ACLMODE, "aclmode", ZFS_ACL_DISCARD,
 223             PROP_INHERIT, ZFS_TYPE_FILESYSTEM,
 224             "discard | groupmask | passthrough | restricted", "ACLMODE",
 225             acl_mode_table);
 226         zprop_register_index(ZFS_PROP_ACLINHERIT, "aclinherit",
 227             ZFS_ACL_RESTRICTED, PROP_INHERIT, ZFS_TYPE_FILESYSTEM,
 228             "discard | noallow | restricted | passthrough | passthrough-x",
 229             "ACLINHERIT", acl_inherit_table);
 230         zprop_register_index(ZFS_PROP_COPIES, "copies", 1, PROP_INHERIT,
 231             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
 232             "1 | 2 | 3", "COPIES", copies_table);
 233         zprop_register_index(ZFS_PROP_PRIMARYCACHE, "primarycache",
 234             ZFS_CACHE_ALL, PROP_INHERIT,
 235             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT | ZFS_TYPE_VOLUME,
 236             "all | none | metadata", "PRIMARYCACHE", cache_table);
 237         zprop_register_index(ZFS_PROP_SECONDARYCACHE, "secondarycache",
 238             ZFS_CACHE_ALL, PROP_INHERIT,
 239             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT | ZFS_TYPE_VOLUME,
 240             "all | none | metadata", "SECONDARYCACHE", cache_table);
 241         zprop_register_index(ZFS_PROP_LOGBIAS, "logbias", ZFS_LOGBIAS_LATENCY,
 242             PROP_INHERIT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
 243             "latency | throughput", "LOGBIAS", logbias_table);
 244 
 245         /* inherit index (boolean) properties */
 246         zprop_register_index(ZFS_PROP_ATIME, "atime", 1, PROP_INHERIT,
 247             ZFS_TYPE_FILESYSTEM, "on | off", "ATIME", boolean_table);
 248         zprop_register_index(ZFS_PROP_DEVICES, "devices", 1, PROP_INHERIT,
 249             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "DEVICES",
 250             boolean_table);
 251         zprop_register_index(ZFS_PROP_EXEC, "exec", 1, PROP_INHERIT,
 252             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "EXEC",
 253             boolean_table);
 254         zprop_register_index(ZFS_PROP_SETUID, "setuid", 1, PROP_INHERIT,
 255             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "SETUID",
 256             boolean_table);
 257         zprop_register_index(ZFS_PROP_READONLY, "readonly", 0, PROP_INHERIT,
 258             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "on | off", "RDONLY",
 259             boolean_table);
 260         zprop_register_index(ZFS_PROP_ZONED, "zoned", 0, PROP_INHERIT,
 261             ZFS_TYPE_FILESYSTEM, "on | off", "ZONED", boolean_table);
 262         zprop_register_index(ZFS_PROP_XATTR, "xattr", 1, PROP_INHERIT,
 263             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "XATTR",
 264             boolean_table);
 265         zprop_register_index(ZFS_PROP_VSCAN, "vscan", 0, PROP_INHERIT,
 266             ZFS_TYPE_FILESYSTEM, "on | off", "VSCAN",
 267             boolean_table);
 268         zprop_register_index(ZFS_PROP_NBMAND, "nbmand", 0, PROP_INHERIT,
 269             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "NBMAND",
 270             boolean_table);
 271 
 272         /* default index properties */
 273         zprop_register_index(ZFS_PROP_VERSION, "version", 0, PROP_DEFAULT,
 274             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
 275             "1 | 2 | 3 | 4 | 5 | current", "VERSION", version_table);
 276         zprop_register_index(ZFS_PROP_CANMOUNT, "canmount", ZFS_CANMOUNT_ON,
 277             PROP_DEFAULT, ZFS_TYPE_FILESYSTEM, "on | off | noauto",
 278             "CANMOUNT", canmount_table);
 279 
 280         /* readonly index (boolean) properties */
 281         zprop_register_index(ZFS_PROP_MOUNTED, "mounted", 0, PROP_READONLY,
 282             ZFS_TYPE_FILESYSTEM, "yes | no", "MOUNTED", boolean_table);
 283         zprop_register_index(ZFS_PROP_DEFER_DESTROY, "defer_destroy", 0,
 284             PROP_READONLY, ZFS_TYPE_SNAPSHOT, "yes | no", "DEFER_DESTROY",
 285             boolean_table);
 286 
 287         /* set once index properties */
 288         zprop_register_index(ZFS_PROP_NORMALIZE, "normalization", 0,
 289             PROP_ONETIME, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
 290             "none | formC | formD | formKC | formKD", "NORMALIZATION",
 291             normalize_table);
 292         zprop_register_index(ZFS_PROP_CASE, "casesensitivity",
 293             ZFS_CASE_SENSITIVE, PROP_ONETIME, ZFS_TYPE_FILESYSTEM |
 294             ZFS_TYPE_SNAPSHOT,
 295             "sensitive | insensitive | mixed", "CASE", case_table);
 296 
 297         /* set once index (boolean) properties */
 298         zprop_register_index(ZFS_PROP_UTF8ONLY, "utf8only", 0, PROP_ONETIME,
 299             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT,
 300             "on | off", "UTF8ONLY", boolean_table);
 301 
 302         /* string properties */
 303         zprop_register_string(ZFS_PROP_ORIGIN, "origin", NULL, PROP_READONLY,
 304             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<snapshot>", "ORIGIN");
 305         zprop_register_string(ZFS_PROP_CLONES, "clones", NULL, PROP_READONLY,
 306             ZFS_TYPE_SNAPSHOT, "<dataset>[,...]", "CLONES");
 307         zprop_register_string(ZFS_PROP_MOUNTPOINT, "mountpoint", "/",
 308             PROP_INHERIT, ZFS_TYPE_FILESYSTEM, "<path> | legacy | none",
 309             "MOUNTPOINT");
 310         zprop_register_string(ZFS_PROP_SHARENFS, "sharenfs", "off",
 311             PROP_INHERIT, ZFS_TYPE_FILESYSTEM, "on | off | share(1M) options",
 312             "SHARENFS");
 313         zprop_register_string(ZFS_PROP_TYPE, "type", NULL, PROP_READONLY,
 314             ZFS_TYPE_DATASET, "filesystem | volume | snapshot", "TYPE");
 315         zprop_register_string(ZFS_PROP_SHARESMB, "sharesmb", "off",
 316             PROP_INHERIT, ZFS_TYPE_FILESYSTEM,
 317             "on | off | sharemgr(1M) options", "SHARESMB");
 318         zprop_register_string(ZFS_PROP_MLSLABEL, "mlslabel",
 319             ZFS_MLSLABEL_DEFAULT, PROP_INHERIT, ZFS_TYPE_DATASET,
 320             "<sensitivity label>", "MLSLABEL");
 321 
 322         /* readonly number properties */
 323         zprop_register_number(ZFS_PROP_USED, "used", 0, PROP_READONLY,
 324             ZFS_TYPE_DATASET, "<size>", "USED");
 325         zprop_register_number(ZFS_PROP_AVAILABLE, "available", 0, PROP_READONLY,
 326             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "AVAIL");
 327         zprop_register_number(ZFS_PROP_REFERENCED, "referenced", 0,
 328             PROP_READONLY, ZFS_TYPE_DATASET, "<size>", "REFER");
 329         zprop_register_number(ZFS_PROP_COMPRESSRATIO, "compressratio", 0,
 330             PROP_READONLY, ZFS_TYPE_DATASET,
 331             "<1.00x or higher if compressed>", "RATIO");
 332         zprop_register_number(ZFS_PROP_REFRATIO, "refcompressratio", 0,
 333             PROP_READONLY, ZFS_TYPE_DATASET,
 334             "<1.00x or higher if compressed>", "REFRATIO");
 335         zprop_register_number(ZFS_PROP_VOLBLOCKSIZE, "volblocksize",
 336             ZVOL_DEFAULT_BLOCKSIZE, PROP_ONETIME,
 337             ZFS_TYPE_VOLUME, "512 to 128k, power of 2", "VOLBLOCK");
 338         zprop_register_number(ZFS_PROP_USEDSNAP, "usedbysnapshots", 0,
 339             PROP_READONLY, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>",
 340             "USEDSNAP");
 341         zprop_register_number(ZFS_PROP_USEDDS, "usedbydataset", 0,
 342             PROP_READONLY, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>",
 343             "USEDDS");
 344         zprop_register_number(ZFS_PROP_USEDCHILD, "usedbychildren", 0,
 345             PROP_READONLY, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>",
 346             "USEDCHILD");
 347         zprop_register_number(ZFS_PROP_USEDREFRESERV, "usedbyrefreservation", 0,
 348             PROP_READONLY,
 349             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "USEDREFRESERV");
 350         zprop_register_number(ZFS_PROP_USERREFS, "userrefs", 0, PROP_READONLY,
 351             ZFS_TYPE_SNAPSHOT, "<count>", "USERREFS");
 352         zprop_register_number(ZFS_PROP_WRITTEN, "written", 0, PROP_READONLY,
 353             ZFS_TYPE_DATASET, "<size>", "WRITTEN");
 354 
 355         /* default number properties */
 356         zprop_register_number(ZFS_PROP_QUOTA, "quota", 0, PROP_DEFAULT,
 357             ZFS_TYPE_FILESYSTEM, "<size> | none", "QUOTA");
 358         zprop_register_number(ZFS_PROP_RESERVATION, "reservation", 0,
 359             PROP_DEFAULT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
 360             "<size> | none", "RESERV");
 361         zprop_register_number(ZFS_PROP_VOLSIZE, "volsize", 0, PROP_DEFAULT,
 362             ZFS_TYPE_VOLUME, "<size>", "VOLSIZE");
 363         zprop_register_number(ZFS_PROP_REFQUOTA, "refquota", 0, PROP_DEFAULT,
 364             ZFS_TYPE_FILESYSTEM, "<size> | none", "REFQUOTA");
 365         zprop_register_number(ZFS_PROP_REFRESERVATION, "refreservation", 0,
 366             PROP_DEFAULT, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME,
 367             "<size> | none", "REFRESERV");
 368 
 369         /* inherit number properties */
 370         zprop_register_number(ZFS_PROP_RECORDSIZE, "recordsize",
 371             SPA_MAXBLOCKSIZE, PROP_INHERIT,
 372             ZFS_TYPE_FILESYSTEM, "512 to 128k, power of 2", "RECSIZE");
 373 
 374         /* hidden properties */
 375         zprop_register_hidden(ZFS_PROP_CREATETXG, "createtxg", PROP_TYPE_NUMBER,
 376             PROP_READONLY, ZFS_TYPE_DATASET, "CREATETXG");
 377         zprop_register_hidden(ZFS_PROP_NUMCLONES, "numclones", PROP_TYPE_NUMBER,
 378             PROP_READONLY, ZFS_TYPE_SNAPSHOT, "NUMCLONES");
 379         zprop_register_hidden(ZFS_PROP_NAME, "name", PROP_TYPE_STRING,
 380             PROP_READONLY, ZFS_TYPE_DATASET, "NAME");
 381         zprop_register_hidden(ZFS_PROP_ISCSIOPTIONS, "iscsioptions",
 382             PROP_TYPE_STRING, PROP_INHERIT, ZFS_TYPE_VOLUME, "ISCSIOPTIONS");
 383         zprop_register_hidden(ZFS_PROP_STMF_SHAREINFO, "stmf_sbd_lu",
 384             PROP_TYPE_STRING, PROP_INHERIT, ZFS_TYPE_VOLUME,
 385             "STMF_SBD_LU");
 386         zprop_register_hidden(ZFS_PROP_GUID, "guid", PROP_TYPE_NUMBER,
 387             PROP_READONLY, ZFS_TYPE_DATASET, "GUID");
 388         zprop_register_hidden(ZFS_PROP_USERACCOUNTING, "useraccounting",
 389             PROP_TYPE_NUMBER, PROP_READONLY, ZFS_TYPE_DATASET,
 390             "USERACCOUNTING");
 391         zprop_register_hidden(ZFS_PROP_UNIQUE, "unique", PROP_TYPE_NUMBER,
 392             PROP_READONLY, ZFS_TYPE_DATASET, "UNIQUE");
 393         zprop_register_hidden(ZFS_PROP_OBJSETID, "objsetid", PROP_TYPE_NUMBER,
 394             PROP_READONLY, ZFS_TYPE_DATASET, "OBJSETID");
 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