1df8bae1dSRodney W. Grimes /* 21c7c3c6aSMatthew Dillon * Copyright (c) 1998 Matthew Dillon, 326f9a767SRodney W. Grimes * Copyright (c) 1994 John S. Dyson 4df8bae1dSRodney W. Grimes * Copyright (c) 1990 University of Utah. 5df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 6df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 7df8bae1dSRodney W. Grimes * 8df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 9df8bae1dSRodney W. Grimes * the Systems Programming Group of the University of Utah Computer 10df8bae1dSRodney W. Grimes * Science Department. 11df8bae1dSRodney W. Grimes * 12df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 13df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 14df8bae1dSRodney W. Grimes * are met: 15df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 17df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 19df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 20df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 215929bcfaSPhilippe Charnier * must display the following acknowledgement: 22df8bae1dSRodney W. Grimes * This product includes software developed by the University of 23df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 24df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 25df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 26df8bae1dSRodney W. Grimes * without specific prior written permission. 27df8bae1dSRodney W. Grimes * 28df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38df8bae1dSRodney W. Grimes * SUCH DAMAGE. 39df8bae1dSRodney W. Grimes * 401c7c3c6aSMatthew Dillon * New Swap System 411c7c3c6aSMatthew Dillon * Matthew Dillon 421c7c3c6aSMatthew Dillon * 431c7c3c6aSMatthew Dillon * Radix Bitmap 'blists'. 441c7c3c6aSMatthew Dillon * 451c7c3c6aSMatthew Dillon * - The new swapper uses the new radix bitmap code. This should scale 461c7c3c6aSMatthew Dillon * to arbitrarily small or arbitrarily large swap spaces and an almost 471c7c3c6aSMatthew Dillon * arbitrary degree of fragmentation. 481c7c3c6aSMatthew Dillon * 491c7c3c6aSMatthew Dillon * Features: 501c7c3c6aSMatthew Dillon * 511c7c3c6aSMatthew Dillon * - on the fly reallocation of swap during putpages. The new system 521c7c3c6aSMatthew Dillon * does not try to keep previously allocated swap blocks for dirty 531c7c3c6aSMatthew Dillon * pages. 541c7c3c6aSMatthew Dillon * 551c7c3c6aSMatthew Dillon * - on the fly deallocation of swap 561c7c3c6aSMatthew Dillon * 571c7c3c6aSMatthew Dillon * - No more garbage collection required. Unnecessarily allocated swap 581c7c3c6aSMatthew Dillon * blocks only exist for dirty vm_page_t's now and these are already 591c7c3c6aSMatthew Dillon * cycled (in a high-load system) by the pager. We also do on-the-fly 601c7c3c6aSMatthew Dillon * removal of invalidated swap blocks when a page is destroyed 611c7c3c6aSMatthew Dillon * or renamed. 621c7c3c6aSMatthew Dillon * 63df8bae1dSRodney W. Grimes * from: Utah $Hdr: swap_pager.c 1.4 91/04/30$ 64df8bae1dSRodney W. Grimes * 65df8bae1dSRodney W. Grimes * @(#)swap_pager.c 8.9 (Berkeley) 3/21/94 66df8bae1dSRodney W. Grimes */ 67df8bae1dSRodney W. Grimes 68874651b1SDavid E. O'Brien #include <sys/cdefs.h> 69874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 70874651b1SDavid E. O'Brien 71df8bae1dSRodney W. Grimes #include <sys/param.h> 72df8bae1dSRodney W. Grimes #include <sys/systm.h> 73af647ddeSBruce Evans #include <sys/conf.h> 7464abb5a5SDavid Greenman #include <sys/kernel.h> 75df8bae1dSRodney W. Grimes #include <sys/proc.h> 769626b608SPoul-Henning Kamp #include <sys/bio.h> 77df8bae1dSRodney W. Grimes #include <sys/buf.h> 78df8bae1dSRodney W. Grimes #include <sys/vnode.h> 79df8bae1dSRodney W. Grimes #include <sys/malloc.h> 80327f4e83SMatthew Dillon #include <sys/sysctl.h> 811c7c3c6aSMatthew Dillon #include <sys/blist.h> 821c7c3c6aSMatthew Dillon #include <sys/lock.h> 830cddd8f0SMatthew Dillon #include <sys/sx.h> 84936524aaSMatthew Dillon #include <sys/vmmeter.h> 85df8bae1dSRodney W. Grimes 86e47ed70bSJohn Dyson #ifndef MAX_PAGEOUT_CLUSTER 87ffc82b0aSJohn Dyson #define MAX_PAGEOUT_CLUSTER 16 88e47ed70bSJohn Dyson #endif 89e47ed70bSJohn Dyson 90e47ed70bSJohn Dyson #define SWB_NPAGES MAX_PAGEOUT_CLUSTER 91e47ed70bSJohn Dyson 921c7c3c6aSMatthew Dillon #include "opt_swap.h" 93df8bae1dSRodney W. Grimes #include <vm/vm.h> 9421cd6e62SSeigo Tanimura #include <vm/pmap.h> 9521cd6e62SSeigo Tanimura #include <vm/vm_map.h> 9621cd6e62SSeigo Tanimura #include <vm/vm_kern.h> 97efeaf95aSDavid Greenman #include <vm/vm_object.h> 98df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 99efeaf95aSDavid Greenman #include <vm/vm_pager.h> 100df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 101df8bae1dSRodney W. Grimes #include <vm/swap_pager.h> 102efeaf95aSDavid Greenman #include <vm/vm_extern.h> 103670d17b5SJeff Roberson #include <vm/uma.h> 104df8bae1dSRodney W. Grimes 1051c7c3c6aSMatthew Dillon #define SWM_FREE 0x02 /* free, period */ 1061c7c3c6aSMatthew Dillon #define SWM_POP 0x04 /* pop out */ 10726f9a767SRodney W. Grimes 10820d3034fSMatthew Dillon int swap_pager_full; /* swap space exhaustion (task killing) */ 10920d3034fSMatthew Dillon static int swap_pager_almost_full; /* swap space exhaustion (w/ hysteresis)*/ 1101c7c3c6aSMatthew Dillon static int nsw_rcount; /* free read buffers */ 111327f4e83SMatthew Dillon static int nsw_wcount_sync; /* limit write buffers / synchronous */ 112327f4e83SMatthew Dillon static int nsw_wcount_async; /* limit write buffers / asynchronous */ 113327f4e83SMatthew Dillon static int nsw_wcount_async_max;/* assigned maximum */ 114327f4e83SMatthew Dillon static int nsw_cluster_max; /* maximum VOP I/O allowed */ 1151c7c3c6aSMatthew Dillon 1161c7c3c6aSMatthew Dillon struct blist *swapblist; 1171c7c3c6aSMatthew Dillon static struct swblock **swhash; 1181c7c3c6aSMatthew Dillon static int swhash_mask; 119327f4e83SMatthew Dillon static int swap_async_max = 4; /* maximum in-progress async I/O's */ 1200cddd8f0SMatthew Dillon static struct sx sw_alloc_sx; 121327f4e83SMatthew Dillon 12224e7ab7cSPoul-Henning Kamp 123327f4e83SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, swap_async_max, 124327f4e83SMatthew Dillon CTLFLAG_RW, &swap_async_max, 0, "Maximum running async swap ops"); 1251c7c3c6aSMatthew Dillon 126edfa785aSRobert Watson #define BLK2DEVIDX(blk) (nswdev > 1 ? blk / dmmax % nswdev : 0) 127edfa785aSRobert Watson 1281c7c3c6aSMatthew Dillon /* 1291c7c3c6aSMatthew Dillon * "named" and "unnamed" anon region objects. Try to reduce the overhead 1301c7c3c6aSMatthew Dillon * of searching a named list by hashing it just a little. 1311c7c3c6aSMatthew Dillon */ 1321c7c3c6aSMatthew Dillon 1331c7c3c6aSMatthew Dillon #define NOBJLISTS 8 1341c7c3c6aSMatthew Dillon 1351c7c3c6aSMatthew Dillon #define NOBJLIST(handle) \ 136af647ddeSBruce Evans (&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)]) 1371c7c3c6aSMatthew Dillon 138a9fa2c05SAlfred Perlstein static struct mtx sw_alloc_mtx; /* protect list manipulation */ 1391c7c3c6aSMatthew Dillon static struct pagerlst swap_pager_object_list[NOBJLISTS]; 1401c7c3c6aSMatthew Dillon struct pagerlst swap_pager_un_object_list; 141670d17b5SJeff Roberson uma_zone_t swap_zone; 1421c7c3c6aSMatthew Dillon 1431c7c3c6aSMatthew Dillon /* 1441c7c3c6aSMatthew Dillon * pagerops for OBJT_SWAP - "swap pager". Some ops are also global procedure 1451c7c3c6aSMatthew Dillon * calls hooked from other parts of the VM system and do not appear here. 1461c7c3c6aSMatthew Dillon * (see vm/swap_pager.h). 1471c7c3c6aSMatthew Dillon */ 148ff98689dSBruce Evans static vm_object_t 14911caded3SAlfred Perlstein swap_pager_alloc(void *handle, vm_ooffset_t size, 15011caded3SAlfred Perlstein vm_prot_t prot, vm_ooffset_t offset); 15111caded3SAlfred Perlstein static void swap_pager_dealloc(vm_object_t object); 15211caded3SAlfred Perlstein static int swap_pager_getpages(vm_object_t, vm_page_t *, int, int); 15311caded3SAlfred Perlstein static void swap_pager_init(void); 15411caded3SAlfred Perlstein static void swap_pager_unswapped(vm_page_t); 15511caded3SAlfred Perlstein static void swap_pager_strategy(vm_object_t, struct bio *); 156f708ef1bSPoul-Henning Kamp 157df8bae1dSRodney W. Grimes struct pagerops swappagerops = { 1581c7c3c6aSMatthew Dillon swap_pager_init, /* early system initialization of pager */ 1591c7c3c6aSMatthew Dillon swap_pager_alloc, /* allocate an OBJT_SWAP object */ 1601c7c3c6aSMatthew Dillon swap_pager_dealloc, /* deallocate an OBJT_SWAP object */ 1611c7c3c6aSMatthew Dillon swap_pager_getpages, /* pagein */ 1621c7c3c6aSMatthew Dillon swap_pager_putpages, /* pageout */ 1631c7c3c6aSMatthew Dillon swap_pager_haspage, /* get backing store status for page */ 164a5296b05SJulian Elischer swap_pager_unswapped, /* remove swap related to page */ 165a5296b05SJulian Elischer swap_pager_strategy /* pager strategy call */ 166df8bae1dSRodney W. Grimes }; 167df8bae1dSRodney W. Grimes 1680b441832SPoul-Henning Kamp static struct buf *getchainbuf(struct bio *bp, struct vnode *vp, int flags); 169e4057dbdSPoul-Henning Kamp static void flushchainbuf(struct buf *nbp); 1700b441832SPoul-Henning Kamp static void waitchainbuf(struct bio *bp, int count, int done); 171e4057dbdSPoul-Henning Kamp 1721c7c3c6aSMatthew Dillon /* 1731c7c3c6aSMatthew Dillon * dmmax is in page-sized chunks with the new swap system. It was 17464bcb9c8SMatthew Dillon * dev-bsized chunks in the old. dmmax is always a power of 2. 1751c7c3c6aSMatthew Dillon * 1761c7c3c6aSMatthew Dillon * swap_*() routines are externally accessible. swp_*() routines are 1771c7c3c6aSMatthew Dillon * internal. 1781c7c3c6aSMatthew Dillon */ 179c410df59SPoul-Henning Kamp int dmmax, dmmax_mask; 18020d3034fSMatthew Dillon int nswap_lowat = 128; /* in pages, swap_pager_almost_full warn */ 18120d3034fSMatthew Dillon int nswap_hiwat = 512; /* in pages, swap_pager_almost_full warn */ 18226f9a767SRodney W. Grimes 183cee313c4SRobert Watson SYSCTL_INT(_vm, OID_AUTO, dmmax, 184cee313c4SRobert Watson CTLFLAG_RD, &dmmax, 0, "Maximum size of a swap block"); 185cee313c4SRobert Watson 18611caded3SAlfred Perlstein static __inline void swp_sizecheck(void); 18711caded3SAlfred Perlstein static void swp_pager_sync_iodone(struct buf *bp); 18811caded3SAlfred Perlstein static void swp_pager_async_iodone(struct buf *bp); 18924a1cce3SDavid Greenman 1901c7c3c6aSMatthew Dillon /* 1911c7c3c6aSMatthew Dillon * Swap bitmap functions 1921c7c3c6aSMatthew Dillon */ 19311caded3SAlfred Perlstein static __inline void swp_pager_freeswapspace(daddr_t blk, int npages); 19411caded3SAlfred Perlstein static __inline daddr_t swp_pager_getswapspace(int npages); 1951c7c3c6aSMatthew Dillon 1961c7c3c6aSMatthew Dillon /* 1971c7c3c6aSMatthew Dillon * Metadata functions 1981c7c3c6aSMatthew Dillon */ 19992da00bbSMatthew Dillon static __inline struct swblock ** 20092da00bbSMatthew Dillon swp_pager_hash(vm_object_t object, vm_pindex_t index); 20111caded3SAlfred Perlstein static void swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t); 20211caded3SAlfred Perlstein static void swp_pager_meta_free(vm_object_t, vm_pindex_t, daddr_t); 20311caded3SAlfred Perlstein static void swp_pager_meta_free_all(vm_object_t); 20411caded3SAlfred Perlstein static daddr_t swp_pager_meta_ctl(vm_object_t, vm_pindex_t, int); 2051c7c3c6aSMatthew Dillon 2061c7c3c6aSMatthew Dillon /* 2071c7c3c6aSMatthew Dillon * SWP_SIZECHECK() - update swap_pager_full indication 2081c7c3c6aSMatthew Dillon * 20920d3034fSMatthew Dillon * update the swap_pager_almost_full indication and warn when we are 21020d3034fSMatthew Dillon * about to run out of swap space, using lowat/hiwat hysteresis. 21120d3034fSMatthew Dillon * 21220d3034fSMatthew Dillon * Clear swap_pager_full ( task killing ) indication when lowat is met. 2131c7c3c6aSMatthew Dillon * 2141c7c3c6aSMatthew Dillon * No restrictions on call 2151c7c3c6aSMatthew Dillon * This routine may not block. 2161c7c3c6aSMatthew Dillon * This routine must be called at splvm() 2171c7c3c6aSMatthew Dillon */ 218c1087c13SBruce Evans static __inline void 2191c7c3c6aSMatthew Dillon swp_sizecheck() 2200d94caffSDavid Greenman { 2210cddd8f0SMatthew Dillon GIANT_REQUIRED; 22223955314SAlfred Perlstein 2231c7c3c6aSMatthew Dillon if (vm_swap_size < nswap_lowat) { 22420d3034fSMatthew Dillon if (swap_pager_almost_full == 0) { 2251af87c92SDavid Greenman printf("swap_pager: out of swap space\n"); 22620d3034fSMatthew Dillon swap_pager_almost_full = 1; 2272b0d37a4SMatthew Dillon } 22820d3034fSMatthew Dillon } else { 22926f9a767SRodney W. Grimes swap_pager_full = 0; 23020d3034fSMatthew Dillon if (vm_swap_size > nswap_hiwat) 23120d3034fSMatthew Dillon swap_pager_almost_full = 0; 23226f9a767SRodney W. Grimes } 2331c7c3c6aSMatthew Dillon } 2341c7c3c6aSMatthew Dillon 2351c7c3c6aSMatthew Dillon /* 2361c7c3c6aSMatthew Dillon * SWAP_PAGER_INIT() - initialize the swap pager! 2371c7c3c6aSMatthew Dillon * 2381c7c3c6aSMatthew Dillon * Expected to be started from system init. NOTE: This code is run 2391c7c3c6aSMatthew Dillon * before much else so be careful what you depend on. Most of the VM 2401c7c3c6aSMatthew Dillon * system has yet to be initialized at this point. 2411c7c3c6aSMatthew Dillon */ 242f5a12711SPoul-Henning Kamp static void 243df8bae1dSRodney W. Grimes swap_pager_init() 244df8bae1dSRodney W. Grimes { 2451c7c3c6aSMatthew Dillon /* 2461c7c3c6aSMatthew Dillon * Initialize object lists 2471c7c3c6aSMatthew Dillon */ 2481c7c3c6aSMatthew Dillon int i; 2491c7c3c6aSMatthew Dillon 2501c7c3c6aSMatthew Dillon for (i = 0; i < NOBJLISTS; ++i) 2511c7c3c6aSMatthew Dillon TAILQ_INIT(&swap_pager_object_list[i]); 25224a1cce3SDavid Greenman TAILQ_INIT(&swap_pager_un_object_list); 2536008862bSJohn Baldwin mtx_init(&sw_alloc_mtx, "swap_pager list", NULL, MTX_DEF); 254df8bae1dSRodney W. Grimes 255df8bae1dSRodney W. Grimes /* 2561c7c3c6aSMatthew Dillon * Device Stripe, in PAGE_SIZE'd blocks 257df8bae1dSRodney W. Grimes */ 2581c7c3c6aSMatthew Dillon dmmax = SWB_NPAGES * 2; 2591c7c3c6aSMatthew Dillon dmmax_mask = ~(dmmax - 1); 2601c7c3c6aSMatthew Dillon } 26126f9a767SRodney W. Grimes 262df8bae1dSRodney W. Grimes /* 2631c7c3c6aSMatthew Dillon * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process 2641c7c3c6aSMatthew Dillon * 2651c7c3c6aSMatthew Dillon * Expected to be started from pageout process once, prior to entering 2661c7c3c6aSMatthew Dillon * its main loop. 267df8bae1dSRodney W. Grimes */ 26824a1cce3SDavid Greenman void 26924a1cce3SDavid Greenman swap_pager_swap_init() 270df8bae1dSRodney W. Grimes { 27121cd6e62SSeigo Tanimura int n, n2; 2720d94caffSDavid Greenman 27326f9a767SRodney W. Grimes /* 2741c7c3c6aSMatthew Dillon * Number of in-transit swap bp operations. Don't 2751c7c3c6aSMatthew Dillon * exhaust the pbufs completely. Make sure we 2761c7c3c6aSMatthew Dillon * initialize workable values (0 will work for hysteresis 2771c7c3c6aSMatthew Dillon * but it isn't very efficient). 2781c7c3c6aSMatthew Dillon * 279327f4e83SMatthew Dillon * The nsw_cluster_max is constrained by the bp->b_pages[] 2801c7c3c6aSMatthew Dillon * array (MAXPHYS/PAGE_SIZE) and our locally defined 2811c7c3c6aSMatthew Dillon * MAX_PAGEOUT_CLUSTER. Also be aware that swap ops are 2821c7c3c6aSMatthew Dillon * constrained by the swap device interleave stripe size. 283327f4e83SMatthew Dillon * 284327f4e83SMatthew Dillon * Currently we hardwire nsw_wcount_async to 4. This limit is 285327f4e83SMatthew Dillon * designed to prevent other I/O from having high latencies due to 286327f4e83SMatthew Dillon * our pageout I/O. The value 4 works well for one or two active swap 287327f4e83SMatthew Dillon * devices but is probably a little low if you have more. Even so, 288327f4e83SMatthew Dillon * a higher value would probably generate only a limited improvement 289327f4e83SMatthew Dillon * with three or four active swap devices since the system does not 290327f4e83SMatthew Dillon * typically have to pageout at extreme bandwidths. We will want 291327f4e83SMatthew Dillon * at least 2 per swap devices, and 4 is a pretty good value if you 292327f4e83SMatthew Dillon * have one NFS swap device due to the command/ack latency over NFS. 293327f4e83SMatthew Dillon * So it all works out pretty well. 29426f9a767SRodney W. Grimes */ 295ad3cce20SMatthew Dillon nsw_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER); 296327f4e83SMatthew Dillon 2976d541bf1SJohn Baldwin mtx_lock(&pbuf_mtx); 2981c7c3c6aSMatthew Dillon nsw_rcount = (nswbuf + 1) / 2; 299327f4e83SMatthew Dillon nsw_wcount_sync = (nswbuf + 3) / 4; 300327f4e83SMatthew Dillon nsw_wcount_async = 4; 301327f4e83SMatthew Dillon nsw_wcount_async_max = nsw_wcount_async; 3026d541bf1SJohn Baldwin mtx_unlock(&pbuf_mtx); 30324a1cce3SDavid Greenman 3041c7c3c6aSMatthew Dillon /* 3051c7c3c6aSMatthew Dillon * Initialize our zone. Right now I'm just guessing on the number 3061c7c3c6aSMatthew Dillon * we need based on the number of pages in the system. Each swblock 3072f9e4e80SMatthew Dillon * can hold 16 pages, so this is probably overkill. This reservation 308ec61f55dSMatthew Dillon * is typically limited to around 32MB by default. 3091c7c3c6aSMatthew Dillon */ 310ec61f55dSMatthew Dillon n = cnt.v_page_count / 2; 3112f9e4e80SMatthew Dillon if (maxswzone && n > maxswzone / sizeof(struct swblock)) 3122f9e4e80SMatthew Dillon n = maxswzone / sizeof(struct swblock); 31321cd6e62SSeigo Tanimura n2 = n; 314670d17b5SJeff Roberson swap_zone = uma_zcreate("SWAPMETA", sizeof(struct swblock), NULL, NULL, 315670d17b5SJeff Roberson NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 3168355f576SJeff Roberson do { 3178355f576SJeff Roberson if (uma_zone_set_obj(swap_zone, NULL, n)) 31861ce6eeeSAlfred Perlstein break; 31961ce6eeeSAlfred Perlstein /* 32061ce6eeeSAlfred Perlstein * if the allocation failed, try a zone two thirds the 32161ce6eeeSAlfred Perlstein * size of the previous attempt. 32261ce6eeeSAlfred Perlstein */ 32361ce6eeeSAlfred Perlstein n -= ((n + 2) / 3); 32461ce6eeeSAlfred Perlstein } while (n > 0); 32521cd6e62SSeigo Tanimura if (swap_zone == NULL) 326670d17b5SJeff Roberson panic("failed to create swap_zone."); 32721cd6e62SSeigo Tanimura if (n2 != n) 32861ce6eeeSAlfred Perlstein printf("Swap zone entries reduced from %d to %d.\n", n2, n); 32921cd6e62SSeigo Tanimura n2 = n; 33024a1cce3SDavid Greenman 3311c7c3c6aSMatthew Dillon /* 3321c7c3c6aSMatthew Dillon * Initialize our meta-data hash table. The swapper does not need to 3331c7c3c6aSMatthew Dillon * be quite as efficient as the VM system, so we do not use an 3341c7c3c6aSMatthew Dillon * oversized hash table. 3351c7c3c6aSMatthew Dillon * 3361c7c3c6aSMatthew Dillon * n: size of hash table, must be power of 2 3371c7c3c6aSMatthew Dillon * swhash_mask: hash table index mask 3381c7c3c6aSMatthew Dillon */ 33961ce6eeeSAlfred Perlstein for (n = 1; n < n2 / 8; n *= 2) 3401c7c3c6aSMatthew Dillon ; 341a163d034SWarner Losh swhash = malloc(sizeof(struct swblock *) * n, M_VMPGDATA, M_WAITOK | M_ZERO); 3421c7c3c6aSMatthew Dillon swhash_mask = n - 1; 34324a1cce3SDavid Greenman } 34424a1cce3SDavid Greenman 34524a1cce3SDavid Greenman /* 3461c7c3c6aSMatthew Dillon * SWAP_PAGER_ALLOC() - allocate a new OBJT_SWAP VM object and instantiate 3471c7c3c6aSMatthew Dillon * its metadata structures. 3481c7c3c6aSMatthew Dillon * 3491c7c3c6aSMatthew Dillon * This routine is called from the mmap and fork code to create a new 3501c7c3c6aSMatthew Dillon * OBJT_SWAP object. We do this by creating an OBJT_DEFAULT object 3511c7c3c6aSMatthew Dillon * and then converting it with swp_pager_meta_build(). 3521c7c3c6aSMatthew Dillon * 3531c7c3c6aSMatthew Dillon * This routine may block in vm_object_allocate() and create a named 3541c7c3c6aSMatthew Dillon * object lookup race, so we must interlock. We must also run at 3551c7c3c6aSMatthew Dillon * splvm() for the object lookup to handle races with interrupts, but 3561c7c3c6aSMatthew Dillon * we do not have to maintain splvm() in between the lookup and the 3571c7c3c6aSMatthew Dillon * add because (I believe) it is not possible to attempt to create 3581c7c3c6aSMatthew Dillon * a new swap object w/handle when a default object with that handle 3591c7c3c6aSMatthew Dillon * already exists. 36024c46d03SAlan Cox * 36124c46d03SAlan Cox * MPSAFE 36224a1cce3SDavid Greenman */ 363f5a12711SPoul-Henning Kamp static vm_object_t 3646cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 365b9dcd593SBruce Evans vm_ooffset_t offset) 36624a1cce3SDavid Greenman { 36724a1cce3SDavid Greenman vm_object_t object; 36824a1cce3SDavid Greenman 36924c46d03SAlan Cox mtx_lock(&Giant); 37024a1cce3SDavid Greenman if (handle) { 3711c7c3c6aSMatthew Dillon /* 3721c7c3c6aSMatthew Dillon * Reference existing named region or allocate new one. There 3731c7c3c6aSMatthew Dillon * should not be a race here against swp_pager_meta_build() 3741c7c3c6aSMatthew Dillon * as called from vm_page_remove() in regards to the lookup 3751c7c3c6aSMatthew Dillon * of the handle. 3761c7c3c6aSMatthew Dillon */ 3770cddd8f0SMatthew Dillon sx_xlock(&sw_alloc_sx); 3781c7c3c6aSMatthew Dillon object = vm_pager_object_lookup(NOBJLIST(handle), handle); 3791c7c3c6aSMatthew Dillon 38024a1cce3SDavid Greenman if (object != NULL) { 38124a1cce3SDavid Greenman vm_object_reference(object); 38224a1cce3SDavid Greenman } else { 3831c7c3c6aSMatthew Dillon object = vm_object_allocate(OBJT_DEFAULT, 3846cde7a16SDavid Greenman OFF_TO_IDX(offset + PAGE_MASK + size)); 38524a1cce3SDavid Greenman object->handle = handle; 3861c7c3c6aSMatthew Dillon 3874dcc5c2dSMatthew Dillon swp_pager_meta_build(object, 0, SWAPBLK_NONE); 38824a1cce3SDavid Greenman } 3890cddd8f0SMatthew Dillon sx_xunlock(&sw_alloc_sx); 39024a1cce3SDavid Greenman } else { 3911c7c3c6aSMatthew Dillon object = vm_object_allocate(OBJT_DEFAULT, 3926cde7a16SDavid Greenman OFF_TO_IDX(offset + PAGE_MASK + size)); 3931c7c3c6aSMatthew Dillon 3944dcc5c2dSMatthew Dillon swp_pager_meta_build(object, 0, SWAPBLK_NONE); 39524a1cce3SDavid Greenman } 39624c46d03SAlan Cox mtx_unlock(&Giant); 39724a1cce3SDavid Greenman return (object); 398df8bae1dSRodney W. Grimes } 399df8bae1dSRodney W. Grimes 40026f9a767SRodney W. Grimes /* 4011c7c3c6aSMatthew Dillon * SWAP_PAGER_DEALLOC() - remove swap metadata from object 4021c7c3c6aSMatthew Dillon * 4031c7c3c6aSMatthew Dillon * The swap backing for the object is destroyed. The code is 4041c7c3c6aSMatthew Dillon * designed such that we can reinstantiate it later, but this 4051c7c3c6aSMatthew Dillon * routine is typically called only when the entire object is 4061c7c3c6aSMatthew Dillon * about to be destroyed. 4071c7c3c6aSMatthew Dillon * 4081c7c3c6aSMatthew Dillon * This routine may block, but no longer does. 4091c7c3c6aSMatthew Dillon * 4101c7c3c6aSMatthew Dillon * The object must be locked or unreferenceable. 41126f9a767SRodney W. Grimes */ 412df8bae1dSRodney W. Grimes static void 4131c7c3c6aSMatthew Dillon swap_pager_dealloc(object) 4142a4895f4SDavid Greenman vm_object_t object; 41526f9a767SRodney W. Grimes { 4164dcc5c2dSMatthew Dillon int s; 4174dcc5c2dSMatthew Dillon 4180cddd8f0SMatthew Dillon GIANT_REQUIRED; 4190cddd8f0SMatthew Dillon 42026f9a767SRodney W. Grimes /* 4211c7c3c6aSMatthew Dillon * Remove from list right away so lookups will fail if we block for 4221c7c3c6aSMatthew Dillon * pageout completion. 42326f9a767SRodney W. Grimes */ 424a9fa2c05SAlfred Perlstein mtx_lock(&sw_alloc_mtx); 4251c7c3c6aSMatthew Dillon if (object->handle == NULL) { 4261c7c3c6aSMatthew Dillon TAILQ_REMOVE(&swap_pager_un_object_list, object, pager_object_list); 42724ea4a96SDavid Greenman } else { 4281c7c3c6aSMatthew Dillon TAILQ_REMOVE(NOBJLIST(object->handle), object, pager_object_list); 42926f9a767SRodney W. Grimes } 430a9fa2c05SAlfred Perlstein mtx_unlock(&sw_alloc_mtx); 4311c7c3c6aSMatthew Dillon 432658ad5ffSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 4331c7c3c6aSMatthew Dillon vm_object_pip_wait(object, "swpdea"); 4341c7c3c6aSMatthew Dillon 4351c7c3c6aSMatthew Dillon /* 4361c7c3c6aSMatthew Dillon * Free all remaining metadata. We only bother to free it from 4371c7c3c6aSMatthew Dillon * the swap meta data. We do not attempt to free swapblk's still 4381c7c3c6aSMatthew Dillon * associated with vm_page_t's for this object. We do not care 4391c7c3c6aSMatthew Dillon * if paging is still in progress on some objects. 4401c7c3c6aSMatthew Dillon */ 4414dcc5c2dSMatthew Dillon s = splvm(); 4421c7c3c6aSMatthew Dillon swp_pager_meta_free_all(object); 4434dcc5c2dSMatthew Dillon splx(s); 4441c7c3c6aSMatthew Dillon } 4451c7c3c6aSMatthew Dillon 4461c7c3c6aSMatthew Dillon /************************************************************************ 4471c7c3c6aSMatthew Dillon * SWAP PAGER BITMAP ROUTINES * 4481c7c3c6aSMatthew Dillon ************************************************************************/ 4491c7c3c6aSMatthew Dillon 4501c7c3c6aSMatthew Dillon /* 4511c7c3c6aSMatthew Dillon * SWP_PAGER_GETSWAPSPACE() - allocate raw swap space 4521c7c3c6aSMatthew Dillon * 4531c7c3c6aSMatthew Dillon * Allocate swap for the requested number of pages. The starting 4541c7c3c6aSMatthew Dillon * swap block number (a page index) is returned or SWAPBLK_NONE 4551c7c3c6aSMatthew Dillon * if the allocation failed. 4561c7c3c6aSMatthew Dillon * 4571c7c3c6aSMatthew Dillon * Also has the side effect of advising that somebody made a mistake 4581c7c3c6aSMatthew Dillon * when they configured swap and didn't configure enough. 4591c7c3c6aSMatthew Dillon * 4601c7c3c6aSMatthew Dillon * Must be called at splvm() to avoid races with bitmap frees from 4611c7c3c6aSMatthew Dillon * vm_page_remove() aka swap_pager_page_removed(). 4621c7c3c6aSMatthew Dillon * 4631c7c3c6aSMatthew Dillon * This routine may not block 4641c7c3c6aSMatthew Dillon * This routine must be called at splvm(). 4651c7c3c6aSMatthew Dillon */ 4661c7c3c6aSMatthew Dillon static __inline daddr_t 4671c7c3c6aSMatthew Dillon swp_pager_getswapspace(npages) 4681c7c3c6aSMatthew Dillon int npages; 4691c7c3c6aSMatthew Dillon { 4701c7c3c6aSMatthew Dillon daddr_t blk; 4711c7c3c6aSMatthew Dillon 4720cddd8f0SMatthew Dillon GIANT_REQUIRED; 4730cddd8f0SMatthew Dillon 4741c7c3c6aSMatthew Dillon if ((blk = blist_alloc(swapblist, npages)) == SWAPBLK_NONE) { 4752b0d37a4SMatthew Dillon if (swap_pager_full != 2) { 4761c7c3c6aSMatthew Dillon printf("swap_pager_getswapspace: failed\n"); 4772b0d37a4SMatthew Dillon swap_pager_full = 2; 47820d3034fSMatthew Dillon swap_pager_almost_full = 1; 4792b0d37a4SMatthew Dillon } 4801c7c3c6aSMatthew Dillon } else { 4811c7c3c6aSMatthew Dillon vm_swap_size -= npages; 482edfa785aSRobert Watson /* per-swap area stats */ 483edfa785aSRobert Watson swdevt[BLK2DEVIDX(blk)].sw_used += npages; 4841c7c3c6aSMatthew Dillon swp_sizecheck(); 4851c7c3c6aSMatthew Dillon } 4861c7c3c6aSMatthew Dillon return (blk); 48726f9a767SRodney W. Grimes } 48826f9a767SRodney W. Grimes 48926f9a767SRodney W. Grimes /* 4901c7c3c6aSMatthew Dillon * SWP_PAGER_FREESWAPSPACE() - free raw swap space 4911c7c3c6aSMatthew Dillon * 4921c7c3c6aSMatthew Dillon * This routine returns the specified swap blocks back to the bitmap. 4931c7c3c6aSMatthew Dillon * 4941c7c3c6aSMatthew Dillon * Note: This routine may not block (it could in the old swap code), 4951c7c3c6aSMatthew Dillon * and through the use of the new blist routines it does not block. 4961c7c3c6aSMatthew Dillon * 4971c7c3c6aSMatthew Dillon * We must be called at splvm() to avoid races with bitmap frees from 4981c7c3c6aSMatthew Dillon * vm_page_remove() aka swap_pager_page_removed(). 4991c7c3c6aSMatthew Dillon * 5001c7c3c6aSMatthew Dillon * This routine may not block 5011c7c3c6aSMatthew Dillon * This routine must be called at splvm(). 50226f9a767SRodney W. Grimes */ 5031c7c3c6aSMatthew Dillon static __inline void 5041c7c3c6aSMatthew Dillon swp_pager_freeswapspace(blk, npages) 5051c7c3c6aSMatthew Dillon daddr_t blk; 5061c7c3c6aSMatthew Dillon int npages; 5070d94caffSDavid Greenman { 50892da00bbSMatthew Dillon struct swdevt *sp = &swdevt[BLK2DEVIDX(blk)]; 50992da00bbSMatthew Dillon 5100cddd8f0SMatthew Dillon GIANT_REQUIRED; 51123955314SAlfred Perlstein 51292da00bbSMatthew Dillon /* per-swap area stats */ 51392da00bbSMatthew Dillon sp->sw_used -= npages; 51492da00bbSMatthew Dillon 51592da00bbSMatthew Dillon /* 51692da00bbSMatthew Dillon * If we are attempting to stop swapping on this device, we 51792da00bbSMatthew Dillon * don't want to mark any blocks free lest they be reused. 51892da00bbSMatthew Dillon */ 51992da00bbSMatthew Dillon if (sp->sw_flags & SW_CLOSING) 52092da00bbSMatthew Dillon return; 52192da00bbSMatthew Dillon 5221c7c3c6aSMatthew Dillon blist_free(swapblist, blk, npages); 5231c7c3c6aSMatthew Dillon vm_swap_size += npages; 5241c7c3c6aSMatthew Dillon swp_sizecheck(); 52526f9a767SRodney W. Grimes } 5261c7c3c6aSMatthew Dillon 52726f9a767SRodney W. Grimes /* 5281c7c3c6aSMatthew Dillon * SWAP_PAGER_FREESPACE() - frees swap blocks associated with a page 5291c7c3c6aSMatthew Dillon * range within an object. 5301c7c3c6aSMatthew Dillon * 5311c7c3c6aSMatthew Dillon * This is a globally accessible routine. 5321c7c3c6aSMatthew Dillon * 5331c7c3c6aSMatthew Dillon * This routine removes swapblk assignments from swap metadata. 5341c7c3c6aSMatthew Dillon * 5351c7c3c6aSMatthew Dillon * The external callers of this routine typically have already destroyed 5361c7c3c6aSMatthew Dillon * or renamed vm_page_t's associated with this range in the object so 5371c7c3c6aSMatthew Dillon * we should be ok. 5384dcc5c2dSMatthew Dillon * 5394dcc5c2dSMatthew Dillon * This routine may be called at any spl. We up our spl to splvm temporarily 5404dcc5c2dSMatthew Dillon * in order to perform the metadata removal. 54126f9a767SRodney W. Grimes */ 54226f9a767SRodney W. Grimes void 54324a1cce3SDavid Greenman swap_pager_freespace(object, start, size) 54424a1cce3SDavid Greenman vm_object_t object; 545a316d390SJohn Dyson vm_pindex_t start; 546a316d390SJohn Dyson vm_size_t size; 54726f9a767SRodney W. Grimes { 5484dcc5c2dSMatthew Dillon int s = splvm(); 54923955314SAlfred Perlstein 55019ba4c8eSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 5511c7c3c6aSMatthew Dillon swp_pager_meta_free(object, start, size); 5524dcc5c2dSMatthew Dillon splx(s); 5534dcc5c2dSMatthew Dillon } 5544dcc5c2dSMatthew Dillon 5554dcc5c2dSMatthew Dillon /* 5564dcc5c2dSMatthew Dillon * SWAP_PAGER_RESERVE() - reserve swap blocks in object 5574dcc5c2dSMatthew Dillon * 5584dcc5c2dSMatthew Dillon * Assigns swap blocks to the specified range within the object. The 5594dcc5c2dSMatthew Dillon * swap blocks are not zerod. Any previous swap assignment is destroyed. 5604dcc5c2dSMatthew Dillon * 5614dcc5c2dSMatthew Dillon * Returns 0 on success, -1 on failure. 5624dcc5c2dSMatthew Dillon */ 5634dcc5c2dSMatthew Dillon int 5644dcc5c2dSMatthew Dillon swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size) 5654dcc5c2dSMatthew Dillon { 5664dcc5c2dSMatthew Dillon int s; 5674dcc5c2dSMatthew Dillon int n = 0; 5684dcc5c2dSMatthew Dillon daddr_t blk = SWAPBLK_NONE; 5694dcc5c2dSMatthew Dillon vm_pindex_t beg = start; /* save start index */ 5704dcc5c2dSMatthew Dillon 5714dcc5c2dSMatthew Dillon s = splvm(); 5724dcc5c2dSMatthew Dillon while (size) { 5734dcc5c2dSMatthew Dillon if (n == 0) { 5744dcc5c2dSMatthew Dillon n = BLIST_MAX_ALLOC; 5754dcc5c2dSMatthew Dillon while ((blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE) { 5764dcc5c2dSMatthew Dillon n >>= 1; 5774dcc5c2dSMatthew Dillon if (n == 0) { 5784dcc5c2dSMatthew Dillon swp_pager_meta_free(object, beg, start - beg); 5794dcc5c2dSMatthew Dillon splx(s); 5804dcc5c2dSMatthew Dillon return (-1); 5814dcc5c2dSMatthew Dillon } 5824dcc5c2dSMatthew Dillon } 5834dcc5c2dSMatthew Dillon } 5844dcc5c2dSMatthew Dillon swp_pager_meta_build(object, start, blk); 5854dcc5c2dSMatthew Dillon --size; 5864dcc5c2dSMatthew Dillon ++start; 5874dcc5c2dSMatthew Dillon ++blk; 5884dcc5c2dSMatthew Dillon --n; 5894dcc5c2dSMatthew Dillon } 5904dcc5c2dSMatthew Dillon swp_pager_meta_free(object, start, n); 5914dcc5c2dSMatthew Dillon splx(s); 5924dcc5c2dSMatthew Dillon return (0); 59326f9a767SRodney W. Grimes } 59426f9a767SRodney W. Grimes 5950a47b48bSJohn Dyson /* 5961c7c3c6aSMatthew Dillon * SWAP_PAGER_COPY() - copy blocks from source pager to destination pager 5971c7c3c6aSMatthew Dillon * and destroy the source. 5981c7c3c6aSMatthew Dillon * 5991c7c3c6aSMatthew Dillon * Copy any valid swapblks from the source to the destination. In 6001c7c3c6aSMatthew Dillon * cases where both the source and destination have a valid swapblk, 6011c7c3c6aSMatthew Dillon * we keep the destination's. 6021c7c3c6aSMatthew Dillon * 6031c7c3c6aSMatthew Dillon * This routine is allowed to block. It may block allocating metadata 6041c7c3c6aSMatthew Dillon * indirectly through swp_pager_meta_build() or if paging is still in 6051c7c3c6aSMatthew Dillon * progress on the source. 6061c7c3c6aSMatthew Dillon * 6074dcc5c2dSMatthew Dillon * This routine can be called at any spl 6084dcc5c2dSMatthew Dillon * 6091c7c3c6aSMatthew Dillon * XXX vm_page_collapse() kinda expects us not to block because we 6101c7c3c6aSMatthew Dillon * supposedly do not need to allocate memory, but for the moment we 6111c7c3c6aSMatthew Dillon * *may* have to get a little memory from the zone allocator, but 6121c7c3c6aSMatthew Dillon * it is taken from the interrupt memory. We should be ok. 6131c7c3c6aSMatthew Dillon * 6141c7c3c6aSMatthew Dillon * The source object contains no vm_page_t's (which is just as well) 6151c7c3c6aSMatthew Dillon * 6161c7c3c6aSMatthew Dillon * The source object is of type OBJT_SWAP. 6171c7c3c6aSMatthew Dillon * 6184dcc5c2dSMatthew Dillon * The source and destination objects must be locked or 6194dcc5c2dSMatthew Dillon * inaccessible (XXX are they ?) 62026f9a767SRodney W. Grimes */ 62126f9a767SRodney W. Grimes void 6221c7c3c6aSMatthew Dillon swap_pager_copy(srcobject, dstobject, offset, destroysource) 62324a1cce3SDavid Greenman vm_object_t srcobject; 62424a1cce3SDavid Greenman vm_object_t dstobject; 625a316d390SJohn Dyson vm_pindex_t offset; 626c0877f10SJohn Dyson int destroysource; 62726f9a767SRodney W. Grimes { 628a316d390SJohn Dyson vm_pindex_t i; 6294dcc5c2dSMatthew Dillon int s; 6304dcc5c2dSMatthew Dillon 6310cddd8f0SMatthew Dillon GIANT_REQUIRED; 6320cddd8f0SMatthew Dillon 6334dcc5c2dSMatthew Dillon s = splvm(); 63426f9a767SRodney W. Grimes /* 6351c7c3c6aSMatthew Dillon * If destroysource is set, we remove the source object from the 6361c7c3c6aSMatthew Dillon * swap_pager internal queue now. 63726f9a767SRodney W. Grimes */ 638cbd8ec09SJohn Dyson if (destroysource) { 639a9fa2c05SAlfred Perlstein mtx_lock(&sw_alloc_mtx); 64024a1cce3SDavid Greenman if (srcobject->handle == NULL) { 6411c7c3c6aSMatthew Dillon TAILQ_REMOVE( 6421c7c3c6aSMatthew Dillon &swap_pager_un_object_list, 6431c7c3c6aSMatthew Dillon srcobject, 6441c7c3c6aSMatthew Dillon pager_object_list 6451c7c3c6aSMatthew Dillon ); 64626f9a767SRodney W. Grimes } else { 6471c7c3c6aSMatthew Dillon TAILQ_REMOVE( 6481c7c3c6aSMatthew Dillon NOBJLIST(srcobject->handle), 6491c7c3c6aSMatthew Dillon srcobject, 6501c7c3c6aSMatthew Dillon pager_object_list 6511c7c3c6aSMatthew Dillon ); 65226f9a767SRodney W. Grimes } 653a9fa2c05SAlfred Perlstein mtx_unlock(&sw_alloc_mtx); 654cbd8ec09SJohn Dyson } 65526f9a767SRodney W. Grimes 6561c7c3c6aSMatthew Dillon /* 6571c7c3c6aSMatthew Dillon * transfer source to destination. 6581c7c3c6aSMatthew Dillon */ 6591c7c3c6aSMatthew Dillon for (i = 0; i < dstobject->size; ++i) { 6601c7c3c6aSMatthew Dillon daddr_t dstaddr; 6611c7c3c6aSMatthew Dillon 6621c7c3c6aSMatthew Dillon /* 6631c7c3c6aSMatthew Dillon * Locate (without changing) the swapblk on the destination, 6641c7c3c6aSMatthew Dillon * unless it is invalid in which case free it silently, or 6651c7c3c6aSMatthew Dillon * if the destination is a resident page, in which case the 6661c7c3c6aSMatthew Dillon * source is thrown away. 6671c7c3c6aSMatthew Dillon */ 6681c7c3c6aSMatthew Dillon dstaddr = swp_pager_meta_ctl(dstobject, i, 0); 6691c7c3c6aSMatthew Dillon 6701c7c3c6aSMatthew Dillon if (dstaddr == SWAPBLK_NONE) { 6711c7c3c6aSMatthew Dillon /* 6721c7c3c6aSMatthew Dillon * Destination has no swapblk and is not resident, 6731c7c3c6aSMatthew Dillon * copy source. 6741c7c3c6aSMatthew Dillon */ 6751c7c3c6aSMatthew Dillon daddr_t srcaddr; 6761c7c3c6aSMatthew Dillon 6771c7c3c6aSMatthew Dillon srcaddr = swp_pager_meta_ctl( 6781c7c3c6aSMatthew Dillon srcobject, 6791c7c3c6aSMatthew Dillon i + offset, 6801c7c3c6aSMatthew Dillon SWM_POP 6811c7c3c6aSMatthew Dillon ); 6821c7c3c6aSMatthew Dillon 6831c7c3c6aSMatthew Dillon if (srcaddr != SWAPBLK_NONE) 6844dcc5c2dSMatthew Dillon swp_pager_meta_build(dstobject, i, srcaddr); 6851c7c3c6aSMatthew Dillon } else { 6861c7c3c6aSMatthew Dillon /* 6871c7c3c6aSMatthew Dillon * Destination has valid swapblk or it is represented 6881c7c3c6aSMatthew Dillon * by a resident page. We destroy the sourceblock. 6891c7c3c6aSMatthew Dillon */ 6901c7c3c6aSMatthew Dillon 6911c7c3c6aSMatthew Dillon swp_pager_meta_ctl(srcobject, i + offset, SWM_FREE); 6921c7c3c6aSMatthew Dillon } 69326f9a767SRodney W. Grimes } 69426f9a767SRodney W. Grimes 69526f9a767SRodney W. Grimes /* 6961c7c3c6aSMatthew Dillon * Free left over swap blocks in source. 6971c7c3c6aSMatthew Dillon * 6981c7c3c6aSMatthew Dillon * We have to revert the type to OBJT_DEFAULT so we do not accidently 6991c7c3c6aSMatthew Dillon * double-remove the object from the swap queues. 70026f9a767SRodney W. Grimes */ 701c0877f10SJohn Dyson if (destroysource) { 7021c7c3c6aSMatthew Dillon swp_pager_meta_free_all(srcobject); 7031c7c3c6aSMatthew Dillon /* 7041c7c3c6aSMatthew Dillon * Reverting the type is not necessary, the caller is going 7051c7c3c6aSMatthew Dillon * to destroy srcobject directly, but I'm doing it here 706956f3135SPhilippe Charnier * for consistency since we've removed the object from its 7071c7c3c6aSMatthew Dillon * queues. 7081c7c3c6aSMatthew Dillon */ 7091c7c3c6aSMatthew Dillon srcobject->type = OBJT_DEFAULT; 710c0877f10SJohn Dyson } 7114dcc5c2dSMatthew Dillon splx(s); 71226f9a767SRodney W. Grimes } 71326f9a767SRodney W. Grimes 714df8bae1dSRodney W. Grimes /* 7151c7c3c6aSMatthew Dillon * SWAP_PAGER_HASPAGE() - determine if we have good backing store for 7161c7c3c6aSMatthew Dillon * the requested page. 7171c7c3c6aSMatthew Dillon * 7181c7c3c6aSMatthew Dillon * We determine whether good backing store exists for the requested 7191c7c3c6aSMatthew Dillon * page and return TRUE if it does, FALSE if it doesn't. 7201c7c3c6aSMatthew Dillon * 7211c7c3c6aSMatthew Dillon * If TRUE, we also try to determine how much valid, contiguous backing 7221c7c3c6aSMatthew Dillon * store exists before and after the requested page within a reasonable 7231c7c3c6aSMatthew Dillon * distance. We do not try to restrict it to the swap device stripe 7241c7c3c6aSMatthew Dillon * (that is handled in getpages/putpages). It probably isn't worth 7251c7c3c6aSMatthew Dillon * doing here. 726df8bae1dSRodney W. Grimes */ 7271c7c3c6aSMatthew Dillon boolean_t 728a316d390SJohn Dyson swap_pager_haspage(object, pindex, before, after) 72924a1cce3SDavid Greenman vm_object_t object; 730a316d390SJohn Dyson vm_pindex_t pindex; 73124a1cce3SDavid Greenman int *before; 73224a1cce3SDavid Greenman int *after; 73326f9a767SRodney W. Grimes { 7341c7c3c6aSMatthew Dillon daddr_t blk0; 73525db2c54SMatthew Dillon int s; 73626f9a767SRodney W. Grimes 7371c7c3c6aSMatthew Dillon /* 7381c7c3c6aSMatthew Dillon * do we have good backing store at the requested index ? 7391c7c3c6aSMatthew Dillon */ 74025db2c54SMatthew Dillon s = splvm(); 7411c7c3c6aSMatthew Dillon blk0 = swp_pager_meta_ctl(object, pindex, 0); 7421c7c3c6aSMatthew Dillon 7434dcc5c2dSMatthew Dillon if (blk0 == SWAPBLK_NONE) { 74425db2c54SMatthew Dillon splx(s); 7451c7c3c6aSMatthew Dillon if (before) 74624a1cce3SDavid Greenman *before = 0; 7471c7c3c6aSMatthew Dillon if (after) 74824a1cce3SDavid Greenman *after = 0; 74926f9a767SRodney W. Grimes return (FALSE); 75026f9a767SRodney W. Grimes } 75126f9a767SRodney W. Grimes 75226f9a767SRodney W. Grimes /* 7531c7c3c6aSMatthew Dillon * find backwards-looking contiguous good backing store 754e47ed70bSJohn Dyson */ 7551c7c3c6aSMatthew Dillon if (before != NULL) { 75626f9a767SRodney W. Grimes int i; 7570d94caffSDavid Greenman 7581c7c3c6aSMatthew Dillon for (i = 1; i < (SWB_NPAGES/2); ++i) { 7591c7c3c6aSMatthew Dillon daddr_t blk; 7601c7c3c6aSMatthew Dillon 7611c7c3c6aSMatthew Dillon if (i > pindex) 7621c7c3c6aSMatthew Dillon break; 7631c7c3c6aSMatthew Dillon blk = swp_pager_meta_ctl(object, pindex - i, 0); 7641c7c3c6aSMatthew Dillon if (blk != blk0 - i) 7651c7c3c6aSMatthew Dillon break; 766ffc82b0aSJohn Dyson } 7671c7c3c6aSMatthew Dillon *before = (i - 1); 76826f9a767SRodney W. Grimes } 76926f9a767SRodney W. Grimes 77026f9a767SRodney W. Grimes /* 7711c7c3c6aSMatthew Dillon * find forward-looking contiguous good backing store 77226f9a767SRodney W. Grimes */ 7731c7c3c6aSMatthew Dillon if (after != NULL) { 7741c7c3c6aSMatthew Dillon int i; 7751c7c3c6aSMatthew Dillon 7761c7c3c6aSMatthew Dillon for (i = 1; i < (SWB_NPAGES/2); ++i) { 7771c7c3c6aSMatthew Dillon daddr_t blk; 7781c7c3c6aSMatthew Dillon 7791c7c3c6aSMatthew Dillon blk = swp_pager_meta_ctl(object, pindex + i, 0); 7801c7c3c6aSMatthew Dillon if (blk != blk0 + i) 7811c7c3c6aSMatthew Dillon break; 78226f9a767SRodney W. Grimes } 7831c7c3c6aSMatthew Dillon *after = (i - 1); 7841c7c3c6aSMatthew Dillon } 78525db2c54SMatthew Dillon splx(s); 7861c7c3c6aSMatthew Dillon return (TRUE); 7871c7c3c6aSMatthew Dillon } 7881c7c3c6aSMatthew Dillon 7891c7c3c6aSMatthew Dillon /* 7901c7c3c6aSMatthew Dillon * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page 7911c7c3c6aSMatthew Dillon * 7921c7c3c6aSMatthew Dillon * This removes any associated swap backing store, whether valid or 7931c7c3c6aSMatthew Dillon * not, from the page. 7941c7c3c6aSMatthew Dillon * 7951c7c3c6aSMatthew Dillon * This routine is typically called when a page is made dirty, at 7961c7c3c6aSMatthew Dillon * which point any associated swap can be freed. MADV_FREE also 7971c7c3c6aSMatthew Dillon * calls us in a special-case situation 7981c7c3c6aSMatthew Dillon * 7991c7c3c6aSMatthew Dillon * NOTE!!! If the page is clean and the swap was valid, the caller 8001c7c3c6aSMatthew Dillon * should make the page dirty before calling this routine. This routine 8011c7c3c6aSMatthew Dillon * does NOT change the m->dirty status of the page. Also: MADV_FREE 8021c7c3c6aSMatthew Dillon * depends on it. 8031c7c3c6aSMatthew Dillon * 8041c7c3c6aSMatthew Dillon * This routine may not block 8054dcc5c2dSMatthew Dillon * This routine must be called at splvm() 8061c7c3c6aSMatthew Dillon */ 8071c7c3c6aSMatthew Dillon static void 8081c7c3c6aSMatthew Dillon swap_pager_unswapped(m) 8091c7c3c6aSMatthew Dillon vm_page_t m; 8101c7c3c6aSMatthew Dillon { 8111c7c3c6aSMatthew Dillon swp_pager_meta_ctl(m->object, m->pindex, SWM_FREE); 8121c7c3c6aSMatthew Dillon } 8131c7c3c6aSMatthew Dillon 8141c7c3c6aSMatthew Dillon /* 815a5296b05SJulian Elischer * SWAP_PAGER_STRATEGY() - read, write, free blocks 816a5296b05SJulian Elischer * 817a5296b05SJulian Elischer * This implements the vm_pager_strategy() interface to swap and allows 818a5296b05SJulian Elischer * other parts of the system to directly access swap as backing store 819a5296b05SJulian Elischer * through vm_objects of type OBJT_SWAP. This is intended to be a 820a5296b05SJulian Elischer * cacheless interface ( i.e. caching occurs at higher levels ). 821a5296b05SJulian Elischer * Therefore we do not maintain any resident pages. All I/O goes 8224dcc5c2dSMatthew Dillon * directly to and from the swap device. 823a5296b05SJulian Elischer * 824a5296b05SJulian Elischer * Note that b_blkno is scaled for PAGE_SIZE 825a5296b05SJulian Elischer * 826a5296b05SJulian Elischer * We currently attempt to run I/O synchronously or asynchronously as 827a5296b05SJulian Elischer * the caller requests. This isn't perfect because we loose error 828a5296b05SJulian Elischer * sequencing when we run multiple ops in parallel to satisfy a request. 829a5296b05SJulian Elischer * But this is swap, so we let it all hang out. 830a5296b05SJulian Elischer */ 831a5296b05SJulian Elischer static void 8320b441832SPoul-Henning Kamp swap_pager_strategy(vm_object_t object, struct bio *bp) 833a5296b05SJulian Elischer { 834a5296b05SJulian Elischer vm_pindex_t start; 835a5296b05SJulian Elischer int count; 8364dcc5c2dSMatthew Dillon int s; 837a5296b05SJulian Elischer char *data; 838a5296b05SJulian Elischer struct buf *nbp = NULL; 839a5296b05SJulian Elischer 8400cddd8f0SMatthew Dillon GIANT_REQUIRED; 8410cddd8f0SMatthew Dillon 8420b441832SPoul-Henning Kamp /* XXX: KASSERT instead ? */ 8430b441832SPoul-Henning Kamp if (bp->bio_bcount & PAGE_MASK) { 844a468031cSPoul-Henning Kamp biofinish(bp, NULL, EINVAL); 8450b441832SPoul-Henning Kamp printf("swap_pager_strategy: bp %p blk %d size %d, not page bounded\n", bp, (int)bp->bio_pblkno, (int)bp->bio_bcount); 846a5296b05SJulian Elischer return; 847a5296b05SJulian Elischer } 848a5296b05SJulian Elischer 849a5296b05SJulian Elischer /* 850a5296b05SJulian Elischer * Clear error indication, initialize page index, count, data pointer. 851a5296b05SJulian Elischer */ 8520b441832SPoul-Henning Kamp bp->bio_error = 0; 8530b441832SPoul-Henning Kamp bp->bio_flags &= ~BIO_ERROR; 8540b441832SPoul-Henning Kamp bp->bio_resid = bp->bio_bcount; 855d6844b6bSTor Egge *(u_int *) &bp->bio_driver1 = 0; 856a5296b05SJulian Elischer 8570b441832SPoul-Henning Kamp start = bp->bio_pblkno; 8580b441832SPoul-Henning Kamp count = howmany(bp->bio_bcount, PAGE_SIZE); 8590b441832SPoul-Henning Kamp data = bp->bio_data; 860a5296b05SJulian Elischer 8614dcc5c2dSMatthew Dillon s = splvm(); 8624dcc5c2dSMatthew Dillon 863a5296b05SJulian Elischer /* 86421144e3bSPoul-Henning Kamp * Deal with BIO_DELETE 865a5296b05SJulian Elischer */ 8660b441832SPoul-Henning Kamp if (bp->bio_cmd == BIO_DELETE) { 867a5296b05SJulian Elischer /* 868a5296b05SJulian Elischer * FREE PAGE(s) - destroy underlying swap that is no longer 869a5296b05SJulian Elischer * needed. 870a5296b05SJulian Elischer */ 871a5296b05SJulian Elischer swp_pager_meta_free(object, start, count); 872a5296b05SJulian Elischer splx(s); 8730b441832SPoul-Henning Kamp bp->bio_resid = 0; 8740b441832SPoul-Henning Kamp biodone(bp); 8754dcc5c2dSMatthew Dillon return; 8764dcc5c2dSMatthew Dillon } 8774dcc5c2dSMatthew Dillon 878a5296b05SJulian Elischer /* 8794dcc5c2dSMatthew Dillon * Execute read or write 880a5296b05SJulian Elischer */ 881a5296b05SJulian Elischer while (count > 0) { 882a5296b05SJulian Elischer daddr_t blk; 883a5296b05SJulian Elischer 884a5296b05SJulian Elischer /* 8854dcc5c2dSMatthew Dillon * Obtain block. If block not found and writing, allocate a 8864dcc5c2dSMatthew Dillon * new block and build it into the object. 8874dcc5c2dSMatthew Dillon */ 8884dcc5c2dSMatthew Dillon 8894dcc5c2dSMatthew Dillon blk = swp_pager_meta_ctl(object, start, 0); 8900b441832SPoul-Henning Kamp if ((blk == SWAPBLK_NONE) && (bp->bio_cmd == BIO_WRITE)) { 8914dcc5c2dSMatthew Dillon blk = swp_pager_getswapspace(1); 8924dcc5c2dSMatthew Dillon if (blk == SWAPBLK_NONE) { 8930b441832SPoul-Henning Kamp bp->bio_error = ENOMEM; 8940b441832SPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 8954dcc5c2dSMatthew Dillon break; 8964dcc5c2dSMatthew Dillon } 8974dcc5c2dSMatthew Dillon swp_pager_meta_build(object, start, blk); 8984dcc5c2dSMatthew Dillon } 8994dcc5c2dSMatthew Dillon 9004dcc5c2dSMatthew Dillon /* 9014dcc5c2dSMatthew Dillon * Do we have to flush our current collection? Yes if: 9024dcc5c2dSMatthew Dillon * 9034dcc5c2dSMatthew Dillon * - no swap block at this index 9044dcc5c2dSMatthew Dillon * - swap block is not contiguous 9054dcc5c2dSMatthew Dillon * - we cross a physical disk boundry in the 9064dcc5c2dSMatthew Dillon * stripe. 907a5296b05SJulian Elischer */ 908a5296b05SJulian Elischer if ( 9094dcc5c2dSMatthew Dillon nbp && (nbp->b_blkno + btoc(nbp->b_bcount) != blk || 9104dcc5c2dSMatthew Dillon ((nbp->b_blkno ^ blk) & dmmax_mask) 911a5296b05SJulian Elischer ) 912a5296b05SJulian Elischer ) { 9134dcc5c2dSMatthew Dillon splx(s); 9140b441832SPoul-Henning Kamp if (bp->bio_cmd == BIO_READ) { 915a5296b05SJulian Elischer ++cnt.v_swapin; 916a5296b05SJulian Elischer cnt.v_swappgsin += btoc(nbp->b_bcount); 9174dcc5c2dSMatthew Dillon } else { 9184dcc5c2dSMatthew Dillon ++cnt.v_swapout; 9194dcc5c2dSMatthew Dillon cnt.v_swappgsout += btoc(nbp->b_bcount); 9204dcc5c2dSMatthew Dillon nbp->b_dirtyend = nbp->b_bcount; 9214dcc5c2dSMatthew Dillon } 922a5296b05SJulian Elischer flushchainbuf(nbp); 9234dcc5c2dSMatthew Dillon s = splvm(); 924a5296b05SJulian Elischer nbp = NULL; 925a5296b05SJulian Elischer } 926a5296b05SJulian Elischer 927a5296b05SJulian Elischer /* 9284dcc5c2dSMatthew Dillon * Add new swapblk to nbp, instantiating nbp if necessary. 9294dcc5c2dSMatthew Dillon * Zero-fill reads are able to take a shortcut. 930a5296b05SJulian Elischer */ 9314dcc5c2dSMatthew Dillon if (blk == SWAPBLK_NONE) { 9324dcc5c2dSMatthew Dillon /* 9334dcc5c2dSMatthew Dillon * We can only get here if we are reading. Since 9344dcc5c2dSMatthew Dillon * we are at splvm() we can safely modify b_resid, 9354dcc5c2dSMatthew Dillon * even if chain ops are in progress. 9364dcc5c2dSMatthew Dillon */ 937a5296b05SJulian Elischer bzero(data, PAGE_SIZE); 9380b441832SPoul-Henning Kamp bp->bio_resid -= PAGE_SIZE; 939a5296b05SJulian Elischer } else { 940a5296b05SJulian Elischer if (nbp == NULL) { 9410b441832SPoul-Henning Kamp nbp = getchainbuf(bp, swapdev_vp, B_ASYNC); 942a5296b05SJulian Elischer nbp->b_blkno = blk; 9434dcc5c2dSMatthew Dillon nbp->b_bcount = 0; 944a5296b05SJulian Elischer nbp->b_data = data; 945a5296b05SJulian Elischer } 946a5296b05SJulian Elischer nbp->b_bcount += PAGE_SIZE; 947a5296b05SJulian Elischer } 948a5296b05SJulian Elischer --count; 949a5296b05SJulian Elischer ++start; 950a5296b05SJulian Elischer data += PAGE_SIZE; 951a5296b05SJulian Elischer } 952a5296b05SJulian Elischer 953a5296b05SJulian Elischer /* 9544dcc5c2dSMatthew Dillon * Flush out last buffer 955a5296b05SJulian Elischer */ 956a5296b05SJulian Elischer splx(s); 957a5296b05SJulian Elischer 958a5296b05SJulian Elischer if (nbp) { 95921144e3bSPoul-Henning Kamp if (nbp->b_iocmd == BIO_READ) { 960a5296b05SJulian Elischer ++cnt.v_swapin; 961a5296b05SJulian Elischer cnt.v_swappgsin += btoc(nbp->b_bcount); 962a5296b05SJulian Elischer } else { 963a5296b05SJulian Elischer ++cnt.v_swapout; 964a5296b05SJulian Elischer cnt.v_swappgsout += btoc(nbp->b_bcount); 9654dcc5c2dSMatthew Dillon nbp->b_dirtyend = nbp->b_bcount; 966a5296b05SJulian Elischer } 967a5296b05SJulian Elischer flushchainbuf(nbp); 9684dcc5c2dSMatthew Dillon /* nbp = NULL; */ 9690cddd8f0SMatthew Dillon } 9704dcc5c2dSMatthew Dillon /* 9714dcc5c2dSMatthew Dillon * Wait for completion. 9724dcc5c2dSMatthew Dillon */ 973a5296b05SJulian Elischer waitchainbuf(bp, 0, 1); 974a5296b05SJulian Elischer } 975a5296b05SJulian Elischer 976a5296b05SJulian Elischer /* 9771c7c3c6aSMatthew Dillon * SWAP_PAGER_GETPAGES() - bring pages in from swap 9781c7c3c6aSMatthew Dillon * 9791c7c3c6aSMatthew Dillon * Attempt to retrieve (m, count) pages from backing store, but make 9801c7c3c6aSMatthew Dillon * sure we retrieve at least m[reqpage]. We try to load in as large 9811c7c3c6aSMatthew Dillon * a chunk surrounding m[reqpage] as is contiguous in swap and which 9821c7c3c6aSMatthew Dillon * belongs to the same object. 9831c7c3c6aSMatthew Dillon * 9841c7c3c6aSMatthew Dillon * The code is designed for asynchronous operation and 9851c7c3c6aSMatthew Dillon * immediate-notification of 'reqpage' but tends not to be 9861c7c3c6aSMatthew Dillon * used that way. Please do not optimize-out this algorithmic 9871c7c3c6aSMatthew Dillon * feature, I intend to improve on it in the future. 9881c7c3c6aSMatthew Dillon * 9891c7c3c6aSMatthew Dillon * The parent has a single vm_object_pip_add() reference prior to 9901c7c3c6aSMatthew Dillon * calling us and we should return with the same. 9911c7c3c6aSMatthew Dillon * 9921c7c3c6aSMatthew Dillon * The parent has BUSY'd the pages. We should return with 'm' 9931c7c3c6aSMatthew Dillon * left busy, but the others adjusted. 9941c7c3c6aSMatthew Dillon */ 995f708ef1bSPoul-Henning Kamp static int 99624a1cce3SDavid Greenman swap_pager_getpages(object, m, count, reqpage) 99724a1cce3SDavid Greenman vm_object_t object; 99826f9a767SRodney W. Grimes vm_page_t *m; 99926f9a767SRodney W. Grimes int count, reqpage; 1000df8bae1dSRodney W. Grimes { 10011c7c3c6aSMatthew Dillon struct buf *bp; 10021c7c3c6aSMatthew Dillon vm_page_t mreq; 10031c7c3c6aSMatthew Dillon int s; 100426f9a767SRodney W. Grimes int i; 100526f9a767SRodney W. Grimes int j; 10061c7c3c6aSMatthew Dillon daddr_t blk; 10071c7c3c6aSMatthew Dillon vm_pindex_t lastpindex; 10080d94caffSDavid Greenman 10091c7c3c6aSMatthew Dillon mreq = m[reqpage]; 10101c7c3c6aSMatthew Dillon 10111c7c3c6aSMatthew Dillon if (mreq->object != object) { 10121c7c3c6aSMatthew Dillon panic("swap_pager_getpages: object mismatch %p/%p", 10131c7c3c6aSMatthew Dillon object, 10141c7c3c6aSMatthew Dillon mreq->object 10151c7c3c6aSMatthew Dillon ); 101626f9a767SRodney W. Grimes } 10171c7c3c6aSMatthew Dillon /* 10181c7c3c6aSMatthew Dillon * Calculate range to retrieve. The pages have already been assigned 10191c7c3c6aSMatthew Dillon * their swapblks. We require a *contiguous* range that falls entirely 10201c7c3c6aSMatthew Dillon * within a single device stripe. If we do not supply it, bad things 10214dcc5c2dSMatthew Dillon * happen. Note that blk, iblk & jblk can be SWAPBLK_NONE, but the 10224dcc5c2dSMatthew Dillon * loops are set up such that the case(s) are handled implicitly. 10234dcc5c2dSMatthew Dillon * 10244dcc5c2dSMatthew Dillon * The swp_*() calls must be made at splvm(). vm_page_free() does 10254dcc5c2dSMatthew Dillon * not need to be, but it will go a little faster if it is. 10261c7c3c6aSMatthew Dillon */ 10274dcc5c2dSMatthew Dillon s = splvm(); 10281c7c3c6aSMatthew Dillon blk = swp_pager_meta_ctl(mreq->object, mreq->pindex, 0); 10291c7c3c6aSMatthew Dillon 10301c7c3c6aSMatthew Dillon for (i = reqpage - 1; i >= 0; --i) { 10311c7c3c6aSMatthew Dillon daddr_t iblk; 10321c7c3c6aSMatthew Dillon 10331c7c3c6aSMatthew Dillon iblk = swp_pager_meta_ctl(m[i]->object, m[i]->pindex, 0); 10341c7c3c6aSMatthew Dillon if (blk != iblk + (reqpage - i)) 103526f9a767SRodney W. Grimes break; 10364dcc5c2dSMatthew Dillon if ((blk ^ iblk) & dmmax_mask) 10374dcc5c2dSMatthew Dillon break; 103826f9a767SRodney W. Grimes } 10391c7c3c6aSMatthew Dillon ++i; 10401c7c3c6aSMatthew Dillon 10411c7c3c6aSMatthew Dillon for (j = reqpage + 1; j < count; ++j) { 10421c7c3c6aSMatthew Dillon daddr_t jblk; 10431c7c3c6aSMatthew Dillon 10441c7c3c6aSMatthew Dillon jblk = swp_pager_meta_ctl(m[j]->object, m[j]->pindex, 0); 10451c7c3c6aSMatthew Dillon if (blk != jblk - (j - reqpage)) 10461c7c3c6aSMatthew Dillon break; 10474dcc5c2dSMatthew Dillon if ((blk ^ jblk) & dmmax_mask) 10484dcc5c2dSMatthew Dillon break; 10491c7c3c6aSMatthew Dillon } 10501c7c3c6aSMatthew Dillon 10511c7c3c6aSMatthew Dillon /* 10521c7c3c6aSMatthew Dillon * free pages outside our collection range. Note: we never free 10531c7c3c6aSMatthew Dillon * mreq, it must remain busy throughout. 10541c7c3c6aSMatthew Dillon */ 1055ab9abe5dSAlan Cox vm_page_lock_queues(); 10561c7c3c6aSMatthew Dillon { 10571c7c3c6aSMatthew Dillon int k; 10581c7c3c6aSMatthew Dillon 10594dcc5c2dSMatthew Dillon for (k = 0; k < i; ++k) 10604dcc5c2dSMatthew Dillon vm_page_free(m[k]); 10614dcc5c2dSMatthew Dillon for (k = j; k < count; ++k) 10621c7c3c6aSMatthew Dillon vm_page_free(m[k]); 10631c7c3c6aSMatthew Dillon } 1064ab9abe5dSAlan Cox vm_page_unlock_queues(); 10654dcc5c2dSMatthew Dillon splx(s); 10664dcc5c2dSMatthew Dillon 10671c7c3c6aSMatthew Dillon 10681c7c3c6aSMatthew Dillon /* 10694dcc5c2dSMatthew Dillon * Return VM_PAGER_FAIL if we have nothing to do. Return mreq 10704dcc5c2dSMatthew Dillon * still busy, but the others unbusied. 10711c7c3c6aSMatthew Dillon */ 10724dcc5c2dSMatthew Dillon if (blk == SWAPBLK_NONE) 107326f9a767SRodney W. Grimes return (VM_PAGER_FAIL); 1074df8bae1dSRodney W. Grimes 107516f62314SDavid Greenman /* 10768630c117SAlan Cox * Getpbuf() can sleep. 10778630c117SAlan Cox */ 10788630c117SAlan Cox VM_OBJECT_UNLOCK(object); 10798630c117SAlan Cox /* 108016f62314SDavid Greenman * Get a swap buffer header to perform the IO 108116f62314SDavid Greenman */ 10821c7c3c6aSMatthew Dillon bp = getpbuf(&nsw_rcount); 108326f9a767SRodney W. Grimes 108416f62314SDavid Greenman /* 108516f62314SDavid Greenman * map our page(s) into kva for input 10861c7c3c6aSMatthew Dillon * 10871c7c3c6aSMatthew Dillon * NOTE: B_PAGING is set by pbgetvp() 108816f62314SDavid Greenman */ 1089d68d828bSAlan Cox pmap_qenter((vm_offset_t)bp->b_data, m + i, j - i); 10901c7c3c6aSMatthew Dillon 109121144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 10921c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 1093fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1094fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 10951c7c3c6aSMatthew Dillon bp->b_blkno = blk - (reqpage - i); 10961c7c3c6aSMatthew Dillon bp->b_bcount = PAGE_SIZE * (j - i); 10971c7c3c6aSMatthew Dillon bp->b_bufsize = PAGE_SIZE * (j - i); 10981c7c3c6aSMatthew Dillon bp->b_pager.pg_reqpage = reqpage - i; 10991c7c3c6aSMatthew Dillon 11008630c117SAlan Cox VM_OBJECT_LOCK(object); 1101b365ea9eSAlan Cox vm_page_lock_queues(); 11021c7c3c6aSMatthew Dillon { 11031c7c3c6aSMatthew Dillon int k; 11041c7c3c6aSMatthew Dillon 11051c7c3c6aSMatthew Dillon for (k = i; k < j; ++k) { 11061c7c3c6aSMatthew Dillon bp->b_pages[k - i] = m[k]; 11071c7c3c6aSMatthew Dillon vm_page_flag_set(m[k], PG_SWAPINPROG); 11081c7c3c6aSMatthew Dillon } 11091c7c3c6aSMatthew Dillon } 1110b365ea9eSAlan Cox vm_page_unlock_queues(); 11118630c117SAlan Cox VM_OBJECT_UNLOCK(object); 11121c7c3c6aSMatthew Dillon bp->b_npages = j - i; 111326f9a767SRodney W. Grimes 11140d94caffSDavid Greenman pbgetvp(swapdev_vp, bp); 1115df8bae1dSRodney W. Grimes 1116976e77fcSDavid Greenman cnt.v_swapin++; 11171c7c3c6aSMatthew Dillon cnt.v_swappgsin += bp->b_npages; 11181c7c3c6aSMatthew Dillon 1119df8bae1dSRodney W. Grimes /* 11201c7c3c6aSMatthew Dillon * We still hold the lock on mreq, and our automatic completion routine 11211c7c3c6aSMatthew Dillon * does not remove it. 1122df8bae1dSRodney W. Grimes */ 1123d68d828bSAlan Cox VM_OBJECT_LOCK(mreq->object); 11241c7c3c6aSMatthew Dillon vm_object_pip_add(mreq->object, bp->b_npages); 1125d68d828bSAlan Cox VM_OBJECT_UNLOCK(mreq->object); 11261c7c3c6aSMatthew Dillon lastpindex = m[j-1]->pindex; 11271c7c3c6aSMatthew Dillon 11281c7c3c6aSMatthew Dillon /* 11291c7c3c6aSMatthew Dillon * perform the I/O. NOTE!!! bp cannot be considered valid after 11301c7c3c6aSMatthew Dillon * this point because we automatically release it on completion. 11311c7c3c6aSMatthew Dillon * Instead, we look at the one page we are interested in which we 11321c7c3c6aSMatthew Dillon * still hold a lock on even through the I/O completion. 11331c7c3c6aSMatthew Dillon * 11341c7c3c6aSMatthew Dillon * The other pages in our m[] array are also released on completion, 11351c7c3c6aSMatthew Dillon * so we cannot assume they are valid anymore either. 11361c7c3c6aSMatthew Dillon * 1137ea3aecf5SPeter Wemm * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY 11381c7c3c6aSMatthew Dillon */ 1139b890cb2cSPeter Wemm BUF_KERNPROC(bp); 114086270230SPoul-Henning Kamp VOP_STRATEGY(bp->b_vp, bp); 114126f9a767SRodney W. Grimes 114226f9a767SRodney W. Grimes /* 11431c7c3c6aSMatthew Dillon * wait for the page we want to complete. PG_SWAPINPROG is always 11441c7c3c6aSMatthew Dillon * cleared on completion. If an I/O error occurs, SWAPBLK_NONE 11451c7c3c6aSMatthew Dillon * is set in the meta-data. 114626f9a767SRodney W. Grimes */ 11471c7c3c6aSMatthew Dillon s = splvm(); 1148b365ea9eSAlan Cox vm_page_lock_queues(); 11491c7c3c6aSMatthew Dillon while ((mreq->flags & PG_SWAPINPROG) != 0) { 11501c7c3c6aSMatthew Dillon vm_page_flag_set(mreq, PG_WANTED | PG_REFERENCED); 11511c7c3c6aSMatthew Dillon cnt.v_intrans++; 1152b365ea9eSAlan Cox if (msleep(mreq, &vm_page_queue_mtx, PSWP, "swread", hz*20)) { 1153ac1e407bSBruce Evans printf( 11541c7c3c6aSMatthew Dillon "swap_pager: indefinite wait buffer: device:" 1155af647ddeSBruce Evans " %s, blkno: %ld, size: %ld\n", 1156af647ddeSBruce Evans devtoname(bp->b_dev), (long)bp->b_blkno, 1157af647ddeSBruce Evans bp->b_bcount 11581c7c3c6aSMatthew Dillon ); 11591c7c3c6aSMatthew Dillon } 11601b119d9dSDavid Greenman } 1161b365ea9eSAlan Cox vm_page_unlock_queues(); 1162df8bae1dSRodney W. Grimes splx(s); 116326f9a767SRodney W. Grimes 11648630c117SAlan Cox VM_OBJECT_LOCK(mreq->object); 116526f9a767SRodney W. Grimes /* 1166a1287949SEivind Eklund * mreq is left busied after completion, but all the other pages 11671c7c3c6aSMatthew Dillon * are freed. If we had an unrecoverable read error the page will 11681c7c3c6aSMatthew Dillon * not be valid. 116926f9a767SRodney W. Grimes */ 11701c7c3c6aSMatthew Dillon if (mreq->valid != VM_PAGE_BITS_ALL) { 11711c7c3c6aSMatthew Dillon return (VM_PAGER_ERROR); 117226f9a767SRodney W. Grimes } else { 11731c7c3c6aSMatthew Dillon return (VM_PAGER_OK); 117426f9a767SRodney W. Grimes } 11751c7c3c6aSMatthew Dillon 11761c7c3c6aSMatthew Dillon /* 11771c7c3c6aSMatthew Dillon * A final note: in a low swap situation, we cannot deallocate swap 11781c7c3c6aSMatthew Dillon * and mark a page dirty here because the caller is likely to mark 11791c7c3c6aSMatthew Dillon * the page clean when we return, causing the page to possibly revert 11801c7c3c6aSMatthew Dillon * to all-zero's later. 11811c7c3c6aSMatthew Dillon */ 1182df8bae1dSRodney W. Grimes } 1183df8bae1dSRodney W. Grimes 11841c7c3c6aSMatthew Dillon /* 11851c7c3c6aSMatthew Dillon * swap_pager_putpages: 11861c7c3c6aSMatthew Dillon * 11871c7c3c6aSMatthew Dillon * Assign swap (if necessary) and initiate I/O on the specified pages. 11881c7c3c6aSMatthew Dillon * 11891c7c3c6aSMatthew Dillon * We support both OBJT_DEFAULT and OBJT_SWAP objects. DEFAULT objects 11901c7c3c6aSMatthew Dillon * are automatically converted to SWAP objects. 11911c7c3c6aSMatthew Dillon * 1192ea3aecf5SPeter Wemm * In a low memory situation we may block in VOP_STRATEGY(), but the new 11931c7c3c6aSMatthew Dillon * vm_page reservation system coupled with properly written VFS devices 11941c7c3c6aSMatthew Dillon * should ensure that no low-memory deadlock occurs. This is an area 11951c7c3c6aSMatthew Dillon * which needs work. 11961c7c3c6aSMatthew Dillon * 11971c7c3c6aSMatthew Dillon * The parent has N vm_object_pip_add() references prior to 11981c7c3c6aSMatthew Dillon * calling us and will remove references for rtvals[] that are 11991c7c3c6aSMatthew Dillon * not set to VM_PAGER_PEND. We need to remove the rest on I/O 12001c7c3c6aSMatthew Dillon * completion. 12011c7c3c6aSMatthew Dillon * 12021c7c3c6aSMatthew Dillon * The parent has soft-busy'd the pages it passes us and will unbusy 12031c7c3c6aSMatthew Dillon * those whos rtvals[] entry is not set to VM_PAGER_PEND on return. 12041c7c3c6aSMatthew Dillon * We need to unbusy the rest on I/O completion. 12051c7c3c6aSMatthew Dillon */ 1206e4542174SMatthew Dillon void 120724a1cce3SDavid Greenman swap_pager_putpages(object, m, count, sync, rtvals) 120824a1cce3SDavid Greenman vm_object_t object; 120926f9a767SRodney W. Grimes vm_page_t *m; 121026f9a767SRodney W. Grimes int count; 121124a1cce3SDavid Greenman boolean_t sync; 121226f9a767SRodney W. Grimes int *rtvals; 1213df8bae1dSRodney W. Grimes { 12141c7c3c6aSMatthew Dillon int i; 12151c7c3c6aSMatthew Dillon int n = 0; 1216df8bae1dSRodney W. Grimes 12170cddd8f0SMatthew Dillon GIANT_REQUIRED; 12181c7c3c6aSMatthew Dillon if (count && m[0]->object != object) { 12191c7c3c6aSMatthew Dillon panic("swap_pager_getpages: object mismatch %p/%p", 12201c7c3c6aSMatthew Dillon object, 12211c7c3c6aSMatthew Dillon m[0]->object 12221c7c3c6aSMatthew Dillon ); 12231c7c3c6aSMatthew Dillon } 12241c7c3c6aSMatthew Dillon /* 12251c7c3c6aSMatthew Dillon * Step 1 12261c7c3c6aSMatthew Dillon * 12271c7c3c6aSMatthew Dillon * Turn object into OBJT_SWAP 12281c7c3c6aSMatthew Dillon * check for bogus sysops 12291c7c3c6aSMatthew Dillon * force sync if not pageout process 12301c7c3c6aSMatthew Dillon */ 12314dcc5c2dSMatthew Dillon if (object->type != OBJT_SWAP) 12324dcc5c2dSMatthew Dillon swp_pager_meta_build(object, 0, SWAPBLK_NONE); 1233e47ed70bSJohn Dyson 1234e47ed70bSJohn Dyson if (curproc != pageproc) 1235e47ed70bSJohn Dyson sync = TRUE; 123626f9a767SRodney W. Grimes 12371c7c3c6aSMatthew Dillon /* 12381c7c3c6aSMatthew Dillon * Step 2 12391c7c3c6aSMatthew Dillon * 1240ad3cce20SMatthew Dillon * Update nsw parameters from swap_async_max sysctl values. 1241ad3cce20SMatthew Dillon * Do not let the sysop crash the machine with bogus numbers. 1242327f4e83SMatthew Dillon */ 12436d541bf1SJohn Baldwin mtx_lock(&pbuf_mtx); 1244327f4e83SMatthew Dillon if (swap_async_max != nsw_wcount_async_max) { 1245327f4e83SMatthew Dillon int n; 1246327f4e83SMatthew Dillon int s; 1247327f4e83SMatthew Dillon 1248327f4e83SMatthew Dillon /* 1249327f4e83SMatthew Dillon * limit range 1250327f4e83SMatthew Dillon */ 1251327f4e83SMatthew Dillon if ((n = swap_async_max) > nswbuf / 2) 1252327f4e83SMatthew Dillon n = nswbuf / 2; 1253327f4e83SMatthew Dillon if (n < 1) 1254327f4e83SMatthew Dillon n = 1; 1255327f4e83SMatthew Dillon swap_async_max = n; 1256327f4e83SMatthew Dillon 1257327f4e83SMatthew Dillon /* 1258327f4e83SMatthew Dillon * Adjust difference ( if possible ). If the current async 1259327f4e83SMatthew Dillon * count is too low, we may not be able to make the adjustment 1260327f4e83SMatthew Dillon * at this time. 1261327f4e83SMatthew Dillon */ 1262327f4e83SMatthew Dillon s = splvm(); 1263327f4e83SMatthew Dillon n -= nsw_wcount_async_max; 1264327f4e83SMatthew Dillon if (nsw_wcount_async + n >= 0) { 1265327f4e83SMatthew Dillon nsw_wcount_async += n; 1266327f4e83SMatthew Dillon nsw_wcount_async_max += n; 1267327f4e83SMatthew Dillon wakeup(&nsw_wcount_async); 1268327f4e83SMatthew Dillon } 1269327f4e83SMatthew Dillon splx(s); 1270327f4e83SMatthew Dillon } 12716d541bf1SJohn Baldwin mtx_unlock(&pbuf_mtx); 1272327f4e83SMatthew Dillon 1273327f4e83SMatthew Dillon /* 1274327f4e83SMatthew Dillon * Step 3 1275327f4e83SMatthew Dillon * 12761c7c3c6aSMatthew Dillon * Assign swap blocks and issue I/O. We reallocate swap on the fly. 12771c7c3c6aSMatthew Dillon * The page is left dirty until the pageout operation completes 12781c7c3c6aSMatthew Dillon * successfully. 12791c7c3c6aSMatthew Dillon */ 12801c7c3c6aSMatthew Dillon for (i = 0; i < count; i += n) { 12811c7c3c6aSMatthew Dillon int s; 12821c7c3c6aSMatthew Dillon int j; 12831c7c3c6aSMatthew Dillon struct buf *bp; 1284a316d390SJohn Dyson daddr_t blk; 128526f9a767SRodney W. Grimes 1286df8bae1dSRodney W. Grimes /* 12871c7c3c6aSMatthew Dillon * Maximum I/O size is limited by a number of factors. 1288df8bae1dSRodney W. Grimes */ 12891c7c3c6aSMatthew Dillon n = min(BLIST_MAX_ALLOC, count - i); 1290327f4e83SMatthew Dillon n = min(n, nsw_cluster_max); 12911c7c3c6aSMatthew Dillon 12924dcc5c2dSMatthew Dillon s = splvm(); 12934dcc5c2dSMatthew Dillon 129426f9a767SRodney W. Grimes /* 12951c7c3c6aSMatthew Dillon * Get biggest block of swap we can. If we fail, fall 12961c7c3c6aSMatthew Dillon * back and try to allocate a smaller block. Don't go 12971c7c3c6aSMatthew Dillon * overboard trying to allocate space if it would overly 12981c7c3c6aSMatthew Dillon * fragment swap. 129926f9a767SRodney W. Grimes */ 13001c7c3c6aSMatthew Dillon while ( 13011c7c3c6aSMatthew Dillon (blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE && 13021c7c3c6aSMatthew Dillon n > 4 13031c7c3c6aSMatthew Dillon ) { 13041c7c3c6aSMatthew Dillon n >>= 1; 130526f9a767SRodney W. Grimes } 13061c7c3c6aSMatthew Dillon if (blk == SWAPBLK_NONE) { 13074dcc5c2dSMatthew Dillon for (j = 0; j < n; ++j) 13081c7c3c6aSMatthew Dillon rtvals[i+j] = VM_PAGER_FAIL; 13094dcc5c2dSMatthew Dillon splx(s); 13101c7c3c6aSMatthew Dillon continue; 131126f9a767SRodney W. Grimes } 131226f9a767SRodney W. Grimes 131326f9a767SRodney W. Grimes /* 13144dcc5c2dSMatthew Dillon * The I/O we are constructing cannot cross a physical 13154dcc5c2dSMatthew Dillon * disk boundry in the swap stripe. Note: we are still 13164dcc5c2dSMatthew Dillon * at splvm(). 131726f9a767SRodney W. Grimes */ 13181c7c3c6aSMatthew Dillon if ((blk ^ (blk + n)) & dmmax_mask) { 13191c7c3c6aSMatthew Dillon j = ((blk + dmmax) & dmmax_mask) - blk; 13201c7c3c6aSMatthew Dillon swp_pager_freeswapspace(blk + j, n - j); 13211c7c3c6aSMatthew Dillon n = j; 1322e47ed70bSJohn Dyson } 132326f9a767SRodney W. Grimes 132426f9a767SRodney W. Grimes /* 13251c7c3c6aSMatthew Dillon * All I/O parameters have been satisfied, build the I/O 13261c7c3c6aSMatthew Dillon * request and assign the swap space. 13271c7c3c6aSMatthew Dillon * 13281c7c3c6aSMatthew Dillon * NOTE: B_PAGING is set by pbgetvp() 132926f9a767SRodney W. Grimes */ 1330327f4e83SMatthew Dillon if (sync == TRUE) { 1331327f4e83SMatthew Dillon bp = getpbuf(&nsw_wcount_sync); 1332327f4e83SMatthew Dillon } else { 1333327f4e83SMatthew Dillon bp = getpbuf(&nsw_wcount_async); 133421144e3bSPoul-Henning Kamp bp->b_flags = B_ASYNC; 1335327f4e83SMatthew Dillon } 1336912e4ae9SPoul-Henning Kamp bp->b_iocmd = BIO_WRITE; 13371c7c3c6aSMatthew Dillon bp->b_spc = NULL; /* not used, but NULL-out anyway */ 133826f9a767SRodney W. Grimes 13391c7c3c6aSMatthew Dillon pmap_qenter((vm_offset_t)bp->b_data, &m[i], n); 13401c7c3c6aSMatthew Dillon 1341fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1342fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 13431c7c3c6aSMatthew Dillon bp->b_bcount = PAGE_SIZE * n; 13441c7c3c6aSMatthew Dillon bp->b_bufsize = PAGE_SIZE * n; 13451c7c3c6aSMatthew Dillon bp->b_blkno = blk; 1346e47ed70bSJohn Dyson 1347a5296b05SJulian Elischer pbgetvp(swapdev_vp, bp); 1348a5296b05SJulian Elischer 13491c7c3c6aSMatthew Dillon for (j = 0; j < n; ++j) { 13501c7c3c6aSMatthew Dillon vm_page_t mreq = m[i+j]; 13511c7c3c6aSMatthew Dillon 13521c7c3c6aSMatthew Dillon swp_pager_meta_build( 13531c7c3c6aSMatthew Dillon mreq->object, 13541c7c3c6aSMatthew Dillon mreq->pindex, 13554dcc5c2dSMatthew Dillon blk + j 13561c7c3c6aSMatthew Dillon ); 13577dbf82dcSMatthew Dillon vm_page_dirty(mreq); 13581c7c3c6aSMatthew Dillon rtvals[i+j] = VM_PAGER_OK; 13591c7c3c6aSMatthew Dillon 1360b365ea9eSAlan Cox vm_page_lock_queues(); 13611c7c3c6aSMatthew Dillon vm_page_flag_set(mreq, PG_SWAPINPROG); 1362b365ea9eSAlan Cox vm_page_unlock_queues(); 13631c7c3c6aSMatthew Dillon bp->b_pages[j] = mreq; 13641c7c3c6aSMatthew Dillon } 13651c7c3c6aSMatthew Dillon bp->b_npages = n; 1366a5296b05SJulian Elischer /* 1367a5296b05SJulian Elischer * Must set dirty range for NFS to work. 1368a5296b05SJulian Elischer */ 1369a5296b05SJulian Elischer bp->b_dirtyoff = 0; 1370a5296b05SJulian Elischer bp->b_dirtyend = bp->b_bcount; 13711c7c3c6aSMatthew Dillon 13721c7c3c6aSMatthew Dillon cnt.v_swapout++; 13731c7c3c6aSMatthew Dillon cnt.v_swappgsout += bp->b_npages; 13746a2eac8aSJeff Roberson VI_LOCK(swapdev_vp); 137526f9a767SRodney W. Grimes swapdev_vp->v_numoutput++; 13766a2eac8aSJeff Roberson VI_UNLOCK(swapdev_vp); 137726f9a767SRodney W. Grimes 13784dcc5c2dSMatthew Dillon splx(s); 13794dcc5c2dSMatthew Dillon 138026f9a767SRodney W. Grimes /* 13811c7c3c6aSMatthew Dillon * asynchronous 13821c7c3c6aSMatthew Dillon * 1383ea3aecf5SPeter Wemm * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY 138426f9a767SRodney W. Grimes */ 13851c7c3c6aSMatthew Dillon if (sync == FALSE) { 13861c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 138767812eacSKirk McKusick BUF_KERNPROC(bp); 138886270230SPoul-Henning Kamp VOP_STRATEGY(bp->b_vp, bp); 13891c7c3c6aSMatthew Dillon 13901c7c3c6aSMatthew Dillon for (j = 0; j < n; ++j) 13911c7c3c6aSMatthew Dillon rtvals[i+j] = VM_PAGER_PEND; 139223955314SAlfred Perlstein /* restart outter loop */ 13931c7c3c6aSMatthew Dillon continue; 139426f9a767SRodney W. Grimes } 1395e47ed70bSJohn Dyson 139626f9a767SRodney W. Grimes /* 13971c7c3c6aSMatthew Dillon * synchronous 13981c7c3c6aSMatthew Dillon * 1399ea3aecf5SPeter Wemm * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY 14001c7c3c6aSMatthew Dillon */ 14011c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_sync_iodone; 140286270230SPoul-Henning Kamp VOP_STRATEGY(bp->b_vp, bp); 14031c7c3c6aSMatthew Dillon 14041c7c3c6aSMatthew Dillon /* 14051c7c3c6aSMatthew Dillon * Wait for the sync I/O to complete, then update rtvals. 14061c7c3c6aSMatthew Dillon * We just set the rtvals[] to VM_PAGER_PEND so we can call 14071c7c3c6aSMatthew Dillon * our async completion routine at the end, thus avoiding a 14081c7c3c6aSMatthew Dillon * double-free. 140926f9a767SRodney W. Grimes */ 14104dcc5c2dSMatthew Dillon s = splbio(); 141126f9a767SRodney W. Grimes while ((bp->b_flags & B_DONE) == 0) { 141224a1cce3SDavid Greenman tsleep(bp, PVM, "swwrt", 0); 141326f9a767SRodney W. Grimes } 14141c7c3c6aSMatthew Dillon for (j = 0; j < n; ++j) 14151c7c3c6aSMatthew Dillon rtvals[i+j] = VM_PAGER_PEND; 14161c7c3c6aSMatthew Dillon /* 14171c7c3c6aSMatthew Dillon * Now that we are through with the bp, we can call the 14181c7c3c6aSMatthew Dillon * normal async completion, which frees everything up. 14191c7c3c6aSMatthew Dillon */ 14201c7c3c6aSMatthew Dillon swp_pager_async_iodone(bp); 142126f9a767SRodney W. Grimes splx(s); 14221c7c3c6aSMatthew Dillon } 14231c7c3c6aSMatthew Dillon } 14241c7c3c6aSMatthew Dillon 14251c7c3c6aSMatthew Dillon /* 14261c7c3c6aSMatthew Dillon * swap_pager_sync_iodone: 14271c7c3c6aSMatthew Dillon * 14281c7c3c6aSMatthew Dillon * Completion routine for synchronous reads and writes from/to swap. 14291c7c3c6aSMatthew Dillon * We just mark the bp is complete and wake up anyone waiting on it. 14301c7c3c6aSMatthew Dillon * 14314dcc5c2dSMatthew Dillon * This routine may not block. This routine is called at splbio() or better. 14321c7c3c6aSMatthew Dillon */ 14331c7c3c6aSMatthew Dillon static void 14341c7c3c6aSMatthew Dillon swp_pager_sync_iodone(bp) 14351c7c3c6aSMatthew Dillon struct buf *bp; 14361c7c3c6aSMatthew Dillon { 14371c7c3c6aSMatthew Dillon bp->b_flags |= B_DONE; 14381c7c3c6aSMatthew Dillon bp->b_flags &= ~B_ASYNC; 14391c7c3c6aSMatthew Dillon wakeup(bp); 14401c7c3c6aSMatthew Dillon } 14411c7c3c6aSMatthew Dillon 14421c7c3c6aSMatthew Dillon /* 14431c7c3c6aSMatthew Dillon * swp_pager_async_iodone: 14441c7c3c6aSMatthew Dillon * 14451c7c3c6aSMatthew Dillon * Completion routine for asynchronous reads and writes from/to swap. 14461c7c3c6aSMatthew Dillon * Also called manually by synchronous code to finish up a bp. 14471c7c3c6aSMatthew Dillon * 14481c7c3c6aSMatthew Dillon * For READ operations, the pages are PG_BUSY'd. For WRITE operations, 14491c7c3c6aSMatthew Dillon * the pages are vm_page_t->busy'd. For READ operations, we PG_BUSY 14501c7c3c6aSMatthew Dillon * unbusy all pages except the 'main' request page. For WRITE 14511c7c3c6aSMatthew Dillon * operations, we vm_page_t->busy'd unbusy all pages ( we can do this 14521c7c3c6aSMatthew Dillon * because we marked them all VM_PAGER_PEND on return from putpages ). 14531c7c3c6aSMatthew Dillon * 14541c7c3c6aSMatthew Dillon * This routine may not block. 14554dcc5c2dSMatthew Dillon * This routine is called at splbio() or better 14564dcc5c2dSMatthew Dillon * 14574dcc5c2dSMatthew Dillon * We up ourselves to splvm() as required for various vm_page related 14584dcc5c2dSMatthew Dillon * calls. 14591c7c3c6aSMatthew Dillon */ 14601c7c3c6aSMatthew Dillon static void 14611c7c3c6aSMatthew Dillon swp_pager_async_iodone(bp) 146254d92145SMatthew Dillon struct buf *bp; 14631c7c3c6aSMatthew Dillon { 14641c7c3c6aSMatthew Dillon int s; 14651c7c3c6aSMatthew Dillon int i; 14661c7c3c6aSMatthew Dillon vm_object_t object = NULL; 14671c7c3c6aSMatthew Dillon 14680cddd8f0SMatthew Dillon GIANT_REQUIRED; 14691c7c3c6aSMatthew Dillon bp->b_flags |= B_DONE; 14701c7c3c6aSMatthew Dillon 14711c7c3c6aSMatthew Dillon /* 14721c7c3c6aSMatthew Dillon * report error 14731c7c3c6aSMatthew Dillon */ 1474c244d2deSPoul-Henning Kamp if (bp->b_ioflags & BIO_ERROR) { 14751c7c3c6aSMatthew Dillon printf( 14761c7c3c6aSMatthew Dillon "swap_pager: I/O error - %s failed; blkno %ld," 14771c7c3c6aSMatthew Dillon "size %ld, error %d\n", 147821144e3bSPoul-Henning Kamp ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"), 14791c7c3c6aSMatthew Dillon (long)bp->b_blkno, 14801c7c3c6aSMatthew Dillon (long)bp->b_bcount, 14811c7c3c6aSMatthew Dillon bp->b_error 14821c7c3c6aSMatthew Dillon ); 14831c7c3c6aSMatthew Dillon } 14841c7c3c6aSMatthew Dillon 14851c7c3c6aSMatthew Dillon /* 14864dcc5c2dSMatthew Dillon * set object, raise to splvm(). 14871c7c3c6aSMatthew Dillon */ 14884dcc5c2dSMatthew Dillon s = splvm(); 148926f9a767SRodney W. Grimes 149026f9a767SRodney W. Grimes /* 149126f9a767SRodney W. Grimes * remove the mapping for kernel virtual 149226f9a767SRodney W. Grimes */ 14931c7c3c6aSMatthew Dillon pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages); 149426f9a767SRodney W. Grimes 149533a609ecSAlan Cox if (bp->b_npages) { 149633a609ecSAlan Cox object = bp->b_pages[0]->object; 149733a609ecSAlan Cox VM_OBJECT_LOCK(object); 149833a609ecSAlan Cox } 149940eab1e9SAlan Cox vm_page_lock_queues(); 150026f9a767SRodney W. Grimes /* 15011c7c3c6aSMatthew Dillon * cleanup pages. If an error occurs writing to swap, we are in 15021c7c3c6aSMatthew Dillon * very serious trouble. If it happens to be a disk error, though, 15031c7c3c6aSMatthew Dillon * we may be able to recover by reassigning the swap later on. So 15041c7c3c6aSMatthew Dillon * in this case we remove the m->swapblk assignment for the page 15051c7c3c6aSMatthew Dillon * but do not free it in the rlist. The errornous block(s) are thus 15061c7c3c6aSMatthew Dillon * never reallocated as swap. Redirty the page and continue. 150726f9a767SRodney W. Grimes */ 15081c7c3c6aSMatthew Dillon for (i = 0; i < bp->b_npages; ++i) { 15091c7c3c6aSMatthew Dillon vm_page_t m = bp->b_pages[i]; 1510e47ed70bSJohn Dyson 15111c7c3c6aSMatthew Dillon vm_page_flag_clear(m, PG_SWAPINPROG); 1512e47ed70bSJohn Dyson 1513c244d2deSPoul-Henning Kamp if (bp->b_ioflags & BIO_ERROR) { 1514ffc82b0aSJohn Dyson /* 15151c7c3c6aSMatthew Dillon * If an error occurs I'd love to throw the swapblk 15161c7c3c6aSMatthew Dillon * away without freeing it back to swapspace, so it 15171c7c3c6aSMatthew Dillon * can never be used again. But I can't from an 15181c7c3c6aSMatthew Dillon * interrupt. 1519ffc82b0aSJohn Dyson */ 152021144e3bSPoul-Henning Kamp if (bp->b_iocmd == BIO_READ) { 15211c7c3c6aSMatthew Dillon /* 15221c7c3c6aSMatthew Dillon * When reading, reqpage needs to stay 15231c7c3c6aSMatthew Dillon * locked for the parent, but all other 15241c7c3c6aSMatthew Dillon * pages can be freed. We still want to 15251c7c3c6aSMatthew Dillon * wakeup the parent waiting on the page, 15261c7c3c6aSMatthew Dillon * though. ( also: pg_reqpage can be -1 and 15271c7c3c6aSMatthew Dillon * not match anything ). 15281c7c3c6aSMatthew Dillon * 15291c7c3c6aSMatthew Dillon * We have to wake specifically requested pages 15301c7c3c6aSMatthew Dillon * up too because we cleared PG_SWAPINPROG and 15311c7c3c6aSMatthew Dillon * someone may be waiting for that. 15321c7c3c6aSMatthew Dillon * 15331c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably 1534956f3135SPhilippe Charnier * be overridden by the original caller of 15351c7c3c6aSMatthew Dillon * getpages so don't play cute tricks here. 15361c7c3c6aSMatthew Dillon * 1537279d7226SMatthew Dillon * XXX IT IS NOT LEGAL TO FREE THE PAGE HERE 1538279d7226SMatthew Dillon * AS THIS MESSES WITH object->memq, and it is 1539279d7226SMatthew Dillon * not legal to mess with object->memq from an 1540279d7226SMatthew Dillon * interrupt. 15411c7c3c6aSMatthew Dillon */ 15421c7c3c6aSMatthew Dillon m->valid = 0; 15431c7c3c6aSMatthew Dillon vm_page_flag_clear(m, PG_ZERO); 15441c7c3c6aSMatthew Dillon if (i != bp->b_pager.pg_reqpage) 15451c7c3c6aSMatthew Dillon vm_page_free(m); 15461c7c3c6aSMatthew Dillon else 15471c7c3c6aSMatthew Dillon vm_page_flash(m); 15481c7c3c6aSMatthew Dillon /* 15491c7c3c6aSMatthew Dillon * If i == bp->b_pager.pg_reqpage, do not wake 15501c7c3c6aSMatthew Dillon * the page up. The caller needs to. 15511c7c3c6aSMatthew Dillon */ 15521c7c3c6aSMatthew Dillon } else { 15531c7c3c6aSMatthew Dillon /* 15541c7c3c6aSMatthew Dillon * If a write error occurs, reactivate page 15551c7c3c6aSMatthew Dillon * so it doesn't clog the inactive list, 15561c7c3c6aSMatthew Dillon * then finish the I/O. 15571c7c3c6aSMatthew Dillon */ 15587dbf82dcSMatthew Dillon vm_page_dirty(m); 15591c7c3c6aSMatthew Dillon vm_page_activate(m); 15601c7c3c6aSMatthew Dillon vm_page_io_finish(m); 15611c7c3c6aSMatthew Dillon } 156221144e3bSPoul-Henning Kamp } else if (bp->b_iocmd == BIO_READ) { 15631c7c3c6aSMatthew Dillon /* 15641c7c3c6aSMatthew Dillon * For read success, clear dirty bits. Nobody should 15651c7c3c6aSMatthew Dillon * have this page mapped but don't take any chances, 15661c7c3c6aSMatthew Dillon * make sure the pmap modify bits are also cleared. 15671c7c3c6aSMatthew Dillon * 15681c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably be 1569956f3135SPhilippe Charnier * overridden by the original caller of getpages so 15701c7c3c6aSMatthew Dillon * we cannot set them in order to free the underlying 15711c7c3c6aSMatthew Dillon * swap in a low-swap situation. I don't think we'd 15721c7c3c6aSMatthew Dillon * want to do that anyway, but it was an optimization 15731c7c3c6aSMatthew Dillon * that existed in the old swapper for a time before 15741c7c3c6aSMatthew Dillon * it got ripped out due to precisely this problem. 15751c7c3c6aSMatthew Dillon * 15761c7c3c6aSMatthew Dillon * clear PG_ZERO in page. 15771c7c3c6aSMatthew Dillon * 15781c7c3c6aSMatthew Dillon * If not the requested page then deactivate it. 15791c7c3c6aSMatthew Dillon * 15801c7c3c6aSMatthew Dillon * Note that the requested page, reqpage, is left 15811c7c3c6aSMatthew Dillon * busied, but we still have to wake it up. The 15821c7c3c6aSMatthew Dillon * other pages are released (unbusied) by 15831c7c3c6aSMatthew Dillon * vm_page_wakeup(). We do not set reqpage's 15841c7c3c6aSMatthew Dillon * valid bits here, it is up to the caller. 15851c7c3c6aSMatthew Dillon */ 15860385347cSPeter Wemm pmap_clear_modify(m); 15871c7c3c6aSMatthew Dillon m->valid = VM_PAGE_BITS_ALL; 15882c28a105SAlan Cox vm_page_undirty(m); 15891c7c3c6aSMatthew Dillon vm_page_flag_clear(m, PG_ZERO); 15901c7c3c6aSMatthew Dillon 15911c7c3c6aSMatthew Dillon /* 15921c7c3c6aSMatthew Dillon * We have to wake specifically requested pages 15931c7c3c6aSMatthew Dillon * up too because we cleared PG_SWAPINPROG and 15941c7c3c6aSMatthew Dillon * could be waiting for it in getpages. However, 15951c7c3c6aSMatthew Dillon * be sure to not unbusy getpages specifically 15961c7c3c6aSMatthew Dillon * requested page - getpages expects it to be 15971c7c3c6aSMatthew Dillon * left busy. 15981c7c3c6aSMatthew Dillon */ 15991c7c3c6aSMatthew Dillon if (i != bp->b_pager.pg_reqpage) { 16001c7c3c6aSMatthew Dillon vm_page_deactivate(m); 16011c7c3c6aSMatthew Dillon vm_page_wakeup(m); 16021c7c3c6aSMatthew Dillon } else { 16031c7c3c6aSMatthew Dillon vm_page_flash(m); 16041c7c3c6aSMatthew Dillon } 16051c7c3c6aSMatthew Dillon } else { 16061c7c3c6aSMatthew Dillon /* 16071c7c3c6aSMatthew Dillon * For write success, clear the modify and dirty 16081c7c3c6aSMatthew Dillon * status, then finish the I/O ( which decrements the 16091c7c3c6aSMatthew Dillon * busy count and possibly wakes waiter's up ). 16101c7c3c6aSMatthew Dillon */ 16110385347cSPeter Wemm pmap_clear_modify(m); 1612c52e7044SAlan Cox vm_page_undirty(m); 16131c7c3c6aSMatthew Dillon vm_page_io_finish(m); 1614936524aaSMatthew Dillon if (!vm_page_count_severe() || !vm_page_try_to_cache(m)) 1615a12cc0e4SAlan Cox pmap_page_protect(m, VM_PROT_READ); 1616ffc82b0aSJohn Dyson } 1617df8bae1dSRodney W. Grimes } 161840eab1e9SAlan Cox vm_page_unlock_queues(); 161926f9a767SRodney W. Grimes 16201c7c3c6aSMatthew Dillon /* 16211c7c3c6aSMatthew Dillon * adjust pip. NOTE: the original parent may still have its own 16221c7c3c6aSMatthew Dillon * pip refs on the object. 16231c7c3c6aSMatthew Dillon */ 16240d420ad3SAlan Cox if (object != NULL) { 16251c7c3c6aSMatthew Dillon vm_object_pip_wakeupn(object, bp->b_npages); 16260d420ad3SAlan Cox VM_OBJECT_UNLOCK(object); 16270d420ad3SAlan Cox } 162826f9a767SRodney W. Grimes 16291c7c3c6aSMatthew Dillon /* 16301c7c3c6aSMatthew Dillon * release the physical I/O buffer 16311c7c3c6aSMatthew Dillon */ 1632327f4e83SMatthew Dillon relpbuf( 1633327f4e83SMatthew Dillon bp, 163421144e3bSPoul-Henning Kamp ((bp->b_iocmd == BIO_READ) ? &nsw_rcount : 1635327f4e83SMatthew Dillon ((bp->b_flags & B_ASYNC) ? 1636327f4e83SMatthew Dillon &nsw_wcount_async : 1637327f4e83SMatthew Dillon &nsw_wcount_sync 1638327f4e83SMatthew Dillon ) 1639327f4e83SMatthew Dillon ) 1640327f4e83SMatthew Dillon ); 164126f9a767SRodney W. Grimes splx(s); 164226f9a767SRodney W. Grimes } 16431c7c3c6aSMatthew Dillon 164492da00bbSMatthew Dillon /* 164592da00bbSMatthew Dillon * swap_pager_isswapped: 164692da00bbSMatthew Dillon * 164792da00bbSMatthew Dillon * Return 1 if at least one page in the given object is paged 164892da00bbSMatthew Dillon * out to the given swap device. 164992da00bbSMatthew Dillon * 165092da00bbSMatthew Dillon * This routine may not block. 165192da00bbSMatthew Dillon */ 165292da00bbSMatthew Dillon int swap_pager_isswapped(vm_object_t object, int devidx) { 165392da00bbSMatthew Dillon daddr_t index = 0; 165492da00bbSMatthew Dillon int bcount; 165592da00bbSMatthew Dillon int i; 165692da00bbSMatthew Dillon 165717cd3642SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 165892da00bbSMatthew Dillon for (bcount = 0; bcount < object->un_pager.swp.swp_bcount; bcount++) { 165992da00bbSMatthew Dillon struct swblock *swap; 166092da00bbSMatthew Dillon 166192da00bbSMatthew Dillon if ((swap = *swp_pager_hash(object, index)) != NULL) { 166292da00bbSMatthew Dillon for (i = 0; i < SWAP_META_PAGES; ++i) { 166392da00bbSMatthew Dillon daddr_t v = swap->swb_pages[i]; 166492da00bbSMatthew Dillon if (v != SWAPBLK_NONE && 166592da00bbSMatthew Dillon BLK2DEVIDX(v) == devidx) 166692da00bbSMatthew Dillon return 1; 166792da00bbSMatthew Dillon } 166892da00bbSMatthew Dillon } 166992da00bbSMatthew Dillon 167092da00bbSMatthew Dillon index += SWAP_META_PAGES; 167192da00bbSMatthew Dillon if (index > 0x20000000) 167292da00bbSMatthew Dillon panic("swap_pager_isswapped: failed to locate all swap meta blocks"); 167392da00bbSMatthew Dillon } 167492da00bbSMatthew Dillon return 0; 167592da00bbSMatthew Dillon } 167692da00bbSMatthew Dillon 167792da00bbSMatthew Dillon /* 167892da00bbSMatthew Dillon * SWP_PAGER_FORCE_PAGEIN() - force a swap block to be paged in 167992da00bbSMatthew Dillon * 168092da00bbSMatthew Dillon * This routine dissociates the page at the given index within a 168192da00bbSMatthew Dillon * swap block from its backing store, paging it in if necessary. 168292da00bbSMatthew Dillon * If the page is paged in, it is placed in the inactive queue, 168392da00bbSMatthew Dillon * since it had its backing store ripped out from under it. 168492da00bbSMatthew Dillon * We also attempt to swap in all other pages in the swap block, 168592da00bbSMatthew Dillon * we only guarantee that the one at the specified index is 168692da00bbSMatthew Dillon * paged in. 168792da00bbSMatthew Dillon * 168892da00bbSMatthew Dillon * XXX - The code to page the whole block in doesn't work, so we 168992da00bbSMatthew Dillon * revert to the one-by-one behavior for now. Sigh. 169092da00bbSMatthew Dillon */ 169192da00bbSMatthew Dillon static __inline void 169292da00bbSMatthew Dillon swp_pager_force_pagein(struct swblock *swap, int idx) 169392da00bbSMatthew Dillon { 169492da00bbSMatthew Dillon vm_object_t object; 169592da00bbSMatthew Dillon vm_page_t m; 169692da00bbSMatthew Dillon vm_pindex_t pindex; 169792da00bbSMatthew Dillon 169892da00bbSMatthew Dillon object = swap->swb_object; 169992da00bbSMatthew Dillon pindex = swap->swb_index; 170092da00bbSMatthew Dillon 1701d22bc710SAlan Cox VM_OBJECT_LOCK(object); 170292da00bbSMatthew Dillon vm_object_pip_add(object, 1); 1703d22bc710SAlan Cox VM_OBJECT_UNLOCK(object); 170492da00bbSMatthew Dillon m = vm_page_grab(object, pindex + idx, VM_ALLOC_NORMAL|VM_ALLOC_RETRY); 170592da00bbSMatthew Dillon if (m->valid == VM_PAGE_BITS_ALL) { 17060fa05eaeSAlan Cox VM_OBJECT_LOCK(object); 170792da00bbSMatthew Dillon vm_object_pip_subtract(object, 1); 17080fa05eaeSAlan Cox VM_OBJECT_UNLOCK(object); 170992da00bbSMatthew Dillon vm_page_lock_queues(); 171092da00bbSMatthew Dillon vm_page_activate(m); 171192da00bbSMatthew Dillon vm_page_dirty(m); 171292da00bbSMatthew Dillon vm_page_wakeup(m); 171392da00bbSMatthew Dillon vm_page_unlock_queues(); 171492da00bbSMatthew Dillon vm_pager_page_unswapped(m); 171592da00bbSMatthew Dillon return; 171692da00bbSMatthew Dillon } 171792da00bbSMatthew Dillon 171892da00bbSMatthew Dillon if (swap_pager_getpages(object, &m, 1, 0) != 171992da00bbSMatthew Dillon VM_PAGER_OK) 172092da00bbSMatthew Dillon panic("swap_pager_force_pagein: read from swap failed");/*XXX*/ 17210fa05eaeSAlan Cox VM_OBJECT_LOCK(object); 172292da00bbSMatthew Dillon vm_object_pip_subtract(object, 1); 17230fa05eaeSAlan Cox VM_OBJECT_UNLOCK(object); 172492da00bbSMatthew Dillon 172592da00bbSMatthew Dillon vm_page_lock_queues(); 172692da00bbSMatthew Dillon vm_page_dirty(m); 172792da00bbSMatthew Dillon vm_page_dontneed(m); 172892da00bbSMatthew Dillon vm_page_wakeup(m); 172992da00bbSMatthew Dillon vm_page_unlock_queues(); 173092da00bbSMatthew Dillon vm_pager_page_unswapped(m); 173192da00bbSMatthew Dillon } 173292da00bbSMatthew Dillon 173392da00bbSMatthew Dillon 173492da00bbSMatthew Dillon /* 173592da00bbSMatthew Dillon * swap_pager_swapoff: 173692da00bbSMatthew Dillon * 173792da00bbSMatthew Dillon * Page in all of the pages that have been paged out to the 173892da00bbSMatthew Dillon * given device. The corresponding blocks in the bitmap must be 173992da00bbSMatthew Dillon * marked as allocated and the device must be flagged SW_CLOSING. 174092da00bbSMatthew Dillon * There may be no processes swapped out to the device. 174192da00bbSMatthew Dillon * 174292da00bbSMatthew Dillon * The sw_used parameter points to the field in the swdev structure 174392da00bbSMatthew Dillon * that contains a count of the number of blocks still allocated 174492da00bbSMatthew Dillon * on the device. If we encounter objects with a nonzero pip count 174592da00bbSMatthew Dillon * in our scan, we use this number to determine if we're really done. 174692da00bbSMatthew Dillon * 174792da00bbSMatthew Dillon * This routine may block. 174892da00bbSMatthew Dillon */ 174992da00bbSMatthew Dillon void 175092da00bbSMatthew Dillon swap_pager_swapoff(int devidx, int *sw_used) 175192da00bbSMatthew Dillon { 175292da00bbSMatthew Dillon struct swblock **pswap; 175392da00bbSMatthew Dillon struct swblock *swap; 175492da00bbSMatthew Dillon vm_object_t waitobj; 175592da00bbSMatthew Dillon daddr_t v; 175692da00bbSMatthew Dillon int i, j; 175792da00bbSMatthew Dillon 175892da00bbSMatthew Dillon GIANT_REQUIRED; 175992da00bbSMatthew Dillon 176092da00bbSMatthew Dillon full_rescan: 176192da00bbSMatthew Dillon waitobj = NULL; 176292da00bbSMatthew Dillon for (i = 0; i <= swhash_mask; i++) { /* '<=' is correct here */ 176392da00bbSMatthew Dillon restart: 176492da00bbSMatthew Dillon pswap = &swhash[i]; 176592da00bbSMatthew Dillon while ((swap = *pswap) != NULL) { 176692da00bbSMatthew Dillon for (j = 0; j < SWAP_META_PAGES; ++j) { 176792da00bbSMatthew Dillon v = swap->swb_pages[j]; 176892da00bbSMatthew Dillon if (v != SWAPBLK_NONE && 176992da00bbSMatthew Dillon BLK2DEVIDX(v) == devidx) 177092da00bbSMatthew Dillon break; 177192da00bbSMatthew Dillon } 177292da00bbSMatthew Dillon if (j < SWAP_META_PAGES) { 177392da00bbSMatthew Dillon swp_pager_force_pagein(swap, j); 177492da00bbSMatthew Dillon goto restart; 177592da00bbSMatthew Dillon } else if (swap->swb_object->paging_in_progress) { 177692da00bbSMatthew Dillon if (!waitobj) 177792da00bbSMatthew Dillon waitobj = swap->swb_object; 177892da00bbSMatthew Dillon } 177992da00bbSMatthew Dillon pswap = &swap->swb_hnext; 178092da00bbSMatthew Dillon } 178192da00bbSMatthew Dillon } 178292da00bbSMatthew Dillon if (waitobj && *sw_used) { 178392da00bbSMatthew Dillon /* 178492da00bbSMatthew Dillon * We wait on an arbitrary object to clock our rescans 178592da00bbSMatthew Dillon * to the rate of paging completion. 178692da00bbSMatthew Dillon */ 17871ca58953SAlan Cox VM_OBJECT_LOCK(waitobj); 178892da00bbSMatthew Dillon vm_object_pip_wait(waitobj, "swpoff"); 17891ca58953SAlan Cox VM_OBJECT_UNLOCK(waitobj); 179092da00bbSMatthew Dillon goto full_rescan; 179192da00bbSMatthew Dillon } 179292da00bbSMatthew Dillon if (*sw_used) 179392da00bbSMatthew Dillon panic("swapoff: failed to locate %d swap blocks", *sw_used); 179492da00bbSMatthew Dillon } 179592da00bbSMatthew Dillon 17961c7c3c6aSMatthew Dillon /************************************************************************ 17971c7c3c6aSMatthew Dillon * SWAP META DATA * 17981c7c3c6aSMatthew Dillon ************************************************************************ 17991c7c3c6aSMatthew Dillon * 18001c7c3c6aSMatthew Dillon * These routines manipulate the swap metadata stored in the 18014dcc5c2dSMatthew Dillon * OBJT_SWAP object. All swp_*() routines must be called at 18024dcc5c2dSMatthew Dillon * splvm() because swap can be freed up by the low level vm_page 18034dcc5c2dSMatthew Dillon * code which might be called from interrupts beyond what splbio() covers. 18041c7c3c6aSMatthew Dillon * 18054dcc5c2dSMatthew Dillon * Swap metadata is implemented with a global hash and not directly 18064dcc5c2dSMatthew Dillon * linked into the object. Instead the object simply contains 18074dcc5c2dSMatthew Dillon * appropriate tracking counters. 18081c7c3c6aSMatthew Dillon */ 18091c7c3c6aSMatthew Dillon 18101c7c3c6aSMatthew Dillon /* 18111c7c3c6aSMatthew Dillon * SWP_PAGER_HASH() - hash swap meta data 18121c7c3c6aSMatthew Dillon * 18134dcc5c2dSMatthew Dillon * This is an inline helper function which hashes the swapblk given 18141c7c3c6aSMatthew Dillon * the object and page index. It returns a pointer to a pointer 18151c7c3c6aSMatthew Dillon * to the object, or a pointer to a NULL pointer if it could not 18161c7c3c6aSMatthew Dillon * find a swapblk. 18174dcc5c2dSMatthew Dillon * 18184dcc5c2dSMatthew Dillon * This routine must be called at splvm(). 18191c7c3c6aSMatthew Dillon */ 18201c7c3c6aSMatthew Dillon static __inline struct swblock ** 18214dcc5c2dSMatthew Dillon swp_pager_hash(vm_object_t object, vm_pindex_t index) 18221c7c3c6aSMatthew Dillon { 18231c7c3c6aSMatthew Dillon struct swblock **pswap; 18241c7c3c6aSMatthew Dillon struct swblock *swap; 18251c7c3c6aSMatthew Dillon 18265125fe4fSIan Dowse index &= ~(vm_pindex_t)SWAP_META_MASK; 1827af647ddeSBruce Evans pswap = &swhash[(index ^ (int)(intptr_t)object) & swhash_mask]; 18281c7c3c6aSMatthew Dillon while ((swap = *pswap) != NULL) { 18291c7c3c6aSMatthew Dillon if (swap->swb_object == object && 18301c7c3c6aSMatthew Dillon swap->swb_index == index 18311c7c3c6aSMatthew Dillon ) { 18321c7c3c6aSMatthew Dillon break; 18331c7c3c6aSMatthew Dillon } 18341c7c3c6aSMatthew Dillon pswap = &swap->swb_hnext; 18351c7c3c6aSMatthew Dillon } 18361c7c3c6aSMatthew Dillon return (pswap); 18371c7c3c6aSMatthew Dillon } 18381c7c3c6aSMatthew Dillon 18391c7c3c6aSMatthew Dillon /* 18401c7c3c6aSMatthew Dillon * SWP_PAGER_META_BUILD() - add swap block to swap meta data for object 18411c7c3c6aSMatthew Dillon * 18421c7c3c6aSMatthew Dillon * We first convert the object to a swap object if it is a default 18431c7c3c6aSMatthew Dillon * object. 18441c7c3c6aSMatthew Dillon * 18451c7c3c6aSMatthew Dillon * The specified swapblk is added to the object's swap metadata. If 18461c7c3c6aSMatthew Dillon * the swapblk is not valid, it is freed instead. Any previously 18471c7c3c6aSMatthew Dillon * assigned swapblk is freed. 18484dcc5c2dSMatthew Dillon * 18494dcc5c2dSMatthew Dillon * This routine must be called at splvm(), except when used to convert 18504dcc5c2dSMatthew Dillon * an OBJT_DEFAULT object into an OBJT_SWAP object. 18511c7c3c6aSMatthew Dillon */ 18521c7c3c6aSMatthew Dillon static void 18531c7c3c6aSMatthew Dillon swp_pager_meta_build( 18541c7c3c6aSMatthew Dillon vm_object_t object, 185523f09d50SIan Dowse vm_pindex_t pindex, 18564dcc5c2dSMatthew Dillon daddr_t swapblk 18571c7c3c6aSMatthew Dillon ) { 18581c7c3c6aSMatthew Dillon struct swblock *swap; 18591c7c3c6aSMatthew Dillon struct swblock **pswap; 186023f09d50SIan Dowse int idx; 18611c7c3c6aSMatthew Dillon 18620cddd8f0SMatthew Dillon GIANT_REQUIRED; 18631c7c3c6aSMatthew Dillon /* 18641c7c3c6aSMatthew Dillon * Convert default object to swap object if necessary 18651c7c3c6aSMatthew Dillon */ 18661c7c3c6aSMatthew Dillon if (object->type != OBJT_SWAP) { 18671c7c3c6aSMatthew Dillon object->type = OBJT_SWAP; 18681c7c3c6aSMatthew Dillon object->un_pager.swp.swp_bcount = 0; 18691c7c3c6aSMatthew Dillon 1870a9fa2c05SAlfred Perlstein mtx_lock(&sw_alloc_mtx); 18711c7c3c6aSMatthew Dillon if (object->handle != NULL) { 18721c7c3c6aSMatthew Dillon TAILQ_INSERT_TAIL( 18731c7c3c6aSMatthew Dillon NOBJLIST(object->handle), 18741c7c3c6aSMatthew Dillon object, 18751c7c3c6aSMatthew Dillon pager_object_list 18761c7c3c6aSMatthew Dillon ); 18771c7c3c6aSMatthew Dillon } else { 18781c7c3c6aSMatthew Dillon TAILQ_INSERT_TAIL( 18791c7c3c6aSMatthew Dillon &swap_pager_un_object_list, 18801c7c3c6aSMatthew Dillon object, 18811c7c3c6aSMatthew Dillon pager_object_list 18821c7c3c6aSMatthew Dillon ); 18831c7c3c6aSMatthew Dillon } 1884a9fa2c05SAlfred Perlstein mtx_unlock(&sw_alloc_mtx); 18851c7c3c6aSMatthew Dillon } 18861c7c3c6aSMatthew Dillon 18871c7c3c6aSMatthew Dillon /* 18881c7c3c6aSMatthew Dillon * Locate hash entry. If not found create, but if we aren't adding 18894dcc5c2dSMatthew Dillon * anything just return. If we run out of space in the map we wait 18904dcc5c2dSMatthew Dillon * and, since the hash table may have changed, retry. 18911c7c3c6aSMatthew Dillon */ 18924dcc5c2dSMatthew Dillon retry: 189323f09d50SIan Dowse pswap = swp_pager_hash(object, pindex); 18941c7c3c6aSMatthew Dillon 18951c7c3c6aSMatthew Dillon if ((swap = *pswap) == NULL) { 18961c7c3c6aSMatthew Dillon int i; 18971c7c3c6aSMatthew Dillon 18981c7c3c6aSMatthew Dillon if (swapblk == SWAPBLK_NONE) 18991c7c3c6aSMatthew Dillon return; 19001c7c3c6aSMatthew Dillon 1901670d17b5SJeff Roberson swap = *pswap = uma_zalloc(swap_zone, M_NOWAIT); 19024dcc5c2dSMatthew Dillon if (swap == NULL) { 19034dcc5c2dSMatthew Dillon VM_WAIT; 19044dcc5c2dSMatthew Dillon goto retry; 19054dcc5c2dSMatthew Dillon } 1906670d17b5SJeff Roberson 19071c7c3c6aSMatthew Dillon swap->swb_hnext = NULL; 19081c7c3c6aSMatthew Dillon swap->swb_object = object; 190923f09d50SIan Dowse swap->swb_index = pindex & ~(vm_pindex_t)SWAP_META_MASK; 19101c7c3c6aSMatthew Dillon swap->swb_count = 0; 19111c7c3c6aSMatthew Dillon 19121c7c3c6aSMatthew Dillon ++object->un_pager.swp.swp_bcount; 19131c7c3c6aSMatthew Dillon 19141c7c3c6aSMatthew Dillon for (i = 0; i < SWAP_META_PAGES; ++i) 19151c7c3c6aSMatthew Dillon swap->swb_pages[i] = SWAPBLK_NONE; 19161c7c3c6aSMatthew Dillon } 19171c7c3c6aSMatthew Dillon 19181c7c3c6aSMatthew Dillon /* 19191c7c3c6aSMatthew Dillon * Delete prior contents of metadata 19201c7c3c6aSMatthew Dillon */ 192123f09d50SIan Dowse idx = pindex & SWAP_META_MASK; 19221c7c3c6aSMatthew Dillon 192323f09d50SIan Dowse if (swap->swb_pages[idx] != SWAPBLK_NONE) { 192423f09d50SIan Dowse swp_pager_freeswapspace(swap->swb_pages[idx], 1); 19251c7c3c6aSMatthew Dillon --swap->swb_count; 19261c7c3c6aSMatthew Dillon } 19271c7c3c6aSMatthew Dillon 19281c7c3c6aSMatthew Dillon /* 19291c7c3c6aSMatthew Dillon * Enter block into metadata 19301c7c3c6aSMatthew Dillon */ 193123f09d50SIan Dowse swap->swb_pages[idx] = swapblk; 19324dcc5c2dSMatthew Dillon if (swapblk != SWAPBLK_NONE) 19331c7c3c6aSMatthew Dillon ++swap->swb_count; 19341c7c3c6aSMatthew Dillon } 19351c7c3c6aSMatthew Dillon 19361c7c3c6aSMatthew Dillon /* 19371c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata 19381c7c3c6aSMatthew Dillon * 19391c7c3c6aSMatthew Dillon * The requested range of blocks is freed, with any associated swap 19401c7c3c6aSMatthew Dillon * returned to the swap bitmap. 19411c7c3c6aSMatthew Dillon * 19421c7c3c6aSMatthew Dillon * This routine will free swap metadata structures as they are cleaned 19431c7c3c6aSMatthew Dillon * out. This routine does *NOT* operate on swap metadata associated 19441c7c3c6aSMatthew Dillon * with resident pages. 19451c7c3c6aSMatthew Dillon * 19461c7c3c6aSMatthew Dillon * This routine must be called at splvm() 19471c7c3c6aSMatthew Dillon */ 19481c7c3c6aSMatthew Dillon static void 19494dcc5c2dSMatthew Dillon swp_pager_meta_free(vm_object_t object, vm_pindex_t index, daddr_t count) 19501c7c3c6aSMatthew Dillon { 19510cddd8f0SMatthew Dillon GIANT_REQUIRED; 195223955314SAlfred Perlstein 19531c7c3c6aSMatthew Dillon if (object->type != OBJT_SWAP) 19541c7c3c6aSMatthew Dillon return; 19551c7c3c6aSMatthew Dillon 19561c7c3c6aSMatthew Dillon while (count > 0) { 19571c7c3c6aSMatthew Dillon struct swblock **pswap; 19581c7c3c6aSMatthew Dillon struct swblock *swap; 19591c7c3c6aSMatthew Dillon 19601c7c3c6aSMatthew Dillon pswap = swp_pager_hash(object, index); 19611c7c3c6aSMatthew Dillon 19621c7c3c6aSMatthew Dillon if ((swap = *pswap) != NULL) { 19631c7c3c6aSMatthew Dillon daddr_t v = swap->swb_pages[index & SWAP_META_MASK]; 19641c7c3c6aSMatthew Dillon 19651c7c3c6aSMatthew Dillon if (v != SWAPBLK_NONE) { 19661c7c3c6aSMatthew Dillon swp_pager_freeswapspace(v, 1); 19671c7c3c6aSMatthew Dillon swap->swb_pages[index & SWAP_META_MASK] = 19681c7c3c6aSMatthew Dillon SWAPBLK_NONE; 19691c7c3c6aSMatthew Dillon if (--swap->swb_count == 0) { 19701c7c3c6aSMatthew Dillon *pswap = swap->swb_hnext; 1971670d17b5SJeff Roberson uma_zfree(swap_zone, swap); 19721c7c3c6aSMatthew Dillon --object->un_pager.swp.swp_bcount; 19731c7c3c6aSMatthew Dillon } 19741c7c3c6aSMatthew Dillon } 19751c7c3c6aSMatthew Dillon --count; 19761c7c3c6aSMatthew Dillon ++index; 19771c7c3c6aSMatthew Dillon } else { 19784dcc5c2dSMatthew Dillon int n = SWAP_META_PAGES - (index & SWAP_META_MASK); 19791c7c3c6aSMatthew Dillon count -= n; 19801c7c3c6aSMatthew Dillon index += n; 19811c7c3c6aSMatthew Dillon } 19821c7c3c6aSMatthew Dillon } 19831c7c3c6aSMatthew Dillon } 19841c7c3c6aSMatthew Dillon 19851c7c3c6aSMatthew Dillon /* 19861c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object 19871c7c3c6aSMatthew Dillon * 19881c7c3c6aSMatthew Dillon * This routine locates and destroys all swap metadata associated with 19891c7c3c6aSMatthew Dillon * an object. 19904dcc5c2dSMatthew Dillon * 19914dcc5c2dSMatthew Dillon * This routine must be called at splvm() 19921c7c3c6aSMatthew Dillon */ 19931c7c3c6aSMatthew Dillon static void 19941c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object) 19951c7c3c6aSMatthew Dillon { 19961c7c3c6aSMatthew Dillon daddr_t index = 0; 19971c7c3c6aSMatthew Dillon 19980cddd8f0SMatthew Dillon GIANT_REQUIRED; 199923955314SAlfred Perlstein 20001c7c3c6aSMatthew Dillon if (object->type != OBJT_SWAP) 20011c7c3c6aSMatthew Dillon return; 20021c7c3c6aSMatthew Dillon 20031c7c3c6aSMatthew Dillon while (object->un_pager.swp.swp_bcount) { 20041c7c3c6aSMatthew Dillon struct swblock **pswap; 20051c7c3c6aSMatthew Dillon struct swblock *swap; 20061c7c3c6aSMatthew Dillon 20071c7c3c6aSMatthew Dillon pswap = swp_pager_hash(object, index); 20081c7c3c6aSMatthew Dillon if ((swap = *pswap) != NULL) { 20091c7c3c6aSMatthew Dillon int i; 20101c7c3c6aSMatthew Dillon 20111c7c3c6aSMatthew Dillon for (i = 0; i < SWAP_META_PAGES; ++i) { 20121c7c3c6aSMatthew Dillon daddr_t v = swap->swb_pages[i]; 20131c7c3c6aSMatthew Dillon if (v != SWAPBLK_NONE) { 20141c7c3c6aSMatthew Dillon --swap->swb_count; 20154dcc5c2dSMatthew Dillon swp_pager_freeswapspace(v, 1); 20161c7c3c6aSMatthew Dillon } 20171c7c3c6aSMatthew Dillon } 20181c7c3c6aSMatthew Dillon if (swap->swb_count != 0) 20191c7c3c6aSMatthew Dillon panic("swap_pager_meta_free_all: swb_count != 0"); 20201c7c3c6aSMatthew Dillon *pswap = swap->swb_hnext; 2021670d17b5SJeff Roberson uma_zfree(swap_zone, swap); 20221c7c3c6aSMatthew Dillon --object->un_pager.swp.swp_bcount; 20231c7c3c6aSMatthew Dillon } 20241c7c3c6aSMatthew Dillon index += SWAP_META_PAGES; 20251c7c3c6aSMatthew Dillon if (index > 0x20000000) 20261c7c3c6aSMatthew Dillon panic("swp_pager_meta_free_all: failed to locate all swap meta blocks"); 20271c7c3c6aSMatthew Dillon } 20281c7c3c6aSMatthew Dillon } 20291c7c3c6aSMatthew Dillon 20301c7c3c6aSMatthew Dillon /* 20311c7c3c6aSMatthew Dillon * SWP_PAGER_METACTL() - misc control of swap and vm_page_t meta data. 20321c7c3c6aSMatthew Dillon * 20331c7c3c6aSMatthew Dillon * This routine is capable of looking up, popping, or freeing 20341c7c3c6aSMatthew Dillon * swapblk assignments in the swap meta data or in the vm_page_t. 20351c7c3c6aSMatthew Dillon * The routine typically returns the swapblk being looked-up, or popped, 20361c7c3c6aSMatthew Dillon * or SWAPBLK_NONE if the block was freed, or SWAPBLK_NONE if the block 20371c7c3c6aSMatthew Dillon * was invalid. This routine will automatically free any invalid 20381c7c3c6aSMatthew Dillon * meta-data swapblks. 20391c7c3c6aSMatthew Dillon * 20401c7c3c6aSMatthew Dillon * It is not possible to store invalid swapblks in the swap meta data 20411c7c3c6aSMatthew Dillon * (other then a literal 'SWAPBLK_NONE'), so we don't bother checking. 20421c7c3c6aSMatthew Dillon * 20431c7c3c6aSMatthew Dillon * When acting on a busy resident page and paging is in progress, we 20441c7c3c6aSMatthew Dillon * have to wait until paging is complete but otherwise can act on the 20451c7c3c6aSMatthew Dillon * busy page. 20461c7c3c6aSMatthew Dillon * 20474dcc5c2dSMatthew Dillon * This routine must be called at splvm(). 20481c7c3c6aSMatthew Dillon * 20494dcc5c2dSMatthew Dillon * SWM_FREE remove and free swap block from metadata 20501c7c3c6aSMatthew Dillon * SWM_POP remove from meta data but do not free.. pop it out 20511c7c3c6aSMatthew Dillon */ 20521c7c3c6aSMatthew Dillon static daddr_t 20531c7c3c6aSMatthew Dillon swp_pager_meta_ctl( 20541c7c3c6aSMatthew Dillon vm_object_t object, 205523f09d50SIan Dowse vm_pindex_t pindex, 20561c7c3c6aSMatthew Dillon int flags 20571c7c3c6aSMatthew Dillon ) { 20584dcc5c2dSMatthew Dillon struct swblock **pswap; 20594dcc5c2dSMatthew Dillon struct swblock *swap; 20604dcc5c2dSMatthew Dillon daddr_t r1; 206123f09d50SIan Dowse int idx; 20624dcc5c2dSMatthew Dillon 20630cddd8f0SMatthew Dillon GIANT_REQUIRED; 20641c7c3c6aSMatthew Dillon /* 20651c7c3c6aSMatthew Dillon * The meta data only exists of the object is OBJT_SWAP 20661c7c3c6aSMatthew Dillon * and even then might not be allocated yet. 20671c7c3c6aSMatthew Dillon */ 20684dcc5c2dSMatthew Dillon if (object->type != OBJT_SWAP) 20691c7c3c6aSMatthew Dillon return (SWAPBLK_NONE); 20701c7c3c6aSMatthew Dillon 20714dcc5c2dSMatthew Dillon r1 = SWAPBLK_NONE; 207223f09d50SIan Dowse pswap = swp_pager_hash(object, pindex); 20731c7c3c6aSMatthew Dillon 20741c7c3c6aSMatthew Dillon if ((swap = *pswap) != NULL) { 207523f09d50SIan Dowse idx = pindex & SWAP_META_MASK; 207623f09d50SIan Dowse r1 = swap->swb_pages[idx]; 20771c7c3c6aSMatthew Dillon 20781c7c3c6aSMatthew Dillon if (r1 != SWAPBLK_NONE) { 20791c7c3c6aSMatthew Dillon if (flags & SWM_FREE) { 20804dcc5c2dSMatthew Dillon swp_pager_freeswapspace(r1, 1); 20811c7c3c6aSMatthew Dillon r1 = SWAPBLK_NONE; 20821c7c3c6aSMatthew Dillon } 20831c7c3c6aSMatthew Dillon if (flags & (SWM_FREE|SWM_POP)) { 208423f09d50SIan Dowse swap->swb_pages[idx] = SWAPBLK_NONE; 20851c7c3c6aSMatthew Dillon if (--swap->swb_count == 0) { 20861c7c3c6aSMatthew Dillon *pswap = swap->swb_hnext; 2087670d17b5SJeff Roberson uma_zfree(swap_zone, swap); 20881c7c3c6aSMatthew Dillon --object->un_pager.swp.swp_bcount; 20891c7c3c6aSMatthew Dillon } 20901c7c3c6aSMatthew Dillon } 20911c7c3c6aSMatthew Dillon } 20921c7c3c6aSMatthew Dillon } 20931c7c3c6aSMatthew Dillon return (r1); 20941c7c3c6aSMatthew Dillon } 20951c7c3c6aSMatthew Dillon 2096e4057dbdSPoul-Henning Kamp /******************************************************** 2097e4057dbdSPoul-Henning Kamp * CHAINING FUNCTIONS * 2098e4057dbdSPoul-Henning Kamp ******************************************************** 2099e4057dbdSPoul-Henning Kamp * 2100e4057dbdSPoul-Henning Kamp * These functions support recursion of I/O operations 2101e4057dbdSPoul-Henning Kamp * on bp's, typically by chaining one or more 'child' bp's 2102e4057dbdSPoul-Henning Kamp * to the parent. Synchronous, asynchronous, and semi-synchronous 2103e4057dbdSPoul-Henning Kamp * chaining is possible. 2104e4057dbdSPoul-Henning Kamp */ 2105e4057dbdSPoul-Henning Kamp 2106e4057dbdSPoul-Henning Kamp /* 2107e4057dbdSPoul-Henning Kamp * vm_pager_chain_iodone: 2108e4057dbdSPoul-Henning Kamp * 2109e4057dbdSPoul-Henning Kamp * io completion routine for child bp. Currently we fudge a bit 2110e4057dbdSPoul-Henning Kamp * on dealing with b_resid. Since users of these routines may issue 2111e4057dbdSPoul-Henning Kamp * multiple children simultaneously, sequencing of the error can be lost. 2112e4057dbdSPoul-Henning Kamp */ 2113e4057dbdSPoul-Henning Kamp static void 2114e4057dbdSPoul-Henning Kamp vm_pager_chain_iodone(struct buf *nbp) 2115e4057dbdSPoul-Henning Kamp { 21160b441832SPoul-Henning Kamp struct bio *bp; 21170b441832SPoul-Henning Kamp u_int *count; 2118e4057dbdSPoul-Henning Kamp 21190b441832SPoul-Henning Kamp bp = nbp->b_caller1; 2120d6844b6bSTor Egge count = (u_int *)&(bp->bio_driver1); 21210b441832SPoul-Henning Kamp if (bp != NULL) { 2122e4057dbdSPoul-Henning Kamp if (nbp->b_ioflags & BIO_ERROR) { 21230b441832SPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 21240b441832SPoul-Henning Kamp bp->bio_error = nbp->b_error; 2125e4057dbdSPoul-Henning Kamp } else if (nbp->b_resid != 0) { 21260b441832SPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 21270b441832SPoul-Henning Kamp bp->bio_error = EINVAL; 2128e4057dbdSPoul-Henning Kamp } else { 21290b441832SPoul-Henning Kamp bp->bio_resid -= nbp->b_bcount; 2130e4057dbdSPoul-Henning Kamp } 21310b441832SPoul-Henning Kamp nbp->b_caller1 = NULL; 21320b441832SPoul-Henning Kamp --(*count); 21330b441832SPoul-Henning Kamp if (bp->bio_flags & BIO_FLAG1) { 21340b441832SPoul-Henning Kamp bp->bio_flags &= ~BIO_FLAG1; 2135e4057dbdSPoul-Henning Kamp wakeup(bp); 2136e4057dbdSPoul-Henning Kamp } 2137e4057dbdSPoul-Henning Kamp } 2138e4057dbdSPoul-Henning Kamp nbp->b_flags |= B_DONE; 2139e4057dbdSPoul-Henning Kamp nbp->b_flags &= ~B_ASYNC; 2140e4057dbdSPoul-Henning Kamp relpbuf(nbp, NULL); 2141e4057dbdSPoul-Henning Kamp } 2142e4057dbdSPoul-Henning Kamp 2143e4057dbdSPoul-Henning Kamp /* 2144e4057dbdSPoul-Henning Kamp * getchainbuf: 2145e4057dbdSPoul-Henning Kamp * 2146e4057dbdSPoul-Henning Kamp * Obtain a physical buffer and chain it to its parent buffer. When 2147e4057dbdSPoul-Henning Kamp * I/O completes, the parent buffer will be B_SIGNAL'd. Errors are 2148e4057dbdSPoul-Henning Kamp * automatically propagated to the parent 2149e4057dbdSPoul-Henning Kamp */ 215037c84183SPoul-Henning Kamp static struct buf * 21510b441832SPoul-Henning Kamp getchainbuf(struct bio *bp, struct vnode *vp, int flags) 2152e4057dbdSPoul-Henning Kamp { 215323955314SAlfred Perlstein struct buf *nbp; 215423955314SAlfred Perlstein u_int *count; 215523955314SAlfred Perlstein 21560cddd8f0SMatthew Dillon GIANT_REQUIRED; 215723955314SAlfred Perlstein nbp = getpbuf(NULL); 2158d6844b6bSTor Egge count = (u_int *)&(bp->bio_driver1); 2159e4057dbdSPoul-Henning Kamp 21600b441832SPoul-Henning Kamp nbp->b_caller1 = bp; 21610b441832SPoul-Henning Kamp ++(*count); 2162e4057dbdSPoul-Henning Kamp 21630b441832SPoul-Henning Kamp if (*count > 4) 2164e4057dbdSPoul-Henning Kamp waitchainbuf(bp, 4, 0); 2165e4057dbdSPoul-Henning Kamp 21660b441832SPoul-Henning Kamp nbp->b_iocmd = bp->bio_cmd; 216757c10583SPoul-Henning Kamp nbp->b_ioflags = 0; 2168e4057dbdSPoul-Henning Kamp nbp->b_flags = flags; 2169fdcc1cc0SJohn Baldwin nbp->b_rcred = crhold(thread0.td_ucred); 2170fdcc1cc0SJohn Baldwin nbp->b_wcred = crhold(thread0.td_ucred); 2171e4057dbdSPoul-Henning Kamp nbp->b_iodone = vm_pager_chain_iodone; 2172e4057dbdSPoul-Henning Kamp 2173e4057dbdSPoul-Henning Kamp if (vp) 2174e4057dbdSPoul-Henning Kamp pbgetvp(vp, nbp); 2175e4057dbdSPoul-Henning Kamp return (nbp); 2176e4057dbdSPoul-Henning Kamp } 2177e4057dbdSPoul-Henning Kamp 217837c84183SPoul-Henning Kamp static void 2179e4057dbdSPoul-Henning Kamp flushchainbuf(struct buf *nbp) 2180e4057dbdSPoul-Henning Kamp { 21810cddd8f0SMatthew Dillon GIANT_REQUIRED; 2182e4057dbdSPoul-Henning Kamp if (nbp->b_bcount) { 2183e4057dbdSPoul-Henning Kamp nbp->b_bufsize = nbp->b_bcount; 2184e4057dbdSPoul-Henning Kamp if (nbp->b_iocmd == BIO_WRITE) 2185e4057dbdSPoul-Henning Kamp nbp->b_dirtyend = nbp->b_bcount; 2186e4057dbdSPoul-Henning Kamp BUF_KERNPROC(nbp); 218786270230SPoul-Henning Kamp VOP_STRATEGY(nbp->b_vp, nbp); 2188e4057dbdSPoul-Henning Kamp } else { 2189e4057dbdSPoul-Henning Kamp bufdone(nbp); 2190e4057dbdSPoul-Henning Kamp } 2191e4057dbdSPoul-Henning Kamp } 2192e4057dbdSPoul-Henning Kamp 219323955314SAlfred Perlstein static void 21940b441832SPoul-Henning Kamp waitchainbuf(struct bio *bp, int limit, int done) 2195e4057dbdSPoul-Henning Kamp { 2196e4057dbdSPoul-Henning Kamp int s; 219723955314SAlfred Perlstein u_int *count; 2198e4057dbdSPoul-Henning Kamp 21990cddd8f0SMatthew Dillon GIANT_REQUIRED; 2200d6844b6bSTor Egge count = (u_int *)&(bp->bio_driver1); 2201e4057dbdSPoul-Henning Kamp s = splbio(); 22020b441832SPoul-Henning Kamp while (*count > limit) { 22030b441832SPoul-Henning Kamp bp->bio_flags |= BIO_FLAG1; 2204e4057dbdSPoul-Henning Kamp tsleep(bp, PRIBIO + 4, "bpchain", 0); 2205e4057dbdSPoul-Henning Kamp } 2206e4057dbdSPoul-Henning Kamp if (done) { 22070b441832SPoul-Henning Kamp if (bp->bio_resid != 0 && !(bp->bio_flags & BIO_ERROR)) { 22080b441832SPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 22090b441832SPoul-Henning Kamp bp->bio_error = EINVAL; 2210e4057dbdSPoul-Henning Kamp } 22110b441832SPoul-Henning Kamp biodone(bp); 2212e4057dbdSPoul-Henning Kamp } 2213e4057dbdSPoul-Henning Kamp splx(s); 2214e4057dbdSPoul-Henning Kamp } 2215e4057dbdSPoul-Henning Kamp 2216