xref: /titanic_51/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 1e194cd1a618eb48f311652742895fc33026c470)
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, 2014 by Delphix. All rights reserved.
24  * Copyright (c) 2014 Integros [integros.com]
25  */
26 
27 /* Portions Copyright 2010 Robert Milkowski */
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/sysmacros.h>
33 #include <sys/kmem.h>
34 #include <sys/pathname.h>
35 #include <sys/vnode.h>
36 #include <sys/vfs.h>
37 #include <sys/vfs_opreg.h>
38 #include <sys/mntent.h>
39 #include <sys/mount.h>
40 #include <sys/cmn_err.h>
41 #include "fs/fs_subr.h"
42 #include <sys/zfs_znode.h>
43 #include <sys/zfs_dir.h>
44 #include <sys/zil.h>
45 #include <sys/fs/zfs.h>
46 #include <sys/dmu.h>
47 #include <sys/dsl_prop.h>
48 #include <sys/dsl_dataset.h>
49 #include <sys/dsl_deleg.h>
50 #include <sys/spa.h>
51 #include <sys/zap.h>
52 #include <sys/sa.h>
53 #include <sys/sa_impl.h>
54 #include <sys/varargs.h>
55 #include <sys/policy.h>
56 #include <sys/atomic.h>
57 #include <sys/mkdev.h>
58 #include <sys/modctl.h>
59 #include <sys/refstr.h>
60 #include <sys/zfs_ioctl.h>
61 #include <sys/zfs_ctldir.h>
62 #include <sys/zfs_fuid.h>
63 #include <sys/bootconf.h>
64 #include <sys/sunddi.h>
65 #include <sys/dnlc.h>
66 #include <sys/dmu_objset.h>
67 #include <sys/spa_boot.h>
68 #include <sys/zfs_events.h>
69 #include "zfs_comutil.h"
70 
71 int zfsfstype;
72 vfsops_t *zfs_vfsops = NULL;
73 static major_t zfs_major;
74 static minor_t zfs_minor;
75 static kmutex_t	zfs_dev_mtx;
76 
77 extern int sys_shutdown;
78 
79 static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
80 static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr);
81 static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
82 static int zfs_root(vfs_t *vfsp, vnode_t **vpp);
83 static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp);
84 static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp);
85 static void zfs_freevfs(vfs_t *vfsp);
86 
87 static const fs_operation_def_t zfs_vfsops_template[] = {
88 	VFSNAME_MOUNT,		{ .vfs_mount = zfs_mount },
89 	VFSNAME_MOUNTROOT,	{ .vfs_mountroot = zfs_mountroot },
90 	VFSNAME_UNMOUNT,	{ .vfs_unmount = zfs_umount },
91 	VFSNAME_ROOT,		{ .vfs_root = zfs_root },
92 	VFSNAME_STATVFS,	{ .vfs_statvfs = zfs_statvfs },
93 	VFSNAME_SYNC,		{ .vfs_sync = zfs_sync },
94 	VFSNAME_VGET,		{ .vfs_vget = zfs_vget },
95 	VFSNAME_FREEVFS,	{ .vfs_freevfs = zfs_freevfs },
96 	NULL,			NULL
97 };
98 
99 static const fs_operation_def_t zfs_vfsops_eio_template[] = {
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[MAXNAMELEN];
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)
573 {
574 	/*
575 	 * Is it a valid type of object to track?
576 	 */
577 	if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
578 		return (SET_ERROR(ENOENT));
579 
580 	/*
581 	 * If we have a NULL data pointer
582 	 * then assume the id's aren't changing and
583 	 * return EEXIST to the dmu to let it know to
584 	 * use the same ids
585 	 */
586 	if (data == NULL)
587 		return (SET_ERROR(EEXIST));
588 
589 	if (bonustype == DMU_OT_ZNODE) {
590 		znode_phys_t *znp = data;
591 		*userp = znp->zp_uid;
592 		*groupp = znp->zp_gid;
593 	} else {
594 		int hdrsize;
595 		sa_hdr_phys_t *sap = data;
596 		sa_hdr_phys_t sa = *sap;
597 		boolean_t swap = B_FALSE;
598 
599 		ASSERT(bonustype == DMU_OT_SA);
600 
601 		if (sa.sa_magic == 0) {
602 			/*
603 			 * This should only happen for newly created
604 			 * files that haven't had the znode data filled
605 			 * in yet.
606 			 */
607 			*userp = 0;
608 			*groupp = 0;
609 			return (0);
610 		}
611 		if (sa.sa_magic == BSWAP_32(SA_MAGIC)) {
612 			sa.sa_magic = SA_MAGIC;
613 			sa.sa_layout_info = BSWAP_16(sa.sa_layout_info);
614 			swap = B_TRUE;
615 		} else {
616 			VERIFY3U(sa.sa_magic, ==, SA_MAGIC);
617 		}
618 
619 		hdrsize = sa_hdrsize(&sa);
620 		VERIFY3U(hdrsize, >=, sizeof (sa_hdr_phys_t));
621 		*userp = *((uint64_t *)((uintptr_t)data + hdrsize +
622 		    SA_UID_OFFSET));
623 		*groupp = *((uint64_t *)((uintptr_t)data + hdrsize +
624 		    SA_GID_OFFSET));
625 		if (swap) {
626 			*userp = BSWAP_64(*userp);
627 			*groupp = BSWAP_64(*groupp);
628 		}
629 	}
630 	return (0);
631 }
632 
633 static void
634 fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr,
635     char *domainbuf, int buflen, uid_t *ridp)
636 {
637 	uint64_t fuid;
638 	const char *domain;
639 
640 	fuid = strtonum(fuidstr, NULL);
641 
642 	domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid));
643 	if (domain)
644 		(void) strlcpy(domainbuf, domain, buflen);
645 	else
646 		domainbuf[0] = '\0';
647 	*ridp = FUID_RID(fuid);
648 }
649 
650 static uint64_t
651 zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type)
652 {
653 	switch (type) {
654 	case ZFS_PROP_USERUSED:
655 		return (DMU_USERUSED_OBJECT);
656 	case ZFS_PROP_GROUPUSED:
657 		return (DMU_GROUPUSED_OBJECT);
658 	case ZFS_PROP_USERQUOTA:
659 		return (zfsvfs->z_userquota_obj);
660 	case ZFS_PROP_GROUPQUOTA:
661 		return (zfsvfs->z_groupquota_obj);
662 	}
663 	return (0);
664 }
665 
666 int
667 zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
668     uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
669 {
670 	int error;
671 	zap_cursor_t zc;
672 	zap_attribute_t za;
673 	zfs_useracct_t *buf = vbuf;
674 	uint64_t obj;
675 
676 	if (!dmu_objset_userspace_present(zfsvfs->z_os))
677 		return (SET_ERROR(ENOTSUP));
678 
679 	obj = zfs_userquota_prop_to_obj(zfsvfs, type);
680 	if (obj == 0) {
681 		*bufsizep = 0;
682 		return (0);
683 	}
684 
685 	for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
686 	    (error = zap_cursor_retrieve(&zc, &za)) == 0;
687 	    zap_cursor_advance(&zc)) {
688 		if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
689 		    *bufsizep)
690 			break;
691 
692 		fuidstr_to_sid(zfsvfs, za.za_name,
693 		    buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
694 
695 		buf->zu_space = za.za_first_integer;
696 		buf++;
697 	}
698 	if (error == ENOENT)
699 		error = 0;
700 
701 	ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
702 	*bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
703 	*cookiep = zap_cursor_serialize(&zc);
704 	zap_cursor_fini(&zc);
705 	return (error);
706 }
707 
708 /*
709  * buf must be big enough (eg, 32 bytes)
710  */
711 static int
712 id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
713     char *buf, boolean_t addok)
714 {
715 	uint64_t fuid;
716 	int domainid = 0;
717 
718 	if (domain && domain[0]) {
719 		domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
720 		if (domainid == -1)
721 			return (SET_ERROR(ENOENT));
722 	}
723 	fuid = FUID_ENCODE(domainid, rid);
724 	(void) sprintf(buf, "%llx", (longlong_t)fuid);
725 	return (0);
726 }
727 
728 int
729 zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
730     const char *domain, uint64_t rid, uint64_t *valp)
731 {
732 	char buf[32];
733 	int err;
734 	uint64_t obj;
735 
736 	*valp = 0;
737 
738 	if (!dmu_objset_userspace_present(zfsvfs->z_os))
739 		return (SET_ERROR(ENOTSUP));
740 
741 	obj = zfs_userquota_prop_to_obj(zfsvfs, type);
742 	if (obj == 0)
743 		return (0);
744 
745 	err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_FALSE);
746 	if (err)
747 		return (err);
748 
749 	err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
750 	if (err == ENOENT)
751 		err = 0;
752 	return (err);
753 }
754 
755 int
756 zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
757     const char *domain, uint64_t rid, uint64_t quota)
758 {
759 	char buf[32];
760 	int err;
761 	dmu_tx_t *tx;
762 	uint64_t *objp;
763 	boolean_t fuid_dirtied;
764 
765 	if (type != ZFS_PROP_USERQUOTA && type != ZFS_PROP_GROUPQUOTA)
766 		return (SET_ERROR(EINVAL));
767 
768 	if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
769 		return (SET_ERROR(ENOTSUP));
770 
771 	objp = (type == ZFS_PROP_USERQUOTA) ? &zfsvfs->z_userquota_obj :
772 	    &zfsvfs->z_groupquota_obj;
773 
774 	err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE);
775 	if (err)
776 		return (err);
777 	fuid_dirtied = zfsvfs->z_fuid_dirty;
778 
779 	tx = dmu_tx_create(zfsvfs->z_os);
780 	dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
781 	if (*objp == 0) {
782 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
783 		    zfs_userquota_prop_prefixes[type]);
784 	}
785 	if (fuid_dirtied)
786 		zfs_fuid_txhold(zfsvfs, tx);
787 	err = dmu_tx_assign(tx, TXG_WAIT);
788 	if (err) {
789 		dmu_tx_abort(tx);
790 		return (err);
791 	}
792 
793 	mutex_enter(&zfsvfs->z_lock);
794 	if (*objp == 0) {
795 		*objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
796 		    DMU_OT_NONE, 0, tx);
797 		VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
798 		    zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
799 	}
800 	mutex_exit(&zfsvfs->z_lock);
801 
802 	if (quota == 0) {
803 		err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
804 		if (err == ENOENT)
805 			err = 0;
806 	} else {
807 		err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, &quota, tx);
808 	}
809 	ASSERT(err == 0);
810 	if (fuid_dirtied)
811 		zfs_fuid_sync(zfsvfs, tx);
812 	dmu_tx_commit(tx);
813 	return (err);
814 }
815 
816 boolean_t
817 zfs_fuid_overquota(zfsvfs_t *zfsvfs, boolean_t isgroup, uint64_t fuid)
818 {
819 	char buf[32];
820 	uint64_t used, quota, usedobj, quotaobj;
821 	int err;
822 
823 	usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
824 	quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
825 
826 	if (quotaobj == 0 || zfsvfs->z_replay)
827 		return (B_FALSE);
828 
829 	(void) sprintf(buf, "%llx", (longlong_t)fuid);
830 	err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
831 	if (err != 0)
832 		return (B_FALSE);
833 
834 	err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
835 	if (err != 0)
836 		return (B_FALSE);
837 	return (used >= quota);
838 }
839 
840 boolean_t
841 zfs_owner_overquota(zfsvfs_t *zfsvfs, znode_t *zp, boolean_t isgroup)
842 {
843 	uint64_t fuid;
844 	uint64_t quotaobj;
845 
846 	quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
847 
848 	fuid = isgroup ? zp->z_gid : zp->z_uid;
849 
850 	if (quotaobj == 0 || zfsvfs->z_replay)
851 		return (B_FALSE);
852 
853 	return (zfs_fuid_overquota(zfsvfs, isgroup, fuid));
854 }
855 
856 int
857 zfsvfs_create(const char *osname, zfsvfs_t **zfvp)
858 {
859 	objset_t *os;
860 	zfsvfs_t *zfsvfs;
861 	uint64_t zval;
862 	int i, error;
863 	uint64_t sa_obj;
864 
865 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
866 
867 	/*
868 	 * We claim to always be readonly so we can open snapshots;
869 	 * other ZPL code will prevent us from writing to snapshots.
870 	 */
871 	error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zfsvfs, &os);
872 	if (error) {
873 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
874 		return (error);
875 	}
876 
877 	/*
878 	 * Initialize the zfs-specific filesystem structure.
879 	 * Should probably make this a kmem cache, shuffle fields,
880 	 * and just bzero up to z_hold_mtx[].
881 	 */
882 	zfsvfs->z_vfs = NULL;
883 	zfsvfs->z_parent = zfsvfs;
884 	zfsvfs->z_max_blksz = SPA_OLD_MAXBLOCKSIZE;
885 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
886 	zfsvfs->z_os = os;
887 
888 	error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
889 	if (error) {
890 		goto out;
891 	} else if (zfsvfs->z_version >
892 	    zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
893 		(void) printf("Can't mount a version %lld file system "
894 		    "on a version %lld pool\n. Pool must be upgraded to mount "
895 		    "this file system.", (u_longlong_t)zfsvfs->z_version,
896 		    (u_longlong_t)spa_version(dmu_objset_spa(os)));
897 		error = SET_ERROR(ENOTSUP);
898 		goto out;
899 	}
900 	if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
901 		goto out;
902 	zfsvfs->z_norm = (int)zval;
903 
904 	if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
905 		goto out;
906 	zfsvfs->z_utf8 = (zval != 0);
907 
908 	if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
909 		goto out;
910 	zfsvfs->z_case = (uint_t)zval;
911 
912 	/*
913 	 * Fold case on file systems that are always or sometimes case
914 	 * insensitive.
915 	 */
916 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
917 	    zfsvfs->z_case == ZFS_CASE_MIXED)
918 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
919 
920 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
921 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
922 
923 	if (zfsvfs->z_use_sa) {
924 		/* should either have both of these objects or none */
925 		error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
926 		    &sa_obj);
927 		if (error)
928 			goto out;
929 	} else {
930 		/*
931 		 * Pre SA versions file systems should never touch
932 		 * either the attribute registration or layout objects.
933 		 */
934 		sa_obj = 0;
935 	}
936 
937 	error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
938 	    &zfsvfs->z_attr_table);
939 	if (error)
940 		goto out;
941 
942 	if (zfsvfs->z_version >= ZPL_VERSION_SA)
943 		sa_register_update_callback(os, zfs_sa_upgrade);
944 
945 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
946 	    &zfsvfs->z_root);
947 	if (error)
948 		goto out;
949 	ASSERT(zfsvfs->z_root != 0);
950 
951 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
952 	    &zfsvfs->z_unlinkedobj);
953 	if (error)
954 		goto out;
955 
956 	error = zap_lookup(os, MASTER_NODE_OBJ,
957 	    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
958 	    8, 1, &zfsvfs->z_userquota_obj);
959 	if (error && error != ENOENT)
960 		goto out;
961 
962 	error = zap_lookup(os, MASTER_NODE_OBJ,
963 	    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
964 	    8, 1, &zfsvfs->z_groupquota_obj);
965 	if (error && error != ENOENT)
966 		goto out;
967 
968 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
969 	    &zfsvfs->z_fuid_obj);
970 	if (error && error != ENOENT)
971 		goto out;
972 
973 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
974 	    &zfsvfs->z_shares_dir);
975 	if (error && error != ENOENT)
976 		goto out;
977 
978 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
979 	mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
980 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
981 	    offsetof(znode_t, z_link_node));
982 	rrm_init(&zfsvfs->z_teardown_lock, B_FALSE);
983 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
984 	rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
985 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
986 		mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
987 
988 	*zfvp = zfsvfs;
989 	return (0);
990 
991 out:
992 	dmu_objset_disown(os, zfsvfs);
993 	*zfvp = NULL;
994 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
995 	return (error);
996 }
997 
998 static int
999 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
1000 {
1001 	int error;
1002 
1003 	error = zfs_register_callbacks(zfsvfs->z_vfs);
1004 	if (error)
1005 		return (error);
1006 
1007 	/*
1008 	 * Set the objset user_ptr to track its zfsvfs.
1009 	 */
1010 	mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1011 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1012 	mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1013 
1014 	zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
1015 
1016 	/*
1017 	 * If we are not mounting (ie: online recv), then we don't
1018 	 * have to worry about replaying the log as we blocked all
1019 	 * operations out since we closed the ZIL.
1020 	 */
1021 	if (mounting) {
1022 		boolean_t readonly;
1023 
1024 		/*
1025 		 * During replay we remove the read only flag to
1026 		 * allow replays to succeed.
1027 		 */
1028 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
1029 		if (readonly != 0)
1030 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
1031 		else
1032 			zfs_unlinked_drain(zfsvfs);
1033 
1034 		/*
1035 		 * Parse and replay the intent log.
1036 		 *
1037 		 * Because of ziltest, this must be done after
1038 		 * zfs_unlinked_drain().  (Further note: ziltest
1039 		 * doesn't use readonly mounts, where
1040 		 * zfs_unlinked_drain() isn't called.)  This is because
1041 		 * ziltest causes spa_sync() to think it's committed,
1042 		 * but actually it is not, so the intent log contains
1043 		 * many txg's worth of changes.
1044 		 *
1045 		 * In particular, if object N is in the unlinked set in
1046 		 * the last txg to actually sync, then it could be
1047 		 * actually freed in a later txg and then reallocated
1048 		 * in a yet later txg.  This would write a "create
1049 		 * object N" record to the intent log.  Normally, this
1050 		 * would be fine because the spa_sync() would have
1051 		 * written out the fact that object N is free, before
1052 		 * we could write the "create object N" intent log
1053 		 * record.
1054 		 *
1055 		 * But when we are in ziltest mode, we advance the "open
1056 		 * txg" without actually spa_sync()-ing the changes to
1057 		 * disk.  So we would see that object N is still
1058 		 * allocated and in the unlinked set, and there is an
1059 		 * intent log record saying to allocate it.
1060 		 */
1061 		if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
1062 			if (zil_replay_disable) {
1063 				zil_destroy(zfsvfs->z_log, B_FALSE);
1064 			} else {
1065 				zfsvfs->z_replay = B_TRUE;
1066 				zil_replay(zfsvfs->z_os, zfsvfs,
1067 				    zfs_replay_vector);
1068 				zfsvfs->z_replay = B_FALSE;
1069 			}
1070 		}
1071 		zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
1072 	}
1073 
1074 	return (0);
1075 }
1076 
1077 void
1078 zfsvfs_free(zfsvfs_t *zfsvfs)
1079 {
1080 	int i;
1081 	extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
1082 
1083 	/*
1084 	 * This is a barrier to prevent the filesystem from going away in
1085 	 * zfs_znode_move() until we can safely ensure that the filesystem is
1086 	 * not unmounted. We consider the filesystem valid before the barrier
1087 	 * and invalid after the barrier.
1088 	 */
1089 	rw_enter(&zfsvfs_lock, RW_READER);
1090 	rw_exit(&zfsvfs_lock);
1091 
1092 	zfs_fuid_destroy(zfsvfs);
1093 
1094 	mutex_destroy(&zfsvfs->z_znodes_lock);
1095 	mutex_destroy(&zfsvfs->z_lock);
1096 	list_destroy(&zfsvfs->z_all_znodes);
1097 	rrm_destroy(&zfsvfs->z_teardown_lock);
1098 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
1099 	rw_destroy(&zfsvfs->z_fuid_lock);
1100 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1101 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1102 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1103 }
1104 
1105 static void
1106 zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
1107 {
1108 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1109 	if (zfsvfs->z_vfs) {
1110 		if (zfsvfs->z_use_fuids) {
1111 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1112 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1113 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1114 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1115 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1116 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1117 		} else {
1118 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1119 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1120 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1121 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1122 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1123 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1124 		}
1125 	}
1126 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1127 }
1128 
1129 static int
1130 zfs_domount(vfs_t *vfsp, char *osname)
1131 {
1132 	dev_t mount_dev;
1133 	uint64_t recordsize, fsid_guid;
1134 	int error = 0;
1135 	zfsvfs_t *zfsvfs;
1136 
1137 	ASSERT(vfsp);
1138 	ASSERT(osname);
1139 
1140 	error = zfsvfs_create(osname, &zfsvfs);
1141 	if (error)
1142 		return (error);
1143 	zfsvfs->z_vfs = vfsp;
1144 
1145 	/* Initialize the generic filesystem structure. */
1146 	vfsp->vfs_bcount = 0;
1147 	vfsp->vfs_data = NULL;
1148 
1149 	if (zfs_create_unique_device(&mount_dev) == -1) {
1150 		error = SET_ERROR(ENODEV);
1151 		goto out;
1152 	}
1153 	ASSERT(vfs_devismounted(mount_dev) == 0);
1154 
1155 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
1156 	    NULL))
1157 		goto out;
1158 
1159 	vfsp->vfs_dev = mount_dev;
1160 	vfsp->vfs_fstype = zfsfstype;
1161 	vfsp->vfs_bsize = recordsize;
1162 	vfsp->vfs_flag |= VFS_NOTRUNC;
1163 	vfsp->vfs_data = zfsvfs;
1164 
1165 	/*
1166 	 * The fsid is 64 bits, composed of an 8-bit fs type, which
1167 	 * separates our fsid from any other filesystem types, and a
1168 	 * 56-bit objset unique ID.  The objset unique ID is unique to
1169 	 * all objsets open on this system, provided by unique_create().
1170 	 * The 8-bit fs type must be put in the low bits of fsid[1]
1171 	 * because that's where other Solaris filesystems put it.
1172 	 */
1173 	fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
1174 	ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
1175 	vfsp->vfs_fsid.val[0] = fsid_guid;
1176 	vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
1177 	    zfsfstype & 0xFF;
1178 
1179 	/*
1180 	 * Set features for file system.
1181 	 */
1182 	zfs_set_fuid_feature(zfsvfs);
1183 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
1184 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1185 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1186 		vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
1187 	} else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
1188 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1189 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1190 	}
1191 	vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
1192 
1193 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1194 		uint64_t pval;
1195 
1196 		atime_changed_cb(zfsvfs, B_FALSE);
1197 		readonly_changed_cb(zfsvfs, B_TRUE);
1198 		if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
1199 			goto out;
1200 		xattr_changed_cb(zfsvfs, pval);
1201 		zfsvfs->z_issnap = B_TRUE;
1202 		zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1203 
1204 		mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1205 		dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1206 		mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1207 	} else {
1208 		error = zfsvfs_setup(zfsvfs, B_TRUE);
1209 	}
1210 
1211 	if (!zfsvfs->z_issnap)
1212 		zfsctl_create(zfsvfs);
1213 out:
1214 	if (error) {
1215 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1216 		zfsvfs_free(zfsvfs);
1217 	} else {
1218 		atomic_inc_32(&zfs_active_fs_count);
1219 	}
1220 
1221 	return (error);
1222 }
1223 
1224 void
1225 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
1226 {
1227 	objset_t *os = zfsvfs->z_os;
1228 
1229 	if (!dmu_objset_is_snapshot(os))
1230 		dsl_prop_unregister_all(dmu_objset_ds(os), zfsvfs);
1231 }
1232 
1233 /*
1234  * Convert a decimal digit string to a uint64_t integer.
1235  */
1236 static int
1237 str_to_uint64(char *str, uint64_t *objnum)
1238 {
1239 	uint64_t num = 0;
1240 
1241 	while (*str) {
1242 		if (*str < '0' || *str > '9')
1243 			return (SET_ERROR(EINVAL));
1244 
1245 		num = num*10 + *str++ - '0';
1246 	}
1247 
1248 	*objnum = num;
1249 	return (0);
1250 }
1251 
1252 /*
1253  * The boot path passed from the boot loader is in the form of
1254  * "rootpool-name/root-filesystem-object-number'. Convert this
1255  * string to a dataset name: "rootpool-name/root-filesystem-name".
1256  */
1257 static int
1258 zfs_parse_bootfs(char *bpath, char *outpath)
1259 {
1260 	char *slashp;
1261 	uint64_t objnum;
1262 	int error;
1263 
1264 	if (*bpath == 0 || *bpath == '/')
1265 		return (SET_ERROR(EINVAL));
1266 
1267 	(void) strcpy(outpath, bpath);
1268 
1269 	slashp = strchr(bpath, '/');
1270 
1271 	/* if no '/', just return the pool name */
1272 	if (slashp == NULL) {
1273 		return (0);
1274 	}
1275 
1276 	/* if not a number, just return the root dataset name */
1277 	if (str_to_uint64(slashp+1, &objnum)) {
1278 		return (0);
1279 	}
1280 
1281 	*slashp = '\0';
1282 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
1283 	*slashp = '/';
1284 
1285 	return (error);
1286 }
1287 
1288 /*
1289  * Check that the hex label string is appropriate for the dataset being
1290  * mounted into the global_zone proper.
1291  *
1292  * Return an error if the hex label string is not default or
1293  * admin_low/admin_high.  For admin_low labels, the corresponding
1294  * dataset must be readonly.
1295  */
1296 int
1297 zfs_check_global_label(const char *dsname, const char *hexsl)
1298 {
1299 	if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1300 		return (0);
1301 	if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
1302 		return (0);
1303 	if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
1304 		/* must be readonly */
1305 		uint64_t rdonly;
1306 
1307 		if (dsl_prop_get_integer(dsname,
1308 		    zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
1309 			return (SET_ERROR(EACCES));
1310 		return (rdonly ? 0 : EACCES);
1311 	}
1312 	return (SET_ERROR(EACCES));
1313 }
1314 
1315 /*
1316  * Determine whether the mount is allowed according to MAC check.
1317  * by comparing (where appropriate) label of the dataset against
1318  * the label of the zone being mounted into.  If the dataset has
1319  * no label, create one.
1320  *
1321  * Returns 0 if access allowed, error otherwise (e.g. EACCES)
1322  */
1323 static int
1324 zfs_mount_label_policy(vfs_t *vfsp, char *osname)
1325 {
1326 	int		error, retv;
1327 	zone_t		*mntzone = NULL;
1328 	ts_label_t	*mnt_tsl;
1329 	bslabel_t	*mnt_sl;
1330 	bslabel_t	ds_sl;
1331 	char		ds_hexsl[MAXNAMELEN];
1332 
1333 	retv = EACCES;				/* assume the worst */
1334 
1335 	/*
1336 	 * Start by getting the dataset label if it exists.
1337 	 */
1338 	error = dsl_prop_get(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1339 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
1340 	if (error)
1341 		return (SET_ERROR(EACCES));
1342 
1343 	/*
1344 	 * If labeling is NOT enabled, then disallow the mount of datasets
1345 	 * which have a non-default label already.  No other label checks
1346 	 * are needed.
1347 	 */
1348 	if (!is_system_labeled()) {
1349 		if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1350 			return (0);
1351 		return (SET_ERROR(EACCES));
1352 	}
1353 
1354 	/*
1355 	 * Get the label of the mountpoint.  If mounting into the global
1356 	 * zone (i.e. mountpoint is not within an active zone and the
1357 	 * zoned property is off), the label must be default or
1358 	 * admin_low/admin_high only; no other checks are needed.
1359 	 */
1360 	mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE);
1361 	if (mntzone->zone_id == GLOBAL_ZONEID) {
1362 		uint64_t zoned;
1363 
1364 		zone_rele(mntzone);
1365 
1366 		if (dsl_prop_get_integer(osname,
1367 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
1368 			return (SET_ERROR(EACCES));
1369 		if (!zoned)
1370 			return (zfs_check_global_label(osname, ds_hexsl));
1371 		else
1372 			/*
1373 			 * This is the case of a zone dataset being mounted
1374 			 * initially, before the zone has been fully created;
1375 			 * allow this mount into global zone.
1376 			 */
1377 			return (0);
1378 	}
1379 
1380 	mnt_tsl = mntzone->zone_slabel;
1381 	ASSERT(mnt_tsl != NULL);
1382 	label_hold(mnt_tsl);
1383 	mnt_sl = label2bslabel(mnt_tsl);
1384 
1385 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) {
1386 		/*
1387 		 * The dataset doesn't have a real label, so fabricate one.
1388 		 */
1389 		char *str = NULL;
1390 
1391 		if (l_to_str_internal(mnt_sl, &str) == 0 &&
1392 		    dsl_prop_set_string(osname,
1393 		    zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1394 		    ZPROP_SRC_LOCAL, str) == 0)
1395 			retv = 0;
1396 		if (str != NULL)
1397 			kmem_free(str, strlen(str) + 1);
1398 	} else if (hexstr_to_label(ds_hexsl, &ds_sl) == 0) {
1399 		/*
1400 		 * Now compare labels to complete the MAC check.  If the
1401 		 * labels are equal then allow access.  If the mountpoint
1402 		 * label dominates the dataset label, allow readonly access.
1403 		 * Otherwise, access is denied.
1404 		 */
1405 		if (blequal(mnt_sl, &ds_sl))
1406 			retv = 0;
1407 		else if (bldominates(mnt_sl, &ds_sl)) {
1408 			vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
1409 			retv = 0;
1410 		}
1411 	}
1412 
1413 	label_rele(mnt_tsl);
1414 	zone_rele(mntzone);
1415 	return (retv);
1416 }
1417 
1418 static int
1419 zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
1420 {
1421 	int error = 0;
1422 	static int zfsrootdone = 0;
1423 	zfsvfs_t *zfsvfs = NULL;
1424 	znode_t *zp = NULL;
1425 	vnode_t *vp = NULL;
1426 	char *zfs_bootfs;
1427 	char *zfs_devid;
1428 
1429 	ASSERT(vfsp);
1430 
1431 	/*
1432 	 * The filesystem that we mount as root is defined in the
1433 	 * boot property "zfs-bootfs" with a format of
1434 	 * "poolname/root-dataset-objnum".
1435 	 */
1436 	if (why == ROOT_INIT) {
1437 		if (zfsrootdone++)
1438 			return (SET_ERROR(EBUSY));
1439 		/*
1440 		 * the process of doing a spa_load will require the
1441 		 * clock to be set before we could (for example) do
1442 		 * something better by looking at the timestamp on
1443 		 * an uberblock, so just set it to -1.
1444 		 */
1445 		clkset(-1);
1446 
1447 		if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
1448 			cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
1449 			    "bootfs name");
1450 			return (SET_ERROR(EINVAL));
1451 		}
1452 		zfs_devid = spa_get_bootprop("diskdevid");
1453 		error = spa_import_rootpool(rootfs.bo_name, zfs_devid);
1454 		if (zfs_devid)
1455 			spa_free_bootprop(zfs_devid);
1456 		if (error) {
1457 			spa_free_bootprop(zfs_bootfs);
1458 			cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
1459 			    error);
1460 			return (error);
1461 		}
1462 		if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
1463 			spa_free_bootprop(zfs_bootfs);
1464 			cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
1465 			    error);
1466 			return (error);
1467 		}
1468 
1469 		spa_free_bootprop(zfs_bootfs);
1470 
1471 		if (error = vfs_lock(vfsp))
1472 			return (error);
1473 
1474 		if (error = zfs_domount(vfsp, rootfs.bo_name)) {
1475 			cmn_err(CE_NOTE, "zfs_domount: error %d", error);
1476 			goto out;
1477 		}
1478 
1479 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
1480 		ASSERT(zfsvfs);
1481 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
1482 			cmn_err(CE_NOTE, "zfs_zget: error %d", error);
1483 			goto out;
1484 		}
1485 
1486 		vp = ZTOV(zp);
1487 		mutex_enter(&vp->v_lock);
1488 		vp->v_flag |= VROOT;
1489 		mutex_exit(&vp->v_lock);
1490 		rootvp = vp;
1491 
1492 		/*
1493 		 * Leave rootvp held.  The root file system is never unmounted.
1494 		 */
1495 
1496 		vfs_add((struct vnode *)0, vfsp,
1497 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
1498 out:
1499 		vfs_unlock(vfsp);
1500 		return (error);
1501 	} else if (why == ROOT_REMOUNT) {
1502 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
1503 		vfsp->vfs_flag |= VFS_REMOUNT;
1504 
1505 		/* refresh mount options */
1506 		zfs_unregister_callbacks(vfsp->vfs_data);
1507 		return (zfs_register_callbacks(vfsp));
1508 
1509 	} else if (why == ROOT_UNMOUNT) {
1510 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
1511 		(void) zfs_sync(vfsp, 0, 0);
1512 		return (0);
1513 	}
1514 
1515 	/*
1516 	 * if "why" is equal to anything else other than ROOT_INIT,
1517 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
1518 	 */
1519 	return (SET_ERROR(ENOTSUP));
1520 }
1521 
1522 /*ARGSUSED*/
1523 static int
1524 zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
1525 {
1526 	char		*osname;
1527 	pathname_t	spn;
1528 	int		error = 0;
1529 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
1530 	    UIO_SYSSPACE : UIO_USERSPACE;
1531 	int		canwrite;
1532 
1533 	if (mvp->v_type != VDIR)
1534 		return (SET_ERROR(ENOTDIR));
1535 
1536 	mutex_enter(&mvp->v_lock);
1537 	if ((uap->flags & MS_REMOUNT) == 0 &&
1538 	    (uap->flags & MS_OVERLAY) == 0 &&
1539 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
1540 		mutex_exit(&mvp->v_lock);
1541 		return (SET_ERROR(EBUSY));
1542 	}
1543 	mutex_exit(&mvp->v_lock);
1544 
1545 	/*
1546 	 * ZFS does not support passing unparsed data in via MS_DATA.
1547 	 * Users should use the MS_OPTIONSTR interface; this means
1548 	 * that all option parsing is already done and the options struct
1549 	 * can be interrogated.
1550 	 */
1551 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
1552 		return (SET_ERROR(EINVAL));
1553 
1554 	/*
1555 	 * Get the objset name (the "special" mount argument).
1556 	 */
1557 	if (error = pn_get(uap->spec, fromspace, &spn))
1558 		return (error);
1559 
1560 	osname = spn.pn_path;
1561 
1562 	/*
1563 	 * Check for mount privilege?
1564 	 *
1565 	 * If we don't have privilege then see if
1566 	 * we have local permission to allow it
1567 	 */
1568 	error = secpolicy_fs_mount(cr, mvp, vfsp);
1569 	if (error) {
1570 		if (dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) == 0) {
1571 			vattr_t		vattr;
1572 
1573 			/*
1574 			 * Make sure user is the owner of the mount point
1575 			 * or has sufficient privileges.
1576 			 */
1577 
1578 			vattr.va_mask = AT_UID;
1579 
1580 			if (VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
1581 				goto out;
1582 			}
1583 
1584 			if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 &&
1585 			    VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) {
1586 				goto out;
1587 			}
1588 			secpolicy_fs_mount_clearopts(cr, vfsp);
1589 		} else {
1590 			goto out;
1591 		}
1592 	}
1593 
1594 	/*
1595 	 * Refuse to mount a filesystem if we are in a local zone and the
1596 	 * dataset is not visible.
1597 	 */
1598 	if (!INGLOBALZONE(curproc) &&
1599 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1600 		error = SET_ERROR(EPERM);
1601 		goto out;
1602 	}
1603 
1604 	error = zfs_mount_label_policy(vfsp, osname);
1605 	if (error)
1606 		goto out;
1607 
1608 	/*
1609 	 * When doing a remount, we simply refresh our temporary properties
1610 	 * according to those options set in the current VFS options.
1611 	 */
1612 	if (uap->flags & MS_REMOUNT) {
1613 		/* refresh mount options */
1614 		zfs_unregister_callbacks(vfsp->vfs_data);
1615 		error = zfs_register_callbacks(vfsp);
1616 		goto out;
1617 	}
1618 
1619 	error = zfs_domount(vfsp, osname);
1620 
1621 	/*
1622 	 * Add an extra VFS_HOLD on our parent vfs so that it can't
1623 	 * disappear due to a forced unmount.
1624 	 */
1625 	if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap)
1626 		VFS_HOLD(mvp->v_vfsp);
1627 
1628 out:
1629 	if (error == 0) {
1630 		rw_enter(&rz_zev_rwlock, RW_READER);
1631 		if (rz_zev_callbacks && rz_zev_callbacks->rz_zev_zfs_mount)
1632 			rz_zev_callbacks->rz_zev_zfs_mount(vfsp, mvp, osname,
1633 				uap->flags & MS_REMOUNT ?  B_TRUE : B_FALSE);
1634 		rw_exit(&rz_zev_rwlock);
1635 	}
1636 	pn_free(&spn);
1637 	return (error);
1638 }
1639 
1640 static int
1641 zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1642 {
1643 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1644 	dev32_t d32;
1645 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
1646 
1647 	ZFS_ENTER(zfsvfs);
1648 
1649 	dmu_objset_space(zfsvfs->z_os,
1650 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
1651 
1652 	/*
1653 	 * The underlying storage pool actually uses multiple block sizes.
1654 	 * We report the fragsize as the smallest block size we support,
1655 	 * and we report our blocksize as the filesystem's maximum blocksize.
1656 	 */
1657 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1658 	statp->f_bsize = zfsvfs->z_max_blksz;
1659 
1660 	/*
1661 	 * The following report "total" blocks of various kinds in the
1662 	 * file system, but reported in terms of f_frsize - the
1663 	 * "fragment" size.
1664 	 */
1665 
1666 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
1667 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
1668 	statp->f_bavail = statp->f_bfree; /* no root reservation */
1669 
1670 	/*
1671 	 * statvfs() should really be called statufs(), because it assumes
1672 	 * static metadata.  ZFS doesn't preallocate files, so the best
1673 	 * we can do is report the max that could possibly fit in f_files,
1674 	 * and that minus the number actually used in f_ffree.
1675 	 * For f_ffree, report the smaller of the number of object available
1676 	 * and the number of blocks (each object will take at least a block).
1677 	 */
1678 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
1679 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
1680 	statp->f_files = statp->f_ffree + usedobjs;
1681 
1682 	(void) cmpldev(&d32, vfsp->vfs_dev);
1683 	statp->f_fsid = d32;
1684 
1685 	/*
1686 	 * We're a zfs filesystem.
1687 	 */
1688 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1689 
1690 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1691 
1692 	statp->f_namemax = ZFS_MAXNAMELEN;
1693 
1694 	/*
1695 	 * We have all of 32 characters to stuff a string here.
1696 	 * Is there anything useful we could/should provide?
1697 	 */
1698 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
1699 
1700 	ZFS_EXIT(zfsvfs);
1701 	return (0);
1702 }
1703 
1704 static int
1705 zfs_root(vfs_t *vfsp, vnode_t **vpp)
1706 {
1707 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1708 	znode_t *rootzp;
1709 	int error;
1710 
1711 	ZFS_ENTER(zfsvfs);
1712 
1713 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1714 	if (error == 0)
1715 		*vpp = ZTOV(rootzp);
1716 
1717 	ZFS_EXIT(zfsvfs);
1718 	return (error);
1719 }
1720 
1721 /*
1722  * Teardown the zfsvfs::z_os.
1723  *
1724  * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1725  * and 'z_teardown_inactive_lock' held.
1726  */
1727 static int
1728 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1729 {
1730 	znode_t	*zp;
1731 
1732 	rrm_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1733 
1734 	if (!unmounting) {
1735 		/*
1736 		 * We purge the parent filesystem's vfsp as the parent
1737 		 * filesystem and all of its snapshots have their vnode's
1738 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
1739 		 * 'z_parent' is self referential for non-snapshots.
1740 		 */
1741 		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1742 	}
1743 
1744 	/*
1745 	 * Close the zil. NB: Can't close the zil while zfs_inactive
1746 	 * threads are blocked as zil_close can call zfs_inactive.
1747 	 */
1748 	if (zfsvfs->z_log) {
1749 		zil_close(zfsvfs->z_log);
1750 		zfsvfs->z_log = NULL;
1751 	}
1752 
1753 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
1754 
1755 	/*
1756 	 * If we are not unmounting (ie: online recv) and someone already
1757 	 * unmounted this file system while we were doing the switcheroo,
1758 	 * or a reopen of z_os failed then just bail out now.
1759 	 */
1760 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1761 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
1762 		rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1763 		return (SET_ERROR(EIO));
1764 	}
1765 
1766 	/*
1767 	 * At this point there are no vops active, and any new vops will
1768 	 * fail with EIO since we have z_teardown_lock for writer (only
1769 	 * relavent for forced unmount).
1770 	 *
1771 	 * Release all holds on dbufs.
1772 	 */
1773 	mutex_enter(&zfsvfs->z_znodes_lock);
1774 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
1775 	    zp = list_next(&zfsvfs->z_all_znodes, zp))
1776 		if (zp->z_sa_hdl) {
1777 			ASSERT(ZTOV(zp)->v_count > 0);
1778 			zfs_znode_dmu_fini(zp);
1779 		}
1780 	mutex_exit(&zfsvfs->z_znodes_lock);
1781 
1782 	/*
1783 	 * If we are unmounting, set the unmounted flag and let new vops
1784 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
1785 	 * other vops will fail with EIO.
1786 	 */
1787 	if (unmounting) {
1788 		zfsvfs->z_unmounted = B_TRUE;
1789 		rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1790 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
1791 	}
1792 
1793 	/*
1794 	 * z_os will be NULL if there was an error in attempting to reopen
1795 	 * zfsvfs, so just return as the properties had already been
1796 	 * unregistered and cached data had been evicted before.
1797 	 */
1798 	if (zfsvfs->z_os == NULL)
1799 		return (0);
1800 
1801 	/*
1802 	 * Unregister properties.
1803 	 */
1804 	zfs_unregister_callbacks(zfsvfs);
1805 
1806 	/*
1807 	 * Evict cached data
1808 	 */
1809 	if (dsl_dataset_is_dirty(dmu_objset_ds(zfsvfs->z_os)) &&
1810 	    !(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY))
1811 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1812 	dmu_objset_evict_dbufs(zfsvfs->z_os);
1813 
1814 	return (0);
1815 }
1816 
1817 /*ARGSUSED*/
1818 static int
1819 zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1820 {
1821 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1822 	objset_t *os;
1823 	int ret;
1824 
1825 	ret = secpolicy_fs_unmount(cr, vfsp);
1826 	if (ret) {
1827 		if (dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
1828 		    ZFS_DELEG_PERM_MOUNT, cr))
1829 			return (ret);
1830 	}
1831 
1832 	/*
1833 	 * We purge the parent filesystem's vfsp as the parent filesystem
1834 	 * and all of its snapshots have their vnode's v_vfsp set to the
1835 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
1836 	 * referential for non-snapshots.
1837 	 */
1838 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1839 
1840 	/*
1841 	 * Unmount any snapshots mounted under .zfs before unmounting the
1842 	 * dataset itself.
1843 	 */
1844 	if (zfsvfs->z_ctldir != NULL &&
1845 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1846 		return (ret);
1847 	}
1848 
1849 	if (!(fflag & MS_FORCE)) {
1850 		/*
1851 		 * Check the number of active vnodes in the file system.
1852 		 * Our count is maintained in the vfs structure, but the
1853 		 * number is off by 1 to indicate a hold on the vfs
1854 		 * structure itself.
1855 		 *
1856 		 * The '.zfs' directory maintains a reference of its
1857 		 * own, and any active references underneath are
1858 		 * reflected in the vnode count.
1859 		 */
1860 		if (zfsvfs->z_ctldir == NULL) {
1861 			if (vfsp->vfs_count > 1)
1862 				return (SET_ERROR(EBUSY));
1863 		} else {
1864 			if (vfsp->vfs_count > 2 ||
1865 			    zfsvfs->z_ctldir->v_count > 1)
1866 				return (SET_ERROR(EBUSY));
1867 		}
1868 	}
1869 
1870 	vfsp->vfs_flag |= VFS_UNMOUNTED;
1871 
1872 	rw_enter(&rz_zev_rwlock, RW_READER);
1873 	if (rz_zev_callbacks && rz_zev_callbacks->rz_zev_zfs_umount)
1874 		rz_zev_callbacks->rz_zev_zfs_umount(vfsp);
1875 	rw_exit(&rz_zev_rwlock);
1876 
1877 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1878 	os = zfsvfs->z_os;
1879 
1880 	/*
1881 	 * z_os will be NULL if there was an error in
1882 	 * attempting to reopen zfsvfs.
1883 	 */
1884 	if (os != NULL) {
1885 		/*
1886 		 * Unset the objset user_ptr.
1887 		 */
1888 		mutex_enter(&os->os_user_ptr_lock);
1889 		dmu_objset_set_user(os, NULL);
1890 		mutex_exit(&os->os_user_ptr_lock);
1891 
1892 		/*
1893 		 * Finally release the objset
1894 		 */
1895 		dmu_objset_disown(os, zfsvfs);
1896 	}
1897 
1898 	/*
1899 	 * We can now safely destroy the '.zfs' directory node.
1900 	 */
1901 	if (zfsvfs->z_ctldir != NULL)
1902 		zfsctl_destroy(zfsvfs);
1903 
1904 	return (0);
1905 }
1906 
1907 static int
1908 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1909 {
1910 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1911 	znode_t		*zp;
1912 	uint64_t	object = 0;
1913 	uint64_t	fid_gen = 0;
1914 	uint64_t	gen_mask;
1915 	uint64_t	zp_gen;
1916 	int 		i, err;
1917 
1918 	*vpp = NULL;
1919 
1920 	ZFS_ENTER(zfsvfs);
1921 
1922 	if (fidp->fid_len == LONG_FID_LEN) {
1923 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1924 		uint64_t	objsetid = 0;
1925 		uint64_t	setgen = 0;
1926 
1927 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1928 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1929 
1930 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1931 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1932 
1933 		ZFS_EXIT(zfsvfs);
1934 
1935 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1936 		if (err)
1937 			return (SET_ERROR(EINVAL));
1938 		ZFS_ENTER(zfsvfs);
1939 	}
1940 
1941 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1942 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1943 
1944 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1945 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1946 
1947 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1948 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1949 	} else {
1950 		ZFS_EXIT(zfsvfs);
1951 		return (SET_ERROR(EINVAL));
1952 	}
1953 
1954 	/* A zero fid_gen means we are in the .zfs control directories */
1955 	if (fid_gen == 0 &&
1956 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1957 		*vpp = zfsvfs->z_ctldir;
1958 		ASSERT(*vpp != NULL);
1959 		if (object == ZFSCTL_INO_SNAPDIR) {
1960 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1961 			    0, NULL, NULL, NULL, NULL, NULL) == 0);
1962 		} else {
1963 			VN_HOLD(*vpp);
1964 		}
1965 		ZFS_EXIT(zfsvfs);
1966 		return (0);
1967 	}
1968 
1969 	gen_mask = -1ULL >> (64 - 8 * i);
1970 
1971 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1972 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1973 		ZFS_EXIT(zfsvfs);
1974 		return (err);
1975 	}
1976 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
1977 	    sizeof (uint64_t));
1978 	zp_gen = zp_gen & gen_mask;
1979 	if (zp_gen == 0)
1980 		zp_gen = 1;
1981 	if (zp->z_unlinked || zp_gen != fid_gen) {
1982 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1983 		VN_RELE(ZTOV(zp));
1984 		ZFS_EXIT(zfsvfs);
1985 		return (SET_ERROR(EINVAL));
1986 	}
1987 
1988 	*vpp = ZTOV(zp);
1989 	ZFS_EXIT(zfsvfs);
1990 	return (0);
1991 }
1992 
1993 /*
1994  * Block out VOPs and close zfsvfs_t::z_os
1995  *
1996  * Note, if successful, then we return with the 'z_teardown_lock' and
1997  * 'z_teardown_inactive_lock' write held.  We leave ownership of the underlying
1998  * dataset and objset intact so that they can be atomically handed off during
1999  * a subsequent rollback or recv operation and the resume thereafter.
2000  */
2001 int
2002 zfs_suspend_fs(zfsvfs_t *zfsvfs)
2003 {
2004 	int error;
2005 
2006 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
2007 		return (error);
2008 
2009 	return (0);
2010 }
2011 
2012 /*
2013  * Rebuild SA and release VOPs.  Note that ownership of the underlying dataset
2014  * is an invariant across any of the operations that can be performed while the
2015  * filesystem was suspended.  Whether it succeeded or failed, the preconditions
2016  * are the same: the relevant objset and associated dataset are owned by
2017  * zfsvfs, held, and long held on entry.
2018  */
2019 int
2020 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname)
2021 {
2022 	int err;
2023 	znode_t *zp;
2024 	uint64_t sa_obj = 0;
2025 
2026 	ASSERT(RRM_WRITE_HELD(&zfsvfs->z_teardown_lock));
2027 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
2028 
2029 	/*
2030 	 * We already own this, so just hold and rele it to update the
2031 	 * objset_t, as the one we had before may have been evicted.
2032 	 */
2033 	VERIFY0(dmu_objset_hold(osname, zfsvfs, &zfsvfs->z_os));
2034 	VERIFY3P(zfsvfs->z_os->os_dsl_dataset->ds_owner, ==, zfsvfs);
2035 	VERIFY(dsl_dataset_long_held(zfsvfs->z_os->os_dsl_dataset));
2036 	dmu_objset_rele(zfsvfs->z_os, zfsvfs);
2037 
2038 	/*
2039 	 * Make sure version hasn't changed
2040 	 */
2041 
2042 	err = zfs_get_zplprop(zfsvfs->z_os, ZFS_PROP_VERSION,
2043 	    &zfsvfs->z_version);
2044 
2045 	if (err)
2046 		goto bail;
2047 
2048 	err = zap_lookup(zfsvfs->z_os, MASTER_NODE_OBJ,
2049 	    ZFS_SA_ATTRS, 8, 1, &sa_obj);
2050 
2051 	if (err && zfsvfs->z_version >= ZPL_VERSION_SA)
2052 		goto bail;
2053 
2054 	if ((err = sa_setup(zfsvfs->z_os, sa_obj,
2055 	    zfs_attr_table,  ZPL_END, &zfsvfs->z_attr_table)) != 0)
2056 		goto bail;
2057 
2058 	if (zfsvfs->z_version >= ZPL_VERSION_SA)
2059 		sa_register_update_callback(zfsvfs->z_os,
2060 		    zfs_sa_upgrade);
2061 
2062 	VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
2063 
2064 	zfs_set_fuid_feature(zfsvfs);
2065 
2066 	/*
2067 	 * Attempt to re-establish all the active znodes with
2068 	 * their dbufs.  If a zfs_rezget() fails, then we'll let
2069 	 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
2070 	 * when they try to use their znode.
2071 	 */
2072 	mutex_enter(&zfsvfs->z_znodes_lock);
2073 	for (zp = list_head(&zfsvfs->z_all_znodes); zp;
2074 	    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
2075 		(void) zfs_rezget(zp);
2076 	}
2077 	mutex_exit(&zfsvfs->z_znodes_lock);
2078 
2079 bail:
2080 	/* release the VOPs */
2081 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
2082 	rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2083 
2084 	if (err) {
2085 		/*
2086 		 * Since we couldn't setup the sa framework, try to force
2087 		 * unmount this file system.
2088 		 */
2089 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
2090 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
2091 	}
2092 	return (err);
2093 }
2094 
2095 static void
2096 zfs_freevfs(vfs_t *vfsp)
2097 {
2098 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
2099 
2100 	/*
2101 	 * If this is a snapshot, we have an extra VFS_HOLD on our parent
2102 	 * from zfs_mount().  Release it here.  If we came through
2103 	 * zfs_mountroot() instead, we didn't grab an extra hold, so
2104 	 * skip the VFS_RELE for rootvfs.
2105 	 */
2106 	if (zfsvfs->z_issnap && (vfsp != rootvfs))
2107 		VFS_RELE(zfsvfs->z_parent->z_vfs);
2108 
2109 	zfsvfs_free(zfsvfs);
2110 
2111 	atomic_dec_32(&zfs_active_fs_count);
2112 }
2113 
2114 /*
2115  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
2116  * so we can't safely do any non-idempotent initialization here.
2117  * Leave that to zfs_init() and zfs_fini(), which are called
2118  * from the module's _init() and _fini() entry points.
2119  */
2120 /*ARGSUSED*/
2121 static int
2122 zfs_vfsinit(int fstype, char *name)
2123 {
2124 	int error;
2125 
2126 	zfsfstype = fstype;
2127 
2128 	/*
2129 	 * Setup vfsops and vnodeops tables.
2130 	 */
2131 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
2132 	if (error != 0) {
2133 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
2134 	}
2135 
2136 	error = zfs_create_op_tables();
2137 	if (error) {
2138 		zfs_remove_op_tables();
2139 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
2140 		(void) vfs_freevfsops_by_type(zfsfstype);
2141 		return (error);
2142 	}
2143 
2144 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
2145 
2146 	/*
2147 	 * Unique major number for all zfs mounts.
2148 	 * If we run out of 32-bit minors, we'll getudev() another major.
2149 	 */
2150 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
2151 	zfs_minor = ZFS_MIN_MINOR;
2152 
2153 	return (0);
2154 }
2155 
2156 void
2157 zfs_init(void)
2158 {
2159 	/*
2160 	 * Initialize .zfs directory structures
2161 	 */
2162 	zfsctl_init();
2163 
2164 	/*
2165 	 * Initialize znode cache, vnode ops, etc...
2166 	 */
2167 	zfs_znode_init();
2168 
2169 	dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
2170 }
2171 
2172 void
2173 zfs_fini(void)
2174 {
2175 	zfsctl_fini();
2176 	zfs_znode_fini();
2177 }
2178 
2179 int
2180 zfs_busy(void)
2181 {
2182 	return (zfs_active_fs_count != 0);
2183 }
2184 
2185 int
2186 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
2187 {
2188 	int error;
2189 	objset_t *os = zfsvfs->z_os;
2190 	dmu_tx_t *tx;
2191 
2192 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
2193 		return (SET_ERROR(EINVAL));
2194 
2195 	if (newvers < zfsvfs->z_version)
2196 		return (SET_ERROR(EINVAL));
2197 
2198 	if (zfs_spa_version_map(newvers) >
2199 	    spa_version(dmu_objset_spa(zfsvfs->z_os)))
2200 		return (SET_ERROR(ENOTSUP));
2201 
2202 	tx = dmu_tx_create(os);
2203 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
2204 	if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2205 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
2206 		    ZFS_SA_ATTRS);
2207 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2208 	}
2209 	error = dmu_tx_assign(tx, TXG_WAIT);
2210 	if (error) {
2211 		dmu_tx_abort(tx);
2212 		return (error);
2213 	}
2214 
2215 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
2216 	    8, 1, &newvers, tx);
2217 
2218 	if (error) {
2219 		dmu_tx_commit(tx);
2220 		return (error);
2221 	}
2222 
2223 	if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2224 		uint64_t sa_obj;
2225 
2226 		ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
2227 		    SPA_VERSION_SA);
2228 		sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
2229 		    DMU_OT_NONE, 0, tx);
2230 
2231 		error = zap_add(os, MASTER_NODE_OBJ,
2232 		    ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
2233 		ASSERT0(error);
2234 
2235 		VERIFY(0 == sa_set_sa_object(os, sa_obj));
2236 		sa_register_update_callback(os, zfs_sa_upgrade);
2237 	}
2238 
2239 	spa_history_log_internal_ds(dmu_objset_ds(os), "upgrade", tx,
2240 	    "from %llu to %llu", zfsvfs->z_version, newvers);
2241 
2242 	dmu_tx_commit(tx);
2243 
2244 	zfsvfs->z_version = newvers;
2245 
2246 	zfs_set_fuid_feature(zfsvfs);
2247 
2248 	return (0);
2249 }
2250 
2251 /*
2252  * Read a property stored within the master node.
2253  */
2254 int
2255 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
2256 {
2257 	const char *pname;
2258 	int error = ENOENT;
2259 
2260 	/*
2261 	 * Look up the file system's value for the property.  For the
2262 	 * version property, we look up a slightly different string.
2263 	 */
2264 	if (prop == ZFS_PROP_VERSION)
2265 		pname = ZPL_VERSION_STR;
2266 	else
2267 		pname = zfs_prop_to_name(prop);
2268 
2269 	if (os != NULL)
2270 		error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
2271 
2272 	if (error == ENOENT) {
2273 		/* No value set, use the default value */
2274 		switch (prop) {
2275 		case ZFS_PROP_VERSION:
2276 			*value = ZPL_VERSION;
2277 			break;
2278 		case ZFS_PROP_NORMALIZE:
2279 		case ZFS_PROP_UTF8ONLY:
2280 			*value = 0;
2281 			break;
2282 		case ZFS_PROP_CASE:
2283 			*value = ZFS_CASE_SENSITIVE;
2284 			break;
2285 		default:
2286 			return (error);
2287 		}
2288 		error = 0;
2289 	}
2290 	return (error);
2291 }
2292 
2293 static vfsdef_t vfw = {
2294 	VFSDEF_VERSION,
2295 	MNTTYPE_ZFS,
2296 	zfs_vfsinit,
2297 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS|
2298 	    VSW_XID|VSW_ZMOUNT,
2299 	&zfs_mntopts
2300 };
2301 
2302 struct modlfs zfs_modlfs = {
2303 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
2304 };
2305