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