uvm_vnode.c revision 1.22.2.1.2.7 1 /* $NetBSD: uvm_vnode.c,v 1.22.2.1.2.7 1999/08/09 00:05:56 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;
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 start = 0;
532 stop = -1;
533 by_list = TRUE; /* always go by the list */
534 } else {
535 start = trunc_page(start);
536 stop = round_page(stop);
537 if (stop > round_page(uvn->u_size)) {
538 printf("uvn_flush: oor vp %p start 0x%x stop 0x%x "
539 "size 0x%x\n", uvn, (int)start, (int)stop,
540 (int)round_page(uvn->u_size));
541 }
542
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 (pp->offset < start ||
569 (pp->offset >= stop && stop != -1))
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 (pp->offset < start || pp->offset >= stop) {
612 ppnext = TAILQ_NEXT(pp, listq);
613 continue;
614 }
615
616 } else {
617
618 /*
619 * null check
620 */
621
622 curoff += PAGE_SIZE;
623 if (pp == NULL) {
624 if (curoff < stop)
625 ppnext = uvm_pagelookup(uobj, curoff);
626 continue;
627 }
628
629 }
630
631 /*
632 * handle case where we do not need to clean page (either
633 * because we are not clean or because page is not dirty or
634 * is busy):
635 *
636 * NOTE: we are allowed to deactivate a non-wired active
637 * PG_BUSY page, but once a PG_BUSY page is on the inactive
638 * queue it must stay put until it is !PG_BUSY (so as not to
639 * confuse pagedaemon).
640 */
641
642 if ((flags & PGO_CLEANIT) == 0 || (pp->flags & PG_BUSY) != 0) {
643 needs_clean = FALSE;
644 if ((pp->flags & PG_BUSY) != 0 &&
645 (flags & (PGO_CLEANIT|PGO_SYNCIO)) ==
646 (PGO_CLEANIT|PGO_SYNCIO))
647 need_iosync = TRUE;
648 } else {
649 /*
650 * freeing: nuke all mappings so we can sync
651 * PG_CLEAN bit with no race
652 */
653 if ((pp->flags & PG_CLEAN) != 0 &&
654 (flags & PGO_FREE) != 0 &&
655 (pp->pqflags & PQ_ACTIVE) != 0)
656 pmap_page_protect(PMAP_PGARG(pp), VM_PROT_NONE);
657 if ((pp->flags & PG_CLEAN) != 0 &&
658 pmap_is_modified(PMAP_PGARG(pp)))
659 pp->flags &= ~(PG_CLEAN);
660 pp->flags |= PG_CLEANCHK; /* update "hint" */
661
662 needs_clean = ((pp->flags & PG_CLEAN) == 0);
663 }
664
665 /*
666 * if we don't need a clean... load ppnext and dispose of pp
667 */
668 if (!needs_clean) {
669 /* load ppnext */
670 if (by_list)
671 ppnext = pp->listq.tqe_next;
672 else {
673 if (curoff < stop)
674 ppnext = uvm_pagelookup(uobj, curoff);
675 }
676
677 /* now dispose of pp */
678 if (flags & PGO_DEACTIVATE) {
679 if ((pp->pqflags & PQ_INACTIVE) == 0 &&
680 pp->wire_count == 0) {
681 pmap_page_protect(PMAP_PGARG(pp),
682 VM_PROT_NONE);
683 uvm_pagedeactivate(pp);
684 }
685
686 } else if (flags & PGO_FREE) {
687 if (pp->flags & PG_BUSY) {
688 /* release busy pages */
689 pp->flags |= PG_RELEASED;
690 } else {
691 pmap_page_protect(PMAP_PGARG(pp),
692 VM_PROT_NONE);
693 /* removed page from object */
694 uvm_pagefree(pp);
695 }
696 }
697 /* ppnext is valid so we can continue... */
698 continue;
699 }
700
701 /*
702 * pp points to a page in the locked object that we are
703 * working on. if it is !PG_CLEAN,!PG_BUSY and we asked
704 * for cleaning (PGO_CLEANIT). we clean it now.
705 *
706 * let uvm_pager_put attempted a clustered page out.
707 * note: locked: uobj and page queues.
708 */
709
710 pp->flags |= PG_BUSY; /* we 'own' page now */
711 UVM_PAGE_OWN(pp, "uvn_flush");
712 pmap_page_protect(PMAP_PGARG(pp), VM_PROT_READ);
713 pp_version = pp->version;
714 ReTry:
715 ppsp = pps;
716 npages = sizeof(pps) / sizeof(struct vm_page *);
717
718 /* locked: page queues, uobj */
719 result = uvm_pager_put(uobj, pp, &ppsp, &npages,
720 flags | PGO_DOACTCLUST, start, stop);
721 /* unlocked: page queues, uobj */
722
723 /*
724 * at this point nothing is locked. if we did an async I/O
725 * it is remotely possible for the async i/o to complete and
726 * the page "pp" be freed or what not before we get a chance
727 * to relock the object. in order to detect this, we have
728 * saved the version number of the page in "pp_version".
729 */
730
731 /* relock! */
732 simple_lock(&uobj->vmobjlock);
733 uvm_lock_pageq();
734
735 /*
736 * VM_PAGER_AGAIN: given the structure of this pager, this
737 * can only happen when we are doing async I/O and can't
738 * map the pages into kernel memory (pager_map) due to lack
739 * of vm space. if this happens we drop back to sync I/O.
740 */
741
742 if (result == VM_PAGER_AGAIN) {
743 /*
744 * it is unlikely, but page could have been released
745 * while we had the object lock dropped. we ignore
746 * this now and retry the I/O. we will detect and
747 * handle the released page after the syncio I/O
748 * completes.
749 */
750 #ifdef DIAGNOSTIC
751 if (flags & PGO_SYNCIO)
752 panic("uvn_flush: PGO_SYNCIO return 'try again' error (impossible)");
753 #endif
754 flags |= PGO_SYNCIO;
755 goto ReTry;
756 }
757
758 /*
759 * the cleaning operation is now done. finish up. note that
760 * on error (!OK, !PEND) uvm_pager_put drops the cluster for us.
761 * if success (OK, PEND) then uvm_pager_put returns the cluster
762 * to us in ppsp/npages.
763 */
764
765 /*
766 * for pending async i/o if we are not deactivating/freeing
767 * we can move on to the next page.
768 */
769
770 if (result == VM_PAGER_PEND) {
771
772 if ((flags & (PGO_DEACTIVATE|PGO_FREE)) == 0) {
773 /*
774 * no per-page ops: refresh ppnext and continue
775 */
776 if (by_list) {
777 if (pp->version == pp_version)
778 ppnext = pp->listq.tqe_next;
779 else
780 /* reset */
781 ppnext = uobj->memq.tqh_first;
782 } else {
783 if (curoff < stop)
784 ppnext = uvm_pagelookup(uobj,
785 curoff);
786 }
787 continue;
788 }
789
790 /* need to do anything here? */
791 }
792
793 /*
794 * need to look at each page of the I/O operation. we defer
795 * processing "pp" until the last trip through this "for" loop
796 * so that we can load "ppnext" for the main loop after we
797 * play with the cluster pages [thus the "npages + 1" in the
798 * loop below].
799 */
800
801 for (lcv = 0 ; lcv < npages + 1 ; lcv++) {
802
803 /*
804 * handle ppnext for outside loop, and saving pp
805 * until the end.
806 */
807 if (lcv < npages) {
808 if (ppsp[lcv] == pp)
809 continue; /* skip pp until the end */
810 ptmp = ppsp[lcv];
811 } else {
812 ptmp = pp;
813
814 /* set up next page for outer loop */
815 if (by_list) {
816 if (pp->version == pp_version)
817 ppnext = pp->listq.tqe_next;
818 else
819 /* reset */
820 ppnext = uobj->memq.tqh_first;
821 } else {
822 if (curoff < stop)
823 ppnext = uvm_pagelookup(uobj, curoff);
824 }
825 }
826
827 /*
828 * verify the page didn't get moved while obj was
829 * unlocked
830 */
831 if (result == VM_PAGER_PEND && ptmp->uobject != uobj)
832 continue;
833
834 /*
835 * unbusy the page if I/O is done. note that for
836 * pending I/O it is possible that the I/O op
837 * finished before we relocked the object (in
838 * which case the page is no longer busy).
839 */
840
841 if (result != VM_PAGER_PEND) {
842 if (ptmp->flags & PG_WANTED)
843 /* still holding object lock */
844 wakeup(ptmp);
845
846 ptmp->flags &= ~(PG_WANTED|PG_BUSY);
847 UVM_PAGE_OWN(ptmp, NULL);
848 if (ptmp->flags & PG_RELEASED) {
849
850 /* pgo_releasepg wants this */
851 uvm_unlock_pageq();
852 if (!uvn_releasepg(ptmp, NULL))
853 return (TRUE);
854
855 uvm_lock_pageq(); /* relock */
856 continue; /* next page */
857
858 } else {
859 ptmp->flags |= (PG_CLEAN|PG_CLEANCHK);
860 if ((flags & PGO_FREE) == 0)
861 pmap_clear_modify(
862 PMAP_PGARG(ptmp));
863 }
864 }
865
866 /*
867 * dispose of page
868 */
869
870 if (flags & PGO_DEACTIVATE) {
871 if ((pp->pqflags & PQ_INACTIVE) == 0 &&
872 pp->wire_count == 0) {
873 pmap_page_protect(PMAP_PGARG(ptmp),
874 VM_PROT_NONE);
875 uvm_pagedeactivate(ptmp);
876 }
877
878 } else if (flags & PGO_FREE) {
879 if (result == VM_PAGER_PEND) {
880 if ((ptmp->flags & PG_BUSY) != 0)
881 /* signal for i/o done */
882 ptmp->flags |= PG_RELEASED;
883 } else {
884 if (result != VM_PAGER_OK) {
885 printf("uvn_flush: obj=%p, "
886 "offset=0x%llx. error %d\n",
887 pp->uobject,
888 (long long)pp->offset,
889 result);
890 printf("uvn_flush: WARNING: "
891 "changes to page may be "
892 "lost!\n");
893 retval = FALSE;
894 }
895 pmap_page_protect(PMAP_PGARG(ptmp),
896 VM_PROT_NONE);
897 uvm_pagefree(ptmp);
898 }
899 }
900
901 } /* end of "lcv" for loop */
902
903 } /* end of "pp" for loop */
904
905 /*
906 * done with pagequeues: unlock
907 */
908 uvm_unlock_pageq();
909
910 /*
911 * now wait for all I/O if required.
912 */
913 if (need_iosync) {
914 UVMHIST_LOG(maphist," <<DOING IOSYNC>>",0,0,0,0);
915
916 /*
917 * XXX this doesn't use the new two-flag scheme,
918 * but to use that, all i/o initiators will have to change.
919 */
920
921 s = splbio();
922 while (vp->v_numoutput != 0) {
923 UVMHIST_LOG(ubchist, "waiting for vp %p num %d",
924 vp, vp->v_numoutput,0,0);
925
926 vp->v_flag |= VBWAIT;
927 UVM_UNLOCK_AND_WAIT(&vp->v_numoutput,
928 &uvn->u_obj.vmobjlock,
929 FALSE, "uvn_flush",0);
930 simple_lock(&uvn->u_obj.vmobjlock);
931 }
932 splx(s);
933 }
934
935 /* return, with object locked! */
936 UVMHIST_LOG(maphist,"<- done (retval=0x%x)",retval,0,0,0);
937 return(retval);
938 }
939
940 /*
941 * uvn_cluster
942 *
943 * we are about to do I/O in an object at offset. this function is called
944 * to establish a range of offsets around "offset" in which we can cluster
945 * I/O.
946 *
947 * - currently doesn't matter if obj locked or not.
948 */
949
950 static void
951 uvn_cluster(uobj, offset, loffset, hoffset)
952 struct uvm_object *uobj;
953 voff_t offset;
954 voff_t *loffset, *hoffset; /* OUT */
955 {
956 struct uvm_vnode *uvn = (struct uvm_vnode *)uobj;
957
958 *loffset = offset;
959 *hoffset = min(offset + MAXBSIZE, round_page(uvn->u_size));
960 }
961
962 /*
963 * uvn_put: flush page data to backing store.
964 *
965 * => object must be locked! we will _unlock_ it before starting I/O.
966 * => flags: PGO_SYNCIO -- use sync. I/O
967 * => note: caller must set PG_CLEAN and pmap_clear_modify (if needed)
968 */
969
970 static int
971 uvn_put(uobj, pps, npages, flags)
972 struct uvm_object *uobj;
973 struct vm_page **pps;
974 int npages, flags;
975 {
976 struct vnode *vp = (struct vnode *)uobj;
977 int error, sync;
978
979 sync = (flags & PGO_SYNCIO) ? 1 : 0;
980
981 simple_unlock(&uobj->vmobjlock);
982 error = VOP_PUTPAGES(vp, pps, npages, sync, NULL);
983
984 return uvm_errno2vmerror(error);
985 }
986
987
988 /*
989 * uvn_get: get pages (synchronously) from backing store
990 *
991 * => prefer map unlocked (not required)
992 * => object must be locked! we will _unlock_ it before starting any I/O.
993 * => flags: PGO_ALLPAGES: get all of the pages
994 * PGO_LOCKED: fault data structures are locked
995 * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx]
996 * => NOTE: caller must check for released pages!!
997 */
998
999 static int
1000 uvn_get(uobj, offset, pps, npagesp, centeridx, access_type, advice, flags)
1001 struct uvm_object *uobj;
1002 voff_t offset;
1003 struct vm_page **pps; /* IN/OUT */
1004 int *npagesp; /* IN (OUT if PGO_LOCKED) */
1005 int centeridx;
1006 vm_prot_t access_type;
1007 int advice, flags;
1008 {
1009 struct vnode *vp = (struct vnode *)uobj;
1010 int error;
1011 UVMHIST_FUNC("uvn_get"); UVMHIST_CALLED(ubchist);
1012 UVMHIST_LOG(ubchist, "vp %p off 0x%x", vp, (int)offset, 0,0);
1013
1014 error = VOP_GETPAGES(vp, offset, pps, npagesp, centeridx,
1015 access_type, advice, flags);
1016 return uvm_errno2vmerror(error);
1017 }
1018
1019 /*
1020 * uvn_findpages:
1021 * return the page for the uobj and offset requested, allocating if needed.
1022 * => uobj must be locked.
1023 * => returned page will be BUSY.
1024 */
1025
1026 void
1027 uvn_findpages(uobj, offset, npagesp, pps, flags)
1028 struct uvm_object *uobj;
1029 voff_t offset;
1030 int *npagesp;
1031 struct vm_page **pps;
1032 int flags;
1033 {
1034 int i, rv, npages;
1035
1036 rv = 0;
1037 npages = *npagesp;
1038 for (i = 0; i < npages; i++, offset += PAGE_SIZE) {
1039 rv += uvn_findpage(uobj, offset, &pps[i], flags);
1040 }
1041 *npagesp = rv;
1042 }
1043
1044
1045 static int
1046 uvn_findpage(uobj, offset, pps, flags)
1047 struct uvm_object *uobj;
1048 voff_t offset;
1049 struct vm_page **pps;
1050 int flags;
1051 {
1052 struct vm_page *ptmp;
1053 UVMHIST_FUNC("uvn_findpage"); UVMHIST_CALLED(ubchist);
1054 UVMHIST_LOG(ubchist, "vp %p off 0x%lx", uobj, offset,0,0);
1055
1056 if (*pps != NULL) {
1057 UVMHIST_LOG(ubchist, "dontcare", 0,0,0,0);
1058 return 0;
1059 }
1060 for (;;) {
1061 /* look for an existing page */
1062 ptmp = uvm_pagelookup(uobj, offset);
1063
1064 /* nope? allocate one now */
1065 if (ptmp == NULL) {
1066 if (flags & UFP_NOALLOC) {
1067 UVMHIST_LOG(ubchist, "noalloc", 0,0,0,0);
1068 return 0;
1069 }
1070 ptmp = uvm_pagealloc(uobj, offset, NULL, 0);
1071 if (ptmp == NULL) {
1072 if (flags & UFP_NOWAIT) {
1073 UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
1074 return 0;
1075 }
1076 simple_unlock(&uobj->vmobjlock);
1077 uvm_wait("uvn_fp1");
1078 simple_lock(&uobj->vmobjlock);
1079 continue;
1080 }
1081 UVMHIST_LOG(ubchist, "alloced",0,0,0,0);
1082 break;
1083 } else if (flags & UFP_NOCACHE) {
1084 UVMHIST_LOG(ubchist, "nocache",0,0,0,0);
1085 return 0;
1086 }
1087
1088 /* page is there, see if we need to wait on it */
1089 if ((ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
1090 if (flags & UFP_NOWAIT) {
1091 UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
1092 return 0;
1093 }
1094 ptmp->flags |= PG_WANTED;
1095 UVM_UNLOCK_AND_WAIT(ptmp, &uobj->vmobjlock, 0,
1096 "uvn_fp2",0);
1097 simple_lock(&uobj->vmobjlock);
1098 continue;
1099 }
1100
1101 /* skip PG_RDONLY pages if requested */
1102 if ((flags & UFP_NORDONLY) && (ptmp->flags & PG_RDONLY)) {
1103 UVMHIST_LOG(ubchist, "nordonly",0,0,0,0);
1104 return 0;
1105 }
1106 /* BUSY the page and we're done. */
1107 ptmp->flags |= PG_BUSY;
1108 UVM_PAGE_OWN(ptmp, "uvn_findpage");
1109 UVMHIST_LOG(ubchist, "found",0,0,0,0);
1110 break;
1111 }
1112 *pps = ptmp;
1113 return 1;
1114 }
1115
1116 /*
1117 * uvm_vnp_setsize: grow or shrink a vnode uvn
1118 *
1119 * grow => just update size value
1120 * shrink => toss un-needed pages
1121 *
1122 * => we assume that the caller has a reference of some sort to the
1123 * vnode in question so that it will not be yanked out from under
1124 * us.
1125 *
1126 * called from:
1127 * => truncate fns (ext2fs_truncate, ffs_truncate, detrunc[msdos])
1128 * => "write" fns (ext2fs_write, WRITE [ufs/ufs], msdosfs_write, nfs_write)
1129 * => ffs_balloc [XXX: why? doesn't WRITE handle?]
1130 * => NFS: nfs_loadattrcache, nfs_getattrcache, nfs_setattr
1131 * => union fs: union_newsize
1132 */
1133
1134 void
1135 uvm_vnp_setsize(vp, newsize)
1136 struct vnode *vp;
1137 voff_t newsize;
1138 {
1139 struct uvm_vnode *uvn = &vp->v_uvm;
1140
1141 /*
1142 * lock uvn and check for valid object, and if valid: do it!
1143 */
1144 simple_lock(&uvn->u_obj.vmobjlock);
1145
1146 /*
1147 * now check if the size has changed: if we shrink we had better
1148 * toss some pages...
1149 */
1150
1151 if (uvn->u_size > newsize && uvn->u_size != VSIZENOTSET) {
1152 (void) uvn_flush(&uvn->u_obj, newsize, uvn->u_size, PGO_FREE);
1153 }
1154 uvn->u_size = newsize;
1155 simple_unlock(&uvn->u_obj.vmobjlock);
1156 }
1157
1158 /*
1159 * uvm_vnp_sync: flush all dirty VM pages back to their backing vnodes.
1160 *
1161 * => called from sys_sync with no VM structures locked
1162 * => only one process can do a sync at a time (because the uvn
1163 * structure only has one queue for sync'ing). we ensure this
1164 * by holding the uvn_sync_lock while the sync is in progress.
1165 * other processes attempting a sync will sleep on this lock
1166 * until we are done.
1167 */
1168
1169 void
1170 uvm_vnp_sync(mp)
1171 struct mount *mp;
1172 {
1173 struct uvm_vnode *uvn;
1174 struct vnode *vp;
1175 boolean_t got_lock;
1176
1177 /*
1178 * step 1: ensure we are only ones using the uvn_sync_q by locking
1179 * our lock...
1180 */
1181 lockmgr(&uvn_sync_lock, LK_EXCLUSIVE, (void *)0);
1182
1183 /*
1184 * step 2: build up a simpleq of uvns of interest based on the
1185 * write list. we gain a reference to uvns of interest. must
1186 * be careful about locking uvn's since we will be holding uvn_wl_lock
1187 * in the body of the loop.
1188 */
1189 SIMPLEQ_INIT(&uvn_sync_q);
1190 simple_lock(&uvn_wl_lock);
1191 for (uvn = LIST_FIRST(&uvn_wlist); uvn != NULL;
1192 uvn = LIST_NEXT(uvn, u_wlist)) {
1193
1194 vp = (struct vnode *) uvn;
1195 if (mp && vp->v_mount != mp)
1196 continue;
1197
1198 /* attempt to gain reference */
1199 while ((got_lock = simple_lock_try(&uvn->u_obj.vmobjlock)) ==
1200 FALSE &&
1201 (uvn->u_flags & VXLOCK) == 0)
1202 /* spin */ ;
1203
1204 /*
1205 * we will exit the loop if either if the following are true:
1206 * - we got the lock [always true if NCPU == 1]
1207 * - we failed to get the lock but noticed the vnode was
1208 * "blocked" -- in this case the vnode must be a dying
1209 * vnode, and since dying vnodes are in the process of
1210 * being flushed out, we can safely skip this one
1211 *
1212 * we want to skip over the vnode if we did not get the lock,
1213 * or if the vnode is already dying (due to the above logic).
1214 *
1215 * note that uvn must already be valid because we found it on
1216 * the wlist (this also means it can't be ALOCK'd).
1217 */
1218 if (!got_lock || (uvn->u_flags & VXLOCK) != 0) {
1219 if (got_lock)
1220 simple_unlock(&uvn->u_obj.vmobjlock);
1221 continue; /* skip it */
1222 }
1223
1224 vget(vp, LK_INTERLOCK);
1225
1226 /*
1227 * got it!
1228 */
1229 SIMPLEQ_INSERT_HEAD(&uvn_sync_q, uvn, u_syncq);
1230 }
1231 simple_unlock(&uvn_wl_lock);
1232
1233 /*
1234 * step 3: we now have a list of uvn's that may need cleaning.
1235 * we are holding the uvn_sync_lock, but have dropped the uvn_wl_lock
1236 * (so we can now safely lock uvn's again).
1237 */
1238
1239 for (uvn = uvn_sync_q.sqh_first ; uvn ; uvn = uvn->u_syncq.sqe_next) {
1240 simple_lock(&uvn->u_obj.vmobjlock);
1241 uvn_flush(&uvn->u_obj, 0, 0,
1242 PGO_CLEANIT|PGO_ALLPAGES|PGO_DOACTCLUST);
1243
1244 /*
1245 * if we have the only reference and we just cleaned the uvn,
1246 * then we can pull it out of the VDIRTY state
1247 * thus allowing us to avoid thinking about flushing it again
1248 * on later sync ops.
1249 */
1250 if (uvn->u_obj.uo_refs == 1 && (uvn->u_flags & VDIRTY)) {
1251 simple_lock(&uvn_wl_lock);
1252 LIST_REMOVE(uvn, u_wlist);
1253 uvn->u_flags &= ~VDIRTY;
1254 simple_unlock(&uvn_wl_lock);
1255 }
1256
1257 simple_unlock(&uvn->u_obj.vmobjlock);
1258
1259 /* now drop our reference to the uvn */
1260 uvn_detach(&uvn->u_obj);
1261 }
1262
1263 /*
1264 * done! release sync lock
1265 */
1266 lockmgr(&uvn_sync_lock, LK_RELEASE, (void *)0);
1267 }
1268
1269
1270 /*
1271 * uvm_vnp_zerorange: set a range of bytes in a file to zero.
1272 */
1273
1274 void
1275 uvm_vnp_zerorange(vp, off, len)
1276 struct vnode *vp;
1277 off_t off;
1278 size_t len;
1279 {
1280 void *win;
1281
1282 /*
1283 * XXX invent kzero() and use it
1284 */
1285
1286 while (len) {
1287 vsize_t bytelen = len;
1288
1289 win = ubc_alloc(&vp->v_uvm.u_obj, off, &bytelen, UBC_WRITE);
1290 memset(win, 0, bytelen);
1291 ubc_release(win, 0);
1292
1293 off += bytelen;
1294 len -= bytelen;
1295 }
1296 }
1297
1298 /*
1299 * uvn_doasyncget: start one readahead i/o.
1300 */
1301
1302 static void
1303 uvn_doasyncget(pgs, bytes, blkno)
1304 struct vm_page **pgs;
1305 size_t bytes;
1306 daddr_t blkno;
1307 {
1308 struct buf *bp;
1309 struct vnode *vp = (struct vnode *)pgs[0]->uobject;
1310 int pages = roundup(bytes, PAGE_SIZE) >> PAGE_SHIFT;
1311 int s;
1312 UVMHIST_FUNC("uvn_doasyncget"); UVMHIST_CALLED(ubchist);
1313
1314 UVMHIST_LOG(ubchist, "vp %p offset 0x%x bytes 0x%x blkno 0x%x",
1315 vp, (int)pgs[0]->offset, (int)bytes, (int)blkno);
1316
1317 s = splbio();
1318 bp = pool_get(&bufpool, PR_WAITOK);
1319 splx(s);
1320 bp->b_data = (void *)uvm_pagermapin(pgs, pages, M_WAITOK);
1321 bp->b_flags = B_BUSY|B_READ|B_CALL|B_ASYNC;
1322 bp->b_iodone = uvm_aio_biodone;
1323 bp->b_lblkno = 0;
1324 bp->b_blkno = blkno;
1325 bp->b_bufsize = pages << PAGE_SHIFT;
1326 bp->b_bcount = bytes;
1327 bp->b_vp = vp;
1328 UVMHIST_LOG(ubchist, "bp %p", bp, 0,0,0);
1329
1330 VOP_STRATEGY(bp);
1331 }
1332
1333 #define MAXRAPAGES 16
1334
1335 /*
1336 * asynchronously create pages for a vnode and read their data.
1337 */
1338
1339 void
1340 uvm_vnp_asyncget(vp, off, len)
1341 struct vnode *vp;
1342 off_t off;
1343 size_t len;
1344 {
1345 off_t filesize = vp->v_uvm.u_size;
1346 struct vm_page *pgs[MAXRAPAGES];
1347 struct uvm_object *uobj = &vp->v_uvm.u_obj;
1348 daddr_t lbn, blkno;
1349 int bshift = vp->v_mount->mnt_fs_bshift;
1350 int dev_bshift = vp->v_mount->mnt_dev_bshift;
1351 int i, npages, npgs, startidx, run, bytes, startpage, endpage;
1352 int count;
1353 UVMHIST_FUNC("uvn_asyncget"); UVMHIST_CALLED(ubchist);
1354
1355 if (off != trunc_page(off)) {
1356 panic("off 0x%x not page-aligned", (int)off);
1357 }
1358
1359 UVMHIST_LOG(ubchist, "asyncget off 0x%x len 0x%x",
1360 (int)off, (int)len,0,0);
1361
1362 count = round_page(len) >> PAGE_SHIFT;
1363 while (count > 0) {
1364 if (off >= filesize) {
1365 return;
1366 }
1367
1368 lbn = off >> bshift;
1369 if (VOP_BMAP(vp, lbn, NULL, &blkno, &run) != 0) {
1370 return;
1371 }
1372
1373 UVMHIST_LOG(ubchist, "bmap lbn 0x%x bn 0x%x",
1374 (int)lbn, (int)blkno,0,0);
1375
1376 /* don't do readahead past file holes... */
1377 if (blkno == (daddr_t)-1) {
1378 return;
1379 }
1380
1381 startpage = off >> PAGE_SHIFT;
1382 endpage = min(roundup(off + 1 + (run << bshift), 1 << bshift),
1383 round_page(filesize)) >> PAGE_SHIFT;
1384 npages = min(endpage - startpage, min(count, MAXRAPAGES));
1385
1386 UVMHIST_LOG(ubchist, "off 0x%x run 0x%x "
1387 "startpage %d endpage %d",
1388 (int)off, run, startpage, endpage);
1389 UVMHIST_LOG(ubchist, "runend 0x%x fileend 0x%x sum 0x%x",
1390 (int)roundup(off + 1 + (run << bshift),
1391 (1 << bshift)),
1392 (int)round_page(filesize),
1393 (int)(off + 1 + (run << bshift)), 0);
1394
1395 if (npages == 0) {
1396 return;
1397 }
1398
1399 memset(pgs, 0, npages * sizeof(pgs[0]));
1400
1401 simple_lock(&uobj->vmobjlock);
1402 npgs = npages;
1403 uvn_findpages(uobj, off, &npgs, pgs, UFP_NOWAIT | UFP_NOCACHE);
1404 simple_unlock(&uobj->vmobjlock);
1405
1406 blkno += (off - (lbn << bshift)) >> dev_bshift;
1407
1408 /*
1409 * activate any pages we just allocated.
1410 */
1411
1412 for (i = 0; i < npages; i++) {
1413 if (pgs[i] == NULL) {
1414 continue;
1415 }
1416 uvm_pageactivate(pgs[i]);
1417 }
1418
1419 /*
1420 * start i/os on the pages.
1421 */
1422
1423 for (i = 0; i < npages; i++) {
1424 for (startidx = i; i < npages; i++) {
1425 if (pgs[i] == NULL) {
1426 break;
1427 }
1428 }
1429 if (i > startidx) {
1430 bytes = min((i - startidx) << PAGE_SHIFT,
1431 filesize - pgs[startidx]->offset);
1432 bytes = roundup(bytes, 1 << dev_bshift);
1433
1434 UVMHIST_LOG(ubchist, "bytes i %d startidx %d "
1435 "filesize 0x%x pgoff 0x%x",
1436 i, startidx, (int)filesize,
1437 (int)pgs[startidx]->offset);
1438
1439 uvn_doasyncget(&pgs[startidx], bytes,
1440 blkno + startidx *
1441 (PAGE_SIZE >> dev_bshift));
1442 }
1443 }
1444
1445 off += npages << PAGE_SHIFT;
1446 count -= npages;
1447
1448 /* XXX for now, don't loop */
1449 return;
1450 }
1451 }
1452