xref: /illumos-gate/usr/src/uts/common/vm/vm_swap.c (revision 6cdd461f)
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
5da6c28aaSamw  * Common Development and Distribution License (the "License").
6da6c28aaSamw  * 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  */
21e1936e20SJoshua M. Clulow 
22e1936e20SJoshua M. Clulow /*
23e1936e20SJoshua M. Clulow  * Copyright 2015 Joyent, Inc.
24e1936e20SJoshua M. Clulow  */
25e1936e20SJoshua M. Clulow 
267c478bd9Sstevel@tonic-gate /*
2723d9e5acSMichael Corcoran  * Copyright (c) 1987, 2010, Oracle and/or its affiliates. All rights reserved.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
31*6cdd461fSToomas Soome /*	  All Rights Reserved	*/
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
357c478bd9Sstevel@tonic-gate  * The Regents of the University of California
367c478bd9Sstevel@tonic-gate  * All Rights Reserved
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
397c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
407c478bd9Sstevel@tonic-gate  * contributors.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * Each physical swap area has an associated bitmap representing
457c478bd9Sstevel@tonic-gate  * its physical storage. The bitmap records which swap slots are
467c478bd9Sstevel@tonic-gate  * currently allocated or freed.  Allocation is done by searching
477c478bd9Sstevel@tonic-gate  * through the bitmap for the first free slot. Thus, there's
487c478bd9Sstevel@tonic-gate  * no linear relation between offset within the swap device and the
497c478bd9Sstevel@tonic-gate  * address (within its segment(s)) of the page that the slot backs;
507c478bd9Sstevel@tonic-gate  * instead, it's an arbitrary one-to-one mapping.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * Associated with each swap area is a swapinfo structure.  These
537c478bd9Sstevel@tonic-gate  * structures are linked into a linear list that determines the
547c478bd9Sstevel@tonic-gate  * ordering of swap areas in the logical swap device.  Each contains a
557c478bd9Sstevel@tonic-gate  * pointer to the corresponding bitmap, the area's size, and its
567c478bd9Sstevel@tonic-gate  * associated vnode.
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <sys/types.h>
607c478bd9Sstevel@tonic-gate #include <sys/inttypes.h>
617c478bd9Sstevel@tonic-gate #include <sys/param.h>
627c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
637c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
647c478bd9Sstevel@tonic-gate #include <sys/systm.h>
657c478bd9Sstevel@tonic-gate #include <sys/errno.h>
667c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
677c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
687c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
697c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
707c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
717c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
727c478bd9Sstevel@tonic-gate #include <sys/swap.h>
737c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h>
747c478bd9Sstevel@tonic-gate #include <sys/debug.h>
757c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
767c478bd9Sstevel@tonic-gate #include <sys/fs/swapnode.h>
777c478bd9Sstevel@tonic-gate #include <sys/policy.h>
787c478bd9Sstevel@tonic-gate #include <sys/zone.h>
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #include <vm/as.h>
817c478bd9Sstevel@tonic-gate #include <vm/seg.h>
827c478bd9Sstevel@tonic-gate #include <vm/page.h>
837c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
847c478bd9Sstevel@tonic-gate #include <vm/hat.h>
857c478bd9Sstevel@tonic-gate #include <vm/anon.h>
867c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * To balance the load among multiple swap areas, we don't allow
907c478bd9Sstevel@tonic-gate  * more than swap_maxcontig allocations to be satisfied from a
917c478bd9Sstevel@tonic-gate  * single swap area before moving on to the next swap area.  This
927c478bd9Sstevel@tonic-gate  * effectively "interleaves" allocations among the many swap areas.
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate int swap_maxcontig;	/* set by anon_init() to 1 Mb */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #define	MINIROOTSIZE	12000	/* ~6 Meg XXX */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * XXX - this lock is a kludge. It serializes some aspects of swapadd() and
1007c478bd9Sstevel@tonic-gate  * swapdel() (namely VOP_OPEN, VOP_CLOSE, VN_RELE).  It protects against
1017c478bd9Sstevel@tonic-gate  * somebody swapadd'ing and getting swap slots from a vnode, while someone
1027c478bd9Sstevel@tonic-gate  * else is in the process of closing or rele'ing it.
1037c478bd9Sstevel@tonic-gate  */
1047c478bd9Sstevel@tonic-gate static kmutex_t swap_lock;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate kmutex_t swapinfo_lock;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate  * protected by the swapinfo_lock
1107c478bd9Sstevel@tonic-gate  */
111*6cdd461fSToomas Soome extern struct swapinfo	*swapinfo;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static	struct	swapinfo *silast;
1147c478bd9Sstevel@tonic-gate static	int	nswapfiles;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate static u_offset_t	swap_getoff(struct swapinfo *);
1177c478bd9Sstevel@tonic-gate static int	swapadd(struct vnode *, ulong_t, ulong_t, char *);
1187c478bd9Sstevel@tonic-gate static int	swapdel(struct vnode *, ulong_t);
1197c478bd9Sstevel@tonic-gate static int	swapslot_free(struct vnode *, u_offset_t, struct swapinfo *);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * swap device bitmap allocation macros
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate #define	MAPSHIFT	5
1257c478bd9Sstevel@tonic-gate #define	NBBW		(NBPW * NBBY)	/* number of bits per word */
1267c478bd9Sstevel@tonic-gate #define	TESTBIT(map, i)		(((map)[(i) >> MAPSHIFT] & (1 << (i) % NBBW)))
1277c478bd9Sstevel@tonic-gate #define	SETBIT(map, i)		(((map)[(i) >> MAPSHIFT] |= (1 << (i) % NBBW)))
1287c478bd9Sstevel@tonic-gate #define	CLEARBIT(map, i)	(((map)[(i) >> MAPSHIFT] &= ~(1 << (i) % NBBW)))
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate int swap_debug = 0;	/* set for debug printf's */
1317c478bd9Sstevel@tonic-gate int swap_verify = 0;	/* set to verify slots when freeing and allocating */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate uint_t swapalloc_maxcontig;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate  * Allocate a range of up to *lenp contiguous slots (page) from a physical
1377c478bd9Sstevel@tonic-gate  * swap device. Flags are one of:
1387c478bd9Sstevel@tonic-gate  *	SA_NOT  Must have a slot from a physical swap device other than the
139*6cdd461fSToomas Soome  *		the one containing input (*vpp, *offp).
1407c478bd9Sstevel@tonic-gate  * Less slots than requested may be returned. *lenp allocated slots are
1417c478bd9Sstevel@tonic-gate  * returned starting at *offp on *vpp.
1427c478bd9Sstevel@tonic-gate  * Returns 1 for a successful allocation, 0 for couldn't allocate any slots.
1437c478bd9Sstevel@tonic-gate  */
1447c478bd9Sstevel@tonic-gate int
swap_phys_alloc(struct vnode ** vpp,u_offset_t * offp,size_t * lenp,uint_t flags)1457c478bd9Sstevel@tonic-gate swap_phys_alloc(
1467c478bd9Sstevel@tonic-gate 	struct vnode **vpp,
1477c478bd9Sstevel@tonic-gate 	u_offset_t *offp,
1487c478bd9Sstevel@tonic-gate 	size_t *lenp,
1497c478bd9Sstevel@tonic-gate 	uint_t flags)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	struct swapinfo *sip;
1527c478bd9Sstevel@tonic-gate 	offset_t soff, noff;
1537c478bd9Sstevel@tonic-gate 	size_t len;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	mutex_enter(&swapinfo_lock);
1567c478bd9Sstevel@tonic-gate 	sip = silast;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/* Find a desirable physical device and allocate from it. */
1597c478bd9Sstevel@tonic-gate 	do {
1607c478bd9Sstevel@tonic-gate 		if (sip == NULL)
1617c478bd9Sstevel@tonic-gate 			break;
1627c478bd9Sstevel@tonic-gate 		if (!(sip->si_flags & ST_INDEL) &&
1637c478bd9Sstevel@tonic-gate 		    (spgcnt_t)sip->si_nfpgs > 0) {
1647c478bd9Sstevel@tonic-gate 			/* Caller wants other than specified swap device */
1657c478bd9Sstevel@tonic-gate 			if (flags & SA_NOT) {
1667c478bd9Sstevel@tonic-gate 				if (*vpp != sip->si_vp ||
1677c478bd9Sstevel@tonic-gate 				    *offp < sip->si_soff ||
1687c478bd9Sstevel@tonic-gate 				    *offp >= sip->si_eoff)
1697c478bd9Sstevel@tonic-gate 					goto found;
1707c478bd9Sstevel@tonic-gate 			/* Caller is loose, will take anything */
1717c478bd9Sstevel@tonic-gate 			} else
1727c478bd9Sstevel@tonic-gate 				goto found;
1737c478bd9Sstevel@tonic-gate 		} else if (sip->si_nfpgs == 0)
1747c478bd9Sstevel@tonic-gate 			sip->si_allocs = 0;
1757c478bd9Sstevel@tonic-gate 		if ((sip = sip->si_next) == NULL)
1767c478bd9Sstevel@tonic-gate 			sip = swapinfo;
1777c478bd9Sstevel@tonic-gate 	} while (sip != silast);
1787c478bd9Sstevel@tonic-gate 	mutex_exit(&swapinfo_lock);
1797c478bd9Sstevel@tonic-gate 	return (0);
1807c478bd9Sstevel@tonic-gate found:
1817c478bd9Sstevel@tonic-gate 	soff = swap_getoff(sip);
1827c478bd9Sstevel@tonic-gate 	sip->si_nfpgs--;
1837c478bd9Sstevel@tonic-gate 	if (soff == -1)
1847c478bd9Sstevel@tonic-gate 		panic("swap_alloc: swap_getoff failed!");
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	for (len = PAGESIZE; len < *lenp; len += PAGESIZE) {
1877c478bd9Sstevel@tonic-gate 		if (sip->si_nfpgs == 0)
1887c478bd9Sstevel@tonic-gate 			break;
1897c478bd9Sstevel@tonic-gate 		if (swapalloc_maxcontig && len >= swapalloc_maxcontig)
1907c478bd9Sstevel@tonic-gate 			break;
1917c478bd9Sstevel@tonic-gate 		noff = swap_getoff(sip);
1927c478bd9Sstevel@tonic-gate 		if (noff == -1) {
1937c478bd9Sstevel@tonic-gate 			break;
1947c478bd9Sstevel@tonic-gate 		} else if (noff != soff + len) {
1957c478bd9Sstevel@tonic-gate 			CLEARBIT(sip->si_swapslots, btop(noff - sip->si_soff));
1967c478bd9Sstevel@tonic-gate 			break;
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate 		sip->si_nfpgs--;
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 	*vpp = sip->si_vp;
2017c478bd9Sstevel@tonic-gate 	*offp = soff;
2027c478bd9Sstevel@tonic-gate 	*lenp = len;
2037c478bd9Sstevel@tonic-gate 	ASSERT((spgcnt_t)sip->si_nfpgs >= 0);
2047c478bd9Sstevel@tonic-gate 	sip->si_allocs += btop(len);
2057c478bd9Sstevel@tonic-gate 	if (sip->si_allocs >= swap_maxcontig) {
2067c478bd9Sstevel@tonic-gate 		sip->si_allocs = 0;
2077c478bd9Sstevel@tonic-gate 		if ((silast = sip->si_next) == NULL)
2087c478bd9Sstevel@tonic-gate 			silast = swapinfo;
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_VM, TR_SWAP_ALLOC,
21120a2d3f6Sstans 	    "swap_alloc:sip %p offset %lx", sip, soff);
2127c478bd9Sstevel@tonic-gate 	mutex_exit(&swapinfo_lock);
2137c478bd9Sstevel@tonic-gate 	return (1);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate int swap_backsearch = 0;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate  * Get a free offset on swap device sip.
2207c478bd9Sstevel@tonic-gate  * Return >=0 offset if succeeded, -1 for failure.
2217c478bd9Sstevel@tonic-gate  */
2227c478bd9Sstevel@tonic-gate static u_offset_t
swap_getoff(struct swapinfo * sip)2237c478bd9Sstevel@tonic-gate swap_getoff(struct swapinfo *sip)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	uint_t *sp, *ep;
2267c478bd9Sstevel@tonic-gate 	size_t aoff, boff, poff, slotnumber;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&swapinfo_lock));
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	sip->si_alloccnt++;
2317c478bd9Sstevel@tonic-gate 	for (sp = &sip->si_swapslots[sip->si_hint >> MAPSHIFT],
2327c478bd9Sstevel@tonic-gate 	    ep = &sip->si_swapslots[sip->si_mapsize / NBPW]; sp < ep; sp++) {
2337c478bd9Sstevel@tonic-gate 		if (*sp != (uint_t)0xffffffff)
2347c478bd9Sstevel@tonic-gate 			goto foundentry;
2357c478bd9Sstevel@tonic-gate 		else
2367c478bd9Sstevel@tonic-gate 			sip->si_checkcnt++;
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 	SWAP_PRINT(SW_ALLOC,
2397c478bd9Sstevel@tonic-gate 	    "swap_getoff: couldn't find slot from hint %ld to end\n",
2407c478bd9Sstevel@tonic-gate 	    sip->si_hint, 0, 0, 0, 0);
2417c478bd9Sstevel@tonic-gate 	/*
2427c478bd9Sstevel@tonic-gate 	 * Go backwards? Check for faster method XXX
2437c478bd9Sstevel@tonic-gate 	 */
2447c478bd9Sstevel@tonic-gate 	if (swap_backsearch) {
2457c478bd9Sstevel@tonic-gate 		for (sp = &sip->si_swapslots[sip->si_hint >> MAPSHIFT],
2467c478bd9Sstevel@tonic-gate 		    ep = sip->si_swapslots; sp > ep; sp--) {
2477c478bd9Sstevel@tonic-gate 			if (*sp != (uint_t)0xffffffff)
2487c478bd9Sstevel@tonic-gate 				goto foundentry;
2497c478bd9Sstevel@tonic-gate 			else
2507c478bd9Sstevel@tonic-gate 				sip->si_checkcnt++;
2517c478bd9Sstevel@tonic-gate 		}
2527c478bd9Sstevel@tonic-gate 	} else {
2537c478bd9Sstevel@tonic-gate 		for (sp = sip->si_swapslots,
2547c478bd9Sstevel@tonic-gate 		    ep = &sip->si_swapslots[sip->si_hint >> MAPSHIFT];
2557c478bd9Sstevel@tonic-gate 		    sp < ep; sp++) {
2567c478bd9Sstevel@tonic-gate 			if (*sp != (uint_t)0xffffffff)
2577c478bd9Sstevel@tonic-gate 				goto foundentry;
2587c478bd9Sstevel@tonic-gate 			else
2597c478bd9Sstevel@tonic-gate 				sip->si_checkcnt++;
2607c478bd9Sstevel@tonic-gate 		}
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 	if (*sp == 0xffffffff) {
2637c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "No free swap slots!");
2647c478bd9Sstevel@tonic-gate 		return ((u_offset_t)-1);
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate foundentry:
2687c478bd9Sstevel@tonic-gate 	/*
2697c478bd9Sstevel@tonic-gate 	 * aoff is the page number offset (in bytes) of the si_swapslots
2707c478bd9Sstevel@tonic-gate 	 * array element containing a free page
2717c478bd9Sstevel@tonic-gate 	 *
2727c478bd9Sstevel@tonic-gate 	 * boff is the page number offset of the free page
2737c478bd9Sstevel@tonic-gate 	 * (i.e. cleared bit) in si_swapslots[aoff].
2747c478bd9Sstevel@tonic-gate 	 */
2757c478bd9Sstevel@tonic-gate 	aoff = ((char *)sp - (char *)sip->si_swapslots) * NBBY;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	for (boff = (sip->si_hint % NBBW); boff < NBBW; boff++) {
2787c478bd9Sstevel@tonic-gate 		if (!TESTBIT(sip->si_swapslots, aoff + boff))
2797c478bd9Sstevel@tonic-gate 			goto foundslot;
2807c478bd9Sstevel@tonic-gate 		else
2817c478bd9Sstevel@tonic-gate 			sip->si_checkcnt++;
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 	for (boff = 0; boff < (sip->si_hint % NBBW); boff++) {
2847c478bd9Sstevel@tonic-gate 		if (!TESTBIT(sip->si_swapslots, aoff + boff))
2857c478bd9Sstevel@tonic-gate 			goto foundslot;
2867c478bd9Sstevel@tonic-gate 		else
2877c478bd9Sstevel@tonic-gate 			sip->si_checkcnt++;
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 	panic("swap_getoff: didn't find slot in word hint %ld", sip->si_hint);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate foundslot:
2927c478bd9Sstevel@tonic-gate 	/*
2937c478bd9Sstevel@tonic-gate 	 * Return the offset of the free page in swap device.
2947c478bd9Sstevel@tonic-gate 	 * Convert page number of byte offset and add starting
2957c478bd9Sstevel@tonic-gate 	 * offset of swap device.
2967c478bd9Sstevel@tonic-gate 	 */
2977c478bd9Sstevel@tonic-gate 	slotnumber = aoff + boff;
2987c478bd9Sstevel@tonic-gate 	SWAP_PRINT(SW_ALLOC, "swap_getoff: allocating slot %ld\n",
2997c478bd9Sstevel@tonic-gate 	    slotnumber, 0, 0, 0, 0);
3007c478bd9Sstevel@tonic-gate 	poff = ptob(slotnumber);
3017c478bd9Sstevel@tonic-gate 	if (poff + sip->si_soff >= sip->si_eoff)
3027c478bd9Sstevel@tonic-gate 		printf("ptob(aoff(%ld) + boff(%ld))(%ld) >= eoff(%ld)\n",
3037c478bd9Sstevel@tonic-gate 		    aoff, boff, ptob(slotnumber), (long)sip->si_eoff);
3047c478bd9Sstevel@tonic-gate 	ASSERT(poff < sip->si_eoff);
3057c478bd9Sstevel@tonic-gate 	/*
3067c478bd9Sstevel@tonic-gate 	 * We could verify here that the slot isn't already allocated
3077c478bd9Sstevel@tonic-gate 	 * by looking through all the anon slots.
3087c478bd9Sstevel@tonic-gate 	 */
3097c478bd9Sstevel@tonic-gate 	SETBIT(sip->si_swapslots, slotnumber);
3107c478bd9Sstevel@tonic-gate 	sip->si_hint = slotnumber + 1;	/* hint = next slot */
3117c478bd9Sstevel@tonic-gate 	return (poff + sip->si_soff);
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate /*
3157c478bd9Sstevel@tonic-gate  * Free a swap page.
3167c478bd9Sstevel@tonic-gate  */
3177c478bd9Sstevel@tonic-gate void
swap_phys_free(struct vnode * vp,u_offset_t off,size_t len)3187c478bd9Sstevel@tonic-gate swap_phys_free(struct vnode *vp, u_offset_t off, size_t len)
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate 	struct swapinfo *sip;
3217c478bd9Sstevel@tonic-gate 	ssize_t pagenumber, npage;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	mutex_enter(&swapinfo_lock);
3247c478bd9Sstevel@tonic-gate 	sip = swapinfo;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	do {
3277c478bd9Sstevel@tonic-gate 		if (sip->si_vp == vp &&
3287c478bd9Sstevel@tonic-gate 		    sip->si_soff <= off && off < sip->si_eoff) {
3297c478bd9Sstevel@tonic-gate 			for (pagenumber = btop(off - sip->si_soff),
3307c478bd9Sstevel@tonic-gate 			    npage = btop(len) + pagenumber;
3317c478bd9Sstevel@tonic-gate 			    pagenumber < npage; pagenumber++) {
3327c478bd9Sstevel@tonic-gate 				SWAP_PRINT(SW_ALLOC,
3337c478bd9Sstevel@tonic-gate 				    "swap_phys_free: freeing slot %ld on "
3347c478bd9Sstevel@tonic-gate 				    "sip %p\n",
3357c478bd9Sstevel@tonic-gate 				    pagenumber, sip, 0, 0, 0);
3367c478bd9Sstevel@tonic-gate 				if (!TESTBIT(sip->si_swapslots, pagenumber)) {
3377c478bd9Sstevel@tonic-gate 					panic(
3387c478bd9Sstevel@tonic-gate 					    "swap_phys_free: freeing free slot "
3397c478bd9Sstevel@tonic-gate 					    "%p,%lx\n", (void *)vp,
3407c478bd9Sstevel@tonic-gate 					    ptob(pagenumber) + sip->si_soff);
3417c478bd9Sstevel@tonic-gate 				}
3427c478bd9Sstevel@tonic-gate 				CLEARBIT(sip->si_swapslots, pagenumber);
3437c478bd9Sstevel@tonic-gate 				sip->si_nfpgs++;
3447c478bd9Sstevel@tonic-gate 			}
3457c478bd9Sstevel@tonic-gate 			ASSERT(sip->si_nfpgs <= sip->si_npgs);
3467c478bd9Sstevel@tonic-gate 			mutex_exit(&swapinfo_lock);
3477c478bd9Sstevel@tonic-gate 			return;
3487c478bd9Sstevel@tonic-gate 		}
3497c478bd9Sstevel@tonic-gate 	} while ((sip = sip->si_next) != NULL);
3507c478bd9Sstevel@tonic-gate 	panic("swap_phys_free");
3517c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate /*
3557c478bd9Sstevel@tonic-gate  * Return the anon struct corresponding for the given
3567c478bd9Sstevel@tonic-gate  * <vnode, off> if it is part of the virtual swap device.
3577c478bd9Sstevel@tonic-gate  * Return the anon struct if found, otherwise NULL.
3587c478bd9Sstevel@tonic-gate  */
3597c478bd9Sstevel@tonic-gate struct anon *
swap_anon(struct vnode * vp,u_offset_t off)3607c478bd9Sstevel@tonic-gate swap_anon(struct vnode *vp, u_offset_t off)
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate 	struct anon *ap;
3637c478bd9Sstevel@tonic-gate 
36423d9e5acSMichael Corcoran 	ASSERT(MUTEX_HELD(AH_MUTEX(vp, off)));
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	for (ap = anon_hash[ANON_HASH(vp, off)]; ap != NULL; ap = ap->an_hash) {
3677c478bd9Sstevel@tonic-gate 		if (ap->an_vp == vp && ap->an_off == off)
3687c478bd9Sstevel@tonic-gate 			return (ap);
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 	return (NULL);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate /*
3757c478bd9Sstevel@tonic-gate  * Determine if the vp offset range overlap a swap device.
3767c478bd9Sstevel@tonic-gate  */
3777c478bd9Sstevel@tonic-gate int
swap_in_range(struct vnode * vp,u_offset_t offset,size_t len)3787c478bd9Sstevel@tonic-gate swap_in_range(struct vnode *vp, u_offset_t offset, size_t len)
3797c478bd9Sstevel@tonic-gate {
3807c478bd9Sstevel@tonic-gate 	struct swapinfo *sip;
3817c478bd9Sstevel@tonic-gate 	u_offset_t eoff;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	eoff = offset + len;
3847c478bd9Sstevel@tonic-gate 	ASSERT(eoff > offset);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	mutex_enter(&swapinfo_lock);
3877c478bd9Sstevel@tonic-gate 	sip = swapinfo;
3887c478bd9Sstevel@tonic-gate 	if (vp && sip) {
3897c478bd9Sstevel@tonic-gate 		do {
3907c478bd9Sstevel@tonic-gate 			if (vp != sip->si_vp || eoff <= sip->si_soff ||
3917c478bd9Sstevel@tonic-gate 			    offset >= sip->si_eoff)
3927c478bd9Sstevel@tonic-gate 				continue;
3937c478bd9Sstevel@tonic-gate 			mutex_exit(&swapinfo_lock);
3947c478bd9Sstevel@tonic-gate 			return (1);
3957c478bd9Sstevel@tonic-gate 		} while ((sip = sip->si_next) != NULL);
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 	mutex_exit(&swapinfo_lock);
3987c478bd9Sstevel@tonic-gate 	return (0);
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate /*
4027c478bd9Sstevel@tonic-gate  * See if name is one of our swap files
4037c478bd9Sstevel@tonic-gate  * even though lookupname failed.
4047c478bd9Sstevel@tonic-gate  * This can be used by swapdel to delete
4057c478bd9Sstevel@tonic-gate  * swap resources on remote machines
4067c478bd9Sstevel@tonic-gate  * where the link has gone down.
4077c478bd9Sstevel@tonic-gate  */
4087c478bd9Sstevel@tonic-gate static struct vnode *
swapdel_byname(char * name,ulong_t lowblk)4097c478bd9Sstevel@tonic-gate swapdel_byname(
410*6cdd461fSToomas Soome 	char	*name,			/* pathname to delete */
411*6cdd461fSToomas Soome 	ulong_t lowblk)			/* Low block number of area to delete */
4127c478bd9Sstevel@tonic-gate {
4137c478bd9Sstevel@tonic-gate 	struct swapinfo **sipp, *osip;
4147c478bd9Sstevel@tonic-gate 	u_offset_t soff;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	/*
4177c478bd9Sstevel@tonic-gate 	 * Find the swap file entry for the file to
4187c478bd9Sstevel@tonic-gate 	 * be deleted. Skip any entries that are in
4197c478bd9Sstevel@tonic-gate 	 * transition.
4207c478bd9Sstevel@tonic-gate 	 */
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	soff = ptob(btopr(lowblk << SCTRSHFT)); /* must be page aligned */
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	mutex_enter(&swapinfo_lock);
4257c478bd9Sstevel@tonic-gate 	for (sipp = &swapinfo; (osip = *sipp) != NULL; sipp = &osip->si_next) {
4267c478bd9Sstevel@tonic-gate 		if ((strcmp(osip->si_pname, name) == 0) &&
4277c478bd9Sstevel@tonic-gate 		    (osip->si_soff == soff) && (osip->si_flags == 0)) {
4287c478bd9Sstevel@tonic-gate 			struct vnode *vp = osip->si_vp;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 			VN_HOLD(vp);
4317c478bd9Sstevel@tonic-gate 			mutex_exit(&swapinfo_lock);
4327c478bd9Sstevel@tonic-gate 			return (vp);
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 	mutex_exit(&swapinfo_lock);
4367c478bd9Sstevel@tonic-gate 	return (NULL);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate /*
4417c478bd9Sstevel@tonic-gate  * New system call to manipulate swap files.
4427c478bd9Sstevel@tonic-gate  */
4437c478bd9Sstevel@tonic-gate int
swapctl(int sc_cmd,void * sc_arg,int * rv)4447c478bd9Sstevel@tonic-gate swapctl(int sc_cmd, void *sc_arg, int *rv)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	struct swapinfo *sip, *csip, *tsip;
4477c478bd9Sstevel@tonic-gate 	int error = 0;
4487c478bd9Sstevel@tonic-gate 	struct swapent st, *ust;
4497c478bd9Sstevel@tonic-gate 	struct swapres sr;
4507c478bd9Sstevel@tonic-gate 	struct vnode *vp;
4517c478bd9Sstevel@tonic-gate 	int cnt = 0;
4527c478bd9Sstevel@tonic-gate 	int tmp_nswapfiles;
4537c478bd9Sstevel@tonic-gate 	int nswap;
4547c478bd9Sstevel@tonic-gate 	int length, nlen;
4557c478bd9Sstevel@tonic-gate 	int gplen = 0, plen;
4567c478bd9Sstevel@tonic-gate 	char *swapname;
4577c478bd9Sstevel@tonic-gate 	char *pname;
4587c478bd9Sstevel@tonic-gate 	char *tpname;
4597c478bd9Sstevel@tonic-gate 	struct anoninfo ai;
4607c478bd9Sstevel@tonic-gate 	spgcnt_t avail;
4617c478bd9Sstevel@tonic-gate 	int global = INGLOBALZONE(curproc);
46200792c0bS 	struct zone *zp = curproc->p_zone;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	/*
4657c478bd9Sstevel@tonic-gate 	 * When running in a zone we want to hide the details of the swap
4667c478bd9Sstevel@tonic-gate 	 * devices: we report there only being one swap device named "swap"
4677c478bd9Sstevel@tonic-gate 	 * having a size equal to the sum of the sizes of all real swap devices
4687c478bd9Sstevel@tonic-gate 	 * on the system.
4697c478bd9Sstevel@tonic-gate 	 */
4707c478bd9Sstevel@tonic-gate 	switch (sc_cmd) {
4717c478bd9Sstevel@tonic-gate 	case SC_GETNSWP:
4727c478bd9Sstevel@tonic-gate 		if (global)
4737c478bd9Sstevel@tonic-gate 			*rv = nswapfiles;
4747c478bd9Sstevel@tonic-gate 		else
4757c478bd9Sstevel@tonic-gate 			*rv = 1;
4767c478bd9Sstevel@tonic-gate 		return (0);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	case SC_AINFO:
4797c478bd9Sstevel@tonic-gate 		/*
4807c478bd9Sstevel@tonic-gate 		 * Return anoninfo information with these changes:
4817c478bd9Sstevel@tonic-gate 		 * ani_max = maximum amount of swap space
4827c478bd9Sstevel@tonic-gate 		 *	(including potentially available physical memory)
4837c478bd9Sstevel@tonic-gate 		 * ani_free = amount of unallocated anonymous memory
4847c478bd9Sstevel@tonic-gate 		 *	(some of which might be reserved and including
4857c478bd9Sstevel@tonic-gate 		 *	 potentially available physical memory)
4867c478bd9Sstevel@tonic-gate 		 * ani_resv = amount of claimed (reserved) anonymous memory
4877c478bd9Sstevel@tonic-gate 		 */
4887c478bd9Sstevel@tonic-gate 		avail = MAX((spgcnt_t)(availrmem - swapfs_minfree), 0);
4897c478bd9Sstevel@tonic-gate 		ai.ani_max = (k_anoninfo.ani_max +
49000792c0bS 		    k_anoninfo.ani_mem_resv) + avail;
4917c478bd9Sstevel@tonic-gate 
492b52a336eSPavel Tatashin 		/* Update ani_free */
493b52a336eSPavel Tatashin 		set_anoninfo();
4947c478bd9Sstevel@tonic-gate 		ai.ani_free = k_anoninfo.ani_free + avail;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 		ai.ani_resv = k_anoninfo.ani_phys_resv +
4977c478bd9Sstevel@tonic-gate 		    k_anoninfo.ani_mem_resv;
4987c478bd9Sstevel@tonic-gate 
49900792c0bS 		if (!global && zp->zone_max_swap_ctl != UINT64_MAX) {
50000792c0bS 			/*
50100792c0bS 			 * We're in a non-global zone with a swap cap.  We
50200792c0bS 			 * always report the system-wide values for the global
50300792c0bS 			 * zone, even though it too can have a swap cap.
50400792c0bS 			 */
50500792c0bS 
50600792c0bS 			/*
50700792c0bS 			 * For a swap-capped zone, the numbers are contrived
50800792c0bS 			 * since we don't have a correct value of 'reserved'
50900792c0bS 			 * for the zone.
51000792c0bS 			 *
51100792c0bS 			 * The ani_max value is always the zone's swap cap.
51200792c0bS 			 *
51300792c0bS 			 * The ani_free value is always the difference between
51400792c0bS 			 * the cap and the amount of swap in use by the zone.
51500792c0bS 			 *
51600792c0bS 			 * The ani_resv value is typically set to be the amount
51700792c0bS 			 * of swap in use by the zone, but can be adjusted
51800792c0bS 			 * upwards to indicate how much swap is currently
51900792c0bS 			 * unavailable to that zone due to usage by entities
52000792c0bS 			 * outside the zone.
52100792c0bS 			 *
52200792c0bS 			 * This works as follows.
52300792c0bS 			 *
52400792c0bS 			 * In the 'swap -s' output, the data is displayed
52500792c0bS 			 * as follows:
52600792c0bS 			 *    allocated = ani_max  - ani_free
52700792c0bS 			 *    reserved  = ani_resv - allocated
52800792c0bS 			 *    available = ani_max  - ani_resv
52900792c0bS 			 *
53000792c0bS 			 * Taking a contrived example, if the swap cap is 100
53100792c0bS 			 * and the amount of swap used by the zone is 75, this
53200792c0bS 			 * gives:
53300792c0bS 			 *    allocated = ani_max  - ani_free  = 100 - 25 = 75
53400792c0bS 			 *    reserved  = ani_resv - allocated =  75 - 75 =  0
53500792c0bS 			 *    available = ani_max  - ani_resv  = 100 - 75 = 25
53600792c0bS 			 *
53700792c0bS 			 * In this typical case, you can see that the 'swap -s'
53800792c0bS 			 * 'reserved' will always be 0 inside a swap capped
53900792c0bS 			 * zone.
54000792c0bS 			 *
54100792c0bS 			 * However, if the system as a whole has less free
54200792c0bS 			 * swap than the zone limits allow, then we adjust
54300792c0bS 			 * the ani_resv value up so that it is the difference
54400792c0bS 			 * between the zone cap and the amount of free system
54500792c0bS 			 * swap.  Taking the above example, but when the
54600792c0bS 			 * system as a whole only has 20 of swap available, we
54700792c0bS 			 * get an ani_resv of 100 - 20 = 80.  This gives:
54800792c0bS 			 *    allocated = ani_max  - ani_free  = 100 - 25 = 75
54900792c0bS 			 *    reserved  = ani_resv - allocated =  80 - 75 =  5
55000792c0bS 			 *    available = ani_max  - ani_resv  = 100 - 80 = 20
55100792c0bS 			 *
55200792c0bS 			 * In this case, you can see how the ani_resv value is
55300792c0bS 			 * tweaked up to make the 'swap -s' numbers work inside
55400792c0bS 			 * the zone.
55500792c0bS 			 */
55600792c0bS 			rctl_qty_t cap, used;
55700792c0bS 			pgcnt_t pgcap, sys_avail;
55800792c0bS 
55900792c0bS 			mutex_enter(&zp->zone_mem_lock);
56000792c0bS 			cap = zp->zone_max_swap_ctl;
56100792c0bS 			used = zp->zone_max_swap;
56200792c0bS 			mutex_exit(&zp->zone_mem_lock);
56300792c0bS 
56400792c0bS 			pgcap = MIN(btop(cap), ai.ani_max);
56500792c0bS 			ai.ani_free = pgcap - btop(used);
56600792c0bS 
56700792c0bS 			/* Get the system-wide swap currently available. */
56800792c0bS 			sys_avail = ai.ani_max - ai.ani_resv;
56900792c0bS 			if (sys_avail < ai.ani_free)
57000792c0bS 				ai.ani_resv = pgcap - sys_avail;
57100792c0bS 			else
57200792c0bS 				ai.ani_resv = btop(used);
57300792c0bS 
57400792c0bS 			ai.ani_max = pgcap;
57500792c0bS 		}
57600792c0bS 
5777c478bd9Sstevel@tonic-gate 		if (copyout(&ai, sc_arg, sizeof (struct anoninfo)) != 0)
5787c478bd9Sstevel@tonic-gate 			return (EFAULT);
5797c478bd9Sstevel@tonic-gate 		return (0);
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	case SC_LIST:
5827c478bd9Sstevel@tonic-gate 		if (copyin(sc_arg, &length, sizeof (int)) != 0)
5837c478bd9Sstevel@tonic-gate 			return (EFAULT);
5847c478bd9Sstevel@tonic-gate 		if (!global) {
5857c478bd9Sstevel@tonic-gate 			struct swapent st;
5867c478bd9Sstevel@tonic-gate 			char *swappath = "swap";
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 			if (length < 1)
5897c478bd9Sstevel@tonic-gate 				return (ENOMEM);
5907c478bd9Sstevel@tonic-gate 			ust = (swapent_t *)((swaptbl_t *)sc_arg)->swt_ent;
5917c478bd9Sstevel@tonic-gate 			if (copyin(ust, &st, sizeof (swapent_t)) != 0)
5927c478bd9Sstevel@tonic-gate 				return (EFAULT);
5937c478bd9Sstevel@tonic-gate 			st.ste_start = PAGESIZE >> SCTRSHFT;
5947c478bd9Sstevel@tonic-gate 			st.ste_length = (off_t)0;
5957c478bd9Sstevel@tonic-gate 			st.ste_pages = 0;
5967c478bd9Sstevel@tonic-gate 			st.ste_free = 0;
5977c478bd9Sstevel@tonic-gate 			st.ste_flags = 0;
59800792c0bS 
5997c478bd9Sstevel@tonic-gate 			mutex_enter(&swapinfo_lock);
6007c478bd9Sstevel@tonic-gate 			for (sip = swapinfo, nswap = 0;
6017c478bd9Sstevel@tonic-gate 			    sip != NULL && nswap < nswapfiles;
6027c478bd9Sstevel@tonic-gate 			    sip = sip->si_next, nswap++) {
6037c478bd9Sstevel@tonic-gate 				st.ste_length +=
6047c478bd9Sstevel@tonic-gate 				    (sip->si_eoff - sip->si_soff) >> SCTRSHFT;
6057c478bd9Sstevel@tonic-gate 				st.ste_pages += sip->si_npgs;
6067c478bd9Sstevel@tonic-gate 				st.ste_free += sip->si_nfpgs;
6077c478bd9Sstevel@tonic-gate 			}
6087c478bd9Sstevel@tonic-gate 			mutex_exit(&swapinfo_lock);
60900792c0bS 
61000792c0bS 			if (zp->zone_max_swap_ctl != UINT64_MAX) {
61100792c0bS 				rctl_qty_t cap, used;
61200792c0bS 
61300792c0bS 				mutex_enter(&zp->zone_mem_lock);
61400792c0bS 				cap = zp->zone_max_swap_ctl;
61500792c0bS 				used = zp->zone_max_swap;
61600792c0bS 				mutex_exit(&zp->zone_mem_lock);
61700792c0bS 
61800792c0bS 				st.ste_length = MIN(cap, st.ste_length);
61900792c0bS 				st.ste_pages = MIN(btop(cap), st.ste_pages);
62000792c0bS 				st.ste_free = MIN(st.ste_pages - btop(used),
62100792c0bS 				    st.ste_free);
62200792c0bS 			}
62300792c0bS 
6247c478bd9Sstevel@tonic-gate 			if (copyout(&st, ust, sizeof (swapent_t)) != 0 ||
6257c478bd9Sstevel@tonic-gate 			    copyout(swappath, st.ste_path,
62620a2d3f6Sstans 			    strlen(swappath) + 1) != 0) {
6277c478bd9Sstevel@tonic-gate 				return (EFAULT);
6287c478bd9Sstevel@tonic-gate 			}
6297c478bd9Sstevel@tonic-gate 			*rv = 1;
6307c478bd9Sstevel@tonic-gate 			return (0);
6317c478bd9Sstevel@tonic-gate 		}
6327c478bd9Sstevel@tonic-gate beginning:
633e1936e20SJoshua M. Clulow 		mutex_enter(&swapinfo_lock);
6347c478bd9Sstevel@tonic-gate 		tmp_nswapfiles = nswapfiles;
635e1936e20SJoshua M. Clulow 		mutex_exit(&swapinfo_lock);
636e1936e20SJoshua M. Clulow 
637e1936e20SJoshua M. Clulow 		/*
638e1936e20SJoshua M. Clulow 		 * Return early if there are no swap entries to report:
639e1936e20SJoshua M. Clulow 		 */
640e1936e20SJoshua M. Clulow 		if (tmp_nswapfiles < 1) {
641e1936e20SJoshua M. Clulow 			*rv = 0;
642e1936e20SJoshua M. Clulow 			return (0);
643e1936e20SJoshua M. Clulow 		}
644e1936e20SJoshua M. Clulow 
6457c478bd9Sstevel@tonic-gate 		/* Return an error if not enough space for the whole table. */
6467c478bd9Sstevel@tonic-gate 		if (length < tmp_nswapfiles)
6477c478bd9Sstevel@tonic-gate 			return (ENOMEM);
6487c478bd9Sstevel@tonic-gate 		/*
6497c478bd9Sstevel@tonic-gate 		 * Get memory to hold the swap entries and their names. We'll
6507c478bd9Sstevel@tonic-gate 		 * copy the real entries into these and then copy these out.
6517c478bd9Sstevel@tonic-gate 		 * Allocating the pathname memory is only a guess so we may
6527c478bd9Sstevel@tonic-gate 		 * find that we need more and have to do it again.
6537c478bd9Sstevel@tonic-gate 		 * All this is because we have to hold the anon lock while
6547c478bd9Sstevel@tonic-gate 		 * traversing the swapinfo list, and we can't be doing copyouts
6557c478bd9Sstevel@tonic-gate 		 * and/or kmem_alloc()s during this.
6567c478bd9Sstevel@tonic-gate 		 */
6577c478bd9Sstevel@tonic-gate 		csip = kmem_zalloc(tmp_nswapfiles * sizeof (struct swapinfo),
6587c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
6597c478bd9Sstevel@tonic-gate retry:
6607c478bd9Sstevel@tonic-gate 		nlen = tmp_nswapfiles * (gplen += 100);
6617c478bd9Sstevel@tonic-gate 		pname = kmem_zalloc(nlen, KM_SLEEP);
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 		mutex_enter(&swapinfo_lock);
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 		if (tmp_nswapfiles != nswapfiles) {
6667c478bd9Sstevel@tonic-gate 			mutex_exit(&swapinfo_lock);
6677c478bd9Sstevel@tonic-gate 			kmem_free(pname, nlen);
6687c478bd9Sstevel@tonic-gate 			kmem_free(csip,
6697c478bd9Sstevel@tonic-gate 			    tmp_nswapfiles * sizeof (struct swapinfo));
6707c478bd9Sstevel@tonic-gate 			gplen = 0;
6717c478bd9Sstevel@tonic-gate 			goto beginning;
6727c478bd9Sstevel@tonic-gate 		}
6737c478bd9Sstevel@tonic-gate 		for (sip = swapinfo, tsip = csip, tpname = pname, nswap = 0;
6747c478bd9Sstevel@tonic-gate 		    sip && nswap < tmp_nswapfiles;
6757c478bd9Sstevel@tonic-gate 		    sip = sip->si_next, tsip++, tpname += plen, nswap++) {
6767c478bd9Sstevel@tonic-gate 			plen = sip->si_pnamelen;
6777c478bd9Sstevel@tonic-gate 			if (tpname + plen - pname > nlen) {
6787c478bd9Sstevel@tonic-gate 				mutex_exit(&swapinfo_lock);
6797c478bd9Sstevel@tonic-gate 				kmem_free(pname, nlen);
6807c478bd9Sstevel@tonic-gate 				goto retry;
6817c478bd9Sstevel@tonic-gate 			}
6827c478bd9Sstevel@tonic-gate 			*tsip = *sip;
6837c478bd9Sstevel@tonic-gate 			tsip->si_pname = tpname;
6847c478bd9Sstevel@tonic-gate 			(void) strcpy(tsip->si_pname, sip->si_pname);
6857c478bd9Sstevel@tonic-gate 		}
6867c478bd9Sstevel@tonic-gate 		mutex_exit(&swapinfo_lock);
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 		if (sip) {
6897c478bd9Sstevel@tonic-gate 			error = ENOMEM;
6907c478bd9Sstevel@tonic-gate 			goto lout;
6917c478bd9Sstevel@tonic-gate 		}
6927c478bd9Sstevel@tonic-gate 		ust = (swapent_t *)((swaptbl_t *)sc_arg)->swt_ent;
6937c478bd9Sstevel@tonic-gate 		for (tsip = csip, cnt = 0; cnt < nswap;  tsip++, ust++, cnt++) {
6947c478bd9Sstevel@tonic-gate 			if (copyin(ust, &st, sizeof (swapent_t)) != 0) {
6957c478bd9Sstevel@tonic-gate 				error = EFAULT;
6967c478bd9Sstevel@tonic-gate 				goto lout;
6977c478bd9Sstevel@tonic-gate 			}
6987c478bd9Sstevel@tonic-gate 			st.ste_flags = tsip->si_flags;
6997c478bd9Sstevel@tonic-gate 			st.ste_length =
7007c478bd9Sstevel@tonic-gate 			    (tsip->si_eoff - tsip->si_soff) >> SCTRSHFT;
7017c478bd9Sstevel@tonic-gate 			st.ste_start = tsip->si_soff >> SCTRSHFT;
7027c478bd9Sstevel@tonic-gate 			st.ste_pages = tsip->si_npgs;
7037c478bd9Sstevel@tonic-gate 			st.ste_free = tsip->si_nfpgs;
7047c478bd9Sstevel@tonic-gate 			if (copyout(&st, ust, sizeof (swapent_t)) != 0) {
7057c478bd9Sstevel@tonic-gate 				error = EFAULT;
7067c478bd9Sstevel@tonic-gate 				goto lout;
7077c478bd9Sstevel@tonic-gate 			}
7087c478bd9Sstevel@tonic-gate 			if (!tsip->si_pnamelen)
7097c478bd9Sstevel@tonic-gate 				continue;
7107c478bd9Sstevel@tonic-gate 			if (copyout(tsip->si_pname, st.ste_path,
71120a2d3f6Sstans 			    tsip->si_pnamelen) != 0) {
7127c478bd9Sstevel@tonic-gate 				error = EFAULT;
7137c478bd9Sstevel@tonic-gate 				goto lout;
7147c478bd9Sstevel@tonic-gate 			}
7157c478bd9Sstevel@tonic-gate 		}
7167c478bd9Sstevel@tonic-gate 		*rv = nswap;
7177c478bd9Sstevel@tonic-gate lout:
7187c478bd9Sstevel@tonic-gate 		kmem_free(csip, tmp_nswapfiles * sizeof (struct swapinfo));
7197c478bd9Sstevel@tonic-gate 		kmem_free(pname, nlen);
7207c478bd9Sstevel@tonic-gate 		return (error);
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	case SC_ADD:
7237c478bd9Sstevel@tonic-gate 	case SC_REMOVE:
7247c478bd9Sstevel@tonic-gate 		break;
7257c478bd9Sstevel@tonic-gate 	default:
7267c478bd9Sstevel@tonic-gate 		return (EINVAL);
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 	if ((error = secpolicy_swapctl(CRED())) != 0)
7297c478bd9Sstevel@tonic-gate 		return (error);
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	if (copyin(sc_arg, &sr, sizeof (swapres_t)))
7327c478bd9Sstevel@tonic-gate 		return (EFAULT);
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	/* Allocate the space to read in pathname */
7357c478bd9Sstevel@tonic-gate 	if ((swapname = kmem_alloc(MAXPATHLEN, KM_NOSLEEP)) == NULL)
7367c478bd9Sstevel@tonic-gate 		return (ENOMEM);
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	error = copyinstr(sr.sr_name, swapname, MAXPATHLEN, 0);
7397c478bd9Sstevel@tonic-gate 	if (error)
7407c478bd9Sstevel@tonic-gate 		goto out;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	error = lookupname(swapname, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
7437c478bd9Sstevel@tonic-gate 	if (error) {
7447c478bd9Sstevel@tonic-gate 		if (sc_cmd == SC_ADD)
7457c478bd9Sstevel@tonic-gate 			goto out;
7467c478bd9Sstevel@tonic-gate 		/* see if we match by name */
7477c478bd9Sstevel@tonic-gate 		vp = swapdel_byname(swapname, (size_t)sr.sr_start);
7487c478bd9Sstevel@tonic-gate 		if (vp == NULL)
7497c478bd9Sstevel@tonic-gate 			goto out;
7507c478bd9Sstevel@tonic-gate 	}
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	if (vp->v_flag & (VNOMAP | VNOSWAP)) {
7537c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
7547c478bd9Sstevel@tonic-gate 		error = ENOSYS;
7557c478bd9Sstevel@tonic-gate 		goto out;
7567c478bd9Sstevel@tonic-gate 	}
7577c478bd9Sstevel@tonic-gate 	switch (vp->v_type) {
7587c478bd9Sstevel@tonic-gate 	case VBLK:
7597c478bd9Sstevel@tonic-gate 		break;
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	case VREG:
7627c478bd9Sstevel@tonic-gate 		if (vp->v_vfsp && vn_is_readonly(vp))
7637c478bd9Sstevel@tonic-gate 			error = EROFS;
7647c478bd9Sstevel@tonic-gate 		else
765da6c28aaSamw 			error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL);
7667c478bd9Sstevel@tonic-gate 		break;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	case VDIR:
7697c478bd9Sstevel@tonic-gate 		error = EISDIR;
7707c478bd9Sstevel@tonic-gate 		break;
7717c478bd9Sstevel@tonic-gate 	default:
7727c478bd9Sstevel@tonic-gate 		error = ENOSYS;
7737c478bd9Sstevel@tonic-gate 		break;
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 	if (error == 0) {
7767c478bd9Sstevel@tonic-gate 		if (sc_cmd == SC_REMOVE)
7777c478bd9Sstevel@tonic-gate 			error = swapdel(vp, sr.sr_start);
7787c478bd9Sstevel@tonic-gate 		else
7797c478bd9Sstevel@tonic-gate 			error = swapadd(vp, sr.sr_start,
78020a2d3f6Sstans 			    sr.sr_length, swapname);
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
7837c478bd9Sstevel@tonic-gate out:
7847c478bd9Sstevel@tonic-gate 	kmem_free(swapname, MAXPATHLEN);
7857c478bd9Sstevel@tonic-gate 	return (error);
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate #if defined(_LP64) && defined(_SYSCALL32)
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate int
swapctl32(int sc_cmd,void * sc_arg,int * rv)7917c478bd9Sstevel@tonic-gate swapctl32(int sc_cmd, void *sc_arg, int *rv)
7927c478bd9Sstevel@tonic-gate {
7937c478bd9Sstevel@tonic-gate 	struct swapinfo *sip, *csip, *tsip;
7947c478bd9Sstevel@tonic-gate 	int error = 0;
7957c478bd9Sstevel@tonic-gate 	struct swapent32 st, *ust;
7967c478bd9Sstevel@tonic-gate 	struct swapres32 sr;
7977c478bd9Sstevel@tonic-gate 	struct vnode *vp;
7987c478bd9Sstevel@tonic-gate 	int cnt = 0;
7997c478bd9Sstevel@tonic-gate 	int tmp_nswapfiles;
8007c478bd9Sstevel@tonic-gate 	int nswap;
8017c478bd9Sstevel@tonic-gate 	int length, nlen;
8027c478bd9Sstevel@tonic-gate 	int gplen = 0, plen;
8037c478bd9Sstevel@tonic-gate 	char *swapname;
8047c478bd9Sstevel@tonic-gate 	char *pname;
8057c478bd9Sstevel@tonic-gate 	char *tpname;
8067c478bd9Sstevel@tonic-gate 	struct anoninfo32 ai;
8077c478bd9Sstevel@tonic-gate 	size_t s;
8087c478bd9Sstevel@tonic-gate 	spgcnt_t avail;
80900792c0bS 	int global = INGLOBALZONE(curproc);
81000792c0bS 	struct zone *zp = curproc->p_zone;
8117c478bd9Sstevel@tonic-gate 
81200792c0bS 	/*
81300792c0bS 	 * When running in a zone we want to hide the details of the swap
81400792c0bS 	 * devices: we report there only being one swap device named "swap"
81500792c0bS 	 * having a size equal to the sum of the sizes of all real swap devices
81600792c0bS 	 * on the system.
81700792c0bS 	 */
8187c478bd9Sstevel@tonic-gate 	switch (sc_cmd) {
8197c478bd9Sstevel@tonic-gate 	case SC_GETNSWP:
82000792c0bS 		if (global)
82100792c0bS 			*rv = nswapfiles;
82200792c0bS 		else
82300792c0bS 			*rv = 1;
8247c478bd9Sstevel@tonic-gate 		return (0);
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	case SC_AINFO:
8277c478bd9Sstevel@tonic-gate 		/*
8287c478bd9Sstevel@tonic-gate 		 * Return anoninfo information with these changes:
8297c478bd9Sstevel@tonic-gate 		 * ani_max = maximum amount of swap space
8307c478bd9Sstevel@tonic-gate 		 *	(including potentially available physical memory)
8317c478bd9Sstevel@tonic-gate 		 * ani_free = amount of unallocated anonymous memory
8327c478bd9Sstevel@tonic-gate 		 *	(some of which might be reserved and including
8337c478bd9Sstevel@tonic-gate 		 *	 potentially available physical memory)
8347c478bd9Sstevel@tonic-gate 		 * ani_resv = amount of claimed (reserved) anonymous memory
8357c478bd9Sstevel@tonic-gate 		 */
8367c478bd9Sstevel@tonic-gate 		avail = MAX((spgcnt_t)(availrmem - swapfs_minfree), 0);
8377c478bd9Sstevel@tonic-gate 		s = (k_anoninfo.ani_max + k_anoninfo.ani_mem_resv) + avail;
8387c478bd9Sstevel@tonic-gate 		if (s > UINT32_MAX)
8397c478bd9Sstevel@tonic-gate 			return (EOVERFLOW);
8407c478bd9Sstevel@tonic-gate 		ai.ani_max = s;
8417c478bd9Sstevel@tonic-gate 
842b52a336eSPavel Tatashin 		/* Update ani_free */
843b52a336eSPavel Tatashin 		set_anoninfo();
8447c478bd9Sstevel@tonic-gate 		s = k_anoninfo.ani_free + avail;
8457c478bd9Sstevel@tonic-gate 		if (s > UINT32_MAX)
8467c478bd9Sstevel@tonic-gate 			return (EOVERFLOW);
8477c478bd9Sstevel@tonic-gate 		ai.ani_free = s;
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 		s = k_anoninfo.ani_phys_resv + k_anoninfo.ani_mem_resv;
8507c478bd9Sstevel@tonic-gate 		if (s > UINT32_MAX)
8517c478bd9Sstevel@tonic-gate 			return (EOVERFLOW);
8527c478bd9Sstevel@tonic-gate 		ai.ani_resv = s;
8537c478bd9Sstevel@tonic-gate 
85400792c0bS 		if (!global && zp->zone_max_swap_ctl != UINT64_MAX) {
85500792c0bS 			/*
85600792c0bS 			 * We're in a non-global zone with a swap cap.  We
85700792c0bS 			 * always report the system-wide values for the global
85800792c0bS 			 * zone, even though it too can have a swap cap.
85900792c0bS 			 * See the comment for the SC_AINFO case in swapctl()
86000792c0bS 			 * which explains the following logic.
86100792c0bS 			 */
86200792c0bS 			rctl_qty_t cap, used;
86300792c0bS 			pgcnt_t pgcap, sys_avail;
86400792c0bS 
86500792c0bS 			mutex_enter(&zp->zone_mem_lock);
86600792c0bS 			cap = zp->zone_max_swap_ctl;
86700792c0bS 			used = zp->zone_max_swap;
86800792c0bS 			mutex_exit(&zp->zone_mem_lock);
86900792c0bS 
87000792c0bS 			pgcap = MIN(btop(cap), ai.ani_max);
87100792c0bS 			ai.ani_free = pgcap - btop(used);
87200792c0bS 
87300792c0bS 			/* Get the system-wide swap currently available. */
87400792c0bS 			sys_avail = ai.ani_max - ai.ani_resv;
87500792c0bS 			if (sys_avail < ai.ani_free)
87600792c0bS 				ai.ani_resv = pgcap - sys_avail;
87700792c0bS 			else
87800792c0bS 				ai.ani_resv = btop(used);
87900792c0bS 
88000792c0bS 			ai.ani_max = pgcap;
88100792c0bS 		}
88200792c0bS 
8837c478bd9Sstevel@tonic-gate 		if (copyout(&ai, sc_arg, sizeof (ai)) != 0)
8847c478bd9Sstevel@tonic-gate 			return (EFAULT);
8857c478bd9Sstevel@tonic-gate 		return (0);
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	case SC_LIST:
8887c478bd9Sstevel@tonic-gate 		if (copyin(sc_arg, &length, sizeof (int32_t)) != 0)
8897c478bd9Sstevel@tonic-gate 			return (EFAULT);
89000792c0bS 		if (!global) {
89100792c0bS 			struct swapent32 st;
89200792c0bS 			char *swappath = "swap";
89300792c0bS 
89400792c0bS 			if (length < 1)
89500792c0bS 				return (ENOMEM);
89600792c0bS 			ust = (swapent32_t *)((swaptbl32_t *)sc_arg)->swt_ent;
89700792c0bS 			if (copyin(ust, &st, sizeof (swapent32_t)) != 0)
89800792c0bS 				return (EFAULT);
89900792c0bS 			st.ste_start = PAGESIZE >> SCTRSHFT;
90000792c0bS 			st.ste_length = (off_t)0;
90100792c0bS 			st.ste_pages = 0;
90200792c0bS 			st.ste_free = 0;
90300792c0bS 			st.ste_flags = 0;
90400792c0bS 
90500792c0bS 			mutex_enter(&swapinfo_lock);
90600792c0bS 			for (sip = swapinfo, nswap = 0;
90700792c0bS 			    sip != NULL && nswap < nswapfiles;
90800792c0bS 			    sip = sip->si_next, nswap++) {
90900792c0bS 				st.ste_length +=
91000792c0bS 				    (sip->si_eoff - sip->si_soff) >> SCTRSHFT;
91100792c0bS 				st.ste_pages += sip->si_npgs;
91200792c0bS 				st.ste_free += sip->si_nfpgs;
91300792c0bS 			}
91400792c0bS 			mutex_exit(&swapinfo_lock);
91500792c0bS 
91600792c0bS 			if (zp->zone_max_swap_ctl != UINT64_MAX) {
91700792c0bS 				rctl_qty_t cap, used;
91800792c0bS 
91900792c0bS 				mutex_enter(&zp->zone_mem_lock);
92000792c0bS 				cap = zp->zone_max_swap_ctl;
92100792c0bS 				used = zp->zone_max_swap;
92200792c0bS 				mutex_exit(&zp->zone_mem_lock);
92300792c0bS 
92400792c0bS 				st.ste_length = MIN(cap, st.ste_length);
92500792c0bS 				st.ste_pages = MIN(btop(cap), st.ste_pages);
92600792c0bS 				st.ste_free = MIN(st.ste_pages - btop(used),
92700792c0bS 				    st.ste_free);
92800792c0bS 			}
92900792c0bS 
93000792c0bS 			if (copyout(&st, ust, sizeof (swapent32_t)) != 0 ||
93100792c0bS 			    copyout(swappath, (caddr_t)(uintptr_t)st.ste_path,
93200792c0bS 			    strlen(swappath) + 1) != 0) {
93300792c0bS 				return (EFAULT);
93400792c0bS 			}
93500792c0bS 			*rv = 1;
93600792c0bS 			return (0);
93700792c0bS 		}
9387c478bd9Sstevel@tonic-gate beginning:
939e1936e20SJoshua M. Clulow 		mutex_enter(&swapinfo_lock);
9407c478bd9Sstevel@tonic-gate 		tmp_nswapfiles = nswapfiles;
941e1936e20SJoshua M. Clulow 		mutex_exit(&swapinfo_lock);
942e1936e20SJoshua M. Clulow 
943e1936e20SJoshua M. Clulow 		/*
944e1936e20SJoshua M. Clulow 		 * Return early if there are no swap entries to report:
945e1936e20SJoshua M. Clulow 		 */
946e1936e20SJoshua M. Clulow 		if (tmp_nswapfiles < 1) {
947e1936e20SJoshua M. Clulow 			*rv = 0;
948e1936e20SJoshua M. Clulow 			return (0);
949e1936e20SJoshua M. Clulow 		}
950e1936e20SJoshua M. Clulow 
9517c478bd9Sstevel@tonic-gate 		/* Return an error if not enough space for the whole table. */
9527c478bd9Sstevel@tonic-gate 		if (length < tmp_nswapfiles)
9537c478bd9Sstevel@tonic-gate 			return (ENOMEM);
9547c478bd9Sstevel@tonic-gate 		/*
9557c478bd9Sstevel@tonic-gate 		 * Get memory to hold the swap entries and their names. We'll
9567c478bd9Sstevel@tonic-gate 		 * copy the real entries into these and then copy these out.
9577c478bd9Sstevel@tonic-gate 		 * Allocating the pathname memory is only a guess so we may
9587c478bd9Sstevel@tonic-gate 		 * find that we need more and have to do it again.
9597c478bd9Sstevel@tonic-gate 		 * All this is because we have to hold the anon lock while
9607c478bd9Sstevel@tonic-gate 		 * traversing the swapinfo list, and we can't be doing copyouts
9617c478bd9Sstevel@tonic-gate 		 * and/or kmem_alloc()s during this.
9627c478bd9Sstevel@tonic-gate 		 */
9637c478bd9Sstevel@tonic-gate 		csip = kmem_zalloc(tmp_nswapfiles * sizeof (*csip), KM_SLEEP);
9647c478bd9Sstevel@tonic-gate retry:
9657c478bd9Sstevel@tonic-gate 		nlen = tmp_nswapfiles * (gplen += 100);
9667c478bd9Sstevel@tonic-gate 		pname = kmem_zalloc(nlen, KM_SLEEP);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 		mutex_enter(&swapinfo_lock);
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 		if (tmp_nswapfiles != nswapfiles) {
9717c478bd9Sstevel@tonic-gate 			mutex_exit(&swapinfo_lock);
9727c478bd9Sstevel@tonic-gate 			kmem_free(pname, nlen);
9737c478bd9Sstevel@tonic-gate 			kmem_free(csip, tmp_nswapfiles * sizeof (*csip));
9747c478bd9Sstevel@tonic-gate 			gplen = 0;
9757c478bd9Sstevel@tonic-gate 			goto beginning;
9767c478bd9Sstevel@tonic-gate 		}
9777c478bd9Sstevel@tonic-gate 		for (sip = swapinfo, tsip = csip, tpname = pname, nswap = 0;
9787c478bd9Sstevel@tonic-gate 		    (sip != NULL) && (nswap < tmp_nswapfiles);
9797c478bd9Sstevel@tonic-gate 		    sip = sip->si_next, tsip++, tpname += plen, nswap++) {
9807c478bd9Sstevel@tonic-gate 			plen = sip->si_pnamelen;
9817c478bd9Sstevel@tonic-gate 			if (tpname + plen - pname > nlen) {
9827c478bd9Sstevel@tonic-gate 				mutex_exit(&swapinfo_lock);
9837c478bd9Sstevel@tonic-gate 				kmem_free(pname, nlen);
9847c478bd9Sstevel@tonic-gate 				goto retry;
9857c478bd9Sstevel@tonic-gate 			}
9867c478bd9Sstevel@tonic-gate 			*tsip = *sip;
9877c478bd9Sstevel@tonic-gate 			tsip->si_pname = tpname;
9887c478bd9Sstevel@tonic-gate 			(void) strcpy(tsip->si_pname, sip->si_pname);
9897c478bd9Sstevel@tonic-gate 		}
9907c478bd9Sstevel@tonic-gate 		mutex_exit(&swapinfo_lock);
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 		if (sip != NULL) {
9937c478bd9Sstevel@tonic-gate 			error = ENOMEM;
9947c478bd9Sstevel@tonic-gate 			goto lout;
9957c478bd9Sstevel@tonic-gate 		}
9967c478bd9Sstevel@tonic-gate 		ust = (swapent32_t *)((swaptbl32_t *)sc_arg)->swt_ent;
9977c478bd9Sstevel@tonic-gate 		for (tsip = csip, cnt = 0; cnt < nswap;  tsip++, ust++, cnt++) {
9987c478bd9Sstevel@tonic-gate 			if (copyin(ust, &st, sizeof (*ust)) != 0) {
9997c478bd9Sstevel@tonic-gate 				error = EFAULT;
10007c478bd9Sstevel@tonic-gate 				goto lout;
10017c478bd9Sstevel@tonic-gate 			}
10027c478bd9Sstevel@tonic-gate 			st.ste_flags = tsip->si_flags;
10037c478bd9Sstevel@tonic-gate 			st.ste_length =
10047c478bd9Sstevel@tonic-gate 			    (tsip->si_eoff - tsip->si_soff) >> SCTRSHFT;
10057c478bd9Sstevel@tonic-gate 			st.ste_start = tsip->si_soff >> SCTRSHFT;
10067c478bd9Sstevel@tonic-gate 			st.ste_pages = tsip->si_npgs;
10077c478bd9Sstevel@tonic-gate 			st.ste_free = tsip->si_nfpgs;
10087c478bd9Sstevel@tonic-gate 			if (copyout(&st, ust, sizeof (st)) != 0) {
10097c478bd9Sstevel@tonic-gate 				error = EFAULT;
10107c478bd9Sstevel@tonic-gate 				goto lout;
10117c478bd9Sstevel@tonic-gate 			}
10127c478bd9Sstevel@tonic-gate 			if (!tsip->si_pnamelen)
10137c478bd9Sstevel@tonic-gate 				continue;
10147c478bd9Sstevel@tonic-gate 			if (copyout(tsip->si_pname,
10157c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)st.ste_path,
10167c478bd9Sstevel@tonic-gate 			    tsip->si_pnamelen) != 0) {
10177c478bd9Sstevel@tonic-gate 				error = EFAULT;
10187c478bd9Sstevel@tonic-gate 				goto lout;
10197c478bd9Sstevel@tonic-gate 			}
10207c478bd9Sstevel@tonic-gate 		}
10217c478bd9Sstevel@tonic-gate 		*rv = nswap;
10227c478bd9Sstevel@tonic-gate lout:
10237c478bd9Sstevel@tonic-gate 		kmem_free(csip, tmp_nswapfiles * sizeof (*csip));
10247c478bd9Sstevel@tonic-gate 		kmem_free(pname, nlen);
10257c478bd9Sstevel@tonic-gate 		return (error);
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	case SC_ADD:
10287c478bd9Sstevel@tonic-gate 	case SC_REMOVE:
10297c478bd9Sstevel@tonic-gate 		break;
10307c478bd9Sstevel@tonic-gate 	default:
10317c478bd9Sstevel@tonic-gate 		return (EINVAL);
10327c478bd9Sstevel@tonic-gate 	}
10337c478bd9Sstevel@tonic-gate 	if ((error = secpolicy_swapctl(CRED())) != 0)
10347c478bd9Sstevel@tonic-gate 		return (error);
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	if (copyin(sc_arg, &sr, sizeof (sr)))
10377c478bd9Sstevel@tonic-gate 		return (EFAULT);
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	/* Allocate the space to read in pathname */
10407c478bd9Sstevel@tonic-gate 	if ((swapname = kmem_alloc(MAXPATHLEN, KM_NOSLEEP)) == NULL)
10417c478bd9Sstevel@tonic-gate 		return (ENOMEM);
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	error = copyinstr((caddr_t)(uintptr_t)sr.sr_name,
10447c478bd9Sstevel@tonic-gate 	    swapname, MAXPATHLEN, NULL);
10457c478bd9Sstevel@tonic-gate 	if (error)
10467c478bd9Sstevel@tonic-gate 		goto out;
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	error = lookupname(swapname, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
10497c478bd9Sstevel@tonic-gate 	if (error) {
10507c478bd9Sstevel@tonic-gate 		if (sc_cmd == SC_ADD)
10517c478bd9Sstevel@tonic-gate 			goto out;
10527c478bd9Sstevel@tonic-gate 		/* see if we match by name */
10537c478bd9Sstevel@tonic-gate 		vp = swapdel_byname(swapname, (uint_t)sr.sr_start);
10547c478bd9Sstevel@tonic-gate 		if (vp == NULL)
10557c478bd9Sstevel@tonic-gate 			goto out;
10567c478bd9Sstevel@tonic-gate 	}
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	if (vp->v_flag & (VNOMAP | VNOSWAP)) {
10597c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
10607c478bd9Sstevel@tonic-gate 		error = ENOSYS;
10617c478bd9Sstevel@tonic-gate 		goto out;
10627c478bd9Sstevel@tonic-gate 	}
10637c478bd9Sstevel@tonic-gate 	switch (vp->v_type) {
10647c478bd9Sstevel@tonic-gate 	case VBLK:
10657c478bd9Sstevel@tonic-gate 		break;
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	case VREG:
10687c478bd9Sstevel@tonic-gate 		if (vp->v_vfsp && vn_is_readonly(vp))
10697c478bd9Sstevel@tonic-gate 			error = EROFS;
10707c478bd9Sstevel@tonic-gate 		else
1071da6c28aaSamw 			error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL);
10727c478bd9Sstevel@tonic-gate 		break;
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	case VDIR:
10757c478bd9Sstevel@tonic-gate 		error = EISDIR;
10767c478bd9Sstevel@tonic-gate 		break;
10777c478bd9Sstevel@tonic-gate 	default:
10787c478bd9Sstevel@tonic-gate 		error = ENOSYS;
10797c478bd9Sstevel@tonic-gate 		break;
10807c478bd9Sstevel@tonic-gate 	}
10817c478bd9Sstevel@tonic-gate 	if (error == 0) {
10827c478bd9Sstevel@tonic-gate 		if (sc_cmd == SC_REMOVE)
10837c478bd9Sstevel@tonic-gate 			error = swapdel(vp, sr.sr_start);
10847c478bd9Sstevel@tonic-gate 		else
10857c478bd9Sstevel@tonic-gate 			error = swapadd(vp, sr.sr_start, sr.sr_length,
10867c478bd9Sstevel@tonic-gate 			    swapname);
10877c478bd9Sstevel@tonic-gate 	}
10887c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
10897c478bd9Sstevel@tonic-gate out:
10907c478bd9Sstevel@tonic-gate 	kmem_free(swapname, MAXPATHLEN);
10917c478bd9Sstevel@tonic-gate 	return (error);
10927c478bd9Sstevel@tonic-gate }
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate #endif /* _LP64 && _SYSCALL32 */
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate /*
10977c478bd9Sstevel@tonic-gate  * Add a new swap file.
10987c478bd9Sstevel@tonic-gate  */
10997c478bd9Sstevel@tonic-gate int
swapadd(struct vnode * vp,ulong_t lowblk,ulong_t nblks,char * swapname)11007c478bd9Sstevel@tonic-gate swapadd(struct vnode *vp, ulong_t lowblk, ulong_t nblks, char *swapname)
11017c478bd9Sstevel@tonic-gate {
11027c478bd9Sstevel@tonic-gate 	struct swapinfo **sipp, *nsip = NULL, *esip = NULL;
11037c478bd9Sstevel@tonic-gate 	struct vnode *cvp;
11047c478bd9Sstevel@tonic-gate 	struct vattr vattr;
11057c478bd9Sstevel@tonic-gate 	pgcnt_t pages;
11067c478bd9Sstevel@tonic-gate 	u_offset_t soff, eoff;
11077c478bd9Sstevel@tonic-gate 	int error;
11087c478bd9Sstevel@tonic-gate 	ssize_t i, start, end;
11097c478bd9Sstevel@tonic-gate 	ushort_t wasswap;
11107c478bd9Sstevel@tonic-gate 	ulong_t startblk;
11117c478bd9Sstevel@tonic-gate 	size_t	returned_mem;
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	SWAP_PRINT(SW_CTL, "swapadd: vp %p lowblk %ld nblks %ld swapname %s\n",
11147c478bd9Sstevel@tonic-gate 	    vp, lowblk, nblks, swapname, 0);
11157c478bd9Sstevel@tonic-gate 	/*
11167c478bd9Sstevel@tonic-gate 	 * Get the real vnode. (If vp is not a specnode it just returns vp, so
11177c478bd9Sstevel@tonic-gate 	 * it does the right thing, but having this code know about specnodes
11187c478bd9Sstevel@tonic-gate 	 * violates the spirit of having it be indepedent of vnode type.)
11197c478bd9Sstevel@tonic-gate 	 */
11207c478bd9Sstevel@tonic-gate 	cvp = common_specvp(vp);
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	/*
11237c478bd9Sstevel@tonic-gate 	 * Or in VISSWAP so file system has chance to deny swap-ons during open.
11247c478bd9Sstevel@tonic-gate 	 */
11257c478bd9Sstevel@tonic-gate 	mutex_enter(&cvp->v_lock);
11267c478bd9Sstevel@tonic-gate 	wasswap = cvp->v_flag & VISSWAP;
11277c478bd9Sstevel@tonic-gate 	cvp->v_flag |= VISSWAP;
11287c478bd9Sstevel@tonic-gate 	mutex_exit(&cvp->v_lock);
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	mutex_enter(&swap_lock);
1131da6c28aaSamw 	if (error = VOP_OPEN(&cvp, FREAD|FWRITE, CRED(), NULL)) {
11327c478bd9Sstevel@tonic-gate 		mutex_exit(&swap_lock);
11337c478bd9Sstevel@tonic-gate 		/* restore state of v_flag */
11347c478bd9Sstevel@tonic-gate 		if (!wasswap) {
11357c478bd9Sstevel@tonic-gate 			mutex_enter(&cvp->v_lock);
11367c478bd9Sstevel@tonic-gate 			cvp->v_flag &= ~VISSWAP;
11377c478bd9Sstevel@tonic-gate 			mutex_exit(&cvp->v_lock);
11387c478bd9Sstevel@tonic-gate 		}
11397c478bd9Sstevel@tonic-gate 		return (error);
11407c478bd9Sstevel@tonic-gate 	}
11417c478bd9Sstevel@tonic-gate 	mutex_exit(&swap_lock);
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	/*
11447c478bd9Sstevel@tonic-gate 	 * Get partition size. Return error if empty partition,
11457c478bd9Sstevel@tonic-gate 	 * or if request does not fit within the partition.
11467c478bd9Sstevel@tonic-gate 	 * If this is the first swap device, we can reduce
11477c478bd9Sstevel@tonic-gate 	 * the size of the swap area to match what is
11487c478bd9Sstevel@tonic-gate 	 * available.  This can happen if the system was built
11497c478bd9Sstevel@tonic-gate 	 * on a machine with a different size swap partition.
11507c478bd9Sstevel@tonic-gate 	 */
11517c478bd9Sstevel@tonic-gate 	vattr.va_mask = AT_SIZE;
1152da6c28aaSamw 	if (error = VOP_GETATTR(cvp, &vattr, ATTR_COMM, CRED(), NULL))
11537c478bd9Sstevel@tonic-gate 		goto out;
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	/*
11567c478bd9Sstevel@tonic-gate 	 * Specfs returns a va_size of MAXOFFSET_T (UNKNOWN_SIZE) when the
11577c478bd9Sstevel@tonic-gate 	 * size of the device can't be determined.
11587c478bd9Sstevel@tonic-gate 	 */
11597c478bd9Sstevel@tonic-gate 	if ((vattr.va_size == 0) || (vattr.va_size == MAXOFFSET_T)) {
11607c478bd9Sstevel@tonic-gate 		error = EINVAL;
11617c478bd9Sstevel@tonic-gate 		goto out;
11627c478bd9Sstevel@tonic-gate 	}
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate #ifdef	_ILP32
11657c478bd9Sstevel@tonic-gate 	/*
11667c478bd9Sstevel@tonic-gate 	 * No support for large swap in 32-bit OS, if the size of the swap is
11677c478bd9Sstevel@tonic-gate 	 * bigger than MAXOFF32_T then the size used by swapfs must be limited.
11687c478bd9Sstevel@tonic-gate 	 * This limitation is imposed by the swap subsystem itself, a D_64BIT
11697c478bd9Sstevel@tonic-gate 	 * driver as the target of swap operation should be able to field
11707c478bd9Sstevel@tonic-gate 	 * the IO.
11717c478bd9Sstevel@tonic-gate 	 */
11727c478bd9Sstevel@tonic-gate 	if (vattr.va_size > MAXOFF32_T) {
11737c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE,
117420a2d3f6Sstans 		    "!swap device %s truncated from 0x%llx to 0x%x bytes",
117520a2d3f6Sstans 		    swapname, vattr.va_size, MAXOFF32_T);
11767c478bd9Sstevel@tonic-gate 		vattr.va_size = MAXOFF32_T;
11777c478bd9Sstevel@tonic-gate 	}
11787c478bd9Sstevel@tonic-gate #endif	/* _ILP32 */
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 	/* Fail if file not writeable (try to set size to current size) */
11817c478bd9Sstevel@tonic-gate 	vattr.va_mask = AT_SIZE;
11827c478bd9Sstevel@tonic-gate 	if (error = VOP_SETATTR(cvp, &vattr, 0, CRED(), NULL))
11837c478bd9Sstevel@tonic-gate 		goto out;
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 	/* Fail if fs does not support VOP_PAGEIO */
1186da6c28aaSamw 	error = VOP_PAGEIO(cvp, (page_t *)NULL, (u_offset_t)0, 0, 0, CRED(),
118720a2d3f6Sstans 	    NULL);
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	if (error == ENOSYS)
11907c478bd9Sstevel@tonic-gate 		goto out;
11917c478bd9Sstevel@tonic-gate 	else
11927c478bd9Sstevel@tonic-gate 		error = 0;
11937c478bd9Sstevel@tonic-gate 	/*
11947c478bd9Sstevel@tonic-gate 	 * If swapping on the root filesystem don't put swap blocks that
11957c478bd9Sstevel@tonic-gate 	 * correspond to the miniroot filesystem on the swap free list.
11967c478bd9Sstevel@tonic-gate 	 */
11977c478bd9Sstevel@tonic-gate 	if (cvp == rootdir)
11987c478bd9Sstevel@tonic-gate 		startblk = roundup(MINIROOTSIZE<<SCTRSHFT, klustsize)>>SCTRSHFT;
11997c478bd9Sstevel@tonic-gate 	else				/* Skip 1st page (disk label) */
12007c478bd9Sstevel@tonic-gate 		startblk = (ulong_t)(lowblk ? lowblk : 1);
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	soff = startblk << SCTRSHFT;
12037c478bd9Sstevel@tonic-gate 	if (soff >= vattr.va_size) {
12047c478bd9Sstevel@tonic-gate 		error = EINVAL;
12057c478bd9Sstevel@tonic-gate 		goto out;
12067c478bd9Sstevel@tonic-gate 	}
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	/*
12097c478bd9Sstevel@tonic-gate 	 * If user specified 0 blks, use the size of the device
12107c478bd9Sstevel@tonic-gate 	 */
12117c478bd9Sstevel@tonic-gate 	eoff = nblks ?  soff + (nblks - (startblk - lowblk) << SCTRSHFT) :
121220a2d3f6Sstans 	    vattr.va_size;
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	SWAP_PRINT(SW_CTL, "swapadd: va_size %ld soff %ld eoff %ld\n",
12157c478bd9Sstevel@tonic-gate 	    vattr.va_size, soff, eoff, 0, 0);
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	if (eoff > vattr.va_size) {
12187c478bd9Sstevel@tonic-gate 		error = EINVAL;
12197c478bd9Sstevel@tonic-gate 		goto out;
12207c478bd9Sstevel@tonic-gate 	}
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 	/*
12237c478bd9Sstevel@tonic-gate 	 * The starting and ending offsets must be page aligned.
12247c478bd9Sstevel@tonic-gate 	 * Round soff up to next page boundary, round eoff
12257c478bd9Sstevel@tonic-gate 	 * down to previous page boundary.
12267c478bd9Sstevel@tonic-gate 	 */
12277c478bd9Sstevel@tonic-gate 	soff = ptob(btopr(soff));
12287c478bd9Sstevel@tonic-gate 	eoff = ptob(btop(eoff));
12297c478bd9Sstevel@tonic-gate 	if (soff >= eoff) {
12307c478bd9Sstevel@tonic-gate 		SWAP_PRINT(SW_CTL, "swapadd: soff %ld >= eoff %ld\n",
12317c478bd9Sstevel@tonic-gate 		    soff, eoff, 0, 0, 0);
12327c478bd9Sstevel@tonic-gate 		error = EINVAL;
12337c478bd9Sstevel@tonic-gate 		goto out;
12347c478bd9Sstevel@tonic-gate 	}
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	pages = btop(eoff - soff);
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	/* Allocate and partially set up the new swapinfo */
12397c478bd9Sstevel@tonic-gate 	nsip = kmem_zalloc(sizeof (struct swapinfo), KM_SLEEP);
12407c478bd9Sstevel@tonic-gate 	nsip->si_vp = cvp;
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 	nsip->si_soff = soff;
12437c478bd9Sstevel@tonic-gate 	nsip->si_eoff = eoff;
12447c478bd9Sstevel@tonic-gate 	nsip->si_hint = 0;
12457c478bd9Sstevel@tonic-gate 	nsip->si_checkcnt = nsip->si_alloccnt = 0;
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	nsip->si_pnamelen = (int)strlen(swapname) + 1;
12487c478bd9Sstevel@tonic-gate 	nsip->si_pname = (char *)kmem_zalloc(nsip->si_pnamelen, KM_SLEEP);
12497c478bd9Sstevel@tonic-gate 	bcopy(swapname, nsip->si_pname, nsip->si_pnamelen - 1);
12507c478bd9Sstevel@tonic-gate 	SWAP_PRINT(SW_CTL, "swapadd: allocating swapinfo for %s, %ld pages\n",
12517c478bd9Sstevel@tonic-gate 	    swapname, pages, 0, 0, 0);
12527c478bd9Sstevel@tonic-gate 	/*
12537c478bd9Sstevel@tonic-gate 	 * Size of swapslots map in bytes
12547c478bd9Sstevel@tonic-gate 	 */
12557c478bd9Sstevel@tonic-gate 	nsip->si_mapsize = P2ROUNDUP(pages, NBBW) / NBBY;
12567c478bd9Sstevel@tonic-gate 	nsip->si_swapslots = kmem_zalloc(nsip->si_mapsize, KM_SLEEP);
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	/*
12597c478bd9Sstevel@tonic-gate 	 * Permanently set the bits that can't ever be allocated,
12607c478bd9Sstevel@tonic-gate 	 * i.e. those from the ending offset to the round up slot for the
12617c478bd9Sstevel@tonic-gate 	 * swapslots bit map.
12627c478bd9Sstevel@tonic-gate 	 */
12637c478bd9Sstevel@tonic-gate 	start = pages;
12647c478bd9Sstevel@tonic-gate 	end = P2ROUNDUP(pages, NBBW);
12657c478bd9Sstevel@tonic-gate 	for (i = start; i < end; i++) {
12667c478bd9Sstevel@tonic-gate 		SWAP_PRINT(SW_CTL, "swapadd: set bit for page %ld\n", i,
12677c478bd9Sstevel@tonic-gate 		    0, 0, 0, 0);
12687c478bd9Sstevel@tonic-gate 		SETBIT(nsip->si_swapslots, i);
12697c478bd9Sstevel@tonic-gate 	}
12707c478bd9Sstevel@tonic-gate 	nsip->si_npgs = nsip->si_nfpgs = pages;
12717c478bd9Sstevel@tonic-gate 	/*
12727c478bd9Sstevel@tonic-gate 	 * Now check to see if we can add it. We wait til now to check because
12737c478bd9Sstevel@tonic-gate 	 * we need the swapinfo_lock and we don't want sleep with it (e.g.,
12747c478bd9Sstevel@tonic-gate 	 * during kmem_alloc()) while we're setting up the swapinfo.
12757c478bd9Sstevel@tonic-gate 	 */
12767c478bd9Sstevel@tonic-gate 	mutex_enter(&swapinfo_lock);
12777c478bd9Sstevel@tonic-gate 	for (sipp = &swapinfo; (esip = *sipp) != NULL; sipp = &esip->si_next) {
12787c478bd9Sstevel@tonic-gate 		if (esip->si_vp == cvp) {
12797c478bd9Sstevel@tonic-gate 			if (esip->si_soff == soff && esip->si_npgs == pages &&
12807c478bd9Sstevel@tonic-gate 			    (esip->si_flags & ST_DOINGDEL)) {
12817c478bd9Sstevel@tonic-gate 				/*
12827c478bd9Sstevel@tonic-gate 				 * We are adding a device that we are in the
12837c478bd9Sstevel@tonic-gate 				 * middle of deleting. Just clear the
12847c478bd9Sstevel@tonic-gate 				 * ST_DOINGDEL flag to signal this and
12857c478bd9Sstevel@tonic-gate 				 * the deletion routine will eventually notice
12867c478bd9Sstevel@tonic-gate 				 * it and add it back.
12877c478bd9Sstevel@tonic-gate 				 */
12887c478bd9Sstevel@tonic-gate 				esip->si_flags &= ~ST_DOINGDEL;
12897c478bd9Sstevel@tonic-gate 				mutex_exit(&swapinfo_lock);
12907c478bd9Sstevel@tonic-gate 				goto out;
12917c478bd9Sstevel@tonic-gate 			}
12927c478bd9Sstevel@tonic-gate 			/* disallow overlapping swap files */
12937c478bd9Sstevel@tonic-gate 			if ((soff < esip->si_eoff) && (eoff > esip->si_soff)) {
12947c478bd9Sstevel@tonic-gate 				error = EEXIST;
12957c478bd9Sstevel@tonic-gate 				mutex_exit(&swapinfo_lock);
12967c478bd9Sstevel@tonic-gate 				goto out;
12977c478bd9Sstevel@tonic-gate 			}
12987c478bd9Sstevel@tonic-gate 		}
12997c478bd9Sstevel@tonic-gate 	}
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 	nswapfiles++;
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	/*
13047c478bd9Sstevel@tonic-gate 	 * add new swap device to list and shift allocations to it
13057c478bd9Sstevel@tonic-gate 	 * before updating the anoninfo counters
13067c478bd9Sstevel@tonic-gate 	 */
13077c478bd9Sstevel@tonic-gate 	*sipp = nsip;
13087c478bd9Sstevel@tonic-gate 	silast = nsip;
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	/*
13117c478bd9Sstevel@tonic-gate 	 * Update the total amount of reservable swap space
13127c478bd9Sstevel@tonic-gate 	 * accounting properly for swap space from physical memory
13137c478bd9Sstevel@tonic-gate 	 */
13147c478bd9Sstevel@tonic-gate 	/* New swap device soaks up currently reserved memory swap */
13157c478bd9Sstevel@tonic-gate 	mutex_enter(&anoninfo_lock);
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
13187c478bd9Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	k_anoninfo.ani_max += pages;
13217c478bd9Sstevel@tonic-gate 	ANI_ADD(pages);
13227c478bd9Sstevel@tonic-gate 	if (k_anoninfo.ani_mem_resv > k_anoninfo.ani_locked_swap) {
13237c478bd9Sstevel@tonic-gate 		returned_mem = MIN(k_anoninfo.ani_mem_resv -
13247c478bd9Sstevel@tonic-gate 		    k_anoninfo.ani_locked_swap,
13257c478bd9Sstevel@tonic-gate 		    k_anoninfo.ani_max - k_anoninfo.ani_phys_resv);
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 		ANI_ADD(-returned_mem);
13287c478bd9Sstevel@tonic-gate 		k_anoninfo.ani_free -= returned_mem;
13297c478bd9Sstevel@tonic-gate 		k_anoninfo.ani_mem_resv -= returned_mem;
13307c478bd9Sstevel@tonic-gate 		k_anoninfo.ani_phys_resv += returned_mem;
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 		mutex_enter(&freemem_lock);
13337c478bd9Sstevel@tonic-gate 		availrmem += returned_mem;
13347c478bd9Sstevel@tonic-gate 		mutex_exit(&freemem_lock);
13357c478bd9Sstevel@tonic-gate 	}
13367c478bd9Sstevel@tonic-gate 	/*
13377c478bd9Sstevel@tonic-gate 	 * At boot time, to permit booting small memory machines using
13387c478bd9Sstevel@tonic-gate 	 * only physical memory as swap space, we allowed a dangerously
13397c478bd9Sstevel@tonic-gate 	 * large amount of memory to be used as swap space; now that
13407c478bd9Sstevel@tonic-gate 	 * more physical backing store is available bump down the amount
13417c478bd9Sstevel@tonic-gate 	 * we can get from memory to a safer size.
13427c478bd9Sstevel@tonic-gate 	 */
13437c478bd9Sstevel@tonic-gate 	if (swapfs_minfree < swapfs_desfree) {
13447c478bd9Sstevel@tonic-gate 		mutex_enter(&freemem_lock);
13457c478bd9Sstevel@tonic-gate 		if (availrmem > swapfs_desfree || !k_anoninfo.ani_mem_resv)
13467c478bd9Sstevel@tonic-gate 			swapfs_minfree = swapfs_desfree;
13477c478bd9Sstevel@tonic-gate 		mutex_exit(&freemem_lock);
13487c478bd9Sstevel@tonic-gate 	}
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	SWAP_PRINT(SW_CTL, "swapadd: ani_max %ld ani_free %ld\n",
13517c478bd9Sstevel@tonic-gate 	    k_anoninfo.ani_free, k_anoninfo.ani_free, 0, 0, 0);
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 	mutex_exit(&anoninfo_lock);
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	mutex_exit(&swapinfo_lock);
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 	/* Initialize the dump device */
13587c478bd9Sstevel@tonic-gate 	mutex_enter(&dump_lock);
13597c478bd9Sstevel@tonic-gate 	if (dumpvp == NULL)
13607c478bd9Sstevel@tonic-gate 		(void) dumpinit(vp, swapname, 0);
13617c478bd9Sstevel@tonic-gate 	mutex_exit(&dump_lock);
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 	VN_HOLD(cvp);
13647c478bd9Sstevel@tonic-gate out:
13657c478bd9Sstevel@tonic-gate 	if (error || esip) {
13667c478bd9Sstevel@tonic-gate 		SWAP_PRINT(SW_CTL, "swapadd: error (%d)\n", error, 0, 0, 0, 0);
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 		if (!wasswap) {
13697c478bd9Sstevel@tonic-gate 			mutex_enter(&cvp->v_lock);
13707c478bd9Sstevel@tonic-gate 			cvp->v_flag &= ~VISSWAP;
13717c478bd9Sstevel@tonic-gate 			mutex_exit(&cvp->v_lock);
13727c478bd9Sstevel@tonic-gate 		}
13737c478bd9Sstevel@tonic-gate 		if (nsip) {
13747c478bd9Sstevel@tonic-gate 			kmem_free(nsip->si_swapslots, (size_t)nsip->si_mapsize);
13757c478bd9Sstevel@tonic-gate 			kmem_free(nsip->si_pname, nsip->si_pnamelen);
13767c478bd9Sstevel@tonic-gate 			kmem_free(nsip, sizeof (*nsip));
13777c478bd9Sstevel@tonic-gate 		}
13787c478bd9Sstevel@tonic-gate 		mutex_enter(&swap_lock);
1379da6c28aaSamw 		(void) VOP_CLOSE(cvp, FREAD|FWRITE, 1, (offset_t)0, CRED(),
138020a2d3f6Sstans 		    NULL);
13817c478bd9Sstevel@tonic-gate 		mutex_exit(&swap_lock);
13827c478bd9Sstevel@tonic-gate 	}
13837c478bd9Sstevel@tonic-gate 	return (error);
13847c478bd9Sstevel@tonic-gate }
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate /*
13877c478bd9Sstevel@tonic-gate  * Delete a swap file.
13887c478bd9Sstevel@tonic-gate  */
13897c478bd9Sstevel@tonic-gate static int
swapdel(struct vnode * vp,ulong_t lowblk)13907c478bd9Sstevel@tonic-gate swapdel(
13917c478bd9Sstevel@tonic-gate 	struct vnode *vp,
13927c478bd9Sstevel@tonic-gate 	ulong_t lowblk) /* Low block number of area to delete. */
13937c478bd9Sstevel@tonic-gate {
13947c478bd9Sstevel@tonic-gate 	struct swapinfo **sipp, *osip = NULL;
13957c478bd9Sstevel@tonic-gate 	struct vnode *cvp;
13967c478bd9Sstevel@tonic-gate 	u_offset_t soff;
13977c478bd9Sstevel@tonic-gate 	int error = 0;
13987c478bd9Sstevel@tonic-gate 	u_offset_t toff = 0;
13997c478bd9Sstevel@tonic-gate 	struct vnode *tvp = NULL;
14007c478bd9Sstevel@tonic-gate 	spgcnt_t pages;
14017c478bd9Sstevel@tonic-gate 	struct anon **app, *ap;
14027c478bd9Sstevel@tonic-gate 	kmutex_t *ahm;
14037c478bd9Sstevel@tonic-gate 	pgcnt_t adjust_swap = 0;
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	/* Find the swap file entry for the file to be deleted */
14067c478bd9Sstevel@tonic-gate 	cvp = common_specvp(vp);
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 
1409*6cdd461fSToomas Soome 	lowblk = lowblk ? lowblk : 1;	/* Skip first page (disk label) */
14107c478bd9Sstevel@tonic-gate 	soff = ptob(btopr(lowblk << SCTRSHFT)); /* must be page aligned */
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	mutex_enter(&swapinfo_lock);
14137c478bd9Sstevel@tonic-gate 	for (sipp = &swapinfo; (osip = *sipp) != NULL; sipp = &osip->si_next) {
14147c478bd9Sstevel@tonic-gate 		if ((osip->si_vp == cvp) &&
14157c478bd9Sstevel@tonic-gate 		    (osip->si_soff == soff) && (osip->si_flags == 0))
14167c478bd9Sstevel@tonic-gate 			break;
14177c478bd9Sstevel@tonic-gate 	}
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	/* If the file was not found, error.  */
14207c478bd9Sstevel@tonic-gate 	if (osip == NULL) {
14217c478bd9Sstevel@tonic-gate 		error = EINVAL;
14227c478bd9Sstevel@tonic-gate 		mutex_exit(&swapinfo_lock);
14237c478bd9Sstevel@tonic-gate 		goto out;
14247c478bd9Sstevel@tonic-gate 	}
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	pages = osip->si_npgs;
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	/*
14297c478bd9Sstevel@tonic-gate 	 * Do not delete if we will be low on swap pages.
14307c478bd9Sstevel@tonic-gate 	 */
14317c478bd9Sstevel@tonic-gate 	mutex_enter(&anoninfo_lock);
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
14347c478bd9Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	mutex_enter(&freemem_lock);
14377c478bd9Sstevel@tonic-gate 	if (((k_anoninfo.ani_max - k_anoninfo.ani_phys_resv) +
14387c478bd9Sstevel@tonic-gate 	    MAX((spgcnt_t)(availrmem - swapfs_minfree), 0)) < pages) {
14397c478bd9Sstevel@tonic-gate 		mutex_exit(&freemem_lock);
14407c478bd9Sstevel@tonic-gate 		mutex_exit(&anoninfo_lock);
14417c478bd9Sstevel@tonic-gate 		error = ENOMEM;
14427c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "swapdel - too few free pages");
14437c478bd9Sstevel@tonic-gate 		mutex_exit(&swapinfo_lock);
14447c478bd9Sstevel@tonic-gate 		goto out;
14457c478bd9Sstevel@tonic-gate 	}
14467c478bd9Sstevel@tonic-gate 	mutex_exit(&freemem_lock);
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	k_anoninfo.ani_max -= pages;
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 	/* If needed, reserve memory swap to replace old device */
14517c478bd9Sstevel@tonic-gate 	if (k_anoninfo.ani_phys_resv > k_anoninfo.ani_max) {
14527c478bd9Sstevel@tonic-gate 		adjust_swap = k_anoninfo.ani_phys_resv - k_anoninfo.ani_max;
14537c478bd9Sstevel@tonic-gate 		k_anoninfo.ani_phys_resv -= adjust_swap;
14547c478bd9Sstevel@tonic-gate 		k_anoninfo.ani_mem_resv += adjust_swap;
14557c478bd9Sstevel@tonic-gate 		mutex_enter(&freemem_lock);
14567c478bd9Sstevel@tonic-gate 		availrmem -= adjust_swap;
14577c478bd9Sstevel@tonic-gate 		mutex_exit(&freemem_lock);
14587c478bd9Sstevel@tonic-gate 		ANI_ADD(adjust_swap);
14597c478bd9Sstevel@tonic-gate 	}
14607c478bd9Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
14617c478bd9Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
14627c478bd9Sstevel@tonic-gate 	mutex_exit(&anoninfo_lock);
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 	ANI_ADD(-pages);
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 	/*
14677c478bd9Sstevel@tonic-gate 	 * Set the delete flag.  This prevents anyone from allocating more
14687c478bd9Sstevel@tonic-gate 	 * pages from this file. Also set ST_DOINGDEL. Someone who wants to
14697c478bd9Sstevel@tonic-gate 	 * add the file back while we're deleting it will signify by clearing
14707c478bd9Sstevel@tonic-gate 	 * this flag.
14717c478bd9Sstevel@tonic-gate 	 */
14727c478bd9Sstevel@tonic-gate 	osip->si_flags |= ST_INDEL|ST_DOINGDEL;
14737c478bd9Sstevel@tonic-gate 	mutex_exit(&swapinfo_lock);
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate 	/*
14767c478bd9Sstevel@tonic-gate 	 * Free all the allocated physical slots for this file. We do this
14777c478bd9Sstevel@tonic-gate 	 * by walking through the entire anon hash array, because we need
14787c478bd9Sstevel@tonic-gate 	 * to update all the anon slots that have physical swap slots on
14797c478bd9Sstevel@tonic-gate 	 * this file, and this is the only way to find them all. We go back
14807c478bd9Sstevel@tonic-gate 	 * to the beginning of a bucket after each slot is freed because the
14817c478bd9Sstevel@tonic-gate 	 * anonhash_lock is not held during the free and thus the hash table
14827c478bd9Sstevel@tonic-gate 	 * may change under us.
14837c478bd9Sstevel@tonic-gate 	 */
14847c478bd9Sstevel@tonic-gate 	for (app = anon_hash; app < &anon_hash[ANON_HASH_SIZE]; app++) {
148523d9e5acSMichael Corcoran 		ahm = &anonhash_lock[(app - anon_hash) &
148623d9e5acSMichael Corcoran 		    (AH_LOCK_SIZE - 1)].pad_mutex;
14877c478bd9Sstevel@tonic-gate 		mutex_enter(ahm);
14887c478bd9Sstevel@tonic-gate top:
14897c478bd9Sstevel@tonic-gate 		for (ap = *app; ap != NULL; ap = ap->an_hash) {
14907c478bd9Sstevel@tonic-gate 			if (ap->an_pvp == cvp &&
14917c478bd9Sstevel@tonic-gate 			    ap->an_poff >= osip->si_soff &&
14927c478bd9Sstevel@tonic-gate 			    ap->an_poff < osip->si_eoff) {
14937c478bd9Sstevel@tonic-gate 				ASSERT(TESTBIT(osip->si_swapslots,
14947c478bd9Sstevel@tonic-gate 				    btop((size_t)(ap->an_poff -
14957c478bd9Sstevel@tonic-gate 				    osip->si_soff))));
14967c478bd9Sstevel@tonic-gate 				tvp = ap->an_vp;
14977c478bd9Sstevel@tonic-gate 				toff = ap->an_off;
14987c478bd9Sstevel@tonic-gate 				VN_HOLD(tvp);
14997c478bd9Sstevel@tonic-gate 				mutex_exit(ahm);
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 				error = swapslot_free(tvp, toff, osip);
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 				VN_RELE(tvp);
15047c478bd9Sstevel@tonic-gate 				mutex_enter(ahm);
15057c478bd9Sstevel@tonic-gate 				if (!error && (osip->si_flags & ST_DOINGDEL)) {
15067c478bd9Sstevel@tonic-gate 					goto top;
15077c478bd9Sstevel@tonic-gate 				} else {
15087c478bd9Sstevel@tonic-gate 					if (error) {
15097c478bd9Sstevel@tonic-gate 						cmn_err(CE_WARN,
15107c478bd9Sstevel@tonic-gate 						    "swapslot_free failed %d",
15117c478bd9Sstevel@tonic-gate 						    error);
15127c478bd9Sstevel@tonic-gate 					}
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 					/*
15157c478bd9Sstevel@tonic-gate 					 * Add device back before making it
15167c478bd9Sstevel@tonic-gate 					 * visible.
15177c478bd9Sstevel@tonic-gate 					 */
15187c478bd9Sstevel@tonic-gate 					mutex_enter(&swapinfo_lock);
15197c478bd9Sstevel@tonic-gate 					osip->si_flags &=
15207c478bd9Sstevel@tonic-gate 					    ~(ST_INDEL | ST_DOINGDEL);
15217c478bd9Sstevel@tonic-gate 					mutex_exit(&swapinfo_lock);
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 					/*
15247c478bd9Sstevel@tonic-gate 					 * Update the anon space available
15257c478bd9Sstevel@tonic-gate 					 */
15267c478bd9Sstevel@tonic-gate 					mutex_enter(&anoninfo_lock);
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 					k_anoninfo.ani_phys_resv += adjust_swap;
15297c478bd9Sstevel@tonic-gate 					k_anoninfo.ani_mem_resv -= adjust_swap;
15307c478bd9Sstevel@tonic-gate 					k_anoninfo.ani_max += pages;
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 					mutex_enter(&freemem_lock);
15337c478bd9Sstevel@tonic-gate 					availrmem += adjust_swap;
15347c478bd9Sstevel@tonic-gate 					mutex_exit(&freemem_lock);
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 					mutex_exit(&anoninfo_lock);
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 					ANI_ADD(pages);
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 					mutex_exit(ahm);
15417c478bd9Sstevel@tonic-gate 					goto out;
15427c478bd9Sstevel@tonic-gate 				}
15437c478bd9Sstevel@tonic-gate 			}
15447c478bd9Sstevel@tonic-gate 		}
15457c478bd9Sstevel@tonic-gate 		mutex_exit(ahm);
15467c478bd9Sstevel@tonic-gate 	}
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	/* All done, they'd better all be free! */
15497c478bd9Sstevel@tonic-gate 	mutex_enter(&swapinfo_lock);
15507c478bd9Sstevel@tonic-gate 	ASSERT(osip->si_nfpgs == osip->si_npgs);
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 	/* Now remove it from the swapinfo list */
15537c478bd9Sstevel@tonic-gate 	for (sipp = &swapinfo; *sipp != NULL; sipp = &(*sipp)->si_next) {
15547c478bd9Sstevel@tonic-gate 		if (*sipp == osip)
15557c478bd9Sstevel@tonic-gate 			break;
15567c478bd9Sstevel@tonic-gate 	}
15577c478bd9Sstevel@tonic-gate 	ASSERT(*sipp);
15587c478bd9Sstevel@tonic-gate 	*sipp = osip->si_next;
15597c478bd9Sstevel@tonic-gate 	if (silast == osip)
15607c478bd9Sstevel@tonic-gate 		if ((silast = osip->si_next) == NULL)
15617c478bd9Sstevel@tonic-gate 			silast = swapinfo;
15627c478bd9Sstevel@tonic-gate 	nswapfiles--;
15637c478bd9Sstevel@tonic-gate 	mutex_exit(&swapinfo_lock);
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 	kmem_free(osip->si_swapslots, osip->si_mapsize);
15667c478bd9Sstevel@tonic-gate 	kmem_free(osip->si_pname, osip->si_pnamelen);
15677c478bd9Sstevel@tonic-gate 	kmem_free(osip, sizeof (*osip));
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 	mutex_enter(&dump_lock);
15707c478bd9Sstevel@tonic-gate 	if (cvp == dumpvp)
15717c478bd9Sstevel@tonic-gate 		dumpfini();
15727c478bd9Sstevel@tonic-gate 	mutex_exit(&dump_lock);
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 	/* Release the vnode */
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 	mutex_enter(&swap_lock);
1577da6c28aaSamw 	(void) VOP_CLOSE(cvp, FREAD|FWRITE, 1, (offset_t)0, CRED(), NULL);
15787c478bd9Sstevel@tonic-gate 	mutex_enter(&cvp->v_lock);
15797c478bd9Sstevel@tonic-gate 	cvp->v_flag &= ~VISSWAP;
15807c478bd9Sstevel@tonic-gate 	mutex_exit(&cvp->v_lock);
15817c478bd9Sstevel@tonic-gate 	VN_RELE(cvp);
15827c478bd9Sstevel@tonic-gate 	mutex_exit(&swap_lock);
15837c478bd9Sstevel@tonic-gate out:
15847c478bd9Sstevel@tonic-gate 	return (error);
15857c478bd9Sstevel@tonic-gate }
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate /*
15887c478bd9Sstevel@tonic-gate  * Free up a physical swap slot on swapinfo sip, currently in use by the
15897c478bd9Sstevel@tonic-gate  * anonymous page whose name is (vp, off).
15907c478bd9Sstevel@tonic-gate  */
15917c478bd9Sstevel@tonic-gate static int
swapslot_free(struct vnode * vp,u_offset_t off,struct swapinfo * sip)15927c478bd9Sstevel@tonic-gate swapslot_free(
15937c478bd9Sstevel@tonic-gate 	struct vnode *vp,
15947c478bd9Sstevel@tonic-gate 	u_offset_t off,
15957c478bd9Sstevel@tonic-gate 	struct swapinfo *sip)
15967c478bd9Sstevel@tonic-gate {
159720a2d3f6Sstans 	struct page *pp = NULL;
15987c478bd9Sstevel@tonic-gate 	struct anon *ap = NULL;
15997c478bd9Sstevel@tonic-gate 	int error = 0;
16007c478bd9Sstevel@tonic-gate 	kmutex_t *ahm;
160120a2d3f6Sstans 	struct vnode *pvp = NULL;
160220a2d3f6Sstans 	u_offset_t poff;
160320a2d3f6Sstans 	int	alloc_pg = 0;
16047c478bd9Sstevel@tonic-gate 
160520a2d3f6Sstans 	ASSERT(sip->si_vp != NULL);
16067c478bd9Sstevel@tonic-gate 	/*
160720a2d3f6Sstans 	 * Get the page for the old swap slot if exists or create a new one.
16087c478bd9Sstevel@tonic-gate 	 */
160920a2d3f6Sstans again:
161020a2d3f6Sstans 	if ((pp = page_lookup(vp, off, SE_SHARED)) == NULL) {
161120a2d3f6Sstans 		pp = page_create_va(vp, off, PAGESIZE, PG_WAIT | PG_EXCL,
161220a2d3f6Sstans 		    segkmap, NULL);
161320a2d3f6Sstans 		if (pp == NULL)
161420a2d3f6Sstans 			goto again;
161520a2d3f6Sstans 		alloc_pg = 1;
161620a2d3f6Sstans 
161720a2d3f6Sstans 		error = swap_getphysname(vp, off, &pvp, &poff);
161820a2d3f6Sstans 		if (error || pvp != sip->si_vp || poff < sip->si_soff ||
161920a2d3f6Sstans 		    poff >= sip->si_eoff) {
162020a2d3f6Sstans 			page_io_unlock(pp);
162120a2d3f6Sstans 			/*LINTED: constant in conditional context*/
162220a2d3f6Sstans 			VN_DISPOSE(pp, B_INVAL, 0, kcred);
162320a2d3f6Sstans 			return (0);
162420a2d3f6Sstans 		}
162520a2d3f6Sstans 
162620a2d3f6Sstans 		error = VOP_PAGEIO(pvp, pp, poff, PAGESIZE, B_READ,
162720a2d3f6Sstans 		    CRED(), NULL);
162820a2d3f6Sstans 		if (error) {
162920a2d3f6Sstans 			page_io_unlock(pp);
163020a2d3f6Sstans 			if (error == EFAULT)
163120a2d3f6Sstans 				error = 0;
163220a2d3f6Sstans 			/*LINTED: constant in conditional context*/
163320a2d3f6Sstans 			VN_DISPOSE(pp, B_INVAL, 0, kcred);
163420a2d3f6Sstans 			return (error);
163520a2d3f6Sstans 		}
16367c478bd9Sstevel@tonic-gate 	}
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 	/*
163920a2d3f6Sstans 	 * The anon could have been removed by anon_decref* and/or reallocated
164020a2d3f6Sstans 	 * by anon layer (an_pvp == NULL) with the same vp, off.
164120a2d3f6Sstans 	 * In this case the page which has been allocated needs to
164220a2d3f6Sstans 	 * be freed.
16437c478bd9Sstevel@tonic-gate 	 */
164420a2d3f6Sstans 	if (!alloc_pg)
164520a2d3f6Sstans 		page_io_lock(pp);
164623d9e5acSMichael Corcoran 	ahm = AH_MUTEX(vp, off);
164720a2d3f6Sstans 	mutex_enter(ahm);
164820a2d3f6Sstans 	ap = swap_anon(vp, off);
164920a2d3f6Sstans 	if ((ap == NULL || ap->an_pvp == NULL) && alloc_pg) {
165020a2d3f6Sstans 		mutex_exit(ahm);
165120a2d3f6Sstans 		page_io_unlock(pp);
165220a2d3f6Sstans 		/*LINTED: constant in conditional context*/
165320a2d3f6Sstans 		VN_DISPOSE(pp, B_INVAL, 0, kcred);
165420a2d3f6Sstans 		return (0);
16557c478bd9Sstevel@tonic-gate 	}
165620a2d3f6Sstans 
16577c478bd9Sstevel@tonic-gate 	/*
16587c478bd9Sstevel@tonic-gate 	 * Free the physical slot. It may have been freed up and replaced with
16597c478bd9Sstevel@tonic-gate 	 * another one while we were getting the page so we have to re-verify
16607c478bd9Sstevel@tonic-gate 	 * that this is really one we want. If we do free the slot we have
16617c478bd9Sstevel@tonic-gate 	 * to mark the page modified, as its backing store is now gone.
16627c478bd9Sstevel@tonic-gate 	 */
166320a2d3f6Sstans 	if ((ap != NULL) && (ap->an_pvp == sip->si_vp && ap->an_poff >=
166420a2d3f6Sstans 	    sip->si_soff && ap->an_poff < sip->si_eoff)) {
16657c478bd9Sstevel@tonic-gate 		swap_phys_free(ap->an_pvp, ap->an_poff, PAGESIZE);
16667c478bd9Sstevel@tonic-gate 		ap->an_pvp = NULL;
166720a2d3f6Sstans 		ap->an_poff = 0;
16687c478bd9Sstevel@tonic-gate 		mutex_exit(ahm);
16697c478bd9Sstevel@tonic-gate 		hat_setmod(pp);
16707c478bd9Sstevel@tonic-gate 	} else {
16717c478bd9Sstevel@tonic-gate 		mutex_exit(ahm);
16727c478bd9Sstevel@tonic-gate 	}
16737c478bd9Sstevel@tonic-gate 	page_io_unlock(pp);
167420a2d3f6Sstans 	page_unlock(pp);
167520a2d3f6Sstans 	return (0);
16767c478bd9Sstevel@tonic-gate }
16777c478bd9Sstevel@tonic-gate 
167820a2d3f6Sstans 
16797c478bd9Sstevel@tonic-gate /*
16807c478bd9Sstevel@tonic-gate  * Get contig physical backing store for vp, in the range
16817c478bd9Sstevel@tonic-gate  * [*offp, *offp + *lenp), May back a subrange of this, but must
16827c478bd9Sstevel@tonic-gate  * always include the requested offset or fail. Returns the offsets
16837c478bd9Sstevel@tonic-gate  * backed as [*offp, *offp + *lenp) and the physical offsets used to
16847c478bd9Sstevel@tonic-gate  * back them from *pvpp in the range [*pstartp, *pstartp + *lenp).
1685*6cdd461fSToomas Soome  * Returns	0 for success
1686*6cdd461fSToomas Soome  *		SE_NOANON -- no anon slot for requested paged
16877c478bd9Sstevel@tonic-gate  *		SE_NOSWAP -- no physical swap space available
16887c478bd9Sstevel@tonic-gate  */
16897c478bd9Sstevel@tonic-gate int
swap_newphysname(struct vnode * vp,u_offset_t offset,u_offset_t * offp,size_t * lenp,struct vnode ** pvpp,u_offset_t * poffp)16907c478bd9Sstevel@tonic-gate swap_newphysname(
16917c478bd9Sstevel@tonic-gate 	struct vnode *vp,
16927c478bd9Sstevel@tonic-gate 	u_offset_t offset,
16937c478bd9Sstevel@tonic-gate 	u_offset_t *offp,
16947c478bd9Sstevel@tonic-gate 	size_t *lenp,
16957c478bd9Sstevel@tonic-gate 	struct vnode **pvpp,
16967c478bd9Sstevel@tonic-gate 	u_offset_t *poffp)
16977c478bd9Sstevel@tonic-gate {
16987c478bd9Sstevel@tonic-gate 	struct anon *ap = NULL;		/* anon slot for vp, off */
16997c478bd9Sstevel@tonic-gate 	int error = 0;
17007c478bd9Sstevel@tonic-gate 	struct vnode *pvp;
17017c478bd9Sstevel@tonic-gate 	u_offset_t poff, pstart, prem;
17027c478bd9Sstevel@tonic-gate 	size_t plen;
17037c478bd9Sstevel@tonic-gate 	u_offset_t off, start;
17047c478bd9Sstevel@tonic-gate 	kmutex_t *ahm;
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	ASSERT(*offp <= offset && offset < *offp + *lenp);
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 	/* Get new physical swap slots. */
17097c478bd9Sstevel@tonic-gate 	plen = *lenp;
17107c478bd9Sstevel@tonic-gate 	if (!swap_phys_alloc(&pvp, &pstart, &plen, 0)) {
17117c478bd9Sstevel@tonic-gate 		/*
17127c478bd9Sstevel@tonic-gate 		 * No swap available so return error unless requested
17137c478bd9Sstevel@tonic-gate 		 * offset is already backed in which case return that.
17147c478bd9Sstevel@tonic-gate 		 */
171523d9e5acSMichael Corcoran 		ahm = AH_MUTEX(vp, offset);
17167c478bd9Sstevel@tonic-gate 		mutex_enter(ahm);
17177c478bd9Sstevel@tonic-gate 		if ((ap = swap_anon(vp, offset)) == NULL) {
17187c478bd9Sstevel@tonic-gate 			error = SE_NOANON;
17197c478bd9Sstevel@tonic-gate 			mutex_exit(ahm);
17207c478bd9Sstevel@tonic-gate 			return (error);
17217c478bd9Sstevel@tonic-gate 		}
17227c478bd9Sstevel@tonic-gate 		error = (ap->an_pvp ? 0 : SE_NOSWAP);
17237c478bd9Sstevel@tonic-gate 		*offp = offset;
17247c478bd9Sstevel@tonic-gate 		*lenp = PAGESIZE;
17257c478bd9Sstevel@tonic-gate 		*pvpp = ap->an_pvp;
17267c478bd9Sstevel@tonic-gate 		*poffp = ap->an_poff;
17277c478bd9Sstevel@tonic-gate 		mutex_exit(ahm);
17287c478bd9Sstevel@tonic-gate 		return (error);
17297c478bd9Sstevel@tonic-gate 	}
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 	/*
17327c478bd9Sstevel@tonic-gate 	 * We got plen (<= *lenp) contig slots. Use these to back a
17337c478bd9Sstevel@tonic-gate 	 * subrange of [*offp, *offp + *lenp) which includes offset.
17347c478bd9Sstevel@tonic-gate 	 * For now we just put offset at the end of the kluster.
17357c478bd9Sstevel@tonic-gate 	 * Clearly there are other possible choices - which is best?
17367c478bd9Sstevel@tonic-gate 	 */
17377c478bd9Sstevel@tonic-gate 	start = MAX(*offp,
17387c478bd9Sstevel@tonic-gate 	    (offset + PAGESIZE > plen) ? (offset + PAGESIZE - plen) : 0);
17397c478bd9Sstevel@tonic-gate 	ASSERT(start + plen <= *offp + *lenp);
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 	for (off = start, poff = pstart; poff < pstart + plen;
17427c478bd9Sstevel@tonic-gate 	    off += PAGESIZE, poff += PAGESIZE) {
174323d9e5acSMichael Corcoran 		ahm = AH_MUTEX(vp, off);
17447c478bd9Sstevel@tonic-gate 		mutex_enter(ahm);
17457c478bd9Sstevel@tonic-gate 		if ((ap = swap_anon(vp, off)) != NULL) {
17467c478bd9Sstevel@tonic-gate 			/* Free old slot if any, and assign new one */
17477c478bd9Sstevel@tonic-gate 			if (ap->an_pvp)
17487c478bd9Sstevel@tonic-gate 				swap_phys_free(ap->an_pvp, ap->an_poff,
17497c478bd9Sstevel@tonic-gate 				    PAGESIZE);
17507c478bd9Sstevel@tonic-gate 			ap->an_pvp = pvp;
17517c478bd9Sstevel@tonic-gate 			ap->an_poff = poff;
17527c478bd9Sstevel@tonic-gate 		} else {	/* No anon slot for a klustered page, quit. */
17537c478bd9Sstevel@tonic-gate 			prem = (pstart + plen) - poff;
17547c478bd9Sstevel@tonic-gate 			/* Already did requested page, do partial kluster */
17557c478bd9Sstevel@tonic-gate 			if (off > offset) {
17567c478bd9Sstevel@tonic-gate 				plen = poff - pstart;
17577c478bd9Sstevel@tonic-gate 				error = 0;
17587c478bd9Sstevel@tonic-gate 			/* Fail on requested page, error */
17597c478bd9Sstevel@tonic-gate 			} else if (off == offset)  {
17607c478bd9Sstevel@tonic-gate 				error = SE_NOANON;
17617c478bd9Sstevel@tonic-gate 			/* Fail on prior page, fail on requested page, error */
17627c478bd9Sstevel@tonic-gate 			} else if ((ap = swap_anon(vp, offset)) == NULL) {
17637c478bd9Sstevel@tonic-gate 				error = SE_NOANON;
17647c478bd9Sstevel@tonic-gate 			/* Fail on prior page, got requested page, do only it */
17657c478bd9Sstevel@tonic-gate 			} else {
17667c478bd9Sstevel@tonic-gate 				/* Free old slot if any, and assign new one */
17677c478bd9Sstevel@tonic-gate 				if (ap->an_pvp)
17687c478bd9Sstevel@tonic-gate 					swap_phys_free(ap->an_pvp, ap->an_poff,
17697c478bd9Sstevel@tonic-gate 					    PAGESIZE);
17707c478bd9Sstevel@tonic-gate 				ap->an_pvp = pvp;
17717c478bd9Sstevel@tonic-gate 				ap->an_poff = poff;
17727c478bd9Sstevel@tonic-gate 				/* One page kluster */
17737c478bd9Sstevel@tonic-gate 				start = offset;
17747c478bd9Sstevel@tonic-gate 				plen = PAGESIZE;
17757c478bd9Sstevel@tonic-gate 				pstart = poff;
17767c478bd9Sstevel@tonic-gate 				poff += PAGESIZE;
17777c478bd9Sstevel@tonic-gate 				prem -= PAGESIZE;
17787c478bd9Sstevel@tonic-gate 			}
17797c478bd9Sstevel@tonic-gate 			/* Free unassigned slots */
17807c478bd9Sstevel@tonic-gate 			swap_phys_free(pvp, poff, prem);
17817c478bd9Sstevel@tonic-gate 			mutex_exit(ahm);
17827c478bd9Sstevel@tonic-gate 			break;
17837c478bd9Sstevel@tonic-gate 		}
17847c478bd9Sstevel@tonic-gate 		mutex_exit(ahm);
17857c478bd9Sstevel@tonic-gate 	}
17867c478bd9Sstevel@tonic-gate 	ASSERT(*offp <= start && start + plen <= *offp + *lenp);
17877c478bd9Sstevel@tonic-gate 	ASSERT(start <= offset && offset < start + plen);
17887c478bd9Sstevel@tonic-gate 	*offp = start;
17897c478bd9Sstevel@tonic-gate 	*lenp = plen;
17907c478bd9Sstevel@tonic-gate 	*pvpp = pvp;
17917c478bd9Sstevel@tonic-gate 	*poffp = pstart;
17927c478bd9Sstevel@tonic-gate 	return (error);
17937c478bd9Sstevel@tonic-gate }
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate /*
17977c478bd9Sstevel@tonic-gate  * Get the physical swap backing store location for a given anonymous page
17987c478bd9Sstevel@tonic-gate  * named (vp, off). The backing store name is returned in (*pvpp, *poffp).
1799*6cdd461fSToomas Soome  * Returns	0		success
18007c478bd9Sstevel@tonic-gate  *		EIDRM --	no anon slot (page is not allocated)
18017c478bd9Sstevel@tonic-gate  */
18027c478bd9Sstevel@tonic-gate int
swap_getphysname(struct vnode * vp,u_offset_t off,struct vnode ** pvpp,u_offset_t * poffp)18037c478bd9Sstevel@tonic-gate swap_getphysname(
18047c478bd9Sstevel@tonic-gate 	struct vnode *vp,
18057c478bd9Sstevel@tonic-gate 	u_offset_t off,
18067c478bd9Sstevel@tonic-gate 	struct vnode **pvpp,
18077c478bd9Sstevel@tonic-gate 	u_offset_t *poffp)
18087c478bd9Sstevel@tonic-gate {
18097c478bd9Sstevel@tonic-gate 	struct anon *ap;
18107c478bd9Sstevel@tonic-gate 	int error = 0;
18117c478bd9Sstevel@tonic-gate 	kmutex_t *ahm;
18127c478bd9Sstevel@tonic-gate 
181323d9e5acSMichael Corcoran 	ahm = AH_MUTEX(vp, off);
18147c478bd9Sstevel@tonic-gate 	mutex_enter(ahm);
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 	/* Get anon slot for vp, off */
18177c478bd9Sstevel@tonic-gate 	ap = swap_anon(vp, off);
18187c478bd9Sstevel@tonic-gate 	if (ap == NULL) {
18197c478bd9Sstevel@tonic-gate 		error = EIDRM;
18207c478bd9Sstevel@tonic-gate 		goto out;
18217c478bd9Sstevel@tonic-gate 	}
18227c478bd9Sstevel@tonic-gate 	*pvpp = ap->an_pvp;
18237c478bd9Sstevel@tonic-gate 	*poffp = ap->an_poff;
18247c478bd9Sstevel@tonic-gate out:
18257c478bd9Sstevel@tonic-gate 	mutex_exit(ahm);
18267c478bd9Sstevel@tonic-gate 	return (error);
18277c478bd9Sstevel@tonic-gate }
1828