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