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                 /*
 965                  * Special handling for "xxx_limit=none". In this case its not
 966                  * 0 but MAXLIMIT.
 967                  */
 968                 if ((prop == ZFS_PROP_FILESYSTEM_LIMIT ||
 969                     prop == ZFS_PROP_SNAPSHOT_LIMIT) &&
 970                     nvpair_type(elem) == DATA_TYPE_STRING) {
 971                         (void) nvpair_value_string(elem, &strval);
 972                         if (strcmp(strval, "none") == 0) {
 973                                 if (nvlist_add_uint64(ret, propname,
 974                                     MAXLIMIT) != 0) {
 975                                         (void) no_memory(hdl);
 976                                         goto error;
 977                                 }
 978                                 continue;
 979                         }
 980                 }
 981 
 982                 if (zprop_parse_value(hdl, elem, prop, type, ret,
 983                     &strval, &intval, errbuf) != 0)
 984                         goto error;
 985 
 986                 /*
 987                  * Perform some additional checks for specific properties.
 988                  */
 989                 switch (prop) {
 990                 case ZFS_PROP_VERSION:
 991                 {
 992                         int version;
 993 
 994                         if (zhp == NULL)
 995                                 break;
 996                         version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
 997                         if (intval < version) {
 998                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 999                                     "Can not downgrade; already at version %u"),
1000                                     version);
1001                                 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1002                                 goto error;
1003                         }
1004                         break;
1005                 }
1006 
1007                 case ZFS_PROP_RECORDSIZE:
1008                 case ZFS_PROP_VOLBLOCKSIZE:
1009                         /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
1010                         if (intval < SPA_MINBLOCKSIZE ||
1011                             intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
1012                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1013                                     "'%s' must be power of 2 from %u "
1014                                     "to %uk"), propname,
1015                                     (uint_t)SPA_MINBLOCKSIZE,
1016                                     (uint_t)SPA_MAXBLOCKSIZE >> 10);
1017                                 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1018                                 goto error;
1019                         }
1020                         break;
1021 
1022                 case ZFS_PROP_MLSLABEL:
1023                 {
1024                         /*
1025                          * Verify the mlslabel string and convert to
1026                          * internal hex label string.
1027                          */
1028 
1029                         m_label_t *new_sl;
1030                         char *hex = NULL;       /* internal label string */
1031 
1032                         /* Default value is already OK. */
1033                         if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1034                                 break;
1035 
1036                         /* Verify the label can be converted to binary form */
1037                         if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1038                             (str_to_label(strval, &new_sl, MAC_LABEL,
1039                             L_NO_CORRECTION, NULL) == -1)) {
1040                                 goto badlabel;
1041                         }
1042 
1043                         /* Now translate to hex internal label string */
1044                         if (label_to_str(new_sl, &hex, M_INTERNAL,
1045                             DEF_NAMES) != 0) {
1046                                 if (hex)
1047                                         free(hex);
1048                                 goto badlabel;
1049                         }
1050                         m_label_free(new_sl);
1051 
1052                         /* If string is already in internal form, we're done. */
1053                         if (strcmp(strval, hex) == 0) {
1054                                 free(hex);
1055                                 break;
1056                         }
1057 
1058                         /* Replace the label string with the internal form. */
1059                         (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1060                             DATA_TYPE_STRING);
1061                         verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1062                             hex) == 0);
1063                         free(hex);
1064 
1065                         break;
1066 
1067 badlabel:
1068                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1069                             "invalid mlslabel '%s'"), strval);
1070                         (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1071                         m_label_free(new_sl);   /* OK if null */
1072                         goto error;
1073 
1074                 }
1075 
1076                 case ZFS_PROP_MOUNTPOINT:
1077                 {
1078                         namecheck_err_t why;
1079 
1080                         if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1081                             strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1082                                 break;
1083 
1084                         if (mountpoint_namecheck(strval, &why)) {
1085                                 switch (why) {
1086                                 case NAME_ERR_LEADING_SLASH:
1087                                         zfs_error_aux(hdl,
1088                                             dgettext(TEXT_DOMAIN,
1089                                             "'%s' must be an absolute path, "
1090                                             "'none', or 'legacy'"), propname);
1091                                         break;
1092                                 case NAME_ERR_TOOLONG:
1093                                         zfs_error_aux(hdl,
1094                                             dgettext(TEXT_DOMAIN,
1095                                             "component of '%s' is too long"),
1096                                             propname);
1097                                         break;
1098                                 }
1099                                 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1100                                 goto error;
1101                         }
1102                 }
1103 
1104                         /*FALLTHRU*/
1105 
1106                 case ZFS_PROP_SHARESMB:
1107                 case ZFS_PROP_SHARENFS:
1108                         /*
1109                          * For the mountpoint and sharenfs or sharesmb
1110                          * properties, check if it can be set in a
1111                          * global/non-global zone based on
1112                          * the zoned property value:
1113                          *
1114                          *              global zone         non-global zone
1115                          * --------------------------------------------------
1116                          * zoned=on     mountpoint (no)     mountpoint (yes)
1117                          *              sharenfs (no)       sharenfs (no)
1118                          *              sharesmb (no)       sharesmb (no)
1119                          *
1120                          * zoned=off    mountpoint (yes)        N/A
1121                          *              sharenfs (yes)
1122                          *              sharesmb (yes)
1123                          */
1124                         if (zoned) {
1125                                 if (getzoneid() == GLOBAL_ZONEID) {
1126                                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1127                                             "'%s' cannot be set on "
1128                                             "dataset in a non-global zone"),
1129                                             propname);
1130                                         (void) zfs_error(hdl, EZFS_ZONED,
1131                                             errbuf);
1132                                         goto error;
1133                                 } else if (prop == ZFS_PROP_SHARENFS ||
1134                                     prop == ZFS_PROP_SHARESMB) {
1135                                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1136                                             "'%s' cannot be set in "
1137                                             "a non-global zone"), propname);
1138                                         (void) zfs_error(hdl, EZFS_ZONED,
1139                                             errbuf);
1140                                         goto error;
1141                                 }
1142                         } else if (getzoneid() != GLOBAL_ZONEID) {
1143                                 /*
1144                                  * If zoned property is 'off', this must be in
1145                                  * a global zone. If not, something is wrong.
1146                                  */
1147                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1148                                     "'%s' cannot be set while dataset "
1149                                     "'zoned' property is set"), propname);
1150                                 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1151                                 goto error;
1152                         }
1153 
1154                         /*
1155                          * At this point, it is legitimate to set the
1156                          * property. Now we want to make sure that the
1157                          * property value is valid if it is sharenfs.
1158                          */
1159                         if ((prop == ZFS_PROP_SHARENFS ||
1160                             prop == ZFS_PROP_SHARESMB) &&
1161                             strcmp(strval, "on") != 0 &&
1162                             strcmp(strval, "off") != 0) {
1163                                 zfs_share_proto_t proto;
1164 
1165                                 if (prop == ZFS_PROP_SHARESMB)
1166                                         proto = PROTO_SMB;
1167                                 else
1168                                         proto = PROTO_NFS;
1169 
1170                                 /*
1171                                  * Must be an valid sharing protocol
1172                                  * option string so init the libshare
1173                                  * in order to enable the parser and
1174                                  * then parse the options. We use the
1175                                  * control API since we don't care about
1176                                  * the current configuration and don't
1177                                  * want the overhead of loading it
1178                                  * until we actually do something.
1179                                  */
1180 
1181                                 if (zfs_init_libshare(hdl,
1182                                     SA_INIT_CONTROL_API) != SA_OK) {
1183                                         /*
1184                                          * An error occurred so we can't do
1185                                          * anything
1186                                          */
1187                                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1188                                             "'%s' cannot be set: problem "
1189                                             "in share initialization"),
1190                                             propname);
1191                                         (void) zfs_error(hdl, EZFS_BADPROP,
1192                                             errbuf);
1193                                         goto error;
1194                                 }
1195 
1196                                 if (zfs_parse_options(strval, proto) != SA_OK) {
1197                                         /*
1198                                          * There was an error in parsing so
1199                                          * deal with it by issuing an error
1200                                          * message and leaving after
1201                                          * uninitializing the the libshare
1202                                          * interface.
1203                                          */
1204                                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1205                                             "'%s' cannot be set to invalid "
1206                                             "options"), propname);
1207                                         (void) zfs_error(hdl, EZFS_BADPROP,
1208                                             errbuf);
1209                                         zfs_uninit_libshare(hdl);
1210                                         goto error;
1211                                 }
1212                                 zfs_uninit_libshare(hdl);
1213                         }
1214 
1215                         break;
1216                 case ZFS_PROP_UTF8ONLY:
1217                         chosen_utf = (int)intval;
1218                         break;
1219                 case ZFS_PROP_NORMALIZE:
1220                         chosen_normal = (int)intval;
1221                         break;
1222                 }
1223 
1224                 /*
1225                  * For changes to existing volumes, we have some additional
1226                  * checks to enforce.
1227                  */
1228                 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1229                         uint64_t volsize = zfs_prop_get_int(zhp,
1230                             ZFS_PROP_VOLSIZE);
1231                         uint64_t blocksize = zfs_prop_get_int(zhp,
1232                             ZFS_PROP_VOLBLOCKSIZE);
1233                         char buf[64];
1234 
1235                         switch (prop) {
1236                         case ZFS_PROP_RESERVATION:
1237                         case ZFS_PROP_REFRESERVATION:
1238                                 if (intval > volsize) {
1239                                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1240                                             "'%s' is greater than current "
1241                                             "volume size"), propname);
1242                                         (void) zfs_error(hdl, EZFS_BADPROP,
1243                                             errbuf);
1244                                         goto error;
1245                                 }
1246                                 break;
1247 
1248                         case ZFS_PROP_VOLSIZE:
1249                                 if (intval % blocksize != 0) {
1250                                         zfs_nicenum(blocksize, buf,
1251                                             sizeof (buf));
1252                                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1253                                             "'%s' must be a multiple of "
1254                                             "volume block size (%s)"),
1255                                             propname, buf);
1256                                         (void) zfs_error(hdl, EZFS_BADPROP,
1257                                             errbuf);
1258                                         goto error;
1259                                 }
1260 
1261                                 if (intval == 0) {
1262                                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1263                                             "'%s' cannot be zero"),
1264                                             propname);
1265                                         (void) zfs_error(hdl, EZFS_BADPROP,
1266                                             errbuf);
1267                                         goto error;
1268                                 }
1269                                 break;
1270                         }
1271                 }
1272         }
1273 
1274         /*
1275          * If normalization was chosen, but no UTF8 choice was made,
1276          * enforce rejection of non-UTF8 names.
1277          *
1278          * If normalization was chosen, but rejecting non-UTF8 names
1279          * was explicitly not chosen, it is an error.
1280          */
1281         if (chosen_normal > 0 && chosen_utf < 0) {
1282                 if (nvlist_add_uint64(ret,
1283                     zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1284                         (void) no_memory(hdl);
1285                         goto error;
1286                 }
1287         } else if (chosen_normal > 0 && chosen_utf == 0) {
1288                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1289                     "'%s' must be set 'on' if normalization chosen"),
1290                     zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1291                 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1292                 goto error;
1293         }
1294         return (ret);
1295 
1296 error:
1297         nvlist_free(ret);
1298         return (NULL);
1299 }
1300 
1301 int
1302 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1303 {
1304         uint64_t old_volsize;
1305         uint64_t new_volsize;
1306         uint64_t old_reservation;
1307         uint64_t new_reservation;
1308         zfs_prop_t resv_prop;
1309 
1310         /*
1311          * If this is an existing volume, and someone is setting the volsize,
1312          * make sure that it matches the reservation, or add it if necessary.
1313          */
1314         old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1315         if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1316                 return (-1);
1317         old_reservation = zfs_prop_get_int(zhp, resv_prop);
1318         if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
1319             old_reservation) || nvlist_lookup_uint64(nvl,
1320             zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
1321                 return (0);
1322         }
1323         if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1324             &new_volsize) != 0)
1325                 return (-1);
1326         new_reservation = zvol_volsize_to_reservation(new_volsize,
1327             zhp->zfs_props);
1328         if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1329             new_reservation) != 0) {
1330                 (void) no_memory(zhp->zfs_hdl);
1331                 return (-1);
1332         }
1333         return (1);
1334 }
1335 
1336 void
1337 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1338     char *errbuf)
1339 {
1340         switch (err) {
1341 
1342         case ENOSPC:
1343                 /*
1344                  * For quotas and reservations, ENOSPC indicates
1345                  * something different; setting a quota or reservation
1346                  * doesn't use any disk space.
1347                  */
1348                 switch (prop) {
1349                 case ZFS_PROP_QUOTA:
1350                 case ZFS_PROP_REFQUOTA:
1351                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1352                             "size is less than current used or "
1353                             "reserved space"));
1354                         (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1355                         break;
1356 
1357                 case ZFS_PROP_RESERVATION:
1358                 case ZFS_PROP_REFRESERVATION:
1359                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1360                             "size is greater than available space"));
1361                         (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1362                         break;
1363 
1364                 default:
1365                         (void) zfs_standard_error(hdl, err, errbuf);
1366                         break;
1367                 }
1368                 break;
1369 
1370         case EBUSY:
1371                 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1372                 break;
1373 
1374         case EROFS:
1375                 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1376                 break;
1377 
1378         case ENOTSUP:
1379                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1380                     "pool and or dataset must be upgraded to set this "
1381                     "property or value"));
1382                 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1383                 break;
1384 
1385         case ERANGE:
1386                 if (prop == ZFS_PROP_COMPRESSION) {
1387                         (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1388                             "property setting is not allowed on "
1389                             "bootable datasets"));
1390                         (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1391                 } else {
1392                         (void) zfs_standard_error(hdl, err, errbuf);
1393                 }
1394                 break;
1395 
1396         case EINVAL:
1397                 if (prop == ZPROP_INVAL) {
1398                         (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1399                 } else {
1400                         (void) zfs_standard_error(hdl, err, errbuf);
1401                 }
1402                 break;
1403 
1404         case EOVERFLOW:
1405                 /*
1406                  * This platform can't address a volume this big.
1407                  */
1408 #ifdef _ILP32
1409                 if (prop == ZFS_PROP_VOLSIZE) {
1410                         (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1411                         break;
1412                 }
1413 #endif
1414                 /* FALLTHROUGH */
1415         default:
1416                 (void) zfs_standard_error(hdl, err, errbuf);
1417         }
1418 }
1419 
1420 /*
1421  * Given a property name and value, set the property for the given dataset.
1422  */
1423 int
1424 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1425 {
1426         zfs_cmd_t zc = { 0 };
1427         int ret = -1;
1428         prop_changelist_t *cl = NULL;
1429         char errbuf[1024];
1430         libzfs_handle_t *hdl = zhp->zfs_hdl;
1431         nvlist_t *nvl = NULL, *realprops;
1432         zfs_prop_t prop;
1433         boolean_t do_prefix = B_TRUE;
1434         int added_resv;
1435 
1436         (void) snprintf(errbuf, sizeof (errbuf),
1437             dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1438             zhp->zfs_name);
1439 
1440         if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1441             nvlist_add_string(nvl, propname, propval) != 0) {
1442                 (void) no_memory(hdl);
1443                 goto error;
1444         }
1445 
1446         if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1447             zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1448                 goto error;
1449 
1450         nvlist_free(nvl);
1451         nvl = realprops;
1452 
1453         prop = zfs_name_to_prop(propname);
1454 
1455         if (prop == ZFS_PROP_VOLSIZE) {
1456                 if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1457                         goto error;
1458         }
1459 
1460         if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1461                 goto error;
1462 
1463         if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1464                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1465                     "child dataset with inherited mountpoint is used "
1466                     "in a non-global zone"));
1467                 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1468                 goto error;
1469         }
1470 
1471         /*
1472          * We don't want to unmount & remount the dataset when changing
1473          * its canmount property to 'on' or 'noauto'.  We only use
1474          * the changelist logic to unmount when setting canmount=off.
1475          */
1476         if (prop == ZFS_PROP_CANMOUNT) {
1477                 uint64_t idx;
1478                 int err = zprop_string_to_index(prop, propval, &idx,
1479                     ZFS_TYPE_DATASET);
1480                 if (err == 0 && idx != ZFS_CANMOUNT_OFF)
1481                         do_prefix = B_FALSE;
1482         }
1483 
1484         if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1485                 goto error;
1486 
1487         /*
1488          * Execute the corresponding ioctl() to set this property.
1489          */
1490         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1491 
1492         if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1493                 goto error;
1494 
1495         ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1496 
1497         if (ret != 0) {
1498                 zfs_setprop_error(hdl, prop, errno, errbuf);
1499                 if (added_resv && errno == ENOSPC) {
1500                         /* clean up the volsize property we tried to set */
1501                         uint64_t old_volsize = zfs_prop_get_int(zhp,
1502                             ZFS_PROP_VOLSIZE);
1503                         nvlist_free(nvl);
1504                         zcmd_free_nvlists(&zc);
1505                         if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1506                                 goto error;
1507                         if (nvlist_add_uint64(nvl,
1508                             zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1509                             old_volsize) != 0)
1510                                 goto error;
1511                         if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1512                                 goto error;
1513                         (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1514                 }
1515         } else {
1516                 if (do_prefix)
1517                         ret = changelist_postfix(cl);
1518 
1519                 /*
1520                  * Refresh the statistics so the new property value
1521                  * is reflected.
1522                  */
1523                 if (ret == 0)
1524                         (void) get_stats(zhp);
1525         }
1526 
1527 error:
1528         nvlist_free(nvl);
1529         zcmd_free_nvlists(&zc);
1530         if (cl)
1531                 changelist_free(cl);
1532         return (ret);
1533 }
1534 
1535 /*
1536  * Given a property, inherit the value from the parent dataset, or if received
1537  * is TRUE, revert to the received value, if any.
1538  */
1539 int
1540 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1541 {
1542         zfs_cmd_t zc = { 0 };
1543         int ret;
1544         prop_changelist_t *cl;
1545         libzfs_handle_t *hdl = zhp->zfs_hdl;
1546         char errbuf[1024];
1547         zfs_prop_t prop;
1548 
1549         (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1550             "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1551 
1552         zc.zc_cookie = received;
1553         if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1554                 /*
1555                  * For user properties, the amount of work we have to do is very
1556                  * small, so just do it here.
1557                  */
1558                 if (!zfs_prop_user(propname)) {
1559                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1560                             "invalid property"));
1561                         return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1562                 }
1563 
1564                 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1565                 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1566 
1567                 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1568                         return (zfs_standard_error(hdl, errno, errbuf));
1569 
1570                 return (0);
1571         }
1572 
1573         /*
1574          * Verify that this property is inheritable.
1575          */
1576         if (zfs_prop_readonly(prop))
1577                 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1578 
1579         if (!zfs_prop_inheritable(prop) && !received)
1580                 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1581 
1582         /*
1583          * Check to see if the value applies to this type
1584          */
1585         if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1586                 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1587 
1588         /*
1589          * Normalize the name, to get rid of shorthand abbreviations.
1590          */
1591         propname = zfs_prop_to_name(prop);
1592         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1593         (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1594 
1595         if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1596             zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1597                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1598                     "dataset is used in a non-global zone"));
1599                 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1600         }
1601 
1602         /*
1603          * Determine datasets which will be affected by this change, if any.
1604          */
1605         if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1606                 return (-1);
1607 
1608         if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1609                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1610                     "child dataset with inherited mountpoint is used "
1611                     "in a non-global zone"));
1612                 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1613                 goto error;
1614         }
1615 
1616         if ((ret = changelist_prefix(cl)) != 0)
1617                 goto error;
1618 
1619         if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1620                 return (zfs_standard_error(hdl, errno, errbuf));
1621         } else {
1622 
1623                 if ((ret = changelist_postfix(cl)) != 0)
1624                         goto error;
1625 
1626                 /*
1627                  * Refresh the statistics so the new property is reflected.
1628                  */
1629                 (void) get_stats(zhp);
1630         }
1631 
1632 error:
1633         changelist_free(cl);
1634         return (ret);
1635 }
1636 
1637 /*
1638  * True DSL properties are stored in an nvlist.  The following two functions
1639  * extract them appropriately.
1640  */
1641 static uint64_t
1642 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1643 {
1644         nvlist_t *nv;
1645         uint64_t value;
1646 
1647         *source = NULL;
1648         if (nvlist_lookup_nvlist(zhp->zfs_props,
1649             zfs_prop_to_name(prop), &nv) == 0) {
1650                 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1651                 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1652         } else {
1653                 verify(!zhp->zfs_props_table ||
1654                     zhp->zfs_props_table[prop] == B_TRUE);
1655                 value = zfs_prop_default_numeric(prop);
1656                 *source = "";
1657         }
1658 
1659         return (value);
1660 }
1661 
1662 static char *
1663 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1664 {
1665         nvlist_t *nv;
1666         char *value;
1667 
1668         *source = NULL;
1669         if (nvlist_lookup_nvlist(zhp->zfs_props,
1670             zfs_prop_to_name(prop), &nv) == 0) {
1671                 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1672                 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1673         } else {
1674                 verify(!zhp->zfs_props_table ||
1675                     zhp->zfs_props_table[prop] == B_TRUE);
1676                 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1677                         value = "";
1678                 *source = "";
1679         }
1680 
1681         return (value);
1682 }
1683 
1684 static boolean_t
1685 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1686 {
1687         return (zhp->zfs_props == zhp->zfs_recvd_props);
1688 }
1689 
1690 static void
1691 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1692 {
1693         *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1694         zhp->zfs_props = zhp->zfs_recvd_props;
1695 }
1696 
1697 static void
1698 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1699 {
1700         zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1701         *cookie = 0;
1702 }
1703 
1704 /*
1705  * Internal function for getting a numeric property.  Both zfs_prop_get() and
1706  * zfs_prop_get_int() are built using this interface.
1707  *
1708  * Certain properties can be overridden using 'mount -o'.  In this case, scan
1709  * the contents of the /etc/mnttab entry, searching for the appropriate options.
1710  * If they differ from the on-disk values, report the current values and mark
1711  * the source "temporary".
1712  */
1713 static int
1714 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1715     char **source, uint64_t *val)
1716 {
1717         zfs_cmd_t zc = { 0 };
1718         nvlist_t *zplprops = NULL;
1719         struct mnttab mnt;
1720         char *mntopt_on = NULL;
1721         char *mntopt_off = NULL;
1722         boolean_t received = zfs_is_recvd_props_mode(zhp);
1723 
1724         *source = NULL;
1725 
1726         switch (prop) {
1727         case ZFS_PROP_ATIME:
1728                 mntopt_on = MNTOPT_ATIME;
1729                 mntopt_off = MNTOPT_NOATIME;
1730                 break;
1731 
1732         case ZFS_PROP_DEVICES:
1733                 mntopt_on = MNTOPT_DEVICES;
1734                 mntopt_off = MNTOPT_NODEVICES;
1735                 break;
1736 
1737         case ZFS_PROP_EXEC:
1738                 mntopt_on = MNTOPT_EXEC;
1739                 mntopt_off = MNTOPT_NOEXEC;
1740                 break;
1741 
1742         case ZFS_PROP_READONLY:
1743                 mntopt_on = MNTOPT_RO;
1744                 mntopt_off = MNTOPT_RW;
1745                 break;
1746 
1747         case ZFS_PROP_SETUID:
1748                 mntopt_on = MNTOPT_SETUID;
1749                 mntopt_off = MNTOPT_NOSETUID;
1750                 break;
1751 
1752         case ZFS_PROP_XATTR:
1753                 mntopt_on = MNTOPT_XATTR;
1754                 mntopt_off = MNTOPT_NOXATTR;
1755                 break;
1756 
1757         case ZFS_PROP_NBMAND:
1758                 mntopt_on = MNTOPT_NBMAND;
1759                 mntopt_off = MNTOPT_NONBMAND;
1760                 break;
1761         }
1762 
1763         /*
1764          * Because looking up the mount options is potentially expensive
1765          * (iterating over all of /etc/mnttab), we defer its calculation until
1766          * we're looking up a property which requires its presence.
1767          */
1768         if (!zhp->zfs_mntcheck &&
1769             (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1770                 libzfs_handle_t *hdl = zhp->zfs_hdl;
1771                 struct mnttab entry;
1772 
1773                 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1774                         zhp->zfs_mntopts = zfs_strdup(hdl,
1775                             entry.mnt_mntopts);
1776                         if (zhp->zfs_mntopts == NULL)
1777                                 return (-1);
1778                 }
1779 
1780                 zhp->zfs_mntcheck = B_TRUE;
1781         }
1782 
1783         if (zhp->zfs_mntopts == NULL)
1784                 mnt.mnt_mntopts = "";
1785         else
1786                 mnt.mnt_mntopts = zhp->zfs_mntopts;
1787 
1788         switch (prop) {
1789         case ZFS_PROP_ATIME:
1790         case ZFS_PROP_DEVICES:
1791         case ZFS_PROP_EXEC:
1792         case ZFS_PROP_READONLY:
1793         case ZFS_PROP_SETUID:
1794         case ZFS_PROP_XATTR:
1795         case ZFS_PROP_NBMAND:
1796                 *val = getprop_uint64(zhp, prop, source);
1797 
1798                 if (received)
1799                         break;
1800 
1801                 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1802                         *val = B_TRUE;
1803                         if (src)
1804                                 *src = ZPROP_SRC_TEMPORARY;
1805                 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1806                         *val = B_FALSE;
1807                         if (src)
1808                                 *src = ZPROP_SRC_TEMPORARY;
1809                 }
1810                 break;
1811 
1812         case ZFS_PROP_CANMOUNT:
1813         case ZFS_PROP_VOLSIZE:
1814         case ZFS_PROP_QUOTA:
1815         case ZFS_PROP_REFQUOTA:
1816         case ZFS_PROP_RESERVATION:
1817         case ZFS_PROP_REFRESERVATION:
1818                 *val = getprop_uint64(zhp, prop, source);
1819 
1820                 if (*source == NULL) {
1821                         /* not default, must be local */
1822                         *source = zhp->zfs_name;
1823                 }
1824                 break;
1825 
1826         case ZFS_PROP_MOUNTED:
1827                 *val = (zhp->zfs_mntopts != NULL);
1828                 break;
1829 
1830         case ZFS_PROP_NUMCLONES:
1831                 *val = zhp->zfs_dmustats.dds_num_clones;
1832                 break;
1833 
1834         case ZFS_PROP_VERSION:
1835         case ZFS_PROP_NORMALIZE:
1836         case ZFS_PROP_UTF8ONLY:
1837         case ZFS_PROP_CASE:
1838                 if (zhp->zfs_hdl->libzfs_cachedprops) {
1839                         return (zfs_error(zhp->zfs_hdl, EZFS_PROPCACHED,
1840                             "property unavailable since not cached"));
1841                 }
1842 
1843                 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1844                     zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1845                         return (-1);
1846                 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1847                 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1848                         zcmd_free_nvlists(&zc);
1849                         return (-1);
1850                 }
1851                 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1852                     nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1853                     val) != 0) {
1854                         zcmd_free_nvlists(&zc);
1855                         return (-1);
1856                 }
1857                 if (zplprops)
1858                         nvlist_free(zplprops);
1859                 zcmd_free_nvlists(&zc);
1860                 break;
1861 
1862         default:
1863                 switch (zfs_prop_get_type(prop)) {
1864                 case PROP_TYPE_NUMBER:
1865                 case PROP_TYPE_INDEX:
1866                         *val = getprop_uint64(zhp, prop, source);
1867                         /*
1868                          * If we tried to use a default value for a
1869                          * readonly property, it means that it was not
1870                          * present.
1871                          */
1872                         if (zfs_prop_readonly(prop) &&
1873                             *source != NULL && (*source)[0] == '\0') {
1874                                 *source = NULL;
1875                         }
1876                         break;
1877 
1878                 case PROP_TYPE_STRING:
1879                 default:
1880                         zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1881                             "cannot get non-numeric property"));
1882                         return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1883                             dgettext(TEXT_DOMAIN, "internal error")));
1884                 }
1885         }
1886 
1887         return (0);
1888 }
1889 
1890 /*
1891  * Calculate the source type, given the raw source string.
1892  */
1893 static void
1894 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1895     char *statbuf, size_t statlen)
1896 {
1897         if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1898                 return;
1899 
1900         if (source == NULL) {
1901                 *srctype = ZPROP_SRC_NONE;
1902         } else if (source[0] == '\0') {
1903                 *srctype = ZPROP_SRC_DEFAULT;
1904         } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
1905                 *srctype = ZPROP_SRC_RECEIVED;
1906         } else {
1907                 if (strcmp(source, zhp->zfs_name) == 0) {
1908                         *srctype = ZPROP_SRC_LOCAL;
1909                 } else {
1910                         (void) strlcpy(statbuf, source, statlen);
1911                         *srctype = ZPROP_SRC_INHERITED;
1912                 }
1913         }
1914 
1915 }
1916 
1917 int
1918 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
1919     size_t proplen, boolean_t literal)
1920 {
1921         zfs_prop_t prop;
1922         int err = 0;
1923 
1924         if (zhp->zfs_recvd_props == NULL)
1925                 if (get_recvd_props_ioctl(zhp) != 0)
1926                         return (-1);
1927 
1928         prop = zfs_name_to_prop(propname);
1929 
1930         if (prop != ZPROP_INVAL) {
1931                 uint64_t cookie;
1932                 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
1933                         return (-1);
1934                 zfs_set_recvd_props_mode(zhp, &cookie);
1935                 err = zfs_prop_get(zhp, prop, propbuf, proplen,
1936                     NULL, NULL, 0, literal);
1937                 zfs_unset_recvd_props_mode(zhp, &cookie);
1938         } else {
1939                 nvlist_t *propval;
1940                 char *recvdval;
1941                 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
1942                     propname, &propval) != 0)
1943                         return (-1);
1944                 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
1945                     &recvdval) == 0);
1946                 (void) strlcpy(propbuf, recvdval, proplen);
1947         }
1948 
1949         return (err == 0 ? 0 : -1);
1950 }
1951 
1952 static int
1953 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
1954 {
1955         nvlist_t *value;
1956         nvpair_t *pair;
1957 
1958         value = zfs_get_clones_nvl(zhp);
1959         if (value == NULL)
1960                 return (-1);
1961 
1962         propbuf[0] = '\0';
1963         for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
1964             pair = nvlist_next_nvpair(value, pair)) {
1965                 if (propbuf[0] != '\0')
1966                         (void) strlcat(propbuf, ",", proplen);
1967                 (void) strlcat(propbuf, nvpair_name(pair), proplen);
1968         }
1969 
1970         return (0);
1971 }
1972 
1973 struct get_clones_arg {
1974         uint64_t numclones;
1975         nvlist_t *value;
1976         const char *origin;
1977         char buf[ZFS_MAXNAMELEN];
1978 };
1979 
1980 int
1981 get_clones_cb(zfs_handle_t *zhp, void *arg)
1982 {
1983         struct get_clones_arg *gca = arg;
1984 
1985         if (gca->numclones == 0) {
1986                 zfs_close(zhp);
1987                 return (0);
1988         }
1989 
1990         if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
1991             NULL, NULL, 0, B_TRUE) != 0)
1992                 goto out;
1993         if (strcmp(gca->buf, gca->origin) == 0) {
1994                 if (nvlist_add_boolean(gca->value, zfs_get_name(zhp)) != 0) {
1995                         zfs_close(zhp);
1996                         return (no_memory(zhp->zfs_hdl));
1997                 }
1998                 gca->numclones--;
1999         }
2000 
2001 out:
2002         (void) zfs_iter_children(zhp, get_clones_cb, gca);
2003         zfs_close(zhp);
2004         return (0);
2005 }
2006 
2007 nvlist_t *
2008 zfs_get_clones_nvl(zfs_handle_t *zhp)
2009 {
2010         nvlist_t *nv, *value;
2011 
2012         if (nvlist_lookup_nvlist(zhp->zfs_props,
2013             zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2014                 struct get_clones_arg gca;
2015 
2016                 /*
2017                  * if this is a snapshot, then the kernel wasn't able
2018                  * to get the clones.  Do it by slowly iterating.
2019                  */
2020                 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2021                         return (NULL);
2022                 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2023                         return (NULL);
2024                 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2025                         nvlist_free(nv);
2026                         return (NULL);
2027                 }
2028 
2029                 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2030                 gca.value = value;
2031                 gca.origin = zhp->zfs_name;
2032 
2033                 if (gca.numclones != 0) {
2034                         zfs_handle_t *root;
2035                         char pool[ZFS_MAXNAMELEN];
2036                         char *cp = pool;
2037 
2038                         /* get the pool name */
2039                         (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2040                         (void) strsep(&cp, "/@");
2041                         root = zfs_open(zhp->zfs_hdl, pool,
2042                             ZFS_TYPE_FILESYSTEM);
2043 
2044                         (void) get_clones_cb(root, &gca);
2045                 }
2046 
2047                 if (gca.numclones != 0 ||
2048                     nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2049                     nvlist_add_nvlist(zhp->zfs_props,
2050                     zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2051                         nvlist_free(nv);
2052                         nvlist_free(value);
2053                         return (NULL);
2054                 }
2055                 nvlist_free(nv);
2056                 nvlist_free(value);
2057                 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2058                     zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2059         }
2060 
2061         verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2062 
2063         return (value);
2064 }
2065 
2066 /*
2067  * Retrieve a property from the given object.  If 'literal' is specified, then
2068  * numbers are left as exact values.  Otherwise, numbers are converted to a
2069  * human-readable form.
2070  *
2071  * Returns 0 on success, or -1 on error.
2072  */
2073 int
2074 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2075     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2076 {
2077         char *source = NULL;
2078         uint64_t val;
2079         char *str;
2080         const char *strval;
2081         boolean_t received = zfs_is_recvd_props_mode(zhp);
2082         boolean_t printerr;
2083 
2084         /*
2085          * Check to see if this property applies to our object
2086          */
2087         if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2088                 return (-1);
2089 
2090         if (received && zfs_prop_readonly(prop))
2091                 return (-1);
2092 
2093         if (zhp->zfs_hdl->libzfs_cachedprops &&
2094             zfs_prop_cacheable(prop)) {
2095                 printerr = zhp->zfs_hdl->libzfs_printerr;
2096                 libzfs_print_on_error(zhp->zfs_hdl, B_FALSE);
2097                 (void) zfs_error(zhp->zfs_hdl, EZFS_PROPCACHED,
2098                     "property unavailable since not cached");
2099                 libzfs_print_on_error(zhp->zfs_hdl, printerr);
2100                 return (-1);
2101         }
2102 
2103         if (src)
2104                 *src = ZPROP_SRC_NONE;
2105 
2106         switch (prop) {
2107         case ZFS_PROP_CREATION:
2108                 /*
2109                  * 'creation' is a time_t stored in the statistics.  We convert
2110                  * this into a string unless 'literal' is specified.
2111                  */
2112                 {
2113                         val = getprop_uint64(zhp, prop, &source);
2114                         time_t time = (time_t)val;
2115                         struct tm t;
2116 
2117                         if (literal ||
2118                             localtime_r(&time, &t) == NULL ||
2119                             strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2120                             &t) == 0)
2121                                 (void) snprintf(propbuf, proplen, "%llu", val);
2122                 }
2123                 break;
2124 
2125         case ZFS_PROP_MOUNTPOINT:
2126                 /*
2127                  * Getting the precise mountpoint can be tricky.
2128                  *
2129                  *  - for 'none' or 'legacy', return those values.
2130                  *  - for inherited mountpoints, we want to take everything
2131                  *    after our ancestor and append it to the inherited value.
2132                  *
2133                  * If the pool has an alternate root, we want to prepend that
2134                  * root to any values we return.
2135                  */
2136 
2137                 str = getprop_string(zhp, prop, &source);
2138 
2139                 if (str[0] == '/') {
2140                         char buf[MAXPATHLEN];
2141                         char *root = buf;
2142                         const char *relpath;
2143 
2144                         /*
2145                          * If we inherit the mountpoint, even from a dataset
2146                          * with a received value, the source will be the path of
2147                          * the dataset we inherit from. If source is
2148                          * ZPROP_SOURCE_VAL_RECVD, the received value is not
2149                          * inherited.
2150                          */
2151                         if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2152                                 relpath = "";
2153                         } else {
2154                                 relpath = zhp->zfs_name + strlen(source);
2155                                 if (relpath[0] == '/')
2156                                         relpath++;
2157                         }
2158 
2159                         if ((zpool_get_prop(zhp->zpool_hdl,
2160                             ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2161                             B_FALSE)) || (strcmp(root, "-") == 0))
2162                                 root[0] = '\0';
2163                         /*
2164                          * Special case an alternate root of '/'. This will
2165                          * avoid having multiple leading slashes in the
2166                          * mountpoint path.
2167                          */
2168                         if (strcmp(root, "/") == 0)
2169                                 root++;
2170 
2171                         /*
2172                          * If the mountpoint is '/' then skip over this
2173                          * if we are obtaining either an alternate root or
2174                          * an inherited mountpoint.
2175                          */
2176                         if (str[1] == '\0' && (root[0] != '\0' ||
2177                             relpath[0] != '\0'))
2178                                 str++;
2179 
2180                         if (relpath[0] == '\0')
2181                                 (void) snprintf(propbuf, proplen, "%s%s",
2182                                     root, str);
2183                         else
2184                                 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2185                                     root, str, relpath[0] == '@' ? "" : "/",
2186                                     relpath);
2187                 } else {
2188                         /* 'legacy' or 'none' */
2189                         (void) strlcpy(propbuf, str, proplen);
2190                 }
2191 
2192                 break;
2193 
2194         case ZFS_PROP_ORIGIN:
2195                 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2196                     proplen);
2197                 /*
2198                  * If there is no parent at all, return failure to indicate that
2199                  * it doesn't apply to this dataset.
2200                  */
2201                 if (propbuf[0] == '\0')
2202                         return (-1);
2203                 break;
2204 
2205         case ZFS_PROP_CLONES:
2206                 if (get_clones_string(zhp, propbuf, proplen) != 0)
2207                         return (-1);
2208                 break;
2209 
2210         case ZFS_PROP_QUOTA:
2211         case ZFS_PROP_REFQUOTA:
2212         case ZFS_PROP_RESERVATION:
2213         case ZFS_PROP_REFRESERVATION:
2214 
2215                 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2216                         return (-1);
2217 
2218                 /*
2219                  * If quota or reservation is 0, we translate this into 'none'
2220                  * (unless literal is set), and indicate that it's the default
2221                  * value.  Otherwise, we print the number nicely and indicate
2222                  * that its set locally.
2223                  */
2224                 if (val == 0) {
2225                         if (literal)
2226                                 (void) strlcpy(propbuf, "0", proplen);
2227                         else
2228                                 (void) strlcpy(propbuf, "none", proplen);
2229                 } else {
2230                         if (literal)
2231                                 (void) snprintf(propbuf, proplen, "%llu",
2232                                     (u_longlong_t)val);
2233                         else
2234                                 zfs_nicenum(val, propbuf, proplen);
2235                 }
2236                 break;
2237 
2238         case ZFS_PROP_FILESYSTEM_LIMIT:
2239         case ZFS_PROP_SNAPSHOT_LIMIT:
2240 
2241                 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2242                         return (-1);
2243 
2244                 /*
2245                  * If limit is MAXLIMIT, we translate this into 'none' (unless
2246                  * literal is set), and indicate that it's the default value.
2247                  * Otherwise, we print the number nicely and indicate that its
2248                  * set locally.
2249                  */
2250                 if (literal) {
2251                         (void) snprintf(propbuf, proplen, "%llu",
2252                             (u_longlong_t)val);
2253                 } else if (val == MAXLIMIT) {
2254                         (void) strlcpy(propbuf, "none", proplen);
2255                 } else {
2256                         zfs_nicenum(val, propbuf, proplen);
2257                 }
2258                 break;
2259 
2260         case ZFS_PROP_REFRATIO:
2261         case ZFS_PROP_COMPRESSRATIO:
2262                 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2263                         return (-1);
2264                 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2265                     (u_longlong_t)(val / 100),
2266                     (u_longlong_t)(val % 100));
2267                 break;
2268 
2269         case ZFS_PROP_TYPE:
2270                 switch (zhp->zfs_type) {
2271                 case ZFS_TYPE_FILESYSTEM:
2272                         str = "filesystem";
2273                         break;
2274                 case ZFS_TYPE_VOLUME:
2275                         str = "volume";
2276                         break;
2277                 case ZFS_TYPE_SNAPSHOT:
2278                         str = "snapshot";
2279                         break;
2280                 default:
2281                         abort();
2282                 }
2283                 (void) snprintf(propbuf, proplen, "%s", str);
2284                 break;
2285 
2286         case ZFS_PROP_MOUNTED:
2287                 /*
2288                  * The 'mounted' property is a pseudo-property that described
2289                  * whether the filesystem is currently mounted.  Even though
2290                  * it's a boolean value, the typical values of "on" and "off"
2291                  * don't make sense, so we translate to "yes" and "no".
2292                  */
2293                 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2294                     src, &source, &val) != 0)
2295                         return (-1);
2296                 if (val)
2297                         (void) strlcpy(propbuf, "yes", proplen);
2298                 else
2299                         (void) strlcpy(propbuf, "no", proplen);
2300                 break;
2301 
2302         case ZFS_PROP_NAME:
2303                 /*
2304                  * The 'name' property is a pseudo-property derived from the
2305                  * dataset name.  It is presented as a real property to simplify
2306                  * consumers.
2307                  */
2308                 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2309                 break;
2310 
2311         case ZFS_PROP_MLSLABEL:
2312                 {
2313                         m_label_t *new_sl = NULL;
2314                         char *ascii = NULL;     /* human readable label */
2315 
2316                         (void) strlcpy(propbuf,
2317                             getprop_string(zhp, prop, &source), proplen);
2318 
2319                         if (literal || (strcasecmp(propbuf,
2320                             ZFS_MLSLABEL_DEFAULT) == 0))
2321                                 break;
2322 
2323                         /*
2324                          * Try to translate the internal hex string to
2325                          * human-readable output.  If there are any
2326                          * problems just use the hex string.
2327                          */
2328 
2329                         if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2330                             L_NO_CORRECTION, NULL) == -1) {
2331                                 m_label_free(new_sl);
2332                                 break;
2333                         }
2334 
2335                         if (label_to_str(new_sl, &ascii, M_LABEL,
2336                             DEF_NAMES) != 0) {
2337                                 if (ascii)
2338                                         free(ascii);
2339                                 m_label_free(new_sl);
2340                                 break;
2341                         }
2342                         m_label_free(new_sl);
2343 
2344                         (void) strlcpy(propbuf, ascii, proplen);
2345                         free(ascii);
2346                 }
2347                 break;
2348 
2349         case ZFS_PROP_GUID:
2350                 /*
2351                  * GUIDs are stored as numbers, but they are identifiers.
2352                  * We don't want them to be pretty printed, because pretty
2353                  * printing mangles the ID into a truncated and useless value.
2354                  */
2355                 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2356                         return (-1);
2357                 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2358                 break;
2359 
2360         default:
2361                 switch (zfs_prop_get_type(prop)) {
2362                 case PROP_TYPE_NUMBER:
2363                         if (get_numeric_property(zhp, prop, src,
2364                             &source, &val) != 0)
2365                                 return (-1);
2366                         if (literal)
2367                                 (void) snprintf(propbuf, proplen, "%llu",
2368                                     (u_longlong_t)val);
2369                         else
2370                                 zfs_nicenum(val, propbuf, proplen);
2371                         break;
2372 
2373                 case PROP_TYPE_STRING:
2374                         (void) strlcpy(propbuf,
2375                             getprop_string(zhp, prop, &source), proplen);
2376                         break;
2377 
2378                 case PROP_TYPE_INDEX:
2379                         if (get_numeric_property(zhp, prop, src,
2380                             &source, &val) != 0)
2381                                 return (-1);
2382                         if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2383                                 return (-1);
2384                         (void) strlcpy(propbuf, strval, proplen);
2385                         break;
2386 
2387                 default:
2388                         abort();
2389                 }
2390         }
2391 
2392         get_source(zhp, src, source, statbuf, statlen);
2393 
2394         return (0);
2395 }
2396 
2397 /*
2398  * Utility function to get the given numeric property.  Does no validation that
2399  * the given property is the appropriate type; should only be used with
2400  * hard-coded property types.
2401  */
2402 uint64_t
2403 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2404 {
2405         char *source;
2406         uint64_t val;
2407 
2408         (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2409 
2410         return (val);
2411 }
2412 
2413 int
2414 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2415 {
2416         char buf[64];
2417 
2418         (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2419         return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2420 }
2421 
2422 /*
2423  * Similar to zfs_prop_get(), but returns the value as an integer.
2424  */
2425 int
2426 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2427     zprop_source_t *src, char *statbuf, size_t statlen)
2428 {
2429         char *source;
2430 
2431         /*
2432          * Check to see if this property applies to our object
2433          */
2434         if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2435                 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2436                     dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2437                     zfs_prop_to_name(prop)));
2438         }
2439 
2440         if (src)
2441                 *src = ZPROP_SRC_NONE;
2442 
2443         if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2444                 return (-1);
2445 
2446         get_source(zhp, src, source, statbuf, statlen);
2447 
2448         return (0);
2449 }
2450 
2451 static int
2452 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2453     char **domainp, idmap_rid_t *ridp)
2454 {
2455         idmap_get_handle_t *get_hdl = NULL;
2456         idmap_stat status;
2457         int err = EINVAL;
2458 
2459         if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2460                 goto out;
2461 
2462         if (isuser) {
2463                 err = idmap_get_sidbyuid(get_hdl, id,
2464                     IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2465         } else {
2466                 err = idmap_get_sidbygid(get_hdl, id,
2467                     IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2468         }
2469         if (err == IDMAP_SUCCESS &&
2470             idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2471             status == IDMAP_SUCCESS)
2472                 err = 0;
2473         else
2474                 err = EINVAL;
2475 out:
2476         if (get_hdl)
2477                 idmap_get_destroy(get_hdl);
2478         return (err);
2479 }
2480 
2481 /*
2482  * convert the propname into parameters needed by kernel
2483  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2484  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2485  */
2486 static int
2487 userquota_propname_decode(const char *propname, boolean_t zoned,
2488     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2489 {
2490         zfs_userquota_prop_t type;
2491         char *cp, *end;
2492         char *numericsid = NULL;
2493         boolean_t isuser;
2494 
2495         domain[0] = '\0';
2496 
2497         /* Figure out the property type ({user|group}{quota|space}) */
2498         for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2499                 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2500                     strlen(zfs_userquota_prop_prefixes[type])) == 0)
2501                         break;
2502         }
2503         if (type == ZFS_NUM_USERQUOTA_PROPS)
2504                 return (EINVAL);
2505         *typep = type;
2506 
2507         isuser = (type == ZFS_PROP_USERQUOTA ||
2508             type == ZFS_PROP_USERUSED);
2509 
2510         cp = strchr(propname, '@') + 1;
2511 
2512         if (strchr(cp, '@')) {
2513                 /*
2514                  * It's a SID name (eg "user@domain") that needs to be
2515                  * turned into S-1-domainID-RID.
2516                  */
2517                 directory_error_t e;
2518                 if (zoned && getzoneid() == GLOBAL_ZONEID)
2519                         return (ENOENT);
2520                 if (isuser) {
2521                         e = directory_sid_from_user_name(NULL,
2522                             cp, &numericsid);
2523                 } else {
2524                         e = directory_sid_from_group_name(NULL,
2525                             cp, &numericsid);
2526                 }
2527                 if (e != NULL) {
2528                         directory_error_free(e);
2529                         return (ENOENT);
2530                 }
2531                 if (numericsid == NULL)
2532                         return (ENOENT);
2533                 cp = numericsid;
2534                 /* will be further decoded below */
2535         }
2536 
2537         if (strncmp(cp, "S-1-", 4) == 0) {
2538                 /* It's a numeric SID (eg "S-1-234-567-89") */
2539                 (void) strlcpy(domain, cp, domainlen);
2540                 cp = strrchr(domain, '-');
2541                 *cp = '\0';
2542                 cp++;
2543 
2544                 errno = 0;
2545                 *ridp = strtoull(cp, &end, 10);
2546                 if (numericsid) {
2547                         free(numericsid);
2548                         numericsid = NULL;
2549                 }
2550                 if (errno != 0 || *end != '\0')
2551                         return (EINVAL);
2552         } else if (!isdigit(*cp)) {
2553                 /*
2554                  * It's a user/group name (eg "user") that needs to be
2555                  * turned into a uid/gid
2556                  */
2557                 if (zoned && getzoneid() == GLOBAL_ZONEID)
2558                         return (ENOENT);
2559                 if (isuser) {
2560                         struct passwd *pw;
2561                         pw = getpwnam(cp);
2562                         if (pw == NULL)
2563                                 return (ENOENT);
2564                         *ridp = pw->pw_uid;
2565                 } else {
2566                         struct group *gr;
2567                         gr = getgrnam(cp);
2568                         if (gr == NULL)
2569                                 return (ENOENT);
2570                         *ridp = gr->gr_gid;
2571                 }
2572         } else {
2573                 /* It's a user/group ID (eg "12345"). */
2574                 uid_t id = strtoul(cp, &end, 10);
2575                 idmap_rid_t rid;
2576                 char *mapdomain;
2577 
2578                 if (*end != '\0')
2579                         return (EINVAL);
2580                 if (id > MAXUID) {
2581                         /* It's an ephemeral ID. */
2582                         if (idmap_id_to_numeric_domain_rid(id, isuser,
2583                             &mapdomain, &rid) != 0)
2584                                 return (ENOENT);
2585                         (void) strlcpy(domain, mapdomain, domainlen);
2586                         *ridp = rid;
2587                 } else {
2588                         *ridp = id;
2589                 }
2590         }
2591 
2592         ASSERT3P(numericsid, ==, NULL);
2593         return (0);
2594 }
2595 
2596 static int
2597 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2598     uint64_t *propvalue, zfs_userquota_prop_t *typep)
2599 {
2600         int err;
2601         zfs_cmd_t zc = { 0 };
2602 
2603         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2604 
2605         err = userquota_propname_decode(propname,
2606             zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2607             typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2608         zc.zc_objset_type = *typep;
2609         if (err)
2610                 return (err);
2611 
2612         err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2613         if (err)
2614                 return (err);
2615 
2616         *propvalue = zc.zc_cookie;
2617         return (0);
2618 }
2619 
2620 int
2621 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2622     uint64_t *propvalue)
2623 {
2624         zfs_userquota_prop_t type;
2625 
2626         return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2627             &type));
2628 }
2629 
2630 int
2631 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2632     char *propbuf, int proplen, boolean_t literal)
2633 {
2634         int err;
2635         uint64_t propvalue;
2636         zfs_userquota_prop_t type;
2637 
2638         err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2639             &type);
2640 
2641         if (err)
2642                 return (err);
2643 
2644         if (literal) {
2645                 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2646         } else if (propvalue == 0 &&
2647             (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2648                 (void) strlcpy(propbuf, "none", proplen);
2649         } else {
2650                 zfs_nicenum(propvalue, propbuf, proplen);
2651         }
2652         return (0);
2653 }
2654 
2655 int
2656 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2657     uint64_t *propvalue)
2658 {
2659         int err;
2660         zfs_cmd_t zc = { 0 };
2661         const char *snapname;
2662 
2663         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2664 
2665         snapname = strchr(propname, '@') + 1;
2666         if (strchr(snapname, '@')) {
2667                 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2668         } else {
2669                 /* snapname is the short name, append it to zhp's fsname */
2670                 char *cp;
2671 
2672                 (void) strlcpy(zc.zc_value, zhp->zfs_name,
2673                     sizeof (zc.zc_value));
2674                 cp = strchr(zc.zc_value, '@');
2675                 if (cp != NULL)
2676                         *cp = '\0';
2677                 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
2678                 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
2679         }
2680 
2681         err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
2682         if (err)
2683                 return (err);
2684 
2685         *propvalue = zc.zc_cookie;
2686         return (0);
2687 }
2688 
2689 int
2690 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
2691     char *propbuf, int proplen, boolean_t literal)
2692 {
2693         int err;
2694         uint64_t propvalue;
2695 
2696         err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2697 
2698         if (err)
2699                 return (err);
2700 
2701         if (literal) {
2702                 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2703         } else {
2704                 zfs_nicenum(propvalue, propbuf, proplen);
2705         }
2706         return (0);
2707 }
2708 
2709 /*
2710  * Returns the name of the given zfs handle.
2711  */
2712 const char *
2713 zfs_get_name(const zfs_handle_t *zhp)
2714 {
2715         return (zhp->zfs_name);
2716 }
2717 
2718 /*
2719  * Returns the type of the given zfs handle.
2720  */
2721 zfs_type_t
2722 zfs_get_type(const zfs_handle_t *zhp)
2723 {
2724         return (zhp->zfs_type);
2725 }
2726 
2727 /*
2728  * Is one dataset name a child dataset of another?
2729  *
2730  * Needs to handle these cases:
2731  * Dataset 1    "a/foo"         "a/foo"         "a/foo"         "a/foo"
2732  * Dataset 2    "a/fo"          "a/foobar"      "a/bar/baz"     "a/foo/bar"
2733  * Descendant?  No.             No.             No.             Yes.
2734  */
2735 static boolean_t
2736 is_descendant(const char *ds1, const char *ds2)
2737 {
2738         size_t d1len = strlen(ds1);
2739 
2740         /* ds2 can't be a descendant if it's smaller */
2741         if (strlen(ds2) < d1len)
2742                 return (B_FALSE);
2743 
2744         /* otherwise, compare strings and verify that there's a '/' char */
2745         return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2746 }
2747 
2748 /*
2749  * Given a complete name, return just the portion that refers to the parent.
2750  * Will return -1 if there is no parent (path is just the name of the
2751  * pool).
2752  */
2753 static int
2754 parent_name(const char *path, char *buf, size_t buflen)
2755 {
2756         char *slashp;
2757 
2758         (void) strlcpy(buf, path, buflen);
2759 
2760         if ((slashp = strrchr(buf, '/')) == NULL)
2761                 return (-1);
2762         *slashp = '\0';
2763 
2764         return (0);
2765 }
2766 
2767 /*
2768  * If accept_ancestor is false, then check to make sure that the given path has
2769  * a parent, and that it exists.  If accept_ancestor is true, then find the
2770  * closest existing ancestor for the given path.  In prefixlen return the
2771  * length of already existing prefix of the given path.  We also fetch the
2772  * 'zoned' property, which is used to validate property settings when creating
2773  * new datasets.
2774  */
2775 static int
2776 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2777     boolean_t accept_ancestor, int *prefixlen)
2778 {
2779         zfs_cmd_t zc = { 0 };
2780         char parent[ZFS_MAXNAMELEN];
2781         char *slash;
2782         zfs_handle_t *zhp;
2783         char errbuf[1024];
2784         uint64_t is_zoned;
2785 
2786         (void) snprintf(errbuf, sizeof (errbuf),
2787             dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2788 
2789         /* get parent, and check to see if this is just a pool */
2790         if (parent_name(path, parent, sizeof (parent)) != 0) {
2791                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2792                     "missing dataset name"));
2793                 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2794         }
2795 
2796         /* check to see if the pool exists */
2797         if ((slash = strchr(parent, '/')) == NULL)
2798                 slash = parent + strlen(parent);
2799         (void) strncpy(zc.zc_name, parent, slash - parent);
2800         zc.zc_name[slash - parent] = '\0';
2801         if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2802             errno == ENOENT) {
2803                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2804                     "no such pool '%s'"), zc.zc_name);
2805                 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2806         }
2807 
2808         /* check to see if the parent dataset exists */
2809         while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2810                 if (errno == ENOENT && accept_ancestor) {
2811                         /*
2812                          * Go deeper to find an ancestor, give up on top level.
2813                          */
2814                         if (parent_name(parent, parent, sizeof (parent)) != 0) {
2815                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2816                                     "no such pool '%s'"), zc.zc_name);
2817                                 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2818                         }
2819                 } else if (errno == ENOENT) {
2820                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2821                             "parent does not exist"));
2822                         return (zfs_error(hdl, EZFS_NOENT, errbuf));
2823                 } else
2824                         return (zfs_standard_error(hdl, errno, errbuf));
2825         }
2826 
2827         is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2828         if (zoned != NULL)
2829                 *zoned = is_zoned;
2830 
2831         /* we are in a non-global zone, but parent is in the global zone */
2832         if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2833                 (void) zfs_standard_error(hdl, EPERM, errbuf);
2834                 zfs_close(zhp);
2835                 return (-1);
2836         }
2837 
2838         /* make sure parent is a filesystem */
2839         if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2840                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2841                     "parent is not a filesystem"));
2842                 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2843                 zfs_close(zhp);
2844                 return (-1);
2845         }
2846 
2847         zfs_close(zhp);
2848         if (prefixlen != NULL)
2849                 *prefixlen = strlen(parent);
2850         return (0);
2851 }
2852 
2853 /*
2854  * Finds whether the dataset of the given type(s) exists.
2855  */
2856 boolean_t
2857 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2858 {
2859         zfs_handle_t *zhp;
2860 
2861         if (!zfs_validate_name(hdl, path, types, B_FALSE))
2862                 return (B_FALSE);
2863 
2864         /*
2865          * Try to get stats for the dataset, which will tell us if it exists.
2866          */
2867         if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2868                 int ds_type = zhp->zfs_type;
2869 
2870                 zfs_close(zhp);
2871                 if (types & ds_type)
2872                         return (B_TRUE);
2873         }
2874         return (B_FALSE);
2875 }
2876 
2877 /*
2878  * Given a path to 'target', create all the ancestors between
2879  * the prefixlen portion of the path, and the target itself.
2880  * Fail if the initial prefixlen-ancestor does not already exist.
2881  */
2882 int
2883 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2884 {
2885         zfs_handle_t *h;
2886         char *cp;
2887         const char *opname;
2888 
2889         /* make sure prefix exists */
2890         cp = target + prefixlen;
2891         if (*cp != '/') {
2892                 assert(strchr(cp, '/') == NULL);
2893                 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2894         } else {
2895                 *cp = '\0';
2896                 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2897                 *cp = '/';
2898         }
2899         if (h == NULL)
2900                 return (-1);
2901         zfs_close(h);
2902 
2903         /*
2904          * Attempt to create, mount, and share any ancestor filesystems,
2905          * up to the prefixlen-long one.
2906          */
2907         for (cp = target + prefixlen + 1;
2908             cp = strchr(cp, '/'); *cp = '/', cp++) {
2909 
2910                 *cp = '\0';
2911 
2912                 h = make_dataset_handle(hdl, target);
2913                 if (h) {
2914                         /* it already exists, nothing to do here */
2915                         zfs_close(h);
2916                         continue;
2917                 }
2918 
2919                 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2920                     NULL) != 0) {
2921                         opname = dgettext(TEXT_DOMAIN, "create");
2922                         goto ancestorerr;
2923                 }
2924 
2925                 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2926                 if (h == NULL) {
2927                         opname = dgettext(TEXT_DOMAIN, "open");
2928                         goto ancestorerr;
2929                 }
2930 
2931                 if (zfs_mount(h, NULL, 0) != 0) {
2932                         opname = dgettext(TEXT_DOMAIN, "mount");
2933                         goto ancestorerr;
2934                 }
2935 
2936                 if (zfs_share(h) != 0) {
2937                         opname = dgettext(TEXT_DOMAIN, "share");
2938                         goto ancestorerr;
2939                 }
2940 
2941                 zfs_close(h);
2942         }
2943 
2944         return (0);
2945 
2946 ancestorerr:
2947         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2948             "failed to %s ancestor '%s'"), opname, target);
2949         return (-1);
2950 }
2951 
2952 /*
2953  * Creates non-existing ancestors of the given path.
2954  */
2955 int
2956 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2957 {
2958         int prefix;
2959         char *path_copy;
2960         int rc;
2961 
2962         if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
2963                 return (-1);
2964 
2965         if ((path_copy = strdup(path)) != NULL) {
2966                 rc = create_parents(hdl, path_copy, prefix);
2967                 free(path_copy);
2968         }
2969         if (path_copy == NULL || rc != 0)
2970                 return (-1);
2971 
2972         return (0);
2973 }
2974 
2975 /*
2976  * Create a new filesystem or volume.
2977  */
2978 int
2979 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2980     nvlist_t *props)
2981 {
2982         int ret;
2983         uint64_t size = 0;
2984         uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2985         char errbuf[1024];
2986         uint64_t zoned;
2987         dmu_objset_type_t ost;
2988 
2989         (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2990             "cannot create '%s'"), path);
2991 
2992         /* validate the path, taking care to note the extended error message */
2993         if (!zfs_validate_name(hdl, path, type, B_TRUE))
2994                 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2995 
2996         /* validate parents exist */
2997         if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2998                 return (-1);
2999 
3000         /*
3001          * The failure modes when creating a dataset of a different type over
3002          * one that already exists is a little strange.  In particular, if you
3003          * try to create a dataset on top of an existing dataset, the ioctl()
3004          * will return ENOENT, not EEXIST.  To prevent this from happening, we
3005          * first try to see if the dataset exists.
3006          */
3007         if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3008                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3009                     "dataset already exists"));
3010                 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3011         }
3012 
3013         if (type == ZFS_TYPE_VOLUME)
3014                 ost = DMU_OST_ZVOL;
3015         else
3016                 ost = DMU_OST_ZFS;
3017 
3018         if (props && (props = zfs_valid_proplist(hdl, type, props,
3019             zoned, NULL, errbuf)) == 0)
3020                 return (-1);
3021 
3022         if (type == ZFS_TYPE_VOLUME) {
3023                 /*
3024                  * If we are creating a volume, the size and block size must
3025                  * satisfy a few restraints.  First, the blocksize must be a
3026                  * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
3027                  * volsize must be a multiple of the block size, and cannot be
3028                  * zero.
3029                  */
3030                 if (props == NULL || nvlist_lookup_uint64(props,
3031                     zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3032                         nvlist_free(props);
3033                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3034                             "missing volume size"));
3035                         return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3036                 }
3037 
3038                 if ((ret = nvlist_lookup_uint64(props,
3039                     zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3040                     &blocksize)) != 0) {
3041                         if (ret == ENOENT) {
3042                                 blocksize = zfs_prop_default_numeric(
3043                                     ZFS_PROP_VOLBLOCKSIZE);
3044                         } else {
3045                                 nvlist_free(props);
3046                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3047                                     "missing volume block size"));
3048                                 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3049                         }
3050                 }
3051 
3052                 if (size == 0) {
3053                         nvlist_free(props);
3054                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3055                             "volume size cannot be zero"));
3056                         return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3057                 }
3058 
3059                 if (size % blocksize != 0) {
3060                         nvlist_free(props);
3061                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3062                             "volume size must be a multiple of volume block "
3063                             "size"));
3064                         return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3065                 }
3066         }
3067 
3068         /* create the dataset */
3069         ret = lzc_create(path, ost, props);
3070         nvlist_free(props);
3071 
3072         /* check for failure */
3073         if (ret != 0) {
3074                 char parent[ZFS_MAXNAMELEN];
3075                 (void) parent_name(path, parent, sizeof (parent));
3076 
3077                 switch (errno) {
3078                 case ENOENT:
3079                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3080                             "no such parent '%s'"), parent);
3081                         return (zfs_error(hdl, EZFS_NOENT, errbuf));
3082 
3083                 case EINVAL:
3084                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3085                             "parent '%s' is not a filesystem"), parent);
3086                         return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3087 
3088                 case EDOM:
3089                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3090                             "volume block size must be power of 2 from "
3091                             "%u to %uk"),
3092                             (uint_t)SPA_MINBLOCKSIZE,
3093                             (uint_t)SPA_MAXBLOCKSIZE >> 10);
3094 
3095                         return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3096 
3097                 case ENOTSUP:
3098                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3099                             "pool must be upgraded to set this "
3100                             "property or value"));
3101                         return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3102 #ifdef _ILP32
3103                 case EOVERFLOW:
3104                         /*
3105                          * This platform can't address a volume this big.
3106                          */
3107                         if (type == ZFS_TYPE_VOLUME)
3108                                 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3109                                     errbuf));
3110 #endif
3111                         /* FALLTHROUGH */
3112                 default:
3113                         return (zfs_standard_error(hdl, errno, errbuf));
3114                 }
3115         }
3116 
3117         return (0);
3118 }
3119 
3120 /*
3121  * Destroys the given dataset.  The caller must make sure that the filesystem
3122  * isn't mounted, and that there are no active dependents. If the file system
3123  * does not exist this function does nothing.
3124  */
3125 int
3126 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3127 {
3128         zfs_cmd_t zc = { 0 };
3129 
3130         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3131 
3132         if (ZFS_IS_VOLUME(zhp)) {
3133                 zc.zc_objset_type = DMU_OST_ZVOL;
3134         } else {
3135                 zc.zc_objset_type = DMU_OST_ZFS;
3136         }
3137 
3138         zc.zc_defer_destroy = defer;
3139         if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3140             errno != ENOENT) {
3141                 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3142                     dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3143                     zhp->zfs_name));
3144         }
3145 
3146         remove_mountpoint(zhp);
3147 
3148         return (0);
3149 }
3150 
3151 struct destroydata {
3152         nvlist_t *nvl;
3153         const char *snapname;
3154 };
3155 
3156 static int
3157 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3158 {
3159         struct destroydata *dd = arg;
3160         zfs_handle_t *szhp;
3161         char name[ZFS_MAXNAMELEN];
3162         int rv = 0;
3163 
3164         (void) snprintf(name, sizeof (name),
3165             "%s@%s", zhp->zfs_name, dd->snapname);
3166 
3167         szhp = make_dataset_handle(zhp->zfs_hdl, name);
3168         if (szhp) {
3169                 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3170                 zfs_close(szhp);
3171         }
3172 
3173         rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3174         zfs_close(zhp);
3175         return (rv);
3176 }
3177 
3178 /*
3179  * Destroys all snapshots with the given name in zhp & descendants.
3180  */
3181 int
3182 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3183 {
3184         int ret;
3185         struct destroydata dd = { 0 };
3186 
3187         dd.snapname = snapname;
3188         verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3189         (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3190 
3191         if (nvlist_next_nvpair(dd.nvl, NULL) == NULL) {
3192                 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3193                     dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3194                     zhp->zfs_name, snapname);
3195         } else {
3196                 ret = zfs_destroy_snaps_nvl(zhp, dd.nvl, defer);
3197         }
3198         nvlist_free(dd.nvl);
3199         return (ret);
3200 }
3201 
3202 /*
3203  * Destroys all the snapshots named in the nvlist.  They must be underneath
3204  * the zhp (either snapshots of it, or snapshots of its descendants).
3205  */
3206 int
3207 zfs_destroy_snaps_nvl(zfs_handle_t *zhp, nvlist_t *snaps, boolean_t defer)
3208 {
3209         int ret;
3210         nvlist_t *errlist;
3211 
3212         ret = lzc_destroy_snaps(snaps, defer, &errlist);
3213 
3214         if (ret != 0) {
3215                 for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3216                     pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3217                         char errbuf[1024];
3218                         (void) snprintf(errbuf, sizeof (errbuf),
3219                             dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3220                             nvpair_name(pair));
3221 
3222                         switch (fnvpair_value_int32(pair)) {
3223                         case EEXIST:
3224                                 zfs_error_aux(zhp->zfs_hdl,
3225                                     dgettext(TEXT_DOMAIN,
3226                                     "snapshot is cloned"));
3227                                 ret = zfs_error(zhp->zfs_hdl, EZFS_EXISTS,
3228                                     errbuf);
3229                                 break;
3230                         default:
3231                                 ret = zfs_standard_error(zhp->zfs_hdl, errno,
3232                                     errbuf);
3233                                 break;
3234                         }
3235                 }
3236         }
3237 
3238         return (ret);
3239 }
3240 
3241 /*
3242  * Clones the given dataset.  The target must be of the same type as the source.
3243  */
3244 int
3245 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3246 {
3247         char parent[ZFS_MAXNAMELEN];
3248         int ret;
3249         char errbuf[1024];
3250         libzfs_handle_t *hdl = zhp->zfs_hdl;
3251         uint64_t zoned;
3252 
3253         assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3254 
3255         (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3256             "cannot create '%s'"), target);
3257 
3258         /* validate the target/clone name */
3259         if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3260                 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3261 
3262         /* validate parents exist */
3263         if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3264                 return (-1);
3265 
3266         (void) parent_name(target, parent, sizeof (parent));
3267 
3268         /* do the clone */
3269 
3270         if (props) {
3271                 zfs_type_t type;
3272                 if (ZFS_IS_VOLUME(zhp)) {
3273                         type = ZFS_TYPE_VOLUME;
3274                 } else {
3275                         type = ZFS_TYPE_FILESYSTEM;
3276                 }
3277                 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3278                     zhp, errbuf)) == NULL)
3279                         return (-1);
3280         }
3281 
3282         ret = lzc_clone(target, zhp->zfs_name, props);
3283         nvlist_free(props);
3284 
3285         if (ret != 0) {
3286                 switch (errno) {
3287 
3288                 case ENOENT:
3289                         /*
3290                          * The parent doesn't exist.  We should have caught this
3291                          * above, but there may a race condition that has since
3292                          * destroyed the parent.
3293                          *
3294                          * At this point, we don't know whether it's the source
3295                          * that doesn't exist anymore, or whether the target
3296                          * dataset doesn't exist.
3297                          */
3298                         zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3299                             "no such parent '%s'"), parent);
3300                         return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3301 
3302                 case EXDEV:
3303                         zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3304                             "source and target pools differ"));
3305                         return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3306                             errbuf));
3307 
3308                 default:
3309                         return (zfs_standard_error(zhp->zfs_hdl, errno,
3310                             errbuf));
3311                 }
3312         }
3313 
3314         return (ret);
3315 }
3316 
3317 /*
3318  * Promotes the given clone fs to be the clone parent.
3319  */
3320 int
3321 zfs_promote(zfs_handle_t *zhp)
3322 {
3323         libzfs_handle_t *hdl = zhp->zfs_hdl;
3324         zfs_cmd_t zc = { 0 };
3325         char parent[MAXPATHLEN];
3326         int ret;
3327         char errbuf[1024];
3328 
3329         (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3330             "cannot promote '%s'"), zhp->zfs_name);
3331 
3332         if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3333                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3334                     "snapshots can not be promoted"));
3335                 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3336         }
3337 
3338         (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3339         if (parent[0] == '\0') {
3340                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3341                     "not a cloned filesystem"));
3342                 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3343         }
3344 
3345         (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3346             sizeof (zc.zc_value));
3347         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3348         ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3349 
3350         if (ret != 0) {
3351                 int save_errno = errno;
3352 
3353                 switch (save_errno) {
3354                 case EEXIST:
3355                         /* There is a conflicting snapshot name. */
3356                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3357                             "conflicting snapshot '%s' from parent '%s'"),
3358                             zc.zc_string, parent);
3359                         return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3360 
3361                 default:
3362                         return (zfs_standard_error(hdl, save_errno, errbuf));
3363                 }
3364         }
3365         return (ret);
3366 }
3367 
3368 typedef struct snapdata {
3369         nvlist_t *sd_nvl;
3370         const char *sd_snapname;
3371 } snapdata_t;
3372 
3373 static int
3374 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3375 {
3376         snapdata_t *sd = arg;
3377         char name[ZFS_MAXNAMELEN];
3378         int rv = 0;
3379 
3380         (void) snprintf(name, sizeof (name),
3381             "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3382 
3383         fnvlist_add_boolean(sd->sd_nvl, name);
3384 
3385         rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3386         zfs_close(zhp);
3387         return (rv);
3388 }
3389 
3390 /*
3391  * Creates snapshots.  The keys in the snaps nvlist are the snapshots to be
3392  * created.
3393  */
3394 int
3395 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3396 {
3397         int ret;
3398         char errbuf[1024];
3399         nvpair_t *elem;
3400         nvlist_t *errors;
3401 
3402         (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3403             "cannot create snapshots "));
3404 
3405         elem = NULL;
3406         while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3407                 const char *snapname = nvpair_name(elem);
3408 
3409                 /* validate the target name */
3410                 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3411                     B_TRUE)) {
3412                         (void) snprintf(errbuf, sizeof (errbuf),
3413                             dgettext(TEXT_DOMAIN,
3414                             "cannot create snapshot '%s'"), snapname);
3415                         return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3416                 }
3417         }
3418 
3419         if (props != NULL &&
3420             (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3421             props, B_FALSE, NULL, errbuf)) == NULL) {
3422                 return (-1);
3423         }
3424 
3425         ret = lzc_snapshot(snaps, props, &errors);
3426 
3427         if (ret != 0) {
3428                 boolean_t printed = B_FALSE;
3429                 for (elem = nvlist_next_nvpair(errors, NULL);
3430                     elem != NULL;
3431                     elem = nvlist_next_nvpair(errors, elem)) {
3432                         (void) snprintf(errbuf, sizeof (errbuf),
3433                             dgettext(TEXT_DOMAIN,
3434                             "cannot create snapshot '%s'"), nvpair_name(elem));
3435                         (void) zfs_standard_error(hdl,
3436                             fnvpair_value_int32(elem), errbuf);
3437                         printed = B_TRUE;
3438                 }
3439                 if (!printed) {
3440                         switch (ret) {
3441                         case EXDEV:
3442                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3443                                     "multiple snapshots of same "
3444                                     "fs not allowed"));
3445                                 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3446 
3447                                 break;
3448                         default:
3449                                 (void) zfs_standard_error(hdl, ret, errbuf);
3450                         }
3451                 }
3452         }
3453 
3454         nvlist_free(props);
3455         nvlist_free(errors);
3456         return (ret);
3457 }
3458 
3459 int
3460 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3461     nvlist_t *props)
3462 {
3463         int ret;
3464         snapdata_t sd = { 0 };
3465         char fsname[ZFS_MAXNAMELEN];
3466         char *cp;
3467         zfs_handle_t *zhp;
3468         char errbuf[1024];
3469 
3470         (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3471             "cannot snapshot %s"), path);
3472 
3473         if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3474                 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3475 
3476         (void) strlcpy(fsname, path, sizeof (fsname));
3477         cp = strchr(fsname, '@');
3478         *cp = '\0';
3479         sd.sd_snapname = cp + 1;
3480 
3481         if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3482             ZFS_TYPE_VOLUME)) == NULL) {
3483                 return (-1);
3484         }
3485 
3486         verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
3487         if (recursive) {
3488                 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
3489         } else {
3490                 fnvlist_add_boolean(sd.sd_nvl, path);
3491         }
3492 
3493         ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
3494         nvlist_free(sd.sd_nvl);
3495         zfs_close(zhp);
3496         return (ret);
3497 }
3498 
3499 /*
3500  * Destroy any more recent snapshots.  We invoke this callback on any dependents
3501  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
3502  * is a dependent and we should just destroy it without checking the transaction
3503  * group.
3504  */
3505 typedef struct rollback_data {
3506         const char      *cb_target;             /* the snapshot */
3507         uint64_t        cb_create;              /* creation time reference */
3508         boolean_t       cb_error;
3509         boolean_t       cb_dependent;
3510         boolean_t       cb_force;
3511 } rollback_data_t;
3512 
3513 static int
3514 rollback_destroy(zfs_handle_t *zhp, void *data)
3515 {
3516         rollback_data_t *cbp = data;
3517 
3518         if (!cbp->cb_dependent) {
3519                 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3520                     zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3521                     zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3522                     cbp->cb_create) {
3523 
3524                         cbp->cb_dependent = B_TRUE;
3525                         cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3526                             rollback_destroy, cbp);
3527                         cbp->cb_dependent = B_FALSE;
3528 
3529                         cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3530                 }
3531         } else {
3532                 /* We must destroy this clone; first unmount it */
3533                 prop_changelist_t *clp;
3534 
3535                 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3536                     cbp->cb_force ? MS_FORCE: 0);
3537                 if (clp == NULL || changelist_prefix(clp) != 0) {
3538                         cbp->cb_error = B_TRUE;
3539                         zfs_close(zhp);
3540                         return (0);
3541                 }
3542                 if (zfs_destroy(zhp, B_FALSE) != 0)
3543                         cbp->cb_error = B_TRUE;
3544                 else
3545                         changelist_remove(clp, zhp->zfs_name);
3546                 (void) changelist_postfix(clp);
3547                 changelist_free(clp);
3548         }
3549 
3550         zfs_close(zhp);
3551         return (0);
3552 }
3553 
3554 /*
3555  * Given a dataset, rollback to a specific snapshot, discarding any
3556  * data changes since then and making it the active dataset.
3557  *
3558  * Any snapshots more recent than the target are destroyed, along with
3559  * their dependents.
3560  */
3561 int
3562 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3563 {
3564         rollback_data_t cb = { 0 };
3565         int err;
3566         zfs_cmd_t zc = { 0 };
3567         boolean_t restore_resv = 0;
3568         uint64_t old_volsize, new_volsize;
3569         zfs_prop_t resv_prop;
3570 
3571         assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3572             zhp->zfs_type == ZFS_TYPE_VOLUME);
3573 
3574         /*
3575          * Destroy all recent snapshots and their dependents.
3576          */
3577         cb.cb_force = force;
3578         cb.cb_target = snap->zfs_name;
3579         cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3580         (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3581 
3582         if (cb.cb_error)
3583                 return (-1);
3584 
3585         /*
3586          * Now that we have verified that the snapshot is the latest,
3587          * rollback to the given snapshot.
3588          */
3589 
3590         if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3591                 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3592                         return (-1);
3593                 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3594                 restore_resv =
3595                     (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3596         }
3597 
3598         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3599 
3600         if (ZFS_IS_VOLUME(zhp))
3601                 zc.zc_objset_type = DMU_OST_ZVOL;
3602         else
3603                 zc.zc_objset_type = DMU_OST_ZFS;
3604 
3605         /*
3606          * We rely on zfs_iter_children() to verify that there are no
3607          * newer snapshots for the given dataset.  Therefore, we can
3608          * simply pass the name on to the ioctl() call.  There is still
3609          * an unlikely race condition where the user has taken a
3610          * snapshot since we verified that this was the most recent.
3611          *
3612          */
3613         if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3614                 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3615                     dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3616                     zhp->zfs_name);
3617                 return (err);
3618         }
3619 
3620         /*
3621          * For volumes, if the pre-rollback volsize matched the pre-
3622          * rollback reservation and the volsize has changed then set
3623          * the reservation property to the post-rollback volsize.
3624          * Make a new handle since the rollback closed the dataset.
3625          */
3626         if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3627             (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3628                 if (restore_resv) {
3629                         new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3630                         if (old_volsize != new_volsize)
3631                                 err = zfs_prop_set_int(zhp, resv_prop,
3632                                     new_volsize);
3633                 }
3634                 zfs_close(zhp);
3635         }
3636         return (err);
3637 }
3638 
3639 /*
3640  * Renames the given dataset.
3641  */
3642 int
3643 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
3644     boolean_t force_unmount)
3645 {
3646         int ret;
3647         zfs_cmd_t zc = { 0 };
3648         char *delim;
3649         prop_changelist_t *cl = NULL;
3650         zfs_handle_t *zhrp = NULL;
3651         char *parentname = NULL;
3652         char parent[ZFS_MAXNAMELEN];
3653         libzfs_handle_t *hdl = zhp->zfs_hdl;
3654         char errbuf[1024];
3655 
3656         /* if we have the same exact name, just return success */
3657         if (strcmp(zhp->zfs_name, target) == 0)
3658                 return (0);
3659 
3660         (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3661             "cannot rename to '%s'"), target);
3662 
3663         /*
3664          * Make sure the target name is valid
3665          */
3666         if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3667                 if ((strchr(target, '@') == NULL) ||
3668                     *target == '@') {
3669                         /*
3670                          * Snapshot target name is abbreviated,
3671                          * reconstruct full dataset name
3672                          */
3673                         (void) strlcpy(parent, zhp->zfs_name,
3674                             sizeof (parent));
3675                         delim = strchr(parent, '@');
3676                         if (strchr(target, '@') == NULL)
3677                                 *(++delim) = '\0';
3678                         else
3679                                 *delim = '\0';
3680                         (void) strlcat(parent, target, sizeof (parent));
3681                         target = parent;
3682                 } else {
3683                         /*
3684                          * Make sure we're renaming within the same dataset.
3685                          */
3686                         delim = strchr(target, '@');
3687                         if (strncmp(zhp->zfs_name, target, delim - target)
3688                             != 0 || zhp->zfs_name[delim - target] != '@') {
3689                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3690                                     "snapshots must be part of same "
3691                                     "dataset"));
3692                                 return (zfs_error(hdl, EZFS_CROSSTARGET,
3693                                     errbuf));
3694                         }
3695                 }
3696                 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3697                         return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3698         } else {
3699                 if (recursive) {
3700                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3701                             "recursive rename must be a snapshot"));
3702                         return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3703                 }
3704 
3705                 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3706                         return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3707 
3708                 /* validate parents */
3709                 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3710                         return (-1);
3711 
3712                 /* make sure we're in the same pool */
3713                 verify((delim = strchr(target, '/')) != NULL);
3714                 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3715                     zhp->zfs_name[delim - target] != '/') {
3716                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3717                             "datasets must be within same pool"));
3718                         return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3719                 }
3720 
3721                 /* new name cannot be a child of the current dataset name */
3722                 if (is_descendant(zhp->zfs_name, target)) {
3723                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3724                             "New dataset name cannot be a descendant of "
3725                             "current dataset name"));
3726                         return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3727                 }
3728         }
3729 
3730         (void) snprintf(errbuf, sizeof (errbuf),
3731             dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3732 
3733         if (getzoneid() == GLOBAL_ZONEID &&
3734             zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3735                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3736                     "dataset is used in a non-global zone"));
3737                 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3738         }
3739 
3740         if (recursive) {
3741 
3742                 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3743                 if (parentname == NULL) {
3744                         ret = -1;
3745                         goto error;
3746                 }
3747                 delim = strchr(parentname, '@');
3748                 *delim = '\0';
3749                 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3750                 if (zhrp == NULL) {
3751                         ret = -1;
3752                         goto error;
3753                 }
3754 
3755         } else {
3756                 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3757                     force_unmount ? MS_FORCE : 0)) == NULL)
3758                         return (-1);
3759 
3760                 if (changelist_haszonedchild(cl)) {
3761                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3762                             "child dataset with inherited mountpoint is used "
3763                             "in a non-global zone"));
3764                         (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3765                         goto error;
3766                 }
3767 
3768                 if ((ret = changelist_prefix(cl)) != 0)
3769                         goto error;
3770         }
3771 
3772         if (ZFS_IS_VOLUME(zhp))
3773                 zc.zc_objset_type = DMU_OST_ZVOL;
3774         else
3775                 zc.zc_objset_type = DMU_OST_ZFS;
3776 
3777         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3778         (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3779 
3780         zc.zc_cookie = recursive;
3781 
3782         if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3783                 /*
3784                  * if it was recursive, the one that actually failed will
3785                  * be in zc.zc_name
3786                  */
3787                 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3788                     "cannot rename '%s'"), zc.zc_name);
3789 
3790                 if (recursive && errno == EEXIST) {
3791                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3792                             "a child dataset already has a snapshot "
3793                             "with the new name"));
3794                         (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3795                 } else {
3796                         (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3797                 }
3798 
3799                 /*
3800                  * On failure, we still want to remount any filesystems that
3801                  * were previously mounted, so we don't alter the system state.
3802                  */
3803                 if (!recursive)
3804                         (void) changelist_postfix(cl);
3805         } else {
3806                 if (!recursive) {
3807                         changelist_rename(cl, zfs_get_name(zhp), target);
3808                         ret = changelist_postfix(cl);
3809                 }
3810         }
3811 
3812 error:
3813         if (parentname) {
3814                 free(parentname);
3815         }
3816         if (zhrp) {
3817                 zfs_close(zhrp);
3818         }
3819         if (cl) {
3820                 changelist_free(cl);
3821         }
3822         return (ret);
3823 }
3824 
3825 nvlist_t *
3826 zfs_get_user_props(zfs_handle_t *zhp)
3827 {
3828         return (zhp->zfs_user_props);
3829 }
3830 
3831 nvlist_t *
3832 zfs_get_recvd_props(zfs_handle_t *zhp)
3833 {
3834         if (zhp->zfs_recvd_props == NULL)
3835                 if (get_recvd_props_ioctl(zhp) != 0)
3836                         return (NULL);
3837         return (zhp->zfs_recvd_props);
3838 }
3839 
3840 /*
3841  * This function is used by 'zfs list' to determine the exact set of columns to
3842  * display, and their maximum widths.  This does two main things:
3843  *
3844  *      - If this is a list of all properties, then expand the list to include
3845  *        all native properties, and set a flag so that for each dataset we look
3846  *        for new unique user properties and add them to the list.
3847  *
3848  *      - For non fixed-width properties, keep track of the maximum width seen
3849  *        so that we can size the column appropriately. If the user has
3850  *        requested received property values, we also need to compute the width
3851  *        of the RECEIVED column.
3852  */
3853 int
3854 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
3855 {
3856         libzfs_handle_t *hdl = zhp->zfs_hdl;
3857         zprop_list_t *entry;
3858         zprop_list_t **last, **start;
3859         nvlist_t *userprops, *propval;
3860         nvpair_t *elem;
3861         char *strval;
3862         char buf[ZFS_MAXPROPLEN];
3863 
3864         if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3865                 return (-1);
3866 
3867         userprops = zfs_get_user_props(zhp);
3868 
3869         entry = *plp;
3870         if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3871                 /*
3872                  * Go through and add any user properties as necessary.  We
3873                  * start by incrementing our list pointer to the first
3874                  * non-native property.
3875                  */
3876                 start = plp;
3877                 while (*start != NULL) {
3878                         if ((*start)->pl_prop == ZPROP_INVAL)
3879                                 break;
3880                         start = &(*start)->pl_next;
3881                 }
3882 
3883                 elem = NULL;
3884                 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3885                         /*
3886                          * See if we've already found this property in our list.
3887                          */
3888                         for (last = start; *last != NULL;
3889                             last = &(*last)->pl_next) {
3890                                 if (strcmp((*last)->pl_user_prop,
3891                                     nvpair_name(elem)) == 0)
3892                                         break;
3893                         }
3894 
3895                         if (*last == NULL) {
3896                                 if ((entry = zfs_alloc(hdl,
3897                                     sizeof (zprop_list_t))) == NULL ||
3898                                     ((entry->pl_user_prop = zfs_strdup(hdl,
3899                                     nvpair_name(elem)))) == NULL) {
3900                                         free(entry);
3901                                         return (-1);
3902                                 }
3903 
3904                                 entry->pl_prop = ZPROP_INVAL;
3905                                 entry->pl_width = strlen(nvpair_name(elem));
3906                                 entry->pl_all = B_TRUE;
3907                                 *last = entry;
3908                         }
3909                 }
3910         }
3911 
3912         /*
3913          * Now go through and check the width of any non-fixed columns
3914          */
3915         for (entry = *plp; entry != NULL; entry = entry->pl_next) {
3916                 if (entry->pl_fixed)
3917                         continue;
3918 
3919                 if (entry->pl_prop != ZPROP_INVAL) {
3920                         if (zfs_prop_get(zhp, entry->pl_prop,
3921                             buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
3922                                 if (strlen(buf) > entry->pl_width)
3923                                         entry->pl_width = strlen(buf);
3924                         }
3925                         if (received && zfs_prop_get_recvd(zhp,
3926                             zfs_prop_to_name(entry->pl_prop),
3927                             buf, sizeof (buf), B_FALSE) == 0)
3928                                 if (strlen(buf) > entry->pl_recvd_width)
3929                                         entry->pl_recvd_width = strlen(buf);
3930                 } else {
3931                         if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
3932                             &propval) == 0) {
3933                                 verify(nvlist_lookup_string(propval,
3934                                     ZPROP_VALUE, &strval) == 0);
3935                                 if (strlen(strval) > entry->pl_width)
3936                                         entry->pl_width = strlen(strval);
3937                         }
3938                         if (received && zfs_prop_get_recvd(zhp,
3939                             entry->pl_user_prop,
3940                             buf, sizeof (buf), B_FALSE) == 0)
3941                                 if (strlen(buf) > entry->pl_recvd_width)
3942                                         entry->pl_recvd_width = strlen(buf);
3943                 }
3944         }
3945 
3946         return (0);
3947 }
3948 
3949 int
3950 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
3951     char *resource, void *export, void *sharetab,
3952     int sharemax, zfs_share_op_t operation)
3953 {
3954         zfs_cmd_t zc = { 0 };
3955         int error;
3956 
3957         (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3958         (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3959         if (resource)
3960                 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
3961         zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
3962         zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
3963         zc.zc_share.z_sharetype = operation;
3964         zc.zc_share.z_sharemax = sharemax;
3965         error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
3966         return (error);
3967 }
3968 
3969 void
3970 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
3971 {
3972         nvpair_t *curr;
3973 
3974         /*
3975          * Keep a reference to the props-table against which we prune the
3976          * properties.
3977          */
3978         zhp->zfs_props_table = props;
3979 
3980         curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
3981 
3982         while (curr) {
3983                 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
3984                 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
3985 
3986                 /*
3987                  * User properties will result in ZPROP_INVAL, and since we
3988                  * only know how to prune standard ZFS properties, we always
3989                  * leave these in the list.  This can also happen if we
3990                  * encounter an unknown DSL property (when running older
3991                  * software, for example).
3992                  */
3993                 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
3994                         (void) nvlist_remove(zhp->zfs_props,
3995                             nvpair_name(curr), nvpair_type(curr));
3996                 curr = next;
3997         }
3998 }
3999 
4000 static int
4001 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4002     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4003 {
4004         zfs_cmd_t zc = { 0 };
4005         nvlist_t *nvlist = NULL;
4006         int error;
4007 
4008         (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4009         (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4010         zc.zc_cookie = (uint64_t)cmd;
4011 
4012         if (cmd == ZFS_SMB_ACL_RENAME) {
4013                 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4014                         (void) no_memory(hdl);
4015                         return (NULL);
4016                 }
4017         }
4018 
4019         switch (cmd) {
4020         case ZFS_SMB_ACL_ADD:
4021         case ZFS_SMB_ACL_REMOVE:
4022                 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4023                 break;
4024         case ZFS_SMB_ACL_RENAME:
4025                 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4026                     resource1) != 0) {
4027                                 (void) no_memory(hdl);
4028                                 return (-1);
4029                 }
4030                 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4031                     resource2) != 0) {
4032                                 (void) no_memory(hdl);
4033                                 return (-1);
4034                 }
4035                 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4036                         nvlist_free(nvlist);
4037                         return (-1);
4038                 }
4039                 break;
4040         case ZFS_SMB_ACL_PURGE:
4041                 break;
4042         default:
4043                 return (-1);
4044         }
4045         error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4046         if (nvlist)
4047                 nvlist_free(nvlist);
4048         return (error);
4049 }
4050 
4051 int
4052 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4053     char *path, char *resource)
4054 {
4055         return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4056             resource, NULL));
4057 }
4058 
4059 int
4060 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4061     char *path, char *resource)
4062 {
4063         return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4064             resource, NULL));
4065 }
4066 
4067 int
4068 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4069 {
4070         return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4071             NULL, NULL));
4072 }
4073 
4074 int
4075 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4076     char *oldname, char *newname)
4077 {
4078         return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4079             oldname, newname));
4080 }
4081 
4082 int
4083 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4084     zfs_userspace_cb_t func, void *arg)
4085 {
4086         zfs_cmd_t zc = { 0 };
4087         zfs_useracct_t buf[100];
4088         libzfs_handle_t *hdl = zhp->zfs_hdl;
4089         int ret;
4090 
4091         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4092 
4093         zc.zc_objset_type = type;
4094         zc.zc_nvlist_dst = (uintptr_t)buf;
4095 
4096         for (;;) {
4097                 zfs_useracct_t *zua = buf;
4098 
4099                 zc.zc_nvlist_dst_size = sizeof (buf);
4100                 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4101                         char errbuf[ZFS_MAXNAMELEN + 32];
4102 
4103                         (void) snprintf(errbuf, sizeof (errbuf),
4104                             dgettext(TEXT_DOMAIN,
4105                             "cannot get used/quota for %s"), zc.zc_name);
4106                         return (zfs_standard_error_fmt(hdl, errno, errbuf));
4107                 }
4108                 if (zc.zc_nvlist_dst_size == 0)
4109                         break;
4110 
4111                 while (zc.zc_nvlist_dst_size > 0) {
4112                         if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4113                             zua->zu_space)) != 0)
4114                                 return (ret);
4115                         zua++;
4116                         zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4117                 }
4118         }
4119 
4120         return (0);
4121 }
4122 
4123 int
4124 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4125     boolean_t recursive, boolean_t temphold, boolean_t enoent_ok,
4126     int cleanup_fd, uint64_t dsobj, uint64_t createtxg)
4127 {
4128         zfs_cmd_t zc = { 0 };
4129         libzfs_handle_t *hdl = zhp->zfs_hdl;
4130 
4131         ASSERT(!recursive || dsobj == 0);
4132 
4133         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4134         (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4135         if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4136             >= sizeof (zc.zc_string))
4137                 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4138         zc.zc_cookie = recursive;
4139         zc.zc_temphold = temphold;
4140         zc.zc_cleanup_fd = cleanup_fd;
4141         zc.zc_sendobj = dsobj;
4142         zc.zc_createtxg = createtxg;
4143 
4144         if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
4145                 char errbuf[ZFS_MAXNAMELEN+32];
4146 
4147                 /*
4148                  * if it was recursive, the one that actually failed will be in
4149                  * zc.zc_name.
4150                  */
4151                 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4152                     "cannot hold '%s@%s'"), zc.zc_name, snapname);
4153                 switch (errno) {
4154                 case E2BIG:
4155                         /*
4156                          * Temporary tags wind up having the ds object id
4157                          * prepended. So even if we passed the length check
4158                          * above, it's still possible for the tag to wind
4159                          * up being slightly too long.
4160                          */
4161                         return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf));
4162                 case ENOTSUP:
4163                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4164                             "pool must be upgraded"));
4165                         return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4166                 case EINVAL:
4167                         return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4168                 case EEXIST:
4169                         return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
4170                 case ENOENT:
4171                         if (enoent_ok)
4172                                 return (ENOENT);
4173                         /* FALLTHROUGH */
4174                 default:
4175                         return (zfs_standard_error_fmt(hdl, errno, errbuf));
4176                 }
4177         }
4178 
4179         return (0);
4180 }
4181 
4182 int
4183 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4184     boolean_t recursive)
4185 {
4186         zfs_cmd_t zc = { 0 };
4187         libzfs_handle_t *hdl = zhp->zfs_hdl;
4188 
4189         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4190         (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4191         if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4192             >= sizeof (zc.zc_string))
4193                 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4194         zc.zc_cookie = recursive;
4195 
4196         if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
4197                 char errbuf[ZFS_MAXNAMELEN+32];
4198 
4199                 /*
4200                  * if it was recursive, the one that actually failed will be in
4201                  * zc.zc_name.
4202                  */
4203                 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4204                     "cannot release '%s' from '%s@%s'"), tag, zc.zc_name,
4205                     snapname);
4206                 switch (errno) {
4207                 case ESRCH:
4208                         return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
4209                 case ENOTSUP:
4210                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4211                             "pool must be upgraded"));
4212                         return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4213                 case EINVAL:
4214                         return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4215                 default:
4216                         return (zfs_standard_error_fmt(hdl, errno, errbuf));
4217                 }
4218         }
4219 
4220         return (0);
4221 }
4222 
4223 int
4224 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4225 {
4226         zfs_cmd_t zc = { 0 };
4227         libzfs_handle_t *hdl = zhp->zfs_hdl;
4228         int nvsz = 2048;
4229         void *nvbuf;
4230         int err = 0;
4231         char errbuf[ZFS_MAXNAMELEN+32];
4232 
4233         assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4234             zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4235 
4236 tryagain:
4237 
4238         nvbuf = malloc(nvsz);
4239         if (nvbuf == NULL) {
4240                 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4241                 goto out;
4242         }
4243 
4244         zc.zc_nvlist_dst_size = nvsz;
4245         zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4246 
4247         (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4248 
4249         if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4250                 (void) snprintf(errbuf, sizeof (errbuf),
4251                     dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4252                     zc.zc_name);
4253                 switch (errno) {
4254                 case ENOMEM:
4255                         free(nvbuf);
4256                         nvsz = zc.zc_nvlist_dst_size;
4257                         goto tryagain;
4258 
4259                 case ENOTSUP:
4260                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4261                             "pool must be upgraded"));
4262                         err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4263                         break;
4264                 case EINVAL:
4265                         err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4266                         break;
4267                 case ENOENT:
4268                         err = zfs_error(hdl, EZFS_NOENT, errbuf);
4269                         break;
4270                 default:
4271                         err = zfs_standard_error_fmt(hdl, errno, errbuf);
4272                         break;
4273                 }
4274         } else {
4275                 /* success */
4276                 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4277                 if (rc) {
4278                         (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4279                             TEXT_DOMAIN, "cannot get permissions on '%s'"),
4280                             zc.zc_name);
4281                         err = zfs_standard_error_fmt(hdl, rc, errbuf);
4282                 }
4283         }
4284 
4285         free(nvbuf);
4286 out:
4287         return (err);
4288 }
4289 
4290 int
4291 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4292 {
4293         zfs_cmd_t zc = { 0 };
4294         libzfs_handle_t *hdl = zhp->zfs_hdl;
4295         char *nvbuf;
4296         char errbuf[ZFS_MAXNAMELEN+32];
4297         size_t nvsz;
4298         int err;
4299 
4300         assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4301             zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4302 
4303         err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4304         assert(err == 0);
4305 
4306         nvbuf = malloc(nvsz);
4307 
4308         err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4309         assert(err == 0);
4310 
4311         zc.zc_nvlist_src_size = nvsz;
4312         zc.zc_nvlist_src = (uintptr_t)nvbuf;
4313         zc.zc_perm_action = un;
4314 
4315         (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4316 
4317         if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4318                 (void) snprintf(errbuf, sizeof (errbuf),
4319                     dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4320                     zc.zc_name);
4321                 switch (errno) {
4322                 case ENOTSUP:
4323                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4324                             "pool must be upgraded"));
4325                         err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4326                         break;
4327                 case EINVAL:
4328                         err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4329                         break;
4330                 case ENOENT:
4331                         err = zfs_error(hdl, EZFS_NOENT, errbuf);
4332                         break;
4333                 default:
4334                         err = zfs_standard_error_fmt(hdl, errno, errbuf);
4335                         break;
4336                 }
4337         }
4338 
4339         free(nvbuf);
4340 
4341         return (err);
4342 }
4343 
4344 int
4345 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4346 {
4347         zfs_cmd_t zc = { 0 };
4348         libzfs_handle_t *hdl = zhp->zfs_hdl;
4349         int nvsz = 2048;
4350         void *nvbuf;
4351         int err = 0;
4352         char errbuf[ZFS_MAXNAMELEN+32];
4353 
4354         assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
4355 
4356 tryagain:
4357 
4358         nvbuf = malloc(nvsz);
4359         if (nvbuf == NULL) {
4360                 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4361                 goto out;
4362         }
4363 
4364         zc.zc_nvlist_dst_size = nvsz;
4365         zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4366 
4367         (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4368 
4369         if (zfs_ioctl(hdl, ZFS_IOC_GET_HOLDS, &zc) != 0) {
4370                 (void) snprintf(errbuf, sizeof (errbuf),
4371                     dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4372                     zc.zc_name);
4373                 switch (errno) {
4374                 case ENOMEM:
4375                         free(nvbuf);
4376                         nvsz = zc.zc_nvlist_dst_size;
4377                         goto tryagain;
4378 
4379                 case ENOTSUP:
4380                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4381                             "pool must be upgraded"));
4382                         err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4383                         break;
4384                 case EINVAL:
4385                         err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4386                         break;
4387                 case ENOENT:
4388                         err = zfs_error(hdl, EZFS_NOENT, errbuf);
4389                         break;
4390                 default:
4391                         err = zfs_standard_error_fmt(hdl, errno, errbuf);
4392                         break;
4393                 }
4394         } else {
4395                 /* success */
4396                 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4397                 if (rc) {
4398                         (void) snprintf(errbuf, sizeof (errbuf),
4399                             dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4400                             zc.zc_name);
4401                         err = zfs_standard_error_fmt(hdl, rc, errbuf);
4402                 }
4403         }
4404 
4405         free(nvbuf);
4406 out:
4407         return (err);
4408 }
4409 
4410 uint64_t
4411 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4412 {
4413         uint64_t numdb;
4414         uint64_t nblocks, volblocksize;
4415         int ncopies;
4416         char *strval;
4417 
4418         if (nvlist_lookup_string(props,
4419             zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4420                 ncopies = atoi(strval);
4421         else
4422                 ncopies = 1;
4423         if (nvlist_lookup_uint64(props,
4424             zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4425             &volblocksize) != 0)
4426                 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4427         nblocks = volsize/volblocksize;
4428         /* start with metadnode L0-L6 */
4429         numdb = 7;
4430         /* calculate number of indirects */
4431         while (nblocks > 1) {
4432                 nblocks += DNODES_PER_LEVEL - 1;
4433                 nblocks /= DNODES_PER_LEVEL;
4434                 numdb += nblocks;
4435         }
4436         numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4437         volsize *= ncopies;
4438         /*
4439          * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4440          * compressed, but in practice they compress down to about
4441          * 1100 bytes
4442          */
4443         numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4444         volsize += numdb;
4445         return (volsize);
4446 }