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