xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 033f983390fa5d2b54e3e09d83ac9000d71ddaae)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/sysmacros.h>
32 #include <sys/kmem.h>
33 #include <sys/pathname.h>
34 #include <sys/acl.h>
35 #include <sys/vnode.h>
36 #include <sys/vfs.h>
37 #include <sys/mntent.h>
38 #include <sys/mount.h>
39 #include <sys/cmn_err.h>
40 #include "fs/fs_subr.h"
41 #include <sys/zfs_znode.h>
42 #include <sys/zil.h>
43 #include <sys/fs/zfs.h>
44 #include <sys/dmu.h>
45 #include <sys/dsl_prop.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/mkdev.h>
52 #include <sys/modctl.h>
53 #include <sys/zfs_ioctl.h>
54 #include <sys/zfs_ctldir.h>
55 #include <sys/sunddi.h>
56 #include <sys/dnlc.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 	(void) dnlc_purge_vfsp(vfsp, 0);
707 
708 	/*
709 	 * Unmount any snapshots mounted under .zfs before unmounting the
710 	 * dataset itself.
711 	 */
712 	if (zfsvfs->z_ctldir != NULL &&
713 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0)
714 		return (ret);
715 
716 	if (fflag & MS_FORCE) {
717 		vfsp->vfs_flag |= VFS_UNMOUNTED;
718 		zfsvfs->z_unmounted1 = B_TRUE;
719 
720 		/*
721 		 * Wait for all zfs threads to leave zfs.
722 		 * Grabbing a rwlock as reader in all vops and
723 		 * as writer here doesn't work because it too easy to get
724 		 * multiple reader enters as zfs can re-enter itself.
725 		 * This can lead to deadlock if there is an intervening
726 		 * rw_enter as writer.
727 		 * So a file system threads ref count (z_op_cnt) is used.
728 		 * A polling loop on z_op_cnt may seem inefficient, but
729 		 * - this saves all threads on exit from having to grab a
730 		 *   mutex in order to cv_signal
731 		 * - only occurs on forced unmount in the rare case when
732 		 *   there are outstanding threads within the file system.
733 		 */
734 		while (zfsvfs->z_op_cnt) {
735 			delay(1);
736 		}
737 
738 		zfs_objset_close(zfsvfs);
739 
740 		return (0);
741 	}
742 
743 	zfs_zcache_flush(zfsvfs);
744 
745 	/*
746 	 * Stop all delete threads.
747 	 */
748 	(void) zfs_delete_thread_target(zfsvfs, 0);
749 
750 	/*
751 	 * Check the number of active vnodes in the file system.
752 	 * Our count is maintained in the vfs structure, but the number
753 	 * is off by 1 to indicate a hold on the vfs structure itself.
754 	 *
755 	 * The '.zfs' directory maintains a reference of its own, and any active
756 	 * references underneath are reflected in the vnode count.
757 	 */
758 	if (zfsvfs->z_ctldir == NULL) {
759 		if (vfsp->vfs_count > 1) {
760 			if ((zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) == 0)
761 				(void) zfs_delete_thread_target(zfsvfs, 1);
762 			return (EBUSY);
763 		}
764 	} else {
765 		if (vfsp->vfs_count > 2 ||
766 		    (zfsvfs->z_ctldir->v_count > 1 && !(fflag & MS_FORCE))) {
767 			if ((zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) == 0)
768 				(void) zfs_delete_thread_target(zfsvfs, 1);
769 			return (EBUSY);
770 		}
771 	}
772 
773 	vfsp->vfs_flag |= VFS_UNMOUNTED;
774 	zfs_objset_close(zfsvfs);
775 
776 	return (0);
777 }
778 
779 static int
780 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
781 {
782 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
783 	znode_t		*zp;
784 	uint64_t	object = 0;
785 	uint64_t	fid_gen = 0;
786 	uint64_t	gen_mask;
787 	uint64_t	zp_gen;
788 	int 		i, err;
789 
790 	*vpp = NULL;
791 
792 	ZFS_ENTER(zfsvfs);
793 
794 	if (fidp->fid_len == LONG_FID_LEN) {
795 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
796 		uint64_t	objsetid = 0;
797 		uint64_t	setgen = 0;
798 
799 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
800 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
801 
802 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
803 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
804 
805 		ZFS_EXIT(zfsvfs);
806 
807 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
808 		if (err)
809 			return (EINVAL);
810 		ZFS_ENTER(zfsvfs);
811 	}
812 
813 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
814 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
815 
816 		for (i = 0; i < sizeof (zfid->zf_object); i++)
817 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
818 
819 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
820 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
821 	} else {
822 		ZFS_EXIT(zfsvfs);
823 		return (EINVAL);
824 	}
825 
826 	/* A zero fid_gen means we are in the .zfs control directories */
827 	if (fid_gen == 0 &&
828 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
829 		*vpp = zfsvfs->z_ctldir;
830 		ASSERT(*vpp != NULL);
831 		if (object == ZFSCTL_INO_SNAPDIR) {
832 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
833 			    0, NULL, NULL) == 0);
834 		} else {
835 			VN_HOLD(*vpp);
836 		}
837 		ZFS_EXIT(zfsvfs);
838 		return (0);
839 	}
840 
841 	gen_mask = -1ULL >> (64 - 8 * i);
842 
843 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
844 	if (err = zfs_zget(zfsvfs, object, &zp)) {
845 		ZFS_EXIT(zfsvfs);
846 		return (err);
847 	}
848 	zp_gen = zp->z_phys->zp_gen & gen_mask;
849 	if (zp_gen == 0)
850 		zp_gen = 1;
851 	if (zp->z_reap || zp_gen != fid_gen) {
852 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
853 		VN_RELE(ZTOV(zp));
854 		ZFS_EXIT(zfsvfs);
855 		return (EINVAL);
856 	}
857 
858 	*vpp = ZTOV(zp);
859 	ZFS_EXIT(zfsvfs);
860 	return (0);
861 }
862 
863 static void
864 zfs_objset_close(zfsvfs_t *zfsvfs)
865 {
866 	zfs_delete_t	*zd = &zfsvfs->z_delete_head;
867 	znode_t		*zp, *nextzp;
868 	objset_t	*os = zfsvfs->z_os;
869 	struct dsl_dataset *ds;
870 
871 	/*
872 	 * Stop all delete threads.
873 	 */
874 	(void) zfs_delete_thread_target(zfsvfs, 0);
875 
876 	/*
877 	 * For forced unmount, at this point all vops except zfs_inactive
878 	 * are erroring EIO. We need to now suspend zfs_inactive threads
879 	 * while we are freeing dbufs before switching zfs_inactive
880 	 * to use behaviour without a objset.
881 	 */
882 	rw_enter(&zfsvfs->z_um_lock, RW_WRITER);
883 
884 	zfs_zcache_flush(zfsvfs);
885 
886 	/*
887 	 * Release all delete in progress znodes
888 	 * They will be processed when the file system remounts.
889 	 */
890 	mutex_enter(&zd->z_mutex);
891 	while (zp = list_head(&zd->z_znodes)) {
892 		list_remove(&zd->z_znodes, zp);
893 		zp->z_dbuf_held = 0;
894 		dmu_buf_rele(zp->z_dbuf);
895 	}
896 	mutex_exit(&zd->z_mutex);
897 
898 	/*
899 	 * Release all holds on dbufs
900 	 * Note, although we have stopped all other vop threads and
901 	 * zfs_inactive(), the dmu can callback via znode_pageout_func()
902 	 * which can zfs_znode_free() the znode.
903 	 * So we lock z_all_znodes; search the list for a held
904 	 * dbuf; drop the lock (we know zp can't disappear if we hold
905 	 * a dbuf lock; then regrab the lock and restart.
906 	 */
907 	mutex_enter(&zfsvfs->z_znodes_lock);
908 	for (zp = list_head(&zfsvfs->z_all_znodes); zp; zp = nextzp) {
909 		nextzp = list_next(&zfsvfs->z_all_znodes, zp);
910 		if (zp->z_dbuf_held) {
911 			/* dbufs should only be held when force unmounting */
912 			zp->z_dbuf_held = 0;
913 			mutex_exit(&zfsvfs->z_znodes_lock);
914 			dmu_buf_rele(zp->z_dbuf);
915 			/* Start again */
916 			mutex_enter(&zfsvfs->z_znodes_lock);
917 			nextzp = list_head(&zfsvfs->z_all_znodes);
918 		}
919 	}
920 	mutex_exit(&zfsvfs->z_znodes_lock);
921 
922 	/*
923 	 * Unregister properties.
924 	 */
925 	if (!dmu_objset_is_snapshot(os)) {
926 		ds = dmu_objset_ds(os);
927 
928 		VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
929 		    zfsvfs) == 0);
930 
931 		VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
932 		    zfsvfs) == 0);
933 
934 		VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
935 		    zfsvfs) == 0);
936 
937 		VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
938 		    zfsvfs) == 0);
939 
940 		VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
941 		    zfsvfs) == 0);
942 
943 		VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
944 		    zfsvfs) == 0);
945 
946 		VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
947 		    zfsvfs) == 0);
948 
949 		VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
950 		    zfsvfs) == 0);
951 
952 		VERIFY(dsl_prop_unregister(ds, "aclinherit",
953 		    acl_inherit_changed_cb, zfsvfs) == 0);
954 	}
955 
956 	/*
957 	 * Make the dmu drop all it dbuf holds so that zfs_inactive
958 	 * can then safely free znode/vnodes.
959 	 */
960 	txg_wait_synced(dmu_objset_pool(os), 0);
961 
962 	/*
963 	 * Switch zfs_inactive to behaviour without an objset.
964 	 * It just tosses cached pages and frees the znode & vnode.
965 	 * Then re-enable zfs_inactive threads in that new behaviour.
966 	 */
967 	zfsvfs->z_unmounted2 = B_TRUE;
968 	rw_exit(&zfsvfs->z_um_lock); /* re-enable any zfs_inactive threads */
969 
970 	/*
971 	 * Close the zil. Can't close the zil while zfs_inactive
972 	 * threads are blocked as zil_close can call zfs_inactive.
973 	 */
974 	if (zfsvfs->z_log) {
975 		zil_close(zfsvfs->z_log);
976 		zfsvfs->z_log = NULL;
977 	}
978 
979 	/*
980 	 * Finally close the objset
981 	 */
982 	dmu_objset_close(os);
983 
984 	/*
985 	 * We can now safely destroy the '.zfs' directory node.
986 	 */
987 	if (zfsvfs->z_ctldir != NULL)
988 		zfsctl_destroy(zfsvfs);
989 
990 }
991 
992 static void
993 zfs_freevfs(vfs_t *vfsp)
994 {
995 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
996 
997 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
998 
999 	atomic_add_32(&zfs_active_fs_count, -1);
1000 }
1001 
1002 /*
1003  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
1004  * so we can't safely do any non-idempotent initialization here.
1005  * Leave that to zfs_init() and zfs_fini(), which are called
1006  * from the module's _init() and _fini() entry points.
1007  */
1008 /*ARGSUSED*/
1009 static int
1010 zfs_vfsinit(int fstype, char *name)
1011 {
1012 	int error;
1013 
1014 	zfsfstype = fstype;
1015 
1016 	/*
1017 	 * Setup vfsops and vnodeops tables.
1018 	 */
1019 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
1020 	if (error != 0) {
1021 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
1022 	}
1023 
1024 	error = zfs_create_op_tables();
1025 	if (error) {
1026 		zfs_remove_op_tables();
1027 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
1028 		(void) vfs_freevfsops_by_type(zfsfstype);
1029 		return (error);
1030 	}
1031 
1032 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
1033 
1034 	/*
1035 	 * Unique major number for all zfs mounts.
1036 	 * If we run out of 32-bit minors, we'll getudev() another major.
1037 	 */
1038 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
1039 	zfs_minor = ZFS_MIN_MINOR;
1040 
1041 	return (0);
1042 }
1043 
1044 void
1045 zfs_init(void)
1046 {
1047 	/*
1048 	 * Initialize .zfs directory structures
1049 	 */
1050 	zfsctl_init();
1051 
1052 	/*
1053 	 * Initialize znode cache, vnode ops, etc...
1054 	 */
1055 	zfs_znode_init();
1056 }
1057 
1058 void
1059 zfs_fini(void)
1060 {
1061 	zfsctl_fini();
1062 	zfs_znode_fini();
1063 }
1064 
1065 int
1066 zfs_busy(void)
1067 {
1068 	return (zfs_active_fs_count != 0);
1069 }
1070 
1071 static vfsdef_t vfw = {
1072 	VFSDEF_VERSION,
1073 	MNTTYPE_ZFS,
1074 	zfs_vfsinit,
1075 	VSW_HASPROTO | VSW_CANRWRO | VSW_CANREMOUNT | VSW_VOLATILEDEV,
1076 	&zfs_mntopts
1077 };
1078 
1079 struct modlfs zfs_modlfs = {
1080 	&mod_fsops, "ZFS filesystem version 1", &vfw
1081 };
1082