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