xref: /titanic_51/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 16dd44c265271a75647fb0bb41109bb7c585a526)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/sysmacros.h>
30 #include <sys/kmem.h>
31 #include <sys/pathname.h>
32 #include <sys/vnode.h>
33 #include <sys/vfs.h>
34 #include <sys/vfs_opreg.h>
35 #include <sys/mntent.h>
36 #include <sys/mount.h>
37 #include <sys/cmn_err.h>
38 #include "fs/fs_subr.h"
39 #include <sys/zfs_znode.h>
40 #include <sys/zfs_dir.h>
41 #include <sys/zil.h>
42 #include <sys/fs/zfs.h>
43 #include <sys/dmu.h>
44 #include <sys/dsl_prop.h>
45 #include <sys/dsl_dataset.h>
46 #include <sys/dsl_deleg.h>
47 #include <sys/spa.h>
48 #include <sys/zap.h>
49 #include <sys/varargs.h>
50 #include <sys/policy.h>
51 #include <sys/atomic.h>
52 #include <sys/mkdev.h>
53 #include <sys/modctl.h>
54 #include <sys/refstr.h>
55 #include <sys/zfs_ioctl.h>
56 #include <sys/zfs_ctldir.h>
57 #include <sys/zfs_fuid.h>
58 #include <sys/bootconf.h>
59 #include <sys/sunddi.h>
60 #include <sys/dnlc.h>
61 #include <sys/dmu_objset.h>
62 #include <sys/spa_boot.h>
63 
64 int zfsfstype;
65 vfsops_t *zfs_vfsops = NULL;
66 static major_t zfs_major;
67 static minor_t zfs_minor;
68 static kmutex_t	zfs_dev_mtx;
69 
70 static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
71 static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr);
72 static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
73 static int zfs_root(vfs_t *vfsp, vnode_t **vpp);
74 static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp);
75 static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp);
76 static void zfs_freevfs(vfs_t *vfsp);
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 /*
333  * The nbmand mount option can be changed at mount time.
334  * We can't allow it to be toggled on live file systems or incorrect
335  * behavior may be seen from cifs clients
336  *
337  * This property isn't registered via dsl_prop_register(), but this callback
338  * will be called when a file system is first mounted
339  */
340 static void
341 nbmand_changed_cb(void *arg, uint64_t newval)
342 {
343 	zfsvfs_t *zfsvfs = arg;
344 	if (newval == FALSE) {
345 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
346 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
347 	} else {
348 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
349 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
350 	}
351 }
352 
353 static void
354 snapdir_changed_cb(void *arg, uint64_t newval)
355 {
356 	zfsvfs_t *zfsvfs = arg;
357 
358 	zfsvfs->z_show_ctldir = newval;
359 }
360 
361 static void
362 vscan_changed_cb(void *arg, uint64_t newval)
363 {
364 	zfsvfs_t *zfsvfs = arg;
365 
366 	zfsvfs->z_vscan = newval;
367 }
368 
369 static void
370 acl_mode_changed_cb(void *arg, uint64_t newval)
371 {
372 	zfsvfs_t *zfsvfs = arg;
373 
374 	zfsvfs->z_acl_mode = newval;
375 }
376 
377 static void
378 acl_inherit_changed_cb(void *arg, uint64_t newval)
379 {
380 	zfsvfs_t *zfsvfs = arg;
381 
382 	zfsvfs->z_acl_inherit = newval;
383 }
384 
385 static int
386 zfs_register_callbacks(vfs_t *vfsp)
387 {
388 	struct dsl_dataset *ds = NULL;
389 	objset_t *os = NULL;
390 	zfsvfs_t *zfsvfs = NULL;
391 	uint64_t nbmand;
392 	int readonly, do_readonly = B_FALSE;
393 	int setuid, do_setuid = B_FALSE;
394 	int exec, do_exec = B_FALSE;
395 	int devices, do_devices = B_FALSE;
396 	int xattr, do_xattr = B_FALSE;
397 	int atime, do_atime = B_FALSE;
398 	int error = 0;
399 
400 	ASSERT(vfsp);
401 	zfsvfs = vfsp->vfs_data;
402 	ASSERT(zfsvfs);
403 	os = zfsvfs->z_os;
404 
405 	/*
406 	 * The act of registering our callbacks will destroy any mount
407 	 * options we may have.  In order to enable temporary overrides
408 	 * of mount options, we stash away the current values and
409 	 * restore them after we register the callbacks.
410 	 */
411 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
412 		readonly = B_TRUE;
413 		do_readonly = B_TRUE;
414 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
415 		readonly = B_FALSE;
416 		do_readonly = B_TRUE;
417 	}
418 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
419 		devices = B_FALSE;
420 		setuid = B_FALSE;
421 		do_devices = B_TRUE;
422 		do_setuid = B_TRUE;
423 	} else {
424 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
425 			devices = B_FALSE;
426 			do_devices = B_TRUE;
427 		} else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
428 			devices = B_TRUE;
429 			do_devices = B_TRUE;
430 		}
431 
432 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
433 			setuid = B_FALSE;
434 			do_setuid = B_TRUE;
435 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
436 			setuid = B_TRUE;
437 			do_setuid = B_TRUE;
438 		}
439 	}
440 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
441 		exec = B_FALSE;
442 		do_exec = B_TRUE;
443 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
444 		exec = B_TRUE;
445 		do_exec = B_TRUE;
446 	}
447 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
448 		xattr = B_FALSE;
449 		do_xattr = B_TRUE;
450 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
451 		xattr = B_TRUE;
452 		do_xattr = B_TRUE;
453 	}
454 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
455 		atime = B_FALSE;
456 		do_atime = B_TRUE;
457 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
458 		atime = B_TRUE;
459 		do_atime = B_TRUE;
460 	}
461 
462 	/*
463 	 * nbmand is a special property.  It can only be changed at
464 	 * mount time.
465 	 *
466 	 * This is weird, but it is documented to only be changeable
467 	 * at mount time.
468 	 */
469 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
470 		nbmand = B_FALSE;
471 	} else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
472 		nbmand = B_TRUE;
473 	} else {
474 		char osname[MAXNAMELEN];
475 
476 		dmu_objset_name(os, osname);
477 		if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
478 		    NULL)) {
479 			return (error);
480 		}
481 	}
482 
483 	/*
484 	 * Register property callbacks.
485 	 *
486 	 * It would probably be fine to just check for i/o error from
487 	 * the first prop_register(), but I guess I like to go
488 	 * overboard...
489 	 */
490 	ds = dmu_objset_ds(os);
491 	error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
492 	error = error ? error : dsl_prop_register(ds,
493 	    "xattr", xattr_changed_cb, zfsvfs);
494 	error = error ? error : dsl_prop_register(ds,
495 	    "recordsize", blksz_changed_cb, zfsvfs);
496 	error = error ? error : dsl_prop_register(ds,
497 	    "readonly", readonly_changed_cb, zfsvfs);
498 	error = error ? error : dsl_prop_register(ds,
499 	    "devices", devices_changed_cb, zfsvfs);
500 	error = error ? error : dsl_prop_register(ds,
501 	    "setuid", setuid_changed_cb, zfsvfs);
502 	error = error ? error : dsl_prop_register(ds,
503 	    "exec", exec_changed_cb, zfsvfs);
504 	error = error ? error : dsl_prop_register(ds,
505 	    "snapdir", snapdir_changed_cb, zfsvfs);
506 	error = error ? error : dsl_prop_register(ds,
507 	    "aclmode", acl_mode_changed_cb, zfsvfs);
508 	error = error ? error : dsl_prop_register(ds,
509 	    "aclinherit", acl_inherit_changed_cb, zfsvfs);
510 	error = error ? error : dsl_prop_register(ds,
511 	    "vscan", vscan_changed_cb, zfsvfs);
512 	if (error)
513 		goto unregister;
514 
515 	/*
516 	 * Invoke our callbacks to restore temporary mount options.
517 	 */
518 	if (do_readonly)
519 		readonly_changed_cb(zfsvfs, readonly);
520 	if (do_setuid)
521 		setuid_changed_cb(zfsvfs, setuid);
522 	if (do_exec)
523 		exec_changed_cb(zfsvfs, exec);
524 	if (do_devices)
525 		devices_changed_cb(zfsvfs, devices);
526 	if (do_xattr)
527 		xattr_changed_cb(zfsvfs, xattr);
528 	if (do_atime)
529 		atime_changed_cb(zfsvfs, atime);
530 
531 	nbmand_changed_cb(zfsvfs, nbmand);
532 
533 	return (0);
534 
535 unregister:
536 	/*
537 	 * We may attempt to unregister some callbacks that are not
538 	 * registered, but this is OK; it will simply return ENOMSG,
539 	 * which we will ignore.
540 	 */
541 	(void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
542 	(void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
543 	(void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
544 	(void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
545 	(void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs);
546 	(void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
547 	(void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
548 	(void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
549 	(void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
550 	(void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
551 	    zfsvfs);
552 	(void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
553 	return (error);
554 
555 }
556 
557 static int
558 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
559 {
560 	int error;
561 
562 	error = zfs_register_callbacks(zfsvfs->z_vfs);
563 	if (error)
564 		return (error);
565 
566 	/*
567 	 * Set the objset user_ptr to track its zfsvfs.
568 	 */
569 	mutex_enter(&zfsvfs->z_os->os->os_user_ptr_lock);
570 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
571 	mutex_exit(&zfsvfs->z_os->os->os_user_ptr_lock);
572 
573 	/*
574 	 * If we are not mounting (ie: online recv), then we don't
575 	 * have to worry about replaying the log as we blocked all
576 	 * operations out since we closed the ZIL.
577 	 */
578 	if (mounting) {
579 		boolean_t readonly;
580 
581 		/*
582 		 * During replay we remove the read only flag to
583 		 * allow replays to succeed.
584 		 */
585 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
586 		if (readonly != 0)
587 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
588 		else
589 			zfs_unlinked_drain(zfsvfs);
590 
591 		zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
592 		if (zil_disable) {
593 			zil_destroy(zfsvfs->z_log, 0);
594 			zfsvfs->z_log = NULL;
595 		} else {
596 			/*
597 			 * Parse and replay the intent log.
598 			 *
599 			 * Because of ziltest, this must be done after
600 			 * zfs_unlinked_drain().  (Further note: ziltest
601 			 * doesn't use readonly mounts, where
602 			 * zfs_unlinked_drain() isn't called.)  This is because
603 			 * ziltest causes spa_sync() to think it's committed,
604 			 * but actually it is not, so the intent log contains
605 			 * many txg's worth of changes.
606 			 *
607 			 * In particular, if object N is in the unlinked set in
608 			 * the last txg to actually sync, then it could be
609 			 * actually freed in a later txg and then reallocated
610 			 * in a yet later txg.  This would write a "create
611 			 * object N" record to the intent log.  Normally, this
612 			 * would be fine because the spa_sync() would have
613 			 * written out the fact that object N is free, before
614 			 * we could write the "create object N" intent log
615 			 * record.
616 			 *
617 			 * But when we are in ziltest mode, we advance the "open
618 			 * txg" without actually spa_sync()-ing the changes to
619 			 * disk.  So we would see that object N is still
620 			 * allocated and in the unlinked set, and there is an
621 			 * intent log record saying to allocate it.
622 			 */
623 			zfsvfs->z_replay = B_TRUE;
624 			zil_replay(zfsvfs->z_os, zfsvfs, zfs_replay_vector);
625 			zfsvfs->z_replay = B_FALSE;
626 		}
627 		zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
628 	}
629 
630 	return (0);
631 }
632 
633 static void
634 zfs_freezfsvfs(zfsvfs_t *zfsvfs)
635 {
636 	mutex_destroy(&zfsvfs->z_znodes_lock);
637 	mutex_destroy(&zfsvfs->z_online_recv_lock);
638 	mutex_destroy(&zfsvfs->z_lock);
639 	list_destroy(&zfsvfs->z_all_znodes);
640 	rrw_destroy(&zfsvfs->z_teardown_lock);
641 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
642 	rw_destroy(&zfsvfs->z_fuid_lock);
643 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
644 }
645 
646 static int
647 zfs_domount(vfs_t *vfsp, char *osname)
648 {
649 	dev_t mount_dev;
650 	uint64_t recordsize, readonly;
651 	int error = 0;
652 	int mode;
653 	zfsvfs_t *zfsvfs;
654 	znode_t *zp = NULL;
655 
656 	ASSERT(vfsp);
657 	ASSERT(osname);
658 
659 	/*
660 	 * Initialize the zfs-specific filesystem structure.
661 	 * Should probably make this a kmem cache, shuffle fields,
662 	 * and just bzero up to z_hold_mtx[].
663 	 */
664 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
665 	zfsvfs->z_vfs = vfsp;
666 	zfsvfs->z_parent = zfsvfs;
667 	zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
668 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
669 
670 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
671 	mutex_init(&zfsvfs->z_online_recv_lock, NULL, MUTEX_DEFAULT, NULL);
672 	mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
673 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
674 	    offsetof(znode_t, z_link_node));
675 	rrw_init(&zfsvfs->z_teardown_lock);
676 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
677 	rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
678 
679 	/* Initialize the generic filesystem structure. */
680 	vfsp->vfs_bcount = 0;
681 	vfsp->vfs_data = NULL;
682 
683 	if (zfs_create_unique_device(&mount_dev) == -1) {
684 		error = ENODEV;
685 		goto out;
686 	}
687 	ASSERT(vfs_devismounted(mount_dev) == 0);
688 
689 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
690 	    NULL))
691 		goto out;
692 
693 	vfsp->vfs_dev = mount_dev;
694 	vfsp->vfs_fstype = zfsfstype;
695 	vfsp->vfs_bsize = recordsize;
696 	vfsp->vfs_flag |= VFS_NOTRUNC;
697 	vfsp->vfs_data = zfsvfs;
698 
699 	if (error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL))
700 		goto out;
701 
702 	mode = DS_MODE_OWNER;
703 	if (readonly)
704 		mode |= DS_MODE_READONLY;
705 
706 	error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
707 	if (error == EROFS) {
708 		mode = DS_MODE_OWNER | DS_MODE_READONLY;
709 		error = dmu_objset_open(osname, DMU_OST_ZFS, mode,
710 		    &zfsvfs->z_os);
711 	}
712 
713 	if (error)
714 		goto out;
715 
716 	if (error = zfs_init_fs(zfsvfs, &zp))
717 		goto out;
718 
719 	/* The call to zfs_init_fs leaves the vnode held, release it here. */
720 	VN_RELE(ZTOV(zp));
721 
722 	/*
723 	 * Set features for file system.
724 	 */
725 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
726 	if (zfsvfs->z_use_fuids) {
727 		vfs_set_feature(vfsp, VFSFT_XVATTR);
728 		vfs_set_feature(vfsp, VFSFT_SYSATTR_VIEWS);
729 		vfs_set_feature(vfsp, VFSFT_ACEMASKONACCESS);
730 		vfs_set_feature(vfsp, VFSFT_ACLONCREATE);
731 	}
732 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
733 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
734 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
735 		vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
736 	} else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
737 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
738 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
739 	}
740 
741 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
742 		uint64_t pval;
743 
744 		ASSERT(mode & DS_MODE_READONLY);
745 		atime_changed_cb(zfsvfs, B_FALSE);
746 		readonly_changed_cb(zfsvfs, B_TRUE);
747 		if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
748 			goto out;
749 		xattr_changed_cb(zfsvfs, pval);
750 		zfsvfs->z_issnap = B_TRUE;
751 	} else {
752 		error = zfsvfs_setup(zfsvfs, B_TRUE);
753 	}
754 
755 	if (!zfsvfs->z_issnap)
756 		zfsctl_create(zfsvfs);
757 out:
758 	if (error) {
759 		if (zfsvfs->z_os)
760 			dmu_objset_close(zfsvfs->z_os);
761 		zfs_freezfsvfs(zfsvfs);
762 	} else {
763 		atomic_add_32(&zfs_active_fs_count, 1);
764 	}
765 
766 	return (error);
767 }
768 
769 void
770 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
771 {
772 	objset_t *os = zfsvfs->z_os;
773 	struct dsl_dataset *ds;
774 
775 	/*
776 	 * Unregister properties.
777 	 */
778 	if (!dmu_objset_is_snapshot(os)) {
779 		ds = dmu_objset_ds(os);
780 		VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
781 		    zfsvfs) == 0);
782 
783 		VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
784 		    zfsvfs) == 0);
785 
786 		VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
787 		    zfsvfs) == 0);
788 
789 		VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
790 		    zfsvfs) == 0);
791 
792 		VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
793 		    zfsvfs) == 0);
794 
795 		VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
796 		    zfsvfs) == 0);
797 
798 		VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
799 		    zfsvfs) == 0);
800 
801 		VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
802 		    zfsvfs) == 0);
803 
804 		VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
805 		    zfsvfs) == 0);
806 
807 		VERIFY(dsl_prop_unregister(ds, "aclinherit",
808 		    acl_inherit_changed_cb, zfsvfs) == 0);
809 
810 		VERIFY(dsl_prop_unregister(ds, "vscan",
811 		    vscan_changed_cb, zfsvfs) == 0);
812 	}
813 }
814 
815 /*
816  * Convert a decimal digit string to a uint64_t integer.
817  */
818 static int
819 str_to_uint64(char *str, uint64_t *objnum)
820 {
821 	uint64_t num = 0;
822 
823 	while (*str) {
824 		if (*str < '0' || *str > '9')
825 			return (EINVAL);
826 
827 		num = num*10 + *str++ - '0';
828 	}
829 
830 	*objnum = num;
831 	return (0);
832 }
833 
834 /*
835  * The boot path passed from the boot loader is in the form of
836  * "rootpool-name/root-filesystem-object-number'. Convert this
837  * string to a dataset name: "rootpool-name/root-filesystem-name".
838  */
839 static int
840 zfs_parse_bootfs(char *bpath, char *outpath)
841 {
842 	char *slashp;
843 	uint64_t objnum;
844 	int error;
845 
846 	if (*bpath == 0 || *bpath == '/')
847 		return (EINVAL);
848 
849 	(void) strcpy(outpath, bpath);
850 
851 	slashp = strchr(bpath, '/');
852 
853 	/* if no '/', just return the pool name */
854 	if (slashp == NULL) {
855 		return (0);
856 	}
857 
858 	/* if not a number, just return the root dataset name */
859 	if (str_to_uint64(slashp+1, &objnum)) {
860 		return (0);
861 	}
862 
863 	*slashp = '\0';
864 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
865 	*slashp = '/';
866 
867 	return (error);
868 }
869 
870 static int
871 zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
872 {
873 	int error = 0;
874 	static int zfsrootdone = 0;
875 	zfsvfs_t *zfsvfs = NULL;
876 	znode_t *zp = NULL;
877 	vnode_t *vp = NULL;
878 	char *zfs_bootfs;
879 	char *zfs_devid;
880 
881 	ASSERT(vfsp);
882 
883 	/*
884 	 * The filesystem that we mount as root is defined in the
885 	 * boot property "zfs-bootfs" with a format of
886 	 * "poolname/root-dataset-objnum".
887 	 */
888 	if (why == ROOT_INIT) {
889 		if (zfsrootdone++)
890 			return (EBUSY);
891 		/*
892 		 * the process of doing a spa_load will require the
893 		 * clock to be set before we could (for example) do
894 		 * something better by looking at the timestamp on
895 		 * an uberblock, so just set it to -1.
896 		 */
897 		clkset(-1);
898 
899 		if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
900 			cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
901 			    "bootfs name");
902 			return (EINVAL);
903 		}
904 		zfs_devid = spa_get_bootprop("diskdevid");
905 		error = spa_import_rootpool(rootfs.bo_name, zfs_devid);
906 		if (zfs_devid)
907 			spa_free_bootprop(zfs_devid);
908 		if (error) {
909 			spa_free_bootprop(zfs_bootfs);
910 			cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
911 			    error);
912 			return (error);
913 		}
914 		if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
915 			spa_free_bootprop(zfs_bootfs);
916 			cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
917 			    error);
918 			return (error);
919 		}
920 
921 		spa_free_bootprop(zfs_bootfs);
922 
923 		if (error = vfs_lock(vfsp))
924 			return (error);
925 
926 		if (error = zfs_domount(vfsp, rootfs.bo_name)) {
927 			cmn_err(CE_NOTE, "zfs_domount: error %d", error);
928 			goto out;
929 		}
930 
931 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
932 		ASSERT(zfsvfs);
933 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
934 			cmn_err(CE_NOTE, "zfs_zget: error %d", error);
935 			goto out;
936 		}
937 
938 		vp = ZTOV(zp);
939 		mutex_enter(&vp->v_lock);
940 		vp->v_flag |= VROOT;
941 		mutex_exit(&vp->v_lock);
942 		rootvp = vp;
943 
944 		/*
945 		 * Leave rootvp held.  The root file system is never unmounted.
946 		 */
947 
948 		vfs_add((struct vnode *)0, vfsp,
949 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
950 out:
951 		vfs_unlock(vfsp);
952 		return (error);
953 	} else if (why == ROOT_REMOUNT) {
954 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
955 		vfsp->vfs_flag |= VFS_REMOUNT;
956 
957 		/* refresh mount options */
958 		zfs_unregister_callbacks(vfsp->vfs_data);
959 		return (zfs_register_callbacks(vfsp));
960 
961 	} else if (why == ROOT_UNMOUNT) {
962 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
963 		(void) zfs_sync(vfsp, 0, 0);
964 		return (0);
965 	}
966 
967 	/*
968 	 * if "why" is equal to anything else other than ROOT_INIT,
969 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
970 	 */
971 	return (ENOTSUP);
972 }
973 
974 /*ARGSUSED*/
975 static int
976 zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
977 {
978 	char		*osname;
979 	pathname_t	spn;
980 	int		error = 0;
981 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
982 	    UIO_SYSSPACE : UIO_USERSPACE;
983 	int		canwrite;
984 
985 	if (mvp->v_type != VDIR)
986 		return (ENOTDIR);
987 
988 	mutex_enter(&mvp->v_lock);
989 	if ((uap->flags & MS_REMOUNT) == 0 &&
990 	    (uap->flags & MS_OVERLAY) == 0 &&
991 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
992 		mutex_exit(&mvp->v_lock);
993 		return (EBUSY);
994 	}
995 	mutex_exit(&mvp->v_lock);
996 
997 	/*
998 	 * ZFS does not support passing unparsed data in via MS_DATA.
999 	 * Users should use the MS_OPTIONSTR interface; this means
1000 	 * that all option parsing is already done and the options struct
1001 	 * can be interrogated.
1002 	 */
1003 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
1004 		return (EINVAL);
1005 
1006 	/*
1007 	 * Get the objset name (the "special" mount argument).
1008 	 */
1009 	if (error = pn_get(uap->spec, fromspace, &spn))
1010 		return (error);
1011 
1012 	osname = spn.pn_path;
1013 
1014 	/*
1015 	 * Check for mount privilege?
1016 	 *
1017 	 * If we don't have privilege then see if
1018 	 * we have local permission to allow it
1019 	 */
1020 	error = secpolicy_fs_mount(cr, mvp, vfsp);
1021 	if (error) {
1022 		error = dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr);
1023 		if (error == 0) {
1024 			vattr_t		vattr;
1025 
1026 			/*
1027 			 * Make sure user is the owner of the mount point
1028 			 * or has sufficient privileges.
1029 			 */
1030 
1031 			vattr.va_mask = AT_UID;
1032 
1033 			if (error = VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
1034 				goto out;
1035 			}
1036 
1037 			if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 &&
1038 			    VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) {
1039 				error = EPERM;
1040 				goto out;
1041 			}
1042 
1043 			secpolicy_fs_mount_clearopts(cr, vfsp);
1044 		} else {
1045 			goto out;
1046 		}
1047 	}
1048 
1049 	/*
1050 	 * Refuse to mount a filesystem if we are in a local zone and the
1051 	 * dataset is not visible.
1052 	 */
1053 	if (!INGLOBALZONE(curproc) &&
1054 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1055 		error = EPERM;
1056 		goto out;
1057 	}
1058 
1059 	/*
1060 	 * When doing a remount, we simply refresh our temporary properties
1061 	 * according to those options set in the current VFS options.
1062 	 */
1063 	if (uap->flags & MS_REMOUNT) {
1064 		/* refresh mount options */
1065 		zfs_unregister_callbacks(vfsp->vfs_data);
1066 		error = zfs_register_callbacks(vfsp);
1067 		goto out;
1068 	}
1069 
1070 	error = zfs_domount(vfsp, osname);
1071 
1072 out:
1073 	pn_free(&spn);
1074 	return (error);
1075 }
1076 
1077 static int
1078 zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1079 {
1080 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1081 	dev32_t d32;
1082 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
1083 
1084 	ZFS_ENTER(zfsvfs);
1085 
1086 	dmu_objset_space(zfsvfs->z_os,
1087 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
1088 
1089 	/*
1090 	 * The underlying storage pool actually uses multiple block sizes.
1091 	 * We report the fragsize as the smallest block size we support,
1092 	 * and we report our blocksize as the filesystem's maximum blocksize.
1093 	 */
1094 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1095 	statp->f_bsize = zfsvfs->z_max_blksz;
1096 
1097 	/*
1098 	 * The following report "total" blocks of various kinds in the
1099 	 * file system, but reported in terms of f_frsize - the
1100 	 * "fragment" size.
1101 	 */
1102 
1103 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
1104 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
1105 	statp->f_bavail = statp->f_bfree; /* no root reservation */
1106 
1107 	/*
1108 	 * statvfs() should really be called statufs(), because it assumes
1109 	 * static metadata.  ZFS doesn't preallocate files, so the best
1110 	 * we can do is report the max that could possibly fit in f_files,
1111 	 * and that minus the number actually used in f_ffree.
1112 	 * For f_ffree, report the smaller of the number of object available
1113 	 * and the number of blocks (each object will take at least a block).
1114 	 */
1115 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
1116 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
1117 	statp->f_files = statp->f_ffree + usedobjs;
1118 
1119 	(void) cmpldev(&d32, vfsp->vfs_dev);
1120 	statp->f_fsid = d32;
1121 
1122 	/*
1123 	 * We're a zfs filesystem.
1124 	 */
1125 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1126 
1127 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1128 
1129 	statp->f_namemax = ZFS_MAXNAMELEN;
1130 
1131 	/*
1132 	 * We have all of 32 characters to stuff a string here.
1133 	 * Is there anything useful we could/should provide?
1134 	 */
1135 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
1136 
1137 	ZFS_EXIT(zfsvfs);
1138 	return (0);
1139 }
1140 
1141 static int
1142 zfs_root(vfs_t *vfsp, vnode_t **vpp)
1143 {
1144 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1145 	znode_t *rootzp;
1146 	int error;
1147 
1148 	ZFS_ENTER(zfsvfs);
1149 
1150 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1151 	if (error == 0)
1152 		*vpp = ZTOV(rootzp);
1153 
1154 	ZFS_EXIT(zfsvfs);
1155 	return (error);
1156 }
1157 
1158 /*
1159  * Teardown the zfsvfs::z_os.
1160  *
1161  * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1162  * and 'z_teardown_inactive_lock' held.
1163  */
1164 static int
1165 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1166 {
1167 	znode_t	*zp;
1168 
1169 	rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1170 
1171 	if (!unmounting) {
1172 		/*
1173 		 * We purge the parent filesystem's vfsp as the parent
1174 		 * filesystem and all of its snapshots have their vnode's
1175 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
1176 		 * 'z_parent' is self referential for non-snapshots.
1177 		 */
1178 		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1179 	}
1180 
1181 	/*
1182 	 * Close the zil. NB: Can't close the zil while zfs_inactive
1183 	 * threads are blocked as zil_close can call zfs_inactive.
1184 	 */
1185 	if (zfsvfs->z_log) {
1186 		zil_close(zfsvfs->z_log);
1187 		zfsvfs->z_log = NULL;
1188 	}
1189 
1190 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
1191 
1192 	/*
1193 	 * If we are not unmounting (ie: online recv) and someone already
1194 	 * unmounted this file system while we were doing the switcheroo,
1195 	 * or a reopen of z_os failed then just bail out now.
1196 	 */
1197 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1198 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
1199 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1200 		return (EIO);
1201 	}
1202 
1203 	/*
1204 	 * At this point there are no vops active, and any new vops will
1205 	 * fail with EIO since we have z_teardown_lock for writer (only
1206 	 * relavent for forced unmount).
1207 	 *
1208 	 * Release all holds on dbufs.
1209 	 */
1210 	mutex_enter(&zfsvfs->z_znodes_lock);
1211 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
1212 	    zp = list_next(&zfsvfs->z_all_znodes, zp))
1213 		if (zp->z_dbuf) {
1214 			ASSERT(ZTOV(zp)->v_count > 0);
1215 			zfs_znode_dmu_fini(zp);
1216 		}
1217 	mutex_exit(&zfsvfs->z_znodes_lock);
1218 
1219 	/*
1220 	 * If we are unmounting, set the unmounted flag and let new vops
1221 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
1222 	 * other vops will fail with EIO.
1223 	 */
1224 	if (unmounting) {
1225 		zfsvfs->z_unmounted = B_TRUE;
1226 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1227 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
1228 	}
1229 
1230 	/*
1231 	 * z_os will be NULL if there was an error in attempting to reopen
1232 	 * zfsvfs, so just return as the properties had already been
1233 	 * unregistered and cached data had been evicted before.
1234 	 */
1235 	if (zfsvfs->z_os == NULL)
1236 		return (0);
1237 
1238 	/*
1239 	 * Unregister properties.
1240 	 */
1241 	zfs_unregister_callbacks(zfsvfs);
1242 
1243 	/*
1244 	 * Evict cached data
1245 	 */
1246 	if (dmu_objset_evict_dbufs(zfsvfs->z_os)) {
1247 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1248 		(void) dmu_objset_evict_dbufs(zfsvfs->z_os);
1249 	}
1250 
1251 	return (0);
1252 }
1253 
1254 /*ARGSUSED*/
1255 static int
1256 zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1257 {
1258 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1259 	objset_t *os;
1260 	int ret;
1261 
1262 	ret = secpolicy_fs_unmount(cr, vfsp);
1263 	if (ret) {
1264 		ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
1265 		    ZFS_DELEG_PERM_MOUNT, cr);
1266 		if (ret)
1267 			return (ret);
1268 	}
1269 
1270 	/*
1271 	 * We purge the parent filesystem's vfsp as the parent filesystem
1272 	 * and all of its snapshots have their vnode's v_vfsp set to the
1273 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
1274 	 * referential for non-snapshots.
1275 	 */
1276 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1277 
1278 	/*
1279 	 * Unmount any snapshots mounted under .zfs before unmounting the
1280 	 * dataset itself.
1281 	 */
1282 	if (zfsvfs->z_ctldir != NULL &&
1283 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1284 		return (ret);
1285 	}
1286 
1287 	if (!(fflag & MS_FORCE)) {
1288 		/*
1289 		 * Check the number of active vnodes in the file system.
1290 		 * Our count is maintained in the vfs structure, but the
1291 		 * number is off by 1 to indicate a hold on the vfs
1292 		 * structure itself.
1293 		 *
1294 		 * The '.zfs' directory maintains a reference of its
1295 		 * own, and any active references underneath are
1296 		 * reflected in the vnode count.
1297 		 */
1298 		if (zfsvfs->z_ctldir == NULL) {
1299 			if (vfsp->vfs_count > 1)
1300 				return (EBUSY);
1301 		} else {
1302 			if (vfsp->vfs_count > 2 ||
1303 			    zfsvfs->z_ctldir->v_count > 1)
1304 				return (EBUSY);
1305 		}
1306 	}
1307 
1308 	vfsp->vfs_flag |= VFS_UNMOUNTED;
1309 
1310 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1311 	os = zfsvfs->z_os;
1312 
1313 	/*
1314 	 * z_os will be NULL if there was an error in
1315 	 * attempting to reopen zfsvfs.
1316 	 */
1317 	if (os != NULL) {
1318 		/*
1319 		 * Unset the objset user_ptr.
1320 		 */
1321 		mutex_enter(&os->os->os_user_ptr_lock);
1322 		dmu_objset_set_user(os, NULL);
1323 		mutex_exit(&os->os->os_user_ptr_lock);
1324 
1325 		/*
1326 		 * Finally release the objset
1327 		 */
1328 		dmu_objset_close(os);
1329 	}
1330 
1331 	/*
1332 	 * We can now safely destroy the '.zfs' directory node.
1333 	 */
1334 	if (zfsvfs->z_ctldir != NULL)
1335 		zfsctl_destroy(zfsvfs);
1336 
1337 	return (0);
1338 }
1339 
1340 static int
1341 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1342 {
1343 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1344 	znode_t		*zp;
1345 	uint64_t	object = 0;
1346 	uint64_t	fid_gen = 0;
1347 	uint64_t	gen_mask;
1348 	uint64_t	zp_gen;
1349 	int 		i, err;
1350 
1351 	*vpp = NULL;
1352 
1353 	ZFS_ENTER(zfsvfs);
1354 
1355 	if (fidp->fid_len == LONG_FID_LEN) {
1356 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1357 		uint64_t	objsetid = 0;
1358 		uint64_t	setgen = 0;
1359 
1360 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1361 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1362 
1363 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1364 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1365 
1366 		ZFS_EXIT(zfsvfs);
1367 
1368 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1369 		if (err)
1370 			return (EINVAL);
1371 		ZFS_ENTER(zfsvfs);
1372 	}
1373 
1374 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1375 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1376 
1377 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1378 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1379 
1380 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1381 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1382 	} else {
1383 		ZFS_EXIT(zfsvfs);
1384 		return (EINVAL);
1385 	}
1386 
1387 	/* A zero fid_gen means we are in the .zfs control directories */
1388 	if (fid_gen == 0 &&
1389 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1390 		*vpp = zfsvfs->z_ctldir;
1391 		ASSERT(*vpp != NULL);
1392 		if (object == ZFSCTL_INO_SNAPDIR) {
1393 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1394 			    0, NULL, NULL, NULL, NULL, NULL) == 0);
1395 		} else {
1396 			VN_HOLD(*vpp);
1397 		}
1398 		ZFS_EXIT(zfsvfs);
1399 		return (0);
1400 	}
1401 
1402 	gen_mask = -1ULL >> (64 - 8 * i);
1403 
1404 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1405 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1406 		ZFS_EXIT(zfsvfs);
1407 		return (err);
1408 	}
1409 	zp_gen = zp->z_phys->zp_gen & gen_mask;
1410 	if (zp_gen == 0)
1411 		zp_gen = 1;
1412 	if (zp->z_unlinked || zp_gen != fid_gen) {
1413 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1414 		VN_RELE(ZTOV(zp));
1415 		ZFS_EXIT(zfsvfs);
1416 		return (EINVAL);
1417 	}
1418 
1419 	*vpp = ZTOV(zp);
1420 	ZFS_EXIT(zfsvfs);
1421 	return (0);
1422 }
1423 
1424 /*
1425  * Block out VOPs and close zfsvfs_t::z_os
1426  *
1427  * Note, if successful, then we return with the 'z_teardown_lock' and
1428  * 'z_teardown_inactive_lock' write held.
1429  */
1430 int
1431 zfs_suspend_fs(zfsvfs_t *zfsvfs, char *name, int *mode)
1432 {
1433 	int error;
1434 
1435 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
1436 		return (error);
1437 
1438 	*mode = zfsvfs->z_os->os_mode;
1439 	dmu_objset_name(zfsvfs->z_os, name);
1440 	dmu_objset_close(zfsvfs->z_os);
1441 
1442 	return (0);
1443 }
1444 
1445 /*
1446  * Reopen zfsvfs_t::z_os and release VOPs.
1447  */
1448 int
1449 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname, int mode)
1450 {
1451 	int err;
1452 
1453 	ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
1454 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
1455 
1456 	err = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
1457 	if (err) {
1458 		zfsvfs->z_os = NULL;
1459 	} else {
1460 		znode_t *zp;
1461 
1462 		VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
1463 
1464 		/*
1465 		 * Attempt to re-establish all the active znodes with
1466 		 * their dbufs.  If a zfs_rezget() fails, then we'll let
1467 		 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
1468 		 * when they try to use their znode.
1469 		 */
1470 		mutex_enter(&zfsvfs->z_znodes_lock);
1471 		for (zp = list_head(&zfsvfs->z_all_znodes); zp;
1472 		    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1473 			(void) zfs_rezget(zp);
1474 		}
1475 		mutex_exit(&zfsvfs->z_znodes_lock);
1476 
1477 	}
1478 
1479 	/* release the VOPs */
1480 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
1481 	rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1482 
1483 	if (err) {
1484 		/*
1485 		 * Since we couldn't reopen zfsvfs::z_os, force
1486 		 * unmount this file system.
1487 		 */
1488 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
1489 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
1490 	}
1491 	return (err);
1492 }
1493 
1494 static void
1495 zfs_freevfs(vfs_t *vfsp)
1496 {
1497 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1498 	int i;
1499 
1500 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1501 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1502 
1503 	zfs_fuid_destroy(zfsvfs);
1504 	zfs_freezfsvfs(zfsvfs);
1505 
1506 	atomic_add_32(&zfs_active_fs_count, -1);
1507 }
1508 
1509 /*
1510  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
1511  * so we can't safely do any non-idempotent initialization here.
1512  * Leave that to zfs_init() and zfs_fini(), which are called
1513  * from the module's _init() and _fini() entry points.
1514  */
1515 /*ARGSUSED*/
1516 static int
1517 zfs_vfsinit(int fstype, char *name)
1518 {
1519 	int error;
1520 
1521 	zfsfstype = fstype;
1522 
1523 	/*
1524 	 * Setup vfsops and vnodeops tables.
1525 	 */
1526 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
1527 	if (error != 0) {
1528 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
1529 	}
1530 
1531 	error = zfs_create_op_tables();
1532 	if (error) {
1533 		zfs_remove_op_tables();
1534 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
1535 		(void) vfs_freevfsops_by_type(zfsfstype);
1536 		return (error);
1537 	}
1538 
1539 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
1540 
1541 	/*
1542 	 * Unique major number for all zfs mounts.
1543 	 * If we run out of 32-bit minors, we'll getudev() another major.
1544 	 */
1545 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
1546 	zfs_minor = ZFS_MIN_MINOR;
1547 
1548 	return (0);
1549 }
1550 
1551 void
1552 zfs_init(void)
1553 {
1554 	/*
1555 	 * Initialize .zfs directory structures
1556 	 */
1557 	zfsctl_init();
1558 
1559 	/*
1560 	 * Initialize znode cache, vnode ops, etc...
1561 	 */
1562 	zfs_znode_init();
1563 }
1564 
1565 void
1566 zfs_fini(void)
1567 {
1568 	zfsctl_fini();
1569 	zfs_znode_fini();
1570 }
1571 
1572 int
1573 zfs_busy(void)
1574 {
1575 	return (zfs_active_fs_count != 0);
1576 }
1577 
1578 int
1579 zfs_set_version(const char *name, uint64_t newvers)
1580 {
1581 	int error;
1582 	objset_t *os;
1583 	dmu_tx_t *tx;
1584 	uint64_t curvers;
1585 
1586 	/*
1587 	 * XXX for now, require that the filesystem be unmounted.  Would
1588 	 * be nice to find the zfsvfs_t and just update that if
1589 	 * possible.
1590 	 */
1591 
1592 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
1593 		return (EINVAL);
1594 
1595 	error = dmu_objset_open(name, DMU_OST_ZFS, DS_MODE_OWNER, &os);
1596 	if (error)
1597 		return (error);
1598 
1599 	error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1600 	    8, 1, &curvers);
1601 	if (error)
1602 		goto out;
1603 	if (newvers < curvers) {
1604 		error = EINVAL;
1605 		goto out;
1606 	}
1607 
1608 	tx = dmu_tx_create(os);
1609 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 0, ZPL_VERSION_STR);
1610 	error = dmu_tx_assign(tx, TXG_WAIT);
1611 	if (error) {
1612 		dmu_tx_abort(tx);
1613 		goto out;
1614 	}
1615 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1,
1616 	    &newvers, tx);
1617 
1618 	spa_history_internal_log(LOG_DS_UPGRADE,
1619 	    dmu_objset_spa(os), tx, CRED(),
1620 	    "oldver=%llu newver=%llu dataset = %llu", curvers, newvers,
1621 	    dmu_objset_id(os));
1622 	dmu_tx_commit(tx);
1623 
1624 out:
1625 	dmu_objset_close(os);
1626 	return (error);
1627 }
1628 
1629 /*
1630  * Read a property stored within the master node.
1631  */
1632 int
1633 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
1634 {
1635 	const char *pname;
1636 	int error = ENOENT;
1637 
1638 	/*
1639 	 * Look up the file system's value for the property.  For the
1640 	 * version property, we look up a slightly different string.
1641 	 */
1642 	if (prop == ZFS_PROP_VERSION)
1643 		pname = ZPL_VERSION_STR;
1644 	else
1645 		pname = zfs_prop_to_name(prop);
1646 
1647 	if (os != NULL)
1648 		error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
1649 
1650 	if (error == ENOENT) {
1651 		/* No value set, use the default value */
1652 		switch (prop) {
1653 		case ZFS_PROP_VERSION:
1654 			*value = ZPL_VERSION;
1655 			break;
1656 		case ZFS_PROP_NORMALIZE:
1657 		case ZFS_PROP_UTF8ONLY:
1658 			*value = 0;
1659 			break;
1660 		case ZFS_PROP_CASE:
1661 			*value = ZFS_CASE_SENSITIVE;
1662 			break;
1663 		default:
1664 			return (error);
1665 		}
1666 		error = 0;
1667 	}
1668 	return (error);
1669 }
1670 
1671 static vfsdef_t vfw = {
1672 	VFSDEF_VERSION,
1673 	MNTTYPE_ZFS,
1674 	zfs_vfsinit,
1675 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS|
1676 	    VSW_XID,
1677 	&zfs_mntopts
1678 };
1679 
1680 struct modlfs zfs_modlfs = {
1681 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
1682 };
1683