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