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