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