xref: /titanic_52/usr/src/uts/common/fs/swapfs/swap_subr.c (revision 604f3914b253f1fce97b83ce72a612afbb2dc4ca)
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
5aa59c4cbSrsb  * Common Development and Distribution License (the "License").
6aa59c4cbSrsb  * 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 /*
22*604f3914SVijay Balakrishna, SG-RPE  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
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/systm.h>
297c478bd9Sstevel@tonic-gate #include <sys/errno.h>
307c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
317c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
32aa59c4cbSrsb #include <sys/vfs_opreg.h>
337c478bd9Sstevel@tonic-gate #include <sys/swap.h>
347c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
357c478bd9Sstevel@tonic-gate #include <sys/buf.h>
367c478bd9Sstevel@tonic-gate #include <sys/callb.h>
377c478bd9Sstevel@tonic-gate #include <sys/debug.h>
387c478bd9Sstevel@tonic-gate #include <vm/seg.h>
397c478bd9Sstevel@tonic-gate #include <sys/fs/swapnode.h>
407c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
417c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
427c478bd9Sstevel@tonic-gate #include <sys/mem_config.h>
437c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate extern const fs_operation_def_t swap_vnodeops_template[];
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * swapfs_minfree is the amount of physical memory (actually remaining
497c478bd9Sstevel@tonic-gate  * availrmem) that we want to keep free for the rest of the system.  This
507c478bd9Sstevel@tonic-gate  * means that swapfs can only grow to availrmem - swapfs_minfree.  This
517c478bd9Sstevel@tonic-gate  * can be set as just constant value or a certain percentage of installed
527c478bd9Sstevel@tonic-gate  * physical memory. It is set in swapinit().
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  * Users who want to change the amount of memory that can be used as swap
557c478bd9Sstevel@tonic-gate  * space should do so by setting swapfs_desfree at boot time,
567c478bd9Sstevel@tonic-gate  * not swapfs_minfree.
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate pgcnt_t swapfs_desfree = 0;
607c478bd9Sstevel@tonic-gate pgcnt_t swapfs_minfree = 0;
617c478bd9Sstevel@tonic-gate pgcnt_t swapfs_reserve = 0;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #ifdef SWAPFS_DEBUG
647c478bd9Sstevel@tonic-gate int swapfs_debug;
657c478bd9Sstevel@tonic-gate #endif /* SWAPFS_DEBUG */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate static int swapfs_vpcount;
697c478bd9Sstevel@tonic-gate static kmutex_t swapfs_lock;
707c478bd9Sstevel@tonic-gate static struct async_reqs *sw_ar, *sw_pendlist, *sw_freelist;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static struct vnode **swap_vnodes;	/* ptr's to swap vnodes */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static void swap_init_mem_config(void);
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static pgcnt_t initial_swapfs_desfree;
777c478bd9Sstevel@tonic-gate static pgcnt_t initial_swapfs_minfree;
787c478bd9Sstevel@tonic-gate static pgcnt_t initial_swapfs_reserve;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate static int swap_sync(struct vfs *vfsp, short flag, struct cred *cr);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static void
837c478bd9Sstevel@tonic-gate swapfs_recalc_save_initial(void)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	initial_swapfs_desfree = swapfs_desfree;
867c478bd9Sstevel@tonic-gate 	initial_swapfs_minfree = swapfs_minfree;
877c478bd9Sstevel@tonic-gate 	initial_swapfs_reserve = swapfs_reserve;
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate static int
917c478bd9Sstevel@tonic-gate swapfs_recalc(pgcnt_t pgs)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	pgcnt_t new_swapfs_desfree;
947c478bd9Sstevel@tonic-gate 	pgcnt_t new_swapfs_minfree;
957c478bd9Sstevel@tonic-gate 	pgcnt_t new_swapfs_reserve;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	new_swapfs_desfree = initial_swapfs_desfree;
987c478bd9Sstevel@tonic-gate 	new_swapfs_minfree = initial_swapfs_minfree;
997c478bd9Sstevel@tonic-gate 	new_swapfs_reserve = initial_swapfs_reserve;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	if (new_swapfs_desfree == 0)
1027c478bd9Sstevel@tonic-gate 		new_swapfs_desfree = btopr(7 * 512 * 1024); /* 3-1/2Mb */;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (new_swapfs_minfree == 0) {
1057c478bd9Sstevel@tonic-gate 		/*
1067c478bd9Sstevel@tonic-gate 		 * We set this lower than we'd like here, 2Mb, because we
1077c478bd9Sstevel@tonic-gate 		 * always boot on swapfs. It's up to a safer value,
1087c478bd9Sstevel@tonic-gate 		 * swapfs_desfree, when/if we add physical swap devices
1097c478bd9Sstevel@tonic-gate 		 * in swapadd(). Users who want to change the amount of
1107c478bd9Sstevel@tonic-gate 		 * memory that can be used as swap space should do so by
1117c478bd9Sstevel@tonic-gate 		 * setting swapfs_desfree at boot time, not swapfs_minfree.
1127c478bd9Sstevel@tonic-gate 		 * However, swapfs_minfree is tunable by install as a
1137c478bd9Sstevel@tonic-gate 		 * workaround for bugid 1147463.
1147c478bd9Sstevel@tonic-gate 		 */
1157c478bd9Sstevel@tonic-gate 		new_swapfs_minfree = MAX(btopr(2 * 1024 * 1024), pgs >> 3);
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	/*
1197c478bd9Sstevel@tonic-gate 	 * priv processes can reserve memory as swap as long as availrmem
1207c478bd9Sstevel@tonic-gate 	 * remains greater than swapfs_minfree; in the case of non-priv
1217c478bd9Sstevel@tonic-gate 	 * processes, memory can be reserved as swap only if availrmem
1227c478bd9Sstevel@tonic-gate 	 * doesn't fall below (swapfs_minfree + swapfs_reserve). Thus,
1237c478bd9Sstevel@tonic-gate 	 * swapfs_reserve amount of memswap is not available to non-priv
1247c478bd9Sstevel@tonic-gate 	 * processes. This protects daemons such as automounter dying
1257c478bd9Sstevel@tonic-gate 	 * as a result of application processes eating away almost entire
1267c478bd9Sstevel@tonic-gate 	 * membased swap. This safeguard becomes useless if apps are run
1277c478bd9Sstevel@tonic-gate 	 * with root access.
1287c478bd9Sstevel@tonic-gate 	 *
1297c478bd9Sstevel@tonic-gate 	 * set swapfs_reserve to a minimum of 4Mb or 1/128 of physmem whichever
1307c478bd9Sstevel@tonic-gate 	 * is greater up to the limit of 128 MB.
1317c478bd9Sstevel@tonic-gate 	 */
1327c478bd9Sstevel@tonic-gate 	if (new_swapfs_reserve == 0)
1337c478bd9Sstevel@tonic-gate 		new_swapfs_reserve = MIN(btopr(128 * 1024 * 1024),
1347c478bd9Sstevel@tonic-gate 		    MAX(btopr(4 * 1024 * 1024), pgs >> 7));
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	/* Test basic numeric viability. */
1377c478bd9Sstevel@tonic-gate 	if (new_swapfs_minfree > pgs)
1387c478bd9Sstevel@tonic-gate 		return (0);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	/* Equivalent test to anon_resvmem() check. */
1417c478bd9Sstevel@tonic-gate 	if (availrmem < new_swapfs_minfree) {
1427c478bd9Sstevel@tonic-gate 		/*
1437c478bd9Sstevel@tonic-gate 		 * If ism pages are being used, then there must be agreement
1447c478bd9Sstevel@tonic-gate 		 * between these two policies.
1457c478bd9Sstevel@tonic-gate 		 */
1467c478bd9Sstevel@tonic-gate 		if ((availrmem > segspt_minfree) && (segspt_minfree > 0)) {
1477c478bd9Sstevel@tonic-gate 			new_swapfs_minfree = segspt_minfree;
1487c478bd9Sstevel@tonic-gate 		} else {
1497c478bd9Sstevel@tonic-gate 			return (0);
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	swapfs_desfree = new_swapfs_desfree;
1547c478bd9Sstevel@tonic-gate 	swapfs_minfree = new_swapfs_minfree;
1557c478bd9Sstevel@tonic-gate 	swapfs_reserve = new_swapfs_reserve;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	return (1);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
1617c478bd9Sstevel@tonic-gate int
1627c478bd9Sstevel@tonic-gate swapinit(int fstype, char *name)
1637c478bd9Sstevel@tonic-gate {							/* reserve for mp */
1647c478bd9Sstevel@tonic-gate 	ssize_t sw_freelist_size = klustsize / PAGESIZE * 2;
1657c478bd9Sstevel@tonic-gate 	int i, error;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	static const fs_operation_def_t swap_vfsops[] = {
168aa59c4cbSrsb 		VFSNAME_SYNC, { .vfs_sync = swap_sync },
1697c478bd9Sstevel@tonic-gate 		NULL, NULL
1707c478bd9Sstevel@tonic-gate 	};
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	SWAPFS_PRINT(SWAP_SUBR, "swapinit\n", 0, 0, 0, 0, 0);
1737c478bd9Sstevel@tonic-gate 	mutex_init(&swapfs_lock, NULL, MUTEX_DEFAULT, NULL);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	swap_vnodes = kmem_zalloc(MAX_SWAP_VNODES * sizeof (struct vnode *),
1767c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	swapfs_recalc_save_initial();
1797c478bd9Sstevel@tonic-gate 	if (!swapfs_recalc(physmem))
1807c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "swapfs_minfree(%lu) > physmem(%lu)",
1817c478bd9Sstevel@tonic-gate 		    swapfs_minfree, physmem);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/*
1847c478bd9Sstevel@tonic-gate 	 * Arrange for a callback on memory size change.
1857c478bd9Sstevel@tonic-gate 	 */
1867c478bd9Sstevel@tonic-gate 	swap_init_mem_config();
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	sw_ar = (struct async_reqs *)
1897c478bd9Sstevel@tonic-gate 	    kmem_zalloc(sw_freelist_size*sizeof (struct async_reqs), KM_SLEEP);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	error = vfs_setfsops(fstype, swap_vfsops, NULL);
1927c478bd9Sstevel@tonic-gate 	if (error != 0) {
1937c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "swapinit: bad vfs ops template");
1947c478bd9Sstevel@tonic-gate 		return (error);
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	error = vn_make_ops(name, swap_vnodeops_template, &swap_vnodeops);
1987c478bd9Sstevel@tonic-gate 	if (error != 0) {
1997c478bd9Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
2007c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "swapinit: bad vnode ops template");
2017c478bd9Sstevel@tonic-gate 		return (error);
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 	sw_freelist = sw_ar;
2047c478bd9Sstevel@tonic-gate 	for (i = 0; i < sw_freelist_size - 1; i++)
2057c478bd9Sstevel@tonic-gate 		sw_ar[i].a_next = &sw_ar[i + 1];
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	return (0);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /*
2117c478bd9Sstevel@tonic-gate  * Get a swapfs vnode corresponding to the specified identifier.
2127c478bd9Sstevel@tonic-gate  */
2137c478bd9Sstevel@tonic-gate struct vnode *
2147c478bd9Sstevel@tonic-gate swapfs_getvp(ulong_t vidx)
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate 	struct vnode *vp;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	vp = swap_vnodes[vidx];
2197c478bd9Sstevel@tonic-gate 	if (vp) {
2207c478bd9Sstevel@tonic-gate 		return (vp);
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	mutex_enter(&swapfs_lock);
2247c478bd9Sstevel@tonic-gate 	vp = swap_vnodes[vidx];
2257c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
2267c478bd9Sstevel@tonic-gate 		vp = vn_alloc(KM_SLEEP);
2277c478bd9Sstevel@tonic-gate 		vn_setops(vp, swap_vnodeops);
2287c478bd9Sstevel@tonic-gate 		vp->v_type = VREG;
2297c478bd9Sstevel@tonic-gate 		vp->v_flag |= (VISSWAP|VISSWAPFS);
2307c478bd9Sstevel@tonic-gate 		swap_vnodes[vidx] = vp;
2317c478bd9Sstevel@tonic-gate 		swapfs_vpcount++;
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 	mutex_exit(&swapfs_lock);
2347c478bd9Sstevel@tonic-gate 	return (vp);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate int swap_lo;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2407c478bd9Sstevel@tonic-gate static int
2417c478bd9Sstevel@tonic-gate swap_sync(struct vfs *vfsp, short flag, struct cred *cr)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	struct vnode *vp;
2447c478bd9Sstevel@tonic-gate 	int i;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	if (!(flag & SYNC_ALL))
2477c478bd9Sstevel@tonic-gate 		return (1);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/*
2507c478bd9Sstevel@tonic-gate 	 * assumes that we are the only one left to access this so that
2517c478bd9Sstevel@tonic-gate 	 * no need to use swapfs_lock (since it's staticly defined)
2527c478bd9Sstevel@tonic-gate 	 */
2537c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_SWAP_VNODES; i++) {
2547c478bd9Sstevel@tonic-gate 		vp = swap_vnodes[i];
2557c478bd9Sstevel@tonic-gate 		if (vp) {
2567c478bd9Sstevel@tonic-gate 			VN_HOLD(vp);
2577c478bd9Sstevel@tonic-gate 			(void) VOP_PUTPAGE(vp, (offset_t)0, 0,
258da6c28aaSamw 			    (B_ASYNC | B_FREE), kcred, NULL);
2597c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
2607c478bd9Sstevel@tonic-gate 		}
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 	return (0);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate extern int sw_pending_size;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate /*
2687c478bd9Sstevel@tonic-gate  * Take an async request off the pending queue
2697c478bd9Sstevel@tonic-gate  */
2707c478bd9Sstevel@tonic-gate struct async_reqs *
2717c478bd9Sstevel@tonic-gate sw_getreq()
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	struct async_reqs *arg;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	mutex_enter(&swapfs_lock);
2767c478bd9Sstevel@tonic-gate 	arg = sw_pendlist;
2777c478bd9Sstevel@tonic-gate 	if (arg) {
2787c478bd9Sstevel@tonic-gate 		sw_pendlist = arg->a_next;
2797c478bd9Sstevel@tonic-gate 		arg->a_next = NULL;
2807c478bd9Sstevel@tonic-gate 		sw_pending_size -= PAGESIZE;
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 	ASSERT(sw_pending_size >= 0);
2837c478bd9Sstevel@tonic-gate 	mutex_exit(&swapfs_lock);
2847c478bd9Sstevel@tonic-gate 	return (arg);
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate /*
2887c478bd9Sstevel@tonic-gate  * Put an async request on the pending queue
2897c478bd9Sstevel@tonic-gate  */
2907c478bd9Sstevel@tonic-gate void
2917c478bd9Sstevel@tonic-gate sw_putreq(struct async_reqs *arg)
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate 	/* Hold onto it */
2947c478bd9Sstevel@tonic-gate 	VN_HOLD(arg->a_vp);
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	mutex_enter(&swapfs_lock);
2977c478bd9Sstevel@tonic-gate 	arg->a_next = sw_pendlist;
2987c478bd9Sstevel@tonic-gate 	sw_pendlist = arg;
2997c478bd9Sstevel@tonic-gate 	sw_pending_size += PAGESIZE;
3007c478bd9Sstevel@tonic-gate 	mutex_exit(&swapfs_lock);
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate /*
3047c478bd9Sstevel@tonic-gate  * Put an async request back on the pending queue
3057c478bd9Sstevel@tonic-gate  */
3067c478bd9Sstevel@tonic-gate void
3077c478bd9Sstevel@tonic-gate sw_putbackreq(struct async_reqs *arg)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	mutex_enter(&swapfs_lock);
3107c478bd9Sstevel@tonic-gate 	arg->a_next = sw_pendlist;
3117c478bd9Sstevel@tonic-gate 	sw_pendlist = arg;
3127c478bd9Sstevel@tonic-gate 	sw_pending_size += PAGESIZE;
3137c478bd9Sstevel@tonic-gate 	mutex_exit(&swapfs_lock);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate /*
3177c478bd9Sstevel@tonic-gate  * Take an async request structure off the free list
3187c478bd9Sstevel@tonic-gate  */
3197c478bd9Sstevel@tonic-gate struct async_reqs *
3207c478bd9Sstevel@tonic-gate sw_getfree()
3217c478bd9Sstevel@tonic-gate {
3227c478bd9Sstevel@tonic-gate 	struct async_reqs *arg;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	mutex_enter(&swapfs_lock);
3257c478bd9Sstevel@tonic-gate 	arg = sw_freelist;
3267c478bd9Sstevel@tonic-gate 	if (arg) {
3277c478bd9Sstevel@tonic-gate 		sw_freelist = arg->a_next;
3287c478bd9Sstevel@tonic-gate 		arg->a_next = NULL;
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 	mutex_exit(&swapfs_lock);
3317c478bd9Sstevel@tonic-gate 	return (arg);
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate /*
3357c478bd9Sstevel@tonic-gate  * Put an async request structure on the free list
3367c478bd9Sstevel@tonic-gate  */
3377c478bd9Sstevel@tonic-gate void
3387c478bd9Sstevel@tonic-gate sw_putfree(struct async_reqs *arg)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	/* Release our hold - should have locked the page by now */
3417c478bd9Sstevel@tonic-gate 	VN_RELE(arg->a_vp);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	mutex_enter(&swapfs_lock);
3447c478bd9Sstevel@tonic-gate 	arg->a_next = sw_freelist;
3457c478bd9Sstevel@tonic-gate 	sw_freelist = arg;
3467c478bd9Sstevel@tonic-gate 	mutex_exit(&swapfs_lock);
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate static pgcnt_t swapfs_pending_delete;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3527c478bd9Sstevel@tonic-gate static void
3537c478bd9Sstevel@tonic-gate swap_mem_config_post_add(
3547c478bd9Sstevel@tonic-gate 	void *arg,
3557c478bd9Sstevel@tonic-gate 	pgcnt_t delta_swaps)
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate 	(void) swapfs_recalc(physmem - swapfs_pending_delete);
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3617c478bd9Sstevel@tonic-gate static int
3627c478bd9Sstevel@tonic-gate swap_mem_config_pre_del(
3637c478bd9Sstevel@tonic-gate 	void *arg,
3647c478bd9Sstevel@tonic-gate 	pgcnt_t delta_swaps)
3657c478bd9Sstevel@tonic-gate {
3667c478bd9Sstevel@tonic-gate 	pgcnt_t nv;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	nv = atomic_add_long_nv(&swapfs_pending_delete, (spgcnt_t)delta_swaps);
3697c478bd9Sstevel@tonic-gate 	if (!swapfs_recalc(physmem - nv)) {
3707c478bd9Sstevel@tonic-gate 		/*
3717c478bd9Sstevel@tonic-gate 		 * Tidy-up is done by the call to post_del which
3727c478bd9Sstevel@tonic-gate 		 * is always made.
3737c478bd9Sstevel@tonic-gate 		 */
374*604f3914SVijay Balakrishna, SG-RPE 		cmn_err(CE_NOTE, "Memory operation refused to ensure system "
375*604f3914SVijay Balakrishna, SG-RPE 		    "doesn't deadlock due to excessive consumption by swapfs.");
3767c478bd9Sstevel@tonic-gate 		return (EBUSY);
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 	return (0);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3827c478bd9Sstevel@tonic-gate static void
3837c478bd9Sstevel@tonic-gate swap_mem_config_post_del(
3847c478bd9Sstevel@tonic-gate 	void *arg,
3857c478bd9Sstevel@tonic-gate 	pgcnt_t delta_swaps,
3867c478bd9Sstevel@tonic-gate 	int cancelled)
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	pgcnt_t nv;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	nv = atomic_add_long_nv(&swapfs_pending_delete, -(spgcnt_t)delta_swaps);
3917c478bd9Sstevel@tonic-gate 	(void) swapfs_recalc(physmem - nv);
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate static kphysm_setup_vector_t swap_mem_config_vec = {
3957c478bd9Sstevel@tonic-gate 	KPHYSM_SETUP_VECTOR_VERSION,
3967c478bd9Sstevel@tonic-gate 	swap_mem_config_post_add,
3977c478bd9Sstevel@tonic-gate 	swap_mem_config_pre_del,
3987c478bd9Sstevel@tonic-gate 	swap_mem_config_post_del,
3997c478bd9Sstevel@tonic-gate };
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate static void
4027c478bd9Sstevel@tonic-gate swap_init_mem_config(void)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	int ret;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	ret = kphysm_setup_func_register(&swap_mem_config_vec, (void *)NULL);
4077c478bd9Sstevel@tonic-gate 	ASSERT(ret == 0);
4087c478bd9Sstevel@tonic-gate }
409