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