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