145749Smckusick /* 245749Smckusick * Copyright (c) 1990 University of Utah. 345749Smckusick * Copyright (c) 1991 The Regents of the University of California. 445749Smckusick * All rights reserved. 545749Smckusick * 645749Smckusick * This code is derived from software contributed to Berkeley by 745749Smckusick * the Systems Programming Group of the University of Utah Computer 845749Smckusick * Science Department. 945749Smckusick * 1045749Smckusick * %sccs.include.redist.c% 1145749Smckusick * 1249289Shibler * from: Utah $Hdr: swap_pager.c 1.4 91/04/30$ 1349289Shibler * 14*55051Spendry * @(#)swap_pager.c 7.12 (Berkeley) 07/12/92 1545749Smckusick */ 1645749Smckusick 1745749Smckusick /* 1845749Smckusick * Quick hack to page to dedicated partition(s). 1945749Smckusick * TODO: 2045749Smckusick * Add multiprocessor locks 2145749Smckusick * Deal with async writes in a better fashion 2245749Smckusick */ 2345749Smckusick 2445749Smckusick #include "swappager.h" 2545749Smckusick #if NSWAPPAGER > 0 2645749Smckusick 2753341Sbostic #include <sys/param.h> 2853341Sbostic #include <sys/systm.h> 2953341Sbostic #include <sys/proc.h> 3053341Sbostic #include <sys/buf.h> 3153341Sbostic #include <sys/map.h> 3253496Sheideman #include <sys/vnode.h> 3353341Sbostic #include <sys/malloc.h> 3445749Smckusick 35*55051Spendry #include <miscfs/specfs/specdev.h> 36*55051Spendry 3753341Sbostic #include <vm/vm.h> 3853341Sbostic #include <vm/queue.h> 3953341Sbostic #include <vm/vm_page.h> 4053341Sbostic #include <vm/vm_pageout.h> 4153341Sbostic #include <vm/swap_pager.h> 4245749Smckusick 4345749Smckusick #define NSWSIZES 16 /* size of swtab */ 4445749Smckusick #define NPENDINGIO 64 /* max # of pending cleans */ 4545749Smckusick #define MAXDADDRS 64 /* max # of disk addrs for fixed allocations */ 4645749Smckusick 4745749Smckusick #ifdef DEBUG 4845749Smckusick int swpagerdebug = 0x100; 4945749Smckusick #define SDB_FOLLOW 0x001 5045749Smckusick #define SDB_INIT 0x002 5145749Smckusick #define SDB_ALLOC 0x004 5245749Smckusick #define SDB_IO 0x008 5345749Smckusick #define SDB_WRITE 0x010 5445749Smckusick #define SDB_FAIL 0x020 5545749Smckusick #define SDB_ALLOCBLK 0x040 5645749Smckusick #define SDB_FULL 0x080 5745749Smckusick #define SDB_ANOM 0x100 5845749Smckusick #define SDB_ANOMPANIC 0x200 5945749Smckusick #endif 6045749Smckusick 6145749Smckusick struct swpagerclean { 6245749Smckusick queue_head_t spc_list; 6345749Smckusick int spc_flags; 6445749Smckusick struct buf *spc_bp; 6545749Smckusick sw_pager_t spc_swp; 6645749Smckusick vm_offset_t spc_kva; 6745749Smckusick vm_page_t spc_m; 6845749Smckusick } swcleanlist[NPENDINGIO]; 6953341Sbostic typedef struct swpagerclean *swp_clean_t; 7045749Smckusick 7153341Sbostic 7245749Smckusick /* spc_flags values */ 7345749Smckusick #define SPC_FREE 0x00 7445749Smckusick #define SPC_BUSY 0x01 7545749Smckusick #define SPC_DONE 0x02 7645749Smckusick #define SPC_ERROR 0x04 7745749Smckusick #define SPC_DIRTY 0x08 7845749Smckusick 7945749Smckusick struct swtab { 8045749Smckusick vm_size_t st_osize; /* size of object (bytes) */ 8145749Smckusick int st_bsize; /* vs. size of swap block (DEV_BSIZE units) */ 8245749Smckusick #ifdef DEBUG 8345749Smckusick u_long st_inuse; /* number in this range in use */ 8445749Smckusick u_long st_usecnt; /* total used of this size */ 8545749Smckusick #endif 8645749Smckusick } swtab[NSWSIZES+1]; 8745749Smckusick 8845749Smckusick #ifdef DEBUG 8945749Smckusick int swap_pager_pendingio; /* max pending async "clean" ops */ 9045749Smckusick int swap_pager_poip; /* pageouts in progress */ 9145749Smckusick int swap_pager_piip; /* pageins in progress */ 9245749Smckusick #endif 9345749Smckusick 9445749Smckusick queue_head_t swap_pager_inuse; /* list of pending page cleans */ 9545749Smckusick queue_head_t swap_pager_free; /* list of free pager clean structs */ 9645749Smckusick queue_head_t swap_pager_list; /* list of "named" anon regions */ 9745749Smckusick 9853341Sbostic static int swap_pager_finish __P((swp_clean_t)); 9953341Sbostic static void swap_pager_init __P((void)); 10053341Sbostic static vm_pager_t swap_pager_alloc __P((caddr_t, vm_size_t, vm_prot_t)); 10153341Sbostic static boolean_t swap_pager_clean __P((vm_page_t, int)); 10253341Sbostic static void swap_pager_dealloc __P((vm_pager_t)); 10353341Sbostic static int swap_pager_getpage 10453341Sbostic __P((vm_pager_t, vm_page_t, boolean_t)); 10553341Sbostic static boolean_t swap_pager_haspage __P((vm_pager_t, vm_offset_t)); 10653341Sbostic static int swap_pager_io __P((sw_pager_t, vm_page_t, int)); 10753341Sbostic static void swap_pager_iodone __P((struct buf *)); 10853341Sbostic static int swap_pager_putpage 10953341Sbostic __P((vm_pager_t, vm_page_t, boolean_t)); 11053341Sbostic 11153341Sbostic struct pagerops swappagerops = { 11253341Sbostic swap_pager_init, 11353341Sbostic swap_pager_alloc, 11453341Sbostic swap_pager_dealloc, 11553341Sbostic swap_pager_getpage, 11653341Sbostic swap_pager_putpage, 11753341Sbostic swap_pager_haspage 11853341Sbostic }; 11953341Sbostic 12053341Sbostic static void 12145749Smckusick swap_pager_init() 12245749Smckusick { 12345749Smckusick register swp_clean_t spc; 12445749Smckusick register int i, bsize; 12545749Smckusick extern int dmmin, dmmax; 12645749Smckusick int maxbsize; 12745749Smckusick 12845749Smckusick #ifdef DEBUG 12945749Smckusick if (swpagerdebug & (SDB_FOLLOW|SDB_INIT)) 13045749Smckusick printf("swpg_init()\n"); 13145749Smckusick #endif 13245749Smckusick dfltpagerops = &swappagerops; 13345749Smckusick queue_init(&swap_pager_list); 13445749Smckusick 13545749Smckusick /* 13645749Smckusick * Initialize clean lists 13745749Smckusick */ 13845749Smckusick queue_init(&swap_pager_inuse); 13945749Smckusick queue_init(&swap_pager_free); 14045749Smckusick for (i = 0, spc = swcleanlist; i < NPENDINGIO; i++, spc++) { 14145749Smckusick queue_enter(&swap_pager_free, spc, swp_clean_t, spc_list); 14245749Smckusick spc->spc_flags = SPC_FREE; 14345749Smckusick } 14445749Smckusick 14545749Smckusick /* 14645749Smckusick * Calculate the swap allocation constants. 14745749Smckusick */ 14845749Smckusick if (dmmin == 0) { 14945749Smckusick dmmin = DMMIN; 15045749Smckusick if (dmmin < CLBYTES/DEV_BSIZE) 15145749Smckusick dmmin = CLBYTES/DEV_BSIZE; 15245749Smckusick } 15345749Smckusick if (dmmax == 0) 15445749Smckusick dmmax = DMMAX; 15545749Smckusick 15645749Smckusick /* 15745749Smckusick * Fill in our table of object size vs. allocation size 15845749Smckusick */ 15945749Smckusick bsize = btodb(PAGE_SIZE); 16045749Smckusick if (bsize < dmmin) 16145749Smckusick bsize = dmmin; 16245749Smckusick maxbsize = btodb(sizeof(sw_bm_t) * NBBY * PAGE_SIZE); 16345749Smckusick if (maxbsize > dmmax) 16445749Smckusick maxbsize = dmmax; 16545749Smckusick for (i = 0; i < NSWSIZES; i++) { 16645749Smckusick swtab[i].st_osize = (vm_size_t) (MAXDADDRS * dbtob(bsize)); 16745749Smckusick swtab[i].st_bsize = bsize; 16845749Smckusick #ifdef DEBUG 16945749Smckusick if (swpagerdebug & SDB_INIT) 17045749Smckusick printf("swpg_init: ix %d, size %x, bsize %x\n", 17145749Smckusick i, swtab[i].st_osize, swtab[i].st_bsize); 17245749Smckusick #endif 17345749Smckusick if (bsize >= maxbsize) 17445749Smckusick break; 17545749Smckusick bsize *= 2; 17645749Smckusick } 17745749Smckusick swtab[i].st_osize = 0; 17845749Smckusick swtab[i].st_bsize = bsize; 17945749Smckusick } 18045749Smckusick 18145749Smckusick /* 18245749Smckusick * Allocate a pager structure and associated resources. 18345749Smckusick * Note that if we are called from the pageout daemon (handle == NULL) 18445749Smckusick * we should not wait for memory as it could resulting in deadlock. 18545749Smckusick */ 18653341Sbostic static vm_pager_t 18745749Smckusick swap_pager_alloc(handle, size, prot) 18845749Smckusick caddr_t handle; 18945749Smckusick register vm_size_t size; 19045749Smckusick vm_prot_t prot; 19145749Smckusick { 19245749Smckusick register vm_pager_t pager; 19345749Smckusick register sw_pager_t swp; 19445749Smckusick struct swtab *swt; 19545749Smckusick int waitok; 19645749Smckusick 19745749Smckusick #ifdef DEBUG 19845749Smckusick if (swpagerdebug & (SDB_FOLLOW|SDB_ALLOC)) 19945749Smckusick printf("swpg_alloc(%x, %x, %x)\n", handle, size, prot); 20045749Smckusick #endif 20145749Smckusick /* 20245749Smckusick * If this is a "named" anonymous region, look it up and 20345749Smckusick * return the appropriate pager if it exists. 20445749Smckusick */ 20545749Smckusick if (handle) { 20645749Smckusick pager = vm_pager_lookup(&swap_pager_list, handle); 20748386Skarels if (pager != NULL) { 20845749Smckusick /* 20945749Smckusick * Use vm_object_lookup to gain a reference 21045749Smckusick * to the object and also to remove from the 21145749Smckusick * object cache. 21245749Smckusick */ 21348386Skarels if (vm_object_lookup(pager) == NULL) 21445749Smckusick panic("swap_pager_alloc: bad object"); 21545749Smckusick return(pager); 21645749Smckusick } 21745749Smckusick } 21845749Smckusick /* 21945749Smckusick * Pager doesn't exist, allocate swap management resources 22045749Smckusick * and initialize. 22145749Smckusick */ 22245749Smckusick waitok = handle ? M_WAITOK : M_NOWAIT; 22345749Smckusick pager = (vm_pager_t)malloc(sizeof *pager, M_VMPAGER, waitok); 22448386Skarels if (pager == NULL) 22548386Skarels return(NULL); 22645749Smckusick swp = (sw_pager_t)malloc(sizeof *swp, M_VMPGDATA, waitok); 22745749Smckusick if (swp == NULL) { 22845749Smckusick #ifdef DEBUG 22945749Smckusick if (swpagerdebug & SDB_FAIL) 23045749Smckusick printf("swpg_alloc: swpager malloc failed\n"); 23145749Smckusick #endif 23245749Smckusick free((caddr_t)pager, M_VMPAGER); 23348386Skarels return(NULL); 23445749Smckusick } 23545749Smckusick size = round_page(size); 23645749Smckusick for (swt = swtab; swt->st_osize; swt++) 23745749Smckusick if (size <= swt->st_osize) 23845749Smckusick break; 23945749Smckusick #ifdef DEBUG 24045749Smckusick swt->st_inuse++; 24145749Smckusick swt->st_usecnt++; 24245749Smckusick #endif 24345749Smckusick swp->sw_osize = size; 24445749Smckusick swp->sw_bsize = swt->st_bsize; 24545749Smckusick swp->sw_nblocks = (btodb(size) + swp->sw_bsize - 1) / swp->sw_bsize; 24645749Smckusick swp->sw_blocks = (sw_blk_t) 24745749Smckusick malloc(swp->sw_nblocks*sizeof(*swp->sw_blocks), 24845749Smckusick M_VMPGDATA, M_NOWAIT); 24945749Smckusick if (swp->sw_blocks == NULL) { 25045749Smckusick free((caddr_t)swp, M_VMPGDATA); 25145749Smckusick free((caddr_t)pager, M_VMPAGER); 25245749Smckusick #ifdef DEBUG 25345749Smckusick if (swpagerdebug & SDB_FAIL) 25445749Smckusick printf("swpg_alloc: sw_blocks malloc failed\n"); 25545749Smckusick swt->st_inuse--; 25645749Smckusick swt->st_usecnt--; 25745749Smckusick #endif 25845749Smckusick return(FALSE); 25945749Smckusick } 26045749Smckusick bzero((caddr_t)swp->sw_blocks, 26145749Smckusick swp->sw_nblocks * sizeof(*swp->sw_blocks)); 26245749Smckusick swp->sw_poip = 0; 26345749Smckusick if (handle) { 26445749Smckusick vm_object_t object; 26545749Smckusick 26645749Smckusick swp->sw_flags = SW_NAMED; 26745749Smckusick queue_enter(&swap_pager_list, pager, vm_pager_t, pg_list); 26845749Smckusick /* 26945749Smckusick * Consistant with other pagers: return with object 27045749Smckusick * referenced. Can't do this with handle == NULL 27145749Smckusick * since it might be the pageout daemon calling. 27245749Smckusick */ 27345749Smckusick object = vm_object_allocate(size); 27445749Smckusick vm_object_enter(object, pager); 27545749Smckusick vm_object_setpager(object, pager, 0, FALSE); 27645749Smckusick } else { 27745749Smckusick swp->sw_flags = 0; 27845749Smckusick queue_init(&pager->pg_list); 27945749Smckusick } 28045749Smckusick pager->pg_handle = handle; 28145749Smckusick pager->pg_ops = &swappagerops; 28245749Smckusick pager->pg_type = PG_SWAP; 28345749Smckusick pager->pg_data = (caddr_t)swp; 28445749Smckusick 28545749Smckusick #ifdef DEBUG 28645749Smckusick if (swpagerdebug & SDB_ALLOC) 28745749Smckusick printf("swpg_alloc: pg_data %x, %x of %x at %x\n", 28845749Smckusick swp, swp->sw_nblocks, swp->sw_bsize, swp->sw_blocks); 28945749Smckusick #endif 29045749Smckusick return(pager); 29145749Smckusick } 29245749Smckusick 29353341Sbostic static void 29445749Smckusick swap_pager_dealloc(pager) 29545749Smckusick vm_pager_t pager; 29645749Smckusick { 29745749Smckusick register int i; 29845749Smckusick register sw_blk_t bp; 29945749Smckusick register sw_pager_t swp; 30045749Smckusick struct swtab *swt; 30145749Smckusick int s; 30245749Smckusick 30345749Smckusick #ifdef DEBUG 30445749Smckusick /* save panic time state */ 30545749Smckusick if ((swpagerdebug & SDB_ANOMPANIC) && panicstr) 30645749Smckusick return; 30745749Smckusick if (swpagerdebug & (SDB_FOLLOW|SDB_ALLOC)) 30845749Smckusick printf("swpg_dealloc(%x)\n", pager); 30945749Smckusick #endif 31045749Smckusick /* 31145749Smckusick * Remove from list right away so lookups will fail if we 31245749Smckusick * block for pageout completion. 31345749Smckusick */ 31445749Smckusick swp = (sw_pager_t) pager->pg_data; 31545749Smckusick if (swp->sw_flags & SW_NAMED) { 31645749Smckusick queue_remove(&swap_pager_list, pager, vm_pager_t, pg_list); 31745749Smckusick swp->sw_flags &= ~SW_NAMED; 31845749Smckusick } 31945749Smckusick #ifdef DEBUG 32045749Smckusick for (swt = swtab; swt->st_osize; swt++) 32145749Smckusick if (swp->sw_osize <= swt->st_osize) 32245749Smckusick break; 32345749Smckusick swt->st_inuse--; 32445749Smckusick #endif 32545749Smckusick 32645749Smckusick /* 32745749Smckusick * Wait for all pageouts to finish and remove 32845749Smckusick * all entries from cleaning list. 32945749Smckusick */ 33045749Smckusick s = splbio(); 33145749Smckusick while (swp->sw_poip) { 33245749Smckusick swp->sw_flags |= SW_WANTED; 33353341Sbostic assert_wait((int)swp, 0); 33445749Smckusick thread_block(); 33545749Smckusick } 33645749Smckusick splx(s); 33748386Skarels (void) swap_pager_clean(NULL, B_WRITE); 33845749Smckusick 33945749Smckusick /* 34045749Smckusick * Free left over swap blocks 34145749Smckusick */ 34245749Smckusick for (i = 0, bp = swp->sw_blocks; i < swp->sw_nblocks; i++, bp++) 34345749Smckusick if (bp->swb_block) { 34445749Smckusick #ifdef DEBUG 34545749Smckusick if (swpagerdebug & (SDB_ALLOCBLK|SDB_FULL)) 34645749Smckusick printf("swpg_dealloc: blk %x\n", 34745749Smckusick bp->swb_block); 34845749Smckusick #endif 34945749Smckusick rmfree(swapmap, swp->sw_bsize, bp->swb_block); 35045749Smckusick } 35145749Smckusick /* 35245749Smckusick * Free swap management resources 35345749Smckusick */ 35445749Smckusick free((caddr_t)swp->sw_blocks, M_VMPGDATA); 35545749Smckusick free((caddr_t)swp, M_VMPGDATA); 35645749Smckusick free((caddr_t)pager, M_VMPAGER); 35745749Smckusick } 35845749Smckusick 35953341Sbostic static int 36045749Smckusick swap_pager_getpage(pager, m, sync) 36145749Smckusick vm_pager_t pager; 36245749Smckusick vm_page_t m; 36345749Smckusick boolean_t sync; 36445749Smckusick { 36545749Smckusick #ifdef DEBUG 36645749Smckusick if (swpagerdebug & SDB_FOLLOW) 36745749Smckusick printf("swpg_getpage(%x, %x, %d)\n", pager, m, sync); 36845749Smckusick #endif 36945749Smckusick return(swap_pager_io((sw_pager_t)pager->pg_data, m, B_READ)); 37045749Smckusick } 37145749Smckusick 37253341Sbostic static int 37345749Smckusick swap_pager_putpage(pager, m, sync) 37445749Smckusick vm_pager_t pager; 37545749Smckusick vm_page_t m; 37645749Smckusick boolean_t sync; 37745749Smckusick { 37845749Smckusick int flags; 37945749Smckusick 38045749Smckusick #ifdef DEBUG 38145749Smckusick if (swpagerdebug & SDB_FOLLOW) 38245749Smckusick printf("swpg_putpage(%x, %x, %d)\n", pager, m, sync); 38345749Smckusick #endif 38448386Skarels if (pager == NULL) { 38548386Skarels (void) swap_pager_clean(NULL, B_WRITE); 38654817Storek return (VM_PAGER_OK); /* ??? */ 38745749Smckusick } 38845749Smckusick flags = B_WRITE; 38945749Smckusick if (!sync) 39045749Smckusick flags |= B_ASYNC; 39145749Smckusick return(swap_pager_io((sw_pager_t)pager->pg_data, m, flags)); 39245749Smckusick } 39345749Smckusick 39453341Sbostic static boolean_t 39545749Smckusick swap_pager_haspage(pager, offset) 39645749Smckusick vm_pager_t pager; 39745749Smckusick vm_offset_t offset; 39845749Smckusick { 39945749Smckusick register sw_pager_t swp; 40045749Smckusick register sw_blk_t swb; 40145749Smckusick int ix; 40245749Smckusick 40345749Smckusick #ifdef DEBUG 40445749Smckusick if (swpagerdebug & (SDB_FOLLOW|SDB_ALLOCBLK)) 40545749Smckusick printf("swpg_haspage(%x, %x) ", pager, offset); 40645749Smckusick #endif 40745749Smckusick swp = (sw_pager_t) pager->pg_data; 40845749Smckusick ix = offset / dbtob(swp->sw_bsize); 40945749Smckusick if (swp->sw_blocks == NULL || ix >= swp->sw_nblocks) { 41045749Smckusick #ifdef DEBUG 41145749Smckusick if (swpagerdebug & (SDB_FAIL|SDB_FOLLOW|SDB_ALLOCBLK)) 41245749Smckusick printf("swpg_haspage: %x bad offset %x, ix %x\n", 41345749Smckusick swp->sw_blocks, offset, ix); 41445749Smckusick #endif 41545749Smckusick return(FALSE); 41645749Smckusick } 41745749Smckusick swb = &swp->sw_blocks[ix]; 41845749Smckusick if (swb->swb_block) 41945749Smckusick ix = atop(offset % dbtob(swp->sw_bsize)); 42045749Smckusick #ifdef DEBUG 42145749Smckusick if (swpagerdebug & SDB_ALLOCBLK) 42245749Smckusick printf("%x blk %x+%x ", swp->sw_blocks, swb->swb_block, ix); 42345749Smckusick if (swpagerdebug & (SDB_FOLLOW|SDB_ALLOCBLK)) 42445749Smckusick printf("-> %c\n", 42545749Smckusick "FT"[swb->swb_block && (swb->swb_mask & (1 << ix))]); 42645749Smckusick #endif 42745749Smckusick if (swb->swb_block && (swb->swb_mask & (1 << ix))) 42845749Smckusick return(TRUE); 42945749Smckusick return(FALSE); 43045749Smckusick } 43145749Smckusick 43245749Smckusick /* 43345749Smckusick * Scaled down version of swap(). 43445749Smckusick * Assumes that PAGE_SIZE < MAXPHYS; i.e. only one operation needed. 43545749Smckusick * BOGUS: lower level IO routines expect a KVA so we have to map our 43645749Smckusick * provided physical page into the KVA to keep them happy. 43745749Smckusick */ 43853341Sbostic static int 43945749Smckusick swap_pager_io(swp, m, flags) 44045749Smckusick register sw_pager_t swp; 44145749Smckusick vm_page_t m; 44245749Smckusick int flags; 44345749Smckusick { 44445749Smckusick register struct buf *bp; 44545749Smckusick register sw_blk_t swb; 44645749Smckusick register int s; 44745749Smckusick int ix; 44845749Smckusick boolean_t rv; 44945749Smckusick vm_offset_t kva, off; 45045749Smckusick swp_clean_t spc; 45145749Smckusick 45245749Smckusick #ifdef DEBUG 45345749Smckusick /* save panic time state */ 45445749Smckusick if ((swpagerdebug & SDB_ANOMPANIC) && panicstr) 45553341Sbostic return (VM_PAGER_FAIL); /* XXX: correct return? */ 45645749Smckusick if (swpagerdebug & (SDB_FOLLOW|SDB_IO)) 45745749Smckusick printf("swpg_io(%x, %x, %x)\n", swp, m, flags); 45845749Smckusick #endif 45945749Smckusick 46045749Smckusick /* 46145749Smckusick * For reads (pageins) and synchronous writes, we clean up 46249289Shibler * all completed async pageouts. 46345749Smckusick */ 46445749Smckusick if ((flags & B_ASYNC) == 0) { 46545749Smckusick s = splbio(); 46649289Shibler #ifdef DEBUG 46749289Shibler /* 46849289Shibler * Check to see if this page is currently being cleaned. 46949289Shibler * If it is, we just wait til the operation is done before 47049289Shibler * continuing. 47149289Shibler */ 47245749Smckusick while (swap_pager_clean(m, flags&B_READ)) { 47349289Shibler if (swpagerdebug & SDB_ANOM) 47449289Shibler printf("swap_pager_io: page %x cleaning\n", m); 47549289Shibler 47645749Smckusick swp->sw_flags |= SW_WANTED; 47753341Sbostic assert_wait((int)swp, 0); 47845749Smckusick thread_block(); 47945749Smckusick } 48049289Shibler #else 48149289Shibler (void) swap_pager_clean(m, flags&B_READ); 48249289Shibler #endif 48345749Smckusick splx(s); 48445749Smckusick } 48545749Smckusick /* 48645749Smckusick * For async writes (pageouts), we cleanup completed pageouts so 48745749Smckusick * that all available resources are freed. Also tells us if this 48845749Smckusick * page is already being cleaned. If it is, or no resources 48945749Smckusick * are available, we try again later. 49045749Smckusick */ 49149289Shibler else if (swap_pager_clean(m, B_WRITE) || 49249289Shibler queue_empty(&swap_pager_free)) { 49349289Shibler #ifdef DEBUG 49449289Shibler if ((swpagerdebug & SDB_ANOM) && 49549289Shibler !queue_empty(&swap_pager_free)) 49649289Shibler printf("swap_pager_io: page %x already cleaning\n", m); 49749289Shibler #endif 49845749Smckusick return(VM_PAGER_FAIL); 49949289Shibler } 50045749Smckusick 50145749Smckusick /* 50245749Smckusick * Determine swap block and allocate as necessary. 50345749Smckusick */ 50445749Smckusick off = m->offset + m->object->paging_offset; 50545749Smckusick ix = off / dbtob(swp->sw_bsize); 50645749Smckusick if (swp->sw_blocks == NULL || ix >= swp->sw_nblocks) { 50745749Smckusick #ifdef DEBUG 50845749Smckusick if (swpagerdebug & SDB_FAIL) 50945749Smckusick printf("swpg_io: bad offset %x+%x(%d) in %x\n", 51045749Smckusick m->offset, m->object->paging_offset, 51145749Smckusick ix, swp->sw_blocks); 51245749Smckusick #endif 51345749Smckusick return(VM_PAGER_FAIL); 51445749Smckusick } 51545749Smckusick swb = &swp->sw_blocks[ix]; 51645749Smckusick off = off % dbtob(swp->sw_bsize); 51745749Smckusick if (flags & B_READ) { 51845749Smckusick if (swb->swb_block == 0 || 51945749Smckusick (swb->swb_mask & (1 << atop(off))) == 0) { 52045749Smckusick #ifdef DEBUG 52145749Smckusick if (swpagerdebug & (SDB_ALLOCBLK|SDB_FAIL)) 52245749Smckusick printf("swpg_io: %x bad read: blk %x+%x, mask %x, off %x+%x\n", 52345749Smckusick swp->sw_blocks, 52445749Smckusick swb->swb_block, atop(off), 52545749Smckusick swb->swb_mask, 52645749Smckusick m->offset, m->object->paging_offset); 52745749Smckusick #endif 52845749Smckusick /* XXX: should we zero page here?? */ 52945749Smckusick return(VM_PAGER_FAIL); 53045749Smckusick } 53145749Smckusick } else if (swb->swb_block == 0) { 53245749Smckusick swb->swb_block = rmalloc(swapmap, swp->sw_bsize); 53345749Smckusick if (swb->swb_block == 0) { 53445749Smckusick #ifdef DEBUG 53545749Smckusick if (swpagerdebug & SDB_FAIL) 53645749Smckusick printf("swpg_io: rmalloc of %x failed\n", 53745749Smckusick swp->sw_bsize); 53845749Smckusick #endif 53945749Smckusick return(VM_PAGER_FAIL); 54045749Smckusick } 54145749Smckusick #ifdef DEBUG 54245749Smckusick if (swpagerdebug & (SDB_FULL|SDB_ALLOCBLK)) 54345749Smckusick printf("swpg_io: %x alloc blk %x at ix %x\n", 54445749Smckusick swp->sw_blocks, swb->swb_block, ix); 54545749Smckusick #endif 54645749Smckusick } 54745749Smckusick 54845749Smckusick /* 54945749Smckusick * Allocate a kernel virtual address and initialize so that PTE 55045749Smckusick * is available for lower level IO drivers. 55145749Smckusick */ 55245749Smckusick kva = vm_pager_map_page(m); 55345749Smckusick 55445749Smckusick /* 55545749Smckusick * Get a swap buffer header and perform the IO 55645749Smckusick */ 55745749Smckusick s = splbio(); 55845749Smckusick while (bswlist.av_forw == NULL) { 55945749Smckusick #ifdef DEBUG 56045749Smckusick if (swpagerdebug & SDB_ANOM) 56149289Shibler printf("swap_pager_io: wait on swbuf for %x (%d)\n", 56245749Smckusick m, flags); 56345749Smckusick #endif 56445749Smckusick bswlist.b_flags |= B_WANTED; 56545749Smckusick sleep((caddr_t)&bswlist, PSWP+1); 56645749Smckusick } 56745749Smckusick bp = bswlist.av_forw; 56845749Smckusick bswlist.av_forw = bp->av_forw; 56945749Smckusick splx(s); 57045749Smckusick bp->b_flags = B_BUSY | (flags & B_READ); 57148386Skarels bp->b_proc = &proc0; /* XXX (but without B_PHYS set this is ok) */ 57245749Smckusick bp->b_un.b_addr = (caddr_t)kva; 57345749Smckusick bp->b_blkno = swb->swb_block + btodb(off); 57445749Smckusick VHOLD(swapdev_vp); 57545749Smckusick bp->b_vp = swapdev_vp; 57646985Smckusick if (swapdev_vp->v_type == VBLK) 57746985Smckusick bp->b_dev = swapdev_vp->v_rdev; 57845749Smckusick bp->b_bcount = PAGE_SIZE; 57953213Smckusick if ((bp->b_flags & B_READ) == 0) { 58053213Smckusick bp->b_dirtyoff = 0; 58153213Smckusick bp->b_dirtyend = PAGE_SIZE; 58245749Smckusick swapdev_vp->v_numoutput++; 58353213Smckusick } 58445749Smckusick 58545749Smckusick /* 58645749Smckusick * If this is an async write we set up additional buffer fields 58745749Smckusick * and place a "cleaning" entry on the inuse queue. 58845749Smckusick */ 58945749Smckusick if ((flags & (B_READ|B_ASYNC)) == B_ASYNC) { 59045749Smckusick #ifdef DEBUG 59145749Smckusick if (queue_empty(&swap_pager_free)) 59245749Smckusick panic("swpg_io: lost spc"); 59345749Smckusick #endif 59445749Smckusick queue_remove_first(&swap_pager_free, 59545749Smckusick spc, swp_clean_t, spc_list); 59645749Smckusick #ifdef DEBUG 59745749Smckusick if (spc->spc_flags != SPC_FREE) 59845749Smckusick panic("swpg_io: bad free spc"); 59945749Smckusick #endif 60045749Smckusick spc->spc_flags = SPC_BUSY; 60145749Smckusick spc->spc_bp = bp; 60245749Smckusick spc->spc_swp = swp; 60345749Smckusick spc->spc_kva = kva; 60445749Smckusick spc->spc_m = m; 60545749Smckusick bp->b_flags |= B_CALL; 60645749Smckusick bp->b_iodone = swap_pager_iodone; 60745749Smckusick s = splbio(); 60845749Smckusick swp->sw_poip++; 60945749Smckusick queue_enter(&swap_pager_inuse, spc, swp_clean_t, spc_list); 61045749Smckusick 61145749Smckusick #ifdef DEBUG 61245749Smckusick swap_pager_poip++; 61345749Smckusick if (swpagerdebug & SDB_WRITE) 61445749Smckusick printf("swpg_io: write: bp=%x swp=%x spc=%x poip=%d\n", 61545749Smckusick bp, swp, spc, swp->sw_poip); 61645749Smckusick if ((swpagerdebug & SDB_ALLOCBLK) && 61745749Smckusick (swb->swb_mask & (1 << atop(off))) == 0) 61845749Smckusick printf("swpg_io: %x write blk %x+%x\n", 61945749Smckusick swp->sw_blocks, swb->swb_block, atop(off)); 62045749Smckusick #endif 62145749Smckusick swb->swb_mask |= (1 << atop(off)); 62245749Smckusick splx(s); 62345749Smckusick } 62445749Smckusick #ifdef DEBUG 62545749Smckusick if (swpagerdebug & SDB_IO) 62645749Smckusick printf("swpg_io: IO start: bp %x, db %x, va %x, pa %x\n", 62745749Smckusick bp, swb->swb_block+btodb(off), kva, VM_PAGE_TO_PHYS(m)); 62845749Smckusick #endif 62945749Smckusick VOP_STRATEGY(bp); 63045749Smckusick if ((flags & (B_READ|B_ASYNC)) == B_ASYNC) { 63145749Smckusick #ifdef DEBUG 63245749Smckusick if (swpagerdebug & SDB_IO) 63345749Smckusick printf("swpg_io: IO started: bp %x\n", bp); 63445749Smckusick #endif 63545749Smckusick return(VM_PAGER_PEND); 63645749Smckusick } 63745749Smckusick s = splbio(); 63845749Smckusick #ifdef DEBUG 63945749Smckusick if (flags & B_READ) 64045749Smckusick swap_pager_piip++; 64145749Smckusick else 64245749Smckusick swap_pager_poip++; 64345749Smckusick #endif 64445749Smckusick while ((bp->b_flags & B_DONE) == 0) { 64553341Sbostic assert_wait((int)bp, 0); 64645749Smckusick thread_block(); 64745749Smckusick } 64845749Smckusick #ifdef DEBUG 64945749Smckusick if (flags & B_READ) 65045749Smckusick --swap_pager_piip; 65145749Smckusick else 65245749Smckusick --swap_pager_poip; 65345749Smckusick #endif 65445749Smckusick rv = (bp->b_flags & B_ERROR) ? VM_PAGER_FAIL : VM_PAGER_OK; 65545749Smckusick bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY); 65645749Smckusick bp->av_forw = bswlist.av_forw; 65745749Smckusick bswlist.av_forw = bp; 65845749Smckusick if (bp->b_vp) 65945749Smckusick brelvp(bp); 66045749Smckusick if (bswlist.b_flags & B_WANTED) { 66145749Smckusick bswlist.b_flags &= ~B_WANTED; 66245749Smckusick thread_wakeup((int)&bswlist); 66345749Smckusick } 66445749Smckusick if ((flags & B_READ) == 0 && rv == VM_PAGER_OK) { 66549289Shibler m->clean = TRUE; 66645749Smckusick pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 66745749Smckusick } 66845749Smckusick splx(s); 66945749Smckusick #ifdef DEBUG 67045749Smckusick if (swpagerdebug & SDB_IO) 67145749Smckusick printf("swpg_io: IO done: bp %x, rv %d\n", bp, rv); 67245749Smckusick if ((swpagerdebug & SDB_FAIL) && rv == VM_PAGER_FAIL) 67345749Smckusick printf("swpg_io: IO error\n"); 67445749Smckusick #endif 67545749Smckusick vm_pager_unmap_page(kva); 67645749Smckusick return(rv); 67745749Smckusick } 67845749Smckusick 67953341Sbostic static boolean_t 68045749Smckusick swap_pager_clean(m, rw) 68145749Smckusick vm_page_t m; 68245749Smckusick int rw; 68345749Smckusick { 68445749Smckusick register swp_clean_t spc, tspc; 68545749Smckusick register int s; 68645749Smckusick 68745749Smckusick #ifdef DEBUG 68845749Smckusick /* save panic time state */ 68945749Smckusick if ((swpagerdebug & SDB_ANOMPANIC) && panicstr) 69054817Storek return (FALSE); /* ??? */ 69145749Smckusick if (swpagerdebug & SDB_FOLLOW) 69245749Smckusick printf("swpg_clean(%x, %d)\n", m, rw); 69345749Smckusick #endif 69448386Skarels tspc = NULL; 69545749Smckusick for (;;) { 69645749Smckusick /* 69745749Smckusick * Look up and removal from inuse list must be done 69845749Smckusick * at splbio() to avoid conflicts with swap_pager_iodone. 69945749Smckusick */ 70045749Smckusick s = splbio(); 70145749Smckusick spc = (swp_clean_t) queue_first(&swap_pager_inuse); 70245749Smckusick while (!queue_end(&swap_pager_inuse, (queue_entry_t)spc)) { 70345749Smckusick if ((spc->spc_flags & SPC_DONE) && 70445749Smckusick swap_pager_finish(spc)) { 70545749Smckusick queue_remove(&swap_pager_inuse, spc, 70645749Smckusick swp_clean_t, spc_list); 70745749Smckusick break; 70845749Smckusick } 70945749Smckusick if (m && m == spc->spc_m) { 71045749Smckusick #ifdef DEBUG 71145749Smckusick if (swpagerdebug & SDB_ANOM) 71249289Shibler printf("swap_pager_clean: page %x on list, flags %x\n", 71345749Smckusick m, spc->spc_flags); 71445749Smckusick #endif 71545749Smckusick tspc = spc; 71645749Smckusick } 71745749Smckusick spc = (swp_clean_t) queue_next(&spc->spc_list); 71845749Smckusick } 71945749Smckusick 72045749Smckusick /* 72145749Smckusick * No operations done, thats all we can do for now. 72245749Smckusick */ 72345749Smckusick if (queue_end(&swap_pager_inuse, (queue_entry_t)spc)) 72445749Smckusick break; 72545749Smckusick splx(s); 72645749Smckusick 72745749Smckusick /* 72845749Smckusick * The desired page was found to be busy earlier in 72945749Smckusick * the scan but has since completed. 73045749Smckusick */ 73145749Smckusick if (tspc && tspc == spc) { 73245749Smckusick #ifdef DEBUG 73345749Smckusick if (swpagerdebug & SDB_ANOM) 73449289Shibler printf("swap_pager_clean: page %x done while looking\n", 73545749Smckusick m); 73645749Smckusick #endif 73748386Skarels tspc = NULL; 73845749Smckusick } 73945749Smckusick spc->spc_flags = SPC_FREE; 74045749Smckusick vm_pager_unmap_page(spc->spc_kva); 74145749Smckusick queue_enter(&swap_pager_free, spc, swp_clean_t, spc_list); 74245749Smckusick #ifdef DEBUG 74345749Smckusick if (swpagerdebug & SDB_WRITE) 74445749Smckusick printf("swpg_clean: free spc %x\n", spc); 74545749Smckusick #endif 74645749Smckusick } 74749289Shibler #ifdef DEBUG 74845749Smckusick /* 74945749Smckusick * If we found that the desired page is already being cleaned 75045749Smckusick * mark it so that swap_pager_iodone() will not set the clean 75145749Smckusick * flag before the pageout daemon has another chance to clean it. 75245749Smckusick */ 75345749Smckusick if (tspc && rw == B_WRITE) { 75445749Smckusick if (swpagerdebug & SDB_ANOM) 75549289Shibler printf("swap_pager_clean: page %x on clean list\n", 75649289Shibler tspc); 75745749Smckusick tspc->spc_flags |= SPC_DIRTY; 75845749Smckusick } 75949289Shibler #endif 76045749Smckusick splx(s); 76145749Smckusick 76245749Smckusick #ifdef DEBUG 76345749Smckusick if (swpagerdebug & SDB_WRITE) 76445749Smckusick printf("swpg_clean: return %d\n", tspc ? TRUE : FALSE); 76545749Smckusick if ((swpagerdebug & SDB_ANOM) && tspc) 76645749Smckusick printf("swpg_clean: %s of cleaning page %x\n", 76745749Smckusick rw == B_READ ? "get" : "put", m); 76845749Smckusick #endif 76945749Smckusick return(tspc ? TRUE : FALSE); 77045749Smckusick } 77145749Smckusick 77253341Sbostic static int 77345749Smckusick swap_pager_finish(spc) 77445749Smckusick register swp_clean_t spc; 77545749Smckusick { 77645749Smckusick vm_object_t object = spc->spc_m->object; 77745749Smckusick 77845749Smckusick /* 77945749Smckusick * Mark the paging operation as done. 78045749Smckusick * (XXX) If we cannot get the lock, leave it til later. 78145749Smckusick * (XXX) Also we are assuming that an async write is a 78245749Smckusick * pageout operation that has incremented the counter. 78345749Smckusick */ 78445749Smckusick if (!vm_object_lock_try(object)) 78545749Smckusick return(0); 78645749Smckusick 78745749Smckusick if (--object->paging_in_progress == 0) 78845749Smckusick thread_wakeup((int) object); 78945749Smckusick 79049289Shibler #ifdef DEBUG 79145749Smckusick /* 79245749Smckusick * XXX: this isn't even close to the right thing to do, 79345749Smckusick * introduces a variety of race conditions. 79445749Smckusick * 79545749Smckusick * If dirty, vm_pageout() has attempted to clean the page 79645749Smckusick * again. In this case we do not do anything as we will 79749289Shibler * see the page again shortly. 79845749Smckusick */ 79949289Shibler if (spc->spc_flags & SPC_DIRTY) { 80049289Shibler if (swpagerdebug & SDB_ANOM) 80149289Shibler printf("swap_pager_finish: page %x dirty again\n", 80249289Shibler spc->spc_m); 80349289Shibler spc->spc_m->busy = FALSE; 80449289Shibler PAGE_WAKEUP(spc->spc_m); 80549289Shibler vm_object_unlock(object); 80649289Shibler return(1); 80745749Smckusick } 80849289Shibler #endif 80945749Smckusick /* 81049289Shibler * If no error mark as clean and inform the pmap system. 81149289Shibler * If error, mark as dirty so we will try again. 81249289Shibler * (XXX could get stuck doing this, should give up after awhile) 81345749Smckusick */ 81449289Shibler if (spc->spc_flags & SPC_ERROR) { 81549289Shibler printf("swap_pager_finish: clean of page %x failed\n", 81649289Shibler VM_PAGE_TO_PHYS(spc->spc_m)); 81749289Shibler spc->spc_m->laundry = TRUE; 81849289Shibler } else { 81949289Shibler spc->spc_m->clean = TRUE; 82049289Shibler pmap_clear_modify(VM_PAGE_TO_PHYS(spc->spc_m)); 82149289Shibler } 82249289Shibler spc->spc_m->busy = FALSE; 82345749Smckusick PAGE_WAKEUP(spc->spc_m); 82445749Smckusick 82545749Smckusick vm_object_unlock(object); 82645749Smckusick return(1); 82745749Smckusick } 82845749Smckusick 82953341Sbostic static void 83045749Smckusick swap_pager_iodone(bp) 83145749Smckusick register struct buf *bp; 83245749Smckusick { 83345749Smckusick register swp_clean_t spc; 83445749Smckusick daddr_t blk; 83545749Smckusick int s; 83645749Smckusick 83745749Smckusick #ifdef DEBUG 83845749Smckusick /* save panic time state */ 83945749Smckusick if ((swpagerdebug & SDB_ANOMPANIC) && panicstr) 84045749Smckusick return; 84145749Smckusick if (swpagerdebug & SDB_FOLLOW) 84245749Smckusick printf("swpg_iodone(%x)\n", bp); 84345749Smckusick #endif 84445749Smckusick s = splbio(); 84545749Smckusick spc = (swp_clean_t) queue_first(&swap_pager_inuse); 84645749Smckusick while (!queue_end(&swap_pager_inuse, (queue_entry_t)spc)) { 84745749Smckusick if (spc->spc_bp == bp) 84845749Smckusick break; 84945749Smckusick spc = (swp_clean_t) queue_next(&spc->spc_list); 85045749Smckusick } 85145749Smckusick #ifdef DEBUG 85245749Smckusick if (queue_end(&swap_pager_inuse, (queue_entry_t)spc)) 85349289Shibler panic("swap_pager_iodone: bp not found"); 85445749Smckusick #endif 85545749Smckusick 85645749Smckusick spc->spc_flags &= ~SPC_BUSY; 85745749Smckusick spc->spc_flags |= SPC_DONE; 85845749Smckusick if (bp->b_flags & B_ERROR) 85945749Smckusick spc->spc_flags |= SPC_ERROR; 86045749Smckusick spc->spc_bp = NULL; 86145749Smckusick blk = bp->b_blkno; 86245749Smckusick 86345749Smckusick #ifdef DEBUG 86445749Smckusick --swap_pager_poip; 86545749Smckusick if (swpagerdebug & SDB_WRITE) 86645749Smckusick printf("swpg_iodone: bp=%x swp=%x flags=%x spc=%x poip=%x\n", 86745749Smckusick bp, spc->spc_swp, spc->spc_swp->sw_flags, 86845749Smckusick spc, spc->spc_swp->sw_poip); 86945749Smckusick #endif 87045749Smckusick 87145749Smckusick spc->spc_swp->sw_poip--; 87245749Smckusick if (spc->spc_swp->sw_flags & SW_WANTED) { 87345749Smckusick spc->spc_swp->sw_flags &= ~SW_WANTED; 87445749Smckusick thread_wakeup((int)spc->spc_swp); 87545749Smckusick } 87645749Smckusick 87745749Smckusick bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY); 87845749Smckusick bp->av_forw = bswlist.av_forw; 87945749Smckusick bswlist.av_forw = bp; 88045749Smckusick if (bp->b_vp) 88145749Smckusick brelvp(bp); 88245749Smckusick if (bswlist.b_flags & B_WANTED) { 88345749Smckusick bswlist.b_flags &= ~B_WANTED; 88445749Smckusick thread_wakeup((int)&bswlist); 88545749Smckusick } 88645749Smckusick thread_wakeup((int) &vm_pages_needed); 88745749Smckusick splx(s); 88845749Smckusick } 88945749Smckusick #endif 890