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