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  * Portions Copyright 2011 Martin Matuska
  25  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  26  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
  27  * Copyright (c) 2012 by Delphix. All rights reserved.
  28  */
  29 
  30 /*
  31  * ZFS ioctls.
  32  *
  33  * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
  34  * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
  35  *
  36  * There are two ways that we handle ioctls: the legacy way where almost
  37  * all of the logic is in the ioctl callback, and the new way where most
  38  * of the marshalling is handled in the common entry point, zfsdev_ioctl().
  39  *
  40  * Non-legacy ioctls should be registered by calling
  41  * zfs_ioctl_register() from zfs_ioctl_init().  The ioctl is invoked
  42  * from userland by lzc_ioctl().
  43  *
  44  * The registration arguments are as follows:
  45  *
  46  * const char *name
  47  *   The name of the ioctl.  This is used for history logging.  If the
  48  *   ioctl returns successfully (the callback returns 0), and allow_log
  49  *   is true, then a history log entry will be recorded with the input &
  50  *   output nvlists.  The log entry can be printed with "zpool history -i".
  51  *
  52  * zfs_ioc_t ioc
  53  *   The ioctl request number, which userland will pass to ioctl(2).
  54  *   The ioctl numbers can change from release to release, because
  55  *   the caller (libzfs) must be matched to the kernel.
  56  *
  57  * zfs_secpolicy_func_t *secpolicy
  58  *   This function will be called before the zfs_ioc_func_t, to
  59  *   determine if this operation is permitted.  It should return EPERM
  60  *   on failure, and 0 on success.  Checks include determining if the
  61  *   dataset is visible in this zone, and if the user has either all
  62  *   zfs privileges in the zone (SYS_MOUNT), or has been granted permission
  63  *   to do this operation on this dataset with "zfs allow".
  64  *
  65  * zfs_ioc_namecheck_t namecheck
  66  *   This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
  67  *   name, a dataset name, or nothing.  If the name is not well-formed,
  68  *   the ioctl will fail and the callback will not be called.
  69  *   Therefore, the callback can assume that the name is well-formed
  70  *   (e.g. is null-terminated, doesn't have more than one '@' character,
  71  *   doesn't have invalid characters).
  72  *
  73  * zfs_ioc_poolcheck_t pool_check
  74  *   This specifies requirements on the pool state.  If the pool does
  75  *   not meet them (is suspended or is readonly), the ioctl will fail
  76  *   and the callback will not be called.  If any checks are specified
  77  *   (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
  78  *   Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
  79  *   POOL_CHECK_READONLY).
  80  *
  81  * boolean_t smush_outnvlist
  82  *   If smush_outnvlist is true, then the output is presumed to be a
  83  *   list of errors, and it will be "smushed" down to fit into the
  84  *   caller's buffer, by removing some entries and replacing them with a
  85  *   single "N_MORE_ERRORS" entry indicating how many were removed.  See
  86  *   nvlist_smush() for details.  If smush_outnvlist is false, and the
  87  *   outnvlist does not fit into the userland-provided buffer, then the
  88  *   ioctl will fail with ENOMEM.
  89  *
  90  * zfs_ioc_func_t *func
  91  *   The callback function that will perform the operation.
  92  *
  93  *   The callback should return 0 on success, or an error number on
  94  *   failure.  If the function fails, the userland ioctl will return -1,
  95  *   and errno will be set to the callback's return value.  The callback
  96  *   will be called with the following arguments:
  97  *
  98  *   const char *name
  99  *     The name of the pool or dataset to operate on, from
 100  *     zfs_cmd_t:zc_name.  The 'namecheck' argument specifies the
 101  *     expected type (pool, dataset, or none).
 102  *
 103  *   nvlist_t *innvl
 104  *     The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src.  Or
 105  *     NULL if no input nvlist was provided.  Changes to this nvlist are
 106  *     ignored.  If the input nvlist could not be deserialized, the
 107  *     ioctl will fail and the callback will not be called.
 108  *
 109  *   nvlist_t *outnvl
 110  *     The output nvlist, initially empty.  The callback can fill it in,
 111  *     and it will be returned to userland by serializing it into
 112  *     zfs_cmd_t:zc_nvlist_dst.  If it is non-empty, and serialization
 113  *     fails (e.g. because the caller didn't supply a large enough
 114  *     buffer), then the overall ioctl will fail.  See the
 115  *     'smush_nvlist' argument above for additional behaviors.
 116  *
 117  *     There are two typical uses of the output nvlist:
 118  *       - To return state, e.g. property values.  In this case,
 119  *         smush_outnvlist should be false.  If the buffer was not large
 120  *         enough, the caller will reallocate a larger buffer and try
 121  *         the ioctl again.
 122  *
 123  *       - To return multiple errors from an ioctl which makes on-disk
 124  *         changes.  In this case, smush_outnvlist should be true.
 125  *         Ioctls which make on-disk modifications should generally not
 126  *         use the outnvl if they succeed, because the caller can not
 127  *         distinguish between the operation failing, and
 128  *         deserialization failing.
 129  */
 130 
 131 #include <sys/types.h>
 132 #include <sys/param.h>
 133 #include <sys/errno.h>
 134 #include <sys/uio.h>
 135 #include <sys/buf.h>
 136 #include <sys/modctl.h>
 137 #include <sys/open.h>
 138 #include <sys/file.h>
 139 #include <sys/kmem.h>
 140 #include <sys/conf.h>
 141 #include <sys/cmn_err.h>
 142 #include <sys/stat.h>
 143 #include <sys/zfs_ioctl.h>
 144 #include <sys/zfs_vfsops.h>
 145 #include <sys/zfs_znode.h>
 146 #include <sys/zap.h>
 147 #include <sys/spa.h>
 148 #include <sys/spa_impl.h>
 149 #include <sys/vdev.h>
 150 #include <sys/priv_impl.h>
 151 #include <sys/dmu.h>
 152 #include <sys/dsl_dir.h>
 153 #include <sys/dsl_dataset.h>
 154 #include <sys/dsl_prop.h>
 155 #include <sys/dsl_deleg.h>
 156 #include <sys/dmu_objset.h>
 157 #include <sys/dmu_impl.h>
 158 #include <sys/ddi.h>
 159 #include <sys/sunddi.h>
 160 #include <sys/sunldi.h>
 161 #include <sys/policy.h>
 162 #include <sys/zone.h>
 163 #include <sys/nvpair.h>
 164 #include <sys/pathname.h>
 165 #include <sys/mount.h>
 166 #include <sys/sdt.h>
 167 #include <sys/fs/zfs.h>
 168 #include <sys/zfs_ctldir.h>
 169 #include <sys/zfs_dir.h>
 170 #include <sys/zfs_onexit.h>
 171 #include <sys/zvol.h>
 172 #include <sys/dsl_scan.h>
 173 #include <sharefs/share.h>
 174 #include <sys/dmu_objset.h>
 175 
 176 #include "zfs_namecheck.h"
 177 #include "zfs_prop.h"
 178 #include "zfs_deleg.h"
 179 #include "zfs_comutil.h"
 180 
 181 extern struct modlfs zfs_modlfs;
 182 
 183 extern void zfs_init(void);
 184 extern void zfs_fini(void);
 185 
 186 ldi_ident_t zfs_li = NULL;
 187 dev_info_t *zfs_dip;
 188 
 189 uint_t zfs_fsyncer_key;
 190 extern uint_t rrw_tsd_key;
 191 static uint_t zfs_allow_log_key;
 192 
 193 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
 194 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
 195 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
 196 
 197 typedef enum {
 198         NO_NAME,
 199         POOL_NAME,
 200         DATASET_NAME
 201 } zfs_ioc_namecheck_t;
 202 
 203 typedef enum {
 204         POOL_CHECK_NONE         = 1 << 0,
 205         POOL_CHECK_SUSPENDED    = 1 << 1,
 206         POOL_CHECK_READONLY     = 1 << 2,
 207 } zfs_ioc_poolcheck_t;
 208 
 209 typedef struct zfs_ioc_vec {
 210         zfs_ioc_legacy_func_t   *zvec_legacy_func;
 211         zfs_ioc_func_t          *zvec_func;
 212         zfs_secpolicy_func_t    *zvec_secpolicy;
 213         zfs_ioc_namecheck_t     zvec_namecheck;
 214         boolean_t               zvec_allow_log;
 215         zfs_ioc_poolcheck_t     zvec_pool_check;
 216         boolean_t               zvec_smush_outnvlist;
 217         const char              *zvec_name;
 218 } zfs_ioc_vec_t;
 219 
 220 /* This array is indexed by zfs_userquota_prop_t */
 221 static const char *userquota_perms[] = {
 222         ZFS_DELEG_PERM_USERUSED,
 223         ZFS_DELEG_PERM_USERQUOTA,
 224         ZFS_DELEG_PERM_GROUPUSED,
 225         ZFS_DELEG_PERM_GROUPQUOTA,
 226 };
 227 
 228 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
 229 static int zfs_check_settable(const char *name, nvpair_t *property,
 230     cred_t *cr);
 231 static int zfs_check_clearable(char *dataset, nvlist_t *props,
 232     nvlist_t **errors);
 233 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
 234     boolean_t *);
 235 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
 236 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
 237 
 238 static int
 239 zfs_is_wormed(const char *name)
 240 {
 241         char worminfo[13] = {0};
 242         char cname[MAXNAMELEN];
 243         char *end;
 244 
 245         (void) strlcpy(cname, name, MAXNAMELEN);
 246         end = strchr(cname, '@');
 247         if (end)
 248                 *end = 0;
 249 
 250         if (dsl_prop_get(cname, "nms:worm", 1, 12, &worminfo, NULL) == 0 &&
 251             worminfo[0] && strcmp(worminfo, "0") != 0 &&
 252             strcmp(worminfo, "off") != 0 && strcmp(worminfo, "-") != 0) {
 253                 return (1);
 254         }
 255         return (0);
 256 }
 257 
 258 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
 259 void
 260 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
 261 {
 262         const char *newfile;
 263         char buf[512];
 264         va_list adx;
 265 
 266         /*
 267          * Get rid of annoying "../common/" prefix to filename.
 268          */
 269         newfile = strrchr(file, '/');
 270         if (newfile != NULL) {
 271                 newfile = newfile + 1; /* Get rid of leading / */
 272         } else {
 273                 newfile = file;
 274         }
 275 
 276         va_start(adx, fmt);
 277         (void) vsnprintf(buf, sizeof (buf), fmt, adx);
 278         va_end(adx);
 279 
 280         /*
 281          * To get this data, use the zfs-dprintf probe as so:
 282          * dtrace -q -n 'zfs-dprintf \
 283          *      /stringof(arg0) == "dbuf.c"/ \
 284          *      {printf("%s: %s", stringof(arg1), stringof(arg3))}'
 285          * arg0 = file name
 286          * arg1 = function name
 287          * arg2 = line number
 288          * arg3 = message
 289          */
 290         DTRACE_PROBE4(zfs__dprintf,
 291             char *, newfile, char *, func, int, line, char *, buf);
 292 }
 293 
 294 static void
 295 history_str_free(char *buf)
 296 {
 297         kmem_free(buf, HIS_MAX_RECORD_LEN);
 298 }
 299 
 300 static char *
 301 history_str_get(zfs_cmd_t *zc)
 302 {
 303         char *buf;
 304 
 305         if (zc->zc_history == NULL)
 306                 return (NULL);
 307 
 308         buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
 309         if (copyinstr((void *)(uintptr_t)zc->zc_history,
 310             buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
 311                 history_str_free(buf);
 312                 return (NULL);
 313         }
 314 
 315         buf[HIS_MAX_RECORD_LEN -1] = '\0';
 316 
 317         return (buf);
 318 }
 319 
 320 /*
 321  * Check to see if the named dataset is currently defined as bootable
 322  */
 323 static boolean_t
 324 zfs_is_bootfs(const char *name)
 325 {
 326         objset_t *os;
 327 
 328         if (dmu_objset_hold(name, FTAG, &os) == 0) {
 329                 boolean_t ret;
 330                 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
 331                 dmu_objset_rele(os, FTAG);
 332                 return (ret);
 333         }
 334         return (B_FALSE);
 335 }
 336 
 337 /*
 338  * zfs_earlier_version
 339  *
 340  *      Return non-zero if the spa version is less than requested version.
 341  */
 342 static int
 343 zfs_earlier_version(const char *name, int version)
 344 {
 345         spa_t *spa;
 346 
 347         if (spa_open(name, &spa, FTAG) == 0) {
 348                 if (spa_version(spa) < version) {
 349                         spa_close(spa, FTAG);
 350                         return (1);
 351                 }
 352                 spa_close(spa, FTAG);
 353         }
 354         return (0);
 355 }
 356 
 357 /*
 358  * zpl_earlier_version
 359  *
 360  * Return TRUE if the ZPL version is less than requested version.
 361  */
 362 static boolean_t
 363 zpl_earlier_version(const char *name, int version)
 364 {
 365         objset_t *os;
 366         boolean_t rc = B_TRUE;
 367 
 368         if (dmu_objset_hold(name, FTAG, &os) == 0) {
 369                 uint64_t zplversion;
 370 
 371                 if (dmu_objset_type(os) != DMU_OST_ZFS) {
 372                         dmu_objset_rele(os, FTAG);
 373                         return (B_TRUE);
 374                 }
 375                 /* XXX reading from non-owned objset */
 376                 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
 377                         rc = zplversion < version;
 378                 dmu_objset_rele(os, FTAG);
 379         }
 380         return (rc);
 381 }
 382 
 383 static void
 384 zfs_log_history(zfs_cmd_t *zc)
 385 {
 386         spa_t *spa;
 387         char *buf;
 388 
 389         if ((buf = history_str_get(zc)) == NULL)
 390                 return;
 391 
 392         if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
 393                 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
 394                         (void) spa_history_log(spa, buf);
 395                 spa_close(spa, FTAG);
 396         }
 397         history_str_free(buf);
 398 }
 399 
 400 /*
 401  * Policy for top-level read operations (list pools).  Requires no privileges,
 402  * and can be used in the local zone, as there is no associated dataset.
 403  */
 404 /* ARGSUSED */
 405 static int
 406 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 407 {
 408         return (0);
 409 }
 410 
 411 /*
 412  * Policy for dataset read operations (list children, get statistics).  Requires
 413  * no privileges, but must be visible in the local zone.
 414  */
 415 /* ARGSUSED */
 416 static int
 417 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 418 {
 419         if (INGLOBALZONE(curproc) ||
 420             zone_dataset_visible(zc->zc_name, NULL))
 421                 return (0);
 422 
 423         return (ENOENT);
 424 }
 425 
 426 static int
 427 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
 428 {
 429         int writable = 1;
 430 
 431         /*
 432          * The dataset must be visible by this zone -- check this first
 433          * so they don't see EPERM on something they shouldn't know about.
 434          */
 435         if (!INGLOBALZONE(curproc) &&
 436             !zone_dataset_visible(dataset, &writable))
 437                 return (ENOENT);
 438 
 439         if (INGLOBALZONE(curproc)) {
 440                 /*
 441                  * If the fs is zoned, only root can access it from the
 442                  * global zone.
 443                  */
 444                 if (secpolicy_zfs(cr) && zoned)
 445                         return (EPERM);
 446         } else {
 447                 /*
 448                  * If we are in a local zone, the 'zoned' property must be set.
 449                  */
 450                 if (!zoned)
 451                         return (EPERM);
 452 
 453                 /* must be writable by this zone */
 454                 if (!writable)
 455                         return (EPERM);
 456         }
 457         return (0);
 458 }
 459 
 460 static int
 461 zfs_dozonecheck(const char *dataset, cred_t *cr)
 462 {
 463         uint64_t zoned;
 464 
 465         if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
 466                 return (ENOENT);
 467 
 468         return (zfs_dozonecheck_impl(dataset, zoned, cr));
 469 }
 470 
 471 static int
 472 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
 473 {
 474         uint64_t zoned;
 475 
 476         rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
 477         if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL)) {
 478                 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
 479                 return (ENOENT);
 480         }
 481         rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
 482 
 483         return (zfs_dozonecheck_impl(dataset, zoned, cr));
 484 }
 485 
 486 static int
 487 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
 488 {
 489         int error;
 490         dsl_dataset_t *ds;
 491 
 492         error = dsl_dataset_hold(name, FTAG, &ds);
 493         if (error != 0)
 494                 return (error);
 495 
 496         error = zfs_dozonecheck_ds(name, ds, cr);
 497         if (error == 0) {
 498                 error = secpolicy_zfs(cr);
 499                 if (error)
 500                         error = dsl_deleg_access_impl(ds, perm, cr);
 501         }
 502 
 503         dsl_dataset_rele(ds, FTAG);
 504         return (error);
 505 }
 506 
 507 static int
 508 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
 509     const char *perm, cred_t *cr)
 510 {
 511         int error;
 512 
 513         error = zfs_dozonecheck_ds(name, ds, cr);
 514         if (error == 0) {
 515                 error = secpolicy_zfs(cr);
 516                 if (error)
 517                         error = dsl_deleg_access_impl(ds, perm, cr);
 518         }
 519         return (error);
 520 }
 521 
 522 /*
 523  * Policy for setting the security label property.
 524  *
 525  * Returns 0 for success, non-zero for access and other errors.
 526  */
 527 static int
 528 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
 529 {
 530         char            ds_hexsl[MAXNAMELEN];
 531         bslabel_t       ds_sl, new_sl;
 532         boolean_t       new_default = FALSE;
 533         uint64_t        zoned;
 534         int             needed_priv = -1;
 535         int             error;
 536 
 537         /* First get the existing dataset label. */
 538         error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
 539             1, sizeof (ds_hexsl), &ds_hexsl, NULL);
 540         if (error)
 541                 return (EPERM);
 542 
 543         if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
 544                 new_default = TRUE;
 545 
 546         /* The label must be translatable */
 547         if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
 548                 return (EINVAL);
 549 
 550         /*
 551          * In a non-global zone, disallow attempts to set a label that
 552          * doesn't match that of the zone; otherwise no other checks
 553          * are needed.
 554          */
 555         if (!INGLOBALZONE(curproc)) {
 556                 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
 557                         return (EPERM);
 558                 return (0);
 559         }
 560 
 561         /*
 562          * For global-zone datasets (i.e., those whose zoned property is
 563          * "off", verify that the specified new label is valid for the
 564          * global zone.
 565          */
 566         if (dsl_prop_get_integer(name,
 567             zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
 568                 return (EPERM);
 569         if (!zoned) {
 570                 if (zfs_check_global_label(name, strval) != 0)
 571                         return (EPERM);
 572         }
 573 
 574         /*
 575          * If the existing dataset label is nondefault, check if the
 576          * dataset is mounted (label cannot be changed while mounted).
 577          * Get the zfsvfs; if there isn't one, then the dataset isn't
 578          * mounted (or isn't a dataset, doesn't exist, ...).
 579          */
 580         if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
 581                 objset_t *os;
 582                 static char *setsl_tag = "setsl_tag";
 583 
 584                 /*
 585                  * Try to own the dataset; abort if there is any error,
 586                  * (e.g., already mounted, in use, or other error).
 587                  */
 588                 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
 589                     setsl_tag, &os);
 590                 if (error)
 591                         return (EPERM);
 592 
 593                 dmu_objset_disown(os, setsl_tag);
 594 
 595                 if (new_default) {
 596                         needed_priv = PRIV_FILE_DOWNGRADE_SL;
 597                         goto out_check;
 598                 }
 599 
 600                 if (hexstr_to_label(strval, &new_sl) != 0)
 601                         return (EPERM);
 602 
 603                 if (blstrictdom(&ds_sl, &new_sl))
 604                         needed_priv = PRIV_FILE_DOWNGRADE_SL;
 605                 else if (blstrictdom(&new_sl, &ds_sl))
 606                         needed_priv = PRIV_FILE_UPGRADE_SL;
 607         } else {
 608                 /* dataset currently has a default label */
 609                 if (!new_default)
 610                         needed_priv = PRIV_FILE_UPGRADE_SL;
 611         }
 612 
 613 out_check:
 614         if (needed_priv != -1)
 615                 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
 616         return (0);
 617 }
 618 
 619 static int
 620 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
 621     cred_t *cr)
 622 {
 623         char *strval;
 624 
 625         /*
 626          * Check permissions for special properties.
 627          */
 628         switch (prop) {
 629         case ZFS_PROP_ZONED:
 630                 /*
 631                  * Disallow setting of 'zoned' from within a local zone.
 632                  */
 633                 if (!INGLOBALZONE(curproc))
 634                         return (EPERM);
 635                 break;
 636 
 637         case ZFS_PROP_QUOTA:
 638                 if (!INGLOBALZONE(curproc)) {
 639                         uint64_t zoned;
 640                         char setpoint[MAXNAMELEN];
 641                         /*
 642                          * Unprivileged users are allowed to modify the
 643                          * quota on things *under* (ie. contained by)
 644                          * the thing they own.
 645                          */
 646                         if (dsl_prop_get_integer(dsname, "zoned", &zoned,
 647                             setpoint))
 648                                 return (EPERM);
 649                         if (!zoned || strlen(dsname) <= strlen(setpoint))
 650                                 return (EPERM);
 651                 }
 652                 break;
 653 
 654         case ZFS_PROP_MLSLABEL:
 655                 if (!is_system_labeled())
 656                         return (EPERM);
 657 
 658                 if (nvpair_value_string(propval, &strval) == 0) {
 659                         int err;
 660 
 661                         err = zfs_set_slabel_policy(dsname, strval, CRED());
 662                         if (err != 0)
 663                                 return (err);
 664                 }
 665                 break;
 666         }
 667 
 668         return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
 669 }
 670 
 671 /* ARGSUSED */
 672 static int
 673 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 674 {
 675         int error;
 676 
 677         error = zfs_dozonecheck(zc->zc_name, cr);
 678         if (error)
 679                 return (error);
 680 
 681         /*
 682          * permission to set permissions will be evaluated later in
 683          * dsl_deleg_can_allow()
 684          */
 685         return (0);
 686 }
 687 
 688 /* ARGSUSED */
 689 static int
 690 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 691 {
 692         return (zfs_secpolicy_write_perms(zc->zc_name,
 693             ZFS_DELEG_PERM_ROLLBACK, cr));
 694 }
 695 
 696 /* ARGSUSED */
 697 static int
 698 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 699 {
 700         spa_t *spa;
 701         dsl_pool_t *dp;
 702         dsl_dataset_t *ds;
 703         char *cp;
 704         int error;
 705 
 706         /*
 707          * Generate the current snapshot name from the given objsetid, then
 708          * use that name for the secpolicy/zone checks.
 709          */
 710         cp = strchr(zc->zc_name, '@');
 711         if (cp == NULL)
 712                 return (EINVAL);
 713         error = spa_open(zc->zc_name, &spa, FTAG);
 714         if (error)
 715                 return (error);
 716 
 717         dp = spa_get_dsl(spa);
 718         rw_enter(&dp->dp_config_rwlock, RW_READER);
 719         error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
 720         rw_exit(&dp->dp_config_rwlock);
 721         spa_close(spa, FTAG);
 722         if (error)
 723                 return (error);
 724 
 725         dsl_dataset_name(ds, zc->zc_name);
 726 
 727         error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
 728             ZFS_DELEG_PERM_SEND, cr);
 729         dsl_dataset_rele(ds, FTAG);
 730 
 731         return (error);
 732 }
 733 
 734 /* ARGSUSED */
 735 static int
 736 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 737 {
 738         return (zfs_secpolicy_write_perms(zc->zc_name,
 739             ZFS_DELEG_PERM_SEND, cr));
 740 }
 741 
 742 /* ARGSUSED */
 743 static int
 744 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 745 {
 746         vnode_t *vp;
 747         int error;
 748 
 749         if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
 750             NO_FOLLOW, NULL, &vp)) != 0)
 751                 return (error);
 752 
 753         /* Now make sure mntpnt and dataset are ZFS */
 754 
 755         if (vp->v_vfsp->vfs_fstype != zfsfstype ||
 756             (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
 757             zc->zc_name) != 0)) {
 758                 VN_RELE(vp);
 759                 return (EPERM);
 760         }
 761 
 762         VN_RELE(vp);
 763         return (dsl_deleg_access(zc->zc_name,
 764             ZFS_DELEG_PERM_SHARE, cr));
 765 }
 766 
 767 int
 768 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 769 {
 770         if (!INGLOBALZONE(curproc))
 771                 return (EPERM);
 772 
 773         if (secpolicy_nfs(cr) == 0) {
 774                 return (0);
 775         } else {
 776                 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
 777         }
 778 }
 779 
 780 int
 781 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 782 {
 783         if (!INGLOBALZONE(curproc))
 784                 return (EPERM);
 785 
 786         if (secpolicy_smb(cr) == 0) {
 787                 return (0);
 788         } else {
 789                 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
 790         }
 791 }
 792 
 793 static int
 794 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
 795 {
 796         char *cp;
 797 
 798         /*
 799          * Remove the @bla or /bla from the end of the name to get the parent.
 800          */
 801         (void) strncpy(parent, datasetname, parentsize);
 802         cp = strrchr(parent, '@');
 803         if (cp != NULL) {
 804                 cp[0] = '\0';
 805         } else {
 806                 cp = strrchr(parent, '/');
 807                 if (cp == NULL)
 808                         return (ENOENT);
 809                 cp[0] = '\0';
 810         }
 811 
 812         return (0);
 813 }
 814 
 815 int
 816 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
 817 {
 818         int error;
 819 
 820         if ((error = zfs_secpolicy_write_perms(name,
 821             ZFS_DELEG_PERM_MOUNT, cr)) != 0)
 822                 return (error);
 823 
 824         return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
 825 }
 826 
 827 /* ARGSUSED */
 828 static int
 829 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 830 {
 831         return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
 832 }
 833 
 834 /*
 835  * Destroying snapshots with delegated permissions requires
 836  * descendant mount and destroy permissions.
 837  */
 838 /* ARGSUSED */
 839 static int
 840 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 841 {
 842         nvlist_t *snaps;
 843         nvpair_t *pair, *nextpair;
 844         int error = 0;
 845 
 846         if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
 847                 return (EINVAL);
 848         for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
 849             pair = nextpair) {
 850                 dsl_dataset_t *ds;
 851 
 852                 nextpair = nvlist_next_nvpair(snaps, pair);
 853                 error = dsl_dataset_hold(nvpair_name(pair), FTAG, &ds);
 854                 if (error == 0) {
 855                         dsl_dataset_rele(ds, FTAG);
 856                 } else if (error == ENOENT) {
 857                         /*
 858                          * Ignore any snapshots that don't exist (we consider
 859                          * them "already destroyed").  Remove the name from the
 860                          * nvl here in case the snapshot is created between
 861                          * now and when we try to destroy it (in which case
 862                          * we don't want to destroy it since we haven't
 863                          * checked for permission).
 864                          */
 865                         fnvlist_remove_nvpair(snaps, pair);
 866                         error = 0;
 867                         continue;
 868                 } else {
 869                         break;
 870                 }
 871                 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
 872                 if (error != 0)
 873                         break;
 874         }
 875 
 876         return (error);
 877 }
 878 
 879 int
 880 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
 881 {
 882         char    parentname[MAXNAMELEN];
 883         int     error;
 884 
 885         if ((error = zfs_secpolicy_write_perms(from,
 886             ZFS_DELEG_PERM_RENAME, cr)) != 0)
 887                 return (error);
 888 
 889         if ((error = zfs_secpolicy_write_perms(from,
 890             ZFS_DELEG_PERM_MOUNT, cr)) != 0)
 891                 return (error);
 892 
 893         if ((error = zfs_get_parent(to, parentname,
 894             sizeof (parentname))) != 0)
 895                 return (error);
 896 
 897         if ((error = zfs_secpolicy_write_perms(parentname,
 898             ZFS_DELEG_PERM_CREATE, cr)) != 0)
 899                 return (error);
 900 
 901         if ((error = zfs_secpolicy_write_perms(parentname,
 902             ZFS_DELEG_PERM_MOUNT, cr)) != 0)
 903                 return (error);
 904 
 905         return (error);
 906 }
 907 
 908 /* ARGSUSED */
 909 static int
 910 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 911 {
 912         return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
 913 }
 914 
 915 /* ARGSUSED */
 916 static int
 917 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 918 {
 919         char    parentname[MAXNAMELEN];
 920         objset_t *clone;
 921         int error;
 922 
 923         error = zfs_secpolicy_write_perms(zc->zc_name,
 924             ZFS_DELEG_PERM_PROMOTE, cr);
 925         if (error)
 926                 return (error);
 927 
 928         error = dmu_objset_hold(zc->zc_name, FTAG, &clone);
 929 
 930         if (error == 0) {
 931                 dsl_dataset_t *pclone = NULL;
 932                 dsl_dir_t *dd;
 933                 dd = clone->os_dsl_dataset->ds_dir;
 934 
 935                 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
 936                 error = dsl_dataset_hold_obj(dd->dd_pool,
 937                     dd->dd_phys->dd_origin_obj, FTAG, &pclone);
 938                 rw_exit(&dd->dd_pool->dp_config_rwlock);
 939                 if (error) {
 940                         dmu_objset_rele(clone, FTAG);
 941                         return (error);
 942                 }
 943 
 944                 error = zfs_secpolicy_write_perms(zc->zc_name,
 945                     ZFS_DELEG_PERM_MOUNT, cr);
 946 
 947                 dsl_dataset_name(pclone, parentname);
 948                 dmu_objset_rele(clone, FTAG);
 949                 dsl_dataset_rele(pclone, FTAG);
 950                 if (error == 0)
 951                         error = zfs_secpolicy_write_perms(parentname,
 952                             ZFS_DELEG_PERM_PROMOTE, cr);
 953         }
 954         return (error);
 955 }
 956 
 957 /* ARGSUSED */
 958 static int
 959 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 960 {
 961         int error;
 962 
 963         if ((error = zfs_secpolicy_write_perms(zc->zc_name,
 964             ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
 965                 return (error);
 966 
 967         if ((error = zfs_secpolicy_write_perms(zc->zc_name,
 968             ZFS_DELEG_PERM_MOUNT, cr)) != 0)
 969                 return (error);
 970 
 971         return (zfs_secpolicy_write_perms(zc->zc_name,
 972             ZFS_DELEG_PERM_CREATE, cr));
 973 }
 974 
 975 int
 976 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
 977 {
 978         return (zfs_secpolicy_write_perms(name,
 979             ZFS_DELEG_PERM_SNAPSHOT, cr));
 980 }
 981 
 982 /*
 983  * Check for permission to create each snapshot in the nvlist.
 984  */
 985 /* ARGSUSED */
 986 static int
 987 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 988 {
 989         nvlist_t *snaps;
 990         int error;
 991         nvpair_t *pair;
 992 
 993         if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
 994                 return (EINVAL);
 995         for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
 996             pair = nvlist_next_nvpair(snaps, pair)) {
 997                 char *name = nvpair_name(pair);
 998                 char *atp = strchr(name, '@');
 999 
1000                 if (atp == NULL) {
1001                         error = EINVAL;
1002                         break;
1003                 }
1004                 *atp = '\0';
1005                 error = zfs_secpolicy_snapshot_perms(name, cr);
1006                 *atp = '@';
1007                 if (error != 0)
1008                         break;
1009         }
1010         return (error);
1011 }
1012 
1013 /* ARGSUSED */
1014 static int
1015 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1016 {
1017         /*
1018          * Even root must have a proper TSD so that we know what pool
1019          * to log to.
1020          */
1021         if (tsd_get(zfs_allow_log_key) == NULL)
1022                 return (EPERM);
1023         return (0);
1024 }
1025 
1026 static int
1027 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1028 {
1029         char    parentname[MAXNAMELEN];
1030         int     error;
1031         char    *origin;
1032 
1033         if ((error = zfs_get_parent(zc->zc_name, parentname,
1034             sizeof (parentname))) != 0)
1035                 return (error);
1036 
1037         if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1038             (error = zfs_secpolicy_write_perms(origin,
1039             ZFS_DELEG_PERM_CLONE, cr)) != 0)
1040                 return (error);
1041 
1042         if ((error = zfs_secpolicy_write_perms(parentname,
1043             ZFS_DELEG_PERM_CREATE, cr)) != 0)
1044                 return (error);
1045 
1046         return (zfs_secpolicy_write_perms(parentname,
1047             ZFS_DELEG_PERM_MOUNT, cr));
1048 }
1049 
1050 /*
1051  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
1052  * SYS_CONFIG privilege, which is not available in a local zone.
1053  */
1054 /* ARGSUSED */
1055 static int
1056 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1057 {
1058         if (secpolicy_sys_config(cr, B_FALSE) != 0)
1059                 return (EPERM);
1060 
1061         return (0);
1062 }
1063 
1064 /*
1065  * Policy for object to name lookups.
1066  */
1067 /* ARGSUSED */
1068 static int
1069 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1070 {
1071         int error;
1072 
1073         if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1074                 return (0);
1075 
1076         error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1077         return (error);
1078 }
1079 
1080 /*
1081  * Policy for fault injection.  Requires all privileges.
1082  */
1083 /* ARGSUSED */
1084 static int
1085 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1086 {
1087         return (secpolicy_zinject(cr));
1088 }
1089 
1090 /* ARGSUSED */
1091 static int
1092 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1093 {
1094         zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1095 
1096         if (prop == ZPROP_INVAL) {
1097                 if (!zfs_prop_user(zc->zc_value))
1098                         return (EINVAL);
1099                 return (zfs_secpolicy_write_perms(zc->zc_name,
1100                     ZFS_DELEG_PERM_USERPROP, cr));
1101         } else {
1102                 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1103                     NULL, cr));
1104         }
1105 }
1106 
1107 static int
1108 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1109 {
1110         int err = zfs_secpolicy_read(zc, innvl, cr);
1111         if (err)
1112                 return (err);
1113 
1114         if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1115                 return (EINVAL);
1116 
1117         if (zc->zc_value[0] == 0) {
1118                 /*
1119                  * They are asking about a posix uid/gid.  If it's
1120                  * themself, allow it.
1121                  */
1122                 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1123                     zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
1124                         if (zc->zc_guid == crgetuid(cr))
1125                                 return (0);
1126                 } else {
1127                         if (groupmember(zc->zc_guid, cr))
1128                                 return (0);
1129                 }
1130         }
1131 
1132         return (zfs_secpolicy_write_perms(zc->zc_name,
1133             userquota_perms[zc->zc_objset_type], cr));
1134 }
1135 
1136 static int
1137 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1138 {
1139         int err = zfs_secpolicy_read(zc, innvl, cr);
1140         if (err)
1141                 return (err);
1142 
1143         if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1144                 return (EINVAL);
1145 
1146         return (zfs_secpolicy_write_perms(zc->zc_name,
1147             userquota_perms[zc->zc_objset_type], cr));
1148 }
1149 
1150 /* ARGSUSED */
1151 static int
1152 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1153 {
1154         return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1155             NULL, cr));
1156 }
1157 
1158 /* ARGSUSED */
1159 static int
1160 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1161 {
1162         return (zfs_secpolicy_write_perms(zc->zc_name,
1163             ZFS_DELEG_PERM_HOLD, cr));
1164 }
1165 
1166 /* ARGSUSED */
1167 static int
1168 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1169 {
1170         return (zfs_secpolicy_write_perms(zc->zc_name,
1171             ZFS_DELEG_PERM_RELEASE, cr));
1172 }
1173 
1174 /*
1175  * Policy for allowing temporary snapshots to be taken or released
1176  */
1177 static int
1178 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1179 {
1180         /*
1181          * A temporary snapshot is the same as a snapshot,
1182          * hold, destroy and release all rolled into one.
1183          * Delegated diff alone is sufficient that we allow this.
1184          */
1185         int error;
1186 
1187         if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1188             ZFS_DELEG_PERM_DIFF, cr)) == 0)
1189                 return (0);
1190 
1191         error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1192         if (!error)
1193                 error = zfs_secpolicy_hold(zc, innvl, cr);
1194         if (!error)
1195                 error = zfs_secpolicy_release(zc, innvl, cr);
1196         if (!error)
1197                 error = zfs_secpolicy_destroy(zc, innvl, cr);
1198         return (error);
1199 }
1200 
1201 /*
1202  * Returns the nvlist as specified by the user in the zfs_cmd_t.
1203  */
1204 static int
1205 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1206 {
1207         char *packed;
1208         int error;
1209         nvlist_t *list = NULL;
1210 
1211         /*
1212          * Read in and unpack the user-supplied nvlist.
1213          */
1214         if (size == 0)
1215                 return (EINVAL);
1216 
1217         packed = kmem_alloc(size, KM_SLEEP);
1218 
1219         if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1220             iflag)) != 0) {
1221                 kmem_free(packed, size);
1222                 return (error);
1223         }
1224 
1225         if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1226                 kmem_free(packed, size);
1227                 return (error);
1228         }
1229 
1230         kmem_free(packed, size);
1231 
1232         *nvp = list;
1233         return (0);
1234 }
1235 
1236 /*
1237  * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1238  * Entries will be removed from the end of the nvlist, and one int32 entry
1239  * named "N_MORE_ERRORS" will be added indicating how many entries were
1240  * removed.
1241  */
1242 static int
1243 nvlist_smush(nvlist_t *errors, size_t max)
1244 {
1245         size_t size;
1246 
1247         size = fnvlist_size(errors);
1248 
1249         if (size > max) {
1250                 nvpair_t *more_errors;
1251                 int n = 0;
1252 
1253                 if (max < 1024)
1254                         return (ENOMEM);
1255 
1256                 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1257                 more_errors = nvlist_prev_nvpair(errors, NULL);
1258 
1259                 do {
1260                         nvpair_t *pair = nvlist_prev_nvpair(errors,
1261                             more_errors);
1262                         fnvlist_remove_nvpair(errors, pair);
1263                         n++;
1264                         size = fnvlist_size(errors);
1265                 } while (size > max);
1266 
1267                 fnvlist_remove_nvpair(errors, more_errors);
1268                 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1269                 ASSERT3U(fnvlist_size(errors), <=, max);
1270         }
1271 
1272         return (0);
1273 }
1274 
1275 static int
1276 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1277 {
1278         char *packed = NULL;
1279         int error = 0;
1280         size_t size;
1281 
1282         size = fnvlist_size(nvl);
1283 
1284         if (size > zc->zc_nvlist_dst_size) {
1285                 error = ENOMEM;
1286         } else {
1287                 packed = fnvlist_pack(nvl, &size);
1288                 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1289                     size, zc->zc_iflags) != 0)
1290                         error = EFAULT;
1291                 fnvlist_pack_free(packed, size);
1292         }
1293 
1294         zc->zc_nvlist_dst_size = size;
1295         zc->zc_nvlist_dst_filled = B_TRUE;
1296         return (error);
1297 }
1298 
1299 static int
1300 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1301 {
1302         objset_t *os;
1303         int error;
1304 
1305         error = dmu_objset_hold(dsname, FTAG, &os);
1306         if (error)
1307                 return (error);
1308         if (dmu_objset_type(os) != DMU_OST_ZFS) {
1309                 dmu_objset_rele(os, FTAG);
1310                 return (EINVAL);
1311         }
1312 
1313         mutex_enter(&os->os_user_ptr_lock);
1314         *zfvp = dmu_objset_get_user(os);
1315         if (*zfvp) {
1316                 VFS_HOLD((*zfvp)->z_vfs);
1317         } else {
1318                 error = ESRCH;
1319         }
1320         mutex_exit(&os->os_user_ptr_lock);
1321         dmu_objset_rele(os, FTAG);
1322         return (error);
1323 }
1324 
1325 /*
1326  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1327  * case its z_vfs will be NULL, and it will be opened as the owner.
1328  * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1329  * which prevents all vnode ops from running.
1330  */
1331 static int
1332 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1333 {
1334         int error = 0;
1335 
1336         if (getzfsvfs(name, zfvp) != 0)
1337                 error = zfsvfs_create(name, zfvp);
1338         if (error == 0) {
1339                 rrw_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1340                     RW_READER, tag);
1341                 if ((*zfvp)->z_unmounted) {
1342                         /*
1343                          * XXX we could probably try again, since the unmounting
1344                          * thread should be just about to disassociate the
1345                          * objset from the zfsvfs.
1346                          */
1347                         rrw_exit(&(*zfvp)->z_teardown_lock, tag);
1348                         return (EBUSY);
1349                 }
1350         }
1351         return (error);
1352 }
1353 
1354 static void
1355 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1356 {
1357         rrw_exit(&zfsvfs->z_teardown_lock, tag);
1358 
1359         if (zfsvfs->z_vfs) {
1360                 VFS_RELE(zfsvfs->z_vfs);
1361         } else {
1362                 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1363                 zfsvfs_free(zfsvfs);
1364         }
1365 }
1366 
1367 static int
1368 zfs_ioc_pool_create(zfs_cmd_t *zc)
1369 {
1370         int error;
1371         nvlist_t *config, *props = NULL;
1372         nvlist_t *rootprops = NULL;
1373         nvlist_t *zplprops = NULL;
1374 
1375         if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1376             zc->zc_iflags, &config))
1377                 return (error);
1378 
1379         if (zc->zc_nvlist_src_size != 0 && (error =
1380             get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1381             zc->zc_iflags, &props))) {
1382                 nvlist_free(config);
1383                 return (error);
1384         }
1385 
1386         if (props) {
1387                 nvlist_t *nvl = NULL;
1388                 uint64_t version = SPA_VERSION;
1389 
1390                 (void) nvlist_lookup_uint64(props,
1391                     zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1392                 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1393                         error = EINVAL;
1394                         goto pool_props_bad;
1395                 }
1396                 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1397                 if (nvl) {
1398                         error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1399                         if (error != 0) {
1400                                 nvlist_free(config);
1401                                 nvlist_free(props);
1402                                 return (error);
1403                         }
1404                         (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1405                 }
1406                 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1407                 error = zfs_fill_zplprops_root(version, rootprops,
1408                     zplprops, NULL);
1409                 if (error)
1410                         goto pool_props_bad;
1411         }
1412 
1413         error = spa_create(zc->zc_name, config, props, zplprops);
1414 
1415         /*
1416          * Set the remaining root properties
1417          */
1418         if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1419             ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1420                 (void) spa_destroy(zc->zc_name);
1421 
1422 pool_props_bad:
1423         nvlist_free(rootprops);
1424         nvlist_free(zplprops);
1425         nvlist_free(config);
1426         nvlist_free(props);
1427 
1428         return (error);
1429 }
1430 
1431 static int
1432 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1433 {
1434         int error;
1435         zfs_log_history(zc);
1436         error = spa_destroy(zc->zc_name);
1437         if (error == 0)
1438                 zvol_remove_minors(zc->zc_name);
1439         return (error);
1440 }
1441 
1442 static int
1443 zfs_ioc_pool_import(zfs_cmd_t *zc)
1444 {
1445         nvlist_t *config, *props = NULL;
1446         uint64_t guid;
1447         int error;
1448 
1449         if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1450             zc->zc_iflags, &config)) != 0)
1451                 return (error);
1452 
1453         if (zc->zc_nvlist_src_size != 0 && (error =
1454             get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1455             zc->zc_iflags, &props))) {
1456                 nvlist_free(config);
1457                 return (error);
1458         }
1459 
1460         if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1461             guid != zc->zc_guid)
1462                 error = EINVAL;
1463         else
1464                 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1465 
1466         if (zc->zc_nvlist_dst != 0) {
1467                 int err;
1468 
1469                 if ((err = put_nvlist(zc, config)) != 0)
1470                         error = err;
1471         }
1472 
1473         nvlist_free(config);
1474 
1475         if (props)
1476                 nvlist_free(props);
1477 
1478         return (error);
1479 }
1480 
1481 static int
1482 zfs_ioc_pool_export(zfs_cmd_t *zc)
1483 {
1484         int error;
1485         boolean_t force = (boolean_t)zc->zc_cookie;
1486         boolean_t hardforce = (boolean_t)zc->zc_guid;
1487 
1488         zfs_log_history(zc);
1489         error = spa_export(zc->zc_name, NULL, force, hardforce);
1490         if (error == 0)
1491                 zvol_remove_minors(zc->zc_name);
1492         return (error);
1493 }
1494 
1495 static int
1496 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1497 {
1498         nvlist_t *configs;
1499         int error;
1500 
1501         if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1502                 return (EEXIST);
1503 
1504         error = put_nvlist(zc, configs);
1505 
1506         nvlist_free(configs);
1507 
1508         return (error);
1509 }
1510 
1511 /*
1512  * inputs:
1513  * zc_name              name of the pool
1514  *
1515  * outputs:
1516  * zc_cookie            real errno
1517  * zc_nvlist_dst        config nvlist
1518  * zc_nvlist_dst_size   size of config nvlist
1519  */
1520 static int
1521 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1522 {
1523         nvlist_t *config;
1524         int error;
1525         int ret = 0;
1526 
1527         error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1528             sizeof (zc->zc_value));
1529 
1530         if (config != NULL) {
1531                 ret = put_nvlist(zc, config);
1532                 nvlist_free(config);
1533 
1534                 /*
1535                  * The config may be present even if 'error' is non-zero.
1536                  * In this case we return success, and preserve the real errno
1537                  * in 'zc_cookie'.
1538                  */
1539                 zc->zc_cookie = error;
1540         } else {
1541                 ret = error;
1542         }
1543 
1544         return (ret);
1545 }
1546 
1547 /*
1548  * Try to import the given pool, returning pool stats as appropriate so that
1549  * user land knows which devices are available and overall pool health.
1550  */
1551 static int
1552 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1553 {
1554         nvlist_t *tryconfig, *config;
1555         int error;
1556 
1557         if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1558             zc->zc_iflags, &tryconfig)) != 0)
1559                 return (error);
1560 
1561         config = spa_tryimport(tryconfig);
1562 
1563         nvlist_free(tryconfig);
1564 
1565         if (config == NULL)
1566                 return (EINVAL);
1567 
1568         error = put_nvlist(zc, config);
1569         nvlist_free(config);
1570 
1571         return (error);
1572 }
1573 
1574 /*
1575  * inputs:
1576  * zc_name              name of the pool
1577  * zc_cookie            scan func (pool_scan_func_t)
1578  */
1579 static int
1580 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1581 {
1582         spa_t *spa;
1583         int error;
1584 
1585         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1586                 return (error);
1587 
1588         if (zc->zc_cookie == POOL_SCAN_NONE)
1589                 error = spa_scan_stop(spa);
1590         else
1591                 error = spa_scan(spa, zc->zc_cookie);
1592 
1593         spa_close(spa, FTAG);
1594 
1595         return (error);
1596 }
1597 
1598 static int
1599 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1600 {
1601         spa_t *spa;
1602         int error;
1603 
1604         error = spa_open(zc->zc_name, &spa, FTAG);
1605         if (error == 0) {
1606                 spa_freeze(spa);
1607                 spa_close(spa, FTAG);
1608         }
1609         return (error);
1610 }
1611 
1612 static int
1613 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1614 {
1615         spa_t *spa;
1616         int error;
1617 
1618         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1619                 return (error);
1620 
1621         if (zc->zc_cookie < spa_version(spa) ||
1622             !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1623                 spa_close(spa, FTAG);
1624                 return (EINVAL);
1625         }
1626 
1627         spa_upgrade(spa, zc->zc_cookie);
1628         spa_close(spa, FTAG);
1629 
1630         return (error);
1631 }
1632 
1633 static int
1634 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1635 {
1636         spa_t *spa;
1637         char *hist_buf;
1638         uint64_t size;
1639         int error;
1640 
1641         if ((size = zc->zc_history_len) == 0)
1642                 return (EINVAL);
1643 
1644         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1645                 return (error);
1646 
1647         if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1648                 spa_close(spa, FTAG);
1649                 return (ENOTSUP);
1650         }
1651 
1652         hist_buf = kmem_alloc(size, KM_SLEEP);
1653         if ((error = spa_history_get(spa, &zc->zc_history_offset,
1654             &zc->zc_history_len, hist_buf)) == 0) {
1655                 error = ddi_copyout(hist_buf,
1656                     (void *)(uintptr_t)zc->zc_history,
1657                     zc->zc_history_len, zc->zc_iflags);
1658         }
1659 
1660         spa_close(spa, FTAG);
1661         kmem_free(hist_buf, size);
1662         return (error);
1663 }
1664 
1665 static int
1666 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1667 {
1668         spa_t *spa;
1669         int error;
1670 
1671         error = spa_open(zc->zc_name, &spa, FTAG);
1672         if (error == 0) {
1673                 error = spa_change_guid(spa);
1674                 spa_close(spa, FTAG);
1675         }
1676         return (error);
1677 }
1678 
1679 static int
1680 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1681 {
1682         int error;
1683 
1684         if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
1685                 return (error);
1686 
1687         return (0);
1688 }
1689 
1690 /*
1691  * inputs:
1692  * zc_name              name of filesystem
1693  * zc_obj               object to find
1694  *
1695  * outputs:
1696  * zc_value             name of object
1697  */
1698 static int
1699 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1700 {
1701         objset_t *os;
1702         int error;
1703 
1704         /* XXX reading from objset not owned */
1705         if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1706                 return (error);
1707         if (dmu_objset_type(os) != DMU_OST_ZFS) {
1708                 dmu_objset_rele(os, FTAG);
1709                 return (EINVAL);
1710         }
1711         error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1712             sizeof (zc->zc_value));
1713         dmu_objset_rele(os, FTAG);
1714 
1715         return (error);
1716 }
1717 
1718 /*
1719  * inputs:
1720  * zc_name              name of filesystem
1721  * zc_obj               object to find
1722  *
1723  * outputs:
1724  * zc_stat              stats on object
1725  * zc_value             path to object
1726  */
1727 static int
1728 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1729 {
1730         objset_t *os;
1731         int error;
1732 
1733         /* XXX reading from objset not owned */
1734         if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1735                 return (error);
1736         if (dmu_objset_type(os) != DMU_OST_ZFS) {
1737                 dmu_objset_rele(os, FTAG);
1738                 return (EINVAL);
1739         }
1740         error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1741             sizeof (zc->zc_value));
1742         dmu_objset_rele(os, FTAG);
1743 
1744         return (error);
1745 }
1746 
1747 static int
1748 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1749 {
1750         spa_t *spa;
1751         int error;
1752         nvlist_t *config, **l2cache, **spares;
1753         uint_t nl2cache = 0, nspares = 0;
1754 
1755         error = spa_open(zc->zc_name, &spa, FTAG);
1756         if (error != 0)
1757                 return (error);
1758 
1759         error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1760             zc->zc_iflags, &config);
1761         (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1762             &l2cache, &nl2cache);
1763 
1764         (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1765             &spares, &nspares);
1766 
1767         /*
1768          * A root pool with concatenated devices is not supported.
1769          * Thus, can not add a device to a root pool.
1770          *
1771          * Intent log device can not be added to a rootpool because
1772          * during mountroot, zil is replayed, a seperated log device
1773          * can not be accessed during the mountroot time.
1774          *
1775          * l2cache and spare devices are ok to be added to a rootpool.
1776          */
1777         if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1778                 nvlist_free(config);
1779                 spa_close(spa, FTAG);
1780                 return (EDOM);
1781         }
1782 
1783         if (error == 0) {
1784                 error = spa_vdev_add(spa, config);
1785                 nvlist_free(config);
1786         }
1787         spa_close(spa, FTAG);
1788         return (error);
1789 }
1790 
1791 /*
1792  * inputs:
1793  * zc_name              name of the pool
1794  * zc_nvlist_conf       nvlist of devices to remove
1795  * zc_cookie            to stop the remove?
1796  */
1797 static int
1798 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1799 {
1800         spa_t *spa;
1801         int error;
1802 
1803         error = spa_open(zc->zc_name, &spa, FTAG);
1804         if (error != 0)
1805                 return (error);
1806         error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1807         spa_close(spa, FTAG);
1808         return (error);
1809 }
1810 
1811 static int
1812 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1813 {
1814         spa_t *spa;
1815         int error;
1816         vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1817 
1818         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1819                 return (error);
1820         switch (zc->zc_cookie) {
1821         case VDEV_STATE_ONLINE:
1822                 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1823                 break;
1824 
1825         case VDEV_STATE_OFFLINE:
1826                 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1827                 break;
1828 
1829         case VDEV_STATE_FAULTED:
1830                 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1831                     zc->zc_obj != VDEV_AUX_EXTERNAL)
1832                         zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1833 
1834                 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1835                 break;
1836 
1837         case VDEV_STATE_DEGRADED:
1838                 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1839                     zc->zc_obj != VDEV_AUX_EXTERNAL)
1840                         zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1841 
1842                 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1843                 break;
1844 
1845         default:
1846                 error = EINVAL;
1847         }
1848         zc->zc_cookie = newstate;
1849         spa_close(spa, FTAG);
1850         return (error);
1851 }
1852 
1853 static int
1854 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1855 {
1856         spa_t *spa;
1857         int replacing = zc->zc_cookie;
1858         nvlist_t *config;
1859         int error;
1860 
1861         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1862                 return (error);
1863 
1864         if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1865             zc->zc_iflags, &config)) == 0) {
1866                 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1867                 nvlist_free(config);
1868         }
1869 
1870         spa_close(spa, FTAG);
1871         return (error);
1872 }
1873 
1874 static int
1875 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1876 {
1877         spa_t *spa;
1878         int error;
1879 
1880         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1881                 return (error);
1882 
1883         error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1884 
1885         spa_close(spa, FTAG);
1886         return (error);
1887 }
1888 
1889 static int
1890 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1891 {
1892         spa_t *spa;
1893         nvlist_t *config, *props = NULL;
1894         int error;
1895         boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1896 
1897         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1898                 return (error);
1899 
1900         if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1901             zc->zc_iflags, &config)) {
1902                 spa_close(spa, FTAG);
1903                 return (error);
1904         }
1905 
1906         if (zc->zc_nvlist_src_size != 0 && (error =
1907             get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1908             zc->zc_iflags, &props))) {
1909                 spa_close(spa, FTAG);
1910                 nvlist_free(config);
1911                 return (error);
1912         }
1913 
1914         error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1915 
1916         spa_close(spa, FTAG);
1917 
1918         nvlist_free(config);
1919         nvlist_free(props);
1920 
1921         return (error);
1922 }
1923 
1924 static int
1925 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1926 {
1927         spa_t *spa;
1928         char *path = zc->zc_value;
1929         uint64_t guid = zc->zc_guid;
1930         int error;
1931 
1932         error = spa_open(zc->zc_name, &spa, FTAG);
1933         if (error != 0)
1934                 return (error);
1935 
1936         error = spa_vdev_setpath(spa, guid, path);
1937         spa_close(spa, FTAG);
1938         return (error);
1939 }
1940 
1941 static int
1942 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
1943 {
1944         spa_t *spa;
1945         char *fru = zc->zc_value;
1946         uint64_t guid = zc->zc_guid;
1947         int error;
1948 
1949         error = spa_open(zc->zc_name, &spa, FTAG);
1950         if (error != 0)
1951                 return (error);
1952 
1953         error = spa_vdev_setfru(spa, guid, fru);
1954         spa_close(spa, FTAG);
1955         return (error);
1956 }
1957 
1958 static int
1959 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
1960 {
1961         int error = 0;
1962         nvlist_t *nv;
1963 
1964         dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1965 
1966         if (zc->zc_nvlist_dst != 0 &&
1967             (error = dsl_prop_get_all(os, &nv)) == 0) {
1968                 dmu_objset_stats(os, nv);
1969                 /*
1970                  * NB: zvol_get_stats() will read the objset contents,
1971                  * which we aren't supposed to do with a
1972                  * DS_MODE_USER hold, because it could be
1973                  * inconsistent.  So this is a bit of a workaround...
1974                  * XXX reading with out owning
1975                  */
1976                 if (!zc->zc_objset_stats.dds_inconsistent &&
1977                     dmu_objset_type(os) == DMU_OST_ZVOL) {
1978                         error = zvol_get_stats(os, nv);
1979                         if (error == EIO)
1980                                 return (error);
1981                         VERIFY3S(error, ==, 0);
1982                 }
1983                 error = put_nvlist(zc, nv);
1984                 nvlist_free(nv);
1985         }
1986 
1987         return (error);
1988 }
1989 
1990 /*
1991  * inputs:
1992  * zc_name              name of filesystem
1993  * zc_nvlist_dst_size   size of buffer for property nvlist
1994  *
1995  * outputs:
1996  * zc_objset_stats      stats
1997  * zc_nvlist_dst        property nvlist
1998  * zc_nvlist_dst_size   size of property nvlist
1999  */
2000 static int
2001 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2002 {
2003         objset_t *os = NULL;
2004         int error;
2005 
2006         if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
2007                 return (error);
2008 
2009         error = zfs_ioc_objset_stats_impl(zc, os);
2010 
2011         dmu_objset_rele(os, FTAG);
2012 
2013         return (error);
2014 }
2015 
2016 /*
2017  * inputs:
2018  * zc_name              name of filesystem
2019  * zc_nvlist_dst_size   size of buffer for property nvlist
2020  *
2021  * outputs:
2022  * zc_nvlist_dst        received property nvlist
2023  * zc_nvlist_dst_size   size of received property nvlist
2024  *
2025  * Gets received properties (distinct from local properties on or after
2026  * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2027  * local property values.
2028  */
2029 static int
2030 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2031 {
2032         objset_t *os = NULL;
2033         int error;
2034         nvlist_t *nv;
2035 
2036         if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
2037                 return (error);
2038 
2039         /*
2040          * Without this check, we would return local property values if the
2041          * caller has not already received properties on or after
2042          * SPA_VERSION_RECVD_PROPS.
2043          */
2044         if (!dsl_prop_get_hasrecvd(os)) {
2045                 dmu_objset_rele(os, FTAG);
2046                 return (ENOTSUP);
2047         }
2048 
2049         if (zc->zc_nvlist_dst != 0 &&
2050             (error = dsl_prop_get_received(os, &nv)) == 0) {
2051                 error = put_nvlist(zc, nv);
2052                 nvlist_free(nv);
2053         }
2054 
2055         dmu_objset_rele(os, FTAG);
2056         return (error);
2057 }
2058 
2059 static int
2060 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2061 {
2062         uint64_t value;
2063         int error;
2064 
2065         /*
2066          * zfs_get_zplprop() will either find a value or give us
2067          * the default value (if there is one).
2068          */
2069         if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2070                 return (error);
2071         VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2072         return (0);
2073 }
2074 
2075 /*
2076  * inputs:
2077  * zc_name              name of filesystem
2078  * zc_nvlist_dst_size   size of buffer for zpl property nvlist
2079  *
2080  * outputs:
2081  * zc_nvlist_dst        zpl property nvlist
2082  * zc_nvlist_dst_size   size of zpl property nvlist
2083  */
2084 static int
2085 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2086 {
2087         objset_t *os;
2088         int err;
2089 
2090         /* XXX reading without owning */
2091         if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2092                 return (err);
2093 
2094         dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2095 
2096         /*
2097          * NB: nvl_add_zplprop() will read the objset contents,
2098          * which we aren't supposed to do with a DS_MODE_USER
2099          * hold, because it could be inconsistent.
2100          */
2101         if (zc->zc_nvlist_dst != NULL &&
2102             !zc->zc_objset_stats.dds_inconsistent &&
2103             dmu_objset_type(os) == DMU_OST_ZFS) {
2104                 nvlist_t *nv;
2105 
2106                 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2107                 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2108                     (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2109                     (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2110                     (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2111                         err = put_nvlist(zc, nv);
2112                 nvlist_free(nv);
2113         } else {
2114                 err = ENOENT;
2115         }
2116         dmu_objset_rele(os, FTAG);
2117         return (err);
2118 }
2119 
2120 static boolean_t
2121 dataset_name_hidden(const char *name)
2122 {
2123         /*
2124          * Skip over datasets that are not visible in this zone,
2125          * internal datasets (which have a $ in their name), and
2126          * temporary datasets (which have a % in their name).
2127          */
2128         if (strchr(name, '$') != NULL)
2129                 return (B_TRUE);
2130         if (strchr(name, '%') != NULL)
2131                 return (B_TRUE);
2132         if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
2133                 return (B_TRUE);
2134         return (B_FALSE);
2135 }
2136 
2137 /*
2138  * inputs:
2139  * zc_name              name of filesystem
2140  * zc_cookie            zap cursor
2141  * zc_nvlist_dst_size   size of buffer for property nvlist
2142  *
2143  * outputs:
2144  * zc_name              name of next filesystem
2145  * zc_cookie            zap cursor
2146  * zc_objset_stats      stats
2147  * zc_nvlist_dst        property nvlist
2148  * zc_nvlist_dst_size   size of property nvlist
2149  */
2150 static int
2151 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2152 {
2153         objset_t *os;
2154         int error;
2155         char *p;
2156         size_t orig_len = strlen(zc->zc_name);
2157 
2158 top:
2159         if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
2160                 if (error == ENOENT)
2161                         error = ESRCH;
2162                 return (error);
2163         }
2164 
2165         p = strrchr(zc->zc_name, '/');
2166         if (p == NULL || p[1] != '\0')
2167                 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2168         p = zc->zc_name + strlen(zc->zc_name);
2169 
2170         /*
2171          * Pre-fetch the datasets.  dmu_objset_prefetch() always returns 0
2172          * but is not declared void because its called by dmu_objset_find().
2173          */
2174         if (zc->zc_cookie == 0) {
2175                 uint64_t cookie = 0;
2176                 int len = sizeof (zc->zc_name) - (p - zc->zc_name);
2177 
2178                 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) {
2179                         if (!dataset_name_hidden(zc->zc_name))
2180                                 (void) dmu_objset_prefetch(zc->zc_name, NULL);
2181                 }
2182         }
2183 
2184         do {
2185                 error = dmu_dir_list_next(os,
2186                     sizeof (zc->zc_name) - (p - zc->zc_name), p,
2187                     NULL, &zc->zc_cookie);
2188                 if (error == ENOENT)
2189                         error = ESRCH;
2190         } while (error == 0 && dataset_name_hidden(zc->zc_name));
2191         dmu_objset_rele(os, FTAG);
2192 
2193         /*
2194          * If it's an internal dataset (ie. with a '$' in its name),
2195          * don't try to get stats for it, otherwise we'll return ENOENT.
2196          */
2197         if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2198                 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2199                 if (error == ENOENT) {
2200                         /* We lost a race with destroy, get the next one. */
2201                         zc->zc_name[orig_len] = '\0';
2202                         goto top;
2203                 }
2204         }
2205         return (error);
2206 }
2207 
2208 /*
2209  * inputs:
2210  * zc_name              name of filesystem
2211  * zc_cookie            zap cursor
2212  * zc_nvlist_dst_size   size of buffer for property nvlist
2213  *
2214  * outputs:
2215  * zc_name              name of next snapshot
2216  * zc_objset_stats      stats
2217  * zc_nvlist_dst        property nvlist
2218  * zc_nvlist_dst_size   size of property nvlist
2219  */
2220 static int
2221 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2222 {
2223         objset_t *os;
2224         int error;
2225 
2226 top:
2227         if (zc->zc_cookie == 0)
2228                 (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
2229                     NULL, DS_FIND_SNAPSHOTS);
2230 
2231         error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2232         if (error)
2233                 return (error == ENOENT ? ESRCH : error);
2234 
2235         /*
2236          * A dataset name of maximum length cannot have any snapshots,
2237          * so exit immediately.
2238          */
2239         if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2240                 dmu_objset_rele(os, FTAG);
2241                 return (ESRCH);
2242         }
2243 
2244         error = dmu_snapshot_list_next(os,
2245             sizeof (zc->zc_name) - strlen(zc->zc_name),
2246             zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2247             NULL);
2248 
2249         if (error == 0) {
2250                 dsl_dataset_t *ds;
2251                 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2252 
2253                 /*
2254                  * Since we probably don't have a hold on this snapshot,
2255                  * it's possible that the objsetid could have been destroyed
2256                  * and reused for a new objset. It's OK if this happens during
2257                  * a zfs send operation, since the new createtxg will be
2258                  * beyond the range we're interested in.
2259                  */
2260                 rw_enter(&dp->dp_config_rwlock, RW_READER);
2261                 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2262                 rw_exit(&dp->dp_config_rwlock);
2263                 if (error) {
2264                         if (error == ENOENT) {
2265                                 /* Racing with destroy, get the next one. */
2266                                 *strchr(zc->zc_name, '@') = '\0';
2267                                 dmu_objset_rele(os, FTAG);
2268                                 goto top;
2269                         }
2270                 } else {
2271                         objset_t *ossnap;
2272 
2273                         error = dmu_objset_from_ds(ds, &ossnap);
2274                         if (error == 0)
2275                                 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2276                         dsl_dataset_rele(ds, FTAG);
2277                 }
2278         } else if (error == ENOENT) {
2279                 error = ESRCH;
2280         }
2281 
2282         dmu_objset_rele(os, FTAG);
2283         /* if we failed, undo the @ that we tacked on to zc_name */
2284         if (error)
2285                 *strchr(zc->zc_name, '@') = '\0';
2286         return (error);
2287 }
2288 
2289 static int
2290 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2291 {
2292         const char *propname = nvpair_name(pair);
2293         uint64_t *valary;
2294         unsigned int vallen;
2295         const char *domain;
2296         char *dash;
2297         zfs_userquota_prop_t type;
2298         uint64_t rid;
2299         uint64_t quota;
2300         zfsvfs_t *zfsvfs;
2301         int err;
2302 
2303         if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2304                 nvlist_t *attrs;
2305                 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2306                 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2307                     &pair) != 0)
2308                         return (EINVAL);
2309         }
2310 
2311         /*
2312          * A correctly constructed propname is encoded as
2313          * userquota@<rid>-<domain>.
2314          */
2315         if ((dash = strchr(propname, '-')) == NULL ||
2316             nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2317             vallen != 3)
2318                 return (EINVAL);
2319 
2320         domain = dash + 1;
2321         type = valary[0];
2322         rid = valary[1];
2323         quota = valary[2];
2324 
2325         err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2326         if (err == 0) {
2327                 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2328                 zfsvfs_rele(zfsvfs, FTAG);
2329         }
2330 
2331         return (err);
2332 }
2333 
2334 /*
2335  * If the named property is one that has a special function to set its value,
2336  * return 0 on success and a positive error code on failure; otherwise if it is
2337  * not one of the special properties handled by this function, return -1.
2338  *
2339  * XXX: It would be better for callers of the property interface if we handled
2340  * these special cases in dsl_prop.c (in the dsl layer).
2341  */
2342 static int
2343 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2344     nvpair_t *pair)
2345 {
2346         const char *propname = nvpair_name(pair);
2347         zfs_prop_t prop = zfs_name_to_prop(propname);
2348         uint64_t intval;
2349         int err;
2350 
2351         if (prop == ZPROP_INVAL) {
2352                 if (zfs_prop_userquota(propname))
2353                         return (zfs_prop_set_userquota(dsname, pair));
2354                 return (-1);
2355         }
2356 
2357         if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2358                 nvlist_t *attrs;
2359                 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2360                 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2361                     &pair) == 0);
2362         }
2363 
2364         if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2365                 return (-1);
2366 
2367         VERIFY(0 == nvpair_value_uint64(pair, &intval));
2368 
2369         switch (prop) {
2370         case ZFS_PROP_QUOTA:
2371                 err = dsl_dir_set_quota(dsname, source, intval);
2372                 break;
2373         case ZFS_PROP_REFQUOTA:
2374                 err = dsl_dataset_set_quota(dsname, source, intval);
2375                 break;
2376         case ZFS_PROP_RESERVATION:
2377                 err = dsl_dir_set_reservation(dsname, source, intval);
2378                 break;
2379         case ZFS_PROP_REFRESERVATION:
2380                 err = dsl_dataset_set_reservation(dsname, source, intval);
2381                 break;
2382         case ZFS_PROP_VOLSIZE:
2383                 err = zvol_set_volsize(dsname, ddi_driver_major(zfs_dip),
2384                     intval);
2385                 break;
2386         case ZFS_PROP_VERSION:
2387         {
2388                 zfsvfs_t *zfsvfs;
2389 
2390                 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2391                         break;
2392 
2393                 err = zfs_set_version(zfsvfs, intval);
2394                 zfsvfs_rele(zfsvfs, FTAG);
2395 
2396                 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2397                         zfs_cmd_t *zc;
2398 
2399                         zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2400                         (void) strcpy(zc->zc_name, dsname);
2401                         (void) zfs_ioc_userspace_upgrade(zc);
2402                         kmem_free(zc, sizeof (zfs_cmd_t));
2403                 }
2404                 break;
2405         }
2406 
2407         default:
2408                 err = -1;
2409         }
2410 
2411         return (err);
2412 }
2413 
2414 /*
2415  * This function is best effort. If it fails to set any of the given properties,
2416  * it continues to set as many as it can and returns the last error
2417  * encountered. If the caller provides a non-NULL errlist, it will be filled in
2418  * with the list of names of all the properties that failed along with the
2419  * corresponding error numbers.
2420  *
2421  * If every property is set successfully, zero is returned and errlist is not
2422  * modified.
2423  */
2424 int
2425 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2426     nvlist_t *errlist)
2427 {
2428         nvpair_t *pair;
2429         nvpair_t *propval;
2430         int rv = 0;
2431         uint64_t intval;
2432         char *strval;
2433         nvlist_t *genericnvl = fnvlist_alloc();
2434         nvlist_t *retrynvl = fnvlist_alloc();
2435         zfsvfs_t *zfsvfs;
2436         boolean_t set_worm = B_FALSE;
2437 
2438 retry:
2439         pair = NULL;
2440         while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2441                 const char *propname = nvpair_name(pair);
2442                 zfs_prop_t prop = zfs_name_to_prop(propname);
2443                 int err = 0;
2444 
2445                 if (!set_worm && (strcmp(propname, "nms:worm") == 0)) {
2446                         set_worm = B_TRUE;
2447                 }
2448 
2449                 cmn_err(CE_NOTE, "The property is %s\n", propname);
2450                 /* decode the property value */
2451                 propval = pair;
2452                 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2453                         nvlist_t *attrs;
2454                         attrs = fnvpair_value_nvlist(pair);
2455                         if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2456                             &propval) != 0)
2457                                 err = EINVAL;
2458                 }
2459 
2460                 /* Validate value type */
2461                 if (err == 0 && prop == ZPROP_INVAL) {
2462                         if (zfs_prop_user(propname)) {
2463                                 if (nvpair_type(propval) != DATA_TYPE_STRING)
2464                                         err = EINVAL;
2465                         } else if (zfs_prop_userquota(propname)) {
2466                                 if (nvpair_type(propval) !=
2467                                     DATA_TYPE_UINT64_ARRAY)
2468                                         err = EINVAL;
2469                         } else {
2470                                 err = EINVAL;
2471                         }
2472                 } else if (err == 0) {
2473                         if (nvpair_type(propval) == DATA_TYPE_STRING) {
2474                                 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2475                                         err = EINVAL;
2476                         } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2477                                 const char *unused;
2478 
2479                                 intval = fnvpair_value_uint64(propval);
2480 
2481                                 switch (zfs_prop_get_type(prop)) {
2482                                 case PROP_TYPE_NUMBER:
2483                                         break;
2484                                 case PROP_TYPE_STRING:
2485                                         err = EINVAL;
2486                                         break;
2487                                 case PROP_TYPE_INDEX:
2488                                         if (zfs_prop_index_to_string(prop,
2489                                             intval, &unused) != 0)
2490                                                 err = EINVAL;
2491                                         break;
2492                                 default:
2493                                         cmn_err(CE_PANIC,
2494                                             "unknown property type");
2495                                 }
2496                         } else {
2497                                 err = EINVAL;
2498                         }
2499                 }
2500 
2501                 /* Validate permissions */
2502                 if (err == 0)
2503                         err = zfs_check_settable(dsname, pair, CRED());
2504 
2505                 if (err == 0) {
2506                         err = zfs_prop_set_special(dsname, source, pair);
2507                         if (err == -1) {
2508                                 /*
2509                                  * For better performance we build up a list of
2510                                  * properties to set in a single transaction.
2511                                  */
2512                                 err = nvlist_add_nvpair(genericnvl, pair);
2513                         } else if (err != 0 && nvl != retrynvl) {
2514                                 /*
2515                                  * This may be a spurious error caused by
2516                                  * receiving quota and reservation out of order.
2517                                  * Try again in a second pass.
2518                                  */
2519                                 err = nvlist_add_nvpair(retrynvl, pair);
2520                         }
2521                 }
2522 
2523                 if (err != 0) {
2524                         if (errlist != NULL)
2525                                 fnvlist_add_int32(errlist, propname, err);
2526                         rv = err;
2527                 }
2528         }
2529 
2530         if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2531                 nvl = retrynvl;
2532                 goto retry;
2533         }
2534 
2535         if (!nvlist_empty(genericnvl) &&
2536             dsl_props_set(dsname, source, genericnvl) != 0) {
2537                 /*
2538                  * If this fails, we still want to set as many properties as we
2539                  * can, so try setting them individually.
2540                  */
2541                 pair = NULL;
2542                 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2543                         const char *propname = nvpair_name(pair);
2544                         int err = 0;
2545 
2546                         propval = pair;
2547                         if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2548                                 nvlist_t *attrs;
2549                                 attrs = fnvpair_value_nvlist(pair);
2550                                 propval = fnvlist_lookup_nvpair(attrs,
2551                                     ZPROP_VALUE);
2552                         }
2553 
2554                         if (nvpair_type(propval) == DATA_TYPE_STRING) {
2555                                 strval = fnvpair_value_string(propval);
2556                                 err = dsl_prop_set(dsname, propname, source, 1,
2557                                     strlen(strval) + 1, strval);
2558                         } else {
2559                                 intval = fnvpair_value_uint64(propval);
2560                                 err = dsl_prop_set(dsname, propname, source, 8,
2561                                     1, &intval);
2562                         }
2563 
2564                         if (err != 0) {
2565                                 if (errlist != NULL) {
2566                                         fnvlist_add_int32(errlist, propname,
2567                                             err);
2568                                 }
2569                                 rv = err;
2570                         }
2571                 }
2572         }
2573         nvlist_free(genericnvl);
2574         nvlist_free(retrynvl);
2575 
2576         if (set_worm && getzfsvfs(dsname, &zfsvfs) == 0) {
2577                 if (zfs_is_wormed(dsname)) {
2578                         zfsvfs->z_isworm = B_TRUE;
2579                 } else {
2580                         zfsvfs->z_isworm = B_FALSE;
2581                 }
2582                 VFS_RELE(zfsvfs->z_vfs);
2583         }
2584 
2585         return (rv);
2586 }
2587 
2588 /*
2589  * Check that all the properties are valid user properties.
2590  */
2591 static int
2592 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2593 {
2594         nvpair_t *pair = NULL;
2595         int error = 0;
2596 
2597         while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2598                 const char *propname = nvpair_name(pair);
2599                 char *valstr;
2600 
2601                 if (!zfs_prop_user(propname) ||
2602                     nvpair_type(pair) != DATA_TYPE_STRING)
2603                         return (EINVAL);
2604 
2605                 if (error = zfs_secpolicy_write_perms(fsname,
2606                     ZFS_DELEG_PERM_USERPROP, CRED()))
2607                         return (error);
2608 
2609                 if (strlen(propname) >= ZAP_MAXNAMELEN)
2610                         return (ENAMETOOLONG);
2611 
2612                 VERIFY(nvpair_value_string(pair, &valstr) == 0);
2613                 if (strlen(valstr) >= ZAP_MAXVALUELEN)
2614                         return (E2BIG);
2615         }
2616         return (0);
2617 }
2618 
2619 static void
2620 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2621 {
2622         nvpair_t *pair;
2623 
2624         VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2625 
2626         pair = NULL;
2627         while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2628                 if (nvlist_exists(skipped, nvpair_name(pair)))
2629                         continue;
2630 
2631                 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2632         }
2633 }
2634 
2635 static int
2636 clear_received_props(objset_t *os, const char *fs, nvlist_t *props,
2637     nvlist_t *skipped)
2638 {
2639         int err = 0;
2640         nvlist_t *cleared_props = NULL;
2641         props_skip(props, skipped, &cleared_props);
2642         if (!nvlist_empty(cleared_props)) {
2643                 /*
2644                  * Acts on local properties until the dataset has received
2645                  * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2646                  */
2647                 zprop_source_t flags = (ZPROP_SRC_NONE |
2648                     (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0));
2649                 err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL);
2650         }
2651         nvlist_free(cleared_props);
2652         return (err);
2653 }
2654 
2655 /*
2656  * inputs:
2657  * zc_name              name of filesystem
2658  * zc_value             name of property to set
2659  * zc_nvlist_src{_size} nvlist of properties to apply
2660  * zc_cookie            received properties flag
2661  *
2662  * outputs:
2663  * zc_nvlist_dst{_size} error for each unapplied received property
2664  */
2665 static int
2666 zfs_ioc_set_prop(zfs_cmd_t *zc)
2667 {
2668         nvlist_t *nvl;
2669         boolean_t received = zc->zc_cookie;
2670         zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2671             ZPROP_SRC_LOCAL);
2672         nvlist_t *errors;
2673         int error;
2674 
2675         if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2676             zc->zc_iflags, &nvl)) != 0)
2677                 return (error);
2678 
2679         if (received) {
2680                 nvlist_t *origprops;
2681                 objset_t *os;
2682 
2683                 if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
2684                         if (dsl_prop_get_received(os, &origprops) == 0) {
2685                                 (void) clear_received_props(os,
2686                                     zc->zc_name, origprops, nvl);
2687                                 nvlist_free(origprops);
2688                         }
2689 
2690                         dsl_prop_set_hasrecvd(os);
2691                         dmu_objset_rele(os, FTAG);
2692                 }
2693         }
2694 
2695         errors = fnvlist_alloc();
2696         error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2697 
2698         if (zc->zc_nvlist_dst != NULL && errors != NULL) {
2699                 (void) put_nvlist(zc, errors);
2700         }
2701 
2702         nvlist_free(errors);
2703         nvlist_free(nvl);
2704         return (error);
2705 }
2706 
2707 /*
2708  * inputs:
2709  * zc_name              name of filesystem
2710  * zc_value             name of property to inherit
2711  * zc_cookie            revert to received value if TRUE
2712  *
2713  * outputs:             none
2714  */
2715 static int
2716 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2717 {
2718         const char *propname = zc->zc_value;
2719         zfs_prop_t prop = zfs_name_to_prop(propname);
2720         boolean_t received = zc->zc_cookie;
2721         zprop_source_t source = (received
2722             ? ZPROP_SRC_NONE            /* revert to received value, if any */
2723             : ZPROP_SRC_INHERITED);     /* explicitly inherit */
2724 
2725         if (received) {
2726                 nvlist_t *dummy;
2727                 nvpair_t *pair;
2728                 zprop_type_t type;
2729                 int err;
2730 
2731                 /*
2732                  * zfs_prop_set_special() expects properties in the form of an
2733                  * nvpair with type info.
2734                  */
2735                 if (prop == ZPROP_INVAL) {
2736                         if (!zfs_prop_user(propname))
2737                                 return (EINVAL);
2738 
2739                         type = PROP_TYPE_STRING;
2740                 } else if (prop == ZFS_PROP_VOLSIZE ||
2741                     prop == ZFS_PROP_VERSION) {
2742                         return (EINVAL);
2743                 } else {
2744                         type = zfs_prop_get_type(prop);
2745                 }
2746 
2747                 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2748 
2749                 switch (type) {
2750                 case PROP_TYPE_STRING:
2751                         VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2752                         break;
2753                 case PROP_TYPE_NUMBER:
2754                 case PROP_TYPE_INDEX:
2755                         VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2756                         break;
2757                 default:
2758                         nvlist_free(dummy);
2759                         return (EINVAL);
2760                 }
2761 
2762                 pair = nvlist_next_nvpair(dummy, NULL);
2763                 err = zfs_prop_set_special(zc->zc_name, source, pair);
2764                 nvlist_free(dummy);
2765                 if (err != -1)
2766                         return (err); /* special property already handled */
2767         } else {
2768                 /*
2769                  * Only check this in the non-received case. We want to allow
2770                  * 'inherit -S' to revert non-inheritable properties like quota
2771                  * and reservation to the received or default values even though
2772                  * they are not considered inheritable.
2773                  */
2774                 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2775                         return (EINVAL);
2776         }
2777 
2778         /* property name has been validated by zfs_secpolicy_inherit_prop() */
2779         return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL));
2780 }
2781 
2782 static int
2783 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2784 {
2785         nvlist_t *props;
2786         spa_t *spa;
2787         int error;
2788         nvpair_t *pair;
2789 
2790         if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2791             zc->zc_iflags, &props))
2792                 return (error);
2793 
2794         /*
2795          * If the only property is the configfile, then just do a spa_lookup()
2796          * to handle the faulted case.
2797          */
2798         pair = nvlist_next_nvpair(props, NULL);
2799         if (pair != NULL && strcmp(nvpair_name(pair),
2800             zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2801             nvlist_next_nvpair(props, pair) == NULL) {
2802                 mutex_enter(&spa_namespace_lock);
2803                 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2804                         spa_configfile_set(spa, props, B_FALSE);
2805                         spa_config_sync(spa, B_FALSE, B_TRUE);
2806                 }
2807                 mutex_exit(&spa_namespace_lock);
2808                 if (spa != NULL) {
2809                         nvlist_free(props);
2810                         return (0);
2811                 }
2812         }
2813 
2814         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2815                 nvlist_free(props);
2816                 return (error);
2817         }
2818 
2819         error = spa_prop_set(spa, props);
2820 
2821         nvlist_free(props);
2822         spa_close(spa, FTAG);
2823 
2824         return (error);
2825 }
2826 
2827 static int
2828 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2829 {
2830         spa_t *spa;
2831         int error;
2832         nvlist_t *nvp = NULL;
2833 
2834         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2835                 /*
2836                  * If the pool is faulted, there may be properties we can still
2837                  * get (such as altroot and cachefile), so attempt to get them
2838                  * anyway.
2839                  */
2840                 mutex_enter(&spa_namespace_lock);
2841                 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2842                         error = spa_prop_get(spa, &nvp);
2843                 mutex_exit(&spa_namespace_lock);
2844         } else {
2845                 error = spa_prop_get(spa, &nvp);
2846                 spa_close(spa, FTAG);
2847         }
2848 
2849         if (error == 0 && zc->zc_nvlist_dst != NULL)
2850                 error = put_nvlist(zc, nvp);
2851         else
2852                 error = EFAULT;
2853 
2854         nvlist_free(nvp);
2855         return (error);
2856 }
2857 
2858 /*
2859  * inputs:
2860  * zc_name              name of filesystem
2861  * zc_nvlist_src{_size} nvlist of delegated permissions
2862  * zc_perm_action       allow/unallow flag
2863  *
2864  * outputs:             none
2865  */
2866 static int
2867 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2868 {
2869         int error;
2870         nvlist_t *fsaclnv = NULL;
2871 
2872         if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2873             zc->zc_iflags, &fsaclnv)) != 0)
2874                 return (error);
2875 
2876         /*
2877          * Verify nvlist is constructed correctly
2878          */
2879         if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2880                 nvlist_free(fsaclnv);
2881                 return (EINVAL);
2882         }
2883 
2884         /*
2885          * If we don't have PRIV_SYS_MOUNT, then validate
2886          * that user is allowed to hand out each permission in
2887          * the nvlist(s)
2888          */
2889 
2890         error = secpolicy_zfs(CRED());
2891         if (error) {
2892                 if (zc->zc_perm_action == B_FALSE) {
2893                         error = dsl_deleg_can_allow(zc->zc_name,
2894                             fsaclnv, CRED());
2895                 } else {
2896                         error = dsl_deleg_can_unallow(zc->zc_name,
2897                             fsaclnv, CRED());
2898                 }
2899         }
2900 
2901         if (error == 0)
2902                 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2903 
2904         nvlist_free(fsaclnv);
2905         return (error);
2906 }
2907 
2908 /*
2909  * inputs:
2910  * zc_name              name of filesystem
2911  *
2912  * outputs:
2913  * zc_nvlist_src{_size} nvlist of delegated permissions
2914  */
2915 static int
2916 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2917 {
2918         nvlist_t *nvp;
2919         int error;
2920 
2921         if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2922                 error = put_nvlist(zc, nvp);
2923                 nvlist_free(nvp);
2924         }
2925 
2926         return (error);
2927 }
2928 
2929 /*
2930  * Search the vfs list for a specified resource.  Returns a pointer to it
2931  * or NULL if no suitable entry is found. The caller of this routine
2932  * is responsible for releasing the returned vfs pointer.
2933  */
2934 static vfs_t *
2935 zfs_get_vfs(const char *resource)
2936 {
2937         struct vfs *vfsp;
2938         struct vfs *vfs_found = NULL;
2939 
2940         vfs_list_read_lock();
2941         vfsp = rootvfs;
2942         do {
2943                 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2944                         VFS_HOLD(vfsp);
2945                         vfs_found = vfsp;
2946                         break;
2947                 }
2948                 vfsp = vfsp->vfs_next;
2949         } while (vfsp != rootvfs);
2950         vfs_list_unlock();
2951         return (vfs_found);
2952 }
2953 
2954 /* ARGSUSED */
2955 static void
2956 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2957 {
2958         zfs_creat_t *zct = arg;
2959 
2960         zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2961 }
2962 
2963 #define ZFS_PROP_UNDEFINED      ((uint64_t)-1)
2964 
2965 /*
2966  * inputs:
2967  * createprops          list of properties requested by creator
2968  * default_zplver       zpl version to use if unspecified in createprops
2969  * fuids_ok             fuids allowed in this version of the spa?
2970  * os                   parent objset pointer (NULL if root fs)
2971  *
2972  * outputs:
2973  * zplprops     values for the zplprops we attach to the master node object
2974  * is_ci        true if requested file system will be purely case-insensitive
2975  *
2976  * Determine the settings for utf8only, normalization and
2977  * casesensitivity.  Specific values may have been requested by the
2978  * creator and/or we can inherit values from the parent dataset.  If
2979  * the file system is of too early a vintage, a creator can not
2980  * request settings for these properties, even if the requested
2981  * setting is the default value.  We don't actually want to create dsl
2982  * properties for these, so remove them from the source nvlist after
2983  * processing.
2984  */
2985 static int
2986 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
2987     boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
2988     nvlist_t *zplprops, boolean_t *is_ci)
2989 {
2990         uint64_t sense = ZFS_PROP_UNDEFINED;
2991         uint64_t norm = ZFS_PROP_UNDEFINED;
2992         uint64_t u8 = ZFS_PROP_UNDEFINED;
2993 
2994         ASSERT(zplprops != NULL);
2995 
2996         /*
2997          * Pull out creator prop choices, if any.
2998          */
2999         if (createprops) {
3000                 (void) nvlist_lookup_uint64(createprops,
3001                     zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3002                 (void) nvlist_lookup_uint64(createprops,
3003                     zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3004                 (void) nvlist_remove_all(createprops,
3005                     zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3006                 (void) nvlist_lookup_uint64(createprops,
3007                     zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3008                 (void) nvlist_remove_all(createprops,
3009                     zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3010                 (void) nvlist_lookup_uint64(createprops,
3011                     zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3012                 (void) nvlist_remove_all(createprops,
3013                     zfs_prop_to_name(ZFS_PROP_CASE));
3014         }
3015 
3016         /*
3017          * If the zpl version requested is whacky or the file system
3018          * or pool is version is too "young" to support normalization
3019          * and the creator tried to set a value for one of the props,
3020          * error out.
3021          */
3022         if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3023             (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3024             (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3025             (zplver < ZPL_VERSION_NORMALIZATION &&
3026             (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3027             sense != ZFS_PROP_UNDEFINED)))
3028                 return (ENOTSUP);
3029 
3030         /*
3031          * Put the version in the zplprops
3032          */
3033         VERIFY(nvlist_add_uint64(zplprops,
3034             zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3035 
3036         if (norm == ZFS_PROP_UNDEFINED)
3037                 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3038         VERIFY(nvlist_add_uint64(zplprops,
3039             zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3040 
3041         if (os) {
3042                 char osname[MAXNAMELEN];
3043 
3044                 dmu_objset_name(os, osname);
3045                 if (zfs_is_wormed(osname))
3046                         return (EPERM);
3047         }
3048 
3049         /*
3050          * If we're normalizing, names must always be valid UTF-8 strings.
3051          */
3052         if (norm)
3053                 u8 = 1;
3054         if (u8 == ZFS_PROP_UNDEFINED)
3055                 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3056         VERIFY(nvlist_add_uint64(zplprops,
3057             zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3058 
3059         if (sense == ZFS_PROP_UNDEFINED)
3060                 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3061         VERIFY(nvlist_add_uint64(zplprops,
3062             zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3063 
3064         if (is_ci)
3065                 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3066 
3067         return (0);
3068 }
3069 
3070 static int
3071 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3072     nvlist_t *zplprops, boolean_t *is_ci)
3073 {
3074         boolean_t fuids_ok, sa_ok;
3075         uint64_t zplver = ZPL_VERSION;
3076         objset_t *os = NULL;
3077         char parentname[MAXNAMELEN];
3078         char *cp;
3079         spa_t *spa;
3080         uint64_t spa_vers;
3081         int error;
3082 
3083         (void) strlcpy(parentname, dataset, sizeof (parentname));
3084         cp = strrchr(parentname, '/');
3085         ASSERT(cp != NULL);
3086         cp[0] = '\0';
3087 
3088         if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3089                 return (error);
3090 
3091         spa_vers = spa_version(spa);
3092         spa_close(spa, FTAG);
3093 
3094         zplver = zfs_zpl_version_map(spa_vers);
3095         fuids_ok = (zplver >= ZPL_VERSION_FUID);
3096         sa_ok = (zplver >= ZPL_VERSION_SA);
3097 
3098         /*
3099          * Open parent object set so we can inherit zplprop values.
3100          */
3101         if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3102                 return (error);
3103 
3104         error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3105             zplprops, is_ci);
3106         dmu_objset_rele(os, FTAG);
3107         return (error);
3108 }
3109 
3110 static int
3111 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3112     nvlist_t *zplprops, boolean_t *is_ci)
3113 {
3114         boolean_t fuids_ok;
3115         boolean_t sa_ok;
3116         uint64_t zplver = ZPL_VERSION;
3117         int error;
3118 
3119         zplver = zfs_zpl_version_map(spa_vers);
3120         fuids_ok = (zplver >= ZPL_VERSION_FUID);
3121         sa_ok = (zplver >= ZPL_VERSION_SA);
3122 
3123         error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3124             createprops, zplprops, is_ci);
3125         return (error);
3126 }
3127 
3128 /*
3129  * innvl: {
3130  *     "type" -> dmu_objset_type_t (int32)
3131  *     (optional) "props" -> { prop -> value }
3132  * }
3133  *
3134  * outnvl: propname -> error code (int32)
3135  */
3136 static int
3137 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3138 {
3139         int error = 0;
3140         zfs_creat_t zct = { 0 };
3141         nvlist_t *nvprops = NULL;
3142         void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3143         int32_t type32;
3144         dmu_objset_type_t type;
3145         boolean_t is_insensitive = B_FALSE;
3146         char parent[MAXNAMELEN];
3147 
3148         if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3149                 return (EINVAL);
3150         type = type32;
3151         (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3152 
3153         switch (type) {
3154         case DMU_OST_ZFS:
3155                 cbfunc = zfs_create_cb;
3156                 break;
3157 
3158         case DMU_OST_ZVOL:
3159                 cbfunc = zvol_create_cb;
3160                 break;
3161 
3162         default:
3163                 cbfunc = NULL;
3164                 break;
3165         }
3166         if (strchr(fsname, '@') ||
3167             strchr(fsname, '%'))
3168                 return (EINVAL);
3169 
3170         zct.zct_props = nvprops;
3171 
3172         if (cbfunc == NULL)
3173                 return (EINVAL);
3174 
3175         if (zfs_get_parent(name, parent, MAXNAMELEN) == 0 &&
3176             zfs_is_wormed(parent)) {
3177                 return (EPERM);
3178         }
3179 
3180         if (type == DMU_OST_ZVOL) {
3181                 uint64_t volsize, volblocksize;
3182 
3183                 if (nvprops == NULL)
3184                         return (EINVAL);
3185                 if (nvlist_lookup_uint64(nvprops,
3186                     zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3187                         return (EINVAL);
3188 
3189                 if ((error = nvlist_lookup_uint64(nvprops,
3190                     zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3191                     &volblocksize)) != 0 && error != ENOENT)
3192                         return (EINVAL);
3193 
3194                 if (error != 0)
3195                         volblocksize = zfs_prop_default_numeric(
3196                             ZFS_PROP_VOLBLOCKSIZE);
3197 
3198                 if ((error = zvol_check_volblocksize(
3199                     volblocksize)) != 0 ||
3200                     (error = zvol_check_volsize(volsize,
3201                     volblocksize)) != 0)
3202                         return (error);
3203         } else if (type == DMU_OST_ZFS) {
3204                 int error;
3205 
3206                 /*
3207                  * We have to have normalization and
3208                  * case-folding flags correct when we do the
3209                  * file system creation, so go figure them out
3210                  * now.
3211                  */
3212                 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3213                     NV_UNIQUE_NAME, KM_SLEEP) == 0);
3214                 error = zfs_fill_zplprops(fsname, nvprops,
3215                     zct.zct_zplprops, &is_insensitive);
3216                 if (error != 0) {
3217                         nvlist_free(zct.zct_zplprops);
3218                         return (error);
3219                 }
3220         }
3221 
3222         error = dmu_objset_create(fsname, type,
3223             is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3224         nvlist_free(zct.zct_zplprops);
3225 
3226         /*
3227          * It would be nice to do this atomically.
3228          */
3229         if (error == 0) {
3230                 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3231                     nvprops, outnvl);
3232                 if (error != 0)
3233                         (void) dmu_objset_destroy(fsname, B_FALSE);
3234         }
3235         return (error);
3236 }
3237 
3238 /*
3239  * innvl: {
3240  *     "origin" -> name of origin snapshot
3241  *     (optional) "props" -> { prop -> value }
3242  * }
3243  *
3244  * outnvl: propname -> error code (int32)
3245  */
3246 static int
3247 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3248 {
3249         int error = 0;
3250         nvlist_t *nvprops = NULL;
3251         char *origin_name;
3252         dsl_dataset_t *origin;
3253 
3254         if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3255                 return (EINVAL);
3256         (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3257 
3258         if (strchr(fsname, '@') ||
3259             strchr(fsname, '%'))
3260                 return (EINVAL);
3261 
3262         if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3263                 return (EINVAL);
3264 
3265         error = dsl_dataset_hold(origin_name, FTAG, &origin);
3266         if (error)
3267                 return (error);
3268 
3269         error = dmu_objset_clone(fsname, origin, 0);
3270         dsl_dataset_rele(origin, FTAG);
3271         if (error)
3272                 return (error);
3273 
3274         /*
3275          * It would be nice to do this atomically.
3276          */
3277         if (error == 0) {
3278                 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3279                     nvprops, outnvl);
3280                 if (error != 0)
3281                         (void) dmu_objset_destroy(fsname, B_FALSE);
3282         }
3283         return (error);
3284 }
3285 
3286 /*
3287  * innvl: {
3288  *     "snaps" -> { snapshot1, snapshot2 }
3289  *     (optional) "props" -> { prop -> value (string) }
3290  * }
3291  *
3292  * outnvl: snapshot -> error code (int32)
3293  *
3294  */
3295 static int
3296 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3297 {
3298         nvlist_t *snaps;
3299         nvlist_t *props = NULL;
3300         int error, poollen;
3301         nvpair_t *pair;
3302 
3303         (void) nvlist_lookup_nvlist(innvl, "props", &props);
3304         if ((error = zfs_check_userprops(poolname, props)) != 0)
3305                 return (error);
3306 
3307         if (!nvlist_empty(props) &&
3308             zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3309                 return (ENOTSUP);
3310 
3311         if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3312                 return (EINVAL);
3313         poollen = strlen(poolname);
3314         for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3315             pair = nvlist_next_nvpair(snaps, pair)) {
3316                 const char *name = nvpair_name(pair);
3317                 const char *cp = strchr(name, '@');
3318 
3319                 /*
3320                  * The snap name must contain an @, and the part after it must
3321                  * contain only valid characters.
3322                  */
3323                 if (cp == NULL || snapshot_namecheck(cp + 1, NULL, NULL) != 0)
3324                         return (EINVAL);
3325 
3326                 /*
3327                  * The snap must be in the specified pool.
3328                  */
3329                 if (strncmp(name, poolname, poollen) != 0 ||
3330                     (name[poollen] != '/' && name[poollen] != '@'))
3331                         return (EXDEV);
3332 
3333                 /* This must be the only snap of this fs. */
3334                 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3335                     pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3336                         if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3337                             == 0) {
3338                                 return (EXDEV);
3339                         }
3340                 }
3341         }
3342 
3343         error = dmu_objset_snapshot(snaps, props, outnvl);
3344         return (error);
3345 }
3346 
3347 /*
3348  * innvl: "message" -> string
3349  */
3350 /* ARGSUSED */
3351 static int
3352 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3353 {
3354         char *message;
3355         spa_t *spa;
3356         int error;
3357         char *poolname;
3358 
3359         /*
3360          * The poolname in the ioctl is not set, we get it from the TSD,
3361          * which was set at the end of the last successful ioctl that allows
3362          * logging.  The secpolicy func already checked that it is set.
3363          * Only one log ioctl is allowed after each successful ioctl, so
3364          * we clear the TSD here.
3365          */
3366         poolname = tsd_get(zfs_allow_log_key);
3367         (void) tsd_set(zfs_allow_log_key, NULL);
3368         error = spa_open(poolname, &spa, FTAG);
3369         strfree(poolname);
3370         if (error != 0)
3371                 return (error);
3372 
3373         if (nvlist_lookup_string(innvl, "message", &message) != 0)  {
3374                 spa_close(spa, FTAG);
3375                 return (EINVAL);
3376         }
3377 
3378         if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3379                 spa_close(spa, FTAG);
3380                 return (ENOTSUP);
3381         }
3382 
3383         error = spa_history_log(spa, message);
3384         spa_close(spa, FTAG);
3385         return (error);
3386 }
3387 
3388 /* ARGSUSED */
3389 int
3390 zfs_unmount_snap(const char *name, void *arg)
3391 {
3392         vfs_t *vfsp;
3393         int err;
3394 
3395         if (strchr(name, '@') == NULL)
3396                 return (0);
3397 
3398         vfsp = zfs_get_vfs(name);
3399         if (vfsp == NULL)
3400                 return (0);
3401 
3402         if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
3403                 VFS_RELE(vfsp);
3404                 return (err);
3405         }
3406         VFS_RELE(vfsp);
3407 
3408         /*
3409          * Always force the unmount for snapshots.
3410          */
3411         return (dounmount(vfsp, MS_FORCE, kcred));
3412 }
3413 
3414 /*
3415  * innvl: {
3416  *     "snaps" -> { snapshot1, snapshot2 }
3417  *     (optional boolean) "defer"
3418  * }
3419  *
3420  * outnvl: snapshot -> error code (int32)
3421  *
3422  */
3423 static int
3424 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3425 {
3426         int poollen;
3427         nvlist_t *snaps;
3428         nvpair_t *pair;
3429         boolean_t defer;
3430 
3431         if (zfs_is_wormed(poolname))
3432                 return (EPERM);
3433 
3434         if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3435                 return (EINVAL);
3436         defer = nvlist_exists(innvl, "defer");
3437 
3438         poollen = strlen(poolname);
3439         for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3440             pair = nvlist_next_nvpair(snaps, pair)) {
3441                 const char *name = nvpair_name(pair);
3442 
3443                 /*
3444                  * The snap must be in the specified pool.
3445                  */
3446                 if (strncmp(name, poolname, poollen) != 0 ||
3447                     (name[poollen] != '/' && name[poollen] != '@'))
3448                         return (EXDEV);
3449 
3450                 /*
3451                  * Ignore failures to unmount; dmu_snapshots_destroy_nvl()
3452                  * will deal with this gracefully (by filling in outnvl).
3453                  */
3454                 (void) zfs_unmount_snap(name, NULL);
3455         }
3456 
3457         return (dmu_snapshots_destroy_nvl(snaps, defer, outnvl));
3458 }
3459 
3460 /*
3461  * inputs:
3462  * zc_name              name of dataset to destroy
3463  * zc_objset_type       type of objset
3464  * zc_defer_destroy     mark for deferred destroy
3465  *
3466  * outputs:             none
3467  */
3468 static int
3469 zfs_ioc_destroy(zfs_cmd_t *zc)
3470 {
3471         int err;
3472 
3473         if (zfs_is_wormed(zc->zc_name))
3474                 return (EPERM);
3475 
3476         if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
3477                 err = zfs_unmount_snap(zc->zc_name, NULL);
3478                 if (err)
3479                         return (err);
3480         }
3481 
3482         err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
3483         if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3484                 (void) zvol_remove_minor(zc->zc_name);
3485         return (err);
3486 }
3487 
3488 /*
3489  * inputs:
3490  * zc_name      name of dataset to rollback (to most recent snapshot)
3491  *
3492  * outputs:     none
3493  */
3494 static int
3495 zfs_ioc_rollback(zfs_cmd_t *zc)
3496 {
3497         dsl_dataset_t *ds, *clone;
3498         int error;
3499         zfsvfs_t *zfsvfs;
3500         char *clone_name;
3501 
3502         if (zfs_is_wormed(zc->zc_name))
3503                 return (EPERM);
3504 
3505         error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
3506         if (error)
3507                 return (error);
3508 
3509         /* must not be a snapshot */
3510         if (dsl_dataset_is_snapshot(ds)) {
3511                 dsl_dataset_rele(ds, FTAG);
3512                 return (EINVAL);
3513         }
3514 
3515         /* must have a most recent snapshot */
3516         if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
3517                 dsl_dataset_rele(ds, FTAG);
3518                 return (EINVAL);
3519         }
3520 
3521         /*
3522          * Create clone of most recent snapshot.
3523          */
3524         clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
3525         error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
3526         if (error)
3527                 goto out;
3528 
3529         error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
3530         if (error)
3531                 goto out;
3532 
3533         /*
3534          * Do clone swap.
3535          */
3536         if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
3537                 error = zfs_suspend_fs(zfsvfs);
3538                 if (error == 0) {
3539                         int resume_err;
3540 
3541                         if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3542                                 error = dsl_dataset_clone_swap(clone, ds,
3543                                     B_TRUE);
3544                                 dsl_dataset_disown(ds, FTAG);
3545                                 ds = NULL;
3546                         } else {
3547                                 error = EBUSY;
3548                         }
3549                         resume_err = zfs_resume_fs(zfsvfs, zc->zc_name);
3550                         error = error ? error : resume_err;
3551                 }
3552                 VFS_RELE(zfsvfs->z_vfs);
3553         } else {
3554                 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3555                         error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
3556                         dsl_dataset_disown(ds, FTAG);
3557                         ds = NULL;
3558                 } else {
3559                         error = EBUSY;
3560                 }
3561         }
3562 
3563         /*
3564          * Destroy clone (which also closes it).
3565          */
3566         (void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
3567 
3568 out:
3569         strfree(clone_name);
3570         if (ds)
3571                 dsl_dataset_rele(ds, FTAG);
3572         return (error);
3573 }
3574 
3575 /*
3576  * inputs:
3577  * zc_name      old name of dataset
3578  * zc_value     new name of dataset
3579  * zc_cookie    recursive flag (only valid for snapshots)
3580  *
3581  * outputs:     none
3582  */
3583 static int
3584 zfs_ioc_rename(zfs_cmd_t *zc)
3585 {
3586         boolean_t recursive = zc->zc_cookie & 1;
3587 
3588         if (zfs_is_wormed(zc->zc_name))
3589                 return (EPERM);
3590 
3591         zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3592         if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3593             strchr(zc->zc_value, '%'))
3594                 return (EINVAL);
3595 
3596         /*
3597          * Unmount snapshot unless we're doing a recursive rename,
3598          * in which case the dataset code figures out which snapshots
3599          * to unmount.
3600          */
3601         if (!recursive && strchr(zc->zc_name, '@') != NULL &&
3602             zc->zc_objset_type == DMU_OST_ZFS) {
3603                 int err = zfs_unmount_snap(zc->zc_name, NULL);
3604                 if (err)
3605                         return (err);
3606         }
3607         if (zc->zc_objset_type == DMU_OST_ZVOL)
3608                 (void) zvol_remove_minor(zc->zc_name);
3609         return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
3610 }
3611 
3612 static int
3613 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3614 {
3615         const char *propname = nvpair_name(pair);
3616         boolean_t issnap = (strchr(dsname, '@') != NULL);
3617         zfs_prop_t prop = zfs_name_to_prop(propname);
3618         uint64_t intval;
3619         int err;
3620 
3621         if (prop == ZPROP_INVAL) {
3622                 if (zfs_prop_user(propname)) {
3623                         if (err = zfs_secpolicy_write_perms(dsname,
3624                             ZFS_DELEG_PERM_USERPROP, cr))
3625                                 return (err);
3626                         return (0);
3627                 }
3628 
3629                 if (!issnap && zfs_prop_userquota(propname)) {
3630                         const char *perm = NULL;
3631                         const char *uq_prefix =
3632                             zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3633                         const char *gq_prefix =
3634                             zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3635 
3636                         if (strncmp(propname, uq_prefix,
3637                             strlen(uq_prefix)) == 0) {
3638                                 perm = ZFS_DELEG_PERM_USERQUOTA;
3639                         } else if (strncmp(propname, gq_prefix,
3640                             strlen(gq_prefix)) == 0) {
3641                                 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3642                         } else {
3643                                 /* USERUSED and GROUPUSED are read-only */
3644                                 return (EINVAL);
3645                         }
3646 
3647                         if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
3648                                 return (err);
3649                         return (0);
3650                 }
3651 
3652                 return (EINVAL);
3653         }
3654 
3655         if (issnap)
3656                 return (EINVAL);
3657 
3658         if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3659                 /*
3660                  * dsl_prop_get_all_impl() returns properties in this
3661                  * format.
3662                  */
3663                 nvlist_t *attrs;
3664                 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3665                 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3666                     &pair) == 0);
3667         }
3668 
3669         /*
3670          * Check that this value is valid for this pool version
3671          */
3672         switch (prop) {
3673         case ZFS_PROP_COMPRESSION:
3674                 /*
3675                  * If the user specified gzip compression, make sure
3676                  * the SPA supports it. We ignore any errors here since
3677                  * we'll catch them later.
3678                  */
3679                 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3680                     nvpair_value_uint64(pair, &intval) == 0) {
3681                         if (intval >= ZIO_COMPRESS_GZIP_1 &&
3682                             intval <= ZIO_COMPRESS_GZIP_9 &&
3683                             zfs_earlier_version(dsname,
3684                             SPA_VERSION_GZIP_COMPRESSION)) {
3685                                 return (ENOTSUP);
3686                         }
3687 
3688                         if (intval == ZIO_COMPRESS_ZLE &&
3689                             zfs_earlier_version(dsname,
3690                             SPA_VERSION_ZLE_COMPRESSION))
3691                                 return (ENOTSUP);
3692 
3693                         /*
3694                          * If this is a bootable dataset then
3695                          * verify that the compression algorithm
3696                          * is supported for booting. We must return
3697                          * something other than ENOTSUP since it
3698                          * implies a downrev pool version.
3699                          */
3700                         if (zfs_is_bootfs(dsname) &&
3701                             !BOOTFS_COMPRESS_VALID(intval)) {
3702                                 return (ERANGE);
3703                         }
3704                 }
3705                 break;
3706 
3707         case ZFS_PROP_COPIES:
3708                 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3709                         return (ENOTSUP);
3710                 break;
3711 
3712         case ZFS_PROP_DEDUP:
3713                 if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3714                         return (ENOTSUP);
3715                 break;
3716 
3717         case ZFS_PROP_SHARESMB:
3718                 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3719                         return (ENOTSUP);
3720                 break;
3721 
3722         case ZFS_PROP_ACLINHERIT:
3723                 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3724                     nvpair_value_uint64(pair, &intval) == 0) {
3725                         if (intval == ZFS_ACL_PASSTHROUGH_X &&
3726                             zfs_earlier_version(dsname,
3727                             SPA_VERSION_PASSTHROUGH_X))
3728                                 return (ENOTSUP);
3729                 }
3730                 break;
3731         }
3732 
3733         return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3734 }
3735 
3736 /*
3737  * Removes properties from the given props list that fail permission checks
3738  * needed to clear them and to restore them in case of a receive error. For each
3739  * property, make sure we have both set and inherit permissions.
3740  *
3741  * Returns the first error encountered if any permission checks fail. If the
3742  * caller provides a non-NULL errlist, it also gives the complete list of names
3743  * of all the properties that failed a permission check along with the
3744  * corresponding error numbers. The caller is responsible for freeing the
3745  * returned errlist.
3746  *
3747  * If every property checks out successfully, zero is returned and the list
3748  * pointed at by errlist is NULL.
3749  */
3750 static int
3751 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3752 {
3753         zfs_cmd_t *zc;
3754         nvpair_t *pair, *next_pair;
3755         nvlist_t *errors;
3756         int err, rv = 0;
3757 
3758         if (props == NULL)
3759                 return (0);
3760 
3761         VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3762 
3763         zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3764         (void) strcpy(zc->zc_name, dataset);
3765         pair = nvlist_next_nvpair(props, NULL);
3766         while (pair != NULL) {
3767                 next_pair = nvlist_next_nvpair(props, pair);
3768 
3769                 (void) strcpy(zc->zc_value, nvpair_name(pair));
3770                 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3771                     (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
3772                         VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3773                         VERIFY(nvlist_add_int32(errors,
3774                             zc->zc_value, err) == 0);
3775                 }
3776                 pair = next_pair;
3777         }
3778         kmem_free(zc, sizeof (zfs_cmd_t));
3779 
3780         if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3781                 nvlist_free(errors);
3782                 errors = NULL;
3783         } else {
3784                 VERIFY(nvpair_value_int32(pair, &rv) == 0);
3785         }
3786 
3787         if (errlist == NULL)
3788                 nvlist_free(errors);
3789         else
3790                 *errlist = errors;
3791 
3792         return (rv);
3793 }
3794 
3795 static boolean_t
3796 propval_equals(nvpair_t *p1, nvpair_t *p2)
3797 {
3798         if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3799                 /* dsl_prop_get_all_impl() format */
3800                 nvlist_t *attrs;
3801                 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3802                 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3803                     &p1) == 0);
3804         }
3805 
3806         if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
3807                 nvlist_t *attrs;
3808                 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
3809                 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3810                     &p2) == 0);
3811         }
3812 
3813         if (nvpair_type(p1) != nvpair_type(p2))
3814                 return (B_FALSE);
3815 
3816         if (nvpair_type(p1) == DATA_TYPE_STRING) {
3817                 char *valstr1, *valstr2;
3818 
3819                 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
3820                 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
3821                 return (strcmp(valstr1, valstr2) == 0);
3822         } else {
3823                 uint64_t intval1, intval2;
3824 
3825                 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
3826                 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
3827                 return (intval1 == intval2);
3828         }
3829 }
3830 
3831 /*
3832  * Remove properties from props if they are not going to change (as determined
3833  * by comparison with origprops). Remove them from origprops as well, since we
3834  * do not need to clear or restore properties that won't change.
3835  */
3836 static void
3837 props_reduce(nvlist_t *props, nvlist_t *origprops)
3838 {
3839         nvpair_t *pair, *next_pair;
3840 
3841         if (origprops == NULL)
3842                 return; /* all props need to be received */
3843 
3844         pair = nvlist_next_nvpair(props, NULL);
3845         while (pair != NULL) {
3846                 const char *propname = nvpair_name(pair);
3847                 nvpair_t *match;
3848 
3849                 next_pair = nvlist_next_nvpair(props, pair);
3850 
3851                 if ((nvlist_lookup_nvpair(origprops, propname,
3852                     &match) != 0) || !propval_equals(pair, match))
3853                         goto next; /* need to set received value */
3854 
3855                 /* don't clear the existing received value */
3856                 (void) nvlist_remove_nvpair(origprops, match);
3857                 /* don't bother receiving the property */
3858                 (void) nvlist_remove_nvpair(props, pair);
3859 next:
3860                 pair = next_pair;
3861         }
3862 }
3863 
3864 #ifdef  DEBUG
3865 static boolean_t zfs_ioc_recv_inject_err;
3866 #endif
3867 
3868 /*
3869  * inputs:
3870  * zc_name              name of containing filesystem
3871  * zc_nvlist_src{_size} nvlist of properties to apply
3872  * zc_value             name of snapshot to create
3873  * zc_string            name of clone origin (if DRR_FLAG_CLONE)
3874  * zc_cookie            file descriptor to recv from
3875  * zc_begin_record      the BEGIN record of the stream (not byteswapped)
3876  * zc_guid              force flag
3877  * zc_cleanup_fd        cleanup-on-exit file descriptor
3878  * zc_action_handle     handle for this guid/ds mapping (or zero on first call)
3879  *
3880  * outputs:
3881  * zc_cookie            number of bytes read
3882  * zc_nvlist_dst{_size} error for each unapplied received property
3883  * zc_obj               zprop_errflags_t
3884  * zc_action_handle     handle for this guid/ds mapping
3885  */
3886 static int
3887 zfs_ioc_recv(zfs_cmd_t *zc)
3888 {
3889         file_t *fp;
3890         objset_t *os;
3891         dmu_recv_cookie_t drc;
3892         boolean_t force = (boolean_t)zc->zc_guid;
3893         int fd;
3894         int error = 0;
3895         int props_error = 0;
3896         nvlist_t *errors;
3897         offset_t off;
3898         nvlist_t *props = NULL; /* sent properties */
3899         nvlist_t *origprops = NULL; /* existing properties */
3900         objset_t *origin = NULL;
3901         char *tosnap;
3902         char tofs[ZFS_MAXNAMELEN];
3903         boolean_t first_recvd_props = B_FALSE;
3904 
3905         if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3906             strchr(zc->zc_value, '@') == NULL ||
3907             strchr(zc->zc_value, '%'))
3908                 return (EINVAL);
3909 
3910         (void) strcpy(tofs, zc->zc_value);
3911         tosnap = strchr(tofs, '@');
3912         *tosnap++ = '\0';
3913 
3914         if (zc->zc_nvlist_src != NULL &&
3915             (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3916             zc->zc_iflags, &props)) != 0)
3917                 return (error);
3918 
3919         fd = zc->zc_cookie;
3920         fp = getf(fd);
3921         if (fp == NULL) {
3922                 nvlist_free(props);
3923                 return (EBADF);
3924         }
3925 
3926         VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3927 
3928         if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
3929                 if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) &&
3930                     !dsl_prop_get_hasrecvd(os)) {
3931                         first_recvd_props = B_TRUE;
3932                 }
3933 
3934                 /*
3935                  * If new received properties are supplied, they are to
3936                  * completely replace the existing received properties, so stash
3937                  * away the existing ones.
3938                  */
3939                 if (dsl_prop_get_received(os, &origprops) == 0) {
3940                         nvlist_t *errlist = NULL;
3941                         /*
3942                          * Don't bother writing a property if its value won't
3943                          * change (and avoid the unnecessary security checks).
3944                          *
3945                          * The first receive after SPA_VERSION_RECVD_PROPS is a
3946                          * special case where we blow away all local properties
3947                          * regardless.
3948                          */
3949                         if (!first_recvd_props)
3950                                 props_reduce(props, origprops);
3951                         if (zfs_check_clearable(tofs, origprops,
3952                             &errlist) != 0)
3953                                 (void) nvlist_merge(errors, errlist, 0);
3954                         nvlist_free(errlist);
3955                 }
3956 
3957                 dmu_objset_rele(os, FTAG);
3958         }
3959 
3960         if (zc->zc_string[0]) {
3961                 error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
3962                 if (error)
3963                         goto out;
3964         }
3965 
3966         error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds,
3967             &zc->zc_begin_record, force, origin, &drc);
3968         if (origin)
3969                 dmu_objset_rele(origin, FTAG);
3970         if (error)
3971                 goto out;
3972 
3973         /*
3974          * Set properties before we receive the stream so that they are applied
3975          * to the new data. Note that we must call dmu_recv_stream() if
3976          * dmu_recv_begin() succeeds.
3977          */
3978         if (props) {
3979                 if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) {
3980                         if (drc.drc_newfs) {
3981                                 if (spa_version(os->os_spa) >=
3982                                     SPA_VERSION_RECVD_PROPS)
3983                                         first_recvd_props = B_TRUE;
3984                         } else if (origprops != NULL) {
3985                                 if (clear_received_props(os, tofs, origprops,
3986                                     first_recvd_props ? NULL : props) != 0)
3987                                         zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3988                         } else {
3989                                 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3990                         }
3991                         dsl_prop_set_hasrecvd(os);
3992                 } else if (!drc.drc_newfs) {
3993                         zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3994                 }
3995 
3996                 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
3997                     props, errors);
3998         }
3999 
4000         if (zc->zc_nvlist_dst_size != 0 &&
4001             (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4002             put_nvlist(zc, errors) != 0)) {
4003                 /*
4004                  * Caller made zc->zc_nvlist_dst less than the minimum expected
4005                  * size or supplied an invalid address.
4006                  */
4007                 props_error = EINVAL;
4008         }
4009 
4010         off = fp->f_offset;
4011         error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
4012             &zc->zc_action_handle);
4013 
4014         if (error == 0) {
4015                 zfsvfs_t *zfsvfs = NULL;
4016 
4017                 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4018                         /* online recv */
4019                         int end_err;
4020 
4021                         error = zfs_suspend_fs(zfsvfs);
4022                         /*
4023                          * If the suspend fails, then the recv_end will
4024                          * likely also fail, and clean up after itself.
4025                          */
4026                         end_err = dmu_recv_end(&drc);
4027                         if (error == 0)
4028                                 error = zfs_resume_fs(zfsvfs, tofs);
4029                         error = error ? error : end_err;
4030                         VFS_RELE(zfsvfs->z_vfs);
4031                 } else {
4032                         error = dmu_recv_end(&drc);
4033                 }
4034         }
4035 
4036         zc->zc_cookie = off - fp->f_offset;
4037         if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4038                 fp->f_offset = off;
4039 
4040 #ifdef  DEBUG
4041         if (zfs_ioc_recv_inject_err) {
4042                 zfs_ioc_recv_inject_err = B_FALSE;
4043                 error = 1;
4044         }
4045 #endif
4046         /*
4047          * On error, restore the original props.
4048          */
4049         if (error && props) {
4050                 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
4051                         if (clear_received_props(os, tofs, props, NULL) != 0) {
4052                                 /*
4053                                  * We failed to clear the received properties.
4054                                  * Since we may have left a $recvd value on the
4055                                  * system, we can't clear the $hasrecvd flag.
4056                                  */
4057                                 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4058                         } else if (first_recvd_props) {
4059                                 dsl_prop_unset_hasrecvd(os);
4060                         }
4061                         dmu_objset_rele(os, FTAG);
4062                 } else if (!drc.drc_newfs) {
4063                         /* We failed to clear the received properties. */
4064                         zc->zc_obj |= ZPROP_ERR_NORESTORE;
4065                 }
4066 
4067                 if (origprops == NULL && !drc.drc_newfs) {
4068                         /* We failed to stash the original properties. */
4069                         zc->zc_obj |= ZPROP_ERR_NORESTORE;
4070                 }
4071 
4072                 /*
4073                  * dsl_props_set() will not convert RECEIVED to LOCAL on or
4074                  * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4075                  * explictly if we're restoring local properties cleared in the
4076                  * first new-style receive.
4077                  */
4078                 if (origprops != NULL &&
4079                     zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4080                     ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4081                     origprops, NULL) != 0) {
4082                         /*
4083                          * We stashed the original properties but failed to
4084                          * restore them.
4085                          */
4086                         zc->zc_obj |= ZPROP_ERR_NORESTORE;
4087                 }
4088         }
4089 out:
4090         nvlist_free(props);
4091         nvlist_free(origprops);
4092         nvlist_free(errors);
4093         releasef(fd);
4094 
4095         if (error == 0)
4096                 error = props_error;
4097 
4098         return (error);
4099 }
4100 
4101 /*
4102  * inputs:
4103  * zc_name      name of snapshot to send
4104  * zc_cookie    file descriptor to send stream to
4105  * zc_obj       fromorigin flag (mutually exclusive with zc_fromobj)
4106  * zc_sendobj   objsetid of snapshot to send
4107  * zc_fromobj   objsetid of incremental fromsnap (may be zero)
4108  * zc_guid      if set, estimate size of stream only.  zc_cookie is ignored.
4109  *              output size in zc_objset_type.
4110  *
4111  * outputs: none
4112  */
4113 static int
4114 zfs_ioc_send(zfs_cmd_t *zc)
4115 {
4116         objset_t *fromsnap = NULL;
4117         objset_t *tosnap;
4118         int error;
4119         offset_t off;
4120         dsl_dataset_t *ds;
4121         dsl_dataset_t *dsfrom = NULL;
4122         spa_t *spa;
4123         dsl_pool_t *dp;
4124         boolean_t estimate = (zc->zc_guid != 0);
4125 
4126         error = spa_open(zc->zc_name, &spa, FTAG);
4127         if (error)
4128                 return (error);
4129 
4130         dp = spa_get_dsl(spa);
4131         rw_enter(&dp->dp_config_rwlock, RW_READER);
4132         error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
4133         rw_exit(&dp->dp_config_rwlock);
4134         spa_close(spa, FTAG);
4135         if (error)
4136                 return (error);
4137 
4138         error = dmu_objset_from_ds(ds, &tosnap);
4139         if (error) {
4140                 dsl_dataset_rele(ds, FTAG);
4141                 return (error);
4142         }
4143 
4144         if (zc->zc_fromobj != 0) {
4145                 rw_enter(&dp->dp_config_rwlock, RW_READER);
4146                 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, FTAG, &dsfrom);
4147                 rw_exit(&dp->dp_config_rwlock);
4148                 if (error) {
4149                         dsl_dataset_rele(ds, FTAG);
4150                         return (error);
4151                 }
4152                 error = dmu_objset_from_ds(dsfrom, &fromsnap);
4153                 if (error) {
4154                         dsl_dataset_rele(dsfrom, FTAG);
4155                         dsl_dataset_rele(ds, FTAG);
4156                         return (error);
4157                 }
4158         }
4159 
4160         if (zc->zc_obj) {
4161                 dsl_pool_t *dp = ds->ds_dir->dd_pool;
4162 
4163                 if (fromsnap != NULL) {
4164                         dsl_dataset_rele(dsfrom, FTAG);
4165                         dsl_dataset_rele(ds, FTAG);
4166                         return (EINVAL);
4167                 }
4168 
4169                 if (dsl_dir_is_clone(ds->ds_dir)) {
4170                         rw_enter(&dp->dp_config_rwlock, RW_READER);
4171                         error = dsl_dataset_hold_obj(dp,
4172                             ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &dsfrom);
4173                         rw_exit(&dp->dp_config_rwlock);
4174                         if (error) {
4175                                 dsl_dataset_rele(ds, FTAG);
4176                                 return (error);
4177                         }
4178                         error = dmu_objset_from_ds(dsfrom, &fromsnap);
4179                         if (error) {
4180                                 dsl_dataset_rele(dsfrom, FTAG);
4181                                 dsl_dataset_rele(ds, FTAG);
4182                                 return (error);
4183                         }
4184                 }
4185         }
4186 
4187         if (estimate) {
4188                 error = dmu_send_estimate(tosnap, fromsnap,
4189                     &zc->zc_objset_type);
4190         } else {
4191                 file_t *fp = getf(zc->zc_cookie);
4192                 if (fp == NULL) {
4193                         dsl_dataset_rele(ds, FTAG);
4194                         if (dsfrom)
4195                                 dsl_dataset_rele(dsfrom, FTAG);
4196                         return (EBADF);
4197                 }
4198 
4199                 off = fp->f_offset;
4200                 error = dmu_send(tosnap, fromsnap,
4201                     zc->zc_cookie, fp->f_vnode, &off);
4202 
4203                 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4204                         fp->f_offset = off;
4205                 releasef(zc->zc_cookie);
4206         }
4207         if (dsfrom)
4208                 dsl_dataset_rele(dsfrom, FTAG);
4209         dsl_dataset_rele(ds, FTAG);
4210         return (error);
4211 }
4212 
4213 /*
4214  * inputs:
4215  * zc_name      name of snapshot on which to report progress
4216  * zc_cookie    file descriptor of send stream
4217  *
4218  * outputs:
4219  * zc_cookie    number of bytes written in send stream thus far
4220  */
4221 static int
4222 zfs_ioc_send_progress(zfs_cmd_t *zc)
4223 {
4224         dsl_dataset_t *ds;
4225         dmu_sendarg_t *dsp = NULL;
4226         int error;
4227 
4228         if ((error = dsl_dataset_hold(zc->zc_name, FTAG, &ds)) != 0)
4229                 return (error);
4230 
4231         mutex_enter(&ds->ds_sendstream_lock);
4232 
4233         /*
4234          * Iterate over all the send streams currently active on this dataset.
4235          * If there's one which matches the specified file descriptor _and_ the
4236          * stream was started by the current process, return the progress of
4237          * that stream.
4238          */
4239         for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
4240             dsp = list_next(&ds->ds_sendstreams, dsp)) {
4241                 if (dsp->dsa_outfd == zc->zc_cookie &&
4242                     dsp->dsa_proc == curproc)
4243                         break;
4244         }
4245 
4246         if (dsp != NULL)
4247                 zc->zc_cookie = *(dsp->dsa_off);
4248         else
4249                 error = ENOENT;
4250 
4251         mutex_exit(&ds->ds_sendstream_lock);
4252         dsl_dataset_rele(ds, FTAG);
4253         return (error);
4254 }
4255 
4256 static int
4257 zfs_ioc_inject_fault(zfs_cmd_t *zc)
4258 {
4259         int id, error;
4260 
4261         error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4262             &zc->zc_inject_record);
4263 
4264         if (error == 0)
4265                 zc->zc_guid = (uint64_t)id;
4266 
4267         return (error);
4268 }
4269 
4270 static int
4271 zfs_ioc_clear_fault(zfs_cmd_t *zc)
4272 {
4273         return (zio_clear_fault((int)zc->zc_guid));
4274 }
4275 
4276 static int
4277 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4278 {
4279         int id = (int)zc->zc_guid;
4280         int error;
4281 
4282         error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4283             &zc->zc_inject_record);
4284 
4285         zc->zc_guid = id;
4286 
4287         return (error);
4288 }
4289 
4290 static int
4291 zfs_ioc_error_log(zfs_cmd_t *zc)
4292 {
4293         spa_t *spa;
4294         int error;
4295         size_t count = (size_t)zc->zc_nvlist_dst_size;
4296 
4297         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4298                 return (error);
4299 
4300         error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4301             &count);
4302         if (error == 0)
4303                 zc->zc_nvlist_dst_size = count;
4304         else
4305                 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4306 
4307         spa_close(spa, FTAG);
4308 
4309         return (error);
4310 }
4311 
4312 static int
4313 zfs_ioc_clear(zfs_cmd_t *zc)
4314 {
4315         spa_t *spa;
4316         vdev_t *vd;
4317         int error;
4318 
4319         /*
4320          * On zpool clear we also fix up missing slogs
4321          */
4322         mutex_enter(&spa_namespace_lock);
4323         spa = spa_lookup(zc->zc_name);
4324         if (spa == NULL) {
4325                 mutex_exit(&spa_namespace_lock);
4326                 return (EIO);
4327         }
4328         if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4329                 /* we need to let spa_open/spa_load clear the chains */
4330                 spa_set_log_state(spa, SPA_LOG_CLEAR);
4331         }
4332         spa->spa_last_open_failed = 0;
4333         mutex_exit(&spa_namespace_lock);
4334 
4335         if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4336                 error = spa_open(zc->zc_name, &spa, FTAG);
4337         } else {
4338                 nvlist_t *policy;
4339                 nvlist_t *config = NULL;
4340 
4341                 if (zc->zc_nvlist_src == NULL)
4342                         return (EINVAL);
4343 
4344                 if ((error = get_nvlist(zc->zc_nvlist_src,
4345                     zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4346                         error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4347                             policy, &config);
4348                         if (config != NULL) {
4349                                 int err;
4350 
4351                                 if ((err = put_nvlist(zc, config)) != 0)
4352                                         error = err;
4353                                 nvlist_free(config);
4354                         }
4355                         nvlist_free(policy);
4356                 }
4357         }
4358 
4359         if (error)
4360                 return (error);
4361 
4362         spa_vdev_state_enter(spa, SCL_NONE);
4363 
4364         if (zc->zc_guid == 0) {
4365                 vd = NULL;
4366         } else {
4367                 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4368                 if (vd == NULL) {
4369                         (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4370                         spa_close(spa, FTAG);
4371                         return (ENODEV);
4372                 }
4373         }
4374 
4375         vdev_clear(spa, vd);
4376 
4377         (void) spa_vdev_state_exit(spa, NULL, 0);
4378 
4379         /*
4380          * Resume any suspended I/Os.
4381          */
4382         if (zio_resume(spa) != 0)
4383                 error = EIO;
4384 
4385         spa_close(spa, FTAG);
4386 
4387         return (error);
4388 }
4389 
4390 static int
4391 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
4392 {
4393         spa_t *spa;
4394         int error;
4395 
4396         error = spa_open(zc->zc_name, &spa, FTAG);
4397         if (error)
4398                 return (error);
4399 
4400         spa_vdev_state_enter(spa, SCL_NONE);
4401         vdev_reopen(spa->spa_root_vdev);
4402         (void) spa_vdev_state_exit(spa, NULL, 0);
4403         spa_close(spa, FTAG);
4404         return (0);
4405 }
4406 /*
4407  * inputs:
4408  * zc_name      name of filesystem
4409  * zc_value     name of origin snapshot
4410  *
4411  * outputs:
4412  * zc_string    name of conflicting snapshot, if there is one
4413  */
4414 static int
4415 zfs_ioc_promote(zfs_cmd_t *zc)
4416 {
4417         char *cp;
4418 
4419         /*
4420          * We don't need to unmount *all* the origin fs's snapshots, but
4421          * it's easier.
4422          */
4423         cp = strchr(zc->zc_value, '@');
4424         if (cp)
4425                 *cp = '\0';
4426         (void) dmu_objset_find(zc->zc_value,
4427             zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
4428         return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4429 }
4430 
4431 /*
4432  * Retrieve a single {user|group}{used|quota}@... property.
4433  *
4434  * inputs:
4435  * zc_name      name of filesystem
4436  * zc_objset_type zfs_userquota_prop_t
4437  * zc_value     domain name (eg. "S-1-234-567-89")
4438  * zc_guid      RID/UID/GID
4439  *
4440  * outputs:
4441  * zc_cookie    property value
4442  */
4443 static int
4444 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4445 {
4446         zfsvfs_t *zfsvfs;
4447         int error;
4448 
4449         if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4450                 return (EINVAL);
4451 
4452         error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4453         if (error)
4454                 return (error);
4455 
4456         error = zfs_userspace_one(zfsvfs,
4457             zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4458         zfsvfs_rele(zfsvfs, FTAG);
4459 
4460         return (error);
4461 }
4462 
4463 /*
4464  * inputs:
4465  * zc_name              name of filesystem
4466  * zc_cookie            zap cursor
4467  * zc_objset_type       zfs_userquota_prop_t
4468  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4469  *
4470  * outputs:
4471  * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4472  * zc_cookie    zap cursor
4473  */
4474 static int
4475 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4476 {
4477         zfsvfs_t *zfsvfs;
4478         int bufsize = zc->zc_nvlist_dst_size;
4479 
4480         if (bufsize <= 0)
4481                 return (ENOMEM);
4482 
4483         int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4484         if (error)
4485                 return (error);
4486 
4487         void *buf = kmem_alloc(bufsize, KM_SLEEP);
4488 
4489         error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
4490             buf, &zc->zc_nvlist_dst_size);
4491 
4492         if (error == 0) {
4493                 error = xcopyout(buf,
4494                     (void *)(uintptr_t)zc->zc_nvlist_dst,
4495                     zc->zc_nvlist_dst_size);
4496         }
4497         kmem_free(buf, bufsize);
4498         zfsvfs_rele(zfsvfs, FTAG);
4499 
4500         return (error);
4501 }
4502 
4503 /*
4504  * inputs:
4505  * zc_name              name of filesystem
4506  *
4507  * outputs:
4508  * none
4509  */
4510 static int
4511 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4512 {
4513         objset_t *os;
4514         int error = 0;
4515         zfsvfs_t *zfsvfs;
4516 
4517         if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4518                 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
4519                         /*
4520                          * If userused is not enabled, it may be because the
4521                          * objset needs to be closed & reopened (to grow the
4522                          * objset_phys_t).  Suspend/resume the fs will do that.
4523                          */
4524                         error = zfs_suspend_fs(zfsvfs);
4525                         if (error == 0)
4526                                 error = zfs_resume_fs(zfsvfs, zc->zc_name);
4527                 }
4528                 if (error == 0)
4529                         error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
4530                 VFS_RELE(zfsvfs->z_vfs);
4531         } else {
4532                 /* XXX kind of reading contents without owning */
4533                 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4534                 if (error)
4535                         return (error);
4536 
4537                 error = dmu_objset_userspace_upgrade(os);
4538                 dmu_objset_rele(os, FTAG);
4539         }
4540 
4541         return (error);
4542 }
4543 
4544 /*
4545  * We don't want to have a hard dependency
4546  * against some special symbols in sharefs
4547  * nfs, and smbsrv.  Determine them if needed when
4548  * the first file system is shared.
4549  * Neither sharefs, nfs or smbsrv are unloadable modules.
4550  */
4551 int (*znfsexport_fs)(void *arg);
4552 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4553 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4554 
4555 int zfs_nfsshare_inited;
4556 int zfs_smbshare_inited;
4557 
4558 ddi_modhandle_t nfs_mod;
4559 ddi_modhandle_t sharefs_mod;
4560 ddi_modhandle_t smbsrv_mod;
4561 kmutex_t zfs_share_lock;
4562 
4563 static int
4564 zfs_init_sharefs()
4565 {
4566         int error;
4567 
4568         ASSERT(MUTEX_HELD(&zfs_share_lock));
4569         /* Both NFS and SMB shares also require sharetab support. */
4570         if (sharefs_mod == NULL && ((sharefs_mod =
4571             ddi_modopen("fs/sharefs",
4572             KRTLD_MODE_FIRST, &error)) == NULL)) {
4573                 return (ENOSYS);
4574         }
4575         if (zshare_fs == NULL && ((zshare_fs =
4576             (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4577             ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4578                 return (ENOSYS);
4579         }
4580         return (0);
4581 }
4582 
4583 static int
4584 zfs_ioc_share(zfs_cmd_t *zc)
4585 {
4586         int error;
4587         int opcode;
4588 
4589         switch (zc->zc_share.z_sharetype) {
4590         case ZFS_SHARE_NFS:
4591         case ZFS_UNSHARE_NFS:
4592                 if (zfs_nfsshare_inited == 0) {
4593                         mutex_enter(&zfs_share_lock);
4594                         if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4595                             KRTLD_MODE_FIRST, &error)) == NULL)) {
4596                                 mutex_exit(&zfs_share_lock);
4597                                 return (ENOSYS);
4598                         }
4599                         if (znfsexport_fs == NULL &&
4600                             ((znfsexport_fs = (int (*)(void *))
4601                             ddi_modsym(nfs_mod,
4602                             "nfs_export", &error)) == NULL)) {
4603                                 mutex_exit(&zfs_share_lock);
4604                                 return (ENOSYS);
4605                         }
4606                         error = zfs_init_sharefs();
4607                         if (error) {
4608                                 mutex_exit(&zfs_share_lock);
4609                                 return (ENOSYS);
4610                         }
4611                         zfs_nfsshare_inited = 1;
4612                         mutex_exit(&zfs_share_lock);
4613                 }
4614                 break;
4615         case ZFS_SHARE_SMB:
4616         case ZFS_UNSHARE_SMB:
4617                 if (zfs_smbshare_inited == 0) {
4618                         mutex_enter(&zfs_share_lock);
4619                         if (smbsrv_mod == NULL && ((smbsrv_mod =
4620                             ddi_modopen("drv/smbsrv",
4621                             KRTLD_MODE_FIRST, &error)) == NULL)) {
4622                                 mutex_exit(&zfs_share_lock);
4623                                 return (ENOSYS);
4624                         }
4625                         if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4626                             (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4627                             "smb_server_share", &error)) == NULL)) {
4628                                 mutex_exit(&zfs_share_lock);
4629                                 return (ENOSYS);
4630                         }
4631                         error = zfs_init_sharefs();
4632                         if (error) {
4633                                 mutex_exit(&zfs_share_lock);
4634                                 return (ENOSYS);
4635                         }
4636                         zfs_smbshare_inited = 1;
4637                         mutex_exit(&zfs_share_lock);
4638                 }
4639                 break;
4640         default:
4641                 return (EINVAL);
4642         }
4643 
4644         switch (zc->zc_share.z_sharetype) {
4645         case ZFS_SHARE_NFS:
4646         case ZFS_UNSHARE_NFS:
4647                 if (error =
4648                     znfsexport_fs((void *)
4649                     (uintptr_t)zc->zc_share.z_exportdata))
4650                         return (error);
4651                 break;
4652         case ZFS_SHARE_SMB:
4653         case ZFS_UNSHARE_SMB:
4654                 if (error = zsmbexport_fs((void *)
4655                     (uintptr_t)zc->zc_share.z_exportdata,
4656                     zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4657                     B_TRUE: B_FALSE)) {
4658                         return (error);
4659                 }
4660                 break;
4661         }
4662 
4663         opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4664             zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4665             SHAREFS_ADD : SHAREFS_REMOVE;
4666 
4667         /*
4668          * Add or remove share from sharetab
4669          */
4670         error = zshare_fs(opcode,
4671             (void *)(uintptr_t)zc->zc_share.z_sharedata,
4672             zc->zc_share.z_sharemax);
4673 
4674         return (error);
4675 
4676 }
4677 
4678 ace_t full_access[] = {
4679         {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4680 };
4681 
4682 /*
4683  * inputs:
4684  * zc_name              name of containing filesystem
4685  * zc_obj               object # beyond which we want next in-use object #
4686  *
4687  * outputs:
4688  * zc_obj               next in-use object #
4689  */
4690 static int
4691 zfs_ioc_next_obj(zfs_cmd_t *zc)
4692 {
4693         objset_t *os = NULL;
4694         int error;
4695 
4696         error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4697         if (error)
4698                 return (error);
4699 
4700         error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4701             os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
4702 
4703         dmu_objset_rele(os, FTAG);
4704         return (error);
4705 }
4706 
4707 /*
4708  * inputs:
4709  * zc_name              name of filesystem
4710  * zc_value             prefix name for snapshot
4711  * zc_cleanup_fd        cleanup-on-exit file descriptor for calling process
4712  *
4713  * outputs:
4714  * zc_value             short name of new snapshot
4715  */
4716 static int
4717 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
4718 {
4719         char *snap_name;
4720         int error;
4721 
4722         snap_name = kmem_asprintf("%s@%s-%016llx", zc->zc_name, zc->zc_value,
4723             (u_longlong_t)ddi_get_lbolt64());
4724 
4725         if (strlen(snap_name) >= MAXPATHLEN) {
4726                 strfree(snap_name);
4727                 return (E2BIG);
4728         }
4729 
4730         error = dmu_objset_snapshot_tmp(snap_name, "%temp", zc->zc_cleanup_fd);
4731         if (error != 0) {
4732                 strfree(snap_name);
4733                 return (error);
4734         }
4735 
4736         (void) strcpy(zc->zc_value, strchr(snap_name, '@') + 1);
4737         strfree(snap_name);
4738         return (0);
4739 }
4740 
4741 /*
4742  * inputs:
4743  * zc_name              name of "to" snapshot
4744  * zc_value             name of "from" snapshot
4745  * zc_cookie            file descriptor to write diff data on
4746  *
4747  * outputs:
4748  * dmu_diff_record_t's to the file descriptor
4749  */
4750 static int
4751 zfs_ioc_diff(zfs_cmd_t *zc)
4752 {
4753         objset_t *fromsnap;
4754         objset_t *tosnap;
4755         file_t *fp;
4756         offset_t off;
4757         int error;
4758 
4759         error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
4760         if (error)
4761                 return (error);
4762 
4763         error = dmu_objset_hold(zc->zc_value, FTAG, &fromsnap);
4764         if (error) {
4765                 dmu_objset_rele(tosnap, FTAG);
4766                 return (error);
4767         }
4768 
4769         fp = getf(zc->zc_cookie);
4770         if (fp == NULL) {
4771                 dmu_objset_rele(fromsnap, FTAG);
4772                 dmu_objset_rele(tosnap, FTAG);
4773                 return (EBADF);
4774         }
4775 
4776         off = fp->f_offset;
4777 
4778         error = dmu_diff(tosnap, fromsnap, fp->f_vnode, &off);
4779 
4780         if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4781                 fp->f_offset = off;
4782         releasef(zc->zc_cookie);
4783 
4784         dmu_objset_rele(fromsnap, FTAG);
4785         dmu_objset_rele(tosnap, FTAG);
4786         return (error);
4787 }
4788 
4789 /*
4790  * Remove all ACL files in shares dir
4791  */
4792 static int
4793 zfs_smb_acl_purge(znode_t *dzp)
4794 {
4795         zap_cursor_t    zc;
4796         zap_attribute_t zap;
4797         zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4798         int error;
4799 
4800         for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
4801             (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4802             zap_cursor_advance(&zc)) {
4803                 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4804                     NULL, 0)) != 0)
4805                         break;
4806         }
4807         zap_cursor_fini(&zc);
4808         return (error);
4809 }
4810 
4811 static int
4812 zfs_ioc_smb_acl(zfs_cmd_t *zc)
4813 {
4814         vnode_t *vp;
4815         znode_t *dzp;
4816         vnode_t *resourcevp = NULL;
4817         znode_t *sharedir;
4818         zfsvfs_t *zfsvfs;
4819         nvlist_t *nvlist;
4820         char *src, *target;
4821         vattr_t vattr;
4822         vsecattr_t vsec;
4823         int error = 0;
4824 
4825         if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4826             NO_FOLLOW, NULL, &vp)) != 0)
4827                 return (error);
4828 
4829         /* Now make sure mntpnt and dataset are ZFS */
4830 
4831         if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4832             (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4833             zc->zc_name) != 0)) {
4834                 VN_RELE(vp);
4835                 return (EINVAL);
4836         }
4837 
4838         dzp = VTOZ(vp);
4839         zfsvfs = dzp->z_zfsvfs;
4840         ZFS_ENTER(zfsvfs);
4841 
4842         /*
4843          * Create share dir if its missing.
4844          */
4845         mutex_enter(&zfsvfs->z_lock);
4846         if (zfsvfs->z_shares_dir == 0) {
4847                 dmu_tx_t *tx;
4848 
4849                 tx = dmu_tx_create(zfsvfs->z_os);
4850                 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
4851                     ZFS_SHARES_DIR);
4852                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
4853                 error = dmu_tx_assign(tx, TXG_WAIT);
4854                 if (error) {
4855                         dmu_tx_abort(tx);
4856                 } else {
4857                         error = zfs_create_share_dir(zfsvfs, tx);
4858                         dmu_tx_commit(tx);
4859                 }
4860                 if (error) {
4861                         mutex_exit(&zfsvfs->z_lock);
4862                         VN_RELE(vp);
4863                         ZFS_EXIT(zfsvfs);
4864                         return (error);
4865                 }
4866         }
4867         mutex_exit(&zfsvfs->z_lock);
4868 
4869         ASSERT(zfsvfs->z_shares_dir);
4870         if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
4871                 VN_RELE(vp);
4872                 ZFS_EXIT(zfsvfs);
4873                 return (error);
4874         }
4875 
4876         switch (zc->zc_cookie) {
4877         case ZFS_SMB_ACL_ADD:
4878                 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
4879                 vattr.va_type = VREG;
4880                 vattr.va_mode = S_IFREG|0777;
4881                 vattr.va_uid = 0;
4882                 vattr.va_gid = 0;
4883 
4884                 vsec.vsa_mask = VSA_ACE;
4885                 vsec.vsa_aclentp = &full_access;
4886                 vsec.vsa_aclentsz = sizeof (full_access);
4887                 vsec.vsa_aclcnt = 1;
4888 
4889                 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
4890                     &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
4891                 if (resourcevp)
4892                         VN_RELE(resourcevp);
4893                 break;
4894 
4895         case ZFS_SMB_ACL_REMOVE:
4896                 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
4897                     NULL, 0);
4898                 break;
4899 
4900         case ZFS_SMB_ACL_RENAME:
4901                 if ((error = get_nvlist(zc->zc_nvlist_src,
4902                     zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
4903                         VN_RELE(vp);
4904                         ZFS_EXIT(zfsvfs);
4905                         return (error);
4906                 }
4907                 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
4908                     nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
4909                     &target)) {
4910                         VN_RELE(vp);
4911                         VN_RELE(ZTOV(sharedir));
4912                         ZFS_EXIT(zfsvfs);
4913                         nvlist_free(nvlist);
4914                         return (error);
4915                 }
4916                 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
4917                     kcred, NULL, 0);
4918                 nvlist_free(nvlist);
4919                 break;
4920 
4921         case ZFS_SMB_ACL_PURGE:
4922                 error = zfs_smb_acl_purge(sharedir);
4923                 break;
4924 
4925         default:
4926                 error = EINVAL;
4927                 break;
4928         }
4929 
4930         VN_RELE(vp);
4931         VN_RELE(ZTOV(sharedir));
4932 
4933         ZFS_EXIT(zfsvfs);
4934 
4935         return (error);
4936 }
4937 
4938 /*
4939  * inputs:
4940  * zc_name              name of filesystem
4941  * zc_value             short name of snap
4942  * zc_string            user-supplied tag for this hold
4943  * zc_cookie            recursive flag
4944  * zc_temphold          set if hold is temporary
4945  * zc_cleanup_fd        cleanup-on-exit file descriptor for calling process
4946  * zc_sendobj           if non-zero, the objid for zc_name@zc_value
4947  * zc_createtxg         if zc_sendobj is non-zero, snap must have zc_createtxg
4948  *
4949  * outputs:             none
4950  */
4951 static int
4952 zfs_ioc_hold(zfs_cmd_t *zc)
4953 {
4954         boolean_t recursive = zc->zc_cookie;
4955         spa_t *spa;
4956         dsl_pool_t *dp;
4957         dsl_dataset_t *ds;
4958         int error;
4959         minor_t minor = 0;
4960 
4961         if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4962                 return (EINVAL);
4963 
4964         if (zc->zc_sendobj == 0) {
4965                 return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
4966                     zc->zc_string, recursive, zc->zc_temphold,
4967                     zc->zc_cleanup_fd));
4968         }
4969 
4970         if (recursive)
4971                 return (EINVAL);
4972 
4973         error = spa_open(zc->zc_name, &spa, FTAG);
4974         if (error)
4975                 return (error);
4976 
4977         dp = spa_get_dsl(spa);
4978         rw_enter(&dp->dp_config_rwlock, RW_READER);
4979         error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
4980         rw_exit(&dp->dp_config_rwlock);
4981         spa_close(spa, FTAG);
4982         if (error)
4983                 return (error);
4984 
4985         /*
4986          * Until we have a hold on this snapshot, it's possible that
4987          * zc_sendobj could've been destroyed and reused as part
4988          * of a later txg.  Make sure we're looking at the right object.
4989          */
4990         if (zc->zc_createtxg != ds->ds_phys->ds_creation_txg) {
4991                 dsl_dataset_rele(ds, FTAG);
4992                 return (ENOENT);
4993         }
4994 
4995         if (zc->zc_cleanup_fd != -1 && zc->zc_temphold) {
4996                 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
4997                 if (error) {
4998                         dsl_dataset_rele(ds, FTAG);
4999                         return (error);
5000                 }
5001         }
5002 
5003         error = dsl_dataset_user_hold_for_send(ds, zc->zc_string,
5004             zc->zc_temphold);
5005         if (minor != 0) {
5006                 if (error == 0) {
5007                         dsl_register_onexit_hold_cleanup(ds, zc->zc_string,
5008                             minor);
5009                 }
5010                 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5011         }
5012         dsl_dataset_rele(ds, FTAG);
5013 
5014         return (error);
5015 }
5016 
5017 /*
5018  * inputs:
5019  * zc_name      name of dataset from which we're releasing a user hold
5020  * zc_value     short name of snap
5021  * zc_string    user-supplied tag for this hold
5022  * zc_cookie    recursive flag
5023  *
5024  * outputs:     none
5025  */
5026 static int
5027 zfs_ioc_release(zfs_cmd_t *zc)
5028 {
5029         boolean_t recursive = zc->zc_cookie;
5030 
5031         if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
5032                 return (EINVAL);
5033 
5034         return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
5035             zc->zc_string, recursive));
5036 }
5037 
5038 /*
5039  * inputs:
5040  * zc_name              name of filesystem
5041  *
5042  * outputs:
5043  * zc_nvlist_src{_size} nvlist of snapshot holds
5044  */
5045 static int
5046 zfs_ioc_get_holds(zfs_cmd_t *zc)
5047 {
5048         nvlist_t *nvp;
5049         int error;
5050 
5051         if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
5052                 error = put_nvlist(zc, nvp);
5053                 nvlist_free(nvp);
5054         }
5055 
5056         return (error);
5057 }
5058 
5059 /*
5060  * inputs:
5061  * zc_name              name of new filesystem or snapshot
5062  * zc_value             full name of old snapshot
5063  *
5064  * outputs:
5065  * zc_cookie            space in bytes
5066  * zc_objset_type       compressed space in bytes
5067  * zc_perm_action       uncompressed space in bytes
5068  */
5069 static int
5070 zfs_ioc_space_written(zfs_cmd_t *zc)
5071 {
5072         int error;
5073         dsl_dataset_t *new, *old;
5074 
5075         error = dsl_dataset_hold(zc->zc_name, FTAG, &new);
5076         if (error != 0)
5077                 return (error);
5078         error = dsl_dataset_hold(zc->zc_value, FTAG, &old);
5079         if (error != 0) {
5080                 dsl_dataset_rele(new, FTAG);
5081                 return (error);
5082         }
5083 
5084         error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5085             &zc->zc_objset_type, &zc->zc_perm_action);
5086         dsl_dataset_rele(old, FTAG);
5087         dsl_dataset_rele(new, FTAG);
5088         return (error);
5089 }
5090 /*
5091  * innvl: {
5092  *     "firstsnap" -> snapshot name
5093  * }
5094  *
5095  * outnvl: {
5096  *     "used" -> space in bytes
5097  *     "compressed" -> compressed space in bytes
5098  *     "uncompressed" -> uncompressed space in bytes
5099  * }
5100  */
5101 static int
5102 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
5103 {
5104         int error;
5105         dsl_dataset_t *new, *old;
5106         char *firstsnap;
5107         uint64_t used, comp, uncomp;
5108 
5109         if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5110                 return (EINVAL);
5111 
5112         error = dsl_dataset_hold(lastsnap, FTAG, &new);
5113         if (error != 0)
5114                 return (error);
5115         error = dsl_dataset_hold(firstsnap, FTAG, &old);
5116         if (error != 0) {
5117                 dsl_dataset_rele(new, FTAG);
5118                 return (error);
5119         }
5120 
5121         error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5122         dsl_dataset_rele(old, FTAG);
5123         dsl_dataset_rele(new, FTAG);
5124         fnvlist_add_uint64(outnvl, "used", used);
5125         fnvlist_add_uint64(outnvl, "compressed", comp);
5126         fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5127         return (error);
5128 }
5129 
5130 /*
5131  * innvl: {
5132  *     "fd" -> file descriptor to write stream to (int32)
5133  *     (optional) "fromsnap" -> full snap name to send an incremental from
5134  * }
5135  *
5136  * outnvl is unused
5137  */
5138 /* ARGSUSED */
5139 static int
5140 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5141 {
5142         objset_t *fromsnap = NULL;
5143         objset_t *tosnap;
5144         int error;
5145         offset_t off;
5146         char *fromname;
5147         int fd;
5148 
5149         error = nvlist_lookup_int32(innvl, "fd", &fd);
5150         if (error != 0)
5151                 return (EINVAL);
5152 
5153         error = dmu_objset_hold(snapname, FTAG, &tosnap);
5154         if (error)
5155                 return (error);
5156 
5157         error = nvlist_lookup_string(innvl, "fromsnap", &fromname);
5158         if (error == 0) {
5159                 error = dmu_objset_hold(fromname, FTAG, &fromsnap);
5160                 if (error) {
5161                         dmu_objset_rele(tosnap, FTAG);
5162                         return (error);
5163                 }
5164         }
5165 
5166         file_t *fp = getf(fd);
5167         if (fp == NULL) {
5168                 dmu_objset_rele(tosnap, FTAG);
5169                 if (fromsnap != NULL)
5170                         dmu_objset_rele(fromsnap, FTAG);
5171                 return (EBADF);
5172         }
5173 
5174         off = fp->f_offset;
5175         error = dmu_send(tosnap, fromsnap, fd, fp->f_vnode, &off);
5176 
5177         if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5178                 fp->f_offset = off;
5179         releasef(fd);
5180         if (fromsnap != NULL)
5181                 dmu_objset_rele(fromsnap, FTAG);
5182         dmu_objset_rele(tosnap, FTAG);
5183         return (error);
5184 }
5185 
5186 /*
5187  * Determine approximately how large a zfs send stream will be -- the number
5188  * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5189  *
5190  * innvl: {
5191  *     (optional) "fromsnap" -> full snap name to send an incremental from
5192  * }
5193  *
5194  * outnvl: {
5195  *     "space" -> bytes of space (uint64)
5196  * }
5197  */
5198 static int
5199 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5200 {
5201         objset_t *fromsnap = NULL;
5202         objset_t *tosnap;
5203         int error;
5204         char *fromname;
5205         uint64_t space;
5206 
5207         error = dmu_objset_hold(snapname, FTAG, &tosnap);
5208         if (error)
5209                 return (error);
5210 
5211         error = nvlist_lookup_string(innvl, "fromsnap", &fromname);
5212         if (error == 0) {
5213                 error = dmu_objset_hold(fromname, FTAG, &fromsnap);
5214                 if (error) {
5215                         dmu_objset_rele(tosnap, FTAG);
5216                         return (error);
5217                 }
5218         }
5219 
5220         error = dmu_send_estimate(tosnap, fromsnap, &space);
5221         fnvlist_add_uint64(outnvl, "space", space);
5222 
5223         if (fromsnap != NULL)
5224                 dmu_objset_rele(fromsnap, FTAG);
5225         dmu_objset_rele(tosnap, FTAG);
5226         return (error);
5227 }
5228 
5229 
5230 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
5231 
5232 static void
5233 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5234     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5235     boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
5236 {
5237         zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5238 
5239         ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5240         ASSERT3U(ioc, <, ZFS_IOC_LAST);
5241         ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5242         ASSERT3P(vec->zvec_func, ==, NULL);
5243 
5244         vec->zvec_legacy_func = func;
5245         vec->zvec_secpolicy = secpolicy;
5246         vec->zvec_namecheck = namecheck;
5247         vec->zvec_allow_log = log_history;
5248         vec->zvec_pool_check = pool_check;
5249 }
5250 
5251 /*
5252  * See the block comment at the beginning of this file for details on
5253  * each argument to this function.
5254  */
5255 static void
5256 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
5257     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5258     zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
5259     boolean_t allow_log)
5260 {
5261         zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5262 
5263         ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5264         ASSERT3U(ioc, <, ZFS_IOC_LAST);
5265         ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5266         ASSERT3P(vec->zvec_func, ==, NULL);
5267 
5268         /* if we are logging, the name must be valid */
5269         ASSERT(!allow_log || namecheck != NO_NAME);
5270 
5271         vec->zvec_name = name;
5272         vec->zvec_func = func;
5273         vec->zvec_secpolicy = secpolicy;
5274         vec->zvec_namecheck = namecheck;
5275         vec->zvec_pool_check = pool_check;
5276         vec->zvec_smush_outnvlist = smush_outnvlist;
5277         vec->zvec_allow_log = allow_log;
5278 }
5279 
5280 static void
5281 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5282     zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
5283     zfs_ioc_poolcheck_t pool_check)
5284 {
5285         zfs_ioctl_register_legacy(ioc, func, secpolicy,
5286             POOL_NAME, log_history, pool_check);
5287 }
5288 
5289 static void
5290 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5291     zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
5292 {
5293         zfs_ioctl_register_legacy(ioc, func, secpolicy,
5294             DATASET_NAME, B_FALSE, pool_check);
5295 }
5296 
5297 static void
5298 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5299 {
5300         zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
5301             POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5302 }
5303 
5304 static void
5305 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5306     zfs_secpolicy_func_t *secpolicy)
5307 {
5308         zfs_ioctl_register_legacy(ioc, func, secpolicy,
5309             NO_NAME, B_FALSE, POOL_CHECK_NONE);
5310 }
5311 
5312 static void
5313 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
5314     zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
5315 {
5316         zfs_ioctl_register_legacy(ioc, func, secpolicy,
5317             DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
5318 }
5319 
5320 static void
5321 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5322 {
5323         zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
5324             zfs_secpolicy_read);
5325 }
5326 
5327 static void
5328 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5329         zfs_secpolicy_func_t *secpolicy)
5330 {
5331         zfs_ioctl_register_legacy(ioc, func, secpolicy,
5332             DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5333 }
5334 
5335 static void
5336 zfs_ioctl_init(void)
5337 {
5338         zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
5339             zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
5340             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5341 
5342         zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
5343             zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
5344             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
5345 
5346         zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
5347             zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
5348             POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5349 
5350         zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
5351             zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
5352             POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5353 
5354         zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
5355             zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
5356             POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5357 
5358         zfs_ioctl_register("create", ZFS_IOC_CREATE,
5359             zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
5360             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5361 
5362         zfs_ioctl_register("clone", ZFS_IOC_CLONE,
5363             zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
5364             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5365 
5366         zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
5367             zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
5368             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5369 
5370         /* IOCTLS that use the legacy function signature */
5371 
5372         zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
5373             zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
5374 
5375         zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
5376             zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5377         zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
5378             zfs_ioc_pool_scan);
5379         zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
5380             zfs_ioc_pool_upgrade);
5381         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
5382             zfs_ioc_vdev_add);
5383         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
5384             zfs_ioc_vdev_remove);
5385         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
5386             zfs_ioc_vdev_set_state);
5387         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
5388             zfs_ioc_vdev_attach);
5389         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
5390             zfs_ioc_vdev_detach);
5391         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
5392             zfs_ioc_vdev_setpath);
5393         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
5394             zfs_ioc_vdev_setfru);
5395         zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
5396             zfs_ioc_pool_set_props);
5397         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
5398             zfs_ioc_vdev_split);
5399         zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
5400             zfs_ioc_pool_reguid);
5401 
5402         zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
5403             zfs_ioc_pool_configs, zfs_secpolicy_none);
5404         zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
5405             zfs_ioc_pool_tryimport, zfs_secpolicy_config);
5406         zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
5407             zfs_ioc_inject_fault, zfs_secpolicy_inject);
5408         zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
5409             zfs_ioc_clear_fault, zfs_secpolicy_inject);
5410         zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
5411             zfs_ioc_inject_list_next, zfs_secpolicy_inject);
5412 
5413         /*
5414          * pool destroy, and export don't log the history as part of
5415          * zfsdev_ioctl, but rather zfs_ioc_pool_export
5416          * does the logging of those commands.
5417          */
5418         zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
5419             zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5420         zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
5421             zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5422 
5423         zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
5424             zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5425         zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
5426             zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5427 
5428         zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
5429             zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
5430         zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
5431             zfs_ioc_dsobj_to_dsname,
5432             zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
5433         zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
5434             zfs_ioc_pool_get_history,
5435             zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
5436 
5437         zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
5438             zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5439 
5440         zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
5441             zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
5442         zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
5443             zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
5444 
5445         zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
5446             zfs_ioc_space_written);
5447         zfs_ioctl_register_dataset_read(ZFS_IOC_GET_HOLDS,
5448             zfs_ioc_get_holds);
5449         zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
5450             zfs_ioc_objset_recvd_props);
5451         zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
5452             zfs_ioc_next_obj);
5453         zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
5454             zfs_ioc_get_fsacl);
5455         zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
5456             zfs_ioc_objset_stats);
5457         zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
5458             zfs_ioc_objset_zplprops);
5459         zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
5460             zfs_ioc_dataset_list_next);
5461         zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
5462             zfs_ioc_snapshot_list_next);
5463         zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
5464             zfs_ioc_send_progress);
5465 
5466         zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
5467             zfs_ioc_diff, zfs_secpolicy_diff);
5468         zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
5469             zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
5470         zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
5471             zfs_ioc_obj_to_path, zfs_secpolicy_diff);
5472         zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
5473             zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
5474         zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
5475             zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
5476         zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
5477             zfs_ioc_send, zfs_secpolicy_send);
5478 
5479         zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
5480             zfs_secpolicy_none);
5481         zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
5482             zfs_secpolicy_destroy);
5483         zfs_ioctl_register_dataset_modify(ZFS_IOC_ROLLBACK, zfs_ioc_rollback,
5484             zfs_secpolicy_rollback);
5485         zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
5486             zfs_secpolicy_rename);
5487         zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
5488             zfs_secpolicy_recv);
5489         zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
5490             zfs_secpolicy_promote);
5491         zfs_ioctl_register_dataset_modify(ZFS_IOC_HOLD, zfs_ioc_hold,
5492             zfs_secpolicy_hold);
5493         zfs_ioctl_register_dataset_modify(ZFS_IOC_RELEASE, zfs_ioc_release,
5494             zfs_secpolicy_release);
5495         zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
5496             zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
5497         zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
5498             zfs_secpolicy_set_fsacl);
5499 
5500         zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
5501             zfs_secpolicy_share, POOL_CHECK_NONE);
5502         zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
5503             zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
5504         zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
5505             zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
5506             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5507         zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
5508             zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
5509             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5510 }
5511 
5512 int
5513 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5514     zfs_ioc_poolcheck_t check)
5515 {
5516         spa_t *spa;
5517         int error;
5518 
5519         ASSERT(type == POOL_NAME || type == DATASET_NAME);
5520 
5521         if (check & POOL_CHECK_NONE)
5522                 return (0);
5523 
5524         error = spa_open(name, &spa, FTAG);
5525         if (error == 0) {
5526                 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5527                         error = EAGAIN;
5528                 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
5529                         error = EROFS;
5530                 spa_close(spa, FTAG);
5531         }
5532         return (error);
5533 }
5534 
5535 /*
5536  * Find a free minor number.
5537  */
5538 minor_t
5539 zfsdev_minor_alloc(void)
5540 {
5541         static minor_t last_minor;
5542         minor_t m;
5543 
5544         ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5545 
5546         for (m = last_minor + 1; m != last_minor; m++) {
5547                 if (m > ZFSDEV_MAX_MINOR)
5548                         m = 1;
5549                 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
5550                         last_minor = m;
5551                         return (m);
5552                 }
5553         }
5554 
5555         return (0);
5556 }
5557 
5558 static int
5559 zfs_ctldev_init(dev_t *devp)
5560 {
5561         minor_t minor;
5562         zfs_soft_state_t *zs;
5563 
5564         ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5565         ASSERT(getminor(*devp) == 0);
5566 
5567         minor = zfsdev_minor_alloc();
5568         if (minor == 0)
5569                 return (ENXIO);
5570 
5571         if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
5572                 return (EAGAIN);
5573 
5574         *devp = makedevice(getemajor(*devp), minor);
5575 
5576         zs = ddi_get_soft_state(zfsdev_state, minor);
5577         zs->zss_type = ZSST_CTLDEV;
5578         zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
5579 
5580         return (0);
5581 }
5582 
5583 static void
5584 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
5585 {
5586         ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5587 
5588         zfs_onexit_destroy(zo);
5589         ddi_soft_state_free(zfsdev_state, minor);
5590 }
5591 
5592 void *
5593 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
5594 {
5595         zfs_soft_state_t *zp;
5596 
5597         zp = ddi_get_soft_state(zfsdev_state, minor);
5598         if (zp == NULL || zp->zss_type != which)
5599                 return (NULL);
5600 
5601         return (zp->zss_data);
5602 }
5603 
5604 static int
5605 zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
5606 {
5607         int error = 0;
5608 
5609         if (getminor(*devp) != 0)
5610                 return (zvol_open(devp, flag, otyp, cr));
5611 
5612         /* This is the control device. Allocate a new minor if requested. */
5613         if (flag & FEXCL) {
5614                 mutex_enter(&zfsdev_state_lock);
5615                 error = zfs_ctldev_init(devp);
5616                 mutex_exit(&zfsdev_state_lock);
5617         }
5618 
5619         return (error);
5620 }
5621 
5622 static int
5623 zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
5624 {
5625         zfs_onexit_t *zo;
5626         minor_t minor = getminor(dev);
5627 
5628         if (minor == 0)
5629                 return (0);
5630 
5631         mutex_enter(&zfsdev_state_lock);
5632         zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
5633         if (zo == NULL) {
5634                 mutex_exit(&zfsdev_state_lock);
5635                 return (zvol_close(dev, flag, otyp, cr));
5636         }
5637         zfs_ctldev_destroy(zo, minor);
5638         mutex_exit(&zfsdev_state_lock);
5639 
5640         return (0);
5641 }
5642 
5643 static int
5644 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
5645 {
5646         zfs_cmd_t *zc;
5647         uint_t vecnum;
5648         int error, rc, len;
5649         minor_t minor = getminor(dev);
5650         const zfs_ioc_vec_t *vec;
5651         char *saved_poolname = NULL;
5652         nvlist_t *innvl = NULL;
5653 
5654         if (minor != 0 &&
5655             zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
5656                 return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
5657 
5658         vecnum = cmd - ZFS_IOC_FIRST;
5659         ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
5660 
5661         if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
5662                 return (EINVAL);
5663         vec = &zfs_ioc_vec[vecnum];
5664 
5665         zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
5666 
5667         error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
5668         if (error != 0) {
5669                 error = EFAULT;
5670                 goto out;
5671         }
5672 
5673         zc->zc_iflags = flag & FKIOCTL;
5674         if (zc->zc_nvlist_src_size != 0) {
5675                 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
5676                     zc->zc_iflags, &innvl);
5677                 if (error != 0)
5678                         goto out;
5679         }
5680 
5681         /*
5682          * Ensure that all pool/dataset names are valid before we pass down to
5683          * the lower layers.
5684          */
5685         zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5686         switch (vec->zvec_namecheck) {
5687         case POOL_NAME:
5688                 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
5689                         error = EINVAL;
5690                 else
5691                         error = pool_status_check(zc->zc_name,
5692                             vec->zvec_namecheck, vec->zvec_pool_check);
5693                 break;
5694 
5695         case DATASET_NAME:
5696                 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
5697                         error = EINVAL;
5698                 else
5699                         error = pool_status_check(zc->zc_name,
5700                             vec->zvec_namecheck, vec->zvec_pool_check);
5701                 break;
5702 
5703         case NO_NAME:
5704                 break;
5705         }
5706 
5707 
5708         if (error == 0 && !(flag & FKIOCTL))
5709                 error = vec->zvec_secpolicy(zc, innvl, cr);
5710 
5711         if (error != 0)
5712                 goto out;
5713 
5714         /* legacy ioctls can modify zc_name */
5715         len = strcspn(zc->zc_name, "/@") + 1;
5716         saved_poolname = kmem_alloc(len, KM_SLEEP);
5717         (void) strlcpy(saved_poolname, zc->zc_name, len);
5718 
5719         if (vec->zvec_func != NULL) {
5720                 nvlist_t *outnvl;
5721                 int puterror = 0;
5722                 spa_t *spa;
5723                 nvlist_t *lognv = NULL;
5724 
5725                 ASSERT(vec->zvec_legacy_func == NULL);
5726 
5727                 /*
5728                  * Add the innvl to the lognv before calling the func,
5729                  * in case the func changes the innvl.
5730                  */
5731                 if (vec->zvec_allow_log) {
5732                         lognv = fnvlist_alloc();
5733                         fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
5734                             vec->zvec_name);
5735                         if (!nvlist_empty(innvl)) {
5736                                 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
5737                                     innvl);
5738                         }
5739                 }
5740 
5741                 outnvl = fnvlist_alloc();
5742                 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
5743 
5744                 if (error == 0 && vec->zvec_allow_log &&
5745                     spa_open(zc->zc_name, &spa, FTAG) == 0) {
5746                         if (!nvlist_empty(outnvl)) {
5747                                 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
5748                                     outnvl);
5749                         }
5750                         (void) spa_history_log_nvl(spa, lognv);
5751                         spa_close(spa, FTAG);
5752                 }
5753                 fnvlist_free(lognv);
5754 
5755                 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
5756                         int smusherror = 0;
5757                         if (vec->zvec_smush_outnvlist) {
5758                                 smusherror = nvlist_smush(outnvl,
5759                                     zc->zc_nvlist_dst_size);
5760                         }
5761                         if (smusherror == 0)
5762                                 puterror = put_nvlist(zc, outnvl);
5763                 }
5764 
5765                 if (puterror != 0)
5766                         error = puterror;
5767 
5768                 nvlist_free(outnvl);
5769         } else {
5770                 error = vec->zvec_legacy_func(zc);
5771         }
5772 
5773 out:
5774         nvlist_free(innvl);
5775         rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
5776         if (error == 0 && rc != 0)
5777                 error = EFAULT;
5778         if (error == 0 && vec->zvec_allow_log) {
5779                 char *s = tsd_get(zfs_allow_log_key);
5780                 if (s != NULL)
5781                         strfree(s);
5782                 (void) tsd_set(zfs_allow_log_key, saved_poolname);
5783         } else {
5784                 if (saved_poolname != NULL)
5785                         strfree(saved_poolname);
5786         }
5787 
5788         kmem_free(zc, sizeof (zfs_cmd_t));
5789         return (error);
5790 }
5791 
5792 static int
5793 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5794 {
5795         if (cmd != DDI_ATTACH)
5796                 return (DDI_FAILURE);
5797 
5798         if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
5799             DDI_PSEUDO, 0) == DDI_FAILURE)
5800                 return (DDI_FAILURE);
5801 
5802         zfs_dip = dip;
5803 
5804         ddi_report_dev(dip);
5805 
5806         return (DDI_SUCCESS);
5807 }
5808 
5809 static int
5810 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
5811 {
5812         if (spa_busy() || zfs_busy() || zvol_busy())
5813                 return (DDI_FAILURE);
5814 
5815         if (cmd != DDI_DETACH)
5816                 return (DDI_FAILURE);
5817 
5818         zfs_dip = NULL;
5819 
5820         ddi_prop_remove_all(dip);
5821         ddi_remove_minor_node(dip, NULL);
5822 
5823         return (DDI_SUCCESS);
5824 }
5825 
5826 /*ARGSUSED*/
5827 static int
5828 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
5829 {
5830         switch (infocmd) {
5831         case DDI_INFO_DEVT2DEVINFO:
5832                 *result = zfs_dip;
5833                 return (DDI_SUCCESS);
5834 
5835         case DDI_INFO_DEVT2INSTANCE:
5836                 *result = (void *)0;
5837                 return (DDI_SUCCESS);
5838         }
5839 
5840         return (DDI_FAILURE);
5841 }
5842 
5843 /*
5844  * OK, so this is a little weird.
5845  *
5846  * /dev/zfs is the control node, i.e. minor 0.
5847  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
5848  *
5849  * /dev/zfs has basically nothing to do except serve up ioctls,
5850  * so most of the standard driver entry points are in zvol.c.
5851  */
5852 static struct cb_ops zfs_cb_ops = {
5853         zfsdev_open,    /* open */
5854         zfsdev_close,   /* close */
5855         zvol_strategy,  /* strategy */
5856         nodev,          /* print */
5857         zvol_dump,      /* dump */
5858         zvol_read,      /* read */
5859         zvol_write,     /* write */
5860         zfsdev_ioctl,   /* ioctl */
5861         nodev,          /* devmap */
5862         nodev,          /* mmap */
5863         nodev,          /* segmap */
5864         nochpoll,       /* poll */
5865         ddi_prop_op,    /* prop_op */
5866         NULL,           /* streamtab */
5867         D_NEW | D_MP | D_64BIT,         /* Driver compatibility flag */
5868         CB_REV,         /* version */
5869         nodev,          /* async read */
5870         nodev,          /* async write */
5871 };
5872 
5873 static struct dev_ops zfs_dev_ops = {
5874         DEVO_REV,       /* version */
5875         0,              /* refcnt */
5876         zfs_info,       /* info */
5877         nulldev,        /* identify */
5878         nulldev,        /* probe */
5879         zfs_attach,     /* attach */
5880         zfs_detach,     /* detach */
5881         nodev,          /* reset */
5882         &zfs_cb_ops,        /* driver operations */
5883         NULL,           /* no bus operations */
5884         NULL,           /* power */
5885         ddi_quiesce_not_needed, /* quiesce */
5886 };
5887 
5888 static struct modldrv zfs_modldrv = {
5889         &mod_driverops,
5890         "ZFS storage pool",
5891         &zfs_dev_ops
5892 };
5893 
5894 static struct modlinkage modlinkage = {
5895         MODREV_1,
5896         (void *)&zfs_modlfs,
5897         (void *)&zfs_modldrv,
5898         NULL
5899 };
5900 
5901 static void
5902 zfs_allow_log_destroy(void *arg)
5903 {
5904         char *poolname = arg;
5905         strfree(poolname);
5906 }
5907 
5908 int
5909 _init(void)
5910 {
5911         int error;
5912 
5913         spa_init(FREAD | FWRITE);
5914         zfs_init();
5915         zvol_init();
5916         zfs_ioctl_init();
5917 
5918         if ((error = mod_install(&modlinkage)) != 0) {
5919                 zvol_fini();
5920                 zfs_fini();
5921                 spa_fini();
5922                 return (error);
5923         }
5924 
5925         tsd_create(&zfs_fsyncer_key, NULL);
5926         tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
5927         tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
5928 
5929         error = ldi_ident_from_mod(&modlinkage, &zfs_li);
5930         ASSERT(error == 0);
5931         mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
5932 
5933         return (0);
5934 }
5935 
5936 int
5937 _fini(void)
5938 {
5939         int error;
5940 
5941         if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
5942                 return (EBUSY);
5943 
5944         if ((error = mod_remove(&modlinkage)) != 0)
5945                 return (error);
5946 
5947         zvol_fini();
5948         zfs_fini();
5949         spa_fini();
5950         if (zfs_nfsshare_inited)
5951                 (void) ddi_modclose(nfs_mod);
5952         if (zfs_smbshare_inited)
5953                 (void) ddi_modclose(smbsrv_mod);
5954         if (zfs_nfsshare_inited || zfs_smbshare_inited)
5955                 (void) ddi_modclose(sharefs_mod);
5956 
5957         tsd_destroy(&zfs_fsyncer_key);
5958         ldi_ident_release(zfs_li);
5959         zfs_li = NULL;
5960         mutex_destroy(&zfs_share_lock);
5961 
5962         return (error);
5963 }
5964 
5965 int
5966 _info(struct modinfo *modinfop)
5967 {
5968         return (mod_info(&modlinkage, modinfop));
5969 }