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 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * Master property table. 30 * 31 * This table keeps track of all the properties supported by ZFS, and their 32 * various attributes. Not all of these are needed by the kernel, and several 33 * are only used by a single libzfs client. But having them here centralizes 34 * all property information in one location. 35 * 36 * name The human-readable string representing this property 37 * proptype Basic type (string, boolean, number) 38 * default Default value for the property. Sadly, C only allows 39 * you to initialize the first member of a union, so we 40 * have two default members for each property. 41 * attr Attributes (readonly, inheritable) for the property 42 * types Valid dataset types to which this applies 43 * values String describing acceptable values for the property 44 * colname The column header for 'zfs list' 45 * colfmt The column formatting for 'zfs list' 46 * 47 * This table must match the order of property types in libzfs.h. 48 */ 49 50 #include <sys/zio.h> 51 #include <sys/spa.h> 52 #include <sys/zfs_acl.h> 53 #include <sys/zfs_ioctl.h> 54 55 #include "zfs_prop.h" 56 57 #if defined(_KERNEL) 58 #include <sys/systm.h> 59 #else 60 #include <stdlib.h> 61 #include <string.h> 62 #include <ctype.h> 63 #endif 64 65 typedef enum { 66 prop_default, 67 prop_readonly, 68 prop_inherit 69 } prop_attr_t; 70 71 typedef struct { 72 const char *pd_name; 73 zfs_proptype_t pd_proptype; 74 uint64_t pd_numdefault; 75 const char *pd_strdefault; 76 prop_attr_t pd_attr; 77 int pd_types; 78 const char *pd_values; 79 const char *pd_colname; 80 boolean_t pd_rightalign; 81 } prop_desc_t; 82 83 static prop_desc_t zfs_prop_table[ZFS_NPROP_ALL] = { 84 { "type", prop_type_string, 0, NULL, prop_readonly, 85 ZFS_TYPE_ANY, "filesystem | volume | snapshot", "TYPE", B_TRUE }, 86 { "creation", prop_type_number, 0, NULL, prop_readonly, 87 ZFS_TYPE_ANY, "<date>", "CREATION", B_FALSE }, 88 { "used", prop_type_number, 0, NULL, prop_readonly, 89 ZFS_TYPE_ANY, "<size>", "USED", B_TRUE }, 90 { "available", prop_type_number, 0, NULL, prop_readonly, 91 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "AVAIL", B_TRUE }, 92 { "referenced", prop_type_number, 0, NULL, prop_readonly, 93 ZFS_TYPE_ANY, 94 "<size>", "REFER", B_TRUE }, 95 { "compressratio", prop_type_number, 0, NULL, prop_readonly, 96 ZFS_TYPE_ANY, "<1.00x or higher if compressed>", "RATIO", B_TRUE }, 97 { "mounted", prop_type_boolean, 0, NULL, prop_readonly, 98 ZFS_TYPE_FILESYSTEM, "yes | no | -", "MOUNTED", B_TRUE }, 99 { "origin", prop_type_string, 0, NULL, prop_readonly, 100 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<snapshot>", "ORIGIN", 101 B_FALSE }, 102 { "quota", prop_type_number, 0, NULL, prop_default, 103 ZFS_TYPE_FILESYSTEM, "<size> | none", "QUOTA", B_TRUE }, 104 { "reservation", prop_type_number, 0, NULL, prop_default, 105 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, 106 "<size> | none", "RESERV", B_TRUE }, 107 { "volsize", prop_type_number, 0, NULL, prop_default, 108 ZFS_TYPE_VOLUME, "<size>", "VOLSIZE", B_TRUE }, 109 { "volblocksize", prop_type_number, 8192, NULL, prop_readonly, 110 ZFS_TYPE_VOLUME, "512 to 128k, power of 2", "VOLBLOCK", B_TRUE }, 111 { "recordsize", prop_type_number, SPA_MAXBLOCKSIZE, NULL, 112 prop_inherit, 113 ZFS_TYPE_FILESYSTEM, 114 "512 to 128k, power of 2", "RECSIZE", B_TRUE }, 115 { "mountpoint", prop_type_string, 0, "/", prop_inherit, 116 ZFS_TYPE_FILESYSTEM, 117 "<path> | legacy | none", "MOUNTPOINT", B_FALSE }, 118 { "sharenfs", prop_type_string, 0, "off", prop_inherit, 119 ZFS_TYPE_FILESYSTEM, 120 "on | off | share(1M) options", "SHARENFS", B_FALSE }, 121 { "checksum", prop_type_index, ZIO_CHECKSUM_DEFAULT, "on", 122 prop_inherit, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, 123 "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM", B_TRUE }, 124 { "compression", prop_type_index, ZIO_COMPRESS_DEFAULT, "off", 125 prop_inherit, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, 126 "on | off | lzjb", "COMPRESS", B_TRUE }, 127 { "atime", prop_type_boolean, 1, NULL, prop_inherit, 128 ZFS_TYPE_FILESYSTEM, 129 "on | off", "ATIME", B_TRUE }, 130 { "devices", prop_type_boolean, 1, NULL, prop_inherit, 131 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, 132 "on | off", "DEVICES", B_TRUE }, 133 { "exec", prop_type_boolean, 1, NULL, prop_inherit, 134 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, 135 "on | off", "EXEC", B_TRUE }, 136 { "setuid", prop_type_boolean, 1, NULL, prop_inherit, 137 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "SETUID", 138 B_TRUE }, 139 { "readonly", prop_type_boolean, 0, NULL, prop_inherit, 140 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, 141 "on | off", "RDONLY", B_TRUE }, 142 { "zoned", prop_type_boolean, 0, NULL, prop_inherit, 143 ZFS_TYPE_FILESYSTEM, 144 "on | off", "ZONED", B_TRUE }, 145 { "snapdir", prop_type_index, ZFS_SNAPDIR_HIDDEN, "hidden", 146 prop_inherit, 147 ZFS_TYPE_FILESYSTEM, 148 "hidden | visible", "SNAPDIR", B_TRUE }, 149 { "aclmode", prop_type_index, ZFS_ACL_GROUPMASK, "groupmask", 150 prop_inherit, ZFS_TYPE_FILESYSTEM, 151 "discard | groupmask | passthrough", "ACLMODE", B_TRUE }, 152 { "aclinherit", prop_type_index, ZFS_ACL_SECURE, "secure", 153 prop_inherit, ZFS_TYPE_FILESYSTEM, 154 "discard | noallow | secure | passthrough", "ACLINHERIT", B_TRUE }, 155 { "canmount", prop_type_boolean, 1, NULL, prop_default, 156 ZFS_TYPE_FILESYSTEM, 157 "on | off", "CANMOUNT", B_TRUE }, 158 { "createtxg", prop_type_number, 0, NULL, prop_readonly, 159 ZFS_TYPE_ANY, NULL, NULL, B_FALSE}, 160 { "name", prop_type_string, 0, NULL, prop_readonly, 161 ZFS_TYPE_ANY, NULL, "NAME", B_FALSE }, 162 }; 163 164 zfs_proptype_t 165 zfs_prop_get_type(zfs_prop_t prop) 166 { 167 return (zfs_prop_table[prop].pd_proptype); 168 } 169 170 static boolean_t 171 propname_match(const char *p, zfs_prop_t prop, size_t len) 172 { 173 const char *propname = zfs_prop_table[prop].pd_name; 174 #ifndef _KERNEL 175 const char *colname = zfs_prop_table[prop].pd_colname; 176 int c; 177 #endif 178 179 #ifndef _KERNEL 180 if (colname == NULL) 181 return (B_FALSE); 182 #endif 183 184 if (len == strlen(propname) && 185 strncmp(p, propname, len) == 0) 186 return (B_TRUE); 187 188 #ifndef _KERNEL 189 if (len != strlen(colname)) 190 return (B_FALSE); 191 192 for (c = 0; c < len; c++) 193 if (p[c] != tolower(colname[c])) 194 break; 195 196 return (colname[c] == '\0'); 197 #else 198 return (B_FALSE); 199 #endif 200 } 201 202 203 /* 204 * Given a property name, returns the corresponding property ID. 205 */ 206 zfs_prop_t 207 zfs_name_to_prop(const char *propname) 208 { 209 int i; 210 211 for (i = 0; i < ZFS_NPROP_ALL; i++) { 212 if (propname_match(propname, i, strlen(propname))) 213 return (i); 214 } 215 216 return (ZFS_PROP_INVAL); 217 } 218 219 /* 220 * For user property names, we allow all lowercase alphanumeric characters, plus 221 * a few useful punctuation characters. 222 */ 223 static int 224 valid_char(char c) 225 { 226 return ((c >= 'a' && c <= 'z') || 227 (c >= '0' && c <= '9') || 228 c == '-' || c == '_' || c == '.' || c == ':'); 229 } 230 231 /* 232 * Returns true if this is a valid user-defined property (one with a ':'). 233 */ 234 boolean_t 235 zfs_prop_user(const char *name) 236 { 237 int i; 238 char c; 239 boolean_t foundsep = B_FALSE; 240 241 for (i = 0; i < strlen(name); i++) { 242 c = name[i]; 243 if (!valid_char(c)) 244 return (B_FALSE); 245 if (c == ':') 246 foundsep = B_TRUE; 247 } 248 249 if (!foundsep) 250 return (B_FALSE); 251 252 return (B_TRUE); 253 } 254 255 /* 256 * Return the default value for the given property. 257 */ 258 const char * 259 zfs_prop_default_string(zfs_prop_t prop) 260 { 261 return (zfs_prop_table[prop].pd_strdefault); 262 } 263 264 uint64_t 265 zfs_prop_default_numeric(zfs_prop_t prop) 266 { 267 return (zfs_prop_table[prop].pd_numdefault); 268 } 269 270 /* 271 * Returns TRUE if the property is readonly. 272 */ 273 int 274 zfs_prop_readonly(zfs_prop_t prop) 275 { 276 return (zfs_prop_table[prop].pd_attr == prop_readonly); 277 } 278 279 /* 280 * Given a property ID, returns the corresponding name. 281 */ 282 const char * 283 zfs_prop_to_name(zfs_prop_t prop) 284 { 285 return (zfs_prop_table[prop].pd_name); 286 } 287 288 /* 289 * Returns TRUE if the property is inheritable. 290 */ 291 int 292 zfs_prop_inheritable(zfs_prop_t prop) 293 { 294 return (zfs_prop_table[prop].pd_attr == prop_inherit); 295 } 296 297 typedef struct zfs_index { 298 const char *name; 299 uint64_t index; 300 } zfs_index_t; 301 302 static zfs_index_t checksum_table[] = { 303 { "on", ZIO_CHECKSUM_ON }, 304 { "off", ZIO_CHECKSUM_OFF }, 305 { "fletcher2", ZIO_CHECKSUM_FLETCHER_2 }, 306 { "fletcher4", ZIO_CHECKSUM_FLETCHER_4 }, 307 { "sha256", ZIO_CHECKSUM_SHA256 }, 308 { NULL } 309 }; 310 311 static zfs_index_t compress_table[] = { 312 { "on", ZIO_COMPRESS_ON }, 313 { "off", ZIO_COMPRESS_OFF }, 314 { "lzjb", ZIO_COMPRESS_LZJB }, 315 { NULL } 316 }; 317 318 static zfs_index_t snapdir_table[] = { 319 { "hidden", ZFS_SNAPDIR_HIDDEN }, 320 { "visible", ZFS_SNAPDIR_VISIBLE }, 321 { NULL } 322 }; 323 324 static zfs_index_t acl_mode_table[] = { 325 { "discard", ZFS_ACL_DISCARD }, 326 { "groupmask", ZFS_ACL_GROUPMASK }, 327 { "passthrough", ZFS_ACL_PASSTHROUGH }, 328 { NULL } 329 }; 330 331 static zfs_index_t acl_inherit_table[] = { 332 { "discard", ZFS_ACL_DISCARD }, 333 { "noallow", ZFS_ACL_NOALLOW }, 334 { "secure", ZFS_ACL_SECURE }, 335 { "passthrough", ZFS_ACL_PASSTHROUGH }, 336 { NULL } 337 }; 338 339 static zfs_index_t * 340 zfs_prop_index_table(zfs_prop_t prop) 341 { 342 switch (prop) { 343 case ZFS_PROP_CHECKSUM: 344 return (checksum_table); 345 case ZFS_PROP_COMPRESSION: 346 return (compress_table); 347 case ZFS_PROP_SNAPDIR: 348 return (snapdir_table); 349 case ZFS_PROP_ACLMODE: 350 return (acl_mode_table); 351 case ZFS_PROP_ACLINHERIT: 352 return (acl_inherit_table); 353 default: 354 return (NULL); 355 } 356 } 357 358 359 /* 360 * Tables of index types, plus functions to convert between the user view 361 * (strings) and internal representation (uint64_t). 362 */ 363 int 364 zfs_prop_string_to_index(zfs_prop_t prop, const char *string, uint64_t *index) 365 { 366 zfs_index_t *table; 367 int i; 368 369 if ((table = zfs_prop_index_table(prop)) == NULL) 370 return (-1); 371 372 for (i = 0; table[i].name != NULL; i++) { 373 if (strcmp(string, table[i].name) == 0) { 374 *index = table[i].index; 375 return (0); 376 } 377 } 378 379 return (-1); 380 } 381 382 int 383 zfs_prop_index_to_string(zfs_prop_t prop, uint64_t index, const char **string) 384 { 385 zfs_index_t *table; 386 int i; 387 388 if ((table = zfs_prop_index_table(prop)) == NULL) 389 return (-1); 390 391 for (i = 0; table[i].name != NULL; i++) { 392 if (table[i].index == index) { 393 *string = table[i].name; 394 return (0); 395 } 396 } 397 398 return (-1); 399 } 400 401 #ifndef _KERNEL 402 403 /* 404 * Returns TRUE if the property applies to the given dataset types. 405 */ 406 int 407 zfs_prop_valid_for_type(zfs_prop_t prop, int types) 408 { 409 return ((zfs_prop_table[prop].pd_types & types) != 0); 410 } 411 412 /* 413 * Returns a string describing the set of acceptable values for the given 414 * property, or NULL if it cannot be set. 415 */ 416 const char * 417 zfs_prop_values(zfs_prop_t prop) 418 { 419 return (zfs_prop_table[prop].pd_values); 420 } 421 422 /* 423 * Returns TRUE if this property is a string type. Note that index types 424 * (compression, checksum) are treated as strings in userland, even though they 425 * are stored numerically on disk. 426 */ 427 int 428 zfs_prop_is_string(zfs_prop_t prop) 429 { 430 return (zfs_prop_table[prop].pd_proptype == prop_type_string || 431 zfs_prop_table[prop].pd_proptype == prop_type_index); 432 } 433 434 /* 435 * Returns the column header for the given property. Used only in 436 * 'zfs list -o', but centralized here with the other property information. 437 */ 438 const char * 439 zfs_prop_column_name(zfs_prop_t prop) 440 { 441 return (zfs_prop_table[prop].pd_colname); 442 } 443 444 /* 445 * Returns whether the given property should be displayed right-justified for 446 * 'zfs list'. 447 */ 448 boolean_t 449 zfs_prop_align_right(zfs_prop_t prop) 450 { 451 return (zfs_prop_table[prop].pd_rightalign); 452 } 453 454 /* 455 * Determines the minimum width for the column, and indicates whether it's fixed 456 * or not. Only string columns are non-fixed. 457 */ 458 size_t 459 zfs_prop_width(zfs_prop_t prop, boolean_t *fixed) 460 { 461 prop_desc_t *pd = &zfs_prop_table[prop]; 462 zfs_index_t *idx; 463 size_t ret; 464 int i; 465 466 *fixed = B_TRUE; 467 468 /* 469 * Start with the width of the column name. 470 */ 471 ret = strlen(pd->pd_colname); 472 473 /* 474 * For fixed-width values, make sure the width is large enough to hold 475 * any possible value. 476 */ 477 switch (pd->pd_proptype) { 478 case prop_type_number: 479 /* 480 * The maximum length of a human-readable number is 5 characters 481 * ("20.4M", for example). 482 */ 483 if (ret < 5) 484 ret = 5; 485 /* 486 * 'creation' is handled specially because it's a number 487 * internally, but displayed as a date string. 488 */ 489 if (prop == ZFS_PROP_CREATION) 490 *fixed = B_FALSE; 491 break; 492 case prop_type_boolean: 493 /* 494 * The maximum length of a boolean value is 3 characters, for 495 * "off". 496 */ 497 if (ret < 3) 498 ret = 3; 499 break; 500 case prop_type_index: 501 idx = zfs_prop_index_table(prop); 502 for (i = 0; idx[i].name != NULL; i++) { 503 if (strlen(idx[i].name) > ret) 504 ret = strlen(idx[i].name); 505 } 506 break; 507 508 case prop_type_string: 509 *fixed = B_FALSE; 510 break; 511 } 512 513 return (ret); 514 } 515 516 #endif 517