xref: /titanic_50/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision e4b86885570d77af552e9cf94f142f4d744fb8c8)
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 2008 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 #include <sys/spa_boot.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_register_callbacks(vfs_t *vfsp)
389 {
390 	struct dsl_dataset *ds = NULL;
391 	objset_t *os = NULL;
392 	zfsvfs_t *zfsvfs = NULL;
393 	uint64_t nbmand;
394 	int readonly, do_readonly = B_FALSE;
395 	int setuid, do_setuid = B_FALSE;
396 	int exec, do_exec = B_FALSE;
397 	int devices, do_devices = B_FALSE;
398 	int xattr, do_xattr = B_FALSE;
399 	int atime, do_atime = B_FALSE;
400 	int error = 0;
401 
402 	ASSERT(vfsp);
403 	zfsvfs = vfsp->vfs_data;
404 	ASSERT(zfsvfs);
405 	os = zfsvfs->z_os;
406 
407 	/*
408 	 * The act of registering our callbacks will destroy any mount
409 	 * options we may have.  In order to enable temporary overrides
410 	 * of mount options, we stash away the current values and
411 	 * restore them after we register the callbacks.
412 	 */
413 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
414 		readonly = B_TRUE;
415 		do_readonly = B_TRUE;
416 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
417 		readonly = B_FALSE;
418 		do_readonly = B_TRUE;
419 	}
420 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
421 		devices = B_FALSE;
422 		setuid = B_FALSE;
423 		do_devices = B_TRUE;
424 		do_setuid = B_TRUE;
425 	} else {
426 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
427 			devices = B_FALSE;
428 			do_devices = B_TRUE;
429 		} else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
430 			devices = B_TRUE;
431 			do_devices = B_TRUE;
432 		}
433 
434 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
435 			setuid = B_FALSE;
436 			do_setuid = B_TRUE;
437 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
438 			setuid = B_TRUE;
439 			do_setuid = B_TRUE;
440 		}
441 	}
442 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
443 		exec = B_FALSE;
444 		do_exec = B_TRUE;
445 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
446 		exec = B_TRUE;
447 		do_exec = B_TRUE;
448 	}
449 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
450 		xattr = B_FALSE;
451 		do_xattr = B_TRUE;
452 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
453 		xattr = B_TRUE;
454 		do_xattr = B_TRUE;
455 	}
456 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
457 		atime = B_FALSE;
458 		do_atime = B_TRUE;
459 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
460 		atime = B_TRUE;
461 		do_atime = B_TRUE;
462 	}
463 
464 	/*
465 	 * nbmand is a special property.  It can only be changed at
466 	 * mount time.
467 	 *
468 	 * This is weird, but it is documented to only be changeable
469 	 * at mount time.
470 	 */
471 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
472 		nbmand = B_FALSE;
473 	} else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
474 		nbmand = B_TRUE;
475 	} else {
476 		char osname[MAXNAMELEN];
477 
478 		dmu_objset_name(os, osname);
479 		if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
480 		    NULL)) {
481 			return (error);
482 		}
483 	}
484 
485 	/*
486 	 * Register property callbacks.
487 	 *
488 	 * It would probably be fine to just check for i/o error from
489 	 * the first prop_register(), but I guess I like to go
490 	 * overboard...
491 	 */
492 	ds = dmu_objset_ds(os);
493 	error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
494 	error = error ? error : dsl_prop_register(ds,
495 	    "xattr", xattr_changed_cb, zfsvfs);
496 	error = error ? error : dsl_prop_register(ds,
497 	    "recordsize", blksz_changed_cb, zfsvfs);
498 	error = error ? error : dsl_prop_register(ds,
499 	    "readonly", readonly_changed_cb, zfsvfs);
500 	error = error ? error : dsl_prop_register(ds,
501 	    "devices", devices_changed_cb, zfsvfs);
502 	error = error ? error : dsl_prop_register(ds,
503 	    "setuid", setuid_changed_cb, zfsvfs);
504 	error = error ? error : dsl_prop_register(ds,
505 	    "exec", exec_changed_cb, zfsvfs);
506 	error = error ? error : dsl_prop_register(ds,
507 	    "snapdir", snapdir_changed_cb, zfsvfs);
508 	error = error ? error : dsl_prop_register(ds,
509 	    "aclmode", acl_mode_changed_cb, zfsvfs);
510 	error = error ? error : dsl_prop_register(ds,
511 	    "aclinherit", acl_inherit_changed_cb, zfsvfs);
512 	error = error ? error : dsl_prop_register(ds,
513 	    "vscan", vscan_changed_cb, zfsvfs);
514 	if (error)
515 		goto unregister;
516 
517 	/*
518 	 * Invoke our callbacks to restore temporary mount options.
519 	 */
520 	if (do_readonly)
521 		readonly_changed_cb(zfsvfs, readonly);
522 	if (do_setuid)
523 		setuid_changed_cb(zfsvfs, setuid);
524 	if (do_exec)
525 		exec_changed_cb(zfsvfs, exec);
526 	if (do_devices)
527 		devices_changed_cb(zfsvfs, devices);
528 	if (do_xattr)
529 		xattr_changed_cb(zfsvfs, xattr);
530 	if (do_atime)
531 		atime_changed_cb(zfsvfs, atime);
532 
533 	nbmand_changed_cb(zfsvfs, nbmand);
534 
535 	return (0);
536 
537 unregister:
538 	/*
539 	 * We may attempt to unregister some callbacks that are not
540 	 * registered, but this is OK; it will simply return ENOMSG,
541 	 * which we will ignore.
542 	 */
543 	(void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
544 	(void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
545 	(void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
546 	(void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
547 	(void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs);
548 	(void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
549 	(void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
550 	(void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
551 	(void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
552 	(void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
553 	    zfsvfs);
554 	(void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
555 	return (error);
556 
557 }
558 
559 static int
560 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
561 {
562 	uint_t readonly;
563 	int error;
564 
565 	error = zfs_register_callbacks(zfsvfs->z_vfs);
566 	if (error)
567 		return (error);
568 
569 	/*
570 	 * Set the objset user_ptr to track its zfsvfs.
571 	 */
572 	mutex_enter(&zfsvfs->z_os->os->os_user_ptr_lock);
573 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
574 	mutex_exit(&zfsvfs->z_os->os->os_user_ptr_lock);
575 
576 	/*
577 	 * If we are not mounting (ie: online recv), then we don't
578 	 * have to worry about replaying the log as we blocked all
579 	 * operations out since we closed the ZIL.
580 	 */
581 	if (mounting) {
582 		/*
583 		 * During replay we remove the read only flag to
584 		 * allow replays to succeed.
585 		 */
586 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
587 		if (readonly != 0)
588 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
589 		else
590 			zfs_unlinked_drain(zfsvfs);
591 
592 		/*
593 		 * Parse and replay the intent log.
594 		 *
595 		 * Because of ziltest, this must be done after
596 		 * zfs_unlinked_drain().  (Further note: ziltest doesn't
597 		 * use readonly mounts, where zfs_unlinked_drain() isn't
598 		 * called.)  This is because ziltest causes spa_sync()
599 		 * to think it's committed, but actually it is not, so
600 		 * the intent log contains many txg's worth of changes.
601 		 *
602 		 * In particular, if object N is in the unlinked set in
603 		 * the last txg to actually sync, then it could be
604 		 * actually freed in a later txg and then reallocated in
605 		 * a yet later txg.  This would write a "create object
606 		 * N" record to the intent log.  Normally, this would be
607 		 * fine because the spa_sync() would have written out
608 		 * the fact that object N is free, before we could write
609 		 * the "create object N" intent log record.
610 		 *
611 		 * But when we are in ziltest mode, we advance the "open
612 		 * txg" without actually spa_sync()-ing the changes to
613 		 * disk.  So we would see that object N is still
614 		 * allocated and in the unlinked set, and there is an
615 		 * intent log record saying to allocate it.
616 		 */
617 		zil_replay(zfsvfs->z_os, zfsvfs, &zfsvfs->z_assign,
618 		    zfs_replay_vector);
619 
620 		zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
621 	}
622 
623 	if (!zil_disable)
624 		zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
625 
626 	return (0);
627 }
628 
629 static void
630 zfs_freezfsvfs(zfsvfs_t *zfsvfs)
631 {
632 	mutex_destroy(&zfsvfs->z_znodes_lock);
633 	mutex_destroy(&zfsvfs->z_online_recv_lock);
634 	list_destroy(&zfsvfs->z_all_znodes);
635 	rrw_destroy(&zfsvfs->z_teardown_lock);
636 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
637 	rw_destroy(&zfsvfs->z_fuid_lock);
638 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
639 }
640 
641 static int
642 zfs_domount(vfs_t *vfsp, char *osname)
643 {
644 	dev_t mount_dev;
645 	uint64_t recordsize, readonly;
646 	int error = 0;
647 	int mode;
648 	zfsvfs_t *zfsvfs;
649 	znode_t *zp = NULL;
650 
651 	ASSERT(vfsp);
652 	ASSERT(osname);
653 
654 	/*
655 	 * Initialize the zfs-specific filesystem structure.
656 	 * Should probably make this a kmem cache, shuffle fields,
657 	 * and just bzero up to z_hold_mtx[].
658 	 */
659 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
660 	zfsvfs->z_vfs = vfsp;
661 	zfsvfs->z_parent = zfsvfs;
662 	zfsvfs->z_assign = TXG_NOWAIT;
663 	zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
664 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
665 
666 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
667 	mutex_init(&zfsvfs->z_online_recv_lock, NULL, MUTEX_DEFAULT, NULL);
668 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
669 	    offsetof(znode_t, z_link_node));
670 	rrw_init(&zfsvfs->z_teardown_lock);
671 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
672 	rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
673 
674 	/* Initialize the generic filesystem structure. */
675 	vfsp->vfs_bcount = 0;
676 	vfsp->vfs_data = NULL;
677 
678 	if (zfs_create_unique_device(&mount_dev) == -1) {
679 		error = ENODEV;
680 		goto out;
681 	}
682 	ASSERT(vfs_devismounted(mount_dev) == 0);
683 
684 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
685 	    NULL))
686 		goto out;
687 
688 	vfsp->vfs_dev = mount_dev;
689 	vfsp->vfs_fstype = zfsfstype;
690 	vfsp->vfs_bsize = recordsize;
691 	vfsp->vfs_flag |= VFS_NOTRUNC;
692 	vfsp->vfs_data = zfsvfs;
693 
694 	if (error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL))
695 		goto out;
696 
697 	mode = DS_MODE_OWNER;
698 	if (readonly)
699 		mode |= DS_MODE_READONLY;
700 
701 	error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
702 	if (error == EROFS) {
703 		mode = DS_MODE_OWNER | DS_MODE_READONLY;
704 		error = dmu_objset_open(osname, DMU_OST_ZFS, mode,
705 		    &zfsvfs->z_os);
706 	}
707 
708 	if (error)
709 		goto out;
710 
711 	if (error = zfs_init_fs(zfsvfs, &zp))
712 		goto out;
713 
714 	/* The call to zfs_init_fs leaves the vnode held, release it here. */
715 	VN_RELE(ZTOV(zp));
716 
717 	/*
718 	 * Set features for file system.
719 	 */
720 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
721 	if (zfsvfs->z_use_fuids) {
722 		vfs_set_feature(vfsp, VFSFT_XVATTR);
723 		vfs_set_feature(vfsp, VFSFT_ACEMASKONACCESS);
724 		vfs_set_feature(vfsp, VFSFT_ACLONCREATE);
725 	}
726 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
727 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
728 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
729 		vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
730 	} else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
731 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
732 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
733 	}
734 
735 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
736 		uint64_t pval;
737 
738 		ASSERT(mode & DS_MODE_READONLY);
739 		atime_changed_cb(zfsvfs, B_FALSE);
740 		readonly_changed_cb(zfsvfs, B_TRUE);
741 		if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
742 			goto out;
743 		xattr_changed_cb(zfsvfs, pval);
744 		zfsvfs->z_issnap = B_TRUE;
745 	} else {
746 		error = zfsvfs_setup(zfsvfs, B_TRUE);
747 	}
748 
749 	if (!zfsvfs->z_issnap)
750 		zfsctl_create(zfsvfs);
751 out:
752 	if (error) {
753 		if (zfsvfs->z_os)
754 			dmu_objset_close(zfsvfs->z_os);
755 		zfs_freezfsvfs(zfsvfs);
756 	} else {
757 		atomic_add_32(&zfs_active_fs_count, 1);
758 	}
759 
760 	return (error);
761 }
762 
763 void
764 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
765 {
766 	objset_t *os = zfsvfs->z_os;
767 	struct dsl_dataset *ds;
768 
769 	/*
770 	 * Unregister properties.
771 	 */
772 	if (!dmu_objset_is_snapshot(os)) {
773 		ds = dmu_objset_ds(os);
774 		VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
775 		    zfsvfs) == 0);
776 
777 		VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
778 		    zfsvfs) == 0);
779 
780 		VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
781 		    zfsvfs) == 0);
782 
783 		VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
784 		    zfsvfs) == 0);
785 
786 		VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
787 		    zfsvfs) == 0);
788 
789 		VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
790 		    zfsvfs) == 0);
791 
792 		VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
793 		    zfsvfs) == 0);
794 
795 		VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
796 		    zfsvfs) == 0);
797 
798 		VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
799 		    zfsvfs) == 0);
800 
801 		VERIFY(dsl_prop_unregister(ds, "aclinherit",
802 		    acl_inherit_changed_cb, zfsvfs) == 0);
803 
804 		VERIFY(dsl_prop_unregister(ds, "vscan",
805 		    vscan_changed_cb, zfsvfs) == 0);
806 	}
807 }
808 
809 /*
810  * Convert a decimal digit string to a uint64_t integer.
811  */
812 static int
813 str_to_uint64(char *str, uint64_t *objnum)
814 {
815 	uint64_t num = 0;
816 
817 	while (*str) {
818 		if (*str < '0' || *str > '9')
819 			return (EINVAL);
820 
821 		num = num*10 + *str++ - '0';
822 	}
823 
824 	*objnum = num;
825 	return (0);
826 }
827 
828 /*
829  * The boot path passed from the boot loader is in the form of
830  * "rootpool-name/root-filesystem-object-number'. Convert this
831  * string to a dataset name: "rootpool-name/root-filesystem-name".
832  */
833 static int
834 zfs_parse_bootfs(char *bpath, char *outpath)
835 {
836 	char *slashp;
837 	uint64_t objnum;
838 	int error;
839 
840 	if (*bpath == 0 || *bpath == '/')
841 		return (EINVAL);
842 
843 	slashp = strchr(bpath, '/');
844 
845 	/* if no '/', just return the pool name */
846 	if (slashp == NULL) {
847 		(void) strcpy(outpath, bpath);
848 		return (0);
849 	}
850 
851 	if (error = str_to_uint64(slashp+1, &objnum))
852 		return (error);
853 
854 	*slashp = '\0';
855 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
856 	*slashp = '/';
857 
858 	return (error);
859 }
860 
861 static int
862 zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
863 {
864 	int error = 0;
865 	static int zfsrootdone = 0;
866 	zfsvfs_t *zfsvfs = NULL;
867 	znode_t *zp = NULL;
868 	vnode_t *vp = NULL;
869 	char *zfs_bootfs;
870 	char *zfs_devid;
871 
872 	ASSERT(vfsp);
873 
874 	/*
875 	 * The filesystem that we mount as root is defined in the
876 	 * boot property "zfs-bootfs" with a format of
877 	 * "poolname/root-dataset-objnum".
878 	 */
879 	if (why == ROOT_INIT) {
880 		if (zfsrootdone++)
881 			return (EBUSY);
882 		/*
883 		 * the process of doing a spa_load will require the
884 		 * clock to be set before we could (for example) do
885 		 * something better by looking at the timestamp on
886 		 * an uberblock, so just set it to -1.
887 		 */
888 		clkset(-1);
889 
890 		if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
891 			cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
892 			    "bootfs name");
893 			return (EINVAL);
894 		}
895 		zfs_devid = spa_get_bootprop("diskdevid");
896 		error = spa_import_rootpool(rootfs.bo_name, zfs_devid);
897 		if (zfs_devid)
898 			spa_free_bootprop(zfs_devid);
899 		if (error) {
900 			spa_free_bootprop(zfs_bootfs);
901 			cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
902 			    error);
903 			return (error);
904 		}
905 		if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
906 			spa_free_bootprop(zfs_bootfs);
907 			cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
908 			    error);
909 			return (error);
910 		}
911 
912 		spa_free_bootprop(zfs_bootfs);
913 
914 		if (error = vfs_lock(vfsp))
915 			return (error);
916 
917 		if (error = zfs_domount(vfsp, rootfs.bo_name)) {
918 			cmn_err(CE_NOTE, "zfs_domount: error %d", error);
919 			goto out;
920 		}
921 
922 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
923 		ASSERT(zfsvfs);
924 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
925 			cmn_err(CE_NOTE, "zfs_zget: error %d", error);
926 			goto out;
927 		}
928 
929 		vp = ZTOV(zp);
930 		mutex_enter(&vp->v_lock);
931 		vp->v_flag |= VROOT;
932 		mutex_exit(&vp->v_lock);
933 		rootvp = vp;
934 
935 		/*
936 		 * Leave rootvp held.  The root file system is never unmounted.
937 		 */
938 
939 		vfs_add((struct vnode *)0, vfsp,
940 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
941 out:
942 		vfs_unlock(vfsp);
943 		return (error);
944 	} else if (why == ROOT_REMOUNT) {
945 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
946 		vfsp->vfs_flag |= VFS_REMOUNT;
947 
948 		/* refresh mount options */
949 		zfs_unregister_callbacks(vfsp->vfs_data);
950 		return (zfs_register_callbacks(vfsp));
951 
952 	} else if (why == ROOT_UNMOUNT) {
953 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
954 		(void) zfs_sync(vfsp, 0, 0);
955 		return (0);
956 	}
957 
958 	/*
959 	 * if "why" is equal to anything else other than ROOT_INIT,
960 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
961 	 */
962 	return (ENOTSUP);
963 }
964 
965 /*ARGSUSED*/
966 static int
967 zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
968 {
969 	char		*osname;
970 	pathname_t	spn;
971 	int		error = 0;
972 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
973 	    UIO_SYSSPACE : UIO_USERSPACE;
974 	int		canwrite;
975 
976 	if (mvp->v_type != VDIR)
977 		return (ENOTDIR);
978 
979 	mutex_enter(&mvp->v_lock);
980 	if ((uap->flags & MS_REMOUNT) == 0 &&
981 	    (uap->flags & MS_OVERLAY) == 0 &&
982 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
983 		mutex_exit(&mvp->v_lock);
984 		return (EBUSY);
985 	}
986 	mutex_exit(&mvp->v_lock);
987 
988 	/*
989 	 * ZFS does not support passing unparsed data in via MS_DATA.
990 	 * Users should use the MS_OPTIONSTR interface; this means
991 	 * that all option parsing is already done and the options struct
992 	 * can be interrogated.
993 	 */
994 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
995 		return (EINVAL);
996 
997 	/*
998 	 * Get the objset name (the "special" mount argument).
999 	 */
1000 	if (error = pn_get(uap->spec, fromspace, &spn))
1001 		return (error);
1002 
1003 	osname = spn.pn_path;
1004 
1005 	/*
1006 	 * Check for mount privilege?
1007 	 *
1008 	 * If we don't have privilege then see if
1009 	 * we have local permission to allow it
1010 	 */
1011 	error = secpolicy_fs_mount(cr, mvp, vfsp);
1012 	if (error) {
1013 		error = dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr);
1014 		if (error == 0) {
1015 			vattr_t		vattr;
1016 
1017 			/*
1018 			 * Make sure user is the owner of the mount point
1019 			 * or has sufficient privileges.
1020 			 */
1021 
1022 			vattr.va_mask = AT_UID;
1023 
1024 			if (error = VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
1025 				goto out;
1026 			}
1027 
1028 			if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 &&
1029 			    VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) {
1030 				error = EPERM;
1031 				goto out;
1032 			}
1033 
1034 			secpolicy_fs_mount_clearopts(cr, vfsp);
1035 		} else {
1036 			goto out;
1037 		}
1038 	}
1039 
1040 	/*
1041 	 * Refuse to mount a filesystem if we are in a local zone and the
1042 	 * dataset is not visible.
1043 	 */
1044 	if (!INGLOBALZONE(curproc) &&
1045 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1046 		error = EPERM;
1047 		goto out;
1048 	}
1049 
1050 	/*
1051 	 * When doing a remount, we simply refresh our temporary properties
1052 	 * according to those options set in the current VFS options.
1053 	 */
1054 	if (uap->flags & MS_REMOUNT) {
1055 		/* refresh mount options */
1056 		zfs_unregister_callbacks(vfsp->vfs_data);
1057 		error = zfs_register_callbacks(vfsp);
1058 		goto out;
1059 	}
1060 
1061 	error = zfs_domount(vfsp, osname);
1062 
1063 out:
1064 	pn_free(&spn);
1065 	return (error);
1066 }
1067 
1068 static int
1069 zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1070 {
1071 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1072 	dev32_t d32;
1073 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
1074 
1075 	ZFS_ENTER(zfsvfs);
1076 
1077 	dmu_objset_space(zfsvfs->z_os,
1078 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
1079 
1080 	/*
1081 	 * The underlying storage pool actually uses multiple block sizes.
1082 	 * We report the fragsize as the smallest block size we support,
1083 	 * and we report our blocksize as the filesystem's maximum blocksize.
1084 	 */
1085 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1086 	statp->f_bsize = zfsvfs->z_max_blksz;
1087 
1088 	/*
1089 	 * The following report "total" blocks of various kinds in the
1090 	 * file system, but reported in terms of f_frsize - the
1091 	 * "fragment" size.
1092 	 */
1093 
1094 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
1095 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
1096 	statp->f_bavail = statp->f_bfree; /* no root reservation */
1097 
1098 	/*
1099 	 * statvfs() should really be called statufs(), because it assumes
1100 	 * static metadata.  ZFS doesn't preallocate files, so the best
1101 	 * we can do is report the max that could possibly fit in f_files,
1102 	 * and that minus the number actually used in f_ffree.
1103 	 * For f_ffree, report the smaller of the number of object available
1104 	 * and the number of blocks (each object will take at least a block).
1105 	 */
1106 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
1107 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
1108 	statp->f_files = statp->f_ffree + usedobjs;
1109 
1110 	(void) cmpldev(&d32, vfsp->vfs_dev);
1111 	statp->f_fsid = d32;
1112 
1113 	/*
1114 	 * We're a zfs filesystem.
1115 	 */
1116 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1117 
1118 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1119 
1120 	statp->f_namemax = ZFS_MAXNAMELEN;
1121 
1122 	/*
1123 	 * We have all of 32 characters to stuff a string here.
1124 	 * Is there anything useful we could/should provide?
1125 	 */
1126 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
1127 
1128 	ZFS_EXIT(zfsvfs);
1129 	return (0);
1130 }
1131 
1132 static int
1133 zfs_root(vfs_t *vfsp, vnode_t **vpp)
1134 {
1135 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1136 	znode_t *rootzp;
1137 	int error;
1138 
1139 	ZFS_ENTER(zfsvfs);
1140 
1141 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1142 	if (error == 0)
1143 		*vpp = ZTOV(rootzp);
1144 
1145 	ZFS_EXIT(zfsvfs);
1146 	return (error);
1147 }
1148 
1149 /*
1150  * Teardown the zfsvfs::z_os.
1151  *
1152  * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1153  * and 'z_teardown_inactive_lock' held.
1154  */
1155 static int
1156 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1157 {
1158 	znode_t	*zp;
1159 
1160 	rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1161 
1162 	if (!unmounting) {
1163 		/*
1164 		 * We purge the parent filesystem's vfsp as the parent
1165 		 * filesystem and all of its snapshots have their vnode's
1166 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
1167 		 * 'z_parent' is self referential for non-snapshots.
1168 		 */
1169 		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1170 	}
1171 
1172 	/*
1173 	 * Close the zil. NB: Can't close the zil while zfs_inactive
1174 	 * threads are blocked as zil_close can call zfs_inactive.
1175 	 */
1176 	if (zfsvfs->z_log) {
1177 		zil_close(zfsvfs->z_log);
1178 		zfsvfs->z_log = NULL;
1179 	}
1180 
1181 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
1182 
1183 	/*
1184 	 * If we are not unmounting (ie: online recv) and someone already
1185 	 * unmounted this file system while we were doing the switcheroo,
1186 	 * or a reopen of z_os failed then just bail out now.
1187 	 */
1188 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1189 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
1190 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1191 		return (EIO);
1192 	}
1193 
1194 	/*
1195 	 * At this point there are no vops active, and any new vops will
1196 	 * fail with EIO since we have z_teardown_lock for writer (only
1197 	 * relavent for forced unmount).
1198 	 *
1199 	 * Release all holds on dbufs.
1200 	 */
1201 	mutex_enter(&zfsvfs->z_znodes_lock);
1202 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
1203 	    zp = list_next(&zfsvfs->z_all_znodes, zp))
1204 		if (zp->z_dbuf) {
1205 			ASSERT(ZTOV(zp)->v_count > 0);
1206 			zfs_znode_dmu_fini(zp);
1207 		}
1208 	mutex_exit(&zfsvfs->z_znodes_lock);
1209 
1210 	/*
1211 	 * If we are unmounting, set the unmounted flag and let new vops
1212 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
1213 	 * other vops will fail with EIO.
1214 	 */
1215 	if (unmounting) {
1216 		zfsvfs->z_unmounted = B_TRUE;
1217 		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1218 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
1219 	}
1220 
1221 	/*
1222 	 * z_os will be NULL if there was an error in attempting to reopen
1223 	 * zfsvfs, so just return as the properties had already been
1224 	 * unregistered and cached data had been evicted before.
1225 	 */
1226 	if (zfsvfs->z_os == NULL)
1227 		return (0);
1228 
1229 	/*
1230 	 * Unregister properties.
1231 	 */
1232 	zfs_unregister_callbacks(zfsvfs);
1233 
1234 	/*
1235 	 * Evict cached data
1236 	 */
1237 	if (dmu_objset_evict_dbufs(zfsvfs->z_os)) {
1238 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1239 		(void) dmu_objset_evict_dbufs(zfsvfs->z_os);
1240 	}
1241 
1242 	return (0);
1243 }
1244 
1245 /*ARGSUSED*/
1246 static int
1247 zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1248 {
1249 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1250 	objset_t *os;
1251 	int ret;
1252 
1253 	ret = secpolicy_fs_unmount(cr, vfsp);
1254 	if (ret) {
1255 		ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
1256 		    ZFS_DELEG_PERM_MOUNT, cr);
1257 		if (ret)
1258 			return (ret);
1259 	}
1260 
1261 	/*
1262 	 * We purge the parent filesystem's vfsp as the parent filesystem
1263 	 * and all of its snapshots have their vnode's v_vfsp set to the
1264 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
1265 	 * referential for non-snapshots.
1266 	 */
1267 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1268 
1269 	/*
1270 	 * Unmount any snapshots mounted under .zfs before unmounting the
1271 	 * dataset itself.
1272 	 */
1273 	if (zfsvfs->z_ctldir != NULL &&
1274 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1275 		return (ret);
1276 	}
1277 
1278 	if (!(fflag & MS_FORCE)) {
1279 		/*
1280 		 * Check the number of active vnodes in the file system.
1281 		 * Our count is maintained in the vfs structure, but the
1282 		 * number is off by 1 to indicate a hold on the vfs
1283 		 * structure itself.
1284 		 *
1285 		 * The '.zfs' directory maintains a reference of its
1286 		 * own, and any active references underneath are
1287 		 * reflected in the vnode count.
1288 		 */
1289 		if (zfsvfs->z_ctldir == NULL) {
1290 			if (vfsp->vfs_count > 1)
1291 				return (EBUSY);
1292 		} else {
1293 			if (vfsp->vfs_count > 2 ||
1294 			    zfsvfs->z_ctldir->v_count > 1)
1295 				return (EBUSY);
1296 		}
1297 	}
1298 
1299 	vfsp->vfs_flag |= VFS_UNMOUNTED;
1300 
1301 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1302 	os = zfsvfs->z_os;
1303 
1304 	/*
1305 	 * z_os will be NULL if there was an error in
1306 	 * attempting to reopen zfsvfs.
1307 	 */
1308 	if (os != NULL) {
1309 		/*
1310 		 * Unset the objset user_ptr.
1311 		 */
1312 		mutex_enter(&os->os->os_user_ptr_lock);
1313 		dmu_objset_set_user(os, NULL);
1314 		mutex_exit(&os->os->os_user_ptr_lock);
1315 
1316 		/*
1317 		 * Finally release the objset
1318 		 */
1319 		dmu_objset_close(os);
1320 	}
1321 
1322 	/*
1323 	 * We can now safely destroy the '.zfs' directory node.
1324 	 */
1325 	if (zfsvfs->z_ctldir != NULL)
1326 		zfsctl_destroy(zfsvfs);
1327 
1328 	return (0);
1329 }
1330 
1331 static int
1332 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1333 {
1334 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1335 	znode_t		*zp;
1336 	uint64_t	object = 0;
1337 	uint64_t	fid_gen = 0;
1338 	uint64_t	gen_mask;
1339 	uint64_t	zp_gen;
1340 	int 		i, err;
1341 
1342 	*vpp = NULL;
1343 
1344 	ZFS_ENTER(zfsvfs);
1345 
1346 	if (fidp->fid_len == LONG_FID_LEN) {
1347 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1348 		uint64_t	objsetid = 0;
1349 		uint64_t	setgen = 0;
1350 
1351 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1352 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1353 
1354 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1355 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1356 
1357 		ZFS_EXIT(zfsvfs);
1358 
1359 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1360 		if (err)
1361 			return (EINVAL);
1362 		ZFS_ENTER(zfsvfs);
1363 	}
1364 
1365 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1366 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1367 
1368 		for (i = 0; i < sizeof (zfid->zf_object); i++)
1369 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1370 
1371 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1372 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1373 	} else {
1374 		ZFS_EXIT(zfsvfs);
1375 		return (EINVAL);
1376 	}
1377 
1378 	/* A zero fid_gen means we are in the .zfs control directories */
1379 	if (fid_gen == 0 &&
1380 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1381 		*vpp = zfsvfs->z_ctldir;
1382 		ASSERT(*vpp != NULL);
1383 		if (object == ZFSCTL_INO_SNAPDIR) {
1384 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1385 			    0, NULL, NULL, NULL, NULL, NULL) == 0);
1386 		} else {
1387 			VN_HOLD(*vpp);
1388 		}
1389 		ZFS_EXIT(zfsvfs);
1390 		return (0);
1391 	}
1392 
1393 	gen_mask = -1ULL >> (64 - 8 * i);
1394 
1395 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1396 	if (err = zfs_zget(zfsvfs, object, &zp)) {
1397 		ZFS_EXIT(zfsvfs);
1398 		return (err);
1399 	}
1400 	zp_gen = zp->z_phys->zp_gen & gen_mask;
1401 	if (zp_gen == 0)
1402 		zp_gen = 1;
1403 	if (zp->z_unlinked || zp_gen != fid_gen) {
1404 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1405 		VN_RELE(ZTOV(zp));
1406 		ZFS_EXIT(zfsvfs);
1407 		return (EINVAL);
1408 	}
1409 
1410 	*vpp = ZTOV(zp);
1411 	ZFS_EXIT(zfsvfs);
1412 	return (0);
1413 }
1414 
1415 /*
1416  * Block out VOPs and close zfsvfs_t::z_os
1417  *
1418  * Note, if successful, then we return with the 'z_teardown_lock' and
1419  * 'z_teardown_inactive_lock' write held.
1420  */
1421 int
1422 zfs_suspend_fs(zfsvfs_t *zfsvfs, char *name, int *mode)
1423 {
1424 	int error;
1425 
1426 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
1427 		return (error);
1428 
1429 	*mode = zfsvfs->z_os->os_mode;
1430 	dmu_objset_name(zfsvfs->z_os, name);
1431 	dmu_objset_close(zfsvfs->z_os);
1432 
1433 	return (0);
1434 }
1435 
1436 /*
1437  * Reopen zfsvfs_t::z_os and release VOPs.
1438  */
1439 int
1440 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname, int mode)
1441 {
1442 	int err;
1443 
1444 	ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
1445 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
1446 
1447 	err = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
1448 	if (err) {
1449 		zfsvfs->z_os = NULL;
1450 	} else {
1451 		znode_t *zp;
1452 
1453 		VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
1454 
1455 		/*
1456 		 * Attempt to re-establish all the active znodes with
1457 		 * their dbufs.  If a zfs_rezget() fails, then we'll let
1458 		 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
1459 		 * when they try to use their znode.
1460 		 */
1461 		mutex_enter(&zfsvfs->z_znodes_lock);
1462 		for (zp = list_head(&zfsvfs->z_all_znodes); zp;
1463 		    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1464 			(void) zfs_rezget(zp);
1465 		}
1466 		mutex_exit(&zfsvfs->z_znodes_lock);
1467 
1468 	}
1469 
1470 	/* release the VOPs */
1471 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
1472 	rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1473 
1474 	if (err) {
1475 		/*
1476 		 * Since we couldn't reopen zfsvfs::z_os, force
1477 		 * unmount this file system.
1478 		 */
1479 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
1480 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
1481 	}
1482 	return (err);
1483 }
1484 
1485 static void
1486 zfs_freevfs(vfs_t *vfsp)
1487 {
1488 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1489 	int i;
1490 
1491 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1492 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1493 
1494 	zfs_fuid_destroy(zfsvfs);
1495 	zfs_freezfsvfs(zfsvfs);
1496 
1497 	atomic_add_32(&zfs_active_fs_count, -1);
1498 }
1499 
1500 /*
1501  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
1502  * so we can't safely do any non-idempotent initialization here.
1503  * Leave that to zfs_init() and zfs_fini(), which are called
1504  * from the module's _init() and _fini() entry points.
1505  */
1506 /*ARGSUSED*/
1507 static int
1508 zfs_vfsinit(int fstype, char *name)
1509 {
1510 	int error;
1511 
1512 	zfsfstype = fstype;
1513 
1514 	/*
1515 	 * Setup vfsops and vnodeops tables.
1516 	 */
1517 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
1518 	if (error != 0) {
1519 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
1520 	}
1521 
1522 	error = zfs_create_op_tables();
1523 	if (error) {
1524 		zfs_remove_op_tables();
1525 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
1526 		(void) vfs_freevfsops_by_type(zfsfstype);
1527 		return (error);
1528 	}
1529 
1530 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
1531 
1532 	/*
1533 	 * Unique major number for all zfs mounts.
1534 	 * If we run out of 32-bit minors, we'll getudev() another major.
1535 	 */
1536 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
1537 	zfs_minor = ZFS_MIN_MINOR;
1538 
1539 	return (0);
1540 }
1541 
1542 void
1543 zfs_init(void)
1544 {
1545 	/*
1546 	 * Initialize .zfs directory structures
1547 	 */
1548 	zfsctl_init();
1549 
1550 	/*
1551 	 * Initialize znode cache, vnode ops, etc...
1552 	 */
1553 	zfs_znode_init();
1554 }
1555 
1556 void
1557 zfs_fini(void)
1558 {
1559 	zfsctl_fini();
1560 	zfs_znode_fini();
1561 }
1562 
1563 int
1564 zfs_busy(void)
1565 {
1566 	return (zfs_active_fs_count != 0);
1567 }
1568 
1569 int
1570 zfs_set_version(const char *name, uint64_t newvers)
1571 {
1572 	int error;
1573 	objset_t *os;
1574 	dmu_tx_t *tx;
1575 	uint64_t curvers;
1576 
1577 	/*
1578 	 * XXX for now, require that the filesystem be unmounted.  Would
1579 	 * be nice to find the zfsvfs_t and just update that if
1580 	 * possible.
1581 	 */
1582 
1583 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
1584 		return (EINVAL);
1585 
1586 	error = dmu_objset_open(name, DMU_OST_ZFS, DS_MODE_OWNER, &os);
1587 	if (error)
1588 		return (error);
1589 
1590 	error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1591 	    8, 1, &curvers);
1592 	if (error)
1593 		goto out;
1594 	if (newvers < curvers) {
1595 		error = EINVAL;
1596 		goto out;
1597 	}
1598 
1599 	tx = dmu_tx_create(os);
1600 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 0, ZPL_VERSION_STR);
1601 	error = dmu_tx_assign(tx, TXG_WAIT);
1602 	if (error) {
1603 		dmu_tx_abort(tx);
1604 		goto out;
1605 	}
1606 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1,
1607 	    &newvers, tx);
1608 
1609 	spa_history_internal_log(LOG_DS_UPGRADE,
1610 	    dmu_objset_spa(os), tx, CRED(),
1611 	    "oldver=%llu newver=%llu dataset = %llu", curvers, newvers,
1612 	    dmu_objset_id(os));
1613 	dmu_tx_commit(tx);
1614 
1615 out:
1616 	dmu_objset_close(os);
1617 	return (error);
1618 }
1619 
1620 /*
1621  * Read a property stored within the master node.
1622  */
1623 int
1624 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
1625 {
1626 	const char *pname;
1627 	int error = ENOENT;
1628 
1629 	/*
1630 	 * Look up the file system's value for the property.  For the
1631 	 * version property, we look up a slightly different string.
1632 	 */
1633 	if (prop == ZFS_PROP_VERSION)
1634 		pname = ZPL_VERSION_STR;
1635 	else
1636 		pname = zfs_prop_to_name(prop);
1637 
1638 	if (os != NULL)
1639 		error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
1640 
1641 	if (error == ENOENT) {
1642 		/* No value set, use the default value */
1643 		switch (prop) {
1644 		case ZFS_PROP_VERSION:
1645 			*value = ZPL_VERSION;
1646 			break;
1647 		case ZFS_PROP_NORMALIZE:
1648 		case ZFS_PROP_UTF8ONLY:
1649 			*value = 0;
1650 			break;
1651 		case ZFS_PROP_CASE:
1652 			*value = ZFS_CASE_SENSITIVE;
1653 			break;
1654 		default:
1655 			return (error);
1656 		}
1657 		error = 0;
1658 	}
1659 	return (error);
1660 }
1661 
1662 static vfsdef_t vfw = {
1663 	VFSDEF_VERSION,
1664 	MNTTYPE_ZFS,
1665 	zfs_vfsinit,
1666 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS|
1667 	    VSW_XID,
1668 	&zfs_mntopts
1669 };
1670 
1671 struct modlfs zfs_modlfs = {
1672 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
1673 };
1674