xref: /titanic_44/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/errno.h>
31 #include <sys/uio.h>
32 #include <sys/buf.h>
33 #include <sys/modctl.h>
34 #include <sys/open.h>
35 #include <sys/file.h>
36 #include <sys/kmem.h>
37 #include <sys/conf.h>
38 #include <sys/cmn_err.h>
39 #include <sys/stat.h>
40 #include <sys/zfs_ioctl.h>
41 #include <sys/zap.h>
42 #include <sys/spa.h>
43 #include <sys/vdev.h>
44 #include <sys/dmu.h>
45 #include <sys/dsl_dir.h>
46 #include <sys/dsl_dataset.h>
47 #include <sys/dsl_prop.h>
48 #include <sys/ddi.h>
49 #include <sys/sunddi.h>
50 #include <sys/sunldi.h>
51 #include <sys/policy.h>
52 #include <sys/zone.h>
53 #include <sys/nvpair.h>
54 #include <sys/pathname.h>
55 #include <sys/mount.h>
56 #include <sys/sdt.h>
57 #include <sys/fs/zfs.h>
58 #include <sys/zfs_ctldir.h>
59 #include <sys/zvol.h>
60 
61 #include "zfs_namecheck.h"
62 #include "zfs_prop.h"
63 
64 extern struct modlfs zfs_modlfs;
65 
66 extern void zfs_init(void);
67 extern void zfs_fini(void);
68 
69 ldi_ident_t zfs_li = NULL;
70 dev_info_t *zfs_dip;
71 
72 typedef int zfs_ioc_func_t(zfs_cmd_t *);
73 typedef int zfs_secpolicy_func_t(const char *, cred_t *);
74 
75 typedef struct zfs_ioc_vec {
76 	zfs_ioc_func_t		*zvec_func;
77 	zfs_secpolicy_func_t	*zvec_secpolicy;
78 	enum {
79 		no_name,
80 		pool_name,
81 		dataset_name
82 	}			zvec_namecheck;
83 } zfs_ioc_vec_t;
84 
85 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
86 void
87 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
88 {
89 	const char *newfile;
90 	char buf[256];
91 	va_list adx;
92 
93 	/*
94 	 * Get rid of annoying "../common/" prefix to filename.
95 	 */
96 	newfile = strrchr(file, '/');
97 	if (newfile != NULL) {
98 		newfile = newfile + 1; /* Get rid of leading / */
99 	} else {
100 		newfile = file;
101 	}
102 
103 	va_start(adx, fmt);
104 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
105 	va_end(adx);
106 
107 	/*
108 	 * To get this data, use the zfs-dprintf probe as so:
109 	 * dtrace -q -n 'zfs-dprintf \
110 	 *	/stringof(arg0) == "dbuf.c"/ \
111 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
112 	 * arg0 = file name
113 	 * arg1 = function name
114 	 * arg2 = line number
115 	 * arg3 = message
116 	 */
117 	DTRACE_PROBE4(zfs__dprintf,
118 	    char *, newfile, char *, func, int, line, char *, buf);
119 }
120 
121 /*
122  * Policy for top-level read operations (list pools).  Requires no privileges,
123  * and can be used in the local zone, as there is no associated dataset.
124  */
125 /* ARGSUSED */
126 static int
127 zfs_secpolicy_none(const char *unused1, cred_t *cr)
128 {
129 	return (0);
130 }
131 
132 /*
133  * Policy for dataset read operations (list children, get statistics).  Requires
134  * no privileges, but must be visible in the local zone.
135  */
136 /* ARGSUSED */
137 static int
138 zfs_secpolicy_read(const char *dataset, cred_t *cr)
139 {
140 	if (INGLOBALZONE(curproc) ||
141 	    zone_dataset_visible(dataset, NULL))
142 		return (0);
143 
144 	return (ENOENT);
145 }
146 
147 static int
148 zfs_dozonecheck(const char *dataset, cred_t *cr)
149 {
150 	uint64_t zoned;
151 	int writable = 1;
152 
153 	/*
154 	 * The dataset must be visible by this zone -- check this first
155 	 * so they don't see EPERM on something they shouldn't know about.
156 	 */
157 	if (!INGLOBALZONE(curproc) &&
158 	    !zone_dataset_visible(dataset, &writable))
159 		return (ENOENT);
160 
161 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
162 		return (ENOENT);
163 
164 	if (INGLOBALZONE(curproc)) {
165 		/*
166 		 * If the fs is zoned, only root can access it from the
167 		 * global zone.
168 		 */
169 		if (secpolicy_zfs(cr) && zoned)
170 			return (EPERM);
171 	} else {
172 		/*
173 		 * If we are in a local zone, the 'zoned' property must be set.
174 		 */
175 		if (!zoned)
176 			return (EPERM);
177 
178 		/* must be writable by this zone */
179 		if (!writable)
180 			return (EPERM);
181 	}
182 	return (0);
183 }
184 
185 /*
186  * Policy for dataset write operations (create children, set properties, etc).
187  * Requires SYS_MOUNT privilege, and must be writable in the local zone.
188  */
189 int
190 zfs_secpolicy_write(const char *dataset, cred_t *cr)
191 {
192 	int error;
193 
194 	if (error = zfs_dozonecheck(dataset, cr))
195 		return (error);
196 
197 	return (secpolicy_zfs(cr));
198 }
199 
200 /*
201  * Policy for operations that want to write a dataset's parent:
202  * create, destroy, snapshot, clone, restore.
203  */
204 static int
205 zfs_secpolicy_parent(const char *dataset, cred_t *cr)
206 {
207 	char parentname[MAXNAMELEN];
208 	char *cp;
209 
210 	/*
211 	 * Remove the @bla or /bla from the end of the name to get the parent.
212 	 */
213 	(void) strncpy(parentname, dataset, sizeof (parentname));
214 	cp = strrchr(parentname, '@');
215 	if (cp != NULL) {
216 		cp[0] = '\0';
217 	} else {
218 		cp = strrchr(parentname, '/');
219 		if (cp == NULL)
220 			return (ENOENT);
221 		cp[0] = '\0';
222 
223 	}
224 
225 	return (zfs_secpolicy_write(parentname, cr));
226 }
227 
228 /*
229  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
230  * SYS_CONFIG privilege, which is not available in a local zone.
231  */
232 /* ARGSUSED */
233 static int
234 zfs_secpolicy_config(const char *unused, cred_t *cr)
235 {
236 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
237 		return (EPERM);
238 
239 	return (0);
240 }
241 
242 /*
243  * Policy for fault injection.  Requires all privileges.
244  */
245 /* ARGSUSED */
246 static int
247 zfs_secpolicy_inject(const char *unused, cred_t *cr)
248 {
249 	return (secpolicy_zinject(cr));
250 }
251 
252 /*
253  * Returns the nvlist as specified by the user in the zfs_cmd_t.
254  */
255 static int
256 get_nvlist(zfs_cmd_t *zc, nvlist_t **nvp)
257 {
258 	char *packed;
259 	size_t size;
260 	int error;
261 	nvlist_t *config = NULL;
262 
263 	/*
264 	 * Read in and unpack the user-supplied nvlist.
265 	 */
266 	if ((size = zc->zc_nvlist_src_size) == 0)
267 		return (EINVAL);
268 
269 	packed = kmem_alloc(size, KM_SLEEP);
270 
271 	if ((error = xcopyin((void *)(uintptr_t)zc->zc_nvlist_src, packed,
272 	    size)) != 0) {
273 		kmem_free(packed, size);
274 		return (error);
275 	}
276 
277 	if ((error = nvlist_unpack(packed, size, &config, 0)) != 0) {
278 		kmem_free(packed, size);
279 		return (error);
280 	}
281 
282 	kmem_free(packed, size);
283 
284 	*nvp = config;
285 	return (0);
286 }
287 
288 static int
289 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
290 {
291 	char *packed = NULL;
292 	size_t size;
293 	int error;
294 
295 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
296 
297 	if (size > zc->zc_nvlist_dst_size) {
298 		error = ENOMEM;
299 	} else {
300 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
301 		    KM_SLEEP) == 0);
302 		error = xcopyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
303 		    size);
304 		kmem_free(packed, size);
305 	}
306 
307 	zc->zc_nvlist_dst_size = size;
308 	return (error);
309 }
310 
311 static int
312 zfs_ioc_pool_create(zfs_cmd_t *zc)
313 {
314 	int error;
315 	nvlist_t *config;
316 
317 	if ((error = get_nvlist(zc, &config)) != 0)
318 		return (error);
319 
320 	error = spa_create(zc->zc_name, config, zc->zc_value[0] == '\0' ?
321 	    NULL : zc->zc_value);
322 
323 	nvlist_free(config);
324 
325 	return (error);
326 }
327 
328 static int
329 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
330 {
331 	return (spa_destroy(zc->zc_name));
332 }
333 
334 static int
335 zfs_ioc_pool_import(zfs_cmd_t *zc)
336 {
337 	int error;
338 	nvlist_t *config;
339 	uint64_t guid;
340 
341 	if ((error = get_nvlist(zc, &config)) != 0)
342 		return (error);
343 
344 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
345 	    guid != zc->zc_guid)
346 		error = EINVAL;
347 	else
348 		error = spa_import(zc->zc_name, config,
349 		    zc->zc_value[0] == '\0' ? NULL : zc->zc_value);
350 
351 	nvlist_free(config);
352 
353 	return (error);
354 }
355 
356 static int
357 zfs_ioc_pool_export(zfs_cmd_t *zc)
358 {
359 	return (spa_export(zc->zc_name, NULL));
360 }
361 
362 static int
363 zfs_ioc_pool_configs(zfs_cmd_t *zc)
364 {
365 	nvlist_t *configs;
366 	int error;
367 
368 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
369 		return (EEXIST);
370 
371 	error = put_nvlist(zc, configs);
372 
373 	nvlist_free(configs);
374 
375 	return (error);
376 }
377 
378 static int
379 zfs_ioc_pool_stats(zfs_cmd_t *zc)
380 {
381 	nvlist_t *config;
382 	int error;
383 	int ret = 0;
384 
385 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
386 	    sizeof (zc->zc_value));
387 
388 	if (config != NULL) {
389 		ret = put_nvlist(zc, config);
390 		nvlist_free(config);
391 
392 		/*
393 		 * The config may be present even if 'error' is non-zero.
394 		 * In this case we return success, and preserve the real errno
395 		 * in 'zc_cookie'.
396 		 */
397 		zc->zc_cookie = error;
398 	} else {
399 		ret = error;
400 	}
401 
402 	return (ret);
403 }
404 
405 /*
406  * Try to import the given pool, returning pool stats as appropriate so that
407  * user land knows which devices are available and overall pool health.
408  */
409 static int
410 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
411 {
412 	nvlist_t *tryconfig, *config;
413 	int error;
414 
415 	if ((error = get_nvlist(zc, &tryconfig)) != 0)
416 		return (error);
417 
418 	config = spa_tryimport(tryconfig);
419 
420 	nvlist_free(tryconfig);
421 
422 	if (config == NULL)
423 		return (EINVAL);
424 
425 	error = put_nvlist(zc, config);
426 	nvlist_free(config);
427 
428 	return (error);
429 }
430 
431 static int
432 zfs_ioc_pool_scrub(zfs_cmd_t *zc)
433 {
434 	spa_t *spa;
435 	int error;
436 
437 	error = spa_open(zc->zc_name, &spa, FTAG);
438 	if (error == 0) {
439 		error = spa_scrub(spa, zc->zc_cookie, B_FALSE);
440 		spa_close(spa, FTAG);
441 	}
442 	return (error);
443 }
444 
445 static int
446 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
447 {
448 	spa_t *spa;
449 	int error;
450 
451 	error = spa_open(zc->zc_name, &spa, FTAG);
452 	if (error == 0) {
453 		spa_freeze(spa);
454 		spa_close(spa, FTAG);
455 	}
456 	return (error);
457 }
458 
459 static int
460 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
461 {
462 	spa_t *spa;
463 	int error;
464 
465 	error = spa_open(zc->zc_name, &spa, FTAG);
466 	if (error == 0) {
467 		spa_upgrade(spa);
468 		spa_close(spa, FTAG);
469 	}
470 	return (error);
471 }
472 
473 static int
474 zfs_ioc_vdev_add(zfs_cmd_t *zc)
475 {
476 	spa_t *spa;
477 	int error;
478 	nvlist_t *config;
479 
480 	error = spa_open(zc->zc_name, &spa, FTAG);
481 	if (error != 0)
482 		return (error);
483 
484 	if ((error = get_nvlist(zc, &config)) == 0) {
485 		error = spa_vdev_add(spa, config);
486 		nvlist_free(config);
487 	}
488 
489 	spa_close(spa, FTAG);
490 	return (error);
491 }
492 
493 static int
494 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
495 {
496 	spa_t *spa;
497 	int error;
498 
499 	error = spa_open(zc->zc_name, &spa, FTAG);
500 	if (error != 0)
501 		return (error);
502 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
503 	spa_close(spa, FTAG);
504 	return (error);
505 }
506 
507 static int
508 zfs_ioc_vdev_online(zfs_cmd_t *zc)
509 {
510 	spa_t *spa;
511 	int error;
512 
513 	error = spa_open(zc->zc_name, &spa, FTAG);
514 	if (error != 0)
515 		return (error);
516 	error = vdev_online(spa, zc->zc_guid);
517 	spa_close(spa, FTAG);
518 	return (error);
519 }
520 
521 static int
522 zfs_ioc_vdev_offline(zfs_cmd_t *zc)
523 {
524 	spa_t *spa;
525 	int istmp = zc->zc_cookie;
526 	int error;
527 
528 	error = spa_open(zc->zc_name, &spa, FTAG);
529 	if (error != 0)
530 		return (error);
531 	error = vdev_offline(spa, zc->zc_guid, istmp);
532 	spa_close(spa, FTAG);
533 	return (error);
534 }
535 
536 static int
537 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
538 {
539 	spa_t *spa;
540 	int replacing = zc->zc_cookie;
541 	nvlist_t *config;
542 	int error;
543 
544 	error = spa_open(zc->zc_name, &spa, FTAG);
545 	if (error != 0)
546 		return (error);
547 
548 	if ((error = get_nvlist(zc, &config)) == 0) {
549 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
550 		nvlist_free(config);
551 	}
552 
553 	spa_close(spa, FTAG);
554 	return (error);
555 }
556 
557 static int
558 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
559 {
560 	spa_t *spa;
561 	int error;
562 
563 	error = spa_open(zc->zc_name, &spa, FTAG);
564 	if (error != 0)
565 		return (error);
566 
567 	error = spa_vdev_detach(spa, zc->zc_guid, B_FALSE);
568 
569 	spa_close(spa, FTAG);
570 	return (error);
571 }
572 
573 static int
574 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
575 {
576 	spa_t *spa;
577 	char *path = zc->zc_value;
578 	uint64_t guid = zc->zc_guid;
579 	int error;
580 
581 	error = spa_open(zc->zc_name, &spa, FTAG);
582 	if (error != 0)
583 		return (error);
584 
585 	error = spa_vdev_setpath(spa, guid, path);
586 
587 	spa_close(spa, FTAG);
588 	return (error);
589 }
590 
591 static int
592 zfs_ioc_objset_stats(zfs_cmd_t *zc)
593 {
594 	objset_t *os = NULL;
595 	int error;
596 	nvlist_t *nv;
597 
598 retry:
599 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
600 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
601 	if (error != 0) {
602 		/*
603 		 * This is ugly: dmu_objset_open() can return EBUSY if
604 		 * the objset is held exclusively. Fortunately this hold is
605 		 * only for a short while, so we retry here.
606 		 * This avoids user code having to handle EBUSY,
607 		 * for example for a "zfs list".
608 		 */
609 		if (error == EBUSY) {
610 			delay(1);
611 			goto retry;
612 		}
613 		return (error);
614 	}
615 
616 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
617 
618 	if (zc->zc_nvlist_dst != 0 &&
619 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
620 		dmu_objset_stats(os, nv);
621 		if (dmu_objset_type(os) == DMU_OST_ZVOL)
622 			VERIFY(zvol_get_stats(os, nv) == 0);
623 		error = put_nvlist(zc, nv);
624 		nvlist_free(nv);
625 	}
626 
627 	spa_altroot(dmu_objset_spa(os), zc->zc_value, sizeof (zc->zc_value));
628 
629 	dmu_objset_close(os);
630 	return (error);
631 }
632 
633 static int
634 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
635 {
636 	objset_t *os;
637 	int error;
638 	char *p;
639 
640 retry:
641 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
642 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
643 	if (error != 0) {
644 		/*
645 		 * This is ugly: dmu_objset_open() can return EBUSY if
646 		 * the objset is held exclusively. Fortunately this hold is
647 		 * only for a short while, so we retry here.
648 		 * This avoids user code having to handle EBUSY,
649 		 * for example for a "zfs list".
650 		 */
651 		if (error == EBUSY) {
652 			delay(1);
653 			goto retry;
654 		}
655 		if (error == ENOENT)
656 			error = ESRCH;
657 		return (error);
658 	}
659 
660 	p = strrchr(zc->zc_name, '/');
661 	if (p == NULL || p[1] != '\0')
662 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
663 	p = zc->zc_name + strlen(zc->zc_name);
664 
665 	do {
666 		error = dmu_dir_list_next(os,
667 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
668 		    NULL, &zc->zc_cookie);
669 		if (error == ENOENT)
670 			error = ESRCH;
671 	} while (error == 0 && !INGLOBALZONE(curproc) &&
672 	    !zone_dataset_visible(zc->zc_name, NULL));
673 
674 	/*
675 	 * If it's a hidden dataset (ie. with a '$' in its name), don't
676 	 * try to get stats for it.  Userland will skip over it.
677 	 */
678 	if (error == 0 && strchr(zc->zc_name, '$') == NULL)
679 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
680 
681 	dmu_objset_close(os);
682 	return (error);
683 }
684 
685 static int
686 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
687 {
688 	objset_t *os;
689 	int error;
690 
691 retry:
692 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
693 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
694 	if (error != 0) {
695 		/*
696 		 * This is ugly: dmu_objset_open() can return EBUSY if
697 		 * the objset is held exclusively. Fortunately this hold is
698 		 * only for a short while, so we retry here.
699 		 * This avoids user code having to handle EBUSY,
700 		 * for example for a "zfs list".
701 		 */
702 		if (error == EBUSY) {
703 			delay(1);
704 			goto retry;
705 		}
706 		if (error == ENOENT)
707 			error = ESRCH;
708 		return (error);
709 	}
710 
711 	/*
712 	 * A dataset name of maximum length cannot have any snapshots,
713 	 * so exit immediately.
714 	 */
715 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
716 		dmu_objset_close(os);
717 		return (ESRCH);
718 	}
719 
720 	error = dmu_snapshot_list_next(os,
721 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
722 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie);
723 	if (error == ENOENT)
724 		error = ESRCH;
725 
726 	if (error == 0)
727 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
728 
729 	dmu_objset_close(os);
730 	return (error);
731 }
732 
733 static int
734 zfs_set_prop_nvlist(const char *name, dev_t dev, cred_t *cr, nvlist_t *nvl)
735 {
736 	nvpair_t *elem;
737 	int error;
738 	const char *propname;
739 	zfs_prop_t prop;
740 	uint64_t intval;
741 	char *strval;
742 
743 	elem = NULL;
744 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
745 		propname = nvpair_name(elem);
746 
747 		if ((prop = zfs_name_to_prop(propname)) ==
748 		    ZFS_PROP_INVAL) {
749 			/*
750 			 * If this is a user-defined property, it must be a
751 			 * string, and there is no further validation to do.
752 			 */
753 			if (!zfs_prop_user(propname) ||
754 			    nvpair_type(elem) != DATA_TYPE_STRING)
755 				return (EINVAL);
756 
757 			VERIFY(nvpair_value_string(elem, &strval) == 0);
758 			error = dsl_prop_set(name, propname, 1,
759 			    strlen(strval) + 1, strval);
760 			if (error == 0)
761 				continue;
762 			else
763 				break;
764 		}
765 
766 		/*
767 		 * Check permissions for special properties.
768 		 */
769 		switch (prop) {
770 		case ZFS_PROP_ZONED:
771 			/*
772 			 * Disallow setting of 'zoned' from within a local zone.
773 			 */
774 			if (!INGLOBALZONE(curproc))
775 				return (EPERM);
776 			break;
777 
778 		case ZFS_PROP_QUOTA:
779 			if (error = zfs_dozonecheck(name, cr))
780 				return (error);
781 
782 			if (!INGLOBALZONE(curproc)) {
783 				uint64_t zoned;
784 				char setpoint[MAXNAMELEN];
785 				int dslen;
786 				/*
787 				 * Unprivileged users are allowed to modify the
788 				 * quota on things *under* (ie. contained by)
789 				 * the thing they own.
790 				 */
791 				if (dsl_prop_get_integer(name, "zoned", &zoned,
792 				    setpoint))
793 					return (EPERM);
794 				if (!zoned) /* this shouldn't happen */
795 					return (EPERM);
796 				dslen = strlen(name);
797 				if (dslen <= strlen(setpoint))
798 					return (EPERM);
799 			}
800 		}
801 
802 		switch (prop) {
803 		case ZFS_PROP_QUOTA:
804 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
805 			    (error = dsl_dir_set_quota(name,
806 			    intval)) != 0)
807 				return (error);
808 			break;
809 
810 		case ZFS_PROP_RESERVATION:
811 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
812 			    (error = dsl_dir_set_reservation(name,
813 			    intval)) != 0)
814 				return (error);
815 			break;
816 
817 		case ZFS_PROP_VOLSIZE:
818 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
819 			    (error = zvol_set_volsize(name, dev,
820 			    intval)) != 0)
821 				return (error);
822 			break;
823 
824 		case ZFS_PROP_VOLBLOCKSIZE:
825 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
826 			    (error = zvol_set_volblocksize(name,
827 			    intval)) != 0)
828 				return (error);
829 			break;
830 
831 		default:
832 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
833 				if (zfs_prop_get_type(prop) !=
834 				    prop_type_string)
835 					return (EINVAL);
836 				VERIFY(nvpair_value_string(elem, &strval) == 0);
837 				if ((error = dsl_prop_set(name,
838 				    nvpair_name(elem), 1, strlen(strval) + 1,
839 				    strval)) != 0)
840 					return (error);
841 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
842 				const char *unused;
843 
844 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
845 
846 				switch (zfs_prop_get_type(prop)) {
847 				case prop_type_number:
848 					break;
849 				case prop_type_boolean:
850 					if (intval > 1)
851 						return (EINVAL);
852 					break;
853 				case prop_type_string:
854 					return (EINVAL);
855 				case prop_type_index:
856 					if (zfs_prop_index_to_string(prop,
857 					    intval, &unused) != 0)
858 						return (EINVAL);
859 					break;
860 				default:
861 					cmn_err(CE_PANIC, "unknown property "
862 					    "type");
863 					break;
864 				}
865 
866 				if ((error = dsl_prop_set(name, propname,
867 				    8, 1, &intval)) != 0)
868 					return (error);
869 			} else {
870 				return (EINVAL);
871 			}
872 			break;
873 		}
874 	}
875 
876 	return (0);
877 }
878 
879 static int
880 zfs_ioc_set_prop(zfs_cmd_t *zc)
881 {
882 	nvlist_t *nvl;
883 	int error;
884 	zfs_prop_t prop;
885 
886 	/*
887 	 * If zc_value is set, then this is an attempt to inherit a value.
888 	 * Otherwise, zc_nvlist refers to a list of properties to set.
889 	 */
890 	if (zc->zc_value[0] != '\0') {
891 		if (!zfs_prop_user(zc->zc_value) &&
892 		    ((prop = zfs_name_to_prop(zc->zc_value)) ==
893 		    ZFS_PROP_INVAL ||
894 		    !zfs_prop_inheritable(prop)))
895 			return (EINVAL);
896 
897 		return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
898 	}
899 
900 	if ((error = get_nvlist(zc, &nvl)) != 0)
901 		return (error);
902 
903 	error = zfs_set_prop_nvlist(zc->zc_name, zc->zc_dev,
904 	    (cred_t *)(uintptr_t)zc->zc_cred, nvl);
905 	nvlist_free(nvl);
906 	return (error);
907 }
908 
909 static int
910 zfs_ioc_create_minor(zfs_cmd_t *zc)
911 {
912 	return (zvol_create_minor(zc->zc_name, zc->zc_dev));
913 }
914 
915 static int
916 zfs_ioc_remove_minor(zfs_cmd_t *zc)
917 {
918 	return (zvol_remove_minor(zc->zc_name));
919 }
920 
921 /*
922  * Search the vfs list for a specified resource.  Returns a pointer to it
923  * or NULL if no suitable entry is found. The caller of this routine
924  * is responsible for releasing the returned vfs pointer.
925  */
926 static vfs_t *
927 zfs_get_vfs(const char *resource)
928 {
929 	struct vfs *vfsp;
930 	struct vfs *vfs_found = NULL;
931 
932 	vfs_list_read_lock();
933 	vfsp = rootvfs;
934 	do {
935 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
936 			VFS_HOLD(vfsp);
937 			vfs_found = vfsp;
938 			break;
939 		}
940 		vfsp = vfsp->vfs_next;
941 	} while (vfsp != rootvfs);
942 	vfs_list_unlock();
943 	return (vfs_found);
944 }
945 
946 static void
947 zfs_create_cb(objset_t *os, void *arg, dmu_tx_t *tx)
948 {
949 	zfs_create_data_t *zc = arg;
950 	zfs_create_fs(os, (cred_t *)(uintptr_t)zc->zc_cred, tx);
951 }
952 
953 static int
954 zfs_ioc_create(zfs_cmd_t *zc)
955 {
956 	objset_t *clone;
957 	int error = 0;
958 	zfs_create_data_t cbdata = { 0 };
959 	void (*cbfunc)(objset_t *os, void *arg, dmu_tx_t *tx);
960 	dmu_objset_type_t type = zc->zc_objset_type;
961 
962 	switch (type) {
963 
964 	case DMU_OST_ZFS:
965 		cbfunc = zfs_create_cb;
966 		break;
967 
968 	case DMU_OST_ZVOL:
969 		cbfunc = zvol_create_cb;
970 		break;
971 
972 	default:
973 		cbfunc = NULL;
974 	}
975 	if (strchr(zc->zc_name, '@'))
976 		return (EINVAL);
977 
978 	if (zc->zc_nvlist_src != NULL &&
979 	    (error = get_nvlist(zc, &cbdata.zc_props)) != 0)
980 		return (error);
981 
982 	cbdata.zc_cred = (cred_t *)(uintptr_t)zc->zc_cred;
983 	cbdata.zc_dev = (dev_t)zc->zc_dev;
984 
985 	if (zc->zc_value[0] != '\0') {
986 		/*
987 		 * We're creating a clone of an existing snapshot.
988 		 */
989 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
990 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
991 			nvlist_free(cbdata.zc_props);
992 			return (EINVAL);
993 		}
994 
995 		error = dmu_objset_open(zc->zc_value, type,
996 		    DS_MODE_STANDARD | DS_MODE_READONLY, &clone);
997 		if (error) {
998 			nvlist_free(cbdata.zc_props);
999 			return (error);
1000 		}
1001 		error = dmu_objset_create(zc->zc_name, type, clone, NULL, NULL);
1002 		dmu_objset_close(clone);
1003 	} else {
1004 		if (cbfunc == NULL) {
1005 			nvlist_free(cbdata.zc_props);
1006 			return (EINVAL);
1007 		}
1008 
1009 		if (type == DMU_OST_ZVOL) {
1010 			uint64_t volsize, volblocksize;
1011 
1012 			if (cbdata.zc_props == NULL ||
1013 			    nvlist_lookup_uint64(cbdata.zc_props,
1014 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1015 			    &volsize) != 0) {
1016 				nvlist_free(cbdata.zc_props);
1017 				return (EINVAL);
1018 			}
1019 
1020 			if ((error = nvlist_lookup_uint64(cbdata.zc_props,
1021 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1022 			    &volblocksize)) != 0 && error != ENOENT) {
1023 				nvlist_free(cbdata.zc_props);
1024 				return (EINVAL);
1025 			}
1026 
1027 			if (error != 0)
1028 				volblocksize = zfs_prop_default_numeric(
1029 				    ZFS_PROP_VOLBLOCKSIZE);
1030 
1031 			if ((error = zvol_check_volblocksize(
1032 			    volblocksize)) != 0 ||
1033 			    (error = zvol_check_volsize(volsize,
1034 			    volblocksize)) != 0) {
1035 				nvlist_free(cbdata.zc_props);
1036 				return (error);
1037 			}
1038 		}
1039 
1040 		error = dmu_objset_create(zc->zc_name, type, NULL, cbfunc,
1041 		    &cbdata);
1042 	}
1043 
1044 	/*
1045 	 * It would be nice to do this atomically.
1046 	 */
1047 	if (error == 0) {
1048 		if ((error = zfs_set_prop_nvlist(zc->zc_name,
1049 		    zc->zc_dev, (cred_t *)(uintptr_t)zc->zc_cred,
1050 		    cbdata.zc_props)) != 0)
1051 			(void) dmu_objset_destroy(zc->zc_name);
1052 	}
1053 
1054 	nvlist_free(cbdata.zc_props);
1055 	return (error);
1056 }
1057 
1058 static int
1059 zfs_ioc_snapshot(zfs_cmd_t *zc)
1060 {
1061 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
1062 		return (EINVAL);
1063 	return (dmu_objset_snapshot(zc->zc_name,
1064 	    zc->zc_value, zc->zc_cookie));
1065 }
1066 
1067 static int
1068 zfs_unmount_snap(char *name, void *arg)
1069 {
1070 	char *snapname = arg;
1071 	char *cp;
1072 	vfs_t *vfsp = NULL;
1073 
1074 	/*
1075 	 * Snapshots (which are under .zfs control) must be unmounted
1076 	 * before they can be destroyed.
1077 	 */
1078 
1079 	if (snapname) {
1080 		(void) strcat(name, "@");
1081 		(void) strcat(name, snapname);
1082 		vfsp = zfs_get_vfs(name);
1083 		cp = strchr(name, '@');
1084 		*cp = '\0';
1085 	} else if (strchr(name, '@')) {
1086 		vfsp = zfs_get_vfs(name);
1087 	}
1088 
1089 	if (vfsp) {
1090 		/*
1091 		 * Always force the unmount for snapshots.
1092 		 */
1093 		int flag = MS_FORCE;
1094 		int err;
1095 
1096 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
1097 			VFS_RELE(vfsp);
1098 			return (err);
1099 		}
1100 		VFS_RELE(vfsp);
1101 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
1102 			return (err);
1103 	}
1104 	return (0);
1105 }
1106 
1107 static int
1108 zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
1109 {
1110 	int err;
1111 
1112 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
1113 		return (EINVAL);
1114 	err = dmu_objset_find(zc->zc_name,
1115 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
1116 	if (err)
1117 		return (err);
1118 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
1119 }
1120 
1121 static int
1122 zfs_ioc_destroy(zfs_cmd_t *zc)
1123 {
1124 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
1125 		int err = zfs_unmount_snap(zc->zc_name, NULL);
1126 		if (err)
1127 			return (err);
1128 	}
1129 
1130 	return (dmu_objset_destroy(zc->zc_name));
1131 }
1132 
1133 static int
1134 zfs_ioc_rollback(zfs_cmd_t *zc)
1135 {
1136 	return (dmu_objset_rollback(zc->zc_name));
1137 }
1138 
1139 static int
1140 zfs_ioc_rename(zfs_cmd_t *zc)
1141 {
1142 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
1143 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0)
1144 		return (EINVAL);
1145 
1146 	if (strchr(zc->zc_name, '@') != NULL &&
1147 	    zc->zc_objset_type == DMU_OST_ZFS) {
1148 		int err = zfs_unmount_snap(zc->zc_name, NULL);
1149 		if (err)
1150 			return (err);
1151 	}
1152 
1153 	return (dmu_objset_rename(zc->zc_name, zc->zc_value));
1154 }
1155 
1156 static int
1157 zfs_ioc_recvbackup(zfs_cmd_t *zc)
1158 {
1159 	file_t *fp;
1160 	int error, fd;
1161 	offset_t new_off;
1162 
1163 	fd = zc->zc_cookie;
1164 	fp = getf(fd);
1165 	if (fp == NULL)
1166 		return (EBADF);
1167 	error = dmu_recvbackup(zc->zc_value, &zc->zc_begin_record,
1168 	    &zc->zc_cookie, (boolean_t)zc->zc_guid, fp->f_vnode,
1169 	    fp->f_offset);
1170 
1171 	new_off = fp->f_offset + zc->zc_cookie;
1172 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &new_off) == 0)
1173 		fp->f_offset = new_off;
1174 
1175 	releasef(fd);
1176 	return (error);
1177 }
1178 
1179 static int
1180 zfs_ioc_sendbackup(zfs_cmd_t *zc)
1181 {
1182 	objset_t *fromsnap = NULL;
1183 	objset_t *tosnap;
1184 	file_t *fp;
1185 	int error;
1186 
1187 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1188 	    DS_MODE_STANDARD | DS_MODE_READONLY, &tosnap);
1189 	if (error)
1190 		return (error);
1191 
1192 	if (zc->zc_value[0] != '\0') {
1193 		char buf[MAXPATHLEN];
1194 		char *cp;
1195 
1196 		(void) strncpy(buf, zc->zc_name, sizeof (buf));
1197 		cp = strchr(buf, '@');
1198 		if (cp)
1199 			*(cp+1) = 0;
1200 		(void) strncat(buf, zc->zc_value, sizeof (buf));
1201 		error = dmu_objset_open(buf, DMU_OST_ANY,
1202 		    DS_MODE_STANDARD | DS_MODE_READONLY, &fromsnap);
1203 		if (error) {
1204 			dmu_objset_close(tosnap);
1205 			return (error);
1206 		}
1207 	}
1208 
1209 	fp = getf(zc->zc_cookie);
1210 	if (fp == NULL) {
1211 		dmu_objset_close(tosnap);
1212 		if (fromsnap)
1213 			dmu_objset_close(fromsnap);
1214 		return (EBADF);
1215 	}
1216 
1217 	error = dmu_sendbackup(tosnap, fromsnap, fp->f_vnode);
1218 
1219 	releasef(zc->zc_cookie);
1220 	if (fromsnap)
1221 		dmu_objset_close(fromsnap);
1222 	dmu_objset_close(tosnap);
1223 	return (error);
1224 }
1225 
1226 static int
1227 zfs_ioc_inject_fault(zfs_cmd_t *zc)
1228 {
1229 	int id, error;
1230 
1231 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
1232 	    &zc->zc_inject_record);
1233 
1234 	if (error == 0)
1235 		zc->zc_guid = (uint64_t)id;
1236 
1237 	return (error);
1238 }
1239 
1240 static int
1241 zfs_ioc_clear_fault(zfs_cmd_t *zc)
1242 {
1243 	return (zio_clear_fault((int)zc->zc_guid));
1244 }
1245 
1246 static int
1247 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
1248 {
1249 	int id = (int)zc->zc_guid;
1250 	int error;
1251 
1252 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
1253 	    &zc->zc_inject_record);
1254 
1255 	zc->zc_guid = id;
1256 
1257 	return (error);
1258 }
1259 
1260 static int
1261 zfs_ioc_error_log(zfs_cmd_t *zc)
1262 {
1263 	spa_t *spa;
1264 	int error;
1265 	size_t count = (size_t)zc->zc_nvlist_dst_size;
1266 
1267 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1268 		return (error);
1269 
1270 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
1271 	    &count);
1272 	if (error == 0)
1273 		zc->zc_nvlist_dst_size = count;
1274 	else
1275 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
1276 
1277 	spa_close(spa, FTAG);
1278 
1279 	return (error);
1280 }
1281 
1282 static int
1283 zfs_ioc_clear(zfs_cmd_t *zc)
1284 {
1285 	spa_t *spa;
1286 	vdev_t *vd;
1287 	int error;
1288 
1289 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1290 		return (error);
1291 
1292 	spa_config_enter(spa, RW_WRITER, FTAG);
1293 
1294 	if (zc->zc_guid == 0) {
1295 		vd = NULL;
1296 	} else if ((vd = spa_lookup_by_guid(spa, zc->zc_guid)) == NULL) {
1297 		spa_config_exit(spa, FTAG);
1298 		spa_close(spa, FTAG);
1299 		return (ENODEV);
1300 	}
1301 
1302 	vdev_clear(spa, vd);
1303 
1304 	spa_config_exit(spa, FTAG);
1305 
1306 	spa_close(spa, FTAG);
1307 
1308 	return (0);
1309 }
1310 
1311 static int
1312 zfs_ioc_bookmark_name(zfs_cmd_t *zc)
1313 {
1314 	spa_t *spa;
1315 	int error;
1316 	nvlist_t *nvl;
1317 
1318 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1319 		return (error);
1320 
1321 	VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1322 
1323 	error = spa_bookmark_name(spa, &zc->zc_bookmark, nvl);
1324 	if (error == 0)
1325 		error = put_nvlist(zc, nvl);
1326 	nvlist_free(nvl);
1327 
1328 	spa_close(spa, FTAG);
1329 
1330 	return (error);
1331 }
1332 
1333 static int
1334 zfs_ioc_promote(zfs_cmd_t *zc)
1335 {
1336 	char *cp;
1337 
1338 	/*
1339 	 * We don't need to unmount *all* the origin fs's snapshots, but
1340 	 * it's easier.
1341 	 */
1342 	cp = strchr(zc->zc_value, '@');
1343 	if (cp)
1344 		*cp = '\0';
1345 	(void) dmu_objset_find(zc->zc_value,
1346 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
1347 	return (dsl_dataset_promote(zc->zc_name));
1348 }
1349 
1350 static zfs_ioc_vec_t zfs_ioc_vec[] = {
1351 	{ zfs_ioc_pool_create,		zfs_secpolicy_config,	pool_name },
1352 	{ zfs_ioc_pool_destroy,		zfs_secpolicy_config,	pool_name },
1353 	{ zfs_ioc_pool_import,		zfs_secpolicy_config,	pool_name },
1354 	{ zfs_ioc_pool_export,		zfs_secpolicy_config,	pool_name },
1355 	{ zfs_ioc_pool_configs,		zfs_secpolicy_none,	no_name },
1356 	{ zfs_ioc_pool_stats,		zfs_secpolicy_read,	pool_name },
1357 	{ zfs_ioc_pool_tryimport,	zfs_secpolicy_config,	no_name },
1358 	{ zfs_ioc_pool_scrub,		zfs_secpolicy_config,	pool_name },
1359 	{ zfs_ioc_pool_freeze,		zfs_secpolicy_config,	no_name },
1360 	{ zfs_ioc_pool_upgrade,		zfs_secpolicy_config,	pool_name },
1361 	{ zfs_ioc_vdev_add,		zfs_secpolicy_config,	pool_name },
1362 	{ zfs_ioc_vdev_remove,		zfs_secpolicy_config,	pool_name },
1363 	{ zfs_ioc_vdev_online,		zfs_secpolicy_config,	pool_name },
1364 	{ zfs_ioc_vdev_offline,		zfs_secpolicy_config,	pool_name },
1365 	{ zfs_ioc_vdev_attach,		zfs_secpolicy_config,	pool_name },
1366 	{ zfs_ioc_vdev_detach,		zfs_secpolicy_config,	pool_name },
1367 	{ zfs_ioc_vdev_setpath,		zfs_secpolicy_config,	pool_name },
1368 	{ zfs_ioc_objset_stats,		zfs_secpolicy_read,	dataset_name },
1369 	{ zfs_ioc_dataset_list_next,	zfs_secpolicy_read,	dataset_name },
1370 	{ zfs_ioc_snapshot_list_next,	zfs_secpolicy_read,	dataset_name },
1371 	{ zfs_ioc_set_prop,		zfs_secpolicy_write,	dataset_name },
1372 	{ zfs_ioc_create_minor,		zfs_secpolicy_config,	dataset_name },
1373 	{ zfs_ioc_remove_minor,		zfs_secpolicy_config,	dataset_name },
1374 	{ zfs_ioc_create,		zfs_secpolicy_parent,	dataset_name },
1375 	{ zfs_ioc_destroy,		zfs_secpolicy_parent,	dataset_name },
1376 	{ zfs_ioc_rollback,		zfs_secpolicy_write,	dataset_name },
1377 	{ zfs_ioc_rename,		zfs_secpolicy_write,	dataset_name },
1378 	{ zfs_ioc_recvbackup,		zfs_secpolicy_write,	dataset_name },
1379 	{ zfs_ioc_sendbackup,		zfs_secpolicy_write,	dataset_name },
1380 	{ zfs_ioc_inject_fault,		zfs_secpolicy_inject,	no_name },
1381 	{ zfs_ioc_clear_fault,		zfs_secpolicy_inject,	no_name },
1382 	{ zfs_ioc_inject_list_next,	zfs_secpolicy_inject,	no_name },
1383 	{ zfs_ioc_error_log,		zfs_secpolicy_inject,	pool_name },
1384 	{ zfs_ioc_clear,		zfs_secpolicy_config,	pool_name },
1385 	{ zfs_ioc_bookmark_name,	zfs_secpolicy_inject,	pool_name },
1386 	{ zfs_ioc_promote,		zfs_secpolicy_write,	dataset_name },
1387 	{ zfs_ioc_destroy_snaps,	zfs_secpolicy_write,	dataset_name },
1388 	{ zfs_ioc_snapshot,		zfs_secpolicy_write,	dataset_name }
1389 };
1390 
1391 static int
1392 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1393 {
1394 	zfs_cmd_t *zc;
1395 	uint_t vec;
1396 	int error, rc;
1397 
1398 	if (getminor(dev) != 0)
1399 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
1400 
1401 	vec = cmd - ZFS_IOC;
1402 
1403 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
1404 		return (EINVAL);
1405 
1406 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
1407 
1408 	error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t));
1409 
1410 	if (error == 0) {
1411 		zc->zc_cred = (uintptr_t)cr;
1412 		zc->zc_dev = dev;
1413 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc->zc_name, cr);
1414 	}
1415 
1416 	/*
1417 	 * Ensure that all pool/dataset names are valid before we pass down to
1418 	 * the lower layers.
1419 	 */
1420 	if (error == 0) {
1421 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
1422 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
1423 		case pool_name:
1424 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
1425 				error = EINVAL;
1426 			break;
1427 
1428 		case dataset_name:
1429 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
1430 				error = EINVAL;
1431 			break;
1432 
1433 		case no_name:
1434 			break;
1435 		}
1436 	}
1437 
1438 	if (error == 0)
1439 		error = zfs_ioc_vec[vec].zvec_func(zc);
1440 
1441 	rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t));
1442 	if (error == 0)
1443 		error = rc;
1444 
1445 	kmem_free(zc, sizeof (zfs_cmd_t));
1446 	return (error);
1447 }
1448 
1449 static int
1450 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1451 {
1452 	if (cmd != DDI_ATTACH)
1453 		return (DDI_FAILURE);
1454 
1455 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
1456 	    DDI_PSEUDO, 0) == DDI_FAILURE)
1457 		return (DDI_FAILURE);
1458 
1459 	zfs_dip = dip;
1460 
1461 	ddi_report_dev(dip);
1462 
1463 	return (DDI_SUCCESS);
1464 }
1465 
1466 static int
1467 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1468 {
1469 	if (spa_busy() || zfs_busy() || zvol_busy())
1470 		return (DDI_FAILURE);
1471 
1472 	if (cmd != DDI_DETACH)
1473 		return (DDI_FAILURE);
1474 
1475 	zfs_dip = NULL;
1476 
1477 	ddi_prop_remove_all(dip);
1478 	ddi_remove_minor_node(dip, NULL);
1479 
1480 	return (DDI_SUCCESS);
1481 }
1482 
1483 /*ARGSUSED*/
1484 static int
1485 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1486 {
1487 	switch (infocmd) {
1488 	case DDI_INFO_DEVT2DEVINFO:
1489 		*result = zfs_dip;
1490 		return (DDI_SUCCESS);
1491 
1492 	case DDI_INFO_DEVT2INSTANCE:
1493 		*result = (void *)0;
1494 		return (DDI_SUCCESS);
1495 	}
1496 
1497 	return (DDI_FAILURE);
1498 }
1499 
1500 /*
1501  * OK, so this is a little weird.
1502  *
1503  * /dev/zfs is the control node, i.e. minor 0.
1504  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
1505  *
1506  * /dev/zfs has basically nothing to do except serve up ioctls,
1507  * so most of the standard driver entry points are in zvol.c.
1508  */
1509 static struct cb_ops zfs_cb_ops = {
1510 	zvol_open,	/* open */
1511 	zvol_close,	/* close */
1512 	zvol_strategy,	/* strategy */
1513 	nodev,		/* print */
1514 	nodev,		/* dump */
1515 	zvol_read,	/* read */
1516 	zvol_write,	/* write */
1517 	zfsdev_ioctl,	/* ioctl */
1518 	nodev,		/* devmap */
1519 	nodev,		/* mmap */
1520 	nodev,		/* segmap */
1521 	nochpoll,	/* poll */
1522 	ddi_prop_op,	/* prop_op */
1523 	NULL,		/* streamtab */
1524 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
1525 	CB_REV,		/* version */
1526 	zvol_aread,	/* async read */
1527 	zvol_awrite,	/* async write */
1528 };
1529 
1530 static struct dev_ops zfs_dev_ops = {
1531 	DEVO_REV,	/* version */
1532 	0,		/* refcnt */
1533 	zfs_info,	/* info */
1534 	nulldev,	/* identify */
1535 	nulldev,	/* probe */
1536 	zfs_attach,	/* attach */
1537 	zfs_detach,	/* detach */
1538 	nodev,		/* reset */
1539 	&zfs_cb_ops,	/* driver operations */
1540 	NULL		/* no bus operations */
1541 };
1542 
1543 static struct modldrv zfs_modldrv = {
1544 	&mod_driverops, "ZFS storage pool version " ZFS_VERSION_STRING,
1545 	    &zfs_dev_ops
1546 };
1547 
1548 static struct modlinkage modlinkage = {
1549 	MODREV_1,
1550 	(void *)&zfs_modlfs,
1551 	(void *)&zfs_modldrv,
1552 	NULL
1553 };
1554 
1555 int
1556 _init(void)
1557 {
1558 	int error;
1559 
1560 	spa_init(FREAD | FWRITE);
1561 	zfs_init();
1562 	zvol_init();
1563 
1564 	if ((error = mod_install(&modlinkage)) != 0) {
1565 		zvol_fini();
1566 		zfs_fini();
1567 		spa_fini();
1568 		return (error);
1569 	}
1570 
1571 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
1572 	ASSERT(error == 0);
1573 
1574 	return (0);
1575 }
1576 
1577 int
1578 _fini(void)
1579 {
1580 	int error;
1581 
1582 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
1583 		return (EBUSY);
1584 
1585 	if ((error = mod_remove(&modlinkage)) != 0)
1586 		return (error);
1587 
1588 	zvol_fini();
1589 	zfs_fini();
1590 	spa_fini();
1591 
1592 	ldi_ident_release(zfs_li);
1593 	zfs_li = NULL;
1594 
1595 	return (error);
1596 }
1597 
1598 int
1599 _info(struct modinfo *modinfop)
1600 {
1601 	return (mod_info(&modlinkage, modinfop));
1602 }
1603