1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*0Sstevel@tonic-gate /* All Rights Reserved */ 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate /* 31*0Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 32*0Sstevel@tonic-gate * The Regents of the University of California 33*0Sstevel@tonic-gate * All Rights Reserved 34*0Sstevel@tonic-gate * 35*0Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 36*0Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 37*0Sstevel@tonic-gate * contributors. 38*0Sstevel@tonic-gate */ 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate /* 43*0Sstevel@tonic-gate * Each physical swap area has an associated bitmap representing 44*0Sstevel@tonic-gate * its physical storage. The bitmap records which swap slots are 45*0Sstevel@tonic-gate * currently allocated or freed. Allocation is done by searching 46*0Sstevel@tonic-gate * through the bitmap for the first free slot. Thus, there's 47*0Sstevel@tonic-gate * no linear relation between offset within the swap device and the 48*0Sstevel@tonic-gate * address (within its segment(s)) of the page that the slot backs; 49*0Sstevel@tonic-gate * instead, it's an arbitrary one-to-one mapping. 50*0Sstevel@tonic-gate * 51*0Sstevel@tonic-gate * Associated with each swap area is a swapinfo structure. These 52*0Sstevel@tonic-gate * structures are linked into a linear list that determines the 53*0Sstevel@tonic-gate * ordering of swap areas in the logical swap device. Each contains a 54*0Sstevel@tonic-gate * pointer to the corresponding bitmap, the area's size, and its 55*0Sstevel@tonic-gate * associated vnode. 56*0Sstevel@tonic-gate */ 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate #include <sys/types.h> 59*0Sstevel@tonic-gate #include <sys/inttypes.h> 60*0Sstevel@tonic-gate #include <sys/param.h> 61*0Sstevel@tonic-gate #include <sys/t_lock.h> 62*0Sstevel@tonic-gate #include <sys/sysmacros.h> 63*0Sstevel@tonic-gate #include <sys/systm.h> 64*0Sstevel@tonic-gate #include <sys/errno.h> 65*0Sstevel@tonic-gate #include <sys/kmem.h> 66*0Sstevel@tonic-gate #include <sys/vfs.h> 67*0Sstevel@tonic-gate #include <sys/vnode.h> 68*0Sstevel@tonic-gate #include <sys/pathname.h> 69*0Sstevel@tonic-gate #include <sys/cmn_err.h> 70*0Sstevel@tonic-gate #include <sys/vtrace.h> 71*0Sstevel@tonic-gate #include <sys/swap.h> 72*0Sstevel@tonic-gate #include <sys/dumphdr.h> 73*0Sstevel@tonic-gate #include <sys/debug.h> 74*0Sstevel@tonic-gate #include <sys/fs/snode.h> 75*0Sstevel@tonic-gate #include <sys/fs/swapnode.h> 76*0Sstevel@tonic-gate #include <sys/policy.h> 77*0Sstevel@tonic-gate #include <sys/zone.h> 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate #include <vm/as.h> 80*0Sstevel@tonic-gate #include <vm/seg.h> 81*0Sstevel@tonic-gate #include <vm/page.h> 82*0Sstevel@tonic-gate #include <vm/seg_vn.h> 83*0Sstevel@tonic-gate #include <vm/hat.h> 84*0Sstevel@tonic-gate #include <vm/anon.h> 85*0Sstevel@tonic-gate #include <vm/seg_map.h> 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate /* 88*0Sstevel@tonic-gate * To balance the load among multiple swap areas, we don't allow 89*0Sstevel@tonic-gate * more than swap_maxcontig allocations to be satisfied from a 90*0Sstevel@tonic-gate * single swap area before moving on to the next swap area. This 91*0Sstevel@tonic-gate * effectively "interleaves" allocations among the many swap areas. 92*0Sstevel@tonic-gate */ 93*0Sstevel@tonic-gate int swap_maxcontig; /* set by anon_init() to 1 Mb */ 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate #define MINIROOTSIZE 12000 /* ~6 Meg XXX */ 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gate /* 98*0Sstevel@tonic-gate * XXX - this lock is a kludge. It serializes some aspects of swapadd() and 99*0Sstevel@tonic-gate * swapdel() (namely VOP_OPEN, VOP_CLOSE, VN_RELE). It protects against 100*0Sstevel@tonic-gate * somebody swapadd'ing and getting swap slots from a vnode, while someone 101*0Sstevel@tonic-gate * else is in the process of closing or rele'ing it. 102*0Sstevel@tonic-gate */ 103*0Sstevel@tonic-gate static kmutex_t swap_lock; 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate kmutex_t swapinfo_lock; 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate /* 108*0Sstevel@tonic-gate * protected by the swapinfo_lock 109*0Sstevel@tonic-gate */ 110*0Sstevel@tonic-gate struct swapinfo *swapinfo; 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate static struct swapinfo *silast; 113*0Sstevel@tonic-gate static int nswapfiles; 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate static u_offset_t swap_getoff(struct swapinfo *); 116*0Sstevel@tonic-gate static int swapadd(struct vnode *, ulong_t, ulong_t, char *); 117*0Sstevel@tonic-gate static int swapdel(struct vnode *, ulong_t); 118*0Sstevel@tonic-gate static int swapslot_free(struct vnode *, u_offset_t, struct swapinfo *); 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate /* 121*0Sstevel@tonic-gate * swap device bitmap allocation macros 122*0Sstevel@tonic-gate */ 123*0Sstevel@tonic-gate #define MAPSHIFT 5 124*0Sstevel@tonic-gate #define NBBW (NBPW * NBBY) /* number of bits per word */ 125*0Sstevel@tonic-gate #define TESTBIT(map, i) (((map)[(i) >> MAPSHIFT] & (1 << (i) % NBBW))) 126*0Sstevel@tonic-gate #define SETBIT(map, i) (((map)[(i) >> MAPSHIFT] |= (1 << (i) % NBBW))) 127*0Sstevel@tonic-gate #define CLEARBIT(map, i) (((map)[(i) >> MAPSHIFT] &= ~(1 << (i) % NBBW))) 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate int swap_debug = 0; /* set for debug printf's */ 130*0Sstevel@tonic-gate int swap_verify = 0; /* set to verify slots when freeing and allocating */ 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate uint_t swapalloc_maxcontig; 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate /* 135*0Sstevel@tonic-gate * Allocate a range of up to *lenp contiguous slots (page) from a physical 136*0Sstevel@tonic-gate * swap device. Flags are one of: 137*0Sstevel@tonic-gate * SA_NOT Must have a slot from a physical swap device other than the 138*0Sstevel@tonic-gate * the one containing input (*vpp, *offp). 139*0Sstevel@tonic-gate * Less slots than requested may be returned. *lenp allocated slots are 140*0Sstevel@tonic-gate * returned starting at *offp on *vpp. 141*0Sstevel@tonic-gate * Returns 1 for a successful allocation, 0 for couldn't allocate any slots. 142*0Sstevel@tonic-gate */ 143*0Sstevel@tonic-gate int 144*0Sstevel@tonic-gate swap_phys_alloc( 145*0Sstevel@tonic-gate struct vnode **vpp, 146*0Sstevel@tonic-gate u_offset_t *offp, 147*0Sstevel@tonic-gate size_t *lenp, 148*0Sstevel@tonic-gate uint_t flags) 149*0Sstevel@tonic-gate { 150*0Sstevel@tonic-gate struct swapinfo *sip; 151*0Sstevel@tonic-gate offset_t soff, noff; 152*0Sstevel@tonic-gate size_t len; 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 155*0Sstevel@tonic-gate sip = silast; 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gate /* Find a desirable physical device and allocate from it. */ 158*0Sstevel@tonic-gate do { 159*0Sstevel@tonic-gate if (sip == NULL) 160*0Sstevel@tonic-gate break; 161*0Sstevel@tonic-gate if (!(sip->si_flags & ST_INDEL) && 162*0Sstevel@tonic-gate (spgcnt_t)sip->si_nfpgs > 0) { 163*0Sstevel@tonic-gate /* Caller wants other than specified swap device */ 164*0Sstevel@tonic-gate if (flags & SA_NOT) { 165*0Sstevel@tonic-gate if (*vpp != sip->si_vp || 166*0Sstevel@tonic-gate *offp < sip->si_soff || 167*0Sstevel@tonic-gate *offp >= sip->si_eoff) 168*0Sstevel@tonic-gate goto found; 169*0Sstevel@tonic-gate /* Caller is loose, will take anything */ 170*0Sstevel@tonic-gate } else 171*0Sstevel@tonic-gate goto found; 172*0Sstevel@tonic-gate } else if (sip->si_nfpgs == 0) 173*0Sstevel@tonic-gate sip->si_allocs = 0; 174*0Sstevel@tonic-gate if ((sip = sip->si_next) == NULL) 175*0Sstevel@tonic-gate sip = swapinfo; 176*0Sstevel@tonic-gate } while (sip != silast); 177*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 178*0Sstevel@tonic-gate return (0); 179*0Sstevel@tonic-gate found: 180*0Sstevel@tonic-gate soff = swap_getoff(sip); 181*0Sstevel@tonic-gate sip->si_nfpgs--; 182*0Sstevel@tonic-gate if (soff == -1) 183*0Sstevel@tonic-gate panic("swap_alloc: swap_getoff failed!"); 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate for (len = PAGESIZE; len < *lenp; len += PAGESIZE) { 186*0Sstevel@tonic-gate if (sip->si_nfpgs == 0) 187*0Sstevel@tonic-gate break; 188*0Sstevel@tonic-gate if (swapalloc_maxcontig && len >= swapalloc_maxcontig) 189*0Sstevel@tonic-gate break; 190*0Sstevel@tonic-gate noff = swap_getoff(sip); 191*0Sstevel@tonic-gate if (noff == -1) { 192*0Sstevel@tonic-gate break; 193*0Sstevel@tonic-gate } else if (noff != soff + len) { 194*0Sstevel@tonic-gate CLEARBIT(sip->si_swapslots, btop(noff - sip->si_soff)); 195*0Sstevel@tonic-gate break; 196*0Sstevel@tonic-gate } 197*0Sstevel@tonic-gate sip->si_nfpgs--; 198*0Sstevel@tonic-gate } 199*0Sstevel@tonic-gate *vpp = sip->si_vp; 200*0Sstevel@tonic-gate *offp = soff; 201*0Sstevel@tonic-gate *lenp = len; 202*0Sstevel@tonic-gate ASSERT((spgcnt_t)sip->si_nfpgs >= 0); 203*0Sstevel@tonic-gate sip->si_allocs += btop(len); 204*0Sstevel@tonic-gate if (sip->si_allocs >= swap_maxcontig) { 205*0Sstevel@tonic-gate sip->si_allocs = 0; 206*0Sstevel@tonic-gate if ((silast = sip->si_next) == NULL) 207*0Sstevel@tonic-gate silast = swapinfo; 208*0Sstevel@tonic-gate } 209*0Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_SWAP_ALLOC, 210*0Sstevel@tonic-gate "swap_alloc:sip %p offset %lx", sip, soff); 211*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 212*0Sstevel@tonic-gate return (1); 213*0Sstevel@tonic-gate } 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gate int swap_backsearch = 0; 216*0Sstevel@tonic-gate 217*0Sstevel@tonic-gate /* 218*0Sstevel@tonic-gate * Get a free offset on swap device sip. 219*0Sstevel@tonic-gate * Return >=0 offset if succeeded, -1 for failure. 220*0Sstevel@tonic-gate */ 221*0Sstevel@tonic-gate static u_offset_t 222*0Sstevel@tonic-gate swap_getoff(struct swapinfo *sip) 223*0Sstevel@tonic-gate { 224*0Sstevel@tonic-gate uint_t *sp, *ep; 225*0Sstevel@tonic-gate size_t aoff, boff, poff, slotnumber; 226*0Sstevel@tonic-gate 227*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&swapinfo_lock)); 228*0Sstevel@tonic-gate 229*0Sstevel@tonic-gate sip->si_alloccnt++; 230*0Sstevel@tonic-gate for (sp = &sip->si_swapslots[sip->si_hint >> MAPSHIFT], 231*0Sstevel@tonic-gate ep = &sip->si_swapslots[sip->si_mapsize / NBPW]; sp < ep; sp++) { 232*0Sstevel@tonic-gate if (*sp != (uint_t)0xffffffff) 233*0Sstevel@tonic-gate goto foundentry; 234*0Sstevel@tonic-gate else 235*0Sstevel@tonic-gate sip->si_checkcnt++; 236*0Sstevel@tonic-gate } 237*0Sstevel@tonic-gate SWAP_PRINT(SW_ALLOC, 238*0Sstevel@tonic-gate "swap_getoff: couldn't find slot from hint %ld to end\n", 239*0Sstevel@tonic-gate sip->si_hint, 0, 0, 0, 0); 240*0Sstevel@tonic-gate /* 241*0Sstevel@tonic-gate * Go backwards? Check for faster method XXX 242*0Sstevel@tonic-gate */ 243*0Sstevel@tonic-gate if (swap_backsearch) { 244*0Sstevel@tonic-gate for (sp = &sip->si_swapslots[sip->si_hint >> MAPSHIFT], 245*0Sstevel@tonic-gate ep = sip->si_swapslots; sp > ep; sp--) { 246*0Sstevel@tonic-gate if (*sp != (uint_t)0xffffffff) 247*0Sstevel@tonic-gate goto foundentry; 248*0Sstevel@tonic-gate else 249*0Sstevel@tonic-gate sip->si_checkcnt++; 250*0Sstevel@tonic-gate } 251*0Sstevel@tonic-gate } else { 252*0Sstevel@tonic-gate for (sp = sip->si_swapslots, 253*0Sstevel@tonic-gate ep = &sip->si_swapslots[sip->si_hint >> MAPSHIFT]; 254*0Sstevel@tonic-gate sp < ep; sp++) { 255*0Sstevel@tonic-gate if (*sp != (uint_t)0xffffffff) 256*0Sstevel@tonic-gate goto foundentry; 257*0Sstevel@tonic-gate else 258*0Sstevel@tonic-gate sip->si_checkcnt++; 259*0Sstevel@tonic-gate } 260*0Sstevel@tonic-gate } 261*0Sstevel@tonic-gate if (*sp == 0xffffffff) { 262*0Sstevel@tonic-gate cmn_err(CE_WARN, "No free swap slots!"); 263*0Sstevel@tonic-gate return ((u_offset_t)-1); 264*0Sstevel@tonic-gate } 265*0Sstevel@tonic-gate 266*0Sstevel@tonic-gate foundentry: 267*0Sstevel@tonic-gate /* 268*0Sstevel@tonic-gate * aoff is the page number offset (in bytes) of the si_swapslots 269*0Sstevel@tonic-gate * array element containing a free page 270*0Sstevel@tonic-gate * 271*0Sstevel@tonic-gate * boff is the page number offset of the free page 272*0Sstevel@tonic-gate * (i.e. cleared bit) in si_swapslots[aoff]. 273*0Sstevel@tonic-gate */ 274*0Sstevel@tonic-gate aoff = ((char *)sp - (char *)sip->si_swapslots) * NBBY; 275*0Sstevel@tonic-gate 276*0Sstevel@tonic-gate for (boff = (sip->si_hint % NBBW); boff < NBBW; boff++) { 277*0Sstevel@tonic-gate if (!TESTBIT(sip->si_swapslots, aoff + boff)) 278*0Sstevel@tonic-gate goto foundslot; 279*0Sstevel@tonic-gate else 280*0Sstevel@tonic-gate sip->si_checkcnt++; 281*0Sstevel@tonic-gate } 282*0Sstevel@tonic-gate for (boff = 0; boff < (sip->si_hint % NBBW); boff++) { 283*0Sstevel@tonic-gate if (!TESTBIT(sip->si_swapslots, aoff + boff)) 284*0Sstevel@tonic-gate goto foundslot; 285*0Sstevel@tonic-gate else 286*0Sstevel@tonic-gate sip->si_checkcnt++; 287*0Sstevel@tonic-gate } 288*0Sstevel@tonic-gate panic("swap_getoff: didn't find slot in word hint %ld", sip->si_hint); 289*0Sstevel@tonic-gate 290*0Sstevel@tonic-gate foundslot: 291*0Sstevel@tonic-gate /* 292*0Sstevel@tonic-gate * Return the offset of the free page in swap device. 293*0Sstevel@tonic-gate * Convert page number of byte offset and add starting 294*0Sstevel@tonic-gate * offset of swap device. 295*0Sstevel@tonic-gate */ 296*0Sstevel@tonic-gate slotnumber = aoff + boff; 297*0Sstevel@tonic-gate SWAP_PRINT(SW_ALLOC, "swap_getoff: allocating slot %ld\n", 298*0Sstevel@tonic-gate slotnumber, 0, 0, 0, 0); 299*0Sstevel@tonic-gate poff = ptob(slotnumber); 300*0Sstevel@tonic-gate if (poff + sip->si_soff >= sip->si_eoff) 301*0Sstevel@tonic-gate printf("ptob(aoff(%ld) + boff(%ld))(%ld) >= eoff(%ld)\n", 302*0Sstevel@tonic-gate aoff, boff, ptob(slotnumber), (long)sip->si_eoff); 303*0Sstevel@tonic-gate ASSERT(poff < sip->si_eoff); 304*0Sstevel@tonic-gate /* 305*0Sstevel@tonic-gate * We could verify here that the slot isn't already allocated 306*0Sstevel@tonic-gate * by looking through all the anon slots. 307*0Sstevel@tonic-gate */ 308*0Sstevel@tonic-gate SETBIT(sip->si_swapslots, slotnumber); 309*0Sstevel@tonic-gate sip->si_hint = slotnumber + 1; /* hint = next slot */ 310*0Sstevel@tonic-gate return (poff + sip->si_soff); 311*0Sstevel@tonic-gate } 312*0Sstevel@tonic-gate 313*0Sstevel@tonic-gate /* 314*0Sstevel@tonic-gate * Free a swap page. 315*0Sstevel@tonic-gate */ 316*0Sstevel@tonic-gate void 317*0Sstevel@tonic-gate swap_phys_free(struct vnode *vp, u_offset_t off, size_t len) 318*0Sstevel@tonic-gate { 319*0Sstevel@tonic-gate struct swapinfo *sip; 320*0Sstevel@tonic-gate ssize_t pagenumber, npage; 321*0Sstevel@tonic-gate 322*0Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 323*0Sstevel@tonic-gate sip = swapinfo; 324*0Sstevel@tonic-gate 325*0Sstevel@tonic-gate do { 326*0Sstevel@tonic-gate if (sip->si_vp == vp && 327*0Sstevel@tonic-gate sip->si_soff <= off && off < sip->si_eoff) { 328*0Sstevel@tonic-gate for (pagenumber = btop(off - sip->si_soff), 329*0Sstevel@tonic-gate npage = btop(len) + pagenumber; 330*0Sstevel@tonic-gate pagenumber < npage; pagenumber++) { 331*0Sstevel@tonic-gate SWAP_PRINT(SW_ALLOC, 332*0Sstevel@tonic-gate "swap_phys_free: freeing slot %ld on " 333*0Sstevel@tonic-gate "sip %p\n", 334*0Sstevel@tonic-gate pagenumber, sip, 0, 0, 0); 335*0Sstevel@tonic-gate if (!TESTBIT(sip->si_swapslots, pagenumber)) { 336*0Sstevel@tonic-gate panic( 337*0Sstevel@tonic-gate "swap_phys_free: freeing free slot " 338*0Sstevel@tonic-gate "%p,%lx\n", (void *)vp, 339*0Sstevel@tonic-gate ptob(pagenumber) + sip->si_soff); 340*0Sstevel@tonic-gate } 341*0Sstevel@tonic-gate CLEARBIT(sip->si_swapslots, pagenumber); 342*0Sstevel@tonic-gate sip->si_nfpgs++; 343*0Sstevel@tonic-gate } 344*0Sstevel@tonic-gate ASSERT(sip->si_nfpgs <= sip->si_npgs); 345*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 346*0Sstevel@tonic-gate return; 347*0Sstevel@tonic-gate } 348*0Sstevel@tonic-gate } while ((sip = sip->si_next) != NULL); 349*0Sstevel@tonic-gate panic("swap_phys_free"); 350*0Sstevel@tonic-gate /*NOTREACHED*/ 351*0Sstevel@tonic-gate } 352*0Sstevel@tonic-gate 353*0Sstevel@tonic-gate /* 354*0Sstevel@tonic-gate * Return the anon struct corresponding for the given 355*0Sstevel@tonic-gate * <vnode, off> if it is part of the virtual swap device. 356*0Sstevel@tonic-gate * Return the anon struct if found, otherwise NULL. 357*0Sstevel@tonic-gate */ 358*0Sstevel@tonic-gate struct anon * 359*0Sstevel@tonic-gate swap_anon(struct vnode *vp, u_offset_t off) 360*0Sstevel@tonic-gate { 361*0Sstevel@tonic-gate struct anon *ap; 362*0Sstevel@tonic-gate 363*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&anonhash_lock[AH_LOCK(vp, off)])); 364*0Sstevel@tonic-gate 365*0Sstevel@tonic-gate for (ap = anon_hash[ANON_HASH(vp, off)]; ap != NULL; ap = ap->an_hash) { 366*0Sstevel@tonic-gate if (ap->an_vp == vp && ap->an_off == off) 367*0Sstevel@tonic-gate return (ap); 368*0Sstevel@tonic-gate } 369*0Sstevel@tonic-gate return (NULL); 370*0Sstevel@tonic-gate } 371*0Sstevel@tonic-gate 372*0Sstevel@tonic-gate 373*0Sstevel@tonic-gate /* 374*0Sstevel@tonic-gate * Determine if the vp offset range overlap a swap device. 375*0Sstevel@tonic-gate */ 376*0Sstevel@tonic-gate int 377*0Sstevel@tonic-gate swap_in_range(struct vnode *vp, u_offset_t offset, size_t len) 378*0Sstevel@tonic-gate { 379*0Sstevel@tonic-gate struct swapinfo *sip; 380*0Sstevel@tonic-gate u_offset_t eoff; 381*0Sstevel@tonic-gate 382*0Sstevel@tonic-gate eoff = offset + len; 383*0Sstevel@tonic-gate ASSERT(eoff > offset); 384*0Sstevel@tonic-gate 385*0Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 386*0Sstevel@tonic-gate sip = swapinfo; 387*0Sstevel@tonic-gate if (vp && sip) { 388*0Sstevel@tonic-gate do { 389*0Sstevel@tonic-gate if (vp != sip->si_vp || eoff <= sip->si_soff || 390*0Sstevel@tonic-gate offset >= sip->si_eoff) 391*0Sstevel@tonic-gate continue; 392*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 393*0Sstevel@tonic-gate return (1); 394*0Sstevel@tonic-gate } while ((sip = sip->si_next) != NULL); 395*0Sstevel@tonic-gate } 396*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 397*0Sstevel@tonic-gate return (0); 398*0Sstevel@tonic-gate } 399*0Sstevel@tonic-gate 400*0Sstevel@tonic-gate /* 401*0Sstevel@tonic-gate * See if name is one of our swap files 402*0Sstevel@tonic-gate * even though lookupname failed. 403*0Sstevel@tonic-gate * This can be used by swapdel to delete 404*0Sstevel@tonic-gate * swap resources on remote machines 405*0Sstevel@tonic-gate * where the link has gone down. 406*0Sstevel@tonic-gate */ 407*0Sstevel@tonic-gate static struct vnode * 408*0Sstevel@tonic-gate swapdel_byname( 409*0Sstevel@tonic-gate char *name, /* pathname to delete */ 410*0Sstevel@tonic-gate ulong_t lowblk) /* Low block number of area to delete */ 411*0Sstevel@tonic-gate { 412*0Sstevel@tonic-gate struct swapinfo **sipp, *osip; 413*0Sstevel@tonic-gate u_offset_t soff; 414*0Sstevel@tonic-gate 415*0Sstevel@tonic-gate /* 416*0Sstevel@tonic-gate * Find the swap file entry for the file to 417*0Sstevel@tonic-gate * be deleted. Skip any entries that are in 418*0Sstevel@tonic-gate * transition. 419*0Sstevel@tonic-gate */ 420*0Sstevel@tonic-gate 421*0Sstevel@tonic-gate soff = ptob(btopr(lowblk << SCTRSHFT)); /* must be page aligned */ 422*0Sstevel@tonic-gate 423*0Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 424*0Sstevel@tonic-gate for (sipp = &swapinfo; (osip = *sipp) != NULL; sipp = &osip->si_next) { 425*0Sstevel@tonic-gate if ((strcmp(osip->si_pname, name) == 0) && 426*0Sstevel@tonic-gate (osip->si_soff == soff) && (osip->si_flags == 0)) { 427*0Sstevel@tonic-gate struct vnode *vp = osip->si_vp; 428*0Sstevel@tonic-gate 429*0Sstevel@tonic-gate VN_HOLD(vp); 430*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 431*0Sstevel@tonic-gate return (vp); 432*0Sstevel@tonic-gate } 433*0Sstevel@tonic-gate } 434*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 435*0Sstevel@tonic-gate return (NULL); 436*0Sstevel@tonic-gate } 437*0Sstevel@tonic-gate 438*0Sstevel@tonic-gate 439*0Sstevel@tonic-gate /* 440*0Sstevel@tonic-gate * New system call to manipulate swap files. 441*0Sstevel@tonic-gate */ 442*0Sstevel@tonic-gate int 443*0Sstevel@tonic-gate swapctl(int sc_cmd, void *sc_arg, int *rv) 444*0Sstevel@tonic-gate { 445*0Sstevel@tonic-gate struct swapinfo *sip, *csip, *tsip; 446*0Sstevel@tonic-gate int error = 0; 447*0Sstevel@tonic-gate struct swapent st, *ust; 448*0Sstevel@tonic-gate struct swapres sr; 449*0Sstevel@tonic-gate struct vnode *vp; 450*0Sstevel@tonic-gate int cnt = 0; 451*0Sstevel@tonic-gate int tmp_nswapfiles; 452*0Sstevel@tonic-gate int nswap; 453*0Sstevel@tonic-gate int length, nlen; 454*0Sstevel@tonic-gate int gplen = 0, plen; 455*0Sstevel@tonic-gate char *swapname; 456*0Sstevel@tonic-gate char *pname; 457*0Sstevel@tonic-gate char *tpname; 458*0Sstevel@tonic-gate struct anoninfo ai; 459*0Sstevel@tonic-gate spgcnt_t avail; 460*0Sstevel@tonic-gate int global = INGLOBALZONE(curproc); 461*0Sstevel@tonic-gate 462*0Sstevel@tonic-gate /* 463*0Sstevel@tonic-gate * When running in a zone we want to hide the details of the swap 464*0Sstevel@tonic-gate * devices: we report there only being one swap device named "swap" 465*0Sstevel@tonic-gate * having a size equal to the sum of the sizes of all real swap devices 466*0Sstevel@tonic-gate * on the system. 467*0Sstevel@tonic-gate */ 468*0Sstevel@tonic-gate switch (sc_cmd) { 469*0Sstevel@tonic-gate case SC_GETNSWP: 470*0Sstevel@tonic-gate if (global) 471*0Sstevel@tonic-gate *rv = nswapfiles; 472*0Sstevel@tonic-gate else 473*0Sstevel@tonic-gate *rv = 1; 474*0Sstevel@tonic-gate return (0); 475*0Sstevel@tonic-gate 476*0Sstevel@tonic-gate case SC_AINFO: 477*0Sstevel@tonic-gate /* 478*0Sstevel@tonic-gate * Return anoninfo information with these changes: 479*0Sstevel@tonic-gate * ani_max = maximum amount of swap space 480*0Sstevel@tonic-gate * (including potentially available physical memory) 481*0Sstevel@tonic-gate * ani_free = amount of unallocated anonymous memory 482*0Sstevel@tonic-gate * (some of which might be reserved and including 483*0Sstevel@tonic-gate * potentially available physical memory) 484*0Sstevel@tonic-gate * ani_resv = amount of claimed (reserved) anonymous memory 485*0Sstevel@tonic-gate */ 486*0Sstevel@tonic-gate avail = MAX((spgcnt_t)(availrmem - swapfs_minfree), 0); 487*0Sstevel@tonic-gate ai.ani_max = (k_anoninfo.ani_max + 488*0Sstevel@tonic-gate k_anoninfo.ani_mem_resv) +avail; 489*0Sstevel@tonic-gate 490*0Sstevel@tonic-gate ai.ani_free = k_anoninfo.ani_free + avail; 491*0Sstevel@tonic-gate 492*0Sstevel@tonic-gate ai.ani_resv = k_anoninfo.ani_phys_resv + 493*0Sstevel@tonic-gate k_anoninfo.ani_mem_resv; 494*0Sstevel@tonic-gate 495*0Sstevel@tonic-gate if (copyout(&ai, sc_arg, sizeof (struct anoninfo)) != 0) 496*0Sstevel@tonic-gate return (EFAULT); 497*0Sstevel@tonic-gate return (0); 498*0Sstevel@tonic-gate 499*0Sstevel@tonic-gate case SC_LIST: 500*0Sstevel@tonic-gate if (copyin(sc_arg, &length, sizeof (int)) != 0) 501*0Sstevel@tonic-gate return (EFAULT); 502*0Sstevel@tonic-gate if (!global) { 503*0Sstevel@tonic-gate struct swapent st; 504*0Sstevel@tonic-gate char *swappath = "swap"; 505*0Sstevel@tonic-gate 506*0Sstevel@tonic-gate if (length < 1) 507*0Sstevel@tonic-gate return (ENOMEM); 508*0Sstevel@tonic-gate ust = (swapent_t *)((swaptbl_t *)sc_arg)->swt_ent; 509*0Sstevel@tonic-gate if (copyin(ust, &st, sizeof (swapent_t)) != 0) 510*0Sstevel@tonic-gate return (EFAULT); 511*0Sstevel@tonic-gate st.ste_start = PAGESIZE >> SCTRSHFT; 512*0Sstevel@tonic-gate st.ste_length = (off_t)0; 513*0Sstevel@tonic-gate st.ste_pages = 0; 514*0Sstevel@tonic-gate st.ste_free = 0; 515*0Sstevel@tonic-gate st.ste_flags = 0; 516*0Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 517*0Sstevel@tonic-gate for (sip = swapinfo, nswap = 0; 518*0Sstevel@tonic-gate sip != NULL && nswap < nswapfiles; 519*0Sstevel@tonic-gate sip = sip->si_next, nswap++) { 520*0Sstevel@tonic-gate st.ste_length += 521*0Sstevel@tonic-gate (sip->si_eoff - sip->si_soff) >> SCTRSHFT; 522*0Sstevel@tonic-gate st.ste_pages += sip->si_npgs; 523*0Sstevel@tonic-gate st.ste_free += sip->si_nfpgs; 524*0Sstevel@tonic-gate } 525*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 526*0Sstevel@tonic-gate if (copyout(&st, ust, sizeof (swapent_t)) != 0 || 527*0Sstevel@tonic-gate copyout(swappath, st.ste_path, 528*0Sstevel@tonic-gate strlen(swappath) + 1) != 0) { 529*0Sstevel@tonic-gate return (EFAULT); 530*0Sstevel@tonic-gate } 531*0Sstevel@tonic-gate *rv = 1; 532*0Sstevel@tonic-gate return (0); 533*0Sstevel@tonic-gate } 534*0Sstevel@tonic-gate beginning: 535*0Sstevel@tonic-gate tmp_nswapfiles = nswapfiles; 536*0Sstevel@tonic-gate /* Return an error if not enough space for the whole table. */ 537*0Sstevel@tonic-gate if (length < tmp_nswapfiles) 538*0Sstevel@tonic-gate return (ENOMEM); 539*0Sstevel@tonic-gate /* 540*0Sstevel@tonic-gate * Get memory to hold the swap entries and their names. We'll 541*0Sstevel@tonic-gate * copy the real entries into these and then copy these out. 542*0Sstevel@tonic-gate * Allocating the pathname memory is only a guess so we may 543*0Sstevel@tonic-gate * find that we need more and have to do it again. 544*0Sstevel@tonic-gate * All this is because we have to hold the anon lock while 545*0Sstevel@tonic-gate * traversing the swapinfo list, and we can't be doing copyouts 546*0Sstevel@tonic-gate * and/or kmem_alloc()s during this. 547*0Sstevel@tonic-gate */ 548*0Sstevel@tonic-gate csip = kmem_zalloc(tmp_nswapfiles * sizeof (struct swapinfo), 549*0Sstevel@tonic-gate KM_SLEEP); 550*0Sstevel@tonic-gate retry: 551*0Sstevel@tonic-gate nlen = tmp_nswapfiles * (gplen += 100); 552*0Sstevel@tonic-gate pname = kmem_zalloc(nlen, KM_SLEEP); 553*0Sstevel@tonic-gate 554*0Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 555*0Sstevel@tonic-gate 556*0Sstevel@tonic-gate if (tmp_nswapfiles != nswapfiles) { 557*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 558*0Sstevel@tonic-gate kmem_free(pname, nlen); 559*0Sstevel@tonic-gate kmem_free(csip, 560*0Sstevel@tonic-gate tmp_nswapfiles * sizeof (struct swapinfo)); 561*0Sstevel@tonic-gate gplen = 0; 562*0Sstevel@tonic-gate goto beginning; 563*0Sstevel@tonic-gate } 564*0Sstevel@tonic-gate for (sip = swapinfo, tsip = csip, tpname = pname, nswap = 0; 565*0Sstevel@tonic-gate sip && nswap < tmp_nswapfiles; 566*0Sstevel@tonic-gate sip = sip->si_next, tsip++, tpname += plen, nswap++) { 567*0Sstevel@tonic-gate plen = sip->si_pnamelen; 568*0Sstevel@tonic-gate if (tpname + plen - pname > nlen) { 569*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 570*0Sstevel@tonic-gate kmem_free(pname, nlen); 571*0Sstevel@tonic-gate goto retry; 572*0Sstevel@tonic-gate } 573*0Sstevel@tonic-gate *tsip = *sip; 574*0Sstevel@tonic-gate tsip->si_pname = tpname; 575*0Sstevel@tonic-gate (void) strcpy(tsip->si_pname, sip->si_pname); 576*0Sstevel@tonic-gate } 577*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 578*0Sstevel@tonic-gate 579*0Sstevel@tonic-gate if (sip) { 580*0Sstevel@tonic-gate error = ENOMEM; 581*0Sstevel@tonic-gate goto lout; 582*0Sstevel@tonic-gate } 583*0Sstevel@tonic-gate ust = (swapent_t *)((swaptbl_t *)sc_arg)->swt_ent; 584*0Sstevel@tonic-gate for (tsip = csip, cnt = 0; cnt < nswap; tsip++, ust++, cnt++) { 585*0Sstevel@tonic-gate if (copyin(ust, &st, sizeof (swapent_t)) != 0) { 586*0Sstevel@tonic-gate error = EFAULT; 587*0Sstevel@tonic-gate goto lout; 588*0Sstevel@tonic-gate } 589*0Sstevel@tonic-gate st.ste_flags = tsip->si_flags; 590*0Sstevel@tonic-gate st.ste_length = 591*0Sstevel@tonic-gate (tsip->si_eoff - tsip->si_soff) >> SCTRSHFT; 592*0Sstevel@tonic-gate st.ste_start = tsip->si_soff >> SCTRSHFT; 593*0Sstevel@tonic-gate st.ste_pages = tsip->si_npgs; 594*0Sstevel@tonic-gate st.ste_free = tsip->si_nfpgs; 595*0Sstevel@tonic-gate if (copyout(&st, ust, sizeof (swapent_t)) != 0) { 596*0Sstevel@tonic-gate error = EFAULT; 597*0Sstevel@tonic-gate goto lout; 598*0Sstevel@tonic-gate } 599*0Sstevel@tonic-gate if (!tsip->si_pnamelen) 600*0Sstevel@tonic-gate continue; 601*0Sstevel@tonic-gate if (copyout(tsip->si_pname, st.ste_path, 602*0Sstevel@tonic-gate tsip->si_pnamelen) != 0) { 603*0Sstevel@tonic-gate error = EFAULT; 604*0Sstevel@tonic-gate goto lout; 605*0Sstevel@tonic-gate } 606*0Sstevel@tonic-gate } 607*0Sstevel@tonic-gate *rv = nswap; 608*0Sstevel@tonic-gate lout: 609*0Sstevel@tonic-gate kmem_free(csip, tmp_nswapfiles * sizeof (struct swapinfo)); 610*0Sstevel@tonic-gate kmem_free(pname, nlen); 611*0Sstevel@tonic-gate return (error); 612*0Sstevel@tonic-gate 613*0Sstevel@tonic-gate case SC_ADD: 614*0Sstevel@tonic-gate case SC_REMOVE: 615*0Sstevel@tonic-gate break; 616*0Sstevel@tonic-gate default: 617*0Sstevel@tonic-gate return (EINVAL); 618*0Sstevel@tonic-gate } 619*0Sstevel@tonic-gate if ((error = secpolicy_swapctl(CRED())) != 0) 620*0Sstevel@tonic-gate return (error); 621*0Sstevel@tonic-gate 622*0Sstevel@tonic-gate if (copyin(sc_arg, &sr, sizeof (swapres_t))) 623*0Sstevel@tonic-gate return (EFAULT); 624*0Sstevel@tonic-gate 625*0Sstevel@tonic-gate /* Allocate the space to read in pathname */ 626*0Sstevel@tonic-gate if ((swapname = kmem_alloc(MAXPATHLEN, KM_NOSLEEP)) == NULL) 627*0Sstevel@tonic-gate return (ENOMEM); 628*0Sstevel@tonic-gate 629*0Sstevel@tonic-gate error = copyinstr(sr.sr_name, swapname, MAXPATHLEN, 0); 630*0Sstevel@tonic-gate if (error) 631*0Sstevel@tonic-gate goto out; 632*0Sstevel@tonic-gate 633*0Sstevel@tonic-gate error = lookupname(swapname, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp); 634*0Sstevel@tonic-gate if (error) { 635*0Sstevel@tonic-gate if (sc_cmd == SC_ADD) 636*0Sstevel@tonic-gate goto out; 637*0Sstevel@tonic-gate /* see if we match by name */ 638*0Sstevel@tonic-gate vp = swapdel_byname(swapname, (size_t)sr.sr_start); 639*0Sstevel@tonic-gate if (vp == NULL) 640*0Sstevel@tonic-gate goto out; 641*0Sstevel@tonic-gate } 642*0Sstevel@tonic-gate 643*0Sstevel@tonic-gate if (vp->v_flag & (VNOMAP | VNOSWAP)) { 644*0Sstevel@tonic-gate VN_RELE(vp); 645*0Sstevel@tonic-gate error = ENOSYS; 646*0Sstevel@tonic-gate goto out; 647*0Sstevel@tonic-gate } 648*0Sstevel@tonic-gate switch (vp->v_type) { 649*0Sstevel@tonic-gate case VBLK: 650*0Sstevel@tonic-gate break; 651*0Sstevel@tonic-gate 652*0Sstevel@tonic-gate case VREG: 653*0Sstevel@tonic-gate if (vp->v_vfsp && vn_is_readonly(vp)) 654*0Sstevel@tonic-gate error = EROFS; 655*0Sstevel@tonic-gate else 656*0Sstevel@tonic-gate error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED()); 657*0Sstevel@tonic-gate break; 658*0Sstevel@tonic-gate 659*0Sstevel@tonic-gate case VDIR: 660*0Sstevel@tonic-gate error = EISDIR; 661*0Sstevel@tonic-gate break; 662*0Sstevel@tonic-gate default: 663*0Sstevel@tonic-gate error = ENOSYS; 664*0Sstevel@tonic-gate break; 665*0Sstevel@tonic-gate } 666*0Sstevel@tonic-gate if (error == 0) { 667*0Sstevel@tonic-gate if (sc_cmd == SC_REMOVE) 668*0Sstevel@tonic-gate error = swapdel(vp, sr.sr_start); 669*0Sstevel@tonic-gate else 670*0Sstevel@tonic-gate error = swapadd(vp, sr.sr_start, 671*0Sstevel@tonic-gate sr.sr_length, swapname); 672*0Sstevel@tonic-gate } 673*0Sstevel@tonic-gate VN_RELE(vp); 674*0Sstevel@tonic-gate out: 675*0Sstevel@tonic-gate kmem_free(swapname, MAXPATHLEN); 676*0Sstevel@tonic-gate return (error); 677*0Sstevel@tonic-gate } 678*0Sstevel@tonic-gate 679*0Sstevel@tonic-gate #if defined(_LP64) && defined(_SYSCALL32) 680*0Sstevel@tonic-gate 681*0Sstevel@tonic-gate int 682*0Sstevel@tonic-gate swapctl32(int sc_cmd, void *sc_arg, int *rv) 683*0Sstevel@tonic-gate { 684*0Sstevel@tonic-gate struct swapinfo *sip, *csip, *tsip; 685*0Sstevel@tonic-gate int error = 0; 686*0Sstevel@tonic-gate struct swapent32 st, *ust; 687*0Sstevel@tonic-gate struct swapres32 sr; 688*0Sstevel@tonic-gate struct vnode *vp; 689*0Sstevel@tonic-gate int cnt = 0; 690*0Sstevel@tonic-gate int tmp_nswapfiles; 691*0Sstevel@tonic-gate int nswap; 692*0Sstevel@tonic-gate int length, nlen; 693*0Sstevel@tonic-gate int gplen = 0, plen; 694*0Sstevel@tonic-gate char *swapname; 695*0Sstevel@tonic-gate char *pname; 696*0Sstevel@tonic-gate char *tpname; 697*0Sstevel@tonic-gate struct anoninfo32 ai; 698*0Sstevel@tonic-gate size_t s; 699*0Sstevel@tonic-gate spgcnt_t avail; 700*0Sstevel@tonic-gate 701*0Sstevel@tonic-gate switch (sc_cmd) { 702*0Sstevel@tonic-gate case SC_GETNSWP: 703*0Sstevel@tonic-gate *rv = nswapfiles; 704*0Sstevel@tonic-gate return (0); 705*0Sstevel@tonic-gate 706*0Sstevel@tonic-gate case SC_AINFO: 707*0Sstevel@tonic-gate /* 708*0Sstevel@tonic-gate * Return anoninfo information with these changes: 709*0Sstevel@tonic-gate * ani_max = maximum amount of swap space 710*0Sstevel@tonic-gate * (including potentially available physical memory) 711*0Sstevel@tonic-gate * ani_free = amount of unallocated anonymous memory 712*0Sstevel@tonic-gate * (some of which might be reserved and including 713*0Sstevel@tonic-gate * potentially available physical memory) 714*0Sstevel@tonic-gate * ani_resv = amount of claimed (reserved) anonymous memory 715*0Sstevel@tonic-gate */ 716*0Sstevel@tonic-gate avail = MAX((spgcnt_t)(availrmem - swapfs_minfree), 0); 717*0Sstevel@tonic-gate s = (k_anoninfo.ani_max + k_anoninfo.ani_mem_resv) + avail; 718*0Sstevel@tonic-gate if (s > UINT32_MAX) 719*0Sstevel@tonic-gate return (EOVERFLOW); 720*0Sstevel@tonic-gate ai.ani_max = s; 721*0Sstevel@tonic-gate 722*0Sstevel@tonic-gate s = k_anoninfo.ani_free + avail; 723*0Sstevel@tonic-gate if (s > UINT32_MAX) 724*0Sstevel@tonic-gate return (EOVERFLOW); 725*0Sstevel@tonic-gate ai.ani_free = s; 726*0Sstevel@tonic-gate 727*0Sstevel@tonic-gate s = k_anoninfo.ani_phys_resv + k_anoninfo.ani_mem_resv; 728*0Sstevel@tonic-gate if (s > UINT32_MAX) 729*0Sstevel@tonic-gate return (EOVERFLOW); 730*0Sstevel@tonic-gate ai.ani_resv = s; 731*0Sstevel@tonic-gate 732*0Sstevel@tonic-gate if (copyout(&ai, sc_arg, sizeof (ai)) != 0) 733*0Sstevel@tonic-gate return (EFAULT); 734*0Sstevel@tonic-gate return (0); 735*0Sstevel@tonic-gate 736*0Sstevel@tonic-gate case SC_LIST: 737*0Sstevel@tonic-gate if (copyin(sc_arg, &length, sizeof (int32_t)) != 0) 738*0Sstevel@tonic-gate return (EFAULT); 739*0Sstevel@tonic-gate beginning: 740*0Sstevel@tonic-gate tmp_nswapfiles = nswapfiles; 741*0Sstevel@tonic-gate /* Return an error if not enough space for the whole table. */ 742*0Sstevel@tonic-gate if (length < tmp_nswapfiles) 743*0Sstevel@tonic-gate return (ENOMEM); 744*0Sstevel@tonic-gate /* 745*0Sstevel@tonic-gate * Get memory to hold the swap entries and their names. We'll 746*0Sstevel@tonic-gate * copy the real entries into these and then copy these out. 747*0Sstevel@tonic-gate * Allocating the pathname memory is only a guess so we may 748*0Sstevel@tonic-gate * find that we need more and have to do it again. 749*0Sstevel@tonic-gate * All this is because we have to hold the anon lock while 750*0Sstevel@tonic-gate * traversing the swapinfo list, and we can't be doing copyouts 751*0Sstevel@tonic-gate * and/or kmem_alloc()s during this. 752*0Sstevel@tonic-gate */ 753*0Sstevel@tonic-gate csip = kmem_zalloc(tmp_nswapfiles * sizeof (*csip), KM_SLEEP); 754*0Sstevel@tonic-gate retry: 755*0Sstevel@tonic-gate nlen = tmp_nswapfiles * (gplen += 100); 756*0Sstevel@tonic-gate pname = kmem_zalloc(nlen, KM_SLEEP); 757*0Sstevel@tonic-gate 758*0Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 759*0Sstevel@tonic-gate 760*0Sstevel@tonic-gate if (tmp_nswapfiles != nswapfiles) { 761*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 762*0Sstevel@tonic-gate kmem_free(pname, nlen); 763*0Sstevel@tonic-gate kmem_free(csip, tmp_nswapfiles * sizeof (*csip)); 764*0Sstevel@tonic-gate gplen = 0; 765*0Sstevel@tonic-gate goto beginning; 766*0Sstevel@tonic-gate } 767*0Sstevel@tonic-gate for (sip = swapinfo, tsip = csip, tpname = pname, nswap = 0; 768*0Sstevel@tonic-gate (sip != NULL) && (nswap < tmp_nswapfiles); 769*0Sstevel@tonic-gate sip = sip->si_next, tsip++, tpname += plen, nswap++) { 770*0Sstevel@tonic-gate plen = sip->si_pnamelen; 771*0Sstevel@tonic-gate if (tpname + plen - pname > nlen) { 772*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 773*0Sstevel@tonic-gate kmem_free(pname, nlen); 774*0Sstevel@tonic-gate goto retry; 775*0Sstevel@tonic-gate } 776*0Sstevel@tonic-gate *tsip = *sip; 777*0Sstevel@tonic-gate tsip->si_pname = tpname; 778*0Sstevel@tonic-gate (void) strcpy(tsip->si_pname, sip->si_pname); 779*0Sstevel@tonic-gate } 780*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 781*0Sstevel@tonic-gate 782*0Sstevel@tonic-gate if (sip != NULL) { 783*0Sstevel@tonic-gate error = ENOMEM; 784*0Sstevel@tonic-gate goto lout; 785*0Sstevel@tonic-gate } 786*0Sstevel@tonic-gate ust = (swapent32_t *)((swaptbl32_t *)sc_arg)->swt_ent; 787*0Sstevel@tonic-gate for (tsip = csip, cnt = 0; cnt < nswap; tsip++, ust++, cnt++) { 788*0Sstevel@tonic-gate if (copyin(ust, &st, sizeof (*ust)) != 0) { 789*0Sstevel@tonic-gate error = EFAULT; 790*0Sstevel@tonic-gate goto lout; 791*0Sstevel@tonic-gate } 792*0Sstevel@tonic-gate st.ste_flags = tsip->si_flags; 793*0Sstevel@tonic-gate st.ste_length = 794*0Sstevel@tonic-gate (tsip->si_eoff - tsip->si_soff) >> SCTRSHFT; 795*0Sstevel@tonic-gate st.ste_start = tsip->si_soff >> SCTRSHFT; 796*0Sstevel@tonic-gate st.ste_pages = tsip->si_npgs; 797*0Sstevel@tonic-gate st.ste_free = tsip->si_nfpgs; 798*0Sstevel@tonic-gate if (copyout(&st, ust, sizeof (st)) != 0) { 799*0Sstevel@tonic-gate error = EFAULT; 800*0Sstevel@tonic-gate goto lout; 801*0Sstevel@tonic-gate } 802*0Sstevel@tonic-gate if (!tsip->si_pnamelen) 803*0Sstevel@tonic-gate continue; 804*0Sstevel@tonic-gate if (copyout(tsip->si_pname, 805*0Sstevel@tonic-gate (caddr_t)(uintptr_t)st.ste_path, 806*0Sstevel@tonic-gate tsip->si_pnamelen) != 0) { 807*0Sstevel@tonic-gate error = EFAULT; 808*0Sstevel@tonic-gate goto lout; 809*0Sstevel@tonic-gate } 810*0Sstevel@tonic-gate } 811*0Sstevel@tonic-gate *rv = nswap; 812*0Sstevel@tonic-gate lout: 813*0Sstevel@tonic-gate kmem_free(csip, tmp_nswapfiles * sizeof (*csip)); 814*0Sstevel@tonic-gate kmem_free(pname, nlen); 815*0Sstevel@tonic-gate return (error); 816*0Sstevel@tonic-gate 817*0Sstevel@tonic-gate case SC_ADD: 818*0Sstevel@tonic-gate case SC_REMOVE: 819*0Sstevel@tonic-gate break; 820*0Sstevel@tonic-gate default: 821*0Sstevel@tonic-gate return (EINVAL); 822*0Sstevel@tonic-gate } 823*0Sstevel@tonic-gate if ((error = secpolicy_swapctl(CRED())) != 0) 824*0Sstevel@tonic-gate return (error); 825*0Sstevel@tonic-gate 826*0Sstevel@tonic-gate if (copyin(sc_arg, &sr, sizeof (sr))) 827*0Sstevel@tonic-gate return (EFAULT); 828*0Sstevel@tonic-gate 829*0Sstevel@tonic-gate /* Allocate the space to read in pathname */ 830*0Sstevel@tonic-gate if ((swapname = kmem_alloc(MAXPATHLEN, KM_NOSLEEP)) == NULL) 831*0Sstevel@tonic-gate return (ENOMEM); 832*0Sstevel@tonic-gate 833*0Sstevel@tonic-gate error = copyinstr((caddr_t)(uintptr_t)sr.sr_name, 834*0Sstevel@tonic-gate swapname, MAXPATHLEN, NULL); 835*0Sstevel@tonic-gate if (error) 836*0Sstevel@tonic-gate goto out; 837*0Sstevel@tonic-gate 838*0Sstevel@tonic-gate error = lookupname(swapname, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp); 839*0Sstevel@tonic-gate if (error) { 840*0Sstevel@tonic-gate if (sc_cmd == SC_ADD) 841*0Sstevel@tonic-gate goto out; 842*0Sstevel@tonic-gate /* see if we match by name */ 843*0Sstevel@tonic-gate vp = swapdel_byname(swapname, (uint_t)sr.sr_start); 844*0Sstevel@tonic-gate if (vp == NULL) 845*0Sstevel@tonic-gate goto out; 846*0Sstevel@tonic-gate } 847*0Sstevel@tonic-gate 848*0Sstevel@tonic-gate if (vp->v_flag & (VNOMAP | VNOSWAP)) { 849*0Sstevel@tonic-gate VN_RELE(vp); 850*0Sstevel@tonic-gate error = ENOSYS; 851*0Sstevel@tonic-gate goto out; 852*0Sstevel@tonic-gate } 853*0Sstevel@tonic-gate switch (vp->v_type) { 854*0Sstevel@tonic-gate case VBLK: 855*0Sstevel@tonic-gate break; 856*0Sstevel@tonic-gate 857*0Sstevel@tonic-gate case VREG: 858*0Sstevel@tonic-gate if (vp->v_vfsp && vn_is_readonly(vp)) 859*0Sstevel@tonic-gate error = EROFS; 860*0Sstevel@tonic-gate else 861*0Sstevel@tonic-gate error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED()); 862*0Sstevel@tonic-gate break; 863*0Sstevel@tonic-gate 864*0Sstevel@tonic-gate case VDIR: 865*0Sstevel@tonic-gate error = EISDIR; 866*0Sstevel@tonic-gate break; 867*0Sstevel@tonic-gate default: 868*0Sstevel@tonic-gate error = ENOSYS; 869*0Sstevel@tonic-gate break; 870*0Sstevel@tonic-gate } 871*0Sstevel@tonic-gate if (error == 0) { 872*0Sstevel@tonic-gate if (sc_cmd == SC_REMOVE) 873*0Sstevel@tonic-gate error = swapdel(vp, sr.sr_start); 874*0Sstevel@tonic-gate else 875*0Sstevel@tonic-gate error = swapadd(vp, sr.sr_start, sr.sr_length, 876*0Sstevel@tonic-gate swapname); 877*0Sstevel@tonic-gate } 878*0Sstevel@tonic-gate VN_RELE(vp); 879*0Sstevel@tonic-gate out: 880*0Sstevel@tonic-gate kmem_free(swapname, MAXPATHLEN); 881*0Sstevel@tonic-gate return (error); 882*0Sstevel@tonic-gate } 883*0Sstevel@tonic-gate 884*0Sstevel@tonic-gate #endif /* _LP64 && _SYSCALL32 */ 885*0Sstevel@tonic-gate 886*0Sstevel@tonic-gate /* 887*0Sstevel@tonic-gate * Add a new swap file. 888*0Sstevel@tonic-gate */ 889*0Sstevel@tonic-gate int 890*0Sstevel@tonic-gate swapadd(struct vnode *vp, ulong_t lowblk, ulong_t nblks, char *swapname) 891*0Sstevel@tonic-gate { 892*0Sstevel@tonic-gate struct swapinfo **sipp, *nsip = NULL, *esip = NULL; 893*0Sstevel@tonic-gate struct vnode *cvp; 894*0Sstevel@tonic-gate struct vattr vattr; 895*0Sstevel@tonic-gate pgcnt_t pages; 896*0Sstevel@tonic-gate u_offset_t soff, eoff; 897*0Sstevel@tonic-gate int error; 898*0Sstevel@tonic-gate ssize_t i, start, end; 899*0Sstevel@tonic-gate ushort_t wasswap; 900*0Sstevel@tonic-gate ulong_t startblk; 901*0Sstevel@tonic-gate size_t returned_mem; 902*0Sstevel@tonic-gate 903*0Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: vp %p lowblk %ld nblks %ld swapname %s\n", 904*0Sstevel@tonic-gate vp, lowblk, nblks, swapname, 0); 905*0Sstevel@tonic-gate /* 906*0Sstevel@tonic-gate * Get the real vnode. (If vp is not a specnode it just returns vp, so 907*0Sstevel@tonic-gate * it does the right thing, but having this code know about specnodes 908*0Sstevel@tonic-gate * violates the spirit of having it be indepedent of vnode type.) 909*0Sstevel@tonic-gate */ 910*0Sstevel@tonic-gate cvp = common_specvp(vp); 911*0Sstevel@tonic-gate 912*0Sstevel@tonic-gate /* 913*0Sstevel@tonic-gate * Or in VISSWAP so file system has chance to deny swap-ons during open. 914*0Sstevel@tonic-gate */ 915*0Sstevel@tonic-gate mutex_enter(&cvp->v_lock); 916*0Sstevel@tonic-gate wasswap = cvp->v_flag & VISSWAP; 917*0Sstevel@tonic-gate cvp->v_flag |= VISSWAP; 918*0Sstevel@tonic-gate mutex_exit(&cvp->v_lock); 919*0Sstevel@tonic-gate 920*0Sstevel@tonic-gate mutex_enter(&swap_lock); 921*0Sstevel@tonic-gate if (error = VOP_OPEN(&cvp, FREAD|FWRITE, CRED())) { 922*0Sstevel@tonic-gate mutex_exit(&swap_lock); 923*0Sstevel@tonic-gate /* restore state of v_flag */ 924*0Sstevel@tonic-gate if (!wasswap) { 925*0Sstevel@tonic-gate mutex_enter(&cvp->v_lock); 926*0Sstevel@tonic-gate cvp->v_flag &= ~VISSWAP; 927*0Sstevel@tonic-gate mutex_exit(&cvp->v_lock); 928*0Sstevel@tonic-gate } 929*0Sstevel@tonic-gate return (error); 930*0Sstevel@tonic-gate } 931*0Sstevel@tonic-gate mutex_exit(&swap_lock); 932*0Sstevel@tonic-gate 933*0Sstevel@tonic-gate /* 934*0Sstevel@tonic-gate * Get partition size. Return error if empty partition, 935*0Sstevel@tonic-gate * or if request does not fit within the partition. 936*0Sstevel@tonic-gate * If this is the first swap device, we can reduce 937*0Sstevel@tonic-gate * the size of the swap area to match what is 938*0Sstevel@tonic-gate * available. This can happen if the system was built 939*0Sstevel@tonic-gate * on a machine with a different size swap partition. 940*0Sstevel@tonic-gate */ 941*0Sstevel@tonic-gate vattr.va_mask = AT_SIZE; 942*0Sstevel@tonic-gate if (error = VOP_GETATTR(cvp, &vattr, ATTR_COMM, CRED())) 943*0Sstevel@tonic-gate goto out; 944*0Sstevel@tonic-gate 945*0Sstevel@tonic-gate /* 946*0Sstevel@tonic-gate * Specfs returns a va_size of MAXOFFSET_T (UNKNOWN_SIZE) when the 947*0Sstevel@tonic-gate * size of the device can't be determined. 948*0Sstevel@tonic-gate */ 949*0Sstevel@tonic-gate if ((vattr.va_size == 0) || (vattr.va_size == MAXOFFSET_T)) { 950*0Sstevel@tonic-gate error = EINVAL; 951*0Sstevel@tonic-gate goto out; 952*0Sstevel@tonic-gate } 953*0Sstevel@tonic-gate 954*0Sstevel@tonic-gate #ifdef _ILP32 955*0Sstevel@tonic-gate /* 956*0Sstevel@tonic-gate * No support for large swap in 32-bit OS, if the size of the swap is 957*0Sstevel@tonic-gate * bigger than MAXOFF32_T then the size used by swapfs must be limited. 958*0Sstevel@tonic-gate * This limitation is imposed by the swap subsystem itself, a D_64BIT 959*0Sstevel@tonic-gate * driver as the target of swap operation should be able to field 960*0Sstevel@tonic-gate * the IO. 961*0Sstevel@tonic-gate */ 962*0Sstevel@tonic-gate if (vattr.va_size > MAXOFF32_T) { 963*0Sstevel@tonic-gate cmn_err(CE_NOTE, 964*0Sstevel@tonic-gate "!swap device %s truncated from 0x%llx to 0x%x bytes", 965*0Sstevel@tonic-gate swapname, vattr.va_size, MAXOFF32_T); 966*0Sstevel@tonic-gate vattr.va_size = MAXOFF32_T; 967*0Sstevel@tonic-gate } 968*0Sstevel@tonic-gate #endif /* _ILP32 */ 969*0Sstevel@tonic-gate 970*0Sstevel@tonic-gate /* Fail if file not writeable (try to set size to current size) */ 971*0Sstevel@tonic-gate vattr.va_mask = AT_SIZE; 972*0Sstevel@tonic-gate if (error = VOP_SETATTR(cvp, &vattr, 0, CRED(), NULL)) 973*0Sstevel@tonic-gate goto out; 974*0Sstevel@tonic-gate 975*0Sstevel@tonic-gate /* Fail if fs does not support VOP_PAGEIO */ 976*0Sstevel@tonic-gate error = VOP_PAGEIO(cvp, (page_t *)NULL, (u_offset_t)0, 0, 0, CRED()); 977*0Sstevel@tonic-gate 978*0Sstevel@tonic-gate if (error == ENOSYS) 979*0Sstevel@tonic-gate goto out; 980*0Sstevel@tonic-gate else 981*0Sstevel@tonic-gate error = 0; 982*0Sstevel@tonic-gate /* 983*0Sstevel@tonic-gate * If swapping on the root filesystem don't put swap blocks that 984*0Sstevel@tonic-gate * correspond to the miniroot filesystem on the swap free list. 985*0Sstevel@tonic-gate */ 986*0Sstevel@tonic-gate if (cvp == rootdir) 987*0Sstevel@tonic-gate startblk = roundup(MINIROOTSIZE<<SCTRSHFT, klustsize)>>SCTRSHFT; 988*0Sstevel@tonic-gate else /* Skip 1st page (disk label) */ 989*0Sstevel@tonic-gate startblk = (ulong_t)(lowblk ? lowblk : 1); 990*0Sstevel@tonic-gate 991*0Sstevel@tonic-gate soff = startblk << SCTRSHFT; 992*0Sstevel@tonic-gate if (soff >= vattr.va_size) { 993*0Sstevel@tonic-gate error = EINVAL; 994*0Sstevel@tonic-gate goto out; 995*0Sstevel@tonic-gate } 996*0Sstevel@tonic-gate 997*0Sstevel@tonic-gate /* 998*0Sstevel@tonic-gate * If user specified 0 blks, use the size of the device 999*0Sstevel@tonic-gate */ 1000*0Sstevel@tonic-gate eoff = nblks ? soff + (nblks - (startblk - lowblk) << SCTRSHFT) : 1001*0Sstevel@tonic-gate vattr.va_size; 1002*0Sstevel@tonic-gate 1003*0Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: va_size %ld soff %ld eoff %ld\n", 1004*0Sstevel@tonic-gate vattr.va_size, soff, eoff, 0, 0); 1005*0Sstevel@tonic-gate 1006*0Sstevel@tonic-gate if (eoff > vattr.va_size) { 1007*0Sstevel@tonic-gate error = EINVAL; 1008*0Sstevel@tonic-gate goto out; 1009*0Sstevel@tonic-gate } 1010*0Sstevel@tonic-gate 1011*0Sstevel@tonic-gate /* 1012*0Sstevel@tonic-gate * The starting and ending offsets must be page aligned. 1013*0Sstevel@tonic-gate * Round soff up to next page boundary, round eoff 1014*0Sstevel@tonic-gate * down to previous page boundary. 1015*0Sstevel@tonic-gate */ 1016*0Sstevel@tonic-gate soff = ptob(btopr(soff)); 1017*0Sstevel@tonic-gate eoff = ptob(btop(eoff)); 1018*0Sstevel@tonic-gate if (soff >= eoff) { 1019*0Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: soff %ld >= eoff %ld\n", 1020*0Sstevel@tonic-gate soff, eoff, 0, 0, 0); 1021*0Sstevel@tonic-gate error = EINVAL; 1022*0Sstevel@tonic-gate goto out; 1023*0Sstevel@tonic-gate } 1024*0Sstevel@tonic-gate 1025*0Sstevel@tonic-gate pages = btop(eoff - soff); 1026*0Sstevel@tonic-gate 1027*0Sstevel@tonic-gate /* Allocate and partially set up the new swapinfo */ 1028*0Sstevel@tonic-gate nsip = kmem_zalloc(sizeof (struct swapinfo), KM_SLEEP); 1029*0Sstevel@tonic-gate nsip->si_vp = cvp; 1030*0Sstevel@tonic-gate 1031*0Sstevel@tonic-gate nsip->si_soff = soff; 1032*0Sstevel@tonic-gate nsip->si_eoff = eoff; 1033*0Sstevel@tonic-gate nsip->si_hint = 0; 1034*0Sstevel@tonic-gate nsip->si_checkcnt = nsip->si_alloccnt = 0; 1035*0Sstevel@tonic-gate 1036*0Sstevel@tonic-gate nsip->si_pnamelen = (int)strlen(swapname) + 1; 1037*0Sstevel@tonic-gate nsip->si_pname = (char *)kmem_zalloc(nsip->si_pnamelen, KM_SLEEP); 1038*0Sstevel@tonic-gate bcopy(swapname, nsip->si_pname, nsip->si_pnamelen - 1); 1039*0Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: allocating swapinfo for %s, %ld pages\n", 1040*0Sstevel@tonic-gate swapname, pages, 0, 0, 0); 1041*0Sstevel@tonic-gate /* 1042*0Sstevel@tonic-gate * Size of swapslots map in bytes 1043*0Sstevel@tonic-gate */ 1044*0Sstevel@tonic-gate nsip->si_mapsize = P2ROUNDUP(pages, NBBW) / NBBY; 1045*0Sstevel@tonic-gate nsip->si_swapslots = kmem_zalloc(nsip->si_mapsize, KM_SLEEP); 1046*0Sstevel@tonic-gate 1047*0Sstevel@tonic-gate /* 1048*0Sstevel@tonic-gate * Permanently set the bits that can't ever be allocated, 1049*0Sstevel@tonic-gate * i.e. those from the ending offset to the round up slot for the 1050*0Sstevel@tonic-gate * swapslots bit map. 1051*0Sstevel@tonic-gate */ 1052*0Sstevel@tonic-gate start = pages; 1053*0Sstevel@tonic-gate end = P2ROUNDUP(pages, NBBW); 1054*0Sstevel@tonic-gate for (i = start; i < end; i++) { 1055*0Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: set bit for page %ld\n", i, 1056*0Sstevel@tonic-gate 0, 0, 0, 0); 1057*0Sstevel@tonic-gate SETBIT(nsip->si_swapslots, i); 1058*0Sstevel@tonic-gate } 1059*0Sstevel@tonic-gate nsip->si_npgs = nsip->si_nfpgs = pages; 1060*0Sstevel@tonic-gate /* 1061*0Sstevel@tonic-gate * Now check to see if we can add it. We wait til now to check because 1062*0Sstevel@tonic-gate * we need the swapinfo_lock and we don't want sleep with it (e.g., 1063*0Sstevel@tonic-gate * during kmem_alloc()) while we're setting up the swapinfo. 1064*0Sstevel@tonic-gate */ 1065*0Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 1066*0Sstevel@tonic-gate for (sipp = &swapinfo; (esip = *sipp) != NULL; sipp = &esip->si_next) { 1067*0Sstevel@tonic-gate if (esip->si_vp == cvp) { 1068*0Sstevel@tonic-gate if (esip->si_soff == soff && esip->si_npgs == pages && 1069*0Sstevel@tonic-gate (esip->si_flags & ST_DOINGDEL)) { 1070*0Sstevel@tonic-gate /* 1071*0Sstevel@tonic-gate * We are adding a device that we are in the 1072*0Sstevel@tonic-gate * middle of deleting. Just clear the 1073*0Sstevel@tonic-gate * ST_DOINGDEL flag to signal this and 1074*0Sstevel@tonic-gate * the deletion routine will eventually notice 1075*0Sstevel@tonic-gate * it and add it back. 1076*0Sstevel@tonic-gate */ 1077*0Sstevel@tonic-gate esip->si_flags &= ~ST_DOINGDEL; 1078*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 1079*0Sstevel@tonic-gate goto out; 1080*0Sstevel@tonic-gate } 1081*0Sstevel@tonic-gate /* disallow overlapping swap files */ 1082*0Sstevel@tonic-gate if ((soff < esip->si_eoff) && (eoff > esip->si_soff)) { 1083*0Sstevel@tonic-gate error = EEXIST; 1084*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 1085*0Sstevel@tonic-gate goto out; 1086*0Sstevel@tonic-gate } 1087*0Sstevel@tonic-gate } 1088*0Sstevel@tonic-gate } 1089*0Sstevel@tonic-gate 1090*0Sstevel@tonic-gate nswapfiles++; 1091*0Sstevel@tonic-gate 1092*0Sstevel@tonic-gate /* 1093*0Sstevel@tonic-gate * add new swap device to list and shift allocations to it 1094*0Sstevel@tonic-gate * before updating the anoninfo counters 1095*0Sstevel@tonic-gate */ 1096*0Sstevel@tonic-gate *sipp = nsip; 1097*0Sstevel@tonic-gate silast = nsip; 1098*0Sstevel@tonic-gate 1099*0Sstevel@tonic-gate /* 1100*0Sstevel@tonic-gate * Update the total amount of reservable swap space 1101*0Sstevel@tonic-gate * accounting properly for swap space from physical memory 1102*0Sstevel@tonic-gate */ 1103*0Sstevel@tonic-gate /* New swap device soaks up currently reserved memory swap */ 1104*0Sstevel@tonic-gate mutex_enter(&anoninfo_lock); 1105*0Sstevel@tonic-gate 1106*0Sstevel@tonic-gate ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap); 1107*0Sstevel@tonic-gate ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv); 1108*0Sstevel@tonic-gate 1109*0Sstevel@tonic-gate k_anoninfo.ani_max += pages; 1110*0Sstevel@tonic-gate ANI_ADD(pages); 1111*0Sstevel@tonic-gate if (k_anoninfo.ani_mem_resv > k_anoninfo.ani_locked_swap) { 1112*0Sstevel@tonic-gate returned_mem = MIN(k_anoninfo.ani_mem_resv - 1113*0Sstevel@tonic-gate k_anoninfo.ani_locked_swap, 1114*0Sstevel@tonic-gate k_anoninfo.ani_max - k_anoninfo.ani_phys_resv); 1115*0Sstevel@tonic-gate 1116*0Sstevel@tonic-gate ANI_ADD(-returned_mem); 1117*0Sstevel@tonic-gate k_anoninfo.ani_free -= returned_mem; 1118*0Sstevel@tonic-gate k_anoninfo.ani_mem_resv -= returned_mem; 1119*0Sstevel@tonic-gate k_anoninfo.ani_phys_resv += returned_mem; 1120*0Sstevel@tonic-gate 1121*0Sstevel@tonic-gate mutex_enter(&freemem_lock); 1122*0Sstevel@tonic-gate availrmem += returned_mem; 1123*0Sstevel@tonic-gate mutex_exit(&freemem_lock); 1124*0Sstevel@tonic-gate } 1125*0Sstevel@tonic-gate /* 1126*0Sstevel@tonic-gate * At boot time, to permit booting small memory machines using 1127*0Sstevel@tonic-gate * only physical memory as swap space, we allowed a dangerously 1128*0Sstevel@tonic-gate * large amount of memory to be used as swap space; now that 1129*0Sstevel@tonic-gate * more physical backing store is available bump down the amount 1130*0Sstevel@tonic-gate * we can get from memory to a safer size. 1131*0Sstevel@tonic-gate */ 1132*0Sstevel@tonic-gate if (swapfs_minfree < swapfs_desfree) { 1133*0Sstevel@tonic-gate mutex_enter(&freemem_lock); 1134*0Sstevel@tonic-gate if (availrmem > swapfs_desfree || !k_anoninfo.ani_mem_resv) 1135*0Sstevel@tonic-gate swapfs_minfree = swapfs_desfree; 1136*0Sstevel@tonic-gate mutex_exit(&freemem_lock); 1137*0Sstevel@tonic-gate } 1138*0Sstevel@tonic-gate 1139*0Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: ani_max %ld ani_free %ld\n", 1140*0Sstevel@tonic-gate k_anoninfo.ani_free, k_anoninfo.ani_free, 0, 0, 0); 1141*0Sstevel@tonic-gate 1142*0Sstevel@tonic-gate mutex_exit(&anoninfo_lock); 1143*0Sstevel@tonic-gate 1144*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 1145*0Sstevel@tonic-gate 1146*0Sstevel@tonic-gate /* Initialize the dump device */ 1147*0Sstevel@tonic-gate mutex_enter(&dump_lock); 1148*0Sstevel@tonic-gate if (dumpvp == NULL) 1149*0Sstevel@tonic-gate (void) dumpinit(vp, swapname, 0); 1150*0Sstevel@tonic-gate mutex_exit(&dump_lock); 1151*0Sstevel@tonic-gate 1152*0Sstevel@tonic-gate VN_HOLD(cvp); 1153*0Sstevel@tonic-gate out: 1154*0Sstevel@tonic-gate if (error || esip) { 1155*0Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: error (%d)\n", error, 0, 0, 0, 0); 1156*0Sstevel@tonic-gate 1157*0Sstevel@tonic-gate if (!wasswap) { 1158*0Sstevel@tonic-gate mutex_enter(&cvp->v_lock); 1159*0Sstevel@tonic-gate cvp->v_flag &= ~VISSWAP; 1160*0Sstevel@tonic-gate mutex_exit(&cvp->v_lock); 1161*0Sstevel@tonic-gate } 1162*0Sstevel@tonic-gate if (nsip) { 1163*0Sstevel@tonic-gate kmem_free(nsip->si_swapslots, (size_t)nsip->si_mapsize); 1164*0Sstevel@tonic-gate kmem_free(nsip->si_pname, nsip->si_pnamelen); 1165*0Sstevel@tonic-gate kmem_free(nsip, sizeof (*nsip)); 1166*0Sstevel@tonic-gate } 1167*0Sstevel@tonic-gate mutex_enter(&swap_lock); 1168*0Sstevel@tonic-gate (void) VOP_CLOSE(cvp, FREAD|FWRITE, 1, (offset_t)0, CRED()); 1169*0Sstevel@tonic-gate mutex_exit(&swap_lock); 1170*0Sstevel@tonic-gate } 1171*0Sstevel@tonic-gate return (error); 1172*0Sstevel@tonic-gate } 1173*0Sstevel@tonic-gate 1174*0Sstevel@tonic-gate /* 1175*0Sstevel@tonic-gate * Delete a swap file. 1176*0Sstevel@tonic-gate */ 1177*0Sstevel@tonic-gate static int 1178*0Sstevel@tonic-gate swapdel( 1179*0Sstevel@tonic-gate struct vnode *vp, 1180*0Sstevel@tonic-gate ulong_t lowblk) /* Low block number of area to delete. */ 1181*0Sstevel@tonic-gate { 1182*0Sstevel@tonic-gate struct swapinfo **sipp, *osip = NULL; 1183*0Sstevel@tonic-gate struct vnode *cvp; 1184*0Sstevel@tonic-gate u_offset_t soff; 1185*0Sstevel@tonic-gate int error = 0; 1186*0Sstevel@tonic-gate u_offset_t toff = 0; 1187*0Sstevel@tonic-gate struct vnode *tvp = NULL; 1188*0Sstevel@tonic-gate spgcnt_t pages; 1189*0Sstevel@tonic-gate struct anon **app, *ap; 1190*0Sstevel@tonic-gate kmutex_t *ahm; 1191*0Sstevel@tonic-gate pgcnt_t adjust_swap = 0; 1192*0Sstevel@tonic-gate 1193*0Sstevel@tonic-gate /* Find the swap file entry for the file to be deleted */ 1194*0Sstevel@tonic-gate cvp = common_specvp(vp); 1195*0Sstevel@tonic-gate 1196*0Sstevel@tonic-gate 1197*0Sstevel@tonic-gate lowblk = lowblk ? lowblk : 1; /* Skip first page (disk label) */ 1198*0Sstevel@tonic-gate soff = ptob(btopr(lowblk << SCTRSHFT)); /* must be page aligned */ 1199*0Sstevel@tonic-gate 1200*0Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 1201*0Sstevel@tonic-gate for (sipp = &swapinfo; (osip = *sipp) != NULL; sipp = &osip->si_next) { 1202*0Sstevel@tonic-gate if ((osip->si_vp == cvp) && 1203*0Sstevel@tonic-gate (osip->si_soff == soff) && (osip->si_flags == 0)) 1204*0Sstevel@tonic-gate break; 1205*0Sstevel@tonic-gate } 1206*0Sstevel@tonic-gate 1207*0Sstevel@tonic-gate /* If the file was not found, error. */ 1208*0Sstevel@tonic-gate if (osip == NULL) { 1209*0Sstevel@tonic-gate error = EINVAL; 1210*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 1211*0Sstevel@tonic-gate goto out; 1212*0Sstevel@tonic-gate } 1213*0Sstevel@tonic-gate 1214*0Sstevel@tonic-gate pages = osip->si_npgs; 1215*0Sstevel@tonic-gate 1216*0Sstevel@tonic-gate /* 1217*0Sstevel@tonic-gate * Do not delete if we will be low on swap pages. 1218*0Sstevel@tonic-gate */ 1219*0Sstevel@tonic-gate mutex_enter(&anoninfo_lock); 1220*0Sstevel@tonic-gate 1221*0Sstevel@tonic-gate ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap); 1222*0Sstevel@tonic-gate ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv); 1223*0Sstevel@tonic-gate 1224*0Sstevel@tonic-gate mutex_enter(&freemem_lock); 1225*0Sstevel@tonic-gate if (((k_anoninfo.ani_max - k_anoninfo.ani_phys_resv) + 1226*0Sstevel@tonic-gate MAX((spgcnt_t)(availrmem - swapfs_minfree), 0)) < pages) { 1227*0Sstevel@tonic-gate mutex_exit(&freemem_lock); 1228*0Sstevel@tonic-gate mutex_exit(&anoninfo_lock); 1229*0Sstevel@tonic-gate error = ENOMEM; 1230*0Sstevel@tonic-gate cmn_err(CE_WARN, "swapdel - too few free pages"); 1231*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 1232*0Sstevel@tonic-gate goto out; 1233*0Sstevel@tonic-gate } 1234*0Sstevel@tonic-gate mutex_exit(&freemem_lock); 1235*0Sstevel@tonic-gate 1236*0Sstevel@tonic-gate k_anoninfo.ani_max -= pages; 1237*0Sstevel@tonic-gate 1238*0Sstevel@tonic-gate /* If needed, reserve memory swap to replace old device */ 1239*0Sstevel@tonic-gate if (k_anoninfo.ani_phys_resv > k_anoninfo.ani_max) { 1240*0Sstevel@tonic-gate adjust_swap = k_anoninfo.ani_phys_resv - k_anoninfo.ani_max; 1241*0Sstevel@tonic-gate k_anoninfo.ani_phys_resv -= adjust_swap; 1242*0Sstevel@tonic-gate k_anoninfo.ani_mem_resv += adjust_swap; 1243*0Sstevel@tonic-gate mutex_enter(&freemem_lock); 1244*0Sstevel@tonic-gate availrmem -= adjust_swap; 1245*0Sstevel@tonic-gate mutex_exit(&freemem_lock); 1246*0Sstevel@tonic-gate ANI_ADD(adjust_swap); 1247*0Sstevel@tonic-gate } 1248*0Sstevel@tonic-gate ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap); 1249*0Sstevel@tonic-gate ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv); 1250*0Sstevel@tonic-gate mutex_exit(&anoninfo_lock); 1251*0Sstevel@tonic-gate 1252*0Sstevel@tonic-gate ANI_ADD(-pages); 1253*0Sstevel@tonic-gate 1254*0Sstevel@tonic-gate /* 1255*0Sstevel@tonic-gate * Set the delete flag. This prevents anyone from allocating more 1256*0Sstevel@tonic-gate * pages from this file. Also set ST_DOINGDEL. Someone who wants to 1257*0Sstevel@tonic-gate * add the file back while we're deleting it will signify by clearing 1258*0Sstevel@tonic-gate * this flag. 1259*0Sstevel@tonic-gate */ 1260*0Sstevel@tonic-gate osip->si_flags |= ST_INDEL|ST_DOINGDEL; 1261*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 1262*0Sstevel@tonic-gate 1263*0Sstevel@tonic-gate /* 1264*0Sstevel@tonic-gate * Free all the allocated physical slots for this file. We do this 1265*0Sstevel@tonic-gate * by walking through the entire anon hash array, because we need 1266*0Sstevel@tonic-gate * to update all the anon slots that have physical swap slots on 1267*0Sstevel@tonic-gate * this file, and this is the only way to find them all. We go back 1268*0Sstevel@tonic-gate * to the beginning of a bucket after each slot is freed because the 1269*0Sstevel@tonic-gate * anonhash_lock is not held during the free and thus the hash table 1270*0Sstevel@tonic-gate * may change under us. 1271*0Sstevel@tonic-gate */ 1272*0Sstevel@tonic-gate for (app = anon_hash; app < &anon_hash[ANON_HASH_SIZE]; app++) { 1273*0Sstevel@tonic-gate ahm = &anonhash_lock[(app-anon_hash) & (AH_LOCK_SIZE - 1)]; 1274*0Sstevel@tonic-gate mutex_enter(ahm); 1275*0Sstevel@tonic-gate top: 1276*0Sstevel@tonic-gate for (ap = *app; ap != NULL; ap = ap->an_hash) { 1277*0Sstevel@tonic-gate if (ap->an_pvp == cvp && 1278*0Sstevel@tonic-gate ap->an_poff >= osip->si_soff && 1279*0Sstevel@tonic-gate ap->an_poff < osip->si_eoff) { 1280*0Sstevel@tonic-gate ASSERT(TESTBIT(osip->si_swapslots, 1281*0Sstevel@tonic-gate btop((size_t)(ap->an_poff - 1282*0Sstevel@tonic-gate osip->si_soff)))); 1283*0Sstevel@tonic-gate tvp = ap->an_vp; 1284*0Sstevel@tonic-gate toff = ap->an_off; 1285*0Sstevel@tonic-gate VN_HOLD(tvp); 1286*0Sstevel@tonic-gate mutex_exit(ahm); 1287*0Sstevel@tonic-gate 1288*0Sstevel@tonic-gate error = swapslot_free(tvp, toff, osip); 1289*0Sstevel@tonic-gate 1290*0Sstevel@tonic-gate VN_RELE(tvp); 1291*0Sstevel@tonic-gate mutex_enter(ahm); 1292*0Sstevel@tonic-gate if (!error && (osip->si_flags & ST_DOINGDEL)) { 1293*0Sstevel@tonic-gate goto top; 1294*0Sstevel@tonic-gate } else { 1295*0Sstevel@tonic-gate if (error) { 1296*0Sstevel@tonic-gate cmn_err(CE_WARN, 1297*0Sstevel@tonic-gate "swapslot_free failed %d", 1298*0Sstevel@tonic-gate error); 1299*0Sstevel@tonic-gate } 1300*0Sstevel@tonic-gate 1301*0Sstevel@tonic-gate /* 1302*0Sstevel@tonic-gate * Add device back before making it 1303*0Sstevel@tonic-gate * visible. 1304*0Sstevel@tonic-gate */ 1305*0Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 1306*0Sstevel@tonic-gate osip->si_flags &= 1307*0Sstevel@tonic-gate ~(ST_INDEL | ST_DOINGDEL); 1308*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 1309*0Sstevel@tonic-gate 1310*0Sstevel@tonic-gate /* 1311*0Sstevel@tonic-gate * Update the anon space available 1312*0Sstevel@tonic-gate */ 1313*0Sstevel@tonic-gate mutex_enter(&anoninfo_lock); 1314*0Sstevel@tonic-gate 1315*0Sstevel@tonic-gate k_anoninfo.ani_phys_resv += adjust_swap; 1316*0Sstevel@tonic-gate k_anoninfo.ani_mem_resv -= adjust_swap; 1317*0Sstevel@tonic-gate k_anoninfo.ani_max += pages; 1318*0Sstevel@tonic-gate 1319*0Sstevel@tonic-gate mutex_enter(&freemem_lock); 1320*0Sstevel@tonic-gate availrmem += adjust_swap; 1321*0Sstevel@tonic-gate mutex_exit(&freemem_lock); 1322*0Sstevel@tonic-gate 1323*0Sstevel@tonic-gate mutex_exit(&anoninfo_lock); 1324*0Sstevel@tonic-gate 1325*0Sstevel@tonic-gate ANI_ADD(pages); 1326*0Sstevel@tonic-gate 1327*0Sstevel@tonic-gate mutex_exit(ahm); 1328*0Sstevel@tonic-gate goto out; 1329*0Sstevel@tonic-gate } 1330*0Sstevel@tonic-gate } 1331*0Sstevel@tonic-gate } 1332*0Sstevel@tonic-gate mutex_exit(ahm); 1333*0Sstevel@tonic-gate } 1334*0Sstevel@tonic-gate 1335*0Sstevel@tonic-gate /* All done, they'd better all be free! */ 1336*0Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 1337*0Sstevel@tonic-gate ASSERT(osip->si_nfpgs == osip->si_npgs); 1338*0Sstevel@tonic-gate 1339*0Sstevel@tonic-gate /* Now remove it from the swapinfo list */ 1340*0Sstevel@tonic-gate for (sipp = &swapinfo; *sipp != NULL; sipp = &(*sipp)->si_next) { 1341*0Sstevel@tonic-gate if (*sipp == osip) 1342*0Sstevel@tonic-gate break; 1343*0Sstevel@tonic-gate } 1344*0Sstevel@tonic-gate ASSERT(*sipp); 1345*0Sstevel@tonic-gate *sipp = osip->si_next; 1346*0Sstevel@tonic-gate if (silast == osip) 1347*0Sstevel@tonic-gate if ((silast = osip->si_next) == NULL) 1348*0Sstevel@tonic-gate silast = swapinfo; 1349*0Sstevel@tonic-gate nswapfiles--; 1350*0Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 1351*0Sstevel@tonic-gate 1352*0Sstevel@tonic-gate kmem_free(osip->si_swapslots, osip->si_mapsize); 1353*0Sstevel@tonic-gate kmem_free(osip->si_pname, osip->si_pnamelen); 1354*0Sstevel@tonic-gate kmem_free(osip, sizeof (*osip)); 1355*0Sstevel@tonic-gate 1356*0Sstevel@tonic-gate mutex_enter(&dump_lock); 1357*0Sstevel@tonic-gate if (cvp == dumpvp) 1358*0Sstevel@tonic-gate dumpfini(); 1359*0Sstevel@tonic-gate mutex_exit(&dump_lock); 1360*0Sstevel@tonic-gate 1361*0Sstevel@tonic-gate /* Release the vnode */ 1362*0Sstevel@tonic-gate 1363*0Sstevel@tonic-gate mutex_enter(&swap_lock); 1364*0Sstevel@tonic-gate (void) VOP_CLOSE(cvp, FREAD|FWRITE, 1, (offset_t)0, CRED()); 1365*0Sstevel@tonic-gate mutex_enter(&cvp->v_lock); 1366*0Sstevel@tonic-gate cvp->v_flag &= ~VISSWAP; 1367*0Sstevel@tonic-gate mutex_exit(&cvp->v_lock); 1368*0Sstevel@tonic-gate VN_RELE(cvp); 1369*0Sstevel@tonic-gate mutex_exit(&swap_lock); 1370*0Sstevel@tonic-gate out: 1371*0Sstevel@tonic-gate return (error); 1372*0Sstevel@tonic-gate } 1373*0Sstevel@tonic-gate 1374*0Sstevel@tonic-gate /* 1375*0Sstevel@tonic-gate * Free up a physical swap slot on swapinfo sip, currently in use by the 1376*0Sstevel@tonic-gate * anonymous page whose name is (vp, off). 1377*0Sstevel@tonic-gate */ 1378*0Sstevel@tonic-gate static int 1379*0Sstevel@tonic-gate swapslot_free( 1380*0Sstevel@tonic-gate struct vnode *vp, 1381*0Sstevel@tonic-gate u_offset_t off, 1382*0Sstevel@tonic-gate struct swapinfo *sip) 1383*0Sstevel@tonic-gate { 1384*0Sstevel@tonic-gate struct page *pl[2], *pp; 1385*0Sstevel@tonic-gate struct anon *ap = NULL; 1386*0Sstevel@tonic-gate int error = 0; 1387*0Sstevel@tonic-gate kmutex_t *ahm; 1388*0Sstevel@tonic-gate 1389*0Sstevel@tonic-gate /* 1390*0Sstevel@tonic-gate * Get the page for the old swap slot and i/o lock it. 1391*0Sstevel@tonic-gate * Users of the physical slot will synchronize on the i/o lock. 1392*0Sstevel@tonic-gate */ 1393*0Sstevel@tonic-gate if (error = VOP_GETPAGE(vp, (offset_t)off, ptob(1), NULL, 1394*0Sstevel@tonic-gate pl, ptob(1), segkmap, NULL, S_READ, CRED())) { 1395*0Sstevel@tonic-gate /* 1396*0Sstevel@tonic-gate * Anon slot went away (EIDRM) or vp was truncated (EFAULT) 1397*0Sstevel@tonic-gate * while we got the page. Thus the physical slot must be 1398*0Sstevel@tonic-gate * free, so we have succeeded. 1399*0Sstevel@tonic-gate */ 1400*0Sstevel@tonic-gate if (error == EIDRM || error == EFAULT) 1401*0Sstevel@tonic-gate error = 0; 1402*0Sstevel@tonic-gate return (error); 1403*0Sstevel@tonic-gate } 1404*0Sstevel@tonic-gate pp = pl[0]; 1405*0Sstevel@tonic-gate page_io_lock(pp); 1406*0Sstevel@tonic-gate 1407*0Sstevel@tonic-gate ahm = &anonhash_lock[AH_LOCK(vp, off)]; 1408*0Sstevel@tonic-gate mutex_enter(ahm); 1409*0Sstevel@tonic-gate /* 1410*0Sstevel@tonic-gate * Get the anon slot; anon struct cannot vanish while we hold 1411*0Sstevel@tonic-gate * SE_SHARED lock on the physical page since anon_decref() blocks 1412*0Sstevel@tonic-gate * in page_lookup() before it can proceed further to remove 1413*0Sstevel@tonic-gate * anon struct from anon_hash table. 1414*0Sstevel@tonic-gate */ 1415*0Sstevel@tonic-gate if ((ap = swap_anon(vp, off)) == NULL) { 1416*0Sstevel@tonic-gate panic("swapslot_free(%p, %llx, %p), page: %p, null anon", 1417*0Sstevel@tonic-gate vp, off, sip, pp); 1418*0Sstevel@tonic-gate } 1419*0Sstevel@tonic-gate /* 1420*0Sstevel@tonic-gate * Free the physical slot. It may have been freed up and replaced with 1421*0Sstevel@tonic-gate * another one while we were getting the page so we have to re-verify 1422*0Sstevel@tonic-gate * that this is really one we want. If we do free the slot we have 1423*0Sstevel@tonic-gate * to mark the page modified, as its backing store is now gone. 1424*0Sstevel@tonic-gate */ 1425*0Sstevel@tonic-gate if (ap->an_pvp == sip->si_vp && ap->an_poff >= sip->si_soff && 1426*0Sstevel@tonic-gate ap->an_poff < sip->si_eoff) { 1427*0Sstevel@tonic-gate swap_phys_free(ap->an_pvp, ap->an_poff, PAGESIZE); 1428*0Sstevel@tonic-gate ap->an_pvp = NULL; 1429*0Sstevel@tonic-gate ap->an_poff = NULL; 1430*0Sstevel@tonic-gate mutex_exit(ahm); 1431*0Sstevel@tonic-gate hat_setmod(pp); 1432*0Sstevel@tonic-gate } else { 1433*0Sstevel@tonic-gate mutex_exit(ahm); 1434*0Sstevel@tonic-gate } 1435*0Sstevel@tonic-gate out: 1436*0Sstevel@tonic-gate /* Release the page locks */ 1437*0Sstevel@tonic-gate page_unlock(pp); 1438*0Sstevel@tonic-gate page_io_unlock(pp); 1439*0Sstevel@tonic-gate return (error); 1440*0Sstevel@tonic-gate } 1441*0Sstevel@tonic-gate 1442*0Sstevel@tonic-gate /* 1443*0Sstevel@tonic-gate * Get contig physical backing store for vp, in the range 1444*0Sstevel@tonic-gate * [*offp, *offp + *lenp), May back a subrange of this, but must 1445*0Sstevel@tonic-gate * always include the requested offset or fail. Returns the offsets 1446*0Sstevel@tonic-gate * backed as [*offp, *offp + *lenp) and the physical offsets used to 1447*0Sstevel@tonic-gate * back them from *pvpp in the range [*pstartp, *pstartp + *lenp). 1448*0Sstevel@tonic-gate * Returns 0 for success 1449*0Sstevel@tonic-gate * SE_NOANON -- no anon slot for requested paged 1450*0Sstevel@tonic-gate * SE_NOSWAP -- no physical swap space available 1451*0Sstevel@tonic-gate */ 1452*0Sstevel@tonic-gate int 1453*0Sstevel@tonic-gate swap_newphysname( 1454*0Sstevel@tonic-gate struct vnode *vp, 1455*0Sstevel@tonic-gate u_offset_t offset, 1456*0Sstevel@tonic-gate u_offset_t *offp, 1457*0Sstevel@tonic-gate size_t *lenp, 1458*0Sstevel@tonic-gate struct vnode **pvpp, 1459*0Sstevel@tonic-gate u_offset_t *poffp) 1460*0Sstevel@tonic-gate { 1461*0Sstevel@tonic-gate struct anon *ap = NULL; /* anon slot for vp, off */ 1462*0Sstevel@tonic-gate int error = 0; 1463*0Sstevel@tonic-gate struct vnode *pvp; 1464*0Sstevel@tonic-gate u_offset_t poff, pstart, prem; 1465*0Sstevel@tonic-gate size_t plen; 1466*0Sstevel@tonic-gate u_offset_t off, start; 1467*0Sstevel@tonic-gate kmutex_t *ahm; 1468*0Sstevel@tonic-gate 1469*0Sstevel@tonic-gate ASSERT(*offp <= offset && offset < *offp + *lenp); 1470*0Sstevel@tonic-gate 1471*0Sstevel@tonic-gate /* Get new physical swap slots. */ 1472*0Sstevel@tonic-gate plen = *lenp; 1473*0Sstevel@tonic-gate if (!swap_phys_alloc(&pvp, &pstart, &plen, 0)) { 1474*0Sstevel@tonic-gate /* 1475*0Sstevel@tonic-gate * No swap available so return error unless requested 1476*0Sstevel@tonic-gate * offset is already backed in which case return that. 1477*0Sstevel@tonic-gate */ 1478*0Sstevel@tonic-gate ahm = &anonhash_lock[AH_LOCK(vp, offset)]; 1479*0Sstevel@tonic-gate mutex_enter(ahm); 1480*0Sstevel@tonic-gate if ((ap = swap_anon(vp, offset)) == NULL) { 1481*0Sstevel@tonic-gate error = SE_NOANON; 1482*0Sstevel@tonic-gate mutex_exit(ahm); 1483*0Sstevel@tonic-gate return (error); 1484*0Sstevel@tonic-gate } 1485*0Sstevel@tonic-gate error = (ap->an_pvp ? 0 : SE_NOSWAP); 1486*0Sstevel@tonic-gate *offp = offset; 1487*0Sstevel@tonic-gate *lenp = PAGESIZE; 1488*0Sstevel@tonic-gate *pvpp = ap->an_pvp; 1489*0Sstevel@tonic-gate *poffp = ap->an_poff; 1490*0Sstevel@tonic-gate mutex_exit(ahm); 1491*0Sstevel@tonic-gate return (error); 1492*0Sstevel@tonic-gate } 1493*0Sstevel@tonic-gate 1494*0Sstevel@tonic-gate /* 1495*0Sstevel@tonic-gate * We got plen (<= *lenp) contig slots. Use these to back a 1496*0Sstevel@tonic-gate * subrange of [*offp, *offp + *lenp) which includes offset. 1497*0Sstevel@tonic-gate * For now we just put offset at the end of the kluster. 1498*0Sstevel@tonic-gate * Clearly there are other possible choices - which is best? 1499*0Sstevel@tonic-gate */ 1500*0Sstevel@tonic-gate start = MAX(*offp, 1501*0Sstevel@tonic-gate (offset + PAGESIZE > plen) ? (offset + PAGESIZE - plen) : 0); 1502*0Sstevel@tonic-gate ASSERT(start + plen <= *offp + *lenp); 1503*0Sstevel@tonic-gate 1504*0Sstevel@tonic-gate for (off = start, poff = pstart; poff < pstart + plen; 1505*0Sstevel@tonic-gate off += PAGESIZE, poff += PAGESIZE) { 1506*0Sstevel@tonic-gate ahm = &anonhash_lock[AH_LOCK(vp, off)]; 1507*0Sstevel@tonic-gate mutex_enter(ahm); 1508*0Sstevel@tonic-gate if ((ap = swap_anon(vp, off)) != NULL) { 1509*0Sstevel@tonic-gate /* Free old slot if any, and assign new one */ 1510*0Sstevel@tonic-gate if (ap->an_pvp) 1511*0Sstevel@tonic-gate swap_phys_free(ap->an_pvp, ap->an_poff, 1512*0Sstevel@tonic-gate PAGESIZE); 1513*0Sstevel@tonic-gate ap->an_pvp = pvp; 1514*0Sstevel@tonic-gate ap->an_poff = poff; 1515*0Sstevel@tonic-gate } else { /* No anon slot for a klustered page, quit. */ 1516*0Sstevel@tonic-gate prem = (pstart + plen) - poff; 1517*0Sstevel@tonic-gate /* Already did requested page, do partial kluster */ 1518*0Sstevel@tonic-gate if (off > offset) { 1519*0Sstevel@tonic-gate plen = poff - pstart; 1520*0Sstevel@tonic-gate error = 0; 1521*0Sstevel@tonic-gate /* Fail on requested page, error */ 1522*0Sstevel@tonic-gate } else if (off == offset) { 1523*0Sstevel@tonic-gate error = SE_NOANON; 1524*0Sstevel@tonic-gate /* Fail on prior page, fail on requested page, error */ 1525*0Sstevel@tonic-gate } else if ((ap = swap_anon(vp, offset)) == NULL) { 1526*0Sstevel@tonic-gate error = SE_NOANON; 1527*0Sstevel@tonic-gate /* Fail on prior page, got requested page, do only it */ 1528*0Sstevel@tonic-gate } else { 1529*0Sstevel@tonic-gate /* Free old slot if any, and assign new one */ 1530*0Sstevel@tonic-gate if (ap->an_pvp) 1531*0Sstevel@tonic-gate swap_phys_free(ap->an_pvp, ap->an_poff, 1532*0Sstevel@tonic-gate PAGESIZE); 1533*0Sstevel@tonic-gate ap->an_pvp = pvp; 1534*0Sstevel@tonic-gate ap->an_poff = poff; 1535*0Sstevel@tonic-gate /* One page kluster */ 1536*0Sstevel@tonic-gate start = offset; 1537*0Sstevel@tonic-gate plen = PAGESIZE; 1538*0Sstevel@tonic-gate pstart = poff; 1539*0Sstevel@tonic-gate poff += PAGESIZE; 1540*0Sstevel@tonic-gate prem -= PAGESIZE; 1541*0Sstevel@tonic-gate } 1542*0Sstevel@tonic-gate /* Free unassigned slots */ 1543*0Sstevel@tonic-gate swap_phys_free(pvp, poff, prem); 1544*0Sstevel@tonic-gate mutex_exit(ahm); 1545*0Sstevel@tonic-gate break; 1546*0Sstevel@tonic-gate } 1547*0Sstevel@tonic-gate mutex_exit(ahm); 1548*0Sstevel@tonic-gate } 1549*0Sstevel@tonic-gate ASSERT(*offp <= start && start + plen <= *offp + *lenp); 1550*0Sstevel@tonic-gate ASSERT(start <= offset && offset < start + plen); 1551*0Sstevel@tonic-gate *offp = start; 1552*0Sstevel@tonic-gate *lenp = plen; 1553*0Sstevel@tonic-gate *pvpp = pvp; 1554*0Sstevel@tonic-gate *poffp = pstart; 1555*0Sstevel@tonic-gate return (error); 1556*0Sstevel@tonic-gate } 1557*0Sstevel@tonic-gate 1558*0Sstevel@tonic-gate 1559*0Sstevel@tonic-gate /* 1560*0Sstevel@tonic-gate * Get the physical swap backing store location for a given anonymous page 1561*0Sstevel@tonic-gate * named (vp, off). The backing store name is returned in (*pvpp, *poffp). 1562*0Sstevel@tonic-gate * Returns 0 success 1563*0Sstevel@tonic-gate * EIDRM -- no anon slot (page is not allocated) 1564*0Sstevel@tonic-gate */ 1565*0Sstevel@tonic-gate int 1566*0Sstevel@tonic-gate swap_getphysname( 1567*0Sstevel@tonic-gate struct vnode *vp, 1568*0Sstevel@tonic-gate u_offset_t off, 1569*0Sstevel@tonic-gate struct vnode **pvpp, 1570*0Sstevel@tonic-gate u_offset_t *poffp) 1571*0Sstevel@tonic-gate { 1572*0Sstevel@tonic-gate struct anon *ap; 1573*0Sstevel@tonic-gate int error = 0; 1574*0Sstevel@tonic-gate kmutex_t *ahm; 1575*0Sstevel@tonic-gate 1576*0Sstevel@tonic-gate ahm = &anonhash_lock[AH_LOCK(vp, off)]; 1577*0Sstevel@tonic-gate mutex_enter(ahm); 1578*0Sstevel@tonic-gate 1579*0Sstevel@tonic-gate /* Get anon slot for vp, off */ 1580*0Sstevel@tonic-gate ap = swap_anon(vp, off); 1581*0Sstevel@tonic-gate if (ap == NULL) { 1582*0Sstevel@tonic-gate error = EIDRM; 1583*0Sstevel@tonic-gate goto out; 1584*0Sstevel@tonic-gate } 1585*0Sstevel@tonic-gate *pvpp = ap->an_pvp; 1586*0Sstevel@tonic-gate *poffp = ap->an_poff; 1587*0Sstevel@tonic-gate out: 1588*0Sstevel@tonic-gate mutex_exit(ahm); 1589*0Sstevel@tonic-gate return (error); 1590*0Sstevel@tonic-gate } 1591