17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55a59a8b3Srsb  * Common Development and Distribution License (the "License").
65a59a8b3Srsb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
220fbb751dSJohn Levon  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
23401bc9afSJoshua M. Clulow  * Copyright 2015 Joyent, Inc.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
297c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
307c478bd9Sstevel@tonic-gate #include <sys/time.h>
317c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
327c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
33aa59c4cbSrsb #include <sys/vfs_opreg.h>
347c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <sys/uio.h>
377c478bd9Sstevel@tonic-gate #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate #include <sys/errno.h>
397c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
407c478bd9Sstevel@tonic-gate #include <sys/cred.h>
417c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
427c478bd9Sstevel@tonic-gate #include <sys/mount.h>
437c478bd9Sstevel@tonic-gate #include <sys/debug.h>
447c478bd9Sstevel@tonic-gate #include <sys/systm.h>
457c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
467c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
477c478bd9Sstevel@tonic-gate #include <vm/page.h>
487c478bd9Sstevel@tonic-gate #include <vm/anon.h>
497c478bd9Sstevel@tonic-gate #include <sys/model.h>
507c478bd9Sstevel@tonic-gate #include <sys/policy.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #include <sys/fs/swapnode.h>
537c478bd9Sstevel@tonic-gate #include <sys/fs/tmp.h>
547c478bd9Sstevel@tonic-gate #include <sys/fs/tmpnode.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static int tmpfsfstype;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * tmpfs vfs operations.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate static int tmpfsinit(int, char *);
627c478bd9Sstevel@tonic-gate static int tmp_mount(struct vfs *, struct vnode *,
637c478bd9Sstevel@tonic-gate 	struct mounta *, struct cred *);
647c478bd9Sstevel@tonic-gate static int tmp_unmount(struct vfs *, int, struct cred *);
657c478bd9Sstevel@tonic-gate static int tmp_root(struct vfs *, struct vnode **);
667c478bd9Sstevel@tonic-gate static int tmp_statvfs(struct vfs *, struct statvfs64 *);
677c478bd9Sstevel@tonic-gate static int tmp_vget(struct vfs *, struct vnode **, struct fid *);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate  * Loadable module wrapper
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static mntopts_t tmpfs_proto_opttbl;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static vfsdef_t vfw = {
777c478bd9Sstevel@tonic-gate 	VFSDEF_VERSION,
787c478bd9Sstevel@tonic-gate 	"tmpfs",
797c478bd9Sstevel@tonic-gate 	tmpfsinit,
8030633d6fSJerry Jelinek 	VSW_HASPROTO|VSW_CANREMOUNT|VSW_STATS|VSW_ZMOUNT,
817c478bd9Sstevel@tonic-gate 	&tmpfs_proto_opttbl
827c478bd9Sstevel@tonic-gate };
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * in-kernel mnttab options
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
887c478bd9Sstevel@tonic-gate static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate static mntopt_t tmpfs_options[] = {
917c478bd9Sstevel@tonic-gate 	/* Option name		Cancel Opt	Arg	Flags		Data */
927c478bd9Sstevel@tonic-gate 	{ MNTOPT_XATTR,		xattr_cancel,	NULL,	MO_DEFAULT,	NULL},
93*30df2103SToomas Soome 	{ MNTOPT_NOXATTR,	noxattr_cancel,	NULL,	0,		NULL},
94401bc9afSJoshua M. Clulow 	{ "size",		NULL,		"0",	MO_HASVALUE,	NULL},
95401bc9afSJoshua M. Clulow 	{ "mode",		NULL,		NULL,	MO_HASVALUE,	NULL}
967c478bd9Sstevel@tonic-gate };
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static mntopts_t tmpfs_proto_opttbl = {
1007c478bd9Sstevel@tonic-gate 	sizeof (tmpfs_options) / sizeof (mntopt_t),
1017c478bd9Sstevel@tonic-gate 	tmpfs_options
1027c478bd9Sstevel@tonic-gate };
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * Module linkage information
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate static struct modlfs modlfs = {
1087c478bd9Sstevel@tonic-gate 	&mod_fsops, "filesystem for tmpfs", &vfw
1097c478bd9Sstevel@tonic-gate };
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1127c478bd9Sstevel@tonic-gate 	MODREV_1, &modlfs, NULL
1137c478bd9Sstevel@tonic-gate };
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate int
_init()1167c478bd9Sstevel@tonic-gate _init()
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate int
_fini()1227c478bd9Sstevel@tonic-gate _fini()
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	int error;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
1277c478bd9Sstevel@tonic-gate 	if (error)
1287c478bd9Sstevel@tonic-gate 		return (error);
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * Tear down the operations vectors
1317c478bd9Sstevel@tonic-gate 	 */
1327c478bd9Sstevel@tonic-gate 	(void) vfs_freevfsops_by_type(tmpfsfstype);
1337c478bd9Sstevel@tonic-gate 	vn_freevnodeops(tmp_vnodeops);
1347c478bd9Sstevel@tonic-gate 	return (0);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1387c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate  * The following are patchable variables limiting the amount of system
1457c478bd9Sstevel@tonic-gate  * resources tmpfs can use.
1467c478bd9Sstevel@tonic-gate  *
1477c478bd9Sstevel@tonic-gate  * tmpfs_maxkmem limits the amount of kernel kmem_alloc memory
1487c478bd9Sstevel@tonic-gate  * tmpfs can use for it's data structures (e.g. tmpnodes, directory entries)
1497c478bd9Sstevel@tonic-gate  * It is not determined by setting a hard limit but rather as a percentage of
1507c478bd9Sstevel@tonic-gate  * physical memory which is determined when tmpfs is first used in the system.
1517c478bd9Sstevel@tonic-gate  *
1527c478bd9Sstevel@tonic-gate  * tmpfs_minfree is the minimum amount of swap space that tmpfs leaves for
1537c478bd9Sstevel@tonic-gate  * the rest of the system.  In other words, if the amount of free swap space
1547c478bd9Sstevel@tonic-gate  * in the system (i.e. anoninfo.ani_free) drops below tmpfs_minfree, tmpfs
1557c478bd9Sstevel@tonic-gate  * anon allocations will fail.
1567c478bd9Sstevel@tonic-gate  *
1577c478bd9Sstevel@tonic-gate  * There is also a per mount limit on the amount of swap space
1587c478bd9Sstevel@tonic-gate  * (tmount.tm_anonmax) settable via a mount option.
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate size_t tmpfs_maxkmem = 0;
1617c478bd9Sstevel@tonic-gate size_t tmpfs_minfree = 0;
1627c478bd9Sstevel@tonic-gate size_t tmp_kmemspace;		/* bytes of kernel heap used by all tmpfs */
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate static major_t tmpfs_major;
1657c478bd9Sstevel@tonic-gate static minor_t tmpfs_minor;
1667c478bd9Sstevel@tonic-gate static kmutex_t	tmpfs_minor_lock;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * initialize global tmpfs locks and such
1707c478bd9Sstevel@tonic-gate  * called when loading tmpfs module
1717c478bd9Sstevel@tonic-gate  */
1727c478bd9Sstevel@tonic-gate static int
tmpfsinit(int fstype,char * name)1737c478bd9Sstevel@tonic-gate tmpfsinit(int fstype, char *name)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	static const fs_operation_def_t tmp_vfsops_template[] = {
176aa59c4cbSrsb 		VFSNAME_MOUNT,		{ .vfs_mount = tmp_mount },
177aa59c4cbSrsb 		VFSNAME_UNMOUNT,	{ .vfs_unmount = tmp_unmount },
178aa59c4cbSrsb 		VFSNAME_ROOT,		{ .vfs_root = tmp_root },
179aa59c4cbSrsb 		VFSNAME_STATVFS,	{ .vfs_statvfs = tmp_statvfs },
180aa59c4cbSrsb 		VFSNAME_VGET,		{ .vfs_vget = tmp_vget },
181aa59c4cbSrsb 		NULL,			NULL
1827c478bd9Sstevel@tonic-gate 	};
1837c478bd9Sstevel@tonic-gate 	int error;
1847c478bd9Sstevel@tonic-gate 	extern  void    tmpfs_hash_init();
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	tmpfs_hash_init();
1877c478bd9Sstevel@tonic-gate 	tmpfsfstype = fstype;
1887c478bd9Sstevel@tonic-gate 	ASSERT(tmpfsfstype != 0);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	error = vfs_setfsops(fstype, tmp_vfsops_template, NULL);
1917c478bd9Sstevel@tonic-gate 	if (error != 0) {
1927c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "tmpfsinit: bad vfs ops template");
1937c478bd9Sstevel@tonic-gate 		return (error);
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	error = vn_make_ops(name, tmp_vnodeops_template, &tmp_vnodeops);
1977c478bd9Sstevel@tonic-gate 	if (error != 0) {
1987c478bd9Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
1997c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "tmpfsinit: bad vnode ops template");
2007c478bd9Sstevel@tonic-gate 		return (error);
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/*
2047c478bd9Sstevel@tonic-gate 	 * tmpfs_minfree doesn't need to be some function of configured
2057c478bd9Sstevel@tonic-gate 	 * swap space since it really is an absolute limit of swap space
2067c478bd9Sstevel@tonic-gate 	 * which still allows other processes to execute.
2077c478bd9Sstevel@tonic-gate 	 */
2087c478bd9Sstevel@tonic-gate 	if (tmpfs_minfree == 0) {
2097c478bd9Sstevel@tonic-gate 		/*
2107c478bd9Sstevel@tonic-gate 		 * Set if not patched
2117c478bd9Sstevel@tonic-gate 		 */
2127c478bd9Sstevel@tonic-gate 		tmpfs_minfree = btopr(TMPMINFREE);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/*
2167c478bd9Sstevel@tonic-gate 	 * The maximum amount of space tmpfs can allocate is
2177c478bd9Sstevel@tonic-gate 	 * TMPMAXPROCKMEM percent of kernel memory
2187c478bd9Sstevel@tonic-gate 	 */
2197c478bd9Sstevel@tonic-gate 	if (tmpfs_maxkmem == 0)
2207c478bd9Sstevel@tonic-gate 		tmpfs_maxkmem = MAX(PAGESIZE, kmem_maxavail() / TMPMAXFRACKMEM);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if ((tmpfs_major = getudev()) == (major_t)-1) {
2237c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "tmpfsinit: Can't get unique device number.");
2247c478bd9Sstevel@tonic-gate 		tmpfs_major = 0;
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 	mutex_init(&tmpfs_minor_lock, NULL, MUTEX_DEFAULT, NULL);
2277c478bd9Sstevel@tonic-gate 	return (0);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate static int
tmp_mount(vfs_t * vfsp,vnode_t * mvp,struct mounta * uap,cred_t * cr)231401bc9afSJoshua M. Clulow tmp_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	struct tmount *tm = NULL;
2347c478bd9Sstevel@tonic-gate 	struct tmpnode *tp;
2357c478bd9Sstevel@tonic-gate 	struct pathname dpn;
2367c478bd9Sstevel@tonic-gate 	int error;
2377c478bd9Sstevel@tonic-gate 	pgcnt_t anonmax;
2387c478bd9Sstevel@tonic-gate 	struct vattr rattr;
2397c478bd9Sstevel@tonic-gate 	int got_attrs;
240401bc9afSJoshua M. Clulow 	boolean_t mode_arg = B_FALSE;
241401bc9afSJoshua M. Clulow 	mode_t root_mode = 0777;
242401bc9afSJoshua M. Clulow 	char *argstr;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
2457c478bd9Sstevel@tonic-gate 		return (error);
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	if (mvp->v_type != VDIR)
2487c478bd9Sstevel@tonic-gate 		return (ENOTDIR);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	mutex_enter(&mvp->v_lock);
25130633d6fSJerry Jelinek 	if ((uap->flags & MS_REMOUNT) == 0 && (uap->flags & MS_OVERLAY) == 0 &&
2527c478bd9Sstevel@tonic-gate 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
2537c478bd9Sstevel@tonic-gate 		mutex_exit(&mvp->v_lock);
2547c478bd9Sstevel@tonic-gate 		return (EBUSY);
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 	mutex_exit(&mvp->v_lock);
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	/*
2597c478bd9Sstevel@tonic-gate 	 * Having the resource be anything but "swap" doesn't make sense.
2607c478bd9Sstevel@tonic-gate 	 */
261d7de0ceaSRobert Harris 	vfs_setresource(vfsp, "swap", 0);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	/*
2647c478bd9Sstevel@tonic-gate 	 * now look for options we understand...
2657c478bd9Sstevel@tonic-gate 	 */
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	/* tmpfs doesn't support read-only mounts */
2687c478bd9Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
2697c478bd9Sstevel@tonic-gate 		error = EINVAL;
2707c478bd9Sstevel@tonic-gate 		goto out;
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	/*
2747c478bd9Sstevel@tonic-gate 	 * tm_anonmax is set according to the mount arguments
2757c478bd9Sstevel@tonic-gate 	 * if any.  Otherwise, it is set to a maximum value.
2767c478bd9Sstevel@tonic-gate 	 */
277401bc9afSJoshua M. Clulow 	if (vfs_optionisset(vfsp, "size", &argstr)) {
278401bc9afSJoshua M. Clulow 		if ((error = tmp_convnum(argstr, &anonmax)) != 0)
2797c478bd9Sstevel@tonic-gate 			goto out;
2807c478bd9Sstevel@tonic-gate 	} else {
2817c478bd9Sstevel@tonic-gate 		anonmax = ULONG_MAX;
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 
284401bc9afSJoshua M. Clulow 	/*
285401bc9afSJoshua M. Clulow 	 * The "mode" mount argument allows the operator to override the
286401bc9afSJoshua M. Clulow 	 * permissions of the root of the tmpfs mount.
287401bc9afSJoshua M. Clulow 	 */
288401bc9afSJoshua M. Clulow 	if (vfs_optionisset(vfsp, "mode", &argstr)) {
289401bc9afSJoshua M. Clulow 		if ((error = tmp_convmode(argstr, &root_mode)) != 0) {
290401bc9afSJoshua M. Clulow 			goto out;
291401bc9afSJoshua M. Clulow 		}
292401bc9afSJoshua M. Clulow 		mode_arg = B_TRUE;
293401bc9afSJoshua M. Clulow 	}
294401bc9afSJoshua M. Clulow 
2957c478bd9Sstevel@tonic-gate 	if (error = pn_get(uap->dir,
2967c478bd9Sstevel@tonic-gate 	    (uap->flags & MS_SYSSPACE) ? UIO_SYSSPACE : UIO_USERSPACE, &dpn))
2977c478bd9Sstevel@tonic-gate 		goto out;
2987c478bd9Sstevel@tonic-gate 
29930633d6fSJerry Jelinek 	if (uap->flags & MS_REMOUNT) {
30030633d6fSJerry Jelinek 		tm = (struct tmount *)VFSTOTM(vfsp);
30130633d6fSJerry Jelinek 
30230633d6fSJerry Jelinek 		/*
30330633d6fSJerry Jelinek 		 * If we change the size so its less than what is currently
30430633d6fSJerry Jelinek 		 * being used, we allow that. The file system will simply be
30530633d6fSJerry Jelinek 		 * full until enough files have been removed to get below the
30630633d6fSJerry Jelinek 		 * new max.
30730633d6fSJerry Jelinek 		 */
30830633d6fSJerry Jelinek 		mutex_enter(&tm->tm_contents);
30930633d6fSJerry Jelinek 		tm->tm_anonmax = anonmax;
31030633d6fSJerry Jelinek 		mutex_exit(&tm->tm_contents);
31130633d6fSJerry Jelinek 		goto out;
31230633d6fSJerry Jelinek 	}
31330633d6fSJerry Jelinek 
3147c478bd9Sstevel@tonic-gate 	if ((tm = tmp_memalloc(sizeof (struct tmount), 0)) == NULL) {
3157c478bd9Sstevel@tonic-gate 		pn_free(&dpn);
3167c478bd9Sstevel@tonic-gate 		error = ENOMEM;
3177c478bd9Sstevel@tonic-gate 		goto out;
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	/*
3217c478bd9Sstevel@tonic-gate 	 * find an available minor device number for this mount
3227c478bd9Sstevel@tonic-gate 	 */
3237c478bd9Sstevel@tonic-gate 	mutex_enter(&tmpfs_minor_lock);
3247c478bd9Sstevel@tonic-gate 	do {
3257c478bd9Sstevel@tonic-gate 		tmpfs_minor = (tmpfs_minor + 1) & L_MAXMIN32;
3267c478bd9Sstevel@tonic-gate 		tm->tm_dev = makedevice(tmpfs_major, tmpfs_minor);
3277c478bd9Sstevel@tonic-gate 	} while (vfs_devismounted(tm->tm_dev));
3287c478bd9Sstevel@tonic-gate 	mutex_exit(&tmpfs_minor_lock);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	/*
3317c478bd9Sstevel@tonic-gate 	 * Set but don't bother entering the mutex
3327c478bd9Sstevel@tonic-gate 	 * (tmount not on mount list yet)
3337c478bd9Sstevel@tonic-gate 	 */
3347c478bd9Sstevel@tonic-gate 	mutex_init(&tm->tm_contents, NULL, MUTEX_DEFAULT, NULL);
3357c478bd9Sstevel@tonic-gate 	mutex_init(&tm->tm_renamelck, NULL, MUTEX_DEFAULT, NULL);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	tm->tm_vfsp = vfsp;
3387c478bd9Sstevel@tonic-gate 	tm->tm_anonmax = anonmax;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	vfsp->vfs_data = (caddr_t)tm;
3417c478bd9Sstevel@tonic-gate 	vfsp->vfs_fstype = tmpfsfstype;
3427c478bd9Sstevel@tonic-gate 	vfsp->vfs_dev = tm->tm_dev;
3437c478bd9Sstevel@tonic-gate 	vfsp->vfs_bsize = PAGESIZE;
3447c478bd9Sstevel@tonic-gate 	vfsp->vfs_flag |= VFS_NOTRUNC;
3457c478bd9Sstevel@tonic-gate 	vfs_make_fsid(&vfsp->vfs_fsid, tm->tm_dev, tmpfsfstype);
3467c478bd9Sstevel@tonic-gate 	tm->tm_mntpath = tmp_memalloc(dpn.pn_pathlen + 1, TMP_MUSTHAVE);
3477c478bd9Sstevel@tonic-gate 	(void) strcpy(tm->tm_mntpath, dpn.pn_path);
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * allocate and initialize root tmpnode structure
3517c478bd9Sstevel@tonic-gate 	 */
3527c478bd9Sstevel@tonic-gate 	bzero(&rattr, sizeof (struct vattr));
353401bc9afSJoshua M. Clulow 	rattr.va_mode = (mode_t)(S_IFDIR | root_mode);
3547c478bd9Sstevel@tonic-gate 	rattr.va_type = VDIR;
3557c478bd9Sstevel@tonic-gate 	rattr.va_rdev = 0;
3567c478bd9Sstevel@tonic-gate 	tp = tmp_memalloc(sizeof (struct tmpnode), TMP_MUSTHAVE);
3577c478bd9Sstevel@tonic-gate 	tmpnode_init(tm, tp, &rattr, cr);
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	/*
3607c478bd9Sstevel@tonic-gate 	 * Get the mode, uid, and gid from the underlying mount point.
3617c478bd9Sstevel@tonic-gate 	 */
3627c478bd9Sstevel@tonic-gate 	rattr.va_mask = AT_MODE|AT_UID|AT_GID;	/* Hint to getattr */
363da6c28aaSamw 	got_attrs = VOP_GETATTR(mvp, &rattr, 0, cr, NULL);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	rw_enter(&tp->tn_rwlock, RW_WRITER);
3667c478bd9Sstevel@tonic-gate 	TNTOV(tp)->v_flag |= VROOT;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	/*
3697c478bd9Sstevel@tonic-gate 	 * If the getattr succeeded, use its results.  Otherwise allow
3707c478bd9Sstevel@tonic-gate 	 * the previously set hardwired defaults to prevail.
3717c478bd9Sstevel@tonic-gate 	 */
3727c478bd9Sstevel@tonic-gate 	if (got_attrs == 0) {
373401bc9afSJoshua M. Clulow 		if (!mode_arg) {
374401bc9afSJoshua M. Clulow 			/*
375401bc9afSJoshua M. Clulow 			 * Only use the underlying mount point for the
376401bc9afSJoshua M. Clulow 			 * mode if the "mode" mount argument was not
377401bc9afSJoshua M. Clulow 			 * provided.
378401bc9afSJoshua M. Clulow 			 */
379401bc9afSJoshua M. Clulow 			tp->tn_mode = rattr.va_mode;
380401bc9afSJoshua M. Clulow 		}
3817c478bd9Sstevel@tonic-gate 		tp->tn_uid = rattr.va_uid;
3827c478bd9Sstevel@tonic-gate 		tp->tn_gid = rattr.va_gid;
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	/*
3867c478bd9Sstevel@tonic-gate 	 * initialize linked list of tmpnodes so that the back pointer of
3877c478bd9Sstevel@tonic-gate 	 * the root tmpnode always points to the last one on the list
3887c478bd9Sstevel@tonic-gate 	 * and the forward pointer of the last node is null
3897c478bd9Sstevel@tonic-gate 	 */
3907c478bd9Sstevel@tonic-gate 	tp->tn_back = tp;
3917c478bd9Sstevel@tonic-gate 	tp->tn_forw = NULL;
3927c478bd9Sstevel@tonic-gate 	tp->tn_nlink = 0;
3937c478bd9Sstevel@tonic-gate 	tm->tm_rootnode = tp;
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	tdirinit(tp, tp);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	rw_exit(&tp->tn_rwlock);
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	pn_free(&dpn);
4007c478bd9Sstevel@tonic-gate 	error = 0;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate out:
403da6c28aaSamw 	if (error == 0)
4049660e5cbSJanice Chang 		vfs_set_feature(vfsp, VFSFT_SYSATTR_VIEWS);
405da6c28aaSamw 
4067c478bd9Sstevel@tonic-gate 	return (error);
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate static int
tmp_unmount(struct vfs * vfsp,int flag,struct cred * cr)4107c478bd9Sstevel@tonic-gate tmp_unmount(struct vfs *vfsp, int flag, struct cred *cr)
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate 	struct tmount *tm = (struct tmount *)VFSTOTM(vfsp);
4137c478bd9Sstevel@tonic-gate 	struct tmpnode *tnp, *cancel;
4147c478bd9Sstevel@tonic-gate 	struct vnode	*vp;
4157c478bd9Sstevel@tonic-gate 	int error;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if ((error = secpolicy_fs_unmount(cr, vfsp)) != 0)
4187c478bd9Sstevel@tonic-gate 		return (error);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	/*
4217c478bd9Sstevel@tonic-gate 	 * forced unmount is not supported by this file system
4227c478bd9Sstevel@tonic-gate 	 * and thus, ENOTSUP, is being returned.
4237c478bd9Sstevel@tonic-gate 	 */
4247c478bd9Sstevel@tonic-gate 	if (flag & MS_FORCE)
4257c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	mutex_enter(&tm->tm_contents);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	/*
4307c478bd9Sstevel@tonic-gate 	 * If there are no open files, only the root node should have
4317c478bd9Sstevel@tonic-gate 	 * a reference count.
4327c478bd9Sstevel@tonic-gate 	 * With tm_contents held, nothing can be added or removed.
4337c478bd9Sstevel@tonic-gate 	 * There may be some dirty pages.  To prevent fsflush from
4347c478bd9Sstevel@tonic-gate 	 * disrupting the unmount, put a hold on each node while scanning.
4357c478bd9Sstevel@tonic-gate 	 * If we find a previously referenced node, undo the holds we have
4367c478bd9Sstevel@tonic-gate 	 * placed and fail EBUSY.
4377c478bd9Sstevel@tonic-gate 	 */
4387c478bd9Sstevel@tonic-gate 	tnp = tm->tm_rootnode;
4397c478bd9Sstevel@tonic-gate 	if (TNTOV(tnp)->v_count > 1) {
4407c478bd9Sstevel@tonic-gate 		mutex_exit(&tm->tm_contents);
4417c478bd9Sstevel@tonic-gate 		return (EBUSY);
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	for (tnp = tnp->tn_forw; tnp; tnp = tnp->tn_forw) {
4457c478bd9Sstevel@tonic-gate 		if ((vp = TNTOV(tnp))->v_count > 0) {
4467c478bd9Sstevel@tonic-gate 			cancel = tm->tm_rootnode->tn_forw;
4477c478bd9Sstevel@tonic-gate 			while (cancel != tnp) {
4487c478bd9Sstevel@tonic-gate 				vp = TNTOV(cancel);
4497c478bd9Sstevel@tonic-gate 				ASSERT(vp->v_count > 0);
4507c478bd9Sstevel@tonic-gate 				VN_RELE(vp);
4517c478bd9Sstevel@tonic-gate 				cancel = cancel->tn_forw;
4527c478bd9Sstevel@tonic-gate 			}
4537c478bd9Sstevel@tonic-gate 			mutex_exit(&tm->tm_contents);
4547c478bd9Sstevel@tonic-gate 			return (EBUSY);
4557c478bd9Sstevel@tonic-gate 		}
4567c478bd9Sstevel@tonic-gate 		VN_HOLD(vp);
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	/*
4607c478bd9Sstevel@tonic-gate 	 * We can drop the mutex now because no one can find this mount
4617c478bd9Sstevel@tonic-gate 	 */
4627c478bd9Sstevel@tonic-gate 	mutex_exit(&tm->tm_contents);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	/*
4657c478bd9Sstevel@tonic-gate 	 * Free all kmemalloc'd and anonalloc'd memory associated with
4667c478bd9Sstevel@tonic-gate 	 * this filesystem.  To do this, we go through the file list twice,
4677c478bd9Sstevel@tonic-gate 	 * once to remove all the directory entries, and then to remove
4687c478bd9Sstevel@tonic-gate 	 * all the files.  We do this because there is useful code in
4697c478bd9Sstevel@tonic-gate 	 * tmpnode_free which assumes that the directory entry has been
4707c478bd9Sstevel@tonic-gate 	 * removed before the file.
4717c478bd9Sstevel@tonic-gate 	 */
4727c478bd9Sstevel@tonic-gate 	/*
4737c478bd9Sstevel@tonic-gate 	 * Remove all directory entries
4747c478bd9Sstevel@tonic-gate 	 */
4757c478bd9Sstevel@tonic-gate 	for (tnp = tm->tm_rootnode; tnp; tnp = tnp->tn_forw) {
4767c478bd9Sstevel@tonic-gate 		rw_enter(&tnp->tn_rwlock, RW_WRITER);
4777c478bd9Sstevel@tonic-gate 		if (tnp->tn_type == VDIR)
4787c478bd9Sstevel@tonic-gate 			tdirtrunc(tnp);
4797c478bd9Sstevel@tonic-gate 		if (tnp->tn_vnode->v_flag & V_XATTRDIR) {
4807c478bd9Sstevel@tonic-gate 			/*
4817c478bd9Sstevel@tonic-gate 			 * Account for implicit attrdir reference.
4827c478bd9Sstevel@tonic-gate 			 */
4837c478bd9Sstevel@tonic-gate 			ASSERT(tnp->tn_nlink > 0);
4847c478bd9Sstevel@tonic-gate 			DECR_COUNT(&tnp->tn_nlink, &tnp->tn_tlock);
4857c478bd9Sstevel@tonic-gate 		}
4867c478bd9Sstevel@tonic-gate 		rw_exit(&tnp->tn_rwlock);
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	ASSERT(tm->tm_rootnode);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	/*
4927c478bd9Sstevel@tonic-gate 	 * All links are gone, v_count is keeping nodes in place.
4937c478bd9Sstevel@tonic-gate 	 * VN_RELE should make the node disappear, unless somebody
4947c478bd9Sstevel@tonic-gate 	 * is holding pages against it.  Nap and retry until it disappears.
4957c478bd9Sstevel@tonic-gate 	 *
4967c478bd9Sstevel@tonic-gate 	 * We re-acquire the lock to prevent others who have a HOLD on
4977c478bd9Sstevel@tonic-gate 	 * a tmpnode via its pages or anon slots from blowing it away
4987c478bd9Sstevel@tonic-gate 	 * (in tmp_inactive) while we're trying to get to it here. Once
4997c478bd9Sstevel@tonic-gate 	 * we have a HOLD on it we know it'll stick around.
5007c478bd9Sstevel@tonic-gate 	 *
5017c478bd9Sstevel@tonic-gate 	 */
5027c478bd9Sstevel@tonic-gate 	mutex_enter(&tm->tm_contents);
5037c478bd9Sstevel@tonic-gate 	/*
5047c478bd9Sstevel@tonic-gate 	 * Remove all the files (except the rootnode) backwards.
5057c478bd9Sstevel@tonic-gate 	 */
5067c478bd9Sstevel@tonic-gate 	while ((tnp = tm->tm_rootnode->tn_back) != tm->tm_rootnode) {
5077c478bd9Sstevel@tonic-gate 		mutex_exit(&tm->tm_contents);
5087c478bd9Sstevel@tonic-gate 		/*
5097c478bd9Sstevel@tonic-gate 		 * Inhibit tmp_inactive from touching attribute directory
5107c478bd9Sstevel@tonic-gate 		 * as all nodes will be released here.
5117c478bd9Sstevel@tonic-gate 		 * Note we handled the link count in pass 2 above.
5127c478bd9Sstevel@tonic-gate 		 */
5137c478bd9Sstevel@tonic-gate 		rw_enter(&tnp->tn_rwlock, RW_WRITER);
5147c478bd9Sstevel@tonic-gate 		tnp->tn_xattrdp = NULL;
5157c478bd9Sstevel@tonic-gate 		rw_exit(&tnp->tn_rwlock);
5167c478bd9Sstevel@tonic-gate 		vp = TNTOV(tnp);
5177c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
5187c478bd9Sstevel@tonic-gate 		mutex_enter(&tm->tm_contents);
5197c478bd9Sstevel@tonic-gate 		/*
5207c478bd9Sstevel@tonic-gate 		 * It's still there after the RELE. Someone else like pageout
5217c478bd9Sstevel@tonic-gate 		 * has a hold on it so wait a bit and then try again - we know
5227c478bd9Sstevel@tonic-gate 		 * they'll give it up soon.
5237c478bd9Sstevel@tonic-gate 		 */
5247c478bd9Sstevel@tonic-gate 		if (tnp == tm->tm_rootnode->tn_back) {
5257c478bd9Sstevel@tonic-gate 			VN_HOLD(vp);
5267c478bd9Sstevel@tonic-gate 			mutex_exit(&tm->tm_contents);
5277c478bd9Sstevel@tonic-gate 			delay(hz / 4);
5287c478bd9Sstevel@tonic-gate 			mutex_enter(&tm->tm_contents);
5297c478bd9Sstevel@tonic-gate 		}
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate 	mutex_exit(&tm->tm_contents);
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	tm->tm_rootnode->tn_xattrdp = NULL;
5347c478bd9Sstevel@tonic-gate 	VN_RELE(TNTOV(tm->tm_rootnode));
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	ASSERT(tm->tm_mntpath);
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	tmp_memfree(tm->tm_mntpath, strlen(tm->tm_mntpath) + 1);
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	ASSERT(tm->tm_anonmem == 0);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	mutex_destroy(&tm->tm_contents);
5437c478bd9Sstevel@tonic-gate 	mutex_destroy(&tm->tm_renamelck);
5447c478bd9Sstevel@tonic-gate 	tmp_memfree(tm, sizeof (struct tmount));
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	return (0);
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate /*
5507c478bd9Sstevel@tonic-gate  * return root tmpnode for given vnode
5517c478bd9Sstevel@tonic-gate  */
5527c478bd9Sstevel@tonic-gate static int
tmp_root(struct vfs * vfsp,struct vnode ** vpp)5537c478bd9Sstevel@tonic-gate tmp_root(struct vfs *vfsp, struct vnode **vpp)
5547c478bd9Sstevel@tonic-gate {
5557c478bd9Sstevel@tonic-gate 	struct tmount *tm = (struct tmount *)VFSTOTM(vfsp);
5567c478bd9Sstevel@tonic-gate 	struct tmpnode *tp = tm->tm_rootnode;
5577c478bd9Sstevel@tonic-gate 	struct vnode *vp;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	ASSERT(tp);
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	vp = TNTOV(tp);
5627c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
5637c478bd9Sstevel@tonic-gate 	*vpp = vp;
5647c478bd9Sstevel@tonic-gate 	return (0);
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate static int
tmp_statvfs(struct vfs * vfsp,struct statvfs64 * sbp)5687c478bd9Sstevel@tonic-gate tmp_statvfs(struct vfs *vfsp, struct statvfs64 *sbp)
5697c478bd9Sstevel@tonic-gate {
5707c478bd9Sstevel@tonic-gate 	struct tmount	*tm = (struct tmount *)VFSTOTM(vfsp);
5717c478bd9Sstevel@tonic-gate 	ulong_t	blocks;
5727c478bd9Sstevel@tonic-gate 	dev32_t d32;
5733d2ef5a6S 	zoneid_t eff_zid;
5743d2ef5a6S 	struct zone *zp;
5753d2ef5a6S 
5763d2ef5a6S 	/*
5773d2ef5a6S 	 * The file system may have been mounted by the global zone on
5783d2ef5a6S 	 * behalf of the non-global zone.  In that case, the tmount zone_id
5793d2ef5a6S 	 * will be the global zone.  We still want to show the swap cap inside
5803d2ef5a6S 	 * the zone in this case, even though the file system was mounted by
5813d2ef5a6S 	 * the global zone.
5823d2ef5a6S 	 */
5833d2ef5a6S 	if (curproc->p_zone->zone_id != GLOBAL_ZONEUNIQID)
5843d2ef5a6S 		zp = curproc->p_zone;
5853d2ef5a6S 	else
5863d2ef5a6S 		zp = tm->tm_vfsp->vfs_zone;
5873d2ef5a6S 
5883d2ef5a6S 	if (zp == NULL)
5893d2ef5a6S 		eff_zid = GLOBAL_ZONEUNIQID;
5903d2ef5a6S 	else
5913d2ef5a6S 		eff_zid = zp->zone_id;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	sbp->f_bsize = PAGESIZE;
5947c478bd9Sstevel@tonic-gate 	sbp->f_frsize = PAGESIZE;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	/*
5977c478bd9Sstevel@tonic-gate 	 * Find the amount of available physical and memory swap
5987c478bd9Sstevel@tonic-gate 	 */
5997c478bd9Sstevel@tonic-gate 	mutex_enter(&anoninfo_lock);
6007c478bd9Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
6017c478bd9Sstevel@tonic-gate 	blocks = (ulong_t)CURRENT_TOTAL_AVAILABLE_SWAP;
6027c478bd9Sstevel@tonic-gate 	mutex_exit(&anoninfo_lock);
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	/*
6057c478bd9Sstevel@tonic-gate 	 * If tm_anonmax for this mount is less than the available swap space
6067c478bd9Sstevel@tonic-gate 	 * (minus the amount tmpfs can't use), use that instead
6077c478bd9Sstevel@tonic-gate 	 */
6087c478bd9Sstevel@tonic-gate 	if (blocks > tmpfs_minfree)
6097c478bd9Sstevel@tonic-gate 		sbp->f_bfree = MIN(blocks - tmpfs_minfree,
6107c478bd9Sstevel@tonic-gate 		    tm->tm_anonmax - tm->tm_anonmem);
6117c478bd9Sstevel@tonic-gate 	else
6127c478bd9Sstevel@tonic-gate 		sbp->f_bfree = 0;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	sbp->f_bavail = sbp->f_bfree;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	/*
6177c478bd9Sstevel@tonic-gate 	 * Total number of blocks is what's available plus what's been used
6187c478bd9Sstevel@tonic-gate 	 */
6197c478bd9Sstevel@tonic-gate 	sbp->f_blocks = (fsblkcnt64_t)(sbp->f_bfree + tm->tm_anonmem);
6207c478bd9Sstevel@tonic-gate 
6213d2ef5a6S 	if (eff_zid != GLOBAL_ZONEUNIQID &&
6223d2ef5a6S 	    zp->zone_max_swap_ctl != UINT64_MAX) {
6233d2ef5a6S 		/*
6243d2ef5a6S 		 * If the fs is used by a non-global zone with a swap cap,
6253d2ef5a6S 		 * then report the capped size.
6263d2ef5a6S 		 */
6273d2ef5a6S 		rctl_qty_t cap, used;
6283d2ef5a6S 		pgcnt_t pgcap, pgused;
6293d2ef5a6S 
6303d2ef5a6S 		mutex_enter(&zp->zone_mem_lock);
6313d2ef5a6S 		cap = zp->zone_max_swap_ctl;
6323d2ef5a6S 		used = zp->zone_max_swap;
6333d2ef5a6S 		mutex_exit(&zp->zone_mem_lock);
6343d2ef5a6S 
6353d2ef5a6S 		pgcap = btop(cap);
6363d2ef5a6S 		pgused = btop(used);
6373d2ef5a6S 
6383d2ef5a6S 		sbp->f_bfree = MIN(pgcap - pgused, sbp->f_bfree);
6393d2ef5a6S 		sbp->f_bavail = sbp->f_bfree;
6403d2ef5a6S 		sbp->f_blocks = MIN(pgcap, sbp->f_blocks);
6413d2ef5a6S 	}
6423d2ef5a6S 
6437c478bd9Sstevel@tonic-gate 	/*
6447c478bd9Sstevel@tonic-gate 	 * The maximum number of files available is approximately the number
6457c478bd9Sstevel@tonic-gate 	 * of tmpnodes we can allocate from the remaining kernel memory
6467c478bd9Sstevel@tonic-gate 	 * available to tmpfs.  This is fairly inaccurate since it doesn't
6477c478bd9Sstevel@tonic-gate 	 * take into account the names stored in the directory entries.
6487c478bd9Sstevel@tonic-gate 	 */
6497c478bd9Sstevel@tonic-gate 	if (tmpfs_maxkmem > tmp_kmemspace)
6507c478bd9Sstevel@tonic-gate 		sbp->f_ffree = (tmpfs_maxkmem - tmp_kmemspace) /
6517c478bd9Sstevel@tonic-gate 		    (sizeof (struct tmpnode) + sizeof (struct tdirent));
6527c478bd9Sstevel@tonic-gate 	else
6537c478bd9Sstevel@tonic-gate 		sbp->f_ffree = 0;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	sbp->f_files = tmpfs_maxkmem /
6567c478bd9Sstevel@tonic-gate 	    (sizeof (struct tmpnode) + sizeof (struct tdirent));
6577c478bd9Sstevel@tonic-gate 	sbp->f_favail = (fsfilcnt64_t)(sbp->f_ffree);
6587c478bd9Sstevel@tonic-gate 	(void) cmpldev(&d32, vfsp->vfs_dev);
6597c478bd9Sstevel@tonic-gate 	sbp->f_fsid = d32;
6607c478bd9Sstevel@tonic-gate 	(void) strcpy(sbp->f_basetype, vfssw[tmpfsfstype].vsw_name);
6617c478bd9Sstevel@tonic-gate 	(void) strncpy(sbp->f_fstr, tm->tm_mntpath, sizeof (sbp->f_fstr));
6627c478bd9Sstevel@tonic-gate 	/*
6637c478bd9Sstevel@tonic-gate 	 * ensure null termination
6647c478bd9Sstevel@tonic-gate 	 */
6657c478bd9Sstevel@tonic-gate 	sbp->f_fstr[sizeof (sbp->f_fstr) - 1] = '\0';
6667c478bd9Sstevel@tonic-gate 	sbp->f_flag = vf_to_stf(vfsp->vfs_flag);
6677c478bd9Sstevel@tonic-gate 	sbp->f_namemax = MAXNAMELEN - 1;
6687c478bd9Sstevel@tonic-gate 	return (0);
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate static int
tmp_vget(struct vfs * vfsp,struct vnode ** vpp,struct fid * fidp)6727c478bd9Sstevel@tonic-gate tmp_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp)
6737c478bd9Sstevel@tonic-gate {
6747c478bd9Sstevel@tonic-gate 	struct tfid *tfid;
6757c478bd9Sstevel@tonic-gate 	struct tmount *tm = (struct tmount *)VFSTOTM(vfsp);
6767c478bd9Sstevel@tonic-gate 	struct tmpnode *tp = NULL;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	tfid = (struct tfid *)fidp;
6797c478bd9Sstevel@tonic-gate 	*vpp = NULL;
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	mutex_enter(&tm->tm_contents);
6827c478bd9Sstevel@tonic-gate 	for (tp = tm->tm_rootnode; tp; tp = tp->tn_forw) {
6837c478bd9Sstevel@tonic-gate 		mutex_enter(&tp->tn_tlock);
6847c478bd9Sstevel@tonic-gate 		if (tp->tn_nodeid == tfid->tfid_ino) {
6857c478bd9Sstevel@tonic-gate 			/*
6867c478bd9Sstevel@tonic-gate 			 * If the gen numbers don't match we know the
6877c478bd9Sstevel@tonic-gate 			 * file won't be found since only one tmpnode
6887c478bd9Sstevel@tonic-gate 			 * can have this number at a time.
6897c478bd9Sstevel@tonic-gate 			 */
6907c478bd9Sstevel@tonic-gate 			if (tp->tn_gen != tfid->tfid_gen || tp->tn_nlink == 0) {
6917c478bd9Sstevel@tonic-gate 				mutex_exit(&tp->tn_tlock);
6927c478bd9Sstevel@tonic-gate 				mutex_exit(&tm->tm_contents);
6937c478bd9Sstevel@tonic-gate 				return (0);
6947c478bd9Sstevel@tonic-gate 			}
6957c478bd9Sstevel@tonic-gate 			*vpp = (struct vnode *)TNTOV(tp);
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 			VN_HOLD(*vpp);
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 			if ((tp->tn_mode & S_ISVTX) &&
7007c478bd9Sstevel@tonic-gate 			    !(tp->tn_mode & (S_IXUSR | S_IFDIR))) {
7017c478bd9Sstevel@tonic-gate 				mutex_enter(&(*vpp)->v_lock);
7027c478bd9Sstevel@tonic-gate 				(*vpp)->v_flag |= VISSWAP;
7037c478bd9Sstevel@tonic-gate 				mutex_exit(&(*vpp)->v_lock);
7047c478bd9Sstevel@tonic-gate 			}
7057c478bd9Sstevel@tonic-gate 			mutex_exit(&tp->tn_tlock);
7067c478bd9Sstevel@tonic-gate 			mutex_exit(&tm->tm_contents);
7077c478bd9Sstevel@tonic-gate 			return (0);
7087c478bd9Sstevel@tonic-gate 		}
7097c478bd9Sstevel@tonic-gate 		mutex_exit(&tp->tn_tlock);
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 	mutex_exit(&tm->tm_contents);
7127c478bd9Sstevel@tonic-gate 	return (0);
7137c478bd9Sstevel@tonic-gate }
714