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 5*5331Samw * Common Development and Distribution License (the "License"). 6*5331Samw * 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 /* 22*5331Samw * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 270Sstevel@tonic-gate /* All Rights Reserved */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 310Sstevel@tonic-gate * The Regents of the University of California 320Sstevel@tonic-gate * All Rights Reserved 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 350Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 360Sstevel@tonic-gate * contributors. 370Sstevel@tonic-gate */ 380Sstevel@tonic-gate 390Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 400Sstevel@tonic-gate 410Sstevel@tonic-gate /* 420Sstevel@tonic-gate * Each physical swap area has an associated bitmap representing 430Sstevel@tonic-gate * its physical storage. The bitmap records which swap slots are 440Sstevel@tonic-gate * currently allocated or freed. Allocation is done by searching 450Sstevel@tonic-gate * through the bitmap for the first free slot. Thus, there's 460Sstevel@tonic-gate * no linear relation between offset within the swap device and the 470Sstevel@tonic-gate * address (within its segment(s)) of the page that the slot backs; 480Sstevel@tonic-gate * instead, it's an arbitrary one-to-one mapping. 490Sstevel@tonic-gate * 500Sstevel@tonic-gate * Associated with each swap area is a swapinfo structure. These 510Sstevel@tonic-gate * structures are linked into a linear list that determines the 520Sstevel@tonic-gate * ordering of swap areas in the logical swap device. Each contains a 530Sstevel@tonic-gate * pointer to the corresponding bitmap, the area's size, and its 540Sstevel@tonic-gate * associated vnode. 550Sstevel@tonic-gate */ 560Sstevel@tonic-gate 570Sstevel@tonic-gate #include <sys/types.h> 580Sstevel@tonic-gate #include <sys/inttypes.h> 590Sstevel@tonic-gate #include <sys/param.h> 600Sstevel@tonic-gate #include <sys/t_lock.h> 610Sstevel@tonic-gate #include <sys/sysmacros.h> 620Sstevel@tonic-gate #include <sys/systm.h> 630Sstevel@tonic-gate #include <sys/errno.h> 640Sstevel@tonic-gate #include <sys/kmem.h> 650Sstevel@tonic-gate #include <sys/vfs.h> 660Sstevel@tonic-gate #include <sys/vnode.h> 670Sstevel@tonic-gate #include <sys/pathname.h> 680Sstevel@tonic-gate #include <sys/cmn_err.h> 690Sstevel@tonic-gate #include <sys/vtrace.h> 700Sstevel@tonic-gate #include <sys/swap.h> 710Sstevel@tonic-gate #include <sys/dumphdr.h> 720Sstevel@tonic-gate #include <sys/debug.h> 730Sstevel@tonic-gate #include <sys/fs/snode.h> 740Sstevel@tonic-gate #include <sys/fs/swapnode.h> 750Sstevel@tonic-gate #include <sys/policy.h> 760Sstevel@tonic-gate #include <sys/zone.h> 770Sstevel@tonic-gate 780Sstevel@tonic-gate #include <vm/as.h> 790Sstevel@tonic-gate #include <vm/seg.h> 800Sstevel@tonic-gate #include <vm/page.h> 810Sstevel@tonic-gate #include <vm/seg_vn.h> 820Sstevel@tonic-gate #include <vm/hat.h> 830Sstevel@tonic-gate #include <vm/anon.h> 840Sstevel@tonic-gate #include <vm/seg_map.h> 850Sstevel@tonic-gate 860Sstevel@tonic-gate /* 870Sstevel@tonic-gate * To balance the load among multiple swap areas, we don't allow 880Sstevel@tonic-gate * more than swap_maxcontig allocations to be satisfied from a 890Sstevel@tonic-gate * single swap area before moving on to the next swap area. This 900Sstevel@tonic-gate * effectively "interleaves" allocations among the many swap areas. 910Sstevel@tonic-gate */ 920Sstevel@tonic-gate int swap_maxcontig; /* set by anon_init() to 1 Mb */ 930Sstevel@tonic-gate 940Sstevel@tonic-gate #define MINIROOTSIZE 12000 /* ~6 Meg XXX */ 950Sstevel@tonic-gate 960Sstevel@tonic-gate /* 970Sstevel@tonic-gate * XXX - this lock is a kludge. It serializes some aspects of swapadd() and 980Sstevel@tonic-gate * swapdel() (namely VOP_OPEN, VOP_CLOSE, VN_RELE). It protects against 990Sstevel@tonic-gate * somebody swapadd'ing and getting swap slots from a vnode, while someone 1000Sstevel@tonic-gate * else is in the process of closing or rele'ing it. 1010Sstevel@tonic-gate */ 1020Sstevel@tonic-gate static kmutex_t swap_lock; 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate kmutex_t swapinfo_lock; 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate /* 1070Sstevel@tonic-gate * protected by the swapinfo_lock 1080Sstevel@tonic-gate */ 1090Sstevel@tonic-gate struct swapinfo *swapinfo; 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate static struct swapinfo *silast; 1120Sstevel@tonic-gate static int nswapfiles; 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate static u_offset_t swap_getoff(struct swapinfo *); 1150Sstevel@tonic-gate static int swapadd(struct vnode *, ulong_t, ulong_t, char *); 1160Sstevel@tonic-gate static int swapdel(struct vnode *, ulong_t); 1170Sstevel@tonic-gate static int swapslot_free(struct vnode *, u_offset_t, struct swapinfo *); 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate /* 1200Sstevel@tonic-gate * swap device bitmap allocation macros 1210Sstevel@tonic-gate */ 1220Sstevel@tonic-gate #define MAPSHIFT 5 1230Sstevel@tonic-gate #define NBBW (NBPW * NBBY) /* number of bits per word */ 1240Sstevel@tonic-gate #define TESTBIT(map, i) (((map)[(i) >> MAPSHIFT] & (1 << (i) % NBBW))) 1250Sstevel@tonic-gate #define SETBIT(map, i) (((map)[(i) >> MAPSHIFT] |= (1 << (i) % NBBW))) 1260Sstevel@tonic-gate #define CLEARBIT(map, i) (((map)[(i) >> MAPSHIFT] &= ~(1 << (i) % NBBW))) 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate int swap_debug = 0; /* set for debug printf's */ 1290Sstevel@tonic-gate int swap_verify = 0; /* set to verify slots when freeing and allocating */ 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate uint_t swapalloc_maxcontig; 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate /* 1340Sstevel@tonic-gate * Allocate a range of up to *lenp contiguous slots (page) from a physical 1350Sstevel@tonic-gate * swap device. Flags are one of: 1360Sstevel@tonic-gate * SA_NOT Must have a slot from a physical swap device other than the 1370Sstevel@tonic-gate * the one containing input (*vpp, *offp). 1380Sstevel@tonic-gate * Less slots than requested may be returned. *lenp allocated slots are 1390Sstevel@tonic-gate * returned starting at *offp on *vpp. 1400Sstevel@tonic-gate * Returns 1 for a successful allocation, 0 for couldn't allocate any slots. 1410Sstevel@tonic-gate */ 1420Sstevel@tonic-gate int 1430Sstevel@tonic-gate swap_phys_alloc( 1440Sstevel@tonic-gate struct vnode **vpp, 1450Sstevel@tonic-gate u_offset_t *offp, 1460Sstevel@tonic-gate size_t *lenp, 1470Sstevel@tonic-gate uint_t flags) 1480Sstevel@tonic-gate { 1490Sstevel@tonic-gate struct swapinfo *sip; 1500Sstevel@tonic-gate offset_t soff, noff; 1510Sstevel@tonic-gate size_t len; 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 1540Sstevel@tonic-gate sip = silast; 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate /* Find a desirable physical device and allocate from it. */ 1570Sstevel@tonic-gate do { 1580Sstevel@tonic-gate if (sip == NULL) 1590Sstevel@tonic-gate break; 1600Sstevel@tonic-gate if (!(sip->si_flags & ST_INDEL) && 1610Sstevel@tonic-gate (spgcnt_t)sip->si_nfpgs > 0) { 1620Sstevel@tonic-gate /* Caller wants other than specified swap device */ 1630Sstevel@tonic-gate if (flags & SA_NOT) { 1640Sstevel@tonic-gate if (*vpp != sip->si_vp || 1650Sstevel@tonic-gate *offp < sip->si_soff || 1660Sstevel@tonic-gate *offp >= sip->si_eoff) 1670Sstevel@tonic-gate goto found; 1680Sstevel@tonic-gate /* Caller is loose, will take anything */ 1690Sstevel@tonic-gate } else 1700Sstevel@tonic-gate goto found; 1710Sstevel@tonic-gate } else if (sip->si_nfpgs == 0) 1720Sstevel@tonic-gate sip->si_allocs = 0; 1730Sstevel@tonic-gate if ((sip = sip->si_next) == NULL) 1740Sstevel@tonic-gate sip = swapinfo; 1750Sstevel@tonic-gate } while (sip != silast); 1760Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 1770Sstevel@tonic-gate return (0); 1780Sstevel@tonic-gate found: 1790Sstevel@tonic-gate soff = swap_getoff(sip); 1800Sstevel@tonic-gate sip->si_nfpgs--; 1810Sstevel@tonic-gate if (soff == -1) 1820Sstevel@tonic-gate panic("swap_alloc: swap_getoff failed!"); 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate for (len = PAGESIZE; len < *lenp; len += PAGESIZE) { 1850Sstevel@tonic-gate if (sip->si_nfpgs == 0) 1860Sstevel@tonic-gate break; 1870Sstevel@tonic-gate if (swapalloc_maxcontig && len >= swapalloc_maxcontig) 1880Sstevel@tonic-gate break; 1890Sstevel@tonic-gate noff = swap_getoff(sip); 1900Sstevel@tonic-gate if (noff == -1) { 1910Sstevel@tonic-gate break; 1920Sstevel@tonic-gate } else if (noff != soff + len) { 1930Sstevel@tonic-gate CLEARBIT(sip->si_swapslots, btop(noff - sip->si_soff)); 1940Sstevel@tonic-gate break; 1950Sstevel@tonic-gate } 1960Sstevel@tonic-gate sip->si_nfpgs--; 1970Sstevel@tonic-gate } 1980Sstevel@tonic-gate *vpp = sip->si_vp; 1990Sstevel@tonic-gate *offp = soff; 2000Sstevel@tonic-gate *lenp = len; 2010Sstevel@tonic-gate ASSERT((spgcnt_t)sip->si_nfpgs >= 0); 2020Sstevel@tonic-gate sip->si_allocs += btop(len); 2030Sstevel@tonic-gate if (sip->si_allocs >= swap_maxcontig) { 2040Sstevel@tonic-gate sip->si_allocs = 0; 2050Sstevel@tonic-gate if ((silast = sip->si_next) == NULL) 2060Sstevel@tonic-gate silast = swapinfo; 2070Sstevel@tonic-gate } 2080Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_SWAP_ALLOC, 2090Sstevel@tonic-gate "swap_alloc:sip %p offset %lx", sip, soff); 2100Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 2110Sstevel@tonic-gate return (1); 2120Sstevel@tonic-gate } 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate int swap_backsearch = 0; 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate /* 2170Sstevel@tonic-gate * Get a free offset on swap device sip. 2180Sstevel@tonic-gate * Return >=0 offset if succeeded, -1 for failure. 2190Sstevel@tonic-gate */ 2200Sstevel@tonic-gate static u_offset_t 2210Sstevel@tonic-gate swap_getoff(struct swapinfo *sip) 2220Sstevel@tonic-gate { 2230Sstevel@tonic-gate uint_t *sp, *ep; 2240Sstevel@tonic-gate size_t aoff, boff, poff, slotnumber; 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate ASSERT(MUTEX_HELD(&swapinfo_lock)); 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate sip->si_alloccnt++; 2290Sstevel@tonic-gate for (sp = &sip->si_swapslots[sip->si_hint >> MAPSHIFT], 2300Sstevel@tonic-gate ep = &sip->si_swapslots[sip->si_mapsize / NBPW]; sp < ep; sp++) { 2310Sstevel@tonic-gate if (*sp != (uint_t)0xffffffff) 2320Sstevel@tonic-gate goto foundentry; 2330Sstevel@tonic-gate else 2340Sstevel@tonic-gate sip->si_checkcnt++; 2350Sstevel@tonic-gate } 2360Sstevel@tonic-gate SWAP_PRINT(SW_ALLOC, 2370Sstevel@tonic-gate "swap_getoff: couldn't find slot from hint %ld to end\n", 2380Sstevel@tonic-gate sip->si_hint, 0, 0, 0, 0); 2390Sstevel@tonic-gate /* 2400Sstevel@tonic-gate * Go backwards? Check for faster method XXX 2410Sstevel@tonic-gate */ 2420Sstevel@tonic-gate if (swap_backsearch) { 2430Sstevel@tonic-gate for (sp = &sip->si_swapslots[sip->si_hint >> MAPSHIFT], 2440Sstevel@tonic-gate ep = sip->si_swapslots; sp > ep; sp--) { 2450Sstevel@tonic-gate if (*sp != (uint_t)0xffffffff) 2460Sstevel@tonic-gate goto foundentry; 2470Sstevel@tonic-gate else 2480Sstevel@tonic-gate sip->si_checkcnt++; 2490Sstevel@tonic-gate } 2500Sstevel@tonic-gate } else { 2510Sstevel@tonic-gate for (sp = sip->si_swapslots, 2520Sstevel@tonic-gate ep = &sip->si_swapslots[sip->si_hint >> MAPSHIFT]; 2530Sstevel@tonic-gate sp < ep; sp++) { 2540Sstevel@tonic-gate if (*sp != (uint_t)0xffffffff) 2550Sstevel@tonic-gate goto foundentry; 2560Sstevel@tonic-gate else 2570Sstevel@tonic-gate sip->si_checkcnt++; 2580Sstevel@tonic-gate } 2590Sstevel@tonic-gate } 2600Sstevel@tonic-gate if (*sp == 0xffffffff) { 2610Sstevel@tonic-gate cmn_err(CE_WARN, "No free swap slots!"); 2620Sstevel@tonic-gate return ((u_offset_t)-1); 2630Sstevel@tonic-gate } 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate foundentry: 2660Sstevel@tonic-gate /* 2670Sstevel@tonic-gate * aoff is the page number offset (in bytes) of the si_swapslots 2680Sstevel@tonic-gate * array element containing a free page 2690Sstevel@tonic-gate * 2700Sstevel@tonic-gate * boff is the page number offset of the free page 2710Sstevel@tonic-gate * (i.e. cleared bit) in si_swapslots[aoff]. 2720Sstevel@tonic-gate */ 2730Sstevel@tonic-gate aoff = ((char *)sp - (char *)sip->si_swapslots) * NBBY; 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate for (boff = (sip->si_hint % NBBW); boff < NBBW; boff++) { 2760Sstevel@tonic-gate if (!TESTBIT(sip->si_swapslots, aoff + boff)) 2770Sstevel@tonic-gate goto foundslot; 2780Sstevel@tonic-gate else 2790Sstevel@tonic-gate sip->si_checkcnt++; 2800Sstevel@tonic-gate } 2810Sstevel@tonic-gate for (boff = 0; boff < (sip->si_hint % NBBW); boff++) { 2820Sstevel@tonic-gate if (!TESTBIT(sip->si_swapslots, aoff + boff)) 2830Sstevel@tonic-gate goto foundslot; 2840Sstevel@tonic-gate else 2850Sstevel@tonic-gate sip->si_checkcnt++; 2860Sstevel@tonic-gate } 2870Sstevel@tonic-gate panic("swap_getoff: didn't find slot in word hint %ld", sip->si_hint); 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate foundslot: 2900Sstevel@tonic-gate /* 2910Sstevel@tonic-gate * Return the offset of the free page in swap device. 2920Sstevel@tonic-gate * Convert page number of byte offset and add starting 2930Sstevel@tonic-gate * offset of swap device. 2940Sstevel@tonic-gate */ 2950Sstevel@tonic-gate slotnumber = aoff + boff; 2960Sstevel@tonic-gate SWAP_PRINT(SW_ALLOC, "swap_getoff: allocating slot %ld\n", 2970Sstevel@tonic-gate slotnumber, 0, 0, 0, 0); 2980Sstevel@tonic-gate poff = ptob(slotnumber); 2990Sstevel@tonic-gate if (poff + sip->si_soff >= sip->si_eoff) 3000Sstevel@tonic-gate printf("ptob(aoff(%ld) + boff(%ld))(%ld) >= eoff(%ld)\n", 3010Sstevel@tonic-gate aoff, boff, ptob(slotnumber), (long)sip->si_eoff); 3020Sstevel@tonic-gate ASSERT(poff < sip->si_eoff); 3030Sstevel@tonic-gate /* 3040Sstevel@tonic-gate * We could verify here that the slot isn't already allocated 3050Sstevel@tonic-gate * by looking through all the anon slots. 3060Sstevel@tonic-gate */ 3070Sstevel@tonic-gate SETBIT(sip->si_swapslots, slotnumber); 3080Sstevel@tonic-gate sip->si_hint = slotnumber + 1; /* hint = next slot */ 3090Sstevel@tonic-gate return (poff + sip->si_soff); 3100Sstevel@tonic-gate } 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate /* 3130Sstevel@tonic-gate * Free a swap page. 3140Sstevel@tonic-gate */ 3150Sstevel@tonic-gate void 3160Sstevel@tonic-gate swap_phys_free(struct vnode *vp, u_offset_t off, size_t len) 3170Sstevel@tonic-gate { 3180Sstevel@tonic-gate struct swapinfo *sip; 3190Sstevel@tonic-gate ssize_t pagenumber, npage; 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 3220Sstevel@tonic-gate sip = swapinfo; 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate do { 3250Sstevel@tonic-gate if (sip->si_vp == vp && 3260Sstevel@tonic-gate sip->si_soff <= off && off < sip->si_eoff) { 3270Sstevel@tonic-gate for (pagenumber = btop(off - sip->si_soff), 3280Sstevel@tonic-gate npage = btop(len) + pagenumber; 3290Sstevel@tonic-gate pagenumber < npage; pagenumber++) { 3300Sstevel@tonic-gate SWAP_PRINT(SW_ALLOC, 3310Sstevel@tonic-gate "swap_phys_free: freeing slot %ld on " 3320Sstevel@tonic-gate "sip %p\n", 3330Sstevel@tonic-gate pagenumber, sip, 0, 0, 0); 3340Sstevel@tonic-gate if (!TESTBIT(sip->si_swapslots, pagenumber)) { 3350Sstevel@tonic-gate panic( 3360Sstevel@tonic-gate "swap_phys_free: freeing free slot " 3370Sstevel@tonic-gate "%p,%lx\n", (void *)vp, 3380Sstevel@tonic-gate ptob(pagenumber) + sip->si_soff); 3390Sstevel@tonic-gate } 3400Sstevel@tonic-gate CLEARBIT(sip->si_swapslots, pagenumber); 3410Sstevel@tonic-gate sip->si_nfpgs++; 3420Sstevel@tonic-gate } 3430Sstevel@tonic-gate ASSERT(sip->si_nfpgs <= sip->si_npgs); 3440Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 3450Sstevel@tonic-gate return; 3460Sstevel@tonic-gate } 3470Sstevel@tonic-gate } while ((sip = sip->si_next) != NULL); 3480Sstevel@tonic-gate panic("swap_phys_free"); 3490Sstevel@tonic-gate /*NOTREACHED*/ 3500Sstevel@tonic-gate } 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate /* 3530Sstevel@tonic-gate * Return the anon struct corresponding for the given 3540Sstevel@tonic-gate * <vnode, off> if it is part of the virtual swap device. 3550Sstevel@tonic-gate * Return the anon struct if found, otherwise NULL. 3560Sstevel@tonic-gate */ 3570Sstevel@tonic-gate struct anon * 3580Sstevel@tonic-gate swap_anon(struct vnode *vp, u_offset_t off) 3590Sstevel@tonic-gate { 3600Sstevel@tonic-gate struct anon *ap; 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate ASSERT(MUTEX_HELD(&anonhash_lock[AH_LOCK(vp, off)])); 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate for (ap = anon_hash[ANON_HASH(vp, off)]; ap != NULL; ap = ap->an_hash) { 3650Sstevel@tonic-gate if (ap->an_vp == vp && ap->an_off == off) 3660Sstevel@tonic-gate return (ap); 3670Sstevel@tonic-gate } 3680Sstevel@tonic-gate return (NULL); 3690Sstevel@tonic-gate } 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate /* 3730Sstevel@tonic-gate * Determine if the vp offset range overlap a swap device. 3740Sstevel@tonic-gate */ 3750Sstevel@tonic-gate int 3760Sstevel@tonic-gate swap_in_range(struct vnode *vp, u_offset_t offset, size_t len) 3770Sstevel@tonic-gate { 3780Sstevel@tonic-gate struct swapinfo *sip; 3790Sstevel@tonic-gate u_offset_t eoff; 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate eoff = offset + len; 3820Sstevel@tonic-gate ASSERT(eoff > offset); 3830Sstevel@tonic-gate 3840Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 3850Sstevel@tonic-gate sip = swapinfo; 3860Sstevel@tonic-gate if (vp && sip) { 3870Sstevel@tonic-gate do { 3880Sstevel@tonic-gate if (vp != sip->si_vp || eoff <= sip->si_soff || 3890Sstevel@tonic-gate offset >= sip->si_eoff) 3900Sstevel@tonic-gate continue; 3910Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 3920Sstevel@tonic-gate return (1); 3930Sstevel@tonic-gate } while ((sip = sip->si_next) != NULL); 3940Sstevel@tonic-gate } 3950Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 3960Sstevel@tonic-gate return (0); 3970Sstevel@tonic-gate } 3980Sstevel@tonic-gate 3990Sstevel@tonic-gate /* 4000Sstevel@tonic-gate * See if name is one of our swap files 4010Sstevel@tonic-gate * even though lookupname failed. 4020Sstevel@tonic-gate * This can be used by swapdel to delete 4030Sstevel@tonic-gate * swap resources on remote machines 4040Sstevel@tonic-gate * where the link has gone down. 4050Sstevel@tonic-gate */ 4060Sstevel@tonic-gate static struct vnode * 4070Sstevel@tonic-gate swapdel_byname( 4080Sstevel@tonic-gate char *name, /* pathname to delete */ 4090Sstevel@tonic-gate ulong_t lowblk) /* Low block number of area to delete */ 4100Sstevel@tonic-gate { 4110Sstevel@tonic-gate struct swapinfo **sipp, *osip; 4120Sstevel@tonic-gate u_offset_t soff; 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate /* 4150Sstevel@tonic-gate * Find the swap file entry for the file to 4160Sstevel@tonic-gate * be deleted. Skip any entries that are in 4170Sstevel@tonic-gate * transition. 4180Sstevel@tonic-gate */ 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate soff = ptob(btopr(lowblk << SCTRSHFT)); /* must be page aligned */ 4210Sstevel@tonic-gate 4220Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 4230Sstevel@tonic-gate for (sipp = &swapinfo; (osip = *sipp) != NULL; sipp = &osip->si_next) { 4240Sstevel@tonic-gate if ((strcmp(osip->si_pname, name) == 0) && 4250Sstevel@tonic-gate (osip->si_soff == soff) && (osip->si_flags == 0)) { 4260Sstevel@tonic-gate struct vnode *vp = osip->si_vp; 4270Sstevel@tonic-gate 4280Sstevel@tonic-gate VN_HOLD(vp); 4290Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 4300Sstevel@tonic-gate return (vp); 4310Sstevel@tonic-gate } 4320Sstevel@tonic-gate } 4330Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 4340Sstevel@tonic-gate return (NULL); 4350Sstevel@tonic-gate } 4360Sstevel@tonic-gate 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate /* 4390Sstevel@tonic-gate * New system call to manipulate swap files. 4400Sstevel@tonic-gate */ 4410Sstevel@tonic-gate int 4420Sstevel@tonic-gate swapctl(int sc_cmd, void *sc_arg, int *rv) 4430Sstevel@tonic-gate { 4440Sstevel@tonic-gate struct swapinfo *sip, *csip, *tsip; 4450Sstevel@tonic-gate int error = 0; 4460Sstevel@tonic-gate struct swapent st, *ust; 4470Sstevel@tonic-gate struct swapres sr; 4480Sstevel@tonic-gate struct vnode *vp; 4490Sstevel@tonic-gate int cnt = 0; 4500Sstevel@tonic-gate int tmp_nswapfiles; 4510Sstevel@tonic-gate int nswap; 4520Sstevel@tonic-gate int length, nlen; 4530Sstevel@tonic-gate int gplen = 0, plen; 4540Sstevel@tonic-gate char *swapname; 4550Sstevel@tonic-gate char *pname; 4560Sstevel@tonic-gate char *tpname; 4570Sstevel@tonic-gate struct anoninfo ai; 4580Sstevel@tonic-gate spgcnt_t avail; 4590Sstevel@tonic-gate int global = INGLOBALZONE(curproc); 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate /* 4620Sstevel@tonic-gate * When running in a zone we want to hide the details of the swap 4630Sstevel@tonic-gate * devices: we report there only being one swap device named "swap" 4640Sstevel@tonic-gate * having a size equal to the sum of the sizes of all real swap devices 4650Sstevel@tonic-gate * on the system. 4660Sstevel@tonic-gate */ 4670Sstevel@tonic-gate switch (sc_cmd) { 4680Sstevel@tonic-gate case SC_GETNSWP: 4690Sstevel@tonic-gate if (global) 4700Sstevel@tonic-gate *rv = nswapfiles; 4710Sstevel@tonic-gate else 4720Sstevel@tonic-gate *rv = 1; 4730Sstevel@tonic-gate return (0); 4740Sstevel@tonic-gate 4750Sstevel@tonic-gate case SC_AINFO: 4760Sstevel@tonic-gate /* 4770Sstevel@tonic-gate * Return anoninfo information with these changes: 4780Sstevel@tonic-gate * ani_max = maximum amount of swap space 4790Sstevel@tonic-gate * (including potentially available physical memory) 4800Sstevel@tonic-gate * ani_free = amount of unallocated anonymous memory 4810Sstevel@tonic-gate * (some of which might be reserved and including 4820Sstevel@tonic-gate * potentially available physical memory) 4830Sstevel@tonic-gate * ani_resv = amount of claimed (reserved) anonymous memory 4840Sstevel@tonic-gate */ 4850Sstevel@tonic-gate avail = MAX((spgcnt_t)(availrmem - swapfs_minfree), 0); 4860Sstevel@tonic-gate ai.ani_max = (k_anoninfo.ani_max + 4870Sstevel@tonic-gate k_anoninfo.ani_mem_resv) +avail; 4880Sstevel@tonic-gate 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 4940Sstevel@tonic-gate if (copyout(&ai, sc_arg, sizeof (struct anoninfo)) != 0) 4950Sstevel@tonic-gate return (EFAULT); 4960Sstevel@tonic-gate return (0); 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate case SC_LIST: 4990Sstevel@tonic-gate if (copyin(sc_arg, &length, sizeof (int)) != 0) 5000Sstevel@tonic-gate return (EFAULT); 5010Sstevel@tonic-gate if (!global) { 5020Sstevel@tonic-gate struct swapent st; 5030Sstevel@tonic-gate char *swappath = "swap"; 5040Sstevel@tonic-gate 5050Sstevel@tonic-gate if (length < 1) 5060Sstevel@tonic-gate return (ENOMEM); 5070Sstevel@tonic-gate ust = (swapent_t *)((swaptbl_t *)sc_arg)->swt_ent; 5080Sstevel@tonic-gate if (copyin(ust, &st, sizeof (swapent_t)) != 0) 5090Sstevel@tonic-gate return (EFAULT); 5100Sstevel@tonic-gate st.ste_start = PAGESIZE >> SCTRSHFT; 5110Sstevel@tonic-gate st.ste_length = (off_t)0; 5120Sstevel@tonic-gate st.ste_pages = 0; 5130Sstevel@tonic-gate st.ste_free = 0; 5140Sstevel@tonic-gate st.ste_flags = 0; 5150Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 5160Sstevel@tonic-gate for (sip = swapinfo, nswap = 0; 5170Sstevel@tonic-gate sip != NULL && nswap < nswapfiles; 5180Sstevel@tonic-gate sip = sip->si_next, nswap++) { 5190Sstevel@tonic-gate st.ste_length += 5200Sstevel@tonic-gate (sip->si_eoff - sip->si_soff) >> SCTRSHFT; 5210Sstevel@tonic-gate st.ste_pages += sip->si_npgs; 5220Sstevel@tonic-gate st.ste_free += sip->si_nfpgs; 5230Sstevel@tonic-gate } 5240Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 5250Sstevel@tonic-gate if (copyout(&st, ust, sizeof (swapent_t)) != 0 || 5260Sstevel@tonic-gate copyout(swappath, st.ste_path, 5270Sstevel@tonic-gate strlen(swappath) + 1) != 0) { 5280Sstevel@tonic-gate return (EFAULT); 5290Sstevel@tonic-gate } 5300Sstevel@tonic-gate *rv = 1; 5310Sstevel@tonic-gate return (0); 5320Sstevel@tonic-gate } 5330Sstevel@tonic-gate beginning: 5340Sstevel@tonic-gate tmp_nswapfiles = nswapfiles; 5350Sstevel@tonic-gate /* Return an error if not enough space for the whole table. */ 5360Sstevel@tonic-gate if (length < tmp_nswapfiles) 5370Sstevel@tonic-gate return (ENOMEM); 5380Sstevel@tonic-gate /* 5390Sstevel@tonic-gate * Get memory to hold the swap entries and their names. We'll 5400Sstevel@tonic-gate * copy the real entries into these and then copy these out. 5410Sstevel@tonic-gate * Allocating the pathname memory is only a guess so we may 5420Sstevel@tonic-gate * find that we need more and have to do it again. 5430Sstevel@tonic-gate * All this is because we have to hold the anon lock while 5440Sstevel@tonic-gate * traversing the swapinfo list, and we can't be doing copyouts 5450Sstevel@tonic-gate * and/or kmem_alloc()s during this. 5460Sstevel@tonic-gate */ 5470Sstevel@tonic-gate csip = kmem_zalloc(tmp_nswapfiles * sizeof (struct swapinfo), 5480Sstevel@tonic-gate KM_SLEEP); 5490Sstevel@tonic-gate retry: 5500Sstevel@tonic-gate nlen = tmp_nswapfiles * (gplen += 100); 5510Sstevel@tonic-gate pname = kmem_zalloc(nlen, KM_SLEEP); 5520Sstevel@tonic-gate 5530Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate if (tmp_nswapfiles != nswapfiles) { 5560Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 5570Sstevel@tonic-gate kmem_free(pname, nlen); 5580Sstevel@tonic-gate kmem_free(csip, 5590Sstevel@tonic-gate tmp_nswapfiles * sizeof (struct swapinfo)); 5600Sstevel@tonic-gate gplen = 0; 5610Sstevel@tonic-gate goto beginning; 5620Sstevel@tonic-gate } 5630Sstevel@tonic-gate for (sip = swapinfo, tsip = csip, tpname = pname, nswap = 0; 5640Sstevel@tonic-gate sip && nswap < tmp_nswapfiles; 5650Sstevel@tonic-gate sip = sip->si_next, tsip++, tpname += plen, nswap++) { 5660Sstevel@tonic-gate plen = sip->si_pnamelen; 5670Sstevel@tonic-gate if (tpname + plen - pname > nlen) { 5680Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 5690Sstevel@tonic-gate kmem_free(pname, nlen); 5700Sstevel@tonic-gate goto retry; 5710Sstevel@tonic-gate } 5720Sstevel@tonic-gate *tsip = *sip; 5730Sstevel@tonic-gate tsip->si_pname = tpname; 5740Sstevel@tonic-gate (void) strcpy(tsip->si_pname, sip->si_pname); 5750Sstevel@tonic-gate } 5760Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate if (sip) { 5790Sstevel@tonic-gate error = ENOMEM; 5800Sstevel@tonic-gate goto lout; 5810Sstevel@tonic-gate } 5820Sstevel@tonic-gate ust = (swapent_t *)((swaptbl_t *)sc_arg)->swt_ent; 5830Sstevel@tonic-gate for (tsip = csip, cnt = 0; cnt < nswap; tsip++, ust++, cnt++) { 5840Sstevel@tonic-gate if (copyin(ust, &st, sizeof (swapent_t)) != 0) { 5850Sstevel@tonic-gate error = EFAULT; 5860Sstevel@tonic-gate goto lout; 5870Sstevel@tonic-gate } 5880Sstevel@tonic-gate st.ste_flags = tsip->si_flags; 5890Sstevel@tonic-gate st.ste_length = 5900Sstevel@tonic-gate (tsip->si_eoff - tsip->si_soff) >> SCTRSHFT; 5910Sstevel@tonic-gate st.ste_start = tsip->si_soff >> SCTRSHFT; 5920Sstevel@tonic-gate st.ste_pages = tsip->si_npgs; 5930Sstevel@tonic-gate st.ste_free = tsip->si_nfpgs; 5940Sstevel@tonic-gate if (copyout(&st, ust, sizeof (swapent_t)) != 0) { 5950Sstevel@tonic-gate error = EFAULT; 5960Sstevel@tonic-gate goto lout; 5970Sstevel@tonic-gate } 5980Sstevel@tonic-gate if (!tsip->si_pnamelen) 5990Sstevel@tonic-gate continue; 6000Sstevel@tonic-gate if (copyout(tsip->si_pname, st.ste_path, 6010Sstevel@tonic-gate tsip->si_pnamelen) != 0) { 6020Sstevel@tonic-gate error = EFAULT; 6030Sstevel@tonic-gate goto lout; 6040Sstevel@tonic-gate } 6050Sstevel@tonic-gate } 6060Sstevel@tonic-gate *rv = nswap; 6070Sstevel@tonic-gate lout: 6080Sstevel@tonic-gate kmem_free(csip, tmp_nswapfiles * sizeof (struct swapinfo)); 6090Sstevel@tonic-gate kmem_free(pname, nlen); 6100Sstevel@tonic-gate return (error); 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate case SC_ADD: 6130Sstevel@tonic-gate case SC_REMOVE: 6140Sstevel@tonic-gate break; 6150Sstevel@tonic-gate default: 6160Sstevel@tonic-gate return (EINVAL); 6170Sstevel@tonic-gate } 6180Sstevel@tonic-gate if ((error = secpolicy_swapctl(CRED())) != 0) 6190Sstevel@tonic-gate return (error); 6200Sstevel@tonic-gate 6210Sstevel@tonic-gate if (copyin(sc_arg, &sr, sizeof (swapres_t))) 6220Sstevel@tonic-gate return (EFAULT); 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate /* Allocate the space to read in pathname */ 6250Sstevel@tonic-gate if ((swapname = kmem_alloc(MAXPATHLEN, KM_NOSLEEP)) == NULL) 6260Sstevel@tonic-gate return (ENOMEM); 6270Sstevel@tonic-gate 6280Sstevel@tonic-gate error = copyinstr(sr.sr_name, swapname, MAXPATHLEN, 0); 6290Sstevel@tonic-gate if (error) 6300Sstevel@tonic-gate goto out; 6310Sstevel@tonic-gate 6320Sstevel@tonic-gate error = lookupname(swapname, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp); 6330Sstevel@tonic-gate if (error) { 6340Sstevel@tonic-gate if (sc_cmd == SC_ADD) 6350Sstevel@tonic-gate goto out; 6360Sstevel@tonic-gate /* see if we match by name */ 6370Sstevel@tonic-gate vp = swapdel_byname(swapname, (size_t)sr.sr_start); 6380Sstevel@tonic-gate if (vp == NULL) 6390Sstevel@tonic-gate goto out; 6400Sstevel@tonic-gate } 6410Sstevel@tonic-gate 6420Sstevel@tonic-gate if (vp->v_flag & (VNOMAP | VNOSWAP)) { 6430Sstevel@tonic-gate VN_RELE(vp); 6440Sstevel@tonic-gate error = ENOSYS; 6450Sstevel@tonic-gate goto out; 6460Sstevel@tonic-gate } 6470Sstevel@tonic-gate switch (vp->v_type) { 6480Sstevel@tonic-gate case VBLK: 6490Sstevel@tonic-gate break; 6500Sstevel@tonic-gate 6510Sstevel@tonic-gate case VREG: 6520Sstevel@tonic-gate if (vp->v_vfsp && vn_is_readonly(vp)) 6530Sstevel@tonic-gate error = EROFS; 6540Sstevel@tonic-gate else 655*5331Samw error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL); 6560Sstevel@tonic-gate break; 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate case VDIR: 6590Sstevel@tonic-gate error = EISDIR; 6600Sstevel@tonic-gate break; 6610Sstevel@tonic-gate default: 6620Sstevel@tonic-gate error = ENOSYS; 6630Sstevel@tonic-gate break; 6640Sstevel@tonic-gate } 6650Sstevel@tonic-gate if (error == 0) { 6660Sstevel@tonic-gate if (sc_cmd == SC_REMOVE) 6670Sstevel@tonic-gate error = swapdel(vp, sr.sr_start); 6680Sstevel@tonic-gate else 6690Sstevel@tonic-gate error = swapadd(vp, sr.sr_start, 6700Sstevel@tonic-gate sr.sr_length, swapname); 6710Sstevel@tonic-gate } 6720Sstevel@tonic-gate VN_RELE(vp); 6730Sstevel@tonic-gate out: 6740Sstevel@tonic-gate kmem_free(swapname, MAXPATHLEN); 6750Sstevel@tonic-gate return (error); 6760Sstevel@tonic-gate } 6770Sstevel@tonic-gate 6780Sstevel@tonic-gate #if defined(_LP64) && defined(_SYSCALL32) 6790Sstevel@tonic-gate 6800Sstevel@tonic-gate int 6810Sstevel@tonic-gate swapctl32(int sc_cmd, void *sc_arg, int *rv) 6820Sstevel@tonic-gate { 6830Sstevel@tonic-gate struct swapinfo *sip, *csip, *tsip; 6840Sstevel@tonic-gate int error = 0; 6850Sstevel@tonic-gate struct swapent32 st, *ust; 6860Sstevel@tonic-gate struct swapres32 sr; 6870Sstevel@tonic-gate struct vnode *vp; 6880Sstevel@tonic-gate int cnt = 0; 6890Sstevel@tonic-gate int tmp_nswapfiles; 6900Sstevel@tonic-gate int nswap; 6910Sstevel@tonic-gate int length, nlen; 6920Sstevel@tonic-gate int gplen = 0, plen; 6930Sstevel@tonic-gate char *swapname; 6940Sstevel@tonic-gate char *pname; 6950Sstevel@tonic-gate char *tpname; 6960Sstevel@tonic-gate struct anoninfo32 ai; 6970Sstevel@tonic-gate size_t s; 6980Sstevel@tonic-gate spgcnt_t avail; 6990Sstevel@tonic-gate 7000Sstevel@tonic-gate switch (sc_cmd) { 7010Sstevel@tonic-gate case SC_GETNSWP: 7020Sstevel@tonic-gate *rv = nswapfiles; 7030Sstevel@tonic-gate return (0); 7040Sstevel@tonic-gate 7050Sstevel@tonic-gate case SC_AINFO: 7060Sstevel@tonic-gate /* 7070Sstevel@tonic-gate * Return anoninfo information with these changes: 7080Sstevel@tonic-gate * ani_max = maximum amount of swap space 7090Sstevel@tonic-gate * (including potentially available physical memory) 7100Sstevel@tonic-gate * ani_free = amount of unallocated anonymous memory 7110Sstevel@tonic-gate * (some of which might be reserved and including 7120Sstevel@tonic-gate * potentially available physical memory) 7130Sstevel@tonic-gate * ani_resv = amount of claimed (reserved) anonymous memory 7140Sstevel@tonic-gate */ 7150Sstevel@tonic-gate avail = MAX((spgcnt_t)(availrmem - swapfs_minfree), 0); 7160Sstevel@tonic-gate s = (k_anoninfo.ani_max + k_anoninfo.ani_mem_resv) + avail; 7170Sstevel@tonic-gate if (s > UINT32_MAX) 7180Sstevel@tonic-gate return (EOVERFLOW); 7190Sstevel@tonic-gate ai.ani_max = s; 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate s = k_anoninfo.ani_free + avail; 7220Sstevel@tonic-gate if (s > UINT32_MAX) 7230Sstevel@tonic-gate return (EOVERFLOW); 7240Sstevel@tonic-gate ai.ani_free = s; 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate s = k_anoninfo.ani_phys_resv + k_anoninfo.ani_mem_resv; 7270Sstevel@tonic-gate if (s > UINT32_MAX) 7280Sstevel@tonic-gate return (EOVERFLOW); 7290Sstevel@tonic-gate ai.ani_resv = s; 7300Sstevel@tonic-gate 7310Sstevel@tonic-gate if (copyout(&ai, sc_arg, sizeof (ai)) != 0) 7320Sstevel@tonic-gate return (EFAULT); 7330Sstevel@tonic-gate return (0); 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate case SC_LIST: 7360Sstevel@tonic-gate if (copyin(sc_arg, &length, sizeof (int32_t)) != 0) 7370Sstevel@tonic-gate return (EFAULT); 7380Sstevel@tonic-gate beginning: 7390Sstevel@tonic-gate tmp_nswapfiles = nswapfiles; 7400Sstevel@tonic-gate /* Return an error if not enough space for the whole table. */ 7410Sstevel@tonic-gate if (length < tmp_nswapfiles) 7420Sstevel@tonic-gate return (ENOMEM); 7430Sstevel@tonic-gate /* 7440Sstevel@tonic-gate * Get memory to hold the swap entries and their names. We'll 7450Sstevel@tonic-gate * copy the real entries into these and then copy these out. 7460Sstevel@tonic-gate * Allocating the pathname memory is only a guess so we may 7470Sstevel@tonic-gate * find that we need more and have to do it again. 7480Sstevel@tonic-gate * All this is because we have to hold the anon lock while 7490Sstevel@tonic-gate * traversing the swapinfo list, and we can't be doing copyouts 7500Sstevel@tonic-gate * and/or kmem_alloc()s during this. 7510Sstevel@tonic-gate */ 7520Sstevel@tonic-gate csip = kmem_zalloc(tmp_nswapfiles * sizeof (*csip), KM_SLEEP); 7530Sstevel@tonic-gate retry: 7540Sstevel@tonic-gate nlen = tmp_nswapfiles * (gplen += 100); 7550Sstevel@tonic-gate pname = kmem_zalloc(nlen, KM_SLEEP); 7560Sstevel@tonic-gate 7570Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate if (tmp_nswapfiles != nswapfiles) { 7600Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 7610Sstevel@tonic-gate kmem_free(pname, nlen); 7620Sstevel@tonic-gate kmem_free(csip, tmp_nswapfiles * sizeof (*csip)); 7630Sstevel@tonic-gate gplen = 0; 7640Sstevel@tonic-gate goto beginning; 7650Sstevel@tonic-gate } 7660Sstevel@tonic-gate for (sip = swapinfo, tsip = csip, tpname = pname, nswap = 0; 7670Sstevel@tonic-gate (sip != NULL) && (nswap < tmp_nswapfiles); 7680Sstevel@tonic-gate sip = sip->si_next, tsip++, tpname += plen, nswap++) { 7690Sstevel@tonic-gate plen = sip->si_pnamelen; 7700Sstevel@tonic-gate if (tpname + plen - pname > nlen) { 7710Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 7720Sstevel@tonic-gate kmem_free(pname, nlen); 7730Sstevel@tonic-gate goto retry; 7740Sstevel@tonic-gate } 7750Sstevel@tonic-gate *tsip = *sip; 7760Sstevel@tonic-gate tsip->si_pname = tpname; 7770Sstevel@tonic-gate (void) strcpy(tsip->si_pname, sip->si_pname); 7780Sstevel@tonic-gate } 7790Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 7800Sstevel@tonic-gate 7810Sstevel@tonic-gate if (sip != NULL) { 7820Sstevel@tonic-gate error = ENOMEM; 7830Sstevel@tonic-gate goto lout; 7840Sstevel@tonic-gate } 7850Sstevel@tonic-gate ust = (swapent32_t *)((swaptbl32_t *)sc_arg)->swt_ent; 7860Sstevel@tonic-gate for (tsip = csip, cnt = 0; cnt < nswap; tsip++, ust++, cnt++) { 7870Sstevel@tonic-gate if (copyin(ust, &st, sizeof (*ust)) != 0) { 7880Sstevel@tonic-gate error = EFAULT; 7890Sstevel@tonic-gate goto lout; 7900Sstevel@tonic-gate } 7910Sstevel@tonic-gate st.ste_flags = tsip->si_flags; 7920Sstevel@tonic-gate st.ste_length = 7930Sstevel@tonic-gate (tsip->si_eoff - tsip->si_soff) >> SCTRSHFT; 7940Sstevel@tonic-gate st.ste_start = tsip->si_soff >> SCTRSHFT; 7950Sstevel@tonic-gate st.ste_pages = tsip->si_npgs; 7960Sstevel@tonic-gate st.ste_free = tsip->si_nfpgs; 7970Sstevel@tonic-gate if (copyout(&st, ust, sizeof (st)) != 0) { 7980Sstevel@tonic-gate error = EFAULT; 7990Sstevel@tonic-gate goto lout; 8000Sstevel@tonic-gate } 8010Sstevel@tonic-gate if (!tsip->si_pnamelen) 8020Sstevel@tonic-gate continue; 8030Sstevel@tonic-gate if (copyout(tsip->si_pname, 8040Sstevel@tonic-gate (caddr_t)(uintptr_t)st.ste_path, 8050Sstevel@tonic-gate tsip->si_pnamelen) != 0) { 8060Sstevel@tonic-gate error = EFAULT; 8070Sstevel@tonic-gate goto lout; 8080Sstevel@tonic-gate } 8090Sstevel@tonic-gate } 8100Sstevel@tonic-gate *rv = nswap; 8110Sstevel@tonic-gate lout: 8120Sstevel@tonic-gate kmem_free(csip, tmp_nswapfiles * sizeof (*csip)); 8130Sstevel@tonic-gate kmem_free(pname, nlen); 8140Sstevel@tonic-gate return (error); 8150Sstevel@tonic-gate 8160Sstevel@tonic-gate case SC_ADD: 8170Sstevel@tonic-gate case SC_REMOVE: 8180Sstevel@tonic-gate break; 8190Sstevel@tonic-gate default: 8200Sstevel@tonic-gate return (EINVAL); 8210Sstevel@tonic-gate } 8220Sstevel@tonic-gate if ((error = secpolicy_swapctl(CRED())) != 0) 8230Sstevel@tonic-gate return (error); 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate if (copyin(sc_arg, &sr, sizeof (sr))) 8260Sstevel@tonic-gate return (EFAULT); 8270Sstevel@tonic-gate 8280Sstevel@tonic-gate /* Allocate the space to read in pathname */ 8290Sstevel@tonic-gate if ((swapname = kmem_alloc(MAXPATHLEN, KM_NOSLEEP)) == NULL) 8300Sstevel@tonic-gate return (ENOMEM); 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate error = copyinstr((caddr_t)(uintptr_t)sr.sr_name, 8330Sstevel@tonic-gate swapname, MAXPATHLEN, NULL); 8340Sstevel@tonic-gate if (error) 8350Sstevel@tonic-gate goto out; 8360Sstevel@tonic-gate 8370Sstevel@tonic-gate error = lookupname(swapname, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp); 8380Sstevel@tonic-gate if (error) { 8390Sstevel@tonic-gate if (sc_cmd == SC_ADD) 8400Sstevel@tonic-gate goto out; 8410Sstevel@tonic-gate /* see if we match by name */ 8420Sstevel@tonic-gate vp = swapdel_byname(swapname, (uint_t)sr.sr_start); 8430Sstevel@tonic-gate if (vp == NULL) 8440Sstevel@tonic-gate goto out; 8450Sstevel@tonic-gate } 8460Sstevel@tonic-gate 8470Sstevel@tonic-gate if (vp->v_flag & (VNOMAP | VNOSWAP)) { 8480Sstevel@tonic-gate VN_RELE(vp); 8490Sstevel@tonic-gate error = ENOSYS; 8500Sstevel@tonic-gate goto out; 8510Sstevel@tonic-gate } 8520Sstevel@tonic-gate switch (vp->v_type) { 8530Sstevel@tonic-gate case VBLK: 8540Sstevel@tonic-gate break; 8550Sstevel@tonic-gate 8560Sstevel@tonic-gate case VREG: 8570Sstevel@tonic-gate if (vp->v_vfsp && vn_is_readonly(vp)) 8580Sstevel@tonic-gate error = EROFS; 8590Sstevel@tonic-gate else 860*5331Samw error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL); 8610Sstevel@tonic-gate break; 8620Sstevel@tonic-gate 8630Sstevel@tonic-gate case VDIR: 8640Sstevel@tonic-gate error = EISDIR; 8650Sstevel@tonic-gate break; 8660Sstevel@tonic-gate default: 8670Sstevel@tonic-gate error = ENOSYS; 8680Sstevel@tonic-gate break; 8690Sstevel@tonic-gate } 8700Sstevel@tonic-gate if (error == 0) { 8710Sstevel@tonic-gate if (sc_cmd == SC_REMOVE) 8720Sstevel@tonic-gate error = swapdel(vp, sr.sr_start); 8730Sstevel@tonic-gate else 8740Sstevel@tonic-gate error = swapadd(vp, sr.sr_start, sr.sr_length, 8750Sstevel@tonic-gate swapname); 8760Sstevel@tonic-gate } 8770Sstevel@tonic-gate VN_RELE(vp); 8780Sstevel@tonic-gate out: 8790Sstevel@tonic-gate kmem_free(swapname, MAXPATHLEN); 8800Sstevel@tonic-gate return (error); 8810Sstevel@tonic-gate } 8820Sstevel@tonic-gate 8830Sstevel@tonic-gate #endif /* _LP64 && _SYSCALL32 */ 8840Sstevel@tonic-gate 8850Sstevel@tonic-gate /* 8860Sstevel@tonic-gate * Add a new swap file. 8870Sstevel@tonic-gate */ 8880Sstevel@tonic-gate int 8890Sstevel@tonic-gate swapadd(struct vnode *vp, ulong_t lowblk, ulong_t nblks, char *swapname) 8900Sstevel@tonic-gate { 8910Sstevel@tonic-gate struct swapinfo **sipp, *nsip = NULL, *esip = NULL; 8920Sstevel@tonic-gate struct vnode *cvp; 8930Sstevel@tonic-gate struct vattr vattr; 8940Sstevel@tonic-gate pgcnt_t pages; 8950Sstevel@tonic-gate u_offset_t soff, eoff; 8960Sstevel@tonic-gate int error; 8970Sstevel@tonic-gate ssize_t i, start, end; 8980Sstevel@tonic-gate ushort_t wasswap; 8990Sstevel@tonic-gate ulong_t startblk; 9000Sstevel@tonic-gate size_t returned_mem; 9010Sstevel@tonic-gate 9020Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: vp %p lowblk %ld nblks %ld swapname %s\n", 9030Sstevel@tonic-gate vp, lowblk, nblks, swapname, 0); 9040Sstevel@tonic-gate /* 9050Sstevel@tonic-gate * Get the real vnode. (If vp is not a specnode it just returns vp, so 9060Sstevel@tonic-gate * it does the right thing, but having this code know about specnodes 9070Sstevel@tonic-gate * violates the spirit of having it be indepedent of vnode type.) 9080Sstevel@tonic-gate */ 9090Sstevel@tonic-gate cvp = common_specvp(vp); 9100Sstevel@tonic-gate 9110Sstevel@tonic-gate /* 9120Sstevel@tonic-gate * Or in VISSWAP so file system has chance to deny swap-ons during open. 9130Sstevel@tonic-gate */ 9140Sstevel@tonic-gate mutex_enter(&cvp->v_lock); 9150Sstevel@tonic-gate wasswap = cvp->v_flag & VISSWAP; 9160Sstevel@tonic-gate cvp->v_flag |= VISSWAP; 9170Sstevel@tonic-gate mutex_exit(&cvp->v_lock); 9180Sstevel@tonic-gate 9190Sstevel@tonic-gate mutex_enter(&swap_lock); 920*5331Samw if (error = VOP_OPEN(&cvp, FREAD|FWRITE, CRED(), NULL)) { 9210Sstevel@tonic-gate mutex_exit(&swap_lock); 9220Sstevel@tonic-gate /* restore state of v_flag */ 9230Sstevel@tonic-gate if (!wasswap) { 9240Sstevel@tonic-gate mutex_enter(&cvp->v_lock); 9250Sstevel@tonic-gate cvp->v_flag &= ~VISSWAP; 9260Sstevel@tonic-gate mutex_exit(&cvp->v_lock); 9270Sstevel@tonic-gate } 9280Sstevel@tonic-gate return (error); 9290Sstevel@tonic-gate } 9300Sstevel@tonic-gate mutex_exit(&swap_lock); 9310Sstevel@tonic-gate 9320Sstevel@tonic-gate /* 9330Sstevel@tonic-gate * Get partition size. Return error if empty partition, 9340Sstevel@tonic-gate * or if request does not fit within the partition. 9350Sstevel@tonic-gate * If this is the first swap device, we can reduce 9360Sstevel@tonic-gate * the size of the swap area to match what is 9370Sstevel@tonic-gate * available. This can happen if the system was built 9380Sstevel@tonic-gate * on a machine with a different size swap partition. 9390Sstevel@tonic-gate */ 9400Sstevel@tonic-gate vattr.va_mask = AT_SIZE; 941*5331Samw if (error = VOP_GETATTR(cvp, &vattr, ATTR_COMM, CRED(), NULL)) 9420Sstevel@tonic-gate goto out; 9430Sstevel@tonic-gate 9440Sstevel@tonic-gate /* 9450Sstevel@tonic-gate * Specfs returns a va_size of MAXOFFSET_T (UNKNOWN_SIZE) when the 9460Sstevel@tonic-gate * size of the device can't be determined. 9470Sstevel@tonic-gate */ 9480Sstevel@tonic-gate if ((vattr.va_size == 0) || (vattr.va_size == MAXOFFSET_T)) { 9490Sstevel@tonic-gate error = EINVAL; 9500Sstevel@tonic-gate goto out; 9510Sstevel@tonic-gate } 9520Sstevel@tonic-gate 9530Sstevel@tonic-gate #ifdef _ILP32 9540Sstevel@tonic-gate /* 9550Sstevel@tonic-gate * No support for large swap in 32-bit OS, if the size of the swap is 9560Sstevel@tonic-gate * bigger than MAXOFF32_T then the size used by swapfs must be limited. 9570Sstevel@tonic-gate * This limitation is imposed by the swap subsystem itself, a D_64BIT 9580Sstevel@tonic-gate * driver as the target of swap operation should be able to field 9590Sstevel@tonic-gate * the IO. 9600Sstevel@tonic-gate */ 9610Sstevel@tonic-gate if (vattr.va_size > MAXOFF32_T) { 9620Sstevel@tonic-gate cmn_err(CE_NOTE, 9630Sstevel@tonic-gate "!swap device %s truncated from 0x%llx to 0x%x bytes", 9640Sstevel@tonic-gate swapname, vattr.va_size, MAXOFF32_T); 9650Sstevel@tonic-gate vattr.va_size = MAXOFF32_T; 9660Sstevel@tonic-gate } 9670Sstevel@tonic-gate #endif /* _ILP32 */ 9680Sstevel@tonic-gate 9690Sstevel@tonic-gate /* Fail if file not writeable (try to set size to current size) */ 9700Sstevel@tonic-gate vattr.va_mask = AT_SIZE; 9710Sstevel@tonic-gate if (error = VOP_SETATTR(cvp, &vattr, 0, CRED(), NULL)) 9720Sstevel@tonic-gate goto out; 9730Sstevel@tonic-gate 9740Sstevel@tonic-gate /* Fail if fs does not support VOP_PAGEIO */ 975*5331Samw error = VOP_PAGEIO(cvp, (page_t *)NULL, (u_offset_t)0, 0, 0, CRED(), 976*5331Samw NULL); 9770Sstevel@tonic-gate 9780Sstevel@tonic-gate if (error == ENOSYS) 9790Sstevel@tonic-gate goto out; 9800Sstevel@tonic-gate else 9810Sstevel@tonic-gate error = 0; 9820Sstevel@tonic-gate /* 9830Sstevel@tonic-gate * If swapping on the root filesystem don't put swap blocks that 9840Sstevel@tonic-gate * correspond to the miniroot filesystem on the swap free list. 9850Sstevel@tonic-gate */ 9860Sstevel@tonic-gate if (cvp == rootdir) 9870Sstevel@tonic-gate startblk = roundup(MINIROOTSIZE<<SCTRSHFT, klustsize)>>SCTRSHFT; 9880Sstevel@tonic-gate else /* Skip 1st page (disk label) */ 9890Sstevel@tonic-gate startblk = (ulong_t)(lowblk ? lowblk : 1); 9900Sstevel@tonic-gate 9910Sstevel@tonic-gate soff = startblk << SCTRSHFT; 9920Sstevel@tonic-gate if (soff >= vattr.va_size) { 9930Sstevel@tonic-gate error = EINVAL; 9940Sstevel@tonic-gate goto out; 9950Sstevel@tonic-gate } 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate /* 9980Sstevel@tonic-gate * If user specified 0 blks, use the size of the device 9990Sstevel@tonic-gate */ 10000Sstevel@tonic-gate eoff = nblks ? soff + (nblks - (startblk - lowblk) << SCTRSHFT) : 10010Sstevel@tonic-gate vattr.va_size; 10020Sstevel@tonic-gate 10030Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: va_size %ld soff %ld eoff %ld\n", 10040Sstevel@tonic-gate vattr.va_size, soff, eoff, 0, 0); 10050Sstevel@tonic-gate 10060Sstevel@tonic-gate if (eoff > vattr.va_size) { 10070Sstevel@tonic-gate error = EINVAL; 10080Sstevel@tonic-gate goto out; 10090Sstevel@tonic-gate } 10100Sstevel@tonic-gate 10110Sstevel@tonic-gate /* 10120Sstevel@tonic-gate * The starting and ending offsets must be page aligned. 10130Sstevel@tonic-gate * Round soff up to next page boundary, round eoff 10140Sstevel@tonic-gate * down to previous page boundary. 10150Sstevel@tonic-gate */ 10160Sstevel@tonic-gate soff = ptob(btopr(soff)); 10170Sstevel@tonic-gate eoff = ptob(btop(eoff)); 10180Sstevel@tonic-gate if (soff >= eoff) { 10190Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: soff %ld >= eoff %ld\n", 10200Sstevel@tonic-gate soff, eoff, 0, 0, 0); 10210Sstevel@tonic-gate error = EINVAL; 10220Sstevel@tonic-gate goto out; 10230Sstevel@tonic-gate } 10240Sstevel@tonic-gate 10250Sstevel@tonic-gate pages = btop(eoff - soff); 10260Sstevel@tonic-gate 10270Sstevel@tonic-gate /* Allocate and partially set up the new swapinfo */ 10280Sstevel@tonic-gate nsip = kmem_zalloc(sizeof (struct swapinfo), KM_SLEEP); 10290Sstevel@tonic-gate nsip->si_vp = cvp; 10300Sstevel@tonic-gate 10310Sstevel@tonic-gate nsip->si_soff = soff; 10320Sstevel@tonic-gate nsip->si_eoff = eoff; 10330Sstevel@tonic-gate nsip->si_hint = 0; 10340Sstevel@tonic-gate nsip->si_checkcnt = nsip->si_alloccnt = 0; 10350Sstevel@tonic-gate 10360Sstevel@tonic-gate nsip->si_pnamelen = (int)strlen(swapname) + 1; 10370Sstevel@tonic-gate nsip->si_pname = (char *)kmem_zalloc(nsip->si_pnamelen, KM_SLEEP); 10380Sstevel@tonic-gate bcopy(swapname, nsip->si_pname, nsip->si_pnamelen - 1); 10390Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: allocating swapinfo for %s, %ld pages\n", 10400Sstevel@tonic-gate swapname, pages, 0, 0, 0); 10410Sstevel@tonic-gate /* 10420Sstevel@tonic-gate * Size of swapslots map in bytes 10430Sstevel@tonic-gate */ 10440Sstevel@tonic-gate nsip->si_mapsize = P2ROUNDUP(pages, NBBW) / NBBY; 10450Sstevel@tonic-gate nsip->si_swapslots = kmem_zalloc(nsip->si_mapsize, KM_SLEEP); 10460Sstevel@tonic-gate 10470Sstevel@tonic-gate /* 10480Sstevel@tonic-gate * Permanently set the bits that can't ever be allocated, 10490Sstevel@tonic-gate * i.e. those from the ending offset to the round up slot for the 10500Sstevel@tonic-gate * swapslots bit map. 10510Sstevel@tonic-gate */ 10520Sstevel@tonic-gate start = pages; 10530Sstevel@tonic-gate end = P2ROUNDUP(pages, NBBW); 10540Sstevel@tonic-gate for (i = start; i < end; i++) { 10550Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: set bit for page %ld\n", i, 10560Sstevel@tonic-gate 0, 0, 0, 0); 10570Sstevel@tonic-gate SETBIT(nsip->si_swapslots, i); 10580Sstevel@tonic-gate } 10590Sstevel@tonic-gate nsip->si_npgs = nsip->si_nfpgs = pages; 10600Sstevel@tonic-gate /* 10610Sstevel@tonic-gate * Now check to see if we can add it. We wait til now to check because 10620Sstevel@tonic-gate * we need the swapinfo_lock and we don't want sleep with it (e.g., 10630Sstevel@tonic-gate * during kmem_alloc()) while we're setting up the swapinfo. 10640Sstevel@tonic-gate */ 10650Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 10660Sstevel@tonic-gate for (sipp = &swapinfo; (esip = *sipp) != NULL; sipp = &esip->si_next) { 10670Sstevel@tonic-gate if (esip->si_vp == cvp) { 10680Sstevel@tonic-gate if (esip->si_soff == soff && esip->si_npgs == pages && 10690Sstevel@tonic-gate (esip->si_flags & ST_DOINGDEL)) { 10700Sstevel@tonic-gate /* 10710Sstevel@tonic-gate * We are adding a device that we are in the 10720Sstevel@tonic-gate * middle of deleting. Just clear the 10730Sstevel@tonic-gate * ST_DOINGDEL flag to signal this and 10740Sstevel@tonic-gate * the deletion routine will eventually notice 10750Sstevel@tonic-gate * it and add it back. 10760Sstevel@tonic-gate */ 10770Sstevel@tonic-gate esip->si_flags &= ~ST_DOINGDEL; 10780Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 10790Sstevel@tonic-gate goto out; 10800Sstevel@tonic-gate } 10810Sstevel@tonic-gate /* disallow overlapping swap files */ 10820Sstevel@tonic-gate if ((soff < esip->si_eoff) && (eoff > esip->si_soff)) { 10830Sstevel@tonic-gate error = EEXIST; 10840Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 10850Sstevel@tonic-gate goto out; 10860Sstevel@tonic-gate } 10870Sstevel@tonic-gate } 10880Sstevel@tonic-gate } 10890Sstevel@tonic-gate 10900Sstevel@tonic-gate nswapfiles++; 10910Sstevel@tonic-gate 10920Sstevel@tonic-gate /* 10930Sstevel@tonic-gate * add new swap device to list and shift allocations to it 10940Sstevel@tonic-gate * before updating the anoninfo counters 10950Sstevel@tonic-gate */ 10960Sstevel@tonic-gate *sipp = nsip; 10970Sstevel@tonic-gate silast = nsip; 10980Sstevel@tonic-gate 10990Sstevel@tonic-gate /* 11000Sstevel@tonic-gate * Update the total amount of reservable swap space 11010Sstevel@tonic-gate * accounting properly for swap space from physical memory 11020Sstevel@tonic-gate */ 11030Sstevel@tonic-gate /* New swap device soaks up currently reserved memory swap */ 11040Sstevel@tonic-gate mutex_enter(&anoninfo_lock); 11050Sstevel@tonic-gate 11060Sstevel@tonic-gate ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap); 11070Sstevel@tonic-gate ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv); 11080Sstevel@tonic-gate 11090Sstevel@tonic-gate k_anoninfo.ani_max += pages; 11100Sstevel@tonic-gate ANI_ADD(pages); 11110Sstevel@tonic-gate if (k_anoninfo.ani_mem_resv > k_anoninfo.ani_locked_swap) { 11120Sstevel@tonic-gate returned_mem = MIN(k_anoninfo.ani_mem_resv - 11130Sstevel@tonic-gate k_anoninfo.ani_locked_swap, 11140Sstevel@tonic-gate k_anoninfo.ani_max - k_anoninfo.ani_phys_resv); 11150Sstevel@tonic-gate 11160Sstevel@tonic-gate ANI_ADD(-returned_mem); 11170Sstevel@tonic-gate k_anoninfo.ani_free -= returned_mem; 11180Sstevel@tonic-gate k_anoninfo.ani_mem_resv -= returned_mem; 11190Sstevel@tonic-gate k_anoninfo.ani_phys_resv += returned_mem; 11200Sstevel@tonic-gate 11210Sstevel@tonic-gate mutex_enter(&freemem_lock); 11220Sstevel@tonic-gate availrmem += returned_mem; 11230Sstevel@tonic-gate mutex_exit(&freemem_lock); 11240Sstevel@tonic-gate } 11250Sstevel@tonic-gate /* 11260Sstevel@tonic-gate * At boot time, to permit booting small memory machines using 11270Sstevel@tonic-gate * only physical memory as swap space, we allowed a dangerously 11280Sstevel@tonic-gate * large amount of memory to be used as swap space; now that 11290Sstevel@tonic-gate * more physical backing store is available bump down the amount 11300Sstevel@tonic-gate * we can get from memory to a safer size. 11310Sstevel@tonic-gate */ 11320Sstevel@tonic-gate if (swapfs_minfree < swapfs_desfree) { 11330Sstevel@tonic-gate mutex_enter(&freemem_lock); 11340Sstevel@tonic-gate if (availrmem > swapfs_desfree || !k_anoninfo.ani_mem_resv) 11350Sstevel@tonic-gate swapfs_minfree = swapfs_desfree; 11360Sstevel@tonic-gate mutex_exit(&freemem_lock); 11370Sstevel@tonic-gate } 11380Sstevel@tonic-gate 11390Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: ani_max %ld ani_free %ld\n", 11400Sstevel@tonic-gate k_anoninfo.ani_free, k_anoninfo.ani_free, 0, 0, 0); 11410Sstevel@tonic-gate 11420Sstevel@tonic-gate mutex_exit(&anoninfo_lock); 11430Sstevel@tonic-gate 11440Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 11450Sstevel@tonic-gate 11460Sstevel@tonic-gate /* Initialize the dump device */ 11470Sstevel@tonic-gate mutex_enter(&dump_lock); 11480Sstevel@tonic-gate if (dumpvp == NULL) 11490Sstevel@tonic-gate (void) dumpinit(vp, swapname, 0); 11500Sstevel@tonic-gate mutex_exit(&dump_lock); 11510Sstevel@tonic-gate 11520Sstevel@tonic-gate VN_HOLD(cvp); 11530Sstevel@tonic-gate out: 11540Sstevel@tonic-gate if (error || esip) { 11550Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: error (%d)\n", error, 0, 0, 0, 0); 11560Sstevel@tonic-gate 11570Sstevel@tonic-gate if (!wasswap) { 11580Sstevel@tonic-gate mutex_enter(&cvp->v_lock); 11590Sstevel@tonic-gate cvp->v_flag &= ~VISSWAP; 11600Sstevel@tonic-gate mutex_exit(&cvp->v_lock); 11610Sstevel@tonic-gate } 11620Sstevel@tonic-gate if (nsip) { 11630Sstevel@tonic-gate kmem_free(nsip->si_swapslots, (size_t)nsip->si_mapsize); 11640Sstevel@tonic-gate kmem_free(nsip->si_pname, nsip->si_pnamelen); 11650Sstevel@tonic-gate kmem_free(nsip, sizeof (*nsip)); 11660Sstevel@tonic-gate } 11670Sstevel@tonic-gate mutex_enter(&swap_lock); 1168*5331Samw (void) VOP_CLOSE(cvp, FREAD|FWRITE, 1, (offset_t)0, CRED(), 1169*5331Samw NULL); 11700Sstevel@tonic-gate mutex_exit(&swap_lock); 11710Sstevel@tonic-gate } 11720Sstevel@tonic-gate return (error); 11730Sstevel@tonic-gate } 11740Sstevel@tonic-gate 11750Sstevel@tonic-gate /* 11760Sstevel@tonic-gate * Delete a swap file. 11770Sstevel@tonic-gate */ 11780Sstevel@tonic-gate static int 11790Sstevel@tonic-gate swapdel( 11800Sstevel@tonic-gate struct vnode *vp, 11810Sstevel@tonic-gate ulong_t lowblk) /* Low block number of area to delete. */ 11820Sstevel@tonic-gate { 11830Sstevel@tonic-gate struct swapinfo **sipp, *osip = NULL; 11840Sstevel@tonic-gate struct vnode *cvp; 11850Sstevel@tonic-gate u_offset_t soff; 11860Sstevel@tonic-gate int error = 0; 11870Sstevel@tonic-gate u_offset_t toff = 0; 11880Sstevel@tonic-gate struct vnode *tvp = NULL; 11890Sstevel@tonic-gate spgcnt_t pages; 11900Sstevel@tonic-gate struct anon **app, *ap; 11910Sstevel@tonic-gate kmutex_t *ahm; 11920Sstevel@tonic-gate pgcnt_t adjust_swap = 0; 11930Sstevel@tonic-gate 11940Sstevel@tonic-gate /* Find the swap file entry for the file to be deleted */ 11950Sstevel@tonic-gate cvp = common_specvp(vp); 11960Sstevel@tonic-gate 11970Sstevel@tonic-gate 11980Sstevel@tonic-gate lowblk = lowblk ? lowblk : 1; /* Skip first page (disk label) */ 11990Sstevel@tonic-gate soff = ptob(btopr(lowblk << SCTRSHFT)); /* must be page aligned */ 12000Sstevel@tonic-gate 12010Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 12020Sstevel@tonic-gate for (sipp = &swapinfo; (osip = *sipp) != NULL; sipp = &osip->si_next) { 12030Sstevel@tonic-gate if ((osip->si_vp == cvp) && 12040Sstevel@tonic-gate (osip->si_soff == soff) && (osip->si_flags == 0)) 12050Sstevel@tonic-gate break; 12060Sstevel@tonic-gate } 12070Sstevel@tonic-gate 12080Sstevel@tonic-gate /* If the file was not found, error. */ 12090Sstevel@tonic-gate if (osip == NULL) { 12100Sstevel@tonic-gate error = EINVAL; 12110Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 12120Sstevel@tonic-gate goto out; 12130Sstevel@tonic-gate } 12140Sstevel@tonic-gate 12150Sstevel@tonic-gate pages = osip->si_npgs; 12160Sstevel@tonic-gate 12170Sstevel@tonic-gate /* 12180Sstevel@tonic-gate * Do not delete if we will be low on swap pages. 12190Sstevel@tonic-gate */ 12200Sstevel@tonic-gate mutex_enter(&anoninfo_lock); 12210Sstevel@tonic-gate 12220Sstevel@tonic-gate ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap); 12230Sstevel@tonic-gate ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv); 12240Sstevel@tonic-gate 12250Sstevel@tonic-gate mutex_enter(&freemem_lock); 12260Sstevel@tonic-gate if (((k_anoninfo.ani_max - k_anoninfo.ani_phys_resv) + 12270Sstevel@tonic-gate MAX((spgcnt_t)(availrmem - swapfs_minfree), 0)) < pages) { 12280Sstevel@tonic-gate mutex_exit(&freemem_lock); 12290Sstevel@tonic-gate mutex_exit(&anoninfo_lock); 12300Sstevel@tonic-gate error = ENOMEM; 12310Sstevel@tonic-gate cmn_err(CE_WARN, "swapdel - too few free pages"); 12320Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 12330Sstevel@tonic-gate goto out; 12340Sstevel@tonic-gate } 12350Sstevel@tonic-gate mutex_exit(&freemem_lock); 12360Sstevel@tonic-gate 12370Sstevel@tonic-gate k_anoninfo.ani_max -= pages; 12380Sstevel@tonic-gate 12390Sstevel@tonic-gate /* If needed, reserve memory swap to replace old device */ 12400Sstevel@tonic-gate if (k_anoninfo.ani_phys_resv > k_anoninfo.ani_max) { 12410Sstevel@tonic-gate adjust_swap = k_anoninfo.ani_phys_resv - k_anoninfo.ani_max; 12420Sstevel@tonic-gate k_anoninfo.ani_phys_resv -= adjust_swap; 12430Sstevel@tonic-gate k_anoninfo.ani_mem_resv += adjust_swap; 12440Sstevel@tonic-gate mutex_enter(&freemem_lock); 12450Sstevel@tonic-gate availrmem -= adjust_swap; 12460Sstevel@tonic-gate mutex_exit(&freemem_lock); 12470Sstevel@tonic-gate ANI_ADD(adjust_swap); 12480Sstevel@tonic-gate } 12490Sstevel@tonic-gate ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap); 12500Sstevel@tonic-gate ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv); 12510Sstevel@tonic-gate mutex_exit(&anoninfo_lock); 12520Sstevel@tonic-gate 12530Sstevel@tonic-gate ANI_ADD(-pages); 12540Sstevel@tonic-gate 12550Sstevel@tonic-gate /* 12560Sstevel@tonic-gate * Set the delete flag. This prevents anyone from allocating more 12570Sstevel@tonic-gate * pages from this file. Also set ST_DOINGDEL. Someone who wants to 12580Sstevel@tonic-gate * add the file back while we're deleting it will signify by clearing 12590Sstevel@tonic-gate * this flag. 12600Sstevel@tonic-gate */ 12610Sstevel@tonic-gate osip->si_flags |= ST_INDEL|ST_DOINGDEL; 12620Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 12630Sstevel@tonic-gate 12640Sstevel@tonic-gate /* 12650Sstevel@tonic-gate * Free all the allocated physical slots for this file. We do this 12660Sstevel@tonic-gate * by walking through the entire anon hash array, because we need 12670Sstevel@tonic-gate * to update all the anon slots that have physical swap slots on 12680Sstevel@tonic-gate * this file, and this is the only way to find them all. We go back 12690Sstevel@tonic-gate * to the beginning of a bucket after each slot is freed because the 12700Sstevel@tonic-gate * anonhash_lock is not held during the free and thus the hash table 12710Sstevel@tonic-gate * may change under us. 12720Sstevel@tonic-gate */ 12730Sstevel@tonic-gate for (app = anon_hash; app < &anon_hash[ANON_HASH_SIZE]; app++) { 12740Sstevel@tonic-gate ahm = &anonhash_lock[(app-anon_hash) & (AH_LOCK_SIZE - 1)]; 12750Sstevel@tonic-gate mutex_enter(ahm); 12760Sstevel@tonic-gate top: 12770Sstevel@tonic-gate for (ap = *app; ap != NULL; ap = ap->an_hash) { 12780Sstevel@tonic-gate if (ap->an_pvp == cvp && 12790Sstevel@tonic-gate ap->an_poff >= osip->si_soff && 12800Sstevel@tonic-gate ap->an_poff < osip->si_eoff) { 12810Sstevel@tonic-gate ASSERT(TESTBIT(osip->si_swapslots, 12820Sstevel@tonic-gate btop((size_t)(ap->an_poff - 12830Sstevel@tonic-gate osip->si_soff)))); 12840Sstevel@tonic-gate tvp = ap->an_vp; 12850Sstevel@tonic-gate toff = ap->an_off; 12860Sstevel@tonic-gate VN_HOLD(tvp); 12870Sstevel@tonic-gate mutex_exit(ahm); 12880Sstevel@tonic-gate 12890Sstevel@tonic-gate error = swapslot_free(tvp, toff, osip); 12900Sstevel@tonic-gate 12910Sstevel@tonic-gate VN_RELE(tvp); 12920Sstevel@tonic-gate mutex_enter(ahm); 12930Sstevel@tonic-gate if (!error && (osip->si_flags & ST_DOINGDEL)) { 12940Sstevel@tonic-gate goto top; 12950Sstevel@tonic-gate } else { 12960Sstevel@tonic-gate if (error) { 12970Sstevel@tonic-gate cmn_err(CE_WARN, 12980Sstevel@tonic-gate "swapslot_free failed %d", 12990Sstevel@tonic-gate error); 13000Sstevel@tonic-gate } 13010Sstevel@tonic-gate 13020Sstevel@tonic-gate /* 13030Sstevel@tonic-gate * Add device back before making it 13040Sstevel@tonic-gate * visible. 13050Sstevel@tonic-gate */ 13060Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 13070Sstevel@tonic-gate osip->si_flags &= 13080Sstevel@tonic-gate ~(ST_INDEL | ST_DOINGDEL); 13090Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 13100Sstevel@tonic-gate 13110Sstevel@tonic-gate /* 13120Sstevel@tonic-gate * Update the anon space available 13130Sstevel@tonic-gate */ 13140Sstevel@tonic-gate mutex_enter(&anoninfo_lock); 13150Sstevel@tonic-gate 13160Sstevel@tonic-gate k_anoninfo.ani_phys_resv += adjust_swap; 13170Sstevel@tonic-gate k_anoninfo.ani_mem_resv -= adjust_swap; 13180Sstevel@tonic-gate k_anoninfo.ani_max += pages; 13190Sstevel@tonic-gate 13200Sstevel@tonic-gate mutex_enter(&freemem_lock); 13210Sstevel@tonic-gate availrmem += adjust_swap; 13220Sstevel@tonic-gate mutex_exit(&freemem_lock); 13230Sstevel@tonic-gate 13240Sstevel@tonic-gate mutex_exit(&anoninfo_lock); 13250Sstevel@tonic-gate 13260Sstevel@tonic-gate ANI_ADD(pages); 13270Sstevel@tonic-gate 13280Sstevel@tonic-gate mutex_exit(ahm); 13290Sstevel@tonic-gate goto out; 13300Sstevel@tonic-gate } 13310Sstevel@tonic-gate } 13320Sstevel@tonic-gate } 13330Sstevel@tonic-gate mutex_exit(ahm); 13340Sstevel@tonic-gate } 13350Sstevel@tonic-gate 13360Sstevel@tonic-gate /* All done, they'd better all be free! */ 13370Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 13380Sstevel@tonic-gate ASSERT(osip->si_nfpgs == osip->si_npgs); 13390Sstevel@tonic-gate 13400Sstevel@tonic-gate /* Now remove it from the swapinfo list */ 13410Sstevel@tonic-gate for (sipp = &swapinfo; *sipp != NULL; sipp = &(*sipp)->si_next) { 13420Sstevel@tonic-gate if (*sipp == osip) 13430Sstevel@tonic-gate break; 13440Sstevel@tonic-gate } 13450Sstevel@tonic-gate ASSERT(*sipp); 13460Sstevel@tonic-gate *sipp = osip->si_next; 13470Sstevel@tonic-gate if (silast == osip) 13480Sstevel@tonic-gate if ((silast = osip->si_next) == NULL) 13490Sstevel@tonic-gate silast = swapinfo; 13500Sstevel@tonic-gate nswapfiles--; 13510Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 13520Sstevel@tonic-gate 13530Sstevel@tonic-gate kmem_free(osip->si_swapslots, osip->si_mapsize); 13540Sstevel@tonic-gate kmem_free(osip->si_pname, osip->si_pnamelen); 13550Sstevel@tonic-gate kmem_free(osip, sizeof (*osip)); 13560Sstevel@tonic-gate 13570Sstevel@tonic-gate mutex_enter(&dump_lock); 13580Sstevel@tonic-gate if (cvp == dumpvp) 13590Sstevel@tonic-gate dumpfini(); 13600Sstevel@tonic-gate mutex_exit(&dump_lock); 13610Sstevel@tonic-gate 13620Sstevel@tonic-gate /* Release the vnode */ 13630Sstevel@tonic-gate 13640Sstevel@tonic-gate mutex_enter(&swap_lock); 1365*5331Samw (void) VOP_CLOSE(cvp, FREAD|FWRITE, 1, (offset_t)0, CRED(), NULL); 13660Sstevel@tonic-gate mutex_enter(&cvp->v_lock); 13670Sstevel@tonic-gate cvp->v_flag &= ~VISSWAP; 13680Sstevel@tonic-gate mutex_exit(&cvp->v_lock); 13690Sstevel@tonic-gate VN_RELE(cvp); 13700Sstevel@tonic-gate mutex_exit(&swap_lock); 13710Sstevel@tonic-gate out: 13720Sstevel@tonic-gate return (error); 13730Sstevel@tonic-gate } 13740Sstevel@tonic-gate 13750Sstevel@tonic-gate /* 13760Sstevel@tonic-gate * Free up a physical swap slot on swapinfo sip, currently in use by the 13770Sstevel@tonic-gate * anonymous page whose name is (vp, off). 13780Sstevel@tonic-gate */ 13790Sstevel@tonic-gate static int 13800Sstevel@tonic-gate swapslot_free( 13810Sstevel@tonic-gate struct vnode *vp, 13820Sstevel@tonic-gate u_offset_t off, 13830Sstevel@tonic-gate struct swapinfo *sip) 13840Sstevel@tonic-gate { 13850Sstevel@tonic-gate struct page *pl[2], *pp; 13860Sstevel@tonic-gate struct anon *ap = NULL; 13870Sstevel@tonic-gate int error = 0; 13880Sstevel@tonic-gate kmutex_t *ahm; 13890Sstevel@tonic-gate 13900Sstevel@tonic-gate /* 13910Sstevel@tonic-gate * Get the page for the old swap slot and i/o lock it. 13920Sstevel@tonic-gate * Users of the physical slot will synchronize on the i/o lock. 13930Sstevel@tonic-gate */ 13940Sstevel@tonic-gate if (error = VOP_GETPAGE(vp, (offset_t)off, ptob(1), NULL, 1395*5331Samw pl, ptob(1), segkmap, NULL, S_READ, CRED(), NULL)) { 13960Sstevel@tonic-gate /* 13970Sstevel@tonic-gate * Anon slot went away (EIDRM) or vp was truncated (EFAULT) 13980Sstevel@tonic-gate * while we got the page. Thus the physical slot must be 13990Sstevel@tonic-gate * free, so we have succeeded. 14000Sstevel@tonic-gate */ 14010Sstevel@tonic-gate if (error == EIDRM || error == EFAULT) 14020Sstevel@tonic-gate error = 0; 14030Sstevel@tonic-gate return (error); 14040Sstevel@tonic-gate } 14050Sstevel@tonic-gate pp = pl[0]; 14060Sstevel@tonic-gate page_io_lock(pp); 14070Sstevel@tonic-gate 14080Sstevel@tonic-gate ahm = &anonhash_lock[AH_LOCK(vp, off)]; 14090Sstevel@tonic-gate mutex_enter(ahm); 14100Sstevel@tonic-gate /* 14110Sstevel@tonic-gate * Get the anon slot; anon struct cannot vanish while we hold 14120Sstevel@tonic-gate * SE_SHARED lock on the physical page since anon_decref() blocks 14130Sstevel@tonic-gate * in page_lookup() before it can proceed further to remove 14140Sstevel@tonic-gate * anon struct from anon_hash table. 14150Sstevel@tonic-gate */ 14160Sstevel@tonic-gate if ((ap = swap_anon(vp, off)) == NULL) { 14170Sstevel@tonic-gate panic("swapslot_free(%p, %llx, %p), page: %p, null anon", 14180Sstevel@tonic-gate vp, off, sip, pp); 14190Sstevel@tonic-gate } 14200Sstevel@tonic-gate /* 14210Sstevel@tonic-gate * Free the physical slot. It may have been freed up and replaced with 14220Sstevel@tonic-gate * another one while we were getting the page so we have to re-verify 14230Sstevel@tonic-gate * that this is really one we want. If we do free the slot we have 14240Sstevel@tonic-gate * to mark the page modified, as its backing store is now gone. 14250Sstevel@tonic-gate */ 14260Sstevel@tonic-gate if (ap->an_pvp == sip->si_vp && ap->an_poff >= sip->si_soff && 14270Sstevel@tonic-gate ap->an_poff < sip->si_eoff) { 14280Sstevel@tonic-gate swap_phys_free(ap->an_pvp, ap->an_poff, PAGESIZE); 14290Sstevel@tonic-gate ap->an_pvp = NULL; 14300Sstevel@tonic-gate ap->an_poff = NULL; 14310Sstevel@tonic-gate mutex_exit(ahm); 14320Sstevel@tonic-gate hat_setmod(pp); 14330Sstevel@tonic-gate } else { 14340Sstevel@tonic-gate mutex_exit(ahm); 14350Sstevel@tonic-gate } 14360Sstevel@tonic-gate out: 14370Sstevel@tonic-gate /* Release the page locks */ 14380Sstevel@tonic-gate page_unlock(pp); 14390Sstevel@tonic-gate page_io_unlock(pp); 14400Sstevel@tonic-gate return (error); 14410Sstevel@tonic-gate } 14420Sstevel@tonic-gate 14430Sstevel@tonic-gate /* 14440Sstevel@tonic-gate * Get contig physical backing store for vp, in the range 14450Sstevel@tonic-gate * [*offp, *offp + *lenp), May back a subrange of this, but must 14460Sstevel@tonic-gate * always include the requested offset or fail. Returns the offsets 14470Sstevel@tonic-gate * backed as [*offp, *offp + *lenp) and the physical offsets used to 14480Sstevel@tonic-gate * back them from *pvpp in the range [*pstartp, *pstartp + *lenp). 14490Sstevel@tonic-gate * Returns 0 for success 14500Sstevel@tonic-gate * SE_NOANON -- no anon slot for requested paged 14510Sstevel@tonic-gate * SE_NOSWAP -- no physical swap space available 14520Sstevel@tonic-gate */ 14530Sstevel@tonic-gate int 14540Sstevel@tonic-gate swap_newphysname( 14550Sstevel@tonic-gate struct vnode *vp, 14560Sstevel@tonic-gate u_offset_t offset, 14570Sstevel@tonic-gate u_offset_t *offp, 14580Sstevel@tonic-gate size_t *lenp, 14590Sstevel@tonic-gate struct vnode **pvpp, 14600Sstevel@tonic-gate u_offset_t *poffp) 14610Sstevel@tonic-gate { 14620Sstevel@tonic-gate struct anon *ap = NULL; /* anon slot for vp, off */ 14630Sstevel@tonic-gate int error = 0; 14640Sstevel@tonic-gate struct vnode *pvp; 14650Sstevel@tonic-gate u_offset_t poff, pstart, prem; 14660Sstevel@tonic-gate size_t plen; 14670Sstevel@tonic-gate u_offset_t off, start; 14680Sstevel@tonic-gate kmutex_t *ahm; 14690Sstevel@tonic-gate 14700Sstevel@tonic-gate ASSERT(*offp <= offset && offset < *offp + *lenp); 14710Sstevel@tonic-gate 14720Sstevel@tonic-gate /* Get new physical swap slots. */ 14730Sstevel@tonic-gate plen = *lenp; 14740Sstevel@tonic-gate if (!swap_phys_alloc(&pvp, &pstart, &plen, 0)) { 14750Sstevel@tonic-gate /* 14760Sstevel@tonic-gate * No swap available so return error unless requested 14770Sstevel@tonic-gate * offset is already backed in which case return that. 14780Sstevel@tonic-gate */ 14790Sstevel@tonic-gate ahm = &anonhash_lock[AH_LOCK(vp, offset)]; 14800Sstevel@tonic-gate mutex_enter(ahm); 14810Sstevel@tonic-gate if ((ap = swap_anon(vp, offset)) == NULL) { 14820Sstevel@tonic-gate error = SE_NOANON; 14830Sstevel@tonic-gate mutex_exit(ahm); 14840Sstevel@tonic-gate return (error); 14850Sstevel@tonic-gate } 14860Sstevel@tonic-gate error = (ap->an_pvp ? 0 : SE_NOSWAP); 14870Sstevel@tonic-gate *offp = offset; 14880Sstevel@tonic-gate *lenp = PAGESIZE; 14890Sstevel@tonic-gate *pvpp = ap->an_pvp; 14900Sstevel@tonic-gate *poffp = ap->an_poff; 14910Sstevel@tonic-gate mutex_exit(ahm); 14920Sstevel@tonic-gate return (error); 14930Sstevel@tonic-gate } 14940Sstevel@tonic-gate 14950Sstevel@tonic-gate /* 14960Sstevel@tonic-gate * We got plen (<= *lenp) contig slots. Use these to back a 14970Sstevel@tonic-gate * subrange of [*offp, *offp + *lenp) which includes offset. 14980Sstevel@tonic-gate * For now we just put offset at the end of the kluster. 14990Sstevel@tonic-gate * Clearly there are other possible choices - which is best? 15000Sstevel@tonic-gate */ 15010Sstevel@tonic-gate start = MAX(*offp, 15020Sstevel@tonic-gate (offset + PAGESIZE > plen) ? (offset + PAGESIZE - plen) : 0); 15030Sstevel@tonic-gate ASSERT(start + plen <= *offp + *lenp); 15040Sstevel@tonic-gate 15050Sstevel@tonic-gate for (off = start, poff = pstart; poff < pstart + plen; 15060Sstevel@tonic-gate off += PAGESIZE, poff += PAGESIZE) { 15070Sstevel@tonic-gate ahm = &anonhash_lock[AH_LOCK(vp, off)]; 15080Sstevel@tonic-gate mutex_enter(ahm); 15090Sstevel@tonic-gate if ((ap = swap_anon(vp, off)) != NULL) { 15100Sstevel@tonic-gate /* Free old slot if any, and assign new one */ 15110Sstevel@tonic-gate if (ap->an_pvp) 15120Sstevel@tonic-gate swap_phys_free(ap->an_pvp, ap->an_poff, 15130Sstevel@tonic-gate PAGESIZE); 15140Sstevel@tonic-gate ap->an_pvp = pvp; 15150Sstevel@tonic-gate ap->an_poff = poff; 15160Sstevel@tonic-gate } else { /* No anon slot for a klustered page, quit. */ 15170Sstevel@tonic-gate prem = (pstart + plen) - poff; 15180Sstevel@tonic-gate /* Already did requested page, do partial kluster */ 15190Sstevel@tonic-gate if (off > offset) { 15200Sstevel@tonic-gate plen = poff - pstart; 15210Sstevel@tonic-gate error = 0; 15220Sstevel@tonic-gate /* Fail on requested page, error */ 15230Sstevel@tonic-gate } else if (off == offset) { 15240Sstevel@tonic-gate error = SE_NOANON; 15250Sstevel@tonic-gate /* Fail on prior page, fail on requested page, error */ 15260Sstevel@tonic-gate } else if ((ap = swap_anon(vp, offset)) == NULL) { 15270Sstevel@tonic-gate error = SE_NOANON; 15280Sstevel@tonic-gate /* Fail on prior page, got requested page, do only it */ 15290Sstevel@tonic-gate } else { 15300Sstevel@tonic-gate /* Free old slot if any, and assign new one */ 15310Sstevel@tonic-gate if (ap->an_pvp) 15320Sstevel@tonic-gate swap_phys_free(ap->an_pvp, ap->an_poff, 15330Sstevel@tonic-gate PAGESIZE); 15340Sstevel@tonic-gate ap->an_pvp = pvp; 15350Sstevel@tonic-gate ap->an_poff = poff; 15360Sstevel@tonic-gate /* One page kluster */ 15370Sstevel@tonic-gate start = offset; 15380Sstevel@tonic-gate plen = PAGESIZE; 15390Sstevel@tonic-gate pstart = poff; 15400Sstevel@tonic-gate poff += PAGESIZE; 15410Sstevel@tonic-gate prem -= PAGESIZE; 15420Sstevel@tonic-gate } 15430Sstevel@tonic-gate /* Free unassigned slots */ 15440Sstevel@tonic-gate swap_phys_free(pvp, poff, prem); 15450Sstevel@tonic-gate mutex_exit(ahm); 15460Sstevel@tonic-gate break; 15470Sstevel@tonic-gate } 15480Sstevel@tonic-gate mutex_exit(ahm); 15490Sstevel@tonic-gate } 15500Sstevel@tonic-gate ASSERT(*offp <= start && start + plen <= *offp + *lenp); 15510Sstevel@tonic-gate ASSERT(start <= offset && offset < start + plen); 15520Sstevel@tonic-gate *offp = start; 15530Sstevel@tonic-gate *lenp = plen; 15540Sstevel@tonic-gate *pvpp = pvp; 15550Sstevel@tonic-gate *poffp = pstart; 15560Sstevel@tonic-gate return (error); 15570Sstevel@tonic-gate } 15580Sstevel@tonic-gate 15590Sstevel@tonic-gate 15600Sstevel@tonic-gate /* 15610Sstevel@tonic-gate * Get the physical swap backing store location for a given anonymous page 15620Sstevel@tonic-gate * named (vp, off). The backing store name is returned in (*pvpp, *poffp). 15630Sstevel@tonic-gate * Returns 0 success 15640Sstevel@tonic-gate * EIDRM -- no anon slot (page is not allocated) 15650Sstevel@tonic-gate */ 15660Sstevel@tonic-gate int 15670Sstevel@tonic-gate swap_getphysname( 15680Sstevel@tonic-gate struct vnode *vp, 15690Sstevel@tonic-gate u_offset_t off, 15700Sstevel@tonic-gate struct vnode **pvpp, 15710Sstevel@tonic-gate u_offset_t *poffp) 15720Sstevel@tonic-gate { 15730Sstevel@tonic-gate struct anon *ap; 15740Sstevel@tonic-gate int error = 0; 15750Sstevel@tonic-gate kmutex_t *ahm; 15760Sstevel@tonic-gate 15770Sstevel@tonic-gate ahm = &anonhash_lock[AH_LOCK(vp, off)]; 15780Sstevel@tonic-gate mutex_enter(ahm); 15790Sstevel@tonic-gate 15800Sstevel@tonic-gate /* Get anon slot for vp, off */ 15810Sstevel@tonic-gate ap = swap_anon(vp, off); 15820Sstevel@tonic-gate if (ap == NULL) { 15830Sstevel@tonic-gate error = EIDRM; 15840Sstevel@tonic-gate goto out; 15850Sstevel@tonic-gate } 15860Sstevel@tonic-gate *pvpp = ap->an_pvp; 15870Sstevel@tonic-gate *poffp = ap->an_poff; 15880Sstevel@tonic-gate out: 15890Sstevel@tonic-gate mutex_exit(ahm); 15900Sstevel@tonic-gate return (error); 15910Sstevel@tonic-gate } 1592