zfs_vfsops.c revision 196978
1235368Sgnn/*
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#include <sys/types.h>
27#include <sys/param.h>
28#include <sys/systm.h>
29#include <sys/kernel.h>
30#include <sys/sysmacros.h>
31#include <sys/kmem.h>
32#include <sys/acl.h>
33#include <sys/vnode.h>
34#include <sys/vfs.h>
35#include <sys/mntent.h>
36#include <sys/mount.h>
37#include <sys/cmn_err.h>
38#include <sys/zfs_znode.h>
39#include <sys/zfs_dir.h>
40#include <sys/zil.h>
41#include <sys/fs/zfs.h>
42#include <sys/dmu.h>
43#include <sys/dsl_prop.h>
44#include <sys/dsl_dataset.h>
45#include <sys/dsl_deleg.h>
46#include <sys/spa.h>
47#include <sys/zap.h>
48#include <sys/varargs.h>
49#include <sys/policy.h>
50#include <sys/atomic.h>
51#include <sys/zfs_ioctl.h>
52#include <sys/zfs_ctldir.h>
53#include <sys/zfs_fuid.h>
54#include <sys/sunddi.h>
55#include <sys/dnlc.h>
56#include <sys/dmu_objset.h>
57#include <sys/spa_boot.h>
58#include <sys/vdev_impl.h>	/* VDEV_BOOT_VERSION */
59
60struct mtx zfs_debug_mtx;
61MTX_SYSINIT(zfs_debug_mtx, &zfs_debug_mtx, "zfs_debug", MTX_DEF);
62
63SYSCTL_NODE(_vfs, OID_AUTO, zfs, CTLFLAG_RW, 0, "ZFS file system");
64
65int zfs_super_owner = 0;
66SYSCTL_INT(_vfs_zfs, OID_AUTO, super_owner, CTLFLAG_RW, &zfs_super_owner, 0,
67    "File system owner can perform privileged operation on his file systems");
68
69int zfs_debug_level = 0;
70TUNABLE_INT("vfs.zfs.debug", &zfs_debug_level);
71SYSCTL_INT(_vfs_zfs, OID_AUTO, debug, CTLFLAG_RW, &zfs_debug_level, 0,
72    "Debug level");
73
74SYSCTL_NODE(_vfs_zfs, OID_AUTO, version, CTLFLAG_RD, 0, "ZFS versions");
75static int zfs_version_acl = ZFS_ACL_VERSION;
76SYSCTL_INT(_vfs_zfs_version, OID_AUTO, acl, CTLFLAG_RD, &zfs_version_acl, 0,
77    "ZFS_ACL_VERSION");
78static int zfs_version_dmu_backup_header = DMU_BACKUP_HEADER_VERSION;
79SYSCTL_INT(_vfs_zfs_version, OID_AUTO, dmu_backup_header, CTLFLAG_RD,
80    &zfs_version_dmu_backup_header, 0, "DMU_BACKUP_HEADER_VERSION");
81static int zfs_version_dmu_backup_stream = DMU_BACKUP_STREAM_VERSION;
82SYSCTL_INT(_vfs_zfs_version, OID_AUTO, dmu_backup_stream, CTLFLAG_RD,
83    &zfs_version_dmu_backup_stream, 0, "DMU_BACKUP_STREAM_VERSION");
84static int zfs_version_spa = SPA_VERSION;
85SYSCTL_INT(_vfs_zfs_version, OID_AUTO, spa, CTLFLAG_RD, &zfs_version_spa, 0,
86    "SPA_VERSION");
87static int zfs_version_vdev_boot = VDEV_BOOT_VERSION;
88SYSCTL_INT(_vfs_zfs_version, OID_AUTO, vdev_boot, CTLFLAG_RD,
89    &zfs_version_vdev_boot, 0, "VDEV_BOOT_VERSION");
90static int zfs_version_zpl = ZPL_VERSION;
91SYSCTL_INT(_vfs_zfs_version, OID_AUTO, zpl, CTLFLAG_RD, &zfs_version_zpl, 0,
92    "ZPL_VERSION");
93
94static int zfs_mount(vfs_t *vfsp);
95static int zfs_umount(vfs_t *vfsp, int fflag);
96static int zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp);
97static int zfs_statfs(vfs_t *vfsp, struct statfs *statp);
98static int zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp);
99static int zfs_sync(vfs_t *vfsp, int waitfor);
100static int zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, vnode_t **vpp);
101static void zfs_objset_close(zfsvfs_t *zfsvfs);
102static void zfs_freevfs(vfs_t *vfsp);
103
104static struct vfsops zfs_vfsops = {
105	.vfs_mount =		zfs_mount,
106	.vfs_unmount =		zfs_umount,
107	.vfs_root =		zfs_root,
108	.vfs_statfs =		zfs_statfs,
109	.vfs_vget =		zfs_vget,
110	.vfs_sync =		zfs_sync,
111	.vfs_fhtovp =		zfs_fhtovp,
112};
113
114VFS_SET(zfs_vfsops, zfs, VFCF_JAIL | VFCF_DELEGADMIN);
115
116/*
117 * We need to keep a count of active fs's.
118 * This is necessary to prevent our module
119 * from being unloaded after a umount -f
120 */
121static uint32_t	zfs_active_fs_count = 0;
122
123/*ARGSUSED*/
124static int
125zfs_sync(vfs_t *vfsp, int waitfor)
126{
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	if (vfsp != NULL) {
136		/*
137		 * Sync a specific filesystem.
138		 */
139		zfsvfs_t *zfsvfs = vfsp->vfs_data;
140		int error;
141
142		error = vfs_stdsync(vfsp, waitfor);
143		if (error != 0)
144			return (error);
145
146		ZFS_ENTER(zfsvfs);
147		if (zfsvfs->z_log != NULL)
148			zil_commit(zfsvfs->z_log, UINT64_MAX, 0);
149		else
150			txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
151		ZFS_EXIT(zfsvfs);
152	} else {
153		/*
154		 * Sync all ZFS filesystems.  This is what happens when you
155		 * run sync(1M).  Unlike other filesystems, ZFS honors the
156		 * request by waiting for all pools to commit all dirty data.
157		 */
158		spa_sync_allpools();
159	}
160
161	return (0);
162}
163
164static void
165atime_changed_cb(void *arg, uint64_t newval)
166{
167	zfsvfs_t *zfsvfs = arg;
168
169	if (newval == TRUE) {
170		zfsvfs->z_atime = TRUE;
171		zfsvfs->z_vfs->vfs_flag &= ~MNT_NOATIME;
172		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
173		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
174	} else {
175		zfsvfs->z_atime = FALSE;
176		zfsvfs->z_vfs->vfs_flag |= MNT_NOATIME;
177		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
178		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
179	}
180}
181
182static void
183xattr_changed_cb(void *arg, uint64_t newval)
184{
185	zfsvfs_t *zfsvfs = arg;
186
187	if (newval == TRUE) {
188		/* XXX locking on vfs_flag? */
189#ifdef TODO
190		zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
191#endif
192		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
193		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
194	} else {
195		/* XXX locking on vfs_flag? */
196#ifdef TODO
197		zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
198#endif
199		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
200		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
201	}
202}
203
204static void
205blksz_changed_cb(void *arg, uint64_t newval)
206{
207	zfsvfs_t *zfsvfs = arg;
208
209	if (newval < SPA_MINBLOCKSIZE ||
210	    newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
211		newval = SPA_MAXBLOCKSIZE;
212
213	zfsvfs->z_max_blksz = newval;
214	zfsvfs->z_vfs->vfs_bsize = newval;
215}
216
217static void
218readonly_changed_cb(void *arg, uint64_t newval)
219{
220	zfsvfs_t *zfsvfs = arg;
221
222	if (newval) {
223		/* XXX locking on vfs_flag? */
224		zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
225		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
226		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
227	} else {
228		/* XXX locking on vfs_flag? */
229		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
230		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
231		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
232	}
233}
234
235static void
236setuid_changed_cb(void *arg, uint64_t newval)
237{
238	zfsvfs_t *zfsvfs = arg;
239
240	if (newval == FALSE) {
241		zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
242		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
243		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
244	} else {
245		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
246		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
247		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
248	}
249}
250
251static void
252exec_changed_cb(void *arg, uint64_t newval)
253{
254	zfsvfs_t *zfsvfs = arg;
255
256	if (newval == FALSE) {
257		zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
258		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
259		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
260	} else {
261		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
262		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
263		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
264	}
265}
266
267/*
268 * The nbmand mount option can be changed at mount time.
269 * We can't allow it to be toggled on live file systems or incorrect
270 * behavior may be seen from cifs clients
271 *
272 * This property isn't registered via dsl_prop_register(), but this callback
273 * will be called when a file system is first mounted
274 */
275static void
276nbmand_changed_cb(void *arg, uint64_t newval)
277{
278	zfsvfs_t *zfsvfs = arg;
279	if (newval == FALSE) {
280		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
281		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
282	} else {
283		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
284		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
285	}
286}
287
288static void
289snapdir_changed_cb(void *arg, uint64_t newval)
290{
291	zfsvfs_t *zfsvfs = arg;
292
293	zfsvfs->z_show_ctldir = newval;
294}
295
296static void
297vscan_changed_cb(void *arg, uint64_t newval)
298{
299	zfsvfs_t *zfsvfs = arg;
300
301	zfsvfs->z_vscan = newval;
302}
303
304static void
305acl_mode_changed_cb(void *arg, uint64_t newval)
306{
307	zfsvfs_t *zfsvfs = arg;
308
309	zfsvfs->z_acl_mode = newval;
310}
311
312static void
313acl_inherit_changed_cb(void *arg, uint64_t newval)
314{
315	zfsvfs_t *zfsvfs = arg;
316
317	zfsvfs->z_acl_inherit = newval;
318}
319
320static int
321zfs_register_callbacks(vfs_t *vfsp)
322{
323	struct dsl_dataset *ds = NULL;
324	objset_t *os = NULL;
325	zfsvfs_t *zfsvfs = NULL;
326	uint64_t nbmand;
327	int readonly, do_readonly = FALSE;
328	int setuid, do_setuid = FALSE;
329	int exec, do_exec = FALSE;
330	int xattr, do_xattr = FALSE;
331	int atime, do_atime = FALSE;
332	int error = 0;
333
334	ASSERT(vfsp);
335	zfsvfs = vfsp->vfs_data;
336	ASSERT(zfsvfs);
337	os = zfsvfs->z_os;
338
339	/*
340	 * This function can be called for a snapshot when we update snapshot's
341	 * mount point, which isn't really supported.
342	 */
343	if (dmu_objset_is_snapshot(os))
344		return (EOPNOTSUPP);
345
346	/*
347	 * The act of registering our callbacks will destroy any mount
348	 * options we may have.  In order to enable temporary overrides
349	 * of mount options, we stash away the current values and
350	 * restore them after we register the callbacks.
351	 */
352	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
353		readonly = B_TRUE;
354		do_readonly = B_TRUE;
355	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
356		readonly = B_FALSE;
357		do_readonly = B_TRUE;
358	}
359	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
360		setuid = B_FALSE;
361		do_setuid = B_TRUE;
362	} else {
363		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
364			setuid = B_FALSE;
365			do_setuid = B_TRUE;
366		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
367			setuid = B_TRUE;
368			do_setuid = B_TRUE;
369		}
370	}
371	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
372		exec = B_FALSE;
373		do_exec = B_TRUE;
374	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
375		exec = B_TRUE;
376		do_exec = B_TRUE;
377	}
378	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
379		xattr = B_FALSE;
380		do_xattr = B_TRUE;
381	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
382		xattr = B_TRUE;
383		do_xattr = B_TRUE;
384	}
385	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
386		atime = B_FALSE;
387		do_atime = B_TRUE;
388	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
389		atime = B_TRUE;
390		do_atime = B_TRUE;
391	}
392
393	/*
394	 * nbmand is a special property.  It can only be changed at
395	 * mount time.
396	 *
397	 * This is weird, but it is documented to only be changeable
398	 * at mount time.
399	 */
400	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
401		nbmand = B_FALSE;
402	} else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
403		nbmand = B_TRUE;
404	} else {
405		char osname[MAXNAMELEN];
406
407		dmu_objset_name(os, osname);
408		if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
409		    NULL)) {
410			return (error);
411		}
412	}
413
414	/*
415	 * Register property callbacks.
416	 *
417	 * It would probably be fine to just check for i/o error from
418	 * the first prop_register(), but I guess I like to go
419	 * overboard...
420	 */
421	ds = dmu_objset_ds(os);
422	error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
423	error = error ? error : dsl_prop_register(ds,
424	    "xattr", xattr_changed_cb, zfsvfs);
425	error = error ? error : dsl_prop_register(ds,
426	    "recordsize", blksz_changed_cb, zfsvfs);
427	error = error ? error : dsl_prop_register(ds,
428	    "readonly", readonly_changed_cb, zfsvfs);
429	error = error ? error : dsl_prop_register(ds,
430	    "setuid", setuid_changed_cb, zfsvfs);
431	error = error ? error : dsl_prop_register(ds,
432	    "exec", exec_changed_cb, zfsvfs);
433	error = error ? error : dsl_prop_register(ds,
434	    "snapdir", snapdir_changed_cb, zfsvfs);
435	error = error ? error : dsl_prop_register(ds,
436	    "aclmode", acl_mode_changed_cb, zfsvfs);
437	error = error ? error : dsl_prop_register(ds,
438	    "aclinherit", acl_inherit_changed_cb, zfsvfs);
439	error = error ? error : dsl_prop_register(ds,
440	    "vscan", vscan_changed_cb, zfsvfs);
441	if (error)
442		goto unregister;
443
444	/*
445	 * Invoke our callbacks to restore temporary mount options.
446	 */
447	if (do_readonly)
448		readonly_changed_cb(zfsvfs, readonly);
449	if (do_setuid)
450		setuid_changed_cb(zfsvfs, setuid);
451	if (do_exec)
452		exec_changed_cb(zfsvfs, exec);
453	if (do_xattr)
454		xattr_changed_cb(zfsvfs, xattr);
455	if (do_atime)
456		atime_changed_cb(zfsvfs, atime);
457
458	nbmand_changed_cb(zfsvfs, nbmand);
459
460	return (0);
461
462unregister:
463	/*
464	 * We may attempt to unregister some callbacks that are not
465	 * registered, but this is OK; it will simply return ENOMSG,
466	 * which we will ignore.
467	 */
468	(void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
469	(void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
470	(void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
471	(void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
472	(void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
473	(void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
474	(void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
475	(void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
476	(void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
477	    zfsvfs);
478	(void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
479	return (error);
480
481}
482
483static int
484zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
485{
486	int error;
487
488	error = zfs_register_callbacks(zfsvfs->z_vfs);
489	if (error)
490		return (error);
491
492	/*
493	 * Set the objset user_ptr to track its zfsvfs.
494	 */
495	mutex_enter(&zfsvfs->z_os->os->os_user_ptr_lock);
496	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
497	mutex_exit(&zfsvfs->z_os->os->os_user_ptr_lock);
498
499	/*
500	 * If we are not mounting (ie: online recv), then we don't
501	 * have to worry about replaying the log as we blocked all
502	 * operations out since we closed the ZIL.
503	 */
504	if (mounting) {
505		boolean_t readonly;
506
507		/*
508		 * During replay we remove the read only flag to
509		 * allow replays to succeed.
510		 */
511		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
512		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
513
514		/*
515		 * Parse and replay the intent log.
516		 */
517		zil_replay(zfsvfs->z_os, zfsvfs, &zfsvfs->z_assign,
518		    zfs_replay_vector, zfs_unlinked_drain);
519
520		zfs_unlinked_drain(zfsvfs);
521		zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
522	}
523
524	if (!zil_disable)
525		zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
526
527	return (0);
528}
529
530static void
531zfs_freezfsvfs(zfsvfs_t *zfsvfs)
532{
533	mutex_destroy(&zfsvfs->z_znodes_lock);
534	mutex_destroy(&zfsvfs->z_online_recv_lock);
535	list_destroy(&zfsvfs->z_all_znodes);
536	rrw_destroy(&zfsvfs->z_teardown_lock);
537	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
538	rw_destroy(&zfsvfs->z_fuid_lock);
539	kmem_free(zfsvfs, sizeof (zfsvfs_t));
540}
541
542static int
543zfs_domount(vfs_t *vfsp, char *osname)
544{
545	uint64_t recordsize, readonly;
546	int error = 0;
547	int mode;
548	zfsvfs_t *zfsvfs;
549	znode_t *zp = NULL;
550
551	ASSERT(vfsp);
552	ASSERT(osname);
553
554	/*
555	 * Initialize the zfs-specific filesystem structure.
556	 * Should probably make this a kmem cache, shuffle fields,
557	 * and just bzero up to z_hold_mtx[].
558	 */
559	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
560	zfsvfs->z_vfs = vfsp;
561	zfsvfs->z_parent = zfsvfs;
562	zfsvfs->z_assign = TXG_NOWAIT;
563	zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
564	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
565
566	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
567	mutex_init(&zfsvfs->z_online_recv_lock, NULL, MUTEX_DEFAULT, NULL);
568	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
569	    offsetof(znode_t, z_link_node));
570	rrw_init(&zfsvfs->z_teardown_lock);
571	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
572	rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
573
574	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
575	    NULL))
576		goto out;
577	zfsvfs->z_vfs->vfs_bsize = recordsize;
578
579	vfsp->vfs_data = zfsvfs;
580	vfsp->mnt_flag |= MNT_LOCAL;
581	vfsp->mnt_kern_flag |= MNTK_MPSAFE;
582	vfsp->mnt_kern_flag |= MNTK_LOOKUP_SHARED;
583	vfsp->mnt_kern_flag |= MNTK_SHARED_WRITES;
584
585	if (error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL))
586		goto out;
587
588	mode = DS_MODE_OWNER;
589	if (readonly)
590		mode |= DS_MODE_READONLY;
591
592	error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
593	if (error == EROFS) {
594		mode = DS_MODE_OWNER | DS_MODE_READONLY;
595		error = dmu_objset_open(osname, DMU_OST_ZFS, mode,
596		    &zfsvfs->z_os);
597	}
598
599	if (error)
600		goto out;
601
602	if (error = zfs_init_fs(zfsvfs, &zp))
603		goto out;
604
605	/*
606	 * Set features for file system.
607	 */
608	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
609	if (zfsvfs->z_use_fuids) {
610		vfs_set_feature(vfsp, VFSFT_XVATTR);
611		vfs_set_feature(vfsp, VFSFT_SYSATTR_VIEWS);
612		vfs_set_feature(vfsp, VFSFT_ACEMASKONACCESS);
613		vfs_set_feature(vfsp, VFSFT_ACLONCREATE);
614	}
615	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
616		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
617		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
618		vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
619	} else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
620		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
621		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
622	}
623
624	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
625		uint64_t pval;
626
627		ASSERT(mode & DS_MODE_READONLY);
628		atime_changed_cb(zfsvfs, B_FALSE);
629		readonly_changed_cb(zfsvfs, B_TRUE);
630		if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
631			goto out;
632		xattr_changed_cb(zfsvfs, pval);
633		zfsvfs->z_issnap = B_TRUE;
634	} else {
635		error = zfsvfs_setup(zfsvfs, B_TRUE);
636	}
637
638	vfs_mountedfrom(vfsp, osname);
639
640	if (!zfsvfs->z_issnap)
641		zfsctl_create(zfsvfs);
642out:
643	if (error) {
644		if (zfsvfs->z_os)
645			dmu_objset_close(zfsvfs->z_os);
646		zfs_freezfsvfs(zfsvfs);
647	} else {
648		atomic_add_32(&zfs_active_fs_count, 1);
649	}
650
651	return (error);
652}
653
654void
655zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
656{
657	objset_t *os = zfsvfs->z_os;
658	struct dsl_dataset *ds;
659
660	/*
661	 * Unregister properties.
662	 */
663	if (!dmu_objset_is_snapshot(os)) {
664		ds = dmu_objset_ds(os);
665		VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
666		    zfsvfs) == 0);
667
668		VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
669		    zfsvfs) == 0);
670
671		VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
672		    zfsvfs) == 0);
673
674		VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
675		    zfsvfs) == 0);
676
677		VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
678		    zfsvfs) == 0);
679
680		VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
681		    zfsvfs) == 0);
682
683		VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
684		    zfsvfs) == 0);
685
686		VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
687		    zfsvfs) == 0);
688
689		VERIFY(dsl_prop_unregister(ds, "aclinherit",
690		    acl_inherit_changed_cb, zfsvfs) == 0);
691
692		VERIFY(dsl_prop_unregister(ds, "vscan",
693		    vscan_changed_cb, zfsvfs) == 0);
694	}
695}
696
697/*ARGSUSED*/
698static int
699zfs_mount(vfs_t *vfsp)
700{
701	kthread_t	*td = curthread;
702	vnode_t		*mvp = vfsp->mnt_vnodecovered;
703	cred_t		*cr = td->td_ucred;
704	char		*osname;
705	int		error = 0;
706	int		canwrite;
707
708	if (vfs_getopt(vfsp->mnt_optnew, "from", (void **)&osname, NULL))
709		return (EINVAL);
710
711	/*
712	 * If full-owner-access is enabled and delegated administration is
713	 * turned on, we must set nosuid.
714	 */
715	if (zfs_super_owner &&
716	    dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) != ECANCELED) {
717		secpolicy_fs_mount_clearopts(cr, vfsp);
718	}
719
720	/*
721	 * Check for mount privilege?
722	 *
723	 * If we don't have privilege then see if
724	 * we have local permission to allow it
725	 */
726	error = secpolicy_fs_mount(cr, mvp, vfsp);
727	if (error) {
728		error = dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr);
729		if (error != 0)
730			goto out;
731
732		if (!(vfsp->vfs_flag & MS_REMOUNT)) {
733			vattr_t		vattr;
734
735			/*
736			 * Make sure user is the owner of the mount point
737			 * or has sufficient privileges.
738			 */
739
740			vattr.va_mask = AT_UID;
741
742			vn_lock(mvp, LK_SHARED | LK_RETRY);
743			if (error = VOP_GETATTR(mvp, &vattr, cr)) {
744				VOP_UNLOCK(mvp, 0);
745				goto out;
746			}
747
748#if 0 /* CHECK THIS! Is probably needed for zfs_suser. */
749			if (secpolicy_vnode_owner(mvp, cr, vattr.va_uid) != 0 &&
750			    VOP_ACCESS(mvp, VWRITE, cr, td) != 0) {
751				error = EPERM;
752				goto out;
753			}
754#else
755			if (error = secpolicy_vnode_owner(mvp, cr, vattr.va_uid)) {
756				VOP_UNLOCK(mvp, 0);
757				goto out;
758			}
759
760			if (error = VOP_ACCESS(mvp, VWRITE, cr, td)) {
761				VOP_UNLOCK(mvp, 0);
762				goto out;
763			}
764			VOP_UNLOCK(mvp, 0);
765#endif
766		}
767
768		secpolicy_fs_mount_clearopts(cr, vfsp);
769	}
770
771	/*
772	 * Refuse to mount a filesystem if we are in a local zone and the
773	 * dataset is not visible.
774	 */
775	if (!INGLOBALZONE(curthread) &&
776	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
777		error = EPERM;
778		goto out;
779	}
780
781	/*
782	 * When doing a remount, we simply refresh our temporary properties
783	 * according to those options set in the current VFS options.
784	 */
785	if (vfsp->vfs_flag & MS_REMOUNT) {
786		/* refresh mount options */
787		zfs_unregister_callbacks(vfsp->vfs_data);
788		error = zfs_register_callbacks(vfsp);
789		goto out;
790	}
791
792	DROP_GIANT();
793	error = zfs_domount(vfsp, osname);
794	PICKUP_GIANT();
795out:
796	return (error);
797}
798
799static int
800zfs_statfs(vfs_t *vfsp, struct statfs *statp)
801{
802	zfsvfs_t *zfsvfs = vfsp->vfs_data;
803	uint64_t refdbytes, availbytes, usedobjs, availobjs;
804
805	statp->f_version = STATFS_VERSION;
806
807	ZFS_ENTER(zfsvfs);
808
809	dmu_objset_space(zfsvfs->z_os,
810	    &refdbytes, &availbytes, &usedobjs, &availobjs);
811
812	/*
813	 * The underlying storage pool actually uses multiple block sizes.
814	 * We report the fragsize as the smallest block size we support,
815	 * and we report our blocksize as the filesystem's maximum blocksize.
816	 */
817	statp->f_bsize = zfsvfs->z_vfs->vfs_bsize;
818	statp->f_iosize = zfsvfs->z_vfs->vfs_bsize;
819
820	/*
821	 * The following report "total" blocks of various kinds in the
822	 * file system, but reported in terms of f_frsize - the
823	 * "fragment" size.
824	 */
825
826	statp->f_blocks = (refdbytes + availbytes) / statp->f_bsize;
827	statp->f_bfree = availbytes / statp->f_bsize;
828	statp->f_bavail = statp->f_bfree; /* no root reservation */
829
830	/*
831	 * statvfs() should really be called statufs(), because it assumes
832	 * static metadata.  ZFS doesn't preallocate files, so the best
833	 * we can do is report the max that could possibly fit in f_files,
834	 * and that minus the number actually used in f_ffree.
835	 * For f_ffree, report the smaller of the number of object available
836	 * and the number of blocks (each object will take at least a block).
837	 */
838	statp->f_ffree = MIN(availobjs, statp->f_bfree);
839	statp->f_files = statp->f_ffree + usedobjs;
840
841	/*
842	 * We're a zfs filesystem.
843	 */
844	(void) strlcpy(statp->f_fstypename, "zfs", sizeof(statp->f_fstypename));
845
846	strlcpy(statp->f_mntfromname, vfsp->mnt_stat.f_mntfromname,
847	    sizeof(statp->f_mntfromname));
848	strlcpy(statp->f_mntonname, vfsp->mnt_stat.f_mntonname,
849	    sizeof(statp->f_mntonname));
850
851	statp->f_namemax = ZFS_MAXNAMELEN;
852
853	ZFS_EXIT(zfsvfs);
854	return (0);
855}
856
857static int
858zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp)
859{
860	zfsvfs_t *zfsvfs = vfsp->vfs_data;
861	znode_t *rootzp;
862	int error;
863
864	ZFS_ENTER(zfsvfs);
865
866	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
867	if (error == 0) {
868		*vpp = ZTOV(rootzp);
869		error = vn_lock(*vpp, flags);
870		(*vpp)->v_vflag |= VV_ROOT;
871	}
872
873	ZFS_EXIT(zfsvfs);
874	return (error);
875}
876
877/*
878 * Teardown the zfsvfs::z_os.
879 *
880 * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
881 * and 'z_teardown_inactive_lock' held.
882 */
883static int
884zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
885{
886	znode_t	*zp;
887
888	rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
889
890	if (!unmounting) {
891		/*
892		 * We purge the parent filesystem's vfsp as the parent
893		 * filesystem and all of its snapshots have their vnode's
894		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
895		 * 'z_parent' is self referential for non-snapshots.
896		 */
897		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
898	}
899
900	/*
901	 * Close the zil. NB: Can't close the zil while zfs_inactive
902	 * threads are blocked as zil_close can call zfs_inactive.
903	 */
904	if (zfsvfs->z_log) {
905		zil_close(zfsvfs->z_log);
906		zfsvfs->z_log = NULL;
907	}
908
909	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
910
911	/*
912	 * If we are not unmounting (ie: online recv) and someone already
913	 * unmounted this file system while we were doing the switcheroo,
914	 * or a reopen of z_os failed then just bail out now.
915	 */
916	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
917		rw_exit(&zfsvfs->z_teardown_inactive_lock);
918		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
919		return (EIO);
920	}
921
922	/*
923	 * At this point there are no vops active, and any new vops will
924	 * fail with EIO since we have z_teardown_lock for writer (only
925	 * relavent for forced unmount).
926	 *
927	 * Release all holds on dbufs.
928	 */
929	mutex_enter(&zfsvfs->z_znodes_lock);
930	for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
931	    zp = list_next(&zfsvfs->z_all_znodes, zp))
932		if (zp->z_dbuf) {
933			ASSERT(ZTOV(zp)->v_count >= 0);
934			zfs_znode_dmu_fini(zp);
935		}
936	mutex_exit(&zfsvfs->z_znodes_lock);
937
938	/*
939	 * If we are unmounting, set the unmounted flag and let new vops
940	 * unblock.  zfs_inactive will have the unmounted behavior, and all
941	 * other vops will fail with EIO.
942	 */
943	if (unmounting) {
944		zfsvfs->z_unmounted = B_TRUE;
945		rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
946		rw_exit(&zfsvfs->z_teardown_inactive_lock);
947	}
948
949	/*
950	 * z_os will be NULL if there was an error in attempting to reopen
951	 * zfsvfs, so just return as the properties had already been
952	 * unregistered and cached data had been evicted before.
953	 */
954	if (zfsvfs->z_os == NULL)
955		return (0);
956
957	/*
958	 * Unregister properties.
959	 */
960	zfs_unregister_callbacks(zfsvfs);
961
962	/*
963	 * Evict cached data
964	 */
965	if (dmu_objset_evict_dbufs(zfsvfs->z_os)) {
966		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
967		(void) dmu_objset_evict_dbufs(zfsvfs->z_os);
968	}
969
970	return (0);
971}
972
973/*ARGSUSED*/
974static int
975zfs_umount(vfs_t *vfsp, int fflag)
976{
977	zfsvfs_t *zfsvfs = vfsp->vfs_data;
978	objset_t *os;
979	cred_t *cr = curthread->td_ucred;
980	int ret;
981
982	if (fflag & MS_FORCE) {
983		/* TODO: Force unmount is not well implemented yet, so deny it. */
984		ZFS_LOG(0, "Force unmount is experimental - report any problems.");
985	}
986
987	ret = secpolicy_fs_unmount(cr, vfsp);
988	if (ret) {
989		ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
990		    ZFS_DELEG_PERM_MOUNT, cr);
991		if (ret)
992			return (ret);
993	}
994	/*
995	 * We purge the parent filesystem's vfsp as the parent filesystem
996	 * and all of its snapshots have their vnode's v_vfsp set to the
997	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
998	 * referential for non-snapshots.
999	 */
1000	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1001
1002	/*
1003	 * Unmount any snapshots mounted under .zfs before unmounting the
1004	 * dataset itself.
1005	 */
1006	if (zfsvfs->z_ctldir != NULL) {
1007		if ((ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0)
1008			return (ret);
1009		ret = vflush(vfsp, 0, 0, curthread);
1010		ASSERT(ret == EBUSY);
1011		if (!(fflag & MS_FORCE)) {
1012			if (zfsvfs->z_ctldir->v_count > 1)
1013				return (EBUSY);
1014			ASSERT(zfsvfs->z_ctldir->v_count == 1);
1015		}
1016		zfsctl_destroy(zfsvfs);
1017		ASSERT(zfsvfs->z_ctldir == NULL);
1018	}
1019
1020	/*
1021	 * Flush all the files.
1022	 */
1023	ret = vflush(vfsp, 1, (fflag & MS_FORCE) ? FORCECLOSE : 0, curthread);
1024	if (ret != 0) {
1025		if (!zfsvfs->z_issnap) {
1026			zfsctl_create(zfsvfs);
1027			ASSERT(zfsvfs->z_ctldir != NULL);
1028		}
1029		return (ret);
1030	}
1031
1032	if (!(fflag & MS_FORCE)) {
1033		/*
1034		 * Check the number of active vnodes in the file system.
1035		 * Our count is maintained in the vfs structure, but the
1036		 * number is off by 1 to indicate a hold on the vfs
1037		 * structure itself.
1038		 *
1039		 * The '.zfs' directory maintains a reference of its
1040		 * own, and any active references underneath are
1041		 * reflected in the vnode count.
1042		 */
1043		if (zfsvfs->z_ctldir == NULL) {
1044			if (vfsp->vfs_count > 1)
1045				return (EBUSY);
1046		} else {
1047			if (vfsp->vfs_count > 2 ||
1048			    zfsvfs->z_ctldir->v_count > 1)
1049				return (EBUSY);
1050		}
1051	} else {
1052		MNT_ILOCK(vfsp);
1053		vfsp->mnt_kern_flag |= MNTK_UNMOUNTF;
1054		MNT_IUNLOCK(vfsp);
1055	}
1056
1057	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1058	os = zfsvfs->z_os;
1059
1060	/*
1061	 * z_os will be NULL if there was an error in
1062	 * attempting to reopen zfsvfs.
1063	 */
1064	if (os != NULL) {
1065		/*
1066		 * Unset the objset user_ptr.
1067		 */
1068		mutex_enter(&os->os->os_user_ptr_lock);
1069		dmu_objset_set_user(os, NULL);
1070		mutex_exit(&os->os->os_user_ptr_lock);
1071
1072		/*
1073		 * Finally release the objset
1074		 */
1075		dmu_objset_close(os);
1076	}
1077
1078	/*
1079	 * We can now safely destroy the '.zfs' directory node.
1080	 */
1081	if (zfsvfs->z_ctldir != NULL)
1082		zfsctl_destroy(zfsvfs);
1083	if (zfsvfs->z_issnap) {
1084		vnode_t *svp = vfsp->mnt_vnodecovered;
1085
1086		ASSERT(svp->v_count == 2 || svp->v_count == 1);
1087		if (svp->v_count == 2)
1088			VN_RELE(svp);
1089	}
1090	zfs_freevfs(vfsp);
1091
1092	return (0);
1093}
1094
1095static int
1096zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp)
1097{
1098	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1099	znode_t		*zp;
1100	int 		err;
1101
1102	ZFS_ENTER(zfsvfs);
1103	err = zfs_zget(zfsvfs, ino, &zp);
1104	if (err == 0 && zp->z_unlinked) {
1105		VN_RELE(ZTOV(zp));
1106		err = EINVAL;
1107	}
1108	if (err != 0)
1109		*vpp = NULL;
1110	else {
1111		*vpp = ZTOV(zp);
1112		vn_lock(*vpp, flags);
1113	}
1114	ZFS_EXIT(zfsvfs);
1115	return (err);
1116}
1117
1118static int
1119zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, vnode_t **vpp)
1120{
1121	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
1122	znode_t		*zp;
1123	uint64_t	object = 0;
1124	uint64_t	fid_gen = 0;
1125	uint64_t	gen_mask;
1126	uint64_t	zp_gen;
1127	int		i, err;
1128
1129	*vpp = NULL;
1130
1131	ZFS_ENTER(zfsvfs);
1132
1133	if (fidp->fid_len == LONG_FID_LEN) {
1134		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
1135		uint64_t	objsetid = 0;
1136		uint64_t	setgen = 0;
1137
1138		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1139			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1140
1141		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1142			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1143
1144		ZFS_EXIT(zfsvfs);
1145
1146		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1147		if (err)
1148			return (EINVAL);
1149		ZFS_ENTER(zfsvfs);
1150	}
1151
1152	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1153		zfid_short_t	*zfid = (zfid_short_t *)fidp;
1154
1155		for (i = 0; i < sizeof (zfid->zf_object); i++)
1156			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1157
1158		for (i = 0; i < sizeof (zfid->zf_gen); i++)
1159			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1160	} else {
1161		ZFS_EXIT(zfsvfs);
1162		return (EINVAL);
1163	}
1164
1165	/* A zero fid_gen means we are in the .zfs control directories */
1166	if (fid_gen == 0 &&
1167	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1168		*vpp = zfsvfs->z_ctldir;
1169		ASSERT(*vpp != NULL);
1170		if (object == ZFSCTL_INO_SNAPDIR) {
1171			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1172			    0, NULL, NULL, NULL, NULL, NULL) == 0);
1173		} else {
1174			VN_HOLD(*vpp);
1175		}
1176		vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1177		ZFS_EXIT(zfsvfs);
1178		return (0);
1179	}
1180
1181	gen_mask = -1ULL >> (64 - 8 * i);
1182
1183	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1184	if (err = zfs_zget(zfsvfs, object, &zp)) {
1185		ZFS_EXIT(zfsvfs);
1186		return (err);
1187	}
1188	zp_gen = zp->z_phys->zp_gen & gen_mask;
1189	if (zp_gen == 0)
1190		zp_gen = 1;
1191	if (zp->z_unlinked || zp_gen != fid_gen) {
1192		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1193		VN_RELE(ZTOV(zp));
1194		ZFS_EXIT(zfsvfs);
1195		return (EINVAL);
1196	}
1197
1198	*vpp = ZTOV(zp);
1199	vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1200	vnode_create_vobject(*vpp, zp->z_phys->zp_size, curthread);
1201	ZFS_EXIT(zfsvfs);
1202	return (0);
1203}
1204
1205/*
1206 * Block out VOPs and close zfsvfs_t::z_os
1207 *
1208 * Note, if successful, then we return with the 'z_teardown_lock' and
1209 * 'z_teardown_inactive_lock' write held.
1210 */
1211int
1212zfs_suspend_fs(zfsvfs_t *zfsvfs, char *name, int *mode)
1213{
1214	int error;
1215
1216	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
1217		return (error);
1218
1219	*mode = zfsvfs->z_os->os_mode;
1220	dmu_objset_name(zfsvfs->z_os, name);
1221	dmu_objset_close(zfsvfs->z_os);
1222
1223	return (0);
1224}
1225
1226/*
1227 * Reopen zfsvfs_t::z_os and release VOPs.
1228 */
1229int
1230zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname, int mode)
1231{
1232	int err;
1233
1234	ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
1235	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
1236
1237	err = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
1238	if (err) {
1239		zfsvfs->z_os = NULL;
1240	} else {
1241		znode_t *zp;
1242
1243		VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
1244
1245		/*
1246		 * Attempt to re-establish all the active znodes with
1247		 * their dbufs.  If a zfs_rezget() fails, then we'll let
1248		 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
1249		 * when they try to use their znode.
1250		 */
1251		mutex_enter(&zfsvfs->z_znodes_lock);
1252		for (zp = list_head(&zfsvfs->z_all_znodes); zp;
1253		    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1254			(void) zfs_rezget(zp);
1255		}
1256		mutex_exit(&zfsvfs->z_znodes_lock);
1257
1258	}
1259
1260	/* release the VOPs */
1261	rw_exit(&zfsvfs->z_teardown_inactive_lock);
1262	rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1263
1264	if (err) {
1265		/*
1266		 * Since we couldn't reopen zfsvfs::z_os, force
1267		 * unmount this file system.
1268		 */
1269		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
1270			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, curthread);
1271	}
1272	return (err);
1273}
1274
1275static void
1276zfs_freevfs(vfs_t *vfsp)
1277{
1278	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1279	int i;
1280
1281	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1282		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1283
1284	zfs_fuid_destroy(zfsvfs);
1285	zfs_freezfsvfs(zfsvfs);
1286
1287	atomic_add_32(&zfs_active_fs_count, -1);
1288}
1289
1290#ifdef __i386__
1291static int desiredvnodes_backup;
1292#endif
1293
1294static void
1295zfs_vnodes_adjust(void)
1296{
1297#ifdef __i386__
1298	int newdesiredvnodes;
1299
1300	desiredvnodes_backup = desiredvnodes;
1301
1302	/*
1303	 * We calculate newdesiredvnodes the same way it is done in
1304	 * vntblinit(). If it is equal to desiredvnodes, it means that
1305	 * it wasn't tuned by the administrator and we can tune it down.
1306	 */
1307	newdesiredvnodes = min(maxproc + cnt.v_page_count / 4, 2 *
1308	    vm_kmem_size / (5 * (sizeof(struct vm_object) +
1309	    sizeof(struct vnode))));
1310	if (newdesiredvnodes == desiredvnodes)
1311		desiredvnodes = (3 * newdesiredvnodes) / 4;
1312#endif
1313}
1314
1315static void
1316zfs_vnodes_adjust_back(void)
1317{
1318
1319#ifdef __i386__
1320	desiredvnodes = desiredvnodes_backup;
1321#endif
1322}
1323
1324void
1325zfs_init(void)
1326{
1327
1328	printf("ZFS filesystem version " SPA_VERSION_STRING "\n");
1329
1330	/*
1331	 * Initialize znode cache, vnode ops, etc...
1332	 */
1333	zfs_znode_init();
1334
1335	/*
1336	 * Initialize .zfs directory structures
1337	 */
1338	zfsctl_init();
1339
1340	/*
1341	 * Reduce number of vnode. Originally number of vnodes is calculated
1342	 * with UFS inode in mind. We reduce it here, because it's too big for
1343	 * ZFS/i386.
1344	 */
1345	zfs_vnodes_adjust();
1346}
1347
1348void
1349zfs_fini(void)
1350{
1351	zfsctl_fini();
1352	zfs_znode_fini();
1353	zfs_vnodes_adjust_back();
1354}
1355
1356int
1357zfs_busy(void)
1358{
1359	return (zfs_active_fs_count != 0);
1360}
1361
1362int
1363zfs_set_version(const char *name, uint64_t newvers)
1364{
1365	int error;
1366	objset_t *os;
1367	dmu_tx_t *tx;
1368	uint64_t curvers;
1369
1370	/*
1371	 * XXX for now, require that the filesystem be unmounted.  Would
1372	 * be nice to find the zfsvfs_t and just update that if
1373	 * possible.
1374	 */
1375
1376	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
1377		return (EINVAL);
1378
1379	error = dmu_objset_open(name, DMU_OST_ZFS, DS_MODE_OWNER, &os);
1380	if (error)
1381		return (error);
1382
1383	error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1384	    8, 1, &curvers);
1385	if (error)
1386		goto out;
1387	if (newvers < curvers) {
1388		error = EINVAL;
1389		goto out;
1390	}
1391
1392	tx = dmu_tx_create(os);
1393	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 0, ZPL_VERSION_STR);
1394	error = dmu_tx_assign(tx, TXG_WAIT);
1395	if (error) {
1396		dmu_tx_abort(tx);
1397		goto out;
1398	}
1399	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1,
1400	    &newvers, tx);
1401
1402	spa_history_internal_log(LOG_DS_UPGRADE,
1403	    dmu_objset_spa(os), tx, CRED(),
1404	    "oldver=%llu newver=%llu dataset = %llu", curvers, newvers,
1405	    dmu_objset_id(os));
1406	dmu_tx_commit(tx);
1407
1408out:
1409	dmu_objset_close(os);
1410	return (error);
1411}
1412/*
1413 * Read a property stored within the master node.
1414 */
1415int
1416zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
1417{
1418	const char *pname;
1419	int error = ENOENT;
1420
1421	/*
1422	 * Look up the file system's value for the property.  For the
1423	 * version property, we look up a slightly different string.
1424	 */
1425	if (prop == ZFS_PROP_VERSION)
1426		pname = ZPL_VERSION_STR;
1427	else
1428		pname = zfs_prop_to_name(prop);
1429
1430	if (os != NULL)
1431		error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
1432
1433	if (error == ENOENT) {
1434		/* No value set, use the default value */
1435		switch (prop) {
1436		case ZFS_PROP_VERSION:
1437			*value = ZPL_VERSION;
1438			break;
1439		case ZFS_PROP_NORMALIZE:
1440		case ZFS_PROP_UTF8ONLY:
1441			*value = 0;
1442			break;
1443		case ZFS_PROP_CASE:
1444			*value = ZFS_CASE_SENSITIVE;
1445			break;
1446		default:
1447			return (error);
1448		}
1449		error = 0;
1450	}
1451	return (error);
1452}
1453