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