xref: /freebsd/sys/vm/vm_object.c (revision c0503609a04a603d41f33f654fd118b2f36d4844)
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  *
64c0503609SDavid Greenman  * $Id: vm_object.c,v 1.25 1995/02/21 01:22:47 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 
248ba8da839SDavid Greenman 		if (object->ref_count == 0)
249ba8da839SDavid Greenman 			panic("vm_object_deallocate: object deallocated too many times");
250ba8da839SDavid Greenman 
251df8bae1dSRodney W. Grimes 		/*
2520d94caffSDavid Greenman 		 * The cache holds a reference (uncounted) to the object; we
2530d94caffSDavid Greenman 		 * must lock it before removing the object.
254df8bae1dSRodney W. Grimes 		 */
255df8bae1dSRodney W. Grimes 
256df8bae1dSRodney W. Grimes 		vm_object_cache_lock();
257df8bae1dSRodney W. Grimes 
258df8bae1dSRodney W. Grimes 		/*
259df8bae1dSRodney W. Grimes 		 * Lose the reference
260df8bae1dSRodney W. Grimes 		 */
261df8bae1dSRodney W. Grimes 		vm_object_lock(object);
262df8bae1dSRodney W. Grimes 		if (--(object->ref_count) != 0) {
263010cf3b9SDavid Greenman 			if (object->ref_count == 1) {
264a1f6d91cSDavid Greenman 				vm_object_t robject;
265a1f6d91cSDavid Greenman 				robject = object->reverse_shadow_head.tqh_first;
266a1f6d91cSDavid Greenman 				if( robject) {
267a1f6d91cSDavid Greenman 					int s;
268a1f6d91cSDavid Greenman 					robject->ref_count += 2;
2699b4814bbSDavid Greenman 					object->ref_count += 2;
270a1f6d91cSDavid Greenman 
271a1f6d91cSDavid Greenman 					do {
272a1f6d91cSDavid Greenman 						s = splhigh();
273a1f6d91cSDavid Greenman 						while (robject->paging_in_progress) {
274c0503609SDavid Greenman 							robject->flags |= OBJ_PIPWNT;
275a1f6d91cSDavid Greenman 							tsleep(robject, PVM, "objde1", 0);
2760d94caffSDavid Greenman 						}
277a1f6d91cSDavid Greenman 
278a1f6d91cSDavid Greenman 						while (object->paging_in_progress) {
279c0503609SDavid Greenman 							object->flags |= OBJ_PIPWNT;
280a1f6d91cSDavid Greenman 							tsleep(object, PVM, "objde2", 0);
281a1f6d91cSDavid Greenman 						}
282a1f6d91cSDavid Greenman 						splx(s);
283a1f6d91cSDavid Greenman 
284a1f6d91cSDavid Greenman 					} while( object->paging_in_progress || robject->paging_in_progress);
285a1f6d91cSDavid Greenman 
2869b4814bbSDavid Greenman 					object->ref_count -= 2;
287a1f6d91cSDavid Greenman 					robject->ref_count -= 2;
288a1f6d91cSDavid Greenman 					if( robject->ref_count == 0) {
289a1f6d91cSDavid Greenman 						vm_object_unlock(object);
290a1f6d91cSDavid Greenman 						vm_object_cache_unlock();
291a1f6d91cSDavid Greenman 						robject->ref_count += 1;
292ba8da839SDavid Greenman 						object = robject;
293ba8da839SDavid Greenman 						continue;
294a1f6d91cSDavid Greenman 					}
295a1f6d91cSDavid Greenman 					vm_object_cache_unlock();
296a1f6d91cSDavid Greenman 					vm_object_unlock(object);
297a1f6d91cSDavid Greenman 					vm_object_lock(robject);
298a1f6d91cSDavid Greenman 					vm_object_collapse(robject);
299a1f6d91cSDavid Greenman 					return;
300010cf3b9SDavid Greenman 				}
301010cf3b9SDavid Greenman 			}
30226f9a767SRodney W. Grimes 			vm_object_unlock(object);
303df8bae1dSRodney W. Grimes 			/*
3040d94caffSDavid Greenman 			 * If there are still references, then we are done.
305df8bae1dSRodney W. Grimes 			 */
306df8bae1dSRodney W. Grimes 			vm_object_cache_unlock();
307df8bae1dSRodney W. Grimes 			return;
308df8bae1dSRodney W. Grimes 		}
309df8bae1dSRodney W. Grimes 		/*
3100d94caffSDavid Greenman 		 * See if this object can persist.  If so, enter it in the
3110d94caffSDavid Greenman 		 * cache, then deactivate all of its pages.
312df8bae1dSRodney W. Grimes 		 */
313df8bae1dSRodney W. Grimes 
314df8bae1dSRodney W. Grimes 		if (object->flags & OBJ_CANPERSIST) {
315df8bae1dSRodney W. Grimes 
316df8bae1dSRodney W. Grimes 			TAILQ_INSERT_TAIL(&vm_object_cached_list, object,
317df8bae1dSRodney W. Grimes 			    cached_list);
318df8bae1dSRodney W. Grimes 			vm_object_cached++;
319df8bae1dSRodney W. Grimes 			vm_object_cache_unlock();
320df8bae1dSRodney W. Grimes 
321df8bae1dSRodney W. Grimes 			vm_object_unlock(object);
322df8bae1dSRodney W. Grimes 
323df8bae1dSRodney W. Grimes 			vm_object_cache_trim();
324df8bae1dSRodney W. Grimes 			return;
325df8bae1dSRodney W. Grimes 		}
326df8bae1dSRodney W. Grimes 		/*
327df8bae1dSRodney W. Grimes 		 * Make sure no one can look us up now.
328df8bae1dSRodney W. Grimes 		 */
3290d94caffSDavid Greenman 		object->flags |= OBJ_DEAD;
330df8bae1dSRodney W. Grimes 		vm_object_remove(object->pager);
331df8bae1dSRodney W. Grimes 		vm_object_cache_unlock();
332df8bae1dSRodney W. Grimes 
333df8bae1dSRodney W. Grimes 		temp = object->shadow;
3342fe6e4d7SDavid Greenman 		if (temp)
3352fe6e4d7SDavid Greenman 			TAILQ_REMOVE(&temp->reverse_shadow_head, object, reverse_shadow_list);
336df8bae1dSRodney W. Grimes 		vm_object_terminate(object);
337df8bae1dSRodney W. Grimes 		/* unlocks and deallocates object */
338df8bae1dSRodney W. Grimes 		object = temp;
339df8bae1dSRodney W. Grimes 	}
340df8bae1dSRodney W. Grimes }
341df8bae1dSRodney W. Grimes 
342df8bae1dSRodney W. Grimes /*
343df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
344df8bae1dSRodney W. Grimes  *	up all previously used resources.
345df8bae1dSRodney W. Grimes  *
346df8bae1dSRodney W. Grimes  *	The object must be locked.
347df8bae1dSRodney W. Grimes  */
34826f9a767SRodney W. Grimes void
34926f9a767SRodney W. Grimes vm_object_terminate(object)
350df8bae1dSRodney W. Grimes 	register vm_object_t object;
351df8bae1dSRodney W. Grimes {
3520d94caffSDavid Greenman 	register vm_page_t p, next;
353df8bae1dSRodney W. Grimes 	vm_object_t shadow_object;
35426f9a767SRodney W. Grimes 	int s;
3550d94caffSDavid Greenman 	struct vnode *vp = NULL;
356df8bae1dSRodney W. Grimes 
357df8bae1dSRodney W. Grimes 	/*
3580d94caffSDavid Greenman 	 * Detach the object from its shadow if we are the shadow's copy.
359df8bae1dSRodney W. Grimes 	 */
360df8bae1dSRodney W. Grimes 	if ((shadow_object = object->shadow) != NULL) {
361df8bae1dSRodney W. Grimes 		vm_object_lock(shadow_object);
362df8bae1dSRodney W. Grimes 		if (shadow_object->copy == object)
363df8bae1dSRodney W. Grimes 			shadow_object->copy = NULL;
36426f9a767SRodney W. Grimes /*
365df8bae1dSRodney W. Grimes 		else if (shadow_object->copy != NULL)
366df8bae1dSRodney W. Grimes 			panic("vm_object_terminate: copy/shadow inconsistency");
36726f9a767SRodney W. Grimes */
368df8bae1dSRodney W. Grimes 		vm_object_unlock(shadow_object);
369df8bae1dSRodney W. Grimes 	}
3700d94caffSDavid Greenman 	if (object->pager && (object->pager->pg_type == PG_VNODE)) {
3710d94caffSDavid Greenman 		vn_pager_t vnp = object->pager->pg_data;
372df8bae1dSRodney W. Grimes 
3730d94caffSDavid Greenman 		vp = vnp->vnp_vp;
3740d94caffSDavid Greenman 		VOP_FSYNC(vp, NOCRED, MNT_WAIT, NULL);
3750d94caffSDavid Greenman 		vinvalbuf(vp, 0, NOCRED, NULL, 0, 0);
3760d94caffSDavid Greenman 	}
377df8bae1dSRodney W. Grimes 	/*
3780d94caffSDavid Greenman 	 * Wait until the pageout daemon is through with the object.
379df8bae1dSRodney W. Grimes 	 */
38026f9a767SRodney W. Grimes 
3810d94caffSDavid Greenman 	s = splhigh();
382df8bae1dSRodney W. Grimes 	while (object->paging_in_progress) {
3830d94caffSDavid Greenman 		vm_object_unlock(object);
384c0503609SDavid Greenman 		object->flags |= OBJ_PIPWNT;
3850d94caffSDavid Greenman 		tsleep((caddr_t) object, PVM, "objtrm", 0);
386df8bae1dSRodney W. Grimes 		vm_object_lock(object);
387df8bae1dSRodney W. Grimes 	}
3880d94caffSDavid Greenman 	splx(s);
389df8bae1dSRodney W. Grimes 
390df8bae1dSRodney W. Grimes 	/*
3910d94caffSDavid Greenman 	 * While the paging system is locked, pull the object's pages off the
3920d94caffSDavid Greenman 	 * active and inactive queues.  This keeps the pageout daemon from
3930d94caffSDavid Greenman 	 * playing with them during vm_pager_deallocate.
394df8bae1dSRodney W. Grimes 	 *
3950d94caffSDavid Greenman 	 * We can't free the pages yet, because the object's pager may have to
3960d94caffSDavid Greenman 	 * write them out before deallocating the paging space.
397df8bae1dSRodney W. Grimes 	 */
39826f9a767SRodney W. Grimes 
3990d94caffSDavid Greenman 	for (p = object->memq.tqh_first; p; p = next) {
40026f9a767SRodney W. Grimes 		VM_PAGE_CHECK(p);
4010d94caffSDavid Greenman 		next = p->listq.tqe_next;
40226f9a767SRodney W. Grimes 
40326f9a767SRodney W. Grimes 		vm_page_lock_queues();
4040d94caffSDavid Greenman 		if (p->flags & PG_CACHE)
4050d94caffSDavid Greenman 			vm_page_free(p);
4060d94caffSDavid Greenman 		else
4070d94caffSDavid Greenman 			vm_page_unqueue(p);
40826f9a767SRodney W. Grimes 		vm_page_unlock_queues();
4090d94caffSDavid Greenman 		p = next;
41026f9a767SRodney W. Grimes 	}
41126f9a767SRodney W. Grimes 
41226f9a767SRodney W. Grimes 	if (object->paging_in_progress != 0)
41326f9a767SRodney W. Grimes 		panic("vm_object_deallocate: pageout in progress");
41426f9a767SRodney W. Grimes 
41526f9a767SRodney W. Grimes 	/*
4160d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
4170d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
41826f9a767SRodney W. Grimes 	 */
41926f9a767SRodney W. Grimes 
420a465acdaSDavid Greenman 	if (((object->flags & OBJ_INTERNAL) == 0) &&
421a465acdaSDavid Greenman 	    object->pager && (object->pager->pg_type != PG_DEVICE)) {
422df8bae1dSRodney W. Grimes 		(void) vm_object_page_clean(object, 0, 0, TRUE, TRUE);
423df8bae1dSRodney W. Grimes 	}
424df8bae1dSRodney W. Grimes 	/*
4250d94caffSDavid Greenman 	 * one last time -- get rid of buffers that might have been created
4260d94caffSDavid Greenman 	 * for the vm_object_page_clean
4270d94caffSDavid Greenman 	 */
4280d94caffSDavid Greenman 	if (vp != NULL) {
4290d94caffSDavid Greenman 		vm_object_unlock(object);
4300d94caffSDavid Greenman 		vinvalbuf(vp, 0, NOCRED, NULL, 0, 0);
4310d94caffSDavid Greenman 		vm_object_lock(object);
4320d94caffSDavid Greenman 	}
4330d94caffSDavid Greenman 	/*
4340d94caffSDavid Greenman 	 * Now free the pages. For internal objects, this also removes them
4350d94caffSDavid Greenman 	 * from paging queues.
436df8bae1dSRodney W. Grimes 	 */
437df8bae1dSRodney W. Grimes 	while ((p = object->memq.tqh_first) != NULL) {
438df8bae1dSRodney W. Grimes 		VM_PAGE_CHECK(p);
439df8bae1dSRodney W. Grimes 		vm_page_lock_queues();
4400d94caffSDavid Greenman 		PAGE_WAKEUP(p);
441df8bae1dSRodney W. Grimes 		vm_page_free(p);
442df8bae1dSRodney W. Grimes 		cnt.v_pfree++;
443df8bae1dSRodney W. Grimes 		vm_page_unlock_queues();
444df8bae1dSRodney W. Grimes 	}
4450d94caffSDavid Greenman 	vm_object_unlock(object);
446df8bae1dSRodney W. Grimes 
447df8bae1dSRodney W. Grimes 	/*
448df8bae1dSRodney W. Grimes 	 * Let the pager know object is dead.
449df8bae1dSRodney W. Grimes 	 */
450df8bae1dSRodney W. Grimes 	if (object->pager != NULL)
451df8bae1dSRodney W. Grimes 		vm_pager_deallocate(object->pager);
452df8bae1dSRodney W. Grimes 
453df8bae1dSRodney W. Grimes 	simple_lock(&vm_object_list_lock);
454df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
455df8bae1dSRodney W. Grimes 	vm_object_count--;
456df8bae1dSRodney W. Grimes 	simple_unlock(&vm_object_list_lock);
457df8bae1dSRodney W. Grimes 
458df8bae1dSRodney W. Grimes 	/*
459df8bae1dSRodney W. Grimes 	 * Free the space for the object.
460df8bae1dSRodney W. Grimes 	 */
461df8bae1dSRodney W. Grimes 	free((caddr_t) object, M_VMOBJ);
462df8bae1dSRodney W. Grimes }
463df8bae1dSRodney W. Grimes 
464df8bae1dSRodney W. Grimes /*
465df8bae1dSRodney W. Grimes  *	vm_object_page_clean
466df8bae1dSRodney W. Grimes  *
467df8bae1dSRodney W. Grimes  *	Clean all dirty pages in the specified range of object.
46826f9a767SRodney W. Grimes  *	Leaves page on whatever queue it is currently on.
46926f9a767SRodney W. Grimes  *
47026f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
47126f9a767SRodney W. Grimes  *
47226f9a767SRodney W. Grimes  *	The object must be locked.
47326f9a767SRodney W. Grimes  */
47426f9a767SRodney W. Grimes #if 1
47526f9a767SRodney W. Grimes boolean_t
47626f9a767SRodney W. Grimes vm_object_page_clean(object, start, end, syncio, de_queue)
47726f9a767SRodney W. Grimes 	register vm_object_t object;
47826f9a767SRodney W. Grimes 	register vm_offset_t start;
47926f9a767SRodney W. Grimes 	register vm_offset_t end;
48026f9a767SRodney W. Grimes 	boolean_t syncio;
48126f9a767SRodney W. Grimes 	boolean_t de_queue;
48226f9a767SRodney W. Grimes {
48326f9a767SRodney W. Grimes 	register vm_page_t p, nextp;
48426f9a767SRodney W. Grimes 	int size;
48526f9a767SRodney W. Grimes 
48626f9a767SRodney W. Grimes 	if (object->pager == NULL)
48726f9a767SRodney W. Grimes 		return 1;
48826f9a767SRodney W. Grimes 
48926f9a767SRodney W. Grimes 	if (start != end) {
49026f9a767SRodney W. Grimes 		start = trunc_page(start);
49126f9a767SRodney W. Grimes 		end = round_page(end);
49226f9a767SRodney W. Grimes 	}
49326f9a767SRodney W. Grimes 	size = end - start;
49426f9a767SRodney W. Grimes 
49526f9a767SRodney W. Grimes again:
49626f9a767SRodney W. Grimes 	/*
49726f9a767SRodney W. Grimes 	 * Wait until the pageout daemon is through with the object.
49826f9a767SRodney W. Grimes 	 */
49926f9a767SRodney W. Grimes 	while (object->paging_in_progress) {
500c0503609SDavid Greenman 		object->flags |= OBJ_PIPWNT;
5010d94caffSDavid Greenman 		tsleep(object, PVM, "objpcw", 0);
50226f9a767SRodney W. Grimes 	}
50326f9a767SRodney W. Grimes 
50426f9a767SRodney W. Grimes 	nextp = object->memq.tqh_first;
50526f9a767SRodney W. Grimes 	while ((p = nextp) && ((start == end) || (size != 0))) {
50626f9a767SRodney W. Grimes 		nextp = p->listq.tqe_next;
50726f9a767SRodney W. Grimes 		if (start == end || (p->offset >= start && p->offset < end)) {
5080d94caffSDavid Greenman 			if ((p->flags & PG_BUSY) || p->busy) {
5090d94caffSDavid Greenman 				int s = splhigh();
51026f9a767SRodney W. Grimes 
5110d94caffSDavid Greenman 				p->flags |= PG_WANTED;
5120d94caffSDavid Greenman 				tsleep(p, PVM, "objpcn", 0);
5130d94caffSDavid Greenman 				splx(s);
5140d94caffSDavid Greenman 				goto again;
5150d94caffSDavid Greenman 			}
51626f9a767SRodney W. Grimes 			size -= PAGE_SIZE;
51726f9a767SRodney W. Grimes 
5180d94caffSDavid Greenman 			vm_page_test_dirty(p);
51926f9a767SRodney W. Grimes 
5200d94caffSDavid Greenman 			if ((p->dirty & p->valid) != 0) {
52126f9a767SRodney W. Grimes 				vm_pageout_clean(p, VM_PAGEOUT_FORCE);
52226f9a767SRodney W. Grimes 				goto again;
52326f9a767SRodney W. Grimes 			}
52426f9a767SRodney W. Grimes 		}
52526f9a767SRodney W. Grimes 	}
52626f9a767SRodney W. Grimes 	wakeup((caddr_t) object);
52726f9a767SRodney W. Grimes 	return 1;
52826f9a767SRodney W. Grimes }
52926f9a767SRodney W. Grimes #endif
53026f9a767SRodney W. Grimes /*
53126f9a767SRodney W. Grimes  *	vm_object_page_clean
53226f9a767SRodney W. Grimes  *
53326f9a767SRodney W. Grimes  *	Clean all dirty pages in the specified range of object.
534df8bae1dSRodney W. Grimes  *	If syncio is TRUE, page cleaning is done synchronously.
535df8bae1dSRodney W. Grimes  *	If de_queue is TRUE, pages are removed from any paging queue
536df8bae1dSRodney W. Grimes  *	they were on, otherwise they are left on whatever queue they
537df8bae1dSRodney W. Grimes  *	were on before the cleaning operation began.
538df8bae1dSRodney W. Grimes  *
539df8bae1dSRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
540df8bae1dSRodney W. Grimes  *
541df8bae1dSRodney W. Grimes  *	The object must be locked.
542df8bae1dSRodney W. Grimes  *
543df8bae1dSRodney W. Grimes  *	Returns TRUE if all was well, FALSE if there was a pager error
544df8bae1dSRodney W. Grimes  *	somewhere.  We attempt to clean (and dequeue) all pages regardless
545df8bae1dSRodney W. Grimes  *	of where an error occurs.
546df8bae1dSRodney W. Grimes  */
54726f9a767SRodney W. Grimes #if 0
548df8bae1dSRodney W. Grimes boolean_t
549df8bae1dSRodney W. Grimes vm_object_page_clean(object, start, end, syncio, de_queue)
550df8bae1dSRodney W. Grimes 	register vm_object_t object;
551df8bae1dSRodney W. Grimes 	register vm_offset_t start;
552df8bae1dSRodney W. Grimes 	register vm_offset_t end;
553df8bae1dSRodney W. Grimes 	boolean_t syncio;
554df8bae1dSRodney W. Grimes 	boolean_t de_queue;
555df8bae1dSRodney W. Grimes {
556df8bae1dSRodney W. Grimes 	register vm_page_t p;
557df8bae1dSRodney W. Grimes 	int onqueue;
558df8bae1dSRodney W. Grimes 	boolean_t noerror = TRUE;
559df8bae1dSRodney W. Grimes 
560df8bae1dSRodney W. Grimes 	if (object == NULL)
561df8bae1dSRodney W. Grimes 		return (TRUE);
562df8bae1dSRodney W. Grimes 
563df8bae1dSRodney W. Grimes 	/*
564df8bae1dSRodney W. Grimes 	 * If it is an internal object and there is no pager, attempt to
5650d94caffSDavid Greenman 	 * allocate one.  Note that vm_object_collapse may relocate one from a
5660d94caffSDavid Greenman 	 * collapsed object so we must recheck afterward.
567df8bae1dSRodney W. Grimes 	 */
568df8bae1dSRodney W. Grimes 	if ((object->flags & OBJ_INTERNAL) && object->pager == NULL) {
569df8bae1dSRodney W. Grimes 		vm_object_collapse(object);
570df8bae1dSRodney W. Grimes 		if (object->pager == NULL) {
571df8bae1dSRodney W. Grimes 			vm_pager_t pager;
572df8bae1dSRodney W. Grimes 
573df8bae1dSRodney W. Grimes 			vm_object_unlock(object);
574df8bae1dSRodney W. Grimes 			pager = vm_pager_allocate(PG_DFLT, (caddr_t) 0,
575df8bae1dSRodney W. Grimes 			    object->size, VM_PROT_ALL,
576df8bae1dSRodney W. Grimes 			    (vm_offset_t) 0);
577df8bae1dSRodney W. Grimes 			if (pager)
578df8bae1dSRodney W. Grimes 				vm_object_setpager(object, pager, 0, FALSE);
579df8bae1dSRodney W. Grimes 			vm_object_lock(object);
580df8bae1dSRodney W. Grimes 		}
581df8bae1dSRodney W. Grimes 	}
582df8bae1dSRodney W. Grimes 	if (object->pager == NULL)
583df8bae1dSRodney W. Grimes 		return (FALSE);
584df8bae1dSRodney W. Grimes 
585df8bae1dSRodney W. Grimes again:
586df8bae1dSRodney W. Grimes 	/*
587df8bae1dSRodney W. Grimes 	 * Wait until the pageout daemon is through with the object.
588df8bae1dSRodney W. Grimes 	 */
589df8bae1dSRodney W. Grimes 	while (object->paging_in_progress) {
590df8bae1dSRodney W. Grimes 		vm_object_sleep((int) object, object, FALSE);
591df8bae1dSRodney W. Grimes 		vm_object_lock(object);
592df8bae1dSRodney W. Grimes 	}
593df8bae1dSRodney W. Grimes 	/*
594df8bae1dSRodney W. Grimes 	 * Loop through the object page list cleaning as necessary.
595df8bae1dSRodney W. Grimes 	 */
596df8bae1dSRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) {
59726f9a767SRodney W. Grimes 		onqueue = 0;
598df8bae1dSRodney W. Grimes 		if ((start == end || p->offset >= start && p->offset < end) &&
599df8bae1dSRodney W. Grimes 		    !(p->flags & PG_FICTITIOUS)) {
6000d94caffSDavid Greenman 			vm_page_test_dirty(p);
601df8bae1dSRodney W. Grimes 			/*
6020d94caffSDavid Greenman 			 * Remove the page from any paging queue. This needs
6030d94caffSDavid Greenman 			 * to be done if either we have been explicitly asked
6040d94caffSDavid Greenman 			 * to do so or it is about to be cleaned (see comment
6050d94caffSDavid Greenman 			 * below).
606df8bae1dSRodney W. Grimes 			 */
6070d94caffSDavid Greenman 			if (de_queue || (p->dirty & p->valid)) {
608df8bae1dSRodney W. Grimes 				vm_page_lock_queues();
609df8bae1dSRodney W. Grimes 				if (p->flags & PG_ACTIVE) {
610df8bae1dSRodney W. Grimes 					TAILQ_REMOVE(&vm_page_queue_active,
611df8bae1dSRodney W. Grimes 					    p, pageq);
612df8bae1dSRodney W. Grimes 					p->flags &= ~PG_ACTIVE;
613df8bae1dSRodney W. Grimes 					cnt.v_active_count--;
614df8bae1dSRodney W. Grimes 					onqueue = 1;
615df8bae1dSRodney W. Grimes 				} else if (p->flags & PG_INACTIVE) {
616df8bae1dSRodney W. Grimes 					TAILQ_REMOVE(&vm_page_queue_inactive,
617df8bae1dSRodney W. Grimes 					    p, pageq);
618df8bae1dSRodney W. Grimes 					p->flags &= ~PG_INACTIVE;
619df8bae1dSRodney W. Grimes 					cnt.v_inactive_count--;
620df8bae1dSRodney W. Grimes 					onqueue = -1;
621df8bae1dSRodney W. Grimes 				} else
622df8bae1dSRodney W. Grimes 					onqueue = 0;
623df8bae1dSRodney W. Grimes 				vm_page_unlock_queues();
624df8bae1dSRodney W. Grimes 			}
625df8bae1dSRodney W. Grimes 			/*
626df8bae1dSRodney W. Grimes 			 * To ensure the state of the page doesn't change
6270d94caffSDavid Greenman 			 * during the clean operation we do two things. First
6280d94caffSDavid Greenman 			 * we set the busy bit and write-protect all mappings
6290d94caffSDavid Greenman 			 * to ensure that write accesses to the page block (in
6300d94caffSDavid Greenman 			 * vm_fault).  Second, we remove the page from any
6310d94caffSDavid Greenman 			 * paging queue to foil the pageout daemon
6320d94caffSDavid Greenman 			 * (vm_pageout_scan).
633df8bae1dSRodney W. Grimes 			 */
634df8bae1dSRodney W. Grimes 			pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_READ);
6350d94caffSDavid Greenman 			if (p->dirty & p->valid) {
636df8bae1dSRodney W. Grimes 				p->flags |= PG_BUSY;
637df8bae1dSRodney W. Grimes 				object->paging_in_progress++;
638df8bae1dSRodney W. Grimes 				vm_object_unlock(object);
639df8bae1dSRodney W. Grimes 				/*
6400d94caffSDavid Greenman 				 * XXX if put fails we mark the page as clean
6410d94caffSDavid Greenman 				 * to avoid an infinite loop. Will loose
6420d94caffSDavid Greenman 				 * changes to the page.
643df8bae1dSRodney W. Grimes 				 */
644df8bae1dSRodney W. Grimes 				if (vm_pager_put(object->pager, p, syncio)) {
645df8bae1dSRodney W. Grimes 					printf("%s: pager_put error\n",
646df8bae1dSRodney W. Grimes 					    "vm_object_page_clean");
6470d94caffSDavid Greenman 					p->dirty = 0;
648df8bae1dSRodney W. Grimes 					noerror = FALSE;
649df8bae1dSRodney W. Grimes 				}
650df8bae1dSRodney W. Grimes 				vm_object_lock(object);
651df8bae1dSRodney W. Grimes 				object->paging_in_progress--;
652df8bae1dSRodney W. Grimes 				if (!de_queue && onqueue) {
653df8bae1dSRodney W. Grimes 					vm_page_lock_queues();
654df8bae1dSRodney W. Grimes 					if (onqueue > 0)
655df8bae1dSRodney W. Grimes 						vm_page_activate(p);
656df8bae1dSRodney W. Grimes 					else
657df8bae1dSRodney W. Grimes 						vm_page_deactivate(p);
658df8bae1dSRodney W. Grimes 					vm_page_unlock_queues();
659df8bae1dSRodney W. Grimes 				}
660df8bae1dSRodney W. Grimes 				PAGE_WAKEUP(p);
661df8bae1dSRodney W. Grimes 				goto again;
662df8bae1dSRodney W. Grimes 			}
663df8bae1dSRodney W. Grimes 		}
664df8bae1dSRodney W. Grimes 	}
665df8bae1dSRodney W. Grimes 	return (noerror);
666df8bae1dSRodney W. Grimes }
66726f9a767SRodney W. Grimes #endif
668df8bae1dSRodney W. Grimes 
669df8bae1dSRodney W. Grimes /*
670df8bae1dSRodney W. Grimes  *	vm_object_deactivate_pages
671df8bae1dSRodney W. Grimes  *
672df8bae1dSRodney W. Grimes  *	Deactivate all pages in the specified object.  (Keep its pages
673df8bae1dSRodney W. Grimes  *	in memory even though it is no longer referenced.)
674df8bae1dSRodney W. Grimes  *
675df8bae1dSRodney W. Grimes  *	The object must be locked.
676df8bae1dSRodney W. Grimes  */
677df8bae1dSRodney W. Grimes void
678df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object)
679df8bae1dSRodney W. Grimes 	register vm_object_t object;
680df8bae1dSRodney W. Grimes {
681df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
682df8bae1dSRodney W. Grimes 
683df8bae1dSRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = next) {
684df8bae1dSRodney W. Grimes 		next = p->listq.tqe_next;
685df8bae1dSRodney W. Grimes 		vm_page_lock_queues();
686df8bae1dSRodney W. Grimes 		vm_page_deactivate(p);
687df8bae1dSRodney W. Grimes 		vm_page_unlock_queues();
688df8bae1dSRodney W. Grimes 	}
689df8bae1dSRodney W. Grimes }
690df8bae1dSRodney W. Grimes 
691df8bae1dSRodney W. Grimes /*
692df8bae1dSRodney W. Grimes  *	Trim the object cache to size.
693df8bae1dSRodney W. Grimes  */
694df8bae1dSRodney W. Grimes void
695df8bae1dSRodney W. Grimes vm_object_cache_trim()
696df8bae1dSRodney W. Grimes {
697df8bae1dSRodney W. Grimes 	register vm_object_t object;
698df8bae1dSRodney W. Grimes 
699df8bae1dSRodney W. Grimes 	vm_object_cache_lock();
7000d94caffSDavid Greenman 	while (vm_object_cached > vm_object_cache_max) {
701df8bae1dSRodney W. Grimes 		object = vm_object_cached_list.tqh_first;
702df8bae1dSRodney W. Grimes 		vm_object_cache_unlock();
703df8bae1dSRodney W. Grimes 
704df8bae1dSRodney W. Grimes 		if (object != vm_object_lookup(object->pager))
7058e58bf68SDavid Greenman 			panic("vm_object_cache_trim: I'm sooo confused.");
706df8bae1dSRodney W. Grimes 
707df8bae1dSRodney W. Grimes 		pager_cache(object, FALSE);
708df8bae1dSRodney W. Grimes 
709df8bae1dSRodney W. Grimes 		vm_object_cache_lock();
710df8bae1dSRodney W. Grimes 	}
711df8bae1dSRodney W. Grimes 	vm_object_cache_unlock();
712df8bae1dSRodney W. Grimes }
713df8bae1dSRodney W. Grimes 
71426f9a767SRodney W. Grimes 
715df8bae1dSRodney W. Grimes /*
716df8bae1dSRodney W. Grimes  *	vm_object_pmap_copy:
717df8bae1dSRodney W. Grimes  *
718df8bae1dSRodney W. Grimes  *	Makes all physical pages in the specified
719df8bae1dSRodney W. Grimes  *	object range copy-on-write.  No writeable
720df8bae1dSRodney W. Grimes  *	references to these pages should remain.
721df8bae1dSRodney W. Grimes  *
722df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
723df8bae1dSRodney W. Grimes  */
7240d94caffSDavid Greenman void
7250d94caffSDavid Greenman vm_object_pmap_copy(object, start, end)
726df8bae1dSRodney W. Grimes 	register vm_object_t object;
727df8bae1dSRodney W. Grimes 	register vm_offset_t start;
728df8bae1dSRodney W. Grimes 	register vm_offset_t end;
729df8bae1dSRodney W. Grimes {
730df8bae1dSRodney W. Grimes 	register vm_page_t p;
731df8bae1dSRodney W. Grimes 
732df8bae1dSRodney W. Grimes 	if (object == NULL)
733df8bae1dSRodney W. Grimes 		return;
734df8bae1dSRodney W. Grimes 
735df8bae1dSRodney W. Grimes 	vm_object_lock(object);
736df8bae1dSRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) {
737df8bae1dSRodney W. Grimes 		if ((start <= p->offset) && (p->offset < end)) {
738df8bae1dSRodney W. Grimes 			pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_READ);
739df8bae1dSRodney W. Grimes 			p->flags |= PG_COPYONWRITE;
740df8bae1dSRodney W. Grimes 		}
741df8bae1dSRodney W. Grimes 	}
742df8bae1dSRodney W. Grimes 	vm_object_unlock(object);
743df8bae1dSRodney W. Grimes }
744df8bae1dSRodney W. Grimes 
745df8bae1dSRodney W. Grimes /*
746df8bae1dSRodney W. Grimes  *	vm_object_pmap_remove:
747df8bae1dSRodney W. Grimes  *
748df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
749df8bae1dSRodney W. Grimes  *	object range from all physical maps.
750df8bae1dSRodney W. Grimes  *
751df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
752df8bae1dSRodney W. Grimes  */
75326f9a767SRodney W. Grimes void
75426f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end)
755df8bae1dSRodney W. Grimes 	register vm_object_t object;
756df8bae1dSRodney W. Grimes 	register vm_offset_t start;
757df8bae1dSRodney W. Grimes 	register vm_offset_t end;
758df8bae1dSRodney W. Grimes {
759df8bae1dSRodney W. Grimes 	register vm_page_t p;
760a481f200SDavid Greenman 	int s;
761df8bae1dSRodney W. Grimes 
762df8bae1dSRodney W. Grimes 	if (object == NULL)
763df8bae1dSRodney W. Grimes 		return;
7642fe6e4d7SDavid Greenman 	++object->paging_in_progress;
765df8bae1dSRodney W. Grimes 
766df8bae1dSRodney W. Grimes 	vm_object_lock(object);
76726f9a767SRodney W. Grimes again:
76826f9a767SRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) {
76926f9a767SRodney W. Grimes 		if ((start <= p->offset) && (p->offset < end)) {
770a481f200SDavid Greenman 			s = splhigh();
7710d94caffSDavid Greenman 			if ((p->flags & PG_BUSY) || p->busy) {
77226f9a767SRodney W. Grimes 				p->flags |= PG_WANTED;
77326f9a767SRodney W. Grimes 				tsleep((caddr_t) p, PVM, "vmopmr", 0);
774a481f200SDavid Greenman 				splx(s);
77526f9a767SRodney W. Grimes 				goto again;
77626f9a767SRodney W. Grimes 			}
777a481f200SDavid Greenman 			splx(s);
778df8bae1dSRodney W. Grimes 			pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
77926f9a767SRodney W. Grimes 		}
78026f9a767SRodney W. Grimes 	}
781df8bae1dSRodney W. Grimes 	vm_object_unlock(object);
7822fe6e4d7SDavid Greenman 	--object->paging_in_progress;
783c0503609SDavid Greenman 	if (object->paging_in_progress == 0 && (object->flags & OBJ_PIPWNT)) {
784c0503609SDavid Greenman 		object->flags &= ~OBJ_PIPWNT;
7852fe6e4d7SDavid Greenman 		wakeup((caddr_t) object);
786df8bae1dSRodney W. Grimes 	}
787c0503609SDavid Greenman }
788df8bae1dSRodney W. Grimes 
789df8bae1dSRodney W. Grimes /*
790df8bae1dSRodney W. Grimes  *	vm_object_copy:
791df8bae1dSRodney W. Grimes  *
792df8bae1dSRodney W. Grimes  *	Create a new object which is a copy of an existing
793df8bae1dSRodney W. Grimes  *	object, and mark all of the pages in the existing
794df8bae1dSRodney W. Grimes  *	object 'copy-on-write'.  The new object has one reference.
795df8bae1dSRodney W. Grimes  *	Returns the new object.
796df8bae1dSRodney W. Grimes  *
797df8bae1dSRodney W. Grimes  *	May defer the copy until later if the object is not backed
798df8bae1dSRodney W. Grimes  *	up by a non-default pager.
799df8bae1dSRodney W. Grimes  */
8000d94caffSDavid Greenman void
8010d94caffSDavid Greenman vm_object_copy(src_object, src_offset, size,
802df8bae1dSRodney W. Grimes     dst_object, dst_offset, src_needs_copy)
803df8bae1dSRodney W. Grimes 	register vm_object_t src_object;
804df8bae1dSRodney W. Grimes 	vm_offset_t src_offset;
805df8bae1dSRodney W. Grimes 	vm_size_t size;
806df8bae1dSRodney W. Grimes 	vm_object_t *dst_object;/* OUT */
807df8bae1dSRodney W. Grimes 	vm_offset_t *dst_offset;/* OUT */
808df8bae1dSRodney W. Grimes 	boolean_t *src_needs_copy;	/* OUT */
809df8bae1dSRodney W. Grimes {
810df8bae1dSRodney W. Grimes 	register vm_object_t new_copy;
811df8bae1dSRodney W. Grimes 	register vm_object_t old_copy;
812df8bae1dSRodney W. Grimes 	vm_offset_t new_start, new_end;
813df8bae1dSRodney W. Grimes 
814df8bae1dSRodney W. Grimes 	register vm_page_t p;
815df8bae1dSRodney W. Grimes 
816df8bae1dSRodney W. Grimes 	if (src_object == NULL) {
817df8bae1dSRodney W. Grimes 		/*
818df8bae1dSRodney W. Grimes 		 * Nothing to copy
819df8bae1dSRodney W. Grimes 		 */
820df8bae1dSRodney W. Grimes 		*dst_object = NULL;
821df8bae1dSRodney W. Grimes 		*dst_offset = 0;
822df8bae1dSRodney W. Grimes 		*src_needs_copy = FALSE;
823df8bae1dSRodney W. Grimes 		return;
824df8bae1dSRodney W. Grimes 	}
825df8bae1dSRodney W. Grimes 	/*
8260d94caffSDavid Greenman 	 * If the object's pager is null_pager or the default pager, we don't
8270d94caffSDavid Greenman 	 * have to make a copy of it.  Instead, we set the needs copy flag and
828df8bae1dSRodney W. Grimes 	 * make a shadow later.
829df8bae1dSRodney W. Grimes 	 */
830df8bae1dSRodney W. Grimes 
831df8bae1dSRodney W. Grimes 	vm_object_lock(src_object);
83226f9a767SRodney W. Grimes 
83326f9a767SRodney W. Grimes 	/*
83426f9a767SRodney W. Grimes 	 * Try to collapse the object before copying it.
83526f9a767SRodney W. Grimes 	 */
83626f9a767SRodney W. Grimes 
83726f9a767SRodney W. Grimes 	vm_object_collapse(src_object);
83826f9a767SRodney W. Grimes 
839df8bae1dSRodney W. Grimes 	if (src_object->pager == NULL ||
840df8bae1dSRodney W. Grimes 	    (src_object->flags & OBJ_INTERNAL)) {
841df8bae1dSRodney W. Grimes 
842df8bae1dSRodney W. Grimes 		/*
843df8bae1dSRodney W. Grimes 		 * Make another reference to the object
844df8bae1dSRodney W. Grimes 		 */
845df8bae1dSRodney W. Grimes 		src_object->ref_count++;
846df8bae1dSRodney W. Grimes 
847df8bae1dSRodney W. Grimes 		/*
848df8bae1dSRodney W. Grimes 		 * Mark all of the pages copy-on-write.
849df8bae1dSRodney W. Grimes 		 */
850df8bae1dSRodney W. Grimes 		for (p = src_object->memq.tqh_first; p; p = p->listq.tqe_next)
851df8bae1dSRodney W. Grimes 			if (src_offset <= p->offset &&
852df8bae1dSRodney W. Grimes 			    p->offset < src_offset + size)
853df8bae1dSRodney W. Grimes 				p->flags |= PG_COPYONWRITE;
854df8bae1dSRodney W. Grimes 		vm_object_unlock(src_object);
855df8bae1dSRodney W. Grimes 
856df8bae1dSRodney W. Grimes 		*dst_object = src_object;
857df8bae1dSRodney W. Grimes 		*dst_offset = src_offset;
858df8bae1dSRodney W. Grimes 
859df8bae1dSRodney W. Grimes 		/*
860df8bae1dSRodney W. Grimes 		 * Must make a shadow when write is desired
861df8bae1dSRodney W. Grimes 		 */
862df8bae1dSRodney W. Grimes 		*src_needs_copy = TRUE;
863df8bae1dSRodney W. Grimes 		return;
864df8bae1dSRodney W. Grimes 	}
865df8bae1dSRodney W. Grimes 	/*
8660d94caffSDavid Greenman 	 * If the object has a pager, the pager wants to see all of the
8670d94caffSDavid Greenman 	 * changes.  We need a copy-object for the changed pages.
868df8bae1dSRodney W. Grimes 	 *
8690d94caffSDavid Greenman 	 * If there is a copy-object, and it is empty, no changes have been made
8700d94caffSDavid Greenman 	 * to the object since the copy-object was made.  We can use the same
8710d94caffSDavid Greenman 	 * copy- object.
872df8bae1dSRodney W. Grimes 	 */
873df8bae1dSRodney W. Grimes 
874df8bae1dSRodney W. Grimes Retry1:
875df8bae1dSRodney W. Grimes 	old_copy = src_object->copy;
876df8bae1dSRodney W. Grimes 	if (old_copy != NULL) {
877df8bae1dSRodney W. Grimes 		/*
878df8bae1dSRodney W. Grimes 		 * Try to get the locks (out of order)
879df8bae1dSRodney W. Grimes 		 */
880df8bae1dSRodney W. Grimes 		if (!vm_object_lock_try(old_copy)) {
881df8bae1dSRodney W. Grimes 			vm_object_unlock(src_object);
882df8bae1dSRodney W. Grimes 
883df8bae1dSRodney W. Grimes 			/* should spin a bit here... */
8840d94caffSDavid Greenman 			tsleep((caddr_t) old_copy, PVM, "cpylck", 1);
885df8bae1dSRodney W. Grimes 			vm_object_lock(src_object);
886df8bae1dSRodney W. Grimes 			goto Retry1;
887df8bae1dSRodney W. Grimes 		}
888df8bae1dSRodney W. Grimes 		if (old_copy->resident_page_count == 0 &&
889df8bae1dSRodney W. Grimes 		    old_copy->pager == NULL) {
890df8bae1dSRodney W. Grimes 			/*
8910d94caffSDavid Greenman 			 * Return another reference to the existing
8920d94caffSDavid Greenman 			 * copy-object.
893df8bae1dSRodney W. Grimes 			 */
894df8bae1dSRodney W. Grimes 			old_copy->ref_count++;
895df8bae1dSRodney W. Grimes 			vm_object_unlock(old_copy);
896df8bae1dSRodney W. Grimes 			vm_object_unlock(src_object);
897df8bae1dSRodney W. Grimes 			*dst_object = old_copy;
898df8bae1dSRodney W. Grimes 			*dst_offset = src_offset;
899df8bae1dSRodney W. Grimes 			*src_needs_copy = FALSE;
900df8bae1dSRodney W. Grimes 			return;
901df8bae1dSRodney W. Grimes 		}
902df8bae1dSRodney W. Grimes 		vm_object_unlock(old_copy);
903df8bae1dSRodney W. Grimes 	}
904df8bae1dSRodney W. Grimes 	vm_object_unlock(src_object);
905df8bae1dSRodney W. Grimes 
906df8bae1dSRodney W. Grimes 	/*
9070d94caffSDavid Greenman 	 * If the object has a pager, the pager wants to see all of the
9080d94caffSDavid Greenman 	 * changes.  We must make a copy-object and put the changed pages
9090d94caffSDavid Greenman 	 * there.
910df8bae1dSRodney W. Grimes 	 *
9110d94caffSDavid Greenman 	 * The copy-object is always made large enough to completely shadow the
9120d94caffSDavid Greenman 	 * original object, since it may have several users who want to shadow
913df8bae1dSRodney W. Grimes 	 * the original object at different points.
914df8bae1dSRodney W. Grimes 	 */
915df8bae1dSRodney W. Grimes 
916df8bae1dSRodney W. Grimes 	new_copy = vm_object_allocate(src_object->size);
917df8bae1dSRodney W. Grimes 
918df8bae1dSRodney W. Grimes Retry2:
919df8bae1dSRodney W. Grimes 	vm_object_lock(src_object);
920df8bae1dSRodney W. Grimes 	/*
921df8bae1dSRodney W. Grimes 	 * Copy object may have changed while we were unlocked
922df8bae1dSRodney W. Grimes 	 */
923df8bae1dSRodney W. Grimes 	old_copy = src_object->copy;
924df8bae1dSRodney W. Grimes 	if (old_copy != NULL) {
925df8bae1dSRodney W. Grimes 		/*
926df8bae1dSRodney W. Grimes 		 * Try to get the locks (out of order)
927df8bae1dSRodney W. Grimes 		 */
928df8bae1dSRodney W. Grimes 		if (!vm_object_lock_try(old_copy)) {
929df8bae1dSRodney W. Grimes 			vm_object_unlock(src_object);
9300d94caffSDavid Greenman 			tsleep((caddr_t) old_copy, PVM, "cpylck", 1);
931df8bae1dSRodney W. Grimes 			goto Retry2;
932df8bae1dSRodney W. Grimes 		}
933df8bae1dSRodney W. Grimes 		/*
934df8bae1dSRodney W. Grimes 		 * Consistency check
935df8bae1dSRodney W. Grimes 		 */
936df8bae1dSRodney W. Grimes 		if (old_copy->shadow != src_object ||
937df8bae1dSRodney W. Grimes 		    old_copy->shadow_offset != (vm_offset_t) 0)
938df8bae1dSRodney W. Grimes 			panic("vm_object_copy: copy/shadow inconsistency");
939df8bae1dSRodney W. Grimes 
940df8bae1dSRodney W. Grimes 		/*
9410d94caffSDavid Greenman 		 * Make the old copy-object shadow the new one. It will
9420d94caffSDavid Greenman 		 * receive no more pages from the original object.
943df8bae1dSRodney W. Grimes 		 */
944df8bae1dSRodney W. Grimes 
945df8bae1dSRodney W. Grimes 		src_object->ref_count--;	/* remove ref. from old_copy */
9462fe6e4d7SDavid Greenman 		if (old_copy->shadow)
9472fe6e4d7SDavid Greenman 			TAILQ_REMOVE(&old_copy->shadow->reverse_shadow_head, old_copy, reverse_shadow_list);
948df8bae1dSRodney W. Grimes 		old_copy->shadow = new_copy;
9492fe6e4d7SDavid Greenman 		TAILQ_INSERT_TAIL(&old_copy->shadow->reverse_shadow_head, old_copy, reverse_shadow_list);
9500d94caffSDavid Greenman 		new_copy->ref_count++;	/* locking not needed - we have the
9510d94caffSDavid Greenman 					 * only pointer */
952df8bae1dSRodney W. Grimes 		vm_object_unlock(old_copy);	/* done with old_copy */
953df8bae1dSRodney W. Grimes 	}
954df8bae1dSRodney W. Grimes 	new_start = (vm_offset_t) 0;	/* always shadow original at 0 */
955df8bae1dSRodney W. Grimes 	new_end = (vm_offset_t) new_copy->size;	/* for the whole object */
956df8bae1dSRodney W. Grimes 
957df8bae1dSRodney W. Grimes 	/*
958df8bae1dSRodney W. Grimes 	 * Point the new copy at the existing object.
959df8bae1dSRodney W. Grimes 	 */
960df8bae1dSRodney W. Grimes 
961df8bae1dSRodney W. Grimes 	new_copy->shadow = src_object;
9622fe6e4d7SDavid Greenman 	TAILQ_INSERT_TAIL(&new_copy->shadow->reverse_shadow_head, new_copy, reverse_shadow_list);
963df8bae1dSRodney W. Grimes 	new_copy->shadow_offset = new_start;
964df8bae1dSRodney W. Grimes 	src_object->ref_count++;
965df8bae1dSRodney W. Grimes 	src_object->copy = new_copy;
966df8bae1dSRodney W. Grimes 
967df8bae1dSRodney W. Grimes 	/*
9680d94caffSDavid Greenman 	 * Mark all the affected pages of the existing object copy-on-write.
969df8bae1dSRodney W. Grimes 	 */
970df8bae1dSRodney W. Grimes 	for (p = src_object->memq.tqh_first; p != NULL; p = p->listq.tqe_next)
971df8bae1dSRodney W. Grimes 		if ((new_start <= p->offset) && (p->offset < new_end))
972df8bae1dSRodney W. Grimes 			p->flags |= PG_COPYONWRITE;
973df8bae1dSRodney W. Grimes 
974df8bae1dSRodney W. Grimes 	vm_object_unlock(src_object);
975df8bae1dSRodney W. Grimes 
976df8bae1dSRodney W. Grimes 	*dst_object = new_copy;
977df8bae1dSRodney W. Grimes 	*dst_offset = src_offset - new_start;
978df8bae1dSRodney W. Grimes 	*src_needs_copy = FALSE;
979df8bae1dSRodney W. Grimes }
980df8bae1dSRodney W. Grimes 
981df8bae1dSRodney W. Grimes /*
982df8bae1dSRodney W. Grimes  *	vm_object_shadow:
983df8bae1dSRodney W. Grimes  *
984df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
985df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
986df8bae1dSRodney W. Grimes  *	object reference is deallocated.
987df8bae1dSRodney W. Grimes  *
988df8bae1dSRodney W. Grimes  *	The new object and offset into that object
989df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
990df8bae1dSRodney W. Grimes  */
991df8bae1dSRodney W. Grimes 
99226f9a767SRodney W. Grimes void
99326f9a767SRodney W. Grimes vm_object_shadow(object, offset, length)
994df8bae1dSRodney W. Grimes 	vm_object_t *object;	/* IN/OUT */
995df8bae1dSRodney W. Grimes 	vm_offset_t *offset;	/* IN/OUT */
996df8bae1dSRodney W. Grimes 	vm_size_t length;
997df8bae1dSRodney W. Grimes {
998df8bae1dSRodney W. Grimes 	register vm_object_t source;
999df8bae1dSRodney W. Grimes 	register vm_object_t result;
1000df8bae1dSRodney W. Grimes 
1001df8bae1dSRodney W. Grimes 	source = *object;
1002df8bae1dSRodney W. Grimes 
1003df8bae1dSRodney W. Grimes 	/*
1004df8bae1dSRodney W. Grimes 	 * Allocate a new object with the given length
1005df8bae1dSRodney W. Grimes 	 */
1006df8bae1dSRodney W. Grimes 
1007df8bae1dSRodney W. Grimes 	if ((result = vm_object_allocate(length)) == NULL)
1008df8bae1dSRodney W. Grimes 		panic("vm_object_shadow: no object for shadowing");
1009df8bae1dSRodney W. Grimes 
1010df8bae1dSRodney W. Grimes 	/*
10110d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
10120d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
10130d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
10140d94caffSDavid Greenman 	 * of reference count.
1015df8bae1dSRodney W. Grimes 	 */
1016df8bae1dSRodney W. Grimes 	result->shadow = source;
10177b18a718SDavid Greenman 	if (source)
10182fe6e4d7SDavid Greenman 		TAILQ_INSERT_TAIL(&result->shadow->reverse_shadow_head, result, reverse_shadow_list);
1019df8bae1dSRodney W. Grimes 
1020df8bae1dSRodney W. Grimes 	/*
10210d94caffSDavid Greenman 	 * Store the offset into the source object, and fix up the offset into
10220d94caffSDavid Greenman 	 * the new object.
1023df8bae1dSRodney W. Grimes 	 */
1024df8bae1dSRodney W. Grimes 
1025df8bae1dSRodney W. Grimes 	result->shadow_offset = *offset;
1026df8bae1dSRodney W. Grimes 
1027df8bae1dSRodney W. Grimes 	/*
1028df8bae1dSRodney W. Grimes 	 * Return the new things
1029df8bae1dSRodney W. Grimes 	 */
1030df8bae1dSRodney W. Grimes 
1031df8bae1dSRodney W. Grimes 	*offset = 0;
1032df8bae1dSRodney W. Grimes 	*object = result;
1033df8bae1dSRodney W. Grimes }
1034df8bae1dSRodney W. Grimes 
1035df8bae1dSRodney W. Grimes /*
1036df8bae1dSRodney W. Grimes  *	Set the specified object's pager to the specified pager.
1037df8bae1dSRodney W. Grimes  */
1038df8bae1dSRodney W. Grimes 
103926f9a767SRodney W. Grimes void
104026f9a767SRodney W. Grimes vm_object_setpager(object, pager, paging_offset,
1041df8bae1dSRodney W. Grimes     read_only)
1042df8bae1dSRodney W. Grimes 	vm_object_t object;
1043df8bae1dSRodney W. Grimes 	vm_pager_t pager;
1044df8bae1dSRodney W. Grimes 	vm_offset_t paging_offset;
1045df8bae1dSRodney W. Grimes 	boolean_t read_only;
1046df8bae1dSRodney W. Grimes {
1047df8bae1dSRodney W. Grimes 	vm_object_lock(object);	/* XXX ? */
104826f9a767SRodney W. Grimes 	if (object->pager && object->pager != pager) {
104926f9a767SRodney W. Grimes 		panic("!!!pager already allocated!!!\n");
105026f9a767SRodney W. Grimes 	}
1051df8bae1dSRodney W. Grimes 	object->pager = pager;
1052df8bae1dSRodney W. Grimes 	object->paging_offset = paging_offset;
1053df8bae1dSRodney W. Grimes 	vm_object_unlock(object);	/* XXX ? */
1054df8bae1dSRodney W. Grimes }
1055df8bae1dSRodney W. Grimes 
1056df8bae1dSRodney W. Grimes /*
1057df8bae1dSRodney W. Grimes  *	vm_object_hash hashes the pager/id pair.
1058df8bae1dSRodney W. Grimes  */
1059df8bae1dSRodney W. Grimes 
1060df8bae1dSRodney W. Grimes #define vm_object_hash(pager) \
106126f9a767SRodney W. Grimes 	(((unsigned)pager >> 5)%VM_OBJECT_HASH_COUNT)
1062df8bae1dSRodney W. Grimes 
1063df8bae1dSRodney W. Grimes /*
1064df8bae1dSRodney W. Grimes  *	vm_object_lookup looks in the object cache for an object with the
1065df8bae1dSRodney W. Grimes  *	specified pager and paging id.
1066df8bae1dSRodney W. Grimes  */
1067df8bae1dSRodney W. Grimes 
10680d94caffSDavid Greenman vm_object_t
10690d94caffSDavid Greenman vm_object_lookup(pager)
1070df8bae1dSRodney W. Grimes 	vm_pager_t pager;
1071df8bae1dSRodney W. Grimes {
1072df8bae1dSRodney W. Grimes 	register vm_object_hash_entry_t entry;
1073df8bae1dSRodney W. Grimes 	vm_object_t object;
1074df8bae1dSRodney W. Grimes 
1075a08a17a3SDavid Greenman 	cnt.v_lookups++;
1076df8bae1dSRodney W. Grimes 	vm_object_cache_lock();
1077df8bae1dSRodney W. Grimes 
1078df8bae1dSRodney W. Grimes 	for (entry = vm_object_hashtable[vm_object_hash(pager)].tqh_first;
1079df8bae1dSRodney W. Grimes 	    entry != NULL;
1080df8bae1dSRodney W. Grimes 	    entry = entry->hash_links.tqe_next) {
1081df8bae1dSRodney W. Grimes 		object = entry->object;
1082df8bae1dSRodney W. Grimes 		if (object->pager == pager) {
1083df8bae1dSRodney W. Grimes 			vm_object_lock(object);
1084df8bae1dSRodney W. Grimes 			if (object->ref_count == 0) {
1085df8bae1dSRodney W. Grimes 				TAILQ_REMOVE(&vm_object_cached_list, object,
1086df8bae1dSRodney W. Grimes 				    cached_list);
1087df8bae1dSRodney W. Grimes 				vm_object_cached--;
1088df8bae1dSRodney W. Grimes 			}
1089df8bae1dSRodney W. Grimes 			object->ref_count++;
1090df8bae1dSRodney W. Grimes 			vm_object_unlock(object);
1091df8bae1dSRodney W. Grimes 			vm_object_cache_unlock();
1092a08a17a3SDavid Greenman 			cnt.v_hits++;
1093df8bae1dSRodney W. Grimes 			return (object);
1094df8bae1dSRodney W. Grimes 		}
1095df8bae1dSRodney W. Grimes 	}
1096df8bae1dSRodney W. Grimes 
1097df8bae1dSRodney W. Grimes 	vm_object_cache_unlock();
1098df8bae1dSRodney W. Grimes 	return (NULL);
1099df8bae1dSRodney W. Grimes }
1100df8bae1dSRodney W. Grimes 
1101df8bae1dSRodney W. Grimes /*
1102df8bae1dSRodney W. Grimes  *	vm_object_enter enters the specified object/pager/id into
1103df8bae1dSRodney W. Grimes  *	the hash table.
1104df8bae1dSRodney W. Grimes  */
1105df8bae1dSRodney W. Grimes 
11060d94caffSDavid Greenman void
11070d94caffSDavid Greenman vm_object_enter(object, pager)
1108df8bae1dSRodney W. Grimes 	vm_object_t object;
1109df8bae1dSRodney W. Grimes 	vm_pager_t pager;
1110df8bae1dSRodney W. Grimes {
1111df8bae1dSRodney W. Grimes 	struct vm_object_hash_head *bucket;
1112df8bae1dSRodney W. Grimes 	register vm_object_hash_entry_t entry;
1113df8bae1dSRodney W. Grimes 
1114df8bae1dSRodney W. Grimes 	/*
11150d94caffSDavid Greenman 	 * We don't cache null objects, and we can't cache objects with the
11160d94caffSDavid Greenman 	 * null pager.
1117df8bae1dSRodney W. Grimes 	 */
1118df8bae1dSRodney W. Grimes 
1119df8bae1dSRodney W. Grimes 	if (object == NULL)
1120df8bae1dSRodney W. Grimes 		return;
1121df8bae1dSRodney W. Grimes 	if (pager == NULL)
1122df8bae1dSRodney W. Grimes 		return;
1123df8bae1dSRodney W. Grimes 
1124df8bae1dSRodney W. Grimes 	bucket = &vm_object_hashtable[vm_object_hash(pager)];
1125df8bae1dSRodney W. Grimes 	entry = (vm_object_hash_entry_t)
1126df8bae1dSRodney W. Grimes 	    malloc((u_long) sizeof *entry, M_VMOBJHASH, M_WAITOK);
1127df8bae1dSRodney W. Grimes 	entry->object = object;
1128df8bae1dSRodney W. Grimes 
1129df8bae1dSRodney W. Grimes 	vm_object_cache_lock();
1130df8bae1dSRodney W. Grimes 	TAILQ_INSERT_TAIL(bucket, entry, hash_links);
1131df8bae1dSRodney W. Grimes 	vm_object_cache_unlock();
1132df8bae1dSRodney W. Grimes }
1133df8bae1dSRodney W. Grimes 
1134df8bae1dSRodney W. Grimes /*
1135df8bae1dSRodney W. Grimes  *	vm_object_remove:
1136df8bae1dSRodney W. Grimes  *
1137df8bae1dSRodney W. Grimes  *	Remove the pager from the hash table.
1138df8bae1dSRodney W. Grimes  *	Note:  This assumes that the object cache
1139df8bae1dSRodney W. Grimes  *	is locked.  XXX this should be fixed
1140df8bae1dSRodney W. Grimes  *	by reorganizing vm_object_deallocate.
1141df8bae1dSRodney W. Grimes  */
1142df8bae1dSRodney W. Grimes void
1143df8bae1dSRodney W. Grimes vm_object_remove(pager)
1144df8bae1dSRodney W. Grimes 	register vm_pager_t pager;
1145df8bae1dSRodney W. Grimes {
1146df8bae1dSRodney W. Grimes 	struct vm_object_hash_head *bucket;
1147df8bae1dSRodney W. Grimes 	register vm_object_hash_entry_t entry;
1148df8bae1dSRodney W. Grimes 	register vm_object_t object;
1149df8bae1dSRodney W. Grimes 
1150df8bae1dSRodney W. Grimes 	bucket = &vm_object_hashtable[vm_object_hash(pager)];
1151df8bae1dSRodney W. Grimes 
1152df8bae1dSRodney W. Grimes 	for (entry = bucket->tqh_first;
1153df8bae1dSRodney W. Grimes 	    entry != NULL;
1154df8bae1dSRodney W. Grimes 	    entry = entry->hash_links.tqe_next) {
1155df8bae1dSRodney W. Grimes 		object = entry->object;
1156df8bae1dSRodney W. Grimes 		if (object->pager == pager) {
1157df8bae1dSRodney W. Grimes 			TAILQ_REMOVE(bucket, entry, hash_links);
1158df8bae1dSRodney W. Grimes 			free((caddr_t) entry, M_VMOBJHASH);
1159df8bae1dSRodney W. Grimes 			break;
1160df8bae1dSRodney W. Grimes 		}
1161df8bae1dSRodney W. Grimes 	}
1162df8bae1dSRodney W. Grimes }
1163df8bae1dSRodney W. Grimes 
11642fe6e4d7SDavid Greenman /*
11652fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
11662fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
11672fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
11682fe6e4d7SDavid Greenman  */
11692fe6e4d7SDavid Greenman static void
11702fe6e4d7SDavid Greenman vm_object_qcollapse(object)
11712fe6e4d7SDavid Greenman 	register vm_object_t object;
11722fe6e4d7SDavid Greenman {
11732fe6e4d7SDavid Greenman 	register vm_object_t backing_object;
11742fe6e4d7SDavid Greenman 	register vm_offset_t backing_offset, new_offset;
11752fe6e4d7SDavid Greenman 	register vm_page_t p, pp;
11762fe6e4d7SDavid Greenman 	register vm_size_t size;
11772fe6e4d7SDavid Greenman 
11782fe6e4d7SDavid Greenman 	backing_object = object->shadow;
11792fe6e4d7SDavid Greenman 	if (!backing_object)
11802fe6e4d7SDavid Greenman 		return;
11812fe6e4d7SDavid Greenman 	if ((backing_object->flags & OBJ_INTERNAL) == 0)
11822fe6e4d7SDavid Greenman 		return;
11832fe6e4d7SDavid Greenman 	if (backing_object->shadow != NULL &&
11842fe6e4d7SDavid Greenman 	    backing_object->shadow->copy == backing_object)
11852fe6e4d7SDavid Greenman 		return;
11862fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
11872fe6e4d7SDavid Greenman 		return;
11882fe6e4d7SDavid Greenman 
1189010cf3b9SDavid Greenman 	backing_object->ref_count += 2;
1190010cf3b9SDavid Greenman 
11912fe6e4d7SDavid Greenman 	backing_offset = object->shadow_offset;
11922fe6e4d7SDavid Greenman 	size = object->size;
11932fe6e4d7SDavid Greenman 	p = backing_object->memq.tqh_first;
11942fe6e4d7SDavid Greenman 	while (p) {
11952fe6e4d7SDavid Greenman 		vm_page_t next;
11960d94caffSDavid Greenman 
11972fe6e4d7SDavid Greenman 		next = p->listq.tqe_next;
11980d94caffSDavid Greenman 		if ((p->flags & (PG_BUSY | PG_FICTITIOUS | PG_CACHE)) ||
11990d94caffSDavid Greenman 		    !p->valid || p->hold_count || p->wire_count || p->busy || p->bmapped) {
12002fe6e4d7SDavid Greenman 			p = next;
12012fe6e4d7SDavid Greenman 			continue;
12022fe6e4d7SDavid Greenman 		}
12030d94caffSDavid Greenman 		pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
12042fe6e4d7SDavid Greenman 		new_offset = (p->offset - backing_offset);
12052fe6e4d7SDavid Greenman 		if (p->offset < backing_offset ||
12062fe6e4d7SDavid Greenman 		    new_offset >= size) {
12072fe6e4d7SDavid Greenman 			if (backing_object->pager)
12080d94caffSDavid Greenman 				swap_pager_freespace(backing_object->pager,
12090d94caffSDavid Greenman 				    backing_object->paging_offset + p->offset, PAGE_SIZE);
12100d94caffSDavid Greenman 			vm_page_lock_queues();
12112fe6e4d7SDavid Greenman 			vm_page_free(p);
12122fe6e4d7SDavid Greenman 			vm_page_unlock_queues();
12132fe6e4d7SDavid Greenman 		} else {
12142fe6e4d7SDavid Greenman 			pp = vm_page_lookup(object, new_offset);
12152fe6e4d7SDavid Greenman 			if (pp != NULL || (object->pager && vm_pager_has_page(object->pager,
12162fe6e4d7SDavid Greenman 				    object->paging_offset + new_offset))) {
12172fe6e4d7SDavid Greenman 				if (backing_object->pager)
12180d94caffSDavid Greenman 					swap_pager_freespace(backing_object->pager,
12190d94caffSDavid Greenman 					    backing_object->paging_offset + p->offset, PAGE_SIZE);
12200d94caffSDavid Greenman 				vm_page_lock_queues();
12212fe6e4d7SDavid Greenman 				vm_page_free(p);
12222fe6e4d7SDavid Greenman 				vm_page_unlock_queues();
12232fe6e4d7SDavid Greenman 			} else {
1224a1f6d91cSDavid Greenman 				if( backing_object->pager)
1225a1f6d91cSDavid Greenman 					swap_pager_freespace(backing_object->pager,
1226a1f6d91cSDavid Greenman 					    backing_object->paging_offset + p->offset, PAGE_SIZE);
12272fe6e4d7SDavid Greenman 				vm_page_rename(p, object, new_offset);
12289b4814bbSDavid Greenman 				p->dirty = VM_PAGE_BITS_ALL;
12292fe6e4d7SDavid Greenman 			}
12302fe6e4d7SDavid Greenman 		}
12312fe6e4d7SDavid Greenman 		p = next;
12322fe6e4d7SDavid Greenman 	}
1233010cf3b9SDavid Greenman 	backing_object->ref_count -= 2;
12342fe6e4d7SDavid Greenman }
12352fe6e4d7SDavid Greenman 
1236df8bae1dSRodney W. Grimes boolean_t vm_object_collapse_allowed = TRUE;
12370d94caffSDavid Greenman 
1238df8bae1dSRodney W. Grimes /*
1239df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1240df8bae1dSRodney W. Grimes  *
1241df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1242df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1243df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1244df8bae1dSRodney W. Grimes  *
1245df8bae1dSRodney W. Grimes  *	Requires that the object be locked and the page
1246df8bae1dSRodney W. Grimes  *	queues be unlocked.
1247df8bae1dSRodney W. Grimes  *
124826f9a767SRodney W. Grimes  *	This routine has significant changes by John S. Dyson
124926f9a767SRodney W. Grimes  *	to fix some swap memory leaks.  18 Dec 93
125026f9a767SRodney W. Grimes  *
1251df8bae1dSRodney W. Grimes  */
125226f9a767SRodney W. Grimes void
125326f9a767SRodney W. Grimes vm_object_collapse(object)
1254df8bae1dSRodney W. Grimes 	register vm_object_t object;
1255df8bae1dSRodney W. Grimes 
1256df8bae1dSRodney W. Grimes {
1257df8bae1dSRodney W. Grimes 	register vm_object_t backing_object;
1258df8bae1dSRodney W. Grimes 	register vm_offset_t backing_offset;
1259df8bae1dSRodney W. Grimes 	register vm_size_t size;
1260df8bae1dSRodney W. Grimes 	register vm_offset_t new_offset;
1261df8bae1dSRodney W. Grimes 	register vm_page_t p, pp;
1262df8bae1dSRodney W. Grimes 
1263df8bae1dSRodney W. Grimes 	if (!vm_object_collapse_allowed)
1264df8bae1dSRodney W. Grimes 		return;
1265df8bae1dSRodney W. Grimes 
1266df8bae1dSRodney W. Grimes 	while (TRUE) {
1267df8bae1dSRodney W. Grimes 		/*
1268df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1269df8bae1dSRodney W. Grimes 		 *
12700d94caffSDavid Greenman 		 * The object exists and no pages in it are currently being paged
12710d94caffSDavid Greenman 		 * out.
1272df8bae1dSRodney W. Grimes 		 */
12732fe6e4d7SDavid Greenman 		if (object == NULL)
1274df8bae1dSRodney W. Grimes 			return;
1275df8bae1dSRodney W. Grimes 
1276b9921222SDavid Greenman 		/*
1277b9921222SDavid Greenman 		 * Make sure there is a backing object.
1278b9921222SDavid Greenman 		 */
1279df8bae1dSRodney W. Grimes 		if ((backing_object = object->shadow) == NULL)
1280df8bae1dSRodney W. Grimes 			return;
1281df8bae1dSRodney W. Grimes 
12829b4814bbSDavid Greenman 		if ((object->flags & OBJ_DEAD) || (backing_object->flags & OBJ_DEAD))
12839b4814bbSDavid Greenman 			return;
12849b4814bbSDavid Greenman 
1285b9921222SDavid Greenman 		if (object->paging_in_progress != 0) {
1286b9921222SDavid Greenman 			if (backing_object) {
1287b9921222SDavid Greenman 				if (vm_object_lock_try(backing_object)) {
1288b9921222SDavid Greenman 					vm_object_qcollapse(object);
1289b9921222SDavid Greenman 					vm_object_unlock(backing_object);
1290b9921222SDavid Greenman 				}
1291b9921222SDavid Greenman 			}
1292b9921222SDavid Greenman 			return;
1293b9921222SDavid Greenman 		}
1294b9921222SDavid Greenman 
1295df8bae1dSRodney W. Grimes 		vm_object_lock(backing_object);
1296df8bae1dSRodney W. Grimes 		/*
12970d94caffSDavid Greenman 		 * ... The backing object is not read_only, and no pages in
12980d94caffSDavid Greenman 		 * the backing object are currently being paged out. The
12990d94caffSDavid Greenman 		 * backing object is internal.
1300df8bae1dSRodney W. Grimes 		 */
1301df8bae1dSRodney W. Grimes 
1302df8bae1dSRodney W. Grimes 		if ((backing_object->flags & OBJ_INTERNAL) == 0 ||
1303df8bae1dSRodney W. Grimes 		    backing_object->paging_in_progress != 0) {
13042fe6e4d7SDavid Greenman 			vm_object_qcollapse(object);
1305a1f6d91cSDavid Greenman 			vm_object_unlock(backing_object);
1306df8bae1dSRodney W. Grimes 			return;
1307df8bae1dSRodney W. Grimes 		}
1308df8bae1dSRodney W. Grimes 		/*
13090d94caffSDavid Greenman 		 * The backing object can't be a copy-object: the
13100d94caffSDavid Greenman 		 * shadow_offset for the copy-object must stay as 0.
13110d94caffSDavid Greenman 		 * Furthermore (for the 'we have all the pages' case), if we
13120d94caffSDavid Greenman 		 * bypass backing_object and just shadow the next object in
13130d94caffSDavid Greenman 		 * the chain, old pages from that object would then have to be
13140d94caffSDavid Greenman 		 * copied BOTH into the (former) backing_object and into the
1315df8bae1dSRodney W. Grimes 		 * parent object.
1316df8bae1dSRodney W. Grimes 		 */
1317df8bae1dSRodney W. Grimes 		if (backing_object->shadow != NULL &&
131826f9a767SRodney W. Grimes 		    backing_object->shadow->copy == backing_object) {
1319df8bae1dSRodney W. Grimes 			vm_object_unlock(backing_object);
1320df8bae1dSRodney W. Grimes 			return;
1321df8bae1dSRodney W. Grimes 		}
1322df8bae1dSRodney W. Grimes 		/*
132326f9a767SRodney W. Grimes 		 * we can deal only with the swap pager
132426f9a767SRodney W. Grimes 		 */
132526f9a767SRodney W. Grimes 		if ((object->pager &&
132626f9a767SRodney W. Grimes 			object->pager->pg_type != PG_SWAP) ||
132726f9a767SRodney W. Grimes 		    (backing_object->pager &&
132826f9a767SRodney W. Grimes 			backing_object->pager->pg_type != PG_SWAP)) {
132926f9a767SRodney W. Grimes 			vm_object_unlock(backing_object);
133026f9a767SRodney W. Grimes 			return;
133126f9a767SRodney W. Grimes 		}
133226f9a767SRodney W. Grimes 		/*
13330d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
13340d94caffSDavid Greenman 		 * the parent is the only reference to it) or (perhaps) remove
13350d94caffSDavid Greenman 		 * the parent's reference to it.
1336df8bae1dSRodney W. Grimes 		 */
1337df8bae1dSRodney W. Grimes 
1338df8bae1dSRodney W. Grimes 		backing_offset = object->shadow_offset;
1339df8bae1dSRodney W. Grimes 		size = object->size;
1340df8bae1dSRodney W. Grimes 
1341df8bae1dSRodney W. Grimes 		/*
13420d94caffSDavid Greenman 		 * If there is exactly one reference to the backing object, we
13430d94caffSDavid Greenman 		 * can collapse it into the parent.
1344df8bae1dSRodney W. Grimes 		 */
1345df8bae1dSRodney W. Grimes 
1346df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1347df8bae1dSRodney W. Grimes 
1348a1f6d91cSDavid Greenman 			backing_object->flags |= OBJ_DEAD;
1349df8bae1dSRodney W. Grimes 			/*
1350df8bae1dSRodney W. Grimes 			 * We can collapse the backing object.
1351df8bae1dSRodney W. Grimes 			 *
13520d94caffSDavid Greenman 			 * Move all in-memory pages from backing_object to the
13530d94caffSDavid Greenman 			 * parent.  Pages that have been paged out will be
13540d94caffSDavid Greenman 			 * overwritten by any of the parent's pages that
13550d94caffSDavid Greenman 			 * shadow them.
1356df8bae1dSRodney W. Grimes 			 */
1357df8bae1dSRodney W. Grimes 
135805f0fdd2SPoul-Henning Kamp 			while ((p = backing_object->memq.tqh_first) != 0) {
135926f9a767SRodney W. Grimes 
1360df8bae1dSRodney W. Grimes 				new_offset = (p->offset - backing_offset);
1361df8bae1dSRodney W. Grimes 
1362df8bae1dSRodney W. Grimes 				/*
13630d94caffSDavid Greenman 				 * If the parent has a page here, or if this
13640d94caffSDavid Greenman 				 * page falls outside the parent, dispose of
13650d94caffSDavid Greenman 				 * it.
1366df8bae1dSRodney W. Grimes 				 *
1367df8bae1dSRodney W. Grimes 				 * Otherwise, move it as planned.
1368df8bae1dSRodney W. Grimes 				 */
1369df8bae1dSRodney W. Grimes 
1370df8bae1dSRodney W. Grimes 				if (p->offset < backing_offset ||
1371df8bae1dSRodney W. Grimes 				    new_offset >= size) {
1372df8bae1dSRodney W. Grimes 					vm_page_lock_queues();
13732fe6e4d7SDavid Greenman 					pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
13740d94caffSDavid Greenman 					PAGE_WAKEUP(p);
1375df8bae1dSRodney W. Grimes 					vm_page_free(p);
1376df8bae1dSRodney W. Grimes 					vm_page_unlock_queues();
1377df8bae1dSRodney W. Grimes 				} else {
1378df8bae1dSRodney W. Grimes 					pp = vm_page_lookup(object, new_offset);
137926f9a767SRodney W. Grimes 					if (pp != NULL || (object->pager && vm_pager_has_page(object->pager,
138026f9a767SRodney W. Grimes 					    object->paging_offset + new_offset))) {
1381df8bae1dSRodney W. Grimes 						vm_page_lock_queues();
13822fe6e4d7SDavid Greenman 						pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
13830d94caffSDavid Greenman 						PAGE_WAKEUP(p);
1384df8bae1dSRodney W. Grimes 						vm_page_free(p);
1385df8bae1dSRodney W. Grimes 						vm_page_unlock_queues();
138626f9a767SRodney W. Grimes 					} else {
1387df8bae1dSRodney W. Grimes 						vm_page_rename(p, object, new_offset);
1388df8bae1dSRodney W. Grimes 					}
1389df8bae1dSRodney W. Grimes 				}
1390df8bae1dSRodney W. Grimes 			}
1391df8bae1dSRodney W. Grimes 
1392df8bae1dSRodney W. Grimes 			/*
1393df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1394df8bae1dSRodney W. Grimes 			 */
1395df8bae1dSRodney W. Grimes 
1396df8bae1dSRodney W. Grimes 			if (backing_object->pager) {
139726f9a767SRodney W. Grimes 				backing_object->paging_in_progress++;
139826f9a767SRodney W. Grimes 				if (object->pager) {
139926f9a767SRodney W. Grimes 					vm_pager_t bopager;
14000d94caffSDavid Greenman 
140126f9a767SRodney W. Grimes 					object->paging_in_progress++;
140226f9a767SRodney W. Grimes 					/*
140326f9a767SRodney W. Grimes 					 * copy shadow object pages into ours
14040d94caffSDavid Greenman 					 * and destroy unneeded pages in
14050d94caffSDavid Greenman 					 * shadow object.
140626f9a767SRodney W. Grimes 					 */
140726f9a767SRodney W. Grimes 					bopager = backing_object->pager;
140826f9a767SRodney W. Grimes 					vm_object_remove(backing_object->pager);
1409ba8da839SDavid Greenman 					backing_object->pager = NULL;
141026f9a767SRodney W. Grimes 					swap_pager_copy(
141126f9a767SRodney W. Grimes 					    bopager, backing_object->paging_offset,
141226f9a767SRodney W. Grimes 					    object->pager, object->paging_offset,
141326f9a767SRodney W. Grimes 					    object->shadow_offset);
141426f9a767SRodney W. Grimes 					object->paging_in_progress--;
1415c0503609SDavid Greenman 					if (object->paging_in_progress == 0 &&
1416c0503609SDavid Greenman 					    (object->flags & OBJ_PIPWNT)) {
1417c0503609SDavid Greenman 						object->flags &= ~OBJ_PIPWNT;
141826f9a767SRodney W. Grimes 						wakeup((caddr_t) object);
1419c0503609SDavid Greenman 					}
142026f9a767SRodney W. Grimes 				} else {
142126f9a767SRodney W. Grimes 					object->paging_in_progress++;
142226f9a767SRodney W. Grimes 					/*
142326f9a767SRodney W. Grimes 					 * grab the shadow objects pager
142426f9a767SRodney W. Grimes 					 */
142526f9a767SRodney W. Grimes 					object->pager = backing_object->pager;
142626f9a767SRodney W. Grimes 					object->paging_offset = backing_object->paging_offset + backing_offset;
142726f9a767SRodney W. Grimes 					vm_object_remove(backing_object->pager);
142826f9a767SRodney W. Grimes 					backing_object->pager = NULL;
142926f9a767SRodney W. Grimes 					/*
143026f9a767SRodney W. Grimes 					 * free unnecessary blocks
143126f9a767SRodney W. Grimes 					 */
143226f9a767SRodney W. Grimes 					swap_pager_freespace(object->pager, 0, object->paging_offset);
143326f9a767SRodney W. Grimes 					object->paging_in_progress--;
1434c0503609SDavid Greenman 					if (object->paging_in_progress == 0 &&
1435c0503609SDavid Greenman 					    (object->flags & OBJ_PIPWNT)) {
1436c0503609SDavid Greenman 						object->flags &= ~OBJ_PIPWNT;
143726f9a767SRodney W. Grimes 						wakeup((caddr_t) object);
1438df8bae1dSRodney W. Grimes 					}
1439c0503609SDavid Greenman 				}
1440c0503609SDavid Greenman 
144126f9a767SRodney W. Grimes 				backing_object->paging_in_progress--;
1442c0503609SDavid Greenman 				if (backing_object->paging_in_progress == 0 &&
1443c0503609SDavid Greenman 				    (backing_object->flags & OBJ_PIPWNT)) {
1444c0503609SDavid Greenman 					backing_object->flags &= ~OBJ_PIPWNT;
144526f9a767SRodney W. Grimes 					wakeup((caddr_t) backing_object);
144626f9a767SRodney W. Grimes 				}
1447c0503609SDavid Greenman 			}
1448df8bae1dSRodney W. Grimes 			/*
1449df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
1450df8bae1dSRodney W. Grimes 			 * Note that the reference to backing_object->shadow
1451df8bae1dSRodney W. Grimes 			 * moves from within backing_object to within object.
1452df8bae1dSRodney W. Grimes 			 */
1453df8bae1dSRodney W. Grimes 
14540d94caffSDavid Greenman 			TAILQ_REMOVE(&object->shadow->reverse_shadow_head, object,
14550d94caffSDavid Greenman 			    reverse_shadow_list);
14562fe6e4d7SDavid Greenman 			if (backing_object->shadow)
14570d94caffSDavid Greenman 				TAILQ_REMOVE(&backing_object->shadow->reverse_shadow_head,
14580d94caffSDavid Greenman 				    backing_object, reverse_shadow_list);
1459df8bae1dSRodney W. Grimes 			object->shadow = backing_object->shadow;
14602fe6e4d7SDavid Greenman 			if (object->shadow)
14610d94caffSDavid Greenman 				TAILQ_INSERT_TAIL(&object->shadow->reverse_shadow_head,
14620d94caffSDavid Greenman 				    object, reverse_shadow_list);
14632fe6e4d7SDavid Greenman 
1464df8bae1dSRodney W. Grimes 			object->shadow_offset += backing_object->shadow_offset;
1465df8bae1dSRodney W. Grimes 			if (object->shadow != NULL &&
1466df8bae1dSRodney W. Grimes 			    object->shadow->copy != NULL) {
1467df8bae1dSRodney W. Grimes 				panic("vm_object_collapse: we collapsed a copy-object!");
1468df8bae1dSRodney W. Grimes 			}
1469df8bae1dSRodney W. Grimes 			/*
1470df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1471df8bae1dSRodney W. Grimes 			 *
14720d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
14730d94caffSDavid Greenman 			 * and no object references within it, all that is
14740d94caffSDavid Greenman 			 * necessary is to dispose of it.
1475df8bae1dSRodney W. Grimes 			 */
1476df8bae1dSRodney W. Grimes 
1477df8bae1dSRodney W. Grimes 			vm_object_unlock(backing_object);
1478df8bae1dSRodney W. Grimes 
1479df8bae1dSRodney W. Grimes 			simple_lock(&vm_object_list_lock);
1480df8bae1dSRodney W. Grimes 			TAILQ_REMOVE(&vm_object_list, backing_object,
1481df8bae1dSRodney W. Grimes 			    object_list);
1482df8bae1dSRodney W. Grimes 			vm_object_count--;
1483df8bae1dSRodney W. Grimes 			simple_unlock(&vm_object_list_lock);
1484df8bae1dSRodney W. Grimes 
1485df8bae1dSRodney W. Grimes 			free((caddr_t) backing_object, M_VMOBJ);
1486df8bae1dSRodney W. Grimes 
1487df8bae1dSRodney W. Grimes 			object_collapses++;
14880d94caffSDavid Greenman 		} else {
1489df8bae1dSRodney W. Grimes 			/*
1490df8bae1dSRodney W. Grimes 			 * If all of the pages in the backing object are
14910d94caffSDavid Greenman 			 * shadowed by the parent object, the parent object no
14920d94caffSDavid Greenman 			 * longer has to shadow the backing object; it can
14930d94caffSDavid Greenman 			 * shadow the next one in the chain.
1494df8bae1dSRodney W. Grimes 			 *
14950d94caffSDavid Greenman 			 * The backing object must not be paged out - we'd have
14960d94caffSDavid Greenman 			 * to check all of the paged-out pages, as well.
1497df8bae1dSRodney W. Grimes 			 */
1498df8bae1dSRodney W. Grimes 
1499df8bae1dSRodney W. Grimes 			if (backing_object->pager != NULL) {
1500df8bae1dSRodney W. Grimes 				vm_object_unlock(backing_object);
1501df8bae1dSRodney W. Grimes 				return;
1502df8bae1dSRodney W. Grimes 			}
1503df8bae1dSRodney W. Grimes 			/*
15040d94caffSDavid Greenman 			 * Should have a check for a 'small' number of pages
15050d94caffSDavid Greenman 			 * here.
1506df8bae1dSRodney W. Grimes 			 */
1507df8bae1dSRodney W. Grimes 
150826f9a767SRodney W. Grimes 			for (p = backing_object->memq.tqh_first; p; p = p->listq.tqe_next) {
1509df8bae1dSRodney W. Grimes 				new_offset = (p->offset - backing_offset);
1510df8bae1dSRodney W. Grimes 
1511df8bae1dSRodney W. Grimes 				/*
15120d94caffSDavid Greenman 				 * If the parent has a page here, or if this
15130d94caffSDavid Greenman 				 * page falls outside the parent, keep going.
1514df8bae1dSRodney W. Grimes 				 *
15150d94caffSDavid Greenman 				 * Otherwise, the backing_object must be left in
15160d94caffSDavid Greenman 				 * the chain.
1517df8bae1dSRodney W. Grimes 				 */
1518df8bae1dSRodney W. Grimes 
1519df8bae1dSRodney W. Grimes 				if (p->offset >= backing_offset &&
152026f9a767SRodney W. Grimes 				    new_offset <= size &&
15210d94caffSDavid Greenman 				    ((pp = vm_page_lookup(object, new_offset)) == NULL ||
15220d94caffSDavid Greenman 					!pp->valid) &&
152326f9a767SRodney W. Grimes 				    (!object->pager || !vm_pager_has_page(object->pager, object->paging_offset + new_offset))) {
1524df8bae1dSRodney W. Grimes 					/*
15250d94caffSDavid Greenman 					 * Page still needed. Can't go any
15260d94caffSDavid Greenman 					 * further.
1527df8bae1dSRodney W. Grimes 					 */
1528df8bae1dSRodney W. Grimes 					vm_object_unlock(backing_object);
1529df8bae1dSRodney W. Grimes 					return;
1530df8bae1dSRodney W. Grimes 				}
1531df8bae1dSRodney W. Grimes 			}
1532df8bae1dSRodney W. Grimes 
1533df8bae1dSRodney W. Grimes 			/*
15340d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
15350d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
15360d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1537df8bae1dSRodney W. Grimes 			 */
1538df8bae1dSRodney W. Grimes 
15390d94caffSDavid Greenman 			TAILQ_REMOVE(&object->shadow->reverse_shadow_head,
15400d94caffSDavid Greenman 			    object, reverse_shadow_list);
154126f9a767SRodney W. Grimes 			vm_object_reference(object->shadow = backing_object->shadow);
15422fe6e4d7SDavid Greenman 			if (object->shadow)
15430d94caffSDavid Greenman 				TAILQ_INSERT_TAIL(&object->shadow->reverse_shadow_head,
15440d94caffSDavid Greenman 				    object, reverse_shadow_list);
1545df8bae1dSRodney W. Grimes 			object->shadow_offset += backing_object->shadow_offset;
1546df8bae1dSRodney W. Grimes 
1547df8bae1dSRodney W. Grimes 			/*
15480d94caffSDavid Greenman 			 * Backing object might have had a copy pointer to us.
15490d94caffSDavid Greenman 			 * If it did, clear it.
1550df8bae1dSRodney W. Grimes 			 */
1551df8bae1dSRodney W. Grimes 			if (backing_object->copy == object) {
1552df8bae1dSRodney W. Grimes 				backing_object->copy = NULL;
1553df8bae1dSRodney W. Grimes 			}
15540d94caffSDavid Greenman 			/*
15550d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
15560d94caffSDavid Greenman 			 * its ref_count was at least 2, it will not vanish;
15570d94caffSDavid Greenman 			 * so we don't need to call vm_object_deallocate.
1558df8bae1dSRodney W. Grimes 			 */
155926f9a767SRodney W. Grimes 			if (backing_object->ref_count == 1)
156026f9a767SRodney W. Grimes 				printf("should have called obj deallocate\n");
1561df8bae1dSRodney W. Grimes 			backing_object->ref_count--;
1562df8bae1dSRodney W. Grimes 			vm_object_unlock(backing_object);
1563df8bae1dSRodney W. Grimes 
1564df8bae1dSRodney W. Grimes 			object_bypasses++;
1565df8bae1dSRodney W. Grimes 
1566df8bae1dSRodney W. Grimes 		}
1567df8bae1dSRodney W. Grimes 
1568df8bae1dSRodney W. Grimes 		/*
1569df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1570df8bae1dSRodney W. Grimes 		 */
1571df8bae1dSRodney W. Grimes 	}
1572df8bae1dSRodney W. Grimes }
1573df8bae1dSRodney W. Grimes 
1574df8bae1dSRodney W. Grimes /*
1575df8bae1dSRodney W. Grimes  *	vm_object_page_remove: [internal]
1576df8bae1dSRodney W. Grimes  *
1577df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
1578df8bae1dSRodney W. Grimes  *	object range from the object's list of pages.
1579df8bae1dSRodney W. Grimes  *
1580df8bae1dSRodney W. Grimes  *	The object must be locked.
1581df8bae1dSRodney W. Grimes  */
158226f9a767SRodney W. Grimes void
158326f9a767SRodney W. Grimes vm_object_page_remove(object, start, end)
1584df8bae1dSRodney W. Grimes 	register vm_object_t object;
1585df8bae1dSRodney W. Grimes 	register vm_offset_t start;
1586df8bae1dSRodney W. Grimes 	register vm_offset_t end;
1587df8bae1dSRodney W. Grimes {
1588df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
158926f9a767SRodney W. Grimes 	vm_offset_t size;
159026f9a767SRodney W. Grimes 	int s;
1591df8bae1dSRodney W. Grimes 
1592df8bae1dSRodney W. Grimes 	if (object == NULL)
1593df8bae1dSRodney W. Grimes 		return;
1594df8bae1dSRodney W. Grimes 
15952fe6e4d7SDavid Greenman 	object->paging_in_progress++;
159626f9a767SRodney W. Grimes 	start = trunc_page(start);
159726f9a767SRodney W. Grimes 	end = round_page(end);
159826f9a767SRodney W. Grimes again:
159926f9a767SRodney W. Grimes 	size = end - start;
160026f9a767SRodney W. Grimes 	if (size > 4 * PAGE_SIZE || size >= object->size / 4) {
16010d94caffSDavid Greenman 		for (p = object->memq.tqh_first; p != NULL; p = next) {
1602df8bae1dSRodney W. Grimes 			next = p->listq.tqe_next;
1603df8bae1dSRodney W. Grimes 			if ((start <= p->offset) && (p->offset < end)) {
1604a481f200SDavid Greenman 				s = splhigh();
16050d94caffSDavid Greenman 				if (p->bmapped) {
16060d94caffSDavid Greenman 					splx(s);
16070d94caffSDavid Greenman 					continue;
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);
1616df8bae1dSRodney W. Grimes 				pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
1617df8bae1dSRodney W. Grimes 				vm_page_lock_queues();
16180d94caffSDavid Greenman 				PAGE_WAKEUP(p);
1619df8bae1dSRodney W. Grimes 				vm_page_free(p);
1620df8bae1dSRodney W. Grimes 				vm_page_unlock_queues();
162126f9a767SRodney W. Grimes 			}
162226f9a767SRodney W. Grimes 		}
162326f9a767SRodney W. Grimes 	} else {
162426f9a767SRodney W. Grimes 		while (size > 0) {
162505f0fdd2SPoul-Henning Kamp 			while ((p = vm_page_lookup(object, start)) != 0) {
1626a481f200SDavid Greenman 				s = splhigh();
16270d94caffSDavid Greenman 				if (p->bmapped) {
16280d94caffSDavid Greenman 					splx(s);
16290d94caffSDavid Greenman 					break;
16300d94caffSDavid Greenman 				}
16310d94caffSDavid Greenman 				if ((p->flags & PG_BUSY) || p->busy) {
163226f9a767SRodney W. Grimes 					p->flags |= PG_WANTED;
163326f9a767SRodney W. Grimes 					tsleep((caddr_t) p, PVM, "vmopar", 0);
1634a481f200SDavid Greenman 					splx(s);
163526f9a767SRodney W. Grimes 					goto again;
163626f9a767SRodney W. Grimes 				}
1637a481f200SDavid Greenman 				splx(s);
163826f9a767SRodney W. Grimes 				pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
163926f9a767SRodney W. Grimes 				vm_page_lock_queues();
16400d94caffSDavid Greenman 				PAGE_WAKEUP(p);
164126f9a767SRodney W. Grimes 				vm_page_free(p);
164226f9a767SRodney W. Grimes 				vm_page_unlock_queues();
164326f9a767SRodney W. Grimes 			}
164426f9a767SRodney W. Grimes 			start += PAGE_SIZE;
164526f9a767SRodney W. Grimes 			size -= PAGE_SIZE;
1646df8bae1dSRodney W. Grimes 		}
1647df8bae1dSRodney W. Grimes 	}
16482fe6e4d7SDavid Greenman 	--object->paging_in_progress;
1649c0503609SDavid Greenman 	if (object->paging_in_progress == 0 && (object->flags & OBJ_PIPWNT)) {
1650c0503609SDavid Greenman 		object->flags &= ~OBJ_PIPWNT;
16512fe6e4d7SDavid Greenman 		wakeup((caddr_t) object);
1652df8bae1dSRodney W. Grimes 	}
1653c0503609SDavid Greenman }
1654df8bae1dSRodney W. Grimes 
1655df8bae1dSRodney W. Grimes /*
1656df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1657df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1658df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1659df8bae1dSRodney W. Grimes  *
1660df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1661df8bae1dSRodney W. Grimes  *
1662df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1663df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1664df8bae1dSRodney W. Grimes  *
1665df8bae1dSRodney W. Grimes  *	Parameters:
1666df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1667df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1668df8bae1dSRodney W. Grimes  *		next_object	Second object into coalesce
1669df8bae1dSRodney W. Grimes  *		next_offset	Offset into next_object
1670df8bae1dSRodney W. Grimes  *
1671df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
1672df8bae1dSRodney W. Grimes  *		next_size	Size of reference to next_object
1673df8bae1dSRodney W. Grimes  *
1674df8bae1dSRodney W. Grimes  *	Conditions:
1675df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1676df8bae1dSRodney W. Grimes  */
16770d94caffSDavid Greenman boolean_t
16780d94caffSDavid Greenman vm_object_coalesce(prev_object, next_object,
1679df8bae1dSRodney W. Grimes     prev_offset, next_offset,
1680df8bae1dSRodney W. Grimes     prev_size, next_size)
1681df8bae1dSRodney W. Grimes 	register vm_object_t prev_object;
1682df8bae1dSRodney W. Grimes 	vm_object_t next_object;
1683df8bae1dSRodney W. Grimes 	vm_offset_t prev_offset, next_offset;
1684df8bae1dSRodney W. Grimes 	vm_size_t prev_size, next_size;
1685df8bae1dSRodney W. Grimes {
1686df8bae1dSRodney W. Grimes 	vm_size_t newsize;
1687df8bae1dSRodney W. Grimes 
1688df8bae1dSRodney W. Grimes 	if (next_object != NULL) {
1689df8bae1dSRodney W. Grimes 		return (FALSE);
1690df8bae1dSRodney W. Grimes 	}
1691df8bae1dSRodney W. Grimes 	if (prev_object == NULL) {
1692df8bae1dSRodney W. Grimes 		return (TRUE);
1693df8bae1dSRodney W. Grimes 	}
1694df8bae1dSRodney W. Grimes 	vm_object_lock(prev_object);
1695df8bae1dSRodney W. Grimes 
1696df8bae1dSRodney W. Grimes 	/*
1697df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1698df8bae1dSRodney W. Grimes 	 */
1699df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1700df8bae1dSRodney W. Grimes 
1701df8bae1dSRodney W. Grimes 	/*
17020d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
17030d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
17040d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1705df8bae1dSRodney W. Grimes 	 */
1706df8bae1dSRodney W. Grimes 
1707df8bae1dSRodney W. Grimes 	if (prev_object->ref_count > 1 ||
1708df8bae1dSRodney W. Grimes 	    prev_object->pager != NULL ||
1709df8bae1dSRodney W. Grimes 	    prev_object->shadow != NULL ||
1710df8bae1dSRodney W. Grimes 	    prev_object->copy != NULL) {
1711df8bae1dSRodney W. Grimes 		vm_object_unlock(prev_object);
1712df8bae1dSRodney W. Grimes 		return (FALSE);
1713df8bae1dSRodney W. Grimes 	}
1714df8bae1dSRodney W. Grimes 	/*
17150d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
17160d94caffSDavid Greenman 	 * deallocation.
1717df8bae1dSRodney W. Grimes 	 */
1718df8bae1dSRodney W. Grimes 
1719df8bae1dSRodney W. Grimes 	vm_object_page_remove(prev_object,
1720df8bae1dSRodney W. Grimes 	    prev_offset + prev_size,
1721df8bae1dSRodney W. Grimes 	    prev_offset + prev_size + next_size);
1722df8bae1dSRodney W. Grimes 
1723df8bae1dSRodney W. Grimes 	/*
1724df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1725df8bae1dSRodney W. Grimes 	 */
1726df8bae1dSRodney W. Grimes 	newsize = prev_offset + prev_size + next_size;
1727df8bae1dSRodney W. Grimes 	if (newsize > prev_object->size)
1728df8bae1dSRodney W. Grimes 		prev_object->size = newsize;
1729df8bae1dSRodney W. Grimes 
1730df8bae1dSRodney W. Grimes 	vm_object_unlock(prev_object);
1731df8bae1dSRodney W. Grimes 	return (TRUE);
1732df8bae1dSRodney W. Grimes }
1733df8bae1dSRodney W. Grimes 
1734df8bae1dSRodney W. Grimes /*
173526f9a767SRodney W. Grimes  * returns page after looking up in shadow chain
173626f9a767SRodney W. Grimes  */
173726f9a767SRodney W. Grimes 
173826f9a767SRodney W. Grimes vm_page_t
173926f9a767SRodney W. Grimes vm_object_page_lookup(object, offset)
174026f9a767SRodney W. Grimes 	vm_object_t object;
174126f9a767SRodney W. Grimes 	vm_offset_t offset;
174226f9a767SRodney W. Grimes {
174326f9a767SRodney W. Grimes 	vm_page_t m;
17440d94caffSDavid Greenman 
174526f9a767SRodney W. Grimes 	if (!(m = vm_page_lookup(object, offset))) {
174626f9a767SRodney W. Grimes 		if (!object->shadow)
174726f9a767SRodney W. Grimes 			return 0;
174826f9a767SRodney W. Grimes 		else
174926f9a767SRodney W. Grimes 			return vm_object_page_lookup(object->shadow, offset + object->shadow_offset);
175026f9a767SRodney W. Grimes 	}
175126f9a767SRodney W. Grimes 	return m;
175226f9a767SRodney W. Grimes }
175326f9a767SRodney W. Grimes 
1754a1f6d91cSDavid Greenman int
1755a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry)
1756a1f6d91cSDavid Greenman 	vm_map_t map;
1757a1f6d91cSDavid Greenman 	vm_object_t object;
1758a1f6d91cSDavid Greenman 	vm_map_entry_t entry;
1759a1f6d91cSDavid Greenman {
1760a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1761a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1762a1f6d91cSDavid Greenman 	vm_object_t obj;
1763a1f6d91cSDavid Greenman 	int entcount;
1764a1f6d91cSDavid Greenman 
1765a1f6d91cSDavid Greenman 	if (map == 0)
1766a1f6d91cSDavid Greenman 		return 0;
1767a1f6d91cSDavid Greenman 
1768a1f6d91cSDavid Greenman 	if (entry == 0) {
1769a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1770a1f6d91cSDavid Greenman 		entcount = map->nentries;
1771a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1772a1f6d91cSDavid Greenman 			if( _vm_object_in_map(map, object, tmpe)) {
1773a1f6d91cSDavid Greenman 				return 1;
1774a1f6d91cSDavid Greenman 			}
1775a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1776a1f6d91cSDavid Greenman 		}
1777a1f6d91cSDavid Greenman 	} else if (entry->is_sub_map || entry->is_a_map) {
1778a1f6d91cSDavid Greenman 		tmpm = entry->object.share_map;
1779a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1780a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1781a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1782a1f6d91cSDavid Greenman 			if( _vm_object_in_map(tmpm, object, tmpe)) {
1783a1f6d91cSDavid Greenman 				return 1;
1784a1f6d91cSDavid Greenman 			}
1785a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1786a1f6d91cSDavid Greenman 		}
1787a1f6d91cSDavid Greenman 	} else if (obj = entry->object.vm_object) {
1788a1f6d91cSDavid Greenman 		for(; obj; obj=obj->shadow)
1789a1f6d91cSDavid Greenman 			if( obj == object) {
1790a1f6d91cSDavid Greenman 				return 1;
1791a1f6d91cSDavid Greenman 			}
1792a1f6d91cSDavid Greenman 	}
1793a1f6d91cSDavid Greenman 	return 0;
1794a1f6d91cSDavid Greenman }
1795a1f6d91cSDavid Greenman 
1796a1f6d91cSDavid Greenman int
1797a1f6d91cSDavid Greenman vm_object_in_map( object)
1798a1f6d91cSDavid Greenman 	vm_object_t object;
1799a1f6d91cSDavid Greenman {
1800a1f6d91cSDavid Greenman 	struct proc *p;
1801a1f6d91cSDavid Greenman 	for (p = (struct proc *) allproc; p != NULL; p = p->p_next) {
1802a1f6d91cSDavid Greenman 		if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1803a1f6d91cSDavid Greenman 			continue;
1804a1f6d91cSDavid Greenman /*
1805a1f6d91cSDavid Greenman 		if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
1806a1f6d91cSDavid Greenman 			continue;
1807a1f6d91cSDavid Greenman 		}
1808a1f6d91cSDavid Greenman */
1809a1f6d91cSDavid Greenman 		if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1810a1f6d91cSDavid Greenman 			return 1;
1811a1f6d91cSDavid Greenman 	}
1812a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kernel_map, object, 0))
1813a1f6d91cSDavid Greenman 		return 1;
1814a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kmem_map, object, 0))
1815a1f6d91cSDavid Greenman 		return 1;
1816a1f6d91cSDavid Greenman 	if( _vm_object_in_map( pager_map, object, 0))
1817a1f6d91cSDavid Greenman 		return 1;
1818a1f6d91cSDavid Greenman 	if( _vm_object_in_map( buffer_map, object, 0))
1819a1f6d91cSDavid Greenman 		return 1;
1820a1f6d91cSDavid Greenman 	if( _vm_object_in_map( io_map, object, 0))
1821a1f6d91cSDavid Greenman 		return 1;
1822a1f6d91cSDavid Greenman 	if( _vm_object_in_map( phys_map, object, 0))
1823a1f6d91cSDavid Greenman 		return 1;
1824a1f6d91cSDavid Greenman 	if( _vm_object_in_map( mb_map, object, 0))
1825a1f6d91cSDavid Greenman 		return 1;
1826a1f6d91cSDavid Greenman 	if( _vm_object_in_map( u_map, object, 0))
1827a1f6d91cSDavid Greenman 		return 1;
1828a1f6d91cSDavid Greenman 	return 0;
1829a1f6d91cSDavid Greenman }
1830a1f6d91cSDavid Greenman 
1831a1f6d91cSDavid Greenman void
1832a1f6d91cSDavid Greenman vm_object_check() {
1833a1f6d91cSDavid Greenman 	int i;
1834a1f6d91cSDavid Greenman 	int maxhash = 0;
1835a1f6d91cSDavid Greenman 	vm_object_t object;
1836a1f6d91cSDavid Greenman 	vm_object_hash_entry_t entry;
1837a1f6d91cSDavid Greenman 
1838a1f6d91cSDavid Greenman 	/*
1839a1f6d91cSDavid Greenman 	 * make sure that no internal objs are hashed
1840a1f6d91cSDavid Greenman 	 */
1841a1f6d91cSDavid Greenman 	for (i=0; i<VM_OBJECT_HASH_COUNT;i++) {
1842a1f6d91cSDavid Greenman 		int lsize = 0;
1843a1f6d91cSDavid Greenman 		for (entry = vm_object_hashtable[i].tqh_first;
1844a1f6d91cSDavid Greenman 		    entry != NULL;
1845a1f6d91cSDavid Greenman 		    entry = entry->hash_links.tqe_next) {
1846a1f6d91cSDavid Greenman 			if( entry->object->flags & OBJ_INTERNAL) {
1847a1f6d91cSDavid Greenman 				printf("vmochk: internal obj on hash: size: %d\n", entry->object->size);
1848a1f6d91cSDavid Greenman 			}
1849a1f6d91cSDavid Greenman 			++lsize;
1850a1f6d91cSDavid Greenman 		}
1851a1f6d91cSDavid Greenman 		if( lsize > maxhash)
1852a1f6d91cSDavid Greenman 			maxhash = lsize;
1853a1f6d91cSDavid Greenman 	}
1854a1f6d91cSDavid Greenman 
1855a1f6d91cSDavid Greenman 	printf("maximum object hash queue size: %d\n",  maxhash);
1856a1f6d91cSDavid Greenman 
1857a1f6d91cSDavid Greenman 	/*
1858a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
1859a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
1860a1f6d91cSDavid Greenman 	 */
1861a1f6d91cSDavid Greenman 	for (object = vm_object_list.tqh_first;
1862a1f6d91cSDavid Greenman 			object != NULL;
1863a1f6d91cSDavid Greenman 			object = object->object_list.tqe_next) {
1864a1f6d91cSDavid Greenman 		if( object->flags & OBJ_INTERNAL) {
1865a1f6d91cSDavid Greenman 			if( object->ref_count == 0) {
1866a1f6d91cSDavid Greenman 				printf("vmochk: internal obj has zero ref count: %d\n",
1867a1f6d91cSDavid Greenman 					object->size);
1868a1f6d91cSDavid Greenman 			}
1869a1f6d91cSDavid Greenman 			if( !vm_object_in_map(object)) {
1870a1f6d91cSDavid 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);
1871a1f6d91cSDavid Greenman 			}
1872a1f6d91cSDavid Greenman 		}
1873a1f6d91cSDavid Greenman 	}
1874a1f6d91cSDavid Greenman }
1875a1f6d91cSDavid Greenman 
187626f9a767SRodney W. Grimes #define DEBUG
18778a129caeSDavid Greenman #if defined(DEBUG) || defined(DDB)
187826f9a767SRodney W. Grimes /*
1879df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
1880df8bae1dSRodney W. Grimes  */
18810d94caffSDavid Greenman void
18820d94caffSDavid Greenman vm_object_print(object, full)
1883df8bae1dSRodney W. Grimes 	vm_object_t object;
1884df8bae1dSRodney W. Grimes 	boolean_t full;
1885df8bae1dSRodney W. Grimes {
1886df8bae1dSRodney W. Grimes 	register vm_page_t p;
1887df8bae1dSRodney W. Grimes 	extern indent;
1888df8bae1dSRodney W. Grimes 
1889df8bae1dSRodney W. Grimes 	register int count;
1890df8bae1dSRodney W. Grimes 
1891df8bae1dSRodney W. Grimes 	if (object == NULL)
1892df8bae1dSRodney W. Grimes 		return;
1893df8bae1dSRodney W. Grimes 
1894df8bae1dSRodney W. Grimes 	iprintf("Object 0x%x: size=0x%x, res=%d, ref=%d, ",
1895df8bae1dSRodney W. Grimes 	    (int) object, (int) object->size,
1896df8bae1dSRodney W. Grimes 	    object->resident_page_count, object->ref_count);
1897df8bae1dSRodney W. Grimes 	printf("pager=0x%x+0x%x, shadow=(0x%x)+0x%x\n",
1898df8bae1dSRodney W. Grimes 	    (int) object->pager, (int) object->paging_offset,
1899df8bae1dSRodney W. Grimes 	    (int) object->shadow, (int) object->shadow_offset);
190005f0fdd2SPoul-Henning Kamp 	printf("cache: next=%p, prev=%p\n",
1901df8bae1dSRodney W. Grimes 	    object->cached_list.tqe_next, object->cached_list.tqe_prev);
1902df8bae1dSRodney W. Grimes 
1903df8bae1dSRodney W. Grimes 	if (!full)
1904df8bae1dSRodney W. Grimes 		return;
1905df8bae1dSRodney W. Grimes 
1906df8bae1dSRodney W. Grimes 	indent += 2;
1907df8bae1dSRodney W. Grimes 	count = 0;
1908df8bae1dSRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) {
1909df8bae1dSRodney W. Grimes 		if (count == 0)
1910df8bae1dSRodney W. Grimes 			iprintf("memory:=");
1911df8bae1dSRodney W. Grimes 		else if (count == 6) {
1912df8bae1dSRodney W. Grimes 			printf("\n");
1913df8bae1dSRodney W. Grimes 			iprintf(" ...");
1914df8bae1dSRodney W. Grimes 			count = 0;
1915df8bae1dSRodney W. Grimes 		} else
1916df8bae1dSRodney W. Grimes 			printf(",");
1917df8bae1dSRodney W. Grimes 		count++;
1918df8bae1dSRodney W. Grimes 
191905f0fdd2SPoul-Henning Kamp 		printf("(off=0x%lx,page=0x%lx)",
192005f0fdd2SPoul-Henning Kamp 		    (u_long) p->offset, (u_long) VM_PAGE_TO_PHYS(p));
1921df8bae1dSRodney W. Grimes 	}
1922df8bae1dSRodney W. Grimes 	if (count != 0)
1923df8bae1dSRodney W. Grimes 		printf("\n");
1924df8bae1dSRodney W. Grimes 	indent -= 2;
1925df8bae1dSRodney W. Grimes }
19268a129caeSDavid Greenman #endif				/* defined(DEBUG) || defined(DDB) */
1927