Home | History | Annotate | Line # | Download | only in uvm
uvm_vnode.c revision 1.17.2.5
      1 /*	$NetBSD: uvm_vnode.c,v 1.17.2.5 1999/04/29 05:36:41 chs Exp $	*/
      2 
      3 /*
      4  * XXXCDC: "ROUGH DRAFT" QUALITY UVM PRE-RELEASE FILE!
      5  *         >>>USE AT YOUR OWN RISK, WORK IS NOT FINISHED<<<
      6  */
      7 /*
      8  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      9  * Copyright (c) 1991, 1993
     10  *      The Regents of the University of California.
     11  * Copyright (c) 1990 University of Utah.
     12  *
     13  * All rights reserved.
     14  *
     15  * This code is derived from software contributed to Berkeley by
     16  * the Systems Programming Group of the University of Utah Computer
     17  * Science Department.
     18  *
     19  * Redistribution and use in source and binary forms, with or without
     20  * modification, are permitted provided that the following conditions
     21  * are met:
     22  * 1. Redistributions of source code must retain the above copyright
     23  *    notice, this list of conditions and the following disclaimer.
     24  * 2. Redistributions in binary form must reproduce the above copyright
     25  *    notice, this list of conditions and the following disclaimer in the
     26  *    documentation and/or other materials provided with the distribution.
     27  * 3. All advertising materials mentioning features or use of this software
     28  *    must display the following acknowledgement:
     29  *      This product includes software developed by Charles D. Cranor,
     30  *	Washington University, the University of California, Berkeley and
     31  *	its contributors.
     32  * 4. Neither the name of the University nor the names of its contributors
     33  *    may be used to endorse or promote products derived from this software
     34  *    without specific prior written permission.
     35  *
     36  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     37  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     39  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     40  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     41  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     42  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     44  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     46  * SUCH DAMAGE.
     47  *
     48  *      @(#)vnode_pager.c       8.8 (Berkeley) 2/13/94
     49  * from: Id: uvm_vnode.c,v 1.1.2.26 1998/02/02 20:38:07 chuck Exp
     50  */
     51 
     52 #include "fs_nfs.h"
     53 #include "opt_uvm.h"
     54 #include "opt_uvmhist.h"
     55 
     56 /*
     57  * uvm_vnode.c: the vnode pager.
     58  */
     59 
     60 #include <sys/param.h>
     61 #include <sys/systm.h>
     62 #include <sys/kernel.h>
     63 #include <sys/proc.h>
     64 #include <sys/malloc.h>
     65 #include <sys/vnode.h>
     66 #include <sys/disklabel.h>
     67 #include <sys/ioctl.h>
     68 #include <sys/fcntl.h>
     69 #include <sys/conf.h>
     70 
     71 #include <miscfs/specfs/specdev.h>
     72 
     73 #include <vm/vm.h>
     74 #include <vm/vm_page.h>
     75 #include <vm/vm_kern.h>
     76 
     77 #include <uvm/uvm.h>
     78 #include <uvm/uvm_vnode.h>
     79 
     80 /*
     81  * private global data structure
     82  *
     83  * we keep a list of writeable active vnode-backed VM objects for sync op.
     84  * we keep a simpleq of vnodes that are currently being sync'd.
     85  */
     86 
     87 LIST_HEAD(uvn_list_struct, uvm_vnode);
     88 static struct uvn_list_struct uvn_wlist;	/* writeable uvns */
     89 static simple_lock_data_t uvn_wl_lock;		/* locks uvn_wlist */
     90 
     91 SIMPLEQ_HEAD(uvn_sq_struct, uvm_vnode);
     92 static struct uvn_sq_struct uvn_sync_q;		/* sync'ing uvns */
     93 lock_data_t uvn_sync_lock;			/* locks sync operation */
     94 
     95 /*
     96  * functions
     97  */
     98 
     99 static int		uvn_asyncget __P((struct uvm_object *, vaddr_t,
    100 					    int));
    101 struct uvm_object *	uvn_attach __P((void *, vm_prot_t));
    102 static void		uvn_cluster __P((struct uvm_object *, vaddr_t,
    103 					 vaddr_t *, vaddr_t *));
    104 static void		uvn_detach __P((struct uvm_object *));
    105 static int		uvn_findpage __P((struct uvm_object *, vaddr_t,
    106 					  struct vm_page **, int));
    107 static boolean_t	uvn_flush __P((struct uvm_object *, vaddr_t,
    108 				       vaddr_t, int));
    109 static int		uvn_get __P((struct uvm_object *, vaddr_t,
    110 				     vm_page_t *, int *, int,
    111 				     vm_prot_t, int, int));
    112 static void		uvn_init __P((void));
    113 static int		uvn_put __P((struct uvm_object *, vm_page_t *,
    114 				     int, boolean_t));
    115 static void		uvn_reference __P((struct uvm_object *));
    116 static boolean_t	uvn_releasepg __P((struct vm_page *,
    117 					   struct vm_page **));
    118 
    119 /*
    120  * master pager structure
    121  */
    122 
    123 struct uvm_pagerops uvm_vnodeops = {
    124 	uvn_init,
    125 	uvn_attach,
    126 	uvn_reference,
    127 	uvn_detach,
    128 	NULL,			/* no specialized fault routine required */
    129 	uvn_flush,
    130 	uvn_get,
    131 	uvn_asyncget,
    132 	uvn_put,
    133 	uvn_cluster,
    134 	uvm_mk_pcluster, /* use generic version of this: see uvm_pager.c */
    135 	uvm_shareprot,	 /* !NULL: allow us in share maps */
    136 	NULL,		 /* AIO-DONE function (not until we have asyncio) */
    137 	uvn_releasepg,
    138 };
    139 
    140 /*
    141  * the ops!
    142  */
    143 
    144 /*
    145  * uvn_init
    146  *
    147  * init pager private data structures.
    148  */
    149 
    150 static void
    151 uvn_init()
    152 {
    153 
    154 	LIST_INIT(&uvn_wlist);
    155 	simple_lock_init(&uvn_wl_lock);
    156 	/* note: uvn_sync_q init'd in uvm_vnp_sync() */
    157 	lockinit(&uvn_sync_lock, PVM, "uvnsync", 0, 0);
    158 }
    159 
    160 /*
    161  * uvn_attach
    162  *
    163  * attach a vnode structure to a VM object.  if the vnode is already
    164  * attached, then just bump the reference count by one and return the
    165  * VM object.   if not already attached, attach and return the new VM obj.
    166  * the "accessprot" tells the max access the attaching thread wants to
    167  * our pages.
    168  *
    169  * => caller must _not_ already be holding the lock on the uvm_object.
    170  * => in fact, nothing should be locked so that we can sleep here.
    171  * => note that uvm_object is first thing in vnode structure, so their
    172  *    pointers are equiv.
    173  */
    174 
    175 struct uvm_object *
    176 uvn_attach(arg, accessprot)
    177 	void *arg;
    178 	vm_prot_t accessprot;
    179 {
    180 	struct vnode *vp = arg;
    181 	struct uvm_vnode *uvn = &vp->v_uvm;
    182 	struct vattr vattr;
    183 	int oldflags, result;
    184 	struct partinfo pi;
    185 	off_t used_vnode_size;
    186 	UVMHIST_FUNC("uvn_attach"); UVMHIST_CALLED(maphist);
    187 
    188 	UVMHIST_LOG(maphist, "(vn=0x%x)", arg,0,0,0);
    189 
    190 	used_vnode_size = (u_quad_t)0;	/* XXX gcc -Wuninitialized */
    191 
    192 	/*
    193 	 * first get a lock on the uvn.
    194 	 */
    195 	simple_lock(&uvn->u_obj.vmobjlock);
    196 	while (uvn->u_flags & UVM_VNODE_BLOCKED) {
    197 		uvn->u_flags |= UVM_VNODE_WANTED;
    198 		UVMHIST_LOG(maphist, "  SLEEPING on blocked vn",0,0,0,0);
    199 		UVM_UNLOCK_AND_WAIT(uvn, &uvn->u_obj.vmobjlock, FALSE,
    200 		    "uvn_attach", 0);
    201 		simple_lock(&uvn->u_obj.vmobjlock);
    202 		UVMHIST_LOG(maphist,"  WOKE UP",0,0,0,0);
    203 	}
    204 
    205 	/*
    206 	 * if we're mapping a BLK device, make sure it is a disk.
    207 	 */
    208 	if (vp->v_type == VBLK && bdevsw[major(vp->v_rdev)].d_type != D_DISK) {
    209 		simple_unlock(&uvn->u_obj.vmobjlock);
    210 		UVMHIST_LOG(maphist,"<- done (VBLK not D_DISK!)", 0,0,0,0);
    211 		return(NULL);
    212 	}
    213 
    214 	oldflags = 0;
    215 
    216 #ifdef DIAGNOSTIC
    217 	if (vp->v_type != VREG) {
    218 		panic("uvn_attach: vp %p not VREG", vp);
    219 	}
    220 #endif
    221 
    222 	/*
    223 	 * set up our idea of the size
    224 	 * if this hasn't been done already.
    225 	 */
    226 	if (uvn->u_size == VSIZENOTSET) {
    227 
    228 	uvn->u_flags = UVM_VNODE_ALOCK;
    229 	simple_unlock(&uvn->u_obj.vmobjlock); /* drop lock in case we sleep */
    230 		/* XXX: curproc? */
    231 	if (vp->v_type == VBLK) {
    232 		/*
    233 		 * We could implement this as a specfs getattr call, but:
    234 		 *
    235 		 *	(1) VOP_GETATTR() would get the file system
    236 		 *	    vnode operation, not the specfs operation.
    237 		 *
    238 		 *	(2) All we want is the size, anyhow.
    239 		 */
    240 		result = (*bdevsw[major(vp->v_rdev)].d_ioctl)(vp->v_rdev,
    241 		    DIOCGPART, (caddr_t)&pi, FREAD, curproc);
    242 		if (result == 0) {
    243 			/* XXX should remember blocksize */
    244 			used_vnode_size = (u_quad_t)pi.disklab->d_secsize *
    245 			    (u_quad_t)pi.part->p_size;
    246 		}
    247 	} else {
    248 		result = VOP_GETATTR(vp, &vattr, curproc->p_ucred, curproc);
    249 		if (result == 0)
    250 			used_vnode_size = vattr.va_size;
    251 	}
    252 
    253 
    254 	/*
    255 	 * make sure that the newsize fits within a vaddr_t
    256 	 * XXX: need to revise addressing data types
    257 	 */
    258 	if (used_vnode_size > (vaddr_t) -PAGE_SIZE) {
    259 #ifdef DEBUG
    260 		printf("uvn_attach: vn %p size truncated %qx->%x\n", vp,
    261 		    used_vnode_size, -PAGE_SIZE);
    262 #endif
    263 		used_vnode_size = (vaddr_t) -PAGE_SIZE;
    264 	}
    265 
    266 	/* relock object */
    267 	simple_lock(&uvn->u_obj.vmobjlock);
    268 
    269 	if (uvn->u_flags & UVM_VNODE_WANTED)
    270 		wakeup(uvn);
    271 	uvn->u_flags = 0;
    272 
    273 	if (result != 0) {
    274 		simple_unlock(&uvn->u_obj.vmobjlock); /* drop lock */
    275 		UVMHIST_LOG(maphist,"<- done (VOP_GETATTR FAILED!)", 0,0,0,0);
    276 		return(NULL);
    277 	}
    278 	uvn->u_size = used_vnode_size;
    279 
    280 	}
    281 
    282 	/* check for new writeable uvn */
    283 	if ((accessprot & VM_PROT_WRITE) != 0 &&
    284 	    (uvn->u_flags & UVM_VNODE_WRITEABLE) == 0) {
    285 		simple_lock(&uvn_wl_lock);
    286 		LIST_INSERT_HEAD(&uvn_wlist, uvn, u_wlist);
    287 		uvn->u_flags |= UVM_VNODE_WRITEABLE;
    288 		simple_unlock(&uvn_wl_lock);
    289 		/* we are now on wlist! */
    290 	}
    291 
    292 	/* unlock and return */
    293 	simple_unlock(&uvn->u_obj.vmobjlock);
    294 	UVMHIST_LOG(maphist,"<- done, refcnt=%d", uvn->u_obj.uo_refs,
    295 	    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
    313 uvn_reference(uobj)
    314 	struct uvm_object *uobj;
    315 {
    316 	UVMHIST_FUNC("uvn_reference"); UVMHIST_CALLED(maphist);
    317 
    318 	VREF((struct vnode *)uobj);
    319 }
    320 
    321 /*
    322  * uvn_detach
    323  *
    324  * remove a reference to a VM object.
    325  *
    326  * => caller must call with object unlocked and map locked.
    327  * => this starts the detach process, but doesn't have to finish it
    328  *    (async i/o could still be pending).
    329  */
    330 static void
    331 uvn_detach(uobj)
    332 	struct uvm_object *uobj;
    333 {
    334 	UVMHIST_FUNC("uvn_detach"); UVMHIST_CALLED(maphist);
    335 
    336 	vrele((struct vnode *)uobj);
    337 }
    338 
    339 /*
    340  * uvm_vnp_terminate: external hook to clear out a vnode's VM
    341  *
    342  * called in two cases:
    343  *  [1] when a persisting vnode vm object (i.e. one with a zero reference
    344  *      count) needs to be freed so that a vnode can be reused.  this
    345  *      happens under "getnewvnode" in vfs_subr.c.   if the vnode from
    346  *      the free list is still attached (i.e. not VBAD) then vgone is
    347  *	called.   as part of the vgone trace this should get called to
    348  *	free the vm object.   this is the common case.
    349  *  [2] when a filesystem is being unmounted by force (MNT_FORCE,
    350  *	"umount -f") the vgone() function is called on active vnodes
    351  *	on the mounted file systems to kill their data (the vnodes become
    352  *	"dead" ones [see src/sys/miscfs/deadfs/...]).  that results in a
    353  *	call here (even if the uvn is still in use -- i.e. has a non-zero
    354  *	reference count).  this case happens at "umount -f" and during a
    355  *	"reboot/halt" operation.
    356  *
    357  * => the caller must XLOCK and VOP_LOCK the vnode before calling us
    358  *	[protects us from getting a vnode that is already in the DYING
    359  *	 state...]
    360  * => unlike uvn_detach, this function must not return until all the
    361  *	uvn's pages are disposed of.
    362  * => in case [2] the uvn is still alive after this call, but all I/O
    363  *	ops will fail (due to the backing vnode now being "dead").  this
    364  *	will prob. kill any process using the uvn due to pgo_get failing.
    365  */
    366 
    367 void
    368 uvm_vnp_terminate(vp)
    369 	struct vnode *vp;
    370 {
    371 	struct uvm_vnode *uvn = &vp->v_uvm;
    372 
    373 	if (uvn->u_flags & UVM_VNODE_WRITEABLE) {
    374 		simple_lock(&uvn_wl_lock);
    375 		LIST_REMOVE(uvn, u_wlist);
    376 		uvn->u_flags &= ~(UVM_VNODE_WRITEABLE);
    377 		simple_unlock(&uvn_wl_lock);
    378 	}
    379 }
    380 
    381 /*
    382  * uvn_releasepg: handled a released page in a uvn
    383  *
    384  * => "pg" is a PG_BUSY [caller owns it], PG_RELEASED page that we need
    385  *	to dispose of.
    386  * => caller must handled PG_WANTED case
    387  * => called with page's object locked, pageq's unlocked
    388  * => returns TRUE if page's object is still alive, FALSE if we
    389  *	killed the page's object.    if we return TRUE, then we
    390  *	return with the object locked.
    391  * => if (nextpgp != NULL) => we return pageq.tqe_next here, and return
    392  *				with the page queues locked [for pagedaemon]
    393  * => if (nextpgp == NULL) => we return with page queues unlocked [normal case]
    394  * => we kill the uvn if it is not referenced and we are suppose to
    395  *	kill it ("relkill").
    396  */
    397 
    398 boolean_t
    399 uvn_releasepg(pg, nextpgp)
    400 	struct vm_page *pg;
    401 	struct vm_page **nextpgp;	/* OUT */
    402 {
    403 	struct uvm_vnode *uvn = (struct uvm_vnode *) pg->uobject;
    404 #ifdef DIAGNOSTIC
    405 	if ((pg->flags & PG_RELEASED) == 0)
    406 		panic("uvn_releasepg: page not released!");
    407 #endif
    408 
    409 	/*
    410 	 * dispose of the page [caller handles PG_WANTED]
    411 	 */
    412 	pmap_page_protect(PMAP_PGARG(pg), VM_PROT_NONE);
    413 	uvm_lock_pageq();
    414 	if (nextpgp)
    415 		*nextpgp = pg->pageq.tqe_next;	/* next page for daemon */
    416 	uvm_pagefree(pg);
    417 	if (!nextpgp)
    418 		uvm_unlock_pageq();
    419 
    420 #ifdef UBC
    421 	/* XXX I'm sure we need to do something here. */
    422 	uvn = uvn;
    423 #else
    424 	/*
    425 	 * now see if we need to kill the object
    426 	 */
    427 	if (uvn->u_flags & UVM_VNODE_RELKILL) {
    428 		if (uvn->u_obj.uo_refs)
    429 			panic("uvn_releasepg: kill flag set on referenced "
    430 			    "object!");
    431 		if (uvn->u_obj.uo_npages == 0) {
    432 			if (uvn->u_flags & UVM_VNODE_WRITEABLE) {
    433 				simple_lock(&uvn_wl_lock);
    434 				LIST_REMOVE(uvn, u_wlist);
    435 				simple_unlock(&uvn_wl_lock);
    436 			}
    437 #ifdef DIAGNOSTIC
    438 			if (uvn->u_obj.memq.tqh_first)
    439 	panic("uvn_releasepg: pages in object with npages == 0");
    440 #endif
    441 			if (uvn->u_flags & UVM_VNODE_WANTED)
    442 				/* still holding object lock */
    443 				wakeup(uvn);
    444 
    445 			uvn->u_flags = 0;		/* DEAD! */
    446 			simple_unlock(&uvn->u_obj.vmobjlock);
    447 			return (FALSE);
    448 		}
    449 	}
    450 #endif
    451 	return (TRUE);
    452 }
    453 
    454 /*
    455  * NOTE: currently we have to use VOP_READ/VOP_WRITE because they go
    456  * through the buffer cache and allow I/O in any size.  These VOPs use
    457  * synchronous i/o.  [vs. VOP_STRATEGY which can be async, but doesn't
    458  * go through the buffer cache or allow I/O sizes larger than a
    459  * block].  we will eventually want to change this.
    460  *
    461  * issues to consider:
    462  *   uvm provides the uvm_aiodesc structure for async i/o management.
    463  * there are two tailq's in the uvm. structure... one for pending async
    464  * i/o and one for "done" async i/o.   to do an async i/o one puts
    465  * an aiodesc on the "pending" list (protected by splbio()), starts the
    466  * i/o and returns VM_PAGER_PEND.    when the i/o is done, we expect
    467  * some sort of "i/o done" function to be called (at splbio(), interrupt
    468  * time).   this function should remove the aiodesc from the pending list
    469  * and place it on the "done" list and wakeup the daemon.   the daemon
    470  * will run at normal spl() and will remove all items from the "done"
    471  * list and call the "aiodone" hook for each done request (see uvm_pager.c).
    472  * [in the old vm code, this was done by calling the "put" routine with
    473  * null arguments which made the code harder to read and understand because
    474  * you had one function ("put") doing two things.]
    475  *
    476  * so the current pager needs:
    477  *   int uvn_aiodone(struct uvm_aiodesc *)
    478  *
    479  * => return KERN_SUCCESS (aio finished, free it).  otherwise requeue for
    480  *	later collection.
    481  * => called with pageq's locked by the daemon.
    482  *
    483  * general outline:
    484  * - "try" to lock object.   if fail, just return (will try again later)
    485  * - drop "u_nio" (this req is done!)
    486  * - if (object->iosync && u_naio == 0) { wakeup &uvn->u_naio }
    487  * - get "page" structures (atop?).
    488  * - handle "wanted" pages
    489  * - handle "released" pages [using pgo_releasepg]
    490  *   >>> pgo_releasepg may kill the object
    491  * dont forget to look at "object" wanted flag in all cases.
    492  */
    493 
    494 
    495 /*
    496  * uvn_flush: flush pages out of a uvm object.
    497  *
    498  * => object should be locked by caller.   we may _unlock_ the object
    499  *	if (and only if) we need to clean a page (PGO_CLEANIT).
    500  *	we return with the object locked.
    501  * => if PGO_CLEANIT is set, we may block (due to I/O).   thus, a caller
    502  *	might want to unlock higher level resources (e.g. vm_map)
    503  *	before calling flush.
    504  * => if PGO_CLEANIT is not set, then we will neither unlock the object
    505  *	or block.
    506  * => if PGO_ALLPAGE is set, then all pages in the object are valid targets
    507  *	for flushing.
    508  * => NOTE: we rely on the fact that the object's memq is a TAILQ and
    509  *	that new pages are inserted on the tail end of the list.   thus,
    510  *	we can make a complete pass through the object in one go by starting
    511  *	at the head and working towards the tail (new pages are put in
    512  *	front of us).
    513  * => NOTE: we are allowed to lock the page queues, so the caller
    514  *	must not be holding the lock on them [e.g. pagedaemon had
    515  *	better not call us with the queues locked]
    516  * => we return TRUE unless we encountered some sort of I/O error
    517  *
    518  * comment on "cleaning" object and PG_BUSY pages:
    519  *	this routine is holding the lock on the object.   the only time
    520  *	that it can run into a PG_BUSY page that it does not own is if
    521  *	some other process has started I/O on the page (e.g. either
    522  *	a pagein, or a pageout).    if the PG_BUSY page is being paged
    523  *	in, then it can not be dirty (!PG_CLEAN) because no one has
    524  *	had a chance to modify it yet.    if the PG_BUSY page is being
    525  *	paged out then it means that someone else has already started
    526  *	cleaning the page for us (how nice!).    in this case, if we
    527  *	have syncio specified, then after we make our pass through the
    528  *	object we need to wait for the other PG_BUSY pages to clear
    529  *	off (i.e. we need to do an iosync).   also note that once a
    530  *	page is PG_BUSY it must stay in its object until it is un-busyed.
    531  *
    532  * note on page traversal:
    533  *	we can traverse the pages in an object either by going down the
    534  *	linked list in "uobj->memq", or we can go over the address range
    535  *	by page doing hash table lookups for each address.    depending
    536  *	on how many pages are in the object it may be cheaper to do one
    537  *	or the other.   we set "by_list" to true if we are using memq.
    538  *	if the cost of a hash lookup was equal to the cost of the list
    539  *	traversal we could compare the number of pages in the start->stop
    540  *	range to the total number of pages in the object.   however, it
    541  *	seems that a hash table lookup is more expensive than the linked
    542  *	list traversal, so we multiply the number of pages in the
    543  *	start->stop range by a penalty which we define below.
    544  */
    545 
    546 #define UVN_HASH_PENALTY 4	/* XXX: a guess */
    547 
    548 static boolean_t
    549 uvn_flush(uobj, start, stop, flags)
    550 	struct uvm_object *uobj;
    551 	vaddr_t start, stop;
    552 	int flags;
    553 {
    554 	struct uvm_vnode *uvn = (struct uvm_vnode *) uobj;
    555 	struct vm_page *pp, *ppnext, *ptmp;
    556 	struct vm_page *pps[MAXBSIZE >> PAGE_SHIFT], **ppsp;
    557 	int npages, result, lcv;
    558 	boolean_t retval, need_iosync, by_list, needs_clean;
    559 	vaddr_t curoff;
    560 	u_short pp_version;
    561 	UVMHIST_FUNC("uvn_flush"); UVMHIST_CALLED(maphist);
    562 
    563 #ifdef UBC
    564 	if (uvn->u_size == VSIZENOTSET) {
    565 		void vp_name(void *);
    566 
    567 #ifdef DEBUG
    568 		printf("uvn_flush: size not set vp %p\n", uvn);
    569 		if ((flags & PGO_ALLPAGES) == 0)
    570 			printf("... and PGO_ALLPAGES not set: "
    571 			       "start 0x%lx end 0x%lx flags 0x%x\n",
    572 			       start, stop, flags);
    573 		vp_name(uvn);
    574 #endif
    575 		flags |= PGO_ALLPAGES;
    576 	}
    577 #if 0
    578 	/* XXX unfortunately this is legitimate */
    579 	if ((flags & PGO_FREE) && uobj->uo_refs) {
    580 		printf("uvn_flush: PGO_FREE on ref'd vp %p\n", uobj);
    581 		Debugger();
    582 	}
    583 #endif
    584 #endif
    585 
    586 	curoff = 0;	/* XXX: shut up gcc */
    587 	/*
    588 	 * get init vals and determine how we are going to traverse object
    589 	 */
    590 
    591 	need_iosync = FALSE;
    592 	retval = TRUE;		/* return value */
    593 	if (flags & PGO_ALLPAGES) {
    594 		start = 0;
    595 #ifdef UBC
    596 		stop = -1;
    597 #else
    598 		stop = round_page(uvn->u_size);
    599 #endif
    600 		by_list = TRUE;		/* always go by the list */
    601 	} else {
    602 		start = trunc_page(start);
    603 		stop = round_page(stop);
    604 		if (stop > round_page(uvn->u_size)) {
    605 			printf("uvn_flush: oor vp %p start 0x%x stop 0x%x size 0x%x\n", uvn, (int)start, (int)stop, (int)round_page(uvn->u_size));
    606 		}
    607 
    608 		by_list = (uobj->uo_npages <=
    609 		    ((stop - start) >> PAGE_SHIFT) * UVN_HASH_PENALTY);
    610 	}
    611 
    612 	UVMHIST_LOG(maphist,
    613 	    " flush start=0x%x, stop=0x%x, by_list=%d, flags=0x%x",
    614 	    start, stop, by_list, flags);
    615 
    616 	/*
    617 	 * PG_CLEANCHK: this bit is used by the pgo_mk_pcluster function as
    618 	 * a _hint_ as to how up to date the PG_CLEAN bit is.   if the hint
    619 	 * is wrong it will only prevent us from clustering... it won't break
    620 	 * anything.   we clear all PG_CLEANCHK bits here, and pgo_mk_pcluster
    621 	 * will set them as it syncs PG_CLEAN.   This is only an issue if we
    622 	 * are looking at non-inactive pages (because inactive page's PG_CLEAN
    623 	 * bit is always up to date since there are no mappings).
    624 	 * [borrowed PG_CLEANCHK idea from FreeBSD VM]
    625 	 */
    626 
    627 	if ((flags & PGO_CLEANIT) != 0 &&
    628 	    uobj->pgops->pgo_mk_pcluster != NULL) {
    629 		if (by_list) {
    630 			for (pp = TAILQ_FIRST(&uobj->memq);
    631 			     pp != NULL ;
    632 			     pp = TAILQ_NEXT(pp, listq)) {
    633 				if (pp->offset < start ||
    634 				    (pp->offset >= stop && stop != -1))
    635 					continue;
    636 				pp->flags &= ~PG_CLEANCHK;
    637 			}
    638 
    639 		} else {   /* by hash */
    640 			for (curoff = start ; curoff < stop;
    641 			    curoff += PAGE_SIZE) {
    642 				pp = uvm_pagelookup(uobj, curoff);
    643 				if (pp)
    644 					pp->flags &= ~PG_CLEANCHK;
    645 			}
    646 		}
    647 	}
    648 
    649 	/*
    650 	 * now do it.   note: we must update ppnext in body of loop or we
    651 	 * will get stuck.  we need to use ppnext because we may free "pp"
    652 	 * before doing the next loop.
    653 	 */
    654 
    655 	if (by_list) {
    656 		pp = TAILQ_FIRST(&uobj->memq);
    657 	} else {
    658 		curoff = start;
    659 		pp = uvm_pagelookup(uobj, curoff);
    660 	}
    661 
    662 	ppnext = NULL;	/* XXX: shut up gcc */
    663 	ppsp = NULL;		/* XXX: shut up gcc */
    664 	uvm_lock_pageq();	/* page queues locked */
    665 
    666 	/* locked: both page queues and uobj */
    667 	for ( ; (by_list && pp != NULL) ||
    668 	  (!by_list && curoff < stop) ; pp = ppnext) {
    669 
    670 		if (by_list) {
    671 
    672 			/*
    673 			 * range check
    674 			 */
    675 
    676 			if (pp->offset < start || pp->offset >= stop) {
    677 				ppnext = TAILQ_NEXT(pp, listq);
    678 				continue;
    679 			}
    680 
    681 		} else {
    682 
    683 			/*
    684 			 * null check
    685 			 */
    686 
    687 			curoff += PAGE_SIZE;
    688 			if (pp == NULL) {
    689 				if (curoff < stop)
    690 					ppnext = uvm_pagelookup(uobj, curoff);
    691 				continue;
    692 			}
    693 
    694 		}
    695 
    696 		/*
    697 		 * handle case where we do not need to clean page (either
    698 		 * because we are not clean or because page is not dirty or
    699 		 * is busy):
    700 		 *
    701 		 * NOTE: we are allowed to deactivate a non-wired active
    702 		 * PG_BUSY page, but once a PG_BUSY page is on the inactive
    703 		 * queue it must stay put until it is !PG_BUSY (so as not to
    704 		 * confuse pagedaemon).
    705 		 */
    706 
    707 		if ((flags & PGO_CLEANIT) == 0 || (pp->flags & PG_BUSY) != 0) {
    708 			needs_clean = FALSE;
    709 			if ((pp->flags & PG_BUSY) != 0 &&
    710 			    (flags & (PGO_CLEANIT|PGO_SYNCIO)) ==
    711 			             (PGO_CLEANIT|PGO_SYNCIO))
    712 				need_iosync = TRUE;
    713 		} else {
    714 			/*
    715 			 * freeing: nuke all mappings so we can sync
    716 			 * PG_CLEAN bit with no race
    717 			 */
    718 			if ((pp->flags & PG_CLEAN) != 0 &&
    719 			    (flags & PGO_FREE) != 0 &&
    720 			    (pp->pqflags & PQ_ACTIVE) != 0)
    721 				pmap_page_protect(PMAP_PGARG(pp), VM_PROT_NONE);
    722 			if ((pp->flags & PG_CLEAN) != 0 &&
    723 			    pmap_is_modified(PMAP_PGARG(pp)))
    724 				pp->flags &= ~(PG_CLEAN);
    725 			pp->flags |= PG_CLEANCHK;	/* update "hint" */
    726 
    727 			needs_clean = ((pp->flags & PG_CLEAN) == 0);
    728 		}
    729 
    730 		/*
    731 		 * if we don't need a clean... load ppnext and dispose of pp
    732 		 */
    733 		if (!needs_clean) {
    734 			/* load ppnext */
    735 			if (by_list)
    736 				ppnext = pp->listq.tqe_next;
    737 			else {
    738 				if (curoff < stop)
    739 					ppnext = uvm_pagelookup(uobj, curoff);
    740 			}
    741 
    742 			/* now dispose of pp */
    743 			if (flags & PGO_DEACTIVATE) {
    744 				if ((pp->pqflags & PQ_INACTIVE) == 0 &&
    745 				    pp->wire_count == 0) {
    746 					pmap_page_protect(PMAP_PGARG(pp),
    747 					    VM_PROT_NONE);
    748 					uvm_pagedeactivate(pp);
    749 				}
    750 
    751 			} else if (flags & PGO_FREE) {
    752 				if (pp->flags & PG_BUSY) {
    753 					/* release busy pages */
    754 					pp->flags |= PG_RELEASED;
    755 				} else {
    756 					pmap_page_protect(PMAP_PGARG(pp),
    757 					    VM_PROT_NONE);
    758 					/* removed page from object */
    759 					uvm_pagefree(pp);
    760 				}
    761 			}
    762 			/* ppnext is valid so we can continue... */
    763 			continue;
    764 		}
    765 
    766 		/*
    767 		 * pp points to a page in the locked object that we are
    768 		 * working on.  if it is !PG_CLEAN,!PG_BUSY and we asked
    769 		 * for cleaning (PGO_CLEANIT).  we clean it now.
    770 		 *
    771 		 * let uvm_pager_put attempted a clustered page out.
    772 		 * note: locked: uobj and page queues.
    773 		 */
    774 
    775 		pp->flags |= PG_BUSY;	/* we 'own' page now */
    776 		UVM_PAGE_OWN(pp, "uvn_flush");
    777 		pmap_page_protect(PMAP_PGARG(pp), VM_PROT_READ);
    778 		pp_version = pp->version;
    779 ReTry:
    780 		ppsp = pps;
    781 		npages = sizeof(pps) / sizeof(struct vm_page *);
    782 
    783 		/* locked: page queues, uobj */
    784 		result = uvm_pager_put(uobj, pp, &ppsp, &npages,
    785 				       flags | PGO_DOACTCLUST, start, stop);
    786 		/* unlocked: page queues, uobj */
    787 
    788 		/*
    789 		 * at this point nothing is locked.   if we did an async I/O
    790 		 * it is remotely possible for the async i/o to complete and
    791 		 * the page "pp" be freed or what not before we get a chance
    792 		 * to relock the object.   in order to detect this, we have
    793 		 * saved the version number of the page in "pp_version".
    794 		 */
    795 
    796 		/* relock! */
    797 		simple_lock(&uobj->vmobjlock);
    798 		uvm_lock_pageq();
    799 
    800 		/*
    801 		 * VM_PAGER_AGAIN: given the structure of this pager, this
    802 		 * can only happen when  we are doing async I/O and can't
    803 		 * map the pages into kernel memory (pager_map) due to lack
    804 		 * of vm space.   if this happens we drop back to sync I/O.
    805 		 */
    806 
    807 		if (result == VM_PAGER_AGAIN) {
    808 			/*
    809 			 * it is unlikely, but page could have been released
    810 			 * while we had the object lock dropped.   we ignore
    811 			 * this now and retry the I/O.  we will detect and
    812 			 * handle the released page after the syncio I/O
    813 			 * completes.
    814 			 */
    815 #ifdef DIAGNOSTIC
    816 			if (flags & PGO_SYNCIO)
    817 	panic("uvn_flush: PGO_SYNCIO return 'try again' error (impossible)");
    818 #endif
    819 			flags |= PGO_SYNCIO;
    820 			goto ReTry;
    821 		}
    822 
    823 		/*
    824 		 * the cleaning operation is now done.   finish up.  note that
    825 		 * on error (!OK, !PEND) uvm_pager_put drops the cluster for us.
    826 		 * if success (OK, PEND) then uvm_pager_put returns the cluster
    827 		 * to us in ppsp/npages.
    828 		 */
    829 
    830 		/*
    831 		 * for pending async i/o if we are not deactivating/freeing
    832 		 * we can move on to the next page.
    833 		 */
    834 
    835 		if (result == VM_PAGER_PEND) {
    836 
    837 			if ((flags & (PGO_DEACTIVATE|PGO_FREE)) == 0) {
    838 				/*
    839 				 * no per-page ops: refresh ppnext and continue
    840 				 */
    841 				if (by_list) {
    842 					if (pp->version == pp_version)
    843 						ppnext = pp->listq.tqe_next;
    844 					else
    845 						/* reset */
    846 						ppnext = uobj->memq.tqh_first;
    847 				} else {
    848 					if (curoff < stop)
    849 						ppnext = uvm_pagelookup(uobj,
    850 						    curoff);
    851 				}
    852 				continue;
    853 			}
    854 
    855 			/* need to do anything here? */
    856 		}
    857 
    858 		/*
    859 		 * need to look at each page of the I/O operation.  we defer
    860 		 * processing "pp" until the last trip through this "for" loop
    861 		 * so that we can load "ppnext" for the main loop after we
    862 		 * play with the cluster pages [thus the "npages + 1" in the
    863 		 * loop below].
    864 		 */
    865 
    866 		for (lcv = 0 ; lcv < npages + 1 ; lcv++) {
    867 
    868 			/*
    869 			 * handle ppnext for outside loop, and saving pp
    870 			 * until the end.
    871 			 */
    872 			if (lcv < npages) {
    873 				if (ppsp[lcv] == pp)
    874 					continue; /* skip pp until the end */
    875 				ptmp = ppsp[lcv];
    876 			} else {
    877 				ptmp = pp;
    878 
    879 				/* set up next page for outer loop */
    880 				if (by_list) {
    881 					if (pp->version == pp_version)
    882 						ppnext = pp->listq.tqe_next;
    883 					else
    884 						/* reset */
    885 						ppnext = uobj->memq.tqh_first;
    886 				} else {
    887 					if (curoff < stop)
    888 					ppnext = uvm_pagelookup(uobj, curoff);
    889 				}
    890 			}
    891 
    892 			/*
    893 			 * verify the page didn't get moved while obj was
    894 			 * unlocked
    895 			 */
    896 			if (result == VM_PAGER_PEND && ptmp->uobject != uobj)
    897 				continue;
    898 
    899 			/*
    900 			 * unbusy the page if I/O is done.   note that for
    901 			 * pending I/O it is possible that the I/O op
    902 			 * finished before we relocked the object (in
    903 			 * which case the page is no longer busy).
    904 			 */
    905 
    906 			if (result != VM_PAGER_PEND) {
    907 				if (ptmp->flags & PG_WANTED)
    908 					/* still holding object lock */
    909 					wakeup(ptmp);
    910 
    911 				ptmp->flags &= ~(PG_WANTED|PG_BUSY);
    912 				UVM_PAGE_OWN(ptmp, NULL);
    913 				if (ptmp->flags & PG_RELEASED) {
    914 
    915 					/* pgo_releasepg wants this */
    916 					uvm_unlock_pageq();
    917 					if (!uvn_releasepg(ptmp, NULL))
    918 						return (TRUE);
    919 
    920 					uvm_lock_pageq();	/* relock */
    921 					continue;		/* next page */
    922 
    923 				} else {
    924 					ptmp->flags |= (PG_CLEAN|PG_CLEANCHK);
    925 					if ((flags & PGO_FREE) == 0)
    926 						pmap_clear_modify(
    927 						    PMAP_PGARG(ptmp));
    928 				}
    929 			}
    930 
    931 			/*
    932 			 * dispose of page
    933 			 */
    934 
    935 			if (flags & PGO_DEACTIVATE) {
    936 				if ((pp->pqflags & PQ_INACTIVE) == 0 &&
    937 				    pp->wire_count == 0) {
    938 					pmap_page_protect(PMAP_PGARG(ptmp),
    939 					    VM_PROT_NONE);
    940 					uvm_pagedeactivate(ptmp);
    941 				}
    942 
    943 			} else if (flags & PGO_FREE) {
    944 				if (result == VM_PAGER_PEND) {
    945 					if ((ptmp->flags & PG_BUSY) != 0)
    946 						/* signal for i/o done */
    947 						ptmp->flags |= PG_RELEASED;
    948 				} else {
    949 					if (result != VM_PAGER_OK) {
    950 						printf("uvn_flush: obj=%p, "
    951 						   "offset=0x%lx.  error %d\n",
    952 						    pp->uobject, pp->offset,
    953 						    result);
    954 						printf("uvn_flush: WARNING: "
    955 						    "changes to page may be "
    956 						    "lost!\n");
    957 						retval = FALSE;
    958 					}
    959 					pmap_page_protect(PMAP_PGARG(ptmp),
    960 					    VM_PROT_NONE);
    961 					uvm_pagefree(ptmp);
    962 				}
    963 			}
    964 
    965 		}		/* end of "lcv" for loop */
    966 
    967 	}		/* end of "pp" for loop */
    968 
    969 	/*
    970 	 * done with pagequeues: unlock
    971 	 */
    972 	uvm_unlock_pageq();
    973 
    974 	/*
    975 	 * now wait for all I/O if required.
    976 	 */
    977 #ifdef UBC
    978 	/*
    979 	 * XXX currently not needed since all i/o is sync.
    980 	 * merge this with VBWAIT.
    981 	 */
    982 #else
    983 	if (need_iosync) {
    984 
    985 		UVMHIST_LOG(maphist,"  <<DOING IOSYNC>>",0,0,0,0);
    986 		while (uvn->u_nio != 0) {
    987 			uvn->u_flags |= UVM_VNODE_IOSYNC;
    988 			UVM_UNLOCK_AND_WAIT(&uvn->u_nio, &uvn->u_obj.vmobjlock,
    989 			  FALSE, "uvn_flush",0);
    990 			simple_lock(&uvn->u_obj.vmobjlock);
    991 		}
    992 		if (uvn->u_flags & UVM_VNODE_IOSYNCWANTED)
    993 			wakeup(&uvn->u_flags);
    994 		uvn->u_flags &= ~(UVM_VNODE_IOSYNC|UVM_VNODE_IOSYNCWANTED);
    995 	}
    996 #endif
    997 
    998 	/* return, with object locked! */
    999 	UVMHIST_LOG(maphist,"<- done (retval=0x%x)",retval,0,0,0);
   1000 	return(retval);
   1001 }
   1002 
   1003 /*
   1004  * uvn_cluster
   1005  *
   1006  * we are about to do I/O in an object at offset.   this function is called
   1007  * to establish a range of offsets around "offset" in which we can cluster
   1008  * I/O.
   1009  *
   1010  * - currently doesn't matter if obj locked or not.
   1011  */
   1012 
   1013 static void
   1014 uvn_cluster(uobj, offset, loffset, hoffset)
   1015 	struct uvm_object *uobj;
   1016 	vaddr_t offset;
   1017 	vaddr_t *loffset, *hoffset; /* OUT */
   1018 {
   1019 	struct uvm_vnode *uvn = (struct uvm_vnode *) uobj;
   1020 	UVMHIST_FUNC("uvn_cluster"); UVMHIST_CALLED(ubchist);
   1021 
   1022 	*loffset = offset;
   1023 
   1024 	if (*loffset >= uvn->u_size)
   1025 #ifdef UBC
   1026 	{
   1027 		/* XXX nfs writes cause trouble with this */
   1028 		*loffset = *hoffset = offset;
   1029 UVMHIST_LOG(ubchist, "uvn_cluster: offset out of range: vp %p loffset 0x%x",
   1030 		      uobj, (int)*loffset, 0,0);
   1031 Debugger();
   1032 		return;
   1033 	}
   1034 #else
   1035 		panic("uvn_cluster: offset out of range: vp %p loffset 0x%x",
   1036 		      uobj, (int) *loffset);
   1037 #endif
   1038 
   1039 	/*
   1040 	 * XXX: old pager claims we could use VOP_BMAP to get maxcontig value.
   1041 	 */
   1042 	*hoffset = *loffset + MAXBSIZE;
   1043 	if (*hoffset > round_page(uvn->u_size))	/* past end? */
   1044 		*hoffset = round_page(uvn->u_size);
   1045 
   1046 	return;
   1047 }
   1048 
   1049 /*
   1050  * uvn_put: flush page data to backing store.
   1051  *
   1052  * => prefer map unlocked (not required)
   1053  * => object must be locked!   we will _unlock_ it before starting I/O.
   1054  * => flags: PGO_SYNCIO -- use sync. I/O
   1055  * => note: caller must set PG_CLEAN and pmap_clear_modify (if needed)
   1056  * => XXX: currently we use VOP_READ/VOP_WRITE which are only sync.
   1057  *	[thus we never do async i/o!  see iodone comment]
   1058  */
   1059 
   1060 static int
   1061 uvn_put(uobj, pps, npages, flags)
   1062 	struct uvm_object *uobj;
   1063 	struct vm_page **pps;
   1064 	int npages, flags;
   1065 {
   1066 	int retval, sync;
   1067 
   1068 	sync = (flags & PGO_SYNCIO) ? 1 : 0;
   1069 
   1070 	/* note: object locked */
   1071 	simple_lock_assert(&uobj->vmobjlock, SLOCK_LOCKED);
   1072 
   1073 	/* XXX why would the VOP need it locked? */
   1074 	/* currently, just to increment vp->v_numoutput (aka uvn->u_nio) */
   1075 	simple_unlock(&uobj->vmobjlock);
   1076 	retval = VOP_PUTPAGES((struct vnode *)uobj, pps, npages, sync, &retval);
   1077 	/* note: object unlocked */
   1078 	simple_lock_assert(&uobj->vmobjlock, SLOCK_UNLOCKED);
   1079 
   1080 	return(retval);
   1081 }
   1082 
   1083 
   1084 /*
   1085  * uvn_get: get pages (synchronously) from backing store
   1086  *
   1087  * => prefer map unlocked (not required)
   1088  * => object must be locked!  we will _unlock_ it before starting any I/O.
   1089  * => flags: PGO_ALLPAGES: get all of the pages
   1090  *           PGO_LOCKED: fault data structures are locked
   1091  * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx]
   1092  * => NOTE: caller must check for released pages!!
   1093  */
   1094 
   1095 static int
   1096 uvn_get(uobj, offset, pps, npagesp, centeridx, access_type, advice, flags)
   1097 	struct uvm_object *uobj;
   1098 	vaddr_t offset;
   1099 	struct vm_page **pps;		/* IN/OUT */
   1100 	int *npagesp;			/* IN (OUT if PGO_LOCKED) */
   1101 	int centeridx, advice, flags;
   1102 	vm_prot_t access_type;
   1103 {
   1104 	struct vnode *vp = (struct vnode *)uobj;
   1105 	int error;
   1106 
   1107 	simple_lock_assert(&uobj->vmobjlock, SLOCK_LOCKED);
   1108 	error = VOP_GETPAGES(vp, offset, pps, npagesp, centeridx,
   1109 			     access_type, advice, flags);
   1110 	simple_lock_assert(&uobj->vmobjlock, flags & PGO_LOCKED ?
   1111 			   SLOCK_LOCKED : SLOCK_UNLOCKED);
   1112 
   1113 	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
   1114 }
   1115 
   1116 /*
   1117  * uvn_findpages:
   1118  * return the page for the uobj and offset requested, allocating if needed.
   1119  * => uobj must be locked.
   1120  * => returned page will be BUSY.
   1121  */
   1122 
   1123 void
   1124 uvn_findpages(uobj, offset, npagesp, pps, flags)
   1125 	struct uvm_object *uobj;
   1126 	vaddr_t offset;
   1127 	int *npagesp;
   1128 	struct vm_page **pps;
   1129 	int flags;
   1130 {
   1131 	int i, rv, npages;
   1132 
   1133 	rv = 0;
   1134 	npages = *npagesp;
   1135 	for (i = 0; i < npages; i++, offset += PAGE_SIZE) {
   1136 		rv += uvn_findpage(uobj, offset, &pps[i], flags);
   1137 	}
   1138 	*npagesp = rv;
   1139 }
   1140 
   1141 
   1142 static int
   1143 uvn_findpage(uobj, offset, pps, flags)
   1144 	struct uvm_object *uobj;
   1145 	vaddr_t offset;
   1146 	struct vm_page **pps;
   1147 	int flags;
   1148 {
   1149 	struct vm_page *ptmp;
   1150 	UVMHIST_FUNC("uvn_findpage"); UVMHIST_CALLED(ubchist);
   1151 	UVMHIST_LOG(ubchist, "vp %p off 0x%lx", uobj, offset,0,0);
   1152 
   1153 	simple_lock_assert(&uobj->vmobjlock, SLOCK_LOCKED);
   1154 
   1155 	if (*pps == PGO_DONTCARE) {
   1156 		UVMHIST_LOG(ubchist, "dontcare", 0,0,0,0);
   1157 		return 0;
   1158 	}
   1159 #ifdef DIAGNOTISTIC
   1160 	if (*pps != NULL) {
   1161 		panic("uvn_findpage: *pps not NULL");
   1162 	}
   1163 #endif
   1164 
   1165 	for (;;) {
   1166 		/* look for an existing page */
   1167 		ptmp = uvm_pagelookup(uobj, offset);
   1168 
   1169 		/* nope?   allocate one now */
   1170 		if (ptmp == NULL) {
   1171 			if (flags & UFP_NOALLOC) {
   1172 				UVMHIST_LOG(ubchist, "noalloc", 0,0,0,0);
   1173 				return 0;
   1174 			}
   1175 			ptmp = uvm_pagealloc(uobj, offset, NULL);
   1176 			if (ptmp == NULL) {
   1177 				if (flags & UFP_NOWAIT) {
   1178 					UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
   1179 					return 0;
   1180 				}
   1181 				simple_unlock(&uobj->vmobjlock);
   1182 				uvm_wait("uvn_fp1");
   1183 				simple_lock(&uobj->vmobjlock);
   1184 				continue;
   1185 			}
   1186 			UVMHIST_LOG(ubchist, "alloced",0,0,0,0);
   1187 			break;
   1188 		} else if (flags & UFP_NOCACHE) {
   1189 			UVMHIST_LOG(ubchist, "nocache",0,0,0,0);
   1190 			return 0;
   1191 		}
   1192 
   1193 		/* page is there, see if we need to wait on it */
   1194 		if ((ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
   1195 			if (flags & UFP_NOWAIT) {
   1196 				UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
   1197 				return 0;
   1198 			}
   1199 			ptmp->flags |= PG_WANTED;
   1200 			UVM_UNLOCK_AND_WAIT(ptmp, &uobj->vmobjlock, 0,
   1201 					    "uvn_fp2",0);
   1202 			simple_lock(&uobj->vmobjlock);
   1203 			continue;
   1204 		}
   1205 
   1206 		/* BUSY the page and we're done. */
   1207 		ptmp->flags |= PG_BUSY;
   1208 		UVM_PAGE_OWN(ptmp, "uvn_findpage");
   1209 		UVMHIST_LOG(ubchist, "found",0,0,0,0);
   1210 		break;
   1211 	}
   1212 	*pps = ptmp;
   1213 	return 1;
   1214 }
   1215 
   1216 /*
   1217  * uvn_asyncget: start async I/O to bring pages into ram
   1218  *
   1219  * => caller must lock object(???XXX: see if this is best)
   1220  * => could be called from uvn_get or a madvise() fault-ahead.
   1221  * => if it fails, it doesn't matter.
   1222  */
   1223 
   1224 static int
   1225 uvn_asyncget(uobj, offset, npages)
   1226 	struct uvm_object *uobj;
   1227 	vaddr_t offset;
   1228 	int npages;
   1229 {
   1230 
   1231 	/*
   1232 	 * XXXCDC: we can't do async I/O yet
   1233 	 */
   1234 	printf("uvn_asyncget called\n");
   1235 	return (KERN_SUCCESS);
   1236 }
   1237 
   1238 /*
   1239  * uvm_vnp_uncache: disable "persisting" in a vnode... when last reference
   1240  * is gone we will kill the object (flushing dirty pages back to the vnode
   1241  * if needed).
   1242  *
   1243  * => returns TRUE if there was no uvm_object attached or if there was
   1244  *	one and we killed it [i.e. if there is no active uvn]
   1245  * => called with the vnode VOP_LOCK'd [we will unlock it for I/O, if
   1246  *	needed]
   1247  *
   1248  * => XXX: given that we now kill uvn's when a vnode is recycled (without
   1249  *	having to hold a reference on the vnode) and given a working
   1250  *	uvm_vnp_sync(), how does that effect the need for this function?
   1251  *      [XXXCDC: seems like it can die?]
   1252  *
   1253  * => XXX: this function should DIE once we merge the VM and buffer
   1254  *	cache.
   1255  *
   1256  * research shows that this is called in the following places:
   1257  * ext2fs_truncate, ffs_truncate, detrunc[msdosfs]: called when vnode
   1258  *	changes sizes
   1259  * ext2fs_write, WRITE [ufs_readwrite], msdosfs_write: called when we
   1260  *	are written to
   1261  * ex2fs_chmod, ufs_chmod: called if VTEXT vnode and the sticky bit
   1262  *	is off
   1263  * ffs_realloccg: when we can't extend the current block and have
   1264  *	to allocate a new one we call this [XXX: why?]
   1265  * nfsrv_rename, rename_files: called when the target filename is there
   1266  *	and we want to remove it
   1267  * nfsrv_remove, sys_unlink: called on file we are removing
   1268  * nfsrv_access: if VTEXT and we want WRITE access and we don't uncache
   1269  *	then return "text busy"
   1270  * nfs_open: seems to uncache any file opened with nfs
   1271  * vn_writechk: if VTEXT vnode and can't uncache return "text busy"
   1272  */
   1273 
   1274 boolean_t
   1275 uvm_vnp_uncache(vp)
   1276 	struct vnode *vp;
   1277 {
   1278 	return(TRUE);
   1279 }
   1280 
   1281 /*
   1282  * uvm_vnp_setsize: grow or shrink a vnode uvn
   1283  *
   1284  * grow   => just update size value
   1285  * shrink => toss un-needed pages
   1286  *
   1287  * => we assume that the caller has a reference of some sort to the
   1288  *	vnode in question so that it will not be yanked out from under
   1289  *	us.
   1290  *
   1291  * called from:
   1292  *  => truncate fns (ext2fs_truncate, ffs_truncate, detrunc[msdos])
   1293  *  => "write" fns (ext2fs_write, WRITE [ufs/ufs], msdosfs_write, nfs_write)
   1294  *  => ffs_balloc [XXX: why? doesn't WRITE handle?]
   1295  *  => NFS: nfs_loadattrcache, nfs_getattrcache, nfs_setattr
   1296  *  => union fs: union_newsize
   1297  */
   1298 
   1299 void
   1300 uvm_vnp_setsize(vp, newsize)
   1301 	struct vnode *vp;
   1302 	u_quad_t newsize;
   1303 {
   1304 	struct uvm_vnode *uvn = &vp->v_uvm;
   1305 
   1306 	/*
   1307 	 * lock uvn and check for valid object, and if valid: do it!
   1308 	 */
   1309 	simple_lock(&uvn->u_obj.vmobjlock);
   1310 #ifdef UBC
   1311 #else
   1312 	if (uvn->u_flags & UVM_VNODE_VALID) {
   1313 #endif
   1314 		/*
   1315 		 * make sure that the newsize fits within a vaddr_t
   1316 		 * XXX: need to revise addressing data types
   1317 		 */
   1318 
   1319 		if (newsize > (vaddr_t) -PAGE_SIZE) {
   1320 #ifdef DEBUG
   1321 			printf("uvm_vnp_setsize: vn %p size truncated "
   1322 			    "%qx->%lx\n", vp, newsize, (vaddr_t)-PAGE_SIZE);
   1323 #endif
   1324 			newsize = (vaddr_t)-PAGE_SIZE;
   1325 		}
   1326 
   1327 		/*
   1328 		 * now check if the size has changed: if we shrink we had better
   1329 		 * toss some pages...
   1330 		 */
   1331 
   1332 #ifdef UBC
   1333 		if (uvn->u_size > newsize && uvn->u_size != VSIZENOTSET) {
   1334 #else
   1335 /*
   1336 		if (uvn->u_size > newsize) {
   1337 */
   1338 #endif
   1339 			(void)uvn_flush(&uvn->u_obj, (vaddr_t)newsize,
   1340 					uvn->u_size, PGO_FREE);
   1341 		}
   1342 #ifdef DEBUGxx
   1343 printf("uvm_vnp_setsize: vp %p newsize 0x%x\n", vp, (int)newsize);
   1344 #endif
   1345 		uvn->u_size = (vaddr_t)newsize;
   1346 #ifdef UBC
   1347 #else
   1348 	}
   1349 #endif
   1350 	simple_unlock(&uvn->u_obj.vmobjlock);
   1351 }
   1352 
   1353 /*
   1354  * uvm_vnp_sync: flush all dirty VM pages back to their backing vnodes.
   1355  *
   1356  * => called from sys_sync with no VM structures locked
   1357  * => only one process can do a sync at a time (because the uvn
   1358  *    structure only has one queue for sync'ing).  we ensure this
   1359  *    by holding the uvn_sync_lock while the sync is in progress.
   1360  *    other processes attempting a sync will sleep on this lock
   1361  *    until we are done.
   1362  */
   1363 
   1364 void
   1365 uvm_vnp_sync(mp)
   1366 	struct mount *mp;
   1367 {
   1368 	struct uvm_vnode *uvn;
   1369 	struct vnode *vp;
   1370 	boolean_t got_lock;
   1371 
   1372 	/*
   1373 	 * step 1: ensure we are only ones using the uvn_sync_q by locking
   1374 	 * our lock...
   1375 	 */
   1376 	lockmgr(&uvn_sync_lock, LK_EXCLUSIVE, (void *)0);
   1377 
   1378 	/*
   1379 	 * step 2: build up a simpleq of uvns of interest based on the
   1380 	 * write list.   we gain a reference to uvns of interest.  must
   1381 	 * be careful about locking uvn's since we will be holding uvn_wl_lock
   1382 	 * in the body of the loop.
   1383 	 */
   1384 	SIMPLEQ_INIT(&uvn_sync_q);
   1385 	simple_lock(&uvn_wl_lock);
   1386 	for (uvn = LIST_FIRST(&uvn_wlist); uvn != NULL;
   1387 	     uvn = LIST_NEXT(uvn, u_wlist)) {
   1388 
   1389 		vp = (struct vnode *) uvn;
   1390 		if (mp && vp->v_mount != mp)
   1391 			continue;
   1392 
   1393 		/* attempt to gain reference */
   1394 		while ((got_lock = simple_lock_try(&uvn->u_obj.vmobjlock)) ==
   1395 		    						FALSE &&
   1396 				(uvn->u_flags & UVM_VNODE_BLOCKED) == 0)
   1397 			/* spin */ ;
   1398 
   1399 		/*
   1400 		 * we will exit the loop if either if the following are true:
   1401 		 *  - we got the lock [always true if NCPU == 1]
   1402 		 *  - we failed to get the lock but noticed the vnode was
   1403 		 * 	"blocked" -- in this case the vnode must be a dying
   1404 		 *	vnode, and since dying vnodes are in the process of
   1405 		 *	being flushed out, we can safely skip this one
   1406 		 *
   1407 		 * we want to skip over the vnode if we did not get the lock,
   1408 		 * or if the vnode is already dying (due to the above logic).
   1409 		 *
   1410 		 * note that uvn must already be valid because we found it on
   1411 		 * the wlist (this also means it can't be ALOCK'd).
   1412 		 */
   1413 		if (!got_lock || (uvn->u_flags & UVM_VNODE_BLOCKED) != 0) {
   1414 			if (got_lock)
   1415 				simple_unlock(&uvn->u_obj.vmobjlock);
   1416 			continue;		/* skip it */
   1417 		}
   1418 
   1419 		/*
   1420 		 * gain reference.   watch out for persisting uvns (need to
   1421 		 * regain vnode REF).
   1422 		 */
   1423 #ifdef UBC
   1424 		vget(vp, LK_INTERLOCK);
   1425 #else
   1426 		if (uvn->u_obj.uo_refs == 0)
   1427 			VREF(vp);
   1428 		uvn->u_obj.uo_refs++;
   1429 		simple_unlock(&uvn->u_obj.vmobjlock);
   1430 #endif
   1431 
   1432 		/*
   1433 		 * got it!
   1434 		 */
   1435 		SIMPLEQ_INSERT_HEAD(&uvn_sync_q, uvn, u_syncq);
   1436 	}
   1437 	simple_unlock(&uvn_wl_lock);
   1438 
   1439 	/*
   1440 	 * step 3: we now have a list of uvn's that may need cleaning.
   1441 	 * we are holding the uvn_sync_lock, but have dropped the uvn_wl_lock
   1442 	 * (so we can now safely lock uvn's again).
   1443 	 */
   1444 
   1445 	for (uvn = uvn_sync_q.sqh_first ; uvn ; uvn = uvn->u_syncq.sqe_next) {
   1446 		simple_lock(&uvn->u_obj.vmobjlock);
   1447 #ifdef UBC
   1448 #else
   1449 #ifdef DIAGNOSTIC
   1450 		if (uvn->u_flags & UVM_VNODE_DYING) {
   1451 			printf("uvm_vnp_sync: dying vnode on sync list\n");
   1452 		}
   1453 #endif
   1454 #endif
   1455 		/*
   1456 		 * XXX use PGO_SYNCIO for now to avoid problems with
   1457 		 * uvmexp.paging.
   1458 		 */
   1459 
   1460 		uvn_flush(&uvn->u_obj, 0, 0,
   1461 		    PGO_CLEANIT|PGO_ALLPAGES|PGO_DOACTCLUST|PGO_SYNCIO);
   1462 
   1463 		/*
   1464 		 * if we have the only reference and we just cleaned the uvn,
   1465 		 * then we can pull it out of the UVM_VNODE_WRITEABLE state
   1466 		 * thus allowing us to avoid thinking about flushing it again
   1467 		 * on later sync ops.
   1468 		 */
   1469 		if (uvn->u_obj.uo_refs == 1 &&
   1470 		    (uvn->u_flags & UVM_VNODE_WRITEABLE)) {
   1471 			simple_lock(&uvn_wl_lock);
   1472 			LIST_REMOVE(uvn, u_wlist);
   1473 			uvn->u_flags &= ~UVM_VNODE_WRITEABLE;
   1474 			simple_unlock(&uvn_wl_lock);
   1475 		}
   1476 
   1477 		simple_unlock(&uvn->u_obj.vmobjlock);
   1478 
   1479 		/* now drop our reference to the uvn */
   1480 		uvn_detach(&uvn->u_obj);
   1481 	}
   1482 
   1483 	/*
   1484 	 * done!  release sync lock
   1485 	 */
   1486 	lockmgr(&uvn_sync_lock, LK_RELEASE, (void *)0);
   1487 }
   1488 
   1489 
   1490 /*
   1491  * uvm_vnp_setpageblknos:  find pages and set their blknos.
   1492  * this is used for two purposes:  updating blknos in existing pages
   1493  * when the data is relocated on disk, and preallocating pages when
   1494  * those pages are about to be completely overwritten.
   1495  *
   1496  * => vp's uobj should not be locked, and is returned not locked.
   1497  */
   1498 
   1499 void
   1500 uvm_vnp_setpageblknos(vp, off, len, blkno, ufp_flags, zero)
   1501 	struct vnode *vp;
   1502 	off_t off, len;
   1503 	daddr_t blkno;
   1504 	int ufp_flags;
   1505 	boolean_t zero;
   1506 {
   1507 	int i;
   1508 	int npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
   1509 	struct vm_page *pgs[npages];
   1510 	struct uvm_object *uobj = &vp->v_uvm.u_obj;
   1511 
   1512 	memset(pgs, 0, npages);
   1513 	simple_lock(&uobj->vmobjlock);
   1514 	uvn_findpages(uobj, trunc_page(off), &npages, pgs, ufp_flags);
   1515 	for (i = 0; i < npages; i++) {
   1516 		if (pgs[i] == NULL) {
   1517 			continue;
   1518 		}
   1519 		pgs[i]->blkno = blkno;
   1520 		blkno += PAGE_SIZE >> DEV_BSHIFT;
   1521 		if (zero) {
   1522 			uvm_pagezero(pgs[i]);
   1523 		}
   1524 	}
   1525 	uvm_pager_dropcluster(uobj, NULL, pgs, &npages, PGO_PDFREECLUST, 0);
   1526 	simple_unlock(&uobj->vmobjlock);
   1527 }
   1528 
   1529 
   1530 /*
   1531  * uvm_vnp_zerorange:  set a range of bytes in a file to zero.
   1532  * this is called from fs-specific code when truncating a file
   1533  * to zero the part of last block that is past the new end-of-file.
   1534  */
   1535 void
   1536 uvm_vnp_zerorange(vp, off, len)
   1537 	struct vnode *vp;
   1538 	off_t off;
   1539 	size_t len;
   1540 {
   1541 	void *win;
   1542 
   1543 	/*
   1544 	 * XXX invent kzero() and use it
   1545 	 */
   1546 
   1547 	while (len) {
   1548 		int byteoff = off & (MAXBSIZE - 1);
   1549 		int bytelen = min(len, MAXBSIZE - byteoff);
   1550 
   1551 		win = ubc_alloc(&vp->v_uvm.u_obj, off, bytelen, UBC_WRITE);
   1552 		memset(win + (off & (MAXBSIZE - 1)), 0, bytelen);
   1553 		ubc_release(win, 0);
   1554 	}
   1555 }
   1556