Home | History | Annotate | Download | only in zfs
      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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  */
     25 
     26 #pragma ident	"@(#)zfs_ioctl.c	1.56	07/12/09 SMI"
     27 
     28 #include <sys/types.h>
     29 #include <sys/param.h>
     30 #include <sys/errno.h>
     31 #include <sys/uio.h>
     32 #include <sys/buf.h>
     33 #include <sys/modctl.h>
     34 #include <sys/open.h>
     35 #include <sys/file.h>
     36 #include <sys/kmem.h>
     37 #include <sys/conf.h>
     38 #include <sys/cmn_err.h>
     39 #include <sys/stat.h>
     40 #include <sys/zfs_ioctl.h>
     41 #include <sys/zfs_znode.h>
     42 #include <sys/zap.h>
     43 #include <sys/spa.h>
     44 #include <sys/spa_impl.h>
     45 #include <sys/vdev.h>
     46 #include <sys/vdev_impl.h>
     47 #include <sys/dmu.h>
     48 #include <sys/dsl_dir.h>
     49 #include <sys/dsl_dataset.h>
     50 #include <sys/dsl_prop.h>
     51 #include <sys/dsl_deleg.h>
     52 #include <sys/dmu_objset.h>
     53 #include <sys/ddi.h>
     54 #include <sys/sunddi.h>
     55 #include <sys/sunldi.h>
     56 #include <sys/policy.h>
     57 #include <sys/zone.h>
     58 #include <sys/nvpair.h>
     59 #include <sys/pathname.h>
     60 #include <sys/mount.h>
     61 #include <sys/sdt.h>
     62 #include <sys/fs/zfs.h>
     63 #include <sys/zfs_ctldir.h>
     64 #include <sys/zfs_dir.h>
     65 #include <sys/zvol.h>
     66 #include <sharefs/share.h>
     67 #include <sys/dmu_objset.h>
     68 
     69 #include "zfs_namecheck.h"
     70 #include "zfs_prop.h"
     71 #include "zfs_deleg.h"
     72 
     73 extern struct modlfs zfs_modlfs;
     74 
     75 extern void zfs_init(void);
     76 extern void zfs_fini(void);
     77 
     78 ldi_ident_t zfs_li = NULL;
     79 dev_info_t *zfs_dip;
     80 
     81 typedef int zfs_ioc_func_t(zfs_cmd_t *);
     82 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
     83 
     84 typedef struct zfs_ioc_vec {
     85 	zfs_ioc_func_t		*zvec_func;
     86 	zfs_secpolicy_func_t	*zvec_secpolicy;
     87 	enum {
     88 		NO_NAME,
     89 		POOL_NAME,
     90 		DATASET_NAME
     91 	} zvec_namecheck;
     92 	boolean_t		zvec_his_log;
     93 } zfs_ioc_vec_t;
     94 
     95 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
     96 void
     97 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
     98 {
     99 	const char *newfile;
    100 	char buf[256];
    101 	va_list adx;
    102 
    103 	/*
    104 	 * Get rid of annoying "../common/" prefix to filename.
    105 	 */
    106 	newfile = strrchr(file, '/');
    107 	if (newfile != NULL) {
    108 		newfile = newfile + 1; /* Get rid of leading / */
    109 	} else {
    110 		newfile = file;
    111 	}
    112 
    113 	va_start(adx, fmt);
    114 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
    115 	va_end(adx);
    116 
    117 	/*
    118 	 * To get this data, use the zfs-dprintf probe as so:
    119 	 * dtrace -q -n 'zfs-dprintf \
    120 	 *	/stringof(arg0) == "dbuf.c"/ \
    121 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
    122 	 * arg0 = file name
    123 	 * arg1 = function name
    124 	 * arg2 = line number
    125 	 * arg3 = message
    126 	 */
    127 	DTRACE_PROBE4(zfs__dprintf,
    128 	    char *, newfile, char *, func, int, line, char *, buf);
    129 }
    130 
    131 static void
    132 history_str_free(char *buf)
    133 {
    134 	kmem_free(buf, HIS_MAX_RECORD_LEN);
    135 }
    136 
    137 static char *
    138 history_str_get(zfs_cmd_t *zc)
    139 {
    140 	char *buf;
    141 
    142 	if (zc->zc_history == NULL)
    143 		return (NULL);
    144 
    145 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
    146 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
    147 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
    148 		history_str_free(buf);
    149 		return (NULL);
    150 	}
    151 
    152 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
    153 
    154 	return (buf);
    155 }
    156 
    157 /*
    158  * zfs_check_version
    159  *
    160  *	Return non-zero if the spa version is less than requested version.
    161  */
    162 static int
    163 zfs_check_version(const char *name, int version)
    164 {
    165 
    166 	spa_t *spa;
    167 
    168 	if (spa_open(name, &spa, FTAG) == 0) {
    169 		if (spa_version(spa) < version) {
    170 			spa_close(spa, FTAG);
    171 			return (1);
    172 		}
    173 		spa_close(spa, FTAG);
    174 	}
    175 	return (0);
    176 }
    177 
    178 static void
    179 zfs_log_history(zfs_cmd_t *zc)
    180 {
    181 	spa_t *spa;
    182 	char *buf;
    183 
    184 	if ((buf = history_str_get(zc)) == NULL)
    185 		return;
    186 
    187 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
    188 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
    189 			(void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
    190 		spa_close(spa, FTAG);
    191 	}
    192 	history_str_free(buf);
    193 }
    194 
    195 /*
    196  * Policy for top-level read operations (list pools).  Requires no privileges,
    197  * and can be used in the local zone, as there is no associated dataset.
    198  */
    199 /* ARGSUSED */
    200 static int
    201 zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
    202 {
    203 	return (0);
    204 }
    205 
    206 /*
    207  * Policy for dataset read operations (list children, get statistics).  Requires
    208  * no privileges, but must be visible in the local zone.
    209  */
    210 /* ARGSUSED */
    211 static int
    212 zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
    213 {
    214 	if (INGLOBALZONE(curproc) ||
    215 	    zone_dataset_visible(zc->zc_name, NULL))
    216 		return (0);
    217 
    218 	return (ENOENT);
    219 }
    220 
    221 static int
    222 zfs_dozonecheck(const char *dataset, cred_t *cr)
    223 {
    224 	uint64_t zoned;
    225 	int writable = 1;
    226 
    227 	/*
    228 	 * The dataset must be visible by this zone -- check this first
    229 	 * so they don't see EPERM on something they shouldn't know about.
    230 	 */
    231 	if (!INGLOBALZONE(curproc) &&
    232 	    !zone_dataset_visible(dataset, &writable))
    233 		return (ENOENT);
    234 
    235 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
    236 		return (ENOENT);
    237 
    238 	if (INGLOBALZONE(curproc)) {
    239 		/*
    240 		 * If the fs is zoned, only root can access it from the
    241 		 * global zone.
    242 		 */
    243 		if (secpolicy_zfs(cr) && zoned)
    244 			return (EPERM);
    245 	} else {
    246 		/*
    247 		 * If we are in a local zone, the 'zoned' property must be set.
    248 		 */
    249 		if (!zoned)
    250 			return (EPERM);
    251 
    252 		/* must be writable by this zone */
    253 		if (!writable)
    254 			return (EPERM);
    255 	}
    256 	return (0);
    257 }
    258 
    259 int
    260 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
    261 {
    262 	int error;
    263 
    264 	error = zfs_dozonecheck(name, cr);
    265 	if (error == 0) {
    266 		error = secpolicy_zfs(cr);
    267 		if (error)
    268 			error = dsl_deleg_access(name, perm, cr);
    269 	}
    270 	return (error);
    271 }
    272 
    273 static int
    274 zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr)
    275 {
    276 	/*
    277 	 * Check permissions for special properties.
    278 	 */
    279 	switch (prop) {
    280 	case ZFS_PROP_ZONED:
    281 		/*
    282 		 * Disallow setting of 'zoned' from within a local zone.
    283 		 */
    284 		if (!INGLOBALZONE(curproc))
    285 			return (EPERM);
    286 		break;
    287 
    288 	case ZFS_PROP_QUOTA:
    289 		if (!INGLOBALZONE(curproc)) {
    290 			uint64_t zoned;
    291 			char setpoint[MAXNAMELEN];
    292 			/*
    293 			 * Unprivileged users are allowed to modify the
    294 			 * quota on things *under* (ie. contained by)
    295 			 * the thing they own.
    296 			 */
    297 			if (dsl_prop_get_integer(name, "zoned", &zoned,
    298 			    setpoint))
    299 				return (EPERM);
    300 			if (!zoned || strlen(name) <= strlen(setpoint))
    301 				return (EPERM);
    302 		}
    303 		break;
    304 	}
    305 
    306 	return (zfs_secpolicy_write_perms(name, zfs_prop_to_name(prop), cr));
    307 }
    308 
    309 int
    310 zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
    311 {
    312 	int error;
    313 
    314 	error = zfs_dozonecheck(zc->zc_name, cr);
    315 	if (error)
    316 		return (error);
    317 
    318 	/*
    319 	 * permission to set permissions will be evaluated later in
    320 	 * dsl_deleg_can_allow()
    321 	 */
    322 	return (0);
    323 }
    324 
    325 int
    326 zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
    327 {
    328 	int error;
    329 	error = zfs_secpolicy_write_perms(zc->zc_name,
    330 	    ZFS_DELEG_PERM_ROLLBACK, cr);
    331 	if (error == 0)
    332 		error = zfs_secpolicy_write_perms(zc->zc_name,
    333 		    ZFS_DELEG_PERM_MOUNT, cr);
    334 	return (error);
    335 }
    336 
    337 int
    338 zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
    339 {
    340 	return (zfs_secpolicy_write_perms(zc->zc_name,
    341 	    ZFS_DELEG_PERM_SEND, cr));
    342 }
    343 
    344 int
    345 zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
    346 {
    347 	if (!INGLOBALZONE(curproc))
    348 		return (EPERM);
    349 
    350 	if (secpolicy_nfs(cr) == 0) {
    351 		return (0);
    352 	} else {
    353 		vnode_t *vp;
    354 		int error;
    355 
    356 		if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
    357 		    NO_FOLLOW, NULL, &vp)) != 0)
    358 			return (error);
    359 
    360 		/* Now make sure mntpnt and dataset are ZFS */
    361 
    362 		if (vp->v_vfsp->vfs_fstype != zfsfstype ||
    363 		    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
    364 		    zc->zc_name) != 0)) {
    365 			VN_RELE(vp);
    366 			return (EPERM);
    367 		}
    368 
    369 		VN_RELE(vp);
    370 		return (dsl_deleg_access(zc->zc_name,
    371 		    ZFS_DELEG_PERM_SHARE, cr));
    372 	}
    373 }
    374 
    375 static int
    376 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
    377 {
    378 	char *cp;
    379 
    380 	/*
    381 	 * Remove the @bla or /bla from the end of the name to get the parent.
    382 	 */
    383 	(void) strncpy(parent, datasetname, parentsize);
    384 	cp = strrchr(parent, '@');
    385 	if (cp != NULL) {
    386 		cp[0] = '\0';
    387 	} else {
    388 		cp = strrchr(parent, '/');
    389 		if (cp == NULL)
    390 			return (ENOENT);
    391 		cp[0] = '\0';
    392 	}
    393 
    394 	return (0);
    395 }
    396 
    397 int
    398 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
    399 {
    400 	int error;
    401 
    402 	if ((error = zfs_secpolicy_write_perms(name,
    403 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
    404 		return (error);
    405 
    406 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
    407 }
    408 
    409 static int
    410 zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
    411 {
    412 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
    413 }
    414 
    415 /*
    416  * Must have sys_config privilege to check the iscsi permission
    417  */
    418 /* ARGSUSED */
    419 static int
    420 zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr)
    421 {
    422 	return (secpolicy_zfs(cr));
    423 }
    424 
    425 int
    426 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
    427 {
    428 	char 	parentname[MAXNAMELEN];
    429 	int	error;
    430 
    431 	if ((error = zfs_secpolicy_write_perms(from,
    432 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
    433 		return (error);
    434 
    435 	if ((error = zfs_secpolicy_write_perms(from,
    436 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
    437 		return (error);
    438 
    439 	if ((error = zfs_get_parent(to, parentname,
    440 	    sizeof (parentname))) != 0)
    441 		return (error);
    442 
    443 	if ((error = zfs_secpolicy_write_perms(parentname,
    444 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
    445 		return (error);
    446 
    447 	if ((error = zfs_secpolicy_write_perms(parentname,
    448 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
    449 		return (error);
    450 
    451 	return (error);
    452 }
    453 
    454 static int
    455 zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
    456 {
    457 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
    458 }
    459 
    460 static int
    461 zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
    462 {
    463 	char 	parentname[MAXNAMELEN];
    464 	objset_t *clone;
    465 	int error;
    466 
    467 	error = zfs_secpolicy_write_perms(zc->zc_name,
    468 	    ZFS_DELEG_PERM_PROMOTE, cr);
    469 	if (error)
    470 		return (error);
    471 
    472 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
    473 	    DS_MODE_STANDARD | DS_MODE_READONLY, &clone);
    474 
    475 	if (error == 0) {
    476 		dsl_dataset_t *pclone = NULL;
    477 		dsl_dir_t *dd;
    478 		dd = clone->os->os_dsl_dataset->ds_dir;
    479 
    480 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
    481 		error = dsl_dataset_open_obj(dd->dd_pool,
    482 		    dd->dd_phys->dd_origin_obj, NULL,
    483 		    DS_MODE_NONE, FTAG, &pclone);
    484 		rw_exit(&dd->dd_pool->dp_config_rwlock);
    485 		if (error) {
    486 			dmu_objset_close(clone);
    487 			return (error);
    488 		}
    489 
    490 		error = zfs_secpolicy_write_perms(zc->zc_name,
    491 		    ZFS_DELEG_PERM_MOUNT, cr);
    492 
    493 		dsl_dataset_name(pclone, parentname);
    494 		dmu_objset_close(clone);
    495 		dsl_dataset_close(pclone, DS_MODE_NONE, FTAG);
    496 		if (error == 0)
    497 			error = zfs_secpolicy_write_perms(parentname,
    498 			    ZFS_DELEG_PERM_PROMOTE, cr);
    499 	}
    500 	return (error);
    501 }
    502 
    503 static int
    504 zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
    505 {
    506 	int error;
    507 
    508 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
    509 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
    510 		return (error);
    511 
    512 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
    513 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
    514 		return (error);
    515 
    516 	return (zfs_secpolicy_write_perms(zc->zc_name,
    517 	    ZFS_DELEG_PERM_CREATE, cr));
    518 }
    519 
    520 int
    521 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
    522 {
    523 	int error;
    524 
    525 	if ((error = zfs_secpolicy_write_perms(name,
    526 	    ZFS_DELEG_PERM_SNAPSHOT, cr)) != 0)
    527 		return (error);
    528 
    529 	error = zfs_secpolicy_write_perms(name,
    530 	    ZFS_DELEG_PERM_MOUNT, cr);
    531 
    532 	return (error);
    533 }
    534 
    535 static int
    536 zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
    537 {
    538 
    539 	return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
    540 }
    541 
    542 static int
    543 zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
    544 {
    545 	char 	parentname[MAXNAMELEN];
    546 	int 	error;
    547 
    548 	if ((error = zfs_get_parent(zc->zc_name, parentname,
    549 	    sizeof (parentname))) != 0)
    550 		return (error);
    551 
    552 	if (zc->zc_value[0] != '\0') {
    553 		if ((error = zfs_secpolicy_write_perms(zc->zc_value,
    554 		    ZFS_DELEG_PERM_CLONE, cr)) != 0)
    555 			return (error);
    556 	}
    557 
    558 	if ((error = zfs_secpolicy_write_perms(parentname,
    559 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
    560 		return (error);
    561 
    562 	error = zfs_secpolicy_write_perms(parentname,
    563 	    ZFS_DELEG_PERM_MOUNT, cr);
    564 
    565 	return (error);
    566 }
    567 
    568 static int
    569 zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
    570 {
    571 	int error;
    572 
    573 	error = secpolicy_fs_unmount(cr, NULL);
    574 	if (error) {
    575 		error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
    576 	}
    577 	return (error);
    578 }
    579 
    580 /*
    581  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
    582  * SYS_CONFIG privilege, which is not available in a local zone.
    583  */
    584 /* ARGSUSED */
    585 static int
    586 zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
    587 {
    588 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
    589 		return (EPERM);
    590 
    591 	return (0);
    592 }
    593 
    594 /*
    595  * Just like zfs_secpolicy_config, except that we will check for
    596  * mount permission on the dataset for permission to create/remove
    597  * the minor nodes.
    598  */
    599 static int
    600 zfs_secpolicy_minor(zfs_cmd_t *zc, cred_t *cr)
    601 {
    602 	if (secpolicy_sys_config(cr, B_FALSE) != 0) {
    603 		return (dsl_deleg_access(zc->zc_name,
    604 		    ZFS_DELEG_PERM_MOUNT, cr));
    605 	}
    606 
    607 	return (0);
    608 }
    609 
    610 /*
    611  * Policy for fault injection.  Requires all privileges.
    612  */
    613 /* ARGSUSED */
    614 static int
    615 zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
    616 {
    617 	return (secpolicy_zinject(cr));
    618 }
    619 
    620 static int
    621 zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
    622 {
    623 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
    624 
    625 	if (prop == ZPROP_INVAL) {
    626 		if (!zfs_prop_user(zc->zc_value))
    627 			return (EINVAL);
    628 		return (zfs_secpolicy_write_perms(zc->zc_name,
    629 		    ZFS_DELEG_PERM_USERPROP, cr));
    630 	} else {
    631 		if (!zfs_prop_inheritable(prop))
    632 			return (EINVAL);
    633 		return (zfs_secpolicy_setprop(zc->zc_name, prop, cr));
    634 	}
    635 }
    636 
    637 /*
    638  * Returns the nvlist as specified by the user in the zfs_cmd_t.
    639  */
    640 static int
    641 get_nvlist(uint64_t nvl, uint64_t size, nvlist_t **nvp)
    642 {
    643 	char *packed;
    644 	int error;
    645 	nvlist_t *list = NULL;
    646 
    647 	/*
    648 	 * Read in and unpack the user-supplied nvlist.
    649 	 */
    650 	if (size == 0)
    651 		return (EINVAL);
    652 
    653 	packed = kmem_alloc(size, KM_SLEEP);
    654 
    655 	if ((error = xcopyin((void *)(uintptr_t)nvl, packed, size)) != 0) {
    656 		kmem_free(packed, size);
    657 		return (error);
    658 	}
    659 
    660 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
    661 		kmem_free(packed, size);
    662 		return (error);
    663 	}
    664 
    665 	kmem_free(packed, size);
    666 
    667 	*nvp = list;
    668 	return (0);
    669 }
    670 
    671 static int
    672 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
    673 {
    674 	char *packed = NULL;
    675 	size_t size;
    676 	int error;
    677 
    678 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
    679 
    680 	if (size > zc->zc_nvlist_dst_size) {
    681 		error = ENOMEM;
    682 	} else {
    683 		packed = kmem_alloc(size, KM_SLEEP);
    684 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
    685 		    KM_SLEEP) == 0);
    686 		error = xcopyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
    687 		    size);
    688 		kmem_free(packed, size);
    689 	}
    690 
    691 	zc->zc_nvlist_dst_size = size;
    692 	return (error);
    693 }
    694 
    695 static int
    696 zfs_ioc_pool_create(zfs_cmd_t *zc)
    697 {
    698 	int error;
    699 	nvlist_t *config, *props = NULL;
    700 	char *buf;
    701 
    702 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
    703 	    &config))
    704 		return (error);
    705 
    706 	if (zc->zc_nvlist_src_size != 0 && (error =
    707 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) {
    708 		nvlist_free(config);
    709 		return (error);
    710 	}
    711 
    712 	buf = history_str_get(zc);
    713 
    714 	error = spa_create(zc->zc_name, config, props, buf);
    715 
    716 	if (buf != NULL)
    717 		history_str_free(buf);
    718 
    719 	nvlist_free(config);
    720 
    721 	if (props)
    722 		nvlist_free(props);
    723 
    724 	return (error);
    725 }
    726 
    727 static int
    728 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
    729 {
    730 	int error;
    731 	zfs_log_history(zc);
    732 	error = spa_destroy(zc->zc_name);
    733 	return (error);
    734 }
    735 
    736 static int
    737 zfs_ioc_pool_import(zfs_cmd_t *zc)
    738 {
    739 	int error;
    740 	nvlist_t *config, *props = NULL;
    741 	uint64_t guid;
    742 
    743 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
    744 	    &config)) != 0)
    745 		return (error);
    746 
    747 	if (zc->zc_nvlist_src_size != 0 && (error =
    748 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) {
    749 		nvlist_free(config);
    750 		return (error);
    751 	}
    752 
    753 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
    754 	    guid != zc->zc_guid)
    755 		error = EINVAL;
    756 	else
    757 		error = spa_import(zc->zc_name, config, props);
    758 
    759 	nvlist_free(config);
    760 
    761 	if (props)
    762 		nvlist_free(props);
    763 
    764 	return (error);
    765 }
    766 
    767 static int
    768 zfs_ioc_pool_export(zfs_cmd_t *zc)
    769 {
    770 	int error;
    771 	zfs_log_history(zc);
    772 	error = spa_export(zc->zc_name, NULL);
    773 	return (error);
    774 }
    775 
    776 static int
    777 zfs_ioc_pool_configs(zfs_cmd_t *zc)
    778 {
    779 	nvlist_t *configs;
    780 	int error;
    781 
    782 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
    783 		return (EEXIST);
    784 
    785 	error = put_nvlist(zc, configs);
    786 
    787 	nvlist_free(configs);
    788 
    789 	return (error);
    790 }
    791 
    792 static int
    793 zfs_ioc_pool_stats(zfs_cmd_t *zc)
    794 {
    795 	nvlist_t *config;
    796 	int error;
    797 	int ret = 0;
    798 
    799 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
    800 	    sizeof (zc->zc_value));
    801 
    802 	if (config != NULL) {
    803 		ret = put_nvlist(zc, config);
    804 		nvlist_free(config);
    805 
    806 		/*
    807 		 * The config may be present even if 'error' is non-zero.
    808 		 * In this case we return success, and preserve the real errno
    809 		 * in 'zc_cookie'.
    810 		 */
    811 		zc->zc_cookie = error;
    812 	} else {
    813 		ret = error;
    814 	}
    815 
    816 	return (ret);
    817 }
    818 
    819 /*
    820  * Try to import the given pool, returning pool stats as appropriate so that
    821  * user land knows which devices are available and overall pool health.
    822  */
    823 static int
    824 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
    825 {
    826 	nvlist_t *tryconfig, *config;
    827 	int error;
    828 
    829 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
    830 	    &tryconfig)) != 0)
    831 		return (error);
    832 
    833 	config = spa_tryimport(tryconfig);
    834 
    835 	nvlist_free(tryconfig);
    836 
    837 	if (config == NULL)
    838 		return (EINVAL);
    839 
    840 	error = put_nvlist(zc, config);
    841 	nvlist_free(config);
    842 
    843 	return (error);
    844 }
    845 
    846 static int
    847 zfs_ioc_pool_scrub(zfs_cmd_t *zc)
    848 {
    849 	spa_t *spa;
    850 	int error;
    851 
    852 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
    853 		return (error);
    854 
    855 	mutex_enter(&spa_namespace_lock);
    856 	error = spa_scrub(spa, zc->zc_cookie, B_FALSE);
    857 	mutex_exit(&spa_namespace_lock);
    858 
    859 	spa_close(spa, FTAG);
    860 
    861 	return (error);
    862 }
    863 
    864 static int
    865 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
    866 {
    867 	spa_t *spa;
    868 	int error;
    869 
    870 	error = spa_open(zc->zc_name, &spa, FTAG);
    871 	if (error == 0) {
    872 		spa_freeze(spa);
    873 		spa_close(spa, FTAG);
    874 	}
    875 	return (error);
    876 }
    877 
    878 static int
    879 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
    880 {
    881 	spa_t *spa;
    882 	int error;
    883 
    884 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
    885 		return (error);
    886 
    887 	if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
    888 		spa_close(spa, FTAG);
    889 		return (EINVAL);
    890 	}
    891 
    892 	spa_upgrade(spa, zc->zc_cookie);
    893 	spa_close(spa, FTAG);
    894 
    895 	return (error);
    896 }
    897 
    898 static int
    899 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
    900 {
    901 	spa_t *spa;
    902 	char *hist_buf;
    903 	uint64_t size;
    904 	int error;
    905 
    906 	if ((size = zc->zc_history_len) == 0)
    907 		return (EINVAL);
    908 
    909 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
    910 		return (error);
    911 
    912 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
    913 		spa_close(spa, FTAG);
    914 		return (ENOTSUP);
    915 	}
    916 
    917 	hist_buf = kmem_alloc(size, KM_SLEEP);
    918 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
    919 	    &zc->zc_history_len, hist_buf)) == 0) {
    920 		error = xcopyout(hist_buf,
    921 		    (char *)(uintptr_t)zc->zc_history,
    922 		    zc->zc_history_len);
    923 	}
    924 
    925 	spa_close(spa, FTAG);
    926 	kmem_free(hist_buf, size);
    927 	return (error);
    928 }
    929 
    930 static int
    931 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
    932 {
    933 	int error;
    934 
    935 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
    936 		return (error);
    937 
    938 	return (0);
    939 }
    940 
    941 static int
    942 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
    943 {
    944 	objset_t *osp;
    945 	int error;
    946 
    947 	if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS,
    948 	    DS_MODE_NONE | DS_MODE_READONLY, &osp)) != 0)
    949 		return (error);
    950 
    951 	error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value,
    952 	    sizeof (zc->zc_value));
    953 	dmu_objset_close(osp);
    954 
    955 	return (error);
    956 }
    957 
    958 static int
    959 zfs_ioc_vdev_add(zfs_cmd_t *zc)
    960 {
    961 	spa_t *spa;
    962 	int error;
    963 	nvlist_t *config, **l2cache;
    964 	uint_t nl2cache;
    965 
    966 	error = spa_open(zc->zc_name, &spa, FTAG);
    967 	if (error != 0)
    968 		return (error);
    969 
    970 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
    971 	    &config);
    972 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
    973 	    &l2cache, &nl2cache);
    974 
    975 	/*
    976 	 * A root pool with concatenated devices is not supported.
    977 	 * Thus, can not add a device to a root pool with one device.
    978 	 * Allow for l2cache devices to be added.
    979 	 */
    980 	if (spa->spa_root_vdev->vdev_children == 1 && spa->spa_bootfs != 0 &&
    981 	    nl2cache == 0) {
    982 		spa_close(spa, FTAG);
    983 		return (EDOM);
    984 	}
    985 
    986 	if (error == 0) {
    987 		error = spa_vdev_add(spa, config);
    988 		nvlist_free(config);
    989 	}
    990 	spa_close(spa, FTAG);
    991 	return (error);
    992 }
    993 
    994 static int
    995 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
    996 {
    997 	spa_t *spa;
    998 	int error;
    999 
   1000 	error = spa_open(zc->zc_name, &spa, FTAG);
   1001 	if (error != 0)
   1002 		return (error);
   1003 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
   1004 	spa_close(spa, FTAG);
   1005 	return (error);
   1006 }
   1007 
   1008 static int
   1009 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
   1010 {
   1011 	spa_t *spa;
   1012 	int error;
   1013 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
   1014 
   1015 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
   1016 		return (error);
   1017 	switch (zc->zc_cookie) {
   1018 	case VDEV_STATE_ONLINE:
   1019 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
   1020 		break;
   1021 
   1022 	case VDEV_STATE_OFFLINE:
   1023 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
   1024 		break;
   1025 
   1026 	case VDEV_STATE_FAULTED:
   1027 		error = vdev_fault(spa, zc->zc_guid);
   1028 		break;
   1029 
   1030 	case VDEV_STATE_DEGRADED:
   1031 		error = vdev_degrade(spa, zc->zc_guid);
   1032 		break;
   1033 
   1034 	default:
   1035 		error = EINVAL;
   1036 	}
   1037 	zc->zc_cookie = newstate;
   1038 	spa_close(spa, FTAG);
   1039 	return (error);
   1040 }
   1041 
   1042 static int
   1043 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
   1044 {
   1045 	spa_t *spa;
   1046 	int replacing = zc->zc_cookie;
   1047 	nvlist_t *config;
   1048 	int error;
   1049 
   1050 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
   1051 		return (error);
   1052 
   1053 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
   1054 	    &config)) == 0) {
   1055 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
   1056 		nvlist_free(config);
   1057 	}
   1058 
   1059 	spa_close(spa, FTAG);
   1060 	return (error);
   1061 }
   1062 
   1063 static int
   1064 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
   1065 {
   1066 	spa_t *spa;
   1067 	int error;
   1068 
   1069 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
   1070 		return (error);
   1071 
   1072 	error = spa_vdev_detach(spa, zc->zc_guid, B_FALSE);
   1073 
   1074 	spa_close(spa, FTAG);
   1075 	return (error);
   1076 }
   1077 
   1078 static int
   1079 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
   1080 {
   1081 	spa_t *spa;
   1082 	char *path = zc->zc_value;
   1083 	uint64_t guid = zc->zc_guid;
   1084 	int error;
   1085 
   1086 	error = spa_open(zc->zc_name, &spa, FTAG);
   1087 	if (error != 0)
   1088 		return (error);
   1089 
   1090 	error = spa_vdev_setpath(spa, guid, path);
   1091 	spa_close(spa, FTAG);
   1092 	return (error);
   1093 }
   1094 
   1095 static int
   1096 zfs_os_open_retry(char *name, objset_t **os)
   1097 {
   1098 	int error;
   1099 
   1100 retry:
   1101 	error = dmu_objset_open(name, DMU_OST_ANY,
   1102 	    DS_MODE_STANDARD | DS_MODE_READONLY, os);
   1103 	if (error != 0) {
   1104 		/*
   1105 		 * This is ugly: dmu_objset_open() can return EBUSY if
   1106 		 * the objset is held exclusively. Fortunately this hold is
   1107 		 * only for a short while, so we retry here.
   1108 		 * This avoids user code having to handle EBUSY,
   1109 		 * for example for a "zfs list".
   1110 		 */
   1111 		if (error == EBUSY) {
   1112 			delay(1);
   1113 			goto retry;
   1114 		}
   1115 	}
   1116 	return (error);
   1117 }
   1118 
   1119 /*
   1120  * inputs:
   1121  * zc_name		name of filesystem
   1122  * zc_nvlist_dst_size	size of buffer for property nvlist
   1123  *
   1124  * outputs:
   1125  * zc_objset_stats	stats
   1126  * zc_nvlist_dst	property nvlist
   1127  * zc_nvlist_dst_size	size of property nvlist
   1128  * zc_value		alternate root
   1129  */
   1130 static int
   1131 zfs_ioc_objset_stats(zfs_cmd_t *zc)
   1132 {
   1133 	objset_t *os = NULL;
   1134 	int error;
   1135 	nvlist_t *nv;
   1136 
   1137 	if ((error = zfs_os_open_retry(zc->zc_name, &os)) != 0)
   1138 		return (error);
   1139 
   1140 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
   1141 
   1142 	if (zc->zc_nvlist_dst != 0 &&
   1143 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
   1144 		dmu_objset_stats(os, nv);
   1145 		/*
   1146 		 * NB: zvol_get_stats() will read the objset contents,
   1147 		 * which we aren't supposed to do with a
   1148 		 * DS_MODE_STANDARD open, because it could be
   1149 		 * inconsistent.  So this is a bit of a workaround...
   1150 		 */
   1151 		if (!zc->zc_objset_stats.dds_inconsistent) {
   1152 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
   1153 				VERIFY(zvol_get_stats(os, nv) == 0);
   1154 		}
   1155 		error = put_nvlist(zc, nv);
   1156 		nvlist_free(nv);
   1157 	}
   1158 
   1159 	spa_altroot(dmu_objset_spa(os), zc->zc_value, sizeof (zc->zc_value));
   1160 
   1161 	dmu_objset_close(os);
   1162 	return (error);
   1163 }
   1164 
   1165 static int
   1166 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
   1167 {
   1168 	uint64_t value;
   1169 	int error;
   1170 
   1171 	/*
   1172 	 * zfs_get_zplprop() will either find a value or give us
   1173 	 * the default value (if there is one).
   1174 	 */
   1175 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
   1176 		return (error);
   1177 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
   1178 	return (0);
   1179 }
   1180 
   1181 /*
   1182  * inputs:
   1183  * zc_name		name of filesystem
   1184  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
   1185  *
   1186  * outputs:
   1187  * zc_nvlist_dst	zpl property nvlist
   1188  * zc_nvlist_dst_size	size of zpl property nvlist
   1189  */
   1190 static int
   1191 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
   1192 {
   1193 	objset_t *os;
   1194 	int err;
   1195 
   1196 	if ((err = zfs_os_open_retry(zc->zc_name, &os)) != 0)
   1197 		return (err);
   1198 
   1199 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
   1200 
   1201 	/*
   1202 	 * NB: nvl_add_zplprop() will read the objset contents,
   1203 	 * which we aren't supposed to do with a DS_MODE_STANDARD
   1204 	 * open, because it could be inconsistent.
   1205 	 */
   1206 	if (zc->zc_nvlist_dst != NULL &&
   1207 	    !zc->zc_objset_stats.dds_inconsistent &&
   1208 	    dmu_objset_type(os) == DMU_OST_ZFS) {
   1209 		nvlist_t *nv;
   1210 
   1211 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
   1212 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
   1213 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
   1214 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
   1215 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
   1216 			err = put_nvlist(zc, nv);
   1217 		nvlist_free(nv);
   1218 	} else {
   1219 		err = ENOENT;
   1220 	}
   1221 	dmu_objset_close(os);
   1222 	return (err);
   1223 }
   1224 
   1225 /*
   1226  * inputs:
   1227  * zc_name		name of filesystem
   1228  * zc_cookie		zap cursor
   1229  * zc_nvlist_dst_size	size of buffer for property nvlist
   1230  *
   1231  * outputs:
   1232  * zc_name		name of next filesystem
   1233  * zc_objset_stats	stats
   1234  * zc_nvlist_dst	property nvlist
   1235  * zc_nvlist_dst_size	size of property nvlist
   1236  * zc_value		alternate root
   1237  */
   1238 static int
   1239 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
   1240 {
   1241 	objset_t *os;
   1242 	int error;
   1243 	char *p;
   1244 
   1245 	if ((error = zfs_os_open_retry(zc->zc_name, &os)) != 0) {
   1246 		if (error == ENOENT)
   1247 			error = ESRCH;
   1248 		return (error);
   1249 	}
   1250 
   1251 	p = strrchr(zc->zc_name, '/');
   1252 	if (p == NULL || p[1] != '\0')
   1253 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
   1254 	p = zc->zc_name + strlen(zc->zc_name);
   1255 
   1256 	do {
   1257 		error = dmu_dir_list_next(os,
   1258 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
   1259 		    NULL, &zc->zc_cookie);
   1260 		if (error == ENOENT)
   1261 			error = ESRCH;
   1262 	} while (error == 0 && !INGLOBALZONE(curproc) &&
   1263 	    !zone_dataset_visible(zc->zc_name, NULL));
   1264 
   1265 	/*
   1266 	 * If it's a hidden dataset (ie. with a '$' in its name), don't
   1267 	 * try to get stats for it.  Userland will skip over it.
   1268 	 */
   1269 	if (error == 0 && strchr(zc->zc_name, '$') == NULL)
   1270 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
   1271 
   1272 	dmu_objset_close(os);
   1273 	return (error);
   1274 }
   1275 
   1276 /*
   1277  * inputs:
   1278  * zc_name		name of filesystem
   1279  * zc_cookie		zap cursor
   1280  * zc_nvlist_dst_size	size of buffer for property nvlist
   1281  *
   1282  * outputs:
   1283  * zc_name		name of next snapshot
   1284  * zc_objset_stats	stats
   1285  * zc_nvlist_dst	property nvlist
   1286  * zc_nvlist_dst_size	size of property nvlist
   1287  * zc_value		alternate root
   1288  */
   1289 static int
   1290 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
   1291 {
   1292 	objset_t *os;
   1293 	int error;
   1294 
   1295 	if ((error = zfs_os_open_retry(zc->zc_name, &os)) != 0) {
   1296 		if (error == ENOENT)
   1297 			error = ESRCH;
   1298 		return (error);
   1299 	}
   1300 
   1301 	/*
   1302 	 * A dataset name of maximum length cannot have any snapshots,
   1303 	 * so exit immediately.
   1304 	 */
   1305 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
   1306 		dmu_objset_close(os);
   1307 		return (ESRCH);
   1308 	}
   1309 
   1310 	error = dmu_snapshot_list_next(os,
   1311 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
   1312 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
   1313 	if (error == ENOENT)
   1314 		error = ESRCH;
   1315 
   1316 	if (error == 0)
   1317 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
   1318 
   1319 	/* if we failed, undo the @ that we tacked on to zc_name */
   1320 	if (error != 0)
   1321 		*strchr(zc->zc_name, '@') = '\0';
   1322 
   1323 	dmu_objset_close(os);
   1324 	return (error);
   1325 }
   1326 
   1327 static int
   1328 zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
   1329 {
   1330 	nvpair_t *elem;
   1331 	int error;
   1332 	uint64_t intval;
   1333 	char *strval;
   1334 
   1335 	/*
   1336 	 * First validate permission to set all of the properties
   1337 	 */
   1338 	elem = NULL;
   1339 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
   1340 		const char *propname = nvpair_name(elem);
   1341 		zfs_prop_t prop = zfs_name_to_prop(propname);
   1342 
   1343 		if (prop == ZPROP_INVAL) {
   1344 			/*
   1345 			 * If this is a user-defined property, it must be a
   1346 			 * string, and there is no further validation to do.
   1347 			 */
   1348 			if (!zfs_prop_user(propname) ||
   1349 			    nvpair_type(elem) != DATA_TYPE_STRING)
   1350 				return (EINVAL);
   1351 
   1352 			if (error = zfs_secpolicy_write_perms(name,
   1353 			    ZFS_DELEG_PERM_USERPROP, CRED()))
   1354 				return (error);
   1355 			continue;
   1356 		}
   1357 
   1358 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
   1359 			return (error);
   1360 
   1361 		/*
   1362 		 * Check that this value is valid for this pool version
   1363 		 */
   1364 		switch (prop) {
   1365 		case ZFS_PROP_COMPRESSION:
   1366 			/*
   1367 			 * If the user specified gzip compression, make sure
   1368 			 * the SPA supports it. We ignore any errors here since
   1369 			 * we'll catch them later.
   1370 			 */
   1371 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
   1372 			    nvpair_value_uint64(elem, &intval) == 0 &&
   1373 			    intval >= ZIO_COMPRESS_GZIP_1 &&
   1374 			    intval <= ZIO_COMPRESS_GZIP_9) {
   1375 				if (zfs_check_version(name,
   1376 				    SPA_VERSION_GZIP_COMPRESSION))
   1377 					return (ENOTSUP);
   1378 			}
   1379 			break;
   1380 
   1381 		case ZFS_PROP_COPIES:
   1382 			if (zfs_check_version(name, SPA_VERSION_DITTO_BLOCKS))
   1383 				return (ENOTSUP);
   1384 			break;
   1385 		}
   1386 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
   1387 			return (error);
   1388 	}
   1389 
   1390 	elem = NULL;
   1391 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
   1392 		const char *propname = nvpair_name(elem);
   1393 		zfs_prop_t prop = zfs_name_to_prop(propname);
   1394 
   1395 		if (prop == ZPROP_INVAL) {
   1396 			VERIFY(nvpair_value_string(elem, &strval) == 0);
   1397 			error = dsl_prop_set(name, propname, 1,
   1398 			    strlen(strval) + 1, strval);
   1399 			if (error == 0)
   1400 				continue;
   1401 			else
   1402 				return (error);
   1403 		}
   1404 
   1405 		switch (prop) {
   1406 		case ZFS_PROP_QUOTA:
   1407 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
   1408 			    (error = dsl_dir_set_quota(name, intval)) != 0)
   1409 				return (error);
   1410 			break;
   1411 
   1412 		case ZFS_PROP_REFQUOTA:
   1413 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
   1414 			    (error = dsl_dataset_set_quota(name, intval)) != 0)
   1415 				return (error);
   1416 			break;
   1417 
   1418 		case ZFS_PROP_RESERVATION:
   1419 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
   1420 			    (error = dsl_dir_set_reservation(name,
   1421 			    intval)) != 0)
   1422 				return (error);
   1423 			break;
   1424 
   1425 		case ZFS_PROP_REFRESERVATION:
   1426 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
   1427 			    (error = dsl_dataset_set_reservation(name,
   1428 			    intval)) != 0)
   1429 				return (error);
   1430 			break;
   1431 
   1432 		case ZFS_PROP_VOLSIZE:
   1433 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
   1434 			    (error = zvol_set_volsize(name,
   1435 			    ddi_driver_major(zfs_dip), intval)) != 0)
   1436 				return (error);
   1437 			break;
   1438 
   1439 		case ZFS_PROP_VOLBLOCKSIZE:
   1440 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
   1441 			    (error = zvol_set_volblocksize(name, intval)) != 0)
   1442 				return (error);
   1443 			break;
   1444 
   1445 		case ZFS_PROP_VERSION:
   1446 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
   1447 			    (error = zfs_set_version(name, intval)) != 0)
   1448 				return (error);
   1449 			break;
   1450 
   1451 		default:
   1452 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
   1453 				if (zfs_prop_get_type(prop) !=
   1454 				    PROP_TYPE_STRING)
   1455 					return (EINVAL);
   1456 				VERIFY(nvpair_value_string(elem, &strval) == 0);
   1457 				if ((error = dsl_prop_set(name,
   1458 				    nvpair_name(elem), 1, strlen(strval) + 1,
   1459 				    strval)) != 0)
   1460 					return (error);
   1461 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
   1462 				const char *unused;
   1463 
   1464 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
   1465 
   1466 				switch (zfs_prop_get_type(prop)) {
   1467 				case PROP_TYPE_NUMBER:
   1468 					break;
   1469 				case PROP_TYPE_STRING:
   1470 					return (EINVAL);
   1471 				case PROP_TYPE_INDEX:
   1472 					if (zfs_prop_index_to_string(prop,
   1473 					    intval, &unused) != 0)
   1474 						return (EINVAL);
   1475 					break;
   1476 				default:
   1477 					cmn_err(CE_PANIC,
   1478 					    "unknown property type");
   1479 					break;
   1480 				}
   1481 
   1482 				if ((error = dsl_prop_set(name, propname,
   1483 				    8, 1, &intval)) != 0)
   1484 					return (error);
   1485 			} else {
   1486 				return (EINVAL);
   1487 			}
   1488 			break;
   1489 		}
   1490 	}
   1491 
   1492 	return (0);
   1493 }
   1494 
   1495 /*
   1496  * inputs:
   1497  * zc_name		name of filesystem
   1498  * zc_value		name of property to inherit
   1499  * zc_nvlist_src{_size}	nvlist of properties to apply
   1500  *
   1501  * outputs:		none
   1502  */
   1503 static int
   1504 zfs_ioc_set_prop(zfs_cmd_t *zc)
   1505 {
   1506 	nvlist_t *nvl;
   1507 	int error;
   1508 
   1509 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
   1510 	    &nvl)) != 0)
   1511 		return (error);
   1512 
   1513 	error = zfs_set_prop_nvlist(zc->zc_name, nvl);
   1514 
   1515 	nvlist_free(nvl);
   1516 	return (error);
   1517 }
   1518 
   1519 /*
   1520  * inputs:
   1521  * zc_name		name of filesystem
   1522  * zc_value		name of property to inherit
   1523  *
   1524  * outputs:		none
   1525  */
   1526 static int
   1527 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
   1528 {
   1529 	/* the property name has been validated by zfs_secpolicy_inherit() */
   1530 	return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
   1531 }
   1532 
   1533 static int
   1534 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
   1535 {
   1536 	nvlist_t *props;
   1537 	spa_t *spa;
   1538 	int error;
   1539 
   1540 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
   1541 	    &props)))
   1542 		return (error);
   1543 
   1544 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
   1545 		nvlist_free(props);
   1546 		return (error);
   1547 	}
   1548 
   1549 	error = spa_prop_set(spa, props);
   1550 
   1551 	nvlist_free(props);
   1552 	spa_close(spa, FTAG);
   1553 
   1554 	return (error);
   1555 }
   1556 
   1557 static int
   1558 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
   1559 {
   1560 	spa_t *spa;
   1561 	int error;
   1562 	nvlist_t *nvp = NULL;
   1563 
   1564 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
   1565 		return (error);
   1566 
   1567 	error = spa_prop_get(spa, &nvp);
   1568 
   1569 	if (error == 0 && zc->zc_nvlist_dst != NULL)
   1570 		error = put_nvlist(zc, nvp);
   1571 	else
   1572 		error = EFAULT;
   1573 
   1574 	spa_close(spa, FTAG);
   1575 
   1576 	if (nvp)
   1577 		nvlist_free(nvp);
   1578 	return (error);
   1579 }
   1580 
   1581 static int
   1582 zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
   1583 {
   1584 	nvlist_t *nvp;
   1585 	int error;
   1586 	uint32_t uid;
   1587 	uint32_t gid;
   1588 	uint32_t *groups;
   1589 	uint_t group_cnt;
   1590 	cred_t	*usercred;
   1591 
   1592 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
   1593 	    &nvp)) != 0) {
   1594 		return (error);
   1595 	}
   1596 
   1597 	if ((error = nvlist_lookup_uint32(nvp,
   1598 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
   1599 		nvlist_free(nvp);
   1600 		return (EPERM);
   1601 	}
   1602 
   1603 	if ((error = nvlist_lookup_uint32(nvp,
   1604 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
   1605 		nvlist_free(nvp);
   1606 		return (EPERM);
   1607 	}
   1608 
   1609 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
   1610 	    &groups, &group_cnt)) != 0) {
   1611 		nvlist_free(nvp);
   1612 		return (EPERM);
   1613 	}
   1614 	usercred = cralloc();
   1615 	if ((crsetugid(usercred, uid, gid) != 0) ||
   1616 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
   1617 		nvlist_free(nvp);
   1618 		crfree(usercred);
   1619 		return (EPERM);
   1620 	}
   1621 	nvlist_free(nvp);
   1622 	error = dsl_deleg_access(zc->zc_name,
   1623 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
   1624 	crfree(usercred);
   1625 	return (error);
   1626 }
   1627 
   1628 /*
   1629  * inputs:
   1630  * zc_name		name of filesystem
   1631  * zc_nvlist_src{_size}	nvlist of delegated permissions
   1632  * zc_perm_action	allow/unallow flag
   1633  *
   1634  * outputs:		none
   1635  */
   1636 static int
   1637 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
   1638 {
   1639 	int error;
   1640 	nvlist_t *fsaclnv = NULL;
   1641 
   1642 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
   1643 	    &fsaclnv)) != 0)
   1644 		return (error);
   1645 
   1646 	/*
   1647 	 * Verify nvlist is constructed correctly
   1648 	 */
   1649 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
   1650 		nvlist_free(fsaclnv);
   1651 		return (EINVAL);
   1652 	}
   1653 
   1654 	/*
   1655 	 * If we don't have PRIV_SYS_MOUNT, then validate
   1656 	 * that user is allowed to hand out each permission in
   1657 	 * the nvlist(s)
   1658 	 */
   1659 
   1660 	error = secpolicy_zfs(CRED());
   1661 	if (error) {
   1662 		if (zc->zc_perm_action == B_FALSE) {
   1663 			error = dsl_deleg_can_allow(zc->zc_name,
   1664 			    fsaclnv, CRED());
   1665 		} else {
   1666 			error = dsl_deleg_can_unallow(zc->zc_name,
   1667 			    fsaclnv, CRED());
   1668 		}
   1669 	}
   1670 
   1671 	if (error == 0)
   1672 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
   1673 
   1674 	nvlist_free(fsaclnv);
   1675 	return (error);
   1676 }
   1677 
   1678 /*
   1679  * inputs:
   1680  * zc_name		name of filesystem
   1681  *
   1682  * outputs:
   1683  * zc_nvlist_src{_size}	nvlist of delegated permissions
   1684  */
   1685 static int
   1686 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
   1687 {
   1688 	nvlist_t *nvp;
   1689 	int error;
   1690 
   1691 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
   1692 		error = put_nvlist(zc, nvp);
   1693 		nvlist_free(nvp);
   1694 	}
   1695 
   1696 	return (error);
   1697 }
   1698 
   1699 /*
   1700  * inputs:
   1701  * zc_name		name of volume
   1702  *
   1703  * outputs:		none
   1704  */
   1705 static int
   1706 zfs_ioc_create_minor(zfs_cmd_t *zc)
   1707 {
   1708 	return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
   1709 }
   1710 
   1711 /*
   1712  * inputs:
   1713  * zc_name		name of volume
   1714  *
   1715  * outputs:		none
   1716  */
   1717 static int
   1718 zfs_ioc_remove_minor(zfs_cmd_t *zc)
   1719 {
   1720 	return (zvol_remove_minor(zc->zc_name));
   1721 }
   1722 
   1723 /*
   1724  * Search the vfs list for a specified resource.  Returns a pointer to it
   1725  * or NULL if no suitable entry is found. The caller of this routine
   1726  * is responsible for releasing the returned vfs pointer.
   1727  */
   1728 static vfs_t *
   1729 zfs_get_vfs(const char *resource)
   1730 {
   1731 	struct vfs *vfsp;
   1732 	struct vfs *vfs_found = NULL;
   1733 
   1734 	vfs_list_read_lock();
   1735 	vfsp = rootvfs;
   1736 	do {
   1737 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
   1738 			VFS_HOLD(vfsp);
   1739 			vfs_found = vfsp;
   1740 			break;
   1741 		}
   1742 		vfsp = vfsp->vfs_next;
   1743 	} while (vfsp != rootvfs);
   1744 	vfs_list_unlock();
   1745 	return (vfs_found);
   1746 }
   1747 
   1748 /* ARGSUSED */
   1749 static void
   1750 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
   1751 {
   1752 	zfs_creat_t *zct = arg;
   1753 
   1754 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
   1755 }
   1756 
   1757 #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
   1758 
   1759 /*
   1760  * inputs:
   1761  * createprops	list of properties requested by creator
   1762  * dataset	name of dataset we are creating
   1763  *
   1764  * outputs:
   1765  * zplprops	values for the zplprops we attach to the master node object
   1766  *
   1767  * Determine the settings for utf8only, normalization and
   1768  * casesensitivity.  Specific values may have been requested by the
   1769  * creator and/or we can inherit values from the parent dataset.  If
   1770  * the file system is of too early a vintage, a creator can not
   1771  * request settings for these properties, even if the requested
   1772  * setting is the default value.  We don't actually want to create dsl
   1773  * properties for these, so remove them from the source nvlist after
   1774  * processing.
   1775  */
   1776 static int
   1777 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
   1778     nvlist_t *zplprops, uint64_t zplver)
   1779 {
   1780 	objset_t *os;
   1781 	char parentname[MAXNAMELEN];
   1782 	char *cp;
   1783 	uint64_t sense = ZFS_PROP_UNDEFINED;
   1784 	uint64_t norm = ZFS_PROP_UNDEFINED;
   1785 	uint64_t u8 = ZFS_PROP_UNDEFINED;
   1786 	int error = 0;
   1787 
   1788 	ASSERT(zplprops != NULL);
   1789 
   1790 	(void) strlcpy(parentname, dataset, sizeof (parentname));
   1791 	cp = strrchr(parentname, '/');
   1792 	ASSERT(cp != NULL);
   1793 	cp[0] = '\0';
   1794 
   1795 	/*
   1796 	 * Pull out creator prop choices, if any.
   1797 	 */
   1798 	if (createprops) {
   1799 		(void) nvlist_lookup_uint64(createprops,
   1800 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
   1801 		(void) nvlist_remove_all(createprops,
   1802 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
   1803 		(void) nvlist_lookup_uint64(createprops,
   1804 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
   1805 		(void) nvlist_remove_all(createprops,
   1806 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
   1807 		(void) nvlist_lookup_uint64(createprops,
   1808 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
   1809 		(void) nvlist_remove_all(createprops,
   1810 		    zfs_prop_to_name(ZFS_PROP_CASE));
   1811 	}
   1812 
   1813 	/*
   1814 	 * If the file system or pool is version is too "young" to
   1815 	 * support normalization and the creator tried to set a value
   1816 	 * for one of the props, error out.  We only need check the
   1817 	 * ZPL version because we've already checked by now that the
   1818 	 * SPA version is compatible with the selected ZPL version.
   1819 	 */
   1820 	if (zplver < ZPL_VERSION_NORMALIZATION &&
   1821 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
   1822 	    sense != ZFS_PROP_UNDEFINED))
   1823 		return (ENOTSUP);
   1824 
   1825 	/*
   1826 	 * Put the version in the zplprops
   1827 	 */
   1828 	VERIFY(nvlist_add_uint64(zplprops,
   1829 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
   1830 
   1831 	/*
   1832 	 * Open parent object set so we can inherit zplprop values if
   1833 	 * necessary.
   1834 	 */
   1835 	if ((error = zfs_os_open_retry(parentname, &os)) != 0)
   1836 		return (error);
   1837 
   1838 	if (norm == ZFS_PROP_UNDEFINED)
   1839 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
   1840 	VERIFY(nvlist_add_uint64(zplprops,
   1841 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
   1842 
   1843 	/*
   1844 	 * If we're normalizing, names must always be valid UTF-8 strings.
   1845 	 */
   1846 	if (norm)
   1847 		u8 = 1;
   1848 	if (u8 == ZFS_PROP_UNDEFINED)
   1849 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
   1850 	VERIFY(nvlist_add_uint64(zplprops,
   1851 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
   1852 
   1853 	if (sense == ZFS_PROP_UNDEFINED)
   1854 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
   1855 	VERIFY(nvlist_add_uint64(zplprops,
   1856 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
   1857 
   1858 	dmu_objset_close(os);
   1859 	return (0);
   1860 }
   1861 
   1862 /*
   1863  * inputs:
   1864  * zc_objset_type	type of objset to create (fs vs zvol)
   1865  * zc_name		name of new objset
   1866  * zc_value		name of snapshot to clone from (may be empty)
   1867  * zc_nvlist_src{_size}	nvlist of properties to apply
   1868  *
   1869  * outputs: none
   1870  */
   1871 static int
   1872 zfs_ioc_create(zfs_cmd_t *zc)
   1873 {
   1874 	objset_t *clone;
   1875 	int error = 0;
   1876 	zfs_creat_t zct;
   1877 	nvlist_t *nvprops = NULL;
   1878 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
   1879 	dmu_objset_type_t type = zc->zc_objset_type;
   1880 
   1881 	switch (type) {
   1882 
   1883 	case DMU_OST_ZFS:
   1884 		cbfunc = zfs_create_cb;
   1885 		break;
   1886 
   1887 	case DMU_OST_ZVOL:
   1888 		cbfunc = zvol_create_cb;
   1889 		break;
   1890 
   1891 	default:
   1892 		cbfunc = NULL;
   1893 	}
   1894 	if (strchr(zc->zc_name, '@') ||
   1895 	    strchr(zc->zc_name, '%'))
   1896 		return (EINVAL);
   1897 
   1898 	if (zc->zc_nvlist_src != NULL &&
   1899 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
   1900 	    &nvprops)) != 0)
   1901 		return (error);
   1902 
   1903 	zct.zct_zplprops = NULL;
   1904 	zct.zct_props = nvprops;
   1905 
   1906 	if (zc->zc_value[0] != '\0') {
   1907 		/*
   1908 		 * We're creating a clone of an existing snapshot.
   1909 		 */
   1910 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
   1911 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
   1912 			nvlist_free(nvprops);
   1913 			return (EINVAL);
   1914 		}
   1915 
   1916 		error = dmu_objset_open(zc->zc_value, type,
   1917 		    DS_MODE_STANDARD | DS_MODE_READONLY, &clone);
   1918 		if (error) {
   1919 			nvlist_free(nvprops);
   1920 			return (error);
   1921 		}
   1922 		error = dmu_objset_create(zc->zc_name, type, clone, NULL, NULL);
   1923 		if (error) {
   1924 			dmu_objset_close(clone);
   1925 			nvlist_free(nvprops);
   1926 			return (error);
   1927 		}
   1928 		dmu_objset_close(clone);
   1929 	} else {
   1930 		if (cbfunc == NULL) {
   1931 			nvlist_free(nvprops);
   1932 			return (EINVAL);
   1933 		}
   1934 
   1935 		if (type == DMU_OST_ZVOL) {
   1936 			uint64_t volsize, volblocksize;
   1937 
   1938 			if (nvprops == NULL ||
   1939 			    nvlist_lookup_uint64(nvprops,
   1940 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
   1941 			    &volsize) != 0) {
   1942 				nvlist_free(nvprops);
   1943 				return (EINVAL);
   1944 			}
   1945 
   1946 			if ((error = nvlist_lookup_uint64(nvprops,
   1947 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
   1948 			    &volblocksize)) != 0 && error != ENOENT) {
   1949 				nvlist_free(nvprops);
   1950 				return (EINVAL);
   1951 			}
   1952 
   1953 			if (error != 0)
   1954 				volblocksize = zfs_prop_default_numeric(
   1955 				    ZFS_PROP_VOLBLOCKSIZE);
   1956 
   1957 			if ((error = zvol_check_volblocksize(
   1958 			    volblocksize)) != 0 ||
   1959 			    (error = zvol_check_volsize(volsize,
   1960 			    volblocksize)) != 0) {
   1961 				nvlist_free(nvprops);
   1962 				return (error);
   1963 			}
   1964 		} else if (type == DMU_OST_ZFS) {
   1965 			uint64_t version;
   1966 			int error;
   1967 
   1968 			/*
   1969 			 * Default ZPL version to non-FUID capable if the
   1970 			 * pool is not upgraded to support FUIDs.
   1971 			 */
   1972 			if (zfs_check_version(zc->zc_name, SPA_VERSION_FUID))
   1973 				version = ZPL_VERSION_FUID - 1;
   1974 			else
   1975 				version = ZPL_VERSION;
   1976 
   1977 			/*
   1978 			 * Potentially override default ZPL version based
   1979 			 * on creator's request.
   1980 			 */
   1981 			(void) nvlist_lookup_uint64(nvprops,
   1982 			    zfs_prop_to_name(ZFS_PROP_VERSION), &version);
   1983 
   1984 			/*
   1985 			 * Make sure version we ended up with is kosher
   1986 			 */
   1987 			if ((version < ZPL_VERSION_INITIAL ||
   1988 			    version > ZPL_VERSION) ||
   1989 			    (version >= ZPL_VERSION_FUID &&
   1990 			    zfs_check_version(zc->zc_name, SPA_VERSION_FUID))) {
   1991 				nvlist_free(nvprops);
   1992 				return (ENOTSUP);
   1993 			}
   1994 
   1995 			/*
   1996 			 * We have to have normalization and
   1997 			 * case-folding flags correct when we do the
   1998 			 * file system creation, so go figure them out
   1999 			 * now.
   2000 			 */
   2001 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
   2002 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
   2003 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
   2004 			    zct.zct_zplprops, version);
   2005 			if (error != 0) {
   2006 				nvlist_free(nvprops);
   2007 				nvlist_free(zct.zct_zplprops);
   2008 				return (error);
   2009 			}
   2010 		}
   2011 		error = dmu_objset_create(zc->zc_name, type, NULL, cbfunc,
   2012 		    &zct);
   2013 		nvlist_free(zct.zct_zplprops);
   2014 	}
   2015 
   2016 	/*
   2017 	 * It would be nice to do this atomically.
   2018 	 */
   2019 	if (error == 0) {
   2020 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
   2021 			(void) dmu_objset_destroy(zc->zc_name);
   2022 	}
   2023 
   2024 	nvlist_free(nvprops);
   2025 	return (error);
   2026 }
   2027 
   2028 /*
   2029  * inputs:
   2030  * zc_name	name of filesystem
   2031  * zc_value	short name of snapshot
   2032  * zc_cookie	recursive flag
   2033  *
   2034  * outputs:	none
   2035  */
   2036 static int
   2037 zfs_ioc_snapshot(zfs_cmd_t *zc)
   2038 {
   2039 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
   2040 		return (EINVAL);
   2041 	return (dmu_objset_snapshot(zc->zc_name,
   2042 	    zc->zc_value, zc->zc_cookie));
   2043 }
   2044 
   2045 int
   2046 zfs_unmount_snap(char *name, void *arg)
   2047 {
   2048 	char *snapname = arg;
   2049 	char *cp;
   2050 	vfs_t *vfsp = NULL;
   2051 
   2052 	/*
   2053 	 * Snapshots (which are under .zfs control) must be unmounted
   2054 	 * before they can be destroyed.
   2055 	 */
   2056 
   2057 	if (snapname) {
   2058 		(void) strcat(name, "@");
   2059 		(void) strcat(name, snapname);
   2060 		vfsp = zfs_get_vfs(name);
   2061 		cp = strchr(name, '@');
   2062 		*cp = '\0';
   2063 	} else if (strchr(name, '@')) {
   2064 		vfsp = zfs_get_vfs(name);
   2065 	}
   2066 
   2067 	if (vfsp) {
   2068 		/*
   2069 		 * Always force the unmount for snapshots.
   2070 		 */
   2071 		int flag = MS_FORCE;
   2072 		int err;
   2073 
   2074 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
   2075 			VFS_RELE(vfsp);
   2076 			return (err);
   2077 		}
   2078 		VFS_RELE(vfsp);
   2079 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
   2080 			return (err);
   2081 	}
   2082 	return (0);
   2083 }
   2084 
   2085 /*
   2086  * inputs:
   2087  * zc_name	name of filesystem
   2088  * zc_value	short name of snapshot
   2089  *
   2090  * outputs:	none
   2091  */
   2092 static int
   2093 zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
   2094 {
   2095 	int err;
   2096 
   2097 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
   2098 		return (EINVAL);
   2099 	err = dmu_objset_find(zc->zc_name,
   2100 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
   2101 	if (err)
   2102 		return (err);
   2103 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
   2104 }
   2105 
   2106 /*
   2107  * inputs:
   2108  * zc_name		name of dataset to destroy
   2109  * zc_objset_type	type of objset
   2110  *
   2111  * outputs:		none
   2112  */
   2113 static int
   2114 zfs_ioc_destroy(zfs_cmd_t *zc)
   2115 {
   2116 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
   2117 		int err = zfs_unmount_snap(zc->zc_name, NULL);
   2118 		if (err)
   2119 			return (err);
   2120 	}
   2121 
   2122 	return (dmu_objset_destroy(zc->zc_name));
   2123 }
   2124 
   2125 /*
   2126  * inputs:
   2127  * zc_name	name of dataset to rollback (to most recent snapshot)
   2128  *
   2129  * outputs:	none
   2130  */
   2131 static int
   2132 zfs_ioc_rollback(zfs_cmd_t *zc)
   2133 {
   2134 	objset_t *os;
   2135 	int error;
   2136 	zfsvfs_t *zfsvfs = NULL;
   2137 
   2138 	/*
   2139 	 * Get the zfsvfs for the receiving objset. There
   2140 	 * won't be one if we're operating on a zvol, if the
   2141 	 * objset doesn't exist yet, or is not mounted.
   2142 	 */
   2143 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
   2144 	    DS_MODE_STANDARD, &os);
   2145 	if (error)
   2146 		return (error);
   2147 
   2148 	if (dmu_objset_type(os) == DMU_OST_ZFS) {
   2149 		mutex_enter(&os->os->os_user_ptr_lock);
   2150 		zfsvfs = dmu_objset_get_user(os);
   2151 		if (zfsvfs != NULL)
   2152 			VFS_HOLD(zfsvfs->z_vfs);
   2153 		mutex_exit(&os->os->os_user_ptr_lock);
   2154 	}
   2155 
   2156 	if (zfsvfs != NULL) {
   2157 		char osname[MAXNAMELEN];
   2158 		int mode;
   2159 
   2160 		VERIFY3U(0, ==, zfs_suspend_fs(zfsvfs, osname, &mode));
   2161 		ASSERT(strcmp(osname, zc->zc_name) == 0);
   2162 		error = dmu_objset_rollback(os);
   2163 		VERIFY3U(0, ==, zfs_resume_fs(zfsvfs, osname, mode));
   2164 
   2165 		VFS_RELE(zfsvfs->z_vfs);
   2166 	} else {
   2167 		error = dmu_objset_rollback(os);
   2168 	}
   2169 	/* Note, the dmu_objset_rollback() closes the objset for us. */
   2170 
   2171 	return (error);
   2172 }
   2173 
   2174 /*
   2175  * inputs:
   2176  * zc_name	old name of dataset
   2177  * zc_value	new name of dataset
   2178  * zc_cookie	recursive flag (only valid for snapshots)
   2179  *
   2180  * outputs:	none
   2181  */
   2182 static int
   2183 zfs_ioc_rename(zfs_cmd_t *zc)
   2184 {
   2185 	boolean_t recursive = zc->zc_cookie & 1;
   2186 
   2187 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
   2188 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
   2189 	    strchr(zc->zc_value, '%'))
   2190 		return (EINVAL);
   2191 
   2192 	/*
   2193 	 * Unmount snapshot unless we're doing a recursive rename,
   2194 	 * in which case the dataset code figures out which snapshots
   2195 	 * to unmount.
   2196 	 */
   2197 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
   2198 	    zc->zc_objset_type == DMU_OST_ZFS) {
   2199 		int err = zfs_unmount_snap(zc->zc_name, NULL);
   2200 		if (err)
   2201 			return (err);
   2202 	}
   2203 
   2204 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
   2205 }
   2206 
   2207 /*
   2208  * inputs:
   2209  * zc_name		name of containing filesystem
   2210  * zc_nvlist_src{_size}	nvlist of properties to apply
   2211  * zc_value		name of snapshot to create
   2212  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
   2213  * zc_cookie		file descriptor to recv from
   2214  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
   2215  * zc_guid		force flag
   2216  *
   2217  * outputs:
   2218  * zc_cookie		number of bytes read
   2219  */
   2220 static int
   2221 zfs_ioc_recv(zfs_cmd_t *zc)
   2222 {
   2223 	file_t *fp;
   2224 	objset_t *os;
   2225 	dmu_recv_cookie_t drc;
   2226 	zfsvfs_t *zfsvfs = NULL;
   2227 	boolean_t force = (boolean_t)zc->zc_guid;
   2228 	int error, fd;
   2229 	offset_t off;
   2230 	nvlist_t *props = NULL;
   2231 	objset_t *origin = NULL;
   2232 	char *tosnap;
   2233 	char tofs[ZFS_MAXNAMELEN];
   2234 
   2235 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
   2236 	    strchr(zc->zc_value, '@') == NULL ||
   2237 	    strchr(zc->zc_value, '%'))
   2238 		return (EINVAL);
   2239 
   2240 	(void) strcpy(tofs, zc->zc_value);
   2241 	tosnap = strchr(tofs, '@');
   2242 	*tosnap = '\0';
   2243 	tosnap++;
   2244 
   2245 	if (zc->zc_nvlist_src != NULL &&
   2246 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
   2247 	    &props)) != 0)
   2248 		return (error);
   2249 
   2250 	fd = zc->zc_cookie;
   2251 	fp = getf(fd);
   2252 	if (fp == NULL) {
   2253 		nvlist_free(props);
   2254 		return (EBADF);
   2255 	}
   2256 
   2257 	/*
   2258 	 * Get the zfsvfs for the receiving objset. There
   2259 	 * won't be one if we're operating on a zvol, if the
   2260 	 * objset doesn't exist yet, or is not mounted.
   2261 	 */
   2262 
   2263 	error = dmu_objset_open(tofs, DMU_OST_ZFS,
   2264 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
   2265 	if (!error) {
   2266 		mutex_enter(&os->os->os_user_ptr_lock);
   2267 		zfsvfs = dmu_objset_get_user(os);
   2268 		if (zfsvfs != NULL)
   2269 			VFS_HOLD(zfsvfs->z_vfs);
   2270 		mutex_exit(&os->os->os_user_ptr_lock);
   2271 		dmu_objset_close(os);
   2272 	}
   2273 
   2274 	if (zc->zc_string[0]) {
   2275 		error = dmu_objset_open(zc->zc_string, DMU_OST_ANY,
   2276 		    DS_MODE_STANDARD | DS_MODE_READONLY, &origin);
   2277 		if (error) {
   2278 			if (zfsvfs != NULL)
   2279 				VFS_RELE(zfsvfs->z_vfs);
   2280 			nvlist_free(props);
   2281 			releasef(fd);
   2282 			return (error);
   2283 		}
   2284 	}
   2285 
   2286 	error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
   2287 	    force, origin, zfsvfs != NULL, &drc);
   2288 	if (origin)
   2289 		dmu_objset_close(origin);
   2290 	if (error) {
   2291 		if (zfsvfs != NULL)
   2292 			VFS_RELE(zfsvfs->z_vfs);
   2293 		nvlist_free(props);
   2294 		releasef(fd);
   2295 		return (error);
   2296 	}
   2297 
   2298 	/*
   2299 	 * If properties are supplied, they are to completely replace
   2300 	 * the existing ones; "inherit" any existing properties.
   2301 	 */
   2302 	if (props) {
   2303 		objset_t *os;
   2304 		nvlist_t *nv = NULL;
   2305 
   2306 		error = dmu_objset_open(tofs, DMU_OST_ANY,
   2307 		    DS_MODE_STANDARD | DS_MODE_READONLY | DS_MODE_INCONSISTENT,
   2308 		    &os);
   2309 		if (error == 0) {
   2310 			error = dsl_prop_get_all(os, &nv);
   2311 			dmu_objset_close(os);
   2312 		}
   2313 		if (error == 0) {
   2314 			nvpair_t *elem;
   2315 			zfs_cmd_t *zc2;
   2316 			zc2 = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
   2317 
   2318 			(void) strcpy(zc2->zc_name, tofs);
   2319 			for (elem = nvlist_next_nvpair(nv, NULL); elem;
   2320 			    elem = nvlist_next_nvpair(nv, elem)) {
   2321 				(void) strcpy(zc2->zc_value, nvpair_name(elem));
   2322 				if (zfs_secpolicy_inherit(zc2, CRED()) == 0)
   2323 					(void) zfs_ioc_inherit_prop(zc2);
   2324 			}
   2325 			kmem_free(zc2, sizeof (zfs_cmd_t));
   2326 		}
   2327 		if (nv)
   2328 			nvlist_free(nv);
   2329 	}
   2330 
   2331 	/*
   2332 	 * Set properties.  Note, we ignore errors.  Would be better to
   2333 	 * do best-effort in zfs_set_prop_nvlist, too.
   2334 	 */
   2335 	(void) zfs_set_prop_nvlist(tofs, props);
   2336 	nvlist_free(props);
   2337 
   2338 	off = fp->f_offset;
   2339 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
   2340 
   2341 	if (error == 0) {
   2342 		if (zfsvfs != NULL) {
   2343 			char osname[MAXNAMELEN];
   2344 			int mode;
   2345 
   2346 			(void) zfs_suspend_fs(zfsvfs, osname, &mode);
   2347 			error = dmu_recv_end(&drc);
   2348 			error |= zfs_resume_fs(zfsvfs, osname, mode);
   2349 		} else {
   2350 			error = dmu_recv_end(&drc);
   2351 		}
   2352 	}
   2353 	if (zfsvfs != NULL)
   2354 		VFS_RELE(zfsvfs->z_vfs);
   2355 
   2356 	zc->zc_cookie = off - fp->f_offset;
   2357 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
   2358 		fp->f_offset = off;
   2359 
   2360 	releasef(fd);
   2361 	return (error);
   2362 }
   2363 
   2364 /*
   2365  * inputs:
   2366  * zc_name	name of snapshot to send
   2367  * zc_value	short name of incremental fromsnap (may be empty)
   2368  * zc_cookie	file descriptor to send stream to
   2369  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
   2370  *
   2371  * outputs: none
   2372  */
   2373 static int
   2374 zfs_ioc_send(zfs_cmd_t *zc)
   2375 {
   2376 	objset_t *fromsnap = NULL;
   2377 	objset_t *tosnap;
   2378 	file_t *fp;
   2379 	int error;
   2380 	offset_t off;
   2381 
   2382 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
   2383 	    DS_MODE_STANDARD | DS_MODE_READONLY, &tosnap);
   2384 	if (error)
   2385 		return (error);
   2386 
   2387 	if (zc->zc_value[0] != '\0') {
   2388 		char buf[MAXPATHLEN];
   2389 		char *cp;
   2390 
   2391 		(void) strncpy(buf, zc->zc_name, sizeof (buf));
   2392 		cp = strchr(buf, '@');
   2393 		if (cp)
   2394 			*(cp+1) = 0;
   2395 		(void) strncat(buf, zc->zc_value, sizeof (buf));
   2396 		error = dmu_objset_open(buf, DMU_OST_ANY,
   2397 		    DS_MODE_STANDARD | DS_MODE_READONLY, &fromsnap);
   2398 		if (error) {
   2399 			dmu_objset_close(tosnap);
   2400 			return (error);
   2401 		}
   2402 	}
   2403 
   2404 	fp = getf(zc->zc_cookie);
   2405 	if (fp == NULL) {
   2406 		dmu_objset_close(tosnap);
   2407 		if (fromsnap)
   2408 			dmu_objset_close(fromsnap);
   2409 		return (EBADF);
   2410 	}
   2411 
   2412 	off = fp->f_offset;
   2413 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
   2414 
   2415 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
   2416 		fp->f_offset = off;
   2417 	releasef(zc->zc_cookie);
   2418 	if (fromsnap)
   2419 		dmu_objset_close(fromsnap);
   2420 	dmu_objset_close(tosnap);
   2421 	return (error);
   2422 }
   2423 
   2424 static int
   2425 zfs_ioc_inject_fault(zfs_cmd_t *zc)
   2426 {
   2427 	int id, error;
   2428 
   2429 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
   2430 	    &zc->zc_inject_record);
   2431 
   2432 	if (error == 0)
   2433 		zc->zc_guid = (uint64_t)id;
   2434 
   2435 	return (error);
   2436 }
   2437 
   2438 static int
   2439 zfs_ioc_clear_fault(zfs_cmd_t *zc)
   2440 {
   2441 	return (zio_clear_fault((int)zc->zc_guid));
   2442 }
   2443 
   2444 static int
   2445 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
   2446 {
   2447 	int id = (int)zc->zc_guid;
   2448 	int error;
   2449 
   2450 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
   2451 	    &zc->zc_inject_record);
   2452 
   2453 	zc->zc_guid = id;
   2454 
   2455 	return (error);
   2456 }
   2457 
   2458 static int
   2459 zfs_ioc_error_log(zfs_cmd_t *zc)
   2460 {
   2461 	spa_t *spa;
   2462 	int error;
   2463 	size_t count = (size_t)zc->zc_nvlist_dst_size;
   2464 
   2465 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
   2466 		return (error);
   2467 
   2468 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
   2469 	    &count);
   2470 	if (error == 0)
   2471 		zc->zc_nvlist_dst_size = count;
   2472 	else
   2473 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
   2474 
   2475 	spa_close(spa, FTAG);
   2476 
   2477 	return (error);
   2478 }
   2479 
   2480 static int
   2481 zfs_ioc_clear(zfs_cmd_t *zc)
   2482 {
   2483 	spa_t *spa;
   2484 	vdev_t *vd;
   2485 	uint64_t txg;
   2486 	int error;
   2487 
   2488 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
   2489 		return (error);
   2490 
   2491 	/*
   2492 	 * Try to resume any I/Os which may have been suspended
   2493 	 * as a result of a complete pool failure.
   2494 	 */
   2495 	if (!list_is_empty(&spa->spa_zio_list)) {
   2496 		if (zio_vdev_resume_io(spa) != 0) {
   2497 			spa_close(spa, FTAG);
   2498 			return (EIO);
   2499 		}
   2500 	}
   2501 
   2502 	txg = spa_vdev_enter(spa);
   2503 
   2504 	if (zc->zc_guid == 0) {
   2505 		vd = NULL;
   2506 	} else if ((vd = spa_lookup_by_guid(spa, zc->zc_guid)) == NULL) {
   2507 		spa_aux_vdev_t *sav;
   2508 		int i;
   2509 
   2510 		/*
   2511 		 * Check if this is an l2cache device.
   2512 		 */
   2513 		ASSERT(spa != NULL);
   2514 		sav = &spa->spa_l2cache;
   2515 		for (i = 0; i < sav->sav_count; i++) {
   2516 			if (sav->sav_vdevs[i]->vdev_guid == zc->zc_guid) {
   2517 				vd = sav->sav_vdevs[i];
   2518 				break;
   2519 			}
   2520 		}
   2521 
   2522 		if (vd == NULL) {
   2523 			(void) spa_vdev_exit(spa, NULL, txg, ENODEV);
   2524 			spa_close(spa, FTAG);
   2525 			return (ENODEV);
   2526 		}
   2527 	}
   2528 
   2529 	vdev_clear(spa, vd, B_TRUE);
   2530 
   2531 	(void) spa_vdev_exit(spa, NULL, txg, 0);
   2532 
   2533 	spa_close(spa, FTAG);
   2534 
   2535 	return (0);
   2536 }
   2537 
   2538 /*
   2539  * inputs:
   2540  * zc_name	name of filesystem
   2541  * zc_value	name of origin snapshot
   2542  *
   2543  * outputs:	none
   2544  */
   2545 static int
   2546 zfs_ioc_promote(zfs_cmd_t *zc)
   2547 {
   2548 	char *cp;
   2549 
   2550 	/*
   2551 	 * We don't need to unmount *all* the origin fs's snapshots, but
   2552 	 * it's easier.
   2553 	 */
   2554 	cp = strchr(zc->zc_value, '@');
   2555 	if (cp)
   2556 		*cp = '\0';
   2557 	(void) dmu_objset_find(zc->zc_value,
   2558 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
   2559 	return (dsl_dataset_promote(zc->zc_name));
   2560 }
   2561 
   2562 /*
   2563  * We don't want to have a hard dependency
   2564  * against some special symbols in sharefs
   2565  * nfs, and smbsrv.  Determine them if needed when
   2566  * the first file system is shared.
   2567  * Neither sharefs, nfs or smbsrv are unloadable modules.
   2568  */
   2569 int (*znfsexport_fs)(void *arg);
   2570 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
   2571 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
   2572 
   2573 int zfs_nfsshare_inited;
   2574 int zfs_smbshare_inited;
   2575 
   2576 ddi_modhandle_t nfs_mod;
   2577 ddi_modhandle_t sharefs_mod;
   2578 ddi_modhandle_t smbsrv_mod;
   2579 kmutex_t zfs_share_lock;
   2580 
   2581 static int
   2582 zfs_init_sharefs()
   2583 {
   2584 	int error;
   2585 
   2586 	ASSERT(MUTEX_HELD(&zfs_share_lock));
   2587 	/* Both NFS and SMB shares also require sharetab support. */
   2588 	if (sharefs_mod == NULL && ((sharefs_mod =
   2589 	    ddi_modopen("fs/sharefs",
   2590 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
   2591 		return (ENOSYS);
   2592 	}
   2593 	if (zshare_fs == NULL && ((zshare_fs =
   2594 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
   2595 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
   2596 		return (ENOSYS);
   2597 	}
   2598 	return (0);
   2599 }
   2600 
   2601 static int
   2602 zfs_ioc_share(zfs_cmd_t *zc)
   2603 {
   2604 	int error;
   2605 	int opcode;
   2606 
   2607 	switch (zc->zc_share.z_sharetype) {
   2608 	case ZFS_SHARE_NFS:
   2609 	case ZFS_UNSHARE_NFS:
   2610 		if (zfs_nfsshare_inited == 0) {
   2611 			mutex_enter(&zfs_share_lock);
   2612 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
   2613 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
   2614 				mutex_exit(&zfs_share_lock);
   2615 				return (ENOSYS);
   2616 			}
   2617 			if (znfsexport_fs == NULL &&
   2618 			    ((znfsexport_fs = (int (*)(void *))
   2619 			    ddi_modsym(nfs_mod,
   2620 			    "nfs_export", &error)) == NULL)) {
   2621 				mutex_exit(&zfs_share_lock);
   2622 				return (ENOSYS);
   2623 			}
   2624 			error = zfs_init_sharefs();
   2625 			if (error) {
   2626 				mutex_exit(&zfs_share_lock);
   2627 				return (ENOSYS);
   2628 			}
   2629 			zfs_nfsshare_inited = 1;
   2630 			mutex_exit(&zfs_share_lock);
   2631 		}
   2632 		break;
   2633 	case ZFS_SHARE_SMB:
   2634 	case ZFS_UNSHARE_SMB:
   2635 		if (zfs_smbshare_inited == 0) {
   2636 			mutex_enter(&zfs_share_lock);
   2637 			if (smbsrv_mod == NULL && ((smbsrv_mod =
   2638 			    ddi_modopen("drv/smbsrv",
   2639 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
   2640 				mutex_exit(&zfs_share_lock);
   2641 				return (ENOSYS);
   2642 			}
   2643 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
   2644 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
   2645 			    "lmshrd_share_upcall", &error)) == NULL)) {
   2646 				mutex_exit(&zfs_share_lock);
   2647 				return (ENOSYS);
   2648 			}
   2649 			error = zfs_init_sharefs();
   2650 			if (error) {
   2651 				mutex_exit(&zfs_share_lock);
   2652 				return (ENOSYS);
   2653 			}
   2654 			zfs_smbshare_inited = 1;
   2655 			mutex_exit(&zfs_share_lock);
   2656 		}
   2657 		break;
   2658 	default:
   2659 		return (EINVAL);
   2660 	}
   2661 
   2662 	switch (zc->zc_share.z_sharetype) {
   2663 	case ZFS_SHARE_NFS:
   2664 	case ZFS_UNSHARE_NFS:
   2665 		if (error =
   2666 		    znfsexport_fs((void *)
   2667 		    (uintptr_t)zc->zc_share.z_exportdata))
   2668 			return (error);
   2669 		break;
   2670 	case ZFS_SHARE_SMB:
   2671 	case ZFS_UNSHARE_SMB:
   2672 		if (error = zsmbexport_fs((void *)
   2673 		    (uintptr_t)zc->zc_share.z_exportdata,
   2674 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
   2675 		    B_TRUE : B_FALSE)) {
   2676 			return (error);
   2677 		}
   2678 		break;
   2679 	}
   2680 
   2681 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
   2682 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
   2683 	    SHAREFS_ADD : SHAREFS_REMOVE;
   2684 
   2685 	/*
   2686 	 * Add or remove share from sharetab
   2687 	 */
   2688 	error = zshare_fs(opcode,
   2689 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
   2690 	    zc->zc_share.z_sharemax);
   2691 
   2692 	return (error);
   2693 
   2694 }
   2695 
   2696 /*
   2697  * pool create, destroy, and export don't log the history as part of
   2698  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
   2699  * do the logging of those commands.
   2700  */
   2701 static zfs_ioc_vec_t zfs_ioc_vec[] = {
   2702 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE },
   2703 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE },
   2704 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE },
   2705 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE },
   2706 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE },
   2707 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE },
   2708 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE },
   2709 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE },
   2710 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE },
   2711 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE },
   2712 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE },
   2713 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE },
   2714 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE },
   2715 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE },
   2716 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
   2717 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
   2718 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE },
   2719 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE },
   2720 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
   2721 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read,
   2722 	    DATASET_NAME, B_FALSE },
   2723 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read,
   2724 	    DATASET_NAME, B_FALSE },
   2725 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE },
   2726 	{ zfs_ioc_create_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
   2727 	{ zfs_ioc_remove_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
   2728 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE },
   2729 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE },
   2730 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE },
   2731 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE },
   2732 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE },
   2733 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE },
   2734 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE },
   2735 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE },
   2736 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE },
   2737 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE },
   2738 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE },
   2739 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE },
   2740 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE },
   2741 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE },
   2742 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE },
   2743 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE },
   2744 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE },
   2745 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE },
   2746 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE },
   2747 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
   2748 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi,
   2749 	    DATASET_NAME, B_FALSE },
   2750 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE },
   2751 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE },
   2752 };
   2753 
   2754 static int
   2755 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
   2756 {
   2757 	zfs_cmd_t *zc;
   2758 	uint_t vec;
   2759 	int error, rc;
   2760 
   2761 	if (getminor(dev) != 0)
   2762 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
   2763 
   2764 	vec = cmd - ZFS_IOC;
   2765 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
   2766 
   2767 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
   2768 		return (EINVAL);
   2769 
   2770 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
   2771 
   2772 	error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t));
   2773 
   2774 	if (error == 0)
   2775 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
   2776 
   2777 	/*
   2778 	 * Ensure that all pool/dataset names are valid before we pass down to
   2779 	 * the lower layers.
   2780 	 */
   2781 	if (error == 0) {
   2782 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
   2783 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
   2784 		case POOL_NAME:
   2785 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
   2786 				error = EINVAL;
   2787 			break;
   2788 
   2789 		case DATASET_NAME:
   2790 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
   2791 				error = EINVAL;
   2792 			break;
   2793 
   2794 		case NO_NAME:
   2795 			break;
   2796 		}
   2797 	}
   2798 
   2799 	if (error == 0)
   2800 		error = zfs_ioc_vec[vec].zvec_func(zc);
   2801 
   2802 	rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t));
   2803 	if (error == 0) {
   2804 		error = rc;
   2805 		if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE)
   2806 			zfs_log_history(zc);
   2807 	}
   2808 
   2809 	kmem_free(zc, sizeof (zfs_cmd_t));
   2810 	return (error);
   2811 }
   2812 
   2813 static int
   2814 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
   2815 {
   2816 	if (cmd != DDI_ATTACH)
   2817 		return (DDI_FAILURE);
   2818 
   2819 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
   2820 	    DDI_PSEUDO, 0) == DDI_FAILURE)
   2821 		return (DDI_FAILURE);
   2822 
   2823 	zfs_dip = dip;
   2824 
   2825 	ddi_report_dev(dip);
   2826 
   2827 	return (DDI_SUCCESS);
   2828 }
   2829 
   2830 static int
   2831 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
   2832 {
   2833 	if (spa_busy() || zfs_busy() || zvol_busy())
   2834 		return (DDI_FAILURE);
   2835 
   2836 	if (cmd != DDI_DETACH)
   2837 		return (DDI_FAILURE);
   2838 
   2839 	zfs_dip = NULL;
   2840 
   2841 	ddi_prop_remove_all(dip);
   2842 	ddi_remove_minor_node(dip, NULL);
   2843 
   2844 	return (DDI_SUCCESS);
   2845 }
   2846 
   2847 /*ARGSUSED*/
   2848 static int
   2849 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
   2850 {
   2851 	switch (infocmd) {
   2852 	case DDI_INFO_DEVT2DEVINFO:
   2853 		*result = zfs_dip;
   2854 		return (DDI_SUCCESS);
   2855 
   2856 	case DDI_INFO_DEVT2INSTANCE:
   2857 		*result = (void *)0;
   2858 		return (DDI_SUCCESS);
   2859 	}
   2860 
   2861 	return (DDI_FAILURE);
   2862 }
   2863 
   2864 /*
   2865  * OK, so this is a little weird.
   2866  *
   2867  * /dev/zfs is the control node, i.e. minor 0.
   2868  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
   2869  *
   2870  * /dev/zfs has basically nothing to do except serve up ioctls,
   2871  * so most of the standard driver entry points are in zvol.c.
   2872  */
   2873 static struct cb_ops zfs_cb_ops = {
   2874 	zvol_open,	/* open */
   2875 	zvol_close,	/* close */
   2876 	zvol_strategy,	/* strategy */
   2877 	nodev,		/* print */
   2878 	nodev,		/* dump */
   2879 	zvol_read,	/* read */
   2880 	zvol_write,	/* write */
   2881 	zfsdev_ioctl,	/* ioctl */
   2882 	nodev,		/* devmap */
   2883 	nodev,		/* mmap */
   2884 	nodev,		/* segmap */
   2885 	nochpoll,	/* poll */
   2886 	ddi_prop_op,	/* prop_op */
   2887 	NULL,		/* streamtab */
   2888 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
   2889 	CB_REV,		/* version */
   2890 	nodev,		/* async read */
   2891 	nodev,		/* async write */
   2892 };
   2893 
   2894 static struct dev_ops zfs_dev_ops = {
   2895 	DEVO_REV,	/* version */
   2896 	0,		/* refcnt */
   2897 	zfs_info,	/* info */
   2898 	nulldev,	/* identify */
   2899 	nulldev,	/* probe */
   2900 	zfs_attach,	/* attach */
   2901 	zfs_detach,	/* detach */
   2902 	nodev,		/* reset */
   2903 	&zfs_cb_ops,	/* driver operations */
   2904 	NULL		/* no bus operations */
   2905 };
   2906 
   2907 static struct modldrv zfs_modldrv = {
   2908 	&mod_driverops, "ZFS storage pool version " SPA_VERSION_STRING,
   2909 	    &zfs_dev_ops
   2910 };
   2911 
   2912 static struct modlinkage modlinkage = {
   2913 	MODREV_1,
   2914 	(void *)&zfs_modlfs,
   2915 	(void *)&zfs_modldrv,
   2916 	NULL
   2917 };
   2918 
   2919 
   2920 uint_t zfs_fsyncer_key;
   2921 extern uint_t rrw_tsd_key;
   2922 
   2923 int
   2924 _init(void)
   2925 {
   2926 	int error;
   2927 
   2928 	spa_init(FREAD | FWRITE);
   2929 	zfs_init();
   2930 	zvol_init();
   2931 
   2932 	if ((error = mod_install(&modlinkage)) != 0) {
   2933 		zvol_fini();
   2934 		zfs_fini();
   2935 		spa_fini();
   2936 		return (error);
   2937 	}
   2938 
   2939 	tsd_create(&zfs_fsyncer_key, NULL);
   2940 	tsd_create(&rrw_tsd_key, NULL);
   2941 
   2942 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
   2943 	ASSERT(error == 0);
   2944 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
   2945 
   2946 	return (0);
   2947 }
   2948 
   2949 int
   2950 _fini(void)
   2951 {
   2952 	int error;
   2953 
   2954 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
   2955 		return (EBUSY);
   2956 
   2957 	if ((error = mod_remove(&modlinkage)) != 0)
   2958 		return (error);
   2959 
   2960 	zvol_fini();
   2961 	zfs_fini();
   2962 	spa_fini();
   2963 	if (zfs_nfsshare_inited)
   2964 		(void) ddi_modclose(nfs_mod);
   2965 	if (zfs_smbshare_inited)
   2966 		(void) ddi_modclose(smbsrv_mod);
   2967 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
   2968 		(void) ddi_modclose(sharefs_mod);
   2969 
   2970 	tsd_destroy(&zfs_fsyncer_key);
   2971 	ldi_ident_release(zfs_li);
   2972 	zfs_li = NULL;
   2973 	mutex_destroy(&zfs_share_lock);
   2974 
   2975 	return (error);
   2976 }
   2977 
   2978 int
   2979 _info(struct modinfo *modinfop)
   2980 {
   2981 	return (mod_info(&modlinkage, modinfop));
   2982 }
   2983