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