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