Home | History | Annotate | Line # | Download | only in common
      1  1.1  haad /*
      2  1.1  haad  * CDDL HEADER START
      3  1.1  haad  *
      4  1.1  haad  * The contents of this file are subject to the terms of the
      5  1.1  haad  * Common Development and Distribution License (the "License").
      6  1.1  haad  * You may not use this file except in compliance with the License.
      7  1.1  haad  *
      8  1.1  haad  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  1.1  haad  * or http://www.opensolaris.org/os/licensing.
     10  1.1  haad  * See the License for the specific language governing permissions
     11  1.1  haad  * and limitations under the License.
     12  1.1  haad  *
     13  1.1  haad  * When distributing Covered Code, include this CDDL HEADER in each
     14  1.1  haad  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  1.1  haad  * If applicable, add the following below this CDDL HEADER, with the
     16  1.1  haad  * fields enclosed by brackets "[]" replaced with your own identifying
     17  1.1  haad  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  1.1  haad  *
     19  1.1  haad  * CDDL HEADER END
     20  1.1  haad  */
     21  1.1  haad 
     22  1.1  haad /*
     23  1.4   chs  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
     24  1.4   chs  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
     25  1.4   chs  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
     26  1.4   chs  * Copyright (c) 2012 DEY Storage Systems, Inc.  All rights reserved.
     27  1.4   chs  * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
     28  1.4   chs  * Copyright (c) 2013 Martin Matuska. All rights reserved.
     29  1.4   chs  * Copyright (c) 2013 Steven Hartland. All rights reserved.
     30  1.4   chs  * Copyright (c) 2014 Integros [integros.com]
     31  1.4   chs  * Copyright 2016 Nexenta Systems, Inc.
     32  1.4   chs  * Copyright 2016 Igor Kozhukhov <ikozhukhov (at) gmail.com>
     33  1.1  haad  */
     34  1.1  haad 
     35  1.1  haad #include <ctype.h>
     36  1.1  haad #include <errno.h>
     37  1.1  haad #include <libintl.h>
     38  1.1  haad #include <math.h>
     39  1.1  haad #include <stdio.h>
     40  1.1  haad #include <stdlib.h>
     41  1.1  haad #include <strings.h>
     42  1.1  haad #include <unistd.h>
     43  1.1  haad #include <stddef.h>
     44  1.1  haad #include <zone.h>
     45  1.1  haad #include <fcntl.h>
     46  1.1  haad #include <sys/mntent.h>
     47  1.1  haad #include <sys/mount.h>
     48  1.1  haad #include <priv.h>
     49  1.1  haad #include <pwd.h>
     50  1.1  haad #include <grp.h>
     51  1.1  haad #include <stddef.h>
     52  1.2  haad #include <idmap.h>
     53  1.1  haad 
     54  1.2  haad #include <sys/dnode.h>
     55  1.1  haad #include <sys/spa.h>
     56  1.1  haad #include <sys/zap.h>
     57  1.4   chs #include <sys/misc.h>
     58  1.1  haad #include <libzfs.h>
     59  1.1  haad 
     60  1.1  haad #include "zfs_namecheck.h"
     61  1.1  haad #include "zfs_prop.h"
     62  1.1  haad #include "libzfs_impl.h"
     63  1.1  haad #include "zfs_deleg.h"
     64  1.1  haad 
     65  1.2  haad static int userquota_propname_decode(const char *propname, boolean_t zoned,
     66  1.2  haad     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
     67  1.1  haad 
     68  1.1  haad /*
     69  1.1  haad  * Given a single type (not a mask of types), return the type in a human
     70  1.1  haad  * readable form.
     71  1.1  haad  */
     72  1.1  haad const char *
     73  1.1  haad zfs_type_to_name(zfs_type_t type)
     74  1.1  haad {
     75  1.1  haad 	switch (type) {
     76  1.1  haad 	case ZFS_TYPE_FILESYSTEM:
     77  1.1  haad 		return (dgettext(TEXT_DOMAIN, "filesystem"));
     78  1.1  haad 	case ZFS_TYPE_SNAPSHOT:
     79  1.1  haad 		return (dgettext(TEXT_DOMAIN, "snapshot"));
     80  1.1  haad 	case ZFS_TYPE_VOLUME:
     81  1.1  haad 		return (dgettext(TEXT_DOMAIN, "volume"));
     82  1.4   chs 	case ZFS_TYPE_POOL:
     83  1.4   chs 		return (dgettext(TEXT_DOMAIN, "pool"));
     84  1.4   chs 	case ZFS_TYPE_BOOKMARK:
     85  1.4   chs 		return (dgettext(TEXT_DOMAIN, "bookmark"));
     86  1.4   chs 	default:
     87  1.4   chs 		assert(!"unhandled zfs_type_t");
     88  1.1  haad 	}
     89  1.1  haad 
     90  1.1  haad 	return (NULL);
     91  1.1  haad }
     92  1.1  haad 
     93  1.1  haad /*
     94  1.1  haad  * Validate a ZFS path.  This is used even before trying to open the dataset, to
     95  1.2  haad  * provide a more meaningful error message.  We call zfs_error_aux() to
     96  1.2  haad  * explain exactly why the name was not valid.
     97  1.1  haad  */
     98  1.4   chs int
     99  1.1  haad zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
    100  1.1  haad     boolean_t modifying)
    101  1.1  haad {
    102  1.1  haad 	namecheck_err_t why;
    103  1.1  haad 	char what;
    104  1.1  haad 
    105  1.4   chs 	(void) zfs_prop_get_table();
    106  1.1  haad 	if (dataset_namecheck(path, &why, &what) != 0) {
    107  1.1  haad 		if (hdl != NULL) {
    108  1.1  haad 			switch (why) {
    109  1.1  haad 			case NAME_ERR_TOOLONG:
    110  1.1  haad 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
    111  1.1  haad 				    "name is too long"));
    112  1.1  haad 				break;
    113  1.1  haad 
    114  1.1  haad 			case NAME_ERR_LEADING_SLASH:
    115  1.1  haad 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
    116  1.1  haad 				    "leading slash in name"));
    117  1.1  haad 				break;
    118  1.1  haad 
    119  1.1  haad 			case NAME_ERR_EMPTY_COMPONENT:
    120  1.1  haad 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
    121  1.1  haad 				    "empty component in name"));
    122  1.1  haad 				break;
    123  1.1  haad 
    124  1.1  haad 			case NAME_ERR_TRAILING_SLASH:
    125  1.1  haad 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
    126  1.1  haad 				    "trailing slash in name"));
    127  1.1  haad 				break;
    128  1.1  haad 
    129  1.1  haad 			case NAME_ERR_INVALCHAR:
    130  1.1  haad 				zfs_error_aux(hdl,
    131  1.1  haad 				    dgettext(TEXT_DOMAIN, "invalid character "
    132  1.1  haad 				    "'%c' in name"), what);
    133  1.1  haad 				break;
    134  1.1  haad 
    135  1.1  haad 			case NAME_ERR_MULTIPLE_AT:
    136  1.1  haad 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
    137  1.1  haad 				    "multiple '@' delimiters in name"));
    138  1.1  haad 				break;
    139  1.1  haad 
    140  1.1  haad 			case NAME_ERR_NOLETTER:
    141  1.1  haad 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
    142  1.1  haad 				    "pool doesn't begin with a letter"));
    143  1.1  haad 				break;
    144  1.1  haad 
    145  1.1  haad 			case NAME_ERR_RESERVED:
    146  1.1  haad 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
    147  1.1  haad 				    "name is reserved"));
    148  1.1  haad 				break;
    149  1.1  haad 
    150  1.1  haad 			case NAME_ERR_DISKLIKE:
    151  1.1  haad 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
    152  1.1  haad 				    "reserved disk name"));
    153  1.1  haad 				break;
    154  1.4   chs 
    155  1.4   chs 			default:
    156  1.4   chs 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
    157  1.4   chs 				    "(%d) not defined"), why);
    158  1.4   chs 				break;
    159  1.1  haad 			}
    160  1.1  haad 		}
    161  1.1  haad 
    162  1.1  haad 		return (0);
    163  1.1  haad 	}
    164  1.1  haad 
    165  1.1  haad 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
    166  1.1  haad 		if (hdl != NULL)
    167  1.1  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
    168  1.1  haad 			    "snapshot delimiter '@' in filesystem name"));
    169  1.1  haad 		return (0);
    170  1.1  haad 	}
    171  1.1  haad 
    172  1.1  haad 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
    173  1.1  haad 		if (hdl != NULL)
    174  1.1  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
    175  1.1  haad 			    "missing '@' delimiter in snapshot name"));
    176  1.1  haad 		return (0);
    177  1.1  haad 	}
    178  1.1  haad 
    179  1.1  haad 	if (modifying && strchr(path, '%') != NULL) {
    180  1.1  haad 		if (hdl != NULL)
    181  1.1  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
    182  1.1  haad 			    "invalid character %c in name"), '%');
    183  1.1  haad 		return (0);
    184  1.1  haad 	}
    185  1.1  haad 
    186  1.1  haad 	return (-1);
    187  1.1  haad }
    188  1.1  haad 
    189  1.1  haad int
    190  1.1  haad zfs_name_valid(const char *name, zfs_type_t type)
    191  1.1  haad {
    192  1.1  haad 	if (type == ZFS_TYPE_POOL)
    193  1.1  haad 		return (zpool_name_valid(NULL, B_FALSE, name));
    194  1.1  haad 	return (zfs_validate_name(NULL, name, type, B_FALSE));
    195  1.1  haad }
    196  1.1  haad 
    197  1.1  haad /*
    198  1.1  haad  * This function takes the raw DSL properties, and filters out the user-defined
    199  1.1  haad  * properties into a separate nvlist.
    200  1.1  haad  */
    201  1.1  haad static nvlist_t *
    202  1.1  haad process_user_props(zfs_handle_t *zhp, nvlist_t *props)
    203  1.1  haad {
    204  1.1  haad 	libzfs_handle_t *hdl = zhp->zfs_hdl;
    205  1.1  haad 	nvpair_t *elem;
    206  1.1  haad 	nvlist_t *propval;
    207  1.1  haad 	nvlist_t *nvl;
    208  1.1  haad 
    209  1.1  haad 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
    210  1.1  haad 		(void) no_memory(hdl);
    211  1.1  haad 		return (NULL);
    212  1.1  haad 	}
    213  1.1  haad 
    214  1.1  haad 	elem = NULL;
    215  1.1  haad 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
    216  1.1  haad 		if (!zfs_prop_user(nvpair_name(elem)))
    217  1.1  haad 			continue;
    218  1.1  haad 
    219  1.1  haad 		verify(nvpair_value_nvlist(elem, &propval) == 0);
    220  1.1  haad 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
    221  1.1  haad 			nvlist_free(nvl);
    222  1.1  haad 			(void) no_memory(hdl);
    223  1.1  haad 			return (NULL);
    224  1.1  haad 		}
    225  1.1  haad 	}
    226  1.1  haad 
    227  1.1  haad 	return (nvl);
    228  1.1  haad }
    229  1.1  haad 
    230  1.1  haad static zpool_handle_t *
    231  1.1  haad zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
    232  1.1  haad {
    233  1.1  haad 	libzfs_handle_t *hdl = zhp->zfs_hdl;
    234  1.1  haad 	zpool_handle_t *zph;
    235  1.1  haad 
    236  1.1  haad 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
    237  1.1  haad 		if (hdl->libzfs_pool_handles != NULL)
    238  1.1  haad 			zph->zpool_next = hdl->libzfs_pool_handles;
    239  1.1  haad 		hdl->libzfs_pool_handles = zph;
    240  1.1  haad 	}
    241  1.1  haad 	return (zph);
    242  1.1  haad }
    243  1.1  haad 
    244  1.1  haad static zpool_handle_t *
    245  1.1  haad zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
    246  1.1  haad {
    247  1.1  haad 	libzfs_handle_t *hdl = zhp->zfs_hdl;
    248  1.1  haad 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
    249  1.1  haad 
    250  1.1  haad 	while ((zph != NULL) &&
    251  1.1  haad 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
    252  1.1  haad 		zph = zph->zpool_next;
    253  1.1  haad 	return (zph);
    254  1.1  haad }
    255  1.1  haad 
    256  1.1  haad /*
    257  1.1  haad  * Returns a handle to the pool that contains the provided dataset.
    258  1.1  haad  * If a handle to that pool already exists then that handle is returned.
    259  1.1  haad  * Otherwise, a new handle is created and added to the list of handles.
    260  1.1  haad  */
    261  1.1  haad static zpool_handle_t *
    262  1.1  haad zpool_handle(zfs_handle_t *zhp)
    263  1.1  haad {
    264  1.1  haad 	char *pool_name;
    265  1.1  haad 	int len;
    266  1.1  haad 	zpool_handle_t *zph;
    267  1.1  haad 
    268  1.4   chs 	len = strcspn(zhp->zfs_name, "/@#") + 1;
    269  1.1  haad 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
    270  1.1  haad 	(void) strlcpy(pool_name, zhp->zfs_name, len);
    271  1.1  haad 
    272  1.1  haad 	zph = zpool_find_handle(zhp, pool_name, len);
    273  1.1  haad 	if (zph == NULL)
    274  1.1  haad 		zph = zpool_add_handle(zhp, pool_name);
    275  1.1  haad 
    276  1.1  haad 	free(pool_name);
    277  1.1  haad 	return (zph);
    278  1.1  haad }
    279  1.1  haad 
    280  1.1  haad void
    281  1.1  haad zpool_free_handles(libzfs_handle_t *hdl)
    282  1.1  haad {
    283  1.1  haad 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
    284  1.1  haad 
    285  1.1  haad 	while (zph != NULL) {
    286  1.1  haad 		next = zph->zpool_next;
    287  1.1  haad 		zpool_close(zph);
    288  1.1  haad 		zph = next;
    289  1.1  haad 	}
    290  1.1  haad 	hdl->libzfs_pool_handles = NULL;
    291  1.1  haad }
    292  1.1  haad 
    293  1.1  haad /*
    294  1.1  haad  * Utility function to gather stats (objset and zpl) for the given object.
    295  1.1  haad  */
    296  1.1  haad static int
    297  1.2  haad get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
    298  1.1  haad {
    299  1.1  haad 	libzfs_handle_t *hdl = zhp->zfs_hdl;
    300  1.1  haad 
    301  1.2  haad 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
    302  1.2  haad 
    303  1.2  haad 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
    304  1.2  haad 		if (errno == ENOMEM) {
    305  1.2  haad 			if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
    306  1.2  haad 				return (-1);
    307  1.2  haad 			}
    308  1.2  haad 		} else {
    309  1.2  haad 			return (-1);
    310  1.2  haad 		}
    311  1.2  haad 	}
    312  1.2  haad 	return (0);
    313  1.2  haad }
    314  1.2  haad 
    315  1.2  haad /*
    316  1.2  haad  * Utility function to get the received properties of the given object.
    317  1.2  haad  */
    318  1.2  haad static int
    319  1.2  haad get_recvd_props_ioctl(zfs_handle_t *zhp)
    320  1.2  haad {
    321  1.2  haad 	libzfs_handle_t *hdl = zhp->zfs_hdl;
    322  1.2  haad 	nvlist_t *recvdprops;
    323  1.2  haad 	zfs_cmd_t zc = { 0 };
    324  1.2  haad 	int err;
    325  1.1  haad 
    326  1.1  haad 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
    327  1.1  haad 		return (-1);
    328  1.1  haad 
    329  1.2  haad 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
    330  1.2  haad 
    331  1.2  haad 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
    332  1.1  haad 		if (errno == ENOMEM) {
    333  1.1  haad 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
    334  1.1  haad 				return (-1);
    335  1.1  haad 			}
    336  1.1  haad 		} else {
    337  1.1  haad 			zcmd_free_nvlists(&zc);
    338  1.1  haad 			return (-1);
    339  1.1  haad 		}
    340  1.1  haad 	}
    341  1.1  haad 
    342  1.2  haad 	err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
    343  1.2  haad 	zcmd_free_nvlists(&zc);
    344  1.2  haad 	if (err != 0)
    345  1.2  haad 		return (-1);
    346  1.2  haad 
    347  1.2  haad 	nvlist_free(zhp->zfs_recvd_props);
    348  1.2  haad 	zhp->zfs_recvd_props = recvdprops;
    349  1.2  haad 
    350  1.2  haad 	return (0);
    351  1.2  haad }
    352  1.2  haad 
    353  1.2  haad static int
    354  1.2  haad put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
    355  1.2  haad {
    356  1.2  haad 	nvlist_t *allprops, *userprops;
    357  1.2  haad 
    358  1.2  haad 	zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
    359  1.1  haad 
    360  1.2  haad 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
    361  1.1  haad 		return (-1);
    362  1.1  haad 	}
    363  1.1  haad 
    364  1.2  haad 	/*
    365  1.2  haad 	 * XXX Why do we store the user props separately, in addition to
    366  1.2  haad 	 * storing them in zfs_props?
    367  1.2  haad 	 */
    368  1.1  haad 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
    369  1.1  haad 		nvlist_free(allprops);
    370  1.1  haad 		return (-1);
    371  1.1  haad 	}
    372  1.1  haad 
    373  1.1  haad 	nvlist_free(zhp->zfs_props);
    374  1.1  haad 	nvlist_free(zhp->zfs_user_props);
    375  1.1  haad 
    376  1.1  haad 	zhp->zfs_props = allprops;
    377  1.1  haad 	zhp->zfs_user_props = userprops;
    378  1.1  haad 
    379  1.1  haad 	return (0);
    380  1.1  haad }
    381  1.1  haad 
    382  1.2  haad static int
    383  1.2  haad get_stats(zfs_handle_t *zhp)
    384  1.2  haad {
    385  1.2  haad 	int rc = 0;
    386  1.2  haad 	zfs_cmd_t zc = { 0 };
    387  1.2  haad 
    388  1.2  haad 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
    389  1.2  haad 		return (-1);
    390  1.2  haad 	if (get_stats_ioctl(zhp, &zc) != 0)
    391  1.2  haad 		rc = -1;
    392  1.2  haad 	else if (put_stats_zhdl(zhp, &zc) != 0)
    393  1.2  haad 		rc = -1;
    394  1.2  haad 	zcmd_free_nvlists(&zc);
    395  1.2  haad 	return (rc);
    396  1.2  haad }
    397  1.2  haad 
    398  1.1  haad /*
    399  1.1  haad  * Refresh the properties currently stored in the handle.
    400  1.1  haad  */
    401  1.1  haad void
    402  1.1  haad zfs_refresh_properties(zfs_handle_t *zhp)
    403  1.1  haad {
    404  1.1  haad 	(void) get_stats(zhp);
    405  1.1  haad }
    406  1.1  haad 
    407  1.1  haad /*
    408  1.1  haad  * Makes a handle from the given dataset name.  Used by zfs_open() and
    409  1.1  haad  * zfs_iter_* to create child handles on the fly.
    410  1.1  haad  */
    411  1.2  haad static int
    412  1.2  haad make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
    413  1.1  haad {
    414  1.2  haad 	if (put_stats_zhdl(zhp, zc) != 0)
    415  1.2  haad 		return (-1);
    416  1.1  haad 
    417  1.1  haad 	/*
    418  1.1  haad 	 * We've managed to open the dataset and gather statistics.  Determine
    419  1.1  haad 	 * the high-level type.
    420  1.1  haad 	 */
    421  1.1  haad 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
    422  1.1  haad 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
    423  1.1  haad 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
    424  1.1  haad 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
    425  1.1  haad 	else
    426  1.1  haad 		abort();
    427  1.1  haad 
    428  1.1  haad 	if (zhp->zfs_dmustats.dds_is_snapshot)
    429  1.1  haad 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
    430  1.1  haad 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
    431  1.1  haad 		zhp->zfs_type = ZFS_TYPE_VOLUME;
    432  1.1  haad 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
    433  1.1  haad 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
    434  1.1  haad 	else
    435  1.1  haad 		abort();	/* we should never see any other types */
    436  1.1  haad 
    437  1.4   chs 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
    438  1.4   chs 		return (-1);
    439  1.4   chs 
    440  1.2  haad 	return (0);
    441  1.2  haad }
    442  1.2  haad 
    443  1.2  haad zfs_handle_t *
    444  1.2  haad make_dataset_handle(libzfs_handle_t *hdl, const char *path)
    445  1.2  haad {
    446  1.2  haad 	zfs_cmd_t zc = { 0 };
    447  1.2  haad 
    448  1.2  haad 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
    449  1.2  haad 
    450  1.2  haad 	if (zhp == NULL)
    451  1.2  haad 		return (NULL);
    452  1.2  haad 
    453  1.2  haad 	zhp->zfs_hdl = hdl;
    454  1.2  haad 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
    455  1.2  haad 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
    456  1.2  haad 		free(zhp);
    457  1.2  haad 		return (NULL);
    458  1.2  haad 	}
    459  1.2  haad 	if (get_stats_ioctl(zhp, &zc) == -1) {
    460  1.2  haad 		zcmd_free_nvlists(&zc);
    461  1.2  haad 		free(zhp);
    462  1.2  haad 		return (NULL);
    463  1.2  haad 	}
    464  1.2  haad 	if (make_dataset_handle_common(zhp, &zc) == -1) {
    465  1.2  haad 		free(zhp);
    466  1.2  haad 		zhp = NULL;
    467  1.2  haad 	}
    468  1.2  haad 	zcmd_free_nvlists(&zc);
    469  1.2  haad 	return (zhp);
    470  1.2  haad }
    471  1.2  haad 
    472  1.4   chs zfs_handle_t *
    473  1.2  haad make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
    474  1.2  haad {
    475  1.2  haad 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
    476  1.2  haad 
    477  1.2  haad 	if (zhp == NULL)
    478  1.2  haad 		return (NULL);
    479  1.2  haad 
    480  1.2  haad 	zhp->zfs_hdl = hdl;
    481  1.2  haad 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
    482  1.2  haad 	if (make_dataset_handle_common(zhp, zc) == -1) {
    483  1.2  haad 		free(zhp);
    484  1.2  haad 		return (NULL);
    485  1.2  haad 	}
    486  1.1  haad 	return (zhp);
    487  1.1  haad }
    488  1.1  haad 
    489  1.4   chs zfs_handle_t *
    490  1.4   chs make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
    491  1.4   chs {
    492  1.4   chs 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
    493  1.4   chs 
    494  1.4   chs 	if (zhp == NULL)
    495  1.4   chs 		return (NULL);
    496  1.4   chs 
    497  1.4   chs 	zhp->zfs_hdl = pzhp->zfs_hdl;
    498  1.4   chs 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
    499  1.4   chs 	zhp->zfs_head_type = pzhp->zfs_type;
    500  1.4   chs 	zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
    501  1.4   chs 	zhp->zpool_hdl = zpool_handle(zhp);
    502  1.4   chs 	return (zhp);
    503  1.4   chs }
    504  1.4   chs 
    505  1.4   chs zfs_handle_t *
    506  1.4   chs zfs_handle_dup(zfs_handle_t *zhp_orig)
    507  1.4   chs {
    508  1.4   chs 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
    509  1.4   chs 
    510  1.4   chs 	if (zhp == NULL)
    511  1.4   chs 		return (NULL);
    512  1.4   chs 
    513  1.4   chs 	zhp->zfs_hdl = zhp_orig->zfs_hdl;
    514  1.4   chs 	zhp->zpool_hdl = zhp_orig->zpool_hdl;
    515  1.4   chs 	(void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
    516  1.4   chs 	    sizeof (zhp->zfs_name));
    517  1.4   chs 	zhp->zfs_type = zhp_orig->zfs_type;
    518  1.4   chs 	zhp->zfs_head_type = zhp_orig->zfs_head_type;
    519  1.4   chs 	zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
    520  1.4   chs 	if (zhp_orig->zfs_props != NULL) {
    521  1.4   chs 		if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
    522  1.4   chs 			(void) no_memory(zhp->zfs_hdl);
    523  1.4   chs 			zfs_close(zhp);
    524  1.4   chs 			return (NULL);
    525  1.4   chs 		}
    526  1.4   chs 	}
    527  1.4   chs 	if (zhp_orig->zfs_user_props != NULL) {
    528  1.4   chs 		if (nvlist_dup(zhp_orig->zfs_user_props,
    529  1.4   chs 		    &zhp->zfs_user_props, 0) != 0) {
    530  1.4   chs 			(void) no_memory(zhp->zfs_hdl);
    531  1.4   chs 			zfs_close(zhp);
    532  1.4   chs 			return (NULL);
    533  1.4   chs 		}
    534  1.4   chs 	}
    535  1.4   chs 	if (zhp_orig->zfs_recvd_props != NULL) {
    536  1.4   chs 		if (nvlist_dup(zhp_orig->zfs_recvd_props,
    537  1.4   chs 		    &zhp->zfs_recvd_props, 0)) {
    538  1.4   chs 			(void) no_memory(zhp->zfs_hdl);
    539  1.4   chs 			zfs_close(zhp);
    540  1.4   chs 			return (NULL);
    541  1.4   chs 		}
    542  1.4   chs 	}
    543  1.4   chs 	zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
    544  1.4   chs 	if (zhp_orig->zfs_mntopts != NULL) {
    545  1.4   chs 		zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
    546  1.4   chs 		    zhp_orig->zfs_mntopts);
    547  1.4   chs 	}
    548  1.4   chs 	zhp->zfs_props_table = zhp_orig->zfs_props_table;
    549  1.4   chs 	return (zhp);
    550  1.4   chs }
    551  1.4   chs 
    552  1.4   chs boolean_t
    553  1.4   chs zfs_bookmark_exists(const char *path)
    554  1.4   chs {
    555  1.4   chs 	nvlist_t *bmarks;
    556  1.4   chs 	nvlist_t *props;
    557  1.4   chs 	char fsname[ZFS_MAX_DATASET_NAME_LEN];
    558  1.4   chs 	char *bmark_name;
    559  1.4   chs 	char *pound;
    560  1.4   chs 	int err;
    561  1.4   chs 	boolean_t rv;
    562  1.4   chs 
    563  1.4   chs 
    564  1.4   chs 	(void) strlcpy(fsname, path, sizeof (fsname));
    565  1.4   chs 	pound = strchr(fsname, '#');
    566  1.4   chs 	if (pound == NULL)
    567  1.4   chs 		return (B_FALSE);
    568  1.4   chs 
    569  1.4   chs 	*pound = '\0';
    570  1.4   chs 	bmark_name = pound + 1;
    571  1.4   chs 	props = fnvlist_alloc();
    572  1.4   chs 	err = lzc_get_bookmarks(fsname, props, &bmarks);
    573  1.4   chs 	nvlist_free(props);
    574  1.4   chs 	if (err != 0) {
    575  1.4   chs 		nvlist_free(bmarks);
    576  1.4   chs 		return (B_FALSE);
    577  1.4   chs 	}
    578  1.4   chs 
    579  1.4   chs 	rv = nvlist_exists(bmarks, bmark_name);
    580  1.4   chs 	nvlist_free(bmarks);
    581  1.4   chs 	return (rv);
    582  1.4   chs }
    583  1.4   chs 
    584  1.4   chs zfs_handle_t *
    585  1.4   chs make_bookmark_handle(zfs_handle_t *parent, const char *path,
    586  1.4   chs     nvlist_t *bmark_props)
    587  1.4   chs {
    588  1.4   chs 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
    589  1.4   chs 
    590  1.4   chs 	if (zhp == NULL)
    591  1.4   chs 		return (NULL);
    592  1.4   chs 
    593  1.4   chs 	/* Fill in the name. */
    594  1.4   chs 	zhp->zfs_hdl = parent->zfs_hdl;
    595  1.4   chs 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
    596  1.4   chs 
    597  1.4   chs 	/* Set the property lists. */
    598  1.4   chs 	if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
    599  1.4   chs 		free(zhp);
    600  1.4   chs 		return (NULL);
    601  1.4   chs 	}
    602  1.4   chs 
    603  1.4   chs 	/* Set the types. */
    604  1.4   chs 	zhp->zfs_head_type = parent->zfs_head_type;
    605  1.4   chs 	zhp->zfs_type = ZFS_TYPE_BOOKMARK;
    606  1.4   chs 
    607  1.4   chs 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
    608  1.4   chs 		nvlist_free(zhp->zfs_props);
    609  1.4   chs 		free(zhp);
    610  1.4   chs 		return (NULL);
    611  1.4   chs 	}
    612  1.4   chs 
    613  1.4   chs 	return (zhp);
    614  1.4   chs }
    615  1.4   chs 
    616  1.1  haad /*
    617  1.1  haad  * Opens the given snapshot, filesystem, or volume.   The 'types'
    618  1.1  haad  * argument is a mask of acceptable types.  The function will print an
    619  1.1  haad  * appropriate error message and return NULL if it can't be opened.
    620  1.1  haad  */
    621  1.1  haad zfs_handle_t *
    622  1.1  haad zfs_open(libzfs_handle_t *hdl, const char *path, int types)
    623  1.1  haad {
    624  1.1  haad 	zfs_handle_t *zhp;
    625  1.1  haad 	char errbuf[1024];
    626  1.1  haad 
    627  1.1  haad 	(void) snprintf(errbuf, sizeof (errbuf),
    628  1.1  haad 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
    629  1.1  haad 
    630  1.1  haad 	/*
    631  1.1  haad 	 * Validate the name before we even try to open it.
    632  1.1  haad 	 */
    633  1.1  haad 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
    634  1.1  haad 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
    635  1.1  haad 		    "invalid dataset name"));
    636  1.1  haad 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
    637  1.1  haad 		return (NULL);
    638  1.1  haad 	}
    639  1.1  haad 
    640  1.1  haad 	/*
    641  1.1  haad 	 * Try to get stats for the dataset, which will tell us if it exists.
    642  1.1  haad 	 */
    643  1.1  haad 	errno = 0;
    644  1.1  haad 	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
    645  1.1  haad 		(void) zfs_standard_error(hdl, errno, errbuf);
    646  1.1  haad 		return (NULL);
    647  1.1  haad 	}
    648  1.1  haad 
    649  1.4   chs 	if (zhp == NULL) {
    650  1.4   chs 		char *at = strchr(path, '@');
    651  1.4   chs 
    652  1.4   chs 		if (at != NULL)
    653  1.4   chs 			*at = '\0';
    654  1.4   chs 		errno = 0;
    655  1.4   chs 		if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
    656  1.4   chs 			(void) zfs_standard_error(hdl, errno, errbuf);
    657  1.4   chs 			return (NULL);
    658  1.4   chs 		}
    659  1.4   chs 		if (at != NULL)
    660  1.4   chs 			*at = '@';
    661  1.4   chs 		(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
    662  1.4   chs 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
    663  1.4   chs 	}
    664  1.4   chs 
    665  1.1  haad 	if (!(types & zhp->zfs_type)) {
    666  1.1  haad 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
    667  1.1  haad 		zfs_close(zhp);
    668  1.1  haad 		return (NULL);
    669  1.1  haad 	}
    670  1.1  haad 
    671  1.1  haad 	return (zhp);
    672  1.1  haad }
    673  1.1  haad 
    674  1.1  haad /*
    675  1.1  haad  * Release a ZFS handle.  Nothing to do but free the associated memory.
    676  1.1  haad  */
    677  1.1  haad void
    678  1.1  haad zfs_close(zfs_handle_t *zhp)
    679  1.1  haad {
    680  1.1  haad 	if (zhp->zfs_mntopts)
    681  1.1  haad 		free(zhp->zfs_mntopts);
    682  1.1  haad 	nvlist_free(zhp->zfs_props);
    683  1.1  haad 	nvlist_free(zhp->zfs_user_props);
    684  1.2  haad 	nvlist_free(zhp->zfs_recvd_props);
    685  1.1  haad 	free(zhp);
    686  1.1  haad }
    687  1.1  haad 
    688  1.2  haad typedef struct mnttab_node {
    689  1.2  haad 	struct mnttab mtn_mt;
    690  1.2  haad 	avl_node_t mtn_node;
    691  1.2  haad } mnttab_node_t;
    692  1.2  haad 
    693  1.2  haad static int
    694  1.2  haad libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
    695  1.2  haad {
    696  1.2  haad 	const mnttab_node_t *mtn1 = arg1;
    697  1.2  haad 	const mnttab_node_t *mtn2 = arg2;
    698  1.2  haad 	int rv;
    699  1.2  haad 
    700  1.2  haad 	rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
    701  1.2  haad 
    702  1.2  haad 	if (rv == 0)
    703  1.2  haad 		return (0);
    704  1.2  haad 	return (rv > 0 ? 1 : -1);
    705  1.2  haad }
    706  1.2  haad 
    707  1.2  haad void
    708  1.2  haad libzfs_mnttab_init(libzfs_handle_t *hdl)
    709  1.2  haad {
    710  1.2  haad 	assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
    711  1.2  haad 	avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
    712  1.2  haad 	    sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
    713  1.2  haad }
    714  1.2  haad 
    715  1.4   chs static void
    716  1.2  haad libzfs_mnttab_update(libzfs_handle_t *hdl)
    717  1.2  haad {
    718  1.2  haad 	struct mnttab entry;
    719  1.2  haad 
    720  1.2  haad 	rewind(hdl->libzfs_mnttab);
    721  1.2  haad 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
    722  1.2  haad 		mnttab_node_t *mtn;
    723  1.2  haad 
    724  1.2  haad 		if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
    725  1.2  haad 			continue;
    726  1.2  haad 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
    727  1.2  haad 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
    728  1.2  haad 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
    729  1.2  haad 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
    730  1.2  haad 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
    731  1.2  haad 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
    732  1.2  haad 	}
    733  1.2  haad }
    734  1.2  haad 
    735  1.2  haad void
    736  1.2  haad libzfs_mnttab_fini(libzfs_handle_t *hdl)
    737  1.2  haad {
    738  1.2  haad 	void *cookie = NULL;
    739  1.2  haad 	mnttab_node_t *mtn;
    740  1.2  haad 
    741  1.4   chs 	while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
    742  1.4   chs 	    != NULL) {
    743  1.2  haad 		free(mtn->mtn_mt.mnt_special);
    744  1.2  haad 		free(mtn->mtn_mt.mnt_mountp);
    745  1.2  haad 		free(mtn->mtn_mt.mnt_fstype);
    746  1.2  haad 		free(mtn->mtn_mt.mnt_mntopts);
    747  1.2  haad 		free(mtn);
    748  1.2  haad 	}
    749  1.2  haad 	avl_destroy(&hdl->libzfs_mnttab_cache);
    750  1.2  haad }
    751  1.2  haad 
    752  1.2  haad void
    753  1.2  haad libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
    754  1.2  haad {
    755  1.2  haad 	hdl->libzfs_mnttab_enable = enable;
    756  1.2  haad }
    757  1.2  haad 
    758  1.2  haad int
    759  1.2  haad libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
    760  1.2  haad     struct mnttab *entry)
    761  1.2  haad {
    762  1.2  haad 	mnttab_node_t find;
    763  1.2  haad 	mnttab_node_t *mtn;
    764  1.2  haad 
    765  1.2  haad 	if (!hdl->libzfs_mnttab_enable) {
    766  1.2  haad 		struct mnttab srch = { 0 };
    767  1.2  haad 
    768  1.2  haad 		if (avl_numnodes(&hdl->libzfs_mnttab_cache))
    769  1.2  haad 			libzfs_mnttab_fini(hdl);
    770  1.2  haad 		rewind(hdl->libzfs_mnttab);
    771  1.2  haad 		srch.mnt_special = (char *)fsname;
    772  1.2  haad 		srch.mnt_fstype = MNTTYPE_ZFS;
    773  1.2  haad 		if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
    774  1.2  haad 			return (0);
    775  1.2  haad 		else
    776  1.2  haad 			return (ENOENT);
    777  1.2  haad 	}
    778  1.2  haad 
    779  1.2  haad 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
    780  1.2  haad 		libzfs_mnttab_update(hdl);
    781  1.2  haad 
    782  1.2  haad 	find.mtn_mt.mnt_special = (char *)fsname;
    783  1.2  haad 	mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
    784  1.2  haad 	if (mtn) {
    785  1.2  haad 		*entry = mtn->mtn_mt;
    786  1.2  haad 		return (0);
    787  1.2  haad 	}
    788  1.2  haad 	return (ENOENT);
    789  1.2  haad }
    790  1.2  haad 
    791  1.2  haad void
    792  1.2  haad libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
    793  1.2  haad     const char *mountp, const char *mntopts)
    794  1.2  haad {
    795  1.2  haad 	mnttab_node_t *mtn;
    796  1.2  haad 
    797  1.2  haad 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
    798  1.2  haad 		return;
    799  1.2  haad 	mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
    800  1.2  haad 	mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
    801  1.2  haad 	mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
    802  1.2  haad 	mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
    803  1.2  haad 	mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
    804  1.2  haad 	avl_add(&hdl->libzfs_mnttab_cache, mtn);
    805  1.2  haad }
    806  1.2  haad 
    807  1.2  haad void
    808  1.2  haad libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
    809  1.2  haad {
    810  1.2  haad 	mnttab_node_t find;
    811  1.2  haad 	mnttab_node_t *ret;
    812  1.2  haad 
    813  1.2  haad 	find.mtn_mt.mnt_special = (char *)fsname;
    814  1.4   chs 	if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
    815  1.4   chs 	    != NULL) {
    816  1.2  haad 		avl_remove(&hdl->libzfs_mnttab_cache, ret);
    817  1.2  haad 		free(ret->mtn_mt.mnt_special);
    818  1.2  haad 		free(ret->mtn_mt.mnt_mountp);
    819  1.2  haad 		free(ret->mtn_mt.mnt_fstype);
    820  1.2  haad 		free(ret->mtn_mt.mnt_mntopts);
    821  1.2  haad 		free(ret);
    822  1.2  haad 	}
    823  1.2  haad }
    824  1.2  haad 
    825  1.1  haad int
    826  1.1  haad zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
    827  1.1  haad {
    828  1.1  haad 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
    829  1.1  haad 
    830  1.1  haad 	if (zpool_handle == NULL)
    831  1.1  haad 		return (-1);
    832  1.1  haad 
    833  1.1  haad 	*spa_version = zpool_get_prop_int(zpool_handle,
    834  1.1  haad 	    ZPOOL_PROP_VERSION, NULL);
    835  1.1  haad 	return (0);
    836  1.1  haad }
    837  1.1  haad 
    838  1.1  haad /*
    839  1.1  haad  * The choice of reservation property depends on the SPA version.
    840  1.1  haad  */
    841  1.1  haad static int
    842  1.1  haad zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
    843  1.1  haad {
    844  1.1  haad 	int spa_version;
    845  1.1  haad 
    846  1.1  haad 	if (zfs_spa_version(zhp, &spa_version) < 0)
    847  1.1  haad 		return (-1);
    848  1.1  haad 
    849  1.1  haad 	if (spa_version >= SPA_VERSION_REFRESERVATION)
    850  1.1  haad 		*resv_prop = ZFS_PROP_REFRESERVATION;
    851  1.1  haad 	else
    852  1.1  haad 		*resv_prop = ZFS_PROP_RESERVATION;
    853  1.1  haad 
    854  1.1  haad 	return (0);
    855  1.1  haad }
    856  1.1  haad 
    857  1.1  haad /*
    858  1.1  haad  * Given an nvlist of properties to set, validates that they are correct, and
    859  1.1  haad  * parses any numeric properties (index, boolean, etc) if they are specified as
    860  1.1  haad  * strings.
    861  1.1  haad  */
    862  1.1  haad nvlist_t *
    863  1.1  haad zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
    864  1.4   chs     uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
    865  1.4   chs     const char *errbuf)
    866  1.1  haad {
    867  1.1  haad 	nvpair_t *elem;
    868  1.1  haad 	uint64_t intval;
    869  1.1  haad 	char *strval;
    870  1.1  haad 	zfs_prop_t prop;
    871  1.1  haad 	nvlist_t *ret;
    872  1.1  haad 	int chosen_normal = -1;
    873  1.1  haad 	int chosen_utf = -1;
    874  1.1  haad 
    875  1.1  haad 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
    876  1.1  haad 		(void) no_memory(hdl);
    877  1.1  haad 		return (NULL);
    878  1.1  haad 	}
    879  1.1  haad 
    880  1.2  haad 	/*
    881  1.2  haad 	 * Make sure this property is valid and applies to this type.
    882  1.2  haad 	 */
    883  1.2  haad 
    884  1.1  haad 	elem = NULL;
    885  1.1  haad 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
    886  1.1  haad 		const char *propname = nvpair_name(elem);
    887  1.1  haad 
    888  1.2  haad 		prop = zfs_name_to_prop(propname);
    889  1.2  haad 		if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
    890  1.1  haad 			/*
    891  1.2  haad 			 * This is a user property: make sure it's a
    892  1.1  haad 			 * string, and that it's less than ZAP_MAXNAMELEN.
    893  1.1  haad 			 */
    894  1.1  haad 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
    895  1.1  haad 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
    896  1.1  haad 				    "'%s' must be a string"), propname);
    897  1.1  haad 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
    898  1.1  haad 				goto error;
    899  1.1  haad 			}
    900  1.1  haad 
    901  1.1  haad 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
    902  1.1  haad 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
    903  1.1  haad 				    "property name '%s' is too long"),
    904  1.1  haad 				    propname);
    905  1.1  haad 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
    906  1.1  haad 				goto error;
    907  1.1  haad 			}
    908  1.1  haad 
    909  1.1  haad 			(void) nvpair_value_string(elem, &strval);
    910  1.1  haad 			if (nvlist_add_string(ret, propname, strval) != 0) {
    911  1.1  haad 				(void) no_memory(hdl);
    912  1.1  haad 				goto error;
    913  1.1  haad 			}
    914  1.1  haad 			continue;
    915  1.1  haad 		}
    916  1.1  haad 
    917  1.2  haad 		/*
    918  1.2  haad 		 * Currently, only user properties can be modified on
    919  1.2  haad 		 * snapshots.
    920  1.2  haad 		 */
    921  1.1  haad 		if (type == ZFS_TYPE_SNAPSHOT) {
    922  1.1  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
    923  1.1  haad 			    "this property can not be modified for snapshots"));
    924  1.1  haad 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
    925  1.1  haad 			goto error;
    926  1.1  haad 		}
    927  1.1  haad 
    928  1.2  haad 		if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
    929  1.2  haad 			zfs_userquota_prop_t uqtype;
    930  1.2  haad 			char newpropname[128];
    931  1.2  haad 			char domain[128];
    932  1.2  haad 			uint64_t rid;
    933  1.2  haad 			uint64_t valary[3];
    934  1.2  haad 
    935  1.2  haad 			if (userquota_propname_decode(propname, zoned,
    936  1.2  haad 			    &uqtype, domain, sizeof (domain), &rid) != 0) {
    937  1.2  haad 				zfs_error_aux(hdl,
    938  1.2  haad 				    dgettext(TEXT_DOMAIN,
    939  1.2  haad 				    "'%s' has an invalid user/group name"),
    940  1.2  haad 				    propname);
    941  1.2  haad 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
    942  1.2  haad 				goto error;
    943  1.2  haad 			}
    944  1.2  haad 
    945  1.2  haad 			if (uqtype != ZFS_PROP_USERQUOTA &&
    946  1.2  haad 			    uqtype != ZFS_PROP_GROUPQUOTA) {
    947  1.2  haad 				zfs_error_aux(hdl,
    948  1.2  haad 				    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
    949  1.2  haad 				    propname);
    950  1.2  haad 				(void) zfs_error(hdl, EZFS_PROPREADONLY,
    951  1.2  haad 				    errbuf);
    952  1.2  haad 				goto error;
    953  1.2  haad 			}
    954  1.2  haad 
    955  1.2  haad 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
    956  1.2  haad 				(void) nvpair_value_string(elem, &strval);
    957  1.2  haad 				if (strcmp(strval, "none") == 0) {
    958  1.2  haad 					intval = 0;
    959  1.2  haad 				} else if (zfs_nicestrtonum(hdl,
    960  1.2  haad 				    strval, &intval) != 0) {
    961  1.2  haad 					(void) zfs_error(hdl,
    962  1.2  haad 					    EZFS_BADPROP, errbuf);
    963  1.2  haad 					goto error;
    964  1.2  haad 				}
    965  1.2  haad 			} else if (nvpair_type(elem) ==
    966  1.2  haad 			    DATA_TYPE_UINT64) {
    967  1.2  haad 				(void) nvpair_value_uint64(elem, &intval);
    968  1.2  haad 				if (intval == 0) {
    969  1.2  haad 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
    970  1.2  haad 					    "use 'none' to disable "
    971  1.2  haad 					    "userquota/groupquota"));
    972  1.2  haad 					goto error;
    973  1.2  haad 				}
    974  1.2  haad 			} else {
    975  1.2  haad 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
    976  1.2  haad 				    "'%s' must be a number"), propname);
    977  1.2  haad 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
    978  1.2  haad 				goto error;
    979  1.2  haad 			}
    980  1.2  haad 
    981  1.2  haad 			/*
    982  1.2  haad 			 * Encode the prop name as
    983  1.2  haad 			 * userquota@<hex-rid>-domain, to make it easy
    984  1.2  haad 			 * for the kernel to decode.
    985  1.2  haad 			 */
    986  1.2  haad 			(void) snprintf(newpropname, sizeof (newpropname),
    987  1.2  haad 			    "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
    988  1.2  haad 			    (longlong_t)rid, domain);
    989  1.2  haad 			valary[0] = uqtype;
    990  1.2  haad 			valary[1] = rid;
    991  1.2  haad 			valary[2] = intval;
    992  1.2  haad 			if (nvlist_add_uint64_array(ret, newpropname,
    993  1.2  haad 			    valary, 3) != 0) {
    994  1.2  haad 				(void) no_memory(hdl);
    995  1.2  haad 				goto error;
    996  1.2  haad 			}
    997  1.2  haad 			continue;
    998  1.4   chs 		} else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
    999  1.4   chs 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1000  1.4   chs 			    "'%s' is readonly"),
   1001  1.4   chs 			    propname);
   1002  1.4   chs 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
   1003  1.4   chs 			goto error;
   1004  1.2  haad 		}
   1005  1.2  haad 
   1006  1.2  haad 		if (prop == ZPROP_INVAL) {
   1007  1.2  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1008  1.2  haad 			    "invalid property '%s'"), propname);
   1009  1.2  haad 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
   1010  1.2  haad 			goto error;
   1011  1.2  haad 		}
   1012  1.2  haad 
   1013  1.1  haad 		if (!zfs_prop_valid_for_type(prop, type)) {
   1014  1.1  haad 			zfs_error_aux(hdl,
   1015  1.1  haad 			    dgettext(TEXT_DOMAIN, "'%s' does not "
   1016  1.1  haad 			    "apply to datasets of this type"), propname);
   1017  1.1  haad 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
   1018  1.1  haad 			goto error;
   1019  1.1  haad 		}
   1020  1.1  haad 
   1021  1.1  haad 		if (zfs_prop_readonly(prop) &&
   1022  1.1  haad 		    (!zfs_prop_setonce(prop) || zhp != NULL)) {
   1023  1.1  haad 			zfs_error_aux(hdl,
   1024  1.1  haad 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
   1025  1.1  haad 			    propname);
   1026  1.1  haad 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
   1027  1.1  haad 			goto error;
   1028  1.1  haad 		}
   1029  1.1  haad 
   1030  1.1  haad 		if (zprop_parse_value(hdl, elem, prop, type, ret,
   1031  1.1  haad 		    &strval, &intval, errbuf) != 0)
   1032  1.1  haad 			goto error;
   1033  1.1  haad 
   1034  1.1  haad 		/*
   1035  1.1  haad 		 * Perform some additional checks for specific properties.
   1036  1.1  haad 		 */
   1037  1.1  haad 		switch (prop) {
   1038  1.1  haad 		case ZFS_PROP_VERSION:
   1039  1.1  haad 		{
   1040  1.1  haad 			int version;
   1041  1.1  haad 
   1042  1.1  haad 			if (zhp == NULL)
   1043  1.1  haad 				break;
   1044  1.1  haad 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
   1045  1.1  haad 			if (intval < version) {
   1046  1.1  haad 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1047  1.1  haad 				    "Can not downgrade; already at version %u"),
   1048  1.1  haad 				    version);
   1049  1.1  haad 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
   1050  1.1  haad 				goto error;
   1051  1.1  haad 			}
   1052  1.1  haad 			break;
   1053  1.1  haad 		}
   1054  1.1  haad 
   1055  1.4   chs 		case ZFS_PROP_VOLBLOCKSIZE:
   1056  1.1  haad 		case ZFS_PROP_RECORDSIZE:
   1057  1.4   chs 		{
   1058  1.4   chs 			int maxbs = SPA_MAXBLOCKSIZE;
   1059  1.4   chs 			if (zpool_hdl != NULL) {
   1060  1.4   chs 				maxbs = zpool_get_prop_int(zpool_hdl,
   1061  1.4   chs 				    ZPOOL_PROP_MAXBLOCKSIZE, NULL);
   1062  1.4   chs 			}
   1063  1.4   chs 			/*
   1064  1.4   chs 			 * Volumes are limited to a volblocksize of 128KB,
   1065  1.4   chs 			 * because they typically service workloads with
   1066  1.4   chs 			 * small random writes, which incur a large performance
   1067  1.4   chs 			 * penalty with large blocks.
   1068  1.4   chs 			 */
   1069  1.4   chs 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
   1070  1.4   chs 				maxbs = SPA_OLD_MAXBLOCKSIZE;
   1071  1.4   chs 			/*
   1072  1.4   chs 			 * The value must be a power of two between
   1073  1.4   chs 			 * SPA_MINBLOCKSIZE and maxbs.
   1074  1.4   chs 			 */
   1075  1.1  haad 			if (intval < SPA_MINBLOCKSIZE ||
   1076  1.4   chs 			    intval > maxbs || !ISP2(intval)) {
   1077  1.1  haad 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1078  1.4   chs 				    "'%s' must be power of 2 from 512B "
   1079  1.4   chs 				    "to %uKB"), propname, maxbs >> 10);
   1080  1.1  haad 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
   1081  1.1  haad 				goto error;
   1082  1.1  haad 			}
   1083  1.1  haad 			break;
   1084  1.4   chs 		}
   1085  1.2  haad 		case ZFS_PROP_MLSLABEL:
   1086  1.2  haad 		{
   1087  1.4   chs #ifdef illumos
   1088  1.2  haad 			/*
   1089  1.2  haad 			 * Verify the mlslabel string and convert to
   1090  1.2  haad 			 * internal hex label string.
   1091  1.2  haad 			 */
   1092  1.2  haad 
   1093  1.2  haad 			m_label_t *new_sl;
   1094  1.2  haad 			char *hex = NULL;	/* internal label string */
   1095  1.2  haad 
   1096  1.2  haad 			/* Default value is already OK. */
   1097  1.2  haad 			if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
   1098  1.2  haad 				break;
   1099  1.2  haad 
   1100  1.2  haad 			/* Verify the label can be converted to binary form */
   1101  1.2  haad 			if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
   1102  1.2  haad 			    (str_to_label(strval, &new_sl, MAC_LABEL,
   1103  1.2  haad 			    L_NO_CORRECTION, NULL) == -1)) {
   1104  1.2  haad 				goto badlabel;
   1105  1.2  haad 			}
   1106  1.2  haad 
   1107  1.2  haad 			/* Now translate to hex internal label string */
   1108  1.2  haad 			if (label_to_str(new_sl, &hex, M_INTERNAL,
   1109  1.2  haad 			    DEF_NAMES) != 0) {
   1110  1.2  haad 				if (hex)
   1111  1.2  haad 					free(hex);
   1112  1.2  haad 				goto badlabel;
   1113  1.2  haad 			}
   1114  1.2  haad 			m_label_free(new_sl);
   1115  1.2  haad 
   1116  1.2  haad 			/* If string is already in internal form, we're done. */
   1117  1.2  haad 			if (strcmp(strval, hex) == 0) {
   1118  1.2  haad 				free(hex);
   1119  1.2  haad 				break;
   1120  1.2  haad 			}
   1121  1.2  haad 
   1122  1.2  haad 			/* Replace the label string with the internal form. */
   1123  1.2  haad 			(void) nvlist_remove(ret, zfs_prop_to_name(prop),
   1124  1.2  haad 			    DATA_TYPE_STRING);
   1125  1.2  haad 			verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
   1126  1.2  haad 			    hex) == 0);
   1127  1.2  haad 			free(hex);
   1128  1.2  haad 
   1129  1.2  haad 			break;
   1130  1.2  haad 
   1131  1.2  haad badlabel:
   1132  1.2  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1133  1.2  haad 			    "invalid mlslabel '%s'"), strval);
   1134  1.2  haad 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
   1135  1.2  haad 			m_label_free(new_sl);	/* OK if null */
   1136  1.4   chs #else	/* !illumos */
   1137  1.4   chs 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1138  1.5   nia 			    "mlslabel is not supported on NetBSD"));
   1139  1.4   chs 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
   1140  1.4   chs #endif	/* illumos */
   1141  1.2  haad 			goto error;
   1142  1.1  haad 
   1143  1.2  haad 		}
   1144  1.4   chs 
   1145  1.1  haad 		case ZFS_PROP_MOUNTPOINT:
   1146  1.1  haad 		{
   1147  1.1  haad 			namecheck_err_t why;
   1148  1.1  haad 
   1149  1.1  haad 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
   1150  1.1  haad 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
   1151  1.1  haad 				break;
   1152  1.1  haad 
   1153  1.1  haad 			if (mountpoint_namecheck(strval, &why)) {
   1154  1.1  haad 				switch (why) {
   1155  1.1  haad 				case NAME_ERR_LEADING_SLASH:
   1156  1.1  haad 					zfs_error_aux(hdl,
   1157  1.1  haad 					    dgettext(TEXT_DOMAIN,
   1158  1.1  haad 					    "'%s' must be an absolute path, "
   1159  1.1  haad 					    "'none', or 'legacy'"), propname);
   1160  1.1  haad 					break;
   1161  1.1  haad 				case NAME_ERR_TOOLONG:
   1162  1.1  haad 					zfs_error_aux(hdl,
   1163  1.1  haad 					    dgettext(TEXT_DOMAIN,
   1164  1.1  haad 					    "component of '%s' is too long"),
   1165  1.1  haad 					    propname);
   1166  1.1  haad 					break;
   1167  1.4   chs 
   1168  1.4   chs 				default:
   1169  1.4   chs 					zfs_error_aux(hdl,
   1170  1.4   chs 					    dgettext(TEXT_DOMAIN,
   1171  1.4   chs 					    "(%d) not defined"),
   1172  1.4   chs 					    why);
   1173  1.4   chs 					break;
   1174  1.1  haad 				}
   1175  1.1  haad 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
   1176  1.1  haad 				goto error;
   1177  1.1  haad 			}
   1178  1.1  haad 		}
   1179  1.1  haad 
   1180  1.1  haad 			/*FALLTHRU*/
   1181  1.1  haad 
   1182  1.1  haad 		case ZFS_PROP_SHARESMB:
   1183  1.1  haad 		case ZFS_PROP_SHARENFS:
   1184  1.1  haad 			/*
   1185  1.1  haad 			 * For the mountpoint and sharenfs or sharesmb
   1186  1.1  haad 			 * properties, check if it can be set in a
   1187  1.1  haad 			 * global/non-global zone based on
   1188  1.1  haad 			 * the zoned property value:
   1189  1.1  haad 			 *
   1190  1.1  haad 			 *		global zone	    non-global zone
   1191  1.1  haad 			 * --------------------------------------------------
   1192  1.1  haad 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
   1193  1.1  haad 			 *		sharenfs (no)	    sharenfs (no)
   1194  1.1  haad 			 *		sharesmb (no)	    sharesmb (no)
   1195  1.1  haad 			 *
   1196  1.1  haad 			 * zoned=off	mountpoint (yes)	N/A
   1197  1.1  haad 			 *		sharenfs (yes)
   1198  1.1  haad 			 *		sharesmb (yes)
   1199  1.1  haad 			 */
   1200  1.1  haad 			if (zoned) {
   1201  1.1  haad 				if (getzoneid() == GLOBAL_ZONEID) {
   1202  1.1  haad 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1203  1.1  haad 					    "'%s' cannot be set on "
   1204  1.1  haad 					    "dataset in a non-global zone"),
   1205  1.1  haad 					    propname);
   1206  1.1  haad 					(void) zfs_error(hdl, EZFS_ZONED,
   1207  1.1  haad 					    errbuf);
   1208  1.1  haad 					goto error;
   1209  1.1  haad 				} else if (prop == ZFS_PROP_SHARENFS ||
   1210  1.1  haad 				    prop == ZFS_PROP_SHARESMB) {
   1211  1.1  haad 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1212  1.1  haad 					    "'%s' cannot be set in "
   1213  1.1  haad 					    "a non-global zone"), propname);
   1214  1.1  haad 					(void) zfs_error(hdl, EZFS_ZONED,
   1215  1.1  haad 					    errbuf);
   1216  1.1  haad 					goto error;
   1217  1.1  haad 				}
   1218  1.1  haad 			} else if (getzoneid() != GLOBAL_ZONEID) {
   1219  1.1  haad 				/*
   1220  1.1  haad 				 * If zoned property is 'off', this must be in
   1221  1.2  haad 				 * a global zone. If not, something is wrong.
   1222  1.1  haad 				 */
   1223  1.1  haad 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1224  1.1  haad 				    "'%s' cannot be set while dataset "
   1225  1.1  haad 				    "'zoned' property is set"), propname);
   1226  1.1  haad 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
   1227  1.1  haad 				goto error;
   1228  1.1  haad 			}
   1229  1.1  haad 
   1230  1.1  haad 			/*
   1231  1.1  haad 			 * At this point, it is legitimate to set the
   1232  1.1  haad 			 * property. Now we want to make sure that the
   1233  1.1  haad 			 * property value is valid if it is sharenfs.
   1234  1.1  haad 			 */
   1235  1.1  haad 			if ((prop == ZFS_PROP_SHARENFS ||
   1236  1.1  haad 			    prop == ZFS_PROP_SHARESMB) &&
   1237  1.1  haad 			    strcmp(strval, "on") != 0 &&
   1238  1.1  haad 			    strcmp(strval, "off") != 0) {
   1239  1.1  haad 				zfs_share_proto_t proto;
   1240  1.1  haad 
   1241  1.1  haad 				if (prop == ZFS_PROP_SHARESMB)
   1242  1.1  haad 					proto = PROTO_SMB;
   1243  1.1  haad 				else
   1244  1.1  haad 					proto = PROTO_NFS;
   1245  1.1  haad 
   1246  1.1  haad 				/*
   1247  1.1  haad 				 * Must be an valid sharing protocol
   1248  1.1  haad 				 * option string so init the libshare
   1249  1.1  haad 				 * in order to enable the parser and
   1250  1.1  haad 				 * then parse the options. We use the
   1251  1.1  haad 				 * control API since we don't care about
   1252  1.1  haad 				 * the current configuration and don't
   1253  1.1  haad 				 * want the overhead of loading it
   1254  1.1  haad 				 * until we actually do something.
   1255  1.1  haad 				 */
   1256  1.1  haad 
   1257  1.1  haad 				if (zfs_init_libshare(hdl,
   1258  1.1  haad 				    SA_INIT_CONTROL_API) != SA_OK) {
   1259  1.1  haad 					/*
   1260  1.1  haad 					 * An error occurred so we can't do
   1261  1.1  haad 					 * anything
   1262  1.1  haad 					 */
   1263  1.1  haad 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1264  1.1  haad 					    "'%s' cannot be set: problem "
   1265  1.1  haad 					    "in share initialization"),
   1266  1.1  haad 					    propname);
   1267  1.1  haad 					(void) zfs_error(hdl, EZFS_BADPROP,
   1268  1.1  haad 					    errbuf);
   1269  1.1  haad 					goto error;
   1270  1.1  haad 				}
   1271  1.1  haad 
   1272  1.1  haad 				if (zfs_parse_options(strval, proto) != SA_OK) {
   1273  1.1  haad 					/*
   1274  1.1  haad 					 * There was an error in parsing so
   1275  1.1  haad 					 * deal with it by issuing an error
   1276  1.1  haad 					 * message and leaving after
   1277  1.1  haad 					 * uninitializing the the libshare
   1278  1.1  haad 					 * interface.
   1279  1.1  haad 					 */
   1280  1.1  haad 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1281  1.1  haad 					    "'%s' cannot be set to invalid "
   1282  1.1  haad 					    "options"), propname);
   1283  1.1  haad 					(void) zfs_error(hdl, EZFS_BADPROP,
   1284  1.1  haad 					    errbuf);
   1285  1.1  haad 					zfs_uninit_libshare(hdl);
   1286  1.1  haad 					goto error;
   1287  1.1  haad 				}
   1288  1.1  haad 				zfs_uninit_libshare(hdl);
   1289  1.1  haad 			}
   1290  1.1  haad 
   1291  1.1  haad 			break;
   1292  1.4   chs 
   1293  1.1  haad 		case ZFS_PROP_UTF8ONLY:
   1294  1.1  haad 			chosen_utf = (int)intval;
   1295  1.1  haad 			break;
   1296  1.4   chs 
   1297  1.1  haad 		case ZFS_PROP_NORMALIZE:
   1298  1.1  haad 			chosen_normal = (int)intval;
   1299  1.1  haad 			break;
   1300  1.4   chs 
   1301  1.4   chs 		default:
   1302  1.4   chs 			break;
   1303  1.1  haad 		}
   1304  1.1  haad 
   1305  1.1  haad 		/*
   1306  1.1  haad 		 * For changes to existing volumes, we have some additional
   1307  1.1  haad 		 * checks to enforce.
   1308  1.1  haad 		 */
   1309  1.1  haad 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
   1310  1.1  haad 			uint64_t volsize = zfs_prop_get_int(zhp,
   1311  1.1  haad 			    ZFS_PROP_VOLSIZE);
   1312  1.1  haad 			uint64_t blocksize = zfs_prop_get_int(zhp,
   1313  1.1  haad 			    ZFS_PROP_VOLBLOCKSIZE);
   1314  1.1  haad 			char buf[64];
   1315  1.1  haad 
   1316  1.1  haad 			switch (prop) {
   1317  1.1  haad 			case ZFS_PROP_RESERVATION:
   1318  1.1  haad 			case ZFS_PROP_REFRESERVATION:
   1319  1.1  haad 				if (intval > volsize) {
   1320  1.1  haad 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1321  1.1  haad 					    "'%s' is greater than current "
   1322  1.1  haad 					    "volume size"), propname);
   1323  1.1  haad 					(void) zfs_error(hdl, EZFS_BADPROP,
   1324  1.1  haad 					    errbuf);
   1325  1.1  haad 					goto error;
   1326  1.1  haad 				}
   1327  1.1  haad 				break;
   1328  1.1  haad 
   1329  1.1  haad 			case ZFS_PROP_VOLSIZE:
   1330  1.1  haad 				if (intval % blocksize != 0) {
   1331  1.1  haad 					zfs_nicenum(blocksize, buf,
   1332  1.1  haad 					    sizeof (buf));
   1333  1.1  haad 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1334  1.1  haad 					    "'%s' must be a multiple of "
   1335  1.1  haad 					    "volume block size (%s)"),
   1336  1.1  haad 					    propname, buf);
   1337  1.1  haad 					(void) zfs_error(hdl, EZFS_BADPROP,
   1338  1.1  haad 					    errbuf);
   1339  1.1  haad 					goto error;
   1340  1.1  haad 				}
   1341  1.1  haad 
   1342  1.1  haad 				if (intval == 0) {
   1343  1.1  haad 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1344  1.1  haad 					    "'%s' cannot be zero"),
   1345  1.1  haad 					    propname);
   1346  1.1  haad 					(void) zfs_error(hdl, EZFS_BADPROP,
   1347  1.1  haad 					    errbuf);
   1348  1.1  haad 					goto error;
   1349  1.1  haad 				}
   1350  1.1  haad 				break;
   1351  1.4   chs 
   1352  1.4   chs 			default:
   1353  1.4   chs 				break;
   1354  1.1  haad 			}
   1355  1.1  haad 		}
   1356  1.1  haad 	}
   1357  1.1  haad 
   1358  1.1  haad 	/*
   1359  1.1  haad 	 * If normalization was chosen, but no UTF8 choice was made,
   1360  1.1  haad 	 * enforce rejection of non-UTF8 names.
   1361  1.1  haad 	 *
   1362  1.1  haad 	 * If normalization was chosen, but rejecting non-UTF8 names
   1363  1.1  haad 	 * was explicitly not chosen, it is an error.
   1364  1.1  haad 	 */
   1365  1.1  haad 	if (chosen_normal > 0 && chosen_utf < 0) {
   1366  1.1  haad 		if (nvlist_add_uint64(ret,
   1367  1.1  haad 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
   1368  1.1  haad 			(void) no_memory(hdl);
   1369  1.1  haad 			goto error;
   1370  1.1  haad 		}
   1371  1.1  haad 	} else if (chosen_normal > 0 && chosen_utf == 0) {
   1372  1.1  haad 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1373  1.1  haad 		    "'%s' must be set 'on' if normalization chosen"),
   1374  1.1  haad 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
   1375  1.1  haad 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
   1376  1.1  haad 		goto error;
   1377  1.1  haad 	}
   1378  1.4   chs 	return (ret);
   1379  1.4   chs 
   1380  1.4   chs error:
   1381  1.4   chs 	nvlist_free(ret);
   1382  1.4   chs 	return (NULL);
   1383  1.4   chs }
   1384  1.4   chs 
   1385  1.4   chs int
   1386  1.4   chs zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
   1387  1.4   chs {
   1388  1.4   chs 	uint64_t old_volsize;
   1389  1.4   chs 	uint64_t new_volsize;
   1390  1.4   chs 	uint64_t old_reservation;
   1391  1.4   chs 	uint64_t new_reservation;
   1392  1.4   chs 	zfs_prop_t resv_prop;
   1393  1.4   chs 	nvlist_t *props;
   1394  1.1  haad 
   1395  1.1  haad 	/*
   1396  1.1  haad 	 * If this is an existing volume, and someone is setting the volsize,
   1397  1.1  haad 	 * make sure that it matches the reservation, or add it if necessary.
   1398  1.1  haad 	 */
   1399  1.4   chs 	old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
   1400  1.4   chs 	if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
   1401  1.4   chs 		return (-1);
   1402  1.4   chs 	old_reservation = zfs_prop_get_int(zhp, resv_prop);
   1403  1.1  haad 
   1404  1.4   chs 	props = fnvlist_alloc();
   1405  1.4   chs 	fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
   1406  1.4   chs 	    zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
   1407  1.4   chs 
   1408  1.4   chs 	if ((zvol_volsize_to_reservation(old_volsize, props) !=
   1409  1.4   chs 	    old_reservation) || nvlist_exists(nvl,
   1410  1.4   chs 	    zfs_prop_to_name(resv_prop))) {
   1411  1.4   chs 		fnvlist_free(props);
   1412  1.4   chs 		return (0);
   1413  1.4   chs 	}
   1414  1.4   chs 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
   1415  1.4   chs 	    &new_volsize) != 0) {
   1416  1.4   chs 		fnvlist_free(props);
   1417  1.4   chs 		return (-1);
   1418  1.4   chs 	}
   1419  1.4   chs 	new_reservation = zvol_volsize_to_reservation(new_volsize, props);
   1420  1.4   chs 	fnvlist_free(props);
   1421  1.1  haad 
   1422  1.4   chs 	if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
   1423  1.4   chs 	    new_reservation) != 0) {
   1424  1.4   chs 		(void) no_memory(zhp->zfs_hdl);
   1425  1.4   chs 		return (-1);
   1426  1.1  haad 	}
   1427  1.4   chs 	return (1);
   1428  1.1  haad }
   1429  1.1  haad 
   1430  1.2  haad void
   1431  1.2  haad zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
   1432  1.2  haad     char *errbuf)
   1433  1.1  haad {
   1434  1.2  haad 	switch (err) {
   1435  1.1  haad 
   1436  1.2  haad 	case ENOSPC:
   1437  1.1  haad 		/*
   1438  1.2  haad 		 * For quotas and reservations, ENOSPC indicates
   1439  1.2  haad 		 * something different; setting a quota or reservation
   1440  1.2  haad 		 * doesn't use any disk space.
   1441  1.1  haad 		 */
   1442  1.2  haad 		switch (prop) {
   1443  1.2  haad 		case ZFS_PROP_QUOTA:
   1444  1.2  haad 		case ZFS_PROP_REFQUOTA:
   1445  1.2  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1446  1.2  haad 			    "size is less than current used or "
   1447  1.2  haad 			    "reserved space"));
   1448  1.2  haad 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
   1449  1.2  haad 			break;
   1450  1.1  haad 
   1451  1.2  haad 		case ZFS_PROP_RESERVATION:
   1452  1.2  haad 		case ZFS_PROP_REFRESERVATION:
   1453  1.2  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1454  1.2  haad 			    "size is greater than available space"));
   1455  1.2  haad 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
   1456  1.2  haad 			break;
   1457  1.1  haad 
   1458  1.2  haad 		default:
   1459  1.2  haad 			(void) zfs_standard_error(hdl, err, errbuf);
   1460  1.2  haad 			break;
   1461  1.1  haad 		}
   1462  1.2  haad 		break;
   1463  1.1  haad 
   1464  1.2  haad 	case EBUSY:
   1465  1.2  haad 		(void) zfs_standard_error(hdl, EBUSY, errbuf);
   1466  1.2  haad 		break;
   1467  1.1  haad 
   1468  1.2  haad 	case EROFS:
   1469  1.2  haad 		(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
   1470  1.2  haad 		break;
   1471  1.1  haad 
   1472  1.4   chs 	case E2BIG:
   1473  1.4   chs 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1474  1.4   chs 		    "property value too long"));
   1475  1.4   chs 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
   1476  1.4   chs 		break;
   1477  1.4   chs 
   1478  1.2  haad 	case ENOTSUP:
   1479  1.2  haad 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1480  1.2  haad 		    "pool and or dataset must be upgraded to set this "
   1481  1.2  haad 		    "property or value"));
   1482  1.2  haad 		(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
   1483  1.2  haad 		break;
   1484  1.1  haad 
   1485  1.2  haad 	case ERANGE:
   1486  1.4   chs 	case EDOM:
   1487  1.4   chs 		if (prop == ZFS_PROP_COMPRESSION ||
   1488  1.4   chs 		    prop == ZFS_PROP_RECORDSIZE) {
   1489  1.2  haad 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1490  1.2  haad 			    "property setting is not allowed on "
   1491  1.2  haad 			    "bootable datasets"));
   1492  1.2  haad 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
   1493  1.4   chs 		} else if (prop == ZFS_PROP_CHECKSUM ||
   1494  1.4   chs 		    prop == ZFS_PROP_DEDUP) {
   1495  1.4   chs 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1496  1.4   chs 			    "property setting is not allowed on "
   1497  1.4   chs 			    "root pools"));
   1498  1.4   chs 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
   1499  1.4   chs 		} else {
   1500  1.4   chs 			(void) zfs_standard_error(hdl, err, errbuf);
   1501  1.4   chs 		}
   1502  1.4   chs 		break;
   1503  1.4   chs 
   1504  1.4   chs 	case EINVAL:
   1505  1.4   chs 		if (prop == ZPROP_INVAL) {
   1506  1.4   chs 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
   1507  1.1  haad 		} else {
   1508  1.2  haad 			(void) zfs_standard_error(hdl, err, errbuf);
   1509  1.1  haad 		}
   1510  1.2  haad 		break;
   1511  1.1  haad 
   1512  1.2  haad 	case EOVERFLOW:
   1513  1.1  haad 		/*
   1514  1.2  haad 		 * This platform can't address a volume this big.
   1515  1.1  haad 		 */
   1516  1.2  haad #ifdef _ILP32
   1517  1.2  haad 		if (prop == ZFS_PROP_VOLSIZE) {
   1518  1.2  haad 			(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
   1519  1.1  haad 			break;
   1520  1.1  haad 		}
   1521  1.2  haad #endif
   1522  1.2  haad 		/* FALLTHROUGH */
   1523  1.1  haad 	default:
   1524  1.2  haad 		(void) zfs_standard_error(hdl, err, errbuf);
   1525  1.1  haad 	}
   1526  1.1  haad }
   1527  1.1  haad 
   1528  1.1  haad /*
   1529  1.1  haad  * Given a property name and value, set the property for the given dataset.
   1530  1.1  haad  */
   1531  1.1  haad int
   1532  1.1  haad zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
   1533  1.1  haad {
   1534  1.1  haad 	int ret = -1;
   1535  1.1  haad 	char errbuf[1024];
   1536  1.1  haad 	libzfs_handle_t *hdl = zhp->zfs_hdl;
   1537  1.4   chs 	nvlist_t *nvl = NULL;
   1538  1.1  haad 
   1539  1.1  haad 	(void) snprintf(errbuf, sizeof (errbuf),
   1540  1.1  haad 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
   1541  1.1  haad 	    zhp->zfs_name);
   1542  1.1  haad 
   1543  1.1  haad 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
   1544  1.1  haad 	    nvlist_add_string(nvl, propname, propval) != 0) {
   1545  1.1  haad 		(void) no_memory(hdl);
   1546  1.1  haad 		goto error;
   1547  1.1  haad 	}
   1548  1.1  haad 
   1549  1.4   chs 	ret = zfs_prop_set_list(zhp, nvl);
   1550  1.1  haad 
   1551  1.4   chs error:
   1552  1.1  haad 	nvlist_free(nvl);
   1553  1.4   chs 	return (ret);
   1554  1.4   chs }
   1555  1.4   chs 
   1556  1.4   chs 
   1557  1.4   chs 
   1558  1.4   chs /*
   1559  1.4   chs  * Given an nvlist of property names and values, set the properties for the
   1560  1.4   chs  * given dataset.
   1561  1.4   chs  */
   1562  1.4   chs int
   1563  1.4   chs zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
   1564  1.4   chs {
   1565  1.4   chs 	zfs_cmd_t zc = { 0 };
   1566  1.4   chs 	int ret = -1;
   1567  1.4   chs 	prop_changelist_t **cls = NULL;
   1568  1.4   chs 	int cl_idx;
   1569  1.4   chs 	char errbuf[1024];
   1570  1.4   chs 	libzfs_handle_t *hdl = zhp->zfs_hdl;
   1571  1.4   chs 	nvlist_t *nvl;
   1572  1.4   chs 	int nvl_len;
   1573  1.4   chs 	int added_resv = 0;
   1574  1.1  haad 
   1575  1.4   chs 	(void) snprintf(errbuf, sizeof (errbuf),
   1576  1.4   chs 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
   1577  1.4   chs 	    zhp->zfs_name);
   1578  1.1  haad 
   1579  1.4   chs 	if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
   1580  1.4   chs 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
   1581  1.4   chs 	    errbuf)) == NULL)
   1582  1.1  haad 		goto error;
   1583  1.1  haad 
   1584  1.4   chs 	/*
   1585  1.4   chs 	 * We have to check for any extra properties which need to be added
   1586  1.4   chs 	 * before computing the length of the nvlist.
   1587  1.4   chs 	 */
   1588  1.4   chs 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
   1589  1.4   chs 	    elem != NULL;
   1590  1.4   chs 	    elem = nvlist_next_nvpair(nvl, elem)) {
   1591  1.4   chs 		if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
   1592  1.4   chs 		    (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
   1593  1.4   chs 			goto error;
   1594  1.4   chs 		}
   1595  1.1  haad 	}
   1596  1.1  haad 	/*
   1597  1.4   chs 	 * Check how many properties we're setting and allocate an array to
   1598  1.4   chs 	 * store changelist pointers for postfix().
   1599  1.1  haad 	 */
   1600  1.4   chs 	nvl_len = 0;
   1601  1.4   chs 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
   1602  1.4   chs 	    elem != NULL;
   1603  1.4   chs 	    elem = nvlist_next_nvpair(nvl, elem))
   1604  1.4   chs 		nvl_len++;
   1605  1.4   chs 	if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
   1606  1.4   chs 		goto error;
   1607  1.4   chs 
   1608  1.4   chs 	cl_idx = 0;
   1609  1.4   chs 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
   1610  1.4   chs 	    elem != NULL;
   1611  1.4   chs 	    elem = nvlist_next_nvpair(nvl, elem)) {
   1612  1.1  haad 
   1613  1.4   chs 		zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
   1614  1.4   chs 
   1615  1.4   chs 		assert(cl_idx < nvl_len);
   1616  1.4   chs 		/*
   1617  1.4   chs 		 * We don't want to unmount & remount the dataset when changing
   1618  1.4   chs 		 * its canmount property to 'on' or 'noauto'.  We only use
   1619  1.4   chs 		 * the changelist logic to unmount when setting canmount=off.
   1620  1.4   chs 		 */
   1621  1.4   chs 		if (prop != ZFS_PROP_CANMOUNT ||
   1622  1.4   chs 		    (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
   1623  1.4   chs 		    zfs_is_mounted(zhp, NULL))) {
   1624  1.4   chs 			cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
   1625  1.4   chs 			if (cls[cl_idx] == NULL)
   1626  1.4   chs 				goto error;
   1627  1.4   chs 		}
   1628  1.4   chs 
   1629  1.4   chs 		if (prop == ZFS_PROP_MOUNTPOINT &&
   1630  1.4   chs 		    changelist_haszonedchild(cls[cl_idx])) {
   1631  1.4   chs 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1632  1.4   chs 			    "child dataset with inherited mountpoint is used "
   1633  1.4   chs 			    "in a non-global zone"));
   1634  1.4   chs 			ret = zfs_error(hdl, EZFS_ZONED, errbuf);
   1635  1.4   chs 			goto error;
   1636  1.4   chs 		}
   1637  1.4   chs 
   1638  1.4   chs 		/* We don't support those properties on FreeBSD. */
   1639  1.4   chs 		switch (prop) {
   1640  1.4   chs 		case ZFS_PROP_DEVICES:
   1641  1.4   chs 		case ZFS_PROP_ISCSIOPTIONS:
   1642  1.4   chs 		case ZFS_PROP_XATTR:
   1643  1.4   chs 		case ZFS_PROP_VSCAN:
   1644  1.4   chs 		case ZFS_PROP_NBMAND:
   1645  1.4   chs 		case ZFS_PROP_MLSLABEL:
   1646  1.4   chs 			(void) snprintf(errbuf, sizeof (errbuf),
   1647  1.5   nia 			    "property '%s' not supported on NetBSD",
   1648  1.4   chs 			    nvpair_name(elem));
   1649  1.4   chs 			ret = zfs_error(hdl, EZFS_PERM, errbuf);
   1650  1.4   chs 			goto error;
   1651  1.4   chs 		}
   1652  1.4   chs 
   1653  1.4   chs 		if (cls[cl_idx] != NULL &&
   1654  1.4   chs 		    (ret = changelist_prefix(cls[cl_idx])) != 0)
   1655  1.4   chs 			goto error;
   1656  1.4   chs 
   1657  1.4   chs 		cl_idx++;
   1658  1.4   chs 	}
   1659  1.4   chs 	assert(cl_idx == nvl_len);
   1660  1.1  haad 
   1661  1.1  haad 	/*
   1662  1.4   chs 	 * Execute the corresponding ioctl() to set this list of properties.
   1663  1.1  haad 	 */
   1664  1.1  haad 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
   1665  1.1  haad 
   1666  1.4   chs 	if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
   1667  1.4   chs 	    (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
   1668  1.1  haad 		goto error;
   1669  1.1  haad 
   1670  1.1  haad 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
   1671  1.2  haad 
   1672  1.1  haad 	if (ret != 0) {
   1673  1.4   chs 		/* Get the list of unset properties back and report them. */
   1674  1.4   chs 		nvlist_t *errorprops = NULL;
   1675  1.4   chs 		if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
   1676  1.4   chs 			goto error;
   1677  1.4   chs 		for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
   1678  1.4   chs 		    elem != NULL;
   1679  1.4   chs 		    elem = nvlist_next_nvpair(nvl, elem)) {
   1680  1.4   chs 			zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
   1681  1.4   chs 			zfs_setprop_error(hdl, prop, errno, errbuf);
   1682  1.4   chs 		}
   1683  1.4   chs 		nvlist_free(errorprops);
   1684  1.4   chs 
   1685  1.4   chs 		if (added_resv && errno == ENOSPC) {
   1686  1.4   chs 			/* clean up the volsize property we tried to set */
   1687  1.4   chs 			uint64_t old_volsize = zfs_prop_get_int(zhp,
   1688  1.4   chs 			    ZFS_PROP_VOLSIZE);
   1689  1.4   chs 			nvlist_free(nvl);
   1690  1.4   chs 			nvl = NULL;
   1691  1.4   chs 			zcmd_free_nvlists(&zc);
   1692  1.4   chs 
   1693  1.4   chs 			if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
   1694  1.4   chs 				goto error;
   1695  1.4   chs 			if (nvlist_add_uint64(nvl,
   1696  1.4   chs 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
   1697  1.4   chs 			    old_volsize) != 0)
   1698  1.4   chs 				goto error;
   1699  1.4   chs 			if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
   1700  1.4   chs 				goto error;
   1701  1.4   chs 			(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
   1702  1.4   chs 		}
   1703  1.1  haad 	} else {
   1704  1.4   chs 		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
   1705  1.4   chs 			if (cls[cl_idx] != NULL) {
   1706  1.4   chs 				int clp_err = changelist_postfix(cls[cl_idx]);
   1707  1.4   chs 				if (clp_err != 0)
   1708  1.4   chs 					ret = clp_err;
   1709  1.4   chs 			}
   1710  1.4   chs 		}
   1711  1.1  haad 
   1712  1.1  haad 		/*
   1713  1.1  haad 		 * Refresh the statistics so the new property value
   1714  1.1  haad 		 * is reflected.
   1715  1.1  haad 		 */
   1716  1.1  haad 		if (ret == 0)
   1717  1.1  haad 			(void) get_stats(zhp);
   1718  1.1  haad 	}
   1719  1.1  haad 
   1720  1.1  haad error:
   1721  1.1  haad 	nvlist_free(nvl);
   1722  1.1  haad 	zcmd_free_nvlists(&zc);
   1723  1.4   chs 	if (cls != NULL) {
   1724  1.4   chs 		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
   1725  1.4   chs 			if (cls[cl_idx] != NULL)
   1726  1.4   chs 				changelist_free(cls[cl_idx]);
   1727  1.4   chs 		}
   1728  1.4   chs 		free(cls);
   1729  1.4   chs 	}
   1730  1.1  haad 	return (ret);
   1731  1.1  haad }
   1732  1.1  haad 
   1733  1.1  haad /*
   1734  1.2  haad  * Given a property, inherit the value from the parent dataset, or if received
   1735  1.2  haad  * is TRUE, revert to the received value, if any.
   1736  1.1  haad  */
   1737  1.1  haad int
   1738  1.2  haad zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
   1739  1.1  haad {
   1740  1.1  haad 	zfs_cmd_t zc = { 0 };
   1741  1.1  haad 	int ret;
   1742  1.1  haad 	prop_changelist_t *cl;
   1743  1.1  haad 	libzfs_handle_t *hdl = zhp->zfs_hdl;
   1744  1.1  haad 	char errbuf[1024];
   1745  1.1  haad 	zfs_prop_t prop;
   1746  1.1  haad 
   1747  1.1  haad 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
   1748  1.1  haad 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
   1749  1.1  haad 
   1750  1.2  haad 	zc.zc_cookie = received;
   1751  1.1  haad 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
   1752  1.1  haad 		/*
   1753  1.1  haad 		 * For user properties, the amount of work we have to do is very
   1754  1.1  haad 		 * small, so just do it here.
   1755  1.1  haad 		 */
   1756  1.1  haad 		if (!zfs_prop_user(propname)) {
   1757  1.1  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1758  1.1  haad 			    "invalid property"));
   1759  1.1  haad 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
   1760  1.1  haad 		}
   1761  1.1  haad 
   1762  1.1  haad 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
   1763  1.1  haad 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
   1764  1.1  haad 
   1765  1.1  haad 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
   1766  1.1  haad 			return (zfs_standard_error(hdl, errno, errbuf));
   1767  1.1  haad 
   1768  1.1  haad 		return (0);
   1769  1.1  haad 	}
   1770  1.1  haad 
   1771  1.1  haad 	/*
   1772  1.1  haad 	 * Verify that this property is inheritable.
   1773  1.1  haad 	 */
   1774  1.1  haad 	if (zfs_prop_readonly(prop))
   1775  1.1  haad 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
   1776  1.1  haad 
   1777  1.2  haad 	if (!zfs_prop_inheritable(prop) && !received)
   1778  1.1  haad 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
   1779  1.1  haad 
   1780  1.1  haad 	/*
   1781  1.1  haad 	 * Check to see if the value applies to this type
   1782  1.1  haad 	 */
   1783  1.1  haad 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
   1784  1.1  haad 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
   1785  1.1  haad 
   1786  1.1  haad 	/*
   1787  1.4   chs 	 * Normalize the name, to get rid of shorthand abbreviations.
   1788  1.1  haad 	 */
   1789  1.1  haad 	propname = zfs_prop_to_name(prop);
   1790  1.1  haad 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
   1791  1.1  haad 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
   1792  1.1  haad 
   1793  1.1  haad 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
   1794  1.1  haad 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
   1795  1.1  haad 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1796  1.1  haad 		    "dataset is used in a non-global zone"));
   1797  1.1  haad 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
   1798  1.1  haad 	}
   1799  1.1  haad 
   1800  1.1  haad 	/*
   1801  1.1  haad 	 * Determine datasets which will be affected by this change, if any.
   1802  1.1  haad 	 */
   1803  1.1  haad 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
   1804  1.1  haad 		return (-1);
   1805  1.1  haad 
   1806  1.1  haad 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
   1807  1.1  haad 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   1808  1.1  haad 		    "child dataset with inherited mountpoint is used "
   1809  1.1  haad 		    "in a non-global zone"));
   1810  1.1  haad 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
   1811  1.1  haad 		goto error;
   1812  1.1  haad 	}
   1813  1.1  haad 
   1814  1.1  haad 	if ((ret = changelist_prefix(cl)) != 0)
   1815  1.1  haad 		goto error;
   1816  1.1  haad 
   1817  1.1  haad 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
   1818  1.1  haad 		return (zfs_standard_error(hdl, errno, errbuf));
   1819  1.1  haad 	} else {
   1820  1.1  haad 
   1821  1.1  haad 		if ((ret = changelist_postfix(cl)) != 0)
   1822  1.1  haad 			goto error;
   1823  1.1  haad 
   1824  1.1  haad 		/*
   1825  1.1  haad 		 * Refresh the statistics so the new property is reflected.
   1826  1.1  haad 		 */
   1827  1.1  haad 		(void) get_stats(zhp);
   1828  1.1  haad 	}
   1829  1.1  haad 
   1830  1.1  haad error:
   1831  1.1  haad 	changelist_free(cl);
   1832  1.1  haad 	return (ret);
   1833  1.1  haad }
   1834  1.1  haad 
   1835  1.1  haad /*
   1836  1.1  haad  * True DSL properties are stored in an nvlist.  The following two functions
   1837  1.1  haad  * extract them appropriately.
   1838  1.1  haad  */
   1839  1.1  haad static uint64_t
   1840  1.1  haad getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
   1841  1.1  haad {
   1842  1.1  haad 	nvlist_t *nv;
   1843  1.1  haad 	uint64_t value;
   1844  1.1  haad 
   1845  1.1  haad 	*source = NULL;
   1846  1.1  haad 	if (nvlist_lookup_nvlist(zhp->zfs_props,
   1847  1.1  haad 	    zfs_prop_to_name(prop), &nv) == 0) {
   1848  1.1  haad 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
   1849  1.1  haad 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
   1850  1.1  haad 	} else {
   1851  1.2  haad 		verify(!zhp->zfs_props_table ||
   1852  1.2  haad 		    zhp->zfs_props_table[prop] == B_TRUE);
   1853  1.1  haad 		value = zfs_prop_default_numeric(prop);
   1854  1.1  haad 		*source = "";
   1855  1.1  haad 	}
   1856  1.1  haad 
   1857  1.1  haad 	return (value);
   1858  1.1  haad }
   1859  1.1  haad 
   1860  1.4   chs static const char *
   1861  1.1  haad getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
   1862  1.1  haad {
   1863  1.1  haad 	nvlist_t *nv;
   1864  1.4   chs 	const char *value;
   1865  1.1  haad 
   1866  1.1  haad 	*source = NULL;
   1867  1.1  haad 	if (nvlist_lookup_nvlist(zhp->zfs_props,
   1868  1.1  haad 	    zfs_prop_to_name(prop), &nv) == 0) {
   1869  1.4   chs 		value = fnvlist_lookup_string(nv, ZPROP_VALUE);
   1870  1.1  haad 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
   1871  1.1  haad 	} else {
   1872  1.2  haad 		verify(!zhp->zfs_props_table ||
   1873  1.2  haad 		    zhp->zfs_props_table[prop] == B_TRUE);
   1874  1.4   chs 		value = zfs_prop_default_string(prop);
   1875  1.1  haad 		*source = "";
   1876  1.1  haad 	}
   1877  1.1  haad 
   1878  1.1  haad 	return (value);
   1879  1.1  haad }
   1880  1.1  haad 
   1881  1.2  haad static boolean_t
   1882  1.2  haad zfs_is_recvd_props_mode(zfs_handle_t *zhp)
   1883  1.2  haad {
   1884  1.2  haad 	return (zhp->zfs_props == zhp->zfs_recvd_props);
   1885  1.2  haad }
   1886  1.2  haad 
   1887  1.2  haad static void
   1888  1.2  haad zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
   1889  1.2  haad {
   1890  1.2  haad 	*cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
   1891  1.2  haad 	zhp->zfs_props = zhp->zfs_recvd_props;
   1892  1.2  haad }
   1893  1.2  haad 
   1894  1.2  haad static void
   1895  1.2  haad zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
   1896  1.2  haad {
   1897  1.2  haad 	zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
   1898  1.2  haad 	*cookie = 0;
   1899  1.2  haad }
   1900  1.2  haad 
   1901  1.1  haad /*
   1902  1.1  haad  * Internal function for getting a numeric property.  Both zfs_prop_get() and
   1903  1.1  haad  * zfs_prop_get_int() are built using this interface.
   1904  1.1  haad  *
   1905  1.1  haad  * Certain properties can be overridden using 'mount -o'.  In this case, scan
   1906  1.1  haad  * the contents of the /etc/mnttab entry, searching for the appropriate options.
   1907  1.1  haad  * If they differ from the on-disk values, report the current values and mark
   1908  1.1  haad  * the source "temporary".
   1909  1.1  haad  */
   1910  1.1  haad static int
   1911  1.1  haad get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
   1912  1.1  haad     char **source, uint64_t *val)
   1913  1.1  haad {
   1914  1.1  haad 	zfs_cmd_t zc = { 0 };
   1915  1.1  haad 	nvlist_t *zplprops = NULL;
   1916  1.1  haad 	struct mnttab mnt;
   1917  1.1  haad 	char *mntopt_on = NULL;
   1918  1.1  haad 	char *mntopt_off = NULL;
   1919  1.2  haad 	boolean_t received = zfs_is_recvd_props_mode(zhp);
   1920  1.1  haad 
   1921  1.1  haad 	*source = NULL;
   1922  1.1  haad 
   1923  1.1  haad 	switch (prop) {
   1924  1.1  haad 	case ZFS_PROP_ATIME:
   1925  1.1  haad 		mntopt_on = MNTOPT_ATIME;
   1926  1.1  haad 		mntopt_off = MNTOPT_NOATIME;
   1927  1.1  haad 		break;
   1928  1.1  haad 
   1929  1.1  haad 	case ZFS_PROP_DEVICES:
   1930  1.1  haad 		mntopt_on = MNTOPT_DEVICES;
   1931  1.1  haad 		mntopt_off = MNTOPT_NODEVICES;
   1932  1.1  haad 		break;
   1933  1.1  haad 
   1934  1.1  haad 	case ZFS_PROP_EXEC:
   1935  1.1  haad 		mntopt_on = MNTOPT_EXEC;
   1936  1.1  haad 		mntopt_off = MNTOPT_NOEXEC;
   1937  1.1  haad 		break;
   1938  1.1  haad 
   1939  1.1  haad 	case ZFS_PROP_READONLY:
   1940  1.1  haad 		mntopt_on = MNTOPT_RO;
   1941  1.1  haad 		mntopt_off = MNTOPT_RW;
   1942  1.1  haad 		break;
   1943  1.1  haad 
   1944  1.1  haad 	case ZFS_PROP_SETUID:
   1945  1.1  haad 		mntopt_on = MNTOPT_SETUID;
   1946  1.1  haad 		mntopt_off = MNTOPT_NOSETUID;
   1947  1.1  haad 		break;
   1948  1.1  haad 
   1949  1.1  haad 	case ZFS_PROP_XATTR:
   1950  1.1  haad 		mntopt_on = MNTOPT_XATTR;
   1951  1.1  haad 		mntopt_off = MNTOPT_NOXATTR;
   1952  1.1  haad 		break;
   1953  1.1  haad 
   1954  1.1  haad 	case ZFS_PROP_NBMAND:
   1955  1.1  haad 		mntopt_on = MNTOPT_NBMAND;
   1956  1.1  haad 		mntopt_off = MNTOPT_NONBMAND;
   1957  1.1  haad 		break;
   1958  1.4   chs 
   1959  1.4   chs 	default:
   1960  1.4   chs 		break;
   1961  1.1  haad 	}
   1962  1.1  haad 
   1963  1.1  haad 	/*
   1964  1.1  haad 	 * Because looking up the mount options is potentially expensive
   1965  1.1  haad 	 * (iterating over all of /etc/mnttab), we defer its calculation until
   1966  1.1  haad 	 * we're looking up a property which requires its presence.
   1967  1.1  haad 	 */
   1968  1.1  haad 	if (!zhp->zfs_mntcheck &&
   1969  1.1  haad 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
   1970  1.2  haad 		libzfs_handle_t *hdl = zhp->zfs_hdl;
   1971  1.2  haad 		struct mnttab entry;
   1972  1.1  haad 
   1973  1.2  haad 		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
   1974  1.2  haad 			zhp->zfs_mntopts = zfs_strdup(hdl,
   1975  1.1  haad 			    entry.mnt_mntopts);
   1976  1.1  haad 			if (zhp->zfs_mntopts == NULL)
   1977  1.1  haad 				return (-1);
   1978  1.1  haad 		}
   1979  1.1  haad 
   1980  1.1  haad 		zhp->zfs_mntcheck = B_TRUE;
   1981  1.1  haad 	}
   1982  1.1  haad 
   1983  1.1  haad 	if (zhp->zfs_mntopts == NULL)
   1984  1.1  haad 		mnt.mnt_mntopts = "";
   1985  1.1  haad 	else
   1986  1.1  haad 		mnt.mnt_mntopts = zhp->zfs_mntopts;
   1987  1.1  haad 
   1988  1.1  haad 	switch (prop) {
   1989  1.1  haad 	case ZFS_PROP_ATIME:
   1990  1.1  haad 	case ZFS_PROP_DEVICES:
   1991  1.1  haad 	case ZFS_PROP_EXEC:
   1992  1.1  haad 	case ZFS_PROP_READONLY:
   1993  1.1  haad 	case ZFS_PROP_SETUID:
   1994  1.1  haad 	case ZFS_PROP_XATTR:
   1995  1.1  haad 	case ZFS_PROP_NBMAND:
   1996  1.1  haad 		*val = getprop_uint64(zhp, prop, source);
   1997  1.1  haad 
   1998  1.2  haad 		if (received)
   1999  1.2  haad 			break;
   2000  1.2  haad 
   2001  1.1  haad 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
   2002  1.1  haad 			*val = B_TRUE;
   2003  1.1  haad 			if (src)
   2004  1.1  haad 				*src = ZPROP_SRC_TEMPORARY;
   2005  1.1  haad 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
   2006  1.1  haad 			*val = B_FALSE;
   2007  1.1  haad 			if (src)
   2008  1.1  haad 				*src = ZPROP_SRC_TEMPORARY;
   2009  1.1  haad 		}
   2010  1.1  haad 		break;
   2011  1.1  haad 
   2012  1.1  haad 	case ZFS_PROP_CANMOUNT:
   2013  1.2  haad 	case ZFS_PROP_VOLSIZE:
   2014  1.1  haad 	case ZFS_PROP_QUOTA:
   2015  1.1  haad 	case ZFS_PROP_REFQUOTA:
   2016  1.1  haad 	case ZFS_PROP_RESERVATION:
   2017  1.1  haad 	case ZFS_PROP_REFRESERVATION:
   2018  1.4   chs 	case ZFS_PROP_FILESYSTEM_LIMIT:
   2019  1.4   chs 	case ZFS_PROP_SNAPSHOT_LIMIT:
   2020  1.4   chs 	case ZFS_PROP_FILESYSTEM_COUNT:
   2021  1.4   chs 	case ZFS_PROP_SNAPSHOT_COUNT:
   2022  1.1  haad 		*val = getprop_uint64(zhp, prop, source);
   2023  1.2  haad 
   2024  1.2  haad 		if (*source == NULL) {
   2025  1.2  haad 			/* not default, must be local */
   2026  1.1  haad 			*source = zhp->zfs_name;
   2027  1.2  haad 		}
   2028  1.1  haad 		break;
   2029  1.1  haad 
   2030  1.1  haad 	case ZFS_PROP_MOUNTED:
   2031  1.1  haad 		*val = (zhp->zfs_mntopts != NULL);
   2032  1.1  haad 		break;
   2033  1.1  haad 
   2034  1.1  haad 	case ZFS_PROP_NUMCLONES:
   2035  1.1  haad 		*val = zhp->zfs_dmustats.dds_num_clones;
   2036  1.1  haad 		break;
   2037  1.1  haad 
   2038  1.1  haad 	case ZFS_PROP_VERSION:
   2039  1.1  haad 	case ZFS_PROP_NORMALIZE:
   2040  1.1  haad 	case ZFS_PROP_UTF8ONLY:
   2041  1.1  haad 	case ZFS_PROP_CASE:
   2042  1.1  haad 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
   2043  1.1  haad 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
   2044  1.1  haad 			return (-1);
   2045  1.1  haad 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
   2046  1.1  haad 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
   2047  1.1  haad 			zcmd_free_nvlists(&zc);
   2048  1.2  haad 			return (-1);
   2049  1.1  haad 		}
   2050  1.1  haad 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
   2051  1.1  haad 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
   2052  1.1  haad 		    val) != 0) {
   2053  1.1  haad 			zcmd_free_nvlists(&zc);
   2054  1.2  haad 			return (-1);
   2055  1.1  haad 		}
   2056  1.4   chs 		nvlist_free(zplprops);
   2057  1.1  haad 		zcmd_free_nvlists(&zc);
   2058  1.1  haad 		break;
   2059  1.1  haad 
   2060  1.4   chs 	case ZFS_PROP_INCONSISTENT:
   2061  1.4   chs 		*val = zhp->zfs_dmustats.dds_inconsistent;
   2062  1.4   chs 		break;
   2063  1.4   chs 
   2064  1.1  haad 	default:
   2065  1.1  haad 		switch (zfs_prop_get_type(prop)) {
   2066  1.1  haad 		case PROP_TYPE_NUMBER:
   2067  1.1  haad 		case PROP_TYPE_INDEX:
   2068  1.1  haad 			*val = getprop_uint64(zhp, prop, source);
   2069  1.1  haad 			/*
   2070  1.2  haad 			 * If we tried to use a default value for a
   2071  1.1  haad 			 * readonly property, it means that it was not
   2072  1.2  haad 			 * present.
   2073  1.1  haad 			 */
   2074  1.1  haad 			if (zfs_prop_readonly(prop) &&
   2075  1.2  haad 			    *source != NULL && (*source)[0] == '\0') {
   2076  1.2  haad 				*source = NULL;
   2077  1.1  haad 			}
   2078  1.1  haad 			break;
   2079  1.1  haad 
   2080  1.1  haad 		case PROP_TYPE_STRING:
   2081  1.1  haad 		default:
   2082  1.1  haad 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
   2083  1.1  haad 			    "cannot get non-numeric property"));
   2084  1.1  haad 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
   2085  1.1  haad 			    dgettext(TEXT_DOMAIN, "internal error")));
   2086  1.1  haad 		}
   2087  1.1  haad 	}
   2088  1.1  haad 
   2089  1.1  haad 	return (0);
   2090  1.1  haad }
   2091  1.1  haad 
   2092  1.1  haad /*
   2093  1.1  haad  * Calculate the source type, given the raw source string.
   2094  1.1  haad  */
   2095  1.1  haad static void
   2096  1.1  haad get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
   2097  1.1  haad     char *statbuf, size_t statlen)
   2098  1.1  haad {
   2099  1.1  haad 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
   2100  1.1  haad 		return;
   2101  1.1  haad 
   2102  1.1  haad 	if (source == NULL) {
   2103  1.1  haad 		*srctype = ZPROP_SRC_NONE;
   2104  1.1  haad 	} else if (source[0] == '\0') {
   2105  1.1  haad 		*srctype = ZPROP_SRC_DEFAULT;
   2106  1.2  haad 	} else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
   2107  1.2  haad 		*srctype = ZPROP_SRC_RECEIVED;
   2108  1.1  haad 	} else {
   2109  1.1  haad 		if (strcmp(source, zhp->zfs_name) == 0) {
   2110  1.1  haad 			*srctype = ZPROP_SRC_LOCAL;
   2111  1.1  haad 		} else {
   2112  1.1  haad 			(void) strlcpy(statbuf, source, statlen);
   2113  1.1  haad 			*srctype = ZPROP_SRC_INHERITED;
   2114  1.1  haad 		}
   2115  1.1  haad 	}
   2116  1.1  haad 
   2117  1.1  haad }
   2118  1.1  haad 
   2119  1.2  haad int
   2120  1.2  haad zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
   2121  1.2  haad     size_t proplen, boolean_t literal)
   2122  1.2  haad {
   2123  1.2  haad 	zfs_prop_t prop;
   2124  1.2  haad 	int err = 0;
   2125  1.2  haad 
   2126  1.2  haad 	if (zhp->zfs_recvd_props == NULL)
   2127  1.2  haad 		if (get_recvd_props_ioctl(zhp) != 0)
   2128  1.2  haad 			return (-1);
   2129  1.2  haad 
   2130  1.2  haad 	prop = zfs_name_to_prop(propname);
   2131  1.2  haad 
   2132  1.2  haad 	if (prop != ZPROP_INVAL) {
   2133  1.2  haad 		uint64_t cookie;
   2134  1.2  haad 		if (!nvlist_exists(zhp->zfs_recvd_props, propname))
   2135  1.2  haad 			return (-1);
   2136  1.2  haad 		zfs_set_recvd_props_mode(zhp, &cookie);
   2137  1.2  haad 		err = zfs_prop_get(zhp, prop, propbuf, proplen,
   2138  1.2  haad 		    NULL, NULL, 0, literal);
   2139  1.2  haad 		zfs_unset_recvd_props_mode(zhp, &cookie);
   2140  1.2  haad 	} else {
   2141  1.2  haad 		nvlist_t *propval;
   2142  1.2  haad 		char *recvdval;
   2143  1.2  haad 		if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
   2144  1.2  haad 		    propname, &propval) != 0)
   2145  1.2  haad 			return (-1);
   2146  1.2  haad 		verify(nvlist_lookup_string(propval, ZPROP_VALUE,
   2147  1.2  haad 		    &recvdval) == 0);
   2148  1.2  haad 		(void) strlcpy(propbuf, recvdval, proplen);
   2149  1.2  haad 	}
   2150  1.2  haad 
   2151  1.2  haad 	return (err == 0 ? 0 : -1);
   2152  1.2  haad }
   2153  1.2  haad 
   2154  1.4   chs static int
   2155  1.4   chs get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
   2156  1.4   chs {
   2157  1.4   chs 	nvlist_t *value;
   2158  1.4   chs 	nvpair_t *pair;
   2159  1.4   chs 
   2160  1.4   chs 	value = zfs_get_clones_nvl(zhp);
   2161  1.4   chs 	if (value == NULL)
   2162  1.4   chs 		return (-1);
   2163  1.4   chs 
   2164  1.4   chs 	propbuf[0] = '\0';
   2165  1.4   chs 	for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
   2166  1.4   chs 	    pair = nvlist_next_nvpair(value, pair)) {
   2167  1.4   chs 		if (propbuf[0] != '\0')
   2168  1.4   chs 			(void) strlcat(propbuf, ",", proplen);
   2169  1.4   chs 		(void) strlcat(propbuf, nvpair_name(pair), proplen);
   2170  1.4   chs 	}
   2171  1.4   chs 
   2172  1.4   chs 	return (0);
   2173  1.4   chs }
   2174  1.4   chs 
   2175  1.4   chs struct get_clones_arg {
   2176  1.4   chs 	uint64_t numclones;
   2177  1.4   chs 	nvlist_t *value;
   2178  1.4   chs 	const char *origin;
   2179  1.4   chs 	char buf[ZFS_MAX_DATASET_NAME_LEN];
   2180  1.4   chs };
   2181  1.4   chs 
   2182  1.4   chs int
   2183  1.4   chs get_clones_cb(zfs_handle_t *zhp, void *arg)
   2184  1.4   chs {
   2185  1.4   chs 	struct get_clones_arg *gca = arg;
   2186  1.4   chs 
   2187  1.4   chs 	if (gca->numclones == 0) {
   2188  1.4   chs 		zfs_close(zhp);
   2189  1.4   chs 		return (0);
   2190  1.4   chs 	}
   2191  1.4   chs 
   2192  1.4   chs 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
   2193  1.4   chs 	    NULL, NULL, 0, B_TRUE) != 0)
   2194  1.4   chs 		goto out;
   2195  1.4   chs 	if (strcmp(gca->buf, gca->origin) == 0) {
   2196  1.4   chs 		fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
   2197  1.4   chs 		gca->numclones--;
   2198  1.4   chs 	}
   2199  1.4   chs 
   2200  1.4   chs out:
   2201  1.4   chs 	(void) zfs_iter_children(zhp, get_clones_cb, gca);
   2202  1.4   chs 	zfs_close(zhp);
   2203  1.4   chs 	return (0);
   2204  1.4   chs }
   2205  1.4   chs 
   2206  1.4   chs nvlist_t *
   2207  1.4   chs zfs_get_clones_nvl(zfs_handle_t *zhp)
   2208  1.4   chs {
   2209  1.4   chs 	nvlist_t *nv, *value;
   2210  1.4   chs 
   2211  1.4   chs 	if (nvlist_lookup_nvlist(zhp->zfs_props,
   2212  1.4   chs 	    zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
   2213  1.4   chs 		struct get_clones_arg gca;
   2214  1.4   chs 
   2215  1.4   chs 		/*
   2216  1.4   chs 		 * if this is a snapshot, then the kernel wasn't able
   2217  1.4   chs 		 * to get the clones.  Do it by slowly iterating.
   2218  1.4   chs 		 */
   2219  1.4   chs 		if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
   2220  1.4   chs 			return (NULL);
   2221  1.4   chs 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
   2222  1.4   chs 			return (NULL);
   2223  1.4   chs 		if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
   2224  1.4   chs 			nvlist_free(nv);
   2225  1.4   chs 			return (NULL);
   2226  1.4   chs 		}
   2227  1.4   chs 
   2228  1.4   chs 		gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
   2229  1.4   chs 		gca.value = value;
   2230  1.4   chs 		gca.origin = zhp->zfs_name;
   2231  1.4   chs 
   2232  1.4   chs 		if (gca.numclones != 0) {
   2233  1.4   chs 			zfs_handle_t *root;
   2234  1.4   chs 			char pool[ZFS_MAX_DATASET_NAME_LEN];
   2235  1.4   chs 			char *cp = pool;
   2236  1.4   chs 
   2237  1.4   chs 			/* get the pool name */
   2238  1.4   chs 			(void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
   2239  1.4   chs 			(void) strsep(&cp, "/@");
   2240  1.4   chs 			root = zfs_open(zhp->zfs_hdl, pool,
   2241  1.4   chs 			    ZFS_TYPE_FILESYSTEM);
   2242  1.4   chs 
   2243  1.4   chs 			(void) get_clones_cb(root, &gca);
   2244  1.4   chs 		}
   2245  1.4   chs 
   2246  1.4   chs 		if (gca.numclones != 0 ||
   2247  1.4   chs 		    nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
   2248  1.4   chs 		    nvlist_add_nvlist(zhp->zfs_props,
   2249  1.4   chs 		    zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
   2250  1.4   chs 			nvlist_free(nv);
   2251  1.4   chs 			nvlist_free(value);
   2252  1.4   chs 			return (NULL);
   2253  1.4   chs 		}
   2254  1.4   chs 		nvlist_free(nv);
   2255  1.4   chs 		nvlist_free(value);
   2256  1.4   chs 		verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
   2257  1.4   chs 		    zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
   2258  1.4   chs 	}
   2259  1.4   chs 
   2260  1.4   chs 	verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
   2261  1.4   chs 
   2262  1.4   chs 	return (value);
   2263  1.4   chs }
   2264  1.4   chs 
   2265  1.1  haad /*
   2266  1.1  haad  * Retrieve a property from the given object.  If 'literal' is specified, then
   2267  1.1  haad  * numbers are left as exact values.  Otherwise, numbers are converted to a
   2268  1.1  haad  * human-readable form.
   2269  1.1  haad  *
   2270  1.1  haad  * Returns 0 on success, or -1 on error.
   2271  1.1  haad  */
   2272  1.1  haad int
   2273  1.1  haad zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
   2274  1.1  haad     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
   2275  1.1  haad {
   2276  1.1  haad 	char *source = NULL;
   2277  1.1  haad 	uint64_t val;
   2278  1.4   chs 	const char *str;
   2279  1.1  haad 	const char *strval;
   2280  1.2  haad 	boolean_t received = zfs_is_recvd_props_mode(zhp);
   2281  1.1  haad 
   2282  1.1  haad 	/*
   2283  1.1  haad 	 * Check to see if this property applies to our object
   2284  1.1  haad 	 */
   2285  1.1  haad 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
   2286  1.1  haad 		return (-1);
   2287  1.1  haad 
   2288  1.2  haad 	if (received && zfs_prop_readonly(prop))
   2289  1.2  haad 		return (-1);
   2290  1.2  haad 
   2291  1.1  haad 	if (src)
   2292  1.1  haad 		*src = ZPROP_SRC_NONE;
   2293  1.1  haad 
   2294  1.1  haad 	switch (prop) {
   2295  1.1  haad 	case ZFS_PROP_CREATION:
   2296  1.1  haad 		/*
   2297  1.1  haad 		 * 'creation' is a time_t stored in the statistics.  We convert
   2298  1.1  haad 		 * this into a string unless 'literal' is specified.
   2299  1.1  haad 		 */
   2300  1.1  haad 		{
   2301  1.1  haad 			val = getprop_uint64(zhp, prop, &source);
   2302  1.1  haad 			time_t time = (time_t)val;
   2303  1.1  haad 			struct tm t;
   2304  1.1  haad 
   2305  1.1  haad 			if (literal ||
   2306  1.1  haad 			    localtime_r(&time, &t) == NULL ||
   2307  1.1  haad 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
   2308  1.1  haad 			    &t) == 0)
   2309  1.1  haad 				(void) snprintf(propbuf, proplen, "%llu", val);
   2310  1.1  haad 		}
   2311  1.1  haad 		break;
   2312  1.1  haad 
   2313  1.1  haad 	case ZFS_PROP_MOUNTPOINT:
   2314  1.1  haad 		/*
   2315  1.1  haad 		 * Getting the precise mountpoint can be tricky.
   2316  1.1  haad 		 *
   2317  1.1  haad 		 *  - for 'none' or 'legacy', return those values.
   2318  1.1  haad 		 *  - for inherited mountpoints, we want to take everything
   2319  1.1  haad 		 *    after our ancestor and append it to the inherited value.
   2320  1.1  haad 		 *
   2321  1.1  haad 		 * If the pool has an alternate root, we want to prepend that
   2322  1.1  haad 		 * root to any values we return.
   2323  1.1  haad 		 */
   2324  1.1  haad 
   2325  1.1  haad 		str = getprop_string(zhp, prop, &source);
   2326  1.1  haad 
   2327  1.1  haad 		if (str[0] == '/') {
   2328  1.1  haad 			char buf[MAXPATHLEN];
   2329  1.1  haad 			char *root = buf;
   2330  1.2  haad 			const char *relpath;
   2331  1.1  haad 
   2332  1.2  haad 			/*
   2333  1.2  haad 			 * If we inherit the mountpoint, even from a dataset
   2334  1.2  haad 			 * with a received value, the source will be the path of
   2335  1.2  haad 			 * the dataset we inherit from. If source is
   2336  1.2  haad 			 * ZPROP_SOURCE_VAL_RECVD, the received value is not
   2337  1.2  haad 			 * inherited.
   2338  1.2  haad 			 */
   2339  1.2  haad 			if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
   2340  1.2  haad 				relpath = "";
   2341  1.2  haad 			} else {
   2342  1.2  haad 				relpath = zhp->zfs_name + strlen(source);
   2343  1.2  haad 				if (relpath[0] == '/')
   2344  1.2  haad 					relpath++;
   2345  1.2  haad 			}
   2346  1.1  haad 
   2347  1.1  haad 			if ((zpool_get_prop(zhp->zpool_hdl,
   2348  1.4   chs 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
   2349  1.4   chs 			    B_FALSE)) || (strcmp(root, "-") == 0))
   2350  1.1  haad 				root[0] = '\0';
   2351  1.1  haad 			/*
   2352  1.1  haad 			 * Special case an alternate root of '/'. This will
   2353  1.1  haad 			 * avoid having multiple leading slashes in the
   2354  1.1  haad 			 * mountpoint path.
   2355  1.1  haad 			 */
   2356  1.1  haad 			if (strcmp(root, "/") == 0)
   2357  1.1  haad 				root++;
   2358  1.1  haad 
   2359  1.1  haad 			/*
   2360  1.1  haad 			 * If the mountpoint is '/' then skip over this
   2361  1.1  haad 			 * if we are obtaining either an alternate root or
   2362  1.1  haad 			 * an inherited mountpoint.
   2363  1.1  haad 			 */
   2364  1.1  haad 			if (str[1] == '\0' && (root[0] != '\0' ||
   2365  1.1  haad 			    relpath[0] != '\0'))
   2366  1.1  haad 				str++;
   2367  1.1  haad 
   2368  1.1  haad 			if (relpath[0] == '\0')
   2369  1.1  haad 				(void) snprintf(propbuf, proplen, "%s%s",
   2370  1.1  haad 				    root, str);
   2371  1.1  haad 			else
   2372  1.1  haad 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
   2373  1.1  haad 				    root, str, relpath[0] == '@' ? "" : "/",
   2374  1.1  haad 				    relpath);
   2375  1.1  haad 		} else {
   2376  1.1  haad 			/* 'legacy' or 'none' */
   2377  1.1  haad 			(void) strlcpy(propbuf, str, proplen);
   2378  1.1  haad 		}
   2379  1.1  haad 
   2380  1.1  haad 		break;
   2381  1.1  haad 
   2382  1.1  haad 	case ZFS_PROP_ORIGIN:
   2383  1.4   chs 		str = getprop_string(zhp, prop, &source);
   2384  1.4   chs 		if (str == NULL)
   2385  1.4   chs 			return (-1);
   2386  1.4   chs 		(void) strlcpy(propbuf, str, proplen);
   2387  1.4   chs 		break;
   2388  1.4   chs 
   2389  1.4   chs 	case ZFS_PROP_CLONES:
   2390  1.4   chs 		if (get_clones_string(zhp, propbuf, proplen) != 0)
   2391  1.1  haad 			return (-1);
   2392  1.1  haad 		break;
   2393  1.1  haad 
   2394  1.1  haad 	case ZFS_PROP_QUOTA:
   2395  1.1  haad 	case ZFS_PROP_REFQUOTA:
   2396  1.1  haad 	case ZFS_PROP_RESERVATION:
   2397  1.1  haad 	case ZFS_PROP_REFRESERVATION:
   2398  1.1  haad 
   2399  1.1  haad 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
   2400  1.1  haad 			return (-1);
   2401  1.1  haad 
   2402  1.1  haad 		/*
   2403  1.1  haad 		 * If quota or reservation is 0, we translate this into 'none'
   2404  1.1  haad 		 * (unless literal is set), and indicate that it's the default
   2405  1.1  haad 		 * value.  Otherwise, we print the number nicely and indicate
   2406  1.1  haad 		 * that its set locally.
   2407  1.1  haad 		 */
   2408  1.1  haad 		if (val == 0) {
   2409  1.1  haad 			if (literal)
   2410  1.1  haad 				(void) strlcpy(propbuf, "0", proplen);
   2411  1.1  haad 			else
   2412  1.1  haad 				(void) strlcpy(propbuf, "none", proplen);
   2413  1.1  haad 		} else {
   2414  1.1  haad 			if (literal)
   2415  1.1  haad 				(void) snprintf(propbuf, proplen, "%llu",
   2416  1.1  haad 				    (u_longlong_t)val);
   2417  1.1  haad 			else
   2418  1.1  haad 				zfs_nicenum(val, propbuf, proplen);
   2419  1.1  haad 		}
   2420  1.1  haad 		break;
   2421  1.1  haad 
   2422  1.4   chs 	case ZFS_PROP_FILESYSTEM_LIMIT:
   2423  1.4   chs 	case ZFS_PROP_SNAPSHOT_LIMIT:
   2424  1.4   chs 	case ZFS_PROP_FILESYSTEM_COUNT:
   2425  1.4   chs 	case ZFS_PROP_SNAPSHOT_COUNT:
   2426  1.4   chs 
   2427  1.4   chs 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
   2428  1.4   chs 			return (-1);
   2429  1.4   chs 
   2430  1.4   chs 		/*
   2431  1.4   chs 		 * If limit is UINT64_MAX, we translate this into 'none' (unless
   2432  1.4   chs 		 * literal is set), and indicate that it's the default value.
   2433  1.4   chs 		 * Otherwise, we print the number nicely and indicate that it's
   2434  1.4   chs 		 * set locally.
   2435  1.4   chs 		 */
   2436  1.4   chs 		if (literal) {
   2437  1.4   chs 			(void) snprintf(propbuf, proplen, "%llu",
   2438  1.4   chs 			    (u_longlong_t)val);
   2439  1.4   chs 		} else if (val == UINT64_MAX) {
   2440  1.4   chs 			(void) strlcpy(propbuf, "none", proplen);
   2441  1.4   chs 		} else {
   2442  1.4   chs 			zfs_nicenum(val, propbuf, proplen);
   2443  1.4   chs 		}
   2444  1.4   chs 		break;
   2445  1.4   chs 
   2446  1.4   chs 	case ZFS_PROP_REFRATIO:
   2447  1.1  haad 	case ZFS_PROP_COMPRESSRATIO:
   2448  1.1  haad 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
   2449  1.1  haad 			return (-1);
   2450  1.2  haad 		(void) snprintf(propbuf, proplen, "%llu.%02llux",
   2451  1.2  haad 		    (u_longlong_t)(val / 100),
   2452  1.2  haad 		    (u_longlong_t)(val % 100));
   2453  1.1  haad 		break;
   2454  1.1  haad 
   2455  1.1  haad 	case ZFS_PROP_TYPE:
   2456  1.1  haad 		switch (zhp->zfs_type) {
   2457  1.1  haad 		case ZFS_TYPE_FILESYSTEM:
   2458  1.1  haad 			str = "filesystem";
   2459  1.1  haad 			break;
   2460  1.1  haad 		case ZFS_TYPE_VOLUME:
   2461  1.1  haad 			str = "volume";
   2462  1.1  haad 			break;
   2463  1.1  haad 		case ZFS_TYPE_SNAPSHOT:
   2464  1.1  haad 			str = "snapshot";
   2465  1.1  haad 			break;
   2466  1.4   chs 		case ZFS_TYPE_BOOKMARK:
   2467  1.4   chs 			str = "bookmark";
   2468  1.4   chs 			break;
   2469  1.1  haad 		default:
   2470  1.1  haad 			abort();
   2471  1.1  haad 		}
   2472  1.1  haad 		(void) snprintf(propbuf, proplen, "%s", str);
   2473  1.1  haad 		break;
   2474  1.1  haad 
   2475  1.1  haad 	case ZFS_PROP_MOUNTED:
   2476  1.1  haad 		/*
   2477  1.1  haad 		 * The 'mounted' property is a pseudo-property that described
   2478  1.1  haad 		 * whether the filesystem is currently mounted.  Even though
   2479  1.1  haad 		 * it's a boolean value, the typical values of "on" and "off"
   2480  1.1  haad 		 * don't make sense, so we translate to "yes" and "no".
   2481  1.1  haad 		 */
   2482  1.1  haad 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
   2483  1.1  haad 		    src, &source, &val) != 0)
   2484  1.1  haad 			return (-1);
   2485  1.1  haad 		if (val)
   2486  1.1  haad 			(void) strlcpy(propbuf, "yes", proplen);
   2487  1.1  haad 		else
   2488  1.1  haad 			(void) strlcpy(propbuf, "no", proplen);
   2489  1.1  haad 		break;
   2490  1.1  haad 
   2491  1.1  haad 	case ZFS_PROP_NAME:
   2492  1.1  haad 		/*
   2493  1.1  haad 		 * The 'name' property is a pseudo-property derived from the
   2494  1.1  haad 		 * dataset name.  It is presented as a real property to simplify
   2495  1.1  haad 		 * consumers.
   2496  1.1  haad 		 */
   2497  1.1  haad 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
   2498  1.1  haad 		break;
   2499  1.4   chs 
   2500  1.2  haad 	case ZFS_PROP_MLSLABEL:
   2501  1.2  haad 		{
   2502  1.4   chs #ifdef illumos
   2503  1.2  haad 			m_label_t *new_sl = NULL;
   2504  1.2  haad 			char *ascii = NULL;	/* human readable label */
   2505  1.2  haad 
   2506  1.2  haad 			(void) strlcpy(propbuf,
   2507  1.2  haad 			    getprop_string(zhp, prop, &source), proplen);
   2508  1.2  haad 
   2509  1.2  haad 			if (literal || (strcasecmp(propbuf,
   2510  1.2  haad 			    ZFS_MLSLABEL_DEFAULT) == 0))
   2511  1.2  haad 				break;
   2512  1.1  haad 
   2513  1.2  haad 			/*
   2514  1.2  haad 			 * Try to translate the internal hex string to
   2515  1.2  haad 			 * human-readable output.  If there are any
   2516  1.2  haad 			 * problems just use the hex string.
   2517  1.2  haad 			 */
   2518  1.2  haad 
   2519  1.2  haad 			if (str_to_label(propbuf, &new_sl, MAC_LABEL,
   2520  1.2  haad 			    L_NO_CORRECTION, NULL) == -1) {
   2521  1.2  haad 				m_label_free(new_sl);
   2522  1.2  haad 				break;
   2523  1.2  haad 			}
   2524  1.2  haad 
   2525  1.2  haad 			if (label_to_str(new_sl, &ascii, M_LABEL,
   2526  1.2  haad 			    DEF_NAMES) != 0) {
   2527  1.2  haad 				if (ascii)
   2528  1.2  haad 					free(ascii);
   2529  1.2  haad 				m_label_free(new_sl);
   2530  1.2  haad 				break;
   2531  1.2  haad 			}
   2532  1.2  haad 			m_label_free(new_sl);
   2533  1.2  haad 
   2534  1.2  haad 			(void) strlcpy(propbuf, ascii, proplen);
   2535  1.2  haad 			free(ascii);
   2536  1.4   chs #else	/* !illumos */
   2537  1.4   chs 			propbuf[0] = '\0';
   2538  1.4   chs #endif	/* illumos */
   2539  1.2  haad 		}
   2540  1.2  haad 		break;
   2541  1.4   chs 
   2542  1.4   chs 	case ZFS_PROP_GUID:
   2543  1.4   chs 		/*
   2544  1.4   chs 		 * GUIDs are stored as numbers, but they are identifiers.
   2545  1.4   chs 		 * We don't want them to be pretty printed, because pretty
   2546  1.4   chs 		 * printing mangles the ID into a truncated and useless value.
   2547  1.4   chs 		 */
   2548  1.4   chs 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
   2549  1.4   chs 			return (-1);
   2550  1.4   chs 		(void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
   2551  1.4   chs 		break;
   2552  1.4   chs 
   2553  1.1  haad 	default:
   2554  1.1  haad 		switch (zfs_prop_get_type(prop)) {
   2555  1.1  haad 		case PROP_TYPE_NUMBER:
   2556  1.1  haad 			if (get_numeric_property(zhp, prop, src,
   2557  1.1  haad 			    &source, &val) != 0)
   2558  1.1  haad 				return (-1);
   2559  1.1  haad 			if (literal)
   2560  1.1  haad 				(void) snprintf(propbuf, proplen, "%llu",
   2561  1.1  haad 				    (u_longlong_t)val);
   2562  1.1  haad 			else
   2563  1.1  haad 				zfs_nicenum(val, propbuf, proplen);
   2564  1.1  haad 			break;
   2565  1.1  haad 
   2566  1.1  haad 		case PROP_TYPE_STRING:
   2567  1.4   chs 			str = getprop_string(zhp, prop, &source);
   2568  1.4   chs 			if (str == NULL)
   2569  1.4   chs 				return (-1);
   2570  1.4   chs 			(void) strlcpy(propbuf, str, proplen);
   2571  1.1  haad 			break;
   2572  1.1  haad 
   2573  1.1  haad 		case PROP_TYPE_INDEX:
   2574  1.1  haad 			if (get_numeric_property(zhp, prop, src,
   2575  1.1  haad 			    &source, &val) != 0)
   2576  1.1  haad 				return (-1);
   2577  1.1  haad 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
   2578  1.1  haad 				return (-1);
   2579  1.1  haad 			(void) strlcpy(propbuf, strval, proplen);
   2580  1.1  haad 			break;
   2581  1.1  haad 
   2582  1.1  haad 		default:
   2583  1.1  haad 			abort();
   2584  1.1  haad 		}
   2585  1.1  haad 	}
   2586  1.1  haad 
   2587  1.1  haad 	get_source(zhp, src, source, statbuf, statlen);
   2588  1.1  haad 
   2589  1.1  haad 	return (0);
   2590  1.1  haad }
   2591  1.1  haad 
   2592  1.1  haad /*
   2593  1.1  haad  * Utility function to get the given numeric property.  Does no validation that
   2594  1.1  haad  * the given property is the appropriate type; should only be used with
   2595  1.1  haad  * hard-coded property types.
   2596  1.1  haad  */
   2597  1.1  haad uint64_t
   2598  1.1  haad zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
   2599  1.1  haad {
   2600  1.2  haad 	char *source;
   2601  1.2  haad 	uint64_t val;
   2602  1.2  haad 
   2603  1.2  haad 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
   2604  1.2  haad 
   2605  1.2  haad 	return (val);
   2606  1.2  haad }
   2607  1.2  haad 
   2608  1.2  haad int
   2609  1.2  haad zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
   2610  1.2  haad {
   2611  1.2  haad 	char buf[64];
   2612  1.2  haad 
   2613  1.2  haad 	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
   2614  1.2  haad 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
   2615  1.2  haad }
   2616  1.2  haad 
   2617  1.2  haad /*
   2618  1.2  haad  * Similar to zfs_prop_get(), but returns the value as an integer.
   2619  1.2  haad  */
   2620  1.2  haad int
   2621  1.2  haad zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
   2622  1.2  haad     zprop_source_t *src, char *statbuf, size_t statlen)
   2623  1.2  haad {
   2624  1.2  haad 	char *source;
   2625  1.2  haad 
   2626  1.2  haad 	/*
   2627  1.2  haad 	 * Check to see if this property applies to our object
   2628  1.2  haad 	 */
   2629  1.2  haad 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
   2630  1.2  haad 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
   2631  1.2  haad 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
   2632  1.2  haad 		    zfs_prop_to_name(prop)));
   2633  1.2  haad 	}
   2634  1.2  haad 
   2635  1.2  haad 	if (src)
   2636  1.2  haad 		*src = ZPROP_SRC_NONE;
   2637  1.2  haad 
   2638  1.2  haad 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
   2639  1.2  haad 		return (-1);
   2640  1.2  haad 
   2641  1.2  haad 	get_source(zhp, src, source, statbuf, statlen);
   2642  1.2  haad 
   2643  1.2  haad 	return (0);
   2644  1.2  haad }
   2645  1.2  haad 
   2646  1.2  haad static int
   2647  1.2  haad idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
   2648  1.2  haad     char **domainp, idmap_rid_t *ridp)
   2649  1.2  haad {
   2650  1.4   chs #ifdef illumos
   2651  1.2  haad 	idmap_get_handle_t *get_hdl = NULL;
   2652  1.2  haad 	idmap_stat status;
   2653  1.2  haad 	int err = EINVAL;
   2654  1.2  haad 
   2655  1.4   chs 	if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
   2656  1.2  haad 		goto out;
   2657  1.2  haad 
   2658  1.2  haad 	if (isuser) {
   2659  1.2  haad 		err = idmap_get_sidbyuid(get_hdl, id,
   2660  1.2  haad 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
   2661  1.2  haad 	} else {
   2662  1.2  haad 		err = idmap_get_sidbygid(get_hdl, id,
   2663  1.2  haad 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
   2664  1.2  haad 	}
   2665  1.2  haad 	if (err == IDMAP_SUCCESS &&
   2666  1.2  haad 	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
   2667  1.2  haad 	    status == IDMAP_SUCCESS)
   2668  1.2  haad 		err = 0;
   2669  1.2  haad 	else
   2670  1.2  haad 		err = EINVAL;
   2671  1.2  haad out:
   2672  1.2  haad 	if (get_hdl)
   2673  1.2  haad 		idmap_get_destroy(get_hdl);
   2674  1.2  haad 	return (err);
   2675  1.4   chs #else	/* !illumos */
   2676  1.4   chs 	assert(!"invalid code path");
   2677  1.4   chs 	return (EINVAL); // silence compiler warning
   2678  1.4   chs #endif	/* illumos */
   2679  1.2  haad }
   2680  1.2  haad 
   2681  1.2  haad /*
   2682  1.2  haad  * convert the propname into parameters needed by kernel
   2683  1.2  haad  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
   2684  1.2  haad  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
   2685  1.2  haad  */
   2686  1.2  haad static int
   2687  1.2  haad userquota_propname_decode(const char *propname, boolean_t zoned,
   2688  1.2  haad     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
   2689  1.2  haad {
   2690  1.2  haad 	zfs_userquota_prop_t type;
   2691  1.2  haad 	char *cp, *end;
   2692  1.2  haad 	char *numericsid = NULL;
   2693  1.2  haad 	boolean_t isuser;
   2694  1.2  haad 
   2695  1.2  haad 	domain[0] = '\0';
   2696  1.4   chs 	*ridp = 0;
   2697  1.2  haad 	/* Figure out the property type ({user|group}{quota|space}) */
   2698  1.2  haad 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
   2699  1.2  haad 		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
   2700  1.2  haad 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
   2701  1.2  haad 			break;
   2702  1.2  haad 	}
   2703  1.2  haad 	if (type == ZFS_NUM_USERQUOTA_PROPS)
   2704  1.2  haad 		return (EINVAL);
   2705  1.2  haad 	*typep = type;
   2706  1.2  haad 
   2707  1.2  haad 	isuser = (type == ZFS_PROP_USERQUOTA ||
   2708  1.2  haad 	    type == ZFS_PROP_USERUSED);
   2709  1.2  haad 
   2710  1.2  haad 	cp = strchr(propname, '@') + 1;
   2711  1.2  haad 
   2712  1.2  haad 	if (strchr(cp, '@')) {
   2713  1.4   chs #ifdef illumos
   2714  1.2  haad 		/*
   2715  1.2  haad 		 * It's a SID name (eg "user@domain") that needs to be
   2716  1.2  haad 		 * turned into S-1-domainID-RID.
   2717  1.2  haad 		 */
   2718  1.4   chs 		int flag = 0;
   2719  1.4   chs 		idmap_stat stat, map_stat;
   2720  1.4   chs 		uid_t pid;
   2721  1.4   chs 		idmap_rid_t rid;
   2722  1.4   chs 		idmap_get_handle_t *gh = NULL;
   2723  1.4   chs 
   2724  1.4   chs 		stat = idmap_get_create(&gh);
   2725  1.4   chs 		if (stat != IDMAP_SUCCESS) {
   2726  1.4   chs 			idmap_get_destroy(gh);
   2727  1.4   chs 			return (ENOMEM);
   2728  1.4   chs 		}
   2729  1.2  haad 		if (zoned && getzoneid() == GLOBAL_ZONEID)
   2730  1.2  haad 			return (ENOENT);
   2731  1.2  haad 		if (isuser) {
   2732  1.4   chs 			stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
   2733  1.4   chs 			if (stat < 0)
   2734  1.4   chs 				return (ENOENT);
   2735  1.4   chs 			stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
   2736  1.4   chs 			    &rid, &map_stat);
   2737  1.2  haad 		} else {
   2738  1.4   chs 			stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
   2739  1.4   chs 			if (stat < 0)
   2740  1.4   chs 				return (ENOENT);
   2741  1.4   chs 			stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
   2742  1.4   chs 			    &rid, &map_stat);
   2743  1.4   chs 		}
   2744  1.4   chs 		if (stat < 0) {
   2745  1.4   chs 			idmap_get_destroy(gh);
   2746  1.4   chs 			return (ENOENT);
   2747  1.2  haad 		}
   2748  1.4   chs 		stat = idmap_get_mappings(gh);
   2749  1.4   chs 		idmap_get_destroy(gh);
   2750  1.4   chs 
   2751  1.4   chs 		if (stat < 0) {
   2752  1.2  haad 			return (ENOENT);
   2753  1.2  haad 		}
   2754  1.2  haad 		if (numericsid == NULL)
   2755  1.2  haad 			return (ENOENT);
   2756  1.2  haad 		cp = numericsid;
   2757  1.4   chs 		*ridp = rid;
   2758  1.2  haad 		/* will be further decoded below */
   2759  1.4   chs #else	/* !illumos */
   2760  1.4   chs 		return (ENOENT);
   2761  1.4   chs #endif	/* illumos */
   2762  1.2  haad 	}
   2763  1.2  haad 
   2764  1.2  haad 	if (strncmp(cp, "S-1-", 4) == 0) {
   2765  1.2  haad 		/* It's a numeric SID (eg "S-1-234-567-89") */
   2766  1.2  haad 		(void) strlcpy(domain, cp, domainlen);
   2767  1.2  haad 		errno = 0;
   2768  1.4   chs 		if (*ridp == 0) {
   2769  1.4   chs 			cp = strrchr(domain, '-');
   2770  1.4   chs 			*cp = '\0';
   2771  1.4   chs 			cp++;
   2772  1.4   chs 			*ridp = strtoull(cp, &end, 10);
   2773  1.4   chs 		} else {
   2774  1.4   chs 			end = "";
   2775  1.4   chs 		}
   2776  1.2  haad 		if (numericsid) {
   2777  1.2  haad 			free(numericsid);
   2778  1.2  haad 			numericsid = NULL;
   2779  1.2  haad 		}
   2780  1.2  haad 		if (errno != 0 || *end != '\0')
   2781  1.2  haad 			return (EINVAL);
   2782  1.2  haad 	} else if (!isdigit(*cp)) {
   2783  1.2  haad 		/*
   2784  1.2  haad 		 * It's a user/group name (eg "user") that needs to be
   2785  1.2  haad 		 * turned into a uid/gid
   2786  1.2  haad 		 */
   2787  1.2  haad 		if (zoned && getzoneid() == GLOBAL_ZONEID)
   2788  1.2  haad 			return (ENOENT);
   2789  1.2  haad 		if (isuser) {
   2790  1.2  haad 			struct passwd *pw;
   2791  1.2  haad 			pw = getpwnam(cp);
   2792  1.2  haad 			if (pw == NULL)
   2793  1.2  haad 				return (ENOENT);
   2794  1.2  haad 			*ridp = pw->pw_uid;
   2795  1.2  haad 		} else {
   2796  1.2  haad 			struct group *gr;
   2797  1.2  haad 			gr = getgrnam(cp);
   2798  1.2  haad 			if (gr == NULL)
   2799  1.2  haad 				return (ENOENT);
   2800  1.2  haad 			*ridp = gr->gr_gid;
   2801  1.2  haad 		}
   2802  1.2  haad 	} else {
   2803  1.2  haad 		/* It's a user/group ID (eg "12345"). */
   2804  1.2  haad 		uid_t id = strtoul(cp, &end, 10);
   2805  1.2  haad 		idmap_rid_t rid;
   2806  1.2  haad 		char *mapdomain;
   2807  1.2  haad 
   2808  1.2  haad 		if (*end != '\0')
   2809  1.2  haad 			return (EINVAL);
   2810  1.2  haad 		if (id > MAXUID) {
   2811  1.2  haad 			/* It's an ephemeral ID. */
   2812  1.2  haad 			if (idmap_id_to_numeric_domain_rid(id, isuser,
   2813  1.2  haad 			    &mapdomain, &rid) != 0)
   2814  1.2  haad 				return (ENOENT);
   2815  1.2  haad 			(void) strlcpy(domain, mapdomain, domainlen);
   2816  1.2  haad 			*ridp = rid;
   2817  1.2  haad 		} else {
   2818  1.2  haad 			*ridp = id;
   2819  1.2  haad 		}
   2820  1.2  haad 	}
   2821  1.2  haad 
   2822  1.2  haad 	ASSERT3P(numericsid, ==, NULL);
   2823  1.2  haad 	return (0);
   2824  1.2  haad }
   2825  1.2  haad 
   2826  1.2  haad static int
   2827  1.2  haad zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
   2828  1.2  haad     uint64_t *propvalue, zfs_userquota_prop_t *typep)
   2829  1.2  haad {
   2830  1.2  haad 	int err;
   2831  1.2  haad 	zfs_cmd_t zc = { 0 };
   2832  1.2  haad 
   2833  1.4   chs 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
   2834  1.2  haad 
   2835  1.2  haad 	err = userquota_propname_decode(propname,
   2836  1.2  haad 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
   2837  1.2  haad 	    typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
   2838  1.2  haad 	zc.zc_objset_type = *typep;
   2839  1.2  haad 	if (err)
   2840  1.2  haad 		return (err);
   2841  1.1  haad 
   2842  1.2  haad 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
   2843  1.2  haad 	if (err)
   2844  1.2  haad 		return (err);
   2845  1.1  haad 
   2846  1.2  haad 	*propvalue = zc.zc_cookie;
   2847  1.2  haad 	return (0);
   2848  1.1  haad }
   2849  1.1  haad 
   2850  1.1  haad int
   2851  1.2  haad zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
   2852  1.2  haad     uint64_t *propvalue)
   2853  1.1  haad {
   2854  1.2  haad 	zfs_userquota_prop_t type;
   2855  1.1  haad 
   2856  1.2  haad 	return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
   2857  1.2  haad 	    &type));
   2858  1.1  haad }
   2859  1.1  haad 
   2860  1.1  haad int
   2861  1.2  haad zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
   2862  1.2  haad     char *propbuf, int proplen, boolean_t literal)
   2863  1.1  haad {
   2864  1.2  haad 	int err;
   2865  1.2  haad 	uint64_t propvalue;
   2866  1.2  haad 	zfs_userquota_prop_t type;
   2867  1.1  haad 
   2868  1.2  haad 	err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
   2869  1.2  haad 	    &type);
   2870  1.1  haad 
   2871  1.2  haad 	if (err)
   2872  1.2  haad 		return (err);
   2873  1.1  haad 
   2874  1.2  haad 	if (literal) {
   2875  1.2  haad 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
   2876  1.2  haad 	} else if (propvalue == 0 &&
   2877  1.2  haad 	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
   2878  1.2  haad 		(void) strlcpy(propbuf, "none", proplen);
   2879  1.2  haad 	} else {
   2880  1.2  haad 		zfs_nicenum(propvalue, propbuf, proplen);
   2881  1.2  haad 	}
   2882  1.1  haad 	return (0);
   2883  1.1  haad }
   2884  1.1  haad 
   2885  1.4   chs int
   2886  1.4   chs zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
   2887  1.4   chs     uint64_t *propvalue)
   2888  1.1  haad {
   2889  1.4   chs 	int err;
   2890  1.4   chs 	zfs_cmd_t zc = { 0 };
   2891  1.4   chs 	const char *snapname;
   2892  1.4   chs 
   2893  1.4   chs 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
   2894  1.1  haad 
   2895  1.4   chs 	snapname = strchr(propname, '@') + 1;
   2896  1.4   chs 	if (strchr(snapname, '@')) {
   2897  1.4   chs 		(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
   2898  1.4   chs 	} else {
   2899  1.4   chs 		/* snapname is the short name, append it to zhp's fsname */
   2900  1.4   chs 		char *cp;
   2901  1.1  haad 
   2902  1.4   chs 		(void) strlcpy(zc.zc_value, zhp->zfs_name,
   2903  1.4   chs 		    sizeof (zc.zc_value));
   2904  1.4   chs 		cp = strchr(zc.zc_value, '@');
   2905  1.4   chs 		if (cp != NULL)
   2906  1.4   chs 			*cp = '\0';
   2907  1.4   chs 		(void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
   2908  1.4   chs 		(void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
   2909  1.4   chs 	}
   2910  1.2  haad 
   2911  1.4   chs 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
   2912  1.4   chs 	if (err)
   2913  1.4   chs 		return (err);
   2914  1.2  haad 
   2915  1.4   chs 	*propvalue = zc.zc_cookie;
   2916  1.4   chs 	return (0);
   2917  1.2  haad }
   2918  1.2  haad 
   2919  1.1  haad int
   2920  1.4   chs zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
   2921  1.4   chs     char *propbuf, int proplen, boolean_t literal)
   2922  1.1  haad {
   2923  1.4   chs 	int err;
   2924  1.4   chs 	uint64_t propvalue;
   2925  1.1  haad 
   2926  1.4   chs 	err = zfs_prop_get_written_int(zhp, propname, &propvalue);
   2927  1.1  haad 
   2928  1.4   chs 	if (err)
   2929  1.4   chs 		return (err);
   2930  1.1  haad 
   2931  1.4   chs 	if (literal) {
   2932  1.4   chs 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
   2933  1.4   chs 	} else {
   2934  1.4   chs 		zfs_nicenum(propvalue, propbuf, proplen);
   2935  1.1  haad 	}
   2936  1.4   chs 	return (0);
   2937  1.1  haad }
   2938  1.1  haad 
   2939  1.1  haad /*
   2940  1.4   chs  * Returns the name of the given zfs handle.
   2941  1.1  haad  */
   2942  1.4   chs const char *
   2943  1.4   chs zfs_get_name(const zfs_handle_t *zhp)
   2944  1.1  haad {
   2945  1.4   chs 	return (zhp->zfs_name);
   2946  1.4   chs }
   2947  1.1  haad 
   2948  1.4   chs /*
   2949  1.4   chs  * Returns the name of the parent pool for the given zfs handle.
   2950  1.4   chs  */
   2951  1.4   chs const char *
   2952  1.4   chs zfs_get_pool_name(const zfs_handle_t *zhp)
   2953  1.4   chs {
   2954  1.4   chs 	return (zhp->zpool_hdl->zpool_name);
   2955  1.1  haad }
   2956  1.1  haad 
   2957  1.1  haad /*
   2958  1.4   chs  * Returns the type of the given zfs handle.
   2959  1.1  haad  */
   2960  1.4   chs zfs_type_t
   2961  1.4   chs zfs_get_type(const zfs_handle_t *zhp)
   2962  1.1  haad {
   2963  1.4   chs 	return (zhp->zfs_type);
   2964  1.1  haad }
   2965  1.1  haad 
   2966  1.1  haad /*
   2967  1.2  haad  * Is one dataset name a child dataset of another?
   2968  1.2  haad  *
   2969  1.2  haad  * Needs to handle these cases:
   2970  1.2  haad  * Dataset 1	"a/foo"		"a/foo"		"a/foo"		"a/foo"
   2971  1.2  haad  * Dataset 2	"a/fo"		"a/foobar"	"a/bar/baz"	"a/foo/bar"
   2972  1.2  haad  * Descendant?	No.		No.		No.		Yes.
   2973  1.2  haad  */
   2974  1.2  haad static boolean_t
   2975  1.2  haad is_descendant(const char *ds1, const char *ds2)
   2976  1.2  haad {
   2977  1.2  haad 	size_t d1len = strlen(ds1);
   2978  1.2  haad 
   2979  1.2  haad 	/* ds2 can't be a descendant if it's smaller */
   2980  1.2  haad 	if (strlen(ds2) < d1len)
   2981  1.2  haad 		return (B_FALSE);
   2982  1.2  haad 
   2983  1.2  haad 	/* otherwise, compare strings and verify that there's a '/' char */
   2984  1.2  haad 	return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
   2985  1.2  haad }
   2986  1.2  haad 
   2987  1.2  haad /*
   2988  1.1  haad  * Given a complete name, return just the portion that refers to the parent.
   2989  1.4   chs  * Will return -1 if there is no parent (path is just the name of the
   2990  1.4   chs  * pool).
   2991  1.1  haad  */
   2992  1.1  haad static int
   2993  1.1  haad parent_name(const char *path, char *buf, size_t buflen)
   2994  1.1  haad {
   2995  1.4   chs 	char *slashp;
   2996  1.1  haad 
   2997  1.4   chs 	(void) strlcpy(buf, path, buflen);
   2998  1.4   chs 
   2999  1.4   chs 	if ((slashp = strrchr(buf, '/')) == NULL)
   3000  1.1  haad 		return (-1);
   3001  1.4   chs 	*slashp = '\0';
   3002  1.1  haad 
   3003  1.1  haad 	return (0);
   3004  1.1  haad }
   3005  1.1  haad 
   3006  1.1  haad /*
   3007  1.1  haad  * If accept_ancestor is false, then check to make sure that the given path has
   3008  1.1  haad  * a parent, and that it exists.  If accept_ancestor is true, then find the
   3009  1.1  haad  * closest existing ancestor for the given path.  In prefixlen return the
   3010  1.1  haad  * length of already existing prefix of the given path.  We also fetch the
   3011  1.1  haad  * 'zoned' property, which is used to validate property settings when creating
   3012  1.1  haad  * new datasets.
   3013  1.1  haad  */
   3014  1.1  haad static int
   3015  1.1  haad check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
   3016  1.1  haad     boolean_t accept_ancestor, int *prefixlen)
   3017  1.1  haad {
   3018  1.1  haad 	zfs_cmd_t zc = { 0 };
   3019  1.4   chs 	char parent[ZFS_MAX_DATASET_NAME_LEN];
   3020  1.1  haad 	char *slash;
   3021  1.1  haad 	zfs_handle_t *zhp;
   3022  1.1  haad 	char errbuf[1024];
   3023  1.2  haad 	uint64_t is_zoned;
   3024  1.1  haad 
   3025  1.2  haad 	(void) snprintf(errbuf, sizeof (errbuf),
   3026  1.2  haad 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
   3027  1.1  haad 
   3028  1.1  haad 	/* get parent, and check to see if this is just a pool */
   3029  1.1  haad 	if (parent_name(path, parent, sizeof (parent)) != 0) {
   3030  1.1  haad 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3031  1.1  haad 		    "missing dataset name"));
   3032  1.1  haad 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
   3033  1.1  haad 	}
   3034  1.1  haad 
   3035  1.1  haad 	/* check to see if the pool exists */
   3036  1.1  haad 	if ((slash = strchr(parent, '/')) == NULL)
   3037  1.1  haad 		slash = parent + strlen(parent);
   3038  1.1  haad 	(void) strncpy(zc.zc_name, parent, slash - parent);
   3039  1.1  haad 	zc.zc_name[slash - parent] = '\0';
   3040  1.1  haad 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
   3041  1.1  haad 	    errno == ENOENT) {
   3042  1.1  haad 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3043  1.1  haad 		    "no such pool '%s'"), zc.zc_name);
   3044  1.1  haad 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
   3045  1.1  haad 	}
   3046  1.1  haad 
   3047  1.1  haad 	/* check to see if the parent dataset exists */
   3048  1.1  haad 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
   3049  1.1  haad 		if (errno == ENOENT && accept_ancestor) {
   3050  1.1  haad 			/*
   3051  1.1  haad 			 * Go deeper to find an ancestor, give up on top level.
   3052  1.1  haad 			 */
   3053  1.1  haad 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
   3054  1.1  haad 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3055  1.1  haad 				    "no such pool '%s'"), zc.zc_name);
   3056  1.1  haad 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
   3057  1.1  haad 			}
   3058  1.1  haad 		} else if (errno == ENOENT) {
   3059  1.1  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3060  1.1  haad 			    "parent does not exist"));
   3061  1.1  haad 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
   3062  1.1  haad 		} else
   3063  1.1  haad 			return (zfs_standard_error(hdl, errno, errbuf));
   3064  1.1  haad 	}
   3065  1.1  haad 
   3066  1.2  haad 	is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
   3067  1.2  haad 	if (zoned != NULL)
   3068  1.2  haad 		*zoned = is_zoned;
   3069  1.2  haad 
   3070  1.1  haad 	/* we are in a non-global zone, but parent is in the global zone */
   3071  1.2  haad 	if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
   3072  1.1  haad 		(void) zfs_standard_error(hdl, EPERM, errbuf);
   3073  1.1  haad 		zfs_close(zhp);
   3074  1.1  haad 		return (-1);
   3075  1.1  haad 	}
   3076  1.1  haad 
   3077  1.1  haad 	/* make sure parent is a filesystem */
   3078  1.1  haad 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
   3079  1.1  haad 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3080  1.1  haad 		    "parent is not a filesystem"));
   3081  1.1  haad 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
   3082  1.1  haad 		zfs_close(zhp);
   3083  1.1  haad 		return (-1);
   3084  1.1  haad 	}
   3085  1.1  haad 
   3086  1.1  haad 	zfs_close(zhp);
   3087  1.1  haad 	if (prefixlen != NULL)
   3088  1.1  haad 		*prefixlen = strlen(parent);
   3089  1.1  haad 	return (0);
   3090  1.1  haad }
   3091  1.1  haad 
   3092  1.1  haad /*
   3093  1.1  haad  * Finds whether the dataset of the given type(s) exists.
   3094  1.1  haad  */
   3095  1.1  haad boolean_t
   3096  1.1  haad zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
   3097  1.1  haad {
   3098  1.1  haad 	zfs_handle_t *zhp;
   3099  1.1  haad 
   3100  1.1  haad 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
   3101  1.1  haad 		return (B_FALSE);
   3102  1.1  haad 
   3103  1.1  haad 	/*
   3104  1.1  haad 	 * Try to get stats for the dataset, which will tell us if it exists.
   3105  1.1  haad 	 */
   3106  1.1  haad 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
   3107  1.1  haad 		int ds_type = zhp->zfs_type;
   3108  1.1  haad 
   3109  1.1  haad 		zfs_close(zhp);
   3110  1.1  haad 		if (types & ds_type)
   3111  1.1  haad 			return (B_TRUE);
   3112  1.1  haad 	}
   3113  1.1  haad 	return (B_FALSE);
   3114  1.1  haad }
   3115  1.1  haad 
   3116  1.1  haad /*
   3117  1.1  haad  * Given a path to 'target', create all the ancestors between
   3118  1.1  haad  * the prefixlen portion of the path, and the target itself.
   3119  1.1  haad  * Fail if the initial prefixlen-ancestor does not already exist.
   3120  1.1  haad  */
   3121  1.1  haad int
   3122  1.1  haad create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
   3123  1.1  haad {
   3124  1.1  haad 	zfs_handle_t *h;
   3125  1.1  haad 	char *cp;
   3126  1.1  haad 	const char *opname;
   3127  1.1  haad 
   3128  1.1  haad 	/* make sure prefix exists */
   3129  1.1  haad 	cp = target + prefixlen;
   3130  1.1  haad 	if (*cp != '/') {
   3131  1.1  haad 		assert(strchr(cp, '/') == NULL);
   3132  1.1  haad 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
   3133  1.1  haad 	} else {
   3134  1.1  haad 		*cp = '\0';
   3135  1.1  haad 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
   3136  1.1  haad 		*cp = '/';
   3137  1.1  haad 	}
   3138  1.1  haad 	if (h == NULL)
   3139  1.1  haad 		return (-1);
   3140  1.1  haad 	zfs_close(h);
   3141  1.1  haad 
   3142  1.1  haad 	/*
   3143  1.1  haad 	 * Attempt to create, mount, and share any ancestor filesystems,
   3144  1.1  haad 	 * up to the prefixlen-long one.
   3145  1.1  haad 	 */
   3146  1.1  haad 	for (cp = target + prefixlen + 1;
   3147  1.4   chs 	    (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
   3148  1.1  haad 
   3149  1.1  haad 		*cp = '\0';
   3150  1.1  haad 
   3151  1.1  haad 		h = make_dataset_handle(hdl, target);
   3152  1.1  haad 		if (h) {
   3153  1.1  haad 			/* it already exists, nothing to do here */
   3154  1.1  haad 			zfs_close(h);
   3155  1.1  haad 			continue;
   3156  1.1  haad 		}
   3157  1.1  haad 
   3158  1.1  haad 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
   3159  1.1  haad 		    NULL) != 0) {
   3160  1.1  haad 			opname = dgettext(TEXT_DOMAIN, "create");
   3161  1.1  haad 			goto ancestorerr;
   3162  1.1  haad 		}
   3163  1.1  haad 
   3164  1.1  haad 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
   3165  1.1  haad 		if (h == NULL) {
   3166  1.1  haad 			opname = dgettext(TEXT_DOMAIN, "open");
   3167  1.1  haad 			goto ancestorerr;
   3168  1.1  haad 		}
   3169  1.1  haad 
   3170  1.1  haad 		if (zfs_mount(h, NULL, 0) != 0) {
   3171  1.1  haad 			opname = dgettext(TEXT_DOMAIN, "mount");
   3172  1.1  haad 			goto ancestorerr;
   3173  1.1  haad 		}
   3174  1.1  haad 
   3175  1.1  haad 		if (zfs_share(h) != 0) {
   3176  1.1  haad 			opname = dgettext(TEXT_DOMAIN, "share");
   3177  1.1  haad 			goto ancestorerr;
   3178  1.1  haad 		}
   3179  1.1  haad 
   3180  1.1  haad 		zfs_close(h);
   3181  1.1  haad 	}
   3182  1.1  haad 
   3183  1.1  haad 	return (0);
   3184  1.1  haad 
   3185  1.1  haad ancestorerr:
   3186  1.1  haad 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3187  1.1  haad 	    "failed to %s ancestor '%s'"), opname, target);
   3188  1.1  haad 	return (-1);
   3189  1.1  haad }
   3190  1.1  haad 
   3191  1.1  haad /*
   3192  1.1  haad  * Creates non-existing ancestors of the given path.
   3193  1.1  haad  */
   3194  1.1  haad int
   3195  1.1  haad zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
   3196  1.1  haad {
   3197  1.1  haad 	int prefix;
   3198  1.1  haad 	char *path_copy;
   3199  1.4   chs 	int rc = 0;
   3200  1.1  haad 
   3201  1.2  haad 	if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
   3202  1.1  haad 		return (-1);
   3203  1.1  haad 
   3204  1.1  haad 	if ((path_copy = strdup(path)) != NULL) {
   3205  1.1  haad 		rc = create_parents(hdl, path_copy, prefix);
   3206  1.1  haad 		free(path_copy);
   3207  1.1  haad 	}
   3208  1.1  haad 	if (path_copy == NULL || rc != 0)
   3209  1.1  haad 		return (-1);
   3210  1.1  haad 
   3211  1.1  haad 	return (0);
   3212  1.1  haad }
   3213  1.1  haad 
   3214  1.1  haad /*
   3215  1.1  haad  * Create a new filesystem or volume.
   3216  1.1  haad  */
   3217  1.1  haad int
   3218  1.1  haad zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
   3219  1.1  haad     nvlist_t *props)
   3220  1.1  haad {
   3221  1.1  haad 	int ret;
   3222  1.1  haad 	uint64_t size = 0;
   3223  1.1  haad 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
   3224  1.1  haad 	char errbuf[1024];
   3225  1.1  haad 	uint64_t zoned;
   3226  1.4   chs 	enum lzc_dataset_type ost;
   3227  1.1  haad 
   3228  1.1  haad 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
   3229  1.1  haad 	    "cannot create '%s'"), path);
   3230  1.1  haad 
   3231  1.1  haad 	/* validate the path, taking care to note the extended error message */
   3232  1.1  haad 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
   3233  1.1  haad 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
   3234  1.1  haad 
   3235  1.1  haad 	/* validate parents exist */
   3236  1.1  haad 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
   3237  1.1  haad 		return (-1);
   3238  1.1  haad 
   3239  1.1  haad 	/*
   3240  1.1  haad 	 * The failure modes when creating a dataset of a different type over
   3241  1.1  haad 	 * one that already exists is a little strange.  In particular, if you
   3242  1.1  haad 	 * try to create a dataset on top of an existing dataset, the ioctl()
   3243  1.1  haad 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
   3244  1.1  haad 	 * first try to see if the dataset exists.
   3245  1.1  haad 	 */
   3246  1.4   chs 	if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
   3247  1.1  haad 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3248  1.1  haad 		    "dataset already exists"));
   3249  1.1  haad 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
   3250  1.1  haad 	}
   3251  1.1  haad 
   3252  1.1  haad 	if (type == ZFS_TYPE_VOLUME)
   3253  1.4   chs 		ost = LZC_DATSET_TYPE_ZVOL;
   3254  1.1  haad 	else
   3255  1.4   chs 		ost = LZC_DATSET_TYPE_ZFS;
   3256  1.4   chs 
   3257  1.4   chs 	/* open zpool handle for prop validation */
   3258  1.4   chs 	char pool_path[ZFS_MAX_DATASET_NAME_LEN];
   3259  1.4   chs 	(void) strlcpy(pool_path, path, sizeof (pool_path));
   3260  1.4   chs 
   3261  1.4   chs 	/* truncate pool_path at first slash */
   3262  1.4   chs 	char *p = strchr(pool_path, '/');
   3263  1.4   chs 	if (p != NULL)
   3264  1.4   chs 		*p = '\0';
   3265  1.4   chs 
   3266  1.4   chs 	zpool_handle_t *zpool_handle = zpool_open(hdl, pool_path);
   3267  1.1  haad 
   3268  1.1  haad 	if (props && (props = zfs_valid_proplist(hdl, type, props,
   3269  1.4   chs 	    zoned, NULL, zpool_handle, errbuf)) == 0) {
   3270  1.4   chs 		zpool_close(zpool_handle);
   3271  1.1  haad 		return (-1);
   3272  1.4   chs 	}
   3273  1.4   chs 	zpool_close(zpool_handle);
   3274  1.1  haad 
   3275  1.1  haad 	if (type == ZFS_TYPE_VOLUME) {
   3276  1.1  haad 		/*
   3277  1.1  haad 		 * If we are creating a volume, the size and block size must
   3278  1.1  haad 		 * satisfy a few restraints.  First, the blocksize must be a
   3279  1.1  haad 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
   3280  1.1  haad 		 * volsize must be a multiple of the block size, and cannot be
   3281  1.1  haad 		 * zero.
   3282  1.1  haad 		 */
   3283  1.1  haad 		if (props == NULL || nvlist_lookup_uint64(props,
   3284  1.1  haad 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
   3285  1.1  haad 			nvlist_free(props);
   3286  1.1  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3287  1.1  haad 			    "missing volume size"));
   3288  1.1  haad 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
   3289  1.1  haad 		}
   3290  1.1  haad 
   3291  1.1  haad 		if ((ret = nvlist_lookup_uint64(props,
   3292  1.1  haad 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
   3293  1.1  haad 		    &blocksize)) != 0) {
   3294  1.1  haad 			if (ret == ENOENT) {
   3295  1.1  haad 				blocksize = zfs_prop_default_numeric(
   3296  1.1  haad 				    ZFS_PROP_VOLBLOCKSIZE);
   3297  1.1  haad 			} else {
   3298  1.1  haad 				nvlist_free(props);
   3299  1.1  haad 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3300  1.1  haad 				    "missing volume block size"));
   3301  1.1  haad 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
   3302  1.1  haad 			}
   3303  1.1  haad 		}
   3304  1.1  haad 
   3305  1.1  haad 		if (size == 0) {
   3306  1.1  haad 			nvlist_free(props);
   3307  1.1  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3308  1.1  haad 			    "volume size cannot be zero"));
   3309  1.1  haad 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
   3310  1.1  haad 		}
   3311  1.1  haad 
   3312  1.1  haad 		if (size % blocksize != 0) {
   3313  1.1  haad 			nvlist_free(props);
   3314  1.1  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3315  1.1  haad 			    "volume size must be a multiple of volume block "
   3316  1.1  haad 			    "size"));
   3317  1.1  haad 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
   3318  1.1  haad 		}
   3319  1.1  haad 	}
   3320  1.1  haad 
   3321  1.4   chs 	/* create the dataset */
   3322  1.4   chs 	ret = lzc_create(path, ost, props);
   3323  1.1  haad 	nvlist_free(props);
   3324  1.1  haad 
   3325  1.1  haad 	/* check for failure */
   3326  1.1  haad 	if (ret != 0) {
   3327  1.4   chs 		char parent[ZFS_MAX_DATASET_NAME_LEN];
   3328  1.1  haad 		(void) parent_name(path, parent, sizeof (parent));
   3329  1.1  haad 
   3330  1.1  haad 		switch (errno) {
   3331  1.1  haad 		case ENOENT:
   3332  1.1  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3333  1.1  haad 			    "no such parent '%s'"), parent);
   3334  1.1  haad 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
   3335  1.1  haad 
   3336  1.1  haad 		case EINVAL:
   3337  1.1  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3338  1.1  haad 			    "parent '%s' is not a filesystem"), parent);
   3339  1.1  haad 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
   3340  1.1  haad 
   3341  1.1  haad 		case ENOTSUP:
   3342  1.1  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3343  1.1  haad 			    "pool must be upgraded to set this "
   3344  1.1  haad 			    "property or value"));
   3345  1.1  haad 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
   3346  1.1  haad #ifdef _ILP32
   3347  1.1  haad 		case EOVERFLOW:
   3348  1.1  haad 			/*
   3349  1.1  haad 			 * This platform can't address a volume this big.
   3350  1.1  haad 			 */
   3351  1.1  haad 			if (type == ZFS_TYPE_VOLUME)
   3352  1.1  haad 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
   3353  1.1  haad 				    errbuf));
   3354  1.1  haad #endif
   3355  1.1  haad 			/* FALLTHROUGH */
   3356  1.1  haad 		default:
   3357  1.1  haad 			return (zfs_standard_error(hdl, errno, errbuf));
   3358  1.1  haad 		}
   3359  1.1  haad 	}
   3360  1.1  haad 
   3361  1.1  haad 	return (0);
   3362  1.1  haad }
   3363  1.1  haad 
   3364  1.1  haad /*
   3365  1.1  haad  * Destroys the given dataset.  The caller must make sure that the filesystem
   3366  1.4   chs  * isn't mounted, and that there are no active dependents. If the file system
   3367  1.4   chs  * does not exist this function does nothing.
   3368  1.1  haad  */
   3369  1.1  haad int
   3370  1.2  haad zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
   3371  1.1  haad {
   3372  1.1  haad 	zfs_cmd_t zc = { 0 };
   3373  1.1  haad 
   3374  1.4   chs 	if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
   3375  1.4   chs 		nvlist_t *nv = fnvlist_alloc();
   3376  1.4   chs 		fnvlist_add_boolean(nv, zhp->zfs_name);
   3377  1.4   chs 		int error = lzc_destroy_bookmarks(nv, NULL);
   3378  1.4   chs 		fnvlist_free(nv);
   3379  1.4   chs 		if (error != 0) {
   3380  1.4   chs 			return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
   3381  1.4   chs 			    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
   3382  1.4   chs 			    zhp->zfs_name));
   3383  1.4   chs 		}
   3384  1.4   chs 		return (0);
   3385  1.4   chs 	}
   3386  1.4   chs 
   3387  1.1  haad 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
   3388  1.1  haad 
   3389  1.1  haad 	if (ZFS_IS_VOLUME(zhp)) {
   3390  1.1  haad 		zc.zc_objset_type = DMU_OST_ZVOL;
   3391  1.1  haad 	} else {
   3392  1.1  haad 		zc.zc_objset_type = DMU_OST_ZFS;
   3393  1.1  haad 	}
   3394  1.1  haad 
   3395  1.2  haad 	zc.zc_defer_destroy = defer;
   3396  1.4   chs 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
   3397  1.4   chs 	    errno != ENOENT) {
   3398  1.1  haad 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
   3399  1.1  haad 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
   3400  1.1  haad 		    zhp->zfs_name));
   3401  1.1  haad 	}
   3402  1.1  haad 
   3403  1.1  haad 	remove_mountpoint(zhp);
   3404  1.1  haad 
   3405  1.1  haad 	return (0);
   3406  1.1  haad }
   3407  1.1  haad 
   3408  1.1  haad struct destroydata {
   3409  1.4   chs 	nvlist_t *nvl;
   3410  1.4   chs 	const char *snapname;
   3411  1.1  haad };
   3412  1.1  haad 
   3413  1.1  haad static int
   3414  1.2  haad zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
   3415  1.1  haad {
   3416  1.1  haad 	struct destroydata *dd = arg;
   3417  1.4   chs 	char name[ZFS_MAX_DATASET_NAME_LEN];
   3418  1.2  haad 	int rv = 0;
   3419  1.1  haad 
   3420  1.4   chs 	(void) snprintf(name, sizeof (name),
   3421  1.4   chs 	    "%s@%s", zhp->zfs_name, dd->snapname);
   3422  1.4   chs 
   3423  1.4   chs 	if (lzc_exists(name))
   3424  1.4   chs 		verify(nvlist_add_boolean(dd->nvl, name) == 0);
   3425  1.1  haad 
   3426  1.4   chs 	rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
   3427  1.4   chs 	zfs_close(zhp);
   3428  1.1  haad 	return (rv);
   3429  1.1  haad }
   3430  1.1  haad 
   3431  1.1  haad /*
   3432  1.1  haad  * Destroys all snapshots with the given name in zhp & descendants.
   3433  1.1  haad  */
   3434  1.1  haad int
   3435  1.2  haad zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
   3436  1.1  haad {
   3437  1.1  haad 	int ret;
   3438  1.1  haad 	struct destroydata dd = { 0 };
   3439  1.1  haad 
   3440  1.1  haad 	dd.snapname = snapname;
   3441  1.4   chs 	verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
   3442  1.4   chs 	(void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
   3443  1.1  haad 
   3444  1.4   chs 	if (nvlist_empty(dd.nvl)) {
   3445  1.4   chs 		ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
   3446  1.1  haad 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
   3447  1.4   chs 		    zhp->zfs_name, snapname);
   3448  1.4   chs 	} else {
   3449  1.4   chs 		ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
   3450  1.1  haad 	}
   3451  1.4   chs 	nvlist_free(dd.nvl);
   3452  1.4   chs 	return (ret);
   3453  1.4   chs }
   3454  1.1  haad 
   3455  1.4   chs /*
   3456  1.4   chs  * Destroys all the snapshots named in the nvlist.
   3457  1.4   chs  */
   3458  1.4   chs int
   3459  1.4   chs zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
   3460  1.4   chs {
   3461  1.4   chs 	int ret;
   3462  1.4   chs 	nvlist_t *errlist = NULL;
   3463  1.1  haad 
   3464  1.4   chs 	ret = lzc_destroy_snaps(snaps, defer, &errlist);
   3465  1.4   chs 
   3466  1.4   chs 	if (ret == 0) {
   3467  1.4   chs 		nvlist_free(errlist);
   3468  1.4   chs 		return (0);
   3469  1.4   chs 	}
   3470  1.4   chs 
   3471  1.4   chs 	if (nvlist_empty(errlist)) {
   3472  1.1  haad 		char errbuf[1024];
   3473  1.4   chs 		(void) snprintf(errbuf, sizeof (errbuf),
   3474  1.4   chs 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
   3475  1.1  haad 
   3476  1.4   chs 		ret = zfs_standard_error(hdl, ret, errbuf);
   3477  1.4   chs 	}
   3478  1.4   chs 	for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
   3479  1.4   chs 	    pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
   3480  1.4   chs 		char errbuf[1024];
   3481  1.4   chs 		(void) snprintf(errbuf, sizeof (errbuf),
   3482  1.4   chs 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
   3483  1.4   chs 		    nvpair_name(pair));
   3484  1.1  haad 
   3485  1.4   chs 		switch (fnvpair_value_int32(pair)) {
   3486  1.1  haad 		case EEXIST:
   3487  1.4   chs 			zfs_error_aux(hdl,
   3488  1.4   chs 			    dgettext(TEXT_DOMAIN, "snapshot is cloned"));
   3489  1.4   chs 			ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
   3490  1.4   chs 			break;
   3491  1.1  haad 		default:
   3492  1.4   chs 			ret = zfs_standard_error(hdl, errno, errbuf);
   3493  1.4   chs 			break;
   3494  1.1  haad 		}
   3495  1.1  haad 	}
   3496  1.1  haad 
   3497  1.4   chs 	nvlist_free(errlist);
   3498  1.4   chs 	return (ret);
   3499  1.1  haad }
   3500  1.1  haad 
   3501  1.1  haad /*
   3502  1.1  haad  * Clones the given dataset.  The target must be of the same type as the source.
   3503  1.1  haad  */
   3504  1.1  haad int
   3505  1.1  haad zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
   3506  1.1  haad {
   3507  1.4   chs 	char parent[ZFS_MAX_DATASET_NAME_LEN];
   3508  1.1  haad 	int ret;
   3509  1.1  haad 	char errbuf[1024];
   3510  1.1  haad 	libzfs_handle_t *hdl = zhp->zfs_hdl;
   3511  1.1  haad 	uint64_t zoned;
   3512  1.1  haad 
   3513  1.1  haad 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
   3514  1.1  haad 
   3515  1.1  haad 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
   3516  1.1  haad 	    "cannot create '%s'"), target);
   3517  1.1  haad 
   3518  1.4   chs 	/* validate the target/clone name */
   3519  1.1  haad 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
   3520  1.1  haad 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
   3521  1.1  haad 
   3522  1.1  haad 	/* validate parents exist */
   3523  1.1  haad 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
   3524  1.1  haad 		return (-1);
   3525  1.1  haad 
   3526  1.1  haad 	(void) parent_name(target, parent, sizeof (parent));
   3527  1.1  haad 
   3528  1.1  haad 	/* do the clone */
   3529  1.1  haad 
   3530  1.1  haad 	if (props) {
   3531  1.4   chs 		zfs_type_t type;
   3532  1.4   chs 		if (ZFS_IS_VOLUME(zhp)) {
   3533  1.4   chs 			type = ZFS_TYPE_VOLUME;
   3534  1.4   chs 		} else {
   3535  1.4   chs 			type = ZFS_TYPE_FILESYSTEM;
   3536  1.4   chs 		}
   3537  1.1  haad 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
   3538  1.4   chs 		    zhp, zhp->zpool_hdl, errbuf)) == NULL)
   3539  1.1  haad 			return (-1);
   3540  1.1  haad 	}
   3541  1.1  haad 
   3542  1.4   chs 	ret = lzc_clone(target, zhp->zfs_name, props);
   3543  1.4   chs 	nvlist_free(props);
   3544  1.1  haad 
   3545  1.1  haad 	if (ret != 0) {
   3546  1.1  haad 		switch (errno) {
   3547  1.1  haad 
   3548  1.1  haad 		case ENOENT:
   3549  1.1  haad 			/*
   3550  1.1  haad 			 * The parent doesn't exist.  We should have caught this
   3551  1.1  haad 			 * above, but there may a race condition that has since
   3552  1.1  haad 			 * destroyed the parent.
   3553  1.1  haad 			 *
   3554  1.1  haad 			 * At this point, we don't know whether it's the source
   3555  1.1  haad 			 * that doesn't exist anymore, or whether the target
   3556  1.1  haad 			 * dataset doesn't exist.
   3557  1.1  haad 			 */
   3558  1.1  haad 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
   3559  1.1  haad 			    "no such parent '%s'"), parent);
   3560  1.1  haad 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
   3561  1.1  haad 
   3562  1.1  haad 		case EXDEV:
   3563  1.1  haad 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
   3564  1.1  haad 			    "source and target pools differ"));
   3565  1.1  haad 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
   3566  1.1  haad 			    errbuf));
   3567  1.1  haad 
   3568  1.1  haad 		default:
   3569  1.1  haad 			return (zfs_standard_error(zhp->zfs_hdl, errno,
   3570  1.1  haad 			    errbuf));
   3571  1.1  haad 		}
   3572  1.1  haad 	}
   3573  1.1  haad 
   3574  1.1  haad 	return (ret);
   3575  1.1  haad }
   3576  1.1  haad 
   3577  1.1  haad /*
   3578  1.1  haad  * Promotes the given clone fs to be the clone parent.
   3579  1.1  haad  */
   3580  1.1  haad int
   3581  1.1  haad zfs_promote(zfs_handle_t *zhp)
   3582  1.1  haad {
   3583  1.1  haad 	libzfs_handle_t *hdl = zhp->zfs_hdl;
   3584  1.1  haad 	zfs_cmd_t zc = { 0 };
   3585  1.1  haad 	char parent[MAXPATHLEN];
   3586  1.1  haad 	int ret;
   3587  1.1  haad 	char errbuf[1024];
   3588  1.1  haad 
   3589  1.1  haad 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
   3590  1.1  haad 	    "cannot promote '%s'"), zhp->zfs_name);
   3591  1.1  haad 
   3592  1.1  haad 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
   3593  1.1  haad 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3594  1.1  haad 		    "snapshots can not be promoted"));
   3595  1.1  haad 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
   3596  1.1  haad 	}
   3597  1.1  haad 
   3598  1.1  haad 	(void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
   3599  1.1  haad 	if (parent[0] == '\0') {
   3600  1.1  haad 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3601  1.1  haad 		    "not a cloned filesystem"));
   3602  1.1  haad 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
   3603  1.1  haad 	}
   3604  1.1  haad 
   3605  1.1  haad 	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
   3606  1.1  haad 	    sizeof (zc.zc_value));
   3607  1.1  haad 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
   3608  1.1  haad 	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
   3609  1.1  haad 
   3610  1.1  haad 	if (ret != 0) {
   3611  1.1  haad 		int save_errno = errno;
   3612  1.1  haad 
   3613  1.1  haad 		switch (save_errno) {
   3614  1.1  haad 		case EEXIST:
   3615  1.2  haad 			/* There is a conflicting snapshot name. */
   3616  1.1  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3617  1.2  haad 			    "conflicting snapshot '%s' from parent '%s'"),
   3618  1.2  haad 			    zc.zc_string, parent);
   3619  1.1  haad 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
   3620  1.1  haad 
   3621  1.1  haad 		default:
   3622  1.1  haad 			return (zfs_standard_error(hdl, save_errno, errbuf));
   3623  1.1  haad 		}
   3624  1.1  haad 	}
   3625  1.1  haad 	return (ret);
   3626  1.1  haad }
   3627  1.1  haad 
   3628  1.4   chs typedef struct snapdata {
   3629  1.4   chs 	nvlist_t *sd_nvl;
   3630  1.4   chs 	const char *sd_snapname;
   3631  1.4   chs } snapdata_t;
   3632  1.4   chs 
   3633  1.4   chs static int
   3634  1.4   chs zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
   3635  1.4   chs {
   3636  1.4   chs 	snapdata_t *sd = arg;
   3637  1.4   chs 	char name[ZFS_MAX_DATASET_NAME_LEN];
   3638  1.4   chs 	int rv = 0;
   3639  1.4   chs 
   3640  1.4   chs 	if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
   3641  1.4   chs 		(void) snprintf(name, sizeof (name),
   3642  1.4   chs 		    "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
   3643  1.4   chs 
   3644  1.4   chs 		fnvlist_add_boolean(sd->sd_nvl, name);
   3645  1.4   chs 
   3646  1.4   chs 		rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
   3647  1.4   chs 	}
   3648  1.4   chs 	zfs_close(zhp);
   3649  1.4   chs 
   3650  1.4   chs 	return (rv);
   3651  1.4   chs }
   3652  1.4   chs 
   3653  1.1  haad /*
   3654  1.4   chs  * Creates snapshots.  The keys in the snaps nvlist are the snapshots to be
   3655  1.4   chs  * created.
   3656  1.1  haad  */
   3657  1.1  haad int
   3658  1.4   chs zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
   3659  1.4   chs {
   3660  1.4   chs 	int ret;
   3661  1.4   chs 	char errbuf[1024];
   3662  1.4   chs 	nvpair_t *elem;
   3663  1.4   chs 	nvlist_t *errors;
   3664  1.4   chs 
   3665  1.4   chs 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
   3666  1.4   chs 	    "cannot create snapshots "));
   3667  1.4   chs 
   3668  1.4   chs 	elem = NULL;
   3669  1.4   chs 	while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
   3670  1.4   chs 		const char *snapname = nvpair_name(elem);
   3671  1.4   chs 
   3672  1.4   chs 		/* validate the target name */
   3673  1.4   chs 		if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
   3674  1.4   chs 		    B_TRUE)) {
   3675  1.4   chs 			(void) snprintf(errbuf, sizeof (errbuf),
   3676  1.4   chs 			    dgettext(TEXT_DOMAIN,
   3677  1.4   chs 			    "cannot create snapshot '%s'"), snapname);
   3678  1.4   chs 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
   3679  1.4   chs 		}
   3680  1.4   chs 	}
   3681  1.4   chs 
   3682  1.4   chs 	/*
   3683  1.4   chs 	 * get pool handle for prop validation. assumes all snaps are in the
   3684  1.4   chs 	 * same pool, as does lzc_snapshot (below).
   3685  1.4   chs 	 */
   3686  1.4   chs 	char pool[ZFS_MAX_DATASET_NAME_LEN];
   3687  1.4   chs 	elem = nvlist_next_nvpair(snaps, NULL);
   3688  1.4   chs 	(void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
   3689  1.4   chs 	pool[strcspn(pool, "/@")] = '\0';
   3690  1.4   chs 	zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
   3691  1.4   chs 
   3692  1.4   chs 	if (props != NULL &&
   3693  1.4   chs 	    (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
   3694  1.4   chs 	    props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
   3695  1.4   chs 		zpool_close(zpool_hdl);
   3696  1.4   chs 		return (-1);
   3697  1.4   chs 	}
   3698  1.4   chs 	zpool_close(zpool_hdl);
   3699  1.4   chs 
   3700  1.4   chs 	ret = lzc_snapshot(snaps, props, &errors);
   3701  1.4   chs 
   3702  1.4   chs 	if (ret != 0) {
   3703  1.4   chs 		boolean_t printed = B_FALSE;
   3704  1.4   chs 		for (elem = nvlist_next_nvpair(errors, NULL);
   3705  1.4   chs 		    elem != NULL;
   3706  1.4   chs 		    elem = nvlist_next_nvpair(errors, elem)) {
   3707  1.4   chs 			(void) snprintf(errbuf, sizeof (errbuf),
   3708  1.4   chs 			    dgettext(TEXT_DOMAIN,
   3709  1.4   chs 			    "cannot create snapshot '%s'"), nvpair_name(elem));
   3710  1.4   chs 			(void) zfs_standard_error(hdl,
   3711  1.4   chs 			    fnvpair_value_int32(elem), errbuf);
   3712  1.4   chs 			printed = B_TRUE;
   3713  1.4   chs 		}
   3714  1.4   chs 		if (!printed) {
   3715  1.4   chs 			switch (ret) {
   3716  1.4   chs 			case EXDEV:
   3717  1.4   chs 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3718  1.4   chs 				    "multiple snapshots of same "
   3719  1.4   chs 				    "fs not allowed"));
   3720  1.4   chs 				(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
   3721  1.4   chs 
   3722  1.4   chs 				break;
   3723  1.4   chs 			default:
   3724  1.4   chs 				(void) zfs_standard_error(hdl, ret, errbuf);
   3725  1.4   chs 			}
   3726  1.4   chs 		}
   3727  1.4   chs 	}
   3728  1.4   chs 
   3729  1.4   chs 	nvlist_free(props);
   3730  1.4   chs 	nvlist_free(errors);
   3731  1.4   chs 	return (ret);
   3732  1.4   chs }
   3733  1.4   chs 
   3734  1.4   chs int
   3735  1.1  haad zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
   3736  1.1  haad     nvlist_t *props)
   3737  1.1  haad {
   3738  1.4   chs 	int ret;
   3739  1.4   chs 	snapdata_t sd = { 0 };
   3740  1.4   chs 	char fsname[ZFS_MAX_DATASET_NAME_LEN];
   3741  1.4   chs 	char *cp;
   3742  1.1  haad 	zfs_handle_t *zhp;
   3743  1.1  haad 	char errbuf[1024];
   3744  1.1  haad 
   3745  1.1  haad 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
   3746  1.4   chs 	    "cannot snapshot %s"), path);
   3747  1.1  haad 
   3748  1.1  haad 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
   3749  1.1  haad 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
   3750  1.1  haad 
   3751  1.4   chs 	(void) strlcpy(fsname, path, sizeof (fsname));
   3752  1.4   chs 	cp = strchr(fsname, '@');
   3753  1.4   chs 	*cp = '\0';
   3754  1.4   chs 	sd.sd_snapname = cp + 1;
   3755  1.1  haad 
   3756  1.4   chs 	if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
   3757  1.1  haad 	    ZFS_TYPE_VOLUME)) == NULL) {
   3758  1.1  haad 		return (-1);
   3759  1.1  haad 	}
   3760  1.1  haad 
   3761  1.4   chs 	verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
   3762  1.4   chs 	if (recursive) {
   3763  1.4   chs 		(void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
   3764  1.4   chs 	} else {
   3765  1.4   chs 		fnvlist_add_boolean(sd.sd_nvl, path);
   3766  1.1  haad 	}
   3767  1.1  haad 
   3768  1.4   chs 	ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
   3769  1.4   chs 	nvlist_free(sd.sd_nvl);
   3770  1.1  haad 	zfs_close(zhp);
   3771  1.1  haad 	return (ret);
   3772  1.1  haad }
   3773  1.1  haad 
   3774  1.1  haad /*
   3775  1.1  haad  * Destroy any more recent snapshots.  We invoke this callback on any dependents
   3776  1.1  haad  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
   3777  1.1  haad  * is a dependent and we should just destroy it without checking the transaction
   3778  1.1  haad  * group.
   3779  1.1  haad  */
   3780  1.1  haad typedef struct rollback_data {
   3781  1.1  haad 	const char	*cb_target;		/* the snapshot */
   3782  1.1  haad 	uint64_t	cb_create;		/* creation time reference */
   3783  1.1  haad 	boolean_t	cb_error;
   3784  1.1  haad 	boolean_t	cb_force;
   3785  1.1  haad } rollback_data_t;
   3786  1.1  haad 
   3787  1.1  haad static int
   3788  1.4   chs rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
   3789  1.4   chs {
   3790  1.4   chs 	rollback_data_t *cbp = data;
   3791  1.4   chs 	prop_changelist_t *clp;
   3792  1.4   chs 
   3793  1.4   chs 	/* We must destroy this clone; first unmount it */
   3794  1.4   chs 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
   3795  1.4   chs 	    cbp->cb_force ? MS_FORCE: 0);
   3796  1.4   chs 	if (clp == NULL || changelist_prefix(clp) != 0) {
   3797  1.4   chs 		cbp->cb_error = B_TRUE;
   3798  1.4   chs 		zfs_close(zhp);
   3799  1.4   chs 		return (0);
   3800  1.4   chs 	}
   3801  1.4   chs 	if (zfs_destroy(zhp, B_FALSE) != 0)
   3802  1.4   chs 		cbp->cb_error = B_TRUE;
   3803  1.4   chs 	else
   3804  1.4   chs 		changelist_remove(clp, zhp->zfs_name);
   3805  1.4   chs 	(void) changelist_postfix(clp);
   3806  1.4   chs 	changelist_free(clp);
   3807  1.4   chs 
   3808  1.4   chs 	zfs_close(zhp);
   3809  1.4   chs 	return (0);
   3810  1.4   chs }
   3811  1.4   chs 
   3812  1.4   chs static int
   3813  1.1  haad rollback_destroy(zfs_handle_t *zhp, void *data)
   3814  1.1  haad {
   3815  1.1  haad 	rollback_data_t *cbp = data;
   3816  1.1  haad 
   3817  1.4   chs 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
   3818  1.4   chs 		cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
   3819  1.4   chs 		    rollback_destroy_dependent, cbp);
   3820  1.1  haad 
   3821  1.4   chs 		cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
   3822  1.1  haad 	}
   3823  1.1  haad 
   3824  1.1  haad 	zfs_close(zhp);
   3825  1.1  haad 	return (0);
   3826  1.1  haad }
   3827  1.1  haad 
   3828  1.1  haad /*
   3829  1.1  haad  * Given a dataset, rollback to a specific snapshot, discarding any
   3830  1.1  haad  * data changes since then and making it the active dataset.
   3831  1.1  haad  *
   3832  1.4   chs  * Any snapshots and bookmarks more recent than the target are
   3833  1.4   chs  * destroyed, along with their dependents (i.e. clones).
   3834  1.1  haad  */
   3835  1.1  haad int
   3836  1.1  haad zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
   3837  1.1  haad {
   3838  1.1  haad 	rollback_data_t cb = { 0 };
   3839  1.1  haad 	int err;
   3840  1.1  haad 	boolean_t restore_resv = 0;
   3841  1.4   chs 	uint64_t old_volsize = 0, new_volsize;
   3842  1.1  haad 	zfs_prop_t resv_prop;
   3843  1.1  haad 
   3844  1.1  haad 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
   3845  1.1  haad 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
   3846  1.1  haad 
   3847  1.1  haad 	/*
   3848  1.4   chs 	 * Destroy all recent snapshots and their dependents.
   3849  1.1  haad 	 */
   3850  1.1  haad 	cb.cb_force = force;
   3851  1.1  haad 	cb.cb_target = snap->zfs_name;
   3852  1.1  haad 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
   3853  1.4   chs 	(void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
   3854  1.4   chs 	(void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
   3855  1.1  haad 
   3856  1.1  haad 	if (cb.cb_error)
   3857  1.1  haad 		return (-1);
   3858  1.1  haad 
   3859  1.1  haad 	/*
   3860  1.1  haad 	 * Now that we have verified that the snapshot is the latest,
   3861  1.1  haad 	 * rollback to the given snapshot.
   3862  1.1  haad 	 */
   3863  1.1  haad 
   3864  1.1  haad 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
   3865  1.1  haad 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
   3866  1.1  haad 			return (-1);
   3867  1.1  haad 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
   3868  1.1  haad 		restore_resv =
   3869  1.1  haad 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
   3870  1.1  haad 	}
   3871  1.1  haad 
   3872  1.1  haad 	/*
   3873  1.1  haad 	 * We rely on zfs_iter_children() to verify that there are no
   3874  1.1  haad 	 * newer snapshots for the given dataset.  Therefore, we can
   3875  1.1  haad 	 * simply pass the name on to the ioctl() call.  There is still
   3876  1.1  haad 	 * an unlikely race condition where the user has taken a
   3877  1.1  haad 	 * snapshot since we verified that this was the most recent.
   3878  1.1  haad 	 */
   3879  1.4   chs 	err = lzc_rollback(zhp->zfs_name, NULL, 0);
   3880  1.4   chs 	if (err != 0) {
   3881  1.1  haad 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
   3882  1.1  haad 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
   3883  1.1  haad 		    zhp->zfs_name);
   3884  1.1  haad 		return (err);
   3885  1.1  haad 	}
   3886  1.1  haad 
   3887  1.1  haad 	/*
   3888  1.1  haad 	 * For volumes, if the pre-rollback volsize matched the pre-
   3889  1.1  haad 	 * rollback reservation and the volsize has changed then set
   3890  1.1  haad 	 * the reservation property to the post-rollback volsize.
   3891  1.1  haad 	 * Make a new handle since the rollback closed the dataset.
   3892  1.1  haad 	 */
   3893  1.1  haad 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
   3894  1.1  haad 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
   3895  1.1  haad 		if (restore_resv) {
   3896  1.1  haad 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
   3897  1.1  haad 			if (old_volsize != new_volsize)
   3898  1.1  haad 				err = zfs_prop_set_int(zhp, resv_prop,
   3899  1.1  haad 				    new_volsize);
   3900  1.1  haad 		}
   3901  1.1  haad 		zfs_close(zhp);
   3902  1.1  haad 	}
   3903  1.1  haad 	return (err);
   3904  1.1  haad }
   3905  1.1  haad 
   3906  1.1  haad /*
   3907  1.1  haad  * Renames the given dataset.
   3908  1.1  haad  */
   3909  1.1  haad int
   3910  1.4   chs zfs_rename(zfs_handle_t *zhp, const char *source, const char *target,
   3911  1.4   chs     renameflags_t flags)
   3912  1.1  haad {
   3913  1.4   chs 	int ret = 0;
   3914  1.1  haad 	zfs_cmd_t zc = { 0 };
   3915  1.1  haad 	char *delim;
   3916  1.1  haad 	prop_changelist_t *cl = NULL;
   3917  1.1  haad 	zfs_handle_t *zhrp = NULL;
   3918  1.1  haad 	char *parentname = NULL;
   3919  1.4   chs 	char parent[ZFS_MAX_DATASET_NAME_LEN];
   3920  1.4   chs 	char property[ZFS_MAXPROPLEN];
   3921  1.1  haad 	libzfs_handle_t *hdl = zhp->zfs_hdl;
   3922  1.1  haad 	char errbuf[1024];
   3923  1.1  haad 
   3924  1.1  haad 	/* if we have the same exact name, just return success */
   3925  1.1  haad 	if (strcmp(zhp->zfs_name, target) == 0)
   3926  1.1  haad 		return (0);
   3927  1.1  haad 
   3928  1.1  haad 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
   3929  1.1  haad 	    "cannot rename to '%s'"), target);
   3930  1.1  haad 
   3931  1.4   chs 	if (source != NULL) {
   3932  1.4   chs 		/*
   3933  1.4   chs 		 * This is recursive snapshots rename, put snapshot name
   3934  1.4   chs 		 * (that might not exist) into zfs_name.
   3935  1.4   chs 		 */
   3936  1.4   chs 		assert(flags.recurse);
   3937  1.4   chs 
   3938  1.4   chs 		(void) strlcat(zhp->zfs_name, "@", sizeof(zhp->zfs_name));
   3939  1.4   chs 		(void) strlcat(zhp->zfs_name, source, sizeof(zhp->zfs_name));
   3940  1.4   chs 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
   3941  1.4   chs 	}
   3942  1.4   chs 
   3943  1.1  haad 	/*
   3944  1.1  haad 	 * Make sure the target name is valid
   3945  1.1  haad 	 */
   3946  1.1  haad 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
   3947  1.1  haad 		if ((strchr(target, '@') == NULL) ||
   3948  1.1  haad 		    *target == '@') {
   3949  1.1  haad 			/*
   3950  1.1  haad 			 * Snapshot target name is abbreviated,
   3951  1.1  haad 			 * reconstruct full dataset name
   3952  1.1  haad 			 */
   3953  1.1  haad 			(void) strlcpy(parent, zhp->zfs_name,
   3954  1.1  haad 			    sizeof (parent));
   3955  1.1  haad 			delim = strchr(parent, '@');
   3956  1.1  haad 			if (strchr(target, '@') == NULL)
   3957  1.1  haad 				*(++delim) = '\0';
   3958  1.1  haad 			else
   3959  1.1  haad 				*delim = '\0';
   3960  1.1  haad 			(void) strlcat(parent, target, sizeof (parent));
   3961  1.1  haad 			target = parent;
   3962  1.1  haad 		} else {
   3963  1.1  haad 			/*
   3964  1.1  haad 			 * Make sure we're renaming within the same dataset.
   3965  1.1  haad 			 */
   3966  1.1  haad 			delim = strchr(target, '@');
   3967  1.1  haad 			if (strncmp(zhp->zfs_name, target, delim - target)
   3968  1.1  haad 			    != 0 || zhp->zfs_name[delim - target] != '@') {
   3969  1.1  haad 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3970  1.1  haad 				    "snapshots must be part of same "
   3971  1.1  haad 				    "dataset"));
   3972  1.1  haad 				return (zfs_error(hdl, EZFS_CROSSTARGET,
   3973  1.1  haad 				    errbuf));
   3974  1.1  haad 			}
   3975  1.1  haad 		}
   3976  1.1  haad 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
   3977  1.1  haad 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
   3978  1.1  haad 	} else {
   3979  1.4   chs 		if (flags.recurse) {
   3980  1.1  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3981  1.1  haad 			    "recursive rename must be a snapshot"));
   3982  1.1  haad 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
   3983  1.1  haad 		}
   3984  1.1  haad 
   3985  1.1  haad 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
   3986  1.1  haad 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
   3987  1.1  haad 
   3988  1.1  haad 		/* validate parents */
   3989  1.2  haad 		if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
   3990  1.1  haad 			return (-1);
   3991  1.1  haad 
   3992  1.1  haad 		/* make sure we're in the same pool */
   3993  1.1  haad 		verify((delim = strchr(target, '/')) != NULL);
   3994  1.1  haad 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
   3995  1.1  haad 		    zhp->zfs_name[delim - target] != '/') {
   3996  1.1  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   3997  1.1  haad 			    "datasets must be within same pool"));
   3998  1.1  haad 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
   3999  1.1  haad 		}
   4000  1.1  haad 
   4001  1.1  haad 		/* new name cannot be a child of the current dataset name */
   4002  1.2  haad 		if (is_descendant(zhp->zfs_name, target)) {
   4003  1.1  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   4004  1.2  haad 			    "New dataset name cannot be a descendant of "
   4005  1.1  haad 			    "current dataset name"));
   4006  1.1  haad 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
   4007  1.1  haad 		}
   4008  1.1  haad 	}
   4009  1.1  haad 
   4010  1.1  haad 	(void) snprintf(errbuf, sizeof (errbuf),
   4011  1.1  haad 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
   4012  1.1  haad 
   4013  1.1  haad 	if (getzoneid() == GLOBAL_ZONEID &&
   4014  1.1  haad 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
   4015  1.1  haad 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   4016  1.1  haad 		    "dataset is used in a non-global zone"));
   4017  1.1  haad 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
   4018  1.1  haad 	}
   4019  1.1  haad 
   4020  1.4   chs 	/*
   4021  1.4   chs 	 * Avoid unmounting file systems with mountpoint property set to
   4022  1.4   chs 	 * 'legacy' or 'none' even if -u option is not given.
   4023  1.4   chs 	 */
   4024  1.4   chs 	if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
   4025  1.4   chs 	    !flags.recurse && !flags.nounmount &&
   4026  1.4   chs 	    zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, property,
   4027  1.4   chs 	    sizeof (property), NULL, NULL, 0, B_FALSE) == 0 &&
   4028  1.4   chs 	    (strcmp(property, "legacy") == 0 ||
   4029  1.4   chs 	     strcmp(property, "none") == 0)) {
   4030  1.4   chs 		flags.nounmount = B_TRUE;
   4031  1.4   chs 	}
   4032  1.4   chs 	if (flags.recurse) {
   4033  1.1  haad 
   4034  1.1  haad 		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
   4035  1.1  haad 		if (parentname == NULL) {
   4036  1.1  haad 			ret = -1;
   4037  1.1  haad 			goto error;
   4038  1.1  haad 		}
   4039  1.1  haad 		delim = strchr(parentname, '@');
   4040  1.1  haad 		*delim = '\0';
   4041  1.1  haad 		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
   4042  1.1  haad 		if (zhrp == NULL) {
   4043  1.1  haad 			ret = -1;
   4044  1.1  haad 			goto error;
   4045  1.1  haad 		}
   4046  1.4   chs 	} else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
   4047  1.4   chs 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME,
   4048  1.4   chs 		    flags.nounmount ? CL_GATHER_DONT_UNMOUNT : 0,
   4049  1.4   chs 		    flags.forceunmount ? MS_FORCE : 0)) == NULL) {
   4050  1.1  haad 			return (-1);
   4051  1.4   chs 		}
   4052  1.1  haad 
   4053  1.1  haad 		if (changelist_haszonedchild(cl)) {
   4054  1.1  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   4055  1.1  haad 			    "child dataset with inherited mountpoint is used "
   4056  1.1  haad 			    "in a non-global zone"));
   4057  1.1  haad 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
   4058  1.4   chs 			ret = -1;
   4059  1.1  haad 			goto error;
   4060  1.1  haad 		}
   4061  1.1  haad 
   4062  1.1  haad 		if ((ret = changelist_prefix(cl)) != 0)
   4063  1.1  haad 			goto error;
   4064  1.1  haad 	}
   4065  1.1  haad 
   4066  1.1  haad 	if (ZFS_IS_VOLUME(zhp))
   4067  1.1  haad 		zc.zc_objset_type = DMU_OST_ZVOL;
   4068  1.1  haad 	else
   4069  1.1  haad 		zc.zc_objset_type = DMU_OST_ZFS;
   4070  1.1  haad 
   4071  1.1  haad 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
   4072  1.1  haad 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
   4073  1.1  haad 
   4074  1.4   chs 	zc.zc_cookie = flags.recurse ? 1 : 0;
   4075  1.4   chs 	if (flags.nounmount)
   4076  1.4   chs 		zc.zc_cookie |= 2;
   4077  1.1  haad 
   4078  1.2  haad 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
   4079  1.2  haad 		/*
   4080  1.2  haad 		 * if it was recursive, the one that actually failed will
   4081  1.2  haad 		 * be in zc.zc_name
   4082  1.2  haad 		 */
   4083  1.2  haad 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
   4084  1.2  haad 		    "cannot rename '%s'"), zc.zc_name);
   4085  1.1  haad 
   4086  1.4   chs 		if (flags.recurse && errno == EEXIST) {
   4087  1.2  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   4088  1.2  haad 			    "a child dataset already has a snapshot "
   4089  1.2  haad 			    "with the new name"));
   4090  1.2  haad 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
   4091  1.1  haad 		} else {
   4092  1.2  haad 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
   4093  1.1  haad 		}
   4094  1.1  haad 
   4095  1.1  haad 		/*
   4096  1.2  haad 		 * On failure, we still want to remount any filesystems that
   4097  1.2  haad 		 * were previously mounted, so we don't alter the system state.
   4098  1.1  haad 		 */
   4099  1.4   chs 		if (cl != NULL)
   4100  1.2  haad 			(void) changelist_postfix(cl);
   4101  1.2  haad 	} else {
   4102  1.4   chs 		if (cl != NULL) {
   4103  1.2  haad 			changelist_rename(cl, zfs_get_name(zhp), target);
   4104  1.2  haad 			ret = changelist_postfix(cl);
   4105  1.1  haad 		}
   4106  1.1  haad 	}
   4107  1.1  haad 
   4108  1.2  haad error:
   4109  1.4   chs 	if (parentname != NULL) {
   4110  1.2  haad 		free(parentname);
   4111  1.2  haad 	}
   4112  1.4   chs 	if (zhrp != NULL) {
   4113  1.2  haad 		zfs_close(zhrp);
   4114  1.2  haad 	}
   4115  1.4   chs 	if (cl != NULL) {
   4116  1.2  haad 		changelist_free(cl);
   4117  1.2  haad 	}
   4118  1.2  haad 	return (ret);
   4119  1.1  haad }
   4120  1.1  haad 
   4121  1.2  haad nvlist_t *
   4122  1.2  haad zfs_get_user_props(zfs_handle_t *zhp)
   4123  1.1  haad {
   4124  1.2  haad 	return (zhp->zfs_user_props);
   4125  1.1  haad }
   4126  1.1  haad 
   4127  1.1  haad nvlist_t *
   4128  1.2  haad zfs_get_recvd_props(zfs_handle_t *zhp)
   4129  1.1  haad {
   4130  1.2  haad 	if (zhp->zfs_recvd_props == NULL)
   4131  1.2  haad 		if (get_recvd_props_ioctl(zhp) != 0)
   4132  1.2  haad 			return (NULL);
   4133  1.2  haad 	return (zhp->zfs_recvd_props);
   4134  1.1  haad }
   4135  1.1  haad 
   4136  1.1  haad /*
   4137  1.1  haad  * This function is used by 'zfs list' to determine the exact set of columns to
   4138  1.1  haad  * display, and their maximum widths.  This does two main things:
   4139  1.1  haad  *
   4140  1.1  haad  *      - If this is a list of all properties, then expand the list to include
   4141  1.1  haad  *        all native properties, and set a flag so that for each dataset we look
   4142  1.1  haad  *        for new unique user properties and add them to the list.
   4143  1.1  haad  *
   4144  1.1  haad  *      - For non fixed-width properties, keep track of the maximum width seen
   4145  1.2  haad  *        so that we can size the column appropriately. If the user has
   4146  1.2  haad  *        requested received property values, we also need to compute the width
   4147  1.2  haad  *        of the RECEIVED column.
   4148  1.1  haad  */
   4149  1.1  haad int
   4150  1.4   chs zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
   4151  1.4   chs     boolean_t literal)
   4152  1.1  haad {
   4153  1.1  haad 	libzfs_handle_t *hdl = zhp->zfs_hdl;
   4154  1.1  haad 	zprop_list_t *entry;
   4155  1.1  haad 	zprop_list_t **last, **start;
   4156  1.1  haad 	nvlist_t *userprops, *propval;
   4157  1.1  haad 	nvpair_t *elem;
   4158  1.1  haad 	char *strval;
   4159  1.1  haad 	char buf[ZFS_MAXPROPLEN];
   4160  1.1  haad 
   4161  1.1  haad 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
   4162  1.1  haad 		return (-1);
   4163  1.1  haad 
   4164  1.1  haad 	userprops = zfs_get_user_props(zhp);
   4165  1.1  haad 
   4166  1.1  haad 	entry = *plp;
   4167  1.1  haad 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
   4168  1.1  haad 		/*
   4169  1.1  haad 		 * Go through and add any user properties as necessary.  We
   4170  1.1  haad 		 * start by incrementing our list pointer to the first
   4171  1.1  haad 		 * non-native property.
   4172  1.1  haad 		 */
   4173  1.1  haad 		start = plp;
   4174  1.1  haad 		while (*start != NULL) {
   4175  1.1  haad 			if ((*start)->pl_prop == ZPROP_INVAL)
   4176  1.1  haad 				break;
   4177  1.1  haad 			start = &(*start)->pl_next;
   4178  1.1  haad 		}
   4179  1.1  haad 
   4180  1.1  haad 		elem = NULL;
   4181  1.1  haad 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
   4182  1.1  haad 			/*
   4183  1.1  haad 			 * See if we've already found this property in our list.
   4184  1.1  haad 			 */
   4185  1.1  haad 			for (last = start; *last != NULL;
   4186  1.1  haad 			    last = &(*last)->pl_next) {
   4187  1.1  haad 				if (strcmp((*last)->pl_user_prop,
   4188  1.1  haad 				    nvpair_name(elem)) == 0)
   4189  1.1  haad 					break;
   4190  1.1  haad 			}
   4191  1.1  haad 
   4192  1.1  haad 			if (*last == NULL) {
   4193  1.1  haad 				if ((entry = zfs_alloc(hdl,
   4194  1.1  haad 				    sizeof (zprop_list_t))) == NULL ||
   4195  1.1  haad 				    ((entry->pl_user_prop = zfs_strdup(hdl,
   4196  1.1  haad 				    nvpair_name(elem)))) == NULL) {
   4197  1.1  haad 					free(entry);
   4198  1.1  haad 					return (-1);
   4199  1.1  haad 				}
   4200  1.1  haad 
   4201  1.1  haad 				entry->pl_prop = ZPROP_INVAL;
   4202  1.1  haad 				entry->pl_width = strlen(nvpair_name(elem));
   4203  1.1  haad 				entry->pl_all = B_TRUE;
   4204  1.1  haad 				*last = entry;
   4205  1.1  haad 			}
   4206  1.1  haad 		}
   4207  1.1  haad 	}
   4208  1.1  haad 
   4209  1.1  haad 	/*
   4210  1.1  haad 	 * Now go through and check the width of any non-fixed columns
   4211  1.1  haad 	 */
   4212  1.1  haad 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
   4213  1.4   chs 		if (entry->pl_fixed && !literal)
   4214  1.1  haad 			continue;
   4215  1.1  haad 
   4216  1.1  haad 		if (entry->pl_prop != ZPROP_INVAL) {
   4217  1.1  haad 			if (zfs_prop_get(zhp, entry->pl_prop,
   4218  1.4   chs 			    buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
   4219  1.1  haad 				if (strlen(buf) > entry->pl_width)
   4220  1.1  haad 					entry->pl_width = strlen(buf);
   4221  1.1  haad 			}
   4222  1.2  haad 			if (received && zfs_prop_get_recvd(zhp,
   4223  1.2  haad 			    zfs_prop_to_name(entry->pl_prop),
   4224  1.4   chs 			    buf, sizeof (buf), literal) == 0)
   4225  1.2  haad 				if (strlen(buf) > entry->pl_recvd_width)
   4226  1.2  haad 					entry->pl_recvd_width = strlen(buf);
   4227  1.2  haad 		} else {
   4228  1.2  haad 			if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
   4229  1.2  haad 			    &propval) == 0) {
   4230  1.2  haad 				verify(nvlist_lookup_string(propval,
   4231  1.2  haad 				    ZPROP_VALUE, &strval) == 0);
   4232  1.2  haad 				if (strlen(strval) > entry->pl_width)
   4233  1.2  haad 					entry->pl_width = strlen(strval);
   4234  1.2  haad 			}
   4235  1.2  haad 			if (received && zfs_prop_get_recvd(zhp,
   4236  1.2  haad 			    entry->pl_user_prop,
   4237  1.4   chs 			    buf, sizeof (buf), literal) == 0)
   4238  1.2  haad 				if (strlen(buf) > entry->pl_recvd_width)
   4239  1.2  haad 					entry->pl_recvd_width = strlen(buf);
   4240  1.1  haad 		}
   4241  1.1  haad 	}
   4242  1.1  haad 
   4243  1.1  haad 	return (0);
   4244  1.1  haad }
   4245  1.1  haad 
   4246  1.1  haad int
   4247  1.1  haad zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
   4248  1.2  haad     char *resource, void *export, void *sharetab,
   4249  1.2  haad     int sharemax, zfs_share_op_t operation)
   4250  1.1  haad {
   4251  1.1  haad 	zfs_cmd_t zc = { 0 };
   4252  1.1  haad 	int error;
   4253  1.1  haad 
   4254  1.1  haad 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
   4255  1.1  haad 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
   4256  1.2  haad 	if (resource)
   4257  1.2  haad 		(void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
   4258  1.1  haad 	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
   4259  1.1  haad 	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
   4260  1.1  haad 	zc.zc_share.z_sharetype = operation;
   4261  1.1  haad 	zc.zc_share.z_sharemax = sharemax;
   4262  1.2  haad 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
   4263  1.2  haad 	return (error);
   4264  1.2  haad }
   4265  1.2  haad 
   4266  1.2  haad void
   4267  1.2  haad zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
   4268  1.2  haad {
   4269  1.2  haad 	nvpair_t *curr;
   4270  1.2  haad 
   4271  1.2  haad 	/*
   4272  1.2  haad 	 * Keep a reference to the props-table against which we prune the
   4273  1.2  haad 	 * properties.
   4274  1.2  haad 	 */
   4275  1.2  haad 	zhp->zfs_props_table = props;
   4276  1.2  haad 
   4277  1.2  haad 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
   4278  1.2  haad 
   4279  1.2  haad 	while (curr) {
   4280  1.2  haad 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
   4281  1.2  haad 		nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
   4282  1.2  haad 
   4283  1.2  haad 		/*
   4284  1.2  haad 		 * User properties will result in ZPROP_INVAL, and since we
   4285  1.2  haad 		 * only know how to prune standard ZFS properties, we always
   4286  1.2  haad 		 * leave these in the list.  This can also happen if we
   4287  1.2  haad 		 * encounter an unknown DSL property (when running older
   4288  1.2  haad 		 * software, for example).
   4289  1.2  haad 		 */
   4290  1.2  haad 		if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
   4291  1.2  haad 			(void) nvlist_remove(zhp->zfs_props,
   4292  1.2  haad 			    nvpair_name(curr), nvpair_type(curr));
   4293  1.2  haad 		curr = next;
   4294  1.2  haad 	}
   4295  1.2  haad }
   4296  1.2  haad 
   4297  1.4   chs #ifdef illumos
   4298  1.2  haad static int
   4299  1.2  haad zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
   4300  1.2  haad     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
   4301  1.2  haad {
   4302  1.2  haad 	zfs_cmd_t zc = { 0 };
   4303  1.2  haad 	nvlist_t *nvlist = NULL;
   4304  1.2  haad 	int error;
   4305  1.2  haad 
   4306  1.2  haad 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
   4307  1.2  haad 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
   4308  1.2  haad 	zc.zc_cookie = (uint64_t)cmd;
   4309  1.2  haad 
   4310  1.2  haad 	if (cmd == ZFS_SMB_ACL_RENAME) {
   4311  1.2  haad 		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
   4312  1.2  haad 			(void) no_memory(hdl);
   4313  1.4   chs 			return (0);
   4314  1.2  haad 		}
   4315  1.2  haad 	}
   4316  1.2  haad 
   4317  1.2  haad 	switch (cmd) {
   4318  1.2  haad 	case ZFS_SMB_ACL_ADD:
   4319  1.2  haad 	case ZFS_SMB_ACL_REMOVE:
   4320  1.2  haad 		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
   4321  1.2  haad 		break;
   4322  1.2  haad 	case ZFS_SMB_ACL_RENAME:
   4323  1.2  haad 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
   4324  1.2  haad 		    resource1) != 0) {
   4325  1.2  haad 				(void) no_memory(hdl);
   4326  1.2  haad 				return (-1);
   4327  1.2  haad 		}
   4328  1.2  haad 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
   4329  1.2  haad 		    resource2) != 0) {
   4330  1.2  haad 				(void) no_memory(hdl);
   4331  1.2  haad 				return (-1);
   4332  1.2  haad 		}
   4333  1.2  haad 		if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
   4334  1.2  haad 			nvlist_free(nvlist);
   4335  1.2  haad 			return (-1);
   4336  1.2  haad 		}
   4337  1.2  haad 		break;
   4338  1.2  haad 	case ZFS_SMB_ACL_PURGE:
   4339  1.2  haad 		break;
   4340  1.2  haad 	default:
   4341  1.2  haad 		return (-1);
   4342  1.2  haad 	}
   4343  1.2  haad 	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
   4344  1.4   chs 	nvlist_free(nvlist);
   4345  1.2  haad 	return (error);
   4346  1.2  haad }
   4347  1.2  haad 
   4348  1.2  haad int
   4349  1.2  haad zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
   4350  1.2  haad     char *path, char *resource)
   4351  1.2  haad {
   4352  1.2  haad 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
   4353  1.2  haad 	    resource, NULL));
   4354  1.2  haad }
   4355  1.2  haad 
   4356  1.2  haad int
   4357  1.2  haad zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
   4358  1.2  haad     char *path, char *resource)
   4359  1.2  haad {
   4360  1.2  haad 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
   4361  1.2  haad 	    resource, NULL));
   4362  1.2  haad }
   4363  1.2  haad 
   4364  1.2  haad int
   4365  1.2  haad zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
   4366  1.2  haad {
   4367  1.2  haad 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
   4368  1.2  haad 	    NULL, NULL));
   4369  1.2  haad }
   4370  1.2  haad 
   4371  1.2  haad int
   4372  1.2  haad zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
   4373  1.2  haad     char *oldname, char *newname)
   4374  1.2  haad {
   4375  1.2  haad 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
   4376  1.2  haad 	    oldname, newname));
   4377  1.2  haad }
   4378  1.4   chs #endif	/* illumos */
   4379  1.2  haad 
   4380  1.2  haad int
   4381  1.2  haad zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
   4382  1.2  haad     zfs_userspace_cb_t func, void *arg)
   4383  1.2  haad {
   4384  1.2  haad 	zfs_cmd_t zc = { 0 };
   4385  1.2  haad 	zfs_useracct_t buf[100];
   4386  1.4   chs 	libzfs_handle_t *hdl = zhp->zfs_hdl;
   4387  1.4   chs 	int ret;
   4388  1.2  haad 
   4389  1.4   chs 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
   4390  1.2  haad 
   4391  1.2  haad 	zc.zc_objset_type = type;
   4392  1.2  haad 	zc.zc_nvlist_dst = (uintptr_t)buf;
   4393  1.2  haad 
   4394  1.4   chs 	for (;;) {
   4395  1.2  haad 		zfs_useracct_t *zua = buf;
   4396  1.2  haad 
   4397  1.2  haad 		zc.zc_nvlist_dst_size = sizeof (buf);
   4398  1.4   chs 		if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
   4399  1.4   chs 			char errbuf[1024];
   4400  1.4   chs 
   4401  1.4   chs 			(void) snprintf(errbuf, sizeof (errbuf),
   4402  1.4   chs 			    dgettext(TEXT_DOMAIN,
   4403  1.4   chs 			    "cannot get used/quota for %s"), zc.zc_name);
   4404  1.4   chs 			return (zfs_standard_error_fmt(hdl, errno, errbuf));
   4405  1.4   chs 		}
   4406  1.4   chs 		if (zc.zc_nvlist_dst_size == 0)
   4407  1.2  haad 			break;
   4408  1.2  haad 
   4409  1.2  haad 		while (zc.zc_nvlist_dst_size > 0) {
   4410  1.4   chs 			if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
   4411  1.4   chs 			    zua->zu_space)) != 0)
   4412  1.4   chs 				return (ret);
   4413  1.2  haad 			zua++;
   4414  1.2  haad 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
   4415  1.2  haad 		}
   4416  1.2  haad 	}
   4417  1.2  haad 
   4418  1.4   chs 	return (0);
   4419  1.4   chs }
   4420  1.4   chs 
   4421  1.4   chs struct holdarg {
   4422  1.4   chs 	nvlist_t *nvl;
   4423  1.4   chs 	const char *snapname;
   4424  1.4   chs 	const char *tag;
   4425  1.4   chs 	boolean_t recursive;
   4426  1.4   chs 	int error;
   4427  1.4   chs };
   4428  1.4   chs 
   4429  1.4   chs static int
   4430  1.4   chs zfs_hold_one(zfs_handle_t *zhp, void *arg)
   4431  1.4   chs {
   4432  1.4   chs 	struct holdarg *ha = arg;
   4433  1.4   chs 	char name[ZFS_MAX_DATASET_NAME_LEN];
   4434  1.4   chs 	int rv = 0;
   4435  1.4   chs 
   4436  1.4   chs 	(void) snprintf(name, sizeof (name),
   4437  1.4   chs 	    "%s@%s", zhp->zfs_name, ha->snapname);
   4438  1.4   chs 
   4439  1.4   chs 	if (lzc_exists(name))
   4440  1.4   chs 		fnvlist_add_string(ha->nvl, name, ha->tag);
   4441  1.4   chs 
   4442  1.4   chs 	if (ha->recursive)
   4443  1.4   chs 		rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
   4444  1.4   chs 	zfs_close(zhp);
   4445  1.4   chs 	return (rv);
   4446  1.2  haad }
   4447  1.2  haad 
   4448  1.2  haad int
   4449  1.2  haad zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
   4450  1.4   chs     boolean_t recursive, int cleanup_fd)
   4451  1.2  haad {
   4452  1.4   chs 	int ret;
   4453  1.4   chs 	struct holdarg ha;
   4454  1.4   chs 
   4455  1.4   chs 	ha.nvl = fnvlist_alloc();
   4456  1.4   chs 	ha.snapname = snapname;
   4457  1.4   chs 	ha.tag = tag;
   4458  1.4   chs 	ha.recursive = recursive;
   4459  1.4   chs 	(void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
   4460  1.4   chs 
   4461  1.4   chs 	if (nvlist_empty(ha.nvl)) {
   4462  1.4   chs 		char errbuf[1024];
   4463  1.4   chs 
   4464  1.4   chs 		fnvlist_free(ha.nvl);
   4465  1.4   chs 		ret = ENOENT;
   4466  1.4   chs 		(void) snprintf(errbuf, sizeof (errbuf),
   4467  1.4   chs 		    dgettext(TEXT_DOMAIN,
   4468  1.4   chs 		    "cannot hold snapshot '%s@%s'"),
   4469  1.4   chs 		    zhp->zfs_name, snapname);
   4470  1.4   chs 		(void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
   4471  1.4   chs 		return (ret);
   4472  1.4   chs 	}
   4473  1.4   chs 
   4474  1.4   chs 	ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
   4475  1.4   chs 	fnvlist_free(ha.nvl);
   4476  1.4   chs 
   4477  1.4   chs 	return (ret);
   4478  1.4   chs }
   4479  1.4   chs 
   4480  1.4   chs int
   4481  1.4   chs zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
   4482  1.4   chs {
   4483  1.4   chs 	int ret;
   4484  1.4   chs 	nvlist_t *errors;
   4485  1.2  haad 	libzfs_handle_t *hdl = zhp->zfs_hdl;
   4486  1.4   chs 	char errbuf[1024];
   4487  1.4   chs 	nvpair_t *elem;
   4488  1.4   chs 
   4489  1.4   chs 	errors = NULL;
   4490  1.4   chs 	ret = lzc_hold(holds, cleanup_fd, &errors);
   4491  1.2  haad 
   4492  1.4   chs 	if (ret == 0) {
   4493  1.4   chs 		/* There may be errors even in the success case. */
   4494  1.4   chs 		fnvlist_free(errors);
   4495  1.4   chs 		return (0);
   4496  1.4   chs 	}
   4497  1.2  haad 
   4498  1.4   chs 	if (nvlist_empty(errors)) {
   4499  1.4   chs 		/* no hold-specific errors */
   4500  1.4   chs 		(void) snprintf(errbuf, sizeof (errbuf),
   4501  1.4   chs 		    dgettext(TEXT_DOMAIN, "cannot hold"));
   4502  1.4   chs 		switch (ret) {
   4503  1.4   chs 		case ENOTSUP:
   4504  1.4   chs 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   4505  1.4   chs 			    "pool must be upgraded"));
   4506  1.4   chs 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
   4507  1.4   chs 			break;
   4508  1.4   chs 		case EINVAL:
   4509  1.4   chs 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
   4510  1.4   chs 			break;
   4511  1.4   chs 		default:
   4512  1.4   chs 			(void) zfs_standard_error(hdl, ret, errbuf);
   4513  1.4   chs 		}
   4514  1.4   chs 	}
   4515  1.2  haad 
   4516  1.4   chs 	for (elem = nvlist_next_nvpair(errors, NULL);
   4517  1.4   chs 	    elem != NULL;
   4518  1.4   chs 	    elem = nvlist_next_nvpair(errors, elem)) {
   4519  1.4   chs 		(void) snprintf(errbuf, sizeof (errbuf),
   4520  1.4   chs 		    dgettext(TEXT_DOMAIN,
   4521  1.4   chs 		    "cannot hold snapshot '%s'"), nvpair_name(elem));
   4522  1.4   chs 		switch (fnvpair_value_int32(elem)) {
   4523  1.2  haad 		case E2BIG:
   4524  1.2  haad 			/*
   4525  1.2  haad 			 * Temporary tags wind up having the ds object id
   4526  1.2  haad 			 * prepended. So even if we passed the length check
   4527  1.2  haad 			 * above, it's still possible for the tag to wind
   4528  1.2  haad 			 * up being slightly too long.
   4529  1.2  haad 			 */
   4530  1.4   chs 			(void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
   4531  1.4   chs 			break;
   4532  1.2  haad 		case EINVAL:
   4533  1.4   chs 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
   4534  1.4   chs 			break;
   4535  1.2  haad 		case EEXIST:
   4536  1.4   chs 			(void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
   4537  1.4   chs 			break;
   4538  1.2  haad 		default:
   4539  1.4   chs 			(void) zfs_standard_error(hdl,
   4540  1.4   chs 			    fnvpair_value_int32(elem), errbuf);
   4541  1.2  haad 		}
   4542  1.2  haad 	}
   4543  1.2  haad 
   4544  1.4   chs 	fnvlist_free(errors);
   4545  1.4   chs 	return (ret);
   4546  1.2  haad }
   4547  1.2  haad 
   4548  1.4   chs static int
   4549  1.4   chs zfs_release_one(zfs_handle_t *zhp, void *arg)
   4550  1.4   chs {
   4551  1.4   chs 	struct holdarg *ha = arg;
   4552  1.4   chs 	char name[ZFS_MAX_DATASET_NAME_LEN];
   4553  1.4   chs 	int rv = 0;
   4554  1.4   chs 	nvlist_t *existing_holds;
   4555  1.4   chs 
   4556  1.4   chs 	(void) snprintf(name, sizeof (name),
   4557  1.4   chs 	    "%s@%s", zhp->zfs_name, ha->snapname);
   4558  1.4   chs 
   4559  1.4   chs 	if (lzc_get_holds(name, &existing_holds) != 0) {
   4560  1.4   chs 		ha->error = ENOENT;
   4561  1.4   chs 	} else if (!nvlist_exists(existing_holds, ha->tag)) {
   4562  1.4   chs 		ha->error = ESRCH;
   4563  1.4   chs 	} else {
   4564  1.4   chs 		nvlist_t *torelease = fnvlist_alloc();
   4565  1.4   chs 		fnvlist_add_boolean(torelease, ha->tag);
   4566  1.4   chs 		fnvlist_add_nvlist(ha->nvl, name, torelease);
   4567  1.4   chs 		fnvlist_free(torelease);
   4568  1.4   chs 	}
   4569  1.4   chs 
   4570  1.4   chs 	if (ha->recursive)
   4571  1.4   chs 		rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
   4572  1.4   chs 	zfs_close(zhp);
   4573  1.4   chs 	return (rv);
   4574  1.4   chs }
   4575  1.2  haad 
   4576  1.4   chs int
   4577  1.4   chs zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
   4578  1.4   chs     boolean_t recursive)
   4579  1.2  haad {
   4580  1.4   chs 	int ret;
   4581  1.4   chs 	struct holdarg ha;
   4582  1.4   chs 	nvlist_t *errors = NULL;
   4583  1.4   chs 	nvpair_t *elem;
   4584  1.4   chs 	libzfs_handle_t *hdl = zhp->zfs_hdl;
   4585  1.4   chs 	char errbuf[1024];
   4586  1.2  haad 
   4587  1.4   chs 	ha.nvl = fnvlist_alloc();
   4588  1.4   chs 	ha.snapname = snapname;
   4589  1.4   chs 	ha.tag = tag;
   4590  1.4   chs 	ha.recursive = recursive;
   4591  1.4   chs 	ha.error = 0;
   4592  1.4   chs 	(void) zfs_release_one(zfs_handle_dup(zhp), &ha);
   4593  1.4   chs 
   4594  1.4   chs 	if (nvlist_empty(ha.nvl)) {
   4595  1.4   chs 		fnvlist_free(ha.nvl);
   4596  1.4   chs 		ret = ha.error;
   4597  1.4   chs 		(void) snprintf(errbuf, sizeof (errbuf),
   4598  1.4   chs 		    dgettext(TEXT_DOMAIN,
   4599  1.4   chs 		    "cannot release hold from snapshot '%s@%s'"),
   4600  1.4   chs 		    zhp->zfs_name, snapname);
   4601  1.4   chs 		if (ret == ESRCH) {
   4602  1.4   chs 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
   4603  1.4   chs 		} else {
   4604  1.4   chs 			(void) zfs_standard_error(hdl, ret, errbuf);
   4605  1.4   chs 		}
   4606  1.4   chs 		return (ret);
   4607  1.4   chs 	}
   4608  1.2  haad 
   4609  1.4   chs 	ret = lzc_release(ha.nvl, &errors);
   4610  1.4   chs 	fnvlist_free(ha.nvl);
   4611  1.2  haad 
   4612  1.4   chs 	if (ret == 0) {
   4613  1.4   chs 		/* There may be errors even in the success case. */
   4614  1.4   chs 		fnvlist_free(errors);
   4615  1.2  haad 		return (0);
   4616  1.2  haad 	}
   4617  1.2  haad 
   4618  1.4   chs 	if (nvlist_empty(errors)) {
   4619  1.4   chs 		/* no hold-specific errors */
   4620  1.4   chs 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
   4621  1.4   chs 		    "cannot release"));
   4622  1.4   chs 		switch (errno) {
   4623  1.4   chs 		case ENOTSUP:
   4624  1.4   chs 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   4625  1.4   chs 			    "pool must be upgraded"));
   4626  1.4   chs 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
   4627  1.4   chs 			break;
   4628  1.4   chs 		default:
   4629  1.4   chs 			(void) zfs_standard_error_fmt(hdl, errno, errbuf);
   4630  1.2  haad 		}
   4631  1.2  haad 	}
   4632  1.2  haad 
   4633  1.4   chs 	for (elem = nvlist_next_nvpair(errors, NULL);
   4634  1.4   chs 	    elem != NULL;
   4635  1.4   chs 	    elem = nvlist_next_nvpair(errors, elem)) {
   4636  1.4   chs 		(void) snprintf(errbuf, sizeof (errbuf),
   4637  1.4   chs 		    dgettext(TEXT_DOMAIN,
   4638  1.4   chs 		    "cannot release hold from snapshot '%s'"),
   4639  1.4   chs 		    nvpair_name(elem));
   4640  1.4   chs 		switch (fnvpair_value_int32(elem)) {
   4641  1.4   chs 		case ESRCH:
   4642  1.4   chs 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
   4643  1.4   chs 			break;
   4644  1.4   chs 		case EINVAL:
   4645  1.4   chs 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
   4646  1.4   chs 			break;
   4647  1.4   chs 		default:
   4648  1.4   chs 			(void) zfs_standard_error_fmt(hdl,
   4649  1.4   chs 			    fnvpair_value_int32(elem), errbuf);
   4650  1.4   chs 		}
   4651  1.4   chs 	}
   4652  1.2  haad 
   4653  1.4   chs 	fnvlist_free(errors);
   4654  1.4   chs 	return (ret);
   4655  1.2  haad }
   4656  1.2  haad 
   4657  1.2  haad int
   4658  1.4   chs zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
   4659  1.2  haad {
   4660  1.4   chs 	zfs_cmd_t zc = { 0 };
   4661  1.4   chs 	libzfs_handle_t *hdl = zhp->zfs_hdl;
   4662  1.4   chs 	int nvsz = 2048;
   4663  1.4   chs 	void *nvbuf;
   4664  1.4   chs 	int err = 0;
   4665  1.4   chs 	char errbuf[1024];
   4666  1.4   chs 
   4667  1.4   chs 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
   4668  1.4   chs 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
   4669  1.4   chs 
   4670  1.4   chs tryagain:
   4671  1.4   chs 
   4672  1.4   chs 	nvbuf = malloc(nvsz);
   4673  1.4   chs 	if (nvbuf == NULL) {
   4674  1.4   chs 		err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
   4675  1.4   chs 		goto out;
   4676  1.4   chs 	}
   4677  1.2  haad 
   4678  1.4   chs 	zc.zc_nvlist_dst_size = nvsz;
   4679  1.4   chs 	zc.zc_nvlist_dst = (uintptr_t)nvbuf;
   4680  1.2  haad 
   4681  1.4   chs 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
   4682  1.1  haad 
   4683  1.4   chs 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
   4684  1.4   chs 		(void) snprintf(errbuf, sizeof (errbuf),
   4685  1.4   chs 		    dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
   4686  1.4   chs 		    zc.zc_name);
   4687  1.4   chs 		switch (errno) {
   4688  1.4   chs 		case ENOMEM:
   4689  1.4   chs 			free(nvbuf);
   4690  1.4   chs 			nvsz = zc.zc_nvlist_dst_size;
   4691  1.4   chs 			goto tryagain;
   4692  1.4   chs 
   4693  1.4   chs 		case ENOTSUP:
   4694  1.4   chs 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   4695  1.4   chs 			    "pool must be upgraded"));
   4696  1.4   chs 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
   4697  1.4   chs 			break;
   4698  1.4   chs 		case EINVAL:
   4699  1.4   chs 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
   4700  1.4   chs 			break;
   4701  1.4   chs 		case ENOENT:
   4702  1.4   chs 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
   4703  1.4   chs 			break;
   4704  1.4   chs 		default:
   4705  1.4   chs 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
   4706  1.4   chs 			break;
   4707  1.4   chs 		}
   4708  1.4   chs 	} else {
   4709  1.4   chs 		/* success */
   4710  1.4   chs 		int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
   4711  1.4   chs 		if (rc) {
   4712  1.4   chs 			(void) snprintf(errbuf, sizeof (errbuf), dgettext(
   4713  1.4   chs 			    TEXT_DOMAIN, "cannot get permissions on '%s'"),
   4714  1.4   chs 			    zc.zc_name);
   4715  1.4   chs 			err = zfs_standard_error_fmt(hdl, rc, errbuf);
   4716  1.4   chs 		}
   4717  1.2  haad 	}
   4718  1.4   chs 
   4719  1.4   chs 	free(nvbuf);
   4720  1.4   chs out:
   4721  1.4   chs 	return (err);
   4722  1.1  haad }
   4723  1.2  haad 
   4724  1.2  haad int
   4725  1.4   chs zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
   4726  1.2  haad {
   4727  1.2  haad 	zfs_cmd_t zc = { 0 };
   4728  1.2  haad 	libzfs_handle_t *hdl = zhp->zfs_hdl;
   4729  1.4   chs 	char *nvbuf;
   4730  1.4   chs 	char errbuf[1024];
   4731  1.4   chs 	size_t nvsz;
   4732  1.4   chs 	int err;
   4733  1.4   chs 
   4734  1.4   chs 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
   4735  1.4   chs 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
   4736  1.4   chs 
   4737  1.4   chs 	err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
   4738  1.4   chs 	assert(err == 0);
   4739  1.4   chs 
   4740  1.4   chs 	nvbuf = malloc(nvsz);
   4741  1.4   chs 
   4742  1.4   chs 	err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
   4743  1.4   chs 	assert(err == 0);
   4744  1.4   chs 
   4745  1.4   chs 	zc.zc_nvlist_src_size = nvsz;
   4746  1.4   chs 	zc.zc_nvlist_src = (uintptr_t)nvbuf;
   4747  1.4   chs 	zc.zc_perm_action = un;
   4748  1.2  haad 
   4749  1.2  haad 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
   4750  1.2  haad 
   4751  1.4   chs 	if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
   4752  1.4   chs 		(void) snprintf(errbuf, sizeof (errbuf),
   4753  1.4   chs 		    dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
   4754  1.4   chs 		    zc.zc_name);
   4755  1.2  haad 		switch (errno) {
   4756  1.2  haad 		case ENOTSUP:
   4757  1.2  haad 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   4758  1.2  haad 			    "pool must be upgraded"));
   4759  1.4   chs 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
   4760  1.4   chs 			break;
   4761  1.2  haad 		case EINVAL:
   4762  1.4   chs 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
   4763  1.4   chs 			break;
   4764  1.4   chs 		case ENOENT:
   4765  1.4   chs 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
   4766  1.4   chs 			break;
   4767  1.2  haad 		default:
   4768  1.4   chs 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
   4769  1.4   chs 			break;
   4770  1.2  haad 		}
   4771  1.2  haad 	}
   4772  1.2  haad 
   4773  1.4   chs 	free(nvbuf);
   4774  1.4   chs 
   4775  1.4   chs 	return (err);
   4776  1.2  haad }
   4777  1.2  haad 
   4778  1.2  haad int
   4779  1.4   chs zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
   4780  1.2  haad {
   4781  1.4   chs 	int err;
   4782  1.4   chs 	char errbuf[1024];
   4783  1.4   chs 
   4784  1.4   chs 	err = lzc_get_holds(zhp->zfs_name, nvl);
   4785  1.2  haad 
   4786  1.4   chs 	if (err != 0) {
   4787  1.4   chs 		libzfs_handle_t *hdl = zhp->zfs_hdl;
   4788  1.4   chs 
   4789  1.4   chs 		(void) snprintf(errbuf, sizeof (errbuf),
   4790  1.4   chs 		    dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
   4791  1.4   chs 		    zhp->zfs_name);
   4792  1.4   chs 		switch (err) {
   4793  1.4   chs 		case ENOTSUP:
   4794  1.4   chs 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   4795  1.4   chs 			    "pool must be upgraded"));
   4796  1.4   chs 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
   4797  1.4   chs 			break;
   4798  1.4   chs 		case EINVAL:
   4799  1.4   chs 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
   4800  1.4   chs 			break;
   4801  1.4   chs 		case ENOENT:
   4802  1.4   chs 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
   4803  1.4   chs 			break;
   4804  1.4   chs 		default:
   4805  1.4   chs 			err = zfs_standard_error_fmt(hdl, errno, errbuf);
   4806  1.4   chs 			break;
   4807  1.4   chs 		}
   4808  1.4   chs 	}
   4809  1.2  haad 
   4810  1.4   chs 	return (err);
   4811  1.2  haad }
   4812  1.2  haad 
   4813  1.4   chs /*
   4814  1.4   chs  * Convert the zvol's volume size to an appropriate reservation.
   4815  1.4   chs  * Note: If this routine is updated, it is necessary to update the ZFS test
   4816  1.4   chs  * suite's shell version in reservation.kshlib.
   4817  1.4   chs  */
   4818  1.2  haad uint64_t
   4819  1.2  haad zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
   4820  1.2  haad {
   4821  1.2  haad 	uint64_t numdb;
   4822  1.2  haad 	uint64_t nblocks, volblocksize;
   4823  1.2  haad 	int ncopies;
   4824  1.2  haad 	char *strval;
   4825  1.2  haad 
   4826  1.2  haad 	if (nvlist_lookup_string(props,
   4827  1.2  haad 	    zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
   4828  1.2  haad 		ncopies = atoi(strval);
   4829  1.2  haad 	else
   4830  1.2  haad 		ncopies = 1;
   4831  1.2  haad 	if (nvlist_lookup_uint64(props,
   4832  1.2  haad 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
   4833  1.2  haad 	    &volblocksize) != 0)
   4834  1.2  haad 		volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
   4835  1.2  haad 	nblocks = volsize/volblocksize;
   4836  1.2  haad 	/* start with metadnode L0-L6 */
   4837  1.2  haad 	numdb = 7;
   4838  1.2  haad 	/* calculate number of indirects */
   4839  1.2  haad 	while (nblocks > 1) {
   4840  1.2  haad 		nblocks += DNODES_PER_LEVEL - 1;
   4841  1.2  haad 		nblocks /= DNODES_PER_LEVEL;
   4842  1.2  haad 		numdb += nblocks;
   4843  1.2  haad 	}
   4844  1.2  haad 	numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
   4845  1.2  haad 	volsize *= ncopies;
   4846  1.2  haad 	/*
   4847  1.2  haad 	 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
   4848  1.2  haad 	 * compressed, but in practice they compress down to about
   4849  1.2  haad 	 * 1100 bytes
   4850  1.2  haad 	 */
   4851  1.2  haad 	numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
   4852  1.2  haad 	volsize += numdb;
   4853  1.2  haad 	return (volsize);
   4854  1.2  haad }
   4855  1.4   chs 
   4856  1.4   chs #ifdef __FreeBSD__
   4857  1.4   chs /*
   4858  1.4   chs  * Attach/detach the given filesystem to/from the given jail.
   4859  1.4   chs  */
   4860  1.4   chs int
   4861  1.4   chs zfs_jail(zfs_handle_t *zhp, int jailid, int attach)
   4862  1.4   chs {
   4863  1.4   chs 	libzfs_handle_t *hdl = zhp->zfs_hdl;
   4864  1.4   chs 	zfs_cmd_t zc = { 0 };
   4865  1.4   chs 	char errbuf[1024];
   4866  1.4   chs 	unsigned long cmd;
   4867  1.4   chs 	int ret;
   4868  1.4   chs 
   4869  1.4   chs 	if (attach) {
   4870  1.4   chs 		(void) snprintf(errbuf, sizeof (errbuf),
   4871  1.4   chs 		    dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
   4872  1.4   chs 	} else {
   4873  1.4   chs 		(void) snprintf(errbuf, sizeof (errbuf),
   4874  1.4   chs 		    dgettext(TEXT_DOMAIN, "cannot unjail '%s'"), zhp->zfs_name);
   4875  1.4   chs 	}
   4876  1.4   chs 
   4877  1.4   chs 	switch (zhp->zfs_type) {
   4878  1.4   chs 	case ZFS_TYPE_VOLUME:
   4879  1.4   chs 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   4880  1.4   chs 		    "volumes can not be jailed"));
   4881  1.4   chs 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
   4882  1.4   chs 	case ZFS_TYPE_SNAPSHOT:
   4883  1.4   chs 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   4884  1.4   chs 		    "snapshots can not be jailed"));
   4885  1.4   chs 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
   4886  1.4   chs 	}
   4887  1.4   chs 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
   4888  1.4   chs 
   4889  1.4   chs 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
   4890  1.4   chs 	zc.zc_objset_type = DMU_OST_ZFS;
   4891  1.4   chs 	zc.zc_jailid = jailid;
   4892  1.4   chs 
   4893  1.4   chs 	cmd = attach ? ZFS_IOC_JAIL : ZFS_IOC_UNJAIL;
   4894  1.4   chs 	if ((ret = ioctl(hdl->libzfs_fd, cmd, &zc)) != 0)
   4895  1.4   chs 		zfs_standard_error(hdl, errno, errbuf);
   4896  1.4   chs 
   4897  1.4   chs 	return (ret);
   4898  1.4   chs }
   4899  1.4   chs #endif
   4900