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