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