xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 874395d5f8cae2b9cd2d1fcbfcfe963a0c23966d)
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/zfs_fuid.h>
60 #include <sys/bootconf.h>
61 #include <sys/sunddi.h>
62 #include <sys/dnlc.h>
63 #include <sys/dmu_objset.h>
64 
65 int zfsfstype;
66 vfsops_t *zfs_vfsops = NULL;
67 static major_t zfs_major;
68 static minor_t zfs_minor;
69 static kmutex_t	zfs_dev_mtx;
70 
71 static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
72 static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr);
73 static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
74 static int zfs_root(vfs_t *vfsp, vnode_t **vpp);
75 static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp);
76 static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp);
77 static void zfs_freevfs(vfs_t *vfsp);
78 
79 static const fs_operation_def_t zfs_vfsops_template[] = {
80 	VFSNAME_MOUNT,		{ .vfs_mount = zfs_mount },
81 	VFSNAME_MOUNTROOT,	{ .vfs_mountroot = zfs_mountroot },
82 	VFSNAME_UNMOUNT,	{ .vfs_unmount = zfs_umount },
83 	VFSNAME_ROOT,		{ .vfs_root = zfs_root },
84 	VFSNAME_STATVFS,	{ .vfs_statvfs = zfs_statvfs },
85 	VFSNAME_SYNC,		{ .vfs_sync = zfs_sync },
86 	VFSNAME_VGET,		{ .vfs_vget = zfs_vget },
87 	VFSNAME_FREEVFS,	{ .vfs_freevfs = zfs_freevfs },
88 	NULL,			NULL
89 };
90 
91 static const fs_operation_def_t zfs_vfsops_eio_template[] = {
92 	VFSNAME_FREEVFS,	{ .vfs_freevfs =  zfs_freevfs },
93 	NULL,			NULL
94 };
95 
96 /*
97  * We need to keep a count of active fs's.
98  * This is necessary to prevent our module
99  * from being unloaded after a umount -f
100  */
101 static uint32_t	zfs_active_fs_count = 0;
102 
103 static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
104 static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
105 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
106 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
107 
108 /*
109  * MO_DEFAULT is not used since the default value is determined
110  * by the equivalent property.
111  */
112 static mntopt_t mntopts[] = {
113 	{ MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL },
114 	{ MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL },
115 	{ MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL },
116 	{ MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
117 };
118 
119 static mntopts_t zfs_mntopts = {
120 	sizeof (mntopts) / sizeof (mntopt_t),
121 	mntopts
122 };
123 
124 /*ARGSUSED*/
125 int
126 zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
127 {
128 	/*
129 	 * Data integrity is job one.  We don't want a compromised kernel
130 	 * writing to the storage pool, so we never sync during panic.
131 	 */
132 	if (panicstr)
133 		return (0);
134 
135 	/*
136 	 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
137 	 * to sync metadata, which they would otherwise cache indefinitely.
138 	 * Semantically, the only requirement is that the sync be initiated.
139 	 * The DMU syncs out txgs frequently, so there's nothing to do.
140 	 */
141 	if (flag & SYNC_ATTR)
142 		return (0);
143 
144 	if (vfsp != NULL) {
145 		/*
146 		 * Sync a specific filesystem.
147 		 */
148 		zfsvfs_t *zfsvfs = vfsp->vfs_data;
149 
150 		ZFS_ENTER(zfsvfs);
151 		if (zfsvfs->z_log != NULL)
152 			zil_commit(zfsvfs->z_log, UINT64_MAX, 0);
153 		else
154 			txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
155 		ZFS_EXIT(zfsvfs);
156 	} else {
157 		/*
158 		 * Sync all ZFS filesystems.  This is what happens when you
159 		 * run sync(1M).  Unlike other filesystems, ZFS honors the
160 		 * request by waiting for all pools to commit all dirty data.
161 		 */
162 		spa_sync_allpools();
163 	}
164 
165 	return (0);
166 }
167 
168 static int
169 zfs_create_unique_device(dev_t *dev)
170 {
171 	major_t new_major;
172 
173 	do {
174 		ASSERT3U(zfs_minor, <=, MAXMIN32);
175 		minor_t start = zfs_minor;
176 		do {
177 			mutex_enter(&zfs_dev_mtx);
178 			if (zfs_minor >= MAXMIN32) {
179 				/*
180 				 * If we're still using the real major
181 				 * keep out of /dev/zfs and /dev/zvol minor
182 				 * number space.  If we're using a getudev()'ed
183 				 * major number, we can use all of its minors.
184 				 */
185 				if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
186 					zfs_minor = ZFS_MIN_MINOR;
187 				else
188 					zfs_minor = 0;
189 			} else {
190 				zfs_minor++;
191 			}
192 			*dev = makedevice(zfs_major, zfs_minor);
193 			mutex_exit(&zfs_dev_mtx);
194 		} while (vfs_devismounted(*dev) && zfs_minor != start);
195 		if (zfs_minor == start) {
196 			/*
197 			 * We are using all ~262,000 minor numbers for the
198 			 * current major number.  Create a new major number.
199 			 */
200 			if ((new_major = getudev()) == (major_t)-1) {
201 				cmn_err(CE_WARN,
202 				    "zfs_mount: Can't get unique major "
203 				    "device number.");
204 				return (-1);
205 			}
206 			mutex_enter(&zfs_dev_mtx);
207 			zfs_major = new_major;
208 			zfs_minor = 0;
209 
210 			mutex_exit(&zfs_dev_mtx);
211 		} else {
212 			break;
213 		}
214 		/* CONSTANTCONDITION */
215 	} while (1);
216 
217 	return (0);
218 }
219 
220 static void
221 atime_changed_cb(void *arg, uint64_t newval)
222 {
223 	zfsvfs_t *zfsvfs = arg;
224 
225 	if (newval == TRUE) {
226 		zfsvfs->z_atime = TRUE;
227 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
228 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
229 	} else {
230 		zfsvfs->z_atime = FALSE;
231 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
232 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
233 	}
234 }
235 
236 static void
237 xattr_changed_cb(void *arg, uint64_t newval)
238 {
239 	zfsvfs_t *zfsvfs = arg;
240 
241 	if (newval == TRUE) {
242 		/* XXX locking on vfs_flag? */
243 		zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
244 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
245 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
246 	} else {
247 		/* XXX locking on vfs_flag? */
248 		zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
249 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
250 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
251 	}
252 }
253 
254 static void
255 blksz_changed_cb(void *arg, uint64_t newval)
256 {
257 	zfsvfs_t *zfsvfs = arg;
258 
259 	if (newval < SPA_MINBLOCKSIZE ||
260 	    newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
261 		newval = SPA_MAXBLOCKSIZE;
262 
263 	zfsvfs->z_max_blksz = newval;
264 	zfsvfs->z_vfs->vfs_bsize = newval;
265 }
266 
267 static void
268 readonly_changed_cb(void *arg, uint64_t newval)
269 {
270 	zfsvfs_t *zfsvfs = arg;
271 
272 	if (newval) {
273 		/* XXX locking on vfs_flag? */
274 		zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
275 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
276 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
277 	} else {
278 		/* XXX locking on vfs_flag? */
279 		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
280 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
281 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
282 	}
283 }
284 
285 static void
286 devices_changed_cb(void *arg, uint64_t newval)
287 {
288 	zfsvfs_t *zfsvfs = arg;
289 
290 	if (newval == FALSE) {
291 		zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
292 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
293 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
294 	} else {
295 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
296 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
297 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
298 	}
299 }
300 
301 static void
302 setuid_changed_cb(void *arg, uint64_t newval)
303 {
304 	zfsvfs_t *zfsvfs = arg;
305 
306 	if (newval == FALSE) {
307 		zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
308 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
309 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
310 	} else {
311 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
312 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
313 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
314 	}
315 }
316 
317 static void
318 exec_changed_cb(void *arg, uint64_t newval)
319 {
320 	zfsvfs_t *zfsvfs = arg;
321 
322 	if (newval == FALSE) {
323 		zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
324 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
325 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
326 	} else {
327 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
328 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
329 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
330 	}
331 }
332 
333 /*
334  * The nbmand mount option can be changed at mount time.
335  * We can't allow it to be toggled on live file systems or incorrect
336  * behavior may be seen from cifs clients
337  *
338  * This property isn't registered via dsl_prop_register(), but this callback
339  * will be called when a file system is first mounted
340  */
341 static void
342 nbmand_changed_cb(void *arg, uint64_t newval)
343 {
344 	zfsvfs_t *zfsvfs = arg;
345 	if (newval == FALSE) {
346 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
347 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
348 	} else {
349 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
350 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
351 	}
352 }
353 
354 static void
355 snapdir_changed_cb(void *arg, uint64_t newval)
356 {
357 	zfsvfs_t *zfsvfs = arg;
358 
359 	zfsvfs->z_show_ctldir = newval;
360 }
361 
362 static void
363 vscan_changed_cb(void *arg, uint64_t newval)
364 {
365 	zfsvfs_t *zfsvfs = arg;
366 
367 	zfsvfs->z_vscan = newval;
368 }
369 
370 static void
371 acl_mode_changed_cb(void *arg, uint64_t newval)
372 {
373 	zfsvfs_t *zfsvfs = arg;
374 
375 	zfsvfs->z_acl_mode = newval;
376 }
377 
378 static void
379 acl_inherit_changed_cb(void *arg, uint64_t newval)
380 {
381 	zfsvfs_t *zfsvfs = arg;
382 
383 	zfsvfs->z_acl_inherit = newval;
384 }
385 
386 static int
387 zfs_register_callbacks(vfs_t *vfsp)
388 {
389 	struct dsl_dataset *ds = NULL;
390 	objset_t *os = NULL;
391 	zfsvfs_t *zfsvfs = NULL;
392 	uint64_t nbmand;
393 	int readonly, do_readonly = B_FALSE;
394 	int setuid, do_setuid = B_FALSE;
395 	int exec, do_exec = B_FALSE;
396 	int devices, do_devices = B_FALSE;
397 	int xattr, do_xattr = B_FALSE;
398 	int atime, do_atime = B_FALSE;
399 	int error = 0;
400 
401 	ASSERT(vfsp);
402 	zfsvfs = vfsp->vfs_data;
403 	ASSERT(zfsvfs);
404 	os = zfsvfs->z_os;
405 
406 	/*
407 	 * The act of registering our callbacks will destroy any mount
408 	 * options we may have.  In order to enable temporary overrides
409 	 * of mount options, we stash away the current values and
410 	 * restore them after we register the callbacks.
411 	 */
412 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
413 		readonly = B_TRUE;
414 		do_readonly = B_TRUE;
415 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
416 		readonly = B_FALSE;
417 		do_readonly = B_TRUE;
418 	}
419 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
420 		devices = B_FALSE;
421 		setuid = B_FALSE;
422 		do_devices = B_TRUE;
423 		do_setuid = B_TRUE;
424 	} else {
425 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
426 			devices = B_FALSE;
427 			do_devices = B_TRUE;
428 		} else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
429 			devices = B_TRUE;
430 			do_devices = B_TRUE;
431 		}
432 
433 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
434 			setuid = B_FALSE;
435 			do_setuid = B_TRUE;
436 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
437 			setuid = B_TRUE;
438 			do_setuid = B_TRUE;
439 		}
440 	}
441 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
442 		exec = B_FALSE;
443 		do_exec = B_TRUE;
444 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
445 		exec = B_TRUE;
446 		do_exec = B_TRUE;
447 	}
448 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
449 		xattr = B_FALSE;
450 		do_xattr = B_TRUE;
451 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
452 		xattr = B_TRUE;
453 		do_xattr = B_TRUE;
454 	}
455 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
456 		atime = B_FALSE;
457 		do_atime = B_TRUE;
458 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
459 		atime = B_TRUE;
460 		do_atime = B_TRUE;
461 	}
462 
463 	/*
464 	 * nbmand is a special property.  It can only be changed at
465 	 * mount time.
466 	 *
467 	 * This is weird, but it is documented to only be changeable
468 	 * at mount time.
469 	 */
470 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
471 		nbmand = B_FALSE;
472 	} else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
473 		nbmand = B_TRUE;
474 	} else {
475 		char osname[MAXNAMELEN];
476 
477 		dmu_objset_name(os, osname);
478 		if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
479 		    NULL))
480 		return (error);
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 	uint_t readonly;
561 	int error;
562 
563 	error = zfs_register_callbacks(zfsvfs->z_vfs);
564 	if (error)
565 		return (error);
566 
567 	/*
568 	 * Set the objset user_ptr to track its zfsvfs.
569 	 */
570 	mutex_enter(&zfsvfs->z_os->os->os_user_ptr_lock);
571 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
572 	mutex_exit(&zfsvfs->z_os->os->os_user_ptr_lock);
573 
574 	/*
575 	 * If we are not mounting (ie: online recv), then we don't
576 	 * have to worry about replaying the log as we blocked all
577 	 * operations out since we closed the ZIL.
578 	 */
579 	if (mounting) {
580 		/*
581 		 * During replay we remove the read only flag to
582 		 * allow replays to succeed.
583 		 */
584 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
585 		if (readonly != 0)
586 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
587 		else
588 			zfs_unlinked_drain(zfsvfs);
589 
590 		/*
591 		 * Parse and replay the intent log.
592 		 *
593 		 * Because of ziltest, this must be done after
594 		 * zfs_unlinked_drain().  (Further note: ziltest doesn't
595 		 * use readonly mounts, where zfs_unlinked_drain() isn't
596 		 * called.)  This is because ziltest causes spa_sync()
597 		 * to think it's committed, but actually it is not, so
598 		 * the intent log contains many txg's worth of changes.
599 		 *
600 		 * In particular, if object N is in the unlinked set in
601 		 * the last txg to actually sync, then it could be
602 		 * actually freed in a later txg and then reallocated in
603 		 * a yet later txg.  This would write a "create object
604 		 * N" record to the intent log.  Normally, this would be
605 		 * fine because the spa_sync() would have written out
606 		 * the fact that object N is free, before we could write
607 		 * the "create object N" intent log record.
608 		 *
609 		 * But when we are in ziltest mode, we advance the "open
610 		 * txg" without actually spa_sync()-ing the changes to
611 		 * disk.  So we would see that object N is still
612 		 * allocated and in the unlinked set, and there is an
613 		 * intent log record saying to allocate it.
614 		 */
615 		zil_replay(zfsvfs->z_os, zfsvfs, &zfsvfs->z_assign,
616 		    zfs_replay_vector);
617 
618 		zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
619 	}
620 
621 	if (!zil_disable)
622 		zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
623 
624 	return (0);
625 }
626 
627 static int
628 zfs_domount(vfs_t *vfsp, char *osname, cred_t *cr)
629 {
630 	dev_t mount_dev;
631 	uint64_t recordsize, readonly;
632 	int error = 0;
633 	int mode;
634 	zfsvfs_t *zfsvfs;
635 	znode_t *zp = NULL;
636 
637 	ASSERT(vfsp);
638 	ASSERT(osname);
639 
640 	/*
641 	 * Initialize the zfs-specific filesystem structure.
642 	 * Should probably make this a kmem cache, shuffle fields,
643 	 * and just bzero up to z_hold_mtx[].
644 	 */
645 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
646 	zfsvfs->z_vfs = vfsp;
647 	zfsvfs->z_parent = zfsvfs;
648 	zfsvfs->z_assign = TXG_NOWAIT;
649 	zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
650 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
651 
652 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
653 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
654 	    offsetof(znode_t, z_link_node));
655 	rrw_init(&zfsvfs->z_teardown_lock);
656 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
657 	rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
658 
659 	/* Initialize the generic filesystem structure. */
660 	vfsp->vfs_bcount = 0;
661 	vfsp->vfs_data = NULL;
662 
663 	if (zfs_create_unique_device(&mount_dev) == -1) {
664 		error = ENODEV;
665 		goto out;
666 	}
667 	ASSERT(vfs_devismounted(mount_dev) == 0);
668 
669 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
670 	    NULL))
671 		goto out;
672 
673 	vfsp->vfs_dev = mount_dev;
674 	vfsp->vfs_fstype = zfsfstype;
675 	vfsp->vfs_bsize = recordsize;
676 	vfsp->vfs_flag |= VFS_NOTRUNC;
677 	vfsp->vfs_data = zfsvfs;
678 
679 	if (error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL))
680 		goto out;
681 
682 	if (readonly)
683 		mode = DS_MODE_PRIMARY | DS_MODE_READONLY;
684 	else
685 		mode = DS_MODE_PRIMARY;
686 
687 	error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
688 	if (error == EROFS) {
689 		mode = DS_MODE_PRIMARY | DS_MODE_READONLY;
690 		error = dmu_objset_open(osname, DMU_OST_ZFS, mode,
691 		    &zfsvfs->z_os);
692 	}
693 
694 	if (error)
695 		goto out;
696 
697 	if (error = zfs_init_fs(zfsvfs, &zp, cr))
698 		goto out;
699 
700 	/* The call to zfs_init_fs leaves the vnode held, release it here. */
701 	VN_RELE(ZTOV(zp));
702 
703 	/*
704 	 * Set features for file system.
705 	 */
706 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
707 	if (zfsvfs->z_use_fuids) {
708 		vfs_set_feature(vfsp, VFSFT_XVATTR);
709 		vfs_set_feature(vfsp, VFSFT_ACEMASKONACCESS);
710 		vfs_set_feature(vfsp, VFSFT_ACLONCREATE);
711 	}
712 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
713 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
714 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
715 		vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
716 	} else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
717 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
718 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
719 	}
720 
721 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
722 		uint64_t pval;
723 
724 		ASSERT(mode & DS_MODE_READONLY);
725 		atime_changed_cb(zfsvfs, B_FALSE);
726 		readonly_changed_cb(zfsvfs, B_TRUE);
727 		if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
728 			goto out;
729 		xattr_changed_cb(zfsvfs, pval);
730 		zfsvfs->z_issnap = B_TRUE;
731 	} else {
732 		error = zfsvfs_setup(zfsvfs, B_TRUE);
733 	}
734 
735 	if (!zfsvfs->z_issnap)
736 		zfsctl_create(zfsvfs);
737 out:
738 	if (error) {
739 		if (zfsvfs->z_os)
740 			dmu_objset_close(zfsvfs->z_os);
741 		mutex_destroy(&zfsvfs->z_znodes_lock);
742 		list_destroy(&zfsvfs->z_all_znodes);
743 		rrw_destroy(&zfsvfs->z_teardown_lock);
744 		rw_destroy(&zfsvfs->z_teardown_inactive_lock);
745 		rw_destroy(&zfsvfs->z_fuid_lock);
746 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
747 	} else {
748 		atomic_add_32(&zfs_active_fs_count, 1);
749 	}
750 
751 	return (error);
752 }
753 
754 void
755 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
756 {
757 	objset_t *os = zfsvfs->z_os;
758 	struct dsl_dataset *ds;
759 
760 	/*
761 	 * Unregister properties.
762 	 */
763 	if (!dmu_objset_is_snapshot(os)) {
764 		ds = dmu_objset_ds(os);
765 		VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
766 		    zfsvfs) == 0);
767 
768 		VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
769 		    zfsvfs) == 0);
770 
771 		VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
772 		    zfsvfs) == 0);
773 
774 		VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
775 		    zfsvfs) == 0);
776 
777 		VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
778 		    zfsvfs) == 0);
779 
780 		VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
781 		    zfsvfs) == 0);
782 
783 		VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
784 		    zfsvfs) == 0);
785 
786 		VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
787 		    zfsvfs) == 0);
788 
789 		VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
790 		    zfsvfs) == 0);
791 
792 		VERIFY(dsl_prop_unregister(ds, "aclinherit",
793 		    acl_inherit_changed_cb, zfsvfs) == 0);
794 
795 		VERIFY(dsl_prop_unregister(ds, "vscan",
796 		    vscan_changed_cb, zfsvfs) == 0);
797 	}
798 }
799 
800 /*
801  * Convert a decimal digit string to a uint64_t integer.
802  */
803 static int
804 str_to_uint64(char *str, uint64_t *objnum)
805 {
806 	uint64_t num = 0;
807 
808 	while (*str) {
809 		if (*str < '0' || *str > '9')
810 			return (EINVAL);
811 
812 		num = num*10 + *str++ - '0';
813 	}
814 
815 	*objnum = num;
816 	return (0);
817 }
818 
819 /*
820  * The boot path passed from the boot loader is in the form of
821  * "rootpool-name/root-filesystem-object-number'. Convert this
822  * string to a dataset name: "rootpool-name/root-filesystem-name".
823  */
824 static int
825 parse_bootpath(char *bpath, char *outpath)
826 {
827 	char *slashp;
828 	uint64_t objnum;
829 	int error;
830 
831 	if (*bpath == 0 || *bpath == '/')
832 		return (EINVAL);
833 
834 	slashp = strchr(bpath, '/');
835 
836 	/* if no '/', just return the pool name */
837 	if (slashp == NULL) {
838 		(void) strcpy(outpath, bpath);
839 		return (0);
840 	}
841 
842 	if (error = str_to_uint64(slashp+1, &objnum))
843 		return (error);
844 
845 	*slashp = '\0';
846 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
847 	*slashp = '/';
848 
849 	return (error);
850 }
851 
852 static int
853 zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
854 {
855 	int error = 0;
856 	int ret = 0;
857 	static int zfsrootdone = 0;
858 	zfsvfs_t *zfsvfs = NULL;
859 	znode_t *zp = NULL;
860 	vnode_t *vp = NULL;
861 	char *zfs_bootpath;
862 
863 	ASSERT(vfsp);
864 
865 	/*
866 	 * The filesystem that we mount as root is defined in the
867 	 * "zfs-bootfs" property.
868 	 */
869 	if (why == ROOT_INIT) {
870 		if (zfsrootdone++)
871 			return (EBUSY);
872 
873 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
874 		    DDI_PROP_DONTPASS, "zfs-bootfs", &zfs_bootpath) !=
875 		    DDI_SUCCESS)
876 			return (EIO);
877 
878 		error = parse_bootpath(zfs_bootpath, rootfs.bo_name);
879 		ddi_prop_free(zfs_bootpath);
880 
881 		if (error)
882 			return (error);
883 
884 		if (error = vfs_lock(vfsp))
885 			return (error);
886 
887 		if (error = zfs_domount(vfsp, rootfs.bo_name, CRED()))
888 			goto out;
889 
890 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
891 		ASSERT(zfsvfs);
892 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp))
893 			goto out;
894 
895 		vp = ZTOV(zp);
896 		mutex_enter(&vp->v_lock);
897 		vp->v_flag |= VROOT;
898 		mutex_exit(&vp->v_lock);
899 		rootvp = vp;
900 
901 		/*
902 		 * The zfs_zget call above returns with a hold on vp, we release
903 		 * it here.
904 		 */
905 		VN_RELE(vp);
906 
907 		/*
908 		 * Mount root as readonly initially, it will be remouted
909 		 * read/write by /lib/svc/method/fs-usr.
910 		 */
911 		readonly_changed_cb(vfsp->vfs_data, B_TRUE);
912 		vfs_add((struct vnode *)0, vfsp,
913 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
914 out:
915 		vfs_unlock(vfsp);
916 		ret = (error) ? error : 0;
917 		return (ret);
918 	} else if (why == ROOT_REMOUNT) {
919 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
920 		vfsp->vfs_flag |= VFS_REMOUNT;
921 
922 		/* refresh mount options */
923 		zfs_unregister_callbacks(vfsp->vfs_data);
924 		return (zfs_register_callbacks(vfsp));
925 
926 	} else if (why == ROOT_UNMOUNT) {
927 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
928 		(void) zfs_sync(vfsp, 0, 0);
929 		return (0);
930 	}
931 
932 	/*
933 	 * if "why" is equal to anything else other than ROOT_INIT,
934 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
935 	 */
936 	return (ENOTSUP);
937 }
938 
939 /*ARGSUSED*/
940 static int
941 zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
942 {
943 	char		*osname;
944 	pathname_t	spn;
945 	int		error = 0;
946 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
947 	    UIO_SYSSPACE : UIO_USERSPACE;
948 	int		canwrite;
949 
950 	if (mvp->v_type != VDIR)
951 		return (ENOTDIR);
952 
953 	mutex_enter(&mvp->v_lock);
954 	if ((uap->flags & MS_REMOUNT) == 0 &&
955 	    (uap->flags & MS_OVERLAY) == 0 &&
956 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
957 		mutex_exit(&mvp->v_lock);
958 		return (EBUSY);
959 	}
960 	mutex_exit(&mvp->v_lock);
961 
962 	/*
963 	 * ZFS does not support passing unparsed data in via MS_DATA.
964 	 * Users should use the MS_OPTIONSTR interface; this means
965 	 * that all option parsing is already done and the options struct
966 	 * can be interrogated.
967 	 */
968 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
969 		return (EINVAL);
970 
971 	/*
972 	 * Get the objset name (the "special" mount argument).
973 	 */
974 	if (error = pn_get(uap->spec, fromspace, &spn))
975 		return (error);
976 
977 	osname = spn.pn_path;
978 
979 	/*
980 	 * Check for mount privilege?
981 	 *
982 	 * If we don't have privilege then see if
983 	 * we have local permission to allow it
984 	 */
985 	error = secpolicy_fs_mount(cr, mvp, vfsp);
986 	if (error) {
987 		error = dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr);
988 		if (error == 0) {
989 			vattr_t		vattr;
990 
991 			/*
992 			 * Make sure user is the owner of the mount point
993 			 * or has sufficient privileges.
994 			 */
995 
996 			vattr.va_mask = AT_UID;
997 
998 			if (error = VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
999 				goto out;
1000 			}
1001 
1002 			if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 &&
1003 			    VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) {
1004 				error = EPERM;
1005 				goto out;
1006 			}
1007 
1008 			secpolicy_fs_mount_clearopts(cr, vfsp);
1009 		} else {
1010 			goto out;
1011 		}
1012 	}
1013 
1014 	/*
1015 	 * Refuse to mount a filesystem if we are in a local zone and the
1016 	 * dataset is not visible.
1017 	 */
1018 	if (!INGLOBALZONE(curproc) &&
1019 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1020 		error = EPERM;
1021 		goto out;
1022 	}
1023 
1024 	/*
1025 	 * When doing a remount, we simply refresh our temporary properties
1026 	 * according to those options set in the current VFS options.
1027 	 */
1028 	if (uap->flags & MS_REMOUNT) {
1029 		/* refresh mount options */
1030 		zfs_unregister_callbacks(vfsp->vfs_data);
1031 		error = zfs_register_callbacks(vfsp);
1032 		goto out;
1033 	}
1034 
1035 	error = zfs_domount(vfsp, osname, cr);
1036 
1037 out:
1038 	pn_free(&spn);
1039 	return (error);
1040 }
1041 
1042 static int
1043 zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1044 {
1045 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1046 	dev32_t d32;
1047 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
1048 
1049 	ZFS_ENTER(zfsvfs);
1050 
1051 	dmu_objset_space(zfsvfs->z_os,
1052 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
1053 
1054 	/*
1055 	 * The underlying storage pool actually uses multiple block sizes.
1056 	 * We report the fragsize as the smallest block size we support,
1057 	 * and we report our blocksize as the filesystem's maximum blocksize.
1058 	 */
1059 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1060 	statp->f_bsize = zfsvfs->z_max_blksz;
1061 
1062 	/*
1063 	 * The following report "total" blocks of various kinds in the
1064 	 * file system, but reported in terms of f_frsize - the
1065 	 * "fragment" size.
1066 	 */
1067 
1068 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
1069 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
1070 	statp->f_bavail = statp->f_bfree; /* no root reservation */
1071 
1072 	/*
1073 	 * statvfs() should really be called statufs(), because it assumes
1074 	 * static metadata.  ZFS doesn't preallocate files, so the best
1075 	 * we can do is report the max that could possibly fit in f_files,
1076 	 * and that minus the number actually used in f_ffree.
1077 	 * For f_ffree, report the smaller of the number of object available
1078 	 * and the number of blocks (each object will take at least a block).
1079 	 */
1080 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
1081 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
1082 	statp->f_files = statp->f_ffree + usedobjs;
1083 
1084 	(void) cmpldev(&d32, vfsp->vfs_dev);
1085 	statp->f_fsid = d32;
1086 
1087 	/*
1088 	 * We're a zfs filesystem.
1089 	 */
1090 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1091 
1092 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1093 
1094 	statp->f_namemax = ZFS_MAXNAMELEN;
1095 
1096 	/*
1097 	 * We have all of 32 characters to stuff a string here.
1098 	 * Is there anything useful we could/should provide?
1099 	 */
1100 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
1101 
1102 	ZFS_EXIT(zfsvfs);
1103 	return (0);
1104 }
1105 
1106 static int
1107 zfs_root(vfs_t *vfsp, vnode_t **vpp)
1108 {
1109 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1110 	znode_t *rootzp;
1111 	int error;
1112 
1113 	ZFS_ENTER(zfsvfs);
1114 
1115 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1116 	if (error == 0)
1117 		*vpp = ZTOV(rootzp);
1118 
1119 	ZFS_EXIT(zfsvfs);
1120 	return (error);
1121 }
1122 
1123 /*
1124  * Teardown the zfsvfs::z_os.
1125  *
1126  * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1127  * and 'z_teardown_inactive_lock' held.
1128  */
1129 static int
1130 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1131 {
1132 	objset_t *os = zfsvfs->z_os;
1133 	znode_t	*zp;
1134 
1135 	rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1136 
1137 	if (!unmounting) {
1138 		/*
1139 		 * We purge the parent filesystem's vfsp as the parent
1140 		 * filesystem and all of its snapshots have their vnode's
1141 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
1142 		 * 'z_parent' is self referential for non-snapshots.
1143 		 */
1144 		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1145 	}
1146 
1147 	/*
1148 	 * Close the zil. NB: Can't close the zil while zfs_inactive
1149 	 * threads are blocked as zil_close can call zfs_inactive.
1150 	 */
1151 	if (zfsvfs->z_log) {
1152 		zil_close(zfsvfs->z_log);
1153 		zfsvfs->z_log = NULL;
1154 	}
1155 
1156 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
1157 
1158 	/*
1159 	 * If we are not unmounting (ie: online recv) and someone already
1160 	 * unmounted this file system while we were doing the switcheroo,
1161 	 * or a reopen of z_os failed then just bail out now.
1162 	 */
1163 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1164 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
1165 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1166 		return (EIO);
1167 	}
1168 
1169 	/*
1170 	 * At this point there are no vops active, and any new vops will
1171 	 * fail with EIO since we have z_teardown_lock for writer (only
1172 	 * relavent for forced unmount).
1173 	 *
1174 	 * Release all holds on dbufs.
1175 	 */
1176 	mutex_enter(&zfsvfs->z_znodes_lock);
1177 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
1178 	    zp = list_next(&zfsvfs->z_all_znodes, zp))
1179 		if (zp->z_dbuf) {
1180 			ASSERT(ZTOV(zp)->v_count > 0);
1181 			zfs_znode_dmu_fini(zp);
1182 		}
1183 	mutex_exit(&zfsvfs->z_znodes_lock);
1184 
1185 	/*
1186 	 * If we are unmounting, set the unmounted flag and let new vops
1187 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
1188 	 * other vops will fail with EIO.
1189 	 */
1190 	if (unmounting) {
1191 		zfsvfs->z_unmounted = B_TRUE;
1192 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1193 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
1194 	}
1195 
1196 	/*
1197 	 * z_os will be NULL if there was an error in attempting to reopen
1198 	 * zfsvfs, so just return as the properties had already been
1199 	 * unregistered and cached data had been evicted before.
1200 	 */
1201 	if (zfsvfs->z_os == NULL)
1202 		return (0);
1203 
1204 	/*
1205 	 * Unregister properties.
1206 	 */
1207 	zfs_unregister_callbacks(zfsvfs);
1208 
1209 	/*
1210 	 * Evict cached data
1211 	 */
1212 	if (dmu_objset_evict_dbufs(os)) {
1213 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1214 		(void) dmu_objset_evict_dbufs(os);
1215 	}
1216 
1217 	return (0);
1218 }
1219 
1220 /*ARGSUSED*/
1221 static int
1222 zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1223 {
1224 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1225 	objset_t *os;
1226 	int ret;
1227 
1228 	ret = secpolicy_fs_unmount(cr, vfsp);
1229 	if (ret) {
1230 		ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
1231 		    ZFS_DELEG_PERM_MOUNT, cr);
1232 		if (ret)
1233 			return (ret);
1234 	}
1235 
1236 	/*
1237 	 * We purge the parent filesystem's vfsp as the parent filesystem
1238 	 * and all of its snapshots have their vnode's v_vfsp set to the
1239 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
1240 	 * referential for non-snapshots.
1241 	 */
1242 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1243 
1244 	/*
1245 	 * Unmount any snapshots mounted under .zfs before unmounting the
1246 	 * dataset itself.
1247 	 */
1248 	if (zfsvfs->z_ctldir != NULL &&
1249 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1250 		return (ret);
1251 	}
1252 
1253 	if (!(fflag & MS_FORCE)) {
1254 		/*
1255 		 * Check the number of active vnodes in the file system.
1256 		 * Our count is maintained in the vfs structure, but the
1257 		 * number is off by 1 to indicate a hold on the vfs
1258 		 * structure itself.
1259 		 *
1260 		 * The '.zfs' directory maintains a reference of its
1261 		 * own, and any active references underneath are
1262 		 * reflected in the vnode count.
1263 		 */
1264 		if (zfsvfs->z_ctldir == NULL) {
1265 			if (vfsp->vfs_count > 1)
1266 				return (EBUSY);
1267 		} else {
1268 			if (vfsp->vfs_count > 2 ||
1269 			    zfsvfs->z_ctldir->v_count > 1)
1270 				return (EBUSY);
1271 		}
1272 	}
1273 
1274 	vfsp->vfs_flag |= VFS_UNMOUNTED;
1275 
1276 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1277 	os = zfsvfs->z_os;
1278 
1279 	/*
1280 	 * z_os will be NULL if there was an error in
1281 	 * attempting to reopen zfsvfs.
1282 	 */
1283 	if (os != NULL) {
1284 		/*
1285 		 * Unset the objset user_ptr.
1286 		 */
1287 		mutex_enter(&os->os->os_user_ptr_lock);
1288 		dmu_objset_set_user(os, NULL);
1289 		mutex_exit(&os->os->os_user_ptr_lock);
1290 
1291 		/*
1292 		 * Finally close the objset
1293 		 */
1294 		dmu_objset_close(os);
1295 	}
1296 
1297 	/*
1298 	 * We can now safely destroy the '.zfs' directory node.
1299 	 */
1300 	if (zfsvfs->z_ctldir != NULL)
1301 		zfsctl_destroy(zfsvfs);
1302 
1303 	return (0);
1304 }
1305 
1306 static int
1307 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1308 {
1309 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1310 	znode_t		*zp;
1311 	uint64_t	object = 0;
1312 	uint64_t	fid_gen = 0;
1313 	uint64_t	gen_mask;
1314 	uint64_t	zp_gen;
1315 	int 		i, err;
1316 
1317 	*vpp = NULL;
1318 
1319 	ZFS_ENTER(zfsvfs);
1320 
1321 	if (fidp->fid_len == LONG_FID_LEN) {
1322 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1323 		uint64_t	objsetid = 0;
1324 		uint64_t	setgen = 0;
1325 
1326 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1327 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1328 
1329 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1330 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1331 
1332 		ZFS_EXIT(zfsvfs);
1333 
1334 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1335 		if (err)
1336 			return (EINVAL);
1337 		ZFS_ENTER(zfsvfs);
1338 	}
1339 
1340 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1341 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1342 
1343 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1344 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1345 
1346 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1347 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1348 	} else {
1349 		ZFS_EXIT(zfsvfs);
1350 		return (EINVAL);
1351 	}
1352 
1353 	/* A zero fid_gen means we are in the .zfs control directories */
1354 	if (fid_gen == 0 &&
1355 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1356 		*vpp = zfsvfs->z_ctldir;
1357 		ASSERT(*vpp != NULL);
1358 		if (object == ZFSCTL_INO_SNAPDIR) {
1359 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1360 			    0, NULL, NULL, NULL, NULL, NULL) == 0);
1361 		} else {
1362 			VN_HOLD(*vpp);
1363 		}
1364 		ZFS_EXIT(zfsvfs);
1365 		return (0);
1366 	}
1367 
1368 	gen_mask = -1ULL >> (64 - 8 * i);
1369 
1370 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1371 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1372 		ZFS_EXIT(zfsvfs);
1373 		return (err);
1374 	}
1375 	zp_gen = zp->z_phys->zp_gen & gen_mask;
1376 	if (zp_gen == 0)
1377 		zp_gen = 1;
1378 	if (zp->z_unlinked || zp_gen != fid_gen) {
1379 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1380 		VN_RELE(ZTOV(zp));
1381 		ZFS_EXIT(zfsvfs);
1382 		return (EINVAL);
1383 	}
1384 
1385 	*vpp = ZTOV(zp);
1386 	ZFS_EXIT(zfsvfs);
1387 	return (0);
1388 }
1389 
1390 /*
1391  * Block out VOPs and close zfsvfs_t::z_os
1392  *
1393  * Note, if successful, then we return with the 'z_teardown_lock' and
1394  * 'z_teardown_inactive_lock' write held.
1395  */
1396 int
1397 zfs_suspend_fs(zfsvfs_t *zfsvfs, char *name, int *mode)
1398 {
1399 	int error;
1400 
1401 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
1402 		return (error);
1403 
1404 	*mode = zfsvfs->z_os->os_mode;
1405 	dmu_objset_name(zfsvfs->z_os, name);
1406 	dmu_objset_close(zfsvfs->z_os);
1407 
1408 	return (0);
1409 }
1410 
1411 /*
1412  * Reopen zfsvfs_t::z_os and release VOPs.
1413  */
1414 int
1415 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname, int mode)
1416 {
1417 	int err;
1418 
1419 	ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
1420 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
1421 
1422 	err = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
1423 	if (err) {
1424 		zfsvfs->z_os = NULL;
1425 	} else {
1426 		znode_t *zp;
1427 
1428 		VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
1429 
1430 		/*
1431 		 * Attempt to re-establish all the active znodes with
1432 		 * their dbufs.  If a zfs_rezget() fails, then we'll let
1433 		 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
1434 		 * when they try to use their znode.
1435 		 */
1436 		mutex_enter(&zfsvfs->z_znodes_lock);
1437 		for (zp = list_head(&zfsvfs->z_all_znodes); zp;
1438 		    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1439 			(void) zfs_rezget(zp);
1440 		}
1441 		mutex_exit(&zfsvfs->z_znodes_lock);
1442 
1443 	}
1444 
1445 	/* release the VOPs */
1446 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
1447 	rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1448 
1449 	if (err) {
1450 		/*
1451 		 * Since we couldn't reopen zfsvfs::z_os, force
1452 		 * unmount this file system.
1453 		 */
1454 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
1455 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
1456 	}
1457 	return (err);
1458 }
1459 
1460 static void
1461 zfs_freevfs(vfs_t *vfsp)
1462 {
1463 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1464 	int i;
1465 
1466 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1467 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1468 
1469 	mutex_destroy(&zfsvfs->z_znodes_lock);
1470 	list_destroy(&zfsvfs->z_all_znodes);
1471 	rrw_destroy(&zfsvfs->z_teardown_lock);
1472 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
1473 	zfs_fuid_destroy(zfsvfs);
1474 	rw_destroy(&zfsvfs->z_fuid_lock);
1475 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1476 
1477 	atomic_add_32(&zfs_active_fs_count, -1);
1478 }
1479 
1480 /*
1481  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
1482  * so we can't safely do any non-idempotent initialization here.
1483  * Leave that to zfs_init() and zfs_fini(), which are called
1484  * from the module's _init() and _fini() entry points.
1485  */
1486 /*ARGSUSED*/
1487 static int
1488 zfs_vfsinit(int fstype, char *name)
1489 {
1490 	int error;
1491 
1492 	zfsfstype = fstype;
1493 
1494 	/*
1495 	 * Setup vfsops and vnodeops tables.
1496 	 */
1497 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
1498 	if (error != 0) {
1499 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
1500 	}
1501 
1502 	error = zfs_create_op_tables();
1503 	if (error) {
1504 		zfs_remove_op_tables();
1505 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
1506 		(void) vfs_freevfsops_by_type(zfsfstype);
1507 		return (error);
1508 	}
1509 
1510 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
1511 
1512 	/*
1513 	 * Unique major number for all zfs mounts.
1514 	 * If we run out of 32-bit minors, we'll getudev() another major.
1515 	 */
1516 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
1517 	zfs_minor = ZFS_MIN_MINOR;
1518 
1519 	return (0);
1520 }
1521 
1522 void
1523 zfs_init(void)
1524 {
1525 	/*
1526 	 * Initialize .zfs directory structures
1527 	 */
1528 	zfsctl_init();
1529 
1530 	/*
1531 	 * Initialize znode cache, vnode ops, etc...
1532 	 */
1533 	zfs_znode_init();
1534 }
1535 
1536 void
1537 zfs_fini(void)
1538 {
1539 	zfsctl_fini();
1540 	zfs_znode_fini();
1541 }
1542 
1543 int
1544 zfs_busy(void)
1545 {
1546 	return (zfs_active_fs_count != 0);
1547 }
1548 
1549 int
1550 zfs_set_version(const char *name, uint64_t newvers)
1551 {
1552 	int error;
1553 	objset_t *os;
1554 	dmu_tx_t *tx;
1555 	uint64_t curvers;
1556 
1557 	/*
1558 	 * XXX for now, require that the filesystem be unmounted.  Would
1559 	 * be nice to find the zfsvfs_t and just update that if
1560 	 * possible.
1561 	 */
1562 
1563 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
1564 		return (EINVAL);
1565 
1566 	error = dmu_objset_open(name, DMU_OST_ZFS, DS_MODE_PRIMARY, &os);
1567 	if (error)
1568 		return (error);
1569 
1570 	error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1571 	    8, 1, &curvers);
1572 	if (error)
1573 		goto out;
1574 	if (newvers < curvers) {
1575 		error = EINVAL;
1576 		goto out;
1577 	}
1578 
1579 	tx = dmu_tx_create(os);
1580 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 0, ZPL_VERSION_STR);
1581 	error = dmu_tx_assign(tx, TXG_WAIT);
1582 	if (error) {
1583 		dmu_tx_abort(tx);
1584 		goto out;
1585 	}
1586 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1,
1587 	    &newvers, tx);
1588 
1589 	spa_history_internal_log(LOG_DS_UPGRADE,
1590 	    dmu_objset_spa(os), tx, CRED(),
1591 	    "oldver=%llu newver=%llu dataset = %llu", curvers, newvers,
1592 	    dmu_objset_id(os));
1593 	dmu_tx_commit(tx);
1594 
1595 out:
1596 	dmu_objset_close(os);
1597 	return (error);
1598 }
1599 
1600 /*
1601  * Read a property stored within the master node.
1602  */
1603 int
1604 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
1605 {
1606 	const char *pname;
1607 	int error;
1608 
1609 	/*
1610 	 * Look up the file system's value for the property.  For the
1611 	 * version property, we look up a slightly different string.
1612 	 * Also, there is no default VERSION value, so if we don't
1613 	 * find it, return the error.
1614 	 */
1615 	if (prop == ZFS_PROP_VERSION)
1616 		pname = ZPL_VERSION_STR;
1617 	else
1618 		pname = zfs_prop_to_name(prop);
1619 
1620 	error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
1621 
1622 	if (!error) {
1623 		return (0);
1624 	} else if (prop == ZFS_PROP_VERSION || error != ENOENT) {
1625 		return (error);
1626 	} else {
1627 		/* No value set, use the default value */
1628 		switch (prop) {
1629 		case ZFS_PROP_NORMALIZE:
1630 		case ZFS_PROP_UTF8ONLY:
1631 			*value = 0;
1632 			break;
1633 		case ZFS_PROP_CASE:
1634 			*value = ZFS_CASE_SENSITIVE;
1635 			break;
1636 		default:
1637 			return (ENOENT);
1638 		}
1639 	}
1640 	return (0);
1641 }
1642 
1643 static vfsdef_t vfw = {
1644 	VFSDEF_VERSION,
1645 	MNTTYPE_ZFS,
1646 	zfs_vfsinit,
1647 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS|
1648 	    VSW_XID,
1649 	&zfs_mntopts
1650 };
1651 
1652 struct modlfs zfs_modlfs = {
1653 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
1654 };
1655