xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision ecd6cf800b63704be73fb264c3f5b6e0dafc068d)
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/vnode.h>
35 #include <sys/vfs.h>
36 #include <sys/vfs_opreg.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/dsl_dataset.h>
48 #include <sys/dsl_deleg.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/refstr.h>
57 #include <sys/zfs_ioctl.h>
58 #include <sys/zfs_ctldir.h>
59 #include <sys/bootconf.h>
60 #include <sys/sunddi.h>
61 #include <sys/dnlc.h>
62 
63 int zfsfstype;
64 vfsops_t *zfs_vfsops = NULL;
65 static major_t zfs_major;
66 static minor_t zfs_minor;
67 static kmutex_t	zfs_dev_mtx;
68 
69 static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
70 static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr);
71 static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
72 static int zfs_root(vfs_t *vfsp, vnode_t **vpp);
73 static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp);
74 static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp);
75 static void zfs_freevfs(vfs_t *vfsp);
76 static void zfs_objset_close(zfsvfs_t *zfsvfs);
77 
78 static const fs_operation_def_t zfs_vfsops_template[] = {
79 	VFSNAME_MOUNT,		{ .vfs_mount = zfs_mount },
80 	VFSNAME_MOUNTROOT,	{ .vfs_mountroot = zfs_mountroot },
81 	VFSNAME_UNMOUNT,	{ .vfs_unmount = zfs_umount },
82 	VFSNAME_ROOT,		{ .vfs_root = zfs_root },
83 	VFSNAME_STATVFS,	{ .vfs_statvfs = zfs_statvfs },
84 	VFSNAME_SYNC,		{ .vfs_sync = zfs_sync },
85 	VFSNAME_VGET,		{ .vfs_vget = zfs_vget },
86 	VFSNAME_FREEVFS,	{ .vfs_freevfs = zfs_freevfs },
87 	NULL,			NULL
88 };
89 
90 static const fs_operation_def_t zfs_vfsops_eio_template[] = {
91 	VFSNAME_FREEVFS,	{ .vfs_freevfs =  zfs_freevfs },
92 	NULL,			NULL
93 };
94 
95 /*
96  * We need to keep a count of active fs's.
97  * This is necessary to prevent our module
98  * from being unloaded after a umount -f
99  */
100 static uint32_t	zfs_active_fs_count = 0;
101 
102 static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
103 static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
104 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
105 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
106 
107 /*
108  * MNTOPT_DEFAULT was removed from MNTOPT_XATTR, since the
109  * default value is now determined by the xattr property.
110  */
111 static mntopt_t mntopts[] = {
112 	{ MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL },
113 	{ MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL },
114 	{ MNTOPT_NOATIME, noatime_cancel, NULL, MO_DEFAULT, NULL },
115 	{ MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
116 };
117 
118 static mntopts_t zfs_mntopts = {
119 	sizeof (mntopts) / sizeof (mntopt_t),
120 	mntopts
121 };
122 
123 /*ARGSUSED*/
124 int
125 zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
126 {
127 	/*
128 	 * Data integrity is job one.  We don't want a compromised kernel
129 	 * writing to the storage pool, so we never sync during panic.
130 	 */
131 	if (panicstr)
132 		return (0);
133 
134 	/*
135 	 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
136 	 * to sync metadata, which they would otherwise cache indefinitely.
137 	 * Semantically, the only requirement is that the sync be initiated.
138 	 * The DMU syncs out txgs frequently, so there's nothing to do.
139 	 */
140 	if (flag & SYNC_ATTR)
141 		return (0);
142 
143 	if (vfsp != NULL) {
144 		/*
145 		 * Sync a specific filesystem.
146 		 */
147 		zfsvfs_t *zfsvfs = vfsp->vfs_data;
148 
149 		ZFS_ENTER(zfsvfs);
150 		if (zfsvfs->z_log != NULL)
151 			zil_commit(zfsvfs->z_log, UINT64_MAX, 0);
152 		else
153 			txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
154 		ZFS_EXIT(zfsvfs);
155 	} else {
156 		/*
157 		 * Sync all ZFS filesystems.  This is what happens when you
158 		 * run sync(1M).  Unlike other filesystems, ZFS honors the
159 		 * request by waiting for all pools to commit all dirty data.
160 		 */
161 		spa_sync_allpools();
162 	}
163 
164 	return (0);
165 }
166 
167 static int
168 zfs_create_unique_device(dev_t *dev)
169 {
170 	major_t new_major;
171 
172 	do {
173 		ASSERT3U(zfs_minor, <=, MAXMIN32);
174 		minor_t start = zfs_minor;
175 		do {
176 			mutex_enter(&zfs_dev_mtx);
177 			if (zfs_minor >= MAXMIN32) {
178 				/*
179 				 * If we're still using the real major
180 				 * keep out of /dev/zfs and /dev/zvol minor
181 				 * number space.  If we're using a getudev()'ed
182 				 * major number, we can use all of its minors.
183 				 */
184 				if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
185 					zfs_minor = ZFS_MIN_MINOR;
186 				else
187 					zfs_minor = 0;
188 			} else {
189 				zfs_minor++;
190 			}
191 			*dev = makedevice(zfs_major, zfs_minor);
192 			mutex_exit(&zfs_dev_mtx);
193 		} while (vfs_devismounted(*dev) && zfs_minor != start);
194 		if (zfs_minor == start) {
195 			/*
196 			 * We are using all ~262,000 minor numbers for the
197 			 * current major number.  Create a new major number.
198 			 */
199 			if ((new_major = getudev()) == (major_t)-1) {
200 				cmn_err(CE_WARN,
201 				    "zfs_mount: Can't get unique major "
202 				    "device number.");
203 				return (-1);
204 			}
205 			mutex_enter(&zfs_dev_mtx);
206 			zfs_major = new_major;
207 			zfs_minor = 0;
208 
209 			mutex_exit(&zfs_dev_mtx);
210 		} else {
211 			break;
212 		}
213 		/* CONSTANTCONDITION */
214 	} while (1);
215 
216 	return (0);
217 }
218 
219 static void
220 atime_changed_cb(void *arg, uint64_t newval)
221 {
222 	zfsvfs_t *zfsvfs = arg;
223 
224 	if (newval == TRUE) {
225 		zfsvfs->z_atime = TRUE;
226 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
227 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
228 	} else {
229 		zfsvfs->z_atime = FALSE;
230 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
231 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
232 	}
233 }
234 
235 static void
236 xattr_changed_cb(void *arg, uint64_t newval)
237 {
238 	zfsvfs_t *zfsvfs = arg;
239 
240 	if (newval == TRUE) {
241 		/* XXX locking on vfs_flag? */
242 		zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
243 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
244 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
245 	} else {
246 		/* XXX locking on vfs_flag? */
247 		zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
248 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
249 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
250 	}
251 }
252 
253 static void
254 blksz_changed_cb(void *arg, uint64_t newval)
255 {
256 	zfsvfs_t *zfsvfs = arg;
257 
258 	if (newval < SPA_MINBLOCKSIZE ||
259 	    newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
260 		newval = SPA_MAXBLOCKSIZE;
261 
262 	zfsvfs->z_max_blksz = newval;
263 	zfsvfs->z_vfs->vfs_bsize = newval;
264 }
265 
266 static void
267 readonly_changed_cb(void *arg, uint64_t newval)
268 {
269 	zfsvfs_t *zfsvfs = arg;
270 
271 	if (newval) {
272 		/* XXX locking on vfs_flag? */
273 		zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
274 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
275 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
276 	} else {
277 		/* XXX locking on vfs_flag? */
278 		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
279 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
280 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
281 	}
282 }
283 
284 static void
285 devices_changed_cb(void *arg, uint64_t newval)
286 {
287 	zfsvfs_t *zfsvfs = arg;
288 
289 	if (newval == FALSE) {
290 		zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
291 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
292 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
293 	} else {
294 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
295 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
296 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
297 	}
298 }
299 
300 static void
301 setuid_changed_cb(void *arg, uint64_t newval)
302 {
303 	zfsvfs_t *zfsvfs = arg;
304 
305 	if (newval == FALSE) {
306 		zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
307 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
308 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
309 	} else {
310 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
311 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
312 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
313 	}
314 }
315 
316 static void
317 exec_changed_cb(void *arg, uint64_t newval)
318 {
319 	zfsvfs_t *zfsvfs = arg;
320 
321 	if (newval == FALSE) {
322 		zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
323 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
324 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
325 	} else {
326 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
327 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
328 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
329 	}
330 }
331 
332 static void
333 snapdir_changed_cb(void *arg, uint64_t newval)
334 {
335 	zfsvfs_t *zfsvfs = arg;
336 
337 	zfsvfs->z_show_ctldir = newval;
338 }
339 
340 static void
341 acl_mode_changed_cb(void *arg, uint64_t newval)
342 {
343 	zfsvfs_t *zfsvfs = arg;
344 
345 	zfsvfs->z_acl_mode = newval;
346 }
347 
348 static void
349 acl_inherit_changed_cb(void *arg, uint64_t newval)
350 {
351 	zfsvfs_t *zfsvfs = arg;
352 
353 	zfsvfs->z_acl_inherit = newval;
354 }
355 
356 static int
357 zfs_refresh_properties(vfs_t *vfsp)
358 {
359 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
360 
361 	/*
362 	 * Remount operations default to "rw" unless "ro" is explicitly
363 	 * specified.
364 	 */
365 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
366 		readonly_changed_cb(zfsvfs, B_TRUE);
367 	} else {
368 		if (!dmu_objset_is_snapshot(zfsvfs->z_os))
369 			readonly_changed_cb(zfsvfs, B_FALSE);
370 		else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL))
371 			return (EROFS);
372 	}
373 
374 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
375 		devices_changed_cb(zfsvfs, B_FALSE);
376 		setuid_changed_cb(zfsvfs, B_FALSE);
377 	} else {
378 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
379 			devices_changed_cb(zfsvfs, B_FALSE);
380 		else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL))
381 			devices_changed_cb(zfsvfs, B_TRUE);
382 
383 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))
384 			setuid_changed_cb(zfsvfs, B_FALSE);
385 		else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL))
386 			setuid_changed_cb(zfsvfs, B_TRUE);
387 	}
388 
389 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL))
390 		exec_changed_cb(zfsvfs, B_FALSE);
391 	else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL))
392 		exec_changed_cb(zfsvfs, B_TRUE);
393 
394 	if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL))
395 		atime_changed_cb(zfsvfs, B_TRUE);
396 	else if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL))
397 		atime_changed_cb(zfsvfs, B_FALSE);
398 
399 	if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL))
400 		xattr_changed_cb(zfsvfs, B_TRUE);
401 	else if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL))
402 		xattr_changed_cb(zfsvfs, B_FALSE);
403 
404 	return (0);
405 }
406 
407 static int
408 zfs_register_callbacks(vfs_t *vfsp)
409 {
410 	struct dsl_dataset *ds = NULL;
411 	objset_t *os = NULL;
412 	zfsvfs_t *zfsvfs = NULL;
413 	int readonly, do_readonly = FALSE;
414 	int setuid, do_setuid = FALSE;
415 	int exec, do_exec = FALSE;
416 	int devices, do_devices = FALSE;
417 	int xattr, do_xattr = FALSE;
418 	int error = 0;
419 
420 	ASSERT(vfsp);
421 	zfsvfs = vfsp->vfs_data;
422 	ASSERT(zfsvfs);
423 	os = zfsvfs->z_os;
424 
425 	/*
426 	 * The act of registering our callbacks will destroy any mount
427 	 * options we may have.  In order to enable temporary overrides
428 	 * of mount options, we stash away the current values and
429 	 * restore them after we register the callbacks.
430 	 */
431 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
432 		readonly = B_TRUE;
433 		do_readonly = B_TRUE;
434 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
435 		readonly = B_FALSE;
436 		do_readonly = B_TRUE;
437 	}
438 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
439 		devices = B_FALSE;
440 		setuid = B_FALSE;
441 		do_devices = B_TRUE;
442 		do_setuid = B_TRUE;
443 	} else {
444 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
445 			devices = B_FALSE;
446 			do_devices = B_TRUE;
447 		} else if (vfs_optionisset(vfsp, 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 /*
702  * Convert a decimal digit string to a uint64_t integer.
703  */
704 static int
705 str_to_uint64(char *str, uint64_t *objnum)
706 {
707 	uint64_t num = 0;
708 
709 	while (*str) {
710 		if (*str < '0' || *str > '9')
711 			return (EINVAL);
712 
713 		num = num*10 + *str++ - '0';
714 	}
715 
716 	*objnum = num;
717 	return (0);
718 }
719 
720 
721 /*
722  * The boot path passed from the boot loader is in the form of
723  * "rootpool-name/root-filesystem-object-number'. Convert this
724  * string to a dataset name: "rootpool-name/root-filesystem-name".
725  */
726 static int
727 parse_bootpath(char *bpath, char *outpath)
728 {
729 	char *slashp;
730 	uint64_t objnum;
731 	int error;
732 
733 	if (*bpath == 0 || *bpath == '/')
734 		return (EINVAL);
735 
736 	slashp = strchr(bpath, '/');
737 
738 	/* if no '/', just return the pool name */
739 	if (slashp == NULL) {
740 		(void) strcpy(outpath, bpath);
741 		return (0);
742 	}
743 
744 	if (error = str_to_uint64(slashp+1, &objnum))
745 		return (error);
746 
747 	*slashp = '\0';
748 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
749 	*slashp = '/';
750 
751 	return (error);
752 }
753 
754 static int
755 zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
756 {
757 	int error = 0;
758 	int ret = 0;
759 	static int zfsrootdone = 0;
760 	zfsvfs_t *zfsvfs = NULL;
761 	znode_t *zp = NULL;
762 	vnode_t *vp = NULL;
763 	char *zfs_bootpath;
764 
765 	ASSERT(vfsp);
766 
767 	/*
768 	 * The filesystem that we mount as root is defined in the
769 	 * "zfs-bootfs" property.
770 	 */
771 	if (why == ROOT_INIT) {
772 		if (zfsrootdone++)
773 			return (EBUSY);
774 
775 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
776 		    DDI_PROP_DONTPASS, "zfs-bootfs", &zfs_bootpath) !=
777 		    DDI_SUCCESS)
778 			return (EIO);
779 
780 		error = parse_bootpath(zfs_bootpath, rootfs.bo_name);
781 		ddi_prop_free(zfs_bootpath);
782 
783 		if (error)
784 			return (error);
785 
786 		if (error = vfs_lock(vfsp))
787 			return (error);
788 
789 		if (error = zfs_domount(vfsp, rootfs.bo_name, CRED()))
790 			goto out;
791 
792 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
793 		ASSERT(zfsvfs);
794 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp))
795 			goto out;
796 
797 		vp = ZTOV(zp);
798 		mutex_enter(&vp->v_lock);
799 		vp->v_flag |= VROOT;
800 		mutex_exit(&vp->v_lock);
801 		rootvp = vp;
802 
803 		/*
804 		 * The zfs_zget call above returns with a hold on vp, we release
805 		 * it here.
806 		 */
807 		VN_RELE(vp);
808 
809 		/*
810 		 * Mount root as readonly initially, it will be remouted
811 		 * read/write by /lib/svc/method/fs-usr.
812 		 */
813 		readonly_changed_cb(vfsp->vfs_data, B_TRUE);
814 		vfs_add((struct vnode *)0, vfsp,
815 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
816 out:
817 		vfs_unlock(vfsp);
818 		ret = (error) ? error : 0;
819 		return (ret);
820 
821 	} else if (why == ROOT_REMOUNT) {
822 
823 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
824 		vfsp->vfs_flag |= VFS_REMOUNT;
825 		return (zfs_refresh_properties(vfsp));
826 
827 	} else if (why == ROOT_UNMOUNT) {
828 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
829 		(void) zfs_sync(vfsp, 0, 0);
830 		return (0);
831 	}
832 
833 	/*
834 	 * if "why" is equal to anything else other than ROOT_INIT,
835 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
836 	 */
837 	return (ENOTSUP);
838 }
839 
840 /*ARGSUSED*/
841 static int
842 zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
843 {
844 	char		*osname;
845 	pathname_t	spn;
846 	int		error = 0;
847 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
848 	    UIO_SYSSPACE : UIO_USERSPACE;
849 	int		canwrite;
850 
851 	if (mvp->v_type != VDIR)
852 		return (ENOTDIR);
853 
854 	mutex_enter(&mvp->v_lock);
855 	if ((uap->flags & MS_REMOUNT) == 0 &&
856 	    (uap->flags & MS_OVERLAY) == 0 &&
857 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
858 		mutex_exit(&mvp->v_lock);
859 		return (EBUSY);
860 	}
861 	mutex_exit(&mvp->v_lock);
862 
863 	/*
864 	 * ZFS does not support passing unparsed data in via MS_DATA.
865 	 * Users should use the MS_OPTIONSTR interface; this means
866 	 * that all option parsing is already done and the options struct
867 	 * can be interrogated.
868 	 */
869 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
870 		return (EINVAL);
871 
872 	/*
873 	 * When doing a remount, we simply refresh our temporary properties
874 	 * according to those options set in the current VFS options.
875 	 */
876 	if (uap->flags & MS_REMOUNT) {
877 		return (zfs_refresh_properties(vfsp));
878 	}
879 
880 	/*
881 	 * Get the objset name (the "special" mount argument).
882 	 */
883 	if (error = pn_get(uap->spec, fromspace, &spn))
884 		return (error);
885 
886 	osname = spn.pn_path;
887 
888 	/*
889 	 * Check for mount privilege?
890 	 *
891 	 * If we don't have privilege then see if
892 	 * we have local permission to allow it
893 	 */
894 	error = secpolicy_fs_mount(cr, mvp, vfsp);
895 	if (error) {
896 		error = dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr);
897 		if (error == 0) {
898 			vattr_t		vattr;
899 
900 			/*
901 			 * Make sure user is the owner of the mount point
902 			 * or has sufficient privileges.
903 			 */
904 
905 			vattr.va_mask = AT_UID;
906 
907 			if (VOP_GETATTR(mvp, &vattr, 0, cr)) {
908 				goto out;
909 			}
910 
911 			if (error = secpolicy_vnode_owner(cr, vattr.va_uid)) {
912 				goto out;
913 			}
914 
915 			if (error = VOP_ACCESS(mvp, VWRITE, 0, cr)) {
916 				goto out;
917 			}
918 
919 			secpolicy_fs_mount_clearopts(cr, vfsp);
920 		} else {
921 			goto out;
922 		}
923 	}
924 
925 	/*
926 	 * Refuse to mount a filesystem if we are in a local zone and the
927 	 * dataset is not visible.
928 	 */
929 	if (!INGLOBALZONE(curproc) &&
930 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
931 		error = EPERM;
932 		goto out;
933 	}
934 
935 	error = zfs_domount(vfsp, osname, cr);
936 
937 out:
938 	pn_free(&spn);
939 	return (error);
940 }
941 
942 static int
943 zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
944 {
945 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
946 	dev32_t d32;
947 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
948 
949 	ZFS_ENTER(zfsvfs);
950 
951 	dmu_objset_space(zfsvfs->z_os,
952 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
953 
954 	/*
955 	 * The underlying storage pool actually uses multiple block sizes.
956 	 * We report the fragsize as the smallest block size we support,
957 	 * and we report our blocksize as the filesystem's maximum blocksize.
958 	 */
959 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
960 	statp->f_bsize = zfsvfs->z_max_blksz;
961 
962 	/*
963 	 * The following report "total" blocks of various kinds in the
964 	 * file system, but reported in terms of f_frsize - the
965 	 * "fragment" size.
966 	 */
967 
968 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
969 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
970 	statp->f_bavail = statp->f_bfree; /* no root reservation */
971 
972 	/*
973 	 * statvfs() should really be called statufs(), because it assumes
974 	 * static metadata.  ZFS doesn't preallocate files, so the best
975 	 * we can do is report the max that could possibly fit in f_files,
976 	 * and that minus the number actually used in f_ffree.
977 	 * For f_ffree, report the smaller of the number of object available
978 	 * and the number of blocks (each object will take at least a block).
979 	 */
980 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
981 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
982 	statp->f_files = statp->f_ffree + usedobjs;
983 
984 	(void) cmpldev(&d32, vfsp->vfs_dev);
985 	statp->f_fsid = d32;
986 
987 	/*
988 	 * We're a zfs filesystem.
989 	 */
990 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
991 
992 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
993 
994 	statp->f_namemax = ZFS_MAXNAMELEN;
995 
996 	/*
997 	 * We have all of 32 characters to stuff a string here.
998 	 * Is there anything useful we could/should provide?
999 	 */
1000 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
1001 
1002 	ZFS_EXIT(zfsvfs);
1003 	return (0);
1004 }
1005 
1006 static int
1007 zfs_root(vfs_t *vfsp, vnode_t **vpp)
1008 {
1009 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1010 	znode_t *rootzp;
1011 	int error;
1012 
1013 	ZFS_ENTER(zfsvfs);
1014 
1015 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1016 	if (error == 0)
1017 		*vpp = ZTOV(rootzp);
1018 
1019 	ZFS_EXIT(zfsvfs);
1020 	return (error);
1021 }
1022 
1023 /*ARGSUSED*/
1024 static int
1025 zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1026 {
1027 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1028 	int ret;
1029 
1030 	ret = secpolicy_fs_unmount(cr, vfsp);
1031 	if (ret) {
1032 		ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
1033 		    ZFS_DELEG_PERM_MOUNT, cr);
1034 		if (ret)
1035 			return (ret);
1036 	}
1037 
1038 	(void) dnlc_purge_vfsp(vfsp, 0);
1039 
1040 	/*
1041 	 * Unmount any snapshots mounted under .zfs before unmounting the
1042 	 * dataset itself.
1043 	 */
1044 	if (zfsvfs->z_ctldir != NULL &&
1045 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1046 		return (ret);
1047 	}
1048 
1049 	if (fflag & MS_FORCE) {
1050 		vfsp->vfs_flag |= VFS_UNMOUNTED;
1051 		zfsvfs->z_unmounted1 = B_TRUE;
1052 
1053 		/*
1054 		 * Ensure that z_unmounted1 reaches global visibility
1055 		 * before z_op_cnt.
1056 		 */
1057 		membar_producer();
1058 
1059 		/*
1060 		 * Wait for all zfs threads to leave zfs.
1061 		 * Grabbing a rwlock as reader in all vops and
1062 		 * as writer here doesn't work because it too easy to get
1063 		 * multiple reader enters as zfs can re-enter itself.
1064 		 * This can lead to deadlock if there is an intervening
1065 		 * rw_enter as writer.
1066 		 * So a file system threads ref count (z_op_cnt) is used.
1067 		 * A polling loop on z_op_cnt may seem inefficient, but
1068 		 * - this saves all threads on exit from having to grab a
1069 		 *   mutex in order to cv_signal
1070 		 * - only occurs on forced unmount in the rare case when
1071 		 *   there are outstanding threads within the file system.
1072 		 */
1073 		while (zfsvfs->z_op_cnt) {
1074 			delay(1);
1075 		}
1076 
1077 		zfs_objset_close(zfsvfs);
1078 
1079 		return (0);
1080 	}
1081 	/*
1082 	 * Check the number of active vnodes in the file system.
1083 	 * Our count is maintained in the vfs structure, but the number
1084 	 * is off by 1 to indicate a hold on the vfs structure itself.
1085 	 *
1086 	 * The '.zfs' directory maintains a reference of its own, and any active
1087 	 * references underneath are reflected in the vnode count.
1088 	 */
1089 	if (zfsvfs->z_ctldir == NULL) {
1090 		if (vfsp->vfs_count > 1)
1091 			return (EBUSY);
1092 	} else {
1093 		if (vfsp->vfs_count > 2 ||
1094 		    (zfsvfs->z_ctldir->v_count > 1 && !(fflag & MS_FORCE))) {
1095 			return (EBUSY);
1096 		}
1097 	}
1098 
1099 	vfsp->vfs_flag |= VFS_UNMOUNTED;
1100 	zfs_objset_close(zfsvfs);
1101 
1102 	return (0);
1103 }
1104 
1105 static int
1106 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1107 {
1108 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1109 	znode_t		*zp;
1110 	uint64_t	object = 0;
1111 	uint64_t	fid_gen = 0;
1112 	uint64_t	gen_mask;
1113 	uint64_t	zp_gen;
1114 	int 		i, err;
1115 
1116 	*vpp = NULL;
1117 
1118 	ZFS_ENTER(zfsvfs);
1119 
1120 	if (fidp->fid_len == LONG_FID_LEN) {
1121 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1122 		uint64_t	objsetid = 0;
1123 		uint64_t	setgen = 0;
1124 
1125 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1126 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1127 
1128 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1129 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1130 
1131 		ZFS_EXIT(zfsvfs);
1132 
1133 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1134 		if (err)
1135 			return (EINVAL);
1136 		ZFS_ENTER(zfsvfs);
1137 	}
1138 
1139 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1140 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1141 
1142 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1143 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1144 
1145 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1146 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1147 	} else {
1148 		ZFS_EXIT(zfsvfs);
1149 		return (EINVAL);
1150 	}
1151 
1152 	/* A zero fid_gen means we are in the .zfs control directories */
1153 	if (fid_gen == 0 &&
1154 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1155 		*vpp = zfsvfs->z_ctldir;
1156 		ASSERT(*vpp != NULL);
1157 		if (object == ZFSCTL_INO_SNAPDIR) {
1158 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1159 			    0, NULL, NULL) == 0);
1160 		} else {
1161 			VN_HOLD(*vpp);
1162 		}
1163 		ZFS_EXIT(zfsvfs);
1164 		return (0);
1165 	}
1166 
1167 	gen_mask = -1ULL >> (64 - 8 * i);
1168 
1169 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1170 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1171 		ZFS_EXIT(zfsvfs);
1172 		return (err);
1173 	}
1174 	zp_gen = zp->z_phys->zp_gen & gen_mask;
1175 	if (zp_gen == 0)
1176 		zp_gen = 1;
1177 	if (zp->z_unlinked || zp_gen != fid_gen) {
1178 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1179 		VN_RELE(ZTOV(zp));
1180 		ZFS_EXIT(zfsvfs);
1181 		return (EINVAL);
1182 	}
1183 
1184 	*vpp = ZTOV(zp);
1185 	ZFS_EXIT(zfsvfs);
1186 	return (0);
1187 }
1188 
1189 static void
1190 zfs_objset_close(zfsvfs_t *zfsvfs)
1191 {
1192 	znode_t		*zp, *nextzp;
1193 	objset_t	*os = zfsvfs->z_os;
1194 
1195 	/*
1196 	 * For forced unmount, at this point all vops except zfs_inactive
1197 	 * are erroring EIO. We need to now suspend zfs_inactive threads
1198 	 * while we are freeing dbufs before switching zfs_inactive
1199 	 * to use behaviour without a objset.
1200 	 */
1201 	rw_enter(&zfsvfs->z_um_lock, RW_WRITER);
1202 
1203 	/*
1204 	 * Release all holds on dbufs
1205 	 * Note, although we have stopped all other vop threads and
1206 	 * zfs_inactive(), the dmu can callback via znode_pageout_func()
1207 	 * which can zfs_znode_free() the znode.
1208 	 * So we lock z_all_znodes; search the list for a held
1209 	 * dbuf; drop the lock (we know zp can't disappear if we hold
1210 	 * a dbuf lock; then regrab the lock and restart.
1211 	 */
1212 	mutex_enter(&zfsvfs->z_znodes_lock);
1213 	for (zp = list_head(&zfsvfs->z_all_znodes); zp; zp = nextzp) {
1214 		nextzp = list_next(&zfsvfs->z_all_znodes, zp);
1215 		if (zp->z_dbuf_held) {
1216 			/* dbufs should only be held when force unmounting */
1217 			zp->z_dbuf_held = 0;
1218 			mutex_exit(&zfsvfs->z_znodes_lock);
1219 			dmu_buf_rele(zp->z_dbuf, NULL);
1220 			/* Start again */
1221 			mutex_enter(&zfsvfs->z_znodes_lock);
1222 			nextzp = list_head(&zfsvfs->z_all_znodes);
1223 		}
1224 	}
1225 	mutex_exit(&zfsvfs->z_znodes_lock);
1226 
1227 	/*
1228 	 * Unregister properties.
1229 	 */
1230 	if (!dmu_objset_is_snapshot(os))
1231 		zfs_unregister_callbacks(zfsvfs);
1232 
1233 	/*
1234 	 * Switch zfs_inactive to behaviour without an objset.
1235 	 * It just tosses cached pages and frees the znode & vnode.
1236 	 * Then re-enable zfs_inactive threads in that new behaviour.
1237 	 */
1238 	zfsvfs->z_unmounted2 = B_TRUE;
1239 	rw_exit(&zfsvfs->z_um_lock); /* re-enable any zfs_inactive threads */
1240 
1241 	/*
1242 	 * Close the zil. Can't close the zil while zfs_inactive
1243 	 * threads are blocked as zil_close can call zfs_inactive.
1244 	 */
1245 	if (zfsvfs->z_log) {
1246 		zil_close(zfsvfs->z_log);
1247 		zfsvfs->z_log = NULL;
1248 	}
1249 
1250 	/*
1251 	 * Evict all dbufs so that cached znodes will be freed
1252 	 */
1253 	if (dmu_objset_evict_dbufs(os, 1)) {
1254 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1255 		(void) dmu_objset_evict_dbufs(os, 0);
1256 	}
1257 
1258 	/*
1259 	 * Finally close the objset
1260 	 */
1261 	dmu_objset_close(os);
1262 
1263 	/*
1264 	 * We can now safely destroy the '.zfs' directory node.
1265 	 */
1266 	if (zfsvfs->z_ctldir != NULL)
1267 		zfsctl_destroy(zfsvfs);
1268 
1269 }
1270 
1271 static void
1272 zfs_freevfs(vfs_t *vfsp)
1273 {
1274 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1275 
1276 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1277 
1278 	atomic_add_32(&zfs_active_fs_count, -1);
1279 }
1280 
1281 /*
1282  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
1283  * so we can't safely do any non-idempotent initialization here.
1284  * Leave that to zfs_init() and zfs_fini(), which are called
1285  * from the module's _init() and _fini() entry points.
1286  */
1287 /*ARGSUSED*/
1288 static int
1289 zfs_vfsinit(int fstype, char *name)
1290 {
1291 	int error;
1292 
1293 	zfsfstype = fstype;
1294 
1295 	/*
1296 	 * Setup vfsops and vnodeops tables.
1297 	 */
1298 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
1299 	if (error != 0) {
1300 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
1301 	}
1302 
1303 	error = zfs_create_op_tables();
1304 	if (error) {
1305 		zfs_remove_op_tables();
1306 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
1307 		(void) vfs_freevfsops_by_type(zfsfstype);
1308 		return (error);
1309 	}
1310 
1311 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
1312 
1313 	/*
1314 	 * Unique major number for all zfs mounts.
1315 	 * If we run out of 32-bit minors, we'll getudev() another major.
1316 	 */
1317 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
1318 	zfs_minor = ZFS_MIN_MINOR;
1319 
1320 	return (0);
1321 }
1322 
1323 void
1324 zfs_init(void)
1325 {
1326 	/*
1327 	 * Initialize .zfs directory structures
1328 	 */
1329 	zfsctl_init();
1330 
1331 	/*
1332 	 * Initialize znode cache, vnode ops, etc...
1333 	 */
1334 	zfs_znode_init();
1335 }
1336 
1337 void
1338 zfs_fini(void)
1339 {
1340 	zfsctl_fini();
1341 	zfs_znode_fini();
1342 }
1343 
1344 int
1345 zfs_busy(void)
1346 {
1347 	return (zfs_active_fs_count != 0);
1348 }
1349 
1350 static vfsdef_t vfw = {
1351 	VFSDEF_VERSION,
1352 	MNTTYPE_ZFS,
1353 	zfs_vfsinit,
1354 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS,
1355 	&zfs_mntopts
1356 };
1357 
1358 struct modlfs zfs_modlfs = {
1359 	&mod_fsops, "ZFS filesystem version " ZFS_VERSION_STRING, &vfw
1360 };
1361