xref: /titanic_50/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 44743693dce3212f5edba623e0cb0327bd4337a3)
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 
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  * MO_DEFAULT is not used since the default value is determined
108  * by the equivalent 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, 0, 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_register_callbacks(vfs_t *vfsp)
357 {
358 	struct dsl_dataset *ds = NULL;
359 	objset_t *os = NULL;
360 	zfsvfs_t *zfsvfs = NULL;
361 	int readonly, do_readonly = FALSE;
362 	int setuid, do_setuid = FALSE;
363 	int exec, do_exec = FALSE;
364 	int devices, do_devices = FALSE;
365 	int xattr, do_xattr = FALSE;
366 	int atime, do_atime = FALSE;
367 	int error = 0;
368 
369 	ASSERT(vfsp);
370 	zfsvfs = vfsp->vfs_data;
371 	ASSERT(zfsvfs);
372 	os = zfsvfs->z_os;
373 
374 	/*
375 	 * The act of registering our callbacks will destroy any mount
376 	 * options we may have.  In order to enable temporary overrides
377 	 * of mount options, we stash away the current values and
378 	 * restore them after we register the callbacks.
379 	 */
380 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
381 		readonly = B_TRUE;
382 		do_readonly = B_TRUE;
383 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
384 		readonly = B_FALSE;
385 		do_readonly = B_TRUE;
386 	}
387 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
388 		devices = B_FALSE;
389 		setuid = B_FALSE;
390 		do_devices = B_TRUE;
391 		do_setuid = B_TRUE;
392 	} else {
393 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
394 			devices = B_FALSE;
395 			do_devices = B_TRUE;
396 		} else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
397 			devices = B_TRUE;
398 			do_devices = B_TRUE;
399 		}
400 
401 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
402 			setuid = B_FALSE;
403 			do_setuid = B_TRUE;
404 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
405 			setuid = B_TRUE;
406 			do_setuid = B_TRUE;
407 		}
408 	}
409 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
410 		exec = B_FALSE;
411 		do_exec = B_TRUE;
412 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
413 		exec = B_TRUE;
414 		do_exec = B_TRUE;
415 	}
416 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
417 		xattr = B_FALSE;
418 		do_xattr = B_TRUE;
419 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
420 		xattr = B_TRUE;
421 		do_xattr = B_TRUE;
422 	}
423 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
424 		atime = B_FALSE;
425 		do_atime = B_TRUE;
426 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
427 		atime = B_TRUE;
428 		do_atime = B_TRUE;
429 	}
430 
431 	/*
432 	 * Register property callbacks.
433 	 *
434 	 * It would probably be fine to just check for i/o error from
435 	 * the first prop_register(), but I guess I like to go
436 	 * overboard...
437 	 */
438 	ds = dmu_objset_ds(os);
439 	error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
440 	error = error ? error : dsl_prop_register(ds,
441 	    "xattr", xattr_changed_cb, zfsvfs);
442 	error = error ? error : dsl_prop_register(ds,
443 	    "recordsize", blksz_changed_cb, zfsvfs);
444 	error = error ? error : dsl_prop_register(ds,
445 	    "readonly", readonly_changed_cb, zfsvfs);
446 	error = error ? error : dsl_prop_register(ds,
447 	    "devices", devices_changed_cb, zfsvfs);
448 	error = error ? error : dsl_prop_register(ds,
449 	    "setuid", setuid_changed_cb, zfsvfs);
450 	error = error ? error : dsl_prop_register(ds,
451 	    "exec", exec_changed_cb, zfsvfs);
452 	error = error ? error : dsl_prop_register(ds,
453 	    "snapdir", snapdir_changed_cb, zfsvfs);
454 	error = error ? error : dsl_prop_register(ds,
455 	    "aclmode", acl_mode_changed_cb, zfsvfs);
456 	error = error ? error : dsl_prop_register(ds,
457 	    "aclinherit", acl_inherit_changed_cb, zfsvfs);
458 	if (error)
459 		goto unregister;
460 
461 	/*
462 	 * Invoke our callbacks to restore temporary mount options.
463 	 */
464 	if (do_readonly)
465 		readonly_changed_cb(zfsvfs, readonly);
466 	if (do_setuid)
467 		setuid_changed_cb(zfsvfs, setuid);
468 	if (do_exec)
469 		exec_changed_cb(zfsvfs, exec);
470 	if (do_devices)
471 		devices_changed_cb(zfsvfs, devices);
472 	if (do_xattr)
473 		xattr_changed_cb(zfsvfs, xattr);
474 	if (do_atime)
475 		atime_changed_cb(zfsvfs, atime);
476 
477 	return (0);
478 
479 unregister:
480 	/*
481 	 * We may attempt to unregister some callbacks that are not
482 	 * registered, but this is OK; it will simply return ENOMSG,
483 	 * which we will ignore.
484 	 */
485 	(void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
486 	(void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
487 	(void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
488 	(void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
489 	(void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs);
490 	(void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
491 	(void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
492 	(void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
493 	(void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
494 	(void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
495 	    zfsvfs);
496 	return (error);
497 
498 }
499 
500 static int
501 zfs_domount(vfs_t *vfsp, char *osname, cred_t *cr)
502 {
503 	dev_t mount_dev;
504 	uint64_t recordsize, readonly;
505 	int error = 0;
506 	int mode;
507 	zfsvfs_t *zfsvfs;
508 	znode_t *zp = NULL;
509 
510 	ASSERT(vfsp);
511 	ASSERT(osname);
512 
513 	/*
514 	 * Initialize the zfs-specific filesystem structure.
515 	 * Should probably make this a kmem cache, shuffle fields,
516 	 * and just bzero up to z_hold_mtx[].
517 	 */
518 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
519 	zfsvfs->z_vfs = vfsp;
520 	zfsvfs->z_parent = zfsvfs;
521 	zfsvfs->z_assign = TXG_NOWAIT;
522 	zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
523 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
524 
525 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
526 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
527 	    offsetof(znode_t, z_link_node));
528 	rw_init(&zfsvfs->z_unmount_lock, NULL, RW_DEFAULT, NULL);
529 	rw_init(&zfsvfs->z_unmount_inactive_lock, NULL, RW_DEFAULT, NULL);
530 
531 	/* Initialize the generic filesystem structure. */
532 	vfsp->vfs_bcount = 0;
533 	vfsp->vfs_data = NULL;
534 
535 	if (zfs_create_unique_device(&mount_dev) == -1) {
536 		error = ENODEV;
537 		goto out;
538 	}
539 	ASSERT(vfs_devismounted(mount_dev) == 0);
540 
541 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
542 	    NULL))
543 		goto out;
544 
545 	vfsp->vfs_dev = mount_dev;
546 	vfsp->vfs_fstype = zfsfstype;
547 	vfsp->vfs_bsize = recordsize;
548 	vfsp->vfs_flag |= VFS_NOTRUNC;
549 	vfsp->vfs_data = zfsvfs;
550 
551 	if (error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL))
552 		goto out;
553 
554 	if (readonly)
555 		mode = DS_MODE_PRIMARY | DS_MODE_READONLY;
556 	else
557 		mode = DS_MODE_PRIMARY;
558 
559 	error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
560 	if (error == EROFS) {
561 		mode = DS_MODE_PRIMARY | DS_MODE_READONLY;
562 		error = dmu_objset_open(osname, DMU_OST_ZFS, mode,
563 		    &zfsvfs->z_os);
564 	}
565 
566 	if (error)
567 		goto out;
568 
569 	if (error = zfs_init_fs(zfsvfs, &zp, cr))
570 		goto out;
571 
572 	/* The call to zfs_init_fs leaves the vnode held, release it here. */
573 	VN_RELE(ZTOV(zp));
574 
575 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
576 		uint64_t xattr;
577 
578 		ASSERT(mode & DS_MODE_READONLY);
579 		atime_changed_cb(zfsvfs, B_FALSE);
580 		readonly_changed_cb(zfsvfs, B_TRUE);
581 		if (error = dsl_prop_get_integer(osname, "xattr", &xattr, NULL))
582 			goto out;
583 		xattr_changed_cb(zfsvfs, xattr);
584 		zfsvfs->z_issnap = B_TRUE;
585 	} else {
586 		uint_t readonly;
587 
588 		error = zfs_register_callbacks(vfsp);
589 		if (error)
590 			goto out;
591 
592 		/*
593 		 * During replay we remove the read only flag to
594 		 * allow replays to succeed.
595 		 */
596 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
597 		if (readonly != 0)
598 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
599 		else
600 			zfs_unlinked_drain(zfsvfs);
601 
602 		/*
603 		 * Parse and replay the intent log.
604 		 *
605 		 * Because of ziltest, this must be done after
606 		 * zfs_unlinked_drain().  (Further note: ziltest doesn't
607 		 * use readonly mounts, where zfs_unlinked_drain() isn't
608 		 * called.)  This is because ziltest causes spa_sync()
609 		 * to think it's committed, but actually it is not, so
610 		 * the intent log contains many txg's worth of changes.
611 		 *
612 		 * In particular, if object N is in the unlinked set in
613 		 * the last txg to actually sync, then it could be
614 		 * actually freed in a later txg and then reallocated in
615 		 * a yet later txg.  This would write a "create object
616 		 * N" record to the intent log.  Normally, this would be
617 		 * fine because the spa_sync() would have written out
618 		 * the fact that object N is free, before we could write
619 		 * the "create object N" intent log record.
620 		 *
621 		 * But when we are in ziltest mode, we advance the "open
622 		 * txg" without actually spa_sync()-ing the changes to
623 		 * disk.  So we would see that object N is still
624 		 * allocated and in the unlinked set, and there is an
625 		 * intent log record saying to allocate it.
626 		 */
627 		zil_replay(zfsvfs->z_os, zfsvfs, &zfsvfs->z_assign,
628 		    zfs_replay_vector);
629 
630 		zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
631 
632 		if (!zil_disable)
633 			zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
634 	}
635 
636 	if (!zfsvfs->z_issnap)
637 		zfsctl_create(zfsvfs);
638 out:
639 	if (error) {
640 		if (zfsvfs->z_os)
641 			dmu_objset_close(zfsvfs->z_os);
642 		mutex_destroy(&zfsvfs->z_znodes_lock);
643 		list_destroy(&zfsvfs->z_all_znodes);
644 		rw_destroy(&zfsvfs->z_unmount_lock);
645 		rw_destroy(&zfsvfs->z_unmount_inactive_lock);
646 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
647 	} else {
648 		atomic_add_32(&zfs_active_fs_count, 1);
649 	}
650 
651 	return (error);
652 }
653 
654 void
655 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
656 {
657 	objset_t *os = zfsvfs->z_os;
658 	struct dsl_dataset *ds;
659 
660 	/*
661 	 * Unregister properties.
662 	 */
663 	if (!dmu_objset_is_snapshot(os)) {
664 		ds = dmu_objset_ds(os);
665 		VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
666 		    zfsvfs) == 0);
667 
668 		VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
669 		    zfsvfs) == 0);
670 
671 		VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
672 		    zfsvfs) == 0);
673 
674 		VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
675 		    zfsvfs) == 0);
676 
677 		VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
678 		    zfsvfs) == 0);
679 
680 		VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
681 		    zfsvfs) == 0);
682 
683 		VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
684 		    zfsvfs) == 0);
685 
686 		VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
687 		    zfsvfs) == 0);
688 
689 		VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
690 		    zfsvfs) == 0);
691 
692 		VERIFY(dsl_prop_unregister(ds, "aclinherit",
693 		    acl_inherit_changed_cb, zfsvfs) == 0);
694 	}
695 }
696 
697 /*
698  * Convert a decimal digit string to a uint64_t integer.
699  */
700 static int
701 str_to_uint64(char *str, uint64_t *objnum)
702 {
703 	uint64_t num = 0;
704 
705 	while (*str) {
706 		if (*str < '0' || *str > '9')
707 			return (EINVAL);
708 
709 		num = num*10 + *str++ - '0';
710 	}
711 
712 	*objnum = num;
713 	return (0);
714 }
715 
716 /*
717  * The boot path passed from the boot loader is in the form of
718  * "rootpool-name/root-filesystem-object-number'. Convert this
719  * string to a dataset name: "rootpool-name/root-filesystem-name".
720  */
721 static int
722 parse_bootpath(char *bpath, char *outpath)
723 {
724 	char *slashp;
725 	uint64_t objnum;
726 	int error;
727 
728 	if (*bpath == 0 || *bpath == '/')
729 		return (EINVAL);
730 
731 	slashp = strchr(bpath, '/');
732 
733 	/* if no '/', just return the pool name */
734 	if (slashp == NULL) {
735 		(void) strcpy(outpath, bpath);
736 		return (0);
737 	}
738 
739 	if (error = str_to_uint64(slashp+1, &objnum))
740 		return (error);
741 
742 	*slashp = '\0';
743 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
744 	*slashp = '/';
745 
746 	return (error);
747 }
748 
749 static int
750 zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
751 {
752 	int error = 0;
753 	int ret = 0;
754 	static int zfsrootdone = 0;
755 	zfsvfs_t *zfsvfs = NULL;
756 	znode_t *zp = NULL;
757 	vnode_t *vp = NULL;
758 	char *zfs_bootpath;
759 
760 	ASSERT(vfsp);
761 
762 	/*
763 	 * The filesystem that we mount as root is defined in the
764 	 * "zfs-bootfs" property.
765 	 */
766 	if (why == ROOT_INIT) {
767 		if (zfsrootdone++)
768 			return (EBUSY);
769 
770 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
771 		    DDI_PROP_DONTPASS, "zfs-bootfs", &zfs_bootpath) !=
772 		    DDI_SUCCESS)
773 			return (EIO);
774 
775 		error = parse_bootpath(zfs_bootpath, rootfs.bo_name);
776 		ddi_prop_free(zfs_bootpath);
777 
778 		if (error)
779 			return (error);
780 
781 		if (error = vfs_lock(vfsp))
782 			return (error);
783 
784 		if (error = zfs_domount(vfsp, rootfs.bo_name, CRED()))
785 			goto out;
786 
787 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
788 		ASSERT(zfsvfs);
789 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp))
790 			goto out;
791 
792 		vp = ZTOV(zp);
793 		mutex_enter(&vp->v_lock);
794 		vp->v_flag |= VROOT;
795 		mutex_exit(&vp->v_lock);
796 		rootvp = vp;
797 
798 		/*
799 		 * The zfs_zget call above returns with a hold on vp, we release
800 		 * it here.
801 		 */
802 		VN_RELE(vp);
803 
804 		/*
805 		 * Mount root as readonly initially, it will be remouted
806 		 * read/write by /lib/svc/method/fs-usr.
807 		 */
808 		readonly_changed_cb(vfsp->vfs_data, B_TRUE);
809 		vfs_add((struct vnode *)0, vfsp,
810 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
811 out:
812 		vfs_unlock(vfsp);
813 		ret = (error) ? error : 0;
814 		return (ret);
815 	} else if (why == ROOT_REMOUNT) {
816 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
817 		vfsp->vfs_flag |= VFS_REMOUNT;
818 
819 		/* refresh mount options */
820 		zfs_unregister_callbacks(vfsp->vfs_data);
821 		return (zfs_register_callbacks(vfsp));
822 
823 	} else if (why == ROOT_UNMOUNT) {
824 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
825 		(void) zfs_sync(vfsp, 0, 0);
826 		return (0);
827 	}
828 
829 	/*
830 	 * if "why" is equal to anything else other than ROOT_INIT,
831 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
832 	 */
833 	return (ENOTSUP);
834 }
835 
836 /*ARGSUSED*/
837 static int
838 zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
839 {
840 	char		*osname;
841 	pathname_t	spn;
842 	int		error = 0;
843 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
844 	    UIO_SYSSPACE : UIO_USERSPACE;
845 	int		canwrite;
846 
847 	if (mvp->v_type != VDIR)
848 		return (ENOTDIR);
849 
850 	mutex_enter(&mvp->v_lock);
851 	if ((uap->flags & MS_REMOUNT) == 0 &&
852 	    (uap->flags & MS_OVERLAY) == 0 &&
853 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
854 		mutex_exit(&mvp->v_lock);
855 		return (EBUSY);
856 	}
857 	mutex_exit(&mvp->v_lock);
858 
859 	/*
860 	 * ZFS does not support passing unparsed data in via MS_DATA.
861 	 * Users should use the MS_OPTIONSTR interface; this means
862 	 * that all option parsing is already done and the options struct
863 	 * can be interrogated.
864 	 */
865 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
866 		return (EINVAL);
867 
868 	/*
869 	 * Get the objset name (the "special" mount argument).
870 	 */
871 	if (error = pn_get(uap->spec, fromspace, &spn))
872 		return (error);
873 
874 	osname = spn.pn_path;
875 
876 	/*
877 	 * Check for mount privilege?
878 	 *
879 	 * If we don't have privilege then see if
880 	 * we have local permission to allow it
881 	 */
882 	error = secpolicy_fs_mount(cr, mvp, vfsp);
883 	if (error) {
884 		error = dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr);
885 		if (error == 0) {
886 			vattr_t		vattr;
887 
888 			/*
889 			 * Make sure user is the owner of the mount point
890 			 * or has sufficient privileges.
891 			 */
892 
893 			vattr.va_mask = AT_UID;
894 
895 			if (error = VOP_GETATTR(mvp, &vattr, 0, cr)) {
896 				goto out;
897 			}
898 
899 			if (error = secpolicy_vnode_owner(cr, vattr.va_uid)) {
900 				goto out;
901 			}
902 
903 			if (error = VOP_ACCESS(mvp, VWRITE, 0, cr)) {
904 				goto out;
905 			}
906 
907 			secpolicy_fs_mount_clearopts(cr, vfsp);
908 		} else {
909 			goto out;
910 		}
911 	}
912 
913 	/*
914 	 * Refuse to mount a filesystem if we are in a local zone and the
915 	 * dataset is not visible.
916 	 */
917 	if (!INGLOBALZONE(curproc) &&
918 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
919 		error = EPERM;
920 		goto out;
921 	}
922 
923 	/*
924 	 * When doing a remount, we simply refresh our temporary properties
925 	 * according to those options set in the current VFS options.
926 	 */
927 	if (uap->flags & MS_REMOUNT) {
928 		/* refresh mount options */
929 		zfs_unregister_callbacks(vfsp->vfs_data);
930 		error = zfs_register_callbacks(vfsp);
931 		goto out;
932 	}
933 
934 	error = zfs_domount(vfsp, osname, cr);
935 
936 out:
937 	pn_free(&spn);
938 	return (error);
939 }
940 
941 static int
942 zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
943 {
944 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
945 	dev32_t d32;
946 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
947 
948 	ZFS_ENTER(zfsvfs);
949 
950 	dmu_objset_space(zfsvfs->z_os,
951 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
952 
953 	/*
954 	 * The underlying storage pool actually uses multiple block sizes.
955 	 * We report the fragsize as the smallest block size we support,
956 	 * and we report our blocksize as the filesystem's maximum blocksize.
957 	 */
958 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
959 	statp->f_bsize = zfsvfs->z_max_blksz;
960 
961 	/*
962 	 * The following report "total" blocks of various kinds in the
963 	 * file system, but reported in terms of f_frsize - the
964 	 * "fragment" size.
965 	 */
966 
967 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
968 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
969 	statp->f_bavail = statp->f_bfree; /* no root reservation */
970 
971 	/*
972 	 * statvfs() should really be called statufs(), because it assumes
973 	 * static metadata.  ZFS doesn't preallocate files, so the best
974 	 * we can do is report the max that could possibly fit in f_files,
975 	 * and that minus the number actually used in f_ffree.
976 	 * For f_ffree, report the smaller of the number of object available
977 	 * and the number of blocks (each object will take at least a block).
978 	 */
979 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
980 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
981 	statp->f_files = statp->f_ffree + usedobjs;
982 
983 	(void) cmpldev(&d32, vfsp->vfs_dev);
984 	statp->f_fsid = d32;
985 
986 	/*
987 	 * We're a zfs filesystem.
988 	 */
989 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
990 
991 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
992 
993 	statp->f_namemax = ZFS_MAXNAMELEN;
994 
995 	/*
996 	 * We have all of 32 characters to stuff a string here.
997 	 * Is there anything useful we could/should provide?
998 	 */
999 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
1000 
1001 	ZFS_EXIT(zfsvfs);
1002 	return (0);
1003 }
1004 
1005 static int
1006 zfs_root(vfs_t *vfsp, vnode_t **vpp)
1007 {
1008 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1009 	znode_t *rootzp;
1010 	int error;
1011 
1012 	ZFS_ENTER(zfsvfs);
1013 
1014 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1015 	if (error == 0)
1016 		*vpp = ZTOV(rootzp);
1017 
1018 	ZFS_EXIT(zfsvfs);
1019 	return (error);
1020 }
1021 
1022 /*ARGSUSED*/
1023 static int
1024 zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1025 {
1026 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1027 	objset_t *os = zfsvfs->z_os;
1028 	znode_t	*zp, *nextzp;
1029 	int ret;
1030 
1031 	ret = secpolicy_fs_unmount(cr, vfsp);
1032 	if (ret) {
1033 		ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
1034 		    ZFS_DELEG_PERM_MOUNT, cr);
1035 		if (ret)
1036 			return (ret);
1037 	}
1038 
1039 	/*
1040 	 * We purge the parent filesystem's vfsp as the parent filesystem
1041 	 * and all of its snapshots have their vnode's v_vfsp set to the
1042 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
1043 	 * referential for non-snapshots.
1044 	 */
1045 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1046 
1047 	/*
1048 	 * Unmount any snapshots mounted under .zfs before unmounting the
1049 	 * dataset itself.
1050 	 */
1051 	if (zfsvfs->z_ctldir != NULL &&
1052 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1053 		return (ret);
1054 	}
1055 
1056 	if (!(fflag & MS_FORCE)) {
1057 		/*
1058 		 * Check the number of active vnodes in the file system.
1059 		 * Our count is maintained in the vfs structure, but the
1060 		 * number is off by 1 to indicate a hold on the vfs
1061 		 * structure itself.
1062 		 *
1063 		 * The '.zfs' directory maintains a reference of its
1064 		 * own, and any active references underneath are
1065 		 * reflected in the vnode count.
1066 		 */
1067 		if (zfsvfs->z_ctldir == NULL) {
1068 			if (vfsp->vfs_count > 1)
1069 				return (EBUSY);
1070 		} else {
1071 			if (vfsp->vfs_count > 2 ||
1072 			    zfsvfs->z_ctldir->v_count > 1) {
1073 				return (EBUSY);
1074 			}
1075 		}
1076 	}
1077 
1078 	vfsp->vfs_flag |= VFS_UNMOUNTED;
1079 
1080 	rw_enter(&zfsvfs->z_unmount_lock, RW_WRITER);
1081 	rw_enter(&zfsvfs->z_unmount_inactive_lock, RW_WRITER);
1082 
1083 	/*
1084 	 * At this point there are no vops active, and any new vops will
1085 	 * fail with EIO since we have z_unmount_lock for writer (only
1086 	 * relavent for forced unmount).
1087 	 *
1088 	 * Release all holds on dbufs.
1089 	 * Note, the dmu can still callback via znode_pageout_func()
1090 	 * which can zfs_znode_free() the znode.  So we lock
1091 	 * z_all_znodes; search the list for a held dbuf; drop the lock
1092 	 * (we know zp can't disappear if we hold a dbuf lock) then
1093 	 * regrab the lock and restart.
1094 	 */
1095 	mutex_enter(&zfsvfs->z_znodes_lock);
1096 	for (zp = list_head(&zfsvfs->z_all_znodes); zp; zp = nextzp) {
1097 		nextzp = list_next(&zfsvfs->z_all_znodes, zp);
1098 		if (zp->z_dbuf_held) {
1099 			/* dbufs should only be held when force unmounting */
1100 			zp->z_dbuf_held = 0;
1101 			mutex_exit(&zfsvfs->z_znodes_lock);
1102 			dmu_buf_rele(zp->z_dbuf, NULL);
1103 			/* Start again */
1104 			mutex_enter(&zfsvfs->z_znodes_lock);
1105 			nextzp = list_head(&zfsvfs->z_all_znodes);
1106 		}
1107 	}
1108 	mutex_exit(&zfsvfs->z_znodes_lock);
1109 
1110 	/*
1111 	 * Set the unmounted flag and let new vops unblock.
1112 	 * zfs_inactive will have the unmounted behavior, and all other
1113 	 * vops will fail with EIO.
1114 	 */
1115 	zfsvfs->z_unmounted = B_TRUE;
1116 	rw_exit(&zfsvfs->z_unmount_lock);
1117 	rw_exit(&zfsvfs->z_unmount_inactive_lock);
1118 
1119 	/*
1120 	 * Unregister properties.
1121 	 */
1122 	if (!dmu_objset_is_snapshot(os))
1123 		zfs_unregister_callbacks(zfsvfs);
1124 
1125 	/*
1126 	 * Close the zil. NB: Can't close the zil while zfs_inactive
1127 	 * threads are blocked as zil_close can call zfs_inactive.
1128 	 */
1129 	if (zfsvfs->z_log) {
1130 		zil_close(zfsvfs->z_log);
1131 		zfsvfs->z_log = NULL;
1132 	}
1133 
1134 	/*
1135 	 * Evict cached data
1136 	 */
1137 	(void) dmu_objset_evict_dbufs(os);
1138 
1139 	/*
1140 	 * Finally close the objset
1141 	 */
1142 	dmu_objset_close(os);
1143 
1144 	/*
1145 	 * We can now safely destroy the '.zfs' directory node.
1146 	 */
1147 	if (zfsvfs->z_ctldir != NULL)
1148 		zfsctl_destroy(zfsvfs);
1149 
1150 	return (0);
1151 }
1152 
1153 static int
1154 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1155 {
1156 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1157 	znode_t		*zp;
1158 	uint64_t	object = 0;
1159 	uint64_t	fid_gen = 0;
1160 	uint64_t	gen_mask;
1161 	uint64_t	zp_gen;
1162 	int 		i, err;
1163 
1164 	*vpp = NULL;
1165 
1166 	ZFS_ENTER(zfsvfs);
1167 
1168 	if (fidp->fid_len == LONG_FID_LEN) {
1169 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1170 		uint64_t	objsetid = 0;
1171 		uint64_t	setgen = 0;
1172 
1173 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1174 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1175 
1176 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1177 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1178 
1179 		ZFS_EXIT(zfsvfs);
1180 
1181 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1182 		if (err)
1183 			return (EINVAL);
1184 		ZFS_ENTER(zfsvfs);
1185 	}
1186 
1187 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1188 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1189 
1190 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1191 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1192 
1193 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1194 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1195 	} else {
1196 		ZFS_EXIT(zfsvfs);
1197 		return (EINVAL);
1198 	}
1199 
1200 	/* A zero fid_gen means we are in the .zfs control directories */
1201 	if (fid_gen == 0 &&
1202 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1203 		*vpp = zfsvfs->z_ctldir;
1204 		ASSERT(*vpp != NULL);
1205 		if (object == ZFSCTL_INO_SNAPDIR) {
1206 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1207 			    0, NULL, NULL) == 0);
1208 		} else {
1209 			VN_HOLD(*vpp);
1210 		}
1211 		ZFS_EXIT(zfsvfs);
1212 		return (0);
1213 	}
1214 
1215 	gen_mask = -1ULL >> (64 - 8 * i);
1216 
1217 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1218 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1219 		ZFS_EXIT(zfsvfs);
1220 		return (err);
1221 	}
1222 	zp_gen = zp->z_phys->zp_gen & gen_mask;
1223 	if (zp_gen == 0)
1224 		zp_gen = 1;
1225 	if (zp->z_unlinked || zp_gen != fid_gen) {
1226 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1227 		VN_RELE(ZTOV(zp));
1228 		ZFS_EXIT(zfsvfs);
1229 		return (EINVAL);
1230 	}
1231 
1232 	*vpp = ZTOV(zp);
1233 	ZFS_EXIT(zfsvfs);
1234 	return (0);
1235 }
1236 
1237 static void
1238 zfs_freevfs(vfs_t *vfsp)
1239 {
1240 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1241 	int i;
1242 
1243 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1244 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1245 
1246 	mutex_destroy(&zfsvfs->z_znodes_lock);
1247 	list_destroy(&zfsvfs->z_all_znodes);
1248 	rw_destroy(&zfsvfs->z_unmount_lock);
1249 	rw_destroy(&zfsvfs->z_unmount_inactive_lock);
1250 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1251 
1252 	atomic_add_32(&zfs_active_fs_count, -1);
1253 }
1254 
1255 /*
1256  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
1257  * so we can't safely do any non-idempotent initialization here.
1258  * Leave that to zfs_init() and zfs_fini(), which are called
1259  * from the module's _init() and _fini() entry points.
1260  */
1261 /*ARGSUSED*/
1262 static int
1263 zfs_vfsinit(int fstype, char *name)
1264 {
1265 	int error;
1266 
1267 	zfsfstype = fstype;
1268 
1269 	/*
1270 	 * Setup vfsops and vnodeops tables.
1271 	 */
1272 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
1273 	if (error != 0) {
1274 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
1275 	}
1276 
1277 	error = zfs_create_op_tables();
1278 	if (error) {
1279 		zfs_remove_op_tables();
1280 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
1281 		(void) vfs_freevfsops_by_type(zfsfstype);
1282 		return (error);
1283 	}
1284 
1285 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
1286 
1287 	/*
1288 	 * Unique major number for all zfs mounts.
1289 	 * If we run out of 32-bit minors, we'll getudev() another major.
1290 	 */
1291 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
1292 	zfs_minor = ZFS_MIN_MINOR;
1293 
1294 	return (0);
1295 }
1296 
1297 void
1298 zfs_init(void)
1299 {
1300 	/*
1301 	 * Initialize .zfs directory structures
1302 	 */
1303 	zfsctl_init();
1304 
1305 	/*
1306 	 * Initialize znode cache, vnode ops, etc...
1307 	 */
1308 	zfs_znode_init();
1309 }
1310 
1311 void
1312 zfs_fini(void)
1313 {
1314 	zfsctl_fini();
1315 	zfs_znode_fini();
1316 }
1317 
1318 int
1319 zfs_busy(void)
1320 {
1321 	return (zfs_active_fs_count != 0);
1322 }
1323 
1324 int
1325 zfs_get_version(objset_t *os, uint64_t *version)
1326 {
1327 	int error;
1328 
1329 	error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1, version);
1330 	return (error);
1331 }
1332 
1333 int
1334 zfs_set_version(const char *name, uint64_t newvers)
1335 {
1336 	int error;
1337 	objset_t *os;
1338 	dmu_tx_t *tx;
1339 	uint64_t curvers;
1340 
1341 	/*
1342 	 * XXX for now, require that the filesystem be unmounted.  Would
1343 	 * be nice to find the zfsvfs_t and just update that if
1344 	 * possible.
1345 	 */
1346 
1347 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
1348 		return (EINVAL);
1349 
1350 	error = dmu_objset_open(name, DMU_OST_ZFS, DS_MODE_PRIMARY, &os);
1351 	if (error)
1352 		return (error);
1353 
1354 	error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1355 	    8, 1, &curvers);
1356 	if (error)
1357 		goto out;
1358 	if (newvers < curvers) {
1359 		error = EINVAL;
1360 		goto out;
1361 	}
1362 
1363 	tx = dmu_tx_create(os);
1364 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 0, ZPL_VERSION_STR);
1365 	error = dmu_tx_assign(tx, TXG_WAIT);
1366 	if (error) {
1367 		dmu_tx_abort(tx);
1368 		goto out;
1369 	}
1370 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1,
1371 	    &newvers, tx);
1372 
1373 	spa_history_internal_log(LOG_DS_UPGRADE,
1374 	    dmu_objset_spa(os), tx, CRED(),
1375 	    "oldver=%llu newver=%llu dataset = %llu", curvers, newvers,
1376 	    dmu_objset_id(os));
1377 	dmu_tx_commit(tx);
1378 
1379 out:
1380 	dmu_objset_close(os);
1381 	return (error);
1382 }
1383 
1384 static vfsdef_t vfw = {
1385 	VFSDEF_VERSION,
1386 	MNTTYPE_ZFS,
1387 	zfs_vfsinit,
1388 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS,
1389 	&zfs_mntopts
1390 };
1391 
1392 struct modlfs zfs_modlfs = {
1393 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
1394 };
1395