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