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