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