libzfs_dataset.c revision 30925561c223021e91d15899cbe75f80e54d8889
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER START
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * The contents of this file are subject to the terms of the
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * Common Development and Distribution License (the "License").
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * You may not use this file except in compliance with the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fa9e4066f08beec538e775443c5be79dd423fcabahrens * or http://www.opensolaris.org/os/licensing.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * See the License for the specific language governing permissions
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and limitations under the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * When distributing Covered Code, include this CDDL HEADER in each
fa9e4066f08beec538e775443c5be79dd423fcabahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If applicable, add the following below this CDDL HEADER, with the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * fields enclosed by brackets "[]" replaced with your own identifying
fa9e4066f08beec538e775443c5be79dd423fcabahrens * information: Portions Copyright [yyyy] [name of copyright owner]
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER END
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
1195e687f1c03c8d57417b5999578922e20a3554Mark J Musante/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Copyright (c) 2013, Joyent, Inc. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Copyright (c) 2013 Martin Matuska. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Copyright (c) 2013 Steven Hartland. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <ctype.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <errno.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <libintl.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <math.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <stdio.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <stdlib.h>
990b4856d0eaada6f8140335733a1b1771ed2746lling#include <strings.h>
990b4856d0eaada6f8140335733a1b1771ed2746lling#include <unistd.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <stddef.h>
5aba80db367b061758a29154d304977d00d8e4f4ck#include <zone.h>
5aba80db367b061758a29154d304977d00d8e4f4ck#include <fcntl.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/mntent.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/mount.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <priv.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <pwd.h>
91ebeef555ce7f899b6270a3c2df47b51f7ad59aahrens#include <grp.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <stddef.h>
99653d4ee642c6528e88224f12409a5f23060994eschrock#include <ucred.h>
99653d4ee642c6528e88224f12409a5f23060994eschrock#include <idmap.h>
99653d4ee642c6528e88224f12409a5f23060994eschrock#include <aclutils.h>
99653d4ee642c6528e88224f12409a5f23060994eschrock#include <directory.h>
99653d4ee642c6528e88224f12409a5f23060994eschrock
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dnode.h>
99653d4ee642c6528e88224f12409a5f23060994eschrock#include <sys/spa.h>
99653d4ee642c6528e88224f12409a5f23060994eschrock#include <sys/zap.h>
99653d4ee642c6528e88224f12409a5f23060994eschrock#include <libzfs.h>
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock#include "zfs_namecheck.h"
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include "zfs_prop.h"
99653d4ee642c6528e88224f12409a5f23060994eschrock#include "libzfs_impl.h"
99653d4ee642c6528e88224f12409a5f23060994eschrock#include "zfs_deleg.h"
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrockstatic int userquota_propname_decode(const char *propname, boolean_t zoned,
99653d4ee642c6528e88224f12409a5f23060994eschrock zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock/*
99653d4ee642c6528e88224f12409a5f23060994eschrock * Given a single type (not a mask of types), return the type in a human
99653d4ee642c6528e88224f12409a5f23060994eschrock * readable form.
99653d4ee642c6528e88224f12409a5f23060994eschrock */
99653d4ee642c6528e88224f12409a5f23060994eschrockconst char *
99653d4ee642c6528e88224f12409a5f23060994eschrockzfs_type_to_name(zfs_type_t type)
99653d4ee642c6528e88224f12409a5f23060994eschrock{
99653d4ee642c6528e88224f12409a5f23060994eschrock switch (type) {
99653d4ee642c6528e88224f12409a5f23060994eschrock case ZFS_TYPE_FILESYSTEM:
99653d4ee642c6528e88224f12409a5f23060994eschrock return (dgettext(TEXT_DOMAIN, "filesystem"));
99653d4ee642c6528e88224f12409a5f23060994eschrock case ZFS_TYPE_SNAPSHOT:
99653d4ee642c6528e88224f12409a5f23060994eschrock return (dgettext(TEXT_DOMAIN, "snapshot"));
99653d4ee642c6528e88224f12409a5f23060994eschrock case ZFS_TYPE_VOLUME:
99653d4ee642c6528e88224f12409a5f23060994eschrock return (dgettext(TEXT_DOMAIN, "volume"));
99653d4ee642c6528e88224f12409a5f23060994eschrock }
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock return (NULL);
99653d4ee642c6528e88224f12409a5f23060994eschrock}
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock/*
99653d4ee642c6528e88224f12409a5f23060994eschrock * Given a path and mask of ZFS types, return a string describing this dataset.
99653d4ee642c6528e88224f12409a5f23060994eschrock * This is used when we fail to open a dataset and we cannot get an exact type.
99653d4ee642c6528e88224f12409a5f23060994eschrock * We guess what the type would have been based on the path and the mask of
99653d4ee642c6528e88224f12409a5f23060994eschrock * acceptable types.
99653d4ee642c6528e88224f12409a5f23060994eschrock */
99653d4ee642c6528e88224f12409a5f23060994eschrockstatic const char *
99653d4ee642c6528e88224f12409a5f23060994eschrockpath_to_str(const char *path, int types)
99653d4ee642c6528e88224f12409a5f23060994eschrock{
99653d4ee642c6528e88224f12409a5f23060994eschrock /*
99653d4ee642c6528e88224f12409a5f23060994eschrock * When given a single type, always report the exact type.
99653d4ee642c6528e88224f12409a5f23060994eschrock */
99653d4ee642c6528e88224f12409a5f23060994eschrock if (types == ZFS_TYPE_SNAPSHOT)
99653d4ee642c6528e88224f12409a5f23060994eschrock return (dgettext(TEXT_DOMAIN, "snapshot"));
99653d4ee642c6528e88224f12409a5f23060994eschrock if (types == ZFS_TYPE_FILESYSTEM)
99653d4ee642c6528e88224f12409a5f23060994eschrock return (dgettext(TEXT_DOMAIN, "filesystem"));
99653d4ee642c6528e88224f12409a5f23060994eschrock if (types == ZFS_TYPE_VOLUME)
99653d4ee642c6528e88224f12409a5f23060994eschrock return (dgettext(TEXT_DOMAIN, "volume"));
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock /*
99653d4ee642c6528e88224f12409a5f23060994eschrock * The user is requesting more than one type of dataset. If this is the
99653d4ee642c6528e88224f12409a5f23060994eschrock * case, consult the path itself. If we're looking for a snapshot, and
99653d4ee642c6528e88224f12409a5f23060994eschrock * a '@' is found, then report it as "snapshot". Otherwise, remove the
99653d4ee642c6528e88224f12409a5f23060994eschrock * snapshot attribute and try again.
99653d4ee642c6528e88224f12409a5f23060994eschrock */
99653d4ee642c6528e88224f12409a5f23060994eschrock if (types & ZFS_TYPE_SNAPSHOT) {
99653d4ee642c6528e88224f12409a5f23060994eschrock if (strchr(path, '@') != NULL)
99653d4ee642c6528e88224f12409a5f23060994eschrock return (dgettext(TEXT_DOMAIN, "snapshot"));
99653d4ee642c6528e88224f12409a5f23060994eschrock return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
99653d4ee642c6528e88224f12409a5f23060994eschrock }
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock /*
99653d4ee642c6528e88224f12409a5f23060994eschrock * The user has requested either filesystems or volumes.
99653d4ee642c6528e88224f12409a5f23060994eschrock * We have no way of knowing a priori what type this would be, so always
99653d4ee642c6528e88224f12409a5f23060994eschrock * report it as "filesystem" or "volume", our two primitive types.
99653d4ee642c6528e88224f12409a5f23060994eschrock */
99653d4ee642c6528e88224f12409a5f23060994eschrock if (types & ZFS_TYPE_FILESYSTEM)
99653d4ee642c6528e88224f12409a5f23060994eschrock return (dgettext(TEXT_DOMAIN, "filesystem"));
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock assert(types & ZFS_TYPE_VOLUME);
99653d4ee642c6528e88224f12409a5f23060994eschrock return (dgettext(TEXT_DOMAIN, "volume"));
99653d4ee642c6528e88224f12409a5f23060994eschrock}
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock/*
99653d4ee642c6528e88224f12409a5f23060994eschrock * Validate a ZFS path. This is used even before trying to open the dataset, to
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * provide a more meaningful error message. We call zfs_error_aux() to
99653d4ee642c6528e88224f12409a5f23060994eschrock * explain exactly why the name was not valid.
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl */
99653d4ee642c6528e88224f12409a5f23060994eschrockint
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwzfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw boolean_t modifying)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw namecheck_err_t why;
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks char what;
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks (void) zfs_prop_get_table();
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks if (dataset_namecheck(path, &why, &what) != 0) {
99653d4ee642c6528e88224f12409a5f23060994eschrock if (hdl != NULL) {
99653d4ee642c6528e88224f12409a5f23060994eschrock switch (why) {
99653d4ee642c6528e88224f12409a5f23060994eschrock case NAME_ERR_TOOLONG:
99653d4ee642c6528e88224f12409a5f23060994eschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6e27f86863a47869e7b2514d7445d3ea6fc6cf4aSam Falkner "name is too long"));
6e27f86863a47869e7b2514d7445d3ea6fc6cf4aSam Falkner break;
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock case NAME_ERR_LEADING_SLASH:
99653d4ee642c6528e88224f12409a5f23060994eschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
99653d4ee642c6528e88224f12409a5f23060994eschrock "leading slash in name"));
99653d4ee642c6528e88224f12409a5f23060994eschrock break;
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock case NAME_ERR_EMPTY_COMPONENT:
99653d4ee642c6528e88224f12409a5f23060994eschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
99653d4ee642c6528e88224f12409a5f23060994eschrock "empty component in name"));
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrock break;
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrock
06eeb2ad640ce72d394ac521094bed7681044408ek case NAME_ERR_TRAILING_SLASH:
06eeb2ad640ce72d394ac521094bed7681044408ek zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl "trailing slash in name"));
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl break;
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl case NAME_ERR_INVALCHAR:
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl zfs_error_aux(hdl,
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl dgettext(TEXT_DOMAIN, "invalid character "
b1b8ab34de515a5e83206da22c3d7e563241b021lling "'%c' in name"), what);
b1b8ab34de515a5e83206da22c3d7e563241b021lling break;
b1b8ab34de515a5e83206da22c3d7e563241b021lling
b1b8ab34de515a5e83206da22c3d7e563241b021lling case NAME_ERR_MULTIPLE_AT:
b1b8ab34de515a5e83206da22c3d7e563241b021lling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
b1b8ab34de515a5e83206da22c3d7e563241b021lling "multiple '@' delimiters in name"));
b1b8ab34de515a5e83206da22c3d7e563241b021lling break;
b1b8ab34de515a5e83206da22c3d7e563241b021lling
b1b8ab34de515a5e83206da22c3d7e563241b021lling case NAME_ERR_NOLETTER:
b7661ccca92e6bf5160f4d5d2601efaeaa1f5161mmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
b7661ccca92e6bf5160f4d5d2601efaeaa1f5161mmusante "pool doesn't begin with a letter"));
8488aeb5df27784d479c16cde06a9e25cd9a1152taylor break;
8488aeb5df27784d479c16cde06a9e25cd9a1152taylor
8488aeb5df27784d479c16cde06a9e25cd9a1152taylor case NAME_ERR_RESERVED:
8488aeb5df27784d479c16cde06a9e25cd9a1152taylor zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
8488aeb5df27784d479c16cde06a9e25cd9a1152taylor "name is reserved"));
8488aeb5df27784d479c16cde06a9e25cd9a1152taylor break;
8488aeb5df27784d479c16cde06a9e25cd9a1152taylor
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks case NAME_ERR_DISKLIKE:
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks "reserved disk name"));
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks break;
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks }
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks }
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks return (0);
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks }
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks if (hdl != NULL)
2f8aaab38e6371ad39ed90a1211ba8921acbb4d5eschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2f8aaab38e6371ad39ed90a1211ba8921acbb4d5eschrock "snapshot delimiter '@' in filesystem name"));
fa94a07fd0519b8abfd871ad8fe60e6bebe1e2bbbrendan return (0);
fa94a07fd0519b8abfd871ad8fe60e6bebe1e2bbbrendan }
e7cbe64f7a72dae5cb44f100db60ca88f3313c65gw
e7cbe64f7a72dae5cb44f100db60ca88f3313c65gw if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
e7cbe64f7a72dae5cb44f100db60ca88f3313c65gw if (hdl != NULL)
15e6edf145a9c2bb0e0272cf8debe823bb97529bgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15e6edf145a9c2bb0e0272cf8debe823bb97529bgw "missing '@' delimiter in snapshot name"));
15e6edf145a9c2bb0e0272cf8debe823bb97529bgw return (0);
89a89ebfd7c3b4056afe2c03e959e22824df777dlling }
89a89ebfd7c3b4056afe2c03e959e22824df777dlling
89a89ebfd7c3b4056afe2c03e959e22824df777dlling if (modifying && strchr(path, '%') != NULL) {
e6ca193ded880d478cc39e34ef82d4be36e4445dGeorge Wilson if (hdl != NULL)
e6ca193ded880d478cc39e34ef82d4be36e4445dGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
e6ca193ded880d478cc39e34ef82d4be36e4445dGeorge Wilson "invalid character %c in name"), '%');
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby return (0);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby }
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby return (-1);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby}
ca45db4129beff691dc46576c328149443788af2Chris Kirby
ca45db4129beff691dc46576c328149443788af2Chris Kirbyint
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Altzfs_name_valid(const char *name, zfs_type_t type)
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt{
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt if (type == ZFS_TYPE_POOL)
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt return (zpool_name_valid(NULL, B_FALSE, name));
1195e687f1c03c8d57417b5999578922e20a3554Mark J Musante return (zfs_validate_name(NULL, name, type, B_FALSE));
1195e687f1c03c8d57417b5999578922e20a3554Mark J Musante}
1195e687f1c03c8d57417b5999578922e20a3554Mark J Musante
99653d4ee642c6528e88224f12409a5f23060994eschrock/*
99653d4ee642c6528e88224f12409a5f23060994eschrock * This function takes the raw DSL properties, and filters out the user-defined
99653d4ee642c6528e88224f12409a5f23060994eschrock * properties into a separate nvlist.
c08432ebe688ed6410b302771a9afd8e23e2a7cceschrock */
c08432ebe688ed6410b302771a9afd8e23e2a7cceschrockstatic nvlist_t *
99653d4ee642c6528e88224f12409a5f23060994eschrockprocess_user_props(zfs_handle_t *zhp, nvlist_t *props)
99653d4ee642c6528e88224f12409a5f23060994eschrock{
99653d4ee642c6528e88224f12409a5f23060994eschrock libzfs_handle_t *hdl = zhp->zfs_hdl;
99653d4ee642c6528e88224f12409a5f23060994eschrock nvpair_t *elem;
fa9e4066f08beec538e775443c5be79dd423fcabahrens nvlist_t *propval;
99653d4ee642c6528e88224f12409a5f23060994eschrock nvlist_t *nvl;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens (void) no_memory(hdl);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock elem = NULL;
99653d4ee642c6528e88224f12409a5f23060994eschrock while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
99653d4ee642c6528e88224f12409a5f23060994eschrock if (!zfs_prop_user(nvpair_name(elem)))
99653d4ee642c6528e88224f12409a5f23060994eschrock continue;
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock verify(nvpair_value_nvlist(elem, &propval) == 0);
99653d4ee642c6528e88224f12409a5f23060994eschrock if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
99653d4ee642c6528e88224f12409a5f23060994eschrock nvlist_free(nvl);
99653d4ee642c6528e88224f12409a5f23060994eschrock (void) no_memory(hdl);
99653d4ee642c6528e88224f12409a5f23060994eschrock return (NULL);
99653d4ee642c6528e88224f12409a5f23060994eschrock }
99653d4ee642c6528e88224f12409a5f23060994eschrock }
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock return (nvl);
99653d4ee642c6528e88224f12409a5f23060994eschrock}
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrockstatic zpool_handle_t *
99653d4ee642c6528e88224f12409a5f23060994eschrockzpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
99653d4ee642c6528e88224f12409a5f23060994eschrock{
99653d4ee642c6528e88224f12409a5f23060994eschrock libzfs_handle_t *hdl = zhp->zfs_hdl;
99653d4ee642c6528e88224f12409a5f23060994eschrock zpool_handle_t *zph;
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
99653d4ee642c6528e88224f12409a5f23060994eschrock if (hdl->libzfs_pool_handles != NULL)
99653d4ee642c6528e88224f12409a5f23060994eschrock zph->zpool_next = hdl->libzfs_pool_handles;
99653d4ee642c6528e88224f12409a5f23060994eschrock hdl->libzfs_pool_handles = zph;
b1b8ab34de515a5e83206da22c3d7e563241b021lling }
99653d4ee642c6528e88224f12409a5f23060994eschrock return (zph);
99653d4ee642c6528e88224f12409a5f23060994eschrock}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
99653d4ee642c6528e88224f12409a5f23060994eschrockstatic zpool_handle_t *
99653d4ee642c6528e88224f12409a5f23060994eschrockzpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
ece3d9b3bacef51a5f34d993935eedbb7bb87059lling{
ece3d9b3bacef51a5f34d993935eedbb7bb87059lling libzfs_handle_t *hdl = zhp->zfs_hdl;
ece3d9b3bacef51a5f34d993935eedbb7bb87059lling zpool_handle_t *zph = hdl->libzfs_pool_handles;
ece3d9b3bacef51a5f34d993935eedbb7bb87059lling
ece3d9b3bacef51a5f34d993935eedbb7bb87059lling while ((zph != NULL) &&
ece3d9b3bacef51a5f34d993935eedbb7bb87059lling (strncmp(pool_name, zpool_get_name(zph), len) != 0))
99653d4ee642c6528e88224f12409a5f23060994eschrock zph = zph->zpool_next;
99653d4ee642c6528e88224f12409a5f23060994eschrock return (zph);
ece3d9b3bacef51a5f34d993935eedbb7bb87059lling}
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock/*
99653d4ee642c6528e88224f12409a5f23060994eschrock * Returns a handle to the pool that contains the provided dataset.
99653d4ee642c6528e88224f12409a5f23060994eschrock * If a handle to that pool already exists then that handle is returned.
99653d4ee642c6528e88224f12409a5f23060994eschrock * Otherwise, a new handle is created and added to the list of handles.
99653d4ee642c6528e88224f12409a5f23060994eschrock */
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic zpool_handle_t *
fa9e4066f08beec538e775443c5be79dd423fcabahrenszpool_handle(zfs_handle_t *zhp)
99653d4ee642c6528e88224f12409a5f23060994eschrock{
99653d4ee642c6528e88224f12409a5f23060994eschrock char *pool_name;
fa9e4066f08beec538e775443c5be79dd423fcabahrens int len;
fa9e4066f08beec538e775443c5be79dd423fcabahrens zpool_handle_t *zph;
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock len = strcspn(zhp->zfs_name, "/@#") + 1;
99653d4ee642c6528e88224f12409a5f23060994eschrock pool_name = zfs_alloc(zhp->zfs_hdl, len);
99653d4ee642c6528e88224f12409a5f23060994eschrock (void) strlcpy(pool_name, zhp->zfs_name, len);
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock zph = zpool_find_handle(zhp, pool_name, len);
99653d4ee642c6528e88224f12409a5f23060994eschrock if (zph == NULL)
99653d4ee642c6528e88224f12409a5f23060994eschrock zph = zpool_add_handle(zhp, pool_name);
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock free(pool_name);
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks return (zph);
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks}
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarksvoid
99653d4ee642c6528e88224f12409a5f23060994eschrockzpool_free_handles(libzfs_handle_t *hdl)
99653d4ee642c6528e88224f12409a5f23060994eschrock{
99653d4ee642c6528e88224f12409a5f23060994eschrock zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
99653d4ee642c6528e88224f12409a5f23060994eschrock
6e27f86863a47869e7b2514d7445d3ea6fc6cf4aSam Falkner while (zph != NULL) {
6e27f86863a47869e7b2514d7445d3ea6fc6cf4aSam Falkner next = zph->zpool_next;
6e27f86863a47869e7b2514d7445d3ea6fc6cf4aSam Falkner zpool_close(zph);
6e27f86863a47869e7b2514d7445d3ea6fc6cf4aSam Falkner zph = next;
99653d4ee642c6528e88224f12409a5f23060994eschrock }
99653d4ee642c6528e88224f12409a5f23060994eschrock hdl->libzfs_pool_handles = NULL;
99653d4ee642c6528e88224f12409a5f23060994eschrock}
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock/*
99653d4ee642c6528e88224f12409a5f23060994eschrock * Utility function to gather stats (objset and zpl) for the given object.
99653d4ee642c6528e88224f12409a5f23060994eschrock */
99653d4ee642c6528e88224f12409a5f23060994eschrockstatic int
ece3d9b3bacef51a5f34d993935eedbb7bb87059llingget_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
ece3d9b3bacef51a5f34d993935eedbb7bb87059lling{
ece3d9b3bacef51a5f34d993935eedbb7bb87059lling libzfs_handle_t *hdl = zhp->zfs_hdl;
ece3d9b3bacef51a5f34d993935eedbb7bb87059lling
ece3d9b3bacef51a5f34d993935eedbb7bb87059lling (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
ece3d9b3bacef51a5f34d993935eedbb7bb87059lling
99653d4ee642c6528e88224f12409a5f23060994eschrock while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
99653d4ee642c6528e88224f12409a5f23060994eschrock if (errno == ENOMEM) {
ece3d9b3bacef51a5f34d993935eedbb7bb87059lling if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens } else {
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
99653d4ee642c6528e88224f12409a5f23060994eschrock }
99653d4ee642c6528e88224f12409a5f23060994eschrock return (0);
99653d4ee642c6528e88224f12409a5f23060994eschrock}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
99653d4ee642c6528e88224f12409a5f23060994eschrock * Utility function to get the received properties of the given object.
99653d4ee642c6528e88224f12409a5f23060994eschrock */
97d9e3a676d96208790c885c4766194423e84b24ckstatic int
99653d4ee642c6528e88224f12409a5f23060994eschrockget_recvd_props_ioctl(zfs_handle_t *zhp)
99653d4ee642c6528e88224f12409a5f23060994eschrock{
99653d4ee642c6528e88224f12409a5f23060994eschrock libzfs_handle_t *hdl = zhp->zfs_hdl;
99653d4ee642c6528e88224f12409a5f23060994eschrock nvlist_t *recvdprops;
99653d4ee642c6528e88224f12409a5f23060994eschrock zfs_cmd_t zc = { 0 };
99653d4ee642c6528e88224f12409a5f23060994eschrock int err;
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
99653d4ee642c6528e88224f12409a5f23060994eschrock return (-1);
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
99653d4ee642c6528e88224f12409a5f23060994eschrock if (errno == ENOMEM) {
99653d4ee642c6528e88224f12409a5f23060994eschrock if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
99653d4ee642c6528e88224f12409a5f23060994eschrock return (-1);
99653d4ee642c6528e88224f12409a5f23060994eschrock }
99653d4ee642c6528e88224f12409a5f23060994eschrock } else {
99653d4ee642c6528e88224f12409a5f23060994eschrock zcmd_free_nvlists(&zc);
99653d4ee642c6528e88224f12409a5f23060994eschrock return (-1);
99653d4ee642c6528e88224f12409a5f23060994eschrock }
99653d4ee642c6528e88224f12409a5f23060994eschrock }
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
99653d4ee642c6528e88224f12409a5f23060994eschrock zcmd_free_nvlists(&zc);
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks if (err != 0)
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks return (-1);
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks nvlist_free(zhp->zfs_recvd_props);
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks zhp->zfs_recvd_props = recvdprops;
b7661ccca92e6bf5160f4d5d2601efaeaa1f5161mmusante
b7661ccca92e6bf5160f4d5d2601efaeaa1f5161mmusante return (0);
b7661ccca92e6bf5160f4d5d2601efaeaa1f5161mmusante}
40ff3960ec49d8bb707c8b7f0030f2ac0c014033ck
40ff3960ec49d8bb707c8b7f0030f2ac0c014033ckstatic int
40ff3960ec49d8bb707c8b7f0030f2ac0c014033ckput_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson{
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson nvlist_t *allprops, *userprops;
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson
99653d4ee642c6528e88224f12409a5f23060994eschrock if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson return (-1);
99653d4ee642c6528e88224f12409a5f23060994eschrock }
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock /*
99653d4ee642c6528e88224f12409a5f23060994eschrock * XXX Why do we store the user props separately, in addition to
99653d4ee642c6528e88224f12409a5f23060994eschrock * storing them in zfs_props?
99653d4ee642c6528e88224f12409a5f23060994eschrock */
fa9e4066f08beec538e775443c5be79dd423fcabahrens if ((userprops = process_user_props(zhp, allprops)) == NULL) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens nvlist_free(allprops);
ece3d9b3bacef51a5f34d993935eedbb7bb87059lling return (-1);
ece3d9b3bacef51a5f34d993935eedbb7bb87059lling }
ece3d9b3bacef51a5f34d993935eedbb7bb87059lling
ece3d9b3bacef51a5f34d993935eedbb7bb87059lling nvlist_free(zhp->zfs_props);
ece3d9b3bacef51a5f34d993935eedbb7bb87059lling nvlist_free(zhp->zfs_user_props);
ece3d9b3bacef51a5f34d993935eedbb7bb87059lling
99653d4ee642c6528e88224f12409a5f23060994eschrock zhp->zfs_props = allprops;
99653d4ee642c6528e88224f12409a5f23060994eschrock zhp->zfs_user_props = userprops;
ece3d9b3bacef51a5f34d993935eedbb7bb87059lling
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
99653d4ee642c6528e88224f12409a5f23060994eschrock}
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrockstatic int
99653d4ee642c6528e88224f12409a5f23060994eschrockget_stats(zfs_handle_t *zhp)
99653d4ee642c6528e88224f12409a5f23060994eschrock{
99653d4ee642c6528e88224f12409a5f23060994eschrock int rc = 0;
99653d4ee642c6528e88224f12409a5f23060994eschrock zfs_cmd_t zc = { 0 };
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
99653d4ee642c6528e88224f12409a5f23060994eschrock return (-1);
99653d4ee642c6528e88224f12409a5f23060994eschrock if (get_stats_ioctl(zhp, &zc) != 0)
99653d4ee642c6528e88224f12409a5f23060994eschrock rc = -1;
99653d4ee642c6528e88224f12409a5f23060994eschrock else if (put_stats_zhdl(zhp, &zc) != 0)
99653d4ee642c6528e88224f12409a5f23060994eschrock rc = -1;
99653d4ee642c6528e88224f12409a5f23060994eschrock zcmd_free_nvlists(&zc);
b1b8ab34de515a5e83206da22c3d7e563241b021lling return (rc);
b1b8ab34de515a5e83206da22c3d7e563241b021lling}
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock/*
99653d4ee642c6528e88224f12409a5f23060994eschrock * Refresh the properties currently stored in the handle.
99653d4ee642c6528e88224f12409a5f23060994eschrock */
99653d4ee642c6528e88224f12409a5f23060994eschrockvoid
99653d4ee642c6528e88224f12409a5f23060994eschrockzfs_refresh_properties(zfs_handle_t *zhp)
99653d4ee642c6528e88224f12409a5f23060994eschrock{
99653d4ee642c6528e88224f12409a5f23060994eschrock (void) get_stats(zhp);
99653d4ee642c6528e88224f12409a5f23060994eschrock}
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock/*
43afaaa8b73f73af765f4fa90f39a0f86cb8a364Eric Schrock * Makes a handle from the given dataset name. Used by zfs_open() and
99653d4ee642c6528e88224f12409a5f23060994eschrock * zfs_iter_* to create child handles on the fly.
99653d4ee642c6528e88224f12409a5f23060994eschrock */
99653d4ee642c6528e88224f12409a5f23060994eschrockstatic int
99653d4ee642c6528e88224f12409a5f23060994eschrockmake_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
99653d4ee642c6528e88224f12409a5f23060994eschrock{
99653d4ee642c6528e88224f12409a5f23060994eschrock if (put_stats_zhdl(zhp, zc) != 0)
99653d4ee642c6528e88224f12409a5f23060994eschrock return (-1);
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock /*
99653d4ee642c6528e88224f12409a5f23060994eschrock * We've managed to open the dataset and gather statistics. Determine
99653d4ee642c6528e88224f12409a5f23060994eschrock * the high-level type.
99653d4ee642c6528e88224f12409a5f23060994eschrock */
b1b8ab34de515a5e83206da22c3d7e563241b021lling if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
b1b8ab34de515a5e83206da22c3d7e563241b021lling zhp->zfs_head_type = ZFS_TYPE_VOLUME;
b1b8ab34de515a5e83206da22c3d7e563241b021lling else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
b1b8ab34de515a5e83206da22c3d7e563241b021lling zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
b1b8ab34de515a5e83206da22c3d7e563241b021lling else
b1b8ab34de515a5e83206da22c3d7e563241b021lling abort();
b1b8ab34de515a5e83206da22c3d7e563241b021lling
b1b8ab34de515a5e83206da22c3d7e563241b021lling if (zhp->zfs_dmustats.dds_is_snapshot)
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks zhp->zfs_type = ZFS_TYPE_VOLUME;
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson else
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson abort(); /* we should never see any other types */
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks return (-1);
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock return (0);
99653d4ee642c6528e88224f12409a5f23060994eschrock}
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrockzfs_handle_t *
99653d4ee642c6528e88224f12409a5f23060994eschrockmake_dataset_handle(libzfs_handle_t *hdl, const char *path)
99653d4ee642c6528e88224f12409a5f23060994eschrock{
fa9e4066f08beec538e775443c5be79dd423fcabahrens zfs_cmd_t zc = { 0 };
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (zhp == NULL)
99653d4ee642c6528e88224f12409a5f23060994eschrock return (NULL);
99653d4ee642c6528e88224f12409a5f23060994eschrock
fa9e4066f08beec538e775443c5be79dd423fcabahrens zhp->zfs_hdl = hdl;
99653d4ee642c6528e88224f12409a5f23060994eschrock (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens free(zhp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (get_stats_ioctl(zhp, &zc) == -1) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens zcmd_free_nvlists(&zc);
99653d4ee642c6528e88224f12409a5f23060994eschrock free(zhp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (make_dataset_handle_common(zhp, &zc) == -1) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens free(zhp);
99653d4ee642c6528e88224f12409a5f23060994eschrock zhp = NULL;
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens zcmd_free_nvlists(&zc);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (zhp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrockzfs_handle_t *
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrockmake_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock{
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock if (zhp == NULL)
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock return (NULL);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock zhp->zfs_hdl = hdl;
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock if (make_dataset_handle_common(zhp, zc) == -1) {
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock free(zhp);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock return (NULL);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock }
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock return (zhp);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrenszfs_handle_t *
fa9e4066f08beec538e775443c5be79dd423fcabahrenszfs_handle_dup(zfs_handle_t *zhp_orig)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
99653d4ee642c6528e88224f12409a5f23060994eschrock zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (zhp == NULL)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
99653d4ee642c6528e88224f12409a5f23060994eschrock zhp->zfs_hdl = zhp_orig->zfs_hdl;
fa9e4066f08beec538e775443c5be79dd423fcabahrens zhp->zpool_hdl = zhp_orig->zpool_hdl;
fa9e4066f08beec538e775443c5be79dd423fcabahrens (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
fa9e4066f08beec538e775443c5be79dd423fcabahrens sizeof (zhp->zfs_name));
fa9e4066f08beec538e775443c5be79dd423fcabahrens zhp->zfs_type = zhp_orig->zfs_type;
fa9e4066f08beec538e775443c5be79dd423fcabahrens zhp->zfs_head_type = zhp_orig->zfs_head_type;
fa9e4066f08beec538e775443c5be79dd423fcabahrens zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (zhp_orig->zfs_props != NULL) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens (void) no_memory(zhp->zfs_hdl);
fa9e4066f08beec538e775443c5be79dd423fcabahrens zfs_close(zhp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (zhp_orig->zfs_user_props != NULL) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (nvlist_dup(zhp_orig->zfs_user_props,
5c7098917783942b65876f681a21342761227dadeschrock &zhp->zfs_user_props, 0) != 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens (void) no_memory(zhp->zfs_hdl);
fa9e4066f08beec538e775443c5be79dd423fcabahrens zfs_close(zhp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
5c7098917783942b65876f681a21342761227dadeschrock if (zhp_orig->zfs_recvd_props != NULL) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (nvlist_dup(zhp_orig->zfs_recvd_props,
5c7098917783942b65876f681a21342761227dadeschrock &zhp->zfs_recvd_props, 0)) {
5c7098917783942b65876f681a21342761227dadeschrock (void) no_memory(zhp->zfs_hdl);
5c7098917783942b65876f681a21342761227dadeschrock zfs_close(zhp);
5c7098917783942b65876f681a21342761227dadeschrock return (NULL);
5c7098917783942b65876f681a21342761227dadeschrock }
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
5c7098917783942b65876f681a21342761227dadeschrock zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
5c7098917783942b65876f681a21342761227dadeschrock if (zhp_orig->zfs_mntopts != NULL) {
5c7098917783942b65876f681a21342761227dadeschrock zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
5c7098917783942b65876f681a21342761227dadeschrock zhp_orig->zfs_mntopts);
5c7098917783942b65876f681a21342761227dadeschrock }
5c7098917783942b65876f681a21342761227dadeschrock zhp->zfs_props_table = zhp_orig->zfs_props_table;
5c7098917783942b65876f681a21342761227dadeschrock return (zhp);
5c7098917783942b65876f681a21342761227dadeschrock}
5c7098917783942b65876f681a21342761227dadeschrock
5c7098917783942b65876f681a21342761227dadeschrockboolean_t
5c7098917783942b65876f681a21342761227dadeschrockzfs_bookmark_exists(const char *path)
5c7098917783942b65876f681a21342761227dadeschrock{
3d7072f8bd27709dba14f6fe336f149d25d9e207eschrock nvlist_t *bmarks;
3d7072f8bd27709dba14f6fe336f149d25d9e207eschrock nvlist_t *props;
5c7098917783942b65876f681a21342761227dadeschrock char fsname[ZFS_MAXNAMELEN];
5c7098917783942b65876f681a21342761227dadeschrock char *bmark_name;
5c7098917783942b65876f681a21342761227dadeschrock char *pound;
fa9e4066f08beec538e775443c5be79dd423fcabahrens int err;
99653d4ee642c6528e88224f12409a5f23060994eschrock boolean_t rv;
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock (void) strlcpy(fsname, path, sizeof (fsname));
99653d4ee642c6528e88224f12409a5f23060994eschrock pound = strchr(fsname, '#');
99653d4ee642c6528e88224f12409a5f23060994eschrock if (pound == NULL)
99653d4ee642c6528e88224f12409a5f23060994eschrock return (B_FALSE);
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock *pound = '\0';
99653d4ee642c6528e88224f12409a5f23060994eschrock bmark_name = pound + 1;
99653d4ee642c6528e88224f12409a5f23060994eschrock props = fnvlist_alloc();
99653d4ee642c6528e88224f12409a5f23060994eschrock err = lzc_get_bookmarks(fsname, props, &bmarks);
99653d4ee642c6528e88224f12409a5f23060994eschrock nvlist_free(props);
99653d4ee642c6528e88224f12409a5f23060994eschrock if (err != 0) {
99653d4ee642c6528e88224f12409a5f23060994eschrock nvlist_free(bmarks);
99653d4ee642c6528e88224f12409a5f23060994eschrock return (B_FALSE);
c08432ebe688ed6410b302771a9afd8e23e2a7cceschrock }
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock rv = nvlist_exists(bmarks, bmark_name);
99653d4ee642c6528e88224f12409a5f23060994eschrock nvlist_free(bmarks);
99653d4ee642c6528e88224f12409a5f23060994eschrock return (rv);
99653d4ee642c6528e88224f12409a5f23060994eschrock}
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrockzfs_handle_t *
99653d4ee642c6528e88224f12409a5f23060994eschrockmake_bookmark_handle(zfs_handle_t *parent, const char *path,
99653d4ee642c6528e88224f12409a5f23060994eschrock nvlist_t *bmark_props)
99653d4ee642c6528e88224f12409a5f23060994eschrock{
99653d4ee642c6528e88224f12409a5f23060994eschrock zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
99653d4ee642c6528e88224f12409a5f23060994eschrock
91ebeef555ce7f899b6270a3c2df47b51f7ad59aahrens if (zhp == NULL)
990b4856d0eaada6f8140335733a1b1771ed2746lling return (NULL);
b2634b9c57bbcfa01bb5dec2e196aec32957925fEric Taylor
91ebeef555ce7f899b6270a3c2df47b51f7ad59aahrens /* Fill in the name. */
99653d4ee642c6528e88224f12409a5f23060994eschrock zhp->zfs_hdl = parent->zfs_hdl;
99653d4ee642c6528e88224f12409a5f23060994eschrock (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock /* Set the property lists. */
99653d4ee642c6528e88224f12409a5f23060994eschrock if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
99653d4ee642c6528e88224f12409a5f23060994eschrock free(zhp);
99653d4ee642c6528e88224f12409a5f23060994eschrock return (NULL);
99653d4ee642c6528e88224f12409a5f23060994eschrock }
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock /* Set the types. */
99653d4ee642c6528e88224f12409a5f23060994eschrock zhp->zfs_head_type = parent->zfs_head_type;
6733190958bbcc0bd6d1d601e7ae0a6994dafb45dougm zhp->zfs_type = ZFS_TYPE_BOOKMARK;
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
29ab75c9a733dad2978c4860efd954b5625e3467rm nvlist_free(zhp->zfs_props);
069f55e237020c4a4907b235fc38fafc6442ce94Eric Schrock free(zhp);
99653d4ee642c6528e88224f12409a5f23060994eschrock return (NULL);
b2634b9c57bbcfa01bb5dec2e196aec32957925fEric Taylor }
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock return (zhp);
99653d4ee642c6528e88224f12409a5f23060994eschrock}
99653d4ee642c6528e88224f12409a5f23060994eschrock
99653d4ee642c6528e88224f12409a5f23060994eschrock/*
99653d4ee642c6528e88224f12409a5f23060994eschrock * Opens the given snapshot, filesystem, or volume. The 'types'
99653d4ee642c6528e88224f12409a5f23060994eschrock * argument is a mask of acceptable types. The function will print an
99653d4ee642c6528e88224f12409a5f23060994eschrock * appropriate error message and return NULL if it can't be opened.
99653d4ee642c6528e88224f12409a5f23060994eschrock */
99653d4ee642c6528e88224f12409a5f23060994eschrockzfs_handle_t *
99653d4ee642c6528e88224f12409a5f23060994eschrockzfs_open(libzfs_handle_t *hdl, const char *path, int types)
99653d4ee642c6528e88224f12409a5f23060994eschrock{
99653d4ee642c6528e88224f12409a5f23060994eschrock zfs_handle_t *zhp;
99653d4ee642c6528e88224f12409a5f23060994eschrock char errbuf[1024];
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock
d5b5bb256c576fe5ef26e0795bd40abe77f93246Rich Morris (void) snprintf(errbuf, sizeof (errbuf),
d5b5bb256c576fe5ef26e0795bd40abe77f93246Rich Morris dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
d5b5bb256c576fe5ef26e0795bd40abe77f93246Rich Morris
d5b5bb256c576fe5ef26e0795bd40abe77f93246Rich Morris /*
d5b5bb256c576fe5ef26e0795bd40abe77f93246Rich Morris * Validate the name before we even try to open it.
d5b5bb256c576fe5ef26e0795bd40abe77f93246Rich Morris */
5aba80db367b061758a29154d304977d00d8e4f4ck if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
5aba80db367b061758a29154d304977d00d8e4f4ck zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5aba80db367b061758a29154d304977d00d8e4f4ck "invalid dataset name"));
5aba80db367b061758a29154d304977d00d8e4f4ck (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
5aba80db367b061758a29154d304977d00d8e4f4ck return (NULL);
5aba80db367b061758a29154d304977d00d8e4f4ck }
5aba80db367b061758a29154d304977d00d8e4f4ck
5aba80db367b061758a29154d304977d00d8e4f4ck /*
5aba80db367b061758a29154d304977d00d8e4f4ck * Try to get stats for the dataset, which will tell us if it exists.
5aba80db367b061758a29154d304977d00d8e4f4ck */
5aba80db367b061758a29154d304977d00d8e4f4ck errno = 0;
5aba80db367b061758a29154d304977d00d8e4f4ck if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
5aba80db367b061758a29154d304977d00d8e4f4ck (void) zfs_standard_error(hdl, errno, errbuf);
5aba80db367b061758a29154d304977d00d8e4f4ck return (NULL);
5aba80db367b061758a29154d304977d00d8e4f4ck }
5aba80db367b061758a29154d304977d00d8e4f4ck
5aba80db367b061758a29154d304977d00d8e4f4ck if (!(types & zhp->zfs_type)) {
5aba80db367b061758a29154d304977d00d8e4f4ck (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5aba80db367b061758a29154d304977d00d8e4f4ck zfs_close(zhp);
5aba80db367b061758a29154d304977d00d8e4f4ck return (NULL);
5aba80db367b061758a29154d304977d00d8e4f4ck }
5aba80db367b061758a29154d304977d00d8e4f4ck
5aba80db367b061758a29154d304977d00d8e4f4ck return (zhp);
5aba80db367b061758a29154d304977d00d8e4f4ck}
5aba80db367b061758a29154d304977d00d8e4f4ck
5aba80db367b061758a29154d304977d00d8e4f4ck/*
5aba80db367b061758a29154d304977d00d8e4f4ck * Release a ZFS handle. Nothing to do but free the associated memory.
5aba80db367b061758a29154d304977d00d8e4f4ck */
5aba80db367b061758a29154d304977d00d8e4f4ckvoid
5aba80db367b061758a29154d304977d00d8e4f4ckzfs_close(zfs_handle_t *zhp)
5aba80db367b061758a29154d304977d00d8e4f4ck{
5aba80db367b061758a29154d304977d00d8e4f4ck if (zhp->zfs_mntopts)
5aba80db367b061758a29154d304977d00d8e4f4ck free(zhp->zfs_mntopts);
5aba80db367b061758a29154d304977d00d8e4f4ck nvlist_free(zhp->zfs_props);
5aba80db367b061758a29154d304977d00d8e4f4ck nvlist_free(zhp->zfs_user_props);
5aba80db367b061758a29154d304977d00d8e4f4ck nvlist_free(zhp->zfs_recvd_props);
5aba80db367b061758a29154d304977d00d8e4f4ck free(zhp);
5aba80db367b061758a29154d304977d00d8e4f4ck}
5aba80db367b061758a29154d304977d00d8e4f4ck
5aba80db367b061758a29154d304977d00d8e4f4cktypedef struct mnttab_node {
5aba80db367b061758a29154d304977d00d8e4f4ck struct mnttab mtn_mt;
5aba80db367b061758a29154d304977d00d8e4f4ck avl_node_t mtn_node;
5aba80db367b061758a29154d304977d00d8e4f4ck} mnttab_node_t;
5aba80db367b061758a29154d304977d00d8e4f4ck
5aba80db367b061758a29154d304977d00d8e4f4ckstatic int
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrocklibzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock{
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock const mnttab_node_t *mtn1 = arg1;
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock const mnttab_node_t *mtn2 = arg2;
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock int rv;
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock
9966ca11f4a1481acce85f690fa59e4084050627Matthew Ahrens if (rv == 0)
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock return (0);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock return (rv > 0 ? 1 : -1);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock}
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrockvoid
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrocklibzfs_mnttab_init(libzfs_handle_t *hdl)
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock{
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock}
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrockvoid
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrocklibzfs_mnttab_update(libzfs_handle_t *hdl)
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock{
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock struct mnttab entry;
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock rewind(hdl->libzfs_mnttab);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock mnttab_node_t *mtn;
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock continue;
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock avl_add(&hdl->libzfs_mnttab_cache, mtn);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock }
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock}
990b4856d0eaada6f8140335733a1b1771ed2746lling
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrockvoid
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrocklibzfs_mnttab_fini(libzfs_handle_t *hdl)
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock{
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock void *cookie = NULL;
990b4856d0eaada6f8140335733a1b1771ed2746lling mnttab_node_t *mtn;
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock free(mtn->mtn_mt.mnt_special);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock free(mtn->mtn_mt.mnt_mountp);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock free(mtn->mtn_mt.mnt_fstype);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock free(mtn->mtn_mt.mnt_mntopts);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock free(mtn);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock }
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock avl_destroy(&hdl->libzfs_mnttab_cache);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock}
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrockvoid
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrocklibzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
990b4856d0eaada6f8140335733a1b1771ed2746lling{
990b4856d0eaada6f8140335733a1b1771ed2746lling hdl->libzfs_mnttab_enable = enable;
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock}
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrockint
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrocklibzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
990b4856d0eaada6f8140335733a1b1771ed2746lling struct mnttab *entry)
990b4856d0eaada6f8140335733a1b1771ed2746lling{
990b4856d0eaada6f8140335733a1b1771ed2746lling mnttab_node_t find;
990b4856d0eaada6f8140335733a1b1771ed2746lling mnttab_node_t *mtn;
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling if (!hdl->libzfs_mnttab_enable) {
990b4856d0eaada6f8140335733a1b1771ed2746lling struct mnttab srch = { 0 };
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling if (avl_numnodes(&hdl->libzfs_mnttab_cache))
990b4856d0eaada6f8140335733a1b1771ed2746lling libzfs_mnttab_fini(hdl);
990b4856d0eaada6f8140335733a1b1771ed2746lling rewind(hdl->libzfs_mnttab);
990b4856d0eaada6f8140335733a1b1771ed2746lling srch.mnt_special = (char *)fsname;
990b4856d0eaada6f8140335733a1b1771ed2746lling srch.mnt_fstype = MNTTYPE_ZFS;
990b4856d0eaada6f8140335733a1b1771ed2746lling if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock return (0);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock else
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock return (ENOENT);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock }
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock libzfs_mnttab_update(hdl);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock find.mtn_mt.mnt_special = (char *)fsname;
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock if (mtn) {
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock *entry = mtn->mtn_mt;
b1b8ab34de515a5e83206da22c3d7e563241b021lling return (0);
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling return (ENOENT);
990b4856d0eaada6f8140335733a1b1771ed2746lling}
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746llingvoid
990b4856d0eaada6f8140335733a1b1771ed2746llinglibzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
990b4856d0eaada6f8140335733a1b1771ed2746lling const char *mountp, const char *mntopts)
990b4856d0eaada6f8140335733a1b1771ed2746lling{
990b4856d0eaada6f8140335733a1b1771ed2746lling mnttab_node_t *mtn;
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
990b4856d0eaada6f8140335733a1b1771ed2746lling return;
990b4856d0eaada6f8140335733a1b1771ed2746lling mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
990b4856d0eaada6f8140335733a1b1771ed2746lling mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
990b4856d0eaada6f8140335733a1b1771ed2746lling mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
990b4856d0eaada6f8140335733a1b1771ed2746lling mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
990b4856d0eaada6f8140335733a1b1771ed2746lling mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
990b4856d0eaada6f8140335733a1b1771ed2746lling avl_add(&hdl->libzfs_mnttab_cache, mtn);
990b4856d0eaada6f8140335733a1b1771ed2746lling}
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746llingvoid
990b4856d0eaada6f8140335733a1b1771ed2746llinglibzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
b1b8ab34de515a5e83206da22c3d7e563241b021lling{
990b4856d0eaada6f8140335733a1b1771ed2746lling mnttab_node_t find;
b1b8ab34de515a5e83206da22c3d7e563241b021lling mnttab_node_t *ret;
990b4856d0eaada6f8140335733a1b1771ed2746lling
b1b8ab34de515a5e83206da22c3d7e563241b021lling find.mtn_mt.mnt_special = (char *)fsname;
b1b8ab34de515a5e83206da22c3d7e563241b021lling if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
b1b8ab34de515a5e83206da22c3d7e563241b021lling avl_remove(&hdl->libzfs_mnttab_cache, ret);
b1b8ab34de515a5e83206da22c3d7e563241b021lling free(ret->mtn_mt.mnt_special);
b1b8ab34de515a5e83206da22c3d7e563241b021lling free(ret->mtn_mt.mnt_mountp);
b1b8ab34de515a5e83206da22c3d7e563241b021lling free(ret->mtn_mt.mnt_fstype);
b1b8ab34de515a5e83206da22c3d7e563241b021lling free(ret->mtn_mt.mnt_mntopts);
b1b8ab34de515a5e83206da22c3d7e563241b021lling free(ret);
b1b8ab34de515a5e83206da22c3d7e563241b021lling }
b1b8ab34de515a5e83206da22c3d7e563241b021lling}
b1b8ab34de515a5e83206da22c3d7e563241b021lling
b1b8ab34de515a5e83206da22c3d7e563241b021llingint
b1b8ab34de515a5e83206da22c3d7e563241b021llingzfs_spa_version(zfs_handle_t *zhp, int *spa_version)
b1b8ab34de515a5e83206da22c3d7e563241b021lling{
b1b8ab34de515a5e83206da22c3d7e563241b021lling zpool_handle_t *zpool_handle = zhp->zpool_hdl;
b1b8ab34de515a5e83206da22c3d7e563241b021lling
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson if (zpool_handle == NULL)
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson return (-1);
b1b8ab34de515a5e83206da22c3d7e563241b021lling
b1b8ab34de515a5e83206da22c3d7e563241b021lling *spa_version = zpool_get_prop_int(zpool_handle,
b1b8ab34de515a5e83206da22c3d7e563241b021lling ZPOOL_PROP_VERSION, NULL);
deb8317b8f5925e3f6dd7cb6ed0cdd035f546a5aMark J Musante return (0);
deb8317b8f5925e3f6dd7cb6ed0cdd035f546a5aMark J Musante}
deb8317b8f5925e3f6dd7cb6ed0cdd035f546a5aMark J Musante
deb8317b8f5925e3f6dd7cb6ed0cdd035f546a5aMark J Musante/*
b1b8ab34de515a5e83206da22c3d7e563241b021lling * The choice of reservation property depends on the SPA version.
b1b8ab34de515a5e83206da22c3d7e563241b021lling */
b1b8ab34de515a5e83206da22c3d7e563241b021llingstatic int
b1b8ab34de515a5e83206da22c3d7e563241b021llingzfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
b1b8ab34de515a5e83206da22c3d7e563241b021lling{
b1b8ab34de515a5e83206da22c3d7e563241b021lling int spa_version;
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson
b1b8ab34de515a5e83206da22c3d7e563241b021lling if (zfs_spa_version(zhp, &spa_version) < 0)
b1b8ab34de515a5e83206da22c3d7e563241b021lling return (-1);
b1b8ab34de515a5e83206da22c3d7e563241b021lling
b1b8ab34de515a5e83206da22c3d7e563241b021lling if (spa_version >= SPA_VERSION_REFRESERVATION)
b1b8ab34de515a5e83206da22c3d7e563241b021lling *resv_prop = ZFS_PROP_REFRESERVATION;
990b4856d0eaada6f8140335733a1b1771ed2746lling else
990b4856d0eaada6f8140335733a1b1771ed2746lling *resv_prop = ZFS_PROP_RESERVATION;
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling return (0);
990b4856d0eaada6f8140335733a1b1771ed2746lling}
990b4856d0eaada6f8140335733a1b1771ed2746lling
b1b8ab34de515a5e83206da22c3d7e563241b021lling/*
b1b8ab34de515a5e83206da22c3d7e563241b021lling * Given an nvlist of properties to set, validates that they are correct, and
b1b8ab34de515a5e83206da22c3d7e563241b021lling * parses any numeric properties (index, boolean, etc) if they are specified as
b1b8ab34de515a5e83206da22c3d7e563241b021lling * strings.
b1b8ab34de515a5e83206da22c3d7e563241b021lling */
b1b8ab34de515a5e83206da22c3d7e563241b021llingnvlist_t *
b1b8ab34de515a5e83206da22c3d7e563241b021llingzfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
b1b8ab34de515a5e83206da22c3d7e563241b021lling uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
b1b8ab34de515a5e83206da22c3d7e563241b021lling{
deb8317b8f5925e3f6dd7cb6ed0cdd035f546a5aMark J Musante nvpair_t *elem;
deb8317b8f5925e3f6dd7cb6ed0cdd035f546a5aMark J Musante uint64_t intval;
deb8317b8f5925e3f6dd7cb6ed0cdd035f546a5aMark J Musante char *strval;
deb8317b8f5925e3f6dd7cb6ed0cdd035f546a5aMark J Musante zfs_prop_t prop;
deb8317b8f5925e3f6dd7cb6ed0cdd035f546a5aMark J Musante nvlist_t *ret;
b1b8ab34de515a5e83206da22c3d7e563241b021lling int chosen_normal = -1;
deb8317b8f5925e3f6dd7cb6ed0cdd035f546a5aMark J Musante int chosen_utf = -1;
b1b8ab34de515a5e83206da22c3d7e563241b021lling
b1b8ab34de515a5e83206da22c3d7e563241b021lling if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
b1b8ab34de515a5e83206da22c3d7e563241b021lling (void) no_memory(hdl);
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson return (NULL);
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson }
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson /*
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson * Make sure this property is valid and applies to this type.
b1b8ab34de515a5e83206da22c3d7e563241b021lling */
b1b8ab34de515a5e83206da22c3d7e563241b021lling
b1b8ab34de515a5e83206da22c3d7e563241b021lling elem = NULL;
990b4856d0eaada6f8140335733a1b1771ed2746lling while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
990b4856d0eaada6f8140335733a1b1771ed2746lling const char *propname = nvpair_name(elem);
b1b8ab34de515a5e83206da22c3d7e563241b021lling
b1b8ab34de515a5e83206da22c3d7e563241b021lling prop = zfs_name_to_prop(propname);
b1b8ab34de515a5e83206da22c3d7e563241b021lling if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
b1b8ab34de515a5e83206da22c3d7e563241b021lling /*
b1b8ab34de515a5e83206da22c3d7e563241b021lling * This is a user property: make sure it's a
b1b8ab34de515a5e83206da22c3d7e563241b021lling * string, and that it's less than ZAP_MAXNAMELEN.
b1b8ab34de515a5e83206da22c3d7e563241b021lling */
b1b8ab34de515a5e83206da22c3d7e563241b021lling if (nvpair_type(elem) != DATA_TYPE_STRING) {
b1b8ab34de515a5e83206da22c3d7e563241b021lling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
b1b8ab34de515a5e83206da22c3d7e563241b021lling "'%s' must be a string"), propname);
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
b1b8ab34de515a5e83206da22c3d7e563241b021lling goto error;
b1b8ab34de515a5e83206da22c3d7e563241b021lling }
b1b8ab34de515a5e83206da22c3d7e563241b021lling
b1b8ab34de515a5e83206da22c3d7e563241b021lling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
b1b8ab34de515a5e83206da22c3d7e563241b021lling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
b1b8ab34de515a5e83206da22c3d7e563241b021lling "property name '%s' is too long"),
b1b8ab34de515a5e83206da22c3d7e563241b021lling propname);
b1b8ab34de515a5e83206da22c3d7e563241b021lling (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
b1b8ab34de515a5e83206da22c3d7e563241b021lling goto error;
b1b8ab34de515a5e83206da22c3d7e563241b021lling }
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson (void) nvpair_value_string(elem, &strval);
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson if (nvlist_add_string(ret, propname, strval) != 0) {
b1b8ab34de515a5e83206da22c3d7e563241b021lling (void) no_memory(hdl);
b1b8ab34de515a5e83206da22c3d7e563241b021lling goto error;
b1b8ab34de515a5e83206da22c3d7e563241b021lling }
b1b8ab34de515a5e83206da22c3d7e563241b021lling continue;
b1b8ab34de515a5e83206da22c3d7e563241b021lling }
b1b8ab34de515a5e83206da22c3d7e563241b021lling
b1b8ab34de515a5e83206da22c3d7e563241b021lling /*
b1b8ab34de515a5e83206da22c3d7e563241b021lling * Currently, only user properties can be modified on
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson * snapshots.
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson */
b1b8ab34de515a5e83206da22c3d7e563241b021lling if (type == ZFS_TYPE_SNAPSHOT) {
b1b8ab34de515a5e83206da22c3d7e563241b021lling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
b1b8ab34de515a5e83206da22c3d7e563241b021lling "this property can not be modified for snapshots"));
b1b8ab34de515a5e83206da22c3d7e563241b021lling (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
b1b8ab34de515a5e83206da22c3d7e563241b021lling goto error;
b1b8ab34de515a5e83206da22c3d7e563241b021lling }
b1b8ab34de515a5e83206da22c3d7e563241b021lling
b1b8ab34de515a5e83206da22c3d7e563241b021lling if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
b1b8ab34de515a5e83206da22c3d7e563241b021lling zfs_userquota_prop_t uqtype;
b1b8ab34de515a5e83206da22c3d7e563241b021lling char newpropname[128];
b1b8ab34de515a5e83206da22c3d7e563241b021lling char domain[128];
b1b8ab34de515a5e83206da22c3d7e563241b021lling uint64_t rid;
b1b8ab34de515a5e83206da22c3d7e563241b021lling uint64_t valary[3];
b1b8ab34de515a5e83206da22c3d7e563241b021lling
b1b8ab34de515a5e83206da22c3d7e563241b021lling if (userquota_propname_decode(propname, zoned,
990b4856d0eaada6f8140335733a1b1771ed2746lling &uqtype, domain, sizeof (domain), &rid) != 0) {
990b4856d0eaada6f8140335733a1b1771ed2746lling zfs_error_aux(hdl,
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson dgettext(TEXT_DOMAIN,
b1b8ab34de515a5e83206da22c3d7e563241b021lling "'%s' has an invalid user/group name"),
b1b8ab34de515a5e83206da22c3d7e563241b021lling propname);
b1b8ab34de515a5e83206da22c3d7e563241b021lling (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
b1b8ab34de515a5e83206da22c3d7e563241b021lling goto error;
b1b8ab34de515a5e83206da22c3d7e563241b021lling }
b1b8ab34de515a5e83206da22c3d7e563241b021lling
b1b8ab34de515a5e83206da22c3d7e563241b021lling if (uqtype != ZFS_PROP_USERQUOTA &&
b1b8ab34de515a5e83206da22c3d7e563241b021lling uqtype != ZFS_PROP_GROUPQUOTA) {
b1b8ab34de515a5e83206da22c3d7e563241b021lling zfs_error_aux(hdl,
b1b8ab34de515a5e83206da22c3d7e563241b021lling dgettext(TEXT_DOMAIN, "'%s' is readonly"),
b1b8ab34de515a5e83206da22c3d7e563241b021lling propname);
b1b8ab34de515a5e83206da22c3d7e563241b021lling (void) zfs_error(hdl, EZFS_PROPREADONLY,
990b4856d0eaada6f8140335733a1b1771ed2746lling errbuf);
b1b8ab34de515a5e83206da22c3d7e563241b021lling goto error;
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson }
b1b8ab34de515a5e83206da22c3d7e563241b021lling
b1b8ab34de515a5e83206da22c3d7e563241b021lling if (nvpair_type(elem) == DATA_TYPE_STRING) {
b1b8ab34de515a5e83206da22c3d7e563241b021lling (void) nvpair_value_string(elem, &strval);
b1b8ab34de515a5e83206da22c3d7e563241b021lling if (strcmp(strval, "none") == 0) {
b1b8ab34de515a5e83206da22c3d7e563241b021lling intval = 0;
b1b8ab34de515a5e83206da22c3d7e563241b021lling } else if (zfs_nicestrtonum(hdl,
b1b8ab34de515a5e83206da22c3d7e563241b021lling strval, &intval) != 0) {
b1b8ab34de515a5e83206da22c3d7e563241b021lling (void) zfs_error(hdl,
b1b8ab34de515a5e83206da22c3d7e563241b021lling EZFS_BADPROP, errbuf);
b1b8ab34de515a5e83206da22c3d7e563241b021lling goto error;
b1b8ab34de515a5e83206da22c3d7e563241b021lling }
b1b8ab34de515a5e83206da22c3d7e563241b021lling } else if (nvpair_type(elem) ==
b1b8ab34de515a5e83206da22c3d7e563241b021lling DATA_TYPE_UINT64) {
b1b8ab34de515a5e83206da22c3d7e563241b021lling (void) nvpair_value_uint64(elem, &intval);
b1b8ab34de515a5e83206da22c3d7e563241b021lling if (intval == 0) {
990b4856d0eaada6f8140335733a1b1771ed2746lling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
b1b8ab34de515a5e83206da22c3d7e563241b021lling "use 'none' to disable "
b1b8ab34de515a5e83206da22c3d7e563241b021lling "userquota/groupquota"));
b1b8ab34de515a5e83206da22c3d7e563241b021lling goto error;
990b4856d0eaada6f8140335733a1b1771ed2746lling }
b1b8ab34de515a5e83206da22c3d7e563241b021lling } else {
b1b8ab34de515a5e83206da22c3d7e563241b021lling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
b1b8ab34de515a5e83206da22c3d7e563241b021lling "'%s' must be a number"), propname);
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
b1b8ab34de515a5e83206da22c3d7e563241b021lling goto error;
b1b8ab34de515a5e83206da22c3d7e563241b021lling }
b1b8ab34de515a5e83206da22c3d7e563241b021lling
990b4856d0eaada6f8140335733a1b1771ed2746lling /*
b1b8ab34de515a5e83206da22c3d7e563241b021lling * Encode the prop name as
b1b8ab34de515a5e83206da22c3d7e563241b021lling * userquota@<hex-rid>-domain, to make it easy
b1b8ab34de515a5e83206da22c3d7e563241b021lling * for the kernel to decode.
990b4856d0eaada6f8140335733a1b1771ed2746lling */
b1b8ab34de515a5e83206da22c3d7e563241b021lling (void) snprintf(newpropname, sizeof (newpropname),
b1b8ab34de515a5e83206da22c3d7e563241b021lling "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
b1b8ab34de515a5e83206da22c3d7e563241b021lling (longlong_t)rid, domain);
b1b8ab34de515a5e83206da22c3d7e563241b021lling valary[0] = uqtype;
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson valary[1] = rid;
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson valary[2] = intval;
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson if (nvlist_add_uint64_array(ret, newpropname,
b1b8ab34de515a5e83206da22c3d7e563241b021lling valary, 3) != 0) {
b1b8ab34de515a5e83206da22c3d7e563241b021lling (void) no_memory(hdl);
b1b8ab34de515a5e83206da22c3d7e563241b021lling goto error;
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson }
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson continue;
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
b1b8ab34de515a5e83206da22c3d7e563241b021lling "'%s' is readonly"),
b1b8ab34de515a5e83206da22c3d7e563241b021lling propname);
b1b8ab34de515a5e83206da22c3d7e563241b021lling (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
b1b8ab34de515a5e83206da22c3d7e563241b021lling goto error;
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson }
b1b8ab34de515a5e83206da22c3d7e563241b021lling
b1b8ab34de515a5e83206da22c3d7e563241b021lling if (prop == ZPROP_INVAL) {
b1b8ab34de515a5e83206da22c3d7e563241b021lling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
b1b8ab34de515a5e83206da22c3d7e563241b021lling "invalid property '%s'"), propname);
b1b8ab34de515a5e83206da22c3d7e563241b021lling (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
b1b8ab34de515a5e83206da22c3d7e563241b021lling goto error;
b1b8ab34de515a5e83206da22c3d7e563241b021lling }
b1b8ab34de515a5e83206da22c3d7e563241b021lling
b1b8ab34de515a5e83206da22c3d7e563241b021lling if (!zfs_prop_valid_for_type(prop, type)) {
b1b8ab34de515a5e83206da22c3d7e563241b021lling zfs_error_aux(hdl,
b1b8ab34de515a5e83206da22c3d7e563241b021lling dgettext(TEXT_DOMAIN, "'%s' does not "
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks "apply to datasets of this type"), propname);
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
990b4856d0eaada6f8140335733a1b1771ed2746lling goto error;
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling if (zfs_prop_readonly(prop) &&
990b4856d0eaada6f8140335733a1b1771ed2746lling (!zfs_prop_setonce(prop) || zhp != NULL)) {
990b4856d0eaada6f8140335733a1b1771ed2746lling zfs_error_aux(hdl,
990b4856d0eaada6f8140335733a1b1771ed2746lling dgettext(TEXT_DOMAIN, "'%s' is readonly"),
990b4856d0eaada6f8140335733a1b1771ed2746lling propname);
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
990b4856d0eaada6f8140335733a1b1771ed2746lling goto error;
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling if (zprop_parse_value(hdl, elem, prop, type, ret,
990b4856d0eaada6f8140335733a1b1771ed2746lling &strval, &intval, errbuf) != 0)
990b4856d0eaada6f8140335733a1b1771ed2746lling goto error;
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling /*
990b4856d0eaada6f8140335733a1b1771ed2746lling * Perform some additional checks for specific properties.
990b4856d0eaada6f8140335733a1b1771ed2746lling */
990b4856d0eaada6f8140335733a1b1771ed2746lling switch (prop) {
990b4856d0eaada6f8140335733a1b1771ed2746lling case ZFS_PROP_VERSION:
990b4856d0eaada6f8140335733a1b1771ed2746lling {
990b4856d0eaada6f8140335733a1b1771ed2746lling int version;
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling if (zhp == NULL)
990b4856d0eaada6f8140335733a1b1771ed2746lling break;
990b4856d0eaada6f8140335733a1b1771ed2746lling version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
990b4856d0eaada6f8140335733a1b1771ed2746lling if (intval < version) {
990b4856d0eaada6f8140335733a1b1771ed2746lling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
990b4856d0eaada6f8140335733a1b1771ed2746lling "Can not downgrade; already at version %u"),
990b4856d0eaada6f8140335733a1b1771ed2746lling version);
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
990b4856d0eaada6f8140335733a1b1771ed2746lling goto error;
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling break;
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling case ZFS_PROP_VOLBLOCKSIZE:
990b4856d0eaada6f8140335733a1b1771ed2746lling case ZFS_PROP_RECORDSIZE:
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks {
990b4856d0eaada6f8140335733a1b1771ed2746lling int maxbs = SPA_MAXBLOCKSIZE;
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks if (zhp != NULL) {
990b4856d0eaada6f8140335733a1b1771ed2746lling maxbs = zpool_get_prop_int(zhp->zpool_hdl,
990b4856d0eaada6f8140335733a1b1771ed2746lling ZPOOL_PROP_MAXBLOCKSIZE, NULL);
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks }
990b4856d0eaada6f8140335733a1b1771ed2746lling /*
990b4856d0eaada6f8140335733a1b1771ed2746lling * Volumes are limited to a volblocksize of 128KB,
990b4856d0eaada6f8140335733a1b1771ed2746lling * because they typically service workloads with
990b4856d0eaada6f8140335733a1b1771ed2746lling * small random writes, which incur a large performance
990b4856d0eaada6f8140335733a1b1771ed2746lling * penalty with large blocks.
990b4856d0eaada6f8140335733a1b1771ed2746lling */
990b4856d0eaada6f8140335733a1b1771ed2746lling if (prop == ZFS_PROP_VOLBLOCKSIZE)
990b4856d0eaada6f8140335733a1b1771ed2746lling maxbs = SPA_OLD_MAXBLOCKSIZE;
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks /*
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks * The value must be a power of two between
069f55e237020c4a4907b235fc38fafc6442ce94Eric Schrock * SPA_MINBLOCKSIZE and maxbs.
990b4856d0eaada6f8140335733a1b1771ed2746lling */
f67f35c39aa272d43489ee49625b4965cc83add2Eric Schrock if (intval < SPA_MINBLOCKSIZE ||
990b4856d0eaada6f8140335733a1b1771ed2746lling intval > maxbs || !ISP2(intval)) {
990b4856d0eaada6f8140335733a1b1771ed2746lling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
990b4856d0eaada6f8140335733a1b1771ed2746lling "'%s' must be power of 2 from 512B "
990b4856d0eaada6f8140335733a1b1771ed2746lling "to %uKB"), propname, maxbs >> 10);
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
990b4856d0eaada6f8140335733a1b1771ed2746lling goto error;
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling break;
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling case ZFS_PROP_MLSLABEL:
990b4856d0eaada6f8140335733a1b1771ed2746lling {
990b4856d0eaada6f8140335733a1b1771ed2746lling /*
990b4856d0eaada6f8140335733a1b1771ed2746lling * Verify the mlslabel string and convert to
990b4856d0eaada6f8140335733a1b1771ed2746lling * internal hex label string.
990b4856d0eaada6f8140335733a1b1771ed2746lling */
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling m_label_t *new_sl;
990b4856d0eaada6f8140335733a1b1771ed2746lling char *hex = NULL; /* internal label string */
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling /* Default value is already OK. */
990b4856d0eaada6f8140335733a1b1771ed2746lling if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
990b4856d0eaada6f8140335733a1b1771ed2746lling break;
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling /* Verify the label can be converted to binary form */
990b4856d0eaada6f8140335733a1b1771ed2746lling if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
990b4856d0eaada6f8140335733a1b1771ed2746lling (str_to_label(strval, &new_sl, MAC_LABEL,
990b4856d0eaada6f8140335733a1b1771ed2746lling L_NO_CORRECTION, NULL) == -1)) {
990b4856d0eaada6f8140335733a1b1771ed2746lling goto badlabel;
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling /* Now translate to hex internal label string */
990b4856d0eaada6f8140335733a1b1771ed2746lling if (label_to_str(new_sl, &hex, M_INTERNAL,
990b4856d0eaada6f8140335733a1b1771ed2746lling DEF_NAMES) != 0) {
990b4856d0eaada6f8140335733a1b1771ed2746lling if (hex)
990b4856d0eaada6f8140335733a1b1771ed2746lling free(hex);
990b4856d0eaada6f8140335733a1b1771ed2746lling goto badlabel;
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling m_label_free(new_sl);
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling /* If string is already in internal form, we're done. */
990b4856d0eaada6f8140335733a1b1771ed2746lling if (strcmp(strval, hex) == 0) {
990b4856d0eaada6f8140335733a1b1771ed2746lling free(hex);
990b4856d0eaada6f8140335733a1b1771ed2746lling break;
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling /* Replace the label string with the internal form. */
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) nvlist_remove(ret, zfs_prop_to_name(prop),
990b4856d0eaada6f8140335733a1b1771ed2746lling DATA_TYPE_STRING);
990b4856d0eaada6f8140335733a1b1771ed2746lling verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
990b4856d0eaada6f8140335733a1b1771ed2746lling hex) == 0);
990b4856d0eaada6f8140335733a1b1771ed2746lling free(hex);
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling break;
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746llingbadlabel:
990b4856d0eaada6f8140335733a1b1771ed2746lling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
990b4856d0eaada6f8140335733a1b1771ed2746lling "invalid mlslabel '%s'"), strval);
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
990b4856d0eaada6f8140335733a1b1771ed2746lling m_label_free(new_sl); /* OK if null */
990b4856d0eaada6f8140335733a1b1771ed2746lling goto error;
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling case ZFS_PROP_MOUNTPOINT:
990b4856d0eaada6f8140335733a1b1771ed2746lling {
990b4856d0eaada6f8140335733a1b1771ed2746lling namecheck_err_t why;
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
990b4856d0eaada6f8140335733a1b1771ed2746lling strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
990b4856d0eaada6f8140335733a1b1771ed2746lling break;
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling if (mountpoint_namecheck(strval, &why)) {
990b4856d0eaada6f8140335733a1b1771ed2746lling switch (why) {
990b4856d0eaada6f8140335733a1b1771ed2746lling case NAME_ERR_LEADING_SLASH:
990b4856d0eaada6f8140335733a1b1771ed2746lling zfs_error_aux(hdl,
990b4856d0eaada6f8140335733a1b1771ed2746lling dgettext(TEXT_DOMAIN,
990b4856d0eaada6f8140335733a1b1771ed2746lling "'%s' must be an absolute path, "
990b4856d0eaada6f8140335733a1b1771ed2746lling "'none', or 'legacy'"), propname);
990b4856d0eaada6f8140335733a1b1771ed2746lling break;
990b4856d0eaada6f8140335733a1b1771ed2746lling case NAME_ERR_TOOLONG:
990b4856d0eaada6f8140335733a1b1771ed2746lling zfs_error_aux(hdl,
990b4856d0eaada6f8140335733a1b1771ed2746lling dgettext(TEXT_DOMAIN,
990b4856d0eaada6f8140335733a1b1771ed2746lling "component of '%s' is too long"),
990b4856d0eaada6f8140335733a1b1771ed2746lling propname);
990b4856d0eaada6f8140335733a1b1771ed2746lling break;
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
990b4856d0eaada6f8140335733a1b1771ed2746lling goto error;
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling /*FALLTHRU*/
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling case ZFS_PROP_SHARESMB:
990b4856d0eaada6f8140335733a1b1771ed2746lling case ZFS_PROP_SHARENFS:
990b4856d0eaada6f8140335733a1b1771ed2746lling /*
990b4856d0eaada6f8140335733a1b1771ed2746lling * For the mountpoint and sharenfs or sharesmb
990b4856d0eaada6f8140335733a1b1771ed2746lling * properties, check if it can be set in a
990b4856d0eaada6f8140335733a1b1771ed2746lling * global/non-global zone based on
990b4856d0eaada6f8140335733a1b1771ed2746lling * the zoned property value:
990b4856d0eaada6f8140335733a1b1771ed2746lling *
990b4856d0eaada6f8140335733a1b1771ed2746lling * global zone non-global zone
990b4856d0eaada6f8140335733a1b1771ed2746lling * --------------------------------------------------
990b4856d0eaada6f8140335733a1b1771ed2746lling * zoned=on mountpoint (no) mountpoint (yes)
990b4856d0eaada6f8140335733a1b1771ed2746lling * sharenfs (no) sharenfs (no)
990b4856d0eaada6f8140335733a1b1771ed2746lling * sharesmb (no) sharesmb (no)
990b4856d0eaada6f8140335733a1b1771ed2746lling *
990b4856d0eaada6f8140335733a1b1771ed2746lling * zoned=off mountpoint (yes) N/A
990b4856d0eaada6f8140335733a1b1771ed2746lling * sharenfs (yes)
990b4856d0eaada6f8140335733a1b1771ed2746lling * sharesmb (yes)
990b4856d0eaada6f8140335733a1b1771ed2746lling */
990b4856d0eaada6f8140335733a1b1771ed2746lling if (zoned) {
990b4856d0eaada6f8140335733a1b1771ed2746lling if (getzoneid() == GLOBAL_ZONEID) {
990b4856d0eaada6f8140335733a1b1771ed2746lling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
990b4856d0eaada6f8140335733a1b1771ed2746lling "'%s' cannot be set on "
990b4856d0eaada6f8140335733a1b1771ed2746lling "dataset in a non-global zone"),
990b4856d0eaada6f8140335733a1b1771ed2746lling propname);
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) zfs_error(hdl, EZFS_ZONED,
990b4856d0eaada6f8140335733a1b1771ed2746lling errbuf);
990b4856d0eaada6f8140335733a1b1771ed2746lling goto error;
990b4856d0eaada6f8140335733a1b1771ed2746lling } else if (prop == ZFS_PROP_SHARENFS ||
a9799022bd90b13722204e80112efaa5bf573099ck prop == ZFS_PROP_SHARESMB) {
a9799022bd90b13722204e80112efaa5bf573099ck zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
990b4856d0eaada6f8140335733a1b1771ed2746lling "'%s' cannot be set in "
a9799022bd90b13722204e80112efaa5bf573099ck "a non-global zone"), propname);
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) zfs_error(hdl, EZFS_ZONED,
990b4856d0eaada6f8140335733a1b1771ed2746lling errbuf);
990b4856d0eaada6f8140335733a1b1771ed2746lling goto error;
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling } else if (getzoneid() != GLOBAL_ZONEID) {
a9799022bd90b13722204e80112efaa5bf573099ck /*
990b4856d0eaada6f8140335733a1b1771ed2746lling * If zoned property is 'off', this must be in
990b4856d0eaada6f8140335733a1b1771ed2746lling * a global zone. If not, something is wrong.
990b4856d0eaada6f8140335733a1b1771ed2746lling */
990b4856d0eaada6f8140335733a1b1771ed2746lling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
990b4856d0eaada6f8140335733a1b1771ed2746lling "'%s' cannot be set while dataset "
a9799022bd90b13722204e80112efaa5bf573099ck "'zoned' property is set"), propname);
a9799022bd90b13722204e80112efaa5bf573099ck (void) zfs_error(hdl, EZFS_ZONED, errbuf);
990b4856d0eaada6f8140335733a1b1771ed2746lling goto error;
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling /*
990b4856d0eaada6f8140335733a1b1771ed2746lling * At this point, it is legitimate to set the
990b4856d0eaada6f8140335733a1b1771ed2746lling * property. Now we want to make sure that the
990b4856d0eaada6f8140335733a1b1771ed2746lling * property value is valid if it is sharenfs.
990b4856d0eaada6f8140335733a1b1771ed2746lling */
990b4856d0eaada6f8140335733a1b1771ed2746lling if ((prop == ZFS_PROP_SHARENFS ||
990b4856d0eaada6f8140335733a1b1771ed2746lling prop == ZFS_PROP_SHARESMB) &&
990b4856d0eaada6f8140335733a1b1771ed2746lling strcmp(strval, "on") != 0 &&
990b4856d0eaada6f8140335733a1b1771ed2746lling strcmp(strval, "off") != 0) {
990b4856d0eaada6f8140335733a1b1771ed2746lling zfs_share_proto_t proto;
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling if (prop == ZFS_PROP_SHARESMB)
990b4856d0eaada6f8140335733a1b1771ed2746lling proto = PROTO_SMB;
990b4856d0eaada6f8140335733a1b1771ed2746lling else
990b4856d0eaada6f8140335733a1b1771ed2746lling proto = PROTO_NFS;
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling /*
990b4856d0eaada6f8140335733a1b1771ed2746lling * Must be an valid sharing protocol
990b4856d0eaada6f8140335733a1b1771ed2746lling * option string so init the libshare
990b4856d0eaada6f8140335733a1b1771ed2746lling * in order to enable the parser and
990b4856d0eaada6f8140335733a1b1771ed2746lling * then parse the options. We use the
990b4856d0eaada6f8140335733a1b1771ed2746lling * control API since we don't care about
990b4856d0eaada6f8140335733a1b1771ed2746lling * the current configuration and don't
990b4856d0eaada6f8140335733a1b1771ed2746lling * want the overhead of loading it
990b4856d0eaada6f8140335733a1b1771ed2746lling * until we actually do something.
990b4856d0eaada6f8140335733a1b1771ed2746lling */
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling if (zfs_init_libshare(hdl,
990b4856d0eaada6f8140335733a1b1771ed2746lling SA_INIT_CONTROL_API) != SA_OK) {
990b4856d0eaada6f8140335733a1b1771ed2746lling /*
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * An error occurred so we can't do
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * anything
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens */
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens "'%s' cannot be set: problem "
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens "in share initialization"),
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens propname);
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP,
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens errbuf);
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens goto error;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens }
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens if (zfs_parse_options(strval, proto) != SA_OK) {
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens /*
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * There was an error in parsing so
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * deal with it by issuing an error
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * message and leaving after
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * uninitializing the the libshare
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens * interface.
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens */
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens "'%s' cannot be set to invalid "
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens "options"), propname);
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP,
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens errbuf);
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens zfs_uninit_libshare(hdl);
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens goto error;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens }
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens zfs_uninit_libshare(hdl);
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens }
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens break;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens case ZFS_PROP_UTF8ONLY:
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens chosen_utf = (int)intval;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens break;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens case ZFS_PROP_NORMALIZE:
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens chosen_normal = (int)intval;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens break;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens }
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens /*
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * For changes to existing volumes, we have some additional
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * checks to enforce.
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens */
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
990b4856d0eaada6f8140335733a1b1771ed2746lling uint64_t volsize = zfs_prop_get_int(zhp,
990b4856d0eaada6f8140335733a1b1771ed2746lling ZFS_PROP_VOLSIZE);
990b4856d0eaada6f8140335733a1b1771ed2746lling uint64_t blocksize = zfs_prop_get_int(zhp,
990b4856d0eaada6f8140335733a1b1771ed2746lling ZFS_PROP_VOLBLOCKSIZE);
990b4856d0eaada6f8140335733a1b1771ed2746lling char buf[64];
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling switch (prop) {
990b4856d0eaada6f8140335733a1b1771ed2746lling case ZFS_PROP_RESERVATION:
990b4856d0eaada6f8140335733a1b1771ed2746lling case ZFS_PROP_REFRESERVATION:
990b4856d0eaada6f8140335733a1b1771ed2746lling if (intval > volsize) {
990b4856d0eaada6f8140335733a1b1771ed2746lling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
990b4856d0eaada6f8140335733a1b1771ed2746lling "'%s' is greater than current "
990b4856d0eaada6f8140335733a1b1771ed2746lling "volume size"), propname);
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) zfs_error(hdl, EZFS_BADPROP,
990b4856d0eaada6f8140335733a1b1771ed2746lling errbuf);
990b4856d0eaada6f8140335733a1b1771ed2746lling goto error;
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling break;
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling case ZFS_PROP_VOLSIZE:
990b4856d0eaada6f8140335733a1b1771ed2746lling if (intval % blocksize != 0) {
990b4856d0eaada6f8140335733a1b1771ed2746lling zfs_nicenum(blocksize, buf,
990b4856d0eaada6f8140335733a1b1771ed2746lling sizeof (buf));
990b4856d0eaada6f8140335733a1b1771ed2746lling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
990b4856d0eaada6f8140335733a1b1771ed2746lling "'%s' must be a multiple of "
990b4856d0eaada6f8140335733a1b1771ed2746lling "volume block size (%s)"),
990b4856d0eaada6f8140335733a1b1771ed2746lling propname, buf);
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) zfs_error(hdl, EZFS_BADPROP,
990b4856d0eaada6f8140335733a1b1771ed2746lling errbuf);
990b4856d0eaada6f8140335733a1b1771ed2746lling goto error;
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens if (intval == 0) {
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens "'%s' cannot be zero"),
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens propname);
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP,
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens errbuf);
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens goto error;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens }
990b4856d0eaada6f8140335733a1b1771ed2746lling break;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens }
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling /*
990b4856d0eaada6f8140335733a1b1771ed2746lling * If normalization was chosen, but no UTF8 choice was made,
990b4856d0eaada6f8140335733a1b1771ed2746lling * enforce rejection of non-UTF8 names.
990b4856d0eaada6f8140335733a1b1771ed2746lling *
990b4856d0eaada6f8140335733a1b1771ed2746lling * If normalization was chosen, but rejecting non-UTF8 names
990b4856d0eaada6f8140335733a1b1771ed2746lling * was explicitly not chosen, it is an error.
990b4856d0eaada6f8140335733a1b1771ed2746lling */
990b4856d0eaada6f8140335733a1b1771ed2746lling if (chosen_normal > 0 && chosen_utf < 0) {
990b4856d0eaada6f8140335733a1b1771ed2746lling if (nvlist_add_uint64(ret,
990b4856d0eaada6f8140335733a1b1771ed2746lling zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) no_memory(hdl);
990b4856d0eaada6f8140335733a1b1771ed2746lling goto error;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens }
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens } else if (chosen_normal > 0 && chosen_utf == 0) {
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens "'%s' must be set 'on' if normalization chosen"),
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens goto error;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens }
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens return (ret);
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrenserror:
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens nvlist_free(ret);
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens return (NULL);
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens}
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens
990b4856d0eaada6f8140335733a1b1771ed2746llingint
990b4856d0eaada6f8140335733a1b1771ed2746llingzfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens{
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens uint64_t old_volsize;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens uint64_t new_volsize;
990b4856d0eaada6f8140335733a1b1771ed2746lling uint64_t old_reservation;
990b4856d0eaada6f8140335733a1b1771ed2746lling uint64_t new_reservation;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens zfs_prop_t resv_prop;
990b4856d0eaada6f8140335733a1b1771ed2746lling nvlist_t *props;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens
990b4856d0eaada6f8140335733a1b1771ed2746lling /*
990b4856d0eaada6f8140335733a1b1771ed2746lling * If this is an existing volume, and someone is setting the volsize,
990b4856d0eaada6f8140335733a1b1771ed2746lling * make sure that it matches the reservation, or add it if necessary.
990b4856d0eaada6f8140335733a1b1771ed2746lling */
990b4856d0eaada6f8140335733a1b1771ed2746lling old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
990b4856d0eaada6f8140335733a1b1771ed2746lling if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
990b4856d0eaada6f8140335733a1b1771ed2746lling return (-1);
990b4856d0eaada6f8140335733a1b1771ed2746lling old_reservation = zfs_prop_get_int(zhp, resv_prop);
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling props = fnvlist_alloc();
990b4856d0eaada6f8140335733a1b1771ed2746lling fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
990b4856d0eaada6f8140335733a1b1771ed2746lling zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling if ((zvol_volsize_to_reservation(old_volsize, props) !=
990b4856d0eaada6f8140335733a1b1771ed2746lling old_reservation) || nvlist_exists(nvl,
990b4856d0eaada6f8140335733a1b1771ed2746lling zfs_prop_to_name(resv_prop))) {
990b4856d0eaada6f8140335733a1b1771ed2746lling fnvlist_free(props);
990b4856d0eaada6f8140335733a1b1771ed2746lling return (0);
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
990b4856d0eaada6f8140335733a1b1771ed2746lling &new_volsize) != 0) {
990b4856d0eaada6f8140335733a1b1771ed2746lling fnvlist_free(props);
990b4856d0eaada6f8140335733a1b1771ed2746lling return (-1);
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling new_reservation = zvol_volsize_to_reservation(new_volsize, props);
990b4856d0eaada6f8140335733a1b1771ed2746lling fnvlist_free(props);
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
990b4856d0eaada6f8140335733a1b1771ed2746lling new_reservation) != 0) {
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) no_memory(zhp->zfs_hdl);
990b4856d0eaada6f8140335733a1b1771ed2746lling return (-1);
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling return (1);
990b4856d0eaada6f8140335733a1b1771ed2746lling}
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746llingvoid
990b4856d0eaada6f8140335733a1b1771ed2746llingzfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
990b4856d0eaada6f8140335733a1b1771ed2746lling char *errbuf)
990b4856d0eaada6f8140335733a1b1771ed2746lling{
990b4856d0eaada6f8140335733a1b1771ed2746lling switch (err) {
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling case ENOSPC:
990b4856d0eaada6f8140335733a1b1771ed2746lling /*
990b4856d0eaada6f8140335733a1b1771ed2746lling * For quotas and reservations, ENOSPC indicates
990b4856d0eaada6f8140335733a1b1771ed2746lling * something different; setting a quota or reservation
990b4856d0eaada6f8140335733a1b1771ed2746lling * doesn't use any disk space.
990b4856d0eaada6f8140335733a1b1771ed2746lling */
990b4856d0eaada6f8140335733a1b1771ed2746lling switch (prop) {
990b4856d0eaada6f8140335733a1b1771ed2746lling case ZFS_PROP_QUOTA:
990b4856d0eaada6f8140335733a1b1771ed2746lling case ZFS_PROP_REFQUOTA:
990b4856d0eaada6f8140335733a1b1771ed2746lling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
990b4856d0eaada6f8140335733a1b1771ed2746lling "size is less than current used or "
990b4856d0eaada6f8140335733a1b1771ed2746lling "reserved space"));
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
990b4856d0eaada6f8140335733a1b1771ed2746lling break;
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling case ZFS_PROP_RESERVATION:
990b4856d0eaada6f8140335733a1b1771ed2746lling case ZFS_PROP_REFRESERVATION:
990b4856d0eaada6f8140335733a1b1771ed2746lling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
990b4856d0eaada6f8140335733a1b1771ed2746lling "size is greater than available space"));
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
990b4856d0eaada6f8140335733a1b1771ed2746lling break;
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling default:
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) zfs_standard_error(hdl, err, errbuf);
990b4856d0eaada6f8140335733a1b1771ed2746lling break;
990b4856d0eaada6f8140335733a1b1771ed2746lling }
990b4856d0eaada6f8140335733a1b1771ed2746lling break;
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling case EBUSY:
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) zfs_standard_error(hdl, EBUSY, errbuf);
990b4856d0eaada6f8140335733a1b1771ed2746lling break;
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling case EROFS:
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
990b4856d0eaada6f8140335733a1b1771ed2746lling break;
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling case ENOTSUP:
990b4856d0eaada6f8140335733a1b1771ed2746lling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
990b4856d0eaada6f8140335733a1b1771ed2746lling "pool and or dataset must be upgraded to set this "
990b4856d0eaada6f8140335733a1b1771ed2746lling "property or value"));
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
990b4856d0eaada6f8140335733a1b1771ed2746lling break;
990b4856d0eaada6f8140335733a1b1771ed2746lling
990b4856d0eaada6f8140335733a1b1771ed2746lling case ERANGE:
990b4856d0eaada6f8140335733a1b1771ed2746lling if (prop == ZFS_PROP_COMPRESSION ||
990b4856d0eaada6f8140335733a1b1771ed2746lling prop == ZFS_PROP_RECORDSIZE) {
990b4856d0eaada6f8140335733a1b1771ed2746lling (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
990b4856d0eaada6f8140335733a1b1771ed2746lling "property setting is not allowed on "
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks "bootable datasets"));
(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
} else {
(void) zfs_standard_error(hdl, err, errbuf);
}
break;
case EINVAL:
if (prop == ZPROP_INVAL) {
(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
} else {
(void) zfs_standard_error(hdl, err, errbuf);
}
break;
case EOVERFLOW:
/*
* This platform can't address a volume this big.
*/
#ifdef _ILP32
if (prop == ZFS_PROP_VOLSIZE) {
(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
break;
}
#endif
/* FALLTHROUGH */
default:
(void) zfs_standard_error(hdl, err, errbuf);
}
}
/*
* Given a property name and value, set the property for the given dataset.
*/
int
zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
{
int ret = -1;
char errbuf[1024];
libzfs_handle_t *hdl = zhp->zfs_hdl;
nvlist_t *nvl = NULL;
(void) snprintf(errbuf, sizeof (errbuf),
dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
zhp->zfs_name);
if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
nvlist_add_string(nvl, propname, propval) != 0) {
(void) no_memory(hdl);
goto error;
}
ret = zfs_prop_set_list(zhp, nvl);
error:
nvlist_free(nvl);
return (ret);
}
/*
* Given an nvlist of property names and values, set the properties for the
* given dataset.
*/
int
zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
{
zfs_cmd_t zc = { 0 };
int ret = -1;
prop_changelist_t **cls = NULL;
int cl_idx;
char errbuf[1024];
libzfs_handle_t *hdl = zhp->zfs_hdl;
nvlist_t *nvl;
int nvl_len;
int added_resv;
(void) snprintf(errbuf, sizeof (errbuf),
dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
zhp->zfs_name);
if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
goto error;
/*
* We have to check for any extra properties which need to be added
* before computing the length of the nvlist.
*/
for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
elem != NULL;
elem = nvlist_next_nvpair(nvl, elem)) {
if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
(added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
goto error;
}
}
/*
* Check how many properties we're setting and allocate an array to
* store changelist pointers for postfix().
*/
nvl_len = 0;
for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
elem != NULL;
elem = nvlist_next_nvpair(nvl, elem))
nvl_len++;
if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
goto error;
cl_idx = 0;
for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
elem != NULL;
elem = nvlist_next_nvpair(nvl, elem)) {
zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
assert(cl_idx < nvl_len);
/*
* We don't want to unmount & remount the dataset when changing
* its canmount property to 'on' or 'noauto'. We only use
* the changelist logic to unmount when setting canmount=off.
*/
if (!(prop == ZFS_PROP_CANMOUNT &&
fnvpair_value_uint64(elem) != ZFS_CANMOUNT_OFF)) {
cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
if (cls[cl_idx] == NULL)
goto error;
}
if (prop == ZFS_PROP_MOUNTPOINT &&
changelist_haszonedchild(cls[cl_idx])) {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"child dataset with inherited mountpoint is used "
"in a non-global zone"));
ret = zfs_error(hdl, EZFS_ZONED, errbuf);
goto error;
}
if (cls[cl_idx] != NULL &&
(ret = changelist_prefix(cls[cl_idx])) != 0)
goto error;
cl_idx++;
}
assert(cl_idx == nvl_len);
/*
* Execute the corresponding ioctl() to set this list of properties.
*/
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
(ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
goto error;
ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
if (ret != 0) {
/* Get the list of unset properties back and report them. */
nvlist_t *errorprops = NULL;
if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
goto error;
for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
elem != NULL;
elem = nvlist_next_nvpair(nvl, elem)) {
zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
zfs_setprop_error(hdl, prop, errno, errbuf);
}
nvlist_free(errorprops);
if (added_resv && errno == ENOSPC) {
/* clean up the volsize property we tried to set */
uint64_t old_volsize = zfs_prop_get_int(zhp,
ZFS_PROP_VOLSIZE);
nvlist_free(nvl);
nvl = NULL;
zcmd_free_nvlists(&zc);
if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
goto error;
if (nvlist_add_uint64(nvl,
zfs_prop_to_name(ZFS_PROP_VOLSIZE),
old_volsize) != 0)
goto error;
if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
goto error;
(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
}
} else {
for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
if (cls[cl_idx] != NULL) {
int clp_err = changelist_postfix(cls[cl_idx]);
if (clp_err != 0)
ret = clp_err;
}
}
/*
* Refresh the statistics so the new property value
* is reflected.
*/
if (ret == 0)
(void) get_stats(zhp);
}
error:
nvlist_free(nvl);
zcmd_free_nvlists(&zc);
if (cls != NULL) {
for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
if (cls[cl_idx] != NULL)
changelist_free(cls[cl_idx]);
}
free(cls);
}
return (ret);
}
/*
* Given a property, inherit the value from the parent dataset, or if received
* is TRUE, revert to the received value, if any.
*/
int
zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
{
zfs_cmd_t zc = { 0 };
int ret;
prop_changelist_t *cl;
libzfs_handle_t *hdl = zhp->zfs_hdl;
char errbuf[1024];
zfs_prop_t prop;
(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
"cannot inherit %s for '%s'"), propname, zhp->zfs_name);
zc.zc_cookie = received;
if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
/*
* For user properties, the amount of work we have to do is very
* small, so just do it here.
*/
if (!zfs_prop_user(propname)) {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"invalid property"));
return (zfs_error(hdl, EZFS_BADPROP, errbuf));
}
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
return (zfs_standard_error(hdl, errno, errbuf));
return (0);
}
/*
* Verify that this property is inheritable.
*/
if (zfs_prop_readonly(prop))
return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
if (!zfs_prop_inheritable(prop) && !received)
return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
/*
* Check to see if the value applies to this type
*/
if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
/*
* Normalize the name, to get rid of shorthand abbreviations.
*/
propname = zfs_prop_to_name(prop);
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"dataset is used in a non-global zone"));
return (zfs_error(hdl, EZFS_ZONED, errbuf));
}
/*
* Determine datasets which will be affected by this change, if any.
*/
if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
return (-1);
if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"child dataset with inherited mountpoint is used "
"in a non-global zone"));
ret = zfs_error(hdl, EZFS_ZONED, errbuf);
goto error;
}
if ((ret = changelist_prefix(cl)) != 0)
goto error;
if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
return (zfs_standard_error(hdl, errno, errbuf));
} else {
if ((ret = changelist_postfix(cl)) != 0)
goto error;
/*
* Refresh the statistics so the new property is reflected.
*/
(void) get_stats(zhp);
}
error:
changelist_free(cl);
return (ret);
}
/*
* True DSL properties are stored in an nvlist. The following two functions
* extract them appropriately.
*/
static uint64_t
getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
{
nvlist_t *nv;
uint64_t value;
*source = NULL;
if (nvlist_lookup_nvlist(zhp->zfs_props,
zfs_prop_to_name(prop), &nv) == 0) {
verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
} else {
verify(!zhp->zfs_props_table ||
zhp->zfs_props_table[prop] == B_TRUE);
value = zfs_prop_default_numeric(prop);
*source = "";
}
return (value);
}
static char *
getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
{
nvlist_t *nv;
char *value;
*source = NULL;
if (nvlist_lookup_nvlist(zhp->zfs_props,
zfs_prop_to_name(prop), &nv) == 0) {
verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
} else {
verify(!zhp->zfs_props_table ||
zhp->zfs_props_table[prop] == B_TRUE);
if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
value = "";
*source = "";
}
return (value);
}
static boolean_t
zfs_is_recvd_props_mode(zfs_handle_t *zhp)
{
return (zhp->zfs_props == zhp->zfs_recvd_props);
}
static void
zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
{
*cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
zhp->zfs_props = zhp->zfs_recvd_props;
}
static void
zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
{
zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
*cookie = 0;
}
/*
* Internal function for getting a numeric property. Both zfs_prop_get() and
* zfs_prop_get_int() are built using this interface.
*
* Certain properties can be overridden using 'mount -o'. In this case, scan
* the contents of the /etc/mnttab entry, searching for the appropriate options.
* If they differ from the on-disk values, report the current values and mark
* the source "temporary".
*/
static int
get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
char **source, uint64_t *val)
{
zfs_cmd_t zc = { 0 };
nvlist_t *zplprops = NULL;
struct mnttab mnt;
char *mntopt_on = NULL;
char *mntopt_off = NULL;
boolean_t received = zfs_is_recvd_props_mode(zhp);
*source = NULL;
switch (prop) {
case ZFS_PROP_ATIME:
mntopt_on = MNTOPT_ATIME;
mntopt_off = MNTOPT_NOATIME;
break;
case ZFS_PROP_DEVICES:
mntopt_on = MNTOPT_DEVICES;
mntopt_off = MNTOPT_NODEVICES;
break;
case ZFS_PROP_EXEC:
mntopt_on = MNTOPT_EXEC;
mntopt_off = MNTOPT_NOEXEC;
break;
case ZFS_PROP_READONLY:
mntopt_on = MNTOPT_RO;
mntopt_off = MNTOPT_RW;
break;
case ZFS_PROP_SETUID:
mntopt_on = MNTOPT_SETUID;
mntopt_off = MNTOPT_NOSETUID;
break;
case ZFS_PROP_XATTR:
mntopt_on = MNTOPT_XATTR;
mntopt_off = MNTOPT_NOXATTR;
break;
case ZFS_PROP_NBMAND:
mntopt_on = MNTOPT_NBMAND;
mntopt_off = MNTOPT_NONBMAND;
break;
}
/*
* Because looking up the mount options is potentially expensive
* (iterating over all of /etc/mnttab), we defer its calculation until
* we're looking up a property which requires its presence.
*/
if (!zhp->zfs_mntcheck &&
(mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
libzfs_handle_t *hdl = zhp->zfs_hdl;
struct mnttab entry;
if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
zhp->zfs_mntopts = zfs_strdup(hdl,
entry.mnt_mntopts);
if (zhp->zfs_mntopts == NULL)
return (-1);
}
zhp->zfs_mntcheck = B_TRUE;
}
if (zhp->zfs_mntopts == NULL)
mnt.mnt_mntopts = "";
else
mnt.mnt_mntopts = zhp->zfs_mntopts;
switch (prop) {
case ZFS_PROP_ATIME:
case ZFS_PROP_DEVICES:
case ZFS_PROP_EXEC:
case ZFS_PROP_READONLY:
case ZFS_PROP_SETUID:
case ZFS_PROP_XATTR:
case ZFS_PROP_NBMAND:
*val = getprop_uint64(zhp, prop, source);
if (received)
break;
if (hasmntopt(&mnt, mntopt_on) && !*val) {
*val = B_TRUE;
if (src)
*src = ZPROP_SRC_TEMPORARY;
} else if (hasmntopt(&mnt, mntopt_off) && *val) {
*val = B_FALSE;
if (src)
*src = ZPROP_SRC_TEMPORARY;
}
break;
case ZFS_PROP_CANMOUNT:
case ZFS_PROP_VOLSIZE:
case ZFS_PROP_QUOTA:
case ZFS_PROP_REFQUOTA:
case ZFS_PROP_RESERVATION:
case ZFS_PROP_REFRESERVATION:
case ZFS_PROP_FILESYSTEM_LIMIT:
case ZFS_PROP_SNAPSHOT_LIMIT:
case ZFS_PROP_FILESYSTEM_COUNT:
case ZFS_PROP_SNAPSHOT_COUNT:
*val = getprop_uint64(zhp, prop, source);
if (*source == NULL) {
/* not default, must be local */
*source = zhp->zfs_name;
}
break;
case ZFS_PROP_MOUNTED:
*val = (zhp->zfs_mntopts != NULL);
break;
case ZFS_PROP_NUMCLONES:
*val = zhp->zfs_dmustats.dds_num_clones;
break;
case ZFS_PROP_VERSION:
case ZFS_PROP_NORMALIZE:
case ZFS_PROP_UTF8ONLY:
case ZFS_PROP_CASE:
if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
return (-1);
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
zcmd_free_nvlists(&zc);
return (-1);
}
if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
val) != 0) {
zcmd_free_nvlists(&zc);
return (-1);
}
if (zplprops)
nvlist_free(zplprops);
zcmd_free_nvlists(&zc);
break;
case ZFS_PROP_INCONSISTENT:
*val = zhp->zfs_dmustats.dds_inconsistent;
break;
default:
switch (zfs_prop_get_type(prop)) {
case PROP_TYPE_NUMBER:
case PROP_TYPE_INDEX:
*val = getprop_uint64(zhp, prop, source);
/*
* If we tried to use a default value for a
* readonly property, it means that it was not
* present.
*/
if (zfs_prop_readonly(prop) &&
*source != NULL && (*source)[0] == '\0') {
*source = NULL;
}
break;
case PROP_TYPE_STRING:
default:
zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
"cannot get non-numeric property"));
return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
dgettext(TEXT_DOMAIN, "internal error")));
}
}
return (0);
}
/*
* Calculate the source type, given the raw source string.
*/
static void
get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
char *statbuf, size_t statlen)
{
if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
return;
if (source == NULL) {
*srctype = ZPROP_SRC_NONE;
} else if (source[0] == '\0') {
*srctype = ZPROP_SRC_DEFAULT;
} else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
*srctype = ZPROP_SRC_RECEIVED;
} else {
if (strcmp(source, zhp->zfs_name) == 0) {
*srctype = ZPROP_SRC_LOCAL;
} else {
(void) strlcpy(statbuf, source, statlen);
*srctype = ZPROP_SRC_INHERITED;
}
}
}
int
zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
size_t proplen, boolean_t literal)
{
zfs_prop_t prop;
int err = 0;
if (zhp->zfs_recvd_props == NULL)
if (get_recvd_props_ioctl(zhp) != 0)
return (-1);
prop = zfs_name_to_prop(propname);
if (prop != ZPROP_INVAL) {
uint64_t cookie;
if (!nvlist_exists(zhp->zfs_recvd_props, propname))
return (-1);
zfs_set_recvd_props_mode(zhp, &cookie);
err = zfs_prop_get(zhp, prop, propbuf, proplen,
NULL, NULL, 0, literal);
zfs_unset_recvd_props_mode(zhp, &cookie);
} else {
nvlist_t *propval;
char *recvdval;
if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
propname, &propval) != 0)
return (-1);
verify(nvlist_lookup_string(propval, ZPROP_VALUE,
&recvdval) == 0);
(void) strlcpy(propbuf, recvdval, proplen);
}
return (err == 0 ? 0 : -1);
}
static int
get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
{
nvlist_t *value;
nvpair_t *pair;
value = zfs_get_clones_nvl(zhp);
if (value == NULL)
return (-1);
propbuf[0] = '\0';
for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
pair = nvlist_next_nvpair(value, pair)) {
if (propbuf[0] != '\0')
(void) strlcat(propbuf, ",", proplen);
(void) strlcat(propbuf, nvpair_name(pair), proplen);
}
return (0);
}
struct get_clones_arg {
uint64_t numclones;
nvlist_t *value;
const char *origin;
char buf[ZFS_MAXNAMELEN];
};
int
get_clones_cb(zfs_handle_t *zhp, void *arg)
{
struct get_clones_arg *gca = arg;
if (gca->numclones == 0) {
zfs_close(zhp);
return (0);
}
if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
NULL, NULL, 0, B_TRUE) != 0)
goto out;
if (strcmp(gca->buf, gca->origin) == 0) {
fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
gca->numclones--;
}
out:
(void) zfs_iter_children(zhp, get_clones_cb, gca);
zfs_close(zhp);
return (0);
}
nvlist_t *
zfs_get_clones_nvl(zfs_handle_t *zhp)
{
nvlist_t *nv, *value;
if (nvlist_lookup_nvlist(zhp->zfs_props,
zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
struct get_clones_arg gca;
/*
* if this is a snapshot, then the kernel wasn't able
* to get the clones. Do it by slowly iterating.
*/
if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
return (NULL);
if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
return (NULL);
if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
nvlist_free(nv);
return (NULL);
}
gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
gca.value = value;
gca.origin = zhp->zfs_name;
if (gca.numclones != 0) {
zfs_handle_t *root;
char pool[ZFS_MAXNAMELEN];
char *cp = pool;
/* get the pool name */
(void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
(void) strsep(&cp, "/@");
root = zfs_open(zhp->zfs_hdl, pool,
ZFS_TYPE_FILESYSTEM);
(void) get_clones_cb(root, &gca);
}
if (gca.numclones != 0 ||
nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
nvlist_add_nvlist(zhp->zfs_props,
zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
nvlist_free(nv);
nvlist_free(value);
return (NULL);
}
nvlist_free(nv);
nvlist_free(value);
verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
}
verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
return (value);
}
/*
* Retrieve a property from the given object. If 'literal' is specified, then
* numbers are left as exact values. Otherwise, numbers are converted to a
* human-readable form.
*
* Returns 0 on success, or -1 on error.
*/
int
zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
{
char *source = NULL;
uint64_t val;
char *str;
const char *strval;
boolean_t received = zfs_is_recvd_props_mode(zhp);
/*
* Check to see if this property applies to our object
*/
if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
return (-1);
if (received && zfs_prop_readonly(prop))
return (-1);
if (src)
*src = ZPROP_SRC_NONE;
switch (prop) {
case ZFS_PROP_CREATION:
/*
* 'creation' is a time_t stored in the statistics. We convert
* this into a string unless 'literal' is specified.
*/
{
val = getprop_uint64(zhp, prop, &source);
time_t time = (time_t)val;
struct tm t;
if (literal ||
localtime_r(&time, &t) == NULL ||
strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
&t) == 0)
(void) snprintf(propbuf, proplen, "%llu", val);
}
break;
case ZFS_PROP_MOUNTPOINT:
/*
* Getting the precise mountpoint can be tricky.
*
* - for 'none' or 'legacy', return those values.
* - for inherited mountpoints, we want to take everything
* after our ancestor and append it to the inherited value.
*
* If the pool has an alternate root, we want to prepend that
* root to any values we return.
*/
str = getprop_string(zhp, prop, &source);
if (str[0] == '/') {
char buf[MAXPATHLEN];
char *root = buf;
const char *relpath;
/*
* If we inherit the mountpoint, even from a dataset
* with a received value, the source will be the path of
* the dataset we inherit from. If source is
* ZPROP_SOURCE_VAL_RECVD, the received value is not
* inherited.
*/
if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
relpath = "";
} else {
relpath = zhp->zfs_name + strlen(source);
if (relpath[0] == '/')
relpath++;
}
if ((zpool_get_prop(zhp->zpool_hdl,
ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
B_FALSE)) || (strcmp(root, "-") == 0))
root[0] = '\0';
/*
* Special case an alternate root of '/'. This will
* avoid having multiple leading slashes in the
* mountpoint path.
*/
if (strcmp(root, "/") == 0)
root++;
/*
* If the mountpoint is '/' then skip over this
* if we are obtaining either an alternate root or
* an inherited mountpoint.
*/
if (str[1] == '\0' && (root[0] != '\0' ||
relpath[0] != '\0'))
str++;
if (relpath[0] == '\0')
(void) snprintf(propbuf, proplen, "%s%s",
root, str);
else
(void) snprintf(propbuf, proplen, "%s%s%s%s",
root, str, relpath[0] == '@' ? "" : "/",
relpath);
} else {
/* 'legacy' or 'none' */
(void) strlcpy(propbuf, str, proplen);
}
break;
case ZFS_PROP_ORIGIN:
(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
proplen);
/*
* If there is no parent at all, return failure to indicate that
* it doesn't apply to this dataset.
*/
if (propbuf[0] == '\0')
return (-1);
break;
case ZFS_PROP_CLONES:
if (get_clones_string(zhp, propbuf, proplen) != 0)
return (-1);
break;
case ZFS_PROP_QUOTA:
case ZFS_PROP_REFQUOTA:
case ZFS_PROP_RESERVATION:
case ZFS_PROP_REFRESERVATION:
if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
return (-1);
/*
* If quota or reservation is 0, we translate this into 'none'
* (unless literal is set), and indicate that it's the default
* value. Otherwise, we print the number nicely and indicate
* that its set locally.
*/
if (val == 0) {
if (literal)
(void) strlcpy(propbuf, "0", proplen);
else
(void) strlcpy(propbuf, "none", proplen);
} else {
if (literal)
(void) snprintf(propbuf, proplen, "%llu",
(u_longlong_t)val);
else
zfs_nicenum(val, propbuf, proplen);
}
break;
case ZFS_PROP_FILESYSTEM_LIMIT:
case ZFS_PROP_SNAPSHOT_LIMIT:
case ZFS_PROP_FILESYSTEM_COUNT:
case ZFS_PROP_SNAPSHOT_COUNT:
if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
return (-1);
/*
* If limit is UINT64_MAX, we translate this into 'none' (unless
* literal is set), and indicate that it's the default value.
* Otherwise, we print the number nicely and indicate that it's
* set locally.
*/
if (literal) {
(void) snprintf(propbuf, proplen, "%llu",
(u_longlong_t)val);
} else if (val == UINT64_MAX) {
(void) strlcpy(propbuf, "none", proplen);
} else {
zfs_nicenum(val, propbuf, proplen);
}
break;
case ZFS_PROP_REFRATIO:
case ZFS_PROP_COMPRESSRATIO:
if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
return (-1);
(void) snprintf(propbuf, proplen, "%llu.%02llux",
(u_longlong_t)(val / 100),
(u_longlong_t)(val % 100));
break;
case ZFS_PROP_TYPE:
switch (zhp->zfs_type) {
case ZFS_TYPE_FILESYSTEM:
str = "filesystem";
break;
case ZFS_TYPE_VOLUME:
str = "volume";
break;
case ZFS_TYPE_SNAPSHOT:
str = "snapshot";
break;
case ZFS_TYPE_BOOKMARK:
str = "bookmark";
break;
default:
abort();
}
(void) snprintf(propbuf, proplen, "%s", str);
break;
case ZFS_PROP_MOUNTED:
/*
* The 'mounted' property is a pseudo-property that described
* whether the filesystem is currently mounted. Even though
* it's a boolean value, the typical values of "on" and "off"
* don't make sense, so we translate to "yes" and "no".
*/
if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
src, &source, &val) != 0)
return (-1);
if (val)
(void) strlcpy(propbuf, "yes", proplen);
else
(void) strlcpy(propbuf, "no", proplen);
break;
case ZFS_PROP_NAME:
/*
* The 'name' property is a pseudo-property derived from the
* dataset name. It is presented as a real property to simplify
* consumers.
*/
(void) strlcpy(propbuf, zhp->zfs_name, proplen);
break;
case ZFS_PROP_MLSLABEL:
{
m_label_t *new_sl = NULL;
char *ascii = NULL; /* human readable label */
(void) strlcpy(propbuf,
getprop_string(zhp, prop, &source), proplen);
if (literal || (strcasecmp(propbuf,
ZFS_MLSLABEL_DEFAULT) == 0))
break;
/*
* Try to translate the internal hex string to
* human-readable output. If there are any
* problems just use the hex string.
*/
if (str_to_label(propbuf, &new_sl, MAC_LABEL,
L_NO_CORRECTION, NULL) == -1) {
m_label_free(new_sl);
break;
}
if (label_to_str(new_sl, &ascii, M_LABEL,
DEF_NAMES) != 0) {
if (ascii)
free(ascii);
m_label_free(new_sl);
break;
}
m_label_free(new_sl);
(void) strlcpy(propbuf, ascii, proplen);
free(ascii);
}
break;
case ZFS_PROP_GUID:
/*
* GUIDs are stored as numbers, but they are identifiers.
* We don't want them to be pretty printed, because pretty
* printing mangles the ID into a truncated and useless value.
*/
if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
return (-1);
(void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
break;
default:
switch (zfs_prop_get_type(prop)) {
case PROP_TYPE_NUMBER:
if (get_numeric_property(zhp, prop, src,
&source, &val) != 0)
return (-1);
if (literal)
(void) snprintf(propbuf, proplen, "%llu",
(u_longlong_t)val);
else
zfs_nicenum(val, propbuf, proplen);
break;
case PROP_TYPE_STRING:
(void) strlcpy(propbuf,
getprop_string(zhp, prop, &source), proplen);
break;
case PROP_TYPE_INDEX:
if (get_numeric_property(zhp, prop, src,
&source, &val) != 0)
return (-1);
if (zfs_prop_index_to_string(prop, val, &strval) != 0)
return (-1);
(void) strlcpy(propbuf, strval, proplen);
break;
default:
abort();
}
}
get_source(zhp, src, source, statbuf, statlen);
return (0);
}
/*
* Utility function to get the given numeric property. Does no validation that
* the given property is the appropriate type; should only be used with
* hard-coded property types.
*/
uint64_t
zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
{
char *source;
uint64_t val;
(void) get_numeric_property(zhp, prop, NULL, &source, &val);
return (val);
}
int
zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
{
char buf[64];
(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
}
/*
* Similar to zfs_prop_get(), but returns the value as an integer.
*/
int
zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
zprop_source_t *src, char *statbuf, size_t statlen)
{
char *source;
/*
* Check to see if this property applies to our object
*/
if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
zfs_prop_to_name(prop)));
}
if (src)
*src = ZPROP_SRC_NONE;
if (get_numeric_property(zhp, prop, src, &source, value) != 0)
return (-1);
get_source(zhp, src, source, statbuf, statlen);
return (0);
}
static int
idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
char **domainp, idmap_rid_t *ridp)
{
idmap_get_handle_t *get_hdl = NULL;
idmap_stat status;
int err = EINVAL;
if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
goto out;
if (isuser) {
err = idmap_get_sidbyuid(get_hdl, id,
IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
} else {
err = idmap_get_sidbygid(get_hdl, id,
IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
}
if (err == IDMAP_SUCCESS &&
idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
status == IDMAP_SUCCESS)
err = 0;
else
err = EINVAL;
out:
if (get_hdl)
idmap_get_destroy(get_hdl);
return (err);
}
/*
* convert the propname into parameters needed by kernel
* Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
* Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
*/
static int
userquota_propname_decode(const char *propname, boolean_t zoned,
zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
{
zfs_userquota_prop_t type;
char *cp, *end;
char *numericsid = NULL;
boolean_t isuser;
domain[0] = '\0';
*ridp = 0;
/* Figure out the property type ({user|group}{quota|space}) */
for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
if (strncmp(propname, zfs_userquota_prop_prefixes[type],
strlen(zfs_userquota_prop_prefixes[type])) == 0)
break;
}
if (type == ZFS_NUM_USERQUOTA_PROPS)
return (EINVAL);
*typep = type;
isuser = (type == ZFS_PROP_USERQUOTA ||
type == ZFS_PROP_USERUSED);
cp = strchr(propname, '@') + 1;
if (strchr(cp, '@')) {
/*
* It's a SID name (eg "user@domain") that needs to be
* turned into S-1-domainID-RID.
*/
int flag = 0;
idmap_stat stat, map_stat;
uid_t pid;
idmap_rid_t rid;
idmap_get_handle_t *gh = NULL;
stat = idmap_get_create(&gh);
if (stat != IDMAP_SUCCESS) {
idmap_get_destroy(gh);
return (ENOMEM);
}
if (zoned && getzoneid() == GLOBAL_ZONEID)
return (ENOENT);
if (isuser) {
stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
if (stat < 0)
return (ENOENT);
stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
&rid, &map_stat);
} else {
stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
if (stat < 0)
return (ENOENT);
stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
&rid, &map_stat);
}
if (stat < 0) {
idmap_get_destroy(gh);
return (ENOENT);
}
stat = idmap_get_mappings(gh);
idmap_get_destroy(gh);
if (stat < 0) {
return (ENOENT);
}
if (numericsid == NULL)
return (ENOENT);
cp = numericsid;
*ridp = rid;
/* will be further decoded below */
}
if (strncmp(cp, "S-1-", 4) == 0) {
/* It's a numeric SID (eg "S-1-234-567-89") */
(void) strlcpy(domain, cp, domainlen);
errno = 0;
if (*ridp == 0) {
cp = strrchr(domain, '-');
*cp = '\0';
cp++;
*ridp = strtoull(cp, &end, 10);
} else {
end = "";
}
if (numericsid) {
free(numericsid);
numericsid = NULL;
}
if (errno != 0 || *end != '\0')
return (EINVAL);
} else if (!isdigit(*cp)) {
/*
* It's a user/group name (eg "user") that needs to be
* turned into a uid/gid
*/
if (zoned && getzoneid() == GLOBAL_ZONEID)
return (ENOENT);
if (isuser) {
struct passwd *pw;
pw = getpwnam(cp);
if (pw == NULL)
return (ENOENT);
*ridp = pw->pw_uid;
} else {
struct group *gr;
gr = getgrnam(cp);
if (gr == NULL)
return (ENOENT);
*ridp = gr->gr_gid;
}
} else {
/* It's a user/group ID (eg "12345"). */
uid_t id = strtoul(cp, &end, 10);
idmap_rid_t rid;
char *mapdomain;
if (*end != '\0')
return (EINVAL);
if (id > MAXUID) {
/* It's an ephemeral ID. */
if (idmap_id_to_numeric_domain_rid(id, isuser,
&mapdomain, &rid) != 0)
return (ENOENT);
(void) strlcpy(domain, mapdomain, domainlen);
*ridp = rid;
} else {
*ridp = id;
}
}
ASSERT3P(numericsid, ==, NULL);
return (0);
}
static int
zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
uint64_t *propvalue, zfs_userquota_prop_t *typep)
{
int err;
zfs_cmd_t zc = { 0 };
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
err = userquota_propname_decode(propname,
zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
zc.zc_objset_type = *typep;
if (err)
return (err);
err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
if (err)
return (err);
*propvalue = zc.zc_cookie;
return (0);
}
int
zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
uint64_t *propvalue)
{
zfs_userquota_prop_t type;
return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
&type));
}
int
zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
char *propbuf, int proplen, boolean_t literal)
{
int err;
uint64_t propvalue;
zfs_userquota_prop_t type;
err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
&type);
if (err)
return (err);
if (literal) {
(void) snprintf(propbuf, proplen, "%llu", propvalue);
} else if (propvalue == 0 &&
(type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
(void) strlcpy(propbuf, "none", proplen);
} else {
zfs_nicenum(propvalue, propbuf, proplen);
}
return (0);
}
int
zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
uint64_t *propvalue)
{
int err;
zfs_cmd_t zc = { 0 };
const char *snapname;
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
snapname = strchr(propname, '@') + 1;
if (strchr(snapname, '@')) {
(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
} else {
/* snapname is the short name, append it to zhp's fsname */
char *cp;
(void) strlcpy(zc.zc_value, zhp->zfs_name,
sizeof (zc.zc_value));
cp = strchr(zc.zc_value, '@');
if (cp != NULL)
*cp = '\0';
(void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
(void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
}
err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
if (err)
return (err);
*propvalue = zc.zc_cookie;
return (0);
}
int
zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
char *propbuf, int proplen, boolean_t literal)
{
int err;
uint64_t propvalue;
err = zfs_prop_get_written_int(zhp, propname, &propvalue);
if (err)
return (err);
if (literal) {
(void) snprintf(propbuf, proplen, "%llu", propvalue);
} else {
zfs_nicenum(propvalue, propbuf, proplen);
}
return (0);
}
/*
* Returns the name of the given zfs handle.
*/
const char *
zfs_get_name(const zfs_handle_t *zhp)
{
return (zhp->zfs_name);
}
/*
* Returns the type of the given zfs handle.
*/
zfs_type_t
zfs_get_type(const zfs_handle_t *zhp)
{
return (zhp->zfs_type);
}
/*
* Is one dataset name a child dataset of another?
*
* Needs to handle these cases:
* Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
* Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
* Descendant? No. No. No. Yes.
*/
static boolean_t
is_descendant(const char *ds1, const char *ds2)
{
size_t d1len = strlen(ds1);
/* ds2 can't be a descendant if it's smaller */
if (strlen(ds2) < d1len)
return (B_FALSE);
/* otherwise, compare strings and verify that there's a '/' char */
return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
}
/*
* Given a complete name, return just the portion that refers to the parent.
* Will return -1 if there is no parent (path is just the name of the
* pool).
*/
static int
parent_name(const char *path, char *buf, size_t buflen)
{
char *slashp;
(void) strlcpy(buf, path, buflen);
if ((slashp = strrchr(buf, '/')) == NULL)
return (-1);
*slashp = '\0';
return (0);
}
/*
* If accept_ancestor is false, then check to make sure that the given path has
* a parent, and that it exists. If accept_ancestor is true, then find the
* closest existing ancestor for the given path. In prefixlen return the
* length of already existing prefix of the given path. We also fetch the
* 'zoned' property, which is used to validate property settings when creating
* new datasets.
*/
static int
check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
boolean_t accept_ancestor, int *prefixlen)
{
zfs_cmd_t zc = { 0 };
char parent[ZFS_MAXNAMELEN];
char *slash;
zfs_handle_t *zhp;
char errbuf[1024];
uint64_t is_zoned;
(void) snprintf(errbuf, sizeof (errbuf),
dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
/* get parent, and check to see if this is just a pool */
if (parent_name(path, parent, sizeof (parent)) != 0) {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"missing dataset name"));
return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
}
/* check to see if the pool exists */
if ((slash = strchr(parent, '/')) == NULL)
slash = parent + strlen(parent);
(void) strncpy(zc.zc_name, parent, slash - parent);
zc.zc_name[slash - parent] = '\0';
if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
errno == ENOENT) {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"no such pool '%s'"), zc.zc_name);
return (zfs_error(hdl, EZFS_NOENT, errbuf));
}
/* check to see if the parent dataset exists */
while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
if (errno == ENOENT && accept_ancestor) {
/*
* Go deeper to find an ancestor, give up on top level.
*/
if (parent_name(parent, parent, sizeof (parent)) != 0) {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"no such pool '%s'"), zc.zc_name);
return (zfs_error(hdl, EZFS_NOENT, errbuf));
}
} else if (errno == ENOENT) {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"parent does not exist"));
return (zfs_error(hdl, EZFS_NOENT, errbuf));
} else
return (zfs_standard_error(hdl, errno, errbuf));
}
is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
if (zoned != NULL)
*zoned = is_zoned;
/* we are in a non-global zone, but parent is in the global zone */
if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
(void) zfs_standard_error(hdl, EPERM, errbuf);
zfs_close(zhp);
return (-1);
}
/* make sure parent is a filesystem */
if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"parent is not a filesystem"));
(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
zfs_close(zhp);
return (-1);
}
zfs_close(zhp);
if (prefixlen != NULL)
*prefixlen = strlen(parent);
return (0);
}
/*
* Finds whether the dataset of the given type(s) exists.
*/
boolean_t
zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
{
zfs_handle_t *zhp;
if (!zfs_validate_name(hdl, path, types, B_FALSE))
return (B_FALSE);
/*
* Try to get stats for the dataset, which will tell us if it exists.
*/
if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
int ds_type = zhp->zfs_type;
zfs_close(zhp);
if (types & ds_type)
return (B_TRUE);
}
return (B_FALSE);
}
/*
* Given a path to 'target', create all the ancestors between
* the prefixlen portion of the path, and the target itself.
* Fail if the initial prefixlen-ancestor does not already exist.
*/
int
create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
{
zfs_handle_t *h;
char *cp;
const char *opname;
/* make sure prefix exists */
cp = target + prefixlen;
if (*cp != '/') {
assert(strchr(cp, '/') == NULL);
h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
} else {
*cp = '\0';
h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
*cp = '/';
}
if (h == NULL)
return (-1);
zfs_close(h);
/*
* Attempt to create, mount, and share any ancestor filesystems,
* up to the prefixlen-long one.
*/
for (cp = target + prefixlen + 1;
cp = strchr(cp, '/'); *cp = '/', cp++) {
*cp = '\0';
h = make_dataset_handle(hdl, target);
if (h) {
/* it already exists, nothing to do here */
zfs_close(h);
continue;
}
if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
NULL) != 0) {
opname = dgettext(TEXT_DOMAIN, "create");
goto ancestorerr;
}
h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
if (h == NULL) {
opname = dgettext(TEXT_DOMAIN, "open");
goto ancestorerr;
}
if (zfs_mount(h, NULL, 0) != 0) {
opname = dgettext(TEXT_DOMAIN, "mount");
goto ancestorerr;
}
if (zfs_share(h) != 0) {
opname = dgettext(TEXT_DOMAIN, "share");
goto ancestorerr;
}
zfs_close(h);
}
return (0);
ancestorerr:
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"failed to %s ancestor '%s'"), opname, target);
return (-1);
}
/*
* Creates non-existing ancestors of the given path.
*/
int
zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
{
int prefix;
char *path_copy;
int rc;
if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
return (-1);
if ((path_copy = strdup(path)) != NULL) {
rc = create_parents(hdl, path_copy, prefix);
free(path_copy);
}
if (path_copy == NULL || rc != 0)
return (-1);
return (0);
}
/*
* Create a new filesystem or volume.
*/
int
zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
nvlist_t *props)
{
int ret;
uint64_t size = 0;
uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
char errbuf[1024];
uint64_t zoned;
dmu_objset_type_t ost;
(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
"cannot create '%s'"), path);
/* validate the path, taking care to note the extended error message */
if (!zfs_validate_name(hdl, path, type, B_TRUE))
return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
/* validate parents exist */
if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
return (-1);
/*
* The failure modes when creating a dataset of a different type over
* one that already exists is a little strange. In particular, if you
* try to create a dataset on top of an existing dataset, the ioctl()
* will return ENOENT, not EEXIST. To prevent this from happening, we
* first try to see if the dataset exists.
*/
if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"dataset already exists"));
return (zfs_error(hdl, EZFS_EXISTS, errbuf));
}
if (type == ZFS_TYPE_VOLUME)
ost = DMU_OST_ZVOL;
else
ost = DMU_OST_ZFS;
if (props && (props = zfs_valid_proplist(hdl, type, props,
zoned, NULL, errbuf)) == 0)
return (-1);
if (type == ZFS_TYPE_VOLUME) {
/*
* If we are creating a volume, the size and block size must
* satisfy a few restraints. First, the blocksize must be a
* valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
* volsize must be a multiple of the block size, and cannot be
* zero.
*/
if (props == NULL || nvlist_lookup_uint64(props,
zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
nvlist_free(props);
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"missing volume size"));
return (zfs_error(hdl, EZFS_BADPROP, errbuf));
}
if ((ret = nvlist_lookup_uint64(props,
zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
&blocksize)) != 0) {
if (ret == ENOENT) {
blocksize = zfs_prop_default_numeric(
ZFS_PROP_VOLBLOCKSIZE);
} else {
nvlist_free(props);
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"missing volume block size"));
return (zfs_error(hdl, EZFS_BADPROP, errbuf));
}
}
if (size == 0) {
nvlist_free(props);
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"volume size cannot be zero"));
return (zfs_error(hdl, EZFS_BADPROP, errbuf));
}
if (size % blocksize != 0) {
nvlist_free(props);
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"volume size must be a multiple of volume block "
"size"));
return (zfs_error(hdl, EZFS_BADPROP, errbuf));
}
}
/* create the dataset */
ret = lzc_create(path, ost, props);
nvlist_free(props);
/* check for failure */
if (ret != 0) {
char parent[ZFS_MAXNAMELEN];
(void) parent_name(path, parent, sizeof (parent));
switch (errno) {
case ENOENT:
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"no such parent '%s'"), parent);
return (zfs_error(hdl, EZFS_NOENT, errbuf));
case EINVAL:
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"parent '%s' is not a filesystem"), parent);
return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
case EDOM:
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"volume block size must be power of 2 from "
"512B to 128KB"));
return (zfs_error(hdl, EZFS_BADPROP, errbuf));
case ENOTSUP:
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"pool must be upgraded to set this "
"property or value"));
return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
#ifdef _ILP32
case EOVERFLOW:
/*
* This platform can't address a volume this big.
*/
if (type == ZFS_TYPE_VOLUME)
return (zfs_error(hdl, EZFS_VOLTOOBIG,
errbuf));
#endif
/* FALLTHROUGH */
default:
return (zfs_standard_error(hdl, errno, errbuf));
}
}
return (0);
}
/*
* Destroys the given dataset. The caller must make sure that the filesystem
* isn't mounted, and that there are no active dependents. If the file system
* does not exist this function does nothing.
*/
int
zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
{
zfs_cmd_t zc = { 0 };
if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
nvlist_t *nv = fnvlist_alloc();
fnvlist_add_boolean(nv, zhp->zfs_name);
int error = lzc_destroy_bookmarks(nv, NULL);
fnvlist_free(nv);
if (error != 0) {
return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
zhp->zfs_name));
}
return (0);
}
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
if (ZFS_IS_VOLUME(zhp)) {
zc.zc_objset_type = DMU_OST_ZVOL;
} else {
zc.zc_objset_type = DMU_OST_ZFS;
}
zc.zc_defer_destroy = defer;
if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
errno != ENOENT) {
return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
zhp->zfs_name));
}
remove_mountpoint(zhp);
return (0);
}
struct destroydata {
nvlist_t *nvl;
const char *snapname;
};
static int
zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
{
struct destroydata *dd = arg;
char name[ZFS_MAXNAMELEN];
int rv = 0;
(void) snprintf(name, sizeof (name),
"%s@%s", zhp->zfs_name, dd->snapname);
if (lzc_exists(name))
verify(nvlist_add_boolean(dd->nvl, name) == 0);
rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
zfs_close(zhp);
return (rv);
}
/*
* Destroys all snapshots with the given name in zhp & descendants.
*/
int
zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
{
int ret;
struct destroydata dd = { 0 };
dd.snapname = snapname;
verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
(void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
if (nvlist_empty(dd.nvl)) {
ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
zhp->zfs_name, snapname);
} else {
ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
}
nvlist_free(dd.nvl);
return (ret);
}
/*
* Destroys all the snapshots named in the nvlist.
*/
int
zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
{
int ret;
nvlist_t *errlist;
ret = lzc_destroy_snaps(snaps, defer, &errlist);
if (ret == 0)
return (0);
if (nvlist_empty(errlist)) {
char errbuf[1024];
(void) snprintf(errbuf, sizeof (errbuf),
dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
ret = zfs_standard_error(hdl, ret, errbuf);
}
for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
char errbuf[1024];
(void) snprintf(errbuf, sizeof (errbuf),
dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
nvpair_name(pair));
switch (fnvpair_value_int32(pair)) {
case EEXIST:
zfs_error_aux(hdl,
dgettext(TEXT_DOMAIN, "snapshot is cloned"));
ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
break;
default:
ret = zfs_standard_error(hdl, errno, errbuf);
break;
}
}
return (ret);
}
/*
* Clones the given dataset. The target must be of the same type as the source.
*/
int
zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
{
char parent[ZFS_MAXNAMELEN];
int ret;
char errbuf[1024];
libzfs_handle_t *hdl = zhp->zfs_hdl;
uint64_t zoned;
assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
"cannot create '%s'"), target);
/* validate the target/clone name */
if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
/* validate parents exist */
if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
return (-1);
(void) parent_name(target, parent, sizeof (parent));
/* do the clone */
if (props) {
zfs_type_t type;
if (ZFS_IS_VOLUME(zhp)) {
type = ZFS_TYPE_VOLUME;
} else {
type = ZFS_TYPE_FILESYSTEM;
}
if ((props = zfs_valid_proplist(hdl, type, props, zoned,
zhp, errbuf)) == NULL)
return (-1);
}
ret = lzc_clone(target, zhp->zfs_name, props);
nvlist_free(props);
if (ret != 0) {
switch (errno) {
case ENOENT:
/*
* The parent doesn't exist. We should have caught this
* above, but there may a race condition that has since
* destroyed the parent.
*
* At this point, we don't know whether it's the source
* that doesn't exist anymore, or whether the target
* dataset doesn't exist.
*/
zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
"no such parent '%s'"), parent);
return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
case EXDEV:
zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
"source and target pools differ"));
return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
errbuf));
default:
return (zfs_standard_error(zhp->zfs_hdl, errno,
errbuf));
}
}
return (ret);
}
/*
* Promotes the given clone fs to be the clone parent.
*/
int
zfs_promote(zfs_handle_t *zhp)
{
libzfs_handle_t *hdl = zhp->zfs_hdl;
zfs_cmd_t zc = { 0 };
char parent[MAXPATHLEN];
int ret;
char errbuf[1024];
(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
"cannot promote '%s'"), zhp->zfs_name);
if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"snapshots can not be promoted"));
return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
}
(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
if (parent[0] == '\0') {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"not a cloned filesystem"));
return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
}
(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
sizeof (zc.zc_value));
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
if (ret != 0) {
int save_errno = errno;
switch (save_errno) {
case EEXIST:
/* There is a conflicting snapshot name. */
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"conflicting snapshot '%s' from parent '%s'"),
zc.zc_string, parent);
return (zfs_error(hdl, EZFS_EXISTS, errbuf));
default:
return (zfs_standard_error(hdl, save_errno, errbuf));
}
}
return (ret);
}
typedef struct snapdata {
nvlist_t *sd_nvl;
const char *sd_snapname;
} snapdata_t;
static int
zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
{
snapdata_t *sd = arg;
char name[ZFS_MAXNAMELEN];
int rv = 0;
if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
(void) snprintf(name, sizeof (name),
"%s@%s", zfs_get_name(zhp), sd->sd_snapname);
fnvlist_add_boolean(sd->sd_nvl, name);
rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
}
zfs_close(zhp);
return (rv);
}
/*
* Creates snapshots. The keys in the snaps nvlist are the snapshots to be
* created.
*/
int
zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
{
int ret;
char errbuf[1024];
nvpair_t *elem;
nvlist_t *errors;
(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
"cannot create snapshots "));
elem = NULL;
while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
const char *snapname = nvpair_name(elem);
/* validate the target name */
if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
B_TRUE)) {
(void) snprintf(errbuf, sizeof (errbuf),
dgettext(TEXT_DOMAIN,
"cannot create snapshot '%s'"), snapname);
return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
}
}
if (props != NULL &&
(props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
props, B_FALSE, NULL, errbuf)) == NULL) {
return (-1);
}
ret = lzc_snapshot(snaps, props, &errors);
if (ret != 0) {
boolean_t printed = B_FALSE;
for (elem = nvlist_next_nvpair(errors, NULL);
elem != NULL;
elem = nvlist_next_nvpair(errors, elem)) {
(void) snprintf(errbuf, sizeof (errbuf),
dgettext(TEXT_DOMAIN,
"cannot create snapshot '%s'"), nvpair_name(elem));
(void) zfs_standard_error(hdl,
fnvpair_value_int32(elem), errbuf);
printed = B_TRUE;
}
if (!printed) {
switch (ret) {
case EXDEV:
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"multiple snapshots of same "
"fs not allowed"));
(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
break;
default:
(void) zfs_standard_error(hdl, ret, errbuf);
}
}
}
nvlist_free(props);
nvlist_free(errors);
return (ret);
}
int
zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
nvlist_t *props)
{
int ret;
snapdata_t sd = { 0 };
char fsname[ZFS_MAXNAMELEN];
char *cp;
zfs_handle_t *zhp;
char errbuf[1024];
(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
"cannot snapshot %s"), path);
if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
(void) strlcpy(fsname, path, sizeof (fsname));
cp = strchr(fsname, '@');
*cp = '\0';
sd.sd_snapname = cp + 1;
if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
ZFS_TYPE_VOLUME)) == NULL) {
return (-1);
}
verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
if (recursive) {
(void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
} else {
fnvlist_add_boolean(sd.sd_nvl, path);
}
ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
nvlist_free(sd.sd_nvl);
zfs_close(zhp);
return (ret);
}
/*
* Destroy any more recent snapshots. We invoke this callback on any dependents
* of the snapshot first. If the 'cb_dependent' member is non-zero, then this
* is a dependent and we should just destroy it without checking the transaction
* group.
*/
typedef struct rollback_data {
const char *cb_target; /* the snapshot */
uint64_t cb_create; /* creation time reference */
boolean_t cb_error;
boolean_t cb_force;
} rollback_data_t;
static int
rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
{
rollback_data_t *cbp = data;
prop_changelist_t *clp;
/* We must destroy this clone; first unmount it */
clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
cbp->cb_force ? MS_FORCE: 0);
if (clp == NULL || changelist_prefix(clp) != 0) {
cbp->cb_error = B_TRUE;
zfs_close(zhp);
return (0);
}
if (zfs_destroy(zhp, B_FALSE) != 0)
cbp->cb_error = B_TRUE;
else
changelist_remove(clp, zhp->zfs_name);
(void) changelist_postfix(clp);
changelist_free(clp);
zfs_close(zhp);
return (0);
}
static int
rollback_destroy(zfs_handle_t *zhp, void *data)
{
rollback_data_t *cbp = data;
if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
rollback_destroy_dependent, cbp);
cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
}
zfs_close(zhp);
return (0);
}
/*
* Given a dataset, rollback to a specific snapshot, discarding any
* data changes since then and making it the active dataset.
*
* Any snapshots and bookmarks more recent than the target are
* destroyed, along with their dependents (i.e. clones).
*/
int
zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
{
rollback_data_t cb = { 0 };
int err;
boolean_t restore_resv = 0;
uint64_t old_volsize, new_volsize;
zfs_prop_t resv_prop;
assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
zhp->zfs_type == ZFS_TYPE_VOLUME);
/*
* Destroy all recent snapshots and their dependents.
*/
cb.cb_force = force;
cb.cb_target = snap->zfs_name;
cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
(void) zfs_iter_snapshots(zhp, rollback_destroy, &cb);
(void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
if (cb.cb_error)
return (-1);
/*
* Now that we have verified that the snapshot is the latest,
* rollback to the given snapshot.
*/
if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
return (-1);
old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
restore_resv =
(old_volsize == zfs_prop_get_int(zhp, resv_prop));
}
/*
* We rely on zfs_iter_children() to verify that there are no
* newer snapshots for the given dataset. Therefore, we can
* simply pass the name on to the ioctl() call. There is still
* an unlikely race condition where the user has taken a
* snapshot since we verified that this was the most recent.
*/
err = lzc_rollback(zhp->zfs_name, NULL, 0);
if (err != 0) {
(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
zhp->zfs_name);
return (err);
}
/*
* For volumes, if the pre-rollback volsize matched the pre-
* rollback reservation and the volsize has changed then set
* the reservation property to the post-rollback volsize.
* Make a new handle since the rollback closed the dataset.
*/
if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
(zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
if (restore_resv) {
new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
if (old_volsize != new_volsize)
err = zfs_prop_set_int(zhp, resv_prop,
new_volsize);
}
zfs_close(zhp);
}
return (err);
}
/*
* Renames the given dataset.
*/
int
zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
boolean_t force_unmount)
{
int ret;
zfs_cmd_t zc = { 0 };
char *delim;
prop_changelist_t *cl = NULL;
zfs_handle_t *zhrp = NULL;
char *parentname = NULL;
char parent[ZFS_MAXNAMELEN];
libzfs_handle_t *hdl = zhp->zfs_hdl;
char errbuf[1024];
/* if we have the same exact name, just return success */
if (strcmp(zhp->zfs_name, target) == 0)
return (0);
(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
"cannot rename to '%s'"), target);
/*
* Make sure the target name is valid
*/
if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
if ((strchr(target, '@') == NULL) ||
*target == '@') {
/*
* Snapshot target name is abbreviated,
* reconstruct full dataset name
*/
(void) strlcpy(parent, zhp->zfs_name,
sizeof (parent));
delim = strchr(parent, '@');
if (strchr(target, '@') == NULL)
*(++delim) = '\0';
else
*delim = '\0';
(void) strlcat(parent, target, sizeof (parent));
target = parent;
} else {
/*
* Make sure we're renaming within the same dataset.
*/
delim = strchr(target, '@');
if (strncmp(zhp->zfs_name, target, delim - target)
!= 0 || zhp->zfs_name[delim - target] != '@') {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"snapshots must be part of same "
"dataset"));
return (zfs_error(hdl, EZFS_CROSSTARGET,
errbuf));
}
}
if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
} else {
if (recursive) {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"recursive rename must be a snapshot"));
return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
}
if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
/* validate parents */
if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
return (-1);
/* make sure we're in the same pool */
verify((delim = strchr(target, '/')) != NULL);
if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
zhp->zfs_name[delim - target] != '/') {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"datasets must be within same pool"));
return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
}
/* new name cannot be a child of the current dataset name */
if (is_descendant(zhp->zfs_name, target)) {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"New dataset name cannot be a descendant of "
"current dataset name"));
return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
}
}
(void) snprintf(errbuf, sizeof (errbuf),
dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
if (getzoneid() == GLOBAL_ZONEID &&
zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"dataset is used in a non-global zone"));
return (zfs_error(hdl, EZFS_ZONED, errbuf));
}
if (recursive) {
parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
if (parentname == NULL) {
ret = -1;
goto error;
}
delim = strchr(parentname, '@');
*delim = '\0';
zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
if (zhrp == NULL) {
ret = -1;
goto error;
}
} else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
force_unmount ? MS_FORCE : 0)) == NULL)
return (-1);
if (changelist_haszonedchild(cl)) {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"child dataset with inherited mountpoint is used "
"in a non-global zone"));
(void) zfs_error(hdl, EZFS_ZONED, errbuf);
goto error;
}
if ((ret = changelist_prefix(cl)) != 0)
goto error;
}
if (ZFS_IS_VOLUME(zhp))
zc.zc_objset_type = DMU_OST_ZVOL;
else
zc.zc_objset_type = DMU_OST_ZFS;
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
zc.zc_cookie = recursive;
if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
/*
* if it was recursive, the one that actually failed will
* be in zc.zc_name
*/
(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
"cannot rename '%s'"), zc.zc_name);
if (recursive && errno == EEXIST) {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"a child dataset already has a snapshot "
"with the new name"));
(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
} else {
(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
}
/*
* On failure, we still want to remount any filesystems that
* were previously mounted, so we don't alter the system state.
*/
if (cl != NULL)
(void) changelist_postfix(cl);
} else {
if (cl != NULL) {
changelist_rename(cl, zfs_get_name(zhp), target);
ret = changelist_postfix(cl);
}
}
error:
if (parentname != NULL) {
free(parentname);
}
if (zhrp != NULL) {
zfs_close(zhrp);
}
if (cl != NULL) {
changelist_free(cl);
}
return (ret);
}
nvlist_t *
zfs_get_user_props(zfs_handle_t *zhp)
{
return (zhp->zfs_user_props);
}
nvlist_t *
zfs_get_recvd_props(zfs_handle_t *zhp)
{
if (zhp->zfs_recvd_props == NULL)
if (get_recvd_props_ioctl(zhp) != 0)
return (NULL);
return (zhp->zfs_recvd_props);
}
/*
* This function is used by 'zfs list' to determine the exact set of columns to
* display, and their maximum widths. This does two main things:
*
* - If this is a list of all properties, then expand the list to include
* all native properties, and set a flag so that for each dataset we look
* for new unique user properties and add them to the list.
*
* - For non fixed-width properties, keep track of the maximum width seen
* so that we can size the column appropriately. If the user has
* requested received property values, we also need to compute the width
* of the RECEIVED column.
*/
int
zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
boolean_t literal)
{
libzfs_handle_t *hdl = zhp->zfs_hdl;
zprop_list_t *entry;
zprop_list_t **last, **start;
nvlist_t *userprops, *propval;
nvpair_t *elem;
char *strval;
char buf[ZFS_MAXPROPLEN];
if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
return (-1);
userprops = zfs_get_user_props(zhp);
entry = *plp;
if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
/*
* Go through and add any user properties as necessary. We
* start by incrementing our list pointer to the first
* non-native property.
*/
start = plp;
while (*start != NULL) {
if ((*start)->pl_prop == ZPROP_INVAL)
break;
start = &(*start)->pl_next;
}
elem = NULL;
while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
/*
* See if we've already found this property in our list.
*/
for (last = start; *last != NULL;
last = &(*last)->pl_next) {
if (strcmp((*last)->pl_user_prop,
nvpair_name(elem)) == 0)
break;
}
if (*last == NULL) {
if ((entry = zfs_alloc(hdl,
sizeof (zprop_list_t))) == NULL ||
((entry->pl_user_prop = zfs_strdup(hdl,
nvpair_name(elem)))) == NULL) {
free(entry);
return (-1);
}
entry->pl_prop = ZPROP_INVAL;
entry->pl_width = strlen(nvpair_name(elem));
entry->pl_all = B_TRUE;
*last = entry;
}
}
}
/*
* Now go through and check the width of any non-fixed columns
*/
for (entry = *plp; entry != NULL; entry = entry->pl_next) {
if (entry->pl_fixed && !literal)
continue;
if (entry->pl_prop != ZPROP_INVAL) {
if (zfs_prop_get(zhp, entry->pl_prop,
buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
if (strlen(buf) > entry->pl_width)
entry->pl_width = strlen(buf);
}
if (received && zfs_prop_get_recvd(zhp,
zfs_prop_to_name(entry->pl_prop),
buf, sizeof (buf), literal) == 0)
if (strlen(buf) > entry->pl_recvd_width)
entry->pl_recvd_width = strlen(buf);
} else {
if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
&propval) == 0) {
verify(nvlist_lookup_string(propval,
ZPROP_VALUE, &strval) == 0);
if (strlen(strval) > entry->pl_width)
entry->pl_width = strlen(strval);
}
if (received && zfs_prop_get_recvd(zhp,
entry->pl_user_prop,
buf, sizeof (buf), literal) == 0)
if (strlen(buf) > entry->pl_recvd_width)
entry->pl_recvd_width = strlen(buf);
}
}
return (0);
}
int
zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
char *resource, void *export, void *sharetab,
int sharemax, zfs_share_op_t operation)
{
zfs_cmd_t zc = { 0 };
int error;
(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
if (resource)
(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
zc.zc_share.z_sharetype = operation;
zc.zc_share.z_sharemax = sharemax;
error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
return (error);
}
void
zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
{
nvpair_t *curr;
/*
* Keep a reference to the props-table against which we prune the
* properties.
*/
zhp->zfs_props_table = props;
curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
while (curr) {
zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
/*
* User properties will result in ZPROP_INVAL, and since we
* only know how to prune standard ZFS properties, we always
* leave these in the list. This can also happen if we
* encounter an unknown DSL property (when running older
* software, for example).
*/
if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
(void) nvlist_remove(zhp->zfs_props,
nvpair_name(curr), nvpair_type(curr));
curr = next;
}
}
static int
zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
{
zfs_cmd_t zc = { 0 };
nvlist_t *nvlist = NULL;
int error;
(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
zc.zc_cookie = (uint64_t)cmd;
if (cmd == ZFS_SMB_ACL_RENAME) {
if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
(void) no_memory(hdl);
return (0);
}
}
switch (cmd) {
case ZFS_SMB_ACL_ADD:
case ZFS_SMB_ACL_REMOVE:
(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
break;
case ZFS_SMB_ACL_RENAME:
if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
resource1) != 0) {
(void) no_memory(hdl);
return (-1);
}
if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
resource2) != 0) {
(void) no_memory(hdl);
return (-1);
}
if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
nvlist_free(nvlist);
return (-1);
}
break;
case ZFS_SMB_ACL_PURGE:
break;
default:
return (-1);
}
error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
if (nvlist)
nvlist_free(nvlist);
return (error);
}
int
zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
char *path, char *resource)
{
return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
resource, NULL));
}
int
zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
char *path, char *resource)
{
return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
resource, NULL));
}
int
zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
{
return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
NULL, NULL));
}
int
zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
char *oldname, char *newname)
{
return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
oldname, newname));
}
int
zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
zfs_userspace_cb_t func, void *arg)
{
zfs_cmd_t zc = { 0 };
zfs_useracct_t buf[100];
libzfs_handle_t *hdl = zhp->zfs_hdl;
int ret;
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
zc.zc_objset_type = type;
zc.zc_nvlist_dst = (uintptr_t)buf;
for (;;) {
zfs_useracct_t *zua = buf;
zc.zc_nvlist_dst_size = sizeof (buf);
if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
char errbuf[1024];
(void) snprintf(errbuf, sizeof (errbuf),
dgettext(TEXT_DOMAIN,
"cannot get used/quota for %s"), zc.zc_name);
return (zfs_standard_error_fmt(hdl, errno, errbuf));
}
if (zc.zc_nvlist_dst_size == 0)
break;
while (zc.zc_nvlist_dst_size > 0) {
if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
zua->zu_space)) != 0)
return (ret);
zua++;
zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
}
}
return (0);
}
struct holdarg {
nvlist_t *nvl;
const char *snapname;
const char *tag;
boolean_t recursive;
int error;
};
static int
zfs_hold_one(zfs_handle_t *zhp, void *arg)
{
struct holdarg *ha = arg;
char name[ZFS_MAXNAMELEN];
int rv = 0;
(void) snprintf(name, sizeof (name),
"%s@%s", zhp->zfs_name, ha->snapname);
if (lzc_exists(name))
fnvlist_add_string(ha->nvl, name, ha->tag);
if (ha->recursive)
rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
zfs_close(zhp);
return (rv);
}
int
zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
boolean_t recursive, int cleanup_fd)
{
int ret;
struct holdarg ha;
ha.nvl = fnvlist_alloc();
ha.snapname = snapname;
ha.tag = tag;
ha.recursive = recursive;
(void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
if (nvlist_empty(ha.nvl)) {
char errbuf[1024];
fnvlist_free(ha.nvl);
ret = ENOENT;
(void) snprintf(errbuf, sizeof (errbuf),
dgettext(TEXT_DOMAIN,
"cannot hold snapshot '%s@%s'"),
zhp->zfs_name, snapname);
(void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
return (ret);
}
ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
fnvlist_free(ha.nvl);
return (ret);
}
int
zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
{
int ret;
nvlist_t *errors;
libzfs_handle_t *hdl = zhp->zfs_hdl;
char errbuf[1024];
nvpair_t *elem;
errors = NULL;
ret = lzc_hold(holds, cleanup_fd, &errors);
if (ret == 0) {
/* There may be errors even in the success case. */
fnvlist_free(errors);
return (0);
}
if (nvlist_empty(errors)) {
/* no hold-specific errors */
(void) snprintf(errbuf, sizeof (errbuf),
dgettext(TEXT_DOMAIN, "cannot hold"));
switch (ret) {
case ENOTSUP:
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"pool must be upgraded"));
(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
break;
case EINVAL:
(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
break;
default:
(void) zfs_standard_error(hdl, ret, errbuf);
}
}
for (elem = nvlist_next_nvpair(errors, NULL);
elem != NULL;
elem = nvlist_next_nvpair(errors, elem)) {
(void) snprintf(errbuf, sizeof (errbuf),
dgettext(TEXT_DOMAIN,
"cannot hold snapshot '%s'"), nvpair_name(elem));
switch (fnvpair_value_int32(elem)) {
case E2BIG:
/*
* Temporary tags wind up having the ds object id
* prepended. So even if we passed the length check
* above, it's still possible for the tag to wind
* up being slightly too long.
*/
(void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
break;
case EINVAL:
(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
break;
case EEXIST:
(void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
break;
default:
(void) zfs_standard_error(hdl,
fnvpair_value_int32(elem), errbuf);
}
}
fnvlist_free(errors);
return (ret);
}
static int
zfs_release_one(zfs_handle_t *zhp, void *arg)
{
struct holdarg *ha = arg;
char name[ZFS_MAXNAMELEN];
int rv = 0;
nvlist_t *existing_holds;
(void) snprintf(name, sizeof (name),
"%s@%s", zhp->zfs_name, ha->snapname);
if (lzc_get_holds(name, &existing_holds) != 0) {
ha->error = ENOENT;
} else if (!nvlist_exists(existing_holds, ha->tag)) {
ha->error = ESRCH;
} else {
nvlist_t *torelease = fnvlist_alloc();
fnvlist_add_boolean(torelease, ha->tag);
fnvlist_add_nvlist(ha->nvl, name, torelease);
fnvlist_free(torelease);
}
if (ha->recursive)
rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
zfs_close(zhp);
return (rv);
}
int
zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
boolean_t recursive)
{
int ret;
struct holdarg ha;
nvlist_t *errors = NULL;
nvpair_t *elem;
libzfs_handle_t *hdl = zhp->zfs_hdl;
char errbuf[1024];
ha.nvl = fnvlist_alloc();
ha.snapname = snapname;
ha.tag = tag;
ha.recursive = recursive;
ha.error = 0;
(void) zfs_release_one(zfs_handle_dup(zhp), &ha);
if (nvlist_empty(ha.nvl)) {
fnvlist_free(ha.nvl);
ret = ha.error;
(void) snprintf(errbuf, sizeof (errbuf),
dgettext(TEXT_DOMAIN,
"cannot release hold from snapshot '%s@%s'"),
zhp->zfs_name, snapname);
if (ret == ESRCH) {
(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
} else {
(void) zfs_standard_error(hdl, ret, errbuf);
}
return (ret);
}
ret = lzc_release(ha.nvl, &errors);
fnvlist_free(ha.nvl);
if (ret == 0) {
/* There may be errors even in the success case. */
fnvlist_free(errors);
return (0);
}
if (nvlist_empty(errors)) {
/* no hold-specific errors */
(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
"cannot release"));
switch (errno) {
case ENOTSUP:
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"pool must be upgraded"));
(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
break;
default:
(void) zfs_standard_error_fmt(hdl, errno, errbuf);
}
}
for (elem = nvlist_next_nvpair(errors, NULL);
elem != NULL;
elem = nvlist_next_nvpair(errors, elem)) {
(void) snprintf(errbuf, sizeof (errbuf),
dgettext(TEXT_DOMAIN,
"cannot release hold from snapshot '%s'"),
nvpair_name(elem));
switch (fnvpair_value_int32(elem)) {
case ESRCH:
(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
break;
case EINVAL:
(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
break;
default:
(void) zfs_standard_error_fmt(hdl,
fnvpair_value_int32(elem), errbuf);
}
}
fnvlist_free(errors);
return (ret);
}
int
zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
{
zfs_cmd_t zc = { 0 };
libzfs_handle_t *hdl = zhp->zfs_hdl;
int nvsz = 2048;
void *nvbuf;
int err = 0;
char errbuf[1024];
assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
tryagain:
nvbuf = malloc(nvsz);
if (nvbuf == NULL) {
err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
goto out;
}
zc.zc_nvlist_dst_size = nvsz;
zc.zc_nvlist_dst = (uintptr_t)nvbuf;
(void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
(void) snprintf(errbuf, sizeof (errbuf),
dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
zc.zc_name);
switch (errno) {
case ENOMEM:
free(nvbuf);
nvsz = zc.zc_nvlist_dst_size;
goto tryagain;
case ENOTSUP:
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"pool must be upgraded"));
err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
break;
case EINVAL:
err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
break;
case ENOENT:
err = zfs_error(hdl, EZFS_NOENT, errbuf);
break;
default:
err = zfs_standard_error_fmt(hdl, errno, errbuf);
break;
}
} else {
/* success */
int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
if (rc) {
(void) snprintf(errbuf, sizeof (errbuf), dgettext(
TEXT_DOMAIN, "cannot get permissions on '%s'"),
zc.zc_name);
err = zfs_standard_error_fmt(hdl, rc, errbuf);
}
}
free(nvbuf);
out:
return (err);
}
int
zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
{
zfs_cmd_t zc = { 0 };
libzfs_handle_t *hdl = zhp->zfs_hdl;
char *nvbuf;
char errbuf[1024];
size_t nvsz;
int err;
assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
assert(err == 0);
nvbuf = malloc(nvsz);
err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
assert(err == 0);
zc.zc_nvlist_src_size = nvsz;
zc.zc_nvlist_src = (uintptr_t)nvbuf;
zc.zc_perm_action = un;
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
(void) snprintf(errbuf, sizeof (errbuf),
dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
zc.zc_name);
switch (errno) {
case ENOTSUP:
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"pool must be upgraded"));
err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
break;
case EINVAL:
err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
break;
case ENOENT:
err = zfs_error(hdl, EZFS_NOENT, errbuf);
break;
default:
err = zfs_standard_error_fmt(hdl, errno, errbuf);
break;
}
}
free(nvbuf);
return (err);
}
int
zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
{
int err;
char errbuf[1024];
err = lzc_get_holds(zhp->zfs_name, nvl);
if (err != 0) {
libzfs_handle_t *hdl = zhp->zfs_hdl;
(void) snprintf(errbuf, sizeof (errbuf),
dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
zhp->zfs_name);
switch (err) {
case ENOTSUP:
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"pool must be upgraded"));
err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
break;
case EINVAL:
err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
break;
case ENOENT:
err = zfs_error(hdl, EZFS_NOENT, errbuf);
break;
default:
err = zfs_standard_error_fmt(hdl, errno, errbuf);
break;
}
}
return (err);
}
/*
* Convert the zvol's volume size to an appropriate reservation.
* Note: If this routine is updated, it is necessary to update the ZFS test
* suite's shell version in reservation.kshlib.
*/
uint64_t
zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
{
uint64_t numdb;
uint64_t nblocks, volblocksize;
int ncopies;
char *strval;
if (nvlist_lookup_string(props,
zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
ncopies = atoi(strval);
else
ncopies = 1;
if (nvlist_lookup_uint64(props,
zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
&volblocksize) != 0)
volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
nblocks = volsize/volblocksize;
/* start with metadnode L0-L6 */
numdb = 7;
/* calculate number of indirects */
while (nblocks > 1) {
nblocks += DNODES_PER_LEVEL - 1;
nblocks /= DNODES_PER_LEVEL;
numdb += nblocks;
}
numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
volsize *= ncopies;
/*
* this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
* compressed, but in practice they compress down to about
* 1100 bytes
*/
numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
volsize += numdb;
return (volsize);
}