xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 13147:5f70b43d6386)
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 /*
2212070SMark.Shellenbaum@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens  */
24789Sahrens 
2512294SMark.Musante@Sun.COM /* Portions Copyright 2010 Robert Milkowski */
2612294SMark.Musante@Sun.COM 
27789Sahrens #include <sys/types.h>
28789Sahrens #include <sys/param.h>
29789Sahrens #include <sys/systm.h>
30789Sahrens #include <sys/sysmacros.h>
31789Sahrens #include <sys/kmem.h>
32789Sahrens #include <sys/pathname.h>
33789Sahrens #include <sys/vnode.h>
34789Sahrens #include <sys/vfs.h>
353898Srsb #include <sys/vfs_opreg.h>
36789Sahrens #include <sys/mntent.h>
37789Sahrens #include <sys/mount.h>
38789Sahrens #include <sys/cmn_err.h>
39789Sahrens #include "fs/fs_subr.h"
40789Sahrens #include <sys/zfs_znode.h>
413461Sahrens #include <sys/zfs_dir.h>
42789Sahrens #include <sys/zil.h>
43789Sahrens #include <sys/fs/zfs.h>
44789Sahrens #include <sys/dmu.h>
45789Sahrens #include <sys/dsl_prop.h>
463912Slling #include <sys/dsl_dataset.h>
474543Smarks #include <sys/dsl_deleg.h>
48789Sahrens #include <sys/spa.h>
49789Sahrens #include <sys/zap.h>
5011935SMark.Shellenbaum@Sun.COM #include <sys/sa.h>
51789Sahrens #include <sys/varargs.h>
52789Sahrens #include <sys/policy.h>
53789Sahrens #include <sys/atomic.h>
54789Sahrens #include <sys/mkdev.h>
55789Sahrens #include <sys/modctl.h>
564543Smarks #include <sys/refstr.h>
57789Sahrens #include <sys/zfs_ioctl.h>
58789Sahrens #include <sys/zfs_ctldir.h>
595331Samw #include <sys/zfs_fuid.h>
601544Seschrock #include <sys/bootconf.h>
61849Sbonwick #include <sys/sunddi.h>
621484Sek110237 #include <sys/dnlc.h>
635326Sek110237 #include <sys/dmu_objset.h>
646423Sgw25295 #include <sys/spa_boot.h>
6511935SMark.Shellenbaum@Sun.COM #include <sys/sa.h>
6611935SMark.Shellenbaum@Sun.COM #include "zfs_comutil.h"
67789Sahrens 
68789Sahrens int zfsfstype;
69789Sahrens vfsops_t *zfs_vfsops = NULL;
70849Sbonwick static major_t zfs_major;
71789Sahrens static minor_t zfs_minor;
72789Sahrens static kmutex_t	zfs_dev_mtx;
73789Sahrens 
749234SGeorge.Wilson@Sun.COM extern int sys_shutdown;
759234SGeorge.Wilson@Sun.COM 
76789Sahrens static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
77789Sahrens static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr);
781544Seschrock static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
79789Sahrens static int zfs_root(vfs_t *vfsp, vnode_t **vpp);
80789Sahrens static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp);
81789Sahrens static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp);
82789Sahrens static void zfs_freevfs(vfs_t *vfsp);
83789Sahrens 
84789Sahrens static const fs_operation_def_t zfs_vfsops_template[] = {
853898Srsb 	VFSNAME_MOUNT,		{ .vfs_mount = zfs_mount },
863898Srsb 	VFSNAME_MOUNTROOT,	{ .vfs_mountroot = zfs_mountroot },
873898Srsb 	VFSNAME_UNMOUNT,	{ .vfs_unmount = zfs_umount },
883898Srsb 	VFSNAME_ROOT,		{ .vfs_root = zfs_root },
893898Srsb 	VFSNAME_STATVFS,	{ .vfs_statvfs = zfs_statvfs },
903898Srsb 	VFSNAME_SYNC,		{ .vfs_sync = zfs_sync },
913898Srsb 	VFSNAME_VGET,		{ .vfs_vget = zfs_vget },
923898Srsb 	VFSNAME_FREEVFS,	{ .vfs_freevfs = zfs_freevfs },
933898Srsb 	NULL,			NULL
94789Sahrens };
95789Sahrens 
96789Sahrens static const fs_operation_def_t zfs_vfsops_eio_template[] = {
973898Srsb 	VFSNAME_FREEVFS,	{ .vfs_freevfs =  zfs_freevfs },
983898Srsb 	NULL,			NULL
99789Sahrens };
100789Sahrens 
101789Sahrens /*
102789Sahrens  * We need to keep a count of active fs's.
103789Sahrens  * This is necessary to prevent our module
104789Sahrens  * from being unloaded after a umount -f
105789Sahrens  */
106789Sahrens static uint32_t	zfs_active_fs_count = 0;
107789Sahrens 
108789Sahrens static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
109789Sahrens static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
1103234Sck153898 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
1113234Sck153898 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
112789Sahrens 
1133234Sck153898 /*
1144596Slling  * MO_DEFAULT is not used since the default value is determined
1154596Slling  * by the equivalent property.
1163234Sck153898  */
117789Sahrens static mntopt_t mntopts[] = {
1183234Sck153898 	{ MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL },
1193234Sck153898 	{ MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL },
1204596Slling 	{ MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL },
121789Sahrens 	{ MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
122789Sahrens };
123789Sahrens 
124789Sahrens static mntopts_t zfs_mntopts = {
125789Sahrens 	sizeof (mntopts) / sizeof (mntopt_t),
126789Sahrens 	mntopts
127789Sahrens };
128789Sahrens 
129789Sahrens /*ARGSUSED*/
130789Sahrens int
zfs_sync(vfs_t * vfsp,short flag,cred_t * cr)131789Sahrens zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
132789Sahrens {
133789Sahrens 	/*
134789Sahrens 	 * Data integrity is job one.  We don't want a compromised kernel
135789Sahrens 	 * writing to the storage pool, so we never sync during panic.
136789Sahrens 	 */
137789Sahrens 	if (panicstr)
138789Sahrens 		return (0);
139789Sahrens 
140789Sahrens 	/*
141789Sahrens 	 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
142789Sahrens 	 * to sync metadata, which they would otherwise cache indefinitely.
143789Sahrens 	 * Semantically, the only requirement is that the sync be initiated.
144789Sahrens 	 * The DMU syncs out txgs frequently, so there's nothing to do.
145789Sahrens 	 */
146789Sahrens 	if (flag & SYNC_ATTR)
147789Sahrens 		return (0);
148789Sahrens 
149789Sahrens 	if (vfsp != NULL) {
150789Sahrens 		/*
151789Sahrens 		 * Sync a specific filesystem.
152789Sahrens 		 */
153789Sahrens 		zfsvfs_t *zfsvfs = vfsp->vfs_data;
1549234SGeorge.Wilson@Sun.COM 		dsl_pool_t *dp;
155789Sahrens 
156789Sahrens 		ZFS_ENTER(zfsvfs);
1579234SGeorge.Wilson@Sun.COM 		dp = dmu_objset_pool(zfsvfs->z_os);
1589234SGeorge.Wilson@Sun.COM 
1599234SGeorge.Wilson@Sun.COM 		/*
1609234SGeorge.Wilson@Sun.COM 		 * If the system is shutting down, then skip any
1619234SGeorge.Wilson@Sun.COM 		 * filesystems which may exist on a suspended pool.
1629234SGeorge.Wilson@Sun.COM 		 */
1639234SGeorge.Wilson@Sun.COM 		if (sys_shutdown && spa_suspended(dp->dp_spa)) {
1649234SGeorge.Wilson@Sun.COM 			ZFS_EXIT(zfsvfs);
1659234SGeorge.Wilson@Sun.COM 			return (0);
1669234SGeorge.Wilson@Sun.COM 		}
1679234SGeorge.Wilson@Sun.COM 
168789Sahrens 		if (zfsvfs->z_log != NULL)
16912699SNeil.Perrin@Sun.COM 			zil_commit(zfsvfs->z_log, 0);
17012294SMark.Musante@Sun.COM 
171789Sahrens 		ZFS_EXIT(zfsvfs);
172789Sahrens 	} else {
173789Sahrens 		/*
174789Sahrens 		 * Sync all ZFS filesystems.  This is what happens when you
175789Sahrens 		 * run sync(1M).  Unlike other filesystems, ZFS honors the
176789Sahrens 		 * request by waiting for all pools to commit all dirty data.
177789Sahrens 		 */
178789Sahrens 		spa_sync_allpools();
179789Sahrens 	}
180789Sahrens 
181789Sahrens 	return (0);
182789Sahrens }
183789Sahrens 
1841544Seschrock static int
zfs_create_unique_device(dev_t * dev)1851544Seschrock zfs_create_unique_device(dev_t *dev)
1861544Seschrock {
1871544Seschrock 	major_t new_major;
1881544Seschrock 
1891544Seschrock 	do {
1901544Seschrock 		ASSERT3U(zfs_minor, <=, MAXMIN32);
1911544Seschrock 		minor_t start = zfs_minor;
1921544Seschrock 		do {
1931544Seschrock 			mutex_enter(&zfs_dev_mtx);
1941544Seschrock 			if (zfs_minor >= MAXMIN32) {
1951544Seschrock 				/*
1961544Seschrock 				 * If we're still using the real major
1971544Seschrock 				 * keep out of /dev/zfs and /dev/zvol minor
1981544Seschrock 				 * number space.  If we're using a getudev()'ed
1991544Seschrock 				 * major number, we can use all of its minors.
2001544Seschrock 				 */
2011544Seschrock 				if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
2021544Seschrock 					zfs_minor = ZFS_MIN_MINOR;
2031544Seschrock 				else
2041544Seschrock 					zfs_minor = 0;
2051544Seschrock 			} else {
2061544Seschrock 				zfs_minor++;
2071544Seschrock 			}
2081544Seschrock 			*dev = makedevice(zfs_major, zfs_minor);
2091544Seschrock 			mutex_exit(&zfs_dev_mtx);
2101544Seschrock 		} while (vfs_devismounted(*dev) && zfs_minor != start);
2111544Seschrock 		if (zfs_minor == start) {
2121544Seschrock 			/*
2131544Seschrock 			 * We are using all ~262,000 minor numbers for the
2141544Seschrock 			 * current major number.  Create a new major number.
2151544Seschrock 			 */
2161544Seschrock 			if ((new_major = getudev()) == (major_t)-1) {
2171544Seschrock 				cmn_err(CE_WARN,
2181544Seschrock 				    "zfs_mount: Can't get unique major "
2191544Seschrock 				    "device number.");
2201544Seschrock 				return (-1);
2211544Seschrock 			}
2221544Seschrock 			mutex_enter(&zfs_dev_mtx);
2231544Seschrock 			zfs_major = new_major;
2241544Seschrock 			zfs_minor = 0;
2251544Seschrock 
2261544Seschrock 			mutex_exit(&zfs_dev_mtx);
2271544Seschrock 		} else {
2281544Seschrock 			break;
2291544Seschrock 		}
2301544Seschrock 		/* CONSTANTCONDITION */
2311544Seschrock 	} while (1);
2321544Seschrock 
2331544Seschrock 	return (0);
2341544Seschrock }
2351544Seschrock 
236789Sahrens static void
atime_changed_cb(void * arg,uint64_t newval)237789Sahrens atime_changed_cb(void *arg, uint64_t newval)
238789Sahrens {
239789Sahrens 	zfsvfs_t *zfsvfs = arg;
240789Sahrens 
241789Sahrens 	if (newval == TRUE) {
242789Sahrens 		zfsvfs->z_atime = TRUE;
243789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
244789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
245789Sahrens 	} else {
246789Sahrens 		zfsvfs->z_atime = FALSE;
247789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
248789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
249789Sahrens 	}
250789Sahrens }
251789Sahrens 
252789Sahrens static void
xattr_changed_cb(void * arg,uint64_t newval)2533234Sck153898 xattr_changed_cb(void *arg, uint64_t newval)
2543234Sck153898 {
2553234Sck153898 	zfsvfs_t *zfsvfs = arg;
2563234Sck153898 
2573234Sck153898 	if (newval == TRUE) {
2583234Sck153898 		/* XXX locking on vfs_flag? */
2593234Sck153898 		zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
2603234Sck153898 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
2613234Sck153898 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
2623234Sck153898 	} else {
2633234Sck153898 		/* XXX locking on vfs_flag? */
2643234Sck153898 		zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
2653234Sck153898 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
2663234Sck153898 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
2673234Sck153898 	}
2683234Sck153898 }
2693234Sck153898 
2703234Sck153898 static void
blksz_changed_cb(void * arg,uint64_t newval)271789Sahrens blksz_changed_cb(void *arg, uint64_t newval)
272789Sahrens {
273789Sahrens 	zfsvfs_t *zfsvfs = arg;
274789Sahrens 
275789Sahrens 	if (newval < SPA_MINBLOCKSIZE ||
276789Sahrens 	    newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
277789Sahrens 		newval = SPA_MAXBLOCKSIZE;
278789Sahrens 
279789Sahrens 	zfsvfs->z_max_blksz = newval;
280789Sahrens 	zfsvfs->z_vfs->vfs_bsize = newval;
281789Sahrens }
282789Sahrens 
283789Sahrens static void
readonly_changed_cb(void * arg,uint64_t newval)284789Sahrens readonly_changed_cb(void *arg, uint64_t newval)
285789Sahrens {
286789Sahrens 	zfsvfs_t *zfsvfs = arg;
287789Sahrens 
288789Sahrens 	if (newval) {
289789Sahrens 		/* XXX locking on vfs_flag? */
290789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
291789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
292789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
293789Sahrens 	} else {
294789Sahrens 		/* XXX locking on vfs_flag? */
295789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
296789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
297789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
298789Sahrens 	}
299789Sahrens }
300789Sahrens 
301789Sahrens static void
devices_changed_cb(void * arg,uint64_t newval)302789Sahrens devices_changed_cb(void *arg, uint64_t newval)
303789Sahrens {
304789Sahrens 	zfsvfs_t *zfsvfs = arg;
305789Sahrens 
306789Sahrens 	if (newval == FALSE) {
307789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
308789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
309789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
310789Sahrens 	} else {
311789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
312789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
313789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
314789Sahrens 	}
315789Sahrens }
316789Sahrens 
317789Sahrens static void
setuid_changed_cb(void * arg,uint64_t newval)318789Sahrens setuid_changed_cb(void *arg, uint64_t newval)
319789Sahrens {
320789Sahrens 	zfsvfs_t *zfsvfs = arg;
321789Sahrens 
322789Sahrens 	if (newval == FALSE) {
323789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
324789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
325789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
326789Sahrens 	} else {
327789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
328789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
329789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
330789Sahrens 	}
331789Sahrens }
332789Sahrens 
333789Sahrens static void
exec_changed_cb(void * arg,uint64_t newval)334789Sahrens exec_changed_cb(void *arg, uint64_t newval)
335789Sahrens {
336789Sahrens 	zfsvfs_t *zfsvfs = arg;
337789Sahrens 
338789Sahrens 	if (newval == FALSE) {
339789Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
340789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
341789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
342789Sahrens 	} else {
343789Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
344789Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
345789Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
346789Sahrens 	}
347789Sahrens }
348789Sahrens 
3495331Samw /*
3505331Samw  * The nbmand mount option can be changed at mount time.
3515331Samw  * We can't allow it to be toggled on live file systems or incorrect
3525331Samw  * behavior may be seen from cifs clients
3535331Samw  *
3545331Samw  * This property isn't registered via dsl_prop_register(), but this callback
3555331Samw  * will be called when a file system is first mounted
3565331Samw  */
3575331Samw static void
nbmand_changed_cb(void * arg,uint64_t newval)3585331Samw nbmand_changed_cb(void *arg, uint64_t newval)
3595331Samw {
3605331Samw 	zfsvfs_t *zfsvfs = arg;
3615331Samw 	if (newval == FALSE) {
3625331Samw 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
3635331Samw 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
3645331Samw 	} else {
3655331Samw 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
3665331Samw 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
3675331Samw 	}
3685331Samw }
3695331Samw 
370789Sahrens static void
snapdir_changed_cb(void * arg,uint64_t newval)371789Sahrens snapdir_changed_cb(void *arg, uint64_t newval)
372789Sahrens {
373789Sahrens 	zfsvfs_t *zfsvfs = arg;
374789Sahrens 
375789Sahrens 	zfsvfs->z_show_ctldir = newval;
376789Sahrens }
377789Sahrens 
378789Sahrens static void
vscan_changed_cb(void * arg,uint64_t newval)3795331Samw vscan_changed_cb(void *arg, uint64_t newval)
3805331Samw {
3815331Samw 	zfsvfs_t *zfsvfs = arg;
3825331Samw 
3835331Samw 	zfsvfs->z_vscan = newval;
3845331Samw }
3855331Samw 
3865331Samw static void
acl_inherit_changed_cb(void * arg,uint64_t newval)387789Sahrens acl_inherit_changed_cb(void *arg, uint64_t newval)
388789Sahrens {
389789Sahrens 	zfsvfs_t *zfsvfs = arg;
390789Sahrens 
391789Sahrens 	zfsvfs->z_acl_inherit = newval;
392789Sahrens }
393789Sahrens 
3941544Seschrock static int
zfs_register_callbacks(vfs_t * vfsp)3951544Seschrock zfs_register_callbacks(vfs_t *vfsp)
3961544Seschrock {
3971544Seschrock 	struct dsl_dataset *ds = NULL;
3981544Seschrock 	objset_t *os = NULL;
3991544Seschrock 	zfsvfs_t *zfsvfs = NULL;
4005331Samw 	uint64_t nbmand;
4015331Samw 	int readonly, do_readonly = B_FALSE;
4025331Samw 	int setuid, do_setuid = B_FALSE;
4035331Samw 	int exec, do_exec = B_FALSE;
4045331Samw 	int devices, do_devices = B_FALSE;
4055331Samw 	int xattr, do_xattr = B_FALSE;
4065331Samw 	int atime, do_atime = B_FALSE;
4071544Seschrock 	int error = 0;
4081544Seschrock 
4091544Seschrock 	ASSERT(vfsp);
4101544Seschrock 	zfsvfs = vfsp->vfs_data;
4111544Seschrock 	ASSERT(zfsvfs);
4121544Seschrock 	os = zfsvfs->z_os;
4131544Seschrock 
4141544Seschrock 	/*
4151544Seschrock 	 * The act of registering our callbacks will destroy any mount
4161544Seschrock 	 * options we may have.  In order to enable temporary overrides
4173234Sck153898 	 * of mount options, we stash away the current values and
4181544Seschrock 	 * restore them after we register the callbacks.
4191544Seschrock 	 */
42013049SGeorge.Wilson@Sun.COM 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL) ||
42113049SGeorge.Wilson@Sun.COM 	    !spa_writeable(dmu_objset_spa(os))) {
4221544Seschrock 		readonly = B_TRUE;
4231544Seschrock 		do_readonly = B_TRUE;
4241544Seschrock 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
4251544Seschrock 		readonly = B_FALSE;
4261544Seschrock 		do_readonly = B_TRUE;
4271544Seschrock 	}
4281544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
4291544Seschrock 		devices = B_FALSE;
4301544Seschrock 		setuid = B_FALSE;
4311544Seschrock 		do_devices = B_TRUE;
4321544Seschrock 		do_setuid = B_TRUE;
4331544Seschrock 	} else {
4341544Seschrock 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
4351544Seschrock 			devices = B_FALSE;
4361544Seschrock 			do_devices = B_TRUE;
4373912Slling 		} else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
4381544Seschrock 			devices = B_TRUE;
4391544Seschrock 			do_devices = B_TRUE;
4401544Seschrock 		}
4411544Seschrock 
4421544Seschrock 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
4431544Seschrock 			setuid = B_FALSE;
4441544Seschrock 			do_setuid = B_TRUE;
4451544Seschrock 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
4461544Seschrock 			setuid = B_TRUE;
4471544Seschrock 			do_setuid = B_TRUE;
4481544Seschrock 		}
4491544Seschrock 	}
4501544Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
4511544Seschrock 		exec = B_FALSE;
4521544Seschrock 		do_exec = B_TRUE;
4531544Seschrock 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
4541544Seschrock 		exec = B_TRUE;
4551544Seschrock 		do_exec = B_TRUE;
4561544Seschrock 	}
4573234Sck153898 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
4583234Sck153898 		xattr = B_FALSE;
4593234Sck153898 		do_xattr = B_TRUE;
4603234Sck153898 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
4613234Sck153898 		xattr = B_TRUE;
4623234Sck153898 		do_xattr = B_TRUE;
4633234Sck153898 	}
4644596Slling 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
4654596Slling 		atime = B_FALSE;
4664596Slling 		do_atime = B_TRUE;
4674596Slling 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
4684596Slling 		atime = B_TRUE;
4694596Slling 		do_atime = B_TRUE;
4704596Slling 	}
4711544Seschrock 
4721544Seschrock 	/*
4735331Samw 	 * nbmand is a special property.  It can only be changed at
4745331Samw 	 * mount time.
4755331Samw 	 *
4765331Samw 	 * This is weird, but it is documented to only be changeable
4775331Samw 	 * at mount time.
4785331Samw 	 */
4795331Samw 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
4805331Samw 		nbmand = B_FALSE;
4815331Samw 	} else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
4825331Samw 		nbmand = B_TRUE;
4835331Samw 	} else {
4845331Samw 		char osname[MAXNAMELEN];
4855331Samw 
4865331Samw 		dmu_objset_name(os, osname);
4875331Samw 		if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
4887265Sahrens 		    NULL)) {
4897265Sahrens 			return (error);
4907265Sahrens 		}
4915331Samw 	}
4925331Samw 
4935331Samw 	/*
4941544Seschrock 	 * Register property callbacks.
4951544Seschrock 	 *
4961544Seschrock 	 * It would probably be fine to just check for i/o error from
4971544Seschrock 	 * the first prop_register(), but I guess I like to go
4981544Seschrock 	 * overboard...
4991544Seschrock 	 */
5001544Seschrock 	ds = dmu_objset_ds(os);
5011544Seschrock 	error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
5021544Seschrock 	error = error ? error : dsl_prop_register(ds,
5033234Sck153898 	    "xattr", xattr_changed_cb, zfsvfs);
5043234Sck153898 	error = error ? error : dsl_prop_register(ds,
5051544Seschrock 	    "recordsize", blksz_changed_cb, zfsvfs);
5061544Seschrock 	error = error ? error : dsl_prop_register(ds,
5071544Seschrock 	    "readonly", readonly_changed_cb, zfsvfs);
5081544Seschrock 	error = error ? error : dsl_prop_register(ds,
5091544Seschrock 	    "devices", devices_changed_cb, zfsvfs);
5101544Seschrock 	error = error ? error : dsl_prop_register(ds,
5111544Seschrock 	    "setuid", setuid_changed_cb, zfsvfs);
5121544Seschrock 	error = error ? error : dsl_prop_register(ds,
5131544Seschrock 	    "exec", exec_changed_cb, zfsvfs);
5141544Seschrock 	error = error ? error : dsl_prop_register(ds,
5151544Seschrock 	    "snapdir", snapdir_changed_cb, zfsvfs);
5161544Seschrock 	error = error ? error : dsl_prop_register(ds,
5171544Seschrock 	    "aclinherit", acl_inherit_changed_cb, zfsvfs);
5185331Samw 	error = error ? error : dsl_prop_register(ds,
5195331Samw 	    "vscan", vscan_changed_cb, zfsvfs);
5201544Seschrock 	if (error)
5211544Seschrock 		goto unregister;
5221544Seschrock 
5231544Seschrock 	/*
5241544Seschrock 	 * Invoke our callbacks to restore temporary mount options.
5251544Seschrock 	 */
5261544Seschrock 	if (do_readonly)
5271544Seschrock 		readonly_changed_cb(zfsvfs, readonly);
5281544Seschrock 	if (do_setuid)
5291544Seschrock 		setuid_changed_cb(zfsvfs, setuid);
5301544Seschrock 	if (do_exec)
5311544Seschrock 		exec_changed_cb(zfsvfs, exec);
5321544Seschrock 	if (do_devices)
5331544Seschrock 		devices_changed_cb(zfsvfs, devices);
5343234Sck153898 	if (do_xattr)
5353234Sck153898 		xattr_changed_cb(zfsvfs, xattr);
5364596Slling 	if (do_atime)
5374596Slling 		atime_changed_cb(zfsvfs, atime);
5381544Seschrock 
5395331Samw 	nbmand_changed_cb(zfsvfs, nbmand);
5405331Samw 
5411544Seschrock 	return (0);
5421544Seschrock 
5431544Seschrock unregister:
5441544Seschrock 	/*
5451544Seschrock 	 * We may attempt to unregister some callbacks that are not
5461544Seschrock 	 * registered, but this is OK; it will simply return ENOMSG,
5471544Seschrock 	 * which we will ignore.
5481544Seschrock 	 */
5491544Seschrock 	(void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
5503234Sck153898 	(void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
5511544Seschrock 	(void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
5521544Seschrock 	(void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
5531544Seschrock 	(void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs);
5541544Seschrock 	(void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
5551544Seschrock 	(void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
5561544Seschrock 	(void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
5571544Seschrock 	(void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
5581544Seschrock 	    zfsvfs);
5595331Samw 	(void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
5601544Seschrock 	return (error);
5611544Seschrock 
5621544Seschrock }
5631544Seschrock 
56410407SMatthew.Ahrens@Sun.COM static int
zfs_space_delta_cb(dmu_object_type_t bonustype,void * data,uint64_t * userp,uint64_t * groupp)56511935SMark.Shellenbaum@Sun.COM zfs_space_delta_cb(dmu_object_type_t bonustype, void *data,
56610407SMatthew.Ahrens@Sun.COM     uint64_t *userp, uint64_t *groupp)
5679396SMatthew.Ahrens@Sun.COM {
56811935SMark.Shellenbaum@Sun.COM 	znode_phys_t *znp = data;
56911935SMark.Shellenbaum@Sun.COM 	int error = 0;
5709396SMatthew.Ahrens@Sun.COM 
57112178SMark.Shellenbaum@Sun.COM 	/*
57212178SMark.Shellenbaum@Sun.COM 	 * Is it a valid type of object to track?
57312178SMark.Shellenbaum@Sun.COM 	 */
57411935SMark.Shellenbaum@Sun.COM 	if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
57510407SMatthew.Ahrens@Sun.COM 		return (ENOENT);
5769396SMatthew.Ahrens@Sun.COM 
57712178SMark.Shellenbaum@Sun.COM 	/*
57812178SMark.Shellenbaum@Sun.COM 	 * If we have a NULL data pointer
57912178SMark.Shellenbaum@Sun.COM 	 * then assume the id's aren't changing and
58012178SMark.Shellenbaum@Sun.COM 	 * return EEXIST to the dmu to let it know to
58112178SMark.Shellenbaum@Sun.COM 	 * use the same ids
58212178SMark.Shellenbaum@Sun.COM 	 */
58312178SMark.Shellenbaum@Sun.COM 	if (data == NULL)
58412178SMark.Shellenbaum@Sun.COM 		return (EEXIST);
58512178SMark.Shellenbaum@Sun.COM 
58611935SMark.Shellenbaum@Sun.COM 	if (bonustype == DMU_OT_ZNODE) {
58711935SMark.Shellenbaum@Sun.COM 		*userp = znp->zp_uid;
58811935SMark.Shellenbaum@Sun.COM 		*groupp = znp->zp_gid;
58911935SMark.Shellenbaum@Sun.COM 	} else {
59011935SMark.Shellenbaum@Sun.COM 		int hdrsize;
59111935SMark.Shellenbaum@Sun.COM 
59211935SMark.Shellenbaum@Sun.COM 		ASSERT(bonustype == DMU_OT_SA);
59311935SMark.Shellenbaum@Sun.COM 		hdrsize = sa_hdrsize(data);
59411935SMark.Shellenbaum@Sun.COM 
59511935SMark.Shellenbaum@Sun.COM 		if (hdrsize != 0) {
59611935SMark.Shellenbaum@Sun.COM 			*userp = *((uint64_t *)((uintptr_t)data + hdrsize +
59711935SMark.Shellenbaum@Sun.COM 			    SA_UID_OFFSET));
59811935SMark.Shellenbaum@Sun.COM 			*groupp = *((uint64_t *)((uintptr_t)data + hdrsize +
59911935SMark.Shellenbaum@Sun.COM 			    SA_GID_OFFSET));
60011935SMark.Shellenbaum@Sun.COM 		} else {
60112178SMark.Shellenbaum@Sun.COM 			/*
60212178SMark.Shellenbaum@Sun.COM 			 * This should only happen for newly created
60312178SMark.Shellenbaum@Sun.COM 			 * files that haven't had the znode data filled
60412178SMark.Shellenbaum@Sun.COM 			 * in yet.
60512178SMark.Shellenbaum@Sun.COM 			 */
60612178SMark.Shellenbaum@Sun.COM 			*userp = 0;
60712178SMark.Shellenbaum@Sun.COM 			*groupp = 0;
60811935SMark.Shellenbaum@Sun.COM 		}
60911935SMark.Shellenbaum@Sun.COM 	}
61011935SMark.Shellenbaum@Sun.COM 	return (error);
6119396SMatthew.Ahrens@Sun.COM }
6129396SMatthew.Ahrens@Sun.COM 
6139396SMatthew.Ahrens@Sun.COM static void
fuidstr_to_sid(zfsvfs_t * zfsvfs,const char * fuidstr,char * domainbuf,int buflen,uid_t * ridp)6149396SMatthew.Ahrens@Sun.COM fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr,
6159396SMatthew.Ahrens@Sun.COM     char *domainbuf, int buflen, uid_t *ridp)
6169396SMatthew.Ahrens@Sun.COM {
6179396SMatthew.Ahrens@Sun.COM 	uint64_t fuid;
6189396SMatthew.Ahrens@Sun.COM 	const char *domain;
6199396SMatthew.Ahrens@Sun.COM 
6209396SMatthew.Ahrens@Sun.COM 	fuid = strtonum(fuidstr, NULL);
6219396SMatthew.Ahrens@Sun.COM 
6229396SMatthew.Ahrens@Sun.COM 	domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid));
6239396SMatthew.Ahrens@Sun.COM 	if (domain)
6249396SMatthew.Ahrens@Sun.COM 		(void) strlcpy(domainbuf, domain, buflen);
6259396SMatthew.Ahrens@Sun.COM 	else
6269396SMatthew.Ahrens@Sun.COM 		domainbuf[0] = '\0';
6279396SMatthew.Ahrens@Sun.COM 	*ridp = FUID_RID(fuid);
6289396SMatthew.Ahrens@Sun.COM }
6299396SMatthew.Ahrens@Sun.COM 
6309396SMatthew.Ahrens@Sun.COM static uint64_t
zfs_userquota_prop_to_obj(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type)6319396SMatthew.Ahrens@Sun.COM zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type)
6329396SMatthew.Ahrens@Sun.COM {
6339396SMatthew.Ahrens@Sun.COM 	switch (type) {
6349396SMatthew.Ahrens@Sun.COM 	case ZFS_PROP_USERUSED:
6359396SMatthew.Ahrens@Sun.COM 		return (DMU_USERUSED_OBJECT);
6369396SMatthew.Ahrens@Sun.COM 	case ZFS_PROP_GROUPUSED:
6379396SMatthew.Ahrens@Sun.COM 		return (DMU_GROUPUSED_OBJECT);
6389396SMatthew.Ahrens@Sun.COM 	case ZFS_PROP_USERQUOTA:
6399396SMatthew.Ahrens@Sun.COM 		return (zfsvfs->z_userquota_obj);
6409396SMatthew.Ahrens@Sun.COM 	case ZFS_PROP_GROUPQUOTA:
6419396SMatthew.Ahrens@Sun.COM 		return (zfsvfs->z_groupquota_obj);
6429396SMatthew.Ahrens@Sun.COM 	}
6439396SMatthew.Ahrens@Sun.COM 	return (0);
6449396SMatthew.Ahrens@Sun.COM }
6459396SMatthew.Ahrens@Sun.COM 
6469396SMatthew.Ahrens@Sun.COM int
zfs_userspace_many(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type,uint64_t * cookiep,void * vbuf,uint64_t * bufsizep)6479396SMatthew.Ahrens@Sun.COM zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
6489396SMatthew.Ahrens@Sun.COM     uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
6499396SMatthew.Ahrens@Sun.COM {
6509396SMatthew.Ahrens@Sun.COM 	int error;
6519396SMatthew.Ahrens@Sun.COM 	zap_cursor_t zc;
6529396SMatthew.Ahrens@Sun.COM 	zap_attribute_t za;
6539396SMatthew.Ahrens@Sun.COM 	zfs_useracct_t *buf = vbuf;
6549396SMatthew.Ahrens@Sun.COM 	uint64_t obj;
6559396SMatthew.Ahrens@Sun.COM 
6569396SMatthew.Ahrens@Sun.COM 	if (!dmu_objset_userspace_present(zfsvfs->z_os))
6579396SMatthew.Ahrens@Sun.COM 		return (ENOTSUP);
6589396SMatthew.Ahrens@Sun.COM 
6599396SMatthew.Ahrens@Sun.COM 	obj = zfs_userquota_prop_to_obj(zfsvfs, type);
6609396SMatthew.Ahrens@Sun.COM 	if (obj == 0) {
6619396SMatthew.Ahrens@Sun.COM 		*bufsizep = 0;
6629396SMatthew.Ahrens@Sun.COM 		return (0);
6639396SMatthew.Ahrens@Sun.COM 	}
6649396SMatthew.Ahrens@Sun.COM 
6659396SMatthew.Ahrens@Sun.COM 	for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
6669396SMatthew.Ahrens@Sun.COM 	    (error = zap_cursor_retrieve(&zc, &za)) == 0;
6679396SMatthew.Ahrens@Sun.COM 	    zap_cursor_advance(&zc)) {
6689396SMatthew.Ahrens@Sun.COM 		if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
6699396SMatthew.Ahrens@Sun.COM 		    *bufsizep)
6709396SMatthew.Ahrens@Sun.COM 			break;
6719396SMatthew.Ahrens@Sun.COM 
6729396SMatthew.Ahrens@Sun.COM 		fuidstr_to_sid(zfsvfs, za.za_name,
6739396SMatthew.Ahrens@Sun.COM 		    buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
6749396SMatthew.Ahrens@Sun.COM 
6759396SMatthew.Ahrens@Sun.COM 		buf->zu_space = za.za_first_integer;
6769396SMatthew.Ahrens@Sun.COM 		buf++;
6779396SMatthew.Ahrens@Sun.COM 	}
6789396SMatthew.Ahrens@Sun.COM 	if (error == ENOENT)
6799396SMatthew.Ahrens@Sun.COM 		error = 0;
6809396SMatthew.Ahrens@Sun.COM 
6819396SMatthew.Ahrens@Sun.COM 	ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
6829396SMatthew.Ahrens@Sun.COM 	*bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
6839396SMatthew.Ahrens@Sun.COM 	*cookiep = zap_cursor_serialize(&zc);
6849396SMatthew.Ahrens@Sun.COM 	zap_cursor_fini(&zc);
6859396SMatthew.Ahrens@Sun.COM 	return (error);
6869396SMatthew.Ahrens@Sun.COM }
6879396SMatthew.Ahrens@Sun.COM 
6889396SMatthew.Ahrens@Sun.COM /*
6899396SMatthew.Ahrens@Sun.COM  * buf must be big enough (eg, 32 bytes)
6909396SMatthew.Ahrens@Sun.COM  */
6919396SMatthew.Ahrens@Sun.COM static int
id_to_fuidstr(zfsvfs_t * zfsvfs,const char * domain,uid_t rid,char * buf,boolean_t addok)6929396SMatthew.Ahrens@Sun.COM id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
6939396SMatthew.Ahrens@Sun.COM     char *buf, boolean_t addok)
6949396SMatthew.Ahrens@Sun.COM {
6959396SMatthew.Ahrens@Sun.COM 	uint64_t fuid;
6969396SMatthew.Ahrens@Sun.COM 	int domainid = 0;
6979396SMatthew.Ahrens@Sun.COM 
6989396SMatthew.Ahrens@Sun.COM 	if (domain && domain[0]) {
6999396SMatthew.Ahrens@Sun.COM 		domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
7009396SMatthew.Ahrens@Sun.COM 		if (domainid == -1)
7019396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
7029396SMatthew.Ahrens@Sun.COM 	}
7039396SMatthew.Ahrens@Sun.COM 	fuid = FUID_ENCODE(domainid, rid);
7049396SMatthew.Ahrens@Sun.COM 	(void) sprintf(buf, "%llx", (longlong_t)fuid);
7059396SMatthew.Ahrens@Sun.COM 	return (0);
7069396SMatthew.Ahrens@Sun.COM }
7079396SMatthew.Ahrens@Sun.COM 
7089396SMatthew.Ahrens@Sun.COM int
zfs_userspace_one(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type,const char * domain,uint64_t rid,uint64_t * valp)7099396SMatthew.Ahrens@Sun.COM zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
7109396SMatthew.Ahrens@Sun.COM     const char *domain, uint64_t rid, uint64_t *valp)
7119396SMatthew.Ahrens@Sun.COM {
7129396SMatthew.Ahrens@Sun.COM 	char buf[32];
7139396SMatthew.Ahrens@Sun.COM 	int err;
7149396SMatthew.Ahrens@Sun.COM 	uint64_t obj;
7159396SMatthew.Ahrens@Sun.COM 
7169396SMatthew.Ahrens@Sun.COM 	*valp = 0;
7179396SMatthew.Ahrens@Sun.COM 
7189396SMatthew.Ahrens@Sun.COM 	if (!dmu_objset_userspace_present(zfsvfs->z_os))
7199396SMatthew.Ahrens@Sun.COM 		return (ENOTSUP);
7209396SMatthew.Ahrens@Sun.COM 
7219396SMatthew.Ahrens@Sun.COM 	obj = zfs_userquota_prop_to_obj(zfsvfs, type);
7229396SMatthew.Ahrens@Sun.COM 	if (obj == 0)
7239396SMatthew.Ahrens@Sun.COM 		return (0);
7249396SMatthew.Ahrens@Sun.COM 
7259396SMatthew.Ahrens@Sun.COM 	err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_FALSE);
7269396SMatthew.Ahrens@Sun.COM 	if (err)
7279396SMatthew.Ahrens@Sun.COM 		return (err);
7289396SMatthew.Ahrens@Sun.COM 
7299396SMatthew.Ahrens@Sun.COM 	err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
7309396SMatthew.Ahrens@Sun.COM 	if (err == ENOENT)
7319396SMatthew.Ahrens@Sun.COM 		err = 0;
7329396SMatthew.Ahrens@Sun.COM 	return (err);
7339396SMatthew.Ahrens@Sun.COM }
7349396SMatthew.Ahrens@Sun.COM 
7359396SMatthew.Ahrens@Sun.COM int
zfs_set_userquota(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type,const char * domain,uint64_t rid,uint64_t quota)7369396SMatthew.Ahrens@Sun.COM zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
7379396SMatthew.Ahrens@Sun.COM     const char *domain, uint64_t rid, uint64_t quota)
7389396SMatthew.Ahrens@Sun.COM {
7399396SMatthew.Ahrens@Sun.COM 	char buf[32];
7409396SMatthew.Ahrens@Sun.COM 	int err;
7419396SMatthew.Ahrens@Sun.COM 	dmu_tx_t *tx;
7429396SMatthew.Ahrens@Sun.COM 	uint64_t *objp;
7439396SMatthew.Ahrens@Sun.COM 	boolean_t fuid_dirtied;
7449396SMatthew.Ahrens@Sun.COM 
7459396SMatthew.Ahrens@Sun.COM 	if (type != ZFS_PROP_USERQUOTA && type != ZFS_PROP_GROUPQUOTA)
7469396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
7479396SMatthew.Ahrens@Sun.COM 
7489396SMatthew.Ahrens@Sun.COM 	if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
7499396SMatthew.Ahrens@Sun.COM 		return (ENOTSUP);
7509396SMatthew.Ahrens@Sun.COM 
7519396SMatthew.Ahrens@Sun.COM 	objp = (type == ZFS_PROP_USERQUOTA) ? &zfsvfs->z_userquota_obj :
7529396SMatthew.Ahrens@Sun.COM 	    &zfsvfs->z_groupquota_obj;
7539396SMatthew.Ahrens@Sun.COM 
7549396SMatthew.Ahrens@Sun.COM 	err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE);
7559396SMatthew.Ahrens@Sun.COM 	if (err)
7569396SMatthew.Ahrens@Sun.COM 		return (err);
7579396SMatthew.Ahrens@Sun.COM 	fuid_dirtied = zfsvfs->z_fuid_dirty;
7589396SMatthew.Ahrens@Sun.COM 
7599396SMatthew.Ahrens@Sun.COM 	tx = dmu_tx_create(zfsvfs->z_os);
7609396SMatthew.Ahrens@Sun.COM 	dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
7619396SMatthew.Ahrens@Sun.COM 	if (*objp == 0) {
7629396SMatthew.Ahrens@Sun.COM 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
7639396SMatthew.Ahrens@Sun.COM 		    zfs_userquota_prop_prefixes[type]);
7649396SMatthew.Ahrens@Sun.COM 	}
7659396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
7669396SMatthew.Ahrens@Sun.COM 		zfs_fuid_txhold(zfsvfs, tx);
7679396SMatthew.Ahrens@Sun.COM 	err = dmu_tx_assign(tx, TXG_WAIT);
7689396SMatthew.Ahrens@Sun.COM 	if (err) {
7699396SMatthew.Ahrens@Sun.COM 		dmu_tx_abort(tx);
7709396SMatthew.Ahrens@Sun.COM 		return (err);
7719396SMatthew.Ahrens@Sun.COM 	}
7729396SMatthew.Ahrens@Sun.COM 
7739396SMatthew.Ahrens@Sun.COM 	mutex_enter(&zfsvfs->z_lock);
7749396SMatthew.Ahrens@Sun.COM 	if (*objp == 0) {
7759396SMatthew.Ahrens@Sun.COM 		*objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
7769396SMatthew.Ahrens@Sun.COM 		    DMU_OT_NONE, 0, tx);
7779396SMatthew.Ahrens@Sun.COM 		VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
7789396SMatthew.Ahrens@Sun.COM 		    zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
7799396SMatthew.Ahrens@Sun.COM 	}
7809396SMatthew.Ahrens@Sun.COM 	mutex_exit(&zfsvfs->z_lock);
7819396SMatthew.Ahrens@Sun.COM 
7829396SMatthew.Ahrens@Sun.COM 	if (quota == 0) {
7839396SMatthew.Ahrens@Sun.COM 		err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
7849396SMatthew.Ahrens@Sun.COM 		if (err == ENOENT)
7859396SMatthew.Ahrens@Sun.COM 			err = 0;
7869396SMatthew.Ahrens@Sun.COM 	} else {
7879396SMatthew.Ahrens@Sun.COM 		err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, &quota, tx);
7889396SMatthew.Ahrens@Sun.COM 	}
7899396SMatthew.Ahrens@Sun.COM 	ASSERT(err == 0);
7909396SMatthew.Ahrens@Sun.COM 	if (fuid_dirtied)
7919396SMatthew.Ahrens@Sun.COM 		zfs_fuid_sync(zfsvfs, tx);
7929396SMatthew.Ahrens@Sun.COM 	dmu_tx_commit(tx);
7939396SMatthew.Ahrens@Sun.COM 	return (err);
7949396SMatthew.Ahrens@Sun.COM }
7959396SMatthew.Ahrens@Sun.COM 
7969396SMatthew.Ahrens@Sun.COM boolean_t
zfs_fuid_overquota(zfsvfs_t * zfsvfs,boolean_t isgroup,uint64_t fuid)79711935SMark.Shellenbaum@Sun.COM zfs_fuid_overquota(zfsvfs_t *zfsvfs, boolean_t isgroup, uint64_t fuid)
7989396SMatthew.Ahrens@Sun.COM {
7999396SMatthew.Ahrens@Sun.COM 	char buf[32];
8009396SMatthew.Ahrens@Sun.COM 	uint64_t used, quota, usedobj, quotaobj;
8019396SMatthew.Ahrens@Sun.COM 	int err;
8029396SMatthew.Ahrens@Sun.COM 
8039396SMatthew.Ahrens@Sun.COM 	usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
8049396SMatthew.Ahrens@Sun.COM 	quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
8059396SMatthew.Ahrens@Sun.COM 
8069396SMatthew.Ahrens@Sun.COM 	if (quotaobj == 0 || zfsvfs->z_replay)
8079396SMatthew.Ahrens@Sun.COM 		return (B_FALSE);
8089396SMatthew.Ahrens@Sun.COM 
8099396SMatthew.Ahrens@Sun.COM 	(void) sprintf(buf, "%llx", (longlong_t)fuid);
8109396SMatthew.Ahrens@Sun.COM 	err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
8119396SMatthew.Ahrens@Sun.COM 	if (err != 0)
8129396SMatthew.Ahrens@Sun.COM 		return (B_FALSE);
8139396SMatthew.Ahrens@Sun.COM 
8149396SMatthew.Ahrens@Sun.COM 	err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
8159396SMatthew.Ahrens@Sun.COM 	if (err != 0)
8169396SMatthew.Ahrens@Sun.COM 		return (B_FALSE);
8179396SMatthew.Ahrens@Sun.COM 	return (used >= quota);
8189396SMatthew.Ahrens@Sun.COM }
8199396SMatthew.Ahrens@Sun.COM 
82011935SMark.Shellenbaum@Sun.COM boolean_t
zfs_owner_overquota(zfsvfs_t * zfsvfs,znode_t * zp,boolean_t isgroup)82111935SMark.Shellenbaum@Sun.COM zfs_owner_overquota(zfsvfs_t *zfsvfs, znode_t *zp, boolean_t isgroup)
82211935SMark.Shellenbaum@Sun.COM {
82311935SMark.Shellenbaum@Sun.COM 	uint64_t fuid;
82411935SMark.Shellenbaum@Sun.COM 	uint64_t quotaobj;
82511935SMark.Shellenbaum@Sun.COM 
82611935SMark.Shellenbaum@Sun.COM 	quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
82711935SMark.Shellenbaum@Sun.COM 
82813069SMark.Shellenbaum@Oracle.COM 	fuid = isgroup ? zp->z_gid : zp->z_uid;
82911935SMark.Shellenbaum@Sun.COM 
83011935SMark.Shellenbaum@Sun.COM 	if (quotaobj == 0 || zfsvfs->z_replay)
83111935SMark.Shellenbaum@Sun.COM 		return (B_FALSE);
83211935SMark.Shellenbaum@Sun.COM 
83311935SMark.Shellenbaum@Sun.COM 	return (zfs_fuid_overquota(zfsvfs, isgroup, fuid));
83411935SMark.Shellenbaum@Sun.COM }
83511935SMark.Shellenbaum@Sun.COM 
8369396SMatthew.Ahrens@Sun.COM int
zfsvfs_create(const char * osname,zfsvfs_t ** zfvp)83711185SSean.McEnroe@Sun.COM zfsvfs_create(const char *osname, zfsvfs_t **zfvp)
8389396SMatthew.Ahrens@Sun.COM {
8399396SMatthew.Ahrens@Sun.COM 	objset_t *os;
8409396SMatthew.Ahrens@Sun.COM 	zfsvfs_t *zfsvfs;
8419396SMatthew.Ahrens@Sun.COM 	uint64_t zval;
8429396SMatthew.Ahrens@Sun.COM 	int i, error;
84311935SMark.Shellenbaum@Sun.COM 	uint64_t sa_obj;
8449396SMatthew.Ahrens@Sun.COM 
84510298SMatthew.Ahrens@Sun.COM 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
8469396SMatthew.Ahrens@Sun.COM 
84710298SMatthew.Ahrens@Sun.COM 	/*
84810298SMatthew.Ahrens@Sun.COM 	 * We claim to always be readonly so we can open snapshots;
84910298SMatthew.Ahrens@Sun.COM 	 * other ZPL code will prevent us from writing to snapshots.
85010298SMatthew.Ahrens@Sun.COM 	 */
85110298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zfsvfs, &os);
85210298SMatthew.Ahrens@Sun.COM 	if (error) {
85310298SMatthew.Ahrens@Sun.COM 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
85410298SMatthew.Ahrens@Sun.COM 		return (error);
8559396SMatthew.Ahrens@Sun.COM 	}
8569396SMatthew.Ahrens@Sun.COM 
8579396SMatthew.Ahrens@Sun.COM 	/*
8589396SMatthew.Ahrens@Sun.COM 	 * Initialize the zfs-specific filesystem structure.
8599396SMatthew.Ahrens@Sun.COM 	 * Should probably make this a kmem cache, shuffle fields,
8609396SMatthew.Ahrens@Sun.COM 	 * and just bzero up to z_hold_mtx[].
8619396SMatthew.Ahrens@Sun.COM 	 */
8629396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_vfs = NULL;
8639396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_parent = zfsvfs;
8649396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
8659396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
8669396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_os = os;
8679396SMatthew.Ahrens@Sun.COM 
8689396SMatthew.Ahrens@Sun.COM 	error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
8699396SMatthew.Ahrens@Sun.COM 	if (error) {
8709396SMatthew.Ahrens@Sun.COM 		goto out;
87112070SMark.Shellenbaum@Sun.COM 	} else if (zfsvfs->z_version >
87212070SMark.Shellenbaum@Sun.COM 	    zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
87312070SMark.Shellenbaum@Sun.COM 		(void) printf("Can't mount a version %lld file system "
87412070SMark.Shellenbaum@Sun.COM 		    "on a version %lld pool\n. Pool must be upgraded to mount "
87512070SMark.Shellenbaum@Sun.COM 		    "this file system.", (u_longlong_t)zfsvfs->z_version,
87612070SMark.Shellenbaum@Sun.COM 		    (u_longlong_t)spa_version(dmu_objset_spa(os)));
8779396SMatthew.Ahrens@Sun.COM 		error = ENOTSUP;
8789396SMatthew.Ahrens@Sun.COM 		goto out;
8799396SMatthew.Ahrens@Sun.COM 	}
8809396SMatthew.Ahrens@Sun.COM 	if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
8819396SMatthew.Ahrens@Sun.COM 		goto out;
8829396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_norm = (int)zval;
8839396SMatthew.Ahrens@Sun.COM 
8849396SMatthew.Ahrens@Sun.COM 	if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
8859396SMatthew.Ahrens@Sun.COM 		goto out;
8869396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_utf8 = (zval != 0);
8879396SMatthew.Ahrens@Sun.COM 
8889396SMatthew.Ahrens@Sun.COM 	if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
8899396SMatthew.Ahrens@Sun.COM 		goto out;
8909396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_case = (uint_t)zval;
8919396SMatthew.Ahrens@Sun.COM 
8929396SMatthew.Ahrens@Sun.COM 	/*
8939396SMatthew.Ahrens@Sun.COM 	 * Fold case on file systems that are always or sometimes case
8949396SMatthew.Ahrens@Sun.COM 	 * insensitive.
8959396SMatthew.Ahrens@Sun.COM 	 */
8969396SMatthew.Ahrens@Sun.COM 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
8979396SMatthew.Ahrens@Sun.COM 	    zfsvfs->z_case == ZFS_CASE_MIXED)
8989396SMatthew.Ahrens@Sun.COM 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
8999396SMatthew.Ahrens@Sun.COM 
9009396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
90111935SMark.Shellenbaum@Sun.COM 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
90211935SMark.Shellenbaum@Sun.COM 
90311935SMark.Shellenbaum@Sun.COM 	if (zfsvfs->z_use_sa) {
90411935SMark.Shellenbaum@Sun.COM 		/* should either have both of these objects or none */
90511935SMark.Shellenbaum@Sun.COM 		error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
90611935SMark.Shellenbaum@Sun.COM 		    &sa_obj);
90711935SMark.Shellenbaum@Sun.COM 		if (error)
90811935SMark.Shellenbaum@Sun.COM 			return (error);
90911935SMark.Shellenbaum@Sun.COM 	} else {
91011935SMark.Shellenbaum@Sun.COM 		/*
91111935SMark.Shellenbaum@Sun.COM 		 * Pre SA versions file systems should never touch
91211935SMark.Shellenbaum@Sun.COM 		 * either the attribute registration or layout objects.
91311935SMark.Shellenbaum@Sun.COM 		 */
91411935SMark.Shellenbaum@Sun.COM 		sa_obj = 0;
91511935SMark.Shellenbaum@Sun.COM 	}
91611935SMark.Shellenbaum@Sun.COM 
91712493SMark.Shellenbaum@Oracle.COM 	error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
91812493SMark.Shellenbaum@Oracle.COM 	    &zfsvfs->z_attr_table);
91912493SMark.Shellenbaum@Oracle.COM 	if (error)
92012493SMark.Shellenbaum@Oracle.COM 		goto out;
92111935SMark.Shellenbaum@Sun.COM 
92211935SMark.Shellenbaum@Sun.COM 	if (zfsvfs->z_version >= ZPL_VERSION_SA)
92311935SMark.Shellenbaum@Sun.COM 		sa_register_update_callback(os, zfs_sa_upgrade);
9249396SMatthew.Ahrens@Sun.COM 
9259396SMatthew.Ahrens@Sun.COM 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
9269396SMatthew.Ahrens@Sun.COM 	    &zfsvfs->z_root);
9279396SMatthew.Ahrens@Sun.COM 	if (error)
9289396SMatthew.Ahrens@Sun.COM 		goto out;
9299396SMatthew.Ahrens@Sun.COM 	ASSERT(zfsvfs->z_root != 0);
9309396SMatthew.Ahrens@Sun.COM 
9319396SMatthew.Ahrens@Sun.COM 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
9329396SMatthew.Ahrens@Sun.COM 	    &zfsvfs->z_unlinkedobj);
9339396SMatthew.Ahrens@Sun.COM 	if (error)
9349396SMatthew.Ahrens@Sun.COM 		goto out;
9359396SMatthew.Ahrens@Sun.COM 
9369396SMatthew.Ahrens@Sun.COM 	error = zap_lookup(os, MASTER_NODE_OBJ,
9379396SMatthew.Ahrens@Sun.COM 	    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
9389396SMatthew.Ahrens@Sun.COM 	    8, 1, &zfsvfs->z_userquota_obj);
9399396SMatthew.Ahrens@Sun.COM 	if (error && error != ENOENT)
9409396SMatthew.Ahrens@Sun.COM 		goto out;
9419396SMatthew.Ahrens@Sun.COM 
9429396SMatthew.Ahrens@Sun.COM 	error = zap_lookup(os, MASTER_NODE_OBJ,
9439396SMatthew.Ahrens@Sun.COM 	    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
9449396SMatthew.Ahrens@Sun.COM 	    8, 1, &zfsvfs->z_groupquota_obj);
9459396SMatthew.Ahrens@Sun.COM 	if (error && error != ENOENT)
9469396SMatthew.Ahrens@Sun.COM 		goto out;
9479396SMatthew.Ahrens@Sun.COM 
9489396SMatthew.Ahrens@Sun.COM 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
9499396SMatthew.Ahrens@Sun.COM 	    &zfsvfs->z_fuid_obj);
9509396SMatthew.Ahrens@Sun.COM 	if (error && error != ENOENT)
9519396SMatthew.Ahrens@Sun.COM 		goto out;
9529396SMatthew.Ahrens@Sun.COM 
9539396SMatthew.Ahrens@Sun.COM 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
9549396SMatthew.Ahrens@Sun.COM 	    &zfsvfs->z_shares_dir);
9559396SMatthew.Ahrens@Sun.COM 	if (error && error != ENOENT)
9569396SMatthew.Ahrens@Sun.COM 		goto out;
9579396SMatthew.Ahrens@Sun.COM 
9589396SMatthew.Ahrens@Sun.COM 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
9599396SMatthew.Ahrens@Sun.COM 	mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
9609396SMatthew.Ahrens@Sun.COM 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
9619396SMatthew.Ahrens@Sun.COM 	    offsetof(znode_t, z_link_node));
9629396SMatthew.Ahrens@Sun.COM 	rrw_init(&zfsvfs->z_teardown_lock);
9639396SMatthew.Ahrens@Sun.COM 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
9649396SMatthew.Ahrens@Sun.COM 	rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
9659396SMatthew.Ahrens@Sun.COM 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
9669396SMatthew.Ahrens@Sun.COM 		mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
9679396SMatthew.Ahrens@Sun.COM 
96811185SSean.McEnroe@Sun.COM 	*zfvp = zfsvfs;
9699396SMatthew.Ahrens@Sun.COM 	return (0);
9709396SMatthew.Ahrens@Sun.COM 
9719396SMatthew.Ahrens@Sun.COM out:
97210298SMatthew.Ahrens@Sun.COM 	dmu_objset_disown(os, zfsvfs);
97311185SSean.McEnroe@Sun.COM 	*zfvp = NULL;
9749396SMatthew.Ahrens@Sun.COM 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
9759396SMatthew.Ahrens@Sun.COM 	return (error);
9769396SMatthew.Ahrens@Sun.COM }
9779396SMatthew.Ahrens@Sun.COM 
9781544Seschrock static int
zfsvfs_setup(zfsvfs_t * zfsvfs,boolean_t mounting)9795326Sek110237 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
9805326Sek110237 {
9815326Sek110237 	int error;
9825326Sek110237 
9835326Sek110237 	error = zfs_register_callbacks(zfsvfs->z_vfs);
9845326Sek110237 	if (error)
9855326Sek110237 		return (error);
9865326Sek110237 
9875326Sek110237 	/*
9885326Sek110237 	 * Set the objset user_ptr to track its zfsvfs.
9895326Sek110237 	 */
99010298SMatthew.Ahrens@Sun.COM 	mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
9915326Sek110237 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
99210298SMatthew.Ahrens@Sun.COM 	mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
9935326Sek110237 
9949292SNeil.Perrin@Sun.COM 	zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
9959292SNeil.Perrin@Sun.COM 
9965326Sek110237 	/*
9975326Sek110237 	 * If we are not mounting (ie: online recv), then we don't
9985326Sek110237 	 * have to worry about replaying the log as we blocked all
9995326Sek110237 	 * operations out since we closed the ZIL.
10005326Sek110237 	 */
10015326Sek110237 	if (mounting) {
10027638SNeil.Perrin@Sun.COM 		boolean_t readonly;
10037638SNeil.Perrin@Sun.COM 
10045326Sek110237 		/*
10055326Sek110237 		 * During replay we remove the read only flag to
10065326Sek110237 		 * allow replays to succeed.
10075326Sek110237 		 */
10085326Sek110237 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
10098227SNeil.Perrin@Sun.COM 		if (readonly != 0)
10108227SNeil.Perrin@Sun.COM 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
10118227SNeil.Perrin@Sun.COM 		else
10128227SNeil.Perrin@Sun.COM 			zfs_unlinked_drain(zfsvfs);
10135326Sek110237 
101412294SMark.Musante@Sun.COM 		/*
101512294SMark.Musante@Sun.COM 		 * Parse and replay the intent log.
101612294SMark.Musante@Sun.COM 		 *
101712294SMark.Musante@Sun.COM 		 * Because of ziltest, this must be done after
101812294SMark.Musante@Sun.COM 		 * zfs_unlinked_drain().  (Further note: ziltest
101912294SMark.Musante@Sun.COM 		 * doesn't use readonly mounts, where
102012294SMark.Musante@Sun.COM 		 * zfs_unlinked_drain() isn't called.)  This is because
102112294SMark.Musante@Sun.COM 		 * ziltest causes spa_sync() to think it's committed,
102212294SMark.Musante@Sun.COM 		 * but actually it is not, so the intent log contains
102312294SMark.Musante@Sun.COM 		 * many txg's worth of changes.
102412294SMark.Musante@Sun.COM 		 *
102512294SMark.Musante@Sun.COM 		 * In particular, if object N is in the unlinked set in
102612294SMark.Musante@Sun.COM 		 * the last txg to actually sync, then it could be
102712294SMark.Musante@Sun.COM 		 * actually freed in a later txg and then reallocated
102812294SMark.Musante@Sun.COM 		 * in a yet later txg.  This would write a "create
102912294SMark.Musante@Sun.COM 		 * object N" record to the intent log.  Normally, this
103012294SMark.Musante@Sun.COM 		 * would be fine because the spa_sync() would have
103112294SMark.Musante@Sun.COM 		 * written out the fact that object N is free, before
103212294SMark.Musante@Sun.COM 		 * we could write the "create object N" intent log
103312294SMark.Musante@Sun.COM 		 * record.
103412294SMark.Musante@Sun.COM 		 *
103512294SMark.Musante@Sun.COM 		 * But when we are in ziltest mode, we advance the "open
103612294SMark.Musante@Sun.COM 		 * txg" without actually spa_sync()-ing the changes to
103712294SMark.Musante@Sun.COM 		 * disk.  So we would see that object N is still
103812294SMark.Musante@Sun.COM 		 * allocated and in the unlinked set, and there is an
103912294SMark.Musante@Sun.COM 		 * intent log record saying to allocate it.
104012294SMark.Musante@Sun.COM 		 */
104113049SGeorge.Wilson@Sun.COM 		if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
104213049SGeorge.Wilson@Sun.COM 			if (zil_replay_disable) {
104313049SGeorge.Wilson@Sun.COM 				zil_destroy(zfsvfs->z_log, B_FALSE);
104413049SGeorge.Wilson@Sun.COM 			} else {
104513049SGeorge.Wilson@Sun.COM 				zfsvfs->z_replay = B_TRUE;
104613049SGeorge.Wilson@Sun.COM 				zil_replay(zfsvfs->z_os, zfsvfs,
104713049SGeorge.Wilson@Sun.COM 				    zfs_replay_vector);
104813049SGeorge.Wilson@Sun.COM 				zfsvfs->z_replay = B_FALSE;
104913049SGeorge.Wilson@Sun.COM 			}
10508227SNeil.Perrin@Sun.COM 		}
10515326Sek110237 		zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
10525326Sek110237 	}
10535326Sek110237 
10545326Sek110237 	return (0);
10555326Sek110237 }
10565326Sek110237 
10579396SMatthew.Ahrens@Sun.COM void
zfsvfs_free(zfsvfs_t * zfsvfs)10589396SMatthew.Ahrens@Sun.COM zfsvfs_free(zfsvfs_t *zfsvfs)
10596083Sek110237 {
10609396SMatthew.Ahrens@Sun.COM 	int i;
10619788STom.Erickson@Sun.COM 	extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
10629788STom.Erickson@Sun.COM 
10639788STom.Erickson@Sun.COM 	/*
10649788STom.Erickson@Sun.COM 	 * This is a barrier to prevent the filesystem from going away in
10659788STom.Erickson@Sun.COM 	 * zfs_znode_move() until we can safely ensure that the filesystem is
10669788STom.Erickson@Sun.COM 	 * not unmounted. We consider the filesystem valid before the barrier
10679788STom.Erickson@Sun.COM 	 * and invalid after the barrier.
10689788STom.Erickson@Sun.COM 	 */
10699788STom.Erickson@Sun.COM 	rw_enter(&zfsvfs_lock, RW_READER);
10709788STom.Erickson@Sun.COM 	rw_exit(&zfsvfs_lock);
10719396SMatthew.Ahrens@Sun.COM 
10729396SMatthew.Ahrens@Sun.COM 	zfs_fuid_destroy(zfsvfs);
10739396SMatthew.Ahrens@Sun.COM 
10746083Sek110237 	mutex_destroy(&zfsvfs->z_znodes_lock);
10759030SMark.Shellenbaum@Sun.COM 	mutex_destroy(&zfsvfs->z_lock);
10766083Sek110237 	list_destroy(&zfsvfs->z_all_znodes);
10776083Sek110237 	rrw_destroy(&zfsvfs->z_teardown_lock);
10786083Sek110237 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
10796083Sek110237 	rw_destroy(&zfsvfs->z_fuid_lock);
10809396SMatthew.Ahrens@Sun.COM 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
10819396SMatthew.Ahrens@Sun.COM 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
10826083Sek110237 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
10836083Sek110237 }
10846083Sek110237 
10859396SMatthew.Ahrens@Sun.COM static void
zfs_set_fuid_feature(zfsvfs_t * zfsvfs)10869396SMatthew.Ahrens@Sun.COM zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
10879396SMatthew.Ahrens@Sun.COM {
10889396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1089*13147SMark.Shellenbaum@Oracle.COM 	if (zfsvfs->z_vfs) {
1090*13147SMark.Shellenbaum@Oracle.COM 		if (zfsvfs->z_use_fuids) {
1091*13147SMark.Shellenbaum@Oracle.COM 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1092*13147SMark.Shellenbaum@Oracle.COM 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1093*13147SMark.Shellenbaum@Oracle.COM 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1094*13147SMark.Shellenbaum@Oracle.COM 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1095*13147SMark.Shellenbaum@Oracle.COM 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1096*13147SMark.Shellenbaum@Oracle.COM 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1097*13147SMark.Shellenbaum@Oracle.COM 		} else {
1098*13147SMark.Shellenbaum@Oracle.COM 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1099*13147SMark.Shellenbaum@Oracle.COM 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1100*13147SMark.Shellenbaum@Oracle.COM 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1101*13147SMark.Shellenbaum@Oracle.COM 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1102*13147SMark.Shellenbaum@Oracle.COM 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1103*13147SMark.Shellenbaum@Oracle.COM 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1104*13147SMark.Shellenbaum@Oracle.COM 		}
11059396SMatthew.Ahrens@Sun.COM 	}
110611935SMark.Shellenbaum@Sun.COM 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
11079396SMatthew.Ahrens@Sun.COM }
11089396SMatthew.Ahrens@Sun.COM 
11095326Sek110237 static int
zfs_domount(vfs_t * vfsp,char * osname)11107046Sahrens zfs_domount(vfs_t *vfsp, char *osname)
11111544Seschrock {
11121544Seschrock 	dev_t mount_dev;
11139396SMatthew.Ahrens@Sun.COM 	uint64_t recordsize, fsid_guid;
11141544Seschrock 	int error = 0;
11151544Seschrock 	zfsvfs_t *zfsvfs;
11161544Seschrock 
11171544Seschrock 	ASSERT(vfsp);
11181544Seschrock 	ASSERT(osname);
11191544Seschrock 
112010298SMatthew.Ahrens@Sun.COM 	error = zfsvfs_create(osname, &zfsvfs);
11219396SMatthew.Ahrens@Sun.COM 	if (error)
11229396SMatthew.Ahrens@Sun.COM 		return (error);
11231544Seschrock 	zfsvfs->z_vfs = vfsp;
11241544Seschrock 
11251544Seschrock 	/* Initialize the generic filesystem structure. */
11261544Seschrock 	vfsp->vfs_bcount = 0;
11271544Seschrock 	vfsp->vfs_data = NULL;
11281544Seschrock 
11291544Seschrock 	if (zfs_create_unique_device(&mount_dev) == -1) {
11301544Seschrock 		error = ENODEV;
11311544Seschrock 		goto out;
11321544Seschrock 	}
11331544Seschrock 	ASSERT(vfs_devismounted(mount_dev) == 0);
11341544Seschrock 
11351544Seschrock 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
11361544Seschrock 	    NULL))
11371544Seschrock 		goto out;
11381544Seschrock 
11391544Seschrock 	vfsp->vfs_dev = mount_dev;
11401544Seschrock 	vfsp->vfs_fstype = zfsfstype;
11411544Seschrock 	vfsp->vfs_bsize = recordsize;
11421544Seschrock 	vfsp->vfs_flag |= VFS_NOTRUNC;
11431544Seschrock 	vfsp->vfs_data = zfsvfs;
11441544Seschrock 
11459396SMatthew.Ahrens@Sun.COM 	/*
11469396SMatthew.Ahrens@Sun.COM 	 * The fsid is 64 bits, composed of an 8-bit fs type, which
11479396SMatthew.Ahrens@Sun.COM 	 * separates our fsid from any other filesystem types, and a
11489396SMatthew.Ahrens@Sun.COM 	 * 56-bit objset unique ID.  The objset unique ID is unique to
11499396SMatthew.Ahrens@Sun.COM 	 * all objsets open on this system, provided by unique_create().
11509396SMatthew.Ahrens@Sun.COM 	 * The 8-bit fs type must be put in the low bits of fsid[1]
11519396SMatthew.Ahrens@Sun.COM 	 * because that's where other Solaris filesystems put it.
11529396SMatthew.Ahrens@Sun.COM 	 */
11539396SMatthew.Ahrens@Sun.COM 	fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
11549396SMatthew.Ahrens@Sun.COM 	ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
11559396SMatthew.Ahrens@Sun.COM 	vfsp->vfs_fsid.val[0] = fsid_guid;
11569396SMatthew.Ahrens@Sun.COM 	vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
11579396SMatthew.Ahrens@Sun.COM 	    zfsfstype & 0xFF;
11581544Seschrock 
11595331Samw 	/*
11605331Samw 	 * Set features for file system.
11615331Samw 	 */
11629396SMatthew.Ahrens@Sun.COM 	zfs_set_fuid_feature(zfsvfs);
11635498Stimh 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
11645498Stimh 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
11655498Stimh 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
11665498Stimh 		vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
11675498Stimh 	} else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
11685498Stimh 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
11695498Stimh 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
11705498Stimh 	}
117111539SChunli.Zhang@Sun.COM 	vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
11725331Samw 
11731544Seschrock 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
11745331Samw 		uint64_t pval;
11753234Sck153898 
11761544Seschrock 		atime_changed_cb(zfsvfs, B_FALSE);
11771544Seschrock 		readonly_changed_cb(zfsvfs, B_TRUE);
11785331Samw 		if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
11793234Sck153898 			goto out;
11805331Samw 		xattr_changed_cb(zfsvfs, pval);
11811544Seschrock 		zfsvfs->z_issnap = B_TRUE;
118212530SMark.Musante@Sun.COM 		zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
11839688SMatthew.Ahrens@Sun.COM 
118410298SMatthew.Ahrens@Sun.COM 		mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
11859688SMatthew.Ahrens@Sun.COM 		dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
118610298SMatthew.Ahrens@Sun.COM 		mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
11871544Seschrock 	} else {
11885326Sek110237 		error = zfsvfs_setup(zfsvfs, B_TRUE);
11891544Seschrock 	}
11901544Seschrock 
11911544Seschrock 	if (!zfsvfs->z_issnap)
11921544Seschrock 		zfsctl_create(zfsvfs);
11931544Seschrock out:
11941544Seschrock 	if (error) {
119510298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
11969396SMatthew.Ahrens@Sun.COM 		zfsvfs_free(zfsvfs);
11971544Seschrock 	} else {
11981544Seschrock 		atomic_add_32(&zfs_active_fs_count, 1);
11991544Seschrock 	}
12001544Seschrock 
12011544Seschrock 	return (error);
12021544Seschrock }
12031544Seschrock 
12041544Seschrock void
zfs_unregister_callbacks(zfsvfs_t * zfsvfs)12051544Seschrock zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
12061544Seschrock {
12071544Seschrock 	objset_t *os = zfsvfs->z_os;
12081544Seschrock 	struct dsl_dataset *ds;
12091544Seschrock 
12101544Seschrock 	/*
12111544Seschrock 	 * Unregister properties.
12121544Seschrock 	 */
12131544Seschrock 	if (!dmu_objset_is_snapshot(os)) {
12141544Seschrock 		ds = dmu_objset_ds(os);
12151544Seschrock 		VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
12161544Seschrock 		    zfsvfs) == 0);
12171544Seschrock 
12183234Sck153898 		VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
12193234Sck153898 		    zfsvfs) == 0);
12203234Sck153898 
12211544Seschrock 		VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
12221544Seschrock 		    zfsvfs) == 0);
12231544Seschrock 
12241544Seschrock 		VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
12251544Seschrock 		    zfsvfs) == 0);
12261544Seschrock 
12271544Seschrock 		VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
12281544Seschrock 		    zfsvfs) == 0);
12291544Seschrock 
12301544Seschrock 		VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
12311544Seschrock 		    zfsvfs) == 0);
12321544Seschrock 
12331544Seschrock 		VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
12341544Seschrock 		    zfsvfs) == 0);
12351544Seschrock 
12361544Seschrock 		VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
12371544Seschrock 		    zfsvfs) == 0);
12381544Seschrock 
12391544Seschrock 		VERIFY(dsl_prop_unregister(ds, "aclinherit",
12401544Seschrock 		    acl_inherit_changed_cb, zfsvfs) == 0);
12415331Samw 
12425331Samw 		VERIFY(dsl_prop_unregister(ds, "vscan",
12435331Samw 		    vscan_changed_cb, zfsvfs) == 0);
12441544Seschrock 	}
12451544Seschrock }
12461544Seschrock 
12473912Slling /*
12483912Slling  * Convert a decimal digit string to a uint64_t integer.
12493912Slling  */
12503912Slling static int
str_to_uint64(char * str,uint64_t * objnum)12513912Slling str_to_uint64(char *str, uint64_t *objnum)
12523912Slling {
12533912Slling 	uint64_t num = 0;
12543912Slling 
12553912Slling 	while (*str) {
12563912Slling 		if (*str < '0' || *str > '9')
12573912Slling 			return (EINVAL);
12583912Slling 
12593912Slling 		num = num*10 + *str++ - '0';
12603912Slling 	}
12613912Slling 
12623912Slling 	*objnum = num;
12633912Slling 	return (0);
12643912Slling }
12653912Slling 
12663912Slling /*
12673912Slling  * The boot path passed from the boot loader is in the form of
12683912Slling  * "rootpool-name/root-filesystem-object-number'. Convert this
12693912Slling  * string to a dataset name: "rootpool-name/root-filesystem-name".
12703912Slling  */
12713912Slling static int
zfs_parse_bootfs(char * bpath,char * outpath)12726423Sgw25295 zfs_parse_bootfs(char *bpath, char *outpath)
12733912Slling {
12743912Slling 	char *slashp;
12753912Slling 	uint64_t objnum;
12763912Slling 	int error;
12773912Slling 
12783912Slling 	if (*bpath == 0 || *bpath == '/')
12793912Slling 		return (EINVAL);
12803912Slling 
12817656SSherry.Moore@Sun.COM 	(void) strcpy(outpath, bpath);
12827656SSherry.Moore@Sun.COM 
12833912Slling 	slashp = strchr(bpath, '/');
12843912Slling 
12853912Slling 	/* if no '/', just return the pool name */
12863912Slling 	if (slashp == NULL) {
12873912Slling 		return (0);
12883912Slling 	}
12893912Slling 
12907656SSherry.Moore@Sun.COM 	/* if not a number, just return the root dataset name */
12917656SSherry.Moore@Sun.COM 	if (str_to_uint64(slashp+1, &objnum)) {
12927656SSherry.Moore@Sun.COM 		return (0);
12937656SSherry.Moore@Sun.COM 	}
12943912Slling 
12953912Slling 	*slashp = '\0';
12963912Slling 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
12973912Slling 	*slashp = '/';
12983912Slling 
12993912Slling 	return (error);
13003912Slling }
13013912Slling 
130210972SRic.Aleshire@Sun.COM /*
130310972SRic.Aleshire@Sun.COM  * zfs_check_global_label:
130410972SRic.Aleshire@Sun.COM  *	Check that the hex label string is appropriate for the dataset
130510972SRic.Aleshire@Sun.COM  *	being mounted into the global_zone proper.
130610972SRic.Aleshire@Sun.COM  *
130710972SRic.Aleshire@Sun.COM  *	Return an error if the hex label string is not default or
130810972SRic.Aleshire@Sun.COM  *	admin_low/admin_high.  For admin_low labels, the corresponding
130910972SRic.Aleshire@Sun.COM  *	dataset must be readonly.
131010972SRic.Aleshire@Sun.COM  */
131110972SRic.Aleshire@Sun.COM int
zfs_check_global_label(const char * dsname,const char * hexsl)131210972SRic.Aleshire@Sun.COM zfs_check_global_label(const char *dsname, const char *hexsl)
131310972SRic.Aleshire@Sun.COM {
131410972SRic.Aleshire@Sun.COM 	if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
131510972SRic.Aleshire@Sun.COM 		return (0);
131610972SRic.Aleshire@Sun.COM 	if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
131710972SRic.Aleshire@Sun.COM 		return (0);
131810972SRic.Aleshire@Sun.COM 	if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
131910972SRic.Aleshire@Sun.COM 		/* must be readonly */
132010972SRic.Aleshire@Sun.COM 		uint64_t rdonly;
132110972SRic.Aleshire@Sun.COM 
132210972SRic.Aleshire@Sun.COM 		if (dsl_prop_get_integer(dsname,
132310972SRic.Aleshire@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
132410972SRic.Aleshire@Sun.COM 			return (EACCES);
132510972SRic.Aleshire@Sun.COM 		return (rdonly ? 0 : EACCES);
132610972SRic.Aleshire@Sun.COM 	}
132710972SRic.Aleshire@Sun.COM 	return (EACCES);
132810972SRic.Aleshire@Sun.COM }
132910972SRic.Aleshire@Sun.COM 
133010972SRic.Aleshire@Sun.COM /*
133110972SRic.Aleshire@Sun.COM  * zfs_mount_label_policy:
133210972SRic.Aleshire@Sun.COM  *	Determine whether the mount is allowed according to MAC check.
133310972SRic.Aleshire@Sun.COM  *	by comparing (where appropriate) label of the dataset against
133410972SRic.Aleshire@Sun.COM  *	the label of the zone being mounted into.  If the dataset has
133510972SRic.Aleshire@Sun.COM  *	no label, create one.
133610972SRic.Aleshire@Sun.COM  *
133710972SRic.Aleshire@Sun.COM  *	Returns:
133810972SRic.Aleshire@Sun.COM  *		 0 :	access allowed
133910972SRic.Aleshire@Sun.COM  *		>0 :	error code, such as EACCES
134010972SRic.Aleshire@Sun.COM  */
134110972SRic.Aleshire@Sun.COM static int
zfs_mount_label_policy(vfs_t * vfsp,char * osname)134210972SRic.Aleshire@Sun.COM zfs_mount_label_policy(vfs_t *vfsp, char *osname)
134310972SRic.Aleshire@Sun.COM {
134410972SRic.Aleshire@Sun.COM 	int		error, retv;
134510972SRic.Aleshire@Sun.COM 	zone_t		*mntzone = NULL;
134610972SRic.Aleshire@Sun.COM 	ts_label_t	*mnt_tsl;
134710972SRic.Aleshire@Sun.COM 	bslabel_t	*mnt_sl;
134810972SRic.Aleshire@Sun.COM 	bslabel_t	ds_sl;
134910972SRic.Aleshire@Sun.COM 	char		ds_hexsl[MAXNAMELEN];
135010972SRic.Aleshire@Sun.COM 
135110972SRic.Aleshire@Sun.COM 	retv = EACCES;				/* assume the worst */
135210972SRic.Aleshire@Sun.COM 
135310972SRic.Aleshire@Sun.COM 	/*
135410972SRic.Aleshire@Sun.COM 	 * Start by getting the dataset label if it exists.
135510972SRic.Aleshire@Sun.COM 	 */
135610972SRic.Aleshire@Sun.COM 	error = dsl_prop_get(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
135710972SRic.Aleshire@Sun.COM 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
135810972SRic.Aleshire@Sun.COM 	if (error)
135910972SRic.Aleshire@Sun.COM 		return (EACCES);
136010972SRic.Aleshire@Sun.COM 
136110972SRic.Aleshire@Sun.COM 	/*
136210972SRic.Aleshire@Sun.COM 	 * If labeling is NOT enabled, then disallow the mount of datasets
136310972SRic.Aleshire@Sun.COM 	 * which have a non-default label already.  No other label checks
136410972SRic.Aleshire@Sun.COM 	 * are needed.
136510972SRic.Aleshire@Sun.COM 	 */
136610972SRic.Aleshire@Sun.COM 	if (!is_system_labeled()) {
136710972SRic.Aleshire@Sun.COM 		if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
136810972SRic.Aleshire@Sun.COM 			return (0);
136910972SRic.Aleshire@Sun.COM 		return (EACCES);
137010972SRic.Aleshire@Sun.COM 	}
137110972SRic.Aleshire@Sun.COM 
137210972SRic.Aleshire@Sun.COM 	/*
137310972SRic.Aleshire@Sun.COM 	 * Get the label of the mountpoint.  If mounting into the global
137410972SRic.Aleshire@Sun.COM 	 * zone (i.e. mountpoint is not within an active zone and the
137510972SRic.Aleshire@Sun.COM 	 * zoned property is off), the label must be default or
137610972SRic.Aleshire@Sun.COM 	 * admin_low/admin_high only; no other checks are needed.
137710972SRic.Aleshire@Sun.COM 	 */
137810972SRic.Aleshire@Sun.COM 	mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE);
137910972SRic.Aleshire@Sun.COM 	if (mntzone->zone_id == GLOBAL_ZONEID) {
138010972SRic.Aleshire@Sun.COM 		uint64_t zoned;
138110972SRic.Aleshire@Sun.COM 
138210972SRic.Aleshire@Sun.COM 		zone_rele(mntzone);
138310972SRic.Aleshire@Sun.COM 
138410972SRic.Aleshire@Sun.COM 		if (dsl_prop_get_integer(osname,
138510972SRic.Aleshire@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
138610972SRic.Aleshire@Sun.COM 			return (EACCES);
138710972SRic.Aleshire@Sun.COM 		if (!zoned)
138810972SRic.Aleshire@Sun.COM 			return (zfs_check_global_label(osname, ds_hexsl));
138910972SRic.Aleshire@Sun.COM 		else
139010972SRic.Aleshire@Sun.COM 			/*
139110972SRic.Aleshire@Sun.COM 			 * This is the case of a zone dataset being mounted
139210972SRic.Aleshire@Sun.COM 			 * initially, before the zone has been fully created;
139310972SRic.Aleshire@Sun.COM 			 * allow this mount into global zone.
139410972SRic.Aleshire@Sun.COM 			 */
139510972SRic.Aleshire@Sun.COM 			return (0);
139610972SRic.Aleshire@Sun.COM 	}
139710972SRic.Aleshire@Sun.COM 
139810972SRic.Aleshire@Sun.COM 	mnt_tsl = mntzone->zone_slabel;
139910972SRic.Aleshire@Sun.COM 	ASSERT(mnt_tsl != NULL);
140010972SRic.Aleshire@Sun.COM 	label_hold(mnt_tsl);
140110972SRic.Aleshire@Sun.COM 	mnt_sl = label2bslabel(mnt_tsl);
140210972SRic.Aleshire@Sun.COM 
140310972SRic.Aleshire@Sun.COM 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) {
140410972SRic.Aleshire@Sun.COM 		/*
140510972SRic.Aleshire@Sun.COM 		 * The dataset doesn't have a real label, so fabricate one.
140610972SRic.Aleshire@Sun.COM 		 */
140710972SRic.Aleshire@Sun.COM 		char *str = NULL;
140810972SRic.Aleshire@Sun.COM 
140910972SRic.Aleshire@Sun.COM 		if (l_to_str_internal(mnt_sl, &str) == 0 &&
141010972SRic.Aleshire@Sun.COM 		    dsl_prop_set(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
141111022STom.Erickson@Sun.COM 		    ZPROP_SRC_LOCAL, 1, strlen(str) + 1, str) == 0)
141210972SRic.Aleshire@Sun.COM 			retv = 0;
141310972SRic.Aleshire@Sun.COM 		if (str != NULL)
141410972SRic.Aleshire@Sun.COM 			kmem_free(str, strlen(str) + 1);
141510972SRic.Aleshire@Sun.COM 	} else if (hexstr_to_label(ds_hexsl, &ds_sl) == 0) {
141610972SRic.Aleshire@Sun.COM 		/*
141710972SRic.Aleshire@Sun.COM 		 * Now compare labels to complete the MAC check.  If the
141810972SRic.Aleshire@Sun.COM 		 * labels are equal then allow access.  If the mountpoint
141910972SRic.Aleshire@Sun.COM 		 * label dominates the dataset label, allow readonly access.
142010972SRic.Aleshire@Sun.COM 		 * Otherwise, access is denied.
142110972SRic.Aleshire@Sun.COM 		 */
142210972SRic.Aleshire@Sun.COM 		if (blequal(mnt_sl, &ds_sl))
142310972SRic.Aleshire@Sun.COM 			retv = 0;
142410972SRic.Aleshire@Sun.COM 		else if (bldominates(mnt_sl, &ds_sl)) {
142510972SRic.Aleshire@Sun.COM 			vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
142610972SRic.Aleshire@Sun.COM 			retv = 0;
142710972SRic.Aleshire@Sun.COM 		}
142810972SRic.Aleshire@Sun.COM 	}
142910972SRic.Aleshire@Sun.COM 
143010972SRic.Aleshire@Sun.COM 	label_rele(mnt_tsl);
143110972SRic.Aleshire@Sun.COM 	zone_rele(mntzone);
143210972SRic.Aleshire@Sun.COM 	return (retv);
143310972SRic.Aleshire@Sun.COM }
143410972SRic.Aleshire@Sun.COM 
14351544Seschrock static int
zfs_mountroot(vfs_t * vfsp,enum whymountroot why)14361544Seschrock zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
14371544Seschrock {
14381544Seschrock 	int error = 0;
14391544Seschrock 	static int zfsrootdone = 0;
14401544Seschrock 	zfsvfs_t *zfsvfs = NULL;
14411544Seschrock 	znode_t *zp = NULL;
14421544Seschrock 	vnode_t *vp = NULL;
14436423Sgw25295 	char *zfs_bootfs;
14447147Staylor 	char *zfs_devid;
14451544Seschrock 
14461544Seschrock 	ASSERT(vfsp);
14471544Seschrock 
14481544Seschrock 	/*
14493912Slling 	 * The filesystem that we mount as root is defined in the
14506423Sgw25295 	 * boot property "zfs-bootfs" with a format of
14516423Sgw25295 	 * "poolname/root-dataset-objnum".
14521544Seschrock 	 */
14531544Seschrock 	if (why == ROOT_INIT) {
14541544Seschrock 		if (zfsrootdone++)
14551544Seschrock 			return (EBUSY);
14566423Sgw25295 		/*
14576423Sgw25295 		 * the process of doing a spa_load will require the
14586423Sgw25295 		 * clock to be set before we could (for example) do
14596423Sgw25295 		 * something better by looking at the timestamp on
14606423Sgw25295 		 * an uberblock, so just set it to -1.
14616423Sgw25295 		 */
14626423Sgw25295 		clkset(-1);
14631544Seschrock 
14647147Staylor 		if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
14657147Staylor 			cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
14667147Staylor 			    "bootfs name");
14676423Sgw25295 			return (EINVAL);
14685648Ssetje 		}
14697147Staylor 		zfs_devid = spa_get_bootprop("diskdevid");
14707147Staylor 		error = spa_import_rootpool(rootfs.bo_name, zfs_devid);
14717147Staylor 		if (zfs_devid)
14727147Staylor 			spa_free_bootprop(zfs_devid);
14737147Staylor 		if (error) {
14747147Staylor 			spa_free_bootprop(zfs_bootfs);
14757147Staylor 			cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
14767147Staylor 			    error);
14777147Staylor 			return (error);
14787147Staylor 		}
14797147Staylor 		if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
14807147Staylor 			spa_free_bootprop(zfs_bootfs);
14817147Staylor 			cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
14826423Sgw25295 			    error);
14836423Sgw25295 			return (error);
14846423Sgw25295 		}
14853912Slling 
14867147Staylor 		spa_free_bootprop(zfs_bootfs);
14871544Seschrock 
14881544Seschrock 		if (error = vfs_lock(vfsp))
14891544Seschrock 			return (error);
14901544Seschrock 
14917046Sahrens 		if (error = zfs_domount(vfsp, rootfs.bo_name)) {
14927147Staylor 			cmn_err(CE_NOTE, "zfs_domount: error %d", error);
14931544Seschrock 			goto out;
14946423Sgw25295 		}
14951544Seschrock 
14961544Seschrock 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
14971544Seschrock 		ASSERT(zfsvfs);
14986423Sgw25295 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
14997147Staylor 			cmn_err(CE_NOTE, "zfs_zget: error %d", error);
15001544Seschrock 			goto out;
15016423Sgw25295 		}
15021544Seschrock 
15031544Seschrock 		vp = ZTOV(zp);
15041544Seschrock 		mutex_enter(&vp->v_lock);
15051544Seschrock 		vp->v_flag |= VROOT;
15061544Seschrock 		mutex_exit(&vp->v_lock);
15071544Seschrock 		rootvp = vp;
15081544Seschrock 
15091544Seschrock 		/*
15106570Smarks 		 * Leave rootvp held.  The root file system is never unmounted.
15111544Seschrock 		 */
15121544Seschrock 
15131544Seschrock 		vfs_add((struct vnode *)0, vfsp,
15141544Seschrock 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
15151544Seschrock out:
15161544Seschrock 		vfs_unlock(vfsp);
15176423Sgw25295 		return (error);
15181544Seschrock 	} else if (why == ROOT_REMOUNT) {
15191544Seschrock 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
15201544Seschrock 		vfsp->vfs_flag |= VFS_REMOUNT;
15214596Slling 
15224596Slling 		/* refresh mount options */
15234596Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
15244596Slling 		return (zfs_register_callbacks(vfsp));
15254596Slling 
15261544Seschrock 	} else if (why == ROOT_UNMOUNT) {
15271544Seschrock 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
15281544Seschrock 		(void) zfs_sync(vfsp, 0, 0);
15291544Seschrock 		return (0);
15301544Seschrock 	}
15311544Seschrock 
15321544Seschrock 	/*
15331544Seschrock 	 * if "why" is equal to anything else other than ROOT_INIT,
15341544Seschrock 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
15351544Seschrock 	 */
15361544Seschrock 	return (ENOTSUP);
15371544Seschrock }
15381544Seschrock 
1539789Sahrens /*ARGSUSED*/
1540789Sahrens static int
zfs_mount(vfs_t * vfsp,vnode_t * mvp,struct mounta * uap,cred_t * cr)1541789Sahrens zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
1542789Sahrens {
1543789Sahrens 	char		*osname;
1544789Sahrens 	pathname_t	spn;
1545789Sahrens 	int		error = 0;
1546789Sahrens 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
15473912Slling 	    UIO_SYSSPACE : UIO_USERSPACE;
1548789Sahrens 	int		canwrite;
1549789Sahrens 
1550789Sahrens 	if (mvp->v_type != VDIR)
1551789Sahrens 		return (ENOTDIR);
1552789Sahrens 
1553789Sahrens 	mutex_enter(&mvp->v_lock);
1554789Sahrens 	if ((uap->flags & MS_REMOUNT) == 0 &&
1555789Sahrens 	    (uap->flags & MS_OVERLAY) == 0 &&
1556789Sahrens 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
1557789Sahrens 		mutex_exit(&mvp->v_lock);
1558789Sahrens 		return (EBUSY);
1559789Sahrens 	}
1560789Sahrens 	mutex_exit(&mvp->v_lock);
1561789Sahrens 
1562789Sahrens 	/*
1563789Sahrens 	 * ZFS does not support passing unparsed data in via MS_DATA.
1564789Sahrens 	 * Users should use the MS_OPTIONSTR interface; this means
1565789Sahrens 	 * that all option parsing is already done and the options struct
1566789Sahrens 	 * can be interrogated.
1567789Sahrens 	 */
1568789Sahrens 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
1569789Sahrens 		return (EINVAL);
1570789Sahrens 
1571789Sahrens 	/*
1572789Sahrens 	 * Get the objset name (the "special" mount argument).
1573789Sahrens 	 */
1574789Sahrens 	if (error = pn_get(uap->spec, fromspace, &spn))
1575789Sahrens 		return (error);
1576789Sahrens 
1577789Sahrens 	osname = spn.pn_path;
1578789Sahrens 
15794543Smarks 	/*
15804543Smarks 	 * Check for mount privilege?
15814543Smarks 	 *
15824543Smarks 	 * If we don't have privilege then see if
15834543Smarks 	 * we have local permission to allow it
15844543Smarks 	 */
15854543Smarks 	error = secpolicy_fs_mount(cr, mvp, vfsp);
15864543Smarks 	if (error) {
158711824SMark.Shellenbaum@Sun.COM 		if (dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) == 0) {
15884543Smarks 			vattr_t		vattr;
15894543Smarks 
15904543Smarks 			/*
15914543Smarks 			 * Make sure user is the owner of the mount point
15924543Smarks 			 * or has sufficient privileges.
15934543Smarks 			 */
15944543Smarks 
15954543Smarks 			vattr.va_mask = AT_UID;
15964543Smarks 
159711824SMark.Shellenbaum@Sun.COM 			if (VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
15984543Smarks 				goto out;
15994543Smarks 			}
16004543Smarks 
16015489Smarks 			if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 &&
16025489Smarks 			    VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) {
16034543Smarks 				goto out;
16044543Smarks 			}
16054543Smarks 			secpolicy_fs_mount_clearopts(cr, vfsp);
16064543Smarks 		} else {
16074543Smarks 			goto out;
16084543Smarks 		}
16094543Smarks 	}
1610789Sahrens 
1611789Sahrens 	/*
1612789Sahrens 	 * Refuse to mount a filesystem if we are in a local zone and the
1613789Sahrens 	 * dataset is not visible.
1614789Sahrens 	 */
1615789Sahrens 	if (!INGLOBALZONE(curproc) &&
1616789Sahrens 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1617789Sahrens 		error = EPERM;
1618789Sahrens 		goto out;
1619789Sahrens 	}
1620789Sahrens 
162110972SRic.Aleshire@Sun.COM 	error = zfs_mount_label_policy(vfsp, osname);
162210972SRic.Aleshire@Sun.COM 	if (error)
162310972SRic.Aleshire@Sun.COM 		goto out;
162410972SRic.Aleshire@Sun.COM 
16254596Slling 	/*
16264596Slling 	 * When doing a remount, we simply refresh our temporary properties
16274596Slling 	 * according to those options set in the current VFS options.
16284596Slling 	 */
16294596Slling 	if (uap->flags & MS_REMOUNT) {
16304596Slling 		/* refresh mount options */
16314596Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
16324596Slling 		error = zfs_register_callbacks(vfsp);
16334596Slling 		goto out;
16344596Slling 	}
16354596Slling 
16367046Sahrens 	error = zfs_domount(vfsp, osname);
1637789Sahrens 
16389214Schris.kirby@sun.com 	/*
16399214Schris.kirby@sun.com 	 * Add an extra VFS_HOLD on our parent vfs so that it can't
16409214Schris.kirby@sun.com 	 * disappear due to a forced unmount.
16419214Schris.kirby@sun.com 	 */
16429246Schris.kirby@sun.com 	if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap)
16439214Schris.kirby@sun.com 		VFS_HOLD(mvp->v_vfsp);
16449214Schris.kirby@sun.com 
1645789Sahrens out:
1646789Sahrens 	pn_free(&spn);
1647789Sahrens 	return (error);
1648789Sahrens }
1649789Sahrens 
1650789Sahrens static int
zfs_statvfs(vfs_t * vfsp,struct statvfs64 * statp)1651789Sahrens zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1652789Sahrens {
1653789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1654789Sahrens 	dev32_t d32;
16552885Sahrens 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
1656789Sahrens 
1657789Sahrens 	ZFS_ENTER(zfsvfs);
1658789Sahrens 
16592885Sahrens 	dmu_objset_space(zfsvfs->z_os,
16602885Sahrens 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
1661789Sahrens 
1662789Sahrens 	/*
1663789Sahrens 	 * The underlying storage pool actually uses multiple block sizes.
1664789Sahrens 	 * We report the fragsize as the smallest block size we support,
1665789Sahrens 	 * and we report our blocksize as the filesystem's maximum blocksize.
1666789Sahrens 	 */
1667789Sahrens 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1668789Sahrens 	statp->f_bsize = zfsvfs->z_max_blksz;
1669789Sahrens 
1670789Sahrens 	/*
1671789Sahrens 	 * The following report "total" blocks of various kinds in the
1672789Sahrens 	 * file system, but reported in terms of f_frsize - the
1673789Sahrens 	 * "fragment" size.
1674789Sahrens 	 */
1675789Sahrens 
16762885Sahrens 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
16772885Sahrens 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
1678789Sahrens 	statp->f_bavail = statp->f_bfree; /* no root reservation */
1679789Sahrens 
1680789Sahrens 	/*
1681789Sahrens 	 * statvfs() should really be called statufs(), because it assumes
1682789Sahrens 	 * static metadata.  ZFS doesn't preallocate files, so the best
1683789Sahrens 	 * we can do is report the max that could possibly fit in f_files,
1684789Sahrens 	 * and that minus the number actually used in f_ffree.
1685789Sahrens 	 * For f_ffree, report the smaller of the number of object available
1686789Sahrens 	 * and the number of blocks (each object will take at least a block).
1687789Sahrens 	 */
16882885Sahrens 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
1689789Sahrens 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
16902885Sahrens 	statp->f_files = statp->f_ffree + usedobjs;
1691789Sahrens 
1692789Sahrens 	(void) cmpldev(&d32, vfsp->vfs_dev);
1693789Sahrens 	statp->f_fsid = d32;
1694789Sahrens 
1695789Sahrens 	/*
1696789Sahrens 	 * We're a zfs filesystem.
1697789Sahrens 	 */
1698789Sahrens 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1699789Sahrens 
17001123Smarks 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1701789Sahrens 
1702789Sahrens 	statp->f_namemax = ZFS_MAXNAMELEN;
1703789Sahrens 
1704789Sahrens 	/*
1705789Sahrens 	 * We have all of 32 characters to stuff a string here.
1706789Sahrens 	 * Is there anything useful we could/should provide?
1707789Sahrens 	 */
1708789Sahrens 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
1709789Sahrens 
1710789Sahrens 	ZFS_EXIT(zfsvfs);
1711789Sahrens 	return (0);
1712789Sahrens }
1713789Sahrens 
1714789Sahrens static int
zfs_root(vfs_t * vfsp,vnode_t ** vpp)1715789Sahrens zfs_root(vfs_t *vfsp, vnode_t **vpp)
1716789Sahrens {
1717789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1718789Sahrens 	znode_t *rootzp;
1719789Sahrens 	int error;
1720789Sahrens 
1721789Sahrens 	ZFS_ENTER(zfsvfs);
1722789Sahrens 
1723789Sahrens 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1724789Sahrens 	if (error == 0)
1725789Sahrens 		*vpp = ZTOV(rootzp);
1726789Sahrens 
1727789Sahrens 	ZFS_EXIT(zfsvfs);
1728789Sahrens 	return (error);
1729789Sahrens }
1730789Sahrens 
17315326Sek110237 /*
17325326Sek110237  * Teardown the zfsvfs::z_os.
17335326Sek110237  *
17345326Sek110237  * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
17355326Sek110237  * and 'z_teardown_inactive_lock' held.
17365326Sek110237  */
17375326Sek110237 static int
zfsvfs_teardown(zfsvfs_t * zfsvfs,boolean_t unmounting)17385326Sek110237 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
17395326Sek110237 {
17405642Smaybee 	znode_t	*zp;
17415326Sek110237 
17425326Sek110237 	rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
17435326Sek110237 
17445326Sek110237 	if (!unmounting) {
17455326Sek110237 		/*
17465326Sek110237 		 * We purge the parent filesystem's vfsp as the parent
17475326Sek110237 		 * filesystem and all of its snapshots have their vnode's
17485326Sek110237 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
17495326Sek110237 		 * 'z_parent' is self referential for non-snapshots.
17505326Sek110237 		 */
17515326Sek110237 		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
17525326Sek110237 	}
17535326Sek110237 
17545326Sek110237 	/*
17555326Sek110237 	 * Close the zil. NB: Can't close the zil while zfs_inactive
17565326Sek110237 	 * threads are blocked as zil_close can call zfs_inactive.
17575326Sek110237 	 */
17585326Sek110237 	if (zfsvfs->z_log) {
17595326Sek110237 		zil_close(zfsvfs->z_log);
17605326Sek110237 		zfsvfs->z_log = NULL;
17615326Sek110237 	}
17625326Sek110237 
17635326Sek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
17645326Sek110237 
17655326Sek110237 	/*
17665326Sek110237 	 * If we are not unmounting (ie: online recv) and someone already
17675326Sek110237 	 * unmounted this file system while we were doing the switcheroo,
17685326Sek110237 	 * or a reopen of z_os failed then just bail out now.
17695326Sek110237 	 */
17705326Sek110237 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
17715326Sek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
17725326Sek110237 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
17735326Sek110237 		return (EIO);
17745326Sek110237 	}
17755326Sek110237 
17765326Sek110237 	/*
17775326Sek110237 	 * At this point there are no vops active, and any new vops will
17785326Sek110237 	 * fail with EIO since we have z_teardown_lock for writer (only
17795326Sek110237 	 * relavent for forced unmount).
17805326Sek110237 	 *
17815326Sek110237 	 * Release all holds on dbufs.
17825326Sek110237 	 */
17835326Sek110237 	mutex_enter(&zfsvfs->z_znodes_lock);
17845642Smaybee 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
17855642Smaybee 	    zp = list_next(&zfsvfs->z_all_znodes, zp))
178611935SMark.Shellenbaum@Sun.COM 		if (zp->z_sa_hdl) {
17875642Smaybee 			ASSERT(ZTOV(zp)->v_count > 0);
17885642Smaybee 			zfs_znode_dmu_fini(zp);
17895326Sek110237 		}
17905326Sek110237 	mutex_exit(&zfsvfs->z_znodes_lock);
17915326Sek110237 
17925326Sek110237 	/*
17935326Sek110237 	 * If we are unmounting, set the unmounted flag and let new vops
17945326Sek110237 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
17955326Sek110237 	 * other vops will fail with EIO.
17965326Sek110237 	 */
17975326Sek110237 	if (unmounting) {
17985326Sek110237 		zfsvfs->z_unmounted = B_TRUE;
17995326Sek110237 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
18005326Sek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
18015326Sek110237 	}
18025326Sek110237 
18035326Sek110237 	/*
18045326Sek110237 	 * z_os will be NULL if there was an error in attempting to reopen
18055326Sek110237 	 * zfsvfs, so just return as the properties had already been
18065326Sek110237 	 * unregistered and cached data had been evicted before.
18075326Sek110237 	 */
18085326Sek110237 	if (zfsvfs->z_os == NULL)
18095326Sek110237 		return (0);
18105326Sek110237 
18115326Sek110237 	/*
18125326Sek110237 	 * Unregister properties.
18135326Sek110237 	 */
18145326Sek110237 	zfs_unregister_callbacks(zfsvfs);
18155326Sek110237 
18165326Sek110237 	/*
18175326Sek110237 	 * Evict cached data
18185326Sek110237 	 */
181912722SSanjeev.Bagewadi@Sun.COM 	if (dmu_objset_is_dirty_anywhere(zfsvfs->z_os))
182013049SGeorge.Wilson@Sun.COM 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY))
182113049SGeorge.Wilson@Sun.COM 			txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
182212722SSanjeev.Bagewadi@Sun.COM 	(void) dmu_objset_evict_dbufs(zfsvfs->z_os);
18235326Sek110237 
18245326Sek110237 	return (0);
18255326Sek110237 }
18265326Sek110237 
1827789Sahrens /*ARGSUSED*/
1828789Sahrens static int
zfs_umount(vfs_t * vfsp,int fflag,cred_t * cr)1829789Sahrens zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1830789Sahrens {
1831789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
18325326Sek110237 	objset_t *os;
1833789Sahrens 	int ret;
1834789Sahrens 
18354543Smarks 	ret = secpolicy_fs_unmount(cr, vfsp);
18364543Smarks 	if (ret) {
183711824SMark.Shellenbaum@Sun.COM 		if (dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
183811824SMark.Shellenbaum@Sun.COM 		    ZFS_DELEG_PERM_MOUNT, cr))
18394543Smarks 			return (ret);
18404543Smarks 	}
18411484Sek110237 
18424736Sek110237 	/*
18434736Sek110237 	 * We purge the parent filesystem's vfsp as the parent filesystem
18444736Sek110237 	 * and all of its snapshots have their vnode's v_vfsp set to the
18454736Sek110237 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
18464736Sek110237 	 * referential for non-snapshots.
18474736Sek110237 	 */
18484736Sek110237 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
18491484Sek110237 
1850789Sahrens 	/*
1851789Sahrens 	 * Unmount any snapshots mounted under .zfs before unmounting the
1852789Sahrens 	 * dataset itself.
1853789Sahrens 	 */
1854789Sahrens 	if (zfsvfs->z_ctldir != NULL &&
18554543Smarks 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1856789Sahrens 		return (ret);
18574543Smarks 	}
1858789Sahrens 
18594787Sahrens 	if (!(fflag & MS_FORCE)) {
18604480Sgw25295 		/*
18614787Sahrens 		 * Check the number of active vnodes in the file system.
18624787Sahrens 		 * Our count is maintained in the vfs structure, but the
18634787Sahrens 		 * number is off by 1 to indicate a hold on the vfs
18644787Sahrens 		 * structure itself.
18654787Sahrens 		 *
18664787Sahrens 		 * The '.zfs' directory maintains a reference of its
18674787Sahrens 		 * own, and any active references underneath are
18684787Sahrens 		 * reflected in the vnode count.
1869789Sahrens 		 */
18704787Sahrens 		if (zfsvfs->z_ctldir == NULL) {
18714787Sahrens 			if (vfsp->vfs_count > 1)
18724787Sahrens 				return (EBUSY);
18734787Sahrens 		} else {
18744787Sahrens 			if (vfsp->vfs_count > 2 ||
18755326Sek110237 			    zfsvfs->z_ctldir->v_count > 1)
18764787Sahrens 				return (EBUSY);
1877789Sahrens 		}
1878789Sahrens 	}
1879789Sahrens 
1880789Sahrens 	vfsp->vfs_flag |= VFS_UNMOUNTED;
18814787Sahrens 
18825326Sek110237 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
18835326Sek110237 	os = zfsvfs->z_os;
18844787Sahrens 
18854787Sahrens 	/*
18865326Sek110237 	 * z_os will be NULL if there was an error in
18875326Sek110237 	 * attempting to reopen zfsvfs.
18884787Sahrens 	 */
18895326Sek110237 	if (os != NULL) {
18905326Sek110237 		/*
18915326Sek110237 		 * Unset the objset user_ptr.
18925326Sek110237 		 */
189310298SMatthew.Ahrens@Sun.COM 		mutex_enter(&os->os_user_ptr_lock);
18945326Sek110237 		dmu_objset_set_user(os, NULL);
189510298SMatthew.Ahrens@Sun.COM 		mutex_exit(&os->os_user_ptr_lock);
18964787Sahrens 
18975326Sek110237 		/*
18986689Smaybee 		 * Finally release the objset
18995326Sek110237 		 */
190010298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, zfsvfs);
19014787Sahrens 	}
19024787Sahrens 
19034787Sahrens 	/*
19044787Sahrens 	 * We can now safely destroy the '.zfs' directory node.
19054787Sahrens 	 */
19064787Sahrens 	if (zfsvfs->z_ctldir != NULL)
19074787Sahrens 		zfsctl_destroy(zfsvfs);
1908789Sahrens 
1909789Sahrens 	return (0);
1910789Sahrens }
1911789Sahrens 
1912789Sahrens static int
zfs_vget(vfs_t * vfsp,vnode_t ** vpp,fid_t * fidp)1913789Sahrens zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1914789Sahrens {
1915789Sahrens 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1916789Sahrens 	znode_t		*zp;
1917789Sahrens 	uint64_t	object = 0;
1918789Sahrens 	uint64_t	fid_gen = 0;
1919789Sahrens 	uint64_t	gen_mask;
1920789Sahrens 	uint64_t	zp_gen;
1921789Sahrens 	int 		i, err;
1922789Sahrens 
1923789Sahrens 	*vpp = NULL;
1924789Sahrens 
1925789Sahrens 	ZFS_ENTER(zfsvfs);
1926789Sahrens 
1927789Sahrens 	if (fidp->fid_len == LONG_FID_LEN) {
1928789Sahrens 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1929789Sahrens 		uint64_t	objsetid = 0;
1930789Sahrens 		uint64_t	setgen = 0;
1931789Sahrens 
1932789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1933789Sahrens 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1934789Sahrens 
1935789Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1936789Sahrens 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1937789Sahrens 
1938789Sahrens 		ZFS_EXIT(zfsvfs);
1939789Sahrens 
1940789Sahrens 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1941789Sahrens 		if (err)
1942789Sahrens 			return (EINVAL);
1943789Sahrens 		ZFS_ENTER(zfsvfs);
1944789Sahrens 	}
1945789Sahrens 
1946789Sahrens 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1947789Sahrens 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1948789Sahrens 
1949789Sahrens 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1950789Sahrens 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1951789Sahrens 
1952789Sahrens 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1953789Sahrens 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1954789Sahrens 	} else {
1955789Sahrens 		ZFS_EXIT(zfsvfs);
1956789Sahrens 		return (EINVAL);
1957789Sahrens 	}
1958789Sahrens 
1959789Sahrens 	/* A zero fid_gen means we are in the .zfs control directories */
1960789Sahrens 	if (fid_gen == 0 &&
1961789Sahrens 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1962789Sahrens 		*vpp = zfsvfs->z_ctldir;
1963789Sahrens 		ASSERT(*vpp != NULL);
1964789Sahrens 		if (object == ZFSCTL_INO_SNAPDIR) {
1965789Sahrens 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
19665331Samw 			    0, NULL, NULL, NULL, NULL, NULL) == 0);
1967789Sahrens 		} else {
1968789Sahrens 			VN_HOLD(*vpp);
1969789Sahrens 		}
1970789Sahrens 		ZFS_EXIT(zfsvfs);
1971789Sahrens 		return (0);
1972789Sahrens 	}
1973789Sahrens 
1974789Sahrens 	gen_mask = -1ULL >> (64 - 8 * i);
1975789Sahrens 
1976789Sahrens 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1977789Sahrens 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1978789Sahrens 		ZFS_EXIT(zfsvfs);
1979789Sahrens 		return (err);
1980789Sahrens 	}
198111935SMark.Shellenbaum@Sun.COM 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
198211935SMark.Shellenbaum@Sun.COM 	    sizeof (uint64_t));
198311935SMark.Shellenbaum@Sun.COM 	zp_gen = zp_gen & gen_mask;
1984789Sahrens 	if (zp_gen == 0)
1985789Sahrens 		zp_gen = 1;
19863461Sahrens 	if (zp->z_unlinked || zp_gen != fid_gen) {
1987789Sahrens 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1988789Sahrens 		VN_RELE(ZTOV(zp));
1989789Sahrens 		ZFS_EXIT(zfsvfs);
1990789Sahrens 		return (EINVAL);
1991789Sahrens 	}
1992789Sahrens 
1993789Sahrens 	*vpp = ZTOV(zp);
1994789Sahrens 	ZFS_EXIT(zfsvfs);
1995789Sahrens 	return (0);
1996789Sahrens }
1997789Sahrens 
19985326Sek110237 /*
19995326Sek110237  * Block out VOPs and close zfsvfs_t::z_os
20005326Sek110237  *
20015326Sek110237  * Note, if successful, then we return with the 'z_teardown_lock' and
20025326Sek110237  * 'z_teardown_inactive_lock' write held.
20035326Sek110237  */
20045326Sek110237 int
zfs_suspend_fs(zfsvfs_t * zfsvfs)200510298SMatthew.Ahrens@Sun.COM zfs_suspend_fs(zfsvfs_t *zfsvfs)
20065326Sek110237 {
20075326Sek110237 	int error;
20085326Sek110237 
20095326Sek110237 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
20105326Sek110237 		return (error);
201110298SMatthew.Ahrens@Sun.COM 	dmu_objset_disown(zfsvfs->z_os, zfsvfs);
20125326Sek110237 
20135326Sek110237 	return (0);
20145326Sek110237 }
20155326Sek110237 
20165326Sek110237 /*
20175326Sek110237  * Reopen zfsvfs_t::z_os and release VOPs.
20185326Sek110237  */
20195326Sek110237 int
zfs_resume_fs(zfsvfs_t * zfsvfs,const char * osname)202010298SMatthew.Ahrens@Sun.COM zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname)
20215326Sek110237 {
2022*13147SMark.Shellenbaum@Oracle.COM 	int err;
20235326Sek110237 
20245326Sek110237 	ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
20255326Sek110237 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
20265326Sek110237 
202710298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_own(osname, DMU_OST_ZFS, B_FALSE, zfsvfs,
202810298SMatthew.Ahrens@Sun.COM 	    &zfsvfs->z_os);
20295326Sek110237 	if (err) {
20305326Sek110237 		zfsvfs->z_os = NULL;
20315326Sek110237 	} else {
20325326Sek110237 		znode_t *zp;
203311935SMark.Shellenbaum@Sun.COM 		uint64_t sa_obj = 0;
203411935SMark.Shellenbaum@Sun.COM 
2035*13147SMark.Shellenbaum@Oracle.COM 		/*
2036*13147SMark.Shellenbaum@Oracle.COM 		 * Make sure version hasn't changed
2037*13147SMark.Shellenbaum@Oracle.COM 		 */
2038*13147SMark.Shellenbaum@Oracle.COM 
2039*13147SMark.Shellenbaum@Oracle.COM 		err = zfs_get_zplprop(zfsvfs->z_os, ZFS_PROP_VERSION,
2040*13147SMark.Shellenbaum@Oracle.COM 		    &zfsvfs->z_version);
2041*13147SMark.Shellenbaum@Oracle.COM 
2042*13147SMark.Shellenbaum@Oracle.COM 		if (err)
2043*13147SMark.Shellenbaum@Oracle.COM 			goto bail;
2044*13147SMark.Shellenbaum@Oracle.COM 
2045*13147SMark.Shellenbaum@Oracle.COM 		err = zap_lookup(zfsvfs->z_os, MASTER_NODE_OBJ,
204611935SMark.Shellenbaum@Sun.COM 		    ZFS_SA_ATTRS, 8, 1, &sa_obj);
204711935SMark.Shellenbaum@Sun.COM 
2048*13147SMark.Shellenbaum@Oracle.COM 		if (err && zfsvfs->z_version >= ZPL_VERSION_SA)
204911935SMark.Shellenbaum@Sun.COM 			goto bail;
205011935SMark.Shellenbaum@Sun.COM 
205112493SMark.Shellenbaum@Oracle.COM 		if ((err = sa_setup(zfsvfs->z_os, sa_obj,
205212493SMark.Shellenbaum@Oracle.COM 		    zfs_attr_table,  ZPL_END, &zfsvfs->z_attr_table)) != 0)
205312493SMark.Shellenbaum@Oracle.COM 			goto bail;
20545326Sek110237 
2055*13147SMark.Shellenbaum@Oracle.COM 		if (zfsvfs->z_version >= ZPL_VERSION_SA)
2056*13147SMark.Shellenbaum@Oracle.COM 			sa_register_update_callback(zfsvfs->z_os,
2057*13147SMark.Shellenbaum@Oracle.COM 			    zfs_sa_upgrade);
2058*13147SMark.Shellenbaum@Oracle.COM 
20595326Sek110237 		VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
20605326Sek110237 
2061*13147SMark.Shellenbaum@Oracle.COM 		zfs_set_fuid_feature(zfsvfs);
2062*13147SMark.Shellenbaum@Oracle.COM 
20635326Sek110237 		/*
20645326Sek110237 		 * Attempt to re-establish all the active znodes with
20655326Sek110237 		 * their dbufs.  If a zfs_rezget() fails, then we'll let
20665326Sek110237 		 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
20675326Sek110237 		 * when they try to use their znode.
20685326Sek110237 		 */
20695326Sek110237 		mutex_enter(&zfsvfs->z_znodes_lock);
20705326Sek110237 		for (zp = list_head(&zfsvfs->z_all_znodes); zp;
20715326Sek110237 		    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
20725326Sek110237 			(void) zfs_rezget(zp);
20735326Sek110237 		}
20745326Sek110237 		mutex_exit(&zfsvfs->z_znodes_lock);
20755326Sek110237 	}
20765326Sek110237 
207711935SMark.Shellenbaum@Sun.COM bail:
20785326Sek110237 	/* release the VOPs */
20795326Sek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
20805326Sek110237 	rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
20815326Sek110237 
20825326Sek110237 	if (err) {
20835326Sek110237 		/*
2084*13147SMark.Shellenbaum@Oracle.COM 		 * Since we couldn't reopen zfsvfs::z_os, or
2085*13147SMark.Shellenbaum@Oracle.COM 		 * setup the sa framework force unmount this file system.
20865326Sek110237 		 */
20875326Sek110237 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
20885326Sek110237 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
20895326Sek110237 	}
20905326Sek110237 	return (err);
20915326Sek110237 }
20925326Sek110237 
2093789Sahrens static void
zfs_freevfs(vfs_t * vfsp)2094789Sahrens zfs_freevfs(vfs_t *vfsp)
2095789Sahrens {
2096789Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
20979214Schris.kirby@sun.com 
20989214Schris.kirby@sun.com 	/*
20999214Schris.kirby@sun.com 	 * If this is a snapshot, we have an extra VFS_HOLD on our parent
210012095SChris.Kirby@sun.com 	 * from zfs_mount().  Release it here.  If we came through
210112095SChris.Kirby@sun.com 	 * zfs_mountroot() instead, we didn't grab an extra hold, so
210212095SChris.Kirby@sun.com 	 * skip the VFS_RELE for rootvfs.
21039214Schris.kirby@sun.com 	 */
210412095SChris.Kirby@sun.com 	if (zfsvfs->z_issnap && (vfsp != rootvfs))
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
zfs_vfsinit(int fstype,char * name)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
zfs_init(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
zfs_fini(void)2171789Sahrens zfs_fini(void)
2172789Sahrens {
2173789Sahrens 	zfsctl_fini();
2174789Sahrens 	zfs_znode_fini();
2175789Sahrens }
2176789Sahrens 
2177789Sahrens int
zfs_busy(void)2178789Sahrens zfs_busy(void)
2179789Sahrens {
2180789Sahrens 	return (zfs_active_fs_count != 0);
2181789Sahrens }
2182789Sahrens 
21834577Sahrens int
zfs_set_version(zfsvfs_t * zfsvfs,uint64_t newvers)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 
223712296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_DS_UPGRADE,
223812296SLin.Ling@Sun.COM 	    dmu_objset_spa(os), tx, "oldver=%llu newver=%llu dataset = %llu",
22399396SMatthew.Ahrens@Sun.COM 	    zfsvfs->z_version, newvers, dmu_objset_id(os));
22409396SMatthew.Ahrens@Sun.COM 
22414577Sahrens 	dmu_tx_commit(tx);
22424577Sahrens 
22439396SMatthew.Ahrens@Sun.COM 	zfsvfs->z_version = newvers;
22449396SMatthew.Ahrens@Sun.COM 
2245*13147SMark.Shellenbaum@Oracle.COM 	zfs_set_fuid_feature(zfsvfs);
22469396SMatthew.Ahrens@Sun.COM 
22479396SMatthew.Ahrens@Sun.COM 	return (0);
22484577Sahrens }
22494577Sahrens 
22505498Stimh /*
22515498Stimh  * Read a property stored within the master node.
22525498Stimh  */
22535498Stimh int
zfs_get_zplprop(objset_t * os,zfs_prop_t prop,uint64_t * value)22545498Stimh zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
22555498Stimh {
22565498Stimh 	const char *pname;
22577184Stimh 	int error = ENOENT;
22585498Stimh 
22595498Stimh 	/*
22605498Stimh 	 * Look up the file system's value for the property.  For the
22615498Stimh 	 * version property, we look up a slightly different string.
22625498Stimh 	 */
22635498Stimh 	if (prop == ZFS_PROP_VERSION)
22645498Stimh 		pname = ZPL_VERSION_STR;
22655498Stimh 	else
22665498Stimh 		pname = zfs_prop_to_name(prop);
22675498Stimh 
22687184Stimh 	if (os != NULL)
22697184Stimh 		error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
22705498Stimh 
22716404Smaybee 	if (error == ENOENT) {
22725498Stimh 		/* No value set, use the default value */
22735498Stimh 		switch (prop) {
22746404Smaybee 		case ZFS_PROP_VERSION:
22756404Smaybee 			*value = ZPL_VERSION;
22766404Smaybee 			break;
22775498Stimh 		case ZFS_PROP_NORMALIZE:
22785498Stimh 		case ZFS_PROP_UTF8ONLY:
22795498Stimh 			*value = 0;
22805498Stimh 			break;
22815498Stimh 		case ZFS_PROP_CASE:
22825498Stimh 			*value = ZFS_CASE_SENSITIVE;
22835498Stimh 			break;
22845498Stimh 		default:
22856404Smaybee 			return (error);
22865498Stimh 		}
22876404Smaybee 		error = 0;
22885498Stimh 	}
22896404Smaybee 	return (error);
22905498Stimh }
22915498Stimh 
2292789Sahrens static vfsdef_t vfw = {
2293789Sahrens 	VFSDEF_VERSION,
2294789Sahrens 	MNTTYPE_ZFS,
2295789Sahrens 	zfs_vfsinit,
22965331Samw 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS|
229712633Sjohn.levon@sun.com 	    VSW_XID|VSW_ZMOUNT,
2298789Sahrens 	&zfs_mntopts
2299789Sahrens };
2300789Sahrens 
2301789Sahrens struct modlfs zfs_modlfs = {
23024577Sahrens 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
2303789Sahrens };
2304