xref: /titanic_51/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 441d80aa4f613b6298fc8bd3151f4be02dbf84fc)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/errno.h>
31 #include <sys/uio.h>
32 #include <sys/buf.h>
33 #include <sys/modctl.h>
34 #include <sys/open.h>
35 #include <sys/file.h>
36 #include <sys/kmem.h>
37 #include <sys/conf.h>
38 #include <sys/cmn_err.h>
39 #include <sys/stat.h>
40 #include <sys/zfs_ioctl.h>
41 #include <sys/zap.h>
42 #include <sys/spa.h>
43 #include <sys/vdev.h>
44 #include <sys/dmu.h>
45 #include <sys/dsl_dir.h>
46 #include <sys/dsl_dataset.h>
47 #include <sys/dsl_prop.h>
48 #include <sys/ddi.h>
49 #include <sys/sunddi.h>
50 #include <sys/sunldi.h>
51 #include <sys/policy.h>
52 #include <sys/zone.h>
53 #include <sys/nvpair.h>
54 #include <sys/pathname.h>
55 #include <sys/mount.h>
56 #include <sys/sdt.h>
57 #include <sys/fs/zfs.h>
58 #include <sys/zfs_ctldir.h>
59 
60 #include "zfs_namecheck.h"
61 
62 extern struct modlfs zfs_modlfs;
63 
64 extern void zfs_init(void);
65 extern void zfs_fini(void);
66 
67 ldi_ident_t zfs_li = NULL;
68 dev_info_t *zfs_dip;
69 
70 typedef int zfs_ioc_func_t(zfs_cmd_t *);
71 typedef int zfs_secpolicy_func_t(const char *, const char *, cred_t *);
72 
73 typedef struct zfs_ioc_vec {
74 	zfs_ioc_func_t		*zvec_func;
75 	zfs_secpolicy_func_t	*zvec_secpolicy;
76 	enum {
77 		no_name,
78 		pool_name,
79 		dataset_name
80 	}			zvec_namecheck;
81 } zfs_ioc_vec_t;
82 
83 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
84 void
85 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
86 {
87 	const char *newfile;
88 	char buf[256];
89 	va_list adx;
90 
91 	/*
92 	 * Get rid of annoying "../common/" prefix to filename.
93 	 */
94 	newfile = strrchr(file, '/');
95 	if (newfile != NULL) {
96 		newfile = newfile + 1; /* Get rid of leading / */
97 	} else {
98 		newfile = file;
99 	}
100 
101 	va_start(adx, fmt);
102 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
103 	va_end(adx);
104 
105 	/*
106 	 * To get this data, use the zfs-dprintf probe as so:
107 	 * dtrace -q -n 'zfs-dprintf \
108 	 *	/stringof(arg0) == "dbuf.c"/ \
109 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
110 	 * arg0 = file name
111 	 * arg1 = function name
112 	 * arg2 = line number
113 	 * arg3 = message
114 	 */
115 	DTRACE_PROBE4(zfs__dprintf,
116 	    char *, newfile, char *, func, int, line, char *, buf);
117 }
118 
119 /*
120  * Policy for top-level read operations (list pools).  Requires no privileges,
121  * and can be used in the local zone, as there is no associated dataset.
122  */
123 /* ARGSUSED */
124 static int
125 zfs_secpolicy_none(const char *unused1, const char *unused2, cred_t *cr)
126 {
127 	return (0);
128 }
129 
130 /*
131  * Policy for dataset read operations (list children, get statistics).  Requires
132  * no privileges, but must be visible in the local zone.
133  */
134 /* ARGSUSED */
135 static int
136 zfs_secpolicy_read(const char *dataset, const char *unused, cred_t *cr)
137 {
138 	if (INGLOBALZONE(curproc) ||
139 	    zone_dataset_visible(dataset, NULL))
140 		return (0);
141 
142 	return (ENOENT);
143 }
144 
145 static int
146 zfs_dozonecheck(const char *dataset, cred_t *cr)
147 {
148 	uint64_t zoned;
149 	int writable = 1;
150 
151 	/*
152 	 * The dataset must be visible by this zone -- check this first
153 	 * so they don't see EPERM on something they shouldn't know about.
154 	 */
155 	if (!INGLOBALZONE(curproc) &&
156 	    !zone_dataset_visible(dataset, &writable))
157 		return (ENOENT);
158 
159 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
160 		return (ENOENT);
161 
162 	if (INGLOBALZONE(curproc)) {
163 		/*
164 		 * If the fs is zoned, only root can access it from the
165 		 * global zone.
166 		 */
167 		if (secpolicy_zfs(cr) && zoned)
168 			return (EPERM);
169 	} else {
170 		/*
171 		 * If we are in a local zone, the 'zoned' property must be set.
172 		 */
173 		if (!zoned)
174 			return (EPERM);
175 
176 		/* must be writable by this zone */
177 		if (!writable)
178 			return (EPERM);
179 	}
180 	return (0);
181 }
182 
183 /*
184  * Policy for dataset write operations (create children, set properties, etc).
185  * Requires SYS_MOUNT privilege, and must be writable in the local zone.
186  */
187 /* ARGSUSED */
188 int
189 zfs_secpolicy_write(const char *dataset, const char *unused, cred_t *cr)
190 {
191 	int error;
192 
193 	if (error = zfs_dozonecheck(dataset, cr))
194 		return (error);
195 
196 	return (secpolicy_zfs(cr));
197 }
198 
199 /*
200  * Policy for operations that want to write a dataset's parent:
201  * create, destroy, snapshot, clone, restore.
202  */
203 static int
204 zfs_secpolicy_parent(const char *dataset, const char *unused, cred_t *cr)
205 {
206 	char parentname[MAXNAMELEN];
207 	char *cp;
208 
209 	/*
210 	 * Remove the @bla or /bla from the end of the name to get the parent.
211 	 */
212 	(void) strncpy(parentname, dataset, sizeof (parentname));
213 	cp = strrchr(parentname, '@');
214 	if (cp != NULL) {
215 		cp[0] = '\0';
216 	} else {
217 		cp = strrchr(parentname, '/');
218 		if (cp == NULL)
219 			return (ENOENT);
220 		cp[0] = '\0';
221 
222 	}
223 
224 	return (zfs_secpolicy_write(parentname, unused, cr));
225 }
226 
227 /*
228  * Policy for dataset write operations (create children, set properties, etc).
229  * Requires SYS_MOUNT privilege, and must be writable in the local zone.
230  */
231 static int
232 zfs_secpolicy_setprop(const char *dataset, const char *prop, cred_t *cr)
233 {
234 	int error;
235 
236 	if (error = zfs_dozonecheck(dataset, cr))
237 		return (error);
238 
239 	if (strcmp(prop, "zoned") == 0) {
240 		/*
241 		 * Disallow setting of 'zoned' from within a local zone.
242 		 */
243 		if (!INGLOBALZONE(curproc))
244 			return (EPERM);
245 	}
246 
247 	return (secpolicy_zfs(cr));
248 }
249 
250 /*
251  * Security policy for setting the quota.  This is the same as
252  * zfs_secpolicy_write, except that the local zone may not change the quota at
253  * the zone-property setpoint.
254  */
255 /* ARGSUSED */
256 static int
257 zfs_secpolicy_quota(const char *dataset, const char *unused, cred_t *cr)
258 {
259 	int error;
260 
261 	if (error = zfs_dozonecheck(dataset, cr))
262 		return (error);
263 
264 	if (!INGLOBALZONE(curproc)) {
265 		uint64_t zoned;
266 		char setpoint[MAXNAMELEN];
267 		int dslen;
268 		/*
269 		 * Unprivileged users are allowed to modify the quota
270 		 * on things *under* (ie. contained by) the thing they
271 		 * own.
272 		 */
273 		if (dsl_prop_get_integer(dataset, "zoned", &zoned, setpoint))
274 			return (EPERM);
275 		if (!zoned) /* this shouldn't happen */
276 			return (EPERM);
277 		dslen = strlen(dataset);
278 		if (dslen <= strlen(setpoint))
279 			return (EPERM);
280 	}
281 
282 	return (secpolicy_zfs(cr));
283 }
284 
285 /*
286  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
287  * SYS_CONFIG privilege, which is not available in a local zone.
288  */
289 /* ARGSUSED */
290 static int
291 zfs_secpolicy_config(const char *unused, const char *unused2, cred_t *cr)
292 {
293 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
294 		return (EPERM);
295 
296 	return (0);
297 }
298 
299 /*
300  * Returns the nvlist as specified by the user in the zfs_cmd_t.
301  */
302 static int
303 get_config(zfs_cmd_t *zc, nvlist_t **nvp)
304 {
305 	char *packed;
306 	size_t size;
307 	int error;
308 	nvlist_t *config = NULL;
309 
310 	/*
311 	 * Read in and unpack the user-supplied nvlist.  By this point, we know
312 	 * that the user has the SYS_CONFIG privilege, so allocating arbitrary
313 	 * sized regions of memory should not be a problem.
314 	 */
315 	if ((size = zc->zc_config_src_size) == 0)
316 		return (EINVAL);
317 
318 	packed = kmem_alloc(size, KM_SLEEP);
319 
320 	if ((error = xcopyin((void *)(uintptr_t)zc->zc_config_src, packed,
321 	    size)) != 0) {
322 		kmem_free(packed, size);
323 		return (error);
324 	}
325 
326 	if ((error = nvlist_unpack(packed, size, &config, 0)) != 0) {
327 		kmem_free(packed, size);
328 		return (error);
329 	}
330 
331 	kmem_free(packed, size);
332 
333 	*nvp = config;
334 	return (0);
335 }
336 
337 static int
338 zfs_ioc_pool_create(zfs_cmd_t *zc)
339 {
340 	int error;
341 	nvlist_t *config;
342 
343 	if ((error = get_config(zc, &config)) != 0)
344 		return (error);
345 
346 	error = spa_create(zc->zc_name, config, zc->zc_root[0] == '\0' ?
347 	    NULL : zc->zc_root);
348 
349 	nvlist_free(config);
350 
351 	return (error);
352 }
353 
354 static int
355 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
356 {
357 	return (spa_destroy(zc->zc_name));
358 }
359 
360 static int
361 zfs_ioc_pool_import(zfs_cmd_t *zc)
362 {
363 	int error;
364 	nvlist_t *config;
365 	uint64_t guid;
366 
367 	if ((error = get_config(zc, &config)) != 0)
368 		return (error);
369 
370 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
371 	    guid != zc->zc_pool_guid)
372 		error = EINVAL;
373 	else
374 		error = spa_import(zc->zc_name, config,
375 		    zc->zc_root[0] == '\0' ? NULL : zc->zc_root);
376 
377 	nvlist_free(config);
378 
379 	return (error);
380 }
381 
382 static int
383 zfs_ioc_pool_export(zfs_cmd_t *zc)
384 {
385 	return (spa_export(zc->zc_name));
386 }
387 
388 static int
389 zfs_ioc_pool_configs(zfs_cmd_t *zc)
390 {
391 	nvlist_t *configs;
392 	char *packed = NULL;
393 	size_t size = 0;
394 	int error;
395 
396 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
397 		return (EEXIST);
398 
399 	VERIFY(nvlist_pack(configs, &packed, &size, NV_ENCODE_NATIVE, 0) == 0);
400 
401 	if (size > zc->zc_config_dst_size)
402 		error = ENOMEM;
403 	else
404 		error = xcopyout(packed, (void *)(uintptr_t)zc->zc_config_dst,
405 		    size);
406 
407 	zc->zc_config_dst_size = size;
408 
409 	kmem_free(packed, size);
410 	nvlist_free(configs);
411 
412 	return (error);
413 }
414 
415 static int
416 zfs_ioc_pool_guid(zfs_cmd_t *zc)
417 {
418 	spa_t *spa;
419 	int error;
420 
421 	error = spa_open(zc->zc_name, &spa, FTAG);
422 	if (error == 0) {
423 		zc->zc_pool_guid = spa_guid(spa);
424 		spa_close(spa, FTAG);
425 	}
426 	return (error);
427 }
428 
429 static int
430 zfs_ioc_pool_stats(zfs_cmd_t *zc)
431 {
432 	nvlist_t *config;
433 	char *packed = NULL;
434 	size_t size = 0;
435 	int error;
436 
437 	error = spa_get_stats(zc->zc_name, &config);
438 
439 	if (config != NULL) {
440 		VERIFY(nvlist_pack(config, &packed, &size,
441 		    NV_ENCODE_NATIVE, 0) == 0);
442 
443 		if (size > zc->zc_config_dst_size)
444 			error = ENOMEM;
445 		else if (xcopyout(packed, (void *)(uintptr_t)zc->zc_config_dst,
446 		    size))
447 			error = EFAULT;
448 
449 		zc->zc_config_dst_size = size;
450 
451 		kmem_free(packed, size);
452 		nvlist_free(config);
453 	} else {
454 		ASSERT(error != 0);
455 	}
456 
457 	return (error);
458 }
459 
460 /*
461  * Try to import the given pool, returning pool stats as appropriate so that
462  * user land knows which devices are available and overall pool health.
463  */
464 static int
465 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
466 {
467 	nvlist_t *tryconfig, *config;
468 	char *packed = NULL;
469 	size_t size = 0;
470 	int error;
471 
472 	if ((error = get_config(zc, &tryconfig)) != 0)
473 		return (error);
474 
475 	config = spa_tryimport(tryconfig);
476 
477 	nvlist_free(tryconfig);
478 
479 	if (config == NULL)
480 		return (EINVAL);
481 
482 	VERIFY(nvlist_pack(config, &packed, &size, NV_ENCODE_NATIVE, 0) == 0);
483 
484 	if (size > zc->zc_config_dst_size)
485 		error = ENOMEM;
486 	else
487 		error = xcopyout(packed, (void *)(uintptr_t)zc->zc_config_dst,
488 		    size);
489 
490 	zc->zc_config_dst_size = size;
491 
492 	kmem_free(packed, size);
493 	nvlist_free(config);
494 
495 	return (error);
496 }
497 
498 static int
499 zfs_ioc_pool_scrub(zfs_cmd_t *zc)
500 {
501 	spa_t *spa;
502 	int error;
503 
504 	error = spa_open(zc->zc_name, &spa, FTAG);
505 	if (error == 0) {
506 		error = spa_scrub(spa, zc->zc_cookie, B_FALSE);
507 		spa_close(spa, FTAG);
508 	}
509 	return (error);
510 }
511 
512 static int
513 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
514 {
515 	spa_t *spa;
516 	int error;
517 
518 	error = spa_open(zc->zc_name, &spa, FTAG);
519 	if (error == 0) {
520 		spa_freeze(spa);
521 		spa_close(spa, FTAG);
522 	}
523 	return (error);
524 }
525 
526 static int
527 zfs_ioc_vdev_add(zfs_cmd_t *zc)
528 {
529 	spa_t *spa;
530 	int error;
531 	nvlist_t *config;
532 
533 	error = spa_open(zc->zc_name, &spa, FTAG);
534 	if (error != 0)
535 		return (error);
536 
537 	if ((error = get_config(zc, &config)) == 0) {
538 		error = spa_vdev_add(spa, config);
539 		nvlist_free(config);
540 	}
541 
542 	spa_close(spa, FTAG);
543 	return (error);
544 }
545 
546 /* ARGSUSED */
547 static int
548 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
549 {
550 	return (ENOTSUP);
551 }
552 
553 static int
554 zfs_ioc_vdev_online(zfs_cmd_t *zc)
555 {
556 	spa_t *spa;
557 	char *path = zc->zc_prop_value;
558 	int error;
559 
560 	error = spa_open(zc->zc_name, &spa, FTAG);
561 	if (error != 0)
562 		return (error);
563 	error = vdev_online(spa, path);
564 	spa_close(spa, FTAG);
565 	return (error);
566 }
567 
568 static int
569 zfs_ioc_vdev_offline(zfs_cmd_t *zc)
570 {
571 	spa_t *spa;
572 	char *path = zc->zc_prop_value;
573 	int istmp = zc->zc_cookie;
574 	int error;
575 
576 	error = spa_open(zc->zc_name, &spa, FTAG);
577 	if (error != 0)
578 		return (error);
579 	error = vdev_offline(spa, path, istmp);
580 	spa_close(spa, FTAG);
581 	return (error);
582 }
583 
584 static int
585 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
586 {
587 	spa_t *spa;
588 	char *path = zc->zc_prop_value;
589 	int replacing = zc->zc_cookie;
590 	nvlist_t *config;
591 	int error;
592 
593 	error = spa_open(zc->zc_name, &spa, FTAG);
594 	if (error != 0)
595 		return (error);
596 
597 	if ((error = get_config(zc, &config)) == 0) {
598 		error = spa_vdev_attach(spa, path, config, replacing);
599 		nvlist_free(config);
600 	}
601 
602 	spa_close(spa, FTAG);
603 	return (error);
604 }
605 
606 static int
607 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
608 {
609 	spa_t *spa;
610 	char *path = zc->zc_prop_value;
611 	int error;
612 
613 	error = spa_open(zc->zc_name, &spa, FTAG);
614 	if (error != 0)
615 		return (error);
616 
617 	error = spa_vdev_detach(spa, path, 0, B_FALSE);
618 
619 	spa_close(spa, FTAG);
620 	return (error);
621 }
622 
623 static int
624 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
625 {
626 	spa_t *spa;
627 	char *path = zc->zc_prop_value;
628 	uint64_t guid = zc->zc_pool_guid;
629 	int error;
630 
631 	error = spa_open(zc->zc_name, &spa, FTAG);
632 	if (error != 0)
633 		return (error);
634 
635 	error = spa_vdev_setpath(spa, guid, path);
636 
637 	spa_close(spa, FTAG);
638 	return (error);
639 }
640 
641 
642 static int
643 zfs_ioc_objset_stats(zfs_cmd_t *zc)
644 {
645 	objset_t *os = NULL;
646 	int error;
647 	nvlist_t *nv;
648 	size_t sz;
649 	char *buf;
650 
651 retry:
652 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
653 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
654 	if (error != 0) {
655 		/*
656 		 * This is ugly: dmu_objset_open() can return EBUSY if
657 		 * the objset is held exclusively. Fortunately this hold is
658 		 * only for a short while, so we retry here.
659 		 * This avoids user code having to handle EBUSY,
660 		 * for example for a "zfs list".
661 		 */
662 		if (error == EBUSY) {
663 			delay(1);
664 			goto retry;
665 		}
666 		return (error);
667 	}
668 
669 	dmu_objset_stats(os, &zc->zc_objset_stats);
670 
671 	if (zc->zc_config_src != NULL &&
672 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
673 		VERIFY(nvlist_size(nv, &sz, NV_ENCODE_NATIVE) == 0);
674 		if (sz > zc->zc_config_src_size) {
675 			zc->zc_config_src_size = sz;
676 			error = ENOMEM;
677 		} else {
678 			buf = kmem_alloc(sz, KM_SLEEP);
679 			VERIFY(nvlist_pack(nv, &buf, &sz,
680 			    NV_ENCODE_NATIVE, 0) == 0);
681 			error = xcopyout(buf,
682 			    (void *)(uintptr_t)zc->zc_config_src, sz);
683 			kmem_free(buf, sz);
684 		}
685 		nvlist_free(nv);
686 	}
687 
688 	if (!error && zc->zc_objset_stats.dds_type == DMU_OST_ZVOL)
689 		error = zvol_get_stats(zc, os);
690 
691 	dmu_objset_close(os);
692 	return (error);
693 }
694 
695 static int
696 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
697 {
698 	objset_t *os;
699 	int error;
700 	char *p;
701 
702 retry:
703 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
704 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
705 	if (error != 0) {
706 		/*
707 		 * This is ugly: dmu_objset_open() can return EBUSY if
708 		 * the objset is held exclusively. Fortunately this hold is
709 		 * only for a short while, so we retry here.
710 		 * This avoids user code having to handle EBUSY,
711 		 * for example for a "zfs list".
712 		 */
713 		if (error == EBUSY) {
714 			delay(1);
715 			goto retry;
716 		}
717 		if (error == ENOENT)
718 			error = ESRCH;
719 		return (error);
720 	}
721 
722 	p = strrchr(zc->zc_name, '/');
723 	if (p == NULL || p[1] != '\0')
724 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
725 	p = zc->zc_name + strlen(zc->zc_name);
726 
727 	do {
728 		error = dmu_dir_list_next(os,
729 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
730 		    NULL, &zc->zc_cookie);
731 		if (error == ENOENT)
732 			error = ESRCH;
733 	} while (error == 0 && !INGLOBALZONE(curproc) &&
734 	    !zone_dataset_visible(zc->zc_name, NULL));
735 
736 	/*
737 	 * If it's a hidden dataset (ie. with a '$' in its name), don't
738 	 * try to get stats for it.  Userland will skip over it.
739 	 */
740 	if (error == 0 && strchr(zc->zc_name, '$') == NULL)
741 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
742 
743 	dmu_objset_close(os);
744 	return (error);
745 }
746 
747 static int
748 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
749 {
750 	objset_t *os;
751 	int error;
752 
753 retry:
754 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
755 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
756 	if (error != 0) {
757 		/*
758 		 * This is ugly: dmu_objset_open() can return EBUSY if
759 		 * the objset is held exclusively. Fortunately this hold is
760 		 * only for a short while, so we retry here.
761 		 * This avoids user code having to handle EBUSY,
762 		 * for example for a "zfs list".
763 		 */
764 		if (error == EBUSY) {
765 			delay(1);
766 			goto retry;
767 		}
768 		if (error == ENOENT)
769 			error = ESRCH;
770 		return (error);
771 	}
772 
773 	/*
774 	 * A dataset name of maximum length cannot have any snapshots,
775 	 * so exit immediately.
776 	 */
777 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
778 		dmu_objset_close(os);
779 		return (ESRCH);
780 	}
781 
782 	error = dmu_snapshot_list_next(os,
783 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
784 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie);
785 	if (error == ENOENT)
786 		error = ESRCH;
787 
788 	if (error == 0)
789 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
790 
791 	dmu_objset_close(os);
792 	return (error);
793 }
794 
795 static int
796 zfs_ioc_set_prop(zfs_cmd_t *zc)
797 {
798 	return (dsl_prop_set(zc->zc_name, zc->zc_prop_name,
799 	    zc->zc_intsz, zc->zc_numints, zc->zc_prop_value));
800 }
801 
802 static int
803 zfs_ioc_set_quota(zfs_cmd_t *zc)
804 {
805 	return (dsl_dir_set_quota(zc->zc_name, zc->zc_cookie));
806 }
807 
808 static int
809 zfs_ioc_set_reservation(zfs_cmd_t *zc)
810 {
811 	return (dsl_dir_set_reservation(zc->zc_name, zc->zc_cookie));
812 }
813 
814 static int
815 zfs_ioc_set_volsize(zfs_cmd_t *zc)
816 {
817 	return (zvol_set_volsize(zc));
818 }
819 
820 static int
821 zfs_ioc_set_volblocksize(zfs_cmd_t *zc)
822 {
823 	return (zvol_set_volblocksize(zc));
824 }
825 
826 static int
827 zfs_ioc_create_minor(zfs_cmd_t *zc)
828 {
829 	return (zvol_create_minor(zc));
830 }
831 
832 static int
833 zfs_ioc_remove_minor(zfs_cmd_t *zc)
834 {
835 	return (zvol_remove_minor(zc));
836 }
837 
838 /*
839  * Search the vfs list for a specified resource.  Returns a pointer to it
840  * or NULL if no suitable entry is found. The caller of this routine
841  * is responsible for releasing the returned vfs pointer.
842  */
843 static vfs_t *
844 zfs_get_vfs(const char *resource)
845 {
846 	struct vfs *vfsp;
847 	struct vfs *vfs_found = NULL;
848 
849 	vfs_list_read_lock();
850 	vfsp = rootvfs;
851 	do {
852 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
853 			VFS_HOLD(vfsp);
854 			vfs_found = vfsp;
855 			break;
856 		}
857 		vfsp = vfsp->vfs_next;
858 	} while (vfsp != rootvfs);
859 	vfs_list_unlock();
860 	return (vfs_found);
861 }
862 
863 static void
864 zfs_create_cb(objset_t *os, void *arg, dmu_tx_t *tx)
865 {
866 	zfs_cmd_t *zc = arg;
867 	zfs_create_fs(os, (cred_t *)(uintptr_t)zc->zc_cred, tx);
868 }
869 
870 static int
871 zfs_ioc_create(zfs_cmd_t *zc)
872 {
873 	objset_t *clone;
874 	int error = 0;
875 	void (*cbfunc)(objset_t *os, void *arg, dmu_tx_t *tx);
876 	dmu_objset_type_t type = zc->zc_objset_type;
877 
878 	switch (type) {
879 
880 	case DMU_OST_ZFS:
881 		cbfunc = zfs_create_cb;
882 		break;
883 
884 	case DMU_OST_ZVOL:
885 		cbfunc = zvol_create_cb;
886 		break;
887 
888 	default:
889 		return (EINVAL);
890 	}
891 
892 	if (zc->zc_filename[0] != '\0') {
893 		/*
894 		 * We're creating a clone of an existing snapshot.
895 		 */
896 		zc->zc_filename[sizeof (zc->zc_filename) - 1] = '\0';
897 		if (dataset_namecheck(zc->zc_filename, NULL, NULL) != 0)
898 			return (EINVAL);
899 
900 		error = dmu_objset_open(zc->zc_filename, type,
901 		    DS_MODE_STANDARD | DS_MODE_READONLY, &clone);
902 		if (error)
903 			return (error);
904 		error = dmu_objset_create(zc->zc_name, type, clone, NULL, NULL);
905 		dmu_objset_close(clone);
906 	} else if (strchr(zc->zc_name, '@') != 0) {
907 		/*
908 		 * We're taking a snapshot of an existing dataset.
909 		 */
910 		error = dmu_objset_create(zc->zc_name, type, NULL, NULL, NULL);
911 	} else {
912 		/*
913 		 * We're creating a new dataset.
914 		 */
915 		if (type == DMU_OST_ZVOL) {
916 
917 			if ((error = zvol_check_volblocksize(zc)) != 0)
918 				return (error);
919 
920 			if ((error = zvol_check_volsize(zc,
921 			    zc->zc_volblocksize)) != 0)
922 				return (error);
923 		}
924 		error = dmu_objset_create(zc->zc_name, type, NULL, cbfunc, zc);
925 	}
926 	return (error);
927 }
928 
929 static int
930 zfs_ioc_destroy(zfs_cmd_t *zc)
931 {
932 	if (strchr(zc->zc_name, '@') != NULL &&
933 	    zc->zc_objset_type == DMU_OST_ZFS) {
934 		vfs_t *vfsp;
935 		int err;
936 
937 		/*
938 		 * Snapshots under .zfs control must be unmounted
939 		 * before they can be destroyed.
940 		 */
941 		if ((vfsp = zfs_get_vfs(zc->zc_name)) != NULL) {
942 			/*
943 			 * Always force the unmount for snapshots.
944 			 */
945 			int flag = MS_FORCE;
946 
947 			if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
948 				VFS_RELE(vfsp);
949 				return (err);
950 			}
951 			VFS_RELE(vfsp);
952 			if ((err = dounmount(vfsp, flag, kcred)) != 0)
953 				return (err);
954 		}
955 	}
956 
957 	return (dmu_objset_destroy(zc->zc_name));
958 }
959 
960 static int
961 zfs_ioc_rollback(zfs_cmd_t *zc)
962 {
963 	return (dmu_objset_rollback(zc->zc_name));
964 }
965 
966 static int
967 zfs_ioc_rename(zfs_cmd_t *zc)
968 {
969 	zc->zc_prop_value[sizeof (zc->zc_prop_value) - 1] = '\0';
970 	if (dataset_namecheck(zc->zc_prop_value, NULL, NULL) != 0)
971 		return (EINVAL);
972 
973 	if (strchr(zc->zc_name, '@') != NULL &&
974 	    zc->zc_objset_type == DMU_OST_ZFS) {
975 		vfs_t *vfsp;
976 		int err;
977 
978 		/*
979 		 * Snapshots under .zfs control must be unmounted
980 		 * before they can be renamed.
981 		 */
982 		if ((vfsp = zfs_get_vfs(zc->zc_name)) != NULL) {
983 			/*
984 			 * Always force the unmount for snapshots.
985 			 */
986 			int flag = MS_FORCE;
987 
988 			if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
989 				VFS_RELE(vfsp);
990 				return (err);
991 			}
992 			VFS_RELE(vfsp);
993 			if ((err = dounmount(vfsp, flag, kcred)) != 0)
994 				return (err);
995 		}
996 	}
997 
998 	return (dmu_objset_rename(zc->zc_name, zc->zc_prop_value));
999 }
1000 
1001 static int
1002 zfs_ioc_recvbackup(zfs_cmd_t *zc)
1003 {
1004 	file_t *fp;
1005 	int error, fd;
1006 
1007 	fd = zc->zc_cookie;
1008 	fp = getf(fd);
1009 	if (fp == NULL)
1010 		return (EBADF);
1011 	error = dmu_recvbackup(&zc->zc_begin_record, &zc->zc_cookie,
1012 	    fp->f_vnode, fp->f_offset);
1013 	releasef(fd);
1014 	return (error);
1015 }
1016 
1017 static int
1018 zfs_ioc_sendbackup(zfs_cmd_t *zc)
1019 {
1020 	objset_t *fromsnap = NULL;
1021 	objset_t *tosnap;
1022 	file_t *fp;
1023 	int error;
1024 
1025 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1026 	    DS_MODE_STANDARD | DS_MODE_READONLY, &tosnap);
1027 	if (error)
1028 		return (error);
1029 
1030 	if (zc->zc_prop_value[0] != '\0') {
1031 		error = dmu_objset_open(zc->zc_prop_value, DMU_OST_ANY,
1032 		    DS_MODE_STANDARD | DS_MODE_READONLY, &fromsnap);
1033 		if (error) {
1034 			dmu_objset_close(tosnap);
1035 			return (error);
1036 		}
1037 	}
1038 
1039 	fp = getf(zc->zc_cookie);
1040 	if (fp == NULL) {
1041 		dmu_objset_close(tosnap);
1042 		if (fromsnap)
1043 			dmu_objset_close(fromsnap);
1044 		return (EBADF);
1045 	}
1046 
1047 	error = dmu_sendbackup(tosnap, fromsnap, fp->f_vnode);
1048 
1049 	releasef(zc->zc_cookie);
1050 	if (fromsnap)
1051 		dmu_objset_close(fromsnap);
1052 	dmu_objset_close(tosnap);
1053 	return (error);
1054 }
1055 
1056 static zfs_ioc_vec_t zfs_ioc_vec[] = {
1057 	{ zfs_ioc_pool_create,		zfs_secpolicy_config,	pool_name },
1058 	{ zfs_ioc_pool_destroy,		zfs_secpolicy_config,	pool_name },
1059 	{ zfs_ioc_pool_import,		zfs_secpolicy_config,	pool_name },
1060 	{ zfs_ioc_pool_export,		zfs_secpolicy_config,	pool_name },
1061 	{ zfs_ioc_pool_configs,		zfs_secpolicy_none,	no_name },
1062 	{ zfs_ioc_pool_guid,		zfs_secpolicy_read,	pool_name },
1063 	{ zfs_ioc_pool_stats,		zfs_secpolicy_read,	pool_name },
1064 	{ zfs_ioc_pool_tryimport,	zfs_secpolicy_config,	no_name },
1065 	{ zfs_ioc_pool_scrub,		zfs_secpolicy_config,	pool_name },
1066 	{ zfs_ioc_pool_freeze,		zfs_secpolicy_config,	no_name },
1067 	{ zfs_ioc_vdev_add,		zfs_secpolicy_config,	pool_name },
1068 	{ zfs_ioc_vdev_remove,		zfs_secpolicy_config,	pool_name },
1069 	{ zfs_ioc_vdev_online,		zfs_secpolicy_config,	pool_name },
1070 	{ zfs_ioc_vdev_offline,		zfs_secpolicy_config,	pool_name },
1071 	{ zfs_ioc_vdev_attach,		zfs_secpolicy_config,	pool_name },
1072 	{ zfs_ioc_vdev_detach,		zfs_secpolicy_config,	pool_name },
1073 	{ zfs_ioc_vdev_setpath,		zfs_secpolicy_config,	pool_name },
1074 	{ zfs_ioc_objset_stats,		zfs_secpolicy_read,	dataset_name },
1075 	{ zfs_ioc_dataset_list_next,	zfs_secpolicy_read,	dataset_name },
1076 	{ zfs_ioc_snapshot_list_next,	zfs_secpolicy_read,	dataset_name },
1077 	{ zfs_ioc_set_prop,		zfs_secpolicy_setprop,	dataset_name },
1078 	{ zfs_ioc_set_quota,		zfs_secpolicy_quota,	dataset_name },
1079 	{ zfs_ioc_set_reservation,	zfs_secpolicy_write,	dataset_name },
1080 	{ zfs_ioc_set_volsize,		zfs_secpolicy_config,	dataset_name },
1081 	{ zfs_ioc_set_volblocksize,	zfs_secpolicy_config,	dataset_name },
1082 	{ zfs_ioc_create_minor,		zfs_secpolicy_config,	dataset_name },
1083 	{ zfs_ioc_remove_minor,		zfs_secpolicy_config,	dataset_name },
1084 	{ zfs_ioc_create,		zfs_secpolicy_parent,	dataset_name },
1085 	{ zfs_ioc_destroy,		zfs_secpolicy_parent,	dataset_name },
1086 	{ zfs_ioc_rollback,		zfs_secpolicy_write,	dataset_name },
1087 	{ zfs_ioc_rename,		zfs_secpolicy_write,	dataset_name },
1088 	{ zfs_ioc_recvbackup,		zfs_secpolicy_write,	dataset_name },
1089 	{ zfs_ioc_sendbackup,		zfs_secpolicy_write,	dataset_name },
1090 };
1091 
1092 static int
1093 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1094 {
1095 	zfs_cmd_t *zc;
1096 	uint_t vec;
1097 	int error;
1098 
1099 	if (getminor(dev) != 0)
1100 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
1101 
1102 	vec = cmd - ZFS_IOC;
1103 
1104 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
1105 		return (EINVAL);
1106 
1107 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
1108 
1109 	error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t));
1110 
1111 	if (error == 0) {
1112 		zc->zc_cred = (uintptr_t)cr;
1113 		zc->zc_dev = dev;
1114 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc->zc_name,
1115 		    zc->zc_prop_name, cr);
1116 	}
1117 
1118 	/*
1119 	 * Ensure that all pool/dataset names are valid before we pass down to
1120 	 * the lower layers.
1121 	 */
1122 	if (error == 0) {
1123 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
1124 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
1125 		case pool_name:
1126 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
1127 				error = EINVAL;
1128 			break;
1129 
1130 		case dataset_name:
1131 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
1132 				error = EINVAL;
1133 			break;
1134 		}
1135 	}
1136 
1137 	if (error == 0)
1138 		error = zfs_ioc_vec[vec].zvec_func(zc);
1139 
1140 	if (error == 0 || error == ENOMEM) {
1141 		int rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t));
1142 		if (error == 0)
1143 			error = rc;
1144 	}
1145 
1146 	kmem_free(zc, sizeof (zfs_cmd_t));
1147 	return (error);
1148 }
1149 
1150 static int
1151 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1152 {
1153 	if (cmd != DDI_ATTACH)
1154 		return (DDI_FAILURE);
1155 
1156 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
1157 	    DDI_PSEUDO, 0) == DDI_FAILURE)
1158 		return (DDI_FAILURE);
1159 
1160 	zfs_dip = dip;
1161 
1162 	ddi_report_dev(dip);
1163 
1164 	return (DDI_SUCCESS);
1165 }
1166 
1167 static int
1168 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1169 {
1170 	if (spa_busy() || zfs_busy() || zvol_busy())
1171 		return (DDI_FAILURE);
1172 
1173 	if (cmd != DDI_DETACH)
1174 		return (DDI_FAILURE);
1175 
1176 	zfs_dip = NULL;
1177 
1178 	ddi_prop_remove_all(dip);
1179 	ddi_remove_minor_node(dip, NULL);
1180 
1181 	return (DDI_SUCCESS);
1182 }
1183 
1184 /*ARGSUSED*/
1185 static int
1186 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1187 {
1188 	switch (infocmd) {
1189 	case DDI_INFO_DEVT2DEVINFO:
1190 		*result = zfs_dip;
1191 		return (DDI_SUCCESS);
1192 
1193 	case DDI_INFO_DEVT2INSTANCE:
1194 		*result = (void *)0;
1195 		return (DDI_SUCCESS);
1196 	}
1197 
1198 	return (DDI_FAILURE);
1199 }
1200 
1201 /*
1202  * OK, so this is a little weird.
1203  *
1204  * /dev/zfs is the control node, i.e. minor 0.
1205  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
1206  *
1207  * /dev/zfs has basically nothing to do except serve up ioctls,
1208  * so most of the standard driver entry points are in zvol.c.
1209  */
1210 static struct cb_ops zfs_cb_ops = {
1211 	zvol_open,	/* open */
1212 	zvol_close,	/* close */
1213 	zvol_strategy,	/* strategy */
1214 	nodev,		/* print */
1215 	nodev,		/* dump */
1216 	zvol_read,	/* read */
1217 	zvol_write,	/* write */
1218 	zfsdev_ioctl,	/* ioctl */
1219 	nodev,		/* devmap */
1220 	nodev,		/* mmap */
1221 	nodev,		/* segmap */
1222 	nochpoll,	/* poll */
1223 	ddi_prop_op,	/* prop_op */
1224 	NULL,		/* streamtab */
1225 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
1226 	CB_REV,		/* version */
1227 	zvol_aread,	/* async read */
1228 	zvol_awrite,	/* async write */
1229 };
1230 
1231 static struct dev_ops zfs_dev_ops = {
1232 	DEVO_REV,	/* version */
1233 	0,		/* refcnt */
1234 	zfs_info,	/* info */
1235 	nulldev,	/* identify */
1236 	nulldev,	/* probe */
1237 	zfs_attach,	/* attach */
1238 	zfs_detach,	/* detach */
1239 	nodev,		/* reset */
1240 	&zfs_cb_ops,	/* driver operations */
1241 	NULL		/* no bus operations */
1242 };
1243 
1244 static struct modldrv zfs_modldrv = {
1245 	&mod_driverops, "ZFS storage pool version 1", &zfs_dev_ops
1246 };
1247 
1248 static struct modlinkage modlinkage = {
1249 	MODREV_1,
1250 	(void *)&zfs_modlfs,
1251 	(void *)&zfs_modldrv,
1252 	NULL
1253 };
1254 
1255 int
1256 _init(void)
1257 {
1258 	int error;
1259 
1260 	spa_init(FREAD | FWRITE);
1261 	zfs_init();
1262 	zvol_init();
1263 
1264 	if ((error = mod_install(&modlinkage)) != 0) {
1265 		zvol_fini();
1266 		zfs_fini();
1267 		spa_fini();
1268 		return (error);
1269 	}
1270 
1271 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
1272 	ASSERT(error == 0);
1273 
1274 	return (0);
1275 }
1276 
1277 int
1278 _fini(void)
1279 {
1280 	int error;
1281 
1282 	if (spa_busy() || zfs_busy() || zvol_busy())
1283 		return (EBUSY);
1284 
1285 	if ((error = mod_remove(&modlinkage)) != 0)
1286 		return (error);
1287 
1288 	zvol_fini();
1289 	zfs_fini();
1290 	spa_fini();
1291 
1292 	ldi_ident_release(zfs_li);
1293 	zfs_li = NULL;
1294 
1295 	return (error);
1296 }
1297 
1298 int
1299 _info(struct modinfo *modinfop)
1300 {
1301 	return (mod_info(&modlinkage, modinfop));
1302 }
1303