Home | History | Annotate | Line # | Download | only in uvm
uvm_vnode.c revision 1.26.2.2
      1  1.26.2.2    bouyer /*	$NetBSD: uvm_vnode.c,v 1.26.2.2 2000/11/20 18:12:08 bouyer Exp $	*/
      2       1.1       mrg 
      3       1.1       mrg /*
      4       1.1       mrg  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5       1.1       mrg  * Copyright (c) 1991, 1993
      6       1.1       mrg  *      The Regents of the University of California.
      7       1.1       mrg  * Copyright (c) 1990 University of Utah.
      8       1.1       mrg  *
      9       1.1       mrg  * All rights reserved.
     10       1.1       mrg  *
     11       1.1       mrg  * This code is derived from software contributed to Berkeley by
     12       1.1       mrg  * the Systems Programming Group of the University of Utah Computer
     13       1.1       mrg  * Science Department.
     14       1.1       mrg  *
     15       1.1       mrg  * Redistribution and use in source and binary forms, with or without
     16       1.1       mrg  * modification, are permitted provided that the following conditions
     17       1.1       mrg  * are met:
     18       1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     19       1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     20       1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     21       1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     22       1.1       mrg  *    documentation and/or other materials provided with the distribution.
     23       1.1       mrg  * 3. All advertising materials mentioning features or use of this software
     24       1.1       mrg  *    must display the following acknowledgement:
     25       1.1       mrg  *      This product includes software developed by Charles D. Cranor,
     26       1.1       mrg  *	Washington University, the University of California, Berkeley and
     27       1.1       mrg  *	its contributors.
     28       1.1       mrg  * 4. Neither the name of the University nor the names of its contributors
     29       1.1       mrg  *    may be used to endorse or promote products derived from this software
     30       1.1       mrg  *    without specific prior written permission.
     31       1.1       mrg  *
     32       1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33       1.1       mrg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34       1.1       mrg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35       1.1       mrg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36       1.1       mrg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37       1.1       mrg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38       1.1       mrg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39       1.1       mrg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40       1.1       mrg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41       1.1       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42       1.1       mrg  * SUCH DAMAGE.
     43       1.1       mrg  *
     44       1.1       mrg  *      @(#)vnode_pager.c       8.8 (Berkeley) 2/13/94
     45       1.3       mrg  * from: Id: uvm_vnode.c,v 1.1.2.26 1998/02/02 20:38:07 chuck Exp
     46       1.1       mrg  */
     47       1.1       mrg 
     48       1.6   thorpej #include "fs_nfs.h"
     49       1.4       mrg #include "opt_uvmhist.h"
     50       1.4       mrg 
     51       1.1       mrg /*
     52       1.1       mrg  * uvm_vnode.c: the vnode pager.
     53       1.1       mrg  */
     54       1.1       mrg 
     55       1.1       mrg #include <sys/param.h>
     56       1.1       mrg #include <sys/systm.h>
     57       1.1       mrg #include <sys/proc.h>
     58       1.1       mrg #include <sys/malloc.h>
     59       1.1       mrg #include <sys/vnode.h>
     60      1.13   thorpej #include <sys/disklabel.h>
     61      1.13   thorpej #include <sys/ioctl.h>
     62      1.13   thorpej #include <sys/fcntl.h>
     63      1.13   thorpej #include <sys/conf.h>
     64      1.13   thorpej 
     65      1.13   thorpej #include <miscfs/specfs/specdev.h>
     66       1.1       mrg 
     67       1.1       mrg #include <uvm/uvm.h>
     68       1.1       mrg #include <uvm/uvm_vnode.h>
     69       1.1       mrg 
     70       1.1       mrg /*
     71       1.1       mrg  * private global data structure
     72       1.1       mrg  *
     73       1.1       mrg  * we keep a list of writeable active vnode-backed VM objects for sync op.
     74       1.1       mrg  * we keep a simpleq of vnodes that are currently being sync'd.
     75       1.1       mrg  */
     76       1.1       mrg 
     77       1.1       mrg LIST_HEAD(uvn_list_struct, uvm_vnode);
     78       1.1       mrg static struct uvn_list_struct uvn_wlist;	/* writeable uvns */
     79       1.1       mrg static simple_lock_data_t uvn_wl_lock;		/* locks uvn_wlist */
     80       1.1       mrg 
     81       1.1       mrg SIMPLEQ_HEAD(uvn_sq_struct, uvm_vnode);
     82       1.1       mrg static struct uvn_sq_struct uvn_sync_q;		/* sync'ing uvns */
     83       1.1       mrg lock_data_t uvn_sync_lock;			/* locks sync operation */
     84       1.1       mrg 
     85       1.1       mrg /*
     86       1.1       mrg  * functions
     87       1.1       mrg  */
     88       1.1       mrg 
     89  1.26.2.2    bouyer static int		   uvn_asyncget __P((struct uvm_object *, voff_t,
     90       1.1       mrg 					    int));
     91  1.26.2.2    bouyer static void		   uvn_cluster __P((struct uvm_object *, voff_t,
     92  1.26.2.2    bouyer 					   voff_t *, voff_t *));
     93       1.1       mrg static void                uvn_detach __P((struct uvm_object *));
     94  1.26.2.2    bouyer static boolean_t           uvn_flush __P((struct uvm_object *, voff_t,
     95  1.26.2.2    bouyer 					 voff_t, int));
     96  1.26.2.2    bouyer static int                 uvn_get __P((struct uvm_object *, voff_t,
     97       1.1       mrg 					vm_page_t *, int *, int,
     98       1.1       mrg 					vm_prot_t, int, int));
     99       1.1       mrg static void		   uvn_init __P((void));
    100       1.1       mrg static int		   uvn_io __P((struct uvm_vnode *, vm_page_t *,
    101       1.1       mrg 				      int, int, int));
    102       1.1       mrg static int		   uvn_put __P((struct uvm_object *, vm_page_t *,
    103       1.1       mrg 					int, boolean_t));
    104       1.1       mrg static void                uvn_reference __P((struct uvm_object *));
    105       1.1       mrg static boolean_t	   uvn_releasepg __P((struct vm_page *,
    106       1.1       mrg 					      struct vm_page **));
    107       1.1       mrg 
    108       1.1       mrg /*
    109       1.1       mrg  * master pager structure
    110       1.1       mrg  */
    111       1.1       mrg 
    112       1.1       mrg struct uvm_pagerops uvm_vnodeops = {
    113       1.8       mrg 	uvn_init,
    114       1.8       mrg 	uvn_reference,
    115       1.8       mrg 	uvn_detach,
    116       1.8       mrg 	NULL,			/* no specialized fault routine required */
    117       1.8       mrg 	uvn_flush,
    118       1.8       mrg 	uvn_get,
    119       1.8       mrg 	uvn_asyncget,
    120       1.8       mrg 	uvn_put,
    121       1.8       mrg 	uvn_cluster,
    122       1.8       mrg 	uvm_mk_pcluster, /* use generic version of this: see uvm_pager.c */
    123       1.8       mrg 	NULL,		 /* AIO-DONE function (not until we have asyncio) */
    124       1.8       mrg 	uvn_releasepg,
    125       1.1       mrg };
    126       1.1       mrg 
    127       1.1       mrg /*
    128       1.1       mrg  * the ops!
    129       1.1       mrg  */
    130       1.1       mrg 
    131       1.1       mrg /*
    132       1.1       mrg  * uvn_init
    133       1.1       mrg  *
    134       1.1       mrg  * init pager private data structures.
    135       1.1       mrg  */
    136       1.1       mrg 
    137       1.8       mrg static void
    138       1.8       mrg uvn_init()
    139       1.8       mrg {
    140       1.1       mrg 
    141       1.8       mrg 	LIST_INIT(&uvn_wlist);
    142       1.8       mrg 	simple_lock_init(&uvn_wl_lock);
    143       1.8       mrg 	/* note: uvn_sync_q init'd in uvm_vnp_sync() */
    144       1.8       mrg 	lockinit(&uvn_sync_lock, PVM, "uvnsync", 0, 0);
    145       1.1       mrg }
    146       1.1       mrg 
    147       1.1       mrg /*
    148       1.1       mrg  * uvn_attach
    149       1.1       mrg  *
    150       1.1       mrg  * attach a vnode structure to a VM object.  if the vnode is already
    151       1.1       mrg  * attached, then just bump the reference count by one and return the
    152       1.1       mrg  * VM object.   if not already attached, attach and return the new VM obj.
    153       1.1       mrg  * the "accessprot" tells the max access the attaching thread wants to
    154       1.1       mrg  * our pages.
    155       1.1       mrg  *
    156       1.1       mrg  * => caller must _not_ already be holding the lock on the uvm_object.
    157       1.1       mrg  * => in fact, nothing should be locked so that we can sleep here.
    158       1.1       mrg  * => note that uvm_object is first thing in vnode structure, so their
    159       1.1       mrg  *    pointers are equiv.
    160       1.1       mrg  */
    161       1.1       mrg 
    162       1.8       mrg struct uvm_object *
    163       1.8       mrg uvn_attach(arg, accessprot)
    164       1.8       mrg 	void *arg;
    165       1.8       mrg 	vm_prot_t accessprot;
    166       1.8       mrg {
    167       1.8       mrg 	struct vnode *vp = arg;
    168       1.8       mrg 	struct uvm_vnode *uvn = &vp->v_uvm;
    169       1.8       mrg 	struct vattr vattr;
    170       1.8       mrg 	int oldflags, result;
    171      1.13   thorpej 	struct partinfo pi;
    172       1.8       mrg 	u_quad_t used_vnode_size;
    173       1.8       mrg 	UVMHIST_FUNC("uvn_attach"); UVMHIST_CALLED(maphist);
    174       1.8       mrg 
    175       1.8       mrg 	UVMHIST_LOG(maphist, "(vn=0x%x)", arg,0,0,0);
    176       1.8       mrg 
    177      1.13   thorpej 	used_vnode_size = (u_quad_t)0;	/* XXX gcc -Wuninitialized */
    178      1.13   thorpej 
    179       1.8       mrg 	/*
    180       1.8       mrg 	 * first get a lock on the uvn.
    181       1.8       mrg 	 */
    182       1.8       mrg 	simple_lock(&uvn->u_obj.vmobjlock);
    183       1.8       mrg 	while (uvn->u_flags & UVM_VNODE_BLOCKED) {
    184  1.26.2.2    bouyer 		printf("uvn_attach: blocked at 0x%p flags 0x%x\n",
    185  1.26.2.2    bouyer 		    uvn, uvn->u_flags);
    186       1.8       mrg 		uvn->u_flags |= UVM_VNODE_WANTED;
    187       1.8       mrg 		UVMHIST_LOG(maphist, "  SLEEPING on blocked vn",0,0,0,0);
    188       1.8       mrg 		UVM_UNLOCK_AND_WAIT(uvn, &uvn->u_obj.vmobjlock, FALSE,
    189       1.8       mrg 		    "uvn_attach", 0);
    190       1.8       mrg 		simple_lock(&uvn->u_obj.vmobjlock);
    191       1.8       mrg 		UVMHIST_LOG(maphist,"  WOKE UP",0,0,0,0);
    192       1.8       mrg 	}
    193       1.1       mrg 
    194       1.8       mrg 	/*
    195      1.18    bouyer 	 * if we're mapping a BLK device, make sure it is a disk.
    196      1.13   thorpej 	 */
    197      1.13   thorpej 	if (vp->v_type == VBLK && bdevsw[major(vp->v_rdev)].d_type != D_DISK) {
    198      1.13   thorpej 		simple_unlock(&uvn->u_obj.vmobjlock); /* drop lock */
    199      1.13   thorpej 		UVMHIST_LOG(maphist,"<- done (VBLK not D_DISK!)", 0,0,0,0);
    200      1.13   thorpej 		return(NULL);
    201      1.13   thorpej 	}
    202      1.13   thorpej 
    203      1.13   thorpej 	/*
    204       1.8       mrg 	 * now we have lock and uvn must not be in a blocked state.
    205       1.8       mrg 	 * first check to see if it is already active, in which case
    206       1.8       mrg 	 * we can bump the reference count, check to see if we need to
    207       1.8       mrg 	 * add it to the writeable list, and then return.
    208       1.8       mrg 	 */
    209       1.8       mrg 	if (uvn->u_flags & UVM_VNODE_VALID) {	/* already active? */
    210       1.1       mrg 
    211       1.8       mrg 		/* regain VREF if we were persisting */
    212       1.8       mrg 		if (uvn->u_obj.uo_refs == 0) {
    213       1.8       mrg 			VREF(vp);
    214       1.8       mrg 			UVMHIST_LOG(maphist," VREF (reclaim persisting vnode)",
    215       1.8       mrg 			    0,0,0,0);
    216       1.8       mrg 		}
    217       1.8       mrg 		uvn->u_obj.uo_refs++;		/* bump uvn ref! */
    218       1.8       mrg 
    219       1.8       mrg 		/* check for new writeable uvn */
    220       1.8       mrg 		if ((accessprot & VM_PROT_WRITE) != 0 &&
    221       1.8       mrg 		    (uvn->u_flags & UVM_VNODE_WRITEABLE) == 0) {
    222       1.8       mrg 			simple_lock(&uvn_wl_lock);
    223       1.8       mrg 			LIST_INSERT_HEAD(&uvn_wlist, uvn, u_wlist);
    224       1.8       mrg 			simple_unlock(&uvn_wl_lock);
    225       1.8       mrg 			/* we are now on wlist! */
    226       1.8       mrg 			uvn->u_flags |= UVM_VNODE_WRITEABLE;
    227       1.8       mrg 		}
    228       1.8       mrg 
    229       1.8       mrg 		/* unlock and return */
    230       1.8       mrg 		simple_unlock(&uvn->u_obj.vmobjlock);
    231       1.8       mrg 		UVMHIST_LOG(maphist,"<- done, refcnt=%d", uvn->u_obj.uo_refs,
    232       1.8       mrg 		    0, 0, 0);
    233       1.8       mrg 		return (&uvn->u_obj);
    234       1.8       mrg 	}
    235       1.8       mrg 
    236       1.8       mrg 	/*
    237       1.8       mrg 	 * need to call VOP_GETATTR() to get the attributes, but that could
    238       1.8       mrg 	 * block (due to I/O), so we want to unlock the object before calling.
    239       1.8       mrg 	 * however, we want to keep anyone else from playing with the object
    240       1.8       mrg 	 * while it is unlocked.   to do this we set UVM_VNODE_ALOCK which
    241       1.8       mrg 	 * prevents anyone from attaching to the vnode until we are done with
    242       1.8       mrg 	 * it.
    243       1.8       mrg 	 */
    244       1.8       mrg 	uvn->u_flags = UVM_VNODE_ALOCK;
    245       1.8       mrg 	simple_unlock(&uvn->u_obj.vmobjlock); /* drop lock in case we sleep */
    246       1.8       mrg 		/* XXX: curproc? */
    247       1.8       mrg 
    248      1.13   thorpej 	if (vp->v_type == VBLK) {
    249      1.13   thorpej 		/*
    250      1.13   thorpej 		 * We could implement this as a specfs getattr call, but:
    251      1.13   thorpej 		 *
    252      1.13   thorpej 		 *	(1) VOP_GETATTR() would get the file system
    253      1.13   thorpej 		 *	    vnode operation, not the specfs operation.
    254      1.13   thorpej 		 *
    255      1.13   thorpej 		 *	(2) All we want is the size, anyhow.
    256      1.13   thorpej 		 */
    257      1.13   thorpej 		result = (*bdevsw[major(vp->v_rdev)].d_ioctl)(vp->v_rdev,
    258      1.13   thorpej 		    DIOCGPART, (caddr_t)&pi, FREAD, curproc);
    259      1.13   thorpej 		if (result == 0) {
    260      1.13   thorpej 			/* XXX should remember blocksize */
    261      1.13   thorpej 			used_vnode_size = (u_quad_t)pi.disklab->d_secsize *
    262      1.13   thorpej 			    (u_quad_t)pi.part->p_size;
    263      1.13   thorpej 		}
    264      1.13   thorpej 	} else {
    265      1.13   thorpej 		result = VOP_GETATTR(vp, &vattr, curproc->p_ucred, curproc);
    266      1.13   thorpej 		if (result == 0)
    267      1.13   thorpej 			used_vnode_size = vattr.va_size;
    268       1.8       mrg 	}
    269       1.1       mrg 
    270       1.8       mrg 	/* relock object */
    271       1.8       mrg 	simple_lock(&uvn->u_obj.vmobjlock);
    272       1.1       mrg 
    273       1.8       mrg 	if (result != 0) {
    274       1.8       mrg 		if (uvn->u_flags & UVM_VNODE_WANTED)
    275       1.8       mrg 			wakeup(uvn);
    276       1.8       mrg 		uvn->u_flags = 0;
    277       1.8       mrg 		simple_unlock(&uvn->u_obj.vmobjlock); /* drop lock */
    278       1.8       mrg 		UVMHIST_LOG(maphist,"<- done (VOP_GETATTR FAILED!)", 0,0,0,0);
    279       1.8       mrg 		return(NULL);
    280       1.8       mrg 	}
    281      1.13   thorpej 
    282      1.13   thorpej 	/*
    283      1.15       eeh 	 * make sure that the newsize fits within a vaddr_t
    284      1.13   thorpej 	 * XXX: need to revise addressing data types
    285      1.13   thorpej 	 */
    286      1.18    bouyer #ifdef DEBUG
    287      1.18    bouyer 	if (vp->v_type == VBLK)
    288      1.19       chs 		printf("used_vnode_size = %qu\n", (long long)used_vnode_size);
    289      1.18    bouyer #endif
    290      1.13   thorpej 
    291       1.8       mrg 	/*
    292       1.8       mrg 	 * now set up the uvn.
    293       1.8       mrg 	 */
    294       1.8       mrg 	uvn->u_obj.pgops = &uvm_vnodeops;
    295       1.8       mrg 	TAILQ_INIT(&uvn->u_obj.memq);
    296       1.8       mrg 	uvn->u_obj.uo_npages = 0;
    297       1.8       mrg 	uvn->u_obj.uo_refs = 1;			/* just us... */
    298       1.8       mrg 	oldflags = uvn->u_flags;
    299       1.8       mrg 	uvn->u_flags = UVM_VNODE_VALID|UVM_VNODE_CANPERSIST;
    300       1.8       mrg 	uvn->u_nio = 0;
    301       1.8       mrg 	uvn->u_size = used_vnode_size;
    302       1.8       mrg 
    303       1.8       mrg 	/* if write access, we need to add it to the wlist */
    304       1.8       mrg 	if (accessprot & VM_PROT_WRITE) {
    305       1.8       mrg 		simple_lock(&uvn_wl_lock);
    306       1.8       mrg 		LIST_INSERT_HEAD(&uvn_wlist, uvn, u_wlist);
    307       1.8       mrg 		simple_unlock(&uvn_wl_lock);
    308       1.8       mrg 		uvn->u_flags |= UVM_VNODE_WRITEABLE;	/* we are on wlist! */
    309       1.8       mrg 	}
    310       1.8       mrg 
    311       1.8       mrg 	/*
    312       1.8       mrg 	 * add a reference to the vnode.   this reference will stay as long
    313       1.8       mrg 	 * as there is a valid mapping of the vnode.   dropped when the
    314       1.8       mrg 	 * reference count goes to zero [and we either free or persist].
    315       1.8       mrg 	 */
    316       1.8       mrg 	VREF(vp);
    317       1.8       mrg 	simple_unlock(&uvn->u_obj.vmobjlock);
    318       1.8       mrg 	if (oldflags & UVM_VNODE_WANTED)
    319       1.8       mrg 		wakeup(uvn);
    320       1.1       mrg 
    321       1.8       mrg 	UVMHIST_LOG(maphist,"<- done/VREF, ret 0x%x", &uvn->u_obj,0,0,0);
    322       1.8       mrg 	return(&uvn->u_obj);
    323       1.1       mrg }
    324       1.1       mrg 
    325       1.1       mrg 
    326       1.1       mrg /*
    327       1.1       mrg  * uvn_reference
    328       1.1       mrg  *
    329       1.1       mrg  * duplicate a reference to a VM object.  Note that the reference
    330       1.1       mrg  * count must already be at least one (the passed in reference) so
    331       1.1       mrg  * there is no chance of the uvn being killed or locked out here.
    332       1.1       mrg  *
    333       1.1       mrg  * => caller must call with object unlocked.
    334       1.1       mrg  * => caller must be using the same accessprot as was used at attach time
    335       1.1       mrg  */
    336       1.1       mrg 
    337       1.1       mrg 
    338       1.8       mrg static void
    339       1.8       mrg uvn_reference(uobj)
    340       1.8       mrg 	struct uvm_object *uobj;
    341       1.1       mrg {
    342  1.26.2.1   thorpej #ifdef DEBUG
    343       1.8       mrg 	struct uvm_vnode *uvn = (struct uvm_vnode *) uobj;
    344       1.1       mrg #endif
    345       1.8       mrg 	UVMHIST_FUNC("uvn_reference"); UVMHIST_CALLED(maphist);
    346       1.1       mrg 
    347       1.8       mrg 	simple_lock(&uobj->vmobjlock);
    348  1.26.2.1   thorpej #ifdef DEBUG
    349       1.8       mrg 	if ((uvn->u_flags & UVM_VNODE_VALID) == 0) {
    350       1.8       mrg 		printf("uvn_reference: ref=%d, flags=0x%x\n", uvn->u_flags,
    351       1.8       mrg 		    uobj->uo_refs);
    352       1.8       mrg 		panic("uvn_reference: invalid state");
    353       1.8       mrg 	}
    354       1.1       mrg #endif
    355       1.8       mrg 	uobj->uo_refs++;
    356       1.8       mrg 	UVMHIST_LOG(maphist, "<- done (uobj=0x%x, ref = %d)",
    357       1.1       mrg 	uobj, uobj->uo_refs,0,0);
    358       1.8       mrg 	simple_unlock(&uobj->vmobjlock);
    359       1.1       mrg }
    360       1.1       mrg 
    361       1.1       mrg /*
    362       1.1       mrg  * uvn_detach
    363       1.1       mrg  *
    364       1.1       mrg  * remove a reference to a VM object.
    365       1.1       mrg  *
    366       1.1       mrg  * => caller must call with object unlocked and map locked.
    367       1.1       mrg  * => this starts the detach process, but doesn't have to finish it
    368       1.1       mrg  *    (async i/o could still be pending).
    369       1.1       mrg  */
    370       1.8       mrg static void
    371       1.8       mrg uvn_detach(uobj)
    372       1.8       mrg 	struct uvm_object *uobj;
    373       1.8       mrg {
    374       1.8       mrg 	struct uvm_vnode *uvn;
    375       1.8       mrg 	struct vnode *vp;
    376       1.8       mrg 	int oldflags;
    377       1.8       mrg 	UVMHIST_FUNC("uvn_detach"); UVMHIST_CALLED(maphist);
    378       1.8       mrg 
    379       1.8       mrg 	simple_lock(&uobj->vmobjlock);
    380       1.8       mrg 
    381       1.8       mrg 	UVMHIST_LOG(maphist,"  (uobj=0x%x)  ref=%d", uobj,uobj->uo_refs,0,0);
    382       1.8       mrg 	uobj->uo_refs--;			/* drop ref! */
    383       1.8       mrg 	if (uobj->uo_refs) {			/* still more refs */
    384       1.8       mrg 		simple_unlock(&uobj->vmobjlock);
    385       1.8       mrg 		UVMHIST_LOG(maphist, "<- done (rc>0)", 0,0,0,0);
    386       1.8       mrg 		return;
    387       1.8       mrg 	}
    388       1.1       mrg 
    389       1.8       mrg 	/*
    390       1.8       mrg 	 * get other pointers ...
    391       1.8       mrg 	 */
    392       1.8       mrg 
    393       1.8       mrg 	uvn = (struct uvm_vnode *) uobj;
    394       1.8       mrg 	vp = (struct vnode *) uobj;
    395       1.8       mrg 
    396       1.8       mrg 	/*
    397       1.8       mrg 	 * clear VTEXT flag now that there are no mappings left (VTEXT is used
    398       1.8       mrg 	 * to keep an active text file from being overwritten).
    399       1.8       mrg 	 */
    400       1.8       mrg 	vp->v_flag &= ~VTEXT;
    401       1.8       mrg 
    402       1.8       mrg 	/*
    403       1.8       mrg 	 * we just dropped the last reference to the uvn.   see if we can
    404       1.8       mrg 	 * let it "stick around".
    405       1.8       mrg 	 */
    406       1.8       mrg 
    407       1.8       mrg 	if (uvn->u_flags & UVM_VNODE_CANPERSIST) {
    408       1.8       mrg 		/* won't block */
    409       1.8       mrg 		uvn_flush(uobj, 0, 0, PGO_DEACTIVATE|PGO_ALLPAGES);
    410      1.17       chs 		simple_unlock(&uobj->vmobjlock);
    411       1.8       mrg 		vrele(vp);			/* drop vnode reference */
    412       1.8       mrg 		UVMHIST_LOG(maphist,"<- done/vrele!  (persist)", 0,0,0,0);
    413       1.8       mrg 		return;
    414       1.8       mrg 	}
    415       1.8       mrg 
    416       1.8       mrg 	/*
    417       1.8       mrg 	 * its a goner!
    418       1.8       mrg 	 */
    419       1.8       mrg 
    420       1.8       mrg 	UVMHIST_LOG(maphist,"  its a goner (flushing)!", 0,0,0,0);
    421       1.8       mrg 
    422       1.8       mrg 	uvn->u_flags |= UVM_VNODE_DYING;
    423       1.8       mrg 
    424       1.8       mrg 	/*
    425       1.8       mrg 	 * even though we may unlock in flush, no one can gain a reference
    426       1.8       mrg 	 * to us until we clear the "dying" flag [because it blocks
    427       1.8       mrg 	 * attaches].  we will not do that until after we've disposed of all
    428       1.8       mrg 	 * the pages with uvn_flush().  note that before the flush the only
    429       1.8       mrg 	 * pages that could be marked PG_BUSY are ones that are in async
    430       1.8       mrg 	 * pageout by the daemon.  (there can't be any pending "get"'s
    431       1.8       mrg 	 * because there are no references to the object).
    432       1.8       mrg 	 */
    433       1.1       mrg 
    434       1.8       mrg 	(void) uvn_flush(uobj, 0, 0, PGO_CLEANIT|PGO_FREE|PGO_ALLPAGES);
    435       1.1       mrg 
    436       1.8       mrg 	UVMHIST_LOG(maphist,"  its a goner (done flush)!", 0,0,0,0);
    437       1.8       mrg 
    438       1.8       mrg 	/*
    439       1.8       mrg 	 * given the structure of this pager, the above flush request will
    440       1.8       mrg 	 * create the following state: all the pages that were in the object
    441       1.8       mrg 	 * have either been free'd or they are marked PG_BUSY|PG_RELEASED.
    442       1.8       mrg 	 * the PG_BUSY bit was set either by us or the daemon for async I/O.
    443       1.8       mrg 	 * in either case, if we have pages left we can't kill the object
    444       1.8       mrg 	 * yet because i/o is pending.  in this case we set the "relkill"
    445       1.8       mrg 	 * flag which will cause pgo_releasepg to kill the object once all
    446       1.8       mrg 	 * the I/O's are done [pgo_releasepg will be called from the aiodone
    447       1.8       mrg 	 * routine or from the page daemon].
    448       1.8       mrg 	 */
    449       1.1       mrg 
    450       1.8       mrg 	if (uobj->uo_npages) {		/* I/O pending.  iodone will free */
    451  1.26.2.1   thorpej #ifdef DEBUG
    452       1.8       mrg 		/*
    453       1.8       mrg 		 * XXXCDC: very unlikely to happen until we have async i/o
    454       1.8       mrg 		 * so print a little info message in case it does.
    455       1.8       mrg 		 */
    456       1.8       mrg 		printf("uvn_detach: vn %p has pages left after flush - "
    457       1.8       mrg 		    "relkill mode\n", uobj);
    458       1.1       mrg #endif
    459       1.8       mrg 		uvn->u_flags |= UVM_VNODE_RELKILL;
    460       1.8       mrg 		simple_unlock(&uobj->vmobjlock);
    461       1.8       mrg 		UVMHIST_LOG(maphist,"<- done! (releasepg will kill obj)", 0, 0,
    462       1.8       mrg 		    0, 0);
    463       1.8       mrg 		return;
    464       1.8       mrg 	}
    465       1.8       mrg 
    466       1.8       mrg 	/*
    467       1.8       mrg 	 * kill object now.   note that we can't be on the sync q because
    468       1.8       mrg 	 * all references are gone.
    469       1.8       mrg 	 */
    470       1.8       mrg 	if (uvn->u_flags & UVM_VNODE_WRITEABLE) {
    471       1.8       mrg 		simple_lock(&uvn_wl_lock);		/* protect uvn_wlist */
    472       1.8       mrg 		LIST_REMOVE(uvn, u_wlist);
    473       1.8       mrg 		simple_unlock(&uvn_wl_lock);
    474       1.8       mrg 	}
    475       1.1       mrg #ifdef DIAGNOSTIC
    476       1.8       mrg 	if (uobj->memq.tqh_first != NULL)
    477       1.8       mrg 		panic("uvn_deref: vnode VM object still has pages afer "
    478       1.8       mrg 		    "syncio/free flush");
    479       1.1       mrg #endif
    480       1.8       mrg 	oldflags = uvn->u_flags;
    481       1.8       mrg 	uvn->u_flags = 0;
    482       1.8       mrg 	simple_unlock(&uobj->vmobjlock);
    483       1.8       mrg 
    484       1.8       mrg 	/* wake up any sleepers */
    485       1.8       mrg 	if (oldflags & UVM_VNODE_WANTED)
    486       1.8       mrg 		wakeup(uvn);
    487       1.8       mrg 
    488       1.8       mrg 	/*
    489       1.8       mrg 	 * drop our reference to the vnode.
    490       1.8       mrg 	 */
    491       1.8       mrg 	vrele(vp);
    492       1.8       mrg 	UVMHIST_LOG(maphist,"<- done (vrele) final", 0,0,0,0);
    493       1.1       mrg 
    494       1.8       mrg 	return;
    495       1.1       mrg }
    496       1.1       mrg 
    497       1.1       mrg /*
    498       1.1       mrg  * uvm_vnp_terminate: external hook to clear out a vnode's VM
    499       1.1       mrg  *
    500       1.5       mrg  * called in two cases:
    501       1.5       mrg  *  [1] when a persisting vnode vm object (i.e. one with a zero reference
    502       1.5       mrg  *      count) needs to be freed so that a vnode can be reused.  this
    503       1.5       mrg  *      happens under "getnewvnode" in vfs_subr.c.   if the vnode from
    504       1.5       mrg  *      the free list is still attached (i.e. not VBAD) then vgone is
    505       1.5       mrg  *	called.   as part of the vgone trace this should get called to
    506       1.5       mrg  *	free the vm object.   this is the common case.
    507       1.5       mrg  *  [2] when a filesystem is being unmounted by force (MNT_FORCE,
    508       1.5       mrg  *	"umount -f") the vgone() function is called on active vnodes
    509       1.5       mrg  *	on the mounted file systems to kill their data (the vnodes become
    510       1.5       mrg  *	"dead" ones [see src/sys/miscfs/deadfs/...]).  that results in a
    511       1.5       mrg  *	call here (even if the uvn is still in use -- i.e. has a non-zero
    512       1.5       mrg  *	reference count).  this case happens at "umount -f" and during a
    513       1.5       mrg  *	"reboot/halt" operation.
    514       1.5       mrg  *
    515       1.5       mrg  * => the caller must XLOCK and VOP_LOCK the vnode before calling us
    516       1.5       mrg  *	[protects us from getting a vnode that is already in the DYING
    517       1.5       mrg  *	 state...]
    518       1.5       mrg  * => unlike uvn_detach, this function must not return until all the
    519       1.5       mrg  *	uvn's pages are disposed of.
    520       1.5       mrg  * => in case [2] the uvn is still alive after this call, but all I/O
    521       1.5       mrg  *	ops will fail (due to the backing vnode now being "dead").  this
    522       1.5       mrg  *	will prob. kill any process using the uvn due to pgo_get failing.
    523       1.1       mrg  */
    524       1.1       mrg 
    525       1.8       mrg void
    526       1.8       mrg uvm_vnp_terminate(vp)
    527       1.8       mrg 	struct vnode *vp;
    528       1.8       mrg {
    529       1.8       mrg 	struct uvm_vnode *uvn = &vp->v_uvm;
    530       1.8       mrg 	int oldflags;
    531       1.8       mrg 	UVMHIST_FUNC("uvm_vnp_terminate"); UVMHIST_CALLED(maphist);
    532       1.1       mrg 
    533       1.8       mrg 	/*
    534       1.8       mrg 	 * lock object and check if it is valid
    535       1.8       mrg 	 */
    536       1.8       mrg 	simple_lock(&uvn->u_obj.vmobjlock);
    537       1.8       mrg 	UVMHIST_LOG(maphist, "  vp=0x%x, ref=%d, flag=0x%x", vp,
    538       1.8       mrg 	    uvn->u_obj.uo_refs, uvn->u_flags, 0);
    539       1.8       mrg 	if ((uvn->u_flags & UVM_VNODE_VALID) == 0) {
    540       1.8       mrg 		simple_unlock(&uvn->u_obj.vmobjlock);
    541       1.8       mrg 		UVMHIST_LOG(maphist, "<- done (not active)", 0, 0, 0, 0);
    542       1.8       mrg 		return;
    543       1.8       mrg 	}
    544       1.1       mrg 
    545       1.8       mrg 	/*
    546       1.8       mrg 	 * must be a valid uvn that is not already dying (because XLOCK
    547  1.26.2.2    bouyer 	 * protects us from that).   the uvn can't in the ALOCK state
    548       1.8       mrg 	 * because it is valid, and uvn's that are in the ALOCK state haven't
    549       1.8       mrg 	 * been marked valid yet.
    550       1.8       mrg 	 */
    551       1.1       mrg 
    552       1.5       mrg #ifdef DEBUG
    553       1.8       mrg 	/*
    554       1.8       mrg 	 * debug check: are we yanking the vnode out from under our uvn?
    555       1.8       mrg 	 */
    556       1.8       mrg 	if (uvn->u_obj.uo_refs) {
    557       1.8       mrg 		printf("uvm_vnp_terminate(%p): terminating active vnode "
    558       1.8       mrg 		    "(refs=%d)\n", uvn, uvn->u_obj.uo_refs);
    559       1.8       mrg 	}
    560       1.1       mrg #endif
    561       1.8       mrg 
    562       1.8       mrg 	/*
    563       1.8       mrg 	 * it is possible that the uvn was detached and is in the relkill
    564       1.8       mrg 	 * state [i.e. waiting for async i/o to finish so that releasepg can
    565       1.8       mrg 	 * kill object].  we take over the vnode now and cancel the relkill.
    566       1.8       mrg 	 * we want to know when the i/o is done so we can recycle right
    567       1.8       mrg 	 * away.   note that a uvn can only be in the RELKILL state if it
    568       1.8       mrg 	 * has a zero reference count.
    569       1.8       mrg 	 */
    570       1.8       mrg 
    571       1.8       mrg 	if (uvn->u_flags & UVM_VNODE_RELKILL)
    572       1.8       mrg 		uvn->u_flags &= ~UVM_VNODE_RELKILL;	/* cancel RELKILL */
    573       1.8       mrg 
    574       1.8       mrg 	/*
    575       1.8       mrg 	 * block the uvn by setting the dying flag, and then flush the
    576       1.8       mrg 	 * pages.  (note that flush may unlock object while doing I/O, but
    577       1.8       mrg 	 * it will re-lock it before it returns control here).
    578       1.8       mrg 	 *
    579       1.8       mrg 	 * also, note that we tell I/O that we are already VOP_LOCK'd so
    580       1.8       mrg 	 * that uvn_io doesn't attempt to VOP_LOCK again.
    581       1.8       mrg 	 *
    582       1.8       mrg 	 * XXXCDC: setting VNISLOCKED on an active uvn which is being terminated
    583       1.8       mrg 	 *	due to a forceful unmount might not be a good idea.  maybe we
    584       1.8       mrg 	 *	need a way to pass in this info to uvn_flush through a
    585       1.8       mrg 	 *	pager-defined PGO_ constant [currently there are none].
    586       1.8       mrg 	 */
    587       1.8       mrg 	uvn->u_flags |= UVM_VNODE_DYING|UVM_VNODE_VNISLOCKED;
    588       1.8       mrg 
    589       1.8       mrg 	(void) uvn_flush(&uvn->u_obj, 0, 0, PGO_CLEANIT|PGO_FREE|PGO_ALLPAGES);
    590       1.8       mrg 
    591       1.8       mrg 	/*
    592       1.8       mrg 	 * as we just did a flush we expect all the pages to be gone or in
    593       1.8       mrg 	 * the process of going.  sleep to wait for the rest to go [via iosync].
    594       1.8       mrg 	 */
    595       1.1       mrg 
    596       1.8       mrg 	while (uvn->u_obj.uo_npages) {
    597  1.26.2.1   thorpej #ifdef DEBUG
    598       1.8       mrg 		struct vm_page *pp;
    599       1.8       mrg 		for (pp = uvn->u_obj.memq.tqh_first ; pp != NULL ;
    600       1.8       mrg 		     pp = pp->listq.tqe_next) {
    601       1.8       mrg 			if ((pp->flags & PG_BUSY) == 0)
    602       1.8       mrg 				panic("uvm_vnp_terminate: detected unbusy pg");
    603       1.8       mrg 		}
    604       1.8       mrg 		if (uvn->u_nio == 0)
    605       1.8       mrg 			panic("uvm_vnp_terminate: no I/O to wait for?");
    606       1.8       mrg 		printf("uvm_vnp_terminate: waiting for I/O to fin.\n");
    607       1.8       mrg 		/*
    608       1.8       mrg 		 * XXXCDC: this is unlikely to happen without async i/o so we
    609       1.8       mrg 		 * put a printf in just to keep an eye on it.
    610       1.8       mrg 		 */
    611       1.1       mrg #endif
    612       1.8       mrg 		uvn->u_flags |= UVM_VNODE_IOSYNC;
    613       1.8       mrg 		UVM_UNLOCK_AND_WAIT(&uvn->u_nio, &uvn->u_obj.vmobjlock, FALSE,
    614       1.8       mrg 		    "uvn_term",0);
    615       1.8       mrg 		simple_lock(&uvn->u_obj.vmobjlock);
    616       1.8       mrg 	}
    617       1.8       mrg 
    618       1.8       mrg 	/*
    619       1.8       mrg 	 * done.   now we free the uvn if its reference count is zero
    620       1.8       mrg 	 * (true if we are zapping a persisting uvn).   however, if we are
    621       1.8       mrg 	 * terminating a uvn with active mappings we let it live ... future
    622       1.8       mrg 	 * calls down to the vnode layer will fail.
    623       1.8       mrg 	 */
    624       1.5       mrg 
    625       1.8       mrg 	oldflags = uvn->u_flags;
    626       1.8       mrg 	if (uvn->u_obj.uo_refs) {
    627       1.8       mrg 
    628       1.8       mrg 		/*
    629       1.8       mrg 		 * uvn must live on it is dead-vnode state until all references
    630       1.8       mrg 		 * are gone.   restore flags.    clear CANPERSIST state.
    631       1.8       mrg 		 */
    632       1.8       mrg 
    633       1.8       mrg 		uvn->u_flags &= ~(UVM_VNODE_DYING|UVM_VNODE_VNISLOCKED|
    634       1.5       mrg 		      UVM_VNODE_WANTED|UVM_VNODE_CANPERSIST);
    635       1.8       mrg 
    636       1.8       mrg 	} else {
    637       1.5       mrg 
    638       1.8       mrg 		/*
    639       1.8       mrg 		 * free the uvn now.   note that the VREF reference is already
    640       1.8       mrg 		 * gone [it is dropped when we enter the persist state].
    641       1.8       mrg 		 */
    642       1.8       mrg 		if (uvn->u_flags & UVM_VNODE_IOSYNCWANTED)
    643       1.8       mrg 			panic("uvm_vnp_terminate: io sync wanted bit set");
    644       1.8       mrg 
    645       1.8       mrg 		if (uvn->u_flags & UVM_VNODE_WRITEABLE) {
    646       1.8       mrg 			simple_lock(&uvn_wl_lock);
    647       1.8       mrg 			LIST_REMOVE(uvn, u_wlist);
    648       1.8       mrg 			simple_unlock(&uvn_wl_lock);
    649       1.8       mrg 		}
    650       1.8       mrg 		uvn->u_flags = 0;	/* uvn is history, clear all bits */
    651       1.8       mrg 	}
    652       1.5       mrg 
    653       1.8       mrg 	if (oldflags & UVM_VNODE_WANTED)
    654       1.8       mrg 		wakeup(uvn);		/* object lock still held */
    655       1.5       mrg 
    656       1.8       mrg 	simple_unlock(&uvn->u_obj.vmobjlock);
    657       1.8       mrg 	UVMHIST_LOG(maphist, "<- done", 0, 0, 0, 0);
    658       1.1       mrg 
    659       1.1       mrg }
    660       1.1       mrg 
    661       1.1       mrg /*
    662       1.1       mrg  * uvn_releasepg: handled a released page in a uvn
    663       1.1       mrg  *
    664       1.1       mrg  * => "pg" is a PG_BUSY [caller owns it], PG_RELEASED page that we need
    665       1.1       mrg  *	to dispose of.
    666       1.1       mrg  * => caller must handled PG_WANTED case
    667       1.1       mrg  * => called with page's object locked, pageq's unlocked
    668       1.1       mrg  * => returns TRUE if page's object is still alive, FALSE if we
    669       1.1       mrg  *	killed the page's object.    if we return TRUE, then we
    670       1.1       mrg  *	return with the object locked.
    671       1.1       mrg  * => if (nextpgp != NULL) => we return pageq.tqe_next here, and return
    672       1.1       mrg  *				with the page queues locked [for pagedaemon]
    673       1.1       mrg  * => if (nextpgp == NULL) => we return with page queues unlocked [normal case]
    674       1.1       mrg  * => we kill the uvn if it is not referenced and we are suppose to
    675       1.1       mrg  *	kill it ("relkill").
    676       1.1       mrg  */
    677       1.1       mrg 
    678       1.8       mrg boolean_t
    679       1.8       mrg uvn_releasepg(pg, nextpgp)
    680       1.8       mrg 	struct vm_page *pg;
    681       1.8       mrg 	struct vm_page **nextpgp;	/* OUT */
    682       1.1       mrg {
    683       1.8       mrg 	struct uvm_vnode *uvn = (struct uvm_vnode *) pg->uobject;
    684       1.1       mrg #ifdef DIAGNOSTIC
    685       1.8       mrg 	if ((pg->flags & PG_RELEASED) == 0)
    686       1.8       mrg 		panic("uvn_releasepg: page not released!");
    687       1.1       mrg #endif
    688       1.8       mrg 
    689       1.8       mrg 	/*
    690       1.8       mrg 	 * dispose of the page [caller handles PG_WANTED]
    691       1.8       mrg 	 */
    692      1.26       chs 	pmap_page_protect(pg, VM_PROT_NONE);
    693       1.8       mrg 	uvm_lock_pageq();
    694       1.8       mrg 	if (nextpgp)
    695       1.8       mrg 		*nextpgp = pg->pageq.tqe_next;	/* next page for daemon */
    696       1.8       mrg 	uvm_pagefree(pg);
    697       1.8       mrg 	if (!nextpgp)
    698       1.8       mrg 		uvm_unlock_pageq();
    699       1.8       mrg 
    700       1.8       mrg 	/*
    701       1.8       mrg 	 * now see if we need to kill the object
    702       1.8       mrg 	 */
    703       1.8       mrg 	if (uvn->u_flags & UVM_VNODE_RELKILL) {
    704       1.8       mrg 		if (uvn->u_obj.uo_refs)
    705       1.8       mrg 			panic("uvn_releasepg: kill flag set on referenced "
    706       1.8       mrg 			    "object!");
    707       1.8       mrg 		if (uvn->u_obj.uo_npages == 0) {
    708       1.8       mrg 			if (uvn->u_flags & UVM_VNODE_WRITEABLE) {
    709       1.8       mrg 				simple_lock(&uvn_wl_lock);
    710       1.8       mrg 				LIST_REMOVE(uvn, u_wlist);
    711       1.8       mrg 				simple_unlock(&uvn_wl_lock);
    712       1.8       mrg 			}
    713       1.1       mrg #ifdef DIAGNOSTIC
    714       1.8       mrg 			if (uvn->u_obj.memq.tqh_first)
    715       1.1       mrg 	panic("uvn_releasepg: pages in object with npages == 0");
    716       1.1       mrg #endif
    717       1.8       mrg 			if (uvn->u_flags & UVM_VNODE_WANTED)
    718       1.8       mrg 				/* still holding object lock */
    719       1.8       mrg 				wakeup(uvn);
    720       1.8       mrg 
    721       1.8       mrg 			uvn->u_flags = 0;		/* DEAD! */
    722       1.8       mrg 			simple_unlock(&uvn->u_obj.vmobjlock);
    723       1.8       mrg 			return (FALSE);
    724       1.8       mrg 		}
    725       1.8       mrg 	}
    726       1.8       mrg 	return (TRUE);
    727       1.1       mrg }
    728       1.1       mrg 
    729       1.1       mrg /*
    730       1.1       mrg  * NOTE: currently we have to use VOP_READ/VOP_WRITE because they go
    731       1.1       mrg  * through the buffer cache and allow I/O in any size.  These VOPs use
    732       1.1       mrg  * synchronous i/o.  [vs. VOP_STRATEGY which can be async, but doesn't
    733       1.1       mrg  * go through the buffer cache or allow I/O sizes larger than a
    734       1.1       mrg  * block].  we will eventually want to change this.
    735       1.1       mrg  *
    736       1.1       mrg  * issues to consider:
    737       1.1       mrg  *   uvm provides the uvm_aiodesc structure for async i/o management.
    738       1.1       mrg  * there are two tailq's in the uvm. structure... one for pending async
    739       1.1       mrg  * i/o and one for "done" async i/o.   to do an async i/o one puts
    740       1.1       mrg  * an aiodesc on the "pending" list (protected by splbio()), starts the
    741       1.1       mrg  * i/o and returns VM_PAGER_PEND.    when the i/o is done, we expect
    742       1.1       mrg  * some sort of "i/o done" function to be called (at splbio(), interrupt
    743       1.1       mrg  * time).   this function should remove the aiodesc from the pending list
    744       1.1       mrg  * and place it on the "done" list and wakeup the daemon.   the daemon
    745       1.1       mrg  * will run at normal spl() and will remove all items from the "done"
    746       1.1       mrg  * list and call the "aiodone" hook for each done request (see uvm_pager.c).
    747       1.1       mrg  * [in the old vm code, this was done by calling the "put" routine with
    748       1.1       mrg  * null arguments which made the code harder to read and understand because
    749       1.1       mrg  * you had one function ("put") doing two things.]
    750       1.1       mrg  *
    751       1.1       mrg  * so the current pager needs:
    752       1.1       mrg  *   int uvn_aiodone(struct uvm_aiodesc *)
    753       1.1       mrg  *
    754       1.1       mrg  * => return KERN_SUCCESS (aio finished, free it).  otherwise requeue for
    755       1.1       mrg  *	later collection.
    756       1.1       mrg  * => called with pageq's locked by the daemon.
    757       1.1       mrg  *
    758       1.1       mrg  * general outline:
    759       1.1       mrg  * - "try" to lock object.   if fail, just return (will try again later)
    760       1.1       mrg  * - drop "u_nio" (this req is done!)
    761       1.1       mrg  * - if (object->iosync && u_naio == 0) { wakeup &uvn->u_naio }
    762       1.1       mrg  * - get "page" structures (atop?).
    763       1.1       mrg  * - handle "wanted" pages
    764       1.1       mrg  * - handle "released" pages [using pgo_releasepg]
    765       1.1       mrg  *   >>> pgo_releasepg may kill the object
    766       1.1       mrg  * dont forget to look at "object" wanted flag in all cases.
    767       1.1       mrg  */
    768       1.1       mrg 
    769       1.1       mrg 
    770       1.1       mrg /*
    771       1.1       mrg  * uvn_flush: flush pages out of a uvm object.
    772       1.1       mrg  *
    773       1.1       mrg  * => object should be locked by caller.   we may _unlock_ the object
    774       1.1       mrg  *	if (and only if) we need to clean a page (PGO_CLEANIT).
    775       1.1       mrg  *	we return with the object locked.
    776       1.1       mrg  * => if PGO_CLEANIT is set, we may block (due to I/O).   thus, a caller
    777       1.1       mrg  *	might want to unlock higher level resources (e.g. vm_map)
    778       1.1       mrg  *	before calling flush.
    779       1.1       mrg  * => if PGO_CLEANIT is not set, then we will neither unlock the object
    780       1.1       mrg  *	or block.
    781       1.1       mrg  * => if PGO_ALLPAGE is set, then all pages in the object are valid targets
    782       1.1       mrg  *	for flushing.
    783       1.1       mrg  * => NOTE: we rely on the fact that the object's memq is a TAILQ and
    784       1.1       mrg  *	that new pages are inserted on the tail end of the list.   thus,
    785       1.1       mrg  *	we can make a complete pass through the object in one go by starting
    786       1.1       mrg  *	at the head and working towards the tail (new pages are put in
    787       1.1       mrg  *	front of us).
    788       1.1       mrg  * => NOTE: we are allowed to lock the page queues, so the caller
    789       1.1       mrg  *	must not be holding the lock on them [e.g. pagedaemon had
    790       1.1       mrg  *	better not call us with the queues locked]
    791       1.1       mrg  * => we return TRUE unless we encountered some sort of I/O error
    792       1.1       mrg  *
    793       1.1       mrg  * comment on "cleaning" object and PG_BUSY pages:
    794       1.1       mrg  *	this routine is holding the lock on the object.   the only time
    795       1.1       mrg  *	that it can run into a PG_BUSY page that it does not own is if
    796       1.1       mrg  *	some other process has started I/O on the page (e.g. either
    797       1.1       mrg  *	a pagein, or a pageout).    if the PG_BUSY page is being paged
    798       1.1       mrg  *	in, then it can not be dirty (!PG_CLEAN) because no one has
    799       1.1       mrg  *	had a chance to modify it yet.    if the PG_BUSY page is being
    800       1.1       mrg  *	paged out then it means that someone else has already started
    801       1.1       mrg  *	cleaning the page for us (how nice!).    in this case, if we
    802       1.1       mrg  *	have syncio specified, then after we make our pass through the
    803       1.1       mrg  *	object we need to wait for the other PG_BUSY pages to clear
    804       1.1       mrg  *	off (i.e. we need to do an iosync).   also note that once a
    805       1.1       mrg  *	page is PG_BUSY it must stay in its object until it is un-busyed.
    806       1.1       mrg  *
    807       1.1       mrg  * note on page traversal:
    808       1.1       mrg  *	we can traverse the pages in an object either by going down the
    809       1.1       mrg  *	linked list in "uobj->memq", or we can go over the address range
    810       1.1       mrg  *	by page doing hash table lookups for each address.    depending
    811       1.1       mrg  *	on how many pages are in the object it may be cheaper to do one
    812       1.1       mrg  *	or the other.   we set "by_list" to true if we are using memq.
    813       1.1       mrg  *	if the cost of a hash lookup was equal to the cost of the list
    814       1.1       mrg  *	traversal we could compare the number of pages in the start->stop
    815       1.1       mrg  *	range to the total number of pages in the object.   however, it
    816       1.1       mrg  *	seems that a hash table lookup is more expensive than the linked
    817       1.1       mrg  *	list traversal, so we multiply the number of pages in the
    818       1.1       mrg  *	start->stop range by a penalty which we define below.
    819       1.1       mrg  */
    820       1.1       mrg 
    821       1.8       mrg #define UVN_HASH_PENALTY 4	/* XXX: a guess */
    822       1.1       mrg 
    823       1.8       mrg static boolean_t
    824       1.8       mrg uvn_flush(uobj, start, stop, flags)
    825       1.8       mrg 	struct uvm_object *uobj;
    826  1.26.2.2    bouyer 	voff_t start, stop;
    827       1.8       mrg 	int flags;
    828       1.8       mrg {
    829       1.8       mrg 	struct uvm_vnode *uvn = (struct uvm_vnode *) uobj;
    830       1.8       mrg 	struct vm_page *pp, *ppnext, *ptmp;
    831      1.16       chs 	struct vm_page *pps[MAXBSIZE >> PAGE_SHIFT], **ppsp;
    832       1.8       mrg 	int npages, result, lcv;
    833  1.26.2.2    bouyer 	boolean_t retval, need_iosync, by_list, needs_clean, all;
    834  1.26.2.2    bouyer 	voff_t curoff;
    835       1.8       mrg 	u_short pp_version;
    836       1.8       mrg 	UVMHIST_FUNC("uvn_flush"); UVMHIST_CALLED(maphist);
    837       1.8       mrg 
    838       1.8       mrg 	curoff = 0;	/* XXX: shut up gcc */
    839       1.8       mrg 	/*
    840       1.8       mrg 	 * get init vals and determine how we are going to traverse object
    841       1.8       mrg 	 */
    842       1.1       mrg 
    843       1.8       mrg 	need_iosync = FALSE;
    844       1.8       mrg 	retval = TRUE;		/* return value */
    845       1.8       mrg 	if (flags & PGO_ALLPAGES) {
    846  1.26.2.2    bouyer 		all = TRUE;
    847       1.8       mrg 		by_list = TRUE;		/* always go by the list */
    848       1.8       mrg 	} else {
    849       1.8       mrg 		start = trunc_page(start);
    850       1.8       mrg 		stop = round_page(stop);
    851  1.26.2.1   thorpej #ifdef DEBUG
    852       1.8       mrg 		if (stop > round_page(uvn->u_size))
    853       1.8       mrg 			printf("uvn_flush: strange, got an out of range "
    854       1.8       mrg 			    "flush (fixed)\n");
    855  1.26.2.1   thorpej #endif
    856  1.26.2.2    bouyer 		all = FALSE;
    857       1.8       mrg 		by_list = (uobj->uo_npages <=
    858      1.16       chs 		    ((stop - start) >> PAGE_SHIFT) * UVN_HASH_PENALTY);
    859       1.8       mrg 	}
    860       1.8       mrg 
    861       1.8       mrg 	UVMHIST_LOG(maphist,
    862       1.8       mrg 	    " flush start=0x%x, stop=0x%x, by_list=%d, flags=0x%x",
    863       1.8       mrg 	    start, stop, by_list, flags);
    864       1.8       mrg 
    865       1.8       mrg 	/*
    866       1.8       mrg 	 * PG_CLEANCHK: this bit is used by the pgo_mk_pcluster function as
    867       1.8       mrg 	 * a _hint_ as to how up to date the PG_CLEAN bit is.   if the hint
    868       1.8       mrg 	 * is wrong it will only prevent us from clustering... it won't break
    869       1.8       mrg 	 * anything.   we clear all PG_CLEANCHK bits here, and pgo_mk_pcluster
    870       1.8       mrg 	 * will set them as it syncs PG_CLEAN.   This is only an issue if we
    871       1.8       mrg 	 * are looking at non-inactive pages (because inactive page's PG_CLEAN
    872       1.8       mrg 	 * bit is always up to date since there are no mappings).
    873       1.8       mrg 	 * [borrowed PG_CLEANCHK idea from FreeBSD VM]
    874       1.8       mrg 	 */
    875       1.1       mrg 
    876       1.8       mrg 	if ((flags & PGO_CLEANIT) != 0 &&
    877       1.8       mrg 	    uobj->pgops->pgo_mk_pcluster != NULL) {
    878       1.8       mrg 		if (by_list) {
    879       1.8       mrg 			for (pp = uobj->memq.tqh_first ; pp != NULL ;
    880       1.8       mrg 			    pp = pp->listq.tqe_next) {
    881  1.26.2.2    bouyer 				if (!all &&
    882  1.26.2.2    bouyer 				    (pp->offset < start || pp->offset >= stop))
    883       1.8       mrg 					continue;
    884       1.8       mrg 				pp->flags &= ~PG_CLEANCHK;
    885       1.8       mrg 			}
    886       1.8       mrg 
    887       1.8       mrg 		} else {   /* by hash */
    888       1.8       mrg 			for (curoff = start ; curoff < stop;
    889       1.8       mrg 			    curoff += PAGE_SIZE) {
    890       1.8       mrg 				pp = uvm_pagelookup(uobj, curoff);
    891       1.8       mrg 				if (pp)
    892       1.8       mrg 					pp->flags &= ~PG_CLEANCHK;
    893       1.8       mrg 			}
    894       1.8       mrg 		}
    895       1.8       mrg 	}
    896       1.1       mrg 
    897       1.8       mrg 	/*
    898       1.8       mrg 	 * now do it.   note: we must update ppnext in body of loop or we
    899       1.8       mrg 	 * will get stuck.  we need to use ppnext because we may free "pp"
    900       1.8       mrg 	 * before doing the next loop.
    901       1.8       mrg 	 */
    902       1.1       mrg 
    903       1.8       mrg 	if (by_list) {
    904       1.8       mrg 		pp = uobj->memq.tqh_first;
    905       1.1       mrg 	} else {
    906       1.8       mrg 		curoff = start;
    907       1.8       mrg 		pp = uvm_pagelookup(uobj, curoff);
    908       1.1       mrg 	}
    909       1.8       mrg 
    910       1.8       mrg 	ppnext = NULL;	/* XXX: shut up gcc */
    911       1.8       mrg 	ppsp = NULL;		/* XXX: shut up gcc */
    912       1.8       mrg 	uvm_lock_pageq();	/* page queues locked */
    913       1.8       mrg 
    914       1.8       mrg 	/* locked: both page queues and uobj */
    915       1.8       mrg 	for ( ; (by_list && pp != NULL) ||
    916       1.8       mrg 	  (!by_list && curoff < stop) ; pp = ppnext) {
    917       1.8       mrg 
    918       1.8       mrg 		if (by_list) {
    919       1.8       mrg 
    920       1.8       mrg 			/*
    921       1.8       mrg 			 * range check
    922       1.8       mrg 			 */
    923       1.8       mrg 
    924  1.26.2.2    bouyer 			if (!all &&
    925  1.26.2.2    bouyer 			    (pp->offset < start || pp->offset >= stop)) {
    926       1.8       mrg 				ppnext = pp->listq.tqe_next;
    927       1.8       mrg 				continue;
    928       1.8       mrg 			}
    929       1.8       mrg 
    930       1.8       mrg 		} else {
    931       1.8       mrg 
    932       1.8       mrg 			/*
    933       1.8       mrg 			 * null check
    934       1.8       mrg 			 */
    935       1.8       mrg 
    936       1.8       mrg 			curoff += PAGE_SIZE;
    937       1.8       mrg 			if (pp == NULL) {
    938       1.8       mrg 				if (curoff < stop)
    939       1.8       mrg 					ppnext = uvm_pagelookup(uobj, curoff);
    940       1.8       mrg 				continue;
    941       1.8       mrg 			}
    942       1.8       mrg 
    943       1.8       mrg 		}
    944       1.8       mrg 
    945       1.8       mrg 		/*
    946       1.8       mrg 		 * handle case where we do not need to clean page (either
    947       1.8       mrg 		 * because we are not clean or because page is not dirty or
    948       1.8       mrg 		 * is busy):
    949       1.8       mrg 		 *
    950       1.8       mrg 		 * NOTE: we are allowed to deactivate a non-wired active
    951       1.8       mrg 		 * PG_BUSY page, but once a PG_BUSY page is on the inactive
    952       1.8       mrg 		 * queue it must stay put until it is !PG_BUSY (so as not to
    953       1.8       mrg 		 * confuse pagedaemon).
    954       1.8       mrg 		 */
    955       1.8       mrg 
    956       1.8       mrg 		if ((flags & PGO_CLEANIT) == 0 || (pp->flags & PG_BUSY) != 0) {
    957       1.8       mrg 			needs_clean = FALSE;
    958       1.8       mrg 			if ((pp->flags & PG_BUSY) != 0 &&
    959       1.8       mrg 			    (flags & (PGO_CLEANIT|PGO_SYNCIO)) ==
    960       1.8       mrg 			             (PGO_CLEANIT|PGO_SYNCIO))
    961       1.8       mrg 				need_iosync = TRUE;
    962       1.8       mrg 		} else {
    963       1.8       mrg 			/*
    964       1.8       mrg 			 * freeing: nuke all mappings so we can sync
    965       1.8       mrg 			 * PG_CLEAN bit with no race
    966       1.8       mrg 			 */
    967       1.8       mrg 			if ((pp->flags & PG_CLEAN) != 0 &&
    968       1.8       mrg 			    (flags & PGO_FREE) != 0 &&
    969       1.8       mrg 			    (pp->pqflags & PQ_ACTIVE) != 0)
    970      1.26       chs 				pmap_page_protect(pp, VM_PROT_NONE);
    971       1.8       mrg 			if ((pp->flags & PG_CLEAN) != 0 &&
    972      1.26       chs 			    pmap_is_modified(pp))
    973       1.8       mrg 				pp->flags &= ~(PG_CLEAN);
    974       1.8       mrg 			pp->flags |= PG_CLEANCHK;	/* update "hint" */
    975       1.8       mrg 
    976       1.8       mrg 			needs_clean = ((pp->flags & PG_CLEAN) == 0);
    977       1.8       mrg 		}
    978       1.8       mrg 
    979       1.8       mrg 		/*
    980       1.8       mrg 		 * if we don't need a clean... load ppnext and dispose of pp
    981       1.8       mrg 		 */
    982       1.8       mrg 		if (!needs_clean) {
    983       1.8       mrg 			/* load ppnext */
    984       1.8       mrg 			if (by_list)
    985       1.8       mrg 				ppnext = pp->listq.tqe_next;
    986       1.8       mrg 			else {
    987       1.8       mrg 				if (curoff < stop)
    988       1.8       mrg 					ppnext = uvm_pagelookup(uobj, curoff);
    989       1.8       mrg 			}
    990       1.8       mrg 
    991       1.8       mrg 			/* now dispose of pp */
    992       1.8       mrg 			if (flags & PGO_DEACTIVATE) {
    993       1.8       mrg 				if ((pp->pqflags & PQ_INACTIVE) == 0 &&
    994       1.8       mrg 				    pp->wire_count == 0) {
    995      1.26       chs 					pmap_page_protect(pp, VM_PROT_NONE);
    996       1.8       mrg 					uvm_pagedeactivate(pp);
    997       1.8       mrg 				}
    998       1.8       mrg 
    999       1.8       mrg 			} else if (flags & PGO_FREE) {
   1000       1.8       mrg 				if (pp->flags & PG_BUSY) {
   1001       1.8       mrg 					/* release busy pages */
   1002       1.8       mrg 					pp->flags |= PG_RELEASED;
   1003       1.8       mrg 				} else {
   1004      1.26       chs 					pmap_page_protect(pp, VM_PROT_NONE);
   1005       1.8       mrg 					/* removed page from object */
   1006       1.8       mrg 					uvm_pagefree(pp);
   1007       1.8       mrg 				}
   1008       1.8       mrg 			}
   1009       1.8       mrg 			/* ppnext is valid so we can continue... */
   1010       1.8       mrg 			continue;
   1011       1.8       mrg 		}
   1012       1.8       mrg 
   1013       1.8       mrg 		/*
   1014       1.8       mrg 		 * pp points to a page in the locked object that we are
   1015       1.8       mrg 		 * working on.  if it is !PG_CLEAN,!PG_BUSY and we asked
   1016       1.8       mrg 		 * for cleaning (PGO_CLEANIT).  we clean it now.
   1017       1.8       mrg 		 *
   1018       1.8       mrg 		 * let uvm_pager_put attempted a clustered page out.
   1019       1.8       mrg 		 * note: locked: uobj and page queues.
   1020       1.8       mrg 		 */
   1021       1.8       mrg 
   1022       1.8       mrg 		pp->flags |= PG_BUSY;	/* we 'own' page now */
   1023       1.8       mrg 		UVM_PAGE_OWN(pp, "uvn_flush");
   1024      1.26       chs 		pmap_page_protect(pp, VM_PROT_READ);
   1025       1.8       mrg 		pp_version = pp->version;
   1026       1.1       mrg ReTry:
   1027       1.8       mrg 		ppsp = pps;
   1028       1.8       mrg 		npages = sizeof(pps) / sizeof(struct vm_page *);
   1029       1.1       mrg 
   1030       1.8       mrg 		/* locked: page queues, uobj */
   1031       1.8       mrg 		result = uvm_pager_put(uobj, pp, &ppsp, &npages,
   1032       1.1       mrg 			   flags | PGO_DOACTCLUST, start, stop);
   1033       1.8       mrg 		/* unlocked: page queues, uobj */
   1034       1.1       mrg 
   1035       1.8       mrg 		/*
   1036       1.8       mrg 		 * at this point nothing is locked.   if we did an async I/O
   1037       1.8       mrg 		 * it is remotely possible for the async i/o to complete and
   1038       1.8       mrg 		 * the page "pp" be freed or what not before we get a chance
   1039       1.8       mrg 		 * to relock the object.   in order to detect this, we have
   1040       1.8       mrg 		 * saved the version number of the page in "pp_version".
   1041       1.8       mrg 		 */
   1042       1.8       mrg 
   1043       1.8       mrg 		/* relock! */
   1044       1.8       mrg 		simple_lock(&uobj->vmobjlock);
   1045       1.8       mrg 		uvm_lock_pageq();
   1046       1.8       mrg 
   1047       1.8       mrg 		/*
   1048       1.8       mrg 		 * VM_PAGER_AGAIN: given the structure of this pager, this
   1049       1.8       mrg 		 * can only happen when  we are doing async I/O and can't
   1050       1.8       mrg 		 * map the pages into kernel memory (pager_map) due to lack
   1051       1.8       mrg 		 * of vm space.   if this happens we drop back to sync I/O.
   1052       1.8       mrg 		 */
   1053       1.8       mrg 
   1054       1.8       mrg 		if (result == VM_PAGER_AGAIN) {
   1055       1.8       mrg 			/*
   1056       1.8       mrg 			 * it is unlikely, but page could have been released
   1057       1.8       mrg 			 * while we had the object lock dropped.   we ignore
   1058       1.8       mrg 			 * this now and retry the I/O.  we will detect and
   1059       1.8       mrg 			 * handle the released page after the syncio I/O
   1060       1.8       mrg 			 * completes.
   1061       1.8       mrg 			 */
   1062       1.1       mrg #ifdef DIAGNOSTIC
   1063       1.8       mrg 			if (flags & PGO_SYNCIO)
   1064       1.1       mrg 	panic("uvn_flush: PGO_SYNCIO return 'try again' error (impossible)");
   1065       1.1       mrg #endif
   1066       1.8       mrg 			flags |= PGO_SYNCIO;
   1067       1.8       mrg 			goto ReTry;
   1068       1.8       mrg 		}
   1069       1.8       mrg 
   1070       1.8       mrg 		/*
   1071       1.8       mrg 		 * the cleaning operation is now done.   finish up.  note that
   1072       1.8       mrg 		 * on error (!OK, !PEND) uvm_pager_put drops the cluster for us.
   1073       1.8       mrg 		 * if success (OK, PEND) then uvm_pager_put returns the cluster
   1074       1.8       mrg 		 * to us in ppsp/npages.
   1075       1.8       mrg 		 */
   1076       1.8       mrg 
   1077       1.8       mrg 		/*
   1078       1.8       mrg 		 * for pending async i/o if we are not deactivating/freeing
   1079       1.8       mrg 		 * we can move on to the next page.
   1080       1.8       mrg 		 */
   1081       1.8       mrg 
   1082       1.8       mrg 		if (result == VM_PAGER_PEND) {
   1083       1.8       mrg 
   1084       1.8       mrg 			if ((flags & (PGO_DEACTIVATE|PGO_FREE)) == 0) {
   1085       1.8       mrg 				/*
   1086       1.8       mrg 				 * no per-page ops: refresh ppnext and continue
   1087       1.8       mrg 				 */
   1088       1.8       mrg 				if (by_list) {
   1089       1.8       mrg 					if (pp->version == pp_version)
   1090       1.8       mrg 						ppnext = pp->listq.tqe_next;
   1091       1.8       mrg 					else
   1092       1.8       mrg 						/* reset */
   1093       1.8       mrg 						ppnext = uobj->memq.tqh_first;
   1094       1.8       mrg 				} else {
   1095       1.8       mrg 					if (curoff < stop)
   1096       1.8       mrg 						ppnext = uvm_pagelookup(uobj,
   1097       1.8       mrg 						    curoff);
   1098       1.8       mrg 				}
   1099       1.8       mrg 				continue;
   1100       1.8       mrg 			}
   1101       1.8       mrg 
   1102       1.8       mrg 			/* need to do anything here? */
   1103       1.8       mrg 		}
   1104       1.8       mrg 
   1105       1.8       mrg 		/*
   1106       1.8       mrg 		 * need to look at each page of the I/O operation.  we defer
   1107       1.8       mrg 		 * processing "pp" until the last trip through this "for" loop
   1108       1.8       mrg 		 * so that we can load "ppnext" for the main loop after we
   1109       1.8       mrg 		 * play with the cluster pages [thus the "npages + 1" in the
   1110       1.8       mrg 		 * loop below].
   1111       1.8       mrg 		 */
   1112       1.8       mrg 
   1113       1.8       mrg 		for (lcv = 0 ; lcv < npages + 1 ; lcv++) {
   1114       1.8       mrg 
   1115       1.8       mrg 			/*
   1116       1.8       mrg 			 * handle ppnext for outside loop, and saving pp
   1117       1.8       mrg 			 * until the end.
   1118       1.8       mrg 			 */
   1119       1.8       mrg 			if (lcv < npages) {
   1120       1.8       mrg 				if (ppsp[lcv] == pp)
   1121       1.8       mrg 					continue; /* skip pp until the end */
   1122       1.8       mrg 				ptmp = ppsp[lcv];
   1123       1.8       mrg 			} else {
   1124       1.8       mrg 				ptmp = pp;
   1125       1.8       mrg 
   1126       1.8       mrg 				/* set up next page for outer loop */
   1127       1.8       mrg 				if (by_list) {
   1128       1.8       mrg 					if (pp->version == pp_version)
   1129       1.8       mrg 						ppnext = pp->listq.tqe_next;
   1130       1.8       mrg 					else
   1131       1.8       mrg 						/* reset */
   1132       1.8       mrg 						ppnext = uobj->memq.tqh_first;
   1133       1.8       mrg 				} else {
   1134       1.8       mrg 					if (curoff < stop)
   1135       1.8       mrg 					ppnext = uvm_pagelookup(uobj, curoff);
   1136       1.8       mrg 				}
   1137       1.8       mrg 			}
   1138       1.8       mrg 
   1139       1.8       mrg 			/*
   1140       1.8       mrg 			 * verify the page didn't get moved while obj was
   1141       1.8       mrg 			 * unlocked
   1142       1.8       mrg 			 */
   1143       1.8       mrg 			if (result == VM_PAGER_PEND && ptmp->uobject != uobj)
   1144       1.8       mrg 				continue;
   1145       1.8       mrg 
   1146       1.8       mrg 			/*
   1147       1.8       mrg 			 * unbusy the page if I/O is done.   note that for
   1148       1.8       mrg 			 * pending I/O it is possible that the I/O op
   1149       1.8       mrg 			 * finished before we relocked the object (in
   1150       1.8       mrg 			 * which case the page is no longer busy).
   1151       1.8       mrg 			 */
   1152       1.8       mrg 
   1153       1.8       mrg 			if (result != VM_PAGER_PEND) {
   1154       1.8       mrg 				if (ptmp->flags & PG_WANTED)
   1155       1.8       mrg 					/* still holding object lock */
   1156      1.25   thorpej 					wakeup(ptmp);
   1157       1.8       mrg 
   1158       1.8       mrg 				ptmp->flags &= ~(PG_WANTED|PG_BUSY);
   1159       1.8       mrg 				UVM_PAGE_OWN(ptmp, NULL);
   1160       1.8       mrg 				if (ptmp->flags & PG_RELEASED) {
   1161       1.8       mrg 
   1162       1.8       mrg 					/* pgo_releasepg wants this */
   1163       1.8       mrg 					uvm_unlock_pageq();
   1164       1.8       mrg 					if (!uvn_releasepg(ptmp, NULL))
   1165       1.8       mrg 						return (TRUE);
   1166       1.8       mrg 
   1167       1.8       mrg 					uvm_lock_pageq();	/* relock */
   1168       1.8       mrg 					continue;		/* next page */
   1169       1.8       mrg 
   1170       1.8       mrg 				} else {
   1171       1.8       mrg 					ptmp->flags |= (PG_CLEAN|PG_CLEANCHK);
   1172       1.8       mrg 					if ((flags & PGO_FREE) == 0)
   1173      1.26       chs 						pmap_clear_modify(ptmp);
   1174       1.8       mrg 				}
   1175       1.8       mrg 			}
   1176       1.8       mrg 
   1177       1.8       mrg 			/*
   1178       1.8       mrg 			 * dispose of page
   1179       1.8       mrg 			 */
   1180       1.8       mrg 
   1181       1.8       mrg 			if (flags & PGO_DEACTIVATE) {
   1182       1.8       mrg 				if ((pp->pqflags & PQ_INACTIVE) == 0 &&
   1183       1.8       mrg 				    pp->wire_count == 0) {
   1184      1.26       chs 					pmap_page_protect(ptmp, VM_PROT_NONE);
   1185       1.8       mrg 					uvm_pagedeactivate(ptmp);
   1186       1.8       mrg 				}
   1187       1.8       mrg 
   1188       1.8       mrg 			} else if (flags & PGO_FREE) {
   1189       1.8       mrg 				if (result == VM_PAGER_PEND) {
   1190       1.8       mrg 					if ((ptmp->flags & PG_BUSY) != 0)
   1191       1.8       mrg 						/* signal for i/o done */
   1192       1.8       mrg 						ptmp->flags |= PG_RELEASED;
   1193       1.8       mrg 				} else {
   1194       1.8       mrg 					if (result != VM_PAGER_OK) {
   1195       1.8       mrg 						printf("uvn_flush: obj=%p, "
   1196  1.26.2.2    bouyer 						   "offset=0x%llx.  error "
   1197       1.8       mrg 						   "during pageout.\n",
   1198  1.26.2.2    bouyer 						    pp->uobject,
   1199  1.26.2.2    bouyer 						    (long long)pp->offset);
   1200       1.8       mrg 						printf("uvn_flush: WARNING: "
   1201       1.8       mrg 						    "changes to page may be "
   1202       1.8       mrg 						    "lost!\n");
   1203       1.8       mrg 						retval = FALSE;
   1204       1.8       mrg 					}
   1205      1.26       chs 					pmap_page_protect(ptmp, VM_PROT_NONE);
   1206       1.8       mrg 					uvm_pagefree(ptmp);
   1207       1.8       mrg 				}
   1208       1.8       mrg 			}
   1209       1.1       mrg 
   1210       1.8       mrg 		}		/* end of "lcv" for loop */
   1211       1.1       mrg 
   1212       1.8       mrg 	}		/* end of "pp" for loop */
   1213       1.1       mrg 
   1214       1.8       mrg 	/*
   1215       1.8       mrg 	 * done with pagequeues: unlock
   1216       1.8       mrg 	 */
   1217       1.8       mrg 	uvm_unlock_pageq();
   1218       1.1       mrg 
   1219       1.8       mrg 	/*
   1220       1.8       mrg 	 * now wait for all I/O if required.
   1221       1.8       mrg 	 */
   1222       1.8       mrg 	if (need_iosync) {
   1223       1.1       mrg 
   1224       1.8       mrg 		UVMHIST_LOG(maphist,"  <<DOING IOSYNC>>",0,0,0,0);
   1225       1.8       mrg 		while (uvn->u_nio != 0) {
   1226       1.8       mrg 			uvn->u_flags |= UVM_VNODE_IOSYNC;
   1227       1.8       mrg 			UVM_UNLOCK_AND_WAIT(&uvn->u_nio, &uvn->u_obj.vmobjlock,
   1228       1.8       mrg 			  FALSE, "uvn_flush",0);
   1229       1.8       mrg 			simple_lock(&uvn->u_obj.vmobjlock);
   1230       1.8       mrg 		}
   1231       1.8       mrg 		if (uvn->u_flags & UVM_VNODE_IOSYNCWANTED)
   1232       1.8       mrg 			wakeup(&uvn->u_flags);
   1233       1.8       mrg 		uvn->u_flags &= ~(UVM_VNODE_IOSYNC|UVM_VNODE_IOSYNCWANTED);
   1234       1.1       mrg 	}
   1235       1.1       mrg 
   1236       1.8       mrg 	/* return, with object locked! */
   1237       1.8       mrg 	UVMHIST_LOG(maphist,"<- done (retval=0x%x)",retval,0,0,0);
   1238       1.8       mrg 	return(retval);
   1239       1.1       mrg }
   1240       1.1       mrg 
   1241       1.1       mrg /*
   1242       1.1       mrg  * uvn_cluster
   1243       1.1       mrg  *
   1244       1.1       mrg  * we are about to do I/O in an object at offset.   this function is called
   1245       1.1       mrg  * to establish a range of offsets around "offset" in which we can cluster
   1246       1.1       mrg  * I/O.
   1247       1.1       mrg  *
   1248       1.1       mrg  * - currently doesn't matter if obj locked or not.
   1249       1.1       mrg  */
   1250       1.1       mrg 
   1251       1.8       mrg static void
   1252       1.8       mrg uvn_cluster(uobj, offset, loffset, hoffset)
   1253       1.8       mrg 	struct uvm_object *uobj;
   1254  1.26.2.2    bouyer 	voff_t offset;
   1255  1.26.2.2    bouyer 	voff_t *loffset, *hoffset; /* OUT */
   1256       1.1       mrg {
   1257       1.8       mrg 	struct uvm_vnode *uvn = (struct uvm_vnode *) uobj;
   1258       1.8       mrg 	*loffset = offset;
   1259       1.1       mrg 
   1260       1.8       mrg 	if (*loffset >= uvn->u_size)
   1261       1.8       mrg 		panic("uvn_cluster: offset out of range");
   1262       1.1       mrg 
   1263       1.8       mrg 	/*
   1264       1.8       mrg 	 * XXX: old pager claims we could use VOP_BMAP to get maxcontig value.
   1265       1.8       mrg 	 */
   1266       1.8       mrg 	*hoffset = *loffset + MAXBSIZE;
   1267       1.8       mrg 	if (*hoffset > round_page(uvn->u_size))	/* past end? */
   1268       1.8       mrg 		*hoffset = round_page(uvn->u_size);
   1269       1.1       mrg 
   1270       1.8       mrg 	return;
   1271       1.1       mrg }
   1272       1.1       mrg 
   1273       1.1       mrg /*
   1274       1.1       mrg  * uvn_put: flush page data to backing store.
   1275       1.1       mrg  *
   1276       1.1       mrg  * => prefer map unlocked (not required)
   1277       1.1       mrg  * => object must be locked!   we will _unlock_ it before starting I/O.
   1278       1.1       mrg  * => flags: PGO_SYNCIO -- use sync. I/O
   1279       1.1       mrg  * => note: caller must set PG_CLEAN and pmap_clear_modify (if needed)
   1280       1.1       mrg  * => XXX: currently we use VOP_READ/VOP_WRITE which are only sync.
   1281       1.1       mrg  *	[thus we never do async i/o!  see iodone comment]
   1282       1.1       mrg  */
   1283       1.1       mrg 
   1284       1.8       mrg static int
   1285       1.8       mrg uvn_put(uobj, pps, npages, flags)
   1286       1.8       mrg 	struct uvm_object *uobj;
   1287       1.8       mrg 	struct vm_page **pps;
   1288       1.8       mrg 	int npages, flags;
   1289       1.1       mrg {
   1290       1.8       mrg 	int retval;
   1291       1.1       mrg 
   1292       1.8       mrg 	/* note: object locked */
   1293       1.8       mrg 	retval = uvn_io((struct uvm_vnode*)uobj, pps, npages, flags, UIO_WRITE);
   1294       1.8       mrg 	/* note: object unlocked */
   1295       1.1       mrg 
   1296       1.8       mrg 	return(retval);
   1297       1.1       mrg }
   1298       1.1       mrg 
   1299       1.1       mrg 
   1300       1.1       mrg /*
   1301       1.1       mrg  * uvn_get: get pages (synchronously) from backing store
   1302       1.1       mrg  *
   1303       1.1       mrg  * => prefer map unlocked (not required)
   1304       1.1       mrg  * => object must be locked!  we will _unlock_ it before starting any I/O.
   1305       1.1       mrg  * => flags: PGO_ALLPAGES: get all of the pages
   1306       1.1       mrg  *           PGO_LOCKED: fault data structures are locked
   1307       1.1       mrg  * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx]
   1308       1.1       mrg  * => NOTE: caller must check for released pages!!
   1309       1.1       mrg  */
   1310       1.1       mrg 
   1311       1.8       mrg static int
   1312       1.8       mrg uvn_get(uobj, offset, pps, npagesp, centeridx, access_type, advice, flags)
   1313       1.8       mrg 	struct uvm_object *uobj;
   1314  1.26.2.2    bouyer 	voff_t offset;
   1315       1.8       mrg 	struct vm_page **pps;		/* IN/OUT */
   1316       1.8       mrg 	int *npagesp;			/* IN (OUT if PGO_LOCKED) */
   1317       1.8       mrg 	int centeridx, advice, flags;
   1318       1.8       mrg 	vm_prot_t access_type;
   1319       1.8       mrg {
   1320  1.26.2.2    bouyer 	voff_t current_offset;
   1321       1.8       mrg 	struct vm_page *ptmp;
   1322       1.8       mrg 	int lcv, result, gotpages;
   1323       1.8       mrg 	boolean_t done;
   1324       1.8       mrg 	UVMHIST_FUNC("uvn_get"); UVMHIST_CALLED(maphist);
   1325       1.8       mrg 	UVMHIST_LOG(maphist, "flags=%d", flags,0,0,0);
   1326       1.8       mrg 
   1327       1.8       mrg 	/*
   1328       1.8       mrg 	 * step 1: handled the case where fault data structures are locked.
   1329       1.8       mrg 	 */
   1330       1.8       mrg 
   1331       1.8       mrg 	if (flags & PGO_LOCKED) {
   1332       1.1       mrg 
   1333       1.8       mrg 		/*
   1334       1.8       mrg 		 * gotpages is the current number of pages we've gotten (which
   1335       1.8       mrg 		 * we pass back up to caller via *npagesp.
   1336       1.8       mrg 		 */
   1337       1.8       mrg 
   1338       1.8       mrg 		gotpages = 0;
   1339       1.8       mrg 
   1340       1.8       mrg 		/*
   1341       1.8       mrg 		 * step 1a: get pages that are already resident.   only do this
   1342       1.8       mrg 		 * if the data structures are locked (i.e. the first time
   1343       1.8       mrg 		 * through).
   1344       1.8       mrg 		 */
   1345       1.8       mrg 
   1346       1.8       mrg 		done = TRUE;	/* be optimistic */
   1347       1.8       mrg 
   1348       1.8       mrg 		for (lcv = 0, current_offset = offset ; lcv < *npagesp ;
   1349       1.8       mrg 		    lcv++, current_offset += PAGE_SIZE) {
   1350       1.8       mrg 
   1351       1.8       mrg 			/* do we care about this page?  if not, skip it */
   1352       1.8       mrg 			if (pps[lcv] == PGO_DONTCARE)
   1353       1.8       mrg 				continue;
   1354       1.8       mrg 
   1355       1.8       mrg 			/* lookup page */
   1356       1.8       mrg 			ptmp = uvm_pagelookup(uobj, current_offset);
   1357       1.8       mrg 
   1358       1.8       mrg 			/* to be useful must get a non-busy, non-released pg */
   1359       1.8       mrg 			if (ptmp == NULL ||
   1360       1.8       mrg 			    (ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
   1361       1.8       mrg 				if (lcv == centeridx || (flags & PGO_ALLPAGES)
   1362       1.8       mrg 				    != 0)
   1363       1.8       mrg 				done = FALSE;	/* need to do a wait or I/O! */
   1364       1.8       mrg 				continue;
   1365       1.8       mrg 			}
   1366       1.8       mrg 
   1367       1.8       mrg 			/*
   1368       1.8       mrg 			 * useful page: busy/lock it and plug it in our
   1369       1.8       mrg 			 * result array
   1370       1.8       mrg 			 */
   1371       1.8       mrg 			ptmp->flags |= PG_BUSY;		/* loan up to caller */
   1372       1.8       mrg 			UVM_PAGE_OWN(ptmp, "uvn_get1");
   1373       1.8       mrg 			pps[lcv] = ptmp;
   1374       1.8       mrg 			gotpages++;
   1375       1.8       mrg 
   1376       1.8       mrg 		}	/* "for" lcv loop */
   1377       1.8       mrg 
   1378       1.8       mrg 		/*
   1379       1.8       mrg 		 * XXX: given the "advice", should we consider async read-ahead?
   1380       1.8       mrg 		 * XXX: fault current does deactive of pages behind us.  is
   1381       1.8       mrg 		 * this good (other callers might now).
   1382       1.8       mrg 		 */
   1383       1.8       mrg 		/*
   1384       1.8       mrg 		 * XXX: read-ahead currently handled by buffer cache (bread)
   1385       1.8       mrg 		 * level.
   1386       1.8       mrg 		 * XXX: no async i/o available.
   1387       1.8       mrg 		 * XXX: so we don't do anything now.
   1388       1.8       mrg 		 */
   1389       1.8       mrg 
   1390       1.8       mrg 		/*
   1391       1.8       mrg 		 * step 1c: now we've either done everything needed or we to
   1392       1.8       mrg 		 * unlock and do some waiting or I/O.
   1393       1.8       mrg 		 */
   1394       1.8       mrg 
   1395       1.8       mrg 		*npagesp = gotpages;		/* let caller know */
   1396       1.8       mrg 		if (done)
   1397       1.8       mrg 			return(VM_PAGER_OK);		/* bingo! */
   1398       1.8       mrg 		else
   1399       1.8       mrg 			/* EEK!   Need to unlock and I/O */
   1400       1.8       mrg 			return(VM_PAGER_UNLOCK);
   1401       1.8       mrg 	}
   1402       1.8       mrg 
   1403       1.8       mrg 	/*
   1404       1.8       mrg 	 * step 2: get non-resident or busy pages.
   1405       1.8       mrg 	 * object is locked.   data structures are unlocked.
   1406       1.8       mrg 	 *
   1407       1.8       mrg 	 * XXX: because we can't do async I/O at this level we get things
   1408       1.8       mrg 	 * page at a time (otherwise we'd chunk).   the VOP_READ() will do
   1409       1.8       mrg 	 * async-read-ahead for us at a lower level.
   1410       1.8       mrg 	 */
   1411       1.1       mrg 
   1412       1.8       mrg 	for (lcv = 0, current_offset = offset ;
   1413       1.8       mrg 			 lcv < *npagesp ; lcv++, current_offset += PAGE_SIZE) {
   1414       1.8       mrg 
   1415       1.8       mrg 		/* skip over pages we've already gotten or don't want */
   1416       1.8       mrg 		/* skip over pages we don't _have_ to get */
   1417       1.8       mrg 		if (pps[lcv] != NULL || (lcv != centeridx &&
   1418       1.8       mrg 		    (flags & PGO_ALLPAGES) == 0))
   1419       1.8       mrg 			continue;
   1420       1.8       mrg 
   1421       1.8       mrg 		/*
   1422       1.8       mrg 		 * we have yet to locate the current page (pps[lcv]).   we first
   1423       1.8       mrg 		 * look for a page that is already at the current offset.   if
   1424       1.8       mrg 		 * we fine a page, we check to see if it is busy or released.
   1425       1.8       mrg 		 * if that is the case, then we sleep on the page until it is
   1426       1.8       mrg 		 * no longer busy or released and repeat the lookup.    if the
   1427       1.8       mrg 		 * page we found is neither busy nor released, then we busy it
   1428       1.8       mrg 		 * (so we own it) and plug it into pps[lcv].   this breaks the
   1429       1.8       mrg 		 * following while loop and indicates we are ready to move on
   1430       1.8       mrg 		 * to the next page in the "lcv" loop above.
   1431       1.8       mrg 		 *
   1432       1.8       mrg 		 * if we exit the while loop with pps[lcv] still set to NULL,
   1433       1.8       mrg 		 * then it means that we allocated a new busy/fake/clean page
   1434       1.8       mrg 		 * ptmp in the object and we need to do I/O to fill in the data.
   1435       1.8       mrg 		 */
   1436       1.8       mrg 
   1437       1.8       mrg 		while (pps[lcv] == NULL) {	/* top of "pps" while loop */
   1438       1.8       mrg 
   1439       1.8       mrg 			/* look for a current page */
   1440       1.8       mrg 			ptmp = uvm_pagelookup(uobj, current_offset);
   1441       1.8       mrg 
   1442       1.8       mrg 			/* nope?   allocate one now (if we can) */
   1443       1.8       mrg 			if (ptmp == NULL) {
   1444       1.8       mrg 
   1445       1.8       mrg 				ptmp = uvm_pagealloc(uobj, current_offset,
   1446      1.23       chs 				    NULL, 0);
   1447       1.8       mrg 
   1448       1.8       mrg 				/* out of RAM? */
   1449       1.8       mrg 				if (ptmp == NULL) {
   1450       1.8       mrg 					simple_unlock(&uobj->vmobjlock);
   1451       1.8       mrg 					uvm_wait("uvn_getpage");
   1452       1.8       mrg 					simple_lock(&uobj->vmobjlock);
   1453       1.8       mrg 
   1454       1.8       mrg 					/* goto top of pps while loop */
   1455       1.8       mrg 					continue;
   1456       1.8       mrg 				}
   1457       1.8       mrg 
   1458       1.8       mrg 				/*
   1459       1.8       mrg 				 * got new page ready for I/O.  break pps
   1460       1.8       mrg 				 * while loop.  pps[lcv] is still NULL.
   1461       1.8       mrg 				 */
   1462       1.8       mrg 				break;
   1463       1.8       mrg 			}
   1464       1.8       mrg 
   1465       1.8       mrg 			/* page is there, see if we need to wait on it */
   1466       1.8       mrg 			if ((ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
   1467       1.8       mrg 				ptmp->flags |= PG_WANTED;
   1468       1.8       mrg 				UVM_UNLOCK_AND_WAIT(ptmp,
   1469      1.24   thorpej 				    &uobj->vmobjlock, FALSE, "uvn_get",0);
   1470       1.8       mrg 				simple_lock(&uobj->vmobjlock);
   1471       1.8       mrg 				continue;	/* goto top of pps while loop */
   1472       1.8       mrg 			}
   1473       1.8       mrg 
   1474       1.8       mrg 			/*
   1475       1.8       mrg 			 * if we get here then the page has become resident
   1476       1.8       mrg 			 * and unbusy between steps 1 and 2.  we busy it
   1477       1.8       mrg 			 * now (so we own it) and set pps[lcv] (so that we
   1478       1.8       mrg 			 * exit the while loop).
   1479       1.8       mrg 			 */
   1480       1.8       mrg 			ptmp->flags |= PG_BUSY;
   1481       1.8       mrg 			UVM_PAGE_OWN(ptmp, "uvn_get2");
   1482       1.8       mrg 			pps[lcv] = ptmp;
   1483       1.8       mrg 		}
   1484       1.8       mrg 
   1485       1.8       mrg 		/*
   1486       1.8       mrg 		 * if we own the a valid page at the correct offset, pps[lcv]
   1487       1.8       mrg 		 * will point to it.   nothing more to do except go to the
   1488       1.8       mrg 		 * next page.
   1489       1.8       mrg 		 */
   1490       1.8       mrg 
   1491       1.8       mrg 		if (pps[lcv])
   1492       1.8       mrg 			continue;			/* next lcv */
   1493       1.8       mrg 
   1494       1.8       mrg 		/*
   1495       1.8       mrg 		 * we have a "fake/busy/clean" page that we just allocated.  do
   1496       1.8       mrg 		 * I/O to fill it with valid data.  note that object must be
   1497       1.8       mrg 		 * locked going into uvn_io, but will be unlocked afterwards.
   1498       1.8       mrg 		 */
   1499       1.8       mrg 
   1500       1.8       mrg 		result = uvn_io((struct uvm_vnode *) uobj, &ptmp, 1,
   1501       1.8       mrg 		    PGO_SYNCIO, UIO_READ);
   1502       1.8       mrg 
   1503       1.8       mrg 		/*
   1504       1.8       mrg 		 * I/O done.   object is unlocked (by uvn_io).   because we used
   1505       1.8       mrg 		 * syncio the result can not be PEND or AGAIN.   we must relock
   1506       1.8       mrg 		 * and check for errors.
   1507       1.8       mrg 		 */
   1508       1.8       mrg 
   1509       1.8       mrg 		/* lock object.   check for errors.   */
   1510       1.8       mrg 		simple_lock(&uobj->vmobjlock);
   1511       1.8       mrg 		if (result != VM_PAGER_OK) {
   1512       1.8       mrg 			if (ptmp->flags & PG_WANTED)
   1513       1.8       mrg 				/* object lock still held */
   1514      1.25   thorpej 				wakeup(ptmp);
   1515       1.8       mrg 
   1516       1.8       mrg 			ptmp->flags &= ~(PG_WANTED|PG_BUSY);
   1517       1.8       mrg 			UVM_PAGE_OWN(ptmp, NULL);
   1518       1.8       mrg 			uvm_lock_pageq();
   1519       1.8       mrg 			uvm_pagefree(ptmp);
   1520       1.8       mrg 			uvm_unlock_pageq();
   1521       1.8       mrg 			simple_unlock(&uobj->vmobjlock);
   1522       1.8       mrg 			return(result);
   1523       1.8       mrg 		}
   1524       1.8       mrg 
   1525       1.8       mrg 		/*
   1526       1.8       mrg 		 * we got the page!   clear the fake flag (indicates valid
   1527       1.8       mrg 		 * data now in page) and plug into our result array.   note
   1528       1.8       mrg 		 * that page is still busy.
   1529       1.8       mrg 		 *
   1530       1.8       mrg 		 * it is the callers job to:
   1531       1.8       mrg 		 * => check if the page is released
   1532       1.8       mrg 		 * => unbusy the page
   1533       1.8       mrg 		 * => activate the page
   1534       1.8       mrg 		 */
   1535       1.8       mrg 
   1536       1.8       mrg 		ptmp->flags &= ~PG_FAKE;		/* data is valid ... */
   1537      1.26       chs 		pmap_clear_modify(ptmp);		/* ... and clean */
   1538       1.8       mrg 		pps[lcv] = ptmp;
   1539       1.8       mrg 
   1540       1.8       mrg 	}	/* lcv loop */
   1541       1.8       mrg 
   1542       1.8       mrg 	/*
   1543       1.8       mrg 	 * finally, unlock object and return.
   1544       1.8       mrg 	 */
   1545       1.1       mrg 
   1546       1.8       mrg 	simple_unlock(&uobj->vmobjlock);
   1547       1.8       mrg 	return (VM_PAGER_OK);
   1548       1.1       mrg }
   1549       1.1       mrg 
   1550       1.1       mrg /*
   1551       1.1       mrg  * uvn_asyncget: start async I/O to bring pages into ram
   1552       1.1       mrg  *
   1553       1.1       mrg  * => caller must lock object(???XXX: see if this is best)
   1554       1.1       mrg  * => could be called from uvn_get or a madvise() fault-ahead.
   1555       1.1       mrg  * => if it fails, it doesn't matter.
   1556       1.1       mrg  */
   1557       1.1       mrg 
   1558       1.8       mrg static int
   1559       1.8       mrg uvn_asyncget(uobj, offset, npages)
   1560       1.8       mrg 	struct uvm_object *uobj;
   1561  1.26.2.2    bouyer 	voff_t offset;
   1562       1.8       mrg 	int npages;
   1563       1.8       mrg {
   1564       1.1       mrg 
   1565       1.8       mrg 	/*
   1566       1.8       mrg 	 * XXXCDC: we can't do async I/O yet
   1567       1.8       mrg 	 */
   1568       1.8       mrg 	printf("uvn_asyncget called\n");
   1569       1.8       mrg 	return (KERN_SUCCESS);
   1570       1.1       mrg }
   1571       1.1       mrg 
   1572       1.1       mrg /*
   1573       1.1       mrg  * uvn_io: do I/O to a vnode
   1574       1.1       mrg  *
   1575       1.1       mrg  * => prefer map unlocked (not required)
   1576       1.1       mrg  * => object must be locked!   we will _unlock_ it before starting I/O.
   1577       1.1       mrg  * => flags: PGO_SYNCIO -- use sync. I/O
   1578       1.1       mrg  * => XXX: currently we use VOP_READ/VOP_WRITE which are only sync.
   1579       1.1       mrg  *	[thus we never do async i/o!  see iodone comment]
   1580       1.1       mrg  */
   1581       1.1       mrg 
   1582       1.8       mrg static int
   1583       1.8       mrg uvn_io(uvn, pps, npages, flags, rw)
   1584       1.8       mrg 	struct uvm_vnode *uvn;
   1585       1.8       mrg 	vm_page_t *pps;
   1586       1.8       mrg 	int npages, flags, rw;
   1587       1.8       mrg {
   1588       1.8       mrg 	struct vnode *vn;
   1589       1.8       mrg 	struct uio uio;
   1590       1.8       mrg 	struct iovec iov;
   1591  1.26.2.2    bouyer 	vaddr_t kva;
   1592  1.26.2.2    bouyer 	off_t file_offset;
   1593  1.26.2.2    bouyer 	int waitf, result, mapinflags;
   1594  1.26.2.2    bouyer 	size_t got, wanted;
   1595       1.8       mrg 	UVMHIST_FUNC("uvn_io"); UVMHIST_CALLED(maphist);
   1596       1.1       mrg 
   1597       1.8       mrg 	UVMHIST_LOG(maphist, "rw=%d", rw,0,0,0);
   1598       1.8       mrg 
   1599       1.8       mrg 	/*
   1600       1.8       mrg 	 * init values
   1601       1.8       mrg 	 */
   1602       1.1       mrg 
   1603       1.8       mrg 	waitf = (flags & PGO_SYNCIO) ? M_WAITOK : M_NOWAIT;
   1604       1.8       mrg 	vn = (struct vnode *) uvn;
   1605       1.8       mrg 	file_offset = pps[0]->offset;
   1606       1.8       mrg 
   1607       1.8       mrg 	/*
   1608       1.8       mrg 	 * check for sync'ing I/O.
   1609       1.8       mrg 	 */
   1610       1.8       mrg 
   1611       1.8       mrg 	while (uvn->u_flags & UVM_VNODE_IOSYNC) {
   1612       1.8       mrg 		if (waitf == M_NOWAIT) {
   1613       1.8       mrg 			simple_unlock(&uvn->u_obj.vmobjlock);
   1614       1.8       mrg 			UVMHIST_LOG(maphist,"<- try again (iosync)",0,0,0,0);
   1615       1.8       mrg 			return(VM_PAGER_AGAIN);
   1616       1.8       mrg 		}
   1617       1.8       mrg 		uvn->u_flags |= UVM_VNODE_IOSYNCWANTED;
   1618       1.8       mrg 		UVM_UNLOCK_AND_WAIT(&uvn->u_flags, &uvn->u_obj.vmobjlock,
   1619       1.1       mrg 			FALSE, "uvn_iosync",0);
   1620       1.8       mrg 		simple_lock(&uvn->u_obj.vmobjlock);
   1621       1.8       mrg 	}
   1622       1.1       mrg 
   1623       1.8       mrg 	/*
   1624       1.8       mrg 	 * check size
   1625       1.8       mrg 	 */
   1626       1.8       mrg 
   1627       1.8       mrg 	if (file_offset >= uvn->u_size) {
   1628       1.8       mrg 			simple_unlock(&uvn->u_obj.vmobjlock);
   1629       1.8       mrg 			UVMHIST_LOG(maphist,"<- BAD (size check)",0,0,0,0);
   1630       1.8       mrg 			return(VM_PAGER_BAD);
   1631       1.8       mrg 	}
   1632       1.8       mrg 
   1633       1.8       mrg 	/*
   1634       1.8       mrg 	 * first try and map the pages in (without waiting)
   1635       1.8       mrg 	 */
   1636       1.8       mrg 
   1637  1.26.2.2    bouyer 	mapinflags = (rw == UIO_READ) ?
   1638  1.26.2.2    bouyer 	    UVMPAGER_MAPIN_READ : UVMPAGER_MAPIN_WRITE;
   1639  1.26.2.2    bouyer 
   1640  1.26.2.2    bouyer 	kva = uvm_pagermapin(pps, npages, NULL, mapinflags);
   1641  1.26.2.2    bouyer 	if (kva == 0 && waitf == M_NOWAIT) {
   1642       1.8       mrg 		simple_unlock(&uvn->u_obj.vmobjlock);
   1643       1.8       mrg 		UVMHIST_LOG(maphist,"<- mapin failed (try again)",0,0,0,0);
   1644       1.8       mrg 		return(VM_PAGER_AGAIN);
   1645       1.8       mrg 	}
   1646       1.8       mrg 
   1647       1.8       mrg 	/*
   1648       1.8       mrg 	 * ok, now bump u_nio up.   at this point we are done with uvn
   1649       1.8       mrg 	 * and can unlock it.   if we still don't have a kva, try again
   1650       1.8       mrg 	 * (this time with sleep ok).
   1651       1.8       mrg 	 */
   1652       1.8       mrg 
   1653       1.8       mrg 	uvn->u_nio++;			/* we have an I/O in progress! */
   1654       1.8       mrg 	simple_unlock(&uvn->u_obj.vmobjlock);
   1655       1.8       mrg 	/* NOTE: object now unlocked */
   1656  1.26.2.2    bouyer 	if (kva == 0)
   1657  1.26.2.2    bouyer 		kva = uvm_pagermapin(pps, npages, NULL,
   1658  1.26.2.2    bouyer 		    mapinflags | UVMPAGER_MAPIN_WAITOK);
   1659       1.8       mrg 
   1660       1.8       mrg 	/*
   1661       1.8       mrg 	 * ok, mapped in.  our pages are PG_BUSY so they are not going to
   1662       1.8       mrg 	 * get touched (so we can look at "offset" without having to lock
   1663       1.8       mrg 	 * the object).  set up for I/O.
   1664       1.8       mrg 	 */
   1665       1.8       mrg 
   1666       1.8       mrg 	/*
   1667       1.8       mrg 	 * fill out uio/iov
   1668       1.8       mrg 	 */
   1669       1.8       mrg 
   1670       1.8       mrg 	iov.iov_base = (caddr_t) kva;
   1671      1.16       chs 	wanted = npages << PAGE_SHIFT;
   1672       1.8       mrg 	if (file_offset + wanted > uvn->u_size)
   1673       1.8       mrg 		wanted = uvn->u_size - file_offset;	/* XXX: needed? */
   1674       1.8       mrg 	iov.iov_len = wanted;
   1675       1.8       mrg 	uio.uio_iov = &iov;
   1676       1.8       mrg 	uio.uio_iovcnt = 1;
   1677       1.8       mrg 	uio.uio_offset = file_offset;
   1678       1.8       mrg 	uio.uio_segflg = UIO_SYSSPACE;
   1679       1.8       mrg 	uio.uio_rw = rw;
   1680       1.8       mrg 	uio.uio_resid = wanted;
   1681       1.8       mrg 	uio.uio_procp = NULL;
   1682       1.1       mrg 
   1683       1.8       mrg 	/*
   1684       1.8       mrg 	 * do the I/O!  (XXX: curproc?)
   1685       1.8       mrg 	 */
   1686       1.8       mrg 
   1687       1.8       mrg 	UVMHIST_LOG(maphist, "calling VOP",0,0,0,0);
   1688       1.8       mrg 
   1689      1.21  sommerfe 	/*
   1690      1.21  sommerfe 	 * This process may already have this vnode locked, if we faulted in
   1691      1.21  sommerfe 	 * copyin() or copyout() on a region backed by this vnode
   1692      1.21  sommerfe 	 * while doing I/O to the vnode.  If this is the case, don't
   1693      1.21  sommerfe 	 * panic.. instead, return the error to the user.
   1694      1.21  sommerfe 	 *
   1695      1.21  sommerfe 	 * XXX this is a stopgap to prevent a panic.
   1696      1.21  sommerfe 	 * Ideally, this kind of operation *should* work.
   1697      1.21  sommerfe 	 */
   1698      1.21  sommerfe 	result = 0;
   1699       1.8       mrg 	if ((uvn->u_flags & UVM_VNODE_VNISLOCKED) == 0)
   1700      1.21  sommerfe 		result = vn_lock(vn, LK_EXCLUSIVE | LK_RETRY | LK_RECURSEFAIL);
   1701      1.21  sommerfe 
   1702      1.21  sommerfe 	if (result == 0) {
   1703      1.21  sommerfe 		/* NOTE: vnode now locked! */
   1704       1.8       mrg 
   1705      1.21  sommerfe 		if (rw == UIO_READ)
   1706      1.21  sommerfe 			result = VOP_READ(vn, &uio, 0, curproc->p_ucred);
   1707      1.21  sommerfe 		else
   1708      1.21  sommerfe 			result = VOP_WRITE(vn, &uio, 0, curproc->p_ucred);
   1709       1.8       mrg 
   1710      1.21  sommerfe 		if ((uvn->u_flags & UVM_VNODE_VNISLOCKED) == 0)
   1711      1.21  sommerfe 			VOP_UNLOCK(vn, 0);
   1712      1.21  sommerfe 	}
   1713      1.21  sommerfe 
   1714       1.8       mrg 	/* NOTE: vnode now unlocked (unless vnislocked) */
   1715       1.8       mrg 
   1716       1.8       mrg 	UVMHIST_LOG(maphist, "done calling VOP",0,0,0,0);
   1717       1.8       mrg 
   1718       1.8       mrg 	/*
   1719       1.8       mrg 	 * result == unix style errno (0 == OK!)
   1720       1.8       mrg 	 *
   1721       1.8       mrg 	 * zero out rest of buffer (if needed)
   1722       1.8       mrg 	 */
   1723       1.8       mrg 
   1724       1.8       mrg 	if (result == 0) {
   1725       1.8       mrg 		got = wanted - uio.uio_resid;
   1726       1.8       mrg 
   1727       1.8       mrg 		if (wanted && got == 0) {
   1728       1.8       mrg 			result = EIO;		/* XXX: error? */
   1729       1.8       mrg 		} else if (got < PAGE_SIZE * npages && rw == UIO_READ) {
   1730      1.16       chs 			memset((void *) (kva + got), 0,
   1731      1.16       chs 			       (npages << PAGE_SHIFT) - got);
   1732       1.8       mrg 		}
   1733       1.8       mrg 	}
   1734       1.8       mrg 
   1735       1.8       mrg 	/*
   1736       1.8       mrg 	 * now remove pager mapping
   1737       1.8       mrg 	 */
   1738       1.8       mrg 	uvm_pagermapout(kva, npages);
   1739       1.8       mrg 
   1740       1.8       mrg 	/*
   1741       1.8       mrg 	 * now clean up the object (i.e. drop I/O count)
   1742       1.8       mrg 	 */
   1743       1.8       mrg 
   1744       1.8       mrg 	simple_lock(&uvn->u_obj.vmobjlock);
   1745       1.8       mrg 	/* NOTE: object now locked! */
   1746       1.8       mrg 
   1747       1.8       mrg 	uvn->u_nio--;			/* I/O DONE! */
   1748       1.8       mrg 	if ((uvn->u_flags & UVM_VNODE_IOSYNC) != 0 && uvn->u_nio == 0) {
   1749       1.8       mrg 		wakeup(&uvn->u_nio);
   1750       1.8       mrg 	}
   1751       1.8       mrg 	simple_unlock(&uvn->u_obj.vmobjlock);
   1752       1.8       mrg 	/* NOTE: object now unlocked! */
   1753       1.8       mrg 
   1754       1.8       mrg 	/*
   1755       1.8       mrg 	 * done!
   1756       1.8       mrg 	 */
   1757       1.8       mrg 
   1758       1.8       mrg 	UVMHIST_LOG(maphist, "<- done (result %d)", result,0,0,0);
   1759       1.8       mrg 	if (result == 0)
   1760       1.8       mrg 		return(VM_PAGER_OK);
   1761       1.8       mrg 	else
   1762       1.8       mrg 		return(VM_PAGER_ERROR);
   1763       1.1       mrg }
   1764       1.1       mrg 
   1765       1.1       mrg /*
   1766       1.1       mrg  * uvm_vnp_uncache: disable "persisting" in a vnode... when last reference
   1767       1.1       mrg  * is gone we will kill the object (flushing dirty pages back to the vnode
   1768       1.1       mrg  * if needed).
   1769       1.1       mrg  *
   1770       1.1       mrg  * => returns TRUE if there was no uvm_object attached or if there was
   1771       1.1       mrg  *	one and we killed it [i.e. if there is no active uvn]
   1772       1.1       mrg  * => called with the vnode VOP_LOCK'd [we will unlock it for I/O, if
   1773       1.1       mrg  *	needed]
   1774       1.1       mrg  *
   1775       1.1       mrg  * => XXX: given that we now kill uvn's when a vnode is recycled (without
   1776       1.1       mrg  *	having to hold a reference on the vnode) and given a working
   1777       1.1       mrg  *	uvm_vnp_sync(), how does that effect the need for this function?
   1778       1.1       mrg  *      [XXXCDC: seems like it can die?]
   1779       1.1       mrg  *
   1780       1.1       mrg  * => XXX: this function should DIE once we merge the VM and buffer
   1781       1.1       mrg  *	cache.
   1782       1.1       mrg  *
   1783       1.1       mrg  * research shows that this is called in the following places:
   1784       1.1       mrg  * ext2fs_truncate, ffs_truncate, detrunc[msdosfs]: called when vnode
   1785       1.1       mrg  *	changes sizes
   1786       1.1       mrg  * ext2fs_write, WRITE [ufs_readwrite], msdosfs_write: called when we
   1787       1.1       mrg  *	are written to
   1788       1.1       mrg  * ex2fs_chmod, ufs_chmod: called if VTEXT vnode and the sticky bit
   1789       1.1       mrg  *	is off
   1790       1.1       mrg  * ffs_realloccg: when we can't extend the current block and have
   1791       1.1       mrg  *	to allocate a new one we call this [XXX: why?]
   1792       1.1       mrg  * nfsrv_rename, rename_files: called when the target filename is there
   1793       1.1       mrg  *	and we want to remove it
   1794       1.1       mrg  * nfsrv_remove, sys_unlink: called on file we are removing
   1795       1.1       mrg  * nfsrv_access: if VTEXT and we want WRITE access and we don't uncache
   1796       1.1       mrg  *	then return "text busy"
   1797       1.1       mrg  * nfs_open: seems to uncache any file opened with nfs
   1798       1.1       mrg  * vn_writechk: if VTEXT vnode and can't uncache return "text busy"
   1799       1.1       mrg  */
   1800       1.1       mrg 
   1801       1.8       mrg boolean_t
   1802       1.8       mrg uvm_vnp_uncache(vp)
   1803       1.8       mrg 	struct vnode *vp;
   1804       1.8       mrg {
   1805       1.8       mrg 	struct uvm_vnode *uvn = &vp->v_uvm;
   1806       1.1       mrg 
   1807       1.8       mrg 	/*
   1808       1.8       mrg 	 * lock uvn part of the vnode and check to see if we need to do anything
   1809       1.8       mrg 	 */
   1810       1.1       mrg 
   1811       1.8       mrg 	simple_lock(&uvn->u_obj.vmobjlock);
   1812       1.8       mrg 	if ((uvn->u_flags & UVM_VNODE_VALID) == 0 ||
   1813       1.8       mrg 			(uvn->u_flags & UVM_VNODE_BLOCKED) != 0) {
   1814       1.8       mrg 		simple_unlock(&uvn->u_obj.vmobjlock);
   1815       1.8       mrg 		return(TRUE);
   1816       1.8       mrg 	}
   1817       1.8       mrg 
   1818       1.8       mrg 	/*
   1819       1.8       mrg 	 * we have a valid, non-blocked uvn.   clear persist flag.
   1820       1.8       mrg 	 * if uvn is currently active we can return now.
   1821       1.8       mrg 	 */
   1822       1.8       mrg 
   1823       1.8       mrg 	uvn->u_flags &= ~UVM_VNODE_CANPERSIST;
   1824       1.8       mrg 	if (uvn->u_obj.uo_refs) {
   1825       1.8       mrg 		simple_unlock(&uvn->u_obj.vmobjlock);
   1826       1.8       mrg 		return(FALSE);
   1827       1.8       mrg 	}
   1828       1.8       mrg 
   1829       1.8       mrg 	/*
   1830       1.8       mrg 	 * uvn is currently persisting!   we have to gain a reference to
   1831       1.8       mrg 	 * it so that we can call uvn_detach to kill the uvn.
   1832       1.8       mrg 	 */
   1833       1.1       mrg 
   1834       1.8       mrg 	VREF(vp);			/* seems ok, even with VOP_LOCK */
   1835       1.8       mrg 	uvn->u_obj.uo_refs++;		/* value is now 1 */
   1836       1.8       mrg 	simple_unlock(&uvn->u_obj.vmobjlock);
   1837       1.1       mrg 
   1838       1.1       mrg 
   1839       1.1       mrg #ifdef DEBUG
   1840       1.8       mrg 	/*
   1841       1.8       mrg 	 * carry over sanity check from old vnode pager: the vnode should
   1842       1.8       mrg 	 * be VOP_LOCK'd, and we confirm it here.
   1843       1.8       mrg 	 */
   1844       1.8       mrg 	if (!VOP_ISLOCKED(vp)) {
   1845       1.8       mrg 		boolean_t is_ok_anyway = FALSE;
   1846       1.1       mrg #ifdef NFS
   1847       1.8       mrg 		extern int (**nfsv2_vnodeop_p) __P((void *));
   1848       1.8       mrg 		extern int (**spec_nfsv2nodeop_p) __P((void *));
   1849       1.8       mrg 		extern int (**fifo_nfsv2nodeop_p) __P((void *));
   1850       1.1       mrg 
   1851       1.8       mrg 		/* vnode is NOT VOP_LOCKed: some vnode types _never_ lock */
   1852       1.8       mrg 		if (vp->v_op == nfsv2_vnodeop_p ||
   1853       1.8       mrg 		    vp->v_op == spec_nfsv2nodeop_p) {
   1854       1.8       mrg 			is_ok_anyway = TRUE;
   1855       1.8       mrg 		}
   1856       1.8       mrg 		if (vp->v_op == fifo_nfsv2nodeop_p) {
   1857       1.8       mrg 			is_ok_anyway = TRUE;
   1858       1.8       mrg 		}
   1859       1.1       mrg #endif	/* NFS */
   1860       1.8       mrg 		if (!is_ok_anyway)
   1861       1.8       mrg 			panic("uvm_vnp_uncache: vnode not locked!");
   1862       1.8       mrg 	}
   1863       1.1       mrg #endif	/* DEBUG */
   1864       1.1       mrg 
   1865       1.8       mrg 	/*
   1866       1.8       mrg 	 * now drop our reference to the vnode.   if we have the sole
   1867       1.8       mrg 	 * reference to the vnode then this will cause it to die [as we
   1868       1.8       mrg 	 * just cleared the persist flag].   we have to unlock the vnode
   1869       1.8       mrg 	 * while we are doing this as it may trigger I/O.
   1870       1.8       mrg 	 *
   1871       1.8       mrg 	 * XXX: it might be possible for uvn to get reclaimed while we are
   1872       1.8       mrg 	 * unlocked causing us to return TRUE when we should not.   we ignore
   1873       1.8       mrg 	 * this as a false-positive return value doesn't hurt us.
   1874       1.8       mrg 	 */
   1875       1.8       mrg 	VOP_UNLOCK(vp, 0);
   1876       1.8       mrg 	uvn_detach(&uvn->u_obj);
   1877       1.8       mrg 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1878       1.8       mrg 
   1879       1.8       mrg 	/*
   1880       1.8       mrg 	 * and return...
   1881       1.8       mrg 	 */
   1882       1.8       mrg 
   1883       1.8       mrg 	return(TRUE);
   1884       1.1       mrg }
   1885       1.1       mrg 
   1886       1.1       mrg /*
   1887       1.1       mrg  * uvm_vnp_setsize: grow or shrink a vnode uvn
   1888       1.1       mrg  *
   1889       1.1       mrg  * grow   => just update size value
   1890       1.1       mrg  * shrink => toss un-needed pages
   1891       1.1       mrg  *
   1892       1.1       mrg  * => we assume that the caller has a reference of some sort to the
   1893       1.1       mrg  *	vnode in question so that it will not be yanked out from under
   1894       1.1       mrg  *	us.
   1895       1.1       mrg  *
   1896       1.1       mrg  * called from:
   1897       1.1       mrg  *  => truncate fns (ext2fs_truncate, ffs_truncate, detrunc[msdos])
   1898       1.1       mrg  *  => "write" fns (ext2fs_write, WRITE [ufs/ufs], msdosfs_write, nfs_write)
   1899       1.1       mrg  *  => ffs_balloc [XXX: why? doesn't WRITE handle?]
   1900       1.1       mrg  *  => NFS: nfs_loadattrcache, nfs_getattrcache, nfs_setattr
   1901       1.1       mrg  *  => union fs: union_newsize
   1902       1.1       mrg  */
   1903       1.1       mrg 
   1904       1.8       mrg void
   1905       1.8       mrg uvm_vnp_setsize(vp, newsize)
   1906       1.8       mrg 	struct vnode *vp;
   1907  1.26.2.2    bouyer 	voff_t newsize;
   1908       1.8       mrg {
   1909       1.8       mrg 	struct uvm_vnode *uvn = &vp->v_uvm;
   1910       1.1       mrg 
   1911       1.8       mrg 	/*
   1912       1.8       mrg 	 * lock uvn and check for valid object, and if valid: do it!
   1913       1.8       mrg 	 */
   1914       1.8       mrg 	simple_lock(&uvn->u_obj.vmobjlock);
   1915       1.8       mrg 	if (uvn->u_flags & UVM_VNODE_VALID) {
   1916       1.1       mrg 
   1917       1.8       mrg 		/*
   1918       1.8       mrg 		 * now check if the size has changed: if we shrink we had better
   1919       1.8       mrg 		 * toss some pages...
   1920       1.8       mrg 		 */
   1921       1.8       mrg 
   1922       1.8       mrg 		if (uvn->u_size > newsize) {
   1923  1.26.2.2    bouyer 			(void)uvn_flush(&uvn->u_obj, newsize,
   1924       1.8       mrg 			    uvn->u_size, PGO_FREE);
   1925       1.8       mrg 		}
   1926  1.26.2.2    bouyer 		uvn->u_size = newsize;
   1927       1.8       mrg 	}
   1928       1.8       mrg 	simple_unlock(&uvn->u_obj.vmobjlock);
   1929       1.1       mrg 
   1930       1.8       mrg 	/*
   1931       1.8       mrg 	 * done
   1932       1.8       mrg 	 */
   1933       1.8       mrg 	return;
   1934       1.1       mrg }
   1935       1.1       mrg 
   1936       1.1       mrg /*
   1937       1.1       mrg  * uvm_vnp_sync: flush all dirty VM pages back to their backing vnodes.
   1938       1.1       mrg  *
   1939       1.1       mrg  * => called from sys_sync with no VM structures locked
   1940       1.1       mrg  * => only one process can do a sync at a time (because the uvn
   1941       1.1       mrg  *    structure only has one queue for sync'ing).  we ensure this
   1942       1.1       mrg  *    by holding the uvn_sync_lock while the sync is in progress.
   1943       1.1       mrg  *    other processes attempting a sync will sleep on this lock
   1944       1.1       mrg  *    until we are done.
   1945       1.1       mrg  */
   1946       1.1       mrg 
   1947       1.8       mrg void
   1948       1.8       mrg uvm_vnp_sync(mp)
   1949       1.8       mrg 	struct mount *mp;
   1950       1.8       mrg {
   1951       1.8       mrg 	struct uvm_vnode *uvn;
   1952       1.8       mrg 	struct vnode *vp;
   1953       1.8       mrg 	boolean_t got_lock;
   1954       1.8       mrg 
   1955       1.8       mrg 	/*
   1956       1.8       mrg 	 * step 1: ensure we are only ones using the uvn_sync_q by locking
   1957       1.8       mrg 	 * our lock...
   1958       1.8       mrg 	 */
   1959       1.8       mrg 	lockmgr(&uvn_sync_lock, LK_EXCLUSIVE, (void *)0);
   1960       1.8       mrg 
   1961       1.8       mrg 	/*
   1962       1.8       mrg 	 * step 2: build up a simpleq of uvns of interest based on the
   1963       1.8       mrg 	 * write list.   we gain a reference to uvns of interest.  must
   1964       1.8       mrg 	 * be careful about locking uvn's since we will be holding uvn_wl_lock
   1965       1.8       mrg 	 * in the body of the loop.
   1966       1.8       mrg 	 */
   1967       1.8       mrg 	SIMPLEQ_INIT(&uvn_sync_q);
   1968       1.8       mrg 	simple_lock(&uvn_wl_lock);
   1969       1.8       mrg 	for (uvn = uvn_wlist.lh_first ; uvn != NULL ;
   1970       1.8       mrg 	    uvn = uvn->u_wlist.le_next) {
   1971       1.1       mrg 
   1972       1.8       mrg 		vp = (struct vnode *) uvn;
   1973       1.8       mrg 		if (mp && vp->v_mount != mp)
   1974       1.8       mrg 			continue;
   1975       1.8       mrg 
   1976       1.8       mrg 		/* attempt to gain reference */
   1977       1.8       mrg 		while ((got_lock = simple_lock_try(&uvn->u_obj.vmobjlock)) ==
   1978       1.9     chuck 		    						FALSE &&
   1979       1.9     chuck 				(uvn->u_flags & UVM_VNODE_BLOCKED) == 0)
   1980       1.8       mrg 			/* spin */ ;
   1981       1.8       mrg 
   1982       1.8       mrg 		/*
   1983       1.9     chuck 		 * we will exit the loop if either if the following are true:
   1984       1.9     chuck 		 *  - we got the lock [always true if NCPU == 1]
   1985       1.9     chuck 		 *  - we failed to get the lock but noticed the vnode was
   1986       1.9     chuck 		 * 	"blocked" -- in this case the vnode must be a dying
   1987       1.9     chuck 		 *	vnode, and since dying vnodes are in the process of
   1988       1.9     chuck 		 *	being flushed out, we can safely skip this one
   1989       1.9     chuck 		 *
   1990       1.9     chuck 		 * we want to skip over the vnode if we did not get the lock,
   1991       1.9     chuck 		 * or if the vnode is already dying (due to the above logic).
   1992       1.8       mrg 		 *
   1993       1.8       mrg 		 * note that uvn must already be valid because we found it on
   1994       1.8       mrg 		 * the wlist (this also means it can't be ALOCK'd).
   1995       1.8       mrg 		 */
   1996       1.9     chuck 		if (!got_lock || (uvn->u_flags & UVM_VNODE_BLOCKED) != 0) {
   1997       1.9     chuck 			if (got_lock)
   1998       1.9     chuck 				simple_unlock(&uvn->u_obj.vmobjlock);
   1999       1.9     chuck 			continue;		/* skip it */
   2000       1.9     chuck 		}
   2001       1.8       mrg 
   2002       1.8       mrg 		/*
   2003       1.8       mrg 		 * gain reference.   watch out for persisting uvns (need to
   2004       1.8       mrg 		 * regain vnode REF).
   2005       1.8       mrg 		 */
   2006       1.8       mrg 		if (uvn->u_obj.uo_refs == 0)
   2007       1.8       mrg 			VREF(vp);
   2008       1.8       mrg 		uvn->u_obj.uo_refs++;
   2009       1.8       mrg 		simple_unlock(&uvn->u_obj.vmobjlock);
   2010       1.8       mrg 
   2011       1.8       mrg 		/*
   2012       1.8       mrg 		 * got it!
   2013       1.8       mrg 		 */
   2014       1.8       mrg 		SIMPLEQ_INSERT_HEAD(&uvn_sync_q, uvn, u_syncq);
   2015       1.8       mrg 	}
   2016       1.8       mrg 	simple_unlock(&uvn_wl_lock);
   2017       1.1       mrg 
   2018       1.8       mrg 	/*
   2019       1.8       mrg 	 * step 3: we now have a list of uvn's that may need cleaning.
   2020       1.8       mrg 	 * we are holding the uvn_sync_lock, but have dropped the uvn_wl_lock
   2021       1.8       mrg 	 * (so we can now safely lock uvn's again).
   2022       1.8       mrg 	 */
   2023       1.1       mrg 
   2024       1.8       mrg 	for (uvn = uvn_sync_q.sqh_first ; uvn ; uvn = uvn->u_syncq.sqe_next) {
   2025       1.8       mrg 		simple_lock(&uvn->u_obj.vmobjlock);
   2026  1.26.2.1   thorpej #ifdef DEBUG
   2027       1.8       mrg 		if (uvn->u_flags & UVM_VNODE_DYING) {
   2028       1.8       mrg 			printf("uvm_vnp_sync: dying vnode on sync list\n");
   2029       1.8       mrg 		}
   2030       1.1       mrg #endif
   2031       1.8       mrg 		uvn_flush(&uvn->u_obj, 0, 0,
   2032       1.8       mrg 		    PGO_CLEANIT|PGO_ALLPAGES|PGO_DOACTCLUST);
   2033       1.8       mrg 
   2034       1.8       mrg 		/*
   2035       1.8       mrg 		 * if we have the only reference and we just cleaned the uvn,
   2036       1.8       mrg 		 * then we can pull it out of the UVM_VNODE_WRITEABLE state
   2037       1.8       mrg 		 * thus allowing us to avoid thinking about flushing it again
   2038       1.8       mrg 		 * on later sync ops.
   2039       1.8       mrg 		 */
   2040       1.8       mrg 		if (uvn->u_obj.uo_refs == 1 &&
   2041       1.8       mrg 		    (uvn->u_flags & UVM_VNODE_WRITEABLE)) {
   2042       1.8       mrg 			LIST_REMOVE(uvn, u_wlist);
   2043       1.8       mrg 			uvn->u_flags &= ~UVM_VNODE_WRITEABLE;
   2044       1.8       mrg 		}
   2045       1.8       mrg 
   2046       1.8       mrg 		simple_unlock(&uvn->u_obj.vmobjlock);
   2047       1.1       mrg 
   2048       1.8       mrg 		/* now drop our reference to the uvn */
   2049       1.8       mrg 		uvn_detach(&uvn->u_obj);
   2050       1.8       mrg 	}
   2051       1.8       mrg 
   2052       1.8       mrg 	/*
   2053       1.8       mrg 	 * done!  release sync lock
   2054       1.8       mrg 	 */
   2055       1.8       mrg 	lockmgr(&uvn_sync_lock, LK_RELEASE, (void *)0);
   2056       1.1       mrg }
   2057