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