MAP_VPAGETABLE support part 1/3.
[dragonfly.git] / sys / vm / vm_fault.c
1 /*
2  * Copyright (c) 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1994 John S. Dyson
5  * All rights reserved.
6  * Copyright (c) 1994 David Greenman
7  * All rights reserved.
8  *
9  *
10  * This code is derived from software contributed to Berkeley by
11  * The Mach Operating System project at Carnegie-Mellon University.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *      This product includes software developed by the University of
24  *      California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *      from: @(#)vm_fault.c    8.4 (Berkeley) 1/12/94
42  *
43  *
44  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
45  * All rights reserved.
46  *
47  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
48  *
49  * Permission to use, copy, modify and distribute this software and
50  * its documentation is hereby granted, provided that both the copyright
51  * notice and this permission notice appear in all copies of the
52  * software, derivative works or modified versions, and any portions
53  * thereof, and that both notices appear in supporting documentation.
54  *
55  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
57  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58  *
59  * Carnegie Mellon requests users of this software to return to
60  *
61  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
62  *  School of Computer Science
63  *  Carnegie Mellon University
64  *  Pittsburgh PA 15213-3890
65  *
66  * any improvements or extensions that they make and grant Carnegie the
67  * rights to redistribute these changes.
68  *
69  * $FreeBSD: src/sys/vm/vm_fault.c,v 1.108.2.8 2002/02/26 05:49:27 silby Exp $
70  * $DragonFly: src/sys/vm/vm_fault.c,v 1.26 2006/09/12 18:41:32 dillon Exp $
71  */
72
73 /*
74  *      Page fault handling module.
75  */
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/proc.h>
81 #include <sys/vnode.h>
82 #include <sys/resourcevar.h>
83 #include <sys/vmmeter.h>
84
85 #include <vm/vm.h>
86 #include <vm/vm_param.h>
87 #include <sys/lock.h>
88 #include <vm/pmap.h>
89 #include <vm/vm_map.h>
90 #include <vm/vm_object.h>
91 #include <vm/vm_page.h>
92 #include <vm/vm_pageout.h>
93 #include <vm/vm_kern.h>
94 #include <vm/vm_pager.h>
95 #include <vm/vnode_pager.h>
96 #include <vm/vm_extern.h>
97
98 #include <sys/thread2.h>
99 #include <vm/vm_page2.h>
100
101 #define VM_FAULT_READ_AHEAD 8
102 #define VM_FAULT_READ_BEHIND 7
103 #define VM_FAULT_READ (VM_FAULT_READ_AHEAD+VM_FAULT_READ_BEHIND+1)
104
105 struct faultstate {
106         vm_page_t m;
107         vm_object_t object;
108         vm_pindex_t pindex;
109         vm_page_t first_m;
110         vm_object_t first_object;
111         vm_pindex_t first_pindex;
112         vm_map_t map;
113         vm_map_entry_t entry;
114         int lookup_still_valid;
115         int didlimit;
116         int hardfault;
117         vm_prot_t prot;
118         int fault_flags;
119         int map_generation;
120         boolean_t wired;
121         struct vnode *vp;
122 };
123
124 static int vm_fault_object(struct faultstate *fs, vm_prot_t);
125 static int vm_fault_additional_pages (vm_page_t, int, int, vm_page_t *, int *);
126 static int vm_fault_ratelimit(struct vmspace *vmspace);
127
128 static __inline void
129 release_page(struct faultstate *fs)
130 {
131         vm_page_wakeup(fs->m);
132         vm_page_deactivate(fs->m);
133         fs->m = NULL;
134 }
135
136 static __inline void
137 unlock_map(struct faultstate *fs)
138 {
139         if (fs->lookup_still_valid) {
140                 vm_map_lookup_done(fs->map, fs->entry, 0);
141                 fs->lookup_still_valid = FALSE;
142         }
143 }
144
145 static void
146 _unlock_things(struct faultstate *fs, int dealloc)
147 {
148         vm_object_pip_wakeup(fs->object);
149         if (fs->object != fs->first_object) {
150                 vm_page_free(fs->first_m);
151                 vm_object_pip_wakeup(fs->first_object);
152                 fs->first_m = NULL;
153         }
154         if (dealloc) {
155                 vm_object_deallocate(fs->first_object);
156         }
157         unlock_map(fs); 
158         if (fs->vp != NULL) { 
159                 vput(fs->vp);
160                 fs->vp = NULL;
161         }
162 }
163
164 #define unlock_things(fs) _unlock_things(fs, 0)
165 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
166
167 /*
168  * TRYPAGER 
169  *
170  * Determine if the pager for the current object *might* contain the page.
171  *
172  * We only need to try the pager if this is not a default object (default
173  * objects are zero-fill and have no real pager), and if we are not taking
174  * a wiring fault or if the FS entry is wired.
175  */
176 #define TRYPAGER(fs)    \
177                 (fs->object->type != OBJT_DEFAULT && \
178                 (((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || fs->wired))
179
180 /*
181  * vm_fault:
182  *
183  * Handle a page fault occuring at the given address, requiring the given
184  * permissions, in the map specified.  If successful, the page is inserted
185  * into the associated physical map.
186  *
187  * NOTE: The given address should be truncated to the proper page address.
188  *
189  * KERN_SUCCESS is returned if the page fault is handled; otherwise,
190  * a standard error specifying why the fault is fatal is returned.
191  *
192  * The map in question must be referenced, and remains so.
193  * The caller may hold no locks.
194  */
195 int
196 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags)
197 {
198         int result;
199         struct faultstate fs;
200
201         mycpu->gd_cnt.v_vm_faults++;
202
203         fs.didlimit = 0;
204         fs.hardfault = 0;
205         fs.fault_flags = fault_flags;
206
207 RetryFault:
208         /*
209          * Find the vm_map_entry representing the backing store and resolve
210          * the top level object and page index.  This may have the side
211          * effect of executing a copy-on-write on the map entry and/or
212          * creating a shadow object, but will not COW any actual VM pages.
213          *
214          * On success fs.map is left read-locked and various other fields 
215          * are initialized but not otherwise referenced or locked.
216          *
217          * NOTE!  vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
218          * if the map entry is a virtual page table and also writable,
219          * so we can set the 'A'accessed bit in the virtual page table entry.
220          */
221         fs.map = map;
222         result = vm_map_lookup(&fs.map, vaddr, fault_type,
223                                &fs.entry, &fs.first_object,
224                                &fs.first_pindex, &fs.prot, &fs.wired);
225
226         /*
227          * If the lookup failed or the map protections are incompatible,
228          * the fault generally fails.  However, if the caller is trying
229          * to do a user wiring we have more work to do.
230          */
231         if (result != KERN_SUCCESS) {
232                 if (result != KERN_PROTECTION_FAILURE)
233                         return result;
234                 if ((fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)
235                         return result;
236
237                 /*
238                  * If we are user-wiring a r/w segment, and it is COW, then
239                  * we need to do the COW operation.  Note that we don't
240                  * currently COW RO sections now, because it is NOT desirable
241                  * to COW .text.  We simply keep .text from ever being COW'ed
242                  * and take the heat that one cannot debug wired .text sections.
243                  */
244                 result = vm_map_lookup(&fs.map, vaddr,
245                                        VM_PROT_READ|VM_PROT_WRITE|
246                                         VM_PROT_OVERRIDE_WRITE,
247                                        &fs.entry, &fs.first_object,
248                                        &fs.first_pindex, &fs.prot, &fs.wired);
249                 if (result != KERN_SUCCESS)
250                         return result;
251
252                 /*
253                  * If we don't COW now, on a user wire, the user will never
254                  * be able to write to the mapping.  If we don't make this
255                  * restriction, the bookkeeping would be nearly impossible.
256                  */
257                 if ((fs.entry->protection & VM_PROT_WRITE) == 0)
258                         fs.entry->max_protection &= ~VM_PROT_WRITE;
259         }
260
261         /*
262          * fs.map is read-locked
263          *
264          * Misc checks.  Save the map generation number to detect races.
265          */
266         fs.map_generation = fs.map->timestamp;
267
268         if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
269                 panic("vm_fault: fault on nofault entry, addr: %lx",
270                     (u_long)vaddr);
271         }
272
273         /*
274          * A system map entry may return a NULL object.  No object means
275          * no pager means an unrecoverable kernel fault.
276          */
277         if (fs.first_object == NULL) {
278                 panic("vm_fault: unrecoverable fault at %p in entry %p",
279                         (void *)vaddr, fs.entry);
280         }
281
282         /*
283          * Make a reference to this object to prevent its disposal while we
284          * are messing with it.  Once we have the reference, the map is free
285          * to be diddled.  Since objects reference their shadows (and copies),
286          * they will stay around as well.
287          *
288          * Bump the paging-in-progress count to prevent size changes (e.g.
289          * truncation operations) during I/O.  This must be done after
290          * obtaining the vnode lock in order to avoid possible deadlocks.
291          */
292         vm_object_reference(fs.first_object);
293         fs.vp = vnode_pager_lock(fs.first_object);
294         vm_object_pip_add(fs.first_object, 1);
295
296         fs.lookup_still_valid = TRUE;
297         fs.first_m = NULL;
298
299         /*
300          * If the entry is wired we cannot change the page protection.
301          */
302         if (fs.wired)
303                 fault_type = fs.prot;
304
305         /*
306          * The page we want is at (object, pindex), but if the vm_map_entry
307          * is VM_MAPTYPE_VPAGETABLE we have to traverse the page table to
308          * figure out the actual pindex.
309          */
310         fs.object = fs.first_object;
311         fs.pindex = fs.first_pindex;
312 #if 0
313         /* COMING SOON */
314         if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
315                 fs.pindex = fs.entry->avail_ssize;      /* page directory */
316                 result = vm_fault_object(&fs, fault_type);
317                 if (result == KERN_TRY_AGAIN)
318                         goto RetryFault;
319                 if (result != KERN_SUCCESS) {
320                         /* unlock_and_deallocate(&fs); */
321                         return (result);
322                 }
323
324                 /*
325                  * fs.m is busy on return
326                  */
327                 XXX
328         }
329 #endif
330
331         /*
332          * Now we have the actual (object, pindex), fault in the page.  If
333          * vm_fault_object() fails it will unlock and deallocate the FS
334          * data.  
335          */
336         result = vm_fault_object(&fs, fault_type);
337         if (result == KERN_TRY_AGAIN)
338                 goto RetryFault;
339         if (result != KERN_SUCCESS)
340                 return (result);
341
342         /*
343          * On success vm_fault_object() unlocks but does not deallocate, and
344          * fs.m will contain a busied page.
345          *
346          * Enter the page into the pmap and do pmap-related adjustments.
347          */
348         pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired);
349
350         if (((fs.fault_flags & VM_FAULT_WIRE_MASK) == 0) && (fs.wired == 0)) {
351                 pmap_prefault(fs.map->pmap, vaddr, fs.entry);
352         }
353
354         vm_page_flag_clear(fs.m, PG_ZERO);
355         vm_page_flag_set(fs.m, PG_MAPPED|PG_REFERENCED);
356         if (fs.fault_flags & VM_FAULT_HOLD)
357                 vm_page_hold(fs.m);
358
359         /*
360          * If the page is not wired down, then put it where the pageout daemon
361          * can find it.
362          */
363         if (fs.fault_flags & VM_FAULT_WIRE_MASK) {
364                 if (fs.wired)
365                         vm_page_wire(fs.m);
366                 else
367                         vm_page_unwire(fs.m, 1);
368         } else {
369                 vm_page_activate(fs.m);
370         }
371
372         if (curproc && (curproc->p_flag & P_SWAPPEDOUT) == 0 &&
373             curproc->p_stats) {
374                 if (fs.hardfault) {
375                         curproc->p_stats->p_ru.ru_majflt++;
376                 } else {
377                         curproc->p_stats->p_ru.ru_minflt++;
378                 }
379         }
380
381         /*
382          * Unlock everything, and return
383          */
384         vm_page_wakeup(fs.m);
385         vm_object_deallocate(fs.first_object);
386
387         return (KERN_SUCCESS);
388 }
389
390 /*
391  * Do all operations required to fault in (fs.object, fs.pindex).  Run
392  * through the shadow chain as necessary and do required COW or virtual
393  * copy operations.  The caller has already fully resolved the vm_map_entry
394  * and, if appropriate, has created a copy-on-write layer.  All we need to
395  * do is iterate the object chain.
396  *
397  * On failure (fs) is unlocked and deallocated and the caller may return or
398  * retry depending on the failure code.  On success (fs) is unlocked but not
399  * deallocated, and fs.m will contain a resolved busied page.
400  */
401 static
402 int
403 vm_fault_object(struct faultstate *fs, vm_prot_t fault_type)
404 {
405         vm_object_t next_object;
406         vm_page_t marray[VM_FAULT_READ];
407         int faultcount;
408
409         for (;;) {
410                 /*
411                  * If the object is dead, we stop here
412                  */
413                 if (fs->object->flags & OBJ_DEAD) {
414                         unlock_and_deallocate(fs);
415                         return (KERN_PROTECTION_FAILURE);
416                 }
417
418                 /*
419                  * See if page is resident.  spl protection is required
420                  * to avoid an interrupt unbusy/free race against our
421                  * lookup.  We must hold the protection through a page
422                  * allocation or busy.
423                  */
424                 crit_enter();
425                 fs->m = vm_page_lookup(fs->object, fs->pindex);
426                 if (fs->m != NULL) {
427                         int queue;
428                         /*
429                          * Wait/Retry if the page is busy.  We have to do this
430                          * if the page is busy via either PG_BUSY or 
431                          * vm_page_t->busy because the vm_pager may be using
432                          * vm_page_t->busy for pageouts ( and even pageins if
433                          * it is the vnode pager ), and we could end up trying
434                          * to pagein and pageout the same page simultaneously.
435                          *
436                          * We can theoretically allow the busy case on a read
437                          * fault if the page is marked valid, but since such
438                          * pages are typically already pmap'd, putting that
439                          * special case in might be more effort then it is 
440                          * worth.  We cannot under any circumstances mess
441                          * around with a vm_page_t->busy page except, perhaps,
442                          * to pmap it.
443                          */
444                         if ((fs->m->flags & PG_BUSY) || fs->m->busy) {
445                                 unlock_things(fs);
446                                 vm_page_sleep_busy(fs->m, TRUE, "vmpfw");
447                                 mycpu->gd_cnt.v_intrans++;
448                                 vm_object_deallocate(fs->first_object);
449                                 crit_exit();
450                                 return (KERN_TRY_AGAIN);
451                         }
452
453                         /*
454                          * If reactivating a page from PQ_CACHE we may have
455                          * to rate-limit.
456                          */
457                         queue = fs->m->queue;
458                         vm_page_unqueue_nowakeup(fs->m);
459
460                         if ((queue - fs->m->pc) == PQ_CACHE && 
461                             vm_page_count_severe()) {
462                                 vm_page_activate(fs->m);
463                                 unlock_and_deallocate(fs);
464                                 vm_waitpfault();
465                                 crit_exit();
466                                 return (KERN_TRY_AGAIN);
467                         }
468
469                         /*
470                          * Mark page busy for other processes, and the 
471                          * pagedaemon.  If it still isn't completely valid
472                          * (readable), jump to readrest, else we found the
473                          * page and can return.
474                          *
475                          * We can release the spl once we have marked the
476                          * page busy.
477                          */
478                         vm_page_busy(fs->m);
479                         crit_exit();
480
481                         if (((fs->m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) &&
482                             fs->m->object != kernel_object &&
483                             fs->m->object != kmem_object) {
484                                 goto readrest;
485                         }
486                         break; /* break to PAGE HAS BEEN FOUND */
487                 }
488
489                 /*
490                  * Page is not resident, If this is the search termination
491                  * or the pager might contain the page, allocate a new page.
492                  *
493                  * NOTE: We are still in a critical section.
494                  */
495                 if (TRYPAGER(fs) || fs->object == fs->first_object) {
496                         /*
497                          * If the page is beyond the object size we fail
498                          */
499                         if (fs->pindex >= fs->object->size) {
500                                 crit_exit();
501                                 unlock_and_deallocate(fs);
502                                 return (KERN_PROTECTION_FAILURE);
503                         }
504
505                         /*
506                          * Ratelimit.
507                          */
508                         if (fs->didlimit == 0 && curproc != NULL) {
509                                 int limticks;
510
511                                 limticks = vm_fault_ratelimit(curproc->p_vmspace);
512                                 if (limticks) {
513                                         crit_exit();
514                                         unlock_and_deallocate(fs);
515                                         tsleep(curproc, 0, "vmrate", limticks);
516                                         fs->didlimit = 1;
517                                         return (KERN_TRY_AGAIN);
518                                 }
519                         }
520
521                         /*
522                          * Allocate a new page for this object/offset pair.
523                          */
524                         fs->m = NULL;
525                         if (!vm_page_count_severe()) {
526                                 fs->m = vm_page_alloc(fs->object, fs->pindex,
527                                     (fs->vp || fs->object->backing_object) ? VM_ALLOC_NORMAL : VM_ALLOC_NORMAL | VM_ALLOC_ZERO);
528                         }
529                         if (fs->m == NULL) {
530                                 crit_exit();
531                                 unlock_and_deallocate(fs);
532                                 vm_waitpfault();
533                                 return (KERN_TRY_AGAIN);
534                         }
535                 }
536                 crit_exit();
537
538 readrest:
539                 /*
540                  * We have found a valid page or we have allocated a new page.
541                  * The page thus may not be valid or may not be entirely 
542                  * valid.
543                  *
544                  * Attempt to fault-in the page if there is a chance that the
545                  * pager has it, and potentially fault in additional pages
546                  * at the same time.
547                  *
548                  * We are NOT in splvm here and if TRYPAGER is true then
549                  * fs.m will be non-NULL and will be PG_BUSY for us.
550                  */
551
552                 if (TRYPAGER(fs)) {
553                         int rv;
554                         int reqpage;
555                         int ahead, behind;
556                         u_char behavior = vm_map_entry_behavior(fs->entry);
557
558                         if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
559                                 ahead = 0;
560                                 behind = 0;
561                         } else {
562                                 behind = fs->pindex;
563                                 if (behind > VM_FAULT_READ_BEHIND)
564                                         behind = VM_FAULT_READ_BEHIND;
565
566                                 ahead = fs->object->size - fs->pindex;
567                                 if (ahead < 1)
568                                         ahead = 1;
569                                 if (ahead > VM_FAULT_READ_AHEAD)
570                                         ahead = VM_FAULT_READ_AHEAD;
571                         }
572
573                         if ((fs->first_object->type != OBJT_DEVICE) &&
574                             (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
575                                 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
576                                 fs->pindex >= fs->entry->lastr &&
577                                 fs->pindex < fs->entry->lastr + VM_FAULT_READ))
578                         ) {
579                                 vm_pindex_t firstpindex, tmppindex;
580
581                                 if (fs->first_pindex < 2 * VM_FAULT_READ)
582                                         firstpindex = 0;
583                                 else
584                                         firstpindex = fs->first_pindex - 2 * VM_FAULT_READ;
585
586                                 /*
587                                  * note: partially valid pages cannot be 
588                                  * included in the lookahead - NFS piecemeal
589                                  * writes will barf on it badly.
590                                  *
591                                  * spl protection is required to avoid races
592                                  * between the lookup and an interrupt
593                                  * unbusy/free sequence occuring prior to
594                                  * our busy check.
595                                  */
596                                 crit_enter();
597                                 for (tmppindex = fs->first_pindex - 1;
598                                     tmppindex >= firstpindex;
599                                     --tmppindex
600                                 ) {
601                                         vm_page_t mt;
602
603                                         mt = vm_page_lookup(fs->first_object, tmppindex);
604                                         if (mt == NULL || (mt->valid != VM_PAGE_BITS_ALL))
605                                                 break;
606                                         if (mt->busy ||
607                                                 (mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) ||
608                                                 mt->hold_count ||
609                                                 mt->wire_count) 
610                                                 continue;
611                                         if (mt->dirty == 0)
612                                                 vm_page_test_dirty(mt);
613                                         if (mt->dirty) {
614                                                 vm_page_protect(mt, VM_PROT_NONE);
615                                                 vm_page_deactivate(mt);
616                                         } else {
617                                                 vm_page_cache(mt);
618                                         }
619                                 }
620                                 crit_exit();
621
622                                 ahead += behind;
623                                 behind = 0;
624                         }
625
626                         /*
627                          * now we find out if any other pages should be paged
628                          * in at this time this routine checks to see if the
629                          * pages surrounding this fault reside in the same
630                          * object as the page for this fault.  If they do,
631                          * then they are faulted in also into the object.  The
632                          * array "marray" returned contains an array of
633                          * vm_page_t structs where one of them is the
634                          * vm_page_t passed to the routine.  The reqpage
635                          * return value is the index into the marray for the
636                          * vm_page_t passed to the routine.
637                          *
638                          * fs.m plus the additional pages are PG_BUSY'd.
639                          */
640                         faultcount = vm_fault_additional_pages(
641                             fs->m, behind, ahead, marray, &reqpage);
642
643                         /*
644                          * update lastr imperfectly (we do not know how much
645                          * getpages will actually read), but good enough.
646                          */
647                         fs->entry->lastr = fs->pindex + faultcount - behind;
648
649                         /*
650                          * Call the pager to retrieve the data, if any, after
651                          * releasing the lock on the map.  We hold a ref on
652                          * fs.object and the pages are PG_BUSY'd.
653                          */
654                         unlock_map(fs);
655
656                         if (faultcount) {
657                                 rv = vm_pager_get_pages(fs->object, marray, 
658                                                         faultcount, reqpage);
659                         } else {
660                                 rv = VM_PAGER_FAIL;
661                         }
662
663                         if (rv == VM_PAGER_OK) {
664                                 /*
665                                  * Found the page. Leave it busy while we play
666                                  * with it.
667                                  */
668
669                                 /*
670                                  * Relookup in case pager changed page. Pager
671                                  * is responsible for disposition of old page
672                                  * if moved.
673                                  *
674                                  * XXX other code segments do relookups too.
675                                  * It's a bad abstraction that needs to be
676                                  * fixed/removed.
677                                  */
678                                 fs->m = vm_page_lookup(fs->object, fs->pindex);
679                                 if (fs->m == NULL) {
680                                         unlock_and_deallocate(fs);
681                                         return (KERN_TRY_AGAIN);
682                                 }
683
684                                 ++fs->hardfault;
685                                 break; /* break to PAGE HAS BEEN FOUND */
686                         }
687
688                         /*
689                          * Remove the bogus page (which does not exist at this
690                          * object/offset); before doing so, we must get back
691                          * our object lock to preserve our invariant.
692                          *
693                          * Also wake up any other process that may want to bring
694                          * in this page.
695                          *
696                          * If this is the top-level object, we must leave the
697                          * busy page to prevent another process from rushing
698                          * past us, and inserting the page in that object at
699                          * the same time that we are.
700                          */
701                         if (rv == VM_PAGER_ERROR) {
702                                 if (curproc)
703                                         printf("vm_fault: pager read error, pid %d (%s)\n", curproc->p_pid, curproc->p_comm);
704                                 else
705                                         printf("vm_fault: pager read error, thread %p (%s)\n", curthread, curproc->p_comm);
706                         }
707                         /*
708                          * Data outside the range of the pager or an I/O error
709                          */
710                         /*
711                          * XXX - the check for kernel_map is a kludge to work
712                          * around having the machine panic on a kernel space
713                          * fault w/ I/O error.
714                          */
715                         if (((fs->map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
716                                 (rv == VM_PAGER_BAD)) {
717                                 vm_page_free(fs->m);
718                                 fs->m = NULL;
719                                 unlock_and_deallocate(fs);
720                                 if (rv == VM_PAGER_ERROR)
721                                         return (KERN_FAILURE);
722                                 else
723                                         return (KERN_PROTECTION_FAILURE);
724                                 /* NOT REACHED */
725                         }
726                         if (fs->object != fs->first_object) {
727                                 vm_page_free(fs->m);
728                                 fs->m = NULL;
729                                 /*
730                                  * XXX - we cannot just fall out at this
731                                  * point, m has been freed and is invalid!
732                                  */
733                         }
734                 }
735
736                 /*
737                  * We get here if the object has a default pager (or unwiring) 
738                  * or the pager doesn't have the page.
739                  */
740                 if (fs->object == fs->first_object)
741                         fs->first_m = fs->m;
742
743                 /*
744                  * Move on to the next object.  Lock the next object before
745                  * unlocking the current one.
746                  */
747                 fs->pindex += OFF_TO_IDX(fs->object->backing_object_offset);
748                 next_object = fs->object->backing_object;
749                 if (next_object == NULL) {
750                         /*
751                          * If there's no object left, fill the page in the top
752                          * object with zeros.
753                          */
754                         if (fs->object != fs->first_object) {
755                                 vm_object_pip_wakeup(fs->object);
756
757                                 fs->object = fs->first_object;
758                                 fs->pindex = fs->first_pindex;
759                                 fs->m = fs->first_m;
760                         }
761                         fs->first_m = NULL;
762
763                         /*
764                          * Zero the page if necessary and mark it valid.
765                          */
766                         if ((fs->m->flags & PG_ZERO) == 0) {
767                                 vm_page_zero_fill(fs->m);
768                         } else {
769                                 mycpu->gd_cnt.v_ozfod++;
770                         }
771                         mycpu->gd_cnt.v_zfod++;
772                         fs->m->valid = VM_PAGE_BITS_ALL;
773                         break;  /* break to PAGE HAS BEEN FOUND */
774                 } else {
775                         if (fs->object != fs->first_object) {
776                                 vm_object_pip_wakeup(fs->object);
777                         }
778                         KASSERT(fs->object != next_object, ("object loop %p", next_object));
779                         fs->object = next_object;
780                         vm_object_pip_add(fs->object, 1);
781                 }
782         }
783
784         KASSERT((fs->m->flags & PG_BUSY) != 0,
785                 ("vm_fault: not busy after main loop"));
786
787         /*
788          * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
789          * is held.]
790          */
791
792         /*
793          * If the page is being written, but isn't already owned by the
794          * top-level object, we have to copy it into a new page owned by the
795          * top-level object.
796          */
797         if (fs->object != fs->first_object) {
798                 /*
799                  * We only really need to copy if we want to write it.
800                  */
801                 if (fault_type & VM_PROT_WRITE) {
802                         /*
803                          * This allows pages to be virtually copied from a 
804                          * backing_object into the first_object, where the 
805                          * backing object has no other refs to it, and cannot
806                          * gain any more refs.  Instead of a bcopy, we just 
807                          * move the page from the backing object to the 
808                          * first object.  Note that we must mark the page 
809                          * dirty in the first object so that it will go out 
810                          * to swap when needed.
811                          */
812                         if (fs->map_generation == fs->map->timestamp &&
813                                 /*
814                                  * Only one shadow object
815                                  */
816                                 (fs->object->shadow_count == 1) &&
817                                 /*
818                                  * No COW refs, except us
819                                  */
820                                 (fs->object->ref_count == 1) &&
821                                 /*
822                                  * No one else can look this object up
823                                  */
824                                 (fs->object->handle == NULL) &&
825                                 /*
826                                  * No other ways to look the object up
827                                  */
828                                 ((fs->object->type == OBJT_DEFAULT) ||
829                                  (fs->object->type == OBJT_SWAP)) &&
830                                 /*
831                                  * We don't chase down the shadow chain
832                                  */
833                                 (fs->object == fs->first_object->backing_object) &&
834
835                                 /*
836                                  * grab the lock if we need to
837                                  */
838                                 (fs->lookup_still_valid ||
839                                  lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT) == 0)
840                             ) {
841                                 
842                                 fs->lookup_still_valid = 1;
843                                 /*
844                                  * get rid of the unnecessary page
845                                  */
846                                 vm_page_protect(fs->first_m, VM_PROT_NONE);
847                                 vm_page_free(fs->first_m);
848                                 fs->first_m = NULL;
849
850                                 /*
851                                  * grab the page and put it into the 
852                                  * process'es object.  The page is 
853                                  * automatically made dirty.
854                                  */
855                                 vm_page_rename(fs->m, fs->first_object, fs->first_pindex);
856                                 fs->first_m = fs->m;
857                                 vm_page_busy(fs->first_m);
858                                 fs->m = NULL;
859                                 mycpu->gd_cnt.v_cow_optim++;
860                         } else {
861                                 /*
862                                  * Oh, well, lets copy it.
863                                  */
864                                 vm_page_copy(fs->m, fs->first_m);
865                         }
866
867                         if (fs->m) {
868                                 /*
869                                  * We no longer need the old page or object.
870                                  */
871                                 release_page(fs);
872                         }
873
874                         /*
875                          * fs->object != fs->first_object due to above 
876                          * conditional
877                          */
878                         vm_object_pip_wakeup(fs->object);
879
880                         /*
881                          * Only use the new page below...
882                          */
883
884                         mycpu->gd_cnt.v_cow_faults++;
885                         fs->m = fs->first_m;
886                         fs->object = fs->first_object;
887                         fs->pindex = fs->first_pindex;
888                 } else {
889                         /*
890                          * If it wasn't a write fault avoid having to copy
891                          * the page by mapping it read-only.
892                          */
893                         fs->prot &= ~VM_PROT_WRITE;
894                 }
895         }
896
897         /*
898          * We may have had to unlock a map to do I/O.  If we did then
899          * lookup_still_valid will be FALSE.  If the map generation count
900          * also changed then all sorts of things could have happened while
901          * we were doing the I/O and we need to retry.
902          */
903
904         if (!fs->lookup_still_valid &&
905             (fs->map->timestamp != fs->map_generation)) {
906                 release_page(fs);
907                 unlock_and_deallocate(fs);
908                 return (KERN_TRY_AGAIN);
909         }
910
911
912 #if 0
913         /*
914          * THIS IS THE PREVIOUS CODE, WHICH TRIED TO OPTIMIZE THE RETRY PATH.
915          * SINCE WE HAD TO DO I/O ANYWAY, DON'T BOTHER
916          */
917         if (!fs->lookup_still_valid &&
918             (fs->map->timestamp != fs->map_generation)) {
919                 vm_object_t retry_object;
920                 vm_pindex_t retry_pindex;
921                 vm_prot_t retry_prot;
922
923                 /*
924                  * Since map entries may be pageable, make sure we can take a
925                  * page fault on them.
926                  */
927
928                 /*
929                  * Unlock vnode before the lookup to avoid deadlock.   E.G.
930                  * avoid a deadlock between the inode and exec_map that can
931                  * occur due to locks being obtained in different orders.
932                  */
933
934                 if (fs->vp != NULL) {
935                         vput(fs->vp);
936                         fs->vp = NULL;
937                 }
938                 
939                 if (fs->map->infork) {
940                         release_page(fs);
941                         unlock_and_deallocate(fs);
942                         return (KERN_TRY_AGAIN);
943                 }
944
945                 /*
946                  * To avoid trying to write_lock the map while another process
947                  * has it read_locked (in vm_map_wire), we do not try for
948                  * write permission.  If the page is still writable, we will
949                  * get write permission.  If it is not, or has been marked
950                  * needs_copy, we enter the mapping without write permission,
951                  * and will merely take another fault.
952                  */
953                 result = vm_map_lookup(fs->map, vaddr,
954                                        fault_type & ~VM_PROT_WRITE, &fs->entry,
955                                        &retry_object, &retry_pindex,
956                                        &retry_prot, &fs->wired);
957                 fs->map_generation = fs->map->timestamp;
958
959                 /*
960                  * If we don't need the page any longer, put it on the active
961                  * list (the easiest thing to do here).  If no one needs it,
962                  * pageout will grab it eventually.
963                  */
964
965                 if (result != KERN_SUCCESS) {
966                         release_page(&fs);
967                         unlock_and_deallocate(&fs);
968                         return (result);
969                 }
970                 fs->lookup_still_valid = TRUE;
971
972                 if ((retry_object != fs->first_object) ||
973                     (retry_pindex != fs->first_pindex)) {
974                         release_page(fs);
975                         unlock_and_deallocate(fs);
976                         return (KERN_TRY_AGAIN);
977                 }
978                 /*
979                  * Check whether the protection has changed or the object has
980                  * been copied while we left the map unlocked. Changing from
981                  * read to write permission is OK - we leave the page
982                  * write-protected, and catch the write fault. Changing from
983                  * write to read permission means that we can't mark the page
984                  * write-enabled after all.
985                  */
986                 prot &= retry_prot;
987         }
988 #endif
989
990         /*
991          * Put this page into the physical map. We had to do the unlock above
992          * because pmap_enter may cause other faults.   We don't put the page
993          * back on the active queue until later so that the page-out daemon
994          * won't find us (yet).
995          */
996         if (fs->prot & VM_PROT_WRITE) {
997                 vm_page_flag_set(fs->m, PG_WRITEABLE);
998                 vm_object_set_writeable_dirty(fs->m->object);
999
1000                 /*
1001                  * If the fault is a write, we know that this page is being
1002                  * written NOW so dirty it explicitly to save on 
1003                  * pmap_is_modified() calls later.
1004                  *
1005                  * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
1006                  * if the page is already dirty to prevent data written with
1007                  * the expectation of being synced from not being synced.
1008                  * Likewise if this entry does not request NOSYNC then make
1009                  * sure the page isn't marked NOSYNC.  Applications sharing
1010                  * data should use the same flags to avoid ping ponging.
1011                  *
1012                  * Also tell the backing pager, if any, that it should remove
1013                  * any swap backing since the page is now dirty.
1014                  */
1015                 if (fs->entry->eflags & MAP_ENTRY_NOSYNC) {
1016                         if (fs->m->dirty == 0)
1017                                 vm_page_flag_set(fs->m, PG_NOSYNC);
1018                 } else {
1019                         vm_page_flag_clear(fs->m, PG_NOSYNC);
1020                 }
1021                 if (fs->fault_flags & VM_FAULT_DIRTY) {
1022                         crit_enter();
1023                         vm_page_dirty(fs->m);
1024                         vm_pager_page_unswapped(fs->m);
1025                         crit_exit();
1026                 }
1027         }
1028
1029         /*
1030          * Page had better still be busy
1031          */
1032
1033         KASSERT(fs->m->flags & PG_BUSY,
1034                 ("vm_fault: page %p not busy!", fs->m));
1035
1036         unlock_things(fs);
1037
1038         /*
1039          * Sanity check: page must be completely valid or it is not fit to
1040          * map into user space.  vm_pager_get_pages() ensures this.
1041          */
1042         if (fs->m->valid != VM_PAGE_BITS_ALL) {
1043                 vm_page_zero_invalid(fs->m, TRUE);
1044                 printf("Warning: page %p partially invalid on fault\n", fs->m);
1045         }
1046
1047         return (KERN_SUCCESS);
1048 }
1049
1050 /*
1051  * quick version of vm_fault
1052  */
1053 int
1054 vm_fault_quick(caddr_t v, int prot)
1055 {
1056         int r;
1057
1058         if (prot & VM_PROT_WRITE)
1059                 r = subyte(v, fubyte(v));
1060         else
1061                 r = fubyte(v);
1062         return(r);
1063 }
1064
1065 /*
1066  * Wire down a range of virtual addresses in a map.  The entry in question
1067  * should be marked in-transition and the map must be locked.  We must
1068  * release the map temporarily while faulting-in the page to avoid a
1069  * deadlock.  Note that the entry may be clipped while we are blocked but
1070  * will never be freed.
1071  */
1072 int
1073 vm_fault_wire(vm_map_t map, vm_map_entry_t entry, boolean_t user_wire)
1074 {
1075         boolean_t fictitious;
1076         vm_offset_t start;
1077         vm_offset_t end;
1078         vm_offset_t va;
1079         vm_paddr_t pa;
1080         pmap_t pmap;
1081         int rv;
1082
1083         pmap = vm_map_pmap(map);
1084         start = entry->start;
1085         end = entry->end;
1086         fictitious = entry->object.vm_object &&
1087                         (entry->object.vm_object->type == OBJT_DEVICE);
1088
1089         vm_map_unlock(map);
1090         map->timestamp++;
1091
1092         /*
1093          * We simulate a fault to get the page and enter it in the physical
1094          * map.
1095          */
1096         for (va = start; va < end; va += PAGE_SIZE) {
1097                 if (user_wire) {
1098                         rv = vm_fault(map, va, VM_PROT_READ, 
1099                                         VM_FAULT_USER_WIRE);
1100                 } else {
1101                         rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE,
1102                                         VM_FAULT_CHANGE_WIRING);
1103                 }
1104                 if (rv) {
1105                         while (va > start) {
1106                                 va -= PAGE_SIZE;
1107                                 if ((pa = pmap_extract(pmap, va)) == 0)
1108                                         continue;
1109                                 pmap_change_wiring(pmap, va, FALSE);
1110                                 if (!fictitious)
1111                                         vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1112                         }
1113                         vm_map_lock(map);
1114                         return (rv);
1115                 }
1116         }
1117         vm_map_lock(map);
1118         return (KERN_SUCCESS);
1119 }
1120
1121 /*
1122  * Unwire a range of virtual addresses in a map.  The map should be
1123  * locked.
1124  */
1125 void
1126 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
1127 {
1128         boolean_t fictitious;
1129         vm_offset_t start;
1130         vm_offset_t end;
1131         vm_offset_t va;
1132         vm_paddr_t pa;
1133         pmap_t pmap;
1134
1135         pmap = vm_map_pmap(map);
1136         start = entry->start;
1137         end = entry->end;
1138         fictitious = entry->object.vm_object &&
1139                         (entry->object.vm_object->type == OBJT_DEVICE);
1140
1141         /*
1142          * Since the pages are wired down, we must be able to get their
1143          * mappings from the physical map system.
1144          */
1145         for (va = start; va < end; va += PAGE_SIZE) {
1146                 pa = pmap_extract(pmap, va);
1147                 if (pa != 0) {
1148                         pmap_change_wiring(pmap, va, FALSE);
1149                         if (!fictitious)
1150                                 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1151                 }
1152         }
1153 }
1154
1155 /*
1156  * Reduce the rate at which memory is allocated to a process based
1157  * on the perceived load on the VM system. As the load increases
1158  * the allocation burst rate goes down and the delay increases. 
1159  *
1160  * Rate limiting does not apply when faulting active or inactive
1161  * pages.  When faulting 'cache' pages, rate limiting only applies
1162  * if the system currently has a severe page deficit.
1163  *
1164  * XXX vm_pagesupply should be increased when a page is freed.
1165  *
1166  * We sleep up to 1/10 of a second.
1167  */
1168 static int
1169 vm_fault_ratelimit(struct vmspace *vmspace)
1170 {
1171         if (vm_load_enable == 0)
1172                 return(0);
1173         if (vmspace->vm_pagesupply > 0) {
1174                 --vmspace->vm_pagesupply;
1175                 return(0);
1176         }
1177 #ifdef INVARIANTS
1178         if (vm_load_debug) {
1179                 printf("load %-4d give %d pgs, wait %d, pid %-5d (%s)\n",
1180                         vm_load, 
1181                         (1000 - vm_load ) / 10, vm_load * hz / 10000,
1182                         curproc->p_pid, curproc->p_comm);
1183         }
1184 #endif
1185         vmspace->vm_pagesupply = (1000 - vm_load) / 10;
1186         return(vm_load * hz / 10000);
1187 }
1188
1189 /*
1190  *      Routine:
1191  *              vm_fault_copy_entry
1192  *      Function:
1193  *              Copy all of the pages from a wired-down map entry to another.
1194  *
1195  *      In/out conditions:
1196  *              The source and destination maps must be locked for write.
1197  *              The source map entry must be wired down (or be a sharing map
1198  *              entry corresponding to a main map entry that is wired down).
1199  */
1200
1201 void
1202 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1203     vm_map_entry_t dst_entry, vm_map_entry_t src_entry)
1204 {
1205         vm_object_t dst_object;
1206         vm_object_t src_object;
1207         vm_ooffset_t dst_offset;
1208         vm_ooffset_t src_offset;
1209         vm_prot_t prot;
1210         vm_offset_t vaddr;
1211         vm_page_t dst_m;
1212         vm_page_t src_m;
1213
1214 #ifdef  lint
1215         src_map++;
1216 #endif  /* lint */
1217
1218         src_object = src_entry->object.vm_object;
1219         src_offset = src_entry->offset;
1220
1221         /*
1222          * Create the top-level object for the destination entry. (Doesn't
1223          * actually shadow anything - we copy the pages directly.)
1224          */
1225         dst_object = vm_object_allocate(OBJT_DEFAULT,
1226             (vm_size_t) OFF_TO_IDX(dst_entry->end - dst_entry->start));
1227
1228         dst_entry->object.vm_object = dst_object;
1229         dst_entry->offset = 0;
1230
1231         prot = dst_entry->max_protection;
1232
1233         /*
1234          * Loop through all of the pages in the entry's range, copying each
1235          * one from the source object (it should be there) to the destination
1236          * object.
1237          */
1238         for (vaddr = dst_entry->start, dst_offset = 0;
1239             vaddr < dst_entry->end;
1240             vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1241
1242                 /*
1243                  * Allocate a page in the destination object
1244                  */
1245                 do {
1246                         dst_m = vm_page_alloc(dst_object,
1247                                 OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1248                         if (dst_m == NULL) {
1249                                 vm_wait();
1250                         }
1251                 } while (dst_m == NULL);
1252
1253                 /*
1254                  * Find the page in the source object, and copy it in.
1255                  * (Because the source is wired down, the page will be in
1256                  * memory.)
1257                  */
1258                 src_m = vm_page_lookup(src_object,
1259                         OFF_TO_IDX(dst_offset + src_offset));
1260                 if (src_m == NULL)
1261                         panic("vm_fault_copy_wired: page missing");
1262
1263                 vm_page_copy(src_m, dst_m);
1264
1265                 /*
1266                  * Enter it in the pmap...
1267                  */
1268
1269                 vm_page_flag_clear(dst_m, PG_ZERO);
1270                 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
1271                 vm_page_flag_set(dst_m, PG_WRITEABLE|PG_MAPPED);
1272
1273                 /*
1274                  * Mark it no longer busy, and put it on the active list.
1275                  */
1276                 vm_page_activate(dst_m);
1277                 vm_page_wakeup(dst_m);
1278         }
1279 }
1280
1281
1282 /*
1283  * This routine checks around the requested page for other pages that
1284  * might be able to be faulted in.  This routine brackets the viable
1285  * pages for the pages to be paged in.
1286  *
1287  * Inputs:
1288  *      m, rbehind, rahead
1289  *
1290  * Outputs:
1291  *  marray (array of vm_page_t), reqpage (index of requested page)
1292  *
1293  * Return value:
1294  *  number of pages in marray
1295  */
1296 static int
1297 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
1298     vm_page_t *marray, int *reqpage)
1299 {
1300         int i,j;
1301         vm_object_t object;
1302         vm_pindex_t pindex, startpindex, endpindex, tpindex;
1303         vm_page_t rtm;
1304         int cbehind, cahead;
1305
1306         object = m->object;
1307         pindex = m->pindex;
1308
1309         /*
1310          * we don't fault-ahead for device pager
1311          */
1312         if (object->type == OBJT_DEVICE) {
1313                 *reqpage = 0;
1314                 marray[0] = m;
1315                 return 1;
1316         }
1317
1318         /*
1319          * if the requested page is not available, then give up now
1320          */
1321
1322         if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1323                 return 0;
1324         }
1325
1326         if ((cbehind == 0) && (cahead == 0)) {
1327                 *reqpage = 0;
1328                 marray[0] = m;
1329                 return 1;
1330         }
1331
1332         if (rahead > cahead) {
1333                 rahead = cahead;
1334         }
1335
1336         if (rbehind > cbehind) {
1337                 rbehind = cbehind;
1338         }
1339
1340         /*
1341          * try to do any readahead that we might have free pages for.
1342          */
1343         if ((rahead + rbehind) >
1344                 ((vmstats.v_free_count + vmstats.v_cache_count) - vmstats.v_free_reserved)) {
1345                 pagedaemon_wakeup();
1346                 marray[0] = m;
1347                 *reqpage = 0;
1348                 return 1;
1349         }
1350
1351         /*
1352          * scan backward for the read behind pages -- in memory 
1353          *
1354          * Assume that if the page is not found an interrupt will not
1355          * create it.  Theoretically interrupts can only remove (busy)
1356          * pages, not create new associations.
1357          */
1358         if (pindex > 0) {
1359                 if (rbehind > pindex) {
1360                         rbehind = pindex;
1361                         startpindex = 0;
1362                 } else {
1363                         startpindex = pindex - rbehind;
1364                 }
1365
1366                 crit_enter();
1367                 for ( tpindex = pindex - 1; tpindex >= startpindex; tpindex -= 1) {
1368                         if (vm_page_lookup( object, tpindex)) {
1369                                 startpindex = tpindex + 1;
1370                                 break;
1371                         }
1372                         if (tpindex == 0)
1373                                 break;
1374                 }
1375
1376                 for(i = 0, tpindex = startpindex; tpindex < pindex; i++, tpindex++) {
1377
1378                         rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1379                         if (rtm == NULL) {
1380                                 crit_exit();
1381                                 for (j = 0; j < i; j++) {
1382                                         vm_page_free(marray[j]);
1383                                 }
1384                                 marray[0] = m;
1385                                 *reqpage = 0;
1386                                 return 1;
1387                         }
1388
1389                         marray[i] = rtm;
1390                 }
1391                 crit_exit();
1392         } else {
1393                 startpindex = 0;
1394                 i = 0;
1395         }
1396
1397         marray[i] = m;
1398         /* page offset of the required page */
1399         *reqpage = i;
1400
1401         tpindex = pindex + 1;
1402         i++;
1403
1404         /*
1405          * scan forward for the read ahead pages
1406          */
1407         endpindex = tpindex + rahead;
1408         if (endpindex > object->size)
1409                 endpindex = object->size;
1410
1411         crit_enter();
1412         for( ; tpindex < endpindex; i++, tpindex++) {
1413
1414                 if (vm_page_lookup(object, tpindex)) {
1415                         break;
1416                 }
1417
1418                 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1419                 if (rtm == NULL) {
1420                         break;
1421                 }
1422
1423                 marray[i] = rtm;
1424         }
1425         crit_exit();
1426
1427         /* return number of bytes of pages */
1428         return i;
1429 }