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*7884Sgerald.jelinek@sun.com * Copyright 2008 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 /* 400Sstevel@tonic-gate * Each physical swap area has an associated bitmap representing 410Sstevel@tonic-gate * its physical storage. The bitmap records which swap slots are 420Sstevel@tonic-gate * currently allocated or freed. Allocation is done by searching 430Sstevel@tonic-gate * through the bitmap for the first free slot. Thus, there's 440Sstevel@tonic-gate * no linear relation between offset within the swap device and the 450Sstevel@tonic-gate * address (within its segment(s)) of the page that the slot backs; 460Sstevel@tonic-gate * instead, it's an arbitrary one-to-one mapping. 470Sstevel@tonic-gate * 480Sstevel@tonic-gate * Associated with each swap area is a swapinfo structure. These 490Sstevel@tonic-gate * structures are linked into a linear list that determines the 500Sstevel@tonic-gate * ordering of swap areas in the logical swap device. Each contains a 510Sstevel@tonic-gate * pointer to the corresponding bitmap, the area's size, and its 520Sstevel@tonic-gate * associated vnode. 530Sstevel@tonic-gate */ 540Sstevel@tonic-gate 550Sstevel@tonic-gate #include <sys/types.h> 560Sstevel@tonic-gate #include <sys/inttypes.h> 570Sstevel@tonic-gate #include <sys/param.h> 580Sstevel@tonic-gate #include <sys/t_lock.h> 590Sstevel@tonic-gate #include <sys/sysmacros.h> 600Sstevel@tonic-gate #include <sys/systm.h> 610Sstevel@tonic-gate #include <sys/errno.h> 620Sstevel@tonic-gate #include <sys/kmem.h> 630Sstevel@tonic-gate #include <sys/vfs.h> 640Sstevel@tonic-gate #include <sys/vnode.h> 650Sstevel@tonic-gate #include <sys/pathname.h> 660Sstevel@tonic-gate #include <sys/cmn_err.h> 670Sstevel@tonic-gate #include <sys/vtrace.h> 680Sstevel@tonic-gate #include <sys/swap.h> 690Sstevel@tonic-gate #include <sys/dumphdr.h> 700Sstevel@tonic-gate #include <sys/debug.h> 710Sstevel@tonic-gate #include <sys/fs/snode.h> 720Sstevel@tonic-gate #include <sys/fs/swapnode.h> 730Sstevel@tonic-gate #include <sys/policy.h> 740Sstevel@tonic-gate #include <sys/zone.h> 750Sstevel@tonic-gate 760Sstevel@tonic-gate #include <vm/as.h> 770Sstevel@tonic-gate #include <vm/seg.h> 780Sstevel@tonic-gate #include <vm/page.h> 790Sstevel@tonic-gate #include <vm/seg_vn.h> 800Sstevel@tonic-gate #include <vm/hat.h> 810Sstevel@tonic-gate #include <vm/anon.h> 820Sstevel@tonic-gate #include <vm/seg_map.h> 830Sstevel@tonic-gate 840Sstevel@tonic-gate /* 850Sstevel@tonic-gate * To balance the load among multiple swap areas, we don't allow 860Sstevel@tonic-gate * more than swap_maxcontig allocations to be satisfied from a 870Sstevel@tonic-gate * single swap area before moving on to the next swap area. This 880Sstevel@tonic-gate * effectively "interleaves" allocations among the many swap areas. 890Sstevel@tonic-gate */ 900Sstevel@tonic-gate int swap_maxcontig; /* set by anon_init() to 1 Mb */ 910Sstevel@tonic-gate 920Sstevel@tonic-gate #define MINIROOTSIZE 12000 /* ~6 Meg XXX */ 930Sstevel@tonic-gate 940Sstevel@tonic-gate /* 950Sstevel@tonic-gate * XXX - this lock is a kludge. It serializes some aspects of swapadd() and 960Sstevel@tonic-gate * swapdel() (namely VOP_OPEN, VOP_CLOSE, VN_RELE). It protects against 970Sstevel@tonic-gate * somebody swapadd'ing and getting swap slots from a vnode, while someone 980Sstevel@tonic-gate * else is in the process of closing or rele'ing it. 990Sstevel@tonic-gate */ 1000Sstevel@tonic-gate static kmutex_t swap_lock; 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate kmutex_t swapinfo_lock; 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate /* 1050Sstevel@tonic-gate * protected by the swapinfo_lock 1060Sstevel@tonic-gate */ 1070Sstevel@tonic-gate struct swapinfo *swapinfo; 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate static struct swapinfo *silast; 1100Sstevel@tonic-gate static int nswapfiles; 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate static u_offset_t swap_getoff(struct swapinfo *); 1130Sstevel@tonic-gate static int swapadd(struct vnode *, ulong_t, ulong_t, char *); 1140Sstevel@tonic-gate static int swapdel(struct vnode *, ulong_t); 1150Sstevel@tonic-gate static int swapslot_free(struct vnode *, u_offset_t, struct swapinfo *); 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate /* 1180Sstevel@tonic-gate * swap device bitmap allocation macros 1190Sstevel@tonic-gate */ 1200Sstevel@tonic-gate #define MAPSHIFT 5 1210Sstevel@tonic-gate #define NBBW (NBPW * NBBY) /* number of bits per word */ 1220Sstevel@tonic-gate #define TESTBIT(map, i) (((map)[(i) >> MAPSHIFT] & (1 << (i) % NBBW))) 1230Sstevel@tonic-gate #define SETBIT(map, i) (((map)[(i) >> MAPSHIFT] |= (1 << (i) % NBBW))) 1240Sstevel@tonic-gate #define CLEARBIT(map, i) (((map)[(i) >> MAPSHIFT] &= ~(1 << (i) % NBBW))) 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate int swap_debug = 0; /* set for debug printf's */ 1270Sstevel@tonic-gate int swap_verify = 0; /* set to verify slots when freeing and allocating */ 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate uint_t swapalloc_maxcontig; 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate /* 1320Sstevel@tonic-gate * Allocate a range of up to *lenp contiguous slots (page) from a physical 1330Sstevel@tonic-gate * swap device. Flags are one of: 1340Sstevel@tonic-gate * SA_NOT Must have a slot from a physical swap device other than the 1350Sstevel@tonic-gate * the one containing input (*vpp, *offp). 1360Sstevel@tonic-gate * Less slots than requested may be returned. *lenp allocated slots are 1370Sstevel@tonic-gate * returned starting at *offp on *vpp. 1380Sstevel@tonic-gate * Returns 1 for a successful allocation, 0 for couldn't allocate any slots. 1390Sstevel@tonic-gate */ 1400Sstevel@tonic-gate int 1410Sstevel@tonic-gate swap_phys_alloc( 1420Sstevel@tonic-gate struct vnode **vpp, 1430Sstevel@tonic-gate u_offset_t *offp, 1440Sstevel@tonic-gate size_t *lenp, 1450Sstevel@tonic-gate uint_t flags) 1460Sstevel@tonic-gate { 1470Sstevel@tonic-gate struct swapinfo *sip; 1480Sstevel@tonic-gate offset_t soff, noff; 1490Sstevel@tonic-gate size_t len; 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 1520Sstevel@tonic-gate sip = silast; 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate /* Find a desirable physical device and allocate from it. */ 1550Sstevel@tonic-gate do { 1560Sstevel@tonic-gate if (sip == NULL) 1570Sstevel@tonic-gate break; 1580Sstevel@tonic-gate if (!(sip->si_flags & ST_INDEL) && 1590Sstevel@tonic-gate (spgcnt_t)sip->si_nfpgs > 0) { 1600Sstevel@tonic-gate /* Caller wants other than specified swap device */ 1610Sstevel@tonic-gate if (flags & SA_NOT) { 1620Sstevel@tonic-gate if (*vpp != sip->si_vp || 1630Sstevel@tonic-gate *offp < sip->si_soff || 1640Sstevel@tonic-gate *offp >= sip->si_eoff) 1650Sstevel@tonic-gate goto found; 1660Sstevel@tonic-gate /* Caller is loose, will take anything */ 1670Sstevel@tonic-gate } else 1680Sstevel@tonic-gate goto found; 1690Sstevel@tonic-gate } else if (sip->si_nfpgs == 0) 1700Sstevel@tonic-gate sip->si_allocs = 0; 1710Sstevel@tonic-gate if ((sip = sip->si_next) == NULL) 1720Sstevel@tonic-gate sip = swapinfo; 1730Sstevel@tonic-gate } while (sip != silast); 1740Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 1750Sstevel@tonic-gate return (0); 1760Sstevel@tonic-gate found: 1770Sstevel@tonic-gate soff = swap_getoff(sip); 1780Sstevel@tonic-gate sip->si_nfpgs--; 1790Sstevel@tonic-gate if (soff == -1) 1800Sstevel@tonic-gate panic("swap_alloc: swap_getoff failed!"); 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate for (len = PAGESIZE; len < *lenp; len += PAGESIZE) { 1830Sstevel@tonic-gate if (sip->si_nfpgs == 0) 1840Sstevel@tonic-gate break; 1850Sstevel@tonic-gate if (swapalloc_maxcontig && len >= swapalloc_maxcontig) 1860Sstevel@tonic-gate break; 1870Sstevel@tonic-gate noff = swap_getoff(sip); 1880Sstevel@tonic-gate if (noff == -1) { 1890Sstevel@tonic-gate break; 1900Sstevel@tonic-gate } else if (noff != soff + len) { 1910Sstevel@tonic-gate CLEARBIT(sip->si_swapslots, btop(noff - sip->si_soff)); 1920Sstevel@tonic-gate break; 1930Sstevel@tonic-gate } 1940Sstevel@tonic-gate sip->si_nfpgs--; 1950Sstevel@tonic-gate } 1960Sstevel@tonic-gate *vpp = sip->si_vp; 1970Sstevel@tonic-gate *offp = soff; 1980Sstevel@tonic-gate *lenp = len; 1990Sstevel@tonic-gate ASSERT((spgcnt_t)sip->si_nfpgs >= 0); 2000Sstevel@tonic-gate sip->si_allocs += btop(len); 2010Sstevel@tonic-gate if (sip->si_allocs >= swap_maxcontig) { 2020Sstevel@tonic-gate sip->si_allocs = 0; 2030Sstevel@tonic-gate if ((silast = sip->si_next) == NULL) 2040Sstevel@tonic-gate silast = swapinfo; 2050Sstevel@tonic-gate } 2060Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_SWAP_ALLOC, 2075665Sstans "swap_alloc:sip %p offset %lx", sip, soff); 2080Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 2090Sstevel@tonic-gate return (1); 2100Sstevel@tonic-gate } 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate int swap_backsearch = 0; 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate /* 2150Sstevel@tonic-gate * Get a free offset on swap device sip. 2160Sstevel@tonic-gate * Return >=0 offset if succeeded, -1 for failure. 2170Sstevel@tonic-gate */ 2180Sstevel@tonic-gate static u_offset_t 2190Sstevel@tonic-gate swap_getoff(struct swapinfo *sip) 2200Sstevel@tonic-gate { 2210Sstevel@tonic-gate uint_t *sp, *ep; 2220Sstevel@tonic-gate size_t aoff, boff, poff, slotnumber; 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate ASSERT(MUTEX_HELD(&swapinfo_lock)); 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate sip->si_alloccnt++; 2270Sstevel@tonic-gate for (sp = &sip->si_swapslots[sip->si_hint >> MAPSHIFT], 2280Sstevel@tonic-gate ep = &sip->si_swapslots[sip->si_mapsize / NBPW]; sp < ep; sp++) { 2290Sstevel@tonic-gate if (*sp != (uint_t)0xffffffff) 2300Sstevel@tonic-gate goto foundentry; 2310Sstevel@tonic-gate else 2320Sstevel@tonic-gate sip->si_checkcnt++; 2330Sstevel@tonic-gate } 2340Sstevel@tonic-gate SWAP_PRINT(SW_ALLOC, 2350Sstevel@tonic-gate "swap_getoff: couldn't find slot from hint %ld to end\n", 2360Sstevel@tonic-gate sip->si_hint, 0, 0, 0, 0); 2370Sstevel@tonic-gate /* 2380Sstevel@tonic-gate * Go backwards? Check for faster method XXX 2390Sstevel@tonic-gate */ 2400Sstevel@tonic-gate if (swap_backsearch) { 2410Sstevel@tonic-gate for (sp = &sip->si_swapslots[sip->si_hint >> MAPSHIFT], 2420Sstevel@tonic-gate ep = sip->si_swapslots; sp > ep; sp--) { 2430Sstevel@tonic-gate if (*sp != (uint_t)0xffffffff) 2440Sstevel@tonic-gate goto foundentry; 2450Sstevel@tonic-gate else 2460Sstevel@tonic-gate sip->si_checkcnt++; 2470Sstevel@tonic-gate } 2480Sstevel@tonic-gate } else { 2490Sstevel@tonic-gate for (sp = sip->si_swapslots, 2500Sstevel@tonic-gate ep = &sip->si_swapslots[sip->si_hint >> MAPSHIFT]; 2510Sstevel@tonic-gate sp < ep; sp++) { 2520Sstevel@tonic-gate if (*sp != (uint_t)0xffffffff) 2530Sstevel@tonic-gate goto foundentry; 2540Sstevel@tonic-gate else 2550Sstevel@tonic-gate sip->si_checkcnt++; 2560Sstevel@tonic-gate } 2570Sstevel@tonic-gate } 2580Sstevel@tonic-gate if (*sp == 0xffffffff) { 2590Sstevel@tonic-gate cmn_err(CE_WARN, "No free swap slots!"); 2600Sstevel@tonic-gate return ((u_offset_t)-1); 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate foundentry: 2640Sstevel@tonic-gate /* 2650Sstevel@tonic-gate * aoff is the page number offset (in bytes) of the si_swapslots 2660Sstevel@tonic-gate * array element containing a free page 2670Sstevel@tonic-gate * 2680Sstevel@tonic-gate * boff is the page number offset of the free page 2690Sstevel@tonic-gate * (i.e. cleared bit) in si_swapslots[aoff]. 2700Sstevel@tonic-gate */ 2710Sstevel@tonic-gate aoff = ((char *)sp - (char *)sip->si_swapslots) * NBBY; 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate for (boff = (sip->si_hint % NBBW); boff < NBBW; boff++) { 2740Sstevel@tonic-gate if (!TESTBIT(sip->si_swapslots, aoff + boff)) 2750Sstevel@tonic-gate goto foundslot; 2760Sstevel@tonic-gate else 2770Sstevel@tonic-gate sip->si_checkcnt++; 2780Sstevel@tonic-gate } 2790Sstevel@tonic-gate for (boff = 0; boff < (sip->si_hint % NBBW); boff++) { 2800Sstevel@tonic-gate if (!TESTBIT(sip->si_swapslots, aoff + boff)) 2810Sstevel@tonic-gate goto foundslot; 2820Sstevel@tonic-gate else 2830Sstevel@tonic-gate sip->si_checkcnt++; 2840Sstevel@tonic-gate } 2850Sstevel@tonic-gate panic("swap_getoff: didn't find slot in word hint %ld", sip->si_hint); 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate foundslot: 2880Sstevel@tonic-gate /* 2890Sstevel@tonic-gate * Return the offset of the free page in swap device. 2900Sstevel@tonic-gate * Convert page number of byte offset and add starting 2910Sstevel@tonic-gate * offset of swap device. 2920Sstevel@tonic-gate */ 2930Sstevel@tonic-gate slotnumber = aoff + boff; 2940Sstevel@tonic-gate SWAP_PRINT(SW_ALLOC, "swap_getoff: allocating slot %ld\n", 2950Sstevel@tonic-gate slotnumber, 0, 0, 0, 0); 2960Sstevel@tonic-gate poff = ptob(slotnumber); 2970Sstevel@tonic-gate if (poff + sip->si_soff >= sip->si_eoff) 2980Sstevel@tonic-gate printf("ptob(aoff(%ld) + boff(%ld))(%ld) >= eoff(%ld)\n", 2990Sstevel@tonic-gate aoff, boff, ptob(slotnumber), (long)sip->si_eoff); 3000Sstevel@tonic-gate ASSERT(poff < sip->si_eoff); 3010Sstevel@tonic-gate /* 3020Sstevel@tonic-gate * We could verify here that the slot isn't already allocated 3030Sstevel@tonic-gate * by looking through all the anon slots. 3040Sstevel@tonic-gate */ 3050Sstevel@tonic-gate SETBIT(sip->si_swapslots, slotnumber); 3060Sstevel@tonic-gate sip->si_hint = slotnumber + 1; /* hint = next slot */ 3070Sstevel@tonic-gate return (poff + sip->si_soff); 3080Sstevel@tonic-gate } 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate /* 3110Sstevel@tonic-gate * Free a swap page. 3120Sstevel@tonic-gate */ 3130Sstevel@tonic-gate void 3140Sstevel@tonic-gate swap_phys_free(struct vnode *vp, u_offset_t off, size_t len) 3150Sstevel@tonic-gate { 3160Sstevel@tonic-gate struct swapinfo *sip; 3170Sstevel@tonic-gate ssize_t pagenumber, npage; 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 3200Sstevel@tonic-gate sip = swapinfo; 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate do { 3230Sstevel@tonic-gate if (sip->si_vp == vp && 3240Sstevel@tonic-gate sip->si_soff <= off && off < sip->si_eoff) { 3250Sstevel@tonic-gate for (pagenumber = btop(off - sip->si_soff), 3260Sstevel@tonic-gate npage = btop(len) + pagenumber; 3270Sstevel@tonic-gate pagenumber < npage; pagenumber++) { 3280Sstevel@tonic-gate SWAP_PRINT(SW_ALLOC, 3290Sstevel@tonic-gate "swap_phys_free: freeing slot %ld on " 3300Sstevel@tonic-gate "sip %p\n", 3310Sstevel@tonic-gate pagenumber, sip, 0, 0, 0); 3320Sstevel@tonic-gate if (!TESTBIT(sip->si_swapslots, pagenumber)) { 3330Sstevel@tonic-gate panic( 3340Sstevel@tonic-gate "swap_phys_free: freeing free slot " 3350Sstevel@tonic-gate "%p,%lx\n", (void *)vp, 3360Sstevel@tonic-gate ptob(pagenumber) + sip->si_soff); 3370Sstevel@tonic-gate } 3380Sstevel@tonic-gate CLEARBIT(sip->si_swapslots, pagenumber); 3390Sstevel@tonic-gate sip->si_nfpgs++; 3400Sstevel@tonic-gate } 3410Sstevel@tonic-gate ASSERT(sip->si_nfpgs <= sip->si_npgs); 3420Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 3430Sstevel@tonic-gate return; 3440Sstevel@tonic-gate } 3450Sstevel@tonic-gate } while ((sip = sip->si_next) != NULL); 3460Sstevel@tonic-gate panic("swap_phys_free"); 3470Sstevel@tonic-gate /*NOTREACHED*/ 3480Sstevel@tonic-gate } 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate /* 3510Sstevel@tonic-gate * Return the anon struct corresponding for the given 3520Sstevel@tonic-gate * <vnode, off> if it is part of the virtual swap device. 3530Sstevel@tonic-gate * Return the anon struct if found, otherwise NULL. 3540Sstevel@tonic-gate */ 3550Sstevel@tonic-gate struct anon * 3560Sstevel@tonic-gate swap_anon(struct vnode *vp, u_offset_t off) 3570Sstevel@tonic-gate { 3580Sstevel@tonic-gate struct anon *ap; 3590Sstevel@tonic-gate 3600Sstevel@tonic-gate ASSERT(MUTEX_HELD(&anonhash_lock[AH_LOCK(vp, off)])); 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate for (ap = anon_hash[ANON_HASH(vp, off)]; ap != NULL; ap = ap->an_hash) { 3630Sstevel@tonic-gate if (ap->an_vp == vp && ap->an_off == off) 3640Sstevel@tonic-gate return (ap); 3650Sstevel@tonic-gate } 3660Sstevel@tonic-gate return (NULL); 3670Sstevel@tonic-gate } 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate /* 3710Sstevel@tonic-gate * Determine if the vp offset range overlap a swap device. 3720Sstevel@tonic-gate */ 3730Sstevel@tonic-gate int 3740Sstevel@tonic-gate swap_in_range(struct vnode *vp, u_offset_t offset, size_t len) 3750Sstevel@tonic-gate { 3760Sstevel@tonic-gate struct swapinfo *sip; 3770Sstevel@tonic-gate u_offset_t eoff; 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate eoff = offset + len; 3800Sstevel@tonic-gate ASSERT(eoff > offset); 3810Sstevel@tonic-gate 3820Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 3830Sstevel@tonic-gate sip = swapinfo; 3840Sstevel@tonic-gate if (vp && sip) { 3850Sstevel@tonic-gate do { 3860Sstevel@tonic-gate if (vp != sip->si_vp || eoff <= sip->si_soff || 3870Sstevel@tonic-gate offset >= sip->si_eoff) 3880Sstevel@tonic-gate continue; 3890Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 3900Sstevel@tonic-gate return (1); 3910Sstevel@tonic-gate } while ((sip = sip->si_next) != NULL); 3920Sstevel@tonic-gate } 3930Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 3940Sstevel@tonic-gate return (0); 3950Sstevel@tonic-gate } 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate /* 3980Sstevel@tonic-gate * See if name is one of our swap files 3990Sstevel@tonic-gate * even though lookupname failed. 4000Sstevel@tonic-gate * This can be used by swapdel to delete 4010Sstevel@tonic-gate * swap resources on remote machines 4020Sstevel@tonic-gate * where the link has gone down. 4030Sstevel@tonic-gate */ 4040Sstevel@tonic-gate static struct vnode * 4050Sstevel@tonic-gate swapdel_byname( 4060Sstevel@tonic-gate char *name, /* pathname to delete */ 4070Sstevel@tonic-gate ulong_t lowblk) /* Low block number of area to delete */ 4080Sstevel@tonic-gate { 4090Sstevel@tonic-gate struct swapinfo **sipp, *osip; 4100Sstevel@tonic-gate u_offset_t soff; 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate /* 4130Sstevel@tonic-gate * Find the swap file entry for the file to 4140Sstevel@tonic-gate * be deleted. Skip any entries that are in 4150Sstevel@tonic-gate * transition. 4160Sstevel@tonic-gate */ 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate soff = ptob(btopr(lowblk << SCTRSHFT)); /* must be page aligned */ 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 4210Sstevel@tonic-gate for (sipp = &swapinfo; (osip = *sipp) != NULL; sipp = &osip->si_next) { 4220Sstevel@tonic-gate if ((strcmp(osip->si_pname, name) == 0) && 4230Sstevel@tonic-gate (osip->si_soff == soff) && (osip->si_flags == 0)) { 4240Sstevel@tonic-gate struct vnode *vp = osip->si_vp; 4250Sstevel@tonic-gate 4260Sstevel@tonic-gate VN_HOLD(vp); 4270Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 4280Sstevel@tonic-gate return (vp); 4290Sstevel@tonic-gate } 4300Sstevel@tonic-gate } 4310Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 4320Sstevel@tonic-gate return (NULL); 4330Sstevel@tonic-gate } 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate /* 4370Sstevel@tonic-gate * New system call to manipulate swap files. 4380Sstevel@tonic-gate */ 4390Sstevel@tonic-gate int 4400Sstevel@tonic-gate swapctl(int sc_cmd, void *sc_arg, int *rv) 4410Sstevel@tonic-gate { 4420Sstevel@tonic-gate struct swapinfo *sip, *csip, *tsip; 4430Sstevel@tonic-gate int error = 0; 4440Sstevel@tonic-gate struct swapent st, *ust; 4450Sstevel@tonic-gate struct swapres sr; 4460Sstevel@tonic-gate struct vnode *vp; 4470Sstevel@tonic-gate int cnt = 0; 4480Sstevel@tonic-gate int tmp_nswapfiles; 4490Sstevel@tonic-gate int nswap; 4500Sstevel@tonic-gate int length, nlen; 4510Sstevel@tonic-gate int gplen = 0, plen; 4520Sstevel@tonic-gate char *swapname; 4530Sstevel@tonic-gate char *pname; 4540Sstevel@tonic-gate char *tpname; 4550Sstevel@tonic-gate struct anoninfo ai; 4560Sstevel@tonic-gate spgcnt_t avail; 4570Sstevel@tonic-gate int global = INGLOBALZONE(curproc); 458*7884Sgerald.jelinek@sun.com struct zone *zp = curproc->p_zone; 4590Sstevel@tonic-gate 4600Sstevel@tonic-gate /* 4610Sstevel@tonic-gate * When running in a zone we want to hide the details of the swap 4620Sstevel@tonic-gate * devices: we report there only being one swap device named "swap" 4630Sstevel@tonic-gate * having a size equal to the sum of the sizes of all real swap devices 4640Sstevel@tonic-gate * on the system. 4650Sstevel@tonic-gate */ 4660Sstevel@tonic-gate switch (sc_cmd) { 4670Sstevel@tonic-gate case SC_GETNSWP: 4680Sstevel@tonic-gate if (global) 4690Sstevel@tonic-gate *rv = nswapfiles; 4700Sstevel@tonic-gate else 4710Sstevel@tonic-gate *rv = 1; 4720Sstevel@tonic-gate return (0); 4730Sstevel@tonic-gate 4740Sstevel@tonic-gate case SC_AINFO: 4750Sstevel@tonic-gate /* 4760Sstevel@tonic-gate * Return anoninfo information with these changes: 4770Sstevel@tonic-gate * ani_max = maximum amount of swap space 4780Sstevel@tonic-gate * (including potentially available physical memory) 4790Sstevel@tonic-gate * ani_free = amount of unallocated anonymous memory 4800Sstevel@tonic-gate * (some of which might be reserved and including 4810Sstevel@tonic-gate * potentially available physical memory) 4820Sstevel@tonic-gate * ani_resv = amount of claimed (reserved) anonymous memory 4830Sstevel@tonic-gate */ 4840Sstevel@tonic-gate avail = MAX((spgcnt_t)(availrmem - swapfs_minfree), 0); 4850Sstevel@tonic-gate ai.ani_max = (k_anoninfo.ani_max + 486*7884Sgerald.jelinek@sun.com k_anoninfo.ani_mem_resv) + avail; 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate ai.ani_free = k_anoninfo.ani_free + avail; 4890Sstevel@tonic-gate 4900Sstevel@tonic-gate ai.ani_resv = k_anoninfo.ani_phys_resv + 4910Sstevel@tonic-gate k_anoninfo.ani_mem_resv; 4920Sstevel@tonic-gate 493*7884Sgerald.jelinek@sun.com if (!global && zp->zone_max_swap_ctl != UINT64_MAX) { 494*7884Sgerald.jelinek@sun.com /* 495*7884Sgerald.jelinek@sun.com * We're in a non-global zone with a swap cap. We 496*7884Sgerald.jelinek@sun.com * always report the system-wide values for the global 497*7884Sgerald.jelinek@sun.com * zone, even though it too can have a swap cap. 498*7884Sgerald.jelinek@sun.com */ 499*7884Sgerald.jelinek@sun.com 500*7884Sgerald.jelinek@sun.com /* 501*7884Sgerald.jelinek@sun.com * For a swap-capped zone, the numbers are contrived 502*7884Sgerald.jelinek@sun.com * since we don't have a correct value of 'reserved' 503*7884Sgerald.jelinek@sun.com * for the zone. 504*7884Sgerald.jelinek@sun.com * 505*7884Sgerald.jelinek@sun.com * The ani_max value is always the zone's swap cap. 506*7884Sgerald.jelinek@sun.com * 507*7884Sgerald.jelinek@sun.com * The ani_free value is always the difference between 508*7884Sgerald.jelinek@sun.com * the cap and the amount of swap in use by the zone. 509*7884Sgerald.jelinek@sun.com * 510*7884Sgerald.jelinek@sun.com * The ani_resv value is typically set to be the amount 511*7884Sgerald.jelinek@sun.com * of swap in use by the zone, but can be adjusted 512*7884Sgerald.jelinek@sun.com * upwards to indicate how much swap is currently 513*7884Sgerald.jelinek@sun.com * unavailable to that zone due to usage by entities 514*7884Sgerald.jelinek@sun.com * outside the zone. 515*7884Sgerald.jelinek@sun.com * 516*7884Sgerald.jelinek@sun.com * This works as follows. 517*7884Sgerald.jelinek@sun.com * 518*7884Sgerald.jelinek@sun.com * In the 'swap -s' output, the data is displayed 519*7884Sgerald.jelinek@sun.com * as follows: 520*7884Sgerald.jelinek@sun.com * allocated = ani_max - ani_free 521*7884Sgerald.jelinek@sun.com * reserved = ani_resv - allocated 522*7884Sgerald.jelinek@sun.com * available = ani_max - ani_resv 523*7884Sgerald.jelinek@sun.com * 524*7884Sgerald.jelinek@sun.com * Taking a contrived example, if the swap cap is 100 525*7884Sgerald.jelinek@sun.com * and the amount of swap used by the zone is 75, this 526*7884Sgerald.jelinek@sun.com * gives: 527*7884Sgerald.jelinek@sun.com * allocated = ani_max - ani_free = 100 - 25 = 75 528*7884Sgerald.jelinek@sun.com * reserved = ani_resv - allocated = 75 - 75 = 0 529*7884Sgerald.jelinek@sun.com * available = ani_max - ani_resv = 100 - 75 = 25 530*7884Sgerald.jelinek@sun.com * 531*7884Sgerald.jelinek@sun.com * In this typical case, you can see that the 'swap -s' 532*7884Sgerald.jelinek@sun.com * 'reserved' will always be 0 inside a swap capped 533*7884Sgerald.jelinek@sun.com * zone. 534*7884Sgerald.jelinek@sun.com * 535*7884Sgerald.jelinek@sun.com * However, if the system as a whole has less free 536*7884Sgerald.jelinek@sun.com * swap than the zone limits allow, then we adjust 537*7884Sgerald.jelinek@sun.com * the ani_resv value up so that it is the difference 538*7884Sgerald.jelinek@sun.com * between the zone cap and the amount of free system 539*7884Sgerald.jelinek@sun.com * swap. Taking the above example, but when the 540*7884Sgerald.jelinek@sun.com * system as a whole only has 20 of swap available, we 541*7884Sgerald.jelinek@sun.com * get an ani_resv of 100 - 20 = 80. This gives: 542*7884Sgerald.jelinek@sun.com * allocated = ani_max - ani_free = 100 - 25 = 75 543*7884Sgerald.jelinek@sun.com * reserved = ani_resv - allocated = 80 - 75 = 5 544*7884Sgerald.jelinek@sun.com * available = ani_max - ani_resv = 100 - 80 = 20 545*7884Sgerald.jelinek@sun.com * 546*7884Sgerald.jelinek@sun.com * In this case, you can see how the ani_resv value is 547*7884Sgerald.jelinek@sun.com * tweaked up to make the 'swap -s' numbers work inside 548*7884Sgerald.jelinek@sun.com * the zone. 549*7884Sgerald.jelinek@sun.com */ 550*7884Sgerald.jelinek@sun.com rctl_qty_t cap, used; 551*7884Sgerald.jelinek@sun.com pgcnt_t pgcap, sys_avail; 552*7884Sgerald.jelinek@sun.com 553*7884Sgerald.jelinek@sun.com mutex_enter(&zp->zone_mem_lock); 554*7884Sgerald.jelinek@sun.com cap = zp->zone_max_swap_ctl; 555*7884Sgerald.jelinek@sun.com used = zp->zone_max_swap; 556*7884Sgerald.jelinek@sun.com mutex_exit(&zp->zone_mem_lock); 557*7884Sgerald.jelinek@sun.com 558*7884Sgerald.jelinek@sun.com pgcap = MIN(btop(cap), ai.ani_max); 559*7884Sgerald.jelinek@sun.com ai.ani_free = pgcap - btop(used); 560*7884Sgerald.jelinek@sun.com 561*7884Sgerald.jelinek@sun.com /* Get the system-wide swap currently available. */ 562*7884Sgerald.jelinek@sun.com sys_avail = ai.ani_max - ai.ani_resv; 563*7884Sgerald.jelinek@sun.com if (sys_avail < ai.ani_free) 564*7884Sgerald.jelinek@sun.com ai.ani_resv = pgcap - sys_avail; 565*7884Sgerald.jelinek@sun.com else 566*7884Sgerald.jelinek@sun.com ai.ani_resv = btop(used); 567*7884Sgerald.jelinek@sun.com 568*7884Sgerald.jelinek@sun.com ai.ani_max = pgcap; 569*7884Sgerald.jelinek@sun.com } 570*7884Sgerald.jelinek@sun.com 5710Sstevel@tonic-gate if (copyout(&ai, sc_arg, sizeof (struct anoninfo)) != 0) 5720Sstevel@tonic-gate return (EFAULT); 5730Sstevel@tonic-gate return (0); 5740Sstevel@tonic-gate 5750Sstevel@tonic-gate case SC_LIST: 5760Sstevel@tonic-gate if (copyin(sc_arg, &length, sizeof (int)) != 0) 5770Sstevel@tonic-gate return (EFAULT); 5780Sstevel@tonic-gate if (!global) { 5790Sstevel@tonic-gate struct swapent st; 5800Sstevel@tonic-gate char *swappath = "swap"; 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate if (length < 1) 5830Sstevel@tonic-gate return (ENOMEM); 5840Sstevel@tonic-gate ust = (swapent_t *)((swaptbl_t *)sc_arg)->swt_ent; 5850Sstevel@tonic-gate if (copyin(ust, &st, sizeof (swapent_t)) != 0) 5860Sstevel@tonic-gate return (EFAULT); 5870Sstevel@tonic-gate st.ste_start = PAGESIZE >> SCTRSHFT; 5880Sstevel@tonic-gate st.ste_length = (off_t)0; 5890Sstevel@tonic-gate st.ste_pages = 0; 5900Sstevel@tonic-gate st.ste_free = 0; 5910Sstevel@tonic-gate st.ste_flags = 0; 592*7884Sgerald.jelinek@sun.com 5930Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 5940Sstevel@tonic-gate for (sip = swapinfo, nswap = 0; 5950Sstevel@tonic-gate sip != NULL && nswap < nswapfiles; 5960Sstevel@tonic-gate sip = sip->si_next, nswap++) { 5970Sstevel@tonic-gate st.ste_length += 5980Sstevel@tonic-gate (sip->si_eoff - sip->si_soff) >> SCTRSHFT; 5990Sstevel@tonic-gate st.ste_pages += sip->si_npgs; 6000Sstevel@tonic-gate st.ste_free += sip->si_nfpgs; 6010Sstevel@tonic-gate } 6020Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 603*7884Sgerald.jelinek@sun.com 604*7884Sgerald.jelinek@sun.com if (zp->zone_max_swap_ctl != UINT64_MAX) { 605*7884Sgerald.jelinek@sun.com rctl_qty_t cap, used; 606*7884Sgerald.jelinek@sun.com 607*7884Sgerald.jelinek@sun.com mutex_enter(&zp->zone_mem_lock); 608*7884Sgerald.jelinek@sun.com cap = zp->zone_max_swap_ctl; 609*7884Sgerald.jelinek@sun.com used = zp->zone_max_swap; 610*7884Sgerald.jelinek@sun.com mutex_exit(&zp->zone_mem_lock); 611*7884Sgerald.jelinek@sun.com 612*7884Sgerald.jelinek@sun.com st.ste_length = MIN(cap, st.ste_length); 613*7884Sgerald.jelinek@sun.com st.ste_pages = MIN(btop(cap), st.ste_pages); 614*7884Sgerald.jelinek@sun.com st.ste_free = MIN(st.ste_pages - btop(used), 615*7884Sgerald.jelinek@sun.com st.ste_free); 616*7884Sgerald.jelinek@sun.com } 617*7884Sgerald.jelinek@sun.com 6180Sstevel@tonic-gate if (copyout(&st, ust, sizeof (swapent_t)) != 0 || 6190Sstevel@tonic-gate copyout(swappath, st.ste_path, 6205665Sstans strlen(swappath) + 1) != 0) { 6210Sstevel@tonic-gate return (EFAULT); 6220Sstevel@tonic-gate } 6230Sstevel@tonic-gate *rv = 1; 6240Sstevel@tonic-gate return (0); 6250Sstevel@tonic-gate } 6260Sstevel@tonic-gate beginning: 6270Sstevel@tonic-gate tmp_nswapfiles = nswapfiles; 6280Sstevel@tonic-gate /* Return an error if not enough space for the whole table. */ 6290Sstevel@tonic-gate if (length < tmp_nswapfiles) 6300Sstevel@tonic-gate return (ENOMEM); 6310Sstevel@tonic-gate /* 6320Sstevel@tonic-gate * Get memory to hold the swap entries and their names. We'll 6330Sstevel@tonic-gate * copy the real entries into these and then copy these out. 6340Sstevel@tonic-gate * Allocating the pathname memory is only a guess so we may 6350Sstevel@tonic-gate * find that we need more and have to do it again. 6360Sstevel@tonic-gate * All this is because we have to hold the anon lock while 6370Sstevel@tonic-gate * traversing the swapinfo list, and we can't be doing copyouts 6380Sstevel@tonic-gate * and/or kmem_alloc()s during this. 6390Sstevel@tonic-gate */ 6400Sstevel@tonic-gate csip = kmem_zalloc(tmp_nswapfiles * sizeof (struct swapinfo), 6410Sstevel@tonic-gate KM_SLEEP); 6420Sstevel@tonic-gate retry: 6430Sstevel@tonic-gate nlen = tmp_nswapfiles * (gplen += 100); 6440Sstevel@tonic-gate pname = kmem_zalloc(nlen, KM_SLEEP); 6450Sstevel@tonic-gate 6460Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 6470Sstevel@tonic-gate 6480Sstevel@tonic-gate if (tmp_nswapfiles != nswapfiles) { 6490Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 6500Sstevel@tonic-gate kmem_free(pname, nlen); 6510Sstevel@tonic-gate kmem_free(csip, 6520Sstevel@tonic-gate tmp_nswapfiles * sizeof (struct swapinfo)); 6530Sstevel@tonic-gate gplen = 0; 6540Sstevel@tonic-gate goto beginning; 6550Sstevel@tonic-gate } 6560Sstevel@tonic-gate for (sip = swapinfo, tsip = csip, tpname = pname, nswap = 0; 6570Sstevel@tonic-gate sip && nswap < tmp_nswapfiles; 6580Sstevel@tonic-gate sip = sip->si_next, tsip++, tpname += plen, nswap++) { 6590Sstevel@tonic-gate plen = sip->si_pnamelen; 6600Sstevel@tonic-gate if (tpname + plen - pname > nlen) { 6610Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 6620Sstevel@tonic-gate kmem_free(pname, nlen); 6630Sstevel@tonic-gate goto retry; 6640Sstevel@tonic-gate } 6650Sstevel@tonic-gate *tsip = *sip; 6660Sstevel@tonic-gate tsip->si_pname = tpname; 6670Sstevel@tonic-gate (void) strcpy(tsip->si_pname, sip->si_pname); 6680Sstevel@tonic-gate } 6690Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 6700Sstevel@tonic-gate 6710Sstevel@tonic-gate if (sip) { 6720Sstevel@tonic-gate error = ENOMEM; 6730Sstevel@tonic-gate goto lout; 6740Sstevel@tonic-gate } 6750Sstevel@tonic-gate ust = (swapent_t *)((swaptbl_t *)sc_arg)->swt_ent; 6760Sstevel@tonic-gate for (tsip = csip, cnt = 0; cnt < nswap; tsip++, ust++, cnt++) { 6770Sstevel@tonic-gate if (copyin(ust, &st, sizeof (swapent_t)) != 0) { 6780Sstevel@tonic-gate error = EFAULT; 6790Sstevel@tonic-gate goto lout; 6800Sstevel@tonic-gate } 6810Sstevel@tonic-gate st.ste_flags = tsip->si_flags; 6820Sstevel@tonic-gate st.ste_length = 6830Sstevel@tonic-gate (tsip->si_eoff - tsip->si_soff) >> SCTRSHFT; 6840Sstevel@tonic-gate st.ste_start = tsip->si_soff >> SCTRSHFT; 6850Sstevel@tonic-gate st.ste_pages = tsip->si_npgs; 6860Sstevel@tonic-gate st.ste_free = tsip->si_nfpgs; 6870Sstevel@tonic-gate if (copyout(&st, ust, sizeof (swapent_t)) != 0) { 6880Sstevel@tonic-gate error = EFAULT; 6890Sstevel@tonic-gate goto lout; 6900Sstevel@tonic-gate } 6910Sstevel@tonic-gate if (!tsip->si_pnamelen) 6920Sstevel@tonic-gate continue; 6930Sstevel@tonic-gate if (copyout(tsip->si_pname, st.ste_path, 6945665Sstans tsip->si_pnamelen) != 0) { 6950Sstevel@tonic-gate error = EFAULT; 6960Sstevel@tonic-gate goto lout; 6970Sstevel@tonic-gate } 6980Sstevel@tonic-gate } 6990Sstevel@tonic-gate *rv = nswap; 7000Sstevel@tonic-gate lout: 7010Sstevel@tonic-gate kmem_free(csip, tmp_nswapfiles * sizeof (struct swapinfo)); 7020Sstevel@tonic-gate kmem_free(pname, nlen); 7030Sstevel@tonic-gate return (error); 7040Sstevel@tonic-gate 7050Sstevel@tonic-gate case SC_ADD: 7060Sstevel@tonic-gate case SC_REMOVE: 7070Sstevel@tonic-gate break; 7080Sstevel@tonic-gate default: 7090Sstevel@tonic-gate return (EINVAL); 7100Sstevel@tonic-gate } 7110Sstevel@tonic-gate if ((error = secpolicy_swapctl(CRED())) != 0) 7120Sstevel@tonic-gate return (error); 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate if (copyin(sc_arg, &sr, sizeof (swapres_t))) 7150Sstevel@tonic-gate return (EFAULT); 7160Sstevel@tonic-gate 7170Sstevel@tonic-gate /* Allocate the space to read in pathname */ 7180Sstevel@tonic-gate if ((swapname = kmem_alloc(MAXPATHLEN, KM_NOSLEEP)) == NULL) 7190Sstevel@tonic-gate return (ENOMEM); 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate error = copyinstr(sr.sr_name, swapname, MAXPATHLEN, 0); 7220Sstevel@tonic-gate if (error) 7230Sstevel@tonic-gate goto out; 7240Sstevel@tonic-gate 7250Sstevel@tonic-gate error = lookupname(swapname, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp); 7260Sstevel@tonic-gate if (error) { 7270Sstevel@tonic-gate if (sc_cmd == SC_ADD) 7280Sstevel@tonic-gate goto out; 7290Sstevel@tonic-gate /* see if we match by name */ 7300Sstevel@tonic-gate vp = swapdel_byname(swapname, (size_t)sr.sr_start); 7310Sstevel@tonic-gate if (vp == NULL) 7320Sstevel@tonic-gate goto out; 7330Sstevel@tonic-gate } 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate if (vp->v_flag & (VNOMAP | VNOSWAP)) { 7360Sstevel@tonic-gate VN_RELE(vp); 7370Sstevel@tonic-gate error = ENOSYS; 7380Sstevel@tonic-gate goto out; 7390Sstevel@tonic-gate } 7400Sstevel@tonic-gate switch (vp->v_type) { 7410Sstevel@tonic-gate case VBLK: 7420Sstevel@tonic-gate break; 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate case VREG: 7450Sstevel@tonic-gate if (vp->v_vfsp && vn_is_readonly(vp)) 7460Sstevel@tonic-gate error = EROFS; 7470Sstevel@tonic-gate else 7485331Samw error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL); 7490Sstevel@tonic-gate break; 7500Sstevel@tonic-gate 7510Sstevel@tonic-gate case VDIR: 7520Sstevel@tonic-gate error = EISDIR; 7530Sstevel@tonic-gate break; 7540Sstevel@tonic-gate default: 7550Sstevel@tonic-gate error = ENOSYS; 7560Sstevel@tonic-gate break; 7570Sstevel@tonic-gate } 7580Sstevel@tonic-gate if (error == 0) { 7590Sstevel@tonic-gate if (sc_cmd == SC_REMOVE) 7600Sstevel@tonic-gate error = swapdel(vp, sr.sr_start); 7610Sstevel@tonic-gate else 7620Sstevel@tonic-gate error = swapadd(vp, sr.sr_start, 7635665Sstans sr.sr_length, swapname); 7640Sstevel@tonic-gate } 7650Sstevel@tonic-gate VN_RELE(vp); 7660Sstevel@tonic-gate out: 7670Sstevel@tonic-gate kmem_free(swapname, MAXPATHLEN); 7680Sstevel@tonic-gate return (error); 7690Sstevel@tonic-gate } 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate #if defined(_LP64) && defined(_SYSCALL32) 7720Sstevel@tonic-gate 7730Sstevel@tonic-gate int 7740Sstevel@tonic-gate swapctl32(int sc_cmd, void *sc_arg, int *rv) 7750Sstevel@tonic-gate { 7760Sstevel@tonic-gate struct swapinfo *sip, *csip, *tsip; 7770Sstevel@tonic-gate int error = 0; 7780Sstevel@tonic-gate struct swapent32 st, *ust; 7790Sstevel@tonic-gate struct swapres32 sr; 7800Sstevel@tonic-gate struct vnode *vp; 7810Sstevel@tonic-gate int cnt = 0; 7820Sstevel@tonic-gate int tmp_nswapfiles; 7830Sstevel@tonic-gate int nswap; 7840Sstevel@tonic-gate int length, nlen; 7850Sstevel@tonic-gate int gplen = 0, plen; 7860Sstevel@tonic-gate char *swapname; 7870Sstevel@tonic-gate char *pname; 7880Sstevel@tonic-gate char *tpname; 7890Sstevel@tonic-gate struct anoninfo32 ai; 7900Sstevel@tonic-gate size_t s; 7910Sstevel@tonic-gate spgcnt_t avail; 792*7884Sgerald.jelinek@sun.com int global = INGLOBALZONE(curproc); 793*7884Sgerald.jelinek@sun.com struct zone *zp = curproc->p_zone; 7940Sstevel@tonic-gate 795*7884Sgerald.jelinek@sun.com /* 796*7884Sgerald.jelinek@sun.com * When running in a zone we want to hide the details of the swap 797*7884Sgerald.jelinek@sun.com * devices: we report there only being one swap device named "swap" 798*7884Sgerald.jelinek@sun.com * having a size equal to the sum of the sizes of all real swap devices 799*7884Sgerald.jelinek@sun.com * on the system. 800*7884Sgerald.jelinek@sun.com */ 8010Sstevel@tonic-gate switch (sc_cmd) { 8020Sstevel@tonic-gate case SC_GETNSWP: 803*7884Sgerald.jelinek@sun.com if (global) 804*7884Sgerald.jelinek@sun.com *rv = nswapfiles; 805*7884Sgerald.jelinek@sun.com else 806*7884Sgerald.jelinek@sun.com *rv = 1; 8070Sstevel@tonic-gate return (0); 8080Sstevel@tonic-gate 8090Sstevel@tonic-gate case SC_AINFO: 8100Sstevel@tonic-gate /* 8110Sstevel@tonic-gate * Return anoninfo information with these changes: 8120Sstevel@tonic-gate * ani_max = maximum amount of swap space 8130Sstevel@tonic-gate * (including potentially available physical memory) 8140Sstevel@tonic-gate * ani_free = amount of unallocated anonymous memory 8150Sstevel@tonic-gate * (some of which might be reserved and including 8160Sstevel@tonic-gate * potentially available physical memory) 8170Sstevel@tonic-gate * ani_resv = amount of claimed (reserved) anonymous memory 8180Sstevel@tonic-gate */ 8190Sstevel@tonic-gate avail = MAX((spgcnt_t)(availrmem - swapfs_minfree), 0); 8200Sstevel@tonic-gate s = (k_anoninfo.ani_max + k_anoninfo.ani_mem_resv) + avail; 8210Sstevel@tonic-gate if (s > UINT32_MAX) 8220Sstevel@tonic-gate return (EOVERFLOW); 8230Sstevel@tonic-gate ai.ani_max = s; 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate s = k_anoninfo.ani_free + avail; 8260Sstevel@tonic-gate if (s > UINT32_MAX) 8270Sstevel@tonic-gate return (EOVERFLOW); 8280Sstevel@tonic-gate ai.ani_free = s; 8290Sstevel@tonic-gate 8300Sstevel@tonic-gate s = k_anoninfo.ani_phys_resv + k_anoninfo.ani_mem_resv; 8310Sstevel@tonic-gate if (s > UINT32_MAX) 8320Sstevel@tonic-gate return (EOVERFLOW); 8330Sstevel@tonic-gate ai.ani_resv = s; 8340Sstevel@tonic-gate 835*7884Sgerald.jelinek@sun.com if (!global && zp->zone_max_swap_ctl != UINT64_MAX) { 836*7884Sgerald.jelinek@sun.com /* 837*7884Sgerald.jelinek@sun.com * We're in a non-global zone with a swap cap. We 838*7884Sgerald.jelinek@sun.com * always report the system-wide values for the global 839*7884Sgerald.jelinek@sun.com * zone, even though it too can have a swap cap. 840*7884Sgerald.jelinek@sun.com * See the comment for the SC_AINFO case in swapctl() 841*7884Sgerald.jelinek@sun.com * which explains the following logic. 842*7884Sgerald.jelinek@sun.com */ 843*7884Sgerald.jelinek@sun.com rctl_qty_t cap, used; 844*7884Sgerald.jelinek@sun.com pgcnt_t pgcap, sys_avail; 845*7884Sgerald.jelinek@sun.com 846*7884Sgerald.jelinek@sun.com mutex_enter(&zp->zone_mem_lock); 847*7884Sgerald.jelinek@sun.com cap = zp->zone_max_swap_ctl; 848*7884Sgerald.jelinek@sun.com used = zp->zone_max_swap; 849*7884Sgerald.jelinek@sun.com mutex_exit(&zp->zone_mem_lock); 850*7884Sgerald.jelinek@sun.com 851*7884Sgerald.jelinek@sun.com pgcap = MIN(btop(cap), ai.ani_max); 852*7884Sgerald.jelinek@sun.com ai.ani_free = pgcap - btop(used); 853*7884Sgerald.jelinek@sun.com 854*7884Sgerald.jelinek@sun.com /* Get the system-wide swap currently available. */ 855*7884Sgerald.jelinek@sun.com sys_avail = ai.ani_max - ai.ani_resv; 856*7884Sgerald.jelinek@sun.com if (sys_avail < ai.ani_free) 857*7884Sgerald.jelinek@sun.com ai.ani_resv = pgcap - sys_avail; 858*7884Sgerald.jelinek@sun.com else 859*7884Sgerald.jelinek@sun.com ai.ani_resv = btop(used); 860*7884Sgerald.jelinek@sun.com 861*7884Sgerald.jelinek@sun.com ai.ani_max = pgcap; 862*7884Sgerald.jelinek@sun.com } 863*7884Sgerald.jelinek@sun.com 8640Sstevel@tonic-gate if (copyout(&ai, sc_arg, sizeof (ai)) != 0) 8650Sstevel@tonic-gate return (EFAULT); 8660Sstevel@tonic-gate return (0); 8670Sstevel@tonic-gate 8680Sstevel@tonic-gate case SC_LIST: 8690Sstevel@tonic-gate if (copyin(sc_arg, &length, sizeof (int32_t)) != 0) 8700Sstevel@tonic-gate return (EFAULT); 871*7884Sgerald.jelinek@sun.com if (!global) { 872*7884Sgerald.jelinek@sun.com struct swapent32 st; 873*7884Sgerald.jelinek@sun.com char *swappath = "swap"; 874*7884Sgerald.jelinek@sun.com 875*7884Sgerald.jelinek@sun.com if (length < 1) 876*7884Sgerald.jelinek@sun.com return (ENOMEM); 877*7884Sgerald.jelinek@sun.com ust = (swapent32_t *)((swaptbl32_t *)sc_arg)->swt_ent; 878*7884Sgerald.jelinek@sun.com if (copyin(ust, &st, sizeof (swapent32_t)) != 0) 879*7884Sgerald.jelinek@sun.com return (EFAULT); 880*7884Sgerald.jelinek@sun.com st.ste_start = PAGESIZE >> SCTRSHFT; 881*7884Sgerald.jelinek@sun.com st.ste_length = (off_t)0; 882*7884Sgerald.jelinek@sun.com st.ste_pages = 0; 883*7884Sgerald.jelinek@sun.com st.ste_free = 0; 884*7884Sgerald.jelinek@sun.com st.ste_flags = 0; 885*7884Sgerald.jelinek@sun.com 886*7884Sgerald.jelinek@sun.com mutex_enter(&swapinfo_lock); 887*7884Sgerald.jelinek@sun.com for (sip = swapinfo, nswap = 0; 888*7884Sgerald.jelinek@sun.com sip != NULL && nswap < nswapfiles; 889*7884Sgerald.jelinek@sun.com sip = sip->si_next, nswap++) { 890*7884Sgerald.jelinek@sun.com st.ste_length += 891*7884Sgerald.jelinek@sun.com (sip->si_eoff - sip->si_soff) >> SCTRSHFT; 892*7884Sgerald.jelinek@sun.com st.ste_pages += sip->si_npgs; 893*7884Sgerald.jelinek@sun.com st.ste_free += sip->si_nfpgs; 894*7884Sgerald.jelinek@sun.com } 895*7884Sgerald.jelinek@sun.com mutex_exit(&swapinfo_lock); 896*7884Sgerald.jelinek@sun.com 897*7884Sgerald.jelinek@sun.com if (zp->zone_max_swap_ctl != UINT64_MAX) { 898*7884Sgerald.jelinek@sun.com rctl_qty_t cap, used; 899*7884Sgerald.jelinek@sun.com 900*7884Sgerald.jelinek@sun.com mutex_enter(&zp->zone_mem_lock); 901*7884Sgerald.jelinek@sun.com cap = zp->zone_max_swap_ctl; 902*7884Sgerald.jelinek@sun.com used = zp->zone_max_swap; 903*7884Sgerald.jelinek@sun.com mutex_exit(&zp->zone_mem_lock); 904*7884Sgerald.jelinek@sun.com 905*7884Sgerald.jelinek@sun.com st.ste_length = MIN(cap, st.ste_length); 906*7884Sgerald.jelinek@sun.com st.ste_pages = MIN(btop(cap), st.ste_pages); 907*7884Sgerald.jelinek@sun.com st.ste_free = MIN(st.ste_pages - btop(used), 908*7884Sgerald.jelinek@sun.com st.ste_free); 909*7884Sgerald.jelinek@sun.com } 910*7884Sgerald.jelinek@sun.com 911*7884Sgerald.jelinek@sun.com if (copyout(&st, ust, sizeof (swapent32_t)) != 0 || 912*7884Sgerald.jelinek@sun.com copyout(swappath, (caddr_t)(uintptr_t)st.ste_path, 913*7884Sgerald.jelinek@sun.com strlen(swappath) + 1) != 0) { 914*7884Sgerald.jelinek@sun.com return (EFAULT); 915*7884Sgerald.jelinek@sun.com } 916*7884Sgerald.jelinek@sun.com *rv = 1; 917*7884Sgerald.jelinek@sun.com return (0); 918*7884Sgerald.jelinek@sun.com } 9190Sstevel@tonic-gate beginning: 9200Sstevel@tonic-gate tmp_nswapfiles = nswapfiles; 9210Sstevel@tonic-gate /* Return an error if not enough space for the whole table. */ 9220Sstevel@tonic-gate if (length < tmp_nswapfiles) 9230Sstevel@tonic-gate return (ENOMEM); 9240Sstevel@tonic-gate /* 9250Sstevel@tonic-gate * Get memory to hold the swap entries and their names. We'll 9260Sstevel@tonic-gate * copy the real entries into these and then copy these out. 9270Sstevel@tonic-gate * Allocating the pathname memory is only a guess so we may 9280Sstevel@tonic-gate * find that we need more and have to do it again. 9290Sstevel@tonic-gate * All this is because we have to hold the anon lock while 9300Sstevel@tonic-gate * traversing the swapinfo list, and we can't be doing copyouts 9310Sstevel@tonic-gate * and/or kmem_alloc()s during this. 9320Sstevel@tonic-gate */ 9330Sstevel@tonic-gate csip = kmem_zalloc(tmp_nswapfiles * sizeof (*csip), KM_SLEEP); 9340Sstevel@tonic-gate retry: 9350Sstevel@tonic-gate nlen = tmp_nswapfiles * (gplen += 100); 9360Sstevel@tonic-gate pname = kmem_zalloc(nlen, KM_SLEEP); 9370Sstevel@tonic-gate 9380Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 9390Sstevel@tonic-gate 9400Sstevel@tonic-gate if (tmp_nswapfiles != nswapfiles) { 9410Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 9420Sstevel@tonic-gate kmem_free(pname, nlen); 9430Sstevel@tonic-gate kmem_free(csip, tmp_nswapfiles * sizeof (*csip)); 9440Sstevel@tonic-gate gplen = 0; 9450Sstevel@tonic-gate goto beginning; 9460Sstevel@tonic-gate } 9470Sstevel@tonic-gate for (sip = swapinfo, tsip = csip, tpname = pname, nswap = 0; 9480Sstevel@tonic-gate (sip != NULL) && (nswap < tmp_nswapfiles); 9490Sstevel@tonic-gate sip = sip->si_next, tsip++, tpname += plen, nswap++) { 9500Sstevel@tonic-gate plen = sip->si_pnamelen; 9510Sstevel@tonic-gate if (tpname + plen - pname > nlen) { 9520Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 9530Sstevel@tonic-gate kmem_free(pname, nlen); 9540Sstevel@tonic-gate goto retry; 9550Sstevel@tonic-gate } 9560Sstevel@tonic-gate *tsip = *sip; 9570Sstevel@tonic-gate tsip->si_pname = tpname; 9580Sstevel@tonic-gate (void) strcpy(tsip->si_pname, sip->si_pname); 9590Sstevel@tonic-gate } 9600Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 9610Sstevel@tonic-gate 9620Sstevel@tonic-gate if (sip != NULL) { 9630Sstevel@tonic-gate error = ENOMEM; 9640Sstevel@tonic-gate goto lout; 9650Sstevel@tonic-gate } 9660Sstevel@tonic-gate ust = (swapent32_t *)((swaptbl32_t *)sc_arg)->swt_ent; 9670Sstevel@tonic-gate for (tsip = csip, cnt = 0; cnt < nswap; tsip++, ust++, cnt++) { 9680Sstevel@tonic-gate if (copyin(ust, &st, sizeof (*ust)) != 0) { 9690Sstevel@tonic-gate error = EFAULT; 9700Sstevel@tonic-gate goto lout; 9710Sstevel@tonic-gate } 9720Sstevel@tonic-gate st.ste_flags = tsip->si_flags; 9730Sstevel@tonic-gate st.ste_length = 9740Sstevel@tonic-gate (tsip->si_eoff - tsip->si_soff) >> SCTRSHFT; 9750Sstevel@tonic-gate st.ste_start = tsip->si_soff >> SCTRSHFT; 9760Sstevel@tonic-gate st.ste_pages = tsip->si_npgs; 9770Sstevel@tonic-gate st.ste_free = tsip->si_nfpgs; 9780Sstevel@tonic-gate if (copyout(&st, ust, sizeof (st)) != 0) { 9790Sstevel@tonic-gate error = EFAULT; 9800Sstevel@tonic-gate goto lout; 9810Sstevel@tonic-gate } 9820Sstevel@tonic-gate if (!tsip->si_pnamelen) 9830Sstevel@tonic-gate continue; 9840Sstevel@tonic-gate if (copyout(tsip->si_pname, 9850Sstevel@tonic-gate (caddr_t)(uintptr_t)st.ste_path, 9860Sstevel@tonic-gate tsip->si_pnamelen) != 0) { 9870Sstevel@tonic-gate error = EFAULT; 9880Sstevel@tonic-gate goto lout; 9890Sstevel@tonic-gate } 9900Sstevel@tonic-gate } 9910Sstevel@tonic-gate *rv = nswap; 9920Sstevel@tonic-gate lout: 9930Sstevel@tonic-gate kmem_free(csip, tmp_nswapfiles * sizeof (*csip)); 9940Sstevel@tonic-gate kmem_free(pname, nlen); 9950Sstevel@tonic-gate return (error); 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate case SC_ADD: 9980Sstevel@tonic-gate case SC_REMOVE: 9990Sstevel@tonic-gate break; 10000Sstevel@tonic-gate default: 10010Sstevel@tonic-gate return (EINVAL); 10020Sstevel@tonic-gate } 10030Sstevel@tonic-gate if ((error = secpolicy_swapctl(CRED())) != 0) 10040Sstevel@tonic-gate return (error); 10050Sstevel@tonic-gate 10060Sstevel@tonic-gate if (copyin(sc_arg, &sr, sizeof (sr))) 10070Sstevel@tonic-gate return (EFAULT); 10080Sstevel@tonic-gate 10090Sstevel@tonic-gate /* Allocate the space to read in pathname */ 10100Sstevel@tonic-gate if ((swapname = kmem_alloc(MAXPATHLEN, KM_NOSLEEP)) == NULL) 10110Sstevel@tonic-gate return (ENOMEM); 10120Sstevel@tonic-gate 10130Sstevel@tonic-gate error = copyinstr((caddr_t)(uintptr_t)sr.sr_name, 10140Sstevel@tonic-gate swapname, MAXPATHLEN, NULL); 10150Sstevel@tonic-gate if (error) 10160Sstevel@tonic-gate goto out; 10170Sstevel@tonic-gate 10180Sstevel@tonic-gate error = lookupname(swapname, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp); 10190Sstevel@tonic-gate if (error) { 10200Sstevel@tonic-gate if (sc_cmd == SC_ADD) 10210Sstevel@tonic-gate goto out; 10220Sstevel@tonic-gate /* see if we match by name */ 10230Sstevel@tonic-gate vp = swapdel_byname(swapname, (uint_t)sr.sr_start); 10240Sstevel@tonic-gate if (vp == NULL) 10250Sstevel@tonic-gate goto out; 10260Sstevel@tonic-gate } 10270Sstevel@tonic-gate 10280Sstevel@tonic-gate if (vp->v_flag & (VNOMAP | VNOSWAP)) { 10290Sstevel@tonic-gate VN_RELE(vp); 10300Sstevel@tonic-gate error = ENOSYS; 10310Sstevel@tonic-gate goto out; 10320Sstevel@tonic-gate } 10330Sstevel@tonic-gate switch (vp->v_type) { 10340Sstevel@tonic-gate case VBLK: 10350Sstevel@tonic-gate break; 10360Sstevel@tonic-gate 10370Sstevel@tonic-gate case VREG: 10380Sstevel@tonic-gate if (vp->v_vfsp && vn_is_readonly(vp)) 10390Sstevel@tonic-gate error = EROFS; 10400Sstevel@tonic-gate else 10415331Samw error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL); 10420Sstevel@tonic-gate break; 10430Sstevel@tonic-gate 10440Sstevel@tonic-gate case VDIR: 10450Sstevel@tonic-gate error = EISDIR; 10460Sstevel@tonic-gate break; 10470Sstevel@tonic-gate default: 10480Sstevel@tonic-gate error = ENOSYS; 10490Sstevel@tonic-gate break; 10500Sstevel@tonic-gate } 10510Sstevel@tonic-gate if (error == 0) { 10520Sstevel@tonic-gate if (sc_cmd == SC_REMOVE) 10530Sstevel@tonic-gate error = swapdel(vp, sr.sr_start); 10540Sstevel@tonic-gate else 10550Sstevel@tonic-gate error = swapadd(vp, sr.sr_start, sr.sr_length, 10560Sstevel@tonic-gate swapname); 10570Sstevel@tonic-gate } 10580Sstevel@tonic-gate VN_RELE(vp); 10590Sstevel@tonic-gate out: 10600Sstevel@tonic-gate kmem_free(swapname, MAXPATHLEN); 10610Sstevel@tonic-gate return (error); 10620Sstevel@tonic-gate } 10630Sstevel@tonic-gate 10640Sstevel@tonic-gate #endif /* _LP64 && _SYSCALL32 */ 10650Sstevel@tonic-gate 10660Sstevel@tonic-gate /* 10670Sstevel@tonic-gate * Add a new swap file. 10680Sstevel@tonic-gate */ 10690Sstevel@tonic-gate int 10700Sstevel@tonic-gate swapadd(struct vnode *vp, ulong_t lowblk, ulong_t nblks, char *swapname) 10710Sstevel@tonic-gate { 10720Sstevel@tonic-gate struct swapinfo **sipp, *nsip = NULL, *esip = NULL; 10730Sstevel@tonic-gate struct vnode *cvp; 10740Sstevel@tonic-gate struct vattr vattr; 10750Sstevel@tonic-gate pgcnt_t pages; 10760Sstevel@tonic-gate u_offset_t soff, eoff; 10770Sstevel@tonic-gate int error; 10780Sstevel@tonic-gate ssize_t i, start, end; 10790Sstevel@tonic-gate ushort_t wasswap; 10800Sstevel@tonic-gate ulong_t startblk; 10810Sstevel@tonic-gate size_t returned_mem; 10820Sstevel@tonic-gate 10830Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: vp %p lowblk %ld nblks %ld swapname %s\n", 10840Sstevel@tonic-gate vp, lowblk, nblks, swapname, 0); 10850Sstevel@tonic-gate /* 10860Sstevel@tonic-gate * Get the real vnode. (If vp is not a specnode it just returns vp, so 10870Sstevel@tonic-gate * it does the right thing, but having this code know about specnodes 10880Sstevel@tonic-gate * violates the spirit of having it be indepedent of vnode type.) 10890Sstevel@tonic-gate */ 10900Sstevel@tonic-gate cvp = common_specvp(vp); 10910Sstevel@tonic-gate 10920Sstevel@tonic-gate /* 10930Sstevel@tonic-gate * Or in VISSWAP so file system has chance to deny swap-ons during open. 10940Sstevel@tonic-gate */ 10950Sstevel@tonic-gate mutex_enter(&cvp->v_lock); 10960Sstevel@tonic-gate wasswap = cvp->v_flag & VISSWAP; 10970Sstevel@tonic-gate cvp->v_flag |= VISSWAP; 10980Sstevel@tonic-gate mutex_exit(&cvp->v_lock); 10990Sstevel@tonic-gate 11000Sstevel@tonic-gate mutex_enter(&swap_lock); 11015331Samw if (error = VOP_OPEN(&cvp, FREAD|FWRITE, CRED(), NULL)) { 11020Sstevel@tonic-gate mutex_exit(&swap_lock); 11030Sstevel@tonic-gate /* restore state of v_flag */ 11040Sstevel@tonic-gate if (!wasswap) { 11050Sstevel@tonic-gate mutex_enter(&cvp->v_lock); 11060Sstevel@tonic-gate cvp->v_flag &= ~VISSWAP; 11070Sstevel@tonic-gate mutex_exit(&cvp->v_lock); 11080Sstevel@tonic-gate } 11090Sstevel@tonic-gate return (error); 11100Sstevel@tonic-gate } 11110Sstevel@tonic-gate mutex_exit(&swap_lock); 11120Sstevel@tonic-gate 11130Sstevel@tonic-gate /* 11140Sstevel@tonic-gate * Get partition size. Return error if empty partition, 11150Sstevel@tonic-gate * or if request does not fit within the partition. 11160Sstevel@tonic-gate * If this is the first swap device, we can reduce 11170Sstevel@tonic-gate * the size of the swap area to match what is 11180Sstevel@tonic-gate * available. This can happen if the system was built 11190Sstevel@tonic-gate * on a machine with a different size swap partition. 11200Sstevel@tonic-gate */ 11210Sstevel@tonic-gate vattr.va_mask = AT_SIZE; 11225331Samw if (error = VOP_GETATTR(cvp, &vattr, ATTR_COMM, CRED(), NULL)) 11230Sstevel@tonic-gate goto out; 11240Sstevel@tonic-gate 11250Sstevel@tonic-gate /* 11260Sstevel@tonic-gate * Specfs returns a va_size of MAXOFFSET_T (UNKNOWN_SIZE) when the 11270Sstevel@tonic-gate * size of the device can't be determined. 11280Sstevel@tonic-gate */ 11290Sstevel@tonic-gate if ((vattr.va_size == 0) || (vattr.va_size == MAXOFFSET_T)) { 11300Sstevel@tonic-gate error = EINVAL; 11310Sstevel@tonic-gate goto out; 11320Sstevel@tonic-gate } 11330Sstevel@tonic-gate 11340Sstevel@tonic-gate #ifdef _ILP32 11350Sstevel@tonic-gate /* 11360Sstevel@tonic-gate * No support for large swap in 32-bit OS, if the size of the swap is 11370Sstevel@tonic-gate * bigger than MAXOFF32_T then the size used by swapfs must be limited. 11380Sstevel@tonic-gate * This limitation is imposed by the swap subsystem itself, a D_64BIT 11390Sstevel@tonic-gate * driver as the target of swap operation should be able to field 11400Sstevel@tonic-gate * the IO. 11410Sstevel@tonic-gate */ 11420Sstevel@tonic-gate if (vattr.va_size > MAXOFF32_T) { 11430Sstevel@tonic-gate cmn_err(CE_NOTE, 11445665Sstans "!swap device %s truncated from 0x%llx to 0x%x bytes", 11455665Sstans swapname, vattr.va_size, MAXOFF32_T); 11460Sstevel@tonic-gate vattr.va_size = MAXOFF32_T; 11470Sstevel@tonic-gate } 11480Sstevel@tonic-gate #endif /* _ILP32 */ 11490Sstevel@tonic-gate 11500Sstevel@tonic-gate /* Fail if file not writeable (try to set size to current size) */ 11510Sstevel@tonic-gate vattr.va_mask = AT_SIZE; 11520Sstevel@tonic-gate if (error = VOP_SETATTR(cvp, &vattr, 0, CRED(), NULL)) 11530Sstevel@tonic-gate goto out; 11540Sstevel@tonic-gate 11550Sstevel@tonic-gate /* Fail if fs does not support VOP_PAGEIO */ 11565331Samw error = VOP_PAGEIO(cvp, (page_t *)NULL, (u_offset_t)0, 0, 0, CRED(), 11575665Sstans NULL); 11580Sstevel@tonic-gate 11590Sstevel@tonic-gate if (error == ENOSYS) 11600Sstevel@tonic-gate goto out; 11610Sstevel@tonic-gate else 11620Sstevel@tonic-gate error = 0; 11630Sstevel@tonic-gate /* 11640Sstevel@tonic-gate * If swapping on the root filesystem don't put swap blocks that 11650Sstevel@tonic-gate * correspond to the miniroot filesystem on the swap free list. 11660Sstevel@tonic-gate */ 11670Sstevel@tonic-gate if (cvp == rootdir) 11680Sstevel@tonic-gate startblk = roundup(MINIROOTSIZE<<SCTRSHFT, klustsize)>>SCTRSHFT; 11690Sstevel@tonic-gate else /* Skip 1st page (disk label) */ 11700Sstevel@tonic-gate startblk = (ulong_t)(lowblk ? lowblk : 1); 11710Sstevel@tonic-gate 11720Sstevel@tonic-gate soff = startblk << SCTRSHFT; 11730Sstevel@tonic-gate if (soff >= vattr.va_size) { 11740Sstevel@tonic-gate error = EINVAL; 11750Sstevel@tonic-gate goto out; 11760Sstevel@tonic-gate } 11770Sstevel@tonic-gate 11780Sstevel@tonic-gate /* 11790Sstevel@tonic-gate * If user specified 0 blks, use the size of the device 11800Sstevel@tonic-gate */ 11810Sstevel@tonic-gate eoff = nblks ? soff + (nblks - (startblk - lowblk) << SCTRSHFT) : 11825665Sstans vattr.va_size; 11830Sstevel@tonic-gate 11840Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: va_size %ld soff %ld eoff %ld\n", 11850Sstevel@tonic-gate vattr.va_size, soff, eoff, 0, 0); 11860Sstevel@tonic-gate 11870Sstevel@tonic-gate if (eoff > vattr.va_size) { 11880Sstevel@tonic-gate error = EINVAL; 11890Sstevel@tonic-gate goto out; 11900Sstevel@tonic-gate } 11910Sstevel@tonic-gate 11920Sstevel@tonic-gate /* 11930Sstevel@tonic-gate * The starting and ending offsets must be page aligned. 11940Sstevel@tonic-gate * Round soff up to next page boundary, round eoff 11950Sstevel@tonic-gate * down to previous page boundary. 11960Sstevel@tonic-gate */ 11970Sstevel@tonic-gate soff = ptob(btopr(soff)); 11980Sstevel@tonic-gate eoff = ptob(btop(eoff)); 11990Sstevel@tonic-gate if (soff >= eoff) { 12000Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: soff %ld >= eoff %ld\n", 12010Sstevel@tonic-gate soff, eoff, 0, 0, 0); 12020Sstevel@tonic-gate error = EINVAL; 12030Sstevel@tonic-gate goto out; 12040Sstevel@tonic-gate } 12050Sstevel@tonic-gate 12060Sstevel@tonic-gate pages = btop(eoff - soff); 12070Sstevel@tonic-gate 12080Sstevel@tonic-gate /* Allocate and partially set up the new swapinfo */ 12090Sstevel@tonic-gate nsip = kmem_zalloc(sizeof (struct swapinfo), KM_SLEEP); 12100Sstevel@tonic-gate nsip->si_vp = cvp; 12110Sstevel@tonic-gate 12120Sstevel@tonic-gate nsip->si_soff = soff; 12130Sstevel@tonic-gate nsip->si_eoff = eoff; 12140Sstevel@tonic-gate nsip->si_hint = 0; 12150Sstevel@tonic-gate nsip->si_checkcnt = nsip->si_alloccnt = 0; 12160Sstevel@tonic-gate 12170Sstevel@tonic-gate nsip->si_pnamelen = (int)strlen(swapname) + 1; 12180Sstevel@tonic-gate nsip->si_pname = (char *)kmem_zalloc(nsip->si_pnamelen, KM_SLEEP); 12190Sstevel@tonic-gate bcopy(swapname, nsip->si_pname, nsip->si_pnamelen - 1); 12200Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: allocating swapinfo for %s, %ld pages\n", 12210Sstevel@tonic-gate swapname, pages, 0, 0, 0); 12220Sstevel@tonic-gate /* 12230Sstevel@tonic-gate * Size of swapslots map in bytes 12240Sstevel@tonic-gate */ 12250Sstevel@tonic-gate nsip->si_mapsize = P2ROUNDUP(pages, NBBW) / NBBY; 12260Sstevel@tonic-gate nsip->si_swapslots = kmem_zalloc(nsip->si_mapsize, KM_SLEEP); 12270Sstevel@tonic-gate 12280Sstevel@tonic-gate /* 12290Sstevel@tonic-gate * Permanently set the bits that can't ever be allocated, 12300Sstevel@tonic-gate * i.e. those from the ending offset to the round up slot for the 12310Sstevel@tonic-gate * swapslots bit map. 12320Sstevel@tonic-gate */ 12330Sstevel@tonic-gate start = pages; 12340Sstevel@tonic-gate end = P2ROUNDUP(pages, NBBW); 12350Sstevel@tonic-gate for (i = start; i < end; i++) { 12360Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: set bit for page %ld\n", i, 12370Sstevel@tonic-gate 0, 0, 0, 0); 12380Sstevel@tonic-gate SETBIT(nsip->si_swapslots, i); 12390Sstevel@tonic-gate } 12400Sstevel@tonic-gate nsip->si_npgs = nsip->si_nfpgs = pages; 12410Sstevel@tonic-gate /* 12420Sstevel@tonic-gate * Now check to see if we can add it. We wait til now to check because 12430Sstevel@tonic-gate * we need the swapinfo_lock and we don't want sleep with it (e.g., 12440Sstevel@tonic-gate * during kmem_alloc()) while we're setting up the swapinfo. 12450Sstevel@tonic-gate */ 12460Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 12470Sstevel@tonic-gate for (sipp = &swapinfo; (esip = *sipp) != NULL; sipp = &esip->si_next) { 12480Sstevel@tonic-gate if (esip->si_vp == cvp) { 12490Sstevel@tonic-gate if (esip->si_soff == soff && esip->si_npgs == pages && 12500Sstevel@tonic-gate (esip->si_flags & ST_DOINGDEL)) { 12510Sstevel@tonic-gate /* 12520Sstevel@tonic-gate * We are adding a device that we are in the 12530Sstevel@tonic-gate * middle of deleting. Just clear the 12540Sstevel@tonic-gate * ST_DOINGDEL flag to signal this and 12550Sstevel@tonic-gate * the deletion routine will eventually notice 12560Sstevel@tonic-gate * it and add it back. 12570Sstevel@tonic-gate */ 12580Sstevel@tonic-gate esip->si_flags &= ~ST_DOINGDEL; 12590Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 12600Sstevel@tonic-gate goto out; 12610Sstevel@tonic-gate } 12620Sstevel@tonic-gate /* disallow overlapping swap files */ 12630Sstevel@tonic-gate if ((soff < esip->si_eoff) && (eoff > esip->si_soff)) { 12640Sstevel@tonic-gate error = EEXIST; 12650Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 12660Sstevel@tonic-gate goto out; 12670Sstevel@tonic-gate } 12680Sstevel@tonic-gate } 12690Sstevel@tonic-gate } 12700Sstevel@tonic-gate 12710Sstevel@tonic-gate nswapfiles++; 12720Sstevel@tonic-gate 12730Sstevel@tonic-gate /* 12740Sstevel@tonic-gate * add new swap device to list and shift allocations to it 12750Sstevel@tonic-gate * before updating the anoninfo counters 12760Sstevel@tonic-gate */ 12770Sstevel@tonic-gate *sipp = nsip; 12780Sstevel@tonic-gate silast = nsip; 12790Sstevel@tonic-gate 12800Sstevel@tonic-gate /* 12810Sstevel@tonic-gate * Update the total amount of reservable swap space 12820Sstevel@tonic-gate * accounting properly for swap space from physical memory 12830Sstevel@tonic-gate */ 12840Sstevel@tonic-gate /* New swap device soaks up currently reserved memory swap */ 12850Sstevel@tonic-gate mutex_enter(&anoninfo_lock); 12860Sstevel@tonic-gate 12870Sstevel@tonic-gate ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap); 12880Sstevel@tonic-gate ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv); 12890Sstevel@tonic-gate 12900Sstevel@tonic-gate k_anoninfo.ani_max += pages; 12910Sstevel@tonic-gate ANI_ADD(pages); 12920Sstevel@tonic-gate if (k_anoninfo.ani_mem_resv > k_anoninfo.ani_locked_swap) { 12930Sstevel@tonic-gate returned_mem = MIN(k_anoninfo.ani_mem_resv - 12940Sstevel@tonic-gate k_anoninfo.ani_locked_swap, 12950Sstevel@tonic-gate k_anoninfo.ani_max - k_anoninfo.ani_phys_resv); 12960Sstevel@tonic-gate 12970Sstevel@tonic-gate ANI_ADD(-returned_mem); 12980Sstevel@tonic-gate k_anoninfo.ani_free -= returned_mem; 12990Sstevel@tonic-gate k_anoninfo.ani_mem_resv -= returned_mem; 13000Sstevel@tonic-gate k_anoninfo.ani_phys_resv += returned_mem; 13010Sstevel@tonic-gate 13020Sstevel@tonic-gate mutex_enter(&freemem_lock); 13030Sstevel@tonic-gate availrmem += returned_mem; 13040Sstevel@tonic-gate mutex_exit(&freemem_lock); 13050Sstevel@tonic-gate } 13060Sstevel@tonic-gate /* 13070Sstevel@tonic-gate * At boot time, to permit booting small memory machines using 13080Sstevel@tonic-gate * only physical memory as swap space, we allowed a dangerously 13090Sstevel@tonic-gate * large amount of memory to be used as swap space; now that 13100Sstevel@tonic-gate * more physical backing store is available bump down the amount 13110Sstevel@tonic-gate * we can get from memory to a safer size. 13120Sstevel@tonic-gate */ 13130Sstevel@tonic-gate if (swapfs_minfree < swapfs_desfree) { 13140Sstevel@tonic-gate mutex_enter(&freemem_lock); 13150Sstevel@tonic-gate if (availrmem > swapfs_desfree || !k_anoninfo.ani_mem_resv) 13160Sstevel@tonic-gate swapfs_minfree = swapfs_desfree; 13170Sstevel@tonic-gate mutex_exit(&freemem_lock); 13180Sstevel@tonic-gate } 13190Sstevel@tonic-gate 13200Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: ani_max %ld ani_free %ld\n", 13210Sstevel@tonic-gate k_anoninfo.ani_free, k_anoninfo.ani_free, 0, 0, 0); 13220Sstevel@tonic-gate 13230Sstevel@tonic-gate mutex_exit(&anoninfo_lock); 13240Sstevel@tonic-gate 13250Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 13260Sstevel@tonic-gate 13270Sstevel@tonic-gate /* Initialize the dump device */ 13280Sstevel@tonic-gate mutex_enter(&dump_lock); 13290Sstevel@tonic-gate if (dumpvp == NULL) 13300Sstevel@tonic-gate (void) dumpinit(vp, swapname, 0); 13310Sstevel@tonic-gate mutex_exit(&dump_lock); 13320Sstevel@tonic-gate 13330Sstevel@tonic-gate VN_HOLD(cvp); 13340Sstevel@tonic-gate out: 13350Sstevel@tonic-gate if (error || esip) { 13360Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: error (%d)\n", error, 0, 0, 0, 0); 13370Sstevel@tonic-gate 13380Sstevel@tonic-gate if (!wasswap) { 13390Sstevel@tonic-gate mutex_enter(&cvp->v_lock); 13400Sstevel@tonic-gate cvp->v_flag &= ~VISSWAP; 13410Sstevel@tonic-gate mutex_exit(&cvp->v_lock); 13420Sstevel@tonic-gate } 13430Sstevel@tonic-gate if (nsip) { 13440Sstevel@tonic-gate kmem_free(nsip->si_swapslots, (size_t)nsip->si_mapsize); 13450Sstevel@tonic-gate kmem_free(nsip->si_pname, nsip->si_pnamelen); 13460Sstevel@tonic-gate kmem_free(nsip, sizeof (*nsip)); 13470Sstevel@tonic-gate } 13480Sstevel@tonic-gate mutex_enter(&swap_lock); 13495331Samw (void) VOP_CLOSE(cvp, FREAD|FWRITE, 1, (offset_t)0, CRED(), 13505665Sstans NULL); 13510Sstevel@tonic-gate mutex_exit(&swap_lock); 13520Sstevel@tonic-gate } 13530Sstevel@tonic-gate return (error); 13540Sstevel@tonic-gate } 13550Sstevel@tonic-gate 13560Sstevel@tonic-gate /* 13570Sstevel@tonic-gate * Delete a swap file. 13580Sstevel@tonic-gate */ 13590Sstevel@tonic-gate static int 13600Sstevel@tonic-gate swapdel( 13610Sstevel@tonic-gate struct vnode *vp, 13620Sstevel@tonic-gate ulong_t lowblk) /* Low block number of area to delete. */ 13630Sstevel@tonic-gate { 13640Sstevel@tonic-gate struct swapinfo **sipp, *osip = NULL; 13650Sstevel@tonic-gate struct vnode *cvp; 13660Sstevel@tonic-gate u_offset_t soff; 13670Sstevel@tonic-gate int error = 0; 13680Sstevel@tonic-gate u_offset_t toff = 0; 13690Sstevel@tonic-gate struct vnode *tvp = NULL; 13700Sstevel@tonic-gate spgcnt_t pages; 13710Sstevel@tonic-gate struct anon **app, *ap; 13720Sstevel@tonic-gate kmutex_t *ahm; 13730Sstevel@tonic-gate pgcnt_t adjust_swap = 0; 13740Sstevel@tonic-gate 13750Sstevel@tonic-gate /* Find the swap file entry for the file to be deleted */ 13760Sstevel@tonic-gate cvp = common_specvp(vp); 13770Sstevel@tonic-gate 13780Sstevel@tonic-gate 13790Sstevel@tonic-gate lowblk = lowblk ? lowblk : 1; /* Skip first page (disk label) */ 13800Sstevel@tonic-gate soff = ptob(btopr(lowblk << SCTRSHFT)); /* must be page aligned */ 13810Sstevel@tonic-gate 13820Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 13830Sstevel@tonic-gate for (sipp = &swapinfo; (osip = *sipp) != NULL; sipp = &osip->si_next) { 13840Sstevel@tonic-gate if ((osip->si_vp == cvp) && 13850Sstevel@tonic-gate (osip->si_soff == soff) && (osip->si_flags == 0)) 13860Sstevel@tonic-gate break; 13870Sstevel@tonic-gate } 13880Sstevel@tonic-gate 13890Sstevel@tonic-gate /* If the file was not found, error. */ 13900Sstevel@tonic-gate if (osip == NULL) { 13910Sstevel@tonic-gate error = EINVAL; 13920Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 13930Sstevel@tonic-gate goto out; 13940Sstevel@tonic-gate } 13950Sstevel@tonic-gate 13960Sstevel@tonic-gate pages = osip->si_npgs; 13970Sstevel@tonic-gate 13980Sstevel@tonic-gate /* 13990Sstevel@tonic-gate * Do not delete if we will be low on swap pages. 14000Sstevel@tonic-gate */ 14010Sstevel@tonic-gate mutex_enter(&anoninfo_lock); 14020Sstevel@tonic-gate 14030Sstevel@tonic-gate ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap); 14040Sstevel@tonic-gate ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv); 14050Sstevel@tonic-gate 14060Sstevel@tonic-gate mutex_enter(&freemem_lock); 14070Sstevel@tonic-gate if (((k_anoninfo.ani_max - k_anoninfo.ani_phys_resv) + 14080Sstevel@tonic-gate MAX((spgcnt_t)(availrmem - swapfs_minfree), 0)) < pages) { 14090Sstevel@tonic-gate mutex_exit(&freemem_lock); 14100Sstevel@tonic-gate mutex_exit(&anoninfo_lock); 14110Sstevel@tonic-gate error = ENOMEM; 14120Sstevel@tonic-gate cmn_err(CE_WARN, "swapdel - too few free pages"); 14130Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 14140Sstevel@tonic-gate goto out; 14150Sstevel@tonic-gate } 14160Sstevel@tonic-gate mutex_exit(&freemem_lock); 14170Sstevel@tonic-gate 14180Sstevel@tonic-gate k_anoninfo.ani_max -= pages; 14190Sstevel@tonic-gate 14200Sstevel@tonic-gate /* If needed, reserve memory swap to replace old device */ 14210Sstevel@tonic-gate if (k_anoninfo.ani_phys_resv > k_anoninfo.ani_max) { 14220Sstevel@tonic-gate adjust_swap = k_anoninfo.ani_phys_resv - k_anoninfo.ani_max; 14230Sstevel@tonic-gate k_anoninfo.ani_phys_resv -= adjust_swap; 14240Sstevel@tonic-gate k_anoninfo.ani_mem_resv += adjust_swap; 14250Sstevel@tonic-gate mutex_enter(&freemem_lock); 14260Sstevel@tonic-gate availrmem -= adjust_swap; 14270Sstevel@tonic-gate mutex_exit(&freemem_lock); 14280Sstevel@tonic-gate ANI_ADD(adjust_swap); 14290Sstevel@tonic-gate } 14300Sstevel@tonic-gate ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap); 14310Sstevel@tonic-gate ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv); 14320Sstevel@tonic-gate mutex_exit(&anoninfo_lock); 14330Sstevel@tonic-gate 14340Sstevel@tonic-gate ANI_ADD(-pages); 14350Sstevel@tonic-gate 14360Sstevel@tonic-gate /* 14370Sstevel@tonic-gate * Set the delete flag. This prevents anyone from allocating more 14380Sstevel@tonic-gate * pages from this file. Also set ST_DOINGDEL. Someone who wants to 14390Sstevel@tonic-gate * add the file back while we're deleting it will signify by clearing 14400Sstevel@tonic-gate * this flag. 14410Sstevel@tonic-gate */ 14420Sstevel@tonic-gate osip->si_flags |= ST_INDEL|ST_DOINGDEL; 14430Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 14440Sstevel@tonic-gate 14450Sstevel@tonic-gate /* 14460Sstevel@tonic-gate * Free all the allocated physical slots for this file. We do this 14470Sstevel@tonic-gate * by walking through the entire anon hash array, because we need 14480Sstevel@tonic-gate * to update all the anon slots that have physical swap slots on 14490Sstevel@tonic-gate * this file, and this is the only way to find them all. We go back 14500Sstevel@tonic-gate * to the beginning of a bucket after each slot is freed because the 14510Sstevel@tonic-gate * anonhash_lock is not held during the free and thus the hash table 14520Sstevel@tonic-gate * may change under us. 14530Sstevel@tonic-gate */ 14540Sstevel@tonic-gate for (app = anon_hash; app < &anon_hash[ANON_HASH_SIZE]; app++) { 14550Sstevel@tonic-gate ahm = &anonhash_lock[(app-anon_hash) & (AH_LOCK_SIZE - 1)]; 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); 16150Sstevel@tonic-gate ahm = &anonhash_lock[AH_LOCK(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 */ 16840Sstevel@tonic-gate ahm = &anonhash_lock[AH_LOCK(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) { 17120Sstevel@tonic-gate ahm = &anonhash_lock[AH_LOCK(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 17820Sstevel@tonic-gate ahm = &anonhash_lock[AH_LOCK(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