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