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