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