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