xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 5331:3047ad28a67b)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51484Sek110237  * Common Development and Distribution License (the "License").
61484Sek110237  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
223461Sahrens  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
263246Sck153898 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens #include <sys/types.h>
29789Sahrens #include <sys/param.h>
30789Sahrens #include <sys/systm.h>
31789Sahrens #include <sys/sysmacros.h>
32789Sahrens #include <sys/kmem.h>
33789Sahrens #include <sys/pathname.h>
34789Sahrens #include <sys/vnode.h>
35789Sahrens #include <sys/vfs.h>
363898Srsb #include <sys/vfs_opreg.h>
37789Sahrens #include <sys/mntent.h>
38789Sahrens #include <sys/mount.h>
39789Sahrens #include <sys/cmn_err.h>
40789Sahrens #include "fs/fs_subr.h"
41789Sahrens #include <sys/zfs_znode.h>
423461Sahrens #include <sys/zfs_dir.h>
43*5331Samw #include <sys/zfs_i18n.h>
44789Sahrens #include <sys/zil.h>
45789Sahrens #include <sys/fs/zfs.h>
46789Sahrens #include <sys/dmu.h>
47789Sahrens #include <sys/dsl_prop.h>
483912Slling #include <sys/dsl_dataset.h>
494543Smarks #include <sys/dsl_deleg.h>
50789Sahrens #include <sys/spa.h>
51789Sahrens #include <sys/zap.h>
52789Sahrens #include <sys/varargs.h>
53789Sahrens #include <sys/policy.h>
54789Sahrens #include <sys/atomic.h>
55789Sahrens #include <sys/mkdev.h>
56789Sahrens #include <sys/modctl.h>
574543Smarks #include <sys/refstr.h>
58789Sahrens #include <sys/zfs_ioctl.h>
59789Sahrens #include <sys/zfs_ctldir.h>
60*5331Samw #include <sys/zfs_fuid.h>
611544Seschrock #include <sys/bootconf.h>
62849Sbonwick #include <sys/sunddi.h>
631484Sek110237 #include <sys/dnlc.h>
645326Sek110237 #include <sys/dmu_objset.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 
334*5331Samw /*
335*5331Samw  * The nbmand mount option can be changed at mount time.
336*5331Samw  * We can't allow it to be toggled on live file systems or incorrect
337*5331Samw  * behavior may be seen from cifs clients
338*5331Samw  *
339*5331Samw  * This property isn't registered via dsl_prop_register(), but this callback
340*5331Samw  * will be called when a file system is first mounted
341*5331Samw  */
342*5331Samw static void
343*5331Samw nbmand_changed_cb(void *arg, uint64_t newval)
344*5331Samw {
345*5331Samw 	zfsvfs_t *zfsvfs = arg;
346*5331Samw 	if (newval == FALSE) {
347*5331Samw 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
348*5331Samw 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
349*5331Samw 	} else {
350*5331Samw 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
351*5331Samw 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
352*5331Samw 	}
353*5331Samw }
354*5331Samw 
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
364*5331Samw vscan_changed_cb(void *arg, uint64_t newval)
365*5331Samw {
366*5331Samw 	zfsvfs_t *zfsvfs = arg;
367*5331Samw 
368*5331Samw 	zfsvfs->z_vscan = newval;
369*5331Samw }
370*5331Samw 
371*5331Samw 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
388*5331Samw zfs_normalization_set(char *osname, zfsvfs_t *zfsvfs)
389*5331Samw {
390*5331Samw 	uint64_t pval;
391*5331Samw 	int error;
392*5331Samw 
393*5331Samw 	if (zfsvfs->z_version < ZPL_VERSION_FUID)
394*5331Samw 		return (0);
395*5331Samw 
396*5331Samw 	error = dsl_prop_get_integer(osname, "normalization", &pval, NULL);
397*5331Samw 	if (error)
398*5331Samw 		goto normquit;
399*5331Samw 	switch ((int)pval) {
400*5331Samw 	case ZFS_NORMALIZE_NONE:
401*5331Samw 		break;
402*5331Samw 	case ZFS_NORMALIZE_C:
403*5331Samw 		zfsvfs->z_norm |= U8_TEXTPREP_NFC;
404*5331Samw 		break;
405*5331Samw 	case ZFS_NORMALIZE_KC:
406*5331Samw 		zfsvfs->z_norm |= U8_TEXTPREP_NFKC;
407*5331Samw 		break;
408*5331Samw 	case ZFS_NORMALIZE_D:
409*5331Samw 		zfsvfs->z_norm |= U8_TEXTPREP_NFD;
410*5331Samw 		break;
411*5331Samw 	case ZFS_NORMALIZE_KD:
412*5331Samw 		zfsvfs->z_norm |= U8_TEXTPREP_NFKD;
413*5331Samw 		break;
414*5331Samw 	default:
415*5331Samw 		ASSERT(pval <= ZFS_NORMALIZE_KD);
416*5331Samw 		break;
417*5331Samw 	}
418*5331Samw 
419*5331Samw 	error = dsl_prop_get_integer(osname, "utf8only", &pval, NULL);
420*5331Samw 	if (error)
421*5331Samw 		goto normquit;
422*5331Samw 	if (pval)
423*5331Samw 		zfsvfs->z_case |= ZFS_UTF8_ONLY;
424*5331Samw 	else
425*5331Samw 		zfsvfs->z_case &= ~ZFS_UTF8_ONLY;
426*5331Samw 
427*5331Samw 	error = dsl_prop_get_integer(osname, "casesensitivity", &pval, NULL);
428*5331Samw 	if (error)
429*5331Samw 		goto normquit;
430*5331Samw 	vfs_set_feature(zfsvfs->z_vfs, VFSFT_DIRENTFLAGS);
431*5331Samw 	switch ((int)pval) {
432*5331Samw 	case ZFS_CASE_SENSITIVE:
433*5331Samw 		break;
434*5331Samw 	case ZFS_CASE_INSENSITIVE:
435*5331Samw 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
436*5331Samw 		zfsvfs->z_case |= ZFS_CI_ONLY;
437*5331Samw 		vfs_set_feature(zfsvfs->z_vfs, VFSFT_CASEINSENSITIVE);
438*5331Samw 		vfs_set_feature(zfsvfs->z_vfs, VFSFT_NOCASESENSITIVE);
439*5331Samw 		break;
440*5331Samw 	case ZFS_CASE_MIXED:
441*5331Samw 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
442*5331Samw 		zfsvfs->z_case |= ZFS_CI_MIXD;
443*5331Samw 		vfs_set_feature(zfsvfs->z_vfs, VFSFT_CASEINSENSITIVE);
444*5331Samw 		break;
445*5331Samw 	default:
446*5331Samw 		ASSERT(pval <= ZFS_CASE_MIXED);
447*5331Samw 		break;
448*5331Samw 	}
449*5331Samw 
450*5331Samw normquit:
451*5331Samw 	return (error);
452*5331Samw }
453*5331Samw 
454*5331Samw static int
4551544Seschrock zfs_register_callbacks(vfs_t *vfsp)
4561544Seschrock {
4571544Seschrock 	struct dsl_dataset *ds = NULL;
4581544Seschrock 	objset_t *os = NULL;
4591544Seschrock 	zfsvfs_t *zfsvfs = NULL;
460*5331Samw 	uint64_t nbmand;
461*5331Samw 	int readonly, do_readonly = B_FALSE;
462*5331Samw 	int setuid, do_setuid = B_FALSE;
463*5331Samw 	int exec, do_exec = B_FALSE;
464*5331Samw 	int devices, do_devices = B_FALSE;
465*5331Samw 	int xattr, do_xattr = B_FALSE;
466*5331Samw 	int atime, do_atime = B_FALSE;
4671544Seschrock 	int error = 0;
4681544Seschrock 
4691544Seschrock 	ASSERT(vfsp);
4701544Seschrock 	zfsvfs = vfsp->vfs_data;
4711544Seschrock 	ASSERT(zfsvfs);
4721544Seschrock 	os = zfsvfs->z_os;
4731544Seschrock 
4741544Seschrock 	/*
4751544Seschrock 	 * The act of registering our callbacks will destroy any mount
4761544Seschrock 	 * options we may have.  In order to enable temporary overrides
4773234Sck153898 	 * of mount options, we stash away the current values and
4781544Seschrock 	 * restore them after we register the callbacks.
4791544Seschrock 	 */
4801544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
4811544Seschrock 		readonly = B_TRUE;
4821544Seschrock 		do_readonly = B_TRUE;
4831544Seschrock 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
4841544Seschrock 		readonly = B_FALSE;
4851544Seschrock 		do_readonly = B_TRUE;
4861544Seschrock 	}
4871544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
4881544Seschrock 		devices = B_FALSE;
4891544Seschrock 		setuid = B_FALSE;
4901544Seschrock 		do_devices = B_TRUE;
4911544Seschrock 		do_setuid = B_TRUE;
4921544Seschrock 	} else {
4931544Seschrock 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
4941544Seschrock 			devices = B_FALSE;
4951544Seschrock 			do_devices = B_TRUE;
4963912Slling 		} else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
4971544Seschrock 			devices = B_TRUE;
4981544Seschrock 			do_devices = B_TRUE;
4991544Seschrock 		}
5001544Seschrock 
5011544Seschrock 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
5021544Seschrock 			setuid = B_FALSE;
5031544Seschrock 			do_setuid = B_TRUE;
5041544Seschrock 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
5051544Seschrock 			setuid = B_TRUE;
5061544Seschrock 			do_setuid = B_TRUE;
5071544Seschrock 		}
5081544Seschrock 	}
5091544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
5101544Seschrock 		exec = B_FALSE;
5111544Seschrock 		do_exec = B_TRUE;
5121544Seschrock 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
5131544Seschrock 		exec = B_TRUE;
5141544Seschrock 		do_exec = B_TRUE;
5151544Seschrock 	}
5163234Sck153898 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
5173234Sck153898 		xattr = B_FALSE;
5183234Sck153898 		do_xattr = B_TRUE;
5193234Sck153898 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
5203234Sck153898 		xattr = B_TRUE;
5213234Sck153898 		do_xattr = B_TRUE;
5223234Sck153898 	}
5234596Slling 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
5244596Slling 		atime = B_FALSE;
5254596Slling 		do_atime = B_TRUE;
5264596Slling 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
5274596Slling 		atime = B_TRUE;
5284596Slling 		do_atime = B_TRUE;
5294596Slling 	}
5301544Seschrock 
5311544Seschrock 	/*
532*5331Samw 	 * nbmand is a special property.  It can only be changed at
533*5331Samw 	 * mount time.
534*5331Samw 	 *
535*5331Samw 	 * This is weird, but it is documented to only be changeable
536*5331Samw 	 * at mount time.
537*5331Samw 	 */
538*5331Samw 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
539*5331Samw 		nbmand = B_FALSE;
540*5331Samw 	} else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
541*5331Samw 		nbmand = B_TRUE;
542*5331Samw 	} else {
543*5331Samw 		char osname[MAXNAMELEN];
544*5331Samw 
545*5331Samw 		dmu_objset_name(os, osname);
546*5331Samw 		if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
547*5331Samw 		    NULL))
548*5331Samw 		return (error);
549*5331Samw 	}
550*5331Samw 
551*5331Samw 	/*
5521544Seschrock 	 * Register property callbacks.
5531544Seschrock 	 *
5541544Seschrock 	 * It would probably be fine to just check for i/o error from
5551544Seschrock 	 * the first prop_register(), but I guess I like to go
5561544Seschrock 	 * overboard...
5571544Seschrock 	 */
5581544Seschrock 	ds = dmu_objset_ds(os);
5591544Seschrock 	error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
5601544Seschrock 	error = error ? error : dsl_prop_register(ds,
5613234Sck153898 	    "xattr", xattr_changed_cb, zfsvfs);
5623234Sck153898 	error = error ? error : dsl_prop_register(ds,
5631544Seschrock 	    "recordsize", blksz_changed_cb, zfsvfs);
5641544Seschrock 	error = error ? error : dsl_prop_register(ds,
5651544Seschrock 	    "readonly", readonly_changed_cb, zfsvfs);
5661544Seschrock 	error = error ? error : dsl_prop_register(ds,
5671544Seschrock 	    "devices", devices_changed_cb, zfsvfs);
5681544Seschrock 	error = error ? error : dsl_prop_register(ds,
5691544Seschrock 	    "setuid", setuid_changed_cb, zfsvfs);
5701544Seschrock 	error = error ? error : dsl_prop_register(ds,
5711544Seschrock 	    "exec", exec_changed_cb, zfsvfs);
5721544Seschrock 	error = error ? error : dsl_prop_register(ds,
5731544Seschrock 	    "snapdir", snapdir_changed_cb, zfsvfs);
5741544Seschrock 	error = error ? error : dsl_prop_register(ds,
5751544Seschrock 	    "aclmode", acl_mode_changed_cb, zfsvfs);
5761544Seschrock 	error = error ? error : dsl_prop_register(ds,
5771544Seschrock 	    "aclinherit", acl_inherit_changed_cb, zfsvfs);
578*5331Samw 	error = error ? error : dsl_prop_register(ds,
579*5331Samw 	    "vscan", vscan_changed_cb, zfsvfs);
5801544Seschrock 	if (error)
5811544Seschrock 		goto unregister;
5821544Seschrock 
5831544Seschrock 	/*
5841544Seschrock 	 * Invoke our callbacks to restore temporary mount options.
5851544Seschrock 	 */
5861544Seschrock 	if (do_readonly)
5871544Seschrock 		readonly_changed_cb(zfsvfs, readonly);
5881544Seschrock 	if (do_setuid)
5891544Seschrock 		setuid_changed_cb(zfsvfs, setuid);
5901544Seschrock 	if (do_exec)
5911544Seschrock 		exec_changed_cb(zfsvfs, exec);
5921544Seschrock 	if (do_devices)
5931544Seschrock 		devices_changed_cb(zfsvfs, devices);
5943234Sck153898 	if (do_xattr)
5953234Sck153898 		xattr_changed_cb(zfsvfs, xattr);
5964596Slling 	if (do_atime)
5974596Slling 		atime_changed_cb(zfsvfs, atime);
5981544Seschrock 
599*5331Samw 	nbmand_changed_cb(zfsvfs, nbmand);
600*5331Samw 
6011544Seschrock 	return (0);
6021544Seschrock 
6031544Seschrock unregister:
6041544Seschrock 	/*
6051544Seschrock 	 * We may attempt to unregister some callbacks that are not
6061544Seschrock 	 * registered, but this is OK; it will simply return ENOMSG,
6071544Seschrock 	 * which we will ignore.
6081544Seschrock 	 */
6091544Seschrock 	(void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
6103234Sck153898 	(void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
6111544Seschrock 	(void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
6121544Seschrock 	(void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
6131544Seschrock 	(void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs);
6141544Seschrock 	(void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
6151544Seschrock 	(void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
6161544Seschrock 	(void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
6171544Seschrock 	(void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
6181544Seschrock 	(void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
6191544Seschrock 	    zfsvfs);
620*5331Samw 	(void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
6211544Seschrock 	return (error);
6221544Seschrock 
6231544Seschrock }
6241544Seschrock 
6251544Seschrock static int
6265326Sek110237 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
6275326Sek110237 {
6285326Sek110237 	uint_t readonly;
6295326Sek110237 	int error;
6305326Sek110237 
6315326Sek110237 	error = zfs_register_callbacks(zfsvfs->z_vfs);
6325326Sek110237 	if (error)
6335326Sek110237 		return (error);
6345326Sek110237 
6355326Sek110237 	/*
6365326Sek110237 	 * Set the objset user_ptr to track its zfsvfs.
6375326Sek110237 	 */
6385326Sek110237 	mutex_enter(&zfsvfs->z_os->os->os_user_ptr_lock);
6395326Sek110237 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
6405326Sek110237 	mutex_exit(&zfsvfs->z_os->os->os_user_ptr_lock);
6415326Sek110237 
6425326Sek110237 	/*
6435326Sek110237 	 * If we are not mounting (ie: online recv), then we don't
6445326Sek110237 	 * have to worry about replaying the log as we blocked all
6455326Sek110237 	 * operations out since we closed the ZIL.
6465326Sek110237 	 */
6475326Sek110237 	if (mounting) {
6485326Sek110237 		/*
6495326Sek110237 		 * During replay we remove the read only flag to
6505326Sek110237 		 * allow replays to succeed.
6515326Sek110237 		 */
6525326Sek110237 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
6535326Sek110237 		if (readonly != 0)
6545326Sek110237 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
6555326Sek110237 		else
6565326Sek110237 			zfs_unlinked_drain(zfsvfs);
6575326Sek110237 
6585326Sek110237 		/*
6595326Sek110237 		 * Parse and replay the intent log.
6605326Sek110237 		 *
6615326Sek110237 		 * Because of ziltest, this must be done after
6625326Sek110237 		 * zfs_unlinked_drain().  (Further note: ziltest doesn't
6635326Sek110237 		 * use readonly mounts, where zfs_unlinked_drain() isn't
6645326Sek110237 		 * called.)  This is because ziltest causes spa_sync()
6655326Sek110237 		 * to think it's committed, but actually it is not, so
6665326Sek110237 		 * the intent log contains many txg's worth of changes.
6675326Sek110237 		 *
6685326Sek110237 		 * In particular, if object N is in the unlinked set in
6695326Sek110237 		 * the last txg to actually sync, then it could be
6705326Sek110237 		 * actually freed in a later txg and then reallocated in
6715326Sek110237 		 * a yet later txg.  This would write a "create object
6725326Sek110237 		 * N" record to the intent log.  Normally, this would be
6735326Sek110237 		 * fine because the spa_sync() would have written out
6745326Sek110237 		 * the fact that object N is free, before we could write
6755326Sek110237 		 * the "create object N" intent log record.
6765326Sek110237 		 *
6775326Sek110237 		 * But when we are in ziltest mode, we advance the "open
6785326Sek110237 		 * txg" without actually spa_sync()-ing the changes to
6795326Sek110237 		 * disk.  So we would see that object N is still
6805326Sek110237 		 * allocated and in the unlinked set, and there is an
6815326Sek110237 		 * intent log record saying to allocate it.
6825326Sek110237 		 */
6835326Sek110237 		zil_replay(zfsvfs->z_os, zfsvfs, &zfsvfs->z_assign,
6845326Sek110237 		    zfs_replay_vector);
6855326Sek110237 
6865326Sek110237 		zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
6875326Sek110237 	}
6885326Sek110237 
6895326Sek110237 	if (!zil_disable)
6905326Sek110237 		zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
6915326Sek110237 
6925326Sek110237 	return (0);
6935326Sek110237 }
6945326Sek110237 
6955326Sek110237 static int
6961544Seschrock zfs_domount(vfs_t *vfsp, char *osname, cred_t *cr)
6971544Seschrock {
6981544Seschrock 	dev_t mount_dev;
6991544Seschrock 	uint64_t recordsize, readonly;
7001544Seschrock 	int error = 0;
7011544Seschrock 	int mode;
7021544Seschrock 	zfsvfs_t *zfsvfs;
7031544Seschrock 	znode_t *zp = NULL;
7041544Seschrock 
7051544Seschrock 	ASSERT(vfsp);
7061544Seschrock 	ASSERT(osname);
7071544Seschrock 
7081544Seschrock 	/*
7091544Seschrock 	 * Initialize the zfs-specific filesystem structure.
7101544Seschrock 	 * Should probably make this a kmem cache, shuffle fields,
7111544Seschrock 	 * and just bzero up to z_hold_mtx[].
7121544Seschrock 	 */
7131544Seschrock 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
7141544Seschrock 	zfsvfs->z_vfs = vfsp;
7151544Seschrock 	zfsvfs->z_parent = zfsvfs;
7161544Seschrock 	zfsvfs->z_assign = TXG_NOWAIT;
7171544Seschrock 	zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
7181544Seschrock 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
7191544Seschrock 
7201544Seschrock 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
7211544Seschrock 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
7221544Seschrock 	    offsetof(znode_t, z_link_node));
7235326Sek110237 	rrw_init(&zfsvfs->z_teardown_lock);
7245326Sek110237 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
7251544Seschrock 
7261544Seschrock 	/* Initialize the generic filesystem structure. */
7271544Seschrock 	vfsp->vfs_bcount = 0;
7281544Seschrock 	vfsp->vfs_data = NULL;
7291544Seschrock 
7301544Seschrock 	if (zfs_create_unique_device(&mount_dev) == -1) {
7311544Seschrock 		error = ENODEV;
7321544Seschrock 		goto out;
7331544Seschrock 	}
7341544Seschrock 	ASSERT(vfs_devismounted(mount_dev) == 0);
7351544Seschrock 
7361544Seschrock 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
7371544Seschrock 	    NULL))
7381544Seschrock 		goto out;
7391544Seschrock 
7401544Seschrock 	vfsp->vfs_dev = mount_dev;
7411544Seschrock 	vfsp->vfs_fstype = zfsfstype;
7421544Seschrock 	vfsp->vfs_bsize = recordsize;
7431544Seschrock 	vfsp->vfs_flag |= VFS_NOTRUNC;
7441544Seschrock 	vfsp->vfs_data = zfsvfs;
7451544Seschrock 
7461544Seschrock 	if (error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL))
7471544Seschrock 		goto out;
7481544Seschrock 
7491544Seschrock 	if (readonly)
7501544Seschrock 		mode = DS_MODE_PRIMARY | DS_MODE_READONLY;
7511544Seschrock 	else
7521544Seschrock 		mode = DS_MODE_PRIMARY;
7531544Seschrock 
7541544Seschrock 	error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
7551544Seschrock 	if (error == EROFS) {
7561544Seschrock 		mode = DS_MODE_PRIMARY | DS_MODE_READONLY;
7571544Seschrock 		error = dmu_objset_open(osname, DMU_OST_ZFS, mode,
7581544Seschrock 		    &zfsvfs->z_os);
7591544Seschrock 	}
7601544Seschrock 
7611544Seschrock 	if (error)
7621544Seschrock 		goto out;
7631544Seschrock 
7641544Seschrock 	if (error = zfs_init_fs(zfsvfs, &zp, cr))
7651544Seschrock 		goto out;
7661544Seschrock 
7671544Seschrock 	/* The call to zfs_init_fs leaves the vnode held, release it here. */
7681544Seschrock 	VN_RELE(ZTOV(zp));
7691544Seschrock 
770*5331Samw 	/*
771*5331Samw 	 * Set features for file system.
772*5331Samw 	 */
773*5331Samw 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
774*5331Samw 	if (zfsvfs->z_use_fuids) {
775*5331Samw 		vfs_set_feature(vfsp, VFSFT_XVATTR);
776*5331Samw 		vfs_set_feature(vfsp, VFSFT_ACEMASKONACCESS);
777*5331Samw 		vfs_set_feature(vfsp, VFSFT_ACLONCREATE);
778*5331Samw 	}
779*5331Samw 
780*5331Samw 	/*
781*5331Samw 	 * Set normalization regardless of whether or not the object
782*5331Samw 	 * set is a snapshot.  Snapshots and clones need to have
783*5331Samw 	 * identical normalization as did the file system they
784*5331Samw 	 * originated from.
785*5331Samw 	 */
786*5331Samw 	if ((error = zfs_normalization_set(osname, zfsvfs)) != 0)
787*5331Samw 		goto out;
788*5331Samw 
7891544Seschrock 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
790*5331Samw 		uint64_t pval;
7913234Sck153898 
7921544Seschrock 		ASSERT(mode & DS_MODE_READONLY);
7931544Seschrock 		atime_changed_cb(zfsvfs, B_FALSE);
7941544Seschrock 		readonly_changed_cb(zfsvfs, B_TRUE);
795*5331Samw 		if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
7963234Sck153898 			goto out;
797*5331Samw 		xattr_changed_cb(zfsvfs, pval);
7981544Seschrock 		zfsvfs->z_issnap = B_TRUE;
7991544Seschrock 	} else {
8005326Sek110237 		error = zfsvfs_setup(zfsvfs, B_TRUE);
8011544Seschrock 	}
8021544Seschrock 
8031544Seschrock 	if (!zfsvfs->z_issnap)
8041544Seschrock 		zfsctl_create(zfsvfs);
8051544Seschrock out:
8061544Seschrock 	if (error) {
8071544Seschrock 		if (zfsvfs->z_os)
8081544Seschrock 			dmu_objset_close(zfsvfs->z_os);
8094831Sgw25295 		mutex_destroy(&zfsvfs->z_znodes_lock);
8104831Sgw25295 		list_destroy(&zfsvfs->z_all_znodes);
8115326Sek110237 		rrw_destroy(&zfsvfs->z_teardown_lock);
8125326Sek110237 		rw_destroy(&zfsvfs->z_teardown_inactive_lock);
8131544Seschrock 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
8141544Seschrock 	} else {
8151544Seschrock 		atomic_add_32(&zfs_active_fs_count, 1);
8161544Seschrock 	}
8171544Seschrock 
8181544Seschrock 	return (error);
8191544Seschrock }
8201544Seschrock 
8211544Seschrock void
8221544Seschrock zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
8231544Seschrock {
8241544Seschrock 	objset_t *os = zfsvfs->z_os;
8251544Seschrock 	struct dsl_dataset *ds;
8261544Seschrock 
8271544Seschrock 	/*
8281544Seschrock 	 * Unregister properties.
8291544Seschrock 	 */
8301544Seschrock 	if (!dmu_objset_is_snapshot(os)) {
8311544Seschrock 		ds = dmu_objset_ds(os);
8321544Seschrock 		VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
8331544Seschrock 		    zfsvfs) == 0);
8341544Seschrock 
8353234Sck153898 		VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
8363234Sck153898 		    zfsvfs) == 0);
8373234Sck153898 
8381544Seschrock 		VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
8391544Seschrock 		    zfsvfs) == 0);
8401544Seschrock 
8411544Seschrock 		VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
8421544Seschrock 		    zfsvfs) == 0);
8431544Seschrock 
8441544Seschrock 		VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
8451544Seschrock 		    zfsvfs) == 0);
8461544Seschrock 
8471544Seschrock 		VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
8481544Seschrock 		    zfsvfs) == 0);
8491544Seschrock 
8501544Seschrock 		VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
8511544Seschrock 		    zfsvfs) == 0);
8521544Seschrock 
8531544Seschrock 		VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
8541544Seschrock 		    zfsvfs) == 0);
8551544Seschrock 
8561544Seschrock 		VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
8571544Seschrock 		    zfsvfs) == 0);
8581544Seschrock 
8591544Seschrock 		VERIFY(dsl_prop_unregister(ds, "aclinherit",
8601544Seschrock 		    acl_inherit_changed_cb, zfsvfs) == 0);
861*5331Samw 
862*5331Samw 		VERIFY(dsl_prop_unregister(ds, "vscan",
863*5331Samw 		    vscan_changed_cb, zfsvfs) == 0);
8641544Seschrock 	}
8651544Seschrock }
8661544Seschrock 
8673912Slling /*
8683912Slling  * Convert a decimal digit string to a uint64_t integer.
8693912Slling  */
8703912Slling static int
8713912Slling str_to_uint64(char *str, uint64_t *objnum)
8723912Slling {
8733912Slling 	uint64_t num = 0;
8743912Slling 
8753912Slling 	while (*str) {
8763912Slling 		if (*str < '0' || *str > '9')
8773912Slling 			return (EINVAL);
8783912Slling 
8793912Slling 		num = num*10 + *str++ - '0';
8803912Slling 	}
8813912Slling 
8823912Slling 	*objnum = num;
8833912Slling 	return (0);
8843912Slling }
8853912Slling 
8863912Slling /*
8873912Slling  * The boot path passed from the boot loader is in the form of
8883912Slling  * "rootpool-name/root-filesystem-object-number'. Convert this
8893912Slling  * string to a dataset name: "rootpool-name/root-filesystem-name".
8903912Slling  */
8913912Slling static int
8923912Slling parse_bootpath(char *bpath, char *outpath)
8933912Slling {
8943912Slling 	char *slashp;
8953912Slling 	uint64_t objnum;
8963912Slling 	int error;
8973912Slling 
8983912Slling 	if (*bpath == 0 || *bpath == '/')
8993912Slling 		return (EINVAL);
9003912Slling 
9013912Slling 	slashp = strchr(bpath, '/');
9023912Slling 
9033912Slling 	/* if no '/', just return the pool name */
9043912Slling 	if (slashp == NULL) {
9053912Slling 		(void) strcpy(outpath, bpath);
9063912Slling 		return (0);
9073912Slling 	}
9083912Slling 
9093912Slling 	if (error = str_to_uint64(slashp+1, &objnum))
9103912Slling 		return (error);
9113912Slling 
9123912Slling 	*slashp = '\0';
9133912Slling 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
9143912Slling 	*slashp = '/';
9153912Slling 
9163912Slling 	return (error);
9173912Slling }
9183912Slling 
9191544Seschrock static int
9201544Seschrock zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
9211544Seschrock {
9221544Seschrock 	int error = 0;
9231544Seschrock 	int ret = 0;
9241544Seschrock 	static int zfsrootdone = 0;
9251544Seschrock 	zfsvfs_t *zfsvfs = NULL;
9261544Seschrock 	znode_t *zp = NULL;
9271544Seschrock 	vnode_t *vp = NULL;
9283912Slling 	char *zfs_bootpath;
9291544Seschrock 
9301544Seschrock 	ASSERT(vfsp);
9311544Seschrock 
9321544Seschrock 	/*
9333912Slling 	 * The filesystem that we mount as root is defined in the
9343912Slling 	 * "zfs-bootfs" property.
9351544Seschrock 	 */
9361544Seschrock 	if (why == ROOT_INIT) {
9371544Seschrock 		if (zfsrootdone++)
9381544Seschrock 			return (EBUSY);
9391544Seschrock 
9403912Slling 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
9413912Slling 		    DDI_PROP_DONTPASS, "zfs-bootfs", &zfs_bootpath) !=
9423912Slling 		    DDI_SUCCESS)
9433912Slling 			return (EIO);
9443912Slling 
9453912Slling 		error = parse_bootpath(zfs_bootpath, rootfs.bo_name);
9463912Slling 		ddi_prop_free(zfs_bootpath);
9473912Slling 
9483912Slling 		if (error)
9493912Slling 			return (error);
9501544Seschrock 
9511544Seschrock 		if (error = vfs_lock(vfsp))
9521544Seschrock 			return (error);
9531544Seschrock 
9543912Slling 		if (error = zfs_domount(vfsp, rootfs.bo_name, CRED()))
9551544Seschrock 			goto out;
9561544Seschrock 
9571544Seschrock 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
9581544Seschrock 		ASSERT(zfsvfs);
9591544Seschrock 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp))
9601544Seschrock 			goto out;
9611544Seschrock 
9621544Seschrock 		vp = ZTOV(zp);
9631544Seschrock 		mutex_enter(&vp->v_lock);
9641544Seschrock 		vp->v_flag |= VROOT;
9651544Seschrock 		mutex_exit(&vp->v_lock);
9661544Seschrock 		rootvp = vp;
9671544Seschrock 
9681544Seschrock 		/*
9691544Seschrock 		 * The zfs_zget call above returns with a hold on vp, we release
9701544Seschrock 		 * it here.
9711544Seschrock 		 */
9721544Seschrock 		VN_RELE(vp);
9731544Seschrock 
9741544Seschrock 		/*
9751544Seschrock 		 * Mount root as readonly initially, it will be remouted
9761544Seschrock 		 * read/write by /lib/svc/method/fs-usr.
9771544Seschrock 		 */
9781544Seschrock 		readonly_changed_cb(vfsp->vfs_data, B_TRUE);
9791544Seschrock 		vfs_add((struct vnode *)0, vfsp,
9801544Seschrock 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
9811544Seschrock out:
9821544Seschrock 		vfs_unlock(vfsp);
9831544Seschrock 		ret = (error) ? error : 0;
9841544Seschrock 		return (ret);
9851544Seschrock 	} else if (why == ROOT_REMOUNT) {
9861544Seschrock 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
9871544Seschrock 		vfsp->vfs_flag |= VFS_REMOUNT;
9884596Slling 
9894596Slling 		/* refresh mount options */
9904596Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
9914596Slling 		return (zfs_register_callbacks(vfsp));
9924596Slling 
9931544Seschrock 	} else if (why == ROOT_UNMOUNT) {
9941544Seschrock 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
9951544Seschrock 		(void) zfs_sync(vfsp, 0, 0);
9961544Seschrock 		return (0);
9971544Seschrock 	}
9981544Seschrock 
9991544Seschrock 	/*
10001544Seschrock 	 * if "why" is equal to anything else other than ROOT_INIT,
10011544Seschrock 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
10021544Seschrock 	 */
10031544Seschrock 	return (ENOTSUP);
10041544Seschrock }
10051544Seschrock 
1006789Sahrens /*ARGSUSED*/
1007789Sahrens static int
1008789Sahrens zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
1009789Sahrens {
1010789Sahrens 	char		*osname;
1011789Sahrens 	pathname_t	spn;
1012789Sahrens 	int		error = 0;
1013789Sahrens 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
10143912Slling 	    UIO_SYSSPACE : UIO_USERSPACE;
1015789Sahrens 	int		canwrite;
1016789Sahrens 
1017789Sahrens 	if (mvp->v_type != VDIR)
1018789Sahrens 		return (ENOTDIR);
1019789Sahrens 
1020789Sahrens 	mutex_enter(&mvp->v_lock);
1021789Sahrens 	if ((uap->flags & MS_REMOUNT) == 0 &&
1022789Sahrens 	    (uap->flags & MS_OVERLAY) == 0 &&
1023789Sahrens 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
1024789Sahrens 		mutex_exit(&mvp->v_lock);
1025789Sahrens 		return (EBUSY);
1026789Sahrens 	}
1027789Sahrens 	mutex_exit(&mvp->v_lock);
1028789Sahrens 
1029789Sahrens 	/*
1030789Sahrens 	 * ZFS does not support passing unparsed data in via MS_DATA.
1031789Sahrens 	 * Users should use the MS_OPTIONSTR interface; this means
1032789Sahrens 	 * that all option parsing is already done and the options struct
1033789Sahrens 	 * can be interrogated.
1034789Sahrens 	 */
1035789Sahrens 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
1036789Sahrens 		return (EINVAL);
1037789Sahrens 
1038789Sahrens 	/*
1039789Sahrens 	 * Get the objset name (the "special" mount argument).
1040789Sahrens 	 */
1041789Sahrens 	if (error = pn_get(uap->spec, fromspace, &spn))
1042789Sahrens 		return (error);
1043789Sahrens 
1044789Sahrens 	osname = spn.pn_path;
1045789Sahrens 
10464543Smarks 	/*
10474543Smarks 	 * Check for mount privilege?
10484543Smarks 	 *
10494543Smarks 	 * If we don't have privilege then see if
10504543Smarks 	 * we have local permission to allow it
10514543Smarks 	 */
10524543Smarks 	error = secpolicy_fs_mount(cr, mvp, vfsp);
10534543Smarks 	if (error) {
10544543Smarks 		error = dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr);
10554543Smarks 		if (error == 0) {
10564543Smarks 			vattr_t		vattr;
10574543Smarks 
10584543Smarks 			/*
10594543Smarks 			 * Make sure user is the owner of the mount point
10604543Smarks 			 * or has sufficient privileges.
10614543Smarks 			 */
10624543Smarks 
10634543Smarks 			vattr.va_mask = AT_UID;
10644543Smarks 
1065*5331Samw 			if (error = VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
10664543Smarks 				goto out;
10674543Smarks 			}
10684543Smarks 
10694543Smarks 			if (error = secpolicy_vnode_owner(cr, vattr.va_uid)) {
10704543Smarks 				goto out;
10714543Smarks 			}
10724543Smarks 
1073*5331Samw 			if (error = VOP_ACCESS(mvp, VWRITE, 0, cr, NULL)) {
10744543Smarks 				goto out;
10754543Smarks 			}
10764543Smarks 
10774543Smarks 			secpolicy_fs_mount_clearopts(cr, vfsp);
10784543Smarks 		} else {
10794543Smarks 			goto out;
10804543Smarks 		}
10814543Smarks 	}
1082789Sahrens 
1083789Sahrens 	/*
1084789Sahrens 	 * Refuse to mount a filesystem if we are in a local zone and the
1085789Sahrens 	 * dataset is not visible.
1086789Sahrens 	 */
1087789Sahrens 	if (!INGLOBALZONE(curproc) &&
1088789Sahrens 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1089789Sahrens 		error = EPERM;
1090789Sahrens 		goto out;
1091789Sahrens 	}
1092789Sahrens 
10934596Slling 	/*
10944596Slling 	 * When doing a remount, we simply refresh our temporary properties
10954596Slling 	 * according to those options set in the current VFS options.
10964596Slling 	 */
10974596Slling 	if (uap->flags & MS_REMOUNT) {
10984596Slling 		/* refresh mount options */
10994596Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
11004596Slling 		error = zfs_register_callbacks(vfsp);
11014596Slling 		goto out;
11024596Slling 	}
11034596Slling 
11041544Seschrock 	error = zfs_domount(vfsp, osname, cr);
1105789Sahrens 
1106789Sahrens out:
1107789Sahrens 	pn_free(&spn);
1108789Sahrens 	return (error);
1109789Sahrens }
1110789Sahrens 
1111789Sahrens static int
1112789Sahrens zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1113789Sahrens {
1114789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1115789Sahrens 	dev32_t d32;
11162885Sahrens 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
1117789Sahrens 
1118789Sahrens 	ZFS_ENTER(zfsvfs);
1119789Sahrens 
11202885Sahrens 	dmu_objset_space(zfsvfs->z_os,
11212885Sahrens 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
1122789Sahrens 
1123789Sahrens 	/*
1124789Sahrens 	 * The underlying storage pool actually uses multiple block sizes.
1125789Sahrens 	 * We report the fragsize as the smallest block size we support,
1126789Sahrens 	 * and we report our blocksize as the filesystem's maximum blocksize.
1127789Sahrens 	 */
1128789Sahrens 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1129789Sahrens 	statp->f_bsize = zfsvfs->z_max_blksz;
1130789Sahrens 
1131789Sahrens 	/*
1132789Sahrens 	 * The following report "total" blocks of various kinds in the
1133789Sahrens 	 * file system, but reported in terms of f_frsize - the
1134789Sahrens 	 * "fragment" size.
1135789Sahrens 	 */
1136789Sahrens 
11372885Sahrens 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
11382885Sahrens 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
1139789Sahrens 	statp->f_bavail = statp->f_bfree; /* no root reservation */
1140789Sahrens 
1141789Sahrens 	/*
1142789Sahrens 	 * statvfs() should really be called statufs(), because it assumes
1143789Sahrens 	 * static metadata.  ZFS doesn't preallocate files, so the best
1144789Sahrens 	 * we can do is report the max that could possibly fit in f_files,
1145789Sahrens 	 * and that minus the number actually used in f_ffree.
1146789Sahrens 	 * For f_ffree, report the smaller of the number of object available
1147789Sahrens 	 * and the number of blocks (each object will take at least a block).
1148789Sahrens 	 */
11492885Sahrens 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
1150789Sahrens 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
11512885Sahrens 	statp->f_files = statp->f_ffree + usedobjs;
1152789Sahrens 
1153789Sahrens 	(void) cmpldev(&d32, vfsp->vfs_dev);
1154789Sahrens 	statp->f_fsid = d32;
1155789Sahrens 
1156789Sahrens 	/*
1157789Sahrens 	 * We're a zfs filesystem.
1158789Sahrens 	 */
1159789Sahrens 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1160789Sahrens 
11611123Smarks 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1162789Sahrens 
1163789Sahrens 	statp->f_namemax = ZFS_MAXNAMELEN;
1164789Sahrens 
1165789Sahrens 	/*
1166789Sahrens 	 * We have all of 32 characters to stuff a string here.
1167789Sahrens 	 * Is there anything useful we could/should provide?
1168789Sahrens 	 */
1169789Sahrens 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
1170789Sahrens 
1171789Sahrens 	ZFS_EXIT(zfsvfs);
1172789Sahrens 	return (0);
1173789Sahrens }
1174789Sahrens 
1175789Sahrens static int
1176789Sahrens zfs_root(vfs_t *vfsp, vnode_t **vpp)
1177789Sahrens {
1178789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1179789Sahrens 	znode_t *rootzp;
1180789Sahrens 	int error;
1181789Sahrens 
1182789Sahrens 	ZFS_ENTER(zfsvfs);
1183789Sahrens 
1184789Sahrens 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1185789Sahrens 	if (error == 0)
1186789Sahrens 		*vpp = ZTOV(rootzp);
1187789Sahrens 
1188789Sahrens 	ZFS_EXIT(zfsvfs);
1189789Sahrens 	return (error);
1190789Sahrens }
1191789Sahrens 
11925326Sek110237 /*
11935326Sek110237  * Teardown the zfsvfs::z_os.
11945326Sek110237  *
11955326Sek110237  * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
11965326Sek110237  * and 'z_teardown_inactive_lock' held.
11975326Sek110237  */
11985326Sek110237 static int
11995326Sek110237 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
12005326Sek110237 {
12015326Sek110237 	objset_t *os = zfsvfs->z_os;
12025326Sek110237 	znode_t	*zp, *nextzp;
12035326Sek110237 	znode_t markerzp;
12045326Sek110237 
12055326Sek110237 	rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
12065326Sek110237 
12075326Sek110237 	if (!unmounting) {
12085326Sek110237 		/*
12095326Sek110237 		 * We purge the parent filesystem's vfsp as the parent
12105326Sek110237 		 * filesystem and all of its snapshots have their vnode's
12115326Sek110237 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
12125326Sek110237 		 * 'z_parent' is self referential for non-snapshots.
12135326Sek110237 		 */
12145326Sek110237 		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
12155326Sek110237 	}
12165326Sek110237 
12175326Sek110237 	/*
12185326Sek110237 	 * Close the zil. NB: Can't close the zil while zfs_inactive
12195326Sek110237 	 * threads are blocked as zil_close can call zfs_inactive.
12205326Sek110237 	 */
12215326Sek110237 	if (zfsvfs->z_log) {
12225326Sek110237 		zil_close(zfsvfs->z_log);
12235326Sek110237 		zfsvfs->z_log = NULL;
12245326Sek110237 	}
12255326Sek110237 
12265326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
12275326Sek110237 
12285326Sek110237 	/*
12295326Sek110237 	 * If we are not unmounting (ie: online recv) and someone already
12305326Sek110237 	 * unmounted this file system while we were doing the switcheroo,
12315326Sek110237 	 * or a reopen of z_os failed then just bail out now.
12325326Sek110237 	 */
12335326Sek110237 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
12345326Sek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
12355326Sek110237 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
12365326Sek110237 		return (EIO);
12375326Sek110237 	}
12385326Sek110237 
12395326Sek110237 	/*
12405326Sek110237 	 * At this point there are no vops active, and any new vops will
12415326Sek110237 	 * fail with EIO since we have z_teardown_lock for writer (only
12425326Sek110237 	 * relavent for forced unmount).
12435326Sek110237 	 *
12445326Sek110237 	 * Release all holds on dbufs.
12455326Sek110237 	 * Note, the dmu can still callback via znode_pageout_func()
12465326Sek110237 	 * which can zfs_znode_free() the znode.  So we lock
12475326Sek110237 	 * z_all_znodes; search the list for a held dbuf; drop the lock
12485326Sek110237 	 * (we know zp can't disappear if we hold a dbuf lock) then
12495326Sek110237 	 * regrab the lock and restart.
12505326Sek110237 	 *
12515326Sek110237 	 * Since we have to restart the search after finding each held dbuf,
12525326Sek110237 	 * we do two things to speed up searching: we insert a dummy znode
12535326Sek110237 	 * ('markerzp') to detect the original tail of the list, and move
12545326Sek110237 	 * non-held znodes to the end of the list.  Once we hit 'markerzp',
12555326Sek110237 	 * we know we've looked at each znode and can break out.
12565326Sek110237 	 */
12575326Sek110237 	mutex_enter(&zfsvfs->z_znodes_lock);
12585326Sek110237 	list_insert_tail(&zfsvfs->z_all_znodes, &markerzp);
12595326Sek110237 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != &markerzp;
12605326Sek110237 	    zp = nextzp) {
12615326Sek110237 		nextzp = list_next(&zfsvfs->z_all_znodes, zp);
12625326Sek110237 		if (zp->z_dbuf_held) {
12635326Sek110237 			/* dbufs should only be held when force unmounting */
12645326Sek110237 			zp->z_dbuf_held = 0;
12655326Sek110237 			mutex_exit(&zfsvfs->z_znodes_lock);
12665326Sek110237 			dmu_buf_rele(zp->z_dbuf, NULL);
12675326Sek110237 			/* Start again */
12685326Sek110237 			mutex_enter(&zfsvfs->z_znodes_lock);
12695326Sek110237 			nextzp = list_head(&zfsvfs->z_all_znodes);
12705326Sek110237 		} else {
12715326Sek110237 			list_remove(&zfsvfs->z_all_znodes, zp);
12725326Sek110237 			list_insert_tail(&zfsvfs->z_all_znodes, zp);
12735326Sek110237 		}
12745326Sek110237 	}
12755326Sek110237 	list_remove(&zfsvfs->z_all_znodes, &markerzp);
12765326Sek110237 	mutex_exit(&zfsvfs->z_znodes_lock);
12775326Sek110237 
12785326Sek110237 	/*
12795326Sek110237 	 * If we are unmounting, set the unmounted flag and let new vops
12805326Sek110237 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
12815326Sek110237 	 * other vops will fail with EIO.
12825326Sek110237 	 */
12835326Sek110237 	if (unmounting) {
12845326Sek110237 		zfsvfs->z_unmounted = B_TRUE;
12855326Sek110237 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
12865326Sek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
12875326Sek110237 	}
12885326Sek110237 
12895326Sek110237 	/*
12905326Sek110237 	 * z_os will be NULL if there was an error in attempting to reopen
12915326Sek110237 	 * zfsvfs, so just return as the properties had already been
12925326Sek110237 	 * unregistered and cached data had been evicted before.
12935326Sek110237 	 */
12945326Sek110237 	if (zfsvfs->z_os == NULL)
12955326Sek110237 		return (0);
12965326Sek110237 
12975326Sek110237 	/*
12985326Sek110237 	 * Unregister properties.
12995326Sek110237 	 */
13005326Sek110237 	zfs_unregister_callbacks(zfsvfs);
13015326Sek110237 
13025326Sek110237 	/*
13035326Sek110237 	 * Evict cached data
13045326Sek110237 	 */
13055326Sek110237 	(void) dmu_objset_evict_dbufs(os);
13065326Sek110237 
13075326Sek110237 	return (0);
13085326Sek110237 }
13095326Sek110237 
1310789Sahrens /*ARGSUSED*/
1311789Sahrens static int
1312789Sahrens zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1313789Sahrens {
1314789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
13155326Sek110237 	objset_t *os;
1316789Sahrens 	int ret;
1317789Sahrens 
13184543Smarks 	ret = secpolicy_fs_unmount(cr, vfsp);
13194543Smarks 	if (ret) {
13204543Smarks 		ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
13214543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
13224543Smarks 		if (ret)
13234543Smarks 			return (ret);
13244543Smarks 	}
13251484Sek110237 
13264736Sek110237 	/*
13274736Sek110237 	 * We purge the parent filesystem's vfsp as the parent filesystem
13284736Sek110237 	 * and all of its snapshots have their vnode's v_vfsp set to the
13294736Sek110237 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
13304736Sek110237 	 * referential for non-snapshots.
13314736Sek110237 	 */
13324736Sek110237 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
13331484Sek110237 
1334789Sahrens 	/*
1335789Sahrens 	 * Unmount any snapshots mounted under .zfs before unmounting the
1336789Sahrens 	 * dataset itself.
1337789Sahrens 	 */
1338789Sahrens 	if (zfsvfs->z_ctldir != NULL &&
13394543Smarks 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1340789Sahrens 		return (ret);
13414543Smarks 	}
1342789Sahrens 
13434787Sahrens 	if (!(fflag & MS_FORCE)) {
13444480Sgw25295 		/*
13454787Sahrens 		 * Check the number of active vnodes in the file system.
13464787Sahrens 		 * Our count is maintained in the vfs structure, but the
13474787Sahrens 		 * number is off by 1 to indicate a hold on the vfs
13484787Sahrens 		 * structure itself.
13494787Sahrens 		 *
13504787Sahrens 		 * The '.zfs' directory maintains a reference of its
13514787Sahrens 		 * own, and any active references underneath are
13524787Sahrens 		 * reflected in the vnode count.
1353789Sahrens 		 */
13544787Sahrens 		if (zfsvfs->z_ctldir == NULL) {
13554787Sahrens 			if (vfsp->vfs_count > 1)
13564787Sahrens 				return (EBUSY);
13574787Sahrens 		} else {
13584787Sahrens 			if (vfsp->vfs_count > 2 ||
13595326Sek110237 			    zfsvfs->z_ctldir->v_count > 1)
13604787Sahrens 				return (EBUSY);
1361789Sahrens 		}
1362789Sahrens 	}
1363789Sahrens 
1364789Sahrens 	vfsp->vfs_flag |= VFS_UNMOUNTED;
13654787Sahrens 
13665326Sek110237 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
13675326Sek110237 	os = zfsvfs->z_os;
13684787Sahrens 
13694787Sahrens 	/*
13705326Sek110237 	 * z_os will be NULL if there was an error in
13715326Sek110237 	 * attempting to reopen zfsvfs.
13724787Sahrens 	 */
13735326Sek110237 	if (os != NULL) {
13745326Sek110237 		/*
13755326Sek110237 		 * Unset the objset user_ptr.
13765326Sek110237 		 */
13775326Sek110237 		mutex_enter(&os->os->os_user_ptr_lock);
13785326Sek110237 		dmu_objset_set_user(os, NULL);
13795326Sek110237 		mutex_exit(&os->os->os_user_ptr_lock);
13804787Sahrens 
13815326Sek110237 		/*
13825326Sek110237 		 * Finally close the objset
13835326Sek110237 		 */
13845326Sek110237 		dmu_objset_close(os);
13854787Sahrens 	}
13864787Sahrens 
13874787Sahrens 	/*
13884787Sahrens 	 * We can now safely destroy the '.zfs' directory node.
13894787Sahrens 	 */
13904787Sahrens 	if (zfsvfs->z_ctldir != NULL)
13914787Sahrens 		zfsctl_destroy(zfsvfs);
1392789Sahrens 
1393789Sahrens 	return (0);
1394789Sahrens }
1395789Sahrens 
1396789Sahrens static int
1397789Sahrens zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1398789Sahrens {
1399789Sahrens 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1400789Sahrens 	znode_t		*zp;
1401789Sahrens 	uint64_t	object = 0;
1402789Sahrens 	uint64_t	fid_gen = 0;
1403789Sahrens 	uint64_t	gen_mask;
1404789Sahrens 	uint64_t	zp_gen;
1405789Sahrens 	int 		i, err;
1406789Sahrens 
1407789Sahrens 	*vpp = NULL;
1408789Sahrens 
1409789Sahrens 	ZFS_ENTER(zfsvfs);
1410789Sahrens 
1411789Sahrens 	if (fidp->fid_len == LONG_FID_LEN) {
1412789Sahrens 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1413789Sahrens 		uint64_t	objsetid = 0;
1414789Sahrens 		uint64_t	setgen = 0;
1415789Sahrens 
1416789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1417789Sahrens 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1418789Sahrens 
1419789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1420789Sahrens 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1421789Sahrens 
1422789Sahrens 		ZFS_EXIT(zfsvfs);
1423789Sahrens 
1424789Sahrens 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1425789Sahrens 		if (err)
1426789Sahrens 			return (EINVAL);
1427789Sahrens 		ZFS_ENTER(zfsvfs);
1428789Sahrens 	}
1429789Sahrens 
1430789Sahrens 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1431789Sahrens 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1432789Sahrens 
1433789Sahrens 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1434789Sahrens 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1435789Sahrens 
1436789Sahrens 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1437789Sahrens 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1438789Sahrens 	} else {
1439789Sahrens 		ZFS_EXIT(zfsvfs);
1440789Sahrens 		return (EINVAL);
1441789Sahrens 	}
1442789Sahrens 
1443789Sahrens 	/* A zero fid_gen means we are in the .zfs control directories */
1444789Sahrens 	if (fid_gen == 0 &&
1445789Sahrens 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1446789Sahrens 		*vpp = zfsvfs->z_ctldir;
1447789Sahrens 		ASSERT(*vpp != NULL);
1448789Sahrens 		if (object == ZFSCTL_INO_SNAPDIR) {
1449789Sahrens 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1450*5331Samw 			    0, NULL, NULL, NULL, NULL, NULL) == 0);
1451789Sahrens 		} else {
1452789Sahrens 			VN_HOLD(*vpp);
1453789Sahrens 		}
1454789Sahrens 		ZFS_EXIT(zfsvfs);
1455789Sahrens 		return (0);
1456789Sahrens 	}
1457789Sahrens 
1458789Sahrens 	gen_mask = -1ULL >> (64 - 8 * i);
1459789Sahrens 
1460789Sahrens 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1461789Sahrens 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1462789Sahrens 		ZFS_EXIT(zfsvfs);
1463789Sahrens 		return (err);
1464789Sahrens 	}
1465789Sahrens 	zp_gen = zp->z_phys->zp_gen & gen_mask;
1466789Sahrens 	if (zp_gen == 0)
1467789Sahrens 		zp_gen = 1;
14683461Sahrens 	if (zp->z_unlinked || zp_gen != fid_gen) {
1469789Sahrens 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1470789Sahrens 		VN_RELE(ZTOV(zp));
1471789Sahrens 		ZFS_EXIT(zfsvfs);
1472789Sahrens 		return (EINVAL);
1473789Sahrens 	}
1474789Sahrens 
1475789Sahrens 	*vpp = ZTOV(zp);
1476789Sahrens 	ZFS_EXIT(zfsvfs);
1477789Sahrens 	return (0);
1478789Sahrens }
1479789Sahrens 
14805326Sek110237 /*
14815326Sek110237  * Block out VOPs and close zfsvfs_t::z_os
14825326Sek110237  *
14835326Sek110237  * Note, if successful, then we return with the 'z_teardown_lock' and
14845326Sek110237  * 'z_teardown_inactive_lock' write held.
14855326Sek110237  */
14865326Sek110237 int
14875326Sek110237 zfs_suspend_fs(zfsvfs_t *zfsvfs, char *name, int *mode)
14885326Sek110237 {
14895326Sek110237 	int error;
14905326Sek110237 
14915326Sek110237 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
14925326Sek110237 		return (error);
14935326Sek110237 
14945326Sek110237 	*mode = zfsvfs->z_os->os_mode;
14955326Sek110237 	dmu_objset_name(zfsvfs->z_os, name);
14965326Sek110237 	dmu_objset_close(zfsvfs->z_os);
14975326Sek110237 
14985326Sek110237 	return (0);
14995326Sek110237 }
15005326Sek110237 
15015326Sek110237 /*
15025326Sek110237  * Reopen zfsvfs_t::z_os and release VOPs.
15035326Sek110237  */
15045326Sek110237 int
15055326Sek110237 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname, int mode)
15065326Sek110237 {
15075326Sek110237 	int err;
15085326Sek110237 
15095326Sek110237 	ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
15105326Sek110237 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
15115326Sek110237 
15125326Sek110237 	err = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
15135326Sek110237 	if (err) {
15145326Sek110237 		zfsvfs->z_os = NULL;
15155326Sek110237 	} else {
15165326Sek110237 		znode_t *zp;
15175326Sek110237 
15185326Sek110237 		VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
15195326Sek110237 
15205326Sek110237 		/*
15215326Sek110237 		 * Attempt to re-establish all the active znodes with
15225326Sek110237 		 * their dbufs.  If a zfs_rezget() fails, then we'll let
15235326Sek110237 		 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
15245326Sek110237 		 * when they try to use their znode.
15255326Sek110237 		 */
15265326Sek110237 		mutex_enter(&zfsvfs->z_znodes_lock);
15275326Sek110237 		for (zp = list_head(&zfsvfs->z_all_znodes); zp;
15285326Sek110237 		    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
15295326Sek110237 			ASSERT(!zp->z_dbuf_held);
15305326Sek110237 			(void) zfs_rezget(zp);
15315326Sek110237 		}
15325326Sek110237 		mutex_exit(&zfsvfs->z_znodes_lock);
15335326Sek110237 
15345326Sek110237 	}
15355326Sek110237 
15365326Sek110237 	/* release the VOPs */
15375326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
15385326Sek110237 	rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
15395326Sek110237 
15405326Sek110237 	if (err) {
15415326Sek110237 		/*
15425326Sek110237 		 * Since we couldn't reopen zfsvfs::z_os, force
15435326Sek110237 		 * unmount this file system.
15445326Sek110237 		 */
15455326Sek110237 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
15465326Sek110237 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
15475326Sek110237 	}
15485326Sek110237 	return (err);
15495326Sek110237 }
15505326Sek110237 
1551789Sahrens static void
1552789Sahrens zfs_freevfs(vfs_t *vfsp)
1553789Sahrens {
1554789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
15554831Sgw25295 	int i;
15564831Sgw25295 
15574831Sgw25295 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
15584831Sgw25295 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1559789Sahrens 
15604787Sahrens 	mutex_destroy(&zfsvfs->z_znodes_lock);
15614831Sgw25295 	list_destroy(&zfsvfs->z_all_znodes);
15625326Sek110237 	rrw_destroy(&zfsvfs->z_teardown_lock);
15635326Sek110237 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
1564*5331Samw 	zfs_fuid_destroy(zfsvfs);
1565789Sahrens 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1566789Sahrens 
1567789Sahrens 	atomic_add_32(&zfs_active_fs_count, -1);
1568789Sahrens }
1569789Sahrens 
1570789Sahrens /*
1571789Sahrens  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
1572789Sahrens  * so we can't safely do any non-idempotent initialization here.
1573789Sahrens  * Leave that to zfs_init() and zfs_fini(), which are called
1574789Sahrens  * from the module's _init() and _fini() entry points.
1575789Sahrens  */
1576789Sahrens /*ARGSUSED*/
1577789Sahrens static int
1578789Sahrens zfs_vfsinit(int fstype, char *name)
1579789Sahrens {
1580789Sahrens 	int error;
1581789Sahrens 
1582789Sahrens 	zfsfstype = fstype;
1583789Sahrens 
1584789Sahrens 	/*
1585789Sahrens 	 * Setup vfsops and vnodeops tables.
1586789Sahrens 	 */
1587789Sahrens 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
1588789Sahrens 	if (error != 0) {
1589789Sahrens 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
1590789Sahrens 	}
1591789Sahrens 
1592789Sahrens 	error = zfs_create_op_tables();
1593789Sahrens 	if (error) {
1594789Sahrens 		zfs_remove_op_tables();
1595789Sahrens 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
1596789Sahrens 		(void) vfs_freevfsops_by_type(zfsfstype);
1597789Sahrens 		return (error);
1598789Sahrens 	}
1599789Sahrens 
1600789Sahrens 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
1601789Sahrens 
1602789Sahrens 	/*
1603849Sbonwick 	 * Unique major number for all zfs mounts.
1604849Sbonwick 	 * If we run out of 32-bit minors, we'll getudev() another major.
1605789Sahrens 	 */
1606849Sbonwick 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
1607849Sbonwick 	zfs_minor = ZFS_MIN_MINOR;
1608789Sahrens 
1609789Sahrens 	return (0);
1610789Sahrens }
1611789Sahrens 
1612789Sahrens void
1613789Sahrens zfs_init(void)
1614789Sahrens {
1615789Sahrens 	/*
1616789Sahrens 	 * Initialize .zfs directory structures
1617789Sahrens 	 */
1618789Sahrens 	zfsctl_init();
1619789Sahrens 
1620789Sahrens 	/*
1621789Sahrens 	 * Initialize znode cache, vnode ops, etc...
1622789Sahrens 	 */
1623789Sahrens 	zfs_znode_init();
1624789Sahrens }
1625789Sahrens 
1626789Sahrens void
1627789Sahrens zfs_fini(void)
1628789Sahrens {
1629789Sahrens 	zfsctl_fini();
1630789Sahrens 	zfs_znode_fini();
1631789Sahrens }
1632789Sahrens 
1633789Sahrens int
1634789Sahrens zfs_busy(void)
1635789Sahrens {
1636789Sahrens 	return (zfs_active_fs_count != 0);
1637789Sahrens }
1638789Sahrens 
16394577Sahrens int
16405147Srm160521 zfs_get_version(objset_t *os, uint64_t *version)
16414577Sahrens {
16424577Sahrens 	int error;
16434577Sahrens 
16445147Srm160521 	error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1, version);
16454577Sahrens 	return (error);
16464577Sahrens }
16474577Sahrens 
16484577Sahrens int
16494577Sahrens zfs_set_version(const char *name, uint64_t newvers)
16504577Sahrens {
16514577Sahrens 	int error;
16524577Sahrens 	objset_t *os;
16534577Sahrens 	dmu_tx_t *tx;
16544577Sahrens 	uint64_t curvers;
16554577Sahrens 
16564577Sahrens 	/*
16574577Sahrens 	 * XXX for now, require that the filesystem be unmounted.  Would
16584577Sahrens 	 * be nice to find the zfsvfs_t and just update that if
16594577Sahrens 	 * possible.
16604577Sahrens 	 */
16614577Sahrens 
16624577Sahrens 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
16634577Sahrens 		return (EINVAL);
16644577Sahrens 
16654577Sahrens 	error = dmu_objset_open(name, DMU_OST_ZFS, DS_MODE_PRIMARY, &os);
16664577Sahrens 	if (error)
16674577Sahrens 		return (error);
16684577Sahrens 
16694577Sahrens 	error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
16704577Sahrens 	    8, 1, &curvers);
16714577Sahrens 	if (error)
16724577Sahrens 		goto out;
16734577Sahrens 	if (newvers < curvers) {
16744577Sahrens 		error = EINVAL;
16754577Sahrens 		goto out;
16764577Sahrens 	}
16774577Sahrens 
16784577Sahrens 	tx = dmu_tx_create(os);
16794577Sahrens 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 0, ZPL_VERSION_STR);
16804577Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
16814577Sahrens 	if (error) {
16824577Sahrens 		dmu_tx_abort(tx);
16834577Sahrens 		goto out;
16844577Sahrens 	}
16854577Sahrens 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1,
16864577Sahrens 	    &newvers, tx);
16874577Sahrens 
16884577Sahrens 	spa_history_internal_log(LOG_DS_UPGRADE,
16894577Sahrens 	    dmu_objset_spa(os), tx, CRED(),
16904577Sahrens 	    "oldver=%llu newver=%llu dataset = %llu", curvers, newvers,
16914577Sahrens 	    dmu_objset_id(os));
16924577Sahrens 	dmu_tx_commit(tx);
16934577Sahrens 
16944577Sahrens out:
16954577Sahrens 	dmu_objset_close(os);
16964577Sahrens 	return (error);
16974577Sahrens }
16984577Sahrens 
1699789Sahrens static vfsdef_t vfw = {
1700789Sahrens 	VFSDEF_VERSION,
1701789Sahrens 	MNTTYPE_ZFS,
1702789Sahrens 	zfs_vfsinit,
1703*5331Samw 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS|
1704*5331Samw 	    VSW_XID,
1705789Sahrens 	&zfs_mntopts
1706789Sahrens };
1707789Sahrens 
1708789Sahrens struct modlfs zfs_modlfs = {
17094577Sahrens 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
1710789Sahrens };
1711