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