xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 9246:67c03c93c071)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51484Sek110237  * Common Development and Distribution License (the "License").
61484Sek110237  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
229030SMark.Shellenbaum@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #include <sys/types.h>
27789Sahrens #include <sys/param.h>
28789Sahrens #include <sys/systm.h>
29789Sahrens #include <sys/sysmacros.h>
30789Sahrens #include <sys/kmem.h>
31789Sahrens #include <sys/pathname.h>
32789Sahrens #include <sys/vnode.h>
33789Sahrens #include <sys/vfs.h>
343898Srsb #include <sys/vfs_opreg.h>
35789Sahrens #include <sys/mntent.h>
36789Sahrens #include <sys/mount.h>
37789Sahrens #include <sys/cmn_err.h>
38789Sahrens #include "fs/fs_subr.h"
39789Sahrens #include <sys/zfs_znode.h>
403461Sahrens #include <sys/zfs_dir.h>
41789Sahrens #include <sys/zil.h>
42789Sahrens #include <sys/fs/zfs.h>
43789Sahrens #include <sys/dmu.h>
44789Sahrens #include <sys/dsl_prop.h>
453912Slling #include <sys/dsl_dataset.h>
464543Smarks #include <sys/dsl_deleg.h>
47789Sahrens #include <sys/spa.h>
48789Sahrens #include <sys/zap.h>
49789Sahrens #include <sys/varargs.h>
50789Sahrens #include <sys/policy.h>
51789Sahrens #include <sys/atomic.h>
52789Sahrens #include <sys/mkdev.h>
53789Sahrens #include <sys/modctl.h>
544543Smarks #include <sys/refstr.h>
55789Sahrens #include <sys/zfs_ioctl.h>
56789Sahrens #include <sys/zfs_ctldir.h>
575331Samw #include <sys/zfs_fuid.h>
581544Seschrock #include <sys/bootconf.h>
59849Sbonwick #include <sys/sunddi.h>
601484Sek110237 #include <sys/dnlc.h>
615326Sek110237 #include <sys/dmu_objset.h>
626423Sgw25295 #include <sys/spa_boot.h>
63789Sahrens 
64789Sahrens int zfsfstype;
65789Sahrens vfsops_t *zfs_vfsops = NULL;
66849Sbonwick static major_t zfs_major;
67789Sahrens static minor_t zfs_minor;
68789Sahrens static kmutex_t	zfs_dev_mtx;
69789Sahrens 
709234SGeorge.Wilson@Sun.COM extern int sys_shutdown;
719234SGeorge.Wilson@Sun.COM 
72789Sahrens static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
73789Sahrens static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr);
741544Seschrock static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
75789Sahrens static int zfs_root(vfs_t *vfsp, vnode_t **vpp);
76789Sahrens static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp);
77789Sahrens static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp);
78789Sahrens static void zfs_freevfs(vfs_t *vfsp);
79789Sahrens 
80789Sahrens static const fs_operation_def_t zfs_vfsops_template[] = {
813898Srsb 	VFSNAME_MOUNT,		{ .vfs_mount = zfs_mount },
823898Srsb 	VFSNAME_MOUNTROOT,	{ .vfs_mountroot = zfs_mountroot },
833898Srsb 	VFSNAME_UNMOUNT,	{ .vfs_unmount = zfs_umount },
843898Srsb 	VFSNAME_ROOT,		{ .vfs_root = zfs_root },
853898Srsb 	VFSNAME_STATVFS,	{ .vfs_statvfs = zfs_statvfs },
863898Srsb 	VFSNAME_SYNC,		{ .vfs_sync = zfs_sync },
873898Srsb 	VFSNAME_VGET,		{ .vfs_vget = zfs_vget },
883898Srsb 	VFSNAME_FREEVFS,	{ .vfs_freevfs = zfs_freevfs },
893898Srsb 	NULL,			NULL
90789Sahrens };
91789Sahrens 
92789Sahrens static const fs_operation_def_t zfs_vfsops_eio_template[] = {
933898Srsb 	VFSNAME_FREEVFS,	{ .vfs_freevfs =  zfs_freevfs },
943898Srsb 	NULL,			NULL
95789Sahrens };
96789Sahrens 
97789Sahrens /*
98789Sahrens  * We need to keep a count of active fs's.
99789Sahrens  * This is necessary to prevent our module
100789Sahrens  * from being unloaded after a umount -f
101789Sahrens  */
102789Sahrens static uint32_t	zfs_active_fs_count = 0;
103789Sahrens 
104789Sahrens static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
105789Sahrens static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
1063234Sck153898 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
1073234Sck153898 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
108789Sahrens 
1093234Sck153898 /*
1104596Slling  * MO_DEFAULT is not used since the default value is determined
1114596Slling  * by the equivalent property.
1123234Sck153898  */
113789Sahrens static mntopt_t mntopts[] = {
1143234Sck153898 	{ MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL },
1153234Sck153898 	{ MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL },
1164596Slling 	{ MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL },
117789Sahrens 	{ MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
118789Sahrens };
119789Sahrens 
120789Sahrens static mntopts_t zfs_mntopts = {
121789Sahrens 	sizeof (mntopts) / sizeof (mntopt_t),
122789Sahrens 	mntopts
123789Sahrens };
124789Sahrens 
125789Sahrens /*ARGSUSED*/
126789Sahrens int
127789Sahrens zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
128789Sahrens {
129789Sahrens 	/*
130789Sahrens 	 * Data integrity is job one.  We don't want a compromised kernel
131789Sahrens 	 * writing to the storage pool, so we never sync during panic.
132789Sahrens 	 */
133789Sahrens 	if (panicstr)
134789Sahrens 		return (0);
135789Sahrens 
136789Sahrens 	/*
137789Sahrens 	 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
138789Sahrens 	 * to sync metadata, which they would otherwise cache indefinitely.
139789Sahrens 	 * Semantically, the only requirement is that the sync be initiated.
140789Sahrens 	 * The DMU syncs out txgs frequently, so there's nothing to do.
141789Sahrens 	 */
142789Sahrens 	if (flag & SYNC_ATTR)
143789Sahrens 		return (0);
144789Sahrens 
145789Sahrens 	if (vfsp != NULL) {
146789Sahrens 		/*
147789Sahrens 		 * Sync a specific filesystem.
148789Sahrens 		 */
149789Sahrens 		zfsvfs_t *zfsvfs = vfsp->vfs_data;
1509234SGeorge.Wilson@Sun.COM 		dsl_pool_t *dp;
151789Sahrens 
152789Sahrens 		ZFS_ENTER(zfsvfs);
1539234SGeorge.Wilson@Sun.COM 		dp = dmu_objset_pool(zfsvfs->z_os);
1549234SGeorge.Wilson@Sun.COM 
1559234SGeorge.Wilson@Sun.COM 		/*
1569234SGeorge.Wilson@Sun.COM 		 * If the system is shutting down, then skip any
1579234SGeorge.Wilson@Sun.COM 		 * filesystems which may exist on a suspended pool.
1589234SGeorge.Wilson@Sun.COM 		 */
1599234SGeorge.Wilson@Sun.COM 		if (sys_shutdown && spa_suspended(dp->dp_spa)) {
1609234SGeorge.Wilson@Sun.COM 			ZFS_EXIT(zfsvfs);
1619234SGeorge.Wilson@Sun.COM 			return (0);
1629234SGeorge.Wilson@Sun.COM 		}
1639234SGeorge.Wilson@Sun.COM 
164789Sahrens 		if (zfsvfs->z_log != NULL)
1652638Sperrin 			zil_commit(zfsvfs->z_log, UINT64_MAX, 0);
166789Sahrens 		else
1679234SGeorge.Wilson@Sun.COM 			txg_wait_synced(dp, 0);
168789Sahrens 		ZFS_EXIT(zfsvfs);
169789Sahrens 	} else {
170789Sahrens 		/*
171789Sahrens 		 * Sync all ZFS filesystems.  This is what happens when you
172789Sahrens 		 * run sync(1M).  Unlike other filesystems, ZFS honors the
173789Sahrens 		 * request by waiting for all pools to commit all dirty data.
174789Sahrens 		 */
175789Sahrens 		spa_sync_allpools();
176789Sahrens 	}
177789Sahrens 
178789Sahrens 	return (0);
179789Sahrens }
180789Sahrens 
1811544Seschrock static int
1821544Seschrock zfs_create_unique_device(dev_t *dev)
1831544Seschrock {
1841544Seschrock 	major_t new_major;
1851544Seschrock 
1861544Seschrock 	do {
1871544Seschrock 		ASSERT3U(zfs_minor, <=, MAXMIN32);
1881544Seschrock 		minor_t start = zfs_minor;
1891544Seschrock 		do {
1901544Seschrock 			mutex_enter(&zfs_dev_mtx);
1911544Seschrock 			if (zfs_minor >= MAXMIN32) {
1921544Seschrock 				/*
1931544Seschrock 				 * If we're still using the real major
1941544Seschrock 				 * keep out of /dev/zfs and /dev/zvol minor
1951544Seschrock 				 * number space.  If we're using a getudev()'ed
1961544Seschrock 				 * major number, we can use all of its minors.
1971544Seschrock 				 */
1981544Seschrock 				if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
1991544Seschrock 					zfs_minor = ZFS_MIN_MINOR;
2001544Seschrock 				else
2011544Seschrock 					zfs_minor = 0;
2021544Seschrock 			} else {
2031544Seschrock 				zfs_minor++;
2041544Seschrock 			}
2051544Seschrock 			*dev = makedevice(zfs_major, zfs_minor);
2061544Seschrock 			mutex_exit(&zfs_dev_mtx);
2071544Seschrock 		} while (vfs_devismounted(*dev) && zfs_minor != start);
2081544Seschrock 		if (zfs_minor == start) {
2091544Seschrock 			/*
2101544Seschrock 			 * We are using all ~262,000 minor numbers for the
2111544Seschrock 			 * current major number.  Create a new major number.
2121544Seschrock 			 */
2131544Seschrock 			if ((new_major = getudev()) == (major_t)-1) {
2141544Seschrock 				cmn_err(CE_WARN,
2151544Seschrock 				    "zfs_mount: Can't get unique major "
2161544Seschrock 				    "device number.");
2171544Seschrock 				return (-1);
2181544Seschrock 			}
2191544Seschrock 			mutex_enter(&zfs_dev_mtx);
2201544Seschrock 			zfs_major = new_major;
2211544Seschrock 			zfs_minor = 0;
2221544Seschrock 
2231544Seschrock 			mutex_exit(&zfs_dev_mtx);
2241544Seschrock 		} else {
2251544Seschrock 			break;
2261544Seschrock 		}
2271544Seschrock 		/* CONSTANTCONDITION */
2281544Seschrock 	} while (1);
2291544Seschrock 
2301544Seschrock 	return (0);
2311544Seschrock }
2321544Seschrock 
233789Sahrens static void
234789Sahrens atime_changed_cb(void *arg, uint64_t newval)
235789Sahrens {
236789Sahrens 	zfsvfs_t *zfsvfs = arg;
237789Sahrens 
238789Sahrens 	if (newval == TRUE) {
239789Sahrens 		zfsvfs->z_atime = TRUE;
240789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
241789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
242789Sahrens 	} else {
243789Sahrens 		zfsvfs->z_atime = FALSE;
244789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
245789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
246789Sahrens 	}
247789Sahrens }
248789Sahrens 
249789Sahrens static void
2503234Sck153898 xattr_changed_cb(void *arg, uint64_t newval)
2513234Sck153898 {
2523234Sck153898 	zfsvfs_t *zfsvfs = arg;
2533234Sck153898 
2543234Sck153898 	if (newval == TRUE) {
2553234Sck153898 		/* XXX locking on vfs_flag? */
2563234Sck153898 		zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
2573234Sck153898 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
2583234Sck153898 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
2593234Sck153898 	} else {
2603234Sck153898 		/* XXX locking on vfs_flag? */
2613234Sck153898 		zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
2623234Sck153898 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
2633234Sck153898 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
2643234Sck153898 	}
2653234Sck153898 }
2663234Sck153898 
2673234Sck153898 static void
268789Sahrens blksz_changed_cb(void *arg, uint64_t newval)
269789Sahrens {
270789Sahrens 	zfsvfs_t *zfsvfs = arg;
271789Sahrens 
272789Sahrens 	if (newval < SPA_MINBLOCKSIZE ||
273789Sahrens 	    newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
274789Sahrens 		newval = SPA_MAXBLOCKSIZE;
275789Sahrens 
276789Sahrens 	zfsvfs->z_max_blksz = newval;
277789Sahrens 	zfsvfs->z_vfs->vfs_bsize = newval;
278789Sahrens }
279789Sahrens 
280789Sahrens static void
281789Sahrens readonly_changed_cb(void *arg, uint64_t newval)
282789Sahrens {
283789Sahrens 	zfsvfs_t *zfsvfs = arg;
284789Sahrens 
285789Sahrens 	if (newval) {
286789Sahrens 		/* XXX locking on vfs_flag? */
287789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
288789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
289789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
290789Sahrens 	} else {
291789Sahrens 		/* XXX locking on vfs_flag? */
292789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
293789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
294789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
295789Sahrens 	}
296789Sahrens }
297789Sahrens 
298789Sahrens static void
299789Sahrens devices_changed_cb(void *arg, uint64_t newval)
300789Sahrens {
301789Sahrens 	zfsvfs_t *zfsvfs = arg;
302789Sahrens 
303789Sahrens 	if (newval == FALSE) {
304789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
305789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
306789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
307789Sahrens 	} else {
308789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
309789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
310789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
311789Sahrens 	}
312789Sahrens }
313789Sahrens 
314789Sahrens static void
315789Sahrens setuid_changed_cb(void *arg, uint64_t newval)
316789Sahrens {
317789Sahrens 	zfsvfs_t *zfsvfs = arg;
318789Sahrens 
319789Sahrens 	if (newval == FALSE) {
320789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
321789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
322789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
323789Sahrens 	} else {
324789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
325789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
326789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
327789Sahrens 	}
328789Sahrens }
329789Sahrens 
330789Sahrens static void
331789Sahrens exec_changed_cb(void *arg, uint64_t newval)
332789Sahrens {
333789Sahrens 	zfsvfs_t *zfsvfs = arg;
334789Sahrens 
335789Sahrens 	if (newval == FALSE) {
336789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
337789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
338789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
339789Sahrens 	} else {
340789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
341789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
342789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
343789Sahrens 	}
344789Sahrens }
345789Sahrens 
3465331Samw /*
3475331Samw  * The nbmand mount option can be changed at mount time.
3485331Samw  * We can't allow it to be toggled on live file systems or incorrect
3495331Samw  * behavior may be seen from cifs clients
3505331Samw  *
3515331Samw  * This property isn't registered via dsl_prop_register(), but this callback
3525331Samw  * will be called when a file system is first mounted
3535331Samw  */
3545331Samw static void
3555331Samw nbmand_changed_cb(void *arg, uint64_t newval)
3565331Samw {
3575331Samw 	zfsvfs_t *zfsvfs = arg;
3585331Samw 	if (newval == FALSE) {
3595331Samw 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
3605331Samw 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
3615331Samw 	} else {
3625331Samw 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
3635331Samw 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
3645331Samw 	}
3655331Samw }
3665331Samw 
367789Sahrens static void
368789Sahrens snapdir_changed_cb(void *arg, uint64_t newval)
369789Sahrens {
370789Sahrens 	zfsvfs_t *zfsvfs = arg;
371789Sahrens 
372789Sahrens 	zfsvfs->z_show_ctldir = newval;
373789Sahrens }
374789Sahrens 
375789Sahrens static void
3765331Samw vscan_changed_cb(void *arg, uint64_t newval)
3775331Samw {
3785331Samw 	zfsvfs_t *zfsvfs = arg;
3795331Samw 
3805331Samw 	zfsvfs->z_vscan = newval;
3815331Samw }
3825331Samw 
3835331Samw static void
384789Sahrens acl_mode_changed_cb(void *arg, uint64_t newval)
385789Sahrens {
386789Sahrens 	zfsvfs_t *zfsvfs = arg;
387789Sahrens 
388789Sahrens 	zfsvfs->z_acl_mode = newval;
389789Sahrens }
390789Sahrens 
391789Sahrens static void
392789Sahrens acl_inherit_changed_cb(void *arg, uint64_t newval)
393789Sahrens {
394789Sahrens 	zfsvfs_t *zfsvfs = arg;
395789Sahrens 
396789Sahrens 	zfsvfs->z_acl_inherit = newval;
397789Sahrens }
398789Sahrens 
3991544Seschrock static int
4001544Seschrock zfs_register_callbacks(vfs_t *vfsp)
4011544Seschrock {
4021544Seschrock 	struct dsl_dataset *ds = NULL;
4031544Seschrock 	objset_t *os = NULL;
4041544Seschrock 	zfsvfs_t *zfsvfs = NULL;
4055331Samw 	uint64_t nbmand;
4065331Samw 	int readonly, do_readonly = B_FALSE;
4075331Samw 	int setuid, do_setuid = B_FALSE;
4085331Samw 	int exec, do_exec = B_FALSE;
4095331Samw 	int devices, do_devices = B_FALSE;
4105331Samw 	int xattr, do_xattr = B_FALSE;
4115331Samw 	int atime, do_atime = B_FALSE;
4121544Seschrock 	int error = 0;
4131544Seschrock 
4141544Seschrock 	ASSERT(vfsp);
4151544Seschrock 	zfsvfs = vfsp->vfs_data;
4161544Seschrock 	ASSERT(zfsvfs);
4171544Seschrock 	os = zfsvfs->z_os;
4181544Seschrock 
4191544Seschrock 	/*
4201544Seschrock 	 * The act of registering our callbacks will destroy any mount
4211544Seschrock 	 * options we may have.  In order to enable temporary overrides
4223234Sck153898 	 * of mount options, we stash away the current values and
4231544Seschrock 	 * restore them after we register the callbacks.
4241544Seschrock 	 */
4251544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
4261544Seschrock 		readonly = B_TRUE;
4271544Seschrock 		do_readonly = B_TRUE;
4281544Seschrock 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
4291544Seschrock 		readonly = B_FALSE;
4301544Seschrock 		do_readonly = B_TRUE;
4311544Seschrock 	}
4321544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
4331544Seschrock 		devices = B_FALSE;
4341544Seschrock 		setuid = B_FALSE;
4351544Seschrock 		do_devices = B_TRUE;
4361544Seschrock 		do_setuid = B_TRUE;
4371544Seschrock 	} else {
4381544Seschrock 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
4391544Seschrock 			devices = B_FALSE;
4401544Seschrock 			do_devices = B_TRUE;
4413912Slling 		} else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
4421544Seschrock 			devices = B_TRUE;
4431544Seschrock 			do_devices = B_TRUE;
4441544Seschrock 		}
4451544Seschrock 
4461544Seschrock 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
4471544Seschrock 			setuid = B_FALSE;
4481544Seschrock 			do_setuid = B_TRUE;
4491544Seschrock 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
4501544Seschrock 			setuid = B_TRUE;
4511544Seschrock 			do_setuid = B_TRUE;
4521544Seschrock 		}
4531544Seschrock 	}
4541544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
4551544Seschrock 		exec = B_FALSE;
4561544Seschrock 		do_exec = B_TRUE;
4571544Seschrock 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
4581544Seschrock 		exec = B_TRUE;
4591544Seschrock 		do_exec = B_TRUE;
4601544Seschrock 	}
4613234Sck153898 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
4623234Sck153898 		xattr = B_FALSE;
4633234Sck153898 		do_xattr = B_TRUE;
4643234Sck153898 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
4653234Sck153898 		xattr = B_TRUE;
4663234Sck153898 		do_xattr = B_TRUE;
4673234Sck153898 	}
4684596Slling 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
4694596Slling 		atime = B_FALSE;
4704596Slling 		do_atime = B_TRUE;
4714596Slling 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
4724596Slling 		atime = B_TRUE;
4734596Slling 		do_atime = B_TRUE;
4744596Slling 	}
4751544Seschrock 
4761544Seschrock 	/*
4775331Samw 	 * nbmand is a special property.  It can only be changed at
4785331Samw 	 * mount time.
4795331Samw 	 *
4805331Samw 	 * This is weird, but it is documented to only be changeable
4815331Samw 	 * at mount time.
4825331Samw 	 */
4835331Samw 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
4845331Samw 		nbmand = B_FALSE;
4855331Samw 	} else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
4865331Samw 		nbmand = B_TRUE;
4875331Samw 	} else {
4885331Samw 		char osname[MAXNAMELEN];
4895331Samw 
4905331Samw 		dmu_objset_name(os, osname);
4915331Samw 		if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
4927265Sahrens 		    NULL)) {
4937265Sahrens 			return (error);
4947265Sahrens 		}
4955331Samw 	}
4965331Samw 
4975331Samw 	/*
4981544Seschrock 	 * Register property callbacks.
4991544Seschrock 	 *
5001544Seschrock 	 * It would probably be fine to just check for i/o error from
5011544Seschrock 	 * the first prop_register(), but I guess I like to go
5021544Seschrock 	 * overboard...
5031544Seschrock 	 */
5041544Seschrock 	ds = dmu_objset_ds(os);
5051544Seschrock 	error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
5061544Seschrock 	error = error ? error : dsl_prop_register(ds,
5073234Sck153898 	    "xattr", xattr_changed_cb, zfsvfs);
5083234Sck153898 	error = error ? error : dsl_prop_register(ds,
5091544Seschrock 	    "recordsize", blksz_changed_cb, zfsvfs);
5101544Seschrock 	error = error ? error : dsl_prop_register(ds,
5111544Seschrock 	    "readonly", readonly_changed_cb, zfsvfs);
5121544Seschrock 	error = error ? error : dsl_prop_register(ds,
5131544Seschrock 	    "devices", devices_changed_cb, zfsvfs);
5141544Seschrock 	error = error ? error : dsl_prop_register(ds,
5151544Seschrock 	    "setuid", setuid_changed_cb, zfsvfs);
5161544Seschrock 	error = error ? error : dsl_prop_register(ds,
5171544Seschrock 	    "exec", exec_changed_cb, zfsvfs);
5181544Seschrock 	error = error ? error : dsl_prop_register(ds,
5191544Seschrock 	    "snapdir", snapdir_changed_cb, zfsvfs);
5201544Seschrock 	error = error ? error : dsl_prop_register(ds,
5211544Seschrock 	    "aclmode", acl_mode_changed_cb, zfsvfs);
5221544Seschrock 	error = error ? error : dsl_prop_register(ds,
5231544Seschrock 	    "aclinherit", acl_inherit_changed_cb, zfsvfs);
5245331Samw 	error = error ? error : dsl_prop_register(ds,
5255331Samw 	    "vscan", vscan_changed_cb, zfsvfs);
5261544Seschrock 	if (error)
5271544Seschrock 		goto unregister;
5281544Seschrock 
5291544Seschrock 	/*
5301544Seschrock 	 * Invoke our callbacks to restore temporary mount options.
5311544Seschrock 	 */
5321544Seschrock 	if (do_readonly)
5331544Seschrock 		readonly_changed_cb(zfsvfs, readonly);
5341544Seschrock 	if (do_setuid)
5351544Seschrock 		setuid_changed_cb(zfsvfs, setuid);
5361544Seschrock 	if (do_exec)
5371544Seschrock 		exec_changed_cb(zfsvfs, exec);
5381544Seschrock 	if (do_devices)
5391544Seschrock 		devices_changed_cb(zfsvfs, devices);
5403234Sck153898 	if (do_xattr)
5413234Sck153898 		xattr_changed_cb(zfsvfs, xattr);
5424596Slling 	if (do_atime)
5434596Slling 		atime_changed_cb(zfsvfs, atime);
5441544Seschrock 
5455331Samw 	nbmand_changed_cb(zfsvfs, nbmand);
5465331Samw 
5471544Seschrock 	return (0);
5481544Seschrock 
5491544Seschrock unregister:
5501544Seschrock 	/*
5511544Seschrock 	 * We may attempt to unregister some callbacks that are not
5521544Seschrock 	 * registered, but this is OK; it will simply return ENOMSG,
5531544Seschrock 	 * which we will ignore.
5541544Seschrock 	 */
5551544Seschrock 	(void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
5563234Sck153898 	(void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
5571544Seschrock 	(void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
5581544Seschrock 	(void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
5591544Seschrock 	(void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs);
5601544Seschrock 	(void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
5611544Seschrock 	(void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
5621544Seschrock 	(void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
5631544Seschrock 	(void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
5641544Seschrock 	(void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
5651544Seschrock 	    zfsvfs);
5665331Samw 	(void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
5671544Seschrock 	return (error);
5681544Seschrock 
5691544Seschrock }
5701544Seschrock 
5711544Seschrock static int
5725326Sek110237 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
5735326Sek110237 {
5745326Sek110237 	int error;
5755326Sek110237 
5765326Sek110237 	error = zfs_register_callbacks(zfsvfs->z_vfs);
5775326Sek110237 	if (error)
5785326Sek110237 		return (error);
5795326Sek110237 
5805326Sek110237 	/*
5815326Sek110237 	 * Set the objset user_ptr to track its zfsvfs.
5825326Sek110237 	 */
5835326Sek110237 	mutex_enter(&zfsvfs->z_os->os->os_user_ptr_lock);
5845326Sek110237 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
5855326Sek110237 	mutex_exit(&zfsvfs->z_os->os->os_user_ptr_lock);
5865326Sek110237 
5875326Sek110237 	/*
5885326Sek110237 	 * If we are not mounting (ie: online recv), then we don't
5895326Sek110237 	 * have to worry about replaying the log as we blocked all
5905326Sek110237 	 * operations out since we closed the ZIL.
5915326Sek110237 	 */
5925326Sek110237 	if (mounting) {
5937638SNeil.Perrin@Sun.COM 		boolean_t readonly;
5947638SNeil.Perrin@Sun.COM 
5955326Sek110237 		/*
5965326Sek110237 		 * During replay we remove the read only flag to
5975326Sek110237 		 * allow replays to succeed.
5985326Sek110237 		 */
5995326Sek110237 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
6008227SNeil.Perrin@Sun.COM 		if (readonly != 0)
6018227SNeil.Perrin@Sun.COM 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
6028227SNeil.Perrin@Sun.COM 		else
6038227SNeil.Perrin@Sun.COM 			zfs_unlinked_drain(zfsvfs);
6045326Sek110237 
6058227SNeil.Perrin@Sun.COM 		zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
6068227SNeil.Perrin@Sun.COM 		if (zil_disable) {
6078227SNeil.Perrin@Sun.COM 			zil_destroy(zfsvfs->z_log, 0);
6088227SNeil.Perrin@Sun.COM 			zfsvfs->z_log = NULL;
6098227SNeil.Perrin@Sun.COM 		} else {
6108227SNeil.Perrin@Sun.COM 			/*
6118227SNeil.Perrin@Sun.COM 			 * Parse and replay the intent log.
6128227SNeil.Perrin@Sun.COM 			 *
6138227SNeil.Perrin@Sun.COM 			 * Because of ziltest, this must be done after
6148227SNeil.Perrin@Sun.COM 			 * zfs_unlinked_drain().  (Further note: ziltest
6158227SNeil.Perrin@Sun.COM 			 * doesn't use readonly mounts, where
6168227SNeil.Perrin@Sun.COM 			 * zfs_unlinked_drain() isn't called.)  This is because
6178227SNeil.Perrin@Sun.COM 			 * ziltest causes spa_sync() to think it's committed,
6188227SNeil.Perrin@Sun.COM 			 * but actually it is not, so the intent log contains
6198227SNeil.Perrin@Sun.COM 			 * many txg's worth of changes.
6208227SNeil.Perrin@Sun.COM 			 *
6218227SNeil.Perrin@Sun.COM 			 * In particular, if object N is in the unlinked set in
6228227SNeil.Perrin@Sun.COM 			 * the last txg to actually sync, then it could be
6238227SNeil.Perrin@Sun.COM 			 * actually freed in a later txg and then reallocated
6248227SNeil.Perrin@Sun.COM 			 * in a yet later txg.  This would write a "create
6258227SNeil.Perrin@Sun.COM 			 * object N" record to the intent log.  Normally, this
6268227SNeil.Perrin@Sun.COM 			 * would be fine because the spa_sync() would have
6278227SNeil.Perrin@Sun.COM 			 * written out the fact that object N is free, before
6288227SNeil.Perrin@Sun.COM 			 * we could write the "create object N" intent log
6298227SNeil.Perrin@Sun.COM 			 * record.
6308227SNeil.Perrin@Sun.COM 			 *
6318227SNeil.Perrin@Sun.COM 			 * But when we are in ziltest mode, we advance the "open
6328227SNeil.Perrin@Sun.COM 			 * txg" without actually spa_sync()-ing the changes to
6338227SNeil.Perrin@Sun.COM 			 * disk.  So we would see that object N is still
6348227SNeil.Perrin@Sun.COM 			 * allocated and in the unlinked set, and there is an
6358227SNeil.Perrin@Sun.COM 			 * intent log record saying to allocate it.
6368227SNeil.Perrin@Sun.COM 			 */
6378227SNeil.Perrin@Sun.COM 			zfsvfs->z_replay = B_TRUE;
6388227SNeil.Perrin@Sun.COM 			zil_replay(zfsvfs->z_os, zfsvfs, zfs_replay_vector);
6398227SNeil.Perrin@Sun.COM 			zfsvfs->z_replay = B_FALSE;
6408227SNeil.Perrin@Sun.COM 		}
6415326Sek110237 		zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
6425326Sek110237 	}
6435326Sek110237 
6445326Sek110237 	return (0);
6455326Sek110237 }
6465326Sek110237 
6476083Sek110237 static void
6486083Sek110237 zfs_freezfsvfs(zfsvfs_t *zfsvfs)
6496083Sek110237 {
6506083Sek110237 	mutex_destroy(&zfsvfs->z_znodes_lock);
6516083Sek110237 	mutex_destroy(&zfsvfs->z_online_recv_lock);
6529030SMark.Shellenbaum@Sun.COM 	mutex_destroy(&zfsvfs->z_lock);
6536083Sek110237 	list_destroy(&zfsvfs->z_all_znodes);
6546083Sek110237 	rrw_destroy(&zfsvfs->z_teardown_lock);
6556083Sek110237 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
6566083Sek110237 	rw_destroy(&zfsvfs->z_fuid_lock);
6576083Sek110237 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
6586083Sek110237 }
6596083Sek110237 
6605326Sek110237 static int
6617046Sahrens zfs_domount(vfs_t *vfsp, char *osname)
6621544Seschrock {
6631544Seschrock 	dev_t mount_dev;
6641544Seschrock 	uint64_t recordsize, readonly;
6651544Seschrock 	int error = 0;
6661544Seschrock 	int mode;
6671544Seschrock 	zfsvfs_t *zfsvfs;
6681544Seschrock 	znode_t *zp = NULL;
6691544Seschrock 
6701544Seschrock 	ASSERT(vfsp);
6711544Seschrock 	ASSERT(osname);
6721544Seschrock 
6731544Seschrock 	/*
6741544Seschrock 	 * Initialize the zfs-specific filesystem structure.
6751544Seschrock 	 * Should probably make this a kmem cache, shuffle fields,
6761544Seschrock 	 * and just bzero up to z_hold_mtx[].
6771544Seschrock 	 */
6781544Seschrock 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
6791544Seschrock 	zfsvfs->z_vfs = vfsp;
6801544Seschrock 	zfsvfs->z_parent = zfsvfs;
6811544Seschrock 	zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
6821544Seschrock 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
6839179SMark.Shellenbaum@Sun.COM 	zfsvfs->z_fuid_dirty = B_FALSE;
6841544Seschrock 
6851544Seschrock 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
6866083Sek110237 	mutex_init(&zfsvfs->z_online_recv_lock, NULL, MUTEX_DEFAULT, NULL);
6879030SMark.Shellenbaum@Sun.COM 	mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
6881544Seschrock 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
6891544Seschrock 	    offsetof(znode_t, z_link_node));
6905326Sek110237 	rrw_init(&zfsvfs->z_teardown_lock);
6915326Sek110237 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
6925498Stimh 	rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
6931544Seschrock 
6941544Seschrock 	/* Initialize the generic filesystem structure. */
6951544Seschrock 	vfsp->vfs_bcount = 0;
6961544Seschrock 	vfsp->vfs_data = NULL;
6971544Seschrock 
6981544Seschrock 	if (zfs_create_unique_device(&mount_dev) == -1) {
6991544Seschrock 		error = ENODEV;
7001544Seschrock 		goto out;
7011544Seschrock 	}
7021544Seschrock 	ASSERT(vfs_devismounted(mount_dev) == 0);
7031544Seschrock 
7041544Seschrock 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
7051544Seschrock 	    NULL))
7061544Seschrock 		goto out;
7071544Seschrock 
7081544Seschrock 	vfsp->vfs_dev = mount_dev;
7091544Seschrock 	vfsp->vfs_fstype = zfsfstype;
7101544Seschrock 	vfsp->vfs_bsize = recordsize;
7111544Seschrock 	vfsp->vfs_flag |= VFS_NOTRUNC;
7121544Seschrock 	vfsp->vfs_data = zfsvfs;
7131544Seschrock 
7141544Seschrock 	if (error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL))
7151544Seschrock 		goto out;
7161544Seschrock 
7176689Smaybee 	mode = DS_MODE_OWNER;
7181544Seschrock 	if (readonly)
7196689Smaybee 		mode |= DS_MODE_READONLY;
7201544Seschrock 
7211544Seschrock 	error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
7221544Seschrock 	if (error == EROFS) {
7236689Smaybee 		mode = DS_MODE_OWNER | DS_MODE_READONLY;
7241544Seschrock 		error = dmu_objset_open(osname, DMU_OST_ZFS, mode,
7251544Seschrock 		    &zfsvfs->z_os);
7261544Seschrock 	}
7271544Seschrock 
7281544Seschrock 	if (error)
7291544Seschrock 		goto out;
7301544Seschrock 
7317046Sahrens 	if (error = zfs_init_fs(zfsvfs, &zp))
7321544Seschrock 		goto out;
7331544Seschrock 
7341544Seschrock 	/* The call to zfs_init_fs leaves the vnode held, release it here. */
7351544Seschrock 	VN_RELE(ZTOV(zp));
7361544Seschrock 
7375331Samw 	/*
7385331Samw 	 * Set features for file system.
7395331Samw 	 */
7405331Samw 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
7415331Samw 	if (zfsvfs->z_use_fuids) {
7425331Samw 		vfs_set_feature(vfsp, VFSFT_XVATTR);
7437757SJanice.Chang@Sun.COM 		vfs_set_feature(vfsp, VFSFT_SYSATTR_VIEWS);
7445331Samw 		vfs_set_feature(vfsp, VFSFT_ACEMASKONACCESS);
7455331Samw 		vfs_set_feature(vfsp, VFSFT_ACLONCREATE);
7465331Samw 	}
7475498Stimh 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
7485498Stimh 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
7495498Stimh 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
7505498Stimh 		vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
7515498Stimh 	} else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
7525498Stimh 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
7535498Stimh 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
7545498Stimh 	}
7555331Samw 
7561544Seschrock 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
7575331Samw 		uint64_t pval;
7583234Sck153898 
7591544Seschrock 		ASSERT(mode & DS_MODE_READONLY);
7601544Seschrock 		atime_changed_cb(zfsvfs, B_FALSE);
7611544Seschrock 		readonly_changed_cb(zfsvfs, B_TRUE);
7625331Samw 		if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
7633234Sck153898 			goto out;
7645331Samw 		xattr_changed_cb(zfsvfs, pval);
7651544Seschrock 		zfsvfs->z_issnap = B_TRUE;
7661544Seschrock 	} else {
7675326Sek110237 		error = zfsvfs_setup(zfsvfs, B_TRUE);
7681544Seschrock 	}
7691544Seschrock 
7701544Seschrock 	if (!zfsvfs->z_issnap)
7711544Seschrock 		zfsctl_create(zfsvfs);
7721544Seschrock out:
7731544Seschrock 	if (error) {
7741544Seschrock 		if (zfsvfs->z_os)
7751544Seschrock 			dmu_objset_close(zfsvfs->z_os);
7766083Sek110237 		zfs_freezfsvfs(zfsvfs);
7771544Seschrock 	} else {
7781544Seschrock 		atomic_add_32(&zfs_active_fs_count, 1);
7791544Seschrock 	}
7801544Seschrock 
7811544Seschrock 	return (error);
7821544Seschrock }
7831544Seschrock 
7841544Seschrock void
7851544Seschrock zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
7861544Seschrock {
7871544Seschrock 	objset_t *os = zfsvfs->z_os;
7881544Seschrock 	struct dsl_dataset *ds;
7891544Seschrock 
7901544Seschrock 	/*
7911544Seschrock 	 * Unregister properties.
7921544Seschrock 	 */
7931544Seschrock 	if (!dmu_objset_is_snapshot(os)) {
7941544Seschrock 		ds = dmu_objset_ds(os);
7951544Seschrock 		VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
7961544Seschrock 		    zfsvfs) == 0);
7971544Seschrock 
7983234Sck153898 		VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
7993234Sck153898 		    zfsvfs) == 0);
8003234Sck153898 
8011544Seschrock 		VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
8021544Seschrock 		    zfsvfs) == 0);
8031544Seschrock 
8041544Seschrock 		VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
8051544Seschrock 		    zfsvfs) == 0);
8061544Seschrock 
8071544Seschrock 		VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
8081544Seschrock 		    zfsvfs) == 0);
8091544Seschrock 
8101544Seschrock 		VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
8111544Seschrock 		    zfsvfs) == 0);
8121544Seschrock 
8131544Seschrock 		VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
8141544Seschrock 		    zfsvfs) == 0);
8151544Seschrock 
8161544Seschrock 		VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
8171544Seschrock 		    zfsvfs) == 0);
8181544Seschrock 
8191544Seschrock 		VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
8201544Seschrock 		    zfsvfs) == 0);
8211544Seschrock 
8221544Seschrock 		VERIFY(dsl_prop_unregister(ds, "aclinherit",
8231544Seschrock 		    acl_inherit_changed_cb, zfsvfs) == 0);
8245331Samw 
8255331Samw 		VERIFY(dsl_prop_unregister(ds, "vscan",
8265331Samw 		    vscan_changed_cb, zfsvfs) == 0);
8271544Seschrock 	}
8281544Seschrock }
8291544Seschrock 
8303912Slling /*
8313912Slling  * Convert a decimal digit string to a uint64_t integer.
8323912Slling  */
8333912Slling static int
8343912Slling str_to_uint64(char *str, uint64_t *objnum)
8353912Slling {
8363912Slling 	uint64_t num = 0;
8373912Slling 
8383912Slling 	while (*str) {
8393912Slling 		if (*str < '0' || *str > '9')
8403912Slling 			return (EINVAL);
8413912Slling 
8423912Slling 		num = num*10 + *str++ - '0';
8433912Slling 	}
8443912Slling 
8453912Slling 	*objnum = num;
8463912Slling 	return (0);
8473912Slling }
8483912Slling 
8493912Slling /*
8503912Slling  * The boot path passed from the boot loader is in the form of
8513912Slling  * "rootpool-name/root-filesystem-object-number'. Convert this
8523912Slling  * string to a dataset name: "rootpool-name/root-filesystem-name".
8533912Slling  */
8543912Slling static int
8556423Sgw25295 zfs_parse_bootfs(char *bpath, char *outpath)
8563912Slling {
8573912Slling 	char *slashp;
8583912Slling 	uint64_t objnum;
8593912Slling 	int error;
8603912Slling 
8613912Slling 	if (*bpath == 0 || *bpath == '/')
8623912Slling 		return (EINVAL);
8633912Slling 
8647656SSherry.Moore@Sun.COM 	(void) strcpy(outpath, bpath);
8657656SSherry.Moore@Sun.COM 
8663912Slling 	slashp = strchr(bpath, '/');
8673912Slling 
8683912Slling 	/* if no '/', just return the pool name */
8693912Slling 	if (slashp == NULL) {
8703912Slling 		return (0);
8713912Slling 	}
8723912Slling 
8737656SSherry.Moore@Sun.COM 	/* if not a number, just return the root dataset name */
8747656SSherry.Moore@Sun.COM 	if (str_to_uint64(slashp+1, &objnum)) {
8757656SSherry.Moore@Sun.COM 		return (0);
8767656SSherry.Moore@Sun.COM 	}
8773912Slling 
8783912Slling 	*slashp = '\0';
8793912Slling 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
8803912Slling 	*slashp = '/';
8813912Slling 
8823912Slling 	return (error);
8833912Slling }
8843912Slling 
8851544Seschrock static int
8861544Seschrock zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
8871544Seschrock {
8881544Seschrock 	int error = 0;
8891544Seschrock 	static int zfsrootdone = 0;
8901544Seschrock 	zfsvfs_t *zfsvfs = NULL;
8911544Seschrock 	znode_t *zp = NULL;
8921544Seschrock 	vnode_t *vp = NULL;
8936423Sgw25295 	char *zfs_bootfs;
8947147Staylor 	char *zfs_devid;
8951544Seschrock 
8961544Seschrock 	ASSERT(vfsp);
8971544Seschrock 
8981544Seschrock 	/*
8993912Slling 	 * The filesystem that we mount as root is defined in the
9006423Sgw25295 	 * boot property "zfs-bootfs" with a format of
9016423Sgw25295 	 * "poolname/root-dataset-objnum".
9021544Seschrock 	 */
9031544Seschrock 	if (why == ROOT_INIT) {
9041544Seschrock 		if (zfsrootdone++)
9051544Seschrock 			return (EBUSY);
9066423Sgw25295 		/*
9076423Sgw25295 		 * the process of doing a spa_load will require the
9086423Sgw25295 		 * clock to be set before we could (for example) do
9096423Sgw25295 		 * something better by looking at the timestamp on
9106423Sgw25295 		 * an uberblock, so just set it to -1.
9116423Sgw25295 		 */
9126423Sgw25295 		clkset(-1);
9131544Seschrock 
9147147Staylor 		if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
9157147Staylor 			cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
9167147Staylor 			    "bootfs name");
9176423Sgw25295 			return (EINVAL);
9185648Ssetje 		}
9197147Staylor 		zfs_devid = spa_get_bootprop("diskdevid");
9207147Staylor 		error = spa_import_rootpool(rootfs.bo_name, zfs_devid);
9217147Staylor 		if (zfs_devid)
9227147Staylor 			spa_free_bootprop(zfs_devid);
9237147Staylor 		if (error) {
9247147Staylor 			spa_free_bootprop(zfs_bootfs);
9257147Staylor 			cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
9267147Staylor 			    error);
9277147Staylor 			return (error);
9287147Staylor 		}
9297147Staylor 		if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
9307147Staylor 			spa_free_bootprop(zfs_bootfs);
9317147Staylor 			cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
9326423Sgw25295 			    error);
9336423Sgw25295 			return (error);
9346423Sgw25295 		}
9353912Slling 
9367147Staylor 		spa_free_bootprop(zfs_bootfs);
9371544Seschrock 
9381544Seschrock 		if (error = vfs_lock(vfsp))
9391544Seschrock 			return (error);
9401544Seschrock 
9417046Sahrens 		if (error = zfs_domount(vfsp, rootfs.bo_name)) {
9427147Staylor 			cmn_err(CE_NOTE, "zfs_domount: error %d", error);
9431544Seschrock 			goto out;
9446423Sgw25295 		}
9451544Seschrock 
9461544Seschrock 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
9471544Seschrock 		ASSERT(zfsvfs);
9486423Sgw25295 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
9497147Staylor 			cmn_err(CE_NOTE, "zfs_zget: error %d", error);
9501544Seschrock 			goto out;
9516423Sgw25295 		}
9521544Seschrock 
9531544Seschrock 		vp = ZTOV(zp);
9541544Seschrock 		mutex_enter(&vp->v_lock);
9551544Seschrock 		vp->v_flag |= VROOT;
9561544Seschrock 		mutex_exit(&vp->v_lock);
9571544Seschrock 		rootvp = vp;
9581544Seschrock 
9591544Seschrock 		/*
9606570Smarks 		 * Leave rootvp held.  The root file system is never unmounted.
9611544Seschrock 		 */
9621544Seschrock 
9631544Seschrock 		vfs_add((struct vnode *)0, vfsp,
9641544Seschrock 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
9651544Seschrock out:
9661544Seschrock 		vfs_unlock(vfsp);
9676423Sgw25295 		return (error);
9681544Seschrock 	} else if (why == ROOT_REMOUNT) {
9691544Seschrock 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
9701544Seschrock 		vfsp->vfs_flag |= VFS_REMOUNT;
9714596Slling 
9724596Slling 		/* refresh mount options */
9734596Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
9744596Slling 		return (zfs_register_callbacks(vfsp));
9754596Slling 
9761544Seschrock 	} else if (why == ROOT_UNMOUNT) {
9771544Seschrock 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
9781544Seschrock 		(void) zfs_sync(vfsp, 0, 0);
9791544Seschrock 		return (0);
9801544Seschrock 	}
9811544Seschrock 
9821544Seschrock 	/*
9831544Seschrock 	 * if "why" is equal to anything else other than ROOT_INIT,
9841544Seschrock 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
9851544Seschrock 	 */
9861544Seschrock 	return (ENOTSUP);
9871544Seschrock }
9881544Seschrock 
989789Sahrens /*ARGSUSED*/
990789Sahrens static int
991789Sahrens zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
992789Sahrens {
993789Sahrens 	char		*osname;
994789Sahrens 	pathname_t	spn;
995789Sahrens 	int		error = 0;
996789Sahrens 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
9973912Slling 	    UIO_SYSSPACE : UIO_USERSPACE;
998789Sahrens 	int		canwrite;
999789Sahrens 
1000789Sahrens 	if (mvp->v_type != VDIR)
1001789Sahrens 		return (ENOTDIR);
1002789Sahrens 
1003789Sahrens 	mutex_enter(&mvp->v_lock);
1004789Sahrens 	if ((uap->flags & MS_REMOUNT) == 0 &&
1005789Sahrens 	    (uap->flags & MS_OVERLAY) == 0 &&
1006789Sahrens 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
1007789Sahrens 		mutex_exit(&mvp->v_lock);
1008789Sahrens 		return (EBUSY);
1009789Sahrens 	}
1010789Sahrens 	mutex_exit(&mvp->v_lock);
1011789Sahrens 
1012789Sahrens 	/*
1013789Sahrens 	 * ZFS does not support passing unparsed data in via MS_DATA.
1014789Sahrens 	 * Users should use the MS_OPTIONSTR interface; this means
1015789Sahrens 	 * that all option parsing is already done and the options struct
1016789Sahrens 	 * can be interrogated.
1017789Sahrens 	 */
1018789Sahrens 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
1019789Sahrens 		return (EINVAL);
1020789Sahrens 
1021789Sahrens 	/*
1022789Sahrens 	 * Get the objset name (the "special" mount argument).
1023789Sahrens 	 */
1024789Sahrens 	if (error = pn_get(uap->spec, fromspace, &spn))
1025789Sahrens 		return (error);
1026789Sahrens 
1027789Sahrens 	osname = spn.pn_path;
1028789Sahrens 
10294543Smarks 	/*
10304543Smarks 	 * Check for mount privilege?
10314543Smarks 	 *
10324543Smarks 	 * If we don't have privilege then see if
10334543Smarks 	 * we have local permission to allow it
10344543Smarks 	 */
10354543Smarks 	error = secpolicy_fs_mount(cr, mvp, vfsp);
10364543Smarks 	if (error) {
10374543Smarks 		error = dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr);
10384543Smarks 		if (error == 0) {
10394543Smarks 			vattr_t		vattr;
10404543Smarks 
10414543Smarks 			/*
10424543Smarks 			 * Make sure user is the owner of the mount point
10434543Smarks 			 * or has sufficient privileges.
10444543Smarks 			 */
10454543Smarks 
10464543Smarks 			vattr.va_mask = AT_UID;
10474543Smarks 
10485331Samw 			if (error = VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
10494543Smarks 				goto out;
10504543Smarks 			}
10514543Smarks 
10525489Smarks 			if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 &&
10535489Smarks 			    VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) {
10545489Smarks 				error = EPERM;
10554543Smarks 				goto out;
10564543Smarks 			}
10574543Smarks 
10584543Smarks 			secpolicy_fs_mount_clearopts(cr, vfsp);
10594543Smarks 		} else {
10604543Smarks 			goto out;
10614543Smarks 		}
10624543Smarks 	}
1063789Sahrens 
1064789Sahrens 	/*
1065789Sahrens 	 * Refuse to mount a filesystem if we are in a local zone and the
1066789Sahrens 	 * dataset is not visible.
1067789Sahrens 	 */
1068789Sahrens 	if (!INGLOBALZONE(curproc) &&
1069789Sahrens 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1070789Sahrens 		error = EPERM;
1071789Sahrens 		goto out;
1072789Sahrens 	}
1073789Sahrens 
10744596Slling 	/*
10754596Slling 	 * When doing a remount, we simply refresh our temporary properties
10764596Slling 	 * according to those options set in the current VFS options.
10774596Slling 	 */
10784596Slling 	if (uap->flags & MS_REMOUNT) {
10794596Slling 		/* refresh mount options */
10804596Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
10814596Slling 		error = zfs_register_callbacks(vfsp);
10824596Slling 		goto out;
10834596Slling 	}
10844596Slling 
10857046Sahrens 	error = zfs_domount(vfsp, osname);
1086789Sahrens 
10879214Schris.kirby@sun.com 	/*
10889214Schris.kirby@sun.com 	 * Add an extra VFS_HOLD on our parent vfs so that it can't
10899214Schris.kirby@sun.com 	 * disappear due to a forced unmount.
10909214Schris.kirby@sun.com 	 */
1091*9246Schris.kirby@sun.com 	if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap)
10929214Schris.kirby@sun.com 		VFS_HOLD(mvp->v_vfsp);
10939214Schris.kirby@sun.com 
1094789Sahrens out:
1095789Sahrens 	pn_free(&spn);
1096789Sahrens 	return (error);
1097789Sahrens }
1098789Sahrens 
1099789Sahrens static int
1100789Sahrens zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1101789Sahrens {
1102789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1103789Sahrens 	dev32_t d32;
11042885Sahrens 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
1105789Sahrens 
1106789Sahrens 	ZFS_ENTER(zfsvfs);
1107789Sahrens 
11082885Sahrens 	dmu_objset_space(zfsvfs->z_os,
11092885Sahrens 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
1110789Sahrens 
1111789Sahrens 	/*
1112789Sahrens 	 * The underlying storage pool actually uses multiple block sizes.
1113789Sahrens 	 * We report the fragsize as the smallest block size we support,
1114789Sahrens 	 * and we report our blocksize as the filesystem's maximum blocksize.
1115789Sahrens 	 */
1116789Sahrens 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1117789Sahrens 	statp->f_bsize = zfsvfs->z_max_blksz;
1118789Sahrens 
1119789Sahrens 	/*
1120789Sahrens 	 * The following report "total" blocks of various kinds in the
1121789Sahrens 	 * file system, but reported in terms of f_frsize - the
1122789Sahrens 	 * "fragment" size.
1123789Sahrens 	 */
1124789Sahrens 
11252885Sahrens 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
11262885Sahrens 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
1127789Sahrens 	statp->f_bavail = statp->f_bfree; /* no root reservation */
1128789Sahrens 
1129789Sahrens 	/*
1130789Sahrens 	 * statvfs() should really be called statufs(), because it assumes
1131789Sahrens 	 * static metadata.  ZFS doesn't preallocate files, so the best
1132789Sahrens 	 * we can do is report the max that could possibly fit in f_files,
1133789Sahrens 	 * and that minus the number actually used in f_ffree.
1134789Sahrens 	 * For f_ffree, report the smaller of the number of object available
1135789Sahrens 	 * and the number of blocks (each object will take at least a block).
1136789Sahrens 	 */
11372885Sahrens 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
1138789Sahrens 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
11392885Sahrens 	statp->f_files = statp->f_ffree + usedobjs;
1140789Sahrens 
1141789Sahrens 	(void) cmpldev(&d32, vfsp->vfs_dev);
1142789Sahrens 	statp->f_fsid = d32;
1143789Sahrens 
1144789Sahrens 	/*
1145789Sahrens 	 * We're a zfs filesystem.
1146789Sahrens 	 */
1147789Sahrens 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1148789Sahrens 
11491123Smarks 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1150789Sahrens 
1151789Sahrens 	statp->f_namemax = ZFS_MAXNAMELEN;
1152789Sahrens 
1153789Sahrens 	/*
1154789Sahrens 	 * We have all of 32 characters to stuff a string here.
1155789Sahrens 	 * Is there anything useful we could/should provide?
1156789Sahrens 	 */
1157789Sahrens 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
1158789Sahrens 
1159789Sahrens 	ZFS_EXIT(zfsvfs);
1160789Sahrens 	return (0);
1161789Sahrens }
1162789Sahrens 
1163789Sahrens static int
1164789Sahrens zfs_root(vfs_t *vfsp, vnode_t **vpp)
1165789Sahrens {
1166789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1167789Sahrens 	znode_t *rootzp;
1168789Sahrens 	int error;
1169789Sahrens 
1170789Sahrens 	ZFS_ENTER(zfsvfs);
1171789Sahrens 
1172789Sahrens 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1173789Sahrens 	if (error == 0)
1174789Sahrens 		*vpp = ZTOV(rootzp);
1175789Sahrens 
1176789Sahrens 	ZFS_EXIT(zfsvfs);
1177789Sahrens 	return (error);
1178789Sahrens }
1179789Sahrens 
11805326Sek110237 /*
11815326Sek110237  * Teardown the zfsvfs::z_os.
11825326Sek110237  *
11835326Sek110237  * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
11845326Sek110237  * and 'z_teardown_inactive_lock' held.
11855326Sek110237  */
11865326Sek110237 static int
11875326Sek110237 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
11885326Sek110237 {
11895642Smaybee 	znode_t	*zp;
11905326Sek110237 
11915326Sek110237 	rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
11925326Sek110237 
11935326Sek110237 	if (!unmounting) {
11945326Sek110237 		/*
11955326Sek110237 		 * We purge the parent filesystem's vfsp as the parent
11965326Sek110237 		 * filesystem and all of its snapshots have their vnode's
11975326Sek110237 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
11985326Sek110237 		 * 'z_parent' is self referential for non-snapshots.
11995326Sek110237 		 */
12005326Sek110237 		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
12015326Sek110237 	}
12025326Sek110237 
12035326Sek110237 	/*
12045326Sek110237 	 * Close the zil. NB: Can't close the zil while zfs_inactive
12055326Sek110237 	 * threads are blocked as zil_close can call zfs_inactive.
12065326Sek110237 	 */
12075326Sek110237 	if (zfsvfs->z_log) {
12085326Sek110237 		zil_close(zfsvfs->z_log);
12095326Sek110237 		zfsvfs->z_log = NULL;
12105326Sek110237 	}
12115326Sek110237 
12125326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
12135326Sek110237 
12145326Sek110237 	/*
12155326Sek110237 	 * If we are not unmounting (ie: online recv) and someone already
12165326Sek110237 	 * unmounted this file system while we were doing the switcheroo,
12175326Sek110237 	 * or a reopen of z_os failed then just bail out now.
12185326Sek110237 	 */
12195326Sek110237 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
12205326Sek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
12215326Sek110237 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
12225326Sek110237 		return (EIO);
12235326Sek110237 	}
12245326Sek110237 
12255326Sek110237 	/*
12265326Sek110237 	 * At this point there are no vops active, and any new vops will
12275326Sek110237 	 * fail with EIO since we have z_teardown_lock for writer (only
12285326Sek110237 	 * relavent for forced unmount).
12295326Sek110237 	 *
12305326Sek110237 	 * Release all holds on dbufs.
12315326Sek110237 	 */
12325326Sek110237 	mutex_enter(&zfsvfs->z_znodes_lock);
12335642Smaybee 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
12345642Smaybee 	    zp = list_next(&zfsvfs->z_all_znodes, zp))
12355446Sahrens 		if (zp->z_dbuf) {
12365642Smaybee 			ASSERT(ZTOV(zp)->v_count > 0);
12375642Smaybee 			zfs_znode_dmu_fini(zp);
12385326Sek110237 		}
12395326Sek110237 	mutex_exit(&zfsvfs->z_znodes_lock);
12405326Sek110237 
12415326Sek110237 	/*
12425326Sek110237 	 * If we are unmounting, set the unmounted flag and let new vops
12435326Sek110237 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
12445326Sek110237 	 * other vops will fail with EIO.
12455326Sek110237 	 */
12465326Sek110237 	if (unmounting) {
12475326Sek110237 		zfsvfs->z_unmounted = B_TRUE;
12485326Sek110237 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
12495326Sek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
12505326Sek110237 	}
12515326Sek110237 
12525326Sek110237 	/*
12535326Sek110237 	 * z_os will be NULL if there was an error in attempting to reopen
12545326Sek110237 	 * zfsvfs, so just return as the properties had already been
12555326Sek110237 	 * unregistered and cached data had been evicted before.
12565326Sek110237 	 */
12575326Sek110237 	if (zfsvfs->z_os == NULL)
12585326Sek110237 		return (0);
12595326Sek110237 
12605326Sek110237 	/*
12615326Sek110237 	 * Unregister properties.
12625326Sek110237 	 */
12635326Sek110237 	zfs_unregister_callbacks(zfsvfs);
12645326Sek110237 
12655326Sek110237 	/*
12665326Sek110237 	 * Evict cached data
12675326Sek110237 	 */
12686083Sek110237 	if (dmu_objset_evict_dbufs(zfsvfs->z_os)) {
12695429Smaybee 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
12706083Sek110237 		(void) dmu_objset_evict_dbufs(zfsvfs->z_os);
12715429Smaybee 	}
12725326Sek110237 
12735326Sek110237 	return (0);
12745326Sek110237 }
12755326Sek110237 
1276789Sahrens /*ARGSUSED*/
1277789Sahrens static int
1278789Sahrens zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1279789Sahrens {
1280789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
12815326Sek110237 	objset_t *os;
1282789Sahrens 	int ret;
1283789Sahrens 
12844543Smarks 	ret = secpolicy_fs_unmount(cr, vfsp);
12854543Smarks 	if (ret) {
12864543Smarks 		ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
12874543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
12884543Smarks 		if (ret)
12894543Smarks 			return (ret);
12904543Smarks 	}
12911484Sek110237 
12924736Sek110237 	/*
12934736Sek110237 	 * We purge the parent filesystem's vfsp as the parent filesystem
12944736Sek110237 	 * and all of its snapshots have their vnode's v_vfsp set to the
12954736Sek110237 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
12964736Sek110237 	 * referential for non-snapshots.
12974736Sek110237 	 */
12984736Sek110237 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
12991484Sek110237 
1300789Sahrens 	/*
1301789Sahrens 	 * Unmount any snapshots mounted under .zfs before unmounting the
1302789Sahrens 	 * dataset itself.
1303789Sahrens 	 */
1304789Sahrens 	if (zfsvfs->z_ctldir != NULL &&
13054543Smarks 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1306789Sahrens 		return (ret);
13074543Smarks 	}
1308789Sahrens 
13094787Sahrens 	if (!(fflag & MS_FORCE)) {
13104480Sgw25295 		/*
13114787Sahrens 		 * Check the number of active vnodes in the file system.
13124787Sahrens 		 * Our count is maintained in the vfs structure, but the
13134787Sahrens 		 * number is off by 1 to indicate a hold on the vfs
13144787Sahrens 		 * structure itself.
13154787Sahrens 		 *
13164787Sahrens 		 * The '.zfs' directory maintains a reference of its
13174787Sahrens 		 * own, and any active references underneath are
13184787Sahrens 		 * reflected in the vnode count.
1319789Sahrens 		 */
13204787Sahrens 		if (zfsvfs->z_ctldir == NULL) {
13214787Sahrens 			if (vfsp->vfs_count > 1)
13224787Sahrens 				return (EBUSY);
13234787Sahrens 		} else {
13244787Sahrens 			if (vfsp->vfs_count > 2 ||
13255326Sek110237 			    zfsvfs->z_ctldir->v_count > 1)
13264787Sahrens 				return (EBUSY);
1327789Sahrens 		}
1328789Sahrens 	}
1329789Sahrens 
1330789Sahrens 	vfsp->vfs_flag |= VFS_UNMOUNTED;
13314787Sahrens 
13325326Sek110237 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
13335326Sek110237 	os = zfsvfs->z_os;
13344787Sahrens 
13354787Sahrens 	/*
13365326Sek110237 	 * z_os will be NULL if there was an error in
13375326Sek110237 	 * attempting to reopen zfsvfs.
13384787Sahrens 	 */
13395326Sek110237 	if (os != NULL) {
13405326Sek110237 		/*
13415326Sek110237 		 * Unset the objset user_ptr.
13425326Sek110237 		 */
13435326Sek110237 		mutex_enter(&os->os->os_user_ptr_lock);
13445326Sek110237 		dmu_objset_set_user(os, NULL);
13455326Sek110237 		mutex_exit(&os->os->os_user_ptr_lock);
13464787Sahrens 
13475326Sek110237 		/*
13486689Smaybee 		 * Finally release the objset
13495326Sek110237 		 */
13505326Sek110237 		dmu_objset_close(os);
13514787Sahrens 	}
13524787Sahrens 
13534787Sahrens 	/*
13544787Sahrens 	 * We can now safely destroy the '.zfs' directory node.
13554787Sahrens 	 */
13564787Sahrens 	if (zfsvfs->z_ctldir != NULL)
13574787Sahrens 		zfsctl_destroy(zfsvfs);
1358789Sahrens 
1359789Sahrens 	return (0);
1360789Sahrens }
1361789Sahrens 
1362789Sahrens static int
1363789Sahrens zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1364789Sahrens {
1365789Sahrens 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1366789Sahrens 	znode_t		*zp;
1367789Sahrens 	uint64_t	object = 0;
1368789Sahrens 	uint64_t	fid_gen = 0;
1369789Sahrens 	uint64_t	gen_mask;
1370789Sahrens 	uint64_t	zp_gen;
1371789Sahrens 	int 		i, err;
1372789Sahrens 
1373789Sahrens 	*vpp = NULL;
1374789Sahrens 
1375789Sahrens 	ZFS_ENTER(zfsvfs);
1376789Sahrens 
1377789Sahrens 	if (fidp->fid_len == LONG_FID_LEN) {
1378789Sahrens 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1379789Sahrens 		uint64_t	objsetid = 0;
1380789Sahrens 		uint64_t	setgen = 0;
1381789Sahrens 
1382789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1383789Sahrens 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1384789Sahrens 
1385789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1386789Sahrens 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1387789Sahrens 
1388789Sahrens 		ZFS_EXIT(zfsvfs);
1389789Sahrens 
1390789Sahrens 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1391789Sahrens 		if (err)
1392789Sahrens 			return (EINVAL);
1393789Sahrens 		ZFS_ENTER(zfsvfs);
1394789Sahrens 	}
1395789Sahrens 
1396789Sahrens 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1397789Sahrens 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1398789Sahrens 
1399789Sahrens 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1400789Sahrens 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1401789Sahrens 
1402789Sahrens 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1403789Sahrens 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1404789Sahrens 	} else {
1405789Sahrens 		ZFS_EXIT(zfsvfs);
1406789Sahrens 		return (EINVAL);
1407789Sahrens 	}
1408789Sahrens 
1409789Sahrens 	/* A zero fid_gen means we are in the .zfs control directories */
1410789Sahrens 	if (fid_gen == 0 &&
1411789Sahrens 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1412789Sahrens 		*vpp = zfsvfs->z_ctldir;
1413789Sahrens 		ASSERT(*vpp != NULL);
1414789Sahrens 		if (object == ZFSCTL_INO_SNAPDIR) {
1415789Sahrens 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
14165331Samw 			    0, NULL, NULL, NULL, NULL, NULL) == 0);
1417789Sahrens 		} else {
1418789Sahrens 			VN_HOLD(*vpp);
1419789Sahrens 		}
1420789Sahrens 		ZFS_EXIT(zfsvfs);
1421789Sahrens 		return (0);
1422789Sahrens 	}
1423789Sahrens 
1424789Sahrens 	gen_mask = -1ULL >> (64 - 8 * i);
1425789Sahrens 
1426789Sahrens 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1427789Sahrens 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1428789Sahrens 		ZFS_EXIT(zfsvfs);
1429789Sahrens 		return (err);
1430789Sahrens 	}
1431789Sahrens 	zp_gen = zp->z_phys->zp_gen & gen_mask;
1432789Sahrens 	if (zp_gen == 0)
1433789Sahrens 		zp_gen = 1;
14343461Sahrens 	if (zp->z_unlinked || zp_gen != fid_gen) {
1435789Sahrens 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1436789Sahrens 		VN_RELE(ZTOV(zp));
1437789Sahrens 		ZFS_EXIT(zfsvfs);
1438789Sahrens 		return (EINVAL);
1439789Sahrens 	}
1440789Sahrens 
1441789Sahrens 	*vpp = ZTOV(zp);
1442789Sahrens 	ZFS_EXIT(zfsvfs);
1443789Sahrens 	return (0);
1444789Sahrens }
1445789Sahrens 
14465326Sek110237 /*
14475326Sek110237  * Block out VOPs and close zfsvfs_t::z_os
14485326Sek110237  *
14495326Sek110237  * Note, if successful, then we return with the 'z_teardown_lock' and
14505326Sek110237  * 'z_teardown_inactive_lock' write held.
14515326Sek110237  */
14525326Sek110237 int
14535326Sek110237 zfs_suspend_fs(zfsvfs_t *zfsvfs, char *name, int *mode)
14545326Sek110237 {
14555326Sek110237 	int error;
14565326Sek110237 
14575326Sek110237 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
14585326Sek110237 		return (error);
14595326Sek110237 
14605326Sek110237 	*mode = zfsvfs->z_os->os_mode;
14615326Sek110237 	dmu_objset_name(zfsvfs->z_os, name);
14625326Sek110237 	dmu_objset_close(zfsvfs->z_os);
14635326Sek110237 
14645326Sek110237 	return (0);
14655326Sek110237 }
14665326Sek110237 
14675326Sek110237 /*
14685326Sek110237  * Reopen zfsvfs_t::z_os and release VOPs.
14695326Sek110237  */
14705326Sek110237 int
14715326Sek110237 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname, int mode)
14725326Sek110237 {
14735326Sek110237 	int err;
14745326Sek110237 
14755326Sek110237 	ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
14765326Sek110237 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
14775326Sek110237 
14785326Sek110237 	err = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
14795326Sek110237 	if (err) {
14805326Sek110237 		zfsvfs->z_os = NULL;
14815326Sek110237 	} else {
14825326Sek110237 		znode_t *zp;
14835326Sek110237 
14845326Sek110237 		VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
14855326Sek110237 
14865326Sek110237 		/*
14875326Sek110237 		 * Attempt to re-establish all the active znodes with
14885326Sek110237 		 * their dbufs.  If a zfs_rezget() fails, then we'll let
14895326Sek110237 		 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
14905326Sek110237 		 * when they try to use their znode.
14915326Sek110237 		 */
14925326Sek110237 		mutex_enter(&zfsvfs->z_znodes_lock);
14935326Sek110237 		for (zp = list_head(&zfsvfs->z_all_znodes); zp;
14945326Sek110237 		    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
14955326Sek110237 			(void) zfs_rezget(zp);
14965326Sek110237 		}
14975326Sek110237 		mutex_exit(&zfsvfs->z_znodes_lock);
14985326Sek110237 
14995326Sek110237 	}
15005326Sek110237 
15015326Sek110237 	/* release the VOPs */
15025326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
15035326Sek110237 	rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
15045326Sek110237 
15055326Sek110237 	if (err) {
15065326Sek110237 		/*
15075326Sek110237 		 * Since we couldn't reopen zfsvfs::z_os, force
15085326Sek110237 		 * unmount this file system.
15095326Sek110237 		 */
15105326Sek110237 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
15115326Sek110237 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
15125326Sek110237 	}
15135326Sek110237 	return (err);
15145326Sek110237 }
15155326Sek110237 
1516789Sahrens static void
1517789Sahrens zfs_freevfs(vfs_t *vfsp)
1518789Sahrens {
1519789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
15204831Sgw25295 	int i;
15214831Sgw25295 
15224831Sgw25295 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
15234831Sgw25295 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1524789Sahrens 
15255331Samw 	zfs_fuid_destroy(zfsvfs);
15269214Schris.kirby@sun.com 
15279214Schris.kirby@sun.com 	/*
15289214Schris.kirby@sun.com 	 * If this is a snapshot, we have an extra VFS_HOLD on our parent
15299214Schris.kirby@sun.com 	 * from zfs_mount().  Release it here.
15309214Schris.kirby@sun.com 	 */
15319214Schris.kirby@sun.com 	if (zfsvfs->z_issnap)
15329214Schris.kirby@sun.com 		VFS_RELE(zfsvfs->z_parent->z_vfs);
15339214Schris.kirby@sun.com 
15346083Sek110237 	zfs_freezfsvfs(zfsvfs);
1535789Sahrens 
1536789Sahrens 	atomic_add_32(&zfs_active_fs_count, -1);
1537789Sahrens }
1538789Sahrens 
1539789Sahrens /*
1540789Sahrens  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
1541789Sahrens  * so we can't safely do any non-idempotent initialization here.
1542789Sahrens  * Leave that to zfs_init() and zfs_fini(), which are called
1543789Sahrens  * from the module's _init() and _fini() entry points.
1544789Sahrens  */
1545789Sahrens /*ARGSUSED*/
1546789Sahrens static int
1547789Sahrens zfs_vfsinit(int fstype, char *name)
1548789Sahrens {
1549789Sahrens 	int error;
1550789Sahrens 
1551789Sahrens 	zfsfstype = fstype;
1552789Sahrens 
1553789Sahrens 	/*
1554789Sahrens 	 * Setup vfsops and vnodeops tables.
1555789Sahrens 	 */
1556789Sahrens 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
1557789Sahrens 	if (error != 0) {
1558789Sahrens 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
1559789Sahrens 	}
1560789Sahrens 
1561789Sahrens 	error = zfs_create_op_tables();
1562789Sahrens 	if (error) {
1563789Sahrens 		zfs_remove_op_tables();
1564789Sahrens 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
1565789Sahrens 		(void) vfs_freevfsops_by_type(zfsfstype);
1566789Sahrens 		return (error);
1567789Sahrens 	}
1568789Sahrens 
1569789Sahrens 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
1570789Sahrens 
1571789Sahrens 	/*
1572849Sbonwick 	 * Unique major number for all zfs mounts.
1573849Sbonwick 	 * If we run out of 32-bit minors, we'll getudev() another major.
1574789Sahrens 	 */
1575849Sbonwick 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
1576849Sbonwick 	zfs_minor = ZFS_MIN_MINOR;
1577789Sahrens 
1578789Sahrens 	return (0);
1579789Sahrens }
1580789Sahrens 
1581789Sahrens void
1582789Sahrens zfs_init(void)
1583789Sahrens {
1584789Sahrens 	/*
1585789Sahrens 	 * Initialize .zfs directory structures
1586789Sahrens 	 */
1587789Sahrens 	zfsctl_init();
1588789Sahrens 
1589789Sahrens 	/*
1590789Sahrens 	 * Initialize znode cache, vnode ops, etc...
1591789Sahrens 	 */
1592789Sahrens 	zfs_znode_init();
1593789Sahrens }
1594789Sahrens 
1595789Sahrens void
1596789Sahrens zfs_fini(void)
1597789Sahrens {
1598789Sahrens 	zfsctl_fini();
1599789Sahrens 	zfs_znode_fini();
1600789Sahrens }
1601789Sahrens 
1602789Sahrens int
1603789Sahrens zfs_busy(void)
1604789Sahrens {
1605789Sahrens 	return (zfs_active_fs_count != 0);
1606789Sahrens }
1607789Sahrens 
16084577Sahrens int
16094577Sahrens zfs_set_version(const char *name, uint64_t newvers)
16104577Sahrens {
16114577Sahrens 	int error;
16124577Sahrens 	objset_t *os;
16134577Sahrens 	dmu_tx_t *tx;
16144577Sahrens 	uint64_t curvers;
16154577Sahrens 
16164577Sahrens 	/*
16174577Sahrens 	 * XXX for now, require that the filesystem be unmounted.  Would
16184577Sahrens 	 * be nice to find the zfsvfs_t and just update that if
16194577Sahrens 	 * possible.
16204577Sahrens 	 */
16214577Sahrens 
16224577Sahrens 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
16234577Sahrens 		return (EINVAL);
16244577Sahrens 
16256689Smaybee 	error = dmu_objset_open(name, DMU_OST_ZFS, DS_MODE_OWNER, &os);
16264577Sahrens 	if (error)
16274577Sahrens 		return (error);
16284577Sahrens 
16294577Sahrens 	error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
16304577Sahrens 	    8, 1, &curvers);
16314577Sahrens 	if (error)
16324577Sahrens 		goto out;
16334577Sahrens 	if (newvers < curvers) {
16344577Sahrens 		error = EINVAL;
16354577Sahrens 		goto out;
16364577Sahrens 	}
16374577Sahrens 
16384577Sahrens 	tx = dmu_tx_create(os);
16394577Sahrens 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 0, ZPL_VERSION_STR);
16404577Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
16414577Sahrens 	if (error) {
16424577Sahrens 		dmu_tx_abort(tx);
16434577Sahrens 		goto out;
16444577Sahrens 	}
16454577Sahrens 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1,
16464577Sahrens 	    &newvers, tx);
16474577Sahrens 
16484577Sahrens 	spa_history_internal_log(LOG_DS_UPGRADE,
16494577Sahrens 	    dmu_objset_spa(os), tx, CRED(),
16504577Sahrens 	    "oldver=%llu newver=%llu dataset = %llu", curvers, newvers,
16514577Sahrens 	    dmu_objset_id(os));
16524577Sahrens 	dmu_tx_commit(tx);
16534577Sahrens 
16544577Sahrens out:
16554577Sahrens 	dmu_objset_close(os);
16564577Sahrens 	return (error);
16574577Sahrens }
16584577Sahrens 
16595498Stimh /*
16605498Stimh  * Read a property stored within the master node.
16615498Stimh  */
16625498Stimh int
16635498Stimh zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
16645498Stimh {
16655498Stimh 	const char *pname;
16667184Stimh 	int error = ENOENT;
16675498Stimh 
16685498Stimh 	/*
16695498Stimh 	 * Look up the file system's value for the property.  For the
16705498Stimh 	 * version property, we look up a slightly different string.
16715498Stimh 	 */
16725498Stimh 	if (prop == ZFS_PROP_VERSION)
16735498Stimh 		pname = ZPL_VERSION_STR;
16745498Stimh 	else
16755498Stimh 		pname = zfs_prop_to_name(prop);
16765498Stimh 
16777184Stimh 	if (os != NULL)
16787184Stimh 		error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
16795498Stimh 
16806404Smaybee 	if (error == ENOENT) {
16815498Stimh 		/* No value set, use the default value */
16825498Stimh 		switch (prop) {
16836404Smaybee 		case ZFS_PROP_VERSION:
16846404Smaybee 			*value = ZPL_VERSION;
16856404Smaybee 			break;
16865498Stimh 		case ZFS_PROP_NORMALIZE:
16875498Stimh 		case ZFS_PROP_UTF8ONLY:
16885498Stimh 			*value = 0;
16895498Stimh 			break;
16905498Stimh 		case ZFS_PROP_CASE:
16915498Stimh 			*value = ZFS_CASE_SENSITIVE;
16925498Stimh 			break;
16935498Stimh 		default:
16946404Smaybee 			return (error);
16955498Stimh 		}
16966404Smaybee 		error = 0;
16975498Stimh 	}
16986404Smaybee 	return (error);
16995498Stimh }
17005498Stimh 
1701789Sahrens static vfsdef_t vfw = {
1702789Sahrens 	VFSDEF_VERSION,
1703789Sahrens 	MNTTYPE_ZFS,
1704789Sahrens 	zfs_vfsinit,
17055331Samw 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS|
17065331Samw 	    VSW_XID,
1707789Sahrens 	&zfs_mntopts
1708789Sahrens };
1709789Sahrens 
1710789Sahrens struct modlfs zfs_modlfs = {
17114577Sahrens 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
1712789Sahrens };
1713