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