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