xref: /titanic_44/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 5151fb1220e0ceafdc172203863c73da4285c170)
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/vfs_opreg.h>
38 #include <sys/mntent.h>
39 #include <sys/mount.h>
40 #include <sys/cmn_err.h>
41 #include "fs/fs_subr.h"
42 #include <sys/zfs_znode.h>
43 #include <sys/zfs_dir.h>
44 #include <sys/zil.h>
45 #include <sys/fs/zfs.h>
46 #include <sys/dmu.h>
47 #include <sys/dsl_prop.h>
48 #include <sys/dsl_dataset.h>
49 #include <sys/spa.h>
50 #include <sys/zap.h>
51 #include <sys/varargs.h>
52 #include <sys/policy.h>
53 #include <sys/atomic.h>
54 #include <sys/mkdev.h>
55 #include <sys/modctl.h>
56 #include <sys/zfs_ioctl.h>
57 #include <sys/zfs_ctldir.h>
58 #include <sys/bootconf.h>
59 #include <sys/sunddi.h>
60 #include <sys/dnlc.h>
61 
62 int zfsfstype;
63 vfsops_t *zfs_vfsops = NULL;
64 static major_t zfs_major;
65 static minor_t zfs_minor;
66 static kmutex_t	zfs_dev_mtx;
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,		{ .vfs_mount = zfs_mount },
79 	VFSNAME_MOUNTROOT,	{ .vfs_mountroot = zfs_mountroot },
80 	VFSNAME_UNMOUNT,	{ .vfs_unmount = zfs_umount },
81 	VFSNAME_ROOT,		{ .vfs_root = zfs_root },
82 	VFSNAME_STATVFS,	{ .vfs_statvfs = zfs_statvfs },
83 	VFSNAME_SYNC,		{ .vfs_sync = zfs_sync },
84 	VFSNAME_VGET,		{ .vfs_vget = zfs_vget },
85 	VFSNAME_FREEVFS,	{ .vfs_freevfs = zfs_freevfs },
86 	NULL,			NULL
87 };
88 
89 static const fs_operation_def_t zfs_vfsops_eio_template[] = {
90 	VFSNAME_FREEVFS,	{ .vfs_freevfs =  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, MNTOPT_DEVICES, NULL)) {
447 			devices = B_TRUE;
448 			do_devices = B_TRUE;
449 		}
450 
451 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
452 			setuid = B_FALSE;
453 			do_setuid = B_TRUE;
454 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
455 			setuid = B_TRUE;
456 			do_setuid = B_TRUE;
457 		}
458 	}
459 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
460 		exec = B_FALSE;
461 		do_exec = B_TRUE;
462 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
463 		exec = B_TRUE;
464 		do_exec = B_TRUE;
465 	}
466 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
467 		xattr = B_FALSE;
468 		do_xattr = B_TRUE;
469 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
470 		xattr = B_TRUE;
471 		do_xattr = B_TRUE;
472 	}
473 
474 	/*
475 	 * Register property callbacks.
476 	 *
477 	 * It would probably be fine to just check for i/o error from
478 	 * the first prop_register(), but I guess I like to go
479 	 * overboard...
480 	 */
481 	ds = dmu_objset_ds(os);
482 	error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
483 	error = error ? error : dsl_prop_register(ds,
484 	    "xattr", xattr_changed_cb, zfsvfs);
485 	error = error ? error : dsl_prop_register(ds,
486 	    "recordsize", blksz_changed_cb, zfsvfs);
487 	error = error ? error : dsl_prop_register(ds,
488 	    "readonly", readonly_changed_cb, zfsvfs);
489 	error = error ? error : dsl_prop_register(ds,
490 	    "devices", devices_changed_cb, zfsvfs);
491 	error = error ? error : dsl_prop_register(ds,
492 	    "setuid", setuid_changed_cb, zfsvfs);
493 	error = error ? error : dsl_prop_register(ds,
494 	    "exec", exec_changed_cb, zfsvfs);
495 	error = error ? error : dsl_prop_register(ds,
496 	    "snapdir", snapdir_changed_cb, zfsvfs);
497 	error = error ? error : dsl_prop_register(ds,
498 	    "aclmode", acl_mode_changed_cb, zfsvfs);
499 	error = error ? error : dsl_prop_register(ds,
500 	    "aclinherit", acl_inherit_changed_cb, zfsvfs);
501 	if (error)
502 		goto unregister;
503 
504 	/*
505 	 * Invoke our callbacks to restore temporary mount options.
506 	 */
507 	if (do_readonly)
508 		readonly_changed_cb(zfsvfs, readonly);
509 	if (do_setuid)
510 		setuid_changed_cb(zfsvfs, setuid);
511 	if (do_exec)
512 		exec_changed_cb(zfsvfs, exec);
513 	if (do_devices)
514 		devices_changed_cb(zfsvfs, devices);
515 	if (do_xattr)
516 		xattr_changed_cb(zfsvfs, xattr);
517 
518 	return (0);
519 
520 unregister:
521 	/*
522 	 * We may attempt to unregister some callbacks that are not
523 	 * registered, but this is OK; it will simply return ENOMSG,
524 	 * which we will ignore.
525 	 */
526 	(void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
527 	(void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
528 	(void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
529 	(void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
530 	(void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs);
531 	(void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
532 	(void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
533 	(void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
534 	(void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
535 	(void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
536 	    zfsvfs);
537 	return (error);
538 
539 }
540 
541 static int
542 zfs_domount(vfs_t *vfsp, char *osname, cred_t *cr)
543 {
544 	dev_t mount_dev;
545 	uint64_t recordsize, readonly;
546 	int error = 0;
547 	int mode;
548 	zfsvfs_t *zfsvfs;
549 	znode_t *zp = NULL;
550 
551 	ASSERT(vfsp);
552 	ASSERT(osname);
553 
554 	/*
555 	 * Initialize the zfs-specific filesystem structure.
556 	 * Should probably make this a kmem cache, shuffle fields,
557 	 * and just bzero up to z_hold_mtx[].
558 	 */
559 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
560 	zfsvfs->z_vfs = vfsp;
561 	zfsvfs->z_parent = zfsvfs;
562 	zfsvfs->z_assign = TXG_NOWAIT;
563 	zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
564 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
565 
566 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
567 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
568 	    offsetof(znode_t, z_link_node));
569 	rw_init(&zfsvfs->z_um_lock, NULL, RW_DEFAULT, NULL);
570 
571 	/* Initialize the generic filesystem structure. */
572 	vfsp->vfs_bcount = 0;
573 	vfsp->vfs_data = NULL;
574 
575 	if (zfs_create_unique_device(&mount_dev) == -1) {
576 		error = ENODEV;
577 		goto out;
578 	}
579 	ASSERT(vfs_devismounted(mount_dev) == 0);
580 
581 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
582 	    NULL))
583 		goto out;
584 
585 	vfsp->vfs_dev = mount_dev;
586 	vfsp->vfs_fstype = zfsfstype;
587 	vfsp->vfs_bsize = recordsize;
588 	vfsp->vfs_flag |= VFS_NOTRUNC;
589 	vfsp->vfs_data = zfsvfs;
590 
591 	if (error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL))
592 		goto out;
593 
594 	if (readonly)
595 		mode = DS_MODE_PRIMARY | DS_MODE_READONLY;
596 	else
597 		mode = DS_MODE_PRIMARY;
598 
599 	error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
600 	if (error == EROFS) {
601 		mode = DS_MODE_PRIMARY | DS_MODE_READONLY;
602 		error = dmu_objset_open(osname, DMU_OST_ZFS, mode,
603 		    &zfsvfs->z_os);
604 	}
605 
606 	if (error)
607 		goto out;
608 
609 	if (error = zfs_init_fs(zfsvfs, &zp, cr))
610 		goto out;
611 
612 	/* The call to zfs_init_fs leaves the vnode held, release it here. */
613 	VN_RELE(ZTOV(zp));
614 
615 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
616 		uint64_t xattr;
617 
618 		ASSERT(mode & DS_MODE_READONLY);
619 		atime_changed_cb(zfsvfs, B_FALSE);
620 		readonly_changed_cb(zfsvfs, B_TRUE);
621 		if (error = dsl_prop_get_integer(osname, "xattr", &xattr, NULL))
622 			goto out;
623 		xattr_changed_cb(zfsvfs, xattr);
624 		zfsvfs->z_issnap = B_TRUE;
625 	} else {
626 		error = zfs_register_callbacks(vfsp);
627 		if (error)
628 			goto out;
629 
630 		zfs_unlinked_drain(zfsvfs);
631 
632 		/*
633 		 * Parse and replay the intent log.
634 		 */
635 		zil_replay(zfsvfs->z_os, zfsvfs, &zfsvfs->z_assign,
636 		    zfs_replay_vector);
637 
638 		if (!zil_disable)
639 			zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
640 	}
641 
642 	if (!zfsvfs->z_issnap)
643 		zfsctl_create(zfsvfs);
644 out:
645 	if (error) {
646 		if (zfsvfs->z_os)
647 			dmu_objset_close(zfsvfs->z_os);
648 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
649 	} else {
650 		atomic_add_32(&zfs_active_fs_count, 1);
651 	}
652 
653 	return (error);
654 
655 }
656 
657 void
658 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
659 {
660 	objset_t *os = zfsvfs->z_os;
661 	struct dsl_dataset *ds;
662 
663 	/*
664 	 * Unregister properties.
665 	 */
666 	if (!dmu_objset_is_snapshot(os)) {
667 		ds = dmu_objset_ds(os);
668 		VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
669 		    zfsvfs) == 0);
670 
671 		VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
672 		    zfsvfs) == 0);
673 
674 		VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
675 		    zfsvfs) == 0);
676 
677 		VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
678 		    zfsvfs) == 0);
679 
680 		VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
681 		    zfsvfs) == 0);
682 
683 		VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
684 		    zfsvfs) == 0);
685 
686 		VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
687 		    zfsvfs) == 0);
688 
689 		VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
690 		    zfsvfs) == 0);
691 
692 		VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
693 		    zfsvfs) == 0);
694 
695 		VERIFY(dsl_prop_unregister(ds, "aclinherit",
696 		    acl_inherit_changed_cb, zfsvfs) == 0);
697 	}
698 }
699 
700 /*
701  * Convert a decimal digit string to a uint64_t integer.
702  */
703 static int
704 str_to_uint64(char *str, uint64_t *objnum)
705 {
706 	uint64_t num = 0;
707 
708 	while (*str) {
709 		if (*str < '0' || *str > '9')
710 			return (EINVAL);
711 
712 		num = num*10 + *str++ - '0';
713 	}
714 
715 	*objnum = num;
716 	return (0);
717 }
718 
719 
720 /*
721  * The boot path passed from the boot loader is in the form of
722  * "rootpool-name/root-filesystem-object-number'. Convert this
723  * string to a dataset name: "rootpool-name/root-filesystem-name".
724  */
725 static int
726 parse_bootpath(char *bpath, char *outpath)
727 {
728 	char *slashp;
729 	uint64_t objnum;
730 	int error;
731 
732 	if (*bpath == 0 || *bpath == '/')
733 		return (EINVAL);
734 
735 	slashp = strchr(bpath, '/');
736 
737 	/* if no '/', just return the pool name */
738 	if (slashp == NULL) {
739 		(void) strcpy(outpath, bpath);
740 		return (0);
741 	}
742 
743 	if (error = str_to_uint64(slashp+1, &objnum))
744 		return (error);
745 
746 	*slashp = '\0';
747 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
748 	*slashp = '/';
749 
750 	return (error);
751 }
752 
753 static int
754 zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
755 {
756 	int error = 0;
757 	int ret = 0;
758 	static int zfsrootdone = 0;
759 	zfsvfs_t *zfsvfs = NULL;
760 	znode_t *zp = NULL;
761 	vnode_t *vp = NULL;
762 	char *zfs_bootpath;
763 
764 	ASSERT(vfsp);
765 
766 	/*
767 	 * The filesystem that we mount as root is defined in the
768 	 * "zfs-bootfs" property.
769 	 */
770 	if (why == ROOT_INIT) {
771 		if (zfsrootdone++)
772 			return (EBUSY);
773 
774 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
775 		    DDI_PROP_DONTPASS, "zfs-bootfs", &zfs_bootpath) !=
776 		    DDI_SUCCESS)
777 			return (EIO);
778 
779 		error = parse_bootpath(zfs_bootpath, rootfs.bo_name);
780 		ddi_prop_free(zfs_bootpath);
781 
782 		if (error)
783 			return (error);
784 
785 		if (error = vfs_lock(vfsp))
786 			return (error);
787 
788 		if (error = zfs_domount(vfsp, rootfs.bo_name, CRED()))
789 			goto out;
790 
791 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
792 		ASSERT(zfsvfs);
793 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp))
794 			goto out;
795 
796 		vp = ZTOV(zp);
797 		mutex_enter(&vp->v_lock);
798 		vp->v_flag |= VROOT;
799 		mutex_exit(&vp->v_lock);
800 		rootvp = vp;
801 
802 		/*
803 		 * The zfs_zget call above returns with a hold on vp, we release
804 		 * it here.
805 		 */
806 		VN_RELE(vp);
807 
808 		/*
809 		 * Mount root as readonly initially, it will be remouted
810 		 * read/write by /lib/svc/method/fs-usr.
811 		 */
812 		readonly_changed_cb(vfsp->vfs_data, B_TRUE);
813 		vfs_add((struct vnode *)0, vfsp,
814 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
815 out:
816 		vfs_unlock(vfsp);
817 		ret = (error) ? error : 0;
818 		return (ret);
819 
820 	} else if (why == ROOT_REMOUNT) {
821 
822 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
823 		vfsp->vfs_flag |= VFS_REMOUNT;
824 		return (zfs_refresh_properties(vfsp));
825 
826 	} else if (why == ROOT_UNMOUNT) {
827 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
828 		(void) zfs_sync(vfsp, 0, 0);
829 		return (0);
830 	}
831 
832 	/*
833 	 * if "why" is equal to anything else other than ROOT_INIT,
834 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
835 	 */
836 	return (ENOTSUP);
837 }
838 
839 /*ARGSUSED*/
840 static int
841 zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
842 {
843 	char		*osname;
844 	pathname_t	spn;
845 	int		error = 0;
846 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
847 	    UIO_SYSSPACE : UIO_USERSPACE;
848 	int		canwrite;
849 
850 	if (mvp->v_type != VDIR)
851 		return (ENOTDIR);
852 
853 	mutex_enter(&mvp->v_lock);
854 	if ((uap->flags & MS_REMOUNT) == 0 &&
855 	    (uap->flags & MS_OVERLAY) == 0 &&
856 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
857 		mutex_exit(&mvp->v_lock);
858 		return (EBUSY);
859 	}
860 	mutex_exit(&mvp->v_lock);
861 
862 	/*
863 	 * ZFS does not support passing unparsed data in via MS_DATA.
864 	 * Users should use the MS_OPTIONSTR interface; this means
865 	 * that all option parsing is already done and the options struct
866 	 * can be interrogated.
867 	 */
868 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
869 		return (EINVAL);
870 
871 	/*
872 	 * When doing a remount, we simply refresh our temporary properties
873 	 * according to those options set in the current VFS options.
874 	 */
875 	if (uap->flags & MS_REMOUNT) {
876 		return (zfs_refresh_properties(vfsp));
877 	}
878 
879 	/*
880 	 * Get the objset name (the "special" mount argument).
881 	 */
882 	if (error = pn_get(uap->spec, fromspace, &spn))
883 		return (error);
884 
885 	osname = spn.pn_path;
886 
887 	if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
888 		goto out;
889 
890 	/*
891 	 * Refuse to mount a filesystem if we are in a local zone and the
892 	 * dataset is not visible.
893 	 */
894 	if (!INGLOBALZONE(curproc) &&
895 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
896 		error = EPERM;
897 		goto out;
898 	}
899 
900 	error = zfs_domount(vfsp, osname, cr);
901 
902 out:
903 	pn_free(&spn);
904 	return (error);
905 }
906 
907 static int
908 zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
909 {
910 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
911 	dev32_t d32;
912 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
913 
914 	ZFS_ENTER(zfsvfs);
915 
916 	dmu_objset_space(zfsvfs->z_os,
917 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
918 
919 	/*
920 	 * The underlying storage pool actually uses multiple block sizes.
921 	 * We report the fragsize as the smallest block size we support,
922 	 * and we report our blocksize as the filesystem's maximum blocksize.
923 	 */
924 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
925 	statp->f_bsize = zfsvfs->z_max_blksz;
926 
927 	/*
928 	 * The following report "total" blocks of various kinds in the
929 	 * file system, but reported in terms of f_frsize - the
930 	 * "fragment" size.
931 	 */
932 
933 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
934 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
935 	statp->f_bavail = statp->f_bfree; /* no root reservation */
936 
937 	/*
938 	 * statvfs() should really be called statufs(), because it assumes
939 	 * static metadata.  ZFS doesn't preallocate files, so the best
940 	 * we can do is report the max that could possibly fit in f_files,
941 	 * and that minus the number actually used in f_ffree.
942 	 * For f_ffree, report the smaller of the number of object available
943 	 * and the number of blocks (each object will take at least a block).
944 	 */
945 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
946 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
947 	statp->f_files = statp->f_ffree + usedobjs;
948 
949 	(void) cmpldev(&d32, vfsp->vfs_dev);
950 	statp->f_fsid = d32;
951 
952 	/*
953 	 * We're a zfs filesystem.
954 	 */
955 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
956 
957 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
958 
959 	statp->f_namemax = ZFS_MAXNAMELEN;
960 
961 	/*
962 	 * We have all of 32 characters to stuff a string here.
963 	 * Is there anything useful we could/should provide?
964 	 */
965 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
966 
967 	ZFS_EXIT(zfsvfs);
968 	return (0);
969 }
970 
971 static int
972 zfs_root(vfs_t *vfsp, vnode_t **vpp)
973 {
974 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
975 	znode_t *rootzp;
976 	int error;
977 
978 	ZFS_ENTER(zfsvfs);
979 
980 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
981 	if (error == 0)
982 		*vpp = ZTOV(rootzp);
983 
984 	ZFS_EXIT(zfsvfs);
985 	return (error);
986 }
987 
988 /*ARGSUSED*/
989 static int
990 zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
991 {
992 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
993 	int ret;
994 
995 	if ((ret = secpolicy_fs_unmount(cr, vfsp)) != 0)
996 		return (ret);
997 
998 
999 	(void) dnlc_purge_vfsp(vfsp, 0);
1000 
1001 	/*
1002 	 * Unmount any snapshots mounted under .zfs before unmounting the
1003 	 * dataset itself.
1004 	 */
1005 	if (zfsvfs->z_ctldir != NULL &&
1006 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0)
1007 		return (ret);
1008 
1009 	if (fflag & MS_FORCE) {
1010 		vfsp->vfs_flag |= VFS_UNMOUNTED;
1011 		zfsvfs->z_unmounted1 = B_TRUE;
1012 
1013 		/*
1014 		 * Wait for all zfs threads to leave zfs.
1015 		 * Grabbing a rwlock as reader in all vops and
1016 		 * as writer here doesn't work because it too easy to get
1017 		 * multiple reader enters as zfs can re-enter itself.
1018 		 * This can lead to deadlock if there is an intervening
1019 		 * rw_enter as writer.
1020 		 * So a file system threads ref count (z_op_cnt) is used.
1021 		 * A polling loop on z_op_cnt may seem inefficient, but
1022 		 * - this saves all threads on exit from having to grab a
1023 		 *   mutex in order to cv_signal
1024 		 * - only occurs on forced unmount in the rare case when
1025 		 *   there are outstanding threads within the file system.
1026 		 */
1027 		while (zfsvfs->z_op_cnt) {
1028 			delay(1);
1029 		}
1030 
1031 		zfs_objset_close(zfsvfs);
1032 
1033 		return (0);
1034 	}
1035 	/*
1036 	 * Check the number of active vnodes in the file system.
1037 	 * Our count is maintained in the vfs structure, but the number
1038 	 * is off by 1 to indicate a hold on the vfs structure itself.
1039 	 *
1040 	 * The '.zfs' directory maintains a reference of its own, and any active
1041 	 * references underneath are reflected in the vnode count.
1042 	 */
1043 	if (zfsvfs->z_ctldir == NULL) {
1044 		if (vfsp->vfs_count > 1)
1045 			return (EBUSY);
1046 	} else {
1047 		if (vfsp->vfs_count > 2 ||
1048 		    (zfsvfs->z_ctldir->v_count > 1 && !(fflag & MS_FORCE))) {
1049 			return (EBUSY);
1050 		}
1051 	}
1052 
1053 	vfsp->vfs_flag |= VFS_UNMOUNTED;
1054 	zfs_objset_close(zfsvfs);
1055 
1056 	return (0);
1057 }
1058 
1059 static int
1060 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1061 {
1062 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1063 	znode_t		*zp;
1064 	uint64_t	object = 0;
1065 	uint64_t	fid_gen = 0;
1066 	uint64_t	gen_mask;
1067 	uint64_t	zp_gen;
1068 	int 		i, err;
1069 
1070 	*vpp = NULL;
1071 
1072 	ZFS_ENTER(zfsvfs);
1073 
1074 	if (fidp->fid_len == LONG_FID_LEN) {
1075 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1076 		uint64_t	objsetid = 0;
1077 		uint64_t	setgen = 0;
1078 
1079 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1080 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1081 
1082 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1083 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1084 
1085 		ZFS_EXIT(zfsvfs);
1086 
1087 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1088 		if (err)
1089 			return (EINVAL);
1090 		ZFS_ENTER(zfsvfs);
1091 	}
1092 
1093 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1094 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1095 
1096 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1097 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1098 
1099 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1100 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1101 	} else {
1102 		ZFS_EXIT(zfsvfs);
1103 		return (EINVAL);
1104 	}
1105 
1106 	/* A zero fid_gen means we are in the .zfs control directories */
1107 	if (fid_gen == 0 &&
1108 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1109 		*vpp = zfsvfs->z_ctldir;
1110 		ASSERT(*vpp != NULL);
1111 		if (object == ZFSCTL_INO_SNAPDIR) {
1112 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1113 			    0, NULL, NULL) == 0);
1114 		} else {
1115 			VN_HOLD(*vpp);
1116 		}
1117 		ZFS_EXIT(zfsvfs);
1118 		return (0);
1119 	}
1120 
1121 	gen_mask = -1ULL >> (64 - 8 * i);
1122 
1123 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1124 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1125 		ZFS_EXIT(zfsvfs);
1126 		return (err);
1127 	}
1128 	zp_gen = zp->z_phys->zp_gen & gen_mask;
1129 	if (zp_gen == 0)
1130 		zp_gen = 1;
1131 	if (zp->z_unlinked || zp_gen != fid_gen) {
1132 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1133 		VN_RELE(ZTOV(zp));
1134 		ZFS_EXIT(zfsvfs);
1135 		return (EINVAL);
1136 	}
1137 
1138 	*vpp = ZTOV(zp);
1139 	ZFS_EXIT(zfsvfs);
1140 	return (0);
1141 }
1142 
1143 static void
1144 zfs_objset_close(zfsvfs_t *zfsvfs)
1145 {
1146 	znode_t		*zp, *nextzp;
1147 	objset_t	*os = zfsvfs->z_os;
1148 
1149 	/*
1150 	 * For forced unmount, at this point all vops except zfs_inactive
1151 	 * are erroring EIO. We need to now suspend zfs_inactive threads
1152 	 * while we are freeing dbufs before switching zfs_inactive
1153 	 * to use behaviour without a objset.
1154 	 */
1155 	rw_enter(&zfsvfs->z_um_lock, RW_WRITER);
1156 
1157 	/*
1158 	 * Release all holds on dbufs
1159 	 * Note, although we have stopped all other vop threads and
1160 	 * zfs_inactive(), the dmu can callback via znode_pageout_func()
1161 	 * which can zfs_znode_free() the znode.
1162 	 * So we lock z_all_znodes; search the list for a held
1163 	 * dbuf; drop the lock (we know zp can't disappear if we hold
1164 	 * a dbuf lock; then regrab the lock and restart.
1165 	 */
1166 	mutex_enter(&zfsvfs->z_znodes_lock);
1167 	for (zp = list_head(&zfsvfs->z_all_znodes); zp; zp = nextzp) {
1168 		nextzp = list_next(&zfsvfs->z_all_znodes, zp);
1169 		if (zp->z_dbuf_held) {
1170 			/* dbufs should only be held when force unmounting */
1171 			zp->z_dbuf_held = 0;
1172 			mutex_exit(&zfsvfs->z_znodes_lock);
1173 			dmu_buf_rele(zp->z_dbuf, NULL);
1174 			/* Start again */
1175 			mutex_enter(&zfsvfs->z_znodes_lock);
1176 			nextzp = list_head(&zfsvfs->z_all_znodes);
1177 		}
1178 	}
1179 	mutex_exit(&zfsvfs->z_znodes_lock);
1180 
1181 	/*
1182 	 * Unregister properties.
1183 	 */
1184 	if (!dmu_objset_is_snapshot(os))
1185 		zfs_unregister_callbacks(zfsvfs);
1186 
1187 	/*
1188 	 * Switch zfs_inactive to behaviour without an objset.
1189 	 * It just tosses cached pages and frees the znode & vnode.
1190 	 * Then re-enable zfs_inactive threads in that new behaviour.
1191 	 */
1192 	zfsvfs->z_unmounted2 = B_TRUE;
1193 	rw_exit(&zfsvfs->z_um_lock); /* re-enable any zfs_inactive threads */
1194 
1195 	/*
1196 	 * Close the zil. Can't close the zil while zfs_inactive
1197 	 * threads are blocked as zil_close can call zfs_inactive.
1198 	 */
1199 	if (zfsvfs->z_log) {
1200 		zil_close(zfsvfs->z_log);
1201 		zfsvfs->z_log = NULL;
1202 	}
1203 
1204 	/*
1205 	 * Evict all dbufs so that cached znodes will be freed
1206 	 */
1207 	if (dmu_objset_evict_dbufs(os, 1)) {
1208 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1209 		(void) dmu_objset_evict_dbufs(os, 0);
1210 	}
1211 
1212 	/*
1213 	 * Finally close the objset
1214 	 */
1215 	dmu_objset_close(os);
1216 
1217 	/*
1218 	 * We can now safely destroy the '.zfs' directory node.
1219 	 */
1220 	if (zfsvfs->z_ctldir != NULL)
1221 		zfsctl_destroy(zfsvfs);
1222 
1223 }
1224 
1225 static void
1226 zfs_freevfs(vfs_t *vfsp)
1227 {
1228 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1229 
1230 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1231 
1232 	atomic_add_32(&zfs_active_fs_count, -1);
1233 }
1234 
1235 /*
1236  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
1237  * so we can't safely do any non-idempotent initialization here.
1238  * Leave that to zfs_init() and zfs_fini(), which are called
1239  * from the module's _init() and _fini() entry points.
1240  */
1241 /*ARGSUSED*/
1242 static int
1243 zfs_vfsinit(int fstype, char *name)
1244 {
1245 	int error;
1246 
1247 	zfsfstype = fstype;
1248 
1249 	/*
1250 	 * Setup vfsops and vnodeops tables.
1251 	 */
1252 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
1253 	if (error != 0) {
1254 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
1255 	}
1256 
1257 	error = zfs_create_op_tables();
1258 	if (error) {
1259 		zfs_remove_op_tables();
1260 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
1261 		(void) vfs_freevfsops_by_type(zfsfstype);
1262 		return (error);
1263 	}
1264 
1265 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
1266 
1267 	/*
1268 	 * Unique major number for all zfs mounts.
1269 	 * If we run out of 32-bit minors, we'll getudev() another major.
1270 	 */
1271 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
1272 	zfs_minor = ZFS_MIN_MINOR;
1273 
1274 	return (0);
1275 }
1276 
1277 void
1278 zfs_init(void)
1279 {
1280 	/*
1281 	 * Initialize .zfs directory structures
1282 	 */
1283 	zfsctl_init();
1284 
1285 	/*
1286 	 * Initialize znode cache, vnode ops, etc...
1287 	 */
1288 	zfs_znode_init();
1289 }
1290 
1291 void
1292 zfs_fini(void)
1293 {
1294 	zfsctl_fini();
1295 	zfs_znode_fini();
1296 }
1297 
1298 int
1299 zfs_busy(void)
1300 {
1301 	return (zfs_active_fs_count != 0);
1302 }
1303 
1304 static vfsdef_t vfw = {
1305 	VFSDEF_VERSION,
1306 	MNTTYPE_ZFS,
1307 	zfs_vfsinit,
1308 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS,
1309 	&zfs_mntopts
1310 };
1311 
1312 struct modlfs zfs_modlfs = {
1313 	&mod_fsops, "ZFS filesystem version " ZFS_VERSION_STRING, &vfw
1314 };
1315