xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision e8031f0a)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/sysmacros.h>
33 #include <sys/kmem.h>
34 #include <sys/pathname.h>
35 #include <sys/acl.h>
36 #include <sys/vnode.h>
37 #include <sys/vfs.h>
38 #include <sys/mntent.h>
39 #include <sys/mount.h>
40 #include <sys/cmn_err.h>
41 #include "fs/fs_subr.h"
42 #include <sys/zfs_znode.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/spa.h>
48 #include <sys/zap.h>
49 #include <sys/varargs.h>
50 #include <sys/policy.h>
51 #include <sys/atomic.h>
52 #include <sys/mkdev.h>
53 #include <sys/modctl.h>
54 #include <sys/zfs_ioctl.h>
55 #include <sys/zfs_ctldir.h>
56 #include <sys/sunddi.h>
57 
58 int zfsfstype;
59 vfsops_t *zfs_vfsops = NULL;
60 static major_t zfs_major;
61 static minor_t zfs_minor;
62 static kmutex_t	zfs_dev_mtx;
63 
64 static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
65 static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr);
66 static int zfs_root(vfs_t *vfsp, vnode_t **vpp);
67 static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp);
68 static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp);
69 static void zfs_freevfs(vfs_t *vfsp);
70 static void zfs_objset_close(zfsvfs_t *zfsvfs);
71 
72 static const fs_operation_def_t zfs_vfsops_template[] = {
73 	VFSNAME_MOUNT, zfs_mount,
74 	VFSNAME_UNMOUNT, zfs_umount,
75 	VFSNAME_ROOT, zfs_root,
76 	VFSNAME_STATVFS, zfs_statvfs,
77 	VFSNAME_SYNC, (fs_generic_func_p) zfs_sync,
78 	VFSNAME_VGET, zfs_vget,
79 	VFSNAME_FREEVFS, (fs_generic_func_p) zfs_freevfs,
80 	NULL, NULL
81 };
82 
83 static const fs_operation_def_t zfs_vfsops_eio_template[] = {
84 	VFSNAME_FREEVFS, (fs_generic_func_p) zfs_freevfs,
85 	NULL, NULL
86 };
87 
88 /*
89  * We need to keep a count of active fs's.
90  * This is necessary to prevent our module
91  * from being unloaded after a umount -f
92  */
93 static uint32_t	zfs_active_fs_count = 0;
94 
95 static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
96 static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
97 
98 static mntopt_t mntopts[] = {
99 	{ MNTOPT_XATTR, NULL, NULL, MO_NODISPLAY|MO_DEFAULT, NULL },
100 	{ MNTOPT_NOATIME, noatime_cancel, NULL, MO_DEFAULT, NULL },
101 	{ MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
102 };
103 
104 static mntopts_t zfs_mntopts = {
105 	sizeof (mntopts) / sizeof (mntopt_t),
106 	mntopts
107 };
108 
109 /*ARGSUSED*/
110 int
111 zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
112 {
113 	/*
114 	 * Data integrity is job one.  We don't want a compromised kernel
115 	 * writing to the storage pool, so we never sync during panic.
116 	 */
117 	if (panicstr)
118 		return (0);
119 
120 	/*
121 	 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
122 	 * to sync metadata, which they would otherwise cache indefinitely.
123 	 * Semantically, the only requirement is that the sync be initiated.
124 	 * The DMU syncs out txgs frequently, so there's nothing to do.
125 	 */
126 	if (flag & SYNC_ATTR)
127 		return (0);
128 
129 	if (vfsp != NULL) {
130 		/*
131 		 * Sync a specific filesystem.
132 		 */
133 		zfsvfs_t *zfsvfs = vfsp->vfs_data;
134 
135 		ZFS_ENTER(zfsvfs);
136 		if (zfsvfs->z_log != NULL)
137 			zil_commit(zfsvfs->z_log, UINT64_MAX, FSYNC);
138 		else
139 			txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
140 		ZFS_EXIT(zfsvfs);
141 	} else {
142 		/*
143 		 * Sync all ZFS filesystems.  This is what happens when you
144 		 * run sync(1M).  Unlike other filesystems, ZFS honors the
145 		 * request by waiting for all pools to commit all dirty data.
146 		 */
147 		spa_sync_allpools();
148 	}
149 
150 	return (0);
151 }
152 
153 static void
154 atime_changed_cb(void *arg, uint64_t newval)
155 {
156 	zfsvfs_t *zfsvfs = arg;
157 
158 	if (newval == TRUE) {
159 		zfsvfs->z_atime = TRUE;
160 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
161 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
162 	} else {
163 		zfsvfs->z_atime = FALSE;
164 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
165 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
166 	}
167 }
168 
169 static void
170 blksz_changed_cb(void *arg, uint64_t newval)
171 {
172 	zfsvfs_t *zfsvfs = arg;
173 
174 	if (newval < SPA_MINBLOCKSIZE ||
175 	    newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
176 		newval = SPA_MAXBLOCKSIZE;
177 
178 	zfsvfs->z_max_blksz = newval;
179 	zfsvfs->z_vfs->vfs_bsize = newval;
180 }
181 
182 static void
183 readonly_changed_cb(void *arg, uint64_t newval)
184 {
185 	zfsvfs_t *zfsvfs = arg;
186 
187 	if (newval) {
188 		/* XXX locking on vfs_flag? */
189 		zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
190 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
191 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
192 		(void) zfs_delete_thread_target(zfsvfs, 0);
193 	} else {
194 		/* XXX locking on vfs_flag? */
195 		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
196 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
197 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
198 		(void) zfs_delete_thread_target(zfsvfs, 1);
199 	}
200 }
201 
202 static void
203 devices_changed_cb(void *arg, uint64_t newval)
204 {
205 	zfsvfs_t *zfsvfs = arg;
206 
207 	if (newval == FALSE) {
208 		zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
209 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
210 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
211 	} else {
212 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
213 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
214 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
215 	}
216 }
217 
218 static void
219 setuid_changed_cb(void *arg, uint64_t newval)
220 {
221 	zfsvfs_t *zfsvfs = arg;
222 
223 	if (newval == FALSE) {
224 		zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
225 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
226 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
227 	} else {
228 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
229 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
230 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
231 	}
232 }
233 
234 static void
235 exec_changed_cb(void *arg, uint64_t newval)
236 {
237 	zfsvfs_t *zfsvfs = arg;
238 
239 	if (newval == FALSE) {
240 		zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
241 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
242 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
243 	} else {
244 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
245 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
246 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
247 	}
248 }
249 
250 static void
251 snapdir_changed_cb(void *arg, uint64_t newval)
252 {
253 	zfsvfs_t *zfsvfs = arg;
254 
255 	zfsvfs->z_show_ctldir = newval;
256 }
257 
258 static void
259 acl_mode_changed_cb(void *arg, uint64_t newval)
260 {
261 	zfsvfs_t *zfsvfs = arg;
262 
263 	zfsvfs->z_acl_mode = newval;
264 }
265 
266 static void
267 acl_inherit_changed_cb(void *arg, uint64_t newval)
268 {
269 	zfsvfs_t *zfsvfs = arg;
270 
271 	zfsvfs->z_acl_inherit = newval;
272 }
273 
274 /*ARGSUSED*/
275 static int
276 zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
277 {
278 	zfsvfs_t	*zfsvfs = NULL;
279 	znode_t		*zp = NULL;
280 	vnode_t		*vp = NULL;
281 	objset_t	*os = NULL;
282 	struct dsl_dataset *ds;
283 	char		*osname;
284 	uint64_t	readonly, recordsize;
285 	pathname_t	spn;
286 	dev_t		mount_dev;
287 	major_t		new_major;
288 	int		mode;
289 	int		error = 0;
290 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
291 				UIO_SYSSPACE : UIO_USERSPACE;
292 	int		canwrite;
293 
294 	if (mvp->v_type != VDIR)
295 		return (ENOTDIR);
296 
297 	mutex_enter(&mvp->v_lock);
298 	if ((uap->flags & MS_REMOUNT) == 0 &&
299 	    (uap->flags & MS_OVERLAY) == 0 &&
300 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
301 		mutex_exit(&mvp->v_lock);
302 		return (EBUSY);
303 	}
304 	mutex_exit(&mvp->v_lock);
305 
306 	/*
307 	 * ZFS does not support passing unparsed data in via MS_DATA.
308 	 * Users should use the MS_OPTIONSTR interface; this means
309 	 * that all option parsing is already done and the options struct
310 	 * can be interrogated.
311 	 */
312 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
313 		return (EINVAL);
314 
315 	/*
316 	 * When doing a remount, we simply refresh our temporary properties
317 	 * according to those options set in the current VFS options.
318 	 */
319 	if (uap->flags & MS_REMOUNT) {
320 		zfsvfs = vfsp->vfs_data;
321 
322 		if (vfs_optionisset(vfsp, MNTOPT_RO, NULL))
323 			readonly_changed_cb(zfsvfs, B_TRUE);
324 		else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
325 			if (dmu_objset_is_snapshot(zfsvfs->z_os))
326 				return (EROFS);
327 			readonly_changed_cb(zfsvfs, B_FALSE);
328 		}
329 
330 		if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
331 			devices_changed_cb(zfsvfs, B_FALSE);
332 			setuid_changed_cb(zfsvfs, B_FALSE);
333 		} else {
334 			if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
335 				devices_changed_cb(zfsvfs, B_FALSE);
336 			else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL))
337 				devices_changed_cb(zfsvfs, B_TRUE);
338 
339 			if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))
340 				setuid_changed_cb(zfsvfs, B_FALSE);
341 			else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL))
342 				setuid_changed_cb(zfsvfs, B_TRUE);
343 		}
344 
345 		if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL))
346 			exec_changed_cb(zfsvfs, B_FALSE);
347 		else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL))
348 			exec_changed_cb(zfsvfs, B_TRUE);
349 
350 		return (0);
351 	}
352 
353 	/*
354 	 * Get the objset name (the "special" mount argument).
355 	 */
356 	if (error = pn_get(uap->spec, fromspace, &spn))
357 		return (error);
358 
359 	osname = spn.pn_path;
360 
361 	if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
362 		goto out;
363 
364 	/*
365 	 * Refuse to mount a filesystem if we are in a local zone and the
366 	 * dataset is not visible.
367 	 */
368 	if (!INGLOBALZONE(curproc) &&
369 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
370 		error = EPERM;
371 		goto out;
372 	}
373 
374 	/*
375 	 * Initialize the zfs-specific filesystem structure.
376 	 * Should probably make this a kmem cache, shuffle fields,
377 	 * and just bzero upto z_hold_mtx[].
378 	 */
379 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
380 	zfsvfs->z_vfs = vfsp;
381 	zfsvfs->z_parent = zfsvfs;
382 	zfsvfs->z_assign = TXG_NOWAIT;
383 	zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
384 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
385 
386 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
387 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
388 	    offsetof(znode_t, z_link_node));
389 	rw_init(&zfsvfs->z_um_lock, NULL, RW_DEFAULT, NULL);
390 
391 	/*
392 	 * Initialize the generic filesystem structure.
393 	 */
394 	vfsp->vfs_bcount = 0;
395 	vfsp->vfs_data = NULL;
396 
397 	/*
398 	 * Create a unique device for the mount.
399 	 */
400 	do {
401 		ASSERT3U(zfs_minor, <=, MAXMIN32);
402 		minor_t start = zfs_minor;
403 		do {
404 			mutex_enter(&zfs_dev_mtx);
405 			if (zfs_minor >= MAXMIN32) {
406 				/*
407 				 * If we're still using the real major number,
408 				 * keep out of /dev/zfs and /dev/zvol minor
409 				 * number space.  If we're using a getudev()'ed
410 				 * major number, we can use all of its minors.
411 				 */
412 				if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
413 					zfs_minor = ZFS_MIN_MINOR;
414 				else
415 					zfs_minor = 0;
416 			} else {
417 				zfs_minor++;
418 			}
419 			mount_dev = makedevice(zfs_major, zfs_minor);
420 			mutex_exit(&zfs_dev_mtx);
421 		} while (vfs_devismounted(mount_dev) && zfs_minor != start);
422 		if (zfs_minor == start) {
423 			/*
424 			 * We are using all ~262,000 minor numbers
425 			 * for the current major number.  Create a
426 			 * new major number.
427 			 */
428 			if ((new_major = getudev()) == (major_t)-1) {
429 				cmn_err(CE_WARN,
430 				    "zfs_mount: Can't get unique"
431 				    " major device number.");
432 				goto out;
433 			}
434 			mutex_enter(&zfs_dev_mtx);
435 			zfs_major = new_major;
436 			zfs_minor = 0;
437 			mutex_exit(&zfs_dev_mtx);
438 		} else {
439 			break;
440 		}
441 		/* CONSTANTCONDITION */
442 	} while (1);
443 
444 	ASSERT(vfs_devismounted(mount_dev) == 0);
445 
446 	if (dsl_prop_get_integer(osname, "recordsize", &recordsize, NULL) != 0)
447 		recordsize = SPA_MAXBLOCKSIZE;
448 
449 	vfsp->vfs_dev = mount_dev;
450 	vfsp->vfs_fstype = zfsfstype;
451 	vfsp->vfs_bsize = recordsize;
452 	vfsp->vfs_flag |= VFS_NOTRUNC;
453 	vfsp->vfs_data = zfsvfs;
454 
455 	error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL);
456 	if (error)
457 		goto out;
458 
459 	if (readonly)
460 		mode = DS_MODE_PRIMARY | DS_MODE_READONLY;
461 	else
462 		mode = DS_MODE_PRIMARY;
463 
464 	error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
465 	if (error == EROFS) {
466 		mode = DS_MODE_PRIMARY | DS_MODE_READONLY;
467 		error = dmu_objset_open(osname, DMU_OST_ZFS, mode,
468 		    &zfsvfs->z_os);
469 	}
470 	os = zfsvfs->z_os;
471 
472 	if (error)
473 		goto out;
474 
475 	if (error = zfs_init_fs(zfsvfs, &zp, cr))
476 		goto out;
477 
478 	if (dmu_objset_is_snapshot(os)) {
479 		ASSERT(mode & DS_MODE_READONLY);
480 		atime_changed_cb(zfsvfs, B_FALSE);
481 		readonly_changed_cb(zfsvfs, B_TRUE);
482 		zfsvfs->z_issnap = B_TRUE;
483 	} else {
484 		int do_readonly = FALSE, readonly;
485 		int do_setuid = FALSE, setuid;
486 		int do_exec = FALSE, exec;
487 		int do_devices = FALSE, devices;
488 
489 		/*
490 		 * Start a delete thread running.
491 		 */
492 		(void) zfs_delete_thread_target(zfsvfs, 1);
493 
494 		/*
495 		 * Parse and replay the intent log.
496 		 */
497 		zil_replay(os, zfsvfs, &zfsvfs->z_assign, zfs_replay_vector,
498 		    (void (*)(void *))zfs_delete_wait_empty);
499 
500 		if (!zil_disable)
501 			zfsvfs->z_log = zil_open(os, zfs_get_data);
502 
503 		/*
504 		 * The act of registering our callbacks will destroy any mount
505 		 * options we may have.  In order to enable temporary overrides
506 		 * of mount options, we stash away the current values and
507 		 * restore them after we register the callbacks.
508 		 */
509 		if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
510 			readonly = B_TRUE;
511 			do_readonly = B_TRUE;
512 		} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
513 			readonly = B_FALSE;
514 			do_readonly = B_TRUE;
515 		}
516 		if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
517 			devices = B_FALSE;
518 			setuid = B_FALSE;
519 			do_devices = B_TRUE;
520 			do_setuid = B_TRUE;
521 		} else {
522 			if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
523 				devices = B_FALSE;
524 				do_devices = B_TRUE;
525 			} else if (vfs_optionisset(vfsp,
526 			    MNTOPT_DEVICES, NULL)) {
527 				devices = B_TRUE;
528 				do_devices = B_TRUE;
529 			}
530 
531 			if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
532 				setuid = B_FALSE;
533 				do_setuid = B_TRUE;
534 			} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
535 				setuid = B_TRUE;
536 				do_setuid = B_TRUE;
537 			}
538 		}
539 		if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
540 			exec = B_FALSE;
541 			do_exec = B_TRUE;
542 		} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
543 			exec = B_TRUE;
544 			do_exec = B_TRUE;
545 		}
546 
547 		/*
548 		 * Register property callbacks.
549 		 */
550 		ds = dmu_objset_ds(os);
551 		VERIFY(dsl_prop_register(ds, "atime", atime_changed_cb,
552 		    zfsvfs) == 0);
553 
554 		VERIFY(dsl_prop_register(ds, "recordsize", blksz_changed_cb,
555 		    zfsvfs) == 0);
556 
557 		VERIFY(dsl_prop_register(ds, "readonly", readonly_changed_cb,
558 		    zfsvfs) == 0);
559 
560 		VERIFY(dsl_prop_register(ds, "devices", devices_changed_cb,
561 		    zfsvfs) == 0);
562 
563 		VERIFY(dsl_prop_register(ds, "setuid", setuid_changed_cb,
564 		    zfsvfs) == 0);
565 
566 		VERIFY(dsl_prop_register(ds, "exec", exec_changed_cb,
567 		    zfsvfs) == 0);
568 
569 		VERIFY(dsl_prop_register(ds, "snapdir", snapdir_changed_cb,
570 		    zfsvfs) == 0);
571 
572 		VERIFY(dsl_prop_register(ds, "aclmode", acl_mode_changed_cb,
573 		    zfsvfs) == 0);
574 
575 		VERIFY(dsl_prop_register(ds, "aclinherit",
576 		    acl_inherit_changed_cb, zfsvfs) == 0);
577 
578 
579 		/*
580 		 * Invoke our callbacks to restore temporary mount options.
581 		 */
582 		if (do_readonly)
583 			readonly_changed_cb(zfsvfs, readonly);
584 		if (do_setuid)
585 			setuid_changed_cb(zfsvfs, setuid);
586 		if (do_exec)
587 			exec_changed_cb(zfsvfs, exec);
588 		if (do_devices)
589 			devices_changed_cb(zfsvfs, devices);
590 	}
591 
592 	vp = ZTOV(zp);
593 	if (!zfsvfs->z_issnap)
594 		zfsctl_create(zfsvfs);
595 out:
596 	if (error) {
597 		if (zp)
598 			VN_RELE(vp);
599 
600 		if (zfsvfs) {
601 			if (os)
602 				dmu_objset_close(os);
603 			kmem_free(zfsvfs, sizeof (zfsvfs_t));
604 		}
605 	} else {
606 		atomic_add_32(&zfs_active_fs_count, 1);
607 		VN_RELE(vp);
608 	}
609 
610 	pn_free(&spn);
611 	return (error);
612 }
613 
614 static int
615 zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
616 {
617 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
618 	dmu_objset_stats_t dstats;
619 	dev32_t d32;
620 
621 	ZFS_ENTER(zfsvfs);
622 
623 	dmu_objset_stats(zfsvfs->z_os, &dstats);
624 
625 	/*
626 	 * The underlying storage pool actually uses multiple block sizes.
627 	 * We report the fragsize as the smallest block size we support,
628 	 * and we report our blocksize as the filesystem's maximum blocksize.
629 	 */
630 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
631 	statp->f_bsize = zfsvfs->z_max_blksz;
632 
633 	/*
634 	 * The following report "total" blocks of various kinds in the
635 	 * file system, but reported in terms of f_frsize - the
636 	 * "fragment" size.
637 	 */
638 
639 	statp->f_blocks =
640 	    (dstats.dds_space_refd + dstats.dds_available) >> SPA_MINBLOCKSHIFT;
641 	statp->f_bfree = dstats.dds_available >> SPA_MINBLOCKSHIFT;
642 	statp->f_bavail = statp->f_bfree; /* no root reservation */
643 
644 	/*
645 	 * statvfs() should really be called statufs(), because it assumes
646 	 * static metadata.  ZFS doesn't preallocate files, so the best
647 	 * we can do is report the max that could possibly fit in f_files,
648 	 * and that minus the number actually used in f_ffree.
649 	 * For f_ffree, report the smaller of the number of object available
650 	 * and the number of blocks (each object will take at least a block).
651 	 */
652 	statp->f_ffree = MIN(dstats.dds_objects_avail, statp->f_bfree);
653 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
654 	statp->f_files = statp->f_ffree + dstats.dds_objects_used;
655 
656 	(void) cmpldev(&d32, vfsp->vfs_dev);
657 	statp->f_fsid = d32;
658 
659 	/*
660 	 * We're a zfs filesystem.
661 	 */
662 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
663 
664 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
665 
666 	statp->f_namemax = ZFS_MAXNAMELEN;
667 
668 	/*
669 	 * We have all of 32 characters to stuff a string here.
670 	 * Is there anything useful we could/should provide?
671 	 */
672 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
673 
674 	ZFS_EXIT(zfsvfs);
675 	return (0);
676 }
677 
678 static int
679 zfs_root(vfs_t *vfsp, vnode_t **vpp)
680 {
681 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
682 	znode_t *rootzp;
683 	int error;
684 
685 	ZFS_ENTER(zfsvfs);
686 
687 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
688 	if (error == 0)
689 		*vpp = ZTOV(rootzp);
690 
691 	ZFS_EXIT(zfsvfs);
692 	return (error);
693 }
694 
695 /*ARGSUSED*/
696 static int
697 zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
698 {
699 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
700 	int ret;
701 
702 	if ((ret = secpolicy_fs_unmount(cr, vfsp)) != 0)
703 		return (ret);
704 
705 	/*
706 	 * Unmount any snapshots mounted under .zfs before unmounting the
707 	 * dataset itself.
708 	 */
709 	if (zfsvfs->z_ctldir != NULL &&
710 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0)
711 		return (ret);
712 
713 	if (fflag & MS_FORCE) {
714 		vfsp->vfs_flag |= VFS_UNMOUNTED;
715 		zfsvfs->z_unmounted1 = B_TRUE;
716 
717 		/*
718 		 * Wait for all zfs threads to leave zfs.
719 		 * Grabbing a rwlock as reader in all vops and
720 		 * as writer here doesn't work because it too easy to get
721 		 * multiple reader enters as zfs can re-enter itself.
722 		 * This can lead to deadlock if there is an intervening
723 		 * rw_enter as writer.
724 		 * So a file system threads ref count (z_op_cnt) is used.
725 		 * A polling loop on z_op_cnt may seem inefficient, but
726 		 * - this saves all threads on exit from having to grab a
727 		 *   mutex in order to cv_signal
728 		 * - only occurs on forced unmount in the rare case when
729 		 *   there are outstanding threads within the file system.
730 		 */
731 		while (zfsvfs->z_op_cnt) {
732 			delay(1);
733 		}
734 
735 		zfs_objset_close(zfsvfs);
736 
737 		return (0);
738 	}
739 
740 	zfs_zcache_flush(zfsvfs);
741 
742 	/*
743 	 * Stop all delete threads.
744 	 */
745 	(void) zfs_delete_thread_target(zfsvfs, 0);
746 
747 	/*
748 	 * Check the number of active vnodes in the file system.
749 	 * Our count is maintained in the vfs structure, but the number
750 	 * is off by 1 to indicate a hold on the vfs structure itself.
751 	 *
752 	 * The '.zfs' directory maintains a reference of its own, and any active
753 	 * references underneath are reflected in the vnode count.
754 	 */
755 	if (zfsvfs->z_ctldir == NULL) {
756 		if (vfsp->vfs_count > 1) {
757 			if ((zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) == 0)
758 				(void) zfs_delete_thread_target(zfsvfs, 1);
759 			return (EBUSY);
760 		}
761 	} else {
762 		if (vfsp->vfs_count > 2 ||
763 		    (zfsvfs->z_ctldir->v_count > 1 && !(fflag & MS_FORCE))) {
764 			if ((zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) == 0)
765 				(void) zfs_delete_thread_target(zfsvfs, 1);
766 			return (EBUSY);
767 		}
768 	}
769 
770 	vfsp->vfs_flag |= VFS_UNMOUNTED;
771 	zfs_objset_close(zfsvfs);
772 
773 	/*
774 	 * We can now safely destroy the '.zfs' directory node, which will
775 	 * release its hold on the vfs_t.
776 	 */
777 	if (zfsvfs->z_ctldir != NULL)
778 		zfsctl_destroy(zfsvfs);
779 
780 	return (0);
781 }
782 
783 static int
784 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
785 {
786 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
787 	znode_t		*zp;
788 	uint64_t	object = 0;
789 	uint64_t	fid_gen = 0;
790 	uint64_t	gen_mask;
791 	uint64_t	zp_gen;
792 	int 		i, err;
793 
794 	*vpp = NULL;
795 
796 	ZFS_ENTER(zfsvfs);
797 
798 	if (fidp->fid_len == LONG_FID_LEN) {
799 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
800 		uint64_t	objsetid = 0;
801 		uint64_t	setgen = 0;
802 
803 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
804 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
805 
806 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
807 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
808 
809 		ZFS_EXIT(zfsvfs);
810 
811 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
812 		if (err)
813 			return (EINVAL);
814 		ZFS_ENTER(zfsvfs);
815 	}
816 
817 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
818 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
819 
820 		for (i = 0; i < sizeof (zfid->zf_object); i++)
821 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
822 
823 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
824 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
825 	} else {
826 		ZFS_EXIT(zfsvfs);
827 		return (EINVAL);
828 	}
829 
830 	/* A zero fid_gen means we are in the .zfs control directories */
831 	if (fid_gen == 0 &&
832 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
833 		*vpp = zfsvfs->z_ctldir;
834 		ASSERT(*vpp != NULL);
835 		if (object == ZFSCTL_INO_SNAPDIR) {
836 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
837 			    0, NULL, NULL) == 0);
838 		} else {
839 			VN_HOLD(*vpp);
840 		}
841 		ZFS_EXIT(zfsvfs);
842 		return (0);
843 	}
844 
845 	gen_mask = -1ULL >> (64 - 8 * i);
846 
847 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
848 	if (err = zfs_zget(zfsvfs, object, &zp)) {
849 		ZFS_EXIT(zfsvfs);
850 		return (err);
851 	}
852 	zp_gen = zp->z_phys->zp_gen & gen_mask;
853 	if (zp_gen == 0)
854 		zp_gen = 1;
855 	if (zp->z_reap || zp_gen != fid_gen) {
856 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
857 		VN_RELE(ZTOV(zp));
858 		ZFS_EXIT(zfsvfs);
859 		return (EINVAL);
860 	}
861 
862 	*vpp = ZTOV(zp);
863 	ZFS_EXIT(zfsvfs);
864 	return (0);
865 }
866 
867 static void
868 zfs_objset_close(zfsvfs_t *zfsvfs)
869 {
870 	zfs_delete_t	*zd = &zfsvfs->z_delete_head;
871 	znode_t		*zp, *nextzp;
872 	objset_t	*os = zfsvfs->z_os;
873 	struct dsl_dataset *ds;
874 
875 	/*
876 	 * Stop all delete threads.
877 	 */
878 	(void) zfs_delete_thread_target(zfsvfs, 0);
879 
880 	/*
881 	 * For forced unmount, at this point all vops except zfs_inactive
882 	 * are erroring EIO. We need to now suspend zfs_inactive threads
883 	 * while we are freeing dbufs before switching zfs_inactive
884 	 * to use behaviour without a objset.
885 	 */
886 	rw_enter(&zfsvfs->z_um_lock, RW_WRITER);
887 
888 	zfs_zcache_flush(zfsvfs);
889 
890 	/*
891 	 * Release all delete in progress znodes
892 	 * They will be processed when the file system remounts.
893 	 */
894 	mutex_enter(&zd->z_mutex);
895 	while (zp = list_head(&zd->z_znodes)) {
896 		list_remove(&zd->z_znodes, zp);
897 		zp->z_dbuf_held = 0;
898 		dmu_buf_rele(zp->z_dbuf);
899 	}
900 	mutex_exit(&zd->z_mutex);
901 
902 	/*
903 	 * Release all holds on dbufs
904 	 * Note, although we have stopped all other vop threads and
905 	 * zfs_inactive(), the dmu can callback via znode_pageout_func()
906 	 * which can zfs_znode_free() the znode.
907 	 * So we lock z_all_znodes; search the list for a held
908 	 * dbuf; drop the lock (we know zp can't disappear if we hold
909 	 * a dbuf lock; then regrab the lock and restart.
910 	 */
911 	mutex_enter(&zfsvfs->z_znodes_lock);
912 	for (zp = list_head(&zfsvfs->z_all_znodes); zp; zp = nextzp) {
913 		nextzp = list_next(&zfsvfs->z_all_znodes, zp);
914 		if (zp->z_dbuf_held) {
915 			/* dbufs should only be held when force unmounting */
916 			zp->z_dbuf_held = 0;
917 			mutex_exit(&zfsvfs->z_znodes_lock);
918 			dmu_buf_rele(zp->z_dbuf);
919 			/* Start again */
920 			mutex_enter(&zfsvfs->z_znodes_lock);
921 			nextzp = list_head(&zfsvfs->z_all_znodes);
922 		}
923 	}
924 	mutex_exit(&zfsvfs->z_znodes_lock);
925 
926 	/*
927 	 * Unregister properties.
928 	 */
929 	if (!dmu_objset_is_snapshot(os)) {
930 		ds = dmu_objset_ds(os);
931 
932 		VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
933 		    zfsvfs) == 0);
934 
935 		VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
936 		    zfsvfs) == 0);
937 
938 		VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
939 		    zfsvfs) == 0);
940 
941 		VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
942 		    zfsvfs) == 0);
943 
944 		VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
945 		    zfsvfs) == 0);
946 
947 		VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
948 		    zfsvfs) == 0);
949 
950 		VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
951 		    zfsvfs) == 0);
952 
953 		VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
954 		    zfsvfs) == 0);
955 
956 		VERIFY(dsl_prop_unregister(ds, "aclinherit",
957 		    acl_inherit_changed_cb, zfsvfs) == 0);
958 	}
959 
960 	/*
961 	 * Make the dmu drop all it dbuf holds so that zfs_inactive
962 	 * can then safely free znode/vnodes.
963 	 */
964 	txg_wait_synced(dmu_objset_pool(os), 0);
965 
966 	/*
967 	 * Switch zfs_inactive to behaviour without an objset.
968 	 * It just tosses cached pages and frees the znode & vnode.
969 	 * Then re-enable zfs_inactive threads in that new behaviour.
970 	 */
971 	zfsvfs->z_unmounted2 = B_TRUE;
972 	rw_exit(&zfsvfs->z_um_lock); /* re-enable any zfs_inactive threads */
973 
974 	/*
975 	 * Close the zil. Can't close the zil while zfs_inactive
976 	 * threads are blocked as zil_close can call zfs_inactive.
977 	 */
978 	if (zfsvfs->z_log) {
979 		zil_close(zfsvfs->z_log);
980 		zfsvfs->z_log = NULL;
981 	}
982 
983 	/*
984 	 * Finally close the objset
985 	 */
986 	dmu_objset_close(os);
987 
988 }
989 
990 static void
991 zfs_freevfs(vfs_t *vfsp)
992 {
993 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
994 
995 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
996 
997 	atomic_add_32(&zfs_active_fs_count, -1);
998 }
999 
1000 /*
1001  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
1002  * so we can't safely do any non-idempotent initialization here.
1003  * Leave that to zfs_init() and zfs_fini(), which are called
1004  * from the module's _init() and _fini() entry points.
1005  */
1006 /*ARGSUSED*/
1007 static int
1008 zfs_vfsinit(int fstype, char *name)
1009 {
1010 	int error;
1011 
1012 	zfsfstype = fstype;
1013 
1014 	/*
1015 	 * Setup vfsops and vnodeops tables.
1016 	 */
1017 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
1018 	if (error != 0) {
1019 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
1020 	}
1021 
1022 	error = zfs_create_op_tables();
1023 	if (error) {
1024 		zfs_remove_op_tables();
1025 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
1026 		(void) vfs_freevfsops_by_type(zfsfstype);
1027 		return (error);
1028 	}
1029 
1030 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
1031 
1032 	/*
1033 	 * Unique major number for all zfs mounts.
1034 	 * If we run out of 32-bit minors, we'll getudev() another major.
1035 	 */
1036 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
1037 	zfs_minor = ZFS_MIN_MINOR;
1038 
1039 	return (0);
1040 }
1041 
1042 void
1043 zfs_init(void)
1044 {
1045 	/*
1046 	 * Initialize .zfs directory structures
1047 	 */
1048 	zfsctl_init();
1049 
1050 	/*
1051 	 * Initialize znode cache, vnode ops, etc...
1052 	 */
1053 	zfs_znode_init();
1054 }
1055 
1056 void
1057 zfs_fini(void)
1058 {
1059 	zfsctl_fini();
1060 	zfs_znode_fini();
1061 }
1062 
1063 int
1064 zfs_busy(void)
1065 {
1066 	return (zfs_active_fs_count != 0);
1067 }
1068 
1069 static vfsdef_t vfw = {
1070 	VFSDEF_VERSION,
1071 	MNTTYPE_ZFS,
1072 	zfs_vfsinit,
1073 	VSW_HASPROTO | VSW_CANRWRO | VSW_CANREMOUNT | VSW_VOLATILEDEV,
1074 	&zfs_mntopts
1075 };
1076 
1077 struct modlfs zfs_modlfs = {
1078 	&mod_fsops, "ZFS filesystem version 1", &vfw
1079 };
1080