xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 70561057)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5033f9833Sek  * Common Development and Distribution License (the "License").
6033f9833Sek  * 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.
231fdcbd00SMatthew Ahrens  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
25eb721827SAlek Pinchuk  * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
26f67950b2SNasf-Fan  * Copyright 2019 Joyent, Inc.
2730c304d9SJoshua M. Clulow  * Copyright 2020 Joshua M. Clulow <josh@sysmgr.org>
28b39b008fSAndy Fiddaman  * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
2900bfaff9SPatrick Mooney  * Copyright 2022 Oxide Computer Company
30fa9e4066Sahrens  */
31fa9e4066Sahrens 
3255da60b9SMark J Musante /* Portions Copyright 2010 Robert Milkowski */
3355da60b9SMark J Musante 
34fa9e4066Sahrens #include <sys/types.h>
35fa9e4066Sahrens #include <sys/param.h>
36fa9e4066Sahrens #include <sys/systm.h>
37fa9e4066Sahrens #include <sys/sysmacros.h>
38fa9e4066Sahrens #include <sys/kmem.h>
39fa9e4066Sahrens #include <sys/pathname.h>
40fa9e4066Sahrens #include <sys/vnode.h>
41fa9e4066Sahrens #include <sys/vfs.h>
42aa59c4cbSrsb #include <sys/vfs_opreg.h>
43fa9e4066Sahrens #include <sys/mntent.h>
44fa9e4066Sahrens #include <sys/mount.h>
45fa9e4066Sahrens #include <sys/cmn_err.h>
46fa9e4066Sahrens #include "fs/fs_subr.h"
47fa9e4066Sahrens #include <sys/zfs_znode.h>
48893a6d32Sahrens #include <sys/zfs_dir.h>
49fa9e4066Sahrens #include <sys/zil.h>
50fa9e4066Sahrens #include <sys/fs/zfs.h>
51fa9e4066Sahrens #include <sys/dmu.h>
52fa9e4066Sahrens #include <sys/dsl_prop.h>
53b1b8ab34Slling #include <sys/dsl_dataset.h>
54ecd6cf80Smarks #include <sys/dsl_deleg.h>
55fa9e4066Sahrens #include <sys/spa.h>
56fa9e4066Sahrens #include <sys/zap.h>
570a586ceaSMark Shellenbaum #include <sys/sa.h>
58e828a46dSMatthew Ahrens #include <sys/sa_impl.h>
59fa9e4066Sahrens #include <sys/varargs.h>
60fa9e4066Sahrens #include <sys/policy.h>
61fa9e4066Sahrens #include <sys/atomic.h>
62fa9e4066Sahrens #include <sys/mkdev.h>
63fa9e4066Sahrens #include <sys/modctl.h>
64ecd6cf80Smarks #include <sys/refstr.h>
65fa9e4066Sahrens #include <sys/zfs_ioctl.h>
66fa9e4066Sahrens #include <sys/zfs_ctldir.h>
67da6c28aaSamw #include <sys/zfs_fuid.h>
68ea8dc4b6Seschrock #include <sys/bootconf.h>
6930c304d9SJoshua M. Clulow #include <sys/ddi.h>
70a0965f35Sbonwick #include <sys/sunddi.h>
71033f9833Sek #include <sys/dnlc.h>
72f18faf3fSek #include <sys/dmu_objset.h>
73e7cbe64fSgw #include <sys/spa_boot.h>
7430c304d9SJoshua M. Clulow #include <sys/vdev_impl.h>
750a586ceaSMark Shellenbaum #include "zfs_comutil.h"
76fa9e4066Sahrens 
77fa9e4066Sahrens int zfsfstype;
78fa9e4066Sahrens vfsops_t *zfs_vfsops = NULL;
79a0965f35Sbonwick static major_t zfs_major;
80fa9e4066Sahrens static minor_t zfs_minor;
81fa9e4066Sahrens static kmutex_t	zfs_dev_mtx;
82fa9e4066Sahrens 
8354d692b7SGeorge Wilson extern int sys_shutdown;
8454d692b7SGeorge Wilson 
85fa9e4066Sahrens static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
86fa9e4066Sahrens static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr);
87ea8dc4b6Seschrock static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
88fa9e4066Sahrens static int zfs_root(vfs_t *vfsp, vnode_t **vpp);
89fa9e4066Sahrens static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp);
90fa9e4066Sahrens static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp);
91fa9e4066Sahrens static void zfs_freevfs(vfs_t *vfsp);
92fa9e4066Sahrens 
93fa9e4066Sahrens static const fs_operation_def_t zfs_vfsops_template[] = {
94aa59c4cbSrsb 	VFSNAME_MOUNT,		{ .vfs_mount = zfs_mount },
95aa59c4cbSrsb 	VFSNAME_MOUNTROOT,	{ .vfs_mountroot = zfs_mountroot },
96aa59c4cbSrsb 	VFSNAME_UNMOUNT,	{ .vfs_unmount = zfs_umount },
97aa59c4cbSrsb 	VFSNAME_ROOT,		{ .vfs_root = zfs_root },
98aa59c4cbSrsb 	VFSNAME_STATVFS,	{ .vfs_statvfs = zfs_statvfs },
99aa59c4cbSrsb 	VFSNAME_SYNC,		{ .vfs_sync = zfs_sync },
100aa59c4cbSrsb 	VFSNAME_VGET,		{ .vfs_vget = zfs_vget },
101aa59c4cbSrsb 	VFSNAME_FREEVFS,	{ .vfs_freevfs = zfs_freevfs },
102aa59c4cbSrsb 	NULL,			NULL
103fa9e4066Sahrens };
104fa9e4066Sahrens 
105fa9e4066Sahrens /*
106fa9e4066Sahrens  * We need to keep a count of active fs's.
107fa9e4066Sahrens  * This is necessary to prevent our module
108fa9e4066Sahrens  * from being unloaded after a umount -f
109fa9e4066Sahrens  */
110fa9e4066Sahrens static uint32_t	zfs_active_fs_count = 0;
111fa9e4066Sahrens 
112fa9e4066Sahrens static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
113fa9e4066Sahrens static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
1147b55fa8eSck static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
1157b55fa8eSck static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
116fa9e4066Sahrens 
1177b55fa8eSck /*
118b510d378Slling  * MO_DEFAULT is not used since the default value is determined
119b510d378Slling  * by the equivalent property.
1207b55fa8eSck  */
121fa9e4066Sahrens static mntopt_t mntopts[] = {
1227b55fa8eSck 	{ MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL },
1237b55fa8eSck 	{ MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL },
124b510d378Slling 	{ MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL },
125fa9e4066Sahrens 	{ MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
126fa9e4066Sahrens };
127fa9e4066Sahrens 
128fa9e4066Sahrens static mntopts_t zfs_mntopts = {
129fa9e4066Sahrens 	sizeof (mntopts) / sizeof (mntopt_t),
130fa9e4066Sahrens 	mntopts
131fa9e4066Sahrens };
132fa9e4066Sahrens 
133fa9e4066Sahrens /*ARGSUSED*/
134fa9e4066Sahrens int
zfs_sync(vfs_t * vfsp,short flag,cred_t * cr)135fa9e4066Sahrens zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
136fa9e4066Sahrens {
137fa9e4066Sahrens 	/*
138fa9e4066Sahrens 	 * Data integrity is job one.  We don't want a compromised kernel
139fa9e4066Sahrens 	 * writing to the storage pool, so we never sync during panic.
140fa9e4066Sahrens 	 */
141fa9e4066Sahrens 	if (panicstr)
142fa9e4066Sahrens 		return (0);
143fa9e4066Sahrens 
144fa9e4066Sahrens 	/*
145fa9e4066Sahrens 	 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
146fa9e4066Sahrens 	 * to sync metadata, which they would otherwise cache indefinitely.
147fa9e4066Sahrens 	 * Semantically, the only requirement is that the sync be initiated.
148fa9e4066Sahrens 	 * The DMU syncs out txgs frequently, so there's nothing to do.
149fa9e4066Sahrens 	 */
150fa9e4066Sahrens 	if (flag & SYNC_ATTR)
151fa9e4066Sahrens 		return (0);
152fa9e4066Sahrens 
153fa9e4066Sahrens 	if (vfsp != NULL) {
154fa9e4066Sahrens 		/*
155fa9e4066Sahrens 		 * Sync a specific filesystem.
156fa9e4066Sahrens 		 */
157fa9e4066Sahrens 		zfsvfs_t *zfsvfs = vfsp->vfs_data;
15854d692b7SGeorge Wilson 		dsl_pool_t *dp;
159fa9e4066Sahrens 
160fa9e4066Sahrens 		ZFS_ENTER(zfsvfs);
16154d692b7SGeorge Wilson 		dp = dmu_objset_pool(zfsvfs->z_os);
16254d692b7SGeorge Wilson 
16354d692b7SGeorge Wilson 		/*
16454d692b7SGeorge Wilson 		 * If the system is shutting down, then skip any
16554d692b7SGeorge Wilson 		 * filesystems which may exist on a suspended pool.
16654d692b7SGeorge Wilson 		 */
16754d692b7SGeorge Wilson 		if (sys_shutdown && spa_suspended(dp->dp_spa)) {
16854d692b7SGeorge Wilson 			ZFS_EXIT(zfsvfs);
16954d692b7SGeorge Wilson 			return (0);
17054d692b7SGeorge Wilson 		}
17154d692b7SGeorge Wilson 
172fa9e4066Sahrens 		if (zfsvfs->z_log != NULL)
1735002558fSNeil Perrin 			zil_commit(zfsvfs->z_log, 0);
17455da60b9SMark J Musante 
175fa9e4066Sahrens 		ZFS_EXIT(zfsvfs);
176fa9e4066Sahrens 	} else {
177fa9e4066Sahrens 		/*
178fa9e4066Sahrens 		 * Sync all ZFS filesystems.  This is what happens when you
179bbf21555SRichard Lowe 		 * run sync(8).  Unlike other filesystems, ZFS honors the
180fa9e4066Sahrens 		 * request by waiting for all pools to commit all dirty data.
181fa9e4066Sahrens 		 */
182fa9e4066Sahrens 		spa_sync_allpools();
183fa9e4066Sahrens 	}
184fa9e4066Sahrens 
185fa9e4066Sahrens 	return (0);
186fa9e4066Sahrens }
187fa9e4066Sahrens 
188ea8dc4b6Seschrock static int
zfs_create_unique_device(dev_t * dev)189ea8dc4b6Seschrock zfs_create_unique_device(dev_t *dev)
190ea8dc4b6Seschrock {
191ea8dc4b6Seschrock 	major_t new_major;
192ea8dc4b6Seschrock 
193ea8dc4b6Seschrock 	do {
194ea8dc4b6Seschrock 		ASSERT3U(zfs_minor, <=, MAXMIN32);
195ea8dc4b6Seschrock 		minor_t start = zfs_minor;
196ea8dc4b6Seschrock 		do {
197ea8dc4b6Seschrock 			mutex_enter(&zfs_dev_mtx);
198ea8dc4b6Seschrock 			if (zfs_minor >= MAXMIN32) {
199ea8dc4b6Seschrock 				/*
200ea8dc4b6Seschrock 				 * If we're still using the real major
201ea8dc4b6Seschrock 				 * keep out of /dev/zfs and /dev/zvol minor
202ea8dc4b6Seschrock 				 * number space.  If we're using a getudev()'ed
203ea8dc4b6Seschrock 				 * major number, we can use all of its minors.
204ea8dc4b6Seschrock 				 */
205ea8dc4b6Seschrock 				if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
206ea8dc4b6Seschrock 					zfs_minor = ZFS_MIN_MINOR;
207ea8dc4b6Seschrock 				else
208ea8dc4b6Seschrock 					zfs_minor = 0;
209ea8dc4b6Seschrock 			} else {
210ea8dc4b6Seschrock 				zfs_minor++;
211ea8dc4b6Seschrock 			}
212ea8dc4b6Seschrock 			*dev = makedevice(zfs_major, zfs_minor);
213ea8dc4b6Seschrock 			mutex_exit(&zfs_dev_mtx);
214ea8dc4b6Seschrock 		} while (vfs_devismounted(*dev) && zfs_minor != start);
215ea8dc4b6Seschrock 		if (zfs_minor == start) {
216ea8dc4b6Seschrock 			/*
217ea8dc4b6Seschrock 			 * We are using all ~262,000 minor numbers for the
218ea8dc4b6Seschrock 			 * current major number.  Create a new major number.
219ea8dc4b6Seschrock 			 */
220ea8dc4b6Seschrock 			if ((new_major = getudev()) == (major_t)-1) {
221ea8dc4b6Seschrock 				cmn_err(CE_WARN,
222ea8dc4b6Seschrock 				    "zfs_mount: Can't get unique major "
223ea8dc4b6Seschrock 				    "device number.");
224ea8dc4b6Seschrock 				return (-1);
225ea8dc4b6Seschrock 			}
226ea8dc4b6Seschrock 			mutex_enter(&zfs_dev_mtx);
227ea8dc4b6Seschrock 			zfs_major = new_major;
228ea8dc4b6Seschrock 			zfs_minor = 0;
229ea8dc4b6Seschrock 
230ea8dc4b6Seschrock 			mutex_exit(&zfs_dev_mtx);
231ea8dc4b6Seschrock 		} else {
232ea8dc4b6Seschrock 			break;
233ea8dc4b6Seschrock 		}
234ea8dc4b6Seschrock 		/* CONSTANTCONDITION */
235ea8dc4b6Seschrock 	} while (1);
236ea8dc4b6Seschrock 
237ea8dc4b6Seschrock 	return (0);
238ea8dc4b6Seschrock }
239ea8dc4b6Seschrock 
240fa9e4066Sahrens static void
atime_changed_cb(void * arg,uint64_t newval)241fa9e4066Sahrens atime_changed_cb(void *arg, uint64_t newval)
242fa9e4066Sahrens {
243fa9e4066Sahrens 	zfsvfs_t *zfsvfs = arg;
244fa9e4066Sahrens 
245fa9e4066Sahrens 	if (newval == TRUE) {
246fa9e4066Sahrens 		zfsvfs->z_atime = TRUE;
247fa9e4066Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
248fa9e4066Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
249fa9e4066Sahrens 	} else {
250fa9e4066Sahrens 		zfsvfs->z_atime = FALSE;
251fa9e4066Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
252fa9e4066Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
253fa9e4066Sahrens 	}
254fa9e4066Sahrens }
255fa9e4066Sahrens 
2567b55fa8eSck static void
xattr_changed_cb(void * arg,uint64_t newval)2577b55fa8eSck xattr_changed_cb(void *arg, uint64_t newval)
2587b55fa8eSck {
2597b55fa8eSck 	zfsvfs_t *zfsvfs = arg;
2607b55fa8eSck 
2617b55fa8eSck 	if (newval == TRUE) {
2627b55fa8eSck 		/* XXX locking on vfs_flag? */
2637b55fa8eSck 		zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
2647b55fa8eSck 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
2657b55fa8eSck 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
2667b55fa8eSck 	} else {
2677b55fa8eSck 		/* XXX locking on vfs_flag? */
2687b55fa8eSck 		zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
2697b55fa8eSck 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
2707b55fa8eSck 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
2717b55fa8eSck 	}
2727b55fa8eSck }
2737b55fa8eSck 
274fa9e4066Sahrens static void
blksz_changed_cb(void * arg,uint64_t newval)275fa9e4066Sahrens blksz_changed_cb(void *arg, uint64_t newval)
276fa9e4066Sahrens {
277fa9e4066Sahrens 	zfsvfs_t *zfsvfs = arg;
278b5152584SMatthew Ahrens 	ASSERT3U(newval, <=, spa_maxblocksize(dmu_objset_spa(zfsvfs->z_os)));
279b5152584SMatthew Ahrens 	ASSERT3U(newval, >=, SPA_MINBLOCKSIZE);
280b5152584SMatthew Ahrens 	ASSERT(ISP2(newval));
281fa9e4066Sahrens 
282fa9e4066Sahrens 	zfsvfs->z_max_blksz = newval;
283fa9e4066Sahrens 	zfsvfs->z_vfs->vfs_bsize = newval;
284fa9e4066Sahrens }
285fa9e4066Sahrens 
286fa9e4066Sahrens static void
readonly_changed_cb(void * arg,uint64_t newval)287fa9e4066Sahrens readonly_changed_cb(void *arg, uint64_t newval)
288fa9e4066Sahrens {
289fa9e4066Sahrens 	zfsvfs_t *zfsvfs = arg;
290fa9e4066Sahrens 
291fa9e4066Sahrens 	if (newval) {
292fa9e4066Sahrens 		/* XXX locking on vfs_flag? */
293fa9e4066Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
294fa9e4066Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
295fa9e4066Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
296fa9e4066Sahrens 	} else {
297fa9e4066Sahrens 		/* XXX locking on vfs_flag? */
298fa9e4066Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
299fa9e4066Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
300fa9e4066Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
301fa9e4066Sahrens 	}
302fa9e4066Sahrens }
303fa9e4066Sahrens 
304fa9e4066Sahrens static void
devices_changed_cb(void * arg,uint64_t newval)305fa9e4066Sahrens devices_changed_cb(void *arg, uint64_t newval)
306fa9e4066Sahrens {
307fa9e4066Sahrens 	zfsvfs_t *zfsvfs = arg;
308fa9e4066Sahrens 
309fa9e4066Sahrens 	if (newval == FALSE) {
310fa9e4066Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
311fa9e4066Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
312fa9e4066Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
313fa9e4066Sahrens 	} else {
314fa9e4066Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
315fa9e4066Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
316fa9e4066Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
317fa9e4066Sahrens 	}
318fa9e4066Sahrens }
319fa9e4066Sahrens 
320fa9e4066Sahrens static void
setuid_changed_cb(void * arg,uint64_t newval)321fa9e4066Sahrens setuid_changed_cb(void *arg, uint64_t newval)
322fa9e4066Sahrens {
323fa9e4066Sahrens 	zfsvfs_t *zfsvfs = arg;
324fa9e4066Sahrens 
325fa9e4066Sahrens 	if (newval == FALSE) {
326fa9e4066Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
327fa9e4066Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
328fa9e4066Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
329fa9e4066Sahrens 	} else {
330fa9e4066Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
331fa9e4066Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
332fa9e4066Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
333fa9e4066Sahrens 	}
334fa9e4066Sahrens }
335fa9e4066Sahrens 
336fa9e4066Sahrens static void
exec_changed_cb(void * arg,uint64_t newval)337fa9e4066Sahrens exec_changed_cb(void *arg, uint64_t newval)
338fa9e4066Sahrens {
339fa9e4066Sahrens 	zfsvfs_t *zfsvfs = arg;
340fa9e4066Sahrens 
341fa9e4066Sahrens 	if (newval == FALSE) {
342fa9e4066Sahrens 		zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
343fa9e4066Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
344fa9e4066Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
345fa9e4066Sahrens 	} else {
346fa9e4066Sahrens 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
347fa9e4066Sahrens 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
348fa9e4066Sahrens 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
349fa9e4066Sahrens 	}
350fa9e4066Sahrens }
351fa9e4066Sahrens 
352da6c28aaSamw /*
353da6c28aaSamw  * The nbmand mount option can be changed at mount time.
354da6c28aaSamw  * We can't allow it to be toggled on live file systems or incorrect
355da6c28aaSamw  * behavior may be seen from cifs clients
356da6c28aaSamw  *
357da6c28aaSamw  * This property isn't registered via dsl_prop_register(), but this callback
358da6c28aaSamw  * will be called when a file system is first mounted
359da6c28aaSamw  */
360da6c28aaSamw static void
nbmand_changed_cb(void * arg,uint64_t newval)361da6c28aaSamw nbmand_changed_cb(void *arg, uint64_t newval)
362da6c28aaSamw {
363da6c28aaSamw 	zfsvfs_t *zfsvfs = arg;
364da6c28aaSamw 	if (newval == FALSE) {
365da6c28aaSamw 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
366da6c28aaSamw 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
367da6c28aaSamw 	} else {
368da6c28aaSamw 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
369da6c28aaSamw 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
370da6c28aaSamw 	}
371da6c28aaSamw }
372da6c28aaSamw 
373fa9e4066Sahrens static void
snapdir_changed_cb(void * arg,uint64_t newval)374fa9e4066Sahrens snapdir_changed_cb(void *arg, uint64_t newval)
375fa9e4066Sahrens {
376fa9e4066Sahrens 	zfsvfs_t *zfsvfs = arg;
377fa9e4066Sahrens 
378fa9e4066Sahrens 	zfsvfs->z_show_ctldir = newval;
379fa9e4066Sahrens }
380fa9e4066Sahrens 
381da6c28aaSamw static void
vscan_changed_cb(void * arg,uint64_t newval)382da6c28aaSamw vscan_changed_cb(void *arg, uint64_t newval)
383da6c28aaSamw {
384da6c28aaSamw 	zfsvfs_t *zfsvfs = arg;
385da6c28aaSamw 
386da6c28aaSamw 	zfsvfs->z_vscan = newval;
387da6c28aaSamw }
388da6c28aaSamw 
389a3c49ce1SAlbert Lee static void
acl_mode_changed_cb(void * arg,uint64_t newval)390a3c49ce1SAlbert Lee acl_mode_changed_cb(void *arg, uint64_t newval)
391a3c49ce1SAlbert Lee {
392a3c49ce1SAlbert Lee 	zfsvfs_t *zfsvfs = arg;
393a3c49ce1SAlbert Lee 
394a3c49ce1SAlbert Lee 	zfsvfs->z_acl_mode = newval;
395a3c49ce1SAlbert Lee }
396a3c49ce1SAlbert Lee 
397fa9e4066Sahrens static void
acl_inherit_changed_cb(void * arg,uint64_t newval)398fa9e4066Sahrens acl_inherit_changed_cb(void *arg, uint64_t newval)
399fa9e4066Sahrens {
400fa9e4066Sahrens 	zfsvfs_t *zfsvfs = arg;
401fa9e4066Sahrens 
402fa9e4066Sahrens 	zfsvfs->z_acl_inherit = newval;
403fa9e4066Sahrens }
404fa9e4066Sahrens 
405*70561057SGordon Ross static void
acl_implicit_changed_cb(void * arg,uint64_t newval)406*70561057SGordon Ross acl_implicit_changed_cb(void *arg, uint64_t newval)
407*70561057SGordon Ross {
408*70561057SGordon Ross 	zfsvfs_t *zfsvfs = arg;
409*70561057SGordon Ross 
410*70561057SGordon Ross 	zfsvfs->z_acl_implicit = (boolean_t)newval;
411*70561057SGordon Ross }
412*70561057SGordon Ross 
413ea8dc4b6Seschrock static int
zfs_register_callbacks(vfs_t * vfsp)414ea8dc4b6Seschrock zfs_register_callbacks(vfs_t *vfsp)
415ea8dc4b6Seschrock {
416ea8dc4b6Seschrock 	struct dsl_dataset *ds = NULL;
417ea8dc4b6Seschrock 	objset_t *os = NULL;
418ea8dc4b6Seschrock 	zfsvfs_t *zfsvfs = NULL;
419da6c28aaSamw 	uint64_t nbmand;
420d5285caeSGeorge Wilson 	boolean_t readonly = B_FALSE;
421d5285caeSGeorge Wilson 	boolean_t do_readonly = B_FALSE;
422d5285caeSGeorge Wilson 	boolean_t setuid = B_FALSE;
423d5285caeSGeorge Wilson 	boolean_t do_setuid = B_FALSE;
424d5285caeSGeorge Wilson 	boolean_t exec = B_FALSE;
425d5285caeSGeorge Wilson 	boolean_t do_exec = B_FALSE;
426d5285caeSGeorge Wilson 	boolean_t devices = B_FALSE;
427d5285caeSGeorge Wilson 	boolean_t do_devices = B_FALSE;
428d5285caeSGeorge Wilson 	boolean_t xattr = B_FALSE;
429d5285caeSGeorge Wilson 	boolean_t do_xattr = B_FALSE;
430d5285caeSGeorge Wilson 	boolean_t atime = B_FALSE;
431d5285caeSGeorge Wilson 	boolean_t do_atime = B_FALSE;
432ea8dc4b6Seschrock 	int error = 0;
433ea8dc4b6Seschrock 
434ea8dc4b6Seschrock 	ASSERT(vfsp);
435ea8dc4b6Seschrock 	zfsvfs = vfsp->vfs_data;
436ea8dc4b6Seschrock 	ASSERT(zfsvfs);
437ea8dc4b6Seschrock 	os = zfsvfs->z_os;
438fa9e4066Sahrens 
439fa9e4066Sahrens 	/*
440ea8dc4b6Seschrock 	 * The act of registering our callbacks will destroy any mount
441ea8dc4b6Seschrock 	 * options we may have.  In order to enable temporary overrides
4427b55fa8eSck 	 * of mount options, we stash away the current values and
443ea8dc4b6Seschrock 	 * restore them after we register the callbacks.
444fa9e4066Sahrens 	 */
445f9af39baSGeorge Wilson 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL) ||
446f9af39baSGeorge Wilson 	    !spa_writeable(dmu_objset_spa(os))) {
447ea8dc4b6Seschrock 		readonly = B_TRUE;
448ea8dc4b6Seschrock 		do_readonly = B_TRUE;
449ea8dc4b6Seschrock 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
450ea8dc4b6Seschrock 		readonly = B_FALSE;
451ea8dc4b6Seschrock 		do_readonly = B_TRUE;
452ea8dc4b6Seschrock 	}
453ea8dc4b6Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
454ea8dc4b6Seschrock 		devices = B_FALSE;
455ea8dc4b6Seschrock 		setuid = B_FALSE;
456ea8dc4b6Seschrock 		do_devices = B_TRUE;
457ea8dc4b6Seschrock 		do_setuid = B_TRUE;
458ea8dc4b6Seschrock 	} else {
459ea8dc4b6Seschrock 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
460ea8dc4b6Seschrock 			devices = B_FALSE;
461ea8dc4b6Seschrock 			do_devices = B_TRUE;
462b1b8ab34Slling 		} else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
463ea8dc4b6Seschrock 			devices = B_TRUE;
464ea8dc4b6Seschrock 			do_devices = B_TRUE;
465fa9e4066Sahrens 		}
466fa9e4066Sahrens 
467ea8dc4b6Seschrock 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
468ea8dc4b6Seschrock 			setuid = B_FALSE;
469ea8dc4b6Seschrock 			do_setuid = B_TRUE;
470ea8dc4b6Seschrock 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
471ea8dc4b6Seschrock 			setuid = B_TRUE;
472ea8dc4b6Seschrock 			do_setuid = B_TRUE;
473fa9e4066Sahrens 		}
474ea8dc4b6Seschrock 	}
475ea8dc4b6Seschrock 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
476ea8dc4b6Seschrock 		exec = B_FALSE;
477ea8dc4b6Seschrock 		do_exec = B_TRUE;
478ea8dc4b6Seschrock 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
479ea8dc4b6Seschrock 		exec = B_TRUE;
480ea8dc4b6Seschrock 		do_exec = B_TRUE;
481fa9e4066Sahrens 	}
4827b55fa8eSck 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
4837b55fa8eSck 		xattr = B_FALSE;
4847b55fa8eSck 		do_xattr = B_TRUE;
4857b55fa8eSck 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
4867b55fa8eSck 		xattr = B_TRUE;
4877b55fa8eSck 		do_xattr = B_TRUE;
4887b55fa8eSck 	}
489b510d378Slling 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
490b510d378Slling 		atime = B_FALSE;
491b510d378Slling 		do_atime = B_TRUE;
492b510d378Slling 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
493b510d378Slling 		atime = B_TRUE;
494b510d378Slling 		do_atime = B_TRUE;
495b510d378Slling 	}
496fa9e4066Sahrens 
497da6c28aaSamw 	/*
498da6c28aaSamw 	 * nbmand is a special property.  It can only be changed at
499da6c28aaSamw 	 * mount time.
500da6c28aaSamw 	 *
501da6c28aaSamw 	 * This is weird, but it is documented to only be changeable
502da6c28aaSamw 	 * at mount time.
503da6c28aaSamw 	 */
504da6c28aaSamw 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
505da6c28aaSamw 		nbmand = B_FALSE;
506da6c28aaSamw 	} else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
507da6c28aaSamw 		nbmand = B_TRUE;
508da6c28aaSamw 	} else {
5099adfa60dSMatthew Ahrens 		char osname[ZFS_MAX_DATASET_NAME_LEN];
510da6c28aaSamw 
511da6c28aaSamw 		dmu_objset_name(os, osname);
512da6c28aaSamw 		if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
513bb0ade09Sahrens 		    NULL)) {
514bb0ade09Sahrens 			return (error);
515bb0ade09Sahrens 		}
516da6c28aaSamw 	}
517da6c28aaSamw 
518fa9e4066Sahrens 	/*
519ea8dc4b6Seschrock 	 * Register property callbacks.
520ea8dc4b6Seschrock 	 *
521ea8dc4b6Seschrock 	 * It would probably be fine to just check for i/o error from
522ea8dc4b6Seschrock 	 * the first prop_register(), but I guess I like to go
523ea8dc4b6Seschrock 	 * overboard...
524fa9e4066Sahrens 	 */
525ea8dc4b6Seschrock 	ds = dmu_objset_ds(os);
5263b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5273b2aab18SMatthew Ahrens 	error = dsl_prop_register(ds,
5283b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_ATIME), atime_changed_cb, zfsvfs);
5297b55fa8eSck 	error = error ? error : dsl_prop_register(ds,
5303b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_XATTR), xattr_changed_cb, zfsvfs);
531ea8dc4b6Seschrock 	error = error ? error : dsl_prop_register(ds,
5323b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_RECORDSIZE), blksz_changed_cb, zfsvfs);
533ea8dc4b6Seschrock 	error = error ? error : dsl_prop_register(ds,
5343b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_READONLY), readonly_changed_cb, zfsvfs);
535ea8dc4b6Seschrock 	error = error ? error : dsl_prop_register(ds,
5363b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_DEVICES), devices_changed_cb, zfsvfs);
537ea8dc4b6Seschrock 	error = error ? error : dsl_prop_register(ds,
5383b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_SETUID), setuid_changed_cb, zfsvfs);
539ea8dc4b6Seschrock 	error = error ? error : dsl_prop_register(ds,
5403b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_EXEC), exec_changed_cb, zfsvfs);
541ea8dc4b6Seschrock 	error = error ? error : dsl_prop_register(ds,
5423b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_SNAPDIR), snapdir_changed_cb, zfsvfs);
543a3c49ce1SAlbert Lee 	error = error ? error : dsl_prop_register(ds,
5443b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_ACLMODE), acl_mode_changed_cb, zfsvfs);
545ea8dc4b6Seschrock 	error = error ? error : dsl_prop_register(ds,
5463b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_ACLINHERIT), acl_inherit_changed_cb,
5473b2aab18SMatthew Ahrens 	    zfsvfs);
548*70561057SGordon Ross 	error = error ? error : dsl_prop_register(ds,
549*70561057SGordon Ross 	    zfs_prop_to_name(ZFS_PROP_ACLIMPLICIT),
550*70561057SGordon Ross 	    acl_implicit_changed_cb, zfsvfs);
551da6c28aaSamw 	error = error ? error : dsl_prop_register(ds,
5523b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_VSCAN), vscan_changed_cb, zfsvfs);
5533b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
554ea8dc4b6Seschrock 	if (error)
555ea8dc4b6Seschrock 		goto unregister;
556fa9e4066Sahrens 
557ea8dc4b6Seschrock 	/*
558ea8dc4b6Seschrock 	 * Invoke our callbacks to restore temporary mount options.
559ea8dc4b6Seschrock 	 */
560ea8dc4b6Seschrock 	if (do_readonly)
561ea8dc4b6Seschrock 		readonly_changed_cb(zfsvfs, readonly);
562ea8dc4b6Seschrock 	if (do_setuid)
563ea8dc4b6Seschrock 		setuid_changed_cb(zfsvfs, setuid);
564ea8dc4b6Seschrock 	if (do_exec)
565ea8dc4b6Seschrock 		exec_changed_cb(zfsvfs, exec);
566ea8dc4b6Seschrock 	if (do_devices)
567ea8dc4b6Seschrock 		devices_changed_cb(zfsvfs, devices);
5687b55fa8eSck 	if (do_xattr)
5697b55fa8eSck 		xattr_changed_cb(zfsvfs, xattr);
570b510d378Slling 	if (do_atime)
571b510d378Slling 		atime_changed_cb(zfsvfs, atime);
572fa9e4066Sahrens 
573da6c28aaSamw 	nbmand_changed_cb(zfsvfs, nbmand);
574da6c28aaSamw 
575ea8dc4b6Seschrock 	return (0);
576fa9e4066Sahrens 
577ea8dc4b6Seschrock unregister:
57803bad06fSJustin Gibbs 	dsl_prop_unregister_all(ds, zfsvfs);
579ea8dc4b6Seschrock 	return (error);
580ea8dc4b6Seschrock }
581ea8dc4b6Seschrock 
5829966ca11SMatthew Ahrens static int
zfs_space_delta_cb(dmu_object_type_t bonustype,void * data,uint64_t * userp,uint64_t * groupp,uint64_t * projectp)5830a586ceaSMark Shellenbaum zfs_space_delta_cb(dmu_object_type_t bonustype, void *data,
584f67950b2SNasf-Fan     uint64_t *userp, uint64_t *groupp, uint64_t *projectp)
58514843421SMatthew Ahrens {
586f67950b2SNasf-Fan 	sa_hdr_phys_t sa;
587f67950b2SNasf-Fan 	sa_hdr_phys_t *sap = data;
588f67950b2SNasf-Fan 	uint64_t flags;
589f67950b2SNasf-Fan 	int hdrsize;
590f67950b2SNasf-Fan 	boolean_t swap = B_FALSE;
591f67950b2SNasf-Fan 
59206e0070dSMark Shellenbaum 	/*
59306e0070dSMark Shellenbaum 	 * Is it a valid type of object to track?
59406e0070dSMark Shellenbaum 	 */
5950a586ceaSMark Shellenbaum 	if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
596be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
59714843421SMatthew Ahrens 
59806e0070dSMark Shellenbaum 	/*
59906e0070dSMark Shellenbaum 	 * If we have a NULL data pointer
60006e0070dSMark Shellenbaum 	 * then assume the id's aren't changing and
60106e0070dSMark Shellenbaum 	 * return EEXIST to the dmu to let it know to
60206e0070dSMark Shellenbaum 	 * use the same ids
60306e0070dSMark Shellenbaum 	 */
60406e0070dSMark Shellenbaum 	if (data == NULL)
605be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
60606e0070dSMark Shellenbaum 
6070a586ceaSMark Shellenbaum 	if (bonustype == DMU_OT_ZNODE) {
608e828a46dSMatthew Ahrens 		znode_phys_t *znp = data;
6090a586ceaSMark Shellenbaum 		*userp = znp->zp_uid;
6100a586ceaSMark Shellenbaum 		*groupp = znp->zp_gid;
611f67950b2SNasf-Fan 		*projectp = ZFS_DEFAULT_PROJID;
612f67950b2SNasf-Fan 		return (0);
613f67950b2SNasf-Fan 	}
614f67950b2SNasf-Fan 
615f67950b2SNasf-Fan 	if (sap->sa_magic == 0) {
616f67950b2SNasf-Fan 		/*
617f67950b2SNasf-Fan 		 * This should only happen for newly created files
618f67950b2SNasf-Fan 		 * that haven't had the znode data filled in yet.
619f67950b2SNasf-Fan 		 */
620f67950b2SNasf-Fan 		*userp = 0;
621f67950b2SNasf-Fan 		*groupp = 0;
622f67950b2SNasf-Fan 		*projectp = ZFS_DEFAULT_PROJID;
623f67950b2SNasf-Fan 		return (0);
624f67950b2SNasf-Fan 	}
625f67950b2SNasf-Fan 
626f67950b2SNasf-Fan 	sa = *sap;
627f67950b2SNasf-Fan 	if (sa.sa_magic == BSWAP_32(SA_MAGIC)) {
628f67950b2SNasf-Fan 		sa.sa_magic = SA_MAGIC;
629f67950b2SNasf-Fan 		sa.sa_layout_info = BSWAP_16(sa.sa_layout_info);
630f67950b2SNasf-Fan 		swap = B_TRUE;
6310a586ceaSMark Shellenbaum 	} else {
632f67950b2SNasf-Fan 		VERIFY3U(sa.sa_magic, ==, SA_MAGIC);
633f67950b2SNasf-Fan 	}
6340a586ceaSMark Shellenbaum 
635f67950b2SNasf-Fan 	hdrsize = sa_hdrsize(&sa);
636f67950b2SNasf-Fan 	VERIFY3U(hdrsize, >=, sizeof (sa_hdr_phys_t));
6370a586ceaSMark Shellenbaum 
638f67950b2SNasf-Fan 	*userp = *((uint64_t *)((uintptr_t)data + hdrsize + SA_UID_OFFSET));
639f67950b2SNasf-Fan 	*groupp = *((uint64_t *)((uintptr_t)data + hdrsize + SA_GID_OFFSET));
640f67950b2SNasf-Fan 	flags = *((uint64_t *)((uintptr_t)data + hdrsize + SA_FLAGS_OFFSET));
641f67950b2SNasf-Fan 	if (swap)
642f67950b2SNasf-Fan 		flags = BSWAP_64(flags);
643e828a46dSMatthew Ahrens 
644f67950b2SNasf-Fan 	if (flags & ZFS_PROJID)
645f67950b2SNasf-Fan 		*projectp = *((uint64_t *)((uintptr_t)data + hdrsize +
646f67950b2SNasf-Fan 		    SA_PROJID_OFFSET));
647f67950b2SNasf-Fan 	else
648f67950b2SNasf-Fan 		*projectp = ZFS_DEFAULT_PROJID;
649f67950b2SNasf-Fan 
650f67950b2SNasf-Fan 	if (swap) {
651f67950b2SNasf-Fan 		*userp = BSWAP_64(*userp);
652f67950b2SNasf-Fan 		*groupp = BSWAP_64(*groupp);
653f67950b2SNasf-Fan 		*projectp = BSWAP_64(*projectp);
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 
6654585130bSYuri Pankov 	fuid = zfs_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:
680f67950b2SNasf-Fan 	case ZFS_PROP_USEROBJUSED:
68114843421SMatthew Ahrens 		return (DMU_USERUSED_OBJECT);
68214843421SMatthew Ahrens 	case ZFS_PROP_GROUPUSED:
683f67950b2SNasf-Fan 	case ZFS_PROP_GROUPOBJUSED:
68414843421SMatthew Ahrens 		return (DMU_GROUPUSED_OBJECT);
685f67950b2SNasf-Fan 	case ZFS_PROP_PROJECTUSED:
686f67950b2SNasf-Fan 	case ZFS_PROP_PROJECTOBJUSED:
687f67950b2SNasf-Fan 		return (DMU_PROJECTUSED_OBJECT);
68814843421SMatthew Ahrens 	case ZFS_PROP_USERQUOTA:
68914843421SMatthew Ahrens 		return (zfsvfs->z_userquota_obj);
69014843421SMatthew Ahrens 	case ZFS_PROP_GROUPQUOTA:
69114843421SMatthew Ahrens 		return (zfsvfs->z_groupquota_obj);
692f67950b2SNasf-Fan 	case ZFS_PROP_USEROBJQUOTA:
693f67950b2SNasf-Fan 		return (zfsvfs->z_userobjquota_obj);
694f67950b2SNasf-Fan 	case ZFS_PROP_GROUPOBJQUOTA:
695f67950b2SNasf-Fan 		return (zfsvfs->z_groupobjquota_obj);
696f67950b2SNasf-Fan 	case ZFS_PROP_PROJECTQUOTA:
697f67950b2SNasf-Fan 		return (zfsvfs->z_projectquota_obj);
698f67950b2SNasf-Fan 	case ZFS_PROP_PROJECTOBJQUOTA:
699f67950b2SNasf-Fan 		return (zfsvfs->z_projectobjquota_obj);
700f67950b2SNasf-Fan 	default:
701f67950b2SNasf-Fan 		return (ZFS_NO_OBJECT);
70214843421SMatthew Ahrens 	}
70314843421SMatthew Ahrens }
70414843421SMatthew Ahrens 
70514843421SMatthew Ahrens int
zfs_userspace_many(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type,uint64_t * cookiep,void * vbuf,uint64_t * bufsizep)70614843421SMatthew Ahrens zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
70714843421SMatthew Ahrens     uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
70814843421SMatthew Ahrens {
70914843421SMatthew Ahrens 	int error;
71014843421SMatthew Ahrens 	zap_cursor_t zc;
71114843421SMatthew Ahrens 	zap_attribute_t za;
71214843421SMatthew Ahrens 	zfs_useracct_t *buf = vbuf;
71314843421SMatthew Ahrens 	uint64_t obj;
714f67950b2SNasf-Fan 	int offset = 0;
71514843421SMatthew Ahrens 
71614843421SMatthew Ahrens 	if (!dmu_objset_userspace_present(zfsvfs->z_os))
717be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
71814843421SMatthew Ahrens 
719f67950b2SNasf-Fan 	if ((type == ZFS_PROP_PROJECTQUOTA || type == ZFS_PROP_PROJECTUSED ||
720f67950b2SNasf-Fan 	    type == ZFS_PROP_PROJECTOBJQUOTA ||
721f67950b2SNasf-Fan 	    type == ZFS_PROP_PROJECTOBJUSED) &&
722f67950b2SNasf-Fan 	    !dmu_objset_projectquota_present(zfsvfs->z_os))
723f67950b2SNasf-Fan 		return (SET_ERROR(ENOTSUP));
724f67950b2SNasf-Fan 
725f67950b2SNasf-Fan 	if ((type == ZFS_PROP_USEROBJUSED || type == ZFS_PROP_GROUPOBJUSED ||
726f67950b2SNasf-Fan 	    type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA ||
727f67950b2SNasf-Fan 	    type == ZFS_PROP_PROJECTOBJUSED ||
728f67950b2SNasf-Fan 	    type == ZFS_PROP_PROJECTOBJQUOTA) &&
729f67950b2SNasf-Fan 	    !dmu_objset_userobjspace_present(zfsvfs->z_os))
730f67950b2SNasf-Fan 		return (SET_ERROR(ENOTSUP));
731f67950b2SNasf-Fan 
73214843421SMatthew Ahrens 	obj = zfs_userquota_prop_to_obj(zfsvfs, type);
733f67950b2SNasf-Fan 	if (obj == ZFS_NO_OBJECT) {
73414843421SMatthew Ahrens 		*bufsizep = 0;
73514843421SMatthew Ahrens 		return (0);
73614843421SMatthew Ahrens 	}
73714843421SMatthew Ahrens 
738f67950b2SNasf-Fan 	if (type == ZFS_PROP_USEROBJUSED || type == ZFS_PROP_GROUPOBJUSED ||
739f67950b2SNasf-Fan 	    type == ZFS_PROP_PROJECTOBJUSED)
740f67950b2SNasf-Fan 		offset = DMU_OBJACCT_PREFIX_LEN;
741f67950b2SNasf-Fan 
74214843421SMatthew Ahrens 	for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
74314843421SMatthew Ahrens 	    (error = zap_cursor_retrieve(&zc, &za)) == 0;
74414843421SMatthew Ahrens 	    zap_cursor_advance(&zc)) {
74514843421SMatthew Ahrens 		if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
74614843421SMatthew Ahrens 		    *bufsizep)
74714843421SMatthew Ahrens 			break;
74814843421SMatthew Ahrens 
749f67950b2SNasf-Fan 		/*
750f67950b2SNasf-Fan 		 * skip object quota (with zap name prefix DMU_OBJACCT_PREFIX)
751f67950b2SNasf-Fan 		 * when dealing with block quota and vice versa.
752f67950b2SNasf-Fan 		 */
753f67950b2SNasf-Fan 		if ((offset > 0) != (strncmp(za.za_name, DMU_OBJACCT_PREFIX,
754f67950b2SNasf-Fan 		    DMU_OBJACCT_PREFIX_LEN) == 0))
755f67950b2SNasf-Fan 			continue;
756f67950b2SNasf-Fan 
757f67950b2SNasf-Fan 		fuidstr_to_sid(zfsvfs, za.za_name + offset,
75814843421SMatthew Ahrens 		    buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
75914843421SMatthew Ahrens 
76014843421SMatthew Ahrens 		buf->zu_space = za.za_first_integer;
76114843421SMatthew Ahrens 		buf++;
76214843421SMatthew Ahrens 	}
76314843421SMatthew Ahrens 	if (error == ENOENT)
76414843421SMatthew Ahrens 		error = 0;
76514843421SMatthew Ahrens 
76614843421SMatthew Ahrens 	ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
76714843421SMatthew Ahrens 	*bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
76814843421SMatthew Ahrens 	*cookiep = zap_cursor_serialize(&zc);
76914843421SMatthew Ahrens 	zap_cursor_fini(&zc);
77014843421SMatthew Ahrens 	return (error);
77114843421SMatthew Ahrens }
77214843421SMatthew Ahrens 
77314843421SMatthew Ahrens /*
77414843421SMatthew Ahrens  * buf must be big enough (eg, 32 bytes)
77514843421SMatthew Ahrens  */
77614843421SMatthew Ahrens static int
id_to_fuidstr(zfsvfs_t * zfsvfs,const char * domain,uid_t rid,char * buf,boolean_t addok)77714843421SMatthew Ahrens id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
77814843421SMatthew Ahrens     char *buf, boolean_t addok)
77914843421SMatthew Ahrens {
78014843421SMatthew Ahrens 	uint64_t fuid;
78114843421SMatthew Ahrens 	int domainid = 0;
78214843421SMatthew Ahrens 
78314843421SMatthew Ahrens 	if (domain && domain[0]) {
78414843421SMatthew Ahrens 		domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
78514843421SMatthew Ahrens 		if (domainid == -1)
786be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
78714843421SMatthew Ahrens 	}
78814843421SMatthew Ahrens 	fuid = FUID_ENCODE(domainid, rid);
78914843421SMatthew Ahrens 	(void) sprintf(buf, "%llx", (longlong_t)fuid);
79014843421SMatthew Ahrens 	return (0);
79114843421SMatthew Ahrens }
79214843421SMatthew Ahrens 
79314843421SMatthew Ahrens int
zfs_userspace_one(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type,const char * domain,uint64_t rid,uint64_t * valp)79414843421SMatthew Ahrens zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
79514843421SMatthew Ahrens     const char *domain, uint64_t rid, uint64_t *valp)
79614843421SMatthew Ahrens {
797f67950b2SNasf-Fan 	char buf[20 + DMU_OBJACCT_PREFIX_LEN];
798f67950b2SNasf-Fan 	int offset = 0;
79914843421SMatthew Ahrens 	int err;
80014843421SMatthew Ahrens 	uint64_t obj;
80114843421SMatthew Ahrens 
80214843421SMatthew Ahrens 	*valp = 0;
80314843421SMatthew Ahrens 
80414843421SMatthew Ahrens 	if (!dmu_objset_userspace_present(zfsvfs->z_os))
805be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
80614843421SMatthew Ahrens 
807f67950b2SNasf-Fan 	if ((type == ZFS_PROP_USEROBJUSED || type == ZFS_PROP_GROUPOBJUSED ||
808f67950b2SNasf-Fan 	    type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA ||
809f67950b2SNasf-Fan 	    type == ZFS_PROP_PROJECTOBJUSED ||
810f67950b2SNasf-Fan 	    type == ZFS_PROP_PROJECTOBJQUOTA) &&
811f67950b2SNasf-Fan 	    !dmu_objset_userobjspace_present(zfsvfs->z_os))
812f67950b2SNasf-Fan 		return (SET_ERROR(ENOTSUP));
813f67950b2SNasf-Fan 
814f67950b2SNasf-Fan 	if (type == ZFS_PROP_PROJECTQUOTA || type == ZFS_PROP_PROJECTUSED ||
815f67950b2SNasf-Fan 	    type == ZFS_PROP_PROJECTOBJQUOTA ||
816f67950b2SNasf-Fan 	    type == ZFS_PROP_PROJECTOBJUSED) {
817f67950b2SNasf-Fan 		if (!dmu_objset_projectquota_present(zfsvfs->z_os))
818f67950b2SNasf-Fan 			return (SET_ERROR(ENOTSUP));
819f67950b2SNasf-Fan 		if (!zpl_is_valid_projid(rid))
820f67950b2SNasf-Fan 			return (SET_ERROR(EINVAL));
821f67950b2SNasf-Fan 	}
822f67950b2SNasf-Fan 
82314843421SMatthew Ahrens 	obj = zfs_userquota_prop_to_obj(zfsvfs, type);
824f67950b2SNasf-Fan 	if (obj == ZFS_NO_OBJECT)
82514843421SMatthew Ahrens 		return (0);
82614843421SMatthew Ahrens 
827f67950b2SNasf-Fan 	if (type == ZFS_PROP_USEROBJUSED || type == ZFS_PROP_GROUPOBJUSED ||
828f67950b2SNasf-Fan 	    type == ZFS_PROP_PROJECTOBJUSED) {
829f67950b2SNasf-Fan 		strncpy(buf, DMU_OBJACCT_PREFIX, DMU_OBJACCT_PREFIX_LEN);
830f67950b2SNasf-Fan 		offset = DMU_OBJACCT_PREFIX_LEN;
831f67950b2SNasf-Fan 	}
832f67950b2SNasf-Fan 
833f67950b2SNasf-Fan 	err = id_to_fuidstr(zfsvfs, domain, rid, buf + offset, B_FALSE);
83414843421SMatthew Ahrens 	if (err)
83514843421SMatthew Ahrens 		return (err);
83614843421SMatthew Ahrens 
83714843421SMatthew Ahrens 	err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
83814843421SMatthew Ahrens 	if (err == ENOENT)
83914843421SMatthew Ahrens 		err = 0;
84014843421SMatthew Ahrens 	return (err);
84114843421SMatthew Ahrens }
84214843421SMatthew Ahrens 
84314843421SMatthew Ahrens int
zfs_set_userquota(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type,const char * domain,uint64_t rid,uint64_t quota)84414843421SMatthew Ahrens zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
84514843421SMatthew Ahrens     const char *domain, uint64_t rid, uint64_t quota)
84614843421SMatthew Ahrens {
84714843421SMatthew Ahrens 	char buf[32];
84814843421SMatthew Ahrens 	int err;
84914843421SMatthew Ahrens 	dmu_tx_t *tx;
85014843421SMatthew Ahrens 	uint64_t *objp;
85114843421SMatthew Ahrens 	boolean_t fuid_dirtied;
85214843421SMatthew Ahrens 
85314843421SMatthew Ahrens 	if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
854be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
85514843421SMatthew Ahrens 
856f67950b2SNasf-Fan 	switch (type) {
857f67950b2SNasf-Fan 	case ZFS_PROP_USERQUOTA:
858f67950b2SNasf-Fan 		objp = &zfsvfs->z_userquota_obj;
859f67950b2SNasf-Fan 		break;
860f67950b2SNasf-Fan 	case ZFS_PROP_GROUPQUOTA:
861f67950b2SNasf-Fan 		objp = &zfsvfs->z_groupquota_obj;
862f67950b2SNasf-Fan 		break;
863f67950b2SNasf-Fan 	case ZFS_PROP_USEROBJQUOTA:
864f67950b2SNasf-Fan 		objp = &zfsvfs->z_userobjquota_obj;
865f67950b2SNasf-Fan 		break;
866f67950b2SNasf-Fan 	case ZFS_PROP_GROUPOBJQUOTA:
867f67950b2SNasf-Fan 		objp = &zfsvfs->z_groupobjquota_obj;
868f67950b2SNasf-Fan 		break;
869f67950b2SNasf-Fan 	case ZFS_PROP_PROJECTQUOTA:
870f67950b2SNasf-Fan 		if (!dmu_objset_projectquota_enabled(zfsvfs->z_os))
871f67950b2SNasf-Fan 			return (SET_ERROR(ENOTSUP));
872f67950b2SNasf-Fan 		if (!zpl_is_valid_projid(rid))
873f67950b2SNasf-Fan 			return (SET_ERROR(EINVAL));
874f67950b2SNasf-Fan 
875f67950b2SNasf-Fan 		objp = &zfsvfs->z_projectquota_obj;
876f67950b2SNasf-Fan 		break;
877f67950b2SNasf-Fan 	case ZFS_PROP_PROJECTOBJQUOTA:
878f67950b2SNasf-Fan 		if (!dmu_objset_projectquota_enabled(zfsvfs->z_os))
879f67950b2SNasf-Fan 			return (SET_ERROR(ENOTSUP));
880f67950b2SNasf-Fan 		if (!zpl_is_valid_projid(rid))
881f67950b2SNasf-Fan 			return (SET_ERROR(EINVAL));
882f67950b2SNasf-Fan 
883f67950b2SNasf-Fan 		objp = &zfsvfs->z_projectobjquota_obj;
884f67950b2SNasf-Fan 		break;
885f67950b2SNasf-Fan 	default:
886f67950b2SNasf-Fan 		return (SET_ERROR(EINVAL));
887f67950b2SNasf-Fan 	}
88814843421SMatthew Ahrens 
88914843421SMatthew Ahrens 	err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE);
89014843421SMatthew Ahrens 	if (err)
89114843421SMatthew Ahrens 		return (err);
89214843421SMatthew Ahrens 	fuid_dirtied = zfsvfs->z_fuid_dirty;
89314843421SMatthew Ahrens 
89414843421SMatthew Ahrens 	tx = dmu_tx_create(zfsvfs->z_os);
89514843421SMatthew Ahrens 	dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
89614843421SMatthew Ahrens 	if (*objp == 0) {
89714843421SMatthew Ahrens 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
89814843421SMatthew Ahrens 		    zfs_userquota_prop_prefixes[type]);
89914843421SMatthew Ahrens 	}
90014843421SMatthew Ahrens 	if (fuid_dirtied)
90114843421SMatthew Ahrens 		zfs_fuid_txhold(zfsvfs, tx);
90214843421SMatthew Ahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
90314843421SMatthew Ahrens 	if (err) {
90414843421SMatthew Ahrens 		dmu_tx_abort(tx);
90514843421SMatthew Ahrens 		return (err);
90614843421SMatthew Ahrens 	}
90714843421SMatthew Ahrens 
90814843421SMatthew Ahrens 	mutex_enter(&zfsvfs->z_lock);
90914843421SMatthew Ahrens 	if (*objp == 0) {
91014843421SMatthew Ahrens 		*objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
91114843421SMatthew Ahrens 		    DMU_OT_NONE, 0, tx);
91214843421SMatthew Ahrens 		VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
91314843421SMatthew Ahrens 		    zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
91414843421SMatthew Ahrens 	}
91514843421SMatthew Ahrens 	mutex_exit(&zfsvfs->z_lock);
91614843421SMatthew Ahrens 
91714843421SMatthew Ahrens 	if (quota == 0) {
91814843421SMatthew Ahrens 		err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
91914843421SMatthew Ahrens 		if (err == ENOENT)
92014843421SMatthew Ahrens 			err = 0;
92114843421SMatthew Ahrens 	} else {
92214843421SMatthew Ahrens 		err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, &quota, tx);
92314843421SMatthew Ahrens 	}
92414843421SMatthew Ahrens 	ASSERT(err == 0);
92514843421SMatthew Ahrens 	if (fuid_dirtied)
92614843421SMatthew Ahrens 		zfs_fuid_sync(zfsvfs, tx);
92714843421SMatthew Ahrens 	dmu_tx_commit(tx);
92814843421SMatthew Ahrens 	return (err);
92914843421SMatthew Ahrens }
93014843421SMatthew Ahrens 
93114843421SMatthew Ahrens boolean_t
zfs_id_overobjquota(zfsvfs_t * zfsvfs,uint64_t usedobj,uint64_t id)932f67950b2SNasf-Fan zfs_id_overobjquota(zfsvfs_t *zfsvfs, uint64_t usedobj, uint64_t id)
93314843421SMatthew Ahrens {
934f67950b2SNasf-Fan 	char buf[20 + DMU_OBJACCT_PREFIX_LEN];
935f67950b2SNasf-Fan 	uint64_t used, quota, quotaobj;
93614843421SMatthew Ahrens 	int err;
93714843421SMatthew Ahrens 
938f67950b2SNasf-Fan 	if (!dmu_objset_userobjspace_present(zfsvfs->z_os)) {
939a39df08cSArkadiusz Bubała 		if (dmu_objset_userobjspace_upgradable(zfsvfs->z_os)) {
940a39df08cSArkadiusz Bubała 			dsl_pool_config_enter(
941a39df08cSArkadiusz Bubała 			    dmu_objset_pool(zfsvfs->z_os), FTAG);
942f67950b2SNasf-Fan 			dmu_objset_id_quota_upgrade(zfsvfs->z_os);
943a39df08cSArkadiusz Bubała 			dsl_pool_config_exit(
944a39df08cSArkadiusz Bubała 			    dmu_objset_pool(zfsvfs->z_os), FTAG);
945a39df08cSArkadiusz Bubała 		}
946f67950b2SNasf-Fan 		return (B_FALSE);
947f67950b2SNasf-Fan 	}
94814843421SMatthew Ahrens 
949f67950b2SNasf-Fan 	if (usedobj == DMU_PROJECTUSED_OBJECT) {
950f67950b2SNasf-Fan 		if (!dmu_objset_projectquota_present(zfsvfs->z_os)) {
951f67950b2SNasf-Fan 			if (dmu_objset_projectquota_upgradable(zfsvfs->z_os)) {
952f67950b2SNasf-Fan 				dsl_pool_config_enter(
953f67950b2SNasf-Fan 				    dmu_objset_pool(zfsvfs->z_os), FTAG);
954f67950b2SNasf-Fan 				dmu_objset_id_quota_upgrade(zfsvfs->z_os);
955f67950b2SNasf-Fan 				dsl_pool_config_exit(
956f67950b2SNasf-Fan 				    dmu_objset_pool(zfsvfs->z_os), FTAG);
957f67950b2SNasf-Fan 			}
958f67950b2SNasf-Fan 			return (B_FALSE);
959f67950b2SNasf-Fan 		}
960f67950b2SNasf-Fan 		quotaobj = zfsvfs->z_projectobjquota_obj;
961f67950b2SNasf-Fan 	} else if (usedobj == DMU_USERUSED_OBJECT) {
962f67950b2SNasf-Fan 		quotaobj = zfsvfs->z_userobjquota_obj;
963f67950b2SNasf-Fan 	} else if (usedobj == DMU_GROUPUSED_OBJECT) {
964f67950b2SNasf-Fan 		quotaobj = zfsvfs->z_groupobjquota_obj;
965f67950b2SNasf-Fan 	} else {
966f67950b2SNasf-Fan 		return (B_FALSE);
967f67950b2SNasf-Fan 	}
96814843421SMatthew Ahrens 	if (quotaobj == 0 || zfsvfs->z_replay)
96914843421SMatthew Ahrens 		return (B_FALSE);
97014843421SMatthew Ahrens 
971f67950b2SNasf-Fan 	(void) sprintf(buf, "%llx", (longlong_t)id);
97214843421SMatthew Ahrens 	err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
97314843421SMatthew Ahrens 	if (err != 0)
97414843421SMatthew Ahrens 		return (B_FALSE);
97514843421SMatthew Ahrens 
976f67950b2SNasf-Fan 	(void) sprintf(buf, DMU_OBJACCT_PREFIX "%llx", (longlong_t)id);
97714843421SMatthew Ahrens 	err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
97814843421SMatthew Ahrens 	if (err != 0)
97914843421SMatthew Ahrens 		return (B_FALSE);
98014843421SMatthew Ahrens 	return (used >= quota);
98114843421SMatthew Ahrens }
98214843421SMatthew Ahrens 
9830a586ceaSMark Shellenbaum boolean_t
zfs_id_overblockquota(zfsvfs_t * zfsvfs,uint64_t usedobj,uint64_t id)984f67950b2SNasf-Fan zfs_id_overblockquota(zfsvfs_t *zfsvfs, uint64_t usedobj, uint64_t id)
9850a586ceaSMark Shellenbaum {
986f67950b2SNasf-Fan 	char buf[20];
987f67950b2SNasf-Fan 	uint64_t used, quota, quotaobj;
988f67950b2SNasf-Fan 	int err;
9890a586ceaSMark Shellenbaum 
990f67950b2SNasf-Fan 	if (usedobj == DMU_PROJECTUSED_OBJECT) {
991f67950b2SNasf-Fan 		if (!dmu_objset_projectquota_present(zfsvfs->z_os)) {
992f67950b2SNasf-Fan 			if (dmu_objset_projectquota_upgradable(zfsvfs->z_os)) {
993f67950b2SNasf-Fan 				dsl_pool_config_enter(
994f67950b2SNasf-Fan 				    dmu_objset_pool(zfsvfs->z_os), FTAG);
995f67950b2SNasf-Fan 				dmu_objset_id_quota_upgrade(zfsvfs->z_os);
996f67950b2SNasf-Fan 				dsl_pool_config_exit(
997f67950b2SNasf-Fan 				    dmu_objset_pool(zfsvfs->z_os), FTAG);
998f67950b2SNasf-Fan 			}
999f67950b2SNasf-Fan 			return (B_FALSE);
1000f67950b2SNasf-Fan 		}
1001f67950b2SNasf-Fan 		quotaobj = zfsvfs->z_projectquota_obj;
1002f67950b2SNasf-Fan 	} else if (usedobj == DMU_USERUSED_OBJECT) {
1003f67950b2SNasf-Fan 		quotaobj = zfsvfs->z_userquota_obj;
1004f67950b2SNasf-Fan 	} else if (usedobj == DMU_GROUPUSED_OBJECT) {
1005f67950b2SNasf-Fan 		quotaobj = zfsvfs->z_groupquota_obj;
1006f67950b2SNasf-Fan 	} else {
1007f67950b2SNasf-Fan 		return (B_FALSE);
1008f67950b2SNasf-Fan 	}
1009f67950b2SNasf-Fan 	if (quotaobj == 0 || zfsvfs->z_replay)
1010f67950b2SNasf-Fan 		return (B_FALSE);
10110a586ceaSMark Shellenbaum 
1012f67950b2SNasf-Fan 	(void) sprintf(buf, "%llx", (longlong_t)id);
1013f67950b2SNasf-Fan 	err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
1014f67950b2SNasf-Fan 	if (err != 0)
1015f67950b2SNasf-Fan 		return (B_FALSE);
10160a586ceaSMark Shellenbaum 
1017f67950b2SNasf-Fan 	err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
1018f67950b2SNasf-Fan 	if (err != 0)
10190a586ceaSMark Shellenbaum 		return (B_FALSE);
1020f67950b2SNasf-Fan 	return (used >= quota);
1021f67950b2SNasf-Fan }
10220a586ceaSMark Shellenbaum 
1023f67950b2SNasf-Fan boolean_t
zfs_id_overquota(zfsvfs_t * zfsvfs,uint64_t usedobj,uint64_t id)1024f67950b2SNasf-Fan zfs_id_overquota(zfsvfs_t *zfsvfs, uint64_t usedobj, uint64_t id)
1025f67950b2SNasf-Fan {
1026f67950b2SNasf-Fan 	return (zfs_id_overblockquota(zfsvfs, usedobj, id) ||
1027f67950b2SNasf-Fan 	    zfs_id_overobjquota(zfsvfs, usedobj, id));
10280a586ceaSMark Shellenbaum }
10290a586ceaSMark Shellenbaum 
10301fdcbd00SMatthew Ahrens /*
10311fdcbd00SMatthew Ahrens  * Associate this zfsvfs with the given objset, which must be owned.
10321fdcbd00SMatthew Ahrens  * This will cache a bunch of on-disk state from the objset in the
10331fdcbd00SMatthew Ahrens  * zfsvfs.
10341fdcbd00SMatthew Ahrens  */
10351fdcbd00SMatthew Ahrens static int
zfsvfs_init(zfsvfs_t * zfsvfs,objset_t * os)10361fdcbd00SMatthew Ahrens zfsvfs_init(zfsvfs_t *zfsvfs, objset_t *os)
103714843421SMatthew Ahrens {
10381fdcbd00SMatthew Ahrens 	int error;
10391fdcbd00SMatthew Ahrens 	uint64_t val;
104014843421SMatthew Ahrens 
1041b5152584SMatthew Ahrens 	zfsvfs->z_max_blksz = SPA_OLD_MAXBLOCKSIZE;
104214843421SMatthew Ahrens 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
104314843421SMatthew Ahrens 	zfsvfs->z_os = os;
104414843421SMatthew Ahrens 
104514843421SMatthew Ahrens 	error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
10461fdcbd00SMatthew Ahrens 	if (error != 0)
10471fdcbd00SMatthew Ahrens 		return (error);
10481fdcbd00SMatthew Ahrens 	if (zfsvfs->z_version >
1049dc7cd546SMark Shellenbaum 	    zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
1050dc7cd546SMark Shellenbaum 		(void) printf("Can't mount a version %lld file system "
1051dc7cd546SMark Shellenbaum 		    "on a version %lld pool\n. Pool must be upgraded to mount "
1052dc7cd546SMark Shellenbaum 		    "this file system.", (u_longlong_t)zfsvfs->z_version,
1053dc7cd546SMark Shellenbaum 		    (u_longlong_t)spa_version(dmu_objset_spa(os)));
10541fdcbd00SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
105514843421SMatthew Ahrens 	}
10561fdcbd00SMatthew Ahrens 	error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &val);
10571fdcbd00SMatthew Ahrens 	if (error != 0)
10581fdcbd00SMatthew Ahrens 		return (error);
10591fdcbd00SMatthew Ahrens 	zfsvfs->z_norm = (int)val;
106014843421SMatthew Ahrens 
10611fdcbd00SMatthew Ahrens 	error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &val);
10621fdcbd00SMatthew Ahrens 	if (error != 0)
10631fdcbd00SMatthew Ahrens 		return (error);
10641fdcbd00SMatthew Ahrens 	zfsvfs->z_utf8 = (val != 0);
106514843421SMatthew Ahrens 
10661fdcbd00SMatthew Ahrens 	error = zfs_get_zplprop(os, ZFS_PROP_CASE, &val);
10671fdcbd00SMatthew Ahrens 	if (error != 0)
10681fdcbd00SMatthew Ahrens 		return (error);
10691fdcbd00SMatthew Ahrens 	zfsvfs->z_case = (uint_t)val;
107014843421SMatthew Ahrens 
107114843421SMatthew Ahrens 	/*
107214843421SMatthew Ahrens 	 * Fold case on file systems that are always or sometimes case
107314843421SMatthew Ahrens 	 * insensitive.
107414843421SMatthew Ahrens 	 */
107514843421SMatthew Ahrens 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
107614843421SMatthew Ahrens 	    zfsvfs->z_case == ZFS_CASE_MIXED)
107714843421SMatthew Ahrens 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
107814843421SMatthew Ahrens 
107914843421SMatthew Ahrens 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
10800a586ceaSMark Shellenbaum 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
10810a586ceaSMark Shellenbaum 
10821fdcbd00SMatthew Ahrens 	uint64_t sa_obj = 0;
10830a586ceaSMark Shellenbaum 	if (zfsvfs->z_use_sa) {
10840a586ceaSMark Shellenbaum 		/* should either have both of these objects or none */
10850a586ceaSMark Shellenbaum 		error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
10860a586ceaSMark Shellenbaum 		    &sa_obj);
10871fdcbd00SMatthew Ahrens 		if (error != 0)
10881fdcbd00SMatthew Ahrens 			return (error);
10890a586ceaSMark Shellenbaum 	}
10900a586ceaSMark Shellenbaum 
10911d8ccc7bSMark Shellenbaum 	error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
10921d8ccc7bSMark Shellenbaum 	    &zfsvfs->z_attr_table);
10931fdcbd00SMatthew Ahrens 	if (error != 0)
10941fdcbd00SMatthew Ahrens 		return (error);
10950a586ceaSMark Shellenbaum 
10960a586ceaSMark Shellenbaum 	if (zfsvfs->z_version >= ZPL_VERSION_SA)
10970a586ceaSMark Shellenbaum 		sa_register_update_callback(os, zfs_sa_upgrade);
109814843421SMatthew Ahrens 
109914843421SMatthew Ahrens 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
110014843421SMatthew Ahrens 	    &zfsvfs->z_root);
11011fdcbd00SMatthew Ahrens 	if (error != 0)
11021fdcbd00SMatthew Ahrens 		return (error);
110314843421SMatthew Ahrens 	ASSERT(zfsvfs->z_root != 0);
110414843421SMatthew Ahrens 
110514843421SMatthew Ahrens 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
110614843421SMatthew Ahrens 	    &zfsvfs->z_unlinkedobj);
11071fdcbd00SMatthew Ahrens 	if (error != 0)
11081fdcbd00SMatthew Ahrens 		return (error);
110914843421SMatthew Ahrens 
111014843421SMatthew Ahrens 	error = zap_lookup(os, MASTER_NODE_OBJ,
111114843421SMatthew Ahrens 	    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
111214843421SMatthew Ahrens 	    8, 1, &zfsvfs->z_userquota_obj);
11131fdcbd00SMatthew Ahrens 	if (error == ENOENT)
11141fdcbd00SMatthew Ahrens 		zfsvfs->z_userquota_obj = 0;
11151fdcbd00SMatthew Ahrens 	else if (error != 0)
11161fdcbd00SMatthew Ahrens 		return (error);
111714843421SMatthew Ahrens 
111814843421SMatthew Ahrens 	error = zap_lookup(os, MASTER_NODE_OBJ,
111914843421SMatthew Ahrens 	    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
112014843421SMatthew Ahrens 	    8, 1, &zfsvfs->z_groupquota_obj);
11211fdcbd00SMatthew Ahrens 	if (error == ENOENT)
11221fdcbd00SMatthew Ahrens 		zfsvfs->z_groupquota_obj = 0;
11231fdcbd00SMatthew Ahrens 	else if (error != 0)
11241fdcbd00SMatthew Ahrens 		return (error);
112514843421SMatthew Ahrens 
1126f67950b2SNasf-Fan 	error = zap_lookup(os, MASTER_NODE_OBJ,
1127f67950b2SNasf-Fan 	    zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA],
1128f67950b2SNasf-Fan 	    8, 1, &zfsvfs->z_projectquota_obj);
1129f67950b2SNasf-Fan 	if (error == ENOENT)
1130f67950b2SNasf-Fan 		zfsvfs->z_projectquota_obj = 0;
1131f67950b2SNasf-Fan 	else if (error != 0)
1132f67950b2SNasf-Fan 		return (error);
1133f67950b2SNasf-Fan 
1134f67950b2SNasf-Fan 	error = zap_lookup(os, MASTER_NODE_OBJ,
1135f67950b2SNasf-Fan 	    zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA],
1136f67950b2SNasf-Fan 	    8, 1, &zfsvfs->z_userobjquota_obj);
1137f67950b2SNasf-Fan 	if (error == ENOENT)
1138f67950b2SNasf-Fan 		zfsvfs->z_userobjquota_obj = 0;
1139f67950b2SNasf-Fan 	else if (error != 0)
1140f67950b2SNasf-Fan 		return (error);
1141f67950b2SNasf-Fan 
1142f67950b2SNasf-Fan 	error = zap_lookup(os, MASTER_NODE_OBJ,
1143f67950b2SNasf-Fan 	    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA],
1144f67950b2SNasf-Fan 	    8, 1, &zfsvfs->z_groupobjquota_obj);
1145f67950b2SNasf-Fan 	if (error == ENOENT)
1146f67950b2SNasf-Fan 		zfsvfs->z_groupobjquota_obj = 0;
1147f67950b2SNasf-Fan 	else if (error != 0)
1148f67950b2SNasf-Fan 		return (error);
1149f67950b2SNasf-Fan 
1150f67950b2SNasf-Fan 	error = zap_lookup(os, MASTER_NODE_OBJ,
1151f67950b2SNasf-Fan 	    zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTOBJQUOTA],
1152f67950b2SNasf-Fan 	    8, 1, &zfsvfs->z_projectobjquota_obj);
1153f67950b2SNasf-Fan 	if (error == ENOENT)
1154f67950b2SNasf-Fan 		zfsvfs->z_projectobjquota_obj = 0;
1155f67950b2SNasf-Fan 	else if (error != 0)
1156f67950b2SNasf-Fan 		return (error);
1157f67950b2SNasf-Fan 
115814843421SMatthew Ahrens 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
115914843421SMatthew Ahrens 	    &zfsvfs->z_fuid_obj);
11601fdcbd00SMatthew Ahrens 	if (error == ENOENT)
11611fdcbd00SMatthew Ahrens 		zfsvfs->z_fuid_obj = 0;
11621fdcbd00SMatthew Ahrens 	else if (error != 0)
11631fdcbd00SMatthew Ahrens 		return (error);
116414843421SMatthew Ahrens 
116514843421SMatthew Ahrens 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
116614843421SMatthew Ahrens 	    &zfsvfs->z_shares_dir);
11671fdcbd00SMatthew Ahrens 	if (error == ENOENT)
11681fdcbd00SMatthew Ahrens 		zfsvfs->z_shares_dir = 0;
11691fdcbd00SMatthew Ahrens 	else if (error != 0)
11701fdcbd00SMatthew Ahrens 		return (error);
11711fdcbd00SMatthew Ahrens 
11721fdcbd00SMatthew Ahrens 	return (0);
11731fdcbd00SMatthew Ahrens }
11741fdcbd00SMatthew Ahrens 
11751fdcbd00SMatthew Ahrens int
zfsvfs_create(const char * osname,boolean_t readonly,zfsvfs_t ** zfvp)11768bf394f1STom Caputi zfsvfs_create(const char *osname, boolean_t readonly, zfsvfs_t **zfvp)
11771fdcbd00SMatthew Ahrens {
11781fdcbd00SMatthew Ahrens 	objset_t *os;
11791fdcbd00SMatthew Ahrens 	zfsvfs_t *zfsvfs;
11801fdcbd00SMatthew Ahrens 	int error;
11818bf394f1STom Caputi 	boolean_t ro = (readonly || (strchr(osname, '@') != NULL));
11821fdcbd00SMatthew Ahrens 
11831fdcbd00SMatthew Ahrens 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
11841fdcbd00SMatthew Ahrens 
1185f67950b2SNasf-Fan 	error = dmu_objset_own(osname, DMU_OST_ZFS, ro, B_TRUE, zfsvfs, &os);
1186dfc11533SChris Williamson 	if (error != 0) {
11871fdcbd00SMatthew Ahrens 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
11881fdcbd00SMatthew Ahrens 		return (error);
11891fdcbd00SMatthew Ahrens 	}
11901fdcbd00SMatthew Ahrens 
1191dfc11533SChris Williamson 	error = zfsvfs_create_impl(zfvp, zfsvfs, os);
1192dfc11533SChris Williamson 	if (error != 0) {
1193eb633035STom Caputi 		dmu_objset_disown(os, B_TRUE, zfsvfs);
1194dfc11533SChris Williamson 	}
1195dfc11533SChris Williamson 	return (error);
1196dfc11533SChris Williamson }
1197dfc11533SChris Williamson 
1198dfc11533SChris Williamson 
1199dfc11533SChris Williamson int
zfsvfs_create_impl(zfsvfs_t ** zfvp,zfsvfs_t * zfsvfs,objset_t * os)1200dfc11533SChris Williamson zfsvfs_create_impl(zfsvfs_t **zfvp, zfsvfs_t *zfsvfs, objset_t *os)
1201dfc11533SChris Williamson {
1202dfc11533SChris Williamson 	int error;
1203dfc11533SChris Williamson 
12041fdcbd00SMatthew Ahrens 	zfsvfs->z_vfs = NULL;
12051fdcbd00SMatthew Ahrens 	zfsvfs->z_parent = zfsvfs;
120614843421SMatthew Ahrens 
120714843421SMatthew Ahrens 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
120814843421SMatthew Ahrens 	mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
120914843421SMatthew Ahrens 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
121014843421SMatthew Ahrens 	    offsetof(znode_t, z_link_node));
1211c9030f6cSAlexander Motin 	rrm_init(&zfsvfs->z_teardown_lock, B_FALSE);
121214843421SMatthew Ahrens 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
121314843421SMatthew Ahrens 	rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
12141fdcbd00SMatthew Ahrens 	for (int i = 0; i != ZFS_OBJ_MTX_SZ; i++)
121514843421SMatthew Ahrens 		mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
121614843421SMatthew Ahrens 
12171fdcbd00SMatthew Ahrens 	error = zfsvfs_init(zfsvfs, os);
12181fdcbd00SMatthew Ahrens 	if (error != 0) {
12191fdcbd00SMatthew Ahrens 		*zfvp = NULL;
12201fdcbd00SMatthew Ahrens 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
12211fdcbd00SMatthew Ahrens 		return (error);
12221fdcbd00SMatthew Ahrens 	}
12231fdcbd00SMatthew Ahrens 
1224c5832a53SAlek Pinchuk 	zfsvfs->z_drain_task = TASKQID_INVALID;
1225c5832a53SAlek Pinchuk 	zfsvfs->z_draining = B_FALSE;
1226c5832a53SAlek Pinchuk 	zfsvfs->z_drain_cancel = B_TRUE;
1227c5832a53SAlek Pinchuk 
1228af4c679fSSean McEnroe 	*zfvp = zfsvfs;
122914843421SMatthew Ahrens 	return (0);
123014843421SMatthew Ahrens }
123114843421SMatthew Ahrens 
1232f18faf3fSek static int
zfsvfs_setup(zfsvfs_t * zfsvfs,boolean_t mounting)1233f18faf3fSek zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
1234f18faf3fSek {
1235f18faf3fSek 	int error;
1236f18faf3fSek 
1237f18faf3fSek 	error = zfs_register_callbacks(zfsvfs->z_vfs);
1238f18faf3fSek 	if (error)
1239f18faf3fSek 		return (error);
1240f18faf3fSek 
1241377c02aaSNeil Perrin 	zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
1242377c02aaSNeil Perrin 
1243f18faf3fSek 	/*
1244f18faf3fSek 	 * If we are not mounting (ie: online recv), then we don't
1245f18faf3fSek 	 * have to worry about replaying the log as we blocked all
1246f18faf3fSek 	 * operations out since we closed the ZIL.
1247f18faf3fSek 	 */
1248f18faf3fSek 	if (mounting) {
1249a6e57bd4SNeil Perrin 		boolean_t readonly;
1250a6e57bd4SNeil Perrin 
1251f18faf3fSek 		/*
1252f18faf3fSek 		 * During replay we remove the read only flag to
1253f18faf3fSek 		 * allow replays to succeed.
1254f18faf3fSek 		 */
1255f18faf3fSek 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
1256c5832a53SAlek Pinchuk 		if (readonly != 0) {
12571209a471SNeil Perrin 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
1258c5832a53SAlek Pinchuk 		} else {
12591209a471SNeil Perrin 			zfs_unlinked_drain(zfsvfs);
1260c5832a53SAlek Pinchuk 		}
1261f18faf3fSek 
126255da60b9SMark J Musante 		/*
126355da60b9SMark J Musante 		 * Parse and replay the intent log.
126455da60b9SMark J Musante 		 *
126555da60b9SMark J Musante 		 * Because of ziltest, this must be done after
126655da60b9SMark J Musante 		 * zfs_unlinked_drain().  (Further note: ziltest
126755da60b9SMark J Musante 		 * doesn't use readonly mounts, where
126855da60b9SMark J Musante 		 * zfs_unlinked_drain() isn't called.)  This is because
126955da60b9SMark J Musante 		 * ziltest causes spa_sync() to think it's committed,
127055da60b9SMark J Musante 		 * but actually it is not, so the intent log contains
127155da60b9SMark J Musante 		 * many txg's worth of changes.
127255da60b9SMark J Musante 		 *
127355da60b9SMark J Musante 		 * In particular, if object N is in the unlinked set in
127455da60b9SMark J Musante 		 * the last txg to actually sync, then it could be
127555da60b9SMark J Musante 		 * actually freed in a later txg and then reallocated
127655da60b9SMark J Musante 		 * in a yet later txg.  This would write a "create
127755da60b9SMark J Musante 		 * object N" record to the intent log.  Normally, this
127855da60b9SMark J Musante 		 * would be fine because the spa_sync() would have
127955da60b9SMark J Musante 		 * written out the fact that object N is free, before
128055da60b9SMark J Musante 		 * we could write the "create object N" intent log
128155da60b9SMark J Musante 		 * record.
128255da60b9SMark J Musante 		 *
128355da60b9SMark J Musante 		 * But when we are in ziltest mode, we advance the "open
128455da60b9SMark J Musante 		 * txg" without actually spa_sync()-ing the changes to
128555da60b9SMark J Musante 		 * disk.  So we would see that object N is still
128655da60b9SMark J Musante 		 * allocated and in the unlinked set, and there is an
128755da60b9SMark J Musante 		 * intent log record saying to allocate it.
128855da60b9SMark J Musante 		 */
1289f9af39baSGeorge Wilson 		if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
1290f9af39baSGeorge Wilson 			if (zil_replay_disable) {
1291f9af39baSGeorge Wilson 				zil_destroy(zfsvfs->z_log, B_FALSE);
1292f9af39baSGeorge Wilson 			} else {
1293f9af39baSGeorge Wilson 				zfsvfs->z_replay = B_TRUE;
1294f9af39baSGeorge Wilson 				zil_replay(zfsvfs->z_os, zfsvfs,
1295f9af39baSGeorge Wilson 				    zfs_replay_vector);
1296f9af39baSGeorge Wilson 				zfsvfs->z_replay = B_FALSE;
1297f9af39baSGeorge Wilson 			}
12981209a471SNeil Perrin 		}
1299eb633035STom Caputi 
1300eb633035STom Caputi 		/* restore readonly bit */
1301eb633035STom Caputi 		if (readonly != 0)
1302eb633035STom Caputi 			zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
1303f18faf3fSek 	}
1304f18faf3fSek 
130590f2c094SAndriy Gapon 	/*
130690f2c094SAndriy Gapon 	 * Set the objset user_ptr to track its zfsvfs.
130790f2c094SAndriy Gapon 	 */
130890f2c094SAndriy Gapon 	mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
130990f2c094SAndriy Gapon 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
131090f2c094SAndriy Gapon 	mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
131190f2c094SAndriy Gapon 
1312f18faf3fSek 	return (0);
1313f18faf3fSek }
1314f18faf3fSek 
131514843421SMatthew Ahrens void
zfsvfs_free(zfsvfs_t * zfsvfs)131614843421SMatthew Ahrens zfsvfs_free(zfsvfs_t *zfsvfs)
131747f263f4Sek {
131814843421SMatthew Ahrens 	int i;
13194e9583b2STom Erickson 	extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
13204e9583b2STom Erickson 
13214e9583b2STom Erickson 	/*
13224e9583b2STom Erickson 	 * This is a barrier to prevent the filesystem from going away in
13234e9583b2STom Erickson 	 * zfs_znode_move() until we can safely ensure that the filesystem is
13244e9583b2STom Erickson 	 * not unmounted. We consider the filesystem valid before the barrier
13254e9583b2STom Erickson 	 * and invalid after the barrier.
13264e9583b2STom Erickson 	 */
13274e9583b2STom Erickson 	rw_enter(&zfsvfs_lock, RW_READER);
13284e9583b2STom Erickson 	rw_exit(&zfsvfs_lock);
132914843421SMatthew Ahrens 
133014843421SMatthew Ahrens 	zfs_fuid_destroy(zfsvfs);
133114843421SMatthew Ahrens 
133247f263f4Sek 	mutex_destroy(&zfsvfs->z_znodes_lock);
13339e1320c0SMark Shellenbaum 	mutex_destroy(&zfsvfs->z_lock);
133447f263f4Sek 	list_destroy(&zfsvfs->z_all_znodes);
1335c9030f6cSAlexander Motin 	rrm_destroy(&zfsvfs->z_teardown_lock);
133647f263f4Sek 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
133747f263f4Sek 	rw_destroy(&zfsvfs->z_fuid_lock);
133814843421SMatthew Ahrens 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
133914843421SMatthew Ahrens 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
134047f263f4Sek 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
134147f263f4Sek }
134247f263f4Sek 
134314843421SMatthew Ahrens static void
zfs_set_fuid_feature(zfsvfs_t * zfsvfs)134414843421SMatthew Ahrens zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
134514843421SMatthew Ahrens {
134614843421SMatthew Ahrens 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
134744bffe01SMark Shellenbaum 	if (zfsvfs->z_vfs) {
134844bffe01SMark Shellenbaum 		if (zfsvfs->z_use_fuids) {
134944bffe01SMark Shellenbaum 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
135044bffe01SMark Shellenbaum 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
135144bffe01SMark Shellenbaum 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
135244bffe01SMark Shellenbaum 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
135344bffe01SMark Shellenbaum 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
135444bffe01SMark Shellenbaum 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
135544bffe01SMark Shellenbaum 		} else {
135644bffe01SMark Shellenbaum 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
135744bffe01SMark Shellenbaum 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
135844bffe01SMark Shellenbaum 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
135944bffe01SMark Shellenbaum 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
136044bffe01SMark Shellenbaum 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
136144bffe01SMark Shellenbaum 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
136244bffe01SMark Shellenbaum 		}
136314843421SMatthew Ahrens 	}
13640a586ceaSMark Shellenbaum 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
136514843421SMatthew Ahrens }
136614843421SMatthew Ahrens 
1367ea8dc4b6Seschrock static int
zfs_domount(vfs_t * vfsp,char * osname)1368088f3894Sahrens zfs_domount(vfs_t *vfsp, char *osname)
1369ea8dc4b6Seschrock {
1370ea8dc4b6Seschrock 	dev_t mount_dev;
137114843421SMatthew Ahrens 	uint64_t recordsize, fsid_guid;
1372ea8dc4b6Seschrock 	int error = 0;
1373ea8dc4b6Seschrock 	zfsvfs_t *zfsvfs;
13748bf394f1STom Caputi 	boolean_t readonly = vfsp->vfs_flag & VFS_RDONLY ? B_TRUE : B_FALSE;
1375ea8dc4b6Seschrock 
1376ea8dc4b6Seschrock 	ASSERT(vfsp);
1377ea8dc4b6Seschrock 	ASSERT(osname);
1378fa9e4066Sahrens 
13798bf394f1STom Caputi 	error = zfsvfs_create(osname, readonly, &zfsvfs);
138014843421SMatthew Ahrens 	if (error)
138114843421SMatthew Ahrens 		return (error);
1382fa9e4066Sahrens 	zfsvfs->z_vfs = vfsp;
1383fa9e4066Sahrens 
1384ea8dc4b6Seschrock 	/* Initialize the generic filesystem structure. */
1385fa9e4066Sahrens 	vfsp->vfs_bcount = 0;
1386fa9e4066Sahrens 	vfsp->vfs_data = NULL;
1387fa9e4066Sahrens 
1388ea8dc4b6Seschrock 	if (zfs_create_unique_device(&mount_dev) == -1) {
1389be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENODEV);
1390ea8dc4b6Seschrock 		goto out;
1391ea8dc4b6Seschrock 	}
1392fa9e4066Sahrens 	ASSERT(vfs_devismounted(mount_dev) == 0);
1393fa9e4066Sahrens 
1394ea8dc4b6Seschrock 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
1395ea8dc4b6Seschrock 	    NULL))
1396ea8dc4b6Seschrock 		goto out;
1397fa9e4066Sahrens 
1398fa9e4066Sahrens 	vfsp->vfs_dev = mount_dev;
1399fa9e4066Sahrens 	vfsp->vfs_fstype = zfsfstype;
1400fa9e4066Sahrens 	vfsp->vfs_bsize = recordsize;
1401fa9e4066Sahrens 	vfsp->vfs_flag |= VFS_NOTRUNC;
1402fa9e4066Sahrens 	vfsp->vfs_data = zfsvfs;
1403fa9e4066Sahrens 
140414843421SMatthew Ahrens 	/*
140514843421SMatthew Ahrens 	 * The fsid is 64 bits, composed of an 8-bit fs type, which
140614843421SMatthew Ahrens 	 * separates our fsid from any other filesystem types, and a
140714843421SMatthew Ahrens 	 * 56-bit objset unique ID.  The objset unique ID is unique to
140814843421SMatthew Ahrens 	 * all objsets open on this system, provided by unique_create().
140914843421SMatthew Ahrens 	 * The 8-bit fs type must be put in the low bits of fsid[1]
141014843421SMatthew Ahrens 	 * because that's where other Solaris filesystems put it.
141114843421SMatthew Ahrens 	 */
141214843421SMatthew Ahrens 	fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
141314843421SMatthew Ahrens 	ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
141414843421SMatthew Ahrens 	vfsp->vfs_fsid.val[0] = fsid_guid;
141514843421SMatthew Ahrens 	vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
141614843421SMatthew Ahrens 	    zfsfstype & 0xFF;
1417ea8dc4b6Seschrock 
1418da6c28aaSamw 	/*
1419da6c28aaSamw 	 * Set features for file system.
1420da6c28aaSamw 	 */
142114843421SMatthew Ahrens 	zfs_set_fuid_feature(zfsvfs);
1422de8267e0Stimh 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
1423de8267e0Stimh 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1424de8267e0Stimh 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1425de8267e0Stimh 		vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
1426de8267e0Stimh 	} else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
1427de8267e0Stimh 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1428de8267e0Stimh 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1429de8267e0Stimh 	}
1430c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
1431da6c28aaSamw 
1432ea8dc4b6Seschrock 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1433da6c28aaSamw 		uint64_t pval;
14347b55fa8eSck 
1435fa9e4066Sahrens 		atime_changed_cb(zfsvfs, B_FALSE);
1436fa9e4066Sahrens 		readonly_changed_cb(zfsvfs, B_TRUE);
1437da6c28aaSamw 		if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
14387b55fa8eSck 			goto out;
1439da6c28aaSamw 		xattr_changed_cb(zfsvfs, pval);
1440fa9e4066Sahrens 		zfsvfs->z_issnap = B_TRUE;
1441b9deb9cbSMark J Musante 		zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1442777badbaSMatthew Ahrens 
1443503ad85cSMatthew Ahrens 		mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1444777badbaSMatthew Ahrens 		dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1445503ad85cSMatthew Ahrens 		mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1446fa9e4066Sahrens 	} else {
1447f18faf3fSek 		error = zfsvfs_setup(zfsvfs, B_TRUE);
1448ea8dc4b6Seschrock 	}
1449fa9e4066Sahrens 
145000bfaff9SPatrick Mooney 	/* cache the root vnode for this mount */
145100bfaff9SPatrick Mooney 	znode_t *rootzp;
145200bfaff9SPatrick Mooney 	if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp)) {
145300bfaff9SPatrick Mooney 		goto out;
145400bfaff9SPatrick Mooney 	}
145500bfaff9SPatrick Mooney 	zfsvfs->z_rootdir = ZTOV(rootzp);
145600bfaff9SPatrick Mooney 
1457ea8dc4b6Seschrock 	if (!zfsvfs->z_issnap)
1458ea8dc4b6Seschrock 		zfsctl_create(zfsvfs);
1459ea8dc4b6Seschrock out:
1460ea8dc4b6Seschrock 	if (error) {
1461eb633035STom Caputi 		dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
146214843421SMatthew Ahrens 		zfsvfs_free(zfsvfs);
1463ea8dc4b6Seschrock 	} else {
14641a5e258fSJosef 'Jeff' Sipek 		atomic_inc_32(&zfs_active_fs_count);
1465ea8dc4b6Seschrock 	}
1466fa9e4066Sahrens 
1467ea8dc4b6Seschrock 	return (error);
1468ea8dc4b6Seschrock }
1469ea8dc4b6Seschrock 
1470ea8dc4b6Seschrock void
zfs_unregister_callbacks(zfsvfs_t * zfsvfs)1471ea8dc4b6Seschrock zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
1472ea8dc4b6Seschrock {
1473ea8dc4b6Seschrock 	objset_t *os = zfsvfs->z_os;
1474fa9e4066Sahrens 
147503bad06fSJustin Gibbs 	if (!dmu_objset_is_snapshot(os))
147603bad06fSJustin Gibbs 		dsl_prop_unregister_all(dmu_objset_ds(os), zfsvfs);
1477ea8dc4b6Seschrock }
1478fa9e4066Sahrens 
1479b1b8ab34Slling /*
1480b1b8ab34Slling  * Convert a decimal digit string to a uint64_t integer.
1481b1b8ab34Slling  */
1482b1b8ab34Slling static int
str_to_uint64(char * str,uint64_t * objnum)1483b1b8ab34Slling str_to_uint64(char *str, uint64_t *objnum)
1484b1b8ab34Slling {
1485b1b8ab34Slling 	uint64_t num = 0;
1486b1b8ab34Slling 
1487b1b8ab34Slling 	while (*str) {
1488b1b8ab34Slling 		if (*str < '0' || *str > '9')
1489be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
1490b1b8ab34Slling 
1491b1b8ab34Slling 		num = num*10 + *str++ - '0';
1492b1b8ab34Slling 	}
1493b1b8ab34Slling 
1494b1b8ab34Slling 	*objnum = num;
1495b1b8ab34Slling 	return (0);
1496b1b8ab34Slling }
1497b1b8ab34Slling 
1498b1b8ab34Slling /*
1499b1b8ab34Slling  * The boot path passed from the boot loader is in the form of
1500b1b8ab34Slling  * "rootpool-name/root-filesystem-object-number'. Convert this
1501b1b8ab34Slling  * string to a dataset name: "rootpool-name/root-filesystem-name".
1502b1b8ab34Slling  */
1503b1b8ab34Slling static int
zfs_parse_bootfs(char * bpath,char * outpath)1504e7cbe64fSgw zfs_parse_bootfs(char *bpath, char *outpath)
1505b1b8ab34Slling {
1506b1b8ab34Slling 	char *slashp;
1507b1b8ab34Slling 	uint64_t objnum;
1508b1b8ab34Slling 	int error;
1509b1b8ab34Slling 
1510b1b8ab34Slling 	if (*bpath == 0 || *bpath == '/')
1511be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1512b1b8ab34Slling 
151319397407SSherry Moore 	(void) strcpy(outpath, bpath);
151419397407SSherry Moore 
1515b1b8ab34Slling 	slashp = strchr(bpath, '/');
1516b1b8ab34Slling 
1517b1b8ab34Slling 	/* if no '/', just return the pool name */
1518b1b8ab34Slling 	if (slashp == NULL) {
1519b1b8ab34Slling 		return (0);
1520b1b8ab34Slling 	}
1521b1b8ab34Slling 
152219397407SSherry Moore 	/* if not a number, just return the root dataset name */
152319397407SSherry Moore 	if (str_to_uint64(slashp+1, &objnum)) {
152419397407SSherry Moore 		return (0);
152519397407SSherry Moore 	}
1526b1b8ab34Slling 
1527b1b8ab34Slling 	*slashp = '\0';
1528b1b8ab34Slling 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
1529b1b8ab34Slling 	*slashp = '/';
1530b1b8ab34Slling 
1531b1b8ab34Slling 	return (error);
1532b1b8ab34Slling }
1533b1b8ab34Slling 
15344201a95eSRic Aleshire /*
1535f7170741SWill Andrews  * Check that the hex label string is appropriate for the dataset being
1536f7170741SWill Andrews  * mounted into the global_zone proper.
15374201a95eSRic Aleshire  *
1538f7170741SWill Andrews  * Return an error if the hex label string is not default or
1539f7170741SWill Andrews  * admin_low/admin_high.  For admin_low labels, the corresponding
1540f7170741SWill Andrews  * dataset must be readonly.
15414201a95eSRic Aleshire  */
15424201a95eSRic Aleshire int
zfs_check_global_label(const char * dsname,const char * hexsl)15434201a95eSRic Aleshire zfs_check_global_label(const char *dsname, const char *hexsl)
15444201a95eSRic Aleshire {
15454201a95eSRic Aleshire 	if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
15464201a95eSRic Aleshire 		return (0);
15474201a95eSRic Aleshire 	if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
15484201a95eSRic Aleshire 		return (0);
15494201a95eSRic Aleshire 	if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
15504201a95eSRic Aleshire 		/* must be readonly */
15514201a95eSRic Aleshire 		uint64_t rdonly;
15524201a95eSRic Aleshire 
15534201a95eSRic Aleshire 		if (dsl_prop_get_integer(dsname,
15544201a95eSRic Aleshire 		    zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
1555be6fd75aSMatthew Ahrens 			return (SET_ERROR(EACCES));
15564201a95eSRic Aleshire 		return (rdonly ? 0 : EACCES);
15574201a95eSRic Aleshire 	}
1558be6fd75aSMatthew Ahrens 	return (SET_ERROR(EACCES));
15594201a95eSRic Aleshire }
15604201a95eSRic Aleshire 
1561f67950b2SNasf-Fan static int
zfs_statfs_project(zfsvfs_t * zfsvfs,znode_t * zp,struct statvfs64 * statp,uint32_t bshift)1562f67950b2SNasf-Fan zfs_statfs_project(zfsvfs_t *zfsvfs, znode_t *zp, struct statvfs64 *statp,
1563f67950b2SNasf-Fan     uint32_t bshift)
1564f67950b2SNasf-Fan {
1565f67950b2SNasf-Fan 	char buf[20 + DMU_OBJACCT_PREFIX_LEN];
1566f67950b2SNasf-Fan 	uint64_t offset = DMU_OBJACCT_PREFIX_LEN;
1567f67950b2SNasf-Fan 	uint64_t quota;
1568f67950b2SNasf-Fan 	uint64_t used;
1569f67950b2SNasf-Fan 	int err;
1570f67950b2SNasf-Fan 
1571f67950b2SNasf-Fan 	strlcpy(buf, DMU_OBJACCT_PREFIX, DMU_OBJACCT_PREFIX_LEN + 1);
1572f67950b2SNasf-Fan 	err = id_to_fuidstr(zfsvfs, NULL, zp->z_projid, buf + offset, B_FALSE);
1573f67950b2SNasf-Fan 	if (err)
1574f67950b2SNasf-Fan 		return (err);
1575f67950b2SNasf-Fan 
1576f67950b2SNasf-Fan 	if (zfsvfs->z_projectquota_obj == 0)
1577f67950b2SNasf-Fan 		goto objs;
1578f67950b2SNasf-Fan 
1579f67950b2SNasf-Fan 	err = zap_lookup(zfsvfs->z_os, zfsvfs->z_projectquota_obj,
1580f67950b2SNasf-Fan 	    buf + offset, 8, 1, &quota);
1581f67950b2SNasf-Fan 	if (err == ENOENT)
1582f67950b2SNasf-Fan 		goto objs;
1583f67950b2SNasf-Fan 	else if (err)
1584f67950b2SNasf-Fan 		return (err);
1585f67950b2SNasf-Fan 
1586f67950b2SNasf-Fan 	err = zap_lookup(zfsvfs->z_os, DMU_PROJECTUSED_OBJECT,
1587f67950b2SNasf-Fan 	    buf + offset, 8, 1, &used);
1588f67950b2SNasf-Fan 	if (unlikely(err == ENOENT)) {
1589f67950b2SNasf-Fan 		uint32_t blksize;
1590f67950b2SNasf-Fan 		u_longlong_t nblocks;
1591f67950b2SNasf-Fan 
1592f67950b2SNasf-Fan 		/*
1593f67950b2SNasf-Fan 		 * Quota accounting is async, so it is possible race case.
1594f67950b2SNasf-Fan 		 * There is at least one object with the given project ID.
1595f67950b2SNasf-Fan 		 */
1596f67950b2SNasf-Fan 		sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
1597f67950b2SNasf-Fan 		if (unlikely(zp->z_blksz == 0))
1598f67950b2SNasf-Fan 			blksize = zfsvfs->z_max_blksz;
1599f67950b2SNasf-Fan 
1600f67950b2SNasf-Fan 		used = blksize * nblocks;
1601f67950b2SNasf-Fan 	} else if (err) {
1602f67950b2SNasf-Fan 		return (err);
1603f67950b2SNasf-Fan 	}
1604f67950b2SNasf-Fan 
1605f67950b2SNasf-Fan 	statp->f_blocks = quota >> bshift;
1606f67950b2SNasf-Fan 	statp->f_bfree = (quota > used) ? ((quota - used) >> bshift) : 0;
1607f67950b2SNasf-Fan 	statp->f_bavail = statp->f_bfree;
1608f67950b2SNasf-Fan 
1609f67950b2SNasf-Fan objs:
1610f67950b2SNasf-Fan 	if (zfsvfs->z_projectobjquota_obj == 0)
1611f67950b2SNasf-Fan 		return (0);
1612f67950b2SNasf-Fan 
1613f67950b2SNasf-Fan 	err = zap_lookup(zfsvfs->z_os, zfsvfs->z_projectobjquota_obj,
1614f67950b2SNasf-Fan 	    buf + offset, 8, 1, &quota);
1615f67950b2SNasf-Fan 	if (err == ENOENT)
1616f67950b2SNasf-Fan 		return (0);
1617f67950b2SNasf-Fan 	else if (err)
1618f67950b2SNasf-Fan 		return (err);
1619f67950b2SNasf-Fan 
1620f67950b2SNasf-Fan 	err = zap_lookup(zfsvfs->z_os, DMU_PROJECTUSED_OBJECT,
1621f67950b2SNasf-Fan 	    buf, 8, 1, &used);
1622f67950b2SNasf-Fan 	if (unlikely(err == ENOENT)) {
1623f67950b2SNasf-Fan 		/*
1624f67950b2SNasf-Fan 		 * Quota accounting is async, so it is possible race case.
1625f67950b2SNasf-Fan 		 * There is at least one object with the given project ID.
1626f67950b2SNasf-Fan 		 */
1627f67950b2SNasf-Fan 		used = 1;
1628f67950b2SNasf-Fan 	} else if (err) {
1629f67950b2SNasf-Fan 		return (err);
1630f67950b2SNasf-Fan 	}
1631f67950b2SNasf-Fan 
1632f67950b2SNasf-Fan 	statp->f_files = quota;
1633f67950b2SNasf-Fan 	statp->f_ffree = (quota > used) ? (quota - used) : 0;
1634f67950b2SNasf-Fan 
1635f67950b2SNasf-Fan 	return (0);
1636f67950b2SNasf-Fan }
1637f67950b2SNasf-Fan 
16384201a95eSRic Aleshire /*
1639f7170741SWill Andrews  * Determine whether the mount is allowed according to MAC check.
1640f7170741SWill Andrews  * by comparing (where appropriate) label of the dataset against
1641f7170741SWill Andrews  * the label of the zone being mounted into.  If the dataset has
1642f7170741SWill Andrews  * no label, create one.
16434201a95eSRic Aleshire  *
1644f7170741SWill Andrews  * Returns 0 if access allowed, error otherwise (e.g. EACCES)
16454201a95eSRic Aleshire  */
16464201a95eSRic Aleshire static int
zfs_mount_label_policy(vfs_t * vfsp,char * osname)16474201a95eSRic Aleshire zfs_mount_label_policy(vfs_t *vfsp, char *osname)
16484201a95eSRic Aleshire {
16494201a95eSRic Aleshire 	int		error, retv;
16504201a95eSRic Aleshire 	zone_t		*mntzone = NULL;
16514201a95eSRic Aleshire 	ts_label_t	*mnt_tsl;
16524201a95eSRic Aleshire 	bslabel_t	*mnt_sl;
16534201a95eSRic Aleshire 	bslabel_t	ds_sl;
16544201a95eSRic Aleshire 	char		ds_hexsl[MAXNAMELEN];
16554201a95eSRic Aleshire 
16564201a95eSRic Aleshire 	retv = EACCES;				/* assume the worst */
16574201a95eSRic Aleshire 
16584201a95eSRic Aleshire 	/*
16594201a95eSRic Aleshire 	 * Start by getting the dataset label if it exists.
16604201a95eSRic Aleshire 	 */
16614201a95eSRic Aleshire 	error = dsl_prop_get(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
16624201a95eSRic Aleshire 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
16634201a95eSRic Aleshire 	if (error)
1664be6fd75aSMatthew Ahrens 		return (SET_ERROR(EACCES));
16654201a95eSRic Aleshire 
16664201a95eSRic Aleshire 	/*
16674201a95eSRic Aleshire 	 * If labeling is NOT enabled, then disallow the mount of datasets
16684201a95eSRic Aleshire 	 * which have a non-default label already.  No other label checks
16694201a95eSRic Aleshire 	 * are needed.
16704201a95eSRic Aleshire 	 */
16714201a95eSRic Aleshire 	if (!is_system_labeled()) {
16724201a95eSRic Aleshire 		if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
16734201a95eSRic Aleshire 			return (0);
1674be6fd75aSMatthew Ahrens 		return (SET_ERROR(EACCES));
16754201a95eSRic Aleshire 	}
16764201a95eSRic Aleshire 
16774201a95eSRic Aleshire 	/*
16784201a95eSRic Aleshire 	 * Get the label of the mountpoint.  If mounting into the global
16794201a95eSRic Aleshire 	 * zone (i.e. mountpoint is not within an active zone and the
16804201a95eSRic Aleshire 	 * zoned property is off), the label must be default or
16814201a95eSRic Aleshire 	 * admin_low/admin_high only; no other checks are needed.
16824201a95eSRic Aleshire 	 */
16834201a95eSRic Aleshire 	mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE);
16844201a95eSRic Aleshire 	if (mntzone->zone_id == GLOBAL_ZONEID) {
16854201a95eSRic Aleshire 		uint64_t zoned;
16864201a95eSRic Aleshire 
16874201a95eSRic Aleshire 		zone_rele(mntzone);
16884201a95eSRic Aleshire 
16894201a95eSRic Aleshire 		if (dsl_prop_get_integer(osname,
16904201a95eSRic Aleshire 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
1691be6fd75aSMatthew Ahrens 			return (SET_ERROR(EACCES));
16924201a95eSRic Aleshire 		if (!zoned)
16934201a95eSRic Aleshire 			return (zfs_check_global_label(osname, ds_hexsl));
16944201a95eSRic Aleshire 		else
16954201a95eSRic Aleshire 			/*
16964201a95eSRic Aleshire 			 * This is the case of a zone dataset being mounted
16974201a95eSRic Aleshire 			 * initially, before the zone has been fully created;
16984201a95eSRic Aleshire 			 * allow this mount into global zone.
16994201a95eSRic Aleshire 			 */
17004201a95eSRic Aleshire 			return (0);
17014201a95eSRic Aleshire 	}
17024201a95eSRic Aleshire 
17034201a95eSRic Aleshire 	mnt_tsl = mntzone->zone_slabel;
17044201a95eSRic Aleshire 	ASSERT(mnt_tsl != NULL);
17054201a95eSRic Aleshire 	label_hold(mnt_tsl);
17064201a95eSRic Aleshire 	mnt_sl = label2bslabel(mnt_tsl);
17074201a95eSRic Aleshire 
17084201a95eSRic Aleshire 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) {
17094201a95eSRic Aleshire 		/*
17104201a95eSRic Aleshire 		 * The dataset doesn't have a real label, so fabricate one.
17114201a95eSRic Aleshire 		 */
17124201a95eSRic Aleshire 		char *str = NULL;
17134201a95eSRic Aleshire 
17144201a95eSRic Aleshire 		if (l_to_str_internal(mnt_sl, &str) == 0 &&
17153b2aab18SMatthew Ahrens 		    dsl_prop_set_string(osname,
17163b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_MLSLABEL),
17173b2aab18SMatthew Ahrens 		    ZPROP_SRC_LOCAL, str) == 0)
17184201a95eSRic Aleshire 			retv = 0;
17194201a95eSRic Aleshire 		if (str != NULL)
17204201a95eSRic Aleshire 			kmem_free(str, strlen(str) + 1);
17214201a95eSRic Aleshire 	} else if (hexstr_to_label(ds_hexsl, &ds_sl) == 0) {
17224201a95eSRic Aleshire 		/*
17234201a95eSRic Aleshire 		 * Now compare labels to complete the MAC check.  If the
17244201a95eSRic Aleshire 		 * labels are equal then allow access.  If the mountpoint
17254201a95eSRic Aleshire 		 * label dominates the dataset label, allow readonly access.
17264201a95eSRic Aleshire 		 * Otherwise, access is denied.
17274201a95eSRic Aleshire 		 */
17284201a95eSRic Aleshire 		if (blequal(mnt_sl, &ds_sl))
17294201a95eSRic Aleshire 			retv = 0;
17304201a95eSRic Aleshire 		else if (bldominates(mnt_sl, &ds_sl)) {
17314201a95eSRic Aleshire 			vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
17324201a95eSRic Aleshire 			retv = 0;
17334201a95eSRic Aleshire 		}
17344201a95eSRic Aleshire 	}
17354201a95eSRic Aleshire 
17364201a95eSRic Aleshire 	label_rele(mnt_tsl);
17374201a95eSRic Aleshire 	zone_rele(mntzone);
17384201a95eSRic Aleshire 	return (retv);
17394201a95eSRic Aleshire }
17404201a95eSRic Aleshire 
174130c304d9SJoshua M. Clulow /*
174230c304d9SJoshua M. Clulow  * Load a string-valued boot property and attempt to convert it to a 64-bit
174330c304d9SJoshua M. Clulow  * unsigned integer.  If the value is not present, or the conversion fails,
174430c304d9SJoshua M. Clulow  * return the provided default value.
174530c304d9SJoshua M. Clulow  */
174630c304d9SJoshua M. Clulow static uint64_t
spa_get_bootprop_uint64(const char * name,uint64_t defval)174730c304d9SJoshua M. Clulow spa_get_bootprop_uint64(const char *name, uint64_t defval)
174830c304d9SJoshua M. Clulow {
174930c304d9SJoshua M. Clulow 	char *propval;
175030c304d9SJoshua M. Clulow 	u_longlong_t r;
175130c304d9SJoshua M. Clulow 	int e;
175230c304d9SJoshua M. Clulow 
175330c304d9SJoshua M. Clulow 	if ((propval = spa_get_bootprop(name)) == NULL) {
175430c304d9SJoshua M. Clulow 		/*
175530c304d9SJoshua M. Clulow 		 * The property does not exist.
175630c304d9SJoshua M. Clulow 		 */
175730c304d9SJoshua M. Clulow 		return (defval);
175830c304d9SJoshua M. Clulow 	}
175930c304d9SJoshua M. Clulow 
176030c304d9SJoshua M. Clulow 	e = ddi_strtoull(propval, NULL, 10, &r);
176130c304d9SJoshua M. Clulow 
176230c304d9SJoshua M. Clulow 	spa_free_bootprop(propval);
176330c304d9SJoshua M. Clulow 
176430c304d9SJoshua M. Clulow 	/*
176530c304d9SJoshua M. Clulow 	 * If the conversion succeeded, return the value.  If there was any
176630c304d9SJoshua M. Clulow 	 * kind of failure, just return the default value.
176730c304d9SJoshua M. Clulow 	 */
176830c304d9SJoshua M. Clulow 	return (e == 0 ? r : defval);
176930c304d9SJoshua M. Clulow }
177030c304d9SJoshua M. Clulow 
1771ea8dc4b6Seschrock static int
zfs_mountroot(vfs_t * vfsp,enum whymountroot why)1772ea8dc4b6Seschrock zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
1773ea8dc4b6Seschrock {
1774ea8dc4b6Seschrock 	int error = 0;
1775ea8dc4b6Seschrock 	static int zfsrootdone = 0;
1776ea8dc4b6Seschrock 	zfsvfs_t *zfsvfs = NULL;
1777ea8dc4b6Seschrock 	znode_t *zp = NULL;
1778ea8dc4b6Seschrock 	vnode_t *vp = NULL;
1779e7cbe64fSgw 	char *zfs_bootfs;
1780051aabe6Staylor 	char *zfs_devid;
17818b26092dSJoshua M. Clulow 	char *zfs_rootdisk_path;
178230c304d9SJoshua M. Clulow 	uint64_t zfs_bootpool;
178330c304d9SJoshua M. Clulow 	uint64_t zfs_bootvdev;
1784ea8dc4b6Seschrock 
1785ea8dc4b6Seschrock 	ASSERT(vfsp);
1786ea8dc4b6Seschrock 
1787ea8dc4b6Seschrock 	/*
1788b1b8ab34Slling 	 * The filesystem that we mount as root is defined in the
1789e7cbe64fSgw 	 * boot property "zfs-bootfs" with a format of
1790e7cbe64fSgw 	 * "poolname/root-dataset-objnum".
1791ea8dc4b6Seschrock 	 */
1792ea8dc4b6Seschrock 	if (why == ROOT_INIT) {
1793ea8dc4b6Seschrock 		if (zfsrootdone++)
1794be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBUSY));
179530c304d9SJoshua M. Clulow 
1796e7cbe64fSgw 		/*
1797e7cbe64fSgw 		 * the process of doing a spa_load will require the
1798e7cbe64fSgw 		 * clock to be set before we could (for example) do
1799e7cbe64fSgw 		 * something better by looking at the timestamp on
1800e7cbe64fSgw 		 * an uberblock, so just set it to -1.
1801e7cbe64fSgw 		 */
1802e7cbe64fSgw 		clkset(-1);
1803fa9e4066Sahrens 
1804051aabe6Staylor 		if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
1805051aabe6Staylor 			cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
1806051aabe6Staylor 			    "bootfs name");
1807be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
1808986fd29aSsetje 		}
1809051aabe6Staylor 		zfs_devid = spa_get_bootprop("diskdevid");
181030c304d9SJoshua M. Clulow 
181130c304d9SJoshua M. Clulow 		/*
181230c304d9SJoshua M. Clulow 		 * The boot loader may also provide us with the GUID for both
181330c304d9SJoshua M. Clulow 		 * the pool and the nominated boot vdev.  A GUID value of 0 is
181430c304d9SJoshua M. Clulow 		 * explicitly invalid (see "spa_change_guid()"), so we use this
181530c304d9SJoshua M. Clulow 		 * as a sentinel value when no GUID is present.
181630c304d9SJoshua M. Clulow 		 */
181730c304d9SJoshua M. Clulow 		zfs_bootpool = spa_get_bootprop_uint64("zfs-bootpool", 0);
181830c304d9SJoshua M. Clulow 		zfs_bootvdev = spa_get_bootprop_uint64("zfs-bootvdev", 0);
181930c304d9SJoshua M. Clulow 
18208b26092dSJoshua M. Clulow 		/*
18218b26092dSJoshua M. Clulow 		 * If we have been given a root disk override path, we want to
18228b26092dSJoshua M. Clulow 		 * ignore device paths from the pool configuration and use only
18238b26092dSJoshua M. Clulow 		 * the specific path we were given in the boot properties.
18248b26092dSJoshua M. Clulow 		 */
18258b26092dSJoshua M. Clulow 		zfs_rootdisk_path = spa_get_bootprop("zfs-rootdisk-path");
18268b26092dSJoshua M. Clulow 
182730c304d9SJoshua M. Clulow 		/*
182830c304d9SJoshua M. Clulow 		 * Initialise the early boot device rescan mechanism.  A scan
182930c304d9SJoshua M. Clulow 		 * will not actually be performed unless we need to do so in
183030c304d9SJoshua M. Clulow 		 * order to find the correct /devices path for a relocated
183130c304d9SJoshua M. Clulow 		 * device.
183230c304d9SJoshua M. Clulow 		 */
18338b26092dSJoshua M. Clulow 		vdev_disk_preroot_init(zfs_rootdisk_path);
183430c304d9SJoshua M. Clulow 
183530c304d9SJoshua M. Clulow 		error = spa_import_rootpool(rootfs.bo_name, zfs_devid,
183630c304d9SJoshua M. Clulow 		    zfs_bootpool, zfs_bootvdev);
183730c304d9SJoshua M. Clulow 
183830c304d9SJoshua M. Clulow 		spa_free_bootprop(zfs_devid);
183930c304d9SJoshua M. Clulow 
184030c304d9SJoshua M. Clulow 		if (error != 0) {
1841051aabe6Staylor 			spa_free_bootprop(zfs_bootfs);
18428b26092dSJoshua M. Clulow 			spa_free_bootprop(zfs_rootdisk_path);
184330c304d9SJoshua M. Clulow 			vdev_disk_preroot_fini();
1844051aabe6Staylor 			cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
1845e7cbe64fSgw 			    error);
1846e7cbe64fSgw 			return (error);
1847e7cbe64fSgw 		}
184830c304d9SJoshua M. Clulow 
1849e7cbe64fSgw 		if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
1850051aabe6Staylor 			spa_free_bootprop(zfs_bootfs);
18518b26092dSJoshua M. Clulow 			spa_free_bootprop(zfs_rootdisk_path);
185230c304d9SJoshua M. Clulow 			vdev_disk_preroot_fini();
1853051aabe6Staylor 			cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
1854e7cbe64fSgw 			    error);
1855b1b8ab34Slling 			return (error);
1856e7cbe64fSgw 		}
1857e7cbe64fSgw 
1858051aabe6Staylor 		spa_free_bootprop(zfs_bootfs);
18598b26092dSJoshua M. Clulow 		spa_free_bootprop(zfs_rootdisk_path);
1860fa9e4066Sahrens 
18618b26092dSJoshua M. Clulow 		if ((error = vfs_lock(vfsp)) != 0) {
18628b26092dSJoshua M. Clulow 			vdev_disk_preroot_fini();
1863ea8dc4b6Seschrock 			return (error);
18648b26092dSJoshua M. Clulow 		}
1865fa9e4066Sahrens 
1866088f3894Sahrens 		if (error = zfs_domount(vfsp, rootfs.bo_name)) {
1867051aabe6Staylor 			cmn_err(CE_NOTE, "zfs_domount: error %d", error);
1868ea8dc4b6Seschrock 			goto out;
1869e7cbe64fSgw 		}
1870ea8dc4b6Seschrock 
187100bfaff9SPatrick Mooney 		/* zfs_domount has already cached the root vnode for us */
1872ea8dc4b6Seschrock 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
1873ea8dc4b6Seschrock 		ASSERT(zfsvfs);
187400bfaff9SPatrick Mooney 		ASSERT(zfsvfs->z_rootdir);
1875ea8dc4b6Seschrock 
187600bfaff9SPatrick Mooney 		vp = zfsvfs->z_rootdir;
1877ea8dc4b6Seschrock 		mutex_enter(&vp->v_lock);
1878ea8dc4b6Seschrock 		vp->v_flag |= VROOT;
1879ea8dc4b6Seschrock 		mutex_exit(&vp->v_lock);
1880ea8dc4b6Seschrock 
1881ea8dc4b6Seschrock 		/*
188240d3dfe1Smarks 		 * Leave rootvp held.  The root file system is never unmounted.
1883ea8dc4b6Seschrock 		 */
188400bfaff9SPatrick Mooney 		VN_HOLD(vp);
188500bfaff9SPatrick Mooney 		rootvp = vp;
1886ea8dc4b6Seschrock 
1887ea8dc4b6Seschrock 		vfs_add((struct vnode *)0, vfsp,
1888ea8dc4b6Seschrock 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
1889ea8dc4b6Seschrock out:
18908b26092dSJoshua M. Clulow 		vdev_disk_preroot_fini();
1891ea8dc4b6Seschrock 		vfs_unlock(vfsp);
1892e7cbe64fSgw 		return (error);
1893ea8dc4b6Seschrock 	} else if (why == ROOT_REMOUNT) {
1894ea8dc4b6Seschrock 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
1895ea8dc4b6Seschrock 		vfsp->vfs_flag |= VFS_REMOUNT;
1896b510d378Slling 
1897b510d378Slling 		/* refresh mount options */
1898b510d378Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
1899b510d378Slling 		return (zfs_register_callbacks(vfsp));
1900b510d378Slling 
1901ea8dc4b6Seschrock 	} else if (why == ROOT_UNMOUNT) {
1902ea8dc4b6Seschrock 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
1903ea8dc4b6Seschrock 		(void) zfs_sync(vfsp, 0, 0);
1904ea8dc4b6Seschrock 		return (0);
1905ea8dc4b6Seschrock 	}
1906ea8dc4b6Seschrock 
1907ea8dc4b6Seschrock 	/*
1908ea8dc4b6Seschrock 	 * if "why" is equal to anything else other than ROOT_INIT,
1909ea8dc4b6Seschrock 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
1910ea8dc4b6Seschrock 	 */
1911be6fd75aSMatthew Ahrens 	return (SET_ERROR(ENOTSUP));
1912ea8dc4b6Seschrock }
1913ea8dc4b6Seschrock 
1914ea8dc4b6Seschrock /*ARGSUSED*/
1915ea8dc4b6Seschrock static int
zfs_mount(vfs_t * vfsp,vnode_t * mvp,struct mounta * uap,cred_t * cr)1916ea8dc4b6Seschrock zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
1917ea8dc4b6Seschrock {
1918ea8dc4b6Seschrock 	char		*osname;
1919ea8dc4b6Seschrock 	pathname_t	spn;
1920ea8dc4b6Seschrock 	int		error = 0;
1921ea8dc4b6Seschrock 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
1922b1b8ab34Slling 	    UIO_SYSSPACE : UIO_USERSPACE;
1923ea8dc4b6Seschrock 	int		canwrite;
1924ea8dc4b6Seschrock 
1925ea8dc4b6Seschrock 	if (mvp->v_type != VDIR)
1926be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTDIR));
1927ea8dc4b6Seschrock 
1928ea8dc4b6Seschrock 	mutex_enter(&mvp->v_lock);
1929ea8dc4b6Seschrock 	if ((uap->flags & MS_REMOUNT) == 0 &&
1930ea8dc4b6Seschrock 	    (uap->flags & MS_OVERLAY) == 0 &&
1931ea8dc4b6Seschrock 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
1932ea8dc4b6Seschrock 		mutex_exit(&mvp->v_lock);
1933be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
1934ea8dc4b6Seschrock 	}
1935ea8dc4b6Seschrock 	mutex_exit(&mvp->v_lock);
1936ea8dc4b6Seschrock 
1937ea8dc4b6Seschrock 	/*
1938ea8dc4b6Seschrock 	 * ZFS does not support passing unparsed data in via MS_DATA.
1939ea8dc4b6Seschrock 	 * Users should use the MS_OPTIONSTR interface; this means
1940ea8dc4b6Seschrock 	 * that all option parsing is already done and the options struct
1941ea8dc4b6Seschrock 	 * can be interrogated.
1942ea8dc4b6Seschrock 	 */
1943ea8dc4b6Seschrock 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
1944be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1945ea8dc4b6Seschrock 
1946ea8dc4b6Seschrock 	/*
1947ea8dc4b6Seschrock 	 * Get the objset name (the "special" mount argument).
1948ea8dc4b6Seschrock 	 */
1949ea8dc4b6Seschrock 	if (error = pn_get(uap->spec, fromspace, &spn))
1950ea8dc4b6Seschrock 		return (error);
1951ea8dc4b6Seschrock 
1952ea8dc4b6Seschrock 	osname = spn.pn_path;
1953ea8dc4b6Seschrock 
1954ecd6cf80Smarks 	/*
1955ecd6cf80Smarks 	 * Check for mount privilege?
1956ecd6cf80Smarks 	 *
1957ecd6cf80Smarks 	 * If we don't have privilege then see if
1958ecd6cf80Smarks 	 * we have local permission to allow it
1959ecd6cf80Smarks 	 */
1960ecd6cf80Smarks 	error = secpolicy_fs_mount(cr, mvp, vfsp);
1961ecd6cf80Smarks 	if (error) {
196298679b56SMark Shellenbaum 		if (dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) == 0) {
1963ecd6cf80Smarks 			vattr_t		vattr;
1964ecd6cf80Smarks 
1965ecd6cf80Smarks 			/*
1966ecd6cf80Smarks 			 * Make sure user is the owner of the mount point
1967ecd6cf80Smarks 			 * or has sufficient privileges.
1968ecd6cf80Smarks 			 */
1969ecd6cf80Smarks 
1970ecd6cf80Smarks 			vattr.va_mask = AT_UID;
1971ecd6cf80Smarks 
197298679b56SMark Shellenbaum 			if (VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
1973ecd6cf80Smarks 				goto out;
1974ecd6cf80Smarks 			}
1975ecd6cf80Smarks 
19762459a9eaSmarks 			if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 &&
19772459a9eaSmarks 			    VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) {
1978ecd6cf80Smarks 				goto out;
1979ecd6cf80Smarks 			}
1980ecd6cf80Smarks 			secpolicy_fs_mount_clearopts(cr, vfsp);
1981ecd6cf80Smarks 		} else {
1982ecd6cf80Smarks 			goto out;
1983ecd6cf80Smarks 		}
1984ecd6cf80Smarks 	}
1985ea8dc4b6Seschrock 
1986ea8dc4b6Seschrock 	/*
1987ea8dc4b6Seschrock 	 * Refuse to mount a filesystem if we are in a local zone and the
1988ea8dc4b6Seschrock 	 * dataset is not visible.
1989ea8dc4b6Seschrock 	 */
1990ea8dc4b6Seschrock 	if (!INGLOBALZONE(curproc) &&
1991ea8dc4b6Seschrock 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1992be6fd75aSMatthew Ahrens 		error = SET_ERROR(EPERM);
1993ea8dc4b6Seschrock 		goto out;
1994ea8dc4b6Seschrock 	}
1995ea8dc4b6Seschrock 
19964201a95eSRic Aleshire 	error = zfs_mount_label_policy(vfsp, osname);
19974201a95eSRic Aleshire 	if (error)
19984201a95eSRic Aleshire 		goto out;
19994201a95eSRic Aleshire 
2000b510d378Slling 	/*
2001b510d378Slling 	 * When doing a remount, we simply refresh our temporary properties
2002b510d378Slling 	 * according to those options set in the current VFS options.
2003b510d378Slling 	 */
2004b510d378Slling 	if (uap->flags & MS_REMOUNT) {
2005b510d378Slling 		/* refresh mount options */
2006b510d378Slling 		zfs_unregister_callbacks(vfsp->vfs_data);
2007b510d378Slling 		error = zfs_register_callbacks(vfsp);
2008b510d378Slling 		goto out;
2009b510d378Slling 	}
2010b510d378Slling 
2011088f3894Sahrens 	error = zfs_domount(vfsp, osname);
2012ea8dc4b6Seschrock 
2013142ae85dSChris Kirby 	/*
2014142ae85dSChris Kirby 	 * Add an extra VFS_HOLD on our parent vfs so that it can't
2015142ae85dSChris Kirby 	 * disappear due to a forced unmount.
2016142ae85dSChris Kirby 	 */
2017984a131bSChris Kirby 	if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap)
2018142ae85dSChris Kirby 		VFS_HOLD(mvp->v_vfsp);
2019142ae85dSChris Kirby 
2020ea8dc4b6Seschrock out:
2021fa9e4066Sahrens 	pn_free(&spn);
2022fa9e4066Sahrens 	return (error);
2023fa9e4066Sahrens }
2024fa9e4066Sahrens 
2025fa9e4066Sahrens static int
zfs_statvfs(vfs_t * vfsp,struct statvfs64 * statp)2026fa9e4066Sahrens zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
2027fa9e4066Sahrens {
2028fa9e4066Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
2029fa9e4066Sahrens 	dev32_t d32;
2030a2eea2e1Sahrens 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
2031f67950b2SNasf-Fan 	int err = 0;
2032fa9e4066Sahrens 
2033fa9e4066Sahrens 	ZFS_ENTER(zfsvfs);
2034fa9e4066Sahrens 
2035a2eea2e1Sahrens 	dmu_objset_space(zfsvfs->z_os,
2036a2eea2e1Sahrens 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
2037fa9e4066Sahrens 
2038fa9e4066Sahrens 	/*
2039fa9e4066Sahrens 	 * The underlying storage pool actually uses multiple block sizes.
2040fa9e4066Sahrens 	 * We report the fragsize as the smallest block size we support,
2041fa9e4066Sahrens 	 * and we report our blocksize as the filesystem's maximum blocksize.
2042fa9e4066Sahrens 	 */
2043fa9e4066Sahrens 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
2044fa9e4066Sahrens 	statp->f_bsize = zfsvfs->z_max_blksz;
2045fa9e4066Sahrens 
2046fa9e4066Sahrens 	/*
2047fa9e4066Sahrens 	 * The following report "total" blocks of various kinds in the
2048fa9e4066Sahrens 	 * file system, but reported in terms of f_frsize - the
2049fa9e4066Sahrens 	 * "fragment" size.
2050fa9e4066Sahrens 	 */
2051fa9e4066Sahrens 
2052a2eea2e1Sahrens 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
2053a2eea2e1Sahrens 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
2054fa9e4066Sahrens 	statp->f_bavail = statp->f_bfree; /* no root reservation */
2055fa9e4066Sahrens 
2056fa9e4066Sahrens 	/*
2057fa9e4066Sahrens 	 * statvfs() should really be called statufs(), because it assumes
2058fa9e4066Sahrens 	 * static metadata.  ZFS doesn't preallocate files, so the best
2059fa9e4066Sahrens 	 * we can do is report the max that could possibly fit in f_files,
2060fa9e4066Sahrens 	 * and that minus the number actually used in f_ffree.
2061fa9e4066Sahrens 	 * For f_ffree, report the smaller of the number of object available
2062fa9e4066Sahrens 	 * and the number of blocks (each object will take at least a block).
2063fa9e4066Sahrens 	 */
2064a2eea2e1Sahrens 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
2065fa9e4066Sahrens 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
2066a2eea2e1Sahrens 	statp->f_files = statp->f_ffree + usedobjs;
2067fa9e4066Sahrens 
2068fa9e4066Sahrens 	(void) cmpldev(&d32, vfsp->vfs_dev);
2069fa9e4066Sahrens 	statp->f_fsid = d32;
2070fa9e4066Sahrens 
2071fa9e4066Sahrens 	/*
2072fa9e4066Sahrens 	 * We're a zfs filesystem.
2073fa9e4066Sahrens 	 */
2074fa9e4066Sahrens 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
2075fa9e4066Sahrens 
2076a5be7ebbSmarks 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
2077fa9e4066Sahrens 
20789adfa60dSMatthew Ahrens 	statp->f_namemax = MAXNAMELEN - 1;
2079fa9e4066Sahrens 
2080fa9e4066Sahrens 	/*
2081fa9e4066Sahrens 	 * We have all of 32 characters to stuff a string here.
2082fa9e4066Sahrens 	 * Is there anything useful we could/should provide?
2083fa9e4066Sahrens 	 */
2084fa9e4066Sahrens 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
2085fa9e4066Sahrens 
2086f67950b2SNasf-Fan 	if (dmu_objset_projectquota_enabled(zfsvfs->z_os) &&
2087f67950b2SNasf-Fan 	    dmu_objset_projectquota_present(zfsvfs->z_os)) {
2088f67950b2SNasf-Fan 		znode_t *zp;
2089f67950b2SNasf-Fan 
2090f67950b2SNasf-Fan 		/*
2091f67950b2SNasf-Fan 		 * In ZoL, zfs_statvfs is passed a Linux dentry (directory
2092f67950b2SNasf-Fan 		 * entry), instead of a vfsp. The ZoL code uses the dentry
2093f67950b2SNasf-Fan 		 * to get the znode from the dentry's inode. This represents
2094f67950b2SNasf-Fan 		 * whatever filename was passed to the user-level statvfs
2095f67950b2SNasf-Fan 		 * syscall.
2096f67950b2SNasf-Fan 		 *
2097f67950b2SNasf-Fan 		 * We're using the VFS root znode here, so this represents a
2098f67950b2SNasf-Fan 		 * potential difference from ZoL.
2099f67950b2SNasf-Fan 		 */
2100f67950b2SNasf-Fan 		if (zfs_zget(zfsvfs, zfsvfs->z_root, &zp) == 0) {
2101f67950b2SNasf-Fan 			uint32_t bshift = ddi_fls(statp->f_bsize) - 1;
2102f67950b2SNasf-Fan 
2103f67950b2SNasf-Fan 			if (zp->z_pflags & ZFS_PROJINHERIT && zp->z_projid &&
2104f67950b2SNasf-Fan 			    zpl_is_valid_projid(zp->z_projid))
2105f67950b2SNasf-Fan 				err = zfs_statfs_project(zfsvfs, zp, statp,
2106f67950b2SNasf-Fan 				    bshift);
2107f67950b2SNasf-Fan 			VN_RELE(ZTOV(zp));
2108f67950b2SNasf-Fan 		}
2109f67950b2SNasf-Fan 	}
2110f67950b2SNasf-Fan 
2111fa9e4066Sahrens 	ZFS_EXIT(zfsvfs);
2112f67950b2SNasf-Fan 	return (err);
2113fa9e4066Sahrens }
2114fa9e4066Sahrens 
2115fa9e4066Sahrens static int
zfs_root(vfs_t * vfsp,vnode_t ** vpp)2116fa9e4066Sahrens zfs_root(vfs_t *vfsp, vnode_t **vpp)
2117fa9e4066Sahrens {
2118fa9e4066Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
211900bfaff9SPatrick Mooney 	struct vnode *vp;
2120fa9e4066Sahrens 	int error;
2121fa9e4066Sahrens 
2122fa9e4066Sahrens 	ZFS_ENTER(zfsvfs);
2123fa9e4066Sahrens 
212400bfaff9SPatrick Mooney 	vp = zfsvfs->z_rootdir;
212500bfaff9SPatrick Mooney 	if (vp != NULL) {
212600bfaff9SPatrick Mooney 		VN_HOLD(vp);
212700bfaff9SPatrick Mooney 		error = 0;
212800bfaff9SPatrick Mooney 	} else {
212900bfaff9SPatrick Mooney 		/* forced unmount */
213000bfaff9SPatrick Mooney 		error = EIO;
213100bfaff9SPatrick Mooney 	}
213200bfaff9SPatrick Mooney 	*vpp = vp;
2133fa9e4066Sahrens 
2134fa9e4066Sahrens 	ZFS_EXIT(zfsvfs);
2135fa9e4066Sahrens 	return (error);
213600bfaff9SPatrick Mooney 
2137fa9e4066Sahrens }
2138fa9e4066Sahrens 
2139f18faf3fSek /*
2140f18faf3fSek  * Teardown the zfsvfs::z_os.
2141f18faf3fSek  *
2142eb721827SAlek Pinchuk  * Note, if 'unmounting' is FALSE, we return with the 'z_teardown_lock'
2143f18faf3fSek  * and 'z_teardown_inactive_lock' held.
2144f18faf3fSek  */
2145f18faf3fSek static int
zfsvfs_teardown(zfsvfs_t * zfsvfs,boolean_t unmounting)2146f18faf3fSek zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
2147f18faf3fSek {
2148874395d5Smaybee 	znode_t	*zp;
2149f18faf3fSek 
2150c5832a53SAlek Pinchuk 	zfs_unlinked_drain_stop_wait(zfsvfs);
2151c5832a53SAlek Pinchuk 
2152c9030f6cSAlexander Motin 	rrm_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
2153f18faf3fSek 
2154f18faf3fSek 	if (!unmounting) {
2155f18faf3fSek 		/*
2156f18faf3fSek 		 * We purge the parent filesystem's vfsp as the parent
2157f18faf3fSek 		 * filesystem and all of its snapshots have their vnode's
2158f18faf3fSek 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
2159f18faf3fSek 		 * 'z_parent' is self referential for non-snapshots.
2160f18faf3fSek 		 */
2161f18faf3fSek 		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
2162f18faf3fSek 	}
2163f18faf3fSek 
2164f18faf3fSek 	/*
2165f18faf3fSek 	 * Close the zil. NB: Can't close the zil while zfs_inactive
2166f18faf3fSek 	 * threads are blocked as zil_close can call zfs_inactive.
2167f18faf3fSek 	 */
2168f18faf3fSek 	if (zfsvfs->z_log) {
2169f18faf3fSek 		zil_close(zfsvfs->z_log);
2170f18faf3fSek 		zfsvfs->z_log = NULL;
2171f18faf3fSek 	}
2172f18faf3fSek 
2173f18faf3fSek 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
2174f18faf3fSek 
2175f18faf3fSek 	/*
2176f18faf3fSek 	 * If we are not unmounting (ie: online recv) and someone already
2177f18faf3fSek 	 * unmounted this file system while we were doing the switcheroo,
2178f18faf3fSek 	 * or a reopen of z_os failed then just bail out now.
2179f18faf3fSek 	 */
2180f18faf3fSek 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
2181f18faf3fSek 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
2182c9030f6cSAlexander Motin 		rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2183be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
2184f18faf3fSek 	}
2185f18faf3fSek 
2186f18faf3fSek 	/*
2187f18faf3fSek 	 * At this point there are no vops active, and any new vops will
2188f18faf3fSek 	 * fail with EIO since we have z_teardown_lock for writer (only
2189f18faf3fSek 	 * relavent for forced unmount).
2190f18faf3fSek 	 *
2191f18faf3fSek 	 * Release all holds on dbufs.
2192f18faf3fSek 	 */
2193f18faf3fSek 	mutex_enter(&zfsvfs->z_znodes_lock);
2194874395d5Smaybee 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
2195874395d5Smaybee 	    zp = list_next(&zfsvfs->z_all_znodes, zp))
21960a586ceaSMark Shellenbaum 		if (zp->z_sa_hdl) {
2197874395d5Smaybee 			ASSERT(ZTOV(zp)->v_count > 0);
2198874395d5Smaybee 			zfs_znode_dmu_fini(zp);
2199f18faf3fSek 		}
2200f18faf3fSek 	mutex_exit(&zfsvfs->z_znodes_lock);
2201f18faf3fSek 
2202f18faf3fSek 	/*
2203f18faf3fSek 	 * If we are unmounting, set the unmounted flag and let new vops
2204f18faf3fSek 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
2205f18faf3fSek 	 * other vops will fail with EIO.
2206f18faf3fSek 	 */
2207f18faf3fSek 	if (unmounting) {
220800bfaff9SPatrick Mooney 		/*
220900bfaff9SPatrick Mooney 		 * Clear the cached root vnode now that we are unmounted.
221000bfaff9SPatrick Mooney 		 * Its release must be performed outside the teardown locks to
221100bfaff9SPatrick Mooney 		 * avoid recursive lock entry via zfs_inactive().
221200bfaff9SPatrick Mooney 		 */
221300bfaff9SPatrick Mooney 		vnode_t *vp = zfsvfs->z_rootdir;
221400bfaff9SPatrick Mooney 		zfsvfs->z_rootdir = NULL;
221500bfaff9SPatrick Mooney 
2216f18faf3fSek 		zfsvfs->z_unmounted = B_TRUE;
2217f18faf3fSek 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
2218eb721827SAlek Pinchuk 		rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
221900bfaff9SPatrick Mooney 
222000bfaff9SPatrick Mooney 		/* Drop the cached root vp now that it is safe */
222100bfaff9SPatrick Mooney 		VN_RELE(vp);
2222f18faf3fSek 	}
2223f18faf3fSek 
2224f18faf3fSek 	/*
2225f18faf3fSek 	 * z_os will be NULL if there was an error in attempting to reopen
2226f18faf3fSek 	 * zfsvfs, so just return as the properties had already been
2227f18faf3fSek 	 * unregistered and cached data had been evicted before.
2228f18faf3fSek 	 */
2229f18faf3fSek 	if (zfsvfs->z_os == NULL)
2230f18faf3fSek 		return (0);
2231f18faf3fSek 
2232f18faf3fSek 	/*
2233f18faf3fSek 	 * Unregister properties.
2234f18faf3fSek 	 */
2235f18faf3fSek 	zfs_unregister_callbacks(zfsvfs);
2236f18faf3fSek 
2237f18faf3fSek 	/*
2238f18faf3fSek 	 * Evict cached data
2239f18faf3fSek 	 */
22402e2c1355SMatthew Ahrens 	if (dsl_dataset_is_dirty(dmu_objset_ds(zfsvfs->z_os)) &&
22412e2c1355SMatthew Ahrens 	    !(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY))
22422e2c1355SMatthew Ahrens 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
22433b2aab18SMatthew Ahrens 	dmu_objset_evict_dbufs(zfsvfs->z_os);
2244f18faf3fSek 
2245f18faf3fSek 	return (0);
2246f18faf3fSek }
2247f18faf3fSek 
2248fa9e4066Sahrens /*ARGSUSED*/
2249fa9e4066Sahrens static int
zfs_umount(vfs_t * vfsp,int fflag,cred_t * cr)2250fa9e4066Sahrens zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
2251fa9e4066Sahrens {
2252fa9e4066Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
2253f18faf3fSek 	objset_t *os;
2254fa9e4066Sahrens 	int ret;
2255fa9e4066Sahrens 
2256ecd6cf80Smarks 	ret = secpolicy_fs_unmount(cr, vfsp);
2257ecd6cf80Smarks 	if (ret) {
225898679b56SMark Shellenbaum 		if (dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
225998679b56SMark Shellenbaum 		    ZFS_DELEG_PERM_MOUNT, cr))
2260ecd6cf80Smarks 			return (ret);
2261ecd6cf80Smarks 	}
2262033f9833Sek 
2263ed097989Sek 	/*
2264ed097989Sek 	 * We purge the parent filesystem's vfsp as the parent filesystem
2265ed097989Sek 	 * and all of its snapshots have their vnode's v_vfsp set to the
2266ed097989Sek 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
2267ed097989Sek 	 * referential for non-snapshots.
2268ed097989Sek 	 */
2269ed097989Sek 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
2270033f9833Sek 
2271fa9e4066Sahrens 	/*
2272fa9e4066Sahrens 	 * Unmount any snapshots mounted under .zfs before unmounting the
2273fa9e4066Sahrens 	 * dataset itself.
2274fa9e4066Sahrens 	 */
2275fa9e4066Sahrens 	if (zfsvfs->z_ctldir != NULL &&
2276ecd6cf80Smarks 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
2277fa9e4066Sahrens 		return (ret);
2278ecd6cf80Smarks 	}
2279fa9e4066Sahrens 
228091ebeef5Sahrens 	if (!(fflag & MS_FORCE)) {
2281fa9e4066Sahrens 		/*
228291ebeef5Sahrens 		 * Check the number of active vnodes in the file system.
228391ebeef5Sahrens 		 * Our count is maintained in the vfs structure, but the
228491ebeef5Sahrens 		 * number is off by 1 to indicate a hold on the vfs
228591ebeef5Sahrens 		 * structure itself.
2286fa9e4066Sahrens 		 */
2287b39b008fSAndy Fiddaman 		boolean_t draining;
2288b39b008fSAndy Fiddaman 		uint_t thresh = 1;
228900bfaff9SPatrick Mooney 		vnode_t *ctlvp, *rvp;
229000bfaff9SPatrick Mooney 
229100bfaff9SPatrick Mooney 		/*
229200bfaff9SPatrick Mooney 		 * The cached vnode for the root directory of the mount also
229300bfaff9SPatrick Mooney 		 * maintains a hold on the vfs structure.
229400bfaff9SPatrick Mooney 		 */
229500bfaff9SPatrick Mooney 		rvp = zfsvfs->z_rootdir;
229600bfaff9SPatrick Mooney 		thresh++;
2297b39b008fSAndy Fiddaman 
2298b39b008fSAndy Fiddaman 		/*
2299b39b008fSAndy Fiddaman 		 * The '.zfs' directory maintains a reference of its own, and
2300b39b008fSAndy Fiddaman 		 * any active references underneath are reflected in the vnode
2301b39b008fSAndy Fiddaman 		 * count. Allow one additional reference for it.
2302b39b008fSAndy Fiddaman 		 */
230300bfaff9SPatrick Mooney 		ctlvp = zfsvfs->z_ctldir;
230400bfaff9SPatrick Mooney 		if (ctlvp != NULL) {
2305b39b008fSAndy Fiddaman 			thresh++;
230600bfaff9SPatrick Mooney 		}
2307b39b008fSAndy Fiddaman 
2308b39b008fSAndy Fiddaman 		/*
2309b39b008fSAndy Fiddaman 		 * If it's running, the asynchronous unlinked drain task needs
2310b39b008fSAndy Fiddaman 		 * to be stopped before the number of active vnodes can be
2311b39b008fSAndy Fiddaman 		 * reliably checked.
2312b39b008fSAndy Fiddaman 		 */
2313b39b008fSAndy Fiddaman 		draining = zfsvfs->z_draining;
2314b39b008fSAndy Fiddaman 		if (draining)
2315b39b008fSAndy Fiddaman 			zfs_unlinked_drain_stop_wait(zfsvfs);
2316b39b008fSAndy Fiddaman 
231700bfaff9SPatrick Mooney 		if (vfsp->vfs_count > thresh || rvp->v_count > 1 ||
231800bfaff9SPatrick Mooney 		    (ctlvp != NULL && ctlvp->v_count > 1)) {
2319b39b008fSAndy Fiddaman 			if (draining) {
2320b39b008fSAndy Fiddaman 				/* If it was draining, restart the task */
2321b39b008fSAndy Fiddaman 				zfs_unlinked_drain(zfsvfs);
2322b39b008fSAndy Fiddaman 			}
2323b39b008fSAndy Fiddaman 			return (SET_ERROR(EBUSY));
2324fa9e4066Sahrens 		}
232591ebeef5Sahrens 	}
2326fa9e4066Sahrens 
232791ebeef5Sahrens 	vfsp->vfs_flag |= VFS_UNMOUNTED;
232891ebeef5Sahrens 
2329f18faf3fSek 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
2330f18faf3fSek 	os = zfsvfs->z_os;
233191ebeef5Sahrens 
233291ebeef5Sahrens 	/*
2333f18faf3fSek 	 * z_os will be NULL if there was an error in
2334f18faf3fSek 	 * attempting to reopen zfsvfs.
233591ebeef5Sahrens 	 */
2336f18faf3fSek 	if (os != NULL) {
2337f18faf3fSek 		/*
2338f18faf3fSek 		 * Unset the objset user_ptr.
2339f18faf3fSek 		 */
2340503ad85cSMatthew Ahrens 		mutex_enter(&os->os_user_ptr_lock);
2341f18faf3fSek 		dmu_objset_set_user(os, NULL);
2342503ad85cSMatthew Ahrens 		mutex_exit(&os->os_user_ptr_lock);
234391ebeef5Sahrens 
2344f18faf3fSek 		/*
2345745cd3c5Smaybee 		 * Finally release the objset
2346f18faf3fSek 		 */
2347eb633035STom Caputi 		dmu_objset_disown(os, B_TRUE, zfsvfs);
234891ebeef5Sahrens 	}
234991ebeef5Sahrens 
235091ebeef5Sahrens 	/*
235191ebeef5Sahrens 	 * We can now safely destroy the '.zfs' directory node.
235291ebeef5Sahrens 	 */
235391ebeef5Sahrens 	if (zfsvfs->z_ctldir != NULL)
235491ebeef5Sahrens 		zfsctl_destroy(zfsvfs);
2355fa9e4066Sahrens 
2356fa9e4066Sahrens 	return (0);
2357fa9e4066Sahrens }
2358fa9e4066Sahrens 
2359fa9e4066Sahrens static int
zfs_vget(vfs_t * vfsp,vnode_t ** vpp,fid_t * fidp)2360fa9e4066Sahrens zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
2361fa9e4066Sahrens {
2362fa9e4066Sahrens 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
2363fa9e4066Sahrens 	znode_t		*zp;
2364fa9e4066Sahrens 	uint64_t	object = 0;
2365fa9e4066Sahrens 	uint64_t	fid_gen = 0;
2366fa9e4066Sahrens 	uint64_t	gen_mask;
2367fa9e4066Sahrens 	uint64_t	zp_gen;
23688bf394f1STom Caputi 	int		i, err;
2369fa9e4066Sahrens 
2370fa9e4066Sahrens 	*vpp = NULL;
2371fa9e4066Sahrens 
2372fa9e4066Sahrens 	ZFS_ENTER(zfsvfs);
2373fa9e4066Sahrens 
2374fa9e4066Sahrens 	if (fidp->fid_len == LONG_FID_LEN) {
2375fa9e4066Sahrens 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
2376fa9e4066Sahrens 		uint64_t	objsetid = 0;
2377fa9e4066Sahrens 		uint64_t	setgen = 0;
2378fa9e4066Sahrens 
2379fa9e4066Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
2380fa9e4066Sahrens 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
2381fa9e4066Sahrens 
2382fa9e4066Sahrens 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
2383fa9e4066Sahrens 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
2384fa9e4066Sahrens 
2385fa9e4066Sahrens 		ZFS_EXIT(zfsvfs);
2386fa9e4066Sahrens 
2387fa9e4066Sahrens 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
2388fa9e4066Sahrens 		if (err)
2389be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
2390fa9e4066Sahrens 		ZFS_ENTER(zfsvfs);
2391fa9e4066Sahrens 	}
2392fa9e4066Sahrens 
2393fa9e4066Sahrens 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
2394fa9e4066Sahrens 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
2395fa9e4066Sahrens 
2396fa9e4066Sahrens 		for (i = 0; i < sizeof (zfid->zf_object); i++)
2397fa9e4066Sahrens 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
2398fa9e4066Sahrens 
2399fa9e4066Sahrens 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
2400fa9e4066Sahrens 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
2401fa9e4066Sahrens 	} else {
2402fa9e4066Sahrens 		ZFS_EXIT(zfsvfs);
2403be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2404fa9e4066Sahrens 	}
2405fa9e4066Sahrens 
2406fa9e4066Sahrens 	/* A zero fid_gen means we are in the .zfs control directories */
2407fa9e4066Sahrens 	if (fid_gen == 0 &&
2408fa9e4066Sahrens 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
2409fa9e4066Sahrens 		*vpp = zfsvfs->z_ctldir;
2410fa9e4066Sahrens 		ASSERT(*vpp != NULL);
2411fa9e4066Sahrens 		if (object == ZFSCTL_INO_SNAPDIR) {
2412fa9e4066Sahrens 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
2413da6c28aaSamw 			    0, NULL, NULL, NULL, NULL, NULL) == 0);
2414fa9e4066Sahrens 		} else {
2415fa9e4066Sahrens 			VN_HOLD(*vpp);
2416fa9e4066Sahrens 		}
2417fa9e4066Sahrens 		ZFS_EXIT(zfsvfs);
2418fa9e4066Sahrens 		return (0);
2419fa9e4066Sahrens 	}
2420fa9e4066Sahrens 
2421fa9e4066Sahrens 	gen_mask = -1ULL >> (64 - 8 * i);
2422fa9e4066Sahrens 
2423fa9e4066Sahrens 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
2424fa9e4066Sahrens 	if (err = zfs_zget(zfsvfs, object, &zp)) {
2425fa9e4066Sahrens 		ZFS_EXIT(zfsvfs);
2426fa9e4066Sahrens 		return (err);
2427fa9e4066Sahrens 	}
24280a586ceaSMark Shellenbaum 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
24290a586ceaSMark Shellenbaum 	    sizeof (uint64_t));
24300a586ceaSMark Shellenbaum 	zp_gen = zp_gen & gen_mask;
2431fa9e4066Sahrens 	if (zp_gen == 0)
2432fa9e4066Sahrens 		zp_gen = 1;
2433893a6d32Sahrens 	if (zp->z_unlinked || zp_gen != fid_gen) {
2434fa9e4066Sahrens 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
2435fa9e4066Sahrens 		VN_RELE(ZTOV(zp));
2436fa9e4066Sahrens 		ZFS_EXIT(zfsvfs);
2437be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2438fa9e4066Sahrens 	}
2439fa9e4066Sahrens 
2440fa9e4066Sahrens 	*vpp = ZTOV(zp);
2441fa9e4066Sahrens 	ZFS_EXIT(zfsvfs);
2442fa9e4066Sahrens 	return (0);
2443fa9e4066Sahrens }
2444fa9e4066Sahrens 
2445f18faf3fSek /*
2446f18faf3fSek  * Block out VOPs and close zfsvfs_t::z_os
2447f18faf3fSek  *
2448f18faf3fSek  * Note, if successful, then we return with the 'z_teardown_lock' and
244991948b51SKeith M Wesolowski  * 'z_teardown_inactive_lock' write held.  We leave ownership of the underlying
245091948b51SKeith M Wesolowski  * dataset and objset intact so that they can be atomically handed off during
245191948b51SKeith M Wesolowski  * a subsequent rollback or recv operation and the resume thereafter.
2452f18faf3fSek  */
2453f18faf3fSek int
zfs_suspend_fs(zfsvfs_t * zfsvfs)2454503ad85cSMatthew Ahrens zfs_suspend_fs(zfsvfs_t *zfsvfs)
2455f18faf3fSek {
2456f18faf3fSek 	int error;
2457f18faf3fSek 
2458f18faf3fSek 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
2459f18faf3fSek 		return (error);
2460f18faf3fSek 
2461f18faf3fSek 	return (0);
2462f18faf3fSek }
2463f18faf3fSek 
2464f18faf3fSek /*
246591948b51SKeith M Wesolowski  * Rebuild SA and release VOPs.  Note that ownership of the underlying dataset
246691948b51SKeith M Wesolowski  * is an invariant across any of the operations that can be performed while the
246791948b51SKeith M Wesolowski  * filesystem was suspended.  Whether it succeeded or failed, the preconditions
246891948b51SKeith M Wesolowski  * are the same: the relevant objset and associated dataset are owned by
246991948b51SKeith M Wesolowski  * zfsvfs, held, and long held on entry.
2470f18faf3fSek  */
2471f18faf3fSek int
zfs_resume_fs(zfsvfs_t * zfsvfs,dsl_dataset_t * ds)2472690041b9SAndriy Gapon zfs_resume_fs(zfsvfs_t *zfsvfs, dsl_dataset_t *ds)
2473f18faf3fSek {
247444bffe01SMark Shellenbaum 	int err;
247591948b51SKeith M Wesolowski 	znode_t *zp;
2476f18faf3fSek 
2477c9030f6cSAlexander Motin 	ASSERT(RRM_WRITE_HELD(&zfsvfs->z_teardown_lock));
2478f18faf3fSek 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
2479f18faf3fSek 
248091948b51SKeith M Wesolowski 	/*
2481690041b9SAndriy Gapon 	 * We already own this, so just update the objset_t, as the one we
2482690041b9SAndriy Gapon 	 * had before may have been evicted.
248391948b51SKeith M Wesolowski 	 */
24841fdcbd00SMatthew Ahrens 	objset_t *os;
2485690041b9SAndriy Gapon 	VERIFY3P(ds->ds_owner, ==, zfsvfs);
2486690041b9SAndriy Gapon 	VERIFY(dsl_dataset_long_held(ds));
2487690041b9SAndriy Gapon 	VERIFY0(dmu_objset_from_ds(ds, &os));
24880a586ceaSMark Shellenbaum 
24891fdcbd00SMatthew Ahrens 	err = zfsvfs_init(zfsvfs, os);
24901fdcbd00SMatthew Ahrens 	if (err != 0)
249191948b51SKeith M Wesolowski 		goto bail;
2492f18faf3fSek 
249391948b51SKeith M Wesolowski 	VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
2494f18faf3fSek 
249591948b51SKeith M Wesolowski 	zfs_set_fuid_feature(zfsvfs);
249644bffe01SMark Shellenbaum 
249791948b51SKeith M Wesolowski 	/*
249891948b51SKeith M Wesolowski 	 * Attempt to re-establish all the active znodes with
249991948b51SKeith M Wesolowski 	 * their dbufs.  If a zfs_rezget() fails, then we'll let
250091948b51SKeith M Wesolowski 	 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
250191948b51SKeith M Wesolowski 	 * when they try to use their znode.
250291948b51SKeith M Wesolowski 	 */
250391948b51SKeith M Wesolowski 	mutex_enter(&zfsvfs->z_znodes_lock);
250491948b51SKeith M Wesolowski 	for (zp = list_head(&zfsvfs->z_all_znodes); zp;
250591948b51SKeith M Wesolowski 	    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
250691948b51SKeith M Wesolowski 		(void) zfs_rezget(zp);
2507f18faf3fSek 	}
250891948b51SKeith M Wesolowski 	mutex_exit(&zfsvfs->z_znodes_lock);
2509f18faf3fSek 
2510c5832a53SAlek Pinchuk 	if (((zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) == 0) &&
2511c5832a53SAlek Pinchuk 	    !zfsvfs->z_unmounted) {
2512c5832a53SAlek Pinchuk 		/*
2513c5832a53SAlek Pinchuk 		 * zfs_suspend_fs() could have interrupted freeing
2514c5832a53SAlek Pinchuk 		 * of dnodes. We need to restart this freeing so
2515c5832a53SAlek Pinchuk 		 * that we don't "leak" the space.
2516c5832a53SAlek Pinchuk 		 */
2517c5832a53SAlek Pinchuk 		zfs_unlinked_drain(zfsvfs);
2518c5832a53SAlek Pinchuk 	}
2519c5832a53SAlek Pinchuk 
25200a586ceaSMark Shellenbaum bail:
2521f18faf3fSek 	/* release the VOPs */
2522f18faf3fSek 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
2523c9030f6cSAlexander Motin 	rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2524f18faf3fSek 
2525f18faf3fSek 	if (err) {
2526f18faf3fSek 		/*
252791948b51SKeith M Wesolowski 		 * Since we couldn't setup the sa framework, try to force
252891948b51SKeith M Wesolowski 		 * unmount this file system.
2529f18faf3fSek 		 */
2530f18faf3fSek 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
2531f18faf3fSek 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
2532f18faf3fSek 	}
2533f18faf3fSek 	return (err);
2534f18faf3fSek }
2535f18faf3fSek 
2536fa9e4066Sahrens static void
zfs_freevfs(vfs_t * vfsp)2537fa9e4066Sahrens zfs_freevfs(vfs_t *vfsp)
2538fa9e4066Sahrens {
2539fa9e4066Sahrens 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
2540142ae85dSChris Kirby 
2541142ae85dSChris Kirby 	/*
2542142ae85dSChris Kirby 	 * If this is a snapshot, we have an extra VFS_HOLD on our parent
2543f80ce222SChris Kirby 	 * from zfs_mount().  Release it here.  If we came through
2544f80ce222SChris Kirby 	 * zfs_mountroot() instead, we didn't grab an extra hold, so
2545f80ce222SChris Kirby 	 * skip the VFS_RELE for rootvfs.
2546142ae85dSChris Kirby 	 */
2547f80ce222SChris Kirby 	if (zfsvfs->z_issnap && (vfsp != rootvfs))
2548142ae85dSChris Kirby 		VFS_RELE(zfsvfs->z_parent->z_vfs);
2549142ae85dSChris Kirby 
255014843421SMatthew Ahrens 	zfsvfs_free(zfsvfs);
2551fa9e4066Sahrens 
25521a5e258fSJosef 'Jeff' Sipek 	atomic_dec_32(&zfs_active_fs_count);
2553fa9e4066Sahrens }
2554fa9e4066Sahrens 
2555fa9e4066Sahrens /*
2556fa9e4066Sahrens  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
2557fa9e4066Sahrens  * so we can't safely do any non-idempotent initialization here.
2558fa9e4066Sahrens  * Leave that to zfs_init() and zfs_fini(), which are called
2559fa9e4066Sahrens  * from the module's _init() and _fini() entry points.
2560fa9e4066Sahrens  */
2561fa9e4066Sahrens /*ARGSUSED*/
2562fa9e4066Sahrens static int
zfs_vfsinit(int fstype,char * name)2563fa9e4066Sahrens zfs_vfsinit(int fstype, char *name)
2564fa9e4066Sahrens {
2565fa9e4066Sahrens 	int error;
2566fa9e4066Sahrens 
2567fa9e4066Sahrens 	zfsfstype = fstype;
2568fa9e4066Sahrens 
2569fa9e4066Sahrens 	/*
2570fa9e4066Sahrens 	 * Setup vfsops and vnodeops tables.
2571fa9e4066Sahrens 	 */
2572fa9e4066Sahrens 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
2573fa9e4066Sahrens 	if (error != 0) {
2574fa9e4066Sahrens 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
2575fa9e4066Sahrens 	}
2576fa9e4066Sahrens 
2577fa9e4066Sahrens 	error = zfs_create_op_tables();
2578fa9e4066Sahrens 	if (error) {
2579fa9e4066Sahrens 		zfs_remove_op_tables();
2580fa9e4066Sahrens 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
2581fa9e4066Sahrens 		(void) vfs_freevfsops_by_type(zfsfstype);
2582fa9e4066Sahrens 		return (error);
2583fa9e4066Sahrens 	}
2584fa9e4066Sahrens 
2585fa9e4066Sahrens 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
2586fa9e4066Sahrens 
2587fa9e4066Sahrens 	/*
2588a0965f35Sbonwick 	 * Unique major number for all zfs mounts.
2589a0965f35Sbonwick 	 * If we run out of 32-bit minors, we'll getudev() another major.
2590fa9e4066Sahrens 	 */
2591a0965f35Sbonwick 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
2592a0965f35Sbonwick 	zfs_minor = ZFS_MIN_MINOR;
2593fa9e4066Sahrens 
2594fa9e4066Sahrens 	return (0);
2595fa9e4066Sahrens }
2596fa9e4066Sahrens 
2597fa9e4066Sahrens void
zfs_init(void)2598fa9e4066Sahrens zfs_init(void)
2599fa9e4066Sahrens {
2600fa9e4066Sahrens 	/*
2601fa9e4066Sahrens 	 * Initialize .zfs directory structures
2602fa9e4066Sahrens 	 */
2603fa9e4066Sahrens 	zfsctl_init();
2604fa9e4066Sahrens 
2605fa9e4066Sahrens 	/*
2606fa9e4066Sahrens 	 * Initialize znode cache, vnode ops, etc...
2607fa9e4066Sahrens 	 */
2608fa9e4066Sahrens 	zfs_znode_init();
260914843421SMatthew Ahrens 
261014843421SMatthew Ahrens 	dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
2611fa9e4066Sahrens }
2612fa9e4066Sahrens 
2613fa9e4066Sahrens void
zfs_fini(void)2614fa9e4066Sahrens zfs_fini(void)
2615fa9e4066Sahrens {
2616fa9e4066Sahrens 	zfsctl_fini();
2617fa9e4066Sahrens 	zfs_znode_fini();
2618fa9e4066Sahrens }
2619fa9e4066Sahrens 
2620fa9e4066Sahrens int
zfs_busy(void)2621fa9e4066Sahrens zfs_busy(void)
2622fa9e4066Sahrens {
2623fa9e4066Sahrens 	return (zfs_active_fs_count != 0);
2624fa9e4066Sahrens }
2625fa9e4066Sahrens 
2626e7437265Sahrens int
zfs_set_version(zfsvfs_t * zfsvfs,uint64_t newvers)262714843421SMatthew Ahrens zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
2628e7437265Sahrens {
2629e7437265Sahrens 	int error;
263014843421SMatthew Ahrens 	objset_t *os = zfsvfs->z_os;
2631e7437265Sahrens 	dmu_tx_t *tx;
2632e7437265Sahrens 
2633e7437265Sahrens 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
2634be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2635e7437265Sahrens 
263614843421SMatthew Ahrens 	if (newvers < zfsvfs->z_version)
2637be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2638e7437265Sahrens 
26390a586ceaSMark Shellenbaum 	if (zfs_spa_version_map(newvers) >
26400a586ceaSMark Shellenbaum 	    spa_version(dmu_objset_spa(zfsvfs->z_os)))
2641be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
26420a586ceaSMark Shellenbaum 
2643e7437265Sahrens 	tx = dmu_tx_create(os);
264414843421SMatthew Ahrens 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
26450a586ceaSMark Shellenbaum 	if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
26460a586ceaSMark Shellenbaum 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
26470a586ceaSMark Shellenbaum 		    ZFS_SA_ATTRS);
26480a586ceaSMark Shellenbaum 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
26490a586ceaSMark Shellenbaum 	}
2650e7437265Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
2651e7437265Sahrens 	if (error) {
2652e7437265Sahrens 		dmu_tx_abort(tx);
265314843421SMatthew Ahrens 		return (error);
265414843421SMatthew Ahrens 	}
26550a586ceaSMark Shellenbaum 
265614843421SMatthew Ahrens 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
265714843421SMatthew Ahrens 	    8, 1, &newvers, tx);
265814843421SMatthew Ahrens 
265914843421SMatthew Ahrens 	if (error) {
266014843421SMatthew Ahrens 		dmu_tx_commit(tx);
266114843421SMatthew Ahrens 		return (error);
2662e7437265Sahrens 	}
2663e7437265Sahrens 
26640a586ceaSMark Shellenbaum 	if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
26650a586ceaSMark Shellenbaum 		uint64_t sa_obj;
26660a586ceaSMark Shellenbaum 
26670a586ceaSMark Shellenbaum 		ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
26680a586ceaSMark Shellenbaum 		    SPA_VERSION_SA);
26690a586ceaSMark Shellenbaum 		sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
26700a586ceaSMark Shellenbaum 		    DMU_OT_NONE, 0, tx);
26710a586ceaSMark Shellenbaum 
26720a586ceaSMark Shellenbaum 		error = zap_add(os, MASTER_NODE_OBJ,
26730a586ceaSMark Shellenbaum 		    ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
2674fb09f5aaSMadhav Suresh 		ASSERT0(error);
26750a586ceaSMark Shellenbaum 
26760a586ceaSMark Shellenbaum 		VERIFY(0 == sa_set_sa_object(os, sa_obj));
26770a586ceaSMark Shellenbaum 		sa_register_update_callback(os, zfs_sa_upgrade);
26780a586ceaSMark Shellenbaum 	}
26790a586ceaSMark Shellenbaum 
26804445fffbSMatthew Ahrens 	spa_history_log_internal_ds(dmu_objset_ds(os), "upgrade", tx,
26814445fffbSMatthew Ahrens 	    "from %llu to %llu", zfsvfs->z_version, newvers);
268214843421SMatthew Ahrens 
2683e7437265Sahrens 	dmu_tx_commit(tx);
2684e7437265Sahrens 
268514843421SMatthew Ahrens 	zfsvfs->z_version = newvers;
2686adb52d92SMatthew Ahrens 	os->os_version = newvers;
268714843421SMatthew Ahrens 
268844bffe01SMark Shellenbaum 	zfs_set_fuid_feature(zfsvfs);
268914843421SMatthew Ahrens 
269014843421SMatthew Ahrens 	return (0);
2691e7437265Sahrens }
2692e7437265Sahrens 
2693de8267e0Stimh /*
2694de8267e0Stimh  * Read a property stored within the master node.
2695de8267e0Stimh  */
2696de8267e0Stimh int
zfs_get_zplprop(objset_t * os,zfs_prop_t prop,uint64_t * value)2697de8267e0Stimh zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
2698de8267e0Stimh {
2699adb52d92SMatthew Ahrens 	uint64_t *cached_copy = NULL;
2700de8267e0Stimh 
2701de8267e0Stimh 	/*
2702adb52d92SMatthew Ahrens 	 * Figure out where in the objset_t the cached copy would live, if it
2703adb52d92SMatthew Ahrens 	 * is available for the requested property.
2704de8267e0Stimh 	 */
2705adb52d92SMatthew Ahrens 	if (os != NULL) {
2706adb52d92SMatthew Ahrens 		switch (prop) {
2707adb52d92SMatthew Ahrens 		case ZFS_PROP_VERSION:
2708adb52d92SMatthew Ahrens 			cached_copy = &os->os_version;
2709adb52d92SMatthew Ahrens 			break;
2710adb52d92SMatthew Ahrens 		case ZFS_PROP_NORMALIZE:
2711adb52d92SMatthew Ahrens 			cached_copy = &os->os_normalization;
2712adb52d92SMatthew Ahrens 			break;
2713adb52d92SMatthew Ahrens 		case ZFS_PROP_UTF8ONLY:
2714adb52d92SMatthew Ahrens 			cached_copy = &os->os_utf8only;
2715adb52d92SMatthew Ahrens 			break;
2716adb52d92SMatthew Ahrens 		case ZFS_PROP_CASE:
2717adb52d92SMatthew Ahrens 			cached_copy = &os->os_casesensitivity;
2718adb52d92SMatthew Ahrens 			break;
2719adb52d92SMatthew Ahrens 		default:
2720adb52d92SMatthew Ahrens 			break;
2721adb52d92SMatthew Ahrens 		}
2722adb52d92SMatthew Ahrens 	}
2723adb52d92SMatthew Ahrens 	if (cached_copy != NULL && *cached_copy != OBJSET_PROP_UNINITIALIZED) {
2724adb52d92SMatthew Ahrens 		*value = *cached_copy;
2725adb52d92SMatthew Ahrens 		return (0);
2726adb52d92SMatthew Ahrens 	}
2727adb52d92SMatthew Ahrens 
2728adb52d92SMatthew Ahrens 	/*
2729adb52d92SMatthew Ahrens 	 * If the property wasn't cached, look up the file system's value for
2730adb52d92SMatthew Ahrens 	 * the property. For the version property, we look up a slightly
2731adb52d92SMatthew Ahrens 	 * different string.
2732adb52d92SMatthew Ahrens 	 */
2733adb52d92SMatthew Ahrens 	const char *pname;
2734adb52d92SMatthew Ahrens 	int error = ENOENT;
2735adb52d92SMatthew Ahrens 	if (prop == ZFS_PROP_VERSION) {
2736de8267e0Stimh 		pname = ZPL_VERSION_STR;
2737adb52d92SMatthew Ahrens 	} else {
2738de8267e0Stimh 		pname = zfs_prop_to_name(prop);
2739adb52d92SMatthew Ahrens 	}
2740de8267e0Stimh 
2741b127fe3cSAndriy Gapon 	if (os != NULL) {
2742b127fe3cSAndriy Gapon 		ASSERT3U(os->os_phys->os_type, ==, DMU_OST_ZFS);
27430a48a24eStimh 		error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
2744b127fe3cSAndriy Gapon 	}
2745de8267e0Stimh 
2746aa60ed0eSmaybee 	if (error == ENOENT) {
2747de8267e0Stimh 		/* No value set, use the default value */
2748de8267e0Stimh 		switch (prop) {
2749aa60ed0eSmaybee 		case ZFS_PROP_VERSION:
2750aa60ed0eSmaybee 			*value = ZPL_VERSION;
2751aa60ed0eSmaybee 			break;
2752de8267e0Stimh 		case ZFS_PROP_NORMALIZE:
2753de8267e0Stimh 		case ZFS_PROP_UTF8ONLY:
2754de8267e0Stimh 			*value = 0;
2755de8267e0Stimh 			break;
2756de8267e0Stimh 		case ZFS_PROP_CASE:
2757de8267e0Stimh 			*value = ZFS_CASE_SENSITIVE;
2758de8267e0Stimh 			break;
2759de8267e0Stimh 		default:
2760aa60ed0eSmaybee 			return (error);
2761de8267e0Stimh 		}
2762aa60ed0eSmaybee 		error = 0;
2763de8267e0Stimh 	}
2764adb52d92SMatthew Ahrens 
2765adb52d92SMatthew Ahrens 	/*
2766adb52d92SMatthew Ahrens 	 * If one of the methods for getting the property value above worked,
2767adb52d92SMatthew Ahrens 	 * copy it into the objset_t's cache.
2768adb52d92SMatthew Ahrens 	 */
2769adb52d92SMatthew Ahrens 	if (error == 0 && cached_copy != NULL) {
2770adb52d92SMatthew Ahrens 		*cached_copy = *value;
2771adb52d92SMatthew Ahrens 	}
2772adb52d92SMatthew Ahrens 
2773aa60ed0eSmaybee 	return (error);
2774de8267e0Stimh }
2775de8267e0Stimh 
2776eb721827SAlek Pinchuk /*
2777eb721827SAlek Pinchuk  * Return true if the coresponding vfs's unmounted flag is set.
2778eb721827SAlek Pinchuk  * Otherwise return false.
2779eb721827SAlek Pinchuk  * If this function returns true we know VFS unmount has been initiated.
2780eb721827SAlek Pinchuk  */
2781eb721827SAlek Pinchuk boolean_t
zfs_get_vfs_flag_unmounted(objset_t * os)2782eb721827SAlek Pinchuk zfs_get_vfs_flag_unmounted(objset_t *os)
2783eb721827SAlek Pinchuk {
2784eb721827SAlek Pinchuk 	zfsvfs_t *zfvp;
2785eb721827SAlek Pinchuk 	boolean_t unmounted = B_FALSE;
2786eb721827SAlek Pinchuk 
2787eb721827SAlek Pinchuk 	ASSERT(dmu_objset_type(os) == DMU_OST_ZFS);
2788eb721827SAlek Pinchuk 
2789eb721827SAlek Pinchuk 	mutex_enter(&os->os_user_ptr_lock);
2790eb721827SAlek Pinchuk 	zfvp = dmu_objset_get_user(os);
2791eb721827SAlek Pinchuk 	if (zfvp != NULL && zfvp->z_vfs != NULL &&
2792eb721827SAlek Pinchuk 	    (zfvp->z_vfs->vfs_flag & VFS_UNMOUNTED))
2793eb721827SAlek Pinchuk 		unmounted = B_TRUE;
2794eb721827SAlek Pinchuk 	mutex_exit(&os->os_user_ptr_lock);
2795eb721827SAlek Pinchuk 
2796eb721827SAlek Pinchuk 	return (unmounted);
2797eb721827SAlek Pinchuk }
2798eb721827SAlek Pinchuk 
2799fa9e4066Sahrens static vfsdef_t vfw = {
2800fa9e4066Sahrens 	VFSDEF_VERSION,
2801fa9e4066Sahrens 	MNTTYPE_ZFS,
2802fa9e4066Sahrens 	zfs_vfsinit,
2803da6c28aaSamw 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS|
28040fbb751dSJohn Levon 	    VSW_XID|VSW_ZMOUNT,
2805fa9e4066Sahrens 	&zfs_mntopts
2806fa9e4066Sahrens };
2807fa9e4066Sahrens 
2808fa9e4066Sahrens struct modlfs zfs_modlfs = {
2809e7437265Sahrens 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
2810fa9e4066Sahrens };
2811