xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 5326:6752aa2bd5bc)
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 /*
223461Sahrens  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
263246Sck153898 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens #include <sys/types.h>
29789Sahrens #include <sys/param.h>
30789Sahrens #include <sys/systm.h>
31789Sahrens #include <sys/sysmacros.h>
32789Sahrens #include <sys/kmem.h>
33789Sahrens #include <sys/pathname.h>
34789Sahrens #include <sys/vnode.h>
35789Sahrens #include <sys/vfs.h>
363898Srsb #include <sys/vfs_opreg.h>
37789Sahrens #include <sys/mntent.h>
38789Sahrens #include <sys/mount.h>
39789Sahrens #include <sys/cmn_err.h>
40789Sahrens #include "fs/fs_subr.h"
41789Sahrens #include <sys/zfs_znode.h>
423461Sahrens #include <sys/zfs_dir.h>
43789Sahrens #include <sys/zil.h>
44789Sahrens #include <sys/fs/zfs.h>
45789Sahrens #include <sys/dmu.h>
46789Sahrens #include <sys/dsl_prop.h>
473912Slling #include <sys/dsl_dataset.h>
484543Smarks #include <sys/dsl_deleg.h>
49789Sahrens #include <sys/spa.h>
50789Sahrens #include <sys/zap.h>
51789Sahrens #include <sys/varargs.h>
52789Sahrens #include <sys/policy.h>
53789Sahrens #include <sys/atomic.h>
54789Sahrens #include <sys/mkdev.h>
55789Sahrens #include <sys/modctl.h>
564543Smarks #include <sys/refstr.h>
57789Sahrens #include <sys/zfs_ioctl.h>
58789Sahrens #include <sys/zfs_ctldir.h>
591544Seschrock #include <sys/bootconf.h>
60849Sbonwick #include <sys/sunddi.h>
611484Sek110237 #include <sys/dnlc.h>
62*5326Sek110237 #include <sys/dmu_objset.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 
332789Sahrens static void
333789Sahrens snapdir_changed_cb(void *arg, uint64_t newval)
334789Sahrens {
335789Sahrens 	zfsvfs_t *zfsvfs = arg;
336789Sahrens 
337789Sahrens 	zfsvfs->z_show_ctldir = newval;
338789Sahrens }
339789Sahrens 
340789Sahrens static void
341789Sahrens acl_mode_changed_cb(void *arg, uint64_t newval)
342789Sahrens {
343789Sahrens 	zfsvfs_t *zfsvfs = arg;
344789Sahrens 
345789Sahrens 	zfsvfs->z_acl_mode = newval;
346789Sahrens }
347789Sahrens 
348789Sahrens static void
349789Sahrens acl_inherit_changed_cb(void *arg, uint64_t newval)
350789Sahrens {
351789Sahrens 	zfsvfs_t *zfsvfs = arg;
352789Sahrens 
353789Sahrens 	zfsvfs->z_acl_inherit = newval;
354789Sahrens }
355789Sahrens 
3561544Seschrock static int
3571544Seschrock zfs_register_callbacks(vfs_t *vfsp)
3581544Seschrock {
3591544Seschrock 	struct dsl_dataset *ds = NULL;
3601544Seschrock 	objset_t *os = NULL;
3611544Seschrock 	zfsvfs_t *zfsvfs = NULL;
3623265Sahrens 	int readonly, do_readonly = FALSE;
3633265Sahrens 	int setuid, do_setuid = FALSE;
3643265Sahrens 	int exec, do_exec = FALSE;
3653265Sahrens 	int devices, do_devices = FALSE;
3663265Sahrens 	int xattr, do_xattr = FALSE;
3674596Slling 	int atime, do_atime = FALSE;
3681544Seschrock 	int error = 0;
3691544Seschrock 
3701544Seschrock 	ASSERT(vfsp);
3711544Seschrock 	zfsvfs = vfsp->vfs_data;
3721544Seschrock 	ASSERT(zfsvfs);
3731544Seschrock 	os = zfsvfs->z_os;
3741544Seschrock 
3751544Seschrock 	/*
3761544Seschrock 	 * The act of registering our callbacks will destroy any mount
3771544Seschrock 	 * options we may have.  In order to enable temporary overrides
3783234Sck153898 	 * of mount options, we stash away the current values and
3791544Seschrock 	 * restore them after we register the callbacks.
3801544Seschrock 	 */
3811544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
3821544Seschrock 		readonly = B_TRUE;
3831544Seschrock 		do_readonly = B_TRUE;
3841544Seschrock 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
3851544Seschrock 		readonly = B_FALSE;
3861544Seschrock 		do_readonly = B_TRUE;
3871544Seschrock 	}
3881544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
3891544Seschrock 		devices = B_FALSE;
3901544Seschrock 		setuid = B_FALSE;
3911544Seschrock 		do_devices = B_TRUE;
3921544Seschrock 		do_setuid = B_TRUE;
3931544Seschrock 	} else {
3941544Seschrock 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
3951544Seschrock 			devices = B_FALSE;
3961544Seschrock 			do_devices = B_TRUE;
3973912Slling 		} else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
3981544Seschrock 			devices = B_TRUE;
3991544Seschrock 			do_devices = B_TRUE;
4001544Seschrock 		}
4011544Seschrock 
4021544Seschrock 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
4031544Seschrock 			setuid = B_FALSE;
4041544Seschrock 			do_setuid = B_TRUE;
4051544Seschrock 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
4061544Seschrock 			setuid = B_TRUE;
4071544Seschrock 			do_setuid = B_TRUE;
4081544Seschrock 		}
4091544Seschrock 	}
4101544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
4111544Seschrock 		exec = B_FALSE;
4121544Seschrock 		do_exec = B_TRUE;
4131544Seschrock 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
4141544Seschrock 		exec = B_TRUE;
4151544Seschrock 		do_exec = B_TRUE;
4161544Seschrock 	}
4173234Sck153898 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
4183234Sck153898 		xattr = B_FALSE;
4193234Sck153898 		do_xattr = B_TRUE;
4203234Sck153898 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
4213234Sck153898 		xattr = B_TRUE;
4223234Sck153898 		do_xattr = B_TRUE;
4233234Sck153898 	}
4244596Slling 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
4254596Slling 		atime = B_FALSE;
4264596Slling 		do_atime = B_TRUE;
4274596Slling 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
4284596Slling 		atime = B_TRUE;
4294596Slling 		do_atime = B_TRUE;
4304596Slling 	}
4311544Seschrock 
4321544Seschrock 	/*
4331544Seschrock 	 * Register property callbacks.
4341544Seschrock 	 *
4351544Seschrock 	 * It would probably be fine to just check for i/o error from
4361544Seschrock 	 * the first prop_register(), but I guess I like to go
4371544Seschrock 	 * overboard...
4381544Seschrock 	 */
4391544Seschrock 	ds = dmu_objset_ds(os);
4401544Seschrock 	error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
4411544Seschrock 	error = error ? error : dsl_prop_register(ds,
4423234Sck153898 	    "xattr", xattr_changed_cb, zfsvfs);
4433234Sck153898 	error = error ? error : dsl_prop_register(ds,
4441544Seschrock 	    "recordsize", blksz_changed_cb, zfsvfs);
4451544Seschrock 	error = error ? error : dsl_prop_register(ds,
4461544Seschrock 	    "readonly", readonly_changed_cb, zfsvfs);
4471544Seschrock 	error = error ? error : dsl_prop_register(ds,
4481544Seschrock 	    "devices", devices_changed_cb, zfsvfs);
4491544Seschrock 	error = error ? error : dsl_prop_register(ds,
4501544Seschrock 	    "setuid", setuid_changed_cb, zfsvfs);
4511544Seschrock 	error = error ? error : dsl_prop_register(ds,
4521544Seschrock 	    "exec", exec_changed_cb, zfsvfs);
4531544Seschrock 	error = error ? error : dsl_prop_register(ds,
4541544Seschrock 	    "snapdir", snapdir_changed_cb, zfsvfs);
4551544Seschrock 	error = error ? error : dsl_prop_register(ds,
4561544Seschrock 	    "aclmode", acl_mode_changed_cb, zfsvfs);
4571544Seschrock 	error = error ? error : dsl_prop_register(ds,
4581544Seschrock 	    "aclinherit", acl_inherit_changed_cb, zfsvfs);
4591544Seschrock 	if (error)
4601544Seschrock 		goto unregister;
4611544Seschrock 
4621544Seschrock 	/*
4631544Seschrock 	 * Invoke our callbacks to restore temporary mount options.
4641544Seschrock 	 */
4651544Seschrock 	if (do_readonly)
4661544Seschrock 		readonly_changed_cb(zfsvfs, readonly);
4671544Seschrock 	if (do_setuid)
4681544Seschrock 		setuid_changed_cb(zfsvfs, setuid);
4691544Seschrock 	if (do_exec)
4701544Seschrock 		exec_changed_cb(zfsvfs, exec);
4711544Seschrock 	if (do_devices)
4721544Seschrock 		devices_changed_cb(zfsvfs, devices);
4733234Sck153898 	if (do_xattr)
4743234Sck153898 		xattr_changed_cb(zfsvfs, xattr);
4754596Slling 	if (do_atime)
4764596Slling 		atime_changed_cb(zfsvfs, atime);
4771544Seschrock 
4781544Seschrock 	return (0);
4791544Seschrock 
4801544Seschrock unregister:
4811544Seschrock 	/*
4821544Seschrock 	 * We may attempt to unregister some callbacks that are not
4831544Seschrock 	 * registered, but this is OK; it will simply return ENOMSG,
4841544Seschrock 	 * which we will ignore.
4851544Seschrock 	 */
4861544Seschrock 	(void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
4873234Sck153898 	(void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
4881544Seschrock 	(void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
4891544Seschrock 	(void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
4901544Seschrock 	(void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs);
4911544Seschrock 	(void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
4921544Seschrock 	(void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
4931544Seschrock 	(void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
4941544Seschrock 	(void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
4951544Seschrock 	(void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
4961544Seschrock 	    zfsvfs);
4971544Seschrock 	return (error);
4981544Seschrock 
4991544Seschrock }
5001544Seschrock 
5011544Seschrock static int
502*5326Sek110237 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
503*5326Sek110237 {
504*5326Sek110237 	uint_t readonly;
505*5326Sek110237 	int error;
506*5326Sek110237 
507*5326Sek110237 	error = zfs_register_callbacks(zfsvfs->z_vfs);
508*5326Sek110237 	if (error)
509*5326Sek110237 		return (error);
510*5326Sek110237 
511*5326Sek110237 	/*
512*5326Sek110237 	 * Set the objset user_ptr to track its zfsvfs.
513*5326Sek110237 	 */
514*5326Sek110237 	mutex_enter(&zfsvfs->z_os->os->os_user_ptr_lock);
515*5326Sek110237 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
516*5326Sek110237 	mutex_exit(&zfsvfs->z_os->os->os_user_ptr_lock);
517*5326Sek110237 
518*5326Sek110237 	/*
519*5326Sek110237 	 * If we are not mounting (ie: online recv), then we don't
520*5326Sek110237 	 * have to worry about replaying the log as we blocked all
521*5326Sek110237 	 * operations out since we closed the ZIL.
522*5326Sek110237 	 */
523*5326Sek110237 	if (mounting) {
524*5326Sek110237 		/*
525*5326Sek110237 		 * During replay we remove the read only flag to
526*5326Sek110237 		 * allow replays to succeed.
527*5326Sek110237 		 */
528*5326Sek110237 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
529*5326Sek110237 		if (readonly != 0)
530*5326Sek110237 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
531*5326Sek110237 		else
532*5326Sek110237 			zfs_unlinked_drain(zfsvfs);
533*5326Sek110237 
534*5326Sek110237 		/*
535*5326Sek110237 		 * Parse and replay the intent log.
536*5326Sek110237 		 *
537*5326Sek110237 		 * Because of ziltest, this must be done after
538*5326Sek110237 		 * zfs_unlinked_drain().  (Further note: ziltest doesn't
539*5326Sek110237 		 * use readonly mounts, where zfs_unlinked_drain() isn't
540*5326Sek110237 		 * called.)  This is because ziltest causes spa_sync()
541*5326Sek110237 		 * to think it's committed, but actually it is not, so
542*5326Sek110237 		 * the intent log contains many txg's worth of changes.
543*5326Sek110237 		 *
544*5326Sek110237 		 * In particular, if object N is in the unlinked set in
545*5326Sek110237 		 * the last txg to actually sync, then it could be
546*5326Sek110237 		 * actually freed in a later txg and then reallocated in
547*5326Sek110237 		 * a yet later txg.  This would write a "create object
548*5326Sek110237 		 * N" record to the intent log.  Normally, this would be
549*5326Sek110237 		 * fine because the spa_sync() would have written out
550*5326Sek110237 		 * the fact that object N is free, before we could write
551*5326Sek110237 		 * the "create object N" intent log record.
552*5326Sek110237 		 *
553*5326Sek110237 		 * But when we are in ziltest mode, we advance the "open
554*5326Sek110237 		 * txg" without actually spa_sync()-ing the changes to
555*5326Sek110237 		 * disk.  So we would see that object N is still
556*5326Sek110237 		 * allocated and in the unlinked set, and there is an
557*5326Sek110237 		 * intent log record saying to allocate it.
558*5326Sek110237 		 */
559*5326Sek110237 		zil_replay(zfsvfs->z_os, zfsvfs, &zfsvfs->z_assign,
560*5326Sek110237 		    zfs_replay_vector);
561*5326Sek110237 
562*5326Sek110237 		zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
563*5326Sek110237 	}
564*5326Sek110237 
565*5326Sek110237 	if (!zil_disable)
566*5326Sek110237 		zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
567*5326Sek110237 
568*5326Sek110237 	return (0);
569*5326Sek110237 }
570*5326Sek110237 
571*5326Sek110237 static int
5721544Seschrock zfs_domount(vfs_t *vfsp, char *osname, cred_t *cr)
5731544Seschrock {
5741544Seschrock 	dev_t mount_dev;
5751544Seschrock 	uint64_t recordsize, readonly;
5761544Seschrock 	int error = 0;
5771544Seschrock 	int mode;
5781544Seschrock 	zfsvfs_t *zfsvfs;
5791544Seschrock 	znode_t *zp = NULL;
5801544Seschrock 
5811544Seschrock 	ASSERT(vfsp);
5821544Seschrock 	ASSERT(osname);
5831544Seschrock 
5841544Seschrock 	/*
5851544Seschrock 	 * Initialize the zfs-specific filesystem structure.
5861544Seschrock 	 * Should probably make this a kmem cache, shuffle fields,
5871544Seschrock 	 * and just bzero up to z_hold_mtx[].
5881544Seschrock 	 */
5891544Seschrock 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
5901544Seschrock 	zfsvfs->z_vfs = vfsp;
5911544Seschrock 	zfsvfs->z_parent = zfsvfs;
5921544Seschrock 	zfsvfs->z_assign = TXG_NOWAIT;
5931544Seschrock 	zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
5941544Seschrock 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
5951544Seschrock 
5961544Seschrock 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
5971544Seschrock 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
5981544Seschrock 	    offsetof(znode_t, z_link_node));
599*5326Sek110237 	rrw_init(&zfsvfs->z_teardown_lock);
600*5326Sek110237 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
6011544Seschrock 
6021544Seschrock 	/* Initialize the generic filesystem structure. */
6031544Seschrock 	vfsp->vfs_bcount = 0;
6041544Seschrock 	vfsp->vfs_data = NULL;
6051544Seschrock 
6061544Seschrock 	if (zfs_create_unique_device(&mount_dev) == -1) {
6071544Seschrock 		error = ENODEV;
6081544Seschrock 		goto out;
6091544Seschrock 	}
6101544Seschrock 	ASSERT(vfs_devismounted(mount_dev) == 0);
6111544Seschrock 
6121544Seschrock 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
6131544Seschrock 	    NULL))
6141544Seschrock 		goto out;
6151544Seschrock 
6161544Seschrock 	vfsp->vfs_dev = mount_dev;
6171544Seschrock 	vfsp->vfs_fstype = zfsfstype;
6181544Seschrock 	vfsp->vfs_bsize = recordsize;
6191544Seschrock 	vfsp->vfs_flag |= VFS_NOTRUNC;
6201544Seschrock 	vfsp->vfs_data = zfsvfs;
6211544Seschrock 
6221544Seschrock 	if (error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL))
6231544Seschrock 		goto out;
6241544Seschrock 
6251544Seschrock 	if (readonly)
6261544Seschrock 		mode = DS_MODE_PRIMARY | DS_MODE_READONLY;
6271544Seschrock 	else
6281544Seschrock 		mode = DS_MODE_PRIMARY;
6291544Seschrock 
6301544Seschrock 	error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
6311544Seschrock 	if (error == EROFS) {
6321544Seschrock 		mode = DS_MODE_PRIMARY | DS_MODE_READONLY;
6331544Seschrock 		error = dmu_objset_open(osname, DMU_OST_ZFS, mode,
6341544Seschrock 		    &zfsvfs->z_os);
6351544Seschrock 	}
6361544Seschrock 
6371544Seschrock 	if (error)
6381544Seschrock 		goto out;
6391544Seschrock 
6401544Seschrock 	if (error = zfs_init_fs(zfsvfs, &zp, cr))
6411544Seschrock 		goto out;
6421544Seschrock 
6431544Seschrock 	/* The call to zfs_init_fs leaves the vnode held, release it here. */
6441544Seschrock 	VN_RELE(ZTOV(zp));
6451544Seschrock 
6461544Seschrock 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
6473234Sck153898 		uint64_t xattr;
6483234Sck153898 
6491544Seschrock 		ASSERT(mode & DS_MODE_READONLY);
6501544Seschrock 		atime_changed_cb(zfsvfs, B_FALSE);
6511544Seschrock 		readonly_changed_cb(zfsvfs, B_TRUE);
6523234Sck153898 		if (error = dsl_prop_get_integer(osname, "xattr", &xattr, NULL))
6533234Sck153898 			goto out;
6543234Sck153898 		xattr_changed_cb(zfsvfs, xattr);
6551544Seschrock 		zfsvfs->z_issnap = B_TRUE;
6561544Seschrock 	} else {
657*5326Sek110237 		error = zfsvfs_setup(zfsvfs, B_TRUE);
6581544Seschrock 	}
6591544Seschrock 
6601544Seschrock 	if (!zfsvfs->z_issnap)
6611544Seschrock 		zfsctl_create(zfsvfs);
6621544Seschrock out:
6631544Seschrock 	if (error) {
6641544Seschrock 		if (zfsvfs->z_os)
6651544Seschrock 			dmu_objset_close(zfsvfs->z_os);
6664831Sgw25295 		mutex_destroy(&zfsvfs->z_znodes_lock);
6674831Sgw25295 		list_destroy(&zfsvfs->z_all_znodes);
668*5326Sek110237 		rrw_destroy(&zfsvfs->z_teardown_lock);
669*5326Sek110237 		rw_destroy(&zfsvfs->z_teardown_inactive_lock);
6701544Seschrock 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
6711544Seschrock 	} else {
6721544Seschrock 		atomic_add_32(&zfs_active_fs_count, 1);
6731544Seschrock 	}
6741544Seschrock 
6751544Seschrock 	return (error);
6761544Seschrock }
6771544Seschrock 
6781544Seschrock void
6791544Seschrock zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
6801544Seschrock {
6811544Seschrock 	objset_t *os = zfsvfs->z_os;
6821544Seschrock 	struct dsl_dataset *ds;
6831544Seschrock 
6841544Seschrock 	/*
6851544Seschrock 	 * Unregister properties.
6861544Seschrock 	 */
6871544Seschrock 	if (!dmu_objset_is_snapshot(os)) {
6881544Seschrock 		ds = dmu_objset_ds(os);
6891544Seschrock 		VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
6901544Seschrock 		    zfsvfs) == 0);
6911544Seschrock 
6923234Sck153898 		VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
6933234Sck153898 		    zfsvfs) == 0);
6943234Sck153898 
6951544Seschrock 		VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
6961544Seschrock 		    zfsvfs) == 0);
6971544Seschrock 
6981544Seschrock 		VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
6991544Seschrock 		    zfsvfs) == 0);
7001544Seschrock 
7011544Seschrock 		VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
7021544Seschrock 		    zfsvfs) == 0);
7031544Seschrock 
7041544Seschrock 		VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
7051544Seschrock 		    zfsvfs) == 0);
7061544Seschrock 
7071544Seschrock 		VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
7081544Seschrock 		    zfsvfs) == 0);
7091544Seschrock 
7101544Seschrock 		VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
7111544Seschrock 		    zfsvfs) == 0);
7121544Seschrock 
7131544Seschrock 		VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
7141544Seschrock 		    zfsvfs) == 0);
7151544Seschrock 
7161544Seschrock 		VERIFY(dsl_prop_unregister(ds, "aclinherit",
7171544Seschrock 		    acl_inherit_changed_cb, zfsvfs) == 0);
7181544Seschrock 	}
7191544Seschrock }
7201544Seschrock 
7213912Slling /*
7223912Slling  * Convert a decimal digit string to a uint64_t integer.
7233912Slling  */
7243912Slling static int
7253912Slling str_to_uint64(char *str, uint64_t *objnum)
7263912Slling {
7273912Slling 	uint64_t num = 0;
7283912Slling 
7293912Slling 	while (*str) {
7303912Slling 		if (*str < '0' || *str > '9')
7313912Slling 			return (EINVAL);
7323912Slling 
7333912Slling 		num = num*10 + *str++ - '0';
7343912Slling 	}
7353912Slling 
7363912Slling 	*objnum = num;
7373912Slling 	return (0);
7383912Slling }
7393912Slling 
7403912Slling /*
7413912Slling  * The boot path passed from the boot loader is in the form of
7423912Slling  * "rootpool-name/root-filesystem-object-number'. Convert this
7433912Slling  * string to a dataset name: "rootpool-name/root-filesystem-name".
7443912Slling  */
7453912Slling static int
7463912Slling parse_bootpath(char *bpath, char *outpath)
7473912Slling {
7483912Slling 	char *slashp;
7493912Slling 	uint64_t objnum;
7503912Slling 	int error;
7513912Slling 
7523912Slling 	if (*bpath == 0 || *bpath == '/')
7533912Slling 		return (EINVAL);
7543912Slling 
7553912Slling 	slashp = strchr(bpath, '/');
7563912Slling 
7573912Slling 	/* if no '/', just return the pool name */
7583912Slling 	if (slashp == NULL) {
7593912Slling 		(void) strcpy(outpath, bpath);
7603912Slling 		return (0);
7613912Slling 	}
7623912Slling 
7633912Slling 	if (error = str_to_uint64(slashp+1, &objnum))
7643912Slling 		return (error);
7653912Slling 
7663912Slling 	*slashp = '\0';
7673912Slling 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
7683912Slling 	*slashp = '/';
7693912Slling 
7703912Slling 	return (error);
7713912Slling }
7723912Slling 
7731544Seschrock static int
7741544Seschrock zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
7751544Seschrock {
7761544Seschrock 	int error = 0;
7771544Seschrock 	int ret = 0;
7781544Seschrock 	static int zfsrootdone = 0;
7791544Seschrock 	zfsvfs_t *zfsvfs = NULL;
7801544Seschrock 	znode_t *zp = NULL;
7811544Seschrock 	vnode_t *vp = NULL;
7823912Slling 	char *zfs_bootpath;
7831544Seschrock 
7841544Seschrock 	ASSERT(vfsp);
7851544Seschrock 
7861544Seschrock 	/*
7873912Slling 	 * The filesystem that we mount as root is defined in the
7883912Slling 	 * "zfs-bootfs" property.
7891544Seschrock 	 */
7901544Seschrock 	if (why == ROOT_INIT) {
7911544Seschrock 		if (zfsrootdone++)
7921544Seschrock 			return (EBUSY);
7931544Seschrock 
7943912Slling 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
7953912Slling 		    DDI_PROP_DONTPASS, "zfs-bootfs", &zfs_bootpath) !=
7963912Slling 		    DDI_SUCCESS)
7973912Slling 			return (EIO);
7983912Slling 
7993912Slling 		error = parse_bootpath(zfs_bootpath, rootfs.bo_name);
8003912Slling 		ddi_prop_free(zfs_bootpath);
8013912Slling 
8023912Slling 		if (error)
8033912Slling 			return (error);
8041544Seschrock 
8051544Seschrock 		if (error = vfs_lock(vfsp))
8061544Seschrock 			return (error);
8071544Seschrock 
8083912Slling 		if (error = zfs_domount(vfsp, rootfs.bo_name, CRED()))
8091544Seschrock 			goto out;
8101544Seschrock 
8111544Seschrock 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
8121544Seschrock 		ASSERT(zfsvfs);
8131544Seschrock 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp))
8141544Seschrock 			goto out;
8151544Seschrock 
8161544Seschrock 		vp = ZTOV(zp);
8171544Seschrock 		mutex_enter(&vp->v_lock);
8181544Seschrock 		vp->v_flag |= VROOT;
8191544Seschrock 		mutex_exit(&vp->v_lock);
8201544Seschrock 		rootvp = vp;
8211544Seschrock 
8221544Seschrock 		/*
8231544Seschrock 		 * The zfs_zget call above returns with a hold on vp, we release
8241544Seschrock 		 * it here.
8251544Seschrock 		 */
8261544Seschrock 		VN_RELE(vp);
8271544Seschrock 
8281544Seschrock 		/*
8291544Seschrock 		 * Mount root as readonly initially, it will be remouted
8301544Seschrock 		 * read/write by /lib/svc/method/fs-usr.
8311544Seschrock 		 */
8321544Seschrock 		readonly_changed_cb(vfsp->vfs_data, B_TRUE);
8331544Seschrock 		vfs_add((struct vnode *)0, vfsp,
8341544Seschrock 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
8351544Seschrock out:
8361544Seschrock 		vfs_unlock(vfsp);
8371544Seschrock 		ret = (error) ? error : 0;
8381544Seschrock 		return (ret);
8391544Seschrock 	} else if (why == ROOT_REMOUNT) {
8401544Seschrock 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
8411544Seschrock 		vfsp->vfs_flag |= VFS_REMOUNT;
8424596Slling 
8434596Slling 		/* refresh mount options */
8444596Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
8454596Slling 		return (zfs_register_callbacks(vfsp));
8464596Slling 
8471544Seschrock 	} else if (why == ROOT_UNMOUNT) {
8481544Seschrock 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
8491544Seschrock 		(void) zfs_sync(vfsp, 0, 0);
8501544Seschrock 		return (0);
8511544Seschrock 	}
8521544Seschrock 
8531544Seschrock 	/*
8541544Seschrock 	 * if "why" is equal to anything else other than ROOT_INIT,
8551544Seschrock 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
8561544Seschrock 	 */
8571544Seschrock 	return (ENOTSUP);
8581544Seschrock }
8591544Seschrock 
860789Sahrens /*ARGSUSED*/
861789Sahrens static int
862789Sahrens zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
863789Sahrens {
864789Sahrens 	char		*osname;
865789Sahrens 	pathname_t	spn;
866789Sahrens 	int		error = 0;
867789Sahrens 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
8683912Slling 	    UIO_SYSSPACE : UIO_USERSPACE;
869789Sahrens 	int		canwrite;
870789Sahrens 
871789Sahrens 	if (mvp->v_type != VDIR)
872789Sahrens 		return (ENOTDIR);
873789Sahrens 
874789Sahrens 	mutex_enter(&mvp->v_lock);
875789Sahrens 	if ((uap->flags & MS_REMOUNT) == 0 &&
876789Sahrens 	    (uap->flags & MS_OVERLAY) == 0 &&
877789Sahrens 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
878789Sahrens 		mutex_exit(&mvp->v_lock);
879789Sahrens 		return (EBUSY);
880789Sahrens 	}
881789Sahrens 	mutex_exit(&mvp->v_lock);
882789Sahrens 
883789Sahrens 	/*
884789Sahrens 	 * ZFS does not support passing unparsed data in via MS_DATA.
885789Sahrens 	 * Users should use the MS_OPTIONSTR interface; this means
886789Sahrens 	 * that all option parsing is already done and the options struct
887789Sahrens 	 * can be interrogated.
888789Sahrens 	 */
889789Sahrens 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
890789Sahrens 		return (EINVAL);
891789Sahrens 
892789Sahrens 	/*
893789Sahrens 	 * Get the objset name (the "special" mount argument).
894789Sahrens 	 */
895789Sahrens 	if (error = pn_get(uap->spec, fromspace, &spn))
896789Sahrens 		return (error);
897789Sahrens 
898789Sahrens 	osname = spn.pn_path;
899789Sahrens 
9004543Smarks 	/*
9014543Smarks 	 * Check for mount privilege?
9024543Smarks 	 *
9034543Smarks 	 * If we don't have privilege then see if
9044543Smarks 	 * we have local permission to allow it
9054543Smarks 	 */
9064543Smarks 	error = secpolicy_fs_mount(cr, mvp, vfsp);
9074543Smarks 	if (error) {
9084543Smarks 		error = dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr);
9094543Smarks 		if (error == 0) {
9104543Smarks 			vattr_t		vattr;
9114543Smarks 
9124543Smarks 			/*
9134543Smarks 			 * Make sure user is the owner of the mount point
9144543Smarks 			 * or has sufficient privileges.
9154543Smarks 			 */
9164543Smarks 
9174543Smarks 			vattr.va_mask = AT_UID;
9184543Smarks 
9194614Smarks 			if (error = VOP_GETATTR(mvp, &vattr, 0, cr)) {
9204543Smarks 				goto out;
9214543Smarks 			}
9224543Smarks 
9234543Smarks 			if (error = secpolicy_vnode_owner(cr, vattr.va_uid)) {
9244543Smarks 				goto out;
9254543Smarks 			}
9264543Smarks 
9274543Smarks 			if (error = VOP_ACCESS(mvp, VWRITE, 0, cr)) {
9284543Smarks 				goto out;
9294543Smarks 			}
9304543Smarks 
9314543Smarks 			secpolicy_fs_mount_clearopts(cr, vfsp);
9324543Smarks 		} else {
9334543Smarks 			goto out;
9344543Smarks 		}
9354543Smarks 	}
936789Sahrens 
937789Sahrens 	/*
938789Sahrens 	 * Refuse to mount a filesystem if we are in a local zone and the
939789Sahrens 	 * dataset is not visible.
940789Sahrens 	 */
941789Sahrens 	if (!INGLOBALZONE(curproc) &&
942789Sahrens 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
943789Sahrens 		error = EPERM;
944789Sahrens 		goto out;
945789Sahrens 	}
946789Sahrens 
9474596Slling 	/*
9484596Slling 	 * When doing a remount, we simply refresh our temporary properties
9494596Slling 	 * according to those options set in the current VFS options.
9504596Slling 	 */
9514596Slling 	if (uap->flags & MS_REMOUNT) {
9524596Slling 		/* refresh mount options */
9534596Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
9544596Slling 		error = zfs_register_callbacks(vfsp);
9554596Slling 		goto out;
9564596Slling 	}
9574596Slling 
9581544Seschrock 	error = zfs_domount(vfsp, osname, cr);
959789Sahrens 
960789Sahrens out:
961789Sahrens 	pn_free(&spn);
962789Sahrens 	return (error);
963789Sahrens }
964789Sahrens 
965789Sahrens static int
966789Sahrens zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
967789Sahrens {
968789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
969789Sahrens 	dev32_t d32;
9702885Sahrens 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
971789Sahrens 
972789Sahrens 	ZFS_ENTER(zfsvfs);
973789Sahrens 
9742885Sahrens 	dmu_objset_space(zfsvfs->z_os,
9752885Sahrens 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
976789Sahrens 
977789Sahrens 	/*
978789Sahrens 	 * The underlying storage pool actually uses multiple block sizes.
979789Sahrens 	 * We report the fragsize as the smallest block size we support,
980789Sahrens 	 * and we report our blocksize as the filesystem's maximum blocksize.
981789Sahrens 	 */
982789Sahrens 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
983789Sahrens 	statp->f_bsize = zfsvfs->z_max_blksz;
984789Sahrens 
985789Sahrens 	/*
986789Sahrens 	 * The following report "total" blocks of various kinds in the
987789Sahrens 	 * file system, but reported in terms of f_frsize - the
988789Sahrens 	 * "fragment" size.
989789Sahrens 	 */
990789Sahrens 
9912885Sahrens 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
9922885Sahrens 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
993789Sahrens 	statp->f_bavail = statp->f_bfree; /* no root reservation */
994789Sahrens 
995789Sahrens 	/*
996789Sahrens 	 * statvfs() should really be called statufs(), because it assumes
997789Sahrens 	 * static metadata.  ZFS doesn't preallocate files, so the best
998789Sahrens 	 * we can do is report the max that could possibly fit in f_files,
999789Sahrens 	 * and that minus the number actually used in f_ffree.
1000789Sahrens 	 * For f_ffree, report the smaller of the number of object available
1001789Sahrens 	 * and the number of blocks (each object will take at least a block).
1002789Sahrens 	 */
10032885Sahrens 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
1004789Sahrens 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
10052885Sahrens 	statp->f_files = statp->f_ffree + usedobjs;
1006789Sahrens 
1007789Sahrens 	(void) cmpldev(&d32, vfsp->vfs_dev);
1008789Sahrens 	statp->f_fsid = d32;
1009789Sahrens 
1010789Sahrens 	/*
1011789Sahrens 	 * We're a zfs filesystem.
1012789Sahrens 	 */
1013789Sahrens 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1014789Sahrens 
10151123Smarks 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1016789Sahrens 
1017789Sahrens 	statp->f_namemax = ZFS_MAXNAMELEN;
1018789Sahrens 
1019789Sahrens 	/*
1020789Sahrens 	 * We have all of 32 characters to stuff a string here.
1021789Sahrens 	 * Is there anything useful we could/should provide?
1022789Sahrens 	 */
1023789Sahrens 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
1024789Sahrens 
1025789Sahrens 	ZFS_EXIT(zfsvfs);
1026789Sahrens 	return (0);
1027789Sahrens }
1028789Sahrens 
1029789Sahrens static int
1030789Sahrens zfs_root(vfs_t *vfsp, vnode_t **vpp)
1031789Sahrens {
1032789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1033789Sahrens 	znode_t *rootzp;
1034789Sahrens 	int error;
1035789Sahrens 
1036789Sahrens 	ZFS_ENTER(zfsvfs);
1037789Sahrens 
1038789Sahrens 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1039789Sahrens 	if (error == 0)
1040789Sahrens 		*vpp = ZTOV(rootzp);
1041789Sahrens 
1042789Sahrens 	ZFS_EXIT(zfsvfs);
1043789Sahrens 	return (error);
1044789Sahrens }
1045789Sahrens 
1046*5326Sek110237 /*
1047*5326Sek110237  * Teardown the zfsvfs::z_os.
1048*5326Sek110237  *
1049*5326Sek110237  * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1050*5326Sek110237  * and 'z_teardown_inactive_lock' held.
1051*5326Sek110237  */
1052*5326Sek110237 static int
1053*5326Sek110237 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1054*5326Sek110237 {
1055*5326Sek110237 	objset_t *os = zfsvfs->z_os;
1056*5326Sek110237 	znode_t	*zp, *nextzp;
1057*5326Sek110237 	znode_t markerzp;
1058*5326Sek110237 
1059*5326Sek110237 	rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1060*5326Sek110237 
1061*5326Sek110237 	if (!unmounting) {
1062*5326Sek110237 		/*
1063*5326Sek110237 		 * We purge the parent filesystem's vfsp as the parent
1064*5326Sek110237 		 * filesystem and all of its snapshots have their vnode's
1065*5326Sek110237 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
1066*5326Sek110237 		 * 'z_parent' is self referential for non-snapshots.
1067*5326Sek110237 		 */
1068*5326Sek110237 		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1069*5326Sek110237 	}
1070*5326Sek110237 
1071*5326Sek110237 	/*
1072*5326Sek110237 	 * Close the zil. NB: Can't close the zil while zfs_inactive
1073*5326Sek110237 	 * threads are blocked as zil_close can call zfs_inactive.
1074*5326Sek110237 	 */
1075*5326Sek110237 	if (zfsvfs->z_log) {
1076*5326Sek110237 		zil_close(zfsvfs->z_log);
1077*5326Sek110237 		zfsvfs->z_log = NULL;
1078*5326Sek110237 	}
1079*5326Sek110237 
1080*5326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
1081*5326Sek110237 
1082*5326Sek110237 	/*
1083*5326Sek110237 	 * If we are not unmounting (ie: online recv) and someone already
1084*5326Sek110237 	 * unmounted this file system while we were doing the switcheroo,
1085*5326Sek110237 	 * or a reopen of z_os failed then just bail out now.
1086*5326Sek110237 	 */
1087*5326Sek110237 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1088*5326Sek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
1089*5326Sek110237 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1090*5326Sek110237 		return (EIO);
1091*5326Sek110237 	}
1092*5326Sek110237 
1093*5326Sek110237 	/*
1094*5326Sek110237 	 * At this point there are no vops active, and any new vops will
1095*5326Sek110237 	 * fail with EIO since we have z_teardown_lock for writer (only
1096*5326Sek110237 	 * relavent for forced unmount).
1097*5326Sek110237 	 *
1098*5326Sek110237 	 * Release all holds on dbufs.
1099*5326Sek110237 	 * Note, the dmu can still callback via znode_pageout_func()
1100*5326Sek110237 	 * which can zfs_znode_free() the znode.  So we lock
1101*5326Sek110237 	 * z_all_znodes; search the list for a held dbuf; drop the lock
1102*5326Sek110237 	 * (we know zp can't disappear if we hold a dbuf lock) then
1103*5326Sek110237 	 * regrab the lock and restart.
1104*5326Sek110237 	 *
1105*5326Sek110237 	 * Since we have to restart the search after finding each held dbuf,
1106*5326Sek110237 	 * we do two things to speed up searching: we insert a dummy znode
1107*5326Sek110237 	 * ('markerzp') to detect the original tail of the list, and move
1108*5326Sek110237 	 * non-held znodes to the end of the list.  Once we hit 'markerzp',
1109*5326Sek110237 	 * we know we've looked at each znode and can break out.
1110*5326Sek110237 	 */
1111*5326Sek110237 	mutex_enter(&zfsvfs->z_znodes_lock);
1112*5326Sek110237 	list_insert_tail(&zfsvfs->z_all_znodes, &markerzp);
1113*5326Sek110237 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != &markerzp;
1114*5326Sek110237 	    zp = nextzp) {
1115*5326Sek110237 		nextzp = list_next(&zfsvfs->z_all_znodes, zp);
1116*5326Sek110237 		if (zp->z_dbuf_held) {
1117*5326Sek110237 			/* dbufs should only be held when force unmounting */
1118*5326Sek110237 			zp->z_dbuf_held = 0;
1119*5326Sek110237 			mutex_exit(&zfsvfs->z_znodes_lock);
1120*5326Sek110237 			dmu_buf_rele(zp->z_dbuf, NULL);
1121*5326Sek110237 			/* Start again */
1122*5326Sek110237 			mutex_enter(&zfsvfs->z_znodes_lock);
1123*5326Sek110237 			nextzp = list_head(&zfsvfs->z_all_znodes);
1124*5326Sek110237 		} else {
1125*5326Sek110237 			list_remove(&zfsvfs->z_all_znodes, zp);
1126*5326Sek110237 			list_insert_tail(&zfsvfs->z_all_znodes, zp);
1127*5326Sek110237 		}
1128*5326Sek110237 	}
1129*5326Sek110237 	list_remove(&zfsvfs->z_all_znodes, &markerzp);
1130*5326Sek110237 	mutex_exit(&zfsvfs->z_znodes_lock);
1131*5326Sek110237 
1132*5326Sek110237 	/*
1133*5326Sek110237 	 * If we are unmounting, set the unmounted flag and let new vops
1134*5326Sek110237 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
1135*5326Sek110237 	 * other vops will fail with EIO.
1136*5326Sek110237 	 */
1137*5326Sek110237 	if (unmounting) {
1138*5326Sek110237 		zfsvfs->z_unmounted = B_TRUE;
1139*5326Sek110237 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1140*5326Sek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
1141*5326Sek110237 	}
1142*5326Sek110237 
1143*5326Sek110237 	/*
1144*5326Sek110237 	 * z_os will be NULL if there was an error in attempting to reopen
1145*5326Sek110237 	 * zfsvfs, so just return as the properties had already been
1146*5326Sek110237 	 * unregistered and cached data had been evicted before.
1147*5326Sek110237 	 */
1148*5326Sek110237 	if (zfsvfs->z_os == NULL)
1149*5326Sek110237 		return (0);
1150*5326Sek110237 
1151*5326Sek110237 	/*
1152*5326Sek110237 	 * Unregister properties.
1153*5326Sek110237 	 */
1154*5326Sek110237 	zfs_unregister_callbacks(zfsvfs);
1155*5326Sek110237 
1156*5326Sek110237 	/*
1157*5326Sek110237 	 * Evict cached data
1158*5326Sek110237 	 */
1159*5326Sek110237 	(void) dmu_objset_evict_dbufs(os);
1160*5326Sek110237 
1161*5326Sek110237 	return (0);
1162*5326Sek110237 }
1163*5326Sek110237 
1164789Sahrens /*ARGSUSED*/
1165789Sahrens static int
1166789Sahrens zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1167789Sahrens {
1168789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1169*5326Sek110237 	objset_t *os;
1170789Sahrens 	int ret;
1171789Sahrens 
11724543Smarks 	ret = secpolicy_fs_unmount(cr, vfsp);
11734543Smarks 	if (ret) {
11744543Smarks 		ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
11754543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
11764543Smarks 		if (ret)
11774543Smarks 			return (ret);
11784543Smarks 	}
11791484Sek110237 
11804736Sek110237 	/*
11814736Sek110237 	 * We purge the parent filesystem's vfsp as the parent filesystem
11824736Sek110237 	 * and all of its snapshots have their vnode's v_vfsp set to the
11834736Sek110237 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
11844736Sek110237 	 * referential for non-snapshots.
11854736Sek110237 	 */
11864736Sek110237 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
11871484Sek110237 
1188789Sahrens 	/*
1189789Sahrens 	 * Unmount any snapshots mounted under .zfs before unmounting the
1190789Sahrens 	 * dataset itself.
1191789Sahrens 	 */
1192789Sahrens 	if (zfsvfs->z_ctldir != NULL &&
11934543Smarks 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1194789Sahrens 		return (ret);
11954543Smarks 	}
1196789Sahrens 
11974787Sahrens 	if (!(fflag & MS_FORCE)) {
11984480Sgw25295 		/*
11994787Sahrens 		 * Check the number of active vnodes in the file system.
12004787Sahrens 		 * Our count is maintained in the vfs structure, but the
12014787Sahrens 		 * number is off by 1 to indicate a hold on the vfs
12024787Sahrens 		 * structure itself.
12034787Sahrens 		 *
12044787Sahrens 		 * The '.zfs' directory maintains a reference of its
12054787Sahrens 		 * own, and any active references underneath are
12064787Sahrens 		 * reflected in the vnode count.
1207789Sahrens 		 */
12084787Sahrens 		if (zfsvfs->z_ctldir == NULL) {
12094787Sahrens 			if (vfsp->vfs_count > 1)
12104787Sahrens 				return (EBUSY);
12114787Sahrens 		} else {
12124787Sahrens 			if (vfsp->vfs_count > 2 ||
1213*5326Sek110237 			    zfsvfs->z_ctldir->v_count > 1)
12144787Sahrens 				return (EBUSY);
1215789Sahrens 		}
1216789Sahrens 	}
1217789Sahrens 
1218789Sahrens 	vfsp->vfs_flag |= VFS_UNMOUNTED;
12194787Sahrens 
1220*5326Sek110237 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1221*5326Sek110237 	os = zfsvfs->z_os;
12224787Sahrens 
12234787Sahrens 	/*
1224*5326Sek110237 	 * z_os will be NULL if there was an error in
1225*5326Sek110237 	 * attempting to reopen zfsvfs.
12264787Sahrens 	 */
1227*5326Sek110237 	if (os != NULL) {
1228*5326Sek110237 		/*
1229*5326Sek110237 		 * Unset the objset user_ptr.
1230*5326Sek110237 		 */
1231*5326Sek110237 		mutex_enter(&os->os->os_user_ptr_lock);
1232*5326Sek110237 		dmu_objset_set_user(os, NULL);
1233*5326Sek110237 		mutex_exit(&os->os->os_user_ptr_lock);
12344787Sahrens 
1235*5326Sek110237 		/*
1236*5326Sek110237 		 * Finally close the objset
1237*5326Sek110237 		 */
1238*5326Sek110237 		dmu_objset_close(os);
12394787Sahrens 	}
12404787Sahrens 
12414787Sahrens 	/*
12424787Sahrens 	 * We can now safely destroy the '.zfs' directory node.
12434787Sahrens 	 */
12444787Sahrens 	if (zfsvfs->z_ctldir != NULL)
12454787Sahrens 		zfsctl_destroy(zfsvfs);
1246789Sahrens 
1247789Sahrens 	return (0);
1248789Sahrens }
1249789Sahrens 
1250789Sahrens static int
1251789Sahrens zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1252789Sahrens {
1253789Sahrens 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1254789Sahrens 	znode_t		*zp;
1255789Sahrens 	uint64_t	object = 0;
1256789Sahrens 	uint64_t	fid_gen = 0;
1257789Sahrens 	uint64_t	gen_mask;
1258789Sahrens 	uint64_t	zp_gen;
1259789Sahrens 	int 		i, err;
1260789Sahrens 
1261789Sahrens 	*vpp = NULL;
1262789Sahrens 
1263789Sahrens 	ZFS_ENTER(zfsvfs);
1264789Sahrens 
1265789Sahrens 	if (fidp->fid_len == LONG_FID_LEN) {
1266789Sahrens 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1267789Sahrens 		uint64_t	objsetid = 0;
1268789Sahrens 		uint64_t	setgen = 0;
1269789Sahrens 
1270789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1271789Sahrens 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1272789Sahrens 
1273789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1274789Sahrens 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1275789Sahrens 
1276789Sahrens 		ZFS_EXIT(zfsvfs);
1277789Sahrens 
1278789Sahrens 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1279789Sahrens 		if (err)
1280789Sahrens 			return (EINVAL);
1281789Sahrens 		ZFS_ENTER(zfsvfs);
1282789Sahrens 	}
1283789Sahrens 
1284789Sahrens 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1285789Sahrens 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1286789Sahrens 
1287789Sahrens 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1288789Sahrens 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1289789Sahrens 
1290789Sahrens 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1291789Sahrens 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1292789Sahrens 	} else {
1293789Sahrens 		ZFS_EXIT(zfsvfs);
1294789Sahrens 		return (EINVAL);
1295789Sahrens 	}
1296789Sahrens 
1297789Sahrens 	/* A zero fid_gen means we are in the .zfs control directories */
1298789Sahrens 	if (fid_gen == 0 &&
1299789Sahrens 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1300789Sahrens 		*vpp = zfsvfs->z_ctldir;
1301789Sahrens 		ASSERT(*vpp != NULL);
1302789Sahrens 		if (object == ZFSCTL_INO_SNAPDIR) {
1303789Sahrens 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1304789Sahrens 			    0, NULL, NULL) == 0);
1305789Sahrens 		} else {
1306789Sahrens 			VN_HOLD(*vpp);
1307789Sahrens 		}
1308789Sahrens 		ZFS_EXIT(zfsvfs);
1309789Sahrens 		return (0);
1310789Sahrens 	}
1311789Sahrens 
1312789Sahrens 	gen_mask = -1ULL >> (64 - 8 * i);
1313789Sahrens 
1314789Sahrens 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1315789Sahrens 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1316789Sahrens 		ZFS_EXIT(zfsvfs);
1317789Sahrens 		return (err);
1318789Sahrens 	}
1319789Sahrens 	zp_gen = zp->z_phys->zp_gen & gen_mask;
1320789Sahrens 	if (zp_gen == 0)
1321789Sahrens 		zp_gen = 1;
13223461Sahrens 	if (zp->z_unlinked || zp_gen != fid_gen) {
1323789Sahrens 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1324789Sahrens 		VN_RELE(ZTOV(zp));
1325789Sahrens 		ZFS_EXIT(zfsvfs);
1326789Sahrens 		return (EINVAL);
1327789Sahrens 	}
1328789Sahrens 
1329789Sahrens 	*vpp = ZTOV(zp);
1330789Sahrens 	ZFS_EXIT(zfsvfs);
1331789Sahrens 	return (0);
1332789Sahrens }
1333789Sahrens 
1334*5326Sek110237 /*
1335*5326Sek110237  * Block out VOPs and close zfsvfs_t::z_os
1336*5326Sek110237  *
1337*5326Sek110237  * Note, if successful, then we return with the 'z_teardown_lock' and
1338*5326Sek110237  * 'z_teardown_inactive_lock' write held.
1339*5326Sek110237  */
1340*5326Sek110237 int
1341*5326Sek110237 zfs_suspend_fs(zfsvfs_t *zfsvfs, char *name, int *mode)
1342*5326Sek110237 {
1343*5326Sek110237 	int error;
1344*5326Sek110237 
1345*5326Sek110237 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
1346*5326Sek110237 		return (error);
1347*5326Sek110237 
1348*5326Sek110237 	*mode = zfsvfs->z_os->os_mode;
1349*5326Sek110237 	dmu_objset_name(zfsvfs->z_os, name);
1350*5326Sek110237 	dmu_objset_close(zfsvfs->z_os);
1351*5326Sek110237 
1352*5326Sek110237 	return (0);
1353*5326Sek110237 }
1354*5326Sek110237 
1355*5326Sek110237 /*
1356*5326Sek110237  * Reopen zfsvfs_t::z_os and release VOPs.
1357*5326Sek110237  */
1358*5326Sek110237 int
1359*5326Sek110237 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname, int mode)
1360*5326Sek110237 {
1361*5326Sek110237 	int err;
1362*5326Sek110237 
1363*5326Sek110237 	ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
1364*5326Sek110237 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
1365*5326Sek110237 
1366*5326Sek110237 	err = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
1367*5326Sek110237 	if (err) {
1368*5326Sek110237 		zfsvfs->z_os = NULL;
1369*5326Sek110237 	} else {
1370*5326Sek110237 		znode_t *zp;
1371*5326Sek110237 
1372*5326Sek110237 		VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
1373*5326Sek110237 
1374*5326Sek110237 		/*
1375*5326Sek110237 		 * Attempt to re-establish all the active znodes with
1376*5326Sek110237 		 * their dbufs.  If a zfs_rezget() fails, then we'll let
1377*5326Sek110237 		 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
1378*5326Sek110237 		 * when they try to use their znode.
1379*5326Sek110237 		 */
1380*5326Sek110237 		mutex_enter(&zfsvfs->z_znodes_lock);
1381*5326Sek110237 		for (zp = list_head(&zfsvfs->z_all_znodes); zp;
1382*5326Sek110237 		    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1383*5326Sek110237 			ASSERT(!zp->z_dbuf_held);
1384*5326Sek110237 			(void) zfs_rezget(zp);
1385*5326Sek110237 		}
1386*5326Sek110237 		mutex_exit(&zfsvfs->z_znodes_lock);
1387*5326Sek110237 
1388*5326Sek110237 	}
1389*5326Sek110237 
1390*5326Sek110237 	/* release the VOPs */
1391*5326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
1392*5326Sek110237 	rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1393*5326Sek110237 
1394*5326Sek110237 	if (err) {
1395*5326Sek110237 		/*
1396*5326Sek110237 		 * Since we couldn't reopen zfsvfs::z_os, force
1397*5326Sek110237 		 * unmount this file system.
1398*5326Sek110237 		 */
1399*5326Sek110237 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
1400*5326Sek110237 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
1401*5326Sek110237 	}
1402*5326Sek110237 	return (err);
1403*5326Sek110237 }
1404*5326Sek110237 
1405789Sahrens static void
1406789Sahrens zfs_freevfs(vfs_t *vfsp)
1407789Sahrens {
1408789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
14094831Sgw25295 	int i;
14104831Sgw25295 
14114831Sgw25295 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
14124831Sgw25295 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1413789Sahrens 
14144787Sahrens 	mutex_destroy(&zfsvfs->z_znodes_lock);
14154831Sgw25295 	list_destroy(&zfsvfs->z_all_znodes);
1416*5326Sek110237 	rrw_destroy(&zfsvfs->z_teardown_lock);
1417*5326Sek110237 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
1418789Sahrens 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1419789Sahrens 
1420789Sahrens 	atomic_add_32(&zfs_active_fs_count, -1);
1421789Sahrens }
1422789Sahrens 
1423789Sahrens /*
1424789Sahrens  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
1425789Sahrens  * so we can't safely do any non-idempotent initialization here.
1426789Sahrens  * Leave that to zfs_init() and zfs_fini(), which are called
1427789Sahrens  * from the module's _init() and _fini() entry points.
1428789Sahrens  */
1429789Sahrens /*ARGSUSED*/
1430789Sahrens static int
1431789Sahrens zfs_vfsinit(int fstype, char *name)
1432789Sahrens {
1433789Sahrens 	int error;
1434789Sahrens 
1435789Sahrens 	zfsfstype = fstype;
1436789Sahrens 
1437789Sahrens 	/*
1438789Sahrens 	 * Setup vfsops and vnodeops tables.
1439789Sahrens 	 */
1440789Sahrens 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
1441789Sahrens 	if (error != 0) {
1442789Sahrens 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
1443789Sahrens 	}
1444789Sahrens 
1445789Sahrens 	error = zfs_create_op_tables();
1446789Sahrens 	if (error) {
1447789Sahrens 		zfs_remove_op_tables();
1448789Sahrens 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
1449789Sahrens 		(void) vfs_freevfsops_by_type(zfsfstype);
1450789Sahrens 		return (error);
1451789Sahrens 	}
1452789Sahrens 
1453789Sahrens 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
1454789Sahrens 
1455789Sahrens 	/*
1456849Sbonwick 	 * Unique major number for all zfs mounts.
1457849Sbonwick 	 * If we run out of 32-bit minors, we'll getudev() another major.
1458789Sahrens 	 */
1459849Sbonwick 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
1460849Sbonwick 	zfs_minor = ZFS_MIN_MINOR;
1461789Sahrens 
1462789Sahrens 	return (0);
1463789Sahrens }
1464789Sahrens 
1465789Sahrens void
1466789Sahrens zfs_init(void)
1467789Sahrens {
1468789Sahrens 	/*
1469789Sahrens 	 * Initialize .zfs directory structures
1470789Sahrens 	 */
1471789Sahrens 	zfsctl_init();
1472789Sahrens 
1473789Sahrens 	/*
1474789Sahrens 	 * Initialize znode cache, vnode ops, etc...
1475789Sahrens 	 */
1476789Sahrens 	zfs_znode_init();
1477789Sahrens }
1478789Sahrens 
1479789Sahrens void
1480789Sahrens zfs_fini(void)
1481789Sahrens {
1482789Sahrens 	zfsctl_fini();
1483789Sahrens 	zfs_znode_fini();
1484789Sahrens }
1485789Sahrens 
1486789Sahrens int
1487789Sahrens zfs_busy(void)
1488789Sahrens {
1489789Sahrens 	return (zfs_active_fs_count != 0);
1490789Sahrens }
1491789Sahrens 
14924577Sahrens int
14935147Srm160521 zfs_get_version(objset_t *os, uint64_t *version)
14944577Sahrens {
14954577Sahrens 	int error;
14964577Sahrens 
14975147Srm160521 	error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1, version);
14984577Sahrens 	return (error);
14994577Sahrens }
15004577Sahrens 
15014577Sahrens int
15024577Sahrens zfs_set_version(const char *name, uint64_t newvers)
15034577Sahrens {
15044577Sahrens 	int error;
15054577Sahrens 	objset_t *os;
15064577Sahrens 	dmu_tx_t *tx;
15074577Sahrens 	uint64_t curvers;
15084577Sahrens 
15094577Sahrens 	/*
15104577Sahrens 	 * XXX for now, require that the filesystem be unmounted.  Would
15114577Sahrens 	 * be nice to find the zfsvfs_t and just update that if
15124577Sahrens 	 * possible.
15134577Sahrens 	 */
15144577Sahrens 
15154577Sahrens 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
15164577Sahrens 		return (EINVAL);
15174577Sahrens 
15184577Sahrens 	error = dmu_objset_open(name, DMU_OST_ZFS, DS_MODE_PRIMARY, &os);
15194577Sahrens 	if (error)
15204577Sahrens 		return (error);
15214577Sahrens 
15224577Sahrens 	error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
15234577Sahrens 	    8, 1, &curvers);
15244577Sahrens 	if (error)
15254577Sahrens 		goto out;
15264577Sahrens 	if (newvers < curvers) {
15274577Sahrens 		error = EINVAL;
15284577Sahrens 		goto out;
15294577Sahrens 	}
15304577Sahrens 
15314577Sahrens 	tx = dmu_tx_create(os);
15324577Sahrens 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 0, ZPL_VERSION_STR);
15334577Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
15344577Sahrens 	if (error) {
15354577Sahrens 		dmu_tx_abort(tx);
15364577Sahrens 		goto out;
15374577Sahrens 	}
15384577Sahrens 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1,
15394577Sahrens 	    &newvers, tx);
15404577Sahrens 
15414577Sahrens 	spa_history_internal_log(LOG_DS_UPGRADE,
15424577Sahrens 	    dmu_objset_spa(os), tx, CRED(),
15434577Sahrens 	    "oldver=%llu newver=%llu dataset = %llu", curvers, newvers,
15444577Sahrens 	    dmu_objset_id(os));
15454577Sahrens 	dmu_tx_commit(tx);
15464577Sahrens 
15474577Sahrens out:
15484577Sahrens 	dmu_objset_close(os);
15494577Sahrens 	return (error);
15504577Sahrens }
15514577Sahrens 
1552789Sahrens static vfsdef_t vfw = {
1553789Sahrens 	VFSDEF_VERSION,
1554789Sahrens 	MNTTYPE_ZFS,
1555789Sahrens 	zfs_vfsinit,
15561488Srsb 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS,
1557789Sahrens 	&zfs_mntopts
1558789Sahrens };
1559789Sahrens 
1560789Sahrens struct modlfs zfs_modlfs = {
15614577Sahrens 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
1562789Sahrens };
1563