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 2007 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 boolean_t pd_visible; 82 } prop_desc_t; 83 84 static prop_desc_t zfs_prop_table[] = { 85 { "type", prop_type_string, 0, NULL, prop_readonly, 86 ZFS_TYPE_ANY, "filesystem | volume | snapshot", "TYPE", B_TRUE, 87 B_TRUE }, 88 { "creation", prop_type_number, 0, NULL, prop_readonly, 89 ZFS_TYPE_ANY, "<date>", "CREATION", B_FALSE, B_TRUE }, 90 { "used", prop_type_number, 0, NULL, prop_readonly, 91 ZFS_TYPE_ANY, "<size>", "USED", B_TRUE, B_TRUE }, 92 { "available", prop_type_number, 0, NULL, prop_readonly, 93 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "AVAIL", B_TRUE, 94 B_TRUE }, 95 { "referenced", prop_type_number, 0, NULL, prop_readonly, 96 ZFS_TYPE_ANY, 97 "<size>", "REFER", B_TRUE, B_TRUE }, 98 { "compressratio", prop_type_number, 0, NULL, prop_readonly, 99 ZFS_TYPE_ANY, "<1.00x or higher if compressed>", "RATIO", B_TRUE, 100 B_TRUE }, 101 { "mounted", prop_type_boolean, 0, NULL, prop_readonly, 102 ZFS_TYPE_FILESYSTEM, "yes | no | -", "MOUNTED", B_TRUE, B_TRUE }, 103 { "origin", prop_type_string, 0, NULL, prop_readonly, 104 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<snapshot>", "ORIGIN", 105 B_FALSE, B_TRUE }, 106 { "quota", prop_type_number, 0, NULL, prop_default, 107 ZFS_TYPE_FILESYSTEM, "<size> | none", "QUOTA", B_TRUE, B_TRUE }, 108 { "reservation", prop_type_number, 0, NULL, prop_default, 109 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, 110 "<size> | none", "RESERV", B_TRUE, B_TRUE }, 111 { "volsize", prop_type_number, 0, NULL, prop_default, 112 ZFS_TYPE_VOLUME, "<size>", "VOLSIZE", B_TRUE, B_TRUE }, 113 { "volblocksize", prop_type_number, 8192, NULL, prop_readonly, 114 ZFS_TYPE_VOLUME, "512 to 128k, power of 2", "VOLBLOCK", B_TRUE, 115 B_TRUE }, 116 { "recordsize", prop_type_number, SPA_MAXBLOCKSIZE, NULL, 117 prop_inherit, 118 ZFS_TYPE_FILESYSTEM, 119 "512 to 128k, power of 2", "RECSIZE", B_TRUE, B_TRUE }, 120 { "mountpoint", prop_type_string, 0, "/", prop_inherit, 121 ZFS_TYPE_FILESYSTEM, 122 "<path> | legacy | none", "MOUNTPOINT", B_FALSE, B_TRUE }, 123 { "sharenfs", prop_type_string, 0, "off", prop_inherit, 124 ZFS_TYPE_FILESYSTEM, 125 "on | off | share(1M) options", "SHARENFS", B_FALSE, B_TRUE }, 126 { "checksum", prop_type_index, ZIO_CHECKSUM_DEFAULT, "on", 127 prop_inherit, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, 128 "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM", B_TRUE, 129 B_TRUE }, 130 { "compression", prop_type_index, ZIO_COMPRESS_DEFAULT, "off", 131 prop_inherit, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, 132 "on | off | lzjb | gzip | gzip-[1-9]", "COMPRESS", B_TRUE, B_TRUE }, 133 { "atime", prop_type_boolean, 1, NULL, prop_inherit, 134 ZFS_TYPE_FILESYSTEM, 135 "on | off", "ATIME", B_TRUE, B_TRUE }, 136 { "devices", prop_type_boolean, 1, NULL, prop_inherit, 137 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, 138 "on | off", "DEVICES", B_TRUE, B_TRUE }, 139 { "exec", prop_type_boolean, 1, NULL, prop_inherit, 140 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, 141 "on | off", "EXEC", B_TRUE, B_TRUE }, 142 { "setuid", prop_type_boolean, 1, NULL, prop_inherit, 143 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, "on | off", "SETUID", 144 B_TRUE, B_TRUE }, 145 { "readonly", prop_type_boolean, 0, NULL, prop_inherit, 146 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, 147 "on | off", "RDONLY", B_TRUE, B_TRUE }, 148 { "zoned", prop_type_boolean, 0, NULL, prop_inherit, 149 ZFS_TYPE_FILESYSTEM, 150 "on | off", "ZONED", B_TRUE, B_TRUE }, 151 { "snapdir", prop_type_index, ZFS_SNAPDIR_HIDDEN, "hidden", 152 prop_inherit, 153 ZFS_TYPE_FILESYSTEM, 154 "hidden | visible", "SNAPDIR", B_TRUE, B_TRUE }, 155 { "aclmode", prop_type_index, ZFS_ACL_GROUPMASK, "groupmask", 156 prop_inherit, ZFS_TYPE_FILESYSTEM, 157 "discard | groupmask | passthrough", "ACLMODE", B_TRUE, B_TRUE }, 158 { "aclinherit", prop_type_index, ZFS_ACL_SECURE, "secure", 159 prop_inherit, ZFS_TYPE_FILESYSTEM, 160 "discard | noallow | secure | passthrough", "ACLINHERIT", B_TRUE, 161 B_TRUE }, 162 { "createtxg", prop_type_number, 0, NULL, prop_readonly, 163 ZFS_TYPE_ANY, NULL, NULL, B_FALSE, B_FALSE }, 164 { "name", prop_type_string, 0, NULL, prop_readonly, 165 ZFS_TYPE_ANY, NULL, "NAME", B_FALSE, B_FALSE }, 166 { "canmount", prop_type_boolean, 1, NULL, prop_default, 167 ZFS_TYPE_FILESYSTEM, 168 "on | off", "CANMOUNT", B_TRUE, B_TRUE }, 169 { "shareiscsi", prop_type_string, 0, "off", prop_inherit, 170 ZFS_TYPE_ANY, 171 "on | off | type=<type>", "SHAREISCSI", B_FALSE, B_TRUE }, 172 { "iscsioptions", prop_type_string, 0, NULL, prop_inherit, 173 ZFS_TYPE_VOLUME, NULL, "ISCSIOPTIONS", B_FALSE, B_FALSE }, 174 { "xattr", prop_type_boolean, 1, NULL, prop_inherit, 175 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, 176 "on | off", "XATTR", B_TRUE, B_TRUE }, 177 { "numclones", prop_type_number, 0, NULL, prop_readonly, 178 ZFS_TYPE_SNAPSHOT, NULL, NULL, B_FALSE, B_FALSE }, 179 { "copies", prop_type_index, 1, "1", prop_inherit, 180 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, 181 "1 | 2 | 3", "COPIES", B_TRUE, B_TRUE }, 182 }; 183 184 #define ZFS_PROP_COUNT ((sizeof (zfs_prop_table))/(sizeof (prop_desc_t))) 185 186 zfs_proptype_t 187 zfs_prop_get_type(zfs_prop_t prop) 188 { 189 return (zfs_prop_table[prop].pd_proptype); 190 } 191 192 static boolean_t 193 propname_match(const char *p, zfs_prop_t prop, size_t len) 194 { 195 const char *propname = zfs_prop_table[prop].pd_name; 196 #ifndef _KERNEL 197 const char *colname = zfs_prop_table[prop].pd_colname; 198 int c; 199 #endif 200 201 #ifndef _KERNEL 202 if (colname == NULL) 203 return (B_FALSE); 204 #endif 205 206 if (len == strlen(propname) && 207 strncmp(p, propname, len) == 0) 208 return (B_TRUE); 209 210 #ifndef _KERNEL 211 if (len != strlen(colname)) 212 return (B_FALSE); 213 214 for (c = 0; c < len; c++) 215 if (p[c] != tolower(colname[c])) 216 break; 217 218 return (colname[c] == '\0'); 219 #else 220 return (B_FALSE); 221 #endif 222 } 223 224 zfs_prop_t 225 zfs_name_to_prop_cb(zfs_prop_t prop, void *cb_data) 226 { 227 const char *propname = cb_data; 228 229 if (propname_match(propname, prop, strlen(propname))) 230 return (prop); 231 232 return (ZFS_PROP_CONT); 233 } 234 235 /* 236 * Given a property name, returns the corresponding property ID. 237 */ 238 zfs_prop_t 239 zfs_name_to_prop(const char *propname) 240 { 241 zfs_prop_t prop; 242 243 prop = zfs_prop_iter(zfs_name_to_prop_cb, (void *)propname, B_TRUE); 244 return (prop == ZFS_PROP_CONT ? ZFS_PROP_INVAL : prop); 245 } 246 247 /* 248 * For user property names, we allow all lowercase alphanumeric characters, plus 249 * a few useful punctuation characters. 250 */ 251 static int 252 valid_char(char c) 253 { 254 return ((c >= 'a' && c <= 'z') || 255 (c >= '0' && c <= '9') || 256 c == '-' || c == '_' || c == '.' || c == ':'); 257 } 258 259 /* 260 * Returns true if this is a valid user-defined property (one with a ':'). 261 */ 262 boolean_t 263 zfs_prop_user(const char *name) 264 { 265 int i; 266 char c; 267 boolean_t foundsep = B_FALSE; 268 269 for (i = 0; i < strlen(name); i++) { 270 c = name[i]; 271 if (!valid_char(c)) 272 return (B_FALSE); 273 if (c == ':') 274 foundsep = B_TRUE; 275 } 276 277 if (!foundsep) 278 return (B_FALSE); 279 280 return (B_TRUE); 281 } 282 283 /* 284 * Return the default value for the given property. 285 */ 286 const char * 287 zfs_prop_default_string(zfs_prop_t prop) 288 { 289 return (zfs_prop_table[prop].pd_strdefault); 290 } 291 292 uint64_t 293 zfs_prop_default_numeric(zfs_prop_t prop) 294 { 295 return (zfs_prop_table[prop].pd_numdefault); 296 } 297 298 /* 299 * Returns TRUE if the property is readonly. 300 */ 301 int 302 zfs_prop_readonly(zfs_prop_t prop) 303 { 304 return (zfs_prop_table[prop].pd_attr == prop_readonly); 305 } 306 307 /* 308 * Given a property ID, returns the corresponding name. 309 */ 310 const char * 311 zfs_prop_to_name(zfs_prop_t prop) 312 { 313 return (zfs_prop_table[prop].pd_name); 314 } 315 316 /* 317 * Returns TRUE if the property is inheritable. 318 */ 319 int 320 zfs_prop_inheritable(zfs_prop_t prop) 321 { 322 return (zfs_prop_table[prop].pd_attr == prop_inherit); 323 } 324 325 typedef struct zfs_index { 326 const char *name; 327 uint64_t index; 328 } zfs_index_t; 329 330 static zfs_index_t checksum_table[] = { 331 { "on", ZIO_CHECKSUM_ON }, 332 { "off", ZIO_CHECKSUM_OFF }, 333 { "fletcher2", ZIO_CHECKSUM_FLETCHER_2 }, 334 { "fletcher4", ZIO_CHECKSUM_FLETCHER_4 }, 335 { "sha256", ZIO_CHECKSUM_SHA256 }, 336 { NULL } 337 }; 338 339 static zfs_index_t compress_table[] = { 340 { "on", ZIO_COMPRESS_ON }, 341 { "off", ZIO_COMPRESS_OFF }, 342 { "lzjb", ZIO_COMPRESS_LZJB }, 343 { "gzip", ZIO_COMPRESS_GZIP_6 }, /* the default gzip level */ 344 { "gzip-1", ZIO_COMPRESS_GZIP_1 }, 345 { "gzip-2", ZIO_COMPRESS_GZIP_2 }, 346 { "gzip-3", ZIO_COMPRESS_GZIP_3 }, 347 { "gzip-4", ZIO_COMPRESS_GZIP_4 }, 348 { "gzip-5", ZIO_COMPRESS_GZIP_5 }, 349 { "gzip-6", ZIO_COMPRESS_GZIP_6 }, 350 { "gzip-7", ZIO_COMPRESS_GZIP_7 }, 351 { "gzip-8", ZIO_COMPRESS_GZIP_8 }, 352 { "gzip-9", ZIO_COMPRESS_GZIP_9 }, 353 { NULL } 354 }; 355 356 static zfs_index_t snapdir_table[] = { 357 { "hidden", ZFS_SNAPDIR_HIDDEN }, 358 { "visible", ZFS_SNAPDIR_VISIBLE }, 359 { NULL } 360 }; 361 362 static zfs_index_t acl_mode_table[] = { 363 { "discard", ZFS_ACL_DISCARD }, 364 { "groupmask", ZFS_ACL_GROUPMASK }, 365 { "passthrough", ZFS_ACL_PASSTHROUGH }, 366 { NULL } 367 }; 368 369 static zfs_index_t acl_inherit_table[] = { 370 { "discard", ZFS_ACL_DISCARD }, 371 { "noallow", ZFS_ACL_NOALLOW }, 372 { "secure", ZFS_ACL_SECURE }, 373 { "passthrough", ZFS_ACL_PASSTHROUGH }, 374 { NULL } 375 }; 376 377 static zfs_index_t copies_table[] = { 378 { "1", 1 }, 379 { "2", 2 }, 380 { "3", 3 }, 381 { NULL } 382 }; 383 384 static zfs_index_t * 385 zfs_prop_index_table(zfs_prop_t prop) 386 { 387 switch (prop) { 388 case ZFS_PROP_CHECKSUM: 389 return (checksum_table); 390 case ZFS_PROP_COMPRESSION: 391 return (compress_table); 392 case ZFS_PROP_SNAPDIR: 393 return (snapdir_table); 394 case ZFS_PROP_ACLMODE: 395 return (acl_mode_table); 396 case ZFS_PROP_ACLINHERIT: 397 return (acl_inherit_table); 398 case ZFS_PROP_COPIES: 399 return (copies_table); 400 default: 401 return (NULL); 402 } 403 } 404 405 406 /* 407 * Tables of index types, plus functions to convert between the user view 408 * (strings) and internal representation (uint64_t). 409 */ 410 int 411 zfs_prop_string_to_index(zfs_prop_t prop, const char *string, uint64_t *index) 412 { 413 zfs_index_t *table; 414 int i; 415 416 if ((table = zfs_prop_index_table(prop)) == NULL) 417 return (-1); 418 419 for (i = 0; table[i].name != NULL; i++) { 420 if (strcmp(string, table[i].name) == 0) { 421 *index = table[i].index; 422 return (0); 423 } 424 } 425 426 return (-1); 427 } 428 429 int 430 zfs_prop_index_to_string(zfs_prop_t prop, uint64_t index, const char **string) 431 { 432 zfs_index_t *table; 433 int i; 434 435 if ((table = zfs_prop_index_table(prop)) == NULL) 436 return (-1); 437 438 for (i = 0; table[i].name != NULL; i++) { 439 if (table[i].index == index) { 440 *string = table[i].name; 441 return (0); 442 } 443 } 444 445 return (-1); 446 } 447 448 /* 449 * Determine if the specified property is visible or not. 450 */ 451 boolean_t 452 zfs_prop_is_visible(zfs_prop_t prop) 453 { 454 if (prop < 0) 455 return (B_FALSE); 456 457 return (zfs_prop_table[prop].pd_visible); 458 } 459 460 /* 461 * Iterate over all properties, calling back into the specified function 462 * for each property. We will continue to iterate until we either 463 * reach the end or the callback function something other than 464 * ZFS_PROP_CONT. 465 */ 466 zfs_prop_t 467 zfs_prop_iter(zfs_prop_f func, void *cb, boolean_t show_all) 468 { 469 int i; 470 471 for (i = 0; i < ZFS_PROP_COUNT; i++) { 472 if (zfs_prop_is_visible(i) || show_all) { 473 if (func(i, cb) != ZFS_PROP_CONT) 474 return (i); 475 } 476 } 477 return (ZFS_PROP_CONT); 478 } 479 480 #ifndef _KERNEL 481 482 /* 483 * Returns TRUE if the property applies to the given dataset types. 484 */ 485 int 486 zfs_prop_valid_for_type(zfs_prop_t prop, int types) 487 { 488 return ((zfs_prop_table[prop].pd_types & types) != 0); 489 } 490 491 /* 492 * Returns a string describing the set of acceptable values for the given 493 * property, or NULL if it cannot be set. 494 */ 495 const char * 496 zfs_prop_values(zfs_prop_t prop) 497 { 498 return (zfs_prop_table[prop].pd_values); 499 } 500 501 /* 502 * Returns TRUE if this property is a string type. Note that index types 503 * (compression, checksum) are treated as strings in userland, even though they 504 * are stored numerically on disk. 505 */ 506 int 507 zfs_prop_is_string(zfs_prop_t prop) 508 { 509 return (zfs_prop_table[prop].pd_proptype == prop_type_string || 510 zfs_prop_table[prop].pd_proptype == prop_type_index); 511 } 512 513 /* 514 * Returns the column header for the given property. Used only in 515 * 'zfs list -o', but centralized here with the other property information. 516 */ 517 const char * 518 zfs_prop_column_name(zfs_prop_t prop) 519 { 520 return (zfs_prop_table[prop].pd_colname); 521 } 522 523 /* 524 * Returns whether the given property should be displayed right-justified for 525 * 'zfs list'. 526 */ 527 boolean_t 528 zfs_prop_align_right(zfs_prop_t prop) 529 { 530 return (zfs_prop_table[prop].pd_rightalign); 531 } 532 533 /* 534 * Determines the minimum width for the column, and indicates whether it's fixed 535 * or not. Only string columns are non-fixed. 536 */ 537 size_t 538 zfs_prop_width(zfs_prop_t prop, boolean_t *fixed) 539 { 540 prop_desc_t *pd = &zfs_prop_table[prop]; 541 zfs_index_t *idx; 542 size_t ret; 543 int i; 544 545 *fixed = B_TRUE; 546 547 /* 548 * Start with the width of the column name. 549 */ 550 ret = strlen(pd->pd_colname); 551 552 /* 553 * For fixed-width values, make sure the width is large enough to hold 554 * any possible value. 555 */ 556 switch (pd->pd_proptype) { 557 case prop_type_number: 558 /* 559 * The maximum length of a human-readable number is 5 characters 560 * ("20.4M", for example). 561 */ 562 if (ret < 5) 563 ret = 5; 564 /* 565 * 'creation' is handled specially because it's a number 566 * internally, but displayed as a date string. 567 */ 568 if (prop == ZFS_PROP_CREATION) 569 *fixed = B_FALSE; 570 break; 571 case prop_type_boolean: 572 /* 573 * The maximum length of a boolean value is 3 characters, for 574 * "off". 575 */ 576 if (ret < 3) 577 ret = 3; 578 break; 579 case prop_type_index: 580 idx = zfs_prop_index_table(prop); 581 for (i = 0; idx[i].name != NULL; i++) { 582 if (strlen(idx[i].name) > ret) 583 ret = strlen(idx[i].name); 584 } 585 break; 586 587 case prop_type_string: 588 *fixed = B_FALSE; 589 break; 590 } 591 592 return (ret); 593 } 594 595 #endif 596