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