Home | History | Annotate | Line # | Download | only in uvm
uvm_vnode.c revision 1.36
      1 /*	$NetBSD: uvm_vnode.c,v 1.36 2000/11/24 20:34:01 chs Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5  * Copyright (c) 1991, 1993
      6  *      The Regents of the University of California.
      7  * Copyright (c) 1990 University of Utah.
      8  *
      9  * All rights reserved.
     10  *
     11  * This code is derived from software contributed to Berkeley by
     12  * the Systems Programming Group of the University of Utah Computer
     13  * Science Department.
     14  *
     15  * Redistribution and use in source and binary forms, with or without
     16  * modification, are permitted provided that the following conditions
     17  * are met:
     18  * 1. Redistributions of source code must retain the above copyright
     19  *    notice, this list of conditions and the following disclaimer.
     20  * 2. Redistributions in binary form must reproduce the above copyright
     21  *    notice, this list of conditions and the following disclaimer in the
     22  *    documentation and/or other materials provided with the distribution.
     23  * 3. All advertising materials mentioning features or use of this software
     24  *    must display the following acknowledgement:
     25  *      This product includes software developed by Charles D. Cranor,
     26  *	Washington University, the University of California, Berkeley and
     27  *	its contributors.
     28  * 4. Neither the name of the University nor the names of its contributors
     29  *    may be used to endorse or promote products derived from this software
     30  *    without specific prior written permission.
     31  *
     32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  * SUCH DAMAGE.
     43  *
     44  *      @(#)vnode_pager.c       8.8 (Berkeley) 2/13/94
     45  * from: Id: uvm_vnode.c,v 1.1.2.26 1998/02/02 20:38:07 chuck Exp
     46  */
     47 
     48 #include "fs_nfs.h"
     49 #include "opt_uvmhist.h"
     50 
     51 /*
     52  * uvm_vnode.c: the vnode pager.
     53  */
     54 
     55 #include <sys/param.h>
     56 #include <sys/systm.h>
     57 #include <sys/proc.h>
     58 #include <sys/malloc.h>
     59 #include <sys/vnode.h>
     60 #include <sys/disklabel.h>
     61 #include <sys/ioctl.h>
     62 #include <sys/fcntl.h>
     63 #include <sys/conf.h>
     64 
     65 #include <miscfs/specfs/specdev.h>
     66 
     67 #include <uvm/uvm.h>
     68 #include <uvm/uvm_vnode.h>
     69 
     70 /*
     71  * private global data structure
     72  *
     73  * we keep a list of writeable active vnode-backed VM objects for sync op.
     74  * we keep a simpleq of vnodes that are currently being sync'd.
     75  */
     76 
     77 LIST_HEAD(uvn_list_struct, uvm_vnode);
     78 static struct uvn_list_struct uvn_wlist;	/* writeable uvns */
     79 static simple_lock_data_t uvn_wl_lock;		/* locks uvn_wlist */
     80 
     81 SIMPLEQ_HEAD(uvn_sq_struct, uvm_vnode);
     82 static struct uvn_sq_struct uvn_sync_q;		/* sync'ing uvns */
     83 lock_data_t uvn_sync_lock;			/* locks sync operation */
     84 
     85 /*
     86  * functions
     87  */
     88 
     89 static void		   uvn_cluster __P((struct uvm_object *, voff_t,
     90 					   voff_t *, voff_t *));
     91 static void                uvn_detach __P((struct uvm_object *));
     92 static boolean_t           uvn_flush __P((struct uvm_object *, voff_t,
     93 					 voff_t, int));
     94 static int                 uvn_get __P((struct uvm_object *, voff_t,
     95 					vm_page_t *, int *, int,
     96 					vm_prot_t, int, int));
     97 static void		   uvn_init __P((void));
     98 static int		   uvn_io __P((struct uvm_vnode *, vm_page_t *,
     99 				      int, int, int));
    100 static int		   uvn_put __P((struct uvm_object *, vm_page_t *,
    101 					int, boolean_t));
    102 static void                uvn_reference __P((struct uvm_object *));
    103 static boolean_t	   uvn_releasepg __P((struct vm_page *,
    104 					      struct vm_page **));
    105 
    106 /*
    107  * master pager structure
    108  */
    109 
    110 struct uvm_pagerops uvm_vnodeops = {
    111 	uvn_init,
    112 	uvn_reference,
    113 	uvn_detach,
    114 	NULL,			/* no specialized fault routine required */
    115 	uvn_flush,
    116 	uvn_get,
    117 	uvn_put,
    118 	uvn_cluster,
    119 	uvm_mk_pcluster, /* use generic version of this: see uvm_pager.c */
    120 	uvn_releasepg,
    121 };
    122 
    123 /*
    124  * the ops!
    125  */
    126 
    127 /*
    128  * uvn_init
    129  *
    130  * init pager private data structures.
    131  */
    132 
    133 static void
    134 uvn_init()
    135 {
    136 
    137 	LIST_INIT(&uvn_wlist);
    138 	simple_lock_init(&uvn_wl_lock);
    139 	/* note: uvn_sync_q init'd in uvm_vnp_sync() */
    140 	lockinit(&uvn_sync_lock, PVM, "uvnsync", 0, 0);
    141 }
    142 
    143 /*
    144  * uvn_attach
    145  *
    146  * attach a vnode structure to a VM object.  if the vnode is already
    147  * attached, then just bump the reference count by one and return the
    148  * VM object.   if not already attached, attach and return the new VM obj.
    149  * the "accessprot" tells the max access the attaching thread wants to
    150  * our pages.
    151  *
    152  * => caller must _not_ already be holding the lock on the uvm_object.
    153  * => in fact, nothing should be locked so that we can sleep here.
    154  * => note that uvm_object is first thing in vnode structure, so their
    155  *    pointers are equiv.
    156  */
    157 
    158 struct uvm_object *
    159 uvn_attach(arg, accessprot)
    160 	void *arg;
    161 	vm_prot_t accessprot;
    162 {
    163 	struct vnode *vp = arg;
    164 	struct uvm_vnode *uvn = &vp->v_uvm;
    165 	struct vattr vattr;
    166 	int oldflags, result;
    167 	struct partinfo pi;
    168 	u_quad_t used_vnode_size;
    169 	UVMHIST_FUNC("uvn_attach"); UVMHIST_CALLED(maphist);
    170 
    171 	UVMHIST_LOG(maphist, "(vn=0x%x)", arg,0,0,0);
    172 
    173 	used_vnode_size = (u_quad_t)0;	/* XXX gcc -Wuninitialized */
    174 
    175 	/*
    176 	 * first get a lock on the uvn.
    177 	 */
    178 	simple_lock(&uvn->u_obj.vmobjlock);
    179 	while (uvn->u_flags & UVM_VNODE_BLOCKED) {
    180 		printf("uvn_attach: blocked at 0x%p flags 0x%x\n",
    181 		    uvn, uvn->u_flags);
    182 		uvn->u_flags |= UVM_VNODE_WANTED;
    183 		UVMHIST_LOG(maphist, "  SLEEPING on blocked vn",0,0,0,0);
    184 		UVM_UNLOCK_AND_WAIT(uvn, &uvn->u_obj.vmobjlock, FALSE,
    185 		    "uvn_attach", 0);
    186 		simple_lock(&uvn->u_obj.vmobjlock);
    187 		UVMHIST_LOG(maphist,"  WOKE UP",0,0,0,0);
    188 	}
    189 
    190 	/*
    191 	 * if we're mapping a BLK device, make sure it is a disk.
    192 	 */
    193 	if (vp->v_type == VBLK && bdevsw[major(vp->v_rdev)].d_type != D_DISK) {
    194 		simple_unlock(&uvn->u_obj.vmobjlock); /* drop lock */
    195 		UVMHIST_LOG(maphist,"<- done (VBLK not D_DISK!)", 0,0,0,0);
    196 		return(NULL);
    197 	}
    198 
    199 	/*
    200 	 * now we have lock and uvn must not be in a blocked state.
    201 	 * first check to see if it is already active, in which case
    202 	 * we can bump the reference count, check to see if we need to
    203 	 * add it to the writeable list, and then return.
    204 	 */
    205 	if (uvn->u_flags & UVM_VNODE_VALID) {	/* already active? */
    206 
    207 		/* regain VREF if we were persisting */
    208 		if (uvn->u_obj.uo_refs == 0) {
    209 			VREF(vp);
    210 			UVMHIST_LOG(maphist," VREF (reclaim persisting vnode)",
    211 			    0,0,0,0);
    212 		}
    213 		uvn->u_obj.uo_refs++;		/* bump uvn ref! */
    214 
    215 		/* check for new writeable uvn */
    216 		if ((accessprot & VM_PROT_WRITE) != 0 &&
    217 		    (uvn->u_flags & UVM_VNODE_WRITEABLE) == 0) {
    218 			simple_lock(&uvn_wl_lock);
    219 			LIST_INSERT_HEAD(&uvn_wlist, uvn, u_wlist);
    220 			simple_unlock(&uvn_wl_lock);
    221 			/* we are now on wlist! */
    222 			uvn->u_flags |= UVM_VNODE_WRITEABLE;
    223 		}
    224 
    225 		/* unlock and return */
    226 		simple_unlock(&uvn->u_obj.vmobjlock);
    227 		UVMHIST_LOG(maphist,"<- done, refcnt=%d", uvn->u_obj.uo_refs,
    228 		    0, 0, 0);
    229 		return (&uvn->u_obj);
    230 	}
    231 
    232 	/*
    233 	 * need to call VOP_GETATTR() to get the attributes, but that could
    234 	 * block (due to I/O), so we want to unlock the object before calling.
    235 	 * however, we want to keep anyone else from playing with the object
    236 	 * while it is unlocked.   to do this we set UVM_VNODE_ALOCK which
    237 	 * prevents anyone from attaching to the vnode until we are done with
    238 	 * it.
    239 	 */
    240 	uvn->u_flags = UVM_VNODE_ALOCK;
    241 	simple_unlock(&uvn->u_obj.vmobjlock); /* drop lock in case we sleep */
    242 		/* XXX: curproc? */
    243 
    244 	if (vp->v_type == VBLK) {
    245 		/*
    246 		 * We could implement this as a specfs getattr call, but:
    247 		 *
    248 		 *	(1) VOP_GETATTR() would get the file system
    249 		 *	    vnode operation, not the specfs operation.
    250 		 *
    251 		 *	(2) All we want is the size, anyhow.
    252 		 */
    253 		result = (*bdevsw[major(vp->v_rdev)].d_ioctl)(vp->v_rdev,
    254 		    DIOCGPART, (caddr_t)&pi, FREAD, curproc);
    255 		if (result == 0) {
    256 			/* XXX should remember blocksize */
    257 			used_vnode_size = (u_quad_t)pi.disklab->d_secsize *
    258 			    (u_quad_t)pi.part->p_size;
    259 		}
    260 	} else {
    261 		result = VOP_GETATTR(vp, &vattr, curproc->p_ucred, curproc);
    262 		if (result == 0)
    263 			used_vnode_size = vattr.va_size;
    264 	}
    265 
    266 	/* relock object */
    267 	simple_lock(&uvn->u_obj.vmobjlock);
    268 
    269 	if (result != 0) {
    270 		if (uvn->u_flags & UVM_VNODE_WANTED)
    271 			wakeup(uvn);
    272 		uvn->u_flags = 0;
    273 		simple_unlock(&uvn->u_obj.vmobjlock); /* drop lock */
    274 		UVMHIST_LOG(maphist,"<- done (VOP_GETATTR FAILED!)", 0,0,0,0);
    275 		return(NULL);
    276 	}
    277 
    278 	/*
    279 	 * make sure that the newsize fits within a vaddr_t
    280 	 * XXX: need to revise addressing data types
    281 	 */
    282 #ifdef DEBUG
    283 	if (vp->v_type == VBLK)
    284 		printf("used_vnode_size = %qu\n", (long long)used_vnode_size);
    285 #endif
    286 
    287 	/*
    288 	 * now set up the uvn.
    289 	 */
    290 	uvn->u_obj.pgops = &uvm_vnodeops;
    291 	TAILQ_INIT(&uvn->u_obj.memq);
    292 	uvn->u_obj.uo_npages = 0;
    293 	uvn->u_obj.uo_refs = 1;			/* just us... */
    294 	oldflags = uvn->u_flags;
    295 	uvn->u_flags = UVM_VNODE_VALID|UVM_VNODE_CANPERSIST;
    296 	uvn->u_nio = 0;
    297 	uvn->u_size = used_vnode_size;
    298 
    299 	/* if write access, we need to add it to the wlist */
    300 	if (accessprot & VM_PROT_WRITE) {
    301 		simple_lock(&uvn_wl_lock);
    302 		LIST_INSERT_HEAD(&uvn_wlist, uvn, u_wlist);
    303 		simple_unlock(&uvn_wl_lock);
    304 		uvn->u_flags |= UVM_VNODE_WRITEABLE;	/* we are on wlist! */
    305 	}
    306 
    307 	/*
    308 	 * add a reference to the vnode.   this reference will stay as long
    309 	 * as there is a valid mapping of the vnode.   dropped when the
    310 	 * reference count goes to zero [and we either free or persist].
    311 	 */
    312 	VREF(vp);
    313 	simple_unlock(&uvn->u_obj.vmobjlock);
    314 	if (oldflags & UVM_VNODE_WANTED)
    315 		wakeup(uvn);
    316 
    317 	UVMHIST_LOG(maphist,"<- done/VREF, ret 0x%x", &uvn->u_obj,0,0,0);
    318 	return(&uvn->u_obj);
    319 }
    320 
    321 
    322 /*
    323  * uvn_reference
    324  *
    325  * duplicate a reference to a VM object.  Note that the reference
    326  * count must already be at least one (the passed in reference) so
    327  * there is no chance of the uvn being killed or locked out here.
    328  *
    329  * => caller must call with object unlocked.
    330  * => caller must be using the same accessprot as was used at attach time
    331  */
    332 
    333 
    334 static void
    335 uvn_reference(uobj)
    336 	struct uvm_object *uobj;
    337 {
    338 #ifdef DEBUG
    339 	struct uvm_vnode *uvn = (struct uvm_vnode *) uobj;
    340 #endif
    341 	UVMHIST_FUNC("uvn_reference"); UVMHIST_CALLED(maphist);
    342 
    343 	simple_lock(&uobj->vmobjlock);
    344 #ifdef DEBUG
    345 	if ((uvn->u_flags & UVM_VNODE_VALID) == 0) {
    346 		printf("uvn_reference: ref=%d, flags=0x%x\n", uvn->u_flags,
    347 		    uobj->uo_refs);
    348 		panic("uvn_reference: invalid state");
    349 	}
    350 #endif
    351 	uobj->uo_refs++;
    352 	UVMHIST_LOG(maphist, "<- done (uobj=0x%x, ref = %d)",
    353 	uobj, uobj->uo_refs,0,0);
    354 	simple_unlock(&uobj->vmobjlock);
    355 }
    356 
    357 /*
    358  * uvn_detach
    359  *
    360  * remove a reference to a VM object.
    361  *
    362  * => caller must call with object unlocked and map locked.
    363  * => this starts the detach process, but doesn't have to finish it
    364  *    (async i/o could still be pending).
    365  */
    366 static void
    367 uvn_detach(uobj)
    368 	struct uvm_object *uobj;
    369 {
    370 	struct uvm_vnode *uvn;
    371 	struct vnode *vp;
    372 	int oldflags;
    373 	UVMHIST_FUNC("uvn_detach"); UVMHIST_CALLED(maphist);
    374 
    375 	simple_lock(&uobj->vmobjlock);
    376 
    377 	UVMHIST_LOG(maphist,"  (uobj=0x%x)  ref=%d", uobj,uobj->uo_refs,0,0);
    378 	uobj->uo_refs--;			/* drop ref! */
    379 	if (uobj->uo_refs) {			/* still more refs */
    380 		simple_unlock(&uobj->vmobjlock);
    381 		UVMHIST_LOG(maphist, "<- done (rc>0)", 0,0,0,0);
    382 		return;
    383 	}
    384 
    385 	/*
    386 	 * get other pointers ...
    387 	 */
    388 
    389 	uvn = (struct uvm_vnode *) uobj;
    390 	vp = (struct vnode *) uobj;
    391 
    392 	/*
    393 	 * clear VTEXT flag now that there are no mappings left (VTEXT is used
    394 	 * to keep an active text file from being overwritten).
    395 	 */
    396 	vp->v_flag &= ~VTEXT;
    397 
    398 	/*
    399 	 * we just dropped the last reference to the uvn.   see if we can
    400 	 * let it "stick around".
    401 	 */
    402 
    403 	if (uvn->u_flags & UVM_VNODE_CANPERSIST) {
    404 		/* won't block */
    405 		uvn_flush(uobj, 0, 0, PGO_DEACTIVATE|PGO_ALLPAGES);
    406 		simple_unlock(&uobj->vmobjlock);
    407 		vrele(vp);			/* drop vnode reference */
    408 		UVMHIST_LOG(maphist,"<- done/vrele!  (persist)", 0,0,0,0);
    409 		return;
    410 	}
    411 
    412 	/*
    413 	 * its a goner!
    414 	 */
    415 
    416 	UVMHIST_LOG(maphist,"  its a goner (flushing)!", 0,0,0,0);
    417 
    418 	uvn->u_flags |= UVM_VNODE_DYING;
    419 
    420 	/*
    421 	 * even though we may unlock in flush, no one can gain a reference
    422 	 * to us until we clear the "dying" flag [because it blocks
    423 	 * attaches].  we will not do that until after we've disposed of all
    424 	 * the pages with uvn_flush().  note that before the flush the only
    425 	 * pages that could be marked PG_BUSY are ones that are in async
    426 	 * pageout by the daemon.  (there can't be any pending "get"'s
    427 	 * because there are no references to the object).
    428 	 */
    429 
    430 	(void) uvn_flush(uobj, 0, 0, PGO_CLEANIT|PGO_FREE|PGO_ALLPAGES);
    431 
    432 	UVMHIST_LOG(maphist,"  its a goner (done flush)!", 0,0,0,0);
    433 
    434 	/*
    435 	 * given the structure of this pager, the above flush request will
    436 	 * create the following state: all the pages that were in the object
    437 	 * have either been free'd or they are marked PG_BUSY|PG_RELEASED.
    438 	 * the PG_BUSY bit was set either by us or the daemon for async I/O.
    439 	 * in either case, if we have pages left we can't kill the object
    440 	 * yet because i/o is pending.  in this case we set the "relkill"
    441 	 * flag which will cause pgo_releasepg to kill the object once all
    442 	 * the I/O's are done [pgo_releasepg will be called from the aiodone
    443 	 * routine or from the page daemon].
    444 	 */
    445 
    446 	if (uobj->uo_npages) {		/* I/O pending.  iodone will free */
    447 #ifdef DEBUG
    448 		/*
    449 		 * XXXCDC: very unlikely to happen until we have async i/o
    450 		 * so print a little info message in case it does.
    451 		 */
    452 		printf("uvn_detach: vn %p has pages left after flush - "
    453 		    "relkill mode\n", uobj);
    454 #endif
    455 		uvn->u_flags |= UVM_VNODE_RELKILL;
    456 		simple_unlock(&uobj->vmobjlock);
    457 		UVMHIST_LOG(maphist,"<- done! (releasepg will kill obj)", 0, 0,
    458 		    0, 0);
    459 		return;
    460 	}
    461 
    462 	/*
    463 	 * kill object now.   note that we can't be on the sync q because
    464 	 * all references are gone.
    465 	 */
    466 	if (uvn->u_flags & UVM_VNODE_WRITEABLE) {
    467 		simple_lock(&uvn_wl_lock);		/* protect uvn_wlist */
    468 		LIST_REMOVE(uvn, u_wlist);
    469 		simple_unlock(&uvn_wl_lock);
    470 	}
    471 #ifdef DIAGNOSTIC
    472 	if (uobj->memq.tqh_first != NULL)
    473 		panic("uvn_deref: vnode VM object still has pages afer "
    474 		    "syncio/free flush");
    475 #endif
    476 	oldflags = uvn->u_flags;
    477 	uvn->u_flags = 0;
    478 	simple_unlock(&uobj->vmobjlock);
    479 
    480 	/* wake up any sleepers */
    481 	if (oldflags & UVM_VNODE_WANTED)
    482 		wakeup(uvn);
    483 
    484 	/*
    485 	 * drop our reference to the vnode.
    486 	 */
    487 	vrele(vp);
    488 	UVMHIST_LOG(maphist,"<- done (vrele) final", 0,0,0,0);
    489 
    490 	return;
    491 }
    492 
    493 /*
    494  * uvm_vnp_terminate: external hook to clear out a vnode's VM
    495  *
    496  * called in two cases:
    497  *  [1] when a persisting vnode vm object (i.e. one with a zero reference
    498  *      count) needs to be freed so that a vnode can be reused.  this
    499  *      happens under "getnewvnode" in vfs_subr.c.   if the vnode from
    500  *      the free list is still attached (i.e. not VBAD) then vgone is
    501  *	called.   as part of the vgone trace this should get called to
    502  *	free the vm object.   this is the common case.
    503  *  [2] when a filesystem is being unmounted by force (MNT_FORCE,
    504  *	"umount -f") the vgone() function is called on active vnodes
    505  *	on the mounted file systems to kill their data (the vnodes become
    506  *	"dead" ones [see src/sys/miscfs/deadfs/...]).  that results in a
    507  *	call here (even if the uvn is still in use -- i.e. has a non-zero
    508  *	reference count).  this case happens at "umount -f" and during a
    509  *	"reboot/halt" operation.
    510  *
    511  * => the caller must XLOCK and VOP_LOCK the vnode before calling us
    512  *	[protects us from getting a vnode that is already in the DYING
    513  *	 state...]
    514  * => unlike uvn_detach, this function must not return until all the
    515  *	uvn's pages are disposed of.
    516  * => in case [2] the uvn is still alive after this call, but all I/O
    517  *	ops will fail (due to the backing vnode now being "dead").  this
    518  *	will prob. kill any process using the uvn due to pgo_get failing.
    519  */
    520 
    521 void
    522 uvm_vnp_terminate(vp)
    523 	struct vnode *vp;
    524 {
    525 	struct uvm_vnode *uvn = &vp->v_uvm;
    526 	int oldflags;
    527 	UVMHIST_FUNC("uvm_vnp_terminate"); UVMHIST_CALLED(maphist);
    528 
    529 	/*
    530 	 * lock object and check if it is valid
    531 	 */
    532 	simple_lock(&uvn->u_obj.vmobjlock);
    533 	UVMHIST_LOG(maphist, "  vp=0x%x, ref=%d, flag=0x%x", vp,
    534 	    uvn->u_obj.uo_refs, uvn->u_flags, 0);
    535 	if ((uvn->u_flags & UVM_VNODE_VALID) == 0) {
    536 		simple_unlock(&uvn->u_obj.vmobjlock);
    537 		UVMHIST_LOG(maphist, "<- done (not active)", 0, 0, 0, 0);
    538 		return;
    539 	}
    540 
    541 	/*
    542 	 * must be a valid uvn that is not already dying (because XLOCK
    543 	 * protects us from that).   the uvn can't in the ALOCK state
    544 	 * because it is valid, and uvn's that are in the ALOCK state haven't
    545 	 * been marked valid yet.
    546 	 */
    547 
    548 #ifdef DEBUG
    549 	/*
    550 	 * debug check: are we yanking the vnode out from under our uvn?
    551 	 */
    552 	if (uvn->u_obj.uo_refs) {
    553 		printf("uvm_vnp_terminate(%p): terminating active vnode "
    554 		    "(refs=%d)\n", uvn, uvn->u_obj.uo_refs);
    555 	}
    556 #endif
    557 
    558 	/*
    559 	 * it is possible that the uvn was detached and is in the relkill
    560 	 * state [i.e. waiting for async i/o to finish so that releasepg can
    561 	 * kill object].  we take over the vnode now and cancel the relkill.
    562 	 * we want to know when the i/o is done so we can recycle right
    563 	 * away.   note that a uvn can only be in the RELKILL state if it
    564 	 * has a zero reference count.
    565 	 */
    566 
    567 	if (uvn->u_flags & UVM_VNODE_RELKILL)
    568 		uvn->u_flags &= ~UVM_VNODE_RELKILL;	/* cancel RELKILL */
    569 
    570 	/*
    571 	 * block the uvn by setting the dying flag, and then flush the
    572 	 * pages.  (note that flush may unlock object while doing I/O, but
    573 	 * it will re-lock it before it returns control here).
    574 	 *
    575 	 * also, note that we tell I/O that we are already VOP_LOCK'd so
    576 	 * that uvn_io doesn't attempt to VOP_LOCK again.
    577 	 *
    578 	 * XXXCDC: setting VNISLOCKED on an active uvn which is being terminated
    579 	 *	due to a forceful unmount might not be a good idea.  maybe we
    580 	 *	need a way to pass in this info to uvn_flush through a
    581 	 *	pager-defined PGO_ constant [currently there are none].
    582 	 */
    583 	uvn->u_flags |= UVM_VNODE_DYING|UVM_VNODE_VNISLOCKED;
    584 
    585 	(void) uvn_flush(&uvn->u_obj, 0, 0, PGO_CLEANIT|PGO_FREE|PGO_ALLPAGES);
    586 
    587 	/*
    588 	 * as we just did a flush we expect all the pages to be gone or in
    589 	 * the process of going.  sleep to wait for the rest to go [via iosync].
    590 	 */
    591 
    592 	while (uvn->u_obj.uo_npages) {
    593 #ifdef DEBUG
    594 		struct vm_page *pp;
    595 		for (pp = uvn->u_obj.memq.tqh_first ; pp != NULL ;
    596 		     pp = pp->listq.tqe_next) {
    597 			if ((pp->flags & PG_BUSY) == 0)
    598 				panic("uvm_vnp_terminate: detected unbusy pg");
    599 		}
    600 		if (uvn->u_nio == 0)
    601 			panic("uvm_vnp_terminate: no I/O to wait for?");
    602 		printf("uvm_vnp_terminate: waiting for I/O to fin.\n");
    603 		/*
    604 		 * XXXCDC: this is unlikely to happen without async i/o so we
    605 		 * put a printf in just to keep an eye on it.
    606 		 */
    607 #endif
    608 		uvn->u_flags |= UVM_VNODE_IOSYNC;
    609 		UVM_UNLOCK_AND_WAIT(&uvn->u_nio, &uvn->u_obj.vmobjlock, FALSE,
    610 		    "uvn_term",0);
    611 		simple_lock(&uvn->u_obj.vmobjlock);
    612 	}
    613 
    614 	/*
    615 	 * done.   now we free the uvn if its reference count is zero
    616 	 * (true if we are zapping a persisting uvn).   however, if we are
    617 	 * terminating a uvn with active mappings we let it live ... future
    618 	 * calls down to the vnode layer will fail.
    619 	 */
    620 
    621 	oldflags = uvn->u_flags;
    622 	if (uvn->u_obj.uo_refs) {
    623 
    624 		/*
    625 		 * uvn must live on it is dead-vnode state until all references
    626 		 * are gone.   restore flags.    clear CANPERSIST state.
    627 		 */
    628 
    629 		uvn->u_flags &= ~(UVM_VNODE_DYING|UVM_VNODE_VNISLOCKED|
    630 		      UVM_VNODE_WANTED|UVM_VNODE_CANPERSIST);
    631 
    632 	} else {
    633 
    634 		/*
    635 		 * free the uvn now.   note that the VREF reference is already
    636 		 * gone [it is dropped when we enter the persist state].
    637 		 */
    638 		if (uvn->u_flags & UVM_VNODE_IOSYNCWANTED)
    639 			panic("uvm_vnp_terminate: io sync wanted bit set");
    640 
    641 		if (uvn->u_flags & UVM_VNODE_WRITEABLE) {
    642 			simple_lock(&uvn_wl_lock);
    643 			LIST_REMOVE(uvn, u_wlist);
    644 			simple_unlock(&uvn_wl_lock);
    645 		}
    646 		uvn->u_flags = 0;	/* uvn is history, clear all bits */
    647 	}
    648 
    649 	if (oldflags & UVM_VNODE_WANTED)
    650 		wakeup(uvn);		/* object lock still held */
    651 
    652 	simple_unlock(&uvn->u_obj.vmobjlock);
    653 	UVMHIST_LOG(maphist, "<- done", 0, 0, 0, 0);
    654 
    655 }
    656 
    657 /*
    658  * uvn_releasepg: handled a released page in a uvn
    659  *
    660  * => "pg" is a PG_BUSY [caller owns it], PG_RELEASED page that we need
    661  *	to dispose of.
    662  * => caller must handled PG_WANTED case
    663  * => called with page's object locked, pageq's unlocked
    664  * => returns TRUE if page's object is still alive, FALSE if we
    665  *	killed the page's object.    if we return TRUE, then we
    666  *	return with the object locked.
    667  * => if (nextpgp != NULL) => we return pageq.tqe_next here, and return
    668  *				with the page queues locked [for pagedaemon]
    669  * => if (nextpgp == NULL) => we return with page queues unlocked [normal case]
    670  * => we kill the uvn if it is not referenced and we are suppose to
    671  *	kill it ("relkill").
    672  */
    673 
    674 boolean_t
    675 uvn_releasepg(pg, nextpgp)
    676 	struct vm_page *pg;
    677 	struct vm_page **nextpgp;	/* OUT */
    678 {
    679 	struct uvm_vnode *uvn = (struct uvm_vnode *) pg->uobject;
    680 #ifdef DIAGNOSTIC
    681 	if ((pg->flags & PG_RELEASED) == 0)
    682 		panic("uvn_releasepg: page not released!");
    683 #endif
    684 
    685 	/*
    686 	 * dispose of the page [caller handles PG_WANTED]
    687 	 */
    688 	pmap_page_protect(pg, VM_PROT_NONE);
    689 	uvm_lock_pageq();
    690 	if (nextpgp)
    691 		*nextpgp = pg->pageq.tqe_next;	/* next page for daemon */
    692 	uvm_pagefree(pg);
    693 	if (!nextpgp)
    694 		uvm_unlock_pageq();
    695 
    696 	/*
    697 	 * now see if we need to kill the object
    698 	 */
    699 	if (uvn->u_flags & UVM_VNODE_RELKILL) {
    700 		if (uvn->u_obj.uo_refs)
    701 			panic("uvn_releasepg: kill flag set on referenced "
    702 			    "object!");
    703 		if (uvn->u_obj.uo_npages == 0) {
    704 			if (uvn->u_flags & UVM_VNODE_WRITEABLE) {
    705 				simple_lock(&uvn_wl_lock);
    706 				LIST_REMOVE(uvn, u_wlist);
    707 				simple_unlock(&uvn_wl_lock);
    708 			}
    709 #ifdef DIAGNOSTIC
    710 			if (uvn->u_obj.memq.tqh_first)
    711 	panic("uvn_releasepg: pages in object with npages == 0");
    712 #endif
    713 			if (uvn->u_flags & UVM_VNODE_WANTED)
    714 				/* still holding object lock */
    715 				wakeup(uvn);
    716 
    717 			uvn->u_flags = 0;		/* DEAD! */
    718 			simple_unlock(&uvn->u_obj.vmobjlock);
    719 			return (FALSE);
    720 		}
    721 	}
    722 	return (TRUE);
    723 }
    724 
    725 /*
    726  * NOTE: currently we have to use VOP_READ/VOP_WRITE because they go
    727  * through the buffer cache and allow I/O in any size.  These VOPs use
    728  * synchronous i/o.  [vs. VOP_STRATEGY which can be async, but doesn't
    729  * go through the buffer cache or allow I/O sizes larger than a
    730  * block].  we will eventually want to change this.
    731  *
    732  * issues to consider:
    733  *   uvm provides the uvm_aiodesc structure for async i/o management.
    734  * there are two tailq's in the uvm. structure... one for pending async
    735  * i/o and one for "done" async i/o.   to do an async i/o one puts
    736  * an aiodesc on the "pending" list (protected by splbio()), starts the
    737  * i/o and returns VM_PAGER_PEND.    when the i/o is done, we expect
    738  * some sort of "i/o done" function to be called (at splbio(), interrupt
    739  * time).   this function should remove the aiodesc from the pending list
    740  * and place it on the "done" list and wakeup the daemon.   the daemon
    741  * will run at normal spl() and will remove all items from the "done"
    742  * list and call the "aiodone" hook for each done request (see uvm_pager.c).
    743  * [in the old vm code, this was done by calling the "put" routine with
    744  * null arguments which made the code harder to read and understand because
    745  * you had one function ("put") doing two things.]
    746  *
    747  * so the current pager needs:
    748  *   int uvn_aiodone(struct uvm_aiodesc *)
    749  *
    750  * => return KERN_SUCCESS (aio finished, free it).  otherwise requeue for
    751  *	later collection.
    752  * => called with pageq's locked by the daemon.
    753  *
    754  * general outline:
    755  * - "try" to lock object.   if fail, just return (will try again later)
    756  * - drop "u_nio" (this req is done!)
    757  * - if (object->iosync && u_naio == 0) { wakeup &uvn->u_naio }
    758  * - get "page" structures (atop?).
    759  * - handle "wanted" pages
    760  * - handle "released" pages [using pgo_releasepg]
    761  *   >>> pgo_releasepg may kill the object
    762  * dont forget to look at "object" wanted flag in all cases.
    763  */
    764 
    765 
    766 /*
    767  * uvn_flush: flush pages out of a uvm object.
    768  *
    769  * => object should be locked by caller.   we may _unlock_ the object
    770  *	if (and only if) we need to clean a page (PGO_CLEANIT).
    771  *	we return with the object locked.
    772  * => if PGO_CLEANIT is set, we may block (due to I/O).   thus, a caller
    773  *	might want to unlock higher level resources (e.g. vm_map)
    774  *	before calling flush.
    775  * => if PGO_CLEANIT is not set, then we will neither unlock the object
    776  *	or block.
    777  * => if PGO_ALLPAGE is set, then all pages in the object are valid targets
    778  *	for flushing.
    779  * => NOTE: we rely on the fact that the object's memq is a TAILQ and
    780  *	that new pages are inserted on the tail end of the list.   thus,
    781  *	we can make a complete pass through the object in one go by starting
    782  *	at the head and working towards the tail (new pages are put in
    783  *	front of us).
    784  * => NOTE: we are allowed to lock the page queues, so the caller
    785  *	must not be holding the lock on them [e.g. pagedaemon had
    786  *	better not call us with the queues locked]
    787  * => we return TRUE unless we encountered some sort of I/O error
    788  *
    789  * comment on "cleaning" object and PG_BUSY pages:
    790  *	this routine is holding the lock on the object.   the only time
    791  *	that it can run into a PG_BUSY page that it does not own is if
    792  *	some other process has started I/O on the page (e.g. either
    793  *	a pagein, or a pageout).    if the PG_BUSY page is being paged
    794  *	in, then it can not be dirty (!PG_CLEAN) because no one has
    795  *	had a chance to modify it yet.    if the PG_BUSY page is being
    796  *	paged out then it means that someone else has already started
    797  *	cleaning the page for us (how nice!).    in this case, if we
    798  *	have syncio specified, then after we make our pass through the
    799  *	object we need to wait for the other PG_BUSY pages to clear
    800  *	off (i.e. we need to do an iosync).   also note that once a
    801  *	page is PG_BUSY it must stay in its object until it is un-busyed.
    802  *
    803  * note on page traversal:
    804  *	we can traverse the pages in an object either by going down the
    805  *	linked list in "uobj->memq", or we can go over the address range
    806  *	by page doing hash table lookups for each address.    depending
    807  *	on how many pages are in the object it may be cheaper to do one
    808  *	or the other.   we set "by_list" to true if we are using memq.
    809  *	if the cost of a hash lookup was equal to the cost of the list
    810  *	traversal we could compare the number of pages in the start->stop
    811  *	range to the total number of pages in the object.   however, it
    812  *	seems that a hash table lookup is more expensive than the linked
    813  *	list traversal, so we multiply the number of pages in the
    814  *	start->stop range by a penalty which we define below.
    815  */
    816 
    817 #define UVN_HASH_PENALTY 4	/* XXX: a guess */
    818 
    819 static boolean_t
    820 uvn_flush(uobj, start, stop, flags)
    821 	struct uvm_object *uobj;
    822 	voff_t start, stop;
    823 	int flags;
    824 {
    825 	struct uvm_vnode *uvn = (struct uvm_vnode *) uobj;
    826 	struct vm_page *pp, *ppnext, *ptmp;
    827 	struct vm_page *pps[MAXBSIZE >> PAGE_SHIFT], **ppsp;
    828 	int npages, result, lcv;
    829 	boolean_t retval, need_iosync, by_list, needs_clean, all;
    830 	voff_t curoff;
    831 	u_short pp_version;
    832 	UVMHIST_FUNC("uvn_flush"); UVMHIST_CALLED(maphist);
    833 
    834 	curoff = 0;	/* XXX: shut up gcc */
    835 	/*
    836 	 * get init vals and determine how we are going to traverse object
    837 	 */
    838 
    839 	need_iosync = FALSE;
    840 	retval = TRUE;		/* return value */
    841 	if (flags & PGO_ALLPAGES) {
    842 		all = TRUE;
    843 		by_list = TRUE;		/* always go by the list */
    844 	} else {
    845 		start = trunc_page(start);
    846 		stop = round_page(stop);
    847 #ifdef DEBUG
    848 		if (stop > round_page(uvn->u_size))
    849 			printf("uvn_flush: strange, got an out of range "
    850 			    "flush (fixed)\n");
    851 #endif
    852 		all = FALSE;
    853 		by_list = (uobj->uo_npages <=
    854 		    ((stop - start) >> PAGE_SHIFT) * UVN_HASH_PENALTY);
    855 	}
    856 
    857 	UVMHIST_LOG(maphist,
    858 	    " flush start=0x%x, stop=0x%x, by_list=%d, flags=0x%x",
    859 	    start, stop, by_list, flags);
    860 
    861 	/*
    862 	 * PG_CLEANCHK: this bit is used by the pgo_mk_pcluster function as
    863 	 * a _hint_ as to how up to date the PG_CLEAN bit is.   if the hint
    864 	 * is wrong it will only prevent us from clustering... it won't break
    865 	 * anything.   we clear all PG_CLEANCHK bits here, and pgo_mk_pcluster
    866 	 * will set them as it syncs PG_CLEAN.   This is only an issue if we
    867 	 * are looking at non-inactive pages (because inactive page's PG_CLEAN
    868 	 * bit is always up to date since there are no mappings).
    869 	 * [borrowed PG_CLEANCHK idea from FreeBSD VM]
    870 	 */
    871 
    872 	if ((flags & PGO_CLEANIT) != 0 &&
    873 	    uobj->pgops->pgo_mk_pcluster != NULL) {
    874 		if (by_list) {
    875 			for (pp = uobj->memq.tqh_first ; pp != NULL ;
    876 			    pp = pp->listq.tqe_next) {
    877 				if (!all &&
    878 				    (pp->offset < start || pp->offset >= stop))
    879 					continue;
    880 				pp->flags &= ~PG_CLEANCHK;
    881 			}
    882 
    883 		} else {   /* by hash */
    884 			for (curoff = start ; curoff < stop;
    885 			    curoff += PAGE_SIZE) {
    886 				pp = uvm_pagelookup(uobj, curoff);
    887 				if (pp)
    888 					pp->flags &= ~PG_CLEANCHK;
    889 			}
    890 		}
    891 	}
    892 
    893 	/*
    894 	 * now do it.   note: we must update ppnext in body of loop or we
    895 	 * will get stuck.  we need to use ppnext because we may free "pp"
    896 	 * before doing the next loop.
    897 	 */
    898 
    899 	if (by_list) {
    900 		pp = uobj->memq.tqh_first;
    901 	} else {
    902 		curoff = start;
    903 		pp = uvm_pagelookup(uobj, curoff);
    904 	}
    905 
    906 	ppnext = NULL;	/* XXX: shut up gcc */
    907 	ppsp = NULL;		/* XXX: shut up gcc */
    908 	uvm_lock_pageq();	/* page queues locked */
    909 
    910 	/* locked: both page queues and uobj */
    911 	for ( ; (by_list && pp != NULL) ||
    912 	  (!by_list && curoff < stop) ; pp = ppnext) {
    913 
    914 		if (by_list) {
    915 
    916 			/*
    917 			 * range check
    918 			 */
    919 
    920 			if (!all &&
    921 			    (pp->offset < start || pp->offset >= stop)) {
    922 				ppnext = pp->listq.tqe_next;
    923 				continue;
    924 			}
    925 
    926 		} else {
    927 
    928 			/*
    929 			 * null check
    930 			 */
    931 
    932 			curoff += PAGE_SIZE;
    933 			if (pp == NULL) {
    934 				if (curoff < stop)
    935 					ppnext = uvm_pagelookup(uobj, curoff);
    936 				continue;
    937 			}
    938 
    939 		}
    940 
    941 		/*
    942 		 * handle case where we do not need to clean page (either
    943 		 * because we are not clean or because page is not dirty or
    944 		 * is busy):
    945 		 *
    946 		 * NOTE: we are allowed to deactivate a non-wired active
    947 		 * PG_BUSY page, but once a PG_BUSY page is on the inactive
    948 		 * queue it must stay put until it is !PG_BUSY (so as not to
    949 		 * confuse pagedaemon).
    950 		 */
    951 
    952 		if ((flags & PGO_CLEANIT) == 0 || (pp->flags & PG_BUSY) != 0) {
    953 			needs_clean = FALSE;
    954 			if ((pp->flags & PG_BUSY) != 0 &&
    955 			    (flags & (PGO_CLEANIT|PGO_SYNCIO)) ==
    956 			             (PGO_CLEANIT|PGO_SYNCIO))
    957 				need_iosync = TRUE;
    958 		} else {
    959 			/*
    960 			 * freeing: nuke all mappings so we can sync
    961 			 * PG_CLEAN bit with no race
    962 			 */
    963 			if ((pp->flags & PG_CLEAN) != 0 &&
    964 			    (flags & PGO_FREE) != 0 &&
    965 			    (pp->pqflags & PQ_ACTIVE) != 0)
    966 				pmap_page_protect(pp, VM_PROT_NONE);
    967 			if ((pp->flags & PG_CLEAN) != 0 &&
    968 			    pmap_is_modified(pp))
    969 				pp->flags &= ~(PG_CLEAN);
    970 			pp->flags |= PG_CLEANCHK;	/* update "hint" */
    971 
    972 			needs_clean = ((pp->flags & PG_CLEAN) == 0);
    973 		}
    974 
    975 		/*
    976 		 * if we don't need a clean... load ppnext and dispose of pp
    977 		 */
    978 		if (!needs_clean) {
    979 			/* load ppnext */
    980 			if (by_list)
    981 				ppnext = pp->listq.tqe_next;
    982 			else {
    983 				if (curoff < stop)
    984 					ppnext = uvm_pagelookup(uobj, curoff);
    985 			}
    986 
    987 			/* now dispose of pp */
    988 			if (flags & PGO_DEACTIVATE) {
    989 				if ((pp->pqflags & PQ_INACTIVE) == 0 &&
    990 				    pp->wire_count == 0) {
    991 					pmap_page_protect(pp, VM_PROT_NONE);
    992 					uvm_pagedeactivate(pp);
    993 				}
    994 
    995 			} else if (flags & PGO_FREE) {
    996 				if (pp->flags & PG_BUSY) {
    997 					/* release busy pages */
    998 					pp->flags |= PG_RELEASED;
    999 				} else {
   1000 					pmap_page_protect(pp, VM_PROT_NONE);
   1001 					/* removed page from object */
   1002 					uvm_pagefree(pp);
   1003 				}
   1004 			}
   1005 			/* ppnext is valid so we can continue... */
   1006 			continue;
   1007 		}
   1008 
   1009 		/*
   1010 		 * pp points to a page in the locked object that we are
   1011 		 * working on.  if it is !PG_CLEAN,!PG_BUSY and we asked
   1012 		 * for cleaning (PGO_CLEANIT).  we clean it now.
   1013 		 *
   1014 		 * let uvm_pager_put attempted a clustered page out.
   1015 		 * note: locked: uobj and page queues.
   1016 		 */
   1017 
   1018 		pp->flags |= PG_BUSY;	/* we 'own' page now */
   1019 		UVM_PAGE_OWN(pp, "uvn_flush");
   1020 		pmap_page_protect(pp, VM_PROT_READ);
   1021 		pp_version = pp->version;
   1022 ReTry:
   1023 		ppsp = pps;
   1024 		npages = sizeof(pps) / sizeof(struct vm_page *);
   1025 
   1026 		/* locked: page queues, uobj */
   1027 		result = uvm_pager_put(uobj, pp, &ppsp, &npages,
   1028 			   flags | PGO_DOACTCLUST, start, stop);
   1029 		/* unlocked: page queues, uobj */
   1030 
   1031 		/*
   1032 		 * at this point nothing is locked.   if we did an async I/O
   1033 		 * it is remotely possible for the async i/o to complete and
   1034 		 * the page "pp" be freed or what not before we get a chance
   1035 		 * to relock the object.   in order to detect this, we have
   1036 		 * saved the version number of the page in "pp_version".
   1037 		 */
   1038 
   1039 		/* relock! */
   1040 		simple_lock(&uobj->vmobjlock);
   1041 		uvm_lock_pageq();
   1042 
   1043 		/*
   1044 		 * VM_PAGER_AGAIN: given the structure of this pager, this
   1045 		 * can only happen when  we are doing async I/O and can't
   1046 		 * map the pages into kernel memory (pager_map) due to lack
   1047 		 * of vm space.   if this happens we drop back to sync I/O.
   1048 		 */
   1049 
   1050 		if (result == VM_PAGER_AGAIN) {
   1051 			/*
   1052 			 * it is unlikely, but page could have been released
   1053 			 * while we had the object lock dropped.   we ignore
   1054 			 * this now and retry the I/O.  we will detect and
   1055 			 * handle the released page after the syncio I/O
   1056 			 * completes.
   1057 			 */
   1058 #ifdef DIAGNOSTIC
   1059 			if (flags & PGO_SYNCIO)
   1060 	panic("uvn_flush: PGO_SYNCIO return 'try again' error (impossible)");
   1061 #endif
   1062 			flags |= PGO_SYNCIO;
   1063 			goto ReTry;
   1064 		}
   1065 
   1066 		/*
   1067 		 * the cleaning operation is now done.   finish up.  note that
   1068 		 * on error (!OK, !PEND) uvm_pager_put drops the cluster for us.
   1069 		 * if success (OK, PEND) then uvm_pager_put returns the cluster
   1070 		 * to us in ppsp/npages.
   1071 		 */
   1072 
   1073 		/*
   1074 		 * for pending async i/o if we are not deactivating/freeing
   1075 		 * we can move on to the next page.
   1076 		 */
   1077 
   1078 		if (result == VM_PAGER_PEND) {
   1079 
   1080 			if ((flags & (PGO_DEACTIVATE|PGO_FREE)) == 0) {
   1081 				/*
   1082 				 * no per-page ops: refresh ppnext and continue
   1083 				 */
   1084 				if (by_list) {
   1085 					if (pp->version == pp_version)
   1086 						ppnext = pp->listq.tqe_next;
   1087 					else
   1088 						/* reset */
   1089 						ppnext = uobj->memq.tqh_first;
   1090 				} else {
   1091 					if (curoff < stop)
   1092 						ppnext = uvm_pagelookup(uobj,
   1093 						    curoff);
   1094 				}
   1095 				continue;
   1096 			}
   1097 
   1098 			/* need to do anything here? */
   1099 		}
   1100 
   1101 		/*
   1102 		 * need to look at each page of the I/O operation.  we defer
   1103 		 * processing "pp" until the last trip through this "for" loop
   1104 		 * so that we can load "ppnext" for the main loop after we
   1105 		 * play with the cluster pages [thus the "npages + 1" in the
   1106 		 * loop below].
   1107 		 */
   1108 
   1109 		for (lcv = 0 ; lcv < npages + 1 ; lcv++) {
   1110 
   1111 			/*
   1112 			 * handle ppnext for outside loop, and saving pp
   1113 			 * until the end.
   1114 			 */
   1115 			if (lcv < npages) {
   1116 				if (ppsp[lcv] == pp)
   1117 					continue; /* skip pp until the end */
   1118 				ptmp = ppsp[lcv];
   1119 			} else {
   1120 				ptmp = pp;
   1121 
   1122 				/* set up next page for outer loop */
   1123 				if (by_list) {
   1124 					if (pp->version == pp_version)
   1125 						ppnext = pp->listq.tqe_next;
   1126 					else
   1127 						/* reset */
   1128 						ppnext = uobj->memq.tqh_first;
   1129 				} else {
   1130 					if (curoff < stop)
   1131 					ppnext = uvm_pagelookup(uobj, curoff);
   1132 				}
   1133 			}
   1134 
   1135 			/*
   1136 			 * verify the page didn't get moved while obj was
   1137 			 * unlocked
   1138 			 */
   1139 			if (result == VM_PAGER_PEND && ptmp->uobject != uobj)
   1140 				continue;
   1141 
   1142 			/*
   1143 			 * unbusy the page if I/O is done.   note that for
   1144 			 * pending I/O it is possible that the I/O op
   1145 			 * finished before we relocked the object (in
   1146 			 * which case the page is no longer busy).
   1147 			 */
   1148 
   1149 			if (result != VM_PAGER_PEND) {
   1150 				if (ptmp->flags & PG_WANTED)
   1151 					/* still holding object lock */
   1152 					wakeup(ptmp);
   1153 
   1154 				ptmp->flags &= ~(PG_WANTED|PG_BUSY);
   1155 				UVM_PAGE_OWN(ptmp, NULL);
   1156 				if (ptmp->flags & PG_RELEASED) {
   1157 
   1158 					/* pgo_releasepg wants this */
   1159 					uvm_unlock_pageq();
   1160 					if (!uvn_releasepg(ptmp, NULL))
   1161 						return (TRUE);
   1162 
   1163 					uvm_lock_pageq();	/* relock */
   1164 					continue;		/* next page */
   1165 
   1166 				} else {
   1167 					ptmp->flags |= (PG_CLEAN|PG_CLEANCHK);
   1168 					if ((flags & PGO_FREE) == 0)
   1169 						pmap_clear_modify(ptmp);
   1170 				}
   1171 			}
   1172 
   1173 			/*
   1174 			 * dispose of page
   1175 			 */
   1176 
   1177 			if (flags & PGO_DEACTIVATE) {
   1178 				if ((pp->pqflags & PQ_INACTIVE) == 0 &&
   1179 				    pp->wire_count == 0) {
   1180 					pmap_page_protect(ptmp, VM_PROT_NONE);
   1181 					uvm_pagedeactivate(ptmp);
   1182 				}
   1183 
   1184 			} else if (flags & PGO_FREE) {
   1185 				if (result == VM_PAGER_PEND) {
   1186 					if ((ptmp->flags & PG_BUSY) != 0)
   1187 						/* signal for i/o done */
   1188 						ptmp->flags |= PG_RELEASED;
   1189 				} else {
   1190 					if (result != VM_PAGER_OK) {
   1191 						printf("uvn_flush: obj=%p, "
   1192 						   "offset=0x%llx.  error "
   1193 						   "during pageout.\n",
   1194 						    pp->uobject,
   1195 						    (long long)pp->offset);
   1196 						printf("uvn_flush: WARNING: "
   1197 						    "changes to page may be "
   1198 						    "lost!\n");
   1199 						retval = FALSE;
   1200 					}
   1201 					pmap_page_protect(ptmp, VM_PROT_NONE);
   1202 					uvm_pagefree(ptmp);
   1203 				}
   1204 			}
   1205 
   1206 		}		/* end of "lcv" for loop */
   1207 
   1208 	}		/* end of "pp" for loop */
   1209 
   1210 	/*
   1211 	 * done with pagequeues: unlock
   1212 	 */
   1213 	uvm_unlock_pageq();
   1214 
   1215 	/*
   1216 	 * now wait for all I/O if required.
   1217 	 */
   1218 	if (need_iosync) {
   1219 
   1220 		UVMHIST_LOG(maphist,"  <<DOING IOSYNC>>",0,0,0,0);
   1221 		while (uvn->u_nio != 0) {
   1222 			uvn->u_flags |= UVM_VNODE_IOSYNC;
   1223 			UVM_UNLOCK_AND_WAIT(&uvn->u_nio, &uvn->u_obj.vmobjlock,
   1224 			  FALSE, "uvn_flush",0);
   1225 			simple_lock(&uvn->u_obj.vmobjlock);
   1226 		}
   1227 		if (uvn->u_flags & UVM_VNODE_IOSYNCWANTED)
   1228 			wakeup(&uvn->u_flags);
   1229 		uvn->u_flags &= ~(UVM_VNODE_IOSYNC|UVM_VNODE_IOSYNCWANTED);
   1230 	}
   1231 
   1232 	/* return, with object locked! */
   1233 	UVMHIST_LOG(maphist,"<- done (retval=0x%x)",retval,0,0,0);
   1234 	return(retval);
   1235 }
   1236 
   1237 /*
   1238  * uvn_cluster
   1239  *
   1240  * we are about to do I/O in an object at offset.   this function is called
   1241  * to establish a range of offsets around "offset" in which we can cluster
   1242  * I/O.
   1243  *
   1244  * - currently doesn't matter if obj locked or not.
   1245  */
   1246 
   1247 static void
   1248 uvn_cluster(uobj, offset, loffset, hoffset)
   1249 	struct uvm_object *uobj;
   1250 	voff_t offset;
   1251 	voff_t *loffset, *hoffset; /* OUT */
   1252 {
   1253 	struct uvm_vnode *uvn = (struct uvm_vnode *) uobj;
   1254 	*loffset = offset;
   1255 
   1256 	if (*loffset >= uvn->u_size)
   1257 		panic("uvn_cluster: offset out of range");
   1258 
   1259 	/*
   1260 	 * XXX: old pager claims we could use VOP_BMAP to get maxcontig value.
   1261 	 */
   1262 	*hoffset = *loffset + MAXBSIZE;
   1263 	if (*hoffset > round_page(uvn->u_size))	/* past end? */
   1264 		*hoffset = round_page(uvn->u_size);
   1265 
   1266 	return;
   1267 }
   1268 
   1269 /*
   1270  * uvn_put: flush page data to backing store.
   1271  *
   1272  * => prefer map unlocked (not required)
   1273  * => object must be locked!   we will _unlock_ it before starting I/O.
   1274  * => flags: PGO_SYNCIO -- use sync. I/O
   1275  * => note: caller must set PG_CLEAN and pmap_clear_modify (if needed)
   1276  * => XXX: currently we use VOP_READ/VOP_WRITE which are only sync.
   1277  *	[thus we never do async i/o!  see iodone comment]
   1278  */
   1279 
   1280 static int
   1281 uvn_put(uobj, pps, npages, flags)
   1282 	struct uvm_object *uobj;
   1283 	struct vm_page **pps;
   1284 	int npages, flags;
   1285 {
   1286 	int retval;
   1287 
   1288 	/* note: object locked */
   1289 	retval = uvn_io((struct uvm_vnode*)uobj, pps, npages, flags, UIO_WRITE);
   1290 	/* note: object unlocked */
   1291 
   1292 	return(retval);
   1293 }
   1294 
   1295 
   1296 /*
   1297  * uvn_get: get pages (synchronously) from backing store
   1298  *
   1299  * => prefer map unlocked (not required)
   1300  * => object must be locked!  we will _unlock_ it before starting any I/O.
   1301  * => flags: PGO_ALLPAGES: get all of the pages
   1302  *           PGO_LOCKED: fault data structures are locked
   1303  * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx]
   1304  * => NOTE: caller must check for released pages!!
   1305  */
   1306 
   1307 static int
   1308 uvn_get(uobj, offset, pps, npagesp, centeridx, access_type, advice, flags)
   1309 	struct uvm_object *uobj;
   1310 	voff_t offset;
   1311 	struct vm_page **pps;		/* IN/OUT */
   1312 	int *npagesp;			/* IN (OUT if PGO_LOCKED) */
   1313 	int centeridx, advice, flags;
   1314 	vm_prot_t access_type;
   1315 {
   1316 	voff_t current_offset;
   1317 	struct vm_page *ptmp;
   1318 	int lcv, result, gotpages;
   1319 	boolean_t done;
   1320 	UVMHIST_FUNC("uvn_get"); UVMHIST_CALLED(maphist);
   1321 	UVMHIST_LOG(maphist, "flags=%d", flags,0,0,0);
   1322 
   1323 	/*
   1324 	 * step 1: handled the case where fault data structures are locked.
   1325 	 */
   1326 
   1327 	if (flags & PGO_LOCKED) {
   1328 
   1329 		/*
   1330 		 * gotpages is the current number of pages we've gotten (which
   1331 		 * we pass back up to caller via *npagesp.
   1332 		 */
   1333 
   1334 		gotpages = 0;
   1335 
   1336 		/*
   1337 		 * step 1a: get pages that are already resident.   only do this
   1338 		 * if the data structures are locked (i.e. the first time
   1339 		 * through).
   1340 		 */
   1341 
   1342 		done = TRUE;	/* be optimistic */
   1343 
   1344 		for (lcv = 0, current_offset = offset ; lcv < *npagesp ;
   1345 		    lcv++, current_offset += PAGE_SIZE) {
   1346 
   1347 			/* do we care about this page?  if not, skip it */
   1348 			if (pps[lcv] == PGO_DONTCARE)
   1349 				continue;
   1350 
   1351 			/* lookup page */
   1352 			ptmp = uvm_pagelookup(uobj, current_offset);
   1353 
   1354 			/* to be useful must get a non-busy, non-released pg */
   1355 			if (ptmp == NULL ||
   1356 			    (ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
   1357 				if (lcv == centeridx || (flags & PGO_ALLPAGES)
   1358 				    != 0)
   1359 				done = FALSE;	/* need to do a wait or I/O! */
   1360 				continue;
   1361 			}
   1362 
   1363 			/*
   1364 			 * useful page: busy/lock it and plug it in our
   1365 			 * result array
   1366 			 */
   1367 			ptmp->flags |= PG_BUSY;		/* loan up to caller */
   1368 			UVM_PAGE_OWN(ptmp, "uvn_get1");
   1369 			pps[lcv] = ptmp;
   1370 			gotpages++;
   1371 
   1372 		}	/* "for" lcv loop */
   1373 
   1374 		/*
   1375 		 * XXX: given the "advice", should we consider async read-ahead?
   1376 		 * XXX: fault current does deactive of pages behind us.  is
   1377 		 * this good (other callers might now).
   1378 		 */
   1379 		/*
   1380 		 * XXX: read-ahead currently handled by buffer cache (bread)
   1381 		 * level.
   1382 		 * XXX: no async i/o available.
   1383 		 * XXX: so we don't do anything now.
   1384 		 */
   1385 
   1386 		/*
   1387 		 * step 1c: now we've either done everything needed or we to
   1388 		 * unlock and do some waiting or I/O.
   1389 		 */
   1390 
   1391 		*npagesp = gotpages;		/* let caller know */
   1392 		if (done)
   1393 			return(VM_PAGER_OK);		/* bingo! */
   1394 		else
   1395 			/* EEK!   Need to unlock and I/O */
   1396 			return(VM_PAGER_UNLOCK);
   1397 	}
   1398 
   1399 	/*
   1400 	 * step 2: get non-resident or busy pages.
   1401 	 * object is locked.   data structures are unlocked.
   1402 	 *
   1403 	 * XXX: because we can't do async I/O at this level we get things
   1404 	 * page at a time (otherwise we'd chunk).   the VOP_READ() will do
   1405 	 * async-read-ahead for us at a lower level.
   1406 	 */
   1407 
   1408 	for (lcv = 0, current_offset = offset ;
   1409 			 lcv < *npagesp ; lcv++, current_offset += PAGE_SIZE) {
   1410 
   1411 		/* skip over pages we've already gotten or don't want */
   1412 		/* skip over pages we don't _have_ to get */
   1413 		if (pps[lcv] != NULL || (lcv != centeridx &&
   1414 		    (flags & PGO_ALLPAGES) == 0))
   1415 			continue;
   1416 
   1417 		/*
   1418 		 * we have yet to locate the current page (pps[lcv]).   we first
   1419 		 * look for a page that is already at the current offset.   if
   1420 		 * we fine a page, we check to see if it is busy or released.
   1421 		 * if that is the case, then we sleep on the page until it is
   1422 		 * no longer busy or released and repeat the lookup.    if the
   1423 		 * page we found is neither busy nor released, then we busy it
   1424 		 * (so we own it) and plug it into pps[lcv].   this breaks the
   1425 		 * following while loop and indicates we are ready to move on
   1426 		 * to the next page in the "lcv" loop above.
   1427 		 *
   1428 		 * if we exit the while loop with pps[lcv] still set to NULL,
   1429 		 * then it means that we allocated a new busy/fake/clean page
   1430 		 * ptmp in the object and we need to do I/O to fill in the data.
   1431 		 */
   1432 
   1433 		while (pps[lcv] == NULL) {	/* top of "pps" while loop */
   1434 
   1435 			/* look for a current page */
   1436 			ptmp = uvm_pagelookup(uobj, current_offset);
   1437 
   1438 			/* nope?   allocate one now (if we can) */
   1439 			if (ptmp == NULL) {
   1440 
   1441 				ptmp = uvm_pagealloc(uobj, current_offset,
   1442 				    NULL, 0);
   1443 
   1444 				/* out of RAM? */
   1445 				if (ptmp == NULL) {
   1446 					simple_unlock(&uobj->vmobjlock);
   1447 					uvm_wait("uvn_getpage");
   1448 					simple_lock(&uobj->vmobjlock);
   1449 
   1450 					/* goto top of pps while loop */
   1451 					continue;
   1452 				}
   1453 
   1454 				/*
   1455 				 * got new page ready for I/O.  break pps
   1456 				 * while loop.  pps[lcv] is still NULL.
   1457 				 */
   1458 				break;
   1459 			}
   1460 
   1461 			/* page is there, see if we need to wait on it */
   1462 			if ((ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
   1463 				ptmp->flags |= PG_WANTED;
   1464 				UVM_UNLOCK_AND_WAIT(ptmp,
   1465 				    &uobj->vmobjlock, FALSE, "uvn_get",0);
   1466 				simple_lock(&uobj->vmobjlock);
   1467 				continue;	/* goto top of pps while loop */
   1468 			}
   1469 
   1470 			/*
   1471 			 * if we get here then the page has become resident
   1472 			 * and unbusy between steps 1 and 2.  we busy it
   1473 			 * now (so we own it) and set pps[lcv] (so that we
   1474 			 * exit the while loop).
   1475 			 */
   1476 			ptmp->flags |= PG_BUSY;
   1477 			UVM_PAGE_OWN(ptmp, "uvn_get2");
   1478 			pps[lcv] = ptmp;
   1479 		}
   1480 
   1481 		/*
   1482 		 * if we own the a valid page at the correct offset, pps[lcv]
   1483 		 * will point to it.   nothing more to do except go to the
   1484 		 * next page.
   1485 		 */
   1486 
   1487 		if (pps[lcv])
   1488 			continue;			/* next lcv */
   1489 
   1490 		/*
   1491 		 * we have a "fake/busy/clean" page that we just allocated.  do
   1492 		 * I/O to fill it with valid data.  note that object must be
   1493 		 * locked going into uvn_io, but will be unlocked afterwards.
   1494 		 */
   1495 
   1496 		result = uvn_io((struct uvm_vnode *) uobj, &ptmp, 1,
   1497 		    PGO_SYNCIO, UIO_READ);
   1498 
   1499 		/*
   1500 		 * I/O done.   object is unlocked (by uvn_io).   because we used
   1501 		 * syncio the result can not be PEND or AGAIN.   we must relock
   1502 		 * and check for errors.
   1503 		 */
   1504 
   1505 		/* lock object.   check for errors.   */
   1506 		simple_lock(&uobj->vmobjlock);
   1507 		if (result != VM_PAGER_OK) {
   1508 			if (ptmp->flags & PG_WANTED)
   1509 				/* object lock still held */
   1510 				wakeup(ptmp);
   1511 
   1512 			ptmp->flags &= ~(PG_WANTED|PG_BUSY);
   1513 			UVM_PAGE_OWN(ptmp, NULL);
   1514 			uvm_lock_pageq();
   1515 			uvm_pagefree(ptmp);
   1516 			uvm_unlock_pageq();
   1517 			simple_unlock(&uobj->vmobjlock);
   1518 			return(result);
   1519 		}
   1520 
   1521 		/*
   1522 		 * we got the page!   clear the fake flag (indicates valid
   1523 		 * data now in page) and plug into our result array.   note
   1524 		 * that page is still busy.
   1525 		 *
   1526 		 * it is the callers job to:
   1527 		 * => check if the page is released
   1528 		 * => unbusy the page
   1529 		 * => activate the page
   1530 		 */
   1531 
   1532 		ptmp->flags &= ~PG_FAKE;		/* data is valid ... */
   1533 		pmap_clear_modify(ptmp);		/* ... and clean */
   1534 		pps[lcv] = ptmp;
   1535 
   1536 	}	/* lcv loop */
   1537 
   1538 	/*
   1539 	 * finally, unlock object and return.
   1540 	 */
   1541 
   1542 	simple_unlock(&uobj->vmobjlock);
   1543 	return (VM_PAGER_OK);
   1544 }
   1545 
   1546 /*
   1547  * uvn_io: do I/O to a vnode
   1548  *
   1549  * => prefer map unlocked (not required)
   1550  * => object must be locked!   we will _unlock_ it before starting I/O.
   1551  * => flags: PGO_SYNCIO -- use sync. I/O
   1552  * => XXX: currently we use VOP_READ/VOP_WRITE which are only sync.
   1553  *	[thus we never do async i/o!  see iodone comment]
   1554  */
   1555 
   1556 static int
   1557 uvn_io(uvn, pps, npages, flags, rw)
   1558 	struct uvm_vnode *uvn;
   1559 	vm_page_t *pps;
   1560 	int npages, flags, rw;
   1561 {
   1562 	struct vnode *vn;
   1563 	struct uio uio;
   1564 	struct iovec iov;
   1565 	vaddr_t kva;
   1566 	off_t file_offset;
   1567 	int waitf, result, mapinflags;
   1568 	size_t got, wanted;
   1569 	UVMHIST_FUNC("uvn_io"); UVMHIST_CALLED(maphist);
   1570 
   1571 	UVMHIST_LOG(maphist, "rw=%d", rw,0,0,0);
   1572 
   1573 	/*
   1574 	 * init values
   1575 	 */
   1576 
   1577 	waitf = (flags & PGO_SYNCIO) ? M_WAITOK : M_NOWAIT;
   1578 	vn = (struct vnode *) uvn;
   1579 	file_offset = pps[0]->offset;
   1580 
   1581 	/*
   1582 	 * check for sync'ing I/O.
   1583 	 */
   1584 
   1585 	while (uvn->u_flags & UVM_VNODE_IOSYNC) {
   1586 		if (waitf == M_NOWAIT) {
   1587 			simple_unlock(&uvn->u_obj.vmobjlock);
   1588 			UVMHIST_LOG(maphist,"<- try again (iosync)",0,0,0,0);
   1589 			return(VM_PAGER_AGAIN);
   1590 		}
   1591 		uvn->u_flags |= UVM_VNODE_IOSYNCWANTED;
   1592 		UVM_UNLOCK_AND_WAIT(&uvn->u_flags, &uvn->u_obj.vmobjlock,
   1593 			FALSE, "uvn_iosync",0);
   1594 		simple_lock(&uvn->u_obj.vmobjlock);
   1595 	}
   1596 
   1597 	/*
   1598 	 * check size
   1599 	 */
   1600 
   1601 	if (file_offset >= uvn->u_size) {
   1602 			simple_unlock(&uvn->u_obj.vmobjlock);
   1603 			UVMHIST_LOG(maphist,"<- BAD (size check)",0,0,0,0);
   1604 			return(VM_PAGER_BAD);
   1605 	}
   1606 
   1607 	/*
   1608 	 * first try and map the pages in (without waiting)
   1609 	 */
   1610 
   1611 	mapinflags = (rw == UIO_READ) ?
   1612 	    UVMPAGER_MAPIN_READ : UVMPAGER_MAPIN_WRITE;
   1613 
   1614 	kva = uvm_pagermapin(pps, npages, NULL, mapinflags);
   1615 	if (kva == 0 && waitf == M_NOWAIT) {
   1616 		simple_unlock(&uvn->u_obj.vmobjlock);
   1617 		UVMHIST_LOG(maphist,"<- mapin failed (try again)",0,0,0,0);
   1618 		return(VM_PAGER_AGAIN);
   1619 	}
   1620 
   1621 	/*
   1622 	 * ok, now bump u_nio up.   at this point we are done with uvn
   1623 	 * and can unlock it.   if we still don't have a kva, try again
   1624 	 * (this time with sleep ok).
   1625 	 */
   1626 
   1627 	uvn->u_nio++;			/* we have an I/O in progress! */
   1628 	simple_unlock(&uvn->u_obj.vmobjlock);
   1629 	/* NOTE: object now unlocked */
   1630 	if (kva == 0)
   1631 		kva = uvm_pagermapin(pps, npages, NULL,
   1632 		    mapinflags | UVMPAGER_MAPIN_WAITOK);
   1633 
   1634 	/*
   1635 	 * ok, mapped in.  our pages are PG_BUSY so they are not going to
   1636 	 * get touched (so we can look at "offset" without having to lock
   1637 	 * the object).  set up for I/O.
   1638 	 */
   1639 
   1640 	/*
   1641 	 * fill out uio/iov
   1642 	 */
   1643 
   1644 	iov.iov_base = (caddr_t) kva;
   1645 	wanted = npages << PAGE_SHIFT;
   1646 	if (file_offset + wanted > uvn->u_size)
   1647 		wanted = uvn->u_size - file_offset;	/* XXX: needed? */
   1648 	iov.iov_len = wanted;
   1649 	uio.uio_iov = &iov;
   1650 	uio.uio_iovcnt = 1;
   1651 	uio.uio_offset = file_offset;
   1652 	uio.uio_segflg = UIO_SYSSPACE;
   1653 	uio.uio_rw = rw;
   1654 	uio.uio_resid = wanted;
   1655 	uio.uio_procp = NULL;
   1656 
   1657 	/*
   1658 	 * do the I/O!  (XXX: curproc?)
   1659 	 */
   1660 
   1661 	UVMHIST_LOG(maphist, "calling VOP",0,0,0,0);
   1662 
   1663 	/*
   1664 	 * This process may already have this vnode locked, if we faulted in
   1665 	 * copyin() or copyout() on a region backed by this vnode
   1666 	 * while doing I/O to the vnode.  If this is the case, don't
   1667 	 * panic.. instead, return the error to the user.
   1668 	 *
   1669 	 * XXX this is a stopgap to prevent a panic.
   1670 	 * Ideally, this kind of operation *should* work.
   1671 	 */
   1672 	result = 0;
   1673 	if ((uvn->u_flags & UVM_VNODE_VNISLOCKED) == 0)
   1674 		result = vn_lock(vn, LK_EXCLUSIVE | LK_RETRY | LK_RECURSEFAIL);
   1675 
   1676 	if (result == 0) {
   1677 		/* NOTE: vnode now locked! */
   1678 
   1679 		if (rw == UIO_READ)
   1680 			result = VOP_READ(vn, &uio, 0, curproc->p_ucred);
   1681 		else
   1682 			result = VOP_WRITE(vn, &uio, 0, curproc->p_ucred);
   1683 
   1684 		if ((uvn->u_flags & UVM_VNODE_VNISLOCKED) == 0)
   1685 			VOP_UNLOCK(vn, 0);
   1686 	}
   1687 
   1688 	/* NOTE: vnode now unlocked (unless vnislocked) */
   1689 
   1690 	UVMHIST_LOG(maphist, "done calling VOP",0,0,0,0);
   1691 
   1692 	/*
   1693 	 * result == unix style errno (0 == OK!)
   1694 	 *
   1695 	 * zero out rest of buffer (if needed)
   1696 	 */
   1697 
   1698 	if (result == 0) {
   1699 		got = wanted - uio.uio_resid;
   1700 
   1701 		if (wanted && got == 0) {
   1702 			result = EIO;		/* XXX: error? */
   1703 		} else if (got < PAGE_SIZE * npages && rw == UIO_READ) {
   1704 			memset((void *) (kva + got), 0,
   1705 			       (npages << PAGE_SHIFT) - got);
   1706 		}
   1707 	}
   1708 
   1709 	/*
   1710 	 * now remove pager mapping
   1711 	 */
   1712 	uvm_pagermapout(kva, npages);
   1713 
   1714 	/*
   1715 	 * now clean up the object (i.e. drop I/O count)
   1716 	 */
   1717 
   1718 	simple_lock(&uvn->u_obj.vmobjlock);
   1719 	/* NOTE: object now locked! */
   1720 
   1721 	uvn->u_nio--;			/* I/O DONE! */
   1722 	if ((uvn->u_flags & UVM_VNODE_IOSYNC) != 0 && uvn->u_nio == 0) {
   1723 		wakeup(&uvn->u_nio);
   1724 	}
   1725 	simple_unlock(&uvn->u_obj.vmobjlock);
   1726 	/* NOTE: object now unlocked! */
   1727 
   1728 	/*
   1729 	 * done!
   1730 	 */
   1731 
   1732 	UVMHIST_LOG(maphist, "<- done (result %d)", result,0,0,0);
   1733 	if (result == 0)
   1734 		return(VM_PAGER_OK);
   1735 	else
   1736 		return(VM_PAGER_ERROR);
   1737 }
   1738 
   1739 /*
   1740  * uvm_vnp_uncache: disable "persisting" in a vnode... when last reference
   1741  * is gone we will kill the object (flushing dirty pages back to the vnode
   1742  * if needed).
   1743  *
   1744  * => returns TRUE if there was no uvm_object attached or if there was
   1745  *	one and we killed it [i.e. if there is no active uvn]
   1746  * => called with the vnode VOP_LOCK'd [we will unlock it for I/O, if
   1747  *	needed]
   1748  *
   1749  * => XXX: given that we now kill uvn's when a vnode is recycled (without
   1750  *	having to hold a reference on the vnode) and given a working
   1751  *	uvm_vnp_sync(), how does that effect the need for this function?
   1752  *      [XXXCDC: seems like it can die?]
   1753  *
   1754  * => XXX: this function should DIE once we merge the VM and buffer
   1755  *	cache.
   1756  *
   1757  * research shows that this is called in the following places:
   1758  * ext2fs_truncate, ffs_truncate, detrunc[msdosfs]: called when vnode
   1759  *	changes sizes
   1760  * ext2fs_write, WRITE [ufs_readwrite], msdosfs_write: called when we
   1761  *	are written to
   1762  * ex2fs_chmod, ufs_chmod: called if VTEXT vnode and the sticky bit
   1763  *	is off
   1764  * ffs_realloccg: when we can't extend the current block and have
   1765  *	to allocate a new one we call this [XXX: why?]
   1766  * nfsrv_rename, rename_files: called when the target filename is there
   1767  *	and we want to remove it
   1768  * nfsrv_remove, sys_unlink: called on file we are removing
   1769  * nfsrv_access: if VTEXT and we want WRITE access and we don't uncache
   1770  *	then return "text busy"
   1771  * nfs_open: seems to uncache any file opened with nfs
   1772  * vn_writechk: if VTEXT vnode and can't uncache return "text busy"
   1773  */
   1774 
   1775 boolean_t
   1776 uvm_vnp_uncache(vp)
   1777 	struct vnode *vp;
   1778 {
   1779 	struct uvm_vnode *uvn = &vp->v_uvm;
   1780 
   1781 	/*
   1782 	 * lock uvn part of the vnode and check to see if we need to do anything
   1783 	 */
   1784 
   1785 	simple_lock(&uvn->u_obj.vmobjlock);
   1786 	if ((uvn->u_flags & UVM_VNODE_VALID) == 0 ||
   1787 			(uvn->u_flags & UVM_VNODE_BLOCKED) != 0) {
   1788 		simple_unlock(&uvn->u_obj.vmobjlock);
   1789 		return(TRUE);
   1790 	}
   1791 
   1792 	/*
   1793 	 * we have a valid, non-blocked uvn.   clear persist flag.
   1794 	 * if uvn is currently active we can return now.
   1795 	 */
   1796 
   1797 	uvn->u_flags &= ~UVM_VNODE_CANPERSIST;
   1798 	if (uvn->u_obj.uo_refs) {
   1799 		simple_unlock(&uvn->u_obj.vmobjlock);
   1800 		return(FALSE);
   1801 	}
   1802 
   1803 	/*
   1804 	 * uvn is currently persisting!   we have to gain a reference to
   1805 	 * it so that we can call uvn_detach to kill the uvn.
   1806 	 */
   1807 
   1808 	VREF(vp);			/* seems ok, even with VOP_LOCK */
   1809 	uvn->u_obj.uo_refs++;		/* value is now 1 */
   1810 	simple_unlock(&uvn->u_obj.vmobjlock);
   1811 
   1812 
   1813 #ifdef DEBUG
   1814 	/*
   1815 	 * carry over sanity check from old vnode pager: the vnode should
   1816 	 * be VOP_LOCK'd, and we confirm it here.
   1817 	 */
   1818 	if (!VOP_ISLOCKED(vp)) {
   1819 		boolean_t is_ok_anyway = FALSE;
   1820 #ifdef NFS
   1821 		extern int (**nfsv2_vnodeop_p) __P((void *));
   1822 		extern int (**spec_nfsv2nodeop_p) __P((void *));
   1823 		extern int (**fifo_nfsv2nodeop_p) __P((void *));
   1824 
   1825 		/* vnode is NOT VOP_LOCKed: some vnode types _never_ lock */
   1826 		if (vp->v_op == nfsv2_vnodeop_p ||
   1827 		    vp->v_op == spec_nfsv2nodeop_p) {
   1828 			is_ok_anyway = TRUE;
   1829 		}
   1830 		if (vp->v_op == fifo_nfsv2nodeop_p) {
   1831 			is_ok_anyway = TRUE;
   1832 		}
   1833 #endif	/* NFS */
   1834 		if (!is_ok_anyway)
   1835 			panic("uvm_vnp_uncache: vnode not locked!");
   1836 	}
   1837 #endif	/* DEBUG */
   1838 
   1839 	/*
   1840 	 * now drop our reference to the vnode.   if we have the sole
   1841 	 * reference to the vnode then this will cause it to die [as we
   1842 	 * just cleared the persist flag].   we have to unlock the vnode
   1843 	 * while we are doing this as it may trigger I/O.
   1844 	 *
   1845 	 * XXX: it might be possible for uvn to get reclaimed while we are
   1846 	 * unlocked causing us to return TRUE when we should not.   we ignore
   1847 	 * this as a false-positive return value doesn't hurt us.
   1848 	 */
   1849 	VOP_UNLOCK(vp, 0);
   1850 	uvn_detach(&uvn->u_obj);
   1851 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1852 
   1853 	/*
   1854 	 * and return...
   1855 	 */
   1856 
   1857 	return(TRUE);
   1858 }
   1859 
   1860 /*
   1861  * uvm_vnp_setsize: grow or shrink a vnode uvn
   1862  *
   1863  * grow   => just update size value
   1864  * shrink => toss un-needed pages
   1865  *
   1866  * => we assume that the caller has a reference of some sort to the
   1867  *	vnode in question so that it will not be yanked out from under
   1868  *	us.
   1869  *
   1870  * called from:
   1871  *  => truncate fns (ext2fs_truncate, ffs_truncate, detrunc[msdos])
   1872  *  => "write" fns (ext2fs_write, WRITE [ufs/ufs], msdosfs_write, nfs_write)
   1873  *  => ffs_balloc [XXX: why? doesn't WRITE handle?]
   1874  *  => NFS: nfs_loadattrcache, nfs_getattrcache, nfs_setattr
   1875  *  => union fs: union_newsize
   1876  */
   1877 
   1878 void
   1879 uvm_vnp_setsize(vp, newsize)
   1880 	struct vnode *vp;
   1881 	voff_t newsize;
   1882 {
   1883 	struct uvm_vnode *uvn = &vp->v_uvm;
   1884 
   1885 	/*
   1886 	 * lock uvn and check for valid object, and if valid: do it!
   1887 	 */
   1888 	simple_lock(&uvn->u_obj.vmobjlock);
   1889 	if (uvn->u_flags & UVM_VNODE_VALID) {
   1890 
   1891 		/*
   1892 		 * now check if the size has changed: if we shrink we had better
   1893 		 * toss some pages...
   1894 		 */
   1895 
   1896 		if (uvn->u_size > newsize) {
   1897 			(void)uvn_flush(&uvn->u_obj, newsize,
   1898 			    uvn->u_size, PGO_FREE);
   1899 		}
   1900 		uvn->u_size = newsize;
   1901 	}
   1902 	simple_unlock(&uvn->u_obj.vmobjlock);
   1903 
   1904 	/*
   1905 	 * done
   1906 	 */
   1907 	return;
   1908 }
   1909 
   1910 /*
   1911  * uvm_vnp_sync: flush all dirty VM pages back to their backing vnodes.
   1912  *
   1913  * => called from sys_sync with no VM structures locked
   1914  * => only one process can do a sync at a time (because the uvn
   1915  *    structure only has one queue for sync'ing).  we ensure this
   1916  *    by holding the uvn_sync_lock while the sync is in progress.
   1917  *    other processes attempting a sync will sleep on this lock
   1918  *    until we are done.
   1919  */
   1920 
   1921 void
   1922 uvm_vnp_sync(mp)
   1923 	struct mount *mp;
   1924 {
   1925 	struct uvm_vnode *uvn;
   1926 	struct vnode *vp;
   1927 	boolean_t got_lock;
   1928 
   1929 	/*
   1930 	 * step 1: ensure we are only ones using the uvn_sync_q by locking
   1931 	 * our lock...
   1932 	 */
   1933 	lockmgr(&uvn_sync_lock, LK_EXCLUSIVE, (void *)0);
   1934 
   1935 	/*
   1936 	 * step 2: build up a simpleq of uvns of interest based on the
   1937 	 * write list.   we gain a reference to uvns of interest.  must
   1938 	 * be careful about locking uvn's since we will be holding uvn_wl_lock
   1939 	 * in the body of the loop.
   1940 	 */
   1941 	SIMPLEQ_INIT(&uvn_sync_q);
   1942 	simple_lock(&uvn_wl_lock);
   1943 	for (uvn = uvn_wlist.lh_first ; uvn != NULL ;
   1944 	    uvn = uvn->u_wlist.le_next) {
   1945 
   1946 		vp = (struct vnode *) uvn;
   1947 		if (mp && vp->v_mount != mp)
   1948 			continue;
   1949 
   1950 		/* attempt to gain reference */
   1951 		while ((got_lock = simple_lock_try(&uvn->u_obj.vmobjlock)) ==
   1952 		    						FALSE &&
   1953 				(uvn->u_flags & UVM_VNODE_BLOCKED) == 0)
   1954 			/* spin */ ;
   1955 
   1956 		/*
   1957 		 * we will exit the loop if either if the following are true:
   1958 		 *  - we got the lock [always true if NCPU == 1]
   1959 		 *  - we failed to get the lock but noticed the vnode was
   1960 		 * 	"blocked" -- in this case the vnode must be a dying
   1961 		 *	vnode, and since dying vnodes are in the process of
   1962 		 *	being flushed out, we can safely skip this one
   1963 		 *
   1964 		 * we want to skip over the vnode if we did not get the lock,
   1965 		 * or if the vnode is already dying (due to the above logic).
   1966 		 *
   1967 		 * note that uvn must already be valid because we found it on
   1968 		 * the wlist (this also means it can't be ALOCK'd).
   1969 		 */
   1970 		if (!got_lock || (uvn->u_flags & UVM_VNODE_BLOCKED) != 0) {
   1971 			if (got_lock)
   1972 				simple_unlock(&uvn->u_obj.vmobjlock);
   1973 			continue;		/* skip it */
   1974 		}
   1975 
   1976 		/*
   1977 		 * gain reference.   watch out for persisting uvns (need to
   1978 		 * regain vnode REF).
   1979 		 */
   1980 		if (uvn->u_obj.uo_refs == 0)
   1981 			VREF(vp);
   1982 		uvn->u_obj.uo_refs++;
   1983 		simple_unlock(&uvn->u_obj.vmobjlock);
   1984 
   1985 		/*
   1986 		 * got it!
   1987 		 */
   1988 		SIMPLEQ_INSERT_HEAD(&uvn_sync_q, uvn, u_syncq);
   1989 	}
   1990 	simple_unlock(&uvn_wl_lock);
   1991 
   1992 	/*
   1993 	 * step 3: we now have a list of uvn's that may need cleaning.
   1994 	 * we are holding the uvn_sync_lock, but have dropped the uvn_wl_lock
   1995 	 * (so we can now safely lock uvn's again).
   1996 	 */
   1997 
   1998 	for (uvn = uvn_sync_q.sqh_first ; uvn ; uvn = uvn->u_syncq.sqe_next) {
   1999 		simple_lock(&uvn->u_obj.vmobjlock);
   2000 #ifdef DEBUG
   2001 		if (uvn->u_flags & UVM_VNODE_DYING) {
   2002 			printf("uvm_vnp_sync: dying vnode on sync list\n");
   2003 		}
   2004 #endif
   2005 		uvn_flush(&uvn->u_obj, 0, 0,
   2006 		    PGO_CLEANIT|PGO_ALLPAGES|PGO_DOACTCLUST);
   2007 
   2008 		/*
   2009 		 * if we have the only reference and we just cleaned the uvn,
   2010 		 * then we can pull it out of the UVM_VNODE_WRITEABLE state
   2011 		 * thus allowing us to avoid thinking about flushing it again
   2012 		 * on later sync ops.
   2013 		 */
   2014 		if (uvn->u_obj.uo_refs == 1 &&
   2015 		    (uvn->u_flags & UVM_VNODE_WRITEABLE)) {
   2016 			LIST_REMOVE(uvn, u_wlist);
   2017 			uvn->u_flags &= ~UVM_VNODE_WRITEABLE;
   2018 		}
   2019 
   2020 		simple_unlock(&uvn->u_obj.vmobjlock);
   2021 
   2022 		/* now drop our reference to the uvn */
   2023 		uvn_detach(&uvn->u_obj);
   2024 	}
   2025 
   2026 	/*
   2027 	 * done!  release sync lock
   2028 	 */
   2029 	lockmgr(&uvn_sync_lock, LK_RELEASE, (void *)0);
   2030 }
   2031