xref: /freebsd/sys/vm/vm_object.c (revision a1f6d91cc2800e2993d2c36e1fd25bfe028515d0)
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  *
64a1f6d91cSDavid Greenman  * $Id: vm_object.c,v 1.20 1995/01/25 20:36:29 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);
1730d94caffSDavid Greenman 	vm_object_cache_max = (cnt.v_page_count - 500) / 8;
17426f9a767SRodney W. Grimes 
17526f9a767SRodney W. Grimes 	for (i = 0; i < VM_OBJECT_HASH_COUNT; i++)
17626f9a767SRodney W. Grimes 		TAILQ_INIT(&vm_object_hashtable[i]);
17726f9a767SRodney W. Grimes 
17826f9a767SRodney W. Grimes 	kernel_object = &kernel_object_store;
17926f9a767SRodney W. Grimes 	_vm_object_allocate(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS,
18026f9a767SRodney W. Grimes 	    kernel_object);
18126f9a767SRodney W. Grimes 
18226f9a767SRodney W. Grimes 	kmem_object = &kmem_object_store;
18326f9a767SRodney W. Grimes 	_vm_object_allocate(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS,
18426f9a767SRodney W. Grimes 	    kmem_object);
18526f9a767SRodney W. Grimes }
18626f9a767SRodney W. Grimes 
18726f9a767SRodney W. Grimes /*
18826f9a767SRodney W. Grimes  *	vm_object_allocate:
18926f9a767SRodney W. Grimes  *
19026f9a767SRodney W. Grimes  *	Returns a new object with the given size.
19126f9a767SRodney W. Grimes  */
19226f9a767SRodney W. Grimes 
19326f9a767SRodney W. Grimes vm_object_t
19426f9a767SRodney W. Grimes vm_object_allocate(size)
19526f9a767SRodney W. Grimes 	vm_size_t size;
19626f9a767SRodney W. Grimes {
19726f9a767SRodney W. Grimes 	register vm_object_t result;
19826f9a767SRodney W. Grimes 
19926f9a767SRodney W. Grimes 	result = (vm_object_t)
20026f9a767SRodney W. Grimes 	    malloc((u_long) sizeof *result, M_VMOBJ, M_WAITOK);
20126f9a767SRodney W. Grimes 
20226f9a767SRodney W. Grimes 
20326f9a767SRodney W. Grimes 	_vm_object_allocate(size, result);
20426f9a767SRodney W. Grimes 
20526f9a767SRodney W. Grimes 	return (result);
20626f9a767SRodney W. Grimes }
20726f9a767SRodney W. Grimes 
20826f9a767SRodney W. Grimes 
20926f9a767SRodney W. Grimes /*
210df8bae1dSRodney W. Grimes  *	vm_object_reference:
211df8bae1dSRodney W. Grimes  *
212df8bae1dSRodney W. Grimes  *	Gets another reference to the given object.
213df8bae1dSRodney W. Grimes  */
21426f9a767SRodney W. Grimes inline void
21526f9a767SRodney W. Grimes vm_object_reference(object)
216df8bae1dSRodney W. Grimes 	register vm_object_t object;
217df8bae1dSRodney W. Grimes {
218df8bae1dSRodney W. Grimes 	if (object == NULL)
219df8bae1dSRodney W. Grimes 		return;
220df8bae1dSRodney W. Grimes 
221df8bae1dSRodney W. Grimes 	vm_object_lock(object);
222df8bae1dSRodney W. Grimes 	object->ref_count++;
223df8bae1dSRodney W. Grimes 	vm_object_unlock(object);
224df8bae1dSRodney W. Grimes }
225df8bae1dSRodney W. Grimes 
226df8bae1dSRodney W. Grimes /*
227df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
228df8bae1dSRodney W. Grimes  *
229df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
230df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
231df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
232df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
233df8bae1dSRodney W. Grimes  *	may be relinquished.
234df8bae1dSRodney W. Grimes  *
235df8bae1dSRodney W. Grimes  *	No object may be locked.
236df8bae1dSRodney W. Grimes  */
23726f9a767SRodney W. Grimes void
23826f9a767SRodney W. Grimes vm_object_deallocate(object)
23926f9a767SRodney W. Grimes 	vm_object_t object;
240df8bae1dSRodney W. Grimes {
241df8bae1dSRodney W. Grimes 	vm_object_t temp;
242df8bae1dSRodney W. Grimes 
243df8bae1dSRodney W. Grimes 	while (object != NULL) {
244df8bae1dSRodney W. Grimes 
245df8bae1dSRodney W. Grimes 		/*
2460d94caffSDavid Greenman 		 * The cache holds a reference (uncounted) to the object; we
2470d94caffSDavid Greenman 		 * must lock it before removing the object.
248df8bae1dSRodney W. Grimes 		 */
249df8bae1dSRodney W. Grimes 
250df8bae1dSRodney W. Grimes 		vm_object_cache_lock();
251df8bae1dSRodney W. Grimes 
252df8bae1dSRodney W. Grimes 		/*
253df8bae1dSRodney W. Grimes 		 * Lose the reference
254df8bae1dSRodney W. Grimes 		 */
255df8bae1dSRodney W. Grimes 		vm_object_lock(object);
256df8bae1dSRodney W. Grimes 		if (--(object->ref_count) != 0) {
257010cf3b9SDavid Greenman 			if (object->ref_count == 1) {
258a1f6d91cSDavid Greenman 				vm_object_t robject;
259a1f6d91cSDavid Greenman 				robject = object->reverse_shadow_head.tqh_first;
260a1f6d91cSDavid Greenman 				if( robject) {
261a1f6d91cSDavid Greenman 					int s;
262a1f6d91cSDavid Greenman 					robject->ref_count += 2;
263a1f6d91cSDavid Greenman 					object->ref_count += 1;
264a1f6d91cSDavid Greenman 
265a1f6d91cSDavid Greenman 					do {
266a1f6d91cSDavid Greenman 						s = splhigh();
267a1f6d91cSDavid Greenman 						while( robject->paging_in_progress) {
268a1f6d91cSDavid Greenman 							tsleep(robject, PVM, "objde1", 0);
2690d94caffSDavid Greenman 						}
270a1f6d91cSDavid Greenman 
271a1f6d91cSDavid Greenman 						while( object->paging_in_progress) {
272a1f6d91cSDavid Greenman 							tsleep(object, PVM, "objde2", 0);
273a1f6d91cSDavid Greenman 						}
274a1f6d91cSDavid Greenman 						splx(s);
275a1f6d91cSDavid Greenman 
276a1f6d91cSDavid Greenman 					} while( object->paging_in_progress || robject->paging_in_progress);
277a1f6d91cSDavid Greenman 
278a1f6d91cSDavid Greenman 					object->ref_count -= 1;
279a1f6d91cSDavid Greenman 					robject->ref_count -= 2;
280a1f6d91cSDavid Greenman 					if( robject->ref_count == 0) {
281a1f6d91cSDavid Greenman 						vm_object_unlock(object);
282a1f6d91cSDavid Greenman 						vm_object_cache_unlock();
283a1f6d91cSDavid Greenman 						robject->ref_count += 1;
284a1f6d91cSDavid Greenman 						vm_object_deallocate(robject);
285a1f6d91cSDavid Greenman 						return;
286a1f6d91cSDavid Greenman 					}
287a1f6d91cSDavid Greenman 					vm_object_cache_unlock();
288a1f6d91cSDavid Greenman 					vm_object_unlock(object);
289a1f6d91cSDavid Greenman 					vm_object_lock(robject);
290a1f6d91cSDavid Greenman 					vm_object_collapse(robject);
291a1f6d91cSDavid Greenman 					return;
292010cf3b9SDavid Greenman 				}
293010cf3b9SDavid Greenman 			}
29426f9a767SRodney W. Grimes 			vm_object_unlock(object);
295df8bae1dSRodney W. Grimes 			/*
2960d94caffSDavid Greenman 			 * If there are still references, then we are done.
297df8bae1dSRodney W. Grimes 			 */
298df8bae1dSRodney W. Grimes 			vm_object_cache_unlock();
299df8bae1dSRodney W. Grimes 			return;
300df8bae1dSRodney W. Grimes 		}
301df8bae1dSRodney W. Grimes 		/*
3020d94caffSDavid Greenman 		 * See if this object can persist.  If so, enter it in the
3030d94caffSDavid Greenman 		 * cache, then deactivate all of its pages.
304df8bae1dSRodney W. Grimes 		 */
305df8bae1dSRodney W. Grimes 
306df8bae1dSRodney W. Grimes 		if (object->flags & OBJ_CANPERSIST) {
307df8bae1dSRodney W. Grimes 
308df8bae1dSRodney W. Grimes 			TAILQ_INSERT_TAIL(&vm_object_cached_list, object,
309df8bae1dSRodney W. Grimes 			    cached_list);
310df8bae1dSRodney W. Grimes 			vm_object_cached++;
311df8bae1dSRodney W. Grimes 			vm_object_cache_unlock();
312df8bae1dSRodney W. Grimes 
313df8bae1dSRodney W. Grimes 			vm_object_unlock(object);
314df8bae1dSRodney W. Grimes 
315df8bae1dSRodney W. Grimes 			vm_object_cache_trim();
316df8bae1dSRodney W. Grimes 			return;
317df8bae1dSRodney W. Grimes 		}
318df8bae1dSRodney W. Grimes 		/*
319df8bae1dSRodney W. Grimes 		 * Make sure no one can look us up now.
320df8bae1dSRodney W. Grimes 		 */
3210d94caffSDavid Greenman 		object->flags |= OBJ_DEAD;
322df8bae1dSRodney W. Grimes 		vm_object_remove(object->pager);
323df8bae1dSRodney W. Grimes 		vm_object_cache_unlock();
324df8bae1dSRodney W. Grimes 
325df8bae1dSRodney W. Grimes 		temp = object->shadow;
3262fe6e4d7SDavid Greenman 		if (temp)
3272fe6e4d7SDavid Greenman 			TAILQ_REMOVE(&temp->reverse_shadow_head, object, reverse_shadow_list);
328df8bae1dSRodney W. Grimes 		vm_object_terminate(object);
329df8bae1dSRodney W. Grimes 		/* unlocks and deallocates object */
330df8bae1dSRodney W. Grimes 		object = temp;
331df8bae1dSRodney W. Grimes 	}
332df8bae1dSRodney W. Grimes }
333df8bae1dSRodney W. Grimes 
334df8bae1dSRodney W. Grimes /*
335df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
336df8bae1dSRodney W. Grimes  *	up all previously used resources.
337df8bae1dSRodney W. Grimes  *
338df8bae1dSRodney W. Grimes  *	The object must be locked.
339df8bae1dSRodney W. Grimes  */
34026f9a767SRodney W. Grimes void
34126f9a767SRodney W. Grimes vm_object_terminate(object)
342df8bae1dSRodney W. Grimes 	register vm_object_t object;
343df8bae1dSRodney W. Grimes {
3440d94caffSDavid Greenman 	register vm_page_t p, next;
345df8bae1dSRodney W. Grimes 	vm_object_t shadow_object;
34626f9a767SRodney W. Grimes 	int s;
3470d94caffSDavid Greenman 	struct vnode *vp = NULL;
348df8bae1dSRodney W. Grimes 
349df8bae1dSRodney W. Grimes 	/*
3500d94caffSDavid Greenman 	 * Detach the object from its shadow if we are the shadow's copy.
351df8bae1dSRodney W. Grimes 	 */
352df8bae1dSRodney W. Grimes 	if ((shadow_object = object->shadow) != NULL) {
353df8bae1dSRodney W. Grimes 		vm_object_lock(shadow_object);
354df8bae1dSRodney W. Grimes 		if (shadow_object->copy == object)
355df8bae1dSRodney W. Grimes 			shadow_object->copy = NULL;
35626f9a767SRodney W. Grimes /*
357df8bae1dSRodney W. Grimes 		else if (shadow_object->copy != NULL)
358df8bae1dSRodney W. Grimes 			panic("vm_object_terminate: copy/shadow inconsistency");
35926f9a767SRodney W. Grimes */
360df8bae1dSRodney W. Grimes 		vm_object_unlock(shadow_object);
361df8bae1dSRodney W. Grimes 	}
3620d94caffSDavid Greenman 	if (object->pager && (object->pager->pg_type == PG_VNODE)) {
3630d94caffSDavid Greenman 		vn_pager_t vnp = object->pager->pg_data;
364df8bae1dSRodney W. Grimes 
3650d94caffSDavid Greenman 		vp = vnp->vnp_vp;
3660d94caffSDavid Greenman 		VOP_FSYNC(vp, NOCRED, MNT_WAIT, NULL);
3670d94caffSDavid Greenman 		vinvalbuf(vp, 0, NOCRED, NULL, 0, 0);
3680d94caffSDavid Greenman 	}
369df8bae1dSRodney W. Grimes 	/*
3700d94caffSDavid Greenman 	 * Wait until the pageout daemon is through with the object.
371df8bae1dSRodney W. Grimes 	 */
37226f9a767SRodney W. Grimes 
3730d94caffSDavid Greenman 	s = splhigh();
374df8bae1dSRodney W. Grimes 	while (object->paging_in_progress) {
3750d94caffSDavid Greenman 		vm_object_unlock(object);
3760d94caffSDavid Greenman 		tsleep((caddr_t) object, PVM, "objtrm", 0);
377df8bae1dSRodney W. Grimes 		vm_object_lock(object);
378df8bae1dSRodney W. Grimes 	}
3790d94caffSDavid Greenman 	splx(s);
380df8bae1dSRodney W. Grimes 
381df8bae1dSRodney W. Grimes 	/*
3820d94caffSDavid Greenman 	 * While the paging system is locked, pull the object's pages off the
3830d94caffSDavid Greenman 	 * active and inactive queues.  This keeps the pageout daemon from
3840d94caffSDavid Greenman 	 * playing with them during vm_pager_deallocate.
385df8bae1dSRodney W. Grimes 	 *
3860d94caffSDavid Greenman 	 * We can't free the pages yet, because the object's pager may have to
3870d94caffSDavid Greenman 	 * write them out before deallocating the paging space.
388df8bae1dSRodney W. Grimes 	 */
38926f9a767SRodney W. Grimes 
3900d94caffSDavid Greenman 	for (p = object->memq.tqh_first; p; p = next) {
39126f9a767SRodney W. Grimes 		VM_PAGE_CHECK(p);
3920d94caffSDavid Greenman 		next = p->listq.tqe_next;
39326f9a767SRodney W. Grimes 
39426f9a767SRodney W. Grimes 		vm_page_lock_queues();
3950d94caffSDavid Greenman 		if (p->flags & PG_CACHE)
3960d94caffSDavid Greenman 			vm_page_free(p);
3970d94caffSDavid Greenman 		else
3980d94caffSDavid Greenman 			vm_page_unqueue(p);
39926f9a767SRodney W. Grimes 		vm_page_unlock_queues();
4000d94caffSDavid Greenman 		p = next;
40126f9a767SRodney W. Grimes 	}
40226f9a767SRodney W. Grimes 
40326f9a767SRodney W. Grimes 	if (object->paging_in_progress != 0)
40426f9a767SRodney W. Grimes 		panic("vm_object_deallocate: pageout in progress");
40526f9a767SRodney W. Grimes 
40626f9a767SRodney W. Grimes 	/*
4070d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
4080d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
40926f9a767SRodney W. Grimes 	 */
41026f9a767SRodney W. Grimes 
411a465acdaSDavid Greenman 	if (((object->flags & OBJ_INTERNAL) == 0) &&
412a465acdaSDavid Greenman 	    object->pager && (object->pager->pg_type != PG_DEVICE)) {
413df8bae1dSRodney W. Grimes 		(void) vm_object_page_clean(object, 0, 0, TRUE, TRUE);
414df8bae1dSRodney W. Grimes 	}
415df8bae1dSRodney W. Grimes 	/*
4160d94caffSDavid Greenman 	 * one last time -- get rid of buffers that might have been created
4170d94caffSDavid Greenman 	 * for the vm_object_page_clean
4180d94caffSDavid Greenman 	 */
4190d94caffSDavid Greenman 	if (vp != NULL) {
4200d94caffSDavid Greenman 		vm_object_unlock(object);
4210d94caffSDavid Greenman 		vinvalbuf(vp, 0, NOCRED, NULL, 0, 0);
4220d94caffSDavid Greenman 		vm_object_lock(object);
4230d94caffSDavid Greenman 	}
4240d94caffSDavid Greenman 	/*
4250d94caffSDavid Greenman 	 * Now free the pages. For internal objects, this also removes them
4260d94caffSDavid Greenman 	 * from paging queues.
427df8bae1dSRodney W. Grimes 	 */
428df8bae1dSRodney W. Grimes 	while ((p = object->memq.tqh_first) != NULL) {
429df8bae1dSRodney W. Grimes 		VM_PAGE_CHECK(p);
430df8bae1dSRodney W. Grimes 		vm_page_lock_queues();
4310d94caffSDavid Greenman 		PAGE_WAKEUP(p);
432df8bae1dSRodney W. Grimes 		vm_page_free(p);
433df8bae1dSRodney W. Grimes 		cnt.v_pfree++;
434df8bae1dSRodney W. Grimes 		vm_page_unlock_queues();
435df8bae1dSRodney W. Grimes 	}
4360d94caffSDavid Greenman 	vm_object_unlock(object);
437df8bae1dSRodney W. Grimes 
438df8bae1dSRodney W. Grimes 	/*
439df8bae1dSRodney W. Grimes 	 * Let the pager know object is dead.
440df8bae1dSRodney W. Grimes 	 */
441df8bae1dSRodney W. Grimes 	if (object->pager != NULL)
442df8bae1dSRodney W. Grimes 		vm_pager_deallocate(object->pager);
443df8bae1dSRodney W. Grimes 
444df8bae1dSRodney W. Grimes 	simple_lock(&vm_object_list_lock);
445df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
446df8bae1dSRodney W. Grimes 	vm_object_count--;
447df8bae1dSRodney W. Grimes 	simple_unlock(&vm_object_list_lock);
448df8bae1dSRodney W. Grimes 
449df8bae1dSRodney W. Grimes 	/*
450df8bae1dSRodney W. Grimes 	 * Free the space for the object.
451df8bae1dSRodney W. Grimes 	 */
452df8bae1dSRodney W. Grimes 	free((caddr_t) object, M_VMOBJ);
453df8bae1dSRodney W. Grimes }
454df8bae1dSRodney W. Grimes 
455df8bae1dSRodney W. Grimes /*
456df8bae1dSRodney W. Grimes  *	vm_object_page_clean
457df8bae1dSRodney W. Grimes  *
458df8bae1dSRodney W. Grimes  *	Clean all dirty pages in the specified range of object.
45926f9a767SRodney W. Grimes  *	Leaves page on whatever queue it is currently on.
46026f9a767SRodney W. Grimes  *
46126f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
46226f9a767SRodney W. Grimes  *
46326f9a767SRodney W. Grimes  *	The object must be locked.
46426f9a767SRodney W. Grimes  */
46526f9a767SRodney W. Grimes #if 1
46626f9a767SRodney W. Grimes boolean_t
46726f9a767SRodney W. Grimes vm_object_page_clean(object, start, end, syncio, de_queue)
46826f9a767SRodney W. Grimes 	register vm_object_t object;
46926f9a767SRodney W. Grimes 	register vm_offset_t start;
47026f9a767SRodney W. Grimes 	register vm_offset_t end;
47126f9a767SRodney W. Grimes 	boolean_t syncio;
47226f9a767SRodney W. Grimes 	boolean_t de_queue;
47326f9a767SRodney W. Grimes {
47426f9a767SRodney W. Grimes 	register vm_page_t p, nextp;
47526f9a767SRodney W. Grimes 	int size;
47626f9a767SRodney W. Grimes 
47726f9a767SRodney W. Grimes 	if (object->pager == NULL)
47826f9a767SRodney W. Grimes 		return 1;
47926f9a767SRodney W. Grimes 
48026f9a767SRodney W. Grimes 	if (start != end) {
48126f9a767SRodney W. Grimes 		start = trunc_page(start);
48226f9a767SRodney W. Grimes 		end = round_page(end);
48326f9a767SRodney W. Grimes 	}
48426f9a767SRodney W. Grimes 	size = end - start;
48526f9a767SRodney W. Grimes 
48626f9a767SRodney W. Grimes again:
48726f9a767SRodney W. Grimes 	/*
48826f9a767SRodney W. Grimes 	 * Wait until the pageout daemon is through with the object.
48926f9a767SRodney W. Grimes 	 */
49026f9a767SRodney W. Grimes 	while (object->paging_in_progress) {
4910d94caffSDavid Greenman 		tsleep(object, PVM, "objpcw", 0);
49226f9a767SRodney W. Grimes 	}
49326f9a767SRodney W. Grimes 
49426f9a767SRodney W. Grimes 	nextp = object->memq.tqh_first;
49526f9a767SRodney W. Grimes 	while ((p = nextp) && ((start == end) || (size != 0))) {
49626f9a767SRodney W. Grimes 		nextp = p->listq.tqe_next;
49726f9a767SRodney W. Grimes 		if (start == end || (p->offset >= start && p->offset < end)) {
4980d94caffSDavid Greenman 			if ((p->flags & PG_BUSY) || p->busy) {
4990d94caffSDavid Greenman 				int s = splhigh();
50026f9a767SRodney W. Grimes 
5010d94caffSDavid Greenman 				p->flags |= PG_WANTED;
5020d94caffSDavid Greenman 				tsleep(p, PVM, "objpcn", 0);
5030d94caffSDavid Greenman 				splx(s);
5040d94caffSDavid Greenman 				goto again;
5050d94caffSDavid Greenman 			}
50626f9a767SRodney W. Grimes 			size -= PAGE_SIZE;
50726f9a767SRodney W. Grimes 
5080d94caffSDavid Greenman 			vm_page_test_dirty(p);
50926f9a767SRodney W. Grimes 
5100d94caffSDavid Greenman 			if ((p->dirty & p->valid) != 0) {
51126f9a767SRodney W. Grimes 				vm_pageout_clean(p, VM_PAGEOUT_FORCE);
51226f9a767SRodney W. Grimes 				goto again;
51326f9a767SRodney W. Grimes 			}
51426f9a767SRodney W. Grimes 		}
51526f9a767SRodney W. Grimes 	}
51626f9a767SRodney W. Grimes 	wakeup((caddr_t) object);
51726f9a767SRodney W. Grimes 	return 1;
51826f9a767SRodney W. Grimes }
51926f9a767SRodney W. Grimes #endif
52026f9a767SRodney W. Grimes /*
52126f9a767SRodney W. Grimes  *	vm_object_page_clean
52226f9a767SRodney W. Grimes  *
52326f9a767SRodney W. Grimes  *	Clean all dirty pages in the specified range of object.
524df8bae1dSRodney W. Grimes  *	If syncio is TRUE, page cleaning is done synchronously.
525df8bae1dSRodney W. Grimes  *	If de_queue is TRUE, pages are removed from any paging queue
526df8bae1dSRodney W. Grimes  *	they were on, otherwise they are left on whatever queue they
527df8bae1dSRodney W. Grimes  *	were on before the cleaning operation began.
528df8bae1dSRodney W. Grimes  *
529df8bae1dSRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
530df8bae1dSRodney W. Grimes  *
531df8bae1dSRodney W. Grimes  *	The object must be locked.
532df8bae1dSRodney W. Grimes  *
533df8bae1dSRodney W. Grimes  *	Returns TRUE if all was well, FALSE if there was a pager error
534df8bae1dSRodney W. Grimes  *	somewhere.  We attempt to clean (and dequeue) all pages regardless
535df8bae1dSRodney W. Grimes  *	of where an error occurs.
536df8bae1dSRodney W. Grimes  */
53726f9a767SRodney W. Grimes #if 0
538df8bae1dSRodney W. Grimes boolean_t
539df8bae1dSRodney W. Grimes vm_object_page_clean(object, start, end, syncio, de_queue)
540df8bae1dSRodney W. Grimes 	register vm_object_t object;
541df8bae1dSRodney W. Grimes 	register vm_offset_t start;
542df8bae1dSRodney W. Grimes 	register vm_offset_t end;
543df8bae1dSRodney W. Grimes 	boolean_t syncio;
544df8bae1dSRodney W. Grimes 	boolean_t de_queue;
545df8bae1dSRodney W. Grimes {
546df8bae1dSRodney W. Grimes 	register vm_page_t p;
547df8bae1dSRodney W. Grimes 	int onqueue;
548df8bae1dSRodney W. Grimes 	boolean_t noerror = TRUE;
549df8bae1dSRodney W. Grimes 
550df8bae1dSRodney W. Grimes 	if (object == NULL)
551df8bae1dSRodney W. Grimes 		return (TRUE);
552df8bae1dSRodney W. Grimes 
553df8bae1dSRodney W. Grimes 	/*
554df8bae1dSRodney W. Grimes 	 * If it is an internal object and there is no pager, attempt to
5550d94caffSDavid Greenman 	 * allocate one.  Note that vm_object_collapse may relocate one from a
5560d94caffSDavid Greenman 	 * collapsed object so we must recheck afterward.
557df8bae1dSRodney W. Grimes 	 */
558df8bae1dSRodney W. Grimes 	if ((object->flags & OBJ_INTERNAL) && object->pager == NULL) {
559df8bae1dSRodney W. Grimes 		vm_object_collapse(object);
560df8bae1dSRodney W. Grimes 		if (object->pager == NULL) {
561df8bae1dSRodney W. Grimes 			vm_pager_t pager;
562df8bae1dSRodney W. Grimes 
563df8bae1dSRodney W. Grimes 			vm_object_unlock(object);
564df8bae1dSRodney W. Grimes 			pager = vm_pager_allocate(PG_DFLT, (caddr_t) 0,
565df8bae1dSRodney W. Grimes 			    object->size, VM_PROT_ALL,
566df8bae1dSRodney W. Grimes 			    (vm_offset_t) 0);
567df8bae1dSRodney W. Grimes 			if (pager)
568df8bae1dSRodney W. Grimes 				vm_object_setpager(object, pager, 0, FALSE);
569df8bae1dSRodney W. Grimes 			vm_object_lock(object);
570df8bae1dSRodney W. Grimes 		}
571df8bae1dSRodney W. Grimes 	}
572df8bae1dSRodney W. Grimes 	if (object->pager == NULL)
573df8bae1dSRodney W. Grimes 		return (FALSE);
574df8bae1dSRodney W. Grimes 
575df8bae1dSRodney W. Grimes again:
576df8bae1dSRodney W. Grimes 	/*
577df8bae1dSRodney W. Grimes 	 * Wait until the pageout daemon is through with the object.
578df8bae1dSRodney W. Grimes 	 */
579df8bae1dSRodney W. Grimes 	while (object->paging_in_progress) {
580df8bae1dSRodney W. Grimes 		vm_object_sleep((int) object, object, FALSE);
581df8bae1dSRodney W. Grimes 		vm_object_lock(object);
582df8bae1dSRodney W. Grimes 	}
583df8bae1dSRodney W. Grimes 	/*
584df8bae1dSRodney W. Grimes 	 * Loop through the object page list cleaning as necessary.
585df8bae1dSRodney W. Grimes 	 */
586df8bae1dSRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) {
58726f9a767SRodney W. Grimes 		onqueue = 0;
588df8bae1dSRodney W. Grimes 		if ((start == end || p->offset >= start && p->offset < end) &&
589df8bae1dSRodney W. Grimes 		    !(p->flags & PG_FICTITIOUS)) {
5900d94caffSDavid Greenman 			vm_page_test_dirty(p);
591df8bae1dSRodney W. Grimes 			/*
5920d94caffSDavid Greenman 			 * Remove the page from any paging queue. This needs
5930d94caffSDavid Greenman 			 * to be done if either we have been explicitly asked
5940d94caffSDavid Greenman 			 * to do so or it is about to be cleaned (see comment
5950d94caffSDavid Greenman 			 * below).
596df8bae1dSRodney W. Grimes 			 */
5970d94caffSDavid Greenman 			if (de_queue || (p->dirty & p->valid)) {
598df8bae1dSRodney W. Grimes 				vm_page_lock_queues();
599df8bae1dSRodney W. Grimes 				if (p->flags & PG_ACTIVE) {
600df8bae1dSRodney W. Grimes 					TAILQ_REMOVE(&vm_page_queue_active,
601df8bae1dSRodney W. Grimes 					    p, pageq);
602df8bae1dSRodney W. Grimes 					p->flags &= ~PG_ACTIVE;
603df8bae1dSRodney W. Grimes 					cnt.v_active_count--;
604df8bae1dSRodney W. Grimes 					onqueue = 1;
605df8bae1dSRodney W. Grimes 				} else if (p->flags & PG_INACTIVE) {
606df8bae1dSRodney W. Grimes 					TAILQ_REMOVE(&vm_page_queue_inactive,
607df8bae1dSRodney W. Grimes 					    p, pageq);
608df8bae1dSRodney W. Grimes 					p->flags &= ~PG_INACTIVE;
609df8bae1dSRodney W. Grimes 					cnt.v_inactive_count--;
610df8bae1dSRodney W. Grimes 					onqueue = -1;
611df8bae1dSRodney W. Grimes 				} else
612df8bae1dSRodney W. Grimes 					onqueue = 0;
613df8bae1dSRodney W. Grimes 				vm_page_unlock_queues();
614df8bae1dSRodney W. Grimes 			}
615df8bae1dSRodney W. Grimes 			/*
616df8bae1dSRodney W. Grimes 			 * To ensure the state of the page doesn't change
6170d94caffSDavid Greenman 			 * during the clean operation we do two things. First
6180d94caffSDavid Greenman 			 * we set the busy bit and write-protect all mappings
6190d94caffSDavid Greenman 			 * to ensure that write accesses to the page block (in
6200d94caffSDavid Greenman 			 * vm_fault).  Second, we remove the page from any
6210d94caffSDavid Greenman 			 * paging queue to foil the pageout daemon
6220d94caffSDavid Greenman 			 * (vm_pageout_scan).
623df8bae1dSRodney W. Grimes 			 */
624df8bae1dSRodney W. Grimes 			pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_READ);
6250d94caffSDavid Greenman 			if (p->dirty & p->valid) {
626df8bae1dSRodney W. Grimes 				p->flags |= PG_BUSY;
627df8bae1dSRodney W. Grimes 				object->paging_in_progress++;
628df8bae1dSRodney W. Grimes 				vm_object_unlock(object);
629df8bae1dSRodney W. Grimes 				/*
6300d94caffSDavid Greenman 				 * XXX if put fails we mark the page as clean
6310d94caffSDavid Greenman 				 * to avoid an infinite loop. Will loose
6320d94caffSDavid Greenman 				 * changes to the page.
633df8bae1dSRodney W. Grimes 				 */
634df8bae1dSRodney W. Grimes 				if (vm_pager_put(object->pager, p, syncio)) {
635df8bae1dSRodney W. Grimes 					printf("%s: pager_put error\n",
636df8bae1dSRodney W. Grimes 					    "vm_object_page_clean");
6370d94caffSDavid Greenman 					p->dirty = 0;
638df8bae1dSRodney W. Grimes 					noerror = FALSE;
639df8bae1dSRodney W. Grimes 				}
640df8bae1dSRodney W. Grimes 				vm_object_lock(object);
641df8bae1dSRodney W. Grimes 				object->paging_in_progress--;
642df8bae1dSRodney W. Grimes 				if (!de_queue && onqueue) {
643df8bae1dSRodney W. Grimes 					vm_page_lock_queues();
644df8bae1dSRodney W. Grimes 					if (onqueue > 0)
645df8bae1dSRodney W. Grimes 						vm_page_activate(p);
646df8bae1dSRodney W. Grimes 					else
647df8bae1dSRodney W. Grimes 						vm_page_deactivate(p);
648df8bae1dSRodney W. Grimes 					vm_page_unlock_queues();
649df8bae1dSRodney W. Grimes 				}
650df8bae1dSRodney W. Grimes 				PAGE_WAKEUP(p);
651df8bae1dSRodney W. Grimes 				goto again;
652df8bae1dSRodney W. Grimes 			}
653df8bae1dSRodney W. Grimes 		}
654df8bae1dSRodney W. Grimes 	}
655df8bae1dSRodney W. Grimes 	return (noerror);
656df8bae1dSRodney W. Grimes }
65726f9a767SRodney W. Grimes #endif
658df8bae1dSRodney W. Grimes 
659df8bae1dSRodney W. Grimes /*
660df8bae1dSRodney W. Grimes  *	vm_object_deactivate_pages
661df8bae1dSRodney W. Grimes  *
662df8bae1dSRodney W. Grimes  *	Deactivate all pages in the specified object.  (Keep its pages
663df8bae1dSRodney W. Grimes  *	in memory even though it is no longer referenced.)
664df8bae1dSRodney W. Grimes  *
665df8bae1dSRodney W. Grimes  *	The object must be locked.
666df8bae1dSRodney W. Grimes  */
667df8bae1dSRodney W. Grimes void
668df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object)
669df8bae1dSRodney W. Grimes 	register vm_object_t object;
670df8bae1dSRodney W. Grimes {
671df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
672df8bae1dSRodney W. Grimes 
673df8bae1dSRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = next) {
674df8bae1dSRodney W. Grimes 		next = p->listq.tqe_next;
675df8bae1dSRodney W. Grimes 		vm_page_lock_queues();
676df8bae1dSRodney W. Grimes 		vm_page_deactivate(p);
677df8bae1dSRodney W. Grimes 		vm_page_unlock_queues();
678df8bae1dSRodney W. Grimes 	}
679df8bae1dSRodney W. Grimes }
680df8bae1dSRodney W. Grimes 
681df8bae1dSRodney W. Grimes /*
682df8bae1dSRodney W. Grimes  *	Trim the object cache to size.
683df8bae1dSRodney W. Grimes  */
684df8bae1dSRodney W. Grimes void
685df8bae1dSRodney W. Grimes vm_object_cache_trim()
686df8bae1dSRodney W. Grimes {
687df8bae1dSRodney W. Grimes 	register vm_object_t object;
688df8bae1dSRodney W. Grimes 
689df8bae1dSRodney W. Grimes 	vm_object_cache_lock();
6900d94caffSDavid Greenman 	while (vm_object_cached > vm_object_cache_max) {
691df8bae1dSRodney W. Grimes 		object = vm_object_cached_list.tqh_first;
692df8bae1dSRodney W. Grimes 		vm_object_cache_unlock();
693df8bae1dSRodney W. Grimes 
694df8bae1dSRodney W. Grimes 		if (object != vm_object_lookup(object->pager))
6958e58bf68SDavid Greenman 			panic("vm_object_cache_trim: I'm sooo confused.");
696df8bae1dSRodney W. Grimes 
697df8bae1dSRodney W. Grimes 		pager_cache(object, FALSE);
698df8bae1dSRodney W. Grimes 
699df8bae1dSRodney W. Grimes 		vm_object_cache_lock();
700df8bae1dSRodney W. Grimes 	}
701df8bae1dSRodney W. Grimes 	vm_object_cache_unlock();
702df8bae1dSRodney W. Grimes }
703df8bae1dSRodney W. Grimes 
70426f9a767SRodney W. Grimes 
705df8bae1dSRodney W. Grimes /*
706df8bae1dSRodney W. Grimes  *	vm_object_pmap_copy:
707df8bae1dSRodney W. Grimes  *
708df8bae1dSRodney W. Grimes  *	Makes all physical pages in the specified
709df8bae1dSRodney W. Grimes  *	object range copy-on-write.  No writeable
710df8bae1dSRodney W. Grimes  *	references to these pages should remain.
711df8bae1dSRodney W. Grimes  *
712df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
713df8bae1dSRodney W. Grimes  */
7140d94caffSDavid Greenman void
7150d94caffSDavid Greenman vm_object_pmap_copy(object, start, end)
716df8bae1dSRodney W. Grimes 	register vm_object_t object;
717df8bae1dSRodney W. Grimes 	register vm_offset_t start;
718df8bae1dSRodney W. Grimes 	register vm_offset_t end;
719df8bae1dSRodney W. Grimes {
720df8bae1dSRodney W. Grimes 	register vm_page_t p;
721df8bae1dSRodney W. Grimes 
722df8bae1dSRodney W. Grimes 	if (object == NULL)
723df8bae1dSRodney W. Grimes 		return;
724df8bae1dSRodney W. Grimes 
725df8bae1dSRodney W. Grimes 	vm_object_lock(object);
726df8bae1dSRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) {
727df8bae1dSRodney W. Grimes 		if ((start <= p->offset) && (p->offset < end)) {
728df8bae1dSRodney W. Grimes 			pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_READ);
729df8bae1dSRodney W. Grimes 			p->flags |= PG_COPYONWRITE;
730df8bae1dSRodney W. Grimes 		}
731df8bae1dSRodney W. Grimes 	}
732df8bae1dSRodney W. Grimes 	vm_object_unlock(object);
733df8bae1dSRodney W. Grimes }
734df8bae1dSRodney W. Grimes 
735df8bae1dSRodney W. Grimes /*
736df8bae1dSRodney W. Grimes  *	vm_object_pmap_remove:
737df8bae1dSRodney W. Grimes  *
738df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
739df8bae1dSRodney W. Grimes  *	object range from all physical maps.
740df8bae1dSRodney W. Grimes  *
741df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
742df8bae1dSRodney W. Grimes  */
74326f9a767SRodney W. Grimes void
74426f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end)
745df8bae1dSRodney W. Grimes 	register vm_object_t object;
746df8bae1dSRodney W. Grimes 	register vm_offset_t start;
747df8bae1dSRodney W. Grimes 	register vm_offset_t end;
748df8bae1dSRodney W. Grimes {
749df8bae1dSRodney W. Grimes 	register vm_page_t p;
750a481f200SDavid Greenman 	int s;
751df8bae1dSRodney W. Grimes 
752df8bae1dSRodney W. Grimes 	if (object == NULL)
753df8bae1dSRodney W. Grimes 		return;
7542fe6e4d7SDavid Greenman 	++object->paging_in_progress;
755df8bae1dSRodney W. Grimes 
756df8bae1dSRodney W. Grimes 	vm_object_lock(object);
75726f9a767SRodney W. Grimes again:
75826f9a767SRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) {
75926f9a767SRodney W. Grimes 		if ((start <= p->offset) && (p->offset < end)) {
760a481f200SDavid Greenman 			s = splhigh();
7610d94caffSDavid Greenman 			if ((p->flags & PG_BUSY) || p->busy) {
76226f9a767SRodney W. Grimes 				p->flags |= PG_WANTED;
76326f9a767SRodney W. Grimes 				tsleep((caddr_t) p, PVM, "vmopmr", 0);
764a481f200SDavid Greenman 				splx(s);
76526f9a767SRodney W. Grimes 				goto again;
76626f9a767SRodney W. Grimes 			}
767a481f200SDavid Greenman 			splx(s);
768df8bae1dSRodney W. Grimes 			pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
76926f9a767SRodney W. Grimes 		}
77026f9a767SRodney W. Grimes 	}
771df8bae1dSRodney W. Grimes 	vm_object_unlock(object);
7722fe6e4d7SDavid Greenman 	--object->paging_in_progress;
7732fe6e4d7SDavid Greenman 	if (object->paging_in_progress == 0)
7742fe6e4d7SDavid Greenman 		wakeup((caddr_t) object);
775df8bae1dSRodney W. Grimes }
776df8bae1dSRodney W. Grimes 
777df8bae1dSRodney W. Grimes /*
778df8bae1dSRodney W. Grimes  *	vm_object_copy:
779df8bae1dSRodney W. Grimes  *
780df8bae1dSRodney W. Grimes  *	Create a new object which is a copy of an existing
781df8bae1dSRodney W. Grimes  *	object, and mark all of the pages in the existing
782df8bae1dSRodney W. Grimes  *	object 'copy-on-write'.  The new object has one reference.
783df8bae1dSRodney W. Grimes  *	Returns the new object.
784df8bae1dSRodney W. Grimes  *
785df8bae1dSRodney W. Grimes  *	May defer the copy until later if the object is not backed
786df8bae1dSRodney W. Grimes  *	up by a non-default pager.
787df8bae1dSRodney W. Grimes  */
7880d94caffSDavid Greenman void
7890d94caffSDavid Greenman vm_object_copy(src_object, src_offset, size,
790df8bae1dSRodney W. Grimes     dst_object, dst_offset, src_needs_copy)
791df8bae1dSRodney W. Grimes 	register vm_object_t src_object;
792df8bae1dSRodney W. Grimes 	vm_offset_t src_offset;
793df8bae1dSRodney W. Grimes 	vm_size_t size;
794df8bae1dSRodney W. Grimes 	vm_object_t *dst_object;/* OUT */
795df8bae1dSRodney W. Grimes 	vm_offset_t *dst_offset;/* OUT */
796df8bae1dSRodney W. Grimes 	boolean_t *src_needs_copy;	/* OUT */
797df8bae1dSRodney W. Grimes {
798df8bae1dSRodney W. Grimes 	register vm_object_t new_copy;
799df8bae1dSRodney W. Grimes 	register vm_object_t old_copy;
800df8bae1dSRodney W. Grimes 	vm_offset_t new_start, new_end;
801df8bae1dSRodney W. Grimes 
802df8bae1dSRodney W. Grimes 	register vm_page_t p;
803df8bae1dSRodney W. Grimes 
804df8bae1dSRodney W. Grimes 	if (src_object == NULL) {
805df8bae1dSRodney W. Grimes 		/*
806df8bae1dSRodney W. Grimes 		 * Nothing to copy
807df8bae1dSRodney W. Grimes 		 */
808df8bae1dSRodney W. Grimes 		*dst_object = NULL;
809df8bae1dSRodney W. Grimes 		*dst_offset = 0;
810df8bae1dSRodney W. Grimes 		*src_needs_copy = FALSE;
811df8bae1dSRodney W. Grimes 		return;
812df8bae1dSRodney W. Grimes 	}
813df8bae1dSRodney W. Grimes 	/*
8140d94caffSDavid Greenman 	 * If the object's pager is null_pager or the default pager, we don't
8150d94caffSDavid Greenman 	 * have to make a copy of it.  Instead, we set the needs copy flag and
816df8bae1dSRodney W. Grimes 	 * make a shadow later.
817df8bae1dSRodney W. Grimes 	 */
818df8bae1dSRodney W. Grimes 
819df8bae1dSRodney W. Grimes 	vm_object_lock(src_object);
82026f9a767SRodney W. Grimes 
82126f9a767SRodney W. Grimes 	/*
82226f9a767SRodney W. Grimes 	 * Try to collapse the object before copying it.
82326f9a767SRodney W. Grimes 	 */
82426f9a767SRodney W. Grimes 
82526f9a767SRodney W. Grimes 	vm_object_collapse(src_object);
82626f9a767SRodney W. Grimes 
827df8bae1dSRodney W. Grimes 	if (src_object->pager == NULL ||
828df8bae1dSRodney W. Grimes 	    (src_object->flags & OBJ_INTERNAL)) {
829df8bae1dSRodney W. Grimes 
830df8bae1dSRodney W. Grimes 		/*
831df8bae1dSRodney W. Grimes 		 * Make another reference to the object
832df8bae1dSRodney W. Grimes 		 */
833df8bae1dSRodney W. Grimes 		src_object->ref_count++;
834df8bae1dSRodney W. Grimes 
835df8bae1dSRodney W. Grimes 		/*
836df8bae1dSRodney W. Grimes 		 * Mark all of the pages copy-on-write.
837df8bae1dSRodney W. Grimes 		 */
838df8bae1dSRodney W. Grimes 		for (p = src_object->memq.tqh_first; p; p = p->listq.tqe_next)
839df8bae1dSRodney W. Grimes 			if (src_offset <= p->offset &&
840df8bae1dSRodney W. Grimes 			    p->offset < src_offset + size)
841df8bae1dSRodney W. Grimes 				p->flags |= PG_COPYONWRITE;
842df8bae1dSRodney W. Grimes 		vm_object_unlock(src_object);
843df8bae1dSRodney W. Grimes 
844df8bae1dSRodney W. Grimes 		*dst_object = src_object;
845df8bae1dSRodney W. Grimes 		*dst_offset = src_offset;
846df8bae1dSRodney W. Grimes 
847df8bae1dSRodney W. Grimes 		/*
848df8bae1dSRodney W. Grimes 		 * Must make a shadow when write is desired
849df8bae1dSRodney W. Grimes 		 */
850df8bae1dSRodney W. Grimes 		*src_needs_copy = TRUE;
851df8bae1dSRodney W. Grimes 		return;
852df8bae1dSRodney W. Grimes 	}
853df8bae1dSRodney W. Grimes 	/*
8540d94caffSDavid Greenman 	 * If the object has a pager, the pager wants to see all of the
8550d94caffSDavid Greenman 	 * changes.  We need a copy-object for the changed pages.
856df8bae1dSRodney W. Grimes 	 *
8570d94caffSDavid Greenman 	 * If there is a copy-object, and it is empty, no changes have been made
8580d94caffSDavid Greenman 	 * to the object since the copy-object was made.  We can use the same
8590d94caffSDavid Greenman 	 * copy- object.
860df8bae1dSRodney W. Grimes 	 */
861df8bae1dSRodney W. Grimes 
862df8bae1dSRodney W. Grimes Retry1:
863df8bae1dSRodney W. Grimes 	old_copy = src_object->copy;
864df8bae1dSRodney W. Grimes 	if (old_copy != NULL) {
865df8bae1dSRodney W. Grimes 		/*
866df8bae1dSRodney W. Grimes 		 * Try to get the locks (out of order)
867df8bae1dSRodney W. Grimes 		 */
868df8bae1dSRodney W. Grimes 		if (!vm_object_lock_try(old_copy)) {
869df8bae1dSRodney W. Grimes 			vm_object_unlock(src_object);
870df8bae1dSRodney W. Grimes 
871df8bae1dSRodney W. Grimes 			/* should spin a bit here... */
8720d94caffSDavid Greenman 			tsleep((caddr_t) old_copy, PVM, "cpylck", 1);
873df8bae1dSRodney W. Grimes 			vm_object_lock(src_object);
874df8bae1dSRodney W. Grimes 			goto Retry1;
875df8bae1dSRodney W. Grimes 		}
876df8bae1dSRodney W. Grimes 		if (old_copy->resident_page_count == 0 &&
877df8bae1dSRodney W. Grimes 		    old_copy->pager == NULL) {
878df8bae1dSRodney W. Grimes 			/*
8790d94caffSDavid Greenman 			 * Return another reference to the existing
8800d94caffSDavid Greenman 			 * copy-object.
881df8bae1dSRodney W. Grimes 			 */
882df8bae1dSRodney W. Grimes 			old_copy->ref_count++;
883df8bae1dSRodney W. Grimes 			vm_object_unlock(old_copy);
884df8bae1dSRodney W. Grimes 			vm_object_unlock(src_object);
885df8bae1dSRodney W. Grimes 			*dst_object = old_copy;
886df8bae1dSRodney W. Grimes 			*dst_offset = src_offset;
887df8bae1dSRodney W. Grimes 			*src_needs_copy = FALSE;
888df8bae1dSRodney W. Grimes 			return;
889df8bae1dSRodney W. Grimes 		}
890df8bae1dSRodney W. Grimes 		vm_object_unlock(old_copy);
891df8bae1dSRodney W. Grimes 	}
892df8bae1dSRodney W. Grimes 	vm_object_unlock(src_object);
893df8bae1dSRodney W. Grimes 
894df8bae1dSRodney W. Grimes 	/*
8950d94caffSDavid Greenman 	 * If the object has a pager, the pager wants to see all of the
8960d94caffSDavid Greenman 	 * changes.  We must make a copy-object and put the changed pages
8970d94caffSDavid Greenman 	 * there.
898df8bae1dSRodney W. Grimes 	 *
8990d94caffSDavid Greenman 	 * The copy-object is always made large enough to completely shadow the
9000d94caffSDavid Greenman 	 * original object, since it may have several users who want to shadow
901df8bae1dSRodney W. Grimes 	 * the original object at different points.
902df8bae1dSRodney W. Grimes 	 */
903df8bae1dSRodney W. Grimes 
904df8bae1dSRodney W. Grimes 	new_copy = vm_object_allocate(src_object->size);
905df8bae1dSRodney W. Grimes 
906df8bae1dSRodney W. Grimes Retry2:
907df8bae1dSRodney W. Grimes 	vm_object_lock(src_object);
908df8bae1dSRodney W. Grimes 	/*
909df8bae1dSRodney W. Grimes 	 * Copy object may have changed while we were unlocked
910df8bae1dSRodney W. Grimes 	 */
911df8bae1dSRodney W. Grimes 	old_copy = src_object->copy;
912df8bae1dSRodney W. Grimes 	if (old_copy != NULL) {
913df8bae1dSRodney W. Grimes 		/*
914df8bae1dSRodney W. Grimes 		 * Try to get the locks (out of order)
915df8bae1dSRodney W. Grimes 		 */
916df8bae1dSRodney W. Grimes 		if (!vm_object_lock_try(old_copy)) {
917df8bae1dSRodney W. Grimes 			vm_object_unlock(src_object);
9180d94caffSDavid Greenman 			tsleep((caddr_t) old_copy, PVM, "cpylck", 1);
919df8bae1dSRodney W. Grimes 			goto Retry2;
920df8bae1dSRodney W. Grimes 		}
921df8bae1dSRodney W. Grimes 		/*
922df8bae1dSRodney W. Grimes 		 * Consistency check
923df8bae1dSRodney W. Grimes 		 */
924df8bae1dSRodney W. Grimes 		if (old_copy->shadow != src_object ||
925df8bae1dSRodney W. Grimes 		    old_copy->shadow_offset != (vm_offset_t) 0)
926df8bae1dSRodney W. Grimes 			panic("vm_object_copy: copy/shadow inconsistency");
927df8bae1dSRodney W. Grimes 
928df8bae1dSRodney W. Grimes 		/*
9290d94caffSDavid Greenman 		 * Make the old copy-object shadow the new one. It will
9300d94caffSDavid Greenman 		 * receive no more pages from the original object.
931df8bae1dSRodney W. Grimes 		 */
932df8bae1dSRodney W. Grimes 
933df8bae1dSRodney W. Grimes 		src_object->ref_count--;	/* remove ref. from old_copy */
9342fe6e4d7SDavid Greenman 		if (old_copy->shadow)
9352fe6e4d7SDavid Greenman 			TAILQ_REMOVE(&old_copy->shadow->reverse_shadow_head, old_copy, reverse_shadow_list);
936df8bae1dSRodney W. Grimes 		old_copy->shadow = new_copy;
9372fe6e4d7SDavid Greenman 		TAILQ_INSERT_TAIL(&old_copy->shadow->reverse_shadow_head, old_copy, reverse_shadow_list);
9380d94caffSDavid Greenman 		new_copy->ref_count++;	/* locking not needed - we have the
9390d94caffSDavid Greenman 					 * only pointer */
940df8bae1dSRodney W. Grimes 		vm_object_unlock(old_copy);	/* done with old_copy */
941df8bae1dSRodney W. Grimes 	}
942df8bae1dSRodney W. Grimes 	new_start = (vm_offset_t) 0;	/* always shadow original at 0 */
943df8bae1dSRodney W. Grimes 	new_end = (vm_offset_t) new_copy->size;	/* for the whole object */
944df8bae1dSRodney W. Grimes 
945df8bae1dSRodney W. Grimes 	/*
946df8bae1dSRodney W. Grimes 	 * Point the new copy at the existing object.
947df8bae1dSRodney W. Grimes 	 */
948df8bae1dSRodney W. Grimes 
949df8bae1dSRodney W. Grimes 	new_copy->shadow = src_object;
9502fe6e4d7SDavid Greenman 	TAILQ_INSERT_TAIL(&new_copy->shadow->reverse_shadow_head, new_copy, reverse_shadow_list);
951df8bae1dSRodney W. Grimes 	new_copy->shadow_offset = new_start;
952df8bae1dSRodney W. Grimes 	src_object->ref_count++;
953df8bae1dSRodney W. Grimes 	src_object->copy = new_copy;
954df8bae1dSRodney W. Grimes 
955df8bae1dSRodney W. Grimes 	/*
9560d94caffSDavid Greenman 	 * Mark all the affected pages of the existing object copy-on-write.
957df8bae1dSRodney W. Grimes 	 */
958df8bae1dSRodney W. Grimes 	for (p = src_object->memq.tqh_first; p != NULL; p = p->listq.tqe_next)
959df8bae1dSRodney W. Grimes 		if ((new_start <= p->offset) && (p->offset < new_end))
960df8bae1dSRodney W. Grimes 			p->flags |= PG_COPYONWRITE;
961df8bae1dSRodney W. Grimes 
962df8bae1dSRodney W. Grimes 	vm_object_unlock(src_object);
963df8bae1dSRodney W. Grimes 
964df8bae1dSRodney W. Grimes 	*dst_object = new_copy;
965df8bae1dSRodney W. Grimes 	*dst_offset = src_offset - new_start;
966df8bae1dSRodney W. Grimes 	*src_needs_copy = FALSE;
967df8bae1dSRodney W. Grimes }
968df8bae1dSRodney W. Grimes 
969df8bae1dSRodney W. Grimes /*
970df8bae1dSRodney W. Grimes  *	vm_object_shadow:
971df8bae1dSRodney W. Grimes  *
972df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
973df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
974df8bae1dSRodney W. Grimes  *	object reference is deallocated.
975df8bae1dSRodney W. Grimes  *
976df8bae1dSRodney W. Grimes  *	The new object and offset into that object
977df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
978df8bae1dSRodney W. Grimes  */
979df8bae1dSRodney W. Grimes 
98026f9a767SRodney W. Grimes void
98126f9a767SRodney W. Grimes vm_object_shadow(object, offset, length)
982df8bae1dSRodney W. Grimes 	vm_object_t *object;	/* IN/OUT */
983df8bae1dSRodney W. Grimes 	vm_offset_t *offset;	/* IN/OUT */
984df8bae1dSRodney W. Grimes 	vm_size_t length;
985df8bae1dSRodney W. Grimes {
986df8bae1dSRodney W. Grimes 	register vm_object_t source;
987df8bae1dSRodney W. Grimes 	register vm_object_t result;
988df8bae1dSRodney W. Grimes 
989df8bae1dSRodney W. Grimes 	source = *object;
990df8bae1dSRodney W. Grimes 
991df8bae1dSRodney W. Grimes 	/*
992df8bae1dSRodney W. Grimes 	 * Allocate a new object with the given length
993df8bae1dSRodney W. Grimes 	 */
994df8bae1dSRodney W. Grimes 
995df8bae1dSRodney W. Grimes 	if ((result = vm_object_allocate(length)) == NULL)
996df8bae1dSRodney W. Grimes 		panic("vm_object_shadow: no object for shadowing");
997df8bae1dSRodney W. Grimes 
998df8bae1dSRodney W. Grimes 	/*
9990d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
10000d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
10010d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
10020d94caffSDavid Greenman 	 * of reference count.
1003df8bae1dSRodney W. Grimes 	 */
1004df8bae1dSRodney W. Grimes 	result->shadow = source;
10057b18a718SDavid Greenman 	if (source)
10062fe6e4d7SDavid Greenman 		TAILQ_INSERT_TAIL(&result->shadow->reverse_shadow_head, result, reverse_shadow_list);
1007df8bae1dSRodney W. Grimes 
1008df8bae1dSRodney W. Grimes 	/*
10090d94caffSDavid Greenman 	 * Store the offset into the source object, and fix up the offset into
10100d94caffSDavid Greenman 	 * the new object.
1011df8bae1dSRodney W. Grimes 	 */
1012df8bae1dSRodney W. Grimes 
1013df8bae1dSRodney W. Grimes 	result->shadow_offset = *offset;
1014df8bae1dSRodney W. Grimes 
1015df8bae1dSRodney W. Grimes 	/*
1016df8bae1dSRodney W. Grimes 	 * Return the new things
1017df8bae1dSRodney W. Grimes 	 */
1018df8bae1dSRodney W. Grimes 
1019df8bae1dSRodney W. Grimes 	*offset = 0;
1020df8bae1dSRodney W. Grimes 	*object = result;
1021df8bae1dSRodney W. Grimes }
1022df8bae1dSRodney W. Grimes 
1023df8bae1dSRodney W. Grimes /*
1024df8bae1dSRodney W. Grimes  *	Set the specified object's pager to the specified pager.
1025df8bae1dSRodney W. Grimes  */
1026df8bae1dSRodney W. Grimes 
102726f9a767SRodney W. Grimes void
102826f9a767SRodney W. Grimes vm_object_setpager(object, pager, paging_offset,
1029df8bae1dSRodney W. Grimes     read_only)
1030df8bae1dSRodney W. Grimes 	vm_object_t object;
1031df8bae1dSRodney W. Grimes 	vm_pager_t pager;
1032df8bae1dSRodney W. Grimes 	vm_offset_t paging_offset;
1033df8bae1dSRodney W. Grimes 	boolean_t read_only;
1034df8bae1dSRodney W. Grimes {
1035df8bae1dSRodney W. Grimes 	vm_object_lock(object);	/* XXX ? */
103626f9a767SRodney W. Grimes 	if (object->pager && object->pager != pager) {
103726f9a767SRodney W. Grimes 		panic("!!!pager already allocated!!!\n");
103826f9a767SRodney W. Grimes 	}
1039df8bae1dSRodney W. Grimes 	object->pager = pager;
1040df8bae1dSRodney W. Grimes 	object->paging_offset = paging_offset;
1041df8bae1dSRodney W. Grimes 	vm_object_unlock(object);	/* XXX ? */
1042df8bae1dSRodney W. Grimes }
1043df8bae1dSRodney W. Grimes 
1044df8bae1dSRodney W. Grimes /*
1045df8bae1dSRodney W. Grimes  *	vm_object_hash hashes the pager/id pair.
1046df8bae1dSRodney W. Grimes  */
1047df8bae1dSRodney W. Grimes 
1048df8bae1dSRodney W. Grimes #define vm_object_hash(pager) \
104926f9a767SRodney W. Grimes 	(((unsigned)pager >> 5)%VM_OBJECT_HASH_COUNT)
1050df8bae1dSRodney W. Grimes 
1051df8bae1dSRodney W. Grimes /*
1052df8bae1dSRodney W. Grimes  *	vm_object_lookup looks in the object cache for an object with the
1053df8bae1dSRodney W. Grimes  *	specified pager and paging id.
1054df8bae1dSRodney W. Grimes  */
1055df8bae1dSRodney W. Grimes 
10560d94caffSDavid Greenman vm_object_t
10570d94caffSDavid Greenman vm_object_lookup(pager)
1058df8bae1dSRodney W. Grimes 	vm_pager_t pager;
1059df8bae1dSRodney W. Grimes {
1060df8bae1dSRodney W. Grimes 	register vm_object_hash_entry_t entry;
1061df8bae1dSRodney W. Grimes 	vm_object_t object;
1062df8bae1dSRodney W. Grimes 
1063a08a17a3SDavid Greenman 	cnt.v_lookups++;
1064df8bae1dSRodney W. Grimes 	vm_object_cache_lock();
1065df8bae1dSRodney W. Grimes 
1066df8bae1dSRodney W. Grimes 	for (entry = vm_object_hashtable[vm_object_hash(pager)].tqh_first;
1067df8bae1dSRodney W. Grimes 	    entry != NULL;
1068df8bae1dSRodney W. Grimes 	    entry = entry->hash_links.tqe_next) {
1069df8bae1dSRodney W. Grimes 		object = entry->object;
1070df8bae1dSRodney W. Grimes 		if (object->pager == pager) {
1071df8bae1dSRodney W. Grimes 			vm_object_lock(object);
1072df8bae1dSRodney W. Grimes 			if (object->ref_count == 0) {
1073df8bae1dSRodney W. Grimes 				TAILQ_REMOVE(&vm_object_cached_list, object,
1074df8bae1dSRodney W. Grimes 				    cached_list);
1075df8bae1dSRodney W. Grimes 				vm_object_cached--;
1076df8bae1dSRodney W. Grimes 			}
1077df8bae1dSRodney W. Grimes 			object->ref_count++;
1078df8bae1dSRodney W. Grimes 			vm_object_unlock(object);
1079df8bae1dSRodney W. Grimes 			vm_object_cache_unlock();
1080a08a17a3SDavid Greenman 			cnt.v_hits++;
1081df8bae1dSRodney W. Grimes 			return (object);
1082df8bae1dSRodney W. Grimes 		}
1083df8bae1dSRodney W. Grimes 	}
1084df8bae1dSRodney W. Grimes 
1085df8bae1dSRodney W. Grimes 	vm_object_cache_unlock();
1086df8bae1dSRodney W. Grimes 	return (NULL);
1087df8bae1dSRodney W. Grimes }
1088df8bae1dSRodney W. Grimes 
1089df8bae1dSRodney W. Grimes /*
1090df8bae1dSRodney W. Grimes  *	vm_object_enter enters the specified object/pager/id into
1091df8bae1dSRodney W. Grimes  *	the hash table.
1092df8bae1dSRodney W. Grimes  */
1093df8bae1dSRodney W. Grimes 
10940d94caffSDavid Greenman void
10950d94caffSDavid Greenman vm_object_enter(object, pager)
1096df8bae1dSRodney W. Grimes 	vm_object_t object;
1097df8bae1dSRodney W. Grimes 	vm_pager_t pager;
1098df8bae1dSRodney W. Grimes {
1099df8bae1dSRodney W. Grimes 	struct vm_object_hash_head *bucket;
1100df8bae1dSRodney W. Grimes 	register vm_object_hash_entry_t entry;
1101df8bae1dSRodney W. Grimes 
1102df8bae1dSRodney W. Grimes 	/*
11030d94caffSDavid Greenman 	 * We don't cache null objects, and we can't cache objects with the
11040d94caffSDavid Greenman 	 * null pager.
1105df8bae1dSRodney W. Grimes 	 */
1106df8bae1dSRodney W. Grimes 
1107df8bae1dSRodney W. Grimes 	if (object == NULL)
1108df8bae1dSRodney W. Grimes 		return;
1109df8bae1dSRodney W. Grimes 	if (pager == NULL)
1110df8bae1dSRodney W. Grimes 		return;
1111df8bae1dSRodney W. Grimes 
1112df8bae1dSRodney W. Grimes 	bucket = &vm_object_hashtable[vm_object_hash(pager)];
1113df8bae1dSRodney W. Grimes 	entry = (vm_object_hash_entry_t)
1114df8bae1dSRodney W. Grimes 	    malloc((u_long) sizeof *entry, M_VMOBJHASH, M_WAITOK);
1115df8bae1dSRodney W. Grimes 	entry->object = object;
1116df8bae1dSRodney W. Grimes 	object->flags |= OBJ_CANPERSIST;
1117df8bae1dSRodney W. Grimes 
1118df8bae1dSRodney W. Grimes 	vm_object_cache_lock();
1119df8bae1dSRodney W. Grimes 	TAILQ_INSERT_TAIL(bucket, entry, hash_links);
1120df8bae1dSRodney W. Grimes 	vm_object_cache_unlock();
1121df8bae1dSRodney W. Grimes }
1122df8bae1dSRodney W. Grimes 
1123df8bae1dSRodney W. Grimes /*
1124df8bae1dSRodney W. Grimes  *	vm_object_remove:
1125df8bae1dSRodney W. Grimes  *
1126df8bae1dSRodney W. Grimes  *	Remove the pager from the hash table.
1127df8bae1dSRodney W. Grimes  *	Note:  This assumes that the object cache
1128df8bae1dSRodney W. Grimes  *	is locked.  XXX this should be fixed
1129df8bae1dSRodney W. Grimes  *	by reorganizing vm_object_deallocate.
1130df8bae1dSRodney W. Grimes  */
1131df8bae1dSRodney W. Grimes void
1132df8bae1dSRodney W. Grimes vm_object_remove(pager)
1133df8bae1dSRodney W. Grimes 	register vm_pager_t pager;
1134df8bae1dSRodney W. Grimes {
1135df8bae1dSRodney W. Grimes 	struct vm_object_hash_head *bucket;
1136df8bae1dSRodney W. Grimes 	register vm_object_hash_entry_t entry;
1137df8bae1dSRodney W. Grimes 	register vm_object_t object;
1138df8bae1dSRodney W. Grimes 
1139df8bae1dSRodney W. Grimes 	bucket = &vm_object_hashtable[vm_object_hash(pager)];
1140df8bae1dSRodney W. Grimes 
1141df8bae1dSRodney W. Grimes 	for (entry = bucket->tqh_first;
1142df8bae1dSRodney W. Grimes 	    entry != NULL;
1143df8bae1dSRodney W. Grimes 	    entry = entry->hash_links.tqe_next) {
1144df8bae1dSRodney W. Grimes 		object = entry->object;
1145df8bae1dSRodney W. Grimes 		if (object->pager == pager) {
1146df8bae1dSRodney W. Grimes 			TAILQ_REMOVE(bucket, entry, hash_links);
1147df8bae1dSRodney W. Grimes 			free((caddr_t) entry, M_VMOBJHASH);
1148df8bae1dSRodney W. Grimes 			break;
1149df8bae1dSRodney W. Grimes 		}
1150df8bae1dSRodney W. Grimes 	}
1151df8bae1dSRodney W. Grimes }
1152df8bae1dSRodney W. Grimes 
11532fe6e4d7SDavid Greenman /*
11542fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
11552fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
11562fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
11572fe6e4d7SDavid Greenman  */
11582fe6e4d7SDavid Greenman static void
11592fe6e4d7SDavid Greenman vm_object_qcollapse(object)
11602fe6e4d7SDavid Greenman 	register vm_object_t object;
11612fe6e4d7SDavid Greenman {
11622fe6e4d7SDavid Greenman 	register vm_object_t backing_object;
11632fe6e4d7SDavid Greenman 	register vm_offset_t backing_offset, new_offset;
11642fe6e4d7SDavid Greenman 	register vm_page_t p, pp;
11652fe6e4d7SDavid Greenman 	register vm_size_t size;
11662fe6e4d7SDavid Greenman 
11672fe6e4d7SDavid Greenman 	backing_object = object->shadow;
11682fe6e4d7SDavid Greenman 	if (!backing_object)
11692fe6e4d7SDavid Greenman 		return;
11702fe6e4d7SDavid Greenman 	if ((backing_object->flags & OBJ_INTERNAL) == 0)
11712fe6e4d7SDavid Greenman 		return;
11722fe6e4d7SDavid Greenman 	if (backing_object->shadow != NULL &&
11732fe6e4d7SDavid Greenman 	    backing_object->shadow->copy == backing_object)
11742fe6e4d7SDavid Greenman 		return;
11752fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
11762fe6e4d7SDavid Greenman 		return;
11772fe6e4d7SDavid Greenman 
1178010cf3b9SDavid Greenman 	backing_object->ref_count += 2;
1179010cf3b9SDavid Greenman 
11802fe6e4d7SDavid Greenman 	backing_offset = object->shadow_offset;
11812fe6e4d7SDavid Greenman 	size = object->size;
11822fe6e4d7SDavid Greenman 	p = backing_object->memq.tqh_first;
11832fe6e4d7SDavid Greenman 	while (p) {
11842fe6e4d7SDavid Greenman 		vm_page_t next;
11850d94caffSDavid Greenman 
11862fe6e4d7SDavid Greenman 		next = p->listq.tqe_next;
11870d94caffSDavid Greenman 		if ((p->flags & (PG_BUSY | PG_FICTITIOUS | PG_CACHE)) ||
11880d94caffSDavid Greenman 		    !p->valid || p->hold_count || p->wire_count || p->busy || p->bmapped) {
11892fe6e4d7SDavid Greenman 			p = next;
11902fe6e4d7SDavid Greenman 			continue;
11912fe6e4d7SDavid Greenman 		}
11920d94caffSDavid Greenman 		pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
11932fe6e4d7SDavid Greenman 		new_offset = (p->offset - backing_offset);
11942fe6e4d7SDavid Greenman 		if (p->offset < backing_offset ||
11952fe6e4d7SDavid Greenman 		    new_offset >= size) {
11962fe6e4d7SDavid Greenman 			if (backing_object->pager)
11970d94caffSDavid Greenman 				swap_pager_freespace(backing_object->pager,
11980d94caffSDavid Greenman 				    backing_object->paging_offset + p->offset, PAGE_SIZE);
11990d94caffSDavid Greenman 			vm_page_lock_queues();
12002fe6e4d7SDavid Greenman 			vm_page_free(p);
12012fe6e4d7SDavid Greenman 			vm_page_unlock_queues();
12022fe6e4d7SDavid Greenman 		} else {
12032fe6e4d7SDavid Greenman 			pp = vm_page_lookup(object, new_offset);
12042fe6e4d7SDavid Greenman 			if (pp != NULL || (object->pager && vm_pager_has_page(object->pager,
12052fe6e4d7SDavid Greenman 				    object->paging_offset + new_offset))) {
12062fe6e4d7SDavid Greenman 				if (backing_object->pager)
12070d94caffSDavid Greenman 					swap_pager_freespace(backing_object->pager,
12080d94caffSDavid Greenman 					    backing_object->paging_offset + p->offset, PAGE_SIZE);
12090d94caffSDavid Greenman 				vm_page_lock_queues();
12102fe6e4d7SDavid Greenman 				vm_page_free(p);
12112fe6e4d7SDavid Greenman 				vm_page_unlock_queues();
12122fe6e4d7SDavid Greenman 			} else {
1213a1f6d91cSDavid Greenman 				if( backing_object->pager)
1214a1f6d91cSDavid Greenman 					swap_pager_freespace(backing_object->pager,
1215a1f6d91cSDavid Greenman 					    backing_object->paging_offset + p->offset, PAGE_SIZE);
12162fe6e4d7SDavid Greenman 				vm_page_rename(p, object, new_offset);
12172fe6e4d7SDavid Greenman 			}
12182fe6e4d7SDavid Greenman 		}
12192fe6e4d7SDavid Greenman 		p = next;
12202fe6e4d7SDavid Greenman 	}
1221010cf3b9SDavid Greenman 	backing_object->ref_count -= 2;
12222fe6e4d7SDavid Greenman }
12232fe6e4d7SDavid Greenman 
1224df8bae1dSRodney W. Grimes boolean_t vm_object_collapse_allowed = TRUE;
12250d94caffSDavid Greenman 
1226df8bae1dSRodney W. Grimes /*
1227df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1228df8bae1dSRodney W. Grimes  *
1229df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1230df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1231df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1232df8bae1dSRodney W. Grimes  *
1233df8bae1dSRodney W. Grimes  *	Requires that the object be locked and the page
1234df8bae1dSRodney W. Grimes  *	queues be unlocked.
1235df8bae1dSRodney W. Grimes  *
123626f9a767SRodney W. Grimes  *	This routine has significant changes by John S. Dyson
123726f9a767SRodney W. Grimes  *	to fix some swap memory leaks.  18 Dec 93
123826f9a767SRodney W. Grimes  *
1239df8bae1dSRodney W. Grimes  */
124026f9a767SRodney W. Grimes void
124126f9a767SRodney W. Grimes vm_object_collapse(object)
1242df8bae1dSRodney W. Grimes 	register vm_object_t object;
1243df8bae1dSRodney W. Grimes 
1244df8bae1dSRodney W. Grimes {
1245df8bae1dSRodney W. Grimes 	register vm_object_t backing_object;
1246df8bae1dSRodney W. Grimes 	register vm_offset_t backing_offset;
1247df8bae1dSRodney W. Grimes 	register vm_size_t size;
1248df8bae1dSRodney W. Grimes 	register vm_offset_t new_offset;
1249df8bae1dSRodney W. Grimes 	register vm_page_t p, pp;
1250df8bae1dSRodney W. Grimes 
1251df8bae1dSRodney W. Grimes 	if (!vm_object_collapse_allowed)
1252df8bae1dSRodney W. Grimes 		return;
1253df8bae1dSRodney W. Grimes 
1254df8bae1dSRodney W. Grimes 	while (TRUE) {
1255df8bae1dSRodney W. Grimes 		/*
1256df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1257df8bae1dSRodney W. Grimes 		 *
12580d94caffSDavid Greenman 		 * The object exists and no pages in it are currently being paged
12590d94caffSDavid Greenman 		 * out.
1260df8bae1dSRodney W. Grimes 		 */
12612fe6e4d7SDavid Greenman 		if (object == NULL)
1262df8bae1dSRodney W. Grimes 			return;
1263df8bae1dSRodney W. Grimes 
1264b9921222SDavid Greenman 		/*
1265b9921222SDavid Greenman 		 * Make sure there is a backing object.
1266b9921222SDavid Greenman 		 */
1267df8bae1dSRodney W. Grimes 		if ((backing_object = object->shadow) == NULL)
1268df8bae1dSRodney W. Grimes 			return;
1269df8bae1dSRodney W. Grimes 
1270b9921222SDavid Greenman 		if (object->paging_in_progress != 0) {
1271b9921222SDavid Greenman 			if (backing_object) {
1272b9921222SDavid Greenman 				if (vm_object_lock_try(backing_object)) {
1273b9921222SDavid Greenman 					vm_object_qcollapse(object);
1274b9921222SDavid Greenman 					vm_object_unlock(backing_object);
1275b9921222SDavid Greenman 				}
1276b9921222SDavid Greenman 			}
1277b9921222SDavid Greenman 			return;
1278b9921222SDavid Greenman 		}
1279b9921222SDavid Greenman 
1280df8bae1dSRodney W. Grimes 		vm_object_lock(backing_object);
1281df8bae1dSRodney W. Grimes 		/*
12820d94caffSDavid Greenman 		 * ... The backing object is not read_only, and no pages in
12830d94caffSDavid Greenman 		 * the backing object are currently being paged out. The
12840d94caffSDavid Greenman 		 * backing object is internal.
1285df8bae1dSRodney W. Grimes 		 */
1286df8bae1dSRodney W. Grimes 
1287df8bae1dSRodney W. Grimes 		if ((backing_object->flags & OBJ_INTERNAL) == 0 ||
1288df8bae1dSRodney W. Grimes 		    backing_object->paging_in_progress != 0) {
12892fe6e4d7SDavid Greenman 			vm_object_qcollapse(object);
1290a1f6d91cSDavid Greenman 			vm_object_unlock(backing_object);
1291df8bae1dSRodney W. Grimes 			return;
1292df8bae1dSRodney W. Grimes 		}
1293df8bae1dSRodney W. Grimes 		/*
12940d94caffSDavid Greenman 		 * The backing object can't be a copy-object: the
12950d94caffSDavid Greenman 		 * shadow_offset for the copy-object must stay as 0.
12960d94caffSDavid Greenman 		 * Furthermore (for the 'we have all the pages' case), if we
12970d94caffSDavid Greenman 		 * bypass backing_object and just shadow the next object in
12980d94caffSDavid Greenman 		 * the chain, old pages from that object would then have to be
12990d94caffSDavid Greenman 		 * copied BOTH into the (former) backing_object and into the
1300df8bae1dSRodney W. Grimes 		 * parent object.
1301df8bae1dSRodney W. Grimes 		 */
1302df8bae1dSRodney W. Grimes 		if (backing_object->shadow != NULL &&
130326f9a767SRodney W. Grimes 		    backing_object->shadow->copy == backing_object) {
1304df8bae1dSRodney W. Grimes 			vm_object_unlock(backing_object);
1305df8bae1dSRodney W. Grimes 			return;
1306df8bae1dSRodney W. Grimes 		}
1307df8bae1dSRodney W. Grimes 		/*
130826f9a767SRodney W. Grimes 		 * we can deal only with the swap pager
130926f9a767SRodney W. Grimes 		 */
131026f9a767SRodney W. Grimes 		if ((object->pager &&
131126f9a767SRodney W. Grimes 			object->pager->pg_type != PG_SWAP) ||
131226f9a767SRodney W. Grimes 		    (backing_object->pager &&
131326f9a767SRodney W. Grimes 			backing_object->pager->pg_type != PG_SWAP)) {
131426f9a767SRodney W. Grimes 			vm_object_unlock(backing_object);
131526f9a767SRodney W. Grimes 			return;
131626f9a767SRodney W. Grimes 		}
131726f9a767SRodney W. Grimes 		/*
13180d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
13190d94caffSDavid Greenman 		 * the parent is the only reference to it) or (perhaps) remove
13200d94caffSDavid Greenman 		 * the parent's reference to it.
1321df8bae1dSRodney W. Grimes 		 */
1322df8bae1dSRodney W. Grimes 
1323df8bae1dSRodney W. Grimes 		backing_offset = object->shadow_offset;
1324df8bae1dSRodney W. Grimes 		size = object->size;
1325df8bae1dSRodney W. Grimes 
1326df8bae1dSRodney W. Grimes 		/*
13270d94caffSDavid Greenman 		 * If there is exactly one reference to the backing object, we
13280d94caffSDavid Greenman 		 * can collapse it into the parent.
1329df8bae1dSRodney W. Grimes 		 */
1330df8bae1dSRodney W. Grimes 
1331df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1332df8bae1dSRodney W. Grimes 
1333a1f6d91cSDavid Greenman 			backing_object->flags |= OBJ_DEAD;
1334df8bae1dSRodney W. Grimes 			/*
1335df8bae1dSRodney W. Grimes 			 * We can collapse the backing object.
1336df8bae1dSRodney W. Grimes 			 *
13370d94caffSDavid Greenman 			 * Move all in-memory pages from backing_object to the
13380d94caffSDavid Greenman 			 * parent.  Pages that have been paged out will be
13390d94caffSDavid Greenman 			 * overwritten by any of the parent's pages that
13400d94caffSDavid Greenman 			 * shadow them.
1341df8bae1dSRodney W. Grimes 			 */
1342df8bae1dSRodney W. Grimes 
134305f0fdd2SPoul-Henning Kamp 			while ((p = backing_object->memq.tqh_first) != 0) {
134426f9a767SRodney W. Grimes 
1345df8bae1dSRodney W. Grimes 				new_offset = (p->offset - backing_offset);
1346df8bae1dSRodney W. Grimes 
1347df8bae1dSRodney W. Grimes 				/*
13480d94caffSDavid Greenman 				 * If the parent has a page here, or if this
13490d94caffSDavid Greenman 				 * page falls outside the parent, dispose of
13500d94caffSDavid Greenman 				 * it.
1351df8bae1dSRodney W. Grimes 				 *
1352df8bae1dSRodney W. Grimes 				 * Otherwise, move it as planned.
1353df8bae1dSRodney W. Grimes 				 */
1354df8bae1dSRodney W. Grimes 
1355df8bae1dSRodney W. Grimes 				if (p->offset < backing_offset ||
1356df8bae1dSRodney W. Grimes 				    new_offset >= size) {
1357df8bae1dSRodney W. Grimes 					vm_page_lock_queues();
13582fe6e4d7SDavid Greenman 					pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
13590d94caffSDavid Greenman 					PAGE_WAKEUP(p);
1360df8bae1dSRodney W. Grimes 					vm_page_free(p);
1361df8bae1dSRodney W. Grimes 					vm_page_unlock_queues();
1362df8bae1dSRodney W. Grimes 				} else {
1363df8bae1dSRodney W. Grimes 					pp = vm_page_lookup(object, new_offset);
136426f9a767SRodney W. Grimes 					if (pp != NULL || (object->pager && vm_pager_has_page(object->pager,
136526f9a767SRodney W. Grimes 					    object->paging_offset + new_offset))) {
1366df8bae1dSRodney W. Grimes 						vm_page_lock_queues();
13672fe6e4d7SDavid Greenman 						pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
13680d94caffSDavid Greenman 						PAGE_WAKEUP(p);
1369df8bae1dSRodney W. Grimes 						vm_page_free(p);
1370df8bae1dSRodney W. Grimes 						vm_page_unlock_queues();
137126f9a767SRodney W. Grimes 					} else {
1372df8bae1dSRodney W. Grimes 						vm_page_rename(p, object, new_offset);
1373df8bae1dSRodney W. Grimes 					}
1374df8bae1dSRodney W. Grimes 				}
1375df8bae1dSRodney W. Grimes 			}
1376df8bae1dSRodney W. Grimes 
1377df8bae1dSRodney W. Grimes 			/*
1378df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1379df8bae1dSRodney W. Grimes 			 */
1380df8bae1dSRodney W. Grimes 
1381df8bae1dSRodney W. Grimes 			if (backing_object->pager) {
138226f9a767SRodney W. Grimes 				backing_object->paging_in_progress++;
138326f9a767SRodney W. Grimes 				if (object->pager) {
138426f9a767SRodney W. Grimes 					vm_pager_t bopager;
13850d94caffSDavid Greenman 
138626f9a767SRodney W. Grimes 					object->paging_in_progress++;
138726f9a767SRodney W. Grimes 					/*
138826f9a767SRodney W. Grimes 					 * copy shadow object pages into ours
13890d94caffSDavid Greenman 					 * and destroy unneeded pages in
13900d94caffSDavid Greenman 					 * shadow object.
139126f9a767SRodney W. Grimes 					 */
139226f9a767SRodney W. Grimes 					bopager = backing_object->pager;
1393df8bae1dSRodney W. Grimes 					backing_object->pager = NULL;
139426f9a767SRodney W. Grimes 					vm_object_remove(backing_object->pager);
139526f9a767SRodney W. Grimes 					swap_pager_copy(
139626f9a767SRodney W. Grimes 					    bopager, backing_object->paging_offset,
139726f9a767SRodney W. Grimes 					    object->pager, object->paging_offset,
139826f9a767SRodney W. Grimes 					    object->shadow_offset);
139926f9a767SRodney W. Grimes 					object->paging_in_progress--;
140026f9a767SRodney W. Grimes 					if (object->paging_in_progress == 0)
140126f9a767SRodney W. Grimes 						wakeup((caddr_t) object);
140226f9a767SRodney W. Grimes 				} else {
140326f9a767SRodney W. Grimes 					object->paging_in_progress++;
140426f9a767SRodney W. Grimes 					/*
140526f9a767SRodney W. Grimes 					 * grab the shadow objects pager
140626f9a767SRodney W. Grimes 					 */
140726f9a767SRodney W. Grimes 					object->pager = backing_object->pager;
140826f9a767SRodney W. Grimes 					object->paging_offset = backing_object->paging_offset + backing_offset;
140926f9a767SRodney W. Grimes 					vm_object_remove(backing_object->pager);
141026f9a767SRodney W. Grimes 					backing_object->pager = NULL;
141126f9a767SRodney W. Grimes 					/*
141226f9a767SRodney W. Grimes 					 * free unnecessary blocks
141326f9a767SRodney W. Grimes 					 */
141426f9a767SRodney W. Grimes 					swap_pager_freespace(object->pager, 0, object->paging_offset);
141526f9a767SRodney W. Grimes 					object->paging_in_progress--;
141626f9a767SRodney W. Grimes 					if (object->paging_in_progress == 0)
141726f9a767SRodney W. Grimes 						wakeup((caddr_t) object);
1418df8bae1dSRodney W. Grimes 				}
141926f9a767SRodney W. Grimes 				backing_object->paging_in_progress--;
142026f9a767SRodney W. Grimes 				if (backing_object->paging_in_progress == 0)
142126f9a767SRodney W. Grimes 					wakeup((caddr_t) backing_object);
142226f9a767SRodney W. Grimes 			}
1423df8bae1dSRodney W. Grimes 			/*
1424df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
1425df8bae1dSRodney W. Grimes 			 * Note that the reference to backing_object->shadow
1426df8bae1dSRodney W. Grimes 			 * moves from within backing_object to within object.
1427df8bae1dSRodney W. Grimes 			 */
1428df8bae1dSRodney W. Grimes 
14290d94caffSDavid Greenman 			TAILQ_REMOVE(&object->shadow->reverse_shadow_head, object,
14300d94caffSDavid Greenman 			    reverse_shadow_list);
14312fe6e4d7SDavid Greenman 			if (backing_object->shadow)
14320d94caffSDavid Greenman 				TAILQ_REMOVE(&backing_object->shadow->reverse_shadow_head,
14330d94caffSDavid Greenman 				    backing_object, reverse_shadow_list);
1434df8bae1dSRodney W. Grimes 			object->shadow = backing_object->shadow;
14352fe6e4d7SDavid Greenman 			if (object->shadow)
14360d94caffSDavid Greenman 				TAILQ_INSERT_TAIL(&object->shadow->reverse_shadow_head,
14370d94caffSDavid Greenman 				    object, reverse_shadow_list);
14382fe6e4d7SDavid Greenman 
1439df8bae1dSRodney W. Grimes 			object->shadow_offset += backing_object->shadow_offset;
1440df8bae1dSRodney W. Grimes 			if (object->shadow != NULL &&
1441df8bae1dSRodney W. Grimes 			    object->shadow->copy != NULL) {
1442df8bae1dSRodney W. Grimes 				panic("vm_object_collapse: we collapsed a copy-object!");
1443df8bae1dSRodney W. Grimes 			}
1444df8bae1dSRodney W. Grimes 			/*
1445df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1446df8bae1dSRodney W. Grimes 			 *
14470d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
14480d94caffSDavid Greenman 			 * and no object references within it, all that is
14490d94caffSDavid Greenman 			 * necessary is to dispose of it.
1450df8bae1dSRodney W. Grimes 			 */
1451df8bae1dSRodney W. Grimes 
1452df8bae1dSRodney W. Grimes 			vm_object_unlock(backing_object);
1453df8bae1dSRodney W. Grimes 
1454df8bae1dSRodney W. Grimes 			simple_lock(&vm_object_list_lock);
1455df8bae1dSRodney W. Grimes 			TAILQ_REMOVE(&vm_object_list, backing_object,
1456df8bae1dSRodney W. Grimes 			    object_list);
1457df8bae1dSRodney W. Grimes 			vm_object_count--;
1458df8bae1dSRodney W. Grimes 			simple_unlock(&vm_object_list_lock);
1459df8bae1dSRodney W. Grimes 
1460df8bae1dSRodney W. Grimes 			free((caddr_t) backing_object, M_VMOBJ);
1461df8bae1dSRodney W. Grimes 
1462df8bae1dSRodney W. Grimes 			object_collapses++;
14630d94caffSDavid Greenman 		} else {
1464df8bae1dSRodney W. Grimes 			/*
1465df8bae1dSRodney W. Grimes 			 * If all of the pages in the backing object are
14660d94caffSDavid Greenman 			 * shadowed by the parent object, the parent object no
14670d94caffSDavid Greenman 			 * longer has to shadow the backing object; it can
14680d94caffSDavid Greenman 			 * shadow the next one in the chain.
1469df8bae1dSRodney W. Grimes 			 *
14700d94caffSDavid Greenman 			 * The backing object must not be paged out - we'd have
14710d94caffSDavid Greenman 			 * to check all of the paged-out pages, as well.
1472df8bae1dSRodney W. Grimes 			 */
1473df8bae1dSRodney W. Grimes 
1474df8bae1dSRodney W. Grimes 			if (backing_object->pager != NULL) {
1475df8bae1dSRodney W. Grimes 				vm_object_unlock(backing_object);
1476df8bae1dSRodney W. Grimes 				return;
1477df8bae1dSRodney W. Grimes 			}
1478df8bae1dSRodney W. Grimes 			/*
14790d94caffSDavid Greenman 			 * Should have a check for a 'small' number of pages
14800d94caffSDavid Greenman 			 * here.
1481df8bae1dSRodney W. Grimes 			 */
1482df8bae1dSRodney W. Grimes 
148326f9a767SRodney W. Grimes 			for (p = backing_object->memq.tqh_first; p; p = p->listq.tqe_next) {
1484df8bae1dSRodney W. Grimes 				new_offset = (p->offset - backing_offset);
1485df8bae1dSRodney W. Grimes 
1486df8bae1dSRodney W. Grimes 				/*
14870d94caffSDavid Greenman 				 * If the parent has a page here, or if this
14880d94caffSDavid Greenman 				 * page falls outside the parent, keep going.
1489df8bae1dSRodney W. Grimes 				 *
14900d94caffSDavid Greenman 				 * Otherwise, the backing_object must be left in
14910d94caffSDavid Greenman 				 * the chain.
1492df8bae1dSRodney W. Grimes 				 */
1493df8bae1dSRodney W. Grimes 
1494df8bae1dSRodney W. Grimes 				if (p->offset >= backing_offset &&
149526f9a767SRodney W. Grimes 				    new_offset <= size &&
14960d94caffSDavid Greenman 				    ((pp = vm_page_lookup(object, new_offset)) == NULL ||
14970d94caffSDavid Greenman 					!pp->valid) &&
149826f9a767SRodney W. Grimes 				    (!object->pager || !vm_pager_has_page(object->pager, object->paging_offset + new_offset))) {
1499df8bae1dSRodney W. Grimes 					/*
15000d94caffSDavid Greenman 					 * Page still needed. Can't go any
15010d94caffSDavid Greenman 					 * further.
1502df8bae1dSRodney W. Grimes 					 */
1503df8bae1dSRodney W. Grimes 					vm_object_unlock(backing_object);
1504df8bae1dSRodney W. Grimes 					return;
1505df8bae1dSRodney W. Grimes 				}
1506df8bae1dSRodney W. Grimes 			}
1507df8bae1dSRodney W. Grimes 
1508df8bae1dSRodney W. Grimes 			/*
15090d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
15100d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
15110d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1512df8bae1dSRodney W. Grimes 			 */
1513df8bae1dSRodney W. Grimes 
15140d94caffSDavid Greenman 			TAILQ_REMOVE(&object->shadow->reverse_shadow_head,
15150d94caffSDavid Greenman 			    object, reverse_shadow_list);
151626f9a767SRodney W. Grimes 			vm_object_reference(object->shadow = backing_object->shadow);
15172fe6e4d7SDavid Greenman 			if (object->shadow)
15180d94caffSDavid Greenman 				TAILQ_INSERT_TAIL(&object->shadow->reverse_shadow_head,
15190d94caffSDavid Greenman 				    object, reverse_shadow_list);
1520df8bae1dSRodney W. Grimes 			object->shadow_offset += backing_object->shadow_offset;
1521df8bae1dSRodney W. Grimes 
1522df8bae1dSRodney W. Grimes 			/*
15230d94caffSDavid Greenman 			 * Backing object might have had a copy pointer to us.
15240d94caffSDavid Greenman 			 * If it did, clear it.
1525df8bae1dSRodney W. Grimes 			 */
1526df8bae1dSRodney W. Grimes 			if (backing_object->copy == object) {
1527df8bae1dSRodney W. Grimes 				backing_object->copy = NULL;
1528df8bae1dSRodney W. Grimes 			}
15290d94caffSDavid Greenman 			/*
15300d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
15310d94caffSDavid Greenman 			 * its ref_count was at least 2, it will not vanish;
15320d94caffSDavid Greenman 			 * so we don't need to call vm_object_deallocate.
1533df8bae1dSRodney W. Grimes 			 */
153426f9a767SRodney W. Grimes 			if (backing_object->ref_count == 1)
153526f9a767SRodney W. Grimes 				printf("should have called obj deallocate\n");
1536df8bae1dSRodney W. Grimes 			backing_object->ref_count--;
1537df8bae1dSRodney W. Grimes 			vm_object_unlock(backing_object);
1538df8bae1dSRodney W. Grimes 
1539df8bae1dSRodney W. Grimes 			object_bypasses++;
1540df8bae1dSRodney W. Grimes 
1541df8bae1dSRodney W. Grimes 		}
1542df8bae1dSRodney W. Grimes 
1543df8bae1dSRodney W. Grimes 		/*
1544df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1545df8bae1dSRodney W. Grimes 		 */
1546df8bae1dSRodney W. Grimes 	}
1547df8bae1dSRodney W. Grimes }
1548df8bae1dSRodney W. Grimes 
1549df8bae1dSRodney W. Grimes /*
1550df8bae1dSRodney W. Grimes  *	vm_object_page_remove: [internal]
1551df8bae1dSRodney W. Grimes  *
1552df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
1553df8bae1dSRodney W. Grimes  *	object range from the object's list of pages.
1554df8bae1dSRodney W. Grimes  *
1555df8bae1dSRodney W. Grimes  *	The object must be locked.
1556df8bae1dSRodney W. Grimes  */
155726f9a767SRodney W. Grimes void
155826f9a767SRodney W. Grimes vm_object_page_remove(object, start, end)
1559df8bae1dSRodney W. Grimes 	register vm_object_t object;
1560df8bae1dSRodney W. Grimes 	register vm_offset_t start;
1561df8bae1dSRodney W. Grimes 	register vm_offset_t end;
1562df8bae1dSRodney W. Grimes {
1563df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
156426f9a767SRodney W. Grimes 	vm_offset_t size;
156526f9a767SRodney W. Grimes 	int s;
1566df8bae1dSRodney W. Grimes 
1567df8bae1dSRodney W. Grimes 	if (object == NULL)
1568df8bae1dSRodney W. Grimes 		return;
1569df8bae1dSRodney W. Grimes 
15702fe6e4d7SDavid Greenman 	object->paging_in_progress++;
157126f9a767SRodney W. Grimes 	start = trunc_page(start);
157226f9a767SRodney W. Grimes 	end = round_page(end);
157326f9a767SRodney W. Grimes again:
157426f9a767SRodney W. Grimes 	size = end - start;
157526f9a767SRodney W. Grimes 	if (size > 4 * PAGE_SIZE || size >= object->size / 4) {
15760d94caffSDavid Greenman 		for (p = object->memq.tqh_first; p != NULL; p = next) {
1577df8bae1dSRodney W. Grimes 			next = p->listq.tqe_next;
1578df8bae1dSRodney W. Grimes 			if ((start <= p->offset) && (p->offset < end)) {
1579a481f200SDavid Greenman 				s = splhigh();
15800d94caffSDavid Greenman 				if (p->bmapped) {
15810d94caffSDavid Greenman 					splx(s);
15820d94caffSDavid Greenman 					continue;
15830d94caffSDavid Greenman 				}
15840d94caffSDavid Greenman 				if ((p->flags & PG_BUSY) || p->busy) {
158526f9a767SRodney W. Grimes 					p->flags |= PG_WANTED;
158626f9a767SRodney W. Grimes 					tsleep((caddr_t) p, PVM, "vmopar", 0);
1587a481f200SDavid Greenman 					splx(s);
158826f9a767SRodney W. Grimes 					goto again;
158926f9a767SRodney W. Grimes 				}
1590a481f200SDavid Greenman 				splx(s);
1591df8bae1dSRodney W. Grimes 				pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
1592df8bae1dSRodney W. Grimes 				vm_page_lock_queues();
15930d94caffSDavid Greenman 				PAGE_WAKEUP(p);
1594df8bae1dSRodney W. Grimes 				vm_page_free(p);
1595df8bae1dSRodney W. Grimes 				vm_page_unlock_queues();
159626f9a767SRodney W. Grimes 			}
159726f9a767SRodney W. Grimes 		}
159826f9a767SRodney W. Grimes 	} else {
159926f9a767SRodney W. Grimes 		while (size > 0) {
160005f0fdd2SPoul-Henning Kamp 			while ((p = vm_page_lookup(object, start)) != 0) {
1601a481f200SDavid Greenman 				s = splhigh();
16020d94caffSDavid Greenman 				if (p->bmapped) {
16030d94caffSDavid Greenman 					splx(s);
16040d94caffSDavid Greenman 					break;
16050d94caffSDavid Greenman 				}
16060d94caffSDavid Greenman 				if ((p->flags & PG_BUSY) || p->busy) {
160726f9a767SRodney W. Grimes 					p->flags |= PG_WANTED;
160826f9a767SRodney W. Grimes 					tsleep((caddr_t) p, PVM, "vmopar", 0);
1609a481f200SDavid Greenman 					splx(s);
161026f9a767SRodney W. Grimes 					goto again;
161126f9a767SRodney W. Grimes 				}
1612a481f200SDavid Greenman 				splx(s);
161326f9a767SRodney W. Grimes 				pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
161426f9a767SRodney W. Grimes 				vm_page_lock_queues();
16150d94caffSDavid Greenman 				PAGE_WAKEUP(p);
161626f9a767SRodney W. Grimes 				vm_page_free(p);
161726f9a767SRodney W. Grimes 				vm_page_unlock_queues();
161826f9a767SRodney W. Grimes 			}
161926f9a767SRodney W. Grimes 			start += PAGE_SIZE;
162026f9a767SRodney W. Grimes 			size -= PAGE_SIZE;
1621df8bae1dSRodney W. Grimes 		}
1622df8bae1dSRodney W. Grimes 	}
16232fe6e4d7SDavid Greenman 	--object->paging_in_progress;
16242fe6e4d7SDavid Greenman 	if (object->paging_in_progress == 0)
16252fe6e4d7SDavid Greenman 		wakeup((caddr_t) object);
1626df8bae1dSRodney W. Grimes }
1627df8bae1dSRodney W. Grimes 
1628df8bae1dSRodney W. Grimes /*
1629df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1630df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1631df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1632df8bae1dSRodney W. Grimes  *
1633df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1634df8bae1dSRodney W. Grimes  *
1635df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1636df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1637df8bae1dSRodney W. Grimes  *
1638df8bae1dSRodney W. Grimes  *	Parameters:
1639df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1640df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1641df8bae1dSRodney W. Grimes  *		next_object	Second object into coalesce
1642df8bae1dSRodney W. Grimes  *		next_offset	Offset into next_object
1643df8bae1dSRodney W. Grimes  *
1644df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
1645df8bae1dSRodney W. Grimes  *		next_size	Size of reference to next_object
1646df8bae1dSRodney W. Grimes  *
1647df8bae1dSRodney W. Grimes  *	Conditions:
1648df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1649df8bae1dSRodney W. Grimes  */
16500d94caffSDavid Greenman boolean_t
16510d94caffSDavid Greenman vm_object_coalesce(prev_object, next_object,
1652df8bae1dSRodney W. Grimes     prev_offset, next_offset,
1653df8bae1dSRodney W. Grimes     prev_size, next_size)
1654df8bae1dSRodney W. Grimes 	register vm_object_t prev_object;
1655df8bae1dSRodney W. Grimes 	vm_object_t next_object;
1656df8bae1dSRodney W. Grimes 	vm_offset_t prev_offset, next_offset;
1657df8bae1dSRodney W. Grimes 	vm_size_t prev_size, next_size;
1658df8bae1dSRodney W. Grimes {
1659df8bae1dSRodney W. Grimes 	vm_size_t newsize;
1660df8bae1dSRodney W. Grimes 
1661df8bae1dSRodney W. Grimes 	if (next_object != NULL) {
1662df8bae1dSRodney W. Grimes 		return (FALSE);
1663df8bae1dSRodney W. Grimes 	}
1664df8bae1dSRodney W. Grimes 	if (prev_object == NULL) {
1665df8bae1dSRodney W. Grimes 		return (TRUE);
1666df8bae1dSRodney W. Grimes 	}
1667df8bae1dSRodney W. Grimes 	vm_object_lock(prev_object);
1668df8bae1dSRodney W. Grimes 
1669df8bae1dSRodney W. Grimes 	/*
1670df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1671df8bae1dSRodney W. Grimes 	 */
1672df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1673df8bae1dSRodney W. Grimes 
1674df8bae1dSRodney W. Grimes 	/*
16750d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
16760d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
16770d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1678df8bae1dSRodney W. Grimes 	 */
1679df8bae1dSRodney W. Grimes 
1680df8bae1dSRodney W. Grimes 	if (prev_object->ref_count > 1 ||
1681df8bae1dSRodney W. Grimes 	    prev_object->pager != NULL ||
1682df8bae1dSRodney W. Grimes 	    prev_object->shadow != NULL ||
1683df8bae1dSRodney W. Grimes 	    prev_object->copy != NULL) {
1684df8bae1dSRodney W. Grimes 		vm_object_unlock(prev_object);
1685df8bae1dSRodney W. Grimes 		return (FALSE);
1686df8bae1dSRodney W. Grimes 	}
1687df8bae1dSRodney W. Grimes 	/*
16880d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
16890d94caffSDavid Greenman 	 * deallocation.
1690df8bae1dSRodney W. Grimes 	 */
1691df8bae1dSRodney W. Grimes 
1692df8bae1dSRodney W. Grimes 	vm_object_page_remove(prev_object,
1693df8bae1dSRodney W. Grimes 	    prev_offset + prev_size,
1694df8bae1dSRodney W. Grimes 	    prev_offset + prev_size + next_size);
1695df8bae1dSRodney W. Grimes 
1696df8bae1dSRodney W. Grimes 	/*
1697df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1698df8bae1dSRodney W. Grimes 	 */
1699df8bae1dSRodney W. Grimes 	newsize = prev_offset + prev_size + next_size;
1700df8bae1dSRodney W. Grimes 	if (newsize > prev_object->size)
1701df8bae1dSRodney W. Grimes 		prev_object->size = newsize;
1702df8bae1dSRodney W. Grimes 
1703df8bae1dSRodney W. Grimes 	vm_object_unlock(prev_object);
1704df8bae1dSRodney W. Grimes 	return (TRUE);
1705df8bae1dSRodney W. Grimes }
1706df8bae1dSRodney W. Grimes 
1707df8bae1dSRodney W. Grimes /*
170826f9a767SRodney W. Grimes  * returns page after looking up in shadow chain
170926f9a767SRodney W. Grimes  */
171026f9a767SRodney W. Grimes 
171126f9a767SRodney W. Grimes vm_page_t
171226f9a767SRodney W. Grimes vm_object_page_lookup(object, offset)
171326f9a767SRodney W. Grimes 	vm_object_t object;
171426f9a767SRodney W. Grimes 	vm_offset_t offset;
171526f9a767SRodney W. Grimes {
171626f9a767SRodney W. Grimes 	vm_page_t m;
17170d94caffSDavid Greenman 
171826f9a767SRodney W. Grimes 	if (!(m = vm_page_lookup(object, offset))) {
171926f9a767SRodney W. Grimes 		if (!object->shadow)
172026f9a767SRodney W. Grimes 			return 0;
172126f9a767SRodney W. Grimes 		else
172226f9a767SRodney W. Grimes 			return vm_object_page_lookup(object->shadow, offset + object->shadow_offset);
172326f9a767SRodney W. Grimes 	}
172426f9a767SRodney W. Grimes 	return m;
172526f9a767SRodney W. Grimes }
172626f9a767SRodney W. Grimes 
1727a1f6d91cSDavid Greenman int
1728a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry)
1729a1f6d91cSDavid Greenman 	vm_map_t map;
1730a1f6d91cSDavid Greenman 	vm_object_t object;
1731a1f6d91cSDavid Greenman 	vm_map_entry_t entry;
1732a1f6d91cSDavid Greenman {
1733a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1734a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1735a1f6d91cSDavid Greenman 	vm_object_t obj;
1736a1f6d91cSDavid Greenman 	int entcount;
1737a1f6d91cSDavid Greenman 
1738a1f6d91cSDavid Greenman 	if (map == 0)
1739a1f6d91cSDavid Greenman 		return 0;
1740a1f6d91cSDavid Greenman 
1741a1f6d91cSDavid Greenman 	if (entry == 0) {
1742a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1743a1f6d91cSDavid Greenman 		entcount = map->nentries;
1744a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1745a1f6d91cSDavid Greenman 			if( _vm_object_in_map(map, object, tmpe)) {
1746a1f6d91cSDavid Greenman 				return 1;
1747a1f6d91cSDavid Greenman 			}
1748a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1749a1f6d91cSDavid Greenman 		}
1750a1f6d91cSDavid Greenman 	} else if (entry->is_sub_map || entry->is_a_map) {
1751a1f6d91cSDavid Greenman 		tmpm = entry->object.share_map;
1752a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1753a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1754a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1755a1f6d91cSDavid Greenman 			if( _vm_object_in_map(tmpm, object, tmpe)) {
1756a1f6d91cSDavid Greenman 				return 1;
1757a1f6d91cSDavid Greenman 			}
1758a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1759a1f6d91cSDavid Greenman 		}
1760a1f6d91cSDavid Greenman 	} else if (obj = entry->object.vm_object) {
1761a1f6d91cSDavid Greenman 		for(; obj; obj=obj->shadow)
1762a1f6d91cSDavid Greenman 			if( obj == object) {
1763a1f6d91cSDavid Greenman 				return 1;
1764a1f6d91cSDavid Greenman 			}
1765a1f6d91cSDavid Greenman 	}
1766a1f6d91cSDavid Greenman 	return 0;
1767a1f6d91cSDavid Greenman }
1768a1f6d91cSDavid Greenman 
1769a1f6d91cSDavid Greenman int
1770a1f6d91cSDavid Greenman vm_object_in_map( object)
1771a1f6d91cSDavid Greenman 	vm_object_t object;
1772a1f6d91cSDavid Greenman {
1773a1f6d91cSDavid Greenman 	struct proc *p;
1774a1f6d91cSDavid Greenman 	for (p = (struct proc *) allproc; p != NULL; p = p->p_next) {
1775a1f6d91cSDavid Greenman 		if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1776a1f6d91cSDavid Greenman 			continue;
1777a1f6d91cSDavid Greenman /*
1778a1f6d91cSDavid Greenman 		if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
1779a1f6d91cSDavid Greenman 			continue;
1780a1f6d91cSDavid Greenman 		}
1781a1f6d91cSDavid Greenman */
1782a1f6d91cSDavid Greenman 		if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1783a1f6d91cSDavid Greenman 			return 1;
1784a1f6d91cSDavid Greenman 	}
1785a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kernel_map, object, 0))
1786a1f6d91cSDavid Greenman 		return 1;
1787a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kmem_map, object, 0))
1788a1f6d91cSDavid Greenman 		return 1;
1789a1f6d91cSDavid Greenman 	if( _vm_object_in_map( pager_map, object, 0))
1790a1f6d91cSDavid Greenman 		return 1;
1791a1f6d91cSDavid Greenman 	if( _vm_object_in_map( buffer_map, object, 0))
1792a1f6d91cSDavid Greenman 		return 1;
1793a1f6d91cSDavid Greenman 	if( _vm_object_in_map( io_map, object, 0))
1794a1f6d91cSDavid Greenman 		return 1;
1795a1f6d91cSDavid Greenman 	if( _vm_object_in_map( phys_map, object, 0))
1796a1f6d91cSDavid Greenman 		return 1;
1797a1f6d91cSDavid Greenman 	if( _vm_object_in_map( mb_map, object, 0))
1798a1f6d91cSDavid Greenman 		return 1;
1799a1f6d91cSDavid Greenman 	if( _vm_object_in_map( u_map, object, 0))
1800a1f6d91cSDavid Greenman 		return 1;
1801a1f6d91cSDavid Greenman 	return 0;
1802a1f6d91cSDavid Greenman }
1803a1f6d91cSDavid Greenman 
1804a1f6d91cSDavid Greenman void
1805a1f6d91cSDavid Greenman vm_object_check() {
1806a1f6d91cSDavid Greenman 	int i;
1807a1f6d91cSDavid Greenman 	int maxhash = 0;
1808a1f6d91cSDavid Greenman 	vm_object_t object;
1809a1f6d91cSDavid Greenman 	vm_object_hash_entry_t entry;
1810a1f6d91cSDavid Greenman 
1811a1f6d91cSDavid Greenman 	/*
1812a1f6d91cSDavid Greenman 	 * make sure that no internal objs are hashed
1813a1f6d91cSDavid Greenman 	 */
1814a1f6d91cSDavid Greenman 	for (i=0; i<VM_OBJECT_HASH_COUNT;i++) {
1815a1f6d91cSDavid Greenman 		int lsize = 0;
1816a1f6d91cSDavid Greenman 		for (entry = vm_object_hashtable[i].tqh_first;
1817a1f6d91cSDavid Greenman 		    entry != NULL;
1818a1f6d91cSDavid Greenman 		    entry = entry->hash_links.tqe_next) {
1819a1f6d91cSDavid Greenman 			if( entry->object->flags & OBJ_INTERNAL) {
1820a1f6d91cSDavid Greenman 				printf("vmochk: internal obj on hash: size: %d\n", entry->object->size);
1821a1f6d91cSDavid Greenman 			}
1822a1f6d91cSDavid Greenman 			++lsize;
1823a1f6d91cSDavid Greenman 		}
1824a1f6d91cSDavid Greenman 		if( lsize > maxhash)
1825a1f6d91cSDavid Greenman 			maxhash = lsize;
1826a1f6d91cSDavid Greenman 	}
1827a1f6d91cSDavid Greenman 
1828a1f6d91cSDavid Greenman 	printf("maximum object hash queue size: %d\n",  maxhash);
1829a1f6d91cSDavid Greenman 
1830a1f6d91cSDavid Greenman 	/*
1831a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
1832a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
1833a1f6d91cSDavid Greenman 	 */
1834a1f6d91cSDavid Greenman 	for (object = vm_object_list.tqh_first;
1835a1f6d91cSDavid Greenman 			object != NULL;
1836a1f6d91cSDavid Greenman 			object = object->object_list.tqe_next) {
1837a1f6d91cSDavid Greenman 		if( object->flags & OBJ_INTERNAL) {
1838a1f6d91cSDavid Greenman 			if( object->ref_count == 0) {
1839a1f6d91cSDavid Greenman 				printf("vmochk: internal obj has zero ref count: %d\n",
1840a1f6d91cSDavid Greenman 					object->size);
1841a1f6d91cSDavid Greenman 			}
1842a1f6d91cSDavid Greenman 			if( !vm_object_in_map(object)) {
1843a1f6d91cSDavid 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);
1844a1f6d91cSDavid Greenman 			}
1845a1f6d91cSDavid Greenman 		}
1846a1f6d91cSDavid Greenman 	}
1847a1f6d91cSDavid Greenman }
1848a1f6d91cSDavid Greenman 
184926f9a767SRodney W. Grimes #define DEBUG
18508a129caeSDavid Greenman #if defined(DEBUG) || defined(DDB)
185126f9a767SRodney W. Grimes /*
1852df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
1853df8bae1dSRodney W. Grimes  */
18540d94caffSDavid Greenman void
18550d94caffSDavid Greenman vm_object_print(object, full)
1856df8bae1dSRodney W. Grimes 	vm_object_t object;
1857df8bae1dSRodney W. Grimes 	boolean_t full;
1858df8bae1dSRodney W. Grimes {
1859df8bae1dSRodney W. Grimes 	register vm_page_t p;
1860df8bae1dSRodney W. Grimes 	extern indent;
1861df8bae1dSRodney W. Grimes 
1862df8bae1dSRodney W. Grimes 	register int count;
1863df8bae1dSRodney W. Grimes 
1864df8bae1dSRodney W. Grimes 	if (object == NULL)
1865df8bae1dSRodney W. Grimes 		return;
1866df8bae1dSRodney W. Grimes 
1867df8bae1dSRodney W. Grimes 	iprintf("Object 0x%x: size=0x%x, res=%d, ref=%d, ",
1868df8bae1dSRodney W. Grimes 	    (int) object, (int) object->size,
1869df8bae1dSRodney W. Grimes 	    object->resident_page_count, object->ref_count);
1870df8bae1dSRodney W. Grimes 	printf("pager=0x%x+0x%x, shadow=(0x%x)+0x%x\n",
1871df8bae1dSRodney W. Grimes 	    (int) object->pager, (int) object->paging_offset,
1872df8bae1dSRodney W. Grimes 	    (int) object->shadow, (int) object->shadow_offset);
187305f0fdd2SPoul-Henning Kamp 	printf("cache: next=%p, prev=%p\n",
1874df8bae1dSRodney W. Grimes 	    object->cached_list.tqe_next, object->cached_list.tqe_prev);
1875df8bae1dSRodney W. Grimes 
1876df8bae1dSRodney W. Grimes 	if (!full)
1877df8bae1dSRodney W. Grimes 		return;
1878df8bae1dSRodney W. Grimes 
1879df8bae1dSRodney W. Grimes 	indent += 2;
1880df8bae1dSRodney W. Grimes 	count = 0;
1881df8bae1dSRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) {
1882df8bae1dSRodney W. Grimes 		if (count == 0)
1883df8bae1dSRodney W. Grimes 			iprintf("memory:=");
1884df8bae1dSRodney W. Grimes 		else if (count == 6) {
1885df8bae1dSRodney W. Grimes 			printf("\n");
1886df8bae1dSRodney W. Grimes 			iprintf(" ...");
1887df8bae1dSRodney W. Grimes 			count = 0;
1888df8bae1dSRodney W. Grimes 		} else
1889df8bae1dSRodney W. Grimes 			printf(",");
1890df8bae1dSRodney W. Grimes 		count++;
1891df8bae1dSRodney W. Grimes 
189205f0fdd2SPoul-Henning Kamp 		printf("(off=0x%lx,page=0x%lx)",
189305f0fdd2SPoul-Henning Kamp 		    (u_long) p->offset, (u_long) VM_PAGE_TO_PHYS(p));
1894df8bae1dSRodney W. Grimes 	}
1895df8bae1dSRodney W. Grimes 	if (count != 0)
1896df8bae1dSRodney W. Grimes 		printf("\n");
1897df8bae1dSRodney W. Grimes 	indent -= 2;
1898df8bae1dSRodney W. Grimes }
18998a129caeSDavid Greenman #endif				/* defined(DEBUG) || defined(DDB) */
1900