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", "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 { NULL } 344 }; 345 346 static zfs_index_t snapdir_table[] = { 347 { "hidden", ZFS_SNAPDIR_HIDDEN }, 348 { "visible", ZFS_SNAPDIR_VISIBLE }, 349 { NULL } 350 }; 351 352 static zfs_index_t acl_mode_table[] = { 353 { "discard", ZFS_ACL_DISCARD }, 354 { "groupmask", ZFS_ACL_GROUPMASK }, 355 { "passthrough", ZFS_ACL_PASSTHROUGH }, 356 { NULL } 357 }; 358 359 static zfs_index_t acl_inherit_table[] = { 360 { "discard", ZFS_ACL_DISCARD }, 361 { "noallow", ZFS_ACL_NOALLOW }, 362 { "secure", ZFS_ACL_SECURE }, 363 { "passthrough", ZFS_ACL_PASSTHROUGH }, 364 { NULL } 365 }; 366 367 static zfs_index_t copies_table[] = { 368 { "1", 1 }, 369 { "2", 2 }, 370 { "3", 3 }, 371 { NULL } 372 }; 373 374 static zfs_index_t * 375 zfs_prop_index_table(zfs_prop_t prop) 376 { 377 switch (prop) { 378 case ZFS_PROP_CHECKSUM: 379 return (checksum_table); 380 case ZFS_PROP_COMPRESSION: 381 return (compress_table); 382 case ZFS_PROP_SNAPDIR: 383 return (snapdir_table); 384 case ZFS_PROP_ACLMODE: 385 return (acl_mode_table); 386 case ZFS_PROP_ACLINHERIT: 387 return (acl_inherit_table); 388 case ZFS_PROP_COPIES: 389 return (copies_table); 390 default: 391 return (NULL); 392 } 393 } 394 395 396 /* 397 * Tables of index types, plus functions to convert between the user view 398 * (strings) and internal representation (uint64_t). 399 */ 400 int 401 zfs_prop_string_to_index(zfs_prop_t prop, const char *string, uint64_t *index) 402 { 403 zfs_index_t *table; 404 int i; 405 406 if ((table = zfs_prop_index_table(prop)) == NULL) 407 return (-1); 408 409 for (i = 0; table[i].name != NULL; i++) { 410 if (strcmp(string, table[i].name) == 0) { 411 *index = table[i].index; 412 return (0); 413 } 414 } 415 416 return (-1); 417 } 418 419 int 420 zfs_prop_index_to_string(zfs_prop_t prop, uint64_t index, const char **string) 421 { 422 zfs_index_t *table; 423 int i; 424 425 if ((table = zfs_prop_index_table(prop)) == NULL) 426 return (-1); 427 428 for (i = 0; table[i].name != NULL; i++) { 429 if (table[i].index == index) { 430 *string = table[i].name; 431 return (0); 432 } 433 } 434 435 return (-1); 436 } 437 438 /* 439 * Determine if the specified property is visible or not. 440 */ 441 boolean_t 442 zfs_prop_is_visible(zfs_prop_t prop) 443 { 444 if (prop < 0) 445 return (B_FALSE); 446 447 return (zfs_prop_table[prop].pd_visible); 448 } 449 450 /* 451 * Iterate over all properties, calling back into the specified function 452 * for each property. We will continue to iterate until we either 453 * reach the end or the callback function something other than 454 * ZFS_PROP_CONT. 455 */ 456 zfs_prop_t 457 zfs_prop_iter(zfs_prop_f func, void *cb, boolean_t show_all) 458 { 459 int i; 460 461 for (i = 0; i < ZFS_PROP_COUNT; i++) { 462 if (zfs_prop_is_visible(i) || show_all) { 463 if (func(i, cb) != ZFS_PROP_CONT) 464 return (i); 465 } 466 } 467 return (ZFS_PROP_CONT); 468 } 469 470 #ifndef _KERNEL 471 472 /* 473 * Returns TRUE if the property applies to the given dataset types. 474 */ 475 int 476 zfs_prop_valid_for_type(zfs_prop_t prop, int types) 477 { 478 return ((zfs_prop_table[prop].pd_types & types) != 0); 479 } 480 481 /* 482 * Returns a string describing the set of acceptable values for the given 483 * property, or NULL if it cannot be set. 484 */ 485 const char * 486 zfs_prop_values(zfs_prop_t prop) 487 { 488 return (zfs_prop_table[prop].pd_values); 489 } 490 491 /* 492 * Returns TRUE if this property is a string type. Note that index types 493 * (compression, checksum) are treated as strings in userland, even though they 494 * are stored numerically on disk. 495 */ 496 int 497 zfs_prop_is_string(zfs_prop_t prop) 498 { 499 return (zfs_prop_table[prop].pd_proptype == prop_type_string || 500 zfs_prop_table[prop].pd_proptype == prop_type_index); 501 } 502 503 /* 504 * Returns the column header for the given property. Used only in 505 * 'zfs list -o', but centralized here with the other property information. 506 */ 507 const char * 508 zfs_prop_column_name(zfs_prop_t prop) 509 { 510 return (zfs_prop_table[prop].pd_colname); 511 } 512 513 /* 514 * Returns whether the given property should be displayed right-justified for 515 * 'zfs list'. 516 */ 517 boolean_t 518 zfs_prop_align_right(zfs_prop_t prop) 519 { 520 return (zfs_prop_table[prop].pd_rightalign); 521 } 522 523 /* 524 * Determines the minimum width for the column, and indicates whether it's fixed 525 * or not. Only string columns are non-fixed. 526 */ 527 size_t 528 zfs_prop_width(zfs_prop_t prop, boolean_t *fixed) 529 { 530 prop_desc_t *pd = &zfs_prop_table[prop]; 531 zfs_index_t *idx; 532 size_t ret; 533 int i; 534 535 *fixed = B_TRUE; 536 537 /* 538 * Start with the width of the column name. 539 */ 540 ret = strlen(pd->pd_colname); 541 542 /* 543 * For fixed-width values, make sure the width is large enough to hold 544 * any possible value. 545 */ 546 switch (pd->pd_proptype) { 547 case prop_type_number: 548 /* 549 * The maximum length of a human-readable number is 5 characters 550 * ("20.4M", for example). 551 */ 552 if (ret < 5) 553 ret = 5; 554 /* 555 * 'creation' is handled specially because it's a number 556 * internally, but displayed as a date string. 557 */ 558 if (prop == ZFS_PROP_CREATION) 559 *fixed = B_FALSE; 560 break; 561 case prop_type_boolean: 562 /* 563 * The maximum length of a boolean value is 3 characters, for 564 * "off". 565 */ 566 if (ret < 3) 567 ret = 3; 568 break; 569 case prop_type_index: 570 idx = zfs_prop_index_table(prop); 571 for (i = 0; idx[i].name != NULL; i++) { 572 if (strlen(idx[i].name) > ret) 573 ret = strlen(idx[i].name); 574 } 575 break; 576 577 case prop_type_string: 578 *fixed = B_FALSE; 579 break; 580 } 581 582 return (ret); 583 } 584 585 #endif 586