xref: /titanic_41/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 675fc291908baceb17f92b0b6d961439aaddafc9)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5033f9833Sek110237  * Common Development and Distribution License (the "License").
6033f9833Sek110237  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22dc7cd546SMark Shellenbaum  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23*675fc291SMatthew Ahrens  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
2655da60b9SMark J Musante /* Portions Copyright 2010 Robert Milkowski */
2755da60b9SMark J Musante 
28fa9e4066Sahrens #include <sys/types.h>
29fa9e4066Sahrens #include <sys/param.h>
30fa9e4066Sahrens #include <sys/systm.h>
31fa9e4066Sahrens #include <sys/sysmacros.h>
32fa9e4066Sahrens #include <sys/kmem.h>
33fa9e4066Sahrens #include <sys/pathname.h>
34fa9e4066Sahrens #include <sys/vnode.h>
35fa9e4066Sahrens #include <sys/vfs.h>
36aa59c4cbSrsb #include <sys/vfs_opreg.h>
37fa9e4066Sahrens #include <sys/mntent.h>
38fa9e4066Sahrens #include <sys/mount.h>
39fa9e4066Sahrens #include <sys/cmn_err.h>
40fa9e4066Sahrens #include "fs/fs_subr.h"
41fa9e4066Sahrens #include <sys/zfs_znode.h>
42893a6d32Sahrens #include <sys/zfs_dir.h>
43fa9e4066Sahrens #include <sys/zil.h>
44fa9e4066Sahrens #include <sys/fs/zfs.h>
45fa9e4066Sahrens #include <sys/dmu.h>
46fa9e4066Sahrens #include <sys/dsl_prop.h>
47b1b8ab34Slling #include <sys/dsl_dataset.h>
48ecd6cf80Smarks #include <sys/dsl_deleg.h>
49fa9e4066Sahrens #include <sys/spa.h>
50fa9e4066Sahrens #include <sys/zap.h>
510a586ceaSMark Shellenbaum #include <sys/sa.h>
52e828a46dSMatthew Ahrens #include <sys/sa_impl.h>
53fa9e4066Sahrens #include <sys/varargs.h>
54fa9e4066Sahrens #include <sys/policy.h>
55fa9e4066Sahrens #include <sys/atomic.h>
56fa9e4066Sahrens #include <sys/mkdev.h>
57fa9e4066Sahrens #include <sys/modctl.h>
58ecd6cf80Smarks #include <sys/refstr.h>
59fa9e4066Sahrens #include <sys/zfs_ioctl.h>
60fa9e4066Sahrens #include <sys/zfs_ctldir.h>
61da6c28aaSamw #include <sys/zfs_fuid.h>
62ea8dc4b6Seschrock #include <sys/bootconf.h>
63a0965f35Sbonwick #include <sys/sunddi.h>
64033f9833Sek110237 #include <sys/dnlc.h>
65f18faf3fSek110237 #include <sys/dmu_objset.h>
66e7cbe64fSgw25295 #include <sys/spa_boot.h>
67aa075980SAndreas Jaekel #include <sys/zfs_events.h>
680a586ceaSMark Shellenbaum #include "zfs_comutil.h"
69fa9e4066Sahrens 
70fa9e4066Sahrens int zfsfstype;
71fa9e4066Sahrens vfsops_t *zfs_vfsops = NULL;
72fa9e4066Sahrens static major_t zfs_major;
73fa9e4066Sahrens static minor_t zfs_minor;
74fa9e4066Sahrens static kmutex_t	zfs_dev_mtx;
75fa9e4066Sahrens 
7654d692b7SGeorge Wilson extern int sys_shutdown;
7754d692b7SGeorge Wilson 
78fa9e4066Sahrens static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
79fa9e4066Sahrens static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr);
80ea8dc4b6Seschrock static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
81fa9e4066Sahrens static int zfs_root(vfs_t *vfsp, vnode_t **vpp);
82fa9e4066Sahrens static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp);
83fa9e4066Sahrens static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp);
84fa9e4066Sahrens static void zfs_freevfs(vfs_t *vfsp);
85fa9e4066Sahrens 
86fa9e4066Sahrens static const fs_operation_def_t zfs_vfsops_template[] = {
87aa59c4cbSrsb 	VFSNAME_MOUNT,		{ .vfs_mount = zfs_mount },
88aa59c4cbSrsb 	VFSNAME_MOUNTROOT,	{ .vfs_mountroot = zfs_mountroot },
89aa59c4cbSrsb 	VFSNAME_UNMOUNT,	{ .vfs_unmount = zfs_umount },
90aa59c4cbSrsb 	VFSNAME_ROOT,		{ .vfs_root = zfs_root },
91aa59c4cbSrsb 	VFSNAME_STATVFS,	{ .vfs_statvfs = zfs_statvfs },
92aa59c4cbSrsb 	VFSNAME_SYNC,		{ .vfs_sync = zfs_sync },
93aa59c4cbSrsb 	VFSNAME_VGET,		{ .vfs_vget = zfs_vget },
94aa59c4cbSrsb 	VFSNAME_FREEVFS,	{ .vfs_freevfs = zfs_freevfs },
95fa9e4066Sahrens 	NULL,			NULL
96fa9e4066Sahrens };
97fa9e4066Sahrens 
98fa9e4066Sahrens static const fs_operation_def_t zfs_vfsops_eio_template[] = {
99aa59c4cbSrsb 	VFSNAME_FREEVFS,	{ .vfs_freevfs =  zfs_freevfs },
100fa9e4066Sahrens 	NULL,			NULL
101fa9e4066Sahrens };
102fa9e4066Sahrens 
103fa9e4066Sahrens /*
104fa9e4066Sahrens  * We need to keep a count of active fs's.
105fa9e4066Sahrens  * This is necessary to prevent our module
106fa9e4066Sahrens  * from being unloaded after a umount -f
107fa9e4066Sahrens  */
108fa9e4066Sahrens static uint32_t	zfs_active_fs_count = 0;
109fa9e4066Sahrens 
110fa9e4066Sahrens static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
111fa9e4066Sahrens static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
1127b55fa8eSck153898 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
1137b55fa8eSck153898 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
114fa9e4066Sahrens 
1157b55fa8eSck153898 /*
116b510d378Slling  * MO_DEFAULT is not used since the default value is determined
117b510d378Slling  * by the equivalent property.
1187b55fa8eSck153898  */
119fa9e4066Sahrens static mntopt_t mntopts[] = {
1207b55fa8eSck153898 	{ MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL },
1217b55fa8eSck153898 	{ MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL },
122b510d378Slling 	{ MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL },
123fa9e4066Sahrens 	{ MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
124fa9e4066Sahrens };
125fa9e4066Sahrens 
126fa9e4066Sahrens static mntopts_t zfs_mntopts = {
127fa9e4066Sahrens 	sizeof (mntopts) / sizeof (mntopt_t),
128fa9e4066Sahrens 	mntopts
129fa9e4066Sahrens };
130fa9e4066Sahrens 
131fa9e4066Sahrens /*ARGSUSED*/
132fa9e4066Sahrens int
zfs_sync(vfs_t * vfsp,short flag,cred_t * cr)133fa9e4066Sahrens zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
134fa9e4066Sahrens {
135fa9e4066Sahrens 	/*
136fa9e4066Sahrens 	 * Data integrity is job one.  We don't want a compromised kernel
137fa9e4066Sahrens 	 * writing to the storage pool, so we never sync during panic.
138fa9e4066Sahrens 	 */
139fa9e4066Sahrens 	if (panicstr)
140fa9e4066Sahrens 		return (0);
141fa9e4066Sahrens 
142fa9e4066Sahrens 	/*
143fa9e4066Sahrens 	 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
144fa9e4066Sahrens 	 * to sync metadata, which they would otherwise cache indefinitely.
145fa9e4066Sahrens 	 * Semantically, the only requirement is that the sync be initiated.
146fa9e4066Sahrens 	 * The DMU syncs out txgs frequently, so there's nothing to do.
147fa9e4066Sahrens 	 */
148fa9e4066Sahrens 	if (flag & SYNC_ATTR)
149fa9e4066Sahrens 		return (0);
150fa9e4066Sahrens 
151fa9e4066Sahrens 	if (vfsp != NULL) {
152fa9e4066Sahrens 		/*
153fa9e4066Sahrens 		 * Sync a specific filesystem.
154fa9e4066Sahrens 		 */
155fa9e4066Sahrens 		zfsvfs_t *zfsvfs = vfsp->vfs_data;
15654d692b7SGeorge Wilson 		dsl_pool_t *dp;
157fa9e4066Sahrens 
158fa9e4066Sahrens 		ZFS_ENTER(zfsvfs);
15954d692b7SGeorge Wilson 		dp = dmu_objset_pool(zfsvfs->z_os);
16054d692b7SGeorge Wilson 
16154d692b7SGeorge Wilson 		/*
16254d692b7SGeorge Wilson 		 * If the system is shutting down, then skip any
16354d692b7SGeorge Wilson 		 * filesystems which may exist on a suspended pool.
16454d692b7SGeorge Wilson 		 */
16554d692b7SGeorge Wilson 		if (sys_shutdown && spa_suspended(dp->dp_spa)) {
16654d692b7SGeorge Wilson 			ZFS_EXIT(zfsvfs);
16754d692b7SGeorge Wilson 			return (0);
16854d692b7SGeorge Wilson 		}
16954d692b7SGeorge Wilson 
170fa9e4066Sahrens 		if (zfsvfs->z_log != NULL)
1715002558fSNeil Perrin 			zil_commit(zfsvfs->z_log, 0);
17255da60b9SMark J Musante 
173fa9e4066Sahrens 		ZFS_EXIT(zfsvfs);
174fa9e4066Sahrens 	} else {
175fa9e4066Sahrens 		/*
176fa9e4066Sahrens 		 * Sync all ZFS filesystems.  This is what happens when you
177fa9e4066Sahrens 		 * run sync(1M).  Unlike other filesystems, ZFS honors the
178fa9e4066Sahrens 		 * request by waiting for all pools to commit all dirty data.
179fa9e4066Sahrens 		 */
180fa9e4066Sahrens 		spa_sync_allpools();
181fa9e4066Sahrens 	}
182fa9e4066Sahrens 
183fa9e4066Sahrens 	return (0);
184fa9e4066Sahrens }
185fa9e4066Sahrens 
186ea8dc4b6Seschrock static int
zfs_create_unique_device(dev_t * dev)187ea8dc4b6Seschrock zfs_create_unique_device(dev_t *dev)
188ea8dc4b6Seschrock {
189ea8dc4b6Seschrock 	major_t new_major;
190ea8dc4b6Seschrock 
191ea8dc4b6Seschrock 	do {
192ea8dc4b6Seschrock 		ASSERT3U(zfs_minor, <=, MAXMIN32);
193ea8dc4b6Seschrock 		minor_t start = zfs_minor;
194ea8dc4b6Seschrock 		do {
195ea8dc4b6Seschrock 			mutex_enter(&zfs_dev_mtx);
196ea8dc4b6Seschrock 			if (zfs_minor >= MAXMIN32) {
197ea8dc4b6Seschrock 				/*
198ea8dc4b6Seschrock 				 * If we're still using the real major
199ea8dc4b6Seschrock 				 * keep out of /dev/zfs and /dev/zvol minor
200ea8dc4b6Seschrock 				 * number space.  If we're using a getudev()'ed
201ea8dc4b6Seschrock 				 * major number, we can use all of its minors.
202ea8dc4b6Seschrock 				 */
203ea8dc4b6Seschrock 				if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
204ea8dc4b6Seschrock 					zfs_minor = ZFS_MIN_MINOR;
205ea8dc4b6Seschrock 				else
206ea8dc4b6Seschrock 					zfs_minor = 0;
207ea8dc4b6Seschrock 			} else {
208ea8dc4b6Seschrock 				zfs_minor++;
209ea8dc4b6Seschrock 			}
210ea8dc4b6Seschrock 			*dev = makedevice(zfs_major, zfs_minor);
211ea8dc4b6Seschrock 			mutex_exit(&zfs_dev_mtx);
212ea8dc4b6Seschrock 		} while (vfs_devismounted(*dev) && zfs_minor != start);
213ea8dc4b6Seschrock 		if (zfs_minor == start) {
214ea8dc4b6Seschrock 			/*
215ea8dc4b6Seschrock 			 * We are using all ~262,000 minor numbers for the
216ea8dc4b6Seschrock 			 * current major number.  Create a new major number.
217ea8dc4b6Seschrock 			 */
218ea8dc4b6Seschrock 			if ((new_major = getudev()) == (major_t)-1) {
219ea8dc4b6Seschrock 				cmn_err(CE_WARN,
220ea8dc4b6Seschrock 				    "zfs_mount: Can't get unique major "
221ea8dc4b6Seschrock 				    "device number.");
222ea8dc4b6Seschrock 				return (-1);
223ea8dc4b6Seschrock 			}
224ea8dc4b6Seschrock 			mutex_enter(&zfs_dev_mtx);
225ea8dc4b6Seschrock 			zfs_major = new_major;
226ea8dc4b6Seschrock 			zfs_minor = 0;
227ea8dc4b6Seschrock 
228ea8dc4b6Seschrock 			mutex_exit(&zfs_dev_mtx);
229ea8dc4b6Seschrock 		} else {
230ea8dc4b6Seschrock 			break;
231ea8dc4b6Seschrock 		}
232ea8dc4b6Seschrock 		/* CONSTANTCONDITION */
233ea8dc4b6Seschrock 	} while (1);
234ea8dc4b6Seschrock 
235ea8dc4b6Seschrock 	return (0);
236ea8dc4b6Seschrock }
237ea8dc4b6Seschrock 
238fa9e4066Sahrens static void
atime_changed_cb(void * arg,uint64_t newval)239fa9e4066Sahrens atime_changed_cb(void *arg, uint64_t newval)
240fa9e4066Sahrens {
241fa9e4066Sahrens 	zfsvfs_t *zfsvfs = arg;
242fa9e4066Sahrens 
243fa9e4066Sahrens 	if (newval == TRUE) {
244fa9e4066Sahrens 		zfsvfs->z_atime = TRUE;
245fa9e4066Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
246fa9e4066Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
247fa9e4066Sahrens 	} else {
248fa9e4066Sahrens 		zfsvfs->z_atime = FALSE;
249fa9e4066Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
250fa9e4066Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
251fa9e4066Sahrens 	}
252fa9e4066Sahrens }
253fa9e4066Sahrens 
254fa9e4066Sahrens static void
xattr_changed_cb(void * arg,uint64_t newval)2557b55fa8eSck153898 xattr_changed_cb(void *arg, uint64_t newval)
2567b55fa8eSck153898 {
2577b55fa8eSck153898 	zfsvfs_t *zfsvfs = arg;
2587b55fa8eSck153898 
2597b55fa8eSck153898 	if (newval == TRUE) {
2607b55fa8eSck153898 		/* XXX locking on vfs_flag? */
2617b55fa8eSck153898 		zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
2627b55fa8eSck153898 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
2637b55fa8eSck153898 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
2647b55fa8eSck153898 	} else {
2657b55fa8eSck153898 		/* XXX locking on vfs_flag? */
2667b55fa8eSck153898 		zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
2677b55fa8eSck153898 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
2687b55fa8eSck153898 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
2697b55fa8eSck153898 	}
2707b55fa8eSck153898 }
2717b55fa8eSck153898 
2727b55fa8eSck153898 static void
blksz_changed_cb(void * arg,uint64_t newval)273fa9e4066Sahrens blksz_changed_cb(void *arg, uint64_t newval)
274fa9e4066Sahrens {
275fa9e4066Sahrens 	zfsvfs_t *zfsvfs = arg;
276d1a98260SMatthew Ahrens 	ASSERT3U(newval, <=, spa_maxblocksize(dmu_objset_spa(zfsvfs->z_os)));
277d1a98260SMatthew Ahrens 	ASSERT3U(newval, >=, SPA_MINBLOCKSIZE);
278d1a98260SMatthew Ahrens 	ASSERT(ISP2(newval));
279fa9e4066Sahrens 
280fa9e4066Sahrens 	zfsvfs->z_max_blksz = newval;
281fa9e4066Sahrens 	zfsvfs->z_vfs->vfs_bsize = newval;
282fa9e4066Sahrens }
283fa9e4066Sahrens 
284fa9e4066Sahrens static void
readonly_changed_cb(void * arg,uint64_t newval)285fa9e4066Sahrens readonly_changed_cb(void *arg, uint64_t newval)
286fa9e4066Sahrens {
287fa9e4066Sahrens 	zfsvfs_t *zfsvfs = arg;
288fa9e4066Sahrens 
289fa9e4066Sahrens 	if (newval) {
290fa9e4066Sahrens 		/* XXX locking on vfs_flag? */
291fa9e4066Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
292fa9e4066Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
293fa9e4066Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
294fa9e4066Sahrens 	} else {
295fa9e4066Sahrens 		/* XXX locking on vfs_flag? */
296fa9e4066Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
297fa9e4066Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
298fa9e4066Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
299fa9e4066Sahrens 	}
300fa9e4066Sahrens }
301fa9e4066Sahrens 
302fa9e4066Sahrens static void
devices_changed_cb(void * arg,uint64_t newval)303fa9e4066Sahrens devices_changed_cb(void *arg, uint64_t newval)
304fa9e4066Sahrens {
305fa9e4066Sahrens 	zfsvfs_t *zfsvfs = arg;
306fa9e4066Sahrens 
307fa9e4066Sahrens 	if (newval == FALSE) {
308fa9e4066Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
309fa9e4066Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
310fa9e4066Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
311fa9e4066Sahrens 	} else {
312fa9e4066Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
313fa9e4066Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
314fa9e4066Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
315fa9e4066Sahrens 	}
316fa9e4066Sahrens }
317fa9e4066Sahrens 
318fa9e4066Sahrens static void
setuid_changed_cb(void * arg,uint64_t newval)319fa9e4066Sahrens setuid_changed_cb(void *arg, uint64_t newval)
320fa9e4066Sahrens {
321fa9e4066Sahrens 	zfsvfs_t *zfsvfs = arg;
322fa9e4066Sahrens 
323fa9e4066Sahrens 	if (newval == FALSE) {
324fa9e4066Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
325fa9e4066Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
326fa9e4066Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
327fa9e4066Sahrens 	} else {
328fa9e4066Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
329fa9e4066Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
330fa9e4066Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
331fa9e4066Sahrens 	}
332fa9e4066Sahrens }
333fa9e4066Sahrens 
334fa9e4066Sahrens static void
exec_changed_cb(void * arg,uint64_t newval)335fa9e4066Sahrens exec_changed_cb(void *arg, uint64_t newval)
336fa9e4066Sahrens {
337fa9e4066Sahrens 	zfsvfs_t *zfsvfs = arg;
338fa9e4066Sahrens 
339fa9e4066Sahrens 	if (newval == FALSE) {
340fa9e4066Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
341fa9e4066Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
342fa9e4066Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
343fa9e4066Sahrens 	} else {
344fa9e4066Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
345fa9e4066Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
346fa9e4066Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
347fa9e4066Sahrens 	}
348fa9e4066Sahrens }
349fa9e4066Sahrens 
350da6c28aaSamw /*
351da6c28aaSamw  * The nbmand mount option can be changed at mount time.
352da6c28aaSamw  * We can't allow it to be toggled on live file systems or incorrect
353da6c28aaSamw  * behavior may be seen from cifs clients
354da6c28aaSamw  *
355da6c28aaSamw  * This property isn't registered via dsl_prop_register(), but this callback
356da6c28aaSamw  * will be called when a file system is first mounted
357da6c28aaSamw  */
358da6c28aaSamw static void
nbmand_changed_cb(void * arg,uint64_t newval)359da6c28aaSamw nbmand_changed_cb(void *arg, uint64_t newval)
360da6c28aaSamw {
361da6c28aaSamw 	zfsvfs_t *zfsvfs = arg;
362da6c28aaSamw 	if (newval == FALSE) {
363da6c28aaSamw 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
364da6c28aaSamw 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
365da6c28aaSamw 	} else {
366da6c28aaSamw 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
367da6c28aaSamw 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
368da6c28aaSamw 	}
369da6c28aaSamw }
370da6c28aaSamw 
371fa9e4066Sahrens static void
snapdir_changed_cb(void * arg,uint64_t newval)372fa9e4066Sahrens snapdir_changed_cb(void *arg, uint64_t newval)
373fa9e4066Sahrens {
374fa9e4066Sahrens 	zfsvfs_t *zfsvfs = arg;
375fa9e4066Sahrens 
376fa9e4066Sahrens 	zfsvfs->z_show_ctldir = newval;
377fa9e4066Sahrens }
378fa9e4066Sahrens 
379fa9e4066Sahrens static void
vscan_changed_cb(void * arg,uint64_t newval)380da6c28aaSamw vscan_changed_cb(void *arg, uint64_t newval)
381da6c28aaSamw {
382da6c28aaSamw 	zfsvfs_t *zfsvfs = arg;
383da6c28aaSamw 
384da6c28aaSamw 	zfsvfs->z_vscan = newval;
385da6c28aaSamw }
386da6c28aaSamw 
387da6c28aaSamw static void
acl_mode_changed_cb(void * arg,uint64_t newval)388a3c49ce1SAlbert Lee acl_mode_changed_cb(void *arg, uint64_t newval)
389a3c49ce1SAlbert Lee {
390a3c49ce1SAlbert Lee 	zfsvfs_t *zfsvfs = arg;
391a3c49ce1SAlbert Lee 
392a3c49ce1SAlbert Lee 	zfsvfs->z_acl_mode = newval;
393a3c49ce1SAlbert Lee }
394a3c49ce1SAlbert Lee 
395a3c49ce1SAlbert Lee static void
acl_inherit_changed_cb(void * arg,uint64_t newval)396fa9e4066Sahrens acl_inherit_changed_cb(void *arg, uint64_t newval)
397fa9e4066Sahrens {
398fa9e4066Sahrens 	zfsvfs_t *zfsvfs = arg;
399fa9e4066Sahrens 
400fa9e4066Sahrens 	zfsvfs->z_acl_inherit = newval;
401fa9e4066Sahrens }
402fa9e4066Sahrens 
403fa9e4066Sahrens static int
zfs_register_callbacks(vfs_t * vfsp)404ea8dc4b6Seschrock zfs_register_callbacks(vfs_t *vfsp)
405ea8dc4b6Seschrock {
406ea8dc4b6Seschrock 	struct dsl_dataset *ds = NULL;
407ea8dc4b6Seschrock 	objset_t *os = NULL;
408ea8dc4b6Seschrock 	zfsvfs_t *zfsvfs = NULL;
409da6c28aaSamw 	uint64_t nbmand;
410d5285caeSGeorge Wilson 	boolean_t readonly = B_FALSE;
411d5285caeSGeorge Wilson 	boolean_t do_readonly = B_FALSE;
412d5285caeSGeorge Wilson 	boolean_t setuid = B_FALSE;
413d5285caeSGeorge Wilson 	boolean_t do_setuid = B_FALSE;
414d5285caeSGeorge Wilson 	boolean_t exec = B_FALSE;
415d5285caeSGeorge Wilson 	boolean_t do_exec = B_FALSE;
416d5285caeSGeorge Wilson 	boolean_t devices = B_FALSE;
417d5285caeSGeorge Wilson 	boolean_t do_devices = B_FALSE;
418d5285caeSGeorge Wilson 	boolean_t xattr = B_FALSE;
419d5285caeSGeorge Wilson 	boolean_t do_xattr = B_FALSE;
420d5285caeSGeorge Wilson 	boolean_t atime = B_FALSE;
421d5285caeSGeorge Wilson 	boolean_t do_atime = B_FALSE;
422ea8dc4b6Seschrock 	int error = 0;
423fa9e4066Sahrens 
424ea8dc4b6Seschrock 	ASSERT(vfsp);
425ea8dc4b6Seschrock 	zfsvfs = vfsp->vfs_data;
426ea8dc4b6Seschrock 	ASSERT(zfsvfs);
427ea8dc4b6Seschrock 	os = zfsvfs->z_os;
428fa9e4066Sahrens 
429fa9e4066Sahrens 	/*
430fa9e4066Sahrens 	 * The act of registering our callbacks will destroy any mount
431fa9e4066Sahrens 	 * options we may have.  In order to enable temporary overrides
4327b55fa8eSck153898 	 * of mount options, we stash away the current values and
433fa9e4066Sahrens 	 * restore them after we register the callbacks.
434fa9e4066Sahrens 	 */
435f9af39baSGeorge Wilson 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL) ||
436f9af39baSGeorge Wilson 	    !spa_writeable(dmu_objset_spa(os))) {
437fa9e4066Sahrens 		readonly = B_TRUE;
438fa9e4066Sahrens 		do_readonly = B_TRUE;
439fa9e4066Sahrens 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
440fa9e4066Sahrens 		readonly = B_FALSE;
441fa9e4066Sahrens 		do_readonly = B_TRUE;
442fa9e4066Sahrens 	}
443fa9e4066Sahrens 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
444fa9e4066Sahrens 		devices = B_FALSE;
445fa9e4066Sahrens 		setuid = B_FALSE;
446fa9e4066Sahrens 		do_devices = B_TRUE;
447fa9e4066Sahrens 		do_setuid = B_TRUE;
448fa9e4066Sahrens 	} else {
449fa9e4066Sahrens 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
450fa9e4066Sahrens 			devices = B_FALSE;
451fa9e4066Sahrens 			do_devices = B_TRUE;
452b1b8ab34Slling 		} else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
453fa9e4066Sahrens 			devices = B_TRUE;
454fa9e4066Sahrens 			do_devices = B_TRUE;
455fa9e4066Sahrens 		}
456fa9e4066Sahrens 
457fa9e4066Sahrens 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
458fa9e4066Sahrens 			setuid = B_FALSE;
459fa9e4066Sahrens 			do_setuid = B_TRUE;
460fa9e4066Sahrens 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
461fa9e4066Sahrens 			setuid = B_TRUE;
462fa9e4066Sahrens 			do_setuid = B_TRUE;
463fa9e4066Sahrens 		}
464fa9e4066Sahrens 	}
465fa9e4066Sahrens 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
466fa9e4066Sahrens 		exec = B_FALSE;
467fa9e4066Sahrens 		do_exec = B_TRUE;
468fa9e4066Sahrens 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
469fa9e4066Sahrens 		exec = B_TRUE;
470fa9e4066Sahrens 		do_exec = B_TRUE;
471fa9e4066Sahrens 	}
4727b55fa8eSck153898 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
4737b55fa8eSck153898 		xattr = B_FALSE;
4747b55fa8eSck153898 		do_xattr = B_TRUE;
4757b55fa8eSck153898 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
4767b55fa8eSck153898 		xattr = B_TRUE;
4777b55fa8eSck153898 		do_xattr = B_TRUE;
4787b55fa8eSck153898 	}
479b510d378Slling 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
480b510d378Slling 		atime = B_FALSE;
481b510d378Slling 		do_atime = B_TRUE;
482b510d378Slling 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
483b510d378Slling 		atime = B_TRUE;
484b510d378Slling 		do_atime = B_TRUE;
485b510d378Slling 	}
486fa9e4066Sahrens 
487fa9e4066Sahrens 	/*
488da6c28aaSamw 	 * nbmand is a special property.  It can only be changed at
489da6c28aaSamw 	 * mount time.
490da6c28aaSamw 	 *
491da6c28aaSamw 	 * This is weird, but it is documented to only be changeable
492da6c28aaSamw 	 * at mount time.
493da6c28aaSamw 	 */
494da6c28aaSamw 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
495da6c28aaSamw 		nbmand = B_FALSE;
496da6c28aaSamw 	} else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
497da6c28aaSamw 		nbmand = B_TRUE;
498da6c28aaSamw 	} else {
499*675fc291SMatthew Ahrens 		char osname[ZFS_MAX_DATASET_NAME_LEN];
500da6c28aaSamw 
501da6c28aaSamw 		dmu_objset_name(os, osname);
502da6c28aaSamw 		if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
503bb0ade09Sahrens 		    NULL)) {
504da6c28aaSamw 			return (error);
505da6c28aaSamw 		}
506bb0ade09Sahrens 	}
507da6c28aaSamw 
508da6c28aaSamw 	/*
509fa9e4066Sahrens 	 * Register property callbacks.
510ea8dc4b6Seschrock 	 *
511ea8dc4b6Seschrock 	 * It would probably be fine to just check for i/o error from
512ea8dc4b6Seschrock 	 * the first prop_register(), but I guess I like to go
513ea8dc4b6Seschrock 	 * overboard...
514fa9e4066Sahrens 	 */
515fa9e4066Sahrens 	ds = dmu_objset_ds(os);
5163b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5173b2aab18SMatthew Ahrens 	error = dsl_prop_register(ds,
5183b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_ATIME), atime_changed_cb, zfsvfs);
519ea8dc4b6Seschrock 	error = error ? error : dsl_prop_register(ds,
5203b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_XATTR), xattr_changed_cb, zfsvfs);
5217b55fa8eSck153898 	error = error ? error : dsl_prop_register(ds,
5223b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_RECORDSIZE), blksz_changed_cb, zfsvfs);
523ea8dc4b6Seschrock 	error = error ? error : dsl_prop_register(ds,
5243b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_READONLY), readonly_changed_cb, zfsvfs);
525ea8dc4b6Seschrock 	error = error ? error : dsl_prop_register(ds,
5263b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_DEVICES), devices_changed_cb, zfsvfs);
527ea8dc4b6Seschrock 	error = error ? error : dsl_prop_register(ds,
5283b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_SETUID), setuid_changed_cb, zfsvfs);
529ea8dc4b6Seschrock 	error = error ? error : dsl_prop_register(ds,
5303b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_EXEC), exec_changed_cb, zfsvfs);
531ea8dc4b6Seschrock 	error = error ? error : dsl_prop_register(ds,
5323b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_SNAPDIR), snapdir_changed_cb, zfsvfs);
533ea8dc4b6Seschrock 	error = error ? error : dsl_prop_register(ds,
5343b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_ACLMODE), acl_mode_changed_cb, zfsvfs);
535a3c49ce1SAlbert Lee 	error = error ? error : dsl_prop_register(ds,
5363b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_ACLINHERIT), acl_inherit_changed_cb,
5373b2aab18SMatthew Ahrens 	    zfsvfs);
538da6c28aaSamw 	error = error ? error : dsl_prop_register(ds,
5393b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_VSCAN), vscan_changed_cb, zfsvfs);
5403b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
541ea8dc4b6Seschrock 	if (error)
542ea8dc4b6Seschrock 		goto unregister;
543fa9e4066Sahrens 
544fa9e4066Sahrens 	/*
545fa9e4066Sahrens 	 * Invoke our callbacks to restore temporary mount options.
546fa9e4066Sahrens 	 */
547fa9e4066Sahrens 	if (do_readonly)
548fa9e4066Sahrens 		readonly_changed_cb(zfsvfs, readonly);
549fa9e4066Sahrens 	if (do_setuid)
550fa9e4066Sahrens 		setuid_changed_cb(zfsvfs, setuid);
551fa9e4066Sahrens 	if (do_exec)
552fa9e4066Sahrens 		exec_changed_cb(zfsvfs, exec);
553fa9e4066Sahrens 	if (do_devices)
554fa9e4066Sahrens 		devices_changed_cb(zfsvfs, devices);
5557b55fa8eSck153898 	if (do_xattr)
5567b55fa8eSck153898 		xattr_changed_cb(zfsvfs, xattr);
557b510d378Slling 	if (do_atime)
558b510d378Slling 		atime_changed_cb(zfsvfs, atime);
559ea8dc4b6Seschrock 
560da6c28aaSamw 	nbmand_changed_cb(zfsvfs, nbmand);
561da6c28aaSamw 
562ea8dc4b6Seschrock 	return (0);
563ea8dc4b6Seschrock 
564ea8dc4b6Seschrock unregister:
565ea8dc4b6Seschrock 	/*
566ea8dc4b6Seschrock 	 * We may attempt to unregister some callbacks that are not
567ea8dc4b6Seschrock 	 * registered, but this is OK; it will simply return ENOMSG,
568ea8dc4b6Seschrock 	 * which we will ignore.
569ea8dc4b6Seschrock 	 */
5703b2aab18SMatthew Ahrens 	(void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_ATIME),
5713b2aab18SMatthew Ahrens 	    atime_changed_cb, zfsvfs);
5723b2aab18SMatthew Ahrens 	(void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_XATTR),
5733b2aab18SMatthew Ahrens 	    xattr_changed_cb, zfsvfs);
5743b2aab18SMatthew Ahrens 	(void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
5753b2aab18SMatthew Ahrens 	    blksz_changed_cb, zfsvfs);
5763b2aab18SMatthew Ahrens 	(void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_READONLY),
5773b2aab18SMatthew Ahrens 	    readonly_changed_cb, zfsvfs);
5783b2aab18SMatthew Ahrens 	(void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_DEVICES),
5793b2aab18SMatthew Ahrens 	    devices_changed_cb, zfsvfs);
5803b2aab18SMatthew Ahrens 	(void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_SETUID),
5813b2aab18SMatthew Ahrens 	    setuid_changed_cb, zfsvfs);
5823b2aab18SMatthew Ahrens 	(void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_EXEC),
5833b2aab18SMatthew Ahrens 	    exec_changed_cb, zfsvfs);
5843b2aab18SMatthew Ahrens 	(void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_SNAPDIR),
5853b2aab18SMatthew Ahrens 	    snapdir_changed_cb, zfsvfs);
5863b2aab18SMatthew Ahrens 	(void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_ACLMODE),
5873b2aab18SMatthew Ahrens 	    acl_mode_changed_cb, zfsvfs);
5883b2aab18SMatthew Ahrens 	(void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_ACLINHERIT),
5893b2aab18SMatthew Ahrens 	    acl_inherit_changed_cb, zfsvfs);
5903b2aab18SMatthew Ahrens 	(void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_VSCAN),
5913b2aab18SMatthew Ahrens 	    vscan_changed_cb, zfsvfs);
592ea8dc4b6Seschrock 	return (error);
593fa9e4066Sahrens }
594fa9e4066Sahrens 
5959966ca11SMatthew Ahrens static int
zfs_space_delta_cb(dmu_object_type_t bonustype,void * data,uint64_t * userp,uint64_t * groupp)5960a586ceaSMark Shellenbaum zfs_space_delta_cb(dmu_object_type_t bonustype, void *data,
5979966ca11SMatthew Ahrens     uint64_t *userp, uint64_t *groupp)
59814843421SMatthew Ahrens {
59906e0070dSMark Shellenbaum 	/*
60006e0070dSMark Shellenbaum 	 * Is it a valid type of object to track?
60106e0070dSMark Shellenbaum 	 */
6020a586ceaSMark Shellenbaum 	if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
603be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
60414843421SMatthew Ahrens 
60506e0070dSMark Shellenbaum 	/*
60606e0070dSMark Shellenbaum 	 * If we have a NULL data pointer
60706e0070dSMark Shellenbaum 	 * then assume the id's aren't changing and
60806e0070dSMark Shellenbaum 	 * return EEXIST to the dmu to let it know to
60906e0070dSMark Shellenbaum 	 * use the same ids
61006e0070dSMark Shellenbaum 	 */
61106e0070dSMark Shellenbaum 	if (data == NULL)
612be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
61306e0070dSMark Shellenbaum 
6140a586ceaSMark Shellenbaum 	if (bonustype == DMU_OT_ZNODE) {
615e828a46dSMatthew Ahrens 		znode_phys_t *znp = data;
6169966ca11SMatthew Ahrens 		*userp = znp->zp_uid;
6179966ca11SMatthew Ahrens 		*groupp = znp->zp_gid;
6180a586ceaSMark Shellenbaum 	} else {
6190a586ceaSMark Shellenbaum 		int hdrsize;
620e828a46dSMatthew Ahrens 		sa_hdr_phys_t *sap = data;
621e828a46dSMatthew Ahrens 		sa_hdr_phys_t sa = *sap;
622e828a46dSMatthew Ahrens 		boolean_t swap = B_FALSE;
6230a586ceaSMark Shellenbaum 
6240a586ceaSMark Shellenbaum 		ASSERT(bonustype == DMU_OT_SA);
6250a586ceaSMark Shellenbaum 
626e828a46dSMatthew Ahrens 		if (sa.sa_magic == 0) {
62706e0070dSMark Shellenbaum 			/*
62806e0070dSMark Shellenbaum 			 * This should only happen for newly created
62906e0070dSMark Shellenbaum 			 * files that haven't had the znode data filled
63006e0070dSMark Shellenbaum 			 * in yet.
63106e0070dSMark Shellenbaum 			 */
63206e0070dSMark Shellenbaum 			*userp = 0;
63306e0070dSMark Shellenbaum 			*groupp = 0;
634e828a46dSMatthew Ahrens 			return (0);
635e828a46dSMatthew Ahrens 		}
636e828a46dSMatthew Ahrens 		if (sa.sa_magic == BSWAP_32(SA_MAGIC)) {
637e828a46dSMatthew Ahrens 			sa.sa_magic = SA_MAGIC;
638e828a46dSMatthew Ahrens 			sa.sa_layout_info = BSWAP_16(sa.sa_layout_info);
639e828a46dSMatthew Ahrens 			swap = B_TRUE;
640e828a46dSMatthew Ahrens 		} else {
641e828a46dSMatthew Ahrens 			VERIFY3U(sa.sa_magic, ==, SA_MAGIC);
642e828a46dSMatthew Ahrens 		}
643e828a46dSMatthew Ahrens 
644e828a46dSMatthew Ahrens 		hdrsize = sa_hdrsize(&sa);
645e828a46dSMatthew Ahrens 		VERIFY3U(hdrsize, >=, sizeof (sa_hdr_phys_t));
646e828a46dSMatthew Ahrens 		*userp = *((uint64_t *)((uintptr_t)data + hdrsize +
647e828a46dSMatthew Ahrens 		    SA_UID_OFFSET));
648e828a46dSMatthew Ahrens 		*groupp = *((uint64_t *)((uintptr_t)data + hdrsize +
649e828a46dSMatthew Ahrens 		    SA_GID_OFFSET));
650e828a46dSMatthew Ahrens 		if (swap) {
651e828a46dSMatthew Ahrens 			*userp = BSWAP_64(*userp);
652e828a46dSMatthew Ahrens 			*groupp = BSWAP_64(*groupp);
6530a586ceaSMark Shellenbaum 		}
6540a586ceaSMark Shellenbaum 	}
6553b2aab18SMatthew Ahrens 	return (0);
65614843421SMatthew Ahrens }
65714843421SMatthew Ahrens 
65814843421SMatthew Ahrens static void
fuidstr_to_sid(zfsvfs_t * zfsvfs,const char * fuidstr,char * domainbuf,int buflen,uid_t * ridp)65914843421SMatthew Ahrens fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr,
66014843421SMatthew Ahrens     char *domainbuf, int buflen, uid_t *ridp)
66114843421SMatthew Ahrens {
66214843421SMatthew Ahrens 	uint64_t fuid;
66314843421SMatthew Ahrens 	const char *domain;
66414843421SMatthew Ahrens 
66514843421SMatthew Ahrens 	fuid = strtonum(fuidstr, NULL);
66614843421SMatthew Ahrens 
66714843421SMatthew Ahrens 	domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid));
66814843421SMatthew Ahrens 	if (domain)
66914843421SMatthew Ahrens 		(void) strlcpy(domainbuf, domain, buflen);
67014843421SMatthew Ahrens 	else
67114843421SMatthew Ahrens 		domainbuf[0] = '\0';
67214843421SMatthew Ahrens 	*ridp = FUID_RID(fuid);
67314843421SMatthew Ahrens }
67414843421SMatthew Ahrens 
67514843421SMatthew Ahrens static uint64_t
zfs_userquota_prop_to_obj(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type)67614843421SMatthew Ahrens zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type)
67714843421SMatthew Ahrens {
67814843421SMatthew Ahrens 	switch (type) {
67914843421SMatthew Ahrens 	case ZFS_PROP_USERUSED:
68014843421SMatthew Ahrens 		return (DMU_USERUSED_OBJECT);
68114843421SMatthew Ahrens 	case ZFS_PROP_GROUPUSED:
68214843421SMatthew Ahrens 		return (DMU_GROUPUSED_OBJECT);
68314843421SMatthew Ahrens 	case ZFS_PROP_USERQUOTA:
68414843421SMatthew Ahrens 		return (zfsvfs->z_userquota_obj);
68514843421SMatthew Ahrens 	case ZFS_PROP_GROUPQUOTA:
68614843421SMatthew Ahrens 		return (zfsvfs->z_groupquota_obj);
68714843421SMatthew Ahrens 	}
68814843421SMatthew Ahrens 	return (0);
68914843421SMatthew Ahrens }
69014843421SMatthew Ahrens 
69114843421SMatthew Ahrens int
zfs_userspace_many(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type,uint64_t * cookiep,void * vbuf,uint64_t * bufsizep)69214843421SMatthew Ahrens zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
69314843421SMatthew Ahrens     uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
69414843421SMatthew Ahrens {
69514843421SMatthew Ahrens 	int error;
69614843421SMatthew Ahrens 	zap_cursor_t zc;
69714843421SMatthew Ahrens 	zap_attribute_t za;
69814843421SMatthew Ahrens 	zfs_useracct_t *buf = vbuf;
69914843421SMatthew Ahrens 	uint64_t obj;
70014843421SMatthew Ahrens 
70114843421SMatthew Ahrens 	if (!dmu_objset_userspace_present(zfsvfs->z_os))
702be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
70314843421SMatthew Ahrens 
70414843421SMatthew Ahrens 	obj = zfs_userquota_prop_to_obj(zfsvfs, type);
70514843421SMatthew Ahrens 	if (obj == 0) {
70614843421SMatthew Ahrens 		*bufsizep = 0;
70714843421SMatthew Ahrens 		return (0);
70814843421SMatthew Ahrens 	}
70914843421SMatthew Ahrens 
71014843421SMatthew Ahrens 	for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
71114843421SMatthew Ahrens 	    (error = zap_cursor_retrieve(&zc, &za)) == 0;
71214843421SMatthew Ahrens 	    zap_cursor_advance(&zc)) {
71314843421SMatthew Ahrens 		if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
71414843421SMatthew Ahrens 		    *bufsizep)
71514843421SMatthew Ahrens 			break;
71614843421SMatthew Ahrens 
71714843421SMatthew Ahrens 		fuidstr_to_sid(zfsvfs, za.za_name,
71814843421SMatthew Ahrens 		    buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
71914843421SMatthew Ahrens 
72014843421SMatthew Ahrens 		buf->zu_space = za.za_first_integer;
72114843421SMatthew Ahrens 		buf++;
72214843421SMatthew Ahrens 	}
72314843421SMatthew Ahrens 	if (error == ENOENT)
72414843421SMatthew Ahrens 		error = 0;
72514843421SMatthew Ahrens 
72614843421SMatthew Ahrens 	ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
72714843421SMatthew Ahrens 	*bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
72814843421SMatthew Ahrens 	*cookiep = zap_cursor_serialize(&zc);
72914843421SMatthew Ahrens 	zap_cursor_fini(&zc);
73014843421SMatthew Ahrens 	return (error);
73114843421SMatthew Ahrens }
73214843421SMatthew Ahrens 
73314843421SMatthew Ahrens /*
73414843421SMatthew Ahrens  * buf must be big enough (eg, 32 bytes)
73514843421SMatthew Ahrens  */
73614843421SMatthew Ahrens static int
id_to_fuidstr(zfsvfs_t * zfsvfs,const char * domain,uid_t rid,char * buf,boolean_t addok)73714843421SMatthew Ahrens id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
73814843421SMatthew Ahrens     char *buf, boolean_t addok)
73914843421SMatthew Ahrens {
74014843421SMatthew Ahrens 	uint64_t fuid;
74114843421SMatthew Ahrens 	int domainid = 0;
74214843421SMatthew Ahrens 
74314843421SMatthew Ahrens 	if (domain && domain[0]) {
74414843421SMatthew Ahrens 		domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
74514843421SMatthew Ahrens 		if (domainid == -1)
746be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
74714843421SMatthew Ahrens 	}
74814843421SMatthew Ahrens 	fuid = FUID_ENCODE(domainid, rid);
74914843421SMatthew Ahrens 	(void) sprintf(buf, "%llx", (longlong_t)fuid);
75014843421SMatthew Ahrens 	return (0);
75114843421SMatthew Ahrens }
75214843421SMatthew Ahrens 
75314843421SMatthew Ahrens int
zfs_userspace_one(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type,const char * domain,uint64_t rid,uint64_t * valp)75414843421SMatthew Ahrens zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
75514843421SMatthew Ahrens     const char *domain, uint64_t rid, uint64_t *valp)
75614843421SMatthew Ahrens {
75714843421SMatthew Ahrens 	char buf[32];
75814843421SMatthew Ahrens 	int err;
75914843421SMatthew Ahrens 	uint64_t obj;
76014843421SMatthew Ahrens 
76114843421SMatthew Ahrens 	*valp = 0;
76214843421SMatthew Ahrens 
76314843421SMatthew Ahrens 	if (!dmu_objset_userspace_present(zfsvfs->z_os))
764be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
76514843421SMatthew Ahrens 
76614843421SMatthew Ahrens 	obj = zfs_userquota_prop_to_obj(zfsvfs, type);
76714843421SMatthew Ahrens 	if (obj == 0)
76814843421SMatthew Ahrens 		return (0);
76914843421SMatthew Ahrens 
77014843421SMatthew Ahrens 	err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_FALSE);
77114843421SMatthew Ahrens 	if (err)
77214843421SMatthew Ahrens 		return (err);
77314843421SMatthew Ahrens 
77414843421SMatthew Ahrens 	err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
77514843421SMatthew Ahrens 	if (err == ENOENT)
77614843421SMatthew Ahrens 		err = 0;
77714843421SMatthew Ahrens 	return (err);
77814843421SMatthew Ahrens }
77914843421SMatthew Ahrens 
78014843421SMatthew Ahrens int
zfs_set_userquota(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type,const char * domain,uint64_t rid,uint64_t quota)78114843421SMatthew Ahrens zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
78214843421SMatthew Ahrens     const char *domain, uint64_t rid, uint64_t quota)
78314843421SMatthew Ahrens {
78414843421SMatthew Ahrens 	char buf[32];
78514843421SMatthew Ahrens 	int err;
78614843421SMatthew Ahrens 	dmu_tx_t *tx;
78714843421SMatthew Ahrens 	uint64_t *objp;
78814843421SMatthew Ahrens 	boolean_t fuid_dirtied;
78914843421SMatthew Ahrens 
79014843421SMatthew Ahrens 	if (type != ZFS_PROP_USERQUOTA && type != ZFS_PROP_GROUPQUOTA)
791be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
79214843421SMatthew Ahrens 
79314843421SMatthew Ahrens 	if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
794be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
79514843421SMatthew Ahrens 
79614843421SMatthew Ahrens 	objp = (type == ZFS_PROP_USERQUOTA) ? &zfsvfs->z_userquota_obj :
79714843421SMatthew Ahrens 	    &zfsvfs->z_groupquota_obj;
79814843421SMatthew Ahrens 
79914843421SMatthew Ahrens 	err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE);
80014843421SMatthew Ahrens 	if (err)
80114843421SMatthew Ahrens 		return (err);
80214843421SMatthew Ahrens 	fuid_dirtied = zfsvfs->z_fuid_dirty;
80314843421SMatthew Ahrens 
80414843421SMatthew Ahrens 	tx = dmu_tx_create(zfsvfs->z_os);
80514843421SMatthew Ahrens 	dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
80614843421SMatthew Ahrens 	if (*objp == 0) {
80714843421SMatthew Ahrens 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
80814843421SMatthew Ahrens 		    zfs_userquota_prop_prefixes[type]);
80914843421SMatthew Ahrens 	}
81014843421SMatthew Ahrens 	if (fuid_dirtied)
81114843421SMatthew Ahrens 		zfs_fuid_txhold(zfsvfs, tx);
81214843421SMatthew Ahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
81314843421SMatthew Ahrens 	if (err) {
81414843421SMatthew Ahrens 		dmu_tx_abort(tx);
81514843421SMatthew Ahrens 		return (err);
81614843421SMatthew Ahrens 	}
81714843421SMatthew Ahrens 
81814843421SMatthew Ahrens 	mutex_enter(&zfsvfs->z_lock);
81914843421SMatthew Ahrens 	if (*objp == 0) {
82014843421SMatthew Ahrens 		*objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
82114843421SMatthew Ahrens 		    DMU_OT_NONE, 0, tx);
82214843421SMatthew Ahrens 		VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
82314843421SMatthew Ahrens 		    zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
82414843421SMatthew Ahrens 	}
82514843421SMatthew Ahrens 	mutex_exit(&zfsvfs->z_lock);
82614843421SMatthew Ahrens 
82714843421SMatthew Ahrens 	if (quota == 0) {
82814843421SMatthew Ahrens 		err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
82914843421SMatthew Ahrens 		if (err == ENOENT)
83014843421SMatthew Ahrens 			err = 0;
83114843421SMatthew Ahrens 	} else {
83214843421SMatthew Ahrens 		err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, &quota, tx);
83314843421SMatthew Ahrens 	}
83414843421SMatthew Ahrens 	ASSERT(err == 0);
83514843421SMatthew Ahrens 	if (fuid_dirtied)
83614843421SMatthew Ahrens 		zfs_fuid_sync(zfsvfs, tx);
83714843421SMatthew Ahrens 	dmu_tx_commit(tx);
83814843421SMatthew Ahrens 	return (err);
83914843421SMatthew Ahrens }
84014843421SMatthew Ahrens 
84114843421SMatthew Ahrens boolean_t
zfs_fuid_overquota(zfsvfs_t * zfsvfs,boolean_t isgroup,uint64_t fuid)8420a586ceaSMark Shellenbaum zfs_fuid_overquota(zfsvfs_t *zfsvfs, boolean_t isgroup, uint64_t fuid)
84314843421SMatthew Ahrens {
84414843421SMatthew Ahrens 	char buf[32];
84514843421SMatthew Ahrens 	uint64_t used, quota, usedobj, quotaobj;
84614843421SMatthew Ahrens 	int err;
84714843421SMatthew Ahrens 
84814843421SMatthew Ahrens 	usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
84914843421SMatthew Ahrens 	quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
85014843421SMatthew Ahrens 
85114843421SMatthew Ahrens 	if (quotaobj == 0 || zfsvfs->z_replay)
85214843421SMatthew Ahrens 		return (B_FALSE);
85314843421SMatthew Ahrens 
85414843421SMatthew Ahrens 	(void) sprintf(buf, "%llx", (longlong_t)fuid);
85514843421SMatthew Ahrens 	err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
85614843421SMatthew Ahrens 	if (err != 0)
85714843421SMatthew Ahrens 		return (B_FALSE);
85814843421SMatthew Ahrens 
85914843421SMatthew Ahrens 	err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
86014843421SMatthew Ahrens 	if (err != 0)
86114843421SMatthew Ahrens 		return (B_FALSE);
86214843421SMatthew Ahrens 	return (used >= quota);
86314843421SMatthew Ahrens }
86414843421SMatthew Ahrens 
8650a586ceaSMark Shellenbaum boolean_t
zfs_owner_overquota(zfsvfs_t * zfsvfs,znode_t * zp,boolean_t isgroup)8660a586ceaSMark Shellenbaum zfs_owner_overquota(zfsvfs_t *zfsvfs, znode_t *zp, boolean_t isgroup)
8670a586ceaSMark Shellenbaum {
8680a586ceaSMark Shellenbaum 	uint64_t fuid;
8690a586ceaSMark Shellenbaum 	uint64_t quotaobj;
8700a586ceaSMark Shellenbaum 
8710a586ceaSMark Shellenbaum 	quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
8720a586ceaSMark Shellenbaum 
873f1696b23SMark Shellenbaum 	fuid = isgroup ? zp->z_gid : zp->z_uid;
8740a586ceaSMark Shellenbaum 
8750a586ceaSMark Shellenbaum 	if (quotaobj == 0 || zfsvfs->z_replay)
8760a586ceaSMark Shellenbaum 		return (B_FALSE);
8770a586ceaSMark Shellenbaum 
8780a586ceaSMark Shellenbaum 	return (zfs_fuid_overquota(zfsvfs, isgroup, fuid));
8790a586ceaSMark Shellenbaum }
8800a586ceaSMark Shellenbaum 
88114843421SMatthew Ahrens int
zfsvfs_create(const char * osname,zfsvfs_t ** zfvp)882af4c679fSSean McEnroe zfsvfs_create(const char *osname, zfsvfs_t **zfvp)
88314843421SMatthew Ahrens {
88414843421SMatthew Ahrens 	objset_t *os;
88514843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
88614843421SMatthew Ahrens 	uint64_t zval;
88714843421SMatthew Ahrens 	int i, error;
8880a586ceaSMark Shellenbaum 	uint64_t sa_obj;
88914843421SMatthew Ahrens 
890503ad85cSMatthew Ahrens 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
89114843421SMatthew Ahrens 
892503ad85cSMatthew Ahrens 	/*
893503ad85cSMatthew Ahrens 	 * We claim to always be readonly so we can open snapshots;
894503ad85cSMatthew Ahrens 	 * other ZPL code will prevent us from writing to snapshots.
895503ad85cSMatthew Ahrens 	 */
896503ad85cSMatthew Ahrens 	error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zfsvfs, &os);
897503ad85cSMatthew Ahrens 	if (error) {
898503ad85cSMatthew Ahrens 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
89914843421SMatthew Ahrens 		return (error);
900503ad85cSMatthew Ahrens 	}
90114843421SMatthew Ahrens 
90214843421SMatthew Ahrens 	/*
90314843421SMatthew Ahrens 	 * Initialize the zfs-specific filesystem structure.
90414843421SMatthew Ahrens 	 * Should probably make this a kmem cache, shuffle fields,
90514843421SMatthew Ahrens 	 * and just bzero up to z_hold_mtx[].
90614843421SMatthew Ahrens 	 */
90714843421SMatthew Ahrens 	zfsvfs->z_vfs = NULL;
90814843421SMatthew Ahrens 	zfsvfs->z_parent = zfsvfs;
909d1a98260SMatthew Ahrens 	zfsvfs->z_max_blksz = SPA_OLD_MAXBLOCKSIZE;
91014843421SMatthew Ahrens 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
91114843421SMatthew Ahrens 	zfsvfs->z_os = os;
91214843421SMatthew Ahrens 
91314843421SMatthew Ahrens 	error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
91414843421SMatthew Ahrens 	if (error) {
91514843421SMatthew Ahrens 		goto out;
916dc7cd546SMark Shellenbaum 	} else if (zfsvfs->z_version >
917dc7cd546SMark Shellenbaum 	    zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
918dc7cd546SMark Shellenbaum 		(void) printf("Can't mount a version %lld file system "
919dc7cd546SMark Shellenbaum 		    "on a version %lld pool\n. Pool must be upgraded to mount "
920dc7cd546SMark Shellenbaum 		    "this file system.", (u_longlong_t)zfsvfs->z_version,
921dc7cd546SMark Shellenbaum 		    (u_longlong_t)spa_version(dmu_objset_spa(os)));
922be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOTSUP);
92314843421SMatthew Ahrens 		goto out;
92414843421SMatthew Ahrens 	}
92514843421SMatthew Ahrens 	if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
92614843421SMatthew Ahrens 		goto out;
92714843421SMatthew Ahrens 	zfsvfs->z_norm = (int)zval;
92814843421SMatthew Ahrens 
92914843421SMatthew Ahrens 	if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
93014843421SMatthew Ahrens 		goto out;
93114843421SMatthew Ahrens 	zfsvfs->z_utf8 = (zval != 0);
93214843421SMatthew Ahrens 
93314843421SMatthew Ahrens 	if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
93414843421SMatthew Ahrens 		goto out;
93514843421SMatthew Ahrens 	zfsvfs->z_case = (uint_t)zval;
93614843421SMatthew Ahrens 
93714843421SMatthew Ahrens 	/*
93814843421SMatthew Ahrens 	 * Fold case on file systems that are always or sometimes case
93914843421SMatthew Ahrens 	 * insensitive.
94014843421SMatthew Ahrens 	 */
94114843421SMatthew Ahrens 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
94214843421SMatthew Ahrens 	    zfsvfs->z_case == ZFS_CASE_MIXED)
94314843421SMatthew Ahrens 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
94414843421SMatthew Ahrens 
94514843421SMatthew Ahrens 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
9460a586ceaSMark Shellenbaum 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
9470a586ceaSMark Shellenbaum 
9480a586ceaSMark Shellenbaum 	if (zfsvfs->z_use_sa) {
9490a586ceaSMark Shellenbaum 		/* should either have both of these objects or none */
9500a586ceaSMark Shellenbaum 		error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
9510a586ceaSMark Shellenbaum 		    &sa_obj);
9520a586ceaSMark Shellenbaum 		if (error)
9535f9bb2f3SPaul Dagnelie 			goto out;
9540a586ceaSMark Shellenbaum 	} else {
9550a586ceaSMark Shellenbaum 		/*
9560a586ceaSMark Shellenbaum 		 * Pre SA versions file systems should never touch
9570a586ceaSMark Shellenbaum 		 * either the attribute registration or layout objects.
9580a586ceaSMark Shellenbaum 		 */
9590a586ceaSMark Shellenbaum 		sa_obj = 0;
9600a586ceaSMark Shellenbaum 	}
9610a586ceaSMark Shellenbaum 
9621d8ccc7bSMark Shellenbaum 	error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
9631d8ccc7bSMark Shellenbaum 	    &zfsvfs->z_attr_table);
9641d8ccc7bSMark Shellenbaum 	if (error)
9651d8ccc7bSMark Shellenbaum 		goto out;
9660a586ceaSMark Shellenbaum 
9670a586ceaSMark Shellenbaum 	if (zfsvfs->z_version >= ZPL_VERSION_SA)
9680a586ceaSMark Shellenbaum 		sa_register_update_callback(os, zfs_sa_upgrade);
96914843421SMatthew Ahrens 
97014843421SMatthew Ahrens 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
97114843421SMatthew Ahrens 	    &zfsvfs->z_root);
97214843421SMatthew Ahrens 	if (error)
97314843421SMatthew Ahrens 		goto out;
97414843421SMatthew Ahrens 	ASSERT(zfsvfs->z_root != 0);
97514843421SMatthew Ahrens 
97614843421SMatthew Ahrens 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
97714843421SMatthew Ahrens 	    &zfsvfs->z_unlinkedobj);
97814843421SMatthew Ahrens 	if (error)
97914843421SMatthew Ahrens 		goto out;
98014843421SMatthew Ahrens 
98114843421SMatthew Ahrens 	error = zap_lookup(os, MASTER_NODE_OBJ,
98214843421SMatthew Ahrens 	    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
98314843421SMatthew Ahrens 	    8, 1, &zfsvfs->z_userquota_obj);
98414843421SMatthew Ahrens 	if (error && error != ENOENT)
98514843421SMatthew Ahrens 		goto out;
98614843421SMatthew Ahrens 
98714843421SMatthew Ahrens 	error = zap_lookup(os, MASTER_NODE_OBJ,
98814843421SMatthew Ahrens 	    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
98914843421SMatthew Ahrens 	    8, 1, &zfsvfs->z_groupquota_obj);
99014843421SMatthew Ahrens 	if (error && error != ENOENT)
99114843421SMatthew Ahrens 		goto out;
99214843421SMatthew Ahrens 
99314843421SMatthew Ahrens 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
99414843421SMatthew Ahrens 	    &zfsvfs->z_fuid_obj);
99514843421SMatthew Ahrens 	if (error && error != ENOENT)
99614843421SMatthew Ahrens 		goto out;
99714843421SMatthew Ahrens 
99814843421SMatthew Ahrens 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
99914843421SMatthew Ahrens 	    &zfsvfs->z_shares_dir);
100014843421SMatthew Ahrens 	if (error && error != ENOENT)
100114843421SMatthew Ahrens 		goto out;
100214843421SMatthew Ahrens 
100314843421SMatthew Ahrens 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
100414843421SMatthew Ahrens 	mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
100514843421SMatthew Ahrens 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
100614843421SMatthew Ahrens 	    offsetof(znode_t, z_link_node));
100740d689f8SAlexander Motin 	rrm_init(&zfsvfs->z_teardown_lock, B_FALSE);
100814843421SMatthew Ahrens 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
100914843421SMatthew Ahrens 	rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
101014843421SMatthew Ahrens 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
101114843421SMatthew Ahrens 		mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
101214843421SMatthew Ahrens 
1013af4c679fSSean McEnroe 	*zfvp = zfsvfs;
101414843421SMatthew Ahrens 	return (0);
101514843421SMatthew Ahrens 
101614843421SMatthew Ahrens out:
1017503ad85cSMatthew Ahrens 	dmu_objset_disown(os, zfsvfs);
1018af4c679fSSean McEnroe 	*zfvp = NULL;
101914843421SMatthew Ahrens 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
102014843421SMatthew Ahrens 	return (error);
102114843421SMatthew Ahrens }
102214843421SMatthew Ahrens 
1023ea8dc4b6Seschrock static int
zfsvfs_setup(zfsvfs_t * zfsvfs,boolean_t mounting)1024f18faf3fSek110237 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
1025f18faf3fSek110237 {
1026f18faf3fSek110237 	int error;
1027f18faf3fSek110237 
1028f18faf3fSek110237 	error = zfs_register_callbacks(zfsvfs->z_vfs);
1029f18faf3fSek110237 	if (error)
1030f18faf3fSek110237 		return (error);
1031f18faf3fSek110237 
1032f18faf3fSek110237 	/*
1033f18faf3fSek110237 	 * Set the objset user_ptr to track its zfsvfs.
1034f18faf3fSek110237 	 */
1035503ad85cSMatthew Ahrens 	mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1036f18faf3fSek110237 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1037503ad85cSMatthew Ahrens 	mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1038f18faf3fSek110237 
1039377c02aaSNeil Perrin 	zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
1040377c02aaSNeil Perrin 
1041f18faf3fSek110237 	/*
1042f18faf3fSek110237 	 * If we are not mounting (ie: online recv), then we don't
1043f18faf3fSek110237 	 * have to worry about replaying the log as we blocked all
1044f18faf3fSek110237 	 * operations out since we closed the ZIL.
1045f18faf3fSek110237 	 */
1046f18faf3fSek110237 	if (mounting) {
1047a6e57bd4SNeil Perrin 		boolean_t readonly;
1048a6e57bd4SNeil Perrin 
1049f18faf3fSek110237 		/*
1050f18faf3fSek110237 		 * During replay we remove the read only flag to
1051f18faf3fSek110237 		 * allow replays to succeed.
1052f18faf3fSek110237 		 */
1053f18faf3fSek110237 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
10541209a471SNeil Perrin 		if (readonly != 0)
1055f18faf3fSek110237 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
10561209a471SNeil Perrin 		else
10571209a471SNeil Perrin 			zfs_unlinked_drain(zfsvfs);
1058f18faf3fSek110237 
1059f18faf3fSek110237 		/*
1060f18faf3fSek110237 		 * Parse and replay the intent log.
10611209a471SNeil Perrin 		 *
10621209a471SNeil Perrin 		 * Because of ziltest, this must be done after
10631209a471SNeil Perrin 		 * zfs_unlinked_drain().  (Further note: ziltest
10641209a471SNeil Perrin 		 * doesn't use readonly mounts, where
10651209a471SNeil Perrin 		 * zfs_unlinked_drain() isn't called.)  This is because
10661209a471SNeil Perrin 		 * ziltest causes spa_sync() to think it's committed,
10671209a471SNeil Perrin 		 * but actually it is not, so the intent log contains
10681209a471SNeil Perrin 		 * many txg's worth of changes.
10691209a471SNeil Perrin 		 *
10701209a471SNeil Perrin 		 * In particular, if object N is in the unlinked set in
10711209a471SNeil Perrin 		 * the last txg to actually sync, then it could be
10721209a471SNeil Perrin 		 * actually freed in a later txg and then reallocated
10731209a471SNeil Perrin 		 * in a yet later txg.  This would write a "create
10741209a471SNeil Perrin 		 * object N" record to the intent log.  Normally, this
10751209a471SNeil Perrin 		 * would be fine because the spa_sync() would have
10761209a471SNeil Perrin 		 * written out the fact that object N is free, before
10771209a471SNeil Perrin 		 * we could write the "create object N" intent log
10781209a471SNeil Perrin 		 * record.
10791209a471SNeil Perrin 		 *
10801209a471SNeil Perrin 		 * But when we are in ziltest mode, we advance the "open
10811209a471SNeil Perrin 		 * txg" without actually spa_sync()-ing the changes to
10821209a471SNeil Perrin 		 * disk.  So we would see that object N is still
10831209a471SNeil Perrin 		 * allocated and in the unlinked set, and there is an
10841209a471SNeil Perrin 		 * intent log record saying to allocate it.
1085f18faf3fSek110237 		 */
1086f9af39baSGeorge Wilson 		if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
108755da60b9SMark J Musante 			if (zil_replay_disable) {
108855da60b9SMark J Musante 				zil_destroy(zfsvfs->z_log, B_FALSE);
108955da60b9SMark J Musante 			} else {
10901209a471SNeil Perrin 				zfsvfs->z_replay = B_TRUE;
1091f9af39baSGeorge Wilson 				zil_replay(zfsvfs->z_os, zfsvfs,
1092f9af39baSGeorge Wilson 				    zfs_replay_vector);
10931209a471SNeil Perrin 				zfsvfs->z_replay = B_FALSE;
10941209a471SNeil Perrin 			}
1095f9af39baSGeorge Wilson 		}
1096f18faf3fSek110237 		zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
1097f18faf3fSek110237 	}
1098f18faf3fSek110237 
1099f18faf3fSek110237 	return (0);
1100f18faf3fSek110237 }
1101f18faf3fSek110237 
110214843421SMatthew Ahrens void
zfsvfs_free(zfsvfs_t * zfsvfs)110314843421SMatthew Ahrens zfsvfs_free(zfsvfs_t *zfsvfs)
110447f263f4Sek110237 {
110514843421SMatthew Ahrens 	int i;
11064e9583b2STom Erickson 	extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
11074e9583b2STom Erickson 
11084e9583b2STom Erickson 	/*
11094e9583b2STom Erickson 	 * This is a barrier to prevent the filesystem from going away in
11104e9583b2STom Erickson 	 * zfs_znode_move() until we can safely ensure that the filesystem is
11114e9583b2STom Erickson 	 * not unmounted. We consider the filesystem valid before the barrier
11124e9583b2STom Erickson 	 * and invalid after the barrier.
11134e9583b2STom Erickson 	 */
11144e9583b2STom Erickson 	rw_enter(&zfsvfs_lock, RW_READER);
11154e9583b2STom Erickson 	rw_exit(&zfsvfs_lock);
111614843421SMatthew Ahrens 
111714843421SMatthew Ahrens 	zfs_fuid_destroy(zfsvfs);
111814843421SMatthew Ahrens 
111947f263f4Sek110237 	mutex_destroy(&zfsvfs->z_znodes_lock);
11209e1320c0SMark Shellenbaum 	mutex_destroy(&zfsvfs->z_lock);
112147f263f4Sek110237 	list_destroy(&zfsvfs->z_all_znodes);
112240d689f8SAlexander Motin 	rrm_destroy(&zfsvfs->z_teardown_lock);
112347f263f4Sek110237 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
112447f263f4Sek110237 	rw_destroy(&zfsvfs->z_fuid_lock);
112514843421SMatthew Ahrens 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
112614843421SMatthew Ahrens 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
112747f263f4Sek110237 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
112847f263f4Sek110237 }
112947f263f4Sek110237 
113014843421SMatthew Ahrens static void
zfs_set_fuid_feature(zfsvfs_t * zfsvfs)113114843421SMatthew Ahrens zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
113214843421SMatthew Ahrens {
113314843421SMatthew Ahrens 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
113444bffe01SMark Shellenbaum 	if (zfsvfs->z_vfs) {
113544bffe01SMark Shellenbaum 		if (zfsvfs->z_use_fuids) {
113614843421SMatthew Ahrens 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
113714843421SMatthew Ahrens 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
113814843421SMatthew Ahrens 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
113914843421SMatthew Ahrens 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1140e802abbdSTim Haley 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
11417a286c47SDai Ngo 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
114244bffe01SMark Shellenbaum 		} else {
114344bffe01SMark Shellenbaum 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
114444bffe01SMark Shellenbaum 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
114544bffe01SMark Shellenbaum 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
114644bffe01SMark Shellenbaum 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
114744bffe01SMark Shellenbaum 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
114844bffe01SMark Shellenbaum 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
114944bffe01SMark Shellenbaum 		}
115014843421SMatthew Ahrens 	}
11510a586ceaSMark Shellenbaum 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
115214843421SMatthew Ahrens }
115314843421SMatthew Ahrens 
1154f18faf3fSek110237 static int
zfs_domount(vfs_t * vfsp,char * osname)1155088f3894Sahrens zfs_domount(vfs_t *vfsp, char *osname)
1156ea8dc4b6Seschrock {
1157ea8dc4b6Seschrock 	dev_t mount_dev;
115814843421SMatthew Ahrens 	uint64_t recordsize, fsid_guid;
1159ea8dc4b6Seschrock 	int error = 0;
1160ea8dc4b6Seschrock 	zfsvfs_t *zfsvfs;
1161ea8dc4b6Seschrock 
1162ea8dc4b6Seschrock 	ASSERT(vfsp);
1163ea8dc4b6Seschrock 	ASSERT(osname);
1164ea8dc4b6Seschrock 
1165503ad85cSMatthew Ahrens 	error = zfsvfs_create(osname, &zfsvfs);
116614843421SMatthew Ahrens 	if (error)
116714843421SMatthew Ahrens 		return (error);
1168ea8dc4b6Seschrock 	zfsvfs->z_vfs = vfsp;
1169ea8dc4b6Seschrock 
1170ea8dc4b6Seschrock 	/* Initialize the generic filesystem structure. */
1171ea8dc4b6Seschrock 	vfsp->vfs_bcount = 0;
1172ea8dc4b6Seschrock 	vfsp->vfs_data = NULL;
1173ea8dc4b6Seschrock 
1174ea8dc4b6Seschrock 	if (zfs_create_unique_device(&mount_dev) == -1) {
1175be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENODEV);
1176ea8dc4b6Seschrock 		goto out;
1177ea8dc4b6Seschrock 	}
1178ea8dc4b6Seschrock 	ASSERT(vfs_devismounted(mount_dev) == 0);
1179ea8dc4b6Seschrock 
1180ea8dc4b6Seschrock 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
1181ea8dc4b6Seschrock 	    NULL))
1182ea8dc4b6Seschrock 		goto out;
1183ea8dc4b6Seschrock 
1184ea8dc4b6Seschrock 	vfsp->vfs_dev = mount_dev;
1185ea8dc4b6Seschrock 	vfsp->vfs_fstype = zfsfstype;
1186ea8dc4b6Seschrock 	vfsp->vfs_bsize = recordsize;
1187ea8dc4b6Seschrock 	vfsp->vfs_flag |= VFS_NOTRUNC;
1188ea8dc4b6Seschrock 	vfsp->vfs_data = zfsvfs;
1189ea8dc4b6Seschrock 
119014843421SMatthew Ahrens 	/*
119114843421SMatthew Ahrens 	 * The fsid is 64 bits, composed of an 8-bit fs type, which
119214843421SMatthew Ahrens 	 * separates our fsid from any other filesystem types, and a
119314843421SMatthew Ahrens 	 * 56-bit objset unique ID.  The objset unique ID is unique to
119414843421SMatthew Ahrens 	 * all objsets open on this system, provided by unique_create().
119514843421SMatthew Ahrens 	 * The 8-bit fs type must be put in the low bits of fsid[1]
119614843421SMatthew Ahrens 	 * because that's where other Solaris filesystems put it.
119714843421SMatthew Ahrens 	 */
119814843421SMatthew Ahrens 	fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
119914843421SMatthew Ahrens 	ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
120014843421SMatthew Ahrens 	vfsp->vfs_fsid.val[0] = fsid_guid;
120114843421SMatthew Ahrens 	vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
120214843421SMatthew Ahrens 	    zfsfstype & 0xFF;
1203ea8dc4b6Seschrock 
1204da6c28aaSamw 	/*
1205da6c28aaSamw 	 * Set features for file system.
1206da6c28aaSamw 	 */
120714843421SMatthew Ahrens 	zfs_set_fuid_feature(zfsvfs);
1208de8267e0Stimh 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
1209de8267e0Stimh 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1210de8267e0Stimh 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1211de8267e0Stimh 		vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
1212de8267e0Stimh 	} else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
1213de8267e0Stimh 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1214de8267e0Stimh 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1215de8267e0Stimh 	}
1216c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
1217da6c28aaSamw 
1218ea8dc4b6Seschrock 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1219da6c28aaSamw 		uint64_t pval;
12207b55fa8eSck153898 
1221ea8dc4b6Seschrock 		atime_changed_cb(zfsvfs, B_FALSE);
1222ea8dc4b6Seschrock 		readonly_changed_cb(zfsvfs, B_TRUE);
1223da6c28aaSamw 		if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
12247b55fa8eSck153898 			goto out;
1225da6c28aaSamw 		xattr_changed_cb(zfsvfs, pval);
1226ea8dc4b6Seschrock 		zfsvfs->z_issnap = B_TRUE;
1227b9deb9cbSMark J Musante 		zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1228777badbaSMatthew Ahrens 
1229503ad85cSMatthew Ahrens 		mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1230777badbaSMatthew Ahrens 		dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1231503ad85cSMatthew Ahrens 		mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1232ea8dc4b6Seschrock 	} else {
1233f18faf3fSek110237 		error = zfsvfs_setup(zfsvfs, B_TRUE);
1234ea8dc4b6Seschrock 	}
1235ea8dc4b6Seschrock 
1236fa9e4066Sahrens 	if (!zfsvfs->z_issnap)
1237fa9e4066Sahrens 		zfsctl_create(zfsvfs);
1238fa9e4066Sahrens out:
1239fa9e4066Sahrens 	if (error) {
1240503ad85cSMatthew Ahrens 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
124114843421SMatthew Ahrens 		zfsvfs_free(zfsvfs);
1242fa9e4066Sahrens 	} else {
12430d6bb4c6SJosef 'Jeff' Sipek 		atomic_inc_32(&zfs_active_fs_count);
1244fa9e4066Sahrens 	}
1245fa9e4066Sahrens 
1246ea8dc4b6Seschrock 	return (error);
1247ea8dc4b6Seschrock }
1248ea8dc4b6Seschrock 
1249ea8dc4b6Seschrock void
zfs_unregister_callbacks(zfsvfs_t * zfsvfs)1250ea8dc4b6Seschrock zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
1251ea8dc4b6Seschrock {
1252ea8dc4b6Seschrock 	objset_t *os = zfsvfs->z_os;
1253ea8dc4b6Seschrock 	struct dsl_dataset *ds;
1254ea8dc4b6Seschrock 
1255ea8dc4b6Seschrock 	/*
1256ea8dc4b6Seschrock 	 * Unregister properties.
1257ea8dc4b6Seschrock 	 */
1258ea8dc4b6Seschrock 	if (!dmu_objset_is_snapshot(os)) {
1259ea8dc4b6Seschrock 		ds = dmu_objset_ds(os);
1260ea8dc4b6Seschrock 		VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
1261ea8dc4b6Seschrock 		    zfsvfs) == 0);
1262ea8dc4b6Seschrock 
12637b55fa8eSck153898 		VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
12647b55fa8eSck153898 		    zfsvfs) == 0);
12657b55fa8eSck153898 
1266ea8dc4b6Seschrock 		VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
1267ea8dc4b6Seschrock 		    zfsvfs) == 0);
1268ea8dc4b6Seschrock 
1269ea8dc4b6Seschrock 		VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
1270ea8dc4b6Seschrock 		    zfsvfs) == 0);
1271ea8dc4b6Seschrock 
1272ea8dc4b6Seschrock 		VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
1273ea8dc4b6Seschrock 		    zfsvfs) == 0);
1274ea8dc4b6Seschrock 
1275ea8dc4b6Seschrock 		VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
1276ea8dc4b6Seschrock 		    zfsvfs) == 0);
1277ea8dc4b6Seschrock 
1278ea8dc4b6Seschrock 		VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
1279ea8dc4b6Seschrock 		    zfsvfs) == 0);
1280ea8dc4b6Seschrock 
1281ea8dc4b6Seschrock 		VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
1282ea8dc4b6Seschrock 		    zfsvfs) == 0);
1283ea8dc4b6Seschrock 
1284a3c49ce1SAlbert Lee 		VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
1285a3c49ce1SAlbert Lee 		    zfsvfs) == 0);
1286a3c49ce1SAlbert Lee 
1287ea8dc4b6Seschrock 		VERIFY(dsl_prop_unregister(ds, "aclinherit",
1288ea8dc4b6Seschrock 		    acl_inherit_changed_cb, zfsvfs) == 0);
1289da6c28aaSamw 
1290da6c28aaSamw 		VERIFY(dsl_prop_unregister(ds, "vscan",
1291da6c28aaSamw 		    vscan_changed_cb, zfsvfs) == 0);
1292ea8dc4b6Seschrock 	}
1293ea8dc4b6Seschrock }
1294ea8dc4b6Seschrock 
1295b1b8ab34Slling /*
1296b1b8ab34Slling  * Convert a decimal digit string to a uint64_t integer.
1297b1b8ab34Slling  */
1298b1b8ab34Slling static int
str_to_uint64(char * str,uint64_t * objnum)1299b1b8ab34Slling str_to_uint64(char *str, uint64_t *objnum)
1300b1b8ab34Slling {
1301b1b8ab34Slling 	uint64_t num = 0;
1302b1b8ab34Slling 
1303b1b8ab34Slling 	while (*str) {
1304b1b8ab34Slling 		if (*str < '0' || *str > '9')
1305be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
1306b1b8ab34Slling 
1307b1b8ab34Slling 		num = num*10 + *str++ - '0';
1308b1b8ab34Slling 	}
1309b1b8ab34Slling 
1310b1b8ab34Slling 	*objnum = num;
1311b1b8ab34Slling 	return (0);
1312b1b8ab34Slling }
1313b1b8ab34Slling 
1314b1b8ab34Slling /*
1315b1b8ab34Slling  * The boot path passed from the boot loader is in the form of
1316b1b8ab34Slling  * "rootpool-name/root-filesystem-object-number'. Convert this
1317b1b8ab34Slling  * string to a dataset name: "rootpool-name/root-filesystem-name".
1318b1b8ab34Slling  */
1319b1b8ab34Slling static int
zfs_parse_bootfs(char * bpath,char * outpath)1320e7cbe64fSgw25295 zfs_parse_bootfs(char *bpath, char *outpath)
1321b1b8ab34Slling {
1322b1b8ab34Slling 	char *slashp;
1323b1b8ab34Slling 	uint64_t objnum;
1324b1b8ab34Slling 	int error;
1325b1b8ab34Slling 
1326b1b8ab34Slling 	if (*bpath == 0 || *bpath == '/')
1327be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1328b1b8ab34Slling 
132919397407SSherry Moore 	(void) strcpy(outpath, bpath);
133019397407SSherry Moore 
1331b1b8ab34Slling 	slashp = strchr(bpath, '/');
1332b1b8ab34Slling 
1333b1b8ab34Slling 	/* if no '/', just return the pool name */
1334b1b8ab34Slling 	if (slashp == NULL) {
1335b1b8ab34Slling 		return (0);
1336b1b8ab34Slling 	}
1337b1b8ab34Slling 
133819397407SSherry Moore 	/* if not a number, just return the root dataset name */
133919397407SSherry Moore 	if (str_to_uint64(slashp+1, &objnum)) {
134019397407SSherry Moore 		return (0);
134119397407SSherry Moore 	}
1342b1b8ab34Slling 
1343b1b8ab34Slling 	*slashp = '\0';
1344b1b8ab34Slling 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
1345b1b8ab34Slling 	*slashp = '/';
1346b1b8ab34Slling 
1347b1b8ab34Slling 	return (error);
1348b1b8ab34Slling }
1349b1b8ab34Slling 
13504201a95eSRic Aleshire /*
1351f7170741SWill Andrews  * Check that the hex label string is appropriate for the dataset being
1352f7170741SWill Andrews  * mounted into the global_zone proper.
13534201a95eSRic Aleshire  *
13544201a95eSRic Aleshire  * Return an error if the hex label string is not default or
13554201a95eSRic Aleshire  * admin_low/admin_high.  For admin_low labels, the corresponding
13564201a95eSRic Aleshire  * dataset must be readonly.
13574201a95eSRic Aleshire  */
13584201a95eSRic Aleshire int
zfs_check_global_label(const char * dsname,const char * hexsl)13594201a95eSRic Aleshire zfs_check_global_label(const char *dsname, const char *hexsl)
13604201a95eSRic Aleshire {
13614201a95eSRic Aleshire 	if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
13624201a95eSRic Aleshire 		return (0);
13634201a95eSRic Aleshire 	if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
13644201a95eSRic Aleshire 		return (0);
13654201a95eSRic Aleshire 	if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
13664201a95eSRic Aleshire 		/* must be readonly */
13674201a95eSRic Aleshire 		uint64_t rdonly;
13684201a95eSRic Aleshire 
13694201a95eSRic Aleshire 		if (dsl_prop_get_integer(dsname,
13704201a95eSRic Aleshire 		    zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
1371be6fd75aSMatthew Ahrens 			return (SET_ERROR(EACCES));
13724201a95eSRic Aleshire 		return (rdonly ? 0 : EACCES);
13734201a95eSRic Aleshire 	}
1374be6fd75aSMatthew Ahrens 	return (SET_ERROR(EACCES));
13754201a95eSRic Aleshire }
13764201a95eSRic Aleshire 
13774201a95eSRic Aleshire /*
13784201a95eSRic Aleshire  * Determine whether the mount is allowed according to MAC check.
13794201a95eSRic Aleshire  * by comparing (where appropriate) label of the dataset against
13804201a95eSRic Aleshire  * the label of the zone being mounted into.  If the dataset has
13814201a95eSRic Aleshire  * no label, create one.
13824201a95eSRic Aleshire  *
1383f7170741SWill Andrews  * Returns 0 if access allowed, error otherwise (e.g. EACCES)
13844201a95eSRic Aleshire  */
13854201a95eSRic Aleshire static int
zfs_mount_label_policy(vfs_t * vfsp,char * osname)13864201a95eSRic Aleshire zfs_mount_label_policy(vfs_t *vfsp, char *osname)
13874201a95eSRic Aleshire {
13884201a95eSRic Aleshire 	int		error, retv;
13894201a95eSRic Aleshire 	zone_t		*mntzone = NULL;
13904201a95eSRic Aleshire 	ts_label_t	*mnt_tsl;
13914201a95eSRic Aleshire 	bslabel_t	*mnt_sl;
13924201a95eSRic Aleshire 	bslabel_t	ds_sl;
13934201a95eSRic Aleshire 	char		ds_hexsl[MAXNAMELEN];
13944201a95eSRic Aleshire 
13954201a95eSRic Aleshire 	retv = EACCES;				/* assume the worst */
13964201a95eSRic Aleshire 
13974201a95eSRic Aleshire 	/*
13984201a95eSRic Aleshire 	 * Start by getting the dataset label if it exists.
13994201a95eSRic Aleshire 	 */
14004201a95eSRic Aleshire 	error = dsl_prop_get(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
14014201a95eSRic Aleshire 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
14024201a95eSRic Aleshire 	if (error)
1403be6fd75aSMatthew Ahrens 		return (SET_ERROR(EACCES));
14044201a95eSRic Aleshire 
14054201a95eSRic Aleshire 	/*
14064201a95eSRic Aleshire 	 * If labeling is NOT enabled, then disallow the mount of datasets
14074201a95eSRic Aleshire 	 * which have a non-default label already.  No other label checks
14084201a95eSRic Aleshire 	 * are needed.
14094201a95eSRic Aleshire 	 */
14104201a95eSRic Aleshire 	if (!is_system_labeled()) {
14114201a95eSRic Aleshire 		if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
14124201a95eSRic Aleshire 			return (0);
1413be6fd75aSMatthew Ahrens 		return (SET_ERROR(EACCES));
14144201a95eSRic Aleshire 	}
14154201a95eSRic Aleshire 
14164201a95eSRic Aleshire 	/*
14174201a95eSRic Aleshire 	 * Get the label of the mountpoint.  If mounting into the global
14184201a95eSRic Aleshire 	 * zone (i.e. mountpoint is not within an active zone and the
14194201a95eSRic Aleshire 	 * zoned property is off), the label must be default or
14204201a95eSRic Aleshire 	 * admin_low/admin_high only; no other checks are needed.
14214201a95eSRic Aleshire 	 */
14224201a95eSRic Aleshire 	mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE);
14234201a95eSRic Aleshire 	if (mntzone->zone_id == GLOBAL_ZONEID) {
14244201a95eSRic Aleshire 		uint64_t zoned;
14254201a95eSRic Aleshire 
14264201a95eSRic Aleshire 		zone_rele(mntzone);
14274201a95eSRic Aleshire 
14284201a95eSRic Aleshire 		if (dsl_prop_get_integer(osname,
14294201a95eSRic Aleshire 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
1430be6fd75aSMatthew Ahrens 			return (SET_ERROR(EACCES));
14314201a95eSRic Aleshire 		if (!zoned)
14324201a95eSRic Aleshire 			return (zfs_check_global_label(osname, ds_hexsl));
14334201a95eSRic Aleshire 		else
14344201a95eSRic Aleshire 			/*
14354201a95eSRic Aleshire 			 * This is the case of a zone dataset being mounted
14364201a95eSRic Aleshire 			 * initially, before the zone has been fully created;
14374201a95eSRic Aleshire 			 * allow this mount into global zone.
14384201a95eSRic Aleshire 			 */
14394201a95eSRic Aleshire 			return (0);
14404201a95eSRic Aleshire 	}
14414201a95eSRic Aleshire 
14424201a95eSRic Aleshire 	mnt_tsl = mntzone->zone_slabel;
14434201a95eSRic Aleshire 	ASSERT(mnt_tsl != NULL);
14444201a95eSRic Aleshire 	label_hold(mnt_tsl);
14454201a95eSRic Aleshire 	mnt_sl = label2bslabel(mnt_tsl);
14464201a95eSRic Aleshire 
14474201a95eSRic Aleshire 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) {
14484201a95eSRic Aleshire 		/*
14494201a95eSRic Aleshire 		 * The dataset doesn't have a real label, so fabricate one.
14504201a95eSRic Aleshire 		 */
14514201a95eSRic Aleshire 		char *str = NULL;
14524201a95eSRic Aleshire 
14534201a95eSRic Aleshire 		if (l_to_str_internal(mnt_sl, &str) == 0 &&
14543b2aab18SMatthew Ahrens 		    dsl_prop_set_string(osname,
14553b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_MLSLABEL),
14563b2aab18SMatthew Ahrens 		    ZPROP_SRC_LOCAL, str) == 0)
14574201a95eSRic Aleshire 			retv = 0;
14584201a95eSRic Aleshire 		if (str != NULL)
14594201a95eSRic Aleshire 			kmem_free(str, strlen(str) + 1);
14604201a95eSRic Aleshire 	} else if (hexstr_to_label(ds_hexsl, &ds_sl) == 0) {
14614201a95eSRic Aleshire 		/*
14624201a95eSRic Aleshire 		 * Now compare labels to complete the MAC check.  If the
14634201a95eSRic Aleshire 		 * labels are equal then allow access.  If the mountpoint
14644201a95eSRic Aleshire 		 * label dominates the dataset label, allow readonly access.
14654201a95eSRic Aleshire 		 * Otherwise, access is denied.
14664201a95eSRic Aleshire 		 */
14674201a95eSRic Aleshire 		if (blequal(mnt_sl, &ds_sl))
14684201a95eSRic Aleshire 			retv = 0;
14694201a95eSRic Aleshire 		else if (bldominates(mnt_sl, &ds_sl)) {
14704201a95eSRic Aleshire 			vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
14714201a95eSRic Aleshire 			retv = 0;
14724201a95eSRic Aleshire 		}
14734201a95eSRic Aleshire 	}
14744201a95eSRic Aleshire 
14754201a95eSRic Aleshire 	label_rele(mnt_tsl);
14764201a95eSRic Aleshire 	zone_rele(mntzone);
14774201a95eSRic Aleshire 	return (retv);
14784201a95eSRic Aleshire }
14794201a95eSRic Aleshire 
1480ea8dc4b6Seschrock static int
zfs_mountroot(vfs_t * vfsp,enum whymountroot why)1481ea8dc4b6Seschrock zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
1482ea8dc4b6Seschrock {
1483ea8dc4b6Seschrock 	int error = 0;
1484ea8dc4b6Seschrock 	static int zfsrootdone = 0;
1485ea8dc4b6Seschrock 	zfsvfs_t *zfsvfs = NULL;
1486ea8dc4b6Seschrock 	znode_t *zp = NULL;
1487ea8dc4b6Seschrock 	vnode_t *vp = NULL;
1488e7cbe64fSgw25295 	char *zfs_bootfs;
1489051aabe6Staylor 	char *zfs_devid;
1490ea8dc4b6Seschrock 
1491ea8dc4b6Seschrock 	ASSERT(vfsp);
1492ea8dc4b6Seschrock 
1493ea8dc4b6Seschrock 	/*
1494b1b8ab34Slling 	 * The filesystem that we mount as root is defined in the
1495e7cbe64fSgw25295 	 * boot property "zfs-bootfs" with a format of
1496e7cbe64fSgw25295 	 * "poolname/root-dataset-objnum".
1497ea8dc4b6Seschrock 	 */
1498ea8dc4b6Seschrock 	if (why == ROOT_INIT) {
1499ea8dc4b6Seschrock 		if (zfsrootdone++)
1500be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBUSY));
1501e7cbe64fSgw25295 		/*
1502e7cbe64fSgw25295 		 * the process of doing a spa_load will require the
1503e7cbe64fSgw25295 		 * clock to be set before we could (for example) do
1504e7cbe64fSgw25295 		 * something better by looking at the timestamp on
1505e7cbe64fSgw25295 		 * an uberblock, so just set it to -1.
1506e7cbe64fSgw25295 		 */
1507e7cbe64fSgw25295 		clkset(-1);
1508ea8dc4b6Seschrock 
1509051aabe6Staylor 		if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
1510051aabe6Staylor 			cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
1511051aabe6Staylor 			    "bootfs name");
1512be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
1513986fd29aSsetje 		}
1514051aabe6Staylor 		zfs_devid = spa_get_bootprop("diskdevid");
1515051aabe6Staylor 		error = spa_import_rootpool(rootfs.bo_name, zfs_devid);
1516051aabe6Staylor 		if (zfs_devid)
1517051aabe6Staylor 			spa_free_bootprop(zfs_devid);
1518051aabe6Staylor 		if (error) {
1519051aabe6Staylor 			spa_free_bootprop(zfs_bootfs);
1520051aabe6Staylor 			cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
1521e7cbe64fSgw25295 			    error);
1522b1b8ab34Slling 			return (error);
1523e7cbe64fSgw25295 		}
1524e7cbe64fSgw25295 		if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
1525051aabe6Staylor 			spa_free_bootprop(zfs_bootfs);
1526051aabe6Staylor 			cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
1527e7cbe64fSgw25295 			    error);
1528e7cbe64fSgw25295 			return (error);
1529e7cbe64fSgw25295 		}
1530e7cbe64fSgw25295 
1531051aabe6Staylor 		spa_free_bootprop(zfs_bootfs);
1532ea8dc4b6Seschrock 
1533ea8dc4b6Seschrock 		if (error = vfs_lock(vfsp))
1534ea8dc4b6Seschrock 			return (error);
1535ea8dc4b6Seschrock 
1536088f3894Sahrens 		if (error = zfs_domount(vfsp, rootfs.bo_name)) {
1537051aabe6Staylor 			cmn_err(CE_NOTE, "zfs_domount: error %d", error);
1538ea8dc4b6Seschrock 			goto out;
1539e7cbe64fSgw25295 		}
1540ea8dc4b6Seschrock 
1541ea8dc4b6Seschrock 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
1542ea8dc4b6Seschrock 		ASSERT(zfsvfs);
1543e7cbe64fSgw25295 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
1544051aabe6Staylor 			cmn_err(CE_NOTE, "zfs_zget: error %d", error);
1545ea8dc4b6Seschrock 			goto out;
1546e7cbe64fSgw25295 		}
1547ea8dc4b6Seschrock 
1548ea8dc4b6Seschrock 		vp = ZTOV(zp);
1549ea8dc4b6Seschrock 		mutex_enter(&vp->v_lock);
1550ea8dc4b6Seschrock 		vp->v_flag |= VROOT;
1551ea8dc4b6Seschrock 		mutex_exit(&vp->v_lock);
1552ea8dc4b6Seschrock 		rootvp = vp;
1553ea8dc4b6Seschrock 
1554ea8dc4b6Seschrock 		/*
155540d3dfe1Smarks 		 * Leave rootvp held.  The root file system is never unmounted.
1556ea8dc4b6Seschrock 		 */
1557ea8dc4b6Seschrock 
1558ea8dc4b6Seschrock 		vfs_add((struct vnode *)0, vfsp,
1559ea8dc4b6Seschrock 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
1560ea8dc4b6Seschrock out:
1561ea8dc4b6Seschrock 		vfs_unlock(vfsp);
1562e7cbe64fSgw25295 		return (error);
1563ea8dc4b6Seschrock 	} else if (why == ROOT_REMOUNT) {
1564ea8dc4b6Seschrock 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
1565ea8dc4b6Seschrock 		vfsp->vfs_flag |= VFS_REMOUNT;
1566b510d378Slling 
1567b510d378Slling 		/* refresh mount options */
1568b510d378Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
1569b510d378Slling 		return (zfs_register_callbacks(vfsp));
1570b510d378Slling 
1571ea8dc4b6Seschrock 	} else if (why == ROOT_UNMOUNT) {
1572ea8dc4b6Seschrock 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
1573ea8dc4b6Seschrock 		(void) zfs_sync(vfsp, 0, 0);
1574ea8dc4b6Seschrock 		return (0);
1575ea8dc4b6Seschrock 	}
1576ea8dc4b6Seschrock 
1577ea8dc4b6Seschrock 	/*
1578ea8dc4b6Seschrock 	 * if "why" is equal to anything else other than ROOT_INIT,
1579ea8dc4b6Seschrock 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
1580ea8dc4b6Seschrock 	 */
1581be6fd75aSMatthew Ahrens 	return (SET_ERROR(ENOTSUP));
1582ea8dc4b6Seschrock }
1583ea8dc4b6Seschrock 
1584ea8dc4b6Seschrock /*ARGSUSED*/
1585ea8dc4b6Seschrock static int
zfs_mount(vfs_t * vfsp,vnode_t * mvp,struct mounta * uap,cred_t * cr)1586ea8dc4b6Seschrock zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
1587ea8dc4b6Seschrock {
1588ea8dc4b6Seschrock 	char		*osname;
1589ea8dc4b6Seschrock 	pathname_t	spn;
1590ea8dc4b6Seschrock 	int		error = 0;
1591ea8dc4b6Seschrock 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
1592ea8dc4b6Seschrock 	    UIO_SYSSPACE : UIO_USERSPACE;
1593ea8dc4b6Seschrock 	int		canwrite;
1594ea8dc4b6Seschrock 
1595ea8dc4b6Seschrock 	if (mvp->v_type != VDIR)
1596be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTDIR));
1597ea8dc4b6Seschrock 
1598ea8dc4b6Seschrock 	mutex_enter(&mvp->v_lock);
1599ea8dc4b6Seschrock 	if ((uap->flags & MS_REMOUNT) == 0 &&
1600ea8dc4b6Seschrock 	    (uap->flags & MS_OVERLAY) == 0 &&
1601ea8dc4b6Seschrock 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
1602ea8dc4b6Seschrock 		mutex_exit(&mvp->v_lock);
1603be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
1604ea8dc4b6Seschrock 	}
1605ea8dc4b6Seschrock 	mutex_exit(&mvp->v_lock);
1606ea8dc4b6Seschrock 
1607ea8dc4b6Seschrock 	/*
1608ea8dc4b6Seschrock 	 * ZFS does not support passing unparsed data in via MS_DATA.
1609ea8dc4b6Seschrock 	 * Users should use the MS_OPTIONSTR interface; this means
1610ea8dc4b6Seschrock 	 * that all option parsing is already done and the options struct
1611ea8dc4b6Seschrock 	 * can be interrogated.
1612ea8dc4b6Seschrock 	 */
1613ea8dc4b6Seschrock 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
1614be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1615ea8dc4b6Seschrock 
1616ea8dc4b6Seschrock 	/*
1617ea8dc4b6Seschrock 	 * Get the objset name (the "special" mount argument).
1618ea8dc4b6Seschrock 	 */
1619ea8dc4b6Seschrock 	if (error = pn_get(uap->spec, fromspace, &spn))
1620ea8dc4b6Seschrock 		return (error);
1621ea8dc4b6Seschrock 
1622ea8dc4b6Seschrock 	osname = spn.pn_path;
1623ea8dc4b6Seschrock 
1624ecd6cf80Smarks 	/*
1625ecd6cf80Smarks 	 * Check for mount privilege?
1626ecd6cf80Smarks 	 *
1627ecd6cf80Smarks 	 * If we don't have privilege then see if
1628ecd6cf80Smarks 	 * we have local permission to allow it
1629ecd6cf80Smarks 	 */
1630ecd6cf80Smarks 	error = secpolicy_fs_mount(cr, mvp, vfsp);
1631ecd6cf80Smarks 	if (error) {
163298679b56SMark Shellenbaum 		if (dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) == 0) {
1633ecd6cf80Smarks 			vattr_t		vattr;
1634ecd6cf80Smarks 
1635ecd6cf80Smarks 			/*
1636ecd6cf80Smarks 			 * Make sure user is the owner of the mount point
1637ecd6cf80Smarks 			 * or has sufficient privileges.
1638ecd6cf80Smarks 			 */
1639ecd6cf80Smarks 
1640ecd6cf80Smarks 			vattr.va_mask = AT_UID;
1641ecd6cf80Smarks 
164298679b56SMark Shellenbaum 			if (VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
1643ea8dc4b6Seschrock 				goto out;
1644ecd6cf80Smarks 			}
1645ecd6cf80Smarks 
16462459a9eaSmarks 			if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 &&
16472459a9eaSmarks 			    VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) {
1648ecd6cf80Smarks 				goto out;
1649ecd6cf80Smarks 			}
1650ecd6cf80Smarks 			secpolicy_fs_mount_clearopts(cr, vfsp);
1651ecd6cf80Smarks 		} else {
1652ecd6cf80Smarks 			goto out;
1653ecd6cf80Smarks 		}
1654ecd6cf80Smarks 	}
1655ea8dc4b6Seschrock 
1656ea8dc4b6Seschrock 	/*
1657ea8dc4b6Seschrock 	 * Refuse to mount a filesystem if we are in a local zone and the
1658ea8dc4b6Seschrock 	 * dataset is not visible.
1659ea8dc4b6Seschrock 	 */
1660ea8dc4b6Seschrock 	if (!INGLOBALZONE(curproc) &&
1661ea8dc4b6Seschrock 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1662be6fd75aSMatthew Ahrens 		error = SET_ERROR(EPERM);
1663ea8dc4b6Seschrock 		goto out;
1664ea8dc4b6Seschrock 	}
1665ea8dc4b6Seschrock 
16664201a95eSRic Aleshire 	error = zfs_mount_label_policy(vfsp, osname);
16674201a95eSRic Aleshire 	if (error)
16684201a95eSRic Aleshire 		goto out;
16694201a95eSRic Aleshire 
1670b510d378Slling 	/*
1671b510d378Slling 	 * When doing a remount, we simply refresh our temporary properties
1672b510d378Slling 	 * according to those options set in the current VFS options.
1673b510d378Slling 	 */
1674b510d378Slling 	if (uap->flags & MS_REMOUNT) {
1675b510d378Slling 		/* refresh mount options */
1676b510d378Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
1677b510d378Slling 		error = zfs_register_callbacks(vfsp);
1678b510d378Slling 		goto out;
1679b510d378Slling 	}
1680b510d378Slling 
1681088f3894Sahrens 	error = zfs_domount(vfsp, osname);
1682ea8dc4b6Seschrock 
1683142ae85dSChris Kirby 	/*
1684142ae85dSChris Kirby 	 * Add an extra VFS_HOLD on our parent vfs so that it can't
1685142ae85dSChris Kirby 	 * disappear due to a forced unmount.
1686142ae85dSChris Kirby 	 */
1687984a131bSChris Kirby 	if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap)
1688142ae85dSChris Kirby 		VFS_HOLD(mvp->v_vfsp);
1689142ae85dSChris Kirby 
1690ea8dc4b6Seschrock out:
16918d80322dSAndreas Jaekel 	if (error == 0) {
1692aa075980SAndreas Jaekel 		rw_enter(&rz_zev_rwlock, RW_READER);
1693aa075980SAndreas Jaekel 		if (rz_zev_callbacks && rz_zev_callbacks->rz_zev_zfs_mount)
1694aa075980SAndreas Jaekel 			rz_zev_callbacks->rz_zev_zfs_mount(vfsp, mvp, osname,
1695aa075980SAndreas Jaekel 				uap->flags & MS_REMOUNT ?  B_TRUE : B_FALSE);
1696aa075980SAndreas Jaekel 		rw_exit(&rz_zev_rwlock);
16978d80322dSAndreas Jaekel 	}
1698fa9e4066Sahrens 	pn_free(&spn);
1699fa9e4066Sahrens 	return (error);
1700fa9e4066Sahrens }
1701fa9e4066Sahrens 
1702fa9e4066Sahrens static int
zfs_statvfs(vfs_t * vfsp,struct statvfs64 * statp)1703fa9e4066Sahrens zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1704fa9e4066Sahrens {
1705fa9e4066Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1706fa9e4066Sahrens 	dev32_t d32;
1707a2eea2e1Sahrens 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
1708fa9e4066Sahrens 
1709fa9e4066Sahrens 	ZFS_ENTER(zfsvfs);
1710fa9e4066Sahrens 
1711a2eea2e1Sahrens 	dmu_objset_space(zfsvfs->z_os,
1712a2eea2e1Sahrens 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
1713fa9e4066Sahrens 
1714fa9e4066Sahrens 	/*
1715fa9e4066Sahrens 	 * The underlying storage pool actually uses multiple block sizes.
1716fa9e4066Sahrens 	 * We report the fragsize as the smallest block size we support,
1717fa9e4066Sahrens 	 * and we report our blocksize as the filesystem's maximum blocksize.
1718fa9e4066Sahrens 	 */
1719fa9e4066Sahrens 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1720fa9e4066Sahrens 	statp->f_bsize = zfsvfs->z_max_blksz;
1721fa9e4066Sahrens 
1722fa9e4066Sahrens 	/*
1723fa9e4066Sahrens 	 * The following report "total" blocks of various kinds in the
1724fa9e4066Sahrens 	 * file system, but reported in terms of f_frsize - the
1725fa9e4066Sahrens 	 * "fragment" size.
1726fa9e4066Sahrens 	 */
1727fa9e4066Sahrens 
1728a2eea2e1Sahrens 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
1729a2eea2e1Sahrens 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
1730fa9e4066Sahrens 	statp->f_bavail = statp->f_bfree; /* no root reservation */
1731fa9e4066Sahrens 
1732fa9e4066Sahrens 	/*
1733fa9e4066Sahrens 	 * statvfs() should really be called statufs(), because it assumes
1734fa9e4066Sahrens 	 * static metadata.  ZFS doesn't preallocate files, so the best
1735fa9e4066Sahrens 	 * we can do is report the max that could possibly fit in f_files,
1736fa9e4066Sahrens 	 * and that minus the number actually used in f_ffree.
1737fa9e4066Sahrens 	 * For f_ffree, report the smaller of the number of object available
1738fa9e4066Sahrens 	 * and the number of blocks (each object will take at least a block).
1739fa9e4066Sahrens 	 */
1740a2eea2e1Sahrens 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
1741fa9e4066Sahrens 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
1742a2eea2e1Sahrens 	statp->f_files = statp->f_ffree + usedobjs;
1743fa9e4066Sahrens 
1744fa9e4066Sahrens 	(void) cmpldev(&d32, vfsp->vfs_dev);
1745fa9e4066Sahrens 	statp->f_fsid = d32;
1746fa9e4066Sahrens 
1747fa9e4066Sahrens 	/*
1748fa9e4066Sahrens 	 * We're a zfs filesystem.
1749fa9e4066Sahrens 	 */
1750fa9e4066Sahrens 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1751fa9e4066Sahrens 
1752a5be7ebbSmarks 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1753fa9e4066Sahrens 
1754*675fc291SMatthew Ahrens 	statp->f_namemax = MAXNAMELEN - 1;
1755fa9e4066Sahrens 
1756fa9e4066Sahrens 	/*
1757fa9e4066Sahrens 	 * We have all of 32 characters to stuff a string here.
1758fa9e4066Sahrens 	 * Is there anything useful we could/should provide?
1759fa9e4066Sahrens 	 */
1760fa9e4066Sahrens 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
1761fa9e4066Sahrens 
1762fa9e4066Sahrens 	ZFS_EXIT(zfsvfs);
1763fa9e4066Sahrens 	return (0);
1764fa9e4066Sahrens }
1765fa9e4066Sahrens 
1766fa9e4066Sahrens static int
zfs_root(vfs_t * vfsp,vnode_t ** vpp)1767fa9e4066Sahrens zfs_root(vfs_t *vfsp, vnode_t **vpp)
1768fa9e4066Sahrens {
1769fa9e4066Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1770fa9e4066Sahrens 	znode_t *rootzp;
1771fa9e4066Sahrens 	int error;
1772fa9e4066Sahrens 
1773fa9e4066Sahrens 	ZFS_ENTER(zfsvfs);
1774fa9e4066Sahrens 
1775fa9e4066Sahrens 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1776fa9e4066Sahrens 	if (error == 0)
1777fa9e4066Sahrens 		*vpp = ZTOV(rootzp);
1778fa9e4066Sahrens 
1779fa9e4066Sahrens 	ZFS_EXIT(zfsvfs);
1780fa9e4066Sahrens 	return (error);
1781fa9e4066Sahrens }
1782fa9e4066Sahrens 
1783f18faf3fSek110237 /*
1784f18faf3fSek110237  * Teardown the zfsvfs::z_os.
1785f18faf3fSek110237  *
1786f18faf3fSek110237  * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1787f18faf3fSek110237  * and 'z_teardown_inactive_lock' held.
1788f18faf3fSek110237  */
1789f18faf3fSek110237 static int
zfsvfs_teardown(zfsvfs_t * zfsvfs,boolean_t unmounting)1790f18faf3fSek110237 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1791f18faf3fSek110237 {
1792874395d5Smaybee 	znode_t	*zp;
1793f18faf3fSek110237 
179440d689f8SAlexander Motin 	rrm_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1795f18faf3fSek110237 
1796f18faf3fSek110237 	if (!unmounting) {
1797f18faf3fSek110237 		/*
1798f18faf3fSek110237 		 * We purge the parent filesystem's vfsp as the parent
1799f18faf3fSek110237 		 * filesystem and all of its snapshots have their vnode's
1800f18faf3fSek110237 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
1801f18faf3fSek110237 		 * 'z_parent' is self referential for non-snapshots.
1802f18faf3fSek110237 		 */
1803f18faf3fSek110237 		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1804f18faf3fSek110237 	}
1805f18faf3fSek110237 
1806f18faf3fSek110237 	/*
1807f18faf3fSek110237 	 * Close the zil. NB: Can't close the zil while zfs_inactive
1808f18faf3fSek110237 	 * threads are blocked as zil_close can call zfs_inactive.
1809f18faf3fSek110237 	 */
1810f18faf3fSek110237 	if (zfsvfs->z_log) {
1811f18faf3fSek110237 		zil_close(zfsvfs->z_log);
1812f18faf3fSek110237 		zfsvfs->z_log = NULL;
1813f18faf3fSek110237 	}
1814f18faf3fSek110237 
1815f18faf3fSek110237 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
1816f18faf3fSek110237 
1817f18faf3fSek110237 	/*
1818f18faf3fSek110237 	 * If we are not unmounting (ie: online recv) and someone already
1819f18faf3fSek110237 	 * unmounted this file system while we were doing the switcheroo,
1820f18faf3fSek110237 	 * or a reopen of z_os failed then just bail out now.
1821f18faf3fSek110237 	 */
1822f18faf3fSek110237 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1823f18faf3fSek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
182440d689f8SAlexander Motin 		rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1825be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
1826f18faf3fSek110237 	}
1827f18faf3fSek110237 
1828f18faf3fSek110237 	/*
1829f18faf3fSek110237 	 * At this point there are no vops active, and any new vops will
1830f18faf3fSek110237 	 * fail with EIO since we have z_teardown_lock for writer (only
1831f18faf3fSek110237 	 * relavent for forced unmount).
1832f18faf3fSek110237 	 *
1833f18faf3fSek110237 	 * Release all holds on dbufs.
1834f18faf3fSek110237 	 */
1835f18faf3fSek110237 	mutex_enter(&zfsvfs->z_znodes_lock);
1836874395d5Smaybee 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
1837874395d5Smaybee 	    zp = list_next(&zfsvfs->z_all_znodes, zp))
18380a586ceaSMark Shellenbaum 		if (zp->z_sa_hdl) {
1839874395d5Smaybee 			ASSERT(ZTOV(zp)->v_count > 0);
1840874395d5Smaybee 			zfs_znode_dmu_fini(zp);
1841f18faf3fSek110237 		}
1842f18faf3fSek110237 	mutex_exit(&zfsvfs->z_znodes_lock);
1843f18faf3fSek110237 
1844f18faf3fSek110237 	/*
1845f18faf3fSek110237 	 * If we are unmounting, set the unmounted flag and let new vops
1846f18faf3fSek110237 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
1847f18faf3fSek110237 	 * other vops will fail with EIO.
1848f18faf3fSek110237 	 */
1849f18faf3fSek110237 	if (unmounting) {
1850f18faf3fSek110237 		zfsvfs->z_unmounted = B_TRUE;
185140d689f8SAlexander Motin 		rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1852f18faf3fSek110237 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
1853f18faf3fSek110237 	}
1854f18faf3fSek110237 
1855f18faf3fSek110237 	/*
1856f18faf3fSek110237 	 * z_os will be NULL if there was an error in attempting to reopen
1857f18faf3fSek110237 	 * zfsvfs, so just return as the properties had already been
1858f18faf3fSek110237 	 * unregistered and cached data had been evicted before.
1859f18faf3fSek110237 	 */
1860f18faf3fSek110237 	if (zfsvfs->z_os == NULL)
1861f18faf3fSek110237 		return (0);
1862f18faf3fSek110237 
1863f18faf3fSek110237 	/*
1864f18faf3fSek110237 	 * Unregister properties.
1865f18faf3fSek110237 	 */
1866f18faf3fSek110237 	zfs_unregister_callbacks(zfsvfs);
1867f18faf3fSek110237 
1868f18faf3fSek110237 	/*
1869f18faf3fSek110237 	 * Evict cached data
1870f18faf3fSek110237 	 */
18712e2c1355SMatthew Ahrens 	if (dsl_dataset_is_dirty(dmu_objset_ds(zfsvfs->z_os)) &&
18722e2c1355SMatthew Ahrens 	    !(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY))
1873d3248e8bSmaybee 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
18743b2aab18SMatthew Ahrens 	dmu_objset_evict_dbufs(zfsvfs->z_os);
1875f18faf3fSek110237 
1876f18faf3fSek110237 	return (0);
1877f18faf3fSek110237 }
1878f18faf3fSek110237 
1879fa9e4066Sahrens /*ARGSUSED*/
1880fa9e4066Sahrens static int
zfs_umount(vfs_t * vfsp,int fflag,cred_t * cr)1881fa9e4066Sahrens zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1882fa9e4066Sahrens {
1883fa9e4066Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1884f18faf3fSek110237 	objset_t *os;
1885fa9e4066Sahrens 	int ret;
1886fa9e4066Sahrens 
1887ecd6cf80Smarks 	ret = secpolicy_fs_unmount(cr, vfsp);
1888ecd6cf80Smarks 	if (ret) {
188998679b56SMark Shellenbaum 		if (dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
189098679b56SMark Shellenbaum 		    ZFS_DELEG_PERM_MOUNT, cr))
1891fa9e4066Sahrens 			return (ret);
1892ecd6cf80Smarks 	}
1893033f9833Sek110237 
1894ed097989Sek110237 	/*
1895ed097989Sek110237 	 * We purge the parent filesystem's vfsp as the parent filesystem
1896ed097989Sek110237 	 * and all of its snapshots have their vnode's v_vfsp set to the
1897ed097989Sek110237 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
1898ed097989Sek110237 	 * referential for non-snapshots.
1899ed097989Sek110237 	 */
1900ed097989Sek110237 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1901033f9833Sek110237 
1902fa9e4066Sahrens 	/*
1903fa9e4066Sahrens 	 * Unmount any snapshots mounted under .zfs before unmounting the
1904fa9e4066Sahrens 	 * dataset itself.
1905fa9e4066Sahrens 	 */
1906fa9e4066Sahrens 	if (zfsvfs->z_ctldir != NULL &&
1907ecd6cf80Smarks 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1908fa9e4066Sahrens 		return (ret);
1909ecd6cf80Smarks 	}
1910fa9e4066Sahrens 
191191ebeef5Sahrens 	if (!(fflag & MS_FORCE)) {
1912fa9e4066Sahrens 		/*
1913fa9e4066Sahrens 		 * Check the number of active vnodes in the file system.
191491ebeef5Sahrens 		 * Our count is maintained in the vfs structure, but the
191591ebeef5Sahrens 		 * number is off by 1 to indicate a hold on the vfs
191691ebeef5Sahrens 		 * structure itself.
1917fa9e4066Sahrens 		 *
191891ebeef5Sahrens 		 * The '.zfs' directory maintains a reference of its
191991ebeef5Sahrens 		 * own, and any active references underneath are
192091ebeef5Sahrens 		 * reflected in the vnode count.
1921fa9e4066Sahrens 		 */
1922fa9e4066Sahrens 		if (zfsvfs->z_ctldir == NULL) {
1923893a6d32Sahrens 			if (vfsp->vfs_count > 1)
1924be6fd75aSMatthew Ahrens 				return (SET_ERROR(EBUSY));
1925fa9e4066Sahrens 		} else {
1926fa9e4066Sahrens 			if (vfsp->vfs_count > 2 ||
1927f18faf3fSek110237 			    zfsvfs->z_ctldir->v_count > 1)
1928be6fd75aSMatthew Ahrens 				return (SET_ERROR(EBUSY));
1929fa9e4066Sahrens 		}
1930fa9e4066Sahrens 	}
1931fa9e4066Sahrens 
1932fa9e4066Sahrens 	vfsp->vfs_flag |= VFS_UNMOUNTED;
193391ebeef5Sahrens 
19345be89596SAndreas Jaekel 	rw_enter(&rz_zev_rwlock, RW_READER);
19355be89596SAndreas Jaekel 	if (rz_zev_callbacks && rz_zev_callbacks->rz_zev_zfs_umount)
19365be89596SAndreas Jaekel 		rz_zev_callbacks->rz_zev_zfs_umount(vfsp);
19375be89596SAndreas Jaekel 	rw_exit(&rz_zev_rwlock);
19385be89596SAndreas Jaekel 
1939f18faf3fSek110237 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1940f18faf3fSek110237 	os = zfsvfs->z_os;
194191ebeef5Sahrens 
194291ebeef5Sahrens 	/*
1943f18faf3fSek110237 	 * z_os will be NULL if there was an error in
1944f18faf3fSek110237 	 * attempting to reopen zfsvfs.
194591ebeef5Sahrens 	 */
1946f18faf3fSek110237 	if (os != NULL) {
194791ebeef5Sahrens 		/*
1948f18faf3fSek110237 		 * Unset the objset user_ptr.
194991ebeef5Sahrens 		 */
1950503ad85cSMatthew Ahrens 		mutex_enter(&os->os_user_ptr_lock);
1951f18faf3fSek110237 		dmu_objset_set_user(os, NULL);
1952503ad85cSMatthew Ahrens 		mutex_exit(&os->os_user_ptr_lock);
195391ebeef5Sahrens 
195491ebeef5Sahrens 		/*
1955745cd3c5Smaybee 		 * Finally release the objset
195691ebeef5Sahrens 		 */
1957503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zfsvfs);
1958f18faf3fSek110237 	}
195991ebeef5Sahrens 
196091ebeef5Sahrens 	/*
196191ebeef5Sahrens 	 * We can now safely destroy the '.zfs' directory node.
196291ebeef5Sahrens 	 */
196391ebeef5Sahrens 	if (zfsvfs->z_ctldir != NULL)
196491ebeef5Sahrens 		zfsctl_destroy(zfsvfs);
1965fa9e4066Sahrens 
1966fa9e4066Sahrens 	return (0);
1967fa9e4066Sahrens }
1968fa9e4066Sahrens 
1969fa9e4066Sahrens static int
zfs_vget(vfs_t * vfsp,vnode_t ** vpp,fid_t * fidp)1970fa9e4066Sahrens zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1971fa9e4066Sahrens {
1972fa9e4066Sahrens 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1973fa9e4066Sahrens 	znode_t		*zp;
1974fa9e4066Sahrens 	uint64_t	object = 0;
1975fa9e4066Sahrens 	uint64_t	fid_gen = 0;
1976fa9e4066Sahrens 	uint64_t	gen_mask;
1977fa9e4066Sahrens 	uint64_t	zp_gen;
1978fa9e4066Sahrens 	int 		i, err;
1979fa9e4066Sahrens 
1980fa9e4066Sahrens 	*vpp = NULL;
1981fa9e4066Sahrens 
1982fa9e4066Sahrens 	ZFS_ENTER(zfsvfs);
1983fa9e4066Sahrens 
1984fa9e4066Sahrens 	if (fidp->fid_len == LONG_FID_LEN) {
1985fa9e4066Sahrens 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1986fa9e4066Sahrens 		uint64_t	objsetid = 0;
1987fa9e4066Sahrens 		uint64_t	setgen = 0;
1988fa9e4066Sahrens 
1989fa9e4066Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1990fa9e4066Sahrens 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1991fa9e4066Sahrens 
1992fa9e4066Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1993fa9e4066Sahrens 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1994fa9e4066Sahrens 
1995fa9e4066Sahrens 		ZFS_EXIT(zfsvfs);
1996fa9e4066Sahrens 
1997fa9e4066Sahrens 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1998fa9e4066Sahrens 		if (err)
1999be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
2000fa9e4066Sahrens 		ZFS_ENTER(zfsvfs);
2001fa9e4066Sahrens 	}
2002fa9e4066Sahrens 
2003fa9e4066Sahrens 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
2004fa9e4066Sahrens 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
2005fa9e4066Sahrens 
2006fa9e4066Sahrens 		for (i = 0; i < sizeof (zfid->zf_object); i++)
2007fa9e4066Sahrens 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
2008fa9e4066Sahrens 
2009fa9e4066Sahrens 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
2010fa9e4066Sahrens 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
2011fa9e4066Sahrens 	} else {
2012fa9e4066Sahrens 		ZFS_EXIT(zfsvfs);
2013be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2014fa9e4066Sahrens 	}
2015fa9e4066Sahrens 
2016fa9e4066Sahrens 	/* A zero fid_gen means we are in the .zfs control directories */
2017fa9e4066Sahrens 	if (fid_gen == 0 &&
2018fa9e4066Sahrens 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
2019fa9e4066Sahrens 		*vpp = zfsvfs->z_ctldir;
2020fa9e4066Sahrens 		ASSERT(*vpp != NULL);
2021fa9e4066Sahrens 		if (object == ZFSCTL_INO_SNAPDIR) {
2022fa9e4066Sahrens 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
2023da6c28aaSamw 			    0, NULL, NULL, NULL, NULL, NULL) == 0);
2024fa9e4066Sahrens 		} else {
2025fa9e4066Sahrens 			VN_HOLD(*vpp);
2026fa9e4066Sahrens 		}
2027fa9e4066Sahrens 		ZFS_EXIT(zfsvfs);
2028fa9e4066Sahrens 		return (0);
2029fa9e4066Sahrens 	}
2030fa9e4066Sahrens 
2031fa9e4066Sahrens 	gen_mask = -1ULL >> (64 - 8 * i);
2032fa9e4066Sahrens 
2033fa9e4066Sahrens 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
2034fa9e4066Sahrens 	if (err = zfs_zget(zfsvfs, object, &zp)) {
2035fa9e4066Sahrens 		ZFS_EXIT(zfsvfs);
2036fa9e4066Sahrens 		return (err);
2037fa9e4066Sahrens 	}
20380a586ceaSMark Shellenbaum 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
20390a586ceaSMark Shellenbaum 	    sizeof (uint64_t));
20400a586ceaSMark Shellenbaum 	zp_gen = zp_gen & gen_mask;
2041fa9e4066Sahrens 	if (zp_gen == 0)
2042fa9e4066Sahrens 		zp_gen = 1;
2043893a6d32Sahrens 	if (zp->z_unlinked || zp_gen != fid_gen) {
2044fa9e4066Sahrens 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
2045fa9e4066Sahrens 		VN_RELE(ZTOV(zp));
2046fa9e4066Sahrens 		ZFS_EXIT(zfsvfs);
2047be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2048fa9e4066Sahrens 	}
2049fa9e4066Sahrens 
2050fa9e4066Sahrens 	*vpp = ZTOV(zp);
2051fa9e4066Sahrens 	ZFS_EXIT(zfsvfs);
2052fa9e4066Sahrens 	return (0);
2053fa9e4066Sahrens }
2054fa9e4066Sahrens 
2055f18faf3fSek110237 /*
2056f18faf3fSek110237  * Block out VOPs and close zfsvfs_t::z_os
2057f18faf3fSek110237  *
2058f18faf3fSek110237  * Note, if successful, then we return with the 'z_teardown_lock' and
205991948b51SKeith M Wesolowski  * 'z_teardown_inactive_lock' write held.  We leave ownership of the underlying
206091948b51SKeith M Wesolowski  * dataset and objset intact so that they can be atomically handed off during
206191948b51SKeith M Wesolowski  * a subsequent rollback or recv operation and the resume thereafter.
2062f18faf3fSek110237  */
2063f18faf3fSek110237 int
zfs_suspend_fs(zfsvfs_t * zfsvfs)2064503ad85cSMatthew Ahrens zfs_suspend_fs(zfsvfs_t *zfsvfs)
2065f18faf3fSek110237 {
2066f18faf3fSek110237 	int error;
2067f18faf3fSek110237 
2068f18faf3fSek110237 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
2069f18faf3fSek110237 		return (error);
2070f18faf3fSek110237 
2071f18faf3fSek110237 	return (0);
2072f18faf3fSek110237 }
2073f18faf3fSek110237 
2074f18faf3fSek110237 /*
207591948b51SKeith M Wesolowski  * Rebuild SA and release VOPs.  Note that ownership of the underlying dataset
207691948b51SKeith M Wesolowski  * is an invariant across any of the operations that can be performed while the
207791948b51SKeith M Wesolowski  * filesystem was suspended.  Whether it succeeded or failed, the preconditions
207891948b51SKeith M Wesolowski  * are the same: the relevant objset and associated dataset are owned by
207991948b51SKeith M Wesolowski  * zfsvfs, held, and long held on entry.
2080f18faf3fSek110237  */
2081f18faf3fSek110237 int
zfs_resume_fs(zfsvfs_t * zfsvfs,const char * osname)2082503ad85cSMatthew Ahrens zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname)
2083f18faf3fSek110237 {
208444bffe01SMark Shellenbaum 	int err;
208591948b51SKeith M Wesolowski 	znode_t *zp;
208691948b51SKeith M Wesolowski 	uint64_t sa_obj = 0;
2087f18faf3fSek110237 
208840d689f8SAlexander Motin 	ASSERT(RRM_WRITE_HELD(&zfsvfs->z_teardown_lock));
2089f18faf3fSek110237 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
2090f18faf3fSek110237 
209191948b51SKeith M Wesolowski 	/*
209291948b51SKeith M Wesolowski 	 * We already own this, so just hold and rele it to update the
209391948b51SKeith M Wesolowski 	 * objset_t, as the one we had before may have been evicted.
209491948b51SKeith M Wesolowski 	 */
209591948b51SKeith M Wesolowski 	VERIFY0(dmu_objset_hold(osname, zfsvfs, &zfsvfs->z_os));
209691948b51SKeith M Wesolowski 	VERIFY3P(zfsvfs->z_os->os_dsl_dataset->ds_owner, ==, zfsvfs);
209791948b51SKeith M Wesolowski 	VERIFY(dsl_dataset_long_held(zfsvfs->z_os->os_dsl_dataset));
209891948b51SKeith M Wesolowski 	dmu_objset_rele(zfsvfs->z_os, zfsvfs);
20990a586ceaSMark Shellenbaum 
210044bffe01SMark Shellenbaum 	/*
210144bffe01SMark Shellenbaum 	 * Make sure version hasn't changed
210244bffe01SMark Shellenbaum 	 */
21030a586ceaSMark Shellenbaum 
210444bffe01SMark Shellenbaum 	err = zfs_get_zplprop(zfsvfs->z_os, ZFS_PROP_VERSION,
210544bffe01SMark Shellenbaum 	    &zfsvfs->z_version);
210644bffe01SMark Shellenbaum 
210744bffe01SMark Shellenbaum 	if (err)
21080a586ceaSMark Shellenbaum 		goto bail;
21090a586ceaSMark Shellenbaum 
211044bffe01SMark Shellenbaum 	err = zap_lookup(zfsvfs->z_os, MASTER_NODE_OBJ,
211144bffe01SMark Shellenbaum 	    ZFS_SA_ATTRS, 8, 1, &sa_obj);
211244bffe01SMark Shellenbaum 
211344bffe01SMark Shellenbaum 	if (err && zfsvfs->z_version >= ZPL_VERSION_SA)
211444bffe01SMark Shellenbaum 		goto bail;
21150a586ceaSMark Shellenbaum 
21161d8ccc7bSMark Shellenbaum 	if ((err = sa_setup(zfsvfs->z_os, sa_obj,
21171d8ccc7bSMark Shellenbaum 	    zfs_attr_table,  ZPL_END, &zfsvfs->z_attr_table)) != 0)
21181d8ccc7bSMark Shellenbaum 		goto bail;
2119f18faf3fSek110237 
212044bffe01SMark Shellenbaum 	if (zfsvfs->z_version >= ZPL_VERSION_SA)
212144bffe01SMark Shellenbaum 		sa_register_update_callback(zfsvfs->z_os,
212244bffe01SMark Shellenbaum 		    zfs_sa_upgrade);
212344bffe01SMark Shellenbaum 
2124f18faf3fSek110237 	VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
2125f18faf3fSek110237 
212644bffe01SMark Shellenbaum 	zfs_set_fuid_feature(zfsvfs);
212744bffe01SMark Shellenbaum 
2128f18faf3fSek110237 	/*
2129f18faf3fSek110237 	 * Attempt to re-establish all the active znodes with
2130f18faf3fSek110237 	 * their dbufs.  If a zfs_rezget() fails, then we'll let
2131f18faf3fSek110237 	 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
2132f18faf3fSek110237 	 * when they try to use their znode.
2133f18faf3fSek110237 	 */
2134f18faf3fSek110237 	mutex_enter(&zfsvfs->z_znodes_lock);
2135f18faf3fSek110237 	for (zp = list_head(&zfsvfs->z_all_znodes); zp;
2136f18faf3fSek110237 	    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
2137f18faf3fSek110237 		(void) zfs_rezget(zp);
2138f18faf3fSek110237 	}
2139f18faf3fSek110237 	mutex_exit(&zfsvfs->z_znodes_lock);
2140f18faf3fSek110237 
21410a586ceaSMark Shellenbaum bail:
2142f18faf3fSek110237 	/* release the VOPs */
2143f18faf3fSek110237 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
214440d689f8SAlexander Motin 	rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2145f18faf3fSek110237 
2146f18faf3fSek110237 	if (err) {
2147f18faf3fSek110237 		/*
214891948b51SKeith M Wesolowski 		 * Since we couldn't setup the sa framework, try to force
214991948b51SKeith M Wesolowski 		 * unmount this file system.
2150f18faf3fSek110237 		 */
2151f18faf3fSek110237 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
2152f18faf3fSek110237 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
2153f18faf3fSek110237 	}
2154f18faf3fSek110237 	return (err);
2155f18faf3fSek110237 }
2156f18faf3fSek110237 
2157fa9e4066Sahrens static void
zfs_freevfs(vfs_t * vfsp)2158fa9e4066Sahrens zfs_freevfs(vfs_t *vfsp)
2159fa9e4066Sahrens {
2160fa9e4066Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
2161142ae85dSChris Kirby 
2162142ae85dSChris Kirby 	/*
2163142ae85dSChris Kirby 	 * If this is a snapshot, we have an extra VFS_HOLD on our parent
2164f80ce222SChris Kirby 	 * from zfs_mount().  Release it here.  If we came through
2165f80ce222SChris Kirby 	 * zfs_mountroot() instead, we didn't grab an extra hold, so
2166f80ce222SChris Kirby 	 * skip the VFS_RELE for rootvfs.
2167142ae85dSChris Kirby 	 */
2168f80ce222SChris Kirby 	if (zfsvfs->z_issnap && (vfsp != rootvfs))
2169142ae85dSChris Kirby 		VFS_RELE(zfsvfs->z_parent->z_vfs);
2170142ae85dSChris Kirby 
217114843421SMatthew Ahrens 	zfsvfs_free(zfsvfs);
2172fa9e4066Sahrens 
21730d6bb4c6SJosef 'Jeff' Sipek 	atomic_dec_32(&zfs_active_fs_count);
2174fa9e4066Sahrens }
2175fa9e4066Sahrens 
2176fa9e4066Sahrens /*
2177fa9e4066Sahrens  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
2178fa9e4066Sahrens  * so we can't safely do any non-idempotent initialization here.
2179fa9e4066Sahrens  * Leave that to zfs_init() and zfs_fini(), which are called
2180fa9e4066Sahrens  * from the module's _init() and _fini() entry points.
2181fa9e4066Sahrens  */
2182fa9e4066Sahrens /*ARGSUSED*/
2183fa9e4066Sahrens static int
zfs_vfsinit(int fstype,char * name)2184fa9e4066Sahrens zfs_vfsinit(int fstype, char *name)
2185fa9e4066Sahrens {
2186fa9e4066Sahrens 	int error;
2187fa9e4066Sahrens 
2188fa9e4066Sahrens 	zfsfstype = fstype;
2189fa9e4066Sahrens 
2190fa9e4066Sahrens 	/*
2191fa9e4066Sahrens 	 * Setup vfsops and vnodeops tables.
2192fa9e4066Sahrens 	 */
2193fa9e4066Sahrens 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
2194fa9e4066Sahrens 	if (error != 0) {
2195fa9e4066Sahrens 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
2196fa9e4066Sahrens 	}
2197fa9e4066Sahrens 
2198fa9e4066Sahrens 	error = zfs_create_op_tables();
2199fa9e4066Sahrens 	if (error) {
2200fa9e4066Sahrens 		zfs_remove_op_tables();
2201fa9e4066Sahrens 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
2202fa9e4066Sahrens 		(void) vfs_freevfsops_by_type(zfsfstype);
2203fa9e4066Sahrens 		return (error);
2204fa9e4066Sahrens 	}
2205fa9e4066Sahrens 
2206fa9e4066Sahrens 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
2207fa9e4066Sahrens 
2208fa9e4066Sahrens 	/*
2209a0965f35Sbonwick 	 * Unique major number for all zfs mounts.
2210a0965f35Sbonwick 	 * If we run out of 32-bit minors, we'll getudev() another major.
2211fa9e4066Sahrens 	 */
2212a0965f35Sbonwick 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
2213a0965f35Sbonwick 	zfs_minor = ZFS_MIN_MINOR;
2214fa9e4066Sahrens 
2215fa9e4066Sahrens 	return (0);
2216fa9e4066Sahrens }
2217fa9e4066Sahrens 
2218fa9e4066Sahrens void
zfs_init(void)2219fa9e4066Sahrens zfs_init(void)
2220fa9e4066Sahrens {
2221fa9e4066Sahrens 	/*
2222fa9e4066Sahrens 	 * Initialize .zfs directory structures
2223fa9e4066Sahrens 	 */
2224fa9e4066Sahrens 	zfsctl_init();
2225fa9e4066Sahrens 
2226fa9e4066Sahrens 	/*
2227fa9e4066Sahrens 	 * Initialize znode cache, vnode ops, etc...
2228fa9e4066Sahrens 	 */
2229fa9e4066Sahrens 	zfs_znode_init();
223014843421SMatthew Ahrens 
223114843421SMatthew Ahrens 	dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
2232fa9e4066Sahrens }
2233fa9e4066Sahrens 
2234fa9e4066Sahrens void
zfs_fini(void)2235fa9e4066Sahrens zfs_fini(void)
2236fa9e4066Sahrens {
2237fa9e4066Sahrens 	zfsctl_fini();
2238fa9e4066Sahrens 	zfs_znode_fini();
2239fa9e4066Sahrens }
2240fa9e4066Sahrens 
2241fa9e4066Sahrens int
zfs_busy(void)2242fa9e4066Sahrens zfs_busy(void)
2243fa9e4066Sahrens {
2244fa9e4066Sahrens 	return (zfs_active_fs_count != 0);
2245fa9e4066Sahrens }
2246fa9e4066Sahrens 
2247e7437265Sahrens int
zfs_set_version(zfsvfs_t * zfsvfs,uint64_t newvers)224814843421SMatthew Ahrens zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
2249e7437265Sahrens {
2250e7437265Sahrens 	int error;
225114843421SMatthew Ahrens 	objset_t *os = zfsvfs->z_os;
2252e7437265Sahrens 	dmu_tx_t *tx;
2253e7437265Sahrens 
2254e7437265Sahrens 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
2255be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2256e7437265Sahrens 
225714843421SMatthew Ahrens 	if (newvers < zfsvfs->z_version)
2258be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2259e7437265Sahrens 
22600a586ceaSMark Shellenbaum 	if (zfs_spa_version_map(newvers) >
22610a586ceaSMark Shellenbaum 	    spa_version(dmu_objset_spa(zfsvfs->z_os)))
2262be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
22630a586ceaSMark Shellenbaum 
2264e7437265Sahrens 	tx = dmu_tx_create(os);
226514843421SMatthew Ahrens 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
22660a586ceaSMark Shellenbaum 	if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
22670a586ceaSMark Shellenbaum 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
22680a586ceaSMark Shellenbaum 		    ZFS_SA_ATTRS);
22690a586ceaSMark Shellenbaum 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
22700a586ceaSMark Shellenbaum 	}
2271e7437265Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
2272e7437265Sahrens 	if (error) {
2273e7437265Sahrens 		dmu_tx_abort(tx);
227414843421SMatthew Ahrens 		return (error);
2275e7437265Sahrens 	}
22760a586ceaSMark Shellenbaum 
227714843421SMatthew Ahrens 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
227814843421SMatthew Ahrens 	    8, 1, &newvers, tx);
227914843421SMatthew Ahrens 
228014843421SMatthew Ahrens 	if (error) {
228114843421SMatthew Ahrens 		dmu_tx_commit(tx);
228214843421SMatthew Ahrens 		return (error);
228314843421SMatthew Ahrens 	}
2284e7437265Sahrens 
22850a586ceaSMark Shellenbaum 	if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
22860a586ceaSMark Shellenbaum 		uint64_t sa_obj;
22870a586ceaSMark Shellenbaum 
22880a586ceaSMark Shellenbaum 		ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
22890a586ceaSMark Shellenbaum 		    SPA_VERSION_SA);
22900a586ceaSMark Shellenbaum 		sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
22910a586ceaSMark Shellenbaum 		    DMU_OT_NONE, 0, tx);
22920a586ceaSMark Shellenbaum 
22930a586ceaSMark Shellenbaum 		error = zap_add(os, MASTER_NODE_OBJ,
22940a586ceaSMark Shellenbaum 		    ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
2295fb09f5aaSMadhav Suresh 		ASSERT0(error);
22960a586ceaSMark Shellenbaum 
22970a586ceaSMark Shellenbaum 		VERIFY(0 == sa_set_sa_object(os, sa_obj));
22980a586ceaSMark Shellenbaum 		sa_register_update_callback(os, zfs_sa_upgrade);
22990a586ceaSMark Shellenbaum 	}
23000a586ceaSMark Shellenbaum 
23014445fffbSMatthew Ahrens 	spa_history_log_internal_ds(dmu_objset_ds(os), "upgrade", tx,
23024445fffbSMatthew Ahrens 	    "from %llu to %llu", zfsvfs->z_version, newvers);
230314843421SMatthew Ahrens 
2304e7437265Sahrens 	dmu_tx_commit(tx);
2305e7437265Sahrens 
230614843421SMatthew Ahrens 	zfsvfs->z_version = newvers;
230714843421SMatthew Ahrens 
230814843421SMatthew Ahrens 	zfs_set_fuid_feature(zfsvfs);
230914843421SMatthew Ahrens 
231014843421SMatthew Ahrens 	return (0);
2311e7437265Sahrens }
2312e7437265Sahrens 
2313de8267e0Stimh /*
2314de8267e0Stimh  * Read a property stored within the master node.
2315de8267e0Stimh  */
2316de8267e0Stimh int
zfs_get_zplprop(objset_t * os,zfs_prop_t prop,uint64_t * value)2317de8267e0Stimh zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
2318de8267e0Stimh {
2319de8267e0Stimh 	const char *pname;
23200a48a24eStimh 	int error = ENOENT;
2321de8267e0Stimh 
2322de8267e0Stimh 	/*
2323de8267e0Stimh 	 * Look up the file system's value for the property.  For the
2324de8267e0Stimh 	 * version property, we look up a slightly different string.
2325de8267e0Stimh 	 */
2326de8267e0Stimh 	if (prop == ZFS_PROP_VERSION)
2327de8267e0Stimh 		pname = ZPL_VERSION_STR;
2328de8267e0Stimh 	else
2329de8267e0Stimh 		pname = zfs_prop_to_name(prop);
2330de8267e0Stimh 
23310a48a24eStimh 	if (os != NULL)
2332de8267e0Stimh 		error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
2333de8267e0Stimh 
2334aa60ed0eSmaybee 	if (error == ENOENT) {
2335de8267e0Stimh 		/* No value set, use the default value */
2336de8267e0Stimh 		switch (prop) {
2337aa60ed0eSmaybee 		case ZFS_PROP_VERSION:
2338aa60ed0eSmaybee 			*value = ZPL_VERSION;
2339aa60ed0eSmaybee 			break;
2340de8267e0Stimh 		case ZFS_PROP_NORMALIZE:
2341de8267e0Stimh 		case ZFS_PROP_UTF8ONLY:
2342de8267e0Stimh 			*value = 0;
2343de8267e0Stimh 			break;
2344de8267e0Stimh 		case ZFS_PROP_CASE:
2345de8267e0Stimh 			*value = ZFS_CASE_SENSITIVE;
2346de8267e0Stimh 			break;
2347de8267e0Stimh 		default:
2348aa60ed0eSmaybee 			return (error);
2349de8267e0Stimh 		}
2350aa60ed0eSmaybee 		error = 0;
2351de8267e0Stimh 	}
2352aa60ed0eSmaybee 	return (error);
2353de8267e0Stimh }
2354de8267e0Stimh 
2355fa9e4066Sahrens static vfsdef_t vfw = {
2356fa9e4066Sahrens 	VFSDEF_VERSION,
2357fa9e4066Sahrens 	MNTTYPE_ZFS,
2358fa9e4066Sahrens 	zfs_vfsinit,
2359da6c28aaSamw 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS|
23600fbb751dSJohn Levon 	    VSW_XID|VSW_ZMOUNT,
2361fa9e4066Sahrens 	&zfs_mntopts
2362fa9e4066Sahrens };
2363fa9e4066Sahrens 
2364fa9e4066Sahrens struct modlfs zfs_modlfs = {
2365e7437265Sahrens 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
2366fa9e4066Sahrens };
2367