xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 893a6d32980d24be1349478f44169009d4801c25)
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/systm.h>
31 #include <sys/sysmacros.h>
32 #include <sys/kmem.h>
33 #include <sys/pathname.h>
34 #include <sys/acl.h>
35 #include <sys/vnode.h>
36 #include <sys/vfs.h>
37 #include <sys/mntent.h>
38 #include <sys/mount.h>
39 #include <sys/cmn_err.h>
40 #include "fs/fs_subr.h"
41 #include <sys/zfs_znode.h>
42 #include <sys/zfs_dir.h>
43 #include <sys/zil.h>
44 #include <sys/fs/zfs.h>
45 #include <sys/dmu.h>
46 #include <sys/dsl_prop.h>
47 #include <sys/spa.h>
48 #include <sys/zap.h>
49 #include <sys/varargs.h>
50 #include <sys/policy.h>
51 #include <sys/atomic.h>
52 #include <sys/mkdev.h>
53 #include <sys/modctl.h>
54 #include <sys/zfs_ioctl.h>
55 #include <sys/zfs_ctldir.h>
56 #include <sys/bootconf.h>
57 #include <sys/sunddi.h>
58 #include <sys/dnlc.h>
59 
60 int zfsfstype;
61 vfsops_t *zfs_vfsops = NULL;
62 static major_t zfs_major;
63 static minor_t zfs_minor;
64 static kmutex_t	zfs_dev_mtx;
65 
66 extern char zfs_bootpath[BO_MAXOBJNAME];
67 
68 static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
69 static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr);
70 static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
71 static int zfs_root(vfs_t *vfsp, vnode_t **vpp);
72 static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp);
73 static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp);
74 static void zfs_freevfs(vfs_t *vfsp);
75 static void zfs_objset_close(zfsvfs_t *zfsvfs);
76 
77 static const fs_operation_def_t zfs_vfsops_template[] = {
78 	VFSNAME_MOUNT, zfs_mount,
79 	VFSNAME_MOUNTROOT, zfs_mountroot,
80 	VFSNAME_UNMOUNT, zfs_umount,
81 	VFSNAME_ROOT, zfs_root,
82 	VFSNAME_STATVFS, zfs_statvfs,
83 	VFSNAME_SYNC, (fs_generic_func_p) zfs_sync,
84 	VFSNAME_VGET, zfs_vget,
85 	VFSNAME_FREEVFS, (fs_generic_func_p) zfs_freevfs,
86 	NULL, NULL
87 };
88 
89 static const fs_operation_def_t zfs_vfsops_eio_template[] = {
90 	VFSNAME_FREEVFS, (fs_generic_func_p) zfs_freevfs,
91 	NULL, NULL
92 };
93 
94 /*
95  * We need to keep a count of active fs's.
96  * This is necessary to prevent our module
97  * from being unloaded after a umount -f
98  */
99 static uint32_t	zfs_active_fs_count = 0;
100 
101 static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
102 static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
103 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
104 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
105 
106 /*
107  * MNTOPT_DEFAULT was removed from MNTOPT_XATTR, since the
108  * default value is now determined by the xattr property.
109  */
110 static mntopt_t mntopts[] = {
111 	{ MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL },
112 	{ MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL },
113 	{ MNTOPT_NOATIME, noatime_cancel, NULL, MO_DEFAULT, NULL },
114 	{ MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
115 };
116 
117 static mntopts_t zfs_mntopts = {
118 	sizeof (mntopts) / sizeof (mntopt_t),
119 	mntopts
120 };
121 
122 /*ARGSUSED*/
123 int
124 zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
125 {
126 	/*
127 	 * Data integrity is job one.  We don't want a compromised kernel
128 	 * writing to the storage pool, so we never sync during panic.
129 	 */
130 	if (panicstr)
131 		return (0);
132 
133 	/*
134 	 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
135 	 * to sync metadata, which they would otherwise cache indefinitely.
136 	 * Semantically, the only requirement is that the sync be initiated.
137 	 * The DMU syncs out txgs frequently, so there's nothing to do.
138 	 */
139 	if (flag & SYNC_ATTR)
140 		return (0);
141 
142 	if (vfsp != NULL) {
143 		/*
144 		 * Sync a specific filesystem.
145 		 */
146 		zfsvfs_t *zfsvfs = vfsp->vfs_data;
147 
148 		ZFS_ENTER(zfsvfs);
149 		if (zfsvfs->z_log != NULL)
150 			zil_commit(zfsvfs->z_log, UINT64_MAX, 0);
151 		else
152 			txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
153 		ZFS_EXIT(zfsvfs);
154 	} else {
155 		/*
156 		 * Sync all ZFS filesystems.  This is what happens when you
157 		 * run sync(1M).  Unlike other filesystems, ZFS honors the
158 		 * request by waiting for all pools to commit all dirty data.
159 		 */
160 		spa_sync_allpools();
161 	}
162 
163 	return (0);
164 }
165 
166 static int
167 zfs_create_unique_device(dev_t *dev)
168 {
169 	major_t new_major;
170 
171 	do {
172 		ASSERT3U(zfs_minor, <=, MAXMIN32);
173 		minor_t start = zfs_minor;
174 		do {
175 			mutex_enter(&zfs_dev_mtx);
176 			if (zfs_minor >= MAXMIN32) {
177 				/*
178 				 * If we're still using the real major
179 				 * keep out of /dev/zfs and /dev/zvol minor
180 				 * number space.  If we're using a getudev()'ed
181 				 * major number, we can use all of its minors.
182 				 */
183 				if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
184 					zfs_minor = ZFS_MIN_MINOR;
185 				else
186 					zfs_minor = 0;
187 			} else {
188 				zfs_minor++;
189 			}
190 			*dev = makedevice(zfs_major, zfs_minor);
191 			mutex_exit(&zfs_dev_mtx);
192 		} while (vfs_devismounted(*dev) && zfs_minor != start);
193 		if (zfs_minor == start) {
194 			/*
195 			 * We are using all ~262,000 minor numbers for the
196 			 * current major number.  Create a new major number.
197 			 */
198 			if ((new_major = getudev()) == (major_t)-1) {
199 				cmn_err(CE_WARN,
200 				    "zfs_mount: Can't get unique major "
201 				    "device number.");
202 				return (-1);
203 			}
204 			mutex_enter(&zfs_dev_mtx);
205 			zfs_major = new_major;
206 			zfs_minor = 0;
207 
208 			mutex_exit(&zfs_dev_mtx);
209 		} else {
210 			break;
211 		}
212 		/* CONSTANTCONDITION */
213 	} while (1);
214 
215 	return (0);
216 }
217 
218 static void
219 atime_changed_cb(void *arg, uint64_t newval)
220 {
221 	zfsvfs_t *zfsvfs = arg;
222 
223 	if (newval == TRUE) {
224 		zfsvfs->z_atime = TRUE;
225 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
226 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
227 	} else {
228 		zfsvfs->z_atime = FALSE;
229 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
230 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
231 	}
232 }
233 
234 static void
235 xattr_changed_cb(void *arg, uint64_t newval)
236 {
237 	zfsvfs_t *zfsvfs = arg;
238 
239 	if (newval == TRUE) {
240 		/* XXX locking on vfs_flag? */
241 		zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
242 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
243 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
244 	} else {
245 		/* XXX locking on vfs_flag? */
246 		zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
247 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
248 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
249 	}
250 }
251 
252 static void
253 blksz_changed_cb(void *arg, uint64_t newval)
254 {
255 	zfsvfs_t *zfsvfs = arg;
256 
257 	if (newval < SPA_MINBLOCKSIZE ||
258 	    newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
259 		newval = SPA_MAXBLOCKSIZE;
260 
261 	zfsvfs->z_max_blksz = newval;
262 	zfsvfs->z_vfs->vfs_bsize = newval;
263 }
264 
265 static void
266 readonly_changed_cb(void *arg, uint64_t newval)
267 {
268 	zfsvfs_t *zfsvfs = arg;
269 
270 	if (newval) {
271 		/* XXX locking on vfs_flag? */
272 		zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
273 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
274 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
275 	} else {
276 		/* XXX locking on vfs_flag? */
277 		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
278 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
279 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
280 	}
281 }
282 
283 static void
284 devices_changed_cb(void *arg, uint64_t newval)
285 {
286 	zfsvfs_t *zfsvfs = arg;
287 
288 	if (newval == FALSE) {
289 		zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
290 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
291 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
292 	} else {
293 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
294 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
295 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
296 	}
297 }
298 
299 static void
300 setuid_changed_cb(void *arg, uint64_t newval)
301 {
302 	zfsvfs_t *zfsvfs = arg;
303 
304 	if (newval == FALSE) {
305 		zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
306 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
307 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
308 	} else {
309 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
310 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
311 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
312 	}
313 }
314 
315 static void
316 exec_changed_cb(void *arg, uint64_t newval)
317 {
318 	zfsvfs_t *zfsvfs = arg;
319 
320 	if (newval == FALSE) {
321 		zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
322 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
323 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
324 	} else {
325 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
326 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
327 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
328 	}
329 }
330 
331 static void
332 snapdir_changed_cb(void *arg, uint64_t newval)
333 {
334 	zfsvfs_t *zfsvfs = arg;
335 
336 	zfsvfs->z_show_ctldir = newval;
337 }
338 
339 static void
340 acl_mode_changed_cb(void *arg, uint64_t newval)
341 {
342 	zfsvfs_t *zfsvfs = arg;
343 
344 	zfsvfs->z_acl_mode = newval;
345 }
346 
347 static void
348 acl_inherit_changed_cb(void *arg, uint64_t newval)
349 {
350 	zfsvfs_t *zfsvfs = arg;
351 
352 	zfsvfs->z_acl_inherit = newval;
353 }
354 
355 static int
356 zfs_refresh_properties(vfs_t *vfsp)
357 {
358 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
359 
360 	/*
361 	 * Remount operations default to "rw" unless "ro" is explicitly
362 	 * specified.
363 	 */
364 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
365 		readonly_changed_cb(zfsvfs, B_TRUE);
366 	} else {
367 		if (!dmu_objset_is_snapshot(zfsvfs->z_os))
368 			readonly_changed_cb(zfsvfs, B_FALSE);
369 		else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL))
370 			    return (EROFS);
371 	}
372 
373 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
374 		devices_changed_cb(zfsvfs, B_FALSE);
375 		setuid_changed_cb(zfsvfs, B_FALSE);
376 	} else {
377 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
378 			devices_changed_cb(zfsvfs, B_FALSE);
379 		else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL))
380 			devices_changed_cb(zfsvfs, B_TRUE);
381 
382 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))
383 			setuid_changed_cb(zfsvfs, B_FALSE);
384 		else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL))
385 			setuid_changed_cb(zfsvfs, B_TRUE);
386 	}
387 
388 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL))
389 		exec_changed_cb(zfsvfs, B_FALSE);
390 	else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL))
391 		exec_changed_cb(zfsvfs, B_TRUE);
392 
393 	if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL))
394 		atime_changed_cb(zfsvfs, B_TRUE);
395 	else if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL))
396 		atime_changed_cb(zfsvfs, B_FALSE);
397 
398 	if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL))
399 		xattr_changed_cb(zfsvfs, B_TRUE);
400 	else if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL))
401 		xattr_changed_cb(zfsvfs, B_FALSE);
402 
403 	return (0);
404 }
405 
406 static int
407 zfs_register_callbacks(vfs_t *vfsp)
408 {
409 	struct dsl_dataset *ds = NULL;
410 	objset_t *os = NULL;
411 	zfsvfs_t *zfsvfs = NULL;
412 	int readonly, do_readonly = FALSE;
413 	int setuid, do_setuid = FALSE;
414 	int exec, do_exec = FALSE;
415 	int devices, do_devices = FALSE;
416 	int xattr, do_xattr = FALSE;
417 	int error = 0;
418 
419 	ASSERT(vfsp);
420 	zfsvfs = vfsp->vfs_data;
421 	ASSERT(zfsvfs);
422 	os = zfsvfs->z_os;
423 
424 	/*
425 	 * The act of registering our callbacks will destroy any mount
426 	 * options we may have.  In order to enable temporary overrides
427 	 * of mount options, we stash away the current values and
428 	 * restore them after we register the callbacks.
429 	 */
430 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
431 		readonly = B_TRUE;
432 		do_readonly = B_TRUE;
433 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
434 		readonly = B_FALSE;
435 		do_readonly = B_TRUE;
436 	}
437 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
438 		devices = B_FALSE;
439 		setuid = B_FALSE;
440 		do_devices = B_TRUE;
441 		do_setuid = B_TRUE;
442 	} else {
443 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
444 			devices = B_FALSE;
445 			do_devices = B_TRUE;
446 		} else if (vfs_optionisset(vfsp,
447 			    MNTOPT_DEVICES, NULL)) {
448 			devices = B_TRUE;
449 			do_devices = B_TRUE;
450 		}
451 
452 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
453 			setuid = B_FALSE;
454 			do_setuid = B_TRUE;
455 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
456 			setuid = B_TRUE;
457 			do_setuid = B_TRUE;
458 		}
459 	}
460 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
461 		exec = B_FALSE;
462 		do_exec = B_TRUE;
463 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
464 		exec = B_TRUE;
465 		do_exec = B_TRUE;
466 	}
467 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
468 		xattr = B_FALSE;
469 		do_xattr = B_TRUE;
470 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
471 		xattr = B_TRUE;
472 		do_xattr = B_TRUE;
473 	}
474 
475 	/*
476 	 * Register property callbacks.
477 	 *
478 	 * It would probably be fine to just check for i/o error from
479 	 * the first prop_register(), but I guess I like to go
480 	 * overboard...
481 	 */
482 	ds = dmu_objset_ds(os);
483 	error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
484 	error = error ? error : dsl_prop_register(ds,
485 	    "xattr", xattr_changed_cb, zfsvfs);
486 	error = error ? error : dsl_prop_register(ds,
487 	    "recordsize", blksz_changed_cb, zfsvfs);
488 	error = error ? error : dsl_prop_register(ds,
489 	    "readonly", readonly_changed_cb, zfsvfs);
490 	error = error ? error : dsl_prop_register(ds,
491 	    "devices", devices_changed_cb, zfsvfs);
492 	error = error ? error : dsl_prop_register(ds,
493 	    "setuid", setuid_changed_cb, zfsvfs);
494 	error = error ? error : dsl_prop_register(ds,
495 	    "exec", exec_changed_cb, zfsvfs);
496 	error = error ? error : dsl_prop_register(ds,
497 	    "snapdir", snapdir_changed_cb, zfsvfs);
498 	error = error ? error : dsl_prop_register(ds,
499 	    "aclmode", acl_mode_changed_cb, zfsvfs);
500 	error = error ? error : dsl_prop_register(ds,
501 	    "aclinherit", acl_inherit_changed_cb, zfsvfs);
502 	if (error)
503 		goto unregister;
504 
505 	/*
506 	 * Invoke our callbacks to restore temporary mount options.
507 	 */
508 	if (do_readonly)
509 		readonly_changed_cb(zfsvfs, readonly);
510 	if (do_setuid)
511 		setuid_changed_cb(zfsvfs, setuid);
512 	if (do_exec)
513 		exec_changed_cb(zfsvfs, exec);
514 	if (do_devices)
515 		devices_changed_cb(zfsvfs, devices);
516 	if (do_xattr)
517 		xattr_changed_cb(zfsvfs, xattr);
518 
519 	return (0);
520 
521 unregister:
522 	/*
523 	 * We may attempt to unregister some callbacks that are not
524 	 * registered, but this is OK; it will simply return ENOMSG,
525 	 * which we will ignore.
526 	 */
527 	(void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
528 	(void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
529 	(void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
530 	(void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
531 	(void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs);
532 	(void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
533 	(void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
534 	(void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
535 	(void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
536 	(void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
537 	    zfsvfs);
538 	return (error);
539 
540 }
541 
542 static int
543 zfs_domount(vfs_t *vfsp, char *osname, cred_t *cr)
544 {
545 	dev_t mount_dev;
546 	uint64_t recordsize, readonly;
547 	int error = 0;
548 	int mode;
549 	zfsvfs_t *zfsvfs;
550 	znode_t *zp = NULL;
551 
552 	ASSERT(vfsp);
553 	ASSERT(osname);
554 
555 	/*
556 	 * Initialize the zfs-specific filesystem structure.
557 	 * Should probably make this a kmem cache, shuffle fields,
558 	 * and just bzero up to z_hold_mtx[].
559 	 */
560 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
561 	zfsvfs->z_vfs = vfsp;
562 	zfsvfs->z_parent = zfsvfs;
563 	zfsvfs->z_assign = TXG_NOWAIT;
564 	zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
565 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
566 
567 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
568 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
569 	    offsetof(znode_t, z_link_node));
570 	rw_init(&zfsvfs->z_um_lock, NULL, RW_DEFAULT, NULL);
571 
572 	/* Initialize the generic filesystem structure. */
573 	vfsp->vfs_bcount = 0;
574 	vfsp->vfs_data = NULL;
575 
576 	if (zfs_create_unique_device(&mount_dev) == -1) {
577 		error = ENODEV;
578 		goto out;
579 	}
580 	ASSERT(vfs_devismounted(mount_dev) == 0);
581 
582 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
583 	    NULL))
584 		goto out;
585 
586 	vfsp->vfs_dev = mount_dev;
587 	vfsp->vfs_fstype = zfsfstype;
588 	vfsp->vfs_bsize = recordsize;
589 	vfsp->vfs_flag |= VFS_NOTRUNC;
590 	vfsp->vfs_data = zfsvfs;
591 
592 	if (error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL))
593 		goto out;
594 
595 	if (readonly)
596 		mode = DS_MODE_PRIMARY | DS_MODE_READONLY;
597 	else
598 		mode = DS_MODE_PRIMARY;
599 
600 	error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
601 	if (error == EROFS) {
602 		mode = DS_MODE_PRIMARY | DS_MODE_READONLY;
603 		error = dmu_objset_open(osname, DMU_OST_ZFS, mode,
604 		    &zfsvfs->z_os);
605 	}
606 
607 	if (error)
608 		goto out;
609 
610 	if (error = zfs_init_fs(zfsvfs, &zp, cr))
611 		goto out;
612 
613 	/* The call to zfs_init_fs leaves the vnode held, release it here. */
614 	VN_RELE(ZTOV(zp));
615 
616 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
617 		uint64_t xattr;
618 
619 		ASSERT(mode & DS_MODE_READONLY);
620 		atime_changed_cb(zfsvfs, B_FALSE);
621 		readonly_changed_cb(zfsvfs, B_TRUE);
622 		if (error = dsl_prop_get_integer(osname, "xattr", &xattr, NULL))
623 			goto out;
624 		xattr_changed_cb(zfsvfs, xattr);
625 		zfsvfs->z_issnap = B_TRUE;
626 	} else {
627 		error = zfs_register_callbacks(vfsp);
628 		if (error)
629 			goto out;
630 
631 		zfs_unlinked_drain(zfsvfs);
632 
633 		/*
634 		 * Parse and replay the intent log.
635 		 */
636 		zil_replay(zfsvfs->z_os, zfsvfs, &zfsvfs->z_assign,
637 		    zfs_replay_vector);
638 
639 		if (!zil_disable)
640 			zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
641 	}
642 
643 	if (!zfsvfs->z_issnap)
644 		zfsctl_create(zfsvfs);
645 out:
646 	if (error) {
647 		if (zfsvfs->z_os)
648 			dmu_objset_close(zfsvfs->z_os);
649 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
650 	} else {
651 		atomic_add_32(&zfs_active_fs_count, 1);
652 	}
653 
654 	return (error);
655 
656 }
657 
658 void
659 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
660 {
661 	objset_t *os = zfsvfs->z_os;
662 	struct dsl_dataset *ds;
663 
664 	/*
665 	 * Unregister properties.
666 	 */
667 	if (!dmu_objset_is_snapshot(os)) {
668 		ds = dmu_objset_ds(os);
669 		VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
670 		    zfsvfs) == 0);
671 
672 		VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
673 		    zfsvfs) == 0);
674 
675 		VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
676 		    zfsvfs) == 0);
677 
678 		VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
679 		    zfsvfs) == 0);
680 
681 		VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
682 		    zfsvfs) == 0);
683 
684 		VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
685 		    zfsvfs) == 0);
686 
687 		VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
688 		    zfsvfs) == 0);
689 
690 		VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
691 		    zfsvfs) == 0);
692 
693 		VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
694 		    zfsvfs) == 0);
695 
696 		VERIFY(dsl_prop_unregister(ds, "aclinherit",
697 		    acl_inherit_changed_cb, zfsvfs) == 0);
698 	}
699 }
700 
701 static int
702 zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
703 {
704 	int error = 0;
705 	int ret = 0;
706 	static int zfsrootdone = 0;
707 	zfsvfs_t *zfsvfs = NULL;
708 	znode_t *zp = NULL;
709 	vnode_t *vp = NULL;
710 
711 	ASSERT(vfsp);
712 
713 	/*
714 	 * The filesystem that we mount as root is defined in
715 	 * /etc/system using the zfsroot variable.  The value defined
716 	 * there is copied early in startup code to zfs_bootpath
717 	 * (defined in modsysfile.c).
718 	 */
719 	if (why == ROOT_INIT) {
720 		if (zfsrootdone++)
721 			return (EBUSY);
722 
723 		/*
724 		 * This needs to be done here, so that when we return from
725 		 * mountroot, the vfs resource name will be set correctly.
726 		 */
727 		if (snprintf(rootfs.bo_name, BO_MAXOBJNAME, "%s", zfs_bootpath)
728 		    >= BO_MAXOBJNAME)
729 			return (ENAMETOOLONG);
730 
731 		if (error = vfs_lock(vfsp))
732 			return (error);
733 
734 		if (error = zfs_domount(vfsp, zfs_bootpath, CRED()))
735 			goto out;
736 
737 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
738 		ASSERT(zfsvfs);
739 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp))
740 			goto out;
741 
742 		vp = ZTOV(zp);
743 		mutex_enter(&vp->v_lock);
744 		vp->v_flag |= VROOT;
745 		mutex_exit(&vp->v_lock);
746 		rootvp = vp;
747 
748 		/*
749 		 * The zfs_zget call above returns with a hold on vp, we release
750 		 * it here.
751 		 */
752 		VN_RELE(vp);
753 
754 		/*
755 		 * Mount root as readonly initially, it will be remouted
756 		 * read/write by /lib/svc/method/fs-usr.
757 		 */
758 		readonly_changed_cb(vfsp->vfs_data, B_TRUE);
759 		vfs_add((struct vnode *)0, vfsp,
760 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
761 out:
762 		vfs_unlock(vfsp);
763 		ret = (error) ? error : 0;
764 		return (ret);
765 
766 	} else if (why == ROOT_REMOUNT) {
767 
768 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
769 		vfsp->vfs_flag |= VFS_REMOUNT;
770 		return (zfs_refresh_properties(vfsp));
771 
772 	} else if (why == ROOT_UNMOUNT) {
773 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
774 		(void) zfs_sync(vfsp, 0, 0);
775 		return (0);
776 	}
777 
778 	/*
779 	 * if "why" is equal to anything else other than ROOT_INIT,
780 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
781 	 */
782 	return (ENOTSUP);
783 }
784 
785 /*ARGSUSED*/
786 static int
787 zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
788 {
789 	char		*osname;
790 	pathname_t	spn;
791 	int		error = 0;
792 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
793 				UIO_SYSSPACE : UIO_USERSPACE;
794 	int		canwrite;
795 
796 	if (mvp->v_type != VDIR)
797 		return (ENOTDIR);
798 
799 	mutex_enter(&mvp->v_lock);
800 	if ((uap->flags & MS_REMOUNT) == 0 &&
801 	    (uap->flags & MS_OVERLAY) == 0 &&
802 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
803 		mutex_exit(&mvp->v_lock);
804 		return (EBUSY);
805 	}
806 	mutex_exit(&mvp->v_lock);
807 
808 	/*
809 	 * ZFS does not support passing unparsed data in via MS_DATA.
810 	 * Users should use the MS_OPTIONSTR interface; this means
811 	 * that all option parsing is already done and the options struct
812 	 * can be interrogated.
813 	 */
814 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
815 		return (EINVAL);
816 
817 	/*
818 	 * When doing a remount, we simply refresh our temporary properties
819 	 * according to those options set in the current VFS options.
820 	 */
821 	if (uap->flags & MS_REMOUNT) {
822 		return (zfs_refresh_properties(vfsp));
823 	}
824 
825 	/*
826 	 * Get the objset name (the "special" mount argument).
827 	 */
828 	if (error = pn_get(uap->spec, fromspace, &spn))
829 		return (error);
830 
831 	osname = spn.pn_path;
832 
833 	if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
834 		goto out;
835 
836 	/*
837 	 * Refuse to mount a filesystem if we are in a local zone and the
838 	 * dataset is not visible.
839 	 */
840 	if (!INGLOBALZONE(curproc) &&
841 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
842 		error = EPERM;
843 		goto out;
844 	}
845 
846 	error = zfs_domount(vfsp, osname, cr);
847 
848 out:
849 	pn_free(&spn);
850 	return (error);
851 }
852 
853 static int
854 zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
855 {
856 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
857 	dev32_t d32;
858 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
859 
860 	ZFS_ENTER(zfsvfs);
861 
862 	dmu_objset_space(zfsvfs->z_os,
863 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
864 
865 	/*
866 	 * The underlying storage pool actually uses multiple block sizes.
867 	 * We report the fragsize as the smallest block size we support,
868 	 * and we report our blocksize as the filesystem's maximum blocksize.
869 	 */
870 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
871 	statp->f_bsize = zfsvfs->z_max_blksz;
872 
873 	/*
874 	 * The following report "total" blocks of various kinds in the
875 	 * file system, but reported in terms of f_frsize - the
876 	 * "fragment" size.
877 	 */
878 
879 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
880 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
881 	statp->f_bavail = statp->f_bfree; /* no root reservation */
882 
883 	/*
884 	 * statvfs() should really be called statufs(), because it assumes
885 	 * static metadata.  ZFS doesn't preallocate files, so the best
886 	 * we can do is report the max that could possibly fit in f_files,
887 	 * and that minus the number actually used in f_ffree.
888 	 * For f_ffree, report the smaller of the number of object available
889 	 * and the number of blocks (each object will take at least a block).
890 	 */
891 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
892 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
893 	statp->f_files = statp->f_ffree + usedobjs;
894 
895 	(void) cmpldev(&d32, vfsp->vfs_dev);
896 	statp->f_fsid = d32;
897 
898 	/*
899 	 * We're a zfs filesystem.
900 	 */
901 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
902 
903 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
904 
905 	statp->f_namemax = ZFS_MAXNAMELEN;
906 
907 	/*
908 	 * We have all of 32 characters to stuff a string here.
909 	 * Is there anything useful we could/should provide?
910 	 */
911 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
912 
913 	ZFS_EXIT(zfsvfs);
914 	return (0);
915 }
916 
917 static int
918 zfs_root(vfs_t *vfsp, vnode_t **vpp)
919 {
920 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
921 	znode_t *rootzp;
922 	int error;
923 
924 	ZFS_ENTER(zfsvfs);
925 
926 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
927 	if (error == 0)
928 		*vpp = ZTOV(rootzp);
929 
930 	ZFS_EXIT(zfsvfs);
931 	return (error);
932 }
933 
934 /*ARGSUSED*/
935 static int
936 zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
937 {
938 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
939 	int ret;
940 
941 	if ((ret = secpolicy_fs_unmount(cr, vfsp)) != 0)
942 		return (ret);
943 
944 
945 	(void) dnlc_purge_vfsp(vfsp, 0);
946 
947 	/*
948 	 * Unmount any snapshots mounted under .zfs before unmounting the
949 	 * dataset itself.
950 	 */
951 	if (zfsvfs->z_ctldir != NULL &&
952 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0)
953 		return (ret);
954 
955 	if (fflag & MS_FORCE) {
956 		vfsp->vfs_flag |= VFS_UNMOUNTED;
957 		zfsvfs->z_unmounted1 = B_TRUE;
958 
959 		/*
960 		 * Wait for all zfs threads to leave zfs.
961 		 * Grabbing a rwlock as reader in all vops and
962 		 * as writer here doesn't work because it too easy to get
963 		 * multiple reader enters as zfs can re-enter itself.
964 		 * This can lead to deadlock if there is an intervening
965 		 * rw_enter as writer.
966 		 * So a file system threads ref count (z_op_cnt) is used.
967 		 * A polling loop on z_op_cnt may seem inefficient, but
968 		 * - this saves all threads on exit from having to grab a
969 		 *   mutex in order to cv_signal
970 		 * - only occurs on forced unmount in the rare case when
971 		 *   there are outstanding threads within the file system.
972 		 */
973 		while (zfsvfs->z_op_cnt) {
974 			delay(1);
975 		}
976 
977 		zfs_objset_close(zfsvfs);
978 
979 		return (0);
980 	}
981 	/*
982 	 * Check the number of active vnodes in the file system.
983 	 * Our count is maintained in the vfs structure, but the number
984 	 * is off by 1 to indicate a hold on the vfs structure itself.
985 	 *
986 	 * The '.zfs' directory maintains a reference of its own, and any active
987 	 * references underneath are reflected in the vnode count.
988 	 */
989 	if (zfsvfs->z_ctldir == NULL) {
990 		if (vfsp->vfs_count > 1)
991 			return (EBUSY);
992 	} else {
993 		if (vfsp->vfs_count > 2 ||
994 		    (zfsvfs->z_ctldir->v_count > 1 && !(fflag & MS_FORCE))) {
995 			return (EBUSY);
996 		}
997 	}
998 
999 	vfsp->vfs_flag |= VFS_UNMOUNTED;
1000 	zfs_objset_close(zfsvfs);
1001 
1002 	return (0);
1003 }
1004 
1005 static int
1006 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1007 {
1008 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1009 	znode_t		*zp;
1010 	uint64_t	object = 0;
1011 	uint64_t	fid_gen = 0;
1012 	uint64_t	gen_mask;
1013 	uint64_t	zp_gen;
1014 	int 		i, err;
1015 
1016 	*vpp = NULL;
1017 
1018 	ZFS_ENTER(zfsvfs);
1019 
1020 	if (fidp->fid_len == LONG_FID_LEN) {
1021 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1022 		uint64_t	objsetid = 0;
1023 		uint64_t	setgen = 0;
1024 
1025 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1026 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1027 
1028 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1029 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1030 
1031 		ZFS_EXIT(zfsvfs);
1032 
1033 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1034 		if (err)
1035 			return (EINVAL);
1036 		ZFS_ENTER(zfsvfs);
1037 	}
1038 
1039 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1040 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1041 
1042 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1043 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1044 
1045 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1046 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1047 	} else {
1048 		ZFS_EXIT(zfsvfs);
1049 		return (EINVAL);
1050 	}
1051 
1052 	/* A zero fid_gen means we are in the .zfs control directories */
1053 	if (fid_gen == 0 &&
1054 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1055 		*vpp = zfsvfs->z_ctldir;
1056 		ASSERT(*vpp != NULL);
1057 		if (object == ZFSCTL_INO_SNAPDIR) {
1058 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1059 			    0, NULL, NULL) == 0);
1060 		} else {
1061 			VN_HOLD(*vpp);
1062 		}
1063 		ZFS_EXIT(zfsvfs);
1064 		return (0);
1065 	}
1066 
1067 	gen_mask = -1ULL >> (64 - 8 * i);
1068 
1069 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1070 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1071 		ZFS_EXIT(zfsvfs);
1072 		return (err);
1073 	}
1074 	zp_gen = zp->z_phys->zp_gen & gen_mask;
1075 	if (zp_gen == 0)
1076 		zp_gen = 1;
1077 	if (zp->z_unlinked || zp_gen != fid_gen) {
1078 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1079 		VN_RELE(ZTOV(zp));
1080 		ZFS_EXIT(zfsvfs);
1081 		return (EINVAL);
1082 	}
1083 
1084 	*vpp = ZTOV(zp);
1085 	ZFS_EXIT(zfsvfs);
1086 	return (0);
1087 }
1088 
1089 static void
1090 zfs_objset_close(zfsvfs_t *zfsvfs)
1091 {
1092 	znode_t		*zp, *nextzp;
1093 	objset_t	*os = zfsvfs->z_os;
1094 
1095 	/*
1096 	 * For forced unmount, at this point all vops except zfs_inactive
1097 	 * are erroring EIO. We need to now suspend zfs_inactive threads
1098 	 * while we are freeing dbufs before switching zfs_inactive
1099 	 * to use behaviour without a objset.
1100 	 */
1101 	rw_enter(&zfsvfs->z_um_lock, RW_WRITER);
1102 
1103 	/*
1104 	 * Release all holds on dbufs
1105 	 * Note, although we have stopped all other vop threads and
1106 	 * zfs_inactive(), the dmu can callback via znode_pageout_func()
1107 	 * which can zfs_znode_free() the znode.
1108 	 * So we lock z_all_znodes; search the list for a held
1109 	 * dbuf; drop the lock (we know zp can't disappear if we hold
1110 	 * a dbuf lock; then regrab the lock and restart.
1111 	 */
1112 	mutex_enter(&zfsvfs->z_znodes_lock);
1113 	for (zp = list_head(&zfsvfs->z_all_znodes); zp; zp = nextzp) {
1114 		nextzp = list_next(&zfsvfs->z_all_znodes, zp);
1115 		if (zp->z_dbuf_held) {
1116 			/* dbufs should only be held when force unmounting */
1117 			zp->z_dbuf_held = 0;
1118 			mutex_exit(&zfsvfs->z_znodes_lock);
1119 			dmu_buf_rele(zp->z_dbuf, NULL);
1120 			/* Start again */
1121 			mutex_enter(&zfsvfs->z_znodes_lock);
1122 			nextzp = list_head(&zfsvfs->z_all_znodes);
1123 		}
1124 	}
1125 	mutex_exit(&zfsvfs->z_znodes_lock);
1126 
1127 	/*
1128 	 * Unregister properties.
1129 	 */
1130 	if (!dmu_objset_is_snapshot(os))
1131 		zfs_unregister_callbacks(zfsvfs);
1132 
1133 	/*
1134 	 * Switch zfs_inactive to behaviour without an objset.
1135 	 * It just tosses cached pages and frees the znode & vnode.
1136 	 * Then re-enable zfs_inactive threads in that new behaviour.
1137 	 */
1138 	zfsvfs->z_unmounted2 = B_TRUE;
1139 	rw_exit(&zfsvfs->z_um_lock); /* re-enable any zfs_inactive threads */
1140 
1141 	/*
1142 	 * Close the zil. Can't close the zil while zfs_inactive
1143 	 * threads are blocked as zil_close can call zfs_inactive.
1144 	 */
1145 	if (zfsvfs->z_log) {
1146 		zil_close(zfsvfs->z_log);
1147 		zfsvfs->z_log = NULL;
1148 	}
1149 
1150 	/*
1151 	 * Evict all dbufs so that cached znodes will be freed
1152 	 */
1153 	if (dmu_objset_evict_dbufs(os, 1)) {
1154 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1155 		(void) dmu_objset_evict_dbufs(os, 0);
1156 	}
1157 
1158 	/*
1159 	 * Finally close the objset
1160 	 */
1161 	dmu_objset_close(os);
1162 
1163 	/*
1164 	 * We can now safely destroy the '.zfs' directory node.
1165 	 */
1166 	if (zfsvfs->z_ctldir != NULL)
1167 		zfsctl_destroy(zfsvfs);
1168 
1169 }
1170 
1171 static void
1172 zfs_freevfs(vfs_t *vfsp)
1173 {
1174 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1175 
1176 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1177 
1178 	atomic_add_32(&zfs_active_fs_count, -1);
1179 }
1180 
1181 /*
1182  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
1183  * so we can't safely do any non-idempotent initialization here.
1184  * Leave that to zfs_init() and zfs_fini(), which are called
1185  * from the module's _init() and _fini() entry points.
1186  */
1187 /*ARGSUSED*/
1188 static int
1189 zfs_vfsinit(int fstype, char *name)
1190 {
1191 	int error;
1192 
1193 	zfsfstype = fstype;
1194 
1195 	/*
1196 	 * Setup vfsops and vnodeops tables.
1197 	 */
1198 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
1199 	if (error != 0) {
1200 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
1201 	}
1202 
1203 	error = zfs_create_op_tables();
1204 	if (error) {
1205 		zfs_remove_op_tables();
1206 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
1207 		(void) vfs_freevfsops_by_type(zfsfstype);
1208 		return (error);
1209 	}
1210 
1211 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
1212 
1213 	/*
1214 	 * Unique major number for all zfs mounts.
1215 	 * If we run out of 32-bit minors, we'll getudev() another major.
1216 	 */
1217 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
1218 	zfs_minor = ZFS_MIN_MINOR;
1219 
1220 	return (0);
1221 }
1222 
1223 void
1224 zfs_init(void)
1225 {
1226 	/*
1227 	 * Initialize .zfs directory structures
1228 	 */
1229 	zfsctl_init();
1230 
1231 	/*
1232 	 * Initialize znode cache, vnode ops, etc...
1233 	 */
1234 	zfs_znode_init();
1235 }
1236 
1237 void
1238 zfs_fini(void)
1239 {
1240 	zfsctl_fini();
1241 	zfs_znode_fini();
1242 }
1243 
1244 int
1245 zfs_busy(void)
1246 {
1247 	return (zfs_active_fs_count != 0);
1248 }
1249 
1250 static vfsdef_t vfw = {
1251 	VFSDEF_VERSION,
1252 	MNTTYPE_ZFS,
1253 	zfs_vfsinit,
1254 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS,
1255 	&zfs_mntopts
1256 };
1257 
1258 struct modlfs zfs_modlfs = {
1259 	&mod_fsops, "ZFS filesystem version " ZFS_VERSION_STRING, &vfw
1260 };
1261