uvm_vnode.c revision 1.22.2.1.2.1 1 /* $NetBSD: uvm_vnode.c,v 1.22.2.1.2.1 1999/06/07 04:25:38 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 #include "opt_ddb.h"
51
52 /*
53 * uvm_vnode.c: the vnode pager.
54 */
55
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/kernel.h>
59 #include <sys/proc.h>
60 #include <sys/malloc.h>
61 #include <sys/vnode.h>
62 #include <sys/disklabel.h>
63 #include <sys/ioctl.h>
64 #include <sys/fcntl.h>
65 #include <sys/conf.h>
66 #include <sys/pool.h>
67
68 #include <miscfs/specfs/specdev.h>
69
70 #include <vm/vm.h>
71 #include <vm/vm_page.h>
72 #include <vm/vm_kern.h>
73
74 #include <uvm/uvm.h>
75 #include <uvm/uvm_vnode.h>
76
77 /*
78 * private global data structure
79 *
80 * we keep a list of writeable active vnode-backed VM objects for sync op.
81 * we keep a simpleq of vnodes that are currently being sync'd.
82 */
83
84 LIST_HEAD(uvn_list_struct, uvm_vnode);
85 static struct uvn_list_struct uvn_wlist; /* writeable uvns */
86 static simple_lock_data_t uvn_wl_lock; /* locks uvn_wlist */
87
88 SIMPLEQ_HEAD(uvn_sq_struct, uvm_vnode);
89 static struct uvn_sq_struct uvn_sync_q; /* sync'ing uvns */
90 lock_data_t uvn_sync_lock; /* locks sync operation */
91
92 /*
93 * functions
94 */
95
96 static int uvn_asyncget __P((struct uvm_object *, vaddr_t,
97 int));
98 struct uvm_object * uvn_attach __P((void *, vm_prot_t));
99 static void uvn_cluster __P((struct uvm_object *, vaddr_t,
100 vaddr_t *, vaddr_t *));
101 static void uvn_detach __P((struct uvm_object *));
102 static int uvn_findpage __P((struct uvm_object *, vaddr_t,
103 struct vm_page **, int));
104 static boolean_t uvn_flush __P((struct uvm_object *, vaddr_t,
105 vaddr_t, int));
106 static int uvn_get __P((struct uvm_object *, vaddr_t,
107 vm_page_t *, int *, int,
108 vm_prot_t, int, int));
109 static void uvn_init __P((void));
110 static int uvn_put __P((struct uvm_object *, vm_page_t *,
111 int, boolean_t));
112 static void uvn_reference __P((struct uvm_object *));
113 static boolean_t uvn_releasepg __P((struct vm_page *,
114 struct vm_page **));
115 static void uvn_doasyncget __P((struct vm_page **, size_t,
116 daddr_t));
117
118 /*
119 * master pager structure
120 */
121
122 struct uvm_pagerops uvm_vnodeops = {
123 uvn_init,
124 uvn_reference,
125 uvn_detach,
126 NULL, /* no specialized fault routine required */
127 uvn_flush,
128 uvn_get,
129 uvn_asyncget,
130 uvn_put,
131 uvn_cluster,
132 uvm_mk_pcluster, /* use generic version of this: see uvm_pager.c */
133 uvm_shareprot, /* !NULL: allow us in share maps */
134 NULL, /* AIO-DONE function (not until we have asyncio) */
135 uvn_releasepg,
136 };
137
138 /*
139 * the ops!
140 */
141
142 /*
143 * uvn_init
144 *
145 * init pager private data structures.
146 */
147
148 static void
149 uvn_init()
150 {
151
152 LIST_INIT(&uvn_wlist);
153 simple_lock_init(&uvn_wl_lock);
154 /* note: uvn_sync_q init'd in uvm_vnp_sync() */
155 lockinit(&uvn_sync_lock, PVM, "uvnsync", 0, 0);
156 }
157
158 /*
159 * uvn_attach
160 *
161 * attach a vnode structure to a VM object. if the vnode is already
162 * attached, then just bump the reference count by one and return the
163 * VM object. if not already attached, attach and return the new VM obj.
164 * the "accessprot" tells the max access the attaching thread wants to
165 * our pages.
166 *
167 * => caller must _not_ already be holding the lock on the uvm_object.
168 * => in fact, nothing should be locked so that we can sleep here.
169 * => note that uvm_object is first thing in vnode structure, so their
170 * pointers are equiv.
171 */
172
173 struct uvm_object *
174 uvn_attach(arg, accessprot)
175 void *arg;
176 vm_prot_t accessprot;
177 {
178 struct vnode *vp = arg;
179 struct uvm_vnode *uvn = &vp->v_uvm;
180 struct vattr vattr;
181 int result;
182 struct partinfo pi;
183 off_t used_vnode_size;
184 UVMHIST_FUNC("uvn_attach"); UVMHIST_CALLED(maphist);
185
186 UVMHIST_LOG(maphist, "(vn=0x%x)", arg,0,0,0);
187
188 used_vnode_size = (u_quad_t)0; /* XXX gcc -Wuninitialized */
189
190 /*
191 * first get a lock on the uvn.
192 */
193 simple_lock(&uvn->u_obj.vmobjlock);
194 while (uvn->u_flags & UVM_VNODE_BLOCKED) {
195 uvn->u_flags |= UVM_VNODE_WANTED;
196 UVMHIST_LOG(maphist, " SLEEPING on blocked vn",0,0,0,0);
197 UVM_UNLOCK_AND_WAIT(uvn, &uvn->u_obj.vmobjlock, FALSE,
198 "uvn_attach", 0);
199 simple_lock(&uvn->u_obj.vmobjlock);
200 UVMHIST_LOG(maphist," WOKE UP",0,0,0,0);
201 }
202
203 /*
204 * if we're mapping a BLK device, make sure it is a disk.
205 */
206 if (vp->v_type == VBLK && bdevsw[major(vp->v_rdev)].d_type != D_DISK) {
207 simple_unlock(&uvn->u_obj.vmobjlock);
208 UVMHIST_LOG(maphist,"<- done (VBLK not D_DISK!)", 0,0,0,0);
209 return(NULL);
210 }
211
212 /* check for new writeable uvn */
213 if ((accessprot & VM_PROT_WRITE) != 0 &&
214 (uvn->u_flags & UVM_VNODE_WRITEABLE) == 0) {
215 simple_lock(&uvn_wl_lock);
216 LIST_INSERT_HEAD(&uvn_wlist, uvn, u_wlist);
217 simple_unlock(&uvn_wl_lock);
218 /* we are now on wlist! */
219 uvn->u_flags |= UVM_VNODE_WRITEABLE;
220 }
221 #ifdef DIAGNOSTIC
222 if (vp->v_type != VREG) {
223 panic("uvn_attach: vp %p not VREG", vp);
224 }
225 #endif
226
227 /*
228 * set up our idea of the size
229 * if this hasn't been done already.
230 */
231 if (uvn->u_size == VSIZENOTSET) {
232
233 uvn->u_flags = UVM_VNODE_ALOCK;
234 simple_unlock(&uvn->u_obj.vmobjlock); /* drop lock in case we sleep */
235 /* XXX: curproc? */
236 if (vp->v_type == VBLK) {
237 /*
238 * We could implement this as a specfs getattr call, but:
239 *
240 * (1) VOP_GETATTR() would get the file system
241 * vnode operation, not the specfs operation.
242 *
243 * (2) All we want is the size, anyhow.
244 */
245 result = (*bdevsw[major(vp->v_rdev)].d_ioctl)(vp->v_rdev,
246 DIOCGPART, (caddr_t)&pi, FREAD, curproc);
247 if (result == 0) {
248 /* XXX should remember blocksize */
249 used_vnode_size = (u_quad_t)pi.disklab->d_secsize *
250 (u_quad_t)pi.part->p_size;
251 }
252 } else {
253 result = VOP_GETATTR(vp, &vattr, curproc->p_ucred, curproc);
254 if (result == 0)
255 used_vnode_size = vattr.va_size;
256 }
257
258
259 /*
260 * make sure that the newsize fits within a vaddr_t
261 * XXX: need to revise addressing data types
262 */
263 if (used_vnode_size > (vaddr_t) -PAGE_SIZE) {
264 #ifdef DEBUG
265 printf("uvn_attach: vn %p size truncated %qx->%x\n", vp,
266 (long long)used_vnode_size, -PAGE_SIZE);
267 #endif
268 used_vnode_size = (vaddr_t) -PAGE_SIZE;
269 }
270
271 /* relock object */
272 simple_lock(&uvn->u_obj.vmobjlock);
273
274 if (uvn->u_flags & UVM_VNODE_WANTED)
275 wakeup(uvn);
276 uvn->u_flags = 0;
277
278 if (result != 0) {
279 simple_unlock(&uvn->u_obj.vmobjlock); /* drop lock */
280 UVMHIST_LOG(maphist,"<- done (VOP_GETATTR FAILED!)", 0,0,0,0);
281 return(NULL);
282 }
283 uvn->u_size = used_vnode_size;
284
285 }
286
287 /* unlock and return */
288 simple_unlock(&uvn->u_obj.vmobjlock);
289 UVMHIST_LOG(maphist,"<- done, refcnt=%d", uvn->u_obj.uo_refs,
290 0, 0, 0);
291 return (&uvn->u_obj);
292 }
293
294
295 /*
296 * uvn_reference
297 *
298 * duplicate a reference to a VM object. Note that the reference
299 * count must already be at least one (the passed in reference) so
300 * there is no chance of the uvn being killed or locked out here.
301 *
302 * => caller must call with object unlocked.
303 * => caller must be using the same accessprot as was used at attach time
304 */
305
306
307 static void
308 uvn_reference(uobj)
309 struct uvm_object *uobj;
310 {
311 UVMHIST_FUNC("uvn_reference"); UVMHIST_CALLED(maphist);
312
313 VREF((struct vnode *)uobj);
314 }
315
316 /*
317 * uvn_detach
318 *
319 * remove a reference to a VM object.
320 *
321 * => caller must call with object unlocked and map locked.
322 * => this starts the detach process, but doesn't have to finish it
323 * (async i/o could still be pending).
324 */
325 static void
326 uvn_detach(uobj)
327 struct uvm_object *uobj;
328 {
329 UVMHIST_FUNC("uvn_detach"); UVMHIST_CALLED(maphist);
330 vrele((struct vnode *)uobj);
331 }
332
333 /*
334 * uvm_vnp_terminate: external hook to clear out a vnode's VM
335 *
336 * called in two cases:
337 * [1] when a persisting vnode vm object (i.e. one with a zero reference
338 * count) needs to be freed so that a vnode can be reused. this
339 * happens under "getnewvnode" in vfs_subr.c. if the vnode from
340 * the free list is still attached (i.e. not VBAD) then vgone is
341 * called. as part of the vgone trace this should get called to
342 * free the vm object. this is the common case.
343 * [2] when a filesystem is being unmounted by force (MNT_FORCE,
344 * "umount -f") the vgone() function is called on active vnodes
345 * on the mounted file systems to kill their data (the vnodes become
346 * "dead" ones [see src/sys/miscfs/deadfs/...]). that results in a
347 * call here (even if the uvn is still in use -- i.e. has a non-zero
348 * reference count). this case happens at "umount -f" and during a
349 * "reboot/halt" operation.
350 *
351 * => the caller must XLOCK and VOP_LOCK the vnode before calling us
352 * [protects us from getting a vnode that is already in the DYING
353 * state...]
354 * => unlike uvn_detach, this function must not return until all the
355 * uvn's pages are disposed of.
356 * => in case [2] the uvn is still alive after this call, but all I/O
357 * ops will fail (due to the backing vnode now being "dead"). this
358 * will prob. kill any process using the uvn due to pgo_get failing.
359 */
360
361 void
362 uvm_vnp_terminate(vp)
363 struct vnode *vp;
364 {
365 struct uvm_vnode *uvn = &vp->v_uvm;
366 if (uvn->u_flags & UVM_VNODE_WRITEABLE) {
367 simple_lock(&uvn_wl_lock);
368 LIST_REMOVE(uvn, u_wlist);
369 uvn->u_flags &= ~(UVM_VNODE_WRITEABLE);
370 simple_unlock(&uvn_wl_lock);
371 }
372 }
373
374 /*
375 * uvn_releasepg: handled a released page in a uvn
376 *
377 * => "pg" is a PG_BUSY [caller owns it], PG_RELEASED page that we need
378 * to dispose of.
379 * => caller must handled PG_WANTED case
380 * => called with page's object locked, pageq's unlocked
381 * => returns TRUE if page's object is still alive, FALSE if we
382 * killed the page's object. if we return TRUE, then we
383 * return with the object locked.
384 * => if (nextpgp != NULL) => we return pageq.tqe_next here, and return
385 * with the page queues locked [for pagedaemon]
386 * => if (nextpgp == NULL) => we return with page queues unlocked [normal case]
387 * => we kill the uvn if it is not referenced and we are suppose to
388 * kill it ("relkill").
389 */
390
391 boolean_t
392 uvn_releasepg(pg, nextpgp)
393 struct vm_page *pg;
394 struct vm_page **nextpgp; /* OUT */
395 {
396 struct uvm_vnode *uvn = (struct uvm_vnode *) pg->uobject;
397 #ifdef DIAGNOSTIC
398 if ((pg->flags & PG_RELEASED) == 0)
399 panic("uvn_releasepg: page not released!");
400 #endif
401
402 /*
403 * dispose of the page [caller handles PG_WANTED]
404 */
405 pmap_page_protect(PMAP_PGARG(pg), VM_PROT_NONE);
406 uvm_lock_pageq();
407 if (nextpgp)
408 *nextpgp = pg->pageq.tqe_next; /* next page for daemon */
409 uvm_pagefree(pg);
410 if (!nextpgp)
411 uvm_unlock_pageq();
412
413 #if 1
414 /* XXX I'm sure we need to do something here. */
415 uvn = uvn;
416 #else
417 /*
418 * now see if we need to kill the object
419 */
420 if (uvn->u_flags & UVM_VNODE_RELKILL) {
421 if (uvn->u_obj.uo_refs)
422 panic("uvn_releasepg: kill flag set on referenced "
423 "object!");
424 if (uvn->u_obj.uo_npages == 0) {
425 if (uvn->u_flags & UVM_VNODE_WRITEABLE) {
426 simple_lock(&uvn_wl_lock);
427 LIST_REMOVE(uvn, u_wlist);
428 simple_unlock(&uvn_wl_lock);
429 }
430 #ifdef DIAGNOSTIC
431 if (uvn->u_obj.memq.tqh_first)
432 panic("uvn_releasepg: pages in object with npages == 0");
433 #endif
434 if (uvn->u_flags & UVM_VNODE_WANTED)
435 /* still holding object lock */
436 wakeup(uvn);
437
438 uvn->u_flags = 0; /* DEAD! */
439 simple_unlock(&uvn->u_obj.vmobjlock);
440 return (FALSE);
441 }
442 }
443 #endif
444 return (TRUE);
445 }
446
447 /*
448 * NOTE: currently we have to use VOP_READ/VOP_WRITE because they go
449 * through the buffer cache and allow I/O in any size. These VOPs use
450 * synchronous i/o. [vs. VOP_STRATEGY which can be async, but doesn't
451 * go through the buffer cache or allow I/O sizes larger than a
452 * block]. we will eventually want to change this.
453 *
454 * issues to consider:
455 * uvm provides the uvm_aiodesc structure for async i/o management.
456 * there are two tailq's in the uvm. structure... one for pending async
457 * i/o and one for "done" async i/o. to do an async i/o one puts
458 * an aiodesc on the "pending" list (protected by splbio()), starts the
459 * i/o and returns VM_PAGER_PEND. when the i/o is done, we expect
460 * some sort of "i/o done" function to be called (at splbio(), interrupt
461 * time). this function should remove the aiodesc from the pending list
462 * and place it on the "done" list and wakeup the daemon. the daemon
463 * will run at normal spl() and will remove all items from the "done"
464 * list and call the "aiodone" hook for each done request (see uvm_pager.c).
465 * [in the old vm code, this was done by calling the "put" routine with
466 * null arguments which made the code harder to read and understand because
467 * you had one function ("put") doing two things.]
468 *
469 * so the current pager needs:
470 * int uvn_aiodone(struct uvm_aiodesc *)
471 *
472 * => return KERN_SUCCESS (aio finished, free it). otherwise requeue for
473 * later collection.
474 * => called with pageq's locked by the daemon.
475 *
476 * general outline:
477 * - "try" to lock object. if fail, just return (will try again later)
478 * - drop "u_nio" (this req is done!)
479 * - if (object->iosync && u_naio == 0) { wakeup &uvn->u_naio }
480 * - get "page" structures (atop?).
481 * - handle "wanted" pages
482 * - handle "released" pages [using pgo_releasepg]
483 * >>> pgo_releasepg may kill the object
484 * dont forget to look at "object" wanted flag in all cases.
485 */
486
487
488 /*
489 * uvn_flush: flush pages out of a uvm object.
490 *
491 * => object should be locked by caller. we may _unlock_ the object
492 * if (and only if) we need to clean a page (PGO_CLEANIT).
493 * we return with the object locked.
494 * => if PGO_CLEANIT is set, we may block (due to I/O). thus, a caller
495 * might want to unlock higher level resources (e.g. vm_map)
496 * before calling flush.
497 * => if PGO_CLEANIT is not set, then we will neither unlock the object
498 * or block.
499 * => if PGO_ALLPAGE is set, then all pages in the object are valid targets
500 * for flushing.
501 * => NOTE: we rely on the fact that the object's memq is a TAILQ and
502 * that new pages are inserted on the tail end of the list. thus,
503 * we can make a complete pass through the object in one go by starting
504 * at the head and working towards the tail (new pages are put in
505 * front of us).
506 * => NOTE: we are allowed to lock the page queues, so the caller
507 * must not be holding the lock on them [e.g. pagedaemon had
508 * better not call us with the queues locked]
509 * => we return TRUE unless we encountered some sort of I/O error
510 *
511 * comment on "cleaning" object and PG_BUSY pages:
512 * this routine is holding the lock on the object. the only time
513 * that it can run into a PG_BUSY page that it does not own is if
514 * some other process has started I/O on the page (e.g. either
515 * a pagein, or a pageout). if the PG_BUSY page is being paged
516 * in, then it can not be dirty (!PG_CLEAN) because no one has
517 * had a chance to modify it yet. if the PG_BUSY page is being
518 * paged out then it means that someone else has already started
519 * cleaning the page for us (how nice!). in this case, if we
520 * have syncio specified, then after we make our pass through the
521 * object we need to wait for the other PG_BUSY pages to clear
522 * off (i.e. we need to do an iosync). also note that once a
523 * page is PG_BUSY it must stay in its object until it is un-busyed.
524 *
525 * note on page traversal:
526 * we can traverse the pages in an object either by going down the
527 * linked list in "uobj->memq", or we can go over the address range
528 * by page doing hash table lookups for each address. depending
529 * on how many pages are in the object it may be cheaper to do one
530 * or the other. we set "by_list" to true if we are using memq.
531 * if the cost of a hash lookup was equal to the cost of the list
532 * traversal we could compare the number of pages in the start->stop
533 * range to the total number of pages in the object. however, it
534 * seems that a hash table lookup is more expensive than the linked
535 * list traversal, so we multiply the number of pages in the
536 * start->stop range by a penalty which we define below.
537 */
538
539 #define UVN_HASH_PENALTY 4 /* XXX: a guess */
540
541 static boolean_t
542 uvn_flush(uobj, start, stop, flags)
543 struct uvm_object *uobj;
544 vaddr_t start, stop;
545 int flags;
546 {
547 struct uvm_vnode *uvn = (struct uvm_vnode *) uobj;
548 struct vnode *vp = (struct vnode *)uobj;
549 struct vm_page *pp, *ppnext, *ptmp;
550 struct vm_page *pps[MAXBSIZE >> PAGE_SHIFT], **ppsp;
551 int npages, result, lcv;
552 boolean_t retval, need_iosync, by_list, needs_clean;
553 vaddr_t curoff;
554 u_short pp_version;
555 UVMHIST_FUNC("uvn_flush"); UVMHIST_CALLED(maphist);
556
557 if (uvn->u_size == VSIZENOTSET) {
558 #ifdef DEBUG
559 void vp_name(void *);
560
561 printf("uvn_flush: size not set vp %p\n", uvn);
562 if ((flags & PGO_ALLPAGES) == 0)
563 printf("... and PGO_ALLPAGES not set: "
564 "start 0x%lx end 0x%lx flags 0x%x\n",
565 start, stop, flags);
566 vprint("uvn_flush VSIZENOTSET", vp);
567 vp_name(uvn);
568 #endif
569 flags |= PGO_ALLPAGES;
570 }
571
572 curoff = 0; /* XXX: shut up gcc */
573 /*
574 * get init vals and determine how we are going to traverse object
575 */
576
577 need_iosync = FALSE;
578 retval = TRUE; /* return value */
579 if (flags & PGO_ALLPAGES) {
580 start = 0;
581 stop = -1;
582 by_list = TRUE; /* always go by the list */
583 } else {
584 start = trunc_page(start);
585 stop = round_page(stop);
586 if (stop > round_page(uvn->u_size)) {
587 printf("uvn_flush: oor vp %p start 0x%x stop 0x%x size 0x%x\n", uvn, (int)start, (int)stop, (int)round_page(uvn->u_size));
588 }
589
590 by_list = (uobj->uo_npages <=
591 ((stop - start) >> PAGE_SHIFT) * UVN_HASH_PENALTY);
592 }
593
594 UVMHIST_LOG(maphist,
595 " flush start=0x%x, stop=0x%x, by_list=%d, flags=0x%x",
596 start, stop, by_list, flags);
597
598 /*
599 * PG_CLEANCHK: this bit is used by the pgo_mk_pcluster function as
600 * a _hint_ as to how up to date the PG_CLEAN bit is. if the hint
601 * is wrong it will only prevent us from clustering... it won't break
602 * anything. we clear all PG_CLEANCHK bits here, and pgo_mk_pcluster
603 * will set them as it syncs PG_CLEAN. This is only an issue if we
604 * are looking at non-inactive pages (because inactive page's PG_CLEAN
605 * bit is always up to date since there are no mappings).
606 * [borrowed PG_CLEANCHK idea from FreeBSD VM]
607 */
608
609 if ((flags & PGO_CLEANIT) != 0 &&
610 uobj->pgops->pgo_mk_pcluster != NULL) {
611 if (by_list) {
612 for (pp = TAILQ_FIRST(&uobj->memq);
613 pp != NULL ;
614 pp = TAILQ_NEXT(pp, listq)) {
615 if (pp->offset < start ||
616 (pp->offset >= stop && stop != -1))
617 continue;
618 pp->flags &= ~PG_CLEANCHK;
619 }
620
621 } else { /* by hash */
622 for (curoff = start ; curoff < stop;
623 curoff += PAGE_SIZE) {
624 pp = uvm_pagelookup(uobj, curoff);
625 if (pp)
626 pp->flags &= ~PG_CLEANCHK;
627 }
628 }
629 }
630
631 /*
632 * now do it. note: we must update ppnext in body of loop or we
633 * will get stuck. we need to use ppnext because we may free "pp"
634 * before doing the next loop.
635 */
636
637 if (by_list) {
638 pp = TAILQ_FIRST(&uobj->memq);
639 } else {
640 curoff = start;
641 pp = uvm_pagelookup(uobj, curoff);
642 }
643
644 ppnext = NULL; /* XXX: shut up gcc */
645 ppsp = NULL; /* XXX: shut up gcc */
646 uvm_lock_pageq(); /* page queues locked */
647
648 /* locked: both page queues and uobj */
649 for ( ; (by_list && pp != NULL) ||
650 (!by_list && curoff < stop) ; pp = ppnext) {
651
652 if (by_list) {
653
654 /*
655 * range check
656 */
657
658 if (pp->offset < start || pp->offset >= stop) {
659 ppnext = TAILQ_NEXT(pp, listq);
660 continue;
661 }
662
663 } else {
664
665 /*
666 * null check
667 */
668
669 curoff += PAGE_SIZE;
670 if (pp == NULL) {
671 if (curoff < stop)
672 ppnext = uvm_pagelookup(uobj, curoff);
673 continue;
674 }
675
676 }
677
678 /*
679 * handle case where we do not need to clean page (either
680 * because we are not clean or because page is not dirty or
681 * is busy):
682 *
683 * NOTE: we are allowed to deactivate a non-wired active
684 * PG_BUSY page, but once a PG_BUSY page is on the inactive
685 * queue it must stay put until it is !PG_BUSY (so as not to
686 * confuse pagedaemon).
687 */
688
689 if ((flags & PGO_CLEANIT) == 0 || (pp->flags & PG_BUSY) != 0) {
690 needs_clean = FALSE;
691 if ((pp->flags & PG_BUSY) != 0 &&
692 (flags & (PGO_CLEANIT|PGO_SYNCIO)) ==
693 (PGO_CLEANIT|PGO_SYNCIO))
694 need_iosync = TRUE;
695 } else {
696 /*
697 * freeing: nuke all mappings so we can sync
698 * PG_CLEAN bit with no race
699 */
700 if ((pp->flags & PG_CLEAN) != 0 &&
701 (flags & PGO_FREE) != 0 &&
702 (pp->pqflags & PQ_ACTIVE) != 0)
703 pmap_page_protect(PMAP_PGARG(pp), VM_PROT_NONE);
704 if ((pp->flags & PG_CLEAN) != 0 &&
705 pmap_is_modified(PMAP_PGARG(pp)))
706 pp->flags &= ~(PG_CLEAN);
707 pp->flags |= PG_CLEANCHK; /* update "hint" */
708
709 needs_clean = ((pp->flags & PG_CLEAN) == 0);
710 }
711
712 /*
713 * if we don't need a clean... load ppnext and dispose of pp
714 */
715 if (!needs_clean) {
716 /* load ppnext */
717 if (by_list)
718 ppnext = pp->listq.tqe_next;
719 else {
720 if (curoff < stop)
721 ppnext = uvm_pagelookup(uobj, curoff);
722 }
723
724 /* now dispose of pp */
725 if (flags & PGO_DEACTIVATE) {
726 if ((pp->pqflags & PQ_INACTIVE) == 0 &&
727 pp->wire_count == 0) {
728 pmap_page_protect(PMAP_PGARG(pp),
729 VM_PROT_NONE);
730 uvm_pagedeactivate(pp);
731 }
732
733 } else if (flags & PGO_FREE) {
734 if (pp->flags & PG_BUSY) {
735 /* release busy pages */
736 pp->flags |= PG_RELEASED;
737 } else {
738 pmap_page_protect(PMAP_PGARG(pp),
739 VM_PROT_NONE);
740 /* removed page from object */
741 uvm_pagefree(pp);
742 }
743 }
744 /* ppnext is valid so we can continue... */
745 continue;
746 }
747
748 /*
749 * pp points to a page in the locked object that we are
750 * working on. if it is !PG_CLEAN,!PG_BUSY and we asked
751 * for cleaning (PGO_CLEANIT). we clean it now.
752 *
753 * let uvm_pager_put attempted a clustered page out.
754 * note: locked: uobj and page queues.
755 */
756
757 pp->flags |= PG_BUSY; /* we 'own' page now */
758 UVM_PAGE_OWN(pp, "uvn_flush");
759 pmap_page_protect(PMAP_PGARG(pp), VM_PROT_READ);
760 pp_version = pp->version;
761 ReTry:
762 ppsp = pps;
763 npages = sizeof(pps) / sizeof(struct vm_page *);
764
765 /* locked: page queues, uobj */
766 result = uvm_pager_put(uobj, pp, &ppsp, &npages,
767 flags | PGO_DOACTCLUST, start, stop);
768 /* unlocked: page queues, uobj */
769
770 /*
771 * at this point nothing is locked. if we did an async I/O
772 * it is remotely possible for the async i/o to complete and
773 * the page "pp" be freed or what not before we get a chance
774 * to relock the object. in order to detect this, we have
775 * saved the version number of the page in "pp_version".
776 */
777
778 /* relock! */
779 simple_lock(&uobj->vmobjlock);
780 uvm_lock_pageq();
781
782 /*
783 * VM_PAGER_AGAIN: given the structure of this pager, this
784 * can only happen when we are doing async I/O and can't
785 * map the pages into kernel memory (pager_map) due to lack
786 * of vm space. if this happens we drop back to sync I/O.
787 */
788
789 if (result == VM_PAGER_AGAIN) {
790 /*
791 * it is unlikely, but page could have been released
792 * while we had the object lock dropped. we ignore
793 * this now and retry the I/O. we will detect and
794 * handle the released page after the syncio I/O
795 * completes.
796 */
797 #ifdef DIAGNOSTIC
798 if (flags & PGO_SYNCIO)
799 panic("uvn_flush: PGO_SYNCIO return 'try again' error (impossible)");
800 #endif
801 flags |= PGO_SYNCIO;
802 goto ReTry;
803 }
804
805 /*
806 * the cleaning operation is now done. finish up. note that
807 * on error (!OK, !PEND) uvm_pager_put drops the cluster for us.
808 * if success (OK, PEND) then uvm_pager_put returns the cluster
809 * to us in ppsp/npages.
810 */
811
812 /*
813 * for pending async i/o if we are not deactivating/freeing
814 * we can move on to the next page.
815 */
816
817 if (result == VM_PAGER_PEND) {
818
819 if ((flags & (PGO_DEACTIVATE|PGO_FREE)) == 0) {
820 /*
821 * no per-page ops: refresh ppnext and continue
822 */
823 if (by_list) {
824 if (pp->version == pp_version)
825 ppnext = pp->listq.tqe_next;
826 else
827 /* reset */
828 ppnext = uobj->memq.tqh_first;
829 } else {
830 if (curoff < stop)
831 ppnext = uvm_pagelookup(uobj,
832 curoff);
833 }
834 continue;
835 }
836
837 /* need to do anything here? */
838 }
839
840 /*
841 * need to look at each page of the I/O operation. we defer
842 * processing "pp" until the last trip through this "for" loop
843 * so that we can load "ppnext" for the main loop after we
844 * play with the cluster pages [thus the "npages + 1" in the
845 * loop below].
846 */
847
848 for (lcv = 0 ; lcv < npages + 1 ; lcv++) {
849
850 /*
851 * handle ppnext for outside loop, and saving pp
852 * until the end.
853 */
854 if (lcv < npages) {
855 if (ppsp[lcv] == pp)
856 continue; /* skip pp until the end */
857 ptmp = ppsp[lcv];
858 } else {
859 ptmp = pp;
860
861 /* set up next page for outer loop */
862 if (by_list) {
863 if (pp->version == pp_version)
864 ppnext = pp->listq.tqe_next;
865 else
866 /* reset */
867 ppnext = uobj->memq.tqh_first;
868 } else {
869 if (curoff < stop)
870 ppnext = uvm_pagelookup(uobj, curoff);
871 }
872 }
873
874 /*
875 * verify the page didn't get moved while obj was
876 * unlocked
877 */
878 if (result == VM_PAGER_PEND && ptmp->uobject != uobj)
879 continue;
880
881 /*
882 * unbusy the page if I/O is done. note that for
883 * pending I/O it is possible that the I/O op
884 * finished before we relocked the object (in
885 * which case the page is no longer busy).
886 */
887
888 if (result != VM_PAGER_PEND) {
889 if (ptmp->flags & PG_WANTED)
890 /* still holding object lock */
891 wakeup(ptmp);
892
893 ptmp->flags &= ~(PG_WANTED|PG_BUSY);
894 UVM_PAGE_OWN(ptmp, NULL);
895 if (ptmp->flags & PG_RELEASED) {
896
897 /* pgo_releasepg wants this */
898 uvm_unlock_pageq();
899 if (!uvn_releasepg(ptmp, NULL))
900 return (TRUE);
901
902 uvm_lock_pageq(); /* relock */
903 continue; /* next page */
904
905 } else {
906 ptmp->flags |= (PG_CLEAN|PG_CLEANCHK);
907 if ((flags & PGO_FREE) == 0)
908 pmap_clear_modify(
909 PMAP_PGARG(ptmp));
910 }
911 }
912
913 /*
914 * dispose of page
915 */
916
917 if (flags & PGO_DEACTIVATE) {
918 if ((pp->pqflags & PQ_INACTIVE) == 0 &&
919 pp->wire_count == 0) {
920 pmap_page_protect(PMAP_PGARG(ptmp),
921 VM_PROT_NONE);
922 uvm_pagedeactivate(ptmp);
923 }
924
925 } else if (flags & PGO_FREE) {
926 if (result == VM_PAGER_PEND) {
927 if ((ptmp->flags & PG_BUSY) != 0)
928 /* signal for i/o done */
929 ptmp->flags |= PG_RELEASED;
930 } else {
931 if (result != VM_PAGER_OK) {
932 printf("uvn_flush: obj=%p, "
933 "offset=0x%lx. error %d\n",
934 pp->uobject, pp->offset,
935 result);
936 printf("uvn_flush: WARNING: "
937 "changes to page may be "
938 "lost!\n");
939 retval = FALSE;
940 }
941 pmap_page_protect(PMAP_PGARG(ptmp),
942 VM_PROT_NONE);
943 uvm_pagefree(ptmp);
944 }
945 }
946
947 } /* end of "lcv" for loop */
948
949 } /* end of "pp" for loop */
950
951 /*
952 * done with pagequeues: unlock
953 */
954 uvm_unlock_pageq();
955
956 /*
957 * now wait for all I/O if required.
958 */
959 if (need_iosync) {
960 UVMHIST_LOG(maphist," <<DOING IOSYNC>>",0,0,0,0);
961
962 /*
963 * XXX this doesn't use the new two-flag scheme,
964 * but to use that, all i/o initiators will have to change.
965 */
966
967 while (vp->v_numoutput != 0) {
968 vp->v_flag |= VBWAIT;
969 UVM_UNLOCK_AND_WAIT(&vp->v_numoutput,
970 &uvn->u_obj.vmobjlock,
971 FALSE, "uvn_flush",0);
972 simple_lock(&uvn->u_obj.vmobjlock);
973 }
974 }
975
976 /* return, with object locked! */
977 UVMHIST_LOG(maphist,"<- done (retval=0x%x)",retval,0,0,0);
978 return(retval);
979 }
980
981 /*
982 * uvn_cluster
983 *
984 * we are about to do I/O in an object at offset. this function is called
985 * to establish a range of offsets around "offset" in which we can cluster
986 * I/O.
987 *
988 * - currently doesn't matter if obj locked or not.
989 */
990
991 static void
992 uvn_cluster(uobj, offset, loffset, hoffset)
993 struct uvm_object *uobj;
994 vaddr_t offset;
995 vaddr_t *loffset, *hoffset; /* OUT */
996 {
997 struct uvm_vnode *uvn = (struct uvm_vnode *) uobj;
998 UVMHIST_FUNC("uvn_cluster"); UVMHIST_CALLED(ubchist);
999
1000 *loffset = offset;
1001
1002 if (*loffset >= uvn->u_size)
1003 {
1004 /* XXX nfs writes cause trouble with this */
1005 *loffset = *hoffset = offset;
1006 UVMHIST_LOG(ubchist, "uvn_cluster: offset out of range: vp %p loffset 0x%x",
1007 uobj, (int)*loffset, 0,0);
1008 Debugger();
1009 return;
1010 }
1011
1012 /*
1013 * XXX: old pager claims we could use VOP_BMAP to get maxcontig value.
1014 */
1015 *hoffset = *loffset + MAXBSIZE;
1016 if (*hoffset > round_page(uvn->u_size)) /* past end? */
1017 *hoffset = round_page(uvn->u_size);
1018
1019 return;
1020 }
1021
1022 /*
1023 * uvn_put: flush page data to backing store.
1024 *
1025 * => prefer map unlocked (not required)
1026 * => object must be locked! we will _unlock_ it before starting I/O.
1027 * => flags: PGO_SYNCIO -- use sync. I/O
1028 * => note: caller must set PG_CLEAN and pmap_clear_modify (if needed)
1029 * => XXX: currently we use VOP_READ/VOP_WRITE which are only sync.
1030 * [thus we never do async i/o! see iodone comment]
1031 */
1032
1033 static int
1034 uvn_put(uobj, pps, npages, flags)
1035 struct uvm_object *uobj;
1036 struct vm_page **pps;
1037 int npages, flags;
1038 {
1039 int retval, sync;
1040
1041 sync = (flags & PGO_SYNCIO) ? 1 : 0;
1042
1043 /* note: object locked */
1044 simple_lock_assert(&uobj->vmobjlock, SLOCK_LOCKED);
1045
1046 /* XXX why would the VOP need it locked? */
1047 /* currently, just to increment vp->v_numoutput (aka uvn->u_nio) */
1048 simple_unlock(&uobj->vmobjlock);
1049 retval = VOP_PUTPAGES((struct vnode *)uobj, pps, npages, sync, &retval);
1050 /* note: object unlocked */
1051 simple_lock_assert(&uobj->vmobjlock, SLOCK_UNLOCKED);
1052
1053 return(retval);
1054 }
1055
1056
1057 /*
1058 * uvn_get: get pages (synchronously) from backing store
1059 *
1060 * => prefer map unlocked (not required)
1061 * => object must be locked! we will _unlock_ it before starting any I/O.
1062 * => flags: PGO_ALLPAGES: get all of the pages
1063 * PGO_LOCKED: fault data structures are locked
1064 * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx]
1065 * => NOTE: caller must check for released pages!!
1066 */
1067
1068 static int
1069 uvn_get(uobj, offset, pps, npagesp, centeridx, access_type, advice, flags)
1070 struct uvm_object *uobj;
1071 vaddr_t offset;
1072 struct vm_page **pps; /* IN/OUT */
1073 int *npagesp; /* IN (OUT if PGO_LOCKED) */
1074 int centeridx, advice, flags;
1075 vm_prot_t access_type;
1076 {
1077 struct vnode *vp = (struct vnode *)uobj;
1078 int error;
1079
1080 simple_lock_assert(&uobj->vmobjlock, SLOCK_LOCKED);
1081 error = VOP_GETPAGES(vp, offset, pps, npagesp, centeridx,
1082 access_type, advice, flags);
1083 simple_lock_assert(&uobj->vmobjlock, flags & PGO_LOCKED ?
1084 SLOCK_LOCKED : SLOCK_UNLOCKED);
1085 return error ? VM_PAGER_ERROR : VM_PAGER_OK;
1086 }
1087
1088 /*
1089 * uvn_findpages:
1090 * return the page for the uobj and offset requested, allocating if needed.
1091 * => uobj must be locked.
1092 * => returned page will be BUSY.
1093 */
1094
1095 void
1096 uvn_findpages(uobj, offset, npagesp, pps, flags)
1097 struct uvm_object *uobj;
1098 vaddr_t offset;
1099 int *npagesp;
1100 struct vm_page **pps;
1101 int flags;
1102 {
1103 int i, rv, npages;
1104
1105 rv = 0;
1106 npages = *npagesp;
1107 for (i = 0; i < npages; i++, offset += PAGE_SIZE) {
1108 rv += uvn_findpage(uobj, offset, &pps[i], flags);
1109 }
1110 *npagesp = rv;
1111 }
1112
1113
1114 static int
1115 uvn_findpage(uobj, offset, pps, flags)
1116 struct uvm_object *uobj;
1117 vaddr_t offset;
1118 struct vm_page **pps;
1119 int flags;
1120 {
1121 struct vm_page *ptmp;
1122 UVMHIST_FUNC("uvn_findpage"); UVMHIST_CALLED(ubchist);
1123 UVMHIST_LOG(ubchist, "vp %p off 0x%lx", uobj, offset,0,0);
1124
1125 simple_lock_assert(&uobj->vmobjlock, SLOCK_LOCKED);
1126
1127 if (*pps == PGO_DONTCARE) {
1128 UVMHIST_LOG(ubchist, "dontcare", 0,0,0,0);
1129 return 0;
1130 }
1131 #ifdef DIAGNOTISTIC
1132 if (*pps != NULL) {
1133 panic("uvn_findpage: *pps not NULL");
1134 }
1135 #endif
1136
1137 for (;;) {
1138 /* look for an existing page */
1139 ptmp = uvm_pagelookup(uobj, offset);
1140
1141 /* nope? allocate one now */
1142 if (ptmp == NULL) {
1143 if (flags & UFP_NOALLOC) {
1144 UVMHIST_LOG(ubchist, "noalloc", 0,0,0,0);
1145 return 0;
1146 }
1147 ptmp = uvm_pagealloc(uobj, offset, NULL, 0);
1148 if (ptmp == NULL) {
1149 if (flags & UFP_NOWAIT) {
1150 UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
1151 return 0;
1152 }
1153 simple_unlock(&uobj->vmobjlock);
1154 uvm_wait("uvn_fp1");
1155 simple_lock(&uobj->vmobjlock);
1156 continue;
1157 }
1158 UVMHIST_LOG(ubchist, "alloced",0,0,0,0);
1159 break;
1160 } else if (flags & UFP_NOCACHE) {
1161 UVMHIST_LOG(ubchist, "nocache",0,0,0,0);
1162 return 0;
1163 }
1164
1165 /* page is there, see if we need to wait on it */
1166 if ((ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
1167 if (flags & UFP_NOWAIT) {
1168 UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
1169 return 0;
1170 }
1171 ptmp->flags |= PG_WANTED;
1172 UVM_UNLOCK_AND_WAIT(ptmp, &uobj->vmobjlock, 0,
1173 "uvn_fp2",0);
1174 simple_lock(&uobj->vmobjlock);
1175 continue;
1176 }
1177
1178 /* skip PG_RDONLY pages if requested */
1179 if ((flags & UFP_NORDONLY) && (ptmp->flags & PG_RDONLY)) {
1180 UVMHIST_LOG(ubchist, "nordonly",0,0,0,0);
1181 return 0;
1182 }
1183 /* BUSY the page and we're done. */
1184 ptmp->flags |= PG_BUSY;
1185 UVM_PAGE_OWN(ptmp, "uvn_findpage");
1186 UVMHIST_LOG(ubchist, "found",0,0,0,0);
1187 break;
1188 }
1189 *pps = ptmp;
1190 return 1;
1191 }
1192
1193 /*
1194 * uvn_asyncget: start async I/O to bring pages into ram
1195 *
1196 * => caller must lock object(???XXX: see if this is best)
1197 * => could be called from uvn_get or a madvise() fault-ahead.
1198 * => if it fails, it doesn't matter.
1199 */
1200
1201 static int
1202 uvn_asyncget(uobj, offset, npages)
1203 struct uvm_object *uobj;
1204 vaddr_t offset;
1205 int npages;
1206 {
1207
1208 /*
1209 * XXXCDC: we can't do async I/O yet
1210 */
1211 printf("uvn_asyncget called\n");
1212 return (KERN_SUCCESS);
1213 }
1214
1215 boolean_t
1216 uvm_vnp_uncache(vp)
1217 struct vnode *vp;
1218 {
1219 return(TRUE);
1220 }
1221
1222 /*
1223 * uvm_vnp_setsize: grow or shrink a vnode uvn
1224 *
1225 * grow => just update size value
1226 * shrink => toss un-needed pages
1227 *
1228 * => we assume that the caller has a reference of some sort to the
1229 * vnode in question so that it will not be yanked out from under
1230 * us.
1231 *
1232 * called from:
1233 * => truncate fns (ext2fs_truncate, ffs_truncate, detrunc[msdos])
1234 * => "write" fns (ext2fs_write, WRITE [ufs/ufs], msdosfs_write, nfs_write)
1235 * => ffs_balloc [XXX: why? doesn't WRITE handle?]
1236 * => NFS: nfs_loadattrcache, nfs_getattrcache, nfs_setattr
1237 * => union fs: union_newsize
1238 */
1239
1240 void
1241 uvm_vnp_setsize(vp, newsize)
1242 struct vnode *vp;
1243 u_quad_t newsize;
1244 {
1245 struct uvm_vnode *uvn = &vp->v_uvm;
1246
1247 /*
1248 * lock uvn and check for valid object, and if valid: do it!
1249 */
1250 simple_lock(&uvn->u_obj.vmobjlock);
1251
1252 /*
1253 * make sure that the newsize fits within a vaddr_t
1254 * XXX: need to revise addressing data types
1255 */
1256
1257 if (newsize > (vaddr_t) -PAGE_SIZE) {
1258 #ifdef DEBUG
1259 printf("uvm_vnp_setsize: vn %p size truncated "
1260 "%qx->%lx\n", vp, (long long)newsize,
1261 (vaddr_t)-PAGE_SIZE);
1262 #endif
1263 newsize = (vaddr_t)-PAGE_SIZE;
1264 }
1265
1266 /*
1267 * now check if the size has changed: if we shrink we had better
1268 * toss some pages...
1269 */
1270
1271 if (uvn->u_size > newsize && uvn->u_size != VSIZENOTSET) {
1272 (void) uvn_flush(&uvn->u_obj, (vaddr_t)newsize,
1273 uvn->u_size, PGO_FREE);
1274 }
1275 uvn->u_size = (vaddr_t)newsize;
1276 simple_unlock(&uvn->u_obj.vmobjlock);
1277 }
1278
1279 /*
1280 * uvm_vnp_sync: flush all dirty VM pages back to their backing vnodes.
1281 *
1282 * => called from sys_sync with no VM structures locked
1283 * => only one process can do a sync at a time (because the uvn
1284 * structure only has one queue for sync'ing). we ensure this
1285 * by holding the uvn_sync_lock while the sync is in progress.
1286 * other processes attempting a sync will sleep on this lock
1287 * until we are done.
1288 */
1289
1290 void
1291 uvm_vnp_sync(mp)
1292 struct mount *mp;
1293 {
1294 struct uvm_vnode *uvn;
1295 struct vnode *vp;
1296 boolean_t got_lock;
1297
1298 /*
1299 * step 1: ensure we are only ones using the uvn_sync_q by locking
1300 * our lock...
1301 */
1302 lockmgr(&uvn_sync_lock, LK_EXCLUSIVE, (void *)0);
1303
1304 /*
1305 * step 2: build up a simpleq of uvns of interest based on the
1306 * write list. we gain a reference to uvns of interest. must
1307 * be careful about locking uvn's since we will be holding uvn_wl_lock
1308 * in the body of the loop.
1309 */
1310 SIMPLEQ_INIT(&uvn_sync_q);
1311 simple_lock(&uvn_wl_lock);
1312 for (uvn = LIST_FIRST(&uvn_wlist); uvn != NULL;
1313 uvn = LIST_NEXT(uvn, u_wlist)) {
1314
1315 vp = (struct vnode *) uvn;
1316 if (mp && vp->v_mount != mp)
1317 continue;
1318
1319 /* attempt to gain reference */
1320 while ((got_lock = simple_lock_try(&uvn->u_obj.vmobjlock)) ==
1321 FALSE &&
1322 (uvn->u_flags & UVM_VNODE_BLOCKED) == 0)
1323 /* spin */ ;
1324
1325 /*
1326 * we will exit the loop if either if the following are true:
1327 * - we got the lock [always true if NCPU == 1]
1328 * - we failed to get the lock but noticed the vnode was
1329 * "blocked" -- in this case the vnode must be a dying
1330 * vnode, and since dying vnodes are in the process of
1331 * being flushed out, we can safely skip this one
1332 *
1333 * we want to skip over the vnode if we did not get the lock,
1334 * or if the vnode is already dying (due to the above logic).
1335 *
1336 * note that uvn must already be valid because we found it on
1337 * the wlist (this also means it can't be ALOCK'd).
1338 */
1339 if (!got_lock || (uvn->u_flags & UVM_VNODE_BLOCKED) != 0) {
1340 if (got_lock)
1341 simple_unlock(&uvn->u_obj.vmobjlock);
1342 continue; /* skip it */
1343 }
1344
1345 vget(vp, LK_INTERLOCK);
1346
1347 /*
1348 * got it!
1349 */
1350 SIMPLEQ_INSERT_HEAD(&uvn_sync_q, uvn, u_syncq);
1351 }
1352 simple_unlock(&uvn_wl_lock);
1353
1354 /*
1355 * step 3: we now have a list of uvn's that may need cleaning.
1356 * we are holding the uvn_sync_lock, but have dropped the uvn_wl_lock
1357 * (so we can now safely lock uvn's again).
1358 */
1359
1360 for (uvn = uvn_sync_q.sqh_first ; uvn ; uvn = uvn->u_syncq.sqe_next) {
1361 simple_lock(&uvn->u_obj.vmobjlock);
1362 uvn_flush(&uvn->u_obj, 0, 0,
1363 PGO_CLEANIT|PGO_ALLPAGES|PGO_DOACTCLUST);
1364
1365 /*
1366 * if we have the only reference and we just cleaned the uvn,
1367 * then we can pull it out of the UVM_VNODE_WRITEABLE state
1368 * thus allowing us to avoid thinking about flushing it again
1369 * on later sync ops.
1370 */
1371 if (uvn->u_obj.uo_refs == 1 &&
1372 (uvn->u_flags & UVM_VNODE_WRITEABLE)) {
1373 simple_lock(&uvn_wl_lock);
1374 LIST_REMOVE(uvn, u_wlist);
1375 uvn->u_flags &= ~UVM_VNODE_WRITEABLE;
1376 simple_unlock(&uvn_wl_lock);
1377 }
1378
1379 simple_unlock(&uvn->u_obj.vmobjlock);
1380
1381 /* now drop our reference to the uvn */
1382 uvn_detach(&uvn->u_obj);
1383 }
1384
1385 /*
1386 * done! release sync lock
1387 */
1388 lockmgr(&uvn_sync_lock, LK_RELEASE, (void *)0);
1389 }
1390
1391
1392 /*
1393 * uvm_vnp_zerorange: set a range of bytes in a file to zero.
1394 * this is called from fs-specific code when truncating a file
1395 * to zero the part of last block that is past the new end-of-file.
1396 */
1397 void
1398 uvm_vnp_zerorange(vp, off, len)
1399 struct vnode *vp;
1400 off_t off;
1401 size_t len;
1402 {
1403 void *win;
1404
1405 /*
1406 * XXX invent kzero() and use it
1407 */
1408
1409 while (len) {
1410 vsize_t bytelen = len;
1411
1412 win = ubc_alloc(&vp->v_uvm.u_obj, off, &bytelen, UBC_WRITE);
1413 memset(win, 0, bytelen);
1414 ubc_release(win, 0);
1415
1416 off += bytelen;
1417 len -= bytelen;
1418 }
1419 }
1420
1421 /*
1422 * uvn_doasyncget: start one readahead i/o.
1423 */
1424
1425 static void
1426 uvn_doasyncget(pgs, bytes, blkno)
1427 struct vm_page **pgs;
1428 size_t bytes;
1429 daddr_t blkno;
1430 {
1431 struct uvm_aiobuf *abp;
1432 struct buf *bp;
1433 struct vnode *vp = (struct vnode *)pgs[0]->uobject;
1434 int pages = roundup(bytes, PAGE_SIZE) >> PAGE_SHIFT;
1435 UVMHIST_FUNC("uvn_doasyncget"); UVMHIST_CALLED(ubchist);
1436
1437 UVMHIST_LOG(ubchist, "vp %p offset 0x%x bytes 0x%x blkno 0x%x",
1438 vp, (int)pgs[0]->offset, (int)bytes, (int)blkno);
1439
1440 abp = pool_get(uvm_aiobuf_pool, PR_WAITOK);
1441 abp->aio.aiodone = uvm_aio_aiodone;
1442 abp->aio.kva = uvm_pagermapin(pgs, pages, NULL, M_WAITOK);
1443 abp->aio.npages = pages;
1444 abp->aio.pd_ptr = abp;
1445
1446 bp = &abp->buf;
1447 bzero(bp, sizeof *bp);
1448 bp->b_flags = B_BUSY|B_READ|B_CALL|B_ASYNC;
1449 bp->b_iodone = uvm_aio_biodone;
1450 bp->b_lblkno = 0;
1451 bp->b_blkno = blkno;
1452 bp->b_bufsize = pages << PAGE_SHIFT;
1453 bp->b_bcount = bytes;
1454 bp->b_vp = vp;
1455 bp->b_data = (void *)abp->aio.kva;
1456
1457 VOP_STRATEGY(bp);
1458 }
1459
1460 #define MAXRAPAGES 16
1461
1462 /*
1463 * asynchronously create pages for a vnode and read their data.
1464 */
1465
1466 void
1467 uvm_vnp_asyncget(vp, off, len, bsize)
1468 struct vnode *vp;
1469 off_t off;
1470 size_t len;
1471 size_t bsize;
1472 {
1473 off_t filesize = vp->v_uvm.u_size;
1474 struct vm_page *pgs[MAXRAPAGES];
1475 struct uvm_object *uobj = &vp->v_uvm.u_obj;
1476 daddr_t lbn, blkno;
1477 int i, npages, npgs, startidx, run, bytes, startpage, endpage;
1478 int count;
1479 UVMHIST_FUNC("uvn_asyncget"); UVMHIST_CALLED(ubchist);
1480
1481 if (off != trunc_page(off)) {
1482 panic("off 0x%x not page-aligned", (int)off);
1483 }
1484
1485 UVMHIST_LOG(ubchist, "asyncget off 0x%x len 0x%x",
1486 (int)off, (int)len,0,0);
1487
1488 count = round_page(len) >> PAGE_SHIFT;
1489 while (count > 0) {
1490 if (off >= filesize) {
1491 return;
1492 }
1493
1494 lbn = off / bsize;
1495 if (VOP_BMAP(vp, lbn, NULL, &blkno, &run) != 0) {
1496 return;
1497 }
1498
1499 UVMHIST_LOG(ubchist, "bmap lbn 0x%x bn 0x%x",
1500 (int)lbn, (int)blkno,0,0);
1501
1502 /* don't do readahead past file holes... */
1503 if (blkno == (daddr_t)-1) {
1504 return;
1505 }
1506
1507 startpage = off >> PAGE_SHIFT;
1508 endpage = min(roundup(off + 1 + run * bsize, bsize),
1509 round_page(filesize)) >> PAGE_SHIFT;
1510 npages = min(endpage - startpage, min(count, MAXRAPAGES));
1511
1512 UVMHIST_LOG(ubchist, "off 0x%x run 0x%x "
1513 "startpage %d endpage %d",
1514 (int)off, run, startpage, endpage);
1515 UVMHIST_LOG(ubchist, "runend 0x%x fileend 0x%x sum 0x%x",
1516 (int)roundup(off + 1 + run * bsize, bsize),
1517 (int)round_page(filesize),
1518 (int)(off + 1 + run * bsize), 0);
1519
1520 if (npages == 0) {
1521 return;
1522 }
1523
1524 memset(pgs, 0, npages * sizeof(pgs[0]));
1525
1526 simple_lock(&uobj->vmobjlock);
1527 npgs = npages;
1528 uvn_findpages(uobj, off, &npgs, pgs, UFP_NOWAIT | UFP_NOCACHE);
1529 simple_unlock(&uobj->vmobjlock);
1530
1531 blkno += (off - lbn * bsize) >> DEV_BSHIFT;
1532
1533 /*
1534 * activate any pages we just allocated.
1535 */
1536
1537 for (i = 0; i < npages; i++) {
1538 if (pgs[i] == NULL) {
1539 continue;
1540 }
1541 uvm_pageactivate(pgs[i]);
1542 }
1543
1544 /*
1545 * start i/os on the pages.
1546 */
1547
1548 for (i = 0; i < npages; i++) {
1549 for (startidx = i; i < npages; i++) {
1550 if (pgs[i] == NULL) {
1551 break;
1552 }
1553 }
1554 if (i > startidx) {
1555 bytes = min((i - startidx) << PAGE_SHIFT,
1556 filesize - pgs[startidx]->offset);
1557 bytes = roundup(bytes, DEV_BSIZE);
1558
1559 UVMHIST_LOG(ubchist, "bytes i %d startidx %d "
1560 "filesize 0x%x pgoff 0x%x",
1561 i, startidx, (int)filesize,
1562 (int)pgs[startidx]->offset);
1563
1564 uvn_doasyncget(&pgs[startidx], bytes,
1565 blkno + startidx * (PAGE_SIZE >>
1566 DEV_BSHIFT));
1567 }
1568 }
1569
1570 off += npages << PAGE_SHIFT;
1571 count -= npages;
1572 return;
1573 }
1574 }
1575