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 /* 22*12173SMichael.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 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 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 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 * 3550Sstevel@tonic-gate swap_anon(struct vnode *vp, u_offset_t off) 3560Sstevel@tonic-gate { 3570Sstevel@tonic-gate struct anon *ap; 3580Sstevel@tonic-gate 359*12173SMichael.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 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 * 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 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 4870Sstevel@tonic-gate ai.ani_free = k_anoninfo.ani_free + avail; 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate ai.ani_resv = k_anoninfo.ani_phys_resv + 4900Sstevel@tonic-gate k_anoninfo.ani_mem_resv; 4910Sstevel@tonic-gate 4927884Sgerald.jelinek@sun.com if (!global && zp->zone_max_swap_ctl != UINT64_MAX) { 4937884Sgerald.jelinek@sun.com /* 4947884Sgerald.jelinek@sun.com * We're in a non-global zone with a swap cap. We 4957884Sgerald.jelinek@sun.com * always report the system-wide values for the global 4967884Sgerald.jelinek@sun.com * zone, even though it too can have a swap cap. 4977884Sgerald.jelinek@sun.com */ 4987884Sgerald.jelinek@sun.com 4997884Sgerald.jelinek@sun.com /* 5007884Sgerald.jelinek@sun.com * For a swap-capped zone, the numbers are contrived 5017884Sgerald.jelinek@sun.com * since we don't have a correct value of 'reserved' 5027884Sgerald.jelinek@sun.com * for the zone. 5037884Sgerald.jelinek@sun.com * 5047884Sgerald.jelinek@sun.com * The ani_max value is always the zone's swap cap. 5057884Sgerald.jelinek@sun.com * 5067884Sgerald.jelinek@sun.com * The ani_free value is always the difference between 5077884Sgerald.jelinek@sun.com * the cap and the amount of swap in use by the zone. 5087884Sgerald.jelinek@sun.com * 5097884Sgerald.jelinek@sun.com * The ani_resv value is typically set to be the amount 5107884Sgerald.jelinek@sun.com * of swap in use by the zone, but can be adjusted 5117884Sgerald.jelinek@sun.com * upwards to indicate how much swap is currently 5127884Sgerald.jelinek@sun.com * unavailable to that zone due to usage by entities 5137884Sgerald.jelinek@sun.com * outside the zone. 5147884Sgerald.jelinek@sun.com * 5157884Sgerald.jelinek@sun.com * This works as follows. 5167884Sgerald.jelinek@sun.com * 5177884Sgerald.jelinek@sun.com * In the 'swap -s' output, the data is displayed 5187884Sgerald.jelinek@sun.com * as follows: 5197884Sgerald.jelinek@sun.com * allocated = ani_max - ani_free 5207884Sgerald.jelinek@sun.com * reserved = ani_resv - allocated 5217884Sgerald.jelinek@sun.com * available = ani_max - ani_resv 5227884Sgerald.jelinek@sun.com * 5237884Sgerald.jelinek@sun.com * Taking a contrived example, if the swap cap is 100 5247884Sgerald.jelinek@sun.com * and the amount of swap used by the zone is 75, this 5257884Sgerald.jelinek@sun.com * gives: 5267884Sgerald.jelinek@sun.com * allocated = ani_max - ani_free = 100 - 25 = 75 5277884Sgerald.jelinek@sun.com * reserved = ani_resv - allocated = 75 - 75 = 0 5287884Sgerald.jelinek@sun.com * available = ani_max - ani_resv = 100 - 75 = 25 5297884Sgerald.jelinek@sun.com * 5307884Sgerald.jelinek@sun.com * In this typical case, you can see that the 'swap -s' 5317884Sgerald.jelinek@sun.com * 'reserved' will always be 0 inside a swap capped 5327884Sgerald.jelinek@sun.com * zone. 5337884Sgerald.jelinek@sun.com * 5347884Sgerald.jelinek@sun.com * However, if the system as a whole has less free 5357884Sgerald.jelinek@sun.com * swap than the zone limits allow, then we adjust 5367884Sgerald.jelinek@sun.com * the ani_resv value up so that it is the difference 5377884Sgerald.jelinek@sun.com * between the zone cap and the amount of free system 5387884Sgerald.jelinek@sun.com * swap. Taking the above example, but when the 5397884Sgerald.jelinek@sun.com * system as a whole only has 20 of swap available, we 5407884Sgerald.jelinek@sun.com * get an ani_resv of 100 - 20 = 80. This gives: 5417884Sgerald.jelinek@sun.com * allocated = ani_max - ani_free = 100 - 25 = 75 5427884Sgerald.jelinek@sun.com * reserved = ani_resv - allocated = 80 - 75 = 5 5437884Sgerald.jelinek@sun.com * available = ani_max - ani_resv = 100 - 80 = 20 5447884Sgerald.jelinek@sun.com * 5457884Sgerald.jelinek@sun.com * In this case, you can see how the ani_resv value is 5467884Sgerald.jelinek@sun.com * tweaked up to make the 'swap -s' numbers work inside 5477884Sgerald.jelinek@sun.com * the zone. 5487884Sgerald.jelinek@sun.com */ 5497884Sgerald.jelinek@sun.com rctl_qty_t cap, used; 5507884Sgerald.jelinek@sun.com pgcnt_t pgcap, sys_avail; 5517884Sgerald.jelinek@sun.com 5527884Sgerald.jelinek@sun.com mutex_enter(&zp->zone_mem_lock); 5537884Sgerald.jelinek@sun.com cap = zp->zone_max_swap_ctl; 5547884Sgerald.jelinek@sun.com used = zp->zone_max_swap; 5557884Sgerald.jelinek@sun.com mutex_exit(&zp->zone_mem_lock); 5567884Sgerald.jelinek@sun.com 5577884Sgerald.jelinek@sun.com pgcap = MIN(btop(cap), ai.ani_max); 5587884Sgerald.jelinek@sun.com ai.ani_free = pgcap - btop(used); 5597884Sgerald.jelinek@sun.com 5607884Sgerald.jelinek@sun.com /* Get the system-wide swap currently available. */ 5617884Sgerald.jelinek@sun.com sys_avail = ai.ani_max - ai.ani_resv; 5627884Sgerald.jelinek@sun.com if (sys_avail < ai.ani_free) 5637884Sgerald.jelinek@sun.com ai.ani_resv = pgcap - sys_avail; 5647884Sgerald.jelinek@sun.com else 5657884Sgerald.jelinek@sun.com ai.ani_resv = btop(used); 5667884Sgerald.jelinek@sun.com 5677884Sgerald.jelinek@sun.com ai.ani_max = pgcap; 5687884Sgerald.jelinek@sun.com } 5697884Sgerald.jelinek@sun.com 5700Sstevel@tonic-gate if (copyout(&ai, sc_arg, sizeof (struct anoninfo)) != 0) 5710Sstevel@tonic-gate return (EFAULT); 5720Sstevel@tonic-gate return (0); 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate case SC_LIST: 5750Sstevel@tonic-gate if (copyin(sc_arg, &length, sizeof (int)) != 0) 5760Sstevel@tonic-gate return (EFAULT); 5770Sstevel@tonic-gate if (!global) { 5780Sstevel@tonic-gate struct swapent st; 5790Sstevel@tonic-gate char *swappath = "swap"; 5800Sstevel@tonic-gate 5810Sstevel@tonic-gate if (length < 1) 5820Sstevel@tonic-gate return (ENOMEM); 5830Sstevel@tonic-gate ust = (swapent_t *)((swaptbl_t *)sc_arg)->swt_ent; 5840Sstevel@tonic-gate if (copyin(ust, &st, sizeof (swapent_t)) != 0) 5850Sstevel@tonic-gate return (EFAULT); 5860Sstevel@tonic-gate st.ste_start = PAGESIZE >> SCTRSHFT; 5870Sstevel@tonic-gate st.ste_length = (off_t)0; 5880Sstevel@tonic-gate st.ste_pages = 0; 5890Sstevel@tonic-gate st.ste_free = 0; 5900Sstevel@tonic-gate st.ste_flags = 0; 5917884Sgerald.jelinek@sun.com 5920Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 5930Sstevel@tonic-gate for (sip = swapinfo, nswap = 0; 5940Sstevel@tonic-gate sip != NULL && nswap < nswapfiles; 5950Sstevel@tonic-gate sip = sip->si_next, nswap++) { 5960Sstevel@tonic-gate st.ste_length += 5970Sstevel@tonic-gate (sip->si_eoff - sip->si_soff) >> SCTRSHFT; 5980Sstevel@tonic-gate st.ste_pages += sip->si_npgs; 5990Sstevel@tonic-gate st.ste_free += sip->si_nfpgs; 6000Sstevel@tonic-gate } 6010Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 6027884Sgerald.jelinek@sun.com 6037884Sgerald.jelinek@sun.com if (zp->zone_max_swap_ctl != UINT64_MAX) { 6047884Sgerald.jelinek@sun.com rctl_qty_t cap, used; 6057884Sgerald.jelinek@sun.com 6067884Sgerald.jelinek@sun.com mutex_enter(&zp->zone_mem_lock); 6077884Sgerald.jelinek@sun.com cap = zp->zone_max_swap_ctl; 6087884Sgerald.jelinek@sun.com used = zp->zone_max_swap; 6097884Sgerald.jelinek@sun.com mutex_exit(&zp->zone_mem_lock); 6107884Sgerald.jelinek@sun.com 6117884Sgerald.jelinek@sun.com st.ste_length = MIN(cap, st.ste_length); 6127884Sgerald.jelinek@sun.com st.ste_pages = MIN(btop(cap), st.ste_pages); 6137884Sgerald.jelinek@sun.com st.ste_free = MIN(st.ste_pages - btop(used), 6147884Sgerald.jelinek@sun.com st.ste_free); 6157884Sgerald.jelinek@sun.com } 6167884Sgerald.jelinek@sun.com 6170Sstevel@tonic-gate if (copyout(&st, ust, sizeof (swapent_t)) != 0 || 6180Sstevel@tonic-gate copyout(swappath, st.ste_path, 6195665Sstans strlen(swappath) + 1) != 0) { 6200Sstevel@tonic-gate return (EFAULT); 6210Sstevel@tonic-gate } 6220Sstevel@tonic-gate *rv = 1; 6230Sstevel@tonic-gate return (0); 6240Sstevel@tonic-gate } 6250Sstevel@tonic-gate beginning: 6260Sstevel@tonic-gate tmp_nswapfiles = nswapfiles; 6270Sstevel@tonic-gate /* Return an error if not enough space for the whole table. */ 6280Sstevel@tonic-gate if (length < tmp_nswapfiles) 6290Sstevel@tonic-gate return (ENOMEM); 6300Sstevel@tonic-gate /* 6310Sstevel@tonic-gate * Get memory to hold the swap entries and their names. We'll 6320Sstevel@tonic-gate * copy the real entries into these and then copy these out. 6330Sstevel@tonic-gate * Allocating the pathname memory is only a guess so we may 6340Sstevel@tonic-gate * find that we need more and have to do it again. 6350Sstevel@tonic-gate * All this is because we have to hold the anon lock while 6360Sstevel@tonic-gate * traversing the swapinfo list, and we can't be doing copyouts 6370Sstevel@tonic-gate * and/or kmem_alloc()s during this. 6380Sstevel@tonic-gate */ 6390Sstevel@tonic-gate csip = kmem_zalloc(tmp_nswapfiles * sizeof (struct swapinfo), 6400Sstevel@tonic-gate KM_SLEEP); 6410Sstevel@tonic-gate retry: 6420Sstevel@tonic-gate nlen = tmp_nswapfiles * (gplen += 100); 6430Sstevel@tonic-gate pname = kmem_zalloc(nlen, KM_SLEEP); 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate if (tmp_nswapfiles != nswapfiles) { 6480Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 6490Sstevel@tonic-gate kmem_free(pname, nlen); 6500Sstevel@tonic-gate kmem_free(csip, 6510Sstevel@tonic-gate tmp_nswapfiles * sizeof (struct swapinfo)); 6520Sstevel@tonic-gate gplen = 0; 6530Sstevel@tonic-gate goto beginning; 6540Sstevel@tonic-gate } 6550Sstevel@tonic-gate for (sip = swapinfo, tsip = csip, tpname = pname, nswap = 0; 6560Sstevel@tonic-gate sip && nswap < tmp_nswapfiles; 6570Sstevel@tonic-gate sip = sip->si_next, tsip++, tpname += plen, nswap++) { 6580Sstevel@tonic-gate plen = sip->si_pnamelen; 6590Sstevel@tonic-gate if (tpname + plen - pname > nlen) { 6600Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 6610Sstevel@tonic-gate kmem_free(pname, nlen); 6620Sstevel@tonic-gate goto retry; 6630Sstevel@tonic-gate } 6640Sstevel@tonic-gate *tsip = *sip; 6650Sstevel@tonic-gate tsip->si_pname = tpname; 6660Sstevel@tonic-gate (void) strcpy(tsip->si_pname, sip->si_pname); 6670Sstevel@tonic-gate } 6680Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 6690Sstevel@tonic-gate 6700Sstevel@tonic-gate if (sip) { 6710Sstevel@tonic-gate error = ENOMEM; 6720Sstevel@tonic-gate goto lout; 6730Sstevel@tonic-gate } 6740Sstevel@tonic-gate ust = (swapent_t *)((swaptbl_t *)sc_arg)->swt_ent; 6750Sstevel@tonic-gate for (tsip = csip, cnt = 0; cnt < nswap; tsip++, ust++, cnt++) { 6760Sstevel@tonic-gate if (copyin(ust, &st, sizeof (swapent_t)) != 0) { 6770Sstevel@tonic-gate error = EFAULT; 6780Sstevel@tonic-gate goto lout; 6790Sstevel@tonic-gate } 6800Sstevel@tonic-gate st.ste_flags = tsip->si_flags; 6810Sstevel@tonic-gate st.ste_length = 6820Sstevel@tonic-gate (tsip->si_eoff - tsip->si_soff) >> SCTRSHFT; 6830Sstevel@tonic-gate st.ste_start = tsip->si_soff >> SCTRSHFT; 6840Sstevel@tonic-gate st.ste_pages = tsip->si_npgs; 6850Sstevel@tonic-gate st.ste_free = tsip->si_nfpgs; 6860Sstevel@tonic-gate if (copyout(&st, ust, sizeof (swapent_t)) != 0) { 6870Sstevel@tonic-gate error = EFAULT; 6880Sstevel@tonic-gate goto lout; 6890Sstevel@tonic-gate } 6900Sstevel@tonic-gate if (!tsip->si_pnamelen) 6910Sstevel@tonic-gate continue; 6920Sstevel@tonic-gate if (copyout(tsip->si_pname, st.ste_path, 6935665Sstans tsip->si_pnamelen) != 0) { 6940Sstevel@tonic-gate error = EFAULT; 6950Sstevel@tonic-gate goto lout; 6960Sstevel@tonic-gate } 6970Sstevel@tonic-gate } 6980Sstevel@tonic-gate *rv = nswap; 6990Sstevel@tonic-gate lout: 7000Sstevel@tonic-gate kmem_free(csip, tmp_nswapfiles * sizeof (struct swapinfo)); 7010Sstevel@tonic-gate kmem_free(pname, nlen); 7020Sstevel@tonic-gate return (error); 7030Sstevel@tonic-gate 7040Sstevel@tonic-gate case SC_ADD: 7050Sstevel@tonic-gate case SC_REMOVE: 7060Sstevel@tonic-gate break; 7070Sstevel@tonic-gate default: 7080Sstevel@tonic-gate return (EINVAL); 7090Sstevel@tonic-gate } 7100Sstevel@tonic-gate if ((error = secpolicy_swapctl(CRED())) != 0) 7110Sstevel@tonic-gate return (error); 7120Sstevel@tonic-gate 7130Sstevel@tonic-gate if (copyin(sc_arg, &sr, sizeof (swapres_t))) 7140Sstevel@tonic-gate return (EFAULT); 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate /* Allocate the space to read in pathname */ 7170Sstevel@tonic-gate if ((swapname = kmem_alloc(MAXPATHLEN, KM_NOSLEEP)) == NULL) 7180Sstevel@tonic-gate return (ENOMEM); 7190Sstevel@tonic-gate 7200Sstevel@tonic-gate error = copyinstr(sr.sr_name, swapname, MAXPATHLEN, 0); 7210Sstevel@tonic-gate if (error) 7220Sstevel@tonic-gate goto out; 7230Sstevel@tonic-gate 7240Sstevel@tonic-gate error = lookupname(swapname, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp); 7250Sstevel@tonic-gate if (error) { 7260Sstevel@tonic-gate if (sc_cmd == SC_ADD) 7270Sstevel@tonic-gate goto out; 7280Sstevel@tonic-gate /* see if we match by name */ 7290Sstevel@tonic-gate vp = swapdel_byname(swapname, (size_t)sr.sr_start); 7300Sstevel@tonic-gate if (vp == NULL) 7310Sstevel@tonic-gate goto out; 7320Sstevel@tonic-gate } 7330Sstevel@tonic-gate 7340Sstevel@tonic-gate if (vp->v_flag & (VNOMAP | VNOSWAP)) { 7350Sstevel@tonic-gate VN_RELE(vp); 7360Sstevel@tonic-gate error = ENOSYS; 7370Sstevel@tonic-gate goto out; 7380Sstevel@tonic-gate } 7390Sstevel@tonic-gate switch (vp->v_type) { 7400Sstevel@tonic-gate case VBLK: 7410Sstevel@tonic-gate break; 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate case VREG: 7440Sstevel@tonic-gate if (vp->v_vfsp && vn_is_readonly(vp)) 7450Sstevel@tonic-gate error = EROFS; 7460Sstevel@tonic-gate else 7475331Samw error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL); 7480Sstevel@tonic-gate break; 7490Sstevel@tonic-gate 7500Sstevel@tonic-gate case VDIR: 7510Sstevel@tonic-gate error = EISDIR; 7520Sstevel@tonic-gate break; 7530Sstevel@tonic-gate default: 7540Sstevel@tonic-gate error = ENOSYS; 7550Sstevel@tonic-gate break; 7560Sstevel@tonic-gate } 7570Sstevel@tonic-gate if (error == 0) { 7580Sstevel@tonic-gate if (sc_cmd == SC_REMOVE) 7590Sstevel@tonic-gate error = swapdel(vp, sr.sr_start); 7600Sstevel@tonic-gate else 7610Sstevel@tonic-gate error = swapadd(vp, sr.sr_start, 7625665Sstans sr.sr_length, swapname); 7630Sstevel@tonic-gate } 7640Sstevel@tonic-gate VN_RELE(vp); 7650Sstevel@tonic-gate out: 7660Sstevel@tonic-gate kmem_free(swapname, MAXPATHLEN); 7670Sstevel@tonic-gate return (error); 7680Sstevel@tonic-gate } 7690Sstevel@tonic-gate 7700Sstevel@tonic-gate #if defined(_LP64) && defined(_SYSCALL32) 7710Sstevel@tonic-gate 7720Sstevel@tonic-gate int 7730Sstevel@tonic-gate swapctl32(int sc_cmd, void *sc_arg, int *rv) 7740Sstevel@tonic-gate { 7750Sstevel@tonic-gate struct swapinfo *sip, *csip, *tsip; 7760Sstevel@tonic-gate int error = 0; 7770Sstevel@tonic-gate struct swapent32 st, *ust; 7780Sstevel@tonic-gate struct swapres32 sr; 7790Sstevel@tonic-gate struct vnode *vp; 7800Sstevel@tonic-gate int cnt = 0; 7810Sstevel@tonic-gate int tmp_nswapfiles; 7820Sstevel@tonic-gate int nswap; 7830Sstevel@tonic-gate int length, nlen; 7840Sstevel@tonic-gate int gplen = 0, plen; 7850Sstevel@tonic-gate char *swapname; 7860Sstevel@tonic-gate char *pname; 7870Sstevel@tonic-gate char *tpname; 7880Sstevel@tonic-gate struct anoninfo32 ai; 7890Sstevel@tonic-gate size_t s; 7900Sstevel@tonic-gate spgcnt_t avail; 7917884Sgerald.jelinek@sun.com int global = INGLOBALZONE(curproc); 7927884Sgerald.jelinek@sun.com struct zone *zp = curproc->p_zone; 7930Sstevel@tonic-gate 7947884Sgerald.jelinek@sun.com /* 7957884Sgerald.jelinek@sun.com * When running in a zone we want to hide the details of the swap 7967884Sgerald.jelinek@sun.com * devices: we report there only being one swap device named "swap" 7977884Sgerald.jelinek@sun.com * having a size equal to the sum of the sizes of all real swap devices 7987884Sgerald.jelinek@sun.com * on the system. 7997884Sgerald.jelinek@sun.com */ 8000Sstevel@tonic-gate switch (sc_cmd) { 8010Sstevel@tonic-gate case SC_GETNSWP: 8027884Sgerald.jelinek@sun.com if (global) 8037884Sgerald.jelinek@sun.com *rv = nswapfiles; 8047884Sgerald.jelinek@sun.com else 8057884Sgerald.jelinek@sun.com *rv = 1; 8060Sstevel@tonic-gate return (0); 8070Sstevel@tonic-gate 8080Sstevel@tonic-gate case SC_AINFO: 8090Sstevel@tonic-gate /* 8100Sstevel@tonic-gate * Return anoninfo information with these changes: 8110Sstevel@tonic-gate * ani_max = maximum amount of swap space 8120Sstevel@tonic-gate * (including potentially available physical memory) 8130Sstevel@tonic-gate * ani_free = amount of unallocated anonymous memory 8140Sstevel@tonic-gate * (some of which might be reserved and including 8150Sstevel@tonic-gate * potentially available physical memory) 8160Sstevel@tonic-gate * ani_resv = amount of claimed (reserved) anonymous memory 8170Sstevel@tonic-gate */ 8180Sstevel@tonic-gate avail = MAX((spgcnt_t)(availrmem - swapfs_minfree), 0); 8190Sstevel@tonic-gate s = (k_anoninfo.ani_max + k_anoninfo.ani_mem_resv) + avail; 8200Sstevel@tonic-gate if (s > UINT32_MAX) 8210Sstevel@tonic-gate return (EOVERFLOW); 8220Sstevel@tonic-gate ai.ani_max = s; 8230Sstevel@tonic-gate 8240Sstevel@tonic-gate s = k_anoninfo.ani_free + avail; 8250Sstevel@tonic-gate if (s > UINT32_MAX) 8260Sstevel@tonic-gate return (EOVERFLOW); 8270Sstevel@tonic-gate ai.ani_free = s; 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate s = k_anoninfo.ani_phys_resv + k_anoninfo.ani_mem_resv; 8300Sstevel@tonic-gate if (s > UINT32_MAX) 8310Sstevel@tonic-gate return (EOVERFLOW); 8320Sstevel@tonic-gate ai.ani_resv = s; 8330Sstevel@tonic-gate 8347884Sgerald.jelinek@sun.com if (!global && zp->zone_max_swap_ctl != UINT64_MAX) { 8357884Sgerald.jelinek@sun.com /* 8367884Sgerald.jelinek@sun.com * We're in a non-global zone with a swap cap. We 8377884Sgerald.jelinek@sun.com * always report the system-wide values for the global 8387884Sgerald.jelinek@sun.com * zone, even though it too can have a swap cap. 8397884Sgerald.jelinek@sun.com * See the comment for the SC_AINFO case in swapctl() 8407884Sgerald.jelinek@sun.com * which explains the following logic. 8417884Sgerald.jelinek@sun.com */ 8427884Sgerald.jelinek@sun.com rctl_qty_t cap, used; 8437884Sgerald.jelinek@sun.com pgcnt_t pgcap, sys_avail; 8447884Sgerald.jelinek@sun.com 8457884Sgerald.jelinek@sun.com mutex_enter(&zp->zone_mem_lock); 8467884Sgerald.jelinek@sun.com cap = zp->zone_max_swap_ctl; 8477884Sgerald.jelinek@sun.com used = zp->zone_max_swap; 8487884Sgerald.jelinek@sun.com mutex_exit(&zp->zone_mem_lock); 8497884Sgerald.jelinek@sun.com 8507884Sgerald.jelinek@sun.com pgcap = MIN(btop(cap), ai.ani_max); 8517884Sgerald.jelinek@sun.com ai.ani_free = pgcap - btop(used); 8527884Sgerald.jelinek@sun.com 8537884Sgerald.jelinek@sun.com /* Get the system-wide swap currently available. */ 8547884Sgerald.jelinek@sun.com sys_avail = ai.ani_max - ai.ani_resv; 8557884Sgerald.jelinek@sun.com if (sys_avail < ai.ani_free) 8567884Sgerald.jelinek@sun.com ai.ani_resv = pgcap - sys_avail; 8577884Sgerald.jelinek@sun.com else 8587884Sgerald.jelinek@sun.com ai.ani_resv = btop(used); 8597884Sgerald.jelinek@sun.com 8607884Sgerald.jelinek@sun.com ai.ani_max = pgcap; 8617884Sgerald.jelinek@sun.com } 8627884Sgerald.jelinek@sun.com 8630Sstevel@tonic-gate if (copyout(&ai, sc_arg, sizeof (ai)) != 0) 8640Sstevel@tonic-gate return (EFAULT); 8650Sstevel@tonic-gate return (0); 8660Sstevel@tonic-gate 8670Sstevel@tonic-gate case SC_LIST: 8680Sstevel@tonic-gate if (copyin(sc_arg, &length, sizeof (int32_t)) != 0) 8690Sstevel@tonic-gate return (EFAULT); 8707884Sgerald.jelinek@sun.com if (!global) { 8717884Sgerald.jelinek@sun.com struct swapent32 st; 8727884Sgerald.jelinek@sun.com char *swappath = "swap"; 8737884Sgerald.jelinek@sun.com 8747884Sgerald.jelinek@sun.com if (length < 1) 8757884Sgerald.jelinek@sun.com return (ENOMEM); 8767884Sgerald.jelinek@sun.com ust = (swapent32_t *)((swaptbl32_t *)sc_arg)->swt_ent; 8777884Sgerald.jelinek@sun.com if (copyin(ust, &st, sizeof (swapent32_t)) != 0) 8787884Sgerald.jelinek@sun.com return (EFAULT); 8797884Sgerald.jelinek@sun.com st.ste_start = PAGESIZE >> SCTRSHFT; 8807884Sgerald.jelinek@sun.com st.ste_length = (off_t)0; 8817884Sgerald.jelinek@sun.com st.ste_pages = 0; 8827884Sgerald.jelinek@sun.com st.ste_free = 0; 8837884Sgerald.jelinek@sun.com st.ste_flags = 0; 8847884Sgerald.jelinek@sun.com 8857884Sgerald.jelinek@sun.com mutex_enter(&swapinfo_lock); 8867884Sgerald.jelinek@sun.com for (sip = swapinfo, nswap = 0; 8877884Sgerald.jelinek@sun.com sip != NULL && nswap < nswapfiles; 8887884Sgerald.jelinek@sun.com sip = sip->si_next, nswap++) { 8897884Sgerald.jelinek@sun.com st.ste_length += 8907884Sgerald.jelinek@sun.com (sip->si_eoff - sip->si_soff) >> SCTRSHFT; 8917884Sgerald.jelinek@sun.com st.ste_pages += sip->si_npgs; 8927884Sgerald.jelinek@sun.com st.ste_free += sip->si_nfpgs; 8937884Sgerald.jelinek@sun.com } 8947884Sgerald.jelinek@sun.com mutex_exit(&swapinfo_lock); 8957884Sgerald.jelinek@sun.com 8967884Sgerald.jelinek@sun.com if (zp->zone_max_swap_ctl != UINT64_MAX) { 8977884Sgerald.jelinek@sun.com rctl_qty_t cap, used; 8987884Sgerald.jelinek@sun.com 8997884Sgerald.jelinek@sun.com mutex_enter(&zp->zone_mem_lock); 9007884Sgerald.jelinek@sun.com cap = zp->zone_max_swap_ctl; 9017884Sgerald.jelinek@sun.com used = zp->zone_max_swap; 9027884Sgerald.jelinek@sun.com mutex_exit(&zp->zone_mem_lock); 9037884Sgerald.jelinek@sun.com 9047884Sgerald.jelinek@sun.com st.ste_length = MIN(cap, st.ste_length); 9057884Sgerald.jelinek@sun.com st.ste_pages = MIN(btop(cap), st.ste_pages); 9067884Sgerald.jelinek@sun.com st.ste_free = MIN(st.ste_pages - btop(used), 9077884Sgerald.jelinek@sun.com st.ste_free); 9087884Sgerald.jelinek@sun.com } 9097884Sgerald.jelinek@sun.com 9107884Sgerald.jelinek@sun.com if (copyout(&st, ust, sizeof (swapent32_t)) != 0 || 9117884Sgerald.jelinek@sun.com copyout(swappath, (caddr_t)(uintptr_t)st.ste_path, 9127884Sgerald.jelinek@sun.com strlen(swappath) + 1) != 0) { 9137884Sgerald.jelinek@sun.com return (EFAULT); 9147884Sgerald.jelinek@sun.com } 9157884Sgerald.jelinek@sun.com *rv = 1; 9167884Sgerald.jelinek@sun.com return (0); 9177884Sgerald.jelinek@sun.com } 9180Sstevel@tonic-gate beginning: 9190Sstevel@tonic-gate tmp_nswapfiles = nswapfiles; 9200Sstevel@tonic-gate /* Return an error if not enough space for the whole table. */ 9210Sstevel@tonic-gate if (length < tmp_nswapfiles) 9220Sstevel@tonic-gate return (ENOMEM); 9230Sstevel@tonic-gate /* 9240Sstevel@tonic-gate * Get memory to hold the swap entries and their names. We'll 9250Sstevel@tonic-gate * copy the real entries into these and then copy these out. 9260Sstevel@tonic-gate * Allocating the pathname memory is only a guess so we may 9270Sstevel@tonic-gate * find that we need more and have to do it again. 9280Sstevel@tonic-gate * All this is because we have to hold the anon lock while 9290Sstevel@tonic-gate * traversing the swapinfo list, and we can't be doing copyouts 9300Sstevel@tonic-gate * and/or kmem_alloc()s during this. 9310Sstevel@tonic-gate */ 9320Sstevel@tonic-gate csip = kmem_zalloc(tmp_nswapfiles * sizeof (*csip), KM_SLEEP); 9330Sstevel@tonic-gate retry: 9340Sstevel@tonic-gate nlen = tmp_nswapfiles * (gplen += 100); 9350Sstevel@tonic-gate pname = kmem_zalloc(nlen, KM_SLEEP); 9360Sstevel@tonic-gate 9370Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 9380Sstevel@tonic-gate 9390Sstevel@tonic-gate if (tmp_nswapfiles != nswapfiles) { 9400Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 9410Sstevel@tonic-gate kmem_free(pname, nlen); 9420Sstevel@tonic-gate kmem_free(csip, tmp_nswapfiles * sizeof (*csip)); 9430Sstevel@tonic-gate gplen = 0; 9440Sstevel@tonic-gate goto beginning; 9450Sstevel@tonic-gate } 9460Sstevel@tonic-gate for (sip = swapinfo, tsip = csip, tpname = pname, nswap = 0; 9470Sstevel@tonic-gate (sip != NULL) && (nswap < tmp_nswapfiles); 9480Sstevel@tonic-gate sip = sip->si_next, tsip++, tpname += plen, nswap++) { 9490Sstevel@tonic-gate plen = sip->si_pnamelen; 9500Sstevel@tonic-gate if (tpname + plen - pname > nlen) { 9510Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 9520Sstevel@tonic-gate kmem_free(pname, nlen); 9530Sstevel@tonic-gate goto retry; 9540Sstevel@tonic-gate } 9550Sstevel@tonic-gate *tsip = *sip; 9560Sstevel@tonic-gate tsip->si_pname = tpname; 9570Sstevel@tonic-gate (void) strcpy(tsip->si_pname, sip->si_pname); 9580Sstevel@tonic-gate } 9590Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 9600Sstevel@tonic-gate 9610Sstevel@tonic-gate if (sip != NULL) { 9620Sstevel@tonic-gate error = ENOMEM; 9630Sstevel@tonic-gate goto lout; 9640Sstevel@tonic-gate } 9650Sstevel@tonic-gate ust = (swapent32_t *)((swaptbl32_t *)sc_arg)->swt_ent; 9660Sstevel@tonic-gate for (tsip = csip, cnt = 0; cnt < nswap; tsip++, ust++, cnt++) { 9670Sstevel@tonic-gate if (copyin(ust, &st, sizeof (*ust)) != 0) { 9680Sstevel@tonic-gate error = EFAULT; 9690Sstevel@tonic-gate goto lout; 9700Sstevel@tonic-gate } 9710Sstevel@tonic-gate st.ste_flags = tsip->si_flags; 9720Sstevel@tonic-gate st.ste_length = 9730Sstevel@tonic-gate (tsip->si_eoff - tsip->si_soff) >> SCTRSHFT; 9740Sstevel@tonic-gate st.ste_start = tsip->si_soff >> SCTRSHFT; 9750Sstevel@tonic-gate st.ste_pages = tsip->si_npgs; 9760Sstevel@tonic-gate st.ste_free = tsip->si_nfpgs; 9770Sstevel@tonic-gate if (copyout(&st, ust, sizeof (st)) != 0) { 9780Sstevel@tonic-gate error = EFAULT; 9790Sstevel@tonic-gate goto lout; 9800Sstevel@tonic-gate } 9810Sstevel@tonic-gate if (!tsip->si_pnamelen) 9820Sstevel@tonic-gate continue; 9830Sstevel@tonic-gate if (copyout(tsip->si_pname, 9840Sstevel@tonic-gate (caddr_t)(uintptr_t)st.ste_path, 9850Sstevel@tonic-gate tsip->si_pnamelen) != 0) { 9860Sstevel@tonic-gate error = EFAULT; 9870Sstevel@tonic-gate goto lout; 9880Sstevel@tonic-gate } 9890Sstevel@tonic-gate } 9900Sstevel@tonic-gate *rv = nswap; 9910Sstevel@tonic-gate lout: 9920Sstevel@tonic-gate kmem_free(csip, tmp_nswapfiles * sizeof (*csip)); 9930Sstevel@tonic-gate kmem_free(pname, nlen); 9940Sstevel@tonic-gate return (error); 9950Sstevel@tonic-gate 9960Sstevel@tonic-gate case SC_ADD: 9970Sstevel@tonic-gate case SC_REMOVE: 9980Sstevel@tonic-gate break; 9990Sstevel@tonic-gate default: 10000Sstevel@tonic-gate return (EINVAL); 10010Sstevel@tonic-gate } 10020Sstevel@tonic-gate if ((error = secpolicy_swapctl(CRED())) != 0) 10030Sstevel@tonic-gate return (error); 10040Sstevel@tonic-gate 10050Sstevel@tonic-gate if (copyin(sc_arg, &sr, sizeof (sr))) 10060Sstevel@tonic-gate return (EFAULT); 10070Sstevel@tonic-gate 10080Sstevel@tonic-gate /* Allocate the space to read in pathname */ 10090Sstevel@tonic-gate if ((swapname = kmem_alloc(MAXPATHLEN, KM_NOSLEEP)) == NULL) 10100Sstevel@tonic-gate return (ENOMEM); 10110Sstevel@tonic-gate 10120Sstevel@tonic-gate error = copyinstr((caddr_t)(uintptr_t)sr.sr_name, 10130Sstevel@tonic-gate swapname, MAXPATHLEN, NULL); 10140Sstevel@tonic-gate if (error) 10150Sstevel@tonic-gate goto out; 10160Sstevel@tonic-gate 10170Sstevel@tonic-gate error = lookupname(swapname, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp); 10180Sstevel@tonic-gate if (error) { 10190Sstevel@tonic-gate if (sc_cmd == SC_ADD) 10200Sstevel@tonic-gate goto out; 10210Sstevel@tonic-gate /* see if we match by name */ 10220Sstevel@tonic-gate vp = swapdel_byname(swapname, (uint_t)sr.sr_start); 10230Sstevel@tonic-gate if (vp == NULL) 10240Sstevel@tonic-gate goto out; 10250Sstevel@tonic-gate } 10260Sstevel@tonic-gate 10270Sstevel@tonic-gate if (vp->v_flag & (VNOMAP | VNOSWAP)) { 10280Sstevel@tonic-gate VN_RELE(vp); 10290Sstevel@tonic-gate error = ENOSYS; 10300Sstevel@tonic-gate goto out; 10310Sstevel@tonic-gate } 10320Sstevel@tonic-gate switch (vp->v_type) { 10330Sstevel@tonic-gate case VBLK: 10340Sstevel@tonic-gate break; 10350Sstevel@tonic-gate 10360Sstevel@tonic-gate case VREG: 10370Sstevel@tonic-gate if (vp->v_vfsp && vn_is_readonly(vp)) 10380Sstevel@tonic-gate error = EROFS; 10390Sstevel@tonic-gate else 10405331Samw error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL); 10410Sstevel@tonic-gate break; 10420Sstevel@tonic-gate 10430Sstevel@tonic-gate case VDIR: 10440Sstevel@tonic-gate error = EISDIR; 10450Sstevel@tonic-gate break; 10460Sstevel@tonic-gate default: 10470Sstevel@tonic-gate error = ENOSYS; 10480Sstevel@tonic-gate break; 10490Sstevel@tonic-gate } 10500Sstevel@tonic-gate if (error == 0) { 10510Sstevel@tonic-gate if (sc_cmd == SC_REMOVE) 10520Sstevel@tonic-gate error = swapdel(vp, sr.sr_start); 10530Sstevel@tonic-gate else 10540Sstevel@tonic-gate error = swapadd(vp, sr.sr_start, sr.sr_length, 10550Sstevel@tonic-gate swapname); 10560Sstevel@tonic-gate } 10570Sstevel@tonic-gate VN_RELE(vp); 10580Sstevel@tonic-gate out: 10590Sstevel@tonic-gate kmem_free(swapname, MAXPATHLEN); 10600Sstevel@tonic-gate return (error); 10610Sstevel@tonic-gate } 10620Sstevel@tonic-gate 10630Sstevel@tonic-gate #endif /* _LP64 && _SYSCALL32 */ 10640Sstevel@tonic-gate 10650Sstevel@tonic-gate /* 10660Sstevel@tonic-gate * Add a new swap file. 10670Sstevel@tonic-gate */ 10680Sstevel@tonic-gate int 10690Sstevel@tonic-gate swapadd(struct vnode *vp, ulong_t lowblk, ulong_t nblks, char *swapname) 10700Sstevel@tonic-gate { 10710Sstevel@tonic-gate struct swapinfo **sipp, *nsip = NULL, *esip = NULL; 10720Sstevel@tonic-gate struct vnode *cvp; 10730Sstevel@tonic-gate struct vattr vattr; 10740Sstevel@tonic-gate pgcnt_t pages; 10750Sstevel@tonic-gate u_offset_t soff, eoff; 10760Sstevel@tonic-gate int error; 10770Sstevel@tonic-gate ssize_t i, start, end; 10780Sstevel@tonic-gate ushort_t wasswap; 10790Sstevel@tonic-gate ulong_t startblk; 10800Sstevel@tonic-gate size_t returned_mem; 10810Sstevel@tonic-gate 10820Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: vp %p lowblk %ld nblks %ld swapname %s\n", 10830Sstevel@tonic-gate vp, lowblk, nblks, swapname, 0); 10840Sstevel@tonic-gate /* 10850Sstevel@tonic-gate * Get the real vnode. (If vp is not a specnode it just returns vp, so 10860Sstevel@tonic-gate * it does the right thing, but having this code know about specnodes 10870Sstevel@tonic-gate * violates the spirit of having it be indepedent of vnode type.) 10880Sstevel@tonic-gate */ 10890Sstevel@tonic-gate cvp = common_specvp(vp); 10900Sstevel@tonic-gate 10910Sstevel@tonic-gate /* 10920Sstevel@tonic-gate * Or in VISSWAP so file system has chance to deny swap-ons during open. 10930Sstevel@tonic-gate */ 10940Sstevel@tonic-gate mutex_enter(&cvp->v_lock); 10950Sstevel@tonic-gate wasswap = cvp->v_flag & VISSWAP; 10960Sstevel@tonic-gate cvp->v_flag |= VISSWAP; 10970Sstevel@tonic-gate mutex_exit(&cvp->v_lock); 10980Sstevel@tonic-gate 10990Sstevel@tonic-gate mutex_enter(&swap_lock); 11005331Samw if (error = VOP_OPEN(&cvp, FREAD|FWRITE, CRED(), NULL)) { 11010Sstevel@tonic-gate mutex_exit(&swap_lock); 11020Sstevel@tonic-gate /* restore state of v_flag */ 11030Sstevel@tonic-gate if (!wasswap) { 11040Sstevel@tonic-gate mutex_enter(&cvp->v_lock); 11050Sstevel@tonic-gate cvp->v_flag &= ~VISSWAP; 11060Sstevel@tonic-gate mutex_exit(&cvp->v_lock); 11070Sstevel@tonic-gate } 11080Sstevel@tonic-gate return (error); 11090Sstevel@tonic-gate } 11100Sstevel@tonic-gate mutex_exit(&swap_lock); 11110Sstevel@tonic-gate 11120Sstevel@tonic-gate /* 11130Sstevel@tonic-gate * Get partition size. Return error if empty partition, 11140Sstevel@tonic-gate * or if request does not fit within the partition. 11150Sstevel@tonic-gate * If this is the first swap device, we can reduce 11160Sstevel@tonic-gate * the size of the swap area to match what is 11170Sstevel@tonic-gate * available. This can happen if the system was built 11180Sstevel@tonic-gate * on a machine with a different size swap partition. 11190Sstevel@tonic-gate */ 11200Sstevel@tonic-gate vattr.va_mask = AT_SIZE; 11215331Samw if (error = VOP_GETATTR(cvp, &vattr, ATTR_COMM, CRED(), NULL)) 11220Sstevel@tonic-gate goto out; 11230Sstevel@tonic-gate 11240Sstevel@tonic-gate /* 11250Sstevel@tonic-gate * Specfs returns a va_size of MAXOFFSET_T (UNKNOWN_SIZE) when the 11260Sstevel@tonic-gate * size of the device can't be determined. 11270Sstevel@tonic-gate */ 11280Sstevel@tonic-gate if ((vattr.va_size == 0) || (vattr.va_size == MAXOFFSET_T)) { 11290Sstevel@tonic-gate error = EINVAL; 11300Sstevel@tonic-gate goto out; 11310Sstevel@tonic-gate } 11320Sstevel@tonic-gate 11330Sstevel@tonic-gate #ifdef _ILP32 11340Sstevel@tonic-gate /* 11350Sstevel@tonic-gate * No support for large swap in 32-bit OS, if the size of the swap is 11360Sstevel@tonic-gate * bigger than MAXOFF32_T then the size used by swapfs must be limited. 11370Sstevel@tonic-gate * This limitation is imposed by the swap subsystem itself, a D_64BIT 11380Sstevel@tonic-gate * driver as the target of swap operation should be able to field 11390Sstevel@tonic-gate * the IO. 11400Sstevel@tonic-gate */ 11410Sstevel@tonic-gate if (vattr.va_size > MAXOFF32_T) { 11420Sstevel@tonic-gate cmn_err(CE_NOTE, 11435665Sstans "!swap device %s truncated from 0x%llx to 0x%x bytes", 11445665Sstans swapname, vattr.va_size, MAXOFF32_T); 11450Sstevel@tonic-gate vattr.va_size = MAXOFF32_T; 11460Sstevel@tonic-gate } 11470Sstevel@tonic-gate #endif /* _ILP32 */ 11480Sstevel@tonic-gate 11490Sstevel@tonic-gate /* Fail if file not writeable (try to set size to current size) */ 11500Sstevel@tonic-gate vattr.va_mask = AT_SIZE; 11510Sstevel@tonic-gate if (error = VOP_SETATTR(cvp, &vattr, 0, CRED(), NULL)) 11520Sstevel@tonic-gate goto out; 11530Sstevel@tonic-gate 11540Sstevel@tonic-gate /* Fail if fs does not support VOP_PAGEIO */ 11555331Samw error = VOP_PAGEIO(cvp, (page_t *)NULL, (u_offset_t)0, 0, 0, CRED(), 11565665Sstans NULL); 11570Sstevel@tonic-gate 11580Sstevel@tonic-gate if (error == ENOSYS) 11590Sstevel@tonic-gate goto out; 11600Sstevel@tonic-gate else 11610Sstevel@tonic-gate error = 0; 11620Sstevel@tonic-gate /* 11630Sstevel@tonic-gate * If swapping on the root filesystem don't put swap blocks that 11640Sstevel@tonic-gate * correspond to the miniroot filesystem on the swap free list. 11650Sstevel@tonic-gate */ 11660Sstevel@tonic-gate if (cvp == rootdir) 11670Sstevel@tonic-gate startblk = roundup(MINIROOTSIZE<<SCTRSHFT, klustsize)>>SCTRSHFT; 11680Sstevel@tonic-gate else /* Skip 1st page (disk label) */ 11690Sstevel@tonic-gate startblk = (ulong_t)(lowblk ? lowblk : 1); 11700Sstevel@tonic-gate 11710Sstevel@tonic-gate soff = startblk << SCTRSHFT; 11720Sstevel@tonic-gate if (soff >= vattr.va_size) { 11730Sstevel@tonic-gate error = EINVAL; 11740Sstevel@tonic-gate goto out; 11750Sstevel@tonic-gate } 11760Sstevel@tonic-gate 11770Sstevel@tonic-gate /* 11780Sstevel@tonic-gate * If user specified 0 blks, use the size of the device 11790Sstevel@tonic-gate */ 11800Sstevel@tonic-gate eoff = nblks ? soff + (nblks - (startblk - lowblk) << SCTRSHFT) : 11815665Sstans vattr.va_size; 11820Sstevel@tonic-gate 11830Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: va_size %ld soff %ld eoff %ld\n", 11840Sstevel@tonic-gate vattr.va_size, soff, eoff, 0, 0); 11850Sstevel@tonic-gate 11860Sstevel@tonic-gate if (eoff > vattr.va_size) { 11870Sstevel@tonic-gate error = EINVAL; 11880Sstevel@tonic-gate goto out; 11890Sstevel@tonic-gate } 11900Sstevel@tonic-gate 11910Sstevel@tonic-gate /* 11920Sstevel@tonic-gate * The starting and ending offsets must be page aligned. 11930Sstevel@tonic-gate * Round soff up to next page boundary, round eoff 11940Sstevel@tonic-gate * down to previous page boundary. 11950Sstevel@tonic-gate */ 11960Sstevel@tonic-gate soff = ptob(btopr(soff)); 11970Sstevel@tonic-gate eoff = ptob(btop(eoff)); 11980Sstevel@tonic-gate if (soff >= eoff) { 11990Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: soff %ld >= eoff %ld\n", 12000Sstevel@tonic-gate soff, eoff, 0, 0, 0); 12010Sstevel@tonic-gate error = EINVAL; 12020Sstevel@tonic-gate goto out; 12030Sstevel@tonic-gate } 12040Sstevel@tonic-gate 12050Sstevel@tonic-gate pages = btop(eoff - soff); 12060Sstevel@tonic-gate 12070Sstevel@tonic-gate /* Allocate and partially set up the new swapinfo */ 12080Sstevel@tonic-gate nsip = kmem_zalloc(sizeof (struct swapinfo), KM_SLEEP); 12090Sstevel@tonic-gate nsip->si_vp = cvp; 12100Sstevel@tonic-gate 12110Sstevel@tonic-gate nsip->si_soff = soff; 12120Sstevel@tonic-gate nsip->si_eoff = eoff; 12130Sstevel@tonic-gate nsip->si_hint = 0; 12140Sstevel@tonic-gate nsip->si_checkcnt = nsip->si_alloccnt = 0; 12150Sstevel@tonic-gate 12160Sstevel@tonic-gate nsip->si_pnamelen = (int)strlen(swapname) + 1; 12170Sstevel@tonic-gate nsip->si_pname = (char *)kmem_zalloc(nsip->si_pnamelen, KM_SLEEP); 12180Sstevel@tonic-gate bcopy(swapname, nsip->si_pname, nsip->si_pnamelen - 1); 12190Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: allocating swapinfo for %s, %ld pages\n", 12200Sstevel@tonic-gate swapname, pages, 0, 0, 0); 12210Sstevel@tonic-gate /* 12220Sstevel@tonic-gate * Size of swapslots map in bytes 12230Sstevel@tonic-gate */ 12240Sstevel@tonic-gate nsip->si_mapsize = P2ROUNDUP(pages, NBBW) / NBBY; 12250Sstevel@tonic-gate nsip->si_swapslots = kmem_zalloc(nsip->si_mapsize, KM_SLEEP); 12260Sstevel@tonic-gate 12270Sstevel@tonic-gate /* 12280Sstevel@tonic-gate * Permanently set the bits that can't ever be allocated, 12290Sstevel@tonic-gate * i.e. those from the ending offset to the round up slot for the 12300Sstevel@tonic-gate * swapslots bit map. 12310Sstevel@tonic-gate */ 12320Sstevel@tonic-gate start = pages; 12330Sstevel@tonic-gate end = P2ROUNDUP(pages, NBBW); 12340Sstevel@tonic-gate for (i = start; i < end; i++) { 12350Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: set bit for page %ld\n", i, 12360Sstevel@tonic-gate 0, 0, 0, 0); 12370Sstevel@tonic-gate SETBIT(nsip->si_swapslots, i); 12380Sstevel@tonic-gate } 12390Sstevel@tonic-gate nsip->si_npgs = nsip->si_nfpgs = pages; 12400Sstevel@tonic-gate /* 12410Sstevel@tonic-gate * Now check to see if we can add it. We wait til now to check because 12420Sstevel@tonic-gate * we need the swapinfo_lock and we don't want sleep with it (e.g., 12430Sstevel@tonic-gate * during kmem_alloc()) while we're setting up the swapinfo. 12440Sstevel@tonic-gate */ 12450Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 12460Sstevel@tonic-gate for (sipp = &swapinfo; (esip = *sipp) != NULL; sipp = &esip->si_next) { 12470Sstevel@tonic-gate if (esip->si_vp == cvp) { 12480Sstevel@tonic-gate if (esip->si_soff == soff && esip->si_npgs == pages && 12490Sstevel@tonic-gate (esip->si_flags & ST_DOINGDEL)) { 12500Sstevel@tonic-gate /* 12510Sstevel@tonic-gate * We are adding a device that we are in the 12520Sstevel@tonic-gate * middle of deleting. Just clear the 12530Sstevel@tonic-gate * ST_DOINGDEL flag to signal this and 12540Sstevel@tonic-gate * the deletion routine will eventually notice 12550Sstevel@tonic-gate * it and add it back. 12560Sstevel@tonic-gate */ 12570Sstevel@tonic-gate esip->si_flags &= ~ST_DOINGDEL; 12580Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 12590Sstevel@tonic-gate goto out; 12600Sstevel@tonic-gate } 12610Sstevel@tonic-gate /* disallow overlapping swap files */ 12620Sstevel@tonic-gate if ((soff < esip->si_eoff) && (eoff > esip->si_soff)) { 12630Sstevel@tonic-gate error = EEXIST; 12640Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 12650Sstevel@tonic-gate goto out; 12660Sstevel@tonic-gate } 12670Sstevel@tonic-gate } 12680Sstevel@tonic-gate } 12690Sstevel@tonic-gate 12700Sstevel@tonic-gate nswapfiles++; 12710Sstevel@tonic-gate 12720Sstevel@tonic-gate /* 12730Sstevel@tonic-gate * add new swap device to list and shift allocations to it 12740Sstevel@tonic-gate * before updating the anoninfo counters 12750Sstevel@tonic-gate */ 12760Sstevel@tonic-gate *sipp = nsip; 12770Sstevel@tonic-gate silast = nsip; 12780Sstevel@tonic-gate 12790Sstevel@tonic-gate /* 12800Sstevel@tonic-gate * Update the total amount of reservable swap space 12810Sstevel@tonic-gate * accounting properly for swap space from physical memory 12820Sstevel@tonic-gate */ 12830Sstevel@tonic-gate /* New swap device soaks up currently reserved memory swap */ 12840Sstevel@tonic-gate mutex_enter(&anoninfo_lock); 12850Sstevel@tonic-gate 12860Sstevel@tonic-gate ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap); 12870Sstevel@tonic-gate ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv); 12880Sstevel@tonic-gate 12890Sstevel@tonic-gate k_anoninfo.ani_max += pages; 12900Sstevel@tonic-gate ANI_ADD(pages); 12910Sstevel@tonic-gate if (k_anoninfo.ani_mem_resv > k_anoninfo.ani_locked_swap) { 12920Sstevel@tonic-gate returned_mem = MIN(k_anoninfo.ani_mem_resv - 12930Sstevel@tonic-gate k_anoninfo.ani_locked_swap, 12940Sstevel@tonic-gate k_anoninfo.ani_max - k_anoninfo.ani_phys_resv); 12950Sstevel@tonic-gate 12960Sstevel@tonic-gate ANI_ADD(-returned_mem); 12970Sstevel@tonic-gate k_anoninfo.ani_free -= returned_mem; 12980Sstevel@tonic-gate k_anoninfo.ani_mem_resv -= returned_mem; 12990Sstevel@tonic-gate k_anoninfo.ani_phys_resv += returned_mem; 13000Sstevel@tonic-gate 13010Sstevel@tonic-gate mutex_enter(&freemem_lock); 13020Sstevel@tonic-gate availrmem += returned_mem; 13030Sstevel@tonic-gate mutex_exit(&freemem_lock); 13040Sstevel@tonic-gate } 13050Sstevel@tonic-gate /* 13060Sstevel@tonic-gate * At boot time, to permit booting small memory machines using 13070Sstevel@tonic-gate * only physical memory as swap space, we allowed a dangerously 13080Sstevel@tonic-gate * large amount of memory to be used as swap space; now that 13090Sstevel@tonic-gate * more physical backing store is available bump down the amount 13100Sstevel@tonic-gate * we can get from memory to a safer size. 13110Sstevel@tonic-gate */ 13120Sstevel@tonic-gate if (swapfs_minfree < swapfs_desfree) { 13130Sstevel@tonic-gate mutex_enter(&freemem_lock); 13140Sstevel@tonic-gate if (availrmem > swapfs_desfree || !k_anoninfo.ani_mem_resv) 13150Sstevel@tonic-gate swapfs_minfree = swapfs_desfree; 13160Sstevel@tonic-gate mutex_exit(&freemem_lock); 13170Sstevel@tonic-gate } 13180Sstevel@tonic-gate 13190Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: ani_max %ld ani_free %ld\n", 13200Sstevel@tonic-gate k_anoninfo.ani_free, k_anoninfo.ani_free, 0, 0, 0); 13210Sstevel@tonic-gate 13220Sstevel@tonic-gate mutex_exit(&anoninfo_lock); 13230Sstevel@tonic-gate 13240Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 13250Sstevel@tonic-gate 13260Sstevel@tonic-gate /* Initialize the dump device */ 13270Sstevel@tonic-gate mutex_enter(&dump_lock); 13280Sstevel@tonic-gate if (dumpvp == NULL) 13290Sstevel@tonic-gate (void) dumpinit(vp, swapname, 0); 13300Sstevel@tonic-gate mutex_exit(&dump_lock); 13310Sstevel@tonic-gate 13320Sstevel@tonic-gate VN_HOLD(cvp); 13330Sstevel@tonic-gate out: 13340Sstevel@tonic-gate if (error || esip) { 13350Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: error (%d)\n", error, 0, 0, 0, 0); 13360Sstevel@tonic-gate 13370Sstevel@tonic-gate if (!wasswap) { 13380Sstevel@tonic-gate mutex_enter(&cvp->v_lock); 13390Sstevel@tonic-gate cvp->v_flag &= ~VISSWAP; 13400Sstevel@tonic-gate mutex_exit(&cvp->v_lock); 13410Sstevel@tonic-gate } 13420Sstevel@tonic-gate if (nsip) { 13430Sstevel@tonic-gate kmem_free(nsip->si_swapslots, (size_t)nsip->si_mapsize); 13440Sstevel@tonic-gate kmem_free(nsip->si_pname, nsip->si_pnamelen); 13450Sstevel@tonic-gate kmem_free(nsip, sizeof (*nsip)); 13460Sstevel@tonic-gate } 13470Sstevel@tonic-gate mutex_enter(&swap_lock); 13485331Samw (void) VOP_CLOSE(cvp, FREAD|FWRITE, 1, (offset_t)0, CRED(), 13495665Sstans NULL); 13500Sstevel@tonic-gate mutex_exit(&swap_lock); 13510Sstevel@tonic-gate } 13520Sstevel@tonic-gate return (error); 13530Sstevel@tonic-gate } 13540Sstevel@tonic-gate 13550Sstevel@tonic-gate /* 13560Sstevel@tonic-gate * Delete a swap file. 13570Sstevel@tonic-gate */ 13580Sstevel@tonic-gate static int 13590Sstevel@tonic-gate swapdel( 13600Sstevel@tonic-gate struct vnode *vp, 13610Sstevel@tonic-gate ulong_t lowblk) /* Low block number of area to delete. */ 13620Sstevel@tonic-gate { 13630Sstevel@tonic-gate struct swapinfo **sipp, *osip = NULL; 13640Sstevel@tonic-gate struct vnode *cvp; 13650Sstevel@tonic-gate u_offset_t soff; 13660Sstevel@tonic-gate int error = 0; 13670Sstevel@tonic-gate u_offset_t toff = 0; 13680Sstevel@tonic-gate struct vnode *tvp = NULL; 13690Sstevel@tonic-gate spgcnt_t pages; 13700Sstevel@tonic-gate struct anon **app, *ap; 13710Sstevel@tonic-gate kmutex_t *ahm; 13720Sstevel@tonic-gate pgcnt_t adjust_swap = 0; 13730Sstevel@tonic-gate 13740Sstevel@tonic-gate /* Find the swap file entry for the file to be deleted */ 13750Sstevel@tonic-gate cvp = common_specvp(vp); 13760Sstevel@tonic-gate 13770Sstevel@tonic-gate 13780Sstevel@tonic-gate lowblk = lowblk ? lowblk : 1; /* Skip first page (disk label) */ 13790Sstevel@tonic-gate soff = ptob(btopr(lowblk << SCTRSHFT)); /* must be page aligned */ 13800Sstevel@tonic-gate 13810Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 13820Sstevel@tonic-gate for (sipp = &swapinfo; (osip = *sipp) != NULL; sipp = &osip->si_next) { 13830Sstevel@tonic-gate if ((osip->si_vp == cvp) && 13840Sstevel@tonic-gate (osip->si_soff == soff) && (osip->si_flags == 0)) 13850Sstevel@tonic-gate break; 13860Sstevel@tonic-gate } 13870Sstevel@tonic-gate 13880Sstevel@tonic-gate /* If the file was not found, error. */ 13890Sstevel@tonic-gate if (osip == NULL) { 13900Sstevel@tonic-gate error = EINVAL; 13910Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 13920Sstevel@tonic-gate goto out; 13930Sstevel@tonic-gate } 13940Sstevel@tonic-gate 13950Sstevel@tonic-gate pages = osip->si_npgs; 13960Sstevel@tonic-gate 13970Sstevel@tonic-gate /* 13980Sstevel@tonic-gate * Do not delete if we will be low on swap pages. 13990Sstevel@tonic-gate */ 14000Sstevel@tonic-gate mutex_enter(&anoninfo_lock); 14010Sstevel@tonic-gate 14020Sstevel@tonic-gate ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap); 14030Sstevel@tonic-gate ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv); 14040Sstevel@tonic-gate 14050Sstevel@tonic-gate mutex_enter(&freemem_lock); 14060Sstevel@tonic-gate if (((k_anoninfo.ani_max - k_anoninfo.ani_phys_resv) + 14070Sstevel@tonic-gate MAX((spgcnt_t)(availrmem - swapfs_minfree), 0)) < pages) { 14080Sstevel@tonic-gate mutex_exit(&freemem_lock); 14090Sstevel@tonic-gate mutex_exit(&anoninfo_lock); 14100Sstevel@tonic-gate error = ENOMEM; 14110Sstevel@tonic-gate cmn_err(CE_WARN, "swapdel - too few free pages"); 14120Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 14130Sstevel@tonic-gate goto out; 14140Sstevel@tonic-gate } 14150Sstevel@tonic-gate mutex_exit(&freemem_lock); 14160Sstevel@tonic-gate 14170Sstevel@tonic-gate k_anoninfo.ani_max -= pages; 14180Sstevel@tonic-gate 14190Sstevel@tonic-gate /* If needed, reserve memory swap to replace old device */ 14200Sstevel@tonic-gate if (k_anoninfo.ani_phys_resv > k_anoninfo.ani_max) { 14210Sstevel@tonic-gate adjust_swap = k_anoninfo.ani_phys_resv - k_anoninfo.ani_max; 14220Sstevel@tonic-gate k_anoninfo.ani_phys_resv -= adjust_swap; 14230Sstevel@tonic-gate k_anoninfo.ani_mem_resv += adjust_swap; 14240Sstevel@tonic-gate mutex_enter(&freemem_lock); 14250Sstevel@tonic-gate availrmem -= adjust_swap; 14260Sstevel@tonic-gate mutex_exit(&freemem_lock); 14270Sstevel@tonic-gate ANI_ADD(adjust_swap); 14280Sstevel@tonic-gate } 14290Sstevel@tonic-gate ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap); 14300Sstevel@tonic-gate ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv); 14310Sstevel@tonic-gate mutex_exit(&anoninfo_lock); 14320Sstevel@tonic-gate 14330Sstevel@tonic-gate ANI_ADD(-pages); 14340Sstevel@tonic-gate 14350Sstevel@tonic-gate /* 14360Sstevel@tonic-gate * Set the delete flag. This prevents anyone from allocating more 14370Sstevel@tonic-gate * pages from this file. Also set ST_DOINGDEL. Someone who wants to 14380Sstevel@tonic-gate * add the file back while we're deleting it will signify by clearing 14390Sstevel@tonic-gate * this flag. 14400Sstevel@tonic-gate */ 14410Sstevel@tonic-gate osip->si_flags |= ST_INDEL|ST_DOINGDEL; 14420Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 14430Sstevel@tonic-gate 14440Sstevel@tonic-gate /* 14450Sstevel@tonic-gate * Free all the allocated physical slots for this file. We do this 14460Sstevel@tonic-gate * by walking through the entire anon hash array, because we need 14470Sstevel@tonic-gate * to update all the anon slots that have physical swap slots on 14480Sstevel@tonic-gate * this file, and this is the only way to find them all. We go back 14490Sstevel@tonic-gate * to the beginning of a bucket after each slot is freed because the 14500Sstevel@tonic-gate * anonhash_lock is not held during the free and thus the hash table 14510Sstevel@tonic-gate * may change under us. 14520Sstevel@tonic-gate */ 14530Sstevel@tonic-gate for (app = anon_hash; app < &anon_hash[ANON_HASH_SIZE]; app++) { 1454*12173SMichael.Corcoran@Sun.COM ahm = &anonhash_lock[(app - anon_hash) & 1455*12173SMichael.Corcoran@Sun.COM (AH_LOCK_SIZE - 1)].pad_mutex; 14560Sstevel@tonic-gate mutex_enter(ahm); 14570Sstevel@tonic-gate top: 14580Sstevel@tonic-gate for (ap = *app; ap != NULL; ap = ap->an_hash) { 14590Sstevel@tonic-gate if (ap->an_pvp == cvp && 14600Sstevel@tonic-gate ap->an_poff >= osip->si_soff && 14610Sstevel@tonic-gate ap->an_poff < osip->si_eoff) { 14620Sstevel@tonic-gate ASSERT(TESTBIT(osip->si_swapslots, 14630Sstevel@tonic-gate btop((size_t)(ap->an_poff - 14640Sstevel@tonic-gate osip->si_soff)))); 14650Sstevel@tonic-gate tvp = ap->an_vp; 14660Sstevel@tonic-gate toff = ap->an_off; 14670Sstevel@tonic-gate VN_HOLD(tvp); 14680Sstevel@tonic-gate mutex_exit(ahm); 14690Sstevel@tonic-gate 14700Sstevel@tonic-gate error = swapslot_free(tvp, toff, osip); 14710Sstevel@tonic-gate 14720Sstevel@tonic-gate VN_RELE(tvp); 14730Sstevel@tonic-gate mutex_enter(ahm); 14740Sstevel@tonic-gate if (!error && (osip->si_flags & ST_DOINGDEL)) { 14750Sstevel@tonic-gate goto top; 14760Sstevel@tonic-gate } else { 14770Sstevel@tonic-gate if (error) { 14780Sstevel@tonic-gate cmn_err(CE_WARN, 14790Sstevel@tonic-gate "swapslot_free failed %d", 14800Sstevel@tonic-gate error); 14810Sstevel@tonic-gate } 14820Sstevel@tonic-gate 14830Sstevel@tonic-gate /* 14840Sstevel@tonic-gate * Add device back before making it 14850Sstevel@tonic-gate * visible. 14860Sstevel@tonic-gate */ 14870Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 14880Sstevel@tonic-gate osip->si_flags &= 14890Sstevel@tonic-gate ~(ST_INDEL | ST_DOINGDEL); 14900Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 14910Sstevel@tonic-gate 14920Sstevel@tonic-gate /* 14930Sstevel@tonic-gate * Update the anon space available 14940Sstevel@tonic-gate */ 14950Sstevel@tonic-gate mutex_enter(&anoninfo_lock); 14960Sstevel@tonic-gate 14970Sstevel@tonic-gate k_anoninfo.ani_phys_resv += adjust_swap; 14980Sstevel@tonic-gate k_anoninfo.ani_mem_resv -= adjust_swap; 14990Sstevel@tonic-gate k_anoninfo.ani_max += pages; 15000Sstevel@tonic-gate 15010Sstevel@tonic-gate mutex_enter(&freemem_lock); 15020Sstevel@tonic-gate availrmem += adjust_swap; 15030Sstevel@tonic-gate mutex_exit(&freemem_lock); 15040Sstevel@tonic-gate 15050Sstevel@tonic-gate mutex_exit(&anoninfo_lock); 15060Sstevel@tonic-gate 15070Sstevel@tonic-gate ANI_ADD(pages); 15080Sstevel@tonic-gate 15090Sstevel@tonic-gate mutex_exit(ahm); 15100Sstevel@tonic-gate goto out; 15110Sstevel@tonic-gate } 15120Sstevel@tonic-gate } 15130Sstevel@tonic-gate } 15140Sstevel@tonic-gate mutex_exit(ahm); 15150Sstevel@tonic-gate } 15160Sstevel@tonic-gate 15170Sstevel@tonic-gate /* All done, they'd better all be free! */ 15180Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 15190Sstevel@tonic-gate ASSERT(osip->si_nfpgs == osip->si_npgs); 15200Sstevel@tonic-gate 15210Sstevel@tonic-gate /* Now remove it from the swapinfo list */ 15220Sstevel@tonic-gate for (sipp = &swapinfo; *sipp != NULL; sipp = &(*sipp)->si_next) { 15230Sstevel@tonic-gate if (*sipp == osip) 15240Sstevel@tonic-gate break; 15250Sstevel@tonic-gate } 15260Sstevel@tonic-gate ASSERT(*sipp); 15270Sstevel@tonic-gate *sipp = osip->si_next; 15280Sstevel@tonic-gate if (silast == osip) 15290Sstevel@tonic-gate if ((silast = osip->si_next) == NULL) 15300Sstevel@tonic-gate silast = swapinfo; 15310Sstevel@tonic-gate nswapfiles--; 15320Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 15330Sstevel@tonic-gate 15340Sstevel@tonic-gate kmem_free(osip->si_swapslots, osip->si_mapsize); 15350Sstevel@tonic-gate kmem_free(osip->si_pname, osip->si_pnamelen); 15360Sstevel@tonic-gate kmem_free(osip, sizeof (*osip)); 15370Sstevel@tonic-gate 15380Sstevel@tonic-gate mutex_enter(&dump_lock); 15390Sstevel@tonic-gate if (cvp == dumpvp) 15400Sstevel@tonic-gate dumpfini(); 15410Sstevel@tonic-gate mutex_exit(&dump_lock); 15420Sstevel@tonic-gate 15430Sstevel@tonic-gate /* Release the vnode */ 15440Sstevel@tonic-gate 15450Sstevel@tonic-gate mutex_enter(&swap_lock); 15465331Samw (void) VOP_CLOSE(cvp, FREAD|FWRITE, 1, (offset_t)0, CRED(), NULL); 15470Sstevel@tonic-gate mutex_enter(&cvp->v_lock); 15480Sstevel@tonic-gate cvp->v_flag &= ~VISSWAP; 15490Sstevel@tonic-gate mutex_exit(&cvp->v_lock); 15500Sstevel@tonic-gate VN_RELE(cvp); 15510Sstevel@tonic-gate mutex_exit(&swap_lock); 15520Sstevel@tonic-gate out: 15530Sstevel@tonic-gate return (error); 15540Sstevel@tonic-gate } 15550Sstevel@tonic-gate 15560Sstevel@tonic-gate /* 15570Sstevel@tonic-gate * Free up a physical swap slot on swapinfo sip, currently in use by the 15580Sstevel@tonic-gate * anonymous page whose name is (vp, off). 15590Sstevel@tonic-gate */ 15600Sstevel@tonic-gate static int 15610Sstevel@tonic-gate swapslot_free( 15620Sstevel@tonic-gate struct vnode *vp, 15630Sstevel@tonic-gate u_offset_t off, 15640Sstevel@tonic-gate struct swapinfo *sip) 15650Sstevel@tonic-gate { 15665665Sstans struct page *pp = NULL; 15670Sstevel@tonic-gate struct anon *ap = NULL; 15680Sstevel@tonic-gate int error = 0; 15690Sstevel@tonic-gate kmutex_t *ahm; 15705665Sstans struct vnode *pvp = NULL; 15715665Sstans u_offset_t poff; 15725665Sstans int alloc_pg = 0; 15735665Sstans 15745665Sstans ASSERT(sip->si_vp != NULL); 15755665Sstans /* 15765665Sstans * Get the page for the old swap slot if exists or create a new one. 15775665Sstans */ 15785665Sstans again: 15795665Sstans if ((pp = page_lookup(vp, off, SE_SHARED)) == NULL) { 15805665Sstans pp = page_create_va(vp, off, PAGESIZE, PG_WAIT | PG_EXCL, 15815665Sstans segkmap, NULL); 15825665Sstans if (pp == NULL) 15835665Sstans goto again; 15845665Sstans alloc_pg = 1; 15855665Sstans 15865665Sstans error = swap_getphysname(vp, off, &pvp, &poff); 15875665Sstans if (error || pvp != sip->si_vp || poff < sip->si_soff || 15885665Sstans poff >= sip->si_eoff) { 15895665Sstans page_io_unlock(pp); 15905665Sstans /*LINTED: constant in conditional context*/ 15915665Sstans VN_DISPOSE(pp, B_INVAL, 0, kcred); 15925665Sstans return (0); 15935665Sstans } 15945665Sstans 15955665Sstans error = VOP_PAGEIO(pvp, pp, poff, PAGESIZE, B_READ, 15965665Sstans CRED(), NULL); 15975665Sstans if (error) { 15985665Sstans page_io_unlock(pp); 15995665Sstans if (error == EFAULT) 16005665Sstans error = 0; 16015665Sstans /*LINTED: constant in conditional context*/ 16025665Sstans VN_DISPOSE(pp, B_INVAL, 0, kcred); 16035665Sstans return (error); 16045665Sstans } 16055665Sstans } 16060Sstevel@tonic-gate 16070Sstevel@tonic-gate /* 16085665Sstans * The anon could have been removed by anon_decref* and/or reallocated 16095665Sstans * by anon layer (an_pvp == NULL) with the same vp, off. 16105665Sstans * In this case the page which has been allocated needs to 16115665Sstans * be freed. 16120Sstevel@tonic-gate */ 16135665Sstans if (!alloc_pg) 16145665Sstans page_io_lock(pp); 1615*12173SMichael.Corcoran@Sun.COM ahm = AH_MUTEX(vp, off); 16160Sstevel@tonic-gate mutex_enter(ahm); 16175665Sstans ap = swap_anon(vp, off); 16185665Sstans if ((ap == NULL || ap->an_pvp == NULL) && alloc_pg) { 16195665Sstans mutex_exit(ahm); 16205665Sstans page_io_unlock(pp); 16215665Sstans /*LINTED: constant in conditional context*/ 16225665Sstans VN_DISPOSE(pp, B_INVAL, 0, kcred); 16235665Sstans return (0); 16240Sstevel@tonic-gate } 16255665Sstans 16260Sstevel@tonic-gate /* 16270Sstevel@tonic-gate * Free the physical slot. It may have been freed up and replaced with 16280Sstevel@tonic-gate * another one while we were getting the page so we have to re-verify 16290Sstevel@tonic-gate * that this is really one we want. If we do free the slot we have 16300Sstevel@tonic-gate * to mark the page modified, as its backing store is now gone. 16310Sstevel@tonic-gate */ 16325665Sstans if ((ap != NULL) && (ap->an_pvp == sip->si_vp && ap->an_poff >= 16335665Sstans sip->si_soff && ap->an_poff < sip->si_eoff)) { 16340Sstevel@tonic-gate swap_phys_free(ap->an_pvp, ap->an_poff, PAGESIZE); 16350Sstevel@tonic-gate ap->an_pvp = NULL; 16365665Sstans ap->an_poff = 0; 16370Sstevel@tonic-gate mutex_exit(ahm); 16380Sstevel@tonic-gate hat_setmod(pp); 16390Sstevel@tonic-gate } else { 16400Sstevel@tonic-gate mutex_exit(ahm); 16410Sstevel@tonic-gate } 16425665Sstans page_io_unlock(pp); 16430Sstevel@tonic-gate page_unlock(pp); 16445665Sstans return (0); 16450Sstevel@tonic-gate } 16460Sstevel@tonic-gate 16475665Sstans 16480Sstevel@tonic-gate /* 16490Sstevel@tonic-gate * Get contig physical backing store for vp, in the range 16500Sstevel@tonic-gate * [*offp, *offp + *lenp), May back a subrange of this, but must 16510Sstevel@tonic-gate * always include the requested offset or fail. Returns the offsets 16520Sstevel@tonic-gate * backed as [*offp, *offp + *lenp) and the physical offsets used to 16530Sstevel@tonic-gate * back them from *pvpp in the range [*pstartp, *pstartp + *lenp). 16540Sstevel@tonic-gate * Returns 0 for success 16550Sstevel@tonic-gate * SE_NOANON -- no anon slot for requested paged 16560Sstevel@tonic-gate * SE_NOSWAP -- no physical swap space available 16570Sstevel@tonic-gate */ 16580Sstevel@tonic-gate int 16590Sstevel@tonic-gate swap_newphysname( 16600Sstevel@tonic-gate struct vnode *vp, 16610Sstevel@tonic-gate u_offset_t offset, 16620Sstevel@tonic-gate u_offset_t *offp, 16630Sstevel@tonic-gate size_t *lenp, 16640Sstevel@tonic-gate struct vnode **pvpp, 16650Sstevel@tonic-gate u_offset_t *poffp) 16660Sstevel@tonic-gate { 16670Sstevel@tonic-gate struct anon *ap = NULL; /* anon slot for vp, off */ 16680Sstevel@tonic-gate int error = 0; 16690Sstevel@tonic-gate struct vnode *pvp; 16700Sstevel@tonic-gate u_offset_t poff, pstart, prem; 16710Sstevel@tonic-gate size_t plen; 16720Sstevel@tonic-gate u_offset_t off, start; 16730Sstevel@tonic-gate kmutex_t *ahm; 16740Sstevel@tonic-gate 16750Sstevel@tonic-gate ASSERT(*offp <= offset && offset < *offp + *lenp); 16760Sstevel@tonic-gate 16770Sstevel@tonic-gate /* Get new physical swap slots. */ 16780Sstevel@tonic-gate plen = *lenp; 16790Sstevel@tonic-gate if (!swap_phys_alloc(&pvp, &pstart, &plen, 0)) { 16800Sstevel@tonic-gate /* 16810Sstevel@tonic-gate * No swap available so return error unless requested 16820Sstevel@tonic-gate * offset is already backed in which case return that. 16830Sstevel@tonic-gate */ 1684*12173SMichael.Corcoran@Sun.COM ahm = AH_MUTEX(vp, offset); 16850Sstevel@tonic-gate mutex_enter(ahm); 16860Sstevel@tonic-gate if ((ap = swap_anon(vp, offset)) == NULL) { 16870Sstevel@tonic-gate error = SE_NOANON; 16880Sstevel@tonic-gate mutex_exit(ahm); 16890Sstevel@tonic-gate return (error); 16900Sstevel@tonic-gate } 16910Sstevel@tonic-gate error = (ap->an_pvp ? 0 : SE_NOSWAP); 16920Sstevel@tonic-gate *offp = offset; 16930Sstevel@tonic-gate *lenp = PAGESIZE; 16940Sstevel@tonic-gate *pvpp = ap->an_pvp; 16950Sstevel@tonic-gate *poffp = ap->an_poff; 16960Sstevel@tonic-gate mutex_exit(ahm); 16970Sstevel@tonic-gate return (error); 16980Sstevel@tonic-gate } 16990Sstevel@tonic-gate 17000Sstevel@tonic-gate /* 17010Sstevel@tonic-gate * We got plen (<= *lenp) contig slots. Use these to back a 17020Sstevel@tonic-gate * subrange of [*offp, *offp + *lenp) which includes offset. 17030Sstevel@tonic-gate * For now we just put offset at the end of the kluster. 17040Sstevel@tonic-gate * Clearly there are other possible choices - which is best? 17050Sstevel@tonic-gate */ 17060Sstevel@tonic-gate start = MAX(*offp, 17070Sstevel@tonic-gate (offset + PAGESIZE > plen) ? (offset + PAGESIZE - plen) : 0); 17080Sstevel@tonic-gate ASSERT(start + plen <= *offp + *lenp); 17090Sstevel@tonic-gate 17100Sstevel@tonic-gate for (off = start, poff = pstart; poff < pstart + plen; 17110Sstevel@tonic-gate off += PAGESIZE, poff += PAGESIZE) { 1712*12173SMichael.Corcoran@Sun.COM ahm = AH_MUTEX(vp, off); 17130Sstevel@tonic-gate mutex_enter(ahm); 17140Sstevel@tonic-gate if ((ap = swap_anon(vp, off)) != NULL) { 17150Sstevel@tonic-gate /* Free old slot if any, and assign new one */ 17160Sstevel@tonic-gate if (ap->an_pvp) 17170Sstevel@tonic-gate swap_phys_free(ap->an_pvp, ap->an_poff, 17180Sstevel@tonic-gate PAGESIZE); 17190Sstevel@tonic-gate ap->an_pvp = pvp; 17200Sstevel@tonic-gate ap->an_poff = poff; 17210Sstevel@tonic-gate } else { /* No anon slot for a klustered page, quit. */ 17220Sstevel@tonic-gate prem = (pstart + plen) - poff; 17230Sstevel@tonic-gate /* Already did requested page, do partial kluster */ 17240Sstevel@tonic-gate if (off > offset) { 17250Sstevel@tonic-gate plen = poff - pstart; 17260Sstevel@tonic-gate error = 0; 17270Sstevel@tonic-gate /* Fail on requested page, error */ 17280Sstevel@tonic-gate } else if (off == offset) { 17290Sstevel@tonic-gate error = SE_NOANON; 17300Sstevel@tonic-gate /* Fail on prior page, fail on requested page, error */ 17310Sstevel@tonic-gate } else if ((ap = swap_anon(vp, offset)) == NULL) { 17320Sstevel@tonic-gate error = SE_NOANON; 17330Sstevel@tonic-gate /* Fail on prior page, got requested page, do only it */ 17340Sstevel@tonic-gate } else { 17350Sstevel@tonic-gate /* Free old slot if any, and assign new one */ 17360Sstevel@tonic-gate if (ap->an_pvp) 17370Sstevel@tonic-gate swap_phys_free(ap->an_pvp, ap->an_poff, 17380Sstevel@tonic-gate PAGESIZE); 17390Sstevel@tonic-gate ap->an_pvp = pvp; 17400Sstevel@tonic-gate ap->an_poff = poff; 17410Sstevel@tonic-gate /* One page kluster */ 17420Sstevel@tonic-gate start = offset; 17430Sstevel@tonic-gate plen = PAGESIZE; 17440Sstevel@tonic-gate pstart = poff; 17450Sstevel@tonic-gate poff += PAGESIZE; 17460Sstevel@tonic-gate prem -= PAGESIZE; 17470Sstevel@tonic-gate } 17480Sstevel@tonic-gate /* Free unassigned slots */ 17490Sstevel@tonic-gate swap_phys_free(pvp, poff, prem); 17500Sstevel@tonic-gate mutex_exit(ahm); 17510Sstevel@tonic-gate break; 17520Sstevel@tonic-gate } 17530Sstevel@tonic-gate mutex_exit(ahm); 17540Sstevel@tonic-gate } 17550Sstevel@tonic-gate ASSERT(*offp <= start && start + plen <= *offp + *lenp); 17560Sstevel@tonic-gate ASSERT(start <= offset && offset < start + plen); 17570Sstevel@tonic-gate *offp = start; 17580Sstevel@tonic-gate *lenp = plen; 17590Sstevel@tonic-gate *pvpp = pvp; 17600Sstevel@tonic-gate *poffp = pstart; 17610Sstevel@tonic-gate return (error); 17620Sstevel@tonic-gate } 17630Sstevel@tonic-gate 17640Sstevel@tonic-gate 17650Sstevel@tonic-gate /* 17660Sstevel@tonic-gate * Get the physical swap backing store location for a given anonymous page 17670Sstevel@tonic-gate * named (vp, off). The backing store name is returned in (*pvpp, *poffp). 17680Sstevel@tonic-gate * Returns 0 success 17690Sstevel@tonic-gate * EIDRM -- no anon slot (page is not allocated) 17700Sstevel@tonic-gate */ 17710Sstevel@tonic-gate int 17720Sstevel@tonic-gate swap_getphysname( 17730Sstevel@tonic-gate struct vnode *vp, 17740Sstevel@tonic-gate u_offset_t off, 17750Sstevel@tonic-gate struct vnode **pvpp, 17760Sstevel@tonic-gate u_offset_t *poffp) 17770Sstevel@tonic-gate { 17780Sstevel@tonic-gate struct anon *ap; 17790Sstevel@tonic-gate int error = 0; 17800Sstevel@tonic-gate kmutex_t *ahm; 17810Sstevel@tonic-gate 1782*12173SMichael.Corcoran@Sun.COM ahm = AH_MUTEX(vp, off); 17830Sstevel@tonic-gate mutex_enter(ahm); 17840Sstevel@tonic-gate 17850Sstevel@tonic-gate /* Get anon slot for vp, off */ 17860Sstevel@tonic-gate ap = swap_anon(vp, off); 17870Sstevel@tonic-gate if (ap == NULL) { 17880Sstevel@tonic-gate error = EIDRM; 17890Sstevel@tonic-gate goto out; 17900Sstevel@tonic-gate } 17910Sstevel@tonic-gate *pvpp = ap->an_pvp; 17920Sstevel@tonic-gate *poffp = ap->an_poff; 17930Sstevel@tonic-gate out: 17940Sstevel@tonic-gate mutex_exit(ahm); 17950Sstevel@tonic-gate return (error); 17960Sstevel@tonic-gate } 1797