1185029Spjd/* 2185029Spjd * CDDL HEADER START 3185029Spjd * 4185029Spjd * The contents of this file are subject to the terms of the 5185029Spjd * Common Development and Distribution License (the "License"). 6185029Spjd * You may not use this file except in compliance with the License. 7185029Spjd * 8185029Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9185029Spjd * or http://www.opensolaris.org/os/licensing. 10185029Spjd * See the License for the specific language governing permissions 11185029Spjd * and limitations under the License. 12185029Spjd * 13185029Spjd * When distributing Covered Code, include this CDDL HEADER in each 14185029Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15185029Spjd * If applicable, add the following below this CDDL HEADER, with the 16185029Spjd * fields enclosed by brackets "[]" replaced with your own identifying 17185029Spjd * information: Portions Copyright [yyyy] [name of copyright owner] 18185029Spjd * 19185029Spjd * CDDL HEADER END 20185029Spjd */ 21185029Spjd/* 22219089Spjd * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23185029Spjd * Use is subject to license terms. 24185029Spjd */ 25249643Smm/* 26249643Smm * Copyright (c) 2012 by Delphix. All rights reserved. 27249643Smm */ 28185029Spjd 29185029Spjd/* 30185029Spjd * Common routines used by zfs and zpool property management. 31185029Spjd */ 32185029Spjd 33185029Spjd#include <sys/zio.h> 34185029Spjd#include <sys/spa.h> 35185029Spjd#include <sys/zfs_acl.h> 36185029Spjd#include <sys/zfs_ioctl.h> 37185029Spjd#include <sys/zfs_znode.h> 38185029Spjd#include <sys/fs/zfs.h> 39185029Spjd 40185029Spjd#include "zfs_prop.h" 41185029Spjd#include "zfs_deleg.h" 42185029Spjd 43185029Spjd#if defined(_KERNEL) 44185029Spjd#include <sys/systm.h> 45185029Spjd#include <sys/libkern.h> 46185029Spjd#else 47185029Spjd#include <stdlib.h> 48185029Spjd#include <string.h> 49185029Spjd#include <ctype.h> 50185029Spjd#endif 51185029Spjd 52185029Spjdstatic zprop_desc_t * 53185029Spjdzprop_get_proptable(zfs_type_t type) 54185029Spjd{ 55185029Spjd if (type == ZFS_TYPE_POOL) 56185029Spjd return (zpool_prop_get_table()); 57185029Spjd else 58185029Spjd return (zfs_prop_get_table()); 59185029Spjd} 60185029Spjd 61185029Spjdstatic int 62185029Spjdzprop_get_numprops(zfs_type_t type) 63185029Spjd{ 64185029Spjd if (type == ZFS_TYPE_POOL) 65185029Spjd return (ZPOOL_NUM_PROPS); 66185029Spjd else 67185029Spjd return (ZFS_NUM_PROPS); 68185029Spjd} 69185029Spjd 70185029Spjdvoid 71219089Spjdzprop_register_impl(int prop, const char *name, zprop_type_t type, 72185029Spjd uint64_t numdefault, const char *strdefault, zprop_attr_t attr, 73185029Spjd int objset_types, const char *values, const char *colname, 74185029Spjd boolean_t rightalign, boolean_t visible, const zprop_index_t *idx_tbl) 75185029Spjd{ 76185029Spjd zprop_desc_t *prop_tbl = zprop_get_proptable(objset_types); 77185029Spjd zprop_desc_t *pd; 78185029Spjd 79185029Spjd pd = &prop_tbl[prop]; 80185029Spjd 81185029Spjd ASSERT(pd->pd_name == NULL || pd->pd_name == name); 82219089Spjd ASSERT(name != NULL); 83219089Spjd ASSERT(colname != NULL); 84185029Spjd 85185029Spjd pd->pd_name = name; 86185029Spjd pd->pd_propnum = prop; 87185029Spjd pd->pd_proptype = type; 88185029Spjd pd->pd_numdefault = numdefault; 89185029Spjd pd->pd_strdefault = strdefault; 90185029Spjd pd->pd_attr = attr; 91185029Spjd pd->pd_types = objset_types; 92185029Spjd pd->pd_values = values; 93185029Spjd pd->pd_colname = colname; 94185029Spjd pd->pd_rightalign = rightalign; 95185029Spjd pd->pd_visible = visible; 96185029Spjd pd->pd_table = idx_tbl; 97219089Spjd pd->pd_table_size = 0; 98219089Spjd while (idx_tbl && (idx_tbl++)->pi_name != NULL) 99219089Spjd pd->pd_table_size++; 100185029Spjd} 101185029Spjd 102185029Spjdvoid 103219089Spjdzprop_register_string(int prop, const char *name, const char *def, 104185029Spjd zprop_attr_t attr, int objset_types, const char *values, 105185029Spjd const char *colname) 106185029Spjd{ 107219089Spjd zprop_register_impl(prop, name, PROP_TYPE_STRING, 0, def, attr, 108185029Spjd objset_types, values, colname, B_FALSE, B_TRUE, NULL); 109185029Spjd 110185029Spjd} 111185029Spjd 112185029Spjdvoid 113219089Spjdzprop_register_number(int prop, const char *name, uint64_t def, 114219089Spjd zprop_attr_t attr, int objset_types, const char *values, 115219089Spjd const char *colname) 116185029Spjd{ 117219089Spjd zprop_register_impl(prop, name, PROP_TYPE_NUMBER, def, NULL, attr, 118185029Spjd objset_types, values, colname, B_TRUE, B_TRUE, NULL); 119185029Spjd} 120185029Spjd 121185029Spjdvoid 122219089Spjdzprop_register_index(int prop, const char *name, uint64_t def, 123219089Spjd zprop_attr_t attr, int objset_types, const char *values, 124219089Spjd const char *colname, const zprop_index_t *idx_tbl) 125185029Spjd{ 126219089Spjd zprop_register_impl(prop, name, PROP_TYPE_INDEX, def, NULL, attr, 127185029Spjd objset_types, values, colname, B_TRUE, B_TRUE, idx_tbl); 128185029Spjd} 129185029Spjd 130185029Spjdvoid 131219089Spjdzprop_register_hidden(int prop, const char *name, zprop_type_t type, 132185029Spjd zprop_attr_t attr, int objset_types, const char *colname) 133185029Spjd{ 134219089Spjd zprop_register_impl(prop, name, type, 0, NULL, attr, 135249643Smm objset_types, NULL, colname, 136249643Smm type == PROP_TYPE_NUMBER, B_FALSE, NULL); 137185029Spjd} 138185029Spjd 139185029Spjd 140185029Spjd/* 141185029Spjd * A comparison function we can use to order indexes into property tables. 142185029Spjd */ 143185029Spjdstatic int 144185029Spjdzprop_compare(const void *arg1, const void *arg2) 145185029Spjd{ 146185029Spjd const zprop_desc_t *p1 = *((zprop_desc_t **)arg1); 147185029Spjd const zprop_desc_t *p2 = *((zprop_desc_t **)arg2); 148185029Spjd boolean_t p1ro, p2ro; 149185029Spjd 150185029Spjd p1ro = (p1->pd_attr == PROP_READONLY); 151185029Spjd p2ro = (p2->pd_attr == PROP_READONLY); 152185029Spjd 153185029Spjd if (p1ro == p2ro) 154185029Spjd return (strcmp(p1->pd_name, p2->pd_name)); 155185029Spjd 156185029Spjd return (p1ro ? -1 : 1); 157185029Spjd} 158185029Spjd 159185029Spjd/* 160185029Spjd * Iterate over all properties in the given property table, calling back 161185029Spjd * into the specified function for each property. We will continue to 162185029Spjd * iterate until we either reach the end or the callback function returns 163185029Spjd * something other than ZPROP_CONT. 164185029Spjd */ 165185029Spjdint 166185029Spjdzprop_iter_common(zprop_func func, void *cb, boolean_t show_all, 167185029Spjd boolean_t ordered, zfs_type_t type) 168185029Spjd{ 169185029Spjd int i, j, num_props, size, prop; 170185029Spjd zprop_desc_t *prop_tbl; 171185029Spjd zprop_desc_t **order; 172185029Spjd 173185029Spjd prop_tbl = zprop_get_proptable(type); 174185029Spjd num_props = zprop_get_numprops(type); 175185029Spjd size = num_props * sizeof (zprop_desc_t *); 176185029Spjd 177185029Spjd#if defined(_KERNEL) 178185029Spjd order = kmem_alloc(size, KM_SLEEP); 179185029Spjd#else 180185029Spjd if ((order = malloc(size)) == NULL) 181185029Spjd return (ZPROP_CONT); 182185029Spjd#endif 183185029Spjd 184185029Spjd for (j = 0; j < num_props; j++) 185185029Spjd order[j] = &prop_tbl[j]; 186185029Spjd 187185029Spjd if (ordered) { 188185029Spjd qsort((void *)order, num_props, sizeof (zprop_desc_t *), 189185029Spjd zprop_compare); 190185029Spjd } 191185029Spjd 192185029Spjd prop = ZPROP_CONT; 193185029Spjd for (i = 0; i < num_props; i++) { 194185029Spjd if ((order[i]->pd_visible || show_all) && 195185029Spjd (func(order[i]->pd_propnum, cb) != ZPROP_CONT)) { 196185029Spjd prop = order[i]->pd_propnum; 197185029Spjd break; 198185029Spjd } 199185029Spjd } 200185029Spjd 201185029Spjd#if defined(_KERNEL) 202185029Spjd kmem_free(order, size); 203185029Spjd#else 204185029Spjd free(order); 205185029Spjd#endif 206185029Spjd return (prop); 207185029Spjd} 208185029Spjd 209185029Spjdstatic boolean_t 210185029Spjdpropname_match(const char *p, size_t len, zprop_desc_t *prop_entry) 211185029Spjd{ 212185029Spjd const char *propname = prop_entry->pd_name; 213185029Spjd#ifndef _KERNEL 214185029Spjd const char *colname = prop_entry->pd_colname; 215185029Spjd int c; 216185029Spjd#endif 217185029Spjd 218185029Spjd if (len == strlen(propname) && 219185029Spjd strncmp(p, propname, len) == 0) 220185029Spjd return (B_TRUE); 221185029Spjd 222185029Spjd#ifndef _KERNEL 223209962Smm if (colname == NULL || len != strlen(colname)) 224185029Spjd return (B_FALSE); 225185029Spjd 226185029Spjd for (c = 0; c < len; c++) 227185029Spjd if (p[c] != tolower(colname[c])) 228185029Spjd break; 229185029Spjd 230185029Spjd return (colname[c] == '\0'); 231185029Spjd#else 232185029Spjd return (B_FALSE); 233185029Spjd#endif 234185029Spjd} 235185029Spjd 236185029Spjdtypedef struct name_to_prop_cb { 237185029Spjd const char *propname; 238185029Spjd zprop_desc_t *prop_tbl; 239185029Spjd} name_to_prop_cb_t; 240185029Spjd 241185029Spjdstatic int 242185029Spjdzprop_name_to_prop_cb(int prop, void *cb_data) 243185029Spjd{ 244185029Spjd name_to_prop_cb_t *data = cb_data; 245185029Spjd 246185029Spjd if (propname_match(data->propname, strlen(data->propname), 247185029Spjd &data->prop_tbl[prop])) 248185029Spjd return (prop); 249185029Spjd 250185029Spjd return (ZPROP_CONT); 251185029Spjd} 252185029Spjd 253185029Spjdint 254185029Spjdzprop_name_to_prop(const char *propname, zfs_type_t type) 255185029Spjd{ 256185029Spjd int prop; 257185029Spjd name_to_prop_cb_t cb_data; 258185029Spjd 259185029Spjd cb_data.propname = propname; 260185029Spjd cb_data.prop_tbl = zprop_get_proptable(type); 261185029Spjd 262185029Spjd prop = zprop_iter_common(zprop_name_to_prop_cb, &cb_data, 263185029Spjd B_TRUE, B_FALSE, type); 264185029Spjd 265185029Spjd return (prop == ZPROP_CONT ? ZPROP_INVAL : prop); 266185029Spjd} 267185029Spjd 268185029Spjdint 269185029Spjdzprop_string_to_index(int prop, const char *string, uint64_t *index, 270185029Spjd zfs_type_t type) 271185029Spjd{ 272185029Spjd zprop_desc_t *prop_tbl; 273185029Spjd const zprop_index_t *idx_tbl; 274185029Spjd int i; 275185029Spjd 276185029Spjd if (prop == ZPROP_INVAL || prop == ZPROP_CONT) 277185029Spjd return (-1); 278185029Spjd 279185029Spjd ASSERT(prop < zprop_get_numprops(type)); 280185029Spjd prop_tbl = zprop_get_proptable(type); 281185029Spjd if ((idx_tbl = prop_tbl[prop].pd_table) == NULL) 282185029Spjd return (-1); 283185029Spjd 284185029Spjd for (i = 0; idx_tbl[i].pi_name != NULL; i++) { 285185029Spjd if (strcmp(string, idx_tbl[i].pi_name) == 0) { 286185029Spjd *index = idx_tbl[i].pi_value; 287185029Spjd return (0); 288185029Spjd } 289185029Spjd } 290185029Spjd 291185029Spjd return (-1); 292185029Spjd} 293185029Spjd 294185029Spjdint 295185029Spjdzprop_index_to_string(int prop, uint64_t index, const char **string, 296185029Spjd zfs_type_t type) 297185029Spjd{ 298185029Spjd zprop_desc_t *prop_tbl; 299185029Spjd const zprop_index_t *idx_tbl; 300185029Spjd int i; 301185029Spjd 302185029Spjd if (prop == ZPROP_INVAL || prop == ZPROP_CONT) 303185029Spjd return (-1); 304185029Spjd 305185029Spjd ASSERT(prop < zprop_get_numprops(type)); 306185029Spjd prop_tbl = zprop_get_proptable(type); 307185029Spjd if ((idx_tbl = prop_tbl[prop].pd_table) == NULL) 308185029Spjd return (-1); 309185029Spjd 310185029Spjd for (i = 0; idx_tbl[i].pi_name != NULL; i++) { 311185029Spjd if (idx_tbl[i].pi_value == index) { 312185029Spjd *string = idx_tbl[i].pi_name; 313185029Spjd return (0); 314185029Spjd } 315185029Spjd } 316185029Spjd 317185029Spjd return (-1); 318185029Spjd} 319185029Spjd 320219089Spjd/* 321219089Spjd * Return a random valid property value. Used by ztest. 322219089Spjd */ 323219089Spjduint64_t 324219089Spjdzprop_random_value(int prop, uint64_t seed, zfs_type_t type) 325219089Spjd{ 326219089Spjd zprop_desc_t *prop_tbl; 327219089Spjd const zprop_index_t *idx_tbl; 328219089Spjd 329219089Spjd ASSERT((uint_t)prop < zprop_get_numprops(type)); 330219089Spjd prop_tbl = zprop_get_proptable(type); 331219089Spjd idx_tbl = prop_tbl[prop].pd_table; 332219089Spjd 333219089Spjd if (idx_tbl == NULL) 334219089Spjd return (seed); 335219089Spjd 336219089Spjd return (idx_tbl[seed % prop_tbl[prop].pd_table_size].pi_value); 337219089Spjd} 338219089Spjd 339185029Spjdconst char * 340185029Spjdzprop_values(int prop, zfs_type_t type) 341185029Spjd{ 342185029Spjd zprop_desc_t *prop_tbl; 343185029Spjd 344185029Spjd ASSERT(prop != ZPROP_INVAL && prop != ZPROP_CONT); 345185029Spjd ASSERT(prop < zprop_get_numprops(type)); 346185029Spjd 347185029Spjd prop_tbl = zprop_get_proptable(type); 348185029Spjd 349185029Spjd return (prop_tbl[prop].pd_values); 350185029Spjd} 351185029Spjd 352185029Spjd/* 353185029Spjd * Returns TRUE if the property applies to any of the given dataset types. 354185029Spjd */ 355185029Spjdboolean_t 356185029Spjdzprop_valid_for_type(int prop, zfs_type_t type) 357185029Spjd{ 358185029Spjd zprop_desc_t *prop_tbl; 359185029Spjd 360185029Spjd if (prop == ZPROP_INVAL || prop == ZPROP_CONT) 361185029Spjd return (B_FALSE); 362185029Spjd 363185029Spjd ASSERT(prop < zprop_get_numprops(type)); 364185029Spjd prop_tbl = zprop_get_proptable(type); 365185029Spjd return ((prop_tbl[prop].pd_types & type) != 0); 366185029Spjd} 367185029Spjd 368185029Spjd#ifndef _KERNEL 369185029Spjd 370185029Spjd/* 371185029Spjd * Determines the minimum width for the column, and indicates whether it's fixed 372185029Spjd * or not. Only string columns are non-fixed. 373185029Spjd */ 374185029Spjdsize_t 375185029Spjdzprop_width(int prop, boolean_t *fixed, zfs_type_t type) 376185029Spjd{ 377185029Spjd zprop_desc_t *prop_tbl, *pd; 378185029Spjd const zprop_index_t *idx; 379185029Spjd size_t ret; 380185029Spjd int i; 381185029Spjd 382185029Spjd ASSERT(prop != ZPROP_INVAL && prop != ZPROP_CONT); 383185029Spjd ASSERT(prop < zprop_get_numprops(type)); 384185029Spjd 385185029Spjd prop_tbl = zprop_get_proptable(type); 386185029Spjd pd = &prop_tbl[prop]; 387185029Spjd 388185029Spjd *fixed = B_TRUE; 389185029Spjd 390185029Spjd /* 391185029Spjd * Start with the width of the column name. 392185029Spjd */ 393185029Spjd ret = strlen(pd->pd_colname); 394185029Spjd 395185029Spjd /* 396185029Spjd * For fixed-width values, make sure the width is large enough to hold 397185029Spjd * any possible value. 398185029Spjd */ 399185029Spjd switch (pd->pd_proptype) { 400185029Spjd case PROP_TYPE_NUMBER: 401185029Spjd /* 402185029Spjd * The maximum length of a human-readable number is 5 characters 403185029Spjd * ("20.4M", for example). 404185029Spjd */ 405185029Spjd if (ret < 5) 406185029Spjd ret = 5; 407185029Spjd /* 408185029Spjd * 'creation' is handled specially because it's a number 409185029Spjd * internally, but displayed as a date string. 410185029Spjd */ 411185029Spjd if (prop == ZFS_PROP_CREATION) 412185029Spjd *fixed = B_FALSE; 413185029Spjd break; 414185029Spjd case PROP_TYPE_INDEX: 415185029Spjd idx = prop_tbl[prop].pd_table; 416185029Spjd for (i = 0; idx[i].pi_name != NULL; i++) { 417185029Spjd if (strlen(idx[i].pi_name) > ret) 418185029Spjd ret = strlen(idx[i].pi_name); 419185029Spjd } 420185029Spjd break; 421185029Spjd 422185029Spjd case PROP_TYPE_STRING: 423185029Spjd *fixed = B_FALSE; 424185029Spjd break; 425185029Spjd } 426185029Spjd 427185029Spjd return (ret); 428185029Spjd} 429185029Spjd 430185029Spjd#endif 431