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