17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5da6c28aaSamw * Common Development and Distribution License (the "License"). 6da6c28aaSamw * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 2223d9e5acSMichael Corcoran * Copyright (c) 1987, 2010, Oracle and/or its affiliates. All rights reserved. 237c478bd9Sstevel@tonic-gate */ 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 267c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 307c478bd9Sstevel@tonic-gate * The Regents of the University of California 317c478bd9Sstevel@tonic-gate * All Rights Reserved 327c478bd9Sstevel@tonic-gate * 337c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 347c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 357c478bd9Sstevel@tonic-gate * contributors. 367c478bd9Sstevel@tonic-gate */ 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate /* 397c478bd9Sstevel@tonic-gate * Each physical swap area has an associated bitmap representing 407c478bd9Sstevel@tonic-gate * its physical storage. The bitmap records which swap slots are 417c478bd9Sstevel@tonic-gate * currently allocated or freed. Allocation is done by searching 427c478bd9Sstevel@tonic-gate * through the bitmap for the first free slot. Thus, there's 437c478bd9Sstevel@tonic-gate * no linear relation between offset within the swap device and the 447c478bd9Sstevel@tonic-gate * address (within its segment(s)) of the page that the slot backs; 457c478bd9Sstevel@tonic-gate * instead, it's an arbitrary one-to-one mapping. 467c478bd9Sstevel@tonic-gate * 477c478bd9Sstevel@tonic-gate * Associated with each swap area is a swapinfo structure. These 487c478bd9Sstevel@tonic-gate * structures are linked into a linear list that determines the 497c478bd9Sstevel@tonic-gate * ordering of swap areas in the logical swap device. Each contains a 507c478bd9Sstevel@tonic-gate * pointer to the corresponding bitmap, the area's size, and its 517c478bd9Sstevel@tonic-gate * associated vnode. 527c478bd9Sstevel@tonic-gate */ 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate #include <sys/types.h> 557c478bd9Sstevel@tonic-gate #include <sys/inttypes.h> 567c478bd9Sstevel@tonic-gate #include <sys/param.h> 577c478bd9Sstevel@tonic-gate #include <sys/t_lock.h> 587c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 597c478bd9Sstevel@tonic-gate #include <sys/systm.h> 607c478bd9Sstevel@tonic-gate #include <sys/errno.h> 617c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 627c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 637c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 647c478bd9Sstevel@tonic-gate #include <sys/pathname.h> 657c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 667c478bd9Sstevel@tonic-gate #include <sys/vtrace.h> 677c478bd9Sstevel@tonic-gate #include <sys/swap.h> 687c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h> 697c478bd9Sstevel@tonic-gate #include <sys/debug.h> 707c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h> 717c478bd9Sstevel@tonic-gate #include <sys/fs/swapnode.h> 727c478bd9Sstevel@tonic-gate #include <sys/policy.h> 737c478bd9Sstevel@tonic-gate #include <sys/zone.h> 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate #include <vm/as.h> 767c478bd9Sstevel@tonic-gate #include <vm/seg.h> 777c478bd9Sstevel@tonic-gate #include <vm/page.h> 787c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h> 797c478bd9Sstevel@tonic-gate #include <vm/hat.h> 807c478bd9Sstevel@tonic-gate #include <vm/anon.h> 817c478bd9Sstevel@tonic-gate #include <vm/seg_map.h> 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate /* 847c478bd9Sstevel@tonic-gate * To balance the load among multiple swap areas, we don't allow 857c478bd9Sstevel@tonic-gate * more than swap_maxcontig allocations to be satisfied from a 867c478bd9Sstevel@tonic-gate * single swap area before moving on to the next swap area. This 877c478bd9Sstevel@tonic-gate * effectively "interleaves" allocations among the many swap areas. 887c478bd9Sstevel@tonic-gate */ 897c478bd9Sstevel@tonic-gate int swap_maxcontig; /* set by anon_init() to 1 Mb */ 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate #define MINIROOTSIZE 12000 /* ~6 Meg XXX */ 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate /* 947c478bd9Sstevel@tonic-gate * XXX - this lock is a kludge. It serializes some aspects of swapadd() and 957c478bd9Sstevel@tonic-gate * swapdel() (namely VOP_OPEN, VOP_CLOSE, VN_RELE). It protects against 967c478bd9Sstevel@tonic-gate * somebody swapadd'ing and getting swap slots from a vnode, while someone 977c478bd9Sstevel@tonic-gate * else is in the process of closing or rele'ing it. 987c478bd9Sstevel@tonic-gate */ 997c478bd9Sstevel@tonic-gate static kmutex_t swap_lock; 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate kmutex_t swapinfo_lock; 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate /* 1047c478bd9Sstevel@tonic-gate * protected by the swapinfo_lock 1057c478bd9Sstevel@tonic-gate */ 1067c478bd9Sstevel@tonic-gate struct swapinfo *swapinfo; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate static struct swapinfo *silast; 1097c478bd9Sstevel@tonic-gate static int nswapfiles; 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate static u_offset_t swap_getoff(struct swapinfo *); 1127c478bd9Sstevel@tonic-gate static int swapadd(struct vnode *, ulong_t, ulong_t, char *); 1137c478bd9Sstevel@tonic-gate static int swapdel(struct vnode *, ulong_t); 1147c478bd9Sstevel@tonic-gate static int swapslot_free(struct vnode *, u_offset_t, struct swapinfo *); 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate /* 1177c478bd9Sstevel@tonic-gate * swap device bitmap allocation macros 1187c478bd9Sstevel@tonic-gate */ 1197c478bd9Sstevel@tonic-gate #define MAPSHIFT 5 1207c478bd9Sstevel@tonic-gate #define NBBW (NBPW * NBBY) /* number of bits per word */ 1217c478bd9Sstevel@tonic-gate #define TESTBIT(map, i) (((map)[(i) >> MAPSHIFT] & (1 << (i) % NBBW))) 1227c478bd9Sstevel@tonic-gate #define SETBIT(map, i) (((map)[(i) >> MAPSHIFT] |= (1 << (i) % NBBW))) 1237c478bd9Sstevel@tonic-gate #define CLEARBIT(map, i) (((map)[(i) >> MAPSHIFT] &= ~(1 << (i) % NBBW))) 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate int swap_debug = 0; /* set for debug printf's */ 1267c478bd9Sstevel@tonic-gate int swap_verify = 0; /* set to verify slots when freeing and allocating */ 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate uint_t swapalloc_maxcontig; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate /* 1317c478bd9Sstevel@tonic-gate * Allocate a range of up to *lenp contiguous slots (page) from a physical 1327c478bd9Sstevel@tonic-gate * swap device. Flags are one of: 1337c478bd9Sstevel@tonic-gate * SA_NOT Must have a slot from a physical swap device other than the 1347c478bd9Sstevel@tonic-gate * the one containing input (*vpp, *offp). 1357c478bd9Sstevel@tonic-gate * Less slots than requested may be returned. *lenp allocated slots are 1367c478bd9Sstevel@tonic-gate * returned starting at *offp on *vpp. 1377c478bd9Sstevel@tonic-gate * Returns 1 for a successful allocation, 0 for couldn't allocate any slots. 1387c478bd9Sstevel@tonic-gate */ 1397c478bd9Sstevel@tonic-gate int 1407c478bd9Sstevel@tonic-gate swap_phys_alloc( 1417c478bd9Sstevel@tonic-gate struct vnode **vpp, 1427c478bd9Sstevel@tonic-gate u_offset_t *offp, 1437c478bd9Sstevel@tonic-gate size_t *lenp, 1447c478bd9Sstevel@tonic-gate uint_t flags) 1457c478bd9Sstevel@tonic-gate { 1467c478bd9Sstevel@tonic-gate struct swapinfo *sip; 1477c478bd9Sstevel@tonic-gate offset_t soff, noff; 1487c478bd9Sstevel@tonic-gate size_t len; 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 1517c478bd9Sstevel@tonic-gate sip = silast; 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate /* Find a desirable physical device and allocate from it. */ 1547c478bd9Sstevel@tonic-gate do { 1557c478bd9Sstevel@tonic-gate if (sip == NULL) 1567c478bd9Sstevel@tonic-gate break; 1577c478bd9Sstevel@tonic-gate if (!(sip->si_flags & ST_INDEL) && 1587c478bd9Sstevel@tonic-gate (spgcnt_t)sip->si_nfpgs > 0) { 1597c478bd9Sstevel@tonic-gate /* Caller wants other than specified swap device */ 1607c478bd9Sstevel@tonic-gate if (flags & SA_NOT) { 1617c478bd9Sstevel@tonic-gate if (*vpp != sip->si_vp || 1627c478bd9Sstevel@tonic-gate *offp < sip->si_soff || 1637c478bd9Sstevel@tonic-gate *offp >= sip->si_eoff) 1647c478bd9Sstevel@tonic-gate goto found; 1657c478bd9Sstevel@tonic-gate /* Caller is loose, will take anything */ 1667c478bd9Sstevel@tonic-gate } else 1677c478bd9Sstevel@tonic-gate goto found; 1687c478bd9Sstevel@tonic-gate } else if (sip->si_nfpgs == 0) 1697c478bd9Sstevel@tonic-gate sip->si_allocs = 0; 1707c478bd9Sstevel@tonic-gate if ((sip = sip->si_next) == NULL) 1717c478bd9Sstevel@tonic-gate sip = swapinfo; 1727c478bd9Sstevel@tonic-gate } while (sip != silast); 1737c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 1747c478bd9Sstevel@tonic-gate return (0); 1757c478bd9Sstevel@tonic-gate found: 1767c478bd9Sstevel@tonic-gate soff = swap_getoff(sip); 1777c478bd9Sstevel@tonic-gate sip->si_nfpgs--; 1787c478bd9Sstevel@tonic-gate if (soff == -1) 1797c478bd9Sstevel@tonic-gate panic("swap_alloc: swap_getoff failed!"); 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate for (len = PAGESIZE; len < *lenp; len += PAGESIZE) { 1827c478bd9Sstevel@tonic-gate if (sip->si_nfpgs == 0) 1837c478bd9Sstevel@tonic-gate break; 1847c478bd9Sstevel@tonic-gate if (swapalloc_maxcontig && len >= swapalloc_maxcontig) 1857c478bd9Sstevel@tonic-gate break; 1867c478bd9Sstevel@tonic-gate noff = swap_getoff(sip); 1877c478bd9Sstevel@tonic-gate if (noff == -1) { 1887c478bd9Sstevel@tonic-gate break; 1897c478bd9Sstevel@tonic-gate } else if (noff != soff + len) { 1907c478bd9Sstevel@tonic-gate CLEARBIT(sip->si_swapslots, btop(noff - sip->si_soff)); 1917c478bd9Sstevel@tonic-gate break; 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate sip->si_nfpgs--; 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate *vpp = sip->si_vp; 1967c478bd9Sstevel@tonic-gate *offp = soff; 1977c478bd9Sstevel@tonic-gate *lenp = len; 1987c478bd9Sstevel@tonic-gate ASSERT((spgcnt_t)sip->si_nfpgs >= 0); 1997c478bd9Sstevel@tonic-gate sip->si_allocs += btop(len); 2007c478bd9Sstevel@tonic-gate if (sip->si_allocs >= swap_maxcontig) { 2017c478bd9Sstevel@tonic-gate sip->si_allocs = 0; 2027c478bd9Sstevel@tonic-gate if ((silast = sip->si_next) == NULL) 2037c478bd9Sstevel@tonic-gate silast = swapinfo; 2047c478bd9Sstevel@tonic-gate } 2057c478bd9Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_SWAP_ALLOC, 2067c478bd9Sstevel@tonic-gate "swap_alloc:sip %p offset %lx", sip, soff); 2077c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 2087c478bd9Sstevel@tonic-gate return (1); 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate int swap_backsearch = 0; 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate /* 2147c478bd9Sstevel@tonic-gate * Get a free offset on swap device sip. 2157c478bd9Sstevel@tonic-gate * Return >=0 offset if succeeded, -1 for failure. 2167c478bd9Sstevel@tonic-gate */ 2177c478bd9Sstevel@tonic-gate static u_offset_t 2187c478bd9Sstevel@tonic-gate swap_getoff(struct swapinfo *sip) 2197c478bd9Sstevel@tonic-gate { 2207c478bd9Sstevel@tonic-gate uint_t *sp, *ep; 2217c478bd9Sstevel@tonic-gate size_t aoff, boff, poff, slotnumber; 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&swapinfo_lock)); 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate sip->si_alloccnt++; 2267c478bd9Sstevel@tonic-gate for (sp = &sip->si_swapslots[sip->si_hint >> MAPSHIFT], 2277c478bd9Sstevel@tonic-gate ep = &sip->si_swapslots[sip->si_mapsize / NBPW]; sp < ep; sp++) { 2287c478bd9Sstevel@tonic-gate if (*sp != (uint_t)0xffffffff) 2297c478bd9Sstevel@tonic-gate goto foundentry; 2307c478bd9Sstevel@tonic-gate else 2317c478bd9Sstevel@tonic-gate sip->si_checkcnt++; 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate SWAP_PRINT(SW_ALLOC, 2347c478bd9Sstevel@tonic-gate "swap_getoff: couldn't find slot from hint %ld to end\n", 2357c478bd9Sstevel@tonic-gate sip->si_hint, 0, 0, 0, 0); 2367c478bd9Sstevel@tonic-gate /* 2377c478bd9Sstevel@tonic-gate * Go backwards? Check for faster method XXX 2387c478bd9Sstevel@tonic-gate */ 2397c478bd9Sstevel@tonic-gate if (swap_backsearch) { 2407c478bd9Sstevel@tonic-gate for (sp = &sip->si_swapslots[sip->si_hint >> MAPSHIFT], 2417c478bd9Sstevel@tonic-gate ep = sip->si_swapslots; sp > ep; sp--) { 2427c478bd9Sstevel@tonic-gate if (*sp != (uint_t)0xffffffff) 2437c478bd9Sstevel@tonic-gate goto foundentry; 2447c478bd9Sstevel@tonic-gate else 2457c478bd9Sstevel@tonic-gate sip->si_checkcnt++; 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate } else { 2487c478bd9Sstevel@tonic-gate for (sp = sip->si_swapslots, 2497c478bd9Sstevel@tonic-gate ep = &sip->si_swapslots[sip->si_hint >> MAPSHIFT]; 2507c478bd9Sstevel@tonic-gate sp < ep; sp++) { 2517c478bd9Sstevel@tonic-gate if (*sp != (uint_t)0xffffffff) 2527c478bd9Sstevel@tonic-gate goto foundentry; 2537c478bd9Sstevel@tonic-gate else 2547c478bd9Sstevel@tonic-gate sip->si_checkcnt++; 2557c478bd9Sstevel@tonic-gate } 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate if (*sp == 0xffffffff) { 2587c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "No free swap slots!"); 2597c478bd9Sstevel@tonic-gate return ((u_offset_t)-1); 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate foundentry: 2637c478bd9Sstevel@tonic-gate /* 2647c478bd9Sstevel@tonic-gate * aoff is the page number offset (in bytes) of the si_swapslots 2657c478bd9Sstevel@tonic-gate * array element containing a free page 2667c478bd9Sstevel@tonic-gate * 2677c478bd9Sstevel@tonic-gate * boff is the page number offset of the free page 2687c478bd9Sstevel@tonic-gate * (i.e. cleared bit) in si_swapslots[aoff]. 2697c478bd9Sstevel@tonic-gate */ 2707c478bd9Sstevel@tonic-gate aoff = ((char *)sp - (char *)sip->si_swapslots) * NBBY; 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate for (boff = (sip->si_hint % NBBW); boff < NBBW; boff++) { 2737c478bd9Sstevel@tonic-gate if (!TESTBIT(sip->si_swapslots, aoff + boff)) 2747c478bd9Sstevel@tonic-gate goto foundslot; 2757c478bd9Sstevel@tonic-gate else 2767c478bd9Sstevel@tonic-gate sip->si_checkcnt++; 2777c478bd9Sstevel@tonic-gate } 2787c478bd9Sstevel@tonic-gate for (boff = 0; boff < (sip->si_hint % NBBW); boff++) { 2797c478bd9Sstevel@tonic-gate if (!TESTBIT(sip->si_swapslots, aoff + boff)) 2807c478bd9Sstevel@tonic-gate goto foundslot; 2817c478bd9Sstevel@tonic-gate else 2827c478bd9Sstevel@tonic-gate sip->si_checkcnt++; 2837c478bd9Sstevel@tonic-gate } 2847c478bd9Sstevel@tonic-gate panic("swap_getoff: didn't find slot in word hint %ld", sip->si_hint); 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate foundslot: 2877c478bd9Sstevel@tonic-gate /* 2887c478bd9Sstevel@tonic-gate * Return the offset of the free page in swap device. 2897c478bd9Sstevel@tonic-gate * Convert page number of byte offset and add starting 2907c478bd9Sstevel@tonic-gate * offset of swap device. 2917c478bd9Sstevel@tonic-gate */ 2927c478bd9Sstevel@tonic-gate slotnumber = aoff + boff; 2937c478bd9Sstevel@tonic-gate SWAP_PRINT(SW_ALLOC, "swap_getoff: allocating slot %ld\n", 2947c478bd9Sstevel@tonic-gate slotnumber, 0, 0, 0, 0); 2957c478bd9Sstevel@tonic-gate poff = ptob(slotnumber); 2967c478bd9Sstevel@tonic-gate if (poff + sip->si_soff >= sip->si_eoff) 2977c478bd9Sstevel@tonic-gate printf("ptob(aoff(%ld) + boff(%ld))(%ld) >= eoff(%ld)\n", 2987c478bd9Sstevel@tonic-gate aoff, boff, ptob(slotnumber), (long)sip->si_eoff); 2997c478bd9Sstevel@tonic-gate ASSERT(poff < sip->si_eoff); 3007c478bd9Sstevel@tonic-gate /* 3017c478bd9Sstevel@tonic-gate * We could verify here that the slot isn't already allocated 3027c478bd9Sstevel@tonic-gate * by looking through all the anon slots. 3037c478bd9Sstevel@tonic-gate */ 3047c478bd9Sstevel@tonic-gate SETBIT(sip->si_swapslots, slotnumber); 3057c478bd9Sstevel@tonic-gate sip->si_hint = slotnumber + 1; /* hint = next slot */ 3067c478bd9Sstevel@tonic-gate return (poff + sip->si_soff); 3077c478bd9Sstevel@tonic-gate } 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate /* 3107c478bd9Sstevel@tonic-gate * Free a swap page. 3117c478bd9Sstevel@tonic-gate */ 3127c478bd9Sstevel@tonic-gate void 3137c478bd9Sstevel@tonic-gate swap_phys_free(struct vnode *vp, u_offset_t off, size_t len) 3147c478bd9Sstevel@tonic-gate { 3157c478bd9Sstevel@tonic-gate struct swapinfo *sip; 3167c478bd9Sstevel@tonic-gate ssize_t pagenumber, npage; 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 3197c478bd9Sstevel@tonic-gate sip = swapinfo; 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate do { 3227c478bd9Sstevel@tonic-gate if (sip->si_vp == vp && 3237c478bd9Sstevel@tonic-gate sip->si_soff <= off && off < sip->si_eoff) { 3247c478bd9Sstevel@tonic-gate for (pagenumber = btop(off - sip->si_soff), 3257c478bd9Sstevel@tonic-gate npage = btop(len) + pagenumber; 3267c478bd9Sstevel@tonic-gate pagenumber < npage; pagenumber++) { 3277c478bd9Sstevel@tonic-gate SWAP_PRINT(SW_ALLOC, 3287c478bd9Sstevel@tonic-gate "swap_phys_free: freeing slot %ld on " 3297c478bd9Sstevel@tonic-gate "sip %p\n", 3307c478bd9Sstevel@tonic-gate pagenumber, sip, 0, 0, 0); 3317c478bd9Sstevel@tonic-gate if (!TESTBIT(sip->si_swapslots, pagenumber)) { 3327c478bd9Sstevel@tonic-gate panic( 3337c478bd9Sstevel@tonic-gate "swap_phys_free: freeing free slot " 3347c478bd9Sstevel@tonic-gate "%p,%lx\n", (void *)vp, 3357c478bd9Sstevel@tonic-gate ptob(pagenumber) + sip->si_soff); 3367c478bd9Sstevel@tonic-gate } 3377c478bd9Sstevel@tonic-gate CLEARBIT(sip->si_swapslots, pagenumber); 3387c478bd9Sstevel@tonic-gate sip->si_nfpgs++; 3397c478bd9Sstevel@tonic-gate } 3407c478bd9Sstevel@tonic-gate ASSERT(sip->si_nfpgs <= sip->si_npgs); 3417c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 3427c478bd9Sstevel@tonic-gate return; 3437c478bd9Sstevel@tonic-gate } 3447c478bd9Sstevel@tonic-gate } while ((sip = sip->si_next) != NULL); 3457c478bd9Sstevel@tonic-gate panic("swap_phys_free"); 3467c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 3477c478bd9Sstevel@tonic-gate } 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate /* 3507c478bd9Sstevel@tonic-gate * Return the anon struct corresponding for the given 3517c478bd9Sstevel@tonic-gate * <vnode, off> if it is part of the virtual swap device. 3527c478bd9Sstevel@tonic-gate * Return the anon struct if found, otherwise NULL. 3537c478bd9Sstevel@tonic-gate */ 3547c478bd9Sstevel@tonic-gate struct anon * 3557c478bd9Sstevel@tonic-gate swap_anon(struct vnode *vp, u_offset_t off) 3567c478bd9Sstevel@tonic-gate { 3577c478bd9Sstevel@tonic-gate struct anon *ap; 3587c478bd9Sstevel@tonic-gate 35923d9e5acSMichael Corcoran ASSERT(MUTEX_HELD(AH_MUTEX(vp, off))); 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate for (ap = anon_hash[ANON_HASH(vp, off)]; ap != NULL; ap = ap->an_hash) { 3627c478bd9Sstevel@tonic-gate if (ap->an_vp == vp && ap->an_off == off) 3637c478bd9Sstevel@tonic-gate return (ap); 3647c478bd9Sstevel@tonic-gate } 3657c478bd9Sstevel@tonic-gate return (NULL); 3667c478bd9Sstevel@tonic-gate } 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate 3697c478bd9Sstevel@tonic-gate /* 3707c478bd9Sstevel@tonic-gate * Determine if the vp offset range overlap a swap device. 3717c478bd9Sstevel@tonic-gate */ 3727c478bd9Sstevel@tonic-gate int 3737c478bd9Sstevel@tonic-gate swap_in_range(struct vnode *vp, u_offset_t offset, size_t len) 3747c478bd9Sstevel@tonic-gate { 3757c478bd9Sstevel@tonic-gate struct swapinfo *sip; 3767c478bd9Sstevel@tonic-gate u_offset_t eoff; 3777c478bd9Sstevel@tonic-gate 3787c478bd9Sstevel@tonic-gate eoff = offset + len; 3797c478bd9Sstevel@tonic-gate ASSERT(eoff > offset); 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 3827c478bd9Sstevel@tonic-gate sip = swapinfo; 3837c478bd9Sstevel@tonic-gate if (vp && sip) { 3847c478bd9Sstevel@tonic-gate do { 3857c478bd9Sstevel@tonic-gate if (vp != sip->si_vp || eoff <= sip->si_soff || 3867c478bd9Sstevel@tonic-gate offset >= sip->si_eoff) 3877c478bd9Sstevel@tonic-gate continue; 3887c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 3897c478bd9Sstevel@tonic-gate return (1); 3907c478bd9Sstevel@tonic-gate } while ((sip = sip->si_next) != NULL); 3917c478bd9Sstevel@tonic-gate } 3927c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 3937c478bd9Sstevel@tonic-gate return (0); 3947c478bd9Sstevel@tonic-gate } 3957c478bd9Sstevel@tonic-gate 3967c478bd9Sstevel@tonic-gate /* 3977c478bd9Sstevel@tonic-gate * See if name is one of our swap files 3987c478bd9Sstevel@tonic-gate * even though lookupname failed. 3997c478bd9Sstevel@tonic-gate * This can be used by swapdel to delete 4007c478bd9Sstevel@tonic-gate * swap resources on remote machines 4017c478bd9Sstevel@tonic-gate * where the link has gone down. 4027c478bd9Sstevel@tonic-gate */ 4037c478bd9Sstevel@tonic-gate static struct vnode * 4047c478bd9Sstevel@tonic-gate swapdel_byname( 4057c478bd9Sstevel@tonic-gate char *name, /* pathname to delete */ 4067c478bd9Sstevel@tonic-gate ulong_t lowblk) /* Low block number of area to delete */ 4077c478bd9Sstevel@tonic-gate { 4087c478bd9Sstevel@tonic-gate struct swapinfo **sipp, *osip; 4097c478bd9Sstevel@tonic-gate u_offset_t soff; 4107c478bd9Sstevel@tonic-gate 4117c478bd9Sstevel@tonic-gate /* 4127c478bd9Sstevel@tonic-gate * Find the swap file entry for the file to 4137c478bd9Sstevel@tonic-gate * be deleted. Skip any entries that are in 4147c478bd9Sstevel@tonic-gate * transition. 4157c478bd9Sstevel@tonic-gate */ 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate soff = ptob(btopr(lowblk << SCTRSHFT)); /* must be page aligned */ 4187c478bd9Sstevel@tonic-gate 4197c478bd9Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 4207c478bd9Sstevel@tonic-gate for (sipp = &swapinfo; (osip = *sipp) != NULL; sipp = &osip->si_next) { 4217c478bd9Sstevel@tonic-gate if ((strcmp(osip->si_pname, name) == 0) && 4227c478bd9Sstevel@tonic-gate (osip->si_soff == soff) && (osip->si_flags == 0)) { 4237c478bd9Sstevel@tonic-gate struct vnode *vp = osip->si_vp; 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate VN_HOLD(vp); 4267c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 4277c478bd9Sstevel@tonic-gate return (vp); 4287c478bd9Sstevel@tonic-gate } 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 4317c478bd9Sstevel@tonic-gate return (NULL); 4327c478bd9Sstevel@tonic-gate } 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate 4357c478bd9Sstevel@tonic-gate /* 4367c478bd9Sstevel@tonic-gate * New system call to manipulate swap files. 4377c478bd9Sstevel@tonic-gate */ 4387c478bd9Sstevel@tonic-gate int 4397c478bd9Sstevel@tonic-gate swapctl(int sc_cmd, void *sc_arg, int *rv) 4407c478bd9Sstevel@tonic-gate { 4417c478bd9Sstevel@tonic-gate struct swapinfo *sip, *csip, *tsip; 4427c478bd9Sstevel@tonic-gate int error = 0; 4437c478bd9Sstevel@tonic-gate struct swapent st, *ust; 4447c478bd9Sstevel@tonic-gate struct swapres sr; 4457c478bd9Sstevel@tonic-gate struct vnode *vp; 4467c478bd9Sstevel@tonic-gate int cnt = 0; 4477c478bd9Sstevel@tonic-gate int tmp_nswapfiles; 4487c478bd9Sstevel@tonic-gate int nswap; 4497c478bd9Sstevel@tonic-gate int length, nlen; 4507c478bd9Sstevel@tonic-gate int gplen = 0, plen; 4517c478bd9Sstevel@tonic-gate char *swapname; 4527c478bd9Sstevel@tonic-gate char *pname; 4537c478bd9Sstevel@tonic-gate char *tpname; 4547c478bd9Sstevel@tonic-gate struct anoninfo ai; 4557c478bd9Sstevel@tonic-gate spgcnt_t avail; 4567c478bd9Sstevel@tonic-gate int global = INGLOBALZONE(curproc); 45700792c0bS struct zone *zp = curproc->p_zone; 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate /* 4607c478bd9Sstevel@tonic-gate * When running in a zone we want to hide the details of the swap 4617c478bd9Sstevel@tonic-gate * devices: we report there only being one swap device named "swap" 4627c478bd9Sstevel@tonic-gate * having a size equal to the sum of the sizes of all real swap devices 4637c478bd9Sstevel@tonic-gate * on the system. 4647c478bd9Sstevel@tonic-gate */ 4657c478bd9Sstevel@tonic-gate switch (sc_cmd) { 4667c478bd9Sstevel@tonic-gate case SC_GETNSWP: 4677c478bd9Sstevel@tonic-gate if (global) 4687c478bd9Sstevel@tonic-gate *rv = nswapfiles; 4697c478bd9Sstevel@tonic-gate else 4707c478bd9Sstevel@tonic-gate *rv = 1; 4717c478bd9Sstevel@tonic-gate return (0); 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate case SC_AINFO: 4747c478bd9Sstevel@tonic-gate /* 4757c478bd9Sstevel@tonic-gate * Return anoninfo information with these changes: 4767c478bd9Sstevel@tonic-gate * ani_max = maximum amount of swap space 4777c478bd9Sstevel@tonic-gate * (including potentially available physical memory) 4787c478bd9Sstevel@tonic-gate * ani_free = amount of unallocated anonymous memory 4797c478bd9Sstevel@tonic-gate * (some of which might be reserved and including 4807c478bd9Sstevel@tonic-gate * potentially available physical memory) 4817c478bd9Sstevel@tonic-gate * ani_resv = amount of claimed (reserved) anonymous memory 4827c478bd9Sstevel@tonic-gate */ 4837c478bd9Sstevel@tonic-gate avail = MAX((spgcnt_t)(availrmem - swapfs_minfree), 0); 4847c478bd9Sstevel@tonic-gate ai.ani_max = (k_anoninfo.ani_max + 4857c478bd9Sstevel@tonic-gate k_anoninfo.ani_mem_resv) + avail; 4867c478bd9Sstevel@tonic-gate 487*b52a336eSPavel Tatashin /* Update ani_free */ 488*b52a336eSPavel Tatashin set_anoninfo(); 4897c478bd9Sstevel@tonic-gate ai.ani_free = k_anoninfo.ani_free + avail; 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate ai.ani_resv = k_anoninfo.ani_phys_resv + 4927c478bd9Sstevel@tonic-gate k_anoninfo.ani_mem_resv; 4937c478bd9Sstevel@tonic-gate 49400792c0bS if (!global && zp->zone_max_swap_ctl != UINT64_MAX) { 49500792c0bS /* 49600792c0bS * We're in a non-global zone with a swap cap. We 49700792c0bS * always report the system-wide values for the global 49800792c0bS * zone, even though it too can have a swap cap. 49900792c0bS */ 50000792c0bS 50100792c0bS /* 50200792c0bS * For a swap-capped zone, the numbers are contrived 50300792c0bS * since we don't have a correct value of 'reserved' 50400792c0bS * for the zone. 50500792c0bS * 50600792c0bS * The ani_max value is always the zone's swap cap. 50700792c0bS * 50800792c0bS * The ani_free value is always the difference between 50900792c0bS * the cap and the amount of swap in use by the zone. 51000792c0bS * 51100792c0bS * The ani_resv value is typically set to be the amount 51200792c0bS * of swap in use by the zone, but can be adjusted 51300792c0bS * upwards to indicate how much swap is currently 51400792c0bS * unavailable to that zone due to usage by entities 51500792c0bS * outside the zone. 51600792c0bS * 51700792c0bS * This works as follows. 51800792c0bS * 51900792c0bS * In the 'swap -s' output, the data is displayed 52000792c0bS * as follows: 52100792c0bS * allocated = ani_max - ani_free 52200792c0bS * reserved = ani_resv - allocated 52300792c0bS * available = ani_max - ani_resv 52400792c0bS * 52500792c0bS * Taking a contrived example, if the swap cap is 100 52600792c0bS * and the amount of swap used by the zone is 75, this 52700792c0bS * gives: 52800792c0bS * allocated = ani_max - ani_free = 100 - 25 = 75 52900792c0bS * reserved = ani_resv - allocated = 75 - 75 = 0 53000792c0bS * available = ani_max - ani_resv = 100 - 75 = 25 53100792c0bS * 53200792c0bS * In this typical case, you can see that the 'swap -s' 53300792c0bS * 'reserved' will always be 0 inside a swap capped 53400792c0bS * zone. 53500792c0bS * 53600792c0bS * However, if the system as a whole has less free 53700792c0bS * swap than the zone limits allow, then we adjust 53800792c0bS * the ani_resv value up so that it is the difference 53900792c0bS * between the zone cap and the amount of free system 54000792c0bS * swap. Taking the above example, but when the 54100792c0bS * system as a whole only has 20 of swap available, we 54200792c0bS * get an ani_resv of 100 - 20 = 80. This gives: 54300792c0bS * allocated = ani_max - ani_free = 100 - 25 = 75 54400792c0bS * reserved = ani_resv - allocated = 80 - 75 = 5 54500792c0bS * available = ani_max - ani_resv = 100 - 80 = 20 54600792c0bS * 54700792c0bS * In this case, you can see how the ani_resv value is 54800792c0bS * tweaked up to make the 'swap -s' numbers work inside 54900792c0bS * the zone. 55000792c0bS */ 55100792c0bS rctl_qty_t cap, used; 55200792c0bS pgcnt_t pgcap, sys_avail; 55300792c0bS 55400792c0bS mutex_enter(&zp->zone_mem_lock); 55500792c0bS cap = zp->zone_max_swap_ctl; 55600792c0bS used = zp->zone_max_swap; 55700792c0bS mutex_exit(&zp->zone_mem_lock); 55800792c0bS 55900792c0bS pgcap = MIN(btop(cap), ai.ani_max); 56000792c0bS ai.ani_free = pgcap - btop(used); 56100792c0bS 56200792c0bS /* Get the system-wide swap currently available. */ 56300792c0bS sys_avail = ai.ani_max - ai.ani_resv; 56400792c0bS if (sys_avail < ai.ani_free) 56500792c0bS ai.ani_resv = pgcap - sys_avail; 56600792c0bS else 56700792c0bS ai.ani_resv = btop(used); 56800792c0bS 56900792c0bS ai.ani_max = pgcap; 57000792c0bS } 57100792c0bS 5727c478bd9Sstevel@tonic-gate if (copyout(&ai, sc_arg, sizeof (struct anoninfo)) != 0) 5737c478bd9Sstevel@tonic-gate return (EFAULT); 5747c478bd9Sstevel@tonic-gate return (0); 5757c478bd9Sstevel@tonic-gate 5767c478bd9Sstevel@tonic-gate case SC_LIST: 5777c478bd9Sstevel@tonic-gate if (copyin(sc_arg, &length, sizeof (int)) != 0) 5787c478bd9Sstevel@tonic-gate return (EFAULT); 5797c478bd9Sstevel@tonic-gate if (!global) { 5807c478bd9Sstevel@tonic-gate struct swapent st; 5817c478bd9Sstevel@tonic-gate char *swappath = "swap"; 5827c478bd9Sstevel@tonic-gate 5837c478bd9Sstevel@tonic-gate if (length < 1) 5847c478bd9Sstevel@tonic-gate return (ENOMEM); 5857c478bd9Sstevel@tonic-gate ust = (swapent_t *)((swaptbl_t *)sc_arg)->swt_ent; 5867c478bd9Sstevel@tonic-gate if (copyin(ust, &st, sizeof (swapent_t)) != 0) 5877c478bd9Sstevel@tonic-gate return (EFAULT); 5887c478bd9Sstevel@tonic-gate st.ste_start = PAGESIZE >> SCTRSHFT; 5897c478bd9Sstevel@tonic-gate st.ste_length = (off_t)0; 5907c478bd9Sstevel@tonic-gate st.ste_pages = 0; 5917c478bd9Sstevel@tonic-gate st.ste_free = 0; 5927c478bd9Sstevel@tonic-gate st.ste_flags = 0; 59300792c0bS 5947c478bd9Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 5957c478bd9Sstevel@tonic-gate for (sip = swapinfo, nswap = 0; 5967c478bd9Sstevel@tonic-gate sip != NULL && nswap < nswapfiles; 5977c478bd9Sstevel@tonic-gate sip = sip->si_next, nswap++) { 5987c478bd9Sstevel@tonic-gate st.ste_length += 5997c478bd9Sstevel@tonic-gate (sip->si_eoff - sip->si_soff) >> SCTRSHFT; 6007c478bd9Sstevel@tonic-gate st.ste_pages += sip->si_npgs; 6017c478bd9Sstevel@tonic-gate st.ste_free += sip->si_nfpgs; 6027c478bd9Sstevel@tonic-gate } 6037c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 60400792c0bS 60500792c0bS if (zp->zone_max_swap_ctl != UINT64_MAX) { 60600792c0bS rctl_qty_t cap, used; 60700792c0bS 60800792c0bS mutex_enter(&zp->zone_mem_lock); 60900792c0bS cap = zp->zone_max_swap_ctl; 61000792c0bS used = zp->zone_max_swap; 61100792c0bS mutex_exit(&zp->zone_mem_lock); 61200792c0bS 61300792c0bS st.ste_length = MIN(cap, st.ste_length); 61400792c0bS st.ste_pages = MIN(btop(cap), st.ste_pages); 61500792c0bS st.ste_free = MIN(st.ste_pages - btop(used), 61600792c0bS st.ste_free); 61700792c0bS } 61800792c0bS 6197c478bd9Sstevel@tonic-gate if (copyout(&st, ust, sizeof (swapent_t)) != 0 || 6207c478bd9Sstevel@tonic-gate copyout(swappath, st.ste_path, 6217c478bd9Sstevel@tonic-gate strlen(swappath) + 1) != 0) { 6227c478bd9Sstevel@tonic-gate return (EFAULT); 6237c478bd9Sstevel@tonic-gate } 6247c478bd9Sstevel@tonic-gate *rv = 1; 6257c478bd9Sstevel@tonic-gate return (0); 6267c478bd9Sstevel@tonic-gate } 6277c478bd9Sstevel@tonic-gate beginning: 6287c478bd9Sstevel@tonic-gate tmp_nswapfiles = nswapfiles; 6297c478bd9Sstevel@tonic-gate /* Return an error if not enough space for the whole table. */ 6307c478bd9Sstevel@tonic-gate if (length < tmp_nswapfiles) 6317c478bd9Sstevel@tonic-gate return (ENOMEM); 6327c478bd9Sstevel@tonic-gate /* 6337c478bd9Sstevel@tonic-gate * Get memory to hold the swap entries and their names. We'll 6347c478bd9Sstevel@tonic-gate * copy the real entries into these and then copy these out. 6357c478bd9Sstevel@tonic-gate * Allocating the pathname memory is only a guess so we may 6367c478bd9Sstevel@tonic-gate * find that we need more and have to do it again. 6377c478bd9Sstevel@tonic-gate * All this is because we have to hold the anon lock while 6387c478bd9Sstevel@tonic-gate * traversing the swapinfo list, and we can't be doing copyouts 6397c478bd9Sstevel@tonic-gate * and/or kmem_alloc()s during this. 6407c478bd9Sstevel@tonic-gate */ 6417c478bd9Sstevel@tonic-gate csip = kmem_zalloc(tmp_nswapfiles * sizeof (struct swapinfo), 6427c478bd9Sstevel@tonic-gate KM_SLEEP); 6437c478bd9Sstevel@tonic-gate retry: 6447c478bd9Sstevel@tonic-gate nlen = tmp_nswapfiles * (gplen += 100); 6457c478bd9Sstevel@tonic-gate pname = kmem_zalloc(nlen, KM_SLEEP); 6467c478bd9Sstevel@tonic-gate 6477c478bd9Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 6487c478bd9Sstevel@tonic-gate 6497c478bd9Sstevel@tonic-gate if (tmp_nswapfiles != nswapfiles) { 6507c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 6517c478bd9Sstevel@tonic-gate kmem_free(pname, nlen); 6527c478bd9Sstevel@tonic-gate kmem_free(csip, 6537c478bd9Sstevel@tonic-gate tmp_nswapfiles * sizeof (struct swapinfo)); 6547c478bd9Sstevel@tonic-gate gplen = 0; 6557c478bd9Sstevel@tonic-gate goto beginning; 6567c478bd9Sstevel@tonic-gate } 6577c478bd9Sstevel@tonic-gate for (sip = swapinfo, tsip = csip, tpname = pname, nswap = 0; 6587c478bd9Sstevel@tonic-gate sip && nswap < tmp_nswapfiles; 6597c478bd9Sstevel@tonic-gate sip = sip->si_next, tsip++, tpname += plen, nswap++) { 6607c478bd9Sstevel@tonic-gate plen = sip->si_pnamelen; 6617c478bd9Sstevel@tonic-gate if (tpname + plen - pname > nlen) { 6627c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 6637c478bd9Sstevel@tonic-gate kmem_free(pname, nlen); 6647c478bd9Sstevel@tonic-gate goto retry; 6657c478bd9Sstevel@tonic-gate } 6667c478bd9Sstevel@tonic-gate *tsip = *sip; 6677c478bd9Sstevel@tonic-gate tsip->si_pname = tpname; 6687c478bd9Sstevel@tonic-gate (void) strcpy(tsip->si_pname, sip->si_pname); 6697c478bd9Sstevel@tonic-gate } 6707c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 6717c478bd9Sstevel@tonic-gate 6727c478bd9Sstevel@tonic-gate if (sip) { 6737c478bd9Sstevel@tonic-gate error = ENOMEM; 6747c478bd9Sstevel@tonic-gate goto lout; 6757c478bd9Sstevel@tonic-gate } 6767c478bd9Sstevel@tonic-gate ust = (swapent_t *)((swaptbl_t *)sc_arg)->swt_ent; 6777c478bd9Sstevel@tonic-gate for (tsip = csip, cnt = 0; cnt < nswap; tsip++, ust++, cnt++) { 6787c478bd9Sstevel@tonic-gate if (copyin(ust, &st, sizeof (swapent_t)) != 0) { 6797c478bd9Sstevel@tonic-gate error = EFAULT; 6807c478bd9Sstevel@tonic-gate goto lout; 6817c478bd9Sstevel@tonic-gate } 6827c478bd9Sstevel@tonic-gate st.ste_flags = tsip->si_flags; 6837c478bd9Sstevel@tonic-gate st.ste_length = 6847c478bd9Sstevel@tonic-gate (tsip->si_eoff - tsip->si_soff) >> SCTRSHFT; 6857c478bd9Sstevel@tonic-gate st.ste_start = tsip->si_soff >> SCTRSHFT; 6867c478bd9Sstevel@tonic-gate st.ste_pages = tsip->si_npgs; 6877c478bd9Sstevel@tonic-gate st.ste_free = tsip->si_nfpgs; 6887c478bd9Sstevel@tonic-gate if (copyout(&st, ust, sizeof (swapent_t)) != 0) { 6897c478bd9Sstevel@tonic-gate error = EFAULT; 6907c478bd9Sstevel@tonic-gate goto lout; 6917c478bd9Sstevel@tonic-gate } 6927c478bd9Sstevel@tonic-gate if (!tsip->si_pnamelen) 6937c478bd9Sstevel@tonic-gate continue; 6947c478bd9Sstevel@tonic-gate if (copyout(tsip->si_pname, st.ste_path, 6957c478bd9Sstevel@tonic-gate tsip->si_pnamelen) != 0) { 6967c478bd9Sstevel@tonic-gate error = EFAULT; 6977c478bd9Sstevel@tonic-gate goto lout; 6987c478bd9Sstevel@tonic-gate } 6997c478bd9Sstevel@tonic-gate } 7007c478bd9Sstevel@tonic-gate *rv = nswap; 7017c478bd9Sstevel@tonic-gate lout: 7027c478bd9Sstevel@tonic-gate kmem_free(csip, tmp_nswapfiles * sizeof (struct swapinfo)); 7037c478bd9Sstevel@tonic-gate kmem_free(pname, nlen); 7047c478bd9Sstevel@tonic-gate return (error); 7057c478bd9Sstevel@tonic-gate 7067c478bd9Sstevel@tonic-gate case SC_ADD: 7077c478bd9Sstevel@tonic-gate case SC_REMOVE: 7087c478bd9Sstevel@tonic-gate break; 7097c478bd9Sstevel@tonic-gate default: 7107c478bd9Sstevel@tonic-gate return (EINVAL); 7117c478bd9Sstevel@tonic-gate } 7127c478bd9Sstevel@tonic-gate if ((error = secpolicy_swapctl(CRED())) != 0) 7137c478bd9Sstevel@tonic-gate return (error); 7147c478bd9Sstevel@tonic-gate 7157c478bd9Sstevel@tonic-gate if (copyin(sc_arg, &sr, sizeof (swapres_t))) 7167c478bd9Sstevel@tonic-gate return (EFAULT); 7177c478bd9Sstevel@tonic-gate 7187c478bd9Sstevel@tonic-gate /* Allocate the space to read in pathname */ 7197c478bd9Sstevel@tonic-gate if ((swapname = kmem_alloc(MAXPATHLEN, KM_NOSLEEP)) == NULL) 7207c478bd9Sstevel@tonic-gate return (ENOMEM); 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate error = copyinstr(sr.sr_name, swapname, MAXPATHLEN, 0); 7237c478bd9Sstevel@tonic-gate if (error) 7247c478bd9Sstevel@tonic-gate goto out; 7257c478bd9Sstevel@tonic-gate 7267c478bd9Sstevel@tonic-gate error = lookupname(swapname, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp); 7277c478bd9Sstevel@tonic-gate if (error) { 7287c478bd9Sstevel@tonic-gate if (sc_cmd == SC_ADD) 7297c478bd9Sstevel@tonic-gate goto out; 7307c478bd9Sstevel@tonic-gate /* see if we match by name */ 7317c478bd9Sstevel@tonic-gate vp = swapdel_byname(swapname, (size_t)sr.sr_start); 7327c478bd9Sstevel@tonic-gate if (vp == NULL) 7337c478bd9Sstevel@tonic-gate goto out; 7347c478bd9Sstevel@tonic-gate } 7357c478bd9Sstevel@tonic-gate 7367c478bd9Sstevel@tonic-gate if (vp->v_flag & (VNOMAP | VNOSWAP)) { 7377c478bd9Sstevel@tonic-gate VN_RELE(vp); 7387c478bd9Sstevel@tonic-gate error = ENOSYS; 7397c478bd9Sstevel@tonic-gate goto out; 7407c478bd9Sstevel@tonic-gate } 7417c478bd9Sstevel@tonic-gate switch (vp->v_type) { 7427c478bd9Sstevel@tonic-gate case VBLK: 7437c478bd9Sstevel@tonic-gate break; 7447c478bd9Sstevel@tonic-gate 7457c478bd9Sstevel@tonic-gate case VREG: 7467c478bd9Sstevel@tonic-gate if (vp->v_vfsp && vn_is_readonly(vp)) 7477c478bd9Sstevel@tonic-gate error = EROFS; 7487c478bd9Sstevel@tonic-gate else 749da6c28aaSamw error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL); 7507c478bd9Sstevel@tonic-gate break; 7517c478bd9Sstevel@tonic-gate 7527c478bd9Sstevel@tonic-gate case VDIR: 7537c478bd9Sstevel@tonic-gate error = EISDIR; 7547c478bd9Sstevel@tonic-gate break; 7557c478bd9Sstevel@tonic-gate default: 7567c478bd9Sstevel@tonic-gate error = ENOSYS; 7577c478bd9Sstevel@tonic-gate break; 7587c478bd9Sstevel@tonic-gate } 7597c478bd9Sstevel@tonic-gate if (error == 0) { 7607c478bd9Sstevel@tonic-gate if (sc_cmd == SC_REMOVE) 7617c478bd9Sstevel@tonic-gate error = swapdel(vp, sr.sr_start); 7627c478bd9Sstevel@tonic-gate else 7637c478bd9Sstevel@tonic-gate error = swapadd(vp, sr.sr_start, 7647c478bd9Sstevel@tonic-gate sr.sr_length, swapname); 7657c478bd9Sstevel@tonic-gate } 7667c478bd9Sstevel@tonic-gate VN_RELE(vp); 7677c478bd9Sstevel@tonic-gate out: 7687c478bd9Sstevel@tonic-gate kmem_free(swapname, MAXPATHLEN); 7697c478bd9Sstevel@tonic-gate return (error); 7707c478bd9Sstevel@tonic-gate } 7717c478bd9Sstevel@tonic-gate 7727c478bd9Sstevel@tonic-gate #if defined(_LP64) && defined(_SYSCALL32) 7737c478bd9Sstevel@tonic-gate 7747c478bd9Sstevel@tonic-gate int 7757c478bd9Sstevel@tonic-gate swapctl32(int sc_cmd, void *sc_arg, int *rv) 7767c478bd9Sstevel@tonic-gate { 7777c478bd9Sstevel@tonic-gate struct swapinfo *sip, *csip, *tsip; 7787c478bd9Sstevel@tonic-gate int error = 0; 7797c478bd9Sstevel@tonic-gate struct swapent32 st, *ust; 7807c478bd9Sstevel@tonic-gate struct swapres32 sr; 7817c478bd9Sstevel@tonic-gate struct vnode *vp; 7827c478bd9Sstevel@tonic-gate int cnt = 0; 7837c478bd9Sstevel@tonic-gate int tmp_nswapfiles; 7847c478bd9Sstevel@tonic-gate int nswap; 7857c478bd9Sstevel@tonic-gate int length, nlen; 7867c478bd9Sstevel@tonic-gate int gplen = 0, plen; 7877c478bd9Sstevel@tonic-gate char *swapname; 7887c478bd9Sstevel@tonic-gate char *pname; 7897c478bd9Sstevel@tonic-gate char *tpname; 7907c478bd9Sstevel@tonic-gate struct anoninfo32 ai; 7917c478bd9Sstevel@tonic-gate size_t s; 7927c478bd9Sstevel@tonic-gate spgcnt_t avail; 79300792c0bS int global = INGLOBALZONE(curproc); 79400792c0bS struct zone *zp = curproc->p_zone; 7957c478bd9Sstevel@tonic-gate 79600792c0bS /* 79700792c0bS * When running in a zone we want to hide the details of the swap 79800792c0bS * devices: we report there only being one swap device named "swap" 79900792c0bS * having a size equal to the sum of the sizes of all real swap devices 80000792c0bS * on the system. 80100792c0bS */ 8027c478bd9Sstevel@tonic-gate switch (sc_cmd) { 8037c478bd9Sstevel@tonic-gate case SC_GETNSWP: 80400792c0bS if (global) 8057c478bd9Sstevel@tonic-gate *rv = nswapfiles; 80600792c0bS else 80700792c0bS *rv = 1; 8087c478bd9Sstevel@tonic-gate return (0); 8097c478bd9Sstevel@tonic-gate 8107c478bd9Sstevel@tonic-gate case SC_AINFO: 8117c478bd9Sstevel@tonic-gate /* 8127c478bd9Sstevel@tonic-gate * Return anoninfo information with these changes: 8137c478bd9Sstevel@tonic-gate * ani_max = maximum amount of swap space 8147c478bd9Sstevel@tonic-gate * (including potentially available physical memory) 8157c478bd9Sstevel@tonic-gate * ani_free = amount of unallocated anonymous memory 8167c478bd9Sstevel@tonic-gate * (some of which might be reserved and including 8177c478bd9Sstevel@tonic-gate * potentially available physical memory) 8187c478bd9Sstevel@tonic-gate * ani_resv = amount of claimed (reserved) anonymous memory 8197c478bd9Sstevel@tonic-gate */ 8207c478bd9Sstevel@tonic-gate avail = MAX((spgcnt_t)(availrmem - swapfs_minfree), 0); 8217c478bd9Sstevel@tonic-gate s = (k_anoninfo.ani_max + k_anoninfo.ani_mem_resv) + avail; 8227c478bd9Sstevel@tonic-gate if (s > UINT32_MAX) 8237c478bd9Sstevel@tonic-gate return (EOVERFLOW); 8247c478bd9Sstevel@tonic-gate ai.ani_max = s; 8257c478bd9Sstevel@tonic-gate 826*b52a336eSPavel Tatashin /* Update ani_free */ 827*b52a336eSPavel Tatashin set_anoninfo(); 8287c478bd9Sstevel@tonic-gate s = k_anoninfo.ani_free + avail; 8297c478bd9Sstevel@tonic-gate if (s > UINT32_MAX) 8307c478bd9Sstevel@tonic-gate return (EOVERFLOW); 8317c478bd9Sstevel@tonic-gate ai.ani_free = s; 8327c478bd9Sstevel@tonic-gate 8337c478bd9Sstevel@tonic-gate s = k_anoninfo.ani_phys_resv + k_anoninfo.ani_mem_resv; 8347c478bd9Sstevel@tonic-gate if (s > UINT32_MAX) 8357c478bd9Sstevel@tonic-gate return (EOVERFLOW); 8367c478bd9Sstevel@tonic-gate ai.ani_resv = s; 8377c478bd9Sstevel@tonic-gate 83800792c0bS if (!global && zp->zone_max_swap_ctl != UINT64_MAX) { 83900792c0bS /* 84000792c0bS * We're in a non-global zone with a swap cap. We 84100792c0bS * always report the system-wide values for the global 84200792c0bS * zone, even though it too can have a swap cap. 84300792c0bS * See the comment for the SC_AINFO case in swapctl() 84400792c0bS * which explains the following logic. 84500792c0bS */ 84600792c0bS rctl_qty_t cap, used; 84700792c0bS pgcnt_t pgcap, sys_avail; 84800792c0bS 84900792c0bS mutex_enter(&zp->zone_mem_lock); 85000792c0bS cap = zp->zone_max_swap_ctl; 85100792c0bS used = zp->zone_max_swap; 85200792c0bS mutex_exit(&zp->zone_mem_lock); 85300792c0bS 85400792c0bS pgcap = MIN(btop(cap), ai.ani_max); 85500792c0bS ai.ani_free = pgcap - btop(used); 85600792c0bS 85700792c0bS /* Get the system-wide swap currently available. */ 85800792c0bS sys_avail = ai.ani_max - ai.ani_resv; 85900792c0bS if (sys_avail < ai.ani_free) 86000792c0bS ai.ani_resv = pgcap - sys_avail; 86100792c0bS else 86200792c0bS ai.ani_resv = btop(used); 86300792c0bS 86400792c0bS ai.ani_max = pgcap; 86500792c0bS } 86600792c0bS 8677c478bd9Sstevel@tonic-gate if (copyout(&ai, sc_arg, sizeof (ai)) != 0) 8687c478bd9Sstevel@tonic-gate return (EFAULT); 8697c478bd9Sstevel@tonic-gate return (0); 8707c478bd9Sstevel@tonic-gate 8717c478bd9Sstevel@tonic-gate case SC_LIST: 8727c478bd9Sstevel@tonic-gate if (copyin(sc_arg, &length, sizeof (int32_t)) != 0) 8737c478bd9Sstevel@tonic-gate return (EFAULT); 87400792c0bS if (!global) { 87500792c0bS struct swapent32 st; 87600792c0bS char *swappath = "swap"; 87700792c0bS 87800792c0bS if (length < 1) 87900792c0bS return (ENOMEM); 88000792c0bS ust = (swapent32_t *)((swaptbl32_t *)sc_arg)->swt_ent; 88100792c0bS if (copyin(ust, &st, sizeof (swapent32_t)) != 0) 88200792c0bS return (EFAULT); 88300792c0bS st.ste_start = PAGESIZE >> SCTRSHFT; 88400792c0bS st.ste_length = (off_t)0; 88500792c0bS st.ste_pages = 0; 88600792c0bS st.ste_free = 0; 88700792c0bS st.ste_flags = 0; 88800792c0bS 88900792c0bS mutex_enter(&swapinfo_lock); 89000792c0bS for (sip = swapinfo, nswap = 0; 89100792c0bS sip != NULL && nswap < nswapfiles; 89200792c0bS sip = sip->si_next, nswap++) { 89300792c0bS st.ste_length += 89400792c0bS (sip->si_eoff - sip->si_soff) >> SCTRSHFT; 89500792c0bS st.ste_pages += sip->si_npgs; 89600792c0bS st.ste_free += sip->si_nfpgs; 89700792c0bS } 89800792c0bS mutex_exit(&swapinfo_lock); 89900792c0bS 90000792c0bS if (zp->zone_max_swap_ctl != UINT64_MAX) { 90100792c0bS rctl_qty_t cap, used; 90200792c0bS 90300792c0bS mutex_enter(&zp->zone_mem_lock); 90400792c0bS cap = zp->zone_max_swap_ctl; 90500792c0bS used = zp->zone_max_swap; 90600792c0bS mutex_exit(&zp->zone_mem_lock); 90700792c0bS 90800792c0bS st.ste_length = MIN(cap, st.ste_length); 90900792c0bS st.ste_pages = MIN(btop(cap), st.ste_pages); 91000792c0bS st.ste_free = MIN(st.ste_pages - btop(used), 91100792c0bS st.ste_free); 91200792c0bS } 91300792c0bS 91400792c0bS if (copyout(&st, ust, sizeof (swapent32_t)) != 0 || 91500792c0bS copyout(swappath, (caddr_t)(uintptr_t)st.ste_path, 91600792c0bS strlen(swappath) + 1) != 0) { 91700792c0bS return (EFAULT); 91800792c0bS } 91900792c0bS *rv = 1; 92000792c0bS return (0); 92100792c0bS } 9227c478bd9Sstevel@tonic-gate beginning: 9237c478bd9Sstevel@tonic-gate tmp_nswapfiles = nswapfiles; 9247c478bd9Sstevel@tonic-gate /* Return an error if not enough space for the whole table. */ 9257c478bd9Sstevel@tonic-gate if (length < tmp_nswapfiles) 9267c478bd9Sstevel@tonic-gate return (ENOMEM); 9277c478bd9Sstevel@tonic-gate /* 9287c478bd9Sstevel@tonic-gate * Get memory to hold the swap entries and their names. We'll 9297c478bd9Sstevel@tonic-gate * copy the real entries into these and then copy these out. 9307c478bd9Sstevel@tonic-gate * Allocating the pathname memory is only a guess so we may 9317c478bd9Sstevel@tonic-gate * find that we need more and have to do it again. 9327c478bd9Sstevel@tonic-gate * All this is because we have to hold the anon lock while 9337c478bd9Sstevel@tonic-gate * traversing the swapinfo list, and we can't be doing copyouts 9347c478bd9Sstevel@tonic-gate * and/or kmem_alloc()s during this. 9357c478bd9Sstevel@tonic-gate */ 9367c478bd9Sstevel@tonic-gate csip = kmem_zalloc(tmp_nswapfiles * sizeof (*csip), KM_SLEEP); 9377c478bd9Sstevel@tonic-gate retry: 9387c478bd9Sstevel@tonic-gate nlen = tmp_nswapfiles * (gplen += 100); 9397c478bd9Sstevel@tonic-gate pname = kmem_zalloc(nlen, KM_SLEEP); 9407c478bd9Sstevel@tonic-gate 9417c478bd9Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 9427c478bd9Sstevel@tonic-gate 9437c478bd9Sstevel@tonic-gate if (tmp_nswapfiles != nswapfiles) { 9447c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 9457c478bd9Sstevel@tonic-gate kmem_free(pname, nlen); 9467c478bd9Sstevel@tonic-gate kmem_free(csip, tmp_nswapfiles * sizeof (*csip)); 9477c478bd9Sstevel@tonic-gate gplen = 0; 9487c478bd9Sstevel@tonic-gate goto beginning; 9497c478bd9Sstevel@tonic-gate } 9507c478bd9Sstevel@tonic-gate for (sip = swapinfo, tsip = csip, tpname = pname, nswap = 0; 9517c478bd9Sstevel@tonic-gate (sip != NULL) && (nswap < tmp_nswapfiles); 9527c478bd9Sstevel@tonic-gate sip = sip->si_next, tsip++, tpname += plen, nswap++) { 9537c478bd9Sstevel@tonic-gate plen = sip->si_pnamelen; 9547c478bd9Sstevel@tonic-gate if (tpname + plen - pname > nlen) { 9557c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 9567c478bd9Sstevel@tonic-gate kmem_free(pname, nlen); 9577c478bd9Sstevel@tonic-gate goto retry; 9587c478bd9Sstevel@tonic-gate } 9597c478bd9Sstevel@tonic-gate *tsip = *sip; 9607c478bd9Sstevel@tonic-gate tsip->si_pname = tpname; 9617c478bd9Sstevel@tonic-gate (void) strcpy(tsip->si_pname, sip->si_pname); 9627c478bd9Sstevel@tonic-gate } 9637c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 9647c478bd9Sstevel@tonic-gate 9657c478bd9Sstevel@tonic-gate if (sip != NULL) { 9667c478bd9Sstevel@tonic-gate error = ENOMEM; 9677c478bd9Sstevel@tonic-gate goto lout; 9687c478bd9Sstevel@tonic-gate } 9697c478bd9Sstevel@tonic-gate ust = (swapent32_t *)((swaptbl32_t *)sc_arg)->swt_ent; 9707c478bd9Sstevel@tonic-gate for (tsip = csip, cnt = 0; cnt < nswap; tsip++, ust++, cnt++) { 9717c478bd9Sstevel@tonic-gate if (copyin(ust, &st, sizeof (*ust)) != 0) { 9727c478bd9Sstevel@tonic-gate error = EFAULT; 9737c478bd9Sstevel@tonic-gate goto lout; 9747c478bd9Sstevel@tonic-gate } 9757c478bd9Sstevel@tonic-gate st.ste_flags = tsip->si_flags; 9767c478bd9Sstevel@tonic-gate st.ste_length = 9777c478bd9Sstevel@tonic-gate (tsip->si_eoff - tsip->si_soff) >> SCTRSHFT; 9787c478bd9Sstevel@tonic-gate st.ste_start = tsip->si_soff >> SCTRSHFT; 9797c478bd9Sstevel@tonic-gate st.ste_pages = tsip->si_npgs; 9807c478bd9Sstevel@tonic-gate st.ste_free = tsip->si_nfpgs; 9817c478bd9Sstevel@tonic-gate if (copyout(&st, ust, sizeof (st)) != 0) { 9827c478bd9Sstevel@tonic-gate error = EFAULT; 9837c478bd9Sstevel@tonic-gate goto lout; 9847c478bd9Sstevel@tonic-gate } 9857c478bd9Sstevel@tonic-gate if (!tsip->si_pnamelen) 9867c478bd9Sstevel@tonic-gate continue; 9877c478bd9Sstevel@tonic-gate if (copyout(tsip->si_pname, 9887c478bd9Sstevel@tonic-gate (caddr_t)(uintptr_t)st.ste_path, 9897c478bd9Sstevel@tonic-gate tsip->si_pnamelen) != 0) { 9907c478bd9Sstevel@tonic-gate error = EFAULT; 9917c478bd9Sstevel@tonic-gate goto lout; 9927c478bd9Sstevel@tonic-gate } 9937c478bd9Sstevel@tonic-gate } 9947c478bd9Sstevel@tonic-gate *rv = nswap; 9957c478bd9Sstevel@tonic-gate lout: 9967c478bd9Sstevel@tonic-gate kmem_free(csip, tmp_nswapfiles * sizeof (*csip)); 9977c478bd9Sstevel@tonic-gate kmem_free(pname, nlen); 9987c478bd9Sstevel@tonic-gate return (error); 9997c478bd9Sstevel@tonic-gate 10007c478bd9Sstevel@tonic-gate case SC_ADD: 10017c478bd9Sstevel@tonic-gate case SC_REMOVE: 10027c478bd9Sstevel@tonic-gate break; 10037c478bd9Sstevel@tonic-gate default: 10047c478bd9Sstevel@tonic-gate return (EINVAL); 10057c478bd9Sstevel@tonic-gate } 10067c478bd9Sstevel@tonic-gate if ((error = secpolicy_swapctl(CRED())) != 0) 10077c478bd9Sstevel@tonic-gate return (error); 10087c478bd9Sstevel@tonic-gate 10097c478bd9Sstevel@tonic-gate if (copyin(sc_arg, &sr, sizeof (sr))) 10107c478bd9Sstevel@tonic-gate return (EFAULT); 10117c478bd9Sstevel@tonic-gate 10127c478bd9Sstevel@tonic-gate /* Allocate the space to read in pathname */ 10137c478bd9Sstevel@tonic-gate if ((swapname = kmem_alloc(MAXPATHLEN, KM_NOSLEEP)) == NULL) 10147c478bd9Sstevel@tonic-gate return (ENOMEM); 10157c478bd9Sstevel@tonic-gate 10167c478bd9Sstevel@tonic-gate error = copyinstr((caddr_t)(uintptr_t)sr.sr_name, 10177c478bd9Sstevel@tonic-gate swapname, MAXPATHLEN, NULL); 10187c478bd9Sstevel@tonic-gate if (error) 10197c478bd9Sstevel@tonic-gate goto out; 10207c478bd9Sstevel@tonic-gate 10217c478bd9Sstevel@tonic-gate error = lookupname(swapname, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp); 10227c478bd9Sstevel@tonic-gate if (error) { 10237c478bd9Sstevel@tonic-gate if (sc_cmd == SC_ADD) 10247c478bd9Sstevel@tonic-gate goto out; 10257c478bd9Sstevel@tonic-gate /* see if we match by name */ 10267c478bd9Sstevel@tonic-gate vp = swapdel_byname(swapname, (uint_t)sr.sr_start); 10277c478bd9Sstevel@tonic-gate if (vp == NULL) 10287c478bd9Sstevel@tonic-gate goto out; 10297c478bd9Sstevel@tonic-gate } 10307c478bd9Sstevel@tonic-gate 10317c478bd9Sstevel@tonic-gate if (vp->v_flag & (VNOMAP | VNOSWAP)) { 10327c478bd9Sstevel@tonic-gate VN_RELE(vp); 10337c478bd9Sstevel@tonic-gate error = ENOSYS; 10347c478bd9Sstevel@tonic-gate goto out; 10357c478bd9Sstevel@tonic-gate } 10367c478bd9Sstevel@tonic-gate switch (vp->v_type) { 10377c478bd9Sstevel@tonic-gate case VBLK: 10387c478bd9Sstevel@tonic-gate break; 10397c478bd9Sstevel@tonic-gate 10407c478bd9Sstevel@tonic-gate case VREG: 10417c478bd9Sstevel@tonic-gate if (vp->v_vfsp && vn_is_readonly(vp)) 10427c478bd9Sstevel@tonic-gate error = EROFS; 10437c478bd9Sstevel@tonic-gate else 1044da6c28aaSamw error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL); 10457c478bd9Sstevel@tonic-gate break; 10467c478bd9Sstevel@tonic-gate 10477c478bd9Sstevel@tonic-gate case VDIR: 10487c478bd9Sstevel@tonic-gate error = EISDIR; 10497c478bd9Sstevel@tonic-gate break; 10507c478bd9Sstevel@tonic-gate default: 10517c478bd9Sstevel@tonic-gate error = ENOSYS; 10527c478bd9Sstevel@tonic-gate break; 10537c478bd9Sstevel@tonic-gate } 10547c478bd9Sstevel@tonic-gate if (error == 0) { 10557c478bd9Sstevel@tonic-gate if (sc_cmd == SC_REMOVE) 10567c478bd9Sstevel@tonic-gate error = swapdel(vp, sr.sr_start); 10577c478bd9Sstevel@tonic-gate else 10587c478bd9Sstevel@tonic-gate error = swapadd(vp, sr.sr_start, sr.sr_length, 10597c478bd9Sstevel@tonic-gate swapname); 10607c478bd9Sstevel@tonic-gate } 10617c478bd9Sstevel@tonic-gate VN_RELE(vp); 10627c478bd9Sstevel@tonic-gate out: 10637c478bd9Sstevel@tonic-gate kmem_free(swapname, MAXPATHLEN); 10647c478bd9Sstevel@tonic-gate return (error); 10657c478bd9Sstevel@tonic-gate } 10667c478bd9Sstevel@tonic-gate 10677c478bd9Sstevel@tonic-gate #endif /* _LP64 && _SYSCALL32 */ 10687c478bd9Sstevel@tonic-gate 10697c478bd9Sstevel@tonic-gate /* 10707c478bd9Sstevel@tonic-gate * Add a new swap file. 10717c478bd9Sstevel@tonic-gate */ 10727c478bd9Sstevel@tonic-gate int 10737c478bd9Sstevel@tonic-gate swapadd(struct vnode *vp, ulong_t lowblk, ulong_t nblks, char *swapname) 10747c478bd9Sstevel@tonic-gate { 10757c478bd9Sstevel@tonic-gate struct swapinfo **sipp, *nsip = NULL, *esip = NULL; 10767c478bd9Sstevel@tonic-gate struct vnode *cvp; 10777c478bd9Sstevel@tonic-gate struct vattr vattr; 10787c478bd9Sstevel@tonic-gate pgcnt_t pages; 10797c478bd9Sstevel@tonic-gate u_offset_t soff, eoff; 10807c478bd9Sstevel@tonic-gate int error; 10817c478bd9Sstevel@tonic-gate ssize_t i, start, end; 10827c478bd9Sstevel@tonic-gate ushort_t wasswap; 10837c478bd9Sstevel@tonic-gate ulong_t startblk; 10847c478bd9Sstevel@tonic-gate size_t returned_mem; 10857c478bd9Sstevel@tonic-gate 10867c478bd9Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: vp %p lowblk %ld nblks %ld swapname %s\n", 10877c478bd9Sstevel@tonic-gate vp, lowblk, nblks, swapname, 0); 10887c478bd9Sstevel@tonic-gate /* 10897c478bd9Sstevel@tonic-gate * Get the real vnode. (If vp is not a specnode it just returns vp, so 10907c478bd9Sstevel@tonic-gate * it does the right thing, but having this code know about specnodes 10917c478bd9Sstevel@tonic-gate * violates the spirit of having it be indepedent of vnode type.) 10927c478bd9Sstevel@tonic-gate */ 10937c478bd9Sstevel@tonic-gate cvp = common_specvp(vp); 10947c478bd9Sstevel@tonic-gate 10957c478bd9Sstevel@tonic-gate /* 10967c478bd9Sstevel@tonic-gate * Or in VISSWAP so file system has chance to deny swap-ons during open. 10977c478bd9Sstevel@tonic-gate */ 10987c478bd9Sstevel@tonic-gate mutex_enter(&cvp->v_lock); 10997c478bd9Sstevel@tonic-gate wasswap = cvp->v_flag & VISSWAP; 11007c478bd9Sstevel@tonic-gate cvp->v_flag |= VISSWAP; 11017c478bd9Sstevel@tonic-gate mutex_exit(&cvp->v_lock); 11027c478bd9Sstevel@tonic-gate 11037c478bd9Sstevel@tonic-gate mutex_enter(&swap_lock); 1104da6c28aaSamw if (error = VOP_OPEN(&cvp, FREAD|FWRITE, CRED(), NULL)) { 11057c478bd9Sstevel@tonic-gate mutex_exit(&swap_lock); 11067c478bd9Sstevel@tonic-gate /* restore state of v_flag */ 11077c478bd9Sstevel@tonic-gate if (!wasswap) { 11087c478bd9Sstevel@tonic-gate mutex_enter(&cvp->v_lock); 11097c478bd9Sstevel@tonic-gate cvp->v_flag &= ~VISSWAP; 11107c478bd9Sstevel@tonic-gate mutex_exit(&cvp->v_lock); 11117c478bd9Sstevel@tonic-gate } 11127c478bd9Sstevel@tonic-gate return (error); 11137c478bd9Sstevel@tonic-gate } 11147c478bd9Sstevel@tonic-gate mutex_exit(&swap_lock); 11157c478bd9Sstevel@tonic-gate 11167c478bd9Sstevel@tonic-gate /* 11177c478bd9Sstevel@tonic-gate * Get partition size. Return error if empty partition, 11187c478bd9Sstevel@tonic-gate * or if request does not fit within the partition. 11197c478bd9Sstevel@tonic-gate * If this is the first swap device, we can reduce 11207c478bd9Sstevel@tonic-gate * the size of the swap area to match what is 11217c478bd9Sstevel@tonic-gate * available. This can happen if the system was built 11227c478bd9Sstevel@tonic-gate * on a machine with a different size swap partition. 11237c478bd9Sstevel@tonic-gate */ 11247c478bd9Sstevel@tonic-gate vattr.va_mask = AT_SIZE; 1125da6c28aaSamw if (error = VOP_GETATTR(cvp, &vattr, ATTR_COMM, CRED(), NULL)) 11267c478bd9Sstevel@tonic-gate goto out; 11277c478bd9Sstevel@tonic-gate 11287c478bd9Sstevel@tonic-gate /* 11297c478bd9Sstevel@tonic-gate * Specfs returns a va_size of MAXOFFSET_T (UNKNOWN_SIZE) when the 11307c478bd9Sstevel@tonic-gate * size of the device can't be determined. 11317c478bd9Sstevel@tonic-gate */ 11327c478bd9Sstevel@tonic-gate if ((vattr.va_size == 0) || (vattr.va_size == MAXOFFSET_T)) { 11337c478bd9Sstevel@tonic-gate error = EINVAL; 11347c478bd9Sstevel@tonic-gate goto out; 11357c478bd9Sstevel@tonic-gate } 11367c478bd9Sstevel@tonic-gate 11377c478bd9Sstevel@tonic-gate #ifdef _ILP32 11387c478bd9Sstevel@tonic-gate /* 11397c478bd9Sstevel@tonic-gate * No support for large swap in 32-bit OS, if the size of the swap is 11407c478bd9Sstevel@tonic-gate * bigger than MAXOFF32_T then the size used by swapfs must be limited. 11417c478bd9Sstevel@tonic-gate * This limitation is imposed by the swap subsystem itself, a D_64BIT 11427c478bd9Sstevel@tonic-gate * driver as the target of swap operation should be able to field 11437c478bd9Sstevel@tonic-gate * the IO. 11447c478bd9Sstevel@tonic-gate */ 11457c478bd9Sstevel@tonic-gate if (vattr.va_size > MAXOFF32_T) { 11467c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, 11477c478bd9Sstevel@tonic-gate "!swap device %s truncated from 0x%llx to 0x%x bytes", 11487c478bd9Sstevel@tonic-gate swapname, vattr.va_size, MAXOFF32_T); 11497c478bd9Sstevel@tonic-gate vattr.va_size = MAXOFF32_T; 11507c478bd9Sstevel@tonic-gate } 11517c478bd9Sstevel@tonic-gate #endif /* _ILP32 */ 11527c478bd9Sstevel@tonic-gate 11537c478bd9Sstevel@tonic-gate /* Fail if file not writeable (try to set size to current size) */ 11547c478bd9Sstevel@tonic-gate vattr.va_mask = AT_SIZE; 11557c478bd9Sstevel@tonic-gate if (error = VOP_SETATTR(cvp, &vattr, 0, CRED(), NULL)) 11567c478bd9Sstevel@tonic-gate goto out; 11577c478bd9Sstevel@tonic-gate 11587c478bd9Sstevel@tonic-gate /* Fail if fs does not support VOP_PAGEIO */ 1159da6c28aaSamw error = VOP_PAGEIO(cvp, (page_t *)NULL, (u_offset_t)0, 0, 0, CRED(), 1160da6c28aaSamw NULL); 11617c478bd9Sstevel@tonic-gate 11627c478bd9Sstevel@tonic-gate if (error == ENOSYS) 11637c478bd9Sstevel@tonic-gate goto out; 11647c478bd9Sstevel@tonic-gate else 11657c478bd9Sstevel@tonic-gate error = 0; 11667c478bd9Sstevel@tonic-gate /* 11677c478bd9Sstevel@tonic-gate * If swapping on the root filesystem don't put swap blocks that 11687c478bd9Sstevel@tonic-gate * correspond to the miniroot filesystem on the swap free list. 11697c478bd9Sstevel@tonic-gate */ 11707c478bd9Sstevel@tonic-gate if (cvp == rootdir) 11717c478bd9Sstevel@tonic-gate startblk = roundup(MINIROOTSIZE<<SCTRSHFT, klustsize)>>SCTRSHFT; 11727c478bd9Sstevel@tonic-gate else /* Skip 1st page (disk label) */ 11737c478bd9Sstevel@tonic-gate startblk = (ulong_t)(lowblk ? lowblk : 1); 11747c478bd9Sstevel@tonic-gate 11757c478bd9Sstevel@tonic-gate soff = startblk << SCTRSHFT; 11767c478bd9Sstevel@tonic-gate if (soff >= vattr.va_size) { 11777c478bd9Sstevel@tonic-gate error = EINVAL; 11787c478bd9Sstevel@tonic-gate goto out; 11797c478bd9Sstevel@tonic-gate } 11807c478bd9Sstevel@tonic-gate 11817c478bd9Sstevel@tonic-gate /* 11827c478bd9Sstevel@tonic-gate * If user specified 0 blks, use the size of the device 11837c478bd9Sstevel@tonic-gate */ 11847c478bd9Sstevel@tonic-gate eoff = nblks ? soff + (nblks - (startblk - lowblk) << SCTRSHFT) : 11857c478bd9Sstevel@tonic-gate vattr.va_size; 11867c478bd9Sstevel@tonic-gate 11877c478bd9Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: va_size %ld soff %ld eoff %ld\n", 11887c478bd9Sstevel@tonic-gate vattr.va_size, soff, eoff, 0, 0); 11897c478bd9Sstevel@tonic-gate 11907c478bd9Sstevel@tonic-gate if (eoff > vattr.va_size) { 11917c478bd9Sstevel@tonic-gate error = EINVAL; 11927c478bd9Sstevel@tonic-gate goto out; 11937c478bd9Sstevel@tonic-gate } 11947c478bd9Sstevel@tonic-gate 11957c478bd9Sstevel@tonic-gate /* 11967c478bd9Sstevel@tonic-gate * The starting and ending offsets must be page aligned. 11977c478bd9Sstevel@tonic-gate * Round soff up to next page boundary, round eoff 11987c478bd9Sstevel@tonic-gate * down to previous page boundary. 11997c478bd9Sstevel@tonic-gate */ 12007c478bd9Sstevel@tonic-gate soff = ptob(btopr(soff)); 12017c478bd9Sstevel@tonic-gate eoff = ptob(btop(eoff)); 12027c478bd9Sstevel@tonic-gate if (soff >= eoff) { 12037c478bd9Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: soff %ld >= eoff %ld\n", 12047c478bd9Sstevel@tonic-gate soff, eoff, 0, 0, 0); 12057c478bd9Sstevel@tonic-gate error = EINVAL; 12067c478bd9Sstevel@tonic-gate goto out; 12077c478bd9Sstevel@tonic-gate } 12087c478bd9Sstevel@tonic-gate 12097c478bd9Sstevel@tonic-gate pages = btop(eoff - soff); 12107c478bd9Sstevel@tonic-gate 12117c478bd9Sstevel@tonic-gate /* Allocate and partially set up the new swapinfo */ 12127c478bd9Sstevel@tonic-gate nsip = kmem_zalloc(sizeof (struct swapinfo), KM_SLEEP); 12137c478bd9Sstevel@tonic-gate nsip->si_vp = cvp; 12147c478bd9Sstevel@tonic-gate 12157c478bd9Sstevel@tonic-gate nsip->si_soff = soff; 12167c478bd9Sstevel@tonic-gate nsip->si_eoff = eoff; 12177c478bd9Sstevel@tonic-gate nsip->si_hint = 0; 12187c478bd9Sstevel@tonic-gate nsip->si_checkcnt = nsip->si_alloccnt = 0; 12197c478bd9Sstevel@tonic-gate 12207c478bd9Sstevel@tonic-gate nsip->si_pnamelen = (int)strlen(swapname) + 1; 12217c478bd9Sstevel@tonic-gate nsip->si_pname = (char *)kmem_zalloc(nsip->si_pnamelen, KM_SLEEP); 12227c478bd9Sstevel@tonic-gate bcopy(swapname, nsip->si_pname, nsip->si_pnamelen - 1); 12237c478bd9Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: allocating swapinfo for %s, %ld pages\n", 12247c478bd9Sstevel@tonic-gate swapname, pages, 0, 0, 0); 12257c478bd9Sstevel@tonic-gate /* 12267c478bd9Sstevel@tonic-gate * Size of swapslots map in bytes 12277c478bd9Sstevel@tonic-gate */ 12287c478bd9Sstevel@tonic-gate nsip->si_mapsize = P2ROUNDUP(pages, NBBW) / NBBY; 12297c478bd9Sstevel@tonic-gate nsip->si_swapslots = kmem_zalloc(nsip->si_mapsize, KM_SLEEP); 12307c478bd9Sstevel@tonic-gate 12317c478bd9Sstevel@tonic-gate /* 12327c478bd9Sstevel@tonic-gate * Permanently set the bits that can't ever be allocated, 12337c478bd9Sstevel@tonic-gate * i.e. those from the ending offset to the round up slot for the 12347c478bd9Sstevel@tonic-gate * swapslots bit map. 12357c478bd9Sstevel@tonic-gate */ 12367c478bd9Sstevel@tonic-gate start = pages; 12377c478bd9Sstevel@tonic-gate end = P2ROUNDUP(pages, NBBW); 12387c478bd9Sstevel@tonic-gate for (i = start; i < end; i++) { 12397c478bd9Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: set bit for page %ld\n", i, 12407c478bd9Sstevel@tonic-gate 0, 0, 0, 0); 12417c478bd9Sstevel@tonic-gate SETBIT(nsip->si_swapslots, i); 12427c478bd9Sstevel@tonic-gate } 12437c478bd9Sstevel@tonic-gate nsip->si_npgs = nsip->si_nfpgs = pages; 12447c478bd9Sstevel@tonic-gate /* 12457c478bd9Sstevel@tonic-gate * Now check to see if we can add it. We wait til now to check because 12467c478bd9Sstevel@tonic-gate * we need the swapinfo_lock and we don't want sleep with it (e.g., 12477c478bd9Sstevel@tonic-gate * during kmem_alloc()) while we're setting up the swapinfo. 12487c478bd9Sstevel@tonic-gate */ 12497c478bd9Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 12507c478bd9Sstevel@tonic-gate for (sipp = &swapinfo; (esip = *sipp) != NULL; sipp = &esip->si_next) { 12517c478bd9Sstevel@tonic-gate if (esip->si_vp == cvp) { 12527c478bd9Sstevel@tonic-gate if (esip->si_soff == soff && esip->si_npgs == pages && 12537c478bd9Sstevel@tonic-gate (esip->si_flags & ST_DOINGDEL)) { 12547c478bd9Sstevel@tonic-gate /* 12557c478bd9Sstevel@tonic-gate * We are adding a device that we are in the 12567c478bd9Sstevel@tonic-gate * middle of deleting. Just clear the 12577c478bd9Sstevel@tonic-gate * ST_DOINGDEL flag to signal this and 12587c478bd9Sstevel@tonic-gate * the deletion routine will eventually notice 12597c478bd9Sstevel@tonic-gate * it and add it back. 12607c478bd9Sstevel@tonic-gate */ 12617c478bd9Sstevel@tonic-gate esip->si_flags &= ~ST_DOINGDEL; 12627c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 12637c478bd9Sstevel@tonic-gate goto out; 12647c478bd9Sstevel@tonic-gate } 12657c478bd9Sstevel@tonic-gate /* disallow overlapping swap files */ 12667c478bd9Sstevel@tonic-gate if ((soff < esip->si_eoff) && (eoff > esip->si_soff)) { 12677c478bd9Sstevel@tonic-gate error = EEXIST; 12687c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 12697c478bd9Sstevel@tonic-gate goto out; 12707c478bd9Sstevel@tonic-gate } 12717c478bd9Sstevel@tonic-gate } 12727c478bd9Sstevel@tonic-gate } 12737c478bd9Sstevel@tonic-gate 12747c478bd9Sstevel@tonic-gate nswapfiles++; 12757c478bd9Sstevel@tonic-gate 12767c478bd9Sstevel@tonic-gate /* 12777c478bd9Sstevel@tonic-gate * add new swap device to list and shift allocations to it 12787c478bd9Sstevel@tonic-gate * before updating the anoninfo counters 12797c478bd9Sstevel@tonic-gate */ 12807c478bd9Sstevel@tonic-gate *sipp = nsip; 12817c478bd9Sstevel@tonic-gate silast = nsip; 12827c478bd9Sstevel@tonic-gate 12837c478bd9Sstevel@tonic-gate /* 12847c478bd9Sstevel@tonic-gate * Update the total amount of reservable swap space 12857c478bd9Sstevel@tonic-gate * accounting properly for swap space from physical memory 12867c478bd9Sstevel@tonic-gate */ 12877c478bd9Sstevel@tonic-gate /* New swap device soaks up currently reserved memory swap */ 12887c478bd9Sstevel@tonic-gate mutex_enter(&anoninfo_lock); 12897c478bd9Sstevel@tonic-gate 12907c478bd9Sstevel@tonic-gate ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap); 12917c478bd9Sstevel@tonic-gate ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv); 12927c478bd9Sstevel@tonic-gate 12937c478bd9Sstevel@tonic-gate k_anoninfo.ani_max += pages; 12947c478bd9Sstevel@tonic-gate ANI_ADD(pages); 12957c478bd9Sstevel@tonic-gate if (k_anoninfo.ani_mem_resv > k_anoninfo.ani_locked_swap) { 12967c478bd9Sstevel@tonic-gate returned_mem = MIN(k_anoninfo.ani_mem_resv - 12977c478bd9Sstevel@tonic-gate k_anoninfo.ani_locked_swap, 12987c478bd9Sstevel@tonic-gate k_anoninfo.ani_max - k_anoninfo.ani_phys_resv); 12997c478bd9Sstevel@tonic-gate 13007c478bd9Sstevel@tonic-gate ANI_ADD(-returned_mem); 13017c478bd9Sstevel@tonic-gate k_anoninfo.ani_free -= returned_mem; 13027c478bd9Sstevel@tonic-gate k_anoninfo.ani_mem_resv -= returned_mem; 13037c478bd9Sstevel@tonic-gate k_anoninfo.ani_phys_resv += returned_mem; 13047c478bd9Sstevel@tonic-gate 13057c478bd9Sstevel@tonic-gate mutex_enter(&freemem_lock); 13067c478bd9Sstevel@tonic-gate availrmem += returned_mem; 13077c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 13087c478bd9Sstevel@tonic-gate } 13097c478bd9Sstevel@tonic-gate /* 13107c478bd9Sstevel@tonic-gate * At boot time, to permit booting small memory machines using 13117c478bd9Sstevel@tonic-gate * only physical memory as swap space, we allowed a dangerously 13127c478bd9Sstevel@tonic-gate * large amount of memory to be used as swap space; now that 13137c478bd9Sstevel@tonic-gate * more physical backing store is available bump down the amount 13147c478bd9Sstevel@tonic-gate * we can get from memory to a safer size. 13157c478bd9Sstevel@tonic-gate */ 13167c478bd9Sstevel@tonic-gate if (swapfs_minfree < swapfs_desfree) { 13177c478bd9Sstevel@tonic-gate mutex_enter(&freemem_lock); 13187c478bd9Sstevel@tonic-gate if (availrmem > swapfs_desfree || !k_anoninfo.ani_mem_resv) 13197c478bd9Sstevel@tonic-gate swapfs_minfree = swapfs_desfree; 13207c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 13217c478bd9Sstevel@tonic-gate } 13227c478bd9Sstevel@tonic-gate 13237c478bd9Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: ani_max %ld ani_free %ld\n", 13247c478bd9Sstevel@tonic-gate k_anoninfo.ani_free, k_anoninfo.ani_free, 0, 0, 0); 13257c478bd9Sstevel@tonic-gate 13267c478bd9Sstevel@tonic-gate mutex_exit(&anoninfo_lock); 13277c478bd9Sstevel@tonic-gate 13287c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 13297c478bd9Sstevel@tonic-gate 13307c478bd9Sstevel@tonic-gate /* Initialize the dump device */ 13317c478bd9Sstevel@tonic-gate mutex_enter(&dump_lock); 13327c478bd9Sstevel@tonic-gate if (dumpvp == NULL) 13337c478bd9Sstevel@tonic-gate (void) dumpinit(vp, swapname, 0); 13347c478bd9Sstevel@tonic-gate mutex_exit(&dump_lock); 13357c478bd9Sstevel@tonic-gate 13367c478bd9Sstevel@tonic-gate VN_HOLD(cvp); 13377c478bd9Sstevel@tonic-gate out: 13387c478bd9Sstevel@tonic-gate if (error || esip) { 13397c478bd9Sstevel@tonic-gate SWAP_PRINT(SW_CTL, "swapadd: error (%d)\n", error, 0, 0, 0, 0); 13407c478bd9Sstevel@tonic-gate 13417c478bd9Sstevel@tonic-gate if (!wasswap) { 13427c478bd9Sstevel@tonic-gate mutex_enter(&cvp->v_lock); 13437c478bd9Sstevel@tonic-gate cvp->v_flag &= ~VISSWAP; 13447c478bd9Sstevel@tonic-gate mutex_exit(&cvp->v_lock); 13457c478bd9Sstevel@tonic-gate } 13467c478bd9Sstevel@tonic-gate if (nsip) { 13477c478bd9Sstevel@tonic-gate kmem_free(nsip->si_swapslots, (size_t)nsip->si_mapsize); 13487c478bd9Sstevel@tonic-gate kmem_free(nsip->si_pname, nsip->si_pnamelen); 13497c478bd9Sstevel@tonic-gate kmem_free(nsip, sizeof (*nsip)); 13507c478bd9Sstevel@tonic-gate } 13517c478bd9Sstevel@tonic-gate mutex_enter(&swap_lock); 1352da6c28aaSamw (void) VOP_CLOSE(cvp, FREAD|FWRITE, 1, (offset_t)0, CRED(), 1353da6c28aaSamw NULL); 13547c478bd9Sstevel@tonic-gate mutex_exit(&swap_lock); 13557c478bd9Sstevel@tonic-gate } 13567c478bd9Sstevel@tonic-gate return (error); 13577c478bd9Sstevel@tonic-gate } 13587c478bd9Sstevel@tonic-gate 13597c478bd9Sstevel@tonic-gate /* 13607c478bd9Sstevel@tonic-gate * Delete a swap file. 13617c478bd9Sstevel@tonic-gate */ 13627c478bd9Sstevel@tonic-gate static int 13637c478bd9Sstevel@tonic-gate swapdel( 13647c478bd9Sstevel@tonic-gate struct vnode *vp, 13657c478bd9Sstevel@tonic-gate ulong_t lowblk) /* Low block number of area to delete. */ 13667c478bd9Sstevel@tonic-gate { 13677c478bd9Sstevel@tonic-gate struct swapinfo **sipp, *osip = NULL; 13687c478bd9Sstevel@tonic-gate struct vnode *cvp; 13697c478bd9Sstevel@tonic-gate u_offset_t soff; 13707c478bd9Sstevel@tonic-gate int error = 0; 13717c478bd9Sstevel@tonic-gate u_offset_t toff = 0; 13727c478bd9Sstevel@tonic-gate struct vnode *tvp = NULL; 13737c478bd9Sstevel@tonic-gate spgcnt_t pages; 13747c478bd9Sstevel@tonic-gate struct anon **app, *ap; 13757c478bd9Sstevel@tonic-gate kmutex_t *ahm; 13767c478bd9Sstevel@tonic-gate pgcnt_t adjust_swap = 0; 13777c478bd9Sstevel@tonic-gate 13787c478bd9Sstevel@tonic-gate /* Find the swap file entry for the file to be deleted */ 13797c478bd9Sstevel@tonic-gate cvp = common_specvp(vp); 13807c478bd9Sstevel@tonic-gate 13817c478bd9Sstevel@tonic-gate 13827c478bd9Sstevel@tonic-gate lowblk = lowblk ? lowblk : 1; /* Skip first page (disk label) */ 13837c478bd9Sstevel@tonic-gate soff = ptob(btopr(lowblk << SCTRSHFT)); /* must be page aligned */ 13847c478bd9Sstevel@tonic-gate 13857c478bd9Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 13867c478bd9Sstevel@tonic-gate for (sipp = &swapinfo; (osip = *sipp) != NULL; sipp = &osip->si_next) { 13877c478bd9Sstevel@tonic-gate if ((osip->si_vp == cvp) && 13887c478bd9Sstevel@tonic-gate (osip->si_soff == soff) && (osip->si_flags == 0)) 13897c478bd9Sstevel@tonic-gate break; 13907c478bd9Sstevel@tonic-gate } 13917c478bd9Sstevel@tonic-gate 13927c478bd9Sstevel@tonic-gate /* If the file was not found, error. */ 13937c478bd9Sstevel@tonic-gate if (osip == NULL) { 13947c478bd9Sstevel@tonic-gate error = EINVAL; 13957c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 13967c478bd9Sstevel@tonic-gate goto out; 13977c478bd9Sstevel@tonic-gate } 13987c478bd9Sstevel@tonic-gate 13997c478bd9Sstevel@tonic-gate pages = osip->si_npgs; 14007c478bd9Sstevel@tonic-gate 14017c478bd9Sstevel@tonic-gate /* 14027c478bd9Sstevel@tonic-gate * Do not delete if we will be low on swap pages. 14037c478bd9Sstevel@tonic-gate */ 14047c478bd9Sstevel@tonic-gate mutex_enter(&anoninfo_lock); 14057c478bd9Sstevel@tonic-gate 14067c478bd9Sstevel@tonic-gate ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap); 14077c478bd9Sstevel@tonic-gate ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv); 14087c478bd9Sstevel@tonic-gate 14097c478bd9Sstevel@tonic-gate mutex_enter(&freemem_lock); 14107c478bd9Sstevel@tonic-gate if (((k_anoninfo.ani_max - k_anoninfo.ani_phys_resv) + 14117c478bd9Sstevel@tonic-gate MAX((spgcnt_t)(availrmem - swapfs_minfree), 0)) < pages) { 14127c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 14137c478bd9Sstevel@tonic-gate mutex_exit(&anoninfo_lock); 14147c478bd9Sstevel@tonic-gate error = ENOMEM; 14157c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "swapdel - too few free pages"); 14167c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 14177c478bd9Sstevel@tonic-gate goto out; 14187c478bd9Sstevel@tonic-gate } 14197c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 14207c478bd9Sstevel@tonic-gate 14217c478bd9Sstevel@tonic-gate k_anoninfo.ani_max -= pages; 14227c478bd9Sstevel@tonic-gate 14237c478bd9Sstevel@tonic-gate /* If needed, reserve memory swap to replace old device */ 14247c478bd9Sstevel@tonic-gate if (k_anoninfo.ani_phys_resv > k_anoninfo.ani_max) { 14257c478bd9Sstevel@tonic-gate adjust_swap = k_anoninfo.ani_phys_resv - k_anoninfo.ani_max; 14267c478bd9Sstevel@tonic-gate k_anoninfo.ani_phys_resv -= adjust_swap; 14277c478bd9Sstevel@tonic-gate k_anoninfo.ani_mem_resv += adjust_swap; 14287c478bd9Sstevel@tonic-gate mutex_enter(&freemem_lock); 14297c478bd9Sstevel@tonic-gate availrmem -= adjust_swap; 14307c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 14317c478bd9Sstevel@tonic-gate ANI_ADD(adjust_swap); 14327c478bd9Sstevel@tonic-gate } 14337c478bd9Sstevel@tonic-gate ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap); 14347c478bd9Sstevel@tonic-gate ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv); 14357c478bd9Sstevel@tonic-gate mutex_exit(&anoninfo_lock); 14367c478bd9Sstevel@tonic-gate 14377c478bd9Sstevel@tonic-gate ANI_ADD(-pages); 14387c478bd9Sstevel@tonic-gate 14397c478bd9Sstevel@tonic-gate /* 14407c478bd9Sstevel@tonic-gate * Set the delete flag. This prevents anyone from allocating more 14417c478bd9Sstevel@tonic-gate * pages from this file. Also set ST_DOINGDEL. Someone who wants to 14427c478bd9Sstevel@tonic-gate * add the file back while we're deleting it will signify by clearing 14437c478bd9Sstevel@tonic-gate * this flag. 14447c478bd9Sstevel@tonic-gate */ 14457c478bd9Sstevel@tonic-gate osip->si_flags |= ST_INDEL|ST_DOINGDEL; 14467c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 14477c478bd9Sstevel@tonic-gate 14487c478bd9Sstevel@tonic-gate /* 14497c478bd9Sstevel@tonic-gate * Free all the allocated physical slots for this file. We do this 14507c478bd9Sstevel@tonic-gate * by walking through the entire anon hash array, because we need 14517c478bd9Sstevel@tonic-gate * to update all the anon slots that have physical swap slots on 14527c478bd9Sstevel@tonic-gate * this file, and this is the only way to find them all. We go back 14537c478bd9Sstevel@tonic-gate * to the beginning of a bucket after each slot is freed because the 14547c478bd9Sstevel@tonic-gate * anonhash_lock is not held during the free and thus the hash table 14557c478bd9Sstevel@tonic-gate * may change under us. 14567c478bd9Sstevel@tonic-gate */ 14577c478bd9Sstevel@tonic-gate for (app = anon_hash; app < &anon_hash[ANON_HASH_SIZE]; app++) { 145823d9e5acSMichael Corcoran ahm = &anonhash_lock[(app - anon_hash) & 145923d9e5acSMichael Corcoran (AH_LOCK_SIZE - 1)].pad_mutex; 14607c478bd9Sstevel@tonic-gate mutex_enter(ahm); 14617c478bd9Sstevel@tonic-gate top: 14627c478bd9Sstevel@tonic-gate for (ap = *app; ap != NULL; ap = ap->an_hash) { 14637c478bd9Sstevel@tonic-gate if (ap->an_pvp == cvp && 14647c478bd9Sstevel@tonic-gate ap->an_poff >= osip->si_soff && 14657c478bd9Sstevel@tonic-gate ap->an_poff < osip->si_eoff) { 14667c478bd9Sstevel@tonic-gate ASSERT(TESTBIT(osip->si_swapslots, 14677c478bd9Sstevel@tonic-gate btop((size_t)(ap->an_poff - 14687c478bd9Sstevel@tonic-gate osip->si_soff)))); 14697c478bd9Sstevel@tonic-gate tvp = ap->an_vp; 14707c478bd9Sstevel@tonic-gate toff = ap->an_off; 14717c478bd9Sstevel@tonic-gate VN_HOLD(tvp); 14727c478bd9Sstevel@tonic-gate mutex_exit(ahm); 14737c478bd9Sstevel@tonic-gate 14747c478bd9Sstevel@tonic-gate error = swapslot_free(tvp, toff, osip); 14757c478bd9Sstevel@tonic-gate 14767c478bd9Sstevel@tonic-gate VN_RELE(tvp); 14777c478bd9Sstevel@tonic-gate mutex_enter(ahm); 14787c478bd9Sstevel@tonic-gate if (!error && (osip->si_flags & ST_DOINGDEL)) { 14797c478bd9Sstevel@tonic-gate goto top; 14807c478bd9Sstevel@tonic-gate } else { 14817c478bd9Sstevel@tonic-gate if (error) { 14827c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 14837c478bd9Sstevel@tonic-gate "swapslot_free failed %d", 14847c478bd9Sstevel@tonic-gate error); 14857c478bd9Sstevel@tonic-gate } 14867c478bd9Sstevel@tonic-gate 14877c478bd9Sstevel@tonic-gate /* 14887c478bd9Sstevel@tonic-gate * Add device back before making it 14897c478bd9Sstevel@tonic-gate * visible. 14907c478bd9Sstevel@tonic-gate */ 14917c478bd9Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 14927c478bd9Sstevel@tonic-gate osip->si_flags &= 14937c478bd9Sstevel@tonic-gate ~(ST_INDEL | ST_DOINGDEL); 14947c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 14957c478bd9Sstevel@tonic-gate 14967c478bd9Sstevel@tonic-gate /* 14977c478bd9Sstevel@tonic-gate * Update the anon space available 14987c478bd9Sstevel@tonic-gate */ 14997c478bd9Sstevel@tonic-gate mutex_enter(&anoninfo_lock); 15007c478bd9Sstevel@tonic-gate 15017c478bd9Sstevel@tonic-gate k_anoninfo.ani_phys_resv += adjust_swap; 15027c478bd9Sstevel@tonic-gate k_anoninfo.ani_mem_resv -= adjust_swap; 15037c478bd9Sstevel@tonic-gate k_anoninfo.ani_max += pages; 15047c478bd9Sstevel@tonic-gate 15057c478bd9Sstevel@tonic-gate mutex_enter(&freemem_lock); 15067c478bd9Sstevel@tonic-gate availrmem += adjust_swap; 15077c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 15087c478bd9Sstevel@tonic-gate 15097c478bd9Sstevel@tonic-gate mutex_exit(&anoninfo_lock); 15107c478bd9Sstevel@tonic-gate 15117c478bd9Sstevel@tonic-gate ANI_ADD(pages); 15127c478bd9Sstevel@tonic-gate 15137c478bd9Sstevel@tonic-gate mutex_exit(ahm); 15147c478bd9Sstevel@tonic-gate goto out; 15157c478bd9Sstevel@tonic-gate } 15167c478bd9Sstevel@tonic-gate } 15177c478bd9Sstevel@tonic-gate } 15187c478bd9Sstevel@tonic-gate mutex_exit(ahm); 15197c478bd9Sstevel@tonic-gate } 15207c478bd9Sstevel@tonic-gate 15217c478bd9Sstevel@tonic-gate /* All done, they'd better all be free! */ 15227c478bd9Sstevel@tonic-gate mutex_enter(&swapinfo_lock); 15237c478bd9Sstevel@tonic-gate ASSERT(osip->si_nfpgs == osip->si_npgs); 15247c478bd9Sstevel@tonic-gate 15257c478bd9Sstevel@tonic-gate /* Now remove it from the swapinfo list */ 15267c478bd9Sstevel@tonic-gate for (sipp = &swapinfo; *sipp != NULL; sipp = &(*sipp)->si_next) { 15277c478bd9Sstevel@tonic-gate if (*sipp == osip) 15287c478bd9Sstevel@tonic-gate break; 15297c478bd9Sstevel@tonic-gate } 15307c478bd9Sstevel@tonic-gate ASSERT(*sipp); 15317c478bd9Sstevel@tonic-gate *sipp = osip->si_next; 15327c478bd9Sstevel@tonic-gate if (silast == osip) 15337c478bd9Sstevel@tonic-gate if ((silast = osip->si_next) == NULL) 15347c478bd9Sstevel@tonic-gate silast = swapinfo; 15357c478bd9Sstevel@tonic-gate nswapfiles--; 15367c478bd9Sstevel@tonic-gate mutex_exit(&swapinfo_lock); 15377c478bd9Sstevel@tonic-gate 15387c478bd9Sstevel@tonic-gate kmem_free(osip->si_swapslots, osip->si_mapsize); 15397c478bd9Sstevel@tonic-gate kmem_free(osip->si_pname, osip->si_pnamelen); 15407c478bd9Sstevel@tonic-gate kmem_free(osip, sizeof (*osip)); 15417c478bd9Sstevel@tonic-gate 15427c478bd9Sstevel@tonic-gate mutex_enter(&dump_lock); 15437c478bd9Sstevel@tonic-gate if (cvp == dumpvp) 15447c478bd9Sstevel@tonic-gate dumpfini(); 15457c478bd9Sstevel@tonic-gate mutex_exit(&dump_lock); 15467c478bd9Sstevel@tonic-gate 15477c478bd9Sstevel@tonic-gate /* Release the vnode */ 15487c478bd9Sstevel@tonic-gate 15497c478bd9Sstevel@tonic-gate mutex_enter(&swap_lock); 1550da6c28aaSamw (void) VOP_CLOSE(cvp, FREAD|FWRITE, 1, (offset_t)0, CRED(), NULL); 15517c478bd9Sstevel@tonic-gate mutex_enter(&cvp->v_lock); 15527c478bd9Sstevel@tonic-gate cvp->v_flag &= ~VISSWAP; 15537c478bd9Sstevel@tonic-gate mutex_exit(&cvp->v_lock); 15547c478bd9Sstevel@tonic-gate VN_RELE(cvp); 15557c478bd9Sstevel@tonic-gate mutex_exit(&swap_lock); 15567c478bd9Sstevel@tonic-gate out: 15577c478bd9Sstevel@tonic-gate return (error); 15587c478bd9Sstevel@tonic-gate } 15597c478bd9Sstevel@tonic-gate 15607c478bd9Sstevel@tonic-gate /* 15617c478bd9Sstevel@tonic-gate * Free up a physical swap slot on swapinfo sip, currently in use by the 15627c478bd9Sstevel@tonic-gate * anonymous page whose name is (vp, off). 15637c478bd9Sstevel@tonic-gate */ 15647c478bd9Sstevel@tonic-gate static int 15657c478bd9Sstevel@tonic-gate swapslot_free( 15667c478bd9Sstevel@tonic-gate struct vnode *vp, 15677c478bd9Sstevel@tonic-gate u_offset_t off, 15687c478bd9Sstevel@tonic-gate struct swapinfo *sip) 15697c478bd9Sstevel@tonic-gate { 157020a2d3f6Sstans struct page *pp = NULL; 15717c478bd9Sstevel@tonic-gate struct anon *ap = NULL; 15727c478bd9Sstevel@tonic-gate int error = 0; 15737c478bd9Sstevel@tonic-gate kmutex_t *ahm; 157420a2d3f6Sstans struct vnode *pvp = NULL; 157520a2d3f6Sstans u_offset_t poff; 157620a2d3f6Sstans int alloc_pg = 0; 15777c478bd9Sstevel@tonic-gate 157820a2d3f6Sstans ASSERT(sip->si_vp != NULL); 15797c478bd9Sstevel@tonic-gate /* 158020a2d3f6Sstans * Get the page for the old swap slot if exists or create a new one. 15817c478bd9Sstevel@tonic-gate */ 158220a2d3f6Sstans again: 158320a2d3f6Sstans if ((pp = page_lookup(vp, off, SE_SHARED)) == NULL) { 158420a2d3f6Sstans pp = page_create_va(vp, off, PAGESIZE, PG_WAIT | PG_EXCL, 158520a2d3f6Sstans segkmap, NULL); 158620a2d3f6Sstans if (pp == NULL) 158720a2d3f6Sstans goto again; 158820a2d3f6Sstans alloc_pg = 1; 158920a2d3f6Sstans 159020a2d3f6Sstans error = swap_getphysname(vp, off, &pvp, &poff); 159120a2d3f6Sstans if (error || pvp != sip->si_vp || poff < sip->si_soff || 159220a2d3f6Sstans poff >= sip->si_eoff) { 159320a2d3f6Sstans page_io_unlock(pp); 159420a2d3f6Sstans /*LINTED: constant in conditional context*/ 159520a2d3f6Sstans VN_DISPOSE(pp, B_INVAL, 0, kcred); 159620a2d3f6Sstans return (0); 159720a2d3f6Sstans } 159820a2d3f6Sstans 159920a2d3f6Sstans error = VOP_PAGEIO(pvp, pp, poff, PAGESIZE, B_READ, 160020a2d3f6Sstans CRED(), NULL); 160120a2d3f6Sstans if (error) { 160220a2d3f6Sstans page_io_unlock(pp); 160320a2d3f6Sstans if (error == EFAULT) 16047c478bd9Sstevel@tonic-gate error = 0; 160520a2d3f6Sstans /*LINTED: constant in conditional context*/ 160620a2d3f6Sstans VN_DISPOSE(pp, B_INVAL, 0, kcred); 16077c478bd9Sstevel@tonic-gate return (error); 16087c478bd9Sstevel@tonic-gate } 160920a2d3f6Sstans } 16107c478bd9Sstevel@tonic-gate 161120a2d3f6Sstans /* 161220a2d3f6Sstans * The anon could have been removed by anon_decref* and/or reallocated 161320a2d3f6Sstans * by anon layer (an_pvp == NULL) with the same vp, off. 161420a2d3f6Sstans * In this case the page which has been allocated needs to 161520a2d3f6Sstans * be freed. 161620a2d3f6Sstans */ 161720a2d3f6Sstans if (!alloc_pg) 161820a2d3f6Sstans page_io_lock(pp); 161923d9e5acSMichael Corcoran ahm = AH_MUTEX(vp, off); 16207c478bd9Sstevel@tonic-gate mutex_enter(ahm); 162120a2d3f6Sstans ap = swap_anon(vp, off); 162220a2d3f6Sstans if ((ap == NULL || ap->an_pvp == NULL) && alloc_pg) { 162320a2d3f6Sstans mutex_exit(ahm); 162420a2d3f6Sstans page_io_unlock(pp); 162520a2d3f6Sstans /*LINTED: constant in conditional context*/ 162620a2d3f6Sstans VN_DISPOSE(pp, B_INVAL, 0, kcred); 162720a2d3f6Sstans return (0); 16287c478bd9Sstevel@tonic-gate } 162920a2d3f6Sstans 16307c478bd9Sstevel@tonic-gate /* 16317c478bd9Sstevel@tonic-gate * Free the physical slot. It may have been freed up and replaced with 16327c478bd9Sstevel@tonic-gate * another one while we were getting the page so we have to re-verify 16337c478bd9Sstevel@tonic-gate * that this is really one we want. If we do free the slot we have 16347c478bd9Sstevel@tonic-gate * to mark the page modified, as its backing store is now gone. 16357c478bd9Sstevel@tonic-gate */ 163620a2d3f6Sstans if ((ap != NULL) && (ap->an_pvp == sip->si_vp && ap->an_poff >= 163720a2d3f6Sstans sip->si_soff && ap->an_poff < sip->si_eoff)) { 16387c478bd9Sstevel@tonic-gate swap_phys_free(ap->an_pvp, ap->an_poff, PAGESIZE); 16397c478bd9Sstevel@tonic-gate ap->an_pvp = NULL; 164020a2d3f6Sstans ap->an_poff = 0; 16417c478bd9Sstevel@tonic-gate mutex_exit(ahm); 16427c478bd9Sstevel@tonic-gate hat_setmod(pp); 16437c478bd9Sstevel@tonic-gate } else { 16447c478bd9Sstevel@tonic-gate mutex_exit(ahm); 16457c478bd9Sstevel@tonic-gate } 16467c478bd9Sstevel@tonic-gate page_io_unlock(pp); 164720a2d3f6Sstans page_unlock(pp); 164820a2d3f6Sstans return (0); 16497c478bd9Sstevel@tonic-gate } 16507c478bd9Sstevel@tonic-gate 165120a2d3f6Sstans 16527c478bd9Sstevel@tonic-gate /* 16537c478bd9Sstevel@tonic-gate * Get contig physical backing store for vp, in the range 16547c478bd9Sstevel@tonic-gate * [*offp, *offp + *lenp), May back a subrange of this, but must 16557c478bd9Sstevel@tonic-gate * always include the requested offset or fail. Returns the offsets 16567c478bd9Sstevel@tonic-gate * backed as [*offp, *offp + *lenp) and the physical offsets used to 16577c478bd9Sstevel@tonic-gate * back them from *pvpp in the range [*pstartp, *pstartp + *lenp). 16587c478bd9Sstevel@tonic-gate * Returns 0 for success 16597c478bd9Sstevel@tonic-gate * SE_NOANON -- no anon slot for requested paged 16607c478bd9Sstevel@tonic-gate * SE_NOSWAP -- no physical swap space available 16617c478bd9Sstevel@tonic-gate */ 16627c478bd9Sstevel@tonic-gate int 16637c478bd9Sstevel@tonic-gate swap_newphysname( 16647c478bd9Sstevel@tonic-gate struct vnode *vp, 16657c478bd9Sstevel@tonic-gate u_offset_t offset, 16667c478bd9Sstevel@tonic-gate u_offset_t *offp, 16677c478bd9Sstevel@tonic-gate size_t *lenp, 16687c478bd9Sstevel@tonic-gate struct vnode **pvpp, 16697c478bd9Sstevel@tonic-gate u_offset_t *poffp) 16707c478bd9Sstevel@tonic-gate { 16717c478bd9Sstevel@tonic-gate struct anon *ap = NULL; /* anon slot for vp, off */ 16727c478bd9Sstevel@tonic-gate int error = 0; 16737c478bd9Sstevel@tonic-gate struct vnode *pvp; 16747c478bd9Sstevel@tonic-gate u_offset_t poff, pstart, prem; 16757c478bd9Sstevel@tonic-gate size_t plen; 16767c478bd9Sstevel@tonic-gate u_offset_t off, start; 16777c478bd9Sstevel@tonic-gate kmutex_t *ahm; 16787c478bd9Sstevel@tonic-gate 16797c478bd9Sstevel@tonic-gate ASSERT(*offp <= offset && offset < *offp + *lenp); 16807c478bd9Sstevel@tonic-gate 16817c478bd9Sstevel@tonic-gate /* Get new physical swap slots. */ 16827c478bd9Sstevel@tonic-gate plen = *lenp; 16837c478bd9Sstevel@tonic-gate if (!swap_phys_alloc(&pvp, &pstart, &plen, 0)) { 16847c478bd9Sstevel@tonic-gate /* 16857c478bd9Sstevel@tonic-gate * No swap available so return error unless requested 16867c478bd9Sstevel@tonic-gate * offset is already backed in which case return that. 16877c478bd9Sstevel@tonic-gate */ 168823d9e5acSMichael Corcoran ahm = AH_MUTEX(vp, offset); 16897c478bd9Sstevel@tonic-gate mutex_enter(ahm); 16907c478bd9Sstevel@tonic-gate if ((ap = swap_anon(vp, offset)) == NULL) { 16917c478bd9Sstevel@tonic-gate error = SE_NOANON; 16927c478bd9Sstevel@tonic-gate mutex_exit(ahm); 16937c478bd9Sstevel@tonic-gate return (error); 16947c478bd9Sstevel@tonic-gate } 16957c478bd9Sstevel@tonic-gate error = (ap->an_pvp ? 0 : SE_NOSWAP); 16967c478bd9Sstevel@tonic-gate *offp = offset; 16977c478bd9Sstevel@tonic-gate *lenp = PAGESIZE; 16987c478bd9Sstevel@tonic-gate *pvpp = ap->an_pvp; 16997c478bd9Sstevel@tonic-gate *poffp = ap->an_poff; 17007c478bd9Sstevel@tonic-gate mutex_exit(ahm); 17017c478bd9Sstevel@tonic-gate return (error); 17027c478bd9Sstevel@tonic-gate } 17037c478bd9Sstevel@tonic-gate 17047c478bd9Sstevel@tonic-gate /* 17057c478bd9Sstevel@tonic-gate * We got plen (<= *lenp) contig slots. Use these to back a 17067c478bd9Sstevel@tonic-gate * subrange of [*offp, *offp + *lenp) which includes offset. 17077c478bd9Sstevel@tonic-gate * For now we just put offset at the end of the kluster. 17087c478bd9Sstevel@tonic-gate * Clearly there are other possible choices - which is best? 17097c478bd9Sstevel@tonic-gate */ 17107c478bd9Sstevel@tonic-gate start = MAX(*offp, 17117c478bd9Sstevel@tonic-gate (offset + PAGESIZE > plen) ? (offset + PAGESIZE - plen) : 0); 17127c478bd9Sstevel@tonic-gate ASSERT(start + plen <= *offp + *lenp); 17137c478bd9Sstevel@tonic-gate 17147c478bd9Sstevel@tonic-gate for (off = start, poff = pstart; poff < pstart + plen; 17157c478bd9Sstevel@tonic-gate off += PAGESIZE, poff += PAGESIZE) { 171623d9e5acSMichael Corcoran ahm = AH_MUTEX(vp, off); 17177c478bd9Sstevel@tonic-gate mutex_enter(ahm); 17187c478bd9Sstevel@tonic-gate if ((ap = swap_anon(vp, off)) != NULL) { 17197c478bd9Sstevel@tonic-gate /* Free old slot if any, and assign new one */ 17207c478bd9Sstevel@tonic-gate if (ap->an_pvp) 17217c478bd9Sstevel@tonic-gate swap_phys_free(ap->an_pvp, ap->an_poff, 17227c478bd9Sstevel@tonic-gate PAGESIZE); 17237c478bd9Sstevel@tonic-gate ap->an_pvp = pvp; 17247c478bd9Sstevel@tonic-gate ap->an_poff = poff; 17257c478bd9Sstevel@tonic-gate } else { /* No anon slot for a klustered page, quit. */ 17267c478bd9Sstevel@tonic-gate prem = (pstart + plen) - poff; 17277c478bd9Sstevel@tonic-gate /* Already did requested page, do partial kluster */ 17287c478bd9Sstevel@tonic-gate if (off > offset) { 17297c478bd9Sstevel@tonic-gate plen = poff - pstart; 17307c478bd9Sstevel@tonic-gate error = 0; 17317c478bd9Sstevel@tonic-gate /* Fail on requested page, error */ 17327c478bd9Sstevel@tonic-gate } else if (off == offset) { 17337c478bd9Sstevel@tonic-gate error = SE_NOANON; 17347c478bd9Sstevel@tonic-gate /* Fail on prior page, fail on requested page, error */ 17357c478bd9Sstevel@tonic-gate } else if ((ap = swap_anon(vp, offset)) == NULL) { 17367c478bd9Sstevel@tonic-gate error = SE_NOANON; 17377c478bd9Sstevel@tonic-gate /* Fail on prior page, got requested page, do only it */ 17387c478bd9Sstevel@tonic-gate } else { 17397c478bd9Sstevel@tonic-gate /* Free old slot if any, and assign new one */ 17407c478bd9Sstevel@tonic-gate if (ap->an_pvp) 17417c478bd9Sstevel@tonic-gate swap_phys_free(ap->an_pvp, ap->an_poff, 17427c478bd9Sstevel@tonic-gate PAGESIZE); 17437c478bd9Sstevel@tonic-gate ap->an_pvp = pvp; 17447c478bd9Sstevel@tonic-gate ap->an_poff = poff; 17457c478bd9Sstevel@tonic-gate /* One page kluster */ 17467c478bd9Sstevel@tonic-gate start = offset; 17477c478bd9Sstevel@tonic-gate plen = PAGESIZE; 17487c478bd9Sstevel@tonic-gate pstart = poff; 17497c478bd9Sstevel@tonic-gate poff += PAGESIZE; 17507c478bd9Sstevel@tonic-gate prem -= PAGESIZE; 17517c478bd9Sstevel@tonic-gate } 17527c478bd9Sstevel@tonic-gate /* Free unassigned slots */ 17537c478bd9Sstevel@tonic-gate swap_phys_free(pvp, poff, prem); 17547c478bd9Sstevel@tonic-gate mutex_exit(ahm); 17557c478bd9Sstevel@tonic-gate break; 17567c478bd9Sstevel@tonic-gate } 17577c478bd9Sstevel@tonic-gate mutex_exit(ahm); 17587c478bd9Sstevel@tonic-gate } 17597c478bd9Sstevel@tonic-gate ASSERT(*offp <= start && start + plen <= *offp + *lenp); 17607c478bd9Sstevel@tonic-gate ASSERT(start <= offset && offset < start + plen); 17617c478bd9Sstevel@tonic-gate *offp = start; 17627c478bd9Sstevel@tonic-gate *lenp = plen; 17637c478bd9Sstevel@tonic-gate *pvpp = pvp; 17647c478bd9Sstevel@tonic-gate *poffp = pstart; 17657c478bd9Sstevel@tonic-gate return (error); 17667c478bd9Sstevel@tonic-gate } 17677c478bd9Sstevel@tonic-gate 17687c478bd9Sstevel@tonic-gate 17697c478bd9Sstevel@tonic-gate /* 17707c478bd9Sstevel@tonic-gate * Get the physical swap backing store location for a given anonymous page 17717c478bd9Sstevel@tonic-gate * named (vp, off). The backing store name is returned in (*pvpp, *poffp). 17727c478bd9Sstevel@tonic-gate * Returns 0 success 17737c478bd9Sstevel@tonic-gate * EIDRM -- no anon slot (page is not allocated) 17747c478bd9Sstevel@tonic-gate */ 17757c478bd9Sstevel@tonic-gate int 17767c478bd9Sstevel@tonic-gate swap_getphysname( 17777c478bd9Sstevel@tonic-gate struct vnode *vp, 17787c478bd9Sstevel@tonic-gate u_offset_t off, 17797c478bd9Sstevel@tonic-gate struct vnode **pvpp, 17807c478bd9Sstevel@tonic-gate u_offset_t *poffp) 17817c478bd9Sstevel@tonic-gate { 17827c478bd9Sstevel@tonic-gate struct anon *ap; 17837c478bd9Sstevel@tonic-gate int error = 0; 17847c478bd9Sstevel@tonic-gate kmutex_t *ahm; 17857c478bd9Sstevel@tonic-gate 178623d9e5acSMichael Corcoran ahm = AH_MUTEX(vp, off); 17877c478bd9Sstevel@tonic-gate mutex_enter(ahm); 17887c478bd9Sstevel@tonic-gate 17897c478bd9Sstevel@tonic-gate /* Get anon slot for vp, off */ 17907c478bd9Sstevel@tonic-gate ap = swap_anon(vp, off); 17917c478bd9Sstevel@tonic-gate if (ap == NULL) { 17927c478bd9Sstevel@tonic-gate error = EIDRM; 17937c478bd9Sstevel@tonic-gate goto out; 17947c478bd9Sstevel@tonic-gate } 17957c478bd9Sstevel@tonic-gate *pvpp = ap->an_pvp; 17967c478bd9Sstevel@tonic-gate *poffp = ap->an_poff; 17977c478bd9Sstevel@tonic-gate out: 17987c478bd9Sstevel@tonic-gate mutex_exit(ahm); 17997c478bd9Sstevel@tonic-gate return (error); 18007c478bd9Sstevel@tonic-gate } 1801