xref: /titanic_41/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 672986541be54a7a471bb088e60780c37e371d7e)
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 		 * Ensure that z_unmounted1 reaches global visibility
1015 		 * before z_op_cnt.
1016 		 */
1017 		membar_producer();
1018 
1019 		/*
1020 		 * Wait for all zfs threads to leave zfs.
1021 		 * Grabbing a rwlock as reader in all vops and
1022 		 * as writer here doesn't work because it too easy to get
1023 		 * multiple reader enters as zfs can re-enter itself.
1024 		 * This can lead to deadlock if there is an intervening
1025 		 * rw_enter as writer.
1026 		 * So a file system threads ref count (z_op_cnt) is used.
1027 		 * A polling loop on z_op_cnt may seem inefficient, but
1028 		 * - this saves all threads on exit from having to grab a
1029 		 *   mutex in order to cv_signal
1030 		 * - only occurs on forced unmount in the rare case when
1031 		 *   there are outstanding threads within the file system.
1032 		 */
1033 		while (zfsvfs->z_op_cnt) {
1034 			delay(1);
1035 		}
1036 
1037 		zfs_objset_close(zfsvfs);
1038 
1039 		return (0);
1040 	}
1041 	/*
1042 	 * Check the number of active vnodes in the file system.
1043 	 * Our count is maintained in the vfs structure, but the number
1044 	 * is off by 1 to indicate a hold on the vfs structure itself.
1045 	 *
1046 	 * The '.zfs' directory maintains a reference of its own, and any active
1047 	 * references underneath are reflected in the vnode count.
1048 	 */
1049 	if (zfsvfs->z_ctldir == NULL) {
1050 		if (vfsp->vfs_count > 1)
1051 			return (EBUSY);
1052 	} else {
1053 		if (vfsp->vfs_count > 2 ||
1054 		    (zfsvfs->z_ctldir->v_count > 1 && !(fflag & MS_FORCE))) {
1055 			return (EBUSY);
1056 		}
1057 	}
1058 
1059 	vfsp->vfs_flag |= VFS_UNMOUNTED;
1060 	zfs_objset_close(zfsvfs);
1061 
1062 	return (0);
1063 }
1064 
1065 static int
1066 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1067 {
1068 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1069 	znode_t		*zp;
1070 	uint64_t	object = 0;
1071 	uint64_t	fid_gen = 0;
1072 	uint64_t	gen_mask;
1073 	uint64_t	zp_gen;
1074 	int 		i, err;
1075 
1076 	*vpp = NULL;
1077 
1078 	ZFS_ENTER(zfsvfs);
1079 
1080 	if (fidp->fid_len == LONG_FID_LEN) {
1081 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1082 		uint64_t	objsetid = 0;
1083 		uint64_t	setgen = 0;
1084 
1085 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1086 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1087 
1088 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1089 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1090 
1091 		ZFS_EXIT(zfsvfs);
1092 
1093 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1094 		if (err)
1095 			return (EINVAL);
1096 		ZFS_ENTER(zfsvfs);
1097 	}
1098 
1099 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1100 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1101 
1102 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1103 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1104 
1105 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1106 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1107 	} else {
1108 		ZFS_EXIT(zfsvfs);
1109 		return (EINVAL);
1110 	}
1111 
1112 	/* A zero fid_gen means we are in the .zfs control directories */
1113 	if (fid_gen == 0 &&
1114 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1115 		*vpp = zfsvfs->z_ctldir;
1116 		ASSERT(*vpp != NULL);
1117 		if (object == ZFSCTL_INO_SNAPDIR) {
1118 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1119 			    0, NULL, NULL) == 0);
1120 		} else {
1121 			VN_HOLD(*vpp);
1122 		}
1123 		ZFS_EXIT(zfsvfs);
1124 		return (0);
1125 	}
1126 
1127 	gen_mask = -1ULL >> (64 - 8 * i);
1128 
1129 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1130 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1131 		ZFS_EXIT(zfsvfs);
1132 		return (err);
1133 	}
1134 	zp_gen = zp->z_phys->zp_gen & gen_mask;
1135 	if (zp_gen == 0)
1136 		zp_gen = 1;
1137 	if (zp->z_unlinked || zp_gen != fid_gen) {
1138 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1139 		VN_RELE(ZTOV(zp));
1140 		ZFS_EXIT(zfsvfs);
1141 		return (EINVAL);
1142 	}
1143 
1144 	*vpp = ZTOV(zp);
1145 	ZFS_EXIT(zfsvfs);
1146 	return (0);
1147 }
1148 
1149 static void
1150 zfs_objset_close(zfsvfs_t *zfsvfs)
1151 {
1152 	znode_t		*zp, *nextzp;
1153 	objset_t	*os = zfsvfs->z_os;
1154 
1155 	/*
1156 	 * For forced unmount, at this point all vops except zfs_inactive
1157 	 * are erroring EIO. We need to now suspend zfs_inactive threads
1158 	 * while we are freeing dbufs before switching zfs_inactive
1159 	 * to use behaviour without a objset.
1160 	 */
1161 	rw_enter(&zfsvfs->z_um_lock, RW_WRITER);
1162 
1163 	/*
1164 	 * Release all holds on dbufs
1165 	 * Note, although we have stopped all other vop threads and
1166 	 * zfs_inactive(), the dmu can callback via znode_pageout_func()
1167 	 * which can zfs_znode_free() the znode.
1168 	 * So we lock z_all_znodes; search the list for a held
1169 	 * dbuf; drop the lock (we know zp can't disappear if we hold
1170 	 * a dbuf lock; then regrab the lock and restart.
1171 	 */
1172 	mutex_enter(&zfsvfs->z_znodes_lock);
1173 	for (zp = list_head(&zfsvfs->z_all_znodes); zp; zp = nextzp) {
1174 		nextzp = list_next(&zfsvfs->z_all_znodes, zp);
1175 		if (zp->z_dbuf_held) {
1176 			/* dbufs should only be held when force unmounting */
1177 			zp->z_dbuf_held = 0;
1178 			mutex_exit(&zfsvfs->z_znodes_lock);
1179 			dmu_buf_rele(zp->z_dbuf, NULL);
1180 			/* Start again */
1181 			mutex_enter(&zfsvfs->z_znodes_lock);
1182 			nextzp = list_head(&zfsvfs->z_all_znodes);
1183 		}
1184 	}
1185 	mutex_exit(&zfsvfs->z_znodes_lock);
1186 
1187 	/*
1188 	 * Unregister properties.
1189 	 */
1190 	if (!dmu_objset_is_snapshot(os))
1191 		zfs_unregister_callbacks(zfsvfs);
1192 
1193 	/*
1194 	 * Switch zfs_inactive to behaviour without an objset.
1195 	 * It just tosses cached pages and frees the znode & vnode.
1196 	 * Then re-enable zfs_inactive threads in that new behaviour.
1197 	 */
1198 	zfsvfs->z_unmounted2 = B_TRUE;
1199 	rw_exit(&zfsvfs->z_um_lock); /* re-enable any zfs_inactive threads */
1200 
1201 	/*
1202 	 * Close the zil. Can't close the zil while zfs_inactive
1203 	 * threads are blocked as zil_close can call zfs_inactive.
1204 	 */
1205 	if (zfsvfs->z_log) {
1206 		zil_close(zfsvfs->z_log);
1207 		zfsvfs->z_log = NULL;
1208 	}
1209 
1210 	/*
1211 	 * Evict all dbufs so that cached znodes will be freed
1212 	 */
1213 	if (dmu_objset_evict_dbufs(os, 1)) {
1214 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1215 		(void) dmu_objset_evict_dbufs(os, 0);
1216 	}
1217 
1218 	/*
1219 	 * Finally close the objset
1220 	 */
1221 	dmu_objset_close(os);
1222 
1223 	/*
1224 	 * We can now safely destroy the '.zfs' directory node.
1225 	 */
1226 	if (zfsvfs->z_ctldir != NULL)
1227 		zfsctl_destroy(zfsvfs);
1228 
1229 }
1230 
1231 static void
1232 zfs_freevfs(vfs_t *vfsp)
1233 {
1234 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1235 
1236 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1237 
1238 	atomic_add_32(&zfs_active_fs_count, -1);
1239 }
1240 
1241 /*
1242  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
1243  * so we can't safely do any non-idempotent initialization here.
1244  * Leave that to zfs_init() and zfs_fini(), which are called
1245  * from the module's _init() and _fini() entry points.
1246  */
1247 /*ARGSUSED*/
1248 static int
1249 zfs_vfsinit(int fstype, char *name)
1250 {
1251 	int error;
1252 
1253 	zfsfstype = fstype;
1254 
1255 	/*
1256 	 * Setup vfsops and vnodeops tables.
1257 	 */
1258 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
1259 	if (error != 0) {
1260 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
1261 	}
1262 
1263 	error = zfs_create_op_tables();
1264 	if (error) {
1265 		zfs_remove_op_tables();
1266 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
1267 		(void) vfs_freevfsops_by_type(zfsfstype);
1268 		return (error);
1269 	}
1270 
1271 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
1272 
1273 	/*
1274 	 * Unique major number for all zfs mounts.
1275 	 * If we run out of 32-bit minors, we'll getudev() another major.
1276 	 */
1277 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
1278 	zfs_minor = ZFS_MIN_MINOR;
1279 
1280 	return (0);
1281 }
1282 
1283 void
1284 zfs_init(void)
1285 {
1286 	/*
1287 	 * Initialize .zfs directory structures
1288 	 */
1289 	zfsctl_init();
1290 
1291 	/*
1292 	 * Initialize znode cache, vnode ops, etc...
1293 	 */
1294 	zfs_znode_init();
1295 }
1296 
1297 void
1298 zfs_fini(void)
1299 {
1300 	zfsctl_fini();
1301 	zfs_znode_fini();
1302 }
1303 
1304 int
1305 zfs_busy(void)
1306 {
1307 	return (zfs_active_fs_count != 0);
1308 }
1309 
1310 static vfsdef_t vfw = {
1311 	VFSDEF_VERSION,
1312 	MNTTYPE_ZFS,
1313 	zfs_vfsinit,
1314 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS,
1315 	&zfs_mntopts
1316 };
1317 
1318 struct modlfs zfs_modlfs = {
1319 	&mod_fsops, "ZFS filesystem version " ZFS_VERSION_STRING, &vfw
1320 };
1321