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