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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * Master property table. 31 * 32 * This table keeps track of all the properties supported by ZFS, and their 33 * various attributes. Not all of these are needed by the kernel, and several 34 * are only used by a single libzfs client. But having them here centralizes 35 * all property information in one location. 36 * 37 * name The human-readable string representing this property 38 * proptype Basic type (string, boolean, number) 39 * default Default value for the property. Sadly, C only allows 40 * you to initialize the first member of a union, so we 41 * have two default members for each property. 42 * attr Attributes (readonly, inheritable) for the property 43 * types Valid dataset types to which this applies 44 * values String describing acceptable values for the property 45 * colname The column header for 'zfs list' 46 * colfmt The column formatting for 'zfs list' 47 * 48 * This table must match the order of property types in libzfs.h. 49 */ 50 51 #include <sys/zio.h> 52 #include <sys/spa.h> 53 #include <sys/zfs_acl.h> 54 #include <sys/zfs_ioctl.h> 55 56 #include "zfs_prop.h" 57 58 #if defined(_KERNEL) 59 #include <sys/systm.h> 60 #else 61 #include <stdlib.h> 62 #include <string.h> 63 #include <ctype.h> 64 #endif 65 66 typedef enum { 67 prop_default, 68 prop_readonly, 69 prop_inherit 70 } prop_attr_t; 71 72 typedef struct { 73 const char *pd_name; 74 zfs_proptype_t pd_proptype; 75 uint64_t pd_numdefault; 76 const char *pd_strdefault; 77 prop_attr_t pd_attr; 78 int pd_types; 79 const char *pd_values; 80 const char *pd_colname; 81 const char *pd_colfmt; 82 } prop_desc_t; 83 84 static prop_desc_t zfs_prop_table[ZFS_NPROP_ALL] = { 85 { "type", prop_type_string, 0, NULL, prop_readonly, 86 ZFS_TYPE_ANY, "filesystem | volume | snapshot", "TYPE", "%10s" }, 87 { "creation", prop_type_number, 0, NULL, prop_readonly, 88 ZFS_TYPE_ANY, "<date>", "CREATION", "%-20s" }, 89 { "used", prop_type_number, 0, NULL, prop_readonly, 90 ZFS_TYPE_ANY, "<size>", "USED", "%5s" }, 91 { "available", prop_type_number, 0, NULL, prop_readonly, 92 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, "<size>", "AVAIL", "%5s" }, 93 { "referenced", prop_type_number, 0, NULL, prop_readonly, 94 ZFS_TYPE_SNAPSHOT | ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, 95 "<size>", "REFER", "%5s" }, 96 { "compressratio", prop_type_number, 0, NULL, prop_readonly, 97 ZFS_TYPE_ANY, "<1.00x or higher if compressed>", "RATIO", "%5s" }, 98 { "mounted", prop_type_boolean, 0, NULL, prop_readonly, 99 ZFS_TYPE_FILESYSTEM, "yes | no | -", "MOUNTED", "%7s" }, 100 { "origin", prop_type_string, 0, NULL, prop_readonly, 101 ZFS_TYPE_FILESYSTEM, "<snapshot>", "ORIGIN", "%-20s" }, 102 { "quota", prop_type_number, 0, NULL, prop_default, 103 ZFS_TYPE_FILESYSTEM, "<size> | none", "QUOTA", "%5s" }, 104 { "reservation", prop_type_number, 0, NULL, prop_default, 105 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, 106 "<size> | none", "RESERV", "%6s" }, 107 { "volsize", prop_type_number, 0, NULL, prop_default, 108 ZFS_TYPE_VOLUME, "<size>", "VOLSIZE", "%7s" }, 109 { "volblocksize", prop_type_number, 8192, NULL, prop_default, 110 ZFS_TYPE_VOLUME, "512 to 128k, power of 2", "VOLBLOCK", "%8s" }, 111 { "recordsize", prop_type_number, SPA_MAXBLOCKSIZE, NULL, 112 prop_inherit, 113 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT, 114 "512 to 128k, power of 2", "RECSIZE", "%7s" }, 115 { "mountpoint", prop_type_string, 0, "/", prop_inherit, 116 ZFS_TYPE_FILESYSTEM, 117 "<path> | legacy | none", "MOUNTPOINT", "%-20s" }, 118 { "sharenfs", prop_type_string, 0, "off", prop_inherit, 119 ZFS_TYPE_FILESYSTEM, 120 "on | off | share(1M) options", "SHARENFS", "%-15s" }, 121 { "checksum", prop_type_index, ZIO_CHECKSUM_DEFAULT, NULL, 122 prop_inherit, ZFS_TYPE_ANY, 123 "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM", "%10s" }, 124 { "compression", prop_type_index, ZIO_COMPRESS_DEFAULT, NULL, 125 prop_inherit, ZFS_TYPE_ANY, 126 "on | off | lzjb", "COMPRESS", "%8s" }, 127 { "atime", prop_type_boolean, 1, NULL, prop_inherit, 128 ZFS_TYPE_FILESYSTEM, 129 "on | off", "ATIME", "%5s" }, 130 { "devices", prop_type_boolean, 1, NULL, prop_inherit, 131 ZFS_TYPE_FILESYSTEM, 132 "on | off", "DEVICES", "%7s" }, 133 { "exec", prop_type_boolean, 1, NULL, prop_inherit, 134 ZFS_TYPE_FILESYSTEM, 135 "on | off", "EXEC", "%4s" }, 136 { "setuid", prop_type_boolean, 1, NULL, prop_inherit, 137 ZFS_TYPE_FILESYSTEM, "on | off", "SETUID", "%6s" }, 138 { "readonly", prop_type_boolean, 0, NULL, prop_inherit, 139 ZFS_TYPE_ANY, "on | off", "RDONLY", "%6s" }, 140 { "zoned", prop_type_boolean, 0, NULL, prop_inherit, 141 ZFS_TYPE_ANY, 142 "on | off", "ZONED", "%5s" }, 143 { "snapdir", prop_type_index, ZFS_SNAPDIR_VISIBLE, NULL, 144 prop_inherit, 145 ZFS_TYPE_FILESYSTEM, 146 "hidden | visible", "SNAPDIR", "%7s" }, 147 { "aclmode", prop_type_index, GROUPMASK, NULL, prop_inherit, 148 ZFS_TYPE_FILESYSTEM, 149 "discard | groupmask | passthrough", "ACLMODE", "%11s" }, 150 { "aclinherit", prop_type_index, SECURE, NULL, prop_inherit, 151 ZFS_TYPE_FILESYSTEM, 152 "discard | noallow | secure | passthrough", "ACLINHERIT", "%11s" }, 153 { "createtxg", prop_type_number, 0, NULL, prop_readonly, 154 ZFS_TYPE_ANY, NULL, NULL, NULL}, 155 { "name", prop_type_string, 0, NULL, prop_readonly, 156 ZFS_TYPE_ANY, 157 NULL, "NAME", "%-20s" }, 158 }; 159 160 zfs_proptype_t 161 zfs_prop_get_type(zfs_prop_t prop) 162 { 163 return (zfs_prop_table[prop].pd_proptype); 164 } 165 166 /* 167 * Given a property name, returns the corresponding property ID. 168 */ 169 zfs_prop_t 170 zfs_name_to_prop(const char *propname) 171 { 172 int i; 173 174 for (i = 0; i < ZFS_NPROP_ALL; i++) { 175 if (strcmp(zfs_prop_table[i].pd_name, propname) == 0) 176 return (i); 177 #ifndef _KERNEL 178 if (zfs_prop_table[i].pd_colname != NULL && 179 strcasecmp(zfs_prop_table[i].pd_colname, propname) == 0) 180 return (i); 181 #endif 182 } 183 184 return (ZFS_PROP_INVAL); 185 } 186 187 /* 188 * Return the default value for the given property. 189 */ 190 void 191 zfs_prop_default_string(zfs_prop_t prop, char *buf, size_t buflen) 192 { 193 /* 194 * For index types (compression and checksum), we want the numeric value 195 * in the kernel, but the string value in userland. The kernel will 196 * call zfs_prop_default_numeric() based on the property type. In 197 * userland, the zfs_prop_is_string() will return TRUE for index types, 198 * and we'll return "on" from this function. 199 */ 200 if (zfs_prop_table[prop].pd_proptype == prop_type_index) 201 (void) strncpy(buf, "on", buflen); 202 else 203 (void) strncpy(buf, zfs_prop_table[prop].pd_strdefault, buflen); 204 } 205 206 uint64_t 207 zfs_prop_default_numeric(zfs_prop_t prop) 208 { 209 return (zfs_prop_table[prop].pd_numdefault); 210 } 211 212 /* 213 * Returns TRUE if the property is readonly. 214 */ 215 int 216 zfs_prop_readonly(zfs_prop_t prop) 217 { 218 return (zfs_prop_table[prop].pd_attr == prop_readonly); 219 } 220 221 #ifndef _KERNEL 222 /* 223 * Given a property ID, returns the corresponding name. 224 */ 225 const char * 226 zfs_prop_to_name(zfs_prop_t prop) 227 { 228 return (zfs_prop_table[prop].pd_name); 229 } 230 231 /* 232 * Returns TRUE if the property is inheritable. 233 */ 234 int 235 zfs_prop_inheritable(zfs_prop_t prop) 236 { 237 return (zfs_prop_table[prop].pd_attr == prop_inherit); 238 } 239 240 /* 241 * Returns TRUE if the property applies to the given dataset types. 242 */ 243 int 244 zfs_prop_valid_for_type(zfs_prop_t prop, int types) 245 { 246 return ((zfs_prop_table[prop].pd_types & types) != 0); 247 } 248 249 /* 250 * Returns a string describing the set of acceptable values for the given 251 * property, or NULL if it cannot be set. 252 */ 253 const char * 254 zfs_prop_values(zfs_prop_t prop) 255 { 256 return (zfs_prop_table[prop].pd_values); 257 } 258 259 /* 260 * Returns TRUE if this property is a string type. Note that index types 261 * (compression, checksum) are treated as strings in userland, even though they 262 * are stored numerically on disk. 263 */ 264 int 265 zfs_prop_is_string(zfs_prop_t prop) 266 { 267 return (zfs_prop_table[prop].pd_proptype == prop_type_string || 268 zfs_prop_table[prop].pd_proptype == prop_type_index); 269 } 270 271 /* 272 * Returns the column header for the given property. Used only in 273 * 'zfs list -o', but centralized here with the other property information. 274 */ 275 const char * 276 zfs_prop_column_name(zfs_prop_t prop) 277 { 278 return (zfs_prop_table[prop].pd_colname); 279 } 280 281 /* 282 * Returns the column formatting for the given property. Used only in 283 * 'zfs list -o', but centralized here with the other property information. 284 */ 285 const char * 286 zfs_prop_column_format(zfs_prop_t prop) 287 { 288 return (zfs_prop_table[prop].pd_colfmt); 289 } 290 291 /* 292 * Given a comma-separated list of fields, fills in the specified array with a 293 * list of properties. The keyword "all" can be used to specify all properties. 294 * The 'count' parameter returns the number of matching properties placed into 295 * the list. The 'props' parameter must point to an array of at least 296 * ZFS_NPROP_ALL elements. 297 */ 298 int 299 zfs_get_proplist(char *fields, zfs_prop_t *props, int max, 300 int *count, char **badopt) 301 { 302 int i; 303 size_t len; 304 char *s, *p; 305 306 *count = 0; 307 308 /* 309 * Check for the special value "all", which indicates all properties 310 * should be displayed. 311 */ 312 if (strcmp(fields, "all") == 0) { 313 if (max < ZFS_NPROP_VISIBLE) 314 return (EOVERFLOW); 315 316 for (i = 0; i < ZFS_NPROP_VISIBLE; i++) 317 props[i] = i; 318 *count = ZFS_NPROP_VISIBLE; 319 return (0); 320 } 321 322 /* 323 * It would be nice to use getsubopt() here, but the inclusion of column 324 * aliases makes this more effort than it's worth. 325 */ 326 s = fields; 327 while (*s != '\0') { 328 if ((p = strchr(s, ',')) == NULL) { 329 len = strlen(s); 330 p = s + len; 331 } else { 332 len = p - s; 333 } 334 335 /* 336 * Check for empty options. 337 */ 338 if (len == 0) { 339 *badopt = ""; 340 return (EINVAL); 341 } 342 343 /* 344 * Check all regular property names. 345 */ 346 for (i = 0; i < ZFS_NPROP_ALL; i++) { 347 if (zfs_prop_table[i].pd_colname == NULL) 348 continue; 349 350 if (len == strlen(zfs_prop_table[i].pd_name) && 351 strncmp(s, zfs_prop_table[i].pd_name, len) == 0) 352 break; 353 } 354 355 /* 356 * Check all abbreviated column names. 357 */ 358 if (i == ZFS_NPROP_ALL) { 359 for (i = 0; i < ZFS_NPROP_ALL; i++) { 360 if (zfs_prop_table[i].pd_colname == NULL) 361 continue; 362 363 if (len == 364 strlen(zfs_prop_table[i].pd_colname) && 365 strncasecmp(s, zfs_prop_table[i].pd_colname, 366 len) == 0) 367 break; 368 } 369 } 370 371 /* 372 * If no column is specified, then return failure, setting 373 * 'badopt' to point to the invalid option. 374 */ 375 if (i == ZFS_NPROP_ALL) { 376 s[len] = '\0'; 377 *badopt = s; 378 return (EINVAL); 379 } 380 381 /* 382 * If the user specified too many options (by using the same one 383 * multiple times). return an error with 'badopt' set to NULL to 384 * indicate this condition. 385 */ 386 if (*count == max) 387 return (EOVERFLOW); 388 389 props[*count] = i; 390 *count += 1; 391 392 s = p; 393 if (*s == ',') 394 s++; 395 } 396 397 /* 398 * If no fields were specified, return an error. 399 */ 400 if (*count == 0) { 401 *badopt = ""; 402 return (EINVAL); 403 } 404 405 return (0); 406 } 407 408 #endif 409