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