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