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