xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 6083:23e77aa611b1)
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 /*
22*6083Sek110237  * Copyright 2008 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>
595331Samw #include <sys/zfs_fuid.h>
601544Seschrock #include <sys/bootconf.h>
61849Sbonwick #include <sys/sunddi.h>
621484Sek110237 #include <sys/dnlc.h>
635326Sek110237 #include <sys/dmu_objset.h>
64789Sahrens 
65789Sahrens int zfsfstype;
66789Sahrens vfsops_t *zfs_vfsops = NULL;
67849Sbonwick static major_t zfs_major;
68789Sahrens static minor_t zfs_minor;
69789Sahrens static kmutex_t	zfs_dev_mtx;
70789Sahrens 
71789Sahrens static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
72789Sahrens static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr);
731544Seschrock static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
74789Sahrens static int zfs_root(vfs_t *vfsp, vnode_t **vpp);
75789Sahrens static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp);
76789Sahrens static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp);
77789Sahrens static void zfs_freevfs(vfs_t *vfsp);
78789Sahrens 
79789Sahrens static const fs_operation_def_t zfs_vfsops_template[] = {
803898Srsb 	VFSNAME_MOUNT,		{ .vfs_mount = zfs_mount },
813898Srsb 	VFSNAME_MOUNTROOT,	{ .vfs_mountroot = zfs_mountroot },
823898Srsb 	VFSNAME_UNMOUNT,	{ .vfs_unmount = zfs_umount },
833898Srsb 	VFSNAME_ROOT,		{ .vfs_root = zfs_root },
843898Srsb 	VFSNAME_STATVFS,	{ .vfs_statvfs = zfs_statvfs },
853898Srsb 	VFSNAME_SYNC,		{ .vfs_sync = zfs_sync },
863898Srsb 	VFSNAME_VGET,		{ .vfs_vget = zfs_vget },
873898Srsb 	VFSNAME_FREEVFS,	{ .vfs_freevfs = zfs_freevfs },
883898Srsb 	NULL,			NULL
89789Sahrens };
90789Sahrens 
91789Sahrens static const fs_operation_def_t zfs_vfsops_eio_template[] = {
923898Srsb 	VFSNAME_FREEVFS,	{ .vfs_freevfs =  zfs_freevfs },
933898Srsb 	NULL,			NULL
94789Sahrens };
95789Sahrens 
96789Sahrens /*
97789Sahrens  * We need to keep a count of active fs's.
98789Sahrens  * This is necessary to prevent our module
99789Sahrens  * from being unloaded after a umount -f
100789Sahrens  */
101789Sahrens static uint32_t	zfs_active_fs_count = 0;
102789Sahrens 
103789Sahrens static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
104789Sahrens static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
1053234Sck153898 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
1063234Sck153898 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
107789Sahrens 
1083234Sck153898 /*
1094596Slling  * MO_DEFAULT is not used since the default value is determined
1104596Slling  * by the equivalent property.
1113234Sck153898  */
112789Sahrens static mntopt_t mntopts[] = {
1133234Sck153898 	{ MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL },
1143234Sck153898 	{ MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL },
1154596Slling 	{ MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL },
116789Sahrens 	{ MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
117789Sahrens };
118789Sahrens 
119789Sahrens static mntopts_t zfs_mntopts = {
120789Sahrens 	sizeof (mntopts) / sizeof (mntopt_t),
121789Sahrens 	mntopts
122789Sahrens };
123789Sahrens 
124789Sahrens /*ARGSUSED*/
125789Sahrens int
126789Sahrens zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
127789Sahrens {
128789Sahrens 	/*
129789Sahrens 	 * Data integrity is job one.  We don't want a compromised kernel
130789Sahrens 	 * writing to the storage pool, so we never sync during panic.
131789Sahrens 	 */
132789Sahrens 	if (panicstr)
133789Sahrens 		return (0);
134789Sahrens 
135789Sahrens 	/*
136789Sahrens 	 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
137789Sahrens 	 * to sync metadata, which they would otherwise cache indefinitely.
138789Sahrens 	 * Semantically, the only requirement is that the sync be initiated.
139789Sahrens 	 * The DMU syncs out txgs frequently, so there's nothing to do.
140789Sahrens 	 */
141789Sahrens 	if (flag & SYNC_ATTR)
142789Sahrens 		return (0);
143789Sahrens 
144789Sahrens 	if (vfsp != NULL) {
145789Sahrens 		/*
146789Sahrens 		 * Sync a specific filesystem.
147789Sahrens 		 */
148789Sahrens 		zfsvfs_t *zfsvfs = vfsp->vfs_data;
149789Sahrens 
150789Sahrens 		ZFS_ENTER(zfsvfs);
151789Sahrens 		if (zfsvfs->z_log != NULL)
1522638Sperrin 			zil_commit(zfsvfs->z_log, UINT64_MAX, 0);
153789Sahrens 		else
154789Sahrens 			txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
155789Sahrens 		ZFS_EXIT(zfsvfs);
156789Sahrens 	} else {
157789Sahrens 		/*
158789Sahrens 		 * Sync all ZFS filesystems.  This is what happens when you
159789Sahrens 		 * run sync(1M).  Unlike other filesystems, ZFS honors the
160789Sahrens 		 * request by waiting for all pools to commit all dirty data.
161789Sahrens 		 */
162789Sahrens 		spa_sync_allpools();
163789Sahrens 	}
164789Sahrens 
165789Sahrens 	return (0);
166789Sahrens }
167789Sahrens 
1681544Seschrock static int
1691544Seschrock zfs_create_unique_device(dev_t *dev)
1701544Seschrock {
1711544Seschrock 	major_t new_major;
1721544Seschrock 
1731544Seschrock 	do {
1741544Seschrock 		ASSERT3U(zfs_minor, <=, MAXMIN32);
1751544Seschrock 		minor_t start = zfs_minor;
1761544Seschrock 		do {
1771544Seschrock 			mutex_enter(&zfs_dev_mtx);
1781544Seschrock 			if (zfs_minor >= MAXMIN32) {
1791544Seschrock 				/*
1801544Seschrock 				 * If we're still using the real major
1811544Seschrock 				 * keep out of /dev/zfs and /dev/zvol minor
1821544Seschrock 				 * number space.  If we're using a getudev()'ed
1831544Seschrock 				 * major number, we can use all of its minors.
1841544Seschrock 				 */
1851544Seschrock 				if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
1861544Seschrock 					zfs_minor = ZFS_MIN_MINOR;
1871544Seschrock 				else
1881544Seschrock 					zfs_minor = 0;
1891544Seschrock 			} else {
1901544Seschrock 				zfs_minor++;
1911544Seschrock 			}
1921544Seschrock 			*dev = makedevice(zfs_major, zfs_minor);
1931544Seschrock 			mutex_exit(&zfs_dev_mtx);
1941544Seschrock 		} while (vfs_devismounted(*dev) && zfs_minor != start);
1951544Seschrock 		if (zfs_minor == start) {
1961544Seschrock 			/*
1971544Seschrock 			 * We are using all ~262,000 minor numbers for the
1981544Seschrock 			 * current major number.  Create a new major number.
1991544Seschrock 			 */
2001544Seschrock 			if ((new_major = getudev()) == (major_t)-1) {
2011544Seschrock 				cmn_err(CE_WARN,
2021544Seschrock 				    "zfs_mount: Can't get unique major "
2031544Seschrock 				    "device number.");
2041544Seschrock 				return (-1);
2051544Seschrock 			}
2061544Seschrock 			mutex_enter(&zfs_dev_mtx);
2071544Seschrock 			zfs_major = new_major;
2081544Seschrock 			zfs_minor = 0;
2091544Seschrock 
2101544Seschrock 			mutex_exit(&zfs_dev_mtx);
2111544Seschrock 		} else {
2121544Seschrock 			break;
2131544Seschrock 		}
2141544Seschrock 		/* CONSTANTCONDITION */
2151544Seschrock 	} while (1);
2161544Seschrock 
2171544Seschrock 	return (0);
2181544Seschrock }
2191544Seschrock 
220789Sahrens static void
221789Sahrens atime_changed_cb(void *arg, uint64_t newval)
222789Sahrens {
223789Sahrens 	zfsvfs_t *zfsvfs = arg;
224789Sahrens 
225789Sahrens 	if (newval == TRUE) {
226789Sahrens 		zfsvfs->z_atime = TRUE;
227789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
228789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
229789Sahrens 	} else {
230789Sahrens 		zfsvfs->z_atime = FALSE;
231789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
232789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
233789Sahrens 	}
234789Sahrens }
235789Sahrens 
236789Sahrens static void
2373234Sck153898 xattr_changed_cb(void *arg, uint64_t newval)
2383234Sck153898 {
2393234Sck153898 	zfsvfs_t *zfsvfs = arg;
2403234Sck153898 
2413234Sck153898 	if (newval == TRUE) {
2423234Sck153898 		/* XXX locking on vfs_flag? */
2433234Sck153898 		zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
2443234Sck153898 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
2453234Sck153898 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
2463234Sck153898 	} else {
2473234Sck153898 		/* XXX locking on vfs_flag? */
2483234Sck153898 		zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
2493234Sck153898 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
2503234Sck153898 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
2513234Sck153898 	}
2523234Sck153898 }
2533234Sck153898 
2543234Sck153898 static void
255789Sahrens blksz_changed_cb(void *arg, uint64_t newval)
256789Sahrens {
257789Sahrens 	zfsvfs_t *zfsvfs = arg;
258789Sahrens 
259789Sahrens 	if (newval < SPA_MINBLOCKSIZE ||
260789Sahrens 	    newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
261789Sahrens 		newval = SPA_MAXBLOCKSIZE;
262789Sahrens 
263789Sahrens 	zfsvfs->z_max_blksz = newval;
264789Sahrens 	zfsvfs->z_vfs->vfs_bsize = newval;
265789Sahrens }
266789Sahrens 
267789Sahrens static void
268789Sahrens readonly_changed_cb(void *arg, uint64_t newval)
269789Sahrens {
270789Sahrens 	zfsvfs_t *zfsvfs = arg;
271789Sahrens 
272789Sahrens 	if (newval) {
273789Sahrens 		/* XXX locking on vfs_flag? */
274789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
275789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
276789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
277789Sahrens 	} else {
278789Sahrens 		/* XXX locking on vfs_flag? */
279789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
280789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
281789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
282789Sahrens 	}
283789Sahrens }
284789Sahrens 
285789Sahrens static void
286789Sahrens devices_changed_cb(void *arg, uint64_t newval)
287789Sahrens {
288789Sahrens 	zfsvfs_t *zfsvfs = arg;
289789Sahrens 
290789Sahrens 	if (newval == FALSE) {
291789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
292789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
293789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
294789Sahrens 	} else {
295789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
296789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
297789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
298789Sahrens 	}
299789Sahrens }
300789Sahrens 
301789Sahrens static void
302789Sahrens setuid_changed_cb(void *arg, uint64_t newval)
303789Sahrens {
304789Sahrens 	zfsvfs_t *zfsvfs = arg;
305789Sahrens 
306789Sahrens 	if (newval == FALSE) {
307789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
308789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
309789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
310789Sahrens 	} else {
311789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
312789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
313789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
314789Sahrens 	}
315789Sahrens }
316789Sahrens 
317789Sahrens static void
318789Sahrens exec_changed_cb(void *arg, uint64_t newval)
319789Sahrens {
320789Sahrens 	zfsvfs_t *zfsvfs = arg;
321789Sahrens 
322789Sahrens 	if (newval == FALSE) {
323789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
324789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
325789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
326789Sahrens 	} else {
327789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
328789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
329789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
330789Sahrens 	}
331789Sahrens }
332789Sahrens 
3335331Samw /*
3345331Samw  * The nbmand mount option can be changed at mount time.
3355331Samw  * We can't allow it to be toggled on live file systems or incorrect
3365331Samw  * behavior may be seen from cifs clients
3375331Samw  *
3385331Samw  * This property isn't registered via dsl_prop_register(), but this callback
3395331Samw  * will be called when a file system is first mounted
3405331Samw  */
3415331Samw static void
3425331Samw nbmand_changed_cb(void *arg, uint64_t newval)
3435331Samw {
3445331Samw 	zfsvfs_t *zfsvfs = arg;
3455331Samw 	if (newval == FALSE) {
3465331Samw 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
3475331Samw 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
3485331Samw 	} else {
3495331Samw 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
3505331Samw 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
3515331Samw 	}
3525331Samw }
3535331Samw 
354789Sahrens static void
355789Sahrens snapdir_changed_cb(void *arg, uint64_t newval)
356789Sahrens {
357789Sahrens 	zfsvfs_t *zfsvfs = arg;
358789Sahrens 
359789Sahrens 	zfsvfs->z_show_ctldir = newval;
360789Sahrens }
361789Sahrens 
362789Sahrens static void
3635331Samw vscan_changed_cb(void *arg, uint64_t newval)
3645331Samw {
3655331Samw 	zfsvfs_t *zfsvfs = arg;
3665331Samw 
3675331Samw 	zfsvfs->z_vscan = newval;
3685331Samw }
3695331Samw 
3705331Samw static void
371789Sahrens acl_mode_changed_cb(void *arg, uint64_t newval)
372789Sahrens {
373789Sahrens 	zfsvfs_t *zfsvfs = arg;
374789Sahrens 
375789Sahrens 	zfsvfs->z_acl_mode = newval;
376789Sahrens }
377789Sahrens 
378789Sahrens static void
379789Sahrens acl_inherit_changed_cb(void *arg, uint64_t newval)
380789Sahrens {
381789Sahrens 	zfsvfs_t *zfsvfs = arg;
382789Sahrens 
383789Sahrens 	zfsvfs->z_acl_inherit = newval;
384789Sahrens }
385789Sahrens 
3861544Seschrock static int
3871544Seschrock zfs_register_callbacks(vfs_t *vfsp)
3881544Seschrock {
3891544Seschrock 	struct dsl_dataset *ds = NULL;
3901544Seschrock 	objset_t *os = NULL;
3911544Seschrock 	zfsvfs_t *zfsvfs = NULL;
3925331Samw 	uint64_t nbmand;
3935331Samw 	int readonly, do_readonly = B_FALSE;
3945331Samw 	int setuid, do_setuid = B_FALSE;
3955331Samw 	int exec, do_exec = B_FALSE;
3965331Samw 	int devices, do_devices = B_FALSE;
3975331Samw 	int xattr, do_xattr = B_FALSE;
3985331Samw 	int atime, do_atime = B_FALSE;
3991544Seschrock 	int error = 0;
4001544Seschrock 
4011544Seschrock 	ASSERT(vfsp);
4021544Seschrock 	zfsvfs = vfsp->vfs_data;
4031544Seschrock 	ASSERT(zfsvfs);
4041544Seschrock 	os = zfsvfs->z_os;
4051544Seschrock 
4061544Seschrock 	/*
4071544Seschrock 	 * The act of registering our callbacks will destroy any mount
4081544Seschrock 	 * options we may have.  In order to enable temporary overrides
4093234Sck153898 	 * of mount options, we stash away the current values and
4101544Seschrock 	 * restore them after we register the callbacks.
4111544Seschrock 	 */
4121544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
4131544Seschrock 		readonly = B_TRUE;
4141544Seschrock 		do_readonly = B_TRUE;
4151544Seschrock 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
4161544Seschrock 		readonly = B_FALSE;
4171544Seschrock 		do_readonly = B_TRUE;
4181544Seschrock 	}
4191544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
4201544Seschrock 		devices = B_FALSE;
4211544Seschrock 		setuid = B_FALSE;
4221544Seschrock 		do_devices = B_TRUE;
4231544Seschrock 		do_setuid = B_TRUE;
4241544Seschrock 	} else {
4251544Seschrock 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
4261544Seschrock 			devices = B_FALSE;
4271544Seschrock 			do_devices = B_TRUE;
4283912Slling 		} else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
4291544Seschrock 			devices = B_TRUE;
4301544Seschrock 			do_devices = B_TRUE;
4311544Seschrock 		}
4321544Seschrock 
4331544Seschrock 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
4341544Seschrock 			setuid = B_FALSE;
4351544Seschrock 			do_setuid = B_TRUE;
4361544Seschrock 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
4371544Seschrock 			setuid = B_TRUE;
4381544Seschrock 			do_setuid = B_TRUE;
4391544Seschrock 		}
4401544Seschrock 	}
4411544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
4421544Seschrock 		exec = B_FALSE;
4431544Seschrock 		do_exec = B_TRUE;
4441544Seschrock 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
4451544Seschrock 		exec = B_TRUE;
4461544Seschrock 		do_exec = B_TRUE;
4471544Seschrock 	}
4483234Sck153898 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
4493234Sck153898 		xattr = B_FALSE;
4503234Sck153898 		do_xattr = B_TRUE;
4513234Sck153898 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
4523234Sck153898 		xattr = B_TRUE;
4533234Sck153898 		do_xattr = B_TRUE;
4543234Sck153898 	}
4554596Slling 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
4564596Slling 		atime = B_FALSE;
4574596Slling 		do_atime = B_TRUE;
4584596Slling 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
4594596Slling 		atime = B_TRUE;
4604596Slling 		do_atime = B_TRUE;
4614596Slling 	}
4621544Seschrock 
4631544Seschrock 	/*
4645331Samw 	 * nbmand is a special property.  It can only be changed at
4655331Samw 	 * mount time.
4665331Samw 	 *
4675331Samw 	 * This is weird, but it is documented to only be changeable
4685331Samw 	 * at mount time.
4695331Samw 	 */
4705331Samw 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
4715331Samw 		nbmand = B_FALSE;
4725331Samw 	} else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
4735331Samw 		nbmand = B_TRUE;
4745331Samw 	} else {
4755331Samw 		char osname[MAXNAMELEN];
4765331Samw 
4775331Samw 		dmu_objset_name(os, osname);
4785331Samw 		if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
4795331Samw 		    NULL))
4805331Samw 		return (error);
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 	uint_t readonly;
5615326Sek110237 	int error;
5625326Sek110237 
5635326Sek110237 	error = zfs_register_callbacks(zfsvfs->z_vfs);
5645326Sek110237 	if (error)
5655326Sek110237 		return (error);
5665326Sek110237 
5675326Sek110237 	/*
5685326Sek110237 	 * Set the objset user_ptr to track its zfsvfs.
5695326Sek110237 	 */
5705326Sek110237 	mutex_enter(&zfsvfs->z_os->os->os_user_ptr_lock);
5715326Sek110237 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
5725326Sek110237 	mutex_exit(&zfsvfs->z_os->os->os_user_ptr_lock);
5735326Sek110237 
5745326Sek110237 	/*
5755326Sek110237 	 * If we are not mounting (ie: online recv), then we don't
5765326Sek110237 	 * have to worry about replaying the log as we blocked all
5775326Sek110237 	 * operations out since we closed the ZIL.
5785326Sek110237 	 */
5795326Sek110237 	if (mounting) {
5805326Sek110237 		/*
5815326Sek110237 		 * During replay we remove the read only flag to
5825326Sek110237 		 * allow replays to succeed.
5835326Sek110237 		 */
5845326Sek110237 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
5855326Sek110237 		if (readonly != 0)
5865326Sek110237 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
5875326Sek110237 		else
5885326Sek110237 			zfs_unlinked_drain(zfsvfs);
5895326Sek110237 
5905326Sek110237 		/*
5915326Sek110237 		 * Parse and replay the intent log.
5925326Sek110237 		 *
5935326Sek110237 		 * Because of ziltest, this must be done after
5945326Sek110237 		 * zfs_unlinked_drain().  (Further note: ziltest doesn't
5955326Sek110237 		 * use readonly mounts, where zfs_unlinked_drain() isn't
5965326Sek110237 		 * called.)  This is because ziltest causes spa_sync()
5975326Sek110237 		 * to think it's committed, but actually it is not, so
5985326Sek110237 		 * the intent log contains many txg's worth of changes.
5995326Sek110237 		 *
6005326Sek110237 		 * In particular, if object N is in the unlinked set in
6015326Sek110237 		 * the last txg to actually sync, then it could be
6025326Sek110237 		 * actually freed in a later txg and then reallocated in
6035326Sek110237 		 * a yet later txg.  This would write a "create object
6045326Sek110237 		 * N" record to the intent log.  Normally, this would be
6055326Sek110237 		 * fine because the spa_sync() would have written out
6065326Sek110237 		 * the fact that object N is free, before we could write
6075326Sek110237 		 * the "create object N" intent log record.
6085326Sek110237 		 *
6095326Sek110237 		 * But when we are in ziltest mode, we advance the "open
6105326Sek110237 		 * txg" without actually spa_sync()-ing the changes to
6115326Sek110237 		 * disk.  So we would see that object N is still
6125326Sek110237 		 * allocated and in the unlinked set, and there is an
6135326Sek110237 		 * intent log record saying to allocate it.
6145326Sek110237 		 */
6155326Sek110237 		zil_replay(zfsvfs->z_os, zfsvfs, &zfsvfs->z_assign,
6165326Sek110237 		    zfs_replay_vector);
6175326Sek110237 
6185326Sek110237 		zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
6195326Sek110237 	}
6205326Sek110237 
6215326Sek110237 	if (!zil_disable)
6225326Sek110237 		zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
6235326Sek110237 
6245326Sek110237 	return (0);
6255326Sek110237 }
6265326Sek110237 
627*6083Sek110237 static void
628*6083Sek110237 zfs_freezfsvfs(zfsvfs_t *zfsvfs)
629*6083Sek110237 {
630*6083Sek110237 	mutex_destroy(&zfsvfs->z_znodes_lock);
631*6083Sek110237 	mutex_destroy(&zfsvfs->z_online_recv_lock);
632*6083Sek110237 	list_destroy(&zfsvfs->z_all_znodes);
633*6083Sek110237 	rrw_destroy(&zfsvfs->z_teardown_lock);
634*6083Sek110237 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
635*6083Sek110237 	rw_destroy(&zfsvfs->z_fuid_lock);
636*6083Sek110237 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
637*6083Sek110237 }
638*6083Sek110237 
6395326Sek110237 static int
6401544Seschrock zfs_domount(vfs_t *vfsp, char *osname, cred_t *cr)
6411544Seschrock {
6421544Seschrock 	dev_t mount_dev;
6431544Seschrock 	uint64_t recordsize, readonly;
6441544Seschrock 	int error = 0;
6451544Seschrock 	int mode;
6461544Seschrock 	zfsvfs_t *zfsvfs;
6471544Seschrock 	znode_t *zp = NULL;
6481544Seschrock 
6491544Seschrock 	ASSERT(vfsp);
6501544Seschrock 	ASSERT(osname);
6511544Seschrock 
6521544Seschrock 	/*
6531544Seschrock 	 * Initialize the zfs-specific filesystem structure.
6541544Seschrock 	 * Should probably make this a kmem cache, shuffle fields,
6551544Seschrock 	 * and just bzero up to z_hold_mtx[].
6561544Seschrock 	 */
6571544Seschrock 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
6581544Seschrock 	zfsvfs->z_vfs = vfsp;
6591544Seschrock 	zfsvfs->z_parent = zfsvfs;
6601544Seschrock 	zfsvfs->z_assign = TXG_NOWAIT;
6611544Seschrock 	zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
6621544Seschrock 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
6631544Seschrock 
6641544Seschrock 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
665*6083Sek110237 	mutex_init(&zfsvfs->z_online_recv_lock, NULL, MUTEX_DEFAULT, NULL);
6661544Seschrock 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
6671544Seschrock 	    offsetof(znode_t, z_link_node));
6685326Sek110237 	rrw_init(&zfsvfs->z_teardown_lock);
6695326Sek110237 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
6705498Stimh 	rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
6711544Seschrock 
6721544Seschrock 	/* Initialize the generic filesystem structure. */
6731544Seschrock 	vfsp->vfs_bcount = 0;
6741544Seschrock 	vfsp->vfs_data = NULL;
6751544Seschrock 
6761544Seschrock 	if (zfs_create_unique_device(&mount_dev) == -1) {
6771544Seschrock 		error = ENODEV;
6781544Seschrock 		goto out;
6791544Seschrock 	}
6801544Seschrock 	ASSERT(vfs_devismounted(mount_dev) == 0);
6811544Seschrock 
6821544Seschrock 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
6831544Seschrock 	    NULL))
6841544Seschrock 		goto out;
6851544Seschrock 
6861544Seschrock 	vfsp->vfs_dev = mount_dev;
6871544Seschrock 	vfsp->vfs_fstype = zfsfstype;
6881544Seschrock 	vfsp->vfs_bsize = recordsize;
6891544Seschrock 	vfsp->vfs_flag |= VFS_NOTRUNC;
6901544Seschrock 	vfsp->vfs_data = zfsvfs;
6911544Seschrock 
6921544Seschrock 	if (error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL))
6931544Seschrock 		goto out;
6941544Seschrock 
6951544Seschrock 	if (readonly)
6961544Seschrock 		mode = DS_MODE_PRIMARY | DS_MODE_READONLY;
6971544Seschrock 	else
6981544Seschrock 		mode = DS_MODE_PRIMARY;
6991544Seschrock 
7001544Seschrock 	error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
7011544Seschrock 	if (error == EROFS) {
7021544Seschrock 		mode = DS_MODE_PRIMARY | DS_MODE_READONLY;
7031544Seschrock 		error = dmu_objset_open(osname, DMU_OST_ZFS, mode,
7041544Seschrock 		    &zfsvfs->z_os);
7051544Seschrock 	}
7061544Seschrock 
7071544Seschrock 	if (error)
7081544Seschrock 		goto out;
7091544Seschrock 
7101544Seschrock 	if (error = zfs_init_fs(zfsvfs, &zp, cr))
7111544Seschrock 		goto out;
7121544Seschrock 
7131544Seschrock 	/* The call to zfs_init_fs leaves the vnode held, release it here. */
7141544Seschrock 	VN_RELE(ZTOV(zp));
7151544Seschrock 
7165331Samw 	/*
7175331Samw 	 * Set features for file system.
7185331Samw 	 */
7195331Samw 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
7205331Samw 	if (zfsvfs->z_use_fuids) {
7215331Samw 		vfs_set_feature(vfsp, VFSFT_XVATTR);
7225331Samw 		vfs_set_feature(vfsp, VFSFT_ACEMASKONACCESS);
7235331Samw 		vfs_set_feature(vfsp, VFSFT_ACLONCREATE);
7245331Samw 	}
7255498Stimh 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
7265498Stimh 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
7275498Stimh 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
7285498Stimh 		vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
7295498Stimh 	} else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
7305498Stimh 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
7315498Stimh 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
7325498Stimh 	}
7335331Samw 
7341544Seschrock 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
7355331Samw 		uint64_t pval;
7363234Sck153898 
7371544Seschrock 		ASSERT(mode & DS_MODE_READONLY);
7381544Seschrock 		atime_changed_cb(zfsvfs, B_FALSE);
7391544Seschrock 		readonly_changed_cb(zfsvfs, B_TRUE);
7405331Samw 		if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
7413234Sck153898 			goto out;
7425331Samw 		xattr_changed_cb(zfsvfs, pval);
7431544Seschrock 		zfsvfs->z_issnap = B_TRUE;
7441544Seschrock 	} else {
7455326Sek110237 		error = zfsvfs_setup(zfsvfs, B_TRUE);
7461544Seschrock 	}
7471544Seschrock 
7481544Seschrock 	if (!zfsvfs->z_issnap)
7491544Seschrock 		zfsctl_create(zfsvfs);
7501544Seschrock out:
7511544Seschrock 	if (error) {
7521544Seschrock 		if (zfsvfs->z_os)
7531544Seschrock 			dmu_objset_close(zfsvfs->z_os);
754*6083Sek110237 		zfs_freezfsvfs(zfsvfs);
7551544Seschrock 	} else {
7561544Seschrock 		atomic_add_32(&zfs_active_fs_count, 1);
7571544Seschrock 	}
7581544Seschrock 
7591544Seschrock 	return (error);
7601544Seschrock }
7611544Seschrock 
7621544Seschrock void
7631544Seschrock zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
7641544Seschrock {
7651544Seschrock 	objset_t *os = zfsvfs->z_os;
7661544Seschrock 	struct dsl_dataset *ds;
7671544Seschrock 
7681544Seschrock 	/*
7691544Seschrock 	 * Unregister properties.
7701544Seschrock 	 */
7711544Seschrock 	if (!dmu_objset_is_snapshot(os)) {
7721544Seschrock 		ds = dmu_objset_ds(os);
7731544Seschrock 		VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
7741544Seschrock 		    zfsvfs) == 0);
7751544Seschrock 
7763234Sck153898 		VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
7773234Sck153898 		    zfsvfs) == 0);
7783234Sck153898 
7791544Seschrock 		VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
7801544Seschrock 		    zfsvfs) == 0);
7811544Seschrock 
7821544Seschrock 		VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
7831544Seschrock 		    zfsvfs) == 0);
7841544Seschrock 
7851544Seschrock 		VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
7861544Seschrock 		    zfsvfs) == 0);
7871544Seschrock 
7881544Seschrock 		VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
7891544Seschrock 		    zfsvfs) == 0);
7901544Seschrock 
7911544Seschrock 		VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
7921544Seschrock 		    zfsvfs) == 0);
7931544Seschrock 
7941544Seschrock 		VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
7951544Seschrock 		    zfsvfs) == 0);
7961544Seschrock 
7971544Seschrock 		VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
7981544Seschrock 		    zfsvfs) == 0);
7991544Seschrock 
8001544Seschrock 		VERIFY(dsl_prop_unregister(ds, "aclinherit",
8011544Seschrock 		    acl_inherit_changed_cb, zfsvfs) == 0);
8025331Samw 
8035331Samw 		VERIFY(dsl_prop_unregister(ds, "vscan",
8045331Samw 		    vscan_changed_cb, zfsvfs) == 0);
8051544Seschrock 	}
8061544Seschrock }
8071544Seschrock 
8083912Slling /*
8093912Slling  * Convert a decimal digit string to a uint64_t integer.
8103912Slling  */
8113912Slling static int
8123912Slling str_to_uint64(char *str, uint64_t *objnum)
8133912Slling {
8143912Slling 	uint64_t num = 0;
8153912Slling 
8163912Slling 	while (*str) {
8173912Slling 		if (*str < '0' || *str > '9')
8183912Slling 			return (EINVAL);
8193912Slling 
8203912Slling 		num = num*10 + *str++ - '0';
8213912Slling 	}
8223912Slling 
8233912Slling 	*objnum = num;
8243912Slling 	return (0);
8253912Slling }
8263912Slling 
8273912Slling /*
8283912Slling  * The boot path passed from the boot loader is in the form of
8293912Slling  * "rootpool-name/root-filesystem-object-number'. Convert this
8303912Slling  * string to a dataset name: "rootpool-name/root-filesystem-name".
8313912Slling  */
8323912Slling static int
8333912Slling parse_bootpath(char *bpath, char *outpath)
8343912Slling {
8353912Slling 	char *slashp;
8363912Slling 	uint64_t objnum;
8373912Slling 	int error;
8383912Slling 
8393912Slling 	if (*bpath == 0 || *bpath == '/')
8403912Slling 		return (EINVAL);
8413912Slling 
8423912Slling 	slashp = strchr(bpath, '/');
8433912Slling 
8443912Slling 	/* if no '/', just return the pool name */
8453912Slling 	if (slashp == NULL) {
8463912Slling 		(void) strcpy(outpath, bpath);
8473912Slling 		return (0);
8483912Slling 	}
8493912Slling 
8503912Slling 	if (error = str_to_uint64(slashp+1, &objnum))
8513912Slling 		return (error);
8523912Slling 
8533912Slling 	*slashp = '\0';
8543912Slling 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
8553912Slling 	*slashp = '/';
8563912Slling 
8573912Slling 	return (error);
8583912Slling }
8593912Slling 
8601544Seschrock static int
8611544Seschrock zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
8621544Seschrock {
8631544Seschrock 	int error = 0;
8641544Seschrock 	int ret = 0;
8651544Seschrock 	static int zfsrootdone = 0;
8661544Seschrock 	zfsvfs_t *zfsvfs = NULL;
8671544Seschrock 	znode_t *zp = NULL;
8681544Seschrock 	vnode_t *vp = NULL;
8693912Slling 	char *zfs_bootpath;
8705648Ssetje #if defined(_OBP)
8715648Ssetje 	int proplen;
8725648Ssetje #endif
8731544Seschrock 
8741544Seschrock 	ASSERT(vfsp);
8751544Seschrock 
8761544Seschrock 	/*
8773912Slling 	 * The filesystem that we mount as root is defined in the
8783912Slling 	 * "zfs-bootfs" property.
8791544Seschrock 	 */
8801544Seschrock 	if (why == ROOT_INIT) {
8811544Seschrock 		if (zfsrootdone++)
8821544Seschrock 			return (EBUSY);
8831544Seschrock 
8845648Ssetje #if defined(_OBP)
8855648Ssetje 		proplen = BOP_GETPROPLEN(bootops, "zfs-bootfs");
8865648Ssetje 		if (proplen == 0)
8875648Ssetje 			return (EIO);
8885648Ssetje 		zfs_bootpath = kmem_zalloc(proplen, KM_SLEEP);
8895648Ssetje 		if (BOP_GETPROP(bootops, "zfs-bootfs", zfs_bootpath) == -1) {
8905648Ssetje 			kmem_free(zfs_bootpath, proplen);
8915648Ssetje 			return (EIO);
8925648Ssetje 		}
8935648Ssetje 		error = parse_bootpath(zfs_bootpath, rootfs.bo_name);
8945648Ssetje 		kmem_free(zfs_bootpath, proplen);
8955648Ssetje #else
8963912Slling 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
8973912Slling 		    DDI_PROP_DONTPASS, "zfs-bootfs", &zfs_bootpath) !=
8983912Slling 		    DDI_SUCCESS)
8993912Slling 			return (EIO);
9003912Slling 
9013912Slling 		error = parse_bootpath(zfs_bootpath, rootfs.bo_name);
9023912Slling 		ddi_prop_free(zfs_bootpath);
9035648Ssetje #endif
9043912Slling 
9053912Slling 		if (error)
9063912Slling 			return (error);
9071544Seschrock 
9081544Seschrock 		if (error = vfs_lock(vfsp))
9091544Seschrock 			return (error);
9101544Seschrock 
9113912Slling 		if (error = zfs_domount(vfsp, rootfs.bo_name, CRED()))
9121544Seschrock 			goto out;
9131544Seschrock 
9141544Seschrock 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
9151544Seschrock 		ASSERT(zfsvfs);
9161544Seschrock 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp))
9171544Seschrock 			goto out;
9181544Seschrock 
9191544Seschrock 		vp = ZTOV(zp);
9201544Seschrock 		mutex_enter(&vp->v_lock);
9211544Seschrock 		vp->v_flag |= VROOT;
9221544Seschrock 		mutex_exit(&vp->v_lock);
9231544Seschrock 		rootvp = vp;
9241544Seschrock 
9251544Seschrock 		/*
9261544Seschrock 		 * The zfs_zget call above returns with a hold on vp, we release
9271544Seschrock 		 * it here.
9281544Seschrock 		 */
9291544Seschrock 		VN_RELE(vp);
9301544Seschrock 
9311544Seschrock 		/*
9321544Seschrock 		 * Mount root as readonly initially, it will be remouted
9331544Seschrock 		 * read/write by /lib/svc/method/fs-usr.
9341544Seschrock 		 */
9351544Seschrock 		readonly_changed_cb(vfsp->vfs_data, B_TRUE);
9361544Seschrock 		vfs_add((struct vnode *)0, vfsp,
9371544Seschrock 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
9381544Seschrock out:
9391544Seschrock 		vfs_unlock(vfsp);
9401544Seschrock 		ret = (error) ? error : 0;
9411544Seschrock 		return (ret);
9421544Seschrock 	} else if (why == ROOT_REMOUNT) {
9431544Seschrock 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
9441544Seschrock 		vfsp->vfs_flag |= VFS_REMOUNT;
9454596Slling 
9464596Slling 		/* refresh mount options */
9474596Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
9484596Slling 		return (zfs_register_callbacks(vfsp));
9494596Slling 
9501544Seschrock 	} else if (why == ROOT_UNMOUNT) {
9511544Seschrock 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
9521544Seschrock 		(void) zfs_sync(vfsp, 0, 0);
9531544Seschrock 		return (0);
9541544Seschrock 	}
9551544Seschrock 
9561544Seschrock 	/*
9571544Seschrock 	 * if "why" is equal to anything else other than ROOT_INIT,
9581544Seschrock 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
9591544Seschrock 	 */
9601544Seschrock 	return (ENOTSUP);
9611544Seschrock }
9621544Seschrock 
963789Sahrens /*ARGSUSED*/
964789Sahrens static int
965789Sahrens zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
966789Sahrens {
967789Sahrens 	char		*osname;
968789Sahrens 	pathname_t	spn;
969789Sahrens 	int		error = 0;
970789Sahrens 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
9713912Slling 	    UIO_SYSSPACE : UIO_USERSPACE;
972789Sahrens 	int		canwrite;
973789Sahrens 
974789Sahrens 	if (mvp->v_type != VDIR)
975789Sahrens 		return (ENOTDIR);
976789Sahrens 
977789Sahrens 	mutex_enter(&mvp->v_lock);
978789Sahrens 	if ((uap->flags & MS_REMOUNT) == 0 &&
979789Sahrens 	    (uap->flags & MS_OVERLAY) == 0 &&
980789Sahrens 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
981789Sahrens 		mutex_exit(&mvp->v_lock);
982789Sahrens 		return (EBUSY);
983789Sahrens 	}
984789Sahrens 	mutex_exit(&mvp->v_lock);
985789Sahrens 
986789Sahrens 	/*
987789Sahrens 	 * ZFS does not support passing unparsed data in via MS_DATA.
988789Sahrens 	 * Users should use the MS_OPTIONSTR interface; this means
989789Sahrens 	 * that all option parsing is already done and the options struct
990789Sahrens 	 * can be interrogated.
991789Sahrens 	 */
992789Sahrens 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
993789Sahrens 		return (EINVAL);
994789Sahrens 
995789Sahrens 	/*
996789Sahrens 	 * Get the objset name (the "special" mount argument).
997789Sahrens 	 */
998789Sahrens 	if (error = pn_get(uap->spec, fromspace, &spn))
999789Sahrens 		return (error);
1000789Sahrens 
1001789Sahrens 	osname = spn.pn_path;
1002789Sahrens 
10034543Smarks 	/*
10044543Smarks 	 * Check for mount privilege?
10054543Smarks 	 *
10064543Smarks 	 * If we don't have privilege then see if
10074543Smarks 	 * we have local permission to allow it
10084543Smarks 	 */
10094543Smarks 	error = secpolicy_fs_mount(cr, mvp, vfsp);
10104543Smarks 	if (error) {
10114543Smarks 		error = dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr);
10124543Smarks 		if (error == 0) {
10134543Smarks 			vattr_t		vattr;
10144543Smarks 
10154543Smarks 			/*
10164543Smarks 			 * Make sure user is the owner of the mount point
10174543Smarks 			 * or has sufficient privileges.
10184543Smarks 			 */
10194543Smarks 
10204543Smarks 			vattr.va_mask = AT_UID;
10214543Smarks 
10225331Samw 			if (error = VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
10234543Smarks 				goto out;
10244543Smarks 			}
10254543Smarks 
10265489Smarks 			if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 &&
10275489Smarks 			    VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) {
10285489Smarks 				error = EPERM;
10294543Smarks 				goto out;
10304543Smarks 			}
10314543Smarks 
10324543Smarks 			secpolicy_fs_mount_clearopts(cr, vfsp);
10334543Smarks 		} else {
10344543Smarks 			goto out;
10354543Smarks 		}
10364543Smarks 	}
1037789Sahrens 
1038789Sahrens 	/*
1039789Sahrens 	 * Refuse to mount a filesystem if we are in a local zone and the
1040789Sahrens 	 * dataset is not visible.
1041789Sahrens 	 */
1042789Sahrens 	if (!INGLOBALZONE(curproc) &&
1043789Sahrens 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1044789Sahrens 		error = EPERM;
1045789Sahrens 		goto out;
1046789Sahrens 	}
1047789Sahrens 
10484596Slling 	/*
10494596Slling 	 * When doing a remount, we simply refresh our temporary properties
10504596Slling 	 * according to those options set in the current VFS options.
10514596Slling 	 */
10524596Slling 	if (uap->flags & MS_REMOUNT) {
10534596Slling 		/* refresh mount options */
10544596Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
10554596Slling 		error = zfs_register_callbacks(vfsp);
10564596Slling 		goto out;
10574596Slling 	}
10584596Slling 
10591544Seschrock 	error = zfs_domount(vfsp, osname, cr);
1060789Sahrens 
1061789Sahrens out:
1062789Sahrens 	pn_free(&spn);
1063789Sahrens 	return (error);
1064789Sahrens }
1065789Sahrens 
1066789Sahrens static int
1067789Sahrens zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1068789Sahrens {
1069789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1070789Sahrens 	dev32_t d32;
10712885Sahrens 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
1072789Sahrens 
1073789Sahrens 	ZFS_ENTER(zfsvfs);
1074789Sahrens 
10752885Sahrens 	dmu_objset_space(zfsvfs->z_os,
10762885Sahrens 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
1077789Sahrens 
1078789Sahrens 	/*
1079789Sahrens 	 * The underlying storage pool actually uses multiple block sizes.
1080789Sahrens 	 * We report the fragsize as the smallest block size we support,
1081789Sahrens 	 * and we report our blocksize as the filesystem's maximum blocksize.
1082789Sahrens 	 */
1083789Sahrens 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1084789Sahrens 	statp->f_bsize = zfsvfs->z_max_blksz;
1085789Sahrens 
1086789Sahrens 	/*
1087789Sahrens 	 * The following report "total" blocks of various kinds in the
1088789Sahrens 	 * file system, but reported in terms of f_frsize - the
1089789Sahrens 	 * "fragment" size.
1090789Sahrens 	 */
1091789Sahrens 
10922885Sahrens 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
10932885Sahrens 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
1094789Sahrens 	statp->f_bavail = statp->f_bfree; /* no root reservation */
1095789Sahrens 
1096789Sahrens 	/*
1097789Sahrens 	 * statvfs() should really be called statufs(), because it assumes
1098789Sahrens 	 * static metadata.  ZFS doesn't preallocate files, so the best
1099789Sahrens 	 * we can do is report the max that could possibly fit in f_files,
1100789Sahrens 	 * and that minus the number actually used in f_ffree.
1101789Sahrens 	 * For f_ffree, report the smaller of the number of object available
1102789Sahrens 	 * and the number of blocks (each object will take at least a block).
1103789Sahrens 	 */
11042885Sahrens 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
1105789Sahrens 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
11062885Sahrens 	statp->f_files = statp->f_ffree + usedobjs;
1107789Sahrens 
1108789Sahrens 	(void) cmpldev(&d32, vfsp->vfs_dev);
1109789Sahrens 	statp->f_fsid = d32;
1110789Sahrens 
1111789Sahrens 	/*
1112789Sahrens 	 * We're a zfs filesystem.
1113789Sahrens 	 */
1114789Sahrens 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1115789Sahrens 
11161123Smarks 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1117789Sahrens 
1118789Sahrens 	statp->f_namemax = ZFS_MAXNAMELEN;
1119789Sahrens 
1120789Sahrens 	/*
1121789Sahrens 	 * We have all of 32 characters to stuff a string here.
1122789Sahrens 	 * Is there anything useful we could/should provide?
1123789Sahrens 	 */
1124789Sahrens 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
1125789Sahrens 
1126789Sahrens 	ZFS_EXIT(zfsvfs);
1127789Sahrens 	return (0);
1128789Sahrens }
1129789Sahrens 
1130789Sahrens static int
1131789Sahrens zfs_root(vfs_t *vfsp, vnode_t **vpp)
1132789Sahrens {
1133789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1134789Sahrens 	znode_t *rootzp;
1135789Sahrens 	int error;
1136789Sahrens 
1137789Sahrens 	ZFS_ENTER(zfsvfs);
1138789Sahrens 
1139789Sahrens 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1140789Sahrens 	if (error == 0)
1141789Sahrens 		*vpp = ZTOV(rootzp);
1142789Sahrens 
1143789Sahrens 	ZFS_EXIT(zfsvfs);
1144789Sahrens 	return (error);
1145789Sahrens }
1146789Sahrens 
11475326Sek110237 /*
11485326Sek110237  * Teardown the zfsvfs::z_os.
11495326Sek110237  *
11505326Sek110237  * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
11515326Sek110237  * and 'z_teardown_inactive_lock' held.
11525326Sek110237  */
11535326Sek110237 static int
11545326Sek110237 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
11555326Sek110237 {
11565642Smaybee 	znode_t	*zp;
11575326Sek110237 
11585326Sek110237 	rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
11595326Sek110237 
11605326Sek110237 	if (!unmounting) {
11615326Sek110237 		/*
11625326Sek110237 		 * We purge the parent filesystem's vfsp as the parent
11635326Sek110237 		 * filesystem and all of its snapshots have their vnode's
11645326Sek110237 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
11655326Sek110237 		 * 'z_parent' is self referential for non-snapshots.
11665326Sek110237 		 */
11675326Sek110237 		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
11685326Sek110237 	}
11695326Sek110237 
11705326Sek110237 	/*
11715326Sek110237 	 * Close the zil. NB: Can't close the zil while zfs_inactive
11725326Sek110237 	 * threads are blocked as zil_close can call zfs_inactive.
11735326Sek110237 	 */
11745326Sek110237 	if (zfsvfs->z_log) {
11755326Sek110237 		zil_close(zfsvfs->z_log);
11765326Sek110237 		zfsvfs->z_log = NULL;
11775326Sek110237 	}
11785326Sek110237 
11795326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
11805326Sek110237 
11815326Sek110237 	/*
11825326Sek110237 	 * If we are not unmounting (ie: online recv) and someone already
11835326Sek110237 	 * unmounted this file system while we were doing the switcheroo,
11845326Sek110237 	 * or a reopen of z_os failed then just bail out now.
11855326Sek110237 	 */
11865326Sek110237 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
11875326Sek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
11885326Sek110237 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
11895326Sek110237 		return (EIO);
11905326Sek110237 	}
11915326Sek110237 
11925326Sek110237 	/*
11935326Sek110237 	 * At this point there are no vops active, and any new vops will
11945326Sek110237 	 * fail with EIO since we have z_teardown_lock for writer (only
11955326Sek110237 	 * relavent for forced unmount).
11965326Sek110237 	 *
11975326Sek110237 	 * Release all holds on dbufs.
11985326Sek110237 	 */
11995326Sek110237 	mutex_enter(&zfsvfs->z_znodes_lock);
12005642Smaybee 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
12015642Smaybee 	    zp = list_next(&zfsvfs->z_all_znodes, zp))
12025446Sahrens 		if (zp->z_dbuf) {
12035642Smaybee 			ASSERT(ZTOV(zp)->v_count > 0);
12045642Smaybee 			zfs_znode_dmu_fini(zp);
12055326Sek110237 		}
12065326Sek110237 	mutex_exit(&zfsvfs->z_znodes_lock);
12075326Sek110237 
12085326Sek110237 	/*
12095326Sek110237 	 * If we are unmounting, set the unmounted flag and let new vops
12105326Sek110237 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
12115326Sek110237 	 * other vops will fail with EIO.
12125326Sek110237 	 */
12135326Sek110237 	if (unmounting) {
12145326Sek110237 		zfsvfs->z_unmounted = B_TRUE;
12155326Sek110237 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
12165326Sek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
12175326Sek110237 	}
12185326Sek110237 
12195326Sek110237 	/*
12205326Sek110237 	 * z_os will be NULL if there was an error in attempting to reopen
12215326Sek110237 	 * zfsvfs, so just return as the properties had already been
12225326Sek110237 	 * unregistered and cached data had been evicted before.
12235326Sek110237 	 */
12245326Sek110237 	if (zfsvfs->z_os == NULL)
12255326Sek110237 		return (0);
12265326Sek110237 
12275326Sek110237 	/*
12285326Sek110237 	 * Unregister properties.
12295326Sek110237 	 */
12305326Sek110237 	zfs_unregister_callbacks(zfsvfs);
12315326Sek110237 
12325326Sek110237 	/*
12335326Sek110237 	 * Evict cached data
12345326Sek110237 	 */
1235*6083Sek110237 	if (dmu_objset_evict_dbufs(zfsvfs->z_os)) {
12365429Smaybee 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1237*6083Sek110237 		(void) dmu_objset_evict_dbufs(zfsvfs->z_os);
12385429Smaybee 	}
12395326Sek110237 
12405326Sek110237 	return (0);
12415326Sek110237 }
12425326Sek110237 
1243789Sahrens /*ARGSUSED*/
1244789Sahrens static int
1245789Sahrens zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1246789Sahrens {
1247789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
12485326Sek110237 	objset_t *os;
1249789Sahrens 	int ret;
1250789Sahrens 
12514543Smarks 	ret = secpolicy_fs_unmount(cr, vfsp);
12524543Smarks 	if (ret) {
12534543Smarks 		ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
12544543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
12554543Smarks 		if (ret)
12564543Smarks 			return (ret);
12574543Smarks 	}
12581484Sek110237 
12594736Sek110237 	/*
12604736Sek110237 	 * We purge the parent filesystem's vfsp as the parent filesystem
12614736Sek110237 	 * and all of its snapshots have their vnode's v_vfsp set to the
12624736Sek110237 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
12634736Sek110237 	 * referential for non-snapshots.
12644736Sek110237 	 */
12654736Sek110237 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
12661484Sek110237 
1267789Sahrens 	/*
1268789Sahrens 	 * Unmount any snapshots mounted under .zfs before unmounting the
1269789Sahrens 	 * dataset itself.
1270789Sahrens 	 */
1271789Sahrens 	if (zfsvfs->z_ctldir != NULL &&
12724543Smarks 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1273789Sahrens 		return (ret);
12744543Smarks 	}
1275789Sahrens 
12764787Sahrens 	if (!(fflag & MS_FORCE)) {
12774480Sgw25295 		/*
12784787Sahrens 		 * Check the number of active vnodes in the file system.
12794787Sahrens 		 * Our count is maintained in the vfs structure, but the
12804787Sahrens 		 * number is off by 1 to indicate a hold on the vfs
12814787Sahrens 		 * structure itself.
12824787Sahrens 		 *
12834787Sahrens 		 * The '.zfs' directory maintains a reference of its
12844787Sahrens 		 * own, and any active references underneath are
12854787Sahrens 		 * reflected in the vnode count.
1286789Sahrens 		 */
12874787Sahrens 		if (zfsvfs->z_ctldir == NULL) {
12884787Sahrens 			if (vfsp->vfs_count > 1)
12894787Sahrens 				return (EBUSY);
12904787Sahrens 		} else {
12914787Sahrens 			if (vfsp->vfs_count > 2 ||
12925326Sek110237 			    zfsvfs->z_ctldir->v_count > 1)
12934787Sahrens 				return (EBUSY);
1294789Sahrens 		}
1295789Sahrens 	}
1296789Sahrens 
1297789Sahrens 	vfsp->vfs_flag |= VFS_UNMOUNTED;
12984787Sahrens 
12995326Sek110237 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
13005326Sek110237 	os = zfsvfs->z_os;
13014787Sahrens 
13024787Sahrens 	/*
13035326Sek110237 	 * z_os will be NULL if there was an error in
13045326Sek110237 	 * attempting to reopen zfsvfs.
13054787Sahrens 	 */
13065326Sek110237 	if (os != NULL) {
13075326Sek110237 		/*
13085326Sek110237 		 * Unset the objset user_ptr.
13095326Sek110237 		 */
13105326Sek110237 		mutex_enter(&os->os->os_user_ptr_lock);
13115326Sek110237 		dmu_objset_set_user(os, NULL);
13125326Sek110237 		mutex_exit(&os->os->os_user_ptr_lock);
13134787Sahrens 
13145326Sek110237 		/*
13155326Sek110237 		 * Finally close the objset
13165326Sek110237 		 */
13175326Sek110237 		dmu_objset_close(os);
13184787Sahrens 	}
13194787Sahrens 
13204787Sahrens 	/*
13214787Sahrens 	 * We can now safely destroy the '.zfs' directory node.
13224787Sahrens 	 */
13234787Sahrens 	if (zfsvfs->z_ctldir != NULL)
13244787Sahrens 		zfsctl_destroy(zfsvfs);
1325789Sahrens 
1326789Sahrens 	return (0);
1327789Sahrens }
1328789Sahrens 
1329789Sahrens static int
1330789Sahrens zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1331789Sahrens {
1332789Sahrens 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1333789Sahrens 	znode_t		*zp;
1334789Sahrens 	uint64_t	object = 0;
1335789Sahrens 	uint64_t	fid_gen = 0;
1336789Sahrens 	uint64_t	gen_mask;
1337789Sahrens 	uint64_t	zp_gen;
1338789Sahrens 	int 		i, err;
1339789Sahrens 
1340789Sahrens 	*vpp = NULL;
1341789Sahrens 
1342789Sahrens 	ZFS_ENTER(zfsvfs);
1343789Sahrens 
1344789Sahrens 	if (fidp->fid_len == LONG_FID_LEN) {
1345789Sahrens 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1346789Sahrens 		uint64_t	objsetid = 0;
1347789Sahrens 		uint64_t	setgen = 0;
1348789Sahrens 
1349789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1350789Sahrens 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1351789Sahrens 
1352789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1353789Sahrens 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1354789Sahrens 
1355789Sahrens 		ZFS_EXIT(zfsvfs);
1356789Sahrens 
1357789Sahrens 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1358789Sahrens 		if (err)
1359789Sahrens 			return (EINVAL);
1360789Sahrens 		ZFS_ENTER(zfsvfs);
1361789Sahrens 	}
1362789Sahrens 
1363789Sahrens 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1364789Sahrens 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1365789Sahrens 
1366789Sahrens 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1367789Sahrens 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1368789Sahrens 
1369789Sahrens 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1370789Sahrens 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1371789Sahrens 	} else {
1372789Sahrens 		ZFS_EXIT(zfsvfs);
1373789Sahrens 		return (EINVAL);
1374789Sahrens 	}
1375789Sahrens 
1376789Sahrens 	/* A zero fid_gen means we are in the .zfs control directories */
1377789Sahrens 	if (fid_gen == 0 &&
1378789Sahrens 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1379789Sahrens 		*vpp = zfsvfs->z_ctldir;
1380789Sahrens 		ASSERT(*vpp != NULL);
1381789Sahrens 		if (object == ZFSCTL_INO_SNAPDIR) {
1382789Sahrens 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
13835331Samw 			    0, NULL, NULL, NULL, NULL, NULL) == 0);
1384789Sahrens 		} else {
1385789Sahrens 			VN_HOLD(*vpp);
1386789Sahrens 		}
1387789Sahrens 		ZFS_EXIT(zfsvfs);
1388789Sahrens 		return (0);
1389789Sahrens 	}
1390789Sahrens 
1391789Sahrens 	gen_mask = -1ULL >> (64 - 8 * i);
1392789Sahrens 
1393789Sahrens 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1394789Sahrens 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1395789Sahrens 		ZFS_EXIT(zfsvfs);
1396789Sahrens 		return (err);
1397789Sahrens 	}
1398789Sahrens 	zp_gen = zp->z_phys->zp_gen & gen_mask;
1399789Sahrens 	if (zp_gen == 0)
1400789Sahrens 		zp_gen = 1;
14013461Sahrens 	if (zp->z_unlinked || zp_gen != fid_gen) {
1402789Sahrens 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1403789Sahrens 		VN_RELE(ZTOV(zp));
1404789Sahrens 		ZFS_EXIT(zfsvfs);
1405789Sahrens 		return (EINVAL);
1406789Sahrens 	}
1407789Sahrens 
1408789Sahrens 	*vpp = ZTOV(zp);
1409789Sahrens 	ZFS_EXIT(zfsvfs);
1410789Sahrens 	return (0);
1411789Sahrens }
1412789Sahrens 
14135326Sek110237 /*
14145326Sek110237  * Block out VOPs and close zfsvfs_t::z_os
14155326Sek110237  *
14165326Sek110237  * Note, if successful, then we return with the 'z_teardown_lock' and
14175326Sek110237  * 'z_teardown_inactive_lock' write held.
14185326Sek110237  */
14195326Sek110237 int
14205326Sek110237 zfs_suspend_fs(zfsvfs_t *zfsvfs, char *name, int *mode)
14215326Sek110237 {
14225326Sek110237 	int error;
14235326Sek110237 
14245326Sek110237 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
14255326Sek110237 		return (error);
14265326Sek110237 
14275326Sek110237 	*mode = zfsvfs->z_os->os_mode;
14285326Sek110237 	dmu_objset_name(zfsvfs->z_os, name);
14295326Sek110237 	dmu_objset_close(zfsvfs->z_os);
14305326Sek110237 
14315326Sek110237 	return (0);
14325326Sek110237 }
14335326Sek110237 
14345326Sek110237 /*
14355326Sek110237  * Reopen zfsvfs_t::z_os and release VOPs.
14365326Sek110237  */
14375326Sek110237 int
14385326Sek110237 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname, int mode)
14395326Sek110237 {
14405326Sek110237 	int err;
14415326Sek110237 
14425326Sek110237 	ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
14435326Sek110237 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
14445326Sek110237 
14455326Sek110237 	err = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
14465326Sek110237 	if (err) {
14475326Sek110237 		zfsvfs->z_os = NULL;
14485326Sek110237 	} else {
14495326Sek110237 		znode_t *zp;
14505326Sek110237 
14515326Sek110237 		VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
14525326Sek110237 
14535326Sek110237 		/*
14545326Sek110237 		 * Attempt to re-establish all the active znodes with
14555326Sek110237 		 * their dbufs.  If a zfs_rezget() fails, then we'll let
14565326Sek110237 		 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
14575326Sek110237 		 * when they try to use their znode.
14585326Sek110237 		 */
14595326Sek110237 		mutex_enter(&zfsvfs->z_znodes_lock);
14605326Sek110237 		for (zp = list_head(&zfsvfs->z_all_znodes); zp;
14615326Sek110237 		    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
14625326Sek110237 			(void) zfs_rezget(zp);
14635326Sek110237 		}
14645326Sek110237 		mutex_exit(&zfsvfs->z_znodes_lock);
14655326Sek110237 
14665326Sek110237 	}
14675326Sek110237 
14685326Sek110237 	/* release the VOPs */
14695326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
14705326Sek110237 	rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
14715326Sek110237 
14725326Sek110237 	if (err) {
14735326Sek110237 		/*
14745326Sek110237 		 * Since we couldn't reopen zfsvfs::z_os, force
14755326Sek110237 		 * unmount this file system.
14765326Sek110237 		 */
14775326Sek110237 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
14785326Sek110237 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
14795326Sek110237 	}
14805326Sek110237 	return (err);
14815326Sek110237 }
14825326Sek110237 
1483789Sahrens static void
1484789Sahrens zfs_freevfs(vfs_t *vfsp)
1485789Sahrens {
1486789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
14874831Sgw25295 	int i;
14884831Sgw25295 
14894831Sgw25295 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
14904831Sgw25295 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1491789Sahrens 
14925331Samw 	zfs_fuid_destroy(zfsvfs);
1493*6083Sek110237 	zfs_freezfsvfs(zfsvfs);
1494789Sahrens 
1495789Sahrens 	atomic_add_32(&zfs_active_fs_count, -1);
1496789Sahrens }
1497789Sahrens 
1498789Sahrens /*
1499789Sahrens  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
1500789Sahrens  * so we can't safely do any non-idempotent initialization here.
1501789Sahrens  * Leave that to zfs_init() and zfs_fini(), which are called
1502789Sahrens  * from the module's _init() and _fini() entry points.
1503789Sahrens  */
1504789Sahrens /*ARGSUSED*/
1505789Sahrens static int
1506789Sahrens zfs_vfsinit(int fstype, char *name)
1507789Sahrens {
1508789Sahrens 	int error;
1509789Sahrens 
1510789Sahrens 	zfsfstype = fstype;
1511789Sahrens 
1512789Sahrens 	/*
1513789Sahrens 	 * Setup vfsops and vnodeops tables.
1514789Sahrens 	 */
1515789Sahrens 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
1516789Sahrens 	if (error != 0) {
1517789Sahrens 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
1518789Sahrens 	}
1519789Sahrens 
1520789Sahrens 	error = zfs_create_op_tables();
1521789Sahrens 	if (error) {
1522789Sahrens 		zfs_remove_op_tables();
1523789Sahrens 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
1524789Sahrens 		(void) vfs_freevfsops_by_type(zfsfstype);
1525789Sahrens 		return (error);
1526789Sahrens 	}
1527789Sahrens 
1528789Sahrens 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
1529789Sahrens 
1530789Sahrens 	/*
1531849Sbonwick 	 * Unique major number for all zfs mounts.
1532849Sbonwick 	 * If we run out of 32-bit minors, we'll getudev() another major.
1533789Sahrens 	 */
1534849Sbonwick 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
1535849Sbonwick 	zfs_minor = ZFS_MIN_MINOR;
1536789Sahrens 
1537789Sahrens 	return (0);
1538789Sahrens }
1539789Sahrens 
1540789Sahrens void
1541789Sahrens zfs_init(void)
1542789Sahrens {
1543789Sahrens 	/*
1544789Sahrens 	 * Initialize .zfs directory structures
1545789Sahrens 	 */
1546789Sahrens 	zfsctl_init();
1547789Sahrens 
1548789Sahrens 	/*
1549789Sahrens 	 * Initialize znode cache, vnode ops, etc...
1550789Sahrens 	 */
1551789Sahrens 	zfs_znode_init();
1552789Sahrens }
1553789Sahrens 
1554789Sahrens void
1555789Sahrens zfs_fini(void)
1556789Sahrens {
1557789Sahrens 	zfsctl_fini();
1558789Sahrens 	zfs_znode_fini();
1559789Sahrens }
1560789Sahrens 
1561789Sahrens int
1562789Sahrens zfs_busy(void)
1563789Sahrens {
1564789Sahrens 	return (zfs_active_fs_count != 0);
1565789Sahrens }
1566789Sahrens 
15674577Sahrens int
15684577Sahrens zfs_set_version(const char *name, uint64_t newvers)
15694577Sahrens {
15704577Sahrens 	int error;
15714577Sahrens 	objset_t *os;
15724577Sahrens 	dmu_tx_t *tx;
15734577Sahrens 	uint64_t curvers;
15744577Sahrens 
15754577Sahrens 	/*
15764577Sahrens 	 * XXX for now, require that the filesystem be unmounted.  Would
15774577Sahrens 	 * be nice to find the zfsvfs_t and just update that if
15784577Sahrens 	 * possible.
15794577Sahrens 	 */
15804577Sahrens 
15814577Sahrens 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
15824577Sahrens 		return (EINVAL);
15834577Sahrens 
15844577Sahrens 	error = dmu_objset_open(name, DMU_OST_ZFS, DS_MODE_PRIMARY, &os);
15854577Sahrens 	if (error)
15864577Sahrens 		return (error);
15874577Sahrens 
15884577Sahrens 	error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
15894577Sahrens 	    8, 1, &curvers);
15904577Sahrens 	if (error)
15914577Sahrens 		goto out;
15924577Sahrens 	if (newvers < curvers) {
15934577Sahrens 		error = EINVAL;
15944577Sahrens 		goto out;
15954577Sahrens 	}
15964577Sahrens 
15974577Sahrens 	tx = dmu_tx_create(os);
15984577Sahrens 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 0, ZPL_VERSION_STR);
15994577Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
16004577Sahrens 	if (error) {
16014577Sahrens 		dmu_tx_abort(tx);
16024577Sahrens 		goto out;
16034577Sahrens 	}
16044577Sahrens 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1,
16054577Sahrens 	    &newvers, tx);
16064577Sahrens 
16074577Sahrens 	spa_history_internal_log(LOG_DS_UPGRADE,
16084577Sahrens 	    dmu_objset_spa(os), tx, CRED(),
16094577Sahrens 	    "oldver=%llu newver=%llu dataset = %llu", curvers, newvers,
16104577Sahrens 	    dmu_objset_id(os));
16114577Sahrens 	dmu_tx_commit(tx);
16124577Sahrens 
16134577Sahrens out:
16144577Sahrens 	dmu_objset_close(os);
16154577Sahrens 	return (error);
16164577Sahrens }
16174577Sahrens 
16185498Stimh /*
16195498Stimh  * Read a property stored within the master node.
16205498Stimh  */
16215498Stimh int
16225498Stimh zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
16235498Stimh {
16245498Stimh 	const char *pname;
16255498Stimh 	int error;
16265498Stimh 
16275498Stimh 	/*
16285498Stimh 	 * Look up the file system's value for the property.  For the
16295498Stimh 	 * version property, we look up a slightly different string.
16305498Stimh 	 * Also, there is no default VERSION value, so if we don't
16315498Stimh 	 * find it, return the error.
16325498Stimh 	 */
16335498Stimh 	if (prop == ZFS_PROP_VERSION)
16345498Stimh 		pname = ZPL_VERSION_STR;
16355498Stimh 	else
16365498Stimh 		pname = zfs_prop_to_name(prop);
16375498Stimh 
16385498Stimh 	error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
16395498Stimh 
16405498Stimh 	if (!error) {
16415498Stimh 		return (0);
16425498Stimh 	} else if (prop == ZFS_PROP_VERSION || error != ENOENT) {
16435498Stimh 		return (error);
16445498Stimh 	} else {
16455498Stimh 		/* No value set, use the default value */
16465498Stimh 		switch (prop) {
16475498Stimh 		case ZFS_PROP_NORMALIZE:
16485498Stimh 		case ZFS_PROP_UTF8ONLY:
16495498Stimh 			*value = 0;
16505498Stimh 			break;
16515498Stimh 		case ZFS_PROP_CASE:
16525498Stimh 			*value = ZFS_CASE_SENSITIVE;
16535498Stimh 			break;
16545498Stimh 		default:
16555498Stimh 			return (ENOENT);
16565498Stimh 		}
16575498Stimh 	}
16585498Stimh 	return (0);
16595498Stimh }
16605498Stimh 
1661789Sahrens static vfsdef_t vfw = {
1662789Sahrens 	VFSDEF_VERSION,
1663789Sahrens 	MNTTYPE_ZFS,
1664789Sahrens 	zfs_vfsinit,
16655331Samw 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS|
16665331Samw 	    VSW_XID,
1667789Sahrens 	&zfs_mntopts
1668789Sahrens };
1669789Sahrens 
1670789Sahrens struct modlfs zfs_modlfs = {
16714577Sahrens 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
1672789Sahrens };
1673