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