xref: /csrg-svn/sys/vm/swap_pager.c (revision 53213)
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*53213Smckusick  *	@(#)swap_pager.c	7.6 (Berkeley) 04/20/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 
2745749Smckusick #include "param.h"
2845749Smckusick #include "proc.h"
2945749Smckusick #include "buf.h"
3045749Smckusick #include "map.h"
3145749Smckusick #include "systm.h"
3245749Smckusick #include "specdev.h"
3345749Smckusick #include "vnode.h"
3445749Smckusick #include "malloc.h"
3545749Smckusick #include "queue.h"
3645749Smckusick 
3750550Smckusick #include "vm.h"
3848386Skarels #include "vm_page.h"
3948386Skarels #include "vm_pageout.h"
4048386Skarels #include "swap_pager.h"
4145749Smckusick 
4245749Smckusick #define NSWSIZES	16	/* size of swtab */
4345749Smckusick #define NPENDINGIO	64	/* max # of pending cleans */
4445749Smckusick #define MAXDADDRS	64	/* max # of disk addrs for fixed allocations */
4545749Smckusick 
4645749Smckusick #ifdef DEBUG
4745749Smckusick int	swpagerdebug = 0x100;
4845749Smckusick #define	SDB_FOLLOW	0x001
4945749Smckusick #define SDB_INIT	0x002
5045749Smckusick #define SDB_ALLOC	0x004
5145749Smckusick #define SDB_IO		0x008
5245749Smckusick #define SDB_WRITE	0x010
5345749Smckusick #define SDB_FAIL	0x020
5445749Smckusick #define SDB_ALLOCBLK	0x040
5545749Smckusick #define SDB_FULL	0x080
5645749Smckusick #define SDB_ANOM	0x100
5745749Smckusick #define SDB_ANOMPANIC	0x200
5845749Smckusick #endif
5945749Smckusick 
6045749Smckusick struct swpagerclean {
6145749Smckusick 	queue_head_t		spc_list;
6245749Smckusick 	int			spc_flags;
6345749Smckusick 	struct buf		*spc_bp;
6445749Smckusick 	sw_pager_t		spc_swp;
6545749Smckusick 	vm_offset_t		spc_kva;
6645749Smckusick 	vm_page_t		spc_m;
6745749Smckusick } swcleanlist[NPENDINGIO];
6845749Smckusick typedef	struct swpagerclean	*swp_clean_t;
6945749Smckusick 
7045749Smckusick /* spc_flags values */
7145749Smckusick #define SPC_FREE	0x00
7245749Smckusick #define SPC_BUSY	0x01
7345749Smckusick #define SPC_DONE	0x02
7445749Smckusick #define SPC_ERROR	0x04
7545749Smckusick #define SPC_DIRTY	0x08
7645749Smckusick 
7745749Smckusick struct swtab {
7845749Smckusick 	vm_size_t st_osize;	/* size of object (bytes) */
7945749Smckusick 	int	  st_bsize;	/* vs. size of swap block (DEV_BSIZE units) */
8045749Smckusick #ifdef DEBUG
8145749Smckusick 	u_long	  st_inuse;	/* number in this range in use */
8245749Smckusick 	u_long	  st_usecnt;	/* total used of this size */
8345749Smckusick #endif
8445749Smckusick } swtab[NSWSIZES+1];
8545749Smckusick 
8645749Smckusick #ifdef DEBUG
8745749Smckusick int		swap_pager_pendingio;	/* max pending async "clean" ops */
8845749Smckusick int		swap_pager_poip;	/* pageouts in progress */
8945749Smckusick int		swap_pager_piip;	/* pageins in progress */
9045749Smckusick #endif
9145749Smckusick 
9245749Smckusick queue_head_t	swap_pager_inuse;	/* list of pending page cleans */
9345749Smckusick queue_head_t	swap_pager_free;	/* list of free pager clean structs */
9445749Smckusick queue_head_t	swap_pager_list;	/* list of "named" anon regions */
9545749Smckusick 
9645749Smckusick void
9745749Smckusick swap_pager_init()
9845749Smckusick {
9945749Smckusick 	register swp_clean_t spc;
10045749Smckusick 	register int i, bsize;
10145749Smckusick 	extern int dmmin, dmmax;
10245749Smckusick 	int maxbsize;
10345749Smckusick 
10445749Smckusick #ifdef DEBUG
10545749Smckusick 	if (swpagerdebug & (SDB_FOLLOW|SDB_INIT))
10645749Smckusick 		printf("swpg_init()\n");
10745749Smckusick #endif
10845749Smckusick 	dfltpagerops = &swappagerops;
10945749Smckusick 	queue_init(&swap_pager_list);
11045749Smckusick 
11145749Smckusick 	/*
11245749Smckusick 	 * Initialize clean lists
11345749Smckusick 	 */
11445749Smckusick 	queue_init(&swap_pager_inuse);
11545749Smckusick 	queue_init(&swap_pager_free);
11645749Smckusick 	for (i = 0, spc = swcleanlist; i < NPENDINGIO; i++, spc++) {
11745749Smckusick 		queue_enter(&swap_pager_free, spc, swp_clean_t, spc_list);
11845749Smckusick 		spc->spc_flags = SPC_FREE;
11945749Smckusick 	}
12045749Smckusick 
12145749Smckusick 	/*
12245749Smckusick 	 * Calculate the swap allocation constants.
12345749Smckusick 	 */
12445749Smckusick         if (dmmin == 0) {
12545749Smckusick                 dmmin = DMMIN;
12645749Smckusick 		if (dmmin < CLBYTES/DEV_BSIZE)
12745749Smckusick 			dmmin = CLBYTES/DEV_BSIZE;
12845749Smckusick 	}
12945749Smckusick         if (dmmax == 0)
13045749Smckusick                 dmmax = DMMAX;
13145749Smckusick 
13245749Smckusick 	/*
13345749Smckusick 	 * Fill in our table of object size vs. allocation size
13445749Smckusick 	 */
13545749Smckusick 	bsize = btodb(PAGE_SIZE);
13645749Smckusick 	if (bsize < dmmin)
13745749Smckusick 		bsize = dmmin;
13845749Smckusick 	maxbsize = btodb(sizeof(sw_bm_t) * NBBY * PAGE_SIZE);
13945749Smckusick 	if (maxbsize > dmmax)
14045749Smckusick 		maxbsize = dmmax;
14145749Smckusick 	for (i = 0; i < NSWSIZES; i++) {
14245749Smckusick 		swtab[i].st_osize = (vm_size_t) (MAXDADDRS * dbtob(bsize));
14345749Smckusick 		swtab[i].st_bsize = bsize;
14445749Smckusick #ifdef DEBUG
14545749Smckusick 		if (swpagerdebug & SDB_INIT)
14645749Smckusick 			printf("swpg_init: ix %d, size %x, bsize %x\n",
14745749Smckusick 			       i, swtab[i].st_osize, swtab[i].st_bsize);
14845749Smckusick #endif
14945749Smckusick 		if (bsize >= maxbsize)
15045749Smckusick 			break;
15145749Smckusick 		bsize *= 2;
15245749Smckusick 	}
15345749Smckusick 	swtab[i].st_osize = 0;
15445749Smckusick 	swtab[i].st_bsize = bsize;
15545749Smckusick }
15645749Smckusick 
15745749Smckusick /*
15845749Smckusick  * Allocate a pager structure and associated resources.
15945749Smckusick  * Note that if we are called from the pageout daemon (handle == NULL)
16045749Smckusick  * we should not wait for memory as it could resulting in deadlock.
16145749Smckusick  */
16245749Smckusick vm_pager_t
16345749Smckusick swap_pager_alloc(handle, size, prot)
16445749Smckusick 	caddr_t handle;
16545749Smckusick 	register vm_size_t size;
16645749Smckusick 	vm_prot_t prot;
16745749Smckusick {
16845749Smckusick 	register vm_pager_t pager;
16945749Smckusick 	register sw_pager_t swp;
17045749Smckusick 	struct swtab *swt;
17145749Smckusick 	int waitok;
17245749Smckusick 
17345749Smckusick #ifdef DEBUG
17445749Smckusick 	if (swpagerdebug & (SDB_FOLLOW|SDB_ALLOC))
17545749Smckusick 		printf("swpg_alloc(%x, %x, %x)\n", handle, size, prot);
17645749Smckusick #endif
17745749Smckusick 	/*
17845749Smckusick 	 * If this is a "named" anonymous region, look it up and
17945749Smckusick 	 * return the appropriate pager if it exists.
18045749Smckusick 	 */
18145749Smckusick 	if (handle) {
18245749Smckusick 		pager = vm_pager_lookup(&swap_pager_list, handle);
18348386Skarels 		if (pager != NULL) {
18445749Smckusick 			/*
18545749Smckusick 			 * Use vm_object_lookup to gain a reference
18645749Smckusick 			 * to the object and also to remove from the
18745749Smckusick 			 * object cache.
18845749Smckusick 			 */
18948386Skarels 			if (vm_object_lookup(pager) == NULL)
19045749Smckusick 				panic("swap_pager_alloc: bad object");
19145749Smckusick 			return(pager);
19245749Smckusick 		}
19345749Smckusick 	}
19445749Smckusick 	/*
19545749Smckusick 	 * Pager doesn't exist, allocate swap management resources
19645749Smckusick 	 * and initialize.
19745749Smckusick 	 */
19845749Smckusick 	waitok = handle ? M_WAITOK : M_NOWAIT;
19945749Smckusick 	pager = (vm_pager_t)malloc(sizeof *pager, M_VMPAGER, waitok);
20048386Skarels 	if (pager == NULL)
20148386Skarels 		return(NULL);
20245749Smckusick 	swp = (sw_pager_t)malloc(sizeof *swp, M_VMPGDATA, waitok);
20345749Smckusick 	if (swp == NULL) {
20445749Smckusick #ifdef DEBUG
20545749Smckusick 		if (swpagerdebug & SDB_FAIL)
20645749Smckusick 			printf("swpg_alloc: swpager malloc failed\n");
20745749Smckusick #endif
20845749Smckusick 		free((caddr_t)pager, M_VMPAGER);
20948386Skarels 		return(NULL);
21045749Smckusick 	}
21145749Smckusick 	size = round_page(size);
21245749Smckusick 	for (swt = swtab; swt->st_osize; swt++)
21345749Smckusick 		if (size <= swt->st_osize)
21445749Smckusick 			break;
21545749Smckusick #ifdef DEBUG
21645749Smckusick 	swt->st_inuse++;
21745749Smckusick 	swt->st_usecnt++;
21845749Smckusick #endif
21945749Smckusick 	swp->sw_osize = size;
22045749Smckusick 	swp->sw_bsize = swt->st_bsize;
22145749Smckusick 	swp->sw_nblocks = (btodb(size) + swp->sw_bsize - 1) / swp->sw_bsize;
22245749Smckusick 	swp->sw_blocks = (sw_blk_t)
22345749Smckusick 		malloc(swp->sw_nblocks*sizeof(*swp->sw_blocks),
22445749Smckusick 		       M_VMPGDATA, M_NOWAIT);
22545749Smckusick 	if (swp->sw_blocks == NULL) {
22645749Smckusick 		free((caddr_t)swp, M_VMPGDATA);
22745749Smckusick 		free((caddr_t)pager, M_VMPAGER);
22845749Smckusick #ifdef DEBUG
22945749Smckusick 		if (swpagerdebug & SDB_FAIL)
23045749Smckusick 			printf("swpg_alloc: sw_blocks malloc failed\n");
23145749Smckusick 		swt->st_inuse--;
23245749Smckusick 		swt->st_usecnt--;
23345749Smckusick #endif
23445749Smckusick 		return(FALSE);
23545749Smckusick 	}
23645749Smckusick 	bzero((caddr_t)swp->sw_blocks,
23745749Smckusick 	      swp->sw_nblocks * sizeof(*swp->sw_blocks));
23845749Smckusick 	swp->sw_poip = 0;
23945749Smckusick 	if (handle) {
24045749Smckusick 		vm_object_t object;
24145749Smckusick 
24245749Smckusick 		swp->sw_flags = SW_NAMED;
24345749Smckusick 		queue_enter(&swap_pager_list, pager, vm_pager_t, pg_list);
24445749Smckusick 		/*
24545749Smckusick 		 * Consistant with other pagers: return with object
24645749Smckusick 		 * referenced.  Can't do this with handle == NULL
24745749Smckusick 		 * since it might be the pageout daemon calling.
24845749Smckusick 		 */
24945749Smckusick 		object = vm_object_allocate(size);
25045749Smckusick 		vm_object_enter(object, pager);
25145749Smckusick 		vm_object_setpager(object, pager, 0, FALSE);
25245749Smckusick 	} else {
25345749Smckusick 		swp->sw_flags = 0;
25445749Smckusick 		queue_init(&pager->pg_list);
25545749Smckusick 	}
25645749Smckusick 	pager->pg_handle = handle;
25745749Smckusick 	pager->pg_ops = &swappagerops;
25845749Smckusick 	pager->pg_type = PG_SWAP;
25945749Smckusick 	pager->pg_data = (caddr_t)swp;
26045749Smckusick 
26145749Smckusick #ifdef DEBUG
26245749Smckusick 	if (swpagerdebug & SDB_ALLOC)
26345749Smckusick 		printf("swpg_alloc: pg_data %x, %x of %x at %x\n",
26445749Smckusick 		       swp, swp->sw_nblocks, swp->sw_bsize, swp->sw_blocks);
26545749Smckusick #endif
26645749Smckusick 	return(pager);
26745749Smckusick }
26845749Smckusick 
26945749Smckusick void
27045749Smckusick swap_pager_dealloc(pager)
27145749Smckusick 	vm_pager_t pager;
27245749Smckusick {
27345749Smckusick 	register int i;
27445749Smckusick 	register sw_blk_t bp;
27545749Smckusick 	register sw_pager_t swp;
27645749Smckusick 	struct swtab *swt;
27745749Smckusick 	int s;
27845749Smckusick 
27945749Smckusick #ifdef DEBUG
28045749Smckusick 	/* save panic time state */
28145749Smckusick 	if ((swpagerdebug & SDB_ANOMPANIC) && panicstr)
28245749Smckusick 		return;
28345749Smckusick 	if (swpagerdebug & (SDB_FOLLOW|SDB_ALLOC))
28445749Smckusick 		printf("swpg_dealloc(%x)\n", pager);
28545749Smckusick #endif
28645749Smckusick 	/*
28745749Smckusick 	 * Remove from list right away so lookups will fail if we
28845749Smckusick 	 * block for pageout completion.
28945749Smckusick 	 */
29045749Smckusick 	swp = (sw_pager_t) pager->pg_data;
29145749Smckusick 	if (swp->sw_flags & SW_NAMED) {
29245749Smckusick 		queue_remove(&swap_pager_list, pager, vm_pager_t, pg_list);
29345749Smckusick 		swp->sw_flags &= ~SW_NAMED;
29445749Smckusick 	}
29545749Smckusick #ifdef DEBUG
29645749Smckusick 	for (swt = swtab; swt->st_osize; swt++)
29745749Smckusick 		if (swp->sw_osize <= swt->st_osize)
29845749Smckusick 			break;
29945749Smckusick 	swt->st_inuse--;
30045749Smckusick #endif
30145749Smckusick 
30245749Smckusick 	/*
30345749Smckusick 	 * Wait for all pageouts to finish and remove
30445749Smckusick 	 * all entries from cleaning list.
30545749Smckusick 	 */
30645749Smckusick 	s = splbio();
30745749Smckusick 	while (swp->sw_poip) {
30845749Smckusick 		swp->sw_flags |= SW_WANTED;
30945749Smckusick 		assert_wait((int)swp);
31045749Smckusick 		thread_block();
31145749Smckusick 	}
31245749Smckusick 	splx(s);
31348386Skarels 	(void) swap_pager_clean(NULL, B_WRITE);
31445749Smckusick 
31545749Smckusick 	/*
31645749Smckusick 	 * Free left over swap blocks
31745749Smckusick 	 */
31845749Smckusick 	for (i = 0, bp = swp->sw_blocks; i < swp->sw_nblocks; i++, bp++)
31945749Smckusick 		if (bp->swb_block) {
32045749Smckusick #ifdef DEBUG
32145749Smckusick 			if (swpagerdebug & (SDB_ALLOCBLK|SDB_FULL))
32245749Smckusick 				printf("swpg_dealloc: blk %x\n",
32345749Smckusick 				       bp->swb_block);
32445749Smckusick #endif
32545749Smckusick 			rmfree(swapmap, swp->sw_bsize, bp->swb_block);
32645749Smckusick 		}
32745749Smckusick 	/*
32845749Smckusick 	 * Free swap management resources
32945749Smckusick 	 */
33045749Smckusick 	free((caddr_t)swp->sw_blocks, M_VMPGDATA);
33145749Smckusick 	free((caddr_t)swp, M_VMPGDATA);
33245749Smckusick 	free((caddr_t)pager, M_VMPAGER);
33345749Smckusick }
33445749Smckusick 
33545749Smckusick swap_pager_getpage(pager, m, sync)
33645749Smckusick 	vm_pager_t pager;
33745749Smckusick 	vm_page_t m;
33845749Smckusick 	boolean_t sync;
33945749Smckusick {
34045749Smckusick #ifdef DEBUG
34145749Smckusick 	if (swpagerdebug & SDB_FOLLOW)
34245749Smckusick 		printf("swpg_getpage(%x, %x, %d)\n", pager, m, sync);
34345749Smckusick #endif
34445749Smckusick 	return(swap_pager_io((sw_pager_t)pager->pg_data, m, B_READ));
34545749Smckusick }
34645749Smckusick 
34745749Smckusick swap_pager_putpage(pager, m, sync)
34845749Smckusick 	vm_pager_t pager;
34945749Smckusick 	vm_page_t m;
35045749Smckusick 	boolean_t sync;
35145749Smckusick {
35245749Smckusick 	int flags;
35345749Smckusick 
35445749Smckusick #ifdef DEBUG
35545749Smckusick 	if (swpagerdebug & SDB_FOLLOW)
35645749Smckusick 		printf("swpg_putpage(%x, %x, %d)\n", pager, m, sync);
35745749Smckusick #endif
35848386Skarels 	if (pager == NULL) {
35948386Skarels 		(void) swap_pager_clean(NULL, B_WRITE);
36045749Smckusick 		return;
36145749Smckusick 	}
36245749Smckusick 	flags = B_WRITE;
36345749Smckusick 	if (!sync)
36445749Smckusick 		flags |= B_ASYNC;
36545749Smckusick 	return(swap_pager_io((sw_pager_t)pager->pg_data, m, flags));
36645749Smckusick }
36745749Smckusick 
36845749Smckusick boolean_t
36945749Smckusick swap_pager_haspage(pager, offset)
37045749Smckusick 	vm_pager_t pager;
37145749Smckusick 	vm_offset_t offset;
37245749Smckusick {
37345749Smckusick 	register sw_pager_t swp;
37445749Smckusick 	register sw_blk_t swb;
37545749Smckusick 	int ix;
37645749Smckusick 
37745749Smckusick #ifdef DEBUG
37845749Smckusick 	if (swpagerdebug & (SDB_FOLLOW|SDB_ALLOCBLK))
37945749Smckusick 		printf("swpg_haspage(%x, %x) ", pager, offset);
38045749Smckusick #endif
38145749Smckusick 	swp = (sw_pager_t) pager->pg_data;
38245749Smckusick 	ix = offset / dbtob(swp->sw_bsize);
38345749Smckusick 	if (swp->sw_blocks == NULL || ix >= swp->sw_nblocks) {
38445749Smckusick #ifdef DEBUG
38545749Smckusick 		if (swpagerdebug & (SDB_FAIL|SDB_FOLLOW|SDB_ALLOCBLK))
38645749Smckusick 			printf("swpg_haspage: %x bad offset %x, ix %x\n",
38745749Smckusick 			       swp->sw_blocks, offset, ix);
38845749Smckusick #endif
38945749Smckusick 		return(FALSE);
39045749Smckusick 	}
39145749Smckusick 	swb = &swp->sw_blocks[ix];
39245749Smckusick 	if (swb->swb_block)
39345749Smckusick 		ix = atop(offset % dbtob(swp->sw_bsize));
39445749Smckusick #ifdef DEBUG
39545749Smckusick 	if (swpagerdebug & SDB_ALLOCBLK)
39645749Smckusick 		printf("%x blk %x+%x ", swp->sw_blocks, swb->swb_block, ix);
39745749Smckusick 	if (swpagerdebug & (SDB_FOLLOW|SDB_ALLOCBLK))
39845749Smckusick 		printf("-> %c\n",
39945749Smckusick 		       "FT"[swb->swb_block && (swb->swb_mask & (1 << ix))]);
40045749Smckusick #endif
40145749Smckusick 	if (swb->swb_block && (swb->swb_mask & (1 << ix)))
40245749Smckusick 		return(TRUE);
40345749Smckusick 	return(FALSE);
40445749Smckusick }
40545749Smckusick 
40645749Smckusick /*
40745749Smckusick  * Scaled down version of swap().
40845749Smckusick  * Assumes that PAGE_SIZE < MAXPHYS; i.e. only one operation needed.
40945749Smckusick  * BOGUS:  lower level IO routines expect a KVA so we have to map our
41045749Smckusick  * provided physical page into the KVA to keep them happy.
41145749Smckusick  */
41245749Smckusick swap_pager_io(swp, m, flags)
41345749Smckusick 	register sw_pager_t swp;
41445749Smckusick 	vm_page_t m;
41545749Smckusick 	int flags;
41645749Smckusick {
41745749Smckusick 	register struct buf *bp;
41845749Smckusick 	register sw_blk_t swb;
41945749Smckusick 	register int s;
42045749Smckusick 	int ix;
42145749Smckusick 	boolean_t rv;
42245749Smckusick 	vm_offset_t kva, off;
42345749Smckusick 	swp_clean_t spc;
42445749Smckusick 
42545749Smckusick #ifdef DEBUG
42645749Smckusick 	/* save panic time state */
42745749Smckusick 	if ((swpagerdebug & SDB_ANOMPANIC) && panicstr)
42845749Smckusick 		return;
42945749Smckusick 	if (swpagerdebug & (SDB_FOLLOW|SDB_IO))
43045749Smckusick 		printf("swpg_io(%x, %x, %x)\n", swp, m, flags);
43145749Smckusick #endif
43245749Smckusick 
43345749Smckusick 	/*
43445749Smckusick 	 * For reads (pageins) and synchronous writes, we clean up
43549289Shibler 	 * all completed async pageouts.
43645749Smckusick 	 */
43745749Smckusick 	if ((flags & B_ASYNC) == 0) {
43845749Smckusick 		s = splbio();
43949289Shibler #ifdef DEBUG
44049289Shibler 		/*
44149289Shibler 		 * Check to see if this page is currently being cleaned.
44249289Shibler 		 * If it is, we just wait til the operation is done before
44349289Shibler 		 * continuing.
44449289Shibler 		 */
44545749Smckusick 		while (swap_pager_clean(m, flags&B_READ)) {
44649289Shibler 			if (swpagerdebug & SDB_ANOM)
44749289Shibler 				printf("swap_pager_io: page %x cleaning\n", m);
44849289Shibler 
44945749Smckusick 			swp->sw_flags |= SW_WANTED;
45045749Smckusick 			assert_wait((int)swp);
45145749Smckusick 			thread_block();
45245749Smckusick 		}
45349289Shibler #else
45449289Shibler 		(void) swap_pager_clean(m, flags&B_READ);
45549289Shibler #endif
45645749Smckusick 		splx(s);
45745749Smckusick 	}
45845749Smckusick 	/*
45945749Smckusick 	 * For async writes (pageouts), we cleanup completed pageouts so
46045749Smckusick 	 * that all available resources are freed.  Also tells us if this
46145749Smckusick 	 * page is already being cleaned.  If it is, or no resources
46245749Smckusick 	 * are available, we try again later.
46345749Smckusick 	 */
46449289Shibler 	else if (swap_pager_clean(m, B_WRITE) ||
46549289Shibler 		 queue_empty(&swap_pager_free)) {
46649289Shibler #ifdef DEBUG
46749289Shibler 		if ((swpagerdebug & SDB_ANOM) &&
46849289Shibler 		    !queue_empty(&swap_pager_free))
46949289Shibler 			printf("swap_pager_io: page %x already cleaning\n", m);
47049289Shibler #endif
47145749Smckusick 		return(VM_PAGER_FAIL);
47249289Shibler 	}
47345749Smckusick 
47445749Smckusick 	/*
47545749Smckusick 	 * Determine swap block and allocate as necessary.
47645749Smckusick 	 */
47745749Smckusick 	off = m->offset + m->object->paging_offset;
47845749Smckusick 	ix = off / dbtob(swp->sw_bsize);
47945749Smckusick 	if (swp->sw_blocks == NULL || ix >= swp->sw_nblocks) {
48045749Smckusick #ifdef DEBUG
48145749Smckusick 		if (swpagerdebug & SDB_FAIL)
48245749Smckusick 			printf("swpg_io: bad offset %x+%x(%d) in %x\n",
48345749Smckusick 			       m->offset, m->object->paging_offset,
48445749Smckusick 			       ix, swp->sw_blocks);
48545749Smckusick #endif
48645749Smckusick 		return(VM_PAGER_FAIL);
48745749Smckusick 	}
48845749Smckusick 	swb = &swp->sw_blocks[ix];
48945749Smckusick 	off = off % dbtob(swp->sw_bsize);
49045749Smckusick 	if (flags & B_READ) {
49145749Smckusick 		if (swb->swb_block == 0 ||
49245749Smckusick 		    (swb->swb_mask & (1 << atop(off))) == 0) {
49345749Smckusick #ifdef DEBUG
49445749Smckusick 			if (swpagerdebug & (SDB_ALLOCBLK|SDB_FAIL))
49545749Smckusick 				printf("swpg_io: %x bad read: blk %x+%x, mask %x, off %x+%x\n",
49645749Smckusick 				       swp->sw_blocks,
49745749Smckusick 				       swb->swb_block, atop(off),
49845749Smckusick 				       swb->swb_mask,
49945749Smckusick 				       m->offset, m->object->paging_offset);
50045749Smckusick #endif
50145749Smckusick 			/* XXX: should we zero page here?? */
50245749Smckusick 			return(VM_PAGER_FAIL);
50345749Smckusick 		}
50445749Smckusick 	} else if (swb->swb_block == 0) {
50545749Smckusick 		swb->swb_block = rmalloc(swapmap, swp->sw_bsize);
50645749Smckusick 		if (swb->swb_block == 0) {
50745749Smckusick #ifdef DEBUG
50845749Smckusick 			if (swpagerdebug & SDB_FAIL)
50945749Smckusick 				printf("swpg_io: rmalloc of %x failed\n",
51045749Smckusick 				       swp->sw_bsize);
51145749Smckusick #endif
51245749Smckusick 			return(VM_PAGER_FAIL);
51345749Smckusick 		}
51445749Smckusick #ifdef DEBUG
51545749Smckusick 		if (swpagerdebug & (SDB_FULL|SDB_ALLOCBLK))
51645749Smckusick 			printf("swpg_io: %x alloc blk %x at ix %x\n",
51745749Smckusick 			       swp->sw_blocks, swb->swb_block, ix);
51845749Smckusick #endif
51945749Smckusick 	}
52045749Smckusick 
52145749Smckusick 	/*
52245749Smckusick 	 * Allocate a kernel virtual address and initialize so that PTE
52345749Smckusick 	 * is available for lower level IO drivers.
52445749Smckusick 	 */
52545749Smckusick 	kva = vm_pager_map_page(m);
52645749Smckusick 
52745749Smckusick 	/*
52845749Smckusick 	 * Get a swap buffer header and perform the IO
52945749Smckusick 	 */
53045749Smckusick 	s = splbio();
53145749Smckusick 	while (bswlist.av_forw == NULL) {
53245749Smckusick #ifdef DEBUG
53345749Smckusick 		if (swpagerdebug & SDB_ANOM)
53449289Shibler 			printf("swap_pager_io: wait on swbuf for %x (%d)\n",
53545749Smckusick 			       m, flags);
53645749Smckusick #endif
53745749Smckusick 		bswlist.b_flags |= B_WANTED;
53845749Smckusick 		sleep((caddr_t)&bswlist, PSWP+1);
53945749Smckusick 	}
54045749Smckusick 	bp = bswlist.av_forw;
54145749Smckusick 	bswlist.av_forw = bp->av_forw;
54245749Smckusick 	splx(s);
54345749Smckusick 	bp->b_flags = B_BUSY | (flags & B_READ);
54448386Skarels 	bp->b_proc = &proc0;	/* XXX (but without B_PHYS set this is ok) */
54545749Smckusick 	bp->b_un.b_addr = (caddr_t)kva;
54645749Smckusick 	bp->b_blkno = swb->swb_block + btodb(off);
54745749Smckusick 	VHOLD(swapdev_vp);
54845749Smckusick 	bp->b_vp = swapdev_vp;
54946985Smckusick 	if (swapdev_vp->v_type == VBLK)
55046985Smckusick 		bp->b_dev = swapdev_vp->v_rdev;
55145749Smckusick 	bp->b_bcount = PAGE_SIZE;
552*53213Smckusick 	if ((bp->b_flags & B_READ) == 0) {
553*53213Smckusick 		bp->b_dirtyoff = 0;
554*53213Smckusick 		bp->b_dirtyend = PAGE_SIZE;
55545749Smckusick 		swapdev_vp->v_numoutput++;
556*53213Smckusick 	}
55745749Smckusick 
55845749Smckusick 	/*
55945749Smckusick 	 * If this is an async write we set up additional buffer fields
56045749Smckusick 	 * and place a "cleaning" entry on the inuse queue.
56145749Smckusick 	 */
56245749Smckusick 	if ((flags & (B_READ|B_ASYNC)) == B_ASYNC) {
56345749Smckusick #ifdef DEBUG
56445749Smckusick 		if (queue_empty(&swap_pager_free))
56545749Smckusick 			panic("swpg_io: lost spc");
56645749Smckusick #endif
56745749Smckusick 		queue_remove_first(&swap_pager_free,
56845749Smckusick 				   spc, swp_clean_t, spc_list);
56945749Smckusick #ifdef DEBUG
57045749Smckusick 		if (spc->spc_flags != SPC_FREE)
57145749Smckusick 			panic("swpg_io: bad free spc");
57245749Smckusick #endif
57345749Smckusick 		spc->spc_flags = SPC_BUSY;
57445749Smckusick 		spc->spc_bp = bp;
57545749Smckusick 		spc->spc_swp = swp;
57645749Smckusick 		spc->spc_kva = kva;
57745749Smckusick 		spc->spc_m = m;
57845749Smckusick 		bp->b_flags |= B_CALL;
57945749Smckusick 		bp->b_iodone = swap_pager_iodone;
58045749Smckusick 		s = splbio();
58145749Smckusick 		swp->sw_poip++;
58245749Smckusick 		queue_enter(&swap_pager_inuse, spc, swp_clean_t, spc_list);
58345749Smckusick 
58445749Smckusick #ifdef DEBUG
58545749Smckusick 		swap_pager_poip++;
58645749Smckusick 		if (swpagerdebug & SDB_WRITE)
58745749Smckusick 			printf("swpg_io: write: bp=%x swp=%x spc=%x poip=%d\n",
58845749Smckusick 			       bp, swp, spc, swp->sw_poip);
58945749Smckusick 		if ((swpagerdebug & SDB_ALLOCBLK) &&
59045749Smckusick 		    (swb->swb_mask & (1 << atop(off))) == 0)
59145749Smckusick 			printf("swpg_io: %x write blk %x+%x\n",
59245749Smckusick 			       swp->sw_blocks, swb->swb_block, atop(off));
59345749Smckusick #endif
59445749Smckusick 		swb->swb_mask |= (1 << atop(off));
59545749Smckusick 		splx(s);
59645749Smckusick 	}
59745749Smckusick #ifdef DEBUG
59845749Smckusick 	if (swpagerdebug & SDB_IO)
59945749Smckusick 		printf("swpg_io: IO start: bp %x, db %x, va %x, pa %x\n",
60045749Smckusick 		       bp, swb->swb_block+btodb(off), kva, VM_PAGE_TO_PHYS(m));
60145749Smckusick #endif
60245749Smckusick 	VOP_STRATEGY(bp);
60345749Smckusick 	if ((flags & (B_READ|B_ASYNC)) == B_ASYNC) {
60445749Smckusick #ifdef DEBUG
60545749Smckusick 		if (swpagerdebug & SDB_IO)
60645749Smckusick 			printf("swpg_io:  IO started: bp %x\n", bp);
60745749Smckusick #endif
60845749Smckusick 		return(VM_PAGER_PEND);
60945749Smckusick 	}
61045749Smckusick 	s = splbio();
61145749Smckusick #ifdef DEBUG
61245749Smckusick 	if (flags & B_READ)
61345749Smckusick 		swap_pager_piip++;
61445749Smckusick 	else
61545749Smckusick 		swap_pager_poip++;
61645749Smckusick #endif
61745749Smckusick 	while ((bp->b_flags & B_DONE) == 0) {
61845749Smckusick 		assert_wait((int)bp);
61945749Smckusick 		thread_block();
62045749Smckusick 	}
62145749Smckusick #ifdef DEBUG
62245749Smckusick 	if (flags & B_READ)
62345749Smckusick 		--swap_pager_piip;
62445749Smckusick 	else
62545749Smckusick 		--swap_pager_poip;
62645749Smckusick #endif
62745749Smckusick 	rv = (bp->b_flags & B_ERROR) ? VM_PAGER_FAIL : VM_PAGER_OK;
62845749Smckusick 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY);
62945749Smckusick 	bp->av_forw = bswlist.av_forw;
63045749Smckusick 	bswlist.av_forw = bp;
63145749Smckusick 	if (bp->b_vp)
63245749Smckusick 		brelvp(bp);
63345749Smckusick 	if (bswlist.b_flags & B_WANTED) {
63445749Smckusick 		bswlist.b_flags &= ~B_WANTED;
63545749Smckusick 		thread_wakeup((int)&bswlist);
63645749Smckusick 	}
63745749Smckusick 	if ((flags & B_READ) == 0 && rv == VM_PAGER_OK) {
63849289Shibler 		m->clean = TRUE;
63945749Smckusick 		pmap_clear_modify(VM_PAGE_TO_PHYS(m));
64045749Smckusick 	}
64145749Smckusick 	splx(s);
64245749Smckusick #ifdef DEBUG
64345749Smckusick 	if (swpagerdebug & SDB_IO)
64445749Smckusick 		printf("swpg_io:  IO done: bp %x, rv %d\n", bp, rv);
64545749Smckusick 	if ((swpagerdebug & SDB_FAIL) && rv == VM_PAGER_FAIL)
64645749Smckusick 		printf("swpg_io: IO error\n");
64745749Smckusick #endif
64845749Smckusick 	vm_pager_unmap_page(kva);
64945749Smckusick 	return(rv);
65045749Smckusick }
65145749Smckusick 
65245749Smckusick boolean_t
65345749Smckusick swap_pager_clean(m, rw)
65445749Smckusick 	vm_page_t m;
65545749Smckusick 	int rw;
65645749Smckusick {
65745749Smckusick 	register swp_clean_t spc, tspc;
65845749Smckusick 	register int s;
65945749Smckusick 
66045749Smckusick #ifdef DEBUG
66145749Smckusick 	/* save panic time state */
66245749Smckusick 	if ((swpagerdebug & SDB_ANOMPANIC) && panicstr)
66345749Smckusick 		return;
66445749Smckusick 	if (swpagerdebug & SDB_FOLLOW)
66545749Smckusick 		printf("swpg_clean(%x, %d)\n", m, rw);
66645749Smckusick #endif
66748386Skarels 	tspc = NULL;
66845749Smckusick 	for (;;) {
66945749Smckusick 		/*
67045749Smckusick 		 * Look up and removal from inuse list must be done
67145749Smckusick 		 * at splbio() to avoid conflicts with swap_pager_iodone.
67245749Smckusick 		 */
67345749Smckusick 		s = splbio();
67445749Smckusick 		spc = (swp_clean_t) queue_first(&swap_pager_inuse);
67545749Smckusick 		while (!queue_end(&swap_pager_inuse, (queue_entry_t)spc)) {
67645749Smckusick 			if ((spc->spc_flags & SPC_DONE) &&
67745749Smckusick 			    swap_pager_finish(spc)) {
67845749Smckusick 				queue_remove(&swap_pager_inuse, spc,
67945749Smckusick 					     swp_clean_t, spc_list);
68045749Smckusick 				break;
68145749Smckusick 			}
68245749Smckusick 			if (m && m == spc->spc_m) {
68345749Smckusick #ifdef DEBUG
68445749Smckusick 				if (swpagerdebug & SDB_ANOM)
68549289Shibler 					printf("swap_pager_clean: page %x on list, flags %x\n",
68645749Smckusick 					       m, spc->spc_flags);
68745749Smckusick #endif
68845749Smckusick 				tspc = spc;
68945749Smckusick 			}
69045749Smckusick 			spc = (swp_clean_t) queue_next(&spc->spc_list);
69145749Smckusick 		}
69245749Smckusick 
69345749Smckusick 		/*
69445749Smckusick 		 * No operations done, thats all we can do for now.
69545749Smckusick 		 */
69645749Smckusick 		if (queue_end(&swap_pager_inuse, (queue_entry_t)spc))
69745749Smckusick 			break;
69845749Smckusick 		splx(s);
69945749Smckusick 
70045749Smckusick 		/*
70145749Smckusick 		 * The desired page was found to be busy earlier in
70245749Smckusick 		 * the scan but has since completed.
70345749Smckusick 		 */
70445749Smckusick 		if (tspc && tspc == spc) {
70545749Smckusick #ifdef DEBUG
70645749Smckusick 			if (swpagerdebug & SDB_ANOM)
70749289Shibler 				printf("swap_pager_clean: page %x done while looking\n",
70845749Smckusick 				       m);
70945749Smckusick #endif
71048386Skarels 			tspc = NULL;
71145749Smckusick 		}
71245749Smckusick 		spc->spc_flags = SPC_FREE;
71345749Smckusick 		vm_pager_unmap_page(spc->spc_kva);
71445749Smckusick 		queue_enter(&swap_pager_free, spc, swp_clean_t, spc_list);
71545749Smckusick #ifdef DEBUG
71645749Smckusick 		if (swpagerdebug & SDB_WRITE)
71745749Smckusick 			printf("swpg_clean: free spc %x\n", spc);
71845749Smckusick #endif
71945749Smckusick 	}
72049289Shibler #ifdef DEBUG
72145749Smckusick 	/*
72245749Smckusick 	 * If we found that the desired page is already being cleaned
72345749Smckusick 	 * mark it so that swap_pager_iodone() will not set the clean
72445749Smckusick 	 * flag before the pageout daemon has another chance to clean it.
72545749Smckusick 	 */
72645749Smckusick 	if (tspc && rw == B_WRITE) {
72745749Smckusick 		if (swpagerdebug & SDB_ANOM)
72849289Shibler 			printf("swap_pager_clean: page %x on clean list\n",
72949289Shibler 			       tspc);
73045749Smckusick 		tspc->spc_flags |= SPC_DIRTY;
73145749Smckusick 	}
73249289Shibler #endif
73345749Smckusick 	splx(s);
73445749Smckusick 
73545749Smckusick #ifdef DEBUG
73645749Smckusick 	if (swpagerdebug & SDB_WRITE)
73745749Smckusick 		printf("swpg_clean: return %d\n", tspc ? TRUE : FALSE);
73845749Smckusick 	if ((swpagerdebug & SDB_ANOM) && tspc)
73945749Smckusick 		printf("swpg_clean: %s of cleaning page %x\n",
74045749Smckusick 		       rw == B_READ ? "get" : "put", m);
74145749Smckusick #endif
74245749Smckusick 	return(tspc ? TRUE : FALSE);
74345749Smckusick }
74445749Smckusick 
74545749Smckusick swap_pager_finish(spc)
74645749Smckusick 	register swp_clean_t spc;
74745749Smckusick {
74845749Smckusick 	vm_object_t object = spc->spc_m->object;
74945749Smckusick 
75045749Smckusick 	/*
75145749Smckusick 	 * Mark the paging operation as done.
75245749Smckusick 	 * (XXX) If we cannot get the lock, leave it til later.
75345749Smckusick 	 * (XXX) Also we are assuming that an async write is a
75445749Smckusick 	 *       pageout operation that has incremented the counter.
75545749Smckusick 	 */
75645749Smckusick 	if (!vm_object_lock_try(object))
75745749Smckusick 		return(0);
75845749Smckusick 
75945749Smckusick 	if (--object->paging_in_progress == 0)
76045749Smckusick 		thread_wakeup((int) object);
76145749Smckusick 
76249289Shibler #ifdef DEBUG
76345749Smckusick 	/*
76445749Smckusick 	 * XXX: this isn't even close to the right thing to do,
76545749Smckusick 	 * introduces a variety of race conditions.
76645749Smckusick 	 *
76745749Smckusick 	 * If dirty, vm_pageout() has attempted to clean the page
76845749Smckusick 	 * again.  In this case we do not do anything as we will
76949289Shibler 	 * see the page again shortly.
77045749Smckusick 	 */
77149289Shibler 	if (spc->spc_flags & SPC_DIRTY) {
77249289Shibler 		if (swpagerdebug & SDB_ANOM)
77349289Shibler 			printf("swap_pager_finish: page %x dirty again\n",
77449289Shibler 			       spc->spc_m);
77549289Shibler 		spc->spc_m->busy = FALSE;
77649289Shibler 		PAGE_WAKEUP(spc->spc_m);
77749289Shibler 		vm_object_unlock(object);
77849289Shibler 		return(1);
77945749Smckusick 	}
78049289Shibler #endif
78145749Smckusick 	/*
78249289Shibler 	 * If no error mark as clean and inform the pmap system.
78349289Shibler 	 * If error, mark as dirty so we will try again.
78449289Shibler 	 * (XXX could get stuck doing this, should give up after awhile)
78545749Smckusick 	 */
78649289Shibler 	if (spc->spc_flags & SPC_ERROR) {
78749289Shibler 		printf("swap_pager_finish: clean of page %x failed\n",
78849289Shibler 		       VM_PAGE_TO_PHYS(spc->spc_m));
78949289Shibler 		spc->spc_m->laundry = TRUE;
79049289Shibler 	} else {
79149289Shibler 		spc->spc_m->clean = TRUE;
79249289Shibler 		pmap_clear_modify(VM_PAGE_TO_PHYS(spc->spc_m));
79349289Shibler 	}
79449289Shibler 	spc->spc_m->busy = FALSE;
79545749Smckusick 	PAGE_WAKEUP(spc->spc_m);
79645749Smckusick 
79745749Smckusick 	vm_object_unlock(object);
79845749Smckusick 	return(1);
79945749Smckusick }
80045749Smckusick 
80145749Smckusick swap_pager_iodone(bp)
80245749Smckusick 	register struct buf *bp;
80345749Smckusick {
80445749Smckusick 	register swp_clean_t spc;
80545749Smckusick 	daddr_t blk;
80645749Smckusick 	int s;
80745749Smckusick 
80845749Smckusick #ifdef DEBUG
80945749Smckusick 	/* save panic time state */
81045749Smckusick 	if ((swpagerdebug & SDB_ANOMPANIC) && panicstr)
81145749Smckusick 		return;
81245749Smckusick 	if (swpagerdebug & SDB_FOLLOW)
81345749Smckusick 		printf("swpg_iodone(%x)\n", bp);
81445749Smckusick #endif
81545749Smckusick 	s = splbio();
81645749Smckusick 	spc = (swp_clean_t) queue_first(&swap_pager_inuse);
81745749Smckusick 	while (!queue_end(&swap_pager_inuse, (queue_entry_t)spc)) {
81845749Smckusick 		if (spc->spc_bp == bp)
81945749Smckusick 			break;
82045749Smckusick 		spc = (swp_clean_t) queue_next(&spc->spc_list);
82145749Smckusick 	}
82245749Smckusick #ifdef DEBUG
82345749Smckusick 	if (queue_end(&swap_pager_inuse, (queue_entry_t)spc))
82449289Shibler 		panic("swap_pager_iodone: bp not found");
82545749Smckusick #endif
82645749Smckusick 
82745749Smckusick 	spc->spc_flags &= ~SPC_BUSY;
82845749Smckusick 	spc->spc_flags |= SPC_DONE;
82945749Smckusick 	if (bp->b_flags & B_ERROR)
83045749Smckusick 		spc->spc_flags |= SPC_ERROR;
83145749Smckusick 	spc->spc_bp = NULL;
83245749Smckusick 	blk = bp->b_blkno;
83345749Smckusick 
83445749Smckusick #ifdef DEBUG
83545749Smckusick 	--swap_pager_poip;
83645749Smckusick 	if (swpagerdebug & SDB_WRITE)
83745749Smckusick 		printf("swpg_iodone: bp=%x swp=%x flags=%x spc=%x poip=%x\n",
83845749Smckusick 		       bp, spc->spc_swp, spc->spc_swp->sw_flags,
83945749Smckusick 		       spc, spc->spc_swp->sw_poip);
84045749Smckusick #endif
84145749Smckusick 
84245749Smckusick 	spc->spc_swp->sw_poip--;
84345749Smckusick 	if (spc->spc_swp->sw_flags & SW_WANTED) {
84445749Smckusick 		spc->spc_swp->sw_flags &= ~SW_WANTED;
84545749Smckusick 		thread_wakeup((int)spc->spc_swp);
84645749Smckusick 	}
84745749Smckusick 
84845749Smckusick 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY);
84945749Smckusick 	bp->av_forw = bswlist.av_forw;
85045749Smckusick 	bswlist.av_forw = bp;
85145749Smckusick 	if (bp->b_vp)
85245749Smckusick 		brelvp(bp);
85345749Smckusick 	if (bswlist.b_flags & B_WANTED) {
85445749Smckusick 		bswlist.b_flags &= ~B_WANTED;
85545749Smckusick 		thread_wakeup((int)&bswlist);
85645749Smckusick 	}
85745749Smckusick 	thread_wakeup((int) &vm_pages_needed);
85845749Smckusick 	splx(s);
85945749Smckusick }
86045749Smckusick #endif
861