Correct BSD License clause numbering from 1-2-4 to 1-2-3.
[dragonfly.git] / sys / vm / vm_object.c
1 /*
2  * Copyright (c) 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * The Mach Operating System project at Carnegie-Mellon University.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *      from: @(#)vm_object.c   8.5 (Berkeley) 3/22/94
33  *
34  *
35  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36  * All rights reserved.
37  *
38  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39  *
40  * Permission to use, copy, modify and distribute this software and
41  * its documentation is hereby granted, provided that both the copyright
42  * notice and this permission notice appear in all copies of the
43  * software, derivative works or modified versions, and any portions
44  * thereof, and that both notices appear in supporting documentation.
45  *
46  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49  *
50  * Carnegie Mellon requests users of this software to return to
51  *
52  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53  *  School of Computer Science
54  *  Carnegie Mellon University
55  *  Pittsburgh PA 15213-3890
56  *
57  * any improvements or extensions that they make and grant Carnegie the
58  * rights to redistribute these changes.
59  *
60  * $FreeBSD: src/sys/vm/vm_object.c,v 1.171.2.8 2003/05/26 19:17:56 alc Exp $
61  */
62
63 /*
64  *      Virtual memory object module.
65  */
66
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/proc.h>           /* for curproc, pageproc */
70 #include <sys/thread.h>
71 #include <sys/vnode.h>
72 #include <sys/vmmeter.h>
73 #include <sys/mman.h>
74 #include <sys/mount.h>
75 #include <sys/kernel.h>
76 #include <sys/sysctl.h>
77 #include <sys/refcount.h>
78
79 #include <vm/vm.h>
80 #include <vm/vm_param.h>
81 #include <vm/pmap.h>
82 #include <vm/vm_map.h>
83 #include <vm/vm_object.h>
84 #include <vm/vm_page.h>
85 #include <vm/vm_pageout.h>
86 #include <vm/vm_pager.h>
87 #include <vm/swap_pager.h>
88 #include <vm/vm_kern.h>
89 #include <vm/vm_extern.h>
90 #include <vm/vm_zone.h>
91
92 #define EASY_SCAN_FACTOR        8
93
94 static void     vm_object_qcollapse(vm_object_t object,
95                                     vm_object_t backing_object);
96 static void     vm_object_page_collect_flush(vm_object_t object, vm_page_t p,
97                                              int pagerflags);
98 static void     vm_object_lock_init(vm_object_t);
99
100
101 /*
102  *      Virtual memory objects maintain the actual data
103  *      associated with allocated virtual memory.  A given
104  *      page of memory exists within exactly one object.
105  *
106  *      An object is only deallocated when all "references"
107  *      are given up.  Only one "reference" to a given
108  *      region of an object should be writeable.
109  *
110  *      Associated with each object is a list of all resident
111  *      memory pages belonging to that object; this list is
112  *      maintained by the "vm_page" module, and locked by the object's
113  *      lock.
114  *
115  *      Each object also records a "pager" routine which is
116  *      used to retrieve (and store) pages to the proper backing
117  *      storage.  In addition, objects may be backed by other
118  *      objects from which they were virtual-copied.
119  *
120  *      The only items within the object structure which are
121  *      modified after time of creation are:
122  *              reference count         locked by object's lock
123  *              pager routine           locked by object's lock
124  *
125  */
126
127 struct object_q vm_object_list;         /* locked by vmobj_token */
128 struct vm_object kernel_object;
129
130 static long vm_object_count;            /* locked by vmobj_token */
131 extern int vm_pageout_page_count;
132
133 static long object_collapses;
134 static long object_bypasses;
135 static int next_index;
136 static vm_zone_t obj_zone;
137 static struct vm_zone obj_zone_store;
138 #define VM_OBJECTS_INIT 256
139 static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
140
141 /*
142  * Misc low level routines
143  */
144 static void
145 vm_object_lock_init(vm_object_t obj)
146 {
147 #if defined(DEBUG_LOCKS)
148         int i;
149
150         obj->debug_hold_bitmap = 0;
151         obj->debug_hold_ovfl = 0;
152         for (i = 0; i < VMOBJ_DEBUG_ARRAY_SIZE; i++) {
153                 obj->debug_hold_thrs[i] = NULL;
154                 obj->debug_hold_file[i] = NULL;
155                 obj->debug_hold_line[i] = 0;
156         }
157 #endif
158 }
159
160 void
161 vm_object_lock_swap(void)
162 {
163         lwkt_token_swap();
164 }
165
166 void
167 vm_object_lock(vm_object_t obj)
168 {
169         lwkt_gettoken(&obj->token);
170 }
171
172 /*
173  * Returns TRUE on sucesss
174  */
175 static int
176 vm_object_lock_try(vm_object_t obj)
177 {
178         return(lwkt_trytoken(&obj->token));
179 }
180
181 void
182 vm_object_lock_shared(vm_object_t obj)
183 {
184         lwkt_gettoken_shared(&obj->token);
185 }
186
187 void
188 vm_object_unlock(vm_object_t obj)
189 {
190         lwkt_reltoken(&obj->token);
191 }
192
193 static __inline void
194 vm_object_assert_held(vm_object_t obj)
195 {
196         ASSERT_LWKT_TOKEN_HELD(&obj->token);
197 }
198
199 void
200 #ifndef DEBUG_LOCKS
201 vm_object_hold(vm_object_t obj)
202 #else
203 debugvm_object_hold(vm_object_t obj, char *file, int line)
204 #endif
205 {
206         KKASSERT(obj != NULL);
207
208         /*
209          * Object must be held (object allocation is stable due to callers
210          * context, typically already holding the token on a parent object)
211          * prior to potentially blocking on the lock, otherwise the object
212          * can get ripped away from us.
213          */
214         refcount_acquire(&obj->hold_count);
215         vm_object_lock(obj);
216
217 #if defined(DEBUG_LOCKS)
218         int i;
219         u_int mask;
220
221         for (;;) {
222                 mask = ~obj->debug_hold_bitmap;
223                 cpu_ccfence();
224                 if (mask == 0xFFFFFFFFU) {
225                         if (obj->debug_hold_ovfl == 0)
226                                 obj->debug_hold_ovfl = 1;
227                         break;
228                 }
229                 i = ffs(mask) - 1;
230                 if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask,
231                                       ~mask | (1 << i))) {
232                         obj->debug_hold_bitmap |= (1 << i);
233                         obj->debug_hold_thrs[i] = curthread;
234                         obj->debug_hold_file[i] = file;
235                         obj->debug_hold_line[i] = line;
236                         break;
237                 }
238         }
239 #endif
240 }
241
242 int
243 #ifndef DEBUG_LOCKS
244 vm_object_hold_try(vm_object_t obj)
245 #else
246 debugvm_object_hold_try(vm_object_t obj, char *file, int line)
247 #endif
248 {
249         KKASSERT(obj != NULL);
250
251         /*
252          * Object must be held (object allocation is stable due to callers
253          * context, typically already holding the token on a parent object)
254          * prior to potentially blocking on the lock, otherwise the object
255          * can get ripped away from us.
256          */
257         refcount_acquire(&obj->hold_count);
258         if (vm_object_lock_try(obj) == 0) {
259                 if (refcount_release(&obj->hold_count)) {
260                         if (obj->ref_count == 0 && (obj->flags & OBJ_DEAD))
261                                 zfree(obj_zone, obj);
262                 }
263                 return(0);
264         }
265
266 #if defined(DEBUG_LOCKS)
267         int i;
268         u_int mask;
269
270         for (;;) {
271                 mask = ~obj->debug_hold_bitmap;
272                 cpu_ccfence();
273                 if (mask == 0xFFFFFFFFU) {
274                         if (obj->debug_hold_ovfl == 0)
275                                 obj->debug_hold_ovfl = 1;
276                         break;
277                 }
278                 i = ffs(mask) - 1;
279                 if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask,
280                                       ~mask | (1 << i))) {
281                         obj->debug_hold_bitmap |= (1 << i);
282                         obj->debug_hold_thrs[i] = curthread;
283                         obj->debug_hold_file[i] = file;
284                         obj->debug_hold_line[i] = line;
285                         break;
286                 }
287         }
288 #endif
289         return(1);
290 }
291
292 void
293 #ifndef DEBUG_LOCKS
294 vm_object_hold_shared(vm_object_t obj)
295 #else
296 debugvm_object_hold_shared(vm_object_t obj, char *file, int line)
297 #endif
298 {
299         KKASSERT(obj != NULL);
300
301         /*
302          * Object must be held (object allocation is stable due to callers
303          * context, typically already holding the token on a parent object)
304          * prior to potentially blocking on the lock, otherwise the object
305          * can get ripped away from us.
306          */
307         refcount_acquire(&obj->hold_count);
308         vm_object_lock_shared(obj);
309
310 #if defined(DEBUG_LOCKS)
311         int i;
312         u_int mask;
313
314         for (;;) {
315                 mask = ~obj->debug_hold_bitmap;
316                 cpu_ccfence();
317                 if (mask == 0xFFFFFFFFU) {
318                         if (obj->debug_hold_ovfl == 0)
319                                 obj->debug_hold_ovfl = 1;
320                         break;
321                 }
322                 i = ffs(mask) - 1;
323                 if (atomic_cmpset_int(&obj->debug_hold_bitmap, ~mask,
324                                       ~mask | (1 << i))) {
325                         obj->debug_hold_bitmap |= (1 << i);
326                         obj->debug_hold_thrs[i] = curthread;
327                         obj->debug_hold_file[i] = file;
328                         obj->debug_hold_line[i] = line;
329                         break;
330                 }
331         }
332 #endif
333 }
334
335 /*
336  * Obtain either a shared or exclusive lock on VM object
337  * based on whether this is a terminal vnode object or not.
338  */
339 int
340 #ifndef DEBUG_LOCKS
341 vm_object_hold_maybe_shared(vm_object_t obj)
342 #else
343 debugvm_object_hold_maybe_shared(vm_object_t obj, char *file, int line)
344 #endif
345 {
346         if (vm_shared_fault &&
347             obj->type == OBJT_VNODE &&
348             obj->backing_object == NULL) {
349                 vm_object_hold_shared(obj);
350                 return(1);
351         } else {
352                 vm_object_hold(obj);
353                 return(0);
354         }
355 }
356
357 /*
358  * Drop the token and hold_count on the object.
359  */
360 void
361 vm_object_drop(vm_object_t obj)
362 {
363         if (obj == NULL)
364                 return;
365
366 #if defined(DEBUG_LOCKS)
367         int found = 0;
368         int i;
369
370         for (i = 0; i < VMOBJ_DEBUG_ARRAY_SIZE; i++) {
371                 if ((obj->debug_hold_bitmap & (1 << i)) &&
372                     (obj->debug_hold_thrs[i] == curthread)) {
373                         obj->debug_hold_bitmap &= ~(1 << i);
374                         obj->debug_hold_thrs[i] = NULL;
375                         obj->debug_hold_file[i] = NULL;
376                         obj->debug_hold_line[i] = 0;
377                         found = 1;
378                         break;
379                 }
380         }
381
382         if (found == 0 && obj->debug_hold_ovfl == 0)
383                 panic("vm_object: attempt to drop hold on non-self-held obj");
384 #endif
385
386         /*
387          * No new holders should be possible once we drop hold_count 1->0 as
388          * there is no longer any way to reference the object.
389          */
390         KKASSERT(obj->hold_count > 0);
391         if (refcount_release(&obj->hold_count)) {
392                 if (obj->ref_count == 0 && (obj->flags & OBJ_DEAD)) {
393                         vm_object_unlock(obj);
394                         zfree(obj_zone, obj);
395                 } else {
396                         vm_object_unlock(obj);
397                 }
398         } else {
399                 vm_object_unlock(obj);
400         }
401 }
402
403 /*
404  * Initialize a freshly allocated object, returning a held object.
405  *
406  * Used only by vm_object_allocate() and zinitna().
407  *
408  * No requirements.
409  */
410 void
411 _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
412 {
413         int incr;
414
415         RB_INIT(&object->rb_memq);
416         LIST_INIT(&object->shadow_head);
417         lwkt_token_init(&object->token, "vmobj");
418
419         object->type = type;
420         object->size = size;
421         object->ref_count = 1;
422         object->hold_count = 0;
423         object->flags = 0;
424         if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
425                 vm_object_set_flag(object, OBJ_ONEMAPPING);
426         object->paging_in_progress = 0;
427         object->resident_page_count = 0;
428         object->agg_pv_list_count = 0;
429         object->shadow_count = 0;
430         /* cpu localization twist */
431         object->pg_color = (int)(intptr_t)curthread;
432         if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
433                 incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
434         else
435                 incr = size;
436         next_index = (next_index + incr) & PQ_L2_MASK;
437         object->handle = NULL;
438         object->backing_object = NULL;
439         object->backing_object_offset = (vm_ooffset_t)0;
440
441         object->generation++;
442         object->swblock_count = 0;
443         RB_INIT(&object->swblock_root);
444         vm_object_lock_init(object);
445         pmap_object_init(object);
446
447         vm_object_hold(object);
448         lwkt_gettoken(&vmobj_token);
449         TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
450         vm_object_count++;
451         lwkt_reltoken(&vmobj_token);
452 }
453
454 /*
455  * Initialize the VM objects module.
456  *
457  * Called from the low level boot code only.
458  */
459 void
460 vm_object_init(void)
461 {
462         TAILQ_INIT(&vm_object_list);
463         
464         _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(KvaEnd),
465                             &kernel_object);
466         vm_object_drop(&kernel_object);
467
468         obj_zone = &obj_zone_store;
469         zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
470                 vm_objects_init, VM_OBJECTS_INIT);
471 }
472
473 void
474 vm_object_init2(void)
475 {
476         zinitna(obj_zone, NULL, NULL, 0, 0, ZONE_PANICFAIL, 1);
477 }
478
479 /*
480  * Allocate and return a new object of the specified type and size.
481  *
482  * No requirements.
483  */
484 vm_object_t
485 vm_object_allocate(objtype_t type, vm_pindex_t size)
486 {
487         vm_object_t result;
488
489         result = (vm_object_t) zalloc(obj_zone);
490
491         _vm_object_allocate(type, size, result);
492         vm_object_drop(result);
493
494         return (result);
495 }
496
497 /*
498  * This version returns a held object, allowing further atomic initialization
499  * of the object.
500  */
501 vm_object_t
502 vm_object_allocate_hold(objtype_t type, vm_pindex_t size)
503 {
504         vm_object_t result;
505
506         result = (vm_object_t) zalloc(obj_zone);
507
508         _vm_object_allocate(type, size, result);
509
510         return (result);
511 }
512
513 /*
514  * Add an additional reference to a vm_object.  The object must already be
515  * held.  The original non-lock version is no longer supported.  The object
516  * must NOT be chain locked by anyone at the time the reference is added.
517  *
518  * Referencing a chain-locked object can blow up the fairly sensitive
519  * ref_count and shadow_count tests in the deallocator.  Most callers
520  * will call vm_object_chain_wait() prior to calling
521  * vm_object_reference_locked() to avoid the case.
522  *
523  * The object must be held, but may be held shared if desired (hence why
524  * we use an atomic op).
525  */
526 void
527 vm_object_reference_locked(vm_object_t object)
528 {
529         KKASSERT(object != NULL);
530         ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
531         KKASSERT((object->flags & OBJ_CHAINLOCK) == 0);
532         atomic_add_int(&object->ref_count, 1);
533         if (object->type == OBJT_VNODE) {
534                 vref(object->handle);
535                 /* XXX what if the vnode is being destroyed? */
536         }
537 }
538
539 /*
540  * Object OBJ_CHAINLOCK lock handling.
541  *
542  * The caller can chain-lock backing objects recursively and then
543  * use vm_object_chain_release_all() to undo the whole chain.
544  *
545  * Chain locks are used to prevent collapses and are only applicable
546  * to OBJT_DEFAULT and OBJT_SWAP objects.  Chain locking operations
547  * on other object types are ignored.  This is also important because
548  * it allows e.g. the vnode underlying a memory mapping to take concurrent
549  * faults.
550  *
551  * The object must usually be held on entry, though intermediate
552  * objects need not be held on release.
553  */
554 void
555 vm_object_chain_wait(vm_object_t object)
556 {
557         ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
558         while (object->flags & OBJ_CHAINLOCK) {
559                 vm_object_set_flag(object, OBJ_CHAINWANT);
560                 tsleep(object, 0, "objchain", 0);
561         }
562 }
563
564 void
565 vm_object_chain_acquire(vm_object_t object)
566 {
567         if (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP) {
568                 vm_object_chain_wait(object);
569                 vm_object_set_flag(object, OBJ_CHAINLOCK);
570         }
571 }
572
573 void
574 vm_object_chain_release(vm_object_t object)
575 {
576         ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
577         if (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP) {
578                 KKASSERT(object->flags & OBJ_CHAINLOCK);
579                 if (object->flags & OBJ_CHAINWANT) {
580                         vm_object_clear_flag(object,
581                                              OBJ_CHAINLOCK | OBJ_CHAINWANT);
582                         wakeup(object);
583                 } else {
584                         vm_object_clear_flag(object, OBJ_CHAINLOCK);
585                 }
586         }
587 }
588
589 /*
590  * This releases the entire chain of objects from first_object to and
591  * including stopobj, flowing through object->backing_object.
592  *
593  * We release stopobj first as an optimization as this object is most
594  * likely to be shared across multiple processes.
595  */
596 void
597 vm_object_chain_release_all(vm_object_t first_object, vm_object_t stopobj)
598 {
599         vm_object_t backing_object;
600         vm_object_t object;
601
602         vm_object_chain_release(stopobj);
603         object = first_object;
604
605         while (object != stopobj) {
606                 KKASSERT(object);
607                 if (object != first_object)
608                         vm_object_hold(object);
609                 backing_object = object->backing_object;
610                 vm_object_chain_release(object);
611                 if (object != first_object)
612                         vm_object_drop(object);
613                 object = backing_object;
614         }
615 }
616
617 /*
618  * Dereference an object and its underlying vnode.
619  *
620  * The object must be held exclusively and will remain held on return.
621  * (We don't need an atomic op due to the exclusivity).
622  */
623 static void
624 vm_object_vndeallocate(vm_object_t object)
625 {
626         struct vnode *vp = (struct vnode *) object->handle;
627
628         KASSERT(object->type == OBJT_VNODE,
629             ("vm_object_vndeallocate: not a vnode object"));
630         KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
631         ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
632 #ifdef INVARIANTS
633         if (object->ref_count == 0) {
634                 vprint("vm_object_vndeallocate", vp);
635                 panic("vm_object_vndeallocate: bad object reference count");
636         }
637 #endif
638         object->ref_count--;
639         if (object->ref_count == 0)
640                 vclrflags(vp, VTEXT);
641         vrele(vp);
642 }
643
644 /*
645  * Release a reference to the specified object, gained either through a
646  * vm_object_allocate or a vm_object_reference call.  When all references
647  * are gone, storage associated with this object may be relinquished.
648  *
649  * The caller does not have to hold the object locked but must have control
650  * over the reference in question in order to guarantee that the object
651  * does not get ripped out from under us.
652  *
653  * XXX Currently all deallocations require an exclusive lock.
654  */
655 void
656 vm_object_deallocate(vm_object_t object)
657 {
658         if (object) {
659                 vm_object_hold(object);
660                 vm_object_deallocate_locked(object);
661                 vm_object_drop(object);
662         }
663 }
664
665 void
666 vm_object_deallocate_locked(vm_object_t object)
667 {
668         struct vm_object_dealloc_list *dlist = NULL;
669         struct vm_object_dealloc_list *dtmp;
670         vm_object_t temp;
671         int must_drop = 0;
672
673         /*
674          * We may chain deallocate object, but additional objects may
675          * collect on the dlist which also have to be deallocated.  We
676          * must avoid a recursion, vm_object chains can get deep.
677          */
678 again:
679         while (object != NULL) {
680                 ASSERT_LWKT_TOKEN_HELD_EXCL(&object->token);
681 #if 0
682                 /*
683                  * Don't rip a ref_count out from under an object undergoing
684                  * collapse, it will confuse the collapse code.
685                  */
686                 vm_object_chain_wait(object);
687 #endif
688                 if (object->type == OBJT_VNODE) {
689                         vm_object_vndeallocate(object);
690                         break;
691                 }
692
693                 if (object->ref_count == 0) {
694                         panic("vm_object_deallocate: object deallocated "
695                               "too many times: %d", object->type);
696                 }
697                 if (object->ref_count > 2) {
698                         object->ref_count--;
699                         break;
700                 }
701
702                 /*
703                  * Here on ref_count of one or two, which are special cases for
704                  * objects.
705                  *
706                  * Nominal ref_count > 1 case if the second ref is not from
707                  * a shadow.
708                  *
709                  * (ONEMAPPING only applies to DEFAULT AND SWAP objects)
710                  */
711                 if (object->ref_count == 2 && object->shadow_count == 0) {
712                         if (object->type == OBJT_DEFAULT ||
713                             object->type == OBJT_SWAP) {
714                                 vm_object_set_flag(object, OBJ_ONEMAPPING);
715                         }
716                         object->ref_count--;
717                         break;
718                 }
719
720                 /*
721                  * If the second ref is from a shadow we chain along it
722                  * upwards if object's handle is exhausted.
723                  *
724                  * We have to decrement object->ref_count before potentially
725                  * collapsing the first shadow object or the collapse code
726                  * will not be able to handle the degenerate case to remove
727                  * object.  However, if we do it too early the object can
728                  * get ripped out from under us.
729                  */
730                 if (object->ref_count == 2 && object->shadow_count == 1 &&
731                     object->handle == NULL && (object->type == OBJT_DEFAULT ||
732                                                object->type == OBJT_SWAP)) {
733                         temp = LIST_FIRST(&object->shadow_head);
734                         KKASSERT(temp != NULL);
735                         vm_object_hold(temp);
736
737                         /*
738                          * Wait for any paging to complete so the collapse
739                          * doesn't (or isn't likely to) qcollapse.  pip
740                          * waiting must occur before we acquire the
741                          * chainlock.
742                          */
743                         while (
744                                 temp->paging_in_progress ||
745                                 object->paging_in_progress
746                         ) {
747                                 vm_object_pip_wait(temp, "objde1");
748                                 vm_object_pip_wait(object, "objde2");
749                         }
750
751                         /*
752                          * If the parent is locked we have to give up, as
753                          * otherwise we would be acquiring locks in the
754                          * wrong order and potentially deadlock.
755                          */
756                         if (temp->flags & OBJ_CHAINLOCK) {
757                                 vm_object_drop(temp);
758                                 goto skip;
759                         }
760                         vm_object_chain_acquire(temp);
761
762                         /*
763                          * Recheck/retry after the hold and the paging
764                          * wait, both of which can block us.
765                          */
766                         if (object->ref_count != 2 ||
767                             object->shadow_count != 1 ||
768                             object->handle ||
769                             LIST_FIRST(&object->shadow_head) != temp ||
770                             (object->type != OBJT_DEFAULT &&
771                              object->type != OBJT_SWAP)) {
772                                 vm_object_chain_release(temp);
773                                 vm_object_drop(temp);
774                                 continue;
775                         }
776
777                         /*
778                          * We can safely drop object's ref_count now.
779                          */
780                         KKASSERT(object->ref_count == 2);
781                         object->ref_count--;
782
783                         /*
784                          * If our single parent is not collapseable just
785                          * decrement ref_count (2->1) and stop.
786                          */
787                         if (temp->handle || (temp->type != OBJT_DEFAULT &&
788                                              temp->type != OBJT_SWAP)) {
789                                 vm_object_chain_release(temp);
790                                 vm_object_drop(temp);
791                                 break;
792                         }
793
794                         /*
795                          * At this point we have already dropped object's
796                          * ref_count so it is possible for a race to
797                          * deallocate obj out from under us.  Any collapse
798                          * will re-check the situation.  We must not block
799                          * until we are able to collapse.
800                          *
801                          * Bump temp's ref_count to avoid an unwanted
802                          * degenerate recursion (can't call
803                          * vm_object_reference_locked() because it asserts
804                          * that CHAINLOCK is not set).
805                          */
806                         temp->ref_count++;
807                         KKASSERT(temp->ref_count > 1);
808
809                         /*
810                          * Collapse temp, then deallocate the extra ref
811                          * formally.
812                          */
813                         vm_object_collapse(temp, &dlist);
814                         vm_object_chain_release(temp);
815                         if (must_drop) {
816                                 vm_object_lock_swap();
817                                 vm_object_drop(object);
818                         }
819                         object = temp;
820                         must_drop = 1;
821                         continue;
822                 }
823
824                 /*
825                  * Drop the ref and handle termination on the 1->0 transition.
826                  * We may have blocked above so we have to recheck.
827                  */
828 skip:
829                 KKASSERT(object->ref_count != 0);
830                 if (object->ref_count >= 2) {
831                         object->ref_count--;
832                         break;
833                 }
834                 KKASSERT(object->ref_count == 1);
835
836                 /*
837                  * 1->0 transition.  Chain through the backing_object.
838                  * Maintain the ref until we've located the backing object,
839                  * then re-check.
840                  */
841                 while ((temp = object->backing_object) != NULL) {
842                         vm_object_hold(temp);
843                         if (temp == object->backing_object)
844                                 break;
845                         vm_object_drop(temp);
846                 }
847
848                 /*
849                  * 1->0 transition verified, retry if ref_count is no longer
850                  * 1.  Otherwise disconnect the backing_object (temp) and
851                  * clean up.
852                  */
853                 if (object->ref_count != 1) {
854                         vm_object_drop(temp);
855                         continue;
856                 }
857
858                 /*
859                  * It shouldn't be possible for the object to be chain locked
860                  * if we're removing the last ref on it.
861                  */
862                 KKASSERT((object->flags & OBJ_CHAINLOCK) == 0);
863
864                 if (temp) {
865                         LIST_REMOVE(object, shadow_list);
866                         temp->shadow_count--;
867                         temp->generation++;
868                         object->backing_object = NULL;
869                 }
870
871                 --object->ref_count;
872                 if ((object->flags & OBJ_DEAD) == 0)
873                         vm_object_terminate(object);
874                 if (must_drop && temp)
875                         vm_object_lock_swap();
876                 if (must_drop)
877                         vm_object_drop(object);
878                 object = temp;
879                 must_drop = 1;
880         }
881         if (must_drop && object)
882                 vm_object_drop(object);
883
884         /*
885          * Additional tail recursion on dlist.  Avoid a recursion.  Objects
886          * on the dlist have a hold count but are not locked.
887          */
888         if ((dtmp = dlist) != NULL) {
889                 dlist = dtmp->next;
890                 object = dtmp->object;
891                 kfree(dtmp, M_TEMP);
892
893                 vm_object_lock(object); /* already held, add lock */
894                 must_drop = 1;          /* and we're responsible for it */
895                 goto again;
896         }
897 }
898
899 /*
900  * Destroy the specified object, freeing up related resources.
901  *
902  * The object must have zero references.
903  *
904  * The object must held.  The caller is responsible for dropping the object
905  * after terminate returns.  Terminate does NOT drop the object.
906  */
907 static int vm_object_terminate_callback(vm_page_t p, void *data);
908
909 void
910 vm_object_terminate(vm_object_t object)
911 {
912         /*
913          * Make sure no one uses us.  Once we set OBJ_DEAD we should be
914          * able to safely block.
915          */
916         ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
917         KKASSERT((object->flags & OBJ_DEAD) == 0);
918         vm_object_set_flag(object, OBJ_DEAD);
919
920         /*
921          * Wait for the pageout daemon to be done with the object
922          */
923         vm_object_pip_wait(object, "objtrm1");
924
925         KASSERT(!object->paging_in_progress,
926                 ("vm_object_terminate: pageout in progress"));
927
928         /*
929          * Clean and free the pages, as appropriate. All references to the
930          * object are gone, so we don't need to lock it.
931          */
932         if (object->type == OBJT_VNODE) {
933                 struct vnode *vp;
934
935                 /*
936                  * Clean pages and flush buffers.
937                  *
938                  * NOTE!  TMPFS buffer flushes do not typically flush the
939                  *        actual page to swap as this would be highly
940                  *        inefficient, and normal filesystems usually wrap
941                  *        page flushes with buffer cache buffers.
942                  *
943                  *        To deal with this we have to call vinvalbuf() both
944                  *        before and after the vm_object_page_clean().
945                  */
946                 vp = (struct vnode *) object->handle;
947                 vinvalbuf(vp, V_SAVE, 0, 0);
948                 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
949                 vinvalbuf(vp, V_SAVE, 0, 0);
950         }
951
952         /*
953          * Wait for any I/O to complete, after which there had better not
954          * be any references left on the object.
955          */
956         vm_object_pip_wait(object, "objtrm2");
957
958         if (object->ref_count != 0) {
959                 panic("vm_object_terminate: object with references, "
960                       "ref_count=%d", object->ref_count);
961         }
962
963         /*
964          * Cleanup any shared pmaps associated with this object.
965          */
966         pmap_object_free(object);
967
968         /*
969          * Now free any remaining pages. For internal objects, this also
970          * removes them from paging queues. Don't free wired pages, just
971          * remove them from the object. 
972          */
973         vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
974                                 vm_object_terminate_callback, NULL);
975
976         /*
977          * Let the pager know object is dead.
978          */
979         vm_pager_deallocate(object);
980
981         /*
982          * Wait for the object hold count to hit 1, clean out pages as
983          * we go.  vmobj_token interlocks any race conditions that might
984          * pick the object up from the vm_object_list after we have cleared
985          * rb_memq.
986          */
987         for (;;) {
988                 if (RB_ROOT(&object->rb_memq) == NULL)
989                         break;
990                 kprintf("vm_object_terminate: Warning, object %p "
991                         "still has %d pages\n",
992                         object, object->resident_page_count);
993                 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
994                                         vm_object_terminate_callback, NULL);
995         }
996
997         /*
998          * There had better not be any pages left
999          */
1000         KKASSERT(object->resident_page_count == 0);
1001
1002         /*
1003          * Remove the object from the global object list.
1004          */
1005         lwkt_gettoken(&vmobj_token);
1006         TAILQ_REMOVE(&vm_object_list, object, object_list);
1007         vm_object_count--;
1008         lwkt_reltoken(&vmobj_token);
1009         vm_object_dead_wakeup(object);
1010
1011         if (object->ref_count != 0) {
1012                 panic("vm_object_terminate2: object with references, "
1013                       "ref_count=%d", object->ref_count);
1014         }
1015
1016         /*
1017          * NOTE: The object hold_count is at least 1, so we cannot zfree()
1018          *       the object here.  See vm_object_drop().
1019          */
1020 }
1021
1022 /*
1023  * The caller must hold the object.
1024  */
1025 static int
1026 vm_object_terminate_callback(vm_page_t p, void *data __unused)
1027 {
1028         vm_object_t object;
1029
1030         object = p->object;
1031         vm_page_busy_wait(p, TRUE, "vmpgtrm");
1032         if (object != p->object) {
1033                 kprintf("vm_object_terminate: Warning: Encountered "
1034                         "busied page %p on queue %d\n", p, p->queue);
1035                 vm_page_wakeup(p);
1036         } else if (p->wire_count == 0) {
1037                 /*
1038                  * NOTE: p->dirty and PG_NEED_COMMIT are ignored.
1039                  */
1040                 vm_page_free(p);
1041                 mycpu->gd_cnt.v_pfree++;
1042         } else {
1043                 if (p->queue != PQ_NONE)
1044                         kprintf("vm_object_terminate: Warning: Encountered "
1045                                 "wired page %p on queue %d\n", p, p->queue);
1046                 vm_page_remove(p);
1047                 vm_page_wakeup(p);
1048         }
1049         lwkt_yield();
1050         return(0);
1051 }
1052
1053 /*
1054  * The object is dead but still has an object<->pager association.  Sleep
1055  * and return.  The caller typically retests the association in a loop.
1056  *
1057  * The caller must hold the object.
1058  */
1059 void
1060 vm_object_dead_sleep(vm_object_t object, const char *wmesg)
1061 {
1062         ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1063         if (object->handle) {
1064                 vm_object_set_flag(object, OBJ_DEADWNT);
1065                 tsleep(object, 0, wmesg, 0);
1066                 /* object may be invalid after this point */
1067         }
1068 }
1069
1070 /*
1071  * Wakeup anyone waiting for the object<->pager disassociation on
1072  * a dead object.
1073  *
1074  * The caller must hold the object.
1075  */
1076 void
1077 vm_object_dead_wakeup(vm_object_t object)
1078 {
1079         ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1080         if (object->flags & OBJ_DEADWNT) {
1081                 vm_object_clear_flag(object, OBJ_DEADWNT);
1082                 wakeup(object);
1083         }
1084 }
1085
1086 /*
1087  * Clean all dirty pages in the specified range of object.  Leaves page
1088  * on whatever queue it is currently on.   If NOSYNC is set then do not
1089  * write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
1090  * leaving the object dirty.
1091  *
1092  * When stuffing pages asynchronously, allow clustering.  XXX we need a
1093  * synchronous clustering mode implementation.
1094  *
1095  * Odd semantics: if start == end, we clean everything.
1096  *
1097  * The object must be locked? XXX
1098  */
1099 static int vm_object_page_clean_pass1(struct vm_page *p, void *data);
1100 static int vm_object_page_clean_pass2(struct vm_page *p, void *data);
1101
1102 void
1103 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1104                      int flags)
1105 {
1106         struct rb_vm_page_scan_info info;
1107         struct vnode *vp;
1108         int wholescan;
1109         int pagerflags;
1110         int generation;
1111
1112         vm_object_hold(object);
1113         if (object->type != OBJT_VNODE ||
1114             (object->flags & OBJ_MIGHTBEDIRTY) == 0) {
1115                 vm_object_drop(object);
1116                 return;
1117         }
1118
1119         pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? 
1120                         VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
1121         pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
1122
1123         vp = object->handle;
1124
1125         /*
1126          * Interlock other major object operations.  This allows us to 
1127          * temporarily clear OBJ_WRITEABLE and OBJ_MIGHTBEDIRTY.
1128          */
1129         vm_object_set_flag(object, OBJ_CLEANING);
1130
1131         /*
1132          * Handle 'entire object' case
1133          */
1134         info.start_pindex = start;
1135         if (end == 0) {
1136                 info.end_pindex = object->size - 1;
1137         } else {
1138                 info.end_pindex = end - 1;
1139         }
1140         wholescan = (start == 0 && info.end_pindex == object->size - 1);
1141         info.limit = flags;
1142         info.pagerflags = pagerflags;
1143         info.object = object;
1144
1145         /*
1146          * If cleaning the entire object do a pass to mark the pages read-only.
1147          * If everything worked out ok, clear OBJ_WRITEABLE and
1148          * OBJ_MIGHTBEDIRTY.
1149          */
1150         if (wholescan) {
1151                 info.error = 0;
1152                 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
1153                                         vm_object_page_clean_pass1, &info);
1154                 if (info.error == 0) {
1155                         vm_object_clear_flag(object,
1156                                              OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
1157                         if (object->type == OBJT_VNODE &&
1158                             (vp = (struct vnode *)object->handle) != NULL) {
1159                                 if (vp->v_flag & VOBJDIRTY) 
1160                                         vclrflags(vp, VOBJDIRTY);
1161                         }
1162                 }
1163         }
1164
1165         /*
1166          * Do a pass to clean all the dirty pages we find.
1167          */
1168         do {
1169                 info.error = 0;
1170                 generation = object->generation;
1171                 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
1172                                         vm_object_page_clean_pass2, &info);
1173         } while (info.error || generation != object->generation);
1174
1175         vm_object_clear_flag(object, OBJ_CLEANING);
1176         vm_object_drop(object);
1177 }
1178
1179 /*
1180  * The caller must hold the object.
1181  */
1182 static 
1183 int
1184 vm_object_page_clean_pass1(struct vm_page *p, void *data)
1185 {
1186         struct rb_vm_page_scan_info *info = data;
1187
1188         vm_page_flag_set(p, PG_CLEANCHK);
1189         if ((info->limit & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
1190                 info->error = 1;
1191         } else if (vm_page_busy_try(p, FALSE) == 0) {
1192                 vm_page_protect(p, VM_PROT_READ);       /* must not block */
1193                 vm_page_wakeup(p);
1194         } else {
1195                 info->error = 1;
1196         }
1197         lwkt_yield();
1198         return(0);
1199 }
1200
1201 /*
1202  * The caller must hold the object
1203  */
1204 static 
1205 int
1206 vm_object_page_clean_pass2(struct vm_page *p, void *data)
1207 {
1208         struct rb_vm_page_scan_info *info = data;
1209         int generation;
1210
1211         /*
1212          * Do not mess with pages that were inserted after we started
1213          * the cleaning pass.
1214          */
1215         if ((p->flags & PG_CLEANCHK) == 0)
1216                 goto done;
1217
1218         generation = info->object->generation;
1219         vm_page_busy_wait(p, TRUE, "vpcwai");
1220         if (p->object != info->object ||
1221             info->object->generation != generation) {
1222                 info->error = 1;
1223                 vm_page_wakeup(p);
1224                 goto done;
1225         }
1226
1227         /*
1228          * Before wasting time traversing the pmaps, check for trivial
1229          * cases where the page cannot be dirty.
1230          */
1231         if (p->valid == 0 || (p->queue - p->pc) == PQ_CACHE) {
1232                 KKASSERT((p->dirty & p->valid) == 0 &&
1233                          (p->flags & PG_NEED_COMMIT) == 0);
1234                 vm_page_wakeup(p);
1235                 goto done;
1236         }
1237
1238         /*
1239          * Check whether the page is dirty or not.  The page has been set
1240          * to be read-only so the check will not race a user dirtying the
1241          * page.
1242          */
1243         vm_page_test_dirty(p);
1244         if ((p->dirty & p->valid) == 0 && (p->flags & PG_NEED_COMMIT) == 0) {
1245                 vm_page_flag_clear(p, PG_CLEANCHK);
1246                 vm_page_wakeup(p);
1247                 goto done;
1248         }
1249
1250         /*
1251          * If we have been asked to skip nosync pages and this is a
1252          * nosync page, skip it.  Note that the object flags were
1253          * not cleared in this case (because pass1 will have returned an
1254          * error), so we do not have to set them.
1255          */
1256         if ((info->limit & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
1257                 vm_page_flag_clear(p, PG_CLEANCHK);
1258                 vm_page_wakeup(p);
1259                 goto done;
1260         }
1261
1262         /*
1263          * Flush as many pages as we can.  PG_CLEANCHK will be cleared on
1264          * the pages that get successfully flushed.  Set info->error if
1265          * we raced an object modification.
1266          */
1267         vm_object_page_collect_flush(info->object, p, info->pagerflags);
1268         vm_wait_nominal();
1269 done:
1270         lwkt_yield();
1271         return(0);
1272 }
1273
1274 /*
1275  * Collect the specified page and nearby pages and flush them out.
1276  * The number of pages flushed is returned.  The passed page is busied
1277  * by the caller and we are responsible for its disposition.
1278  *
1279  * The caller must hold the object.
1280  */
1281 static void
1282 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags)
1283 {
1284         int runlen;
1285         int error;
1286         int maxf;
1287         int chkb;
1288         int maxb;
1289         int i;
1290         vm_pindex_t pi;
1291         vm_page_t maf[vm_pageout_page_count];
1292         vm_page_t mab[vm_pageout_page_count];
1293         vm_page_t ma[vm_pageout_page_count];
1294
1295         ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1296
1297         pi = p->pindex;
1298
1299         maxf = 0;
1300         for(i = 1; i < vm_pageout_page_count; i++) {
1301                 vm_page_t tp;
1302
1303                 tp = vm_page_lookup_busy_try(object, pi + i, TRUE, &error);
1304                 if (error)
1305                         break;
1306                 if (tp == NULL)
1307                         break;
1308                 if ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
1309                     (tp->flags & PG_CLEANCHK) == 0) {
1310                         vm_page_wakeup(tp);
1311                         break;
1312                 }
1313                 if ((tp->queue - tp->pc) == PQ_CACHE) {
1314                         vm_page_flag_clear(tp, PG_CLEANCHK);
1315                         vm_page_wakeup(tp);
1316                         break;
1317                 }
1318                 vm_page_test_dirty(tp);
1319                 if ((tp->dirty & tp->valid) == 0 &&
1320                     (tp->flags & PG_NEED_COMMIT) == 0) {
1321                         vm_page_flag_clear(tp, PG_CLEANCHK);
1322                         vm_page_wakeup(tp);
1323                         break;
1324                 }
1325                 maf[i - 1] = tp;
1326                 maxf++;
1327         }
1328
1329         maxb = 0;
1330         chkb = vm_pageout_page_count -  maxf;
1331         /*
1332          * NOTE: chkb can be 0
1333          */
1334         for(i = 1; chkb && i < chkb; i++) {
1335                 vm_page_t tp;
1336
1337                 tp = vm_page_lookup_busy_try(object, pi - i, TRUE, &error);
1338                 if (error)
1339                         break;
1340                 if (tp == NULL)
1341                         break;
1342                 if ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
1343                     (tp->flags & PG_CLEANCHK) == 0) {
1344                         vm_page_wakeup(tp);
1345                         break;
1346                 }
1347                 if ((tp->queue - tp->pc) == PQ_CACHE) {
1348                         vm_page_flag_clear(tp, PG_CLEANCHK);
1349                         vm_page_wakeup(tp);
1350                         break;
1351                 }
1352                 vm_page_test_dirty(tp);
1353                 if ((tp->dirty & tp->valid) == 0 &&
1354                     (tp->flags & PG_NEED_COMMIT) == 0) {
1355                         vm_page_flag_clear(tp, PG_CLEANCHK);
1356                         vm_page_wakeup(tp);
1357                         break;
1358                 }
1359                 mab[i - 1] = tp;
1360                 maxb++;
1361         }
1362
1363         /*
1364          * All pages in the maf[] and mab[] array are busied.
1365          */
1366         for (i = 0; i < maxb; i++) {
1367                 int index = (maxb - i) - 1;
1368                 ma[index] = mab[i];
1369                 vm_page_flag_clear(ma[index], PG_CLEANCHK);
1370         }
1371         vm_page_flag_clear(p, PG_CLEANCHK);
1372         ma[maxb] = p;
1373         for(i = 0; i < maxf; i++) {
1374                 int index = (maxb + i) + 1;
1375                 ma[index] = maf[i];
1376                 vm_page_flag_clear(ma[index], PG_CLEANCHK);
1377         }
1378         runlen = maxb + maxf + 1;
1379
1380         for (i = 0; i < runlen; i++)    /* XXX need this any more? */
1381                 vm_page_hold(ma[i]);
1382
1383         vm_pageout_flush(ma, runlen, pagerflags);
1384
1385         for (i = 0; i < runlen; i++)    /* XXX need this any more? */
1386                 vm_page_unhold(ma[i]);
1387 }
1388
1389 /*
1390  * Same as vm_object_pmap_copy, except range checking really
1391  * works, and is meant for small sections of an object.
1392  *
1393  * This code protects resident pages by making them read-only
1394  * and is typically called on a fork or split when a page
1395  * is converted to copy-on-write.  
1396  *
1397  * NOTE: If the page is already at VM_PROT_NONE, calling
1398  * vm_page_protect will have no effect.
1399  */
1400 void
1401 vm_object_pmap_copy_1(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1402 {
1403         vm_pindex_t idx;
1404         vm_page_t p;
1405
1406         if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
1407                 return;
1408
1409         vm_object_hold(object);
1410         for (idx = start; idx < end; idx++) {
1411                 p = vm_page_lookup(object, idx);
1412                 if (p == NULL)
1413                         continue;
1414                 vm_page_protect(p, VM_PROT_READ);
1415         }
1416         vm_object_drop(object);
1417 }
1418
1419 /*
1420  * Removes all physical pages in the specified object range from all
1421  * physical maps.
1422  *
1423  * The object must *not* be locked.
1424  */
1425
1426 static int vm_object_pmap_remove_callback(vm_page_t p, void *data);
1427
1428 void
1429 vm_object_pmap_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1430 {
1431         struct rb_vm_page_scan_info info;
1432
1433         if (object == NULL)
1434                 return;
1435         info.start_pindex = start;
1436         info.end_pindex = end - 1;
1437
1438         vm_object_hold(object);
1439         vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
1440                                 vm_object_pmap_remove_callback, &info);
1441         if (start == 0 && end == object->size)
1442                 vm_object_clear_flag(object, OBJ_WRITEABLE);
1443         vm_object_drop(object);
1444 }
1445
1446 /*
1447  * The caller must hold the object
1448  */
1449 static int
1450 vm_object_pmap_remove_callback(vm_page_t p, void *data __unused)
1451 {
1452         vm_page_protect(p, VM_PROT_NONE);
1453         return(0);
1454 }
1455
1456 /*
1457  * Implements the madvise function at the object/page level.
1458  *
1459  * MADV_WILLNEED        (any object)
1460  *
1461  *      Activate the specified pages if they are resident.
1462  *
1463  * MADV_DONTNEED        (any object)
1464  *
1465  *      Deactivate the specified pages if they are resident.
1466  *
1467  * MADV_FREE    (OBJT_DEFAULT/OBJT_SWAP objects, OBJ_ONEMAPPING only)
1468  *
1469  *      Deactivate and clean the specified pages if they are
1470  *      resident.  This permits the process to reuse the pages
1471  *      without faulting or the kernel to reclaim the pages
1472  *      without I/O.
1473  *
1474  * No requirements.
1475  */
1476 void
1477 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
1478 {
1479         vm_pindex_t end, tpindex;
1480         vm_object_t tobject;
1481         vm_object_t xobj;
1482         vm_page_t m;
1483         int error;
1484
1485         if (object == NULL)
1486                 return;
1487
1488         end = pindex + count;
1489
1490         vm_object_hold(object);
1491         tobject = object;
1492
1493         /*
1494          * Locate and adjust resident pages
1495          */
1496         for (; pindex < end; pindex += 1) {
1497 relookup:
1498                 if (tobject != object)
1499                         vm_object_drop(tobject);
1500                 tobject = object;
1501                 tpindex = pindex;
1502 shadowlookup:
1503                 /*
1504                  * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1505                  * and those pages must be OBJ_ONEMAPPING.
1506                  */
1507                 if (advise == MADV_FREE) {
1508                         if ((tobject->type != OBJT_DEFAULT &&
1509                              tobject->type != OBJT_SWAP) ||
1510                             (tobject->flags & OBJ_ONEMAPPING) == 0) {
1511                                 continue;
1512                         }
1513                 }
1514
1515                 m = vm_page_lookup_busy_try(tobject, tpindex, TRUE, &error);
1516
1517                 if (error) {
1518                         vm_page_sleep_busy(m, TRUE, "madvpo");
1519                         goto relookup;
1520                 }
1521                 if (m == NULL) {
1522                         /*
1523                          * There may be swap even if there is no backing page
1524                          */
1525                         if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1526                                 swap_pager_freespace(tobject, tpindex, 1);
1527
1528                         /*
1529                          * next object
1530                          */
1531                         while ((xobj = tobject->backing_object) != NULL) {
1532                                 KKASSERT(xobj != object);
1533                                 vm_object_hold(xobj);
1534                                 if (xobj == tobject->backing_object)
1535                                         break;
1536                                 vm_object_drop(xobj);
1537                         }
1538                         if (xobj == NULL)
1539                                 continue;
1540                         tpindex += OFF_TO_IDX(tobject->backing_object_offset);
1541                         if (tobject != object) {
1542                                 vm_object_lock_swap();
1543                                 vm_object_drop(tobject);
1544                         }
1545                         tobject = xobj;
1546                         goto shadowlookup;
1547                 }
1548
1549                 /*
1550                  * If the page is not in a normal active state, we skip it.
1551                  * If the page is not managed there are no page queues to
1552                  * mess with.  Things can break if we mess with pages in
1553                  * any of the below states.
1554                  */
1555                 if (m->wire_count ||
1556                     (m->flags & (PG_UNMANAGED | PG_NEED_COMMIT)) ||
1557                     m->valid != VM_PAGE_BITS_ALL
1558                 ) {
1559                         vm_page_wakeup(m);
1560                         continue;
1561                 }
1562
1563                 /*
1564                  * Theoretically once a page is known not to be busy, an
1565                  * interrupt cannot come along and rip it out from under us.
1566                  */
1567
1568                 if (advise == MADV_WILLNEED) {
1569                         vm_page_activate(m);
1570                 } else if (advise == MADV_DONTNEED) {
1571                         vm_page_dontneed(m);
1572                 } else if (advise == MADV_FREE) {
1573                         /*
1574                          * Mark the page clean.  This will allow the page
1575                          * to be freed up by the system.  However, such pages
1576                          * are often reused quickly by malloc()/free()
1577                          * so we do not do anything that would cause
1578                          * a page fault if we can help it.
1579                          *
1580                          * Specifically, we do not try to actually free
1581                          * the page now nor do we try to put it in the
1582                          * cache (which would cause a page fault on reuse).
1583                          *
1584                          * But we do make the page is freeable as we
1585                          * can without actually taking the step of unmapping
1586                          * it.
1587                          */
1588                         pmap_clear_modify(m);
1589                         m->dirty = 0;
1590                         m->act_count = 0;
1591                         vm_page_dontneed(m);
1592                         if (tobject->type == OBJT_SWAP)
1593                                 swap_pager_freespace(tobject, tpindex, 1);
1594                 }
1595                 vm_page_wakeup(m);
1596         }       
1597         if (tobject != object)
1598                 vm_object_drop(tobject);
1599         vm_object_drop(object);
1600 }
1601
1602 /*
1603  * Create a new object which is backed by the specified existing object
1604  * range.  Replace the pointer and offset that was pointing at the existing
1605  * object with the pointer/offset for the new object.
1606  *
1607  * No other requirements.
1608  */
1609 void
1610 vm_object_shadow(vm_object_t *objectp, vm_ooffset_t *offset, vm_size_t length,
1611                  int addref)
1612 {
1613         vm_object_t source;
1614         vm_object_t result;
1615
1616         source = *objectp;
1617
1618         /*
1619          * Don't create the new object if the old object isn't shared.
1620          * We have to chain wait before adding the reference to avoid
1621          * racing a collapse or deallocation.
1622          *
1623          * Add the additional ref to source here to avoid racing a later
1624          * collapse or deallocation. Clear the ONEMAPPING flag whether
1625          * addref is TRUE or not in this case because the original object
1626          * will be shadowed.
1627          */
1628         if (source) {
1629                 vm_object_hold(source);
1630                 vm_object_chain_wait(source);
1631                 if (source->ref_count == 1 &&
1632                     source->handle == NULL &&
1633                     (source->type == OBJT_DEFAULT ||
1634                      source->type == OBJT_SWAP)) {
1635                         vm_object_drop(source);
1636                         if (addref) {
1637                                 vm_object_reference_locked(source);
1638                                 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1639                         }
1640                         return;
1641                 }
1642                 vm_object_reference_locked(source);
1643                 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1644         }
1645
1646         /*
1647          * Allocate a new object with the given length.  The new object
1648          * is returned referenced but we may have to add another one.
1649          * If we are adding a second reference we must clear OBJ_ONEMAPPING.
1650          * (typically because the caller is about to clone a vm_map_entry).
1651          *
1652          * The source object currently has an extra reference to prevent
1653          * collapses into it while we mess with its shadow list, which
1654          * we will remove later in this routine.
1655          */
1656         if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
1657                 panic("vm_object_shadow: no object for shadowing");
1658         vm_object_hold(result);
1659         if (addref) {
1660                 vm_object_reference_locked(result);
1661                 vm_object_clear_flag(result, OBJ_ONEMAPPING);
1662         }
1663
1664         /*
1665          * The new object shadows the source object.  Chain wait before
1666          * adjusting shadow_count or the shadow list to avoid races.
1667          *
1668          * Try to optimize the result object's page color when shadowing
1669          * in order to maintain page coloring consistency in the combined 
1670          * shadowed object.
1671          */
1672         KKASSERT(result->backing_object == NULL);
1673         result->backing_object = source;
1674         if (source) {
1675                 vm_object_chain_wait(source);
1676                 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1677                 source->shadow_count++;
1678                 source->generation++;
1679                 /* cpu localization twist */
1680                 result->pg_color = (int)(intptr_t)curthread;
1681         }
1682
1683         /*
1684          * Adjust the return storage.  Drop the ref on source before
1685          * returning.
1686          */
1687         result->backing_object_offset = *offset;
1688         vm_object_drop(result);
1689         *offset = 0;
1690         if (source) {
1691                 vm_object_deallocate_locked(source);
1692                 vm_object_drop(source);
1693         }
1694
1695         /*
1696          * Return the new things
1697          */
1698         *objectp = result;
1699 }
1700
1701 #define OBSC_TEST_ALL_SHADOWED  0x0001
1702 #define OBSC_COLLAPSE_NOWAIT    0x0002
1703 #define OBSC_COLLAPSE_WAIT      0x0004
1704
1705 static int vm_object_backing_scan_callback(vm_page_t p, void *data);
1706
1707 /*
1708  * The caller must hold the object.
1709  */
1710 static __inline int
1711 vm_object_backing_scan(vm_object_t object, vm_object_t backing_object, int op)
1712 {
1713         struct rb_vm_page_scan_info info;
1714
1715         vm_object_assert_held(object);
1716         vm_object_assert_held(backing_object);
1717
1718         KKASSERT(backing_object == object->backing_object);
1719         info.backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1720
1721         /*
1722          * Initial conditions
1723          */
1724         if (op & OBSC_TEST_ALL_SHADOWED) {
1725                 /*
1726                  * We do not want to have to test for the existence of
1727                  * swap pages in the backing object.  XXX but with the
1728                  * new swapper this would be pretty easy to do.
1729                  *
1730                  * XXX what about anonymous MAP_SHARED memory that hasn't
1731                  * been ZFOD faulted yet?  If we do not test for this, the
1732                  * shadow test may succeed! XXX
1733                  */
1734                 if (backing_object->type != OBJT_DEFAULT)
1735                         return(0);
1736         }
1737         if (op & OBSC_COLLAPSE_WAIT) {
1738                 KKASSERT((backing_object->flags & OBJ_DEAD) == 0);
1739                 vm_object_set_flag(backing_object, OBJ_DEAD);
1740                 lwkt_gettoken(&vmobj_token);
1741                 TAILQ_REMOVE(&vm_object_list, backing_object, object_list);
1742                 vm_object_count--;
1743                 lwkt_reltoken(&vmobj_token);
1744                 vm_object_dead_wakeup(backing_object);
1745         }
1746
1747         /*
1748          * Our scan.   We have to retry if a negative error code is returned,
1749          * otherwise 0 or 1 will be returned in info.error.  0 Indicates that
1750          * the scan had to be stopped because the parent does not completely
1751          * shadow the child.
1752          */
1753         info.object = object;
1754         info.backing_object = backing_object;
1755         info.limit = op;
1756         do {
1757                 info.error = 1;
1758                 vm_page_rb_tree_RB_SCAN(&backing_object->rb_memq, NULL,
1759                                         vm_object_backing_scan_callback,
1760                                         &info);
1761         } while (info.error < 0);
1762
1763         return(info.error);
1764 }
1765
1766 /*
1767  * The caller must hold the object.
1768  */
1769 static int
1770 vm_object_backing_scan_callback(vm_page_t p, void *data)
1771 {
1772         struct rb_vm_page_scan_info *info = data;
1773         vm_object_t backing_object;
1774         vm_object_t object;
1775         vm_pindex_t pindex;
1776         vm_pindex_t new_pindex;
1777         vm_pindex_t backing_offset_index;
1778         int op;
1779
1780         pindex = p->pindex;
1781         new_pindex = pindex - info->backing_offset_index;
1782         op = info->limit;
1783         object = info->object;
1784         backing_object = info->backing_object;
1785         backing_offset_index = info->backing_offset_index;
1786
1787         if (op & OBSC_TEST_ALL_SHADOWED) {
1788                 vm_page_t pp;
1789
1790                 /*
1791                  * Ignore pages outside the parent object's range
1792                  * and outside the parent object's mapping of the 
1793                  * backing object.
1794                  *
1795                  * note that we do not busy the backing object's
1796                  * page.
1797                  */
1798                 if (pindex < backing_offset_index ||
1799                     new_pindex >= object->size
1800                 ) {
1801                         return(0);
1802                 }
1803
1804                 /*
1805                  * See if the parent has the page or if the parent's
1806                  * object pager has the page.  If the parent has the
1807                  * page but the page is not valid, the parent's
1808                  * object pager must have the page.
1809                  *
1810                  * If this fails, the parent does not completely shadow
1811                  * the object and we might as well give up now.
1812                  */
1813                 pp = vm_page_lookup(object, new_pindex);
1814                 if ((pp == NULL || pp->valid == 0) &&
1815                     !vm_pager_has_page(object, new_pindex)
1816                 ) {
1817                         info->error = 0;        /* problemo */
1818                         return(-1);             /* stop the scan */
1819                 }
1820         }
1821
1822         /*
1823          * Check for busy page.  Note that we may have lost (p) when we
1824          * possibly blocked above.
1825          */
1826         if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1827                 vm_page_t pp;
1828
1829                 if (vm_page_busy_try(p, TRUE)) {
1830                         if (op & OBSC_COLLAPSE_NOWAIT) {
1831                                 return(0);
1832                         } else {
1833                                 /*
1834                                  * If we slept, anything could have
1835                                  * happened.   Ask that the scan be restarted.
1836                                  *
1837                                  * Since the object is marked dead, the
1838                                  * backing offset should not have changed.  
1839                                  */
1840                                 vm_page_sleep_busy(p, TRUE, "vmocol");
1841                                 info->error = -1;
1842                                 return(-1);
1843                         }
1844                 }
1845
1846                 /*
1847                  * If (p) is no longer valid restart the scan.
1848                  */
1849                 if (p->object != backing_object || p->pindex != pindex) {
1850                         kprintf("vm_object_backing_scan: Warning: page "
1851                                 "%p ripped out from under us\n", p);
1852                         vm_page_wakeup(p);
1853                         info->error = -1;
1854                         return(-1);
1855                 }
1856
1857                 if (op & OBSC_COLLAPSE_NOWAIT) {
1858                         if (p->valid == 0 ||
1859                             p->wire_count ||
1860                             (p->flags & PG_NEED_COMMIT)) {
1861                                 vm_page_wakeup(p);
1862                                 return(0);
1863                         }
1864                 } else {
1865                         /* XXX what if p->valid == 0 , hold_count, etc? */
1866                 }
1867
1868                 KASSERT(
1869                     p->object == backing_object,
1870                     ("vm_object_qcollapse(): object mismatch")
1871                 );
1872
1873                 /*
1874                  * Destroy any associated swap
1875                  */
1876                 if (backing_object->type == OBJT_SWAP)
1877                         swap_pager_freespace(backing_object, p->pindex, 1);
1878
1879                 if (
1880                     p->pindex < backing_offset_index ||
1881                     new_pindex >= object->size
1882                 ) {
1883                         /*
1884                          * Page is out of the parent object's range, we 
1885                          * can simply destroy it. 
1886                          */
1887                         vm_page_protect(p, VM_PROT_NONE);
1888                         vm_page_free(p);
1889                         return(0);
1890                 }
1891
1892                 pp = vm_page_lookup(object, new_pindex);
1893                 if (pp != NULL || vm_pager_has_page(object, new_pindex)) {
1894                         /*
1895                          * page already exists in parent OR swap exists
1896                          * for this location in the parent.  Destroy 
1897                          * the original page from the backing object.
1898                          *
1899                          * Leave the parent's page alone
1900                          */
1901                         vm_page_protect(p, VM_PROT_NONE);
1902                         vm_page_free(p);
1903                         return(0);
1904                 }
1905
1906                 /*
1907                  * Page does not exist in parent, rename the
1908                  * page from the backing object to the main object. 
1909                  *
1910                  * If the page was mapped to a process, it can remain 
1911                  * mapped through the rename.
1912                  */
1913                 if ((p->queue - p->pc) == PQ_CACHE)
1914                         vm_page_deactivate(p);
1915
1916                 vm_page_rename(p, object, new_pindex);
1917                 vm_page_wakeup(p);
1918                 /* page automatically made dirty by rename */
1919         }
1920         return(0);
1921 }
1922
1923 /*
1924  * This version of collapse allows the operation to occur earlier and
1925  * when paging_in_progress is true for an object...  This is not a complete
1926  * operation, but should plug 99.9% of the rest of the leaks.
1927  *
1928  * The caller must hold the object and backing_object and both must be
1929  * chainlocked.
1930  *
1931  * (only called from vm_object_collapse)
1932  */
1933 static void
1934 vm_object_qcollapse(vm_object_t object, vm_object_t backing_object)
1935 {
1936         if (backing_object->ref_count == 1) {
1937                 backing_object->ref_count += 2;
1938                 vm_object_backing_scan(object, backing_object,
1939                                        OBSC_COLLAPSE_NOWAIT);
1940                 backing_object->ref_count -= 2;
1941         }
1942 }
1943
1944 /*
1945  * Collapse an object with the object backing it.  Pages in the backing
1946  * object are moved into the parent, and the backing object is deallocated.
1947  * Any conflict is resolved in favor of the parent's existing pages.
1948  *
1949  * object must be held and chain-locked on call.
1950  *
1951  * The caller must have an extra ref on object to prevent a race from
1952  * destroying it during the collapse.
1953  */
1954 void
1955 vm_object_collapse(vm_object_t object, struct vm_object_dealloc_list **dlistp)
1956 {
1957         struct vm_object_dealloc_list *dlist = NULL;
1958         vm_object_t backing_object;
1959
1960         /*
1961          * Only one thread is attempting a collapse at any given moment.
1962          * There are few restrictions for (object) that callers of this
1963          * function check so reentrancy is likely.
1964          */
1965         KKASSERT(object != NULL);
1966         vm_object_assert_held(object);
1967         KKASSERT(object->flags & OBJ_CHAINLOCK);
1968
1969         for (;;) {
1970                 vm_object_t bbobj;
1971                 int dodealloc;
1972
1973                 /*
1974                  * We have to hold the backing object, check races.
1975                  */
1976                 while ((backing_object = object->backing_object) != NULL) {
1977                         vm_object_hold(backing_object);
1978                         if (backing_object == object->backing_object)
1979                                 break;
1980                         vm_object_drop(backing_object);
1981                 }
1982
1983                 /*
1984                  * No backing object?  Nothing to collapse then.
1985                  */
1986                 if (backing_object == NULL)
1987                         break;
1988
1989                 /*
1990                  * You can't collapse with a non-default/non-swap object.
1991                  */
1992                 if (backing_object->type != OBJT_DEFAULT &&
1993                     backing_object->type != OBJT_SWAP) {
1994                         vm_object_drop(backing_object);
1995                         backing_object = NULL;
1996                         break;
1997                 }
1998
1999                 /*
2000                  * Chain-lock the backing object too because if we
2001                  * successfully merge its pages into the top object we
2002                  * will collapse backing_object->backing_object as the
2003                  * new backing_object.  Re-check that it is still our
2004                  * backing object.
2005                  */
2006                 vm_object_chain_acquire(backing_object);
2007                 if (backing_object != object->backing_object) {
2008                         vm_object_chain_release(backing_object);
2009                         vm_object_drop(backing_object);
2010                         continue;
2011                 }
2012
2013                 /*
2014                  * we check the backing object first, because it is most likely
2015                  * not collapsable.
2016                  */
2017                 if (backing_object->handle != NULL ||
2018                     (backing_object->type != OBJT_DEFAULT &&
2019                      backing_object->type != OBJT_SWAP) ||
2020                     (backing_object->flags & OBJ_DEAD) ||
2021                     object->handle != NULL ||
2022                     (object->type != OBJT_DEFAULT &&
2023                      object->type != OBJT_SWAP) ||
2024                     (object->flags & OBJ_DEAD)) {
2025                         break;
2026                 }
2027
2028                 /*
2029                  * If paging is in progress we can't do a normal collapse.
2030                  */
2031                 if (
2032                     object->paging_in_progress != 0 ||
2033                     backing_object->paging_in_progress != 0
2034                 ) {
2035                         vm_object_qcollapse(object, backing_object);
2036                         break;
2037                 }
2038
2039                 /*
2040                  * We know that we can either collapse the backing object (if
2041                  * the parent is the only reference to it) or (perhaps) have
2042                  * the parent bypass the object if the parent happens to shadow
2043                  * all the resident pages in the entire backing object.
2044                  *
2045                  * This is ignoring pager-backed pages such as swap pages.
2046                  * vm_object_backing_scan fails the shadowing test in this
2047                  * case.
2048                  */
2049                 if (backing_object->ref_count == 1) {
2050                         /*
2051                          * If there is exactly one reference to the backing
2052                          * object, we can collapse it into the parent.  
2053                          */
2054                         KKASSERT(object->backing_object == backing_object);
2055                         vm_object_backing_scan(object, backing_object,
2056                                                OBSC_COLLAPSE_WAIT);
2057
2058                         /*
2059                          * Move the pager from backing_object to object.
2060                          */
2061                         if (backing_object->type == OBJT_SWAP) {
2062                                 vm_object_pip_add(backing_object, 1);
2063
2064                                 /*
2065                                  * scrap the paging_offset junk and do a 
2066                                  * discrete copy.  This also removes major 
2067                                  * assumptions about how the swap-pager 
2068                                  * works from where it doesn't belong.  The
2069                                  * new swapper is able to optimize the
2070                                  * destroy-source case.
2071                                  */
2072                                 vm_object_pip_add(object, 1);
2073                                 swap_pager_copy(backing_object, object,
2074                                     OFF_TO_IDX(object->backing_object_offset),
2075                                     TRUE);
2076                                 vm_object_pip_wakeup(object);
2077                                 vm_object_pip_wakeup(backing_object);
2078                         }
2079
2080                         /*
2081                          * Object now shadows whatever backing_object did.
2082                          * Remove object from backing_object's shadow_list.
2083                          */
2084                         LIST_REMOVE(object, shadow_list);
2085                         KKASSERT(object->backing_object == backing_object);
2086                         backing_object->shadow_count--;
2087                         backing_object->generation++;
2088
2089                         /*
2090                          * backing_object->backing_object moves from within
2091                          * backing_object to within object.
2092                          */
2093                         while ((bbobj = backing_object->backing_object) != NULL) {
2094                                 vm_object_hold(bbobj);
2095                                 if (bbobj == backing_object->backing_object)
2096                                         break;
2097                                 vm_object_drop(bbobj);
2098                         }
2099                         if (bbobj) {
2100                                 LIST_REMOVE(backing_object, shadow_list);
2101                                 bbobj->shadow_count--;
2102                                 bbobj->generation++;
2103                                 backing_object->backing_object = NULL;
2104                         }
2105                         object->backing_object = bbobj;
2106                         if (bbobj) {
2107                                 LIST_INSERT_HEAD(&bbobj->shadow_head,
2108                                                  object, shadow_list);
2109                                 bbobj->shadow_count++;
2110                                 bbobj->generation++;
2111                         }
2112
2113                         object->backing_object_offset +=
2114                                 backing_object->backing_object_offset;
2115
2116                         vm_object_drop(bbobj);
2117
2118                         /*
2119                          * Discard the old backing_object.  Nothing should be
2120                          * able to ref it, other than a vm_map_split(),
2121                          * and vm_map_split() will stall on our chain lock.
2122                          * And we control the parent so it shouldn't be
2123                          * possible for it to go away either.
2124                          *
2125                          * Since the backing object has no pages, no pager
2126                          * left, and no object references within it, all
2127                          * that is necessary is to dispose of it.
2128                          */
2129                         KASSERT(backing_object->ref_count == 1,
2130                                 ("backing_object %p was somehow "
2131                                  "re-referenced during collapse!",
2132                                  backing_object));
2133                         KASSERT(RB_EMPTY(&backing_object->rb_memq),
2134                                 ("backing_object %p somehow has left "
2135                                  "over pages during collapse!",
2136                                  backing_object));
2137
2138                         /*
2139                          * The object can be destroyed.
2140                          *
2141                          * XXX just fall through and dodealloc instead
2142                          *     of forcing destruction?
2143                          */
2144                         --backing_object->ref_count;
2145                         if ((backing_object->flags & OBJ_DEAD) == 0)
2146                                 vm_object_terminate(backing_object);
2147                         object_collapses++;
2148                         dodealloc = 0;
2149                 } else {
2150                         /*
2151                          * If we do not entirely shadow the backing object,
2152                          * there is nothing we can do so we give up.
2153                          */
2154                         if (vm_object_backing_scan(object, backing_object,
2155                                                 OBSC_TEST_ALL_SHADOWED) == 0) {
2156                                 break;
2157                         }
2158
2159                         /*
2160                          * bbobj is backing_object->backing_object.  Since
2161                          * object completely shadows backing_object we can
2162                          * bypass it and become backed by bbobj instead.
2163                          */
2164                         while ((bbobj = backing_object->backing_object) != NULL) {
2165                                 vm_object_hold(bbobj);
2166                                 if (bbobj == backing_object->backing_object)
2167                                         break;
2168                                 vm_object_drop(bbobj);
2169                         }
2170
2171                         /*
2172                          * Make object shadow bbobj instead of backing_object.
2173                          * Remove object from backing_object's shadow list.
2174                          *
2175                          * Deallocating backing_object will not remove
2176                          * it, since its reference count is at least 2.
2177                          */
2178                         KKASSERT(object->backing_object == backing_object);
2179                         LIST_REMOVE(object, shadow_list);
2180                         backing_object->shadow_count--;
2181                         backing_object->generation++;
2182
2183                         /*
2184                          * Add a ref to bbobj, bbobj now shadows object.
2185                          *
2186                          * NOTE: backing_object->backing_object still points
2187                          *       to bbobj.  That relationship remains intact
2188                          *       because backing_object has > 1 ref, so
2189                          *       someone else is pointing to it (hence why
2190                          *       we can't collapse it into object and can
2191                          *       only handle the all-shadowed bypass case).
2192                          */
2193                         if (bbobj) {
2194                                 vm_object_chain_wait(bbobj);
2195                                 vm_object_reference_locked(bbobj);
2196                                 LIST_INSERT_HEAD(&bbobj->shadow_head,
2197                                                  object, shadow_list);
2198                                 bbobj->shadow_count++;
2199                                 bbobj->generation++;
2200                                 object->backing_object_offset +=
2201                                         backing_object->backing_object_offset;
2202                                 object->backing_object = bbobj;
2203                                 vm_object_drop(bbobj);
2204                         } else {
2205                                 object->backing_object = NULL;
2206                         }
2207
2208                         /*
2209                          * Drop the reference count on backing_object.  To
2210                          * handle ref_count races properly we can't assume
2211                          * that the ref_count is still at least 2 so we
2212                          * have to actually call vm_object_deallocate()
2213                          * (after clearing the chainlock).
2214                          */
2215                         object_bypasses++;
2216                         dodealloc = 1;
2217                 }
2218
2219                 /*
2220                  * Ok, we want to loop on the new object->bbobj association,
2221                  * possibly collapsing it further.  However if dodealloc is
2222                  * non-zero we have to deallocate the backing_object which
2223                  * itself can potentially undergo a collapse, creating a
2224                  * recursion depth issue with the LWKT token subsystem.
2225                  *
2226                  * In the case where we must deallocate the backing_object
2227                  * it is possible now that the backing_object has a single
2228                  * shadow count on some other object (not represented here
2229                  * as yet), since it no longer shadows us.  Thus when we
2230                  * call vm_object_deallocate() it may attempt to collapse
2231                  * itself into its remaining parent.
2232                  */
2233                 if (dodealloc) {
2234                         struct vm_object_dealloc_list *dtmp;
2235
2236                         vm_object_chain_release(backing_object);
2237                         vm_object_unlock(backing_object);
2238                         /* backing_object remains held */
2239
2240                         /*
2241                          * Auto-deallocation list for caller convenience.
2242                          */
2243                         if (dlistp == NULL)
2244                                 dlistp = &dlist;
2245
2246                         dtmp = kmalloc(sizeof(*dtmp), M_TEMP, M_WAITOK);
2247                         dtmp->object = backing_object;
2248                         dtmp->next = *dlistp;
2249                         *dlistp = dtmp;
2250                 } else {
2251                         vm_object_chain_release(backing_object);
2252                         vm_object_drop(backing_object);
2253                 }
2254                 /* backing_object = NULL; not needed */
2255                 /* loop */
2256         }
2257
2258         /*
2259          * Clean up any left over backing_object
2260          */
2261         if (backing_object) {
2262                 vm_object_chain_release(backing_object);
2263                 vm_object_drop(backing_object);
2264         }
2265
2266         /*
2267          * Clean up any auto-deallocation list.  This is a convenience
2268          * for top-level callers so they don't have to pass &dlist.
2269          * Do not clean up any caller-passed dlistp, the caller will
2270          * do that.
2271          */
2272         if (dlist)
2273                 vm_object_deallocate_list(&dlist);
2274
2275 }
2276
2277 /*
2278  * vm_object_collapse() may collect additional objects in need of
2279  * deallocation.  This routine deallocates these objects.  The
2280  * deallocation itself can trigger additional collapses (which the
2281  * deallocate function takes care of).  This procedure is used to
2282  * reduce procedural recursion since these vm_object shadow chains
2283  * can become quite long.
2284  */
2285 void
2286 vm_object_deallocate_list(struct vm_object_dealloc_list **dlistp)
2287 {
2288         struct vm_object_dealloc_list *dlist;
2289
2290         while ((dlist = *dlistp) != NULL) {
2291                 *dlistp = dlist->next;
2292                 vm_object_lock(dlist->object);
2293                 vm_object_deallocate_locked(dlist->object);
2294                 vm_object_drop(dlist->object);
2295                 kfree(dlist, M_TEMP);
2296         }
2297 }
2298
2299 /*
2300  * Removes all physical pages in the specified object range from the
2301  * object's list of pages.
2302  *
2303  * No requirements.
2304  */
2305 static int vm_object_page_remove_callback(vm_page_t p, void *data);
2306
2307 void
2308 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
2309                       boolean_t clean_only)
2310 {
2311         struct rb_vm_page_scan_info info;
2312         int all;
2313
2314         /*
2315          * Degenerate cases and assertions
2316          */
2317         vm_object_hold(object);
2318         if (object == NULL ||
2319             (object->resident_page_count == 0 && object->swblock_count == 0)) {
2320                 vm_object_drop(object);
2321                 return;
2322         }
2323         KASSERT(object->type != OBJT_PHYS, 
2324                 ("attempt to remove pages from a physical object"));
2325
2326         /*
2327          * Indicate that paging is occuring on the object
2328          */
2329         vm_object_pip_add(object, 1);
2330
2331         /*
2332          * Figure out the actual removal range and whether we are removing
2333          * the entire contents of the object or not.  If removing the entire
2334          * contents, be sure to get all pages, even those that might be 
2335          * beyond the end of the object.
2336          */
2337         info.start_pindex = start;
2338         if (end == 0)
2339                 info.end_pindex = (vm_pindex_t)-1;
2340         else
2341                 info.end_pindex = end - 1;
2342         info.limit = clean_only;
2343         all = (start == 0 && info.end_pindex >= object->size - 1);
2344
2345         /*
2346          * Loop until we are sure we have gotten them all.
2347          */
2348         do {
2349                 info.error = 0;
2350                 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2351                                         vm_object_page_remove_callback, &info);
2352         } while (info.error);
2353
2354         /*
2355          * Remove any related swap if throwing away pages, or for
2356          * non-swap objects (the swap is a clean copy in that case).
2357          */
2358         if (object->type != OBJT_SWAP || clean_only == FALSE) {
2359                 if (all)
2360                         swap_pager_freespace_all(object);
2361                 else
2362                         swap_pager_freespace(object, info.start_pindex,
2363                              info.end_pindex - info.start_pindex + 1);
2364         }
2365
2366         /*
2367          * Cleanup
2368          */
2369         vm_object_pip_wakeup(object);
2370         vm_object_drop(object);
2371 }
2372
2373 /*
2374  * The caller must hold the object
2375  */
2376 static int
2377 vm_object_page_remove_callback(vm_page_t p, void *data)
2378 {
2379         struct rb_vm_page_scan_info *info = data;
2380
2381         if (vm_page_busy_try(p, TRUE)) {
2382                 vm_page_sleep_busy(p, TRUE, "vmopar");
2383                 info->error = 1;
2384                 return(0);
2385         }
2386
2387         /*
2388          * Wired pages cannot be destroyed, but they can be invalidated
2389          * and we do so if clean_only (limit) is not set.
2390          *
2391          * WARNING!  The page may be wired due to being part of a buffer
2392          *           cache buffer, and the buffer might be marked B_CACHE.
2393          *           This is fine as part of a truncation but VFSs must be
2394          *           sure to fix the buffer up when re-extending the file.
2395          *
2396          * NOTE!     PG_NEED_COMMIT is ignored.
2397          */
2398         if (p->wire_count != 0) {
2399                 vm_page_protect(p, VM_PROT_NONE);
2400                 if (info->limit == 0)
2401                         p->valid = 0;
2402                 vm_page_wakeup(p);
2403                 return(0);
2404         }
2405
2406         /*
2407          * limit is our clean_only flag.  If set and the page is dirty or
2408          * requires a commit, do not free it.  If set and the page is being
2409          * held by someone, do not free it.
2410          */
2411         if (info->limit && p->valid) {
2412                 vm_page_test_dirty(p);
2413                 if ((p->valid & p->dirty) || (p->flags & PG_NEED_COMMIT)) {
2414                         vm_page_wakeup(p);
2415                         return(0);
2416                 }
2417 #if 0
2418                 if (p->hold_count) {
2419                         vm_page_wakeup(p);
2420                         return(0);
2421                 }
2422 #endif
2423         }
2424
2425         /*
2426          * Destroy the page
2427          */
2428         vm_page_protect(p, VM_PROT_NONE);
2429         vm_page_free(p);
2430         return(0);
2431 }
2432
2433 /*
2434  * Coalesces two objects backing up adjoining regions of memory into a
2435  * single object.
2436  *
2437  * returns TRUE if objects were combined.
2438  *
2439  * NOTE: Only works at the moment if the second object is NULL -
2440  *       if it's not, which object do we lock first?
2441  *
2442  * Parameters:
2443  *      prev_object     First object to coalesce
2444  *      prev_offset     Offset into prev_object
2445  *      next_object     Second object into coalesce
2446  *      next_offset     Offset into next_object
2447  *
2448  *      prev_size       Size of reference to prev_object
2449  *      next_size       Size of reference to next_object
2450  *
2451  * The caller does not need to hold (prev_object) but must have a stable
2452  * pointer to it (typically by holding the vm_map locked).
2453  */
2454 boolean_t
2455 vm_object_coalesce(vm_object_t prev_object, vm_pindex_t prev_pindex,
2456                    vm_size_t prev_size, vm_size_t next_size)
2457 {
2458         vm_pindex_t next_pindex;
2459
2460         if (prev_object == NULL)
2461                 return (TRUE);
2462
2463         vm_object_hold(prev_object);
2464
2465         if (prev_object->type != OBJT_DEFAULT &&
2466             prev_object->type != OBJT_SWAP) {
2467                 vm_object_drop(prev_object);
2468                 return (FALSE);
2469         }
2470
2471         /*
2472          * Try to collapse the object first
2473          */
2474         vm_object_chain_acquire(prev_object);
2475         vm_object_collapse(prev_object, NULL);
2476
2477         /*
2478          * Can't coalesce if: . more than one reference . paged out . shadows
2479          * another object . has a copy elsewhere (any of which mean that the
2480          * pages not mapped to prev_entry may be in use anyway)
2481          */
2482
2483         if (prev_object->backing_object != NULL) {
2484                 vm_object_chain_release(prev_object);
2485                 vm_object_drop(prev_object);
2486                 return (FALSE);
2487         }
2488
2489         prev_size >>= PAGE_SHIFT;
2490         next_size >>= PAGE_SHIFT;
2491         next_pindex = prev_pindex + prev_size;
2492
2493         if ((prev_object->ref_count > 1) &&
2494             (prev_object->size != next_pindex)) {
2495                 vm_object_chain_release(prev_object);
2496                 vm_object_drop(prev_object);
2497                 return (FALSE);
2498         }
2499
2500         /*
2501          * Remove any pages that may still be in the object from a previous
2502          * deallocation.
2503          */
2504         if (next_pindex < prev_object->size) {
2505                 vm_object_page_remove(prev_object,
2506                                       next_pindex,
2507                                       next_pindex + next_size, FALSE);
2508                 if (prev_object->type == OBJT_SWAP)
2509                         swap_pager_freespace(prev_object,
2510                                              next_pindex, next_size);
2511         }
2512
2513         /*
2514          * Extend the object if necessary.
2515          */
2516         if (next_pindex + next_size > prev_object->size)
2517                 prev_object->size = next_pindex + next_size;
2518
2519         vm_object_chain_release(prev_object);
2520         vm_object_drop(prev_object);
2521         return (TRUE);
2522 }
2523
2524 /*
2525  * Make the object writable and flag is being possibly dirty.
2526  *
2527  * The caller must hold the object. XXX called from vm_page_dirty(),
2528  * There is currently no requirement to hold the object.
2529  */
2530 void
2531 vm_object_set_writeable_dirty(vm_object_t object)
2532 {
2533         struct vnode *vp;
2534
2535         /*vm_object_assert_held(object);*/
2536         /*
2537          * Avoid contention in vm fault path by checking the state before
2538          * issuing an atomic op on it.
2539          */
2540         if ((object->flags & (OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY)) !=
2541             (OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY)) {
2542                 vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
2543         }
2544         if (object->type == OBJT_VNODE &&
2545             (vp = (struct vnode *)object->handle) != NULL) {
2546                 if ((vp->v_flag & VOBJDIRTY) == 0) {
2547                         vsetflags(vp, VOBJDIRTY);
2548                 }
2549         }
2550 }
2551
2552 #include "opt_ddb.h"
2553 #ifdef DDB
2554 #include <sys/kernel.h>
2555
2556 #include <sys/cons.h>
2557
2558 #include <ddb/ddb.h>
2559
2560 static int      _vm_object_in_map (vm_map_t map, vm_object_t object,
2561                                        vm_map_entry_t entry);
2562 static int      vm_object_in_map (vm_object_t object);
2563
2564 /*
2565  * The caller must hold the object.
2566  */
2567 static int
2568 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2569 {
2570         vm_map_t tmpm;
2571         vm_map_entry_t tmpe;
2572         vm_object_t obj, nobj;
2573         int entcount;
2574
2575         if (map == 0)
2576                 return 0;
2577         if (entry == 0) {
2578                 tmpe = map->header.next;
2579                 entcount = map->nentries;
2580                 while (entcount-- && (tmpe != &map->header)) {
2581                         if( _vm_object_in_map(map, object, tmpe)) {
2582                                 return 1;
2583                         }
2584                         tmpe = tmpe->next;
2585                 }
2586                 return (0);
2587         }
2588         switch(entry->maptype) {
2589         case VM_MAPTYPE_SUBMAP:
2590                 tmpm = entry->object.sub_map;
2591                 tmpe = tmpm->header.next;
2592                 entcount = tmpm->nentries;
2593                 while (entcount-- && tmpe != &tmpm->header) {
2594                         if( _vm_object_in_map(tmpm, object, tmpe)) {
2595                                 return 1;
2596                         }
2597                         tmpe = tmpe->next;
2598                 }
2599                 break;
2600         case VM_MAPTYPE_NORMAL:
2601         case VM_MAPTYPE_VPAGETABLE:
2602                 obj = entry->object.vm_object;
2603                 while (obj) {
2604                         if (obj == object) {
2605                                 if (obj != entry->object.vm_object)
2606                                         vm_object_drop(obj);
2607                                 return 1;
2608                         }
2609                         while ((nobj = obj->backing_object) != NULL) {
2610                                 vm_object_hold(nobj);
2611                                 if (nobj == obj->backing_object)
2612                                         break;
2613                                 vm_object_drop(nobj);
2614                         }
2615                         if (obj != entry->object.vm_object) {
2616                                 if (nobj)
2617                                         vm_object_lock_swap();
2618                                 vm_object_drop(obj);
2619                         }
2620                         obj = nobj;
2621                 }
2622                 break;
2623         default:
2624                 break;
2625         }
2626         return 0;
2627 }
2628
2629 static int vm_object_in_map_callback(struct proc *p, void *data);
2630
2631 struct vm_object_in_map_info {
2632         vm_object_t object;
2633         int rv;
2634 };
2635
2636 /*
2637  * Debugging only
2638  */
2639 static int
2640 vm_object_in_map(vm_object_t object)
2641 {
2642         struct vm_object_in_map_info info;
2643
2644         info.rv = 0;
2645         info.object = object;
2646
2647         allproc_scan(vm_object_in_map_callback, &info);
2648         if (info.rv)
2649                 return 1;
2650         if( _vm_object_in_map(&kernel_map, object, 0))
2651                 return 1;
2652         if( _vm_object_in_map(&pager_map, object, 0))
2653                 return 1;
2654         if( _vm_object_in_map(&buffer_map, object, 0))
2655                 return 1;
2656         return 0;
2657 }
2658
2659 /*
2660  * Debugging only
2661  */
2662 static int
2663 vm_object_in_map_callback(struct proc *p, void *data)
2664 {
2665         struct vm_object_in_map_info *info = data;
2666
2667         if (p->p_vmspace) {
2668                 if (_vm_object_in_map(&p->p_vmspace->vm_map, info->object, 0)) {
2669                         info->rv = 1;
2670                         return -1;
2671                 }
2672         }
2673         return (0);
2674 }
2675
2676 DB_SHOW_COMMAND(vmochk, vm_object_check)
2677 {
2678         vm_object_t object;
2679
2680         /*
2681          * make sure that internal objs are in a map somewhere
2682          * and none have zero ref counts.
2683          */
2684         for (object = TAILQ_FIRST(&vm_object_list);
2685                         object != NULL;
2686                         object = TAILQ_NEXT(object, object_list)) {
2687                 if (object->type == OBJT_MARKER)
2688                         continue;
2689                 if (object->handle == NULL &&
2690                     (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2691                         if (object->ref_count == 0) {
2692                                 db_printf("vmochk: internal obj has zero ref count: %ld\n",
2693                                         (long)object->size);
2694                         }
2695                         if (!vm_object_in_map(object)) {
2696                                 db_printf(
2697                         "vmochk: internal obj is not in a map: "
2698                         "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2699                                     object->ref_count, (u_long)object->size, 
2700                                     (u_long)object->size,
2701                                     (void *)object->backing_object);
2702                         }
2703                 }
2704         }
2705 }
2706
2707 /*
2708  * Debugging only
2709  */
2710 DB_SHOW_COMMAND(object, vm_object_print_static)
2711 {
2712         /* XXX convert args. */
2713         vm_object_t object = (vm_object_t)addr;
2714         boolean_t full = have_addr;
2715
2716         vm_page_t p;
2717
2718         /* XXX count is an (unused) arg.  Avoid shadowing it. */
2719 #define count   was_count
2720
2721         int count;
2722
2723         if (object == NULL)
2724                 return;
2725
2726         db_iprintf(
2727             "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
2728             object, (int)object->type, (u_long)object->size,
2729             object->resident_page_count, object->ref_count, object->flags);
2730         /*
2731          * XXX no %qd in kernel.  Truncate object->backing_object_offset.
2732          */
2733         db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
2734             object->shadow_count, 
2735             object->backing_object ? object->backing_object->ref_count : 0,
2736             object->backing_object, (long)object->backing_object_offset);
2737
2738         if (!full)
2739                 return;
2740
2741         db_indent += 2;
2742         count = 0;
2743         RB_FOREACH(p, vm_page_rb_tree, &object->rb_memq) {
2744                 if (count == 0)
2745                         db_iprintf("memory:=");
2746                 else if (count == 6) {
2747                         db_printf("\n");
2748                         db_iprintf(" ...");
2749                         count = 0;
2750                 } else
2751                         db_printf(",");
2752                 count++;
2753
2754                 db_printf("(off=0x%lx,page=0x%lx)",
2755                     (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
2756         }
2757         if (count != 0)
2758                 db_printf("\n");
2759         db_indent -= 2;
2760 }
2761
2762 /* XXX. */
2763 #undef count
2764
2765 /*
2766  * XXX need this non-static entry for calling from vm_map_print.
2767  *
2768  * Debugging only
2769  */
2770 void
2771 vm_object_print(/* db_expr_t */ long addr,
2772                 boolean_t have_addr,
2773                 /* db_expr_t */ long count,
2774                 char *modif)
2775 {
2776         vm_object_print_static(addr, have_addr, count, modif);
2777 }
2778
2779 /*
2780  * Debugging only
2781  */
2782 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2783 {
2784         vm_object_t object;
2785         int nl = 0;
2786         int c;
2787         for (object = TAILQ_FIRST(&vm_object_list);
2788                         object != NULL;
2789                         object = TAILQ_NEXT(object, object_list)) {
2790                 vm_pindex_t idx, fidx;
2791                 vm_pindex_t osize;
2792                 vm_paddr_t pa = -1, padiff;
2793                 int rcount;
2794                 vm_page_t m;
2795
2796                 if (object->type == OBJT_MARKER)
2797                         continue;
2798                 db_printf("new object: %p\n", (void *)object);
2799                 if ( nl > 18) {
2800                         c = cngetc();
2801                         if (c != ' ')
2802                                 return;
2803                         nl = 0;
2804                 }
2805                 nl++;
2806                 rcount = 0;
2807                 fidx = 0;
2808                 osize = object->size;
2809                 if (osize > 128)
2810                         osize = 128;
2811                 for (idx = 0; idx < osize; idx++) {
2812                         m = vm_page_lookup(object, idx);
2813                         if (m == NULL) {
2814                                 if (rcount) {
2815                                         db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2816                                                 (long)fidx, rcount, (long)pa);
2817                                         if ( nl > 18) {
2818                                                 c = cngetc();
2819                                                 if (c != ' ')
2820                                                         return;
2821                                                 nl = 0;
2822                                         }
2823                                         nl++;
2824                                         rcount = 0;
2825                                 }
2826                                 continue;
2827                         }
2828
2829                                 
2830                         if (rcount &&
2831                                 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2832                                 ++rcount;
2833                                 continue;
2834                         }
2835                         if (rcount) {
2836                                 padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
2837                                 padiff >>= PAGE_SHIFT;
2838                                 padiff &= PQ_L2_MASK;
2839                                 if (padiff == 0) {
2840                                         pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
2841                                         ++rcount;
2842                                         continue;
2843                                 }
2844                                 db_printf(" index(%ld)run(%d)pa(0x%lx)",
2845                                         (long)fidx, rcount, (long)pa);
2846                                 db_printf("pd(%ld)\n", (long)padiff);
2847                                 if ( nl > 18) {
2848                                         c = cngetc();
2849                                         if (c != ' ')
2850                                                 return;
2851                                         nl = 0;
2852                                 }
2853                                 nl++;
2854                         }
2855                         fidx = idx;
2856                         pa = VM_PAGE_TO_PHYS(m);
2857                         rcount = 1;
2858                 }
2859                 if (rcount) {
2860                         db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2861                                 (long)fidx, rcount, (long)pa);
2862                         if ( nl > 18) {
2863                                 c = cngetc();
2864                                 if (c != ' ')
2865                                         return;
2866                                 nl = 0;
2867                         }
2868                         nl++;
2869                 }
2870         }
2871 }
2872 #endif /* DDB */