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