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