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