xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 9179:d8fbd96b79b3)
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 
70789Sahrens static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
71789Sahrens static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr);
721544Seschrock static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
73789Sahrens static int zfs_root(vfs_t *vfsp, vnode_t **vpp);
74789Sahrens static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp);
75789Sahrens static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp);
76789Sahrens static void zfs_freevfs(vfs_t *vfsp);
77789Sahrens 
78789Sahrens static const fs_operation_def_t zfs_vfsops_template[] = {
793898Srsb 	VFSNAME_MOUNT,		{ .vfs_mount = zfs_mount },
803898Srsb 	VFSNAME_MOUNTROOT,	{ .vfs_mountroot = zfs_mountroot },
813898Srsb 	VFSNAME_UNMOUNT,	{ .vfs_unmount = zfs_umount },
823898Srsb 	VFSNAME_ROOT,		{ .vfs_root = zfs_root },
833898Srsb 	VFSNAME_STATVFS,	{ .vfs_statvfs = zfs_statvfs },
843898Srsb 	VFSNAME_SYNC,		{ .vfs_sync = zfs_sync },
853898Srsb 	VFSNAME_VGET,		{ .vfs_vget = zfs_vget },
863898Srsb 	VFSNAME_FREEVFS,	{ .vfs_freevfs = zfs_freevfs },
873898Srsb 	NULL,			NULL
88789Sahrens };
89789Sahrens 
90789Sahrens static const fs_operation_def_t zfs_vfsops_eio_template[] = {
913898Srsb 	VFSNAME_FREEVFS,	{ .vfs_freevfs =  zfs_freevfs },
923898Srsb 	NULL,			NULL
93789Sahrens };
94789Sahrens 
95789Sahrens /*
96789Sahrens  * We need to keep a count of active fs's.
97789Sahrens  * This is necessary to prevent our module
98789Sahrens  * from being unloaded after a umount -f
99789Sahrens  */
100789Sahrens static uint32_t	zfs_active_fs_count = 0;
101789Sahrens 
102789Sahrens static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
103789Sahrens static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
1043234Sck153898 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
1053234Sck153898 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
106789Sahrens 
1073234Sck153898 /*
1084596Slling  * MO_DEFAULT is not used since the default value is determined
1094596Slling  * by the equivalent property.
1103234Sck153898  */
111789Sahrens static mntopt_t mntopts[] = {
1123234Sck153898 	{ MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL },
1133234Sck153898 	{ MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL },
1144596Slling 	{ MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL },
115789Sahrens 	{ MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
116789Sahrens };
117789Sahrens 
118789Sahrens static mntopts_t zfs_mntopts = {
119789Sahrens 	sizeof (mntopts) / sizeof (mntopt_t),
120789Sahrens 	mntopts
121789Sahrens };
122789Sahrens 
123789Sahrens /*ARGSUSED*/
124789Sahrens int
125789Sahrens zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
126789Sahrens {
127789Sahrens 	/*
128789Sahrens 	 * Data integrity is job one.  We don't want a compromised kernel
129789Sahrens 	 * writing to the storage pool, so we never sync during panic.
130789Sahrens 	 */
131789Sahrens 	if (panicstr)
132789Sahrens 		return (0);
133789Sahrens 
134789Sahrens 	/*
135789Sahrens 	 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
136789Sahrens 	 * to sync metadata, which they would otherwise cache indefinitely.
137789Sahrens 	 * Semantically, the only requirement is that the sync be initiated.
138789Sahrens 	 * The DMU syncs out txgs frequently, so there's nothing to do.
139789Sahrens 	 */
140789Sahrens 	if (flag & SYNC_ATTR)
141789Sahrens 		return (0);
142789Sahrens 
143789Sahrens 	if (vfsp != NULL) {
144789Sahrens 		/*
145789Sahrens 		 * Sync a specific filesystem.
146789Sahrens 		 */
147789Sahrens 		zfsvfs_t *zfsvfs = vfsp->vfs_data;
148789Sahrens 
149789Sahrens 		ZFS_ENTER(zfsvfs);
150789Sahrens 		if (zfsvfs->z_log != NULL)
1512638Sperrin 			zil_commit(zfsvfs->z_log, UINT64_MAX, 0);
152789Sahrens 		else
153789Sahrens 			txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
154789Sahrens 		ZFS_EXIT(zfsvfs);
155789Sahrens 	} else {
156789Sahrens 		/*
157789Sahrens 		 * Sync all ZFS filesystems.  This is what happens when you
158789Sahrens 		 * run sync(1M).  Unlike other filesystems, ZFS honors the
159789Sahrens 		 * request by waiting for all pools to commit all dirty data.
160789Sahrens 		 */
161789Sahrens 		spa_sync_allpools();
162789Sahrens 	}
163789Sahrens 
164789Sahrens 	return (0);
165789Sahrens }
166789Sahrens 
1671544Seschrock static int
1681544Seschrock zfs_create_unique_device(dev_t *dev)
1691544Seschrock {
1701544Seschrock 	major_t new_major;
1711544Seschrock 
1721544Seschrock 	do {
1731544Seschrock 		ASSERT3U(zfs_minor, <=, MAXMIN32);
1741544Seschrock 		minor_t start = zfs_minor;
1751544Seschrock 		do {
1761544Seschrock 			mutex_enter(&zfs_dev_mtx);
1771544Seschrock 			if (zfs_minor >= MAXMIN32) {
1781544Seschrock 				/*
1791544Seschrock 				 * If we're still using the real major
1801544Seschrock 				 * keep out of /dev/zfs and /dev/zvol minor
1811544Seschrock 				 * number space.  If we're using a getudev()'ed
1821544Seschrock 				 * major number, we can use all of its minors.
1831544Seschrock 				 */
1841544Seschrock 				if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
1851544Seschrock 					zfs_minor = ZFS_MIN_MINOR;
1861544Seschrock 				else
1871544Seschrock 					zfs_minor = 0;
1881544Seschrock 			} else {
1891544Seschrock 				zfs_minor++;
1901544Seschrock 			}
1911544Seschrock 			*dev = makedevice(zfs_major, zfs_minor);
1921544Seschrock 			mutex_exit(&zfs_dev_mtx);
1931544Seschrock 		} while (vfs_devismounted(*dev) && zfs_minor != start);
1941544Seschrock 		if (zfs_minor == start) {
1951544Seschrock 			/*
1961544Seschrock 			 * We are using all ~262,000 minor numbers for the
1971544Seschrock 			 * current major number.  Create a new major number.
1981544Seschrock 			 */
1991544Seschrock 			if ((new_major = getudev()) == (major_t)-1) {
2001544Seschrock 				cmn_err(CE_WARN,
2011544Seschrock 				    "zfs_mount: Can't get unique major "
2021544Seschrock 				    "device number.");
2031544Seschrock 				return (-1);
2041544Seschrock 			}
2051544Seschrock 			mutex_enter(&zfs_dev_mtx);
2061544Seschrock 			zfs_major = new_major;
2071544Seschrock 			zfs_minor = 0;
2081544Seschrock 
2091544Seschrock 			mutex_exit(&zfs_dev_mtx);
2101544Seschrock 		} else {
2111544Seschrock 			break;
2121544Seschrock 		}
2131544Seschrock 		/* CONSTANTCONDITION */
2141544Seschrock 	} while (1);
2151544Seschrock 
2161544Seschrock 	return (0);
2171544Seschrock }
2181544Seschrock 
219789Sahrens static void
220789Sahrens atime_changed_cb(void *arg, uint64_t newval)
221789Sahrens {
222789Sahrens 	zfsvfs_t *zfsvfs = arg;
223789Sahrens 
224789Sahrens 	if (newval == TRUE) {
225789Sahrens 		zfsvfs->z_atime = TRUE;
226789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
227789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
228789Sahrens 	} else {
229789Sahrens 		zfsvfs->z_atime = FALSE;
230789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
231789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
232789Sahrens 	}
233789Sahrens }
234789Sahrens 
235789Sahrens static void
2363234Sck153898 xattr_changed_cb(void *arg, uint64_t newval)
2373234Sck153898 {
2383234Sck153898 	zfsvfs_t *zfsvfs = arg;
2393234Sck153898 
2403234Sck153898 	if (newval == TRUE) {
2413234Sck153898 		/* XXX locking on vfs_flag? */
2423234Sck153898 		zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
2433234Sck153898 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
2443234Sck153898 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
2453234Sck153898 	} else {
2463234Sck153898 		/* XXX locking on vfs_flag? */
2473234Sck153898 		zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
2483234Sck153898 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
2493234Sck153898 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
2503234Sck153898 	}
2513234Sck153898 }
2523234Sck153898 
2533234Sck153898 static void
254789Sahrens blksz_changed_cb(void *arg, uint64_t newval)
255789Sahrens {
256789Sahrens 	zfsvfs_t *zfsvfs = arg;
257789Sahrens 
258789Sahrens 	if (newval < SPA_MINBLOCKSIZE ||
259789Sahrens 	    newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
260789Sahrens 		newval = SPA_MAXBLOCKSIZE;
261789Sahrens 
262789Sahrens 	zfsvfs->z_max_blksz = newval;
263789Sahrens 	zfsvfs->z_vfs->vfs_bsize = newval;
264789Sahrens }
265789Sahrens 
266789Sahrens static void
267789Sahrens readonly_changed_cb(void *arg, uint64_t newval)
268789Sahrens {
269789Sahrens 	zfsvfs_t *zfsvfs = arg;
270789Sahrens 
271789Sahrens 	if (newval) {
272789Sahrens 		/* XXX locking on vfs_flag? */
273789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
274789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
275789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
276789Sahrens 	} else {
277789Sahrens 		/* XXX locking on vfs_flag? */
278789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
279789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
280789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
281789Sahrens 	}
282789Sahrens }
283789Sahrens 
284789Sahrens static void
285789Sahrens devices_changed_cb(void *arg, uint64_t newval)
286789Sahrens {
287789Sahrens 	zfsvfs_t *zfsvfs = arg;
288789Sahrens 
289789Sahrens 	if (newval == FALSE) {
290789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
291789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
292789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
293789Sahrens 	} else {
294789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
295789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
296789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
297789Sahrens 	}
298789Sahrens }
299789Sahrens 
300789Sahrens static void
301789Sahrens setuid_changed_cb(void *arg, uint64_t newval)
302789Sahrens {
303789Sahrens 	zfsvfs_t *zfsvfs = arg;
304789Sahrens 
305789Sahrens 	if (newval == FALSE) {
306789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
307789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
308789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
309789Sahrens 	} else {
310789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
311789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
312789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
313789Sahrens 	}
314789Sahrens }
315789Sahrens 
316789Sahrens static void
317789Sahrens exec_changed_cb(void *arg, uint64_t newval)
318789Sahrens {
319789Sahrens 	zfsvfs_t *zfsvfs = arg;
320789Sahrens 
321789Sahrens 	if (newval == FALSE) {
322789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
323789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
324789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
325789Sahrens 	} else {
326789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
327789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
328789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
329789Sahrens 	}
330789Sahrens }
331789Sahrens 
3325331Samw /*
3335331Samw  * The nbmand mount option can be changed at mount time.
3345331Samw  * We can't allow it to be toggled on live file systems or incorrect
3355331Samw  * behavior may be seen from cifs clients
3365331Samw  *
3375331Samw  * This property isn't registered via dsl_prop_register(), but this callback
3385331Samw  * will be called when a file system is first mounted
3395331Samw  */
3405331Samw static void
3415331Samw nbmand_changed_cb(void *arg, uint64_t newval)
3425331Samw {
3435331Samw 	zfsvfs_t *zfsvfs = arg;
3445331Samw 	if (newval == FALSE) {
3455331Samw 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
3465331Samw 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
3475331Samw 	} else {
3485331Samw 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
3495331Samw 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
3505331Samw 	}
3515331Samw }
3525331Samw 
353789Sahrens static void
354789Sahrens snapdir_changed_cb(void *arg, uint64_t newval)
355789Sahrens {
356789Sahrens 	zfsvfs_t *zfsvfs = arg;
357789Sahrens 
358789Sahrens 	zfsvfs->z_show_ctldir = newval;
359789Sahrens }
360789Sahrens 
361789Sahrens static void
3625331Samw vscan_changed_cb(void *arg, uint64_t newval)
3635331Samw {
3645331Samw 	zfsvfs_t *zfsvfs = arg;
3655331Samw 
3665331Samw 	zfsvfs->z_vscan = newval;
3675331Samw }
3685331Samw 
3695331Samw static void
370789Sahrens acl_mode_changed_cb(void *arg, uint64_t newval)
371789Sahrens {
372789Sahrens 	zfsvfs_t *zfsvfs = arg;
373789Sahrens 
374789Sahrens 	zfsvfs->z_acl_mode = newval;
375789Sahrens }
376789Sahrens 
377789Sahrens static void
378789Sahrens acl_inherit_changed_cb(void *arg, uint64_t newval)
379789Sahrens {
380789Sahrens 	zfsvfs_t *zfsvfs = arg;
381789Sahrens 
382789Sahrens 	zfsvfs->z_acl_inherit = newval;
383789Sahrens }
384789Sahrens 
3851544Seschrock static int
3861544Seschrock zfs_register_callbacks(vfs_t *vfsp)
3871544Seschrock {
3881544Seschrock 	struct dsl_dataset *ds = NULL;
3891544Seschrock 	objset_t *os = NULL;
3901544Seschrock 	zfsvfs_t *zfsvfs = NULL;
3915331Samw 	uint64_t nbmand;
3925331Samw 	int readonly, do_readonly = B_FALSE;
3935331Samw 	int setuid, do_setuid = B_FALSE;
3945331Samw 	int exec, do_exec = B_FALSE;
3955331Samw 	int devices, do_devices = B_FALSE;
3965331Samw 	int xattr, do_xattr = B_FALSE;
3975331Samw 	int atime, do_atime = B_FALSE;
3981544Seschrock 	int error = 0;
3991544Seschrock 
4001544Seschrock 	ASSERT(vfsp);
4011544Seschrock 	zfsvfs = vfsp->vfs_data;
4021544Seschrock 	ASSERT(zfsvfs);
4031544Seschrock 	os = zfsvfs->z_os;
4041544Seschrock 
4051544Seschrock 	/*
4061544Seschrock 	 * The act of registering our callbacks will destroy any mount
4071544Seschrock 	 * options we may have.  In order to enable temporary overrides
4083234Sck153898 	 * of mount options, we stash away the current values and
4091544Seschrock 	 * restore them after we register the callbacks.
4101544Seschrock 	 */
4111544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
4121544Seschrock 		readonly = B_TRUE;
4131544Seschrock 		do_readonly = B_TRUE;
4141544Seschrock 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
4151544Seschrock 		readonly = B_FALSE;
4161544Seschrock 		do_readonly = B_TRUE;
4171544Seschrock 	}
4181544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
4191544Seschrock 		devices = B_FALSE;
4201544Seschrock 		setuid = B_FALSE;
4211544Seschrock 		do_devices = B_TRUE;
4221544Seschrock 		do_setuid = B_TRUE;
4231544Seschrock 	} else {
4241544Seschrock 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
4251544Seschrock 			devices = B_FALSE;
4261544Seschrock 			do_devices = B_TRUE;
4273912Slling 		} else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
4281544Seschrock 			devices = B_TRUE;
4291544Seschrock 			do_devices = B_TRUE;
4301544Seschrock 		}
4311544Seschrock 
4321544Seschrock 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
4331544Seschrock 			setuid = B_FALSE;
4341544Seschrock 			do_setuid = B_TRUE;
4351544Seschrock 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
4361544Seschrock 			setuid = B_TRUE;
4371544Seschrock 			do_setuid = B_TRUE;
4381544Seschrock 		}
4391544Seschrock 	}
4401544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
4411544Seschrock 		exec = B_FALSE;
4421544Seschrock 		do_exec = B_TRUE;
4431544Seschrock 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
4441544Seschrock 		exec = B_TRUE;
4451544Seschrock 		do_exec = B_TRUE;
4461544Seschrock 	}
4473234Sck153898 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
4483234Sck153898 		xattr = B_FALSE;
4493234Sck153898 		do_xattr = B_TRUE;
4503234Sck153898 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
4513234Sck153898 		xattr = B_TRUE;
4523234Sck153898 		do_xattr = B_TRUE;
4533234Sck153898 	}
4544596Slling 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
4554596Slling 		atime = B_FALSE;
4564596Slling 		do_atime = B_TRUE;
4574596Slling 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
4584596Slling 		atime = B_TRUE;
4594596Slling 		do_atime = B_TRUE;
4604596Slling 	}
4611544Seschrock 
4621544Seschrock 	/*
4635331Samw 	 * nbmand is a special property.  It can only be changed at
4645331Samw 	 * mount time.
4655331Samw 	 *
4665331Samw 	 * This is weird, but it is documented to only be changeable
4675331Samw 	 * at mount time.
4685331Samw 	 */
4695331Samw 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
4705331Samw 		nbmand = B_FALSE;
4715331Samw 	} else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
4725331Samw 		nbmand = B_TRUE;
4735331Samw 	} else {
4745331Samw 		char osname[MAXNAMELEN];
4755331Samw 
4765331Samw 		dmu_objset_name(os, osname);
4775331Samw 		if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
4787265Sahrens 		    NULL)) {
4797265Sahrens 			return (error);
4807265Sahrens 		}
4815331Samw 	}
4825331Samw 
4835331Samw 	/*
4841544Seschrock 	 * Register property callbacks.
4851544Seschrock 	 *
4861544Seschrock 	 * It would probably be fine to just check for i/o error from
4871544Seschrock 	 * the first prop_register(), but I guess I like to go
4881544Seschrock 	 * overboard...
4891544Seschrock 	 */
4901544Seschrock 	ds = dmu_objset_ds(os);
4911544Seschrock 	error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
4921544Seschrock 	error = error ? error : dsl_prop_register(ds,
4933234Sck153898 	    "xattr", xattr_changed_cb, zfsvfs);
4943234Sck153898 	error = error ? error : dsl_prop_register(ds,
4951544Seschrock 	    "recordsize", blksz_changed_cb, zfsvfs);
4961544Seschrock 	error = error ? error : dsl_prop_register(ds,
4971544Seschrock 	    "readonly", readonly_changed_cb, zfsvfs);
4981544Seschrock 	error = error ? error : dsl_prop_register(ds,
4991544Seschrock 	    "devices", devices_changed_cb, zfsvfs);
5001544Seschrock 	error = error ? error : dsl_prop_register(ds,
5011544Seschrock 	    "setuid", setuid_changed_cb, zfsvfs);
5021544Seschrock 	error = error ? error : dsl_prop_register(ds,
5031544Seschrock 	    "exec", exec_changed_cb, zfsvfs);
5041544Seschrock 	error = error ? error : dsl_prop_register(ds,
5051544Seschrock 	    "snapdir", snapdir_changed_cb, zfsvfs);
5061544Seschrock 	error = error ? error : dsl_prop_register(ds,
5071544Seschrock 	    "aclmode", acl_mode_changed_cb, zfsvfs);
5081544Seschrock 	error = error ? error : dsl_prop_register(ds,
5091544Seschrock 	    "aclinherit", acl_inherit_changed_cb, zfsvfs);
5105331Samw 	error = error ? error : dsl_prop_register(ds,
5115331Samw 	    "vscan", vscan_changed_cb, zfsvfs);
5121544Seschrock 	if (error)
5131544Seschrock 		goto unregister;
5141544Seschrock 
5151544Seschrock 	/*
5161544Seschrock 	 * Invoke our callbacks to restore temporary mount options.
5171544Seschrock 	 */
5181544Seschrock 	if (do_readonly)
5191544Seschrock 		readonly_changed_cb(zfsvfs, readonly);
5201544Seschrock 	if (do_setuid)
5211544Seschrock 		setuid_changed_cb(zfsvfs, setuid);
5221544Seschrock 	if (do_exec)
5231544Seschrock 		exec_changed_cb(zfsvfs, exec);
5241544Seschrock 	if (do_devices)
5251544Seschrock 		devices_changed_cb(zfsvfs, devices);
5263234Sck153898 	if (do_xattr)
5273234Sck153898 		xattr_changed_cb(zfsvfs, xattr);
5284596Slling 	if (do_atime)
5294596Slling 		atime_changed_cb(zfsvfs, atime);
5301544Seschrock 
5315331Samw 	nbmand_changed_cb(zfsvfs, nbmand);
5325331Samw 
5331544Seschrock 	return (0);
5341544Seschrock 
5351544Seschrock unregister:
5361544Seschrock 	/*
5371544Seschrock 	 * We may attempt to unregister some callbacks that are not
5381544Seschrock 	 * registered, but this is OK; it will simply return ENOMSG,
5391544Seschrock 	 * which we will ignore.
5401544Seschrock 	 */
5411544Seschrock 	(void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
5423234Sck153898 	(void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
5431544Seschrock 	(void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
5441544Seschrock 	(void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
5451544Seschrock 	(void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs);
5461544Seschrock 	(void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
5471544Seschrock 	(void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
5481544Seschrock 	(void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
5491544Seschrock 	(void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
5501544Seschrock 	(void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
5511544Seschrock 	    zfsvfs);
5525331Samw 	(void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
5531544Seschrock 	return (error);
5541544Seschrock 
5551544Seschrock }
5561544Seschrock 
5571544Seschrock static int
5585326Sek110237 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
5595326Sek110237 {
5605326Sek110237 	int error;
5615326Sek110237 
5625326Sek110237 	error = zfs_register_callbacks(zfsvfs->z_vfs);
5635326Sek110237 	if (error)
5645326Sek110237 		return (error);
5655326Sek110237 
5665326Sek110237 	/*
5675326Sek110237 	 * Set the objset user_ptr to track its zfsvfs.
5685326Sek110237 	 */
5695326Sek110237 	mutex_enter(&zfsvfs->z_os->os->os_user_ptr_lock);
5705326Sek110237 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
5715326Sek110237 	mutex_exit(&zfsvfs->z_os->os->os_user_ptr_lock);
5725326Sek110237 
5735326Sek110237 	/*
5745326Sek110237 	 * If we are not mounting (ie: online recv), then we don't
5755326Sek110237 	 * have to worry about replaying the log as we blocked all
5765326Sek110237 	 * operations out since we closed the ZIL.
5775326Sek110237 	 */
5785326Sek110237 	if (mounting) {
5797638SNeil.Perrin@Sun.COM 		boolean_t readonly;
5807638SNeil.Perrin@Sun.COM 
5815326Sek110237 		/*
5825326Sek110237 		 * During replay we remove the read only flag to
5835326Sek110237 		 * allow replays to succeed.
5845326Sek110237 		 */
5855326Sek110237 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
5868227SNeil.Perrin@Sun.COM 		if (readonly != 0)
5878227SNeil.Perrin@Sun.COM 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
5888227SNeil.Perrin@Sun.COM 		else
5898227SNeil.Perrin@Sun.COM 			zfs_unlinked_drain(zfsvfs);
5905326Sek110237 
5918227SNeil.Perrin@Sun.COM 		zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
5928227SNeil.Perrin@Sun.COM 		if (zil_disable) {
5938227SNeil.Perrin@Sun.COM 			zil_destroy(zfsvfs->z_log, 0);
5948227SNeil.Perrin@Sun.COM 			zfsvfs->z_log = NULL;
5958227SNeil.Perrin@Sun.COM 		} else {
5968227SNeil.Perrin@Sun.COM 			/*
5978227SNeil.Perrin@Sun.COM 			 * Parse and replay the intent log.
5988227SNeil.Perrin@Sun.COM 			 *
5998227SNeil.Perrin@Sun.COM 			 * Because of ziltest, this must be done after
6008227SNeil.Perrin@Sun.COM 			 * zfs_unlinked_drain().  (Further note: ziltest
6018227SNeil.Perrin@Sun.COM 			 * doesn't use readonly mounts, where
6028227SNeil.Perrin@Sun.COM 			 * zfs_unlinked_drain() isn't called.)  This is because
6038227SNeil.Perrin@Sun.COM 			 * ziltest causes spa_sync() to think it's committed,
6048227SNeil.Perrin@Sun.COM 			 * but actually it is not, so the intent log contains
6058227SNeil.Perrin@Sun.COM 			 * many txg's worth of changes.
6068227SNeil.Perrin@Sun.COM 			 *
6078227SNeil.Perrin@Sun.COM 			 * In particular, if object N is in the unlinked set in
6088227SNeil.Perrin@Sun.COM 			 * the last txg to actually sync, then it could be
6098227SNeil.Perrin@Sun.COM 			 * actually freed in a later txg and then reallocated
6108227SNeil.Perrin@Sun.COM 			 * in a yet later txg.  This would write a "create
6118227SNeil.Perrin@Sun.COM 			 * object N" record to the intent log.  Normally, this
6128227SNeil.Perrin@Sun.COM 			 * would be fine because the spa_sync() would have
6138227SNeil.Perrin@Sun.COM 			 * written out the fact that object N is free, before
6148227SNeil.Perrin@Sun.COM 			 * we could write the "create object N" intent log
6158227SNeil.Perrin@Sun.COM 			 * record.
6168227SNeil.Perrin@Sun.COM 			 *
6178227SNeil.Perrin@Sun.COM 			 * But when we are in ziltest mode, we advance the "open
6188227SNeil.Perrin@Sun.COM 			 * txg" without actually spa_sync()-ing the changes to
6198227SNeil.Perrin@Sun.COM 			 * disk.  So we would see that object N is still
6208227SNeil.Perrin@Sun.COM 			 * allocated and in the unlinked set, and there is an
6218227SNeil.Perrin@Sun.COM 			 * intent log record saying to allocate it.
6228227SNeil.Perrin@Sun.COM 			 */
6238227SNeil.Perrin@Sun.COM 			zfsvfs->z_replay = B_TRUE;
6248227SNeil.Perrin@Sun.COM 			zil_replay(zfsvfs->z_os, zfsvfs, zfs_replay_vector);
6258227SNeil.Perrin@Sun.COM 			zfsvfs->z_replay = B_FALSE;
6268227SNeil.Perrin@Sun.COM 		}
6275326Sek110237 		zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
6285326Sek110237 	}
6295326Sek110237 
6305326Sek110237 	return (0);
6315326Sek110237 }
6325326Sek110237 
6336083Sek110237 static void
6346083Sek110237 zfs_freezfsvfs(zfsvfs_t *zfsvfs)
6356083Sek110237 {
6366083Sek110237 	mutex_destroy(&zfsvfs->z_znodes_lock);
6376083Sek110237 	mutex_destroy(&zfsvfs->z_online_recv_lock);
6389030SMark.Shellenbaum@Sun.COM 	mutex_destroy(&zfsvfs->z_lock);
6396083Sek110237 	list_destroy(&zfsvfs->z_all_znodes);
6406083Sek110237 	rrw_destroy(&zfsvfs->z_teardown_lock);
6416083Sek110237 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
6426083Sek110237 	rw_destroy(&zfsvfs->z_fuid_lock);
6436083Sek110237 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
6446083Sek110237 }
6456083Sek110237 
6465326Sek110237 static int
6477046Sahrens zfs_domount(vfs_t *vfsp, char *osname)
6481544Seschrock {
6491544Seschrock 	dev_t mount_dev;
6501544Seschrock 	uint64_t recordsize, readonly;
6511544Seschrock 	int error = 0;
6521544Seschrock 	int mode;
6531544Seschrock 	zfsvfs_t *zfsvfs;
6541544Seschrock 	znode_t *zp = NULL;
6551544Seschrock 
6561544Seschrock 	ASSERT(vfsp);
6571544Seschrock 	ASSERT(osname);
6581544Seschrock 
6591544Seschrock 	/*
6601544Seschrock 	 * Initialize the zfs-specific filesystem structure.
6611544Seschrock 	 * Should probably make this a kmem cache, shuffle fields,
6621544Seschrock 	 * and just bzero up to z_hold_mtx[].
6631544Seschrock 	 */
6641544Seschrock 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
6651544Seschrock 	zfsvfs->z_vfs = vfsp;
6661544Seschrock 	zfsvfs->z_parent = zfsvfs;
6671544Seschrock 	zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
6681544Seschrock 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
669*9179SMark.Shellenbaum@Sun.COM 	zfsvfs->z_fuid_dirty = B_FALSE;
6701544Seschrock 
6711544Seschrock 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
6726083Sek110237 	mutex_init(&zfsvfs->z_online_recv_lock, NULL, MUTEX_DEFAULT, NULL);
6739030SMark.Shellenbaum@Sun.COM 	mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
6741544Seschrock 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
6751544Seschrock 	    offsetof(znode_t, z_link_node));
6765326Sek110237 	rrw_init(&zfsvfs->z_teardown_lock);
6775326Sek110237 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
6785498Stimh 	rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
6791544Seschrock 
6801544Seschrock 	/* Initialize the generic filesystem structure. */
6811544Seschrock 	vfsp->vfs_bcount = 0;
6821544Seschrock 	vfsp->vfs_data = NULL;
6831544Seschrock 
6841544Seschrock 	if (zfs_create_unique_device(&mount_dev) == -1) {
6851544Seschrock 		error = ENODEV;
6861544Seschrock 		goto out;
6871544Seschrock 	}
6881544Seschrock 	ASSERT(vfs_devismounted(mount_dev) == 0);
6891544Seschrock 
6901544Seschrock 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
6911544Seschrock 	    NULL))
6921544Seschrock 		goto out;
6931544Seschrock 
6941544Seschrock 	vfsp->vfs_dev = mount_dev;
6951544Seschrock 	vfsp->vfs_fstype = zfsfstype;
6961544Seschrock 	vfsp->vfs_bsize = recordsize;
6971544Seschrock 	vfsp->vfs_flag |= VFS_NOTRUNC;
6981544Seschrock 	vfsp->vfs_data = zfsvfs;
6991544Seschrock 
7001544Seschrock 	if (error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL))
7011544Seschrock 		goto out;
7021544Seschrock 
7036689Smaybee 	mode = DS_MODE_OWNER;
7041544Seschrock 	if (readonly)
7056689Smaybee 		mode |= DS_MODE_READONLY;
7061544Seschrock 
7071544Seschrock 	error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
7081544Seschrock 	if (error == EROFS) {
7096689Smaybee 		mode = DS_MODE_OWNER | DS_MODE_READONLY;
7101544Seschrock 		error = dmu_objset_open(osname, DMU_OST_ZFS, mode,
7111544Seschrock 		    &zfsvfs->z_os);
7121544Seschrock 	}
7131544Seschrock 
7141544Seschrock 	if (error)
7151544Seschrock 		goto out;
7161544Seschrock 
7177046Sahrens 	if (error = zfs_init_fs(zfsvfs, &zp))
7181544Seschrock 		goto out;
7191544Seschrock 
7201544Seschrock 	/* The call to zfs_init_fs leaves the vnode held, release it here. */
7211544Seschrock 	VN_RELE(ZTOV(zp));
7221544Seschrock 
7235331Samw 	/*
7245331Samw 	 * Set features for file system.
7255331Samw 	 */
7265331Samw 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
7275331Samw 	if (zfsvfs->z_use_fuids) {
7285331Samw 		vfs_set_feature(vfsp, VFSFT_XVATTR);
7297757SJanice.Chang@Sun.COM 		vfs_set_feature(vfsp, VFSFT_SYSATTR_VIEWS);
7305331Samw 		vfs_set_feature(vfsp, VFSFT_ACEMASKONACCESS);
7315331Samw 		vfs_set_feature(vfsp, VFSFT_ACLONCREATE);
7325331Samw 	}
7335498Stimh 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
7345498Stimh 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
7355498Stimh 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
7365498Stimh 		vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
7375498Stimh 	} else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
7385498Stimh 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
7395498Stimh 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
7405498Stimh 	}
7415331Samw 
7421544Seschrock 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
7435331Samw 		uint64_t pval;
7443234Sck153898 
7451544Seschrock 		ASSERT(mode & DS_MODE_READONLY);
7461544Seschrock 		atime_changed_cb(zfsvfs, B_FALSE);
7471544Seschrock 		readonly_changed_cb(zfsvfs, B_TRUE);
7485331Samw 		if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
7493234Sck153898 			goto out;
7505331Samw 		xattr_changed_cb(zfsvfs, pval);
7511544Seschrock 		zfsvfs->z_issnap = B_TRUE;
7521544Seschrock 	} else {
7535326Sek110237 		error = zfsvfs_setup(zfsvfs, B_TRUE);
7541544Seschrock 	}
7551544Seschrock 
7561544Seschrock 	if (!zfsvfs->z_issnap)
7571544Seschrock 		zfsctl_create(zfsvfs);
7581544Seschrock out:
7591544Seschrock 	if (error) {
7601544Seschrock 		if (zfsvfs->z_os)
7611544Seschrock 			dmu_objset_close(zfsvfs->z_os);
7626083Sek110237 		zfs_freezfsvfs(zfsvfs);
7631544Seschrock 	} else {
7641544Seschrock 		atomic_add_32(&zfs_active_fs_count, 1);
7651544Seschrock 	}
7661544Seschrock 
7671544Seschrock 	return (error);
7681544Seschrock }
7691544Seschrock 
7701544Seschrock void
7711544Seschrock zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
7721544Seschrock {
7731544Seschrock 	objset_t *os = zfsvfs->z_os;
7741544Seschrock 	struct dsl_dataset *ds;
7751544Seschrock 
7761544Seschrock 	/*
7771544Seschrock 	 * Unregister properties.
7781544Seschrock 	 */
7791544Seschrock 	if (!dmu_objset_is_snapshot(os)) {
7801544Seschrock 		ds = dmu_objset_ds(os);
7811544Seschrock 		VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
7821544Seschrock 		    zfsvfs) == 0);
7831544Seschrock 
7843234Sck153898 		VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
7853234Sck153898 		    zfsvfs) == 0);
7863234Sck153898 
7871544Seschrock 		VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
7881544Seschrock 		    zfsvfs) == 0);
7891544Seschrock 
7901544Seschrock 		VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
7911544Seschrock 		    zfsvfs) == 0);
7921544Seschrock 
7931544Seschrock 		VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
7941544Seschrock 		    zfsvfs) == 0);
7951544Seschrock 
7961544Seschrock 		VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
7971544Seschrock 		    zfsvfs) == 0);
7981544Seschrock 
7991544Seschrock 		VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
8001544Seschrock 		    zfsvfs) == 0);
8011544Seschrock 
8021544Seschrock 		VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
8031544Seschrock 		    zfsvfs) == 0);
8041544Seschrock 
8051544Seschrock 		VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
8061544Seschrock 		    zfsvfs) == 0);
8071544Seschrock 
8081544Seschrock 		VERIFY(dsl_prop_unregister(ds, "aclinherit",
8091544Seschrock 		    acl_inherit_changed_cb, zfsvfs) == 0);
8105331Samw 
8115331Samw 		VERIFY(dsl_prop_unregister(ds, "vscan",
8125331Samw 		    vscan_changed_cb, zfsvfs) == 0);
8131544Seschrock 	}
8141544Seschrock }
8151544Seschrock 
8163912Slling /*
8173912Slling  * Convert a decimal digit string to a uint64_t integer.
8183912Slling  */
8193912Slling static int
8203912Slling str_to_uint64(char *str, uint64_t *objnum)
8213912Slling {
8223912Slling 	uint64_t num = 0;
8233912Slling 
8243912Slling 	while (*str) {
8253912Slling 		if (*str < '0' || *str > '9')
8263912Slling 			return (EINVAL);
8273912Slling 
8283912Slling 		num = num*10 + *str++ - '0';
8293912Slling 	}
8303912Slling 
8313912Slling 	*objnum = num;
8323912Slling 	return (0);
8333912Slling }
8343912Slling 
8353912Slling /*
8363912Slling  * The boot path passed from the boot loader is in the form of
8373912Slling  * "rootpool-name/root-filesystem-object-number'. Convert this
8383912Slling  * string to a dataset name: "rootpool-name/root-filesystem-name".
8393912Slling  */
8403912Slling static int
8416423Sgw25295 zfs_parse_bootfs(char *bpath, char *outpath)
8423912Slling {
8433912Slling 	char *slashp;
8443912Slling 	uint64_t objnum;
8453912Slling 	int error;
8463912Slling 
8473912Slling 	if (*bpath == 0 || *bpath == '/')
8483912Slling 		return (EINVAL);
8493912Slling 
8507656SSherry.Moore@Sun.COM 	(void) strcpy(outpath, bpath);
8517656SSherry.Moore@Sun.COM 
8523912Slling 	slashp = strchr(bpath, '/');
8533912Slling 
8543912Slling 	/* if no '/', just return the pool name */
8553912Slling 	if (slashp == NULL) {
8563912Slling 		return (0);
8573912Slling 	}
8583912Slling 
8597656SSherry.Moore@Sun.COM 	/* if not a number, just return the root dataset name */
8607656SSherry.Moore@Sun.COM 	if (str_to_uint64(slashp+1, &objnum)) {
8617656SSherry.Moore@Sun.COM 		return (0);
8627656SSherry.Moore@Sun.COM 	}
8633912Slling 
8643912Slling 	*slashp = '\0';
8653912Slling 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
8663912Slling 	*slashp = '/';
8673912Slling 
8683912Slling 	return (error);
8693912Slling }
8703912Slling 
8711544Seschrock static int
8721544Seschrock zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
8731544Seschrock {
8741544Seschrock 	int error = 0;
8751544Seschrock 	static int zfsrootdone = 0;
8761544Seschrock 	zfsvfs_t *zfsvfs = NULL;
8771544Seschrock 	znode_t *zp = NULL;
8781544Seschrock 	vnode_t *vp = NULL;
8796423Sgw25295 	char *zfs_bootfs;
8807147Staylor 	char *zfs_devid;
8811544Seschrock 
8821544Seschrock 	ASSERT(vfsp);
8831544Seschrock 
8841544Seschrock 	/*
8853912Slling 	 * The filesystem that we mount as root is defined in the
8866423Sgw25295 	 * boot property "zfs-bootfs" with a format of
8876423Sgw25295 	 * "poolname/root-dataset-objnum".
8881544Seschrock 	 */
8891544Seschrock 	if (why == ROOT_INIT) {
8901544Seschrock 		if (zfsrootdone++)
8911544Seschrock 			return (EBUSY);
8926423Sgw25295 		/*
8936423Sgw25295 		 * the process of doing a spa_load will require the
8946423Sgw25295 		 * clock to be set before we could (for example) do
8956423Sgw25295 		 * something better by looking at the timestamp on
8966423Sgw25295 		 * an uberblock, so just set it to -1.
8976423Sgw25295 		 */
8986423Sgw25295 		clkset(-1);
8991544Seschrock 
9007147Staylor 		if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
9017147Staylor 			cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
9027147Staylor 			    "bootfs name");
9036423Sgw25295 			return (EINVAL);
9045648Ssetje 		}
9057147Staylor 		zfs_devid = spa_get_bootprop("diskdevid");
9067147Staylor 		error = spa_import_rootpool(rootfs.bo_name, zfs_devid);
9077147Staylor 		if (zfs_devid)
9087147Staylor 			spa_free_bootprop(zfs_devid);
9097147Staylor 		if (error) {
9107147Staylor 			spa_free_bootprop(zfs_bootfs);
9117147Staylor 			cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
9127147Staylor 			    error);
9137147Staylor 			return (error);
9147147Staylor 		}
9157147Staylor 		if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
9167147Staylor 			spa_free_bootprop(zfs_bootfs);
9177147Staylor 			cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
9186423Sgw25295 			    error);
9196423Sgw25295 			return (error);
9206423Sgw25295 		}
9213912Slling 
9227147Staylor 		spa_free_bootprop(zfs_bootfs);
9231544Seschrock 
9241544Seschrock 		if (error = vfs_lock(vfsp))
9251544Seschrock 			return (error);
9261544Seschrock 
9277046Sahrens 		if (error = zfs_domount(vfsp, rootfs.bo_name)) {
9287147Staylor 			cmn_err(CE_NOTE, "zfs_domount: error %d", error);
9291544Seschrock 			goto out;
9306423Sgw25295 		}
9311544Seschrock 
9321544Seschrock 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
9331544Seschrock 		ASSERT(zfsvfs);
9346423Sgw25295 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
9357147Staylor 			cmn_err(CE_NOTE, "zfs_zget: error %d", error);
9361544Seschrock 			goto out;
9376423Sgw25295 		}
9381544Seschrock 
9391544Seschrock 		vp = ZTOV(zp);
9401544Seschrock 		mutex_enter(&vp->v_lock);
9411544Seschrock 		vp->v_flag |= VROOT;
9421544Seschrock 		mutex_exit(&vp->v_lock);
9431544Seschrock 		rootvp = vp;
9441544Seschrock 
9451544Seschrock 		/*
9466570Smarks 		 * Leave rootvp held.  The root file system is never unmounted.
9471544Seschrock 		 */
9481544Seschrock 
9491544Seschrock 		vfs_add((struct vnode *)0, vfsp,
9501544Seschrock 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
9511544Seschrock out:
9521544Seschrock 		vfs_unlock(vfsp);
9536423Sgw25295 		return (error);
9541544Seschrock 	} else if (why == ROOT_REMOUNT) {
9551544Seschrock 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
9561544Seschrock 		vfsp->vfs_flag |= VFS_REMOUNT;
9574596Slling 
9584596Slling 		/* refresh mount options */
9594596Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
9604596Slling 		return (zfs_register_callbacks(vfsp));
9614596Slling 
9621544Seschrock 	} else if (why == ROOT_UNMOUNT) {
9631544Seschrock 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
9641544Seschrock 		(void) zfs_sync(vfsp, 0, 0);
9651544Seschrock 		return (0);
9661544Seschrock 	}
9671544Seschrock 
9681544Seschrock 	/*
9691544Seschrock 	 * if "why" is equal to anything else other than ROOT_INIT,
9701544Seschrock 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
9711544Seschrock 	 */
9721544Seschrock 	return (ENOTSUP);
9731544Seschrock }
9741544Seschrock 
975789Sahrens /*ARGSUSED*/
976789Sahrens static int
977789Sahrens zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
978789Sahrens {
979789Sahrens 	char		*osname;
980789Sahrens 	pathname_t	spn;
981789Sahrens 	int		error = 0;
982789Sahrens 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
9833912Slling 	    UIO_SYSSPACE : UIO_USERSPACE;
984789Sahrens 	int		canwrite;
985789Sahrens 
986789Sahrens 	if (mvp->v_type != VDIR)
987789Sahrens 		return (ENOTDIR);
988789Sahrens 
989789Sahrens 	mutex_enter(&mvp->v_lock);
990789Sahrens 	if ((uap->flags & MS_REMOUNT) == 0 &&
991789Sahrens 	    (uap->flags & MS_OVERLAY) == 0 &&
992789Sahrens 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
993789Sahrens 		mutex_exit(&mvp->v_lock);
994789Sahrens 		return (EBUSY);
995789Sahrens 	}
996789Sahrens 	mutex_exit(&mvp->v_lock);
997789Sahrens 
998789Sahrens 	/*
999789Sahrens 	 * ZFS does not support passing unparsed data in via MS_DATA.
1000789Sahrens 	 * Users should use the MS_OPTIONSTR interface; this means
1001789Sahrens 	 * that all option parsing is already done and the options struct
1002789Sahrens 	 * can be interrogated.
1003789Sahrens 	 */
1004789Sahrens 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
1005789Sahrens 		return (EINVAL);
1006789Sahrens 
1007789Sahrens 	/*
1008789Sahrens 	 * Get the objset name (the "special" mount argument).
1009789Sahrens 	 */
1010789Sahrens 	if (error = pn_get(uap->spec, fromspace, &spn))
1011789Sahrens 		return (error);
1012789Sahrens 
1013789Sahrens 	osname = spn.pn_path;
1014789Sahrens 
10154543Smarks 	/*
10164543Smarks 	 * Check for mount privilege?
10174543Smarks 	 *
10184543Smarks 	 * If we don't have privilege then see if
10194543Smarks 	 * we have local permission to allow it
10204543Smarks 	 */
10214543Smarks 	error = secpolicy_fs_mount(cr, mvp, vfsp);
10224543Smarks 	if (error) {
10234543Smarks 		error = dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr);
10244543Smarks 		if (error == 0) {
10254543Smarks 			vattr_t		vattr;
10264543Smarks 
10274543Smarks 			/*
10284543Smarks 			 * Make sure user is the owner of the mount point
10294543Smarks 			 * or has sufficient privileges.
10304543Smarks 			 */
10314543Smarks 
10324543Smarks 			vattr.va_mask = AT_UID;
10334543Smarks 
10345331Samw 			if (error = VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
10354543Smarks 				goto out;
10364543Smarks 			}
10374543Smarks 
10385489Smarks 			if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 &&
10395489Smarks 			    VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) {
10405489Smarks 				error = EPERM;
10414543Smarks 				goto out;
10424543Smarks 			}
10434543Smarks 
10444543Smarks 			secpolicy_fs_mount_clearopts(cr, vfsp);
10454543Smarks 		} else {
10464543Smarks 			goto out;
10474543Smarks 		}
10484543Smarks 	}
1049789Sahrens 
1050789Sahrens 	/*
1051789Sahrens 	 * Refuse to mount a filesystem if we are in a local zone and the
1052789Sahrens 	 * dataset is not visible.
1053789Sahrens 	 */
1054789Sahrens 	if (!INGLOBALZONE(curproc) &&
1055789Sahrens 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1056789Sahrens 		error = EPERM;
1057789Sahrens 		goto out;
1058789Sahrens 	}
1059789Sahrens 
10604596Slling 	/*
10614596Slling 	 * When doing a remount, we simply refresh our temporary properties
10624596Slling 	 * according to those options set in the current VFS options.
10634596Slling 	 */
10644596Slling 	if (uap->flags & MS_REMOUNT) {
10654596Slling 		/* refresh mount options */
10664596Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
10674596Slling 		error = zfs_register_callbacks(vfsp);
10684596Slling 		goto out;
10694596Slling 	}
10704596Slling 
10717046Sahrens 	error = zfs_domount(vfsp, osname);
1072789Sahrens 
1073789Sahrens out:
1074789Sahrens 	pn_free(&spn);
1075789Sahrens 	return (error);
1076789Sahrens }
1077789Sahrens 
1078789Sahrens static int
1079789Sahrens zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1080789Sahrens {
1081789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1082789Sahrens 	dev32_t d32;
10832885Sahrens 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
1084789Sahrens 
1085789Sahrens 	ZFS_ENTER(zfsvfs);
1086789Sahrens 
10872885Sahrens 	dmu_objset_space(zfsvfs->z_os,
10882885Sahrens 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
1089789Sahrens 
1090789Sahrens 	/*
1091789Sahrens 	 * The underlying storage pool actually uses multiple block sizes.
1092789Sahrens 	 * We report the fragsize as the smallest block size we support,
1093789Sahrens 	 * and we report our blocksize as the filesystem's maximum blocksize.
1094789Sahrens 	 */
1095789Sahrens 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1096789Sahrens 	statp->f_bsize = zfsvfs->z_max_blksz;
1097789Sahrens 
1098789Sahrens 	/*
1099789Sahrens 	 * The following report "total" blocks of various kinds in the
1100789Sahrens 	 * file system, but reported in terms of f_frsize - the
1101789Sahrens 	 * "fragment" size.
1102789Sahrens 	 */
1103789Sahrens 
11042885Sahrens 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
11052885Sahrens 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
1106789Sahrens 	statp->f_bavail = statp->f_bfree; /* no root reservation */
1107789Sahrens 
1108789Sahrens 	/*
1109789Sahrens 	 * statvfs() should really be called statufs(), because it assumes
1110789Sahrens 	 * static metadata.  ZFS doesn't preallocate files, so the best
1111789Sahrens 	 * we can do is report the max that could possibly fit in f_files,
1112789Sahrens 	 * and that minus the number actually used in f_ffree.
1113789Sahrens 	 * For f_ffree, report the smaller of the number of object available
1114789Sahrens 	 * and the number of blocks (each object will take at least a block).
1115789Sahrens 	 */
11162885Sahrens 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
1117789Sahrens 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
11182885Sahrens 	statp->f_files = statp->f_ffree + usedobjs;
1119789Sahrens 
1120789Sahrens 	(void) cmpldev(&d32, vfsp->vfs_dev);
1121789Sahrens 	statp->f_fsid = d32;
1122789Sahrens 
1123789Sahrens 	/*
1124789Sahrens 	 * We're a zfs filesystem.
1125789Sahrens 	 */
1126789Sahrens 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1127789Sahrens 
11281123Smarks 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1129789Sahrens 
1130789Sahrens 	statp->f_namemax = ZFS_MAXNAMELEN;
1131789Sahrens 
1132789Sahrens 	/*
1133789Sahrens 	 * We have all of 32 characters to stuff a string here.
1134789Sahrens 	 * Is there anything useful we could/should provide?
1135789Sahrens 	 */
1136789Sahrens 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
1137789Sahrens 
1138789Sahrens 	ZFS_EXIT(zfsvfs);
1139789Sahrens 	return (0);
1140789Sahrens }
1141789Sahrens 
1142789Sahrens static int
1143789Sahrens zfs_root(vfs_t *vfsp, vnode_t **vpp)
1144789Sahrens {
1145789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1146789Sahrens 	znode_t *rootzp;
1147789Sahrens 	int error;
1148789Sahrens 
1149789Sahrens 	ZFS_ENTER(zfsvfs);
1150789Sahrens 
1151789Sahrens 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1152789Sahrens 	if (error == 0)
1153789Sahrens 		*vpp = ZTOV(rootzp);
1154789Sahrens 
1155789Sahrens 	ZFS_EXIT(zfsvfs);
1156789Sahrens 	return (error);
1157789Sahrens }
1158789Sahrens 
11595326Sek110237 /*
11605326Sek110237  * Teardown the zfsvfs::z_os.
11615326Sek110237  *
11625326Sek110237  * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
11635326Sek110237  * and 'z_teardown_inactive_lock' held.
11645326Sek110237  */
11655326Sek110237 static int
11665326Sek110237 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
11675326Sek110237 {
11685642Smaybee 	znode_t	*zp;
11695326Sek110237 
11705326Sek110237 	rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
11715326Sek110237 
11725326Sek110237 	if (!unmounting) {
11735326Sek110237 		/*
11745326Sek110237 		 * We purge the parent filesystem's vfsp as the parent
11755326Sek110237 		 * filesystem and all of its snapshots have their vnode's
11765326Sek110237 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
11775326Sek110237 		 * 'z_parent' is self referential for non-snapshots.
11785326Sek110237 		 */
11795326Sek110237 		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
11805326Sek110237 	}
11815326Sek110237 
11825326Sek110237 	/*
11835326Sek110237 	 * Close the zil. NB: Can't close the zil while zfs_inactive
11845326Sek110237 	 * threads are blocked as zil_close can call zfs_inactive.
11855326Sek110237 	 */
11865326Sek110237 	if (zfsvfs->z_log) {
11875326Sek110237 		zil_close(zfsvfs->z_log);
11885326Sek110237 		zfsvfs->z_log = NULL;
11895326Sek110237 	}
11905326Sek110237 
11915326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
11925326Sek110237 
11935326Sek110237 	/*
11945326Sek110237 	 * If we are not unmounting (ie: online recv) and someone already
11955326Sek110237 	 * unmounted this file system while we were doing the switcheroo,
11965326Sek110237 	 * or a reopen of z_os failed then just bail out now.
11975326Sek110237 	 */
11985326Sek110237 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
11995326Sek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
12005326Sek110237 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
12015326Sek110237 		return (EIO);
12025326Sek110237 	}
12035326Sek110237 
12045326Sek110237 	/*
12055326Sek110237 	 * At this point there are no vops active, and any new vops will
12065326Sek110237 	 * fail with EIO since we have z_teardown_lock for writer (only
12075326Sek110237 	 * relavent for forced unmount).
12085326Sek110237 	 *
12095326Sek110237 	 * Release all holds on dbufs.
12105326Sek110237 	 */
12115326Sek110237 	mutex_enter(&zfsvfs->z_znodes_lock);
12125642Smaybee 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
12135642Smaybee 	    zp = list_next(&zfsvfs->z_all_znodes, zp))
12145446Sahrens 		if (zp->z_dbuf) {
12155642Smaybee 			ASSERT(ZTOV(zp)->v_count > 0);
12165642Smaybee 			zfs_znode_dmu_fini(zp);
12175326Sek110237 		}
12185326Sek110237 	mutex_exit(&zfsvfs->z_znodes_lock);
12195326Sek110237 
12205326Sek110237 	/*
12215326Sek110237 	 * If we are unmounting, set the unmounted flag and let new vops
12225326Sek110237 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
12235326Sek110237 	 * other vops will fail with EIO.
12245326Sek110237 	 */
12255326Sek110237 	if (unmounting) {
12265326Sek110237 		zfsvfs->z_unmounted = B_TRUE;
12275326Sek110237 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
12285326Sek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
12295326Sek110237 	}
12305326Sek110237 
12315326Sek110237 	/*
12325326Sek110237 	 * z_os will be NULL if there was an error in attempting to reopen
12335326Sek110237 	 * zfsvfs, so just return as the properties had already been
12345326Sek110237 	 * unregistered and cached data had been evicted before.
12355326Sek110237 	 */
12365326Sek110237 	if (zfsvfs->z_os == NULL)
12375326Sek110237 		return (0);
12385326Sek110237 
12395326Sek110237 	/*
12405326Sek110237 	 * Unregister properties.
12415326Sek110237 	 */
12425326Sek110237 	zfs_unregister_callbacks(zfsvfs);
12435326Sek110237 
12445326Sek110237 	/*
12455326Sek110237 	 * Evict cached data
12465326Sek110237 	 */
12476083Sek110237 	if (dmu_objset_evict_dbufs(zfsvfs->z_os)) {
12485429Smaybee 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
12496083Sek110237 		(void) dmu_objset_evict_dbufs(zfsvfs->z_os);
12505429Smaybee 	}
12515326Sek110237 
12525326Sek110237 	return (0);
12535326Sek110237 }
12545326Sek110237 
1255789Sahrens /*ARGSUSED*/
1256789Sahrens static int
1257789Sahrens zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1258789Sahrens {
1259789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
12605326Sek110237 	objset_t *os;
1261789Sahrens 	int ret;
1262789Sahrens 
12634543Smarks 	ret = secpolicy_fs_unmount(cr, vfsp);
12644543Smarks 	if (ret) {
12654543Smarks 		ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
12664543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
12674543Smarks 		if (ret)
12684543Smarks 			return (ret);
12694543Smarks 	}
12701484Sek110237 
12714736Sek110237 	/*
12724736Sek110237 	 * We purge the parent filesystem's vfsp as the parent filesystem
12734736Sek110237 	 * and all of its snapshots have their vnode's v_vfsp set to the
12744736Sek110237 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
12754736Sek110237 	 * referential for non-snapshots.
12764736Sek110237 	 */
12774736Sek110237 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
12781484Sek110237 
1279789Sahrens 	/*
1280789Sahrens 	 * Unmount any snapshots mounted under .zfs before unmounting the
1281789Sahrens 	 * dataset itself.
1282789Sahrens 	 */
1283789Sahrens 	if (zfsvfs->z_ctldir != NULL &&
12844543Smarks 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1285789Sahrens 		return (ret);
12864543Smarks 	}
1287789Sahrens 
12884787Sahrens 	if (!(fflag & MS_FORCE)) {
12894480Sgw25295 		/*
12904787Sahrens 		 * Check the number of active vnodes in the file system.
12914787Sahrens 		 * Our count is maintained in the vfs structure, but the
12924787Sahrens 		 * number is off by 1 to indicate a hold on the vfs
12934787Sahrens 		 * structure itself.
12944787Sahrens 		 *
12954787Sahrens 		 * The '.zfs' directory maintains a reference of its
12964787Sahrens 		 * own, and any active references underneath are
12974787Sahrens 		 * reflected in the vnode count.
1298789Sahrens 		 */
12994787Sahrens 		if (zfsvfs->z_ctldir == NULL) {
13004787Sahrens 			if (vfsp->vfs_count > 1)
13014787Sahrens 				return (EBUSY);
13024787Sahrens 		} else {
13034787Sahrens 			if (vfsp->vfs_count > 2 ||
13045326Sek110237 			    zfsvfs->z_ctldir->v_count > 1)
13054787Sahrens 				return (EBUSY);
1306789Sahrens 		}
1307789Sahrens 	}
1308789Sahrens 
1309789Sahrens 	vfsp->vfs_flag |= VFS_UNMOUNTED;
13104787Sahrens 
13115326Sek110237 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
13125326Sek110237 	os = zfsvfs->z_os;
13134787Sahrens 
13144787Sahrens 	/*
13155326Sek110237 	 * z_os will be NULL if there was an error in
13165326Sek110237 	 * attempting to reopen zfsvfs.
13174787Sahrens 	 */
13185326Sek110237 	if (os != NULL) {
13195326Sek110237 		/*
13205326Sek110237 		 * Unset the objset user_ptr.
13215326Sek110237 		 */
13225326Sek110237 		mutex_enter(&os->os->os_user_ptr_lock);
13235326Sek110237 		dmu_objset_set_user(os, NULL);
13245326Sek110237 		mutex_exit(&os->os->os_user_ptr_lock);
13254787Sahrens 
13265326Sek110237 		/*
13276689Smaybee 		 * Finally release the objset
13285326Sek110237 		 */
13295326Sek110237 		dmu_objset_close(os);
13304787Sahrens 	}
13314787Sahrens 
13324787Sahrens 	/*
13334787Sahrens 	 * We can now safely destroy the '.zfs' directory node.
13344787Sahrens 	 */
13354787Sahrens 	if (zfsvfs->z_ctldir != NULL)
13364787Sahrens 		zfsctl_destroy(zfsvfs);
1337789Sahrens 
1338789Sahrens 	return (0);
1339789Sahrens }
1340789Sahrens 
1341789Sahrens static int
1342789Sahrens zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1343789Sahrens {
1344789Sahrens 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1345789Sahrens 	znode_t		*zp;
1346789Sahrens 	uint64_t	object = 0;
1347789Sahrens 	uint64_t	fid_gen = 0;
1348789Sahrens 	uint64_t	gen_mask;
1349789Sahrens 	uint64_t	zp_gen;
1350789Sahrens 	int 		i, err;
1351789Sahrens 
1352789Sahrens 	*vpp = NULL;
1353789Sahrens 
1354789Sahrens 	ZFS_ENTER(zfsvfs);
1355789Sahrens 
1356789Sahrens 	if (fidp->fid_len == LONG_FID_LEN) {
1357789Sahrens 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1358789Sahrens 		uint64_t	objsetid = 0;
1359789Sahrens 		uint64_t	setgen = 0;
1360789Sahrens 
1361789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1362789Sahrens 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1363789Sahrens 
1364789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1365789Sahrens 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1366789Sahrens 
1367789Sahrens 		ZFS_EXIT(zfsvfs);
1368789Sahrens 
1369789Sahrens 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1370789Sahrens 		if (err)
1371789Sahrens 			return (EINVAL);
1372789Sahrens 		ZFS_ENTER(zfsvfs);
1373789Sahrens 	}
1374789Sahrens 
1375789Sahrens 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1376789Sahrens 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1377789Sahrens 
1378789Sahrens 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1379789Sahrens 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1380789Sahrens 
1381789Sahrens 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1382789Sahrens 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1383789Sahrens 	} else {
1384789Sahrens 		ZFS_EXIT(zfsvfs);
1385789Sahrens 		return (EINVAL);
1386789Sahrens 	}
1387789Sahrens 
1388789Sahrens 	/* A zero fid_gen means we are in the .zfs control directories */
1389789Sahrens 	if (fid_gen == 0 &&
1390789Sahrens 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1391789Sahrens 		*vpp = zfsvfs->z_ctldir;
1392789Sahrens 		ASSERT(*vpp != NULL);
1393789Sahrens 		if (object == ZFSCTL_INO_SNAPDIR) {
1394789Sahrens 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
13955331Samw 			    0, NULL, NULL, NULL, NULL, NULL) == 0);
1396789Sahrens 		} else {
1397789Sahrens 			VN_HOLD(*vpp);
1398789Sahrens 		}
1399789Sahrens 		ZFS_EXIT(zfsvfs);
1400789Sahrens 		return (0);
1401789Sahrens 	}
1402789Sahrens 
1403789Sahrens 	gen_mask = -1ULL >> (64 - 8 * i);
1404789Sahrens 
1405789Sahrens 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1406789Sahrens 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1407789Sahrens 		ZFS_EXIT(zfsvfs);
1408789Sahrens 		return (err);
1409789Sahrens 	}
1410789Sahrens 	zp_gen = zp->z_phys->zp_gen & gen_mask;
1411789Sahrens 	if (zp_gen == 0)
1412789Sahrens 		zp_gen = 1;
14133461Sahrens 	if (zp->z_unlinked || zp_gen != fid_gen) {
1414789Sahrens 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1415789Sahrens 		VN_RELE(ZTOV(zp));
1416789Sahrens 		ZFS_EXIT(zfsvfs);
1417789Sahrens 		return (EINVAL);
1418789Sahrens 	}
1419789Sahrens 
1420789Sahrens 	*vpp = ZTOV(zp);
1421789Sahrens 	ZFS_EXIT(zfsvfs);
1422789Sahrens 	return (0);
1423789Sahrens }
1424789Sahrens 
14255326Sek110237 /*
14265326Sek110237  * Block out VOPs and close zfsvfs_t::z_os
14275326Sek110237  *
14285326Sek110237  * Note, if successful, then we return with the 'z_teardown_lock' and
14295326Sek110237  * 'z_teardown_inactive_lock' write held.
14305326Sek110237  */
14315326Sek110237 int
14325326Sek110237 zfs_suspend_fs(zfsvfs_t *zfsvfs, char *name, int *mode)
14335326Sek110237 {
14345326Sek110237 	int error;
14355326Sek110237 
14365326Sek110237 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
14375326Sek110237 		return (error);
14385326Sek110237 
14395326Sek110237 	*mode = zfsvfs->z_os->os_mode;
14405326Sek110237 	dmu_objset_name(zfsvfs->z_os, name);
14415326Sek110237 	dmu_objset_close(zfsvfs->z_os);
14425326Sek110237 
14435326Sek110237 	return (0);
14445326Sek110237 }
14455326Sek110237 
14465326Sek110237 /*
14475326Sek110237  * Reopen zfsvfs_t::z_os and release VOPs.
14485326Sek110237  */
14495326Sek110237 int
14505326Sek110237 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname, int mode)
14515326Sek110237 {
14525326Sek110237 	int err;
14535326Sek110237 
14545326Sek110237 	ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
14555326Sek110237 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
14565326Sek110237 
14575326Sek110237 	err = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
14585326Sek110237 	if (err) {
14595326Sek110237 		zfsvfs->z_os = NULL;
14605326Sek110237 	} else {
14615326Sek110237 		znode_t *zp;
14625326Sek110237 
14635326Sek110237 		VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
14645326Sek110237 
14655326Sek110237 		/*
14665326Sek110237 		 * Attempt to re-establish all the active znodes with
14675326Sek110237 		 * their dbufs.  If a zfs_rezget() fails, then we'll let
14685326Sek110237 		 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
14695326Sek110237 		 * when they try to use their znode.
14705326Sek110237 		 */
14715326Sek110237 		mutex_enter(&zfsvfs->z_znodes_lock);
14725326Sek110237 		for (zp = list_head(&zfsvfs->z_all_znodes); zp;
14735326Sek110237 		    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
14745326Sek110237 			(void) zfs_rezget(zp);
14755326Sek110237 		}
14765326Sek110237 		mutex_exit(&zfsvfs->z_znodes_lock);
14775326Sek110237 
14785326Sek110237 	}
14795326Sek110237 
14805326Sek110237 	/* release the VOPs */
14815326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
14825326Sek110237 	rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
14835326Sek110237 
14845326Sek110237 	if (err) {
14855326Sek110237 		/*
14865326Sek110237 		 * Since we couldn't reopen zfsvfs::z_os, force
14875326Sek110237 		 * unmount this file system.
14885326Sek110237 		 */
14895326Sek110237 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
14905326Sek110237 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
14915326Sek110237 	}
14925326Sek110237 	return (err);
14935326Sek110237 }
14945326Sek110237 
1495789Sahrens static void
1496789Sahrens zfs_freevfs(vfs_t *vfsp)
1497789Sahrens {
1498789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
14994831Sgw25295 	int i;
15004831Sgw25295 
15014831Sgw25295 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
15024831Sgw25295 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1503789Sahrens 
15045331Samw 	zfs_fuid_destroy(zfsvfs);
15056083Sek110237 	zfs_freezfsvfs(zfsvfs);
1506789Sahrens 
1507789Sahrens 	atomic_add_32(&zfs_active_fs_count, -1);
1508789Sahrens }
1509789Sahrens 
1510789Sahrens /*
1511789Sahrens  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
1512789Sahrens  * so we can't safely do any non-idempotent initialization here.
1513789Sahrens  * Leave that to zfs_init() and zfs_fini(), which are called
1514789Sahrens  * from the module's _init() and _fini() entry points.
1515789Sahrens  */
1516789Sahrens /*ARGSUSED*/
1517789Sahrens static int
1518789Sahrens zfs_vfsinit(int fstype, char *name)
1519789Sahrens {
1520789Sahrens 	int error;
1521789Sahrens 
1522789Sahrens 	zfsfstype = fstype;
1523789Sahrens 
1524789Sahrens 	/*
1525789Sahrens 	 * Setup vfsops and vnodeops tables.
1526789Sahrens 	 */
1527789Sahrens 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
1528789Sahrens 	if (error != 0) {
1529789Sahrens 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
1530789Sahrens 	}
1531789Sahrens 
1532789Sahrens 	error = zfs_create_op_tables();
1533789Sahrens 	if (error) {
1534789Sahrens 		zfs_remove_op_tables();
1535789Sahrens 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
1536789Sahrens 		(void) vfs_freevfsops_by_type(zfsfstype);
1537789Sahrens 		return (error);
1538789Sahrens 	}
1539789Sahrens 
1540789Sahrens 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
1541789Sahrens 
1542789Sahrens 	/*
1543849Sbonwick 	 * Unique major number for all zfs mounts.
1544849Sbonwick 	 * If we run out of 32-bit minors, we'll getudev() another major.
1545789Sahrens 	 */
1546849Sbonwick 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
1547849Sbonwick 	zfs_minor = ZFS_MIN_MINOR;
1548789Sahrens 
1549789Sahrens 	return (0);
1550789Sahrens }
1551789Sahrens 
1552789Sahrens void
1553789Sahrens zfs_init(void)
1554789Sahrens {
1555789Sahrens 	/*
1556789Sahrens 	 * Initialize .zfs directory structures
1557789Sahrens 	 */
1558789Sahrens 	zfsctl_init();
1559789Sahrens 
1560789Sahrens 	/*
1561789Sahrens 	 * Initialize znode cache, vnode ops, etc...
1562789Sahrens 	 */
1563789Sahrens 	zfs_znode_init();
1564789Sahrens }
1565789Sahrens 
1566789Sahrens void
1567789Sahrens zfs_fini(void)
1568789Sahrens {
1569789Sahrens 	zfsctl_fini();
1570789Sahrens 	zfs_znode_fini();
1571789Sahrens }
1572789Sahrens 
1573789Sahrens int
1574789Sahrens zfs_busy(void)
1575789Sahrens {
1576789Sahrens 	return (zfs_active_fs_count != 0);
1577789Sahrens }
1578789Sahrens 
15794577Sahrens int
15804577Sahrens zfs_set_version(const char *name, uint64_t newvers)
15814577Sahrens {
15824577Sahrens 	int error;
15834577Sahrens 	objset_t *os;
15844577Sahrens 	dmu_tx_t *tx;
15854577Sahrens 	uint64_t curvers;
15864577Sahrens 
15874577Sahrens 	/*
15884577Sahrens 	 * XXX for now, require that the filesystem be unmounted.  Would
15894577Sahrens 	 * be nice to find the zfsvfs_t and just update that if
15904577Sahrens 	 * possible.
15914577Sahrens 	 */
15924577Sahrens 
15934577Sahrens 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
15944577Sahrens 		return (EINVAL);
15954577Sahrens 
15966689Smaybee 	error = dmu_objset_open(name, DMU_OST_ZFS, DS_MODE_OWNER, &os);
15974577Sahrens 	if (error)
15984577Sahrens 		return (error);
15994577Sahrens 
16004577Sahrens 	error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
16014577Sahrens 	    8, 1, &curvers);
16024577Sahrens 	if (error)
16034577Sahrens 		goto out;
16044577Sahrens 	if (newvers < curvers) {
16054577Sahrens 		error = EINVAL;
16064577Sahrens 		goto out;
16074577Sahrens 	}
16084577Sahrens 
16094577Sahrens 	tx = dmu_tx_create(os);
16104577Sahrens 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 0, ZPL_VERSION_STR);
16114577Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
16124577Sahrens 	if (error) {
16134577Sahrens 		dmu_tx_abort(tx);
16144577Sahrens 		goto out;
16154577Sahrens 	}
16164577Sahrens 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1,
16174577Sahrens 	    &newvers, tx);
16184577Sahrens 
16194577Sahrens 	spa_history_internal_log(LOG_DS_UPGRADE,
16204577Sahrens 	    dmu_objset_spa(os), tx, CRED(),
16214577Sahrens 	    "oldver=%llu newver=%llu dataset = %llu", curvers, newvers,
16224577Sahrens 	    dmu_objset_id(os));
16234577Sahrens 	dmu_tx_commit(tx);
16244577Sahrens 
16254577Sahrens out:
16264577Sahrens 	dmu_objset_close(os);
16274577Sahrens 	return (error);
16284577Sahrens }
16294577Sahrens 
16305498Stimh /*
16315498Stimh  * Read a property stored within the master node.
16325498Stimh  */
16335498Stimh int
16345498Stimh zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
16355498Stimh {
16365498Stimh 	const char *pname;
16377184Stimh 	int error = ENOENT;
16385498Stimh 
16395498Stimh 	/*
16405498Stimh 	 * Look up the file system's value for the property.  For the
16415498Stimh 	 * version property, we look up a slightly different string.
16425498Stimh 	 */
16435498Stimh 	if (prop == ZFS_PROP_VERSION)
16445498Stimh 		pname = ZPL_VERSION_STR;
16455498Stimh 	else
16465498Stimh 		pname = zfs_prop_to_name(prop);
16475498Stimh 
16487184Stimh 	if (os != NULL)
16497184Stimh 		error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
16505498Stimh 
16516404Smaybee 	if (error == ENOENT) {
16525498Stimh 		/* No value set, use the default value */
16535498Stimh 		switch (prop) {
16546404Smaybee 		case ZFS_PROP_VERSION:
16556404Smaybee 			*value = ZPL_VERSION;
16566404Smaybee 			break;
16575498Stimh 		case ZFS_PROP_NORMALIZE:
16585498Stimh 		case ZFS_PROP_UTF8ONLY:
16595498Stimh 			*value = 0;
16605498Stimh 			break;
16615498Stimh 		case ZFS_PROP_CASE:
16625498Stimh 			*value = ZFS_CASE_SENSITIVE;
16635498Stimh 			break;
16645498Stimh 		default:
16656404Smaybee 			return (error);
16665498Stimh 		}
16676404Smaybee 		error = 0;
16685498Stimh 	}
16696404Smaybee 	return (error);
16705498Stimh }
16715498Stimh 
1672789Sahrens static vfsdef_t vfw = {
1673789Sahrens 	VFSDEF_VERSION,
1674789Sahrens 	MNTTYPE_ZFS,
1675789Sahrens 	zfs_vfsinit,
16765331Samw 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS|
16775331Samw 	    VSW_XID,
1678789Sahrens 	&zfs_mntopts
1679789Sahrens };
1680789Sahrens 
1681789Sahrens struct modlfs zfs_modlfs = {
16824577Sahrens 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
1683789Sahrens };
1684