xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 7265:cc18862247da)
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 /*
226083Sek110237  * 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>
646423Sgw25295 #include <sys/spa_boot.h>
65789Sahrens 
66789Sahrens int zfsfstype;
67789Sahrens vfsops_t *zfs_vfsops = NULL;
68849Sbonwick static major_t zfs_major;
69789Sahrens static minor_t zfs_minor;
70789Sahrens static kmutex_t	zfs_dev_mtx;
71789Sahrens 
72789Sahrens static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
73789Sahrens static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr);
741544Seschrock static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
75789Sahrens static int zfs_root(vfs_t *vfsp, vnode_t **vpp);
76789Sahrens static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp);
77789Sahrens static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp);
78789Sahrens static void zfs_freevfs(vfs_t *vfsp);
79789Sahrens 
80789Sahrens static const fs_operation_def_t zfs_vfsops_template[] = {
813898Srsb 	VFSNAME_MOUNT,		{ .vfs_mount = zfs_mount },
823898Srsb 	VFSNAME_MOUNTROOT,	{ .vfs_mountroot = zfs_mountroot },
833898Srsb 	VFSNAME_UNMOUNT,	{ .vfs_unmount = zfs_umount },
843898Srsb 	VFSNAME_ROOT,		{ .vfs_root = zfs_root },
853898Srsb 	VFSNAME_STATVFS,	{ .vfs_statvfs = zfs_statvfs },
863898Srsb 	VFSNAME_SYNC,		{ .vfs_sync = zfs_sync },
873898Srsb 	VFSNAME_VGET,		{ .vfs_vget = zfs_vget },
883898Srsb 	VFSNAME_FREEVFS,	{ .vfs_freevfs = zfs_freevfs },
893898Srsb 	NULL,			NULL
90789Sahrens };
91789Sahrens 
92789Sahrens static const fs_operation_def_t zfs_vfsops_eio_template[] = {
933898Srsb 	VFSNAME_FREEVFS,	{ .vfs_freevfs =  zfs_freevfs },
943898Srsb 	NULL,			NULL
95789Sahrens };
96789Sahrens 
97789Sahrens /*
98789Sahrens  * We need to keep a count of active fs's.
99789Sahrens  * This is necessary to prevent our module
100789Sahrens  * from being unloaded after a umount -f
101789Sahrens  */
102789Sahrens static uint32_t	zfs_active_fs_count = 0;
103789Sahrens 
104789Sahrens static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
105789Sahrens static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
1063234Sck153898 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
1073234Sck153898 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
108789Sahrens 
1093234Sck153898 /*
1104596Slling  * MO_DEFAULT is not used since the default value is determined
1114596Slling  * by the equivalent property.
1123234Sck153898  */
113789Sahrens static mntopt_t mntopts[] = {
1143234Sck153898 	{ MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL },
1153234Sck153898 	{ MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL },
1164596Slling 	{ MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL },
117789Sahrens 	{ MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
118789Sahrens };
119789Sahrens 
120789Sahrens static mntopts_t zfs_mntopts = {
121789Sahrens 	sizeof (mntopts) / sizeof (mntopt_t),
122789Sahrens 	mntopts
123789Sahrens };
124789Sahrens 
125789Sahrens /*ARGSUSED*/
126789Sahrens int
127789Sahrens zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
128789Sahrens {
129789Sahrens 	/*
130789Sahrens 	 * Data integrity is job one.  We don't want a compromised kernel
131789Sahrens 	 * writing to the storage pool, so we never sync during panic.
132789Sahrens 	 */
133789Sahrens 	if (panicstr)
134789Sahrens 		return (0);
135789Sahrens 
136789Sahrens 	/*
137789Sahrens 	 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
138789Sahrens 	 * to sync metadata, which they would otherwise cache indefinitely.
139789Sahrens 	 * Semantically, the only requirement is that the sync be initiated.
140789Sahrens 	 * The DMU syncs out txgs frequently, so there's nothing to do.
141789Sahrens 	 */
142789Sahrens 	if (flag & SYNC_ATTR)
143789Sahrens 		return (0);
144789Sahrens 
145789Sahrens 	if (vfsp != NULL) {
146789Sahrens 		/*
147789Sahrens 		 * Sync a specific filesystem.
148789Sahrens 		 */
149789Sahrens 		zfsvfs_t *zfsvfs = vfsp->vfs_data;
150789Sahrens 
151789Sahrens 		ZFS_ENTER(zfsvfs);
152789Sahrens 		if (zfsvfs->z_log != NULL)
1532638Sperrin 			zil_commit(zfsvfs->z_log, UINT64_MAX, 0);
154789Sahrens 		else
155789Sahrens 			txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
156789Sahrens 		ZFS_EXIT(zfsvfs);
157789Sahrens 	} else {
158789Sahrens 		/*
159789Sahrens 		 * Sync all ZFS filesystems.  This is what happens when you
160789Sahrens 		 * run sync(1M).  Unlike other filesystems, ZFS honors the
161789Sahrens 		 * request by waiting for all pools to commit all dirty data.
162789Sahrens 		 */
163789Sahrens 		spa_sync_allpools();
164789Sahrens 	}
165789Sahrens 
166789Sahrens 	return (0);
167789Sahrens }
168789Sahrens 
1691544Seschrock static int
1701544Seschrock zfs_create_unique_device(dev_t *dev)
1711544Seschrock {
1721544Seschrock 	major_t new_major;
1731544Seschrock 
1741544Seschrock 	do {
1751544Seschrock 		ASSERT3U(zfs_minor, <=, MAXMIN32);
1761544Seschrock 		minor_t start = zfs_minor;
1771544Seschrock 		do {
1781544Seschrock 			mutex_enter(&zfs_dev_mtx);
1791544Seschrock 			if (zfs_minor >= MAXMIN32) {
1801544Seschrock 				/*
1811544Seschrock 				 * If we're still using the real major
1821544Seschrock 				 * keep out of /dev/zfs and /dev/zvol minor
1831544Seschrock 				 * number space.  If we're using a getudev()'ed
1841544Seschrock 				 * major number, we can use all of its minors.
1851544Seschrock 				 */
1861544Seschrock 				if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
1871544Seschrock 					zfs_minor = ZFS_MIN_MINOR;
1881544Seschrock 				else
1891544Seschrock 					zfs_minor = 0;
1901544Seschrock 			} else {
1911544Seschrock 				zfs_minor++;
1921544Seschrock 			}
1931544Seschrock 			*dev = makedevice(zfs_major, zfs_minor);
1941544Seschrock 			mutex_exit(&zfs_dev_mtx);
1951544Seschrock 		} while (vfs_devismounted(*dev) && zfs_minor != start);
1961544Seschrock 		if (zfs_minor == start) {
1971544Seschrock 			/*
1981544Seschrock 			 * We are using all ~262,000 minor numbers for the
1991544Seschrock 			 * current major number.  Create a new major number.
2001544Seschrock 			 */
2011544Seschrock 			if ((new_major = getudev()) == (major_t)-1) {
2021544Seschrock 				cmn_err(CE_WARN,
2031544Seschrock 				    "zfs_mount: Can't get unique major "
2041544Seschrock 				    "device number.");
2051544Seschrock 				return (-1);
2061544Seschrock 			}
2071544Seschrock 			mutex_enter(&zfs_dev_mtx);
2081544Seschrock 			zfs_major = new_major;
2091544Seschrock 			zfs_minor = 0;
2101544Seschrock 
2111544Seschrock 			mutex_exit(&zfs_dev_mtx);
2121544Seschrock 		} else {
2131544Seschrock 			break;
2141544Seschrock 		}
2151544Seschrock 		/* CONSTANTCONDITION */
2161544Seschrock 	} while (1);
2171544Seschrock 
2181544Seschrock 	return (0);
2191544Seschrock }
2201544Seschrock 
221789Sahrens static void
222789Sahrens atime_changed_cb(void *arg, uint64_t newval)
223789Sahrens {
224789Sahrens 	zfsvfs_t *zfsvfs = arg;
225789Sahrens 
226789Sahrens 	if (newval == TRUE) {
227789Sahrens 		zfsvfs->z_atime = TRUE;
228789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
229789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
230789Sahrens 	} else {
231789Sahrens 		zfsvfs->z_atime = FALSE;
232789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
233789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
234789Sahrens 	}
235789Sahrens }
236789Sahrens 
237789Sahrens static void
2383234Sck153898 xattr_changed_cb(void *arg, uint64_t newval)
2393234Sck153898 {
2403234Sck153898 	zfsvfs_t *zfsvfs = arg;
2413234Sck153898 
2423234Sck153898 	if (newval == TRUE) {
2433234Sck153898 		/* XXX locking on vfs_flag? */
2443234Sck153898 		zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
2453234Sck153898 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
2463234Sck153898 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
2473234Sck153898 	} else {
2483234Sck153898 		/* XXX locking on vfs_flag? */
2493234Sck153898 		zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
2503234Sck153898 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
2513234Sck153898 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
2523234Sck153898 	}
2533234Sck153898 }
2543234Sck153898 
2553234Sck153898 static void
256789Sahrens blksz_changed_cb(void *arg, uint64_t newval)
257789Sahrens {
258789Sahrens 	zfsvfs_t *zfsvfs = arg;
259789Sahrens 
260789Sahrens 	if (newval < SPA_MINBLOCKSIZE ||
261789Sahrens 	    newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
262789Sahrens 		newval = SPA_MAXBLOCKSIZE;
263789Sahrens 
264789Sahrens 	zfsvfs->z_max_blksz = newval;
265789Sahrens 	zfsvfs->z_vfs->vfs_bsize = newval;
266789Sahrens }
267789Sahrens 
268789Sahrens static void
269789Sahrens readonly_changed_cb(void *arg, uint64_t newval)
270789Sahrens {
271789Sahrens 	zfsvfs_t *zfsvfs = arg;
272789Sahrens 
273789Sahrens 	if (newval) {
274789Sahrens 		/* XXX locking on vfs_flag? */
275789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
276789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
277789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
278789Sahrens 	} else {
279789Sahrens 		/* XXX locking on vfs_flag? */
280789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
281789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
282789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
283789Sahrens 	}
284789Sahrens }
285789Sahrens 
286789Sahrens static void
287789Sahrens devices_changed_cb(void *arg, uint64_t newval)
288789Sahrens {
289789Sahrens 	zfsvfs_t *zfsvfs = arg;
290789Sahrens 
291789Sahrens 	if (newval == FALSE) {
292789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
293789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
294789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
295789Sahrens 	} else {
296789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
297789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
298789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
299789Sahrens 	}
300789Sahrens }
301789Sahrens 
302789Sahrens static void
303789Sahrens setuid_changed_cb(void *arg, uint64_t newval)
304789Sahrens {
305789Sahrens 	zfsvfs_t *zfsvfs = arg;
306789Sahrens 
307789Sahrens 	if (newval == FALSE) {
308789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
309789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
310789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
311789Sahrens 	} else {
312789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
313789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
314789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
315789Sahrens 	}
316789Sahrens }
317789Sahrens 
318789Sahrens static void
319789Sahrens exec_changed_cb(void *arg, uint64_t newval)
320789Sahrens {
321789Sahrens 	zfsvfs_t *zfsvfs = arg;
322789Sahrens 
323789Sahrens 	if (newval == FALSE) {
324789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
325789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
326789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
327789Sahrens 	} else {
328789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
329789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
330789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
331789Sahrens 	}
332789Sahrens }
333789Sahrens 
3345331Samw /*
3355331Samw  * The nbmand mount option can be changed at mount time.
3365331Samw  * We can't allow it to be toggled on live file systems or incorrect
3375331Samw  * behavior may be seen from cifs clients
3385331Samw  *
3395331Samw  * This property isn't registered via dsl_prop_register(), but this callback
3405331Samw  * will be called when a file system is first mounted
3415331Samw  */
3425331Samw static void
3435331Samw nbmand_changed_cb(void *arg, uint64_t newval)
3445331Samw {
3455331Samw 	zfsvfs_t *zfsvfs = arg;
3465331Samw 	if (newval == FALSE) {
3475331Samw 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
3485331Samw 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
3495331Samw 	} else {
3505331Samw 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
3515331Samw 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
3525331Samw 	}
3535331Samw }
3545331Samw 
355789Sahrens static void
356789Sahrens snapdir_changed_cb(void *arg, uint64_t newval)
357789Sahrens {
358789Sahrens 	zfsvfs_t *zfsvfs = arg;
359789Sahrens 
360789Sahrens 	zfsvfs->z_show_ctldir = newval;
361789Sahrens }
362789Sahrens 
363789Sahrens static void
3645331Samw vscan_changed_cb(void *arg, uint64_t newval)
3655331Samw {
3665331Samw 	zfsvfs_t *zfsvfs = arg;
3675331Samw 
3685331Samw 	zfsvfs->z_vscan = newval;
3695331Samw }
3705331Samw 
3715331Samw static void
372789Sahrens acl_mode_changed_cb(void *arg, uint64_t newval)
373789Sahrens {
374789Sahrens 	zfsvfs_t *zfsvfs = arg;
375789Sahrens 
376789Sahrens 	zfsvfs->z_acl_mode = newval;
377789Sahrens }
378789Sahrens 
379789Sahrens static void
380789Sahrens acl_inherit_changed_cb(void *arg, uint64_t newval)
381789Sahrens {
382789Sahrens 	zfsvfs_t *zfsvfs = arg;
383789Sahrens 
384789Sahrens 	zfsvfs->z_acl_inherit = newval;
385789Sahrens }
386789Sahrens 
3871544Seschrock static int
3881544Seschrock zfs_register_callbacks(vfs_t *vfsp)
3891544Seschrock {
3901544Seschrock 	struct dsl_dataset *ds = NULL;
3911544Seschrock 	objset_t *os = NULL;
3921544Seschrock 	zfsvfs_t *zfsvfs = NULL;
3935331Samw 	uint64_t nbmand;
3945331Samw 	int readonly, do_readonly = B_FALSE;
3955331Samw 	int setuid, do_setuid = B_FALSE;
3965331Samw 	int exec, do_exec = B_FALSE;
3975331Samw 	int devices, do_devices = B_FALSE;
3985331Samw 	int xattr, do_xattr = B_FALSE;
3995331Samw 	int atime, do_atime = B_FALSE;
4001544Seschrock 	int error = 0;
4011544Seschrock 
4021544Seschrock 	ASSERT(vfsp);
4031544Seschrock 	zfsvfs = vfsp->vfs_data;
4041544Seschrock 	ASSERT(zfsvfs);
4051544Seschrock 	os = zfsvfs->z_os;
4061544Seschrock 
4071544Seschrock 	/*
4081544Seschrock 	 * The act of registering our callbacks will destroy any mount
4091544Seschrock 	 * options we may have.  In order to enable temporary overrides
4103234Sck153898 	 * of mount options, we stash away the current values and
4111544Seschrock 	 * restore them after we register the callbacks.
4121544Seschrock 	 */
4131544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
4141544Seschrock 		readonly = B_TRUE;
4151544Seschrock 		do_readonly = B_TRUE;
4161544Seschrock 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
4171544Seschrock 		readonly = B_FALSE;
4181544Seschrock 		do_readonly = B_TRUE;
4191544Seschrock 	}
4201544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
4211544Seschrock 		devices = B_FALSE;
4221544Seschrock 		setuid = B_FALSE;
4231544Seschrock 		do_devices = B_TRUE;
4241544Seschrock 		do_setuid = B_TRUE;
4251544Seschrock 	} else {
4261544Seschrock 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
4271544Seschrock 			devices = B_FALSE;
4281544Seschrock 			do_devices = B_TRUE;
4293912Slling 		} else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
4301544Seschrock 			devices = B_TRUE;
4311544Seschrock 			do_devices = B_TRUE;
4321544Seschrock 		}
4331544Seschrock 
4341544Seschrock 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
4351544Seschrock 			setuid = B_FALSE;
4361544Seschrock 			do_setuid = B_TRUE;
4371544Seschrock 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
4381544Seschrock 			setuid = B_TRUE;
4391544Seschrock 			do_setuid = B_TRUE;
4401544Seschrock 		}
4411544Seschrock 	}
4421544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
4431544Seschrock 		exec = B_FALSE;
4441544Seschrock 		do_exec = B_TRUE;
4451544Seschrock 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
4461544Seschrock 		exec = B_TRUE;
4471544Seschrock 		do_exec = B_TRUE;
4481544Seschrock 	}
4493234Sck153898 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
4503234Sck153898 		xattr = B_FALSE;
4513234Sck153898 		do_xattr = B_TRUE;
4523234Sck153898 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
4533234Sck153898 		xattr = B_TRUE;
4543234Sck153898 		do_xattr = B_TRUE;
4553234Sck153898 	}
4564596Slling 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
4574596Slling 		atime = B_FALSE;
4584596Slling 		do_atime = B_TRUE;
4594596Slling 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
4604596Slling 		atime = B_TRUE;
4614596Slling 		do_atime = B_TRUE;
4624596Slling 	}
4631544Seschrock 
4641544Seschrock 	/*
4655331Samw 	 * nbmand is a special property.  It can only be changed at
4665331Samw 	 * mount time.
4675331Samw 	 *
4685331Samw 	 * This is weird, but it is documented to only be changeable
4695331Samw 	 * at mount time.
4705331Samw 	 */
4715331Samw 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
4725331Samw 		nbmand = B_FALSE;
4735331Samw 	} else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
4745331Samw 		nbmand = B_TRUE;
4755331Samw 	} else {
4765331Samw 		char osname[MAXNAMELEN];
4775331Samw 
4785331Samw 		dmu_objset_name(os, osname);
4795331Samw 		if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
480*7265Sahrens 		    NULL)) {
481*7265Sahrens 			return (error);
482*7265Sahrens 		}
4835331Samw 	}
4845331Samw 
4855331Samw 	/*
4861544Seschrock 	 * Register property callbacks.
4871544Seschrock 	 *
4881544Seschrock 	 * It would probably be fine to just check for i/o error from
4891544Seschrock 	 * the first prop_register(), but I guess I like to go
4901544Seschrock 	 * overboard...
4911544Seschrock 	 */
4921544Seschrock 	ds = dmu_objset_ds(os);
4931544Seschrock 	error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
4941544Seschrock 	error = error ? error : dsl_prop_register(ds,
4953234Sck153898 	    "xattr", xattr_changed_cb, zfsvfs);
4963234Sck153898 	error = error ? error : dsl_prop_register(ds,
4971544Seschrock 	    "recordsize", blksz_changed_cb, zfsvfs);
4981544Seschrock 	error = error ? error : dsl_prop_register(ds,
4991544Seschrock 	    "readonly", readonly_changed_cb, zfsvfs);
5001544Seschrock 	error = error ? error : dsl_prop_register(ds,
5011544Seschrock 	    "devices", devices_changed_cb, zfsvfs);
5021544Seschrock 	error = error ? error : dsl_prop_register(ds,
5031544Seschrock 	    "setuid", setuid_changed_cb, zfsvfs);
5041544Seschrock 	error = error ? error : dsl_prop_register(ds,
5051544Seschrock 	    "exec", exec_changed_cb, zfsvfs);
5061544Seschrock 	error = error ? error : dsl_prop_register(ds,
5071544Seschrock 	    "snapdir", snapdir_changed_cb, zfsvfs);
5081544Seschrock 	error = error ? error : dsl_prop_register(ds,
5091544Seschrock 	    "aclmode", acl_mode_changed_cb, zfsvfs);
5101544Seschrock 	error = error ? error : dsl_prop_register(ds,
5111544Seschrock 	    "aclinherit", acl_inherit_changed_cb, zfsvfs);
5125331Samw 	error = error ? error : dsl_prop_register(ds,
5135331Samw 	    "vscan", vscan_changed_cb, zfsvfs);
5141544Seschrock 	if (error)
5151544Seschrock 		goto unregister;
5161544Seschrock 
5171544Seschrock 	/*
5181544Seschrock 	 * Invoke our callbacks to restore temporary mount options.
5191544Seschrock 	 */
5201544Seschrock 	if (do_readonly)
5211544Seschrock 		readonly_changed_cb(zfsvfs, readonly);
5221544Seschrock 	if (do_setuid)
5231544Seschrock 		setuid_changed_cb(zfsvfs, setuid);
5241544Seschrock 	if (do_exec)
5251544Seschrock 		exec_changed_cb(zfsvfs, exec);
5261544Seschrock 	if (do_devices)
5271544Seschrock 		devices_changed_cb(zfsvfs, devices);
5283234Sck153898 	if (do_xattr)
5293234Sck153898 		xattr_changed_cb(zfsvfs, xattr);
5304596Slling 	if (do_atime)
5314596Slling 		atime_changed_cb(zfsvfs, atime);
5321544Seschrock 
5335331Samw 	nbmand_changed_cb(zfsvfs, nbmand);
5345331Samw 
5351544Seschrock 	return (0);
5361544Seschrock 
5371544Seschrock unregister:
5381544Seschrock 	/*
5391544Seschrock 	 * We may attempt to unregister some callbacks that are not
5401544Seschrock 	 * registered, but this is OK; it will simply return ENOMSG,
5411544Seschrock 	 * which we will ignore.
5421544Seschrock 	 */
5431544Seschrock 	(void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
5443234Sck153898 	(void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
5451544Seschrock 	(void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
5461544Seschrock 	(void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
5471544Seschrock 	(void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs);
5481544Seschrock 	(void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
5491544Seschrock 	(void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
5501544Seschrock 	(void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
5511544Seschrock 	(void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
5521544Seschrock 	(void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
5531544Seschrock 	    zfsvfs);
5545331Samw 	(void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
5551544Seschrock 	return (error);
5561544Seschrock 
5571544Seschrock }
5581544Seschrock 
5591544Seschrock static int
5605326Sek110237 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
5615326Sek110237 {
5625326Sek110237 	uint_t readonly;
5635326Sek110237 	int error;
5645326Sek110237 
5655326Sek110237 	error = zfs_register_callbacks(zfsvfs->z_vfs);
5665326Sek110237 	if (error)
5675326Sek110237 		return (error);
5685326Sek110237 
5695326Sek110237 	/*
5705326Sek110237 	 * Set the objset user_ptr to track its zfsvfs.
5715326Sek110237 	 */
5725326Sek110237 	mutex_enter(&zfsvfs->z_os->os->os_user_ptr_lock);
5735326Sek110237 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
5745326Sek110237 	mutex_exit(&zfsvfs->z_os->os->os_user_ptr_lock);
5755326Sek110237 
5765326Sek110237 	/*
5775326Sek110237 	 * If we are not mounting (ie: online recv), then we don't
5785326Sek110237 	 * have to worry about replaying the log as we blocked all
5795326Sek110237 	 * operations out since we closed the ZIL.
5805326Sek110237 	 */
5815326Sek110237 	if (mounting) {
5825326Sek110237 		/*
5835326Sek110237 		 * During replay we remove the read only flag to
5845326Sek110237 		 * allow replays to succeed.
5855326Sek110237 		 */
5865326Sek110237 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
5875326Sek110237 		if (readonly != 0)
5885326Sek110237 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
5895326Sek110237 		else
5905326Sek110237 			zfs_unlinked_drain(zfsvfs);
5915326Sek110237 
5925326Sek110237 		/*
5935326Sek110237 		 * Parse and replay the intent log.
5945326Sek110237 		 *
5955326Sek110237 		 * Because of ziltest, this must be done after
5965326Sek110237 		 * zfs_unlinked_drain().  (Further note: ziltest doesn't
5975326Sek110237 		 * use readonly mounts, where zfs_unlinked_drain() isn't
5985326Sek110237 		 * called.)  This is because ziltest causes spa_sync()
5995326Sek110237 		 * to think it's committed, but actually it is not, so
6005326Sek110237 		 * the intent log contains many txg's worth of changes.
6015326Sek110237 		 *
6025326Sek110237 		 * In particular, if object N is in the unlinked set in
6035326Sek110237 		 * the last txg to actually sync, then it could be
6045326Sek110237 		 * actually freed in a later txg and then reallocated in
6055326Sek110237 		 * a yet later txg.  This would write a "create object
6065326Sek110237 		 * N" record to the intent log.  Normally, this would be
6075326Sek110237 		 * fine because the spa_sync() would have written out
6085326Sek110237 		 * the fact that object N is free, before we could write
6095326Sek110237 		 * the "create object N" intent log record.
6105326Sek110237 		 *
6115326Sek110237 		 * But when we are in ziltest mode, we advance the "open
6125326Sek110237 		 * txg" without actually spa_sync()-ing the changes to
6135326Sek110237 		 * disk.  So we would see that object N is still
6145326Sek110237 		 * allocated and in the unlinked set, and there is an
6155326Sek110237 		 * intent log record saying to allocate it.
6165326Sek110237 		 */
6175326Sek110237 		zil_replay(zfsvfs->z_os, zfsvfs, &zfsvfs->z_assign,
6185326Sek110237 		    zfs_replay_vector);
6195326Sek110237 
6205326Sek110237 		zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
6215326Sek110237 	}
6225326Sek110237 
6235326Sek110237 	if (!zil_disable)
6245326Sek110237 		zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
6255326Sek110237 
6265326Sek110237 	return (0);
6275326Sek110237 }
6285326Sek110237 
6296083Sek110237 static void
6306083Sek110237 zfs_freezfsvfs(zfsvfs_t *zfsvfs)
6316083Sek110237 {
6326083Sek110237 	mutex_destroy(&zfsvfs->z_znodes_lock);
6336083Sek110237 	mutex_destroy(&zfsvfs->z_online_recv_lock);
6346083Sek110237 	list_destroy(&zfsvfs->z_all_znodes);
6356083Sek110237 	rrw_destroy(&zfsvfs->z_teardown_lock);
6366083Sek110237 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
6376083Sek110237 	rw_destroy(&zfsvfs->z_fuid_lock);
6386083Sek110237 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
6396083Sek110237 }
6406083Sek110237 
6415326Sek110237 static int
6427046Sahrens zfs_domount(vfs_t *vfsp, char *osname)
6431544Seschrock {
6441544Seschrock 	dev_t mount_dev;
6451544Seschrock 	uint64_t recordsize, readonly;
6461544Seschrock 	int error = 0;
6471544Seschrock 	int mode;
6481544Seschrock 	zfsvfs_t *zfsvfs;
6491544Seschrock 	znode_t *zp = NULL;
6501544Seschrock 
6511544Seschrock 	ASSERT(vfsp);
6521544Seschrock 	ASSERT(osname);
6531544Seschrock 
6541544Seschrock 	/*
6551544Seschrock 	 * Initialize the zfs-specific filesystem structure.
6561544Seschrock 	 * Should probably make this a kmem cache, shuffle fields,
6571544Seschrock 	 * and just bzero up to z_hold_mtx[].
6581544Seschrock 	 */
6591544Seschrock 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
6601544Seschrock 	zfsvfs->z_vfs = vfsp;
6611544Seschrock 	zfsvfs->z_parent = zfsvfs;
6621544Seschrock 	zfsvfs->z_assign = TXG_NOWAIT;
6631544Seschrock 	zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
6641544Seschrock 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
6651544Seschrock 
6661544Seschrock 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
6676083Sek110237 	mutex_init(&zfsvfs->z_online_recv_lock, NULL, MUTEX_DEFAULT, NULL);
6681544Seschrock 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
6691544Seschrock 	    offsetof(znode_t, z_link_node));
6705326Sek110237 	rrw_init(&zfsvfs->z_teardown_lock);
6715326Sek110237 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
6725498Stimh 	rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
6731544Seschrock 
6741544Seschrock 	/* Initialize the generic filesystem structure. */
6751544Seschrock 	vfsp->vfs_bcount = 0;
6761544Seschrock 	vfsp->vfs_data = NULL;
6771544Seschrock 
6781544Seschrock 	if (zfs_create_unique_device(&mount_dev) == -1) {
6791544Seschrock 		error = ENODEV;
6801544Seschrock 		goto out;
6811544Seschrock 	}
6821544Seschrock 	ASSERT(vfs_devismounted(mount_dev) == 0);
6831544Seschrock 
6841544Seschrock 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
6851544Seschrock 	    NULL))
6861544Seschrock 		goto out;
6871544Seschrock 
6881544Seschrock 	vfsp->vfs_dev = mount_dev;
6891544Seschrock 	vfsp->vfs_fstype = zfsfstype;
6901544Seschrock 	vfsp->vfs_bsize = recordsize;
6911544Seschrock 	vfsp->vfs_flag |= VFS_NOTRUNC;
6921544Seschrock 	vfsp->vfs_data = zfsvfs;
6931544Seschrock 
6941544Seschrock 	if (error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL))
6951544Seschrock 		goto out;
6961544Seschrock 
6976689Smaybee 	mode = DS_MODE_OWNER;
6981544Seschrock 	if (readonly)
6996689Smaybee 		mode |= DS_MODE_READONLY;
7001544Seschrock 
7011544Seschrock 	error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
7021544Seschrock 	if (error == EROFS) {
7036689Smaybee 		mode = DS_MODE_OWNER | DS_MODE_READONLY;
7041544Seschrock 		error = dmu_objset_open(osname, DMU_OST_ZFS, mode,
7051544Seschrock 		    &zfsvfs->z_os);
7061544Seschrock 	}
7071544Seschrock 
7081544Seschrock 	if (error)
7091544Seschrock 		goto out;
7101544Seschrock 
7117046Sahrens 	if (error = zfs_init_fs(zfsvfs, &zp))
7121544Seschrock 		goto out;
7131544Seschrock 
7141544Seschrock 	/* The call to zfs_init_fs leaves the vnode held, release it here. */
7151544Seschrock 	VN_RELE(ZTOV(zp));
7161544Seschrock 
7175331Samw 	/*
7185331Samw 	 * Set features for file system.
7195331Samw 	 */
7205331Samw 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
7215331Samw 	if (zfsvfs->z_use_fuids) {
7225331Samw 		vfs_set_feature(vfsp, VFSFT_XVATTR);
7235331Samw 		vfs_set_feature(vfsp, VFSFT_ACEMASKONACCESS);
7245331Samw 		vfs_set_feature(vfsp, VFSFT_ACLONCREATE);
7255331Samw 	}
7265498Stimh 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
7275498Stimh 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
7285498Stimh 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
7295498Stimh 		vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
7305498Stimh 	} else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
7315498Stimh 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
7325498Stimh 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
7335498Stimh 	}
7345331Samw 
7351544Seschrock 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
7365331Samw 		uint64_t pval;
7373234Sck153898 
7381544Seschrock 		ASSERT(mode & DS_MODE_READONLY);
7391544Seschrock 		atime_changed_cb(zfsvfs, B_FALSE);
7401544Seschrock 		readonly_changed_cb(zfsvfs, B_TRUE);
7415331Samw 		if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
7423234Sck153898 			goto out;
7435331Samw 		xattr_changed_cb(zfsvfs, pval);
7441544Seschrock 		zfsvfs->z_issnap = B_TRUE;
7451544Seschrock 	} else {
7465326Sek110237 		error = zfsvfs_setup(zfsvfs, B_TRUE);
7471544Seschrock 	}
7481544Seschrock 
7491544Seschrock 	if (!zfsvfs->z_issnap)
7501544Seschrock 		zfsctl_create(zfsvfs);
7511544Seschrock out:
7521544Seschrock 	if (error) {
7531544Seschrock 		if (zfsvfs->z_os)
7541544Seschrock 			dmu_objset_close(zfsvfs->z_os);
7556083Sek110237 		zfs_freezfsvfs(zfsvfs);
7561544Seschrock 	} else {
7571544Seschrock 		atomic_add_32(&zfs_active_fs_count, 1);
7581544Seschrock 	}
7591544Seschrock 
7601544Seschrock 	return (error);
7611544Seschrock }
7621544Seschrock 
7631544Seschrock void
7641544Seschrock zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
7651544Seschrock {
7661544Seschrock 	objset_t *os = zfsvfs->z_os;
7671544Seschrock 	struct dsl_dataset *ds;
7681544Seschrock 
7691544Seschrock 	/*
7701544Seschrock 	 * Unregister properties.
7711544Seschrock 	 */
7721544Seschrock 	if (!dmu_objset_is_snapshot(os)) {
7731544Seschrock 		ds = dmu_objset_ds(os);
7741544Seschrock 		VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
7751544Seschrock 		    zfsvfs) == 0);
7761544Seschrock 
7773234Sck153898 		VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
7783234Sck153898 		    zfsvfs) == 0);
7793234Sck153898 
7801544Seschrock 		VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
7811544Seschrock 		    zfsvfs) == 0);
7821544Seschrock 
7831544Seschrock 		VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
7841544Seschrock 		    zfsvfs) == 0);
7851544Seschrock 
7861544Seschrock 		VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
7871544Seschrock 		    zfsvfs) == 0);
7881544Seschrock 
7891544Seschrock 		VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
7901544Seschrock 		    zfsvfs) == 0);
7911544Seschrock 
7921544Seschrock 		VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
7931544Seschrock 		    zfsvfs) == 0);
7941544Seschrock 
7951544Seschrock 		VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
7961544Seschrock 		    zfsvfs) == 0);
7971544Seschrock 
7981544Seschrock 		VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
7991544Seschrock 		    zfsvfs) == 0);
8001544Seschrock 
8011544Seschrock 		VERIFY(dsl_prop_unregister(ds, "aclinherit",
8021544Seschrock 		    acl_inherit_changed_cb, zfsvfs) == 0);
8035331Samw 
8045331Samw 		VERIFY(dsl_prop_unregister(ds, "vscan",
8055331Samw 		    vscan_changed_cb, zfsvfs) == 0);
8061544Seschrock 	}
8071544Seschrock }
8081544Seschrock 
8093912Slling /*
8103912Slling  * Convert a decimal digit string to a uint64_t integer.
8113912Slling  */
8123912Slling static int
8133912Slling str_to_uint64(char *str, uint64_t *objnum)
8143912Slling {
8153912Slling 	uint64_t num = 0;
8163912Slling 
8173912Slling 	while (*str) {
8183912Slling 		if (*str < '0' || *str > '9')
8193912Slling 			return (EINVAL);
8203912Slling 
8213912Slling 		num = num*10 + *str++ - '0';
8223912Slling 	}
8233912Slling 
8243912Slling 	*objnum = num;
8253912Slling 	return (0);
8263912Slling }
8273912Slling 
8283912Slling /*
8293912Slling  * The boot path passed from the boot loader is in the form of
8303912Slling  * "rootpool-name/root-filesystem-object-number'. Convert this
8313912Slling  * string to a dataset name: "rootpool-name/root-filesystem-name".
8323912Slling  */
8333912Slling static int
8346423Sgw25295 zfs_parse_bootfs(char *bpath, char *outpath)
8353912Slling {
8363912Slling 	char *slashp;
8373912Slling 	uint64_t objnum;
8383912Slling 	int error;
8393912Slling 
8403912Slling 	if (*bpath == 0 || *bpath == '/')
8413912Slling 		return (EINVAL);
8423912Slling 
8433912Slling 	slashp = strchr(bpath, '/');
8443912Slling 
8453912Slling 	/* if no '/', just return the pool name */
8463912Slling 	if (slashp == NULL) {
8473912Slling 		(void) strcpy(outpath, bpath);
8483912Slling 		return (0);
8493912Slling 	}
8503912Slling 
8513912Slling 	if (error = str_to_uint64(slashp+1, &objnum))
8523912Slling 		return (error);
8533912Slling 
8543912Slling 	*slashp = '\0';
8553912Slling 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
8563912Slling 	*slashp = '/';
8573912Slling 
8583912Slling 	return (error);
8593912Slling }
8603912Slling 
8611544Seschrock static int
8621544Seschrock zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
8631544Seschrock {
8641544Seschrock 	int error = 0;
8651544Seschrock 	static int zfsrootdone = 0;
8661544Seschrock 	zfsvfs_t *zfsvfs = NULL;
8671544Seschrock 	znode_t *zp = NULL;
8681544Seschrock 	vnode_t *vp = NULL;
8696423Sgw25295 	char *zfs_bootfs;
8707147Staylor 	char *zfs_devid;
8711544Seschrock 
8721544Seschrock 	ASSERT(vfsp);
8731544Seschrock 
8741544Seschrock 	/*
8753912Slling 	 * The filesystem that we mount as root is defined in the
8766423Sgw25295 	 * boot property "zfs-bootfs" with a format of
8776423Sgw25295 	 * "poolname/root-dataset-objnum".
8781544Seschrock 	 */
8791544Seschrock 	if (why == ROOT_INIT) {
8801544Seschrock 		if (zfsrootdone++)
8811544Seschrock 			return (EBUSY);
8826423Sgw25295 		/*
8836423Sgw25295 		 * the process of doing a spa_load will require the
8846423Sgw25295 		 * clock to be set before we could (for example) do
8856423Sgw25295 		 * something better by looking at the timestamp on
8866423Sgw25295 		 * an uberblock, so just set it to -1.
8876423Sgw25295 		 */
8886423Sgw25295 		clkset(-1);
8891544Seschrock 
8907147Staylor 		if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
8917147Staylor 			cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
8927147Staylor 			    "bootfs name");
8936423Sgw25295 			return (EINVAL);
8945648Ssetje 		}
8957147Staylor 		zfs_devid = spa_get_bootprop("diskdevid");
8967147Staylor 		error = spa_import_rootpool(rootfs.bo_name, zfs_devid);
8977147Staylor 		if (zfs_devid)
8987147Staylor 			spa_free_bootprop(zfs_devid);
8997147Staylor 		if (error) {
9007147Staylor 			spa_free_bootprop(zfs_bootfs);
9017147Staylor 			cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
9027147Staylor 			    error);
9037147Staylor 			return (error);
9047147Staylor 		}
9057147Staylor 		if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
9067147Staylor 			spa_free_bootprop(zfs_bootfs);
9077147Staylor 			cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
9086423Sgw25295 			    error);
9096423Sgw25295 			return (error);
9106423Sgw25295 		}
9113912Slling 
9127147Staylor 		spa_free_bootprop(zfs_bootfs);
9131544Seschrock 
9141544Seschrock 		if (error = vfs_lock(vfsp))
9151544Seschrock 			return (error);
9161544Seschrock 
9177046Sahrens 		if (error = zfs_domount(vfsp, rootfs.bo_name)) {
9187147Staylor 			cmn_err(CE_NOTE, "zfs_domount: error %d", error);
9191544Seschrock 			goto out;
9206423Sgw25295 		}
9211544Seschrock 
9221544Seschrock 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
9231544Seschrock 		ASSERT(zfsvfs);
9246423Sgw25295 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
9257147Staylor 			cmn_err(CE_NOTE, "zfs_zget: error %d", error);
9261544Seschrock 			goto out;
9276423Sgw25295 		}
9281544Seschrock 
9291544Seschrock 		vp = ZTOV(zp);
9301544Seschrock 		mutex_enter(&vp->v_lock);
9311544Seschrock 		vp->v_flag |= VROOT;
9321544Seschrock 		mutex_exit(&vp->v_lock);
9331544Seschrock 		rootvp = vp;
9341544Seschrock 
9351544Seschrock 		/*
9366570Smarks 		 * Leave rootvp held.  The root file system is never unmounted.
9371544Seschrock 		 */
9381544Seschrock 
9391544Seschrock 		vfs_add((struct vnode *)0, vfsp,
9401544Seschrock 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
9411544Seschrock out:
9421544Seschrock 		vfs_unlock(vfsp);
9436423Sgw25295 		return (error);
9441544Seschrock 	} else if (why == ROOT_REMOUNT) {
9451544Seschrock 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
9461544Seschrock 		vfsp->vfs_flag |= VFS_REMOUNT;
9474596Slling 
9484596Slling 		/* refresh mount options */
9494596Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
9504596Slling 		return (zfs_register_callbacks(vfsp));
9514596Slling 
9521544Seschrock 	} else if (why == ROOT_UNMOUNT) {
9531544Seschrock 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
9541544Seschrock 		(void) zfs_sync(vfsp, 0, 0);
9551544Seschrock 		return (0);
9561544Seschrock 	}
9571544Seschrock 
9581544Seschrock 	/*
9591544Seschrock 	 * if "why" is equal to anything else other than ROOT_INIT,
9601544Seschrock 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
9611544Seschrock 	 */
9621544Seschrock 	return (ENOTSUP);
9631544Seschrock }
9641544Seschrock 
965789Sahrens /*ARGSUSED*/
966789Sahrens static int
967789Sahrens zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
968789Sahrens {
969789Sahrens 	char		*osname;
970789Sahrens 	pathname_t	spn;
971789Sahrens 	int		error = 0;
972789Sahrens 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
9733912Slling 	    UIO_SYSSPACE : UIO_USERSPACE;
974789Sahrens 	int		canwrite;
975789Sahrens 
976789Sahrens 	if (mvp->v_type != VDIR)
977789Sahrens 		return (ENOTDIR);
978789Sahrens 
979789Sahrens 	mutex_enter(&mvp->v_lock);
980789Sahrens 	if ((uap->flags & MS_REMOUNT) == 0 &&
981789Sahrens 	    (uap->flags & MS_OVERLAY) == 0 &&
982789Sahrens 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
983789Sahrens 		mutex_exit(&mvp->v_lock);
984789Sahrens 		return (EBUSY);
985789Sahrens 	}
986789Sahrens 	mutex_exit(&mvp->v_lock);
987789Sahrens 
988789Sahrens 	/*
989789Sahrens 	 * ZFS does not support passing unparsed data in via MS_DATA.
990789Sahrens 	 * Users should use the MS_OPTIONSTR interface; this means
991789Sahrens 	 * that all option parsing is already done and the options struct
992789Sahrens 	 * can be interrogated.
993789Sahrens 	 */
994789Sahrens 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
995789Sahrens 		return (EINVAL);
996789Sahrens 
997789Sahrens 	/*
998789Sahrens 	 * Get the objset name (the "special" mount argument).
999789Sahrens 	 */
1000789Sahrens 	if (error = pn_get(uap->spec, fromspace, &spn))
1001789Sahrens 		return (error);
1002789Sahrens 
1003789Sahrens 	osname = spn.pn_path;
1004789Sahrens 
10054543Smarks 	/*
10064543Smarks 	 * Check for mount privilege?
10074543Smarks 	 *
10084543Smarks 	 * If we don't have privilege then see if
10094543Smarks 	 * we have local permission to allow it
10104543Smarks 	 */
10114543Smarks 	error = secpolicy_fs_mount(cr, mvp, vfsp);
10124543Smarks 	if (error) {
10134543Smarks 		error = dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr);
10144543Smarks 		if (error == 0) {
10154543Smarks 			vattr_t		vattr;
10164543Smarks 
10174543Smarks 			/*
10184543Smarks 			 * Make sure user is the owner of the mount point
10194543Smarks 			 * or has sufficient privileges.
10204543Smarks 			 */
10214543Smarks 
10224543Smarks 			vattr.va_mask = AT_UID;
10234543Smarks 
10245331Samw 			if (error = VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
10254543Smarks 				goto out;
10264543Smarks 			}
10274543Smarks 
10285489Smarks 			if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 &&
10295489Smarks 			    VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) {
10305489Smarks 				error = EPERM;
10314543Smarks 				goto out;
10324543Smarks 			}
10334543Smarks 
10344543Smarks 			secpolicy_fs_mount_clearopts(cr, vfsp);
10354543Smarks 		} else {
10364543Smarks 			goto out;
10374543Smarks 		}
10384543Smarks 	}
1039789Sahrens 
1040789Sahrens 	/*
1041789Sahrens 	 * Refuse to mount a filesystem if we are in a local zone and the
1042789Sahrens 	 * dataset is not visible.
1043789Sahrens 	 */
1044789Sahrens 	if (!INGLOBALZONE(curproc) &&
1045789Sahrens 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1046789Sahrens 		error = EPERM;
1047789Sahrens 		goto out;
1048789Sahrens 	}
1049789Sahrens 
10504596Slling 	/*
10514596Slling 	 * When doing a remount, we simply refresh our temporary properties
10524596Slling 	 * according to those options set in the current VFS options.
10534596Slling 	 */
10544596Slling 	if (uap->flags & MS_REMOUNT) {
10554596Slling 		/* refresh mount options */
10564596Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
10574596Slling 		error = zfs_register_callbacks(vfsp);
10584596Slling 		goto out;
10594596Slling 	}
10604596Slling 
10617046Sahrens 	error = zfs_domount(vfsp, osname);
1062789Sahrens 
1063789Sahrens out:
1064789Sahrens 	pn_free(&spn);
1065789Sahrens 	return (error);
1066789Sahrens }
1067789Sahrens 
1068789Sahrens static int
1069789Sahrens zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1070789Sahrens {
1071789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1072789Sahrens 	dev32_t d32;
10732885Sahrens 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
1074789Sahrens 
1075789Sahrens 	ZFS_ENTER(zfsvfs);
1076789Sahrens 
10772885Sahrens 	dmu_objset_space(zfsvfs->z_os,
10782885Sahrens 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
1079789Sahrens 
1080789Sahrens 	/*
1081789Sahrens 	 * The underlying storage pool actually uses multiple block sizes.
1082789Sahrens 	 * We report the fragsize as the smallest block size we support,
1083789Sahrens 	 * and we report our blocksize as the filesystem's maximum blocksize.
1084789Sahrens 	 */
1085789Sahrens 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1086789Sahrens 	statp->f_bsize = zfsvfs->z_max_blksz;
1087789Sahrens 
1088789Sahrens 	/*
1089789Sahrens 	 * The following report "total" blocks of various kinds in the
1090789Sahrens 	 * file system, but reported in terms of f_frsize - the
1091789Sahrens 	 * "fragment" size.
1092789Sahrens 	 */
1093789Sahrens 
10942885Sahrens 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
10952885Sahrens 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
1096789Sahrens 	statp->f_bavail = statp->f_bfree; /* no root reservation */
1097789Sahrens 
1098789Sahrens 	/*
1099789Sahrens 	 * statvfs() should really be called statufs(), because it assumes
1100789Sahrens 	 * static metadata.  ZFS doesn't preallocate files, so the best
1101789Sahrens 	 * we can do is report the max that could possibly fit in f_files,
1102789Sahrens 	 * and that minus the number actually used in f_ffree.
1103789Sahrens 	 * For f_ffree, report the smaller of the number of object available
1104789Sahrens 	 * and the number of blocks (each object will take at least a block).
1105789Sahrens 	 */
11062885Sahrens 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
1107789Sahrens 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
11082885Sahrens 	statp->f_files = statp->f_ffree + usedobjs;
1109789Sahrens 
1110789Sahrens 	(void) cmpldev(&d32, vfsp->vfs_dev);
1111789Sahrens 	statp->f_fsid = d32;
1112789Sahrens 
1113789Sahrens 	/*
1114789Sahrens 	 * We're a zfs filesystem.
1115789Sahrens 	 */
1116789Sahrens 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1117789Sahrens 
11181123Smarks 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1119789Sahrens 
1120789Sahrens 	statp->f_namemax = ZFS_MAXNAMELEN;
1121789Sahrens 
1122789Sahrens 	/*
1123789Sahrens 	 * We have all of 32 characters to stuff a string here.
1124789Sahrens 	 * Is there anything useful we could/should provide?
1125789Sahrens 	 */
1126789Sahrens 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
1127789Sahrens 
1128789Sahrens 	ZFS_EXIT(zfsvfs);
1129789Sahrens 	return (0);
1130789Sahrens }
1131789Sahrens 
1132789Sahrens static int
1133789Sahrens zfs_root(vfs_t *vfsp, vnode_t **vpp)
1134789Sahrens {
1135789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1136789Sahrens 	znode_t *rootzp;
1137789Sahrens 	int error;
1138789Sahrens 
1139789Sahrens 	ZFS_ENTER(zfsvfs);
1140789Sahrens 
1141789Sahrens 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1142789Sahrens 	if (error == 0)
1143789Sahrens 		*vpp = ZTOV(rootzp);
1144789Sahrens 
1145789Sahrens 	ZFS_EXIT(zfsvfs);
1146789Sahrens 	return (error);
1147789Sahrens }
1148789Sahrens 
11495326Sek110237 /*
11505326Sek110237  * Teardown the zfsvfs::z_os.
11515326Sek110237  *
11525326Sek110237  * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
11535326Sek110237  * and 'z_teardown_inactive_lock' held.
11545326Sek110237  */
11555326Sek110237 static int
11565326Sek110237 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
11575326Sek110237 {
11585642Smaybee 	znode_t	*zp;
11595326Sek110237 
11605326Sek110237 	rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
11615326Sek110237 
11625326Sek110237 	if (!unmounting) {
11635326Sek110237 		/*
11645326Sek110237 		 * We purge the parent filesystem's vfsp as the parent
11655326Sek110237 		 * filesystem and all of its snapshots have their vnode's
11665326Sek110237 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
11675326Sek110237 		 * 'z_parent' is self referential for non-snapshots.
11685326Sek110237 		 */
11695326Sek110237 		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
11705326Sek110237 	}
11715326Sek110237 
11725326Sek110237 	/*
11735326Sek110237 	 * Close the zil. NB: Can't close the zil while zfs_inactive
11745326Sek110237 	 * threads are blocked as zil_close can call zfs_inactive.
11755326Sek110237 	 */
11765326Sek110237 	if (zfsvfs->z_log) {
11775326Sek110237 		zil_close(zfsvfs->z_log);
11785326Sek110237 		zfsvfs->z_log = NULL;
11795326Sek110237 	}
11805326Sek110237 
11815326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
11825326Sek110237 
11835326Sek110237 	/*
11845326Sek110237 	 * If we are not unmounting (ie: online recv) and someone already
11855326Sek110237 	 * unmounted this file system while we were doing the switcheroo,
11865326Sek110237 	 * or a reopen of z_os failed then just bail out now.
11875326Sek110237 	 */
11885326Sek110237 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
11895326Sek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
11905326Sek110237 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
11915326Sek110237 		return (EIO);
11925326Sek110237 	}
11935326Sek110237 
11945326Sek110237 	/*
11955326Sek110237 	 * At this point there are no vops active, and any new vops will
11965326Sek110237 	 * fail with EIO since we have z_teardown_lock for writer (only
11975326Sek110237 	 * relavent for forced unmount).
11985326Sek110237 	 *
11995326Sek110237 	 * Release all holds on dbufs.
12005326Sek110237 	 */
12015326Sek110237 	mutex_enter(&zfsvfs->z_znodes_lock);
12025642Smaybee 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
12035642Smaybee 	    zp = list_next(&zfsvfs->z_all_znodes, zp))
12045446Sahrens 		if (zp->z_dbuf) {
12055642Smaybee 			ASSERT(ZTOV(zp)->v_count > 0);
12065642Smaybee 			zfs_znode_dmu_fini(zp);
12075326Sek110237 		}
12085326Sek110237 	mutex_exit(&zfsvfs->z_znodes_lock);
12095326Sek110237 
12105326Sek110237 	/*
12115326Sek110237 	 * If we are unmounting, set the unmounted flag and let new vops
12125326Sek110237 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
12135326Sek110237 	 * other vops will fail with EIO.
12145326Sek110237 	 */
12155326Sek110237 	if (unmounting) {
12165326Sek110237 		zfsvfs->z_unmounted = B_TRUE;
12175326Sek110237 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
12185326Sek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
12195326Sek110237 	}
12205326Sek110237 
12215326Sek110237 	/*
12225326Sek110237 	 * z_os will be NULL if there was an error in attempting to reopen
12235326Sek110237 	 * zfsvfs, so just return as the properties had already been
12245326Sek110237 	 * unregistered and cached data had been evicted before.
12255326Sek110237 	 */
12265326Sek110237 	if (zfsvfs->z_os == NULL)
12275326Sek110237 		return (0);
12285326Sek110237 
12295326Sek110237 	/*
12305326Sek110237 	 * Unregister properties.
12315326Sek110237 	 */
12325326Sek110237 	zfs_unregister_callbacks(zfsvfs);
12335326Sek110237 
12345326Sek110237 	/*
12355326Sek110237 	 * Evict cached data
12365326Sek110237 	 */
12376083Sek110237 	if (dmu_objset_evict_dbufs(zfsvfs->z_os)) {
12385429Smaybee 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
12396083Sek110237 		(void) dmu_objset_evict_dbufs(zfsvfs->z_os);
12405429Smaybee 	}
12415326Sek110237 
12425326Sek110237 	return (0);
12435326Sek110237 }
12445326Sek110237 
1245789Sahrens /*ARGSUSED*/
1246789Sahrens static int
1247789Sahrens zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1248789Sahrens {
1249789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
12505326Sek110237 	objset_t *os;
1251789Sahrens 	int ret;
1252789Sahrens 
12534543Smarks 	ret = secpolicy_fs_unmount(cr, vfsp);
12544543Smarks 	if (ret) {
12554543Smarks 		ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
12564543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
12574543Smarks 		if (ret)
12584543Smarks 			return (ret);
12594543Smarks 	}
12601484Sek110237 
12614736Sek110237 	/*
12624736Sek110237 	 * We purge the parent filesystem's vfsp as the parent filesystem
12634736Sek110237 	 * and all of its snapshots have their vnode's v_vfsp set to the
12644736Sek110237 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
12654736Sek110237 	 * referential for non-snapshots.
12664736Sek110237 	 */
12674736Sek110237 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
12681484Sek110237 
1269789Sahrens 	/*
1270789Sahrens 	 * Unmount any snapshots mounted under .zfs before unmounting the
1271789Sahrens 	 * dataset itself.
1272789Sahrens 	 */
1273789Sahrens 	if (zfsvfs->z_ctldir != NULL &&
12744543Smarks 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1275789Sahrens 		return (ret);
12764543Smarks 	}
1277789Sahrens 
12784787Sahrens 	if (!(fflag & MS_FORCE)) {
12794480Sgw25295 		/*
12804787Sahrens 		 * Check the number of active vnodes in the file system.
12814787Sahrens 		 * Our count is maintained in the vfs structure, but the
12824787Sahrens 		 * number is off by 1 to indicate a hold on the vfs
12834787Sahrens 		 * structure itself.
12844787Sahrens 		 *
12854787Sahrens 		 * The '.zfs' directory maintains a reference of its
12864787Sahrens 		 * own, and any active references underneath are
12874787Sahrens 		 * reflected in the vnode count.
1288789Sahrens 		 */
12894787Sahrens 		if (zfsvfs->z_ctldir == NULL) {
12904787Sahrens 			if (vfsp->vfs_count > 1)
12914787Sahrens 				return (EBUSY);
12924787Sahrens 		} else {
12934787Sahrens 			if (vfsp->vfs_count > 2 ||
12945326Sek110237 			    zfsvfs->z_ctldir->v_count > 1)
12954787Sahrens 				return (EBUSY);
1296789Sahrens 		}
1297789Sahrens 	}
1298789Sahrens 
1299789Sahrens 	vfsp->vfs_flag |= VFS_UNMOUNTED;
13004787Sahrens 
13015326Sek110237 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
13025326Sek110237 	os = zfsvfs->z_os;
13034787Sahrens 
13044787Sahrens 	/*
13055326Sek110237 	 * z_os will be NULL if there was an error in
13065326Sek110237 	 * attempting to reopen zfsvfs.
13074787Sahrens 	 */
13085326Sek110237 	if (os != NULL) {
13095326Sek110237 		/*
13105326Sek110237 		 * Unset the objset user_ptr.
13115326Sek110237 		 */
13125326Sek110237 		mutex_enter(&os->os->os_user_ptr_lock);
13135326Sek110237 		dmu_objset_set_user(os, NULL);
13145326Sek110237 		mutex_exit(&os->os->os_user_ptr_lock);
13154787Sahrens 
13165326Sek110237 		/*
13176689Smaybee 		 * Finally release the objset
13185326Sek110237 		 */
13195326Sek110237 		dmu_objset_close(os);
13204787Sahrens 	}
13214787Sahrens 
13224787Sahrens 	/*
13234787Sahrens 	 * We can now safely destroy the '.zfs' directory node.
13244787Sahrens 	 */
13254787Sahrens 	if (zfsvfs->z_ctldir != NULL)
13264787Sahrens 		zfsctl_destroy(zfsvfs);
1327789Sahrens 
1328789Sahrens 	return (0);
1329789Sahrens }
1330789Sahrens 
1331789Sahrens static int
1332789Sahrens zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1333789Sahrens {
1334789Sahrens 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1335789Sahrens 	znode_t		*zp;
1336789Sahrens 	uint64_t	object = 0;
1337789Sahrens 	uint64_t	fid_gen = 0;
1338789Sahrens 	uint64_t	gen_mask;
1339789Sahrens 	uint64_t	zp_gen;
1340789Sahrens 	int 		i, err;
1341789Sahrens 
1342789Sahrens 	*vpp = NULL;
1343789Sahrens 
1344789Sahrens 	ZFS_ENTER(zfsvfs);
1345789Sahrens 
1346789Sahrens 	if (fidp->fid_len == LONG_FID_LEN) {
1347789Sahrens 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1348789Sahrens 		uint64_t	objsetid = 0;
1349789Sahrens 		uint64_t	setgen = 0;
1350789Sahrens 
1351789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1352789Sahrens 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1353789Sahrens 
1354789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1355789Sahrens 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1356789Sahrens 
1357789Sahrens 		ZFS_EXIT(zfsvfs);
1358789Sahrens 
1359789Sahrens 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1360789Sahrens 		if (err)
1361789Sahrens 			return (EINVAL);
1362789Sahrens 		ZFS_ENTER(zfsvfs);
1363789Sahrens 	}
1364789Sahrens 
1365789Sahrens 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1366789Sahrens 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1367789Sahrens 
1368789Sahrens 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1369789Sahrens 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1370789Sahrens 
1371789Sahrens 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1372789Sahrens 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1373789Sahrens 	} else {
1374789Sahrens 		ZFS_EXIT(zfsvfs);
1375789Sahrens 		return (EINVAL);
1376789Sahrens 	}
1377789Sahrens 
1378789Sahrens 	/* A zero fid_gen means we are in the .zfs control directories */
1379789Sahrens 	if (fid_gen == 0 &&
1380789Sahrens 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1381789Sahrens 		*vpp = zfsvfs->z_ctldir;
1382789Sahrens 		ASSERT(*vpp != NULL);
1383789Sahrens 		if (object == ZFSCTL_INO_SNAPDIR) {
1384789Sahrens 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
13855331Samw 			    0, NULL, NULL, NULL, NULL, NULL) == 0);
1386789Sahrens 		} else {
1387789Sahrens 			VN_HOLD(*vpp);
1388789Sahrens 		}
1389789Sahrens 		ZFS_EXIT(zfsvfs);
1390789Sahrens 		return (0);
1391789Sahrens 	}
1392789Sahrens 
1393789Sahrens 	gen_mask = -1ULL >> (64 - 8 * i);
1394789Sahrens 
1395789Sahrens 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1396789Sahrens 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1397789Sahrens 		ZFS_EXIT(zfsvfs);
1398789Sahrens 		return (err);
1399789Sahrens 	}
1400789Sahrens 	zp_gen = zp->z_phys->zp_gen & gen_mask;
1401789Sahrens 	if (zp_gen == 0)
1402789Sahrens 		zp_gen = 1;
14033461Sahrens 	if (zp->z_unlinked || zp_gen != fid_gen) {
1404789Sahrens 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1405789Sahrens 		VN_RELE(ZTOV(zp));
1406789Sahrens 		ZFS_EXIT(zfsvfs);
1407789Sahrens 		return (EINVAL);
1408789Sahrens 	}
1409789Sahrens 
1410789Sahrens 	*vpp = ZTOV(zp);
1411789Sahrens 	ZFS_EXIT(zfsvfs);
1412789Sahrens 	return (0);
1413789Sahrens }
1414789Sahrens 
14155326Sek110237 /*
14165326Sek110237  * Block out VOPs and close zfsvfs_t::z_os
14175326Sek110237  *
14185326Sek110237  * Note, if successful, then we return with the 'z_teardown_lock' and
14195326Sek110237  * 'z_teardown_inactive_lock' write held.
14205326Sek110237  */
14215326Sek110237 int
14225326Sek110237 zfs_suspend_fs(zfsvfs_t *zfsvfs, char *name, int *mode)
14235326Sek110237 {
14245326Sek110237 	int error;
14255326Sek110237 
14265326Sek110237 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
14275326Sek110237 		return (error);
14285326Sek110237 
14295326Sek110237 	*mode = zfsvfs->z_os->os_mode;
14305326Sek110237 	dmu_objset_name(zfsvfs->z_os, name);
14315326Sek110237 	dmu_objset_close(zfsvfs->z_os);
14325326Sek110237 
14335326Sek110237 	return (0);
14345326Sek110237 }
14355326Sek110237 
14365326Sek110237 /*
14375326Sek110237  * Reopen zfsvfs_t::z_os and release VOPs.
14385326Sek110237  */
14395326Sek110237 int
14405326Sek110237 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname, int mode)
14415326Sek110237 {
14425326Sek110237 	int err;
14435326Sek110237 
14445326Sek110237 	ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
14455326Sek110237 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
14465326Sek110237 
14475326Sek110237 	err = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
14485326Sek110237 	if (err) {
14495326Sek110237 		zfsvfs->z_os = NULL;
14505326Sek110237 	} else {
14515326Sek110237 		znode_t *zp;
14525326Sek110237 
14535326Sek110237 		VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
14545326Sek110237 
14555326Sek110237 		/*
14565326Sek110237 		 * Attempt to re-establish all the active znodes with
14575326Sek110237 		 * their dbufs.  If a zfs_rezget() fails, then we'll let
14585326Sek110237 		 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
14595326Sek110237 		 * when they try to use their znode.
14605326Sek110237 		 */
14615326Sek110237 		mutex_enter(&zfsvfs->z_znodes_lock);
14625326Sek110237 		for (zp = list_head(&zfsvfs->z_all_znodes); zp;
14635326Sek110237 		    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
14645326Sek110237 			(void) zfs_rezget(zp);
14655326Sek110237 		}
14665326Sek110237 		mutex_exit(&zfsvfs->z_znodes_lock);
14675326Sek110237 
14685326Sek110237 	}
14695326Sek110237 
14705326Sek110237 	/* release the VOPs */
14715326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
14725326Sek110237 	rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
14735326Sek110237 
14745326Sek110237 	if (err) {
14755326Sek110237 		/*
14765326Sek110237 		 * Since we couldn't reopen zfsvfs::z_os, force
14775326Sek110237 		 * unmount this file system.
14785326Sek110237 		 */
14795326Sek110237 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
14805326Sek110237 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
14815326Sek110237 	}
14825326Sek110237 	return (err);
14835326Sek110237 }
14845326Sek110237 
1485789Sahrens static void
1486789Sahrens zfs_freevfs(vfs_t *vfsp)
1487789Sahrens {
1488789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
14894831Sgw25295 	int i;
14904831Sgw25295 
14914831Sgw25295 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
14924831Sgw25295 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1493789Sahrens 
14945331Samw 	zfs_fuid_destroy(zfsvfs);
14956083Sek110237 	zfs_freezfsvfs(zfsvfs);
1496789Sahrens 
1497789Sahrens 	atomic_add_32(&zfs_active_fs_count, -1);
1498789Sahrens }
1499789Sahrens 
1500789Sahrens /*
1501789Sahrens  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
1502789Sahrens  * so we can't safely do any non-idempotent initialization here.
1503789Sahrens  * Leave that to zfs_init() and zfs_fini(), which are called
1504789Sahrens  * from the module's _init() and _fini() entry points.
1505789Sahrens  */
1506789Sahrens /*ARGSUSED*/
1507789Sahrens static int
1508789Sahrens zfs_vfsinit(int fstype, char *name)
1509789Sahrens {
1510789Sahrens 	int error;
1511789Sahrens 
1512789Sahrens 	zfsfstype = fstype;
1513789Sahrens 
1514789Sahrens 	/*
1515789Sahrens 	 * Setup vfsops and vnodeops tables.
1516789Sahrens 	 */
1517789Sahrens 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
1518789Sahrens 	if (error != 0) {
1519789Sahrens 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
1520789Sahrens 	}
1521789Sahrens 
1522789Sahrens 	error = zfs_create_op_tables();
1523789Sahrens 	if (error) {
1524789Sahrens 		zfs_remove_op_tables();
1525789Sahrens 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
1526789Sahrens 		(void) vfs_freevfsops_by_type(zfsfstype);
1527789Sahrens 		return (error);
1528789Sahrens 	}
1529789Sahrens 
1530789Sahrens 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
1531789Sahrens 
1532789Sahrens 	/*
1533849Sbonwick 	 * Unique major number for all zfs mounts.
1534849Sbonwick 	 * If we run out of 32-bit minors, we'll getudev() another major.
1535789Sahrens 	 */
1536849Sbonwick 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
1537849Sbonwick 	zfs_minor = ZFS_MIN_MINOR;
1538789Sahrens 
1539789Sahrens 	return (0);
1540789Sahrens }
1541789Sahrens 
1542789Sahrens void
1543789Sahrens zfs_init(void)
1544789Sahrens {
1545789Sahrens 	/*
1546789Sahrens 	 * Initialize .zfs directory structures
1547789Sahrens 	 */
1548789Sahrens 	zfsctl_init();
1549789Sahrens 
1550789Sahrens 	/*
1551789Sahrens 	 * Initialize znode cache, vnode ops, etc...
1552789Sahrens 	 */
1553789Sahrens 	zfs_znode_init();
1554789Sahrens }
1555789Sahrens 
1556789Sahrens void
1557789Sahrens zfs_fini(void)
1558789Sahrens {
1559789Sahrens 	zfsctl_fini();
1560789Sahrens 	zfs_znode_fini();
1561789Sahrens }
1562789Sahrens 
1563789Sahrens int
1564789Sahrens zfs_busy(void)
1565789Sahrens {
1566789Sahrens 	return (zfs_active_fs_count != 0);
1567789Sahrens }
1568789Sahrens 
15694577Sahrens int
15704577Sahrens zfs_set_version(const char *name, uint64_t newvers)
15714577Sahrens {
15724577Sahrens 	int error;
15734577Sahrens 	objset_t *os;
15744577Sahrens 	dmu_tx_t *tx;
15754577Sahrens 	uint64_t curvers;
15764577Sahrens 
15774577Sahrens 	/*
15784577Sahrens 	 * XXX for now, require that the filesystem be unmounted.  Would
15794577Sahrens 	 * be nice to find the zfsvfs_t and just update that if
15804577Sahrens 	 * possible.
15814577Sahrens 	 */
15824577Sahrens 
15834577Sahrens 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
15844577Sahrens 		return (EINVAL);
15854577Sahrens 
15866689Smaybee 	error = dmu_objset_open(name, DMU_OST_ZFS, DS_MODE_OWNER, &os);
15874577Sahrens 	if (error)
15884577Sahrens 		return (error);
15894577Sahrens 
15904577Sahrens 	error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
15914577Sahrens 	    8, 1, &curvers);
15924577Sahrens 	if (error)
15934577Sahrens 		goto out;
15944577Sahrens 	if (newvers < curvers) {
15954577Sahrens 		error = EINVAL;
15964577Sahrens 		goto out;
15974577Sahrens 	}
15984577Sahrens 
15994577Sahrens 	tx = dmu_tx_create(os);
16004577Sahrens 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 0, ZPL_VERSION_STR);
16014577Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
16024577Sahrens 	if (error) {
16034577Sahrens 		dmu_tx_abort(tx);
16044577Sahrens 		goto out;
16054577Sahrens 	}
16064577Sahrens 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1,
16074577Sahrens 	    &newvers, tx);
16084577Sahrens 
16094577Sahrens 	spa_history_internal_log(LOG_DS_UPGRADE,
16104577Sahrens 	    dmu_objset_spa(os), tx, CRED(),
16114577Sahrens 	    "oldver=%llu newver=%llu dataset = %llu", curvers, newvers,
16124577Sahrens 	    dmu_objset_id(os));
16134577Sahrens 	dmu_tx_commit(tx);
16144577Sahrens 
16154577Sahrens out:
16164577Sahrens 	dmu_objset_close(os);
16174577Sahrens 	return (error);
16184577Sahrens }
16194577Sahrens 
16205498Stimh /*
16215498Stimh  * Read a property stored within the master node.
16225498Stimh  */
16235498Stimh int
16245498Stimh zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
16255498Stimh {
16265498Stimh 	const char *pname;
16277184Stimh 	int error = ENOENT;
16285498Stimh 
16295498Stimh 	/*
16305498Stimh 	 * Look up the file system's value for the property.  For the
16315498Stimh 	 * version property, we look up a slightly different string.
16325498Stimh 	 */
16335498Stimh 	if (prop == ZFS_PROP_VERSION)
16345498Stimh 		pname = ZPL_VERSION_STR;
16355498Stimh 	else
16365498Stimh 		pname = zfs_prop_to_name(prop);
16375498Stimh 
16387184Stimh 	if (os != NULL)
16397184Stimh 		error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
16405498Stimh 
16416404Smaybee 	if (error == ENOENT) {
16425498Stimh 		/* No value set, use the default value */
16435498Stimh 		switch (prop) {
16446404Smaybee 		case ZFS_PROP_VERSION:
16456404Smaybee 			*value = ZPL_VERSION;
16466404Smaybee 			break;
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:
16556404Smaybee 			return (error);
16565498Stimh 		}
16576404Smaybee 		error = 0;
16585498Stimh 	}
16596404Smaybee 	return (error);
16605498Stimh }
16615498Stimh 
1662789Sahrens static vfsdef_t vfw = {
1663789Sahrens 	VFSDEF_VERSION,
1664789Sahrens 	MNTTYPE_ZFS,
1665789Sahrens 	zfs_vfsinit,
16665331Samw 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS|
16675331Samw 	    VSW_XID,
1668789Sahrens 	&zfs_mntopts
1669789Sahrens };
1670789Sahrens 
1671789Sahrens struct modlfs zfs_modlfs = {
16724577Sahrens 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
1673789Sahrens };
1674