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_ANY, 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_readonly, 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, 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_FILESYSTEM | ZFS_TYPE_VOLUME, 123 "on | off | fletcher2 | fletcher4 | sha256", "CHECKSUM", "%10s" }, 124 { "compression", prop_type_index, ZIO_COMPRESS_DEFAULT, NULL, 125 prop_inherit, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, 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_FILESYSTEM | ZFS_TYPE_VOLUME, 140 "on | off", "RDONLY", "%6s" }, 141 { "zoned", prop_type_boolean, 0, NULL, prop_inherit, 142 ZFS_TYPE_FILESYSTEM, 143 "on | off", "ZONED", "%5s" }, 144 { "snapdir", prop_type_index, ZFS_SNAPDIR_VISIBLE, NULL, 145 prop_inherit, 146 ZFS_TYPE_FILESYSTEM, 147 "hidden | visible", "SNAPDIR", "%7s" }, 148 { "aclmode", prop_type_index, GROUPMASK, NULL, prop_inherit, 149 ZFS_TYPE_FILESYSTEM, 150 "discard | groupmask | passthrough", "ACLMODE", "%11s" }, 151 { "aclinherit", prop_type_index, SECURE, NULL, prop_inherit, 152 ZFS_TYPE_FILESYSTEM, 153 "discard | noallow | secure | passthrough", "ACLINHERIT", "%11s" }, 154 { "createtxg", prop_type_number, 0, NULL, prop_readonly, 155 ZFS_TYPE_ANY, NULL, NULL, NULL}, 156 { "name", prop_type_string, 0, NULL, prop_readonly, 157 ZFS_TYPE_ANY, 158 NULL, "NAME", "%-20s" }, 159 }; 160 161 zfs_proptype_t 162 zfs_prop_get_type(zfs_prop_t prop) 163 { 164 return (zfs_prop_table[prop].pd_proptype); 165 } 166 167 /* 168 * Given a property name, returns the corresponding property ID. 169 */ 170 zfs_prop_t 171 zfs_name_to_prop(const char *propname) 172 { 173 int i; 174 175 for (i = 0; i < ZFS_NPROP_ALL; i++) { 176 if (strcmp(zfs_prop_table[i].pd_name, propname) == 0) 177 return (i); 178 #ifndef _KERNEL 179 if (zfs_prop_table[i].pd_colname != NULL && 180 strcasecmp(zfs_prop_table[i].pd_colname, propname) == 0) 181 return (i); 182 #endif 183 } 184 185 return (ZFS_PROP_INVAL); 186 } 187 188 /* 189 * Return the default value for the given property. 190 */ 191 void 192 zfs_prop_default_string(zfs_prop_t prop, char *buf, size_t buflen) 193 { 194 /* 195 * For index types (compression and checksum), we want the numeric value 196 * in the kernel, but the string value in userland. The kernel will 197 * call zfs_prop_default_numeric() based on the property type. In 198 * userland, the zfs_prop_is_string() will return TRUE for index types, 199 * and we'll return "on" from this function. 200 */ 201 if (zfs_prop_table[prop].pd_proptype == prop_type_index) 202 (void) strncpy(buf, "on", buflen); 203 else 204 (void) strncpy(buf, zfs_prop_table[prop].pd_strdefault, buflen); 205 } 206 207 uint64_t 208 zfs_prop_default_numeric(zfs_prop_t prop) 209 { 210 return (zfs_prop_table[prop].pd_numdefault); 211 } 212 213 /* 214 * Returns TRUE if the property is readonly. 215 */ 216 int 217 zfs_prop_readonly(zfs_prop_t prop) 218 { 219 return (zfs_prop_table[prop].pd_attr == prop_readonly); 220 } 221 222 #ifndef _KERNEL 223 /* 224 * Given a property ID, returns the corresponding name. 225 */ 226 const char * 227 zfs_prop_to_name(zfs_prop_t prop) 228 { 229 return (zfs_prop_table[prop].pd_name); 230 } 231 232 /* 233 * Returns TRUE if the property is inheritable. 234 */ 235 int 236 zfs_prop_inheritable(zfs_prop_t prop) 237 { 238 return (zfs_prop_table[prop].pd_attr == prop_inherit); 239 } 240 241 /* 242 * Returns TRUE if the property applies to the given dataset types. 243 */ 244 int 245 zfs_prop_valid_for_type(zfs_prop_t prop, int types) 246 { 247 return ((zfs_prop_table[prop].pd_types & types) != 0); 248 } 249 250 /* 251 * Returns a string describing the set of acceptable values for the given 252 * property, or NULL if it cannot be set. 253 */ 254 const char * 255 zfs_prop_values(zfs_prop_t prop) 256 { 257 return (zfs_prop_table[prop].pd_values); 258 } 259 260 /* 261 * Returns TRUE if this property is a string type. Note that index types 262 * (compression, checksum) are treated as strings in userland, even though they 263 * are stored numerically on disk. 264 */ 265 int 266 zfs_prop_is_string(zfs_prop_t prop) 267 { 268 return (zfs_prop_table[prop].pd_proptype == prop_type_string || 269 zfs_prop_table[prop].pd_proptype == prop_type_index); 270 } 271 272 /* 273 * Returns the column header for the given property. Used only in 274 * 'zfs list -o', but centralized here with the other property information. 275 */ 276 const char * 277 zfs_prop_column_name(zfs_prop_t prop) 278 { 279 return (zfs_prop_table[prop].pd_colname); 280 } 281 282 /* 283 * Returns the column formatting for the given property. Used only in 284 * 'zfs list -o', but centralized here with the other property information. 285 */ 286 const char * 287 zfs_prop_column_format(zfs_prop_t prop) 288 { 289 return (zfs_prop_table[prop].pd_colfmt); 290 } 291 292 /* 293 * Given a comma-separated list of fields, fills in the specified array with a 294 * list of properties. The keyword "all" can be used to specify all properties. 295 * The 'count' parameter returns the number of matching properties placed into 296 * the list. The 'props' parameter must point to an array of at least 297 * ZFS_NPROP_ALL elements. 298 */ 299 int 300 zfs_get_proplist(char *fields, zfs_prop_t *props, int max, 301 int *count, char **badopt) 302 { 303 int i; 304 size_t len; 305 char *s, *p; 306 307 *count = 0; 308 309 /* 310 * Check for the special value "all", which indicates all properties 311 * should be displayed. 312 */ 313 if (strcmp(fields, "all") == 0) { 314 if (max < ZFS_NPROP_VISIBLE) 315 return (EOVERFLOW); 316 317 for (i = 0; i < ZFS_NPROP_VISIBLE; i++) 318 props[i] = i; 319 *count = ZFS_NPROP_VISIBLE; 320 return (0); 321 } 322 323 /* 324 * It would be nice to use getsubopt() here, but the inclusion of column 325 * aliases makes this more effort than it's worth. 326 */ 327 s = fields; 328 while (*s != '\0') { 329 if ((p = strchr(s, ',')) == NULL) { 330 len = strlen(s); 331 p = s + len; 332 } else { 333 len = p - s; 334 } 335 336 /* 337 * Check for empty options. 338 */ 339 if (len == 0) { 340 *badopt = ""; 341 return (EINVAL); 342 } 343 344 /* 345 * Check all regular property names. 346 */ 347 for (i = 0; i < ZFS_NPROP_ALL; i++) { 348 if (zfs_prop_table[i].pd_colname == NULL) 349 continue; 350 351 if (len == strlen(zfs_prop_table[i].pd_name) && 352 strncmp(s, zfs_prop_table[i].pd_name, len) == 0) 353 break; 354 } 355 356 /* 357 * Check all abbreviated column names. 358 */ 359 if (i == ZFS_NPROP_ALL) { 360 for (i = 0; i < ZFS_NPROP_ALL; i++) { 361 if (zfs_prop_table[i].pd_colname == NULL) 362 continue; 363 364 if (len == 365 strlen(zfs_prop_table[i].pd_colname) && 366 strncasecmp(s, zfs_prop_table[i].pd_colname, 367 len) == 0) 368 break; 369 } 370 } 371 372 /* 373 * If no column is specified, then return failure, setting 374 * 'badopt' to point to the invalid option. 375 */ 376 if (i == ZFS_NPROP_ALL) { 377 s[len] = '\0'; 378 *badopt = s; 379 return (EINVAL); 380 } 381 382 /* 383 * If the user specified too many options (by using the same one 384 * multiple times). return an error with 'badopt' set to NULL to 385 * indicate this condition. 386 */ 387 if (*count == max) 388 return (EOVERFLOW); 389 390 props[*count] = i; 391 *count += 1; 392 393 s = p; 394 if (*s == ',') 395 s++; 396 } 397 398 /* 399 * If no fields were specified, return an error. 400 */ 401 if (*count == 0) { 402 *badopt = ""; 403 return (EINVAL); 404 } 405 406 return (0); 407 } 408 409 #endif 410