xref: /titanic_51/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision e3c2d6aa3bc760b22fad3c83f876553f0d2c5b66)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/sysmacros.h>
32 #include <sys/kmem.h>
33 #include <sys/pathname.h>
34 #include <sys/vnode.h>
35 #include <sys/vfs.h>
36 #include <sys/vfs_opreg.h>
37 #include <sys/mntent.h>
38 #include <sys/mount.h>
39 #include <sys/cmn_err.h>
40 #include "fs/fs_subr.h"
41 #include <sys/zfs_znode.h>
42 #include <sys/zfs_dir.h>
43 #include <sys/zfs_i18n.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/varargs.h>
53 #include <sys/policy.h>
54 #include <sys/atomic.h>
55 #include <sys/mkdev.h>
56 #include <sys/modctl.h>
57 #include <sys/refstr.h>
58 #include <sys/zfs_ioctl.h>
59 #include <sys/zfs_ctldir.h>
60 #include <sys/zfs_fuid.h>
61 #include <sys/bootconf.h>
62 #include <sys/sunddi.h>
63 #include <sys/dnlc.h>
64 #include <sys/dmu_objset.h>
65 
66 int zfsfstype;
67 vfsops_t *zfs_vfsops = NULL;
68 static major_t zfs_major;
69 static minor_t zfs_minor;
70 static kmutex_t	zfs_dev_mtx;
71 
72 static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
73 static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr);
74 static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
75 static int zfs_root(vfs_t *vfsp, vnode_t **vpp);
76 static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp);
77 static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp);
78 static void zfs_freevfs(vfs_t *vfsp);
79 
80 static const fs_operation_def_t zfs_vfsops_template[] = {
81 	VFSNAME_MOUNT,		{ .vfs_mount = zfs_mount },
82 	VFSNAME_MOUNTROOT,	{ .vfs_mountroot = zfs_mountroot },
83 	VFSNAME_UNMOUNT,	{ .vfs_unmount = zfs_umount },
84 	VFSNAME_ROOT,		{ .vfs_root = zfs_root },
85 	VFSNAME_STATVFS,	{ .vfs_statvfs = zfs_statvfs },
86 	VFSNAME_SYNC,		{ .vfs_sync = zfs_sync },
87 	VFSNAME_VGET,		{ .vfs_vget = zfs_vget },
88 	VFSNAME_FREEVFS,	{ .vfs_freevfs = zfs_freevfs },
89 	NULL,			NULL
90 };
91 
92 static const fs_operation_def_t zfs_vfsops_eio_template[] = {
93 	VFSNAME_FREEVFS,	{ .vfs_freevfs =  zfs_freevfs },
94 	NULL,			NULL
95 };
96 
97 /*
98  * We need to keep a count of active fs's.
99  * This is necessary to prevent our module
100  * from being unloaded after a umount -f
101  */
102 static uint32_t	zfs_active_fs_count = 0;
103 
104 static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
105 static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
106 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
107 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
108 
109 /*
110  * MO_DEFAULT is not used since the default value is determined
111  * by the equivalent property.
112  */
113 static mntopt_t mntopts[] = {
114 	{ MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL },
115 	{ MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL },
116 	{ MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL },
117 	{ MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
118 };
119 
120 static mntopts_t zfs_mntopts = {
121 	sizeof (mntopts) / sizeof (mntopt_t),
122 	mntopts
123 };
124 
125 /*ARGSUSED*/
126 int
127 zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
128 {
129 	/*
130 	 * Data integrity is job one.  We don't want a compromised kernel
131 	 * writing to the storage pool, so we never sync during panic.
132 	 */
133 	if (panicstr)
134 		return (0);
135 
136 	/*
137 	 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
138 	 * to sync metadata, which they would otherwise cache indefinitely.
139 	 * Semantically, the only requirement is that the sync be initiated.
140 	 * The DMU syncs out txgs frequently, so there's nothing to do.
141 	 */
142 	if (flag & SYNC_ATTR)
143 		return (0);
144 
145 	if (vfsp != NULL) {
146 		/*
147 		 * Sync a specific filesystem.
148 		 */
149 		zfsvfs_t *zfsvfs = vfsp->vfs_data;
150 
151 		ZFS_ENTER(zfsvfs);
152 		if (zfsvfs->z_log != NULL)
153 			zil_commit(zfsvfs->z_log, UINT64_MAX, 0);
154 		else
155 			txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
156 		ZFS_EXIT(zfsvfs);
157 	} else {
158 		/*
159 		 * Sync all ZFS filesystems.  This is what happens when you
160 		 * run sync(1M).  Unlike other filesystems, ZFS honors the
161 		 * request by waiting for all pools to commit all dirty data.
162 		 */
163 		spa_sync_allpools();
164 	}
165 
166 	return (0);
167 }
168 
169 static int
170 zfs_create_unique_device(dev_t *dev)
171 {
172 	major_t new_major;
173 
174 	do {
175 		ASSERT3U(zfs_minor, <=, MAXMIN32);
176 		minor_t start = zfs_minor;
177 		do {
178 			mutex_enter(&zfs_dev_mtx);
179 			if (zfs_minor >= MAXMIN32) {
180 				/*
181 				 * If we're still using the real major
182 				 * keep out of /dev/zfs and /dev/zvol minor
183 				 * number space.  If we're using a getudev()'ed
184 				 * major number, we can use all of its minors.
185 				 */
186 				if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
187 					zfs_minor = ZFS_MIN_MINOR;
188 				else
189 					zfs_minor = 0;
190 			} else {
191 				zfs_minor++;
192 			}
193 			*dev = makedevice(zfs_major, zfs_minor);
194 			mutex_exit(&zfs_dev_mtx);
195 		} while (vfs_devismounted(*dev) && zfs_minor != start);
196 		if (zfs_minor == start) {
197 			/*
198 			 * We are using all ~262,000 minor numbers for the
199 			 * current major number.  Create a new major number.
200 			 */
201 			if ((new_major = getudev()) == (major_t)-1) {
202 				cmn_err(CE_WARN,
203 				    "zfs_mount: Can't get unique major "
204 				    "device number.");
205 				return (-1);
206 			}
207 			mutex_enter(&zfs_dev_mtx);
208 			zfs_major = new_major;
209 			zfs_minor = 0;
210 
211 			mutex_exit(&zfs_dev_mtx);
212 		} else {
213 			break;
214 		}
215 		/* CONSTANTCONDITION */
216 	} while (1);
217 
218 	return (0);
219 }
220 
221 static void
222 atime_changed_cb(void *arg, uint64_t newval)
223 {
224 	zfsvfs_t *zfsvfs = arg;
225 
226 	if (newval == TRUE) {
227 		zfsvfs->z_atime = TRUE;
228 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
229 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
230 	} else {
231 		zfsvfs->z_atime = FALSE;
232 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
233 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
234 	}
235 }
236 
237 static void
238 xattr_changed_cb(void *arg, uint64_t newval)
239 {
240 	zfsvfs_t *zfsvfs = arg;
241 
242 	if (newval == TRUE) {
243 		/* XXX locking on vfs_flag? */
244 		zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
245 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
246 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
247 	} else {
248 		/* XXX locking on vfs_flag? */
249 		zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
250 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
251 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
252 	}
253 }
254 
255 static void
256 blksz_changed_cb(void *arg, uint64_t newval)
257 {
258 	zfsvfs_t *zfsvfs = arg;
259 
260 	if (newval < SPA_MINBLOCKSIZE ||
261 	    newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
262 		newval = SPA_MAXBLOCKSIZE;
263 
264 	zfsvfs->z_max_blksz = newval;
265 	zfsvfs->z_vfs->vfs_bsize = newval;
266 }
267 
268 static void
269 readonly_changed_cb(void *arg, uint64_t newval)
270 {
271 	zfsvfs_t *zfsvfs = arg;
272 
273 	if (newval) {
274 		/* XXX locking on vfs_flag? */
275 		zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
276 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
277 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
278 	} else {
279 		/* XXX locking on vfs_flag? */
280 		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
281 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
282 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
283 	}
284 }
285 
286 static void
287 devices_changed_cb(void *arg, uint64_t newval)
288 {
289 	zfsvfs_t *zfsvfs = arg;
290 
291 	if (newval == FALSE) {
292 		zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
293 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
294 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
295 	} else {
296 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
297 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
298 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
299 	}
300 }
301 
302 static void
303 setuid_changed_cb(void *arg, uint64_t newval)
304 {
305 	zfsvfs_t *zfsvfs = arg;
306 
307 	if (newval == FALSE) {
308 		zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
309 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
310 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
311 	} else {
312 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
313 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
314 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
315 	}
316 }
317 
318 static void
319 exec_changed_cb(void *arg, uint64_t newval)
320 {
321 	zfsvfs_t *zfsvfs = arg;
322 
323 	if (newval == FALSE) {
324 		zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
325 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
326 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
327 	} else {
328 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
329 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
330 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
331 	}
332 }
333 
334 /*
335  * The nbmand mount option can be changed at mount time.
336  * We can't allow it to be toggled on live file systems or incorrect
337  * behavior may be seen from cifs clients
338  *
339  * This property isn't registered via dsl_prop_register(), but this callback
340  * will be called when a file system is first mounted
341  */
342 static void
343 nbmand_changed_cb(void *arg, uint64_t newval)
344 {
345 	zfsvfs_t *zfsvfs = arg;
346 	if (newval == FALSE) {
347 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
348 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
349 	} else {
350 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
351 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
352 	}
353 }
354 
355 static void
356 snapdir_changed_cb(void *arg, uint64_t newval)
357 {
358 	zfsvfs_t *zfsvfs = arg;
359 
360 	zfsvfs->z_show_ctldir = newval;
361 }
362 
363 static void
364 vscan_changed_cb(void *arg, uint64_t newval)
365 {
366 	zfsvfs_t *zfsvfs = arg;
367 
368 	zfsvfs->z_vscan = newval;
369 }
370 
371 static void
372 acl_mode_changed_cb(void *arg, uint64_t newval)
373 {
374 	zfsvfs_t *zfsvfs = arg;
375 
376 	zfsvfs->z_acl_mode = newval;
377 }
378 
379 static void
380 acl_inherit_changed_cb(void *arg, uint64_t newval)
381 {
382 	zfsvfs_t *zfsvfs = arg;
383 
384 	zfsvfs->z_acl_inherit = newval;
385 }
386 
387 static int
388 zfs_normalization_set(char *osname, zfsvfs_t *zfsvfs)
389 {
390 	uint64_t pval;
391 	int error;
392 
393 	if (zfsvfs->z_version < ZPL_VERSION_FUID)
394 		return (0);
395 
396 	error = dsl_prop_get_integer(osname, "normalization", &pval, NULL);
397 	if (error)
398 		goto normquit;
399 	switch ((int)pval) {
400 	case ZFS_NORMALIZE_NONE:
401 		break;
402 	case ZFS_NORMALIZE_C:
403 		zfsvfs->z_norm |= U8_TEXTPREP_NFC;
404 		break;
405 	case ZFS_NORMALIZE_KC:
406 		zfsvfs->z_norm |= U8_TEXTPREP_NFKC;
407 		break;
408 	case ZFS_NORMALIZE_D:
409 		zfsvfs->z_norm |= U8_TEXTPREP_NFD;
410 		break;
411 	case ZFS_NORMALIZE_KD:
412 		zfsvfs->z_norm |= U8_TEXTPREP_NFKD;
413 		break;
414 	default:
415 		ASSERT(pval <= ZFS_NORMALIZE_KD);
416 		break;
417 	}
418 
419 	error = dsl_prop_get_integer(osname, "utf8only", &pval, NULL);
420 	if (error)
421 		goto normquit;
422 	if (pval)
423 		zfsvfs->z_case |= ZFS_UTF8_ONLY;
424 	else
425 		zfsvfs->z_case &= ~ZFS_UTF8_ONLY;
426 
427 	error = dsl_prop_get_integer(osname, "casesensitivity", &pval, NULL);
428 	if (error)
429 		goto normquit;
430 	vfs_set_feature(zfsvfs->z_vfs, VFSFT_DIRENTFLAGS);
431 	switch ((int)pval) {
432 	case ZFS_CASE_SENSITIVE:
433 		break;
434 	case ZFS_CASE_INSENSITIVE:
435 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
436 		zfsvfs->z_case |= ZFS_CI_ONLY;
437 		vfs_set_feature(zfsvfs->z_vfs, VFSFT_CASEINSENSITIVE);
438 		vfs_set_feature(zfsvfs->z_vfs, VFSFT_NOCASESENSITIVE);
439 		break;
440 	case ZFS_CASE_MIXED:
441 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
442 		zfsvfs->z_case |= ZFS_CI_MIXD;
443 		vfs_set_feature(zfsvfs->z_vfs, VFSFT_CASEINSENSITIVE);
444 		break;
445 	default:
446 		ASSERT(pval <= ZFS_CASE_MIXED);
447 		break;
448 	}
449 
450 normquit:
451 	return (error);
452 }
453 
454 static int
455 zfs_register_callbacks(vfs_t *vfsp)
456 {
457 	struct dsl_dataset *ds = NULL;
458 	objset_t *os = NULL;
459 	zfsvfs_t *zfsvfs = NULL;
460 	uint64_t nbmand;
461 	int readonly, do_readonly = B_FALSE;
462 	int setuid, do_setuid = B_FALSE;
463 	int exec, do_exec = B_FALSE;
464 	int devices, do_devices = B_FALSE;
465 	int xattr, do_xattr = B_FALSE;
466 	int atime, do_atime = B_FALSE;
467 	int error = 0;
468 
469 	ASSERT(vfsp);
470 	zfsvfs = vfsp->vfs_data;
471 	ASSERT(zfsvfs);
472 	os = zfsvfs->z_os;
473 
474 	/*
475 	 * The act of registering our callbacks will destroy any mount
476 	 * options we may have.  In order to enable temporary overrides
477 	 * of mount options, we stash away the current values and
478 	 * restore them after we register the callbacks.
479 	 */
480 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
481 		readonly = B_TRUE;
482 		do_readonly = B_TRUE;
483 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
484 		readonly = B_FALSE;
485 		do_readonly = B_TRUE;
486 	}
487 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
488 		devices = B_FALSE;
489 		setuid = B_FALSE;
490 		do_devices = B_TRUE;
491 		do_setuid = B_TRUE;
492 	} else {
493 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
494 			devices = B_FALSE;
495 			do_devices = B_TRUE;
496 		} else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
497 			devices = B_TRUE;
498 			do_devices = B_TRUE;
499 		}
500 
501 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
502 			setuid = B_FALSE;
503 			do_setuid = B_TRUE;
504 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
505 			setuid = B_TRUE;
506 			do_setuid = B_TRUE;
507 		}
508 	}
509 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
510 		exec = B_FALSE;
511 		do_exec = B_TRUE;
512 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
513 		exec = B_TRUE;
514 		do_exec = B_TRUE;
515 	}
516 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
517 		xattr = B_FALSE;
518 		do_xattr = B_TRUE;
519 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
520 		xattr = B_TRUE;
521 		do_xattr = B_TRUE;
522 	}
523 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
524 		atime = B_FALSE;
525 		do_atime = B_TRUE;
526 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
527 		atime = B_TRUE;
528 		do_atime = B_TRUE;
529 	}
530 
531 	/*
532 	 * nbmand is a special property.  It can only be changed at
533 	 * mount time.
534 	 *
535 	 * This is weird, but it is documented to only be changeable
536 	 * at mount time.
537 	 */
538 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
539 		nbmand = B_FALSE;
540 	} else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
541 		nbmand = B_TRUE;
542 	} else {
543 		char osname[MAXNAMELEN];
544 
545 		dmu_objset_name(os, osname);
546 		if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
547 		    NULL))
548 		return (error);
549 	}
550 
551 	/*
552 	 * Register property callbacks.
553 	 *
554 	 * It would probably be fine to just check for i/o error from
555 	 * the first prop_register(), but I guess I like to go
556 	 * overboard...
557 	 */
558 	ds = dmu_objset_ds(os);
559 	error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
560 	error = error ? error : dsl_prop_register(ds,
561 	    "xattr", xattr_changed_cb, zfsvfs);
562 	error = error ? error : dsl_prop_register(ds,
563 	    "recordsize", blksz_changed_cb, zfsvfs);
564 	error = error ? error : dsl_prop_register(ds,
565 	    "readonly", readonly_changed_cb, zfsvfs);
566 	error = error ? error : dsl_prop_register(ds,
567 	    "devices", devices_changed_cb, zfsvfs);
568 	error = error ? error : dsl_prop_register(ds,
569 	    "setuid", setuid_changed_cb, zfsvfs);
570 	error = error ? error : dsl_prop_register(ds,
571 	    "exec", exec_changed_cb, zfsvfs);
572 	error = error ? error : dsl_prop_register(ds,
573 	    "snapdir", snapdir_changed_cb, zfsvfs);
574 	error = error ? error : dsl_prop_register(ds,
575 	    "aclmode", acl_mode_changed_cb, zfsvfs);
576 	error = error ? error : dsl_prop_register(ds,
577 	    "aclinherit", acl_inherit_changed_cb, zfsvfs);
578 	error = error ? error : dsl_prop_register(ds,
579 	    "vscan", vscan_changed_cb, zfsvfs);
580 	if (error)
581 		goto unregister;
582 
583 	/*
584 	 * Invoke our callbacks to restore temporary mount options.
585 	 */
586 	if (do_readonly)
587 		readonly_changed_cb(zfsvfs, readonly);
588 	if (do_setuid)
589 		setuid_changed_cb(zfsvfs, setuid);
590 	if (do_exec)
591 		exec_changed_cb(zfsvfs, exec);
592 	if (do_devices)
593 		devices_changed_cb(zfsvfs, devices);
594 	if (do_xattr)
595 		xattr_changed_cb(zfsvfs, xattr);
596 	if (do_atime)
597 		atime_changed_cb(zfsvfs, atime);
598 
599 	nbmand_changed_cb(zfsvfs, nbmand);
600 
601 	return (0);
602 
603 unregister:
604 	/*
605 	 * We may attempt to unregister some callbacks that are not
606 	 * registered, but this is OK; it will simply return ENOMSG,
607 	 * which we will ignore.
608 	 */
609 	(void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
610 	(void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
611 	(void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
612 	(void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
613 	(void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs);
614 	(void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
615 	(void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
616 	(void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
617 	(void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
618 	(void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
619 	    zfsvfs);
620 	(void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
621 	return (error);
622 
623 }
624 
625 static int
626 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
627 {
628 	uint_t readonly;
629 	int error;
630 
631 	error = zfs_register_callbacks(zfsvfs->z_vfs);
632 	if (error)
633 		return (error);
634 
635 	/*
636 	 * Set the objset user_ptr to track its zfsvfs.
637 	 */
638 	mutex_enter(&zfsvfs->z_os->os->os_user_ptr_lock);
639 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
640 	mutex_exit(&zfsvfs->z_os->os->os_user_ptr_lock);
641 
642 	/*
643 	 * If we are not mounting (ie: online recv), then we don't
644 	 * have to worry about replaying the log as we blocked all
645 	 * operations out since we closed the ZIL.
646 	 */
647 	if (mounting) {
648 		/*
649 		 * During replay we remove the read only flag to
650 		 * allow replays to succeed.
651 		 */
652 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
653 		if (readonly != 0)
654 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
655 		else
656 			zfs_unlinked_drain(zfsvfs);
657 
658 		/*
659 		 * Parse and replay the intent log.
660 		 *
661 		 * Because of ziltest, this must be done after
662 		 * zfs_unlinked_drain().  (Further note: ziltest doesn't
663 		 * use readonly mounts, where zfs_unlinked_drain() isn't
664 		 * called.)  This is because ziltest causes spa_sync()
665 		 * to think it's committed, but actually it is not, so
666 		 * the intent log contains many txg's worth of changes.
667 		 *
668 		 * In particular, if object N is in the unlinked set in
669 		 * the last txg to actually sync, then it could be
670 		 * actually freed in a later txg and then reallocated in
671 		 * a yet later txg.  This would write a "create object
672 		 * N" record to the intent log.  Normally, this would be
673 		 * fine because the spa_sync() would have written out
674 		 * the fact that object N is free, before we could write
675 		 * the "create object N" intent log record.
676 		 *
677 		 * But when we are in ziltest mode, we advance the "open
678 		 * txg" without actually spa_sync()-ing the changes to
679 		 * disk.  So we would see that object N is still
680 		 * allocated and in the unlinked set, and there is an
681 		 * intent log record saying to allocate it.
682 		 */
683 		zil_replay(zfsvfs->z_os, zfsvfs, &zfsvfs->z_assign,
684 		    zfs_replay_vector);
685 
686 		zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
687 	}
688 
689 	if (!zil_disable)
690 		zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
691 
692 	return (0);
693 }
694 
695 static int
696 zfs_domount(vfs_t *vfsp, char *osname, cred_t *cr)
697 {
698 	dev_t mount_dev;
699 	uint64_t recordsize, readonly;
700 	int error = 0;
701 	int mode;
702 	zfsvfs_t *zfsvfs;
703 	znode_t *zp = NULL;
704 
705 	ASSERT(vfsp);
706 	ASSERT(osname);
707 
708 	/*
709 	 * Initialize the zfs-specific filesystem structure.
710 	 * Should probably make this a kmem cache, shuffle fields,
711 	 * and just bzero up to z_hold_mtx[].
712 	 */
713 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
714 	zfsvfs->z_vfs = vfsp;
715 	zfsvfs->z_parent = zfsvfs;
716 	zfsvfs->z_assign = TXG_NOWAIT;
717 	zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
718 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
719 
720 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
721 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
722 	    offsetof(znode_t, z_link_node));
723 	rrw_init(&zfsvfs->z_teardown_lock);
724 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
725 
726 	/* Initialize the generic filesystem structure. */
727 	vfsp->vfs_bcount = 0;
728 	vfsp->vfs_data = NULL;
729 
730 	if (zfs_create_unique_device(&mount_dev) == -1) {
731 		error = ENODEV;
732 		goto out;
733 	}
734 	ASSERT(vfs_devismounted(mount_dev) == 0);
735 
736 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
737 	    NULL))
738 		goto out;
739 
740 	vfsp->vfs_dev = mount_dev;
741 	vfsp->vfs_fstype = zfsfstype;
742 	vfsp->vfs_bsize = recordsize;
743 	vfsp->vfs_flag |= VFS_NOTRUNC;
744 	vfsp->vfs_data = zfsvfs;
745 
746 	if (error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL))
747 		goto out;
748 
749 	if (readonly)
750 		mode = DS_MODE_PRIMARY | DS_MODE_READONLY;
751 	else
752 		mode = DS_MODE_PRIMARY;
753 
754 	error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
755 	if (error == EROFS) {
756 		mode = DS_MODE_PRIMARY | DS_MODE_READONLY;
757 		error = dmu_objset_open(osname, DMU_OST_ZFS, mode,
758 		    &zfsvfs->z_os);
759 	}
760 
761 	if (error)
762 		goto out;
763 
764 	if (error = zfs_init_fs(zfsvfs, &zp, cr))
765 		goto out;
766 
767 	/* The call to zfs_init_fs leaves the vnode held, release it here. */
768 	VN_RELE(ZTOV(zp));
769 
770 	/*
771 	 * Set features for file system.
772 	 */
773 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
774 	if (zfsvfs->z_use_fuids) {
775 		vfs_set_feature(vfsp, VFSFT_XVATTR);
776 		vfs_set_feature(vfsp, VFSFT_ACEMASKONACCESS);
777 		vfs_set_feature(vfsp, VFSFT_ACLONCREATE);
778 	}
779 
780 	/*
781 	 * Set normalization regardless of whether or not the object
782 	 * set is a snapshot.  Snapshots and clones need to have
783 	 * identical normalization as did the file system they
784 	 * originated from.
785 	 */
786 	if ((error = zfs_normalization_set(osname, zfsvfs)) != 0)
787 		goto out;
788 
789 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
790 		uint64_t pval;
791 
792 		ASSERT(mode & DS_MODE_READONLY);
793 		atime_changed_cb(zfsvfs, B_FALSE);
794 		readonly_changed_cb(zfsvfs, B_TRUE);
795 		if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
796 			goto out;
797 		xattr_changed_cb(zfsvfs, pval);
798 		zfsvfs->z_issnap = B_TRUE;
799 	} else {
800 		error = zfsvfs_setup(zfsvfs, B_TRUE);
801 	}
802 
803 	if (!zfsvfs->z_issnap)
804 		zfsctl_create(zfsvfs);
805 out:
806 	if (error) {
807 		if (zfsvfs->z_os)
808 			dmu_objset_close(zfsvfs->z_os);
809 		mutex_destroy(&zfsvfs->z_znodes_lock);
810 		list_destroy(&zfsvfs->z_all_znodes);
811 		rrw_destroy(&zfsvfs->z_teardown_lock);
812 		rw_destroy(&zfsvfs->z_teardown_inactive_lock);
813 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
814 	} else {
815 		atomic_add_32(&zfs_active_fs_count, 1);
816 	}
817 
818 	return (error);
819 }
820 
821 void
822 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
823 {
824 	objset_t *os = zfsvfs->z_os;
825 	struct dsl_dataset *ds;
826 
827 	/*
828 	 * Unregister properties.
829 	 */
830 	if (!dmu_objset_is_snapshot(os)) {
831 		ds = dmu_objset_ds(os);
832 		VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
833 		    zfsvfs) == 0);
834 
835 		VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
836 		    zfsvfs) == 0);
837 
838 		VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
839 		    zfsvfs) == 0);
840 
841 		VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
842 		    zfsvfs) == 0);
843 
844 		VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
845 		    zfsvfs) == 0);
846 
847 		VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
848 		    zfsvfs) == 0);
849 
850 		VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
851 		    zfsvfs) == 0);
852 
853 		VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
854 		    zfsvfs) == 0);
855 
856 		VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
857 		    zfsvfs) == 0);
858 
859 		VERIFY(dsl_prop_unregister(ds, "aclinherit",
860 		    acl_inherit_changed_cb, zfsvfs) == 0);
861 
862 		VERIFY(dsl_prop_unregister(ds, "vscan",
863 		    vscan_changed_cb, zfsvfs) == 0);
864 	}
865 }
866 
867 /*
868  * Convert a decimal digit string to a uint64_t integer.
869  */
870 static int
871 str_to_uint64(char *str, uint64_t *objnum)
872 {
873 	uint64_t num = 0;
874 
875 	while (*str) {
876 		if (*str < '0' || *str > '9')
877 			return (EINVAL);
878 
879 		num = num*10 + *str++ - '0';
880 	}
881 
882 	*objnum = num;
883 	return (0);
884 }
885 
886 /*
887  * The boot path passed from the boot loader is in the form of
888  * "rootpool-name/root-filesystem-object-number'. Convert this
889  * string to a dataset name: "rootpool-name/root-filesystem-name".
890  */
891 static int
892 parse_bootpath(char *bpath, char *outpath)
893 {
894 	char *slashp;
895 	uint64_t objnum;
896 	int error;
897 
898 	if (*bpath == 0 || *bpath == '/')
899 		return (EINVAL);
900 
901 	slashp = strchr(bpath, '/');
902 
903 	/* if no '/', just return the pool name */
904 	if (slashp == NULL) {
905 		(void) strcpy(outpath, bpath);
906 		return (0);
907 	}
908 
909 	if (error = str_to_uint64(slashp+1, &objnum))
910 		return (error);
911 
912 	*slashp = '\0';
913 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
914 	*slashp = '/';
915 
916 	return (error);
917 }
918 
919 static int
920 zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
921 {
922 	int error = 0;
923 	int ret = 0;
924 	static int zfsrootdone = 0;
925 	zfsvfs_t *zfsvfs = NULL;
926 	znode_t *zp = NULL;
927 	vnode_t *vp = NULL;
928 	char *zfs_bootpath;
929 
930 	ASSERT(vfsp);
931 
932 	/*
933 	 * The filesystem that we mount as root is defined in the
934 	 * "zfs-bootfs" property.
935 	 */
936 	if (why == ROOT_INIT) {
937 		if (zfsrootdone++)
938 			return (EBUSY);
939 
940 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
941 		    DDI_PROP_DONTPASS, "zfs-bootfs", &zfs_bootpath) !=
942 		    DDI_SUCCESS)
943 			return (EIO);
944 
945 		error = parse_bootpath(zfs_bootpath, rootfs.bo_name);
946 		ddi_prop_free(zfs_bootpath);
947 
948 		if (error)
949 			return (error);
950 
951 		if (error = vfs_lock(vfsp))
952 			return (error);
953 
954 		if (error = zfs_domount(vfsp, rootfs.bo_name, CRED()))
955 			goto out;
956 
957 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
958 		ASSERT(zfsvfs);
959 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp))
960 			goto out;
961 
962 		vp = ZTOV(zp);
963 		mutex_enter(&vp->v_lock);
964 		vp->v_flag |= VROOT;
965 		mutex_exit(&vp->v_lock);
966 		rootvp = vp;
967 
968 		/*
969 		 * The zfs_zget call above returns with a hold on vp, we release
970 		 * it here.
971 		 */
972 		VN_RELE(vp);
973 
974 		/*
975 		 * Mount root as readonly initially, it will be remouted
976 		 * read/write by /lib/svc/method/fs-usr.
977 		 */
978 		readonly_changed_cb(vfsp->vfs_data, B_TRUE);
979 		vfs_add((struct vnode *)0, vfsp,
980 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
981 out:
982 		vfs_unlock(vfsp);
983 		ret = (error) ? error : 0;
984 		return (ret);
985 	} else if (why == ROOT_REMOUNT) {
986 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
987 		vfsp->vfs_flag |= VFS_REMOUNT;
988 
989 		/* refresh mount options */
990 		zfs_unregister_callbacks(vfsp->vfs_data);
991 		return (zfs_register_callbacks(vfsp));
992 
993 	} else if (why == ROOT_UNMOUNT) {
994 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
995 		(void) zfs_sync(vfsp, 0, 0);
996 		return (0);
997 	}
998 
999 	/*
1000 	 * if "why" is equal to anything else other than ROOT_INIT,
1001 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
1002 	 */
1003 	return (ENOTSUP);
1004 }
1005 
1006 /*ARGSUSED*/
1007 static int
1008 zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
1009 {
1010 	char		*osname;
1011 	pathname_t	spn;
1012 	int		error = 0;
1013 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
1014 	    UIO_SYSSPACE : UIO_USERSPACE;
1015 	int		canwrite;
1016 
1017 	if (mvp->v_type != VDIR)
1018 		return (ENOTDIR);
1019 
1020 	mutex_enter(&mvp->v_lock);
1021 	if ((uap->flags & MS_REMOUNT) == 0 &&
1022 	    (uap->flags & MS_OVERLAY) == 0 &&
1023 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
1024 		mutex_exit(&mvp->v_lock);
1025 		return (EBUSY);
1026 	}
1027 	mutex_exit(&mvp->v_lock);
1028 
1029 	/*
1030 	 * ZFS does not support passing unparsed data in via MS_DATA.
1031 	 * Users should use the MS_OPTIONSTR interface; this means
1032 	 * that all option parsing is already done and the options struct
1033 	 * can be interrogated.
1034 	 */
1035 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
1036 		return (EINVAL);
1037 
1038 	/*
1039 	 * Get the objset name (the "special" mount argument).
1040 	 */
1041 	if (error = pn_get(uap->spec, fromspace, &spn))
1042 		return (error);
1043 
1044 	osname = spn.pn_path;
1045 
1046 	/*
1047 	 * Check for mount privilege?
1048 	 *
1049 	 * If we don't have privilege then see if
1050 	 * we have local permission to allow it
1051 	 */
1052 	error = secpolicy_fs_mount(cr, mvp, vfsp);
1053 	if (error) {
1054 		error = dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr);
1055 		if (error == 0) {
1056 			vattr_t		vattr;
1057 
1058 			/*
1059 			 * Make sure user is the owner of the mount point
1060 			 * or has sufficient privileges.
1061 			 */
1062 
1063 			vattr.va_mask = AT_UID;
1064 
1065 			if (error = VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
1066 				goto out;
1067 			}
1068 
1069 			if (error = secpolicy_vnode_owner(cr, vattr.va_uid)) {
1070 				goto out;
1071 			}
1072 
1073 			if (error = VOP_ACCESS(mvp, VWRITE, 0, cr, NULL)) {
1074 				goto out;
1075 			}
1076 
1077 			secpolicy_fs_mount_clearopts(cr, vfsp);
1078 		} else {
1079 			goto out;
1080 		}
1081 	}
1082 
1083 	/*
1084 	 * Refuse to mount a filesystem if we are in a local zone and the
1085 	 * dataset is not visible.
1086 	 */
1087 	if (!INGLOBALZONE(curproc) &&
1088 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1089 		error = EPERM;
1090 		goto out;
1091 	}
1092 
1093 	/*
1094 	 * When doing a remount, we simply refresh our temporary properties
1095 	 * according to those options set in the current VFS options.
1096 	 */
1097 	if (uap->flags & MS_REMOUNT) {
1098 		/* refresh mount options */
1099 		zfs_unregister_callbacks(vfsp->vfs_data);
1100 		error = zfs_register_callbacks(vfsp);
1101 		goto out;
1102 	}
1103 
1104 	error = zfs_domount(vfsp, osname, cr);
1105 
1106 out:
1107 	pn_free(&spn);
1108 	return (error);
1109 }
1110 
1111 static int
1112 zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1113 {
1114 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1115 	dev32_t d32;
1116 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
1117 
1118 	ZFS_ENTER(zfsvfs);
1119 
1120 	dmu_objset_space(zfsvfs->z_os,
1121 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
1122 
1123 	/*
1124 	 * The underlying storage pool actually uses multiple block sizes.
1125 	 * We report the fragsize as the smallest block size we support,
1126 	 * and we report our blocksize as the filesystem's maximum blocksize.
1127 	 */
1128 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1129 	statp->f_bsize = zfsvfs->z_max_blksz;
1130 
1131 	/*
1132 	 * The following report "total" blocks of various kinds in the
1133 	 * file system, but reported in terms of f_frsize - the
1134 	 * "fragment" size.
1135 	 */
1136 
1137 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
1138 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
1139 	statp->f_bavail = statp->f_bfree; /* no root reservation */
1140 
1141 	/*
1142 	 * statvfs() should really be called statufs(), because it assumes
1143 	 * static metadata.  ZFS doesn't preallocate files, so the best
1144 	 * we can do is report the max that could possibly fit in f_files,
1145 	 * and that minus the number actually used in f_ffree.
1146 	 * For f_ffree, report the smaller of the number of object available
1147 	 * and the number of blocks (each object will take at least a block).
1148 	 */
1149 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
1150 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
1151 	statp->f_files = statp->f_ffree + usedobjs;
1152 
1153 	(void) cmpldev(&d32, vfsp->vfs_dev);
1154 	statp->f_fsid = d32;
1155 
1156 	/*
1157 	 * We're a zfs filesystem.
1158 	 */
1159 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1160 
1161 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1162 
1163 	statp->f_namemax = ZFS_MAXNAMELEN;
1164 
1165 	/*
1166 	 * We have all of 32 characters to stuff a string here.
1167 	 * Is there anything useful we could/should provide?
1168 	 */
1169 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
1170 
1171 	ZFS_EXIT(zfsvfs);
1172 	return (0);
1173 }
1174 
1175 static int
1176 zfs_root(vfs_t *vfsp, vnode_t **vpp)
1177 {
1178 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1179 	znode_t *rootzp;
1180 	int error;
1181 
1182 	ZFS_ENTER(zfsvfs);
1183 
1184 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1185 	if (error == 0)
1186 		*vpp = ZTOV(rootzp);
1187 
1188 	ZFS_EXIT(zfsvfs);
1189 	return (error);
1190 }
1191 
1192 /*
1193  * Teardown the zfsvfs::z_os.
1194  *
1195  * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1196  * and 'z_teardown_inactive_lock' held.
1197  */
1198 static int
1199 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1200 {
1201 	objset_t *os = zfsvfs->z_os;
1202 	znode_t	*zp, *nextzp;
1203 	znode_t markerzp;
1204 
1205 	rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1206 
1207 	if (!unmounting) {
1208 		/*
1209 		 * We purge the parent filesystem's vfsp as the parent
1210 		 * filesystem and all of its snapshots have their vnode's
1211 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
1212 		 * 'z_parent' is self referential for non-snapshots.
1213 		 */
1214 		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1215 	}
1216 
1217 	/*
1218 	 * Close the zil. NB: Can't close the zil while zfs_inactive
1219 	 * threads are blocked as zil_close can call zfs_inactive.
1220 	 */
1221 	if (zfsvfs->z_log) {
1222 		zil_close(zfsvfs->z_log);
1223 		zfsvfs->z_log = NULL;
1224 	}
1225 
1226 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
1227 
1228 	/*
1229 	 * If we are not unmounting (ie: online recv) and someone already
1230 	 * unmounted this file system while we were doing the switcheroo,
1231 	 * or a reopen of z_os failed then just bail out now.
1232 	 */
1233 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1234 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
1235 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1236 		return (EIO);
1237 	}
1238 
1239 	/*
1240 	 * At this point there are no vops active, and any new vops will
1241 	 * fail with EIO since we have z_teardown_lock for writer (only
1242 	 * relavent for forced unmount).
1243 	 *
1244 	 * Release all holds on dbufs.
1245 	 * Note, the dmu can still callback via znode_pageout_func()
1246 	 * which can zfs_znode_free() the znode.  So we lock
1247 	 * z_all_znodes; search the list for a held dbuf; drop the lock
1248 	 * (we know zp can't disappear if we hold a dbuf lock) then
1249 	 * regrab the lock and restart.
1250 	 *
1251 	 * Since we have to restart the search after finding each held dbuf,
1252 	 * we do two things to speed up searching: we insert a dummy znode
1253 	 * ('markerzp') to detect the original tail of the list, and move
1254 	 * non-held znodes to the end of the list.  Once we hit 'markerzp',
1255 	 * we know we've looked at each znode and can break out.
1256 	 */
1257 	mutex_enter(&zfsvfs->z_znodes_lock);
1258 	list_insert_tail(&zfsvfs->z_all_znodes, &markerzp);
1259 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != &markerzp;
1260 	    zp = nextzp) {
1261 		nextzp = list_next(&zfsvfs->z_all_znodes, zp);
1262 		if (zp->z_dbuf) {
1263 			/* dbufs should only be held when force unmounting */
1264 			mutex_exit(&zfsvfs->z_znodes_lock);
1265 			dmu_buf_rele(zp->z_dbuf, NULL);
1266 			zp->z_dbuf = NULL;
1267 			mutex_enter(&zfsvfs->z_znodes_lock);
1268 			/* Start again */
1269 			nextzp = list_head(&zfsvfs->z_all_znodes);
1270 		} else {
1271 			list_remove(&zfsvfs->z_all_znodes, zp);
1272 			list_insert_tail(&zfsvfs->z_all_znodes, zp);
1273 		}
1274 	}
1275 	list_remove(&zfsvfs->z_all_znodes, &markerzp);
1276 	mutex_exit(&zfsvfs->z_znodes_lock);
1277 
1278 	/*
1279 	 * If we are unmounting, set the unmounted flag and let new vops
1280 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
1281 	 * other vops will fail with EIO.
1282 	 */
1283 	if (unmounting) {
1284 		zfsvfs->z_unmounted = B_TRUE;
1285 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1286 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
1287 	}
1288 
1289 	/*
1290 	 * z_os will be NULL if there was an error in attempting to reopen
1291 	 * zfsvfs, so just return as the properties had already been
1292 	 * unregistered and cached data had been evicted before.
1293 	 */
1294 	if (zfsvfs->z_os == NULL)
1295 		return (0);
1296 
1297 	/*
1298 	 * Unregister properties.
1299 	 */
1300 	zfs_unregister_callbacks(zfsvfs);
1301 
1302 	/*
1303 	 * Evict cached data
1304 	 */
1305 	if (dmu_objset_evict_dbufs(os)) {
1306 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1307 		(void) dmu_objset_evict_dbufs(os);
1308 	}
1309 
1310 	return (0);
1311 }
1312 
1313 /*ARGSUSED*/
1314 static int
1315 zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1316 {
1317 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1318 	objset_t *os;
1319 	int ret;
1320 
1321 	ret = secpolicy_fs_unmount(cr, vfsp);
1322 	if (ret) {
1323 		ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
1324 		    ZFS_DELEG_PERM_MOUNT, cr);
1325 		if (ret)
1326 			return (ret);
1327 	}
1328 
1329 	/*
1330 	 * We purge the parent filesystem's vfsp as the parent filesystem
1331 	 * and all of its snapshots have their vnode's v_vfsp set to the
1332 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
1333 	 * referential for non-snapshots.
1334 	 */
1335 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1336 
1337 	/*
1338 	 * Unmount any snapshots mounted under .zfs before unmounting the
1339 	 * dataset itself.
1340 	 */
1341 	if (zfsvfs->z_ctldir != NULL &&
1342 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1343 		return (ret);
1344 	}
1345 
1346 	if (!(fflag & MS_FORCE)) {
1347 		/*
1348 		 * Check the number of active vnodes in the file system.
1349 		 * Our count is maintained in the vfs structure, but the
1350 		 * number is off by 1 to indicate a hold on the vfs
1351 		 * structure itself.
1352 		 *
1353 		 * The '.zfs' directory maintains a reference of its
1354 		 * own, and any active references underneath are
1355 		 * reflected in the vnode count.
1356 		 */
1357 		if (zfsvfs->z_ctldir == NULL) {
1358 			if (vfsp->vfs_count > 1)
1359 				return (EBUSY);
1360 		} else {
1361 			if (vfsp->vfs_count > 2 ||
1362 			    zfsvfs->z_ctldir->v_count > 1)
1363 				return (EBUSY);
1364 		}
1365 	}
1366 
1367 	vfsp->vfs_flag |= VFS_UNMOUNTED;
1368 
1369 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1370 	os = zfsvfs->z_os;
1371 
1372 	/*
1373 	 * z_os will be NULL if there was an error in
1374 	 * attempting to reopen zfsvfs.
1375 	 */
1376 	if (os != NULL) {
1377 		/*
1378 		 * Unset the objset user_ptr.
1379 		 */
1380 		mutex_enter(&os->os->os_user_ptr_lock);
1381 		dmu_objset_set_user(os, NULL);
1382 		mutex_exit(&os->os->os_user_ptr_lock);
1383 
1384 		/*
1385 		 * Finally close the objset
1386 		 */
1387 		dmu_objset_close(os);
1388 	}
1389 
1390 	/*
1391 	 * We can now safely destroy the '.zfs' directory node.
1392 	 */
1393 	if (zfsvfs->z_ctldir != NULL)
1394 		zfsctl_destroy(zfsvfs);
1395 
1396 	return (0);
1397 }
1398 
1399 static int
1400 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1401 {
1402 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1403 	znode_t		*zp;
1404 	uint64_t	object = 0;
1405 	uint64_t	fid_gen = 0;
1406 	uint64_t	gen_mask;
1407 	uint64_t	zp_gen;
1408 	int 		i, err;
1409 
1410 	*vpp = NULL;
1411 
1412 	ZFS_ENTER(zfsvfs);
1413 
1414 	if (fidp->fid_len == LONG_FID_LEN) {
1415 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1416 		uint64_t	objsetid = 0;
1417 		uint64_t	setgen = 0;
1418 
1419 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1420 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1421 
1422 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1423 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1424 
1425 		ZFS_EXIT(zfsvfs);
1426 
1427 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1428 		if (err)
1429 			return (EINVAL);
1430 		ZFS_ENTER(zfsvfs);
1431 	}
1432 
1433 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1434 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1435 
1436 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1437 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1438 
1439 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1440 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1441 	} else {
1442 		ZFS_EXIT(zfsvfs);
1443 		return (EINVAL);
1444 	}
1445 
1446 	/* A zero fid_gen means we are in the .zfs control directories */
1447 	if (fid_gen == 0 &&
1448 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1449 		*vpp = zfsvfs->z_ctldir;
1450 		ASSERT(*vpp != NULL);
1451 		if (object == ZFSCTL_INO_SNAPDIR) {
1452 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1453 			    0, NULL, NULL, NULL, NULL, NULL) == 0);
1454 		} else {
1455 			VN_HOLD(*vpp);
1456 		}
1457 		ZFS_EXIT(zfsvfs);
1458 		return (0);
1459 	}
1460 
1461 	gen_mask = -1ULL >> (64 - 8 * i);
1462 
1463 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1464 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1465 		ZFS_EXIT(zfsvfs);
1466 		return (err);
1467 	}
1468 	zp_gen = zp->z_phys->zp_gen & gen_mask;
1469 	if (zp_gen == 0)
1470 		zp_gen = 1;
1471 	if (zp->z_unlinked || zp_gen != fid_gen) {
1472 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1473 		VN_RELE(ZTOV(zp));
1474 		ZFS_EXIT(zfsvfs);
1475 		return (EINVAL);
1476 	}
1477 
1478 	*vpp = ZTOV(zp);
1479 	ZFS_EXIT(zfsvfs);
1480 	return (0);
1481 }
1482 
1483 /*
1484  * Block out VOPs and close zfsvfs_t::z_os
1485  *
1486  * Note, if successful, then we return with the 'z_teardown_lock' and
1487  * 'z_teardown_inactive_lock' write held.
1488  */
1489 int
1490 zfs_suspend_fs(zfsvfs_t *zfsvfs, char *name, int *mode)
1491 {
1492 	int error;
1493 
1494 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
1495 		return (error);
1496 
1497 	*mode = zfsvfs->z_os->os_mode;
1498 	dmu_objset_name(zfsvfs->z_os, name);
1499 	dmu_objset_close(zfsvfs->z_os);
1500 
1501 	return (0);
1502 }
1503 
1504 /*
1505  * Reopen zfsvfs_t::z_os and release VOPs.
1506  */
1507 int
1508 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname, int mode)
1509 {
1510 	int err;
1511 
1512 	ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
1513 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
1514 
1515 	err = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
1516 	if (err) {
1517 		zfsvfs->z_os = NULL;
1518 	} else {
1519 		znode_t *zp;
1520 
1521 		VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
1522 
1523 		/*
1524 		 * Attempt to re-establish all the active znodes with
1525 		 * their dbufs.  If a zfs_rezget() fails, then we'll let
1526 		 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
1527 		 * when they try to use their znode.
1528 		 */
1529 		mutex_enter(&zfsvfs->z_znodes_lock);
1530 		for (zp = list_head(&zfsvfs->z_all_znodes); zp;
1531 		    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1532 			(void) zfs_rezget(zp);
1533 		}
1534 		mutex_exit(&zfsvfs->z_znodes_lock);
1535 
1536 	}
1537 
1538 	/* release the VOPs */
1539 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
1540 	rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1541 
1542 	if (err) {
1543 		/*
1544 		 * Since we couldn't reopen zfsvfs::z_os, force
1545 		 * unmount this file system.
1546 		 */
1547 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
1548 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
1549 	}
1550 	return (err);
1551 }
1552 
1553 static void
1554 zfs_freevfs(vfs_t *vfsp)
1555 {
1556 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1557 	int i;
1558 
1559 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1560 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1561 
1562 	mutex_destroy(&zfsvfs->z_znodes_lock);
1563 	list_destroy(&zfsvfs->z_all_znodes);
1564 	rrw_destroy(&zfsvfs->z_teardown_lock);
1565 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
1566 	zfs_fuid_destroy(zfsvfs);
1567 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1568 
1569 	atomic_add_32(&zfs_active_fs_count, -1);
1570 }
1571 
1572 /*
1573  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
1574  * so we can't safely do any non-idempotent initialization here.
1575  * Leave that to zfs_init() and zfs_fini(), which are called
1576  * from the module's _init() and _fini() entry points.
1577  */
1578 /*ARGSUSED*/
1579 static int
1580 zfs_vfsinit(int fstype, char *name)
1581 {
1582 	int error;
1583 
1584 	zfsfstype = fstype;
1585 
1586 	/*
1587 	 * Setup vfsops and vnodeops tables.
1588 	 */
1589 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
1590 	if (error != 0) {
1591 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
1592 	}
1593 
1594 	error = zfs_create_op_tables();
1595 	if (error) {
1596 		zfs_remove_op_tables();
1597 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
1598 		(void) vfs_freevfsops_by_type(zfsfstype);
1599 		return (error);
1600 	}
1601 
1602 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
1603 
1604 	/*
1605 	 * Unique major number for all zfs mounts.
1606 	 * If we run out of 32-bit minors, we'll getudev() another major.
1607 	 */
1608 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
1609 	zfs_minor = ZFS_MIN_MINOR;
1610 
1611 	return (0);
1612 }
1613 
1614 void
1615 zfs_init(void)
1616 {
1617 	/*
1618 	 * Initialize .zfs directory structures
1619 	 */
1620 	zfsctl_init();
1621 
1622 	/*
1623 	 * Initialize znode cache, vnode ops, etc...
1624 	 */
1625 	zfs_znode_init();
1626 }
1627 
1628 void
1629 zfs_fini(void)
1630 {
1631 	zfsctl_fini();
1632 	zfs_znode_fini();
1633 }
1634 
1635 int
1636 zfs_busy(void)
1637 {
1638 	return (zfs_active_fs_count != 0);
1639 }
1640 
1641 int
1642 zfs_get_version(objset_t *os, uint64_t *version)
1643 {
1644 	int error;
1645 
1646 	error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1, version);
1647 	return (error);
1648 }
1649 
1650 int
1651 zfs_set_version(const char *name, uint64_t newvers)
1652 {
1653 	int error;
1654 	objset_t *os;
1655 	dmu_tx_t *tx;
1656 	uint64_t curvers;
1657 
1658 	/*
1659 	 * XXX for now, require that the filesystem be unmounted.  Would
1660 	 * be nice to find the zfsvfs_t and just update that if
1661 	 * possible.
1662 	 */
1663 
1664 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
1665 		return (EINVAL);
1666 
1667 	error = dmu_objset_open(name, DMU_OST_ZFS, DS_MODE_PRIMARY, &os);
1668 	if (error)
1669 		return (error);
1670 
1671 	error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1672 	    8, 1, &curvers);
1673 	if (error)
1674 		goto out;
1675 	if (newvers < curvers) {
1676 		error = EINVAL;
1677 		goto out;
1678 	}
1679 
1680 	tx = dmu_tx_create(os);
1681 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 0, ZPL_VERSION_STR);
1682 	error = dmu_tx_assign(tx, TXG_WAIT);
1683 	if (error) {
1684 		dmu_tx_abort(tx);
1685 		goto out;
1686 	}
1687 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1,
1688 	    &newvers, tx);
1689 
1690 	spa_history_internal_log(LOG_DS_UPGRADE,
1691 	    dmu_objset_spa(os), tx, CRED(),
1692 	    "oldver=%llu newver=%llu dataset = %llu", curvers, newvers,
1693 	    dmu_objset_id(os));
1694 	dmu_tx_commit(tx);
1695 
1696 out:
1697 	dmu_objset_close(os);
1698 	return (error);
1699 }
1700 
1701 static vfsdef_t vfw = {
1702 	VFSDEF_VERSION,
1703 	MNTTYPE_ZFS,
1704 	zfs_vfsinit,
1705 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS|
1706 	    VSW_XID,
1707 	&zfs_mntopts
1708 };
1709 
1710 struct modlfs zfs_modlfs = {
1711 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
1712 };
1713