xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision a39df08c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24  * Copyright (c) 2014 Integros [integros.com]
25  * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
26  * Copyright 2019 Joyent, Inc.
27  * Copyright 2020 Joshua M. Clulow <josh@sysmgr.org>
28  * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
29  */
30 
31 /* Portions Copyright 2010 Robert Milkowski */
32 
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/sysmacros.h>
37 #include <sys/kmem.h>
38 #include <sys/pathname.h>
39 #include <sys/vnode.h>
40 #include <sys/vfs.h>
41 #include <sys/vfs_opreg.h>
42 #include <sys/mntent.h>
43 #include <sys/mount.h>
44 #include <sys/cmn_err.h>
45 #include "fs/fs_subr.h"
46 #include <sys/zfs_znode.h>
47 #include <sys/zfs_dir.h>
48 #include <sys/zil.h>
49 #include <sys/fs/zfs.h>
50 #include <sys/dmu.h>
51 #include <sys/dsl_prop.h>
52 #include <sys/dsl_dataset.h>
53 #include <sys/dsl_deleg.h>
54 #include <sys/spa.h>
55 #include <sys/zap.h>
56 #include <sys/sa.h>
57 #include <sys/sa_impl.h>
58 #include <sys/varargs.h>
59 #include <sys/policy.h>
60 #include <sys/atomic.h>
61 #include <sys/mkdev.h>
62 #include <sys/modctl.h>
63 #include <sys/refstr.h>
64 #include <sys/zfs_ioctl.h>
65 #include <sys/zfs_ctldir.h>
66 #include <sys/zfs_fuid.h>
67 #include <sys/bootconf.h>
68 #include <sys/ddi.h>
69 #include <sys/sunddi.h>
70 #include <sys/dnlc.h>
71 #include <sys/dmu_objset.h>
72 #include <sys/spa_boot.h>
73 #include <sys/vdev_impl.h>
74 #include "zfs_comutil.h"
75 
76 int zfsfstype;
77 vfsops_t *zfs_vfsops = NULL;
78 static major_t zfs_major;
79 static minor_t zfs_minor;
80 static kmutex_t	zfs_dev_mtx;
81 
82 extern int sys_shutdown;
83 
84 static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
85 static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr);
86 static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
87 static int zfs_root(vfs_t *vfsp, vnode_t **vpp);
88 static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp);
89 static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp);
90 static void zfs_freevfs(vfs_t *vfsp);
91 
92 static const fs_operation_def_t zfs_vfsops_template[] = {
93 	VFSNAME_MOUNT,		{ .vfs_mount = zfs_mount },
94 	VFSNAME_MOUNTROOT,	{ .vfs_mountroot = zfs_mountroot },
95 	VFSNAME_UNMOUNT,	{ .vfs_unmount = zfs_umount },
96 	VFSNAME_ROOT,		{ .vfs_root = zfs_root },
97 	VFSNAME_STATVFS,	{ .vfs_statvfs = zfs_statvfs },
98 	VFSNAME_SYNC,		{ .vfs_sync = zfs_sync },
99 	VFSNAME_VGET,		{ .vfs_vget = zfs_vget },
100 	VFSNAME_FREEVFS,	{ .vfs_freevfs = zfs_freevfs },
101 	NULL,			NULL
102 };
103 
104 /*
105  * We need to keep a count of active fs's.
106  * This is necessary to prevent our module
107  * from being unloaded after a umount -f
108  */
109 static uint32_t	zfs_active_fs_count = 0;
110 
111 static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
112 static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
113 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
114 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
115 
116 /*
117  * MO_DEFAULT is not used since the default value is determined
118  * by the equivalent property.
119  */
120 static mntopt_t mntopts[] = {
121 	{ MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL },
122 	{ MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL },
123 	{ MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL },
124 	{ MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
125 };
126 
127 static mntopts_t zfs_mntopts = {
128 	sizeof (mntopts) / sizeof (mntopt_t),
129 	mntopts
130 };
131 
132 /*ARGSUSED*/
133 int
134 zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
135 {
136 	/*
137 	 * Data integrity is job one.  We don't want a compromised kernel
138 	 * writing to the storage pool, so we never sync during panic.
139 	 */
140 	if (panicstr)
141 		return (0);
142 
143 	/*
144 	 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
145 	 * to sync metadata, which they would otherwise cache indefinitely.
146 	 * Semantically, the only requirement is that the sync be initiated.
147 	 * The DMU syncs out txgs frequently, so there's nothing to do.
148 	 */
149 	if (flag & SYNC_ATTR)
150 		return (0);
151 
152 	if (vfsp != NULL) {
153 		/*
154 		 * Sync a specific filesystem.
155 		 */
156 		zfsvfs_t *zfsvfs = vfsp->vfs_data;
157 		dsl_pool_t *dp;
158 
159 		ZFS_ENTER(zfsvfs);
160 		dp = dmu_objset_pool(zfsvfs->z_os);
161 
162 		/*
163 		 * If the system is shutting down, then skip any
164 		 * filesystems which may exist on a suspended pool.
165 		 */
166 		if (sys_shutdown && spa_suspended(dp->dp_spa)) {
167 			ZFS_EXIT(zfsvfs);
168 			return (0);
169 		}
170 
171 		if (zfsvfs->z_log != NULL)
172 			zil_commit(zfsvfs->z_log, 0);
173 
174 		ZFS_EXIT(zfsvfs);
175 	} else {
176 		/*
177 		 * Sync all ZFS filesystems.  This is what happens when you
178 		 * run sync(1M).  Unlike other filesystems, ZFS honors the
179 		 * request by waiting for all pools to commit all dirty data.
180 		 */
181 		spa_sync_allpools();
182 	}
183 
184 	return (0);
185 }
186 
187 static int
188 zfs_create_unique_device(dev_t *dev)
189 {
190 	major_t new_major;
191 
192 	do {
193 		ASSERT3U(zfs_minor, <=, MAXMIN32);
194 		minor_t start = zfs_minor;
195 		do {
196 			mutex_enter(&zfs_dev_mtx);
197 			if (zfs_minor >= MAXMIN32) {
198 				/*
199 				 * If we're still using the real major
200 				 * keep out of /dev/zfs and /dev/zvol minor
201 				 * number space.  If we're using a getudev()'ed
202 				 * major number, we can use all of its minors.
203 				 */
204 				if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
205 					zfs_minor = ZFS_MIN_MINOR;
206 				else
207 					zfs_minor = 0;
208 			} else {
209 				zfs_minor++;
210 			}
211 			*dev = makedevice(zfs_major, zfs_minor);
212 			mutex_exit(&zfs_dev_mtx);
213 		} while (vfs_devismounted(*dev) && zfs_minor != start);
214 		if (zfs_minor == start) {
215 			/*
216 			 * We are using all ~262,000 minor numbers for the
217 			 * current major number.  Create a new major number.
218 			 */
219 			if ((new_major = getudev()) == (major_t)-1) {
220 				cmn_err(CE_WARN,
221 				    "zfs_mount: Can't get unique major "
222 				    "device number.");
223 				return (-1);
224 			}
225 			mutex_enter(&zfs_dev_mtx);
226 			zfs_major = new_major;
227 			zfs_minor = 0;
228 
229 			mutex_exit(&zfs_dev_mtx);
230 		} else {
231 			break;
232 		}
233 		/* CONSTANTCONDITION */
234 	} while (1);
235 
236 	return (0);
237 }
238 
239 static void
240 atime_changed_cb(void *arg, uint64_t newval)
241 {
242 	zfsvfs_t *zfsvfs = arg;
243 
244 	if (newval == TRUE) {
245 		zfsvfs->z_atime = TRUE;
246 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
247 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
248 	} else {
249 		zfsvfs->z_atime = FALSE;
250 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
251 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
252 	}
253 }
254 
255 static void
256 xattr_changed_cb(void *arg, uint64_t newval)
257 {
258 	zfsvfs_t *zfsvfs = arg;
259 
260 	if (newval == TRUE) {
261 		/* XXX locking on vfs_flag? */
262 		zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
263 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
264 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
265 	} else {
266 		/* XXX locking on vfs_flag? */
267 		zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
268 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
269 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
270 	}
271 }
272 
273 static void
274 blksz_changed_cb(void *arg, uint64_t newval)
275 {
276 	zfsvfs_t *zfsvfs = arg;
277 	ASSERT3U(newval, <=, spa_maxblocksize(dmu_objset_spa(zfsvfs->z_os)));
278 	ASSERT3U(newval, >=, SPA_MINBLOCKSIZE);
279 	ASSERT(ISP2(newval));
280 
281 	zfsvfs->z_max_blksz = newval;
282 	zfsvfs->z_vfs->vfs_bsize = newval;
283 }
284 
285 static void
286 readonly_changed_cb(void *arg, uint64_t newval)
287 {
288 	zfsvfs_t *zfsvfs = arg;
289 
290 	if (newval) {
291 		/* XXX locking on vfs_flag? */
292 		zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
293 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
294 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
295 	} else {
296 		/* XXX locking on vfs_flag? */
297 		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
298 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
299 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
300 	}
301 }
302 
303 static void
304 devices_changed_cb(void *arg, uint64_t newval)
305 {
306 	zfsvfs_t *zfsvfs = arg;
307 
308 	if (newval == FALSE) {
309 		zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
310 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
311 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
312 	} else {
313 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
314 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
315 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
316 	}
317 }
318 
319 static void
320 setuid_changed_cb(void *arg, uint64_t newval)
321 {
322 	zfsvfs_t *zfsvfs = arg;
323 
324 	if (newval == FALSE) {
325 		zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
326 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
327 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
328 	} else {
329 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
330 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
331 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
332 	}
333 }
334 
335 static void
336 exec_changed_cb(void *arg, uint64_t newval)
337 {
338 	zfsvfs_t *zfsvfs = arg;
339 
340 	if (newval == FALSE) {
341 		zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
342 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
343 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
344 	} else {
345 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
346 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
347 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
348 	}
349 }
350 
351 /*
352  * The nbmand mount option can be changed at mount time.
353  * We can't allow it to be toggled on live file systems or incorrect
354  * behavior may be seen from cifs clients
355  *
356  * This property isn't registered via dsl_prop_register(), but this callback
357  * will be called when a file system is first mounted
358  */
359 static void
360 nbmand_changed_cb(void *arg, uint64_t newval)
361 {
362 	zfsvfs_t *zfsvfs = arg;
363 	if (newval == FALSE) {
364 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
365 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
366 	} else {
367 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
368 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
369 	}
370 }
371 
372 static void
373 snapdir_changed_cb(void *arg, uint64_t newval)
374 {
375 	zfsvfs_t *zfsvfs = arg;
376 
377 	zfsvfs->z_show_ctldir = newval;
378 }
379 
380 static void
381 vscan_changed_cb(void *arg, uint64_t newval)
382 {
383 	zfsvfs_t *zfsvfs = arg;
384 
385 	zfsvfs->z_vscan = newval;
386 }
387 
388 static void
389 acl_mode_changed_cb(void *arg, uint64_t newval)
390 {
391 	zfsvfs_t *zfsvfs = arg;
392 
393 	zfsvfs->z_acl_mode = newval;
394 }
395 
396 static void
397 acl_inherit_changed_cb(void *arg, uint64_t newval)
398 {
399 	zfsvfs_t *zfsvfs = arg;
400 
401 	zfsvfs->z_acl_inherit = newval;
402 }
403 
404 static int
405 zfs_register_callbacks(vfs_t *vfsp)
406 {
407 	struct dsl_dataset *ds = NULL;
408 	objset_t *os = NULL;
409 	zfsvfs_t *zfsvfs = NULL;
410 	uint64_t nbmand;
411 	boolean_t readonly = B_FALSE;
412 	boolean_t do_readonly = B_FALSE;
413 	boolean_t setuid = B_FALSE;
414 	boolean_t do_setuid = B_FALSE;
415 	boolean_t exec = B_FALSE;
416 	boolean_t do_exec = B_FALSE;
417 	boolean_t devices = B_FALSE;
418 	boolean_t do_devices = B_FALSE;
419 	boolean_t xattr = B_FALSE;
420 	boolean_t do_xattr = B_FALSE;
421 	boolean_t atime = B_FALSE;
422 	boolean_t do_atime = B_FALSE;
423 	int error = 0;
424 
425 	ASSERT(vfsp);
426 	zfsvfs = vfsp->vfs_data;
427 	ASSERT(zfsvfs);
428 	os = zfsvfs->z_os;
429 
430 	/*
431 	 * The act of registering our callbacks will destroy any mount
432 	 * options we may have.  In order to enable temporary overrides
433 	 * of mount options, we stash away the current values and
434 	 * restore them after we register the callbacks.
435 	 */
436 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL) ||
437 	    !spa_writeable(dmu_objset_spa(os))) {
438 		readonly = B_TRUE;
439 		do_readonly = B_TRUE;
440 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
441 		readonly = B_FALSE;
442 		do_readonly = B_TRUE;
443 	}
444 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
445 		devices = B_FALSE;
446 		setuid = B_FALSE;
447 		do_devices = B_TRUE;
448 		do_setuid = B_TRUE;
449 	} else {
450 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
451 			devices = B_FALSE;
452 			do_devices = B_TRUE;
453 		} else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
454 			devices = B_TRUE;
455 			do_devices = B_TRUE;
456 		}
457 
458 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
459 			setuid = B_FALSE;
460 			do_setuid = B_TRUE;
461 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
462 			setuid = B_TRUE;
463 			do_setuid = B_TRUE;
464 		}
465 	}
466 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
467 		exec = B_FALSE;
468 		do_exec = B_TRUE;
469 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
470 		exec = B_TRUE;
471 		do_exec = B_TRUE;
472 	}
473 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
474 		xattr = B_FALSE;
475 		do_xattr = B_TRUE;
476 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
477 		xattr = B_TRUE;
478 		do_xattr = B_TRUE;
479 	}
480 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
481 		atime = B_FALSE;
482 		do_atime = B_TRUE;
483 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
484 		atime = B_TRUE;
485 		do_atime = B_TRUE;
486 	}
487 
488 	/*
489 	 * nbmand is a special property.  It can only be changed at
490 	 * mount time.
491 	 *
492 	 * This is weird, but it is documented to only be changeable
493 	 * at mount time.
494 	 */
495 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
496 		nbmand = B_FALSE;
497 	} else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
498 		nbmand = B_TRUE;
499 	} else {
500 		char osname[ZFS_MAX_DATASET_NAME_LEN];
501 
502 		dmu_objset_name(os, osname);
503 		if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
504 		    NULL)) {
505 			return (error);
506 		}
507 	}
508 
509 	/*
510 	 * Register property callbacks.
511 	 *
512 	 * It would probably be fine to just check for i/o error from
513 	 * the first prop_register(), but I guess I like to go
514 	 * overboard...
515 	 */
516 	ds = dmu_objset_ds(os);
517 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
518 	error = dsl_prop_register(ds,
519 	    zfs_prop_to_name(ZFS_PROP_ATIME), atime_changed_cb, zfsvfs);
520 	error = error ? error : dsl_prop_register(ds,
521 	    zfs_prop_to_name(ZFS_PROP_XATTR), xattr_changed_cb, zfsvfs);
522 	error = error ? error : dsl_prop_register(ds,
523 	    zfs_prop_to_name(ZFS_PROP_RECORDSIZE), blksz_changed_cb, zfsvfs);
524 	error = error ? error : dsl_prop_register(ds,
525 	    zfs_prop_to_name(ZFS_PROP_READONLY), readonly_changed_cb, zfsvfs);
526 	error = error ? error : dsl_prop_register(ds,
527 	    zfs_prop_to_name(ZFS_PROP_DEVICES), devices_changed_cb, zfsvfs);
528 	error = error ? error : dsl_prop_register(ds,
529 	    zfs_prop_to_name(ZFS_PROP_SETUID), setuid_changed_cb, zfsvfs);
530 	error = error ? error : dsl_prop_register(ds,
531 	    zfs_prop_to_name(ZFS_PROP_EXEC), exec_changed_cb, zfsvfs);
532 	error = error ? error : dsl_prop_register(ds,
533 	    zfs_prop_to_name(ZFS_PROP_SNAPDIR), snapdir_changed_cb, zfsvfs);
534 	error = error ? error : dsl_prop_register(ds,
535 	    zfs_prop_to_name(ZFS_PROP_ACLMODE), acl_mode_changed_cb, zfsvfs);
536 	error = error ? error : dsl_prop_register(ds,
537 	    zfs_prop_to_name(ZFS_PROP_ACLINHERIT), acl_inherit_changed_cb,
538 	    zfsvfs);
539 	error = error ? error : dsl_prop_register(ds,
540 	    zfs_prop_to_name(ZFS_PROP_VSCAN), vscan_changed_cb, zfsvfs);
541 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
542 	if (error)
543 		goto unregister;
544 
545 	/*
546 	 * Invoke our callbacks to restore temporary mount options.
547 	 */
548 	if (do_readonly)
549 		readonly_changed_cb(zfsvfs, readonly);
550 	if (do_setuid)
551 		setuid_changed_cb(zfsvfs, setuid);
552 	if (do_exec)
553 		exec_changed_cb(zfsvfs, exec);
554 	if (do_devices)
555 		devices_changed_cb(zfsvfs, devices);
556 	if (do_xattr)
557 		xattr_changed_cb(zfsvfs, xattr);
558 	if (do_atime)
559 		atime_changed_cb(zfsvfs, atime);
560 
561 	nbmand_changed_cb(zfsvfs, nbmand);
562 
563 	return (0);
564 
565 unregister:
566 	dsl_prop_unregister_all(ds, zfsvfs);
567 	return (error);
568 }
569 
570 static int
571 zfs_space_delta_cb(dmu_object_type_t bonustype, void *data,
572     uint64_t *userp, uint64_t *groupp, uint64_t *projectp)
573 {
574 	sa_hdr_phys_t sa;
575 	sa_hdr_phys_t *sap = data;
576 	uint64_t flags;
577 	int hdrsize;
578 	boolean_t swap = B_FALSE;
579 
580 	/*
581 	 * Is it a valid type of object to track?
582 	 */
583 	if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
584 		return (SET_ERROR(ENOENT));
585 
586 	/*
587 	 * If we have a NULL data pointer
588 	 * then assume the id's aren't changing and
589 	 * return EEXIST to the dmu to let it know to
590 	 * use the same ids
591 	 */
592 	if (data == NULL)
593 		return (SET_ERROR(EEXIST));
594 
595 	if (bonustype == DMU_OT_ZNODE) {
596 		znode_phys_t *znp = data;
597 		*userp = znp->zp_uid;
598 		*groupp = znp->zp_gid;
599 		*projectp = ZFS_DEFAULT_PROJID;
600 		return (0);
601 	}
602 
603 	if (sap->sa_magic == 0) {
604 		/*
605 		 * This should only happen for newly created files
606 		 * that haven't had the znode data filled in yet.
607 		 */
608 		*userp = 0;
609 		*groupp = 0;
610 		*projectp = ZFS_DEFAULT_PROJID;
611 		return (0);
612 	}
613 
614 	sa = *sap;
615 	if (sa.sa_magic == BSWAP_32(SA_MAGIC)) {
616 		sa.sa_magic = SA_MAGIC;
617 		sa.sa_layout_info = BSWAP_16(sa.sa_layout_info);
618 		swap = B_TRUE;
619 	} else {
620 		VERIFY3U(sa.sa_magic, ==, SA_MAGIC);
621 	}
622 
623 	hdrsize = sa_hdrsize(&sa);
624 	VERIFY3U(hdrsize, >=, sizeof (sa_hdr_phys_t));
625 
626 	*userp = *((uint64_t *)((uintptr_t)data + hdrsize + SA_UID_OFFSET));
627 	*groupp = *((uint64_t *)((uintptr_t)data + hdrsize + SA_GID_OFFSET));
628 	flags = *((uint64_t *)((uintptr_t)data + hdrsize + SA_FLAGS_OFFSET));
629 	if (swap)
630 		flags = BSWAP_64(flags);
631 
632 	if (flags & ZFS_PROJID)
633 		*projectp = *((uint64_t *)((uintptr_t)data + hdrsize +
634 		    SA_PROJID_OFFSET));
635 	else
636 		*projectp = ZFS_DEFAULT_PROJID;
637 
638 	if (swap) {
639 		*userp = BSWAP_64(*userp);
640 		*groupp = BSWAP_64(*groupp);
641 		*projectp = BSWAP_64(*projectp);
642 	}
643 	return (0);
644 }
645 
646 static void
647 fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr,
648     char *domainbuf, int buflen, uid_t *ridp)
649 {
650 	uint64_t fuid;
651 	const char *domain;
652 
653 	fuid = zfs_strtonum(fuidstr, NULL);
654 
655 	domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid));
656 	if (domain)
657 		(void) strlcpy(domainbuf, domain, buflen);
658 	else
659 		domainbuf[0] = '\0';
660 	*ridp = FUID_RID(fuid);
661 }
662 
663 static uint64_t
664 zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type)
665 {
666 	switch (type) {
667 	case ZFS_PROP_USERUSED:
668 	case ZFS_PROP_USEROBJUSED:
669 		return (DMU_USERUSED_OBJECT);
670 	case ZFS_PROP_GROUPUSED:
671 	case ZFS_PROP_GROUPOBJUSED:
672 		return (DMU_GROUPUSED_OBJECT);
673 	case ZFS_PROP_PROJECTUSED:
674 	case ZFS_PROP_PROJECTOBJUSED:
675 		return (DMU_PROJECTUSED_OBJECT);
676 	case ZFS_PROP_USERQUOTA:
677 		return (zfsvfs->z_userquota_obj);
678 	case ZFS_PROP_GROUPQUOTA:
679 		return (zfsvfs->z_groupquota_obj);
680 	case ZFS_PROP_USEROBJQUOTA:
681 		return (zfsvfs->z_userobjquota_obj);
682 	case ZFS_PROP_GROUPOBJQUOTA:
683 		return (zfsvfs->z_groupobjquota_obj);
684 	case ZFS_PROP_PROJECTQUOTA:
685 		return (zfsvfs->z_projectquota_obj);
686 	case ZFS_PROP_PROJECTOBJQUOTA:
687 		return (zfsvfs->z_projectobjquota_obj);
688 	default:
689 		return (ZFS_NO_OBJECT);
690 	}
691 }
692 
693 int
694 zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
695     uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
696 {
697 	int error;
698 	zap_cursor_t zc;
699 	zap_attribute_t za;
700 	zfs_useracct_t *buf = vbuf;
701 	uint64_t obj;
702 	int offset = 0;
703 
704 	if (!dmu_objset_userspace_present(zfsvfs->z_os))
705 		return (SET_ERROR(ENOTSUP));
706 
707 	if ((type == ZFS_PROP_PROJECTQUOTA || type == ZFS_PROP_PROJECTUSED ||
708 	    type == ZFS_PROP_PROJECTOBJQUOTA ||
709 	    type == ZFS_PROP_PROJECTOBJUSED) &&
710 	    !dmu_objset_projectquota_present(zfsvfs->z_os))
711 		return (SET_ERROR(ENOTSUP));
712 
713 	if ((type == ZFS_PROP_USEROBJUSED || type == ZFS_PROP_GROUPOBJUSED ||
714 	    type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA ||
715 	    type == ZFS_PROP_PROJECTOBJUSED ||
716 	    type == ZFS_PROP_PROJECTOBJQUOTA) &&
717 	    !dmu_objset_userobjspace_present(zfsvfs->z_os))
718 		return (SET_ERROR(ENOTSUP));
719 
720 	obj = zfs_userquota_prop_to_obj(zfsvfs, type);
721 	if (obj == ZFS_NO_OBJECT) {
722 		*bufsizep = 0;
723 		return (0);
724 	}
725 
726 	if (type == ZFS_PROP_USEROBJUSED || type == ZFS_PROP_GROUPOBJUSED ||
727 	    type == ZFS_PROP_PROJECTOBJUSED)
728 		offset = DMU_OBJACCT_PREFIX_LEN;
729 
730 	for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
731 	    (error = zap_cursor_retrieve(&zc, &za)) == 0;
732 	    zap_cursor_advance(&zc)) {
733 		if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
734 		    *bufsizep)
735 			break;
736 
737 		/*
738 		 * skip object quota (with zap name prefix DMU_OBJACCT_PREFIX)
739 		 * when dealing with block quota and vice versa.
740 		 */
741 		if ((offset > 0) != (strncmp(za.za_name, DMU_OBJACCT_PREFIX,
742 		    DMU_OBJACCT_PREFIX_LEN) == 0))
743 			continue;
744 
745 		fuidstr_to_sid(zfsvfs, za.za_name + offset,
746 		    buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
747 
748 		buf->zu_space = za.za_first_integer;
749 		buf++;
750 	}
751 	if (error == ENOENT)
752 		error = 0;
753 
754 	ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
755 	*bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
756 	*cookiep = zap_cursor_serialize(&zc);
757 	zap_cursor_fini(&zc);
758 	return (error);
759 }
760 
761 /*
762  * buf must be big enough (eg, 32 bytes)
763  */
764 static int
765 id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
766     char *buf, boolean_t addok)
767 {
768 	uint64_t fuid;
769 	int domainid = 0;
770 
771 	if (domain && domain[0]) {
772 		domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
773 		if (domainid == -1)
774 			return (SET_ERROR(ENOENT));
775 	}
776 	fuid = FUID_ENCODE(domainid, rid);
777 	(void) sprintf(buf, "%llx", (longlong_t)fuid);
778 	return (0);
779 }
780 
781 int
782 zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
783     const char *domain, uint64_t rid, uint64_t *valp)
784 {
785 	char buf[20 + DMU_OBJACCT_PREFIX_LEN];
786 	int offset = 0;
787 	int err;
788 	uint64_t obj;
789 
790 	*valp = 0;
791 
792 	if (!dmu_objset_userspace_present(zfsvfs->z_os))
793 		return (SET_ERROR(ENOTSUP));
794 
795 	if ((type == ZFS_PROP_USEROBJUSED || type == ZFS_PROP_GROUPOBJUSED ||
796 	    type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA ||
797 	    type == ZFS_PROP_PROJECTOBJUSED ||
798 	    type == ZFS_PROP_PROJECTOBJQUOTA) &&
799 	    !dmu_objset_userobjspace_present(zfsvfs->z_os))
800 		return (SET_ERROR(ENOTSUP));
801 
802 	if (type == ZFS_PROP_PROJECTQUOTA || type == ZFS_PROP_PROJECTUSED ||
803 	    type == ZFS_PROP_PROJECTOBJQUOTA ||
804 	    type == ZFS_PROP_PROJECTOBJUSED) {
805 		if (!dmu_objset_projectquota_present(zfsvfs->z_os))
806 			return (SET_ERROR(ENOTSUP));
807 		if (!zpl_is_valid_projid(rid))
808 			return (SET_ERROR(EINVAL));
809 	}
810 
811 	obj = zfs_userquota_prop_to_obj(zfsvfs, type);
812 	if (obj == ZFS_NO_OBJECT)
813 		return (0);
814 
815 	if (type == ZFS_PROP_USEROBJUSED || type == ZFS_PROP_GROUPOBJUSED ||
816 	    type == ZFS_PROP_PROJECTOBJUSED) {
817 		strncpy(buf, DMU_OBJACCT_PREFIX, DMU_OBJACCT_PREFIX_LEN);
818 		offset = DMU_OBJACCT_PREFIX_LEN;
819 	}
820 
821 	err = id_to_fuidstr(zfsvfs, domain, rid, buf + offset, B_FALSE);
822 	if (err)
823 		return (err);
824 
825 	err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
826 	if (err == ENOENT)
827 		err = 0;
828 	return (err);
829 }
830 
831 int
832 zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
833     const char *domain, uint64_t rid, uint64_t quota)
834 {
835 	char buf[32];
836 	int err;
837 	dmu_tx_t *tx;
838 	uint64_t *objp;
839 	boolean_t fuid_dirtied;
840 
841 	if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
842 		return (SET_ERROR(ENOTSUP));
843 
844 	switch (type) {
845 	case ZFS_PROP_USERQUOTA:
846 		objp = &zfsvfs->z_userquota_obj;
847 		break;
848 	case ZFS_PROP_GROUPQUOTA:
849 		objp = &zfsvfs->z_groupquota_obj;
850 		break;
851 	case ZFS_PROP_USEROBJQUOTA:
852 		objp = &zfsvfs->z_userobjquota_obj;
853 		break;
854 	case ZFS_PROP_GROUPOBJQUOTA:
855 		objp = &zfsvfs->z_groupobjquota_obj;
856 		break;
857 	case ZFS_PROP_PROJECTQUOTA:
858 		if (!dmu_objset_projectquota_enabled(zfsvfs->z_os))
859 			return (SET_ERROR(ENOTSUP));
860 		if (!zpl_is_valid_projid(rid))
861 			return (SET_ERROR(EINVAL));
862 
863 		objp = &zfsvfs->z_projectquota_obj;
864 		break;
865 	case ZFS_PROP_PROJECTOBJQUOTA:
866 		if (!dmu_objset_projectquota_enabled(zfsvfs->z_os))
867 			return (SET_ERROR(ENOTSUP));
868 		if (!zpl_is_valid_projid(rid))
869 			return (SET_ERROR(EINVAL));
870 
871 		objp = &zfsvfs->z_projectobjquota_obj;
872 		break;
873 	default:
874 		return (SET_ERROR(EINVAL));
875 	}
876 
877 	err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE);
878 	if (err)
879 		return (err);
880 	fuid_dirtied = zfsvfs->z_fuid_dirty;
881 
882 	tx = dmu_tx_create(zfsvfs->z_os);
883 	dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
884 	if (*objp == 0) {
885 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
886 		    zfs_userquota_prop_prefixes[type]);
887 	}
888 	if (fuid_dirtied)
889 		zfs_fuid_txhold(zfsvfs, tx);
890 	err = dmu_tx_assign(tx, TXG_WAIT);
891 	if (err) {
892 		dmu_tx_abort(tx);
893 		return (err);
894 	}
895 
896 	mutex_enter(&zfsvfs->z_lock);
897 	if (*objp == 0) {
898 		*objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
899 		    DMU_OT_NONE, 0, tx);
900 		VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
901 		    zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
902 	}
903 	mutex_exit(&zfsvfs->z_lock);
904 
905 	if (quota == 0) {
906 		err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
907 		if (err == ENOENT)
908 			err = 0;
909 	} else {
910 		err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, &quota, tx);
911 	}
912 	ASSERT(err == 0);
913 	if (fuid_dirtied)
914 		zfs_fuid_sync(zfsvfs, tx);
915 	dmu_tx_commit(tx);
916 	return (err);
917 }
918 
919 boolean_t
920 zfs_id_overobjquota(zfsvfs_t *zfsvfs, uint64_t usedobj, uint64_t id)
921 {
922 	char buf[20 + DMU_OBJACCT_PREFIX_LEN];
923 	uint64_t used, quota, quotaobj;
924 	int err;
925 
926 	if (!dmu_objset_userobjspace_present(zfsvfs->z_os)) {
927 		if (dmu_objset_userobjspace_upgradable(zfsvfs->z_os)) {
928 			dsl_pool_config_enter(
929 			    dmu_objset_pool(zfsvfs->z_os), FTAG);
930 			dmu_objset_id_quota_upgrade(zfsvfs->z_os);
931 			dsl_pool_config_exit(
932 			    dmu_objset_pool(zfsvfs->z_os), FTAG);
933 		}
934 		return (B_FALSE);
935 	}
936 
937 	if (usedobj == DMU_PROJECTUSED_OBJECT) {
938 		if (!dmu_objset_projectquota_present(zfsvfs->z_os)) {
939 			if (dmu_objset_projectquota_upgradable(zfsvfs->z_os)) {
940 				dsl_pool_config_enter(
941 				    dmu_objset_pool(zfsvfs->z_os), FTAG);
942 				dmu_objset_id_quota_upgrade(zfsvfs->z_os);
943 				dsl_pool_config_exit(
944 				    dmu_objset_pool(zfsvfs->z_os), FTAG);
945 			}
946 			return (B_FALSE);
947 		}
948 		quotaobj = zfsvfs->z_projectobjquota_obj;
949 	} else if (usedobj == DMU_USERUSED_OBJECT) {
950 		quotaobj = zfsvfs->z_userobjquota_obj;
951 	} else if (usedobj == DMU_GROUPUSED_OBJECT) {
952 		quotaobj = zfsvfs->z_groupobjquota_obj;
953 	} else {
954 		return (B_FALSE);
955 	}
956 	if (quotaobj == 0 || zfsvfs->z_replay)
957 		return (B_FALSE);
958 
959 	(void) sprintf(buf, "%llx", (longlong_t)id);
960 	err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
961 	if (err != 0)
962 		return (B_FALSE);
963 
964 	(void) sprintf(buf, DMU_OBJACCT_PREFIX "%llx", (longlong_t)id);
965 	err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
966 	if (err != 0)
967 		return (B_FALSE);
968 	return (used >= quota);
969 }
970 
971 boolean_t
972 zfs_id_overblockquota(zfsvfs_t *zfsvfs, uint64_t usedobj, uint64_t id)
973 {
974 	char buf[20];
975 	uint64_t used, quota, quotaobj;
976 	int err;
977 
978 	if (usedobj == DMU_PROJECTUSED_OBJECT) {
979 		if (!dmu_objset_projectquota_present(zfsvfs->z_os)) {
980 			if (dmu_objset_projectquota_upgradable(zfsvfs->z_os)) {
981 				dsl_pool_config_enter(
982 				    dmu_objset_pool(zfsvfs->z_os), FTAG);
983 				dmu_objset_id_quota_upgrade(zfsvfs->z_os);
984 				dsl_pool_config_exit(
985 				    dmu_objset_pool(zfsvfs->z_os), FTAG);
986 			}
987 			return (B_FALSE);
988 		}
989 		quotaobj = zfsvfs->z_projectquota_obj;
990 	} else if (usedobj == DMU_USERUSED_OBJECT) {
991 		quotaobj = zfsvfs->z_userquota_obj;
992 	} else if (usedobj == DMU_GROUPUSED_OBJECT) {
993 		quotaobj = zfsvfs->z_groupquota_obj;
994 	} else {
995 		return (B_FALSE);
996 	}
997 	if (quotaobj == 0 || zfsvfs->z_replay)
998 		return (B_FALSE);
999 
1000 	(void) sprintf(buf, "%llx", (longlong_t)id);
1001 	err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
1002 	if (err != 0)
1003 		return (B_FALSE);
1004 
1005 	err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
1006 	if (err != 0)
1007 		return (B_FALSE);
1008 	return (used >= quota);
1009 }
1010 
1011 boolean_t
1012 zfs_id_overquota(zfsvfs_t *zfsvfs, uint64_t usedobj, uint64_t id)
1013 {
1014 	return (zfs_id_overblockquota(zfsvfs, usedobj, id) ||
1015 	    zfs_id_overobjquota(zfsvfs, usedobj, id));
1016 }
1017 
1018 /*
1019  * Associate this zfsvfs with the given objset, which must be owned.
1020  * This will cache a bunch of on-disk state from the objset in the
1021  * zfsvfs.
1022  */
1023 static int
1024 zfsvfs_init(zfsvfs_t *zfsvfs, objset_t *os)
1025 {
1026 	int error;
1027 	uint64_t val;
1028 
1029 	zfsvfs->z_max_blksz = SPA_OLD_MAXBLOCKSIZE;
1030 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
1031 	zfsvfs->z_os = os;
1032 
1033 	error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
1034 	if (error != 0)
1035 		return (error);
1036 	if (zfsvfs->z_version >
1037 	    zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
1038 		(void) printf("Can't mount a version %lld file system "
1039 		    "on a version %lld pool\n. Pool must be upgraded to mount "
1040 		    "this file system.", (u_longlong_t)zfsvfs->z_version,
1041 		    (u_longlong_t)spa_version(dmu_objset_spa(os)));
1042 		return (SET_ERROR(ENOTSUP));
1043 	}
1044 	error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &val);
1045 	if (error != 0)
1046 		return (error);
1047 	zfsvfs->z_norm = (int)val;
1048 
1049 	error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &val);
1050 	if (error != 0)
1051 		return (error);
1052 	zfsvfs->z_utf8 = (val != 0);
1053 
1054 	error = zfs_get_zplprop(os, ZFS_PROP_CASE, &val);
1055 	if (error != 0)
1056 		return (error);
1057 	zfsvfs->z_case = (uint_t)val;
1058 
1059 	/*
1060 	 * Fold case on file systems that are always or sometimes case
1061 	 * insensitive.
1062 	 */
1063 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
1064 	    zfsvfs->z_case == ZFS_CASE_MIXED)
1065 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
1066 
1067 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1068 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1069 
1070 	uint64_t sa_obj = 0;
1071 	if (zfsvfs->z_use_sa) {
1072 		/* should either have both of these objects or none */
1073 		error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
1074 		    &sa_obj);
1075 		if (error != 0)
1076 			return (error);
1077 	}
1078 
1079 	error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1080 	    &zfsvfs->z_attr_table);
1081 	if (error != 0)
1082 		return (error);
1083 
1084 	if (zfsvfs->z_version >= ZPL_VERSION_SA)
1085 		sa_register_update_callback(os, zfs_sa_upgrade);
1086 
1087 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
1088 	    &zfsvfs->z_root);
1089 	if (error != 0)
1090 		return (error);
1091 	ASSERT(zfsvfs->z_root != 0);
1092 
1093 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
1094 	    &zfsvfs->z_unlinkedobj);
1095 	if (error != 0)
1096 		return (error);
1097 
1098 	error = zap_lookup(os, MASTER_NODE_OBJ,
1099 	    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
1100 	    8, 1, &zfsvfs->z_userquota_obj);
1101 	if (error == ENOENT)
1102 		zfsvfs->z_userquota_obj = 0;
1103 	else if (error != 0)
1104 		return (error);
1105 
1106 	error = zap_lookup(os, MASTER_NODE_OBJ,
1107 	    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
1108 	    8, 1, &zfsvfs->z_groupquota_obj);
1109 	if (error == ENOENT)
1110 		zfsvfs->z_groupquota_obj = 0;
1111 	else if (error != 0)
1112 		return (error);
1113 
1114 	error = zap_lookup(os, MASTER_NODE_OBJ,
1115 	    zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA],
1116 	    8, 1, &zfsvfs->z_projectquota_obj);
1117 	if (error == ENOENT)
1118 		zfsvfs->z_projectquota_obj = 0;
1119 	else if (error != 0)
1120 		return (error);
1121 
1122 	error = zap_lookup(os, MASTER_NODE_OBJ,
1123 	    zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA],
1124 	    8, 1, &zfsvfs->z_userobjquota_obj);
1125 	if (error == ENOENT)
1126 		zfsvfs->z_userobjquota_obj = 0;
1127 	else if (error != 0)
1128 		return (error);
1129 
1130 	error = zap_lookup(os, MASTER_NODE_OBJ,
1131 	    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA],
1132 	    8, 1, &zfsvfs->z_groupobjquota_obj);
1133 	if (error == ENOENT)
1134 		zfsvfs->z_groupobjquota_obj = 0;
1135 	else if (error != 0)
1136 		return (error);
1137 
1138 	error = zap_lookup(os, MASTER_NODE_OBJ,
1139 	    zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTOBJQUOTA],
1140 	    8, 1, &zfsvfs->z_projectobjquota_obj);
1141 	if (error == ENOENT)
1142 		zfsvfs->z_projectobjquota_obj = 0;
1143 	else if (error != 0)
1144 		return (error);
1145 
1146 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
1147 	    &zfsvfs->z_fuid_obj);
1148 	if (error == ENOENT)
1149 		zfsvfs->z_fuid_obj = 0;
1150 	else if (error != 0)
1151 		return (error);
1152 
1153 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
1154 	    &zfsvfs->z_shares_dir);
1155 	if (error == ENOENT)
1156 		zfsvfs->z_shares_dir = 0;
1157 	else if (error != 0)
1158 		return (error);
1159 
1160 	return (0);
1161 }
1162 
1163 int
1164 zfsvfs_create(const char *osname, boolean_t readonly, zfsvfs_t **zfvp)
1165 {
1166 	objset_t *os;
1167 	zfsvfs_t *zfsvfs;
1168 	int error;
1169 	boolean_t ro = (readonly || (strchr(osname, '@') != NULL));
1170 
1171 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
1172 
1173 	error = dmu_objset_own(osname, DMU_OST_ZFS, ro, B_TRUE, zfsvfs, &os);
1174 	if (error != 0) {
1175 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
1176 		return (error);
1177 	}
1178 
1179 	error = zfsvfs_create_impl(zfvp, zfsvfs, os);
1180 	if (error != 0) {
1181 		dmu_objset_disown(os, B_TRUE, zfsvfs);
1182 	}
1183 	return (error);
1184 }
1185 
1186 
1187 int
1188 zfsvfs_create_impl(zfsvfs_t **zfvp, zfsvfs_t *zfsvfs, objset_t *os)
1189 {
1190 	int error;
1191 
1192 	zfsvfs->z_vfs = NULL;
1193 	zfsvfs->z_parent = zfsvfs;
1194 
1195 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1196 	mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
1197 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
1198 	    offsetof(znode_t, z_link_node));
1199 	rrm_init(&zfsvfs->z_teardown_lock, B_FALSE);
1200 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
1201 	rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
1202 	for (int i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1203 		mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1204 
1205 	error = zfsvfs_init(zfsvfs, os);
1206 	if (error != 0) {
1207 		*zfvp = NULL;
1208 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
1209 		return (error);
1210 	}
1211 
1212 	zfsvfs->z_drain_task = TASKQID_INVALID;
1213 	zfsvfs->z_draining = B_FALSE;
1214 	zfsvfs->z_drain_cancel = B_TRUE;
1215 
1216 	*zfvp = zfsvfs;
1217 	return (0);
1218 }
1219 
1220 static int
1221 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
1222 {
1223 	int error;
1224 
1225 	error = zfs_register_callbacks(zfsvfs->z_vfs);
1226 	if (error)
1227 		return (error);
1228 
1229 	zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
1230 
1231 	/*
1232 	 * If we are not mounting (ie: online recv), then we don't
1233 	 * have to worry about replaying the log as we blocked all
1234 	 * operations out since we closed the ZIL.
1235 	 */
1236 	if (mounting) {
1237 		boolean_t readonly;
1238 
1239 		/*
1240 		 * During replay we remove the read only flag to
1241 		 * allow replays to succeed.
1242 		 */
1243 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
1244 		if (readonly != 0) {
1245 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
1246 		} else {
1247 			zfs_unlinked_drain(zfsvfs);
1248 		}
1249 
1250 		/*
1251 		 * Parse and replay the intent log.
1252 		 *
1253 		 * Because of ziltest, this must be done after
1254 		 * zfs_unlinked_drain().  (Further note: ziltest
1255 		 * doesn't use readonly mounts, where
1256 		 * zfs_unlinked_drain() isn't called.)  This is because
1257 		 * ziltest causes spa_sync() to think it's committed,
1258 		 * but actually it is not, so the intent log contains
1259 		 * many txg's worth of changes.
1260 		 *
1261 		 * In particular, if object N is in the unlinked set in
1262 		 * the last txg to actually sync, then it could be
1263 		 * actually freed in a later txg and then reallocated
1264 		 * in a yet later txg.  This would write a "create
1265 		 * object N" record to the intent log.  Normally, this
1266 		 * would be fine because the spa_sync() would have
1267 		 * written out the fact that object N is free, before
1268 		 * we could write the "create object N" intent log
1269 		 * record.
1270 		 *
1271 		 * But when we are in ziltest mode, we advance the "open
1272 		 * txg" without actually spa_sync()-ing the changes to
1273 		 * disk.  So we would see that object N is still
1274 		 * allocated and in the unlinked set, and there is an
1275 		 * intent log record saying to allocate it.
1276 		 */
1277 		if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
1278 			if (zil_replay_disable) {
1279 				zil_destroy(zfsvfs->z_log, B_FALSE);
1280 			} else {
1281 				zfsvfs->z_replay = B_TRUE;
1282 				zil_replay(zfsvfs->z_os, zfsvfs,
1283 				    zfs_replay_vector);
1284 				zfsvfs->z_replay = B_FALSE;
1285 			}
1286 		}
1287 
1288 		/* restore readonly bit */
1289 		if (readonly != 0)
1290 			zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
1291 	}
1292 
1293 	/*
1294 	 * Set the objset user_ptr to track its zfsvfs.
1295 	 */
1296 	mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1297 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1298 	mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1299 
1300 	return (0);
1301 }
1302 
1303 void
1304 zfsvfs_free(zfsvfs_t *zfsvfs)
1305 {
1306 	int i;
1307 	extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
1308 
1309 	/*
1310 	 * This is a barrier to prevent the filesystem from going away in
1311 	 * zfs_znode_move() until we can safely ensure that the filesystem is
1312 	 * not unmounted. We consider the filesystem valid before the barrier
1313 	 * and invalid after the barrier.
1314 	 */
1315 	rw_enter(&zfsvfs_lock, RW_READER);
1316 	rw_exit(&zfsvfs_lock);
1317 
1318 	zfs_fuid_destroy(zfsvfs);
1319 
1320 	mutex_destroy(&zfsvfs->z_znodes_lock);
1321 	mutex_destroy(&zfsvfs->z_lock);
1322 	list_destroy(&zfsvfs->z_all_znodes);
1323 	rrm_destroy(&zfsvfs->z_teardown_lock);
1324 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
1325 	rw_destroy(&zfsvfs->z_fuid_lock);
1326 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1327 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1328 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1329 }
1330 
1331 static void
1332 zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
1333 {
1334 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1335 	if (zfsvfs->z_vfs) {
1336 		if (zfsvfs->z_use_fuids) {
1337 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1338 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1339 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1340 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1341 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1342 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1343 		} else {
1344 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1345 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1346 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1347 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1348 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1349 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1350 		}
1351 	}
1352 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1353 }
1354 
1355 static int
1356 zfs_domount(vfs_t *vfsp, char *osname)
1357 {
1358 	dev_t mount_dev;
1359 	uint64_t recordsize, fsid_guid;
1360 	int error = 0;
1361 	zfsvfs_t *zfsvfs;
1362 	boolean_t readonly = vfsp->vfs_flag & VFS_RDONLY ? B_TRUE : B_FALSE;
1363 
1364 	ASSERT(vfsp);
1365 	ASSERT(osname);
1366 
1367 	error = zfsvfs_create(osname, readonly, &zfsvfs);
1368 	if (error)
1369 		return (error);
1370 	zfsvfs->z_vfs = vfsp;
1371 
1372 	/* Initialize the generic filesystem structure. */
1373 	vfsp->vfs_bcount = 0;
1374 	vfsp->vfs_data = NULL;
1375 
1376 	if (zfs_create_unique_device(&mount_dev) == -1) {
1377 		error = SET_ERROR(ENODEV);
1378 		goto out;
1379 	}
1380 	ASSERT(vfs_devismounted(mount_dev) == 0);
1381 
1382 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
1383 	    NULL))
1384 		goto out;
1385 
1386 	vfsp->vfs_dev = mount_dev;
1387 	vfsp->vfs_fstype = zfsfstype;
1388 	vfsp->vfs_bsize = recordsize;
1389 	vfsp->vfs_flag |= VFS_NOTRUNC;
1390 	vfsp->vfs_data = zfsvfs;
1391 
1392 	/*
1393 	 * The fsid is 64 bits, composed of an 8-bit fs type, which
1394 	 * separates our fsid from any other filesystem types, and a
1395 	 * 56-bit objset unique ID.  The objset unique ID is unique to
1396 	 * all objsets open on this system, provided by unique_create().
1397 	 * The 8-bit fs type must be put in the low bits of fsid[1]
1398 	 * because that's where other Solaris filesystems put it.
1399 	 */
1400 	fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
1401 	ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
1402 	vfsp->vfs_fsid.val[0] = fsid_guid;
1403 	vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
1404 	    zfsfstype & 0xFF;
1405 
1406 	/*
1407 	 * Set features for file system.
1408 	 */
1409 	zfs_set_fuid_feature(zfsvfs);
1410 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
1411 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1412 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1413 		vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
1414 	} else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
1415 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1416 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1417 	}
1418 	vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
1419 
1420 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1421 		uint64_t pval;
1422 
1423 		atime_changed_cb(zfsvfs, B_FALSE);
1424 		readonly_changed_cb(zfsvfs, B_TRUE);
1425 		if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
1426 			goto out;
1427 		xattr_changed_cb(zfsvfs, pval);
1428 		zfsvfs->z_issnap = B_TRUE;
1429 		zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1430 
1431 		mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1432 		dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1433 		mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1434 	} else {
1435 		error = zfsvfs_setup(zfsvfs, B_TRUE);
1436 	}
1437 
1438 	if (!zfsvfs->z_issnap)
1439 		zfsctl_create(zfsvfs);
1440 out:
1441 	if (error) {
1442 		dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
1443 		zfsvfs_free(zfsvfs);
1444 	} else {
1445 		atomic_inc_32(&zfs_active_fs_count);
1446 	}
1447 
1448 	return (error);
1449 }
1450 
1451 void
1452 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
1453 {
1454 	objset_t *os = zfsvfs->z_os;
1455 
1456 	if (!dmu_objset_is_snapshot(os))
1457 		dsl_prop_unregister_all(dmu_objset_ds(os), zfsvfs);
1458 }
1459 
1460 /*
1461  * Convert a decimal digit string to a uint64_t integer.
1462  */
1463 static int
1464 str_to_uint64(char *str, uint64_t *objnum)
1465 {
1466 	uint64_t num = 0;
1467 
1468 	while (*str) {
1469 		if (*str < '0' || *str > '9')
1470 			return (SET_ERROR(EINVAL));
1471 
1472 		num = num*10 + *str++ - '0';
1473 	}
1474 
1475 	*objnum = num;
1476 	return (0);
1477 }
1478 
1479 /*
1480  * The boot path passed from the boot loader is in the form of
1481  * "rootpool-name/root-filesystem-object-number'. Convert this
1482  * string to a dataset name: "rootpool-name/root-filesystem-name".
1483  */
1484 static int
1485 zfs_parse_bootfs(char *bpath, char *outpath)
1486 {
1487 	char *slashp;
1488 	uint64_t objnum;
1489 	int error;
1490 
1491 	if (*bpath == 0 || *bpath == '/')
1492 		return (SET_ERROR(EINVAL));
1493 
1494 	(void) strcpy(outpath, bpath);
1495 
1496 	slashp = strchr(bpath, '/');
1497 
1498 	/* if no '/', just return the pool name */
1499 	if (slashp == NULL) {
1500 		return (0);
1501 	}
1502 
1503 	/* if not a number, just return the root dataset name */
1504 	if (str_to_uint64(slashp+1, &objnum)) {
1505 		return (0);
1506 	}
1507 
1508 	*slashp = '\0';
1509 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
1510 	*slashp = '/';
1511 
1512 	return (error);
1513 }
1514 
1515 /*
1516  * Check that the hex label string is appropriate for the dataset being
1517  * mounted into the global_zone proper.
1518  *
1519  * Return an error if the hex label string is not default or
1520  * admin_low/admin_high.  For admin_low labels, the corresponding
1521  * dataset must be readonly.
1522  */
1523 int
1524 zfs_check_global_label(const char *dsname, const char *hexsl)
1525 {
1526 	if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1527 		return (0);
1528 	if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
1529 		return (0);
1530 	if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
1531 		/* must be readonly */
1532 		uint64_t rdonly;
1533 
1534 		if (dsl_prop_get_integer(dsname,
1535 		    zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
1536 			return (SET_ERROR(EACCES));
1537 		return (rdonly ? 0 : EACCES);
1538 	}
1539 	return (SET_ERROR(EACCES));
1540 }
1541 
1542 static int
1543 zfs_statfs_project(zfsvfs_t *zfsvfs, znode_t *zp, struct statvfs64 *statp,
1544     uint32_t bshift)
1545 {
1546 	char buf[20 + DMU_OBJACCT_PREFIX_LEN];
1547 	uint64_t offset = DMU_OBJACCT_PREFIX_LEN;
1548 	uint64_t quota;
1549 	uint64_t used;
1550 	int err;
1551 
1552 	strlcpy(buf, DMU_OBJACCT_PREFIX, DMU_OBJACCT_PREFIX_LEN + 1);
1553 	err = id_to_fuidstr(zfsvfs, NULL, zp->z_projid, buf + offset, B_FALSE);
1554 	if (err)
1555 		return (err);
1556 
1557 	if (zfsvfs->z_projectquota_obj == 0)
1558 		goto objs;
1559 
1560 	err = zap_lookup(zfsvfs->z_os, zfsvfs->z_projectquota_obj,
1561 	    buf + offset, 8, 1, &quota);
1562 	if (err == ENOENT)
1563 		goto objs;
1564 	else if (err)
1565 		return (err);
1566 
1567 	err = zap_lookup(zfsvfs->z_os, DMU_PROJECTUSED_OBJECT,
1568 	    buf + offset, 8, 1, &used);
1569 	if (unlikely(err == ENOENT)) {
1570 		uint32_t blksize;
1571 		u_longlong_t nblocks;
1572 
1573 		/*
1574 		 * Quota accounting is async, so it is possible race case.
1575 		 * There is at least one object with the given project ID.
1576 		 */
1577 		sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
1578 		if (unlikely(zp->z_blksz == 0))
1579 			blksize = zfsvfs->z_max_blksz;
1580 
1581 		used = blksize * nblocks;
1582 	} else if (err) {
1583 		return (err);
1584 	}
1585 
1586 	statp->f_blocks = quota >> bshift;
1587 	statp->f_bfree = (quota > used) ? ((quota - used) >> bshift) : 0;
1588 	statp->f_bavail = statp->f_bfree;
1589 
1590 objs:
1591 	if (zfsvfs->z_projectobjquota_obj == 0)
1592 		return (0);
1593 
1594 	err = zap_lookup(zfsvfs->z_os, zfsvfs->z_projectobjquota_obj,
1595 	    buf + offset, 8, 1, &quota);
1596 	if (err == ENOENT)
1597 		return (0);
1598 	else if (err)
1599 		return (err);
1600 
1601 	err = zap_lookup(zfsvfs->z_os, DMU_PROJECTUSED_OBJECT,
1602 	    buf, 8, 1, &used);
1603 	if (unlikely(err == ENOENT)) {
1604 		/*
1605 		 * Quota accounting is async, so it is possible race case.
1606 		 * There is at least one object with the given project ID.
1607 		 */
1608 		used = 1;
1609 	} else if (err) {
1610 		return (err);
1611 	}
1612 
1613 	statp->f_files = quota;
1614 	statp->f_ffree = (quota > used) ? (quota - used) : 0;
1615 
1616 	return (0);
1617 }
1618 
1619 /*
1620  * Determine whether the mount is allowed according to MAC check.
1621  * by comparing (where appropriate) label of the dataset against
1622  * the label of the zone being mounted into.  If the dataset has
1623  * no label, create one.
1624  *
1625  * Returns 0 if access allowed, error otherwise (e.g. EACCES)
1626  */
1627 static int
1628 zfs_mount_label_policy(vfs_t *vfsp, char *osname)
1629 {
1630 	int		error, retv;
1631 	zone_t		*mntzone = NULL;
1632 	ts_label_t	*mnt_tsl;
1633 	bslabel_t	*mnt_sl;
1634 	bslabel_t	ds_sl;
1635 	char		ds_hexsl[MAXNAMELEN];
1636 
1637 	retv = EACCES;				/* assume the worst */
1638 
1639 	/*
1640 	 * Start by getting the dataset label if it exists.
1641 	 */
1642 	error = dsl_prop_get(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1643 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
1644 	if (error)
1645 		return (SET_ERROR(EACCES));
1646 
1647 	/*
1648 	 * If labeling is NOT enabled, then disallow the mount of datasets
1649 	 * which have a non-default label already.  No other label checks
1650 	 * are needed.
1651 	 */
1652 	if (!is_system_labeled()) {
1653 		if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1654 			return (0);
1655 		return (SET_ERROR(EACCES));
1656 	}
1657 
1658 	/*
1659 	 * Get the label of the mountpoint.  If mounting into the global
1660 	 * zone (i.e. mountpoint is not within an active zone and the
1661 	 * zoned property is off), the label must be default or
1662 	 * admin_low/admin_high only; no other checks are needed.
1663 	 */
1664 	mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE);
1665 	if (mntzone->zone_id == GLOBAL_ZONEID) {
1666 		uint64_t zoned;
1667 
1668 		zone_rele(mntzone);
1669 
1670 		if (dsl_prop_get_integer(osname,
1671 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
1672 			return (SET_ERROR(EACCES));
1673 		if (!zoned)
1674 			return (zfs_check_global_label(osname, ds_hexsl));
1675 		else
1676 			/*
1677 			 * This is the case of a zone dataset being mounted
1678 			 * initially, before the zone has been fully created;
1679 			 * allow this mount into global zone.
1680 			 */
1681 			return (0);
1682 	}
1683 
1684 	mnt_tsl = mntzone->zone_slabel;
1685 	ASSERT(mnt_tsl != NULL);
1686 	label_hold(mnt_tsl);
1687 	mnt_sl = label2bslabel(mnt_tsl);
1688 
1689 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) {
1690 		/*
1691 		 * The dataset doesn't have a real label, so fabricate one.
1692 		 */
1693 		char *str = NULL;
1694 
1695 		if (l_to_str_internal(mnt_sl, &str) == 0 &&
1696 		    dsl_prop_set_string(osname,
1697 		    zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1698 		    ZPROP_SRC_LOCAL, str) == 0)
1699 			retv = 0;
1700 		if (str != NULL)
1701 			kmem_free(str, strlen(str) + 1);
1702 	} else if (hexstr_to_label(ds_hexsl, &ds_sl) == 0) {
1703 		/*
1704 		 * Now compare labels to complete the MAC check.  If the
1705 		 * labels are equal then allow access.  If the mountpoint
1706 		 * label dominates the dataset label, allow readonly access.
1707 		 * Otherwise, access is denied.
1708 		 */
1709 		if (blequal(mnt_sl, &ds_sl))
1710 			retv = 0;
1711 		else if (bldominates(mnt_sl, &ds_sl)) {
1712 			vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
1713 			retv = 0;
1714 		}
1715 	}
1716 
1717 	label_rele(mnt_tsl);
1718 	zone_rele(mntzone);
1719 	return (retv);
1720 }
1721 
1722 /*
1723  * Load a string-valued boot property and attempt to convert it to a 64-bit
1724  * unsigned integer.  If the value is not present, or the conversion fails,
1725  * return the provided default value.
1726  */
1727 static uint64_t
1728 spa_get_bootprop_uint64(const char *name, uint64_t defval)
1729 {
1730 	char *propval;
1731 	u_longlong_t r;
1732 	int e;
1733 
1734 	if ((propval = spa_get_bootprop(name)) == NULL) {
1735 		/*
1736 		 * The property does not exist.
1737 		 */
1738 		return (defval);
1739 	}
1740 
1741 	e = ddi_strtoull(propval, NULL, 10, &r);
1742 
1743 	spa_free_bootprop(propval);
1744 
1745 	/*
1746 	 * If the conversion succeeded, return the value.  If there was any
1747 	 * kind of failure, just return the default value.
1748 	 */
1749 	return (e == 0 ? r : defval);
1750 }
1751 
1752 static int
1753 zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
1754 {
1755 	int error = 0;
1756 	static int zfsrootdone = 0;
1757 	zfsvfs_t *zfsvfs = NULL;
1758 	znode_t *zp = NULL;
1759 	vnode_t *vp = NULL;
1760 	char *zfs_bootfs;
1761 	char *zfs_devid;
1762 	uint64_t zfs_bootpool;
1763 	uint64_t zfs_bootvdev;
1764 
1765 	ASSERT(vfsp);
1766 
1767 	/*
1768 	 * The filesystem that we mount as root is defined in the
1769 	 * boot property "zfs-bootfs" with a format of
1770 	 * "poolname/root-dataset-objnum".
1771 	 */
1772 	if (why == ROOT_INIT) {
1773 		if (zfsrootdone++)
1774 			return (SET_ERROR(EBUSY));
1775 
1776 		/*
1777 		 * the process of doing a spa_load will require the
1778 		 * clock to be set before we could (for example) do
1779 		 * something better by looking at the timestamp on
1780 		 * an uberblock, so just set it to -1.
1781 		 */
1782 		clkset(-1);
1783 
1784 		if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
1785 			cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
1786 			    "bootfs name");
1787 			return (SET_ERROR(EINVAL));
1788 		}
1789 		zfs_devid = spa_get_bootprop("diskdevid");
1790 
1791 		/*
1792 		 * The boot loader may also provide us with the GUID for both
1793 		 * the pool and the nominated boot vdev.  A GUID value of 0 is
1794 		 * explicitly invalid (see "spa_change_guid()"), so we use this
1795 		 * as a sentinel value when no GUID is present.
1796 		 */
1797 		zfs_bootpool = spa_get_bootprop_uint64("zfs-bootpool", 0);
1798 		zfs_bootvdev = spa_get_bootprop_uint64("zfs-bootvdev", 0);
1799 
1800 		/*
1801 		 * Initialise the early boot device rescan mechanism.  A scan
1802 		 * will not actually be performed unless we need to do so in
1803 		 * order to find the correct /devices path for a relocated
1804 		 * device.
1805 		 */
1806 		vdev_disk_preroot_init();
1807 
1808 		error = spa_import_rootpool(rootfs.bo_name, zfs_devid,
1809 		    zfs_bootpool, zfs_bootvdev);
1810 
1811 		spa_free_bootprop(zfs_devid);
1812 
1813 		if (error != 0) {
1814 			spa_free_bootprop(zfs_bootfs);
1815 			vdev_disk_preroot_fini();
1816 			cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
1817 			    error);
1818 			return (error);
1819 		}
1820 
1821 		if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
1822 			spa_free_bootprop(zfs_bootfs);
1823 			vdev_disk_preroot_fini();
1824 			cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
1825 			    error);
1826 			return (error);
1827 		}
1828 
1829 		spa_free_bootprop(zfs_bootfs);
1830 		vdev_disk_preroot_fini();
1831 
1832 		if (error = vfs_lock(vfsp))
1833 			return (error);
1834 
1835 		if (error = zfs_domount(vfsp, rootfs.bo_name)) {
1836 			cmn_err(CE_NOTE, "zfs_domount: error %d", error);
1837 			goto out;
1838 		}
1839 
1840 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
1841 		ASSERT(zfsvfs);
1842 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
1843 			cmn_err(CE_NOTE, "zfs_zget: error %d", error);
1844 			goto out;
1845 		}
1846 
1847 		vp = ZTOV(zp);
1848 		mutex_enter(&vp->v_lock);
1849 		vp->v_flag |= VROOT;
1850 		mutex_exit(&vp->v_lock);
1851 		rootvp = vp;
1852 
1853 		/*
1854 		 * Leave rootvp held.  The root file system is never unmounted.
1855 		 */
1856 
1857 		vfs_add((struct vnode *)0, vfsp,
1858 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
1859 out:
1860 		vfs_unlock(vfsp);
1861 		return (error);
1862 	} else if (why == ROOT_REMOUNT) {
1863 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
1864 		vfsp->vfs_flag |= VFS_REMOUNT;
1865 
1866 		/* refresh mount options */
1867 		zfs_unregister_callbacks(vfsp->vfs_data);
1868 		return (zfs_register_callbacks(vfsp));
1869 
1870 	} else if (why == ROOT_UNMOUNT) {
1871 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
1872 		(void) zfs_sync(vfsp, 0, 0);
1873 		return (0);
1874 	}
1875 
1876 	/*
1877 	 * if "why" is equal to anything else other than ROOT_INIT,
1878 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
1879 	 */
1880 	return (SET_ERROR(ENOTSUP));
1881 }
1882 
1883 /*ARGSUSED*/
1884 static int
1885 zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
1886 {
1887 	char		*osname;
1888 	pathname_t	spn;
1889 	int		error = 0;
1890 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
1891 	    UIO_SYSSPACE : UIO_USERSPACE;
1892 	int		canwrite;
1893 
1894 	if (mvp->v_type != VDIR)
1895 		return (SET_ERROR(ENOTDIR));
1896 
1897 	mutex_enter(&mvp->v_lock);
1898 	if ((uap->flags & MS_REMOUNT) == 0 &&
1899 	    (uap->flags & MS_OVERLAY) == 0 &&
1900 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
1901 		mutex_exit(&mvp->v_lock);
1902 		return (SET_ERROR(EBUSY));
1903 	}
1904 	mutex_exit(&mvp->v_lock);
1905 
1906 	/*
1907 	 * ZFS does not support passing unparsed data in via MS_DATA.
1908 	 * Users should use the MS_OPTIONSTR interface; this means
1909 	 * that all option parsing is already done and the options struct
1910 	 * can be interrogated.
1911 	 */
1912 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
1913 		return (SET_ERROR(EINVAL));
1914 
1915 	/*
1916 	 * Get the objset name (the "special" mount argument).
1917 	 */
1918 	if (error = pn_get(uap->spec, fromspace, &spn))
1919 		return (error);
1920 
1921 	osname = spn.pn_path;
1922 
1923 	/*
1924 	 * Check for mount privilege?
1925 	 *
1926 	 * If we don't have privilege then see if
1927 	 * we have local permission to allow it
1928 	 */
1929 	error = secpolicy_fs_mount(cr, mvp, vfsp);
1930 	if (error) {
1931 		if (dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) == 0) {
1932 			vattr_t		vattr;
1933 
1934 			/*
1935 			 * Make sure user is the owner of the mount point
1936 			 * or has sufficient privileges.
1937 			 */
1938 
1939 			vattr.va_mask = AT_UID;
1940 
1941 			if (VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
1942 				goto out;
1943 			}
1944 
1945 			if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 &&
1946 			    VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) {
1947 				goto out;
1948 			}
1949 			secpolicy_fs_mount_clearopts(cr, vfsp);
1950 		} else {
1951 			goto out;
1952 		}
1953 	}
1954 
1955 	/*
1956 	 * Refuse to mount a filesystem if we are in a local zone and the
1957 	 * dataset is not visible.
1958 	 */
1959 	if (!INGLOBALZONE(curproc) &&
1960 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1961 		error = SET_ERROR(EPERM);
1962 		goto out;
1963 	}
1964 
1965 	error = zfs_mount_label_policy(vfsp, osname);
1966 	if (error)
1967 		goto out;
1968 
1969 	/*
1970 	 * When doing a remount, we simply refresh our temporary properties
1971 	 * according to those options set in the current VFS options.
1972 	 */
1973 	if (uap->flags & MS_REMOUNT) {
1974 		/* refresh mount options */
1975 		zfs_unregister_callbacks(vfsp->vfs_data);
1976 		error = zfs_register_callbacks(vfsp);
1977 		goto out;
1978 	}
1979 
1980 	error = zfs_domount(vfsp, osname);
1981 
1982 	/*
1983 	 * Add an extra VFS_HOLD on our parent vfs so that it can't
1984 	 * disappear due to a forced unmount.
1985 	 */
1986 	if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap)
1987 		VFS_HOLD(mvp->v_vfsp);
1988 
1989 out:
1990 	pn_free(&spn);
1991 	return (error);
1992 }
1993 
1994 static int
1995 zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1996 {
1997 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1998 	dev32_t d32;
1999 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
2000 	int err = 0;
2001 
2002 	ZFS_ENTER(zfsvfs);
2003 
2004 	dmu_objset_space(zfsvfs->z_os,
2005 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
2006 
2007 	/*
2008 	 * The underlying storage pool actually uses multiple block sizes.
2009 	 * We report the fragsize as the smallest block size we support,
2010 	 * and we report our blocksize as the filesystem's maximum blocksize.
2011 	 */
2012 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
2013 	statp->f_bsize = zfsvfs->z_max_blksz;
2014 
2015 	/*
2016 	 * The following report "total" blocks of various kinds in the
2017 	 * file system, but reported in terms of f_frsize - the
2018 	 * "fragment" size.
2019 	 */
2020 
2021 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
2022 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
2023 	statp->f_bavail = statp->f_bfree; /* no root reservation */
2024 
2025 	/*
2026 	 * statvfs() should really be called statufs(), because it assumes
2027 	 * static metadata.  ZFS doesn't preallocate files, so the best
2028 	 * we can do is report the max that could possibly fit in f_files,
2029 	 * and that minus the number actually used in f_ffree.
2030 	 * For f_ffree, report the smaller of the number of object available
2031 	 * and the number of blocks (each object will take at least a block).
2032 	 */
2033 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
2034 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
2035 	statp->f_files = statp->f_ffree + usedobjs;
2036 
2037 	(void) cmpldev(&d32, vfsp->vfs_dev);
2038 	statp->f_fsid = d32;
2039 
2040 	/*
2041 	 * We're a zfs filesystem.
2042 	 */
2043 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
2044 
2045 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
2046 
2047 	statp->f_namemax = MAXNAMELEN - 1;
2048 
2049 	/*
2050 	 * We have all of 32 characters to stuff a string here.
2051 	 * Is there anything useful we could/should provide?
2052 	 */
2053 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
2054 
2055 	if (dmu_objset_projectquota_enabled(zfsvfs->z_os) &&
2056 	    dmu_objset_projectquota_present(zfsvfs->z_os)) {
2057 		znode_t *zp;
2058 
2059 		/*
2060 		 * In ZoL, zfs_statvfs is passed a Linux dentry (directory
2061 		 * entry), instead of a vfsp. The ZoL code uses the dentry
2062 		 * to get the znode from the dentry's inode. This represents
2063 		 * whatever filename was passed to the user-level statvfs
2064 		 * syscall.
2065 		 *
2066 		 * We're using the VFS root znode here, so this represents a
2067 		 * potential difference from ZoL.
2068 		 */
2069 		if (zfs_zget(zfsvfs, zfsvfs->z_root, &zp) == 0) {
2070 			uint32_t bshift = ddi_fls(statp->f_bsize) - 1;
2071 
2072 			if (zp->z_pflags & ZFS_PROJINHERIT && zp->z_projid &&
2073 			    zpl_is_valid_projid(zp->z_projid))
2074 				err = zfs_statfs_project(zfsvfs, zp, statp,
2075 				    bshift);
2076 			VN_RELE(ZTOV(zp));
2077 		}
2078 	}
2079 
2080 	ZFS_EXIT(zfsvfs);
2081 	return (err);
2082 }
2083 
2084 static int
2085 zfs_root(vfs_t *vfsp, vnode_t **vpp)
2086 {
2087 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
2088 	znode_t *rootzp;
2089 	int error;
2090 
2091 	ZFS_ENTER(zfsvfs);
2092 
2093 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
2094 	if (error == 0)
2095 		*vpp = ZTOV(rootzp);
2096 
2097 	ZFS_EXIT(zfsvfs);
2098 	return (error);
2099 }
2100 
2101 /*
2102  * Teardown the zfsvfs::z_os.
2103  *
2104  * Note, if 'unmounting' is FALSE, we return with the 'z_teardown_lock'
2105  * and 'z_teardown_inactive_lock' held.
2106  */
2107 static int
2108 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
2109 {
2110 	znode_t	*zp;
2111 
2112 	zfs_unlinked_drain_stop_wait(zfsvfs);
2113 
2114 	rrm_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
2115 
2116 	if (!unmounting) {
2117 		/*
2118 		 * We purge the parent filesystem's vfsp as the parent
2119 		 * filesystem and all of its snapshots have their vnode's
2120 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
2121 		 * 'z_parent' is self referential for non-snapshots.
2122 		 */
2123 		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
2124 	}
2125 
2126 	/*
2127 	 * Close the zil. NB: Can't close the zil while zfs_inactive
2128 	 * threads are blocked as zil_close can call zfs_inactive.
2129 	 */
2130 	if (zfsvfs->z_log) {
2131 		zil_close(zfsvfs->z_log);
2132 		zfsvfs->z_log = NULL;
2133 	}
2134 
2135 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
2136 
2137 	/*
2138 	 * If we are not unmounting (ie: online recv) and someone already
2139 	 * unmounted this file system while we were doing the switcheroo,
2140 	 * or a reopen of z_os failed then just bail out now.
2141 	 */
2142 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
2143 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
2144 		rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2145 		return (SET_ERROR(EIO));
2146 	}
2147 
2148 	/*
2149 	 * At this point there are no vops active, and any new vops will
2150 	 * fail with EIO since we have z_teardown_lock for writer (only
2151 	 * relavent for forced unmount).
2152 	 *
2153 	 * Release all holds on dbufs.
2154 	 */
2155 	mutex_enter(&zfsvfs->z_znodes_lock);
2156 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
2157 	    zp = list_next(&zfsvfs->z_all_znodes, zp))
2158 		if (zp->z_sa_hdl) {
2159 			ASSERT(ZTOV(zp)->v_count > 0);
2160 			zfs_znode_dmu_fini(zp);
2161 		}
2162 	mutex_exit(&zfsvfs->z_znodes_lock);
2163 
2164 	/*
2165 	 * If we are unmounting, set the unmounted flag and let new vops
2166 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
2167 	 * other vops will fail with EIO.
2168 	 */
2169 	if (unmounting) {
2170 		zfsvfs->z_unmounted = B_TRUE;
2171 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
2172 		rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2173 	}
2174 
2175 	/*
2176 	 * z_os will be NULL if there was an error in attempting to reopen
2177 	 * zfsvfs, so just return as the properties had already been
2178 	 * unregistered and cached data had been evicted before.
2179 	 */
2180 	if (zfsvfs->z_os == NULL)
2181 		return (0);
2182 
2183 	/*
2184 	 * Unregister properties.
2185 	 */
2186 	zfs_unregister_callbacks(zfsvfs);
2187 
2188 	/*
2189 	 * Evict cached data
2190 	 */
2191 	if (dsl_dataset_is_dirty(dmu_objset_ds(zfsvfs->z_os)) &&
2192 	    !(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY))
2193 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
2194 	dmu_objset_evict_dbufs(zfsvfs->z_os);
2195 
2196 	return (0);
2197 }
2198 
2199 /*ARGSUSED*/
2200 static int
2201 zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
2202 {
2203 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
2204 	objset_t *os;
2205 	int ret;
2206 
2207 	ret = secpolicy_fs_unmount(cr, vfsp);
2208 	if (ret) {
2209 		if (dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
2210 		    ZFS_DELEG_PERM_MOUNT, cr))
2211 			return (ret);
2212 	}
2213 
2214 	/*
2215 	 * We purge the parent filesystem's vfsp as the parent filesystem
2216 	 * and all of its snapshots have their vnode's v_vfsp set to the
2217 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
2218 	 * referential for non-snapshots.
2219 	 */
2220 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
2221 
2222 	/*
2223 	 * Unmount any snapshots mounted under .zfs before unmounting the
2224 	 * dataset itself.
2225 	 */
2226 	if (zfsvfs->z_ctldir != NULL &&
2227 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
2228 		return (ret);
2229 	}
2230 
2231 	if (!(fflag & MS_FORCE)) {
2232 		/*
2233 		 * Check the number of active vnodes in the file system.
2234 		 * Our count is maintained in the vfs structure, but the
2235 		 * number is off by 1 to indicate a hold on the vfs
2236 		 * structure itself.
2237 		 */
2238 		boolean_t draining;
2239 		uint_t thresh = 1;
2240 
2241 		/*
2242 		 * The '.zfs' directory maintains a reference of its own, and
2243 		 * any active references underneath are reflected in the vnode
2244 		 * count. Allow one additional reference for it.
2245 		 */
2246 		if (zfsvfs->z_ctldir != NULL)
2247 			thresh++;
2248 
2249 		/*
2250 		 * If it's running, the asynchronous unlinked drain task needs
2251 		 * to be stopped before the number of active vnodes can be
2252 		 * reliably checked.
2253 		 */
2254 		draining = zfsvfs->z_draining;
2255 		if (draining)
2256 			zfs_unlinked_drain_stop_wait(zfsvfs);
2257 
2258 		if (vfsp->vfs_count > thresh || (zfsvfs->z_ctldir != NULL &&
2259 		    zfsvfs->z_ctldir->v_count > 1)) {
2260 			if (draining) {
2261 				/* If it was draining, restart the task */
2262 				zfs_unlinked_drain(zfsvfs);
2263 			}
2264 			return (SET_ERROR(EBUSY));
2265 		}
2266 	}
2267 
2268 	vfsp->vfs_flag |= VFS_UNMOUNTED;
2269 
2270 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
2271 	os = zfsvfs->z_os;
2272 
2273 	/*
2274 	 * z_os will be NULL if there was an error in
2275 	 * attempting to reopen zfsvfs.
2276 	 */
2277 	if (os != NULL) {
2278 		/*
2279 		 * Unset the objset user_ptr.
2280 		 */
2281 		mutex_enter(&os->os_user_ptr_lock);
2282 		dmu_objset_set_user(os, NULL);
2283 		mutex_exit(&os->os_user_ptr_lock);
2284 
2285 		/*
2286 		 * Finally release the objset
2287 		 */
2288 		dmu_objset_disown(os, B_TRUE, zfsvfs);
2289 	}
2290 
2291 	/*
2292 	 * We can now safely destroy the '.zfs' directory node.
2293 	 */
2294 	if (zfsvfs->z_ctldir != NULL)
2295 		zfsctl_destroy(zfsvfs);
2296 
2297 	return (0);
2298 }
2299 
2300 static int
2301 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
2302 {
2303 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
2304 	znode_t		*zp;
2305 	uint64_t	object = 0;
2306 	uint64_t	fid_gen = 0;
2307 	uint64_t	gen_mask;
2308 	uint64_t	zp_gen;
2309 	int		i, err;
2310 
2311 	*vpp = NULL;
2312 
2313 	ZFS_ENTER(zfsvfs);
2314 
2315 	if (fidp->fid_len == LONG_FID_LEN) {
2316 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
2317 		uint64_t	objsetid = 0;
2318 		uint64_t	setgen = 0;
2319 
2320 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
2321 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
2322 
2323 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
2324 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
2325 
2326 		ZFS_EXIT(zfsvfs);
2327 
2328 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
2329 		if (err)
2330 			return (SET_ERROR(EINVAL));
2331 		ZFS_ENTER(zfsvfs);
2332 	}
2333 
2334 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
2335 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
2336 
2337 		for (i = 0; i < sizeof (zfid->zf_object); i++)
2338 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
2339 
2340 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
2341 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
2342 	} else {
2343 		ZFS_EXIT(zfsvfs);
2344 		return (SET_ERROR(EINVAL));
2345 	}
2346 
2347 	/* A zero fid_gen means we are in the .zfs control directories */
2348 	if (fid_gen == 0 &&
2349 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
2350 		*vpp = zfsvfs->z_ctldir;
2351 		ASSERT(*vpp != NULL);
2352 		if (object == ZFSCTL_INO_SNAPDIR) {
2353 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
2354 			    0, NULL, NULL, NULL, NULL, NULL) == 0);
2355 		} else {
2356 			VN_HOLD(*vpp);
2357 		}
2358 		ZFS_EXIT(zfsvfs);
2359 		return (0);
2360 	}
2361 
2362 	gen_mask = -1ULL >> (64 - 8 * i);
2363 
2364 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
2365 	if (err = zfs_zget(zfsvfs, object, &zp)) {
2366 		ZFS_EXIT(zfsvfs);
2367 		return (err);
2368 	}
2369 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
2370 	    sizeof (uint64_t));
2371 	zp_gen = zp_gen & gen_mask;
2372 	if (zp_gen == 0)
2373 		zp_gen = 1;
2374 	if (zp->z_unlinked || zp_gen != fid_gen) {
2375 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
2376 		VN_RELE(ZTOV(zp));
2377 		ZFS_EXIT(zfsvfs);
2378 		return (SET_ERROR(EINVAL));
2379 	}
2380 
2381 	*vpp = ZTOV(zp);
2382 	ZFS_EXIT(zfsvfs);
2383 	return (0);
2384 }
2385 
2386 /*
2387  * Block out VOPs and close zfsvfs_t::z_os
2388  *
2389  * Note, if successful, then we return with the 'z_teardown_lock' and
2390  * 'z_teardown_inactive_lock' write held.  We leave ownership of the underlying
2391  * dataset and objset intact so that they can be atomically handed off during
2392  * a subsequent rollback or recv operation and the resume thereafter.
2393  */
2394 int
2395 zfs_suspend_fs(zfsvfs_t *zfsvfs)
2396 {
2397 	int error;
2398 
2399 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
2400 		return (error);
2401 
2402 	return (0);
2403 }
2404 
2405 /*
2406  * Rebuild SA and release VOPs.  Note that ownership of the underlying dataset
2407  * is an invariant across any of the operations that can be performed while the
2408  * filesystem was suspended.  Whether it succeeded or failed, the preconditions
2409  * are the same: the relevant objset and associated dataset are owned by
2410  * zfsvfs, held, and long held on entry.
2411  */
2412 int
2413 zfs_resume_fs(zfsvfs_t *zfsvfs, dsl_dataset_t *ds)
2414 {
2415 	int err;
2416 	znode_t *zp;
2417 
2418 	ASSERT(RRM_WRITE_HELD(&zfsvfs->z_teardown_lock));
2419 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
2420 
2421 	/*
2422 	 * We already own this, so just update the objset_t, as the one we
2423 	 * had before may have been evicted.
2424 	 */
2425 	objset_t *os;
2426 	VERIFY3P(ds->ds_owner, ==, zfsvfs);
2427 	VERIFY(dsl_dataset_long_held(ds));
2428 	VERIFY0(dmu_objset_from_ds(ds, &os));
2429 
2430 	err = zfsvfs_init(zfsvfs, os);
2431 	if (err != 0)
2432 		goto bail;
2433 
2434 	VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
2435 
2436 	zfs_set_fuid_feature(zfsvfs);
2437 
2438 	/*
2439 	 * Attempt to re-establish all the active znodes with
2440 	 * their dbufs.  If a zfs_rezget() fails, then we'll let
2441 	 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
2442 	 * when they try to use their znode.
2443 	 */
2444 	mutex_enter(&zfsvfs->z_znodes_lock);
2445 	for (zp = list_head(&zfsvfs->z_all_znodes); zp;
2446 	    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
2447 		(void) zfs_rezget(zp);
2448 	}
2449 	mutex_exit(&zfsvfs->z_znodes_lock);
2450 
2451 	if (((zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) == 0) &&
2452 	    !zfsvfs->z_unmounted) {
2453 		/*
2454 		 * zfs_suspend_fs() could have interrupted freeing
2455 		 * of dnodes. We need to restart this freeing so
2456 		 * that we don't "leak" the space.
2457 		 */
2458 		zfs_unlinked_drain(zfsvfs);
2459 	}
2460 
2461 bail:
2462 	/* release the VOPs */
2463 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
2464 	rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2465 
2466 	if (err) {
2467 		/*
2468 		 * Since we couldn't setup the sa framework, try to force
2469 		 * unmount this file system.
2470 		 */
2471 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
2472 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
2473 	}
2474 	return (err);
2475 }
2476 
2477 static void
2478 zfs_freevfs(vfs_t *vfsp)
2479 {
2480 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
2481 
2482 	/*
2483 	 * If this is a snapshot, we have an extra VFS_HOLD on our parent
2484 	 * from zfs_mount().  Release it here.  If we came through
2485 	 * zfs_mountroot() instead, we didn't grab an extra hold, so
2486 	 * skip the VFS_RELE for rootvfs.
2487 	 */
2488 	if (zfsvfs->z_issnap && (vfsp != rootvfs))
2489 		VFS_RELE(zfsvfs->z_parent->z_vfs);
2490 
2491 	zfsvfs_free(zfsvfs);
2492 
2493 	atomic_dec_32(&zfs_active_fs_count);
2494 }
2495 
2496 /*
2497  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
2498  * so we can't safely do any non-idempotent initialization here.
2499  * Leave that to zfs_init() and zfs_fini(), which are called
2500  * from the module's _init() and _fini() entry points.
2501  */
2502 /*ARGSUSED*/
2503 static int
2504 zfs_vfsinit(int fstype, char *name)
2505 {
2506 	int error;
2507 
2508 	zfsfstype = fstype;
2509 
2510 	/*
2511 	 * Setup vfsops and vnodeops tables.
2512 	 */
2513 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
2514 	if (error != 0) {
2515 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
2516 	}
2517 
2518 	error = zfs_create_op_tables();
2519 	if (error) {
2520 		zfs_remove_op_tables();
2521 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
2522 		(void) vfs_freevfsops_by_type(zfsfstype);
2523 		return (error);
2524 	}
2525 
2526 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
2527 
2528 	/*
2529 	 * Unique major number for all zfs mounts.
2530 	 * If we run out of 32-bit minors, we'll getudev() another major.
2531 	 */
2532 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
2533 	zfs_minor = ZFS_MIN_MINOR;
2534 
2535 	return (0);
2536 }
2537 
2538 void
2539 zfs_init(void)
2540 {
2541 	/*
2542 	 * Initialize .zfs directory structures
2543 	 */
2544 	zfsctl_init();
2545 
2546 	/*
2547 	 * Initialize znode cache, vnode ops, etc...
2548 	 */
2549 	zfs_znode_init();
2550 
2551 	dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
2552 }
2553 
2554 void
2555 zfs_fini(void)
2556 {
2557 	zfsctl_fini();
2558 	zfs_znode_fini();
2559 }
2560 
2561 int
2562 zfs_busy(void)
2563 {
2564 	return (zfs_active_fs_count != 0);
2565 }
2566 
2567 int
2568 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
2569 {
2570 	int error;
2571 	objset_t *os = zfsvfs->z_os;
2572 	dmu_tx_t *tx;
2573 
2574 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
2575 		return (SET_ERROR(EINVAL));
2576 
2577 	if (newvers < zfsvfs->z_version)
2578 		return (SET_ERROR(EINVAL));
2579 
2580 	if (zfs_spa_version_map(newvers) >
2581 	    spa_version(dmu_objset_spa(zfsvfs->z_os)))
2582 		return (SET_ERROR(ENOTSUP));
2583 
2584 	tx = dmu_tx_create(os);
2585 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
2586 	if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2587 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
2588 		    ZFS_SA_ATTRS);
2589 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2590 	}
2591 	error = dmu_tx_assign(tx, TXG_WAIT);
2592 	if (error) {
2593 		dmu_tx_abort(tx);
2594 		return (error);
2595 	}
2596 
2597 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
2598 	    8, 1, &newvers, tx);
2599 
2600 	if (error) {
2601 		dmu_tx_commit(tx);
2602 		return (error);
2603 	}
2604 
2605 	if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2606 		uint64_t sa_obj;
2607 
2608 		ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
2609 		    SPA_VERSION_SA);
2610 		sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
2611 		    DMU_OT_NONE, 0, tx);
2612 
2613 		error = zap_add(os, MASTER_NODE_OBJ,
2614 		    ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
2615 		ASSERT0(error);
2616 
2617 		VERIFY(0 == sa_set_sa_object(os, sa_obj));
2618 		sa_register_update_callback(os, zfs_sa_upgrade);
2619 	}
2620 
2621 	spa_history_log_internal_ds(dmu_objset_ds(os), "upgrade", tx,
2622 	    "from %llu to %llu", zfsvfs->z_version, newvers);
2623 
2624 	dmu_tx_commit(tx);
2625 
2626 	zfsvfs->z_version = newvers;
2627 	os->os_version = newvers;
2628 
2629 	zfs_set_fuid_feature(zfsvfs);
2630 
2631 	return (0);
2632 }
2633 
2634 /*
2635  * Read a property stored within the master node.
2636  */
2637 int
2638 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
2639 {
2640 	uint64_t *cached_copy = NULL;
2641 
2642 	/*
2643 	 * Figure out where in the objset_t the cached copy would live, if it
2644 	 * is available for the requested property.
2645 	 */
2646 	if (os != NULL) {
2647 		switch (prop) {
2648 		case ZFS_PROP_VERSION:
2649 			cached_copy = &os->os_version;
2650 			break;
2651 		case ZFS_PROP_NORMALIZE:
2652 			cached_copy = &os->os_normalization;
2653 			break;
2654 		case ZFS_PROP_UTF8ONLY:
2655 			cached_copy = &os->os_utf8only;
2656 			break;
2657 		case ZFS_PROP_CASE:
2658 			cached_copy = &os->os_casesensitivity;
2659 			break;
2660 		default:
2661 			break;
2662 		}
2663 	}
2664 	if (cached_copy != NULL && *cached_copy != OBJSET_PROP_UNINITIALIZED) {
2665 		*value = *cached_copy;
2666 		return (0);
2667 	}
2668 
2669 	/*
2670 	 * If the property wasn't cached, look up the file system's value for
2671 	 * the property. For the version property, we look up a slightly
2672 	 * different string.
2673 	 */
2674 	const char *pname;
2675 	int error = ENOENT;
2676 	if (prop == ZFS_PROP_VERSION) {
2677 		pname = ZPL_VERSION_STR;
2678 	} else {
2679 		pname = zfs_prop_to_name(prop);
2680 	}
2681 
2682 	if (os != NULL) {
2683 		ASSERT3U(os->os_phys->os_type, ==, DMU_OST_ZFS);
2684 		error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
2685 	}
2686 
2687 	if (error == ENOENT) {
2688 		/* No value set, use the default value */
2689 		switch (prop) {
2690 		case ZFS_PROP_VERSION:
2691 			*value = ZPL_VERSION;
2692 			break;
2693 		case ZFS_PROP_NORMALIZE:
2694 		case ZFS_PROP_UTF8ONLY:
2695 			*value = 0;
2696 			break;
2697 		case ZFS_PROP_CASE:
2698 			*value = ZFS_CASE_SENSITIVE;
2699 			break;
2700 		default:
2701 			return (error);
2702 		}
2703 		error = 0;
2704 	}
2705 
2706 	/*
2707 	 * If one of the methods for getting the property value above worked,
2708 	 * copy it into the objset_t's cache.
2709 	 */
2710 	if (error == 0 && cached_copy != NULL) {
2711 		*cached_copy = *value;
2712 	}
2713 
2714 	return (error);
2715 }
2716 
2717 /*
2718  * Return true if the coresponding vfs's unmounted flag is set.
2719  * Otherwise return false.
2720  * If this function returns true we know VFS unmount has been initiated.
2721  */
2722 boolean_t
2723 zfs_get_vfs_flag_unmounted(objset_t *os)
2724 {
2725 	zfsvfs_t *zfvp;
2726 	boolean_t unmounted = B_FALSE;
2727 
2728 	ASSERT(dmu_objset_type(os) == DMU_OST_ZFS);
2729 
2730 	mutex_enter(&os->os_user_ptr_lock);
2731 	zfvp = dmu_objset_get_user(os);
2732 	if (zfvp != NULL && zfvp->z_vfs != NULL &&
2733 	    (zfvp->z_vfs->vfs_flag & VFS_UNMOUNTED))
2734 		unmounted = B_TRUE;
2735 	mutex_exit(&os->os_user_ptr_lock);
2736 
2737 	return (unmounted);
2738 }
2739 
2740 static vfsdef_t vfw = {
2741 	VFSDEF_VERSION,
2742 	MNTTYPE_ZFS,
2743 	zfs_vfsinit,
2744 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS|
2745 	    VSW_XID|VSW_ZMOUNT,
2746 	&zfs_mntopts
2747 };
2748 
2749 struct modlfs zfs_modlfs = {
2750 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
2751 };
2752