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