xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 12070:01261b276236)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51484Sek110237  * Common Development and Distribution License (the "License").
61484Sek110237  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
22*12070SMark.Shellenbaum@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens  */
24789Sahrens 
25789Sahrens #include <sys/types.h>
26789Sahrens #include <sys/param.h>
27789Sahrens #include <sys/systm.h>
28789Sahrens #include <sys/sysmacros.h>
29789Sahrens #include <sys/kmem.h>
30789Sahrens #include <sys/pathname.h>
31789Sahrens #include <sys/vnode.h>
32789Sahrens #include <sys/vfs.h>
333898Srsb #include <sys/vfs_opreg.h>
34789Sahrens #include <sys/mntent.h>
35789Sahrens #include <sys/mount.h>
36789Sahrens #include <sys/cmn_err.h>
37789Sahrens #include "fs/fs_subr.h"
38789Sahrens #include <sys/zfs_znode.h>
393461Sahrens #include <sys/zfs_dir.h>
40789Sahrens #include <sys/zil.h>
41789Sahrens #include <sys/fs/zfs.h>
42789Sahrens #include <sys/dmu.h>
43789Sahrens #include <sys/dsl_prop.h>
443912Slling #include <sys/dsl_dataset.h>
454543Smarks #include <sys/dsl_deleg.h>
46789Sahrens #include <sys/spa.h>
47789Sahrens #include <sys/zap.h>
4811935SMark.Shellenbaum@Sun.COM #include <sys/sa.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>
6311935SMark.Shellenbaum@Sun.COM #include <sys/sa.h>
6411935SMark.Shellenbaum@Sun.COM #include "zfs_comutil.h"
65789Sahrens 
66789Sahrens int zfsfstype;
67789Sahrens vfsops_t *zfs_vfsops = NULL;
68849Sbonwick static major_t zfs_major;
69789Sahrens static minor_t zfs_minor;
70789Sahrens static kmutex_t	zfs_dev_mtx;
71789Sahrens 
729234SGeorge.Wilson@Sun.COM extern int sys_shutdown;
739234SGeorge.Wilson@Sun.COM 
74789Sahrens static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
75789Sahrens static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr);
761544Seschrock static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
77789Sahrens static int zfs_root(vfs_t *vfsp, vnode_t **vpp);
78789Sahrens static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp);
79789Sahrens static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp);
80789Sahrens static void zfs_freevfs(vfs_t *vfsp);
81789Sahrens 
82789Sahrens static const fs_operation_def_t zfs_vfsops_template[] = {
833898Srsb 	VFSNAME_MOUNT,		{ .vfs_mount = zfs_mount },
843898Srsb 	VFSNAME_MOUNTROOT,	{ .vfs_mountroot = zfs_mountroot },
853898Srsb 	VFSNAME_UNMOUNT,	{ .vfs_unmount = zfs_umount },
863898Srsb 	VFSNAME_ROOT,		{ .vfs_root = zfs_root },
873898Srsb 	VFSNAME_STATVFS,	{ .vfs_statvfs = zfs_statvfs },
883898Srsb 	VFSNAME_SYNC,		{ .vfs_sync = zfs_sync },
893898Srsb 	VFSNAME_VGET,		{ .vfs_vget = zfs_vget },
903898Srsb 	VFSNAME_FREEVFS,	{ .vfs_freevfs = zfs_freevfs },
913898Srsb 	NULL,			NULL
92789Sahrens };
93789Sahrens 
94789Sahrens static const fs_operation_def_t zfs_vfsops_eio_template[] = {
953898Srsb 	VFSNAME_FREEVFS,	{ .vfs_freevfs =  zfs_freevfs },
963898Srsb 	NULL,			NULL
97789Sahrens };
98789Sahrens 
99789Sahrens /*
100789Sahrens  * We need to keep a count of active fs's.
101789Sahrens  * This is necessary to prevent our module
102789Sahrens  * from being unloaded after a umount -f
103789Sahrens  */
104789Sahrens static uint32_t	zfs_active_fs_count = 0;
105789Sahrens 
106789Sahrens static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
107789Sahrens static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
1083234Sck153898 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
1093234Sck153898 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
110789Sahrens 
1113234Sck153898 /*
1124596Slling  * MO_DEFAULT is not used since the default value is determined
1134596Slling  * by the equivalent property.
1143234Sck153898  */
115789Sahrens static mntopt_t mntopts[] = {
1163234Sck153898 	{ MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL },
1173234Sck153898 	{ MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL },
1184596Slling 	{ MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL },
119789Sahrens 	{ MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
120789Sahrens };
121789Sahrens 
122789Sahrens static mntopts_t zfs_mntopts = {
123789Sahrens 	sizeof (mntopts) / sizeof (mntopt_t),
124789Sahrens 	mntopts
125789Sahrens };
126789Sahrens 
127789Sahrens /*ARGSUSED*/
128789Sahrens int
129789Sahrens zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
130789Sahrens {
131789Sahrens 	/*
132789Sahrens 	 * Data integrity is job one.  We don't want a compromised kernel
133789Sahrens 	 * writing to the storage pool, so we never sync during panic.
134789Sahrens 	 */
135789Sahrens 	if (panicstr)
136789Sahrens 		return (0);
137789Sahrens 
138789Sahrens 	/*
139789Sahrens 	 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
140789Sahrens 	 * to sync metadata, which they would otherwise cache indefinitely.
141789Sahrens 	 * Semantically, the only requirement is that the sync be initiated.
142789Sahrens 	 * The DMU syncs out txgs frequently, so there's nothing to do.
143789Sahrens 	 */
144789Sahrens 	if (flag & SYNC_ATTR)
145789Sahrens 		return (0);
146789Sahrens 
147789Sahrens 	if (vfsp != NULL) {
148789Sahrens 		/*
149789Sahrens 		 * Sync a specific filesystem.
150789Sahrens 		 */
151789Sahrens 		zfsvfs_t *zfsvfs = vfsp->vfs_data;
1529234SGeorge.Wilson@Sun.COM 		dsl_pool_t *dp;
153789Sahrens 
154789Sahrens 		ZFS_ENTER(zfsvfs);
1559234SGeorge.Wilson@Sun.COM 		dp = dmu_objset_pool(zfsvfs->z_os);
1569234SGeorge.Wilson@Sun.COM 
1579234SGeorge.Wilson@Sun.COM 		/*
1589234SGeorge.Wilson@Sun.COM 		 * If the system is shutting down, then skip any
1599234SGeorge.Wilson@Sun.COM 		 * filesystems which may exist on a suspended pool.
1609234SGeorge.Wilson@Sun.COM 		 */
1619234SGeorge.Wilson@Sun.COM 		if (sys_shutdown && spa_suspended(dp->dp_spa)) {
1629234SGeorge.Wilson@Sun.COM 			ZFS_EXIT(zfsvfs);
1639234SGeorge.Wilson@Sun.COM 			return (0);
1649234SGeorge.Wilson@Sun.COM 		}
1659234SGeorge.Wilson@Sun.COM 
166789Sahrens 		if (zfsvfs->z_log != NULL)
1672638Sperrin 			zil_commit(zfsvfs->z_log, UINT64_MAX, 0);
168789Sahrens 		else
1699234SGeorge.Wilson@Sun.COM 			txg_wait_synced(dp, 0);
170789Sahrens 		ZFS_EXIT(zfsvfs);
171789Sahrens 	} else {
172789Sahrens 		/*
173789Sahrens 		 * Sync all ZFS filesystems.  This is what happens when you
174789Sahrens 		 * run sync(1M).  Unlike other filesystems, ZFS honors the
175789Sahrens 		 * request by waiting for all pools to commit all dirty data.
176789Sahrens 		 */
177789Sahrens 		spa_sync_allpools();
178789Sahrens 	}
179789Sahrens 
180789Sahrens 	return (0);
181789Sahrens }
182789Sahrens 
1831544Seschrock static int
1841544Seschrock zfs_create_unique_device(dev_t *dev)
1851544Seschrock {
1861544Seschrock 	major_t new_major;
1871544Seschrock 
1881544Seschrock 	do {
1891544Seschrock 		ASSERT3U(zfs_minor, <=, MAXMIN32);
1901544Seschrock 		minor_t start = zfs_minor;
1911544Seschrock 		do {
1921544Seschrock 			mutex_enter(&zfs_dev_mtx);
1931544Seschrock 			if (zfs_minor >= MAXMIN32) {
1941544Seschrock 				/*
1951544Seschrock 				 * If we're still using the real major
1961544Seschrock 				 * keep out of /dev/zfs and /dev/zvol minor
1971544Seschrock 				 * number space.  If we're using a getudev()'ed
1981544Seschrock 				 * major number, we can use all of its minors.
1991544Seschrock 				 */
2001544Seschrock 				if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
2011544Seschrock 					zfs_minor = ZFS_MIN_MINOR;
2021544Seschrock 				else
2031544Seschrock 					zfs_minor = 0;
2041544Seschrock 			} else {
2051544Seschrock 				zfs_minor++;
2061544Seschrock 			}
2071544Seschrock 			*dev = makedevice(zfs_major, zfs_minor);
2081544Seschrock 			mutex_exit(&zfs_dev_mtx);
2091544Seschrock 		} while (vfs_devismounted(*dev) && zfs_minor != start);
2101544Seschrock 		if (zfs_minor == start) {
2111544Seschrock 			/*
2121544Seschrock 			 * We are using all ~262,000 minor numbers for the
2131544Seschrock 			 * current major number.  Create a new major number.
2141544Seschrock 			 */
2151544Seschrock 			if ((new_major = getudev()) == (major_t)-1) {
2161544Seschrock 				cmn_err(CE_WARN,
2171544Seschrock 				    "zfs_mount: Can't get unique major "
2181544Seschrock 				    "device number.");
2191544Seschrock 				return (-1);
2201544Seschrock 			}
2211544Seschrock 			mutex_enter(&zfs_dev_mtx);
2221544Seschrock 			zfs_major = new_major;
2231544Seschrock 			zfs_minor = 0;
2241544Seschrock 
2251544Seschrock 			mutex_exit(&zfs_dev_mtx);
2261544Seschrock 		} else {
2271544Seschrock 			break;
2281544Seschrock 		}
2291544Seschrock 		/* CONSTANTCONDITION */
2301544Seschrock 	} while (1);
2311544Seschrock 
2321544Seschrock 	return (0);
2331544Seschrock }
2341544Seschrock 
235789Sahrens static void
236789Sahrens atime_changed_cb(void *arg, uint64_t newval)
237789Sahrens {
238789Sahrens 	zfsvfs_t *zfsvfs = arg;
239789Sahrens 
240789Sahrens 	if (newval == TRUE) {
241789Sahrens 		zfsvfs->z_atime = TRUE;
242789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
243789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
244789Sahrens 	} else {
245789Sahrens 		zfsvfs->z_atime = FALSE;
246789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
247789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
248789Sahrens 	}
249789Sahrens }
250789Sahrens 
251789Sahrens static void
2523234Sck153898 xattr_changed_cb(void *arg, uint64_t newval)
2533234Sck153898 {
2543234Sck153898 	zfsvfs_t *zfsvfs = arg;
2553234Sck153898 
2563234Sck153898 	if (newval == TRUE) {
2573234Sck153898 		/* XXX locking on vfs_flag? */
2583234Sck153898 		zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
2593234Sck153898 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
2603234Sck153898 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
2613234Sck153898 	} else {
2623234Sck153898 		/* XXX locking on vfs_flag? */
2633234Sck153898 		zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
2643234Sck153898 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
2653234Sck153898 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
2663234Sck153898 	}
2673234Sck153898 }
2683234Sck153898 
2693234Sck153898 static void
270789Sahrens blksz_changed_cb(void *arg, uint64_t newval)
271789Sahrens {
272789Sahrens 	zfsvfs_t *zfsvfs = arg;
273789Sahrens 
274789Sahrens 	if (newval < SPA_MINBLOCKSIZE ||
275789Sahrens 	    newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
276789Sahrens 		newval = SPA_MAXBLOCKSIZE;
277789Sahrens 
278789Sahrens 	zfsvfs->z_max_blksz = newval;
279789Sahrens 	zfsvfs->z_vfs->vfs_bsize = newval;
280789Sahrens }
281789Sahrens 
282789Sahrens static void
283789Sahrens readonly_changed_cb(void *arg, uint64_t newval)
284789Sahrens {
285789Sahrens 	zfsvfs_t *zfsvfs = arg;
286789Sahrens 
287789Sahrens 	if (newval) {
288789Sahrens 		/* XXX locking on vfs_flag? */
289789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
290789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
291789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
292789Sahrens 	} else {
293789Sahrens 		/* XXX locking on vfs_flag? */
294789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
295789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
296789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
297789Sahrens 	}
298789Sahrens }
299789Sahrens 
300789Sahrens static void
301789Sahrens devices_changed_cb(void *arg, uint64_t newval)
302789Sahrens {
303789Sahrens 	zfsvfs_t *zfsvfs = arg;
304789Sahrens 
305789Sahrens 	if (newval == FALSE) {
306789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
307789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
308789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
309789Sahrens 	} else {
310789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
311789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
312789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
313789Sahrens 	}
314789Sahrens }
315789Sahrens 
316789Sahrens static void
317789Sahrens setuid_changed_cb(void *arg, uint64_t newval)
318789Sahrens {
319789Sahrens 	zfsvfs_t *zfsvfs = arg;
320789Sahrens 
321789Sahrens 	if (newval == FALSE) {
322789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
323789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
324789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
325789Sahrens 	} else {
326789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
327789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
328789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
329789Sahrens 	}
330789Sahrens }
331789Sahrens 
332789Sahrens static void
333789Sahrens exec_changed_cb(void *arg, uint64_t newval)
334789Sahrens {
335789Sahrens 	zfsvfs_t *zfsvfs = arg;
336789Sahrens 
337789Sahrens 	if (newval == FALSE) {
338789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
339789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
340789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
341789Sahrens 	} else {
342789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
343789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
344789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
345789Sahrens 	}
346789Sahrens }
347789Sahrens 
3485331Samw /*
3495331Samw  * The nbmand mount option can be changed at mount time.
3505331Samw  * We can't allow it to be toggled on live file systems or incorrect
3515331Samw  * behavior may be seen from cifs clients
3525331Samw  *
3535331Samw  * This property isn't registered via dsl_prop_register(), but this callback
3545331Samw  * will be called when a file system is first mounted
3555331Samw  */
3565331Samw static void
3575331Samw nbmand_changed_cb(void *arg, uint64_t newval)
3585331Samw {
3595331Samw 	zfsvfs_t *zfsvfs = arg;
3605331Samw 	if (newval == FALSE) {
3615331Samw 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
3625331Samw 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
3635331Samw 	} else {
3645331Samw 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
3655331Samw 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
3665331Samw 	}
3675331Samw }
3685331Samw 
369789Sahrens static void
370789Sahrens snapdir_changed_cb(void *arg, uint64_t newval)
371789Sahrens {
372789Sahrens 	zfsvfs_t *zfsvfs = arg;
373789Sahrens 
374789Sahrens 	zfsvfs->z_show_ctldir = newval;
375789Sahrens }
376789Sahrens 
377789Sahrens static void
3785331Samw vscan_changed_cb(void *arg, uint64_t newval)
3795331Samw {
3805331Samw 	zfsvfs_t *zfsvfs = arg;
3815331Samw 
3825331Samw 	zfsvfs->z_vscan = newval;
3835331Samw }
3845331Samw 
3855331Samw static void
386789Sahrens acl_mode_changed_cb(void *arg, uint64_t newval)
387789Sahrens {
388789Sahrens 	zfsvfs_t *zfsvfs = arg;
389789Sahrens 
390789Sahrens 	zfsvfs->z_acl_mode = newval;
391789Sahrens }
392789Sahrens 
393789Sahrens static void
394789Sahrens acl_inherit_changed_cb(void *arg, uint64_t newval)
395789Sahrens {
396789Sahrens 	zfsvfs_t *zfsvfs = arg;
397789Sahrens 
398789Sahrens 	zfsvfs->z_acl_inherit = newval;
399789Sahrens }
400789Sahrens 
4011544Seschrock static int
4021544Seschrock zfs_register_callbacks(vfs_t *vfsp)
4031544Seschrock {
4041544Seschrock 	struct dsl_dataset *ds = NULL;
4051544Seschrock 	objset_t *os = NULL;
4061544Seschrock 	zfsvfs_t *zfsvfs = NULL;
4075331Samw 	uint64_t nbmand;
4085331Samw 	int readonly, do_readonly = B_FALSE;
4095331Samw 	int setuid, do_setuid = B_FALSE;
4105331Samw 	int exec, do_exec = B_FALSE;
4115331Samw 	int devices, do_devices = B_FALSE;
4125331Samw 	int xattr, do_xattr = B_FALSE;
4135331Samw 	int atime, do_atime = B_FALSE;
4141544Seschrock 	int error = 0;
4151544Seschrock 
4161544Seschrock 	ASSERT(vfsp);
4171544Seschrock 	zfsvfs = vfsp->vfs_data;
4181544Seschrock 	ASSERT(zfsvfs);
4191544Seschrock 	os = zfsvfs->z_os;
4201544Seschrock 
4211544Seschrock 	/*
4221544Seschrock 	 * The act of registering our callbacks will destroy any mount
4231544Seschrock 	 * options we may have.  In order to enable temporary overrides
4243234Sck153898 	 * of mount options, we stash away the current values and
4251544Seschrock 	 * restore them after we register the callbacks.
4261544Seschrock 	 */
4271544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
4281544Seschrock 		readonly = B_TRUE;
4291544Seschrock 		do_readonly = B_TRUE;
4301544Seschrock 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
4311544Seschrock 		readonly = B_FALSE;
4321544Seschrock 		do_readonly = B_TRUE;
4331544Seschrock 	}
4341544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
4351544Seschrock 		devices = B_FALSE;
4361544Seschrock 		setuid = B_FALSE;
4371544Seschrock 		do_devices = B_TRUE;
4381544Seschrock 		do_setuid = B_TRUE;
4391544Seschrock 	} else {
4401544Seschrock 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
4411544Seschrock 			devices = B_FALSE;
4421544Seschrock 			do_devices = B_TRUE;
4433912Slling 		} else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
4441544Seschrock 			devices = B_TRUE;
4451544Seschrock 			do_devices = B_TRUE;
4461544Seschrock 		}
4471544Seschrock 
4481544Seschrock 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
4491544Seschrock 			setuid = B_FALSE;
4501544Seschrock 			do_setuid = B_TRUE;
4511544Seschrock 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
4521544Seschrock 			setuid = B_TRUE;
4531544Seschrock 			do_setuid = B_TRUE;
4541544Seschrock 		}
4551544Seschrock 	}
4561544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
4571544Seschrock 		exec = B_FALSE;
4581544Seschrock 		do_exec = B_TRUE;
4591544Seschrock 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
4601544Seschrock 		exec = B_TRUE;
4611544Seschrock 		do_exec = B_TRUE;
4621544Seschrock 	}
4633234Sck153898 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
4643234Sck153898 		xattr = B_FALSE;
4653234Sck153898 		do_xattr = B_TRUE;
4663234Sck153898 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
4673234Sck153898 		xattr = B_TRUE;
4683234Sck153898 		do_xattr = B_TRUE;
4693234Sck153898 	}
4704596Slling 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
4714596Slling 		atime = B_FALSE;
4724596Slling 		do_atime = B_TRUE;
4734596Slling 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
4744596Slling 		atime = B_TRUE;
4754596Slling 		do_atime = B_TRUE;
4764596Slling 	}
4771544Seschrock 
4781544Seschrock 	/*
4795331Samw 	 * nbmand is a special property.  It can only be changed at
4805331Samw 	 * mount time.
4815331Samw 	 *
4825331Samw 	 * This is weird, but it is documented to only be changeable
4835331Samw 	 * at mount time.
4845331Samw 	 */
4855331Samw 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
4865331Samw 		nbmand = B_FALSE;
4875331Samw 	} else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
4885331Samw 		nbmand = B_TRUE;
4895331Samw 	} else {
4905331Samw 		char osname[MAXNAMELEN];
4915331Samw 
4925331Samw 		dmu_objset_name(os, osname);
4935331Samw 		if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
4947265Sahrens 		    NULL)) {
4957265Sahrens 			return (error);
4967265Sahrens 		}
4975331Samw 	}
4985331Samw 
4995331Samw 	/*
5001544Seschrock 	 * Register property callbacks.
5011544Seschrock 	 *
5021544Seschrock 	 * It would probably be fine to just check for i/o error from
5031544Seschrock 	 * the first prop_register(), but I guess I like to go
5041544Seschrock 	 * overboard...
5051544Seschrock 	 */
5061544Seschrock 	ds = dmu_objset_ds(os);
5071544Seschrock 	error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
5081544Seschrock 	error = error ? error : dsl_prop_register(ds,
5093234Sck153898 	    "xattr", xattr_changed_cb, zfsvfs);
5103234Sck153898 	error = error ? error : dsl_prop_register(ds,
5111544Seschrock 	    "recordsize", blksz_changed_cb, zfsvfs);
5121544Seschrock 	error = error ? error : dsl_prop_register(ds,
5131544Seschrock 	    "readonly", readonly_changed_cb, zfsvfs);
5141544Seschrock 	error = error ? error : dsl_prop_register(ds,
5151544Seschrock 	    "devices", devices_changed_cb, zfsvfs);
5161544Seschrock 	error = error ? error : dsl_prop_register(ds,
5171544Seschrock 	    "setuid", setuid_changed_cb, zfsvfs);
5181544Seschrock 	error = error ? error : dsl_prop_register(ds,
5191544Seschrock 	    "exec", exec_changed_cb, zfsvfs);
5201544Seschrock 	error = error ? error : dsl_prop_register(ds,
5211544Seschrock 	    "snapdir", snapdir_changed_cb, zfsvfs);
5221544Seschrock 	error = error ? error : dsl_prop_register(ds,
5231544Seschrock 	    "aclmode", acl_mode_changed_cb, zfsvfs);
5241544Seschrock 	error = error ? error : dsl_prop_register(ds,
5251544Seschrock 	    "aclinherit", acl_inherit_changed_cb, zfsvfs);
5265331Samw 	error = error ? error : dsl_prop_register(ds,
5275331Samw 	    "vscan", vscan_changed_cb, zfsvfs);
5281544Seschrock 	if (error)
5291544Seschrock 		goto unregister;
5301544Seschrock 
5311544Seschrock 	/*
5321544Seschrock 	 * Invoke our callbacks to restore temporary mount options.
5331544Seschrock 	 */
5341544Seschrock 	if (do_readonly)
5351544Seschrock 		readonly_changed_cb(zfsvfs, readonly);
5361544Seschrock 	if (do_setuid)
5371544Seschrock 		setuid_changed_cb(zfsvfs, setuid);
5381544Seschrock 	if (do_exec)
5391544Seschrock 		exec_changed_cb(zfsvfs, exec);
5401544Seschrock 	if (do_devices)
5411544Seschrock 		devices_changed_cb(zfsvfs, devices);
5423234Sck153898 	if (do_xattr)
5433234Sck153898 		xattr_changed_cb(zfsvfs, xattr);
5444596Slling 	if (do_atime)
5454596Slling 		atime_changed_cb(zfsvfs, atime);
5461544Seschrock 
5475331Samw 	nbmand_changed_cb(zfsvfs, nbmand);
5485331Samw 
5491544Seschrock 	return (0);
5501544Seschrock 
5511544Seschrock unregister:
5521544Seschrock 	/*
5531544Seschrock 	 * We may attempt to unregister some callbacks that are not
5541544Seschrock 	 * registered, but this is OK; it will simply return ENOMSG,
5551544Seschrock 	 * which we will ignore.
5561544Seschrock 	 */
5571544Seschrock 	(void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
5583234Sck153898 	(void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
5591544Seschrock 	(void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
5601544Seschrock 	(void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
5611544Seschrock 	(void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs);
5621544Seschrock 	(void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
5631544Seschrock 	(void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
5641544Seschrock 	(void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
5651544Seschrock 	(void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
5661544Seschrock 	(void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
5671544Seschrock 	    zfsvfs);
5685331Samw 	(void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
5691544Seschrock 	return (error);
5701544Seschrock 
5711544Seschrock }
5721544Seschrock 
5739396SMatthew.Ahrens@Sun.COM static void
5749396SMatthew.Ahrens@Sun.COM uidacct(objset_t *os, boolean_t isgroup, uint64_t fuid,
5759396SMatthew.Ahrens@Sun.COM     int64_t delta, dmu_tx_t *tx)
5769396SMatthew.Ahrens@Sun.COM {
5779396SMatthew.Ahrens@Sun.COM 	uint64_t used = 0;
5789396SMatthew.Ahrens@Sun.COM 	char buf[32];
5799396SMatthew.Ahrens@Sun.COM 	int err;
5809396SMatthew.Ahrens@Sun.COM 	uint64_t obj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
5819396SMatthew.Ahrens@Sun.COM 
5829396SMatthew.Ahrens@Sun.COM 	if (delta == 0)
5839396SMatthew.Ahrens@Sun.COM 		return;
5849396SMatthew.Ahrens@Sun.COM 
5859396SMatthew.Ahrens@Sun.COM 	(void) snprintf(buf, sizeof (buf), "%llx", (longlong_t)fuid);
5869396SMatthew.Ahrens@Sun.COM 	err = zap_lookup(os, obj, buf, 8, 1, &used);
58711935SMark.Shellenbaum@Sun.COM 
5889396SMatthew.Ahrens@Sun.COM 	ASSERT(err == 0 || err == ENOENT);
5899396SMatthew.Ahrens@Sun.COM 	/* no underflow/overflow */
5909396SMatthew.Ahrens@Sun.COM 	ASSERT(delta > 0 || used >= -delta);
5919396SMatthew.Ahrens@Sun.COM 	ASSERT(delta < 0 || used + delta > used);
5929396SMatthew.Ahrens@Sun.COM 	used += delta;
5939396SMatthew.Ahrens@Sun.COM 	if (used == 0)
5949396SMatthew.Ahrens@Sun.COM 		err = zap_remove(os, obj, buf, tx);
5959396SMatthew.Ahrens@Sun.COM 	else
5969396SMatthew.Ahrens@Sun.COM 		err = zap_update(os, obj, buf, 8, 1, &used, tx);
5979396SMatthew.Ahrens@Sun.COM 	ASSERT(err == 0);
59811935SMark.Shellenbaum@Sun.COM 
5999396SMatthew.Ahrens@Sun.COM }
6009396SMatthew.Ahrens@Sun.COM 
60110407SMatthew.Ahrens@Sun.COM static int
60211935SMark.Shellenbaum@Sun.COM zfs_space_delta_cb(dmu_object_type_t bonustype, void *data,
60310407SMatthew.Ahrens@Sun.COM     uint64_t *userp, uint64_t *groupp)
6049396SMatthew.Ahrens@Sun.COM {
60511935SMark.Shellenbaum@Sun.COM 	znode_phys_t *znp = data;
60611935SMark.Shellenbaum@Sun.COM 	int error = 0;
6079396SMatthew.Ahrens@Sun.COM 
60811935SMark.Shellenbaum@Sun.COM 	if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
60910407SMatthew.Ahrens@Sun.COM 		return (ENOENT);
6109396SMatthew.Ahrens@Sun.COM 
61111935SMark.Shellenbaum@Sun.COM 	if (bonustype == DMU_OT_ZNODE) {
61211935SMark.Shellenbaum@Sun.COM 		*userp = znp->zp_uid;
61311935SMark.Shellenbaum@Sun.COM 		*groupp = znp->zp_gid;
61411935SMark.Shellenbaum@Sun.COM 	} else {
61511935SMark.Shellenbaum@Sun.COM 		int hdrsize;
61611935SMark.Shellenbaum@Sun.COM 
61711935SMark.Shellenbaum@Sun.COM 		ASSERT(bonustype == DMU_OT_SA);
61811935SMark.Shellenbaum@Sun.COM 		hdrsize = sa_hdrsize(data);
61911935SMark.Shellenbaum@Sun.COM 
62011935SMark.Shellenbaum@Sun.COM 		if (hdrsize != 0) {
62111935SMark.Shellenbaum@Sun.COM 			*userp = *((uint64_t *)((uintptr_t)data + hdrsize +
62211935SMark.Shellenbaum@Sun.COM 			    SA_UID_OFFSET));
62311935SMark.Shellenbaum@Sun.COM 			*groupp = *((uint64_t *)((uintptr_t)data + hdrsize +
62411935SMark.Shellenbaum@Sun.COM 			    SA_GID_OFFSET));
62511935SMark.Shellenbaum@Sun.COM 		} else {
62611935SMark.Shellenbaum@Sun.COM 			error = ENOENT;
62711935SMark.Shellenbaum@Sun.COM 		}
62811935SMark.Shellenbaum@Sun.COM 	}
62911935SMark.Shellenbaum@Sun.COM 	return (error);
6309396SMatthew.Ahrens@Sun.COM }
6319396SMatthew.Ahrens@Sun.COM 
6329396SMatthew.Ahrens@Sun.COM static void
6339396SMatthew.Ahrens@Sun.COM fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr,
6349396SMatthew.Ahrens@Sun.COM     char *domainbuf, int buflen, uid_t *ridp)
6359396SMatthew.Ahrens@Sun.COM {
6369396SMatthew.Ahrens@Sun.COM 	uint64_t fuid;
6379396SMatthew.Ahrens@Sun.COM 	const char *domain;
6389396SMatthew.Ahrens@Sun.COM 
6399396SMatthew.Ahrens@Sun.COM 	fuid = strtonum(fuidstr, NULL);
6409396SMatthew.Ahrens@Sun.COM 
6419396SMatthew.Ahrens@Sun.COM 	domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid));
6429396SMatthew.Ahrens@Sun.COM 	if (domain)
6439396SMatthew.Ahrens@Sun.COM 		(void) strlcpy(domainbuf, domain, buflen);
6449396SMatthew.Ahrens@Sun.COM 	else
6459396SMatthew.Ahrens@Sun.COM 		domainbuf[0] = '\0';
6469396SMatthew.Ahrens@Sun.COM 	*ridp = FUID_RID(fuid);
6479396SMatthew.Ahrens@Sun.COM }
6489396SMatthew.Ahrens@Sun.COM 
6499396SMatthew.Ahrens@Sun.COM static uint64_t
6509396SMatthew.Ahrens@Sun.COM zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type)
6519396SMatthew.Ahrens@Sun.COM {
6529396SMatthew.Ahrens@Sun.COM 	switch (type) {
6539396SMatthew.Ahrens@Sun.COM 	case ZFS_PROP_USERUSED:
6549396SMatthew.Ahrens@Sun.COM 		return (DMU_USERUSED_OBJECT);
6559396SMatthew.Ahrens@Sun.COM 	case ZFS_PROP_GROUPUSED:
6569396SMatthew.Ahrens@Sun.COM 		return (DMU_GROUPUSED_OBJECT);
6579396SMatthew.Ahrens@Sun.COM 	case ZFS_PROP_USERQUOTA:
6589396SMatthew.Ahrens@Sun.COM 		return (zfsvfs->z_userquota_obj);
6599396SMatthew.Ahrens@Sun.COM 	case ZFS_PROP_GROUPQUOTA:
6609396SMatthew.Ahrens@Sun.COM 		return (zfsvfs->z_groupquota_obj);
6619396SMatthew.Ahrens@Sun.COM 	}
6629396SMatthew.Ahrens@Sun.COM 	return (0);
6639396SMatthew.Ahrens@Sun.COM }
6649396SMatthew.Ahrens@Sun.COM 
6659396SMatthew.Ahrens@Sun.COM int
6669396SMatthew.Ahrens@Sun.COM zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
6679396SMatthew.Ahrens@Sun.COM     uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
6689396SMatthew.Ahrens@Sun.COM {
6699396SMatthew.Ahrens@Sun.COM 	int error;
6709396SMatthew.Ahrens@Sun.COM 	zap_cursor_t zc;
6719396SMatthew.Ahrens@Sun.COM 	zap_attribute_t za;
6729396SMatthew.Ahrens@Sun.COM 	zfs_useracct_t *buf = vbuf;
6739396SMatthew.Ahrens@Sun.COM 	uint64_t obj;
6749396SMatthew.Ahrens@Sun.COM 
6759396SMatthew.Ahrens@Sun.COM 	if (!dmu_objset_userspace_present(zfsvfs->z_os))
6769396SMatthew.Ahrens@Sun.COM 		return (ENOTSUP);
6779396SMatthew.Ahrens@Sun.COM 
6789396SMatthew.Ahrens@Sun.COM 	obj = zfs_userquota_prop_to_obj(zfsvfs, type);
6799396SMatthew.Ahrens@Sun.COM 	if (obj == 0) {
6809396SMatthew.Ahrens@Sun.COM 		*bufsizep = 0;
6819396SMatthew.Ahrens@Sun.COM 		return (0);
6829396SMatthew.Ahrens@Sun.COM 	}
6839396SMatthew.Ahrens@Sun.COM 
6849396SMatthew.Ahrens@Sun.COM 	for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
6859396SMatthew.Ahrens@Sun.COM 	    (error = zap_cursor_retrieve(&zc, &za)) == 0;
6869396SMatthew.Ahrens@Sun.COM 	    zap_cursor_advance(&zc)) {
6879396SMatthew.Ahrens@Sun.COM 		if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
6889396SMatthew.Ahrens@Sun.COM 		    *bufsizep)
6899396SMatthew.Ahrens@Sun.COM 			break;
6909396SMatthew.Ahrens@Sun.COM 
6919396SMatthew.Ahrens@Sun.COM 		fuidstr_to_sid(zfsvfs, za.za_name,
6929396SMatthew.Ahrens@Sun.COM 		    buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
6939396SMatthew.Ahrens@Sun.COM 
6949396SMatthew.Ahrens@Sun.COM 		buf->zu_space = za.za_first_integer;
6959396SMatthew.Ahrens@Sun.COM 		buf++;
6969396SMatthew.Ahrens@Sun.COM 	}
6979396SMatthew.Ahrens@Sun.COM 	if (error == ENOENT)
6989396SMatthew.Ahrens@Sun.COM 		error = 0;
6999396SMatthew.Ahrens@Sun.COM 
7009396SMatthew.Ahrens@Sun.COM 	ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
7019396SMatthew.Ahrens@Sun.COM 	*bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
7029396SMatthew.Ahrens@Sun.COM 	*cookiep = zap_cursor_serialize(&zc);
7039396SMatthew.Ahrens@Sun.COM 	zap_cursor_fini(&zc);
7049396SMatthew.Ahrens@Sun.COM 	return (error);
7059396SMatthew.Ahrens@Sun.COM }
7069396SMatthew.Ahrens@Sun.COM 
7079396SMatthew.Ahrens@Sun.COM /*
7089396SMatthew.Ahrens@Sun.COM  * buf must be big enough (eg, 32 bytes)
7099396SMatthew.Ahrens@Sun.COM  */
7109396SMatthew.Ahrens@Sun.COM static int
7119396SMatthew.Ahrens@Sun.COM id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
7129396SMatthew.Ahrens@Sun.COM     char *buf, boolean_t addok)
7139396SMatthew.Ahrens@Sun.COM {
7149396SMatthew.Ahrens@Sun.COM 	uint64_t fuid;
7159396SMatthew.Ahrens@Sun.COM 	int domainid = 0;
7169396SMatthew.Ahrens@Sun.COM 
7179396SMatthew.Ahrens@Sun.COM 	if (domain && domain[0]) {
7189396SMatthew.Ahrens@Sun.COM 		domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
7199396SMatthew.Ahrens@Sun.COM 		if (domainid == -1)
7209396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
7219396SMatthew.Ahrens@Sun.COM 	}
7229396SMatthew.Ahrens@Sun.COM 	fuid = FUID_ENCODE(domainid, rid);
7239396SMatthew.Ahrens@Sun.COM 	(void) sprintf(buf, "%llx", (longlong_t)fuid);
7249396SMatthew.Ahrens@Sun.COM 	return (0);
7259396SMatthew.Ahrens@Sun.COM }
7269396SMatthew.Ahrens@Sun.COM 
7279396SMatthew.Ahrens@Sun.COM int
7289396SMatthew.Ahrens@Sun.COM zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
7299396SMatthew.Ahrens@Sun.COM     const char *domain, uint64_t rid, uint64_t *valp)
7309396SMatthew.Ahrens@Sun.COM {
7319396SMatthew.Ahrens@Sun.COM 	char buf[32];
7329396SMatthew.Ahrens@Sun.COM 	int err;
7339396SMatthew.Ahrens@Sun.COM 	uint64_t obj;
7349396SMatthew.Ahrens@Sun.COM 
7359396SMatthew.Ahrens@Sun.COM 	*valp = 0;
7369396SMatthew.Ahrens@Sun.COM 
7379396SMatthew.Ahrens@Sun.COM 	if (!dmu_objset_userspace_present(zfsvfs->z_os))
7389396SMatthew.Ahrens@Sun.COM 		return (ENOTSUP);
7399396SMatthew.Ahrens@Sun.COM 
7409396SMatthew.Ahrens@Sun.COM 	obj = zfs_userquota_prop_to_obj(zfsvfs, type);
7419396SMatthew.Ahrens@Sun.COM 	if (obj == 0)
7429396SMatthew.Ahrens@Sun.COM 		return (0);
7439396SMatthew.Ahrens@Sun.COM 
7449396SMatthew.Ahrens@Sun.COM 	err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_FALSE);
7459396SMatthew.Ahrens@Sun.COM 	if (err)
7469396SMatthew.Ahrens@Sun.COM 		return (err);
7479396SMatthew.Ahrens@Sun.COM 
7489396SMatthew.Ahrens@Sun.COM 	err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
7499396SMatthew.Ahrens@Sun.COM 	if (err == ENOENT)
7509396SMatthew.Ahrens@Sun.COM 		err = 0;
7519396SMatthew.Ahrens@Sun.COM 	return (err);
7529396SMatthew.Ahrens@Sun.COM }
7539396SMatthew.Ahrens@Sun.COM 
7549396SMatthew.Ahrens@Sun.COM int
7559396SMatthew.Ahrens@Sun.COM zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
7569396SMatthew.Ahrens@Sun.COM     const char *domain, uint64_t rid, uint64_t quota)
7579396SMatthew.Ahrens@Sun.COM {
7589396SMatthew.Ahrens@Sun.COM 	char buf[32];
7599396SMatthew.Ahrens@Sun.COM 	int err;
7609396SMatthew.Ahrens@Sun.COM 	dmu_tx_t *tx;
7619396SMatthew.Ahrens@Sun.COM 	uint64_t *objp;
7629396SMatthew.Ahrens@Sun.COM 	boolean_t fuid_dirtied;
7639396SMatthew.Ahrens@Sun.COM 
7649396SMatthew.Ahrens@Sun.COM 	if (type != ZFS_PROP_USERQUOTA && type != ZFS_PROP_GROUPQUOTA)
7659396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
7669396SMatthew.Ahrens@Sun.COM 
7679396SMatthew.Ahrens@Sun.COM 	if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
7689396SMatthew.Ahrens@Sun.COM 		return (ENOTSUP);
7699396SMatthew.Ahrens@Sun.COM 
7709396SMatthew.Ahrens@Sun.COM 	objp = (type == ZFS_PROP_USERQUOTA) ? &zfsvfs->z_userquota_obj :
7719396SMatthew.Ahrens@Sun.COM 	    &zfsvfs->z_groupquota_obj;
7729396SMatthew.Ahrens@Sun.COM 
7739396SMatthew.Ahrens@Sun.COM 	err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE);
7749396SMatthew.Ahrens@Sun.COM 	if (err)
7759396SMatthew.Ahrens@Sun.COM 		return (err);
7769396SMatthew.Ahrens@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
7779396SMatthew.Ahrens@Sun.COM 
7789396SMatthew.Ahrens@Sun.COM 	tx = dmu_tx_create(zfsvfs->z_os);
7799396SMatthew.Ahrens@Sun.COM 	dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
7809396SMatthew.Ahrens@Sun.COM 	if (*objp == 0) {
7819396SMatthew.Ahrens@Sun.COM 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
7829396SMatthew.Ahrens@Sun.COM 		    zfs_userquota_prop_prefixes[type]);
7839396SMatthew.Ahrens@Sun.COM 	}
7849396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
7859396SMatthew.Ahrens@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
7869396SMatthew.Ahrens@Sun.COM 	err = dmu_tx_assign(tx, TXG_WAIT);
7879396SMatthew.Ahrens@Sun.COM 	if (err) {
7889396SMatthew.Ahrens@Sun.COM 		dmu_tx_abort(tx);
7899396SMatthew.Ahrens@Sun.COM 		return (err);
7909396SMatthew.Ahrens@Sun.COM 	}
7919396SMatthew.Ahrens@Sun.COM 
7929396SMatthew.Ahrens@Sun.COM 	mutex_enter(&zfsvfs->z_lock);
7939396SMatthew.Ahrens@Sun.COM 	if (*objp == 0) {
7949396SMatthew.Ahrens@Sun.COM 		*objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
7959396SMatthew.Ahrens@Sun.COM 		    DMU_OT_NONE, 0, tx);
7969396SMatthew.Ahrens@Sun.COM 		VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
7979396SMatthew.Ahrens@Sun.COM 		    zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
7989396SMatthew.Ahrens@Sun.COM 	}
7999396SMatthew.Ahrens@Sun.COM 	mutex_exit(&zfsvfs->z_lock);
8009396SMatthew.Ahrens@Sun.COM 
8019396SMatthew.Ahrens@Sun.COM 	if (quota == 0) {
8029396SMatthew.Ahrens@Sun.COM 		err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
8039396SMatthew.Ahrens@Sun.COM 		if (err == ENOENT)
8049396SMatthew.Ahrens@Sun.COM 			err = 0;
8059396SMatthew.Ahrens@Sun.COM 	} else {
8069396SMatthew.Ahrens@Sun.COM 		err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, &quota, tx);
8079396SMatthew.Ahrens@Sun.COM 	}
8089396SMatthew.Ahrens@Sun.COM 	ASSERT(err == 0);
8099396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
8109396SMatthew.Ahrens@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
8119396SMatthew.Ahrens@Sun.COM 	dmu_tx_commit(tx);
8129396SMatthew.Ahrens@Sun.COM 	return (err);
8139396SMatthew.Ahrens@Sun.COM }
8149396SMatthew.Ahrens@Sun.COM 
8159396SMatthew.Ahrens@Sun.COM boolean_t
81611935SMark.Shellenbaum@Sun.COM zfs_fuid_overquota(zfsvfs_t *zfsvfs, boolean_t isgroup, uint64_t fuid)
8179396SMatthew.Ahrens@Sun.COM {
8189396SMatthew.Ahrens@Sun.COM 	char buf[32];
8199396SMatthew.Ahrens@Sun.COM 	uint64_t used, quota, usedobj, quotaobj;
8209396SMatthew.Ahrens@Sun.COM 	int err;
8219396SMatthew.Ahrens@Sun.COM 
8229396SMatthew.Ahrens@Sun.COM 	usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
8239396SMatthew.Ahrens@Sun.COM 	quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
8249396SMatthew.Ahrens@Sun.COM 
8259396SMatthew.Ahrens@Sun.COM 	if (quotaobj == 0 || zfsvfs->z_replay)
8269396SMatthew.Ahrens@Sun.COM 		return (B_FALSE);
8279396SMatthew.Ahrens@Sun.COM 
8289396SMatthew.Ahrens@Sun.COM 	(void) sprintf(buf, "%llx", (longlong_t)fuid);
8299396SMatthew.Ahrens@Sun.COM 	err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
8309396SMatthew.Ahrens@Sun.COM 	if (err != 0)
8319396SMatthew.Ahrens@Sun.COM 		return (B_FALSE);
8329396SMatthew.Ahrens@Sun.COM 
8339396SMatthew.Ahrens@Sun.COM 	err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
8349396SMatthew.Ahrens@Sun.COM 	if (err != 0)
8359396SMatthew.Ahrens@Sun.COM 		return (B_FALSE);
8369396SMatthew.Ahrens@Sun.COM 	return (used >= quota);
8379396SMatthew.Ahrens@Sun.COM }
8389396SMatthew.Ahrens@Sun.COM 
83911935SMark.Shellenbaum@Sun.COM boolean_t
84011935SMark.Shellenbaum@Sun.COM zfs_owner_overquota(zfsvfs_t *zfsvfs, znode_t *zp, boolean_t isgroup)
84111935SMark.Shellenbaum@Sun.COM {
84211935SMark.Shellenbaum@Sun.COM 	uint64_t fuid;
84311935SMark.Shellenbaum@Sun.COM 	uint64_t quotaobj;
84411935SMark.Shellenbaum@Sun.COM 	uid_t id;
84511935SMark.Shellenbaum@Sun.COM 
84611935SMark.Shellenbaum@Sun.COM 	quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
84711935SMark.Shellenbaum@Sun.COM 
84811935SMark.Shellenbaum@Sun.COM 	id = isgroup ? zp->z_gid : zp->z_uid;
84911935SMark.Shellenbaum@Sun.COM 
85011935SMark.Shellenbaum@Sun.COM 	if (quotaobj == 0 || zfsvfs->z_replay)
85111935SMark.Shellenbaum@Sun.COM 		return (B_FALSE);
85211935SMark.Shellenbaum@Sun.COM 
85311935SMark.Shellenbaum@Sun.COM 	if (IS_EPHEMERAL(id)) {
85411935SMark.Shellenbaum@Sun.COM 		VERIFY(0 == sa_lookup(zp->z_sa_hdl,
85511935SMark.Shellenbaum@Sun.COM 		    isgroup ? SA_ZPL_GID(zfsvfs) : SA_ZPL_UID(zfsvfs),
85611935SMark.Shellenbaum@Sun.COM 		    &fuid, sizeof (fuid)));
85711935SMark.Shellenbaum@Sun.COM 	} else {
85811935SMark.Shellenbaum@Sun.COM 		fuid = (uint64_t)id;
85911935SMark.Shellenbaum@Sun.COM 	}
86011935SMark.Shellenbaum@Sun.COM 
86111935SMark.Shellenbaum@Sun.COM 	return (zfs_fuid_overquota(zfsvfs, isgroup, fuid));
86211935SMark.Shellenbaum@Sun.COM }
86311935SMark.Shellenbaum@Sun.COM 
8649396SMatthew.Ahrens@Sun.COM int
86511185SSean.McEnroe@Sun.COM zfsvfs_create(const char *osname, zfsvfs_t **zfvp)
8669396SMatthew.Ahrens@Sun.COM {
8679396SMatthew.Ahrens@Sun.COM 	objset_t *os;
8689396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
8699396SMatthew.Ahrens@Sun.COM 	uint64_t zval;
8709396SMatthew.Ahrens@Sun.COM 	int i, error;
87111935SMark.Shellenbaum@Sun.COM 	uint64_t sa_obj;
8729396SMatthew.Ahrens@Sun.COM 
87310298SMatthew.Ahrens@Sun.COM 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
8749396SMatthew.Ahrens@Sun.COM 
87510298SMatthew.Ahrens@Sun.COM 	/*
87610298SMatthew.Ahrens@Sun.COM 	 * We claim to always be readonly so we can open snapshots;
87710298SMatthew.Ahrens@Sun.COM 	 * other ZPL code will prevent us from writing to snapshots.
87810298SMatthew.Ahrens@Sun.COM 	 */
87910298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zfsvfs, &os);
88010298SMatthew.Ahrens@Sun.COM 	if (error) {
88110298SMatthew.Ahrens@Sun.COM 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
88210298SMatthew.Ahrens@Sun.COM 		return (error);
8839396SMatthew.Ahrens@Sun.COM 	}
8849396SMatthew.Ahrens@Sun.COM 
8859396SMatthew.Ahrens@Sun.COM 	/*
8869396SMatthew.Ahrens@Sun.COM 	 * Initialize the zfs-specific filesystem structure.
8879396SMatthew.Ahrens@Sun.COM 	 * Should probably make this a kmem cache, shuffle fields,
8889396SMatthew.Ahrens@Sun.COM 	 * and just bzero up to z_hold_mtx[].
8899396SMatthew.Ahrens@Sun.COM 	 */
8909396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_vfs = NULL;
8919396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_parent = zfsvfs;
8929396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
8939396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
8949396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_os = os;
8959396SMatthew.Ahrens@Sun.COM 
8969396SMatthew.Ahrens@Sun.COM 	error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
8979396SMatthew.Ahrens@Sun.COM 	if (error) {
8989396SMatthew.Ahrens@Sun.COM 		goto out;
899*12070SMark.Shellenbaum@Sun.COM 	} else if (zfsvfs->z_version >
900*12070SMark.Shellenbaum@Sun.COM 	    zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
901*12070SMark.Shellenbaum@Sun.COM 		(void) printf("Can't mount a version %lld file system "
902*12070SMark.Shellenbaum@Sun.COM 		    "on a version %lld pool\n. Pool must be upgraded to mount "
903*12070SMark.Shellenbaum@Sun.COM 		    "this file system.", (u_longlong_t)zfsvfs->z_version,
904*12070SMark.Shellenbaum@Sun.COM 		    (u_longlong_t)spa_version(dmu_objset_spa(os)));
9059396SMatthew.Ahrens@Sun.COM 		error = ENOTSUP;
9069396SMatthew.Ahrens@Sun.COM 		goto out;
9079396SMatthew.Ahrens@Sun.COM 	}
9089396SMatthew.Ahrens@Sun.COM 	if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
9099396SMatthew.Ahrens@Sun.COM 		goto out;
9109396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_norm = (int)zval;
9119396SMatthew.Ahrens@Sun.COM 
9129396SMatthew.Ahrens@Sun.COM 	if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
9139396SMatthew.Ahrens@Sun.COM 		goto out;
9149396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_utf8 = (zval != 0);
9159396SMatthew.Ahrens@Sun.COM 
9169396SMatthew.Ahrens@Sun.COM 	if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
9179396SMatthew.Ahrens@Sun.COM 		goto out;
9189396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_case = (uint_t)zval;
9199396SMatthew.Ahrens@Sun.COM 
9209396SMatthew.Ahrens@Sun.COM 	/*
9219396SMatthew.Ahrens@Sun.COM 	 * Fold case on file systems that are always or sometimes case
9229396SMatthew.Ahrens@Sun.COM 	 * insensitive.
9239396SMatthew.Ahrens@Sun.COM 	 */
9249396SMatthew.Ahrens@Sun.COM 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
9259396SMatthew.Ahrens@Sun.COM 	    zfsvfs->z_case == ZFS_CASE_MIXED)
9269396SMatthew.Ahrens@Sun.COM 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
9279396SMatthew.Ahrens@Sun.COM 
9289396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
92911935SMark.Shellenbaum@Sun.COM 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
93011935SMark.Shellenbaum@Sun.COM 
93111935SMark.Shellenbaum@Sun.COM 	if (zfsvfs->z_use_sa) {
93211935SMark.Shellenbaum@Sun.COM 		/* should either have both of these objects or none */
93311935SMark.Shellenbaum@Sun.COM 		error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
93411935SMark.Shellenbaum@Sun.COM 		    &sa_obj);
93511935SMark.Shellenbaum@Sun.COM 		if (error)
93611935SMark.Shellenbaum@Sun.COM 			return (error);
93711935SMark.Shellenbaum@Sun.COM 	} else {
93811935SMark.Shellenbaum@Sun.COM 		/*
93911935SMark.Shellenbaum@Sun.COM 		 * Pre SA versions file systems should never touch
94011935SMark.Shellenbaum@Sun.COM 		 * either the attribute registration or layout objects.
94111935SMark.Shellenbaum@Sun.COM 		 */
94211935SMark.Shellenbaum@Sun.COM 		sa_obj = 0;
94311935SMark.Shellenbaum@Sun.COM 	}
94411935SMark.Shellenbaum@Sun.COM 
94511935SMark.Shellenbaum@Sun.COM 	zfsvfs->z_attr_table = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END);
94611935SMark.Shellenbaum@Sun.COM 
94711935SMark.Shellenbaum@Sun.COM 	if (zfsvfs->z_version >= ZPL_VERSION_SA)
94811935SMark.Shellenbaum@Sun.COM 		sa_register_update_callback(os, zfs_sa_upgrade);
9499396SMatthew.Ahrens@Sun.COM 
9509396SMatthew.Ahrens@Sun.COM 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
9519396SMatthew.Ahrens@Sun.COM 	    &zfsvfs->z_root);
9529396SMatthew.Ahrens@Sun.COM 	if (error)
9539396SMatthew.Ahrens@Sun.COM 		goto out;
9549396SMatthew.Ahrens@Sun.COM 	ASSERT(zfsvfs->z_root != 0);
9559396SMatthew.Ahrens@Sun.COM 
9569396SMatthew.Ahrens@Sun.COM 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
9579396SMatthew.Ahrens@Sun.COM 	    &zfsvfs->z_unlinkedobj);
9589396SMatthew.Ahrens@Sun.COM 	if (error)
9599396SMatthew.Ahrens@Sun.COM 		goto out;
9609396SMatthew.Ahrens@Sun.COM 
9619396SMatthew.Ahrens@Sun.COM 	error = zap_lookup(os, MASTER_NODE_OBJ,
9629396SMatthew.Ahrens@Sun.COM 	    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
9639396SMatthew.Ahrens@Sun.COM 	    8, 1, &zfsvfs->z_userquota_obj);
9649396SMatthew.Ahrens@Sun.COM 	if (error && error != ENOENT)
9659396SMatthew.Ahrens@Sun.COM 		goto out;
9669396SMatthew.Ahrens@Sun.COM 
9679396SMatthew.Ahrens@Sun.COM 	error = zap_lookup(os, MASTER_NODE_OBJ,
9689396SMatthew.Ahrens@Sun.COM 	    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
9699396SMatthew.Ahrens@Sun.COM 	    8, 1, &zfsvfs->z_groupquota_obj);
9709396SMatthew.Ahrens@Sun.COM 	if (error && error != ENOENT)
9719396SMatthew.Ahrens@Sun.COM 		goto out;
9729396SMatthew.Ahrens@Sun.COM 
9739396SMatthew.Ahrens@Sun.COM 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
9749396SMatthew.Ahrens@Sun.COM 	    &zfsvfs->z_fuid_obj);
9759396SMatthew.Ahrens@Sun.COM 	if (error && error != ENOENT)
9769396SMatthew.Ahrens@Sun.COM 		goto out;
9779396SMatthew.Ahrens@Sun.COM 
9789396SMatthew.Ahrens@Sun.COM 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
9799396SMatthew.Ahrens@Sun.COM 	    &zfsvfs->z_shares_dir);
9809396SMatthew.Ahrens@Sun.COM 	if (error && error != ENOENT)
9819396SMatthew.Ahrens@Sun.COM 		goto out;
9829396SMatthew.Ahrens@Sun.COM 
9839396SMatthew.Ahrens@Sun.COM 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
9849396SMatthew.Ahrens@Sun.COM 	mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
9859396SMatthew.Ahrens@Sun.COM 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
9869396SMatthew.Ahrens@Sun.COM 	    offsetof(znode_t, z_link_node));
9879396SMatthew.Ahrens@Sun.COM 	rrw_init(&zfsvfs->z_teardown_lock);
9889396SMatthew.Ahrens@Sun.COM 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
9899396SMatthew.Ahrens@Sun.COM 	rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
9909396SMatthew.Ahrens@Sun.COM 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
9919396SMatthew.Ahrens@Sun.COM 		mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
9929396SMatthew.Ahrens@Sun.COM 
99311185SSean.McEnroe@Sun.COM 	*zfvp = zfsvfs;
9949396SMatthew.Ahrens@Sun.COM 	return (0);
9959396SMatthew.Ahrens@Sun.COM 
9969396SMatthew.Ahrens@Sun.COM out:
99710298SMatthew.Ahrens@Sun.COM 	dmu_objset_disown(os, zfsvfs);
99811185SSean.McEnroe@Sun.COM 	*zfvp = NULL;
9999396SMatthew.Ahrens@Sun.COM 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
10009396SMatthew.Ahrens@Sun.COM 	return (error);
10019396SMatthew.Ahrens@Sun.COM }
10029396SMatthew.Ahrens@Sun.COM 
10031544Seschrock static int
10045326Sek110237 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
10055326Sek110237 {
10065326Sek110237 	int error;
10075326Sek110237 
10085326Sek110237 	error = zfs_register_callbacks(zfsvfs->z_vfs);
10095326Sek110237 	if (error)
10105326Sek110237 		return (error);
10115326Sek110237 
10125326Sek110237 	/*
10135326Sek110237 	 * Set the objset user_ptr to track its zfsvfs.
10145326Sek110237 	 */
101510298SMatthew.Ahrens@Sun.COM 	mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
10165326Sek110237 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
101710298SMatthew.Ahrens@Sun.COM 	mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
10185326Sek110237 
10199292SNeil.Perrin@Sun.COM 	zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
10209292SNeil.Perrin@Sun.COM 	if (zil_disable) {
102110685SGeorge.Wilson@Sun.COM 		zil_destroy(zfsvfs->z_log, B_FALSE);
10229292SNeil.Perrin@Sun.COM 		zfsvfs->z_log = NULL;
10239292SNeil.Perrin@Sun.COM 	}
10249292SNeil.Perrin@Sun.COM 
10255326Sek110237 	/*
10265326Sek110237 	 * If we are not mounting (ie: online recv), then we don't
10275326Sek110237 	 * have to worry about replaying the log as we blocked all
10285326Sek110237 	 * operations out since we closed the ZIL.
10295326Sek110237 	 */
10305326Sek110237 	if (mounting) {
10317638SNeil.Perrin@Sun.COM 		boolean_t readonly;
10327638SNeil.Perrin@Sun.COM 
10335326Sek110237 		/*
10345326Sek110237 		 * During replay we remove the read only flag to
10355326Sek110237 		 * allow replays to succeed.
10365326Sek110237 		 */
10375326Sek110237 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
10388227SNeil.Perrin@Sun.COM 		if (readonly != 0)
10398227SNeil.Perrin@Sun.COM 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
10408227SNeil.Perrin@Sun.COM 		else
10418227SNeil.Perrin@Sun.COM 			zfs_unlinked_drain(zfsvfs);
10425326Sek110237 
10439292SNeil.Perrin@Sun.COM 		if (zfsvfs->z_log) {
10448227SNeil.Perrin@Sun.COM 			/*
10458227SNeil.Perrin@Sun.COM 			 * Parse and replay the intent log.
10468227SNeil.Perrin@Sun.COM 			 *
10478227SNeil.Perrin@Sun.COM 			 * Because of ziltest, this must be done after
10488227SNeil.Perrin@Sun.COM 			 * zfs_unlinked_drain().  (Further note: ziltest
10498227SNeil.Perrin@Sun.COM 			 * doesn't use readonly mounts, where
10508227SNeil.Perrin@Sun.COM 			 * zfs_unlinked_drain() isn't called.)  This is because
10518227SNeil.Perrin@Sun.COM 			 * ziltest causes spa_sync() to think it's committed,
10528227SNeil.Perrin@Sun.COM 			 * but actually it is not, so the intent log contains
10538227SNeil.Perrin@Sun.COM 			 * many txg's worth of changes.
10548227SNeil.Perrin@Sun.COM 			 *
10558227SNeil.Perrin@Sun.COM 			 * In particular, if object N is in the unlinked set in
10568227SNeil.Perrin@Sun.COM 			 * the last txg to actually sync, then it could be
10578227SNeil.Perrin@Sun.COM 			 * actually freed in a later txg and then reallocated
10588227SNeil.Perrin@Sun.COM 			 * in a yet later txg.  This would write a "create
10598227SNeil.Perrin@Sun.COM 			 * object N" record to the intent log.  Normally, this
10608227SNeil.Perrin@Sun.COM 			 * would be fine because the spa_sync() would have
10618227SNeil.Perrin@Sun.COM 			 * written out the fact that object N is free, before
10628227SNeil.Perrin@Sun.COM 			 * we could write the "create object N" intent log
10638227SNeil.Perrin@Sun.COM 			 * record.
10648227SNeil.Perrin@Sun.COM 			 *
10658227SNeil.Perrin@Sun.COM 			 * But when we are in ziltest mode, we advance the "open
10668227SNeil.Perrin@Sun.COM 			 * txg" without actually spa_sync()-ing the changes to
10678227SNeil.Perrin@Sun.COM 			 * disk.  So we would see that object N is still
10688227SNeil.Perrin@Sun.COM 			 * allocated and in the unlinked set, and there is an
10698227SNeil.Perrin@Sun.COM 			 * intent log record saying to allocate it.
10708227SNeil.Perrin@Sun.COM 			 */
10718227SNeil.Perrin@Sun.COM 			zfsvfs->z_replay = B_TRUE;
10728227SNeil.Perrin@Sun.COM 			zil_replay(zfsvfs->z_os, zfsvfs, zfs_replay_vector);
10738227SNeil.Perrin@Sun.COM 			zfsvfs->z_replay = B_FALSE;
10748227SNeil.Perrin@Sun.COM 		}
10755326Sek110237 		zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
10765326Sek110237 	}
10775326Sek110237 
10785326Sek110237 	return (0);
10795326Sek110237 }
10805326Sek110237 
10819396SMatthew.Ahrens@Sun.COM void
10829396SMatthew.Ahrens@Sun.COM zfsvfs_free(zfsvfs_t *zfsvfs)
10836083Sek110237 {
10849396SMatthew.Ahrens@Sun.COM 	int i;
10859788STom.Erickson@Sun.COM 	extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
10869788STom.Erickson@Sun.COM 
10879788STom.Erickson@Sun.COM 	/*
10889788STom.Erickson@Sun.COM 	 * This is a barrier to prevent the filesystem from going away in
10899788STom.Erickson@Sun.COM 	 * zfs_znode_move() until we can safely ensure that the filesystem is
10909788STom.Erickson@Sun.COM 	 * not unmounted. We consider the filesystem valid before the barrier
10919788STom.Erickson@Sun.COM 	 * and invalid after the barrier.
10929788STom.Erickson@Sun.COM 	 */
10939788STom.Erickson@Sun.COM 	rw_enter(&zfsvfs_lock, RW_READER);
10949788STom.Erickson@Sun.COM 	rw_exit(&zfsvfs_lock);
10959396SMatthew.Ahrens@Sun.COM 
10969396SMatthew.Ahrens@Sun.COM 	zfs_fuid_destroy(zfsvfs);
10979396SMatthew.Ahrens@Sun.COM 
10986083Sek110237 	mutex_destroy(&zfsvfs->z_znodes_lock);
10999030SMark.Shellenbaum@Sun.COM 	mutex_destroy(&zfsvfs->z_lock);
11006083Sek110237 	list_destroy(&zfsvfs->z_all_znodes);
11016083Sek110237 	rrw_destroy(&zfsvfs->z_teardown_lock);
11026083Sek110237 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
11036083Sek110237 	rw_destroy(&zfsvfs->z_fuid_lock);
11049396SMatthew.Ahrens@Sun.COM 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
11059396SMatthew.Ahrens@Sun.COM 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
11066083Sek110237 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
11076083Sek110237 }
11086083Sek110237 
11099396SMatthew.Ahrens@Sun.COM static void
11109396SMatthew.Ahrens@Sun.COM zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
11119396SMatthew.Ahrens@Sun.COM {
11129396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
11139396SMatthew.Ahrens@Sun.COM 	if (zfsvfs->z_use_fuids && zfsvfs->z_vfs) {
11149396SMatthew.Ahrens@Sun.COM 		vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
11159396SMatthew.Ahrens@Sun.COM 		vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
11169396SMatthew.Ahrens@Sun.COM 		vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
11179396SMatthew.Ahrens@Sun.COM 		vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
11189749STim.Haley@Sun.COM 		vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
111910793Sdai.ngo@sun.com 		vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
11209396SMatthew.Ahrens@Sun.COM 	}
112111935SMark.Shellenbaum@Sun.COM 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
11229396SMatthew.Ahrens@Sun.COM }
11239396SMatthew.Ahrens@Sun.COM 
11245326Sek110237 static int
11257046Sahrens zfs_domount(vfs_t *vfsp, char *osname)
11261544Seschrock {
11271544Seschrock 	dev_t mount_dev;
11289396SMatthew.Ahrens@Sun.COM 	uint64_t recordsize, fsid_guid;
11291544Seschrock 	int error = 0;
11301544Seschrock 	zfsvfs_t *zfsvfs;
11311544Seschrock 
11321544Seschrock 	ASSERT(vfsp);
11331544Seschrock 	ASSERT(osname);
11341544Seschrock 
113510298SMatthew.Ahrens@Sun.COM 	error = zfsvfs_create(osname, &zfsvfs);
11369396SMatthew.Ahrens@Sun.COM 	if (error)
11379396SMatthew.Ahrens@Sun.COM 		return (error);
11381544Seschrock 	zfsvfs->z_vfs = vfsp;
11391544Seschrock 
11401544Seschrock 	/* Initialize the generic filesystem structure. */
11411544Seschrock 	vfsp->vfs_bcount = 0;
11421544Seschrock 	vfsp->vfs_data = NULL;
11431544Seschrock 
11441544Seschrock 	if (zfs_create_unique_device(&mount_dev) == -1) {
11451544Seschrock 		error = ENODEV;
11461544Seschrock 		goto out;
11471544Seschrock 	}
11481544Seschrock 	ASSERT(vfs_devismounted(mount_dev) == 0);
11491544Seschrock 
11501544Seschrock 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
11511544Seschrock 	    NULL))
11521544Seschrock 		goto out;
11531544Seschrock 
11541544Seschrock 	vfsp->vfs_dev = mount_dev;
11551544Seschrock 	vfsp->vfs_fstype = zfsfstype;
11561544Seschrock 	vfsp->vfs_bsize = recordsize;
11571544Seschrock 	vfsp->vfs_flag |= VFS_NOTRUNC;
11581544Seschrock 	vfsp->vfs_data = zfsvfs;
11591544Seschrock 
11609396SMatthew.Ahrens@Sun.COM 	/*
11619396SMatthew.Ahrens@Sun.COM 	 * The fsid is 64 bits, composed of an 8-bit fs type, which
11629396SMatthew.Ahrens@Sun.COM 	 * separates our fsid from any other filesystem types, and a
11639396SMatthew.Ahrens@Sun.COM 	 * 56-bit objset unique ID.  The objset unique ID is unique to
11649396SMatthew.Ahrens@Sun.COM 	 * all objsets open on this system, provided by unique_create().
11659396SMatthew.Ahrens@Sun.COM 	 * The 8-bit fs type must be put in the low bits of fsid[1]
11669396SMatthew.Ahrens@Sun.COM 	 * because that's where other Solaris filesystems put it.
11679396SMatthew.Ahrens@Sun.COM 	 */
11689396SMatthew.Ahrens@Sun.COM 	fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
11699396SMatthew.Ahrens@Sun.COM 	ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
11709396SMatthew.Ahrens@Sun.COM 	vfsp->vfs_fsid.val[0] = fsid_guid;
11719396SMatthew.Ahrens@Sun.COM 	vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
11729396SMatthew.Ahrens@Sun.COM 	    zfsfstype & 0xFF;
11731544Seschrock 
11745331Samw 	/*
11755331Samw 	 * Set features for file system.
11765331Samw 	 */
11779396SMatthew.Ahrens@Sun.COM 	zfs_set_fuid_feature(zfsvfs);
11785498Stimh 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
11795498Stimh 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
11805498Stimh 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
11815498Stimh 		vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
11825498Stimh 	} else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
11835498Stimh 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
11845498Stimh 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
11855498Stimh 	}
118611539SChunli.Zhang@Sun.COM 	vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
11875331Samw 
11881544Seschrock 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
11895331Samw 		uint64_t pval;
11903234Sck153898 
11911544Seschrock 		atime_changed_cb(zfsvfs, B_FALSE);
11921544Seschrock 		readonly_changed_cb(zfsvfs, B_TRUE);
11935331Samw 		if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
11943234Sck153898 			goto out;
11955331Samw 		xattr_changed_cb(zfsvfs, pval);
11961544Seschrock 		zfsvfs->z_issnap = B_TRUE;
11979688SMatthew.Ahrens@Sun.COM 
119810298SMatthew.Ahrens@Sun.COM 		mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
11999688SMatthew.Ahrens@Sun.COM 		dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
120010298SMatthew.Ahrens@Sun.COM 		mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
12011544Seschrock 	} else {
12025326Sek110237 		error = zfsvfs_setup(zfsvfs, B_TRUE);
12031544Seschrock 	}
12041544Seschrock 
12051544Seschrock 	if (!zfsvfs->z_issnap)
12061544Seschrock 		zfsctl_create(zfsvfs);
12071544Seschrock out:
12081544Seschrock 	if (error) {
120910298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
12109396SMatthew.Ahrens@Sun.COM 		zfsvfs_free(zfsvfs);
12111544Seschrock 	} else {
12121544Seschrock 		atomic_add_32(&zfs_active_fs_count, 1);
12131544Seschrock 	}
12141544Seschrock 
12151544Seschrock 	return (error);
12161544Seschrock }
12171544Seschrock 
12181544Seschrock void
12191544Seschrock zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
12201544Seschrock {
12211544Seschrock 	objset_t *os = zfsvfs->z_os;
12221544Seschrock 	struct dsl_dataset *ds;
12231544Seschrock 
12241544Seschrock 	/*
12251544Seschrock 	 * Unregister properties.
12261544Seschrock 	 */
12271544Seschrock 	if (!dmu_objset_is_snapshot(os)) {
12281544Seschrock 		ds = dmu_objset_ds(os);
12291544Seschrock 		VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
12301544Seschrock 		    zfsvfs) == 0);
12311544Seschrock 
12323234Sck153898 		VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
12333234Sck153898 		    zfsvfs) == 0);
12343234Sck153898 
12351544Seschrock 		VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
12361544Seschrock 		    zfsvfs) == 0);
12371544Seschrock 
12381544Seschrock 		VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
12391544Seschrock 		    zfsvfs) == 0);
12401544Seschrock 
12411544Seschrock 		VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
12421544Seschrock 		    zfsvfs) == 0);
12431544Seschrock 
12441544Seschrock 		VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
12451544Seschrock 		    zfsvfs) == 0);
12461544Seschrock 
12471544Seschrock 		VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
12481544Seschrock 		    zfsvfs) == 0);
12491544Seschrock 
12501544Seschrock 		VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
12511544Seschrock 		    zfsvfs) == 0);
12521544Seschrock 
12531544Seschrock 		VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
12541544Seschrock 		    zfsvfs) == 0);
12551544Seschrock 
12561544Seschrock 		VERIFY(dsl_prop_unregister(ds, "aclinherit",
12571544Seschrock 		    acl_inherit_changed_cb, zfsvfs) == 0);
12585331Samw 
12595331Samw 		VERIFY(dsl_prop_unregister(ds, "vscan",
12605331Samw 		    vscan_changed_cb, zfsvfs) == 0);
12611544Seschrock 	}
12621544Seschrock }
12631544Seschrock 
12643912Slling /*
12653912Slling  * Convert a decimal digit string to a uint64_t integer.
12663912Slling  */
12673912Slling static int
12683912Slling str_to_uint64(char *str, uint64_t *objnum)
12693912Slling {
12703912Slling 	uint64_t num = 0;
12713912Slling 
12723912Slling 	while (*str) {
12733912Slling 		if (*str < '0' || *str > '9')
12743912Slling 			return (EINVAL);
12753912Slling 
12763912Slling 		num = num*10 + *str++ - '0';
12773912Slling 	}
12783912Slling 
12793912Slling 	*objnum = num;
12803912Slling 	return (0);
12813912Slling }
12823912Slling 
12833912Slling /*
12843912Slling  * The boot path passed from the boot loader is in the form of
12853912Slling  * "rootpool-name/root-filesystem-object-number'. Convert this
12863912Slling  * string to a dataset name: "rootpool-name/root-filesystem-name".
12873912Slling  */
12883912Slling static int
12896423Sgw25295 zfs_parse_bootfs(char *bpath, char *outpath)
12903912Slling {
12913912Slling 	char *slashp;
12923912Slling 	uint64_t objnum;
12933912Slling 	int error;
12943912Slling 
12953912Slling 	if (*bpath == 0 || *bpath == '/')
12963912Slling 		return (EINVAL);
12973912Slling 
12987656SSherry.Moore@Sun.COM 	(void) strcpy(outpath, bpath);
12997656SSherry.Moore@Sun.COM 
13003912Slling 	slashp = strchr(bpath, '/');
13013912Slling 
13023912Slling 	/* if no '/', just return the pool name */
13033912Slling 	if (slashp == NULL) {
13043912Slling 		return (0);
13053912Slling 	}
13063912Slling 
13077656SSherry.Moore@Sun.COM 	/* if not a number, just return the root dataset name */
13087656SSherry.Moore@Sun.COM 	if (str_to_uint64(slashp+1, &objnum)) {
13097656SSherry.Moore@Sun.COM 		return (0);
13107656SSherry.Moore@Sun.COM 	}
13113912Slling 
13123912Slling 	*slashp = '\0';
13133912Slling 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
13143912Slling 	*slashp = '/';
13153912Slling 
13163912Slling 	return (error);
13173912Slling }
13183912Slling 
131910972SRic.Aleshire@Sun.COM /*
132010972SRic.Aleshire@Sun.COM  * zfs_check_global_label:
132110972SRic.Aleshire@Sun.COM  *	Check that the hex label string is appropriate for the dataset
132210972SRic.Aleshire@Sun.COM  *	being mounted into the global_zone proper.
132310972SRic.Aleshire@Sun.COM  *
132410972SRic.Aleshire@Sun.COM  *	Return an error if the hex label string is not default or
132510972SRic.Aleshire@Sun.COM  *	admin_low/admin_high.  For admin_low labels, the corresponding
132610972SRic.Aleshire@Sun.COM  *	dataset must be readonly.
132710972SRic.Aleshire@Sun.COM  */
132810972SRic.Aleshire@Sun.COM int
132910972SRic.Aleshire@Sun.COM zfs_check_global_label(const char *dsname, const char *hexsl)
133010972SRic.Aleshire@Sun.COM {
133110972SRic.Aleshire@Sun.COM 	if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
133210972SRic.Aleshire@Sun.COM 		return (0);
133310972SRic.Aleshire@Sun.COM 	if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
133410972SRic.Aleshire@Sun.COM 		return (0);
133510972SRic.Aleshire@Sun.COM 	if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
133610972SRic.Aleshire@Sun.COM 		/* must be readonly */
133710972SRic.Aleshire@Sun.COM 		uint64_t rdonly;
133810972SRic.Aleshire@Sun.COM 
133910972SRic.Aleshire@Sun.COM 		if (dsl_prop_get_integer(dsname,
134010972SRic.Aleshire@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
134110972SRic.Aleshire@Sun.COM 			return (EACCES);
134210972SRic.Aleshire@Sun.COM 		return (rdonly ? 0 : EACCES);
134310972SRic.Aleshire@Sun.COM 	}
134410972SRic.Aleshire@Sun.COM 	return (EACCES);
134510972SRic.Aleshire@Sun.COM }
134610972SRic.Aleshire@Sun.COM 
134710972SRic.Aleshire@Sun.COM /*
134810972SRic.Aleshire@Sun.COM  * zfs_mount_label_policy:
134910972SRic.Aleshire@Sun.COM  *	Determine whether the mount is allowed according to MAC check.
135010972SRic.Aleshire@Sun.COM  *	by comparing (where appropriate) label of the dataset against
135110972SRic.Aleshire@Sun.COM  *	the label of the zone being mounted into.  If the dataset has
135210972SRic.Aleshire@Sun.COM  *	no label, create one.
135310972SRic.Aleshire@Sun.COM  *
135410972SRic.Aleshire@Sun.COM  *	Returns:
135510972SRic.Aleshire@Sun.COM  *		 0 :	access allowed
135610972SRic.Aleshire@Sun.COM  *		>0 :	error code, such as EACCES
135710972SRic.Aleshire@Sun.COM  */
135810972SRic.Aleshire@Sun.COM static int
135910972SRic.Aleshire@Sun.COM zfs_mount_label_policy(vfs_t *vfsp, char *osname)
136010972SRic.Aleshire@Sun.COM {
136110972SRic.Aleshire@Sun.COM 	int		error, retv;
136210972SRic.Aleshire@Sun.COM 	zone_t		*mntzone = NULL;
136310972SRic.Aleshire@Sun.COM 	ts_label_t	*mnt_tsl;
136410972SRic.Aleshire@Sun.COM 	bslabel_t	*mnt_sl;
136510972SRic.Aleshire@Sun.COM 	bslabel_t	ds_sl;
136610972SRic.Aleshire@Sun.COM 	char		ds_hexsl[MAXNAMELEN];
136710972SRic.Aleshire@Sun.COM 
136810972SRic.Aleshire@Sun.COM 	retv = EACCES;				/* assume the worst */
136910972SRic.Aleshire@Sun.COM 
137010972SRic.Aleshire@Sun.COM 	/*
137110972SRic.Aleshire@Sun.COM 	 * Start by getting the dataset label if it exists.
137210972SRic.Aleshire@Sun.COM 	 */
137310972SRic.Aleshire@Sun.COM 	error = dsl_prop_get(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
137410972SRic.Aleshire@Sun.COM 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
137510972SRic.Aleshire@Sun.COM 	if (error)
137610972SRic.Aleshire@Sun.COM 		return (EACCES);
137710972SRic.Aleshire@Sun.COM 
137810972SRic.Aleshire@Sun.COM 	/*
137910972SRic.Aleshire@Sun.COM 	 * If labeling is NOT enabled, then disallow the mount of datasets
138010972SRic.Aleshire@Sun.COM 	 * which have a non-default label already.  No other label checks
138110972SRic.Aleshire@Sun.COM 	 * are needed.
138210972SRic.Aleshire@Sun.COM 	 */
138310972SRic.Aleshire@Sun.COM 	if (!is_system_labeled()) {
138410972SRic.Aleshire@Sun.COM 		if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
138510972SRic.Aleshire@Sun.COM 			return (0);
138610972SRic.Aleshire@Sun.COM 		return (EACCES);
138710972SRic.Aleshire@Sun.COM 	}
138810972SRic.Aleshire@Sun.COM 
138910972SRic.Aleshire@Sun.COM 	/*
139010972SRic.Aleshire@Sun.COM 	 * Get the label of the mountpoint.  If mounting into the global
139110972SRic.Aleshire@Sun.COM 	 * zone (i.e. mountpoint is not within an active zone and the
139210972SRic.Aleshire@Sun.COM 	 * zoned property is off), the label must be default or
139310972SRic.Aleshire@Sun.COM 	 * admin_low/admin_high only; no other checks are needed.
139410972SRic.Aleshire@Sun.COM 	 */
139510972SRic.Aleshire@Sun.COM 	mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE);
139610972SRic.Aleshire@Sun.COM 	if (mntzone->zone_id == GLOBAL_ZONEID) {
139710972SRic.Aleshire@Sun.COM 		uint64_t zoned;
139810972SRic.Aleshire@Sun.COM 
139910972SRic.Aleshire@Sun.COM 		zone_rele(mntzone);
140010972SRic.Aleshire@Sun.COM 
140110972SRic.Aleshire@Sun.COM 		if (dsl_prop_get_integer(osname,
140210972SRic.Aleshire@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
140310972SRic.Aleshire@Sun.COM 			return (EACCES);
140410972SRic.Aleshire@Sun.COM 		if (!zoned)
140510972SRic.Aleshire@Sun.COM 			return (zfs_check_global_label(osname, ds_hexsl));
140610972SRic.Aleshire@Sun.COM 		else
140710972SRic.Aleshire@Sun.COM 			/*
140810972SRic.Aleshire@Sun.COM 			 * This is the case of a zone dataset being mounted
140910972SRic.Aleshire@Sun.COM 			 * initially, before the zone has been fully created;
141010972SRic.Aleshire@Sun.COM 			 * allow this mount into global zone.
141110972SRic.Aleshire@Sun.COM 			 */
141210972SRic.Aleshire@Sun.COM 			return (0);
141310972SRic.Aleshire@Sun.COM 	}
141410972SRic.Aleshire@Sun.COM 
141510972SRic.Aleshire@Sun.COM 	mnt_tsl = mntzone->zone_slabel;
141610972SRic.Aleshire@Sun.COM 	ASSERT(mnt_tsl != NULL);
141710972SRic.Aleshire@Sun.COM 	label_hold(mnt_tsl);
141810972SRic.Aleshire@Sun.COM 	mnt_sl = label2bslabel(mnt_tsl);
141910972SRic.Aleshire@Sun.COM 
142010972SRic.Aleshire@Sun.COM 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) {
142110972SRic.Aleshire@Sun.COM 		/*
142210972SRic.Aleshire@Sun.COM 		 * The dataset doesn't have a real label, so fabricate one.
142310972SRic.Aleshire@Sun.COM 		 */
142410972SRic.Aleshire@Sun.COM 		char *str = NULL;
142510972SRic.Aleshire@Sun.COM 
142610972SRic.Aleshire@Sun.COM 		if (l_to_str_internal(mnt_sl, &str) == 0 &&
142710972SRic.Aleshire@Sun.COM 		    dsl_prop_set(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
142811022STom.Erickson@Sun.COM 		    ZPROP_SRC_LOCAL, 1, strlen(str) + 1, str) == 0)
142910972SRic.Aleshire@Sun.COM 			retv = 0;
143010972SRic.Aleshire@Sun.COM 		if (str != NULL)
143110972SRic.Aleshire@Sun.COM 			kmem_free(str, strlen(str) + 1);
143210972SRic.Aleshire@Sun.COM 	} else if (hexstr_to_label(ds_hexsl, &ds_sl) == 0) {
143310972SRic.Aleshire@Sun.COM 		/*
143410972SRic.Aleshire@Sun.COM 		 * Now compare labels to complete the MAC check.  If the
143510972SRic.Aleshire@Sun.COM 		 * labels are equal then allow access.  If the mountpoint
143610972SRic.Aleshire@Sun.COM 		 * label dominates the dataset label, allow readonly access.
143710972SRic.Aleshire@Sun.COM 		 * Otherwise, access is denied.
143810972SRic.Aleshire@Sun.COM 		 */
143910972SRic.Aleshire@Sun.COM 		if (blequal(mnt_sl, &ds_sl))
144010972SRic.Aleshire@Sun.COM 			retv = 0;
144110972SRic.Aleshire@Sun.COM 		else if (bldominates(mnt_sl, &ds_sl)) {
144210972SRic.Aleshire@Sun.COM 			vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
144310972SRic.Aleshire@Sun.COM 			retv = 0;
144410972SRic.Aleshire@Sun.COM 		}
144510972SRic.Aleshire@Sun.COM 	}
144610972SRic.Aleshire@Sun.COM 
144710972SRic.Aleshire@Sun.COM 	label_rele(mnt_tsl);
144810972SRic.Aleshire@Sun.COM 	zone_rele(mntzone);
144910972SRic.Aleshire@Sun.COM 	return (retv);
145010972SRic.Aleshire@Sun.COM }
145110972SRic.Aleshire@Sun.COM 
14521544Seschrock static int
14531544Seschrock zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
14541544Seschrock {
14551544Seschrock 	int error = 0;
14561544Seschrock 	static int zfsrootdone = 0;
14571544Seschrock 	zfsvfs_t *zfsvfs = NULL;
14581544Seschrock 	znode_t *zp = NULL;
14591544Seschrock 	vnode_t *vp = NULL;
14606423Sgw25295 	char *zfs_bootfs;
14617147Staylor 	char *zfs_devid;
14621544Seschrock 
14631544Seschrock 	ASSERT(vfsp);
14641544Seschrock 
14651544Seschrock 	/*
14663912Slling 	 * The filesystem that we mount as root is defined in the
14676423Sgw25295 	 * boot property "zfs-bootfs" with a format of
14686423Sgw25295 	 * "poolname/root-dataset-objnum".
14691544Seschrock 	 */
14701544Seschrock 	if (why == ROOT_INIT) {
14711544Seschrock 		if (zfsrootdone++)
14721544Seschrock 			return (EBUSY);
14736423Sgw25295 		/*
14746423Sgw25295 		 * the process of doing a spa_load will require the
14756423Sgw25295 		 * clock to be set before we could (for example) do
14766423Sgw25295 		 * something better by looking at the timestamp on
14776423Sgw25295 		 * an uberblock, so just set it to -1.
14786423Sgw25295 		 */
14796423Sgw25295 		clkset(-1);
14801544Seschrock 
14817147Staylor 		if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
14827147Staylor 			cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
14837147Staylor 			    "bootfs name");
14846423Sgw25295 			return (EINVAL);
14855648Ssetje 		}
14867147Staylor 		zfs_devid = spa_get_bootprop("diskdevid");
14877147Staylor 		error = spa_import_rootpool(rootfs.bo_name, zfs_devid);
14887147Staylor 		if (zfs_devid)
14897147Staylor 			spa_free_bootprop(zfs_devid);
14907147Staylor 		if (error) {
14917147Staylor 			spa_free_bootprop(zfs_bootfs);
14927147Staylor 			cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
14937147Staylor 			    error);
14947147Staylor 			return (error);
14957147Staylor 		}
14967147Staylor 		if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
14977147Staylor 			spa_free_bootprop(zfs_bootfs);
14987147Staylor 			cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
14996423Sgw25295 			    error);
15006423Sgw25295 			return (error);
15016423Sgw25295 		}
15023912Slling 
15037147Staylor 		spa_free_bootprop(zfs_bootfs);
15041544Seschrock 
15051544Seschrock 		if (error = vfs_lock(vfsp))
15061544Seschrock 			return (error);
15071544Seschrock 
15087046Sahrens 		if (error = zfs_domount(vfsp, rootfs.bo_name)) {
15097147Staylor 			cmn_err(CE_NOTE, "zfs_domount: error %d", error);
15101544Seschrock 			goto out;
15116423Sgw25295 		}
15121544Seschrock 
15131544Seschrock 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
15141544Seschrock 		ASSERT(zfsvfs);
15156423Sgw25295 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
15167147Staylor 			cmn_err(CE_NOTE, "zfs_zget: error %d", error);
15171544Seschrock 			goto out;
15186423Sgw25295 		}
15191544Seschrock 
15201544Seschrock 		vp = ZTOV(zp);
15211544Seschrock 		mutex_enter(&vp->v_lock);
15221544Seschrock 		vp->v_flag |= VROOT;
15231544Seschrock 		mutex_exit(&vp->v_lock);
15241544Seschrock 		rootvp = vp;
15251544Seschrock 
15261544Seschrock 		/*
15276570Smarks 		 * Leave rootvp held.  The root file system is never unmounted.
15281544Seschrock 		 */
15291544Seschrock 
15301544Seschrock 		vfs_add((struct vnode *)0, vfsp,
15311544Seschrock 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
15321544Seschrock out:
15331544Seschrock 		vfs_unlock(vfsp);
15346423Sgw25295 		return (error);
15351544Seschrock 	} else if (why == ROOT_REMOUNT) {
15361544Seschrock 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
15371544Seschrock 		vfsp->vfs_flag |= VFS_REMOUNT;
15384596Slling 
15394596Slling 		/* refresh mount options */
15404596Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
15414596Slling 		return (zfs_register_callbacks(vfsp));
15424596Slling 
15431544Seschrock 	} else if (why == ROOT_UNMOUNT) {
15441544Seschrock 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
15451544Seschrock 		(void) zfs_sync(vfsp, 0, 0);
15461544Seschrock 		return (0);
15471544Seschrock 	}
15481544Seschrock 
15491544Seschrock 	/*
15501544Seschrock 	 * if "why" is equal to anything else other than ROOT_INIT,
15511544Seschrock 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
15521544Seschrock 	 */
15531544Seschrock 	return (ENOTSUP);
15541544Seschrock }
15551544Seschrock 
1556789Sahrens /*ARGSUSED*/
1557789Sahrens static int
1558789Sahrens zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
1559789Sahrens {
1560789Sahrens 	char		*osname;
1561789Sahrens 	pathname_t	spn;
1562789Sahrens 	int		error = 0;
1563789Sahrens 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
15643912Slling 	    UIO_SYSSPACE : UIO_USERSPACE;
1565789Sahrens 	int		canwrite;
1566789Sahrens 
1567789Sahrens 	if (mvp->v_type != VDIR)
1568789Sahrens 		return (ENOTDIR);
1569789Sahrens 
1570789Sahrens 	mutex_enter(&mvp->v_lock);
1571789Sahrens 	if ((uap->flags & MS_REMOUNT) == 0 &&
1572789Sahrens 	    (uap->flags & MS_OVERLAY) == 0 &&
1573789Sahrens 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
1574789Sahrens 		mutex_exit(&mvp->v_lock);
1575789Sahrens 		return (EBUSY);
1576789Sahrens 	}
1577789Sahrens 	mutex_exit(&mvp->v_lock);
1578789Sahrens 
1579789Sahrens 	/*
1580789Sahrens 	 * ZFS does not support passing unparsed data in via MS_DATA.
1581789Sahrens 	 * Users should use the MS_OPTIONSTR interface; this means
1582789Sahrens 	 * that all option parsing is already done and the options struct
1583789Sahrens 	 * can be interrogated.
1584789Sahrens 	 */
1585789Sahrens 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
1586789Sahrens 		return (EINVAL);
1587789Sahrens 
1588789Sahrens 	/*
1589789Sahrens 	 * Get the objset name (the "special" mount argument).
1590789Sahrens 	 */
1591789Sahrens 	if (error = pn_get(uap->spec, fromspace, &spn))
1592789Sahrens 		return (error);
1593789Sahrens 
1594789Sahrens 	osname = spn.pn_path;
1595789Sahrens 
15964543Smarks 	/*
15974543Smarks 	 * Check for mount privilege?
15984543Smarks 	 *
15994543Smarks 	 * If we don't have privilege then see if
16004543Smarks 	 * we have local permission to allow it
16014543Smarks 	 */
16024543Smarks 	error = secpolicy_fs_mount(cr, mvp, vfsp);
16034543Smarks 	if (error) {
160411824SMark.Shellenbaum@Sun.COM 		if (dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) == 0) {
16054543Smarks 			vattr_t		vattr;
16064543Smarks 
16074543Smarks 			/*
16084543Smarks 			 * Make sure user is the owner of the mount point
16094543Smarks 			 * or has sufficient privileges.
16104543Smarks 			 */
16114543Smarks 
16124543Smarks 			vattr.va_mask = AT_UID;
16134543Smarks 
161411824SMark.Shellenbaum@Sun.COM 			if (VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
16154543Smarks 				goto out;
16164543Smarks 			}
16174543Smarks 
16185489Smarks 			if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 &&
16195489Smarks 			    VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) {
16204543Smarks 				goto out;
16214543Smarks 			}
16224543Smarks 			secpolicy_fs_mount_clearopts(cr, vfsp);
16234543Smarks 		} else {
16244543Smarks 			goto out;
16254543Smarks 		}
16264543Smarks 	}
1627789Sahrens 
1628789Sahrens 	/*
1629789Sahrens 	 * Refuse to mount a filesystem if we are in a local zone and the
1630789Sahrens 	 * dataset is not visible.
1631789Sahrens 	 */
1632789Sahrens 	if (!INGLOBALZONE(curproc) &&
1633789Sahrens 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1634789Sahrens 		error = EPERM;
1635789Sahrens 		goto out;
1636789Sahrens 	}
1637789Sahrens 
163810972SRic.Aleshire@Sun.COM 	error = zfs_mount_label_policy(vfsp, osname);
163910972SRic.Aleshire@Sun.COM 	if (error)
164010972SRic.Aleshire@Sun.COM 		goto out;
164110972SRic.Aleshire@Sun.COM 
16424596Slling 	/*
16434596Slling 	 * When doing a remount, we simply refresh our temporary properties
16444596Slling 	 * according to those options set in the current VFS options.
16454596Slling 	 */
16464596Slling 	if (uap->flags & MS_REMOUNT) {
16474596Slling 		/* refresh mount options */
16484596Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
16494596Slling 		error = zfs_register_callbacks(vfsp);
16504596Slling 		goto out;
16514596Slling 	}
16524596Slling 
16537046Sahrens 	error = zfs_domount(vfsp, osname);
1654789Sahrens 
16559214Schris.kirby@sun.com 	/*
16569214Schris.kirby@sun.com 	 * Add an extra VFS_HOLD on our parent vfs so that it can't
16579214Schris.kirby@sun.com 	 * disappear due to a forced unmount.
16589214Schris.kirby@sun.com 	 */
16599246Schris.kirby@sun.com 	if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap)
16609214Schris.kirby@sun.com 		VFS_HOLD(mvp->v_vfsp);
16619214Schris.kirby@sun.com 
1662789Sahrens out:
1663789Sahrens 	pn_free(&spn);
1664789Sahrens 	return (error);
1665789Sahrens }
1666789Sahrens 
1667789Sahrens static int
1668789Sahrens zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1669789Sahrens {
1670789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1671789Sahrens 	dev32_t d32;
16722885Sahrens 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
1673789Sahrens 
1674789Sahrens 	ZFS_ENTER(zfsvfs);
1675789Sahrens 
16762885Sahrens 	dmu_objset_space(zfsvfs->z_os,
16772885Sahrens 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
1678789Sahrens 
1679789Sahrens 	/*
1680789Sahrens 	 * The underlying storage pool actually uses multiple block sizes.
1681789Sahrens 	 * We report the fragsize as the smallest block size we support,
1682789Sahrens 	 * and we report our blocksize as the filesystem's maximum blocksize.
1683789Sahrens 	 */
1684789Sahrens 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1685789Sahrens 	statp->f_bsize = zfsvfs->z_max_blksz;
1686789Sahrens 
1687789Sahrens 	/*
1688789Sahrens 	 * The following report "total" blocks of various kinds in the
1689789Sahrens 	 * file system, but reported in terms of f_frsize - the
1690789Sahrens 	 * "fragment" size.
1691789Sahrens 	 */
1692789Sahrens 
16932885Sahrens 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
16942885Sahrens 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
1695789Sahrens 	statp->f_bavail = statp->f_bfree; /* no root reservation */
1696789Sahrens 
1697789Sahrens 	/*
1698789Sahrens 	 * statvfs() should really be called statufs(), because it assumes
1699789Sahrens 	 * static metadata.  ZFS doesn't preallocate files, so the best
1700789Sahrens 	 * we can do is report the max that could possibly fit in f_files,
1701789Sahrens 	 * and that minus the number actually used in f_ffree.
1702789Sahrens 	 * For f_ffree, report the smaller of the number of object available
1703789Sahrens 	 * and the number of blocks (each object will take at least a block).
1704789Sahrens 	 */
17052885Sahrens 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
1706789Sahrens 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
17072885Sahrens 	statp->f_files = statp->f_ffree + usedobjs;
1708789Sahrens 
1709789Sahrens 	(void) cmpldev(&d32, vfsp->vfs_dev);
1710789Sahrens 	statp->f_fsid = d32;
1711789Sahrens 
1712789Sahrens 	/*
1713789Sahrens 	 * We're a zfs filesystem.
1714789Sahrens 	 */
1715789Sahrens 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1716789Sahrens 
17171123Smarks 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1718789Sahrens 
1719789Sahrens 	statp->f_namemax = ZFS_MAXNAMELEN;
1720789Sahrens 
1721789Sahrens 	/*
1722789Sahrens 	 * We have all of 32 characters to stuff a string here.
1723789Sahrens 	 * Is there anything useful we could/should provide?
1724789Sahrens 	 */
1725789Sahrens 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
1726789Sahrens 
1727789Sahrens 	ZFS_EXIT(zfsvfs);
1728789Sahrens 	return (0);
1729789Sahrens }
1730789Sahrens 
1731789Sahrens static int
1732789Sahrens zfs_root(vfs_t *vfsp, vnode_t **vpp)
1733789Sahrens {
1734789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1735789Sahrens 	znode_t *rootzp;
1736789Sahrens 	int error;
1737789Sahrens 
1738789Sahrens 	ZFS_ENTER(zfsvfs);
1739789Sahrens 
1740789Sahrens 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1741789Sahrens 	if (error == 0)
1742789Sahrens 		*vpp = ZTOV(rootzp);
1743789Sahrens 
1744789Sahrens 	ZFS_EXIT(zfsvfs);
1745789Sahrens 	return (error);
1746789Sahrens }
1747789Sahrens 
17485326Sek110237 /*
17495326Sek110237  * Teardown the zfsvfs::z_os.
17505326Sek110237  *
17515326Sek110237  * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
17525326Sek110237  * and 'z_teardown_inactive_lock' held.
17535326Sek110237  */
17545326Sek110237 static int
17555326Sek110237 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
17565326Sek110237 {
17575642Smaybee 	znode_t	*zp;
17585326Sek110237 
17595326Sek110237 	rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
17605326Sek110237 
17615326Sek110237 	if (!unmounting) {
17625326Sek110237 		/*
17635326Sek110237 		 * We purge the parent filesystem's vfsp as the parent
17645326Sek110237 		 * filesystem and all of its snapshots have their vnode's
17655326Sek110237 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
17665326Sek110237 		 * 'z_parent' is self referential for non-snapshots.
17675326Sek110237 		 */
17685326Sek110237 		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
17695326Sek110237 	}
17705326Sek110237 
17715326Sek110237 	/*
17725326Sek110237 	 * Close the zil. NB: Can't close the zil while zfs_inactive
17735326Sek110237 	 * threads are blocked as zil_close can call zfs_inactive.
17745326Sek110237 	 */
17755326Sek110237 	if (zfsvfs->z_log) {
17765326Sek110237 		zil_close(zfsvfs->z_log);
17775326Sek110237 		zfsvfs->z_log = NULL;
17785326Sek110237 	}
17795326Sek110237 
17805326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
17815326Sek110237 
17825326Sek110237 	/*
17835326Sek110237 	 * If we are not unmounting (ie: online recv) and someone already
17845326Sek110237 	 * unmounted this file system while we were doing the switcheroo,
17855326Sek110237 	 * or a reopen of z_os failed then just bail out now.
17865326Sek110237 	 */
17875326Sek110237 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
17885326Sek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
17895326Sek110237 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
17905326Sek110237 		return (EIO);
17915326Sek110237 	}
17925326Sek110237 
17935326Sek110237 	/*
17945326Sek110237 	 * At this point there are no vops active, and any new vops will
17955326Sek110237 	 * fail with EIO since we have z_teardown_lock for writer (only
17965326Sek110237 	 * relavent for forced unmount).
17975326Sek110237 	 *
17985326Sek110237 	 * Release all holds on dbufs.
17995326Sek110237 	 */
18005326Sek110237 	mutex_enter(&zfsvfs->z_znodes_lock);
18015642Smaybee 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
18025642Smaybee 	    zp = list_next(&zfsvfs->z_all_znodes, zp))
180311935SMark.Shellenbaum@Sun.COM 		if (zp->z_sa_hdl) {
18045642Smaybee 			ASSERT(ZTOV(zp)->v_count > 0);
18055642Smaybee 			zfs_znode_dmu_fini(zp);
18065326Sek110237 		}
18075326Sek110237 	mutex_exit(&zfsvfs->z_znodes_lock);
18085326Sek110237 
18095326Sek110237 	/*
18105326Sek110237 	 * If we are unmounting, set the unmounted flag and let new vops
18115326Sek110237 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
18125326Sek110237 	 * other vops will fail with EIO.
18135326Sek110237 	 */
18145326Sek110237 	if (unmounting) {
18155326Sek110237 		zfsvfs->z_unmounted = B_TRUE;
18165326Sek110237 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
18175326Sek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
18185326Sek110237 	}
18195326Sek110237 
18205326Sek110237 	/*
18215326Sek110237 	 * z_os will be NULL if there was an error in attempting to reopen
18225326Sek110237 	 * zfsvfs, so just return as the properties had already been
18235326Sek110237 	 * unregistered and cached data had been evicted before.
18245326Sek110237 	 */
18255326Sek110237 	if (zfsvfs->z_os == NULL)
18265326Sek110237 		return (0);
18275326Sek110237 
18285326Sek110237 	/*
18295326Sek110237 	 * Unregister properties.
18305326Sek110237 	 */
18315326Sek110237 	zfs_unregister_callbacks(zfsvfs);
18325326Sek110237 
18335326Sek110237 	/*
18345326Sek110237 	 * Evict cached data
18355326Sek110237 	 */
18366083Sek110237 	if (dmu_objset_evict_dbufs(zfsvfs->z_os)) {
18375429Smaybee 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
18386083Sek110237 		(void) dmu_objset_evict_dbufs(zfsvfs->z_os);
18395429Smaybee 	}
18405326Sek110237 
18415326Sek110237 	return (0);
18425326Sek110237 }
18435326Sek110237 
1844789Sahrens /*ARGSUSED*/
1845789Sahrens static int
1846789Sahrens zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1847789Sahrens {
1848789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
18495326Sek110237 	objset_t *os;
1850789Sahrens 	int ret;
1851789Sahrens 
18524543Smarks 	ret = secpolicy_fs_unmount(cr, vfsp);
18534543Smarks 	if (ret) {
185411824SMark.Shellenbaum@Sun.COM 		if (dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
185511824SMark.Shellenbaum@Sun.COM 		    ZFS_DELEG_PERM_MOUNT, cr))
18564543Smarks 			return (ret);
18574543Smarks 	}
18581484Sek110237 
18594736Sek110237 	/*
18604736Sek110237 	 * We purge the parent filesystem's vfsp as the parent filesystem
18614736Sek110237 	 * and all of its snapshots have their vnode's v_vfsp set to the
18624736Sek110237 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
18634736Sek110237 	 * referential for non-snapshots.
18644736Sek110237 	 */
18654736Sek110237 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
18661484Sek110237 
1867789Sahrens 	/*
1868789Sahrens 	 * Unmount any snapshots mounted under .zfs before unmounting the
1869789Sahrens 	 * dataset itself.
1870789Sahrens 	 */
1871789Sahrens 	if (zfsvfs->z_ctldir != NULL &&
18724543Smarks 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1873789Sahrens 		return (ret);
18744543Smarks 	}
1875789Sahrens 
18764787Sahrens 	if (!(fflag & MS_FORCE)) {
18774480Sgw25295 		/*
18784787Sahrens 		 * Check the number of active vnodes in the file system.
18794787Sahrens 		 * Our count is maintained in the vfs structure, but the
18804787Sahrens 		 * number is off by 1 to indicate a hold on the vfs
18814787Sahrens 		 * structure itself.
18824787Sahrens 		 *
18834787Sahrens 		 * The '.zfs' directory maintains a reference of its
18844787Sahrens 		 * own, and any active references underneath are
18854787Sahrens 		 * reflected in the vnode count.
1886789Sahrens 		 */
18874787Sahrens 		if (zfsvfs->z_ctldir == NULL) {
18884787Sahrens 			if (vfsp->vfs_count > 1)
18894787Sahrens 				return (EBUSY);
18904787Sahrens 		} else {
18914787Sahrens 			if (vfsp->vfs_count > 2 ||
18925326Sek110237 			    zfsvfs->z_ctldir->v_count > 1)
18934787Sahrens 				return (EBUSY);
1894789Sahrens 		}
1895789Sahrens 	}
1896789Sahrens 
1897789Sahrens 	vfsp->vfs_flag |= VFS_UNMOUNTED;
18984787Sahrens 
18995326Sek110237 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
19005326Sek110237 	os = zfsvfs->z_os;
19014787Sahrens 
19024787Sahrens 	/*
19035326Sek110237 	 * z_os will be NULL if there was an error in
19045326Sek110237 	 * attempting to reopen zfsvfs.
19054787Sahrens 	 */
19065326Sek110237 	if (os != NULL) {
19075326Sek110237 		/*
19085326Sek110237 		 * Unset the objset user_ptr.
19095326Sek110237 		 */
191010298SMatthew.Ahrens@Sun.COM 		mutex_enter(&os->os_user_ptr_lock);
19115326Sek110237 		dmu_objset_set_user(os, NULL);
191210298SMatthew.Ahrens@Sun.COM 		mutex_exit(&os->os_user_ptr_lock);
19134787Sahrens 
19145326Sek110237 		/*
19156689Smaybee 		 * Finally release the objset
19165326Sek110237 		 */
191710298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, zfsvfs);
19184787Sahrens 	}
19194787Sahrens 
19204787Sahrens 	/*
19214787Sahrens 	 * We can now safely destroy the '.zfs' directory node.
19224787Sahrens 	 */
19234787Sahrens 	if (zfsvfs->z_ctldir != NULL)
19244787Sahrens 		zfsctl_destroy(zfsvfs);
1925789Sahrens 
1926789Sahrens 	return (0);
1927789Sahrens }
1928789Sahrens 
1929789Sahrens static int
1930789Sahrens zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1931789Sahrens {
1932789Sahrens 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1933789Sahrens 	znode_t		*zp;
1934789Sahrens 	uint64_t	object = 0;
1935789Sahrens 	uint64_t	fid_gen = 0;
1936789Sahrens 	uint64_t	gen_mask;
1937789Sahrens 	uint64_t	zp_gen;
1938789Sahrens 	int 		i, err;
1939789Sahrens 
1940789Sahrens 	*vpp = NULL;
1941789Sahrens 
1942789Sahrens 	ZFS_ENTER(zfsvfs);
1943789Sahrens 
1944789Sahrens 	if (fidp->fid_len == LONG_FID_LEN) {
1945789Sahrens 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1946789Sahrens 		uint64_t	objsetid = 0;
1947789Sahrens 		uint64_t	setgen = 0;
1948789Sahrens 
1949789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1950789Sahrens 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1951789Sahrens 
1952789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1953789Sahrens 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1954789Sahrens 
1955789Sahrens 		ZFS_EXIT(zfsvfs);
1956789Sahrens 
1957789Sahrens 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1958789Sahrens 		if (err)
1959789Sahrens 			return (EINVAL);
1960789Sahrens 		ZFS_ENTER(zfsvfs);
1961789Sahrens 	}
1962789Sahrens 
1963789Sahrens 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1964789Sahrens 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1965789Sahrens 
1966789Sahrens 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1967789Sahrens 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1968789Sahrens 
1969789Sahrens 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1970789Sahrens 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1971789Sahrens 	} else {
1972789Sahrens 		ZFS_EXIT(zfsvfs);
1973789Sahrens 		return (EINVAL);
1974789Sahrens 	}
1975789Sahrens 
1976789Sahrens 	/* A zero fid_gen means we are in the .zfs control directories */
1977789Sahrens 	if (fid_gen == 0 &&
1978789Sahrens 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1979789Sahrens 		*vpp = zfsvfs->z_ctldir;
1980789Sahrens 		ASSERT(*vpp != NULL);
1981789Sahrens 		if (object == ZFSCTL_INO_SNAPDIR) {
1982789Sahrens 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
19835331Samw 			    0, NULL, NULL, NULL, NULL, NULL) == 0);
1984789Sahrens 		} else {
1985789Sahrens 			VN_HOLD(*vpp);
1986789Sahrens 		}
1987789Sahrens 		ZFS_EXIT(zfsvfs);
1988789Sahrens 		return (0);
1989789Sahrens 	}
1990789Sahrens 
1991789Sahrens 	gen_mask = -1ULL >> (64 - 8 * i);
1992789Sahrens 
1993789Sahrens 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1994789Sahrens 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1995789Sahrens 		ZFS_EXIT(zfsvfs);
1996789Sahrens 		return (err);
1997789Sahrens 	}
199811935SMark.Shellenbaum@Sun.COM 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
199911935SMark.Shellenbaum@Sun.COM 	    sizeof (uint64_t));
200011935SMark.Shellenbaum@Sun.COM 	zp_gen = zp_gen & gen_mask;
2001789Sahrens 	if (zp_gen == 0)
2002789Sahrens 		zp_gen = 1;
20033461Sahrens 	if (zp->z_unlinked || zp_gen != fid_gen) {
2004789Sahrens 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
2005789Sahrens 		VN_RELE(ZTOV(zp));
2006789Sahrens 		ZFS_EXIT(zfsvfs);
2007789Sahrens 		return (EINVAL);
2008789Sahrens 	}
2009789Sahrens 
2010789Sahrens 	*vpp = ZTOV(zp);
2011789Sahrens 	ZFS_EXIT(zfsvfs);
2012789Sahrens 	return (0);
2013789Sahrens }
2014789Sahrens 
20155326Sek110237 /*
20165326Sek110237  * Block out VOPs and close zfsvfs_t::z_os
20175326Sek110237  *
20185326Sek110237  * Note, if successful, then we return with the 'z_teardown_lock' and
20195326Sek110237  * 'z_teardown_inactive_lock' write held.
20205326Sek110237  */
20215326Sek110237 int
202210298SMatthew.Ahrens@Sun.COM zfs_suspend_fs(zfsvfs_t *zfsvfs)
20235326Sek110237 {
20245326Sek110237 	int error;
20255326Sek110237 
20265326Sek110237 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
20275326Sek110237 		return (error);
202810298SMatthew.Ahrens@Sun.COM 	dmu_objset_disown(zfsvfs->z_os, zfsvfs);
20295326Sek110237 
20305326Sek110237 	return (0);
20315326Sek110237 }
20325326Sek110237 
20335326Sek110237 /*
20345326Sek110237  * Reopen zfsvfs_t::z_os and release VOPs.
20355326Sek110237  */
20365326Sek110237 int
203710298SMatthew.Ahrens@Sun.COM zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname)
20385326Sek110237 {
203911935SMark.Shellenbaum@Sun.COM 	int err, err2;
20405326Sek110237 
20415326Sek110237 	ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
20425326Sek110237 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
20435326Sek110237 
204410298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_own(osname, DMU_OST_ZFS, B_FALSE, zfsvfs,
204510298SMatthew.Ahrens@Sun.COM 	    &zfsvfs->z_os);
20465326Sek110237 	if (err) {
20475326Sek110237 		zfsvfs->z_os = NULL;
20485326Sek110237 	} else {
20495326Sek110237 		znode_t *zp;
205011935SMark.Shellenbaum@Sun.COM 		uint64_t sa_obj = 0;
205111935SMark.Shellenbaum@Sun.COM 
205211935SMark.Shellenbaum@Sun.COM 		err2 = zap_lookup(zfsvfs->z_os, MASTER_NODE_OBJ,
205311935SMark.Shellenbaum@Sun.COM 		    ZFS_SA_ATTRS, 8, 1, &sa_obj);
205411935SMark.Shellenbaum@Sun.COM 
205511935SMark.Shellenbaum@Sun.COM 		if ((err || err2) && zfsvfs->z_version >= ZPL_VERSION_SA)
205611935SMark.Shellenbaum@Sun.COM 			goto bail;
205711935SMark.Shellenbaum@Sun.COM 
205811935SMark.Shellenbaum@Sun.COM 
205911935SMark.Shellenbaum@Sun.COM 		zfsvfs->z_attr_table = sa_setup(zfsvfs->z_os, sa_obj,
206011935SMark.Shellenbaum@Sun.COM 		    zfs_attr_table,  ZPL_END);
20615326Sek110237 
20625326Sek110237 		VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
20635326Sek110237 
20645326Sek110237 		/*
20655326Sek110237 		 * Attempt to re-establish all the active znodes with
20665326Sek110237 		 * their dbufs.  If a zfs_rezget() fails, then we'll let
20675326Sek110237 		 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
20685326Sek110237 		 * when they try to use their znode.
20695326Sek110237 		 */
20705326Sek110237 		mutex_enter(&zfsvfs->z_znodes_lock);
20715326Sek110237 		for (zp = list_head(&zfsvfs->z_all_znodes); zp;
20725326Sek110237 		    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
20735326Sek110237 			(void) zfs_rezget(zp);
20745326Sek110237 		}
20755326Sek110237 		mutex_exit(&zfsvfs->z_znodes_lock);
20765326Sek110237 
20775326Sek110237 	}
20785326Sek110237 
207911935SMark.Shellenbaum@Sun.COM bail:
20805326Sek110237 	/* release the VOPs */
20815326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
20825326Sek110237 	rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
20835326Sek110237 
20845326Sek110237 	if (err) {
20855326Sek110237 		/*
20865326Sek110237 		 * Since we couldn't reopen zfsvfs::z_os, force
20875326Sek110237 		 * unmount this file system.
20885326Sek110237 		 */
20895326Sek110237 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
20905326Sek110237 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
20915326Sek110237 	}
20925326Sek110237 	return (err);
20935326Sek110237 }
20945326Sek110237 
2095789Sahrens static void
2096789Sahrens zfs_freevfs(vfs_t *vfsp)
2097789Sahrens {
2098789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
20999214Schris.kirby@sun.com 
21009214Schris.kirby@sun.com 	/*
21019214Schris.kirby@sun.com 	 * If this is a snapshot, we have an extra VFS_HOLD on our parent
21029214Schris.kirby@sun.com 	 * from zfs_mount().  Release it here.
21039214Schris.kirby@sun.com 	 */
21049214Schris.kirby@sun.com 	if (zfsvfs->z_issnap)
21059214Schris.kirby@sun.com 		VFS_RELE(zfsvfs->z_parent->z_vfs);
21069214Schris.kirby@sun.com 
21079396SMatthew.Ahrens@Sun.COM 	zfsvfs_free(zfsvfs);
2108789Sahrens 
2109789Sahrens 	atomic_add_32(&zfs_active_fs_count, -1);
2110789Sahrens }
2111789Sahrens 
2112789Sahrens /*
2113789Sahrens  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
2114789Sahrens  * so we can't safely do any non-idempotent initialization here.
2115789Sahrens  * Leave that to zfs_init() and zfs_fini(), which are called
2116789Sahrens  * from the module's _init() and _fini() entry points.
2117789Sahrens  */
2118789Sahrens /*ARGSUSED*/
2119789Sahrens static int
2120789Sahrens zfs_vfsinit(int fstype, char *name)
2121789Sahrens {
2122789Sahrens 	int error;
2123789Sahrens 
2124789Sahrens 	zfsfstype = fstype;
2125789Sahrens 
2126789Sahrens 	/*
2127789Sahrens 	 * Setup vfsops and vnodeops tables.
2128789Sahrens 	 */
2129789Sahrens 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
2130789Sahrens 	if (error != 0) {
2131789Sahrens 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
2132789Sahrens 	}
2133789Sahrens 
2134789Sahrens 	error = zfs_create_op_tables();
2135789Sahrens 	if (error) {
2136789Sahrens 		zfs_remove_op_tables();
2137789Sahrens 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
2138789Sahrens 		(void) vfs_freevfsops_by_type(zfsfstype);
2139789Sahrens 		return (error);
2140789Sahrens 	}
2141789Sahrens 
2142789Sahrens 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
2143789Sahrens 
2144789Sahrens 	/*
2145849Sbonwick 	 * Unique major number for all zfs mounts.
2146849Sbonwick 	 * If we run out of 32-bit minors, we'll getudev() another major.
2147789Sahrens 	 */
2148849Sbonwick 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
2149849Sbonwick 	zfs_minor = ZFS_MIN_MINOR;
2150789Sahrens 
2151789Sahrens 	return (0);
2152789Sahrens }
2153789Sahrens 
2154789Sahrens void
2155789Sahrens zfs_init(void)
2156789Sahrens {
2157789Sahrens 	/*
2158789Sahrens 	 * Initialize .zfs directory structures
2159789Sahrens 	 */
2160789Sahrens 	zfsctl_init();
2161789Sahrens 
2162789Sahrens 	/*
2163789Sahrens 	 * Initialize znode cache, vnode ops, etc...
2164789Sahrens 	 */
2165789Sahrens 	zfs_znode_init();
21669396SMatthew.Ahrens@Sun.COM 
21679396SMatthew.Ahrens@Sun.COM 	dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
2168789Sahrens }
2169789Sahrens 
2170789Sahrens void
2171789Sahrens zfs_fini(void)
2172789Sahrens {
2173789Sahrens 	zfsctl_fini();
2174789Sahrens 	zfs_znode_fini();
2175789Sahrens }
2176789Sahrens 
2177789Sahrens int
2178789Sahrens zfs_busy(void)
2179789Sahrens {
2180789Sahrens 	return (zfs_active_fs_count != 0);
2181789Sahrens }
2182789Sahrens 
21834577Sahrens int
21849396SMatthew.Ahrens@Sun.COM zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
21854577Sahrens {
21864577Sahrens 	int error;
21879396SMatthew.Ahrens@Sun.COM 	objset_t *os = zfsvfs->z_os;
21884577Sahrens 	dmu_tx_t *tx;
21894577Sahrens 
21904577Sahrens 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
21914577Sahrens 		return (EINVAL);
21924577Sahrens 
21939396SMatthew.Ahrens@Sun.COM 	if (newvers < zfsvfs->z_version)
21949396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
21954577Sahrens 
219611935SMark.Shellenbaum@Sun.COM 	if (zfs_spa_version_map(newvers) >
219711935SMark.Shellenbaum@Sun.COM 	    spa_version(dmu_objset_spa(zfsvfs->z_os)))
219811935SMark.Shellenbaum@Sun.COM 		return (ENOTSUP);
219911935SMark.Shellenbaum@Sun.COM 
22004577Sahrens 	tx = dmu_tx_create(os);
22019396SMatthew.Ahrens@Sun.COM 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
220211935SMark.Shellenbaum@Sun.COM 	if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
220311935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
220411935SMark.Shellenbaum@Sun.COM 		    ZFS_SA_ATTRS);
220511935SMark.Shellenbaum@Sun.COM 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
220611935SMark.Shellenbaum@Sun.COM 	}
22074577Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
22084577Sahrens 	if (error) {
22094577Sahrens 		dmu_tx_abort(tx);
22109396SMatthew.Ahrens@Sun.COM 		return (error);
22114577Sahrens 	}
221211935SMark.Shellenbaum@Sun.COM 
22139396SMatthew.Ahrens@Sun.COM 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
22149396SMatthew.Ahrens@Sun.COM 	    8, 1, &newvers, tx);
22159396SMatthew.Ahrens@Sun.COM 
22169396SMatthew.Ahrens@Sun.COM 	if (error) {
22179396SMatthew.Ahrens@Sun.COM 		dmu_tx_commit(tx);
22189396SMatthew.Ahrens@Sun.COM 		return (error);
22199396SMatthew.Ahrens@Sun.COM 	}
22204577Sahrens 
222111935SMark.Shellenbaum@Sun.COM 	if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
222211935SMark.Shellenbaum@Sun.COM 		uint64_t sa_obj;
222311935SMark.Shellenbaum@Sun.COM 
222411935SMark.Shellenbaum@Sun.COM 		ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
222511935SMark.Shellenbaum@Sun.COM 		    SPA_VERSION_SA);
222611935SMark.Shellenbaum@Sun.COM 		sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
222711935SMark.Shellenbaum@Sun.COM 		    DMU_OT_NONE, 0, tx);
222811935SMark.Shellenbaum@Sun.COM 
222911935SMark.Shellenbaum@Sun.COM 		error = zap_add(os, MASTER_NODE_OBJ,
223011935SMark.Shellenbaum@Sun.COM 		    ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
223111935SMark.Shellenbaum@Sun.COM 		ASSERT3U(error, ==, 0);
223211935SMark.Shellenbaum@Sun.COM 
223311935SMark.Shellenbaum@Sun.COM 		VERIFY(0 == sa_set_sa_object(os, sa_obj));
223411935SMark.Shellenbaum@Sun.COM 		sa_register_update_callback(os, zfs_sa_upgrade);
223511935SMark.Shellenbaum@Sun.COM 	}
223611935SMark.Shellenbaum@Sun.COM 
22374577Sahrens 	spa_history_internal_log(LOG_DS_UPGRADE,
22384577Sahrens 	    dmu_objset_spa(os), tx, CRED(),
22399396SMatthew.Ahrens@Sun.COM 	    "oldver=%llu newver=%llu dataset = %llu",
22409396SMatthew.Ahrens@Sun.COM 	    zfsvfs->z_version, newvers, dmu_objset_id(os));
22419396SMatthew.Ahrens@Sun.COM 
22424577Sahrens 	dmu_tx_commit(tx);
22434577Sahrens 
22449396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_version = newvers;
22459396SMatthew.Ahrens@Sun.COM 
22469396SMatthew.Ahrens@Sun.COM 	if (zfsvfs->z_version >= ZPL_VERSION_FUID)
22479396SMatthew.Ahrens@Sun.COM 		zfs_set_fuid_feature(zfsvfs);
22489396SMatthew.Ahrens@Sun.COM 
22499396SMatthew.Ahrens@Sun.COM 	return (0);
22504577Sahrens }
22514577Sahrens 
22525498Stimh /*
22535498Stimh  * Read a property stored within the master node.
22545498Stimh  */
22555498Stimh int
22565498Stimh zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
22575498Stimh {
22585498Stimh 	const char *pname;
22597184Stimh 	int error = ENOENT;
22605498Stimh 
22615498Stimh 	/*
22625498Stimh 	 * Look up the file system's value for the property.  For the
22635498Stimh 	 * version property, we look up a slightly different string.
22645498Stimh 	 */
22655498Stimh 	if (prop == ZFS_PROP_VERSION)
22665498Stimh 		pname = ZPL_VERSION_STR;
22675498Stimh 	else
22685498Stimh 		pname = zfs_prop_to_name(prop);
22695498Stimh 
22707184Stimh 	if (os != NULL)
22717184Stimh 		error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
22725498Stimh 
22736404Smaybee 	if (error == ENOENT) {
22745498Stimh 		/* No value set, use the default value */
22755498Stimh 		switch (prop) {
22766404Smaybee 		case ZFS_PROP_VERSION:
22776404Smaybee 			*value = ZPL_VERSION;
22786404Smaybee 			break;
22795498Stimh 		case ZFS_PROP_NORMALIZE:
22805498Stimh 		case ZFS_PROP_UTF8ONLY:
22815498Stimh 			*value = 0;
22825498Stimh 			break;
22835498Stimh 		case ZFS_PROP_CASE:
22845498Stimh 			*value = ZFS_CASE_SENSITIVE;
22855498Stimh 			break;
22865498Stimh 		default:
22876404Smaybee 			return (error);
22885498Stimh 		}
22896404Smaybee 		error = 0;
22905498Stimh 	}
22916404Smaybee 	return (error);
22925498Stimh }
22935498Stimh 
2294789Sahrens static vfsdef_t vfw = {
2295789Sahrens 	VFSDEF_VERSION,
2296789Sahrens 	MNTTYPE_ZFS,
2297789Sahrens 	zfs_vfsinit,
22985331Samw 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS|
22995331Samw 	    VSW_XID,
2300789Sahrens 	&zfs_mntopts
2301789Sahrens };
2302789Sahrens 
2303789Sahrens struct modlfs zfs_modlfs = {
23044577Sahrens 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
2305789Sahrens };
2306