Home | History | Annotate | Line # | Download | only in uvm
uvm_vnode.c revision 1.26.2.8
      1  1.26.2.8   bouyer /*	$NetBSD: uvm_vnode.c,v 1.26.2.8 2001/03/23 11:38:28 bouyer Exp $	*/
      2       1.1      mrg 
      3       1.1      mrg /*
      4       1.1      mrg  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5       1.1      mrg  * Copyright (c) 1991, 1993
      6       1.1      mrg  *      The Regents of the University of California.
      7       1.1      mrg  * Copyright (c) 1990 University of Utah.
      8       1.1      mrg  *
      9       1.1      mrg  * All rights reserved.
     10       1.1      mrg  *
     11       1.1      mrg  * This code is derived from software contributed to Berkeley by
     12       1.1      mrg  * the Systems Programming Group of the University of Utah Computer
     13       1.1      mrg  * Science Department.
     14       1.1      mrg  *
     15       1.1      mrg  * Redistribution and use in source and binary forms, with or without
     16       1.1      mrg  * modification, are permitted provided that the following conditions
     17       1.1      mrg  * are met:
     18       1.1      mrg  * 1. Redistributions of source code must retain the above copyright
     19       1.1      mrg  *    notice, this list of conditions and the following disclaimer.
     20       1.1      mrg  * 2. Redistributions in binary form must reproduce the above copyright
     21       1.1      mrg  *    notice, this list of conditions and the following disclaimer in the
     22       1.1      mrg  *    documentation and/or other materials provided with the distribution.
     23       1.1      mrg  * 3. All advertising materials mentioning features or use of this software
     24       1.1      mrg  *    must display the following acknowledgement:
     25       1.1      mrg  *      This product includes software developed by Charles D. Cranor,
     26       1.1      mrg  *	Washington University, the University of California, Berkeley and
     27       1.1      mrg  *	its contributors.
     28       1.1      mrg  * 4. Neither the name of the University nor the names of its contributors
     29       1.1      mrg  *    may be used to endorse or promote products derived from this software
     30       1.1      mrg  *    without specific prior written permission.
     31       1.1      mrg  *
     32       1.1      mrg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33       1.1      mrg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34       1.1      mrg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35       1.1      mrg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36       1.1      mrg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37       1.1      mrg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38       1.1      mrg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39       1.1      mrg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40       1.1      mrg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41       1.1      mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42       1.1      mrg  * SUCH DAMAGE.
     43       1.1      mrg  *
     44       1.1      mrg  *      @(#)vnode_pager.c       8.8 (Berkeley) 2/13/94
     45       1.3      mrg  * from: Id: uvm_vnode.c,v 1.1.2.26 1998/02/02 20:38:07 chuck Exp
     46       1.1      mrg  */
     47       1.1      mrg 
     48       1.6  thorpej #include "fs_nfs.h"
     49       1.4      mrg #include "opt_uvmhist.h"
     50  1.26.2.3   bouyer #include "opt_ddb.h"
     51       1.4      mrg 
     52       1.1      mrg /*
     53       1.1      mrg  * uvm_vnode.c: the vnode pager.
     54       1.1      mrg  */
     55       1.1      mrg 
     56       1.1      mrg #include <sys/param.h>
     57       1.1      mrg #include <sys/systm.h>
     58  1.26.2.3   bouyer #include <sys/kernel.h>
     59       1.1      mrg #include <sys/proc.h>
     60       1.1      mrg #include <sys/malloc.h>
     61       1.1      mrg #include <sys/vnode.h>
     62      1.13  thorpej #include <sys/disklabel.h>
     63      1.13  thorpej #include <sys/ioctl.h>
     64      1.13  thorpej #include <sys/fcntl.h>
     65      1.13  thorpej #include <sys/conf.h>
     66  1.26.2.3   bouyer #include <sys/pool.h>
     67  1.26.2.3   bouyer #include <sys/mount.h>
     68      1.13  thorpej 
     69      1.13  thorpej #include <miscfs/specfs/specdev.h>
     70       1.1      mrg 
     71       1.1      mrg #include <uvm/uvm.h>
     72       1.1      mrg #include <uvm/uvm_vnode.h>
     73       1.1      mrg 
     74       1.1      mrg /*
     75       1.1      mrg  * functions
     76       1.1      mrg  */
     77       1.1      mrg 
     78  1.26.2.3   bouyer static void		uvn_cluster __P((struct uvm_object *, voff_t, voff_t *,
     79  1.26.2.3   bouyer 					 voff_t *));
     80  1.26.2.3   bouyer static void		uvn_detach __P((struct uvm_object *));
     81  1.26.2.3   bouyer static int		uvn_findpage __P((struct uvm_object *, voff_t,
     82  1.26.2.3   bouyer 					  struct vm_page **, int));
     83  1.26.2.3   bouyer static boolean_t	uvn_flush __P((struct uvm_object *, voff_t, voff_t,
     84  1.26.2.3   bouyer 				       int));
     85  1.26.2.3   bouyer static int		uvn_get __P((struct uvm_object *, voff_t, vm_page_t *,
     86  1.26.2.3   bouyer 				     int *, int, vm_prot_t, int, int));
     87  1.26.2.3   bouyer static int		uvn_put __P((struct uvm_object *, vm_page_t *, int,
     88  1.26.2.3   bouyer 				     boolean_t));
     89  1.26.2.3   bouyer static void		uvn_reference __P((struct uvm_object *));
     90  1.26.2.3   bouyer static boolean_t	uvn_releasepg __P((struct vm_page *,
     91  1.26.2.3   bouyer 					   struct vm_page **));
     92       1.1      mrg 
     93       1.1      mrg /*
     94       1.1      mrg  * master pager structure
     95       1.1      mrg  */
     96       1.1      mrg 
     97       1.1      mrg struct uvm_pagerops uvm_vnodeops = {
     98  1.26.2.3   bouyer 	NULL,
     99       1.8      mrg 	uvn_reference,
    100       1.8      mrg 	uvn_detach,
    101  1.26.2.3   bouyer 	NULL,
    102       1.8      mrg 	uvn_flush,
    103       1.8      mrg 	uvn_get,
    104       1.8      mrg 	uvn_put,
    105       1.8      mrg 	uvn_cluster,
    106  1.26.2.3   bouyer 	uvm_mk_pcluster,
    107       1.8      mrg 	uvn_releasepg,
    108       1.1      mrg };
    109       1.1      mrg 
    110       1.1      mrg /*
    111       1.1      mrg  * the ops!
    112       1.1      mrg  */
    113       1.1      mrg 
    114       1.1      mrg /*
    115       1.1      mrg  * uvn_attach
    116       1.1      mrg  *
    117       1.1      mrg  * attach a vnode structure to a VM object.  if the vnode is already
    118       1.1      mrg  * attached, then just bump the reference count by one and return the
    119       1.1      mrg  * VM object.   if not already attached, attach and return the new VM obj.
    120       1.1      mrg  * the "accessprot" tells the max access the attaching thread wants to
    121       1.1      mrg  * our pages.
    122       1.1      mrg  *
    123       1.1      mrg  * => caller must _not_ already be holding the lock on the uvm_object.
    124       1.1      mrg  * => in fact, nothing should be locked so that we can sleep here.
    125       1.1      mrg  * => note that uvm_object is first thing in vnode structure, so their
    126       1.1      mrg  *    pointers are equiv.
    127       1.1      mrg  */
    128       1.1      mrg 
    129       1.8      mrg struct uvm_object *
    130       1.8      mrg uvn_attach(arg, accessprot)
    131       1.8      mrg 	void *arg;
    132       1.8      mrg 	vm_prot_t accessprot;
    133       1.8      mrg {
    134       1.8      mrg 	struct vnode *vp = arg;
    135       1.8      mrg 	struct uvm_vnode *uvn = &vp->v_uvm;
    136       1.8      mrg 	struct vattr vattr;
    137  1.26.2.3   bouyer 	int result;
    138      1.13  thorpej 	struct partinfo pi;
    139  1.26.2.3   bouyer 	voff_t used_vnode_size;
    140       1.8      mrg 	UVMHIST_FUNC("uvn_attach"); UVMHIST_CALLED(maphist);
    141       1.8      mrg 
    142       1.8      mrg 	UVMHIST_LOG(maphist, "(vn=0x%x)", arg,0,0,0);
    143  1.26.2.3   bouyer 	used_vnode_size = (voff_t)0;
    144      1.13  thorpej 
    145       1.8      mrg 	/*
    146       1.8      mrg 	 * first get a lock on the uvn.
    147       1.8      mrg 	 */
    148       1.8      mrg 	simple_lock(&uvn->u_obj.vmobjlock);
    149  1.26.2.3   bouyer 	while (uvn->u_flags & VXLOCK) {
    150  1.26.2.3   bouyer 		uvn->u_flags |= VXWANT;
    151       1.8      mrg 		UVMHIST_LOG(maphist, "  SLEEPING on blocked vn",0,0,0,0);
    152       1.8      mrg 		UVM_UNLOCK_AND_WAIT(uvn, &uvn->u_obj.vmobjlock, FALSE,
    153       1.8      mrg 		    "uvn_attach", 0);
    154       1.8      mrg 		simple_lock(&uvn->u_obj.vmobjlock);
    155       1.8      mrg 		UVMHIST_LOG(maphist,"  WOKE UP",0,0,0,0);
    156       1.8      mrg 	}
    157       1.1      mrg 
    158       1.8      mrg 	/*
    159      1.18   bouyer 	 * if we're mapping a BLK device, make sure it is a disk.
    160      1.13  thorpej 	 */
    161      1.13  thorpej 	if (vp->v_type == VBLK && bdevsw[major(vp->v_rdev)].d_type != D_DISK) {
    162  1.26.2.3   bouyer 		simple_unlock(&uvn->u_obj.vmobjlock);
    163      1.13  thorpej 		UVMHIST_LOG(maphist,"<- done (VBLK not D_DISK!)", 0,0,0,0);
    164      1.13  thorpej 		return(NULL);
    165      1.13  thorpej 	}
    166      1.13  thorpej 
    167  1.26.2.3   bouyer #ifdef DIAGNOSTIC
    168  1.26.2.3   bouyer 	if (vp->v_type != VREG) {
    169  1.26.2.3   bouyer 		panic("uvn_attach: vp %p not VREG", vp);
    170  1.26.2.3   bouyer 	}
    171  1.26.2.3   bouyer #endif
    172       1.8      mrg 
    173       1.8      mrg 	/*
    174  1.26.2.3   bouyer 	 * set up our idea of the size
    175  1.26.2.3   bouyer 	 * if this hasn't been done already.
    176       1.8      mrg 	 */
    177  1.26.2.3   bouyer 	if (uvn->u_size == VSIZENOTSET) {
    178  1.26.2.3   bouyer 
    179  1.26.2.3   bouyer 	uvn->u_flags |= VXLOCK;
    180       1.8      mrg 	simple_unlock(&uvn->u_obj.vmobjlock); /* drop lock in case we sleep */
    181       1.8      mrg 		/* XXX: curproc? */
    182      1.13  thorpej 	if (vp->v_type == VBLK) {
    183      1.13  thorpej 		/*
    184      1.13  thorpej 		 * We could implement this as a specfs getattr call, but:
    185      1.13  thorpej 		 *
    186      1.13  thorpej 		 *	(1) VOP_GETATTR() would get the file system
    187      1.13  thorpej 		 *	    vnode operation, not the specfs operation.
    188      1.13  thorpej 		 *
    189      1.13  thorpej 		 *	(2) All we want is the size, anyhow.
    190      1.13  thorpej 		 */
    191      1.13  thorpej 		result = (*bdevsw[major(vp->v_rdev)].d_ioctl)(vp->v_rdev,
    192      1.13  thorpej 		    DIOCGPART, (caddr_t)&pi, FREAD, curproc);
    193      1.13  thorpej 		if (result == 0) {
    194      1.13  thorpej 			/* XXX should remember blocksize */
    195  1.26.2.3   bouyer 			used_vnode_size = (voff_t)pi.disklab->d_secsize *
    196  1.26.2.3   bouyer 			    (voff_t)pi.part->p_size;
    197      1.13  thorpej 		}
    198      1.13  thorpej 	} else {
    199      1.13  thorpej 		result = VOP_GETATTR(vp, &vattr, curproc->p_ucred, curproc);
    200      1.13  thorpej 		if (result == 0)
    201      1.13  thorpej 			used_vnode_size = vattr.va_size;
    202       1.8      mrg 	}
    203       1.1      mrg 
    204       1.8      mrg 	/* relock object */
    205  1.26.2.3   bouyer 	simple_lock(&uvn->u_obj.vmobjlock);
    206  1.26.2.3   bouyer 
    207  1.26.2.3   bouyer 	if (uvn->u_flags & VXWANT)
    208  1.26.2.3   bouyer 		wakeup(uvn);
    209  1.26.2.3   bouyer 	uvn->u_flags &= ~(VXLOCK|VXWANT);
    210       1.1      mrg 
    211       1.8      mrg 	if (result != 0) {
    212       1.8      mrg 		simple_unlock(&uvn->u_obj.vmobjlock); /* drop lock */
    213       1.8      mrg 		UVMHIST_LOG(maphist,"<- done (VOP_GETATTR FAILED!)", 0,0,0,0);
    214       1.8      mrg 		return(NULL);
    215       1.8      mrg 	}
    216       1.8      mrg 	uvn->u_size = used_vnode_size;
    217       1.8      mrg 
    218       1.8      mrg 	}
    219       1.8      mrg 
    220  1.26.2.3   bouyer 	/* unlock and return */
    221       1.8      mrg 	simple_unlock(&uvn->u_obj.vmobjlock);
    222  1.26.2.3   bouyer 	UVMHIST_LOG(maphist,"<- done, refcnt=%d", uvn->u_obj.uo_refs,
    223  1.26.2.3   bouyer 	    0, 0, 0);
    224  1.26.2.3   bouyer 	return (&uvn->u_obj);
    225       1.1      mrg }
    226       1.1      mrg 
    227       1.1      mrg 
    228       1.1      mrg /*
    229       1.1      mrg  * uvn_reference
    230       1.1      mrg  *
    231       1.1      mrg  * duplicate a reference to a VM object.  Note that the reference
    232       1.1      mrg  * count must already be at least one (the passed in reference) so
    233       1.1      mrg  * there is no chance of the uvn being killed or locked out here.
    234       1.1      mrg  *
    235       1.1      mrg  * => caller must call with object unlocked.
    236       1.1      mrg  * => caller must be using the same accessprot as was used at attach time
    237       1.1      mrg  */
    238       1.1      mrg 
    239       1.1      mrg 
    240       1.8      mrg static void
    241       1.8      mrg uvn_reference(uobj)
    242       1.8      mrg 	struct uvm_object *uobj;
    243       1.1      mrg {
    244  1.26.2.3   bouyer 	VREF((struct vnode *)uobj);
    245       1.1      mrg }
    246       1.1      mrg 
    247       1.1      mrg /*
    248       1.1      mrg  * uvn_detach
    249       1.1      mrg  *
    250       1.1      mrg  * remove a reference to a VM object.
    251       1.1      mrg  *
    252       1.1      mrg  * => caller must call with object unlocked and map locked.
    253       1.1      mrg  */
    254       1.8      mrg static void
    255       1.8      mrg uvn_detach(uobj)
    256       1.8      mrg 	struct uvm_object *uobj;
    257       1.8      mrg {
    258  1.26.2.3   bouyer 	vrele((struct vnode *)uobj);
    259       1.1      mrg }
    260       1.1      mrg 
    261       1.1      mrg /*
    262       1.1      mrg  * uvn_releasepg: handled a released page in a uvn
    263       1.1      mrg  *
    264       1.1      mrg  * => "pg" is a PG_BUSY [caller owns it], PG_RELEASED page that we need
    265       1.1      mrg  *	to dispose of.
    266       1.1      mrg  * => caller must handled PG_WANTED case
    267       1.1      mrg  * => called with page's object locked, pageq's unlocked
    268       1.1      mrg  * => returns TRUE if page's object is still alive, FALSE if we
    269       1.1      mrg  *	killed the page's object.    if we return TRUE, then we
    270       1.1      mrg  *	return with the object locked.
    271  1.26.2.3   bouyer  * => if (nextpgp != NULL) => we return the next page on the queue, and return
    272       1.1      mrg  *				with the page queues locked [for pagedaemon]
    273       1.1      mrg  * => if (nextpgp == NULL) => we return with page queues unlocked [normal case]
    274       1.1      mrg  * => we kill the uvn if it is not referenced and we are suppose to
    275       1.1      mrg  *	kill it ("relkill").
    276       1.1      mrg  */
    277       1.1      mrg 
    278       1.8      mrg boolean_t
    279       1.8      mrg uvn_releasepg(pg, nextpgp)
    280       1.8      mrg 	struct vm_page *pg;
    281       1.8      mrg 	struct vm_page **nextpgp;	/* OUT */
    282       1.1      mrg {
    283  1.26.2.3   bouyer 	KASSERT(pg->flags & PG_RELEASED);
    284       1.8      mrg 
    285       1.8      mrg 	/*
    286       1.8      mrg 	 * dispose of the page [caller handles PG_WANTED]
    287       1.8      mrg 	 */
    288      1.26      chs 	pmap_page_protect(pg, VM_PROT_NONE);
    289       1.8      mrg 	uvm_lock_pageq();
    290       1.8      mrg 	if (nextpgp)
    291  1.26.2.3   bouyer 		*nextpgp = TAILQ_NEXT(pg, pageq);
    292       1.8      mrg 	uvm_pagefree(pg);
    293       1.8      mrg 	if (!nextpgp)
    294       1.8      mrg 		uvm_unlock_pageq();
    295       1.8      mrg 
    296       1.8      mrg 	return (TRUE);
    297       1.1      mrg }
    298       1.1      mrg 
    299       1.1      mrg /*
    300       1.1      mrg  * issues to consider:
    301       1.1      mrg  * there are two tailq's in the uvm. structure... one for pending async
    302       1.1      mrg  * i/o and one for "done" async i/o.   to do an async i/o one puts
    303  1.26.2.7   bouyer  * a buf on the "pending" list (protected by splbio()), starts the
    304  1.26.2.7   bouyer  * i/o and returns 0.    when the i/o is done, we expect
    305       1.1      mrg  * some sort of "i/o done" function to be called (at splbio(), interrupt
    306  1.26.2.7   bouyer  * time).   this function should remove the buf from the pending list
    307       1.1      mrg  * and place it on the "done" list and wakeup the daemon.   the daemon
    308       1.1      mrg  * will run at normal spl() and will remove all items from the "done"
    309  1.26.2.7   bouyer  * list and call the iodone hook for each done request (see uvm_pager.c).
    310       1.1      mrg  *
    311       1.1      mrg  * => return KERN_SUCCESS (aio finished, free it).  otherwise requeue for
    312       1.1      mrg  *	later collection.
    313       1.1      mrg  * => called with pageq's locked by the daemon.
    314       1.1      mrg  *
    315       1.1      mrg  * general outline:
    316       1.1      mrg  * - "try" to lock object.   if fail, just return (will try again later)
    317       1.1      mrg  * - drop "u_nio" (this req is done!)
    318       1.1      mrg  * - if (object->iosync && u_naio == 0) { wakeup &uvn->u_naio }
    319       1.1      mrg  * - get "page" structures (atop?).
    320       1.1      mrg  * - handle "wanted" pages
    321       1.1      mrg  * - handle "released" pages [using pgo_releasepg]
    322       1.1      mrg  *   >>> pgo_releasepg may kill the object
    323       1.1      mrg  * dont forget to look at "object" wanted flag in all cases.
    324       1.1      mrg  */
    325       1.1      mrg 
    326       1.1      mrg 
    327       1.1      mrg /*
    328       1.1      mrg  * uvn_flush: flush pages out of a uvm object.
    329       1.1      mrg  *
    330  1.26.2.6   bouyer  * => "stop == 0" means flush all pages at or after "start".
    331       1.1      mrg  * => object should be locked by caller.   we may _unlock_ the object
    332  1.26.2.5   bouyer  *	if (and only if) we need to clean a page (PGO_CLEANIT), or
    333  1.26.2.5   bouyer  *	if PGO_SYNCIO is set and there are pages busy.
    334       1.1      mrg  *	we return with the object locked.
    335  1.26.2.5   bouyer  * => if PGO_CLEANIT or PGO_SYNCIO is set, we may block (due to I/O).
    336  1.26.2.5   bouyer  *	thus, a caller might want to unlock higher level resources
    337  1.26.2.5   bouyer  *	(e.g. vm_map) before calling flush.
    338  1.26.2.5   bouyer  * => if neither PGO_CLEANIT nor PGO_SYNCIO is set, then we will neither
    339  1.26.2.5   bouyer  *	unlock the object nor block.
    340  1.26.2.5   bouyer  * => if PGO_ALLPAGES is set, then all pages in the object are valid targets
    341       1.1      mrg  *	for flushing.
    342       1.1      mrg  * => NOTE: we rely on the fact that the object's memq is a TAILQ and
    343       1.1      mrg  *	that new pages are inserted on the tail end of the list.   thus,
    344       1.1      mrg  *	we can make a complete pass through the object in one go by starting
    345       1.1      mrg  *	at the head and working towards the tail (new pages are put in
    346       1.1      mrg  *	front of us).
    347       1.1      mrg  * => NOTE: we are allowed to lock the page queues, so the caller
    348       1.1      mrg  *	must not be holding the lock on them [e.g. pagedaemon had
    349       1.1      mrg  *	better not call us with the queues locked]
    350       1.1      mrg  * => we return TRUE unless we encountered some sort of I/O error
    351       1.1      mrg  *
    352       1.1      mrg  * comment on "cleaning" object and PG_BUSY pages:
    353       1.1      mrg  *	this routine is holding the lock on the object.   the only time
    354       1.1      mrg  *	that it can run into a PG_BUSY page that it does not own is if
    355       1.1      mrg  *	some other process has started I/O on the page (e.g. either
    356       1.1      mrg  *	a pagein, or a pageout).    if the PG_BUSY page is being paged
    357       1.1      mrg  *	in, then it can not be dirty (!PG_CLEAN) because no one has
    358       1.1      mrg  *	had a chance to modify it yet.    if the PG_BUSY page is being
    359       1.1      mrg  *	paged out then it means that someone else has already started
    360       1.1      mrg  *	cleaning the page for us (how nice!).    in this case, if we
    361       1.1      mrg  *	have syncio specified, then after we make our pass through the
    362       1.1      mrg  *	object we need to wait for the other PG_BUSY pages to clear
    363       1.1      mrg  *	off (i.e. we need to do an iosync).   also note that once a
    364       1.1      mrg  *	page is PG_BUSY it must stay in its object until it is un-busyed.
    365       1.1      mrg  *
    366       1.1      mrg  * note on page traversal:
    367       1.1      mrg  *	we can traverse the pages in an object either by going down the
    368       1.1      mrg  *	linked list in "uobj->memq", or we can go over the address range
    369       1.1      mrg  *	by page doing hash table lookups for each address.    depending
    370       1.1      mrg  *	on how many pages are in the object it may be cheaper to do one
    371       1.1      mrg  *	or the other.   we set "by_list" to true if we are using memq.
    372       1.1      mrg  *	if the cost of a hash lookup was equal to the cost of the list
    373       1.1      mrg  *	traversal we could compare the number of pages in the start->stop
    374       1.1      mrg  *	range to the total number of pages in the object.   however, it
    375       1.1      mrg  *	seems that a hash table lookup is more expensive than the linked
    376       1.1      mrg  *	list traversal, so we multiply the number of pages in the
    377       1.1      mrg  *	start->stop range by a penalty which we define below.
    378       1.1      mrg  */
    379       1.1      mrg 
    380       1.8      mrg #define UVN_HASH_PENALTY 4	/* XXX: a guess */
    381       1.1      mrg 
    382       1.8      mrg static boolean_t
    383       1.8      mrg uvn_flush(uobj, start, stop, flags)
    384       1.8      mrg 	struct uvm_object *uobj;
    385  1.26.2.2   bouyer 	voff_t start, stop;
    386       1.8      mrg 	int flags;
    387       1.8      mrg {
    388  1.26.2.3   bouyer 	struct uvm_vnode *uvn = (struct uvm_vnode *)uobj;
    389  1.26.2.3   bouyer 	struct vnode *vp = (struct vnode *)uobj;
    390       1.8      mrg 	struct vm_page *pp, *ppnext, *ptmp;
    391  1.26.2.3   bouyer 	struct vm_page *pps[256], **ppsp;
    392  1.26.2.3   bouyer 	int s;
    393       1.8      mrg 	int npages, result, lcv;
    394  1.26.2.3   bouyer 	boolean_t retval, need_iosync, by_list, needs_clean, all, wasclean;
    395  1.26.2.7   bouyer 	boolean_t async = (flags & PGO_SYNCIO) == 0;
    396  1.26.2.2   bouyer 	voff_t curoff;
    397       1.8      mrg 	u_short pp_version;
    398       1.8      mrg 	UVMHIST_FUNC("uvn_flush"); UVMHIST_CALLED(maphist);
    399  1.26.2.3   bouyer 	UVMHIST_LOG(maphist, "uobj %p start 0x%x stop 0x%x flags 0x%x",
    400  1.26.2.3   bouyer 		    uobj, start, stop, flags);
    401  1.26.2.3   bouyer 	KASSERT(flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE));
    402  1.26.2.3   bouyer 
    403  1.26.2.7   bouyer 	if (uobj->uo_npages == 0) {
    404  1.26.2.7   bouyer 		if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL &&
    405  1.26.2.7   bouyer 		    (vp->v_flag & VONWORKLST)) {
    406  1.26.2.7   bouyer 			vp->v_flag &= ~VONWORKLST;
    407  1.26.2.7   bouyer 			LIST_REMOVE(vp, v_synclist);
    408  1.26.2.7   bouyer 		}
    409  1.26.2.7   bouyer 		return TRUE;
    410  1.26.2.7   bouyer 	}
    411  1.26.2.7   bouyer 
    412  1.26.2.3   bouyer #ifdef DEBUG
    413  1.26.2.3   bouyer 	if (uvn->u_size == VSIZENOTSET) {
    414  1.26.2.3   bouyer 		printf("uvn_flush: size not set vp %p\n", uvn);
    415  1.26.2.3   bouyer 		vprint("uvn_flush VSIZENOTSET", vp);
    416  1.26.2.3   bouyer 		flags |= PGO_ALLPAGES;
    417  1.26.2.3   bouyer 	}
    418  1.26.2.3   bouyer #endif
    419       1.8      mrg 
    420       1.8      mrg 	/*
    421       1.8      mrg 	 * get init vals and determine how we are going to traverse object
    422       1.8      mrg 	 */
    423       1.1      mrg 
    424  1.26.2.6   bouyer 	if (stop == 0) {
    425  1.26.2.6   bouyer 		stop = trunc_page(LLONG_MAX);
    426  1.26.2.6   bouyer 	}
    427  1.26.2.3   bouyer 	curoff = 0;
    428       1.8      mrg 	need_iosync = FALSE;
    429  1.26.2.3   bouyer 	retval = TRUE;
    430  1.26.2.3   bouyer 	wasclean = TRUE;
    431       1.8      mrg 	if (flags & PGO_ALLPAGES) {
    432  1.26.2.2   bouyer 		all = TRUE;
    433  1.26.2.3   bouyer 		by_list = TRUE;
    434       1.8      mrg 	} else {
    435       1.8      mrg 		start = trunc_page(start);
    436       1.8      mrg 		stop = round_page(stop);
    437  1.26.2.2   bouyer 		all = FALSE;
    438       1.8      mrg 		by_list = (uobj->uo_npages <=
    439      1.16      chs 		    ((stop - start) >> PAGE_SHIFT) * UVN_HASH_PENALTY);
    440       1.8      mrg 	}
    441       1.8      mrg 
    442       1.8      mrg 	UVMHIST_LOG(maphist,
    443       1.8      mrg 	    " flush start=0x%x, stop=0x%x, by_list=%d, flags=0x%x",
    444       1.8      mrg 	    start, stop, by_list, flags);
    445       1.8      mrg 
    446       1.8      mrg 	/*
    447       1.8      mrg 	 * PG_CLEANCHK: this bit is used by the pgo_mk_pcluster function as
    448       1.8      mrg 	 * a _hint_ as to how up to date the PG_CLEAN bit is.   if the hint
    449       1.8      mrg 	 * is wrong it will only prevent us from clustering... it won't break
    450       1.8      mrg 	 * anything.   we clear all PG_CLEANCHK bits here, and pgo_mk_pcluster
    451       1.8      mrg 	 * will set them as it syncs PG_CLEAN.   This is only an issue if we
    452       1.8      mrg 	 * are looking at non-inactive pages (because inactive page's PG_CLEAN
    453       1.8      mrg 	 * bit is always up to date since there are no mappings).
    454       1.8      mrg 	 * [borrowed PG_CLEANCHK idea from FreeBSD VM]
    455       1.8      mrg 	 */
    456       1.1      mrg 
    457       1.8      mrg 	if ((flags & PGO_CLEANIT) != 0 &&
    458       1.8      mrg 	    uobj->pgops->pgo_mk_pcluster != NULL) {
    459       1.8      mrg 		if (by_list) {
    460  1.26.2.3   bouyer 			TAILQ_FOREACH(pp, &uobj->memq, listq) {
    461  1.26.2.2   bouyer 				if (!all &&
    462  1.26.2.2   bouyer 				    (pp->offset < start || pp->offset >= stop))
    463       1.8      mrg 					continue;
    464       1.8      mrg 				pp->flags &= ~PG_CLEANCHK;
    465       1.8      mrg 			}
    466       1.8      mrg 
    467       1.8      mrg 		} else {   /* by hash */
    468       1.8      mrg 			for (curoff = start ; curoff < stop;
    469       1.8      mrg 			    curoff += PAGE_SIZE) {
    470       1.8      mrg 				pp = uvm_pagelookup(uobj, curoff);
    471       1.8      mrg 				if (pp)
    472       1.8      mrg 					pp->flags &= ~PG_CLEANCHK;
    473       1.8      mrg 			}
    474       1.8      mrg 		}
    475       1.8      mrg 	}
    476       1.1      mrg 
    477       1.8      mrg 	/*
    478       1.8      mrg 	 * now do it.   note: we must update ppnext in body of loop or we
    479       1.8      mrg 	 * will get stuck.  we need to use ppnext because we may free "pp"
    480       1.8      mrg 	 * before doing the next loop.
    481       1.8      mrg 	 */
    482       1.1      mrg 
    483       1.8      mrg 	if (by_list) {
    484  1.26.2.3   bouyer 		pp = TAILQ_FIRST(&uobj->memq);
    485       1.1      mrg 	} else {
    486       1.8      mrg 		curoff = start;
    487       1.8      mrg 		pp = uvm_pagelookup(uobj, curoff);
    488       1.1      mrg 	}
    489       1.8      mrg 
    490  1.26.2.3   bouyer 	ppnext = NULL;
    491  1.26.2.3   bouyer 	ppsp = NULL;
    492  1.26.2.3   bouyer 	uvm_lock_pageq();
    493       1.8      mrg 
    494       1.8      mrg 	/* locked: both page queues and uobj */
    495       1.8      mrg 	for ( ; (by_list && pp != NULL) ||
    496  1.26.2.3   bouyer 		      (!by_list && curoff < stop) ; pp = ppnext) {
    497       1.8      mrg 		if (by_list) {
    498  1.26.2.2   bouyer 			if (!all &&
    499  1.26.2.2   bouyer 			    (pp->offset < start || pp->offset >= stop)) {
    500  1.26.2.3   bouyer 				ppnext = TAILQ_NEXT(pp, listq);
    501       1.8      mrg 				continue;
    502       1.8      mrg 			}
    503       1.8      mrg 		} else {
    504       1.8      mrg 			curoff += PAGE_SIZE;
    505       1.8      mrg 			if (pp == NULL) {
    506       1.8      mrg 				if (curoff < stop)
    507       1.8      mrg 					ppnext = uvm_pagelookup(uobj, curoff);
    508       1.8      mrg 				continue;
    509       1.8      mrg 			}
    510       1.8      mrg 		}
    511       1.8      mrg 
    512       1.8      mrg 		/*
    513       1.8      mrg 		 * handle case where we do not need to clean page (either
    514       1.8      mrg 		 * because we are not clean or because page is not dirty or
    515       1.8      mrg 		 * is busy):
    516       1.8      mrg 		 *
    517       1.8      mrg 		 * NOTE: we are allowed to deactivate a non-wired active
    518       1.8      mrg 		 * PG_BUSY page, but once a PG_BUSY page is on the inactive
    519       1.8      mrg 		 * queue it must stay put until it is !PG_BUSY (so as not to
    520       1.8      mrg 		 * confuse pagedaemon).
    521       1.8      mrg 		 */
    522       1.8      mrg 
    523       1.8      mrg 		if ((flags & PGO_CLEANIT) == 0 || (pp->flags & PG_BUSY) != 0) {
    524       1.8      mrg 			needs_clean = FALSE;
    525  1.26.2.7   bouyer 			if (!async)
    526       1.8      mrg 				need_iosync = TRUE;
    527       1.8      mrg 		} else {
    528  1.26.2.3   bouyer 
    529       1.8      mrg 			/*
    530       1.8      mrg 			 * freeing: nuke all mappings so we can sync
    531       1.8      mrg 			 * PG_CLEAN bit with no race
    532       1.8      mrg 			 */
    533       1.8      mrg 			if ((pp->flags & PG_CLEAN) != 0 &&
    534       1.8      mrg 			    (flags & PGO_FREE) != 0 &&
    535  1.26.2.6   bouyer 			    /* XXX ACTIVE|INACTIVE test unnecessary? */
    536  1.26.2.6   bouyer 			    (pp->pqflags & (PQ_ACTIVE|PQ_INACTIVE)) != 0)
    537      1.26      chs 				pmap_page_protect(pp, VM_PROT_NONE);
    538       1.8      mrg 			if ((pp->flags & PG_CLEAN) != 0 &&
    539      1.26      chs 			    pmap_is_modified(pp))
    540       1.8      mrg 				pp->flags &= ~(PG_CLEAN);
    541  1.26.2.3   bouyer 			pp->flags |= PG_CLEANCHK;
    542       1.8      mrg 			needs_clean = ((pp->flags & PG_CLEAN) == 0);
    543       1.8      mrg 		}
    544       1.8      mrg 
    545       1.8      mrg 		/*
    546       1.8      mrg 		 * if we don't need a clean... load ppnext and dispose of pp
    547       1.8      mrg 		 */
    548       1.8      mrg 		if (!needs_clean) {
    549       1.8      mrg 			if (by_list)
    550  1.26.2.3   bouyer 				ppnext = TAILQ_NEXT(pp, listq);
    551       1.8      mrg 			else {
    552       1.8      mrg 				if (curoff < stop)
    553       1.8      mrg 					ppnext = uvm_pagelookup(uobj, curoff);
    554       1.8      mrg 			}
    555       1.8      mrg 
    556       1.8      mrg 			if (flags & PGO_DEACTIVATE) {
    557       1.8      mrg 				if ((pp->pqflags & PQ_INACTIVE) == 0 &&
    558  1.26.2.4   bouyer 				    (pp->flags & PG_BUSY) == 0 &&
    559       1.8      mrg 				    pp->wire_count == 0) {
    560  1.26.2.6   bouyer 					pmap_clear_reference(pp);
    561       1.8      mrg 					uvm_pagedeactivate(pp);
    562       1.8      mrg 				}
    563       1.8      mrg 
    564       1.8      mrg 			} else if (flags & PGO_FREE) {
    565       1.8      mrg 				if (pp->flags & PG_BUSY) {
    566       1.8      mrg 					pp->flags |= PG_RELEASED;
    567       1.8      mrg 				} else {
    568      1.26      chs 					pmap_page_protect(pp, VM_PROT_NONE);
    569       1.8      mrg 					uvm_pagefree(pp);
    570       1.8      mrg 				}
    571       1.8      mrg 			}
    572       1.8      mrg 			/* ppnext is valid so we can continue... */
    573       1.8      mrg 			continue;
    574       1.8      mrg 		}
    575       1.8      mrg 
    576       1.8      mrg 		/*
    577       1.8      mrg 		 * pp points to a page in the locked object that we are
    578       1.8      mrg 		 * working on.  if it is !PG_CLEAN,!PG_BUSY and we asked
    579       1.8      mrg 		 * for cleaning (PGO_CLEANIT).  we clean it now.
    580       1.8      mrg 		 *
    581       1.8      mrg 		 * let uvm_pager_put attempted a clustered page out.
    582       1.8      mrg 		 * note: locked: uobj and page queues.
    583       1.8      mrg 		 */
    584       1.8      mrg 
    585  1.26.2.3   bouyer 		wasclean = FALSE;
    586       1.8      mrg 		pp->flags |= PG_BUSY;	/* we 'own' page now */
    587       1.8      mrg 		UVM_PAGE_OWN(pp, "uvn_flush");
    588      1.26      chs 		pmap_page_protect(pp, VM_PROT_READ);
    589       1.8      mrg 		pp_version = pp->version;
    590       1.8      mrg 		ppsp = pps;
    591       1.8      mrg 		npages = sizeof(pps) / sizeof(struct vm_page *);
    592       1.1      mrg 
    593       1.8      mrg 		/* locked: page queues, uobj */
    594       1.8      mrg 		result = uvm_pager_put(uobj, pp, &ppsp, &npages,
    595  1.26.2.3   bouyer 				       flags | PGO_DOACTCLUST, start, stop);
    596       1.8      mrg 		/* unlocked: page queues, uobj */
    597       1.1      mrg 
    598       1.8      mrg 		/*
    599       1.8      mrg 		 * at this point nothing is locked.   if we did an async I/O
    600       1.8      mrg 		 * it is remotely possible for the async i/o to complete and
    601       1.8      mrg 		 * the page "pp" be freed or what not before we get a chance
    602       1.8      mrg 		 * to relock the object.   in order to detect this, we have
    603       1.8      mrg 		 * saved the version number of the page in "pp_version".
    604       1.8      mrg 		 */
    605       1.8      mrg 
    606       1.8      mrg 		/* relock! */
    607       1.8      mrg 		simple_lock(&uobj->vmobjlock);
    608       1.8      mrg 		uvm_lock_pageq();
    609       1.8      mrg 
    610       1.8      mrg 		/*
    611  1.26.2.7   bouyer 		 * the cleaning operation is now done.  finish up.  note that
    612  1.26.2.7   bouyer 		 * on error uvm_pager_put drops the cluster for us.
    613  1.26.2.7   bouyer 		 * on success uvm_pager_put returns the cluster to us in
    614  1.26.2.7   bouyer 		 * ppsp/npages.
    615       1.8      mrg 		 */
    616       1.8      mrg 
    617       1.8      mrg 		/*
    618       1.8      mrg 		 * for pending async i/o if we are not deactivating/freeing
    619       1.8      mrg 		 * we can move on to the next page.
    620       1.8      mrg 		 */
    621       1.8      mrg 
    622  1.26.2.7   bouyer 		if (result == 0 && async &&
    623  1.26.2.3   bouyer 		    (flags & (PGO_DEACTIVATE|PGO_FREE)) == 0) {
    624       1.8      mrg 
    625  1.26.2.3   bouyer 			/*
    626  1.26.2.3   bouyer 			 * no per-page ops: refresh ppnext and continue
    627  1.26.2.3   bouyer 			 */
    628  1.26.2.3   bouyer 			if (by_list) {
    629  1.26.2.3   bouyer 				if (pp->version == pp_version)
    630  1.26.2.3   bouyer 					ppnext = TAILQ_NEXT(pp, listq);
    631  1.26.2.3   bouyer 				else
    632  1.26.2.3   bouyer 					ppnext = TAILQ_FIRST(&uobj->memq);
    633  1.26.2.3   bouyer 			} else {
    634  1.26.2.3   bouyer 				if (curoff < stop)
    635  1.26.2.3   bouyer 					ppnext = uvm_pagelookup(uobj, curoff);
    636       1.8      mrg 			}
    637  1.26.2.3   bouyer 			continue;
    638       1.8      mrg 		}
    639       1.8      mrg 
    640       1.8      mrg 		/*
    641       1.8      mrg 		 * need to look at each page of the I/O operation.  we defer
    642       1.8      mrg 		 * processing "pp" until the last trip through this "for" loop
    643       1.8      mrg 		 * so that we can load "ppnext" for the main loop after we
    644       1.8      mrg 		 * play with the cluster pages [thus the "npages + 1" in the
    645       1.8      mrg 		 * loop below].
    646       1.8      mrg 		 */
    647       1.8      mrg 
    648       1.8      mrg 		for (lcv = 0 ; lcv < npages + 1 ; lcv++) {
    649       1.8      mrg 
    650       1.8      mrg 			/*
    651       1.8      mrg 			 * handle ppnext for outside loop, and saving pp
    652       1.8      mrg 			 * until the end.
    653       1.8      mrg 			 */
    654       1.8      mrg 			if (lcv < npages) {
    655       1.8      mrg 				if (ppsp[lcv] == pp)
    656       1.8      mrg 					continue; /* skip pp until the end */
    657       1.8      mrg 				ptmp = ppsp[lcv];
    658       1.8      mrg 			} else {
    659       1.8      mrg 				ptmp = pp;
    660       1.8      mrg 
    661       1.8      mrg 				/* set up next page for outer loop */
    662       1.8      mrg 				if (by_list) {
    663       1.8      mrg 					if (pp->version == pp_version)
    664  1.26.2.3   bouyer 						ppnext = TAILQ_NEXT(pp, listq);
    665       1.8      mrg 					else
    666  1.26.2.3   bouyer 						ppnext = TAILQ_FIRST(
    667  1.26.2.3   bouyer 						    &uobj->memq);
    668       1.8      mrg 				} else {
    669       1.8      mrg 					if (curoff < stop)
    670  1.26.2.3   bouyer 						ppnext = uvm_pagelookup(uobj,
    671  1.26.2.3   bouyer 						    curoff);
    672       1.8      mrg 				}
    673       1.8      mrg 			}
    674       1.8      mrg 
    675       1.8      mrg 			/*
    676  1.26.2.3   bouyer 			 * verify the page wasn't moved while obj was
    677       1.8      mrg 			 * unlocked
    678       1.8      mrg 			 */
    679  1.26.2.7   bouyer 			if (result == 0 && async && ptmp->uobject != uobj)
    680       1.8      mrg 				continue;
    681       1.8      mrg 
    682       1.8      mrg 			/*
    683       1.8      mrg 			 * unbusy the page if I/O is done.   note that for
    684  1.26.2.7   bouyer 			 * async I/O it is possible that the I/O op
    685       1.8      mrg 			 * finished before we relocked the object (in
    686       1.8      mrg 			 * which case the page is no longer busy).
    687       1.8      mrg 			 */
    688       1.8      mrg 
    689  1.26.2.7   bouyer 			if (result != 0 || !async) {
    690  1.26.2.3   bouyer 				if (ptmp->flags & PG_WANTED) {
    691       1.8      mrg 					/* still holding object lock */
    692      1.25  thorpej 					wakeup(ptmp);
    693  1.26.2.3   bouyer 				}
    694       1.8      mrg 				ptmp->flags &= ~(PG_WANTED|PG_BUSY);
    695       1.8      mrg 				UVM_PAGE_OWN(ptmp, NULL);
    696       1.8      mrg 				if (ptmp->flags & PG_RELEASED) {
    697       1.8      mrg 					uvm_unlock_pageq();
    698  1.26.2.3   bouyer 					if (!uvn_releasepg(ptmp, NULL)) {
    699  1.26.2.3   bouyer 						UVMHIST_LOG(maphist,
    700  1.26.2.3   bouyer 							    "released %p",
    701  1.26.2.3   bouyer 							    ptmp, 0,0,0);
    702       1.8      mrg 						return (TRUE);
    703  1.26.2.3   bouyer 					}
    704  1.26.2.3   bouyer 					uvm_lock_pageq();
    705  1.26.2.3   bouyer 					continue;
    706       1.8      mrg 				} else {
    707  1.26.2.3   bouyer 					if ((flags & PGO_WEAK) == 0 &&
    708  1.26.2.7   bouyer 					    !(result == EIO &&
    709  1.26.2.3   bouyer 					      curproc == uvm.pagedaemon_proc)) {
    710  1.26.2.3   bouyer 						ptmp->flags |=
    711  1.26.2.3   bouyer 							(PG_CLEAN|PG_CLEANCHK);
    712  1.26.2.3   bouyer 						if ((flags & PGO_FREE) == 0) {
    713  1.26.2.3   bouyer 							pmap_clear_modify(ptmp);
    714  1.26.2.3   bouyer 						}
    715  1.26.2.3   bouyer 					}
    716       1.8      mrg 				}
    717       1.8      mrg 			}
    718       1.8      mrg 
    719       1.8      mrg 			/*
    720       1.8      mrg 			 * dispose of page
    721       1.8      mrg 			 */
    722       1.8      mrg 
    723       1.8      mrg 			if (flags & PGO_DEACTIVATE) {
    724       1.8      mrg 				if ((pp->pqflags & PQ_INACTIVE) == 0 &&
    725  1.26.2.4   bouyer 				    (pp->flags & PG_BUSY) == 0 &&
    726       1.8      mrg 				    pp->wire_count == 0) {
    727  1.26.2.6   bouyer 					pmap_clear_reference(ptmp);
    728       1.8      mrg 					uvm_pagedeactivate(ptmp);
    729       1.8      mrg 				}
    730       1.8      mrg 			} else if (flags & PGO_FREE) {
    731  1.26.2.7   bouyer 				if (result == 0 && async) {
    732       1.8      mrg 					if ((ptmp->flags & PG_BUSY) != 0)
    733       1.8      mrg 						/* signal for i/o done */
    734       1.8      mrg 						ptmp->flags |= PG_RELEASED;
    735       1.8      mrg 				} else {
    736  1.26.2.7   bouyer 					if (result != 0) {
    737       1.8      mrg 						printf("uvn_flush: obj=%p, "
    738  1.26.2.3   bouyer 						   "offset=0x%llx.  error %d\n",
    739  1.26.2.2   bouyer 						    pp->uobject,
    740  1.26.2.3   bouyer 						    (long long)pp->offset,
    741  1.26.2.3   bouyer 						    result);
    742       1.8      mrg 						printf("uvn_flush: WARNING: "
    743       1.8      mrg 						    "changes to page may be "
    744       1.8      mrg 						    "lost!\n");
    745       1.8      mrg 						retval = FALSE;
    746       1.8      mrg 					}
    747      1.26      chs 					pmap_page_protect(ptmp, VM_PROT_NONE);
    748       1.8      mrg 					uvm_pagefree(ptmp);
    749       1.8      mrg 				}
    750       1.8      mrg 			}
    751       1.8      mrg 		}		/* end of "lcv" for loop */
    752       1.8      mrg 	}		/* end of "pp" for loop */
    753       1.1      mrg 
    754       1.8      mrg 	uvm_unlock_pageq();
    755  1.26.2.3   bouyer 	if ((flags & PGO_CLEANIT) && all && wasclean &&
    756  1.26.2.3   bouyer 	    LIST_FIRST(&vp->v_dirtyblkhd) == NULL &&
    757  1.26.2.3   bouyer 	    (vp->v_flag & VONWORKLST)) {
    758  1.26.2.3   bouyer 		vp->v_flag &= ~VONWORKLST;
    759  1.26.2.3   bouyer 		LIST_REMOVE(vp, v_synclist);
    760  1.26.2.3   bouyer 	}
    761       1.8      mrg 	if (need_iosync) {
    762       1.8      mrg 		UVMHIST_LOG(maphist,"  <<DOING IOSYNC>>",0,0,0,0);
    763  1.26.2.3   bouyer 
    764  1.26.2.3   bouyer 		/*
    765  1.26.2.3   bouyer 		 * XXX this doesn't use the new two-flag scheme,
    766  1.26.2.3   bouyer 		 * but to use that, all i/o initiators will have to change.
    767  1.26.2.3   bouyer 		 */
    768  1.26.2.3   bouyer 
    769  1.26.2.3   bouyer 		s = splbio();
    770  1.26.2.3   bouyer 		while (vp->v_numoutput != 0) {
    771  1.26.2.3   bouyer 			UVMHIST_LOG(ubchist, "waiting for vp %p num %d",
    772  1.26.2.3   bouyer 				    vp, vp->v_numoutput,0,0);
    773  1.26.2.3   bouyer 
    774  1.26.2.3   bouyer 			vp->v_flag |= VBWAIT;
    775  1.26.2.3   bouyer 			UVM_UNLOCK_AND_WAIT(&vp->v_numoutput,
    776  1.26.2.3   bouyer 					    &uvn->u_obj.vmobjlock,
    777  1.26.2.3   bouyer 					    FALSE, "uvn_flush",0);
    778       1.8      mrg 			simple_lock(&uvn->u_obj.vmobjlock);
    779       1.8      mrg 		}
    780  1.26.2.3   bouyer 		splx(s);
    781       1.1      mrg 	}
    782       1.1      mrg 
    783       1.8      mrg 	/* return, with object locked! */
    784       1.8      mrg 	UVMHIST_LOG(maphist,"<- done (retval=0x%x)",retval,0,0,0);
    785       1.8      mrg 	return(retval);
    786       1.1      mrg }
    787       1.1      mrg 
    788       1.1      mrg /*
    789       1.1      mrg  * uvn_cluster
    790       1.1      mrg  *
    791       1.1      mrg  * we are about to do I/O in an object at offset.   this function is called
    792       1.1      mrg  * to establish a range of offsets around "offset" in which we can cluster
    793       1.1      mrg  * I/O.
    794       1.1      mrg  *
    795       1.1      mrg  * - currently doesn't matter if obj locked or not.
    796       1.1      mrg  */
    797       1.1      mrg 
    798       1.8      mrg static void
    799       1.8      mrg uvn_cluster(uobj, offset, loffset, hoffset)
    800       1.8      mrg 	struct uvm_object *uobj;
    801  1.26.2.2   bouyer 	voff_t offset;
    802  1.26.2.2   bouyer 	voff_t *loffset, *hoffset; /* OUT */
    803       1.1      mrg {
    804  1.26.2.3   bouyer 	struct uvm_vnode *uvn = (struct uvm_vnode *)uobj;
    805       1.1      mrg 
    806  1.26.2.3   bouyer 	*loffset = offset;
    807  1.26.2.7   bouyer 	*hoffset = MIN(offset + MAXBSIZE, round_page(uvn->u_size));
    808       1.1      mrg }
    809       1.1      mrg 
    810       1.1      mrg /*
    811       1.1      mrg  * uvn_put: flush page data to backing store.
    812       1.1      mrg  *
    813       1.1      mrg  * => object must be locked!   we will _unlock_ it before starting I/O.
    814       1.1      mrg  * => flags: PGO_SYNCIO -- use sync. I/O
    815       1.1      mrg  * => note: caller must set PG_CLEAN and pmap_clear_modify (if needed)
    816       1.1      mrg  */
    817       1.1      mrg 
    818       1.8      mrg static int
    819       1.8      mrg uvn_put(uobj, pps, npages, flags)
    820       1.8      mrg 	struct uvm_object *uobj;
    821       1.8      mrg 	struct vm_page **pps;
    822       1.8      mrg 	int npages, flags;
    823       1.1      mrg {
    824  1.26.2.3   bouyer 	struct vnode *vp = (struct vnode *)uobj;
    825  1.26.2.3   bouyer 	int error;
    826       1.1      mrg 
    827  1.26.2.3   bouyer 	error = VOP_PUTPAGES(vp, pps, npages, flags, NULL);
    828  1.26.2.7   bouyer 	return error;
    829       1.1      mrg }
    830       1.1      mrg 
    831       1.1      mrg 
    832       1.1      mrg /*
    833       1.1      mrg  * uvn_get: get pages (synchronously) from backing store
    834       1.1      mrg  *
    835       1.1      mrg  * => prefer map unlocked (not required)
    836       1.1      mrg  * => object must be locked!  we will _unlock_ it before starting any I/O.
    837       1.1      mrg  * => flags: PGO_ALLPAGES: get all of the pages
    838       1.1      mrg  *           PGO_LOCKED: fault data structures are locked
    839       1.1      mrg  * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx]
    840       1.1      mrg  * => NOTE: caller must check for released pages!!
    841       1.1      mrg  */
    842       1.1      mrg 
    843       1.8      mrg static int
    844       1.8      mrg uvn_get(uobj, offset, pps, npagesp, centeridx, access_type, advice, flags)
    845       1.8      mrg 	struct uvm_object *uobj;
    846  1.26.2.2   bouyer 	voff_t offset;
    847       1.8      mrg 	struct vm_page **pps;		/* IN/OUT */
    848       1.8      mrg 	int *npagesp;			/* IN (OUT if PGO_LOCKED) */
    849  1.26.2.3   bouyer 	int centeridx;
    850       1.8      mrg 	vm_prot_t access_type;
    851  1.26.2.3   bouyer 	int advice, flags;
    852       1.8      mrg {
    853  1.26.2.3   bouyer 	struct vnode *vp = (struct vnode *)uobj;
    854  1.26.2.3   bouyer 	int error;
    855  1.26.2.3   bouyer 	UVMHIST_FUNC("uvn_get"); UVMHIST_CALLED(ubchist);
    856  1.26.2.3   bouyer 
    857  1.26.2.3   bouyer 	UVMHIST_LOG(ubchist, "vp %p off 0x%x", vp, (int)offset, 0,0);
    858  1.26.2.3   bouyer 	error = VOP_GETPAGES(vp, offset, pps, npagesp, centeridx,
    859  1.26.2.3   bouyer 			     access_type, advice, flags);
    860  1.26.2.7   bouyer 	return error;
    861       1.1      mrg }
    862       1.1      mrg 
    863  1.26.2.3   bouyer 
    864       1.1      mrg /*
    865  1.26.2.3   bouyer  * uvn_findpages:
    866  1.26.2.3   bouyer  * return the page for the uobj and offset requested, allocating if needed.
    867  1.26.2.3   bouyer  * => uobj must be locked.
    868  1.26.2.3   bouyer  * => returned page will be BUSY.
    869       1.1      mrg  */
    870       1.1      mrg 
    871  1.26.2.3   bouyer void
    872  1.26.2.3   bouyer uvn_findpages(uobj, offset, npagesp, pps, flags)
    873       1.8      mrg 	struct uvm_object *uobj;
    874  1.26.2.2   bouyer 	voff_t offset;
    875  1.26.2.3   bouyer 	int *npagesp;
    876  1.26.2.3   bouyer 	struct vm_page **pps;
    877  1.26.2.3   bouyer 	int flags;
    878       1.8      mrg {
    879  1.26.2.3   bouyer 	int i, rv, npages;
    880       1.1      mrg 
    881  1.26.2.3   bouyer 	rv = 0;
    882  1.26.2.3   bouyer 	npages = *npagesp;
    883  1.26.2.3   bouyer 	for (i = 0; i < npages; i++, offset += PAGE_SIZE) {
    884  1.26.2.3   bouyer 		rv += uvn_findpage(uobj, offset, &pps[i], flags);
    885  1.26.2.3   bouyer 	}
    886  1.26.2.3   bouyer 	*npagesp = rv;
    887       1.1      mrg }
    888       1.1      mrg 
    889       1.8      mrg static int
    890  1.26.2.3   bouyer uvn_findpage(uobj, offset, pgp, flags)
    891  1.26.2.3   bouyer 	struct uvm_object *uobj;
    892  1.26.2.3   bouyer 	voff_t offset;
    893  1.26.2.3   bouyer 	struct vm_page **pgp;
    894  1.26.2.3   bouyer 	int flags;
    895       1.8      mrg {
    896  1.26.2.3   bouyer 	struct vm_page *pg;
    897  1.26.2.3   bouyer 	UVMHIST_FUNC("uvn_findpage"); UVMHIST_CALLED(ubchist);
    898  1.26.2.3   bouyer 	UVMHIST_LOG(ubchist, "vp %p off 0x%lx", uobj, offset,0,0);
    899       1.8      mrg 
    900  1.26.2.3   bouyer 	if (*pgp != NULL) {
    901  1.26.2.3   bouyer 		UVMHIST_LOG(ubchist, "dontcare", 0,0,0,0);
    902  1.26.2.3   bouyer 		return 0;
    903  1.26.2.3   bouyer 	}
    904  1.26.2.3   bouyer 	for (;;) {
    905  1.26.2.3   bouyer 		/* look for an existing page */
    906  1.26.2.3   bouyer 		pg = uvm_pagelookup(uobj, offset);
    907  1.26.2.3   bouyer 
    908  1.26.2.3   bouyer 		/* nope?   allocate one now */
    909  1.26.2.3   bouyer 		if (pg == NULL) {
    910  1.26.2.3   bouyer 			if (flags & UFP_NOALLOC) {
    911  1.26.2.3   bouyer 				UVMHIST_LOG(ubchist, "noalloc", 0,0,0,0);
    912  1.26.2.3   bouyer 				return 0;
    913  1.26.2.3   bouyer 			}
    914  1.26.2.7   bouyer 			pg = uvm_pagealloc(uobj, offset, NULL, 0);
    915  1.26.2.3   bouyer 			if (pg == NULL) {
    916  1.26.2.3   bouyer 				if (flags & UFP_NOWAIT) {
    917  1.26.2.3   bouyer 					UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
    918  1.26.2.3   bouyer 					return 0;
    919  1.26.2.3   bouyer 				}
    920  1.26.2.3   bouyer 				simple_unlock(&uobj->vmobjlock);
    921  1.26.2.3   bouyer 				uvm_wait("uvn_fp1");
    922  1.26.2.3   bouyer 				simple_lock(&uobj->vmobjlock);
    923  1.26.2.3   bouyer 				continue;
    924  1.26.2.3   bouyer 			}
    925  1.26.2.7   bouyer 			if (UVM_OBJ_IS_VTEXT(uobj)) {
    926  1.26.2.7   bouyer 				uvmexp.vtextpages++;
    927  1.26.2.7   bouyer 			} else {
    928  1.26.2.7   bouyer 				uvmexp.vnodepages++;
    929  1.26.2.7   bouyer 			}
    930  1.26.2.3   bouyer 			UVMHIST_LOG(ubchist, "alloced",0,0,0,0);
    931  1.26.2.3   bouyer 			break;
    932  1.26.2.3   bouyer 		} else if (flags & UFP_NOCACHE) {
    933  1.26.2.3   bouyer 			UVMHIST_LOG(ubchist, "nocache",0,0,0,0);
    934  1.26.2.3   bouyer 			return 0;
    935       1.8      mrg 		}
    936       1.8      mrg 
    937  1.26.2.3   bouyer 		/* page is there, see if we need to wait on it */
    938  1.26.2.3   bouyer 		if ((pg->flags & (PG_BUSY|PG_RELEASED)) != 0) {
    939  1.26.2.3   bouyer 			if (flags & UFP_NOWAIT) {
    940  1.26.2.3   bouyer 				UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
    941  1.26.2.3   bouyer 				return 0;
    942  1.26.2.3   bouyer 			}
    943  1.26.2.3   bouyer 			pg->flags |= PG_WANTED;
    944  1.26.2.3   bouyer 			UVM_UNLOCK_AND_WAIT(pg, &uobj->vmobjlock, 0,
    945  1.26.2.3   bouyer 					    "uvn_fp2", 0);
    946  1.26.2.3   bouyer 			simple_lock(&uobj->vmobjlock);
    947  1.26.2.3   bouyer 			continue;
    948       1.8      mrg 		}
    949  1.26.2.3   bouyer 
    950  1.26.2.3   bouyer 		/* skip PG_RDONLY pages if requested */
    951  1.26.2.3   bouyer 		if ((flags & UFP_NORDONLY) && (pg->flags & PG_RDONLY)) {
    952  1.26.2.3   bouyer 			UVMHIST_LOG(ubchist, "nordonly",0,0,0,0);
    953  1.26.2.3   bouyer 			return 0;
    954       1.8      mrg 		}
    955       1.1      mrg 
    956  1.26.2.3   bouyer 		/* mark the page BUSY and we're done. */
    957  1.26.2.3   bouyer 		pg->flags |= PG_BUSY;
    958  1.26.2.3   bouyer 		UVM_PAGE_OWN(pg, "uvn_findpage");
    959  1.26.2.3   bouyer 		UVMHIST_LOG(ubchist, "found",0,0,0,0);
    960  1.26.2.3   bouyer 		break;
    961  1.26.2.3   bouyer 	}
    962  1.26.2.3   bouyer 	*pgp = pg;
    963  1.26.2.3   bouyer 	return 1;
    964       1.1      mrg }
    965       1.1      mrg 
    966       1.1      mrg /*
    967       1.1      mrg  * uvm_vnp_setsize: grow or shrink a vnode uvn
    968       1.1      mrg  *
    969       1.1      mrg  * grow   => just update size value
    970       1.1      mrg  * shrink => toss un-needed pages
    971       1.1      mrg  *
    972       1.1      mrg  * => we assume that the caller has a reference of some sort to the
    973       1.1      mrg  *	vnode in question so that it will not be yanked out from under
    974       1.1      mrg  *	us.
    975       1.1      mrg  *
    976       1.1      mrg  * called from:
    977       1.1      mrg  *  => truncate fns (ext2fs_truncate, ffs_truncate, detrunc[msdos])
    978       1.1      mrg  *  => "write" fns (ext2fs_write, WRITE [ufs/ufs], msdosfs_write, nfs_write)
    979       1.1      mrg  *  => ffs_balloc [XXX: why? doesn't WRITE handle?]
    980       1.1      mrg  *  => NFS: nfs_loadattrcache, nfs_getattrcache, nfs_setattr
    981       1.1      mrg  *  => union fs: union_newsize
    982       1.1      mrg  */
    983       1.1      mrg 
    984       1.8      mrg void
    985       1.8      mrg uvm_vnp_setsize(vp, newsize)
    986       1.8      mrg 	struct vnode *vp;
    987  1.26.2.2   bouyer 	voff_t newsize;
    988       1.8      mrg {
    989       1.8      mrg 	struct uvm_vnode *uvn = &vp->v_uvm;
    990  1.26.2.7   bouyer 	voff_t pgend = round_page(newsize);
    991  1.26.2.3   bouyer 	UVMHIST_FUNC("uvm_vnp_setsize"); UVMHIST_CALLED(ubchist);
    992       1.1      mrg 
    993       1.8      mrg 	simple_lock(&uvn->u_obj.vmobjlock);
    994       1.8      mrg 
    995  1.26.2.3   bouyer 	UVMHIST_LOG(ubchist, "old 0x%x new 0x%x", uvn->u_size, newsize, 0,0);
    996       1.1      mrg 
    997       1.8      mrg 	/*
    998  1.26.2.3   bouyer 	 * now check if the size has changed: if we shrink we had better
    999  1.26.2.3   bouyer 	 * toss some pages...
   1000       1.8      mrg 	 */
   1001  1.26.2.3   bouyer 
   1002  1.26.2.7   bouyer 	if (uvn->u_size > pgend && uvn->u_size != VSIZENOTSET) {
   1003  1.26.2.7   bouyer 		(void) uvn_flush(&uvn->u_obj, pgend, 0, PGO_FREE);
   1004  1.26.2.3   bouyer 	}
   1005  1.26.2.3   bouyer 	uvn->u_size = newsize;
   1006  1.26.2.3   bouyer 	simple_unlock(&uvn->u_obj.vmobjlock);
   1007       1.1      mrg }
   1008       1.1      mrg 
   1009       1.1      mrg /*
   1010  1.26.2.3   bouyer  * uvm_vnp_zerorange:  set a range of bytes in a file to zero.
   1011       1.1      mrg  */
   1012       1.1      mrg 
   1013       1.8      mrg void
   1014  1.26.2.3   bouyer uvm_vnp_zerorange(vp, off, len)
   1015       1.8      mrg 	struct vnode *vp;
   1016  1.26.2.3   bouyer 	off_t off;
   1017  1.26.2.3   bouyer 	size_t len;
   1018  1.26.2.3   bouyer {
   1019  1.26.2.3   bouyer         void *win;
   1020       1.8      mrg 
   1021  1.26.2.3   bouyer         /*
   1022  1.26.2.3   bouyer          * XXXUBC invent kzero() and use it
   1023  1.26.2.3   bouyer          */
   1024  1.26.2.3   bouyer 
   1025  1.26.2.3   bouyer         while (len) {
   1026  1.26.2.3   bouyer                 vsize_t bytelen = len;
   1027  1.26.2.3   bouyer 
   1028  1.26.2.3   bouyer                 win = ubc_alloc(&vp->v_uvm.u_obj, off, &bytelen, UBC_WRITE);
   1029  1.26.2.3   bouyer                 memset(win, 0, bytelen);
   1030  1.26.2.3   bouyer                 ubc_release(win, 0);
   1031  1.26.2.3   bouyer 
   1032  1.26.2.3   bouyer                 off += bytelen;
   1033  1.26.2.3   bouyer                 len -= bytelen;
   1034  1.26.2.3   bouyer         }
   1035       1.1      mrg }
   1036