xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision c9431fa1e59a88c2f0abf611f25b97af964449e5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%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 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
438 		return (error);
439 
440 	error = spa_scrub(spa, zc->zc_cookie, B_FALSE);
441 
442 	spa_close(spa, FTAG);
443 
444 	return (error);
445 }
446 
447 static int
448 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
449 {
450 	spa_t *spa;
451 	int error;
452 
453 	error = spa_open(zc->zc_name, &spa, FTAG);
454 	if (error == 0) {
455 		spa_freeze(spa);
456 		spa_close(spa, FTAG);
457 	}
458 	return (error);
459 }
460 
461 static int
462 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
463 {
464 	spa_t *spa;
465 	int error;
466 
467 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
468 		return (error);
469 
470 	spa_upgrade(spa);
471 
472 	spa_close(spa, FTAG);
473 
474 	return (error);
475 }
476 
477 static int
478 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
479 {
480 	spa_t *spa;
481 	char *hist_buf;
482 	uint64_t size;
483 	int error;
484 
485 	if ((size = zc->zc_history_len) == 0)
486 		return (EINVAL);
487 
488 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
489 		return (error);
490 
491 	if (spa_version(spa) < ZFS_VERSION_ZPOOL_HISTORY) {
492 		spa_close(spa, FTAG);
493 		return (ENOTSUP);
494 	}
495 
496 	hist_buf = kmem_alloc(size, KM_SLEEP);
497 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
498 	    &zc->zc_history_len, hist_buf)) == 0) {
499 		error = xcopyout(hist_buf, (char *)(uintptr_t)zc->zc_history,
500 		    zc->zc_history_len);
501 	}
502 
503 	spa_close(spa, FTAG);
504 	kmem_free(hist_buf, size);
505 	return (error);
506 }
507 
508 static int
509 zfs_ioc_pool_log_history(zfs_cmd_t *zc)
510 {
511 	spa_t *spa;
512 	char *history_str = NULL;
513 	size_t size;
514 	int error;
515 
516 	size = zc->zc_history_len;
517 	if (size == 0 || size > HIS_MAX_RECORD_LEN)
518 		return (EINVAL);
519 
520 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
521 		return (error);
522 
523 	if (spa_version(spa) < ZFS_VERSION_ZPOOL_HISTORY) {
524 		spa_close(spa, FTAG);
525 		return (ENOTSUP);
526 	}
527 
528 	/* add one for the NULL delimiter */
529 	size++;
530 	history_str = kmem_alloc(size, KM_SLEEP);
531 	if ((error = xcopyin((void *)(uintptr_t)zc->zc_history, history_str,
532 	    size)) != 0) {
533 		spa_close(spa, FTAG);
534 		kmem_free(history_str, size);
535 		return (error);
536 	}
537 	history_str[size - 1] = '\0';
538 
539 	error = spa_history_log(spa, history_str, zc->zc_history_offset);
540 
541 	spa_close(spa, FTAG);
542 	kmem_free(history_str, size);
543 
544 	return (error);
545 }
546 
547 static int
548 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
549 {
550 	spa_t *spa;
551 	dsl_pool_t *dp;
552 	dsl_dataset_t *ds = NULL;
553 	int error;
554 
555 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
556 		return (error);
557 	dp = spa_get_dsl(spa);
558 	rw_enter(&dp->dp_config_rwlock, RW_READER);
559 	if ((error = dsl_dataset_open_obj(dp, zc->zc_obj,
560 	    NULL, DS_MODE_NONE, FTAG, &ds)) != 0) {
561 		rw_exit(&dp->dp_config_rwlock);
562 		spa_close(spa, FTAG);
563 		return (error);
564 	}
565 	dsl_dataset_name(ds, zc->zc_value);
566 	dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
567 	rw_exit(&dp->dp_config_rwlock);
568 	spa_close(spa, FTAG);
569 
570 	return (0);
571 }
572 
573 static int
574 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
575 {
576 	objset_t *osp;
577 	int error;
578 
579 	if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS,
580 	    DS_MODE_NONE | DS_MODE_READONLY, &osp)) != 0)
581 		return (error);
582 
583 	error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value,
584 	    sizeof (zc->zc_value));
585 	dmu_objset_close(osp);
586 
587 	return (error);
588 }
589 
590 static int
591 zfs_ioc_vdev_add(zfs_cmd_t *zc)
592 {
593 	spa_t *spa;
594 	int error;
595 	nvlist_t *config;
596 
597 	error = spa_open(zc->zc_name, &spa, FTAG);
598 	if (error != 0)
599 		return (error);
600 
601 	if ((error = get_nvlist(zc, &config)) == 0) {
602 		error = spa_vdev_add(spa, config);
603 		nvlist_free(config);
604 	}
605 
606 	spa_close(spa, FTAG);
607 	return (error);
608 }
609 
610 static int
611 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
612 {
613 	spa_t *spa;
614 	int error;
615 
616 	error = spa_open(zc->zc_name, &spa, FTAG);
617 	if (error != 0)
618 		return (error);
619 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
620 	spa_close(spa, FTAG);
621 	return (error);
622 }
623 
624 static int
625 zfs_ioc_vdev_online(zfs_cmd_t *zc)
626 {
627 	spa_t *spa;
628 	int error;
629 
630 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
631 		return (error);
632 	error = vdev_online(spa, zc->zc_guid);
633 	spa_close(spa, FTAG);
634 	return (error);
635 }
636 
637 static int
638 zfs_ioc_vdev_offline(zfs_cmd_t *zc)
639 {
640 	spa_t *spa;
641 	int istmp = zc->zc_cookie;
642 	int error;
643 
644 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
645 		return (error);
646 	error = vdev_offline(spa, zc->zc_guid, istmp);
647 	spa_close(spa, FTAG);
648 	return (error);
649 }
650 
651 static int
652 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
653 {
654 	spa_t *spa;
655 	int replacing = zc->zc_cookie;
656 	nvlist_t *config;
657 	int error;
658 
659 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
660 		return (error);
661 
662 	if ((error = get_nvlist(zc, &config)) == 0) {
663 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
664 		nvlist_free(config);
665 	}
666 
667 	spa_close(spa, FTAG);
668 	return (error);
669 }
670 
671 static int
672 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
673 {
674 	spa_t *spa;
675 	int error;
676 
677 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
678 		return (error);
679 
680 	error = spa_vdev_detach(spa, zc->zc_guid, B_FALSE);
681 
682 	spa_close(spa, FTAG);
683 	return (error);
684 }
685 
686 static int
687 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
688 {
689 	spa_t *spa;
690 	char *path = zc->zc_value;
691 	uint64_t guid = zc->zc_guid;
692 	int error;
693 
694 	error = spa_open(zc->zc_name, &spa, FTAG);
695 	if (error != 0)
696 		return (error);
697 
698 	error = spa_vdev_setpath(spa, guid, path);
699 	spa_close(spa, FTAG);
700 	return (error);
701 }
702 
703 static int
704 zfs_ioc_objset_stats(zfs_cmd_t *zc)
705 {
706 	objset_t *os = NULL;
707 	int error;
708 	nvlist_t *nv;
709 
710 retry:
711 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
712 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
713 	if (error != 0) {
714 		/*
715 		 * This is ugly: dmu_objset_open() can return EBUSY if
716 		 * the objset is held exclusively. Fortunately this hold is
717 		 * only for a short while, so we retry here.
718 		 * This avoids user code having to handle EBUSY,
719 		 * for example for a "zfs list".
720 		 */
721 		if (error == EBUSY) {
722 			delay(1);
723 			goto retry;
724 		}
725 		return (error);
726 	}
727 
728 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
729 
730 	if (zc->zc_nvlist_dst != 0 &&
731 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
732 		dmu_objset_stats(os, nv);
733 		/*
734 		 * NB: zvol_get_stats() will read the objset contents,
735 		 * which we aren't supposed to do with a
736 		 * DS_MODE_STANDARD open, because it could be
737 		 * inconsistent.  So this is a bit of a workaround...
738 		 */
739 		if (!zc->zc_objset_stats.dds_inconsistent &&
740 		    dmu_objset_type(os) == DMU_OST_ZVOL)
741 			VERIFY(zvol_get_stats(os, nv) == 0);
742 		error = put_nvlist(zc, nv);
743 		nvlist_free(nv);
744 	}
745 
746 	spa_altroot(dmu_objset_spa(os), zc->zc_value, sizeof (zc->zc_value));
747 
748 	dmu_objset_close(os);
749 	return (error);
750 }
751 
752 static int
753 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
754 {
755 	objset_t *os;
756 	int error;
757 	char *p;
758 
759 retry:
760 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
761 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
762 	if (error != 0) {
763 		/*
764 		 * This is ugly: dmu_objset_open() can return EBUSY if
765 		 * the objset is held exclusively. Fortunately this hold is
766 		 * only for a short while, so we retry here.
767 		 * This avoids user code having to handle EBUSY,
768 		 * for example for a "zfs list".
769 		 */
770 		if (error == EBUSY) {
771 			delay(1);
772 			goto retry;
773 		}
774 		if (error == ENOENT)
775 			error = ESRCH;
776 		return (error);
777 	}
778 
779 	p = strrchr(zc->zc_name, '/');
780 	if (p == NULL || p[1] != '\0')
781 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
782 	p = zc->zc_name + strlen(zc->zc_name);
783 
784 	do {
785 		error = dmu_dir_list_next(os,
786 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
787 		    NULL, &zc->zc_cookie);
788 		if (error == ENOENT)
789 			error = ESRCH;
790 	} while (error == 0 && !INGLOBALZONE(curproc) &&
791 	    !zone_dataset_visible(zc->zc_name, NULL));
792 
793 	/*
794 	 * If it's a hidden dataset (ie. with a '$' in its name), don't
795 	 * try to get stats for it.  Userland will skip over it.
796 	 */
797 	if (error == 0 && strchr(zc->zc_name, '$') == NULL)
798 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
799 
800 	dmu_objset_close(os);
801 	return (error);
802 }
803 
804 static int
805 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
806 {
807 	objset_t *os;
808 	int error;
809 
810 retry:
811 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
812 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
813 	if (error != 0) {
814 		/*
815 		 * This is ugly: dmu_objset_open() can return EBUSY if
816 		 * the objset is held exclusively. Fortunately this hold is
817 		 * only for a short while, so we retry here.
818 		 * This avoids user code having to handle EBUSY,
819 		 * for example for a "zfs list".
820 		 */
821 		if (error == EBUSY) {
822 			delay(1);
823 			goto retry;
824 		}
825 		if (error == ENOENT)
826 			error = ESRCH;
827 		return (error);
828 	}
829 
830 	/*
831 	 * A dataset name of maximum length cannot have any snapshots,
832 	 * so exit immediately.
833 	 */
834 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
835 		dmu_objset_close(os);
836 		return (ESRCH);
837 	}
838 
839 	error = dmu_snapshot_list_next(os,
840 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
841 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie);
842 	if (error == ENOENT)
843 		error = ESRCH;
844 
845 	if (error == 0)
846 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
847 
848 	dmu_objset_close(os);
849 	return (error);
850 }
851 
852 static int
853 zfs_set_prop_nvlist(const char *name, dev_t dev, cred_t *cr, nvlist_t *nvl)
854 {
855 	nvpair_t *elem;
856 	int error;
857 	const char *propname;
858 	zfs_prop_t prop;
859 	uint64_t intval;
860 	char *strval;
861 	char buf[MAXNAMELEN];
862 	const char *p;
863 	spa_t *spa;
864 
865 	elem = NULL;
866 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
867 		propname = nvpair_name(elem);
868 
869 		if ((prop = zfs_name_to_prop(propname)) ==
870 		    ZFS_PROP_INVAL) {
871 			/*
872 			 * If this is a user-defined property, it must be a
873 			 * string, and there is no further validation to do.
874 			 */
875 			if (!zfs_prop_user(propname) ||
876 			    nvpair_type(elem) != DATA_TYPE_STRING)
877 				return (EINVAL);
878 
879 			VERIFY(nvpair_value_string(elem, &strval) == 0);
880 			error = dsl_prop_set(name, propname, 1,
881 			    strlen(strval) + 1, strval);
882 			if (error == 0)
883 				continue;
884 			else
885 				return (error);
886 		}
887 
888 		/*
889 		 * Check permissions for special properties.
890 		 */
891 		switch (prop) {
892 		case ZFS_PROP_ZONED:
893 			/*
894 			 * Disallow setting of 'zoned' from within a local zone.
895 			 */
896 			if (!INGLOBALZONE(curproc))
897 				return (EPERM);
898 			break;
899 
900 		case ZFS_PROP_QUOTA:
901 			if (error = zfs_dozonecheck(name, cr))
902 				return (error);
903 
904 			if (!INGLOBALZONE(curproc)) {
905 				uint64_t zoned;
906 				char setpoint[MAXNAMELEN];
907 				int dslen;
908 				/*
909 				 * Unprivileged users are allowed to modify the
910 				 * quota on things *under* (ie. contained by)
911 				 * the thing they own.
912 				 */
913 				if (dsl_prop_get_integer(name, "zoned", &zoned,
914 				    setpoint))
915 					return (EPERM);
916 				if (!zoned) /* this shouldn't happen */
917 					return (EPERM);
918 				dslen = strlen(name);
919 				if (dslen <= strlen(setpoint))
920 					return (EPERM);
921 			}
922 			break;
923 
924 		case ZFS_PROP_COMPRESSION:
925 			/*
926 			 * If the user specified gzip compression, make sure
927 			 * the SPA supports it. We ignore any errors here since
928 			 * we'll catch them later.
929 			 */
930 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
931 			    nvpair_value_uint64(elem, &intval) == 0 &&
932 			    intval >= ZIO_COMPRESS_GZIP_1 &&
933 			    intval <= ZIO_COMPRESS_GZIP_9) {
934 				if ((p = strchr(name, '/')) == NULL) {
935 					p = name;
936 				} else {
937 					bcopy(name, buf, p - name);
938 					buf[p - name] = '\0';
939 					p = buf;
940 				}
941 
942 				if (spa_open(p, &spa, FTAG) == 0) {
943 					if (spa_version(spa) <
944 					    ZFS_VERSION_GZIP_COMPRESSION) {
945 						spa_close(spa, FTAG);
946 						return (ENOTSUP);
947 					}
948 
949 					spa_close(spa, FTAG);
950 				}
951 			}
952 			break;
953 		}
954 
955 		switch (prop) {
956 		case ZFS_PROP_QUOTA:
957 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
958 			    (error = dsl_dir_set_quota(name,
959 			    intval)) != 0)
960 				return (error);
961 			break;
962 
963 		case ZFS_PROP_RESERVATION:
964 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
965 			    (error = dsl_dir_set_reservation(name,
966 			    intval)) != 0)
967 				return (error);
968 			break;
969 
970 		case ZFS_PROP_VOLSIZE:
971 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
972 			    (error = zvol_set_volsize(name, dev,
973 			    intval)) != 0)
974 				return (error);
975 			break;
976 
977 		case ZFS_PROP_VOLBLOCKSIZE:
978 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
979 			    (error = zvol_set_volblocksize(name,
980 			    intval)) != 0)
981 				return (error);
982 			break;
983 
984 		default:
985 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
986 				if (zfs_prop_get_type(prop) !=
987 				    prop_type_string)
988 					return (EINVAL);
989 				VERIFY(nvpair_value_string(elem, &strval) == 0);
990 				if ((error = dsl_prop_set(name,
991 				    nvpair_name(elem), 1, strlen(strval) + 1,
992 				    strval)) != 0)
993 					return (error);
994 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
995 				const char *unused;
996 
997 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
998 
999 				switch (zfs_prop_get_type(prop)) {
1000 				case prop_type_number:
1001 					break;
1002 				case prop_type_boolean:
1003 					if (intval > 1)
1004 						return (EINVAL);
1005 					break;
1006 				case prop_type_string:
1007 					return (EINVAL);
1008 				case prop_type_index:
1009 					if (zfs_prop_index_to_string(prop,
1010 					    intval, &unused) != 0)
1011 						return (EINVAL);
1012 					break;
1013 				default:
1014 					cmn_err(CE_PANIC, "unknown property "
1015 					    "type");
1016 					break;
1017 				}
1018 
1019 				if ((error = dsl_prop_set(name, propname,
1020 				    8, 1, &intval)) != 0)
1021 					return (error);
1022 			} else {
1023 				return (EINVAL);
1024 			}
1025 			break;
1026 		}
1027 	}
1028 
1029 	return (0);
1030 }
1031 
1032 static int
1033 zfs_ioc_set_prop(zfs_cmd_t *zc)
1034 {
1035 	nvlist_t *nvl;
1036 	int error;
1037 	zfs_prop_t prop;
1038 
1039 	/*
1040 	 * If zc_value is set, then this is an attempt to inherit a value.
1041 	 * Otherwise, zc_nvlist refers to a list of properties to set.
1042 	 */
1043 	if (zc->zc_value[0] != '\0') {
1044 		if (!zfs_prop_user(zc->zc_value) &&
1045 		    ((prop = zfs_name_to_prop(zc->zc_value)) ==
1046 		    ZFS_PROP_INVAL ||
1047 		    !zfs_prop_inheritable(prop)))
1048 			return (EINVAL);
1049 
1050 		return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
1051 	}
1052 
1053 	if ((error = get_nvlist(zc, &nvl)) != 0)
1054 		return (error);
1055 
1056 	error = zfs_set_prop_nvlist(zc->zc_name, zc->zc_dev,
1057 	    (cred_t *)(uintptr_t)zc->zc_cred, nvl);
1058 	nvlist_free(nvl);
1059 	return (error);
1060 }
1061 
1062 static int
1063 zfs_ioc_create_minor(zfs_cmd_t *zc)
1064 {
1065 	return (zvol_create_minor(zc->zc_name, zc->zc_dev));
1066 }
1067 
1068 static int
1069 zfs_ioc_remove_minor(zfs_cmd_t *zc)
1070 {
1071 	return (zvol_remove_minor(zc->zc_name));
1072 }
1073 
1074 /*
1075  * Search the vfs list for a specified resource.  Returns a pointer to it
1076  * or NULL if no suitable entry is found. The caller of this routine
1077  * is responsible for releasing the returned vfs pointer.
1078  */
1079 static vfs_t *
1080 zfs_get_vfs(const char *resource)
1081 {
1082 	struct vfs *vfsp;
1083 	struct vfs *vfs_found = NULL;
1084 
1085 	vfs_list_read_lock();
1086 	vfsp = rootvfs;
1087 	do {
1088 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
1089 			VFS_HOLD(vfsp);
1090 			vfs_found = vfsp;
1091 			break;
1092 		}
1093 		vfsp = vfsp->vfs_next;
1094 	} while (vfsp != rootvfs);
1095 	vfs_list_unlock();
1096 	return (vfs_found);
1097 }
1098 
1099 static void
1100 zfs_create_cb(objset_t *os, void *arg, dmu_tx_t *tx)
1101 {
1102 	zfs_create_data_t *zc = arg;
1103 	zfs_create_fs(os, (cred_t *)(uintptr_t)zc->zc_cred, tx);
1104 }
1105 
1106 static int
1107 zfs_ioc_create(zfs_cmd_t *zc)
1108 {
1109 	objset_t *clone;
1110 	int error = 0;
1111 	zfs_create_data_t cbdata = { 0 };
1112 	void (*cbfunc)(objset_t *os, void *arg, dmu_tx_t *tx);
1113 	dmu_objset_type_t type = zc->zc_objset_type;
1114 
1115 	switch (type) {
1116 
1117 	case DMU_OST_ZFS:
1118 		cbfunc = zfs_create_cb;
1119 		break;
1120 
1121 	case DMU_OST_ZVOL:
1122 		cbfunc = zvol_create_cb;
1123 		break;
1124 
1125 	default:
1126 		cbfunc = NULL;
1127 	}
1128 	if (strchr(zc->zc_name, '@'))
1129 		return (EINVAL);
1130 
1131 	if (zc->zc_nvlist_src != NULL &&
1132 	    (error = get_nvlist(zc, &cbdata.zc_props)) != 0)
1133 		return (error);
1134 
1135 	cbdata.zc_cred = (cred_t *)(uintptr_t)zc->zc_cred;
1136 	cbdata.zc_dev = (dev_t)zc->zc_dev;
1137 
1138 	if (zc->zc_value[0] != '\0') {
1139 		/*
1140 		 * We're creating a clone of an existing snapshot.
1141 		 */
1142 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
1143 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
1144 			nvlist_free(cbdata.zc_props);
1145 			return (EINVAL);
1146 		}
1147 
1148 		error = dmu_objset_open(zc->zc_value, type,
1149 		    DS_MODE_STANDARD | DS_MODE_READONLY, &clone);
1150 		if (error) {
1151 			nvlist_free(cbdata.zc_props);
1152 			return (error);
1153 		}
1154 		error = dmu_objset_create(zc->zc_name, type, clone, NULL, NULL);
1155 		dmu_objset_close(clone);
1156 	} else {
1157 		if (cbfunc == NULL) {
1158 			nvlist_free(cbdata.zc_props);
1159 			return (EINVAL);
1160 		}
1161 
1162 		if (type == DMU_OST_ZVOL) {
1163 			uint64_t volsize, volblocksize;
1164 
1165 			if (cbdata.zc_props == NULL ||
1166 			    nvlist_lookup_uint64(cbdata.zc_props,
1167 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1168 			    &volsize) != 0) {
1169 				nvlist_free(cbdata.zc_props);
1170 				return (EINVAL);
1171 			}
1172 
1173 			if ((error = nvlist_lookup_uint64(cbdata.zc_props,
1174 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1175 			    &volblocksize)) != 0 && error != ENOENT) {
1176 				nvlist_free(cbdata.zc_props);
1177 				return (EINVAL);
1178 			}
1179 
1180 			if (error != 0)
1181 				volblocksize = zfs_prop_default_numeric(
1182 				    ZFS_PROP_VOLBLOCKSIZE);
1183 
1184 			if ((error = zvol_check_volblocksize(
1185 			    volblocksize)) != 0 ||
1186 			    (error = zvol_check_volsize(volsize,
1187 			    volblocksize)) != 0) {
1188 				nvlist_free(cbdata.zc_props);
1189 				return (error);
1190 			}
1191 		}
1192 
1193 		error = dmu_objset_create(zc->zc_name, type, NULL, cbfunc,
1194 		    &cbdata);
1195 	}
1196 
1197 	/*
1198 	 * It would be nice to do this atomically.
1199 	 */
1200 	if (error == 0) {
1201 		if ((error = zfs_set_prop_nvlist(zc->zc_name,
1202 		    zc->zc_dev, (cred_t *)(uintptr_t)zc->zc_cred,
1203 		    cbdata.zc_props)) != 0)
1204 			(void) dmu_objset_destroy(zc->zc_name);
1205 	}
1206 
1207 	nvlist_free(cbdata.zc_props);
1208 	return (error);
1209 }
1210 
1211 static int
1212 zfs_ioc_snapshot(zfs_cmd_t *zc)
1213 {
1214 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
1215 		return (EINVAL);
1216 	return (dmu_objset_snapshot(zc->zc_name,
1217 	    zc->zc_value, zc->zc_cookie));
1218 }
1219 
1220 static int
1221 zfs_unmount_snap(char *name, void *arg)
1222 {
1223 	char *snapname = arg;
1224 	char *cp;
1225 	vfs_t *vfsp = NULL;
1226 
1227 	/*
1228 	 * Snapshots (which are under .zfs control) must be unmounted
1229 	 * before they can be destroyed.
1230 	 */
1231 
1232 	if (snapname) {
1233 		(void) strcat(name, "@");
1234 		(void) strcat(name, snapname);
1235 		vfsp = zfs_get_vfs(name);
1236 		cp = strchr(name, '@');
1237 		*cp = '\0';
1238 	} else if (strchr(name, '@')) {
1239 		vfsp = zfs_get_vfs(name);
1240 	}
1241 
1242 	if (vfsp) {
1243 		/*
1244 		 * Always force the unmount for snapshots.
1245 		 */
1246 		int flag = MS_FORCE;
1247 		int err;
1248 
1249 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
1250 			VFS_RELE(vfsp);
1251 			return (err);
1252 		}
1253 		VFS_RELE(vfsp);
1254 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
1255 			return (err);
1256 	}
1257 	return (0);
1258 }
1259 
1260 static int
1261 zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
1262 {
1263 	int err;
1264 
1265 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
1266 		return (EINVAL);
1267 	err = dmu_objset_find(zc->zc_name,
1268 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
1269 	if (err)
1270 		return (err);
1271 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
1272 }
1273 
1274 static int
1275 zfs_ioc_destroy(zfs_cmd_t *zc)
1276 {
1277 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
1278 		int err = zfs_unmount_snap(zc->zc_name, NULL);
1279 		if (err)
1280 			return (err);
1281 	}
1282 
1283 	return (dmu_objset_destroy(zc->zc_name));
1284 }
1285 
1286 static int
1287 zfs_ioc_rollback(zfs_cmd_t *zc)
1288 {
1289 	return (dmu_objset_rollback(zc->zc_name));
1290 }
1291 
1292 static int
1293 zfs_ioc_rename(zfs_cmd_t *zc)
1294 {
1295 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
1296 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0)
1297 		return (EINVAL);
1298 
1299 	if (strchr(zc->zc_name, '@') != NULL &&
1300 	    zc->zc_objset_type == DMU_OST_ZFS) {
1301 		int err = zfs_unmount_snap(zc->zc_name, NULL);
1302 		if (err)
1303 			return (err);
1304 	}
1305 
1306 	return (dmu_objset_rename(zc->zc_name, zc->zc_value));
1307 }
1308 
1309 static int
1310 zfs_ioc_recvbackup(zfs_cmd_t *zc)
1311 {
1312 	file_t *fp;
1313 	int error, fd;
1314 	offset_t new_off;
1315 
1316 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
1317 	    strchr(zc->zc_value, '@') == NULL)
1318 		return (EINVAL);
1319 
1320 	fd = zc->zc_cookie;
1321 	fp = getf(fd);
1322 	if (fp == NULL)
1323 		return (EBADF);
1324 	error = dmu_recvbackup(zc->zc_value, &zc->zc_begin_record,
1325 	    &zc->zc_cookie, (boolean_t)zc->zc_guid, fp->f_vnode,
1326 	    fp->f_offset);
1327 
1328 	new_off = fp->f_offset + zc->zc_cookie;
1329 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &new_off) == 0)
1330 		fp->f_offset = new_off;
1331 
1332 	releasef(fd);
1333 	return (error);
1334 }
1335 
1336 static int
1337 zfs_ioc_sendbackup(zfs_cmd_t *zc)
1338 {
1339 	objset_t *fromsnap = NULL;
1340 	objset_t *tosnap;
1341 	file_t *fp;
1342 	int error;
1343 
1344 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1345 	    DS_MODE_STANDARD | DS_MODE_READONLY, &tosnap);
1346 	if (error)
1347 		return (error);
1348 
1349 	if (zc->zc_value[0] != '\0') {
1350 		char buf[MAXPATHLEN];
1351 		char *cp;
1352 
1353 		(void) strncpy(buf, zc->zc_name, sizeof (buf));
1354 		cp = strchr(buf, '@');
1355 		if (cp)
1356 			*(cp+1) = 0;
1357 		(void) strncat(buf, zc->zc_value, sizeof (buf));
1358 		error = dmu_objset_open(buf, DMU_OST_ANY,
1359 		    DS_MODE_STANDARD | DS_MODE_READONLY, &fromsnap);
1360 		if (error) {
1361 			dmu_objset_close(tosnap);
1362 			return (error);
1363 		}
1364 	}
1365 
1366 	fp = getf(zc->zc_cookie);
1367 	if (fp == NULL) {
1368 		dmu_objset_close(tosnap);
1369 		if (fromsnap)
1370 			dmu_objset_close(fromsnap);
1371 		return (EBADF);
1372 	}
1373 
1374 	error = dmu_sendbackup(tosnap, fromsnap, fp->f_vnode);
1375 
1376 	releasef(zc->zc_cookie);
1377 	if (fromsnap)
1378 		dmu_objset_close(fromsnap);
1379 	dmu_objset_close(tosnap);
1380 	return (error);
1381 }
1382 
1383 static int
1384 zfs_ioc_inject_fault(zfs_cmd_t *zc)
1385 {
1386 	int id, error;
1387 
1388 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
1389 	    &zc->zc_inject_record);
1390 
1391 	if (error == 0)
1392 		zc->zc_guid = (uint64_t)id;
1393 
1394 	return (error);
1395 }
1396 
1397 static int
1398 zfs_ioc_clear_fault(zfs_cmd_t *zc)
1399 {
1400 	return (zio_clear_fault((int)zc->zc_guid));
1401 }
1402 
1403 static int
1404 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
1405 {
1406 	int id = (int)zc->zc_guid;
1407 	int error;
1408 
1409 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
1410 	    &zc->zc_inject_record);
1411 
1412 	zc->zc_guid = id;
1413 
1414 	return (error);
1415 }
1416 
1417 static int
1418 zfs_ioc_error_log(zfs_cmd_t *zc)
1419 {
1420 	spa_t *spa;
1421 	int error;
1422 	size_t count = (size_t)zc->zc_nvlist_dst_size;
1423 
1424 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1425 		return (error);
1426 
1427 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
1428 	    &count);
1429 	if (error == 0)
1430 		zc->zc_nvlist_dst_size = count;
1431 	else
1432 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
1433 
1434 	spa_close(spa, FTAG);
1435 
1436 	return (error);
1437 }
1438 
1439 static int
1440 zfs_ioc_clear(zfs_cmd_t *zc)
1441 {
1442 	spa_t *spa;
1443 	vdev_t *vd;
1444 	int error;
1445 
1446 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1447 		return (error);
1448 
1449 	spa_config_enter(spa, RW_WRITER, FTAG);
1450 
1451 	if (zc->zc_guid == 0) {
1452 		vd = NULL;
1453 	} else if ((vd = spa_lookup_by_guid(spa, zc->zc_guid)) == NULL) {
1454 		spa_config_exit(spa, FTAG);
1455 		spa_close(spa, FTAG);
1456 		return (ENODEV);
1457 	}
1458 
1459 	vdev_clear(spa, vd);
1460 
1461 	spa_config_exit(spa, FTAG);
1462 
1463 	spa_close(spa, FTAG);
1464 
1465 	return (0);
1466 }
1467 
1468 static int
1469 zfs_ioc_promote(zfs_cmd_t *zc)
1470 {
1471 	char *cp;
1472 
1473 	/*
1474 	 * We don't need to unmount *all* the origin fs's snapshots, but
1475 	 * it's easier.
1476 	 */
1477 	cp = strchr(zc->zc_value, '@');
1478 	if (cp)
1479 		*cp = '\0';
1480 	(void) dmu_objset_find(zc->zc_value,
1481 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
1482 	return (dsl_dataset_promote(zc->zc_name));
1483 }
1484 
1485 static zfs_ioc_vec_t zfs_ioc_vec[] = {
1486 	{ zfs_ioc_pool_create,		zfs_secpolicy_config,	pool_name },
1487 	{ zfs_ioc_pool_destroy,		zfs_secpolicy_config,	pool_name },
1488 	{ zfs_ioc_pool_import,		zfs_secpolicy_config,	pool_name },
1489 	{ zfs_ioc_pool_export,		zfs_secpolicy_config,	pool_name },
1490 	{ zfs_ioc_pool_configs,		zfs_secpolicy_none,	no_name },
1491 	{ zfs_ioc_pool_stats,		zfs_secpolicy_read,	pool_name },
1492 	{ zfs_ioc_pool_tryimport,	zfs_secpolicy_config,	no_name },
1493 	{ zfs_ioc_pool_scrub,		zfs_secpolicy_config,	pool_name },
1494 	{ zfs_ioc_pool_freeze,		zfs_secpolicy_config,	no_name },
1495 	{ zfs_ioc_pool_upgrade,		zfs_secpolicy_config,	pool_name },
1496 	{ zfs_ioc_pool_get_history,	zfs_secpolicy_config,	pool_name },
1497 	{ zfs_ioc_pool_log_history,	zfs_secpolicy_config,	pool_name },
1498 	{ zfs_ioc_vdev_add,		zfs_secpolicy_config,	pool_name },
1499 	{ zfs_ioc_vdev_remove,		zfs_secpolicy_config,	pool_name },
1500 	{ zfs_ioc_vdev_online,		zfs_secpolicy_config,	pool_name },
1501 	{ zfs_ioc_vdev_offline,		zfs_secpolicy_config,	pool_name },
1502 	{ zfs_ioc_vdev_attach,		zfs_secpolicy_config,	pool_name },
1503 	{ zfs_ioc_vdev_detach,		zfs_secpolicy_config,	pool_name },
1504 	{ zfs_ioc_vdev_setpath,		zfs_secpolicy_config,	pool_name },
1505 	{ zfs_ioc_objset_stats,		zfs_secpolicy_read,	dataset_name },
1506 	{ zfs_ioc_dataset_list_next,	zfs_secpolicy_read,	dataset_name },
1507 	{ zfs_ioc_snapshot_list_next,	zfs_secpolicy_read,	dataset_name },
1508 	{ zfs_ioc_set_prop,		zfs_secpolicy_write,	dataset_name },
1509 	{ zfs_ioc_create_minor,		zfs_secpolicy_config,	dataset_name },
1510 	{ zfs_ioc_remove_minor,		zfs_secpolicy_config,	dataset_name },
1511 	{ zfs_ioc_create,		zfs_secpolicy_parent,	dataset_name },
1512 	{ zfs_ioc_destroy,		zfs_secpolicy_parent,	dataset_name },
1513 	{ zfs_ioc_rollback,		zfs_secpolicy_write,	dataset_name },
1514 	{ zfs_ioc_rename,		zfs_secpolicy_write,	dataset_name },
1515 	{ zfs_ioc_recvbackup,		zfs_secpolicy_write,	dataset_name },
1516 	{ zfs_ioc_sendbackup,		zfs_secpolicy_write,	dataset_name },
1517 	{ zfs_ioc_inject_fault,		zfs_secpolicy_inject,	no_name },
1518 	{ zfs_ioc_clear_fault,		zfs_secpolicy_inject,	no_name },
1519 	{ zfs_ioc_inject_list_next,	zfs_secpolicy_inject,	no_name },
1520 	{ zfs_ioc_error_log,		zfs_secpolicy_inject,	pool_name },
1521 	{ zfs_ioc_clear,		zfs_secpolicy_config,	pool_name },
1522 	{ zfs_ioc_promote,		zfs_secpolicy_write,	dataset_name },
1523 	{ zfs_ioc_destroy_snaps,	zfs_secpolicy_write,	dataset_name },
1524 	{ zfs_ioc_snapshot,		zfs_secpolicy_write,	dataset_name },
1525 	{ zfs_ioc_dsobj_to_dsname,	zfs_secpolicy_config,	pool_name },
1526 	{ zfs_ioc_obj_to_path,		zfs_secpolicy_config,	no_name }
1527 };
1528 
1529 static int
1530 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1531 {
1532 	zfs_cmd_t *zc;
1533 	uint_t vec;
1534 	int error, rc;
1535 
1536 	if (getminor(dev) != 0)
1537 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
1538 
1539 	vec = cmd - ZFS_IOC;
1540 
1541 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
1542 		return (EINVAL);
1543 
1544 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
1545 
1546 	error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t));
1547 
1548 	if (error == 0) {
1549 		zc->zc_cred = (uintptr_t)cr;
1550 		zc->zc_dev = dev;
1551 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc->zc_name, cr);
1552 	}
1553 
1554 	/*
1555 	 * Ensure that all pool/dataset names are valid before we pass down to
1556 	 * the lower layers.
1557 	 */
1558 	if (error == 0) {
1559 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
1560 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
1561 		case pool_name:
1562 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
1563 				error = EINVAL;
1564 			break;
1565 
1566 		case dataset_name:
1567 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
1568 				error = EINVAL;
1569 			break;
1570 
1571 		case no_name:
1572 			break;
1573 		}
1574 	}
1575 
1576 	if (error == 0)
1577 		error = zfs_ioc_vec[vec].zvec_func(zc);
1578 
1579 	rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t));
1580 	if (error == 0)
1581 		error = rc;
1582 
1583 	kmem_free(zc, sizeof (zfs_cmd_t));
1584 	return (error);
1585 }
1586 
1587 static int
1588 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1589 {
1590 	if (cmd != DDI_ATTACH)
1591 		return (DDI_FAILURE);
1592 
1593 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
1594 	    DDI_PSEUDO, 0) == DDI_FAILURE)
1595 		return (DDI_FAILURE);
1596 
1597 	zfs_dip = dip;
1598 
1599 	ddi_report_dev(dip);
1600 
1601 	return (DDI_SUCCESS);
1602 }
1603 
1604 static int
1605 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1606 {
1607 	if (spa_busy() || zfs_busy() || zvol_busy())
1608 		return (DDI_FAILURE);
1609 
1610 	if (cmd != DDI_DETACH)
1611 		return (DDI_FAILURE);
1612 
1613 	zfs_dip = NULL;
1614 
1615 	ddi_prop_remove_all(dip);
1616 	ddi_remove_minor_node(dip, NULL);
1617 
1618 	return (DDI_SUCCESS);
1619 }
1620 
1621 /*ARGSUSED*/
1622 static int
1623 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1624 {
1625 	switch (infocmd) {
1626 	case DDI_INFO_DEVT2DEVINFO:
1627 		*result = zfs_dip;
1628 		return (DDI_SUCCESS);
1629 
1630 	case DDI_INFO_DEVT2INSTANCE:
1631 		*result = (void *)0;
1632 		return (DDI_SUCCESS);
1633 	}
1634 
1635 	return (DDI_FAILURE);
1636 }
1637 
1638 /*
1639  * OK, so this is a little weird.
1640  *
1641  * /dev/zfs is the control node, i.e. minor 0.
1642  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
1643  *
1644  * /dev/zfs has basically nothing to do except serve up ioctls,
1645  * so most of the standard driver entry points are in zvol.c.
1646  */
1647 static struct cb_ops zfs_cb_ops = {
1648 	zvol_open,	/* open */
1649 	zvol_close,	/* close */
1650 	zvol_strategy,	/* strategy */
1651 	nodev,		/* print */
1652 	nodev,		/* dump */
1653 	zvol_read,	/* read */
1654 	zvol_write,	/* write */
1655 	zfsdev_ioctl,	/* ioctl */
1656 	nodev,		/* devmap */
1657 	nodev,		/* mmap */
1658 	nodev,		/* segmap */
1659 	nochpoll,	/* poll */
1660 	ddi_prop_op,	/* prop_op */
1661 	NULL,		/* streamtab */
1662 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
1663 	CB_REV,		/* version */
1664 	nodev,		/* async read */
1665 	nodev,		/* async write */
1666 };
1667 
1668 static struct dev_ops zfs_dev_ops = {
1669 	DEVO_REV,	/* version */
1670 	0,		/* refcnt */
1671 	zfs_info,	/* info */
1672 	nulldev,	/* identify */
1673 	nulldev,	/* probe */
1674 	zfs_attach,	/* attach */
1675 	zfs_detach,	/* detach */
1676 	nodev,		/* reset */
1677 	&zfs_cb_ops,	/* driver operations */
1678 	NULL		/* no bus operations */
1679 };
1680 
1681 static struct modldrv zfs_modldrv = {
1682 	&mod_driverops, "ZFS storage pool version " ZFS_VERSION_STRING,
1683 	    &zfs_dev_ops
1684 };
1685 
1686 static struct modlinkage modlinkage = {
1687 	MODREV_1,
1688 	(void *)&zfs_modlfs,
1689 	(void *)&zfs_modldrv,
1690 	NULL
1691 };
1692 
1693 int
1694 _init(void)
1695 {
1696 	int error;
1697 
1698 	spa_init(FREAD | FWRITE);
1699 	zfs_init();
1700 	zvol_init();
1701 
1702 	if ((error = mod_install(&modlinkage)) != 0) {
1703 		zvol_fini();
1704 		zfs_fini();
1705 		spa_fini();
1706 		return (error);
1707 	}
1708 
1709 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
1710 	ASSERT(error == 0);
1711 
1712 	return (0);
1713 }
1714 
1715 int
1716 _fini(void)
1717 {
1718 	int error;
1719 
1720 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
1721 		return (EBUSY);
1722 
1723 	if ((error = mod_remove(&modlinkage)) != 0)
1724 		return (error);
1725 
1726 	zvol_fini();
1727 	zfs_fini();
1728 	spa_fini();
1729 
1730 	ldi_ident_release(zfs_li);
1731 	zfs_li = NULL;
1732 
1733 	return (error);
1734 }
1735 
1736 int
1737 _info(struct modinfo *modinfop)
1738 {
1739 	return (mod_info(&modlinkage, modinfop));
1740 }
1741