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