xref: /freebsd/sys/vm/vm_object.c (revision 0217125f2be912d927437637492e76b4da2978ee)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
6df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
17df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
18df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
19df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
20df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  *
363c4dd356SDavid Greenman  *	from: @(#)vm_object.c	8.5 (Berkeley) 3/22/94
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *
39df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40df8bae1dSRodney W. Grimes  * All rights reserved.
41df8bae1dSRodney W. Grimes  *
42df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43df8bae1dSRodney W. Grimes  *
44df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
45df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
46df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
47df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
48df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53df8bae1dSRodney W. Grimes  *
54df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
55df8bae1dSRodney W. Grimes  *
56df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57df8bae1dSRodney W. Grimes  *  School of Computer Science
58df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
59df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
60df8bae1dSRodney W. Grimes  *
61df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
62df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
633c4dd356SDavid Greenman  *
640217125fSDavid Greenman  * $Id: vm_object.c,v 1.21 1995/02/02 09:08:48 davidg Exp $
65df8bae1dSRodney W. Grimes  */
66df8bae1dSRodney W. Grimes 
67df8bae1dSRodney W. Grimes /*
68df8bae1dSRodney W. Grimes  *	Virtual memory object module.
69df8bae1dSRodney W. Grimes  */
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes #include <sys/param.h>
72df8bae1dSRodney W. Grimes #include <sys/systm.h>
73f23b4c91SGarrett Wollman #include <sys/kernel.h>
74f23b4c91SGarrett Wollman #include <sys/proc.h>		/* for curproc, pageproc */
75df8bae1dSRodney W. Grimes #include <sys/malloc.h>
760d94caffSDavid Greenman #include <sys/vnode.h>
770d94caffSDavid Greenman #include <sys/mount.h>
78df8bae1dSRodney W. Grimes 
79df8bae1dSRodney W. Grimes #include <vm/vm.h>
80df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
8126f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
820d94caffSDavid Greenman #include <vm/vm_pager.h>
8305f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
840d94caffSDavid Greenman #include <vm/vnode_pager.h>
85a1f6d91cSDavid Greenman #include <vm/vm_kern.h>
8626f9a767SRodney W. Grimes 
8726f9a767SRodney W. Grimes static void _vm_object_allocate(vm_size_t, vm_object_t);
88df8bae1dSRodney W. Grimes 
89df8bae1dSRodney W. Grimes /*
90df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
91df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
92df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
93df8bae1dSRodney W. Grimes  *
94df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
95df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
96df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
97df8bae1dSRodney W. Grimes  *
98df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
99df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
100df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
101df8bae1dSRodney W. Grimes  *	lock.
102df8bae1dSRodney W. Grimes  *
103df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
104df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
105df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
106df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
107df8bae1dSRodney W. Grimes  *
108df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
109df8bae1dSRodney W. Grimes  *	modified after time of creation are:
110df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
111df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
112df8bae1dSRodney W. Grimes  *
113df8bae1dSRodney W. Grimes  */
114df8bae1dSRodney W. Grimes 
11526f9a767SRodney W. Grimes 
116df8bae1dSRodney W. Grimes struct vm_object kernel_object_store;
117df8bae1dSRodney W. Grimes struct vm_object kmem_object_store;
118df8bae1dSRodney W. Grimes 
1190d94caffSDavid Greenman int vm_object_cache_max;
1200d94caffSDavid Greenman 
1210d94caffSDavid Greenman #define	VM_OBJECT_HASH_COUNT	509
122df8bae1dSRodney W. Grimes 
123df8bae1dSRodney W. Grimes struct vm_object_hash_head vm_object_hashtable[VM_OBJECT_HASH_COUNT];
124df8bae1dSRodney W. Grimes 
125df8bae1dSRodney W. Grimes long object_collapses = 0;
126df8bae1dSRodney W. Grimes long object_bypasses = 0;
127df8bae1dSRodney W. Grimes 
128df8bae1dSRodney W. Grimes static void
129df8bae1dSRodney W. Grimes _vm_object_allocate(size, object)
130df8bae1dSRodney W. Grimes 	vm_size_t size;
131df8bae1dSRodney W. Grimes 	register vm_object_t object;
132df8bae1dSRodney W. Grimes {
133df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
1342fe6e4d7SDavid Greenman 	TAILQ_INIT(&object->reverse_shadow_head);
135a1f6d91cSDavid Greenman 
136df8bae1dSRodney W. Grimes 	object->size = size;
137a1f6d91cSDavid Greenman 	object->ref_count = 1;
138a1f6d91cSDavid Greenman 	vm_object_lock_init(object);
139df8bae1dSRodney W. Grimes 	object->flags = OBJ_INTERNAL;	/* vm_allocate_with_pager will reset */
140df8bae1dSRodney W. Grimes 	object->paging_in_progress = 0;
141a1f6d91cSDavid Greenman 	object->resident_page_count = 0;
142df8bae1dSRodney W. Grimes 
143df8bae1dSRodney W. Grimes 	object->pager = NULL;
144df8bae1dSRodney W. Grimes 	object->paging_offset = 0;
145df8bae1dSRodney W. Grimes 	object->shadow = NULL;
146df8bae1dSRodney W. Grimes 	object->shadow_offset = (vm_offset_t) 0;
147a1f6d91cSDavid Greenman 	object->copy = NULL;
148a1f6d91cSDavid Greenman 
149a1f6d91cSDavid Greenman 	object->last_read = 0;
150df8bae1dSRodney W. Grimes 
151df8bae1dSRodney W. Grimes 	simple_lock(&vm_object_list_lock);
152df8bae1dSRodney W. Grimes 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
153df8bae1dSRodney W. Grimes 	vm_object_count++;
154df8bae1dSRodney W. Grimes 	cnt.v_nzfod += atop(size);
155df8bae1dSRodney W. Grimes 	simple_unlock(&vm_object_list_lock);
156df8bae1dSRodney W. Grimes }
157df8bae1dSRodney W. Grimes 
158df8bae1dSRodney W. Grimes /*
15926f9a767SRodney W. Grimes  *	vm_object_init:
16026f9a767SRodney W. Grimes  *
16126f9a767SRodney W. Grimes  *	Initialize the VM objects module.
16226f9a767SRodney W. Grimes  */
16326f9a767SRodney W. Grimes void
16426f9a767SRodney W. Grimes vm_object_init(vm_offset_t nothing)
16526f9a767SRodney W. Grimes {
16626f9a767SRodney W. Grimes 	register int i;
16726f9a767SRodney W. Grimes 
16826f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_cached_list);
16926f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
17026f9a767SRodney W. Grimes 	vm_object_count = 0;
17126f9a767SRodney W. Grimes 	simple_lock_init(&vm_cache_lock);
17226f9a767SRodney W. Grimes 	simple_lock_init(&vm_object_list_lock);
1730217125fSDavid Greenman 
1740217125fSDavid Greenman 	vm_object_cache_max = 84;
1750217125fSDavid Greenman 	if (cnt.v_page_count > 1000)
1760217125fSDavid Greenman 		vm_object_cache_max += (cnt.v_page_count - 1000) / 4;
17726f9a767SRodney W. Grimes 
17826f9a767SRodney W. Grimes 	for (i = 0; i < VM_OBJECT_HASH_COUNT; i++)
17926f9a767SRodney W. Grimes 		TAILQ_INIT(&vm_object_hashtable[i]);
18026f9a767SRodney W. Grimes 
18126f9a767SRodney W. Grimes 	kernel_object = &kernel_object_store;
18226f9a767SRodney W. Grimes 	_vm_object_allocate(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS,
18326f9a767SRodney W. Grimes 	    kernel_object);
18426f9a767SRodney W. Grimes 
18526f9a767SRodney W. Grimes 	kmem_object = &kmem_object_store;
18626f9a767SRodney W. Grimes 	_vm_object_allocate(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS,
18726f9a767SRodney W. Grimes 	    kmem_object);
18826f9a767SRodney W. Grimes }
18926f9a767SRodney W. Grimes 
19026f9a767SRodney W. Grimes /*
19126f9a767SRodney W. Grimes  *	vm_object_allocate:
19226f9a767SRodney W. Grimes  *
19326f9a767SRodney W. Grimes  *	Returns a new object with the given size.
19426f9a767SRodney W. Grimes  */
19526f9a767SRodney W. Grimes 
19626f9a767SRodney W. Grimes vm_object_t
19726f9a767SRodney W. Grimes vm_object_allocate(size)
19826f9a767SRodney W. Grimes 	vm_size_t size;
19926f9a767SRodney W. Grimes {
20026f9a767SRodney W. Grimes 	register vm_object_t result;
20126f9a767SRodney W. Grimes 
20226f9a767SRodney W. Grimes 	result = (vm_object_t)
20326f9a767SRodney W. Grimes 	    malloc((u_long) sizeof *result, M_VMOBJ, M_WAITOK);
20426f9a767SRodney W. Grimes 
20526f9a767SRodney W. Grimes 
20626f9a767SRodney W. Grimes 	_vm_object_allocate(size, result);
20726f9a767SRodney W. Grimes 
20826f9a767SRodney W. Grimes 	return (result);
20926f9a767SRodney W. Grimes }
21026f9a767SRodney W. Grimes 
21126f9a767SRodney W. Grimes 
21226f9a767SRodney W. Grimes /*
213df8bae1dSRodney W. Grimes  *	vm_object_reference:
214df8bae1dSRodney W. Grimes  *
215df8bae1dSRodney W. Grimes  *	Gets another reference to the given object.
216df8bae1dSRodney W. Grimes  */
21726f9a767SRodney W. Grimes inline void
21826f9a767SRodney W. Grimes vm_object_reference(object)
219df8bae1dSRodney W. Grimes 	register vm_object_t object;
220df8bae1dSRodney W. Grimes {
221df8bae1dSRodney W. Grimes 	if (object == NULL)
222df8bae1dSRodney W. Grimes 		return;
223df8bae1dSRodney W. Grimes 
224df8bae1dSRodney W. Grimes 	vm_object_lock(object);
225df8bae1dSRodney W. Grimes 	object->ref_count++;
226df8bae1dSRodney W. Grimes 	vm_object_unlock(object);
227df8bae1dSRodney W. Grimes }
228df8bae1dSRodney W. Grimes 
229df8bae1dSRodney W. Grimes /*
230df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
231df8bae1dSRodney W. Grimes  *
232df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
233df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
234df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
235df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
236df8bae1dSRodney W. Grimes  *	may be relinquished.
237df8bae1dSRodney W. Grimes  *
238df8bae1dSRodney W. Grimes  *	No object may be locked.
239df8bae1dSRodney W. Grimes  */
24026f9a767SRodney W. Grimes void
24126f9a767SRodney W. Grimes vm_object_deallocate(object)
24226f9a767SRodney W. Grimes 	vm_object_t object;
243df8bae1dSRodney W. Grimes {
244df8bae1dSRodney W. Grimes 	vm_object_t temp;
245df8bae1dSRodney W. Grimes 
246df8bae1dSRodney W. Grimes 	while (object != NULL) {
247df8bae1dSRodney W. Grimes 
248df8bae1dSRodney W. Grimes 		/*
2490d94caffSDavid Greenman 		 * The cache holds a reference (uncounted) to the object; we
2500d94caffSDavid Greenman 		 * must lock it before removing the object.
251df8bae1dSRodney W. Grimes 		 */
252df8bae1dSRodney W. Grimes 
253df8bae1dSRodney W. Grimes 		vm_object_cache_lock();
254df8bae1dSRodney W. Grimes 
255df8bae1dSRodney W. Grimes 		/*
256df8bae1dSRodney W. Grimes 		 * Lose the reference
257df8bae1dSRodney W. Grimes 		 */
258df8bae1dSRodney W. Grimes 		vm_object_lock(object);
259df8bae1dSRodney W. Grimes 		if (--(object->ref_count) != 0) {
260010cf3b9SDavid Greenman 			if (object->ref_count == 1) {
261a1f6d91cSDavid Greenman 				vm_object_t robject;
262a1f6d91cSDavid Greenman 				robject = object->reverse_shadow_head.tqh_first;
263a1f6d91cSDavid Greenman 				if( robject) {
264a1f6d91cSDavid Greenman 					int s;
265a1f6d91cSDavid Greenman 					robject->ref_count += 2;
266a1f6d91cSDavid Greenman 					object->ref_count += 1;
267a1f6d91cSDavid Greenman 
268a1f6d91cSDavid Greenman 					do {
269a1f6d91cSDavid Greenman 						s = splhigh();
270a1f6d91cSDavid Greenman 						while( robject->paging_in_progress) {
271a1f6d91cSDavid Greenman 							tsleep(robject, PVM, "objde1", 0);
2720d94caffSDavid Greenman 						}
273a1f6d91cSDavid Greenman 
274a1f6d91cSDavid Greenman 						while( object->paging_in_progress) {
275a1f6d91cSDavid Greenman 							tsleep(object, PVM, "objde2", 0);
276a1f6d91cSDavid Greenman 						}
277a1f6d91cSDavid Greenman 						splx(s);
278a1f6d91cSDavid Greenman 
279a1f6d91cSDavid Greenman 					} while( object->paging_in_progress || robject->paging_in_progress);
280a1f6d91cSDavid Greenman 
281a1f6d91cSDavid Greenman 					object->ref_count -= 1;
282a1f6d91cSDavid Greenman 					robject->ref_count -= 2;
283a1f6d91cSDavid Greenman 					if( robject->ref_count == 0) {
284a1f6d91cSDavid Greenman 						vm_object_unlock(object);
285a1f6d91cSDavid Greenman 						vm_object_cache_unlock();
286a1f6d91cSDavid Greenman 						robject->ref_count += 1;
287a1f6d91cSDavid Greenman 						vm_object_deallocate(robject);
288a1f6d91cSDavid Greenman 						return;
289a1f6d91cSDavid Greenman 					}
290a1f6d91cSDavid Greenman 					vm_object_cache_unlock();
291a1f6d91cSDavid Greenman 					vm_object_unlock(object);
292a1f6d91cSDavid Greenman 					vm_object_lock(robject);
293a1f6d91cSDavid Greenman 					vm_object_collapse(robject);
294a1f6d91cSDavid Greenman 					return;
295010cf3b9SDavid Greenman 				}
296010cf3b9SDavid Greenman 			}
29726f9a767SRodney W. Grimes 			vm_object_unlock(object);
298df8bae1dSRodney W. Grimes 			/*
2990d94caffSDavid Greenman 			 * If there are still references, then we are done.
300df8bae1dSRodney W. Grimes 			 */
301df8bae1dSRodney W. Grimes 			vm_object_cache_unlock();
302df8bae1dSRodney W. Grimes 			return;
303df8bae1dSRodney W. Grimes 		}
304df8bae1dSRodney W. Grimes 		/*
3050d94caffSDavid Greenman 		 * See if this object can persist.  If so, enter it in the
3060d94caffSDavid Greenman 		 * cache, then deactivate all of its pages.
307df8bae1dSRodney W. Grimes 		 */
308df8bae1dSRodney W. Grimes 
309df8bae1dSRodney W. Grimes 		if (object->flags & OBJ_CANPERSIST) {
310df8bae1dSRodney W. Grimes 
311df8bae1dSRodney W. Grimes 			TAILQ_INSERT_TAIL(&vm_object_cached_list, object,
312df8bae1dSRodney W. Grimes 			    cached_list);
313df8bae1dSRodney W. Grimes 			vm_object_cached++;
314df8bae1dSRodney W. Grimes 			vm_object_cache_unlock();
315df8bae1dSRodney W. Grimes 
316df8bae1dSRodney W. Grimes 			vm_object_unlock(object);
317df8bae1dSRodney W. Grimes 
318df8bae1dSRodney W. Grimes 			vm_object_cache_trim();
319df8bae1dSRodney W. Grimes 			return;
320df8bae1dSRodney W. Grimes 		}
321df8bae1dSRodney W. Grimes 		/*
322df8bae1dSRodney W. Grimes 		 * Make sure no one can look us up now.
323df8bae1dSRodney W. Grimes 		 */
3240d94caffSDavid Greenman 		object->flags |= OBJ_DEAD;
325df8bae1dSRodney W. Grimes 		vm_object_remove(object->pager);
326df8bae1dSRodney W. Grimes 		vm_object_cache_unlock();
327df8bae1dSRodney W. Grimes 
328df8bae1dSRodney W. Grimes 		temp = object->shadow;
3292fe6e4d7SDavid Greenman 		if (temp)
3302fe6e4d7SDavid Greenman 			TAILQ_REMOVE(&temp->reverse_shadow_head, object, reverse_shadow_list);
331df8bae1dSRodney W. Grimes 		vm_object_terminate(object);
332df8bae1dSRodney W. Grimes 		/* unlocks and deallocates object */
333df8bae1dSRodney W. Grimes 		object = temp;
334df8bae1dSRodney W. Grimes 	}
335df8bae1dSRodney W. Grimes }
336df8bae1dSRodney W. Grimes 
337df8bae1dSRodney W. Grimes /*
338df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
339df8bae1dSRodney W. Grimes  *	up all previously used resources.
340df8bae1dSRodney W. Grimes  *
341df8bae1dSRodney W. Grimes  *	The object must be locked.
342df8bae1dSRodney W. Grimes  */
34326f9a767SRodney W. Grimes void
34426f9a767SRodney W. Grimes vm_object_terminate(object)
345df8bae1dSRodney W. Grimes 	register vm_object_t object;
346df8bae1dSRodney W. Grimes {
3470d94caffSDavid Greenman 	register vm_page_t p, next;
348df8bae1dSRodney W. Grimes 	vm_object_t shadow_object;
34926f9a767SRodney W. Grimes 	int s;
3500d94caffSDavid Greenman 	struct vnode *vp = NULL;
351df8bae1dSRodney W. Grimes 
352df8bae1dSRodney W. Grimes 	/*
3530d94caffSDavid Greenman 	 * Detach the object from its shadow if we are the shadow's copy.
354df8bae1dSRodney W. Grimes 	 */
355df8bae1dSRodney W. Grimes 	if ((shadow_object = object->shadow) != NULL) {
356df8bae1dSRodney W. Grimes 		vm_object_lock(shadow_object);
357df8bae1dSRodney W. Grimes 		if (shadow_object->copy == object)
358df8bae1dSRodney W. Grimes 			shadow_object->copy = NULL;
35926f9a767SRodney W. Grimes /*
360df8bae1dSRodney W. Grimes 		else if (shadow_object->copy != NULL)
361df8bae1dSRodney W. Grimes 			panic("vm_object_terminate: copy/shadow inconsistency");
36226f9a767SRodney W. Grimes */
363df8bae1dSRodney W. Grimes 		vm_object_unlock(shadow_object);
364df8bae1dSRodney W. Grimes 	}
3650d94caffSDavid Greenman 	if (object->pager && (object->pager->pg_type == PG_VNODE)) {
3660d94caffSDavid Greenman 		vn_pager_t vnp = object->pager->pg_data;
367df8bae1dSRodney W. Grimes 
3680d94caffSDavid Greenman 		vp = vnp->vnp_vp;
3690d94caffSDavid Greenman 		VOP_FSYNC(vp, NOCRED, MNT_WAIT, NULL);
3700d94caffSDavid Greenman 		vinvalbuf(vp, 0, NOCRED, NULL, 0, 0);
3710d94caffSDavid Greenman 	}
372df8bae1dSRodney W. Grimes 	/*
3730d94caffSDavid Greenman 	 * Wait until the pageout daemon is through with the object.
374df8bae1dSRodney W. Grimes 	 */
37526f9a767SRodney W. Grimes 
3760d94caffSDavid Greenman 	s = splhigh();
377df8bae1dSRodney W. Grimes 	while (object->paging_in_progress) {
3780d94caffSDavid Greenman 		vm_object_unlock(object);
3790d94caffSDavid Greenman 		tsleep((caddr_t) object, PVM, "objtrm", 0);
380df8bae1dSRodney W. Grimes 		vm_object_lock(object);
381df8bae1dSRodney W. Grimes 	}
3820d94caffSDavid Greenman 	splx(s);
383df8bae1dSRodney W. Grimes 
384df8bae1dSRodney W. Grimes 	/*
3850d94caffSDavid Greenman 	 * While the paging system is locked, pull the object's pages off the
3860d94caffSDavid Greenman 	 * active and inactive queues.  This keeps the pageout daemon from
3870d94caffSDavid Greenman 	 * playing with them during vm_pager_deallocate.
388df8bae1dSRodney W. Grimes 	 *
3890d94caffSDavid Greenman 	 * We can't free the pages yet, because the object's pager may have to
3900d94caffSDavid Greenman 	 * write them out before deallocating the paging space.
391df8bae1dSRodney W. Grimes 	 */
39226f9a767SRodney W. Grimes 
3930d94caffSDavid Greenman 	for (p = object->memq.tqh_first; p; p = next) {
39426f9a767SRodney W. Grimes 		VM_PAGE_CHECK(p);
3950d94caffSDavid Greenman 		next = p->listq.tqe_next;
39626f9a767SRodney W. Grimes 
39726f9a767SRodney W. Grimes 		vm_page_lock_queues();
3980d94caffSDavid Greenman 		if (p->flags & PG_CACHE)
3990d94caffSDavid Greenman 			vm_page_free(p);
4000d94caffSDavid Greenman 		else
4010d94caffSDavid Greenman 			vm_page_unqueue(p);
40226f9a767SRodney W. Grimes 		vm_page_unlock_queues();
4030d94caffSDavid Greenman 		p = next;
40426f9a767SRodney W. Grimes 	}
40526f9a767SRodney W. Grimes 
40626f9a767SRodney W. Grimes 	if (object->paging_in_progress != 0)
40726f9a767SRodney W. Grimes 		panic("vm_object_deallocate: pageout in progress");
40826f9a767SRodney W. Grimes 
40926f9a767SRodney W. Grimes 	/*
4100d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
4110d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
41226f9a767SRodney W. Grimes 	 */
41326f9a767SRodney W. Grimes 
414a465acdaSDavid Greenman 	if (((object->flags & OBJ_INTERNAL) == 0) &&
415a465acdaSDavid Greenman 	    object->pager && (object->pager->pg_type != PG_DEVICE)) {
416df8bae1dSRodney W. Grimes 		(void) vm_object_page_clean(object, 0, 0, TRUE, TRUE);
417df8bae1dSRodney W. Grimes 	}
418df8bae1dSRodney W. Grimes 	/*
4190d94caffSDavid Greenman 	 * one last time -- get rid of buffers that might have been created
4200d94caffSDavid Greenman 	 * for the vm_object_page_clean
4210d94caffSDavid Greenman 	 */
4220d94caffSDavid Greenman 	if (vp != NULL) {
4230d94caffSDavid Greenman 		vm_object_unlock(object);
4240d94caffSDavid Greenman 		vinvalbuf(vp, 0, NOCRED, NULL, 0, 0);
4250d94caffSDavid Greenman 		vm_object_lock(object);
4260d94caffSDavid Greenman 	}
4270d94caffSDavid Greenman 	/*
4280d94caffSDavid Greenman 	 * Now free the pages. For internal objects, this also removes them
4290d94caffSDavid Greenman 	 * from paging queues.
430df8bae1dSRodney W. Grimes 	 */
431df8bae1dSRodney W. Grimes 	while ((p = object->memq.tqh_first) != NULL) {
432df8bae1dSRodney W. Grimes 		VM_PAGE_CHECK(p);
433df8bae1dSRodney W. Grimes 		vm_page_lock_queues();
4340d94caffSDavid Greenman 		PAGE_WAKEUP(p);
435df8bae1dSRodney W. Grimes 		vm_page_free(p);
436df8bae1dSRodney W. Grimes 		cnt.v_pfree++;
437df8bae1dSRodney W. Grimes 		vm_page_unlock_queues();
438df8bae1dSRodney W. Grimes 	}
4390d94caffSDavid Greenman 	vm_object_unlock(object);
440df8bae1dSRodney W. Grimes 
441df8bae1dSRodney W. Grimes 	/*
442df8bae1dSRodney W. Grimes 	 * Let the pager know object is dead.
443df8bae1dSRodney W. Grimes 	 */
444df8bae1dSRodney W. Grimes 	if (object->pager != NULL)
445df8bae1dSRodney W. Grimes 		vm_pager_deallocate(object->pager);
446df8bae1dSRodney W. Grimes 
447df8bae1dSRodney W. Grimes 	simple_lock(&vm_object_list_lock);
448df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
449df8bae1dSRodney W. Grimes 	vm_object_count--;
450df8bae1dSRodney W. Grimes 	simple_unlock(&vm_object_list_lock);
451df8bae1dSRodney W. Grimes 
452df8bae1dSRodney W. Grimes 	/*
453df8bae1dSRodney W. Grimes 	 * Free the space for the object.
454df8bae1dSRodney W. Grimes 	 */
455df8bae1dSRodney W. Grimes 	free((caddr_t) object, M_VMOBJ);
456df8bae1dSRodney W. Grimes }
457df8bae1dSRodney W. Grimes 
458df8bae1dSRodney W. Grimes /*
459df8bae1dSRodney W. Grimes  *	vm_object_page_clean
460df8bae1dSRodney W. Grimes  *
461df8bae1dSRodney W. Grimes  *	Clean all dirty pages in the specified range of object.
46226f9a767SRodney W. Grimes  *	Leaves page on whatever queue it is currently on.
46326f9a767SRodney W. Grimes  *
46426f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
46526f9a767SRodney W. Grimes  *
46626f9a767SRodney W. Grimes  *	The object must be locked.
46726f9a767SRodney W. Grimes  */
46826f9a767SRodney W. Grimes #if 1
46926f9a767SRodney W. Grimes boolean_t
47026f9a767SRodney W. Grimes vm_object_page_clean(object, start, end, syncio, de_queue)
47126f9a767SRodney W. Grimes 	register vm_object_t object;
47226f9a767SRodney W. Grimes 	register vm_offset_t start;
47326f9a767SRodney W. Grimes 	register vm_offset_t end;
47426f9a767SRodney W. Grimes 	boolean_t syncio;
47526f9a767SRodney W. Grimes 	boolean_t de_queue;
47626f9a767SRodney W. Grimes {
47726f9a767SRodney W. Grimes 	register vm_page_t p, nextp;
47826f9a767SRodney W. Grimes 	int size;
47926f9a767SRodney W. Grimes 
48026f9a767SRodney W. Grimes 	if (object->pager == NULL)
48126f9a767SRodney W. Grimes 		return 1;
48226f9a767SRodney W. Grimes 
48326f9a767SRodney W. Grimes 	if (start != end) {
48426f9a767SRodney W. Grimes 		start = trunc_page(start);
48526f9a767SRodney W. Grimes 		end = round_page(end);
48626f9a767SRodney W. Grimes 	}
48726f9a767SRodney W. Grimes 	size = end - start;
48826f9a767SRodney W. Grimes 
48926f9a767SRodney W. Grimes again:
49026f9a767SRodney W. Grimes 	/*
49126f9a767SRodney W. Grimes 	 * Wait until the pageout daemon is through with the object.
49226f9a767SRodney W. Grimes 	 */
49326f9a767SRodney W. Grimes 	while (object->paging_in_progress) {
4940d94caffSDavid Greenman 		tsleep(object, PVM, "objpcw", 0);
49526f9a767SRodney W. Grimes 	}
49626f9a767SRodney W. Grimes 
49726f9a767SRodney W. Grimes 	nextp = object->memq.tqh_first;
49826f9a767SRodney W. Grimes 	while ((p = nextp) && ((start == end) || (size != 0))) {
49926f9a767SRodney W. Grimes 		nextp = p->listq.tqe_next;
50026f9a767SRodney W. Grimes 		if (start == end || (p->offset >= start && p->offset < end)) {
5010d94caffSDavid Greenman 			if ((p->flags & PG_BUSY) || p->busy) {
5020d94caffSDavid Greenman 				int s = splhigh();
50326f9a767SRodney W. Grimes 
5040d94caffSDavid Greenman 				p->flags |= PG_WANTED;
5050d94caffSDavid Greenman 				tsleep(p, PVM, "objpcn", 0);
5060d94caffSDavid Greenman 				splx(s);
5070d94caffSDavid Greenman 				goto again;
5080d94caffSDavid Greenman 			}
50926f9a767SRodney W. Grimes 			size -= PAGE_SIZE;
51026f9a767SRodney W. Grimes 
5110d94caffSDavid Greenman 			vm_page_test_dirty(p);
51226f9a767SRodney W. Grimes 
5130d94caffSDavid Greenman 			if ((p->dirty & p->valid) != 0) {
51426f9a767SRodney W. Grimes 				vm_pageout_clean(p, VM_PAGEOUT_FORCE);
51526f9a767SRodney W. Grimes 				goto again;
51626f9a767SRodney W. Grimes 			}
51726f9a767SRodney W. Grimes 		}
51826f9a767SRodney W. Grimes 	}
51926f9a767SRodney W. Grimes 	wakeup((caddr_t) object);
52026f9a767SRodney W. Grimes 	return 1;
52126f9a767SRodney W. Grimes }
52226f9a767SRodney W. Grimes #endif
52326f9a767SRodney W. Grimes /*
52426f9a767SRodney W. Grimes  *	vm_object_page_clean
52526f9a767SRodney W. Grimes  *
52626f9a767SRodney W. Grimes  *	Clean all dirty pages in the specified range of object.
527df8bae1dSRodney W. Grimes  *	If syncio is TRUE, page cleaning is done synchronously.
528df8bae1dSRodney W. Grimes  *	If de_queue is TRUE, pages are removed from any paging queue
529df8bae1dSRodney W. Grimes  *	they were on, otherwise they are left on whatever queue they
530df8bae1dSRodney W. Grimes  *	were on before the cleaning operation began.
531df8bae1dSRodney W. Grimes  *
532df8bae1dSRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
533df8bae1dSRodney W. Grimes  *
534df8bae1dSRodney W. Grimes  *	The object must be locked.
535df8bae1dSRodney W. Grimes  *
536df8bae1dSRodney W. Grimes  *	Returns TRUE if all was well, FALSE if there was a pager error
537df8bae1dSRodney W. Grimes  *	somewhere.  We attempt to clean (and dequeue) all pages regardless
538df8bae1dSRodney W. Grimes  *	of where an error occurs.
539df8bae1dSRodney W. Grimes  */
54026f9a767SRodney W. Grimes #if 0
541df8bae1dSRodney W. Grimes boolean_t
542df8bae1dSRodney W. Grimes vm_object_page_clean(object, start, end, syncio, de_queue)
543df8bae1dSRodney W. Grimes 	register vm_object_t object;
544df8bae1dSRodney W. Grimes 	register vm_offset_t start;
545df8bae1dSRodney W. Grimes 	register vm_offset_t end;
546df8bae1dSRodney W. Grimes 	boolean_t syncio;
547df8bae1dSRodney W. Grimes 	boolean_t de_queue;
548df8bae1dSRodney W. Grimes {
549df8bae1dSRodney W. Grimes 	register vm_page_t p;
550df8bae1dSRodney W. Grimes 	int onqueue;
551df8bae1dSRodney W. Grimes 	boolean_t noerror = TRUE;
552df8bae1dSRodney W. Grimes 
553df8bae1dSRodney W. Grimes 	if (object == NULL)
554df8bae1dSRodney W. Grimes 		return (TRUE);
555df8bae1dSRodney W. Grimes 
556df8bae1dSRodney W. Grimes 	/*
557df8bae1dSRodney W. Grimes 	 * If it is an internal object and there is no pager, attempt to
5580d94caffSDavid Greenman 	 * allocate one.  Note that vm_object_collapse may relocate one from a
5590d94caffSDavid Greenman 	 * collapsed object so we must recheck afterward.
560df8bae1dSRodney W. Grimes 	 */
561df8bae1dSRodney W. Grimes 	if ((object->flags & OBJ_INTERNAL) && object->pager == NULL) {
562df8bae1dSRodney W. Grimes 		vm_object_collapse(object);
563df8bae1dSRodney W. Grimes 		if (object->pager == NULL) {
564df8bae1dSRodney W. Grimes 			vm_pager_t pager;
565df8bae1dSRodney W. Grimes 
566df8bae1dSRodney W. Grimes 			vm_object_unlock(object);
567df8bae1dSRodney W. Grimes 			pager = vm_pager_allocate(PG_DFLT, (caddr_t) 0,
568df8bae1dSRodney W. Grimes 			    object->size, VM_PROT_ALL,
569df8bae1dSRodney W. Grimes 			    (vm_offset_t) 0);
570df8bae1dSRodney W. Grimes 			if (pager)
571df8bae1dSRodney W. Grimes 				vm_object_setpager(object, pager, 0, FALSE);
572df8bae1dSRodney W. Grimes 			vm_object_lock(object);
573df8bae1dSRodney W. Grimes 		}
574df8bae1dSRodney W. Grimes 	}
575df8bae1dSRodney W. Grimes 	if (object->pager == NULL)
576df8bae1dSRodney W. Grimes 		return (FALSE);
577df8bae1dSRodney W. Grimes 
578df8bae1dSRodney W. Grimes again:
579df8bae1dSRodney W. Grimes 	/*
580df8bae1dSRodney W. Grimes 	 * Wait until the pageout daemon is through with the object.
581df8bae1dSRodney W. Grimes 	 */
582df8bae1dSRodney W. Grimes 	while (object->paging_in_progress) {
583df8bae1dSRodney W. Grimes 		vm_object_sleep((int) object, object, FALSE);
584df8bae1dSRodney W. Grimes 		vm_object_lock(object);
585df8bae1dSRodney W. Grimes 	}
586df8bae1dSRodney W. Grimes 	/*
587df8bae1dSRodney W. Grimes 	 * Loop through the object page list cleaning as necessary.
588df8bae1dSRodney W. Grimes 	 */
589df8bae1dSRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) {
59026f9a767SRodney W. Grimes 		onqueue = 0;
591df8bae1dSRodney W. Grimes 		if ((start == end || p->offset >= start && p->offset < end) &&
592df8bae1dSRodney W. Grimes 		    !(p->flags & PG_FICTITIOUS)) {
5930d94caffSDavid Greenman 			vm_page_test_dirty(p);
594df8bae1dSRodney W. Grimes 			/*
5950d94caffSDavid Greenman 			 * Remove the page from any paging queue. This needs
5960d94caffSDavid Greenman 			 * to be done if either we have been explicitly asked
5970d94caffSDavid Greenman 			 * to do so or it is about to be cleaned (see comment
5980d94caffSDavid Greenman 			 * below).
599df8bae1dSRodney W. Grimes 			 */
6000d94caffSDavid Greenman 			if (de_queue || (p->dirty & p->valid)) {
601df8bae1dSRodney W. Grimes 				vm_page_lock_queues();
602df8bae1dSRodney W. Grimes 				if (p->flags & PG_ACTIVE) {
603df8bae1dSRodney W. Grimes 					TAILQ_REMOVE(&vm_page_queue_active,
604df8bae1dSRodney W. Grimes 					    p, pageq);
605df8bae1dSRodney W. Grimes 					p->flags &= ~PG_ACTIVE;
606df8bae1dSRodney W. Grimes 					cnt.v_active_count--;
607df8bae1dSRodney W. Grimes 					onqueue = 1;
608df8bae1dSRodney W. Grimes 				} else if (p->flags & PG_INACTIVE) {
609df8bae1dSRodney W. Grimes 					TAILQ_REMOVE(&vm_page_queue_inactive,
610df8bae1dSRodney W. Grimes 					    p, pageq);
611df8bae1dSRodney W. Grimes 					p->flags &= ~PG_INACTIVE;
612df8bae1dSRodney W. Grimes 					cnt.v_inactive_count--;
613df8bae1dSRodney W. Grimes 					onqueue = -1;
614df8bae1dSRodney W. Grimes 				} else
615df8bae1dSRodney W. Grimes 					onqueue = 0;
616df8bae1dSRodney W. Grimes 				vm_page_unlock_queues();
617df8bae1dSRodney W. Grimes 			}
618df8bae1dSRodney W. Grimes 			/*
619df8bae1dSRodney W. Grimes 			 * To ensure the state of the page doesn't change
6200d94caffSDavid Greenman 			 * during the clean operation we do two things. First
6210d94caffSDavid Greenman 			 * we set the busy bit and write-protect all mappings
6220d94caffSDavid Greenman 			 * to ensure that write accesses to the page block (in
6230d94caffSDavid Greenman 			 * vm_fault).  Second, we remove the page from any
6240d94caffSDavid Greenman 			 * paging queue to foil the pageout daemon
6250d94caffSDavid Greenman 			 * (vm_pageout_scan).
626df8bae1dSRodney W. Grimes 			 */
627df8bae1dSRodney W. Grimes 			pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_READ);
6280d94caffSDavid Greenman 			if (p->dirty & p->valid) {
629df8bae1dSRodney W. Grimes 				p->flags |= PG_BUSY;
630df8bae1dSRodney W. Grimes 				object->paging_in_progress++;
631df8bae1dSRodney W. Grimes 				vm_object_unlock(object);
632df8bae1dSRodney W. Grimes 				/*
6330d94caffSDavid Greenman 				 * XXX if put fails we mark the page as clean
6340d94caffSDavid Greenman 				 * to avoid an infinite loop. Will loose
6350d94caffSDavid Greenman 				 * changes to the page.
636df8bae1dSRodney W. Grimes 				 */
637df8bae1dSRodney W. Grimes 				if (vm_pager_put(object->pager, p, syncio)) {
638df8bae1dSRodney W. Grimes 					printf("%s: pager_put error\n",
639df8bae1dSRodney W. Grimes 					    "vm_object_page_clean");
6400d94caffSDavid Greenman 					p->dirty = 0;
641df8bae1dSRodney W. Grimes 					noerror = FALSE;
642df8bae1dSRodney W. Grimes 				}
643df8bae1dSRodney W. Grimes 				vm_object_lock(object);
644df8bae1dSRodney W. Grimes 				object->paging_in_progress--;
645df8bae1dSRodney W. Grimes 				if (!de_queue && onqueue) {
646df8bae1dSRodney W. Grimes 					vm_page_lock_queues();
647df8bae1dSRodney W. Grimes 					if (onqueue > 0)
648df8bae1dSRodney W. Grimes 						vm_page_activate(p);
649df8bae1dSRodney W. Grimes 					else
650df8bae1dSRodney W. Grimes 						vm_page_deactivate(p);
651df8bae1dSRodney W. Grimes 					vm_page_unlock_queues();
652df8bae1dSRodney W. Grimes 				}
653df8bae1dSRodney W. Grimes 				PAGE_WAKEUP(p);
654df8bae1dSRodney W. Grimes 				goto again;
655df8bae1dSRodney W. Grimes 			}
656df8bae1dSRodney W. Grimes 		}
657df8bae1dSRodney W. Grimes 	}
658df8bae1dSRodney W. Grimes 	return (noerror);
659df8bae1dSRodney W. Grimes }
66026f9a767SRodney W. Grimes #endif
661df8bae1dSRodney W. Grimes 
662df8bae1dSRodney W. Grimes /*
663df8bae1dSRodney W. Grimes  *	vm_object_deactivate_pages
664df8bae1dSRodney W. Grimes  *
665df8bae1dSRodney W. Grimes  *	Deactivate all pages in the specified object.  (Keep its pages
666df8bae1dSRodney W. Grimes  *	in memory even though it is no longer referenced.)
667df8bae1dSRodney W. Grimes  *
668df8bae1dSRodney W. Grimes  *	The object must be locked.
669df8bae1dSRodney W. Grimes  */
670df8bae1dSRodney W. Grimes void
671df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object)
672df8bae1dSRodney W. Grimes 	register vm_object_t object;
673df8bae1dSRodney W. Grimes {
674df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
675df8bae1dSRodney W. Grimes 
676df8bae1dSRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = next) {
677df8bae1dSRodney W. Grimes 		next = p->listq.tqe_next;
678df8bae1dSRodney W. Grimes 		vm_page_lock_queues();
679df8bae1dSRodney W. Grimes 		vm_page_deactivate(p);
680df8bae1dSRodney W. Grimes 		vm_page_unlock_queues();
681df8bae1dSRodney W. Grimes 	}
682df8bae1dSRodney W. Grimes }
683df8bae1dSRodney W. Grimes 
684df8bae1dSRodney W. Grimes /*
685df8bae1dSRodney W. Grimes  *	Trim the object cache to size.
686df8bae1dSRodney W. Grimes  */
687df8bae1dSRodney W. Grimes void
688df8bae1dSRodney W. Grimes vm_object_cache_trim()
689df8bae1dSRodney W. Grimes {
690df8bae1dSRodney W. Grimes 	register vm_object_t object;
691df8bae1dSRodney W. Grimes 
692df8bae1dSRodney W. Grimes 	vm_object_cache_lock();
6930d94caffSDavid Greenman 	while (vm_object_cached > vm_object_cache_max) {
694df8bae1dSRodney W. Grimes 		object = vm_object_cached_list.tqh_first;
695df8bae1dSRodney W. Grimes 		vm_object_cache_unlock();
696df8bae1dSRodney W. Grimes 
697df8bae1dSRodney W. Grimes 		if (object != vm_object_lookup(object->pager))
6988e58bf68SDavid Greenman 			panic("vm_object_cache_trim: I'm sooo confused.");
699df8bae1dSRodney W. Grimes 
700df8bae1dSRodney W. Grimes 		pager_cache(object, FALSE);
701df8bae1dSRodney W. Grimes 
702df8bae1dSRodney W. Grimes 		vm_object_cache_lock();
703df8bae1dSRodney W. Grimes 	}
704df8bae1dSRodney W. Grimes 	vm_object_cache_unlock();
705df8bae1dSRodney W. Grimes }
706df8bae1dSRodney W. Grimes 
70726f9a767SRodney W. Grimes 
708df8bae1dSRodney W. Grimes /*
709df8bae1dSRodney W. Grimes  *	vm_object_pmap_copy:
710df8bae1dSRodney W. Grimes  *
711df8bae1dSRodney W. Grimes  *	Makes all physical pages in the specified
712df8bae1dSRodney W. Grimes  *	object range copy-on-write.  No writeable
713df8bae1dSRodney W. Grimes  *	references to these pages should remain.
714df8bae1dSRodney W. Grimes  *
715df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
716df8bae1dSRodney W. Grimes  */
7170d94caffSDavid Greenman void
7180d94caffSDavid Greenman vm_object_pmap_copy(object, start, end)
719df8bae1dSRodney W. Grimes 	register vm_object_t object;
720df8bae1dSRodney W. Grimes 	register vm_offset_t start;
721df8bae1dSRodney W. Grimes 	register vm_offset_t end;
722df8bae1dSRodney W. Grimes {
723df8bae1dSRodney W. Grimes 	register vm_page_t p;
724df8bae1dSRodney W. Grimes 
725df8bae1dSRodney W. Grimes 	if (object == NULL)
726df8bae1dSRodney W. Grimes 		return;
727df8bae1dSRodney W. Grimes 
728df8bae1dSRodney W. Grimes 	vm_object_lock(object);
729df8bae1dSRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) {
730df8bae1dSRodney W. Grimes 		if ((start <= p->offset) && (p->offset < end)) {
731df8bae1dSRodney W. Grimes 			pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_READ);
732df8bae1dSRodney W. Grimes 			p->flags |= PG_COPYONWRITE;
733df8bae1dSRodney W. Grimes 		}
734df8bae1dSRodney W. Grimes 	}
735df8bae1dSRodney W. Grimes 	vm_object_unlock(object);
736df8bae1dSRodney W. Grimes }
737df8bae1dSRodney W. Grimes 
738df8bae1dSRodney W. Grimes /*
739df8bae1dSRodney W. Grimes  *	vm_object_pmap_remove:
740df8bae1dSRodney W. Grimes  *
741df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
742df8bae1dSRodney W. Grimes  *	object range from all physical maps.
743df8bae1dSRodney W. Grimes  *
744df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
745df8bae1dSRodney W. Grimes  */
74626f9a767SRodney W. Grimes void
74726f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end)
748df8bae1dSRodney W. Grimes 	register vm_object_t object;
749df8bae1dSRodney W. Grimes 	register vm_offset_t start;
750df8bae1dSRodney W. Grimes 	register vm_offset_t end;
751df8bae1dSRodney W. Grimes {
752df8bae1dSRodney W. Grimes 	register vm_page_t p;
753a481f200SDavid Greenman 	int s;
754df8bae1dSRodney W. Grimes 
755df8bae1dSRodney W. Grimes 	if (object == NULL)
756df8bae1dSRodney W. Grimes 		return;
7572fe6e4d7SDavid Greenman 	++object->paging_in_progress;
758df8bae1dSRodney W. Grimes 
759df8bae1dSRodney W. Grimes 	vm_object_lock(object);
76026f9a767SRodney W. Grimes again:
76126f9a767SRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) {
76226f9a767SRodney W. Grimes 		if ((start <= p->offset) && (p->offset < end)) {
763a481f200SDavid Greenman 			s = splhigh();
7640d94caffSDavid Greenman 			if ((p->flags & PG_BUSY) || p->busy) {
76526f9a767SRodney W. Grimes 				p->flags |= PG_WANTED;
76626f9a767SRodney W. Grimes 				tsleep((caddr_t) p, PVM, "vmopmr", 0);
767a481f200SDavid Greenman 				splx(s);
76826f9a767SRodney W. Grimes 				goto again;
76926f9a767SRodney W. Grimes 			}
770a481f200SDavid Greenman 			splx(s);
771df8bae1dSRodney W. Grimes 			pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
77226f9a767SRodney W. Grimes 		}
77326f9a767SRodney W. Grimes 	}
774df8bae1dSRodney W. Grimes 	vm_object_unlock(object);
7752fe6e4d7SDavid Greenman 	--object->paging_in_progress;
7762fe6e4d7SDavid Greenman 	if (object->paging_in_progress == 0)
7772fe6e4d7SDavid Greenman 		wakeup((caddr_t) object);
778df8bae1dSRodney W. Grimes }
779df8bae1dSRodney W. Grimes 
780df8bae1dSRodney W. Grimes /*
781df8bae1dSRodney W. Grimes  *	vm_object_copy:
782df8bae1dSRodney W. Grimes  *
783df8bae1dSRodney W. Grimes  *	Create a new object which is a copy of an existing
784df8bae1dSRodney W. Grimes  *	object, and mark all of the pages in the existing
785df8bae1dSRodney W. Grimes  *	object 'copy-on-write'.  The new object has one reference.
786df8bae1dSRodney W. Grimes  *	Returns the new object.
787df8bae1dSRodney W. Grimes  *
788df8bae1dSRodney W. Grimes  *	May defer the copy until later if the object is not backed
789df8bae1dSRodney W. Grimes  *	up by a non-default pager.
790df8bae1dSRodney W. Grimes  */
7910d94caffSDavid Greenman void
7920d94caffSDavid Greenman vm_object_copy(src_object, src_offset, size,
793df8bae1dSRodney W. Grimes     dst_object, dst_offset, src_needs_copy)
794df8bae1dSRodney W. Grimes 	register vm_object_t src_object;
795df8bae1dSRodney W. Grimes 	vm_offset_t src_offset;
796df8bae1dSRodney W. Grimes 	vm_size_t size;
797df8bae1dSRodney W. Grimes 	vm_object_t *dst_object;/* OUT */
798df8bae1dSRodney W. Grimes 	vm_offset_t *dst_offset;/* OUT */
799df8bae1dSRodney W. Grimes 	boolean_t *src_needs_copy;	/* OUT */
800df8bae1dSRodney W. Grimes {
801df8bae1dSRodney W. Grimes 	register vm_object_t new_copy;
802df8bae1dSRodney W. Grimes 	register vm_object_t old_copy;
803df8bae1dSRodney W. Grimes 	vm_offset_t new_start, new_end;
804df8bae1dSRodney W. Grimes 
805df8bae1dSRodney W. Grimes 	register vm_page_t p;
806df8bae1dSRodney W. Grimes 
807df8bae1dSRodney W. Grimes 	if (src_object == NULL) {
808df8bae1dSRodney W. Grimes 		/*
809df8bae1dSRodney W. Grimes 		 * Nothing to copy
810df8bae1dSRodney W. Grimes 		 */
811df8bae1dSRodney W. Grimes 		*dst_object = NULL;
812df8bae1dSRodney W. Grimes 		*dst_offset = 0;
813df8bae1dSRodney W. Grimes 		*src_needs_copy = FALSE;
814df8bae1dSRodney W. Grimes 		return;
815df8bae1dSRodney W. Grimes 	}
816df8bae1dSRodney W. Grimes 	/*
8170d94caffSDavid Greenman 	 * If the object's pager is null_pager or the default pager, we don't
8180d94caffSDavid Greenman 	 * have to make a copy of it.  Instead, we set the needs copy flag and
819df8bae1dSRodney W. Grimes 	 * make a shadow later.
820df8bae1dSRodney W. Grimes 	 */
821df8bae1dSRodney W. Grimes 
822df8bae1dSRodney W. Grimes 	vm_object_lock(src_object);
82326f9a767SRodney W. Grimes 
82426f9a767SRodney W. Grimes 	/*
82526f9a767SRodney W. Grimes 	 * Try to collapse the object before copying it.
82626f9a767SRodney W. Grimes 	 */
82726f9a767SRodney W. Grimes 
82826f9a767SRodney W. Grimes 	vm_object_collapse(src_object);
82926f9a767SRodney W. Grimes 
830df8bae1dSRodney W. Grimes 	if (src_object->pager == NULL ||
831df8bae1dSRodney W. Grimes 	    (src_object->flags & OBJ_INTERNAL)) {
832df8bae1dSRodney W. Grimes 
833df8bae1dSRodney W. Grimes 		/*
834df8bae1dSRodney W. Grimes 		 * Make another reference to the object
835df8bae1dSRodney W. Grimes 		 */
836df8bae1dSRodney W. Grimes 		src_object->ref_count++;
837df8bae1dSRodney W. Grimes 
838df8bae1dSRodney W. Grimes 		/*
839df8bae1dSRodney W. Grimes 		 * Mark all of the pages copy-on-write.
840df8bae1dSRodney W. Grimes 		 */
841df8bae1dSRodney W. Grimes 		for (p = src_object->memq.tqh_first; p; p = p->listq.tqe_next)
842df8bae1dSRodney W. Grimes 			if (src_offset <= p->offset &&
843df8bae1dSRodney W. Grimes 			    p->offset < src_offset + size)
844df8bae1dSRodney W. Grimes 				p->flags |= PG_COPYONWRITE;
845df8bae1dSRodney W. Grimes 		vm_object_unlock(src_object);
846df8bae1dSRodney W. Grimes 
847df8bae1dSRodney W. Grimes 		*dst_object = src_object;
848df8bae1dSRodney W. Grimes 		*dst_offset = src_offset;
849df8bae1dSRodney W. Grimes 
850df8bae1dSRodney W. Grimes 		/*
851df8bae1dSRodney W. Grimes 		 * Must make a shadow when write is desired
852df8bae1dSRodney W. Grimes 		 */
853df8bae1dSRodney W. Grimes 		*src_needs_copy = TRUE;
854df8bae1dSRodney W. Grimes 		return;
855df8bae1dSRodney W. Grimes 	}
856df8bae1dSRodney W. Grimes 	/*
8570d94caffSDavid Greenman 	 * If the object has a pager, the pager wants to see all of the
8580d94caffSDavid Greenman 	 * changes.  We need a copy-object for the changed pages.
859df8bae1dSRodney W. Grimes 	 *
8600d94caffSDavid Greenman 	 * If there is a copy-object, and it is empty, no changes have been made
8610d94caffSDavid Greenman 	 * to the object since the copy-object was made.  We can use the same
8620d94caffSDavid Greenman 	 * copy- object.
863df8bae1dSRodney W. Grimes 	 */
864df8bae1dSRodney W. Grimes 
865df8bae1dSRodney W. Grimes Retry1:
866df8bae1dSRodney W. Grimes 	old_copy = src_object->copy;
867df8bae1dSRodney W. Grimes 	if (old_copy != NULL) {
868df8bae1dSRodney W. Grimes 		/*
869df8bae1dSRodney W. Grimes 		 * Try to get the locks (out of order)
870df8bae1dSRodney W. Grimes 		 */
871df8bae1dSRodney W. Grimes 		if (!vm_object_lock_try(old_copy)) {
872df8bae1dSRodney W. Grimes 			vm_object_unlock(src_object);
873df8bae1dSRodney W. Grimes 
874df8bae1dSRodney W. Grimes 			/* should spin a bit here... */
8750d94caffSDavid Greenman 			tsleep((caddr_t) old_copy, PVM, "cpylck", 1);
876df8bae1dSRodney W. Grimes 			vm_object_lock(src_object);
877df8bae1dSRodney W. Grimes 			goto Retry1;
878df8bae1dSRodney W. Grimes 		}
879df8bae1dSRodney W. Grimes 		if (old_copy->resident_page_count == 0 &&
880df8bae1dSRodney W. Grimes 		    old_copy->pager == NULL) {
881df8bae1dSRodney W. Grimes 			/*
8820d94caffSDavid Greenman 			 * Return another reference to the existing
8830d94caffSDavid Greenman 			 * copy-object.
884df8bae1dSRodney W. Grimes 			 */
885df8bae1dSRodney W. Grimes 			old_copy->ref_count++;
886df8bae1dSRodney W. Grimes 			vm_object_unlock(old_copy);
887df8bae1dSRodney W. Grimes 			vm_object_unlock(src_object);
888df8bae1dSRodney W. Grimes 			*dst_object = old_copy;
889df8bae1dSRodney W. Grimes 			*dst_offset = src_offset;
890df8bae1dSRodney W. Grimes 			*src_needs_copy = FALSE;
891df8bae1dSRodney W. Grimes 			return;
892df8bae1dSRodney W. Grimes 		}
893df8bae1dSRodney W. Grimes 		vm_object_unlock(old_copy);
894df8bae1dSRodney W. Grimes 	}
895df8bae1dSRodney W. Grimes 	vm_object_unlock(src_object);
896df8bae1dSRodney W. Grimes 
897df8bae1dSRodney W. Grimes 	/*
8980d94caffSDavid Greenman 	 * If the object has a pager, the pager wants to see all of the
8990d94caffSDavid Greenman 	 * changes.  We must make a copy-object and put the changed pages
9000d94caffSDavid Greenman 	 * there.
901df8bae1dSRodney W. Grimes 	 *
9020d94caffSDavid Greenman 	 * The copy-object is always made large enough to completely shadow the
9030d94caffSDavid Greenman 	 * original object, since it may have several users who want to shadow
904df8bae1dSRodney W. Grimes 	 * the original object at different points.
905df8bae1dSRodney W. Grimes 	 */
906df8bae1dSRodney W. Grimes 
907df8bae1dSRodney W. Grimes 	new_copy = vm_object_allocate(src_object->size);
908df8bae1dSRodney W. Grimes 
909df8bae1dSRodney W. Grimes Retry2:
910df8bae1dSRodney W. Grimes 	vm_object_lock(src_object);
911df8bae1dSRodney W. Grimes 	/*
912df8bae1dSRodney W. Grimes 	 * Copy object may have changed while we were unlocked
913df8bae1dSRodney W. Grimes 	 */
914df8bae1dSRodney W. Grimes 	old_copy = src_object->copy;
915df8bae1dSRodney W. Grimes 	if (old_copy != NULL) {
916df8bae1dSRodney W. Grimes 		/*
917df8bae1dSRodney W. Grimes 		 * Try to get the locks (out of order)
918df8bae1dSRodney W. Grimes 		 */
919df8bae1dSRodney W. Grimes 		if (!vm_object_lock_try(old_copy)) {
920df8bae1dSRodney W. Grimes 			vm_object_unlock(src_object);
9210d94caffSDavid Greenman 			tsleep((caddr_t) old_copy, PVM, "cpylck", 1);
922df8bae1dSRodney W. Grimes 			goto Retry2;
923df8bae1dSRodney W. Grimes 		}
924df8bae1dSRodney W. Grimes 		/*
925df8bae1dSRodney W. Grimes 		 * Consistency check
926df8bae1dSRodney W. Grimes 		 */
927df8bae1dSRodney W. Grimes 		if (old_copy->shadow != src_object ||
928df8bae1dSRodney W. Grimes 		    old_copy->shadow_offset != (vm_offset_t) 0)
929df8bae1dSRodney W. Grimes 			panic("vm_object_copy: copy/shadow inconsistency");
930df8bae1dSRodney W. Grimes 
931df8bae1dSRodney W. Grimes 		/*
9320d94caffSDavid Greenman 		 * Make the old copy-object shadow the new one. It will
9330d94caffSDavid Greenman 		 * receive no more pages from the original object.
934df8bae1dSRodney W. Grimes 		 */
935df8bae1dSRodney W. Grimes 
936df8bae1dSRodney W. Grimes 		src_object->ref_count--;	/* remove ref. from old_copy */
9372fe6e4d7SDavid Greenman 		if (old_copy->shadow)
9382fe6e4d7SDavid Greenman 			TAILQ_REMOVE(&old_copy->shadow->reverse_shadow_head, old_copy, reverse_shadow_list);
939df8bae1dSRodney W. Grimes 		old_copy->shadow = new_copy;
9402fe6e4d7SDavid Greenman 		TAILQ_INSERT_TAIL(&old_copy->shadow->reverse_shadow_head, old_copy, reverse_shadow_list);
9410d94caffSDavid Greenman 		new_copy->ref_count++;	/* locking not needed - we have the
9420d94caffSDavid Greenman 					 * only pointer */
943df8bae1dSRodney W. Grimes 		vm_object_unlock(old_copy);	/* done with old_copy */
944df8bae1dSRodney W. Grimes 	}
945df8bae1dSRodney W. Grimes 	new_start = (vm_offset_t) 0;	/* always shadow original at 0 */
946df8bae1dSRodney W. Grimes 	new_end = (vm_offset_t) new_copy->size;	/* for the whole object */
947df8bae1dSRodney W. Grimes 
948df8bae1dSRodney W. Grimes 	/*
949df8bae1dSRodney W. Grimes 	 * Point the new copy at the existing object.
950df8bae1dSRodney W. Grimes 	 */
951df8bae1dSRodney W. Grimes 
952df8bae1dSRodney W. Grimes 	new_copy->shadow = src_object;
9532fe6e4d7SDavid Greenman 	TAILQ_INSERT_TAIL(&new_copy->shadow->reverse_shadow_head, new_copy, reverse_shadow_list);
954df8bae1dSRodney W. Grimes 	new_copy->shadow_offset = new_start;
955df8bae1dSRodney W. Grimes 	src_object->ref_count++;
956df8bae1dSRodney W. Grimes 	src_object->copy = new_copy;
957df8bae1dSRodney W. Grimes 
958df8bae1dSRodney W. Grimes 	/*
9590d94caffSDavid Greenman 	 * Mark all the affected pages of the existing object copy-on-write.
960df8bae1dSRodney W. Grimes 	 */
961df8bae1dSRodney W. Grimes 	for (p = src_object->memq.tqh_first; p != NULL; p = p->listq.tqe_next)
962df8bae1dSRodney W. Grimes 		if ((new_start <= p->offset) && (p->offset < new_end))
963df8bae1dSRodney W. Grimes 			p->flags |= PG_COPYONWRITE;
964df8bae1dSRodney W. Grimes 
965df8bae1dSRodney W. Grimes 	vm_object_unlock(src_object);
966df8bae1dSRodney W. Grimes 
967df8bae1dSRodney W. Grimes 	*dst_object = new_copy;
968df8bae1dSRodney W. Grimes 	*dst_offset = src_offset - new_start;
969df8bae1dSRodney W. Grimes 	*src_needs_copy = FALSE;
970df8bae1dSRodney W. Grimes }
971df8bae1dSRodney W. Grimes 
972df8bae1dSRodney W. Grimes /*
973df8bae1dSRodney W. Grimes  *	vm_object_shadow:
974df8bae1dSRodney W. Grimes  *
975df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
976df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
977df8bae1dSRodney W. Grimes  *	object reference is deallocated.
978df8bae1dSRodney W. Grimes  *
979df8bae1dSRodney W. Grimes  *	The new object and offset into that object
980df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
981df8bae1dSRodney W. Grimes  */
982df8bae1dSRodney W. Grimes 
98326f9a767SRodney W. Grimes void
98426f9a767SRodney W. Grimes vm_object_shadow(object, offset, length)
985df8bae1dSRodney W. Grimes 	vm_object_t *object;	/* IN/OUT */
986df8bae1dSRodney W. Grimes 	vm_offset_t *offset;	/* IN/OUT */
987df8bae1dSRodney W. Grimes 	vm_size_t length;
988df8bae1dSRodney W. Grimes {
989df8bae1dSRodney W. Grimes 	register vm_object_t source;
990df8bae1dSRodney W. Grimes 	register vm_object_t result;
991df8bae1dSRodney W. Grimes 
992df8bae1dSRodney W. Grimes 	source = *object;
993df8bae1dSRodney W. Grimes 
994df8bae1dSRodney W. Grimes 	/*
995df8bae1dSRodney W. Grimes 	 * Allocate a new object with the given length
996df8bae1dSRodney W. Grimes 	 */
997df8bae1dSRodney W. Grimes 
998df8bae1dSRodney W. Grimes 	if ((result = vm_object_allocate(length)) == NULL)
999df8bae1dSRodney W. Grimes 		panic("vm_object_shadow: no object for shadowing");
1000df8bae1dSRodney W. Grimes 
1001df8bae1dSRodney W. Grimes 	/*
10020d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
10030d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
10040d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
10050d94caffSDavid Greenman 	 * of reference count.
1006df8bae1dSRodney W. Grimes 	 */
1007df8bae1dSRodney W. Grimes 	result->shadow = source;
10087b18a718SDavid Greenman 	if (source)
10092fe6e4d7SDavid Greenman 		TAILQ_INSERT_TAIL(&result->shadow->reverse_shadow_head, result, reverse_shadow_list);
1010df8bae1dSRodney W. Grimes 
1011df8bae1dSRodney W. Grimes 	/*
10120d94caffSDavid Greenman 	 * Store the offset into the source object, and fix up the offset into
10130d94caffSDavid Greenman 	 * the new object.
1014df8bae1dSRodney W. Grimes 	 */
1015df8bae1dSRodney W. Grimes 
1016df8bae1dSRodney W. Grimes 	result->shadow_offset = *offset;
1017df8bae1dSRodney W. Grimes 
1018df8bae1dSRodney W. Grimes 	/*
1019df8bae1dSRodney W. Grimes 	 * Return the new things
1020df8bae1dSRodney W. Grimes 	 */
1021df8bae1dSRodney W. Grimes 
1022df8bae1dSRodney W. Grimes 	*offset = 0;
1023df8bae1dSRodney W. Grimes 	*object = result;
1024df8bae1dSRodney W. Grimes }
1025df8bae1dSRodney W. Grimes 
1026df8bae1dSRodney W. Grimes /*
1027df8bae1dSRodney W. Grimes  *	Set the specified object's pager to the specified pager.
1028df8bae1dSRodney W. Grimes  */
1029df8bae1dSRodney W. Grimes 
103026f9a767SRodney W. Grimes void
103126f9a767SRodney W. Grimes vm_object_setpager(object, pager, paging_offset,
1032df8bae1dSRodney W. Grimes     read_only)
1033df8bae1dSRodney W. Grimes 	vm_object_t object;
1034df8bae1dSRodney W. Grimes 	vm_pager_t pager;
1035df8bae1dSRodney W. Grimes 	vm_offset_t paging_offset;
1036df8bae1dSRodney W. Grimes 	boolean_t read_only;
1037df8bae1dSRodney W. Grimes {
1038df8bae1dSRodney W. Grimes 	vm_object_lock(object);	/* XXX ? */
103926f9a767SRodney W. Grimes 	if (object->pager && object->pager != pager) {
104026f9a767SRodney W. Grimes 		panic("!!!pager already allocated!!!\n");
104126f9a767SRodney W. Grimes 	}
1042df8bae1dSRodney W. Grimes 	object->pager = pager;
1043df8bae1dSRodney W. Grimes 	object->paging_offset = paging_offset;
1044df8bae1dSRodney W. Grimes 	vm_object_unlock(object);	/* XXX ? */
1045df8bae1dSRodney W. Grimes }
1046df8bae1dSRodney W. Grimes 
1047df8bae1dSRodney W. Grimes /*
1048df8bae1dSRodney W. Grimes  *	vm_object_hash hashes the pager/id pair.
1049df8bae1dSRodney W. Grimes  */
1050df8bae1dSRodney W. Grimes 
1051df8bae1dSRodney W. Grimes #define vm_object_hash(pager) \
105226f9a767SRodney W. Grimes 	(((unsigned)pager >> 5)%VM_OBJECT_HASH_COUNT)
1053df8bae1dSRodney W. Grimes 
1054df8bae1dSRodney W. Grimes /*
1055df8bae1dSRodney W. Grimes  *	vm_object_lookup looks in the object cache for an object with the
1056df8bae1dSRodney W. Grimes  *	specified pager and paging id.
1057df8bae1dSRodney W. Grimes  */
1058df8bae1dSRodney W. Grimes 
10590d94caffSDavid Greenman vm_object_t
10600d94caffSDavid Greenman vm_object_lookup(pager)
1061df8bae1dSRodney W. Grimes 	vm_pager_t pager;
1062df8bae1dSRodney W. Grimes {
1063df8bae1dSRodney W. Grimes 	register vm_object_hash_entry_t entry;
1064df8bae1dSRodney W. Grimes 	vm_object_t object;
1065df8bae1dSRodney W. Grimes 
1066a08a17a3SDavid Greenman 	cnt.v_lookups++;
1067df8bae1dSRodney W. Grimes 	vm_object_cache_lock();
1068df8bae1dSRodney W. Grimes 
1069df8bae1dSRodney W. Grimes 	for (entry = vm_object_hashtable[vm_object_hash(pager)].tqh_first;
1070df8bae1dSRodney W. Grimes 	    entry != NULL;
1071df8bae1dSRodney W. Grimes 	    entry = entry->hash_links.tqe_next) {
1072df8bae1dSRodney W. Grimes 		object = entry->object;
1073df8bae1dSRodney W. Grimes 		if (object->pager == pager) {
1074df8bae1dSRodney W. Grimes 			vm_object_lock(object);
1075df8bae1dSRodney W. Grimes 			if (object->ref_count == 0) {
1076df8bae1dSRodney W. Grimes 				TAILQ_REMOVE(&vm_object_cached_list, object,
1077df8bae1dSRodney W. Grimes 				    cached_list);
1078df8bae1dSRodney W. Grimes 				vm_object_cached--;
1079df8bae1dSRodney W. Grimes 			}
1080df8bae1dSRodney W. Grimes 			object->ref_count++;
1081df8bae1dSRodney W. Grimes 			vm_object_unlock(object);
1082df8bae1dSRodney W. Grimes 			vm_object_cache_unlock();
1083a08a17a3SDavid Greenman 			cnt.v_hits++;
1084df8bae1dSRodney W. Grimes 			return (object);
1085df8bae1dSRodney W. Grimes 		}
1086df8bae1dSRodney W. Grimes 	}
1087df8bae1dSRodney W. Grimes 
1088df8bae1dSRodney W. Grimes 	vm_object_cache_unlock();
1089df8bae1dSRodney W. Grimes 	return (NULL);
1090df8bae1dSRodney W. Grimes }
1091df8bae1dSRodney W. Grimes 
1092df8bae1dSRodney W. Grimes /*
1093df8bae1dSRodney W. Grimes  *	vm_object_enter enters the specified object/pager/id into
1094df8bae1dSRodney W. Grimes  *	the hash table.
1095df8bae1dSRodney W. Grimes  */
1096df8bae1dSRodney W. Grimes 
10970d94caffSDavid Greenman void
10980d94caffSDavid Greenman vm_object_enter(object, pager)
1099df8bae1dSRodney W. Grimes 	vm_object_t object;
1100df8bae1dSRodney W. Grimes 	vm_pager_t pager;
1101df8bae1dSRodney W. Grimes {
1102df8bae1dSRodney W. Grimes 	struct vm_object_hash_head *bucket;
1103df8bae1dSRodney W. Grimes 	register vm_object_hash_entry_t entry;
1104df8bae1dSRodney W. Grimes 
1105df8bae1dSRodney W. Grimes 	/*
11060d94caffSDavid Greenman 	 * We don't cache null objects, and we can't cache objects with the
11070d94caffSDavid Greenman 	 * null pager.
1108df8bae1dSRodney W. Grimes 	 */
1109df8bae1dSRodney W. Grimes 
1110df8bae1dSRodney W. Grimes 	if (object == NULL)
1111df8bae1dSRodney W. Grimes 		return;
1112df8bae1dSRodney W. Grimes 	if (pager == NULL)
1113df8bae1dSRodney W. Grimes 		return;
1114df8bae1dSRodney W. Grimes 
1115df8bae1dSRodney W. Grimes 	bucket = &vm_object_hashtable[vm_object_hash(pager)];
1116df8bae1dSRodney W. Grimes 	entry = (vm_object_hash_entry_t)
1117df8bae1dSRodney W. Grimes 	    malloc((u_long) sizeof *entry, M_VMOBJHASH, M_WAITOK);
1118df8bae1dSRodney W. Grimes 	entry->object = object;
1119df8bae1dSRodney W. Grimes 	object->flags |= OBJ_CANPERSIST;
1120df8bae1dSRodney W. Grimes 
1121df8bae1dSRodney W. Grimes 	vm_object_cache_lock();
1122df8bae1dSRodney W. Grimes 	TAILQ_INSERT_TAIL(bucket, entry, hash_links);
1123df8bae1dSRodney W. Grimes 	vm_object_cache_unlock();
1124df8bae1dSRodney W. Grimes }
1125df8bae1dSRodney W. Grimes 
1126df8bae1dSRodney W. Grimes /*
1127df8bae1dSRodney W. Grimes  *	vm_object_remove:
1128df8bae1dSRodney W. Grimes  *
1129df8bae1dSRodney W. Grimes  *	Remove the pager from the hash table.
1130df8bae1dSRodney W. Grimes  *	Note:  This assumes that the object cache
1131df8bae1dSRodney W. Grimes  *	is locked.  XXX this should be fixed
1132df8bae1dSRodney W. Grimes  *	by reorganizing vm_object_deallocate.
1133df8bae1dSRodney W. Grimes  */
1134df8bae1dSRodney W. Grimes void
1135df8bae1dSRodney W. Grimes vm_object_remove(pager)
1136df8bae1dSRodney W. Grimes 	register vm_pager_t pager;
1137df8bae1dSRodney W. Grimes {
1138df8bae1dSRodney W. Grimes 	struct vm_object_hash_head *bucket;
1139df8bae1dSRodney W. Grimes 	register vm_object_hash_entry_t entry;
1140df8bae1dSRodney W. Grimes 	register vm_object_t object;
1141df8bae1dSRodney W. Grimes 
1142df8bae1dSRodney W. Grimes 	bucket = &vm_object_hashtable[vm_object_hash(pager)];
1143df8bae1dSRodney W. Grimes 
1144df8bae1dSRodney W. Grimes 	for (entry = bucket->tqh_first;
1145df8bae1dSRodney W. Grimes 	    entry != NULL;
1146df8bae1dSRodney W. Grimes 	    entry = entry->hash_links.tqe_next) {
1147df8bae1dSRodney W. Grimes 		object = entry->object;
1148df8bae1dSRodney W. Grimes 		if (object->pager == pager) {
1149df8bae1dSRodney W. Grimes 			TAILQ_REMOVE(bucket, entry, hash_links);
1150df8bae1dSRodney W. Grimes 			free((caddr_t) entry, M_VMOBJHASH);
1151df8bae1dSRodney W. Grimes 			break;
1152df8bae1dSRodney W. Grimes 		}
1153df8bae1dSRodney W. Grimes 	}
1154df8bae1dSRodney W. Grimes }
1155df8bae1dSRodney W. Grimes 
11562fe6e4d7SDavid Greenman /*
11572fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
11582fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
11592fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
11602fe6e4d7SDavid Greenman  */
11612fe6e4d7SDavid Greenman static void
11622fe6e4d7SDavid Greenman vm_object_qcollapse(object)
11632fe6e4d7SDavid Greenman 	register vm_object_t object;
11642fe6e4d7SDavid Greenman {
11652fe6e4d7SDavid Greenman 	register vm_object_t backing_object;
11662fe6e4d7SDavid Greenman 	register vm_offset_t backing_offset, new_offset;
11672fe6e4d7SDavid Greenman 	register vm_page_t p, pp;
11682fe6e4d7SDavid Greenman 	register vm_size_t size;
11692fe6e4d7SDavid Greenman 
11702fe6e4d7SDavid Greenman 	backing_object = object->shadow;
11712fe6e4d7SDavid Greenman 	if (!backing_object)
11722fe6e4d7SDavid Greenman 		return;
11732fe6e4d7SDavid Greenman 	if ((backing_object->flags & OBJ_INTERNAL) == 0)
11742fe6e4d7SDavid Greenman 		return;
11752fe6e4d7SDavid Greenman 	if (backing_object->shadow != NULL &&
11762fe6e4d7SDavid Greenman 	    backing_object->shadow->copy == backing_object)
11772fe6e4d7SDavid Greenman 		return;
11782fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
11792fe6e4d7SDavid Greenman 		return;
11802fe6e4d7SDavid Greenman 
1181010cf3b9SDavid Greenman 	backing_object->ref_count += 2;
1182010cf3b9SDavid Greenman 
11832fe6e4d7SDavid Greenman 	backing_offset = object->shadow_offset;
11842fe6e4d7SDavid Greenman 	size = object->size;
11852fe6e4d7SDavid Greenman 	p = backing_object->memq.tqh_first;
11862fe6e4d7SDavid Greenman 	while (p) {
11872fe6e4d7SDavid Greenman 		vm_page_t next;
11880d94caffSDavid Greenman 
11892fe6e4d7SDavid Greenman 		next = p->listq.tqe_next;
11900d94caffSDavid Greenman 		if ((p->flags & (PG_BUSY | PG_FICTITIOUS | PG_CACHE)) ||
11910d94caffSDavid Greenman 		    !p->valid || p->hold_count || p->wire_count || p->busy || p->bmapped) {
11922fe6e4d7SDavid Greenman 			p = next;
11932fe6e4d7SDavid Greenman 			continue;
11942fe6e4d7SDavid Greenman 		}
11950d94caffSDavid Greenman 		pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
11962fe6e4d7SDavid Greenman 		new_offset = (p->offset - backing_offset);
11972fe6e4d7SDavid Greenman 		if (p->offset < backing_offset ||
11982fe6e4d7SDavid Greenman 		    new_offset >= size) {
11992fe6e4d7SDavid Greenman 			if (backing_object->pager)
12000d94caffSDavid Greenman 				swap_pager_freespace(backing_object->pager,
12010d94caffSDavid Greenman 				    backing_object->paging_offset + p->offset, PAGE_SIZE);
12020d94caffSDavid Greenman 			vm_page_lock_queues();
12032fe6e4d7SDavid Greenman 			vm_page_free(p);
12042fe6e4d7SDavid Greenman 			vm_page_unlock_queues();
12052fe6e4d7SDavid Greenman 		} else {
12062fe6e4d7SDavid Greenman 			pp = vm_page_lookup(object, new_offset);
12072fe6e4d7SDavid Greenman 			if (pp != NULL || (object->pager && vm_pager_has_page(object->pager,
12082fe6e4d7SDavid Greenman 				    object->paging_offset + new_offset))) {
12092fe6e4d7SDavid Greenman 				if (backing_object->pager)
12100d94caffSDavid Greenman 					swap_pager_freespace(backing_object->pager,
12110d94caffSDavid Greenman 					    backing_object->paging_offset + p->offset, PAGE_SIZE);
12120d94caffSDavid Greenman 				vm_page_lock_queues();
12132fe6e4d7SDavid Greenman 				vm_page_free(p);
12142fe6e4d7SDavid Greenman 				vm_page_unlock_queues();
12152fe6e4d7SDavid Greenman 			} else {
1216a1f6d91cSDavid Greenman 				if( backing_object->pager)
1217a1f6d91cSDavid Greenman 					swap_pager_freespace(backing_object->pager,
1218a1f6d91cSDavid Greenman 					    backing_object->paging_offset + p->offset, PAGE_SIZE);
12192fe6e4d7SDavid Greenman 				vm_page_rename(p, object, new_offset);
12202fe6e4d7SDavid Greenman 			}
12212fe6e4d7SDavid Greenman 		}
12222fe6e4d7SDavid Greenman 		p = next;
12232fe6e4d7SDavid Greenman 	}
1224010cf3b9SDavid Greenman 	backing_object->ref_count -= 2;
12252fe6e4d7SDavid Greenman }
12262fe6e4d7SDavid Greenman 
1227df8bae1dSRodney W. Grimes boolean_t vm_object_collapse_allowed = TRUE;
12280d94caffSDavid Greenman 
1229df8bae1dSRodney W. Grimes /*
1230df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1231df8bae1dSRodney W. Grimes  *
1232df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1233df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1234df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1235df8bae1dSRodney W. Grimes  *
1236df8bae1dSRodney W. Grimes  *	Requires that the object be locked and the page
1237df8bae1dSRodney W. Grimes  *	queues be unlocked.
1238df8bae1dSRodney W. Grimes  *
123926f9a767SRodney W. Grimes  *	This routine has significant changes by John S. Dyson
124026f9a767SRodney W. Grimes  *	to fix some swap memory leaks.  18 Dec 93
124126f9a767SRodney W. Grimes  *
1242df8bae1dSRodney W. Grimes  */
124326f9a767SRodney W. Grimes void
124426f9a767SRodney W. Grimes vm_object_collapse(object)
1245df8bae1dSRodney W. Grimes 	register vm_object_t object;
1246df8bae1dSRodney W. Grimes 
1247df8bae1dSRodney W. Grimes {
1248df8bae1dSRodney W. Grimes 	register vm_object_t backing_object;
1249df8bae1dSRodney W. Grimes 	register vm_offset_t backing_offset;
1250df8bae1dSRodney W. Grimes 	register vm_size_t size;
1251df8bae1dSRodney W. Grimes 	register vm_offset_t new_offset;
1252df8bae1dSRodney W. Grimes 	register vm_page_t p, pp;
1253df8bae1dSRodney W. Grimes 
1254df8bae1dSRodney W. Grimes 	if (!vm_object_collapse_allowed)
1255df8bae1dSRodney W. Grimes 		return;
1256df8bae1dSRodney W. Grimes 
1257df8bae1dSRodney W. Grimes 	while (TRUE) {
1258df8bae1dSRodney W. Grimes 		/*
1259df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1260df8bae1dSRodney W. Grimes 		 *
12610d94caffSDavid Greenman 		 * The object exists and no pages in it are currently being paged
12620d94caffSDavid Greenman 		 * out.
1263df8bae1dSRodney W. Grimes 		 */
12642fe6e4d7SDavid Greenman 		if (object == NULL)
1265df8bae1dSRodney W. Grimes 			return;
1266df8bae1dSRodney W. Grimes 
1267b9921222SDavid Greenman 		/*
1268b9921222SDavid Greenman 		 * Make sure there is a backing object.
1269b9921222SDavid Greenman 		 */
1270df8bae1dSRodney W. Grimes 		if ((backing_object = object->shadow) == NULL)
1271df8bae1dSRodney W. Grimes 			return;
1272df8bae1dSRodney W. Grimes 
1273b9921222SDavid Greenman 		if (object->paging_in_progress != 0) {
1274b9921222SDavid Greenman 			if (backing_object) {
1275b9921222SDavid Greenman 				if (vm_object_lock_try(backing_object)) {
1276b9921222SDavid Greenman 					vm_object_qcollapse(object);
1277b9921222SDavid Greenman 					vm_object_unlock(backing_object);
1278b9921222SDavid Greenman 				}
1279b9921222SDavid Greenman 			}
1280b9921222SDavid Greenman 			return;
1281b9921222SDavid Greenman 		}
1282b9921222SDavid Greenman 
1283df8bae1dSRodney W. Grimes 		vm_object_lock(backing_object);
1284df8bae1dSRodney W. Grimes 		/*
12850d94caffSDavid Greenman 		 * ... The backing object is not read_only, and no pages in
12860d94caffSDavid Greenman 		 * the backing object are currently being paged out. The
12870d94caffSDavid Greenman 		 * backing object is internal.
1288df8bae1dSRodney W. Grimes 		 */
1289df8bae1dSRodney W. Grimes 
1290df8bae1dSRodney W. Grimes 		if ((backing_object->flags & OBJ_INTERNAL) == 0 ||
1291df8bae1dSRodney W. Grimes 		    backing_object->paging_in_progress != 0) {
12922fe6e4d7SDavid Greenman 			vm_object_qcollapse(object);
1293a1f6d91cSDavid Greenman 			vm_object_unlock(backing_object);
1294df8bae1dSRodney W. Grimes 			return;
1295df8bae1dSRodney W. Grimes 		}
1296df8bae1dSRodney W. Grimes 		/*
12970d94caffSDavid Greenman 		 * The backing object can't be a copy-object: the
12980d94caffSDavid Greenman 		 * shadow_offset for the copy-object must stay as 0.
12990d94caffSDavid Greenman 		 * Furthermore (for the 'we have all the pages' case), if we
13000d94caffSDavid Greenman 		 * bypass backing_object and just shadow the next object in
13010d94caffSDavid Greenman 		 * the chain, old pages from that object would then have to be
13020d94caffSDavid Greenman 		 * copied BOTH into the (former) backing_object and into the
1303df8bae1dSRodney W. Grimes 		 * parent object.
1304df8bae1dSRodney W. Grimes 		 */
1305df8bae1dSRodney W. Grimes 		if (backing_object->shadow != NULL &&
130626f9a767SRodney W. Grimes 		    backing_object->shadow->copy == backing_object) {
1307df8bae1dSRodney W. Grimes 			vm_object_unlock(backing_object);
1308df8bae1dSRodney W. Grimes 			return;
1309df8bae1dSRodney W. Grimes 		}
1310df8bae1dSRodney W. Grimes 		/*
131126f9a767SRodney W. Grimes 		 * we can deal only with the swap pager
131226f9a767SRodney W. Grimes 		 */
131326f9a767SRodney W. Grimes 		if ((object->pager &&
131426f9a767SRodney W. Grimes 			object->pager->pg_type != PG_SWAP) ||
131526f9a767SRodney W. Grimes 		    (backing_object->pager &&
131626f9a767SRodney W. Grimes 			backing_object->pager->pg_type != PG_SWAP)) {
131726f9a767SRodney W. Grimes 			vm_object_unlock(backing_object);
131826f9a767SRodney W. Grimes 			return;
131926f9a767SRodney W. Grimes 		}
132026f9a767SRodney W. Grimes 		/*
13210d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
13220d94caffSDavid Greenman 		 * the parent is the only reference to it) or (perhaps) remove
13230d94caffSDavid Greenman 		 * the parent's reference to it.
1324df8bae1dSRodney W. Grimes 		 */
1325df8bae1dSRodney W. Grimes 
1326df8bae1dSRodney W. Grimes 		backing_offset = object->shadow_offset;
1327df8bae1dSRodney W. Grimes 		size = object->size;
1328df8bae1dSRodney W. Grimes 
1329df8bae1dSRodney W. Grimes 		/*
13300d94caffSDavid Greenman 		 * If there is exactly one reference to the backing object, we
13310d94caffSDavid Greenman 		 * can collapse it into the parent.
1332df8bae1dSRodney W. Grimes 		 */
1333df8bae1dSRodney W. Grimes 
1334df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1335df8bae1dSRodney W. Grimes 
1336a1f6d91cSDavid Greenman 			backing_object->flags |= OBJ_DEAD;
1337df8bae1dSRodney W. Grimes 			/*
1338df8bae1dSRodney W. Grimes 			 * We can collapse the backing object.
1339df8bae1dSRodney W. Grimes 			 *
13400d94caffSDavid Greenman 			 * Move all in-memory pages from backing_object to the
13410d94caffSDavid Greenman 			 * parent.  Pages that have been paged out will be
13420d94caffSDavid Greenman 			 * overwritten by any of the parent's pages that
13430d94caffSDavid Greenman 			 * shadow them.
1344df8bae1dSRodney W. Grimes 			 */
1345df8bae1dSRodney W. Grimes 
134605f0fdd2SPoul-Henning Kamp 			while ((p = backing_object->memq.tqh_first) != 0) {
134726f9a767SRodney W. Grimes 
1348df8bae1dSRodney W. Grimes 				new_offset = (p->offset - backing_offset);
1349df8bae1dSRodney W. Grimes 
1350df8bae1dSRodney W. Grimes 				/*
13510d94caffSDavid Greenman 				 * If the parent has a page here, or if this
13520d94caffSDavid Greenman 				 * page falls outside the parent, dispose of
13530d94caffSDavid Greenman 				 * it.
1354df8bae1dSRodney W. Grimes 				 *
1355df8bae1dSRodney W. Grimes 				 * Otherwise, move it as planned.
1356df8bae1dSRodney W. Grimes 				 */
1357df8bae1dSRodney W. Grimes 
1358df8bae1dSRodney W. Grimes 				if (p->offset < backing_offset ||
1359df8bae1dSRodney W. Grimes 				    new_offset >= size) {
1360df8bae1dSRodney W. Grimes 					vm_page_lock_queues();
13612fe6e4d7SDavid Greenman 					pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
13620d94caffSDavid Greenman 					PAGE_WAKEUP(p);
1363df8bae1dSRodney W. Grimes 					vm_page_free(p);
1364df8bae1dSRodney W. Grimes 					vm_page_unlock_queues();
1365df8bae1dSRodney W. Grimes 				} else {
1366df8bae1dSRodney W. Grimes 					pp = vm_page_lookup(object, new_offset);
136726f9a767SRodney W. Grimes 					if (pp != NULL || (object->pager && vm_pager_has_page(object->pager,
136826f9a767SRodney W. Grimes 					    object->paging_offset + new_offset))) {
1369df8bae1dSRodney W. Grimes 						vm_page_lock_queues();
13702fe6e4d7SDavid Greenman 						pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
13710d94caffSDavid Greenman 						PAGE_WAKEUP(p);
1372df8bae1dSRodney W. Grimes 						vm_page_free(p);
1373df8bae1dSRodney W. Grimes 						vm_page_unlock_queues();
137426f9a767SRodney W. Grimes 					} else {
1375df8bae1dSRodney W. Grimes 						vm_page_rename(p, object, new_offset);
1376df8bae1dSRodney W. Grimes 					}
1377df8bae1dSRodney W. Grimes 				}
1378df8bae1dSRodney W. Grimes 			}
1379df8bae1dSRodney W. Grimes 
1380df8bae1dSRodney W. Grimes 			/*
1381df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1382df8bae1dSRodney W. Grimes 			 */
1383df8bae1dSRodney W. Grimes 
1384df8bae1dSRodney W. Grimes 			if (backing_object->pager) {
138526f9a767SRodney W. Grimes 				backing_object->paging_in_progress++;
138626f9a767SRodney W. Grimes 				if (object->pager) {
138726f9a767SRodney W. Grimes 					vm_pager_t bopager;
13880d94caffSDavid Greenman 
138926f9a767SRodney W. Grimes 					object->paging_in_progress++;
139026f9a767SRodney W. Grimes 					/*
139126f9a767SRodney W. Grimes 					 * copy shadow object pages into ours
13920d94caffSDavid Greenman 					 * and destroy unneeded pages in
13930d94caffSDavid Greenman 					 * shadow object.
139426f9a767SRodney W. Grimes 					 */
139526f9a767SRodney W. Grimes 					bopager = backing_object->pager;
1396df8bae1dSRodney W. Grimes 					backing_object->pager = NULL;
139726f9a767SRodney W. Grimes 					vm_object_remove(backing_object->pager);
139826f9a767SRodney W. Grimes 					swap_pager_copy(
139926f9a767SRodney W. Grimes 					    bopager, backing_object->paging_offset,
140026f9a767SRodney W. Grimes 					    object->pager, object->paging_offset,
140126f9a767SRodney W. Grimes 					    object->shadow_offset);
140226f9a767SRodney W. Grimes 					object->paging_in_progress--;
140326f9a767SRodney W. Grimes 					if (object->paging_in_progress == 0)
140426f9a767SRodney W. Grimes 						wakeup((caddr_t) object);
140526f9a767SRodney W. Grimes 				} else {
140626f9a767SRodney W. Grimes 					object->paging_in_progress++;
140726f9a767SRodney W. Grimes 					/*
140826f9a767SRodney W. Grimes 					 * grab the shadow objects pager
140926f9a767SRodney W. Grimes 					 */
141026f9a767SRodney W. Grimes 					object->pager = backing_object->pager;
141126f9a767SRodney W. Grimes 					object->paging_offset = backing_object->paging_offset + backing_offset;
141226f9a767SRodney W. Grimes 					vm_object_remove(backing_object->pager);
141326f9a767SRodney W. Grimes 					backing_object->pager = NULL;
141426f9a767SRodney W. Grimes 					/*
141526f9a767SRodney W. Grimes 					 * free unnecessary blocks
141626f9a767SRodney W. Grimes 					 */
141726f9a767SRodney W. Grimes 					swap_pager_freespace(object->pager, 0, object->paging_offset);
141826f9a767SRodney W. Grimes 					object->paging_in_progress--;
141926f9a767SRodney W. Grimes 					if (object->paging_in_progress == 0)
142026f9a767SRodney W. Grimes 						wakeup((caddr_t) object);
1421df8bae1dSRodney W. Grimes 				}
142226f9a767SRodney W. Grimes 				backing_object->paging_in_progress--;
142326f9a767SRodney W. Grimes 				if (backing_object->paging_in_progress == 0)
142426f9a767SRodney W. Grimes 					wakeup((caddr_t) backing_object);
142526f9a767SRodney W. Grimes 			}
1426df8bae1dSRodney W. Grimes 			/*
1427df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
1428df8bae1dSRodney W. Grimes 			 * Note that the reference to backing_object->shadow
1429df8bae1dSRodney W. Grimes 			 * moves from within backing_object to within object.
1430df8bae1dSRodney W. Grimes 			 */
1431df8bae1dSRodney W. Grimes 
14320d94caffSDavid Greenman 			TAILQ_REMOVE(&object->shadow->reverse_shadow_head, object,
14330d94caffSDavid Greenman 			    reverse_shadow_list);
14342fe6e4d7SDavid Greenman 			if (backing_object->shadow)
14350d94caffSDavid Greenman 				TAILQ_REMOVE(&backing_object->shadow->reverse_shadow_head,
14360d94caffSDavid Greenman 				    backing_object, reverse_shadow_list);
1437df8bae1dSRodney W. Grimes 			object->shadow = backing_object->shadow;
14382fe6e4d7SDavid Greenman 			if (object->shadow)
14390d94caffSDavid Greenman 				TAILQ_INSERT_TAIL(&object->shadow->reverse_shadow_head,
14400d94caffSDavid Greenman 				    object, reverse_shadow_list);
14412fe6e4d7SDavid Greenman 
1442df8bae1dSRodney W. Grimes 			object->shadow_offset += backing_object->shadow_offset;
1443df8bae1dSRodney W. Grimes 			if (object->shadow != NULL &&
1444df8bae1dSRodney W. Grimes 			    object->shadow->copy != NULL) {
1445df8bae1dSRodney W. Grimes 				panic("vm_object_collapse: we collapsed a copy-object!");
1446df8bae1dSRodney W. Grimes 			}
1447df8bae1dSRodney W. Grimes 			/*
1448df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1449df8bae1dSRodney W. Grimes 			 *
14500d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
14510d94caffSDavid Greenman 			 * and no object references within it, all that is
14520d94caffSDavid Greenman 			 * necessary is to dispose of it.
1453df8bae1dSRodney W. Grimes 			 */
1454df8bae1dSRodney W. Grimes 
1455df8bae1dSRodney W. Grimes 			vm_object_unlock(backing_object);
1456df8bae1dSRodney W. Grimes 
1457df8bae1dSRodney W. Grimes 			simple_lock(&vm_object_list_lock);
1458df8bae1dSRodney W. Grimes 			TAILQ_REMOVE(&vm_object_list, backing_object,
1459df8bae1dSRodney W. Grimes 			    object_list);
1460df8bae1dSRodney W. Grimes 			vm_object_count--;
1461df8bae1dSRodney W. Grimes 			simple_unlock(&vm_object_list_lock);
1462df8bae1dSRodney W. Grimes 
1463df8bae1dSRodney W. Grimes 			free((caddr_t) backing_object, M_VMOBJ);
1464df8bae1dSRodney W. Grimes 
1465df8bae1dSRodney W. Grimes 			object_collapses++;
14660d94caffSDavid Greenman 		} else {
1467df8bae1dSRodney W. Grimes 			/*
1468df8bae1dSRodney W. Grimes 			 * If all of the pages in the backing object are
14690d94caffSDavid Greenman 			 * shadowed by the parent object, the parent object no
14700d94caffSDavid Greenman 			 * longer has to shadow the backing object; it can
14710d94caffSDavid Greenman 			 * shadow the next one in the chain.
1472df8bae1dSRodney W. Grimes 			 *
14730d94caffSDavid Greenman 			 * The backing object must not be paged out - we'd have
14740d94caffSDavid Greenman 			 * to check all of the paged-out pages, as well.
1475df8bae1dSRodney W. Grimes 			 */
1476df8bae1dSRodney W. Grimes 
1477df8bae1dSRodney W. Grimes 			if (backing_object->pager != NULL) {
1478df8bae1dSRodney W. Grimes 				vm_object_unlock(backing_object);
1479df8bae1dSRodney W. Grimes 				return;
1480df8bae1dSRodney W. Grimes 			}
1481df8bae1dSRodney W. Grimes 			/*
14820d94caffSDavid Greenman 			 * Should have a check for a 'small' number of pages
14830d94caffSDavid Greenman 			 * here.
1484df8bae1dSRodney W. Grimes 			 */
1485df8bae1dSRodney W. Grimes 
148626f9a767SRodney W. Grimes 			for (p = backing_object->memq.tqh_first; p; p = p->listq.tqe_next) {
1487df8bae1dSRodney W. Grimes 				new_offset = (p->offset - backing_offset);
1488df8bae1dSRodney W. Grimes 
1489df8bae1dSRodney W. Grimes 				/*
14900d94caffSDavid Greenman 				 * If the parent has a page here, or if this
14910d94caffSDavid Greenman 				 * page falls outside the parent, keep going.
1492df8bae1dSRodney W. Grimes 				 *
14930d94caffSDavid Greenman 				 * Otherwise, the backing_object must be left in
14940d94caffSDavid Greenman 				 * the chain.
1495df8bae1dSRodney W. Grimes 				 */
1496df8bae1dSRodney W. Grimes 
1497df8bae1dSRodney W. Grimes 				if (p->offset >= backing_offset &&
149826f9a767SRodney W. Grimes 				    new_offset <= size &&
14990d94caffSDavid Greenman 				    ((pp = vm_page_lookup(object, new_offset)) == NULL ||
15000d94caffSDavid Greenman 					!pp->valid) &&
150126f9a767SRodney W. Grimes 				    (!object->pager || !vm_pager_has_page(object->pager, object->paging_offset + new_offset))) {
1502df8bae1dSRodney W. Grimes 					/*
15030d94caffSDavid Greenman 					 * Page still needed. Can't go any
15040d94caffSDavid Greenman 					 * further.
1505df8bae1dSRodney W. Grimes 					 */
1506df8bae1dSRodney W. Grimes 					vm_object_unlock(backing_object);
1507df8bae1dSRodney W. Grimes 					return;
1508df8bae1dSRodney W. Grimes 				}
1509df8bae1dSRodney W. Grimes 			}
1510df8bae1dSRodney W. Grimes 
1511df8bae1dSRodney W. Grimes 			/*
15120d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
15130d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
15140d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1515df8bae1dSRodney W. Grimes 			 */
1516df8bae1dSRodney W. Grimes 
15170d94caffSDavid Greenman 			TAILQ_REMOVE(&object->shadow->reverse_shadow_head,
15180d94caffSDavid Greenman 			    object, reverse_shadow_list);
151926f9a767SRodney W. Grimes 			vm_object_reference(object->shadow = backing_object->shadow);
15202fe6e4d7SDavid Greenman 			if (object->shadow)
15210d94caffSDavid Greenman 				TAILQ_INSERT_TAIL(&object->shadow->reverse_shadow_head,
15220d94caffSDavid Greenman 				    object, reverse_shadow_list);
1523df8bae1dSRodney W. Grimes 			object->shadow_offset += backing_object->shadow_offset;
1524df8bae1dSRodney W. Grimes 
1525df8bae1dSRodney W. Grimes 			/*
15260d94caffSDavid Greenman 			 * Backing object might have had a copy pointer to us.
15270d94caffSDavid Greenman 			 * If it did, clear it.
1528df8bae1dSRodney W. Grimes 			 */
1529df8bae1dSRodney W. Grimes 			if (backing_object->copy == object) {
1530df8bae1dSRodney W. Grimes 				backing_object->copy = NULL;
1531df8bae1dSRodney W. Grimes 			}
15320d94caffSDavid Greenman 			/*
15330d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
15340d94caffSDavid Greenman 			 * its ref_count was at least 2, it will not vanish;
15350d94caffSDavid Greenman 			 * so we don't need to call vm_object_deallocate.
1536df8bae1dSRodney W. Grimes 			 */
153726f9a767SRodney W. Grimes 			if (backing_object->ref_count == 1)
153826f9a767SRodney W. Grimes 				printf("should have called obj deallocate\n");
1539df8bae1dSRodney W. Grimes 			backing_object->ref_count--;
1540df8bae1dSRodney W. Grimes 			vm_object_unlock(backing_object);
1541df8bae1dSRodney W. Grimes 
1542df8bae1dSRodney W. Grimes 			object_bypasses++;
1543df8bae1dSRodney W. Grimes 
1544df8bae1dSRodney W. Grimes 		}
1545df8bae1dSRodney W. Grimes 
1546df8bae1dSRodney W. Grimes 		/*
1547df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1548df8bae1dSRodney W. Grimes 		 */
1549df8bae1dSRodney W. Grimes 	}
1550df8bae1dSRodney W. Grimes }
1551df8bae1dSRodney W. Grimes 
1552df8bae1dSRodney W. Grimes /*
1553df8bae1dSRodney W. Grimes  *	vm_object_page_remove: [internal]
1554df8bae1dSRodney W. Grimes  *
1555df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
1556df8bae1dSRodney W. Grimes  *	object range from the object's list of pages.
1557df8bae1dSRodney W. Grimes  *
1558df8bae1dSRodney W. Grimes  *	The object must be locked.
1559df8bae1dSRodney W. Grimes  */
156026f9a767SRodney W. Grimes void
156126f9a767SRodney W. Grimes vm_object_page_remove(object, start, end)
1562df8bae1dSRodney W. Grimes 	register vm_object_t object;
1563df8bae1dSRodney W. Grimes 	register vm_offset_t start;
1564df8bae1dSRodney W. Grimes 	register vm_offset_t end;
1565df8bae1dSRodney W. Grimes {
1566df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
156726f9a767SRodney W. Grimes 	vm_offset_t size;
156826f9a767SRodney W. Grimes 	int s;
1569df8bae1dSRodney W. Grimes 
1570df8bae1dSRodney W. Grimes 	if (object == NULL)
1571df8bae1dSRodney W. Grimes 		return;
1572df8bae1dSRodney W. Grimes 
15732fe6e4d7SDavid Greenman 	object->paging_in_progress++;
157426f9a767SRodney W. Grimes 	start = trunc_page(start);
157526f9a767SRodney W. Grimes 	end = round_page(end);
157626f9a767SRodney W. Grimes again:
157726f9a767SRodney W. Grimes 	size = end - start;
157826f9a767SRodney W. Grimes 	if (size > 4 * PAGE_SIZE || size >= object->size / 4) {
15790d94caffSDavid Greenman 		for (p = object->memq.tqh_first; p != NULL; p = next) {
1580df8bae1dSRodney W. Grimes 			next = p->listq.tqe_next;
1581df8bae1dSRodney W. Grimes 			if ((start <= p->offset) && (p->offset < end)) {
1582a481f200SDavid Greenman 				s = splhigh();
15830d94caffSDavid Greenman 				if (p->bmapped) {
15840d94caffSDavid Greenman 					splx(s);
15850d94caffSDavid Greenman 					continue;
15860d94caffSDavid Greenman 				}
15870d94caffSDavid Greenman 				if ((p->flags & PG_BUSY) || p->busy) {
158826f9a767SRodney W. Grimes 					p->flags |= PG_WANTED;
158926f9a767SRodney W. Grimes 					tsleep((caddr_t) p, PVM, "vmopar", 0);
1590a481f200SDavid Greenman 					splx(s);
159126f9a767SRodney W. Grimes 					goto again;
159226f9a767SRodney W. Grimes 				}
1593a481f200SDavid Greenman 				splx(s);
1594df8bae1dSRodney W. Grimes 				pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
1595df8bae1dSRodney W. Grimes 				vm_page_lock_queues();
15960d94caffSDavid Greenman 				PAGE_WAKEUP(p);
1597df8bae1dSRodney W. Grimes 				vm_page_free(p);
1598df8bae1dSRodney W. Grimes 				vm_page_unlock_queues();
159926f9a767SRodney W. Grimes 			}
160026f9a767SRodney W. Grimes 		}
160126f9a767SRodney W. Grimes 	} else {
160226f9a767SRodney W. Grimes 		while (size > 0) {
160305f0fdd2SPoul-Henning Kamp 			while ((p = vm_page_lookup(object, start)) != 0) {
1604a481f200SDavid Greenman 				s = splhigh();
16050d94caffSDavid Greenman 				if (p->bmapped) {
16060d94caffSDavid Greenman 					splx(s);
16070d94caffSDavid Greenman 					break;
16080d94caffSDavid Greenman 				}
16090d94caffSDavid Greenman 				if ((p->flags & PG_BUSY) || p->busy) {
161026f9a767SRodney W. Grimes 					p->flags |= PG_WANTED;
161126f9a767SRodney W. Grimes 					tsleep((caddr_t) p, PVM, "vmopar", 0);
1612a481f200SDavid Greenman 					splx(s);
161326f9a767SRodney W. Grimes 					goto again;
161426f9a767SRodney W. Grimes 				}
1615a481f200SDavid Greenman 				splx(s);
161626f9a767SRodney W. Grimes 				pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
161726f9a767SRodney W. Grimes 				vm_page_lock_queues();
16180d94caffSDavid Greenman 				PAGE_WAKEUP(p);
161926f9a767SRodney W. Grimes 				vm_page_free(p);
162026f9a767SRodney W. Grimes 				vm_page_unlock_queues();
162126f9a767SRodney W. Grimes 			}
162226f9a767SRodney W. Grimes 			start += PAGE_SIZE;
162326f9a767SRodney W. Grimes 			size -= PAGE_SIZE;
1624df8bae1dSRodney W. Grimes 		}
1625df8bae1dSRodney W. Grimes 	}
16262fe6e4d7SDavid Greenman 	--object->paging_in_progress;
16272fe6e4d7SDavid Greenman 	if (object->paging_in_progress == 0)
16282fe6e4d7SDavid Greenman 		wakeup((caddr_t) object);
1629df8bae1dSRodney W. Grimes }
1630df8bae1dSRodney W. Grimes 
1631df8bae1dSRodney W. Grimes /*
1632df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1633df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1634df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1635df8bae1dSRodney W. Grimes  *
1636df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1637df8bae1dSRodney W. Grimes  *
1638df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1639df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1640df8bae1dSRodney W. Grimes  *
1641df8bae1dSRodney W. Grimes  *	Parameters:
1642df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1643df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1644df8bae1dSRodney W. Grimes  *		next_object	Second object into coalesce
1645df8bae1dSRodney W. Grimes  *		next_offset	Offset into next_object
1646df8bae1dSRodney W. Grimes  *
1647df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
1648df8bae1dSRodney W. Grimes  *		next_size	Size of reference to next_object
1649df8bae1dSRodney W. Grimes  *
1650df8bae1dSRodney W. Grimes  *	Conditions:
1651df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1652df8bae1dSRodney W. Grimes  */
16530d94caffSDavid Greenman boolean_t
16540d94caffSDavid Greenman vm_object_coalesce(prev_object, next_object,
1655df8bae1dSRodney W. Grimes     prev_offset, next_offset,
1656df8bae1dSRodney W. Grimes     prev_size, next_size)
1657df8bae1dSRodney W. Grimes 	register vm_object_t prev_object;
1658df8bae1dSRodney W. Grimes 	vm_object_t next_object;
1659df8bae1dSRodney W. Grimes 	vm_offset_t prev_offset, next_offset;
1660df8bae1dSRodney W. Grimes 	vm_size_t prev_size, next_size;
1661df8bae1dSRodney W. Grimes {
1662df8bae1dSRodney W. Grimes 	vm_size_t newsize;
1663df8bae1dSRodney W. Grimes 
1664df8bae1dSRodney W. Grimes 	if (next_object != NULL) {
1665df8bae1dSRodney W. Grimes 		return (FALSE);
1666df8bae1dSRodney W. Grimes 	}
1667df8bae1dSRodney W. Grimes 	if (prev_object == NULL) {
1668df8bae1dSRodney W. Grimes 		return (TRUE);
1669df8bae1dSRodney W. Grimes 	}
1670df8bae1dSRodney W. Grimes 	vm_object_lock(prev_object);
1671df8bae1dSRodney W. Grimes 
1672df8bae1dSRodney W. Grimes 	/*
1673df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1674df8bae1dSRodney W. Grimes 	 */
1675df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1676df8bae1dSRodney W. Grimes 
1677df8bae1dSRodney W. Grimes 	/*
16780d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
16790d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
16800d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1681df8bae1dSRodney W. Grimes 	 */
1682df8bae1dSRodney W. Grimes 
1683df8bae1dSRodney W. Grimes 	if (prev_object->ref_count > 1 ||
1684df8bae1dSRodney W. Grimes 	    prev_object->pager != NULL ||
1685df8bae1dSRodney W. Grimes 	    prev_object->shadow != NULL ||
1686df8bae1dSRodney W. Grimes 	    prev_object->copy != NULL) {
1687df8bae1dSRodney W. Grimes 		vm_object_unlock(prev_object);
1688df8bae1dSRodney W. Grimes 		return (FALSE);
1689df8bae1dSRodney W. Grimes 	}
1690df8bae1dSRodney W. Grimes 	/*
16910d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
16920d94caffSDavid Greenman 	 * deallocation.
1693df8bae1dSRodney W. Grimes 	 */
1694df8bae1dSRodney W. Grimes 
1695df8bae1dSRodney W. Grimes 	vm_object_page_remove(prev_object,
1696df8bae1dSRodney W. Grimes 	    prev_offset + prev_size,
1697df8bae1dSRodney W. Grimes 	    prev_offset + prev_size + next_size);
1698df8bae1dSRodney W. Grimes 
1699df8bae1dSRodney W. Grimes 	/*
1700df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1701df8bae1dSRodney W. Grimes 	 */
1702df8bae1dSRodney W. Grimes 	newsize = prev_offset + prev_size + next_size;
1703df8bae1dSRodney W. Grimes 	if (newsize > prev_object->size)
1704df8bae1dSRodney W. Grimes 		prev_object->size = newsize;
1705df8bae1dSRodney W. Grimes 
1706df8bae1dSRodney W. Grimes 	vm_object_unlock(prev_object);
1707df8bae1dSRodney W. Grimes 	return (TRUE);
1708df8bae1dSRodney W. Grimes }
1709df8bae1dSRodney W. Grimes 
1710df8bae1dSRodney W. Grimes /*
171126f9a767SRodney W. Grimes  * returns page after looking up in shadow chain
171226f9a767SRodney W. Grimes  */
171326f9a767SRodney W. Grimes 
171426f9a767SRodney W. Grimes vm_page_t
171526f9a767SRodney W. Grimes vm_object_page_lookup(object, offset)
171626f9a767SRodney W. Grimes 	vm_object_t object;
171726f9a767SRodney W. Grimes 	vm_offset_t offset;
171826f9a767SRodney W. Grimes {
171926f9a767SRodney W. Grimes 	vm_page_t m;
17200d94caffSDavid Greenman 
172126f9a767SRodney W. Grimes 	if (!(m = vm_page_lookup(object, offset))) {
172226f9a767SRodney W. Grimes 		if (!object->shadow)
172326f9a767SRodney W. Grimes 			return 0;
172426f9a767SRodney W. Grimes 		else
172526f9a767SRodney W. Grimes 			return vm_object_page_lookup(object->shadow, offset + object->shadow_offset);
172626f9a767SRodney W. Grimes 	}
172726f9a767SRodney W. Grimes 	return m;
172826f9a767SRodney W. Grimes }
172926f9a767SRodney W. Grimes 
1730a1f6d91cSDavid Greenman int
1731a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry)
1732a1f6d91cSDavid Greenman 	vm_map_t map;
1733a1f6d91cSDavid Greenman 	vm_object_t object;
1734a1f6d91cSDavid Greenman 	vm_map_entry_t entry;
1735a1f6d91cSDavid Greenman {
1736a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1737a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1738a1f6d91cSDavid Greenman 	vm_object_t obj;
1739a1f6d91cSDavid Greenman 	int entcount;
1740a1f6d91cSDavid Greenman 
1741a1f6d91cSDavid Greenman 	if (map == 0)
1742a1f6d91cSDavid Greenman 		return 0;
1743a1f6d91cSDavid Greenman 
1744a1f6d91cSDavid Greenman 	if (entry == 0) {
1745a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1746a1f6d91cSDavid Greenman 		entcount = map->nentries;
1747a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1748a1f6d91cSDavid Greenman 			if( _vm_object_in_map(map, object, tmpe)) {
1749a1f6d91cSDavid Greenman 				return 1;
1750a1f6d91cSDavid Greenman 			}
1751a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1752a1f6d91cSDavid Greenman 		}
1753a1f6d91cSDavid Greenman 	} else if (entry->is_sub_map || entry->is_a_map) {
1754a1f6d91cSDavid Greenman 		tmpm = entry->object.share_map;
1755a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1756a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1757a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1758a1f6d91cSDavid Greenman 			if( _vm_object_in_map(tmpm, object, tmpe)) {
1759a1f6d91cSDavid Greenman 				return 1;
1760a1f6d91cSDavid Greenman 			}
1761a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1762a1f6d91cSDavid Greenman 		}
1763a1f6d91cSDavid Greenman 	} else if (obj = entry->object.vm_object) {
1764a1f6d91cSDavid Greenman 		for(; obj; obj=obj->shadow)
1765a1f6d91cSDavid Greenman 			if( obj == object) {
1766a1f6d91cSDavid Greenman 				return 1;
1767a1f6d91cSDavid Greenman 			}
1768a1f6d91cSDavid Greenman 	}
1769a1f6d91cSDavid Greenman 	return 0;
1770a1f6d91cSDavid Greenman }
1771a1f6d91cSDavid Greenman 
1772a1f6d91cSDavid Greenman int
1773a1f6d91cSDavid Greenman vm_object_in_map( object)
1774a1f6d91cSDavid Greenman 	vm_object_t object;
1775a1f6d91cSDavid Greenman {
1776a1f6d91cSDavid Greenman 	struct proc *p;
1777a1f6d91cSDavid Greenman 	for (p = (struct proc *) allproc; p != NULL; p = p->p_next) {
1778a1f6d91cSDavid Greenman 		if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1779a1f6d91cSDavid Greenman 			continue;
1780a1f6d91cSDavid Greenman /*
1781a1f6d91cSDavid Greenman 		if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
1782a1f6d91cSDavid Greenman 			continue;
1783a1f6d91cSDavid Greenman 		}
1784a1f6d91cSDavid Greenman */
1785a1f6d91cSDavid Greenman 		if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1786a1f6d91cSDavid Greenman 			return 1;
1787a1f6d91cSDavid Greenman 	}
1788a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kernel_map, object, 0))
1789a1f6d91cSDavid Greenman 		return 1;
1790a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kmem_map, object, 0))
1791a1f6d91cSDavid Greenman 		return 1;
1792a1f6d91cSDavid Greenman 	if( _vm_object_in_map( pager_map, object, 0))
1793a1f6d91cSDavid Greenman 		return 1;
1794a1f6d91cSDavid Greenman 	if( _vm_object_in_map( buffer_map, object, 0))
1795a1f6d91cSDavid Greenman 		return 1;
1796a1f6d91cSDavid Greenman 	if( _vm_object_in_map( io_map, object, 0))
1797a1f6d91cSDavid Greenman 		return 1;
1798a1f6d91cSDavid Greenman 	if( _vm_object_in_map( phys_map, object, 0))
1799a1f6d91cSDavid Greenman 		return 1;
1800a1f6d91cSDavid Greenman 	if( _vm_object_in_map( mb_map, object, 0))
1801a1f6d91cSDavid Greenman 		return 1;
1802a1f6d91cSDavid Greenman 	if( _vm_object_in_map( u_map, object, 0))
1803a1f6d91cSDavid Greenman 		return 1;
1804a1f6d91cSDavid Greenman 	return 0;
1805a1f6d91cSDavid Greenman }
1806a1f6d91cSDavid Greenman 
1807a1f6d91cSDavid Greenman void
1808a1f6d91cSDavid Greenman vm_object_check() {
1809a1f6d91cSDavid Greenman 	int i;
1810a1f6d91cSDavid Greenman 	int maxhash = 0;
1811a1f6d91cSDavid Greenman 	vm_object_t object;
1812a1f6d91cSDavid Greenman 	vm_object_hash_entry_t entry;
1813a1f6d91cSDavid Greenman 
1814a1f6d91cSDavid Greenman 	/*
1815a1f6d91cSDavid Greenman 	 * make sure that no internal objs are hashed
1816a1f6d91cSDavid Greenman 	 */
1817a1f6d91cSDavid Greenman 	for (i=0; i<VM_OBJECT_HASH_COUNT;i++) {
1818a1f6d91cSDavid Greenman 		int lsize = 0;
1819a1f6d91cSDavid Greenman 		for (entry = vm_object_hashtable[i].tqh_first;
1820a1f6d91cSDavid Greenman 		    entry != NULL;
1821a1f6d91cSDavid Greenman 		    entry = entry->hash_links.tqe_next) {
1822a1f6d91cSDavid Greenman 			if( entry->object->flags & OBJ_INTERNAL) {
1823a1f6d91cSDavid Greenman 				printf("vmochk: internal obj on hash: size: %d\n", entry->object->size);
1824a1f6d91cSDavid Greenman 			}
1825a1f6d91cSDavid Greenman 			++lsize;
1826a1f6d91cSDavid Greenman 		}
1827a1f6d91cSDavid Greenman 		if( lsize > maxhash)
1828a1f6d91cSDavid Greenman 			maxhash = lsize;
1829a1f6d91cSDavid Greenman 	}
1830a1f6d91cSDavid Greenman 
1831a1f6d91cSDavid Greenman 	printf("maximum object hash queue size: %d\n",  maxhash);
1832a1f6d91cSDavid Greenman 
1833a1f6d91cSDavid Greenman 	/*
1834a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
1835a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
1836a1f6d91cSDavid Greenman 	 */
1837a1f6d91cSDavid Greenman 	for (object = vm_object_list.tqh_first;
1838a1f6d91cSDavid Greenman 			object != NULL;
1839a1f6d91cSDavid Greenman 			object = object->object_list.tqe_next) {
1840a1f6d91cSDavid Greenman 		if( object->flags & OBJ_INTERNAL) {
1841a1f6d91cSDavid Greenman 			if( object->ref_count == 0) {
1842a1f6d91cSDavid Greenman 				printf("vmochk: internal obj has zero ref count: %d\n",
1843a1f6d91cSDavid Greenman 					object->size);
1844a1f6d91cSDavid Greenman 			}
1845a1f6d91cSDavid Greenman 			if( !vm_object_in_map(object)) {
1846a1f6d91cSDavid Greenman 				printf("vmochk: internal obj is not in a map: ref: %d, size: %d, pager: 0x%x, shadow: 0x%x\n", object->ref_count, object->size, object->pager, object->shadow);
1847a1f6d91cSDavid Greenman 			}
1848a1f6d91cSDavid Greenman 		}
1849a1f6d91cSDavid Greenman 	}
1850a1f6d91cSDavid Greenman }
1851a1f6d91cSDavid Greenman 
185226f9a767SRodney W. Grimes #define DEBUG
18538a129caeSDavid Greenman #if defined(DEBUG) || defined(DDB)
185426f9a767SRodney W. Grimes /*
1855df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
1856df8bae1dSRodney W. Grimes  */
18570d94caffSDavid Greenman void
18580d94caffSDavid Greenman vm_object_print(object, full)
1859df8bae1dSRodney W. Grimes 	vm_object_t object;
1860df8bae1dSRodney W. Grimes 	boolean_t full;
1861df8bae1dSRodney W. Grimes {
1862df8bae1dSRodney W. Grimes 	register vm_page_t p;
1863df8bae1dSRodney W. Grimes 	extern indent;
1864df8bae1dSRodney W. Grimes 
1865df8bae1dSRodney W. Grimes 	register int count;
1866df8bae1dSRodney W. Grimes 
1867df8bae1dSRodney W. Grimes 	if (object == NULL)
1868df8bae1dSRodney W. Grimes 		return;
1869df8bae1dSRodney W. Grimes 
1870df8bae1dSRodney W. Grimes 	iprintf("Object 0x%x: size=0x%x, res=%d, ref=%d, ",
1871df8bae1dSRodney W. Grimes 	    (int) object, (int) object->size,
1872df8bae1dSRodney W. Grimes 	    object->resident_page_count, object->ref_count);
1873df8bae1dSRodney W. Grimes 	printf("pager=0x%x+0x%x, shadow=(0x%x)+0x%x\n",
1874df8bae1dSRodney W. Grimes 	    (int) object->pager, (int) object->paging_offset,
1875df8bae1dSRodney W. Grimes 	    (int) object->shadow, (int) object->shadow_offset);
187605f0fdd2SPoul-Henning Kamp 	printf("cache: next=%p, prev=%p\n",
1877df8bae1dSRodney W. Grimes 	    object->cached_list.tqe_next, object->cached_list.tqe_prev);
1878df8bae1dSRodney W. Grimes 
1879df8bae1dSRodney W. Grimes 	if (!full)
1880df8bae1dSRodney W. Grimes 		return;
1881df8bae1dSRodney W. Grimes 
1882df8bae1dSRodney W. Grimes 	indent += 2;
1883df8bae1dSRodney W. Grimes 	count = 0;
1884df8bae1dSRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) {
1885df8bae1dSRodney W. Grimes 		if (count == 0)
1886df8bae1dSRodney W. Grimes 			iprintf("memory:=");
1887df8bae1dSRodney W. Grimes 		else if (count == 6) {
1888df8bae1dSRodney W. Grimes 			printf("\n");
1889df8bae1dSRodney W. Grimes 			iprintf(" ...");
1890df8bae1dSRodney W. Grimes 			count = 0;
1891df8bae1dSRodney W. Grimes 		} else
1892df8bae1dSRodney W. Grimes 			printf(",");
1893df8bae1dSRodney W. Grimes 		count++;
1894df8bae1dSRodney W. Grimes 
189505f0fdd2SPoul-Henning Kamp 		printf("(off=0x%lx,page=0x%lx)",
189605f0fdd2SPoul-Henning Kamp 		    (u_long) p->offset, (u_long) VM_PAGE_TO_PHYS(p));
1897df8bae1dSRodney W. Grimes 	}
1898df8bae1dSRodney W. Grimes 	if (count != 0)
1899df8bae1dSRodney W. Grimes 		printf("\n");
1900df8bae1dSRodney W. Grimes 	indent -= 2;
1901df8bae1dSRodney W. Grimes }
19028a129caeSDavid Greenman #endif				/* defined(DEBUG) || defined(DDB) */
1903