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