xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision d1aea6f139360e9e7f1504facb24f8521047b15c)
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 = 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 	error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zfsvfs, &os);
978 	if (error) {
979 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
980 		return (error);
981 	}
982 
983 	zfsvfs->z_vfs = NULL;
984 	zfsvfs->z_parent = zfsvfs;
985 
986 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
987 	mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
988 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
989 	    offsetof(znode_t, z_link_node));
990 	rrm_init(&zfsvfs->z_teardown_lock, B_FALSE);
991 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
992 	rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
993 	for (int i = 0; i != ZFS_OBJ_MTX_SZ; i++)
994 		mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
995 
996 	error = zfsvfs_init(zfsvfs, os);
997 	if (error != 0) {
998 		dmu_objset_disown(os, zfsvfs);
999 		*zfvp = NULL;
1000 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
1001 		return (error);
1002 	}
1003 
1004 	*zfvp = zfsvfs;
1005 	return (0);
1006 }
1007 
1008 static int
1009 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
1010 {
1011 	int error;
1012 
1013 	error = zfs_register_callbacks(zfsvfs->z_vfs);
1014 	if (error)
1015 		return (error);
1016 
1017 	zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
1018 
1019 	/*
1020 	 * If we are not mounting (ie: online recv), then we don't
1021 	 * have to worry about replaying the log as we blocked all
1022 	 * operations out since we closed the ZIL.
1023 	 */
1024 	if (mounting) {
1025 		boolean_t readonly;
1026 
1027 		/*
1028 		 * During replay we remove the read only flag to
1029 		 * allow replays to succeed.
1030 		 */
1031 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
1032 		if (readonly != 0)
1033 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
1034 		else
1035 			zfs_unlinked_drain(zfsvfs);
1036 
1037 		/*
1038 		 * Parse and replay the intent log.
1039 		 *
1040 		 * Because of ziltest, this must be done after
1041 		 * zfs_unlinked_drain().  (Further note: ziltest
1042 		 * doesn't use readonly mounts, where
1043 		 * zfs_unlinked_drain() isn't called.)  This is because
1044 		 * ziltest causes spa_sync() to think it's committed,
1045 		 * but actually it is not, so the intent log contains
1046 		 * many txg's worth of changes.
1047 		 *
1048 		 * In particular, if object N is in the unlinked set in
1049 		 * the last txg to actually sync, then it could be
1050 		 * actually freed in a later txg and then reallocated
1051 		 * in a yet later txg.  This would write a "create
1052 		 * object N" record to the intent log.  Normally, this
1053 		 * would be fine because the spa_sync() would have
1054 		 * written out the fact that object N is free, before
1055 		 * we could write the "create object N" intent log
1056 		 * record.
1057 		 *
1058 		 * But when we are in ziltest mode, we advance the "open
1059 		 * txg" without actually spa_sync()-ing the changes to
1060 		 * disk.  So we would see that object N is still
1061 		 * allocated and in the unlinked set, and there is an
1062 		 * intent log record saying to allocate it.
1063 		 */
1064 		if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
1065 			if (zil_replay_disable) {
1066 				zil_destroy(zfsvfs->z_log, B_FALSE);
1067 			} else {
1068 				zfsvfs->z_replay = B_TRUE;
1069 				zil_replay(zfsvfs->z_os, zfsvfs,
1070 				    zfs_replay_vector);
1071 				zfsvfs->z_replay = B_FALSE;
1072 			}
1073 		}
1074 		zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
1075 	}
1076 
1077 	/*
1078 	 * Set the objset user_ptr to track its zfsvfs.
1079 	 */
1080 	mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1081 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1082 	mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1083 
1084 	return (0);
1085 }
1086 
1087 void
1088 zfsvfs_free(zfsvfs_t *zfsvfs)
1089 {
1090 	int i;
1091 	extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
1092 
1093 	/*
1094 	 * This is a barrier to prevent the filesystem from going away in
1095 	 * zfs_znode_move() until we can safely ensure that the filesystem is
1096 	 * not unmounted. We consider the filesystem valid before the barrier
1097 	 * and invalid after the barrier.
1098 	 */
1099 	rw_enter(&zfsvfs_lock, RW_READER);
1100 	rw_exit(&zfsvfs_lock);
1101 
1102 	zfs_fuid_destroy(zfsvfs);
1103 
1104 	mutex_destroy(&zfsvfs->z_znodes_lock);
1105 	mutex_destroy(&zfsvfs->z_lock);
1106 	list_destroy(&zfsvfs->z_all_znodes);
1107 	rrm_destroy(&zfsvfs->z_teardown_lock);
1108 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
1109 	rw_destroy(&zfsvfs->z_fuid_lock);
1110 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1111 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1112 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1113 }
1114 
1115 static void
1116 zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
1117 {
1118 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1119 	if (zfsvfs->z_vfs) {
1120 		if (zfsvfs->z_use_fuids) {
1121 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1122 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1123 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1124 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1125 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1126 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1127 		} else {
1128 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1129 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1130 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1131 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1132 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1133 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1134 		}
1135 	}
1136 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1137 }
1138 
1139 static int
1140 zfs_domount(vfs_t *vfsp, char *osname)
1141 {
1142 	dev_t mount_dev;
1143 	uint64_t recordsize, fsid_guid;
1144 	int error = 0;
1145 	zfsvfs_t *zfsvfs;
1146 
1147 	ASSERT(vfsp);
1148 	ASSERT(osname);
1149 
1150 	error = zfsvfs_create(osname, &zfsvfs);
1151 	if (error)
1152 		return (error);
1153 	zfsvfs->z_vfs = vfsp;
1154 
1155 	/* Initialize the generic filesystem structure. */
1156 	vfsp->vfs_bcount = 0;
1157 	vfsp->vfs_data = NULL;
1158 
1159 	if (zfs_create_unique_device(&mount_dev) == -1) {
1160 		error = SET_ERROR(ENODEV);
1161 		goto out;
1162 	}
1163 	ASSERT(vfs_devismounted(mount_dev) == 0);
1164 
1165 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
1166 	    NULL))
1167 		goto out;
1168 
1169 	vfsp->vfs_dev = mount_dev;
1170 	vfsp->vfs_fstype = zfsfstype;
1171 	vfsp->vfs_bsize = recordsize;
1172 	vfsp->vfs_flag |= VFS_NOTRUNC;
1173 	vfsp->vfs_data = zfsvfs;
1174 
1175 	/*
1176 	 * The fsid is 64 bits, composed of an 8-bit fs type, which
1177 	 * separates our fsid from any other filesystem types, and a
1178 	 * 56-bit objset unique ID.  The objset unique ID is unique to
1179 	 * all objsets open on this system, provided by unique_create().
1180 	 * The 8-bit fs type must be put in the low bits of fsid[1]
1181 	 * because that's where other Solaris filesystems put it.
1182 	 */
1183 	fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
1184 	ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
1185 	vfsp->vfs_fsid.val[0] = fsid_guid;
1186 	vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
1187 	    zfsfstype & 0xFF;
1188 
1189 	/*
1190 	 * Set features for file system.
1191 	 */
1192 	zfs_set_fuid_feature(zfsvfs);
1193 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
1194 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1195 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1196 		vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
1197 	} else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
1198 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1199 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1200 	}
1201 	vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
1202 
1203 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1204 		uint64_t pval;
1205 
1206 		atime_changed_cb(zfsvfs, B_FALSE);
1207 		readonly_changed_cb(zfsvfs, B_TRUE);
1208 		if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
1209 			goto out;
1210 		xattr_changed_cb(zfsvfs, pval);
1211 		zfsvfs->z_issnap = B_TRUE;
1212 		zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1213 
1214 		mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1215 		dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1216 		mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1217 	} else {
1218 		error = zfsvfs_setup(zfsvfs, B_TRUE);
1219 	}
1220 
1221 	if (!zfsvfs->z_issnap)
1222 		zfsctl_create(zfsvfs);
1223 out:
1224 	if (error) {
1225 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1226 		zfsvfs_free(zfsvfs);
1227 	} else {
1228 		atomic_inc_32(&zfs_active_fs_count);
1229 	}
1230 
1231 	return (error);
1232 }
1233 
1234 void
1235 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
1236 {
1237 	objset_t *os = zfsvfs->z_os;
1238 
1239 	if (!dmu_objset_is_snapshot(os))
1240 		dsl_prop_unregister_all(dmu_objset_ds(os), zfsvfs);
1241 }
1242 
1243 /*
1244  * Convert a decimal digit string to a uint64_t integer.
1245  */
1246 static int
1247 str_to_uint64(char *str, uint64_t *objnum)
1248 {
1249 	uint64_t num = 0;
1250 
1251 	while (*str) {
1252 		if (*str < '0' || *str > '9')
1253 			return (SET_ERROR(EINVAL));
1254 
1255 		num = num*10 + *str++ - '0';
1256 	}
1257 
1258 	*objnum = num;
1259 	return (0);
1260 }
1261 
1262 /*
1263  * The boot path passed from the boot loader is in the form of
1264  * "rootpool-name/root-filesystem-object-number'. Convert this
1265  * string to a dataset name: "rootpool-name/root-filesystem-name".
1266  */
1267 static int
1268 zfs_parse_bootfs(char *bpath, char *outpath)
1269 {
1270 	char *slashp;
1271 	uint64_t objnum;
1272 	int error;
1273 
1274 	if (*bpath == 0 || *bpath == '/')
1275 		return (SET_ERROR(EINVAL));
1276 
1277 	(void) strcpy(outpath, bpath);
1278 
1279 	slashp = strchr(bpath, '/');
1280 
1281 	/* if no '/', just return the pool name */
1282 	if (slashp == NULL) {
1283 		return (0);
1284 	}
1285 
1286 	/* if not a number, just return the root dataset name */
1287 	if (str_to_uint64(slashp+1, &objnum)) {
1288 		return (0);
1289 	}
1290 
1291 	*slashp = '\0';
1292 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
1293 	*slashp = '/';
1294 
1295 	return (error);
1296 }
1297 
1298 /*
1299  * Check that the hex label string is appropriate for the dataset being
1300  * mounted into the global_zone proper.
1301  *
1302  * Return an error if the hex label string is not default or
1303  * admin_low/admin_high.  For admin_low labels, the corresponding
1304  * dataset must be readonly.
1305  */
1306 int
1307 zfs_check_global_label(const char *dsname, const char *hexsl)
1308 {
1309 	if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1310 		return (0);
1311 	if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
1312 		return (0);
1313 	if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
1314 		/* must be readonly */
1315 		uint64_t rdonly;
1316 
1317 		if (dsl_prop_get_integer(dsname,
1318 		    zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
1319 			return (SET_ERROR(EACCES));
1320 		return (rdonly ? 0 : EACCES);
1321 	}
1322 	return (SET_ERROR(EACCES));
1323 }
1324 
1325 /*
1326  * Determine whether the mount is allowed according to MAC check.
1327  * by comparing (where appropriate) label of the dataset against
1328  * the label of the zone being mounted into.  If the dataset has
1329  * no label, create one.
1330  *
1331  * Returns 0 if access allowed, error otherwise (e.g. EACCES)
1332  */
1333 static int
1334 zfs_mount_label_policy(vfs_t *vfsp, char *osname)
1335 {
1336 	int		error, retv;
1337 	zone_t		*mntzone = NULL;
1338 	ts_label_t	*mnt_tsl;
1339 	bslabel_t	*mnt_sl;
1340 	bslabel_t	ds_sl;
1341 	char		ds_hexsl[MAXNAMELEN];
1342 
1343 	retv = EACCES;				/* assume the worst */
1344 
1345 	/*
1346 	 * Start by getting the dataset label if it exists.
1347 	 */
1348 	error = dsl_prop_get(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1349 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
1350 	if (error)
1351 		return (SET_ERROR(EACCES));
1352 
1353 	/*
1354 	 * If labeling is NOT enabled, then disallow the mount of datasets
1355 	 * which have a non-default label already.  No other label checks
1356 	 * are needed.
1357 	 */
1358 	if (!is_system_labeled()) {
1359 		if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1360 			return (0);
1361 		return (SET_ERROR(EACCES));
1362 	}
1363 
1364 	/*
1365 	 * Get the label of the mountpoint.  If mounting into the global
1366 	 * zone (i.e. mountpoint is not within an active zone and the
1367 	 * zoned property is off), the label must be default or
1368 	 * admin_low/admin_high only; no other checks are needed.
1369 	 */
1370 	mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE);
1371 	if (mntzone->zone_id == GLOBAL_ZONEID) {
1372 		uint64_t zoned;
1373 
1374 		zone_rele(mntzone);
1375 
1376 		if (dsl_prop_get_integer(osname,
1377 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
1378 			return (SET_ERROR(EACCES));
1379 		if (!zoned)
1380 			return (zfs_check_global_label(osname, ds_hexsl));
1381 		else
1382 			/*
1383 			 * This is the case of a zone dataset being mounted
1384 			 * initially, before the zone has been fully created;
1385 			 * allow this mount into global zone.
1386 			 */
1387 			return (0);
1388 	}
1389 
1390 	mnt_tsl = mntzone->zone_slabel;
1391 	ASSERT(mnt_tsl != NULL);
1392 	label_hold(mnt_tsl);
1393 	mnt_sl = label2bslabel(mnt_tsl);
1394 
1395 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) {
1396 		/*
1397 		 * The dataset doesn't have a real label, so fabricate one.
1398 		 */
1399 		char *str = NULL;
1400 
1401 		if (l_to_str_internal(mnt_sl, &str) == 0 &&
1402 		    dsl_prop_set_string(osname,
1403 		    zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1404 		    ZPROP_SRC_LOCAL, str) == 0)
1405 			retv = 0;
1406 		if (str != NULL)
1407 			kmem_free(str, strlen(str) + 1);
1408 	} else if (hexstr_to_label(ds_hexsl, &ds_sl) == 0) {
1409 		/*
1410 		 * Now compare labels to complete the MAC check.  If the
1411 		 * labels are equal then allow access.  If the mountpoint
1412 		 * label dominates the dataset label, allow readonly access.
1413 		 * Otherwise, access is denied.
1414 		 */
1415 		if (blequal(mnt_sl, &ds_sl))
1416 			retv = 0;
1417 		else if (bldominates(mnt_sl, &ds_sl)) {
1418 			vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
1419 			retv = 0;
1420 		}
1421 	}
1422 
1423 	label_rele(mnt_tsl);
1424 	zone_rele(mntzone);
1425 	return (retv);
1426 }
1427 
1428 static int
1429 zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
1430 {
1431 	int error = 0;
1432 	static int zfsrootdone = 0;
1433 	zfsvfs_t *zfsvfs = NULL;
1434 	znode_t *zp = NULL;
1435 	vnode_t *vp = NULL;
1436 	char *zfs_bootfs;
1437 	char *zfs_devid;
1438 
1439 	ASSERT(vfsp);
1440 
1441 	/*
1442 	 * The filesystem that we mount as root is defined in the
1443 	 * boot property "zfs-bootfs" with a format of
1444 	 * "poolname/root-dataset-objnum".
1445 	 */
1446 	if (why == ROOT_INIT) {
1447 		if (zfsrootdone++)
1448 			return (SET_ERROR(EBUSY));
1449 		/*
1450 		 * the process of doing a spa_load will require the
1451 		 * clock to be set before we could (for example) do
1452 		 * something better by looking at the timestamp on
1453 		 * an uberblock, so just set it to -1.
1454 		 */
1455 		clkset(-1);
1456 
1457 		if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
1458 			cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
1459 			    "bootfs name");
1460 			return (SET_ERROR(EINVAL));
1461 		}
1462 		zfs_devid = spa_get_bootprop("diskdevid");
1463 		error = spa_import_rootpool(rootfs.bo_name, zfs_devid);
1464 		if (zfs_devid)
1465 			spa_free_bootprop(zfs_devid);
1466 		if (error) {
1467 			spa_free_bootprop(zfs_bootfs);
1468 			cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
1469 			    error);
1470 			return (error);
1471 		}
1472 		if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
1473 			spa_free_bootprop(zfs_bootfs);
1474 			cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
1475 			    error);
1476 			return (error);
1477 		}
1478 
1479 		spa_free_bootprop(zfs_bootfs);
1480 
1481 		if (error = vfs_lock(vfsp))
1482 			return (error);
1483 
1484 		if (error = zfs_domount(vfsp, rootfs.bo_name)) {
1485 			cmn_err(CE_NOTE, "zfs_domount: error %d", error);
1486 			goto out;
1487 		}
1488 
1489 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
1490 		ASSERT(zfsvfs);
1491 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
1492 			cmn_err(CE_NOTE, "zfs_zget: error %d", error);
1493 			goto out;
1494 		}
1495 
1496 		vp = ZTOV(zp);
1497 		mutex_enter(&vp->v_lock);
1498 		vp->v_flag |= VROOT;
1499 		mutex_exit(&vp->v_lock);
1500 		rootvp = vp;
1501 
1502 		/*
1503 		 * Leave rootvp held.  The root file system is never unmounted.
1504 		 */
1505 
1506 		vfs_add((struct vnode *)0, vfsp,
1507 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
1508 out:
1509 		vfs_unlock(vfsp);
1510 		return (error);
1511 	} else if (why == ROOT_REMOUNT) {
1512 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
1513 		vfsp->vfs_flag |= VFS_REMOUNT;
1514 
1515 		/* refresh mount options */
1516 		zfs_unregister_callbacks(vfsp->vfs_data);
1517 		return (zfs_register_callbacks(vfsp));
1518 
1519 	} else if (why == ROOT_UNMOUNT) {
1520 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
1521 		(void) zfs_sync(vfsp, 0, 0);
1522 		return (0);
1523 	}
1524 
1525 	/*
1526 	 * if "why" is equal to anything else other than ROOT_INIT,
1527 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
1528 	 */
1529 	return (SET_ERROR(ENOTSUP));
1530 }
1531 
1532 /*ARGSUSED*/
1533 static int
1534 zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
1535 {
1536 	char		*osname;
1537 	pathname_t	spn;
1538 	int		error = 0;
1539 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
1540 	    UIO_SYSSPACE : UIO_USERSPACE;
1541 	int		canwrite;
1542 
1543 	if (mvp->v_type != VDIR)
1544 		return (SET_ERROR(ENOTDIR));
1545 
1546 	mutex_enter(&mvp->v_lock);
1547 	if ((uap->flags & MS_REMOUNT) == 0 &&
1548 	    (uap->flags & MS_OVERLAY) == 0 &&
1549 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
1550 		mutex_exit(&mvp->v_lock);
1551 		return (SET_ERROR(EBUSY));
1552 	}
1553 	mutex_exit(&mvp->v_lock);
1554 
1555 	/*
1556 	 * ZFS does not support passing unparsed data in via MS_DATA.
1557 	 * Users should use the MS_OPTIONSTR interface; this means
1558 	 * that all option parsing is already done and the options struct
1559 	 * can be interrogated.
1560 	 */
1561 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
1562 		return (SET_ERROR(EINVAL));
1563 
1564 	/*
1565 	 * Get the objset name (the "special" mount argument).
1566 	 */
1567 	if (error = pn_get(uap->spec, fromspace, &spn))
1568 		return (error);
1569 
1570 	osname = spn.pn_path;
1571 
1572 	/*
1573 	 * Check for mount privilege?
1574 	 *
1575 	 * If we don't have privilege then see if
1576 	 * we have local permission to allow it
1577 	 */
1578 	error = secpolicy_fs_mount(cr, mvp, vfsp);
1579 	if (error) {
1580 		if (dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) == 0) {
1581 			vattr_t		vattr;
1582 
1583 			/*
1584 			 * Make sure user is the owner of the mount point
1585 			 * or has sufficient privileges.
1586 			 */
1587 
1588 			vattr.va_mask = AT_UID;
1589 
1590 			if (VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
1591 				goto out;
1592 			}
1593 
1594 			if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 &&
1595 			    VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) {
1596 				goto out;
1597 			}
1598 			secpolicy_fs_mount_clearopts(cr, vfsp);
1599 		} else {
1600 			goto out;
1601 		}
1602 	}
1603 
1604 	/*
1605 	 * Refuse to mount a filesystem if we are in a local zone and the
1606 	 * dataset is not visible.
1607 	 */
1608 	if (!INGLOBALZONE(curproc) &&
1609 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1610 		error = SET_ERROR(EPERM);
1611 		goto out;
1612 	}
1613 
1614 	error = zfs_mount_label_policy(vfsp, osname);
1615 	if (error)
1616 		goto out;
1617 
1618 	/*
1619 	 * When doing a remount, we simply refresh our temporary properties
1620 	 * according to those options set in the current VFS options.
1621 	 */
1622 	if (uap->flags & MS_REMOUNT) {
1623 		/* refresh mount options */
1624 		zfs_unregister_callbacks(vfsp->vfs_data);
1625 		error = zfs_register_callbacks(vfsp);
1626 		goto out;
1627 	}
1628 
1629 	error = zfs_domount(vfsp, osname);
1630 
1631 	/*
1632 	 * Add an extra VFS_HOLD on our parent vfs so that it can't
1633 	 * disappear due to a forced unmount.
1634 	 */
1635 	if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap)
1636 		VFS_HOLD(mvp->v_vfsp);
1637 
1638 out:
1639 	pn_free(&spn);
1640 	return (error);
1641 }
1642 
1643 static int
1644 zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1645 {
1646 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1647 	dev32_t d32;
1648 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
1649 
1650 	ZFS_ENTER(zfsvfs);
1651 
1652 	dmu_objset_space(zfsvfs->z_os,
1653 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
1654 
1655 	/*
1656 	 * The underlying storage pool actually uses multiple block sizes.
1657 	 * We report the fragsize as the smallest block size we support,
1658 	 * and we report our blocksize as the filesystem's maximum blocksize.
1659 	 */
1660 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1661 	statp->f_bsize = zfsvfs->z_max_blksz;
1662 
1663 	/*
1664 	 * The following report "total" blocks of various kinds in the
1665 	 * file system, but reported in terms of f_frsize - the
1666 	 * "fragment" size.
1667 	 */
1668 
1669 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
1670 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
1671 	statp->f_bavail = statp->f_bfree; /* no root reservation */
1672 
1673 	/*
1674 	 * statvfs() should really be called statufs(), because it assumes
1675 	 * static metadata.  ZFS doesn't preallocate files, so the best
1676 	 * we can do is report the max that could possibly fit in f_files,
1677 	 * and that minus the number actually used in f_ffree.
1678 	 * For f_ffree, report the smaller of the number of object available
1679 	 * and the number of blocks (each object will take at least a block).
1680 	 */
1681 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
1682 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
1683 	statp->f_files = statp->f_ffree + usedobjs;
1684 
1685 	(void) cmpldev(&d32, vfsp->vfs_dev);
1686 	statp->f_fsid = d32;
1687 
1688 	/*
1689 	 * We're a zfs filesystem.
1690 	 */
1691 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1692 
1693 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1694 
1695 	statp->f_namemax = MAXNAMELEN - 1;
1696 
1697 	/*
1698 	 * We have all of 32 characters to stuff a string here.
1699 	 * Is there anything useful we could/should provide?
1700 	 */
1701 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
1702 
1703 	ZFS_EXIT(zfsvfs);
1704 	return (0);
1705 }
1706 
1707 static int
1708 zfs_root(vfs_t *vfsp, vnode_t **vpp)
1709 {
1710 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1711 	znode_t *rootzp;
1712 	int error;
1713 
1714 	ZFS_ENTER(zfsvfs);
1715 
1716 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1717 	if (error == 0)
1718 		*vpp = ZTOV(rootzp);
1719 
1720 	ZFS_EXIT(zfsvfs);
1721 	return (error);
1722 }
1723 
1724 /*
1725  * Teardown the zfsvfs::z_os.
1726  *
1727  * Note, if 'unmounting' is FALSE, we return with the 'z_teardown_lock'
1728  * and 'z_teardown_inactive_lock' held.
1729  */
1730 static int
1731 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1732 {
1733 	znode_t	*zp;
1734 
1735 	rrm_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1736 
1737 	if (!unmounting) {
1738 		/*
1739 		 * We purge the parent filesystem's vfsp as the parent
1740 		 * filesystem and all of its snapshots have their vnode's
1741 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
1742 		 * 'z_parent' is self referential for non-snapshots.
1743 		 */
1744 		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1745 	}
1746 
1747 	/*
1748 	 * Close the zil. NB: Can't close the zil while zfs_inactive
1749 	 * threads are blocked as zil_close can call zfs_inactive.
1750 	 */
1751 	if (zfsvfs->z_log) {
1752 		zil_close(zfsvfs->z_log);
1753 		zfsvfs->z_log = NULL;
1754 	}
1755 
1756 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
1757 
1758 	/*
1759 	 * If we are not unmounting (ie: online recv) and someone already
1760 	 * unmounted this file system while we were doing the switcheroo,
1761 	 * or a reopen of z_os failed then just bail out now.
1762 	 */
1763 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1764 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
1765 		rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1766 		return (SET_ERROR(EIO));
1767 	}
1768 
1769 	/*
1770 	 * At this point there are no vops active, and any new vops will
1771 	 * fail with EIO since we have z_teardown_lock for writer (only
1772 	 * relavent for forced unmount).
1773 	 *
1774 	 * Release all holds on dbufs.
1775 	 */
1776 	mutex_enter(&zfsvfs->z_znodes_lock);
1777 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
1778 	    zp = list_next(&zfsvfs->z_all_znodes, zp))
1779 		if (zp->z_sa_hdl) {
1780 			ASSERT(ZTOV(zp)->v_count > 0);
1781 			zfs_znode_dmu_fini(zp);
1782 		}
1783 	mutex_exit(&zfsvfs->z_znodes_lock);
1784 
1785 	/*
1786 	 * If we are unmounting, set the unmounted flag and let new vops
1787 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
1788 	 * other vops will fail with EIO.
1789 	 */
1790 	if (unmounting) {
1791 		zfsvfs->z_unmounted = B_TRUE;
1792 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
1793 		rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1794 	}
1795 
1796 	/*
1797 	 * z_os will be NULL if there was an error in attempting to reopen
1798 	 * zfsvfs, so just return as the properties had already been
1799 	 * unregistered and cached data had been evicted before.
1800 	 */
1801 	if (zfsvfs->z_os == NULL)
1802 		return (0);
1803 
1804 	/*
1805 	 * Unregister properties.
1806 	 */
1807 	zfs_unregister_callbacks(zfsvfs);
1808 
1809 	/*
1810 	 * Evict cached data
1811 	 */
1812 	if (dsl_dataset_is_dirty(dmu_objset_ds(zfsvfs->z_os)) &&
1813 	    !(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY))
1814 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1815 	dmu_objset_evict_dbufs(zfsvfs->z_os);
1816 
1817 	return (0);
1818 }
1819 
1820 /*ARGSUSED*/
1821 static int
1822 zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1823 {
1824 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1825 	objset_t *os;
1826 	int ret;
1827 
1828 	ret = secpolicy_fs_unmount(cr, vfsp);
1829 	if (ret) {
1830 		if (dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
1831 		    ZFS_DELEG_PERM_MOUNT, cr))
1832 			return (ret);
1833 	}
1834 
1835 	/*
1836 	 * We purge the parent filesystem's vfsp as the parent filesystem
1837 	 * and all of its snapshots have their vnode's v_vfsp set to the
1838 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
1839 	 * referential for non-snapshots.
1840 	 */
1841 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1842 
1843 	/*
1844 	 * Unmount any snapshots mounted under .zfs before unmounting the
1845 	 * dataset itself.
1846 	 */
1847 	if (zfsvfs->z_ctldir != NULL &&
1848 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1849 		return (ret);
1850 	}
1851 
1852 	if (!(fflag & MS_FORCE)) {
1853 		/*
1854 		 * Check the number of active vnodes in the file system.
1855 		 * Our count is maintained in the vfs structure, but the
1856 		 * number is off by 1 to indicate a hold on the vfs
1857 		 * structure itself.
1858 		 *
1859 		 * The '.zfs' directory maintains a reference of its
1860 		 * own, and any active references underneath are
1861 		 * reflected in the vnode count.
1862 		 */
1863 		if (zfsvfs->z_ctldir == NULL) {
1864 			if (vfsp->vfs_count > 1)
1865 				return (SET_ERROR(EBUSY));
1866 		} else {
1867 			if (vfsp->vfs_count > 2 ||
1868 			    zfsvfs->z_ctldir->v_count > 1)
1869 				return (SET_ERROR(EBUSY));
1870 		}
1871 	}
1872 
1873 	vfsp->vfs_flag |= VFS_UNMOUNTED;
1874 
1875 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1876 	os = zfsvfs->z_os;
1877 
1878 	/*
1879 	 * z_os will be NULL if there was an error in
1880 	 * attempting to reopen zfsvfs.
1881 	 */
1882 	if (os != NULL) {
1883 		/*
1884 		 * Unset the objset user_ptr.
1885 		 */
1886 		mutex_enter(&os->os_user_ptr_lock);
1887 		dmu_objset_set_user(os, NULL);
1888 		mutex_exit(&os->os_user_ptr_lock);
1889 
1890 		/*
1891 		 * Finally release the objset
1892 		 */
1893 		dmu_objset_disown(os, zfsvfs);
1894 	}
1895 
1896 	/*
1897 	 * We can now safely destroy the '.zfs' directory node.
1898 	 */
1899 	if (zfsvfs->z_ctldir != NULL)
1900 		zfsctl_destroy(zfsvfs);
1901 
1902 	return (0);
1903 }
1904 
1905 static int
1906 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1907 {
1908 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1909 	znode_t		*zp;
1910 	uint64_t	object = 0;
1911 	uint64_t	fid_gen = 0;
1912 	uint64_t	gen_mask;
1913 	uint64_t	zp_gen;
1914 	int 		i, err;
1915 
1916 	*vpp = NULL;
1917 
1918 	ZFS_ENTER(zfsvfs);
1919 
1920 	if (fidp->fid_len == LONG_FID_LEN) {
1921 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1922 		uint64_t	objsetid = 0;
1923 		uint64_t	setgen = 0;
1924 
1925 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1926 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1927 
1928 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1929 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1930 
1931 		ZFS_EXIT(zfsvfs);
1932 
1933 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1934 		if (err)
1935 			return (SET_ERROR(EINVAL));
1936 		ZFS_ENTER(zfsvfs);
1937 	}
1938 
1939 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1940 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1941 
1942 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1943 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1944 
1945 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1946 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1947 	} else {
1948 		ZFS_EXIT(zfsvfs);
1949 		return (SET_ERROR(EINVAL));
1950 	}
1951 
1952 	/* A zero fid_gen means we are in the .zfs control directories */
1953 	if (fid_gen == 0 &&
1954 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1955 		*vpp = zfsvfs->z_ctldir;
1956 		ASSERT(*vpp != NULL);
1957 		if (object == ZFSCTL_INO_SNAPDIR) {
1958 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1959 			    0, NULL, NULL, NULL, NULL, NULL) == 0);
1960 		} else {
1961 			VN_HOLD(*vpp);
1962 		}
1963 		ZFS_EXIT(zfsvfs);
1964 		return (0);
1965 	}
1966 
1967 	gen_mask = -1ULL >> (64 - 8 * i);
1968 
1969 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1970 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1971 		ZFS_EXIT(zfsvfs);
1972 		return (err);
1973 	}
1974 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
1975 	    sizeof (uint64_t));
1976 	zp_gen = zp_gen & gen_mask;
1977 	if (zp_gen == 0)
1978 		zp_gen = 1;
1979 	if (zp->z_unlinked || zp_gen != fid_gen) {
1980 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1981 		VN_RELE(ZTOV(zp));
1982 		ZFS_EXIT(zfsvfs);
1983 		return (SET_ERROR(EINVAL));
1984 	}
1985 
1986 	*vpp = ZTOV(zp);
1987 	ZFS_EXIT(zfsvfs);
1988 	return (0);
1989 }
1990 
1991 /*
1992  * Block out VOPs and close zfsvfs_t::z_os
1993  *
1994  * Note, if successful, then we return with the 'z_teardown_lock' and
1995  * 'z_teardown_inactive_lock' write held.  We leave ownership of the underlying
1996  * dataset and objset intact so that they can be atomically handed off during
1997  * a subsequent rollback or recv operation and the resume thereafter.
1998  */
1999 int
2000 zfs_suspend_fs(zfsvfs_t *zfsvfs)
2001 {
2002 	int error;
2003 
2004 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
2005 		return (error);
2006 
2007 	return (0);
2008 }
2009 
2010 /*
2011  * Rebuild SA and release VOPs.  Note that ownership of the underlying dataset
2012  * is an invariant across any of the operations that can be performed while the
2013  * filesystem was suspended.  Whether it succeeded or failed, the preconditions
2014  * are the same: the relevant objset and associated dataset are owned by
2015  * zfsvfs, held, and long held on entry.
2016  */
2017 int
2018 zfs_resume_fs(zfsvfs_t *zfsvfs, dsl_dataset_t *ds)
2019 {
2020 	int err;
2021 	znode_t *zp;
2022 
2023 	ASSERT(RRM_WRITE_HELD(&zfsvfs->z_teardown_lock));
2024 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
2025 
2026 	/*
2027 	 * We already own this, so just update the objset_t, as the one we
2028 	 * had before may have been evicted.
2029 	 */
2030 	objset_t *os;
2031 	VERIFY3P(ds->ds_owner, ==, zfsvfs);
2032 	VERIFY(dsl_dataset_long_held(ds));
2033 	VERIFY0(dmu_objset_from_ds(ds, &os));
2034 
2035 	err = zfsvfs_init(zfsvfs, os);
2036 	if (err != 0)
2037 		goto bail;
2038 
2039 	VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
2040 
2041 	zfs_set_fuid_feature(zfsvfs);
2042 
2043 	/*
2044 	 * Attempt to re-establish all the active znodes with
2045 	 * their dbufs.  If a zfs_rezget() fails, then we'll let
2046 	 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
2047 	 * when they try to use their znode.
2048 	 */
2049 	mutex_enter(&zfsvfs->z_znodes_lock);
2050 	for (zp = list_head(&zfsvfs->z_all_znodes); zp;
2051 	    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
2052 		(void) zfs_rezget(zp);
2053 	}
2054 	mutex_exit(&zfsvfs->z_znodes_lock);
2055 
2056 bail:
2057 	/* release the VOPs */
2058 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
2059 	rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2060 
2061 	if (err) {
2062 		/*
2063 		 * Since we couldn't setup the sa framework, try to force
2064 		 * unmount this file system.
2065 		 */
2066 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
2067 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
2068 	}
2069 	return (err);
2070 }
2071 
2072 static void
2073 zfs_freevfs(vfs_t *vfsp)
2074 {
2075 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
2076 
2077 	/*
2078 	 * If this is a snapshot, we have an extra VFS_HOLD on our parent
2079 	 * from zfs_mount().  Release it here.  If we came through
2080 	 * zfs_mountroot() instead, we didn't grab an extra hold, so
2081 	 * skip the VFS_RELE for rootvfs.
2082 	 */
2083 	if (zfsvfs->z_issnap && (vfsp != rootvfs))
2084 		VFS_RELE(zfsvfs->z_parent->z_vfs);
2085 
2086 	zfsvfs_free(zfsvfs);
2087 
2088 	atomic_dec_32(&zfs_active_fs_count);
2089 }
2090 
2091 /*
2092  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
2093  * so we can't safely do any non-idempotent initialization here.
2094  * Leave that to zfs_init() and zfs_fini(), which are called
2095  * from the module's _init() and _fini() entry points.
2096  */
2097 /*ARGSUSED*/
2098 static int
2099 zfs_vfsinit(int fstype, char *name)
2100 {
2101 	int error;
2102 
2103 	zfsfstype = fstype;
2104 
2105 	/*
2106 	 * Setup vfsops and vnodeops tables.
2107 	 */
2108 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
2109 	if (error != 0) {
2110 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
2111 	}
2112 
2113 	error = zfs_create_op_tables();
2114 	if (error) {
2115 		zfs_remove_op_tables();
2116 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
2117 		(void) vfs_freevfsops_by_type(zfsfstype);
2118 		return (error);
2119 	}
2120 
2121 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
2122 
2123 	/*
2124 	 * Unique major number for all zfs mounts.
2125 	 * If we run out of 32-bit minors, we'll getudev() another major.
2126 	 */
2127 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
2128 	zfs_minor = ZFS_MIN_MINOR;
2129 
2130 	return (0);
2131 }
2132 
2133 void
2134 zfs_init(void)
2135 {
2136 	/*
2137 	 * Initialize .zfs directory structures
2138 	 */
2139 	zfsctl_init();
2140 
2141 	/*
2142 	 * Initialize znode cache, vnode ops, etc...
2143 	 */
2144 	zfs_znode_init();
2145 
2146 	dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
2147 }
2148 
2149 void
2150 zfs_fini(void)
2151 {
2152 	zfsctl_fini();
2153 	zfs_znode_fini();
2154 }
2155 
2156 int
2157 zfs_busy(void)
2158 {
2159 	return (zfs_active_fs_count != 0);
2160 }
2161 
2162 int
2163 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
2164 {
2165 	int error;
2166 	objset_t *os = zfsvfs->z_os;
2167 	dmu_tx_t *tx;
2168 
2169 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
2170 		return (SET_ERROR(EINVAL));
2171 
2172 	if (newvers < zfsvfs->z_version)
2173 		return (SET_ERROR(EINVAL));
2174 
2175 	if (zfs_spa_version_map(newvers) >
2176 	    spa_version(dmu_objset_spa(zfsvfs->z_os)))
2177 		return (SET_ERROR(ENOTSUP));
2178 
2179 	tx = dmu_tx_create(os);
2180 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
2181 	if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2182 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
2183 		    ZFS_SA_ATTRS);
2184 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2185 	}
2186 	error = dmu_tx_assign(tx, TXG_WAIT);
2187 	if (error) {
2188 		dmu_tx_abort(tx);
2189 		return (error);
2190 	}
2191 
2192 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
2193 	    8, 1, &newvers, tx);
2194 
2195 	if (error) {
2196 		dmu_tx_commit(tx);
2197 		return (error);
2198 	}
2199 
2200 	if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2201 		uint64_t sa_obj;
2202 
2203 		ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
2204 		    SPA_VERSION_SA);
2205 		sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
2206 		    DMU_OT_NONE, 0, tx);
2207 
2208 		error = zap_add(os, MASTER_NODE_OBJ,
2209 		    ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
2210 		ASSERT0(error);
2211 
2212 		VERIFY(0 == sa_set_sa_object(os, sa_obj));
2213 		sa_register_update_callback(os, zfs_sa_upgrade);
2214 	}
2215 
2216 	spa_history_log_internal_ds(dmu_objset_ds(os), "upgrade", tx,
2217 	    "from %llu to %llu", zfsvfs->z_version, newvers);
2218 
2219 	dmu_tx_commit(tx);
2220 
2221 	zfsvfs->z_version = newvers;
2222 
2223 	zfs_set_fuid_feature(zfsvfs);
2224 
2225 	return (0);
2226 }
2227 
2228 /*
2229  * Read a property stored within the master node.
2230  */
2231 int
2232 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
2233 {
2234 	const char *pname;
2235 	int error = ENOENT;
2236 
2237 	/*
2238 	 * Look up the file system's value for the property.  For the
2239 	 * version property, we look up a slightly different string.
2240 	 */
2241 	if (prop == ZFS_PROP_VERSION)
2242 		pname = ZPL_VERSION_STR;
2243 	else
2244 		pname = zfs_prop_to_name(prop);
2245 
2246 	if (os != NULL) {
2247 		ASSERT3U(os->os_phys->os_type, ==, DMU_OST_ZFS);
2248 		error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
2249 	}
2250 
2251 	if (error == ENOENT) {
2252 		/* No value set, use the default value */
2253 		switch (prop) {
2254 		case ZFS_PROP_VERSION:
2255 			*value = ZPL_VERSION;
2256 			break;
2257 		case ZFS_PROP_NORMALIZE:
2258 		case ZFS_PROP_UTF8ONLY:
2259 			*value = 0;
2260 			break;
2261 		case ZFS_PROP_CASE:
2262 			*value = ZFS_CASE_SENSITIVE;
2263 			break;
2264 		default:
2265 			return (error);
2266 		}
2267 		error = 0;
2268 	}
2269 	return (error);
2270 }
2271 
2272 /*
2273  * Return true if the coresponding vfs's unmounted flag is set.
2274  * Otherwise return false.
2275  * If this function returns true we know VFS unmount has been initiated.
2276  */
2277 boolean_t
2278 zfs_get_vfs_flag_unmounted(objset_t *os)
2279 {
2280 	zfsvfs_t *zfvp;
2281 	boolean_t unmounted = B_FALSE;
2282 
2283 	ASSERT(dmu_objset_type(os) == DMU_OST_ZFS);
2284 
2285 	mutex_enter(&os->os_user_ptr_lock);
2286 	zfvp = dmu_objset_get_user(os);
2287 	if (zfvp != NULL && zfvp->z_vfs != NULL &&
2288 	    (zfvp->z_vfs->vfs_flag & VFS_UNMOUNTED))
2289 		unmounted = B_TRUE;
2290 	mutex_exit(&os->os_user_ptr_lock);
2291 
2292 	return (unmounted);
2293 }
2294 
2295 static vfsdef_t vfw = {
2296 	VFSDEF_VERSION,
2297 	MNTTYPE_ZFS,
2298 	zfs_vfsinit,
2299 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS|
2300 	    VSW_XID|VSW_ZMOUNT,
2301 	&zfs_mntopts
2302 };
2303 
2304 struct modlfs zfs_modlfs = {
2305 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
2306 };
2307