xref: /onnv-gate/usr/src/uts/common/vm/vm_swap.c (revision 12908:80a39220b451)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55331Samw  * Common Development and Distribution License (the "License").
65331Samw  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
2212173SMichael.Corcoran@Sun.COM  * Copyright (c) 1987, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
260Sstevel@tonic-gate /*	  All Rights Reserved  	*/
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
300Sstevel@tonic-gate  * The Regents of the University of California
310Sstevel@tonic-gate  * All Rights Reserved
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
340Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
350Sstevel@tonic-gate  * contributors.
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate  * Each physical swap area has an associated bitmap representing
400Sstevel@tonic-gate  * its physical storage. The bitmap records which swap slots are
410Sstevel@tonic-gate  * currently allocated or freed.  Allocation is done by searching
420Sstevel@tonic-gate  * through the bitmap for the first free slot. Thus, there's
430Sstevel@tonic-gate  * no linear relation between offset within the swap device and the
440Sstevel@tonic-gate  * address (within its segment(s)) of the page that the slot backs;
450Sstevel@tonic-gate  * instead, it's an arbitrary one-to-one mapping.
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * Associated with each swap area is a swapinfo structure.  These
480Sstevel@tonic-gate  * structures are linked into a linear list that determines the
490Sstevel@tonic-gate  * ordering of swap areas in the logical swap device.  Each contains a
500Sstevel@tonic-gate  * pointer to the corresponding bitmap, the area's size, and its
510Sstevel@tonic-gate  * associated vnode.
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #include <sys/types.h>
550Sstevel@tonic-gate #include <sys/inttypes.h>
560Sstevel@tonic-gate #include <sys/param.h>
570Sstevel@tonic-gate #include <sys/t_lock.h>
580Sstevel@tonic-gate #include <sys/sysmacros.h>
590Sstevel@tonic-gate #include <sys/systm.h>
600Sstevel@tonic-gate #include <sys/errno.h>
610Sstevel@tonic-gate #include <sys/kmem.h>
620Sstevel@tonic-gate #include <sys/vfs.h>
630Sstevel@tonic-gate #include <sys/vnode.h>
640Sstevel@tonic-gate #include <sys/pathname.h>
650Sstevel@tonic-gate #include <sys/cmn_err.h>
660Sstevel@tonic-gate #include <sys/vtrace.h>
670Sstevel@tonic-gate #include <sys/swap.h>
680Sstevel@tonic-gate #include <sys/dumphdr.h>
690Sstevel@tonic-gate #include <sys/debug.h>
700Sstevel@tonic-gate #include <sys/fs/snode.h>
710Sstevel@tonic-gate #include <sys/fs/swapnode.h>
720Sstevel@tonic-gate #include <sys/policy.h>
730Sstevel@tonic-gate #include <sys/zone.h>
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #include <vm/as.h>
760Sstevel@tonic-gate #include <vm/seg.h>
770Sstevel@tonic-gate #include <vm/page.h>
780Sstevel@tonic-gate #include <vm/seg_vn.h>
790Sstevel@tonic-gate #include <vm/hat.h>
800Sstevel@tonic-gate #include <vm/anon.h>
810Sstevel@tonic-gate #include <vm/seg_map.h>
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate  * To balance the load among multiple swap areas, we don't allow
850Sstevel@tonic-gate  * more than swap_maxcontig allocations to be satisfied from a
860Sstevel@tonic-gate  * single swap area before moving on to the next swap area.  This
870Sstevel@tonic-gate  * effectively "interleaves" allocations among the many swap areas.
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate int swap_maxcontig;	/* set by anon_init() to 1 Mb */
900Sstevel@tonic-gate 
910Sstevel@tonic-gate #define	MINIROOTSIZE	12000	/* ~6 Meg XXX */
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * XXX - this lock is a kludge. It serializes some aspects of swapadd() and
950Sstevel@tonic-gate  * swapdel() (namely VOP_OPEN, VOP_CLOSE, VN_RELE).  It protects against
960Sstevel@tonic-gate  * somebody swapadd'ing and getting swap slots from a vnode, while someone
970Sstevel@tonic-gate  * else is in the process of closing or rele'ing it.
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate static kmutex_t swap_lock;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate kmutex_t swapinfo_lock;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * protected by the swapinfo_lock
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate struct swapinfo	*swapinfo;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static	struct	swapinfo *silast;
1090Sstevel@tonic-gate static	int	nswapfiles;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static u_offset_t	swap_getoff(struct swapinfo *);
1120Sstevel@tonic-gate static int	swapadd(struct vnode *, ulong_t, ulong_t, char *);
1130Sstevel@tonic-gate static int	swapdel(struct vnode *, ulong_t);
1140Sstevel@tonic-gate static int	swapslot_free(struct vnode *, u_offset_t, struct swapinfo *);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate  * swap device bitmap allocation macros
1180Sstevel@tonic-gate  */
1190Sstevel@tonic-gate #define	MAPSHIFT	5
1200Sstevel@tonic-gate #define	NBBW		(NBPW * NBBY)	/* number of bits per word */
1210Sstevel@tonic-gate #define	TESTBIT(map, i)		(((map)[(i) >> MAPSHIFT] & (1 << (i) % NBBW)))
1220Sstevel@tonic-gate #define	SETBIT(map, i)		(((map)[(i) >> MAPSHIFT] |= (1 << (i) % NBBW)))
1230Sstevel@tonic-gate #define	CLEARBIT(map, i)	(((map)[(i) >> MAPSHIFT] &= ~(1 << (i) % NBBW)))
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate int swap_debug = 0;	/* set for debug printf's */
1260Sstevel@tonic-gate int swap_verify = 0;	/* set to verify slots when freeing and allocating */
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate uint_t swapalloc_maxcontig;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate  * Allocate a range of up to *lenp contiguous slots (page) from a physical
1320Sstevel@tonic-gate  * swap device. Flags are one of:
1330Sstevel@tonic-gate  *	SA_NOT  Must have a slot from a physical swap device other than the
1340Sstevel@tonic-gate  * 		the one containing input (*vpp, *offp).
1350Sstevel@tonic-gate  * Less slots than requested may be returned. *lenp allocated slots are
1360Sstevel@tonic-gate  * returned starting at *offp on *vpp.
1370Sstevel@tonic-gate  * Returns 1 for a successful allocation, 0 for couldn't allocate any slots.
1380Sstevel@tonic-gate  */
1390Sstevel@tonic-gate int
swap_phys_alloc(struct vnode ** vpp,u_offset_t * offp,size_t * lenp,uint_t flags)1400Sstevel@tonic-gate swap_phys_alloc(
1410Sstevel@tonic-gate 	struct vnode **vpp,
1420Sstevel@tonic-gate 	u_offset_t *offp,
1430Sstevel@tonic-gate 	size_t *lenp,
1440Sstevel@tonic-gate 	uint_t flags)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	struct swapinfo *sip;
1470Sstevel@tonic-gate 	offset_t soff, noff;
1480Sstevel@tonic-gate 	size_t len;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	mutex_enter(&swapinfo_lock);
1510Sstevel@tonic-gate 	sip = silast;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	/* Find a desirable physical device and allocate from it. */
1540Sstevel@tonic-gate 	do {
1550Sstevel@tonic-gate 		if (sip == NULL)
1560Sstevel@tonic-gate 			break;
1570Sstevel@tonic-gate 		if (!(sip->si_flags & ST_INDEL) &&
1580Sstevel@tonic-gate 		    (spgcnt_t)sip->si_nfpgs > 0) {
1590Sstevel@tonic-gate 			/* Caller wants other than specified swap device */
1600Sstevel@tonic-gate 			if (flags & SA_NOT) {
1610Sstevel@tonic-gate 				if (*vpp != sip->si_vp ||
1620Sstevel@tonic-gate 				    *offp < sip->si_soff ||
1630Sstevel@tonic-gate 				    *offp >= sip->si_eoff)
1640Sstevel@tonic-gate 					goto found;
1650Sstevel@tonic-gate 			/* Caller is loose, will take anything */
1660Sstevel@tonic-gate 			} else
1670Sstevel@tonic-gate 				goto found;
1680Sstevel@tonic-gate 		} else if (sip->si_nfpgs == 0)
1690Sstevel@tonic-gate 			sip->si_allocs = 0;
1700Sstevel@tonic-gate 		if ((sip = sip->si_next) == NULL)
1710Sstevel@tonic-gate 			sip = swapinfo;
1720Sstevel@tonic-gate 	} while (sip != silast);
1730Sstevel@tonic-gate 	mutex_exit(&swapinfo_lock);
1740Sstevel@tonic-gate 	return (0);
1750Sstevel@tonic-gate found:
1760Sstevel@tonic-gate 	soff = swap_getoff(sip);
1770Sstevel@tonic-gate 	sip->si_nfpgs--;
1780Sstevel@tonic-gate 	if (soff == -1)
1790Sstevel@tonic-gate 		panic("swap_alloc: swap_getoff failed!");
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	for (len = PAGESIZE; len < *lenp; len += PAGESIZE) {
1820Sstevel@tonic-gate 		if (sip->si_nfpgs == 0)
1830Sstevel@tonic-gate 			break;
1840Sstevel@tonic-gate 		if (swapalloc_maxcontig && len >= swapalloc_maxcontig)
1850Sstevel@tonic-gate 			break;
1860Sstevel@tonic-gate 		noff = swap_getoff(sip);
1870Sstevel@tonic-gate 		if (noff == -1) {
1880Sstevel@tonic-gate 			break;
1890Sstevel@tonic-gate 		} else if (noff != soff + len) {
1900Sstevel@tonic-gate 			CLEARBIT(sip->si_swapslots, btop(noff - sip->si_soff));
1910Sstevel@tonic-gate 			break;
1920Sstevel@tonic-gate 		}
1930Sstevel@tonic-gate 		sip->si_nfpgs--;
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 	*vpp = sip->si_vp;
1960Sstevel@tonic-gate 	*offp = soff;
1970Sstevel@tonic-gate 	*lenp = len;
1980Sstevel@tonic-gate 	ASSERT((spgcnt_t)sip->si_nfpgs >= 0);
1990Sstevel@tonic-gate 	sip->si_allocs += btop(len);
2000Sstevel@tonic-gate 	if (sip->si_allocs >= swap_maxcontig) {
2010Sstevel@tonic-gate 		sip->si_allocs = 0;
2020Sstevel@tonic-gate 		if ((silast = sip->si_next) == NULL)
2030Sstevel@tonic-gate 			silast = swapinfo;
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 	TRACE_2(TR_FAC_VM, TR_SWAP_ALLOC,
2065665Sstans 	    "swap_alloc:sip %p offset %lx", sip, soff);
2070Sstevel@tonic-gate 	mutex_exit(&swapinfo_lock);
2080Sstevel@tonic-gate 	return (1);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate int swap_backsearch = 0;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate /*
2140Sstevel@tonic-gate  * Get a free offset on swap device sip.
2150Sstevel@tonic-gate  * Return >=0 offset if succeeded, -1 for failure.
2160Sstevel@tonic-gate  */
2170Sstevel@tonic-gate static u_offset_t
swap_getoff(struct swapinfo * sip)2180Sstevel@tonic-gate swap_getoff(struct swapinfo *sip)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate 	uint_t *sp, *ep;
2210Sstevel@tonic-gate 	size_t aoff, boff, poff, slotnumber;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&swapinfo_lock));
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	sip->si_alloccnt++;
2260Sstevel@tonic-gate 	for (sp = &sip->si_swapslots[sip->si_hint >> MAPSHIFT],
2270Sstevel@tonic-gate 	    ep = &sip->si_swapslots[sip->si_mapsize / NBPW]; sp < ep; sp++) {
2280Sstevel@tonic-gate 		if (*sp != (uint_t)0xffffffff)
2290Sstevel@tonic-gate 			goto foundentry;
2300Sstevel@tonic-gate 		else
2310Sstevel@tonic-gate 			sip->si_checkcnt++;
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 	SWAP_PRINT(SW_ALLOC,
2340Sstevel@tonic-gate 	    "swap_getoff: couldn't find slot from hint %ld to end\n",
2350Sstevel@tonic-gate 	    sip->si_hint, 0, 0, 0, 0);
2360Sstevel@tonic-gate 	/*
2370Sstevel@tonic-gate 	 * Go backwards? Check for faster method XXX
2380Sstevel@tonic-gate 	 */
2390Sstevel@tonic-gate 	if (swap_backsearch) {
2400Sstevel@tonic-gate 		for (sp = &sip->si_swapslots[sip->si_hint >> MAPSHIFT],
2410Sstevel@tonic-gate 		    ep = sip->si_swapslots; sp > ep; sp--) {
2420Sstevel@tonic-gate 			if (*sp != (uint_t)0xffffffff)
2430Sstevel@tonic-gate 				goto foundentry;
2440Sstevel@tonic-gate 			else
2450Sstevel@tonic-gate 				sip->si_checkcnt++;
2460Sstevel@tonic-gate 		}
2470Sstevel@tonic-gate 	} else {
2480Sstevel@tonic-gate 		for (sp = sip->si_swapslots,
2490Sstevel@tonic-gate 		    ep = &sip->si_swapslots[sip->si_hint >> MAPSHIFT];
2500Sstevel@tonic-gate 		    sp < ep; sp++) {
2510Sstevel@tonic-gate 			if (*sp != (uint_t)0xffffffff)
2520Sstevel@tonic-gate 				goto foundentry;
2530Sstevel@tonic-gate 			else
2540Sstevel@tonic-gate 				sip->si_checkcnt++;
2550Sstevel@tonic-gate 		}
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 	if (*sp == 0xffffffff) {
2580Sstevel@tonic-gate 		cmn_err(CE_WARN, "No free swap slots!");
2590Sstevel@tonic-gate 		return ((u_offset_t)-1);
2600Sstevel@tonic-gate 	}
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate foundentry:
2630Sstevel@tonic-gate 	/*
2640Sstevel@tonic-gate 	 * aoff is the page number offset (in bytes) of the si_swapslots
2650Sstevel@tonic-gate 	 * array element containing a free page
2660Sstevel@tonic-gate 	 *
2670Sstevel@tonic-gate 	 * boff is the page number offset of the free page
2680Sstevel@tonic-gate 	 * (i.e. cleared bit) in si_swapslots[aoff].
2690Sstevel@tonic-gate 	 */
2700Sstevel@tonic-gate 	aoff = ((char *)sp - (char *)sip->si_swapslots) * NBBY;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	for (boff = (sip->si_hint % NBBW); boff < NBBW; boff++) {
2730Sstevel@tonic-gate 		if (!TESTBIT(sip->si_swapslots, aoff + boff))
2740Sstevel@tonic-gate 			goto foundslot;
2750Sstevel@tonic-gate 		else
2760Sstevel@tonic-gate 			sip->si_checkcnt++;
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate 	for (boff = 0; boff < (sip->si_hint % NBBW); boff++) {
2790Sstevel@tonic-gate 		if (!TESTBIT(sip->si_swapslots, aoff + boff))
2800Sstevel@tonic-gate 			goto foundslot;
2810Sstevel@tonic-gate 		else
2820Sstevel@tonic-gate 			sip->si_checkcnt++;
2830Sstevel@tonic-gate 	}
2840Sstevel@tonic-gate 	panic("swap_getoff: didn't find slot in word hint %ld", sip->si_hint);
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate foundslot:
2870Sstevel@tonic-gate 	/*
2880Sstevel@tonic-gate 	 * Return the offset of the free page in swap device.
2890Sstevel@tonic-gate 	 * Convert page number of byte offset and add starting
2900Sstevel@tonic-gate 	 * offset of swap device.
2910Sstevel@tonic-gate 	 */
2920Sstevel@tonic-gate 	slotnumber = aoff + boff;
2930Sstevel@tonic-gate 	SWAP_PRINT(SW_ALLOC, "swap_getoff: allocating slot %ld\n",
2940Sstevel@tonic-gate 	    slotnumber, 0, 0, 0, 0);
2950Sstevel@tonic-gate 	poff = ptob(slotnumber);
2960Sstevel@tonic-gate 	if (poff + sip->si_soff >= sip->si_eoff)
2970Sstevel@tonic-gate 		printf("ptob(aoff(%ld) + boff(%ld))(%ld) >= eoff(%ld)\n",
2980Sstevel@tonic-gate 		    aoff, boff, ptob(slotnumber), (long)sip->si_eoff);
2990Sstevel@tonic-gate 	ASSERT(poff < sip->si_eoff);
3000Sstevel@tonic-gate 	/*
3010Sstevel@tonic-gate 	 * We could verify here that the slot isn't already allocated
3020Sstevel@tonic-gate 	 * by looking through all the anon slots.
3030Sstevel@tonic-gate 	 */
3040Sstevel@tonic-gate 	SETBIT(sip->si_swapslots, slotnumber);
3050Sstevel@tonic-gate 	sip->si_hint = slotnumber + 1;	/* hint = next slot */
3060Sstevel@tonic-gate 	return (poff + sip->si_soff);
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate  * Free a swap page.
3110Sstevel@tonic-gate  */
3120Sstevel@tonic-gate void
swap_phys_free(struct vnode * vp,u_offset_t off,size_t len)3130Sstevel@tonic-gate swap_phys_free(struct vnode *vp, u_offset_t off, size_t len)
3140Sstevel@tonic-gate {
3150Sstevel@tonic-gate 	struct swapinfo *sip;
3160Sstevel@tonic-gate 	ssize_t pagenumber, npage;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	mutex_enter(&swapinfo_lock);
3190Sstevel@tonic-gate 	sip = swapinfo;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	do {
3220Sstevel@tonic-gate 		if (sip->si_vp == vp &&
3230Sstevel@tonic-gate 		    sip->si_soff <= off && off < sip->si_eoff) {
3240Sstevel@tonic-gate 			for (pagenumber = btop(off - sip->si_soff),
3250Sstevel@tonic-gate 			    npage = btop(len) + pagenumber;
3260Sstevel@tonic-gate 			    pagenumber < npage; pagenumber++) {
3270Sstevel@tonic-gate 				SWAP_PRINT(SW_ALLOC,
3280Sstevel@tonic-gate 				    "swap_phys_free: freeing slot %ld on "
3290Sstevel@tonic-gate 				    "sip %p\n",
3300Sstevel@tonic-gate 				    pagenumber, sip, 0, 0, 0);
3310Sstevel@tonic-gate 				if (!TESTBIT(sip->si_swapslots, pagenumber)) {
3320Sstevel@tonic-gate 					panic(
3330Sstevel@tonic-gate 					    "swap_phys_free: freeing free slot "
3340Sstevel@tonic-gate 					    "%p,%lx\n", (void *)vp,
3350Sstevel@tonic-gate 					    ptob(pagenumber) + sip->si_soff);
3360Sstevel@tonic-gate 				}
3370Sstevel@tonic-gate 				CLEARBIT(sip->si_swapslots, pagenumber);
3380Sstevel@tonic-gate 				sip->si_nfpgs++;
3390Sstevel@tonic-gate 			}
3400Sstevel@tonic-gate 			ASSERT(sip->si_nfpgs <= sip->si_npgs);
3410Sstevel@tonic-gate 			mutex_exit(&swapinfo_lock);
3420Sstevel@tonic-gate 			return;
3430Sstevel@tonic-gate 		}
3440Sstevel@tonic-gate 	} while ((sip = sip->si_next) != NULL);
3450Sstevel@tonic-gate 	panic("swap_phys_free");
3460Sstevel@tonic-gate 	/*NOTREACHED*/
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate /*
3500Sstevel@tonic-gate  * Return the anon struct corresponding for the given
3510Sstevel@tonic-gate  * <vnode, off> if it is part of the virtual swap device.
3520Sstevel@tonic-gate  * Return the anon struct if found, otherwise NULL.
3530Sstevel@tonic-gate  */
3540Sstevel@tonic-gate struct anon *
swap_anon(struct vnode * vp,u_offset_t off)3550Sstevel@tonic-gate swap_anon(struct vnode *vp, u_offset_t off)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate 	struct anon *ap;
3580Sstevel@tonic-gate 
35912173SMichael.Corcoran@Sun.COM 	ASSERT(MUTEX_HELD(AH_MUTEX(vp, off)));
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	for (ap = anon_hash[ANON_HASH(vp, off)]; ap != NULL; ap = ap->an_hash) {
3620Sstevel@tonic-gate 		if (ap->an_vp == vp && ap->an_off == off)
3630Sstevel@tonic-gate 			return (ap);
3640Sstevel@tonic-gate 	}
3650Sstevel@tonic-gate 	return (NULL);
3660Sstevel@tonic-gate }
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate /*
3700Sstevel@tonic-gate  * Determine if the vp offset range overlap a swap device.
3710Sstevel@tonic-gate  */
3720Sstevel@tonic-gate int
swap_in_range(struct vnode * vp,u_offset_t offset,size_t len)3730Sstevel@tonic-gate swap_in_range(struct vnode *vp, u_offset_t offset, size_t len)
3740Sstevel@tonic-gate {
3750Sstevel@tonic-gate 	struct swapinfo *sip;
3760Sstevel@tonic-gate 	u_offset_t eoff;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	eoff = offset + len;
3790Sstevel@tonic-gate 	ASSERT(eoff > offset);
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	mutex_enter(&swapinfo_lock);
3820Sstevel@tonic-gate 	sip = swapinfo;
3830Sstevel@tonic-gate 	if (vp && sip) {
3840Sstevel@tonic-gate 		do {
3850Sstevel@tonic-gate 			if (vp != sip->si_vp || eoff <= sip->si_soff ||
3860Sstevel@tonic-gate 			    offset >= sip->si_eoff)
3870Sstevel@tonic-gate 				continue;
3880Sstevel@tonic-gate 			mutex_exit(&swapinfo_lock);
3890Sstevel@tonic-gate 			return (1);
3900Sstevel@tonic-gate 		} while ((sip = sip->si_next) != NULL);
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate 	mutex_exit(&swapinfo_lock);
3930Sstevel@tonic-gate 	return (0);
3940Sstevel@tonic-gate }
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate /*
3970Sstevel@tonic-gate  * See if name is one of our swap files
3980Sstevel@tonic-gate  * even though lookupname failed.
3990Sstevel@tonic-gate  * This can be used by swapdel to delete
4000Sstevel@tonic-gate  * swap resources on remote machines
4010Sstevel@tonic-gate  * where the link has gone down.
4020Sstevel@tonic-gate  */
4030Sstevel@tonic-gate static struct vnode *
swapdel_byname(char * name,ulong_t lowblk)4040Sstevel@tonic-gate swapdel_byname(
4050Sstevel@tonic-gate 	char 	*name,			/* pathname to delete */
4060Sstevel@tonic-gate 	ulong_t lowblk) 	/* Low block number of area to delete */
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate 	struct swapinfo **sipp, *osip;
4090Sstevel@tonic-gate 	u_offset_t soff;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	/*
4120Sstevel@tonic-gate 	 * Find the swap file entry for the file to
4130Sstevel@tonic-gate 	 * be deleted. Skip any entries that are in
4140Sstevel@tonic-gate 	 * transition.
4150Sstevel@tonic-gate 	 */
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	soff = ptob(btopr(lowblk << SCTRSHFT)); /* must be page aligned */
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	mutex_enter(&swapinfo_lock);
4200Sstevel@tonic-gate 	for (sipp = &swapinfo; (osip = *sipp) != NULL; sipp = &osip->si_next) {
4210Sstevel@tonic-gate 		if ((strcmp(osip->si_pname, name) == 0) &&
4220Sstevel@tonic-gate 		    (osip->si_soff == soff) && (osip->si_flags == 0)) {
4230Sstevel@tonic-gate 			struct vnode *vp = osip->si_vp;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 			VN_HOLD(vp);
4260Sstevel@tonic-gate 			mutex_exit(&swapinfo_lock);
4270Sstevel@tonic-gate 			return (vp);
4280Sstevel@tonic-gate 		}
4290Sstevel@tonic-gate 	}
4300Sstevel@tonic-gate 	mutex_exit(&swapinfo_lock);
4310Sstevel@tonic-gate 	return (NULL);
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate /*
4360Sstevel@tonic-gate  * New system call to manipulate swap files.
4370Sstevel@tonic-gate  */
4380Sstevel@tonic-gate int
swapctl(int sc_cmd,void * sc_arg,int * rv)4390Sstevel@tonic-gate swapctl(int sc_cmd, void *sc_arg, int *rv)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate 	struct swapinfo *sip, *csip, *tsip;
4420Sstevel@tonic-gate 	int error = 0;
4430Sstevel@tonic-gate 	struct swapent st, *ust;
4440Sstevel@tonic-gate 	struct swapres sr;
4450Sstevel@tonic-gate 	struct vnode *vp;
4460Sstevel@tonic-gate 	int cnt = 0;
4470Sstevel@tonic-gate 	int tmp_nswapfiles;
4480Sstevel@tonic-gate 	int nswap;
4490Sstevel@tonic-gate 	int length, nlen;
4500Sstevel@tonic-gate 	int gplen = 0, plen;
4510Sstevel@tonic-gate 	char *swapname;
4520Sstevel@tonic-gate 	char *pname;
4530Sstevel@tonic-gate 	char *tpname;
4540Sstevel@tonic-gate 	struct anoninfo ai;
4550Sstevel@tonic-gate 	spgcnt_t avail;
4560Sstevel@tonic-gate 	int global = INGLOBALZONE(curproc);
4577884Sgerald.jelinek@sun.com 	struct zone *zp = curproc->p_zone;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	/*
4600Sstevel@tonic-gate 	 * When running in a zone we want to hide the details of the swap
4610Sstevel@tonic-gate 	 * devices: we report there only being one swap device named "swap"
4620Sstevel@tonic-gate 	 * having a size equal to the sum of the sizes of all real swap devices
4630Sstevel@tonic-gate 	 * on the system.
4640Sstevel@tonic-gate 	 */
4650Sstevel@tonic-gate 	switch (sc_cmd) {
4660Sstevel@tonic-gate 	case SC_GETNSWP:
4670Sstevel@tonic-gate 		if (global)
4680Sstevel@tonic-gate 			*rv = nswapfiles;
4690Sstevel@tonic-gate 		else
4700Sstevel@tonic-gate 			*rv = 1;
4710Sstevel@tonic-gate 		return (0);
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	case SC_AINFO:
4740Sstevel@tonic-gate 		/*
4750Sstevel@tonic-gate 		 * Return anoninfo information with these changes:
4760Sstevel@tonic-gate 		 * ani_max = maximum amount of swap space
4770Sstevel@tonic-gate 		 *	(including potentially available physical memory)
4780Sstevel@tonic-gate 		 * ani_free = amount of unallocated anonymous memory
4790Sstevel@tonic-gate 		 *	(some of which might be reserved and including
4800Sstevel@tonic-gate 		 *	 potentially available physical memory)
4810Sstevel@tonic-gate 		 * ani_resv = amount of claimed (reserved) anonymous memory
4820Sstevel@tonic-gate 		 */
4830Sstevel@tonic-gate 		avail = MAX((spgcnt_t)(availrmem - swapfs_minfree), 0);
4840Sstevel@tonic-gate 		ai.ani_max = (k_anoninfo.ani_max +
4857884Sgerald.jelinek@sun.com 		    k_anoninfo.ani_mem_resv) + avail;
4860Sstevel@tonic-gate 
487*12908SPavel.Tatashin@Sun.COM 		/* Update ani_free */
488*12908SPavel.Tatashin@Sun.COM 		set_anoninfo();
4890Sstevel@tonic-gate 		ai.ani_free = k_anoninfo.ani_free + avail;
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 		ai.ani_resv = k_anoninfo.ani_phys_resv +
4920Sstevel@tonic-gate 		    k_anoninfo.ani_mem_resv;
4930Sstevel@tonic-gate 
4947884Sgerald.jelinek@sun.com 		if (!global && zp->zone_max_swap_ctl != UINT64_MAX) {
4957884Sgerald.jelinek@sun.com 			/*
4967884Sgerald.jelinek@sun.com 			 * We're in a non-global zone with a swap cap.  We
4977884Sgerald.jelinek@sun.com 			 * always report the system-wide values for the global
4987884Sgerald.jelinek@sun.com 			 * zone, even though it too can have a swap cap.
4997884Sgerald.jelinek@sun.com 			 */
5007884Sgerald.jelinek@sun.com 
5017884Sgerald.jelinek@sun.com 			/*
5027884Sgerald.jelinek@sun.com 			 * For a swap-capped zone, the numbers are contrived
5037884Sgerald.jelinek@sun.com 			 * since we don't have a correct value of 'reserved'
5047884Sgerald.jelinek@sun.com 			 * for the zone.
5057884Sgerald.jelinek@sun.com 			 *
5067884Sgerald.jelinek@sun.com 			 * The ani_max value is always the zone's swap cap.
5077884Sgerald.jelinek@sun.com 			 *
5087884Sgerald.jelinek@sun.com 			 * The ani_free value is always the difference between
5097884Sgerald.jelinek@sun.com 			 * the cap and the amount of swap in use by the zone.
5107884Sgerald.jelinek@sun.com 			 *
5117884Sgerald.jelinek@sun.com 			 * The ani_resv value is typically set to be the amount
5127884Sgerald.jelinek@sun.com 			 * of swap in use by the zone, but can be adjusted
5137884Sgerald.jelinek@sun.com 			 * upwards to indicate how much swap is currently
5147884Sgerald.jelinek@sun.com 			 * unavailable to that zone due to usage by entities
5157884Sgerald.jelinek@sun.com 			 * outside the zone.
5167884Sgerald.jelinek@sun.com 			 *
5177884Sgerald.jelinek@sun.com 			 * This works as follows.
5187884Sgerald.jelinek@sun.com 			 *
5197884Sgerald.jelinek@sun.com 			 * In the 'swap -s' output, the data is displayed
5207884Sgerald.jelinek@sun.com 			 * as follows:
5217884Sgerald.jelinek@sun.com 			 *    allocated = ani_max  - ani_free
5227884Sgerald.jelinek@sun.com 			 *    reserved  = ani_resv - allocated
5237884Sgerald.jelinek@sun.com 			 *    available = ani_max  - ani_resv
5247884Sgerald.jelinek@sun.com 			 *
5257884Sgerald.jelinek@sun.com 			 * Taking a contrived example, if the swap cap is 100
5267884Sgerald.jelinek@sun.com 			 * and the amount of swap used by the zone is 75, this
5277884Sgerald.jelinek@sun.com 			 * gives:
5287884Sgerald.jelinek@sun.com 			 *    allocated = ani_max  - ani_free  = 100 - 25 = 75
5297884Sgerald.jelinek@sun.com 			 *    reserved  = ani_resv - allocated =  75 - 75 =  0
5307884Sgerald.jelinek@sun.com 			 *    available = ani_max  - ani_resv  = 100 - 75 = 25
5317884Sgerald.jelinek@sun.com 			 *
5327884Sgerald.jelinek@sun.com 			 * In this typical case, you can see that the 'swap -s'
5337884Sgerald.jelinek@sun.com 			 * 'reserved' will always be 0 inside a swap capped
5347884Sgerald.jelinek@sun.com 			 * zone.
5357884Sgerald.jelinek@sun.com 			 *
5367884Sgerald.jelinek@sun.com 			 * However, if the system as a whole has less free
5377884Sgerald.jelinek@sun.com 			 * swap than the zone limits allow, then we adjust
5387884Sgerald.jelinek@sun.com 			 * the ani_resv value up so that it is the difference
5397884Sgerald.jelinek@sun.com 			 * between the zone cap and the amount of free system
5407884Sgerald.jelinek@sun.com 			 * swap.  Taking the above example, but when the
5417884Sgerald.jelinek@sun.com 			 * system as a whole only has 20 of swap available, we
5427884Sgerald.jelinek@sun.com 			 * get an ani_resv of 100 - 20 = 80.  This gives:
5437884Sgerald.jelinek@sun.com 			 *    allocated = ani_max  - ani_free  = 100 - 25 = 75
5447884Sgerald.jelinek@sun.com 			 *    reserved  = ani_resv - allocated =  80 - 75 =  5
5457884Sgerald.jelinek@sun.com 			 *    available = ani_max  - ani_resv  = 100 - 80 = 20
5467884Sgerald.jelinek@sun.com 			 *
5477884Sgerald.jelinek@sun.com 			 * In this case, you can see how the ani_resv value is
5487884Sgerald.jelinek@sun.com 			 * tweaked up to make the 'swap -s' numbers work inside
5497884Sgerald.jelinek@sun.com 			 * the zone.
5507884Sgerald.jelinek@sun.com 			 */
5517884Sgerald.jelinek@sun.com 			rctl_qty_t cap, used;
5527884Sgerald.jelinek@sun.com 			pgcnt_t pgcap, sys_avail;
5537884Sgerald.jelinek@sun.com 
5547884Sgerald.jelinek@sun.com 			mutex_enter(&zp->zone_mem_lock);
5557884Sgerald.jelinek@sun.com 			cap = zp->zone_max_swap_ctl;
5567884Sgerald.jelinek@sun.com 			used = zp->zone_max_swap;
5577884Sgerald.jelinek@sun.com 			mutex_exit(&zp->zone_mem_lock);
5587884Sgerald.jelinek@sun.com 
5597884Sgerald.jelinek@sun.com 			pgcap = MIN(btop(cap), ai.ani_max);
5607884Sgerald.jelinek@sun.com 			ai.ani_free = pgcap - btop(used);
5617884Sgerald.jelinek@sun.com 
5627884Sgerald.jelinek@sun.com 			/* Get the system-wide swap currently available. */
5637884Sgerald.jelinek@sun.com 			sys_avail = ai.ani_max - ai.ani_resv;
5647884Sgerald.jelinek@sun.com 			if (sys_avail < ai.ani_free)
5657884Sgerald.jelinek@sun.com 				ai.ani_resv = pgcap - sys_avail;
5667884Sgerald.jelinek@sun.com 			else
5677884Sgerald.jelinek@sun.com 				ai.ani_resv = btop(used);
5687884Sgerald.jelinek@sun.com 
5697884Sgerald.jelinek@sun.com 			ai.ani_max = pgcap;
5707884Sgerald.jelinek@sun.com 		}
5717884Sgerald.jelinek@sun.com 
5720Sstevel@tonic-gate 		if (copyout(&ai, sc_arg, sizeof (struct anoninfo)) != 0)
5730Sstevel@tonic-gate 			return (EFAULT);
5740Sstevel@tonic-gate 		return (0);
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	case SC_LIST:
5770Sstevel@tonic-gate 		if (copyin(sc_arg, &length, sizeof (int)) != 0)
5780Sstevel@tonic-gate 			return (EFAULT);
5790Sstevel@tonic-gate 		if (!global) {
5800Sstevel@tonic-gate 			struct swapent st;
5810Sstevel@tonic-gate 			char *swappath = "swap";
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 			if (length < 1)
5840Sstevel@tonic-gate 				return (ENOMEM);
5850Sstevel@tonic-gate 			ust = (swapent_t *)((swaptbl_t *)sc_arg)->swt_ent;
5860Sstevel@tonic-gate 			if (copyin(ust, &st, sizeof (swapent_t)) != 0)
5870Sstevel@tonic-gate 				return (EFAULT);
5880Sstevel@tonic-gate 			st.ste_start = PAGESIZE >> SCTRSHFT;
5890Sstevel@tonic-gate 			st.ste_length = (off_t)0;
5900Sstevel@tonic-gate 			st.ste_pages = 0;
5910Sstevel@tonic-gate 			st.ste_free = 0;
5920Sstevel@tonic-gate 			st.ste_flags = 0;
5937884Sgerald.jelinek@sun.com 
5940Sstevel@tonic-gate 			mutex_enter(&swapinfo_lock);
5950Sstevel@tonic-gate 			for (sip = swapinfo, nswap = 0;
5960Sstevel@tonic-gate 			    sip != NULL && nswap < nswapfiles;
5970Sstevel@tonic-gate 			    sip = sip->si_next, nswap++) {
5980Sstevel@tonic-gate 				st.ste_length +=
5990Sstevel@tonic-gate 				    (sip->si_eoff - sip->si_soff) >> SCTRSHFT;
6000Sstevel@tonic-gate 				st.ste_pages += sip->si_npgs;
6010Sstevel@tonic-gate 				st.ste_free += sip->si_nfpgs;
6020Sstevel@tonic-gate 			}
6030Sstevel@tonic-gate 			mutex_exit(&swapinfo_lock);
6047884Sgerald.jelinek@sun.com 
6057884Sgerald.jelinek@sun.com 			if (zp->zone_max_swap_ctl != UINT64_MAX) {
6067884Sgerald.jelinek@sun.com 				rctl_qty_t cap, used;
6077884Sgerald.jelinek@sun.com 
6087884Sgerald.jelinek@sun.com 				mutex_enter(&zp->zone_mem_lock);
6097884Sgerald.jelinek@sun.com 				cap = zp->zone_max_swap_ctl;
6107884Sgerald.jelinek@sun.com 				used = zp->zone_max_swap;
6117884Sgerald.jelinek@sun.com 				mutex_exit(&zp->zone_mem_lock);
6127884Sgerald.jelinek@sun.com 
6137884Sgerald.jelinek@sun.com 				st.ste_length = MIN(cap, st.ste_length);
6147884Sgerald.jelinek@sun.com 				st.ste_pages = MIN(btop(cap), st.ste_pages);
6157884Sgerald.jelinek@sun.com 				st.ste_free = MIN(st.ste_pages - btop(used),
6167884Sgerald.jelinek@sun.com 				    st.ste_free);
6177884Sgerald.jelinek@sun.com 			}
6187884Sgerald.jelinek@sun.com 
6190Sstevel@tonic-gate 			if (copyout(&st, ust, sizeof (swapent_t)) != 0 ||
6200Sstevel@tonic-gate 			    copyout(swappath, st.ste_path,
6215665Sstans 			    strlen(swappath) + 1) != 0) {
6220Sstevel@tonic-gate 				return (EFAULT);
6230Sstevel@tonic-gate 			}
6240Sstevel@tonic-gate 			*rv = 1;
6250Sstevel@tonic-gate 			return (0);
6260Sstevel@tonic-gate 		}
6270Sstevel@tonic-gate beginning:
6280Sstevel@tonic-gate 		tmp_nswapfiles = nswapfiles;
6290Sstevel@tonic-gate 		/* Return an error if not enough space for the whole table. */
6300Sstevel@tonic-gate 		if (length < tmp_nswapfiles)
6310Sstevel@tonic-gate 			return (ENOMEM);
6320Sstevel@tonic-gate 		/*
6330Sstevel@tonic-gate 		 * Get memory to hold the swap entries and their names. We'll
6340Sstevel@tonic-gate 		 * copy the real entries into these and then copy these out.
6350Sstevel@tonic-gate 		 * Allocating the pathname memory is only a guess so we may
6360Sstevel@tonic-gate 		 * find that we need more and have to do it again.
6370Sstevel@tonic-gate 		 * All this is because we have to hold the anon lock while
6380Sstevel@tonic-gate 		 * traversing the swapinfo list, and we can't be doing copyouts
6390Sstevel@tonic-gate 		 * and/or kmem_alloc()s during this.
6400Sstevel@tonic-gate 		 */
6410Sstevel@tonic-gate 		csip = kmem_zalloc(tmp_nswapfiles * sizeof (struct swapinfo),
6420Sstevel@tonic-gate 		    KM_SLEEP);
6430Sstevel@tonic-gate retry:
6440Sstevel@tonic-gate 		nlen = tmp_nswapfiles * (gplen += 100);
6450Sstevel@tonic-gate 		pname = kmem_zalloc(nlen, KM_SLEEP);
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 		mutex_enter(&swapinfo_lock);
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 		if (tmp_nswapfiles != nswapfiles) {
6500Sstevel@tonic-gate 			mutex_exit(&swapinfo_lock);
6510Sstevel@tonic-gate 			kmem_free(pname, nlen);
6520Sstevel@tonic-gate 			kmem_free(csip,
6530Sstevel@tonic-gate 			    tmp_nswapfiles * sizeof (struct swapinfo));
6540Sstevel@tonic-gate 			gplen = 0;
6550Sstevel@tonic-gate 			goto beginning;
6560Sstevel@tonic-gate 		}
6570Sstevel@tonic-gate 		for (sip = swapinfo, tsip = csip, tpname = pname, nswap = 0;
6580Sstevel@tonic-gate 		    sip && nswap < tmp_nswapfiles;
6590Sstevel@tonic-gate 		    sip = sip->si_next, tsip++, tpname += plen, nswap++) {
6600Sstevel@tonic-gate 			plen = sip->si_pnamelen;
6610Sstevel@tonic-gate 			if (tpname + plen - pname > nlen) {
6620Sstevel@tonic-gate 				mutex_exit(&swapinfo_lock);
6630Sstevel@tonic-gate 				kmem_free(pname, nlen);
6640Sstevel@tonic-gate 				goto retry;
6650Sstevel@tonic-gate 			}
6660Sstevel@tonic-gate 			*tsip = *sip;
6670Sstevel@tonic-gate 			tsip->si_pname = tpname;
6680Sstevel@tonic-gate 			(void) strcpy(tsip->si_pname, sip->si_pname);
6690Sstevel@tonic-gate 		}
6700Sstevel@tonic-gate 		mutex_exit(&swapinfo_lock);
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 		if (sip) {
6730Sstevel@tonic-gate 			error = ENOMEM;
6740Sstevel@tonic-gate 			goto lout;
6750Sstevel@tonic-gate 		}
6760Sstevel@tonic-gate 		ust = (swapent_t *)((swaptbl_t *)sc_arg)->swt_ent;
6770Sstevel@tonic-gate 		for (tsip = csip, cnt = 0; cnt < nswap;  tsip++, ust++, cnt++) {
6780Sstevel@tonic-gate 			if (copyin(ust, &st, sizeof (swapent_t)) != 0) {
6790Sstevel@tonic-gate 				error = EFAULT;
6800Sstevel@tonic-gate 				goto lout;
6810Sstevel@tonic-gate 			}
6820Sstevel@tonic-gate 			st.ste_flags = tsip->si_flags;
6830Sstevel@tonic-gate 			st.ste_length =
6840Sstevel@tonic-gate 			    (tsip->si_eoff - tsip->si_soff) >> SCTRSHFT;
6850Sstevel@tonic-gate 			st.ste_start = tsip->si_soff >> SCTRSHFT;
6860Sstevel@tonic-gate 			st.ste_pages = tsip->si_npgs;
6870Sstevel@tonic-gate 			st.ste_free = tsip->si_nfpgs;
6880Sstevel@tonic-gate 			if (copyout(&st, ust, sizeof (swapent_t)) != 0) {
6890Sstevel@tonic-gate 				error = EFAULT;
6900Sstevel@tonic-gate 				goto lout;
6910Sstevel@tonic-gate 			}
6920Sstevel@tonic-gate 			if (!tsip->si_pnamelen)
6930Sstevel@tonic-gate 				continue;
6940Sstevel@tonic-gate 			if (copyout(tsip->si_pname, st.ste_path,
6955665Sstans 			    tsip->si_pnamelen) != 0) {
6960Sstevel@tonic-gate 				error = EFAULT;
6970Sstevel@tonic-gate 				goto lout;
6980Sstevel@tonic-gate 			}
6990Sstevel@tonic-gate 		}
7000Sstevel@tonic-gate 		*rv = nswap;
7010Sstevel@tonic-gate lout:
7020Sstevel@tonic-gate 		kmem_free(csip, tmp_nswapfiles * sizeof (struct swapinfo));
7030Sstevel@tonic-gate 		kmem_free(pname, nlen);
7040Sstevel@tonic-gate 		return (error);
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	case SC_ADD:
7070Sstevel@tonic-gate 	case SC_REMOVE:
7080Sstevel@tonic-gate 		break;
7090Sstevel@tonic-gate 	default:
7100Sstevel@tonic-gate 		return (EINVAL);
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate 	if ((error = secpolicy_swapctl(CRED())) != 0)
7130Sstevel@tonic-gate 		return (error);
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	if (copyin(sc_arg, &sr, sizeof (swapres_t)))
7160Sstevel@tonic-gate 		return (EFAULT);
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	/* Allocate the space to read in pathname */
7190Sstevel@tonic-gate 	if ((swapname = kmem_alloc(MAXPATHLEN, KM_NOSLEEP)) == NULL)
7200Sstevel@tonic-gate 		return (ENOMEM);
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	error = copyinstr(sr.sr_name, swapname, MAXPATHLEN, 0);
7230Sstevel@tonic-gate 	if (error)
7240Sstevel@tonic-gate 		goto out;
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	error = lookupname(swapname, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
7270Sstevel@tonic-gate 	if (error) {
7280Sstevel@tonic-gate 		if (sc_cmd == SC_ADD)
7290Sstevel@tonic-gate 			goto out;
7300Sstevel@tonic-gate 		/* see if we match by name */
7310Sstevel@tonic-gate 		vp = swapdel_byname(swapname, (size_t)sr.sr_start);
7320Sstevel@tonic-gate 		if (vp == NULL)
7330Sstevel@tonic-gate 			goto out;
7340Sstevel@tonic-gate 	}
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	if (vp->v_flag & (VNOMAP | VNOSWAP)) {
7370Sstevel@tonic-gate 		VN_RELE(vp);
7380Sstevel@tonic-gate 		error = ENOSYS;
7390Sstevel@tonic-gate 		goto out;
7400Sstevel@tonic-gate 	}
7410Sstevel@tonic-gate 	switch (vp->v_type) {
7420Sstevel@tonic-gate 	case VBLK:
7430Sstevel@tonic-gate 		break;
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	case VREG:
7460Sstevel@tonic-gate 		if (vp->v_vfsp && vn_is_readonly(vp))
7470Sstevel@tonic-gate 			error = EROFS;
7480Sstevel@tonic-gate 		else
7495331Samw 			error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL);
7500Sstevel@tonic-gate 		break;
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	case VDIR:
7530Sstevel@tonic-gate 		error = EISDIR;
7540Sstevel@tonic-gate 		break;
7550Sstevel@tonic-gate 	default:
7560Sstevel@tonic-gate 		error = ENOSYS;
7570Sstevel@tonic-gate 		break;
7580Sstevel@tonic-gate 	}
7590Sstevel@tonic-gate 	if (error == 0) {
7600Sstevel@tonic-gate 		if (sc_cmd == SC_REMOVE)
7610Sstevel@tonic-gate 			error = swapdel(vp, sr.sr_start);
7620Sstevel@tonic-gate 		else
7630Sstevel@tonic-gate 			error = swapadd(vp, sr.sr_start,
7645665Sstans 			    sr.sr_length, swapname);
7650Sstevel@tonic-gate 	}
7660Sstevel@tonic-gate 	VN_RELE(vp);
7670Sstevel@tonic-gate out:
7680Sstevel@tonic-gate 	kmem_free(swapname, MAXPATHLEN);
7690Sstevel@tonic-gate 	return (error);
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate #if defined(_LP64) && defined(_SYSCALL32)
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate int
swapctl32(int sc_cmd,void * sc_arg,int * rv)7750Sstevel@tonic-gate swapctl32(int sc_cmd, void *sc_arg, int *rv)
7760Sstevel@tonic-gate {
7770Sstevel@tonic-gate 	struct swapinfo *sip, *csip, *tsip;
7780Sstevel@tonic-gate 	int error = 0;
7790Sstevel@tonic-gate 	struct swapent32 st, *ust;
7800Sstevel@tonic-gate 	struct swapres32 sr;
7810Sstevel@tonic-gate 	struct vnode *vp;
7820Sstevel@tonic-gate 	int cnt = 0;
7830Sstevel@tonic-gate 	int tmp_nswapfiles;
7840Sstevel@tonic-gate 	int nswap;
7850Sstevel@tonic-gate 	int length, nlen;
7860Sstevel@tonic-gate 	int gplen = 0, plen;
7870Sstevel@tonic-gate 	char *swapname;
7880Sstevel@tonic-gate 	char *pname;
7890Sstevel@tonic-gate 	char *tpname;
7900Sstevel@tonic-gate 	struct anoninfo32 ai;
7910Sstevel@tonic-gate 	size_t s;
7920Sstevel@tonic-gate 	spgcnt_t avail;
7937884Sgerald.jelinek@sun.com 	int global = INGLOBALZONE(curproc);
7947884Sgerald.jelinek@sun.com 	struct zone *zp = curproc->p_zone;
7950Sstevel@tonic-gate 
7967884Sgerald.jelinek@sun.com 	/*
7977884Sgerald.jelinek@sun.com 	 * When running in a zone we want to hide the details of the swap
7987884Sgerald.jelinek@sun.com 	 * devices: we report there only being one swap device named "swap"
7997884Sgerald.jelinek@sun.com 	 * having a size equal to the sum of the sizes of all real swap devices
8007884Sgerald.jelinek@sun.com 	 * on the system.
8017884Sgerald.jelinek@sun.com 	 */
8020Sstevel@tonic-gate 	switch (sc_cmd) {
8030Sstevel@tonic-gate 	case SC_GETNSWP:
8047884Sgerald.jelinek@sun.com 		if (global)
8057884Sgerald.jelinek@sun.com 			*rv = nswapfiles;
8067884Sgerald.jelinek@sun.com 		else
8077884Sgerald.jelinek@sun.com 			*rv = 1;
8080Sstevel@tonic-gate 		return (0);
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	case SC_AINFO:
8110Sstevel@tonic-gate 		/*
8120Sstevel@tonic-gate 		 * Return anoninfo information with these changes:
8130Sstevel@tonic-gate 		 * ani_max = maximum amount of swap space
8140Sstevel@tonic-gate 		 *	(including potentially available physical memory)
8150Sstevel@tonic-gate 		 * ani_free = amount of unallocated anonymous memory
8160Sstevel@tonic-gate 		 *	(some of which might be reserved and including
8170Sstevel@tonic-gate 		 *	 potentially available physical memory)
8180Sstevel@tonic-gate 		 * ani_resv = amount of claimed (reserved) anonymous memory
8190Sstevel@tonic-gate 		 */
8200Sstevel@tonic-gate 		avail = MAX((spgcnt_t)(availrmem - swapfs_minfree), 0);
8210Sstevel@tonic-gate 		s = (k_anoninfo.ani_max + k_anoninfo.ani_mem_resv) + avail;
8220Sstevel@tonic-gate 		if (s > UINT32_MAX)
8230Sstevel@tonic-gate 			return (EOVERFLOW);
8240Sstevel@tonic-gate 		ai.ani_max = s;
8250Sstevel@tonic-gate 
826*12908SPavel.Tatashin@Sun.COM 		/* Update ani_free */
827*12908SPavel.Tatashin@Sun.COM 		set_anoninfo();
8280Sstevel@tonic-gate 		s = k_anoninfo.ani_free + avail;
8290Sstevel@tonic-gate 		if (s > UINT32_MAX)
8300Sstevel@tonic-gate 			return (EOVERFLOW);
8310Sstevel@tonic-gate 		ai.ani_free = s;
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 		s = k_anoninfo.ani_phys_resv + k_anoninfo.ani_mem_resv;
8340Sstevel@tonic-gate 		if (s > UINT32_MAX)
8350Sstevel@tonic-gate 			return (EOVERFLOW);
8360Sstevel@tonic-gate 		ai.ani_resv = s;
8370Sstevel@tonic-gate 
8387884Sgerald.jelinek@sun.com 		if (!global && zp->zone_max_swap_ctl != UINT64_MAX) {
8397884Sgerald.jelinek@sun.com 			/*
8407884Sgerald.jelinek@sun.com 			 * We're in a non-global zone with a swap cap.  We
8417884Sgerald.jelinek@sun.com 			 * always report the system-wide values for the global
8427884Sgerald.jelinek@sun.com 			 * zone, even though it too can have a swap cap.
8437884Sgerald.jelinek@sun.com 			 * See the comment for the SC_AINFO case in swapctl()
8447884Sgerald.jelinek@sun.com 			 * which explains the following logic.
8457884Sgerald.jelinek@sun.com 			 */
8467884Sgerald.jelinek@sun.com 			rctl_qty_t cap, used;
8477884Sgerald.jelinek@sun.com 			pgcnt_t pgcap, sys_avail;
8487884Sgerald.jelinek@sun.com 
8497884Sgerald.jelinek@sun.com 			mutex_enter(&zp->zone_mem_lock);
8507884Sgerald.jelinek@sun.com 			cap = zp->zone_max_swap_ctl;
8517884Sgerald.jelinek@sun.com 			used = zp->zone_max_swap;
8527884Sgerald.jelinek@sun.com 			mutex_exit(&zp->zone_mem_lock);
8537884Sgerald.jelinek@sun.com 
8547884Sgerald.jelinek@sun.com 			pgcap = MIN(btop(cap), ai.ani_max);
8557884Sgerald.jelinek@sun.com 			ai.ani_free = pgcap - btop(used);
8567884Sgerald.jelinek@sun.com 
8577884Sgerald.jelinek@sun.com 			/* Get the system-wide swap currently available. */
8587884Sgerald.jelinek@sun.com 			sys_avail = ai.ani_max - ai.ani_resv;
8597884Sgerald.jelinek@sun.com 			if (sys_avail < ai.ani_free)
8607884Sgerald.jelinek@sun.com 				ai.ani_resv = pgcap - sys_avail;
8617884Sgerald.jelinek@sun.com 			else
8627884Sgerald.jelinek@sun.com 				ai.ani_resv = btop(used);
8637884Sgerald.jelinek@sun.com 
8647884Sgerald.jelinek@sun.com 			ai.ani_max = pgcap;
8657884Sgerald.jelinek@sun.com 		}
8667884Sgerald.jelinek@sun.com 
8670Sstevel@tonic-gate 		if (copyout(&ai, sc_arg, sizeof (ai)) != 0)
8680Sstevel@tonic-gate 			return (EFAULT);
8690Sstevel@tonic-gate 		return (0);
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	case SC_LIST:
8720Sstevel@tonic-gate 		if (copyin(sc_arg, &length, sizeof (int32_t)) != 0)
8730Sstevel@tonic-gate 			return (EFAULT);
8747884Sgerald.jelinek@sun.com 		if (!global) {
8757884Sgerald.jelinek@sun.com 			struct swapent32 st;
8767884Sgerald.jelinek@sun.com 			char *swappath = "swap";
8777884Sgerald.jelinek@sun.com 
8787884Sgerald.jelinek@sun.com 			if (length < 1)
8797884Sgerald.jelinek@sun.com 				return (ENOMEM);
8807884Sgerald.jelinek@sun.com 			ust = (swapent32_t *)((swaptbl32_t *)sc_arg)->swt_ent;
8817884Sgerald.jelinek@sun.com 			if (copyin(ust, &st, sizeof (swapent32_t)) != 0)
8827884Sgerald.jelinek@sun.com 				return (EFAULT);
8837884Sgerald.jelinek@sun.com 			st.ste_start = PAGESIZE >> SCTRSHFT;
8847884Sgerald.jelinek@sun.com 			st.ste_length = (off_t)0;
8857884Sgerald.jelinek@sun.com 			st.ste_pages = 0;
8867884Sgerald.jelinek@sun.com 			st.ste_free = 0;
8877884Sgerald.jelinek@sun.com 			st.ste_flags = 0;
8887884Sgerald.jelinek@sun.com 
8897884Sgerald.jelinek@sun.com 			mutex_enter(&swapinfo_lock);
8907884Sgerald.jelinek@sun.com 			for (sip = swapinfo, nswap = 0;
8917884Sgerald.jelinek@sun.com 			    sip != NULL && nswap < nswapfiles;
8927884Sgerald.jelinek@sun.com 			    sip = sip->si_next, nswap++) {
8937884Sgerald.jelinek@sun.com 				st.ste_length +=
8947884Sgerald.jelinek@sun.com 				    (sip->si_eoff - sip->si_soff) >> SCTRSHFT;
8957884Sgerald.jelinek@sun.com 				st.ste_pages += sip->si_npgs;
8967884Sgerald.jelinek@sun.com 				st.ste_free += sip->si_nfpgs;
8977884Sgerald.jelinek@sun.com 			}
8987884Sgerald.jelinek@sun.com 			mutex_exit(&swapinfo_lock);
8997884Sgerald.jelinek@sun.com 
9007884Sgerald.jelinek@sun.com 			if (zp->zone_max_swap_ctl != UINT64_MAX) {
9017884Sgerald.jelinek@sun.com 				rctl_qty_t cap, used;
9027884Sgerald.jelinek@sun.com 
9037884Sgerald.jelinek@sun.com 				mutex_enter(&zp->zone_mem_lock);
9047884Sgerald.jelinek@sun.com 				cap = zp->zone_max_swap_ctl;
9057884Sgerald.jelinek@sun.com 				used = zp->zone_max_swap;
9067884Sgerald.jelinek@sun.com 				mutex_exit(&zp->zone_mem_lock);
9077884Sgerald.jelinek@sun.com 
9087884Sgerald.jelinek@sun.com 				st.ste_length = MIN(cap, st.ste_length);
9097884Sgerald.jelinek@sun.com 				st.ste_pages = MIN(btop(cap), st.ste_pages);
9107884Sgerald.jelinek@sun.com 				st.ste_free = MIN(st.ste_pages - btop(used),
9117884Sgerald.jelinek@sun.com 				    st.ste_free);
9127884Sgerald.jelinek@sun.com 			}
9137884Sgerald.jelinek@sun.com 
9147884Sgerald.jelinek@sun.com 			if (copyout(&st, ust, sizeof (swapent32_t)) != 0 ||
9157884Sgerald.jelinek@sun.com 			    copyout(swappath, (caddr_t)(uintptr_t)st.ste_path,
9167884Sgerald.jelinek@sun.com 			    strlen(swappath) + 1) != 0) {
9177884Sgerald.jelinek@sun.com 				return (EFAULT);
9187884Sgerald.jelinek@sun.com 			}
9197884Sgerald.jelinek@sun.com 			*rv = 1;
9207884Sgerald.jelinek@sun.com 			return (0);
9217884Sgerald.jelinek@sun.com 		}
9220Sstevel@tonic-gate beginning:
9230Sstevel@tonic-gate 		tmp_nswapfiles = nswapfiles;
9240Sstevel@tonic-gate 		/* Return an error if not enough space for the whole table. */
9250Sstevel@tonic-gate 		if (length < tmp_nswapfiles)
9260Sstevel@tonic-gate 			return (ENOMEM);
9270Sstevel@tonic-gate 		/*
9280Sstevel@tonic-gate 		 * Get memory to hold the swap entries and their names. We'll
9290Sstevel@tonic-gate 		 * copy the real entries into these and then copy these out.
9300Sstevel@tonic-gate 		 * Allocating the pathname memory is only a guess so we may
9310Sstevel@tonic-gate 		 * find that we need more and have to do it again.
9320Sstevel@tonic-gate 		 * All this is because we have to hold the anon lock while
9330Sstevel@tonic-gate 		 * traversing the swapinfo list, and we can't be doing copyouts
9340Sstevel@tonic-gate 		 * and/or kmem_alloc()s during this.
9350Sstevel@tonic-gate 		 */
9360Sstevel@tonic-gate 		csip = kmem_zalloc(tmp_nswapfiles * sizeof (*csip), KM_SLEEP);
9370Sstevel@tonic-gate retry:
9380Sstevel@tonic-gate 		nlen = tmp_nswapfiles * (gplen += 100);
9390Sstevel@tonic-gate 		pname = kmem_zalloc(nlen, KM_SLEEP);
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 		mutex_enter(&swapinfo_lock);
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 		if (tmp_nswapfiles != nswapfiles) {
9440Sstevel@tonic-gate 			mutex_exit(&swapinfo_lock);
9450Sstevel@tonic-gate 			kmem_free(pname, nlen);
9460Sstevel@tonic-gate 			kmem_free(csip, tmp_nswapfiles * sizeof (*csip));
9470Sstevel@tonic-gate 			gplen = 0;
9480Sstevel@tonic-gate 			goto beginning;
9490Sstevel@tonic-gate 		}
9500Sstevel@tonic-gate 		for (sip = swapinfo, tsip = csip, tpname = pname, nswap = 0;
9510Sstevel@tonic-gate 		    (sip != NULL) && (nswap < tmp_nswapfiles);
9520Sstevel@tonic-gate 		    sip = sip->si_next, tsip++, tpname += plen, nswap++) {
9530Sstevel@tonic-gate 			plen = sip->si_pnamelen;
9540Sstevel@tonic-gate 			if (tpname + plen - pname > nlen) {
9550Sstevel@tonic-gate 				mutex_exit(&swapinfo_lock);
9560Sstevel@tonic-gate 				kmem_free(pname, nlen);
9570Sstevel@tonic-gate 				goto retry;
9580Sstevel@tonic-gate 			}
9590Sstevel@tonic-gate 			*tsip = *sip;
9600Sstevel@tonic-gate 			tsip->si_pname = tpname;
9610Sstevel@tonic-gate 			(void) strcpy(tsip->si_pname, sip->si_pname);
9620Sstevel@tonic-gate 		}
9630Sstevel@tonic-gate 		mutex_exit(&swapinfo_lock);
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 		if (sip != NULL) {
9660Sstevel@tonic-gate 			error = ENOMEM;
9670Sstevel@tonic-gate 			goto lout;
9680Sstevel@tonic-gate 		}
9690Sstevel@tonic-gate 		ust = (swapent32_t *)((swaptbl32_t *)sc_arg)->swt_ent;
9700Sstevel@tonic-gate 		for (tsip = csip, cnt = 0; cnt < nswap;  tsip++, ust++, cnt++) {
9710Sstevel@tonic-gate 			if (copyin(ust, &st, sizeof (*ust)) != 0) {
9720Sstevel@tonic-gate 				error = EFAULT;
9730Sstevel@tonic-gate 				goto lout;
9740Sstevel@tonic-gate 			}
9750Sstevel@tonic-gate 			st.ste_flags = tsip->si_flags;
9760Sstevel@tonic-gate 			st.ste_length =
9770Sstevel@tonic-gate 			    (tsip->si_eoff - tsip->si_soff) >> SCTRSHFT;
9780Sstevel@tonic-gate 			st.ste_start = tsip->si_soff >> SCTRSHFT;
9790Sstevel@tonic-gate 			st.ste_pages = tsip->si_npgs;
9800Sstevel@tonic-gate 			st.ste_free = tsip->si_nfpgs;
9810Sstevel@tonic-gate 			if (copyout(&st, ust, sizeof (st)) != 0) {
9820Sstevel@tonic-gate 				error = EFAULT;
9830Sstevel@tonic-gate 				goto lout;
9840Sstevel@tonic-gate 			}
9850Sstevel@tonic-gate 			if (!tsip->si_pnamelen)
9860Sstevel@tonic-gate 				continue;
9870Sstevel@tonic-gate 			if (copyout(tsip->si_pname,
9880Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)st.ste_path,
9890Sstevel@tonic-gate 			    tsip->si_pnamelen) != 0) {
9900Sstevel@tonic-gate 				error = EFAULT;
9910Sstevel@tonic-gate 				goto lout;
9920Sstevel@tonic-gate 			}
9930Sstevel@tonic-gate 		}
9940Sstevel@tonic-gate 		*rv = nswap;
9950Sstevel@tonic-gate lout:
9960Sstevel@tonic-gate 		kmem_free(csip, tmp_nswapfiles * sizeof (*csip));
9970Sstevel@tonic-gate 		kmem_free(pname, nlen);
9980Sstevel@tonic-gate 		return (error);
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	case SC_ADD:
10010Sstevel@tonic-gate 	case SC_REMOVE:
10020Sstevel@tonic-gate 		break;
10030Sstevel@tonic-gate 	default:
10040Sstevel@tonic-gate 		return (EINVAL);
10050Sstevel@tonic-gate 	}
10060Sstevel@tonic-gate 	if ((error = secpolicy_swapctl(CRED())) != 0)
10070Sstevel@tonic-gate 		return (error);
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	if (copyin(sc_arg, &sr, sizeof (sr)))
10100Sstevel@tonic-gate 		return (EFAULT);
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 	/* Allocate the space to read in pathname */
10130Sstevel@tonic-gate 	if ((swapname = kmem_alloc(MAXPATHLEN, KM_NOSLEEP)) == NULL)
10140Sstevel@tonic-gate 		return (ENOMEM);
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	error = copyinstr((caddr_t)(uintptr_t)sr.sr_name,
10170Sstevel@tonic-gate 	    swapname, MAXPATHLEN, NULL);
10180Sstevel@tonic-gate 	if (error)
10190Sstevel@tonic-gate 		goto out;
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	error = lookupname(swapname, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
10220Sstevel@tonic-gate 	if (error) {
10230Sstevel@tonic-gate 		if (sc_cmd == SC_ADD)
10240Sstevel@tonic-gate 			goto out;
10250Sstevel@tonic-gate 		/* see if we match by name */
10260Sstevel@tonic-gate 		vp = swapdel_byname(swapname, (uint_t)sr.sr_start);
10270Sstevel@tonic-gate 		if (vp == NULL)
10280Sstevel@tonic-gate 			goto out;
10290Sstevel@tonic-gate 	}
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 	if (vp->v_flag & (VNOMAP | VNOSWAP)) {
10320Sstevel@tonic-gate 		VN_RELE(vp);
10330Sstevel@tonic-gate 		error = ENOSYS;
10340Sstevel@tonic-gate 		goto out;
10350Sstevel@tonic-gate 	}
10360Sstevel@tonic-gate 	switch (vp->v_type) {
10370Sstevel@tonic-gate 	case VBLK:
10380Sstevel@tonic-gate 		break;
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	case VREG:
10410Sstevel@tonic-gate 		if (vp->v_vfsp && vn_is_readonly(vp))
10420Sstevel@tonic-gate 			error = EROFS;
10430Sstevel@tonic-gate 		else
10445331Samw 			error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL);
10450Sstevel@tonic-gate 		break;
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	case VDIR:
10480Sstevel@tonic-gate 		error = EISDIR;
10490Sstevel@tonic-gate 		break;
10500Sstevel@tonic-gate 	default:
10510Sstevel@tonic-gate 		error = ENOSYS;
10520Sstevel@tonic-gate 		break;
10530Sstevel@tonic-gate 	}
10540Sstevel@tonic-gate 	if (error == 0) {
10550Sstevel@tonic-gate 		if (sc_cmd == SC_REMOVE)
10560Sstevel@tonic-gate 			error = swapdel(vp, sr.sr_start);
10570Sstevel@tonic-gate 		else
10580Sstevel@tonic-gate 			error = swapadd(vp, sr.sr_start, sr.sr_length,
10590Sstevel@tonic-gate 			    swapname);
10600Sstevel@tonic-gate 	}
10610Sstevel@tonic-gate 	VN_RELE(vp);
10620Sstevel@tonic-gate out:
10630Sstevel@tonic-gate 	kmem_free(swapname, MAXPATHLEN);
10640Sstevel@tonic-gate 	return (error);
10650Sstevel@tonic-gate }
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate #endif /* _LP64 && _SYSCALL32 */
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate /*
10700Sstevel@tonic-gate  * Add a new swap file.
10710Sstevel@tonic-gate  */
10720Sstevel@tonic-gate int
swapadd(struct vnode * vp,ulong_t lowblk,ulong_t nblks,char * swapname)10730Sstevel@tonic-gate swapadd(struct vnode *vp, ulong_t lowblk, ulong_t nblks, char *swapname)
10740Sstevel@tonic-gate {
10750Sstevel@tonic-gate 	struct swapinfo **sipp, *nsip = NULL, *esip = NULL;
10760Sstevel@tonic-gate 	struct vnode *cvp;
10770Sstevel@tonic-gate 	struct vattr vattr;
10780Sstevel@tonic-gate 	pgcnt_t pages;
10790Sstevel@tonic-gate 	u_offset_t soff, eoff;
10800Sstevel@tonic-gate 	int error;
10810Sstevel@tonic-gate 	ssize_t i, start, end;
10820Sstevel@tonic-gate 	ushort_t wasswap;
10830Sstevel@tonic-gate 	ulong_t startblk;
10840Sstevel@tonic-gate 	size_t	returned_mem;
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	SWAP_PRINT(SW_CTL, "swapadd: vp %p lowblk %ld nblks %ld swapname %s\n",
10870Sstevel@tonic-gate 	    vp, lowblk, nblks, swapname, 0);
10880Sstevel@tonic-gate 	/*
10890Sstevel@tonic-gate 	 * Get the real vnode. (If vp is not a specnode it just returns vp, so
10900Sstevel@tonic-gate 	 * it does the right thing, but having this code know about specnodes
10910Sstevel@tonic-gate 	 * violates the spirit of having it be indepedent of vnode type.)
10920Sstevel@tonic-gate 	 */
10930Sstevel@tonic-gate 	cvp = common_specvp(vp);
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	/*
10960Sstevel@tonic-gate 	 * Or in VISSWAP so file system has chance to deny swap-ons during open.
10970Sstevel@tonic-gate 	 */
10980Sstevel@tonic-gate 	mutex_enter(&cvp->v_lock);
10990Sstevel@tonic-gate 	wasswap = cvp->v_flag & VISSWAP;
11000Sstevel@tonic-gate 	cvp->v_flag |= VISSWAP;
11010Sstevel@tonic-gate 	mutex_exit(&cvp->v_lock);
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	mutex_enter(&swap_lock);
11045331Samw 	if (error = VOP_OPEN(&cvp, FREAD|FWRITE, CRED(), NULL)) {
11050Sstevel@tonic-gate 		mutex_exit(&swap_lock);
11060Sstevel@tonic-gate 		/* restore state of v_flag */
11070Sstevel@tonic-gate 		if (!wasswap) {
11080Sstevel@tonic-gate 			mutex_enter(&cvp->v_lock);
11090Sstevel@tonic-gate 			cvp->v_flag &= ~VISSWAP;
11100Sstevel@tonic-gate 			mutex_exit(&cvp->v_lock);
11110Sstevel@tonic-gate 		}
11120Sstevel@tonic-gate 		return (error);
11130Sstevel@tonic-gate 	}
11140Sstevel@tonic-gate 	mutex_exit(&swap_lock);
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	/*
11170Sstevel@tonic-gate 	 * Get partition size. Return error if empty partition,
11180Sstevel@tonic-gate 	 * or if request does not fit within the partition.
11190Sstevel@tonic-gate 	 * If this is the first swap device, we can reduce
11200Sstevel@tonic-gate 	 * the size of the swap area to match what is
11210Sstevel@tonic-gate 	 * available.  This can happen if the system was built
11220Sstevel@tonic-gate 	 * on a machine with a different size swap partition.
11230Sstevel@tonic-gate 	 */
11240Sstevel@tonic-gate 	vattr.va_mask = AT_SIZE;
11255331Samw 	if (error = VOP_GETATTR(cvp, &vattr, ATTR_COMM, CRED(), NULL))
11260Sstevel@tonic-gate 		goto out;
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	/*
11290Sstevel@tonic-gate 	 * Specfs returns a va_size of MAXOFFSET_T (UNKNOWN_SIZE) when the
11300Sstevel@tonic-gate 	 * size of the device can't be determined.
11310Sstevel@tonic-gate 	 */
11320Sstevel@tonic-gate 	if ((vattr.va_size == 0) || (vattr.va_size == MAXOFFSET_T)) {
11330Sstevel@tonic-gate 		error = EINVAL;
11340Sstevel@tonic-gate 		goto out;
11350Sstevel@tonic-gate 	}
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate #ifdef	_ILP32
11380Sstevel@tonic-gate 	/*
11390Sstevel@tonic-gate 	 * No support for large swap in 32-bit OS, if the size of the swap is
11400Sstevel@tonic-gate 	 * bigger than MAXOFF32_T then the size used by swapfs must be limited.
11410Sstevel@tonic-gate 	 * This limitation is imposed by the swap subsystem itself, a D_64BIT
11420Sstevel@tonic-gate 	 * driver as the target of swap operation should be able to field
11430Sstevel@tonic-gate 	 * the IO.
11440Sstevel@tonic-gate 	 */
11450Sstevel@tonic-gate 	if (vattr.va_size > MAXOFF32_T) {
11460Sstevel@tonic-gate 		cmn_err(CE_NOTE,
11475665Sstans 		    "!swap device %s truncated from 0x%llx to 0x%x bytes",
11485665Sstans 		    swapname, vattr.va_size, MAXOFF32_T);
11490Sstevel@tonic-gate 		vattr.va_size = MAXOFF32_T;
11500Sstevel@tonic-gate 	}
11510Sstevel@tonic-gate #endif	/* _ILP32 */
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	/* Fail if file not writeable (try to set size to current size) */
11540Sstevel@tonic-gate 	vattr.va_mask = AT_SIZE;
11550Sstevel@tonic-gate 	if (error = VOP_SETATTR(cvp, &vattr, 0, CRED(), NULL))
11560Sstevel@tonic-gate 		goto out;
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	/* Fail if fs does not support VOP_PAGEIO */
11595331Samw 	error = VOP_PAGEIO(cvp, (page_t *)NULL, (u_offset_t)0, 0, 0, CRED(),
11605665Sstans 	    NULL);
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	if (error == ENOSYS)
11630Sstevel@tonic-gate 		goto out;
11640Sstevel@tonic-gate 	else
11650Sstevel@tonic-gate 		error = 0;
11660Sstevel@tonic-gate 	/*
11670Sstevel@tonic-gate 	 * If swapping on the root filesystem don't put swap blocks that
11680Sstevel@tonic-gate 	 * correspond to the miniroot filesystem on the swap free list.
11690Sstevel@tonic-gate 	 */
11700Sstevel@tonic-gate 	if (cvp == rootdir)
11710Sstevel@tonic-gate 		startblk = roundup(MINIROOTSIZE<<SCTRSHFT, klustsize)>>SCTRSHFT;
11720Sstevel@tonic-gate 	else				/* Skip 1st page (disk label) */
11730Sstevel@tonic-gate 		startblk = (ulong_t)(lowblk ? lowblk : 1);
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	soff = startblk << SCTRSHFT;
11760Sstevel@tonic-gate 	if (soff >= vattr.va_size) {
11770Sstevel@tonic-gate 		error = EINVAL;
11780Sstevel@tonic-gate 		goto out;
11790Sstevel@tonic-gate 	}
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	/*
11820Sstevel@tonic-gate 	 * If user specified 0 blks, use the size of the device
11830Sstevel@tonic-gate 	 */
11840Sstevel@tonic-gate 	eoff = nblks ?  soff + (nblks - (startblk - lowblk) << SCTRSHFT) :
11855665Sstans 	    vattr.va_size;
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	SWAP_PRINT(SW_CTL, "swapadd: va_size %ld soff %ld eoff %ld\n",
11880Sstevel@tonic-gate 	    vattr.va_size, soff, eoff, 0, 0);
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	if (eoff > vattr.va_size) {
11910Sstevel@tonic-gate 		error = EINVAL;
11920Sstevel@tonic-gate 		goto out;
11930Sstevel@tonic-gate 	}
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	/*
11960Sstevel@tonic-gate 	 * The starting and ending offsets must be page aligned.
11970Sstevel@tonic-gate 	 * Round soff up to next page boundary, round eoff
11980Sstevel@tonic-gate 	 * down to previous page boundary.
11990Sstevel@tonic-gate 	 */
12000Sstevel@tonic-gate 	soff = ptob(btopr(soff));
12010Sstevel@tonic-gate 	eoff = ptob(btop(eoff));
12020Sstevel@tonic-gate 	if (soff >= eoff) {
12030Sstevel@tonic-gate 		SWAP_PRINT(SW_CTL, "swapadd: soff %ld >= eoff %ld\n",
12040Sstevel@tonic-gate 		    soff, eoff, 0, 0, 0);
12050Sstevel@tonic-gate 		error = EINVAL;
12060Sstevel@tonic-gate 		goto out;
12070Sstevel@tonic-gate 	}
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 	pages = btop(eoff - soff);
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	/* Allocate and partially set up the new swapinfo */
12120Sstevel@tonic-gate 	nsip = kmem_zalloc(sizeof (struct swapinfo), KM_SLEEP);
12130Sstevel@tonic-gate 	nsip->si_vp = cvp;
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	nsip->si_soff = soff;
12160Sstevel@tonic-gate 	nsip->si_eoff = eoff;
12170Sstevel@tonic-gate 	nsip->si_hint = 0;
12180Sstevel@tonic-gate 	nsip->si_checkcnt = nsip->si_alloccnt = 0;
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 	nsip->si_pnamelen = (int)strlen(swapname) + 1;
12210Sstevel@tonic-gate 	nsip->si_pname = (char *)kmem_zalloc(nsip->si_pnamelen, KM_SLEEP);
12220Sstevel@tonic-gate 	bcopy(swapname, nsip->si_pname, nsip->si_pnamelen - 1);
12230Sstevel@tonic-gate 	SWAP_PRINT(SW_CTL, "swapadd: allocating swapinfo for %s, %ld pages\n",
12240Sstevel@tonic-gate 	    swapname, pages, 0, 0, 0);
12250Sstevel@tonic-gate 	/*
12260Sstevel@tonic-gate 	 * Size of swapslots map in bytes
12270Sstevel@tonic-gate 	 */
12280Sstevel@tonic-gate 	nsip->si_mapsize = P2ROUNDUP(pages, NBBW) / NBBY;
12290Sstevel@tonic-gate 	nsip->si_swapslots = kmem_zalloc(nsip->si_mapsize, KM_SLEEP);
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	/*
12320Sstevel@tonic-gate 	 * Permanently set the bits that can't ever be allocated,
12330Sstevel@tonic-gate 	 * i.e. those from the ending offset to the round up slot for the
12340Sstevel@tonic-gate 	 * swapslots bit map.
12350Sstevel@tonic-gate 	 */
12360Sstevel@tonic-gate 	start = pages;
12370Sstevel@tonic-gate 	end = P2ROUNDUP(pages, NBBW);
12380Sstevel@tonic-gate 	for (i = start; i < end; i++) {
12390Sstevel@tonic-gate 		SWAP_PRINT(SW_CTL, "swapadd: set bit for page %ld\n", i,
12400Sstevel@tonic-gate 		    0, 0, 0, 0);
12410Sstevel@tonic-gate 		SETBIT(nsip->si_swapslots, i);
12420Sstevel@tonic-gate 	}
12430Sstevel@tonic-gate 	nsip->si_npgs = nsip->si_nfpgs = pages;
12440Sstevel@tonic-gate 	/*
12450Sstevel@tonic-gate 	 * Now check to see if we can add it. We wait til now to check because
12460Sstevel@tonic-gate 	 * we need the swapinfo_lock and we don't want sleep with it (e.g.,
12470Sstevel@tonic-gate 	 * during kmem_alloc()) while we're setting up the swapinfo.
12480Sstevel@tonic-gate 	 */
12490Sstevel@tonic-gate 	mutex_enter(&swapinfo_lock);
12500Sstevel@tonic-gate 	for (sipp = &swapinfo; (esip = *sipp) != NULL; sipp = &esip->si_next) {
12510Sstevel@tonic-gate 		if (esip->si_vp == cvp) {
12520Sstevel@tonic-gate 			if (esip->si_soff == soff && esip->si_npgs == pages &&
12530Sstevel@tonic-gate 			    (esip->si_flags & ST_DOINGDEL)) {
12540Sstevel@tonic-gate 				/*
12550Sstevel@tonic-gate 				 * We are adding a device that we are in the
12560Sstevel@tonic-gate 				 * middle of deleting. Just clear the
12570Sstevel@tonic-gate 				 * ST_DOINGDEL flag to signal this and
12580Sstevel@tonic-gate 				 * the deletion routine will eventually notice
12590Sstevel@tonic-gate 				 * it and add it back.
12600Sstevel@tonic-gate 				 */
12610Sstevel@tonic-gate 				esip->si_flags &= ~ST_DOINGDEL;
12620Sstevel@tonic-gate 				mutex_exit(&swapinfo_lock);
12630Sstevel@tonic-gate 				goto out;
12640Sstevel@tonic-gate 			}
12650Sstevel@tonic-gate 			/* disallow overlapping swap files */
12660Sstevel@tonic-gate 			if ((soff < esip->si_eoff) && (eoff > esip->si_soff)) {
12670Sstevel@tonic-gate 				error = EEXIST;
12680Sstevel@tonic-gate 				mutex_exit(&swapinfo_lock);
12690Sstevel@tonic-gate 				goto out;
12700Sstevel@tonic-gate 			}
12710Sstevel@tonic-gate 		}
12720Sstevel@tonic-gate 	}
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 	nswapfiles++;
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 	/*
12770Sstevel@tonic-gate 	 * add new swap device to list and shift allocations to it
12780Sstevel@tonic-gate 	 * before updating the anoninfo counters
12790Sstevel@tonic-gate 	 */
12800Sstevel@tonic-gate 	*sipp = nsip;
12810Sstevel@tonic-gate 	silast = nsip;
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate 	/*
12840Sstevel@tonic-gate 	 * Update the total amount of reservable swap space
12850Sstevel@tonic-gate 	 * accounting properly for swap space from physical memory
12860Sstevel@tonic-gate 	 */
12870Sstevel@tonic-gate 	/* New swap device soaks up currently reserved memory swap */
12880Sstevel@tonic-gate 	mutex_enter(&anoninfo_lock);
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
12910Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	k_anoninfo.ani_max += pages;
12940Sstevel@tonic-gate 	ANI_ADD(pages);
12950Sstevel@tonic-gate 	if (k_anoninfo.ani_mem_resv > k_anoninfo.ani_locked_swap) {
12960Sstevel@tonic-gate 		returned_mem = MIN(k_anoninfo.ani_mem_resv -
12970Sstevel@tonic-gate 		    k_anoninfo.ani_locked_swap,
12980Sstevel@tonic-gate 		    k_anoninfo.ani_max - k_anoninfo.ani_phys_resv);
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 		ANI_ADD(-returned_mem);
13010Sstevel@tonic-gate 		k_anoninfo.ani_free -= returned_mem;
13020Sstevel@tonic-gate 		k_anoninfo.ani_mem_resv -= returned_mem;
13030Sstevel@tonic-gate 		k_anoninfo.ani_phys_resv += returned_mem;
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 		mutex_enter(&freemem_lock);
13060Sstevel@tonic-gate 		availrmem += returned_mem;
13070Sstevel@tonic-gate 		mutex_exit(&freemem_lock);
13080Sstevel@tonic-gate 	}
13090Sstevel@tonic-gate 	/*
13100Sstevel@tonic-gate 	 * At boot time, to permit booting small memory machines using
13110Sstevel@tonic-gate 	 * only physical memory as swap space, we allowed a dangerously
13120Sstevel@tonic-gate 	 * large amount of memory to be used as swap space; now that
13130Sstevel@tonic-gate 	 * more physical backing store is available bump down the amount
13140Sstevel@tonic-gate 	 * we can get from memory to a safer size.
13150Sstevel@tonic-gate 	 */
13160Sstevel@tonic-gate 	if (swapfs_minfree < swapfs_desfree) {
13170Sstevel@tonic-gate 		mutex_enter(&freemem_lock);
13180Sstevel@tonic-gate 		if (availrmem > swapfs_desfree || !k_anoninfo.ani_mem_resv)
13190Sstevel@tonic-gate 			swapfs_minfree = swapfs_desfree;
13200Sstevel@tonic-gate 		mutex_exit(&freemem_lock);
13210Sstevel@tonic-gate 	}
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	SWAP_PRINT(SW_CTL, "swapadd: ani_max %ld ani_free %ld\n",
13240Sstevel@tonic-gate 	    k_anoninfo.ani_free, k_anoninfo.ani_free, 0, 0, 0);
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	mutex_exit(&anoninfo_lock);
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 	mutex_exit(&swapinfo_lock);
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate 	/* Initialize the dump device */
13310Sstevel@tonic-gate 	mutex_enter(&dump_lock);
13320Sstevel@tonic-gate 	if (dumpvp == NULL)
13330Sstevel@tonic-gate 		(void) dumpinit(vp, swapname, 0);
13340Sstevel@tonic-gate 	mutex_exit(&dump_lock);
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	VN_HOLD(cvp);
13370Sstevel@tonic-gate out:
13380Sstevel@tonic-gate 	if (error || esip) {
13390Sstevel@tonic-gate 		SWAP_PRINT(SW_CTL, "swapadd: error (%d)\n", error, 0, 0, 0, 0);
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 		if (!wasswap) {
13420Sstevel@tonic-gate 			mutex_enter(&cvp->v_lock);
13430Sstevel@tonic-gate 			cvp->v_flag &= ~VISSWAP;
13440Sstevel@tonic-gate 			mutex_exit(&cvp->v_lock);
13450Sstevel@tonic-gate 		}
13460Sstevel@tonic-gate 		if (nsip) {
13470Sstevel@tonic-gate 			kmem_free(nsip->si_swapslots, (size_t)nsip->si_mapsize);
13480Sstevel@tonic-gate 			kmem_free(nsip->si_pname, nsip->si_pnamelen);
13490Sstevel@tonic-gate 			kmem_free(nsip, sizeof (*nsip));
13500Sstevel@tonic-gate 		}
13510Sstevel@tonic-gate 		mutex_enter(&swap_lock);
13525331Samw 		(void) VOP_CLOSE(cvp, FREAD|FWRITE, 1, (offset_t)0, CRED(),
13535665Sstans 		    NULL);
13540Sstevel@tonic-gate 		mutex_exit(&swap_lock);
13550Sstevel@tonic-gate 	}
13560Sstevel@tonic-gate 	return (error);
13570Sstevel@tonic-gate }
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate /*
13600Sstevel@tonic-gate  * Delete a swap file.
13610Sstevel@tonic-gate  */
13620Sstevel@tonic-gate static int
swapdel(struct vnode * vp,ulong_t lowblk)13630Sstevel@tonic-gate swapdel(
13640Sstevel@tonic-gate 	struct vnode *vp,
13650Sstevel@tonic-gate 	ulong_t lowblk) /* Low block number of area to delete. */
13660Sstevel@tonic-gate {
13670Sstevel@tonic-gate 	struct swapinfo **sipp, *osip = NULL;
13680Sstevel@tonic-gate 	struct vnode *cvp;
13690Sstevel@tonic-gate 	u_offset_t soff;
13700Sstevel@tonic-gate 	int error = 0;
13710Sstevel@tonic-gate 	u_offset_t toff = 0;
13720Sstevel@tonic-gate 	struct vnode *tvp = NULL;
13730Sstevel@tonic-gate 	spgcnt_t pages;
13740Sstevel@tonic-gate 	struct anon **app, *ap;
13750Sstevel@tonic-gate 	kmutex_t *ahm;
13760Sstevel@tonic-gate 	pgcnt_t adjust_swap = 0;
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 	/* Find the swap file entry for the file to be deleted */
13790Sstevel@tonic-gate 	cvp = common_specvp(vp);
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate 	lowblk = lowblk ? lowblk : 1; 	/* Skip first page (disk label) */
13830Sstevel@tonic-gate 	soff = ptob(btopr(lowblk << SCTRSHFT)); /* must be page aligned */
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 	mutex_enter(&swapinfo_lock);
13860Sstevel@tonic-gate 	for (sipp = &swapinfo; (osip = *sipp) != NULL; sipp = &osip->si_next) {
13870Sstevel@tonic-gate 		if ((osip->si_vp == cvp) &&
13880Sstevel@tonic-gate 		    (osip->si_soff == soff) && (osip->si_flags == 0))
13890Sstevel@tonic-gate 			break;
13900Sstevel@tonic-gate 	}
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate 	/* If the file was not found, error.  */
13930Sstevel@tonic-gate 	if (osip == NULL) {
13940Sstevel@tonic-gate 		error = EINVAL;
13950Sstevel@tonic-gate 		mutex_exit(&swapinfo_lock);
13960Sstevel@tonic-gate 		goto out;
13970Sstevel@tonic-gate 	}
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 	pages = osip->si_npgs;
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate 	/*
14020Sstevel@tonic-gate 	 * Do not delete if we will be low on swap pages.
14030Sstevel@tonic-gate 	 */
14040Sstevel@tonic-gate 	mutex_enter(&anoninfo_lock);
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
14070Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 	mutex_enter(&freemem_lock);
14100Sstevel@tonic-gate 	if (((k_anoninfo.ani_max - k_anoninfo.ani_phys_resv) +
14110Sstevel@tonic-gate 	    MAX((spgcnt_t)(availrmem - swapfs_minfree), 0)) < pages) {
14120Sstevel@tonic-gate 		mutex_exit(&freemem_lock);
14130Sstevel@tonic-gate 		mutex_exit(&anoninfo_lock);
14140Sstevel@tonic-gate 		error = ENOMEM;
14150Sstevel@tonic-gate 		cmn_err(CE_WARN, "swapdel - too few free pages");
14160Sstevel@tonic-gate 		mutex_exit(&swapinfo_lock);
14170Sstevel@tonic-gate 		goto out;
14180Sstevel@tonic-gate 	}
14190Sstevel@tonic-gate 	mutex_exit(&freemem_lock);
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 	k_anoninfo.ani_max -= pages;
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	/* If needed, reserve memory swap to replace old device */
14240Sstevel@tonic-gate 	if (k_anoninfo.ani_phys_resv > k_anoninfo.ani_max) {
14250Sstevel@tonic-gate 		adjust_swap = k_anoninfo.ani_phys_resv - k_anoninfo.ani_max;
14260Sstevel@tonic-gate 		k_anoninfo.ani_phys_resv -= adjust_swap;
14270Sstevel@tonic-gate 		k_anoninfo.ani_mem_resv += adjust_swap;
14280Sstevel@tonic-gate 		mutex_enter(&freemem_lock);
14290Sstevel@tonic-gate 		availrmem -= adjust_swap;
14300Sstevel@tonic-gate 		mutex_exit(&freemem_lock);
14310Sstevel@tonic-gate 		ANI_ADD(adjust_swap);
14320Sstevel@tonic-gate 	}
14330Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
14340Sstevel@tonic-gate 	ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
14350Sstevel@tonic-gate 	mutex_exit(&anoninfo_lock);
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate 	ANI_ADD(-pages);
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 	/*
14400Sstevel@tonic-gate 	 * Set the delete flag.  This prevents anyone from allocating more
14410Sstevel@tonic-gate 	 * pages from this file. Also set ST_DOINGDEL. Someone who wants to
14420Sstevel@tonic-gate 	 * add the file back while we're deleting it will signify by clearing
14430Sstevel@tonic-gate 	 * this flag.
14440Sstevel@tonic-gate 	 */
14450Sstevel@tonic-gate 	osip->si_flags |= ST_INDEL|ST_DOINGDEL;
14460Sstevel@tonic-gate 	mutex_exit(&swapinfo_lock);
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 	/*
14490Sstevel@tonic-gate 	 * Free all the allocated physical slots for this file. We do this
14500Sstevel@tonic-gate 	 * by walking through the entire anon hash array, because we need
14510Sstevel@tonic-gate 	 * to update all the anon slots that have physical swap slots on
14520Sstevel@tonic-gate 	 * this file, and this is the only way to find them all. We go back
14530Sstevel@tonic-gate 	 * to the beginning of a bucket after each slot is freed because the
14540Sstevel@tonic-gate 	 * anonhash_lock is not held during the free and thus the hash table
14550Sstevel@tonic-gate 	 * may change under us.
14560Sstevel@tonic-gate 	 */
14570Sstevel@tonic-gate 	for (app = anon_hash; app < &anon_hash[ANON_HASH_SIZE]; app++) {
145812173SMichael.Corcoran@Sun.COM 		ahm = &anonhash_lock[(app - anon_hash) &
145912173SMichael.Corcoran@Sun.COM 		    (AH_LOCK_SIZE - 1)].pad_mutex;
14600Sstevel@tonic-gate 		mutex_enter(ahm);
14610Sstevel@tonic-gate top:
14620Sstevel@tonic-gate 		for (ap = *app; ap != NULL; ap = ap->an_hash) {
14630Sstevel@tonic-gate 			if (ap->an_pvp == cvp &&
14640Sstevel@tonic-gate 			    ap->an_poff >= osip->si_soff &&
14650Sstevel@tonic-gate 			    ap->an_poff < osip->si_eoff) {
14660Sstevel@tonic-gate 				ASSERT(TESTBIT(osip->si_swapslots,
14670Sstevel@tonic-gate 				    btop((size_t)(ap->an_poff -
14680Sstevel@tonic-gate 				    osip->si_soff))));
14690Sstevel@tonic-gate 				tvp = ap->an_vp;
14700Sstevel@tonic-gate 				toff = ap->an_off;
14710Sstevel@tonic-gate 				VN_HOLD(tvp);
14720Sstevel@tonic-gate 				mutex_exit(ahm);
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 				error = swapslot_free(tvp, toff, osip);
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 				VN_RELE(tvp);
14770Sstevel@tonic-gate 				mutex_enter(ahm);
14780Sstevel@tonic-gate 				if (!error && (osip->si_flags & ST_DOINGDEL)) {
14790Sstevel@tonic-gate 					goto top;
14800Sstevel@tonic-gate 				} else {
14810Sstevel@tonic-gate 					if (error) {
14820Sstevel@tonic-gate 						cmn_err(CE_WARN,
14830Sstevel@tonic-gate 						    "swapslot_free failed %d",
14840Sstevel@tonic-gate 						    error);
14850Sstevel@tonic-gate 					}
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 					/*
14880Sstevel@tonic-gate 					 * Add device back before making it
14890Sstevel@tonic-gate 					 * visible.
14900Sstevel@tonic-gate 					 */
14910Sstevel@tonic-gate 					mutex_enter(&swapinfo_lock);
14920Sstevel@tonic-gate 					osip->si_flags &=
14930Sstevel@tonic-gate 					    ~(ST_INDEL | ST_DOINGDEL);
14940Sstevel@tonic-gate 					mutex_exit(&swapinfo_lock);
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 					/*
14970Sstevel@tonic-gate 					 * Update the anon space available
14980Sstevel@tonic-gate 					 */
14990Sstevel@tonic-gate 					mutex_enter(&anoninfo_lock);
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 					k_anoninfo.ani_phys_resv += adjust_swap;
15020Sstevel@tonic-gate 					k_anoninfo.ani_mem_resv -= adjust_swap;
15030Sstevel@tonic-gate 					k_anoninfo.ani_max += pages;
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 					mutex_enter(&freemem_lock);
15060Sstevel@tonic-gate 					availrmem += adjust_swap;
15070Sstevel@tonic-gate 					mutex_exit(&freemem_lock);
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate 					mutex_exit(&anoninfo_lock);
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 					ANI_ADD(pages);
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 					mutex_exit(ahm);
15140Sstevel@tonic-gate 					goto out;
15150Sstevel@tonic-gate 				}
15160Sstevel@tonic-gate 			}
15170Sstevel@tonic-gate 		}
15180Sstevel@tonic-gate 		mutex_exit(ahm);
15190Sstevel@tonic-gate 	}
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate 	/* All done, they'd better all be free! */
15220Sstevel@tonic-gate 	mutex_enter(&swapinfo_lock);
15230Sstevel@tonic-gate 	ASSERT(osip->si_nfpgs == osip->si_npgs);
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 	/* Now remove it from the swapinfo list */
15260Sstevel@tonic-gate 	for (sipp = &swapinfo; *sipp != NULL; sipp = &(*sipp)->si_next) {
15270Sstevel@tonic-gate 		if (*sipp == osip)
15280Sstevel@tonic-gate 			break;
15290Sstevel@tonic-gate 	}
15300Sstevel@tonic-gate 	ASSERT(*sipp);
15310Sstevel@tonic-gate 	*sipp = osip->si_next;
15320Sstevel@tonic-gate 	if (silast == osip)
15330Sstevel@tonic-gate 		if ((silast = osip->si_next) == NULL)
15340Sstevel@tonic-gate 			silast = swapinfo;
15350Sstevel@tonic-gate 	nswapfiles--;
15360Sstevel@tonic-gate 	mutex_exit(&swapinfo_lock);
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	kmem_free(osip->si_swapslots, osip->si_mapsize);
15390Sstevel@tonic-gate 	kmem_free(osip->si_pname, osip->si_pnamelen);
15400Sstevel@tonic-gate 	kmem_free(osip, sizeof (*osip));
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	mutex_enter(&dump_lock);
15430Sstevel@tonic-gate 	if (cvp == dumpvp)
15440Sstevel@tonic-gate 		dumpfini();
15450Sstevel@tonic-gate 	mutex_exit(&dump_lock);
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 	/* Release the vnode */
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	mutex_enter(&swap_lock);
15505331Samw 	(void) VOP_CLOSE(cvp, FREAD|FWRITE, 1, (offset_t)0, CRED(), NULL);
15510Sstevel@tonic-gate 	mutex_enter(&cvp->v_lock);
15520Sstevel@tonic-gate 	cvp->v_flag &= ~VISSWAP;
15530Sstevel@tonic-gate 	mutex_exit(&cvp->v_lock);
15540Sstevel@tonic-gate 	VN_RELE(cvp);
15550Sstevel@tonic-gate 	mutex_exit(&swap_lock);
15560Sstevel@tonic-gate out:
15570Sstevel@tonic-gate 	return (error);
15580Sstevel@tonic-gate }
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate /*
15610Sstevel@tonic-gate  * Free up a physical swap slot on swapinfo sip, currently in use by the
15620Sstevel@tonic-gate  * anonymous page whose name is (vp, off).
15630Sstevel@tonic-gate  */
15640Sstevel@tonic-gate static int
swapslot_free(struct vnode * vp,u_offset_t off,struct swapinfo * sip)15650Sstevel@tonic-gate swapslot_free(
15660Sstevel@tonic-gate 	struct vnode *vp,
15670Sstevel@tonic-gate 	u_offset_t off,
15680Sstevel@tonic-gate 	struct swapinfo *sip)
15690Sstevel@tonic-gate {
15705665Sstans 	struct page *pp = NULL;
15710Sstevel@tonic-gate 	struct anon *ap = NULL;
15720Sstevel@tonic-gate 	int error = 0;
15730Sstevel@tonic-gate 	kmutex_t *ahm;
15745665Sstans 	struct vnode *pvp = NULL;
15755665Sstans 	u_offset_t poff;
15765665Sstans 	int	alloc_pg = 0;
15775665Sstans 
15785665Sstans 	ASSERT(sip->si_vp != NULL);
15795665Sstans 	/*
15805665Sstans 	 * Get the page for the old swap slot if exists or create a new one.
15815665Sstans 	 */
15825665Sstans again:
15835665Sstans 	if ((pp = page_lookup(vp, off, SE_SHARED)) == NULL) {
15845665Sstans 		pp = page_create_va(vp, off, PAGESIZE, PG_WAIT | PG_EXCL,
15855665Sstans 		    segkmap, NULL);
15865665Sstans 		if (pp == NULL)
15875665Sstans 			goto again;
15885665Sstans 		alloc_pg = 1;
15895665Sstans 
15905665Sstans 		error = swap_getphysname(vp, off, &pvp, &poff);
15915665Sstans 		if (error || pvp != sip->si_vp || poff < sip->si_soff ||
15925665Sstans 		    poff >= sip->si_eoff) {
15935665Sstans 			page_io_unlock(pp);
15945665Sstans 			/*LINTED: constant in conditional context*/
15955665Sstans 			VN_DISPOSE(pp, B_INVAL, 0, kcred);
15965665Sstans 			return (0);
15975665Sstans 		}
15985665Sstans 
15995665Sstans 		error = VOP_PAGEIO(pvp, pp, poff, PAGESIZE, B_READ,
16005665Sstans 		    CRED(), NULL);
16015665Sstans 		if (error) {
16025665Sstans 			page_io_unlock(pp);
16035665Sstans 			if (error == EFAULT)
16045665Sstans 				error = 0;
16055665Sstans 			/*LINTED: constant in conditional context*/
16065665Sstans 			VN_DISPOSE(pp, B_INVAL, 0, kcred);
16075665Sstans 			return (error);
16085665Sstans 		}
16095665Sstans 	}
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	/*
16125665Sstans 	 * The anon could have been removed by anon_decref* and/or reallocated
16135665Sstans 	 * by anon layer (an_pvp == NULL) with the same vp, off.
16145665Sstans 	 * In this case the page which has been allocated needs to
16155665Sstans 	 * be freed.
16160Sstevel@tonic-gate 	 */
16175665Sstans 	if (!alloc_pg)
16185665Sstans 		page_io_lock(pp);
161912173SMichael.Corcoran@Sun.COM 	ahm = AH_MUTEX(vp, off);
16200Sstevel@tonic-gate 	mutex_enter(ahm);
16215665Sstans 	ap = swap_anon(vp, off);
16225665Sstans 	if ((ap == NULL || ap->an_pvp == NULL) && alloc_pg) {
16235665Sstans 		mutex_exit(ahm);
16245665Sstans 		page_io_unlock(pp);
16255665Sstans 		/*LINTED: constant in conditional context*/
16265665Sstans 		VN_DISPOSE(pp, B_INVAL, 0, kcred);
16275665Sstans 		return (0);
16280Sstevel@tonic-gate 	}
16295665Sstans 
16300Sstevel@tonic-gate 	/*
16310Sstevel@tonic-gate 	 * Free the physical slot. It may have been freed up and replaced with
16320Sstevel@tonic-gate 	 * another one while we were getting the page so we have to re-verify
16330Sstevel@tonic-gate 	 * that this is really one we want. If we do free the slot we have
16340Sstevel@tonic-gate 	 * to mark the page modified, as its backing store is now gone.
16350Sstevel@tonic-gate 	 */
16365665Sstans 	if ((ap != NULL) && (ap->an_pvp == sip->si_vp && ap->an_poff >=
16375665Sstans 	    sip->si_soff && ap->an_poff < sip->si_eoff)) {
16380Sstevel@tonic-gate 		swap_phys_free(ap->an_pvp, ap->an_poff, PAGESIZE);
16390Sstevel@tonic-gate 		ap->an_pvp = NULL;
16405665Sstans 		ap->an_poff = 0;
16410Sstevel@tonic-gate 		mutex_exit(ahm);
16420Sstevel@tonic-gate 		hat_setmod(pp);
16430Sstevel@tonic-gate 	} else {
16440Sstevel@tonic-gate 		mutex_exit(ahm);
16450Sstevel@tonic-gate 	}
16465665Sstans 	page_io_unlock(pp);
16470Sstevel@tonic-gate 	page_unlock(pp);
16485665Sstans 	return (0);
16490Sstevel@tonic-gate }
16500Sstevel@tonic-gate 
16515665Sstans 
16520Sstevel@tonic-gate /*
16530Sstevel@tonic-gate  * Get contig physical backing store for vp, in the range
16540Sstevel@tonic-gate  * [*offp, *offp + *lenp), May back a subrange of this, but must
16550Sstevel@tonic-gate  * always include the requested offset or fail. Returns the offsets
16560Sstevel@tonic-gate  * backed as [*offp, *offp + *lenp) and the physical offsets used to
16570Sstevel@tonic-gate  * back them from *pvpp in the range [*pstartp, *pstartp + *lenp).
16580Sstevel@tonic-gate  * Returns 	0 for success
16590Sstevel@tonic-gate  * 		SE_NOANON -- no anon slot for requested paged
16600Sstevel@tonic-gate  *		SE_NOSWAP -- no physical swap space available
16610Sstevel@tonic-gate  */
16620Sstevel@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)16630Sstevel@tonic-gate swap_newphysname(
16640Sstevel@tonic-gate 	struct vnode *vp,
16650Sstevel@tonic-gate 	u_offset_t offset,
16660Sstevel@tonic-gate 	u_offset_t *offp,
16670Sstevel@tonic-gate 	size_t *lenp,
16680Sstevel@tonic-gate 	struct vnode **pvpp,
16690Sstevel@tonic-gate 	u_offset_t *poffp)
16700Sstevel@tonic-gate {
16710Sstevel@tonic-gate 	struct anon *ap = NULL;		/* anon slot for vp, off */
16720Sstevel@tonic-gate 	int error = 0;
16730Sstevel@tonic-gate 	struct vnode *pvp;
16740Sstevel@tonic-gate 	u_offset_t poff, pstart, prem;
16750Sstevel@tonic-gate 	size_t plen;
16760Sstevel@tonic-gate 	u_offset_t off, start;
16770Sstevel@tonic-gate 	kmutex_t *ahm;
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate 	ASSERT(*offp <= offset && offset < *offp + *lenp);
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 	/* Get new physical swap slots. */
16820Sstevel@tonic-gate 	plen = *lenp;
16830Sstevel@tonic-gate 	if (!swap_phys_alloc(&pvp, &pstart, &plen, 0)) {
16840Sstevel@tonic-gate 		/*
16850Sstevel@tonic-gate 		 * No swap available so return error unless requested
16860Sstevel@tonic-gate 		 * offset is already backed in which case return that.
16870Sstevel@tonic-gate 		 */
168812173SMichael.Corcoran@Sun.COM 		ahm = AH_MUTEX(vp, offset);
16890Sstevel@tonic-gate 		mutex_enter(ahm);
16900Sstevel@tonic-gate 		if ((ap = swap_anon(vp, offset)) == NULL) {
16910Sstevel@tonic-gate 			error = SE_NOANON;
16920Sstevel@tonic-gate 			mutex_exit(ahm);
16930Sstevel@tonic-gate 			return (error);
16940Sstevel@tonic-gate 		}
16950Sstevel@tonic-gate 		error = (ap->an_pvp ? 0 : SE_NOSWAP);
16960Sstevel@tonic-gate 		*offp = offset;
16970Sstevel@tonic-gate 		*lenp = PAGESIZE;
16980Sstevel@tonic-gate 		*pvpp = ap->an_pvp;
16990Sstevel@tonic-gate 		*poffp = ap->an_poff;
17000Sstevel@tonic-gate 		mutex_exit(ahm);
17010Sstevel@tonic-gate 		return (error);
17020Sstevel@tonic-gate 	}
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 	/*
17050Sstevel@tonic-gate 	 * We got plen (<= *lenp) contig slots. Use these to back a
17060Sstevel@tonic-gate 	 * subrange of [*offp, *offp + *lenp) which includes offset.
17070Sstevel@tonic-gate 	 * For now we just put offset at the end of the kluster.
17080Sstevel@tonic-gate 	 * Clearly there are other possible choices - which is best?
17090Sstevel@tonic-gate 	 */
17100Sstevel@tonic-gate 	start = MAX(*offp,
17110Sstevel@tonic-gate 	    (offset + PAGESIZE > plen) ? (offset + PAGESIZE - plen) : 0);
17120Sstevel@tonic-gate 	ASSERT(start + plen <= *offp + *lenp);
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 	for (off = start, poff = pstart; poff < pstart + plen;
17150Sstevel@tonic-gate 	    off += PAGESIZE, poff += PAGESIZE) {
171612173SMichael.Corcoran@Sun.COM 		ahm = AH_MUTEX(vp, off);
17170Sstevel@tonic-gate 		mutex_enter(ahm);
17180Sstevel@tonic-gate 		if ((ap = swap_anon(vp, off)) != NULL) {
17190Sstevel@tonic-gate 			/* Free old slot if any, and assign new one */
17200Sstevel@tonic-gate 			if (ap->an_pvp)
17210Sstevel@tonic-gate 				swap_phys_free(ap->an_pvp, ap->an_poff,
17220Sstevel@tonic-gate 				    PAGESIZE);
17230Sstevel@tonic-gate 			ap->an_pvp = pvp;
17240Sstevel@tonic-gate 			ap->an_poff = poff;
17250Sstevel@tonic-gate 		} else {	/* No anon slot for a klustered page, quit. */
17260Sstevel@tonic-gate 			prem = (pstart + plen) - poff;
17270Sstevel@tonic-gate 			/* Already did requested page, do partial kluster */
17280Sstevel@tonic-gate 			if (off > offset) {
17290Sstevel@tonic-gate 				plen = poff - pstart;
17300Sstevel@tonic-gate 				error = 0;
17310Sstevel@tonic-gate 			/* Fail on requested page, error */
17320Sstevel@tonic-gate 			} else if (off == offset)  {
17330Sstevel@tonic-gate 				error = SE_NOANON;
17340Sstevel@tonic-gate 			/* Fail on prior page, fail on requested page, error */
17350Sstevel@tonic-gate 			} else if ((ap = swap_anon(vp, offset)) == NULL) {
17360Sstevel@tonic-gate 				error = SE_NOANON;
17370Sstevel@tonic-gate 			/* Fail on prior page, got requested page, do only it */
17380Sstevel@tonic-gate 			} else {
17390Sstevel@tonic-gate 				/* Free old slot if any, and assign new one */
17400Sstevel@tonic-gate 				if (ap->an_pvp)
17410Sstevel@tonic-gate 					swap_phys_free(ap->an_pvp, ap->an_poff,
17420Sstevel@tonic-gate 					    PAGESIZE);
17430Sstevel@tonic-gate 				ap->an_pvp = pvp;
17440Sstevel@tonic-gate 				ap->an_poff = poff;
17450Sstevel@tonic-gate 				/* One page kluster */
17460Sstevel@tonic-gate 				start = offset;
17470Sstevel@tonic-gate 				plen = PAGESIZE;
17480Sstevel@tonic-gate 				pstart = poff;
17490Sstevel@tonic-gate 				poff += PAGESIZE;
17500Sstevel@tonic-gate 				prem -= PAGESIZE;
17510Sstevel@tonic-gate 			}
17520Sstevel@tonic-gate 			/* Free unassigned slots */
17530Sstevel@tonic-gate 			swap_phys_free(pvp, poff, prem);
17540Sstevel@tonic-gate 			mutex_exit(ahm);
17550Sstevel@tonic-gate 			break;
17560Sstevel@tonic-gate 		}
17570Sstevel@tonic-gate 		mutex_exit(ahm);
17580Sstevel@tonic-gate 	}
17590Sstevel@tonic-gate 	ASSERT(*offp <= start && start + plen <= *offp + *lenp);
17600Sstevel@tonic-gate 	ASSERT(start <= offset && offset < start + plen);
17610Sstevel@tonic-gate 	*offp = start;
17620Sstevel@tonic-gate 	*lenp = plen;
17630Sstevel@tonic-gate 	*pvpp = pvp;
17640Sstevel@tonic-gate 	*poffp = pstart;
17650Sstevel@tonic-gate 	return (error);
17660Sstevel@tonic-gate }
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate 
17690Sstevel@tonic-gate /*
17700Sstevel@tonic-gate  * Get the physical swap backing store location for a given anonymous page
17710Sstevel@tonic-gate  * named (vp, off). The backing store name is returned in (*pvpp, *poffp).
17720Sstevel@tonic-gate  * Returns	0 		success
17730Sstevel@tonic-gate  *		EIDRM --	no anon slot (page is not allocated)
17740Sstevel@tonic-gate  */
17750Sstevel@tonic-gate int
swap_getphysname(struct vnode * vp,u_offset_t off,struct vnode ** pvpp,u_offset_t * poffp)17760Sstevel@tonic-gate swap_getphysname(
17770Sstevel@tonic-gate 	struct vnode *vp,
17780Sstevel@tonic-gate 	u_offset_t off,
17790Sstevel@tonic-gate 	struct vnode **pvpp,
17800Sstevel@tonic-gate 	u_offset_t *poffp)
17810Sstevel@tonic-gate {
17820Sstevel@tonic-gate 	struct anon *ap;
17830Sstevel@tonic-gate 	int error = 0;
17840Sstevel@tonic-gate 	kmutex_t *ahm;
17850Sstevel@tonic-gate 
178612173SMichael.Corcoran@Sun.COM 	ahm = AH_MUTEX(vp, off);
17870Sstevel@tonic-gate 	mutex_enter(ahm);
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 	/* Get anon slot for vp, off */
17900Sstevel@tonic-gate 	ap = swap_anon(vp, off);
17910Sstevel@tonic-gate 	if (ap == NULL) {
17920Sstevel@tonic-gate 		error = EIDRM;
17930Sstevel@tonic-gate 		goto out;
17940Sstevel@tonic-gate 	}
17950Sstevel@tonic-gate 	*pvpp = ap->an_pvp;
17960Sstevel@tonic-gate 	*poffp = ap->an_poff;
17970Sstevel@tonic-gate out:
17980Sstevel@tonic-gate 	mutex_exit(ahm);
17990Sstevel@tonic-gate 	return (error);
18000Sstevel@tonic-gate }
1801