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