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