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 2010 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26#include <sys/types.h>
27#include <sys/param.h>
28#include <sys/errno.h>
29#include <sys/uio.h>
30#include <sys/buf.h>
31#include <sys/modctl.h>
32#include <sys/open.h>
33#include <sys/file.h>
34#include <sys/kmem.h>
35#include <sys/conf.h>
36#include <sys/cmn_err.h>
37#include <sys/stat.h>
38#include <sys/zfs_ioctl.h>
39#include <sys/zfs_vfsops.h>
40#include <sys/zfs_znode.h>
41#include <sys/zap.h>
42#include <sys/spa.h>
43#include <sys/spa_impl.h>
44#include <sys/vdev.h>
45#include <sys/priv_impl.h>
46#include <sys/dmu.h>
47#include <sys/dsl_dir.h>
48#include <sys/dsl_dataset.h>
49#include <sys/dsl_prop.h>
50#include <sys/dsl_deleg.h>
51#include <sys/dmu_objset.h>
52#include <sys/ddi.h>
53#include <sys/sunddi.h>
54#include <sys/sunldi.h>
55#include <sys/policy.h>
56#include <sys/zone.h>
57#include <sys/nvpair.h>
58#include <sys/pathname.h>
59#include <sys/mount.h>
60#include <sys/sdt.h>
61#include <sys/fs/zfs.h>
62#include <sys/zfs_ctldir.h>
63#include <sys/zfs_dir.h>
64#include <sys/zvol.h>
65#include <sharefs/share.h>
66#include <sys/dmu_objset.h>
67#include <sys/callb.h>
68#include <sys/taskq.h>
69
70#include "zfs_namecheck.h"
71#include "zfs_prop.h"
72#include "zfs_deleg.h"
73
74#ifdef __NetBSD__
75static int	zfs_cmajor = -1;
76static int	zfs_bmajor = -1;
77#define	ddi_driver_major(x)	zfs_cmajor
78#endif
79
80extern struct modlfs zfs_modlfs;
81
82extern void zfs_init(void);
83extern void zfs_fini(void);
84
85ldi_ident_t zfs_li = NULL;
86dev_info_t *zfs_dip;
87
88typedef int zfs_ioc_func_t(zfs_cmd_t *);
89typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
90
91typedef enum {
92	NO_NAME,
93	POOL_NAME,
94	DATASET_NAME
95} zfs_ioc_namecheck_t;
96
97typedef struct zfs_ioc_vec {
98	zfs_ioc_func_t		*zvec_func;
99	zfs_secpolicy_func_t	*zvec_secpolicy;
100	zfs_ioc_namecheck_t	zvec_namecheck;
101	boolean_t		zvec_his_log;
102	boolean_t		zvec_pool_check;
103} zfs_ioc_vec_t;
104
105/* This array is indexed by zfs_userquota_prop_t */
106static const char *userquota_perms[] = {
107	ZFS_DELEG_PERM_USERUSED,
108	ZFS_DELEG_PERM_USERQUOTA,
109	ZFS_DELEG_PERM_GROUPUSED,
110	ZFS_DELEG_PERM_GROUPQUOTA,
111};
112
113static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
114static int zfs_check_settable(const char *name, nvpair_t *property,
115    cred_t *cr);
116static int zfs_check_clearable(char *dataset, nvlist_t *props,
117    nvlist_t **errors);
118static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
119    boolean_t *);
120int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t **);
121
122/* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
123void
124__dprintf(const char *file, const char *func, int line, const char *fmt, ...)
125{
126	const char *newfile;
127	char buf[256];
128	va_list adx;
129
130	/*
131	 * Get rid of annoying "../common/" prefix to filename.
132	 */
133	newfile = strrchr(file, '/');
134	if (newfile != NULL) {
135		newfile = newfile + 1; /* Get rid of leading / */
136	} else {
137		newfile = file;
138	}
139
140	va_start(adx, fmt);
141	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
142	va_end(adx);
143
144	/*
145	 * To get this data, use the zfs-dprintf probe as so:
146	 * dtrace -q -n 'zfs-dprintf \
147	 *	/stringof(arg0) == "dbuf.c"/ \
148	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
149	 * arg0 = file name
150	 * arg1 = function name
151	 * arg2 = line number
152	 * arg3 = message
153	 */
154	DTRACE_PROBE4(zfs__dprintf,
155	    char *, newfile, char *, func, int, line, char *, buf);
156}
157
158static void
159history_str_free(char *buf)
160{
161	kmem_free(buf, HIS_MAX_RECORD_LEN);
162}
163
164static char *
165history_str_get(zfs_cmd_t *zc)
166{
167	char *buf;
168
169	if (zc->zc_history == 0)
170		return (NULL);
171
172	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
173	if (copyinstr((void *)(uintptr_t)zc->zc_history,
174	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
175		history_str_free(buf);
176		return (NULL);
177	}
178
179	buf[HIS_MAX_RECORD_LEN -1] = '\0';
180
181	return (buf);
182}
183
184/*
185 * Check to see if the named dataset is currently defined as bootable
186 */
187static boolean_t
188zfs_is_bootfs(const char *name)
189{
190	objset_t *os;
191
192	if (dmu_objset_hold(name, FTAG, &os) == 0) {
193		boolean_t ret;
194		ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
195		dmu_objset_rele(os, FTAG);
196		return (ret);
197	}
198	return (B_FALSE);
199}
200
201/*
202 * zfs_earlier_version
203 *
204 *	Return non-zero if the spa version is less than requested version.
205 */
206static int
207zfs_earlier_version(const char *name, int version)
208{
209	spa_t *spa;
210
211	if (spa_open(name, &spa, FTAG) == 0) {
212		if (spa_version(spa) < version) {
213			spa_close(spa, FTAG);
214			return (1);
215		}
216		spa_close(spa, FTAG);
217	}
218	return (0);
219}
220
221/*
222 * zpl_earlier_version
223 *
224 * Return TRUE if the ZPL version is less than requested version.
225 */
226static boolean_t
227zpl_earlier_version(const char *name, int version)
228{
229	objset_t *os;
230	boolean_t rc = B_TRUE;
231
232	if (dmu_objset_hold(name, FTAG, &os) == 0) {
233		uint64_t zplversion;
234
235		if (dmu_objset_type(os) != DMU_OST_ZFS) {
236			dmu_objset_rele(os, FTAG);
237			return (B_TRUE);
238		}
239		/* XXX reading from non-owned objset */
240		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
241			rc = zplversion < version;
242		dmu_objset_rele(os, FTAG);
243	}
244	return (rc);
245}
246
247static void
248zfs_log_history(zfs_cmd_t *zc)
249{
250	spa_t *spa;
251	char *buf;
252
253	if ((buf = history_str_get(zc)) == NULL)
254		return;
255
256	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
257		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
258			(void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
259		spa_close(spa, FTAG);
260	}
261	history_str_free(buf);
262}
263
264/*
265 * Policy for top-level read operations (list pools).  Requires no privileges,
266 * and can be used in the local zone, as there is no associated dataset.
267 */
268/* ARGSUSED */
269static int
270zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
271{
272	return (0);
273}
274
275/*
276 * Policy for dataset read operations (list children, get statistics).  Requires
277 * no privileges, but must be visible in the local zone.
278 */
279/* ARGSUSED */
280static int
281zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
282{
283	if (INGLOBALZONE(curproc) ||
284	    zone_dataset_visible(zc->zc_name, NULL))
285		return (0);
286
287	return (ENOENT);
288}
289
290static int
291zfs_dozonecheck(const char *dataset, cred_t *cr)
292{
293	uint64_t zoned;
294	int writable = 1;
295
296	/*
297	 * The dataset must be visible by this zone -- check this first
298	 * so they don't see EPERM on something they shouldn't know about.
299	 */
300	if (!INGLOBALZONE(curproc) &&
301	    !zone_dataset_visible(dataset, &writable))
302		return (ENOENT);
303
304	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
305		return (ENOENT);
306
307	if (INGLOBALZONE(curproc)) {
308		/*
309		 * If the fs is zoned, only root can access it from the
310		 * global zone.
311		 */
312		if (secpolicy_zfs(cr) && zoned)
313			return (EPERM);
314	} else {
315		/*
316		 * If we are in a local zone, the 'zoned' property must be set.
317		 */
318		if (!zoned)
319			return (EPERM);
320
321		/* must be writable by this zone */
322		if (!writable)
323			return (EPERM);
324	}
325	return (0);
326}
327
328int
329zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
330{
331	int error;
332
333	error = zfs_dozonecheck(name, cr);
334	if (error == 0) {
335		error = secpolicy_zfs(cr);
336		if (error)
337			error = dsl_deleg_access(name, perm, cr);
338	}
339	return (error);
340}
341
342/*
343 * Policy for setting the security label property.
344 *
345 * Returns 0 for success, non-zero for access and other errors.
346 */
347static int
348zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
349{
350#ifdef PORT_SOLARIS
351	char		ds_hexsl[MAXNAMELEN];
352	bslabel_t	ds_sl, new_sl;
353	boolean_t	new_default = FALSE;
354	uint64_t	zoned;
355	int		needed_priv = -1;
356	int		error;
357
358	/* First get the existing dataset label. */
359	error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
360	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
361	if (error)
362		return (EPERM);
363
364	if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
365		new_default = TRUE;
366
367	/* The label must be translatable */
368	if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
369		return (EINVAL);
370
371	/*
372	 * In a non-global zone, disallow attempts to set a label that
373	 * doesn't match that of the zone; otherwise no other checks
374	 * are needed.
375	 */
376	if (!INGLOBALZONE(curproc)) {
377		if (new_default || !blequal(&new_sl, CR_SL(CRED())))
378			return (EPERM);
379		return (0);
380	}
381
382	/*
383	 * For global-zone datasets (i.e., those whose zoned property is
384	 * "off", verify that the specified new label is valid for the
385	 * global zone.
386	 */
387	if (dsl_prop_get_integer(name,
388	    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
389		return (EPERM);
390	if (!zoned) {
391		if (zfs_check_global_label(name, strval) != 0)
392			return (EPERM);
393	}
394
395	/*
396	 * If the existing dataset label is nondefault, check if the
397	 * dataset is mounted (label cannot be changed while mounted).
398	 * Get the zfsvfs; if there isn't one, then the dataset isn't
399	 * mounted (or isn't a dataset, doesn't exist, ...).
400	 */
401	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
402		objset_t *os;
403		static char *setsl_tag = "setsl_tag";
404
405		/*
406		 * Try to own the dataset; abort if there is any error,
407		 * (e.g., already mounted, in use, or other error).
408		 */
409		error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
410		    setsl_tag, &os);
411		if (error)
412			return (EPERM);
413
414		dmu_objset_disown(os, setsl_tag);
415
416		if (new_default) {
417			needed_priv = PRIV_FILE_DOWNGRADE_SL;
418			goto out_check;
419		}
420
421		if (hexstr_to_label(strval, &new_sl) != 0)
422			return (EPERM);
423
424		if (blstrictdom(&ds_sl, &new_sl))
425			needed_priv = PRIV_FILE_DOWNGRADE_SL;
426		else if (blstrictdom(&new_sl, &ds_sl))
427			needed_priv = PRIV_FILE_UPGRADE_SL;
428	} else {
429		/* dataset currently has a default label */
430		if (!new_default)
431			needed_priv = PRIV_FILE_UPGRADE_SL;
432	}
433
434out_check:
435	if (needed_priv != -1)
436		return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
437	return (0);
438#else
439	return (ENOTSUP);
440#endif
441}
442
443static int
444zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
445    cred_t *cr)
446{
447#ifdef PORT_SOLARIS
448	char *strval;
449
450	/*
451	 * Check permissions for special properties.
452	 */
453	switch (prop) {
454	case ZFS_PROP_ZONED:
455		/*
456		 * Disallow setting of 'zoned' from within a local zone.
457		 */
458		if (!INGLOBALZONE(curproc))
459			return (EPERM);
460		break;
461
462	case ZFS_PROP_QUOTA:
463		if (!INGLOBALZONE(curproc)) {
464			uint64_t zoned;
465			char setpoint[MAXNAMELEN];
466			/*
467			 * Unprivileged users are allowed to modify the
468			 * quota on things *under* (ie. contained by)
469			 * the thing they own.
470			 */
471			if (dsl_prop_get_integer(dsname, "zoned", &zoned,
472			    setpoint))
473				return (EPERM);
474			if (!zoned || strlen(dsname) <= strlen(setpoint))
475				return (EPERM);
476		}
477		break;
478
479	case ZFS_PROP_MLSLABEL:
480		if (!is_system_labeled())
481			return (EPERM);
482
483		if (nvpair_value_string(propval, &strval) == 0) {
484			int err;
485
486			err = zfs_set_slabel_policy(dsname, strval, CRED());
487			if (err != 0)
488				return (err);
489		}
490		break;
491	}
492
493	return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
494#else
495	return (ENOTSUP);
496#endif
497}
498
499int
500zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
501{
502	int error;
503
504	error = zfs_dozonecheck(zc->zc_name, cr);
505	if (error)
506		return (error);
507
508	/*
509	 * permission to set permissions will be evaluated later in
510	 * dsl_deleg_can_allow()
511	 */
512	return (0);
513}
514
515int
516zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
517{
518	return (zfs_secpolicy_write_perms(zc->zc_name,
519	    ZFS_DELEG_PERM_ROLLBACK, cr));
520}
521
522int
523zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
524{
525	return (zfs_secpolicy_write_perms(zc->zc_name,
526	    ZFS_DELEG_PERM_SEND, cr));
527}
528
529static int
530zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
531{
532	vnode_t *vp;
533	int error;
534
535	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
536		    NULL, &vp)) != 0)
537		return (error);
538
539	/* Now make sure mntpnt and dataset are ZFS */
540#ifndef __NetBSD__
541	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
542	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
543	    zc->zc_name) != 0)) {
544		VN_RELE(vp);
545		return (EPERM);
546	}
547#endif
548	VN_RELE(vp);
549	return (dsl_deleg_access(zc->zc_name,
550	    ZFS_DELEG_PERM_SHARE, cr));
551}
552
553int
554zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
555{
556#ifdef __NetBSD__
557	printf("XXX zfs_secpolicy_share write me\n");
558	return EPERM;
559#else
560	if (!INGLOBALZONE(curproc))
561		return (EPERM);
562
563	if (secpolicy_nfs(cr) == 0) {
564		return (0);
565	} else {
566		return (zfs_secpolicy_deleg_share(zc, cr));
567	}
568#endif
569}
570
571int
572zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
573{
574#ifdef __NetBSD__
575	printf("XXX zfs_secpolicy_share write me\n");
576	return EPERM;
577#else
578	if (!INGLOBALZONE(curproc))
579		return (EPERM);
580
581	if (secpolicy_smb(cr) == 0) {
582		return (0);
583	} else {
584		return (zfs_secpolicy_deleg_share(zc, cr));
585	}
586#endif	/* __NetBSD__ */
587}
588
589static int
590zfs_get_parent(const char *datasetname, char *parent, int parentsize)
591{
592	char *cp;
593
594	/*
595	 * Remove the @bla or /bla from the end of the name to get the parent.
596	 */
597	(void) strncpy(parent, datasetname, parentsize);
598	cp = strrchr(parent, '@');
599	if (cp != NULL) {
600		cp[0] = '\0';
601	} else {
602		cp = strrchr(parent, '/');
603		if (cp == NULL)
604			return (ENOENT);
605		cp[0] = '\0';
606	}
607
608	return (0);
609}
610
611int
612zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
613{
614	int error;
615
616	if ((error = zfs_secpolicy_write_perms(name,
617	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
618		return (error);
619
620	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
621}
622
623static int
624zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
625{
626	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
627}
628
629/*
630 * Destroying snapshots with delegated permissions requires
631 * descendent mount and destroy permissions.
632 * Reassemble the full filesystem@snap name so dsl_deleg_access()
633 * can do the correct permission check.
634 *
635 * Since this routine is used when doing a recursive destroy of snapshots
636 * and destroying snapshots requires descendent permissions, a successfull
637 * check of the top level snapshot applies to snapshots of all descendent
638 * datasets as well.
639 */
640static int
641zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, cred_t *cr)
642{
643	int error;
644	char *dsname;
645
646	dsname = kmem_asprintf("%s@%s", zc->zc_name, zc->zc_value);
647
648	error = zfs_secpolicy_destroy_perms(dsname, cr);
649
650	strfree(dsname);
651	return (error);
652}
653
654/*
655 * Must have sys_config privilege to check the iscsi permission
656 */
657/* ARGSUSED */
658static int
659zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr)
660{
661	return (secpolicy_zfs(cr));
662}
663
664int
665zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
666{
667	char	parentname[MAXNAMELEN];
668	int	error;
669
670	if ((error = zfs_secpolicy_write_perms(from,
671	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
672		return (error);
673
674	if ((error = zfs_secpolicy_write_perms(from,
675	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
676		return (error);
677
678	if ((error = zfs_get_parent(to, parentname,
679	    sizeof (parentname))) != 0)
680		return (error);
681
682	if ((error = zfs_secpolicy_write_perms(parentname,
683	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
684		return (error);
685
686	if ((error = zfs_secpolicy_write_perms(parentname,
687	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
688		return (error);
689
690	return (error);
691}
692
693static int
694zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
695{
696	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
697}
698
699static int
700zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
701{
702	char	parentname[MAXNAMELEN];
703	objset_t *clone;
704	int error;
705
706	error = zfs_secpolicy_write_perms(zc->zc_name,
707	    ZFS_DELEG_PERM_PROMOTE, cr);
708	if (error)
709		return (error);
710
711	error = dmu_objset_hold(zc->zc_name, FTAG, &clone);
712
713	if (error == 0) {
714		dsl_dataset_t *pclone = NULL;
715		dsl_dir_t *dd;
716		dd = clone->os_dsl_dataset->ds_dir;
717
718		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
719		error = dsl_dataset_hold_obj(dd->dd_pool,
720		    dd->dd_phys->dd_origin_obj, FTAG, &pclone);
721		rw_exit(&dd->dd_pool->dp_config_rwlock);
722		if (error) {
723			dmu_objset_rele(clone, FTAG);
724			return (error);
725		}
726
727		error = zfs_secpolicy_write_perms(zc->zc_name,
728		    ZFS_DELEG_PERM_MOUNT, cr);
729
730		dsl_dataset_name(pclone, parentname);
731		dmu_objset_rele(clone, FTAG);
732		dsl_dataset_rele(pclone, FTAG);
733		if (error == 0)
734			error = zfs_secpolicy_write_perms(parentname,
735			    ZFS_DELEG_PERM_PROMOTE, cr);
736	}
737	return (error);
738}
739
740static int
741zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
742{
743	int error;
744
745	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
746	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
747		return (error);
748
749	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
750	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
751		return (error);
752
753	return (zfs_secpolicy_write_perms(zc->zc_name,
754	    ZFS_DELEG_PERM_CREATE, cr));
755}
756
757int
758zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
759{
760	return (zfs_secpolicy_write_perms(name,
761	    ZFS_DELEG_PERM_SNAPSHOT, cr));
762}
763
764static int
765zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
766{
767
768	return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
769}
770
771static int
772zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
773{
774	char	parentname[MAXNAMELEN];
775	int	error;
776
777	if ((error = zfs_get_parent(zc->zc_name, parentname,
778	    sizeof (parentname))) != 0)
779		return (error);
780
781	if (zc->zc_value[0] != '\0') {
782		if ((error = zfs_secpolicy_write_perms(zc->zc_value,
783		    ZFS_DELEG_PERM_CLONE, cr)) != 0)
784			return (error);
785	}
786
787	if ((error = zfs_secpolicy_write_perms(parentname,
788	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
789		return (error);
790
791	error = zfs_secpolicy_write_perms(parentname,
792	    ZFS_DELEG_PERM_MOUNT, cr);
793
794	return (error);
795}
796
797static int
798zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
799{
800	int error;
801
802	error = secpolicy_fs_unmount(cr, NULL);
803	if (error) {
804		error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
805	}
806	return (error);
807}
808
809/*
810 * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
811 * SYS_CONFIG privilege, which is not available in a local zone.
812 */
813/* ARGSUSED */
814static int
815zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
816{
817	if (secpolicy_sys_config(cr, B_FALSE) != 0)
818		return (EPERM);
819
820	return (0);
821}
822
823/*
824 * Policy for fault injection.  Requires all privileges.
825 */
826/* ARGSUSED */
827static int
828zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
829{
830	return (secpolicy_zinject(cr));
831}
832
833static int
834zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
835{
836	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
837
838	if (prop == ZPROP_INVAL) {
839		if (!zfs_prop_user(zc->zc_value))
840			return (EINVAL);
841		return (zfs_secpolicy_write_perms(zc->zc_name,
842		    ZFS_DELEG_PERM_USERPROP, cr));
843	} else {
844		return (zfs_secpolicy_setprop(zc->zc_name, prop,
845		    NULL, cr));
846	}
847}
848
849static int
850zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
851{
852	int err = zfs_secpolicy_read(zc, cr);
853	if (err)
854		return (err);
855
856	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
857		return (EINVAL);
858
859	if (zc->zc_value[0] == 0) {
860		/*
861		 * They are asking about a posix uid/gid.  If it's
862		 * themself, allow it.
863		 */
864		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
865		    zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
866			if (zc->zc_guid == crgetuid(cr))
867				return (0);
868		} else {
869			if (groupmember(zc->zc_guid, cr))
870				return (0);
871		}
872	}
873
874	return (zfs_secpolicy_write_perms(zc->zc_name,
875	    userquota_perms[zc->zc_objset_type], cr));
876}
877
878static int
879zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr)
880{
881	int err = zfs_secpolicy_read(zc, cr);
882	if (err)
883		return (err);
884
885	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
886		return (EINVAL);
887
888	return (zfs_secpolicy_write_perms(zc->zc_name,
889	    userquota_perms[zc->zc_objset_type], cr));
890}
891
892static int
893zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
894{
895	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
896	    NULL, cr));
897}
898
899static int
900zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
901{
902	return (zfs_secpolicy_write_perms(zc->zc_name,
903	    ZFS_DELEG_PERM_HOLD, cr));
904}
905
906static int
907zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
908{
909	return (zfs_secpolicy_write_perms(zc->zc_name,
910	    ZFS_DELEG_PERM_RELEASE, cr));
911}
912
913/*
914 * Returns the nvlist as specified by the user in the zfs_cmd_t.
915 */
916static int
917get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
918{
919	char *packed;
920	int error;
921	nvlist_t *list = NULL;
922
923	/*
924	 * Read in and unpack the user-supplied nvlist.
925	 */
926	if (size == 0)
927		return (EINVAL);
928
929	packed = kmem_alloc(size, KM_SLEEP);
930
931	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
932	    iflag)) != 0) {
933		kmem_free(packed, size);
934		return (error);
935	}
936
937	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
938		kmem_free(packed, size);
939		return (error);
940	}
941
942	kmem_free(packed, size);
943
944	*nvp = list;
945	return (0);
946}
947
948static int
949fit_error_list(zfs_cmd_t *zc, nvlist_t **errors)
950{
951	size_t size;
952
953	VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
954
955	if (size > zc->zc_nvlist_dst_size) {
956		nvpair_t *more_errors;
957		int n = 0;
958
959		if (zc->zc_nvlist_dst_size < 1024)
960			return (ENOMEM);
961
962		VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0);
963		more_errors = nvlist_prev_nvpair(*errors, NULL);
964
965		do {
966			nvpair_t *pair = nvlist_prev_nvpair(*errors,
967			    more_errors);
968			VERIFY(nvlist_remove_nvpair(*errors, pair) == 0);
969			n++;
970			VERIFY(nvlist_size(*errors, &size,
971			    NV_ENCODE_NATIVE) == 0);
972		} while (size > zc->zc_nvlist_dst_size);
973
974		VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0);
975		VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0);
976		ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
977		ASSERT(size <= zc->zc_nvlist_dst_size);
978	}
979
980	return (0);
981}
982
983static int
984put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
985{
986	char *packed = NULL;
987	size_t size;
988	int error;
989
990	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
991
992	if (size > zc->zc_nvlist_dst_size) {
993		error = ENOMEM;
994	} else {
995		packed = kmem_alloc(size, KM_SLEEP);
996		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
997		    KM_SLEEP) == 0);
998		error = ddi_copyout(packed,
999		    (void *)(uintptr_t)zc->zc_nvlist_dst, size, zc->zc_iflags);
1000		kmem_free(packed, size);
1001	}
1002
1003	zc->zc_nvlist_dst_size = size;
1004	return (error);
1005}
1006
1007static int
1008getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1009{
1010	objset_t *os;
1011	int error;
1012
1013	error = dmu_objset_hold(dsname, FTAG, &os);
1014	if (error)
1015		return (error);
1016	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1017		dmu_objset_rele(os, FTAG);
1018		return (EINVAL);
1019	}
1020
1021	mutex_enter(&os->os_user_ptr_lock);
1022	*zfvp = dmu_objset_get_user(os);
1023	if (*zfvp) {
1024		VFS_HOLD((*zfvp)->z_vfs);
1025	} else {
1026		error = ESRCH;
1027	}
1028	mutex_exit(&os->os_user_ptr_lock);
1029	dmu_objset_rele(os, FTAG);
1030	return (error);
1031}
1032
1033/*
1034 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1035 * case its z_vfs will be NULL, and it will be opened as the owner.
1036 */
1037static int
1038zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp)
1039{
1040	int error = 0;
1041
1042	if (getzfsvfs(name, zfvp) != 0)
1043		error = zfsvfs_create(name, zfvp);
1044	if (error == 0) {
1045		rrw_enter(&(*zfvp)->z_teardown_lock, RW_READER, tag);
1046		if ((*zfvp)->z_unmounted) {
1047			/*
1048			 * XXX we could probably try again, since the unmounting
1049			 * thread should be just about to disassociate the
1050			 * objset from the zfsvfs.
1051			 */
1052			rrw_exit(&(*zfvp)->z_teardown_lock, tag);
1053			return (EBUSY);
1054		}
1055	}
1056	return (error);
1057}
1058
1059static void
1060zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1061{
1062	rrw_exit(&zfsvfs->z_teardown_lock, tag);
1063
1064	if (zfsvfs->z_vfs) {
1065		VFS_RELE(zfsvfs->z_vfs);
1066	} else {
1067		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1068		zfsvfs_free(zfsvfs);
1069	}
1070}
1071
1072static int
1073zfs_ioc_pool_create(zfs_cmd_t *zc)
1074{
1075	int error;
1076	nvlist_t *config, *props = NULL;
1077	nvlist_t *rootprops = NULL;
1078	nvlist_t *zplprops = NULL;
1079	char *buf;
1080
1081	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1082	    zc->zc_iflags, &config))
1083		return (error);
1084
1085	if (zc->zc_nvlist_src_size != 0 && (error =
1086	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1087	    zc->zc_iflags, &props))) {
1088		nvlist_free(config);
1089		return (error);
1090	}
1091
1092	if (props) {
1093		nvlist_t *nvl = NULL;
1094		uint64_t version = SPA_VERSION;
1095
1096		(void) nvlist_lookup_uint64(props,
1097		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1098		if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
1099			error = EINVAL;
1100			goto pool_props_bad;
1101		}
1102		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1103		if (nvl) {
1104			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1105			if (error != 0) {
1106				nvlist_free(config);
1107				nvlist_free(props);
1108				return (error);
1109			}
1110			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1111		}
1112		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1113		error = zfs_fill_zplprops_root(version, rootprops,
1114		    zplprops, NULL);
1115		if (error)
1116			goto pool_props_bad;
1117	}
1118
1119	buf = history_str_get(zc);
1120
1121	error = spa_create(zc->zc_name, config, props, buf, zplprops);
1122
1123	/*
1124	 * Set the remaining root properties
1125	 */
1126	if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1127	    ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1128		(void) spa_destroy(zc->zc_name);
1129
1130	if (buf != NULL)
1131		history_str_free(buf);
1132
1133pool_props_bad:
1134	nvlist_free(rootprops);
1135	nvlist_free(zplprops);
1136	nvlist_free(config);
1137	nvlist_free(props);
1138
1139	return (error);
1140}
1141
1142static int
1143zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1144{
1145	int error;
1146	zfs_log_history(zc);
1147	error = spa_destroy(zc->zc_name);
1148	if (error == 0)
1149		zvol_remove_minors(zc->zc_name);
1150	return (error);
1151}
1152
1153static int
1154zfs_ioc_pool_import(zfs_cmd_t *zc)
1155{
1156	nvlist_t *config, *props = NULL;
1157	uint64_t guid;
1158	int error;
1159
1160	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1161	    zc->zc_iflags, &config)) != 0)
1162		return (error);
1163
1164	if (zc->zc_nvlist_src_size != 0 && (error =
1165	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1166	    zc->zc_iflags, &props))) {
1167		nvlist_free(config);
1168		return (error);
1169	}
1170
1171	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1172	    guid != zc->zc_guid)
1173		error = EINVAL;
1174	else if (zc->zc_cookie)
1175		error = spa_import_verbatim(zc->zc_name, config, props);
1176	else
1177		error = spa_import(zc->zc_name, config, props);
1178
1179	if (zc->zc_nvlist_dst != 0)
1180		(void) put_nvlist(zc, config);
1181
1182	nvlist_free(config);
1183
1184	if (props)
1185		nvlist_free(props);
1186
1187	return (error);
1188}
1189
1190static int
1191zfs_ioc_pool_export(zfs_cmd_t *zc)
1192{
1193	int error;
1194	boolean_t force = (boolean_t)zc->zc_cookie;
1195	boolean_t hardforce = (boolean_t)zc->zc_guid;
1196
1197	zfs_log_history(zc);
1198	error = spa_export(zc->zc_name, NULL, force, hardforce);
1199	if (error == 0)
1200		zvol_remove_minors(zc->zc_name);
1201	return (error);
1202}
1203
1204static int
1205zfs_ioc_pool_configs(zfs_cmd_t *zc)
1206{
1207	nvlist_t *configs;
1208	int error;
1209
1210	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1211		return (EEXIST);
1212
1213	error = put_nvlist(zc, configs);
1214
1215	nvlist_free(configs);
1216
1217	return (error);
1218}
1219
1220static int
1221zfs_ioc_pool_stats(zfs_cmd_t *zc)
1222{
1223	nvlist_t *config;
1224	int error;
1225	int ret = 0;
1226
1227	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1228	    sizeof (zc->zc_value));
1229
1230	if (config != NULL) {
1231		ret = put_nvlist(zc, config);
1232		nvlist_free(config);
1233
1234		/*
1235		 * The config may be present even if 'error' is non-zero.
1236		 * In this case we return success, and preserve the real errno
1237		 * in 'zc_cookie'.
1238		 */
1239		zc->zc_cookie = error;
1240	} else {
1241		ret = error;
1242	}
1243
1244	return (ret);
1245}
1246
1247/*
1248 * Try to import the given pool, returning pool stats as appropriate so that
1249 * user land knows which devices are available and overall pool health.
1250 */
1251static int
1252zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1253{
1254	nvlist_t *tryconfig, *config;
1255	int error;
1256
1257	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1258	    zc->zc_iflags, &tryconfig)) != 0)
1259		return (error);
1260
1261	config = spa_tryimport(tryconfig);
1262
1263	nvlist_free(tryconfig);
1264
1265	if (config == NULL)
1266		return (EINVAL);
1267
1268	error = put_nvlist(zc, config);
1269	nvlist_free(config);
1270
1271	return (error);
1272}
1273
1274static int
1275zfs_ioc_pool_scrub(zfs_cmd_t *zc)
1276{
1277	spa_t *spa;
1278	int error;
1279
1280	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1281		return (error);
1282
1283	error = spa_scrub(spa, zc->zc_cookie);
1284
1285	spa_close(spa, FTAG);
1286
1287	return (error);
1288}
1289
1290static int
1291zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1292{
1293	spa_t *spa;
1294	int error;
1295
1296	error = spa_open(zc->zc_name, &spa, FTAG);
1297	if (error == 0) {
1298		spa_freeze(spa);
1299		spa_close(spa, FTAG);
1300	}
1301	return (error);
1302}
1303
1304static int
1305zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1306{
1307	spa_t *spa;
1308	int error;
1309
1310	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1311		return (error);
1312
1313	if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
1314		spa_close(spa, FTAG);
1315		return (EINVAL);
1316	}
1317
1318	spa_upgrade(spa, zc->zc_cookie);
1319	spa_close(spa, FTAG);
1320
1321	return (error);
1322}
1323
1324static int
1325zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1326{
1327	spa_t *spa;
1328	char *hist_buf;
1329	uint64_t size;
1330	int error;
1331
1332	if ((size = zc->zc_history_len) == 0)
1333		return (EINVAL);
1334
1335	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1336		return (error);
1337
1338	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1339		spa_close(spa, FTAG);
1340		return (ENOTSUP);
1341	}
1342
1343	hist_buf = kmem_alloc(size, KM_SLEEP);
1344	if ((error = spa_history_get(spa, &zc->zc_history_offset,
1345	    &zc->zc_history_len, hist_buf)) == 0) {
1346		error = ddi_copyout(hist_buf,
1347		    (void *)(uintptr_t)zc->zc_history,
1348		    zc->zc_history_len, zc->zc_iflags);
1349	}
1350
1351	spa_close(spa, FTAG);
1352	kmem_free(hist_buf, size);
1353	return (error);
1354}
1355
1356static int
1357zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1358{
1359	int error;
1360
1361	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
1362		return (error);
1363
1364	return (0);
1365}
1366
1367/*
1368 * inputs:
1369 * zc_name		name of filesystem
1370 * zc_obj		object to find
1371 *
1372 * outputs:
1373 * zc_value		name of object
1374 */
1375static int
1376zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1377{
1378	objset_t *os;
1379	int error;
1380
1381	/* XXX reading from objset not owned */
1382	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1383		return (error);
1384	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1385		dmu_objset_rele(os, FTAG);
1386		return (EINVAL);
1387	}
1388	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1389	    sizeof (zc->zc_value));
1390	dmu_objset_rele(os, FTAG);
1391
1392	return (error);
1393}
1394
1395static int
1396zfs_ioc_vdev_add(zfs_cmd_t *zc)
1397{
1398	spa_t *spa;
1399	int error;
1400	nvlist_t *config, **l2cache, **spares;
1401	uint_t nl2cache = 0, nspares = 0;
1402
1403	error = spa_open(zc->zc_name, &spa, FTAG);
1404	if (error != 0)
1405		return (error);
1406
1407	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1408	    zc->zc_iflags, &config);
1409	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1410	    &l2cache, &nl2cache);
1411
1412	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1413	    &spares, &nspares);
1414
1415	/*
1416	 * A root pool with concatenated devices is not supported.
1417	 * Thus, can not add a device to a root pool.
1418	 *
1419	 * Intent log device can not be added to a rootpool because
1420	 * during mountroot, zil is replayed, a seperated log device
1421	 * can not be accessed during the mountroot time.
1422	 *
1423	 * l2cache and spare devices are ok to be added to a rootpool.
1424	 */
1425	if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1426		nvlist_free(config);
1427		spa_close(spa, FTAG);
1428		return (EDOM);
1429	}
1430
1431	if (error == 0) {
1432		error = spa_vdev_add(spa, config);
1433		nvlist_free(config);
1434	}
1435	spa_close(spa, FTAG);
1436	return (error);
1437}
1438
1439static int
1440zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1441{
1442	spa_t *spa;
1443	int error;
1444
1445	error = spa_open(zc->zc_name, &spa, FTAG);
1446	if (error != 0)
1447		return (error);
1448	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1449	spa_close(spa, FTAG);
1450	return (error);
1451}
1452
1453static int
1454zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1455{
1456	spa_t *spa;
1457	int error;
1458	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1459
1460	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1461		return (error);
1462	switch (zc->zc_cookie) {
1463	case VDEV_STATE_ONLINE:
1464		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1465		break;
1466
1467	case VDEV_STATE_OFFLINE:
1468		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1469		break;
1470
1471	case VDEV_STATE_FAULTED:
1472		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1473		    zc->zc_obj != VDEV_AUX_EXTERNAL)
1474			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1475
1476		error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1477		break;
1478
1479	case VDEV_STATE_DEGRADED:
1480		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1481		    zc->zc_obj != VDEV_AUX_EXTERNAL)
1482			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1483
1484		error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1485		break;
1486
1487	default:
1488		error = EINVAL;
1489	}
1490	zc->zc_cookie = newstate;
1491	spa_close(spa, FTAG);
1492	return (error);
1493}
1494
1495static int
1496zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1497{
1498	spa_t *spa;
1499	int replacing = zc->zc_cookie;
1500	nvlist_t *config;
1501	int error;
1502
1503	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1504		return (error);
1505
1506	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1507	    zc->zc_iflags, &config)) == 0) {
1508		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1509		nvlist_free(config);
1510	}
1511
1512	spa_close(spa, FTAG);
1513	return (error);
1514}
1515
1516static int
1517zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1518{
1519	spa_t *spa;
1520	int error;
1521
1522	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1523		return (error);
1524
1525	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1526
1527	spa_close(spa, FTAG);
1528	return (error);
1529}
1530
1531static int
1532zfs_ioc_vdev_split(zfs_cmd_t *zc)
1533{
1534	spa_t *spa;
1535	nvlist_t *config, *props = NULL;
1536	int error;
1537	boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1538
1539	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1540		return (error);
1541
1542	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1543	    zc->zc_iflags, &config)) {
1544		spa_close(spa, FTAG);
1545		return (error);
1546	}
1547
1548	if (zc->zc_nvlist_src_size != 0 && (error =
1549	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1550	    zc->zc_iflags, &props))) {
1551		spa_close(spa, FTAG);
1552		nvlist_free(config);
1553		return (error);
1554	}
1555
1556	error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1557
1558	spa_close(spa, FTAG);
1559
1560	nvlist_free(config);
1561	nvlist_free(props);
1562
1563	return (error);
1564}
1565
1566static int
1567zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1568{
1569	spa_t *spa;
1570	char *path = zc->zc_value;
1571	uint64_t guid = zc->zc_guid;
1572	int error;
1573
1574	error = spa_open(zc->zc_name, &spa, FTAG);
1575	if (error != 0)
1576		return (error);
1577
1578	error = spa_vdev_setpath(spa, guid, path);
1579	spa_close(spa, FTAG);
1580	return (error);
1581}
1582
1583static int
1584zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
1585{
1586	spa_t *spa;
1587	char *fru = zc->zc_value;
1588	uint64_t guid = zc->zc_guid;
1589	int error;
1590
1591	error = spa_open(zc->zc_name, &spa, FTAG);
1592	if (error != 0)
1593		return (error);
1594
1595	error = spa_vdev_setfru(spa, guid, fru);
1596	spa_close(spa, FTAG);
1597	return (error);
1598}
1599
1600/*
1601 * inputs:
1602 * zc_name		name of filesystem
1603 * zc_nvlist_dst_size	size of buffer for property nvlist
1604 *
1605 * outputs:
1606 * zc_objset_stats	stats
1607 * zc_nvlist_dst	property nvlist
1608 * zc_nvlist_dst_size	size of property nvlist
1609 */
1610static int
1611zfs_ioc_objset_stats(zfs_cmd_t *zc)
1612{
1613	objset_t *os = NULL;
1614	int error;
1615	nvlist_t *nv;
1616
1617	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
1618		return (error);
1619
1620	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1621
1622	if (zc->zc_nvlist_dst != 0 &&
1623	    (error = dsl_prop_get_all(os, &nv)) == 0) {
1624		dmu_objset_stats(os, nv);
1625		/*
1626		 * NB: zvol_get_stats() will read the objset contents,
1627		 * which we aren't supposed to do with a
1628		 * DS_MODE_USER hold, because it could be
1629		 * inconsistent.  So this is a bit of a workaround...
1630		 * XXX reading with out owning
1631		 */
1632		if (!zc->zc_objset_stats.dds_inconsistent) {
1633			if (dmu_objset_type(os) == DMU_OST_ZVOL)
1634				VERIFY(zvol_get_stats(os, nv) == 0);
1635		}
1636		error = put_nvlist(zc, nv);
1637		nvlist_free(nv);
1638	}
1639
1640	dmu_objset_rele(os, FTAG);
1641	return (error);
1642}
1643
1644/*
1645 * inputs:
1646 * zc_name		name of filesystem
1647 * zc_nvlist_dst_size	size of buffer for property nvlist
1648 *
1649 * outputs:
1650 * zc_nvlist_dst	received property nvlist
1651 * zc_nvlist_dst_size	size of received property nvlist
1652 *
1653 * Gets received properties (distinct from local properties on or after
1654 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
1655 * local property values.
1656 */
1657static int
1658zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
1659{
1660	objset_t *os = NULL;
1661	int error;
1662	nvlist_t *nv;
1663
1664	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
1665		return (error);
1666
1667	/*
1668	 * Without this check, we would return local property values if the
1669	 * caller has not already received properties on or after
1670	 * SPA_VERSION_RECVD_PROPS.
1671	 */
1672	if (!dsl_prop_get_hasrecvd(os)) {
1673		dmu_objset_rele(os, FTAG);
1674		return (ENOTSUP);
1675	}
1676
1677	if (zc->zc_nvlist_dst != 0 &&
1678	    (error = dsl_prop_get_received(os, &nv)) == 0) {
1679		error = put_nvlist(zc, nv);
1680		nvlist_free(nv);
1681	}
1682
1683	dmu_objset_rele(os, FTAG);
1684	return (error);
1685}
1686
1687static int
1688nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
1689{
1690	uint64_t value;
1691	int error;
1692
1693	/*
1694	 * zfs_get_zplprop() will either find a value or give us
1695	 * the default value (if there is one).
1696	 */
1697	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
1698		return (error);
1699	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
1700	return (0);
1701}
1702
1703/*
1704 * inputs:
1705 * zc_name		name of filesystem
1706 * zc_nvlist_dst_size	size of buffer for zpl property nvlist
1707 *
1708 * outputs:
1709 * zc_nvlist_dst	zpl property nvlist
1710 * zc_nvlist_dst_size	size of zpl property nvlist
1711 */
1712static int
1713zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
1714{
1715	objset_t *os;
1716	int err;
1717
1718	/* XXX reading without owning */
1719	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
1720		return (err);
1721
1722	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1723
1724	/*
1725	 * NB: nvl_add_zplprop() will read the objset contents,
1726	 * which we aren't supposed to do with a DS_MODE_USER
1727	 * hold, because it could be inconsistent.
1728	 */
1729	if (zc->zc_nvlist_dst != 0 &&
1730	    !zc->zc_objset_stats.dds_inconsistent &&
1731	    dmu_objset_type(os) == DMU_OST_ZFS) {
1732		nvlist_t *nv;
1733
1734		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1735		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
1736		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
1737		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
1738		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
1739			err = put_nvlist(zc, nv);
1740		nvlist_free(nv);
1741	} else {
1742		err = ENOENT;
1743	}
1744	dmu_objset_rele(os, FTAG);
1745	return (err);
1746}
1747
1748static boolean_t
1749dataset_name_hidden(const char *name)
1750{
1751	/*
1752	 * Skip over datasets that are not visible in this zone,
1753	 * internal datasets (which have a $ in their name), and
1754	 * temporary datasets (which have a % in their name).
1755	 */
1756	if (strchr(name, '$') != NULL)
1757		return (B_TRUE);
1758	if (strchr(name, '%') != NULL)
1759		return (B_TRUE);
1760	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
1761		return (B_TRUE);
1762	return (B_FALSE);
1763}
1764
1765/*
1766 * inputs:
1767 * zc_name		name of filesystem
1768 * zc_cookie		zap cursor
1769 * zc_nvlist_dst_size	size of buffer for property nvlist
1770 *
1771 * outputs:
1772 * zc_name		name of next filesystem
1773 * zc_cookie		zap cursor
1774 * zc_objset_stats	stats
1775 * zc_nvlist_dst	property nvlist
1776 * zc_nvlist_dst_size	size of property nvlist
1777 */
1778static int
1779zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1780{
1781	objset_t *os;
1782	int error;
1783	char *p;
1784	size_t orig_len = strlen(zc->zc_name);
1785
1786top:
1787	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
1788		if (error == ENOENT)
1789			error = ESRCH;
1790		return (error);
1791	}
1792
1793	p = strrchr(zc->zc_name, '/');
1794	if (p == NULL || p[1] != '\0')
1795		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1796	p = zc->zc_name + strlen(zc->zc_name);
1797
1798	/*
1799	 * Pre-fetch the datasets.  dmu_objset_prefetch() always returns 0
1800	 * but is not declared void because its called by dmu_objset_find().
1801	 */
1802	if (zc->zc_cookie == 0) {
1803		uint64_t cookie = 0;
1804		int len = sizeof (zc->zc_name) - (p - zc->zc_name);
1805
1806		while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
1807			(void) dmu_objset_prefetch(p, NULL);
1808	}
1809
1810	do {
1811		error = dmu_dir_list_next(os,
1812		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
1813		    NULL, &zc->zc_cookie);
1814		if (error == ENOENT)
1815			error = ESRCH;
1816	} while (error == 0 && dataset_name_hidden(zc->zc_name) &&
1817	    !(zc->zc_iflags & FKIOCTL));
1818	dmu_objset_rele(os, FTAG);
1819
1820	/*
1821	 * If it's an internal dataset (ie. with a '$' in its name),
1822	 * don't try to get stats for it, otherwise we'll return ENOENT.
1823	 */
1824	if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
1825		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1826		if (error == ENOENT) {
1827			/* We lost a race with destroy, get the next one. */
1828			zc->zc_name[orig_len] = '\0';
1829			goto top;
1830		}
1831	}
1832	return (error);
1833}
1834
1835/*
1836 * inputs:
1837 * zc_name		name of filesystem
1838 * zc_cookie		zap cursor
1839 * zc_nvlist_dst_size	size of buffer for property nvlist
1840 *
1841 * outputs:
1842 * zc_name		name of next snapshot
1843 * zc_objset_stats	stats
1844 * zc_nvlist_dst	property nvlist
1845 * zc_nvlist_dst_size	size of property nvlist
1846 */
1847static int
1848zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1849{
1850	objset_t *os;
1851	int error;
1852
1853top:
1854	if (zc->zc_cookie == 0)
1855		(void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
1856		    NULL, DS_FIND_SNAPSHOTS);
1857
1858	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
1859	if (error)
1860		return (error == ENOENT ? ESRCH : error);
1861
1862	/*
1863	 * A dataset name of maximum length cannot have any snapshots,
1864	 * so exit immediately.
1865	 */
1866	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1867		dmu_objset_rele(os, FTAG);
1868		return (ESRCH);
1869	}
1870
1871	error = dmu_snapshot_list_next(os,
1872	    sizeof (zc->zc_name) - strlen(zc->zc_name),
1873	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
1874	dmu_objset_rele(os, FTAG);
1875	if (error == 0) {
1876		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1877		if (error == ENOENT)  {
1878			/* We lost a race with destroy, get the next one. */
1879			*strchr(zc->zc_name, '@') = '\0';
1880			goto top;
1881		}
1882	} else if (error == ENOENT) {
1883		error = ESRCH;
1884	}
1885
1886	/* if we failed, undo the @ that we tacked on to zc_name */
1887	if (error)
1888		*strchr(zc->zc_name, '@') = '\0';
1889	return (error);
1890}
1891
1892static int
1893zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
1894{
1895	const char *propname = nvpair_name(pair);
1896	uint64_t *valary;
1897	unsigned int vallen;
1898	const char *domain;
1899	zfs_userquota_prop_t type;
1900	uint64_t rid;
1901	uint64_t quota;
1902	zfsvfs_t *zfsvfs;
1903	int err;
1904
1905	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
1906		nvlist_t *attrs;
1907		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
1908		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
1909		    &pair) == 0);
1910	}
1911
1912	VERIFY(nvpair_value_uint64_array(pair, &valary, &vallen) == 0);
1913	VERIFY(vallen == 3);
1914	type = valary[0];
1915	rid = valary[1];
1916	quota = valary[2];
1917	/*
1918	 * The propname is encoded as
1919	 * userquota@<rid>-<domain>.
1920	 */
1921	domain = strchr(propname, '-') + 1;
1922
1923	err = zfsvfs_hold(dsname, FTAG, &zfsvfs);
1924	if (err == 0) {
1925		err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
1926		zfsvfs_rele(zfsvfs, FTAG);
1927	}
1928
1929	return (err);
1930}
1931
1932/*
1933 * If the named property is one that has a special function to set its value,
1934 * return 0 on success and a positive error code on failure; otherwise if it is
1935 * not one of the special properties handled by this function, return -1.
1936 *
1937 * XXX: It would be better for callers of the properety interface if we handled
1938 * these special cases in dsl_prop.c (in the dsl layer).
1939 */
1940static int
1941zfs_prop_set_special(const char *dsname, zprop_source_t source,
1942    nvpair_t *pair)
1943{
1944	const char *propname = nvpair_name(pair);
1945	zfs_prop_t prop = zfs_name_to_prop(propname);
1946	uint64_t intval;
1947	int err;
1948
1949	if (prop == ZPROP_INVAL) {
1950		if (zfs_prop_userquota(propname))
1951			return (zfs_prop_set_userquota(dsname, pair));
1952		return (-1);
1953	}
1954
1955	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
1956		nvlist_t *attrs;
1957		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
1958		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
1959		    &pair) == 0);
1960	}
1961
1962	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
1963		return (-1);
1964
1965	VERIFY(0 == nvpair_value_uint64(pair, &intval));
1966
1967	switch (prop) {
1968	case ZFS_PROP_QUOTA:
1969		err = dsl_dir_set_quota(dsname, source, intval);
1970		break;
1971	case ZFS_PROP_REFQUOTA:
1972		err = dsl_dataset_set_quota(dsname, source, intval);
1973		break;
1974	case ZFS_PROP_RESERVATION:
1975		err = dsl_dir_set_reservation(dsname, source, intval);
1976		break;
1977	case ZFS_PROP_REFRESERVATION:
1978		err = dsl_dataset_set_reservation(dsname, source, intval);
1979		break;
1980	case ZFS_PROP_VOLSIZE:
1981		err = zvol_set_volsize(dsname, ddi_driver_major(zfs_dip),
1982		    intval);
1983		break;
1984	case ZFS_PROP_VERSION:
1985	{
1986		zfsvfs_t *zfsvfs;
1987
1988		if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs)) != 0)
1989			break;
1990
1991		err = zfs_set_version(zfsvfs, intval);
1992		zfsvfs_rele(zfsvfs, FTAG);
1993
1994		if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
1995			zfs_cmd_t *zc;
1996
1997			zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
1998			(void) strcpy(zc->zc_name, dsname);
1999			(void) zfs_ioc_userspace_upgrade(zc);
2000			kmem_free(zc, sizeof (zfs_cmd_t));
2001		}
2002		break;
2003	}
2004
2005	default:
2006		err = -1;
2007	}
2008
2009	return (err);
2010}
2011
2012/*
2013 * This function is best effort. If it fails to set any of the given properties,
2014 * it continues to set as many as it can and returns the first error
2015 * encountered. If the caller provides a non-NULL errlist, it also gives the
2016 * complete list of names of all the properties it failed to set along with the
2017 * corresponding error numbers. The caller is responsible for freeing the
2018 * returned errlist.
2019 *
2020 * If every property is set successfully, zero is returned and the list pointed
2021 * at by errlist is NULL.
2022 */
2023int
2024zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2025    nvlist_t **errlist)
2026{
2027	nvpair_t *pair;
2028	nvpair_t *propval;
2029	int rv = 0;
2030	uint64_t intval;
2031	char *strval;
2032	nvlist_t *genericnvl;
2033	nvlist_t *errors;
2034	nvlist_t *retrynvl;
2035
2036	VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2037	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2038	VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2039
2040retry:
2041	pair = NULL;
2042	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2043		const char *propname = nvpair_name(pair);
2044		zfs_prop_t prop = zfs_name_to_prop(propname);
2045		int err = 0;
2046
2047		/* decode the property value */
2048		propval = pair;
2049		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2050			nvlist_t *attrs;
2051			VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2052			VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2053			    &propval) == 0);
2054		}
2055
2056		/* Validate value type */
2057		if (prop == ZPROP_INVAL) {
2058			if (zfs_prop_user(propname)) {
2059				if (nvpair_type(propval) != DATA_TYPE_STRING)
2060					err = EINVAL;
2061			} else if (zfs_prop_userquota(propname)) {
2062				if (nvpair_type(propval) !=
2063				    DATA_TYPE_UINT64_ARRAY)
2064					err = EINVAL;
2065			}
2066		} else {
2067			if (nvpair_type(propval) == DATA_TYPE_STRING) {
2068				if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2069					err = EINVAL;
2070			} else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2071				const char *unused;
2072
2073				VERIFY(nvpair_value_uint64(propval,
2074				    &intval) == 0);
2075
2076				switch (zfs_prop_get_type(prop)) {
2077				case PROP_TYPE_NUMBER:
2078					break;
2079				case PROP_TYPE_STRING:
2080					err = EINVAL;
2081					break;
2082				case PROP_TYPE_INDEX:
2083					if (zfs_prop_index_to_string(prop,
2084					    intval, &unused) != 0)
2085						err = EINVAL;
2086					break;
2087				default:
2088					cmn_err(CE_PANIC,
2089					    "unknown property type");
2090				}
2091			} else {
2092				err = EINVAL;
2093			}
2094		}
2095
2096		/* Validate permissions */
2097		if (err == 0)
2098			err = zfs_check_settable(dsname, pair, CRED());
2099
2100		if (err == 0) {
2101			err = zfs_prop_set_special(dsname, source, pair);
2102			if (err == -1) {
2103				/*
2104				 * For better performance we build up a list of
2105				 * properties to set in a single transaction.
2106				 */
2107				err = nvlist_add_nvpair(genericnvl, pair);
2108			} else if (err != 0 && nvl != retrynvl) {
2109				/*
2110				 * This may be a spurious error caused by
2111				 * receiving quota and reservation out of order.
2112				 * Try again in a second pass.
2113				 */
2114				err = nvlist_add_nvpair(retrynvl, pair);
2115			}
2116		}
2117
2118		if (err != 0)
2119			VERIFY(nvlist_add_int32(errors, propname, err) == 0);
2120	}
2121
2122	if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2123		nvl = retrynvl;
2124		goto retry;
2125	}
2126
2127	if (!nvlist_empty(genericnvl) &&
2128	    dsl_props_set(dsname, source, genericnvl) != 0) {
2129		/*
2130		 * If this fails, we still want to set as many properties as we
2131		 * can, so try setting them individually.
2132		 */
2133		pair = NULL;
2134		while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2135			const char *propname = nvpair_name(pair);
2136			int err = 0;
2137
2138			propval = pair;
2139			if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2140				nvlist_t *attrs;
2141				VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2142				VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2143				    &propval) == 0);
2144			}
2145
2146			if (nvpair_type(propval) == DATA_TYPE_STRING) {
2147				VERIFY(nvpair_value_string(propval,
2148				    &strval) == 0);
2149				err = dsl_prop_set(dsname, propname, source, 1,
2150				    strlen(strval) + 1, strval);
2151			} else {
2152				VERIFY(nvpair_value_uint64(propval,
2153				    &intval) == 0);
2154				err = dsl_prop_set(dsname, propname, source, 8,
2155				    1, &intval);
2156			}
2157
2158			if (err != 0) {
2159				VERIFY(nvlist_add_int32(errors, propname,
2160				    err) == 0);
2161			}
2162		}
2163	}
2164	nvlist_free(genericnvl);
2165	nvlist_free(retrynvl);
2166
2167	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
2168		nvlist_free(errors);
2169		errors = NULL;
2170	} else {
2171		VERIFY(nvpair_value_int32(pair, &rv) == 0);
2172	}
2173
2174	if (errlist == NULL)
2175		nvlist_free(errors);
2176	else
2177		*errlist = errors;
2178
2179	return (rv);
2180}
2181
2182/*
2183 * Check that all the properties are valid user properties.
2184 */
2185static int
2186zfs_check_userprops(char *fsname, nvlist_t *nvl)
2187{
2188	nvpair_t *pair = NULL;
2189	int error = 0;
2190
2191	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2192		const char *propname = nvpair_name(pair);
2193		char *valstr;
2194
2195		if (!zfs_prop_user(propname) ||
2196		    nvpair_type(pair) != DATA_TYPE_STRING)
2197			return (EINVAL);
2198
2199		if (error = zfs_secpolicy_write_perms(fsname,
2200		    ZFS_DELEG_PERM_USERPROP, CRED()))
2201			return (error);
2202
2203		if (strlen(propname) >= ZAP_MAXNAMELEN)
2204			return (ENAMETOOLONG);
2205
2206		VERIFY(nvpair_value_string(pair, &valstr) == 0);
2207		if (strlen(valstr) >= ZAP_MAXVALUELEN)
2208			return (E2BIG);
2209	}
2210	return (0);
2211}
2212
2213static void
2214props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2215{
2216	nvpair_t *pair;
2217
2218	VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2219
2220	pair = NULL;
2221	while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2222		if (nvlist_exists(skipped, nvpair_name(pair)))
2223			continue;
2224
2225		VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2226	}
2227}
2228
2229static int
2230clear_received_props(objset_t *os, const char *fs, nvlist_t *props,
2231    nvlist_t *skipped)
2232{
2233	int err = 0;
2234	nvlist_t *cleared_props = NULL;
2235	props_skip(props, skipped, &cleared_props);
2236	if (!nvlist_empty(cleared_props)) {
2237		/*
2238		 * Acts on local properties until the dataset has received
2239		 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2240		 */
2241		zprop_source_t flags = (ZPROP_SRC_NONE |
2242		    (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0));
2243		err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL);
2244	}
2245	nvlist_free(cleared_props);
2246	return (err);
2247}
2248
2249/*
2250 * inputs:
2251 * zc_name		name of filesystem
2252 * zc_value		name of property to set
2253 * zc_nvlist_src{_size}	nvlist of properties to apply
2254 * zc_cookie		received properties flag
2255 *
2256 * outputs:
2257 * zc_nvlist_dst{_size} error for each unapplied received property
2258 */
2259static int
2260zfs_ioc_set_prop(zfs_cmd_t *zc)
2261{
2262	nvlist_t *nvl;
2263	boolean_t received = zc->zc_cookie;
2264	zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2265	    ZPROP_SRC_LOCAL);
2266	nvlist_t *errors = NULL;
2267	int error;
2268
2269	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2270	    zc->zc_iflags, &nvl)) != 0)
2271		return (error);
2272
2273	if (received) {
2274		nvlist_t *origprops;
2275		objset_t *os;
2276
2277		if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
2278			if (dsl_prop_get_received(os, &origprops) == 0) {
2279				(void) clear_received_props(os,
2280				    zc->zc_name, origprops, nvl);
2281				nvlist_free(origprops);
2282			}
2283
2284			dsl_prop_set_hasrecvd(os);
2285			dmu_objset_rele(os, FTAG);
2286		}
2287	}
2288
2289	error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors);
2290
2291	if (zc->zc_nvlist_dst != 0 && errors != 0) {
2292		(void) put_nvlist(zc, errors);
2293	}
2294
2295	nvlist_free(errors);
2296	nvlist_free(nvl);
2297	return (error);
2298}
2299
2300/*
2301 * inputs:
2302 * zc_name		name of filesystem
2303 * zc_value		name of property to inherit
2304 * zc_cookie		revert to received value if TRUE
2305 *
2306 * outputs:		none
2307 */
2308static int
2309zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2310{
2311	const char *propname = zc->zc_value;
2312	zfs_prop_t prop = zfs_name_to_prop(propname);
2313	boolean_t received = zc->zc_cookie;
2314	zprop_source_t source = (received
2315	    ? ZPROP_SRC_NONE		/* revert to received value, if any */
2316	    : ZPROP_SRC_INHERITED);	/* explicitly inherit */
2317
2318	if (received) {
2319		nvlist_t *dummy;
2320		nvpair_t *pair;
2321		zprop_type_t type;
2322		int err;
2323
2324		/*
2325		 * zfs_prop_set_special() expects properties in the form of an
2326		 * nvpair with type info.
2327		 */
2328		if (prop == ZPROP_INVAL) {
2329			if (!zfs_prop_user(propname))
2330				return (EINVAL);
2331
2332			type = PROP_TYPE_STRING;
2333		} else if (prop == ZFS_PROP_VOLSIZE ||
2334		    prop == ZFS_PROP_VERSION) {
2335			return (EINVAL);
2336		} else {
2337			type = zfs_prop_get_type(prop);
2338		}
2339
2340		VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2341
2342		switch (type) {
2343		case PROP_TYPE_STRING:
2344			VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2345			break;
2346		case PROP_TYPE_NUMBER:
2347		case PROP_TYPE_INDEX:
2348			VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2349			break;
2350		default:
2351			nvlist_free(dummy);
2352			return (EINVAL);
2353		}
2354
2355		pair = nvlist_next_nvpair(dummy, NULL);
2356		err = zfs_prop_set_special(zc->zc_name, source, pair);
2357		nvlist_free(dummy);
2358		if (err != -1)
2359			return (err); /* special property already handled */
2360	} else {
2361		/*
2362		 * Only check this in the non-received case. We want to allow
2363		 * 'inherit -S' to revert non-inheritable properties like quota
2364		 * and reservation to the received or default values even though
2365		 * they are not considered inheritable.
2366		 */
2367		if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2368			return (EINVAL);
2369	}
2370
2371	/* the property name has been validated by zfs_secpolicy_inherit() */
2372	return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL));
2373}
2374
2375static int
2376zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2377{
2378	nvlist_t *props;
2379	spa_t *spa;
2380	int error;
2381	nvpair_t *pair;
2382
2383	if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2384	    zc->zc_iflags, &props))
2385		return (error);
2386
2387	/*
2388	 * If the only property is the configfile, then just do a spa_lookup()
2389	 * to handle the faulted case.
2390	 */
2391	pair = nvlist_next_nvpair(props, NULL);
2392	if (pair != NULL && strcmp(nvpair_name(pair),
2393	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2394	    nvlist_next_nvpair(props, pair) == NULL) {
2395		mutex_enter(&spa_namespace_lock);
2396		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2397			spa_configfile_set(spa, props, B_FALSE);
2398			spa_config_sync(spa, B_FALSE, B_TRUE);
2399		}
2400		mutex_exit(&spa_namespace_lock);
2401		if (spa != NULL) {
2402			nvlist_free(props);
2403			return (0);
2404		}
2405	}
2406
2407	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2408		nvlist_free(props);
2409		return (error);
2410	}
2411
2412	error = spa_prop_set(spa, props);
2413
2414	nvlist_free(props);
2415	spa_close(spa, FTAG);
2416
2417	return (error);
2418}
2419
2420static int
2421zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2422{
2423	spa_t *spa;
2424	int error;
2425	nvlist_t *nvp = NULL;
2426
2427	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2428		/*
2429		 * If the pool is faulted, there may be properties we can still
2430		 * get (such as altroot and cachefile), so attempt to get them
2431		 * anyway.
2432		 */
2433		mutex_enter(&spa_namespace_lock);
2434		if ((spa = spa_lookup(zc->zc_name)) != NULL)
2435			error = spa_prop_get(spa, &nvp);
2436		mutex_exit(&spa_namespace_lock);
2437	} else {
2438		error = spa_prop_get(spa, &nvp);
2439		spa_close(spa, FTAG);
2440	}
2441
2442	if (error == 0 && zc->zc_nvlist_dst != 0)
2443		error = put_nvlist(zc, nvp);
2444	else
2445		error = EFAULT;
2446
2447	nvlist_free(nvp);
2448	return (error);
2449}
2450
2451static int
2452zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
2453{
2454	nvlist_t *nvp;
2455	int error;
2456	uint32_t uid;
2457	uint32_t gid;
2458	uint32_t *groups;
2459	uint_t group_cnt;
2460	cred_t	*usercred;
2461
2462	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2463	    zc->zc_iflags, &nvp)) != 0) {
2464		return (error);
2465	}
2466
2467	if ((error = nvlist_lookup_uint32(nvp,
2468	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
2469		nvlist_free(nvp);
2470		return (EPERM);
2471	}
2472
2473	if ((error = nvlist_lookup_uint32(nvp,
2474	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
2475		nvlist_free(nvp);
2476		return (EPERM);
2477	}
2478
2479	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
2480	    &groups, &group_cnt)) != 0) {
2481		nvlist_free(nvp);
2482		return (EPERM);
2483	}
2484	usercred = cralloc();
2485	if ((crsetugid(usercred, uid, gid) != 0) ||
2486	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
2487		nvlist_free(nvp);
2488		crfree(usercred);
2489		return (EPERM);
2490	}
2491	nvlist_free(nvp);
2492	error = dsl_deleg_access(zc->zc_name,
2493	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
2494	crfree(usercred);
2495	return (error);
2496}
2497
2498/*
2499 * inputs:
2500 * zc_name		name of filesystem
2501 * zc_nvlist_src{_size}	nvlist of delegated permissions
2502 * zc_perm_action	allow/unallow flag
2503 *
2504 * outputs:		none
2505 */
2506static int
2507zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2508{
2509	int error;
2510	nvlist_t *fsaclnv = NULL;
2511
2512	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2513	    zc->zc_iflags, &fsaclnv)) != 0)
2514		return (error);
2515
2516	/*
2517	 * Verify nvlist is constructed correctly
2518	 */
2519	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2520		nvlist_free(fsaclnv);
2521		return (EINVAL);
2522	}
2523
2524	/*
2525	 * If we don't have PRIV_SYS_MOUNT, then validate
2526	 * that user is allowed to hand out each permission in
2527	 * the nvlist(s)
2528	 */
2529
2530	error = secpolicy_zfs(CRED());
2531	if (error) {
2532		if (zc->zc_perm_action == B_FALSE) {
2533			error = dsl_deleg_can_allow(zc->zc_name,
2534			    fsaclnv, CRED());
2535		} else {
2536			error = dsl_deleg_can_unallow(zc->zc_name,
2537			    fsaclnv, CRED());
2538		}
2539	}
2540
2541	if (error == 0)
2542		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2543
2544	nvlist_free(fsaclnv);
2545	return (error);
2546}
2547
2548/*
2549 * inputs:
2550 * zc_name		name of filesystem
2551 *
2552 * outputs:
2553 * zc_nvlist_src{_size}	nvlist of delegated permissions
2554 */
2555static int
2556zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2557{
2558	nvlist_t *nvp;
2559	int error;
2560
2561	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2562		error = put_nvlist(zc, nvp);
2563		nvlist_free(nvp);
2564	}
2565
2566	return (error);
2567}
2568
2569/*
2570 * Search the vfs list for a specified resource.  Returns a pointer to it
2571 * or NULL if no suitable entry is found. The caller of this routine
2572 * is responsible for releasing the returned vfs pointer.
2573 */
2574static vfs_t *
2575zfs_get_vfs(const char *resource)
2576{
2577
2578	printf("XXX zfs_get_vfs write me\n");
2579	return NULL;
2580}
2581
2582/* ARGSUSED */
2583static void
2584zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2585{
2586	zfs_creat_t *zct = arg;
2587
2588	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2589}
2590
2591#define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
2592
2593/*
2594 * inputs:
2595 * createprops		list of properties requested by creator
2596 * default_zplver	zpl version to use if unspecified in createprops
2597 * fuids_ok		fuids allowed in this version of the spa?
2598 * os			parent objset pointer (NULL if root fs)
2599 *
2600 * outputs:
2601 * zplprops	values for the zplprops we attach to the master node object
2602 * is_ci	true if requested file system will be purely case-insensitive
2603 *
2604 * Determine the settings for utf8only, normalization and
2605 * casesensitivity.  Specific values may have been requested by the
2606 * creator and/or we can inherit values from the parent dataset.  If
2607 * the file system is of too early a vintage, a creator can not
2608 * request settings for these properties, even if the requested
2609 * setting is the default value.  We don't actually want to create dsl
2610 * properties for these, so remove them from the source nvlist after
2611 * processing.
2612 */
2613static int
2614zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
2615    boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
2616    boolean_t *is_ci)
2617{
2618	uint64_t sense = ZFS_PROP_UNDEFINED;
2619	uint64_t norm = ZFS_PROP_UNDEFINED;
2620	uint64_t u8 = ZFS_PROP_UNDEFINED;
2621
2622	ASSERT(zplprops != NULL);
2623
2624	/*
2625	 * Pull out creator prop choices, if any.
2626	 */
2627	if (createprops) {
2628		(void) nvlist_lookup_uint64(createprops,
2629		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2630		(void) nvlist_lookup_uint64(createprops,
2631		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2632		(void) nvlist_remove_all(createprops,
2633		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2634		(void) nvlist_lookup_uint64(createprops,
2635		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2636		(void) nvlist_remove_all(createprops,
2637		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2638		(void) nvlist_lookup_uint64(createprops,
2639		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2640		(void) nvlist_remove_all(createprops,
2641		    zfs_prop_to_name(ZFS_PROP_CASE));
2642	}
2643
2644	/*
2645	 * If the zpl version requested is whacky or the file system
2646	 * or pool is version is too "young" to support normalization
2647	 * and the creator tried to set a value for one of the props,
2648	 * error out.
2649	 */
2650	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
2651	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
2652	    (zplver < ZPL_VERSION_NORMALIZATION &&
2653	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
2654	    sense != ZFS_PROP_UNDEFINED)))
2655		return (ENOTSUP);
2656
2657	/*
2658	 * Put the version in the zplprops
2659	 */
2660	VERIFY(nvlist_add_uint64(zplprops,
2661	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
2662
2663	if (norm == ZFS_PROP_UNDEFINED)
2664		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
2665	VERIFY(nvlist_add_uint64(zplprops,
2666	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
2667
2668	/*
2669	 * If we're normalizing, names must always be valid UTF-8 strings.
2670	 */
2671	if (norm)
2672		u8 = 1;
2673	if (u8 == ZFS_PROP_UNDEFINED)
2674		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
2675	VERIFY(nvlist_add_uint64(zplprops,
2676	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
2677
2678	if (sense == ZFS_PROP_UNDEFINED)
2679		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
2680	VERIFY(nvlist_add_uint64(zplprops,
2681	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
2682
2683	if (is_ci)
2684		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
2685
2686	return (0);
2687}
2688
2689static int
2690zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
2691    nvlist_t *zplprops, boolean_t *is_ci)
2692{
2693	boolean_t fuids_ok = B_TRUE;
2694	uint64_t zplver = ZPL_VERSION;
2695	objset_t *os = NULL;
2696	char parentname[MAXNAMELEN];
2697	char *cp;
2698	int error;
2699
2700	(void) strlcpy(parentname, dataset, sizeof (parentname));
2701	cp = strrchr(parentname, '/');
2702	ASSERT(cp != NULL);
2703	cp[0] = '\0';
2704
2705	if (zfs_earlier_version(dataset, SPA_VERSION_USERSPACE))
2706		zplver = ZPL_VERSION_USERSPACE - 1;
2707	if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
2708		zplver = ZPL_VERSION_FUID - 1;
2709		fuids_ok = B_FALSE;
2710	}
2711
2712	/*
2713	 * Open parent object set so we can inherit zplprop values.
2714	 */
2715	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
2716		return (error);
2717
2718	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
2719	    zplprops, is_ci);
2720	dmu_objset_rele(os, FTAG);
2721	return (error);
2722}
2723
2724static int
2725zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
2726    nvlist_t *zplprops, boolean_t *is_ci)
2727{
2728	boolean_t fuids_ok = B_TRUE;
2729	uint64_t zplver = ZPL_VERSION;
2730	int error;
2731
2732	if (spa_vers < SPA_VERSION_FUID) {
2733		zplver = ZPL_VERSION_FUID - 1;
2734		fuids_ok = B_FALSE;
2735	}
2736
2737	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
2738	    zplprops, is_ci);
2739	return (error);
2740}
2741
2742/*
2743 * inputs:
2744 * zc_objset_type	type of objset to create (fs vs zvol)
2745 * zc_name		name of new objset
2746 * zc_value		name of snapshot to clone from (may be empty)
2747 * zc_nvlist_src{_size}	nvlist of properties to apply
2748 *
2749 * outputs: none
2750 */
2751static int
2752zfs_ioc_create(zfs_cmd_t *zc)
2753{
2754	objset_t *clone;
2755	int error = 0;
2756	zfs_creat_t zct;
2757	nvlist_t *nvprops = NULL;
2758	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2759	dmu_objset_type_t type = zc->zc_objset_type;
2760
2761	switch (type) {
2762
2763	case DMU_OST_ZFS:
2764		cbfunc = zfs_create_cb;
2765		break;
2766
2767	case DMU_OST_ZVOL:
2768		cbfunc = zvol_create_cb;
2769		break;
2770
2771	default:
2772		cbfunc = NULL;
2773		break;
2774	}
2775	if (strchr(zc->zc_name, '@') ||
2776	    strchr(zc->zc_name, '%'))
2777		return (EINVAL);
2778
2779	if (zc->zc_nvlist_src != 0 &&
2780	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2781	    zc->zc_iflags, &nvprops)) != 0)
2782		return (error);
2783
2784	zct.zct_zplprops = NULL;
2785	zct.zct_props = nvprops;
2786
2787	if (zc->zc_value[0] != '\0') {
2788		/*
2789		 * We're creating a clone of an existing snapshot.
2790		 */
2791		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2792		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
2793			nvlist_free(nvprops);
2794			return (EINVAL);
2795		}
2796
2797		error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
2798		if (error) {
2799			nvlist_free(nvprops);
2800			return (error);
2801		}
2802
2803		error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
2804		dmu_objset_rele(clone, FTAG);
2805		if (error) {
2806			nvlist_free(nvprops);
2807			return (error);
2808		}
2809	} else {
2810		boolean_t is_insensitive = B_FALSE;
2811
2812		if (cbfunc == NULL) {
2813			nvlist_free(nvprops);
2814			return (EINVAL);
2815		}
2816
2817		if (type == DMU_OST_ZVOL) {
2818			uint64_t volsize, volblocksize;
2819
2820			if (nvprops == NULL ||
2821			    nvlist_lookup_uint64(nvprops,
2822			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
2823			    &volsize) != 0) {
2824				nvlist_free(nvprops);
2825				return (EINVAL);
2826			}
2827
2828			if ((error = nvlist_lookup_uint64(nvprops,
2829			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2830			    &volblocksize)) != 0 && error != ENOENT) {
2831				nvlist_free(nvprops);
2832				return (EINVAL);
2833			}
2834
2835			if (error != 0)
2836				volblocksize = zfs_prop_default_numeric(
2837				    ZFS_PROP_VOLBLOCKSIZE);
2838
2839			if ((error = zvol_check_volblocksize(
2840			    volblocksize)) != 0 ||
2841			    (error = zvol_check_volsize(volsize,
2842			    volblocksize)) != 0) {
2843				nvlist_free(nvprops);
2844				return (error);
2845			}
2846		} else if (type == DMU_OST_ZFS) {
2847			int error;
2848
2849			/*
2850			 * We have to have normalization and
2851			 * case-folding flags correct when we do the
2852			 * file system creation, so go figure them out
2853			 * now.
2854			 */
2855			VERIFY(nvlist_alloc(&zct.zct_zplprops,
2856			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
2857			error = zfs_fill_zplprops(zc->zc_name, nvprops,
2858			    zct.zct_zplprops, &is_insensitive);
2859			if (error != 0) {
2860				nvlist_free(nvprops);
2861				nvlist_free(zct.zct_zplprops);
2862				return (error);
2863			}
2864		}
2865		error = dmu_objset_create(zc->zc_name, type,
2866		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
2867		nvlist_free(zct.zct_zplprops);
2868	}
2869
2870	/*
2871	 * It would be nice to do this atomically.
2872	 */
2873	if (error == 0) {
2874		error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL,
2875		    nvprops, NULL);
2876		if (error != 0)
2877			(void) dmu_objset_destroy(zc->zc_name, B_FALSE);
2878	}
2879	nvlist_free(nvprops);
2880	return (error);
2881}
2882
2883/*
2884 * inputs:
2885 * zc_name	name of filesystem
2886 * zc_value	short name of snapshot
2887 * zc_cookie	recursive flag
2888 * zc_nvlist_src[_size] property list
2889 *
2890 * outputs:
2891 * zc_value	short snapname (i.e. part after the '@')
2892 */
2893static int
2894zfs_ioc_snapshot(zfs_cmd_t *zc)
2895{
2896	nvlist_t *nvprops = NULL;
2897	int error;
2898	boolean_t recursive = zc->zc_cookie;
2899
2900	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
2901		return (EINVAL);
2902
2903	if (zc->zc_nvlist_src != 0 &&
2904	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2905	    zc->zc_iflags, &nvprops)) != 0)
2906		return (error);
2907
2908	error = zfs_check_userprops(zc->zc_name, nvprops);
2909	if (error)
2910		goto out;
2911
2912	if (!nvlist_empty(nvprops) &&
2913	    zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
2914		error = ENOTSUP;
2915		goto out;
2916	}
2917
2918	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value,
2919	    nvprops, recursive);
2920
2921out:
2922	nvlist_free(nvprops);
2923	return (error);
2924}
2925
2926int
2927zfs_unmount_snap(const char *name, void *arg)
2928{
2929	vfs_t *vfsp = NULL;
2930
2931	if (arg) {
2932		char *snapname = arg;
2933		char *fullname = kmem_asprintf("%s@%s", name, snapname);
2934		vfsp = zfs_get_vfs(fullname);
2935		strfree(fullname);
2936	} else if (strchr(name, '@')) {
2937		vfsp = zfs_get_vfs(name);
2938	}
2939
2940	if (vfsp) {
2941#ifdef __NetBSD__
2942		int err;
2943		if ((err = dounmount(vfsp, MNT_FORCE, curlwp)) != 0)
2944			return (err);
2945#else
2946
2947		/*
2948		 * Always force the unmount for snapshots.
2949		 */
2950		int flag = MS_FORCE;
2951		int err;
2952
2953		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
2954			VFS_RELE(vfsp);
2955			return (err);
2956		}
2957		VFS_RELE(vfsp);
2958		if ((err = dounmount(vfsp, flag, kcred)) != 0)
2959			return (err);
2960#endif
2961	}
2962	return (0);
2963}
2964
2965/*
2966 * inputs:
2967 * zc_name		name of filesystem
2968 * zc_value		short name of snapshot
2969 * zc_defer_destroy	mark for deferred destroy
2970 *
2971 * outputs:	none
2972 */
2973static int
2974zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
2975{
2976	int err;
2977
2978	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
2979		return (EINVAL);
2980	err = dmu_objset_find(zc->zc_name,
2981	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
2982	if (err)
2983		return (err);
2984	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value,
2985	    zc->zc_defer_destroy));
2986}
2987
2988/*
2989 * inputs:
2990 * zc_name		name of dataset to destroy
2991 * zc_objset_type	type of objset
2992 * zc_defer_destroy	mark for deferred destroy
2993 *
2994 * outputs:		none
2995 */
2996static int
2997zfs_ioc_destroy(zfs_cmd_t *zc)
2998{
2999	int err;
3000	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
3001		err = zfs_unmount_snap(zc->zc_name, NULL);
3002		if (err)
3003			return (err);
3004	}
3005
3006	err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
3007	if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3008		(void) zvol_remove_minor(zc->zc_name);
3009	return (err);
3010}
3011
3012/*
3013 * inputs:
3014 * zc_name	name of dataset to rollback (to most recent snapshot)
3015 *
3016 * outputs:	none
3017 */
3018static int
3019zfs_ioc_rollback(zfs_cmd_t *zc)
3020{
3021	dsl_dataset_t *ds, *clone;
3022	int error;
3023	zfsvfs_t *zfsvfs;
3024	char *clone_name;
3025
3026	error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
3027	if (error)
3028		return (error);
3029
3030	/* must not be a snapshot */
3031	if (dsl_dataset_is_snapshot(ds)) {
3032		dsl_dataset_rele(ds, FTAG);
3033		return (EINVAL);
3034	}
3035
3036	/* must have a most recent snapshot */
3037	if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
3038		dsl_dataset_rele(ds, FTAG);
3039		return (EINVAL);
3040	}
3041
3042	/*
3043	 * Create clone of most recent snapshot.
3044	 */
3045	clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
3046	error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
3047	if (error)
3048		goto out;
3049
3050	error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
3051	if (error)
3052		goto out;
3053
3054	/*
3055	 * Do clone swap.
3056	 */
3057	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
3058		error = zfs_suspend_fs(zfsvfs);
3059		if (error == 0) {
3060			int resume_err;
3061
3062			if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3063				error = dsl_dataset_clone_swap(clone, ds,
3064				    B_TRUE);
3065				dsl_dataset_disown(ds, FTAG);
3066				ds = NULL;
3067			} else {
3068				error = EBUSY;
3069			}
3070			resume_err = zfs_resume_fs(zfsvfs, zc->zc_name);
3071			error = error ? error : resume_err;
3072		}
3073		VFS_RELE(zfsvfs->z_vfs);
3074	} else {
3075		if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3076			error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
3077			dsl_dataset_disown(ds, FTAG);
3078			ds = NULL;
3079		} else {
3080			error = EBUSY;
3081		}
3082	}
3083
3084	/*
3085	 * Destroy clone (which also closes it).
3086	 */
3087	(void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
3088
3089out:
3090	strfree(clone_name);
3091	if (ds)
3092		dsl_dataset_rele(ds, FTAG);
3093	return (error);
3094}
3095
3096/*
3097 * inputs:
3098 * zc_name	old name of dataset
3099 * zc_value	new name of dataset
3100 * zc_cookie	recursive flag (only valid for snapshots)
3101 *
3102 * outputs:	none
3103 */
3104static int
3105zfs_ioc_rename(zfs_cmd_t *zc)
3106{
3107	boolean_t recursive = zc->zc_cookie & 1;
3108
3109	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3110	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3111	    strchr(zc->zc_value, '%'))
3112		return (EINVAL);
3113
3114	/*
3115	 * Unmount snapshot unless we're doing a recursive rename,
3116	 * in which case the dataset code figures out which snapshots
3117	 * to unmount.
3118	 */
3119	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
3120	    zc->zc_objset_type == DMU_OST_ZFS) {
3121		int err = zfs_unmount_snap(zc->zc_name, NULL);
3122		if (err)
3123			return (err);
3124	}
3125	if (zc->zc_objset_type == DMU_OST_ZVOL)
3126		(void) zvol_remove_minor(zc->zc_name);
3127	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
3128}
3129
3130static int
3131zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3132{
3133	const char *propname = nvpair_name(pair);
3134	boolean_t issnap = (strchr(dsname, '@') != NULL);
3135	zfs_prop_t prop = zfs_name_to_prop(propname);
3136	uint64_t intval;
3137	int err;
3138
3139	if (prop == ZPROP_INVAL) {
3140		if (zfs_prop_user(propname)) {
3141			if (err = zfs_secpolicy_write_perms(dsname,
3142			    ZFS_DELEG_PERM_USERPROP, cr))
3143				return (err);
3144			return (0);
3145		}
3146
3147		if (!issnap && zfs_prop_userquota(propname)) {
3148			const char *perm = NULL;
3149			const char *uq_prefix =
3150			    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3151			const char *gq_prefix =
3152			    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3153
3154			if (strncmp(propname, uq_prefix,
3155			    strlen(uq_prefix)) == 0) {
3156				perm = ZFS_DELEG_PERM_USERQUOTA;
3157			} else if (strncmp(propname, gq_prefix,
3158			    strlen(gq_prefix)) == 0) {
3159				perm = ZFS_DELEG_PERM_GROUPQUOTA;
3160			} else {
3161				/* USERUSED and GROUPUSED are read-only */
3162				return (EINVAL);
3163			}
3164
3165			if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
3166				return (err);
3167			return (0);
3168		}
3169
3170		return (EINVAL);
3171	}
3172
3173	if (issnap)
3174		return (EINVAL);
3175
3176	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3177		/*
3178		 * dsl_prop_get_all_impl() returns properties in this
3179		 * format.
3180		 */
3181		nvlist_t *attrs;
3182		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3183		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3184		    &pair) == 0);
3185	}
3186
3187	/*
3188	 * Check that this value is valid for this pool version
3189	 */
3190	switch (prop) {
3191	case ZFS_PROP_COMPRESSION:
3192		/*
3193		 * If the user specified gzip compression, make sure
3194		 * the SPA supports it. We ignore any errors here since
3195		 * we'll catch them later.
3196		 */
3197		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3198		    nvpair_value_uint64(pair, &intval) == 0) {
3199			if (intval >= ZIO_COMPRESS_GZIP_1 &&
3200			    intval <= ZIO_COMPRESS_GZIP_9 &&
3201			    zfs_earlier_version(dsname,
3202			    SPA_VERSION_GZIP_COMPRESSION)) {
3203				return (ENOTSUP);
3204			}
3205
3206			if (intval == ZIO_COMPRESS_ZLE &&
3207			    zfs_earlier_version(dsname,
3208			    SPA_VERSION_ZLE_COMPRESSION))
3209				return (ENOTSUP);
3210
3211			/*
3212			 * If this is a bootable dataset then
3213			 * verify that the compression algorithm
3214			 * is supported for booting. We must return
3215			 * something other than ENOTSUP since it
3216			 * implies a downrev pool version.
3217			 */
3218			if (zfs_is_bootfs(dsname) &&
3219			    !BOOTFS_COMPRESS_VALID(intval)) {
3220				return (ERANGE);
3221			}
3222		}
3223		break;
3224
3225	case ZFS_PROP_COPIES:
3226		if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3227			return (ENOTSUP);
3228		break;
3229
3230	case ZFS_PROP_DEDUP:
3231		if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3232			return (ENOTSUP);
3233		break;
3234
3235	case ZFS_PROP_SHARESMB:
3236		if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3237			return (ENOTSUP);
3238		break;
3239
3240	case ZFS_PROP_ACLINHERIT:
3241		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3242		    nvpair_value_uint64(pair, &intval) == 0) {
3243			if (intval == ZFS_ACL_PASSTHROUGH_X &&
3244			    zfs_earlier_version(dsname,
3245			    SPA_VERSION_PASSTHROUGH_X))
3246				return (ENOTSUP);
3247		}
3248		break;
3249	}
3250
3251	return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3252}
3253
3254/*
3255 * Removes properties from the given props list that fail permission checks
3256 * needed to clear them and to restore them in case of a receive error. For each
3257 * property, make sure we have both set and inherit permissions.
3258 *
3259 * Returns the first error encountered if any permission checks fail. If the
3260 * caller provides a non-NULL errlist, it also gives the complete list of names
3261 * of all the properties that failed a permission check along with the
3262 * corresponding error numbers. The caller is responsible for freeing the
3263 * returned errlist.
3264 *
3265 * If every property checks out successfully, zero is returned and the list
3266 * pointed at by errlist is NULL.
3267 */
3268static int
3269zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3270{
3271	zfs_cmd_t *zc;
3272	nvpair_t *pair, *next_pair;
3273	nvlist_t *errors;
3274	int err, rv = 0;
3275
3276	if (props == NULL)
3277		return (0);
3278
3279	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3280
3281	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3282	(void) strcpy(zc->zc_name, dataset);
3283	pair = nvlist_next_nvpair(props, NULL);
3284	while (pair != NULL) {
3285		next_pair = nvlist_next_nvpair(props, pair);
3286
3287		(void) strcpy(zc->zc_value, nvpair_name(pair));
3288		if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3289		    (err = zfs_secpolicy_inherit(zc, CRED())) != 0) {
3290			VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3291			VERIFY(nvlist_add_int32(errors,
3292			    zc->zc_value, err) == 0);
3293		}
3294		pair = next_pair;
3295	}
3296	kmem_free(zc, sizeof (zfs_cmd_t));
3297
3298	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3299		nvlist_free(errors);
3300		errors = NULL;
3301	} else {
3302		VERIFY(nvpair_value_int32(pair, &rv) == 0);
3303	}
3304
3305	if (errlist == NULL)
3306		nvlist_free(errors);
3307	else
3308		*errlist = errors;
3309
3310	return (rv);
3311}
3312
3313static boolean_t
3314propval_equals(nvpair_t *p1, nvpair_t *p2)
3315{
3316	if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3317		/* dsl_prop_get_all_impl() format */
3318		nvlist_t *attrs;
3319		VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3320		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3321		    &p1) == 0);
3322	}
3323
3324	if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
3325		nvlist_t *attrs;
3326		VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
3327		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3328		    &p2) == 0);
3329	}
3330
3331	if (nvpair_type(p1) != nvpair_type(p2))
3332		return (B_FALSE);
3333
3334	if (nvpair_type(p1) == DATA_TYPE_STRING) {
3335		char *valstr1, *valstr2;
3336
3337		VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
3338		VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
3339		return (strcmp(valstr1, valstr2) == 0);
3340	} else {
3341		uint64_t intval1, intval2;
3342
3343		VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
3344		VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
3345		return (intval1 == intval2);
3346	}
3347}
3348
3349/*
3350 * Remove properties from props if they are not going to change (as determined
3351 * by comparison with origprops). Remove them from origprops as well, since we
3352 * do not need to clear or restore properties that won't change.
3353 */
3354static void
3355props_reduce(nvlist_t *props, nvlist_t *origprops)
3356{
3357	nvpair_t *pair, *next_pair;
3358
3359	if (origprops == NULL)
3360		return; /* all props need to be received */
3361
3362	pair = nvlist_next_nvpair(props, NULL);
3363	while (pair != NULL) {
3364		const char *propname = nvpair_name(pair);
3365		nvpair_t *match;
3366
3367		next_pair = nvlist_next_nvpair(props, pair);
3368
3369		if ((nvlist_lookup_nvpair(origprops, propname,
3370		    &match) != 0) || !propval_equals(pair, match))
3371			goto next; /* need to set received value */
3372
3373		/* don't clear the existing received value */
3374		(void) nvlist_remove_nvpair(origprops, match);
3375		/* don't bother receiving the property */
3376		(void) nvlist_remove_nvpair(props, pair);
3377next:
3378		pair = next_pair;
3379	}
3380}
3381
3382#ifdef	DEBUG
3383static boolean_t zfs_ioc_recv_inject_err;
3384#endif
3385
3386/*
3387 * inputs:
3388 * zc_name		name of containing filesystem
3389 * zc_nvlist_src{_size}	nvlist of properties to apply
3390 * zc_value		name of snapshot to create
3391 * zc_string		name of clone origin (if DRR_FLAG_CLONE)
3392 * zc_cookie		file descriptor to recv from
3393 * zc_begin_record	the BEGIN record of the stream (not byteswapped)
3394 * zc_guid		force flag
3395 *
3396 * outputs:
3397 * zc_cookie		number of bytes read
3398 * zc_nvlist_dst{_size} error for each unapplied received property
3399 * zc_obj		zprop_errflags_t
3400 */
3401static int
3402zfs_ioc_recv(zfs_cmd_t *zc)
3403{
3404	file_t *fp;
3405	objset_t *os;
3406	dmu_recv_cookie_t drc;
3407	boolean_t force = (boolean_t)zc->zc_guid;
3408	int fd;
3409	int error = 0;
3410	int props_error = 0;
3411	nvlist_t *errors;
3412	offset_t off;
3413	nvlist_t *props = NULL; /* sent properties */
3414	nvlist_t *origprops = NULL; /* existing properties */
3415	objset_t *origin = NULL;
3416	char *tosnap;
3417	char tofs[ZFS_MAXNAMELEN];
3418	boolean_t first_recvd_props = B_FALSE;
3419
3420	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3421	    strchr(zc->zc_value, '@') == NULL ||
3422	    strchr(zc->zc_value, '%'))
3423		return (EINVAL);
3424
3425	(void) strcpy(tofs, zc->zc_value);
3426	tosnap = strchr(tofs, '@');
3427	*tosnap++ = '\0';
3428
3429	if (zc->zc_nvlist_src != 0 &&
3430	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3431	    zc->zc_iflags, &props)) != 0)
3432		return (error);
3433
3434	fd = zc->zc_cookie;
3435	error = fd_getvnode(fd, &fp);
3436	if (error != 0) {
3437		nvlist_free(props);
3438		return (error);
3439	}
3440
3441	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3442
3443	if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
3444		if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) &&
3445		    !dsl_prop_get_hasrecvd(os)) {
3446			first_recvd_props = B_TRUE;
3447		}
3448
3449		/*
3450		 * If new received properties are supplied, they are to
3451		 * completely replace the existing received properties, so stash
3452		 * away the existing ones.
3453		 */
3454		if (dsl_prop_get_received(os, &origprops) == 0) {
3455			nvlist_t *errlist = NULL;
3456			/*
3457			 * Don't bother writing a property if its value won't
3458			 * change (and avoid the unnecessary security checks).
3459			 *
3460			 * The first receive after SPA_VERSION_RECVD_PROPS is a
3461			 * special case where we blow away all local properties
3462			 * regardless.
3463			 */
3464			if (!first_recvd_props)
3465				props_reduce(props, origprops);
3466			if (zfs_check_clearable(tofs, origprops,
3467			    &errlist) != 0)
3468				(void) nvlist_merge(errors, errlist, 0);
3469			nvlist_free(errlist);
3470		}
3471
3472		dmu_objset_rele(os, FTAG);
3473	}
3474
3475	if (zc->zc_string[0]) {
3476		error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
3477		if (error)
3478			goto out;
3479	}
3480
3481	error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds,
3482	    &zc->zc_begin_record, force, origin, &drc);
3483	if (origin)
3484		dmu_objset_rele(origin, FTAG);
3485	if (error)
3486		goto out;
3487
3488	/*
3489	 * Set properties before we receive the stream so that they are applied
3490	 * to the new data. Note that we must call dmu_recv_stream() if
3491	 * dmu_recv_begin() succeeds.
3492	 */
3493	if (props) {
3494		nvlist_t *errlist;
3495
3496		if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) {
3497			if (drc.drc_newfs) {
3498				if (spa_version(os->os_spa) >=
3499				    SPA_VERSION_RECVD_PROPS)
3500					first_recvd_props = B_TRUE;
3501			} else if (origprops != NULL) {
3502				if (clear_received_props(os, tofs, origprops,
3503				    first_recvd_props ? NULL : props) != 0)
3504					zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3505			} else {
3506				zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3507			}
3508			dsl_prop_set_hasrecvd(os);
3509		} else if (!drc.drc_newfs) {
3510			zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3511		}
3512
3513		(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
3514		    props, &errlist);
3515		(void) nvlist_merge(errors, errlist, 0);
3516		nvlist_free(errlist);
3517	}
3518
3519	if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) {
3520		/*
3521		 * Caller made zc->zc_nvlist_dst less than the minimum expected
3522		 * size or supplied an invalid address.
3523		 */
3524		props_error = EINVAL;
3525	}
3526
3527	off = fp->f_offset;
3528	error = dmu_recv_stream(&drc, fp->f_data, &off);
3529
3530	if (error == 0) {
3531		zfsvfs_t *zfsvfs = NULL;
3532
3533		if (getzfsvfs(tofs, &zfsvfs) == 0) {
3534			/* online recv */
3535			int end_err;
3536
3537			error = zfs_suspend_fs(zfsvfs);
3538			/*
3539			 * If the suspend fails, then the recv_end will
3540			 * likely also fail, and clean up after itself.
3541			 */
3542			end_err = dmu_recv_end(&drc);
3543			if (error == 0) {
3544				int resume_err =
3545				    zfs_resume_fs(zfsvfs, tofs);
3546				error = error ? error : resume_err;
3547			}
3548			error = error ? error : end_err;
3549			VFS_RELE(zfsvfs->z_vfs);
3550		} else {
3551			error = dmu_recv_end(&drc);
3552		}
3553	}
3554
3555	zc->zc_cookie = off - fp->f_offset;
3556	if (VOP_SEEK(fp->f_data, fp->f_offset, &off, NULL) == 0)
3557		fp->f_offset = off;
3558
3559#ifdef	DEBUG
3560	if (zfs_ioc_recv_inject_err) {
3561		zfs_ioc_recv_inject_err = B_FALSE;
3562		error = 1;
3563	}
3564#endif
3565	/*
3566	 * On error, restore the original props.
3567	 */
3568	if (error && props) {
3569		if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
3570			if (clear_received_props(os, tofs, props, NULL) != 0) {
3571				/*
3572				 * We failed to clear the received properties.
3573				 * Since we may have left a $recvd value on the
3574				 * system, we can't clear the $hasrecvd flag.
3575				 */
3576				zc->zc_obj |= ZPROP_ERR_NORESTORE;
3577			} else if (first_recvd_props) {
3578				dsl_prop_unset_hasrecvd(os);
3579			}
3580			dmu_objset_rele(os, FTAG);
3581		} else if (!drc.drc_newfs) {
3582			/* We failed to clear the received properties. */
3583			zc->zc_obj |= ZPROP_ERR_NORESTORE;
3584		}
3585
3586		if (origprops == NULL && !drc.drc_newfs) {
3587			/* We failed to stash the original properties. */
3588			zc->zc_obj |= ZPROP_ERR_NORESTORE;
3589		}
3590
3591		/*
3592		 * dsl_props_set() will not convert RECEIVED to LOCAL on or
3593		 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
3594		 * explictly if we're restoring local properties cleared in the
3595		 * first new-style receive.
3596		 */
3597		if (origprops != NULL &&
3598		    zfs_set_prop_nvlist(tofs, (first_recvd_props ?
3599		    ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
3600		    origprops, NULL) != 0) {
3601			/*
3602			 * We stashed the original properties but failed to
3603			 * restore them.
3604			 */
3605			zc->zc_obj |= ZPROP_ERR_NORESTORE;
3606		}
3607	}
3608out:
3609	nvlist_free(props);
3610	nvlist_free(origprops);
3611	nvlist_free(errors);
3612	fd_putfile(fd);
3613
3614	if (error == 0)
3615		error = props_error;
3616
3617	return (error);
3618}
3619
3620/*
3621 * inputs:
3622 * zc_name	name of snapshot to send
3623 * zc_value	short name of incremental fromsnap (may be empty)
3624 * zc_cookie	file descriptor to send stream to
3625 * zc_obj	fromorigin flag (mutually exclusive with zc_value)
3626 *
3627 * outputs: none
3628 */
3629static int
3630zfs_ioc_send(zfs_cmd_t *zc)
3631{
3632	objset_t *fromsnap = NULL;
3633	objset_t *tosnap;
3634	file_t *fp;
3635	int error;
3636	offset_t off;
3637
3638	error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
3639	if (error)
3640		return (error);
3641
3642	if (zc->zc_value[0] != '\0') {
3643		char *buf;
3644		char *cp;
3645
3646		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3647		(void) strncpy(buf, zc->zc_name, MAXPATHLEN);
3648		cp = strchr(buf, '@');
3649		if (cp)
3650			*(cp+1) = 0;
3651		(void) strncat(buf, zc->zc_value, MAXPATHLEN);
3652		error = dmu_objset_hold(buf, FTAG, &fromsnap);
3653		kmem_free(buf, MAXPATHLEN);
3654		if (error) {
3655			dmu_objset_rele(tosnap, FTAG);
3656			return (error);
3657		}
3658	}
3659
3660	error = fd_getvnode(zc->zc_cookie, &fp);
3661	if (error != 0) {
3662		dmu_objset_rele(tosnap, FTAG);
3663		if (fromsnap)
3664			dmu_objset_rele(fromsnap, FTAG);
3665		return (error);
3666	}
3667
3668	off = fp->f_offset;
3669	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_data, &off);
3670
3671	if (VOP_SEEK(fp->f_data, fp->f_offset, &off, NULL) == 0)
3672		fp->f_offset = off;
3673	fd_putfile(zc->zc_cookie);
3674	if (fromsnap)
3675		dmu_objset_rele(fromsnap, FTAG);
3676	dmu_objset_rele(tosnap, FTAG);
3677	return (error);
3678}
3679
3680static int
3681zfs_ioc_inject_fault(zfs_cmd_t *zc)
3682{
3683	int id, error;
3684
3685	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
3686	    &zc->zc_inject_record);
3687
3688	if (error == 0)
3689		zc->zc_guid = (uint64_t)id;
3690
3691	return (error);
3692}
3693
3694static int
3695zfs_ioc_clear_fault(zfs_cmd_t *zc)
3696{
3697	return (zio_clear_fault((int)zc->zc_guid));
3698}
3699
3700static int
3701zfs_ioc_inject_list_next(zfs_cmd_t *zc)
3702{
3703	int id = (int)zc->zc_guid;
3704	int error;
3705
3706	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
3707	    &zc->zc_inject_record);
3708
3709	zc->zc_guid = id;
3710
3711	return (error);
3712}
3713
3714static int
3715zfs_ioc_error_log(zfs_cmd_t *zc)
3716{
3717	spa_t *spa;
3718	int error;
3719	size_t count = (size_t)zc->zc_nvlist_dst_size;
3720
3721	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
3722		return (error);
3723
3724	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
3725	    &count);
3726	if (error == 0)
3727		zc->zc_nvlist_dst_size = count;
3728	else
3729		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
3730
3731	spa_close(spa, FTAG);
3732
3733	return (error);
3734}
3735
3736static int
3737zfs_ioc_clear(zfs_cmd_t *zc)
3738{
3739	spa_t *spa;
3740	vdev_t *vd;
3741	int error;
3742
3743	/*
3744	 * On zpool clear we also fix up missing slogs
3745	 */
3746	mutex_enter(&spa_namespace_lock);
3747	spa = spa_lookup(zc->zc_name);
3748	if (spa == NULL) {
3749		mutex_exit(&spa_namespace_lock);
3750		return (EIO);
3751	}
3752	if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
3753		/* we need to let spa_open/spa_load clear the chains */
3754		spa_set_log_state(spa, SPA_LOG_CLEAR);
3755	}
3756	spa->spa_last_open_failed = 0;
3757	mutex_exit(&spa_namespace_lock);
3758
3759	if (zc->zc_cookie & ZPOOL_NO_REWIND) {
3760		error = spa_open(zc->zc_name, &spa, FTAG);
3761	} else {
3762		nvlist_t *policy;
3763		nvlist_t *config = NULL;
3764
3765		if (zc->zc_nvlist_src == 0)
3766			return (EINVAL);
3767
3768		if ((error = get_nvlist(zc->zc_nvlist_src,
3769		    zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
3770			error = spa_open_rewind(zc->zc_name, &spa, FTAG,
3771			    policy, &config);
3772			if (config != NULL) {
3773				(void) put_nvlist(zc, config);
3774				nvlist_free(config);
3775			}
3776			nvlist_free(policy);
3777		}
3778	}
3779
3780	if (error)
3781		return (error);
3782
3783	spa_vdev_state_enter(spa, SCL_NONE);
3784
3785	if (zc->zc_guid == 0) {
3786		vd = NULL;
3787	} else {
3788		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
3789		if (vd == NULL) {
3790			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
3791			spa_close(spa, FTAG);
3792			return (ENODEV);
3793		}
3794	}
3795
3796	vdev_clear(spa, vd);
3797
3798	(void) spa_vdev_state_exit(spa, NULL, 0);
3799
3800	/*
3801	 * Resume any suspended I/Os.
3802	 */
3803	if (zio_resume(spa) != 0)
3804		error = EIO;
3805
3806	spa_close(spa, FTAG);
3807
3808	return (error);
3809}
3810
3811/*
3812 * inputs:
3813 * zc_name	name of filesystem
3814 * zc_value	name of origin snapshot
3815 *
3816 * outputs:
3817 * zc_string	name of conflicting snapshot, if there is one
3818 */
3819static int
3820zfs_ioc_promote(zfs_cmd_t *zc)
3821{
3822	char *cp;
3823
3824	/*
3825	 * We don't need to unmount *all* the origin fs's snapshots, but
3826	 * it's easier.
3827	 */
3828	cp = strchr(zc->zc_value, '@');
3829	if (cp)
3830		*cp = '\0';
3831	(void) dmu_objset_find(zc->zc_value,
3832	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
3833	return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
3834}
3835
3836/*
3837 * Retrieve a single {user|group}{used|quota}@... property.
3838 *
3839 * inputs:
3840 * zc_name	name of filesystem
3841 * zc_objset_type zfs_userquota_prop_t
3842 * zc_value	domain name (eg. "S-1-234-567-89")
3843 * zc_guid	RID/UID/GID
3844 *
3845 * outputs:
3846 * zc_cookie	property value
3847 */
3848static int
3849zfs_ioc_userspace_one(zfs_cmd_t *zc)
3850{
3851	zfsvfs_t *zfsvfs;
3852	int error;
3853
3854	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
3855		return (EINVAL);
3856
3857	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs);
3858	if (error)
3859		return (error);
3860
3861	error = zfs_userspace_one(zfsvfs,
3862	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
3863	zfsvfs_rele(zfsvfs, FTAG);
3864
3865	return (error);
3866}
3867
3868/*
3869 * inputs:
3870 * zc_name		name of filesystem
3871 * zc_cookie		zap cursor
3872 * zc_objset_type	zfs_userquota_prop_t
3873 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
3874 *
3875 * outputs:
3876 * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
3877 * zc_cookie	zap cursor
3878 */
3879static int
3880zfs_ioc_userspace_many(zfs_cmd_t *zc)
3881{
3882	zfsvfs_t *zfsvfs;
3883	int error;
3884
3885	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs);
3886	if (error)
3887		return (error);
3888
3889	int bufsize = zc->zc_nvlist_dst_size;
3890	void *buf = kmem_alloc(bufsize, KM_SLEEP);
3891
3892	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
3893	    buf, &zc->zc_nvlist_dst_size);
3894
3895	if (error == 0) {
3896		error = xcopyout(buf,
3897		    (void *)(uintptr_t)zc->zc_nvlist_dst,
3898		    zc->zc_nvlist_dst_size);
3899	}
3900	kmem_free(buf, bufsize);
3901	zfsvfs_rele(zfsvfs, FTAG);
3902
3903	return (error);
3904}
3905
3906/*
3907 * inputs:
3908 * zc_name		name of filesystem
3909 *
3910 * outputs:
3911 * none
3912 */
3913static int
3914zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
3915{
3916	objset_t *os;
3917	int error = 0;
3918	zfsvfs_t *zfsvfs;
3919
3920	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
3921		if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
3922			/*
3923			 * If userused is not enabled, it may be because the
3924			 * objset needs to be closed & reopened (to grow the
3925			 * objset_phys_t).  Suspend/resume the fs will do that.
3926			 */
3927			error = zfs_suspend_fs(zfsvfs);
3928			if (error == 0)
3929				error = zfs_resume_fs(zfsvfs, zc->zc_name);
3930		}
3931		if (error == 0)
3932			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
3933		VFS_RELE(zfsvfs->z_vfs);
3934	} else {
3935		/* XXX kind of reading contents without owning */
3936		error = dmu_objset_hold(zc->zc_name, FTAG, &os);
3937		if (error)
3938			return (error);
3939
3940		error = dmu_objset_userspace_upgrade(os);
3941		dmu_objset_rele(os, FTAG);
3942	}
3943
3944	return (error);
3945}
3946
3947/*
3948 * We don't want to have a hard dependency
3949 * against some special symbols in sharefs
3950 * nfs, and smbsrv.  Determine them if needed when
3951 * the first file system is shared.
3952 * Neither sharefs, nfs or smbsrv are unloadable modules.
3953 */
3954#ifdef __NetBSD__
3955
3956static int
3957zfs_ioc_share(zfs_cmd_t *zc)
3958{
3959
3960	return EOPNOTSUPP;
3961}
3962
3963#else	/* __NetBSD__ */
3964
3965int (*znfsexport_fs)(void *arg);
3966int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
3967int (*zsmbexport_fs)(void *arg, boolean_t add_share);
3968
3969int zfs_nfsshare_inited;
3970int zfs_smbshare_inited;
3971
3972ddi_modhandle_t nfs_mod;
3973ddi_modhandle_t sharefs_mod;
3974ddi_modhandle_t smbsrv_mod;
3975kmutex_t zfs_share_lock;
3976
3977static int
3978zfs_init_sharefs()
3979{
3980	int error;
3981
3982	ASSERT(MUTEX_HELD(&zfs_share_lock));
3983	/* Both NFS and SMB shares also require sharetab support. */
3984	if (sharefs_mod == NULL && ((sharefs_mod =
3985	    ddi_modopen("fs/sharefs",
3986	    KRTLD_MODE_FIRST, &error)) == NULL)) {
3987		return (ENOSYS);
3988	}
3989	if (zshare_fs == NULL && ((zshare_fs =
3990	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
3991	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
3992		return (ENOSYS);
3993	}
3994	return (0);
3995}
3996
3997static int
3998zfs_ioc_share(zfs_cmd_t *zc)
3999{
4000	int error;
4001	int opcode;
4002
4003	switch (zc->zc_share.z_sharetype) {
4004	case ZFS_SHARE_NFS:
4005	case ZFS_UNSHARE_NFS:
4006		if (zfs_nfsshare_inited == 0) {
4007			mutex_enter(&zfs_share_lock);
4008			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4009			    KRTLD_MODE_FIRST, &error)) == NULL)) {
4010				mutex_exit(&zfs_share_lock);
4011				return (ENOSYS);
4012			}
4013			if (znfsexport_fs == NULL &&
4014			    ((znfsexport_fs = (int (*)(void *))
4015			    ddi_modsym(nfs_mod,
4016			    "nfs_export", &error)) == NULL)) {
4017				mutex_exit(&zfs_share_lock);
4018				return (ENOSYS);
4019			}
4020			error = zfs_init_sharefs();
4021			if (error) {
4022				mutex_exit(&zfs_share_lock);
4023				return (ENOSYS);
4024			}
4025			zfs_nfsshare_inited = 1;
4026			mutex_exit(&zfs_share_lock);
4027		}
4028		break;
4029	case ZFS_SHARE_SMB:
4030	case ZFS_UNSHARE_SMB:
4031		if (zfs_smbshare_inited == 0) {
4032			mutex_enter(&zfs_share_lock);
4033			if (smbsrv_mod == NULL && ((smbsrv_mod =
4034			    ddi_modopen("drv/smbsrv",
4035			    KRTLD_MODE_FIRST, &error)) == NULL)) {
4036				mutex_exit(&zfs_share_lock);
4037				return (ENOSYS);
4038			}
4039			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4040			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4041			    "smb_server_share", &error)) == NULL)) {
4042				mutex_exit(&zfs_share_lock);
4043				return (ENOSYS);
4044			}
4045			error = zfs_init_sharefs();
4046			if (error) {
4047				mutex_exit(&zfs_share_lock);
4048				return (ENOSYS);
4049			}
4050			zfs_smbshare_inited = 1;
4051			mutex_exit(&zfs_share_lock);
4052		}
4053		break;
4054	default:
4055		return (EINVAL);
4056	}
4057
4058	switch (zc->zc_share.z_sharetype) {
4059	case ZFS_SHARE_NFS:
4060	case ZFS_UNSHARE_NFS:
4061		if (error =
4062		    znfsexport_fs((void *)
4063		    (uintptr_t)zc->zc_share.z_exportdata))
4064			return (error);
4065		break;
4066	case ZFS_SHARE_SMB:
4067	case ZFS_UNSHARE_SMB:
4068		if (error = zsmbexport_fs((void *)
4069		    (uintptr_t)zc->zc_share.z_exportdata,
4070		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4071		    B_TRUE: B_FALSE)) {
4072			return (error);
4073		}
4074		break;
4075	}
4076
4077	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4078	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4079	    SHAREFS_ADD : SHAREFS_REMOVE;
4080
4081	/*
4082	 * Add or remove share from sharetab
4083	 */
4084	error = zshare_fs(opcode,
4085	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
4086	    zc->zc_share.z_sharemax);
4087
4088	return (error);
4089
4090}
4091
4092ace_t full_access[] = {
4093	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4094};
4095
4096/*
4097 * Remove all ACL files in shares dir
4098 */
4099static int
4100zfs_smb_acl_purge(znode_t *dzp)
4101{
4102	zap_cursor_t	zc;
4103	zap_attribute_t	zap;
4104	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4105	int error;
4106
4107	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
4108	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4109	    zap_cursor_advance(&zc)) {
4110		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4111		    NULL, 0)) != 0)
4112			break;
4113	}
4114	zap_cursor_fini(&zc);
4115	return (error);
4116}
4117
4118static int
4119zfs_ioc_smb_acl(zfs_cmd_t *zc)
4120{
4121	vnode_t *vp;
4122	znode_t *dzp;
4123	vnode_t *resourcevp = NULL;
4124	znode_t *sharedir;
4125	zfsvfs_t *zfsvfs;
4126	nvlist_t *nvlist;
4127	char *src, *target;
4128	vattr_t vattr;
4129	vsecattr_t vsec;
4130	int error = 0;
4131
4132	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4133		    NULL, &vp)) != 0)
4134		return (error);
4135
4136	/* Now make sure mntpnt and dataset are ZFS */
4137#ifndef __NetBSD__
4138	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4139	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4140	    zc->zc_name) != 0)) {
4141		VN_RELE(vp);
4142		return (EINVAL);
4143	}
4144#endif
4145	dzp = VTOZ(vp);
4146	zfsvfs = dzp->z_zfsvfs;
4147	ZFS_ENTER(zfsvfs);
4148
4149	/*
4150	 * Create share dir if its missing.
4151	 */
4152	mutex_enter(&zfsvfs->z_lock);
4153	if (zfsvfs->z_shares_dir == 0) {
4154		dmu_tx_t *tx;
4155
4156		tx = dmu_tx_create(zfsvfs->z_os);
4157		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
4158		    ZFS_SHARES_DIR);
4159		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
4160		error = dmu_tx_assign(tx, TXG_WAIT);
4161		if (error) {
4162			dmu_tx_abort(tx);
4163		} else {
4164			error = zfs_create_share_dir(zfsvfs, tx);
4165			dmu_tx_commit(tx);
4166		}
4167		if (error) {
4168			mutex_exit(&zfsvfs->z_lock);
4169			VN_RELE(vp);
4170			ZFS_EXIT(zfsvfs);
4171			return (error);
4172		}
4173	}
4174	mutex_exit(&zfsvfs->z_lock);
4175
4176	ASSERT(zfsvfs->z_shares_dir);
4177	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
4178		VN_RELE(vp);
4179		ZFS_EXIT(zfsvfs);
4180		return (error);
4181	}
4182
4183	switch (zc->zc_cookie) {
4184	case ZFS_SMB_ACL_ADD:
4185		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
4186		vattr.va_type = VREG;
4187		vattr.va_mode = S_IFREG|0777;
4188		vattr.va_uid = 0;
4189		vattr.va_gid = 0;
4190
4191		vsec.vsa_mask = VSA_ACE;
4192		vsec.vsa_aclentp = &full_access;
4193		vsec.vsa_aclentsz = sizeof (full_access);
4194		vsec.vsa_aclcnt = 1;
4195
4196		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
4197		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
4198		if (resourcevp)
4199			VN_RELE(resourcevp);
4200		break;
4201
4202	case ZFS_SMB_ACL_REMOVE:
4203		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
4204		    NULL, 0);
4205		break;
4206
4207	case ZFS_SMB_ACL_RENAME:
4208		if ((error = get_nvlist(zc->zc_nvlist_src,
4209		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
4210			VN_RELE(vp);
4211			ZFS_EXIT(zfsvfs);
4212			return (error);
4213		}
4214		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
4215		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
4216		    &target)) {
4217			VN_RELE(vp);
4218			VN_RELE(ZTOV(sharedir));
4219			ZFS_EXIT(zfsvfs);
4220			nvlist_free(nvlist);
4221			return (error);
4222		}
4223		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
4224		    kcred, NULL, 0);
4225		nvlist_free(nvlist);
4226		break;
4227
4228	case ZFS_SMB_ACL_PURGE:
4229		error = zfs_smb_acl_purge(sharedir);
4230		break;
4231
4232	default:
4233		error = EINVAL;
4234		break;
4235	}
4236
4237	VN_RELE(vp);
4238	VN_RELE(ZTOV(sharedir));
4239
4240	ZFS_EXIT(zfsvfs);
4241
4242	return (error);
4243}
4244#endif	/* __NetBSD__ */
4245
4246/*
4247 * inputs:
4248 * zc_name	name of filesystem
4249 * zc_value	short name of snap
4250 * zc_string	user-supplied tag for this reference
4251 * zc_cookie	recursive flag
4252 * zc_temphold	set if hold is temporary
4253 *
4254 * outputs:		none
4255 */
4256static int
4257zfs_ioc_hold(zfs_cmd_t *zc)
4258{
4259	boolean_t recursive = zc->zc_cookie;
4260
4261	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4262		return (EINVAL);
4263
4264	return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
4265	    zc->zc_string, recursive, zc->zc_temphold));
4266}
4267
4268/*
4269 * inputs:
4270 * zc_name	name of dataset from which we're releasing a user reference
4271 * zc_value	short name of snap
4272 * zc_string	user-supplied tag for this reference
4273 * zc_cookie	recursive flag
4274 *
4275 * outputs:		none
4276 */
4277static int
4278zfs_ioc_release(zfs_cmd_t *zc)
4279{
4280	boolean_t recursive = zc->zc_cookie;
4281
4282	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4283		return (EINVAL);
4284
4285	return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
4286	    zc->zc_string, recursive));
4287}
4288
4289/*
4290 * inputs:
4291 * zc_name		name of filesystem
4292 *
4293 * outputs:
4294 * zc_nvlist_src{_size}	nvlist of snapshot holds
4295 */
4296static int
4297zfs_ioc_get_holds(zfs_cmd_t *zc)
4298{
4299	nvlist_t *nvp;
4300	int error;
4301
4302	if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
4303		error = put_nvlist(zc, nvp);
4304		nvlist_free(nvp);
4305	}
4306
4307	return (error);
4308}
4309
4310/*
4311 * pool create, destroy, and export don't log the history as part of
4312 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
4313 * do the logging of those commands.
4314 */
4315static zfs_ioc_vec_t zfs_ioc_vec[] = {
4316	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4317	    B_FALSE },
4318	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
4319	    B_FALSE },
4320	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4321	    B_FALSE },
4322	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4323	    B_FALSE },
4324	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE,
4325	    B_FALSE },
4326	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4327	    B_FALSE },
4328	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
4329	    B_FALSE },
4330	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4331	    B_TRUE },
4332	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
4333	    B_FALSE },
4334	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE,
4335	    B_TRUE },
4336	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4337	    B_FALSE },
4338	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4339	    B_TRUE },
4340	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4341	    B_TRUE },
4342	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
4343	    B_FALSE },
4344	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4345	    B_TRUE },
4346	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4347	    B_TRUE },
4348	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
4349	    B_TRUE },
4350	{ zfs_ioc_vdev_setfru,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
4351	    B_TRUE },
4352	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4353	    B_TRUE },
4354	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4355	    B_FALSE },
4356	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4357	    B_TRUE },
4358	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4359	    B_TRUE },
4360	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
4361	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
4362	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
4363	    B_TRUE},
4364	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
4365	    B_TRUE },
4366	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE, B_TRUE },
4367	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
4368	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE },
4369	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE,
4370	    B_FALSE },
4371	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4372	    B_FALSE },
4373	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4374	    B_FALSE },
4375	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
4376	    B_FALSE },
4377	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
4378	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
4379	    B_TRUE },
4380	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, DATASET_NAME,
4381	    B_TRUE, B_TRUE },
4382	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
4383	    B_TRUE },
4384	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4385	    B_FALSE },
4386	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
4387	    B_TRUE },
4388	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
4389	    B_TRUE },
4390	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4391	    B_FALSE },
4392	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
4393	    B_TRUE },
4394	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4395	    B_FALSE },
4396	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE,
4397	    B_FALSE },
4398	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
4399	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
4400	    B_TRUE },
4401	/*{ zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
4402	  B_FALSE },*/
4403	{ zfs_ioc_userspace_one, zfs_secpolicy_userspace_one,
4404	    DATASET_NAME, B_FALSE, B_FALSE },
4405	{ zfs_ioc_userspace_many, zfs_secpolicy_userspace_many,
4406	    DATASET_NAME, B_FALSE, B_FALSE },
4407	{ zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
4408	    DATASET_NAME, B_FALSE, B_TRUE },
4409	{ zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, B_TRUE },
4410	{ zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
4411	    B_TRUE },
4412	{ zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4413	    B_TRUE },
4414	{ zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4415	    B_FALSE },
4416	{ zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4417	    B_TRUE }
4418};
4419
4420int
4421pool_status_check(const char *name, zfs_ioc_namecheck_t type)
4422{
4423	spa_t *spa;
4424	int error;
4425
4426	ASSERT(type == POOL_NAME || type == DATASET_NAME);
4427
4428	error = spa_open(name, &spa, FTAG);
4429	if (error == 0) {
4430		if (spa_suspended(spa))
4431			error = EAGAIN;
4432		spa_close(spa, FTAG);
4433	}
4434	return (error);
4435}
4436
4437static int
4438zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
4439{
4440	zfs_cmd_t *zc;
4441	uint_t vec;
4442	int error, rc;
4443
4444	printf("zfsdev_ioctl called \n");
4445
4446	if (getminor(dev) != 0)
4447		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
4448	printf("zfsdev_ioctl -> zvol_ioctl\n");
4449	vec = cmd - ZFS_IOC;
4450	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
4451
4452	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
4453		return (EINVAL);
4454
4455	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
4456
4457	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
4458	if ((error == 0) && !(flag & FKIOCTL))
4459		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
4460
4461	/*
4462	 * Ensure that all pool/dataset names are valid before we pass down to
4463	 * the lower layers.
4464	 */
4465	if (error == 0) {
4466		dprintf("zfsdev_ioctl, zc->zc_name %s\n", zc->zc_name);
4467		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4468		zc->zc_iflags = flag & FKIOCTL;
4469		switch (zfs_ioc_vec[vec].zvec_namecheck) {
4470		case POOL_NAME:
4471			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
4472				error = EINVAL;
4473			if (zfs_ioc_vec[vec].zvec_pool_check)
4474				error = pool_status_check(zc->zc_name,
4475				    zfs_ioc_vec[vec].zvec_namecheck);
4476			break;
4477
4478		case DATASET_NAME:
4479			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
4480				error = EINVAL;
4481			if (zfs_ioc_vec[vec].zvec_pool_check)
4482				error = pool_status_check(zc->zc_name,
4483				    zfs_ioc_vec[vec].zvec_namecheck);
4484			break;
4485
4486		case NO_NAME:
4487			break;
4488		}
4489	}
4490
4491	printf("zfsdev_ioctl -> calling zfs_ioc_vec zvec_func on %d\n", vec);
4492	if (error == 0)
4493		error = zfs_ioc_vec[vec].zvec_func(zc);
4494
4495	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
4496	if (error == 0) {
4497		error = rc;
4498		if (zfs_ioc_vec[vec].zvec_his_log)
4499			zfs_log_history(zc);
4500	}
4501
4502	kmem_free(zc, sizeof (zfs_cmd_t));
4503	printf("zfsdev_ioctl %d\n", error);
4504	return (error);
4505}
4506
4507#ifdef __NetBSD__
4508
4509#include <sys/module.h>
4510#include <uvm/uvm_extern.h>
4511
4512MODULE(MODULE_CLASS_VFS, zfs, "solaris");
4513
4514static int
4515nb_zvol_copen(dev_t dev, int flag, int mode, lwp_t *l)
4516{
4517
4518	return zvol_open(&dev, flag, OTYPCHR, kauth_cred_get());
4519}
4520
4521static int
4522nb_zvol_cclose(dev_t dev, int flag, int mode, lwp_t *l)
4523{
4524
4525	return zvol_close(dev, flag, OTYPCHR, kauth_cred_get());
4526}
4527
4528static int
4529nb_zvol_bopen(dev_t dev, int flag, int mode, lwp_t *l)
4530{
4531
4532	return zvol_open(&dev, flag, OTYPBLK, kauth_cred_get());
4533}
4534
4535static int
4536nb_zvol_bclose(dev_t dev, int flag, int mode, lwp_t *l)
4537{
4538
4539	return zvol_close(dev, flag, OTYPBLK, kauth_cred_get());
4540}
4541
4542static int
4543nb_zvol_read(dev_t dev, struct uio *uio, int flag)
4544{
4545
4546	return zvol_read(dev, uio, kauth_cred_get());
4547}
4548
4549static int
4550nb_zvol_write(dev_t dev, struct uio *uio, int flag)
4551{
4552
4553	return zvol_write(dev, uio, kauth_cred_get());
4554}
4555
4556static int
4557nb_zfsdev_ioctl(dev_t dev, u_long cmd, void *argp, int flag, lwp_t *l)
4558{
4559	int rval;
4560
4561	return zfsdev_ioctl(dev, cmd, (intptr_t)argp, flag, kauth_cred_get(),
4562	    &rval);
4563}
4564
4565const struct bdevsw zfs_bdevsw = {
4566	.d_open = nb_zvol_bopen,
4567	.d_close = nb_zvol_bclose,
4568	.d_strategy = zvol_strategy,
4569	.d_ioctl = nb_zfsdev_ioctl,
4570	.d_dump = nodump,
4571	.d_psize = nosize,
4572	.d_flag = D_DISK | D_MPSAFE
4573};
4574
4575const struct cdevsw zfs_cdevsw = {
4576	.d_open = nb_zvol_copen,
4577	.d_close = nb_zvol_cclose,
4578	.d_read = nb_zvol_read,
4579	.d_write = nb_zvol_write,
4580	.d_ioctl = nb_zfsdev_ioctl,
4581	.d_stop = nostop,
4582	.d_tty = notty,
4583	.d_poll = nopoll,
4584	.d_mmap = nommap,
4585	.d_kqfilter = nokqfilter,
4586	.d_flag = D_DISK | D_MPSAFE
4587};
4588
4589uint_t zfs_fsyncer_key;
4590extern uint_t rrw_tsd_key;
4591
4592/* ZFS must be used on machines with at least 512Mb. */
4593#define ZFS_MIN_MEGS 512
4594
4595static int
4596zfs_modcmd(modcmd_t cmd, void *arg)
4597{
4598	int error;
4599	int active, inactive;
4600	uint64_t availrmem;
4601
4602	switch (cmd) {
4603	case MODULE_CMD_INIT:
4604		if (!rootvnode)
4605			return EAGAIN;
4606
4607		printf("WARNING: ZFS on NetBSD is under development\n");
4608		availrmem = (uint64_t)physmem * PAGE_SIZE / 1048576;
4609		if (availrmem < ZFS_MIN_MEGS * 80 / 100) {
4610			printf("ERROR: at least %dMB of memory required to"
4611			    "use ZFS\n", ZFS_MIN_MEGS);
4612			return ENOMEM;
4613		}
4614		error = lwp_specific_key_create(&zfs_fsyncer_key, NULL);
4615		if (error != 0) {
4616			return error;
4617		}
4618		error = lwp_specific_key_create(&rrw_tsd_key, NULL);
4619		if (error != 0) {
4620			lwp_specific_key_delete(zfs_fsyncer_key);
4621			return error;
4622		}
4623		spa_init(FREAD | FWRITE);
4624		zvol_init();
4625		zfs_vfsinit(16, MOUNT_ZFS); /* I need to use well defined args. */
4626		error = devsw_attach("zfs", &zfs_bdevsw, &zfs_bmajor,
4627		    &zfs_cdevsw, &zfs_cmajor);
4628		if (error != 0) {
4629			zvol_fini();
4630			zfs_vfsfini();
4631			spa_fini();
4632			lwp_specific_key_delete(zfs_fsyncer_key);
4633			lwp_specific_key_delete(rrw_tsd_key);
4634		}
4635		return error;
4636
4637	case MODULE_CMD_FINI:
4638		if (spa_busy() || zfs_busy() || zvol_busy() ||
4639		    zio_injection_enabled)
4640			return EBUSY;
4641		error = devsw_detach(&zfs_bdevsw, &zfs_cdevsw);
4642		zvol_fini();
4643		zfs_vfsfini();
4644		spa_fini();
4645		lwp_specific_key_delete(zfs_fsyncer_key);
4646		lwp_specific_key_delete(rrw_tsd_key);
4647		return error;
4648
4649	case MODULE_CMD_AUTOUNLOAD:
4650		/*
4651		 * We don't want to be autounloaded because unlike
4652		 * other subsystems, we read our own configuration
4653		 * from disk and provide things that might be used
4654		 * later (zvols).
4655		 */
4656		return EBUSY;
4657
4658	default:
4659		return ENOTTY;
4660	}
4661}
4662
4663#else	/* __NetBSD__ */
4664
4665int
4666_fini(void)
4667{
4668	int error;
4669
4670	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
4671		return (EBUSY);
4672
4673	if ((error = mod_remove(&modlinkage)) != 0)
4674		return (error);
4675
4676	zvol_fini();
4677	zfs_fini();
4678	spa_fini();
4679	if (zfs_nfsshare_inited)
4680		(void) ddi_modclose(nfs_mod);
4681	if (zfs_smbshare_inited)
4682		(void) ddi_modclose(smbsrv_mod);
4683	if (zfs_nfsshare_inited || zfs_smbshare_inited)
4684		(void) ddi_modclose(sharefs_mod);
4685
4686	tsd_destroy(&zfs_fsyncer_key);
4687	ldi_ident_release(zfs_li);
4688	zfs_li = NULL;
4689	mutex_destroy(&zfs_share_lock);
4690
4691	return (error);
4692}
4693
4694static int
4695zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4696{
4697	if (cmd != DDI_ATTACH)
4698		return (DDI_FAILURE);
4699
4700	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
4701	    DDI_PSEUDO, 0) == DDI_FAILURE)
4702		return (DDI_FAILURE);
4703
4704	zfs_dip = dip;
4705
4706	ddi_report_dev(dip);
4707
4708	return (DDI_SUCCESS);
4709}
4710
4711static int
4712zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4713{
4714	if (spa_busy() || zfs_busy() || zvol_busy())
4715		return (DDI_FAILURE);
4716
4717	if (cmd != DDI_DETACH)
4718		return (DDI_FAILURE);
4719
4720	zfs_dip = NULL;
4721
4722	ddi_prop_remove_all(dip);
4723	ddi_remove_minor_node(dip, NULL);
4724
4725	return (DDI_SUCCESS);
4726}
4727
4728/*ARGSUSED*/
4729static int
4730zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4731{
4732	switch (infocmd) {
4733	case DDI_INFO_DEVT2DEVINFO:
4734		*result = zfs_dip;
4735		return (DDI_SUCCESS);
4736
4737	case DDI_INFO_DEVT2INSTANCE:
4738		*result = (void *)0;
4739		return (DDI_SUCCESS);
4740	}
4741
4742	return (DDI_FAILURE);
4743}
4744
4745/*
4746 * OK, so this is a little weird.
4747 *
4748 * /dev/zfs is the control node, i.e. minor 0.
4749 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
4750 *
4751 * /dev/zfs has basically nothing to do except serve up ioctls,
4752 * so most of the standard driver entry points are in zvol.c.
4753 */
4754static struct cb_ops zfs_cb_ops = {
4755	zvol_open,	/* open */
4756	zvol_close,	/* close */
4757	zvol_strategy,	/* strategy */
4758	nodev,		/* print */
4759	zvol_dump,	/* dump */
4760	zvol_read,	/* read */
4761	zvol_write,	/* write */
4762	zfsdev_ioctl,	/* ioctl */
4763	nodev,		/* devmap */
4764	nodev,		/* mmap */
4765	nodev,		/* segmap */
4766	nochpoll,	/* poll */
4767	ddi_prop_op,	/* prop_op */
4768	NULL,		/* streamtab */
4769	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
4770	CB_REV,		/* version */
4771	nodev,		/* async read */
4772	nodev,		/* async write */
4773};
4774
4775static struct dev_ops zfs_dev_ops = {
4776	DEVO_REV,	/* version */
4777	0,		/* refcnt */
4778	zfs_info,	/* info */
4779	nulldev,	/* identify */
4780	nulldev,	/* probe */
4781	zfs_attach,	/* attach */
4782	zfs_detach,	/* detach */
4783	nodev,		/* reset */
4784	&zfs_cb_ops,	/* driver operations */
4785	NULL,		/* no bus operations */
4786	NULL,		/* power */
4787	ddi_quiesce_not_needed,	/* quiesce */
4788};
4789
4790static struct modldrv zfs_modldrv = {
4791	&mod_driverops,
4792	"ZFS storage pool",
4793	&zfs_dev_ops
4794};
4795
4796static struct modlinkage modlinkage = {
4797	MODREV_1,
4798	(void *)&zfs_modlfs,
4799	(void *)&zfs_modldrv,
4800	NULL
4801};
4802
4803
4804uint_t zfs_fsyncer_key;
4805extern uint_t rrw_tsd_key;
4806
4807int
4808_init(void)
4809{
4810	int error;
4811
4812	spa_init(FREAD | FWRITE);
4813	zfs_init();
4814	zvol_init();
4815
4816	if ((error = mod_install(&modlinkage)) != 0) {
4817		zvol_fini();
4818		zfs_fini();
4819		spa_fini();
4820		return (error);
4821	}
4822
4823	tsd_create(&zfs_fsyncer_key, NULL);
4824	tsd_create(&rrw_tsd_key, NULL);
4825
4826	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
4827	ASSERT(error == 0);
4828	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
4829
4830	return (0);
4831}
4832
4833int
4834_fini(void)
4835{
4836	int error;
4837
4838	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
4839		return (EBUSY);
4840
4841	if ((error = mod_remove(&modlinkage)) != 0)
4842		return (error);
4843
4844	zvol_fini();
4845	zfs_fini();
4846	spa_fini();
4847	if (zfs_nfsshare_inited)
4848		(void) ddi_modclose(nfs_mod);
4849	if (zfs_smbshare_inited)
4850		(void) ddi_modclose(smbsrv_mod);
4851	if (zfs_nfsshare_inited || zfs_smbshare_inited)
4852		(void) ddi_modclose(sharefs_mod);
4853
4854	tsd_destroy(&zfs_fsyncer_key);
4855	ldi_ident_release(zfs_li);
4856	zfs_li = NULL;
4857	mutex_destroy(&zfs_share_lock);
4858
4859	return (error);
4860}
4861
4862int
4863_info(struct modinfo *modinfop)
4864{
4865	return (mod_info(&modlinkage, modinfop));
4866}
4867#endif	/* __NetBSD__ */
4868