xref: /titanic_41/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 2bd43bc76740c0fef669e90cfcaa4e3f3cb2a2f5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/sysmacros.h>
32 #include <sys/kmem.h>
33 #include <sys/pathname.h>
34 #include <sys/vnode.h>
35 #include <sys/vfs.h>
36 #include <sys/vfs_opreg.h>
37 #include <sys/mntent.h>
38 #include <sys/mount.h>
39 #include <sys/cmn_err.h>
40 #include "fs/fs_subr.h"
41 #include <sys/zfs_znode.h>
42 #include <sys/zfs_dir.h>
43 #include <sys/zil.h>
44 #include <sys/fs/zfs.h>
45 #include <sys/dmu.h>
46 #include <sys/dsl_prop.h>
47 #include <sys/dsl_dataset.h>
48 #include <sys/dsl_deleg.h>
49 #include <sys/spa.h>
50 #include <sys/zap.h>
51 #include <sys/varargs.h>
52 #include <sys/policy.h>
53 #include <sys/atomic.h>
54 #include <sys/mkdev.h>
55 #include <sys/modctl.h>
56 #include <sys/refstr.h>
57 #include <sys/zfs_ioctl.h>
58 #include <sys/zfs_ctldir.h>
59 #include <sys/bootconf.h>
60 #include <sys/sunddi.h>
61 #include <sys/dnlc.h>
62 
63 int zfsfstype;
64 vfsops_t *zfs_vfsops = NULL;
65 static major_t zfs_major;
66 static minor_t zfs_minor;
67 static kmutex_t	zfs_dev_mtx;
68 
69 static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
70 static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr);
71 static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
72 static int zfs_root(vfs_t *vfsp, vnode_t **vpp);
73 static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp);
74 static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp);
75 static void zfs_freevfs(vfs_t *vfsp);
76 static void zfs_objset_close(zfsvfs_t *zfsvfs);
77 
78 static const fs_operation_def_t zfs_vfsops_template[] = {
79 	VFSNAME_MOUNT,		{ .vfs_mount = zfs_mount },
80 	VFSNAME_MOUNTROOT,	{ .vfs_mountroot = zfs_mountroot },
81 	VFSNAME_UNMOUNT,	{ .vfs_unmount = zfs_umount },
82 	VFSNAME_ROOT,		{ .vfs_root = zfs_root },
83 	VFSNAME_STATVFS,	{ .vfs_statvfs = zfs_statvfs },
84 	VFSNAME_SYNC,		{ .vfs_sync = zfs_sync },
85 	VFSNAME_VGET,		{ .vfs_vget = zfs_vget },
86 	VFSNAME_FREEVFS,	{ .vfs_freevfs = zfs_freevfs },
87 	NULL,			NULL
88 };
89 
90 static const fs_operation_def_t zfs_vfsops_eio_template[] = {
91 	VFSNAME_FREEVFS,	{ .vfs_freevfs =  zfs_freevfs },
92 	NULL,			NULL
93 };
94 
95 /*
96  * We need to keep a count of active fs's.
97  * This is necessary to prevent our module
98  * from being unloaded after a umount -f
99  */
100 static uint32_t	zfs_active_fs_count = 0;
101 
102 static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
103 static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
104 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
105 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
106 
107 /*
108  * MO_DEFAULT is not used since the default value is determined
109  * by the equivalent property.
110  */
111 static mntopt_t mntopts[] = {
112 	{ MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL },
113 	{ MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL },
114 	{ MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL },
115 	{ MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
116 };
117 
118 static mntopts_t zfs_mntopts = {
119 	sizeof (mntopts) / sizeof (mntopt_t),
120 	mntopts
121 };
122 
123 /*ARGSUSED*/
124 int
125 zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
126 {
127 	/*
128 	 * Data integrity is job one.  We don't want a compromised kernel
129 	 * writing to the storage pool, so we never sync during panic.
130 	 */
131 	if (panicstr)
132 		return (0);
133 
134 	/*
135 	 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
136 	 * to sync metadata, which they would otherwise cache indefinitely.
137 	 * Semantically, the only requirement is that the sync be initiated.
138 	 * The DMU syncs out txgs frequently, so there's nothing to do.
139 	 */
140 	if (flag & SYNC_ATTR)
141 		return (0);
142 
143 	if (vfsp != NULL) {
144 		/*
145 		 * Sync a specific filesystem.
146 		 */
147 		zfsvfs_t *zfsvfs = vfsp->vfs_data;
148 
149 		ZFS_ENTER(zfsvfs);
150 		if (zfsvfs->z_log != NULL)
151 			zil_commit(zfsvfs->z_log, UINT64_MAX, 0);
152 		else
153 			txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
154 		ZFS_EXIT(zfsvfs);
155 	} else {
156 		/*
157 		 * Sync all ZFS filesystems.  This is what happens when you
158 		 * run sync(1M).  Unlike other filesystems, ZFS honors the
159 		 * request by waiting for all pools to commit all dirty data.
160 		 */
161 		spa_sync_allpools();
162 	}
163 
164 	return (0);
165 }
166 
167 static int
168 zfs_create_unique_device(dev_t *dev)
169 {
170 	major_t new_major;
171 
172 	do {
173 		ASSERT3U(zfs_minor, <=, MAXMIN32);
174 		minor_t start = zfs_minor;
175 		do {
176 			mutex_enter(&zfs_dev_mtx);
177 			if (zfs_minor >= MAXMIN32) {
178 				/*
179 				 * If we're still using the real major
180 				 * keep out of /dev/zfs and /dev/zvol minor
181 				 * number space.  If we're using a getudev()'ed
182 				 * major number, we can use all of its minors.
183 				 */
184 				if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
185 					zfs_minor = ZFS_MIN_MINOR;
186 				else
187 					zfs_minor = 0;
188 			} else {
189 				zfs_minor++;
190 			}
191 			*dev = makedevice(zfs_major, zfs_minor);
192 			mutex_exit(&zfs_dev_mtx);
193 		} while (vfs_devismounted(*dev) && zfs_minor != start);
194 		if (zfs_minor == start) {
195 			/*
196 			 * We are using all ~262,000 minor numbers for the
197 			 * current major number.  Create a new major number.
198 			 */
199 			if ((new_major = getudev()) == (major_t)-1) {
200 				cmn_err(CE_WARN,
201 				    "zfs_mount: Can't get unique major "
202 				    "device number.");
203 				return (-1);
204 			}
205 			mutex_enter(&zfs_dev_mtx);
206 			zfs_major = new_major;
207 			zfs_minor = 0;
208 
209 			mutex_exit(&zfs_dev_mtx);
210 		} else {
211 			break;
212 		}
213 		/* CONSTANTCONDITION */
214 	} while (1);
215 
216 	return (0);
217 }
218 
219 static void
220 atime_changed_cb(void *arg, uint64_t newval)
221 {
222 	zfsvfs_t *zfsvfs = arg;
223 
224 	if (newval == TRUE) {
225 		zfsvfs->z_atime = TRUE;
226 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
227 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
228 	} else {
229 		zfsvfs->z_atime = FALSE;
230 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
231 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
232 	}
233 }
234 
235 static void
236 xattr_changed_cb(void *arg, uint64_t newval)
237 {
238 	zfsvfs_t *zfsvfs = arg;
239 
240 	if (newval == TRUE) {
241 		/* XXX locking on vfs_flag? */
242 		zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
243 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
244 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
245 	} else {
246 		/* XXX locking on vfs_flag? */
247 		zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
248 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
249 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
250 	}
251 }
252 
253 static void
254 blksz_changed_cb(void *arg, uint64_t newval)
255 {
256 	zfsvfs_t *zfsvfs = arg;
257 
258 	if (newval < SPA_MINBLOCKSIZE ||
259 	    newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
260 		newval = SPA_MAXBLOCKSIZE;
261 
262 	zfsvfs->z_max_blksz = newval;
263 	zfsvfs->z_vfs->vfs_bsize = newval;
264 }
265 
266 static void
267 readonly_changed_cb(void *arg, uint64_t newval)
268 {
269 	zfsvfs_t *zfsvfs = arg;
270 
271 	if (newval) {
272 		/* XXX locking on vfs_flag? */
273 		zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
274 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
275 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
276 	} else {
277 		/* XXX locking on vfs_flag? */
278 		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
279 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
280 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
281 	}
282 }
283 
284 static void
285 devices_changed_cb(void *arg, uint64_t newval)
286 {
287 	zfsvfs_t *zfsvfs = arg;
288 
289 	if (newval == FALSE) {
290 		zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
291 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
292 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
293 	} else {
294 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
295 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
296 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
297 	}
298 }
299 
300 static void
301 setuid_changed_cb(void *arg, uint64_t newval)
302 {
303 	zfsvfs_t *zfsvfs = arg;
304 
305 	if (newval == FALSE) {
306 		zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
307 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
308 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
309 	} else {
310 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
311 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
312 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
313 	}
314 }
315 
316 static void
317 exec_changed_cb(void *arg, uint64_t newval)
318 {
319 	zfsvfs_t *zfsvfs = arg;
320 
321 	if (newval == FALSE) {
322 		zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
323 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
324 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
325 	} else {
326 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
327 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
328 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
329 	}
330 }
331 
332 static void
333 snapdir_changed_cb(void *arg, uint64_t newval)
334 {
335 	zfsvfs_t *zfsvfs = arg;
336 
337 	zfsvfs->z_show_ctldir = newval;
338 }
339 
340 static void
341 acl_mode_changed_cb(void *arg, uint64_t newval)
342 {
343 	zfsvfs_t *zfsvfs = arg;
344 
345 	zfsvfs->z_acl_mode = newval;
346 }
347 
348 static void
349 acl_inherit_changed_cb(void *arg, uint64_t newval)
350 {
351 	zfsvfs_t *zfsvfs = arg;
352 
353 	zfsvfs->z_acl_inherit = newval;
354 }
355 
356 static int
357 zfs_register_callbacks(vfs_t *vfsp)
358 {
359 	struct dsl_dataset *ds = NULL;
360 	objset_t *os = NULL;
361 	zfsvfs_t *zfsvfs = NULL;
362 	int readonly, do_readonly = FALSE;
363 	int setuid, do_setuid = FALSE;
364 	int exec, do_exec = FALSE;
365 	int devices, do_devices = FALSE;
366 	int xattr, do_xattr = FALSE;
367 	int atime, do_atime = FALSE;
368 	int error = 0;
369 
370 	ASSERT(vfsp);
371 	zfsvfs = vfsp->vfs_data;
372 	ASSERT(zfsvfs);
373 	os = zfsvfs->z_os;
374 
375 	/*
376 	 * The act of registering our callbacks will destroy any mount
377 	 * options we may have.  In order to enable temporary overrides
378 	 * of mount options, we stash away the current values and
379 	 * restore them after we register the callbacks.
380 	 */
381 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
382 		readonly = B_TRUE;
383 		do_readonly = B_TRUE;
384 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
385 		readonly = B_FALSE;
386 		do_readonly = B_TRUE;
387 	}
388 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
389 		devices = B_FALSE;
390 		setuid = B_FALSE;
391 		do_devices = B_TRUE;
392 		do_setuid = B_TRUE;
393 	} else {
394 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
395 			devices = B_FALSE;
396 			do_devices = B_TRUE;
397 		} else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
398 			devices = B_TRUE;
399 			do_devices = B_TRUE;
400 		}
401 
402 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
403 			setuid = B_FALSE;
404 			do_setuid = B_TRUE;
405 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
406 			setuid = B_TRUE;
407 			do_setuid = B_TRUE;
408 		}
409 	}
410 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
411 		exec = B_FALSE;
412 		do_exec = B_TRUE;
413 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
414 		exec = B_TRUE;
415 		do_exec = B_TRUE;
416 	}
417 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
418 		xattr = B_FALSE;
419 		do_xattr = B_TRUE;
420 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
421 		xattr = B_TRUE;
422 		do_xattr = B_TRUE;
423 	}
424 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
425 		atime = B_FALSE;
426 		do_atime = B_TRUE;
427 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
428 		atime = B_TRUE;
429 		do_atime = B_TRUE;
430 	}
431 
432 	/*
433 	 * Register property callbacks.
434 	 *
435 	 * It would probably be fine to just check for i/o error from
436 	 * the first prop_register(), but I guess I like to go
437 	 * overboard...
438 	 */
439 	ds = dmu_objset_ds(os);
440 	error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
441 	error = error ? error : dsl_prop_register(ds,
442 	    "xattr", xattr_changed_cb, zfsvfs);
443 	error = error ? error : dsl_prop_register(ds,
444 	    "recordsize", blksz_changed_cb, zfsvfs);
445 	error = error ? error : dsl_prop_register(ds,
446 	    "readonly", readonly_changed_cb, zfsvfs);
447 	error = error ? error : dsl_prop_register(ds,
448 	    "devices", devices_changed_cb, zfsvfs);
449 	error = error ? error : dsl_prop_register(ds,
450 	    "setuid", setuid_changed_cb, zfsvfs);
451 	error = error ? error : dsl_prop_register(ds,
452 	    "exec", exec_changed_cb, zfsvfs);
453 	error = error ? error : dsl_prop_register(ds,
454 	    "snapdir", snapdir_changed_cb, zfsvfs);
455 	error = error ? error : dsl_prop_register(ds,
456 	    "aclmode", acl_mode_changed_cb, zfsvfs);
457 	error = error ? error : dsl_prop_register(ds,
458 	    "aclinherit", acl_inherit_changed_cb, zfsvfs);
459 	if (error)
460 		goto unregister;
461 
462 	/*
463 	 * Invoke our callbacks to restore temporary mount options.
464 	 */
465 	if (do_readonly)
466 		readonly_changed_cb(zfsvfs, readonly);
467 	if (do_setuid)
468 		setuid_changed_cb(zfsvfs, setuid);
469 	if (do_exec)
470 		exec_changed_cb(zfsvfs, exec);
471 	if (do_devices)
472 		devices_changed_cb(zfsvfs, devices);
473 	if (do_xattr)
474 		xattr_changed_cb(zfsvfs, xattr);
475 	if (do_atime)
476 		atime_changed_cb(zfsvfs, atime);
477 
478 	return (0);
479 
480 unregister:
481 	/*
482 	 * We may attempt to unregister some callbacks that are not
483 	 * registered, but this is OK; it will simply return ENOMSG,
484 	 * which we will ignore.
485 	 */
486 	(void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
487 	(void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
488 	(void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
489 	(void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
490 	(void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs);
491 	(void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
492 	(void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
493 	(void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
494 	(void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
495 	(void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
496 	    zfsvfs);
497 	return (error);
498 
499 }
500 
501 static int
502 zfs_domount(vfs_t *vfsp, char *osname, cred_t *cr)
503 {
504 	dev_t mount_dev;
505 	uint64_t recordsize, readonly;
506 	int error = 0;
507 	int mode;
508 	zfsvfs_t *zfsvfs;
509 	znode_t *zp = NULL;
510 
511 	ASSERT(vfsp);
512 	ASSERT(osname);
513 
514 	/*
515 	 * Initialize the zfs-specific filesystem structure.
516 	 * Should probably make this a kmem cache, shuffle fields,
517 	 * and just bzero up to z_hold_mtx[].
518 	 */
519 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
520 	zfsvfs->z_vfs = vfsp;
521 	zfsvfs->z_parent = zfsvfs;
522 	zfsvfs->z_assign = TXG_NOWAIT;
523 	zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
524 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
525 
526 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
527 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
528 	    offsetof(znode_t, z_link_node));
529 	rw_init(&zfsvfs->z_um_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 (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 	int ret;
1013 
1014 	ret = secpolicy_fs_unmount(cr, vfsp);
1015 	if (ret) {
1016 		ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
1017 		    ZFS_DELEG_PERM_MOUNT, cr);
1018 		if (ret)
1019 			return (ret);
1020 	}
1021 
1022 	(void) dnlc_purge_vfsp(vfsp, 0);
1023 
1024 	/*
1025 	 * Unmount any snapshots mounted under .zfs before unmounting the
1026 	 * dataset itself.
1027 	 */
1028 	if (zfsvfs->z_ctldir != NULL &&
1029 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1030 		return (ret);
1031 	}
1032 
1033 	if (fflag & MS_FORCE) {
1034 		vfsp->vfs_flag |= VFS_UNMOUNTED;
1035 		zfsvfs->z_unmounted1 = B_TRUE;
1036 
1037 		/*
1038 		 * Ensure that z_unmounted1 reaches global visibility
1039 		 * before z_op_cnt.
1040 		 */
1041 		membar_producer();
1042 
1043 		/*
1044 		 * Wait for all zfs threads to leave zfs.
1045 		 * Grabbing a rwlock as reader in all vops and
1046 		 * as writer here doesn't work because it too easy to get
1047 		 * multiple reader enters as zfs can re-enter itself.
1048 		 * This can lead to deadlock if there is an intervening
1049 		 * rw_enter as writer.
1050 		 * So a file system threads ref count (z_op_cnt) is used.
1051 		 * A polling loop on z_op_cnt may seem inefficient, but
1052 		 * - this saves all threads on exit from having to grab a
1053 		 *   mutex in order to cv_signal
1054 		 * - only occurs on forced unmount in the rare case when
1055 		 *   there are outstanding threads within the file system.
1056 		 */
1057 		while (zfsvfs->z_op_cnt) {
1058 			delay(1);
1059 		}
1060 
1061 		zfs_objset_close(zfsvfs);
1062 
1063 		return (0);
1064 	}
1065 	/*
1066 	 * Check the number of active vnodes in the file system.
1067 	 * Our count is maintained in the vfs structure, but the number
1068 	 * is off by 1 to indicate a hold on the vfs structure itself.
1069 	 *
1070 	 * The '.zfs' directory maintains a reference of its own, and any active
1071 	 * references underneath are reflected in the vnode count.
1072 	 */
1073 	if (zfsvfs->z_ctldir == NULL) {
1074 		if (vfsp->vfs_count > 1)
1075 			return (EBUSY);
1076 	} else {
1077 		if (vfsp->vfs_count > 2 ||
1078 		    (zfsvfs->z_ctldir->v_count > 1 && !(fflag & MS_FORCE))) {
1079 			return (EBUSY);
1080 		}
1081 	}
1082 
1083 	vfsp->vfs_flag |= VFS_UNMOUNTED;
1084 	zfs_objset_close(zfsvfs);
1085 
1086 	return (0);
1087 }
1088 
1089 static int
1090 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1091 {
1092 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1093 	znode_t		*zp;
1094 	uint64_t	object = 0;
1095 	uint64_t	fid_gen = 0;
1096 	uint64_t	gen_mask;
1097 	uint64_t	zp_gen;
1098 	int 		i, err;
1099 
1100 	*vpp = NULL;
1101 
1102 	ZFS_ENTER(zfsvfs);
1103 
1104 	if (fidp->fid_len == LONG_FID_LEN) {
1105 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1106 		uint64_t	objsetid = 0;
1107 		uint64_t	setgen = 0;
1108 
1109 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1110 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1111 
1112 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1113 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1114 
1115 		ZFS_EXIT(zfsvfs);
1116 
1117 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1118 		if (err)
1119 			return (EINVAL);
1120 		ZFS_ENTER(zfsvfs);
1121 	}
1122 
1123 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1124 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1125 
1126 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1127 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1128 
1129 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1130 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1131 	} else {
1132 		ZFS_EXIT(zfsvfs);
1133 		return (EINVAL);
1134 	}
1135 
1136 	/* A zero fid_gen means we are in the .zfs control directories */
1137 	if (fid_gen == 0 &&
1138 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1139 		*vpp = zfsvfs->z_ctldir;
1140 		ASSERT(*vpp != NULL);
1141 		if (object == ZFSCTL_INO_SNAPDIR) {
1142 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1143 			    0, NULL, NULL) == 0);
1144 		} else {
1145 			VN_HOLD(*vpp);
1146 		}
1147 		ZFS_EXIT(zfsvfs);
1148 		return (0);
1149 	}
1150 
1151 	gen_mask = -1ULL >> (64 - 8 * i);
1152 
1153 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1154 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1155 		ZFS_EXIT(zfsvfs);
1156 		return (err);
1157 	}
1158 	zp_gen = zp->z_phys->zp_gen & gen_mask;
1159 	if (zp_gen == 0)
1160 		zp_gen = 1;
1161 	if (zp->z_unlinked || zp_gen != fid_gen) {
1162 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1163 		VN_RELE(ZTOV(zp));
1164 		ZFS_EXIT(zfsvfs);
1165 		return (EINVAL);
1166 	}
1167 
1168 	*vpp = ZTOV(zp);
1169 	ZFS_EXIT(zfsvfs);
1170 	return (0);
1171 }
1172 
1173 static void
1174 zfs_objset_close(zfsvfs_t *zfsvfs)
1175 {
1176 	znode_t		*zp, *nextzp;
1177 	objset_t	*os = zfsvfs->z_os;
1178 
1179 	/*
1180 	 * For forced unmount, at this point all vops except zfs_inactive
1181 	 * are erroring EIO. We need to now suspend zfs_inactive threads
1182 	 * while we are freeing dbufs before switching zfs_inactive
1183 	 * to use behaviour without a objset.
1184 	 */
1185 	rw_enter(&zfsvfs->z_um_lock, RW_WRITER);
1186 
1187 	/*
1188 	 * Release all holds on dbufs
1189 	 * Note, although we have stopped all other vop threads and
1190 	 * zfs_inactive(), the dmu can callback via znode_pageout_func()
1191 	 * which can zfs_znode_free() the znode.
1192 	 * So we lock z_all_znodes; search the list for a held
1193 	 * dbuf; drop the lock (we know zp can't disappear if we hold
1194 	 * a dbuf lock; then regrab the lock and restart.
1195 	 */
1196 	mutex_enter(&zfsvfs->z_znodes_lock);
1197 	for (zp = list_head(&zfsvfs->z_all_znodes); zp; zp = nextzp) {
1198 		nextzp = list_next(&zfsvfs->z_all_znodes, zp);
1199 		if (zp->z_dbuf_held) {
1200 			/* dbufs should only be held when force unmounting */
1201 			zp->z_dbuf_held = 0;
1202 			mutex_exit(&zfsvfs->z_znodes_lock);
1203 			dmu_buf_rele(zp->z_dbuf, NULL);
1204 			/* Start again */
1205 			mutex_enter(&zfsvfs->z_znodes_lock);
1206 			nextzp = list_head(&zfsvfs->z_all_znodes);
1207 		}
1208 	}
1209 	mutex_exit(&zfsvfs->z_znodes_lock);
1210 
1211 	/*
1212 	 * Unregister properties.
1213 	 */
1214 	if (!dmu_objset_is_snapshot(os))
1215 		zfs_unregister_callbacks(zfsvfs);
1216 
1217 	/*
1218 	 * Switch zfs_inactive to behaviour without an objset.
1219 	 * It just tosses cached pages and frees the znode & vnode.
1220 	 * Then re-enable zfs_inactive threads in that new behaviour.
1221 	 */
1222 	zfsvfs->z_unmounted2 = B_TRUE;
1223 	rw_exit(&zfsvfs->z_um_lock); /* re-enable any zfs_inactive threads */
1224 
1225 	/*
1226 	 * Close the zil. Can't close the zil while zfs_inactive
1227 	 * threads are blocked as zil_close can call zfs_inactive.
1228 	 */
1229 	if (zfsvfs->z_log) {
1230 		zil_close(zfsvfs->z_log);
1231 		zfsvfs->z_log = NULL;
1232 	}
1233 
1234 	/*
1235 	 * Evict all dbufs so that cached znodes will be freed
1236 	 */
1237 	if (dmu_objset_evict_dbufs(os, 1)) {
1238 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1239 		(void) dmu_objset_evict_dbufs(os, 0);
1240 	}
1241 
1242 	/*
1243 	 * Finally close the objset
1244 	 */
1245 	dmu_objset_close(os);
1246 
1247 	/*
1248 	 * We can now safely destroy the '.zfs' directory node.
1249 	 */
1250 	if (zfsvfs->z_ctldir != NULL)
1251 		zfsctl_destroy(zfsvfs);
1252 
1253 }
1254 
1255 static void
1256 zfs_freevfs(vfs_t *vfsp)
1257 {
1258 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1259 
1260 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1261 
1262 	atomic_add_32(&zfs_active_fs_count, -1);
1263 }
1264 
1265 /*
1266  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
1267  * so we can't safely do any non-idempotent initialization here.
1268  * Leave that to zfs_init() and zfs_fini(), which are called
1269  * from the module's _init() and _fini() entry points.
1270  */
1271 /*ARGSUSED*/
1272 static int
1273 zfs_vfsinit(int fstype, char *name)
1274 {
1275 	int error;
1276 
1277 	zfsfstype = fstype;
1278 
1279 	/*
1280 	 * Setup vfsops and vnodeops tables.
1281 	 */
1282 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
1283 	if (error != 0) {
1284 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
1285 	}
1286 
1287 	error = zfs_create_op_tables();
1288 	if (error) {
1289 		zfs_remove_op_tables();
1290 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
1291 		(void) vfs_freevfsops_by_type(zfsfstype);
1292 		return (error);
1293 	}
1294 
1295 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
1296 
1297 	/*
1298 	 * Unique major number for all zfs mounts.
1299 	 * If we run out of 32-bit minors, we'll getudev() another major.
1300 	 */
1301 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
1302 	zfs_minor = ZFS_MIN_MINOR;
1303 
1304 	return (0);
1305 }
1306 
1307 void
1308 zfs_init(void)
1309 {
1310 	/*
1311 	 * Initialize .zfs directory structures
1312 	 */
1313 	zfsctl_init();
1314 
1315 	/*
1316 	 * Initialize znode cache, vnode ops, etc...
1317 	 */
1318 	zfs_znode_init();
1319 }
1320 
1321 void
1322 zfs_fini(void)
1323 {
1324 	zfsctl_fini();
1325 	zfs_znode_fini();
1326 }
1327 
1328 int
1329 zfs_busy(void)
1330 {
1331 	return (zfs_active_fs_count != 0);
1332 }
1333 
1334 int
1335 zfs_get_stats(objset_t *os, nvlist_t *nv)
1336 {
1337 	int error;
1338 	uint64_t val;
1339 
1340 	error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1, &val);
1341 	if (error == 0)
1342 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VERSION, val);
1343 
1344 	return (error);
1345 }
1346 
1347 int
1348 zfs_set_version(const char *name, uint64_t newvers)
1349 {
1350 	int error;
1351 	objset_t *os;
1352 	dmu_tx_t *tx;
1353 	uint64_t curvers;
1354 
1355 	/*
1356 	 * XXX for now, require that the filesystem be unmounted.  Would
1357 	 * be nice to find the zfsvfs_t and just update that if
1358 	 * possible.
1359 	 */
1360 
1361 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
1362 		return (EINVAL);
1363 
1364 	error = dmu_objset_open(name, DMU_OST_ZFS, DS_MODE_PRIMARY, &os);
1365 	if (error)
1366 		return (error);
1367 
1368 	error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1369 	    8, 1, &curvers);
1370 	if (error)
1371 		goto out;
1372 	if (newvers < curvers) {
1373 		error = EINVAL;
1374 		goto out;
1375 	}
1376 
1377 	tx = dmu_tx_create(os);
1378 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 0, ZPL_VERSION_STR);
1379 	error = dmu_tx_assign(tx, TXG_WAIT);
1380 	if (error) {
1381 		dmu_tx_abort(tx);
1382 		goto out;
1383 	}
1384 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1,
1385 	    &newvers, tx);
1386 
1387 	spa_history_internal_log(LOG_DS_UPGRADE,
1388 	    dmu_objset_spa(os), tx, CRED(),
1389 	    "oldver=%llu newver=%llu dataset = %llu", curvers, newvers,
1390 	    dmu_objset_id(os));
1391 	dmu_tx_commit(tx);
1392 
1393 out:
1394 	dmu_objset_close(os);
1395 	return (error);
1396 }
1397 
1398 static vfsdef_t vfw = {
1399 	VFSDEF_VERSION,
1400 	MNTTYPE_ZFS,
1401 	zfs_vfsinit,
1402 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS,
1403 	&zfs_mntopts
1404 };
1405 
1406 struct modlfs zfs_modlfs = {
1407 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
1408 };
1409