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