]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/vm/vm_object.c
vm_pageout_flush() might cache the pages that finished write to the
[FreeBSD/FreeBSD.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  * 4. 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
61 /*
62  *      Virtual memory object module.
63  */
64
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
67
68 #include "opt_vm.h"
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/lock.h>
73 #include <sys/mman.h>
74 #include <sys/mount.h>
75 #include <sys/kernel.h>
76 #include <sys/sysctl.h>
77 #include <sys/mutex.h>
78 #include <sys/proc.h>           /* for curproc, pageproc */
79 #include <sys/socket.h>
80 #include <sys/resourcevar.h>
81 #include <sys/vnode.h>
82 #include <sys/vmmeter.h>
83 #include <sys/sx.h>
84
85 #include <vm/vm.h>
86 #include <vm/vm_param.h>
87 #include <vm/pmap.h>
88 #include <vm/vm_map.h>
89 #include <vm/vm_object.h>
90 #include <vm/vm_page.h>
91 #include <vm/vm_pageout.h>
92 #include <vm/vm_pager.h>
93 #include <vm/swap_pager.h>
94 #include <vm/vm_kern.h>
95 #include <vm/vm_extern.h>
96 #include <vm/vm_reserv.h>
97 #include <vm/uma.h>
98
99 static int old_msync;
100 SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0,
101     "Use old (insecure) msync behavior");
102
103 static int      vm_object_page_collect_flush(vm_object_t object, vm_page_t p,
104                     int pagerflags);
105 static void     vm_object_qcollapse(vm_object_t object);
106 static void     vm_object_vndeallocate(vm_object_t object);
107
108 /*
109  *      Virtual memory objects maintain the actual data
110  *      associated with allocated virtual memory.  A given
111  *      page of memory exists within exactly one object.
112  *
113  *      An object is only deallocated when all "references"
114  *      are given up.  Only one "reference" to a given
115  *      region of an object should be writeable.
116  *
117  *      Associated with each object is a list of all resident
118  *      memory pages belonging to that object; this list is
119  *      maintained by the "vm_page" module, and locked by the object's
120  *      lock.
121  *
122  *      Each object also records a "pager" routine which is
123  *      used to retrieve (and store) pages to the proper backing
124  *      storage.  In addition, objects may be backed by other
125  *      objects from which they were virtual-copied.
126  *
127  *      The only items within the object structure which are
128  *      modified after time of creation are:
129  *              reference count         locked by object's lock
130  *              pager routine           locked by object's lock
131  *
132  */
133
134 struct object_q vm_object_list;
135 struct mtx vm_object_list_mtx;  /* lock for object list and count */
136
137 struct vm_object kernel_object_store;
138 struct vm_object kmem_object_store;
139
140 SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0, "VM object stats");
141
142 static long object_collapses;
143 SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD,
144     &object_collapses, 0, "VM object collapses");
145
146 static long object_bypasses;
147 SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD,
148     &object_bypasses, 0, "VM object bypasses");
149
150 static uma_zone_t obj_zone;
151
152 static int vm_object_zinit(void *mem, int size, int flags);
153
154 #ifdef INVARIANTS
155 static void vm_object_zdtor(void *mem, int size, void *arg);
156
157 static void
158 vm_object_zdtor(void *mem, int size, void *arg)
159 {
160         vm_object_t object;
161
162         object = (vm_object_t)mem;
163         KASSERT(TAILQ_EMPTY(&object->memq),
164             ("object %p has resident pages",
165             object));
166 #if VM_NRESERVLEVEL > 0
167         KASSERT(LIST_EMPTY(&object->rvq),
168             ("object %p has reservations",
169             object));
170 #endif
171         KASSERT(object->cache == NULL,
172             ("object %p has cached pages",
173             object));
174         KASSERT(object->paging_in_progress == 0,
175             ("object %p paging_in_progress = %d",
176             object, object->paging_in_progress));
177         KASSERT(object->resident_page_count == 0,
178             ("object %p resident_page_count = %d",
179             object, object->resident_page_count));
180         KASSERT(object->shadow_count == 0,
181             ("object %p shadow_count = %d",
182             object, object->shadow_count));
183 }
184 #endif
185
186 static int
187 vm_object_zinit(void *mem, int size, int flags)
188 {
189         vm_object_t object;
190
191         object = (vm_object_t)mem;
192         bzero(&object->mtx, sizeof(object->mtx));
193         VM_OBJECT_LOCK_INIT(object, "standard object");
194
195         /* These are true for any object that has been freed */
196         object->paging_in_progress = 0;
197         object->resident_page_count = 0;
198         object->shadow_count = 0;
199         return (0);
200 }
201
202 void
203 _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
204 {
205
206         TAILQ_INIT(&object->memq);
207         LIST_INIT(&object->shadow_head);
208
209         object->root = NULL;
210         object->type = type;
211         object->size = size;
212         object->generation = 1;
213         object->ref_count = 1;
214         object->memattr = VM_MEMATTR_DEFAULT;
215         object->flags = 0;
216         object->uip = NULL;
217         object->charge = 0;
218         if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
219                 object->flags = OBJ_ONEMAPPING;
220         object->pg_color = 0;
221         object->handle = NULL;
222         object->backing_object = NULL;
223         object->backing_object_offset = (vm_ooffset_t) 0;
224 #if VM_NRESERVLEVEL > 0
225         LIST_INIT(&object->rvq);
226 #endif
227         object->cache = NULL;
228
229         mtx_lock(&vm_object_list_mtx);
230         TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
231         mtx_unlock(&vm_object_list_mtx);
232 }
233
234 /*
235  *      vm_object_init:
236  *
237  *      Initialize the VM objects module.
238  */
239 void
240 vm_object_init(void)
241 {
242         TAILQ_INIT(&vm_object_list);
243         mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
244         
245         VM_OBJECT_LOCK_INIT(&kernel_object_store, "kernel object");
246         _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
247             kernel_object);
248 #if VM_NRESERVLEVEL > 0
249         kernel_object->flags |= OBJ_COLORED;
250         kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
251 #endif
252
253         VM_OBJECT_LOCK_INIT(&kmem_object_store, "kmem object");
254         _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
255             kmem_object);
256 #if VM_NRESERVLEVEL > 0
257         kmem_object->flags |= OBJ_COLORED;
258         kmem_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
259 #endif
260
261         /*
262          * The lock portion of struct vm_object must be type stable due
263          * to vm_pageout_fallback_object_lock locking a vm object
264          * without holding any references to it.
265          */
266         obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
267 #ifdef INVARIANTS
268             vm_object_zdtor,
269 #else
270             NULL,
271 #endif
272             vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE);
273 }
274
275 void
276 vm_object_clear_flag(vm_object_t object, u_short bits)
277 {
278
279         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
280         object->flags &= ~bits;
281 }
282
283 /*
284  *      Sets the default memory attribute for the specified object.  Pages
285  *      that are allocated to this object are by default assigned this memory
286  *      attribute.
287  *
288  *      Presently, this function must be called before any pages are allocated
289  *      to the object.  In the future, this requirement may be relaxed for
290  *      "default" and "swap" objects.
291  */
292 int
293 vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr)
294 {
295
296         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
297         switch (object->type) {
298         case OBJT_DEFAULT:
299         case OBJT_DEVICE:
300         case OBJT_PHYS:
301         case OBJT_SG:
302         case OBJT_SWAP:
303         case OBJT_VNODE:
304                 if (!TAILQ_EMPTY(&object->memq))
305                         return (KERN_FAILURE);
306                 break;
307         case OBJT_DEAD:
308                 return (KERN_INVALID_ARGUMENT);
309         }
310         object->memattr = memattr;
311         return (KERN_SUCCESS);
312 }
313
314 void
315 vm_object_pip_add(vm_object_t object, short i)
316 {
317
318         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
319         object->paging_in_progress += i;
320 }
321
322 void
323 vm_object_pip_subtract(vm_object_t object, short i)
324 {
325
326         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
327         object->paging_in_progress -= i;
328 }
329
330 void
331 vm_object_pip_wakeup(vm_object_t object)
332 {
333
334         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
335         object->paging_in_progress--;
336         if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
337                 vm_object_clear_flag(object, OBJ_PIPWNT);
338                 wakeup(object);
339         }
340 }
341
342 void
343 vm_object_pip_wakeupn(vm_object_t object, short i)
344 {
345
346         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
347         if (i)
348                 object->paging_in_progress -= i;
349         if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
350                 vm_object_clear_flag(object, OBJ_PIPWNT);
351                 wakeup(object);
352         }
353 }
354
355 void
356 vm_object_pip_wait(vm_object_t object, char *waitid)
357 {
358
359         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
360         while (object->paging_in_progress) {
361                 object->flags |= OBJ_PIPWNT;
362                 msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0);
363         }
364 }
365
366 /*
367  *      vm_object_allocate:
368  *
369  *      Returns a new object with the given size.
370  */
371 vm_object_t
372 vm_object_allocate(objtype_t type, vm_pindex_t size)
373 {
374         vm_object_t object;
375
376         object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
377         _vm_object_allocate(type, size, object);
378         return (object);
379 }
380
381
382 /*
383  *      vm_object_reference:
384  *
385  *      Gets another reference to the given object.  Note: OBJ_DEAD
386  *      objects can be referenced during final cleaning.
387  */
388 void
389 vm_object_reference(vm_object_t object)
390 {
391         if (object == NULL)
392                 return;
393         VM_OBJECT_LOCK(object);
394         vm_object_reference_locked(object);
395         VM_OBJECT_UNLOCK(object);
396 }
397
398 /*
399  *      vm_object_reference_locked:
400  *
401  *      Gets another reference to the given object.
402  *
403  *      The object must be locked.
404  */
405 void
406 vm_object_reference_locked(vm_object_t object)
407 {
408         struct vnode *vp;
409
410         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
411         object->ref_count++;
412         if (object->type == OBJT_VNODE) {
413                 vp = object->handle;
414                 vref(vp);
415         }
416 }
417
418 /*
419  * Handle deallocating an object of type OBJT_VNODE.
420  */
421 static void
422 vm_object_vndeallocate(vm_object_t object)
423 {
424         struct vnode *vp = (struct vnode *) object->handle;
425
426         VFS_ASSERT_GIANT(vp->v_mount);
427         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
428         KASSERT(object->type == OBJT_VNODE,
429             ("vm_object_vndeallocate: not a vnode object"));
430         KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
431 #ifdef INVARIANTS
432         if (object->ref_count == 0) {
433                 vprint("vm_object_vndeallocate", vp);
434                 panic("vm_object_vndeallocate: bad object reference count");
435         }
436 #endif
437
438         object->ref_count--;
439         if (object->ref_count == 0) {
440                 mp_fixme("Unlocked vflag access.");
441                 vp->v_vflag &= ~VV_TEXT;
442         }
443         VM_OBJECT_UNLOCK(object);
444         /*
445          * vrele may need a vop lock
446          */
447         vrele(vp);
448 }
449
450 /*
451  *      vm_object_deallocate:
452  *
453  *      Release a reference to the specified object,
454  *      gained either through a vm_object_allocate
455  *      or a vm_object_reference call.  When all references
456  *      are gone, storage associated with this object
457  *      may be relinquished.
458  *
459  *      No object may be locked.
460  */
461 void
462 vm_object_deallocate(vm_object_t object)
463 {
464         vm_object_t temp;
465
466         while (object != NULL) {
467                 int vfslocked;
468
469                 vfslocked = 0;
470         restart:
471                 VM_OBJECT_LOCK(object);
472                 if (object->type == OBJT_VNODE) {
473                         struct vnode *vp = (struct vnode *) object->handle;
474
475                         /*
476                          * Conditionally acquire Giant for a vnode-backed
477                          * object.  We have to be careful since the type of
478                          * a vnode object can change while the object is
479                          * unlocked.
480                          */
481                         if (VFS_NEEDSGIANT(vp->v_mount) && !vfslocked) {
482                                 vfslocked = 1;
483                                 if (!mtx_trylock(&Giant)) {
484                                         VM_OBJECT_UNLOCK(object);
485                                         mtx_lock(&Giant);
486                                         goto restart;
487                                 }
488                         }
489                         vm_object_vndeallocate(object);
490                         VFS_UNLOCK_GIANT(vfslocked);
491                         return;
492                 } else
493                         /*
494                          * This is to handle the case that the object
495                          * changed type while we dropped its lock to
496                          * obtain Giant.
497                          */
498                         VFS_UNLOCK_GIANT(vfslocked);
499
500                 KASSERT(object->ref_count != 0,
501                         ("vm_object_deallocate: object deallocated too many times: %d", object->type));
502
503                 /*
504                  * If the reference count goes to 0 we start calling
505                  * vm_object_terminate() on the object chain.
506                  * A ref count of 1 may be a special case depending on the
507                  * shadow count being 0 or 1.
508                  */
509                 object->ref_count--;
510                 if (object->ref_count > 1) {
511                         VM_OBJECT_UNLOCK(object);
512                         return;
513                 } else if (object->ref_count == 1) {
514                         if (object->shadow_count == 0 &&
515                             object->handle == NULL &&
516                             (object->type == OBJT_DEFAULT ||
517                              object->type == OBJT_SWAP)) {
518                                 vm_object_set_flag(object, OBJ_ONEMAPPING);
519                         } else if ((object->shadow_count == 1) &&
520                             (object->handle == NULL) &&
521                             (object->type == OBJT_DEFAULT ||
522                              object->type == OBJT_SWAP)) {
523                                 vm_object_t robject;
524
525                                 robject = LIST_FIRST(&object->shadow_head);
526                                 KASSERT(robject != NULL,
527                                     ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
528                                          object->ref_count,
529                                          object->shadow_count));
530                                 if (!VM_OBJECT_TRYLOCK(robject)) {
531                                         /*
532                                          * Avoid a potential deadlock.
533                                          */
534                                         object->ref_count++;
535                                         VM_OBJECT_UNLOCK(object);
536                                         /*
537                                          * More likely than not the thread
538                                          * holding robject's lock has lower
539                                          * priority than the current thread.
540                                          * Let the lower priority thread run.
541                                          */
542                                         pause("vmo_de", 1);
543                                         continue;
544                                 }
545                                 /*
546                                  * Collapse object into its shadow unless its
547                                  * shadow is dead.  In that case, object will
548                                  * be deallocated by the thread that is
549                                  * deallocating its shadow.
550                                  */
551                                 if ((robject->flags & OBJ_DEAD) == 0 &&
552                                     (robject->handle == NULL) &&
553                                     (robject->type == OBJT_DEFAULT ||
554                                      robject->type == OBJT_SWAP)) {
555
556                                         robject->ref_count++;
557 retry:
558                                         if (robject->paging_in_progress) {
559                                                 VM_OBJECT_UNLOCK(object);
560                                                 vm_object_pip_wait(robject,
561                                                     "objde1");
562                                                 temp = robject->backing_object;
563                                                 if (object == temp) {
564                                                         VM_OBJECT_LOCK(object);
565                                                         goto retry;
566                                                 }
567                                         } else if (object->paging_in_progress) {
568                                                 VM_OBJECT_UNLOCK(robject);
569                                                 object->flags |= OBJ_PIPWNT;
570                                                 msleep(object,
571                                                     VM_OBJECT_MTX(object),
572                                                     PDROP | PVM, "objde2", 0);
573                                                 VM_OBJECT_LOCK(robject);
574                                                 temp = robject->backing_object;
575                                                 if (object == temp) {
576                                                         VM_OBJECT_LOCK(object);
577                                                         goto retry;
578                                                 }
579                                         } else
580                                                 VM_OBJECT_UNLOCK(object);
581
582                                         if (robject->ref_count == 1) {
583                                                 robject->ref_count--;
584                                                 object = robject;
585                                                 goto doterm;
586                                         }
587                                         object = robject;
588                                         vm_object_collapse(object);
589                                         VM_OBJECT_UNLOCK(object);
590                                         continue;
591                                 }
592                                 VM_OBJECT_UNLOCK(robject);
593                         }
594                         VM_OBJECT_UNLOCK(object);
595                         return;
596                 }
597 doterm:
598                 temp = object->backing_object;
599                 if (temp != NULL) {
600                         VM_OBJECT_LOCK(temp);
601                         LIST_REMOVE(object, shadow_list);
602                         temp->shadow_count--;
603                         VM_OBJECT_UNLOCK(temp);
604                         object->backing_object = NULL;
605                 }
606                 /*
607                  * Don't double-terminate, we could be in a termination
608                  * recursion due to the terminate having to sync data
609                  * to disk.
610                  */
611                 if ((object->flags & OBJ_DEAD) == 0)
612                         vm_object_terminate(object);
613                 else
614                         VM_OBJECT_UNLOCK(object);
615                 object = temp;
616         }
617 }
618
619 /*
620  *      vm_object_destroy removes the object from the global object list
621  *      and frees the space for the object.
622  */
623 void
624 vm_object_destroy(vm_object_t object)
625 {
626
627         /*
628          * Remove the object from the global object list.
629          */
630         mtx_lock(&vm_object_list_mtx);
631         TAILQ_REMOVE(&vm_object_list, object, object_list);
632         mtx_unlock(&vm_object_list_mtx);
633
634         /*
635          * Release the allocation charge.
636          */
637         if (object->uip != NULL) {
638                 KASSERT(object->type == OBJT_DEFAULT ||
639                     object->type == OBJT_SWAP,
640                     ("vm_object_terminate: non-swap obj %p has uip",
641                      object));
642                 swap_release_by_uid(object->charge, object->uip);
643                 object->charge = 0;
644                 uifree(object->uip);
645                 object->uip = NULL;
646         }
647
648         /*
649          * Free the space for the object.
650          */
651         uma_zfree(obj_zone, object);
652 }
653
654 /*
655  *      vm_object_terminate actually destroys the specified object, freeing
656  *      up all previously used resources.
657  *
658  *      The object must be locked.
659  *      This routine may block.
660  */
661 void
662 vm_object_terminate(vm_object_t object)
663 {
664         vm_page_t p;
665
666         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
667
668         /*
669          * Make sure no one uses us.
670          */
671         vm_object_set_flag(object, OBJ_DEAD);
672
673         /*
674          * wait for the pageout daemon to be done with the object
675          */
676         vm_object_pip_wait(object, "objtrm");
677
678         KASSERT(!object->paging_in_progress,
679                 ("vm_object_terminate: pageout in progress"));
680
681         /*
682          * Clean and free the pages, as appropriate. All references to the
683          * object are gone, so we don't need to lock it.
684          */
685         if (object->type == OBJT_VNODE) {
686                 struct vnode *vp = (struct vnode *)object->handle;
687
688                 /*
689                  * Clean pages and flush buffers.
690                  */
691                 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
692                 VM_OBJECT_UNLOCK(object);
693
694                 vinvalbuf(vp, V_SAVE, 0, 0);
695
696                 VM_OBJECT_LOCK(object);
697         }
698
699         KASSERT(object->ref_count == 0, 
700                 ("vm_object_terminate: object with references, ref_count=%d",
701                 object->ref_count));
702
703         /*
704          * Now free any remaining pages. For internal objects, this also
705          * removes them from paging queues. Don't free wired pages, just
706          * remove them from the object. 
707          */
708         while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
709                 KASSERT(!p->busy && (p->oflags & VPO_BUSY) == 0,
710                         ("vm_object_terminate: freeing busy page %p "
711                         "p->busy = %d, p->oflags %x\n", p, p->busy, p->oflags));
712                 vm_page_lock(p);
713                 if (p->wire_count == 0) {
714                         vm_page_free(p);
715                         PCPU_INC(cnt.v_pfree);
716                 } else
717                         vm_page_remove(p);
718                 vm_page_unlock(p);
719         }
720
721 #if VM_NRESERVLEVEL > 0
722         if (__predict_false(!LIST_EMPTY(&object->rvq)))
723                 vm_reserv_break_all(object);
724 #endif
725         if (__predict_false(object->cache != NULL))
726                 vm_page_cache_free(object, 0, 0);
727
728         /*
729          * Let the pager know object is dead.
730          */
731         vm_pager_deallocate(object);
732         VM_OBJECT_UNLOCK(object);
733
734         vm_object_destroy(object);
735 }
736
737 /*
738  *      vm_object_page_clean
739  *
740  *      Clean all dirty pages in the specified range of object.  Leaves page 
741  *      on whatever queue it is currently on.   If NOSYNC is set then do not
742  *      write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC),
743  *      leaving the object dirty.
744  *
745  *      When stuffing pages asynchronously, allow clustering.  XXX we need a
746  *      synchronous clustering mode implementation.
747  *
748  *      Odd semantics: if start == end, we clean everything.
749  *
750  *      The object must be locked.
751  */
752 void
753 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
754     int flags)
755 {
756         vm_page_t np, p;
757         vm_pindex_t pi, tend;
758         int clearobjflags, curgeneration, n, pagerflags;
759
760         mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED);
761         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
762         KASSERT(object->type == OBJT_VNODE, ("Not a vnode object"));
763         if ((object->flags & OBJ_MIGHTBEDIRTY) == 0 ||
764             object->resident_page_count == 0)
765                 return;
766
767         pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ?
768             VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
769         pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0;
770
771         tend = (end == 0) ? object->size : end;
772
773         vm_object_set_flag(object, OBJ_CLEANING);
774
775         /*
776          * Make the page read-only so we can then clear the object flags.
777          *
778          * However, if this is a nosync mmap then the object is likely to 
779          * stay dirty so do not mess with the page and do not clear the
780          * object flags.
781          */
782         clearobjflags = 1;
783         for (p = vm_page_find_least(object, start);
784             p != NULL && p->pindex < tend; p = TAILQ_NEXT(p, listq)) {
785                 if ((flags & OBJPC_NOSYNC) != 0 &&
786                     (p->oflags & VPO_NOSYNC) != 0)
787                         clearobjflags = 0;
788                 else
789                         pmap_remove_write(p);
790         }
791
792         if (clearobjflags && (start == 0) && (tend == object->size))
793                 vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY);
794
795 rescan:
796         curgeneration = object->generation;
797
798         for (p = vm_page_find_least(object, start); p != NULL; p = np) {
799                 pi = p->pindex;
800                 if (pi >= tend)
801                         break;
802                 np = TAILQ_NEXT(p, listq);
803                 if (p->valid == 0)
804                         continue;
805                 while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
806                         if (object->generation != curgeneration)
807                                 goto rescan;
808                 }
809                 vm_page_test_dirty(p);
810                 if (p->dirty == 0)
811                         continue;
812
813                 /*
814                  * If we have been asked to skip nosync pages and this is a
815                  * nosync page, skip it.  Note that the object flags were
816                  * not cleared in this case so we do not have to set them.
817                  */
818                 if ((flags & OBJPC_NOSYNC) != 0 &&
819                     (p->oflags & VPO_NOSYNC) != 0)
820                         continue;
821
822                 n = vm_object_page_collect_flush(object, p, pagerflags);
823                 KASSERT(n > 0, ("vm_object_page_collect_flush failed"));
824                 if (object->generation != curgeneration)
825                         goto rescan;
826                 np = vm_page_find_least(object, pi + n);
827         }
828 #if 0
829         VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0);
830 #endif
831
832         vm_object_clear_flag(object, OBJ_CLEANING);
833 }
834
835 static int
836 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags)
837 {
838         int runlen;
839         int maxf;
840         int chkb;
841         int maxb;
842         int i, index;
843         vm_pindex_t pi;
844         vm_page_t maf[vm_pageout_page_count];
845         vm_page_t mab[vm_pageout_page_count];
846         vm_page_t ma[vm_pageout_page_count];
847         vm_page_t tp, p1;
848
849         mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED);
850         vm_page_lock_assert(p, MA_NOTOWNED);
851         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
852         pi = p->pindex;
853         maxf = 0;
854         for (i = 1, p1 = p; i < vm_pageout_page_count; i++) {
855                 tp = vm_page_next(p1);
856                 if (tp == NULL || tp->busy != 0 || (tp->oflags & VPO_BUSY) != 0)
857                         break;
858                 vm_page_test_dirty(tp);
859                 if (tp->dirty == 0)
860                         break;
861                 maf[i - 1] = p1 = tp;
862                 maxf++;
863         }
864
865         maxb = 0;
866         chkb = vm_pageout_page_count -  maxf;
867         for (i = 1, p1 = p; i < chkb; i++) {
868                 tp = vm_page_prev(p1);
869                 if (tp == NULL || tp->busy != 0 || (tp->oflags & VPO_BUSY) != 0)
870                         break;
871                 vm_page_test_dirty(tp);
872                 if (tp->dirty == 0)
873                         break;
874                 mab[i - 1] = p1 = tp;
875                 maxb++;
876         }
877
878         for (i = 0; i < maxb; i++) {
879                 index = (maxb - i) - 1;
880                 ma[index] = mab[i];
881         }
882         ma[maxb] = p;
883         for (i = 0; i < maxf; i++) {
884                 index = (maxb + i) + 1;
885                 ma[index] = maf[i];
886         }
887
888         vm_pageout_flush(ma, maxb + maxf + 1, pagerflags, maxb + 1, &runlen);
889         return (runlen);
890 }
891
892 /*
893  * Note that there is absolutely no sense in writing out
894  * anonymous objects, so we track down the vnode object
895  * to write out.
896  * We invalidate (remove) all pages from the address space
897  * for semantic correctness.
898  *
899  * Note: certain anonymous maps, such as MAP_NOSYNC maps,
900  * may start out with a NULL object.
901  */
902 void
903 vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
904     boolean_t syncio, boolean_t invalidate)
905 {
906         vm_object_t backing_object;
907         struct vnode *vp;
908         struct mount *mp;
909         int flags;
910
911         if (object == NULL)
912                 return;
913         VM_OBJECT_LOCK(object);
914         while ((backing_object = object->backing_object) != NULL) {
915                 VM_OBJECT_LOCK(backing_object);
916                 offset += object->backing_object_offset;
917                 VM_OBJECT_UNLOCK(object);
918                 object = backing_object;
919                 if (object->size < OFF_TO_IDX(offset + size))
920                         size = IDX_TO_OFF(object->size) - offset;
921         }
922         /*
923          * Flush pages if writing is allowed, invalidate them
924          * if invalidation requested.  Pages undergoing I/O
925          * will be ignored by vm_object_page_remove().
926          *
927          * We cannot lock the vnode and then wait for paging
928          * to complete without deadlocking against vm_fault.
929          * Instead we simply call vm_object_page_remove() and
930          * allow it to block internally on a page-by-page
931          * basis when it encounters pages undergoing async
932          * I/O.
933          */
934         if (object->type == OBJT_VNODE &&
935             (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
936                 int vfslocked;
937                 vp = object->handle;
938                 VM_OBJECT_UNLOCK(object);
939                 (void) vn_start_write(vp, &mp, V_WAIT);
940                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
941                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
942                 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
943                 flags |= invalidate ? OBJPC_INVAL : 0;
944                 VM_OBJECT_LOCK(object);
945                 vm_object_page_clean(object,
946                     OFF_TO_IDX(offset),
947                     OFF_TO_IDX(offset + size + PAGE_MASK),
948                     flags);
949                 VM_OBJECT_UNLOCK(object);
950                 VOP_UNLOCK(vp, 0);
951                 VFS_UNLOCK_GIANT(vfslocked);
952                 vn_finished_write(mp);
953                 VM_OBJECT_LOCK(object);
954         }
955         if ((object->type == OBJT_VNODE ||
956              object->type == OBJT_DEVICE) && invalidate) {
957                 boolean_t purge;
958                 purge = old_msync || (object->type == OBJT_DEVICE);
959                 vm_object_page_remove(object,
960                     OFF_TO_IDX(offset),
961                     OFF_TO_IDX(offset + size + PAGE_MASK),
962                     purge ? FALSE : TRUE);
963         }
964         VM_OBJECT_UNLOCK(object);
965 }
966
967 /*
968  *      vm_object_madvise:
969  *
970  *      Implements the madvise function at the object/page level.
971  *
972  *      MADV_WILLNEED   (any object)
973  *
974  *          Activate the specified pages if they are resident.
975  *
976  *      MADV_DONTNEED   (any object)
977  *
978  *          Deactivate the specified pages if they are resident.
979  *
980  *      MADV_FREE       (OBJT_DEFAULT/OBJT_SWAP objects,
981  *                       OBJ_ONEMAPPING only)
982  *
983  *          Deactivate and clean the specified pages if they are
984  *          resident.  This permits the process to reuse the pages
985  *          without faulting or the kernel to reclaim the pages
986  *          without I/O.
987  */
988 void
989 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
990 {
991         vm_pindex_t end, tpindex;
992         vm_object_t backing_object, tobject;
993         vm_page_t m;
994
995         if (object == NULL)
996                 return;
997         VM_OBJECT_LOCK(object);
998         end = pindex + count;
999         /*
1000          * Locate and adjust resident pages
1001          */
1002         for (; pindex < end; pindex += 1) {
1003 relookup:
1004                 tobject = object;
1005                 tpindex = pindex;
1006 shadowlookup:
1007                 /*
1008                  * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1009                  * and those pages must be OBJ_ONEMAPPING.
1010                  */
1011                 if (advise == MADV_FREE) {
1012                         if ((tobject->type != OBJT_DEFAULT &&
1013                              tobject->type != OBJT_SWAP) ||
1014                             (tobject->flags & OBJ_ONEMAPPING) == 0) {
1015                                 goto unlock_tobject;
1016                         }
1017                 } else if (tobject->type == OBJT_PHYS)
1018                         goto unlock_tobject;
1019                 m = vm_page_lookup(tobject, tpindex);
1020                 if (m == NULL && advise == MADV_WILLNEED) {
1021                         /*
1022                          * If the page is cached, reactivate it.
1023                          */
1024                         m = vm_page_alloc(tobject, tpindex, VM_ALLOC_IFCACHED |
1025                             VM_ALLOC_NOBUSY);
1026                 }
1027                 if (m == NULL) {
1028                         /*
1029                          * There may be swap even if there is no backing page
1030                          */
1031                         if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1032                                 swap_pager_freespace(tobject, tpindex, 1);
1033                         /*
1034                          * next object
1035                          */
1036                         backing_object = tobject->backing_object;
1037                         if (backing_object == NULL)
1038                                 goto unlock_tobject;
1039                         VM_OBJECT_LOCK(backing_object);
1040                         tpindex += OFF_TO_IDX(tobject->backing_object_offset);
1041                         if (tobject != object)
1042                                 VM_OBJECT_UNLOCK(tobject);
1043                         tobject = backing_object;
1044                         goto shadowlookup;
1045                 } else if (m->valid != VM_PAGE_BITS_ALL)
1046                         goto unlock_tobject;
1047                 /*
1048                  * If the page is not in a normal state, skip it.
1049                  */
1050                 vm_page_lock(m);
1051                 if (m->hold_count != 0 || m->wire_count != 0) {
1052                         vm_page_unlock(m);
1053                         goto unlock_tobject;
1054                 }
1055                 KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
1056                     ("vm_object_madvise: page %p is not managed", m));
1057                 if ((m->oflags & VPO_BUSY) || m->busy) {
1058                         if (advise == MADV_WILLNEED) {
1059                                 /*
1060                                  * Reference the page before unlocking and
1061                                  * sleeping so that the page daemon is less
1062                                  * likely to reclaim it. 
1063                                  */
1064                                 vm_page_lock_queues();
1065                                 vm_page_flag_set(m, PG_REFERENCED);
1066                                 vm_page_unlock_queues();
1067                         }
1068                         vm_page_unlock(m);
1069                         if (object != tobject)
1070                                 VM_OBJECT_UNLOCK(object);
1071                         m->oflags |= VPO_WANTED;
1072                         msleep(m, VM_OBJECT_MTX(tobject), PDROP | PVM, "madvpo",
1073                             0);
1074                         VM_OBJECT_LOCK(object);
1075                         goto relookup;
1076                 }
1077                 if (advise == MADV_WILLNEED) {
1078                         vm_page_activate(m);
1079                 } else if (advise == MADV_DONTNEED) {
1080                         vm_page_dontneed(m);
1081                 } else if (advise == MADV_FREE) {
1082                         /*
1083                          * Mark the page clean.  This will allow the page
1084                          * to be freed up by the system.  However, such pages
1085                          * are often reused quickly by malloc()/free()
1086                          * so we do not do anything that would cause
1087                          * a page fault if we can help it.
1088                          *
1089                          * Specifically, we do not try to actually free
1090                          * the page now nor do we try to put it in the
1091                          * cache (which would cause a page fault on reuse).
1092                          *
1093                          * But we do make the page is freeable as we
1094                          * can without actually taking the step of unmapping
1095                          * it.
1096                          */
1097                         pmap_clear_modify(m);
1098                         m->dirty = 0;
1099                         m->act_count = 0;
1100                         vm_page_dontneed(m);
1101                 }
1102                 vm_page_unlock(m);
1103                 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1104                         swap_pager_freespace(tobject, tpindex, 1);
1105 unlock_tobject:
1106                 if (tobject != object)
1107                         VM_OBJECT_UNLOCK(tobject);
1108         }       
1109         VM_OBJECT_UNLOCK(object);
1110 }
1111
1112 /*
1113  *      vm_object_shadow:
1114  *
1115  *      Create a new object which is backed by the
1116  *      specified existing object range.  The source
1117  *      object reference is deallocated.
1118  *
1119  *      The new object and offset into that object
1120  *      are returned in the source parameters.
1121  */
1122 void
1123 vm_object_shadow(
1124         vm_object_t *object,    /* IN/OUT */
1125         vm_ooffset_t *offset,   /* IN/OUT */
1126         vm_size_t length)
1127 {
1128         vm_object_t source;
1129         vm_object_t result;
1130
1131         source = *object;
1132
1133         /*
1134          * Don't create the new object if the old object isn't shared.
1135          */
1136         if (source != NULL) {
1137                 VM_OBJECT_LOCK(source);
1138                 if (source->ref_count == 1 &&
1139                     source->handle == NULL &&
1140                     (source->type == OBJT_DEFAULT ||
1141                      source->type == OBJT_SWAP)) {
1142                         VM_OBJECT_UNLOCK(source);
1143                         return;
1144                 }
1145                 VM_OBJECT_UNLOCK(source);
1146         }
1147
1148         /*
1149          * Allocate a new object with the given length.
1150          */
1151         result = vm_object_allocate(OBJT_DEFAULT, length);
1152
1153         /*
1154          * The new object shadows the source object, adding a reference to it.
1155          * Our caller changes his reference to point to the new object,
1156          * removing a reference to the source object.  Net result: no change
1157          * of reference count.
1158          *
1159          * Try to optimize the result object's page color when shadowing
1160          * in order to maintain page coloring consistency in the combined 
1161          * shadowed object.
1162          */
1163         result->backing_object = source;
1164         /*
1165          * Store the offset into the source object, and fix up the offset into
1166          * the new object.
1167          */
1168         result->backing_object_offset = *offset;
1169         if (source != NULL) {
1170                 VM_OBJECT_LOCK(source);
1171                 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1172                 source->shadow_count++;
1173 #if VM_NRESERVLEVEL > 0
1174                 result->flags |= source->flags & OBJ_COLORED;
1175                 result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) &
1176                     ((1 << (VM_NFREEORDER - 1)) - 1);
1177 #endif
1178                 VM_OBJECT_UNLOCK(source);
1179         }
1180
1181
1182         /*
1183          * Return the new things
1184          */
1185         *offset = 0;
1186         *object = result;
1187 }
1188
1189 /*
1190  *      vm_object_split:
1191  *
1192  * Split the pages in a map entry into a new object.  This affords
1193  * easier removal of unused pages, and keeps object inheritance from
1194  * being a negative impact on memory usage.
1195  */
1196 void
1197 vm_object_split(vm_map_entry_t entry)
1198 {
1199         vm_page_t m, m_next;
1200         vm_object_t orig_object, new_object, source;
1201         vm_pindex_t idx, offidxstart;
1202         vm_size_t size;
1203
1204         orig_object = entry->object.vm_object;
1205         if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1206                 return;
1207         if (orig_object->ref_count <= 1)
1208                 return;
1209         VM_OBJECT_UNLOCK(orig_object);
1210
1211         offidxstart = OFF_TO_IDX(entry->offset);
1212         size = atop(entry->end - entry->start);
1213
1214         /*
1215          * If swap_pager_copy() is later called, it will convert new_object
1216          * into a swap object.
1217          */
1218         new_object = vm_object_allocate(OBJT_DEFAULT, size);
1219
1220         /*
1221          * At this point, the new object is still private, so the order in
1222          * which the original and new objects are locked does not matter.
1223          */
1224         VM_OBJECT_LOCK(new_object);
1225         VM_OBJECT_LOCK(orig_object);
1226         source = orig_object->backing_object;
1227         if (source != NULL) {
1228                 VM_OBJECT_LOCK(source);
1229                 if ((source->flags & OBJ_DEAD) != 0) {
1230                         VM_OBJECT_UNLOCK(source);
1231                         VM_OBJECT_UNLOCK(orig_object);
1232                         VM_OBJECT_UNLOCK(new_object);
1233                         vm_object_deallocate(new_object);
1234                         VM_OBJECT_LOCK(orig_object);
1235                         return;
1236                 }
1237                 LIST_INSERT_HEAD(&source->shadow_head,
1238                                   new_object, shadow_list);
1239                 source->shadow_count++;
1240                 vm_object_reference_locked(source);     /* for new_object */
1241                 vm_object_clear_flag(source, OBJ_ONEMAPPING);
1242                 VM_OBJECT_UNLOCK(source);
1243                 new_object->backing_object_offset = 
1244                         orig_object->backing_object_offset + entry->offset;
1245                 new_object->backing_object = source;
1246         }
1247         if (orig_object->uip != NULL) {
1248                 new_object->uip = orig_object->uip;
1249                 uihold(orig_object->uip);
1250                 new_object->charge = ptoa(size);
1251                 KASSERT(orig_object->charge >= ptoa(size),
1252                     ("orig_object->charge < 0"));
1253                 orig_object->charge -= ptoa(size);
1254         }
1255 retry:
1256         m = vm_page_find_least(orig_object, offidxstart);
1257         for (; m != NULL && (idx = m->pindex - offidxstart) < size;
1258             m = m_next) {
1259                 m_next = TAILQ_NEXT(m, listq);
1260
1261                 /*
1262                  * We must wait for pending I/O to complete before we can
1263                  * rename the page.
1264                  *
1265                  * We do not have to VM_PROT_NONE the page as mappings should
1266                  * not be changed by this operation.
1267                  */
1268                 if ((m->oflags & VPO_BUSY) || m->busy) {
1269                         VM_OBJECT_UNLOCK(new_object);
1270                         m->oflags |= VPO_WANTED;
1271                         msleep(m, VM_OBJECT_MTX(orig_object), PVM, "spltwt", 0);
1272                         VM_OBJECT_LOCK(new_object);
1273                         goto retry;
1274                 }
1275                 vm_page_lock(m);
1276                 vm_page_rename(m, new_object, idx);
1277                 vm_page_unlock(m);
1278                 /* page automatically made dirty by rename and cache handled */
1279                 vm_page_busy(m);
1280         }
1281         if (orig_object->type == OBJT_SWAP) {
1282                 /*
1283                  * swap_pager_copy() can sleep, in which case the orig_object's
1284                  * and new_object's locks are released and reacquired. 
1285                  */
1286                 swap_pager_copy(orig_object, new_object, offidxstart, 0);
1287
1288                 /*
1289                  * Transfer any cached pages from orig_object to new_object.
1290                  */
1291                 if (__predict_false(orig_object->cache != NULL))
1292                         vm_page_cache_transfer(orig_object, offidxstart,
1293                             new_object);
1294         }
1295         VM_OBJECT_UNLOCK(orig_object);
1296         TAILQ_FOREACH(m, &new_object->memq, listq)
1297                 vm_page_wakeup(m);
1298         VM_OBJECT_UNLOCK(new_object);
1299         entry->object.vm_object = new_object;
1300         entry->offset = 0LL;
1301         vm_object_deallocate(orig_object);
1302         VM_OBJECT_LOCK(new_object);
1303 }
1304
1305 #define OBSC_TEST_ALL_SHADOWED  0x0001
1306 #define OBSC_COLLAPSE_NOWAIT    0x0002
1307 #define OBSC_COLLAPSE_WAIT      0x0004
1308
1309 static int
1310 vm_object_backing_scan(vm_object_t object, int op)
1311 {
1312         int r = 1;
1313         vm_page_t p;
1314         vm_object_t backing_object;
1315         vm_pindex_t backing_offset_index;
1316
1317         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1318         VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED);
1319
1320         backing_object = object->backing_object;
1321         backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1322
1323         /*
1324          * Initial conditions
1325          */
1326         if (op & OBSC_TEST_ALL_SHADOWED) {
1327                 /*
1328                  * We do not want to have to test for the existence of cache
1329                  * or swap pages in the backing object.  XXX but with the
1330                  * new swapper this would be pretty easy to do.
1331                  *
1332                  * XXX what about anonymous MAP_SHARED memory that hasn't
1333                  * been ZFOD faulted yet?  If we do not test for this, the
1334                  * shadow test may succeed! XXX
1335                  */
1336                 if (backing_object->type != OBJT_DEFAULT) {
1337                         return (0);
1338                 }
1339         }
1340         if (op & OBSC_COLLAPSE_WAIT) {
1341                 vm_object_set_flag(backing_object, OBJ_DEAD);
1342         }
1343
1344         /*
1345          * Our scan
1346          */
1347         p = TAILQ_FIRST(&backing_object->memq);
1348         while (p) {
1349                 vm_page_t next = TAILQ_NEXT(p, listq);
1350                 vm_pindex_t new_pindex = p->pindex - backing_offset_index;
1351
1352                 if (op & OBSC_TEST_ALL_SHADOWED) {
1353                         vm_page_t pp;
1354
1355                         /*
1356                          * Ignore pages outside the parent object's range
1357                          * and outside the parent object's mapping of the 
1358                          * backing object.
1359                          *
1360                          * note that we do not busy the backing object's
1361                          * page.
1362                          */
1363                         if (
1364                             p->pindex < backing_offset_index ||
1365                             new_pindex >= object->size
1366                         ) {
1367                                 p = next;
1368                                 continue;
1369                         }
1370
1371                         /*
1372                          * See if the parent has the page or if the parent's
1373                          * object pager has the page.  If the parent has the
1374                          * page but the page is not valid, the parent's
1375                          * object pager must have the page.
1376                          *
1377                          * If this fails, the parent does not completely shadow
1378                          * the object and we might as well give up now.
1379                          */
1380
1381                         pp = vm_page_lookup(object, new_pindex);
1382                         if (
1383                             (pp == NULL || pp->valid == 0) &&
1384                             !vm_pager_has_page(object, new_pindex, NULL, NULL)
1385                         ) {
1386                                 r = 0;
1387                                 break;
1388                         }
1389                 }
1390
1391                 /*
1392                  * Check for busy page
1393                  */
1394                 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1395                         vm_page_t pp;
1396
1397                         if (op & OBSC_COLLAPSE_NOWAIT) {
1398                                 if ((p->oflags & VPO_BUSY) ||
1399                                     !p->valid || 
1400                                     p->busy) {
1401                                         p = next;
1402                                         continue;
1403                                 }
1404                         } else if (op & OBSC_COLLAPSE_WAIT) {
1405                                 if ((p->oflags & VPO_BUSY) || p->busy) {
1406                                         VM_OBJECT_UNLOCK(object);
1407                                         p->oflags |= VPO_WANTED;
1408                                         msleep(p, VM_OBJECT_MTX(backing_object),
1409                                             PDROP | PVM, "vmocol", 0);
1410                                         VM_OBJECT_LOCK(object);
1411                                         VM_OBJECT_LOCK(backing_object);
1412                                         /*
1413                                          * If we slept, anything could have
1414                                          * happened.  Since the object is
1415                                          * marked dead, the backing offset
1416                                          * should not have changed so we
1417                                          * just restart our scan.
1418                                          */
1419                                         p = TAILQ_FIRST(&backing_object->memq);
1420                                         continue;
1421                                 }
1422                         }
1423
1424                         KASSERT(
1425                             p->object == backing_object,
1426                             ("vm_object_backing_scan: object mismatch")
1427                         );
1428
1429                         /*
1430                          * Destroy any associated swap
1431                          */
1432                         if (backing_object->type == OBJT_SWAP) {
1433                                 swap_pager_freespace(
1434                                     backing_object, 
1435                                     p->pindex,
1436                                     1
1437                                 );
1438                         }
1439
1440                         if (
1441                             p->pindex < backing_offset_index ||
1442                             new_pindex >= object->size
1443                         ) {
1444                                 /*
1445                                  * Page is out of the parent object's range, we 
1446                                  * can simply destroy it. 
1447                                  */
1448                                 vm_page_lock(p);
1449                                 KASSERT(!pmap_page_is_mapped(p),
1450                                     ("freeing mapped page %p", p));
1451                                 if (p->wire_count == 0)
1452                                         vm_page_free(p);
1453                                 else
1454                                         vm_page_remove(p);
1455                                 vm_page_unlock(p);
1456                                 p = next;
1457                                 continue;
1458                         }
1459
1460                         pp = vm_page_lookup(object, new_pindex);
1461                         if (
1462                             pp != NULL ||
1463                             vm_pager_has_page(object, new_pindex, NULL, NULL)
1464                         ) {
1465                                 /*
1466                                  * page already exists in parent OR swap exists
1467                                  * for this location in the parent.  Destroy 
1468                                  * the original page from the backing object.
1469                                  *
1470                                  * Leave the parent's page alone
1471                                  */
1472                                 vm_page_lock(p);
1473                                 KASSERT(!pmap_page_is_mapped(p),
1474                                     ("freeing mapped page %p", p));
1475                                 if (p->wire_count == 0)
1476                                         vm_page_free(p);
1477                                 else
1478                                         vm_page_remove(p);
1479                                 vm_page_unlock(p);
1480                                 p = next;
1481                                 continue;
1482                         }
1483
1484 #if VM_NRESERVLEVEL > 0
1485                         /*
1486                          * Rename the reservation.
1487                          */
1488                         vm_reserv_rename(p, object, backing_object,
1489                             backing_offset_index);
1490 #endif
1491
1492                         /*
1493                          * Page does not exist in parent, rename the
1494                          * page from the backing object to the main object. 
1495                          *
1496                          * If the page was mapped to a process, it can remain 
1497                          * mapped through the rename.
1498                          */
1499                         vm_page_lock(p);
1500                         vm_page_rename(p, object, new_pindex);
1501                         vm_page_unlock(p);
1502                         /* page automatically made dirty by rename */
1503                 }
1504                 p = next;
1505         }
1506         return (r);
1507 }
1508
1509
1510 /*
1511  * this version of collapse allows the operation to occur earlier and
1512  * when paging_in_progress is true for an object...  This is not a complete
1513  * operation, but should plug 99.9% of the rest of the leaks.
1514  */
1515 static void
1516 vm_object_qcollapse(vm_object_t object)
1517 {
1518         vm_object_t backing_object = object->backing_object;
1519
1520         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1521         VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED);
1522
1523         if (backing_object->ref_count != 1)
1524                 return;
1525
1526         vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1527 }
1528
1529 /*
1530  *      vm_object_collapse:
1531  *
1532  *      Collapse an object with the object backing it.
1533  *      Pages in the backing object are moved into the
1534  *      parent, and the backing object is deallocated.
1535  */
1536 void
1537 vm_object_collapse(vm_object_t object)
1538 {
1539         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1540         
1541         while (TRUE) {
1542                 vm_object_t backing_object;
1543
1544                 /*
1545                  * Verify that the conditions are right for collapse:
1546                  *
1547                  * The object exists and the backing object exists.
1548                  */
1549                 if ((backing_object = object->backing_object) == NULL)
1550                         break;
1551
1552                 /*
1553                  * we check the backing object first, because it is most likely
1554                  * not collapsable.
1555                  */
1556                 VM_OBJECT_LOCK(backing_object);
1557                 if (backing_object->handle != NULL ||
1558                     (backing_object->type != OBJT_DEFAULT &&
1559                      backing_object->type != OBJT_SWAP) ||
1560                     (backing_object->flags & OBJ_DEAD) ||
1561                     object->handle != NULL ||
1562                     (object->type != OBJT_DEFAULT &&
1563                      object->type != OBJT_SWAP) ||
1564                     (object->flags & OBJ_DEAD)) {
1565                         VM_OBJECT_UNLOCK(backing_object);
1566                         break;
1567                 }
1568
1569                 if (
1570                     object->paging_in_progress != 0 ||
1571                     backing_object->paging_in_progress != 0
1572                 ) {
1573                         vm_object_qcollapse(object);
1574                         VM_OBJECT_UNLOCK(backing_object);
1575                         break;
1576                 }
1577                 /*
1578                  * We know that we can either collapse the backing object (if
1579                  * the parent is the only reference to it) or (perhaps) have
1580                  * the parent bypass the object if the parent happens to shadow
1581                  * all the resident pages in the entire backing object.
1582                  *
1583                  * This is ignoring pager-backed pages such as swap pages.
1584                  * vm_object_backing_scan fails the shadowing test in this
1585                  * case.
1586                  */
1587                 if (backing_object->ref_count == 1) {
1588                         /*
1589                          * If there is exactly one reference to the backing
1590                          * object, we can collapse it into the parent.  
1591                          */
1592                         vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1593
1594 #if VM_NRESERVLEVEL > 0
1595                         /*
1596                          * Break any reservations from backing_object.
1597                          */
1598                         if (__predict_false(!LIST_EMPTY(&backing_object->rvq)))
1599                                 vm_reserv_break_all(backing_object);
1600 #endif
1601
1602                         /*
1603                          * Move the pager from backing_object to object.
1604                          */
1605                         if (backing_object->type == OBJT_SWAP) {
1606                                 /*
1607                                  * swap_pager_copy() can sleep, in which case
1608                                  * the backing_object's and object's locks are
1609                                  * released and reacquired.
1610                                  */
1611                                 swap_pager_copy(
1612                                     backing_object,
1613                                     object,
1614                                     OFF_TO_IDX(object->backing_object_offset), TRUE);
1615
1616                                 /*
1617                                  * Free any cached pages from backing_object.
1618                                  */
1619                                 if (__predict_false(backing_object->cache != NULL))
1620                                         vm_page_cache_free(backing_object, 0, 0);
1621                         }
1622                         /*
1623                          * Object now shadows whatever backing_object did.
1624                          * Note that the reference to 
1625                          * backing_object->backing_object moves from within 
1626                          * backing_object to within object.
1627                          */
1628                         LIST_REMOVE(object, shadow_list);
1629                         backing_object->shadow_count--;
1630                         if (backing_object->backing_object) {
1631                                 VM_OBJECT_LOCK(backing_object->backing_object);
1632                                 LIST_REMOVE(backing_object, shadow_list);
1633                                 LIST_INSERT_HEAD(
1634                                     &backing_object->backing_object->shadow_head,
1635                                     object, shadow_list);
1636                                 /*
1637                                  * The shadow_count has not changed.
1638                                  */
1639                                 VM_OBJECT_UNLOCK(backing_object->backing_object);
1640                         }
1641                         object->backing_object = backing_object->backing_object;
1642                         object->backing_object_offset +=
1643                             backing_object->backing_object_offset;
1644
1645                         /*
1646                          * Discard backing_object.
1647                          *
1648                          * Since the backing object has no pages, no pager left,
1649                          * and no object references within it, all that is
1650                          * necessary is to dispose of it.
1651                          */
1652                         KASSERT(backing_object->ref_count == 1, (
1653 "backing_object %p was somehow re-referenced during collapse!",
1654                             backing_object));
1655                         VM_OBJECT_UNLOCK(backing_object);
1656                         vm_object_destroy(backing_object);
1657
1658                         object_collapses++;
1659                 } else {
1660                         vm_object_t new_backing_object;
1661
1662                         /*
1663                          * If we do not entirely shadow the backing object,
1664                          * there is nothing we can do so we give up.
1665                          */
1666                         if (object->resident_page_count != object->size &&
1667                             vm_object_backing_scan(object,
1668                             OBSC_TEST_ALL_SHADOWED) == 0) {
1669                                 VM_OBJECT_UNLOCK(backing_object);
1670                                 break;
1671                         }
1672
1673                         /*
1674                          * Make the parent shadow the next object in the
1675                          * chain.  Deallocating backing_object will not remove
1676                          * it, since its reference count is at least 2.
1677                          */
1678                         LIST_REMOVE(object, shadow_list);
1679                         backing_object->shadow_count--;
1680
1681                         new_backing_object = backing_object->backing_object;
1682                         if ((object->backing_object = new_backing_object) != NULL) {
1683                                 VM_OBJECT_LOCK(new_backing_object);
1684                                 LIST_INSERT_HEAD(
1685                                     &new_backing_object->shadow_head,
1686                                     object,
1687                                     shadow_list
1688                                 );
1689                                 new_backing_object->shadow_count++;
1690                                 vm_object_reference_locked(new_backing_object);
1691                                 VM_OBJECT_UNLOCK(new_backing_object);
1692                                 object->backing_object_offset +=
1693                                         backing_object->backing_object_offset;
1694                         }
1695
1696                         /*
1697                          * Drop the reference count on backing_object. Since
1698                          * its ref_count was at least 2, it will not vanish.
1699                          */
1700                         backing_object->ref_count--;
1701                         VM_OBJECT_UNLOCK(backing_object);
1702                         object_bypasses++;
1703                 }
1704
1705                 /*
1706                  * Try again with this object's new backing object.
1707                  */
1708         }
1709 }
1710
1711 /*
1712  *      vm_object_page_remove:
1713  *
1714  *      For the given object, either frees or invalidates each of the
1715  *      specified pages.  In general, a page is freed.  However, if a
1716  *      page is wired for any reason other than the existence of a
1717  *      managed, wired mapping, then it may be invalidated but not
1718  *      removed from the object.  Pages are specified by the given
1719  *      range ["start", "end") and Boolean "clean_only".  As a
1720  *      special case, if "end" is zero, then the range extends from
1721  *      "start" to the end of the object.  If "clean_only" is TRUE,
1722  *      then only the non-dirty pages within the specified range are
1723  *      affected.
1724  *
1725  *      In general, this operation should only be performed on objects
1726  *      that contain managed pages.  There are two exceptions.  First,
1727  *      it may be performed on the kernel and kmem objects.  Second,
1728  *      it may be used by msync(..., MS_INVALIDATE) to invalidate
1729  *      device-backed pages.  In both of these cases, "clean_only"
1730  *      must be FALSE.
1731  *
1732  *      The object must be locked.
1733  */
1734 void
1735 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1736     boolean_t clean_only)
1737 {
1738         vm_page_t p, next;
1739         int wirings;
1740
1741         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1742         if (object->resident_page_count == 0)
1743                 goto skipmemq;
1744
1745         /*
1746          * Since physically-backed objects do not use managed pages, we can't
1747          * remove pages from the object (we must instead remove the page
1748          * references, and then destroy the object).
1749          */
1750         KASSERT(object->type != OBJT_PHYS || object == kernel_object ||
1751             object == kmem_object,
1752             ("attempt to remove pages from a physical object"));
1753
1754         vm_object_pip_add(object, 1);
1755 again:
1756         p = vm_page_find_least(object, start);
1757
1758         /*
1759          * Assert: the variable p is either (1) the page with the
1760          * least pindex greater than or equal to the parameter pindex
1761          * or (2) NULL.
1762          */
1763         for (;
1764              p != NULL && (p->pindex < end || end == 0);
1765              p = next) {
1766                 next = TAILQ_NEXT(p, listq);
1767
1768                 /*
1769                  * If the page is wired for any reason besides the
1770                  * existence of managed, wired mappings, then it cannot
1771                  * be freed.  For example, fictitious pages, which
1772                  * represent device memory, are inherently wired and
1773                  * cannot be freed.  They can, however, be invalidated
1774                  * if "clean_only" is FALSE.
1775                  */
1776                 vm_page_lock(p);
1777                 if ((wirings = p->wire_count) != 0 &&
1778                     (wirings = pmap_page_wired_mappings(p)) != p->wire_count) {
1779                         /* Fictitious pages do not have managed mappings. */
1780                         if ((p->flags & PG_FICTITIOUS) == 0)
1781                                 pmap_remove_all(p);
1782                         /* Account for removal of managed, wired mappings. */
1783                         p->wire_count -= wirings;
1784                         if (!clean_only) {
1785                                 p->valid = 0;
1786                                 vm_page_undirty(p);
1787                         }
1788                         vm_page_unlock(p);
1789                         continue;
1790                 }
1791                 if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
1792                         goto again;
1793                 KASSERT((p->flags & PG_FICTITIOUS) == 0,
1794                     ("vm_object_page_remove: page %p is fictitious", p));
1795                 if (clean_only && p->valid) {
1796                         pmap_remove_write(p);
1797                         if (p->dirty) {
1798                                 vm_page_unlock(p);
1799                                 continue;
1800                         }
1801                 }
1802                 pmap_remove_all(p);
1803                 /* Account for removal of managed, wired mappings. */
1804                 if (wirings != 0)
1805                         p->wire_count -= wirings;
1806                 vm_page_free(p);
1807                 vm_page_unlock(p);
1808         }
1809         vm_object_pip_wakeup(object);
1810 skipmemq:
1811         if (__predict_false(object->cache != NULL))
1812                 vm_page_cache_free(object, start, end);
1813 }
1814
1815 /*
1816  *      Populate the specified range of the object with valid pages.  Returns
1817  *      TRUE if the range is successfully populated and FALSE otherwise.
1818  *
1819  *      Note: This function should be optimized to pass a larger array of
1820  *      pages to vm_pager_get_pages() before it is applied to a non-
1821  *      OBJT_DEVICE object.
1822  *
1823  *      The object must be locked.
1824  */
1825 boolean_t
1826 vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1827 {
1828         vm_page_t m, ma[1];
1829         vm_pindex_t pindex;
1830         int rv;
1831
1832         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1833         for (pindex = start; pindex < end; pindex++) {
1834                 m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL |
1835                     VM_ALLOC_RETRY);
1836                 if (m->valid != VM_PAGE_BITS_ALL) {
1837                         ma[0] = m;
1838                         rv = vm_pager_get_pages(object, ma, 1, 0);
1839                         m = vm_page_lookup(object, pindex);
1840                         if (m == NULL)
1841                                 break;
1842                         if (rv != VM_PAGER_OK) {
1843                                 vm_page_lock(m);
1844                                 vm_page_free(m);
1845                                 vm_page_unlock(m);
1846                                 break;
1847                         }
1848                 }
1849                 /*
1850                  * Keep "m" busy because a subsequent iteration may unlock
1851                  * the object.
1852                  */
1853         }
1854         if (pindex > start) {
1855                 m = vm_page_lookup(object, start);
1856                 while (m != NULL && m->pindex < pindex) {
1857                         vm_page_wakeup(m);
1858                         m = TAILQ_NEXT(m, listq);
1859                 }
1860         }
1861         return (pindex == end);
1862 }
1863
1864 /*
1865  *      Routine:        vm_object_coalesce
1866  *      Function:       Coalesces two objects backing up adjoining
1867  *                      regions of memory into a single object.
1868  *
1869  *      returns TRUE if objects were combined.
1870  *
1871  *      NOTE:   Only works at the moment if the second object is NULL -
1872  *              if it's not, which object do we lock first?
1873  *
1874  *      Parameters:
1875  *              prev_object     First object to coalesce
1876  *              prev_offset     Offset into prev_object
1877  *              prev_size       Size of reference to prev_object
1878  *              next_size       Size of reference to the second object
1879  *              reserved        Indicator that extension region has
1880  *                              swap accounted for
1881  *
1882  *      Conditions:
1883  *      The object must *not* be locked.
1884  */
1885 boolean_t
1886 vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
1887     vm_size_t prev_size, vm_size_t next_size, boolean_t reserved)
1888 {
1889         vm_pindex_t next_pindex;
1890
1891         if (prev_object == NULL)
1892                 return (TRUE);
1893         VM_OBJECT_LOCK(prev_object);
1894         if (prev_object->type != OBJT_DEFAULT &&
1895             prev_object->type != OBJT_SWAP) {
1896                 VM_OBJECT_UNLOCK(prev_object);
1897                 return (FALSE);
1898         }
1899
1900         /*
1901          * Try to collapse the object first
1902          */
1903         vm_object_collapse(prev_object);
1904
1905         /*
1906          * Can't coalesce if: . more than one reference . paged out . shadows
1907          * another object . has a copy elsewhere (any of which mean that the
1908          * pages not mapped to prev_entry may be in use anyway)
1909          */
1910         if (prev_object->backing_object != NULL) {
1911                 VM_OBJECT_UNLOCK(prev_object);
1912                 return (FALSE);
1913         }
1914
1915         prev_size >>= PAGE_SHIFT;
1916         next_size >>= PAGE_SHIFT;
1917         next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
1918
1919         if ((prev_object->ref_count > 1) &&
1920             (prev_object->size != next_pindex)) {
1921                 VM_OBJECT_UNLOCK(prev_object);
1922                 return (FALSE);
1923         }
1924
1925         /*
1926          * Account for the charge.
1927          */
1928         if (prev_object->uip != NULL) {
1929
1930                 /*
1931                  * If prev_object was charged, then this mapping,
1932                  * althought not charged now, may become writable
1933                  * later. Non-NULL uip in the object would prevent
1934                  * swap reservation during enabling of the write
1935                  * access, so reserve swap now. Failed reservation
1936                  * cause allocation of the separate object for the map
1937                  * entry, and swap reservation for this entry is
1938                  * managed in appropriate time.
1939                  */
1940                 if (!reserved && !swap_reserve_by_uid(ptoa(next_size),
1941                     prev_object->uip)) {
1942                         return (FALSE);
1943                 }
1944                 prev_object->charge += ptoa(next_size);
1945         }
1946
1947         /*
1948          * Remove any pages that may still be in the object from a previous
1949          * deallocation.
1950          */
1951         if (next_pindex < prev_object->size) {
1952                 vm_object_page_remove(prev_object,
1953                                       next_pindex,
1954                                       next_pindex + next_size, FALSE);
1955                 if (prev_object->type == OBJT_SWAP)
1956                         swap_pager_freespace(prev_object,
1957                                              next_pindex, next_size);
1958 #if 0
1959                 if (prev_object->uip != NULL) {
1960                         KASSERT(prev_object->charge >=
1961                             ptoa(prev_object->size - next_pindex),
1962                             ("object %p overcharged 1 %jx %jx", prev_object,
1963                                 (uintmax_t)next_pindex, (uintmax_t)next_size));
1964                         prev_object->charge -= ptoa(prev_object->size -
1965                             next_pindex);
1966                 }
1967 #endif
1968         }
1969
1970         /*
1971          * Extend the object if necessary.
1972          */
1973         if (next_pindex + next_size > prev_object->size)
1974                 prev_object->size = next_pindex + next_size;
1975
1976         VM_OBJECT_UNLOCK(prev_object);
1977         return (TRUE);
1978 }
1979
1980 void
1981 vm_object_set_writeable_dirty(vm_object_t object)
1982 {
1983
1984         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1985         if (object->type != OBJT_VNODE ||
1986             (object->flags & OBJ_MIGHTBEDIRTY) != 0)
1987                 return;
1988         vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
1989 }
1990
1991 #include "opt_ddb.h"
1992 #ifdef DDB
1993 #include <sys/kernel.h>
1994
1995 #include <sys/cons.h>
1996
1997 #include <ddb/ddb.h>
1998
1999 static int
2000 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2001 {
2002         vm_map_t tmpm;
2003         vm_map_entry_t tmpe;
2004         vm_object_t obj;
2005         int entcount;
2006
2007         if (map == 0)
2008                 return 0;
2009
2010         if (entry == 0) {
2011                 tmpe = map->header.next;
2012                 entcount = map->nentries;
2013                 while (entcount-- && (tmpe != &map->header)) {
2014                         if (_vm_object_in_map(map, object, tmpe)) {
2015                                 return 1;
2016                         }
2017                         tmpe = tmpe->next;
2018                 }
2019         } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2020                 tmpm = entry->object.sub_map;
2021                 tmpe = tmpm->header.next;
2022                 entcount = tmpm->nentries;
2023                 while (entcount-- && tmpe != &tmpm->header) {
2024                         if (_vm_object_in_map(tmpm, object, tmpe)) {
2025                                 return 1;
2026                         }
2027                         tmpe = tmpe->next;
2028                 }
2029         } else if ((obj = entry->object.vm_object) != NULL) {
2030                 for (; obj; obj = obj->backing_object)
2031                         if (obj == object) {
2032                                 return 1;
2033                         }
2034         }
2035         return 0;
2036 }
2037
2038 static int
2039 vm_object_in_map(vm_object_t object)
2040 {
2041         struct proc *p;
2042
2043         /* sx_slock(&allproc_lock); */
2044         FOREACH_PROC_IN_SYSTEM(p) {
2045                 if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2046                         continue;
2047                 if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
2048                         /* sx_sunlock(&allproc_lock); */
2049                         return 1;
2050                 }
2051         }
2052         /* sx_sunlock(&allproc_lock); */
2053         if (_vm_object_in_map(kernel_map, object, 0))
2054                 return 1;
2055         if (_vm_object_in_map(kmem_map, object, 0))
2056                 return 1;
2057         if (_vm_object_in_map(pager_map, object, 0))
2058                 return 1;
2059         if (_vm_object_in_map(buffer_map, object, 0))
2060                 return 1;
2061         return 0;
2062 }
2063
2064 DB_SHOW_COMMAND(vmochk, vm_object_check)
2065 {
2066         vm_object_t object;
2067
2068         /*
2069          * make sure that internal objs are in a map somewhere
2070          * and none have zero ref counts.
2071          */
2072         TAILQ_FOREACH(object, &vm_object_list, object_list) {
2073                 if (object->handle == NULL &&
2074                     (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2075                         if (object->ref_count == 0) {
2076                                 db_printf("vmochk: internal obj has zero ref count: %ld\n",
2077                                         (long)object->size);
2078                         }
2079                         if (!vm_object_in_map(object)) {
2080                                 db_printf(
2081                         "vmochk: internal obj is not in a map: "
2082                         "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2083                                     object->ref_count, (u_long)object->size, 
2084                                     (u_long)object->size,
2085                                     (void *)object->backing_object);
2086                         }
2087                 }
2088         }
2089 }
2090
2091 /*
2092  *      vm_object_print:        [ debug ]
2093  */
2094 DB_SHOW_COMMAND(object, vm_object_print_static)
2095 {
2096         /* XXX convert args. */
2097         vm_object_t object = (vm_object_t)addr;
2098         boolean_t full = have_addr;
2099
2100         vm_page_t p;
2101
2102         /* XXX count is an (unused) arg.  Avoid shadowing it. */
2103 #define count   was_count
2104
2105         int count;
2106
2107         if (object == NULL)
2108                 return;
2109
2110         db_iprintf(
2111             "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x uip %d charge %jx\n",
2112             object, (int)object->type, (uintmax_t)object->size,
2113             object->resident_page_count, object->ref_count, object->flags,
2114             object->uip ? object->uip->ui_uid : -1, (uintmax_t)object->charge);
2115         db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
2116             object->shadow_count, 
2117             object->backing_object ? object->backing_object->ref_count : 0,
2118             object->backing_object, (uintmax_t)object->backing_object_offset);
2119
2120         if (!full)
2121                 return;
2122
2123         db_indent += 2;
2124         count = 0;
2125         TAILQ_FOREACH(p, &object->memq, listq) {
2126                 if (count == 0)
2127                         db_iprintf("memory:=");
2128                 else if (count == 6) {
2129                         db_printf("\n");
2130                         db_iprintf(" ...");
2131                         count = 0;
2132                 } else
2133                         db_printf(",");
2134                 count++;
2135
2136                 db_printf("(off=0x%jx,page=0x%jx)",
2137                     (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2138         }
2139         if (count != 0)
2140                 db_printf("\n");
2141         db_indent -= 2;
2142 }
2143
2144 /* XXX. */
2145 #undef count
2146
2147 /* XXX need this non-static entry for calling from vm_map_print. */
2148 void
2149 vm_object_print(
2150         /* db_expr_t */ long addr,
2151         boolean_t have_addr,
2152         /* db_expr_t */ long count,
2153         char *modif)
2154 {
2155         vm_object_print_static(addr, have_addr, count, modif);
2156 }
2157
2158 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2159 {
2160         vm_object_t object;
2161         vm_pindex_t fidx;
2162         vm_paddr_t pa;
2163         vm_page_t m, prev_m;
2164         int rcount, nl, c;
2165
2166         nl = 0;
2167         TAILQ_FOREACH(object, &vm_object_list, object_list) {
2168                 db_printf("new object: %p\n", (void *)object);
2169                 if (nl > 18) {
2170                         c = cngetc();
2171                         if (c != ' ')
2172                                 return;
2173                         nl = 0;
2174                 }
2175                 nl++;
2176                 rcount = 0;
2177                 fidx = 0;
2178                 pa = -1;
2179                 TAILQ_FOREACH(m, &object->memq, listq) {
2180                         if (m->pindex > 128)
2181                                 break;
2182                         if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL &&
2183                             prev_m->pindex + 1 != m->pindex) {
2184                                 if (rcount) {
2185                                         db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2186                                                 (long)fidx, rcount, (long)pa);
2187                                         if (nl > 18) {
2188                                                 c = cngetc();
2189                                                 if (c != ' ')
2190                                                         return;
2191                                                 nl = 0;
2192                                         }
2193                                         nl++;
2194                                         rcount = 0;
2195                                 }
2196                         }                               
2197                         if (rcount &&
2198                                 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2199                                 ++rcount;
2200                                 continue;
2201                         }
2202                         if (rcount) {
2203                                 db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2204                                         (long)fidx, rcount, (long)pa);
2205                                 if (nl > 18) {
2206                                         c = cngetc();
2207                                         if (c != ' ')
2208                                                 return;
2209                                         nl = 0;
2210                                 }
2211                                 nl++;
2212                         }
2213                         fidx = m->pindex;
2214                         pa = VM_PAGE_TO_PHYS(m);
2215                         rcount = 1;
2216                 }
2217                 if (rcount) {
2218                         db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2219                                 (long)fidx, rcount, (long)pa);
2220                         if (nl > 18) {
2221                                 c = cngetc();
2222                                 if (c != ' ')
2223                                         return;
2224                                 nl = 0;
2225                         }
2226                         nl++;
2227                 }
2228         }
2229 }
2230 #endif /* DDB */