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