1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5906d120cSlling * Common Development and Distribution License (the "License"). 6906d120cSlling * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 227f7322feSeschrock * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27fa9e4066Sahrens 28fa9e4066Sahrens /* 29fa9e4066Sahrens * Master property table. 30fa9e4066Sahrens * 31fa9e4066Sahrens * This table keeps track of all the properties supported by ZFS, and their 32fa9e4066Sahrens * various attributes. Not all of these are needed by the kernel, and several 33fa9e4066Sahrens * are only used by a single libzfs client. But having them here centralizes 34fa9e4066Sahrens * all property information in one location. 35fa9e4066Sahrens * 36fa9e4066Sahrens * name The human-readable string representing this property 37fa9e4066Sahrens * proptype Basic type (string, boolean, number) 38fa9e4066Sahrens * default Default value for the property. Sadly, C only allows 39fa9e4066Sahrens * you to initialize the first member of a union, so we 40fa9e4066Sahrens * have two default members for each property. 41fa9e4066Sahrens * attr Attributes (readonly, inheritable) for the property 42fa9e4066Sahrens * types Valid dataset types to which this applies 43fa9e4066Sahrens * values String describing acceptable values for the property 44fa9e4066Sahrens * colname The column header for 'zfs list' 45fa9e4066Sahrens * colfmt The column formatting for 'zfs list' 46fa9e4066Sahrens * 47fa9e4066Sahrens * This table must match the order of property types in libzfs.h. 48fa9e4066Sahrens */ 49fa9e4066Sahrens 50fa9e4066Sahrens #include <sys/zio.h> 51fa9e4066Sahrens #include <sys/spa.h> 52fa9e4066Sahrens #include <sys/zfs_acl.h> 53fa9e4066Sahrens #include <sys/zfs_ioctl.h> 54fa9e4066Sahrens 55fa9e4066Sahrens #include "zfs_prop.h" 56fa9e4066Sahrens 57fa9e4066Sahrens #if defined(_KERNEL) 58fa9e4066Sahrens #include <sys/systm.h> 59fa9e4066Sahrens #else 60fa9e4066Sahrens #include <stdlib.h> 61fa9e4066Sahrens #include <string.h> 62fa9e4066Sahrens #include <ctype.h> 63fa9e4066Sahrens #endif 64fa9e4066Sahrens 65fa9e4066Sahrens typedef enum { 66fa9e4066Sahrens prop_default, 67fa9e4066Sahrens prop_readonly, 68fa9e4066Sahrens prop_inherit 69fa9e4066Sahrens } prop_attr_t; 70fa9e4066Sahrens 71fa9e4066Sahrens typedef struct { 72fa9e4066Sahrens const char *pd_name; 73fa9e4066Sahrens zfs_proptype_t pd_proptype; 74fa9e4066Sahrens uint64_t pd_numdefault; 75fa9e4066Sahrens const char *pd_strdefault; 76fa9e4066Sahrens prop_attr_t pd_attr; 77fa9e4066Sahrens int pd_types; 78fa9e4066Sahrens const char *pd_values; 79fa9e4066Sahrens const char *pd_colname; 80fa9e4066Sahrens const char *pd_colfmt; 81fa9e4066Sahrens } prop_desc_t; 82fa9e4066Sahrens 83fa9e4066Sahrens static prop_desc_t zfs_prop_table[ZFS_NPROP_ALL] = { 84fa9e4066Sahrens { "type", prop_type_string, 0, NULL, prop_readonly, 85fa9e4066Sahrens ZFS_TYPE_ANY, "filesystem | volume | snapshot", "TYPE", "%10s" }, 86fa9e4066Sahrens { "creation", prop_type_number, 0, NULL, prop_readonly, 87fa9e4066Sahrens ZFS_TYPE_ANY, "<date>", "CREATION", "%-20s" }, 88fa9e4066Sahrens { "used", prop_type_number, 0, NULL, prop_readonly, 89fa9e4066Sahrens ZFS_TYPE_ANY, "<size>", "USED", "%5s" }, 90fa9e4066Sahrens { "available", prop_type_number, 0, NULL, prop_readonly, 91fa9e4066Sahrens ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "AVAIL", "%5s" }, 92fa9e4066Sahrens { "referenced", prop_type_number, 0, NULL, prop_readonly, 934f75c27eSeschrock ZFS_TYPE_ANY, 94fa9e4066Sahrens "<size>", "REFER", "%5s" }, 95fa9e4066Sahrens { "compressratio", prop_type_number, 0, NULL, prop_readonly, 96fa9e4066Sahrens ZFS_TYPE_ANY, "<1.00x or higher if compressed>", "RATIO", "%5s" }, 97fa9e4066Sahrens { "mounted", prop_type_boolean, 0, NULL, prop_readonly, 98fa9e4066Sahrens ZFS_TYPE_FILESYSTEM, "yes | no | -", "MOUNTED", "%7s" }, 99fa9e4066Sahrens { "origin", prop_type_string, 0, NULL, prop_readonly, 100101f5531Slling ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<snapshot>", "ORIGIN", 101101f5531Slling "%-20s" }, 102fa9e4066Sahrens { "quota", prop_type_number, 0, NULL, prop_default, 103fa9e4066Sahrens ZFS_TYPE_FILESYSTEM, "<size> | none", "QUOTA", "%5s" }, 104fa9e4066Sahrens { "reservation", prop_type_number, 0, NULL, prop_default, 105fa9e4066Sahrens ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, 106fa9e4066Sahrens "<size> | none", "RESERV", "%6s" }, 107fa9e4066Sahrens { "volsize", prop_type_number, 0, NULL, prop_default, 108fa9e4066Sahrens ZFS_TYPE_VOLUME, "<size>", "VOLSIZE", "%7s" }, 1094f75c27eSeschrock { "volblocksize", prop_type_number, 8192, NULL, prop_readonly, 110fa9e4066Sahrens ZFS_TYPE_VOLUME, "512 to 128k, power of 2", "VOLBLOCK", "%8s" }, 111fa9e4066Sahrens { "recordsize", prop_type_number, SPA_MAXBLOCKSIZE, NULL, 112fa9e4066Sahrens prop_inherit, 1134f75c27eSeschrock ZFS_TYPE_FILESYSTEM, 114fa9e4066Sahrens "512 to 128k, power of 2", "RECSIZE", "%7s" }, 115fa9e4066Sahrens { "mountpoint", prop_type_string, 0, "/", prop_inherit, 116fa9e4066Sahrens ZFS_TYPE_FILESYSTEM, 117fa9e4066Sahrens "<path> | legacy | none", "MOUNTPOINT", "%-20s" }, 118fa9e4066Sahrens { "sharenfs", prop_type_string, 0, "off", prop_inherit, 119fa9e4066Sahrens ZFS_TYPE_FILESYSTEM, 120fa9e4066Sahrens "on | off | share(1M) options", "SHARENFS", "%-15s" }, 121101f5531Slling { "checksum", prop_type_index, ZIO_CHECKSUM_DEFAULT, "on", 1224f75c27eSeschrock prop_inherit, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, 123fa9e4066Sahrens "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM", "%10s" }, 124101f5531Slling { "compression", prop_type_index, ZIO_COMPRESS_DEFAULT, "off", 1254f75c27eSeschrock prop_inherit, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, 126fa9e4066Sahrens "on | off | lzjb", "COMPRESS", "%8s" }, 127fa9e4066Sahrens { "atime", prop_type_boolean, 1, NULL, prop_inherit, 128fa9e4066Sahrens ZFS_TYPE_FILESYSTEM, 129fa9e4066Sahrens "on | off", "ATIME", "%5s" }, 130fa9e4066Sahrens { "devices", prop_type_boolean, 1, NULL, prop_inherit, 131*12054bfcSnd150628 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, 132fa9e4066Sahrens "on | off", "DEVICES", "%7s" }, 133fa9e4066Sahrens { "exec", prop_type_boolean, 1, NULL, prop_inherit, 134*12054bfcSnd150628 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, 135fa9e4066Sahrens "on | off", "EXEC", "%4s" }, 136fa9e4066Sahrens { "setuid", prop_type_boolean, 1, NULL, prop_inherit, 137*12054bfcSnd150628 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "SETUID", 138*12054bfcSnd150628 "%6s" }, 139fa9e4066Sahrens { "readonly", prop_type_boolean, 0, NULL, prop_inherit, 1404f75c27eSeschrock ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, 1414f75c27eSeschrock "on | off", "RDONLY", "%6s" }, 142fa9e4066Sahrens { "zoned", prop_type_boolean, 0, NULL, prop_inherit, 1434f75c27eSeschrock ZFS_TYPE_FILESYSTEM, 144fa9e4066Sahrens "on | off", "ZONED", "%5s" }, 145906d120cSlling { "snapdir", prop_type_index, ZFS_SNAPDIR_HIDDEN, "hidden", 146a0965f35Sbonwick prop_inherit, 147fa9e4066Sahrens ZFS_TYPE_FILESYSTEM, 148fa9e4066Sahrens "hidden | visible", "SNAPDIR", "%7s" }, 149101f5531Slling { "aclmode", prop_type_index, GROUPMASK, "groupmask", prop_inherit, 150fa9e4066Sahrens ZFS_TYPE_FILESYSTEM, 151fa9e4066Sahrens "discard | groupmask | passthrough", "ACLMODE", "%11s" }, 152101f5531Slling { "aclinherit", prop_type_index, SECURE, "secure", prop_inherit, 153fa9e4066Sahrens ZFS_TYPE_FILESYSTEM, 154fa9e4066Sahrens "discard | noallow | secure | passthrough", "ACLINHERIT", "%11s" }, 155fa9e4066Sahrens { "createtxg", prop_type_number, 0, NULL, prop_readonly, 156fa9e4066Sahrens ZFS_TYPE_ANY, NULL, NULL, NULL}, 157fa9e4066Sahrens { "name", prop_type_string, 0, NULL, prop_readonly, 158fa9e4066Sahrens ZFS_TYPE_ANY, 159fa9e4066Sahrens NULL, "NAME", "%-20s" }, 160fa9e4066Sahrens }; 161fa9e4066Sahrens 162fa9e4066Sahrens zfs_proptype_t 163fa9e4066Sahrens zfs_prop_get_type(zfs_prop_t prop) 164fa9e4066Sahrens { 165fa9e4066Sahrens return (zfs_prop_table[prop].pd_proptype); 166fa9e4066Sahrens } 167fa9e4066Sahrens 1685c709891Seschrock static int 1695c709891Seschrock propname_match(const char *p, int prop, size_t len) 1705c709891Seschrock { 1715c709891Seschrock const char *propname = zfs_prop_table[prop].pd_name; 1725c709891Seschrock #ifndef _KERNEL 1735c709891Seschrock const char *colname = zfs_prop_table[prop].pd_colname; 1745c709891Seschrock int c; 1755c709891Seschrock #endif 1765c709891Seschrock 1773bb79becSeschrock #ifndef _KERNEL 1783bb79becSeschrock if (colname == NULL) 1793bb79becSeschrock return (0); 1803bb79becSeschrock #endif 1813bb79becSeschrock 1825c709891Seschrock if (len == strlen(propname) && 1835c709891Seschrock strncmp(p, propname, len) == 0) 1845c709891Seschrock return (1); 1855c709891Seschrock 1865c709891Seschrock #ifndef _KERNEL 1873bb79becSeschrock if (len != strlen(colname)) 1885c709891Seschrock return (0); 1895c709891Seschrock 1905c709891Seschrock for (c = 0; c < len; c++) 1915c709891Seschrock if (p[c] != tolower(colname[c])) 1925c709891Seschrock break; 1935c709891Seschrock 1945c709891Seschrock return (colname[c] == '\0'); 1955c709891Seschrock #else 1965c709891Seschrock return (0); 1975c709891Seschrock #endif 1985c709891Seschrock } 1995c709891Seschrock 2005c709891Seschrock 201fa9e4066Sahrens /* 202fa9e4066Sahrens * Given a property name, returns the corresponding property ID. 203fa9e4066Sahrens */ 204fa9e4066Sahrens zfs_prop_t 205fa9e4066Sahrens zfs_name_to_prop(const char *propname) 206fa9e4066Sahrens { 207fa9e4066Sahrens int i; 208fa9e4066Sahrens 209fa9e4066Sahrens for (i = 0; i < ZFS_NPROP_ALL; i++) { 2105c709891Seschrock if (propname_match(propname, i, strlen(propname))) 211fa9e4066Sahrens return (i); 212fa9e4066Sahrens } 213fa9e4066Sahrens 214fa9e4066Sahrens return (ZFS_PROP_INVAL); 215fa9e4066Sahrens } 216fa9e4066Sahrens 217fa9e4066Sahrens /* 218fa9e4066Sahrens * Return the default value for the given property. 219fa9e4066Sahrens */ 2207f7322feSeschrock const char * 2217f7322feSeschrock zfs_prop_default_string(zfs_prop_t prop) 222fa9e4066Sahrens { 2237f7322feSeschrock return (zfs_prop_table[prop].pd_strdefault); 224fa9e4066Sahrens } 225fa9e4066Sahrens 226fa9e4066Sahrens uint64_t 227fa9e4066Sahrens zfs_prop_default_numeric(zfs_prop_t prop) 228fa9e4066Sahrens { 229fa9e4066Sahrens return (zfs_prop_table[prop].pd_numdefault); 230fa9e4066Sahrens } 231fa9e4066Sahrens 232fa9e4066Sahrens /* 233fa9e4066Sahrens * Returns TRUE if the property is readonly. 234fa9e4066Sahrens */ 235fa9e4066Sahrens int 236fa9e4066Sahrens zfs_prop_readonly(zfs_prop_t prop) 237fa9e4066Sahrens { 238fa9e4066Sahrens return (zfs_prop_table[prop].pd_attr == prop_readonly); 239fa9e4066Sahrens } 240fa9e4066Sahrens 241fa9e4066Sahrens #ifndef _KERNEL 242fa9e4066Sahrens /* 243fa9e4066Sahrens * Given a property ID, returns the corresponding name. 244fa9e4066Sahrens */ 245fa9e4066Sahrens const char * 246fa9e4066Sahrens zfs_prop_to_name(zfs_prop_t prop) 247fa9e4066Sahrens { 248fa9e4066Sahrens return (zfs_prop_table[prop].pd_name); 249fa9e4066Sahrens } 250fa9e4066Sahrens 251fa9e4066Sahrens /* 252fa9e4066Sahrens * Returns TRUE if the property is inheritable. 253fa9e4066Sahrens */ 254fa9e4066Sahrens int 255fa9e4066Sahrens zfs_prop_inheritable(zfs_prop_t prop) 256fa9e4066Sahrens { 257fa9e4066Sahrens return (zfs_prop_table[prop].pd_attr == prop_inherit); 258fa9e4066Sahrens } 259fa9e4066Sahrens 260fa9e4066Sahrens /* 261fa9e4066Sahrens * Returns TRUE if the property applies to the given dataset types. 262fa9e4066Sahrens */ 263fa9e4066Sahrens int 264fa9e4066Sahrens zfs_prop_valid_for_type(zfs_prop_t prop, int types) 265fa9e4066Sahrens { 266fa9e4066Sahrens return ((zfs_prop_table[prop].pd_types & types) != 0); 267fa9e4066Sahrens } 268fa9e4066Sahrens 269fa9e4066Sahrens /* 270fa9e4066Sahrens * Returns a string describing the set of acceptable values for the given 271fa9e4066Sahrens * property, or NULL if it cannot be set. 272fa9e4066Sahrens */ 273fa9e4066Sahrens const char * 274fa9e4066Sahrens zfs_prop_values(zfs_prop_t prop) 275fa9e4066Sahrens { 276fa9e4066Sahrens return (zfs_prop_table[prop].pd_values); 277fa9e4066Sahrens } 278fa9e4066Sahrens 279fa9e4066Sahrens /* 280fa9e4066Sahrens * Returns TRUE if this property is a string type. Note that index types 281fa9e4066Sahrens * (compression, checksum) are treated as strings in userland, even though they 282fa9e4066Sahrens * are stored numerically on disk. 283fa9e4066Sahrens */ 284fa9e4066Sahrens int 285fa9e4066Sahrens zfs_prop_is_string(zfs_prop_t prop) 286fa9e4066Sahrens { 287fa9e4066Sahrens return (zfs_prop_table[prop].pd_proptype == prop_type_string || 288fa9e4066Sahrens zfs_prop_table[prop].pd_proptype == prop_type_index); 289fa9e4066Sahrens } 290fa9e4066Sahrens 291fa9e4066Sahrens /* 292fa9e4066Sahrens * Returns the column header for the given property. Used only in 293fa9e4066Sahrens * 'zfs list -o', but centralized here with the other property information. 294fa9e4066Sahrens */ 295fa9e4066Sahrens const char * 296fa9e4066Sahrens zfs_prop_column_name(zfs_prop_t prop) 297fa9e4066Sahrens { 298fa9e4066Sahrens return (zfs_prop_table[prop].pd_colname); 299fa9e4066Sahrens } 300fa9e4066Sahrens 301fa9e4066Sahrens /* 302fa9e4066Sahrens * Returns the column formatting for the given property. Used only in 303fa9e4066Sahrens * 'zfs list -o', but centralized here with the other property information. 304fa9e4066Sahrens */ 305fa9e4066Sahrens const char * 306fa9e4066Sahrens zfs_prop_column_format(zfs_prop_t prop) 307fa9e4066Sahrens { 308fa9e4066Sahrens return (zfs_prop_table[prop].pd_colfmt); 309fa9e4066Sahrens } 310fa9e4066Sahrens 311fa9e4066Sahrens /* 31207ba0419Seschrock * Given a comma-separated list of fields, fills in the specified array with a 31307ba0419Seschrock * list of properties. The keyword "all" can be used to specify all properties. 31407ba0419Seschrock * The 'count' parameter returns the number of matching properties placed into 31507ba0419Seschrock * the list. The 'props' parameter must point to an array of at least 31607ba0419Seschrock * ZFS_NPROP_ALL elements. 317fa9e4066Sahrens */ 31807ba0419Seschrock int 31907ba0419Seschrock zfs_get_proplist(char *fields, zfs_prop_t *props, int max, 32007ba0419Seschrock int *count, char **badopt) 321fa9e4066Sahrens { 322fa9e4066Sahrens int i; 32307ba0419Seschrock size_t len; 32407ba0419Seschrock char *s, *p; 325fa9e4066Sahrens 32607ba0419Seschrock *count = 0; 327fa9e4066Sahrens 32807ba0419Seschrock /* 32907ba0419Seschrock * Check for the special value "all", which indicates all properties 33007ba0419Seschrock * should be displayed. 33107ba0419Seschrock */ 33207ba0419Seschrock if (strcmp(fields, "all") == 0) { 33307ba0419Seschrock if (max < ZFS_NPROP_VISIBLE) 33407ba0419Seschrock return (EOVERFLOW); 335fa9e4066Sahrens 33607ba0419Seschrock for (i = 0; i < ZFS_NPROP_VISIBLE; i++) 33707ba0419Seschrock props[i] = i; 33807ba0419Seschrock *count = ZFS_NPROP_VISIBLE; 33907ba0419Seschrock return (0); 340fa9e4066Sahrens } 341fa9e4066Sahrens 342fa9e4066Sahrens /* 34307ba0419Seschrock * It would be nice to use getsubopt() here, but the inclusion of column 34407ba0419Seschrock * aliases makes this more effort than it's worth. 345fa9e4066Sahrens */ 34607ba0419Seschrock s = fields; 34707ba0419Seschrock while (*s != '\0') { 34807ba0419Seschrock if ((p = strchr(s, ',')) == NULL) { 34907ba0419Seschrock len = strlen(s); 35007ba0419Seschrock p = s + len; 351fa9e4066Sahrens } else { 35207ba0419Seschrock len = p - s; 35307ba0419Seschrock } 35407ba0419Seschrock 35507ba0419Seschrock /* 35607ba0419Seschrock * Check for empty options. 35707ba0419Seschrock */ 35807ba0419Seschrock if (len == 0) { 35907ba0419Seschrock *badopt = ""; 36007ba0419Seschrock return (EINVAL); 36107ba0419Seschrock } 36207ba0419Seschrock 36307ba0419Seschrock /* 36407ba0419Seschrock * Check all regular property names. 36507ba0419Seschrock */ 36607ba0419Seschrock for (i = 0; i < ZFS_NPROP_ALL; i++) { 3675c709891Seschrock if (propname_match(s, i, len)) 36807ba0419Seschrock break; 36907ba0419Seschrock } 37007ba0419Seschrock 37107ba0419Seschrock /* 37207ba0419Seschrock * If no column is specified, then return failure, setting 37307ba0419Seschrock * 'badopt' to point to the invalid option. 37407ba0419Seschrock */ 37507ba0419Seschrock if (i == ZFS_NPROP_ALL) { 37607ba0419Seschrock s[len] = '\0'; 37707ba0419Seschrock *badopt = s; 37807ba0419Seschrock return (EINVAL); 37907ba0419Seschrock } 380fa9e4066Sahrens 38107ba0419Seschrock /* 38207ba0419Seschrock * If the user specified too many options (by using the same one 38307ba0419Seschrock * multiple times). return an error with 'badopt' set to NULL to 38407ba0419Seschrock * indicate this condition. 38507ba0419Seschrock */ 38607ba0419Seschrock if (*count == max) 38707ba0419Seschrock return (EOVERFLOW); 388fa9e4066Sahrens 38907ba0419Seschrock props[*count] = i; 39007ba0419Seschrock *count += 1; 39107ba0419Seschrock 39207ba0419Seschrock s = p; 39307ba0419Seschrock if (*s == ',') 39407ba0419Seschrock s++; 39507ba0419Seschrock } 39607ba0419Seschrock 39707ba0419Seschrock /* 39807ba0419Seschrock * If no fields were specified, return an error. 39907ba0419Seschrock */ 40007ba0419Seschrock if (*count == 0) { 40107ba0419Seschrock *badopt = ""; 40207ba0419Seschrock return (EINVAL); 40307ba0419Seschrock } 40407ba0419Seschrock 40507ba0419Seschrock return (0); 406fa9e4066Sahrens } 407fa9e4066Sahrens 408fa9e4066Sahrens #endif 409