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