Path: blob/main/sys/contrib/openzfs/module/zcommon/zprop_common.c
48383 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* 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.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2010 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/25/*26* Copyright (c) 2012 by Delphix. All rights reserved.27*/2829/*30* Common routines used by zfs and zpool property management.31*/3233#include <sys/zio.h>34#include <sys/spa.h>35#include <sys/zfs_acl.h>36#include <sys/zfs_ioctl.h>37#include <sys/zfs_sysfs.h>38#include <sys/zfs_znode.h>39#include <sys/fs/zfs.h>4041#include "zfs_prop.h"42#include "zfs_deleg.h"4344#if !defined(_KERNEL)45#include <stdlib.h>46#include <string.h>47#include <ctype.h>48#include <sys/stat.h>49#endif5051static zprop_desc_t *52zprop_get_proptable(zfs_type_t type)53{54if (type == ZFS_TYPE_POOL)55return (zpool_prop_get_table());56else if (type == ZFS_TYPE_VDEV)57return (vdev_prop_get_table());58else59return (zfs_prop_get_table());60}6162static int63zprop_get_numprops(zfs_type_t type)64{65if (type == ZFS_TYPE_POOL)66return (ZPOOL_NUM_PROPS);67else if (type == ZFS_TYPE_VDEV)68return (VDEV_NUM_PROPS);69else70return (ZFS_NUM_PROPS);71}7273static boolean_t74zfs_mod_supported_prop(const char *name, zfs_type_t type,75const struct zfs_mod_supported_features *sfeatures)76{77/*78* The zfs module spa_feature_table[], whether in-kernel or in libzpool,79* always supports all the properties. libzfs needs to query the running80* module, via sysfs, to determine which properties are supported.81*82* The equivalent _can_ be done on FreeBSD by way of the sysctl83* tree, but this has not been done yet.84*/85#if defined(_KERNEL) || defined(LIB_ZPOOL_BUILD) || defined(__FreeBSD__)86(void) name, (void) type, (void) sfeatures;87return (B_TRUE);88#else89return (zfs_mod_supported(type == ZFS_TYPE_POOL ?90ZFS_SYSFS_POOL_PROPERTIES : (type == ZFS_TYPE_VDEV ?91ZFS_SYSFS_VDEV_PROPERTIES : ZFS_SYSFS_DATASET_PROPERTIES),92name, sfeatures));93#endif94}9596void97zprop_register_impl(int prop, const char *name, zprop_type_t type,98uint64_t numdefault, const char *strdefault, zprop_attr_t attr,99int objset_types, const char *values, const char *colname,100boolean_t rightalign, boolean_t visible, boolean_t flex,101const zprop_index_t *idx_tbl,102const struct zfs_mod_supported_features *sfeatures)103{104zprop_desc_t *prop_tbl = zprop_get_proptable(objset_types);105zprop_desc_t *pd;106107pd = &prop_tbl[prop];108109ASSERT(pd->pd_name == NULL || pd->pd_name == name);110ASSERT(name != NULL);111ASSERT(colname != NULL);112113pd->pd_name = name;114pd->pd_propnum = prop;115pd->pd_proptype = type;116pd->pd_numdefault = numdefault;117pd->pd_strdefault = strdefault;118pd->pd_attr = attr;119pd->pd_types = objset_types;120pd->pd_values = values;121pd->pd_colname = colname;122pd->pd_rightalign = rightalign;123pd->pd_visible = visible;124pd->pd_zfs_mod_supported =125zfs_mod_supported_prop(name, objset_types, sfeatures);126pd->pd_always_flex = flex;127pd->pd_table = idx_tbl;128pd->pd_table_size = 0;129while (idx_tbl && (idx_tbl++)->pi_name != NULL)130pd->pd_table_size++;131}132133void134zprop_register_string(int prop, const char *name, const char *def,135zprop_attr_t attr, int objset_types, const char *values,136const char *colname, const struct zfs_mod_supported_features *sfeatures)137{138zprop_register_impl(prop, name, PROP_TYPE_STRING, 0, def, attr,139objset_types, values, colname, B_FALSE, B_TRUE, B_TRUE, NULL,140sfeatures);141142}143144void145zprop_register_number(int prop, const char *name, uint64_t def,146zprop_attr_t attr, int objset_types, const char *values,147const char *colname, boolean_t flex,148const struct zfs_mod_supported_features *sfeatures)149{150zprop_register_impl(prop, name, PROP_TYPE_NUMBER, def, NULL, attr,151objset_types, values, colname, B_TRUE, B_TRUE, flex, NULL,152sfeatures);153}154155void156zprop_register_index(int prop, const char *name, uint64_t def,157zprop_attr_t attr, int objset_types, const char *values,158const char *colname, const zprop_index_t *idx_tbl,159const struct zfs_mod_supported_features *sfeatures)160{161zprop_register_impl(prop, name, PROP_TYPE_INDEX, def, NULL, attr,162objset_types, values, colname, B_FALSE, B_TRUE, B_TRUE, idx_tbl,163sfeatures);164}165166void167zprop_register_hidden(int prop, const char *name, zprop_type_t type,168zprop_attr_t attr, int objset_types, const char *colname, boolean_t flex,169const struct zfs_mod_supported_features *sfeatures)170{171zprop_register_impl(prop, name, type, 0, NULL, attr,172objset_types, NULL, colname,173type == PROP_TYPE_NUMBER, B_FALSE, flex, NULL, sfeatures);174}175176177/*178* A comparison function we can use to order indexes into property tables.179*/180static int181zprop_compare(const void *arg1, const void *arg2)182{183const zprop_desc_t *p1 = *((zprop_desc_t **)arg1);184const zprop_desc_t *p2 = *((zprop_desc_t **)arg2);185boolean_t p1ro, p2ro;186187p1ro = (p1->pd_attr == PROP_READONLY);188p2ro = (p2->pd_attr == PROP_READONLY);189190if (p1ro == p2ro)191return (strcmp(p1->pd_name, p2->pd_name));192193return (p1ro ? -1 : 1);194}195196/*197* Iterate over all properties in the given property table, calling back198* into the specified function for each property. We will continue to199* iterate until we either reach the end or the callback function returns200* something other than ZPROP_CONT.201*/202int203zprop_iter_common(zprop_func func, void *cb, boolean_t show_all,204boolean_t ordered, zfs_type_t type)205{206int i, num_props, size, prop;207zprop_desc_t *prop_tbl;208zprop_desc_t **order;209210prop_tbl = zprop_get_proptable(type);211num_props = zprop_get_numprops(type);212size = num_props * sizeof (zprop_desc_t *);213214#if defined(_KERNEL)215order = kmem_alloc(size, KM_SLEEP);216#else217if ((order = malloc(size)) == NULL)218return (ZPROP_CONT);219#endif220221for (int j = 0; j < num_props; j++)222order[j] = &prop_tbl[j];223224if (ordered) {225qsort((void *)order, num_props, sizeof (zprop_desc_t *),226zprop_compare);227}228229prop = ZPROP_CONT;230for (i = 0; i < num_props; i++) {231if ((order[i]->pd_visible || show_all) &&232order[i]->pd_zfs_mod_supported &&233(func(order[i]->pd_propnum, cb) != ZPROP_CONT)) {234prop = order[i]->pd_propnum;235break;236}237}238239#if defined(_KERNEL)240kmem_free(order, size);241#else242free(order);243#endif244return (prop);245}246247static boolean_t248propname_match(const char *p, size_t len, zprop_desc_t *prop_entry)249{250const char *propname = prop_entry->pd_name;251#ifndef _KERNEL252const char *colname = prop_entry->pd_colname;253int c;254#endif255256ASSERT(propname != NULL);257258if (len == strlen(propname) &&259strncmp(p, propname, len) == 0)260return (B_TRUE);261262#ifndef _KERNEL263if (colname == NULL || len != strlen(colname))264return (B_FALSE);265266for (c = 0; c < len; c++)267if (p[c] != tolower(colname[c]))268break;269270return (colname[c] == '\0');271#else272return (B_FALSE);273#endif274}275276typedef struct name_to_prop_cb {277const char *propname;278zprop_desc_t *prop_tbl;279} name_to_prop_cb_t;280281static int282zprop_name_to_prop_cb(int prop, void *cb_data)283{284name_to_prop_cb_t *data = cb_data;285286if (propname_match(data->propname, strlen(data->propname),287&data->prop_tbl[prop]))288return (prop);289290return (ZPROP_CONT);291}292293int294zprop_name_to_prop(const char *propname, zfs_type_t type)295{296int prop;297name_to_prop_cb_t cb_data;298299cb_data.propname = propname;300cb_data.prop_tbl = zprop_get_proptable(type);301302prop = zprop_iter_common(zprop_name_to_prop_cb, &cb_data,303B_TRUE, B_FALSE, type);304305return (prop == ZPROP_CONT ? ZPROP_INVAL : prop);306}307308int309zprop_string_to_index(int prop, const char *string, uint64_t *index,310zfs_type_t type)311{312zprop_desc_t *prop_tbl;313const zprop_index_t *idx_tbl;314int i;315316if (prop == ZPROP_INVAL || prop == ZPROP_CONT)317return (-1);318319ASSERT(prop < zprop_get_numprops(type));320prop_tbl = zprop_get_proptable(type);321if ((idx_tbl = prop_tbl[prop].pd_table) == NULL)322return (-1);323324for (i = 0; idx_tbl[i].pi_name != NULL; i++) {325if (strcmp(string, idx_tbl[i].pi_name) == 0) {326*index = idx_tbl[i].pi_value;327return (0);328}329}330331return (-1);332}333334int335zprop_index_to_string(int prop, uint64_t index, const char **string,336zfs_type_t type)337{338zprop_desc_t *prop_tbl;339const zprop_index_t *idx_tbl;340int i;341342if (prop == ZPROP_INVAL || prop == ZPROP_CONT)343return (-1);344345ASSERT(prop < zprop_get_numprops(type));346prop_tbl = zprop_get_proptable(type);347if ((idx_tbl = prop_tbl[prop].pd_table) == NULL)348return (-1);349350for (i = 0; idx_tbl[i].pi_name != NULL; i++) {351if (idx_tbl[i].pi_value == index) {352*string = idx_tbl[i].pi_name;353return (0);354}355}356357return (-1);358}359360/*361* Return a random valid property value. Used by ztest.362*/363uint64_t364zprop_random_value(int prop, uint64_t seed, zfs_type_t type)365{366zprop_desc_t *prop_tbl;367const zprop_index_t *idx_tbl;368369ASSERT((uint_t)prop < zprop_get_numprops(type));370prop_tbl = zprop_get_proptable(type);371idx_tbl = prop_tbl[prop].pd_table;372373if (idx_tbl == NULL)374return (seed);375376return (idx_tbl[seed % prop_tbl[prop].pd_table_size].pi_value);377}378379const char *380zprop_values(int prop, zfs_type_t type)381{382zprop_desc_t *prop_tbl;383384ASSERT(prop != ZPROP_INVAL && prop != ZPROP_CONT);385ASSERT(prop < zprop_get_numprops(type));386387prop_tbl = zprop_get_proptable(type);388389return (prop_tbl[prop].pd_values);390}391392/*393* Returns TRUE if the property applies to any of the given dataset types.394*395* If headcheck is set, the check is being made against the head dataset396* type of a snapshot which requires to return B_TRUE when the property397* is only valid for snapshots.398*/399boolean_t400zprop_valid_for_type(int prop, zfs_type_t type, boolean_t headcheck)401{402zprop_desc_t *prop_tbl;403404if (prop == ZPROP_INVAL || prop == ZPROP_CONT)405return (B_FALSE);406407ASSERT(prop < zprop_get_numprops(type));408prop_tbl = zprop_get_proptable(type);409if (headcheck && prop_tbl[prop].pd_types == ZFS_TYPE_SNAPSHOT)410return (B_TRUE);411return ((prop_tbl[prop].pd_types & type) != 0);412}413414/*415* For user property names, we allow all lowercase alphanumeric characters, plus416* a few useful punctuation characters.417*/418int419zprop_valid_char(char c)420{421return ((c >= 'a' && c <= 'z') ||422(c >= '0' && c <= '9') ||423c == '-' || c == '_' || c == '.' || c == ':');424}425426#ifndef _KERNEL427428/*429* Determines the minimum width for the column, and indicates whether it's fixed430* or not. Only string columns are non-fixed.431*/432size_t433zprop_width(int prop, boolean_t *fixed, zfs_type_t type)434{435zprop_desc_t *prop_tbl, *pd;436const zprop_index_t *idx;437size_t ret;438int i;439440ASSERT(prop != ZPROP_INVAL && prop != ZPROP_CONT);441ASSERT(prop < zprop_get_numprops(type));442443prop_tbl = zprop_get_proptable(type);444pd = &prop_tbl[prop];445446if (type != ZFS_TYPE_POOL && type != ZFS_TYPE_VDEV)447type = ZFS_TYPE_FILESYSTEM;448449*fixed = !pd->pd_always_flex;450451/*452* Start with the width of the column name.453*/454ret = strlen(pd->pd_colname);455456/*457* For fixed-width values, make sure the width is large enough to hold458* any possible value.459*/460switch (pd->pd_proptype) {461case PROP_TYPE_NUMBER:462/*463* The maximum length of a human-readable number is 5 characters464* ("20.4M", for example).465*/466if (ret < 5)467ret = 5;468/*469* 'health' is handled specially because it's a number470* internally, but displayed as a fixed 8 character string.471*/472if (type == ZFS_TYPE_POOL && prop == ZPOOL_PROP_HEALTH)473ret = 8;474break;475476case PROP_TYPE_INDEX:477idx = prop_tbl[prop].pd_table;478for (i = 0; idx[i].pi_name != NULL; i++) {479if (strlen(idx[i].pi_name) > ret)480ret = strlen(idx[i].pi_name);481}482break;483484case PROP_TYPE_STRING:485break;486}487488return (ret);489}490491#endif492493#if defined(_KERNEL)494/* Common routines to initialize property tables */495EXPORT_SYMBOL(zprop_register_impl);496EXPORT_SYMBOL(zprop_register_string);497EXPORT_SYMBOL(zprop_register_number);498EXPORT_SYMBOL(zprop_register_index);499EXPORT_SYMBOL(zprop_register_hidden);500501/* Common routines for zfs and zpool property management */502EXPORT_SYMBOL(zprop_iter_common);503EXPORT_SYMBOL(zprop_name_to_prop);504EXPORT_SYMBOL(zprop_string_to_index);505EXPORT_SYMBOL(zprop_index_to_string);506EXPORT_SYMBOL(zprop_random_value);507EXPORT_SYMBOL(zprop_values);508EXPORT_SYMBOL(zprop_valid_for_type);509EXPORT_SYMBOL(zprop_valid_char);510#endif511512513