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