xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 10972:807794d41b3a)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51484Sek110237  * Common Development and Distribution License (the "License").
61484Sek110237  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
229030SMark.Shellenbaum@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #include <sys/types.h>
27789Sahrens #include <sys/param.h>
28789Sahrens #include <sys/systm.h>
29789Sahrens #include <sys/sysmacros.h>
30789Sahrens #include <sys/kmem.h>
31789Sahrens #include <sys/pathname.h>
32789Sahrens #include <sys/vnode.h>
33789Sahrens #include <sys/vfs.h>
343898Srsb #include <sys/vfs_opreg.h>
35789Sahrens #include <sys/mntent.h>
36789Sahrens #include <sys/mount.h>
37789Sahrens #include <sys/cmn_err.h>
38789Sahrens #include "fs/fs_subr.h"
39789Sahrens #include <sys/zfs_znode.h>
403461Sahrens #include <sys/zfs_dir.h>
41789Sahrens #include <sys/zil.h>
42789Sahrens #include <sys/fs/zfs.h>
43789Sahrens #include <sys/dmu.h>
44789Sahrens #include <sys/dsl_prop.h>
453912Slling #include <sys/dsl_dataset.h>
464543Smarks #include <sys/dsl_deleg.h>
47789Sahrens #include <sys/spa.h>
48789Sahrens #include <sys/zap.h>
49789Sahrens #include <sys/varargs.h>
50789Sahrens #include <sys/policy.h>
51789Sahrens #include <sys/atomic.h>
52789Sahrens #include <sys/mkdev.h>
53789Sahrens #include <sys/modctl.h>
544543Smarks #include <sys/refstr.h>
55789Sahrens #include <sys/zfs_ioctl.h>
56789Sahrens #include <sys/zfs_ctldir.h>
575331Samw #include <sys/zfs_fuid.h>
581544Seschrock #include <sys/bootconf.h>
59849Sbonwick #include <sys/sunddi.h>
601484Sek110237 #include <sys/dnlc.h>
615326Sek110237 #include <sys/dmu_objset.h>
626423Sgw25295 #include <sys/spa_boot.h>
63789Sahrens 
64789Sahrens int zfsfstype;
65789Sahrens vfsops_t *zfs_vfsops = NULL;
66849Sbonwick static major_t zfs_major;
67789Sahrens static minor_t zfs_minor;
68789Sahrens static kmutex_t	zfs_dev_mtx;
69789Sahrens 
709234SGeorge.Wilson@Sun.COM extern int sys_shutdown;
719234SGeorge.Wilson@Sun.COM 
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;
1509234SGeorge.Wilson@Sun.COM 		dsl_pool_t *dp;
151789Sahrens 
152789Sahrens 		ZFS_ENTER(zfsvfs);
1539234SGeorge.Wilson@Sun.COM 		dp = dmu_objset_pool(zfsvfs->z_os);
1549234SGeorge.Wilson@Sun.COM 
1559234SGeorge.Wilson@Sun.COM 		/*
1569234SGeorge.Wilson@Sun.COM 		 * If the system is shutting down, then skip any
1579234SGeorge.Wilson@Sun.COM 		 * filesystems which may exist on a suspended pool.
1589234SGeorge.Wilson@Sun.COM 		 */
1599234SGeorge.Wilson@Sun.COM 		if (sys_shutdown && spa_suspended(dp->dp_spa)) {
1609234SGeorge.Wilson@Sun.COM 			ZFS_EXIT(zfsvfs);
1619234SGeorge.Wilson@Sun.COM 			return (0);
1629234SGeorge.Wilson@Sun.COM 		}
1639234SGeorge.Wilson@Sun.COM 
164789Sahrens 		if (zfsvfs->z_log != NULL)
1652638Sperrin 			zil_commit(zfsvfs->z_log, UINT64_MAX, 0);
166789Sahrens 		else
1679234SGeorge.Wilson@Sun.COM 			txg_wait_synced(dp, 0);
168789Sahrens 		ZFS_EXIT(zfsvfs);
169789Sahrens 	} else {
170789Sahrens 		/*
171789Sahrens 		 * Sync all ZFS filesystems.  This is what happens when you
172789Sahrens 		 * run sync(1M).  Unlike other filesystems, ZFS honors the
173789Sahrens 		 * request by waiting for all pools to commit all dirty data.
174789Sahrens 		 */
175789Sahrens 		spa_sync_allpools();
176789Sahrens 	}
177789Sahrens 
178789Sahrens 	return (0);
179789Sahrens }
180789Sahrens 
1811544Seschrock static int
1821544Seschrock zfs_create_unique_device(dev_t *dev)
1831544Seschrock {
1841544Seschrock 	major_t new_major;
1851544Seschrock 
1861544Seschrock 	do {
1871544Seschrock 		ASSERT3U(zfs_minor, <=, MAXMIN32);
1881544Seschrock 		minor_t start = zfs_minor;
1891544Seschrock 		do {
1901544Seschrock 			mutex_enter(&zfs_dev_mtx);
1911544Seschrock 			if (zfs_minor >= MAXMIN32) {
1921544Seschrock 				/*
1931544Seschrock 				 * If we're still using the real major
1941544Seschrock 				 * keep out of /dev/zfs and /dev/zvol minor
1951544Seschrock 				 * number space.  If we're using a getudev()'ed
1961544Seschrock 				 * major number, we can use all of its minors.
1971544Seschrock 				 */
1981544Seschrock 				if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
1991544Seschrock 					zfs_minor = ZFS_MIN_MINOR;
2001544Seschrock 				else
2011544Seschrock 					zfs_minor = 0;
2021544Seschrock 			} else {
2031544Seschrock 				zfs_minor++;
2041544Seschrock 			}
2051544Seschrock 			*dev = makedevice(zfs_major, zfs_minor);
2061544Seschrock 			mutex_exit(&zfs_dev_mtx);
2071544Seschrock 		} while (vfs_devismounted(*dev) && zfs_minor != start);
2081544Seschrock 		if (zfs_minor == start) {
2091544Seschrock 			/*
2101544Seschrock 			 * We are using all ~262,000 minor numbers for the
2111544Seschrock 			 * current major number.  Create a new major number.
2121544Seschrock 			 */
2131544Seschrock 			if ((new_major = getudev()) == (major_t)-1) {
2141544Seschrock 				cmn_err(CE_WARN,
2151544Seschrock 				    "zfs_mount: Can't get unique major "
2161544Seschrock 				    "device number.");
2171544Seschrock 				return (-1);
2181544Seschrock 			}
2191544Seschrock 			mutex_enter(&zfs_dev_mtx);
2201544Seschrock 			zfs_major = new_major;
2211544Seschrock 			zfs_minor = 0;
2221544Seschrock 
2231544Seschrock 			mutex_exit(&zfs_dev_mtx);
2241544Seschrock 		} else {
2251544Seschrock 			break;
2261544Seschrock 		}
2271544Seschrock 		/* CONSTANTCONDITION */
2281544Seschrock 	} while (1);
2291544Seschrock 
2301544Seschrock 	return (0);
2311544Seschrock }
2321544Seschrock 
233789Sahrens static void
234789Sahrens atime_changed_cb(void *arg, uint64_t newval)
235789Sahrens {
236789Sahrens 	zfsvfs_t *zfsvfs = arg;
237789Sahrens 
238789Sahrens 	if (newval == TRUE) {
239789Sahrens 		zfsvfs->z_atime = TRUE;
240789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
241789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
242789Sahrens 	} else {
243789Sahrens 		zfsvfs->z_atime = FALSE;
244789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
245789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
246789Sahrens 	}
247789Sahrens }
248789Sahrens 
249789Sahrens static void
2503234Sck153898 xattr_changed_cb(void *arg, uint64_t newval)
2513234Sck153898 {
2523234Sck153898 	zfsvfs_t *zfsvfs = arg;
2533234Sck153898 
2543234Sck153898 	if (newval == TRUE) {
2553234Sck153898 		/* XXX locking on vfs_flag? */
2563234Sck153898 		zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
2573234Sck153898 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
2583234Sck153898 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
2593234Sck153898 	} else {
2603234Sck153898 		/* XXX locking on vfs_flag? */
2613234Sck153898 		zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
2623234Sck153898 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
2633234Sck153898 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
2643234Sck153898 	}
2653234Sck153898 }
2663234Sck153898 
2673234Sck153898 static void
268789Sahrens blksz_changed_cb(void *arg, uint64_t newval)
269789Sahrens {
270789Sahrens 	zfsvfs_t *zfsvfs = arg;
271789Sahrens 
272789Sahrens 	if (newval < SPA_MINBLOCKSIZE ||
273789Sahrens 	    newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
274789Sahrens 		newval = SPA_MAXBLOCKSIZE;
275789Sahrens 
276789Sahrens 	zfsvfs->z_max_blksz = newval;
277789Sahrens 	zfsvfs->z_vfs->vfs_bsize = newval;
278789Sahrens }
279789Sahrens 
280789Sahrens static void
281789Sahrens readonly_changed_cb(void *arg, uint64_t newval)
282789Sahrens {
283789Sahrens 	zfsvfs_t *zfsvfs = arg;
284789Sahrens 
285789Sahrens 	if (newval) {
286789Sahrens 		/* XXX locking on vfs_flag? */
287789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
288789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
289789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
290789Sahrens 	} else {
291789Sahrens 		/* XXX locking on vfs_flag? */
292789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
293789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
294789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
295789Sahrens 	}
296789Sahrens }
297789Sahrens 
298789Sahrens static void
299789Sahrens devices_changed_cb(void *arg, uint64_t newval)
300789Sahrens {
301789Sahrens 	zfsvfs_t *zfsvfs = arg;
302789Sahrens 
303789Sahrens 	if (newval == FALSE) {
304789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
305789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
306789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
307789Sahrens 	} else {
308789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
309789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
310789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
311789Sahrens 	}
312789Sahrens }
313789Sahrens 
314789Sahrens static void
315789Sahrens setuid_changed_cb(void *arg, uint64_t newval)
316789Sahrens {
317789Sahrens 	zfsvfs_t *zfsvfs = arg;
318789Sahrens 
319789Sahrens 	if (newval == FALSE) {
320789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
321789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
322789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
323789Sahrens 	} else {
324789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
325789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
326789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
327789Sahrens 	}
328789Sahrens }
329789Sahrens 
330789Sahrens static void
331789Sahrens exec_changed_cb(void *arg, uint64_t newval)
332789Sahrens {
333789Sahrens 	zfsvfs_t *zfsvfs = arg;
334789Sahrens 
335789Sahrens 	if (newval == FALSE) {
336789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
337789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
338789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
339789Sahrens 	} else {
340789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
341789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
342789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
343789Sahrens 	}
344789Sahrens }
345789Sahrens 
3465331Samw /*
3475331Samw  * The nbmand mount option can be changed at mount time.
3485331Samw  * We can't allow it to be toggled on live file systems or incorrect
3495331Samw  * behavior may be seen from cifs clients
3505331Samw  *
3515331Samw  * This property isn't registered via dsl_prop_register(), but this callback
3525331Samw  * will be called when a file system is first mounted
3535331Samw  */
3545331Samw static void
3555331Samw nbmand_changed_cb(void *arg, uint64_t newval)
3565331Samw {
3575331Samw 	zfsvfs_t *zfsvfs = arg;
3585331Samw 	if (newval == FALSE) {
3595331Samw 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
3605331Samw 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
3615331Samw 	} else {
3625331Samw 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
3635331Samw 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
3645331Samw 	}
3655331Samw }
3665331Samw 
367789Sahrens static void
368789Sahrens snapdir_changed_cb(void *arg, uint64_t newval)
369789Sahrens {
370789Sahrens 	zfsvfs_t *zfsvfs = arg;
371789Sahrens 
372789Sahrens 	zfsvfs->z_show_ctldir = newval;
373789Sahrens }
374789Sahrens 
375789Sahrens static void
3765331Samw vscan_changed_cb(void *arg, uint64_t newval)
3775331Samw {
3785331Samw 	zfsvfs_t *zfsvfs = arg;
3795331Samw 
3805331Samw 	zfsvfs->z_vscan = newval;
3815331Samw }
3825331Samw 
3835331Samw static void
384789Sahrens acl_mode_changed_cb(void *arg, uint64_t newval)
385789Sahrens {
386789Sahrens 	zfsvfs_t *zfsvfs = arg;
387789Sahrens 
388789Sahrens 	zfsvfs->z_acl_mode = newval;
389789Sahrens }
390789Sahrens 
391789Sahrens static void
392789Sahrens acl_inherit_changed_cb(void *arg, uint64_t newval)
393789Sahrens {
394789Sahrens 	zfsvfs_t *zfsvfs = arg;
395789Sahrens 
396789Sahrens 	zfsvfs->z_acl_inherit = newval;
397789Sahrens }
398789Sahrens 
3991544Seschrock static int
4001544Seschrock zfs_register_callbacks(vfs_t *vfsp)
4011544Seschrock {
4021544Seschrock 	struct dsl_dataset *ds = NULL;
4031544Seschrock 	objset_t *os = NULL;
4041544Seschrock 	zfsvfs_t *zfsvfs = NULL;
4055331Samw 	uint64_t nbmand;
4065331Samw 	int readonly, do_readonly = B_FALSE;
4075331Samw 	int setuid, do_setuid = B_FALSE;
4085331Samw 	int exec, do_exec = B_FALSE;
4095331Samw 	int devices, do_devices = B_FALSE;
4105331Samw 	int xattr, do_xattr = B_FALSE;
4115331Samw 	int atime, do_atime = B_FALSE;
4121544Seschrock 	int error = 0;
4131544Seschrock 
4141544Seschrock 	ASSERT(vfsp);
4151544Seschrock 	zfsvfs = vfsp->vfs_data;
4161544Seschrock 	ASSERT(zfsvfs);
4171544Seschrock 	os = zfsvfs->z_os;
4181544Seschrock 
4191544Seschrock 	/*
4201544Seschrock 	 * The act of registering our callbacks will destroy any mount
4211544Seschrock 	 * options we may have.  In order to enable temporary overrides
4223234Sck153898 	 * of mount options, we stash away the current values and
4231544Seschrock 	 * restore them after we register the callbacks.
4241544Seschrock 	 */
4251544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
4261544Seschrock 		readonly = B_TRUE;
4271544Seschrock 		do_readonly = B_TRUE;
4281544Seschrock 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
4291544Seschrock 		readonly = B_FALSE;
4301544Seschrock 		do_readonly = B_TRUE;
4311544Seschrock 	}
4321544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
4331544Seschrock 		devices = B_FALSE;
4341544Seschrock 		setuid = B_FALSE;
4351544Seschrock 		do_devices = B_TRUE;
4361544Seschrock 		do_setuid = B_TRUE;
4371544Seschrock 	} else {
4381544Seschrock 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
4391544Seschrock 			devices = B_FALSE;
4401544Seschrock 			do_devices = B_TRUE;
4413912Slling 		} else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
4421544Seschrock 			devices = B_TRUE;
4431544Seschrock 			do_devices = B_TRUE;
4441544Seschrock 		}
4451544Seschrock 
4461544Seschrock 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
4471544Seschrock 			setuid = B_FALSE;
4481544Seschrock 			do_setuid = B_TRUE;
4491544Seschrock 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
4501544Seschrock 			setuid = B_TRUE;
4511544Seschrock 			do_setuid = B_TRUE;
4521544Seschrock 		}
4531544Seschrock 	}
4541544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
4551544Seschrock 		exec = B_FALSE;
4561544Seschrock 		do_exec = B_TRUE;
4571544Seschrock 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
4581544Seschrock 		exec = B_TRUE;
4591544Seschrock 		do_exec = B_TRUE;
4601544Seschrock 	}
4613234Sck153898 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
4623234Sck153898 		xattr = B_FALSE;
4633234Sck153898 		do_xattr = B_TRUE;
4643234Sck153898 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
4653234Sck153898 		xattr = B_TRUE;
4663234Sck153898 		do_xattr = B_TRUE;
4673234Sck153898 	}
4684596Slling 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
4694596Slling 		atime = B_FALSE;
4704596Slling 		do_atime = B_TRUE;
4714596Slling 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
4724596Slling 		atime = B_TRUE;
4734596Slling 		do_atime = B_TRUE;
4744596Slling 	}
4751544Seschrock 
4761544Seschrock 	/*
4775331Samw 	 * nbmand is a special property.  It can only be changed at
4785331Samw 	 * mount time.
4795331Samw 	 *
4805331Samw 	 * This is weird, but it is documented to only be changeable
4815331Samw 	 * at mount time.
4825331Samw 	 */
4835331Samw 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
4845331Samw 		nbmand = B_FALSE;
4855331Samw 	} else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
4865331Samw 		nbmand = B_TRUE;
4875331Samw 	} else {
4885331Samw 		char osname[MAXNAMELEN];
4895331Samw 
4905331Samw 		dmu_objset_name(os, osname);
4915331Samw 		if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
4927265Sahrens 		    NULL)) {
4937265Sahrens 			return (error);
4947265Sahrens 		}
4955331Samw 	}
4965331Samw 
4975331Samw 	/*
4981544Seschrock 	 * Register property callbacks.
4991544Seschrock 	 *
5001544Seschrock 	 * It would probably be fine to just check for i/o error from
5011544Seschrock 	 * the first prop_register(), but I guess I like to go
5021544Seschrock 	 * overboard...
5031544Seschrock 	 */
5041544Seschrock 	ds = dmu_objset_ds(os);
5051544Seschrock 	error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
5061544Seschrock 	error = error ? error : dsl_prop_register(ds,
5073234Sck153898 	    "xattr", xattr_changed_cb, zfsvfs);
5083234Sck153898 	error = error ? error : dsl_prop_register(ds,
5091544Seschrock 	    "recordsize", blksz_changed_cb, zfsvfs);
5101544Seschrock 	error = error ? error : dsl_prop_register(ds,
5111544Seschrock 	    "readonly", readonly_changed_cb, zfsvfs);
5121544Seschrock 	error = error ? error : dsl_prop_register(ds,
5131544Seschrock 	    "devices", devices_changed_cb, zfsvfs);
5141544Seschrock 	error = error ? error : dsl_prop_register(ds,
5151544Seschrock 	    "setuid", setuid_changed_cb, zfsvfs);
5161544Seschrock 	error = error ? error : dsl_prop_register(ds,
5171544Seschrock 	    "exec", exec_changed_cb, zfsvfs);
5181544Seschrock 	error = error ? error : dsl_prop_register(ds,
5191544Seschrock 	    "snapdir", snapdir_changed_cb, zfsvfs);
5201544Seschrock 	error = error ? error : dsl_prop_register(ds,
5211544Seschrock 	    "aclmode", acl_mode_changed_cb, zfsvfs);
5221544Seschrock 	error = error ? error : dsl_prop_register(ds,
5231544Seschrock 	    "aclinherit", acl_inherit_changed_cb, zfsvfs);
5245331Samw 	error = error ? error : dsl_prop_register(ds,
5255331Samw 	    "vscan", vscan_changed_cb, zfsvfs);
5261544Seschrock 	if (error)
5271544Seschrock 		goto unregister;
5281544Seschrock 
5291544Seschrock 	/*
5301544Seschrock 	 * Invoke our callbacks to restore temporary mount options.
5311544Seschrock 	 */
5321544Seschrock 	if (do_readonly)
5331544Seschrock 		readonly_changed_cb(zfsvfs, readonly);
5341544Seschrock 	if (do_setuid)
5351544Seschrock 		setuid_changed_cb(zfsvfs, setuid);
5361544Seschrock 	if (do_exec)
5371544Seschrock 		exec_changed_cb(zfsvfs, exec);
5381544Seschrock 	if (do_devices)
5391544Seschrock 		devices_changed_cb(zfsvfs, devices);
5403234Sck153898 	if (do_xattr)
5413234Sck153898 		xattr_changed_cb(zfsvfs, xattr);
5424596Slling 	if (do_atime)
5434596Slling 		atime_changed_cb(zfsvfs, atime);
5441544Seschrock 
5455331Samw 	nbmand_changed_cb(zfsvfs, nbmand);
5465331Samw 
5471544Seschrock 	return (0);
5481544Seschrock 
5491544Seschrock unregister:
5501544Seschrock 	/*
5511544Seschrock 	 * We may attempt to unregister some callbacks that are not
5521544Seschrock 	 * registered, but this is OK; it will simply return ENOMSG,
5531544Seschrock 	 * which we will ignore.
5541544Seschrock 	 */
5551544Seschrock 	(void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
5563234Sck153898 	(void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
5571544Seschrock 	(void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
5581544Seschrock 	(void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
5591544Seschrock 	(void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs);
5601544Seschrock 	(void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
5611544Seschrock 	(void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
5621544Seschrock 	(void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
5631544Seschrock 	(void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
5641544Seschrock 	(void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
5651544Seschrock 	    zfsvfs);
5665331Samw 	(void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
5671544Seschrock 	return (error);
5681544Seschrock 
5691544Seschrock }
5701544Seschrock 
5719396SMatthew.Ahrens@Sun.COM static void
5729396SMatthew.Ahrens@Sun.COM uidacct(objset_t *os, boolean_t isgroup, uint64_t fuid,
5739396SMatthew.Ahrens@Sun.COM     int64_t delta, dmu_tx_t *tx)
5749396SMatthew.Ahrens@Sun.COM {
5759396SMatthew.Ahrens@Sun.COM 	uint64_t used = 0;
5769396SMatthew.Ahrens@Sun.COM 	char buf[32];
5779396SMatthew.Ahrens@Sun.COM 	int err;
5789396SMatthew.Ahrens@Sun.COM 	uint64_t obj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
5799396SMatthew.Ahrens@Sun.COM 
5809396SMatthew.Ahrens@Sun.COM 	if (delta == 0)
5819396SMatthew.Ahrens@Sun.COM 		return;
5829396SMatthew.Ahrens@Sun.COM 
5839396SMatthew.Ahrens@Sun.COM 	(void) snprintf(buf, sizeof (buf), "%llx", (longlong_t)fuid);
5849396SMatthew.Ahrens@Sun.COM 	err = zap_lookup(os, obj, buf, 8, 1, &used);
5859396SMatthew.Ahrens@Sun.COM 	ASSERT(err == 0 || err == ENOENT);
5869396SMatthew.Ahrens@Sun.COM 	/* no underflow/overflow */
5879396SMatthew.Ahrens@Sun.COM 	ASSERT(delta > 0 || used >= -delta);
5889396SMatthew.Ahrens@Sun.COM 	ASSERT(delta < 0 || used + delta > used);
5899396SMatthew.Ahrens@Sun.COM 	used += delta;
5909396SMatthew.Ahrens@Sun.COM 	if (used == 0)
5919396SMatthew.Ahrens@Sun.COM 		err = zap_remove(os, obj, buf, tx);
5929396SMatthew.Ahrens@Sun.COM 	else
5939396SMatthew.Ahrens@Sun.COM 		err = zap_update(os, obj, buf, 8, 1, &used, tx);
5949396SMatthew.Ahrens@Sun.COM 	ASSERT(err == 0);
5959396SMatthew.Ahrens@Sun.COM }
5969396SMatthew.Ahrens@Sun.COM 
59710407SMatthew.Ahrens@Sun.COM static int
59810407SMatthew.Ahrens@Sun.COM zfs_space_delta_cb(dmu_object_type_t bonustype, void *bonus,
59910407SMatthew.Ahrens@Sun.COM     uint64_t *userp, uint64_t *groupp)
6009396SMatthew.Ahrens@Sun.COM {
60110407SMatthew.Ahrens@Sun.COM 	znode_phys_t *znp = bonus;
6029396SMatthew.Ahrens@Sun.COM 
6039396SMatthew.Ahrens@Sun.COM 	if (bonustype != DMU_OT_ZNODE)
60410407SMatthew.Ahrens@Sun.COM 		return (ENOENT);
6059396SMatthew.Ahrens@Sun.COM 
60610407SMatthew.Ahrens@Sun.COM 	*userp = znp->zp_uid;
60710407SMatthew.Ahrens@Sun.COM 	*groupp = znp->zp_gid;
60810407SMatthew.Ahrens@Sun.COM 	return (0);
6099396SMatthew.Ahrens@Sun.COM }
6109396SMatthew.Ahrens@Sun.COM 
6119396SMatthew.Ahrens@Sun.COM static void
6129396SMatthew.Ahrens@Sun.COM fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr,
6139396SMatthew.Ahrens@Sun.COM     char *domainbuf, int buflen, uid_t *ridp)
6149396SMatthew.Ahrens@Sun.COM {
6159396SMatthew.Ahrens@Sun.COM 	uint64_t fuid;
6169396SMatthew.Ahrens@Sun.COM 	const char *domain;
6179396SMatthew.Ahrens@Sun.COM 
6189396SMatthew.Ahrens@Sun.COM 	fuid = strtonum(fuidstr, NULL);
6199396SMatthew.Ahrens@Sun.COM 
6209396SMatthew.Ahrens@Sun.COM 	domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid));
6219396SMatthew.Ahrens@Sun.COM 	if (domain)
6229396SMatthew.Ahrens@Sun.COM 		(void) strlcpy(domainbuf, domain, buflen);
6239396SMatthew.Ahrens@Sun.COM 	else
6249396SMatthew.Ahrens@Sun.COM 		domainbuf[0] = '\0';
6259396SMatthew.Ahrens@Sun.COM 	*ridp = FUID_RID(fuid);
6269396SMatthew.Ahrens@Sun.COM }
6279396SMatthew.Ahrens@Sun.COM 
6289396SMatthew.Ahrens@Sun.COM static uint64_t
6299396SMatthew.Ahrens@Sun.COM zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type)
6309396SMatthew.Ahrens@Sun.COM {
6319396SMatthew.Ahrens@Sun.COM 	switch (type) {
6329396SMatthew.Ahrens@Sun.COM 	case ZFS_PROP_USERUSED:
6339396SMatthew.Ahrens@Sun.COM 		return (DMU_USERUSED_OBJECT);
6349396SMatthew.Ahrens@Sun.COM 	case ZFS_PROP_GROUPUSED:
6359396SMatthew.Ahrens@Sun.COM 		return (DMU_GROUPUSED_OBJECT);
6369396SMatthew.Ahrens@Sun.COM 	case ZFS_PROP_USERQUOTA:
6379396SMatthew.Ahrens@Sun.COM 		return (zfsvfs->z_userquota_obj);
6389396SMatthew.Ahrens@Sun.COM 	case ZFS_PROP_GROUPQUOTA:
6399396SMatthew.Ahrens@Sun.COM 		return (zfsvfs->z_groupquota_obj);
6409396SMatthew.Ahrens@Sun.COM 	}
6419396SMatthew.Ahrens@Sun.COM 	return (0);
6429396SMatthew.Ahrens@Sun.COM }
6439396SMatthew.Ahrens@Sun.COM 
6449396SMatthew.Ahrens@Sun.COM int
6459396SMatthew.Ahrens@Sun.COM zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
6469396SMatthew.Ahrens@Sun.COM     uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
6479396SMatthew.Ahrens@Sun.COM {
6489396SMatthew.Ahrens@Sun.COM 	int error;
6499396SMatthew.Ahrens@Sun.COM 	zap_cursor_t zc;
6509396SMatthew.Ahrens@Sun.COM 	zap_attribute_t za;
6519396SMatthew.Ahrens@Sun.COM 	zfs_useracct_t *buf = vbuf;
6529396SMatthew.Ahrens@Sun.COM 	uint64_t obj;
6539396SMatthew.Ahrens@Sun.COM 
6549396SMatthew.Ahrens@Sun.COM 	if (!dmu_objset_userspace_present(zfsvfs->z_os))
6559396SMatthew.Ahrens@Sun.COM 		return (ENOTSUP);
6569396SMatthew.Ahrens@Sun.COM 
6579396SMatthew.Ahrens@Sun.COM 	obj = zfs_userquota_prop_to_obj(zfsvfs, type);
6589396SMatthew.Ahrens@Sun.COM 	if (obj == 0) {
6599396SMatthew.Ahrens@Sun.COM 		*bufsizep = 0;
6609396SMatthew.Ahrens@Sun.COM 		return (0);
6619396SMatthew.Ahrens@Sun.COM 	}
6629396SMatthew.Ahrens@Sun.COM 
6639396SMatthew.Ahrens@Sun.COM 	for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
6649396SMatthew.Ahrens@Sun.COM 	    (error = zap_cursor_retrieve(&zc, &za)) == 0;
6659396SMatthew.Ahrens@Sun.COM 	    zap_cursor_advance(&zc)) {
6669396SMatthew.Ahrens@Sun.COM 		if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
6679396SMatthew.Ahrens@Sun.COM 		    *bufsizep)
6689396SMatthew.Ahrens@Sun.COM 			break;
6699396SMatthew.Ahrens@Sun.COM 
6709396SMatthew.Ahrens@Sun.COM 		fuidstr_to_sid(zfsvfs, za.za_name,
6719396SMatthew.Ahrens@Sun.COM 		    buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
6729396SMatthew.Ahrens@Sun.COM 
6739396SMatthew.Ahrens@Sun.COM 		buf->zu_space = za.za_first_integer;
6749396SMatthew.Ahrens@Sun.COM 		buf++;
6759396SMatthew.Ahrens@Sun.COM 	}
6769396SMatthew.Ahrens@Sun.COM 	if (error == ENOENT)
6779396SMatthew.Ahrens@Sun.COM 		error = 0;
6789396SMatthew.Ahrens@Sun.COM 
6799396SMatthew.Ahrens@Sun.COM 	ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
6809396SMatthew.Ahrens@Sun.COM 	*bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
6819396SMatthew.Ahrens@Sun.COM 	*cookiep = zap_cursor_serialize(&zc);
6829396SMatthew.Ahrens@Sun.COM 	zap_cursor_fini(&zc);
6839396SMatthew.Ahrens@Sun.COM 	return (error);
6849396SMatthew.Ahrens@Sun.COM }
6859396SMatthew.Ahrens@Sun.COM 
6869396SMatthew.Ahrens@Sun.COM /*
6879396SMatthew.Ahrens@Sun.COM  * buf must be big enough (eg, 32 bytes)
6889396SMatthew.Ahrens@Sun.COM  */
6899396SMatthew.Ahrens@Sun.COM static int
6909396SMatthew.Ahrens@Sun.COM id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
6919396SMatthew.Ahrens@Sun.COM     char *buf, boolean_t addok)
6929396SMatthew.Ahrens@Sun.COM {
6939396SMatthew.Ahrens@Sun.COM 	uint64_t fuid;
6949396SMatthew.Ahrens@Sun.COM 	int domainid = 0;
6959396SMatthew.Ahrens@Sun.COM 
6969396SMatthew.Ahrens@Sun.COM 	if (domain && domain[0]) {
6979396SMatthew.Ahrens@Sun.COM 		domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
6989396SMatthew.Ahrens@Sun.COM 		if (domainid == -1)
6999396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
7009396SMatthew.Ahrens@Sun.COM 	}
7019396SMatthew.Ahrens@Sun.COM 	fuid = FUID_ENCODE(domainid, rid);
7029396SMatthew.Ahrens@Sun.COM 	(void) sprintf(buf, "%llx", (longlong_t)fuid);
7039396SMatthew.Ahrens@Sun.COM 	return (0);
7049396SMatthew.Ahrens@Sun.COM }
7059396SMatthew.Ahrens@Sun.COM 
7069396SMatthew.Ahrens@Sun.COM int
7079396SMatthew.Ahrens@Sun.COM zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
7089396SMatthew.Ahrens@Sun.COM     const char *domain, uint64_t rid, uint64_t *valp)
7099396SMatthew.Ahrens@Sun.COM {
7109396SMatthew.Ahrens@Sun.COM 	char buf[32];
7119396SMatthew.Ahrens@Sun.COM 	int err;
7129396SMatthew.Ahrens@Sun.COM 	uint64_t obj;
7139396SMatthew.Ahrens@Sun.COM 
7149396SMatthew.Ahrens@Sun.COM 	*valp = 0;
7159396SMatthew.Ahrens@Sun.COM 
7169396SMatthew.Ahrens@Sun.COM 	if (!dmu_objset_userspace_present(zfsvfs->z_os))
7179396SMatthew.Ahrens@Sun.COM 		return (ENOTSUP);
7189396SMatthew.Ahrens@Sun.COM 
7199396SMatthew.Ahrens@Sun.COM 	obj = zfs_userquota_prop_to_obj(zfsvfs, type);
7209396SMatthew.Ahrens@Sun.COM 	if (obj == 0)
7219396SMatthew.Ahrens@Sun.COM 		return (0);
7229396SMatthew.Ahrens@Sun.COM 
7239396SMatthew.Ahrens@Sun.COM 	err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_FALSE);
7249396SMatthew.Ahrens@Sun.COM 	if (err)
7259396SMatthew.Ahrens@Sun.COM 		return (err);
7269396SMatthew.Ahrens@Sun.COM 
7279396SMatthew.Ahrens@Sun.COM 	err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
7289396SMatthew.Ahrens@Sun.COM 	if (err == ENOENT)
7299396SMatthew.Ahrens@Sun.COM 		err = 0;
7309396SMatthew.Ahrens@Sun.COM 	return (err);
7319396SMatthew.Ahrens@Sun.COM }
7329396SMatthew.Ahrens@Sun.COM 
7339396SMatthew.Ahrens@Sun.COM int
7349396SMatthew.Ahrens@Sun.COM zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
7359396SMatthew.Ahrens@Sun.COM     const char *domain, uint64_t rid, uint64_t quota)
7369396SMatthew.Ahrens@Sun.COM {
7379396SMatthew.Ahrens@Sun.COM 	char buf[32];
7389396SMatthew.Ahrens@Sun.COM 	int err;
7399396SMatthew.Ahrens@Sun.COM 	dmu_tx_t *tx;
7409396SMatthew.Ahrens@Sun.COM 	uint64_t *objp;
7419396SMatthew.Ahrens@Sun.COM 	boolean_t fuid_dirtied;
7429396SMatthew.Ahrens@Sun.COM 
7439396SMatthew.Ahrens@Sun.COM 	if (type != ZFS_PROP_USERQUOTA && type != ZFS_PROP_GROUPQUOTA)
7449396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
7459396SMatthew.Ahrens@Sun.COM 
7469396SMatthew.Ahrens@Sun.COM 	if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
7479396SMatthew.Ahrens@Sun.COM 		return (ENOTSUP);
7489396SMatthew.Ahrens@Sun.COM 
7499396SMatthew.Ahrens@Sun.COM 	objp = (type == ZFS_PROP_USERQUOTA) ? &zfsvfs->z_userquota_obj :
7509396SMatthew.Ahrens@Sun.COM 	    &zfsvfs->z_groupquota_obj;
7519396SMatthew.Ahrens@Sun.COM 
7529396SMatthew.Ahrens@Sun.COM 	err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE);
7539396SMatthew.Ahrens@Sun.COM 	if (err)
7549396SMatthew.Ahrens@Sun.COM 		return (err);
7559396SMatthew.Ahrens@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
7569396SMatthew.Ahrens@Sun.COM 
7579396SMatthew.Ahrens@Sun.COM 	tx = dmu_tx_create(zfsvfs->z_os);
7589396SMatthew.Ahrens@Sun.COM 	dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
7599396SMatthew.Ahrens@Sun.COM 	if (*objp == 0) {
7609396SMatthew.Ahrens@Sun.COM 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
7619396SMatthew.Ahrens@Sun.COM 		    zfs_userquota_prop_prefixes[type]);
7629396SMatthew.Ahrens@Sun.COM 	}
7639396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
7649396SMatthew.Ahrens@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
7659396SMatthew.Ahrens@Sun.COM 	err = dmu_tx_assign(tx, TXG_WAIT);
7669396SMatthew.Ahrens@Sun.COM 	if (err) {
7679396SMatthew.Ahrens@Sun.COM 		dmu_tx_abort(tx);
7689396SMatthew.Ahrens@Sun.COM 		return (err);
7699396SMatthew.Ahrens@Sun.COM 	}
7709396SMatthew.Ahrens@Sun.COM 
7719396SMatthew.Ahrens@Sun.COM 	mutex_enter(&zfsvfs->z_lock);
7729396SMatthew.Ahrens@Sun.COM 	if (*objp == 0) {
7739396SMatthew.Ahrens@Sun.COM 		*objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
7749396SMatthew.Ahrens@Sun.COM 		    DMU_OT_NONE, 0, tx);
7759396SMatthew.Ahrens@Sun.COM 		VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
7769396SMatthew.Ahrens@Sun.COM 		    zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
7779396SMatthew.Ahrens@Sun.COM 	}
7789396SMatthew.Ahrens@Sun.COM 	mutex_exit(&zfsvfs->z_lock);
7799396SMatthew.Ahrens@Sun.COM 
7809396SMatthew.Ahrens@Sun.COM 	if (quota == 0) {
7819396SMatthew.Ahrens@Sun.COM 		err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
7829396SMatthew.Ahrens@Sun.COM 		if (err == ENOENT)
7839396SMatthew.Ahrens@Sun.COM 			err = 0;
7849396SMatthew.Ahrens@Sun.COM 	} else {
7859396SMatthew.Ahrens@Sun.COM 		err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, &quota, tx);
7869396SMatthew.Ahrens@Sun.COM 	}
7879396SMatthew.Ahrens@Sun.COM 	ASSERT(err == 0);
7889396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
7899396SMatthew.Ahrens@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
7909396SMatthew.Ahrens@Sun.COM 	dmu_tx_commit(tx);
7919396SMatthew.Ahrens@Sun.COM 	return (err);
7929396SMatthew.Ahrens@Sun.COM }
7939396SMatthew.Ahrens@Sun.COM 
7949396SMatthew.Ahrens@Sun.COM boolean_t
7959396SMatthew.Ahrens@Sun.COM zfs_usergroup_overquota(zfsvfs_t *zfsvfs, boolean_t isgroup, uint64_t fuid)
7969396SMatthew.Ahrens@Sun.COM {
7979396SMatthew.Ahrens@Sun.COM 	char buf[32];
7989396SMatthew.Ahrens@Sun.COM 	uint64_t used, quota, usedobj, quotaobj;
7999396SMatthew.Ahrens@Sun.COM 	int err;
8009396SMatthew.Ahrens@Sun.COM 
8019396SMatthew.Ahrens@Sun.COM 	usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
8029396SMatthew.Ahrens@Sun.COM 	quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
8039396SMatthew.Ahrens@Sun.COM 
8049396SMatthew.Ahrens@Sun.COM 	if (quotaobj == 0 || zfsvfs->z_replay)
8059396SMatthew.Ahrens@Sun.COM 		return (B_FALSE);
8069396SMatthew.Ahrens@Sun.COM 
8079396SMatthew.Ahrens@Sun.COM 	(void) sprintf(buf, "%llx", (longlong_t)fuid);
8089396SMatthew.Ahrens@Sun.COM 	err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
8099396SMatthew.Ahrens@Sun.COM 	if (err != 0)
8109396SMatthew.Ahrens@Sun.COM 		return (B_FALSE);
8119396SMatthew.Ahrens@Sun.COM 
8129396SMatthew.Ahrens@Sun.COM 	err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
8139396SMatthew.Ahrens@Sun.COM 	if (err != 0)
8149396SMatthew.Ahrens@Sun.COM 		return (B_FALSE);
8159396SMatthew.Ahrens@Sun.COM 	return (used >= quota);
8169396SMatthew.Ahrens@Sun.COM }
8179396SMatthew.Ahrens@Sun.COM 
8189396SMatthew.Ahrens@Sun.COM int
81910298SMatthew.Ahrens@Sun.COM zfsvfs_create(const char *osname, zfsvfs_t **zvp)
8209396SMatthew.Ahrens@Sun.COM {
8219396SMatthew.Ahrens@Sun.COM 	objset_t *os;
8229396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
8239396SMatthew.Ahrens@Sun.COM 	uint64_t zval;
8249396SMatthew.Ahrens@Sun.COM 	int i, error;
8259396SMatthew.Ahrens@Sun.COM 
82610298SMatthew.Ahrens@Sun.COM 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
8279396SMatthew.Ahrens@Sun.COM 
82810298SMatthew.Ahrens@Sun.COM 	/*
82910298SMatthew.Ahrens@Sun.COM 	 * We claim to always be readonly so we can open snapshots;
83010298SMatthew.Ahrens@Sun.COM 	 * other ZPL code will prevent us from writing to snapshots.
83110298SMatthew.Ahrens@Sun.COM 	 */
83210298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zfsvfs, &os);
83310298SMatthew.Ahrens@Sun.COM 	if (error) {
83410298SMatthew.Ahrens@Sun.COM 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
83510298SMatthew.Ahrens@Sun.COM 		return (error);
8369396SMatthew.Ahrens@Sun.COM 	}
8379396SMatthew.Ahrens@Sun.COM 
8389396SMatthew.Ahrens@Sun.COM 	/*
8399396SMatthew.Ahrens@Sun.COM 	 * Initialize the zfs-specific filesystem structure.
8409396SMatthew.Ahrens@Sun.COM 	 * Should probably make this a kmem cache, shuffle fields,
8419396SMatthew.Ahrens@Sun.COM 	 * and just bzero up to z_hold_mtx[].
8429396SMatthew.Ahrens@Sun.COM 	 */
8439396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_vfs = NULL;
8449396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_parent = zfsvfs;
8459396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
8469396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
8479396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_os = os;
8489396SMatthew.Ahrens@Sun.COM 
8499396SMatthew.Ahrens@Sun.COM 	error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
8509396SMatthew.Ahrens@Sun.COM 	if (error) {
8519396SMatthew.Ahrens@Sun.COM 		goto out;
8529396SMatthew.Ahrens@Sun.COM 	} else if (zfsvfs->z_version > ZPL_VERSION) {
8539396SMatthew.Ahrens@Sun.COM 		(void) printf("Mismatched versions:  File system "
8549396SMatthew.Ahrens@Sun.COM 		    "is version %llu on-disk format, which is "
8559396SMatthew.Ahrens@Sun.COM 		    "incompatible with this software version %lld!",
8569396SMatthew.Ahrens@Sun.COM 		    (u_longlong_t)zfsvfs->z_version, ZPL_VERSION);
8579396SMatthew.Ahrens@Sun.COM 		error = ENOTSUP;
8589396SMatthew.Ahrens@Sun.COM 		goto out;
8599396SMatthew.Ahrens@Sun.COM 	}
8609396SMatthew.Ahrens@Sun.COM 
8619396SMatthew.Ahrens@Sun.COM 	if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
8629396SMatthew.Ahrens@Sun.COM 		goto out;
8639396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_norm = (int)zval;
8649396SMatthew.Ahrens@Sun.COM 
8659396SMatthew.Ahrens@Sun.COM 	if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
8669396SMatthew.Ahrens@Sun.COM 		goto out;
8679396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_utf8 = (zval != 0);
8689396SMatthew.Ahrens@Sun.COM 
8699396SMatthew.Ahrens@Sun.COM 	if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
8709396SMatthew.Ahrens@Sun.COM 		goto out;
8719396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_case = (uint_t)zval;
8729396SMatthew.Ahrens@Sun.COM 
8739396SMatthew.Ahrens@Sun.COM 	/*
8749396SMatthew.Ahrens@Sun.COM 	 * Fold case on file systems that are always or sometimes case
8759396SMatthew.Ahrens@Sun.COM 	 * insensitive.
8769396SMatthew.Ahrens@Sun.COM 	 */
8779396SMatthew.Ahrens@Sun.COM 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
8789396SMatthew.Ahrens@Sun.COM 	    zfsvfs->z_case == ZFS_CASE_MIXED)
8799396SMatthew.Ahrens@Sun.COM 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
8809396SMatthew.Ahrens@Sun.COM 
8819396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
8829396SMatthew.Ahrens@Sun.COM 
8839396SMatthew.Ahrens@Sun.COM 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
8849396SMatthew.Ahrens@Sun.COM 	    &zfsvfs->z_root);
8859396SMatthew.Ahrens@Sun.COM 	if (error)
8869396SMatthew.Ahrens@Sun.COM 		goto out;
8879396SMatthew.Ahrens@Sun.COM 	ASSERT(zfsvfs->z_root != 0);
8889396SMatthew.Ahrens@Sun.COM 
8899396SMatthew.Ahrens@Sun.COM 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
8909396SMatthew.Ahrens@Sun.COM 	    &zfsvfs->z_unlinkedobj);
8919396SMatthew.Ahrens@Sun.COM 	if (error)
8929396SMatthew.Ahrens@Sun.COM 		goto out;
8939396SMatthew.Ahrens@Sun.COM 
8949396SMatthew.Ahrens@Sun.COM 	error = zap_lookup(os, MASTER_NODE_OBJ,
8959396SMatthew.Ahrens@Sun.COM 	    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
8969396SMatthew.Ahrens@Sun.COM 	    8, 1, &zfsvfs->z_userquota_obj);
8979396SMatthew.Ahrens@Sun.COM 	if (error && error != ENOENT)
8989396SMatthew.Ahrens@Sun.COM 		goto out;
8999396SMatthew.Ahrens@Sun.COM 
9009396SMatthew.Ahrens@Sun.COM 	error = zap_lookup(os, MASTER_NODE_OBJ,
9019396SMatthew.Ahrens@Sun.COM 	    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
9029396SMatthew.Ahrens@Sun.COM 	    8, 1, &zfsvfs->z_groupquota_obj);
9039396SMatthew.Ahrens@Sun.COM 	if (error && error != ENOENT)
9049396SMatthew.Ahrens@Sun.COM 		goto out;
9059396SMatthew.Ahrens@Sun.COM 
9069396SMatthew.Ahrens@Sun.COM 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
9079396SMatthew.Ahrens@Sun.COM 	    &zfsvfs->z_fuid_obj);
9089396SMatthew.Ahrens@Sun.COM 	if (error && error != ENOENT)
9099396SMatthew.Ahrens@Sun.COM 		goto out;
9109396SMatthew.Ahrens@Sun.COM 
9119396SMatthew.Ahrens@Sun.COM 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
9129396SMatthew.Ahrens@Sun.COM 	    &zfsvfs->z_shares_dir);
9139396SMatthew.Ahrens@Sun.COM 	if (error && error != ENOENT)
9149396SMatthew.Ahrens@Sun.COM 		goto out;
9159396SMatthew.Ahrens@Sun.COM 
9169396SMatthew.Ahrens@Sun.COM 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
9179396SMatthew.Ahrens@Sun.COM 	mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
9189396SMatthew.Ahrens@Sun.COM 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
9199396SMatthew.Ahrens@Sun.COM 	    offsetof(znode_t, z_link_node));
9209396SMatthew.Ahrens@Sun.COM 	rrw_init(&zfsvfs->z_teardown_lock);
9219396SMatthew.Ahrens@Sun.COM 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
9229396SMatthew.Ahrens@Sun.COM 	rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
9239396SMatthew.Ahrens@Sun.COM 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
9249396SMatthew.Ahrens@Sun.COM 		mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
9259396SMatthew.Ahrens@Sun.COM 
9269396SMatthew.Ahrens@Sun.COM 	*zvp = zfsvfs;
9279396SMatthew.Ahrens@Sun.COM 	return (0);
9289396SMatthew.Ahrens@Sun.COM 
9299396SMatthew.Ahrens@Sun.COM out:
93010298SMatthew.Ahrens@Sun.COM 	dmu_objset_disown(os, zfsvfs);
9319396SMatthew.Ahrens@Sun.COM 	*zvp = NULL;
9329396SMatthew.Ahrens@Sun.COM 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
9339396SMatthew.Ahrens@Sun.COM 	return (error);
9349396SMatthew.Ahrens@Sun.COM }
9359396SMatthew.Ahrens@Sun.COM 
9361544Seschrock static int
9375326Sek110237 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
9385326Sek110237 {
9395326Sek110237 	int error;
9405326Sek110237 
9415326Sek110237 	error = zfs_register_callbacks(zfsvfs->z_vfs);
9425326Sek110237 	if (error)
9435326Sek110237 		return (error);
9445326Sek110237 
9455326Sek110237 	/*
9465326Sek110237 	 * Set the objset user_ptr to track its zfsvfs.
9475326Sek110237 	 */
94810298SMatthew.Ahrens@Sun.COM 	mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
9495326Sek110237 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
95010298SMatthew.Ahrens@Sun.COM 	mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
9515326Sek110237 
9529292SNeil.Perrin@Sun.COM 	zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
9539292SNeil.Perrin@Sun.COM 	if (zil_disable) {
95410685SGeorge.Wilson@Sun.COM 		zil_destroy(zfsvfs->z_log, B_FALSE);
9559292SNeil.Perrin@Sun.COM 		zfsvfs->z_log = NULL;
9569292SNeil.Perrin@Sun.COM 	}
9579292SNeil.Perrin@Sun.COM 
9585326Sek110237 	/*
9595326Sek110237 	 * If we are not mounting (ie: online recv), then we don't
9605326Sek110237 	 * have to worry about replaying the log as we blocked all
9615326Sek110237 	 * operations out since we closed the ZIL.
9625326Sek110237 	 */
9635326Sek110237 	if (mounting) {
9647638SNeil.Perrin@Sun.COM 		boolean_t readonly;
9657638SNeil.Perrin@Sun.COM 
9665326Sek110237 		/*
9675326Sek110237 		 * During replay we remove the read only flag to
9685326Sek110237 		 * allow replays to succeed.
9695326Sek110237 		 */
9705326Sek110237 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
9718227SNeil.Perrin@Sun.COM 		if (readonly != 0)
9728227SNeil.Perrin@Sun.COM 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
9738227SNeil.Perrin@Sun.COM 		else
9748227SNeil.Perrin@Sun.COM 			zfs_unlinked_drain(zfsvfs);
9755326Sek110237 
9769292SNeil.Perrin@Sun.COM 		if (zfsvfs->z_log) {
9778227SNeil.Perrin@Sun.COM 			/*
9788227SNeil.Perrin@Sun.COM 			 * Parse and replay the intent log.
9798227SNeil.Perrin@Sun.COM 			 *
9808227SNeil.Perrin@Sun.COM 			 * Because of ziltest, this must be done after
9818227SNeil.Perrin@Sun.COM 			 * zfs_unlinked_drain().  (Further note: ziltest
9828227SNeil.Perrin@Sun.COM 			 * doesn't use readonly mounts, where
9838227SNeil.Perrin@Sun.COM 			 * zfs_unlinked_drain() isn't called.)  This is because
9848227SNeil.Perrin@Sun.COM 			 * ziltest causes spa_sync() to think it's committed,
9858227SNeil.Perrin@Sun.COM 			 * but actually it is not, so the intent log contains
9868227SNeil.Perrin@Sun.COM 			 * many txg's worth of changes.
9878227SNeil.Perrin@Sun.COM 			 *
9888227SNeil.Perrin@Sun.COM 			 * In particular, if object N is in the unlinked set in
9898227SNeil.Perrin@Sun.COM 			 * the last txg to actually sync, then it could be
9908227SNeil.Perrin@Sun.COM 			 * actually freed in a later txg and then reallocated
9918227SNeil.Perrin@Sun.COM 			 * in a yet later txg.  This would write a "create
9928227SNeil.Perrin@Sun.COM 			 * object N" record to the intent log.  Normally, this
9938227SNeil.Perrin@Sun.COM 			 * would be fine because the spa_sync() would have
9948227SNeil.Perrin@Sun.COM 			 * written out the fact that object N is free, before
9958227SNeil.Perrin@Sun.COM 			 * we could write the "create object N" intent log
9968227SNeil.Perrin@Sun.COM 			 * record.
9978227SNeil.Perrin@Sun.COM 			 *
9988227SNeil.Perrin@Sun.COM 			 * But when we are in ziltest mode, we advance the "open
9998227SNeil.Perrin@Sun.COM 			 * txg" without actually spa_sync()-ing the changes to
10008227SNeil.Perrin@Sun.COM 			 * disk.  So we would see that object N is still
10018227SNeil.Perrin@Sun.COM 			 * allocated and in the unlinked set, and there is an
10028227SNeil.Perrin@Sun.COM 			 * intent log record saying to allocate it.
10038227SNeil.Perrin@Sun.COM 			 */
10048227SNeil.Perrin@Sun.COM 			zfsvfs->z_replay = B_TRUE;
10058227SNeil.Perrin@Sun.COM 			zil_replay(zfsvfs->z_os, zfsvfs, zfs_replay_vector);
10068227SNeil.Perrin@Sun.COM 			zfsvfs->z_replay = B_FALSE;
10078227SNeil.Perrin@Sun.COM 		}
10085326Sek110237 		zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
10095326Sek110237 	}
10105326Sek110237 
10115326Sek110237 	return (0);
10125326Sek110237 }
10135326Sek110237 
10149396SMatthew.Ahrens@Sun.COM void
10159396SMatthew.Ahrens@Sun.COM zfsvfs_free(zfsvfs_t *zfsvfs)
10166083Sek110237 {
10179396SMatthew.Ahrens@Sun.COM 	int i;
10189788STom.Erickson@Sun.COM 	extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
10199788STom.Erickson@Sun.COM 
10209788STom.Erickson@Sun.COM 	/*
10219788STom.Erickson@Sun.COM 	 * This is a barrier to prevent the filesystem from going away in
10229788STom.Erickson@Sun.COM 	 * zfs_znode_move() until we can safely ensure that the filesystem is
10239788STom.Erickson@Sun.COM 	 * not unmounted. We consider the filesystem valid before the barrier
10249788STom.Erickson@Sun.COM 	 * and invalid after the barrier.
10259788STom.Erickson@Sun.COM 	 */
10269788STom.Erickson@Sun.COM 	rw_enter(&zfsvfs_lock, RW_READER);
10279788STom.Erickson@Sun.COM 	rw_exit(&zfsvfs_lock);
10289396SMatthew.Ahrens@Sun.COM 
10299396SMatthew.Ahrens@Sun.COM 	zfs_fuid_destroy(zfsvfs);
10309396SMatthew.Ahrens@Sun.COM 
10316083Sek110237 	mutex_destroy(&zfsvfs->z_znodes_lock);
10329030SMark.Shellenbaum@Sun.COM 	mutex_destroy(&zfsvfs->z_lock);
10336083Sek110237 	list_destroy(&zfsvfs->z_all_znodes);
10346083Sek110237 	rrw_destroy(&zfsvfs->z_teardown_lock);
10356083Sek110237 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
10366083Sek110237 	rw_destroy(&zfsvfs->z_fuid_lock);
10379396SMatthew.Ahrens@Sun.COM 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
10389396SMatthew.Ahrens@Sun.COM 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
10396083Sek110237 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
10406083Sek110237 }
10416083Sek110237 
10429396SMatthew.Ahrens@Sun.COM static void
10439396SMatthew.Ahrens@Sun.COM zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
10449396SMatthew.Ahrens@Sun.COM {
10459396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
10469396SMatthew.Ahrens@Sun.COM 	if (zfsvfs->z_use_fuids && zfsvfs->z_vfs) {
10479396SMatthew.Ahrens@Sun.COM 		vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
10489396SMatthew.Ahrens@Sun.COM 		vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
10499396SMatthew.Ahrens@Sun.COM 		vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
10509396SMatthew.Ahrens@Sun.COM 		vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
10519749STim.Haley@Sun.COM 		vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
105210793Sdai.ngo@sun.com 		vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
10539396SMatthew.Ahrens@Sun.COM 	}
10549396SMatthew.Ahrens@Sun.COM }
10559396SMatthew.Ahrens@Sun.COM 
10565326Sek110237 static int
10577046Sahrens zfs_domount(vfs_t *vfsp, char *osname)
10581544Seschrock {
10591544Seschrock 	dev_t mount_dev;
10609396SMatthew.Ahrens@Sun.COM 	uint64_t recordsize, fsid_guid;
10611544Seschrock 	int error = 0;
10621544Seschrock 	zfsvfs_t *zfsvfs;
10631544Seschrock 
10641544Seschrock 	ASSERT(vfsp);
10651544Seschrock 	ASSERT(osname);
10661544Seschrock 
106710298SMatthew.Ahrens@Sun.COM 	error = zfsvfs_create(osname, &zfsvfs);
10689396SMatthew.Ahrens@Sun.COM 	if (error)
10699396SMatthew.Ahrens@Sun.COM 		return (error);
10701544Seschrock 	zfsvfs->z_vfs = vfsp;
10711544Seschrock 
10721544Seschrock 	/* Initialize the generic filesystem structure. */
10731544Seschrock 	vfsp->vfs_bcount = 0;
10741544Seschrock 	vfsp->vfs_data = NULL;
10751544Seschrock 
10761544Seschrock 	if (zfs_create_unique_device(&mount_dev) == -1) {
10771544Seschrock 		error = ENODEV;
10781544Seschrock 		goto out;
10791544Seschrock 	}
10801544Seschrock 	ASSERT(vfs_devismounted(mount_dev) == 0);
10811544Seschrock 
10821544Seschrock 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
10831544Seschrock 	    NULL))
10841544Seschrock 		goto out;
10851544Seschrock 
10861544Seschrock 	vfsp->vfs_dev = mount_dev;
10871544Seschrock 	vfsp->vfs_fstype = zfsfstype;
10881544Seschrock 	vfsp->vfs_bsize = recordsize;
10891544Seschrock 	vfsp->vfs_flag |= VFS_NOTRUNC;
10901544Seschrock 	vfsp->vfs_data = zfsvfs;
10911544Seschrock 
10929396SMatthew.Ahrens@Sun.COM 	/*
10939396SMatthew.Ahrens@Sun.COM 	 * The fsid is 64 bits, composed of an 8-bit fs type, which
10949396SMatthew.Ahrens@Sun.COM 	 * separates our fsid from any other filesystem types, and a
10959396SMatthew.Ahrens@Sun.COM 	 * 56-bit objset unique ID.  The objset unique ID is unique to
10969396SMatthew.Ahrens@Sun.COM 	 * all objsets open on this system, provided by unique_create().
10979396SMatthew.Ahrens@Sun.COM 	 * The 8-bit fs type must be put in the low bits of fsid[1]
10989396SMatthew.Ahrens@Sun.COM 	 * because that's where other Solaris filesystems put it.
10999396SMatthew.Ahrens@Sun.COM 	 */
11009396SMatthew.Ahrens@Sun.COM 	fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
11019396SMatthew.Ahrens@Sun.COM 	ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
11029396SMatthew.Ahrens@Sun.COM 	vfsp->vfs_fsid.val[0] = fsid_guid;
11039396SMatthew.Ahrens@Sun.COM 	vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
11049396SMatthew.Ahrens@Sun.COM 	    zfsfstype & 0xFF;
11051544Seschrock 
11065331Samw 	/*
11075331Samw 	 * Set features for file system.
11085331Samw 	 */
11099396SMatthew.Ahrens@Sun.COM 	zfs_set_fuid_feature(zfsvfs);
11105498Stimh 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
11115498Stimh 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
11125498Stimh 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
11135498Stimh 		vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
11145498Stimh 	} else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
11155498Stimh 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
11165498Stimh 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
11175498Stimh 	}
11185331Samw 
11191544Seschrock 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
11205331Samw 		uint64_t pval;
11213234Sck153898 
11221544Seschrock 		atime_changed_cb(zfsvfs, B_FALSE);
11231544Seschrock 		readonly_changed_cb(zfsvfs, B_TRUE);
11245331Samw 		if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
11253234Sck153898 			goto out;
11265331Samw 		xattr_changed_cb(zfsvfs, pval);
11271544Seschrock 		zfsvfs->z_issnap = B_TRUE;
11289688SMatthew.Ahrens@Sun.COM 
112910298SMatthew.Ahrens@Sun.COM 		mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
11309688SMatthew.Ahrens@Sun.COM 		dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
113110298SMatthew.Ahrens@Sun.COM 		mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
11321544Seschrock 	} else {
11335326Sek110237 		error = zfsvfs_setup(zfsvfs, B_TRUE);
11341544Seschrock 	}
11351544Seschrock 
11361544Seschrock 	if (!zfsvfs->z_issnap)
11371544Seschrock 		zfsctl_create(zfsvfs);
11381544Seschrock out:
11391544Seschrock 	if (error) {
114010298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
11419396SMatthew.Ahrens@Sun.COM 		zfsvfs_free(zfsvfs);
11421544Seschrock 	} else {
11431544Seschrock 		atomic_add_32(&zfs_active_fs_count, 1);
11441544Seschrock 	}
11451544Seschrock 
11461544Seschrock 	return (error);
11471544Seschrock }
11481544Seschrock 
11491544Seschrock void
11501544Seschrock zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
11511544Seschrock {
11521544Seschrock 	objset_t *os = zfsvfs->z_os;
11531544Seschrock 	struct dsl_dataset *ds;
11541544Seschrock 
11551544Seschrock 	/*
11561544Seschrock 	 * Unregister properties.
11571544Seschrock 	 */
11581544Seschrock 	if (!dmu_objset_is_snapshot(os)) {
11591544Seschrock 		ds = dmu_objset_ds(os);
11601544Seschrock 		VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
11611544Seschrock 		    zfsvfs) == 0);
11621544Seschrock 
11633234Sck153898 		VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
11643234Sck153898 		    zfsvfs) == 0);
11653234Sck153898 
11661544Seschrock 		VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
11671544Seschrock 		    zfsvfs) == 0);
11681544Seschrock 
11691544Seschrock 		VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
11701544Seschrock 		    zfsvfs) == 0);
11711544Seschrock 
11721544Seschrock 		VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
11731544Seschrock 		    zfsvfs) == 0);
11741544Seschrock 
11751544Seschrock 		VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
11761544Seschrock 		    zfsvfs) == 0);
11771544Seschrock 
11781544Seschrock 		VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
11791544Seschrock 		    zfsvfs) == 0);
11801544Seschrock 
11811544Seschrock 		VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
11821544Seschrock 		    zfsvfs) == 0);
11831544Seschrock 
11841544Seschrock 		VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
11851544Seschrock 		    zfsvfs) == 0);
11861544Seschrock 
11871544Seschrock 		VERIFY(dsl_prop_unregister(ds, "aclinherit",
11881544Seschrock 		    acl_inherit_changed_cb, zfsvfs) == 0);
11895331Samw 
11905331Samw 		VERIFY(dsl_prop_unregister(ds, "vscan",
11915331Samw 		    vscan_changed_cb, zfsvfs) == 0);
11921544Seschrock 	}
11931544Seschrock }
11941544Seschrock 
11953912Slling /*
11963912Slling  * Convert a decimal digit string to a uint64_t integer.
11973912Slling  */
11983912Slling static int
11993912Slling str_to_uint64(char *str, uint64_t *objnum)
12003912Slling {
12013912Slling 	uint64_t num = 0;
12023912Slling 
12033912Slling 	while (*str) {
12043912Slling 		if (*str < '0' || *str > '9')
12053912Slling 			return (EINVAL);
12063912Slling 
12073912Slling 		num = num*10 + *str++ - '0';
12083912Slling 	}
12093912Slling 
12103912Slling 	*objnum = num;
12113912Slling 	return (0);
12123912Slling }
12133912Slling 
12143912Slling /*
12153912Slling  * The boot path passed from the boot loader is in the form of
12163912Slling  * "rootpool-name/root-filesystem-object-number'. Convert this
12173912Slling  * string to a dataset name: "rootpool-name/root-filesystem-name".
12183912Slling  */
12193912Slling static int
12206423Sgw25295 zfs_parse_bootfs(char *bpath, char *outpath)
12213912Slling {
12223912Slling 	char *slashp;
12233912Slling 	uint64_t objnum;
12243912Slling 	int error;
12253912Slling 
12263912Slling 	if (*bpath == 0 || *bpath == '/')
12273912Slling 		return (EINVAL);
12283912Slling 
12297656SSherry.Moore@Sun.COM 	(void) strcpy(outpath, bpath);
12307656SSherry.Moore@Sun.COM 
12313912Slling 	slashp = strchr(bpath, '/');
12323912Slling 
12333912Slling 	/* if no '/', just return the pool name */
12343912Slling 	if (slashp == NULL) {
12353912Slling 		return (0);
12363912Slling 	}
12373912Slling 
12387656SSherry.Moore@Sun.COM 	/* if not a number, just return the root dataset name */
12397656SSherry.Moore@Sun.COM 	if (str_to_uint64(slashp+1, &objnum)) {
12407656SSherry.Moore@Sun.COM 		return (0);
12417656SSherry.Moore@Sun.COM 	}
12423912Slling 
12433912Slling 	*slashp = '\0';
12443912Slling 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
12453912Slling 	*slashp = '/';
12463912Slling 
12473912Slling 	return (error);
12483912Slling }
12493912Slling 
1250*10972SRic.Aleshire@Sun.COM /*
1251*10972SRic.Aleshire@Sun.COM  * zfs_check_global_label:
1252*10972SRic.Aleshire@Sun.COM  *	Check that the hex label string is appropriate for the dataset
1253*10972SRic.Aleshire@Sun.COM  *	being mounted into the global_zone proper.
1254*10972SRic.Aleshire@Sun.COM  *
1255*10972SRic.Aleshire@Sun.COM  *	Return an error if the hex label string is not default or
1256*10972SRic.Aleshire@Sun.COM  *	admin_low/admin_high.  For admin_low labels, the corresponding
1257*10972SRic.Aleshire@Sun.COM  *	dataset must be readonly.
1258*10972SRic.Aleshire@Sun.COM  */
1259*10972SRic.Aleshire@Sun.COM int
1260*10972SRic.Aleshire@Sun.COM zfs_check_global_label(const char *dsname, const char *hexsl)
1261*10972SRic.Aleshire@Sun.COM {
1262*10972SRic.Aleshire@Sun.COM 	if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1263*10972SRic.Aleshire@Sun.COM 		return (0);
1264*10972SRic.Aleshire@Sun.COM 	if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
1265*10972SRic.Aleshire@Sun.COM 		return (0);
1266*10972SRic.Aleshire@Sun.COM 	if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
1267*10972SRic.Aleshire@Sun.COM 		/* must be readonly */
1268*10972SRic.Aleshire@Sun.COM 		uint64_t rdonly;
1269*10972SRic.Aleshire@Sun.COM 
1270*10972SRic.Aleshire@Sun.COM 		if (dsl_prop_get_integer(dsname,
1271*10972SRic.Aleshire@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
1272*10972SRic.Aleshire@Sun.COM 			return (EACCES);
1273*10972SRic.Aleshire@Sun.COM 		return (rdonly ? 0 : EACCES);
1274*10972SRic.Aleshire@Sun.COM 	}
1275*10972SRic.Aleshire@Sun.COM 	return (EACCES);
1276*10972SRic.Aleshire@Sun.COM }
1277*10972SRic.Aleshire@Sun.COM 
1278*10972SRic.Aleshire@Sun.COM /*
1279*10972SRic.Aleshire@Sun.COM  * zfs_mount_label_policy:
1280*10972SRic.Aleshire@Sun.COM  *	Determine whether the mount is allowed according to MAC check.
1281*10972SRic.Aleshire@Sun.COM  *	by comparing (where appropriate) label of the dataset against
1282*10972SRic.Aleshire@Sun.COM  *	the label of the zone being mounted into.  If the dataset has
1283*10972SRic.Aleshire@Sun.COM  *	no label, create one.
1284*10972SRic.Aleshire@Sun.COM  *
1285*10972SRic.Aleshire@Sun.COM  *	Returns:
1286*10972SRic.Aleshire@Sun.COM  *		 0 :	access allowed
1287*10972SRic.Aleshire@Sun.COM  *		>0 :	error code, such as EACCES
1288*10972SRic.Aleshire@Sun.COM  */
1289*10972SRic.Aleshire@Sun.COM static int
1290*10972SRic.Aleshire@Sun.COM zfs_mount_label_policy(vfs_t *vfsp, char *osname)
1291*10972SRic.Aleshire@Sun.COM {
1292*10972SRic.Aleshire@Sun.COM 	int		error, retv;
1293*10972SRic.Aleshire@Sun.COM 	zone_t		*mntzone = NULL;
1294*10972SRic.Aleshire@Sun.COM 	ts_label_t	*mnt_tsl;
1295*10972SRic.Aleshire@Sun.COM 	bslabel_t	*mnt_sl;
1296*10972SRic.Aleshire@Sun.COM 	bslabel_t	ds_sl;
1297*10972SRic.Aleshire@Sun.COM 	char		ds_hexsl[MAXNAMELEN];
1298*10972SRic.Aleshire@Sun.COM 	char		*str2 = NULL;
1299*10972SRic.Aleshire@Sun.COM 
1300*10972SRic.Aleshire@Sun.COM 	retv = EACCES;				/* assume the worst */
1301*10972SRic.Aleshire@Sun.COM 
1302*10972SRic.Aleshire@Sun.COM 	/*
1303*10972SRic.Aleshire@Sun.COM 	 * Start by getting the dataset label if it exists.
1304*10972SRic.Aleshire@Sun.COM 	 */
1305*10972SRic.Aleshire@Sun.COM 	error = dsl_prop_get(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1306*10972SRic.Aleshire@Sun.COM 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
1307*10972SRic.Aleshire@Sun.COM 	if (error)
1308*10972SRic.Aleshire@Sun.COM 		return (EACCES);
1309*10972SRic.Aleshire@Sun.COM 
1310*10972SRic.Aleshire@Sun.COM 	/*
1311*10972SRic.Aleshire@Sun.COM 	 * If labeling is NOT enabled, then disallow the mount of datasets
1312*10972SRic.Aleshire@Sun.COM 	 * which have a non-default label already.  No other label checks
1313*10972SRic.Aleshire@Sun.COM 	 * are needed.
1314*10972SRic.Aleshire@Sun.COM 	 */
1315*10972SRic.Aleshire@Sun.COM 	if (!is_system_labeled()) {
1316*10972SRic.Aleshire@Sun.COM 		if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1317*10972SRic.Aleshire@Sun.COM 			return (0);
1318*10972SRic.Aleshire@Sun.COM 		return (EACCES);
1319*10972SRic.Aleshire@Sun.COM 	}
1320*10972SRic.Aleshire@Sun.COM 
1321*10972SRic.Aleshire@Sun.COM 	/*
1322*10972SRic.Aleshire@Sun.COM 	 * Get the label of the mountpoint.  If mounting into the global
1323*10972SRic.Aleshire@Sun.COM 	 * zone (i.e. mountpoint is not within an active zone and the
1324*10972SRic.Aleshire@Sun.COM 	 * zoned property is off), the label must be default or
1325*10972SRic.Aleshire@Sun.COM 	 * admin_low/admin_high only; no other checks are needed.
1326*10972SRic.Aleshire@Sun.COM 	 */
1327*10972SRic.Aleshire@Sun.COM 	mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE);
1328*10972SRic.Aleshire@Sun.COM 	if (mntzone->zone_id == GLOBAL_ZONEID) {
1329*10972SRic.Aleshire@Sun.COM 		uint64_t zoned;
1330*10972SRic.Aleshire@Sun.COM 
1331*10972SRic.Aleshire@Sun.COM 		zone_rele(mntzone);
1332*10972SRic.Aleshire@Sun.COM 
1333*10972SRic.Aleshire@Sun.COM 		if (dsl_prop_get_integer(osname,
1334*10972SRic.Aleshire@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
1335*10972SRic.Aleshire@Sun.COM 			return (EACCES);
1336*10972SRic.Aleshire@Sun.COM 		if (!zoned)
1337*10972SRic.Aleshire@Sun.COM 			return (zfs_check_global_label(osname, ds_hexsl));
1338*10972SRic.Aleshire@Sun.COM 		else
1339*10972SRic.Aleshire@Sun.COM 			/*
1340*10972SRic.Aleshire@Sun.COM 			 * This is the case of a zone dataset being mounted
1341*10972SRic.Aleshire@Sun.COM 			 * initially, before the zone has been fully created;
1342*10972SRic.Aleshire@Sun.COM 			 * allow this mount into global zone.
1343*10972SRic.Aleshire@Sun.COM 			 */
1344*10972SRic.Aleshire@Sun.COM 			return (0);
1345*10972SRic.Aleshire@Sun.COM 	}
1346*10972SRic.Aleshire@Sun.COM 
1347*10972SRic.Aleshire@Sun.COM 	mnt_tsl = mntzone->zone_slabel;
1348*10972SRic.Aleshire@Sun.COM 	ASSERT(mnt_tsl != NULL);
1349*10972SRic.Aleshire@Sun.COM 	label_hold(mnt_tsl);
1350*10972SRic.Aleshire@Sun.COM 	mnt_sl = label2bslabel(mnt_tsl);
1351*10972SRic.Aleshire@Sun.COM 
1352*10972SRic.Aleshire@Sun.COM 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) {
1353*10972SRic.Aleshire@Sun.COM 		/*
1354*10972SRic.Aleshire@Sun.COM 		 * The dataset doesn't have a real label, so fabricate one.
1355*10972SRic.Aleshire@Sun.COM 		 */
1356*10972SRic.Aleshire@Sun.COM 		char *str = NULL;
1357*10972SRic.Aleshire@Sun.COM 
1358*10972SRic.Aleshire@Sun.COM 		if (l_to_str_internal(mnt_sl, &str) == 0 &&
1359*10972SRic.Aleshire@Sun.COM 		    dsl_prop_set(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1360*10972SRic.Aleshire@Sun.COM 		    1, strlen(str) + 1, str) == 0)
1361*10972SRic.Aleshire@Sun.COM 			retv = 0;
1362*10972SRic.Aleshire@Sun.COM 		if (str != NULL)
1363*10972SRic.Aleshire@Sun.COM 			kmem_free(str, strlen(str) + 1);
1364*10972SRic.Aleshire@Sun.COM 	} else if (hexstr_to_label(ds_hexsl, &ds_sl) == 0) {
1365*10972SRic.Aleshire@Sun.COM 		/*
1366*10972SRic.Aleshire@Sun.COM 		 * Now compare labels to complete the MAC check.  If the
1367*10972SRic.Aleshire@Sun.COM 		 * labels are equal then allow access.  If the mountpoint
1368*10972SRic.Aleshire@Sun.COM 		 * label dominates the dataset label, allow readonly access.
1369*10972SRic.Aleshire@Sun.COM 		 * Otherwise, access is denied.
1370*10972SRic.Aleshire@Sun.COM 		 */
1371*10972SRic.Aleshire@Sun.COM 		if (blequal(mnt_sl, &ds_sl))
1372*10972SRic.Aleshire@Sun.COM 			retv = 0;
1373*10972SRic.Aleshire@Sun.COM 		else if (bldominates(mnt_sl, &ds_sl)) {
1374*10972SRic.Aleshire@Sun.COM 			vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
1375*10972SRic.Aleshire@Sun.COM 			retv = 0;
1376*10972SRic.Aleshire@Sun.COM 		}
1377*10972SRic.Aleshire@Sun.COM 	}
1378*10972SRic.Aleshire@Sun.COM 
1379*10972SRic.Aleshire@Sun.COM 	label_rele(mnt_tsl);
1380*10972SRic.Aleshire@Sun.COM 	zone_rele(mntzone);
1381*10972SRic.Aleshire@Sun.COM 	return (retv);
1382*10972SRic.Aleshire@Sun.COM }
1383*10972SRic.Aleshire@Sun.COM 
13841544Seschrock static int
13851544Seschrock zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
13861544Seschrock {
13871544Seschrock 	int error = 0;
13881544Seschrock 	static int zfsrootdone = 0;
13891544Seschrock 	zfsvfs_t *zfsvfs = NULL;
13901544Seschrock 	znode_t *zp = NULL;
13911544Seschrock 	vnode_t *vp = NULL;
13926423Sgw25295 	char *zfs_bootfs;
13937147Staylor 	char *zfs_devid;
13941544Seschrock 
13951544Seschrock 	ASSERT(vfsp);
13961544Seschrock 
13971544Seschrock 	/*
13983912Slling 	 * The filesystem that we mount as root is defined in the
13996423Sgw25295 	 * boot property "zfs-bootfs" with a format of
14006423Sgw25295 	 * "poolname/root-dataset-objnum".
14011544Seschrock 	 */
14021544Seschrock 	if (why == ROOT_INIT) {
14031544Seschrock 		if (zfsrootdone++)
14041544Seschrock 			return (EBUSY);
14056423Sgw25295 		/*
14066423Sgw25295 		 * the process of doing a spa_load will require the
14076423Sgw25295 		 * clock to be set before we could (for example) do
14086423Sgw25295 		 * something better by looking at the timestamp on
14096423Sgw25295 		 * an uberblock, so just set it to -1.
14106423Sgw25295 		 */
14116423Sgw25295 		clkset(-1);
14121544Seschrock 
14137147Staylor 		if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
14147147Staylor 			cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
14157147Staylor 			    "bootfs name");
14166423Sgw25295 			return (EINVAL);
14175648Ssetje 		}
14187147Staylor 		zfs_devid = spa_get_bootprop("diskdevid");
14197147Staylor 		error = spa_import_rootpool(rootfs.bo_name, zfs_devid);
14207147Staylor 		if (zfs_devid)
14217147Staylor 			spa_free_bootprop(zfs_devid);
14227147Staylor 		if (error) {
14237147Staylor 			spa_free_bootprop(zfs_bootfs);
14247147Staylor 			cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
14257147Staylor 			    error);
14267147Staylor 			return (error);
14277147Staylor 		}
14287147Staylor 		if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
14297147Staylor 			spa_free_bootprop(zfs_bootfs);
14307147Staylor 			cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
14316423Sgw25295 			    error);
14326423Sgw25295 			return (error);
14336423Sgw25295 		}
14343912Slling 
14357147Staylor 		spa_free_bootprop(zfs_bootfs);
14361544Seschrock 
14371544Seschrock 		if (error = vfs_lock(vfsp))
14381544Seschrock 			return (error);
14391544Seschrock 
14407046Sahrens 		if (error = zfs_domount(vfsp, rootfs.bo_name)) {
14417147Staylor 			cmn_err(CE_NOTE, "zfs_domount: error %d", error);
14421544Seschrock 			goto out;
14436423Sgw25295 		}
14441544Seschrock 
14451544Seschrock 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
14461544Seschrock 		ASSERT(zfsvfs);
14476423Sgw25295 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
14487147Staylor 			cmn_err(CE_NOTE, "zfs_zget: error %d", error);
14491544Seschrock 			goto out;
14506423Sgw25295 		}
14511544Seschrock 
14521544Seschrock 		vp = ZTOV(zp);
14531544Seschrock 		mutex_enter(&vp->v_lock);
14541544Seschrock 		vp->v_flag |= VROOT;
14551544Seschrock 		mutex_exit(&vp->v_lock);
14561544Seschrock 		rootvp = vp;
14571544Seschrock 
14581544Seschrock 		/*
14596570Smarks 		 * Leave rootvp held.  The root file system is never unmounted.
14601544Seschrock 		 */
14611544Seschrock 
14621544Seschrock 		vfs_add((struct vnode *)0, vfsp,
14631544Seschrock 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
14641544Seschrock out:
14651544Seschrock 		vfs_unlock(vfsp);
14666423Sgw25295 		return (error);
14671544Seschrock 	} else if (why == ROOT_REMOUNT) {
14681544Seschrock 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
14691544Seschrock 		vfsp->vfs_flag |= VFS_REMOUNT;
14704596Slling 
14714596Slling 		/* refresh mount options */
14724596Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
14734596Slling 		return (zfs_register_callbacks(vfsp));
14744596Slling 
14751544Seschrock 	} else if (why == ROOT_UNMOUNT) {
14761544Seschrock 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
14771544Seschrock 		(void) zfs_sync(vfsp, 0, 0);
14781544Seschrock 		return (0);
14791544Seschrock 	}
14801544Seschrock 
14811544Seschrock 	/*
14821544Seschrock 	 * if "why" is equal to anything else other than ROOT_INIT,
14831544Seschrock 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
14841544Seschrock 	 */
14851544Seschrock 	return (ENOTSUP);
14861544Seschrock }
14871544Seschrock 
1488789Sahrens /*ARGSUSED*/
1489789Sahrens static int
1490789Sahrens zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
1491789Sahrens {
1492789Sahrens 	char		*osname;
1493789Sahrens 	pathname_t	spn;
1494789Sahrens 	int		error = 0;
1495789Sahrens 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
14963912Slling 	    UIO_SYSSPACE : UIO_USERSPACE;
1497789Sahrens 	int		canwrite;
1498789Sahrens 
1499789Sahrens 	if (mvp->v_type != VDIR)
1500789Sahrens 		return (ENOTDIR);
1501789Sahrens 
1502789Sahrens 	mutex_enter(&mvp->v_lock);
1503789Sahrens 	if ((uap->flags & MS_REMOUNT) == 0 &&
1504789Sahrens 	    (uap->flags & MS_OVERLAY) == 0 &&
1505789Sahrens 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
1506789Sahrens 		mutex_exit(&mvp->v_lock);
1507789Sahrens 		return (EBUSY);
1508789Sahrens 	}
1509789Sahrens 	mutex_exit(&mvp->v_lock);
1510789Sahrens 
1511789Sahrens 	/*
1512789Sahrens 	 * ZFS does not support passing unparsed data in via MS_DATA.
1513789Sahrens 	 * Users should use the MS_OPTIONSTR interface; this means
1514789Sahrens 	 * that all option parsing is already done and the options struct
1515789Sahrens 	 * can be interrogated.
1516789Sahrens 	 */
1517789Sahrens 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
1518789Sahrens 		return (EINVAL);
1519789Sahrens 
1520789Sahrens 	/*
1521789Sahrens 	 * Get the objset name (the "special" mount argument).
1522789Sahrens 	 */
1523789Sahrens 	if (error = pn_get(uap->spec, fromspace, &spn))
1524789Sahrens 		return (error);
1525789Sahrens 
1526789Sahrens 	osname = spn.pn_path;
1527789Sahrens 
15284543Smarks 	/*
15294543Smarks 	 * Check for mount privilege?
15304543Smarks 	 *
15314543Smarks 	 * If we don't have privilege then see if
15324543Smarks 	 * we have local permission to allow it
15334543Smarks 	 */
15344543Smarks 	error = secpolicy_fs_mount(cr, mvp, vfsp);
15354543Smarks 	if (error) {
15364543Smarks 		error = dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr);
15374543Smarks 		if (error == 0) {
15384543Smarks 			vattr_t		vattr;
15394543Smarks 
15404543Smarks 			/*
15414543Smarks 			 * Make sure user is the owner of the mount point
15424543Smarks 			 * or has sufficient privileges.
15434543Smarks 			 */
15444543Smarks 
15454543Smarks 			vattr.va_mask = AT_UID;
15464543Smarks 
15475331Samw 			if (error = VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
15484543Smarks 				goto out;
15494543Smarks 			}
15504543Smarks 
15515489Smarks 			if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 &&
15525489Smarks 			    VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) {
15535489Smarks 				error = EPERM;
15544543Smarks 				goto out;
15554543Smarks 			}
15564543Smarks 
15574543Smarks 			secpolicy_fs_mount_clearopts(cr, vfsp);
15584543Smarks 		} else {
15594543Smarks 			goto out;
15604543Smarks 		}
15614543Smarks 	}
1562789Sahrens 
1563789Sahrens 	/*
1564789Sahrens 	 * Refuse to mount a filesystem if we are in a local zone and the
1565789Sahrens 	 * dataset is not visible.
1566789Sahrens 	 */
1567789Sahrens 	if (!INGLOBALZONE(curproc) &&
1568789Sahrens 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1569789Sahrens 		error = EPERM;
1570789Sahrens 		goto out;
1571789Sahrens 	}
1572789Sahrens 
1573*10972SRic.Aleshire@Sun.COM 	error = zfs_mount_label_policy(vfsp, osname);
1574*10972SRic.Aleshire@Sun.COM 	if (error)
1575*10972SRic.Aleshire@Sun.COM 		goto out;
1576*10972SRic.Aleshire@Sun.COM 
15774596Slling 	/*
15784596Slling 	 * When doing a remount, we simply refresh our temporary properties
15794596Slling 	 * according to those options set in the current VFS options.
15804596Slling 	 */
15814596Slling 	if (uap->flags & MS_REMOUNT) {
15824596Slling 		/* refresh mount options */
15834596Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
15844596Slling 		error = zfs_register_callbacks(vfsp);
15854596Slling 		goto out;
15864596Slling 	}
15874596Slling 
15887046Sahrens 	error = zfs_domount(vfsp, osname);
1589789Sahrens 
15909214Schris.kirby@sun.com 	/*
15919214Schris.kirby@sun.com 	 * Add an extra VFS_HOLD on our parent vfs so that it can't
15929214Schris.kirby@sun.com 	 * disappear due to a forced unmount.
15939214Schris.kirby@sun.com 	 */
15949246Schris.kirby@sun.com 	if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap)
15959214Schris.kirby@sun.com 		VFS_HOLD(mvp->v_vfsp);
15969214Schris.kirby@sun.com 
1597789Sahrens out:
1598789Sahrens 	pn_free(&spn);
1599789Sahrens 	return (error);
1600789Sahrens }
1601789Sahrens 
1602789Sahrens static int
1603789Sahrens zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1604789Sahrens {
1605789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1606789Sahrens 	dev32_t d32;
16072885Sahrens 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
1608789Sahrens 
1609789Sahrens 	ZFS_ENTER(zfsvfs);
1610789Sahrens 
16112885Sahrens 	dmu_objset_space(zfsvfs->z_os,
16122885Sahrens 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
1613789Sahrens 
1614789Sahrens 	/*
1615789Sahrens 	 * The underlying storage pool actually uses multiple block sizes.
1616789Sahrens 	 * We report the fragsize as the smallest block size we support,
1617789Sahrens 	 * and we report our blocksize as the filesystem's maximum blocksize.
1618789Sahrens 	 */
1619789Sahrens 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1620789Sahrens 	statp->f_bsize = zfsvfs->z_max_blksz;
1621789Sahrens 
1622789Sahrens 	/*
1623789Sahrens 	 * The following report "total" blocks of various kinds in the
1624789Sahrens 	 * file system, but reported in terms of f_frsize - the
1625789Sahrens 	 * "fragment" size.
1626789Sahrens 	 */
1627789Sahrens 
16282885Sahrens 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
16292885Sahrens 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
1630789Sahrens 	statp->f_bavail = statp->f_bfree; /* no root reservation */
1631789Sahrens 
1632789Sahrens 	/*
1633789Sahrens 	 * statvfs() should really be called statufs(), because it assumes
1634789Sahrens 	 * static metadata.  ZFS doesn't preallocate files, so the best
1635789Sahrens 	 * we can do is report the max that could possibly fit in f_files,
1636789Sahrens 	 * and that minus the number actually used in f_ffree.
1637789Sahrens 	 * For f_ffree, report the smaller of the number of object available
1638789Sahrens 	 * and the number of blocks (each object will take at least a block).
1639789Sahrens 	 */
16402885Sahrens 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
1641789Sahrens 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
16422885Sahrens 	statp->f_files = statp->f_ffree + usedobjs;
1643789Sahrens 
1644789Sahrens 	(void) cmpldev(&d32, vfsp->vfs_dev);
1645789Sahrens 	statp->f_fsid = d32;
1646789Sahrens 
1647789Sahrens 	/*
1648789Sahrens 	 * We're a zfs filesystem.
1649789Sahrens 	 */
1650789Sahrens 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1651789Sahrens 
16521123Smarks 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1653789Sahrens 
1654789Sahrens 	statp->f_namemax = ZFS_MAXNAMELEN;
1655789Sahrens 
1656789Sahrens 	/*
1657789Sahrens 	 * We have all of 32 characters to stuff a string here.
1658789Sahrens 	 * Is there anything useful we could/should provide?
1659789Sahrens 	 */
1660789Sahrens 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
1661789Sahrens 
1662789Sahrens 	ZFS_EXIT(zfsvfs);
1663789Sahrens 	return (0);
1664789Sahrens }
1665789Sahrens 
1666789Sahrens static int
1667789Sahrens zfs_root(vfs_t *vfsp, vnode_t **vpp)
1668789Sahrens {
1669789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1670789Sahrens 	znode_t *rootzp;
1671789Sahrens 	int error;
1672789Sahrens 
1673789Sahrens 	ZFS_ENTER(zfsvfs);
1674789Sahrens 
1675789Sahrens 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1676789Sahrens 	if (error == 0)
1677789Sahrens 		*vpp = ZTOV(rootzp);
1678789Sahrens 
1679789Sahrens 	ZFS_EXIT(zfsvfs);
1680789Sahrens 	return (error);
1681789Sahrens }
1682789Sahrens 
16835326Sek110237 /*
16845326Sek110237  * Teardown the zfsvfs::z_os.
16855326Sek110237  *
16865326Sek110237  * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
16875326Sek110237  * and 'z_teardown_inactive_lock' held.
16885326Sek110237  */
16895326Sek110237 static int
16905326Sek110237 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
16915326Sek110237 {
16925642Smaybee 	znode_t	*zp;
16935326Sek110237 
16945326Sek110237 	rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
16955326Sek110237 
16965326Sek110237 	if (!unmounting) {
16975326Sek110237 		/*
16985326Sek110237 		 * We purge the parent filesystem's vfsp as the parent
16995326Sek110237 		 * filesystem and all of its snapshots have their vnode's
17005326Sek110237 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
17015326Sek110237 		 * 'z_parent' is self referential for non-snapshots.
17025326Sek110237 		 */
17035326Sek110237 		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
17045326Sek110237 	}
17055326Sek110237 
17065326Sek110237 	/*
17075326Sek110237 	 * Close the zil. NB: Can't close the zil while zfs_inactive
17085326Sek110237 	 * threads are blocked as zil_close can call zfs_inactive.
17095326Sek110237 	 */
17105326Sek110237 	if (zfsvfs->z_log) {
17115326Sek110237 		zil_close(zfsvfs->z_log);
17125326Sek110237 		zfsvfs->z_log = NULL;
17135326Sek110237 	}
17145326Sek110237 
17155326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
17165326Sek110237 
17175326Sek110237 	/*
17185326Sek110237 	 * If we are not unmounting (ie: online recv) and someone already
17195326Sek110237 	 * unmounted this file system while we were doing the switcheroo,
17205326Sek110237 	 * or a reopen of z_os failed then just bail out now.
17215326Sek110237 	 */
17225326Sek110237 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
17235326Sek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
17245326Sek110237 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
17255326Sek110237 		return (EIO);
17265326Sek110237 	}
17275326Sek110237 
17285326Sek110237 	/*
17295326Sek110237 	 * At this point there are no vops active, and any new vops will
17305326Sek110237 	 * fail with EIO since we have z_teardown_lock for writer (only
17315326Sek110237 	 * relavent for forced unmount).
17325326Sek110237 	 *
17335326Sek110237 	 * Release all holds on dbufs.
17345326Sek110237 	 */
17355326Sek110237 	mutex_enter(&zfsvfs->z_znodes_lock);
17365642Smaybee 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
17375642Smaybee 	    zp = list_next(&zfsvfs->z_all_znodes, zp))
17385446Sahrens 		if (zp->z_dbuf) {
17395642Smaybee 			ASSERT(ZTOV(zp)->v_count > 0);
17405642Smaybee 			zfs_znode_dmu_fini(zp);
17415326Sek110237 		}
17425326Sek110237 	mutex_exit(&zfsvfs->z_znodes_lock);
17435326Sek110237 
17445326Sek110237 	/*
17455326Sek110237 	 * If we are unmounting, set the unmounted flag and let new vops
17465326Sek110237 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
17475326Sek110237 	 * other vops will fail with EIO.
17485326Sek110237 	 */
17495326Sek110237 	if (unmounting) {
17505326Sek110237 		zfsvfs->z_unmounted = B_TRUE;
17515326Sek110237 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
17525326Sek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
17535326Sek110237 	}
17545326Sek110237 
17555326Sek110237 	/*
17565326Sek110237 	 * z_os will be NULL if there was an error in attempting to reopen
17575326Sek110237 	 * zfsvfs, so just return as the properties had already been
17585326Sek110237 	 * unregistered and cached data had been evicted before.
17595326Sek110237 	 */
17605326Sek110237 	if (zfsvfs->z_os == NULL)
17615326Sek110237 		return (0);
17625326Sek110237 
17635326Sek110237 	/*
17645326Sek110237 	 * Unregister properties.
17655326Sek110237 	 */
17665326Sek110237 	zfs_unregister_callbacks(zfsvfs);
17675326Sek110237 
17685326Sek110237 	/*
17695326Sek110237 	 * Evict cached data
17705326Sek110237 	 */
17716083Sek110237 	if (dmu_objset_evict_dbufs(zfsvfs->z_os)) {
17725429Smaybee 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
17736083Sek110237 		(void) dmu_objset_evict_dbufs(zfsvfs->z_os);
17745429Smaybee 	}
17755326Sek110237 
17765326Sek110237 	return (0);
17775326Sek110237 }
17785326Sek110237 
1779789Sahrens /*ARGSUSED*/
1780789Sahrens static int
1781789Sahrens zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1782789Sahrens {
1783789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
17845326Sek110237 	objset_t *os;
1785789Sahrens 	int ret;
1786789Sahrens 
17874543Smarks 	ret = secpolicy_fs_unmount(cr, vfsp);
17884543Smarks 	if (ret) {
17894543Smarks 		ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
17904543Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
17914543Smarks 		if (ret)
17924543Smarks 			return (ret);
17934543Smarks 	}
17941484Sek110237 
17954736Sek110237 	/*
17964736Sek110237 	 * We purge the parent filesystem's vfsp as the parent filesystem
17974736Sek110237 	 * and all of its snapshots have their vnode's v_vfsp set to the
17984736Sek110237 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
17994736Sek110237 	 * referential for non-snapshots.
18004736Sek110237 	 */
18014736Sek110237 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
18021484Sek110237 
1803789Sahrens 	/*
1804789Sahrens 	 * Unmount any snapshots mounted under .zfs before unmounting the
1805789Sahrens 	 * dataset itself.
1806789Sahrens 	 */
1807789Sahrens 	if (zfsvfs->z_ctldir != NULL &&
18084543Smarks 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1809789Sahrens 		return (ret);
18104543Smarks 	}
1811789Sahrens 
18124787Sahrens 	if (!(fflag & MS_FORCE)) {
18134480Sgw25295 		/*
18144787Sahrens 		 * Check the number of active vnodes in the file system.
18154787Sahrens 		 * Our count is maintained in the vfs structure, but the
18164787Sahrens 		 * number is off by 1 to indicate a hold on the vfs
18174787Sahrens 		 * structure itself.
18184787Sahrens 		 *
18194787Sahrens 		 * The '.zfs' directory maintains a reference of its
18204787Sahrens 		 * own, and any active references underneath are
18214787Sahrens 		 * reflected in the vnode count.
1822789Sahrens 		 */
18234787Sahrens 		if (zfsvfs->z_ctldir == NULL) {
18244787Sahrens 			if (vfsp->vfs_count > 1)
18254787Sahrens 				return (EBUSY);
18264787Sahrens 		} else {
18274787Sahrens 			if (vfsp->vfs_count > 2 ||
18285326Sek110237 			    zfsvfs->z_ctldir->v_count > 1)
18294787Sahrens 				return (EBUSY);
1830789Sahrens 		}
1831789Sahrens 	}
1832789Sahrens 
1833789Sahrens 	vfsp->vfs_flag |= VFS_UNMOUNTED;
18344787Sahrens 
18355326Sek110237 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
18365326Sek110237 	os = zfsvfs->z_os;
18374787Sahrens 
18384787Sahrens 	/*
18395326Sek110237 	 * z_os will be NULL if there was an error in
18405326Sek110237 	 * attempting to reopen zfsvfs.
18414787Sahrens 	 */
18425326Sek110237 	if (os != NULL) {
18435326Sek110237 		/*
18445326Sek110237 		 * Unset the objset user_ptr.
18455326Sek110237 		 */
184610298SMatthew.Ahrens@Sun.COM 		mutex_enter(&os->os_user_ptr_lock);
18475326Sek110237 		dmu_objset_set_user(os, NULL);
184810298SMatthew.Ahrens@Sun.COM 		mutex_exit(&os->os_user_ptr_lock);
18494787Sahrens 
18505326Sek110237 		/*
18516689Smaybee 		 * Finally release the objset
18525326Sek110237 		 */
185310298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, zfsvfs);
18544787Sahrens 	}
18554787Sahrens 
18564787Sahrens 	/*
18574787Sahrens 	 * We can now safely destroy the '.zfs' directory node.
18584787Sahrens 	 */
18594787Sahrens 	if (zfsvfs->z_ctldir != NULL)
18604787Sahrens 		zfsctl_destroy(zfsvfs);
1861789Sahrens 
1862789Sahrens 	return (0);
1863789Sahrens }
1864789Sahrens 
1865789Sahrens static int
1866789Sahrens zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1867789Sahrens {
1868789Sahrens 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1869789Sahrens 	znode_t		*zp;
1870789Sahrens 	uint64_t	object = 0;
1871789Sahrens 	uint64_t	fid_gen = 0;
1872789Sahrens 	uint64_t	gen_mask;
1873789Sahrens 	uint64_t	zp_gen;
1874789Sahrens 	int 		i, err;
1875789Sahrens 
1876789Sahrens 	*vpp = NULL;
1877789Sahrens 
1878789Sahrens 	ZFS_ENTER(zfsvfs);
1879789Sahrens 
1880789Sahrens 	if (fidp->fid_len == LONG_FID_LEN) {
1881789Sahrens 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1882789Sahrens 		uint64_t	objsetid = 0;
1883789Sahrens 		uint64_t	setgen = 0;
1884789Sahrens 
1885789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1886789Sahrens 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1887789Sahrens 
1888789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1889789Sahrens 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1890789Sahrens 
1891789Sahrens 		ZFS_EXIT(zfsvfs);
1892789Sahrens 
1893789Sahrens 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1894789Sahrens 		if (err)
1895789Sahrens 			return (EINVAL);
1896789Sahrens 		ZFS_ENTER(zfsvfs);
1897789Sahrens 	}
1898789Sahrens 
1899789Sahrens 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1900789Sahrens 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1901789Sahrens 
1902789Sahrens 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1903789Sahrens 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1904789Sahrens 
1905789Sahrens 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1906789Sahrens 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1907789Sahrens 	} else {
1908789Sahrens 		ZFS_EXIT(zfsvfs);
1909789Sahrens 		return (EINVAL);
1910789Sahrens 	}
1911789Sahrens 
1912789Sahrens 	/* A zero fid_gen means we are in the .zfs control directories */
1913789Sahrens 	if (fid_gen == 0 &&
1914789Sahrens 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1915789Sahrens 		*vpp = zfsvfs->z_ctldir;
1916789Sahrens 		ASSERT(*vpp != NULL);
1917789Sahrens 		if (object == ZFSCTL_INO_SNAPDIR) {
1918789Sahrens 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
19195331Samw 			    0, NULL, NULL, NULL, NULL, NULL) == 0);
1920789Sahrens 		} else {
1921789Sahrens 			VN_HOLD(*vpp);
1922789Sahrens 		}
1923789Sahrens 		ZFS_EXIT(zfsvfs);
1924789Sahrens 		return (0);
1925789Sahrens 	}
1926789Sahrens 
1927789Sahrens 	gen_mask = -1ULL >> (64 - 8 * i);
1928789Sahrens 
1929789Sahrens 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1930789Sahrens 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1931789Sahrens 		ZFS_EXIT(zfsvfs);
1932789Sahrens 		return (err);
1933789Sahrens 	}
1934789Sahrens 	zp_gen = zp->z_phys->zp_gen & gen_mask;
1935789Sahrens 	if (zp_gen == 0)
1936789Sahrens 		zp_gen = 1;
19373461Sahrens 	if (zp->z_unlinked || zp_gen != fid_gen) {
1938789Sahrens 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1939789Sahrens 		VN_RELE(ZTOV(zp));
1940789Sahrens 		ZFS_EXIT(zfsvfs);
1941789Sahrens 		return (EINVAL);
1942789Sahrens 	}
1943789Sahrens 
1944789Sahrens 	*vpp = ZTOV(zp);
1945789Sahrens 	ZFS_EXIT(zfsvfs);
1946789Sahrens 	return (0);
1947789Sahrens }
1948789Sahrens 
19495326Sek110237 /*
19505326Sek110237  * Block out VOPs and close zfsvfs_t::z_os
19515326Sek110237  *
19525326Sek110237  * Note, if successful, then we return with the 'z_teardown_lock' and
19535326Sek110237  * 'z_teardown_inactive_lock' write held.
19545326Sek110237  */
19555326Sek110237 int
195610298SMatthew.Ahrens@Sun.COM zfs_suspend_fs(zfsvfs_t *zfsvfs)
19575326Sek110237 {
19585326Sek110237 	int error;
19595326Sek110237 
19605326Sek110237 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
19615326Sek110237 		return (error);
196210298SMatthew.Ahrens@Sun.COM 	dmu_objset_disown(zfsvfs->z_os, zfsvfs);
19635326Sek110237 
19645326Sek110237 	return (0);
19655326Sek110237 }
19665326Sek110237 
19675326Sek110237 /*
19685326Sek110237  * Reopen zfsvfs_t::z_os and release VOPs.
19695326Sek110237  */
19705326Sek110237 int
197110298SMatthew.Ahrens@Sun.COM zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname)
19725326Sek110237 {
19735326Sek110237 	int err;
19745326Sek110237 
19755326Sek110237 	ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
19765326Sek110237 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
19775326Sek110237 
197810298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_own(osname, DMU_OST_ZFS, B_FALSE, zfsvfs,
197910298SMatthew.Ahrens@Sun.COM 	    &zfsvfs->z_os);
19805326Sek110237 	if (err) {
19815326Sek110237 		zfsvfs->z_os = NULL;
19825326Sek110237 	} else {
19835326Sek110237 		znode_t *zp;
19845326Sek110237 
19855326Sek110237 		VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
19865326Sek110237 
19875326Sek110237 		/*
19885326Sek110237 		 * Attempt to re-establish all the active znodes with
19895326Sek110237 		 * their dbufs.  If a zfs_rezget() fails, then we'll let
19905326Sek110237 		 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
19915326Sek110237 		 * when they try to use their znode.
19925326Sek110237 		 */
19935326Sek110237 		mutex_enter(&zfsvfs->z_znodes_lock);
19945326Sek110237 		for (zp = list_head(&zfsvfs->z_all_znodes); zp;
19955326Sek110237 		    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
19965326Sek110237 			(void) zfs_rezget(zp);
19975326Sek110237 		}
19985326Sek110237 		mutex_exit(&zfsvfs->z_znodes_lock);
19995326Sek110237 
20005326Sek110237 	}
20015326Sek110237 
20025326Sek110237 	/* release the VOPs */
20035326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
20045326Sek110237 	rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
20055326Sek110237 
20065326Sek110237 	if (err) {
20075326Sek110237 		/*
20085326Sek110237 		 * Since we couldn't reopen zfsvfs::z_os, force
20095326Sek110237 		 * unmount this file system.
20105326Sek110237 		 */
20115326Sek110237 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
20125326Sek110237 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
20135326Sek110237 	}
20145326Sek110237 	return (err);
20155326Sek110237 }
20165326Sek110237 
2017789Sahrens static void
2018789Sahrens zfs_freevfs(vfs_t *vfsp)
2019789Sahrens {
2020789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
20219214Schris.kirby@sun.com 
20229214Schris.kirby@sun.com 	/*
20239214Schris.kirby@sun.com 	 * If this is a snapshot, we have an extra VFS_HOLD on our parent
20249214Schris.kirby@sun.com 	 * from zfs_mount().  Release it here.
20259214Schris.kirby@sun.com 	 */
20269214Schris.kirby@sun.com 	if (zfsvfs->z_issnap)
20279214Schris.kirby@sun.com 		VFS_RELE(zfsvfs->z_parent->z_vfs);
20289214Schris.kirby@sun.com 
20299396SMatthew.Ahrens@Sun.COM 	zfsvfs_free(zfsvfs);
2030789Sahrens 
2031789Sahrens 	atomic_add_32(&zfs_active_fs_count, -1);
2032789Sahrens }
2033789Sahrens 
2034789Sahrens /*
2035789Sahrens  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
2036789Sahrens  * so we can't safely do any non-idempotent initialization here.
2037789Sahrens  * Leave that to zfs_init() and zfs_fini(), which are called
2038789Sahrens  * from the module's _init() and _fini() entry points.
2039789Sahrens  */
2040789Sahrens /*ARGSUSED*/
2041789Sahrens static int
2042789Sahrens zfs_vfsinit(int fstype, char *name)
2043789Sahrens {
2044789Sahrens 	int error;
2045789Sahrens 
2046789Sahrens 	zfsfstype = fstype;
2047789Sahrens 
2048789Sahrens 	/*
2049789Sahrens 	 * Setup vfsops and vnodeops tables.
2050789Sahrens 	 */
2051789Sahrens 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
2052789Sahrens 	if (error != 0) {
2053789Sahrens 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
2054789Sahrens 	}
2055789Sahrens 
2056789Sahrens 	error = zfs_create_op_tables();
2057789Sahrens 	if (error) {
2058789Sahrens 		zfs_remove_op_tables();
2059789Sahrens 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
2060789Sahrens 		(void) vfs_freevfsops_by_type(zfsfstype);
2061789Sahrens 		return (error);
2062789Sahrens 	}
2063789Sahrens 
2064789Sahrens 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
2065789Sahrens 
2066789Sahrens 	/*
2067849Sbonwick 	 * Unique major number for all zfs mounts.
2068849Sbonwick 	 * If we run out of 32-bit minors, we'll getudev() another major.
2069789Sahrens 	 */
2070849Sbonwick 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
2071849Sbonwick 	zfs_minor = ZFS_MIN_MINOR;
2072789Sahrens 
2073789Sahrens 	return (0);
2074789Sahrens }
2075789Sahrens 
2076789Sahrens void
2077789Sahrens zfs_init(void)
2078789Sahrens {
2079789Sahrens 	/*
2080789Sahrens 	 * Initialize .zfs directory structures
2081789Sahrens 	 */
2082789Sahrens 	zfsctl_init();
2083789Sahrens 
2084789Sahrens 	/*
2085789Sahrens 	 * Initialize znode cache, vnode ops, etc...
2086789Sahrens 	 */
2087789Sahrens 	zfs_znode_init();
20889396SMatthew.Ahrens@Sun.COM 
20899396SMatthew.Ahrens@Sun.COM 	dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
2090789Sahrens }
2091789Sahrens 
2092789Sahrens void
2093789Sahrens zfs_fini(void)
2094789Sahrens {
2095789Sahrens 	zfsctl_fini();
2096789Sahrens 	zfs_znode_fini();
2097789Sahrens }
2098789Sahrens 
2099789Sahrens int
2100789Sahrens zfs_busy(void)
2101789Sahrens {
2102789Sahrens 	return (zfs_active_fs_count != 0);
2103789Sahrens }
2104789Sahrens 
21054577Sahrens int
21069396SMatthew.Ahrens@Sun.COM zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
21074577Sahrens {
21084577Sahrens 	int error;
21099396SMatthew.Ahrens@Sun.COM 	objset_t *os = zfsvfs->z_os;
21104577Sahrens 	dmu_tx_t *tx;
21114577Sahrens 
21124577Sahrens 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
21134577Sahrens 		return (EINVAL);
21144577Sahrens 
21159396SMatthew.Ahrens@Sun.COM 	if (newvers < zfsvfs->z_version)
21169396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
21174577Sahrens 
21184577Sahrens 	tx = dmu_tx_create(os);
21199396SMatthew.Ahrens@Sun.COM 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
21204577Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
21214577Sahrens 	if (error) {
21224577Sahrens 		dmu_tx_abort(tx);
21239396SMatthew.Ahrens@Sun.COM 		return (error);
21244577Sahrens 	}
21259396SMatthew.Ahrens@Sun.COM 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
21269396SMatthew.Ahrens@Sun.COM 	    8, 1, &newvers, tx);
21279396SMatthew.Ahrens@Sun.COM 
21289396SMatthew.Ahrens@Sun.COM 	if (error) {
21299396SMatthew.Ahrens@Sun.COM 		dmu_tx_commit(tx);
21309396SMatthew.Ahrens@Sun.COM 		return (error);
21319396SMatthew.Ahrens@Sun.COM 	}
21324577Sahrens 
21334577Sahrens 	spa_history_internal_log(LOG_DS_UPGRADE,
21344577Sahrens 	    dmu_objset_spa(os), tx, CRED(),
21359396SMatthew.Ahrens@Sun.COM 	    "oldver=%llu newver=%llu dataset = %llu",
21369396SMatthew.Ahrens@Sun.COM 	    zfsvfs->z_version, newvers, dmu_objset_id(os));
21379396SMatthew.Ahrens@Sun.COM 
21384577Sahrens 	dmu_tx_commit(tx);
21394577Sahrens 
21409396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_version = newvers;
21419396SMatthew.Ahrens@Sun.COM 
21429396SMatthew.Ahrens@Sun.COM 	if (zfsvfs->z_version >= ZPL_VERSION_FUID)
21439396SMatthew.Ahrens@Sun.COM 		zfs_set_fuid_feature(zfsvfs);
21449396SMatthew.Ahrens@Sun.COM 
21459396SMatthew.Ahrens@Sun.COM 	return (0);
21464577Sahrens }
21474577Sahrens 
21485498Stimh /*
21495498Stimh  * Read a property stored within the master node.
21505498Stimh  */
21515498Stimh int
21525498Stimh zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
21535498Stimh {
21545498Stimh 	const char *pname;
21557184Stimh 	int error = ENOENT;
21565498Stimh 
21575498Stimh 	/*
21585498Stimh 	 * Look up the file system's value for the property.  For the
21595498Stimh 	 * version property, we look up a slightly different string.
21605498Stimh 	 */
21615498Stimh 	if (prop == ZFS_PROP_VERSION)
21625498Stimh 		pname = ZPL_VERSION_STR;
21635498Stimh 	else
21645498Stimh 		pname = zfs_prop_to_name(prop);
21655498Stimh 
21667184Stimh 	if (os != NULL)
21677184Stimh 		error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
21685498Stimh 
21696404Smaybee 	if (error == ENOENT) {
21705498Stimh 		/* No value set, use the default value */
21715498Stimh 		switch (prop) {
21726404Smaybee 		case ZFS_PROP_VERSION:
21736404Smaybee 			*value = ZPL_VERSION;
21746404Smaybee 			break;
21755498Stimh 		case ZFS_PROP_NORMALIZE:
21765498Stimh 		case ZFS_PROP_UTF8ONLY:
21775498Stimh 			*value = 0;
21785498Stimh 			break;
21795498Stimh 		case ZFS_PROP_CASE:
21805498Stimh 			*value = ZFS_CASE_SENSITIVE;
21815498Stimh 			break;
21825498Stimh 		default:
21836404Smaybee 			return (error);
21845498Stimh 		}
21856404Smaybee 		error = 0;
21865498Stimh 	}
21876404Smaybee 	return (error);
21885498Stimh }
21895498Stimh 
2190789Sahrens static vfsdef_t vfw = {
2191789Sahrens 	VFSDEF_VERSION,
2192789Sahrens 	MNTTYPE_ZFS,
2193789Sahrens 	zfs_vfsinit,
21945331Samw 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS|
21955331Samw 	    VSW_XID,
2196789Sahrens 	&zfs_mntopts
2197789Sahrens };
2198789Sahrens 
2199789Sahrens struct modlfs zfs_modlfs = {
22004577Sahrens 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
2201789Sahrens };
2202