mirror_zfs/module/zcommon/zprop_common.c

508 lines
12 KiB
C
Raw Normal View History

2008-11-20 23:01:55 +03:00
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
2008-11-20 23:01:55 +03:00
* Use is subject to license terms.
*/
Illumos #2882, #2883, #2900 2882 implement libzfs_core 2883 changing "canmount" property to "on" should not always remount dataset 2900 "zfs snapshot" should be able to create multiple, arbitrary snapshots at once Reviewed by: George Wilson <george.wilson@delphix.com> Reviewed by: Chris Siden <christopher.siden@delphix.com> Reviewed by: Garrett D'Amore <garrett@damore.org> Reviewed by: Bill Pijewski <wdp@joyent.com> Reviewed by: Dan Kruchinin <dan.kruchinin@gmail.com> Approved by: Eric Schrock <Eric.Schrock@delphix.com> References: https://www.illumos.org/issues/2882 https://www.illumos.org/issues/2883 https://www.illumos.org/issues/2900 illumos/illumos-gate@4445fffbbb1ea25fd0e9ea68b9380dd7a6709025 Ported-by: Tim Chase <tim@chase2k.com> Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov> Closes #1293 Porting notes: WARNING: This patch changes the user/kernel ABI. That means that the zfs/zpool utilities built from master are NOT compatible with the 0.6.2 kernel modules. Ensure you load the matching kernel modules from master after updating the utilities. Otherwise the zfs/zpool commands will be unable to interact with your pool and you will see errors similar to the following: $ zpool list failed to read pool configuration: bad address no pools available $ zfs list no datasets available Add zvol minor device creation to the new zfs_snapshot_nvl function. Remove the logging of the "release" operation in dsl_dataset_user_release_sync(). The logging caused a null dereference because ds->ds_dir is zeroed in dsl_dataset_destroy_sync() and the logging functions try to get the ds name via the dsl_dataset_name() function. I've got no idea why this particular code would have worked in Illumos. This code has subsequently been completely reworked in Illumos commit 3b2aab1 (3464 zfs synctask code needs restructuring). Squash some "may be used uninitialized" warning/erorrs. Fix some printf format warnings for %lld and %llu. Apply a few spa_writeable() changes that were made to Illumos in illumos/illumos-gate.git@cd1c8b8 as part of the 3112, 3113, 3114 and 3115 fixes. Add a missing call to fnvlist_free(nvl) in log_internal() that was added in Illumos to fix issue 3085 but couldn't be ported to ZoL at the time (zfsonlinux/zfs@9e11c73) because it depended on future work.
2013-08-28 15:45:09 +04:00
/*
* Copyright (c) 2012 by Delphix. All rights reserved.
*/
2008-11-20 23:01:55 +03:00
/*
* Common routines used by zfs and zpool property management.
*/
#include <sys/zio.h>
#include <sys/spa.h>
#include <sys/zfs_acl.h>
#include <sys/zfs_ioctl.h>
#include <sys/zfs_sysfs.h>
2008-11-20 23:01:55 +03:00
#include <sys/zfs_znode.h>
#include <sys/fs/zfs.h>
#include "zfs_prop.h"
#include "zfs_deleg.h"
#if !defined(_KERNEL)
2008-11-20 23:01:55 +03:00
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>
2008-11-20 23:01:55 +03:00
#endif
static zprop_desc_t *
zprop_get_proptable(zfs_type_t type)
{
if (type == ZFS_TYPE_POOL)
return (zpool_prop_get_table());
else if (type == ZFS_TYPE_VDEV)
return (vdev_prop_get_table());
2008-11-20 23:01:55 +03:00
else
return (zfs_prop_get_table());
}
static int
zprop_get_numprops(zfs_type_t type)
{
if (type == ZFS_TYPE_POOL)
return (ZPOOL_NUM_PROPS);
else if (type == ZFS_TYPE_VDEV)
return (VDEV_NUM_PROPS);
2008-11-20 23:01:55 +03:00
else
return (ZFS_NUM_PROPS);
}
static boolean_t
zfs_mod_supported_prop(const char *name, zfs_type_t type,
const struct zfs_mod_supported_features *sfeatures)
{
/*
* The zfs module spa_feature_table[], whether in-kernel or in libzpool,
* always supports all the properties. libzfs needs to query the running
* module, via sysfs, to determine which properties are supported.
*
* The equivalent _can_ be done on FreeBSD by way of the sysctl
* tree, but this has not been done yet.
*/
#if defined(_KERNEL) || defined(LIB_ZPOOL_BUILD) || defined(__FreeBSD__)
(void) name, (void) type, (void) sfeatures;
return (B_TRUE);
#else
return (zfs_mod_supported(type == ZFS_TYPE_POOL ?
ZFS_SYSFS_POOL_PROPERTIES : (type == ZFS_TYPE_VDEV ?
ZFS_SYSFS_VDEV_PROPERTIES : ZFS_SYSFS_DATASET_PROPERTIES),
name, sfeatures));
#endif
}
2008-11-20 23:01:55 +03:00
void
zprop_register_impl(int prop, const char *name, zprop_type_t type,
2008-11-20 23:01:55 +03:00
uint64_t numdefault, const char *strdefault, zprop_attr_t attr,
int objset_types, const char *values, const char *colname,
boolean_t rightalign, boolean_t visible, const zprop_index_t *idx_tbl,
const struct zfs_mod_supported_features *sfeatures)
2008-11-20 23:01:55 +03:00
{
zprop_desc_t *prop_tbl = zprop_get_proptable(objset_types);
zprop_desc_t *pd;
pd = &prop_tbl[prop];
ASSERT(pd->pd_name == NULL || pd->pd_name == name);
ASSERT(name != NULL);
ASSERT(colname != NULL);
2008-11-20 23:01:55 +03:00
pd->pd_name = name;
pd->pd_propnum = prop;
pd->pd_proptype = type;
pd->pd_numdefault = numdefault;
pd->pd_strdefault = strdefault;
pd->pd_attr = attr;
pd->pd_types = objset_types;
pd->pd_values = values;
pd->pd_colname = colname;
pd->pd_rightalign = rightalign;
pd->pd_visible = visible;
pd->pd_zfs_mod_supported =
zfs_mod_supported_prop(name, objset_types, sfeatures);
2008-11-20 23:01:55 +03:00
pd->pd_table = idx_tbl;
pd->pd_table_size = 0;
while (idx_tbl && (idx_tbl++)->pi_name != NULL)
pd->pd_table_size++;
2008-11-20 23:01:55 +03:00
}
void
zprop_register_string(int prop, const char *name, const char *def,
2008-11-20 23:01:55 +03:00
zprop_attr_t attr, int objset_types, const char *values,
const char *colname, const struct zfs_mod_supported_features *sfeatures)
2008-11-20 23:01:55 +03:00
{
zprop_register_impl(prop, name, PROP_TYPE_STRING, 0, def, attr,
objset_types, values, colname, B_FALSE, B_TRUE, NULL, sfeatures);
2008-11-20 23:01:55 +03:00
}
void
zprop_register_number(int prop, const char *name, uint64_t def,
zprop_attr_t attr, int objset_types, const char *values,
const char *colname, const struct zfs_mod_supported_features *sfeatures)
2008-11-20 23:01:55 +03:00
{
zprop_register_impl(prop, name, PROP_TYPE_NUMBER, def, NULL, attr,
objset_types, values, colname, B_TRUE, B_TRUE, NULL, sfeatures);
2008-11-20 23:01:55 +03:00
}
void
zprop_register_index(int prop, const char *name, uint64_t def,
zprop_attr_t attr, int objset_types, const char *values,
const char *colname, const zprop_index_t *idx_tbl,
const struct zfs_mod_supported_features *sfeatures)
2008-11-20 23:01:55 +03:00
{
zprop_register_impl(prop, name, PROP_TYPE_INDEX, def, NULL, attr,
objset_types, values, colname, B_FALSE, B_TRUE, idx_tbl, sfeatures);
2008-11-20 23:01:55 +03:00
}
void
zprop_register_hidden(int prop, const char *name, zprop_type_t type,
zprop_attr_t attr, int objset_types, const char *colname,
const struct zfs_mod_supported_features *sfeatures)
2008-11-20 23:01:55 +03:00
{
zprop_register_impl(prop, name, type, 0, NULL, attr,
Illumos #2882, #2883, #2900 2882 implement libzfs_core 2883 changing "canmount" property to "on" should not always remount dataset 2900 "zfs snapshot" should be able to create multiple, arbitrary snapshots at once Reviewed by: George Wilson <george.wilson@delphix.com> Reviewed by: Chris Siden <christopher.siden@delphix.com> Reviewed by: Garrett D'Amore <garrett@damore.org> Reviewed by: Bill Pijewski <wdp@joyent.com> Reviewed by: Dan Kruchinin <dan.kruchinin@gmail.com> Approved by: Eric Schrock <Eric.Schrock@delphix.com> References: https://www.illumos.org/issues/2882 https://www.illumos.org/issues/2883 https://www.illumos.org/issues/2900 illumos/illumos-gate@4445fffbbb1ea25fd0e9ea68b9380dd7a6709025 Ported-by: Tim Chase <tim@chase2k.com> Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov> Closes #1293 Porting notes: WARNING: This patch changes the user/kernel ABI. That means that the zfs/zpool utilities built from master are NOT compatible with the 0.6.2 kernel modules. Ensure you load the matching kernel modules from master after updating the utilities. Otherwise the zfs/zpool commands will be unable to interact with your pool and you will see errors similar to the following: $ zpool list failed to read pool configuration: bad address no pools available $ zfs list no datasets available Add zvol minor device creation to the new zfs_snapshot_nvl function. Remove the logging of the "release" operation in dsl_dataset_user_release_sync(). The logging caused a null dereference because ds->ds_dir is zeroed in dsl_dataset_destroy_sync() and the logging functions try to get the ds name via the dsl_dataset_name() function. I've got no idea why this particular code would have worked in Illumos. This code has subsequently been completely reworked in Illumos commit 3b2aab1 (3464 zfs synctask code needs restructuring). Squash some "may be used uninitialized" warning/erorrs. Fix some printf format warnings for %lld and %llu. Apply a few spa_writeable() changes that were made to Illumos in illumos/illumos-gate.git@cd1c8b8 as part of the 3112, 3113, 3114 and 3115 fixes. Add a missing call to fnvlist_free(nvl) in log_internal() that was added in Illumos to fix issue 3085 but couldn't be ported to ZoL at the time (zfsonlinux/zfs@9e11c73) because it depended on future work.
2013-08-28 15:45:09 +04:00
objset_types, NULL, colname,
type == PROP_TYPE_NUMBER, B_FALSE, NULL, sfeatures);
2008-11-20 23:01:55 +03:00
}
/*
* A comparison function we can use to order indexes into property tables.
*/
static int
zprop_compare(const void *arg1, const void *arg2)
{
const zprop_desc_t *p1 = *((zprop_desc_t **)arg1);
const zprop_desc_t *p2 = *((zprop_desc_t **)arg2);
boolean_t p1ro, p2ro;
p1ro = (p1->pd_attr == PROP_READONLY);
p2ro = (p2->pd_attr == PROP_READONLY);
if (p1ro == p2ro)
return (strcmp(p1->pd_name, p2->pd_name));
return (p1ro ? -1 : 1);
}
/*
* Iterate over all properties in the given property table, calling back
* into the specified function for each property. We will continue to
* iterate until we either reach the end or the callback function returns
* something other than ZPROP_CONT.
*/
int
zprop_iter_common(zprop_func func, void *cb, boolean_t show_all,
boolean_t ordered, zfs_type_t type)
{
int i, num_props, size, prop;
2008-11-20 23:01:55 +03:00
zprop_desc_t *prop_tbl;
zprop_desc_t **order;
prop_tbl = zprop_get_proptable(type);
num_props = zprop_get_numprops(type);
size = num_props * sizeof (zprop_desc_t *);
#if defined(_KERNEL)
order = kmem_alloc(size, KM_SLEEP);
2008-11-20 23:01:55 +03:00
#else
if ((order = malloc(size)) == NULL)
return (ZPROP_CONT);
#endif
for (int j = 0; j < num_props; j++)
2008-11-20 23:01:55 +03:00
order[j] = &prop_tbl[j];
if (ordered) {
qsort((void *)order, num_props, sizeof (zprop_desc_t *),
zprop_compare);
}
prop = ZPROP_CONT;
for (i = 0; i < num_props; i++) {
if ((order[i]->pd_visible || show_all) &&
order[i]->pd_zfs_mod_supported &&
2008-11-20 23:01:55 +03:00
(func(order[i]->pd_propnum, cb) != ZPROP_CONT)) {
prop = order[i]->pd_propnum;
break;
}
}
#if defined(_KERNEL)
kmem_free(order, size);
#else
free(order);
#endif
return (prop);
}
static boolean_t
propname_match(const char *p, size_t len, zprop_desc_t *prop_entry)
{
const char *propname = prop_entry->pd_name;
#ifndef _KERNEL
const char *colname = prop_entry->pd_colname;
int c;
#endif
ASSERT(propname != NULL);
2008-11-20 23:01:55 +03:00
if (len == strlen(propname) &&
strncmp(p, propname, len) == 0)
return (B_TRUE);
#ifndef _KERNEL
2009-07-03 02:44:48 +04:00
if (colname == NULL || len != strlen(colname))
2008-11-20 23:01:55 +03:00
return (B_FALSE);
for (c = 0; c < len; c++)
if (p[c] != tolower(colname[c]))
break;
return (colname[c] == '\0');
#else
return (B_FALSE);
#endif
}
typedef struct name_to_prop_cb {
const char *propname;
zprop_desc_t *prop_tbl;
} name_to_prop_cb_t;
static int
zprop_name_to_prop_cb(int prop, void *cb_data)
{
name_to_prop_cb_t *data = cb_data;
if (propname_match(data->propname, strlen(data->propname),
&data->prop_tbl[prop]))
return (prop);
return (ZPROP_CONT);
}
int
zprop_name_to_prop(const char *propname, zfs_type_t type)
{
int prop;
name_to_prop_cb_t cb_data;
cb_data.propname = propname;
cb_data.prop_tbl = zprop_get_proptable(type);
prop = zprop_iter_common(zprop_name_to_prop_cb, &cb_data,
B_TRUE, B_FALSE, type);
return (prop == ZPROP_CONT ? ZPROP_INVAL : prop);
}
int
zprop_string_to_index(int prop, const char *string, uint64_t *index,
zfs_type_t type)
{
zprop_desc_t *prop_tbl;
const zprop_index_t *idx_tbl;
int i;
if (prop == ZPROP_INVAL || prop == ZPROP_CONT)
return (-1);
ASSERT(prop < zprop_get_numprops(type));
2008-11-20 23:01:55 +03:00
prop_tbl = zprop_get_proptable(type);
if ((idx_tbl = prop_tbl[prop].pd_table) == NULL)
return (-1);
for (i = 0; idx_tbl[i].pi_name != NULL; i++) {
if (strcmp(string, idx_tbl[i].pi_name) == 0) {
*index = idx_tbl[i].pi_value;
return (0);
}
}
return (-1);
}
int
zprop_index_to_string(int prop, uint64_t index, const char **string,
zfs_type_t type)
{
zprop_desc_t *prop_tbl;
const zprop_index_t *idx_tbl;
int i;
if (prop == ZPROP_INVAL || prop == ZPROP_CONT)
return (-1);
ASSERT(prop < zprop_get_numprops(type));
2008-11-20 23:01:55 +03:00
prop_tbl = zprop_get_proptable(type);
if ((idx_tbl = prop_tbl[prop].pd_table) == NULL)
return (-1);
for (i = 0; idx_tbl[i].pi_name != NULL; i++) {
if (idx_tbl[i].pi_value == index) {
*string = idx_tbl[i].pi_name;
return (0);
}
}
return (-1);
}
/*
* Return a random valid property value. Used by ztest.
*/
uint64_t
zprop_random_value(int prop, uint64_t seed, zfs_type_t type)
{
zprop_desc_t *prop_tbl;
const zprop_index_t *idx_tbl;
ASSERT((uint_t)prop < zprop_get_numprops(type));
prop_tbl = zprop_get_proptable(type);
idx_tbl = prop_tbl[prop].pd_table;
if (idx_tbl == NULL)
return (seed);
return (idx_tbl[seed % prop_tbl[prop].pd_table_size].pi_value);
}
2008-11-20 23:01:55 +03:00
const char *
zprop_values(int prop, zfs_type_t type)
{
zprop_desc_t *prop_tbl;
ASSERT(prop != ZPROP_INVAL && prop != ZPROP_CONT);
ASSERT(prop < zprop_get_numprops(type));
2008-11-20 23:01:55 +03:00
prop_tbl = zprop_get_proptable(type);
return (prop_tbl[prop].pd_values);
}
/*
* Returns TRUE if the property applies to any of the given dataset types.
*
* If headcheck is set, the check is being made against the head dataset
* type of a snapshot which requires to return B_TRUE when the property
* is only valid for snapshots.
2008-11-20 23:01:55 +03:00
*/
boolean_t
zprop_valid_for_type(int prop, zfs_type_t type, boolean_t headcheck)
2008-11-20 23:01:55 +03:00
{
zprop_desc_t *prop_tbl;
if (prop == ZPROP_INVAL || prop == ZPROP_CONT)
return (B_FALSE);
2008-11-20 23:01:55 +03:00
ASSERT(prop < zprop_get_numprops(type));
prop_tbl = zprop_get_proptable(type);
if (headcheck && prop_tbl[prop].pd_types == ZFS_TYPE_SNAPSHOT)
return (B_TRUE);
2008-11-20 23:01:55 +03:00
return ((prop_tbl[prop].pd_types & type) != 0);
}
/*
* For user property names, we allow all lowercase alphanumeric characters, plus
* a few useful punctuation characters.
*/
int
zprop_valid_char(char c)
{
return ((c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') ||
c == '-' || c == '_' || c == '.' || c == ':');
}
2008-11-20 23:01:55 +03:00
#ifndef _KERNEL
/*
* Determines the minimum width for the column, and indicates whether it's fixed
* or not. Only string columns are non-fixed.
*/
size_t
zprop_width(int prop, boolean_t *fixed, zfs_type_t type)
{
zprop_desc_t *prop_tbl, *pd;
const zprop_index_t *idx;
size_t ret;
int i;
ASSERT(prop != ZPROP_INVAL && prop != ZPROP_CONT);
ASSERT(prop < zprop_get_numprops(type));
2008-11-20 23:01:55 +03:00
prop_tbl = zprop_get_proptable(type);
pd = &prop_tbl[prop];
*fixed = B_TRUE;
/*
* Start with the width of the column name.
*/
ret = strlen(pd->pd_colname);
/*
* For fixed-width values, make sure the width is large enough to hold
* any possible value.
*/
switch (pd->pd_proptype) {
case PROP_TYPE_NUMBER:
/*
* The maximum length of a human-readable number is 5 characters
* ("20.4M", for example).
*/
if (ret < 5)
ret = 5;
/*
* 'creation' is handled specially because it's a number
* internally, but displayed as a date string.
*/
if (prop == ZFS_PROP_CREATION)
*fixed = B_FALSE;
/*
* 'health' is handled specially because it's a number
* internally, but displayed as a fixed 8 character string.
*/
if (prop == ZPOOL_PROP_HEALTH)
ret = 8;
2008-11-20 23:01:55 +03:00
break;
case PROP_TYPE_INDEX:
idx = prop_tbl[prop].pd_table;
for (i = 0; idx[i].pi_name != NULL; i++) {
if (strlen(idx[i].pi_name) > ret)
ret = strlen(idx[i].pi_name);
}
break;
case PROP_TYPE_STRING:
*fixed = B_FALSE;
break;
}
return (ret);
}
#endif
Update build system and packaging Minimal changes required to integrate the SPL sources in to the ZFS repository build infrastructure and packaging. Build system and packaging: * Renamed SPL_* autoconf m4 macros to ZFS_*. * Removed redundant SPL_* autoconf m4 macros. * Updated the RPM spec files to remove SPL package dependency. * The zfs package obsoletes the spl package, and the zfs-kmod package obsoletes the spl-kmod package. * The zfs-kmod-devel* packages were updated to add compatibility symlinks under /usr/src/spl-x.y.z until all dependent packages can be updated. They will be removed in a future release. * Updated copy-builtin script for in-kernel builds. * Updated DKMS package to include the spl.ko. * Updated stale AUTHORS file to include all contributors. * Updated stale COPYRIGHT and included the SPL as an exception. * Renamed README.markdown to README.md * Renamed OPENSOLARIS.LICENSE to LICENSE. * Renamed DISCLAIMER to NOTICE. Required code changes: * Removed redundant HAVE_SPL macro. * Removed _BOOT from nvpairs since it doesn't apply for Linux. * Initial header cleanup (removal of empty headers, refactoring). * Remove SPL repository clone/build from zimport.sh. * Use of DEFINE_RATELIMIT_STATE and DEFINE_SPINLOCK removed due to build issues when forcing C99 compilation. * Replaced legacy ACCESS_ONCE with READ_ONCE. * Include needed headers for `current` and `EXPORT_SYMBOL`. Reviewed-by: Tony Hutter <hutter2@llnl.gov> Reviewed-by: Olaf Faaland <faaland1@llnl.gov> Reviewed-by: Matthew Ahrens <mahrens@delphix.com> Reviewed-by: Pavel Zakharov <pavel.zakharov@delphix.com> Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov> TEST_ZIMPORT_SKIP="yes" Closes #7556
2018-02-16 04:53:18 +03:00
#if defined(_KERNEL)
/* Common routines to initialize property tables */
EXPORT_SYMBOL(zprop_register_impl);
EXPORT_SYMBOL(zprop_register_string);
EXPORT_SYMBOL(zprop_register_number);
EXPORT_SYMBOL(zprop_register_index);
EXPORT_SYMBOL(zprop_register_hidden);
/* Common routines for zfs and zpool property management */
EXPORT_SYMBOL(zprop_iter_common);
EXPORT_SYMBOL(zprop_name_to_prop);
EXPORT_SYMBOL(zprop_string_to_index);
EXPORT_SYMBOL(zprop_index_to_string);
EXPORT_SYMBOL(zprop_random_value);
EXPORT_SYMBOL(zprop_values);
EXPORT_SYMBOL(zprop_valid_for_type);
EXPORT_SYMBOL(zprop_valid_char);
#endif