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