xref: /freebsd/sys/vm/vm_object.c (revision 0d94caffcad13f94e28da90c9e945749e0659463)
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  *
640d94caffSDavid Greenman  * $Id: vm_object.c,v 1.14 1995/01/05 04:30:40 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>
8526f9a767SRodney W. Grimes 
8626f9a767SRodney W. Grimes static void _vm_object_allocate(vm_size_t, vm_object_t);
872fe6e4d7SDavid Greenman static void vm_object_rcollapse(vm_object_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 {
13326f9a767SRodney W. Grimes 	bzero(object, sizeof *object);
134df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
1352fe6e4d7SDavid Greenman 	TAILQ_INIT(&object->reverse_shadow_head);
136df8bae1dSRodney W. Grimes 	vm_object_lock_init(object);
137df8bae1dSRodney W. Grimes 	object->ref_count = 1;
138df8bae1dSRodney W. Grimes 	object->resident_page_count = 0;
139df8bae1dSRodney W. Grimes 	object->size = size;
140df8bae1dSRodney W. Grimes 	object->flags = OBJ_INTERNAL;	/* vm_allocate_with_pager will reset */
141df8bae1dSRodney W. Grimes 	object->paging_in_progress = 0;
142df8bae1dSRodney W. Grimes 	object->copy = NULL;
143df8bae1dSRodney W. Grimes 
144df8bae1dSRodney W. Grimes 	/*
145df8bae1dSRodney W. Grimes 	 * Object starts out read-write, with no pager.
146df8bae1dSRodney W. Grimes 	 */
147df8bae1dSRodney W. Grimes 
148df8bae1dSRodney W. Grimes 	object->pager = NULL;
149df8bae1dSRodney W. Grimes 	object->paging_offset = 0;
150df8bae1dSRodney W. Grimes 	object->shadow = NULL;
151df8bae1dSRodney W. Grimes 	object->shadow_offset = (vm_offset_t) 0;
152df8bae1dSRodney W. Grimes 
153df8bae1dSRodney W. Grimes 	simple_lock(&vm_object_list_lock);
154df8bae1dSRodney W. Grimes 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
155df8bae1dSRodney W. Grimes 	vm_object_count++;
156df8bae1dSRodney W. Grimes 	cnt.v_nzfod += atop(size);
157df8bae1dSRodney W. Grimes 	simple_unlock(&vm_object_list_lock);
158df8bae1dSRodney W. Grimes }
159df8bae1dSRodney W. Grimes 
160df8bae1dSRodney W. Grimes /*
16126f9a767SRodney W. Grimes  *	vm_object_init:
16226f9a767SRodney W. Grimes  *
16326f9a767SRodney W. Grimes  *	Initialize the VM objects module.
16426f9a767SRodney W. Grimes  */
16526f9a767SRodney W. Grimes void
16626f9a767SRodney W. Grimes vm_object_init(vm_offset_t nothing)
16726f9a767SRodney W. Grimes {
16826f9a767SRodney W. Grimes 	register int i;
16926f9a767SRodney W. Grimes 
17026f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_cached_list);
17126f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
17226f9a767SRodney W. Grimes 	vm_object_count = 0;
17326f9a767SRodney W. Grimes 	simple_lock_init(&vm_cache_lock);
17426f9a767SRodney W. Grimes 	simple_lock_init(&vm_object_list_lock);
1750d94caffSDavid Greenman 	vm_object_cache_max = (cnt.v_page_count - 500) / 8;
17626f9a767SRodney W. Grimes 
17726f9a767SRodney W. Grimes 	for (i = 0; i < VM_OBJECT_HASH_COUNT; i++)
17826f9a767SRodney W. Grimes 		TAILQ_INIT(&vm_object_hashtable[i]);
17926f9a767SRodney W. Grimes 
18026f9a767SRodney W. Grimes 	kernel_object = &kernel_object_store;
18126f9a767SRodney W. Grimes 	_vm_object_allocate(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS,
18226f9a767SRodney W. Grimes 	    kernel_object);
18326f9a767SRodney W. Grimes 
18426f9a767SRodney W. Grimes 	kmem_object = &kmem_object_store;
18526f9a767SRodney W. Grimes 	_vm_object_allocate(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS,
18626f9a767SRodney W. Grimes 	    kmem_object);
18726f9a767SRodney W. Grimes }
18826f9a767SRodney W. Grimes 
18926f9a767SRodney W. Grimes /*
19026f9a767SRodney W. Grimes  *	vm_object_allocate:
19126f9a767SRodney W. Grimes  *
19226f9a767SRodney W. Grimes  *	Returns a new object with the given size.
19326f9a767SRodney W. Grimes  */
19426f9a767SRodney W. Grimes 
19526f9a767SRodney W. Grimes vm_object_t
19626f9a767SRodney W. Grimes vm_object_allocate(size)
19726f9a767SRodney W. Grimes 	vm_size_t size;
19826f9a767SRodney W. Grimes {
19926f9a767SRodney W. Grimes 	register vm_object_t result;
20026f9a767SRodney W. Grimes 
20126f9a767SRodney W. Grimes 	result = (vm_object_t)
20226f9a767SRodney W. Grimes 	    malloc((u_long) sizeof *result, M_VMOBJ, M_WAITOK);
20326f9a767SRodney W. Grimes 
20426f9a767SRodney W. Grimes 
20526f9a767SRodney W. Grimes 	_vm_object_allocate(size, result);
20626f9a767SRodney W. Grimes 
20726f9a767SRodney W. Grimes 	return (result);
20826f9a767SRodney W. Grimes }
20926f9a767SRodney W. Grimes 
21026f9a767SRodney W. Grimes 
21126f9a767SRodney W. Grimes /*
212df8bae1dSRodney W. Grimes  *	vm_object_reference:
213df8bae1dSRodney W. Grimes  *
214df8bae1dSRodney W. Grimes  *	Gets another reference to the given object.
215df8bae1dSRodney W. Grimes  */
21626f9a767SRodney W. Grimes inline void
21726f9a767SRodney W. Grimes vm_object_reference(object)
218df8bae1dSRodney W. Grimes 	register vm_object_t object;
219df8bae1dSRodney W. Grimes {
220df8bae1dSRodney W. Grimes 	if (object == NULL)
221df8bae1dSRodney W. Grimes 		return;
222df8bae1dSRodney W. Grimes 
223df8bae1dSRodney W. Grimes 	vm_object_lock(object);
224df8bae1dSRodney W. Grimes 	object->ref_count++;
225df8bae1dSRodney W. Grimes 	vm_object_unlock(object);
226df8bae1dSRodney W. Grimes }
227df8bae1dSRodney W. Grimes 
228df8bae1dSRodney W. Grimes /*
229df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
230df8bae1dSRodney W. Grimes  *
231df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
232df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
233df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
234df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
235df8bae1dSRodney W. Grimes  *	may be relinquished.
236df8bae1dSRodney W. Grimes  *
237df8bae1dSRodney W. Grimes  *	No object may be locked.
238df8bae1dSRodney W. Grimes  */
23926f9a767SRodney W. Grimes void
24026f9a767SRodney W. Grimes vm_object_deallocate(object)
24126f9a767SRodney W. Grimes 	vm_object_t object;
242df8bae1dSRodney W. Grimes {
243df8bae1dSRodney W. Grimes 	vm_object_t temp;
244df8bae1dSRodney W. Grimes 
245df8bae1dSRodney W. Grimes 	while (object != NULL) {
246df8bae1dSRodney W. Grimes 
247df8bae1dSRodney W. Grimes 		/*
2480d94caffSDavid Greenman 		 * The cache holds a reference (uncounted) to the object; we
2490d94caffSDavid Greenman 		 * must lock it before removing the object.
250df8bae1dSRodney W. Grimes 		 */
251df8bae1dSRodney W. Grimes 
252df8bae1dSRodney W. Grimes 		vm_object_cache_lock();
253df8bae1dSRodney W. Grimes 
254df8bae1dSRodney W. Grimes 		/*
255df8bae1dSRodney W. Grimes 		 * Lose the reference
256df8bae1dSRodney W. Grimes 		 */
257df8bae1dSRodney W. Grimes 		vm_object_lock(object);
258df8bae1dSRodney W. Grimes 		if (--(object->ref_count) != 0) {
259010cf3b9SDavid Greenman 			if (object->ref_count == 1) {
260010cf3b9SDavid Greenman 				if (object->reverse_shadow_head.tqh_first) {
261010cf3b9SDavid Greenman 					++object->reverse_shadow_head.tqh_first->ref_count;
2620d94caffSDavid Greenman 					if (vm_object_lock_try(object->reverse_shadow_head.tqh_first)) {
2632fe6e4d7SDavid Greenman 						vm_object_rcollapse(object->reverse_shadow_head.tqh_first, object);
2640d94caffSDavid Greenman 						vm_object_unlock(object->reverse_shadow_head.tqh_first);
2650d94caffSDavid Greenman 					}
266010cf3b9SDavid Greenman 					vm_object_deallocate(object->reverse_shadow_head.tqh_first);
267010cf3b9SDavid Greenman 				}
268010cf3b9SDavid Greenman 			}
26926f9a767SRodney W. Grimes 			vm_object_unlock(object);
270df8bae1dSRodney W. Grimes 			/*
2710d94caffSDavid Greenman 			 * If there are still references, then we are done.
272df8bae1dSRodney W. Grimes 			 */
273df8bae1dSRodney W. Grimes 			vm_object_cache_unlock();
274df8bae1dSRodney W. Grimes 			return;
275df8bae1dSRodney W. Grimes 		}
276df8bae1dSRodney W. Grimes 		/*
2770d94caffSDavid Greenman 		 * See if this object can persist.  If so, enter it in the
2780d94caffSDavid Greenman 		 * cache, then deactivate all of its pages.
279df8bae1dSRodney W. Grimes 		 */
280df8bae1dSRodney W. Grimes 
281df8bae1dSRodney W. Grimes 		if (object->flags & OBJ_CANPERSIST) {
282df8bae1dSRodney W. Grimes 
283df8bae1dSRodney W. Grimes 			TAILQ_INSERT_TAIL(&vm_object_cached_list, object,
284df8bae1dSRodney W. Grimes 			    cached_list);
285df8bae1dSRodney W. Grimes 			vm_object_cached++;
286df8bae1dSRodney W. Grimes 			vm_object_cache_unlock();
287df8bae1dSRodney W. Grimes 
288df8bae1dSRodney W. Grimes 			vm_object_unlock(object);
289df8bae1dSRodney W. Grimes 
290df8bae1dSRodney W. Grimes 			vm_object_cache_trim();
291df8bae1dSRodney W. Grimes 			return;
292df8bae1dSRodney W. Grimes 		}
293df8bae1dSRodney W. Grimes 		/*
294df8bae1dSRodney W. Grimes 		 * Make sure no one can look us up now.
295df8bae1dSRodney W. Grimes 		 */
2960d94caffSDavid Greenman 		object->flags |= OBJ_DEAD;
297df8bae1dSRodney W. Grimes 		vm_object_remove(object->pager);
298df8bae1dSRodney W. Grimes 		vm_object_cache_unlock();
299df8bae1dSRodney W. Grimes 
300df8bae1dSRodney W. Grimes 		temp = object->shadow;
3012fe6e4d7SDavid Greenman 		if (temp)
3022fe6e4d7SDavid Greenman 			TAILQ_REMOVE(&temp->reverse_shadow_head, object, reverse_shadow_list);
303df8bae1dSRodney W. Grimes 		vm_object_terminate(object);
304df8bae1dSRodney W. Grimes 		/* unlocks and deallocates object */
305df8bae1dSRodney W. Grimes 		object = temp;
306df8bae1dSRodney W. Grimes 	}
307df8bae1dSRodney W. Grimes }
308df8bae1dSRodney W. Grimes 
309df8bae1dSRodney W. Grimes /*
310df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
311df8bae1dSRodney W. Grimes  *	up all previously used resources.
312df8bae1dSRodney W. Grimes  *
313df8bae1dSRodney W. Grimes  *	The object must be locked.
314df8bae1dSRodney W. Grimes  */
31526f9a767SRodney W. Grimes void
31626f9a767SRodney W. Grimes vm_object_terminate(object)
317df8bae1dSRodney W. Grimes 	register vm_object_t object;
318df8bae1dSRodney W. Grimes {
3190d94caffSDavid Greenman 	register vm_page_t p, next;
320df8bae1dSRodney W. Grimes 	vm_object_t shadow_object;
32126f9a767SRodney W. Grimes 	int s;
3220d94caffSDavid Greenman 	struct vnode *vp = NULL;
323df8bae1dSRodney W. Grimes 
324df8bae1dSRodney W. Grimes 	/*
3250d94caffSDavid Greenman 	 * Detach the object from its shadow if we are the shadow's copy.
326df8bae1dSRodney W. Grimes 	 */
327df8bae1dSRodney W. Grimes 	if ((shadow_object = object->shadow) != NULL) {
328df8bae1dSRodney W. Grimes 		vm_object_lock(shadow_object);
329df8bae1dSRodney W. Grimes 		if (shadow_object->copy == object)
330df8bae1dSRodney W. Grimes 			shadow_object->copy = NULL;
33126f9a767SRodney W. Grimes /*
332df8bae1dSRodney W. Grimes 		else if (shadow_object->copy != NULL)
333df8bae1dSRodney W. Grimes 			panic("vm_object_terminate: copy/shadow inconsistency");
33426f9a767SRodney W. Grimes */
335df8bae1dSRodney W. Grimes 		vm_object_unlock(shadow_object);
336df8bae1dSRodney W. Grimes 	}
3370d94caffSDavid Greenman 	if (object->pager && (object->pager->pg_type == PG_VNODE)) {
3380d94caffSDavid Greenman 		vn_pager_t vnp = object->pager->pg_data;
339df8bae1dSRodney W. Grimes 
3400d94caffSDavid Greenman 		vp = vnp->vnp_vp;
3410d94caffSDavid Greenman 		VOP_FSYNC(vp, NOCRED, MNT_WAIT, NULL);
3420d94caffSDavid Greenman 		vinvalbuf(vp, 0, NOCRED, NULL, 0, 0);
3430d94caffSDavid Greenman 	}
344df8bae1dSRodney W. Grimes 	/*
3450d94caffSDavid Greenman 	 * Wait until the pageout daemon is through with the object.
346df8bae1dSRodney W. Grimes 	 */
34726f9a767SRodney W. Grimes 
3480d94caffSDavid Greenman 	s = splhigh();
349df8bae1dSRodney W. Grimes 	while (object->paging_in_progress) {
3500d94caffSDavid Greenman 		vm_object_unlock(object);
3510d94caffSDavid Greenman 		tsleep((caddr_t) object, PVM, "objtrm", 0);
352df8bae1dSRodney W. Grimes 		vm_object_lock(object);
353df8bae1dSRodney W. Grimes 	}
3540d94caffSDavid Greenman 	splx(s);
355df8bae1dSRodney W. Grimes 
356df8bae1dSRodney W. Grimes 	/*
3570d94caffSDavid Greenman 	 * While the paging system is locked, pull the object's pages off the
3580d94caffSDavid Greenman 	 * active and inactive queues.  This keeps the pageout daemon from
3590d94caffSDavid Greenman 	 * playing with them during vm_pager_deallocate.
360df8bae1dSRodney W. Grimes 	 *
3610d94caffSDavid Greenman 	 * We can't free the pages yet, because the object's pager may have to
3620d94caffSDavid Greenman 	 * write them out before deallocating the paging space.
363df8bae1dSRodney W. Grimes 	 */
36426f9a767SRodney W. Grimes 
3650d94caffSDavid Greenman 	for (p = object->memq.tqh_first; p; p = next) {
36626f9a767SRodney W. Grimes 		VM_PAGE_CHECK(p);
3670d94caffSDavid Greenman 		next = p->listq.tqe_next;
36826f9a767SRodney W. Grimes 
36926f9a767SRodney W. Grimes 		vm_page_lock_queues();
3700d94caffSDavid Greenman 		if (p->flags & PG_CACHE)
3710d94caffSDavid Greenman 			vm_page_free(p);
3720d94caffSDavid Greenman 		else
3730d94caffSDavid Greenman 			vm_page_unqueue(p);
37426f9a767SRodney W. Grimes 		vm_page_unlock_queues();
3750d94caffSDavid Greenman 		p = next;
37626f9a767SRodney W. Grimes 	}
37726f9a767SRodney W. Grimes 
37826f9a767SRodney W. Grimes 	if (object->paging_in_progress != 0)
37926f9a767SRodney W. Grimes 		panic("vm_object_deallocate: pageout in progress");
38026f9a767SRodney W. Grimes 
38126f9a767SRodney W. Grimes 	/*
3820d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
3830d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
38426f9a767SRodney W. Grimes 	 */
38526f9a767SRodney W. Grimes 
386df8bae1dSRodney W. Grimes 	if ((object->flags & OBJ_INTERNAL) == 0) {
387df8bae1dSRodney W. Grimes 		(void) vm_object_page_clean(object, 0, 0, TRUE, TRUE);
388df8bae1dSRodney W. Grimes 	}
389df8bae1dSRodney W. Grimes 	/*
3900d94caffSDavid Greenman 	 * one last time -- get rid of buffers that might have been created
3910d94caffSDavid Greenman 	 * for the vm_object_page_clean
3920d94caffSDavid Greenman 	 */
3930d94caffSDavid Greenman 	if (vp != NULL) {
3940d94caffSDavid Greenman 		vm_object_unlock(object);
3950d94caffSDavid Greenman 		vinvalbuf(vp, 0, NOCRED, NULL, 0, 0);
3960d94caffSDavid Greenman 		vm_object_lock(object);
3970d94caffSDavid Greenman 	}
3980d94caffSDavid Greenman 	/*
3990d94caffSDavid Greenman 	 * Now free the pages. For internal objects, this also removes them
4000d94caffSDavid Greenman 	 * from paging queues.
401df8bae1dSRodney W. Grimes 	 */
402df8bae1dSRodney W. Grimes 	while ((p = object->memq.tqh_first) != NULL) {
403df8bae1dSRodney W. Grimes 		VM_PAGE_CHECK(p);
404df8bae1dSRodney W. Grimes 		vm_page_lock_queues();
4050d94caffSDavid Greenman 		PAGE_WAKEUP(p);
406df8bae1dSRodney W. Grimes 		vm_page_free(p);
407df8bae1dSRodney W. Grimes 		cnt.v_pfree++;
408df8bae1dSRodney W. Grimes 		vm_page_unlock_queues();
409df8bae1dSRodney W. Grimes 	}
4100d94caffSDavid Greenman 	vm_object_unlock(object);
411df8bae1dSRodney W. Grimes 
412df8bae1dSRodney W. Grimes 	/*
413df8bae1dSRodney W. Grimes 	 * Let the pager know object is dead.
414df8bae1dSRodney W. Grimes 	 */
415df8bae1dSRodney W. Grimes 	if (object->pager != NULL)
416df8bae1dSRodney W. Grimes 		vm_pager_deallocate(object->pager);
417df8bae1dSRodney W. Grimes 
418df8bae1dSRodney W. Grimes 	simple_lock(&vm_object_list_lock);
419df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
420df8bae1dSRodney W. Grimes 	vm_object_count--;
421df8bae1dSRodney W. Grimes 	simple_unlock(&vm_object_list_lock);
422df8bae1dSRodney W. Grimes 
423df8bae1dSRodney W. Grimes 	/*
424df8bae1dSRodney W. Grimes 	 * Free the space for the object.
425df8bae1dSRodney W. Grimes 	 */
426df8bae1dSRodney W. Grimes 	free((caddr_t) object, M_VMOBJ);
427df8bae1dSRodney W. Grimes }
428df8bae1dSRodney W. Grimes 
429df8bae1dSRodney W. Grimes /*
430df8bae1dSRodney W. Grimes  *	vm_object_page_clean
431df8bae1dSRodney W. Grimes  *
432df8bae1dSRodney W. Grimes  *	Clean all dirty pages in the specified range of object.
43326f9a767SRodney W. Grimes  *	Leaves page on whatever queue it is currently on.
43426f9a767SRodney W. Grimes  *
43526f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
43626f9a767SRodney W. Grimes  *
43726f9a767SRodney W. Grimes  *	The object must be locked.
43826f9a767SRodney W. Grimes  */
43926f9a767SRodney W. Grimes #if 1
44026f9a767SRodney W. Grimes boolean_t
44126f9a767SRodney W. Grimes vm_object_page_clean(object, start, end, syncio, de_queue)
44226f9a767SRodney W. Grimes 	register vm_object_t object;
44326f9a767SRodney W. Grimes 	register vm_offset_t start;
44426f9a767SRodney W. Grimes 	register vm_offset_t end;
44526f9a767SRodney W. Grimes 	boolean_t syncio;
44626f9a767SRodney W. Grimes 	boolean_t de_queue;
44726f9a767SRodney W. Grimes {
44826f9a767SRodney W. Grimes 	register vm_page_t p, nextp;
44926f9a767SRodney W. Grimes 	int size;
45026f9a767SRodney W. Grimes 
45126f9a767SRodney W. Grimes 	if (object->pager == NULL)
45226f9a767SRodney W. Grimes 		return 1;
45326f9a767SRodney W. Grimes 
45426f9a767SRodney W. Grimes 	if (start != end) {
45526f9a767SRodney W. Grimes 		start = trunc_page(start);
45626f9a767SRodney W. Grimes 		end = round_page(end);
45726f9a767SRodney W. Grimes 	}
45826f9a767SRodney W. Grimes 	size = end - start;
45926f9a767SRodney W. Grimes 
46026f9a767SRodney W. Grimes again:
46126f9a767SRodney W. Grimes 	/*
46226f9a767SRodney W. Grimes 	 * Wait until the pageout daemon is through with the object.
46326f9a767SRodney W. Grimes 	 */
46426f9a767SRodney W. Grimes 	while (object->paging_in_progress) {
4650d94caffSDavid Greenman 		tsleep(object, PVM, "objpcw", 0);
46626f9a767SRodney W. Grimes 	}
46726f9a767SRodney W. Grimes 
46826f9a767SRodney W. Grimes 	nextp = object->memq.tqh_first;
46926f9a767SRodney W. Grimes 	while ((p = nextp) && ((start == end) || (size != 0))) {
47026f9a767SRodney W. Grimes 		nextp = p->listq.tqe_next;
47126f9a767SRodney W. Grimes 		if (start == end || (p->offset >= start && p->offset < end)) {
4720d94caffSDavid Greenman 			if ((p->flags & PG_BUSY) || p->busy) {
4730d94caffSDavid Greenman 				int s = splhigh();
47426f9a767SRodney W. Grimes 
4750d94caffSDavid Greenman 				p->flags |= PG_WANTED;
4760d94caffSDavid Greenman 				tsleep(p, PVM, "objpcn", 0);
4770d94caffSDavid Greenman 				splx(s);
4780d94caffSDavid Greenman 				goto again;
4790d94caffSDavid Greenman 			}
48026f9a767SRodney W. Grimes 			size -= PAGE_SIZE;
48126f9a767SRodney W. Grimes 
4820d94caffSDavid Greenman 			vm_page_test_dirty(p);
48326f9a767SRodney W. Grimes 
4840d94caffSDavid Greenman 			if ((p->dirty & p->valid) != 0) {
48526f9a767SRodney W. Grimes 				vm_pageout_clean(p, VM_PAGEOUT_FORCE);
48626f9a767SRodney W. Grimes 				goto again;
48726f9a767SRodney W. Grimes 			}
48826f9a767SRodney W. Grimes 		}
48926f9a767SRodney W. Grimes 	}
49026f9a767SRodney W. Grimes 	wakeup((caddr_t) object);
49126f9a767SRodney W. Grimes 	return 1;
49226f9a767SRodney W. Grimes }
49326f9a767SRodney W. Grimes #endif
49426f9a767SRodney W. Grimes /*
49526f9a767SRodney W. Grimes  *	vm_object_page_clean
49626f9a767SRodney W. Grimes  *
49726f9a767SRodney W. Grimes  *	Clean all dirty pages in the specified range of object.
498df8bae1dSRodney W. Grimes  *	If syncio is TRUE, page cleaning is done synchronously.
499df8bae1dSRodney W. Grimes  *	If de_queue is TRUE, pages are removed from any paging queue
500df8bae1dSRodney W. Grimes  *	they were on, otherwise they are left on whatever queue they
501df8bae1dSRodney W. Grimes  *	were on before the cleaning operation began.
502df8bae1dSRodney W. Grimes  *
503df8bae1dSRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
504df8bae1dSRodney W. Grimes  *
505df8bae1dSRodney W. Grimes  *	The object must be locked.
506df8bae1dSRodney W. Grimes  *
507df8bae1dSRodney W. Grimes  *	Returns TRUE if all was well, FALSE if there was a pager error
508df8bae1dSRodney W. Grimes  *	somewhere.  We attempt to clean (and dequeue) all pages regardless
509df8bae1dSRodney W. Grimes  *	of where an error occurs.
510df8bae1dSRodney W. Grimes  */
51126f9a767SRodney W. Grimes #if 0
512df8bae1dSRodney W. Grimes boolean_t
513df8bae1dSRodney W. Grimes vm_object_page_clean(object, start, end, syncio, de_queue)
514df8bae1dSRodney W. Grimes 	register vm_object_t object;
515df8bae1dSRodney W. Grimes 	register vm_offset_t start;
516df8bae1dSRodney W. Grimes 	register vm_offset_t end;
517df8bae1dSRodney W. Grimes 	boolean_t syncio;
518df8bae1dSRodney W. Grimes 	boolean_t de_queue;
519df8bae1dSRodney W. Grimes {
520df8bae1dSRodney W. Grimes 	register vm_page_t p;
521df8bae1dSRodney W. Grimes 	int onqueue;
522df8bae1dSRodney W. Grimes 	boolean_t noerror = TRUE;
523df8bae1dSRodney W. Grimes 
524df8bae1dSRodney W. Grimes 	if (object == NULL)
525df8bae1dSRodney W. Grimes 		return (TRUE);
526df8bae1dSRodney W. Grimes 
527df8bae1dSRodney W. Grimes 	/*
528df8bae1dSRodney W. Grimes 	 * If it is an internal object and there is no pager, attempt to
5290d94caffSDavid Greenman 	 * allocate one.  Note that vm_object_collapse may relocate one from a
5300d94caffSDavid Greenman 	 * collapsed object so we must recheck afterward.
531df8bae1dSRodney W. Grimes 	 */
532df8bae1dSRodney W. Grimes 	if ((object->flags & OBJ_INTERNAL) && object->pager == NULL) {
533df8bae1dSRodney W. Grimes 		vm_object_collapse(object);
534df8bae1dSRodney W. Grimes 		if (object->pager == NULL) {
535df8bae1dSRodney W. Grimes 			vm_pager_t pager;
536df8bae1dSRodney W. Grimes 
537df8bae1dSRodney W. Grimes 			vm_object_unlock(object);
538df8bae1dSRodney W. Grimes 			pager = vm_pager_allocate(PG_DFLT, (caddr_t) 0,
539df8bae1dSRodney W. Grimes 			    object->size, VM_PROT_ALL,
540df8bae1dSRodney W. Grimes 			    (vm_offset_t) 0);
541df8bae1dSRodney W. Grimes 			if (pager)
542df8bae1dSRodney W. Grimes 				vm_object_setpager(object, pager, 0, FALSE);
543df8bae1dSRodney W. Grimes 			vm_object_lock(object);
544df8bae1dSRodney W. Grimes 		}
545df8bae1dSRodney W. Grimes 	}
546df8bae1dSRodney W. Grimes 	if (object->pager == NULL)
547df8bae1dSRodney W. Grimes 		return (FALSE);
548df8bae1dSRodney W. Grimes 
549df8bae1dSRodney W. Grimes again:
550df8bae1dSRodney W. Grimes 	/*
551df8bae1dSRodney W. Grimes 	 * Wait until the pageout daemon is through with the object.
552df8bae1dSRodney W. Grimes 	 */
553df8bae1dSRodney W. Grimes 	while (object->paging_in_progress) {
554df8bae1dSRodney W. Grimes 		vm_object_sleep((int) object, object, FALSE);
555df8bae1dSRodney W. Grimes 		vm_object_lock(object);
556df8bae1dSRodney W. Grimes 	}
557df8bae1dSRodney W. Grimes 	/*
558df8bae1dSRodney W. Grimes 	 * Loop through the object page list cleaning as necessary.
559df8bae1dSRodney W. Grimes 	 */
560df8bae1dSRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) {
56126f9a767SRodney W. Grimes 		onqueue = 0;
562df8bae1dSRodney W. Grimes 		if ((start == end || p->offset >= start && p->offset < end) &&
563df8bae1dSRodney W. Grimes 		    !(p->flags & PG_FICTITIOUS)) {
5640d94caffSDavid Greenman 			vm_page_test_dirty(p);
565df8bae1dSRodney W. Grimes 			/*
5660d94caffSDavid Greenman 			 * Remove the page from any paging queue. This needs
5670d94caffSDavid Greenman 			 * to be done if either we have been explicitly asked
5680d94caffSDavid Greenman 			 * to do so or it is about to be cleaned (see comment
5690d94caffSDavid Greenman 			 * below).
570df8bae1dSRodney W. Grimes 			 */
5710d94caffSDavid Greenman 			if (de_queue || (p->dirty & p->valid)) {
572df8bae1dSRodney W. Grimes 				vm_page_lock_queues();
573df8bae1dSRodney W. Grimes 				if (p->flags & PG_ACTIVE) {
574df8bae1dSRodney W. Grimes 					TAILQ_REMOVE(&vm_page_queue_active,
575df8bae1dSRodney W. Grimes 					    p, pageq);
576df8bae1dSRodney W. Grimes 					p->flags &= ~PG_ACTIVE;
577df8bae1dSRodney W. Grimes 					cnt.v_active_count--;
578df8bae1dSRodney W. Grimes 					onqueue = 1;
579df8bae1dSRodney W. Grimes 				} else if (p->flags & PG_INACTIVE) {
580df8bae1dSRodney W. Grimes 					TAILQ_REMOVE(&vm_page_queue_inactive,
581df8bae1dSRodney W. Grimes 					    p, pageq);
582df8bae1dSRodney W. Grimes 					p->flags &= ~PG_INACTIVE;
583df8bae1dSRodney W. Grimes 					cnt.v_inactive_count--;
584df8bae1dSRodney W. Grimes 					onqueue = -1;
585df8bae1dSRodney W. Grimes 				} else
586df8bae1dSRodney W. Grimes 					onqueue = 0;
587df8bae1dSRodney W. Grimes 				vm_page_unlock_queues();
588df8bae1dSRodney W. Grimes 			}
589df8bae1dSRodney W. Grimes 			/*
590df8bae1dSRodney W. Grimes 			 * To ensure the state of the page doesn't change
5910d94caffSDavid Greenman 			 * during the clean operation we do two things. First
5920d94caffSDavid Greenman 			 * we set the busy bit and write-protect all mappings
5930d94caffSDavid Greenman 			 * to ensure that write accesses to the page block (in
5940d94caffSDavid Greenman 			 * vm_fault).  Second, we remove the page from any
5950d94caffSDavid Greenman 			 * paging queue to foil the pageout daemon
5960d94caffSDavid Greenman 			 * (vm_pageout_scan).
597df8bae1dSRodney W. Grimes 			 */
598df8bae1dSRodney W. Grimes 			pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_READ);
5990d94caffSDavid Greenman 			if (p->dirty & p->valid) {
600df8bae1dSRodney W. Grimes 				p->flags |= PG_BUSY;
601df8bae1dSRodney W. Grimes 				object->paging_in_progress++;
602df8bae1dSRodney W. Grimes 				vm_object_unlock(object);
603df8bae1dSRodney W. Grimes 				/*
6040d94caffSDavid Greenman 				 * XXX if put fails we mark the page as clean
6050d94caffSDavid Greenman 				 * to avoid an infinite loop. Will loose
6060d94caffSDavid Greenman 				 * changes to the page.
607df8bae1dSRodney W. Grimes 				 */
608df8bae1dSRodney W. Grimes 				if (vm_pager_put(object->pager, p, syncio)) {
609df8bae1dSRodney W. Grimes 					printf("%s: pager_put error\n",
610df8bae1dSRodney W. Grimes 					    "vm_object_page_clean");
6110d94caffSDavid Greenman 					p->dirty = 0;
612df8bae1dSRodney W. Grimes 					noerror = FALSE;
613df8bae1dSRodney W. Grimes 				}
614df8bae1dSRodney W. Grimes 				vm_object_lock(object);
615df8bae1dSRodney W. Grimes 				object->paging_in_progress--;
616df8bae1dSRodney W. Grimes 				if (!de_queue && onqueue) {
617df8bae1dSRodney W. Grimes 					vm_page_lock_queues();
618df8bae1dSRodney W. Grimes 					if (onqueue > 0)
619df8bae1dSRodney W. Grimes 						vm_page_activate(p);
620df8bae1dSRodney W. Grimes 					else
621df8bae1dSRodney W. Grimes 						vm_page_deactivate(p);
622df8bae1dSRodney W. Grimes 					vm_page_unlock_queues();
623df8bae1dSRodney W. Grimes 				}
624df8bae1dSRodney W. Grimes 				PAGE_WAKEUP(p);
625df8bae1dSRodney W. Grimes 				goto again;
626df8bae1dSRodney W. Grimes 			}
627df8bae1dSRodney W. Grimes 		}
628df8bae1dSRodney W. Grimes 	}
629df8bae1dSRodney W. Grimes 	return (noerror);
630df8bae1dSRodney W. Grimes }
63126f9a767SRodney W. Grimes #endif
632df8bae1dSRodney W. Grimes 
633df8bae1dSRodney W. Grimes /*
634df8bae1dSRodney W. Grimes  *	vm_object_deactivate_pages
635df8bae1dSRodney W. Grimes  *
636df8bae1dSRodney W. Grimes  *	Deactivate all pages in the specified object.  (Keep its pages
637df8bae1dSRodney W. Grimes  *	in memory even though it is no longer referenced.)
638df8bae1dSRodney W. Grimes  *
639df8bae1dSRodney W. Grimes  *	The object must be locked.
640df8bae1dSRodney W. Grimes  */
641df8bae1dSRodney W. Grimes void
642df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object)
643df8bae1dSRodney W. Grimes 	register vm_object_t object;
644df8bae1dSRodney W. Grimes {
645df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
646df8bae1dSRodney W. Grimes 
647df8bae1dSRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = next) {
648df8bae1dSRodney W. Grimes 		next = p->listq.tqe_next;
649df8bae1dSRodney W. Grimes 		vm_page_lock_queues();
650df8bae1dSRodney W. Grimes 		vm_page_deactivate(p);
651df8bae1dSRodney W. Grimes 		vm_page_unlock_queues();
652df8bae1dSRodney W. Grimes 	}
653df8bae1dSRodney W. Grimes }
654df8bae1dSRodney W. Grimes 
655df8bae1dSRodney W. Grimes /*
656df8bae1dSRodney W. Grimes  *	Trim the object cache to size.
657df8bae1dSRodney W. Grimes  */
658df8bae1dSRodney W. Grimes void
659df8bae1dSRodney W. Grimes vm_object_cache_trim()
660df8bae1dSRodney W. Grimes {
661df8bae1dSRodney W. Grimes 	register vm_object_t object;
662df8bae1dSRodney W. Grimes 
663df8bae1dSRodney W. Grimes 	vm_object_cache_lock();
6640d94caffSDavid Greenman 	while (vm_object_cached > vm_object_cache_max) {
665df8bae1dSRodney W. Grimes 		object = vm_object_cached_list.tqh_first;
666df8bae1dSRodney W. Grimes 		vm_object_cache_unlock();
667df8bae1dSRodney W. Grimes 
668df8bae1dSRodney W. Grimes 		if (object != vm_object_lookup(object->pager))
6698e58bf68SDavid Greenman 			panic("vm_object_cache_trim: I'm sooo confused.");
670df8bae1dSRodney W. Grimes 
671df8bae1dSRodney W. Grimes 		pager_cache(object, FALSE);
672df8bae1dSRodney W. Grimes 
673df8bae1dSRodney W. Grimes 		vm_object_cache_lock();
674df8bae1dSRodney W. Grimes 	}
675df8bae1dSRodney W. Grimes 	vm_object_cache_unlock();
676df8bae1dSRodney W. Grimes }
677df8bae1dSRodney W. Grimes 
67826f9a767SRodney W. Grimes 
679df8bae1dSRodney W. Grimes /*
680df8bae1dSRodney W. Grimes  *	vm_object_pmap_copy:
681df8bae1dSRodney W. Grimes  *
682df8bae1dSRodney W. Grimes  *	Makes all physical pages in the specified
683df8bae1dSRodney W. Grimes  *	object range copy-on-write.  No writeable
684df8bae1dSRodney W. Grimes  *	references to these pages should remain.
685df8bae1dSRodney W. Grimes  *
686df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
687df8bae1dSRodney W. Grimes  */
6880d94caffSDavid Greenman void
6890d94caffSDavid Greenman vm_object_pmap_copy(object, start, end)
690df8bae1dSRodney W. Grimes 	register vm_object_t object;
691df8bae1dSRodney W. Grimes 	register vm_offset_t start;
692df8bae1dSRodney W. Grimes 	register vm_offset_t end;
693df8bae1dSRodney W. Grimes {
694df8bae1dSRodney W. Grimes 	register vm_page_t p;
695df8bae1dSRodney W. Grimes 
696df8bae1dSRodney W. Grimes 	if (object == NULL)
697df8bae1dSRodney W. Grimes 		return;
698df8bae1dSRodney W. Grimes 
699df8bae1dSRodney W. Grimes 	vm_object_lock(object);
700df8bae1dSRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) {
701df8bae1dSRodney W. Grimes 		if ((start <= p->offset) && (p->offset < end)) {
702df8bae1dSRodney W. Grimes 			pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_READ);
703df8bae1dSRodney W. Grimes 			p->flags |= PG_COPYONWRITE;
704df8bae1dSRodney W. Grimes 		}
705df8bae1dSRodney W. Grimes 	}
706df8bae1dSRodney W. Grimes 	vm_object_unlock(object);
707df8bae1dSRodney W. Grimes }
708df8bae1dSRodney W. Grimes 
709df8bae1dSRodney W. Grimes /*
710df8bae1dSRodney W. Grimes  *	vm_object_pmap_remove:
711df8bae1dSRodney W. Grimes  *
712df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
713df8bae1dSRodney W. Grimes  *	object range from all physical maps.
714df8bae1dSRodney W. Grimes  *
715df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
716df8bae1dSRodney W. Grimes  */
71726f9a767SRodney W. Grimes void
71826f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end)
719df8bae1dSRodney W. Grimes 	register vm_object_t object;
720df8bae1dSRodney W. Grimes 	register vm_offset_t start;
721df8bae1dSRodney W. Grimes 	register vm_offset_t end;
722df8bae1dSRodney W. Grimes {
723df8bae1dSRodney W. Grimes 	register vm_page_t p;
724a481f200SDavid Greenman 	int s;
725df8bae1dSRodney W. Grimes 
726df8bae1dSRodney W. Grimes 	if (object == NULL)
727df8bae1dSRodney W. Grimes 		return;
7282fe6e4d7SDavid Greenman 	++object->paging_in_progress;
729df8bae1dSRodney W. Grimes 
730df8bae1dSRodney W. Grimes 	vm_object_lock(object);
73126f9a767SRodney W. Grimes again:
73226f9a767SRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) {
73326f9a767SRodney W. Grimes 		if ((start <= p->offset) && (p->offset < end)) {
734a481f200SDavid Greenman 			s = splhigh();
7350d94caffSDavid Greenman 			if ((p->flags & PG_BUSY) || p->busy) {
73626f9a767SRodney W. Grimes 				p->flags |= PG_WANTED;
73726f9a767SRodney W. Grimes 				tsleep((caddr_t) p, PVM, "vmopmr", 0);
738a481f200SDavid Greenman 				splx(s);
73926f9a767SRodney W. Grimes 				goto again;
74026f9a767SRodney W. Grimes 			}
741a481f200SDavid Greenman 			splx(s);
742df8bae1dSRodney W. Grimes 			pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
74326f9a767SRodney W. Grimes 		}
74426f9a767SRodney W. Grimes 	}
745df8bae1dSRodney W. Grimes 	vm_object_unlock(object);
7462fe6e4d7SDavid Greenman 	--object->paging_in_progress;
7472fe6e4d7SDavid Greenman 	if (object->paging_in_progress == 0)
7482fe6e4d7SDavid Greenman 		wakeup((caddr_t) object);
749df8bae1dSRodney W. Grimes }
750df8bae1dSRodney W. Grimes 
751df8bae1dSRodney W. Grimes /*
752df8bae1dSRodney W. Grimes  *	vm_object_copy:
753df8bae1dSRodney W. Grimes  *
754df8bae1dSRodney W. Grimes  *	Create a new object which is a copy of an existing
755df8bae1dSRodney W. Grimes  *	object, and mark all of the pages in the existing
756df8bae1dSRodney W. Grimes  *	object 'copy-on-write'.  The new object has one reference.
757df8bae1dSRodney W. Grimes  *	Returns the new object.
758df8bae1dSRodney W. Grimes  *
759df8bae1dSRodney W. Grimes  *	May defer the copy until later if the object is not backed
760df8bae1dSRodney W. Grimes  *	up by a non-default pager.
761df8bae1dSRodney W. Grimes  */
7620d94caffSDavid Greenman void
7630d94caffSDavid Greenman vm_object_copy(src_object, src_offset, size,
764df8bae1dSRodney W. Grimes     dst_object, dst_offset, src_needs_copy)
765df8bae1dSRodney W. Grimes 	register vm_object_t src_object;
766df8bae1dSRodney W. Grimes 	vm_offset_t src_offset;
767df8bae1dSRodney W. Grimes 	vm_size_t size;
768df8bae1dSRodney W. Grimes 	vm_object_t *dst_object;/* OUT */
769df8bae1dSRodney W. Grimes 	vm_offset_t *dst_offset;/* OUT */
770df8bae1dSRodney W. Grimes 	boolean_t *src_needs_copy;	/* OUT */
771df8bae1dSRodney W. Grimes {
772df8bae1dSRodney W. Grimes 	register vm_object_t new_copy;
773df8bae1dSRodney W. Grimes 	register vm_object_t old_copy;
774df8bae1dSRodney W. Grimes 	vm_offset_t new_start, new_end;
775df8bae1dSRodney W. Grimes 
776df8bae1dSRodney W. Grimes 	register vm_page_t p;
777df8bae1dSRodney W. Grimes 
778df8bae1dSRodney W. Grimes 	if (src_object == NULL) {
779df8bae1dSRodney W. Grimes 		/*
780df8bae1dSRodney W. Grimes 		 * Nothing to copy
781df8bae1dSRodney W. Grimes 		 */
782df8bae1dSRodney W. Grimes 		*dst_object = NULL;
783df8bae1dSRodney W. Grimes 		*dst_offset = 0;
784df8bae1dSRodney W. Grimes 		*src_needs_copy = FALSE;
785df8bae1dSRodney W. Grimes 		return;
786df8bae1dSRodney W. Grimes 	}
787df8bae1dSRodney W. Grimes 	/*
7880d94caffSDavid Greenman 	 * If the object's pager is null_pager or the default pager, we don't
7890d94caffSDavid Greenman 	 * have to make a copy of it.  Instead, we set the needs copy flag and
790df8bae1dSRodney W. Grimes 	 * make a shadow later.
791df8bae1dSRodney W. Grimes 	 */
792df8bae1dSRodney W. Grimes 
793df8bae1dSRodney W. Grimes 	vm_object_lock(src_object);
79426f9a767SRodney W. Grimes 
79526f9a767SRodney W. Grimes 	/*
79626f9a767SRodney W. Grimes 	 * Try to collapse the object before copying it.
79726f9a767SRodney W. Grimes 	 */
79826f9a767SRodney W. Grimes 
79926f9a767SRodney W. Grimes 	vm_object_collapse(src_object);
80026f9a767SRodney W. Grimes 
801df8bae1dSRodney W. Grimes 	if (src_object->pager == NULL ||
80226f9a767SRodney W. Grimes 	    src_object->pager->pg_type == PG_SWAP ||
803df8bae1dSRodney W. Grimes 	    (src_object->flags & OBJ_INTERNAL)) {
804df8bae1dSRodney W. Grimes 
805df8bae1dSRodney W. Grimes 		/*
806df8bae1dSRodney W. Grimes 		 * Make another reference to the object
807df8bae1dSRodney W. Grimes 		 */
808df8bae1dSRodney W. Grimes 		src_object->ref_count++;
809df8bae1dSRodney W. Grimes 
810df8bae1dSRodney W. Grimes 		/*
811df8bae1dSRodney W. Grimes 		 * Mark all of the pages copy-on-write.
812df8bae1dSRodney W. Grimes 		 */
813df8bae1dSRodney W. Grimes 		for (p = src_object->memq.tqh_first; p; p = p->listq.tqe_next)
814df8bae1dSRodney W. Grimes 			if (src_offset <= p->offset &&
815df8bae1dSRodney W. Grimes 			    p->offset < src_offset + size)
816df8bae1dSRodney W. Grimes 				p->flags |= PG_COPYONWRITE;
817df8bae1dSRodney W. Grimes 		vm_object_unlock(src_object);
818df8bae1dSRodney W. Grimes 
819df8bae1dSRodney W. Grimes 		*dst_object = src_object;
820df8bae1dSRodney W. Grimes 		*dst_offset = src_offset;
821df8bae1dSRodney W. Grimes 
822df8bae1dSRodney W. Grimes 		/*
823df8bae1dSRodney W. Grimes 		 * Must make a shadow when write is desired
824df8bae1dSRodney W. Grimes 		 */
825df8bae1dSRodney W. Grimes 		*src_needs_copy = TRUE;
826df8bae1dSRodney W. Grimes 		return;
827df8bae1dSRodney W. Grimes 	}
828df8bae1dSRodney W. Grimes 	/*
8290d94caffSDavid Greenman 	 * If the object has a pager, the pager wants to see all of the
8300d94caffSDavid Greenman 	 * changes.  We need a copy-object for the changed pages.
831df8bae1dSRodney W. Grimes 	 *
8320d94caffSDavid Greenman 	 * If there is a copy-object, and it is empty, no changes have been made
8330d94caffSDavid Greenman 	 * to the object since the copy-object was made.  We can use the same
8340d94caffSDavid Greenman 	 * copy- object.
835df8bae1dSRodney W. Grimes 	 */
836df8bae1dSRodney W. Grimes 
837df8bae1dSRodney W. Grimes Retry1:
838df8bae1dSRodney W. Grimes 	old_copy = src_object->copy;
839df8bae1dSRodney W. Grimes 	if (old_copy != NULL) {
840df8bae1dSRodney W. Grimes 		/*
841df8bae1dSRodney W. Grimes 		 * Try to get the locks (out of order)
842df8bae1dSRodney W. Grimes 		 */
843df8bae1dSRodney W. Grimes 		if (!vm_object_lock_try(old_copy)) {
844df8bae1dSRodney W. Grimes 			vm_object_unlock(src_object);
845df8bae1dSRodney W. Grimes 
846df8bae1dSRodney W. Grimes 			/* should spin a bit here... */
8470d94caffSDavid Greenman 			tsleep((caddr_t) old_copy, PVM, "cpylck", 1);
848df8bae1dSRodney W. Grimes 			vm_object_lock(src_object);
849df8bae1dSRodney W. Grimes 			goto Retry1;
850df8bae1dSRodney W. Grimes 		}
851df8bae1dSRodney W. Grimes 		if (old_copy->resident_page_count == 0 &&
852df8bae1dSRodney W. Grimes 		    old_copy->pager == NULL) {
853df8bae1dSRodney W. Grimes 			/*
8540d94caffSDavid Greenman 			 * Return another reference to the existing
8550d94caffSDavid Greenman 			 * copy-object.
856df8bae1dSRodney W. Grimes 			 */
857df8bae1dSRodney W. Grimes 			old_copy->ref_count++;
858df8bae1dSRodney W. Grimes 			vm_object_unlock(old_copy);
859df8bae1dSRodney W. Grimes 			vm_object_unlock(src_object);
860df8bae1dSRodney W. Grimes 			*dst_object = old_copy;
861df8bae1dSRodney W. Grimes 			*dst_offset = src_offset;
862df8bae1dSRodney W. Grimes 			*src_needs_copy = FALSE;
863df8bae1dSRodney W. Grimes 			return;
864df8bae1dSRodney W. Grimes 		}
865df8bae1dSRodney W. Grimes 		vm_object_unlock(old_copy);
866df8bae1dSRodney W. Grimes 	}
867df8bae1dSRodney W. Grimes 	vm_object_unlock(src_object);
868df8bae1dSRodney W. Grimes 
869df8bae1dSRodney W. Grimes 	/*
8700d94caffSDavid Greenman 	 * If the object has a pager, the pager wants to see all of the
8710d94caffSDavid Greenman 	 * changes.  We must make a copy-object and put the changed pages
8720d94caffSDavid Greenman 	 * there.
873df8bae1dSRodney W. Grimes 	 *
8740d94caffSDavid Greenman 	 * The copy-object is always made large enough to completely shadow the
8750d94caffSDavid Greenman 	 * original object, since it may have several users who want to shadow
876df8bae1dSRodney W. Grimes 	 * the original object at different points.
877df8bae1dSRodney W. Grimes 	 */
878df8bae1dSRodney W. Grimes 
879df8bae1dSRodney W. Grimes 	new_copy = vm_object_allocate(src_object->size);
880df8bae1dSRodney W. Grimes 
881df8bae1dSRodney W. Grimes Retry2:
882df8bae1dSRodney W. Grimes 	vm_object_lock(src_object);
883df8bae1dSRodney W. Grimes 	/*
884df8bae1dSRodney W. Grimes 	 * Copy object may have changed while we were unlocked
885df8bae1dSRodney W. Grimes 	 */
886df8bae1dSRodney W. Grimes 	old_copy = src_object->copy;
887df8bae1dSRodney W. Grimes 	if (old_copy != NULL) {
888df8bae1dSRodney W. Grimes 		/*
889df8bae1dSRodney W. Grimes 		 * Try to get the locks (out of order)
890df8bae1dSRodney W. Grimes 		 */
891df8bae1dSRodney W. Grimes 		if (!vm_object_lock_try(old_copy)) {
892df8bae1dSRodney W. Grimes 			vm_object_unlock(src_object);
8930d94caffSDavid Greenman 			tsleep((caddr_t) old_copy, PVM, "cpylck", 1);
894df8bae1dSRodney W. Grimes 			goto Retry2;
895df8bae1dSRodney W. Grimes 		}
896df8bae1dSRodney W. Grimes 		/*
897df8bae1dSRodney W. Grimes 		 * Consistency check
898df8bae1dSRodney W. Grimes 		 */
899df8bae1dSRodney W. Grimes 		if (old_copy->shadow != src_object ||
900df8bae1dSRodney W. Grimes 		    old_copy->shadow_offset != (vm_offset_t) 0)
901df8bae1dSRodney W. Grimes 			panic("vm_object_copy: copy/shadow inconsistency");
902df8bae1dSRodney W. Grimes 
903df8bae1dSRodney W. Grimes 		/*
9040d94caffSDavid Greenman 		 * Make the old copy-object shadow the new one. It will
9050d94caffSDavid Greenman 		 * receive no more pages from the original object.
906df8bae1dSRodney W. Grimes 		 */
907df8bae1dSRodney W. Grimes 
908df8bae1dSRodney W. Grimes 		src_object->ref_count--;	/* remove ref. from old_copy */
9092fe6e4d7SDavid Greenman 		if (old_copy->shadow)
9102fe6e4d7SDavid Greenman 			TAILQ_REMOVE(&old_copy->shadow->reverse_shadow_head, old_copy, reverse_shadow_list);
911df8bae1dSRodney W. Grimes 		old_copy->shadow = new_copy;
9122fe6e4d7SDavid Greenman 		TAILQ_INSERT_TAIL(&old_copy->shadow->reverse_shadow_head, old_copy, reverse_shadow_list);
9130d94caffSDavid Greenman 		new_copy->ref_count++;	/* locking not needed - we have the
9140d94caffSDavid Greenman 					 * only pointer */
915df8bae1dSRodney W. Grimes 		vm_object_unlock(old_copy);	/* done with old_copy */
916df8bae1dSRodney W. Grimes 	}
917df8bae1dSRodney W. Grimes 	new_start = (vm_offset_t) 0;	/* always shadow original at 0 */
918df8bae1dSRodney W. Grimes 	new_end = (vm_offset_t) new_copy->size;	/* for the whole object */
919df8bae1dSRodney W. Grimes 
920df8bae1dSRodney W. Grimes 	/*
921df8bae1dSRodney W. Grimes 	 * Point the new copy at the existing object.
922df8bae1dSRodney W. Grimes 	 */
923df8bae1dSRodney W. Grimes 
924df8bae1dSRodney W. Grimes 	new_copy->shadow = src_object;
9252fe6e4d7SDavid Greenman 	TAILQ_INSERT_TAIL(&new_copy->shadow->reverse_shadow_head, new_copy, reverse_shadow_list);
926df8bae1dSRodney W. Grimes 	new_copy->shadow_offset = new_start;
927df8bae1dSRodney W. Grimes 	src_object->ref_count++;
928df8bae1dSRodney W. Grimes 	src_object->copy = new_copy;
929df8bae1dSRodney W. Grimes 
930df8bae1dSRodney W. Grimes 	/*
9310d94caffSDavid Greenman 	 * Mark all the affected pages of the existing object copy-on-write.
932df8bae1dSRodney W. Grimes 	 */
933df8bae1dSRodney W. Grimes 	for (p = src_object->memq.tqh_first; p != NULL; p = p->listq.tqe_next)
934df8bae1dSRodney W. Grimes 		if ((new_start <= p->offset) && (p->offset < new_end))
935df8bae1dSRodney W. Grimes 			p->flags |= PG_COPYONWRITE;
936df8bae1dSRodney W. Grimes 
937df8bae1dSRodney W. Grimes 	vm_object_unlock(src_object);
938df8bae1dSRodney W. Grimes 
939df8bae1dSRodney W. Grimes 	*dst_object = new_copy;
940df8bae1dSRodney W. Grimes 	*dst_offset = src_offset - new_start;
941df8bae1dSRodney W. Grimes 	*src_needs_copy = FALSE;
942df8bae1dSRodney W. Grimes }
943df8bae1dSRodney W. Grimes 
944df8bae1dSRodney W. Grimes /*
945df8bae1dSRodney W. Grimes  *	vm_object_shadow:
946df8bae1dSRodney W. Grimes  *
947df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
948df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
949df8bae1dSRodney W. Grimes  *	object reference is deallocated.
950df8bae1dSRodney W. Grimes  *
951df8bae1dSRodney W. Grimes  *	The new object and offset into that object
952df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
953df8bae1dSRodney W. Grimes  */
954df8bae1dSRodney W. Grimes 
95526f9a767SRodney W. Grimes void
95626f9a767SRodney W. Grimes vm_object_shadow(object, offset, length)
957df8bae1dSRodney W. Grimes 	vm_object_t *object;	/* IN/OUT */
958df8bae1dSRodney W. Grimes 	vm_offset_t *offset;	/* IN/OUT */
959df8bae1dSRodney W. Grimes 	vm_size_t length;
960df8bae1dSRodney W. Grimes {
961df8bae1dSRodney W. Grimes 	register vm_object_t source;
962df8bae1dSRodney W. Grimes 	register vm_object_t result;
963df8bae1dSRodney W. Grimes 
964df8bae1dSRodney W. Grimes 	source = *object;
965df8bae1dSRodney W. Grimes 
966df8bae1dSRodney W. Grimes 	/*
967df8bae1dSRodney W. Grimes 	 * Allocate a new object with the given length
968df8bae1dSRodney W. Grimes 	 */
969df8bae1dSRodney W. Grimes 
970df8bae1dSRodney W. Grimes 	if ((result = vm_object_allocate(length)) == NULL)
971df8bae1dSRodney W. Grimes 		panic("vm_object_shadow: no object for shadowing");
972df8bae1dSRodney W. Grimes 
973df8bae1dSRodney W. Grimes 	/*
9740d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
9750d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
9760d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
9770d94caffSDavid Greenman 	 * of reference count.
978df8bae1dSRodney W. Grimes 	 */
979df8bae1dSRodney W. Grimes 	result->shadow = source;
9807b18a718SDavid Greenman 	if (source)
9812fe6e4d7SDavid Greenman 		TAILQ_INSERT_TAIL(&result->shadow->reverse_shadow_head, result, reverse_shadow_list);
982df8bae1dSRodney W. Grimes 
983df8bae1dSRodney W. Grimes 	/*
9840d94caffSDavid Greenman 	 * Store the offset into the source object, and fix up the offset into
9850d94caffSDavid Greenman 	 * the new object.
986df8bae1dSRodney W. Grimes 	 */
987df8bae1dSRodney W. Grimes 
988df8bae1dSRodney W. Grimes 	result->shadow_offset = *offset;
989df8bae1dSRodney W. Grimes 
990df8bae1dSRodney W. Grimes 	/*
991df8bae1dSRodney W. Grimes 	 * Return the new things
992df8bae1dSRodney W. Grimes 	 */
993df8bae1dSRodney W. Grimes 
994df8bae1dSRodney W. Grimes 	*offset = 0;
995df8bae1dSRodney W. Grimes 	*object = result;
996df8bae1dSRodney W. Grimes }
997df8bae1dSRodney W. Grimes 
998df8bae1dSRodney W. Grimes /*
999df8bae1dSRodney W. Grimes  *	Set the specified object's pager to the specified pager.
1000df8bae1dSRodney W. Grimes  */
1001df8bae1dSRodney W. Grimes 
100226f9a767SRodney W. Grimes void
100326f9a767SRodney W. Grimes vm_object_setpager(object, pager, paging_offset,
1004df8bae1dSRodney W. Grimes     read_only)
1005df8bae1dSRodney W. Grimes 	vm_object_t object;
1006df8bae1dSRodney W. Grimes 	vm_pager_t pager;
1007df8bae1dSRodney W. Grimes 	vm_offset_t paging_offset;
1008df8bae1dSRodney W. Grimes 	boolean_t read_only;
1009df8bae1dSRodney W. Grimes {
1010df8bae1dSRodney W. Grimes 	vm_object_lock(object);	/* XXX ? */
101126f9a767SRodney W. Grimes 	if (object->pager && object->pager != pager) {
101226f9a767SRodney W. Grimes 		panic("!!!pager already allocated!!!\n");
101326f9a767SRodney W. Grimes 	}
1014df8bae1dSRodney W. Grimes 	object->pager = pager;
1015df8bae1dSRodney W. Grimes 	object->paging_offset = paging_offset;
1016df8bae1dSRodney W. Grimes 	vm_object_unlock(object);	/* XXX ? */
1017df8bae1dSRodney W. Grimes }
1018df8bae1dSRodney W. Grimes 
1019df8bae1dSRodney W. Grimes /*
1020df8bae1dSRodney W. Grimes  *	vm_object_hash hashes the pager/id pair.
1021df8bae1dSRodney W. Grimes  */
1022df8bae1dSRodney W. Grimes 
1023df8bae1dSRodney W. Grimes #define vm_object_hash(pager) \
102426f9a767SRodney W. Grimes 	(((unsigned)pager >> 5)%VM_OBJECT_HASH_COUNT)
1025df8bae1dSRodney W. Grimes 
1026df8bae1dSRodney W. Grimes /*
1027df8bae1dSRodney W. Grimes  *	vm_object_lookup looks in the object cache for an object with the
1028df8bae1dSRodney W. Grimes  *	specified pager and paging id.
1029df8bae1dSRodney W. Grimes  */
1030df8bae1dSRodney W. Grimes 
10310d94caffSDavid Greenman vm_object_t
10320d94caffSDavid Greenman vm_object_lookup(pager)
1033df8bae1dSRodney W. Grimes 	vm_pager_t pager;
1034df8bae1dSRodney W. Grimes {
1035df8bae1dSRodney W. Grimes 	register vm_object_hash_entry_t entry;
1036df8bae1dSRodney W. Grimes 	vm_object_t object;
1037df8bae1dSRodney W. Grimes 
1038a08a17a3SDavid Greenman 	cnt.v_lookups++;
1039df8bae1dSRodney W. Grimes 	vm_object_cache_lock();
1040df8bae1dSRodney W. Grimes 
1041df8bae1dSRodney W. Grimes 	for (entry = vm_object_hashtable[vm_object_hash(pager)].tqh_first;
1042df8bae1dSRodney W. Grimes 	    entry != NULL;
1043df8bae1dSRodney W. Grimes 	    entry = entry->hash_links.tqe_next) {
1044df8bae1dSRodney W. Grimes 		object = entry->object;
1045df8bae1dSRodney W. Grimes 		if (object->pager == pager) {
1046df8bae1dSRodney W. Grimes 			vm_object_lock(object);
1047df8bae1dSRodney W. Grimes 			if (object->ref_count == 0) {
1048df8bae1dSRodney W. Grimes 				TAILQ_REMOVE(&vm_object_cached_list, object,
1049df8bae1dSRodney W. Grimes 				    cached_list);
1050df8bae1dSRodney W. Grimes 				vm_object_cached--;
1051df8bae1dSRodney W. Grimes 			}
1052df8bae1dSRodney W. Grimes 			object->ref_count++;
1053df8bae1dSRodney W. Grimes 			vm_object_unlock(object);
1054df8bae1dSRodney W. Grimes 			vm_object_cache_unlock();
1055a08a17a3SDavid Greenman 			cnt.v_hits++;
1056df8bae1dSRodney W. Grimes 			return (object);
1057df8bae1dSRodney W. Grimes 		}
1058df8bae1dSRodney W. Grimes 	}
1059df8bae1dSRodney W. Grimes 
1060df8bae1dSRodney W. Grimes 	vm_object_cache_unlock();
1061df8bae1dSRodney W. Grimes 	return (NULL);
1062df8bae1dSRodney W. Grimes }
1063df8bae1dSRodney W. Grimes 
1064df8bae1dSRodney W. Grimes /*
1065df8bae1dSRodney W. Grimes  *	vm_object_enter enters the specified object/pager/id into
1066df8bae1dSRodney W. Grimes  *	the hash table.
1067df8bae1dSRodney W. Grimes  */
1068df8bae1dSRodney W. Grimes 
10690d94caffSDavid Greenman void
10700d94caffSDavid Greenman vm_object_enter(object, pager)
1071df8bae1dSRodney W. Grimes 	vm_object_t object;
1072df8bae1dSRodney W. Grimes 	vm_pager_t pager;
1073df8bae1dSRodney W. Grimes {
1074df8bae1dSRodney W. Grimes 	struct vm_object_hash_head *bucket;
1075df8bae1dSRodney W. Grimes 	register vm_object_hash_entry_t entry;
1076df8bae1dSRodney W. Grimes 
1077df8bae1dSRodney W. Grimes 	/*
10780d94caffSDavid Greenman 	 * We don't cache null objects, and we can't cache objects with the
10790d94caffSDavid Greenman 	 * null pager.
1080df8bae1dSRodney W. Grimes 	 */
1081df8bae1dSRodney W. Grimes 
1082df8bae1dSRodney W. Grimes 	if (object == NULL)
1083df8bae1dSRodney W. Grimes 		return;
1084df8bae1dSRodney W. Grimes 	if (pager == NULL)
1085df8bae1dSRodney W. Grimes 		return;
1086df8bae1dSRodney W. Grimes 
1087df8bae1dSRodney W. Grimes 	bucket = &vm_object_hashtable[vm_object_hash(pager)];
1088df8bae1dSRodney W. Grimes 	entry = (vm_object_hash_entry_t)
1089df8bae1dSRodney W. Grimes 	    malloc((u_long) sizeof *entry, M_VMOBJHASH, M_WAITOK);
1090df8bae1dSRodney W. Grimes 	entry->object = object;
1091df8bae1dSRodney W. Grimes 	object->flags |= OBJ_CANPERSIST;
1092df8bae1dSRodney W. Grimes 
1093df8bae1dSRodney W. Grimes 	vm_object_cache_lock();
1094df8bae1dSRodney W. Grimes 	TAILQ_INSERT_TAIL(bucket, entry, hash_links);
1095df8bae1dSRodney W. Grimes 	vm_object_cache_unlock();
1096df8bae1dSRodney W. Grimes }
1097df8bae1dSRodney W. Grimes 
1098df8bae1dSRodney W. Grimes /*
1099df8bae1dSRodney W. Grimes  *	vm_object_remove:
1100df8bae1dSRodney W. Grimes  *
1101df8bae1dSRodney W. Grimes  *	Remove the pager from the hash table.
1102df8bae1dSRodney W. Grimes  *	Note:  This assumes that the object cache
1103df8bae1dSRodney W. Grimes  *	is locked.  XXX this should be fixed
1104df8bae1dSRodney W. Grimes  *	by reorganizing vm_object_deallocate.
1105df8bae1dSRodney W. Grimes  */
1106df8bae1dSRodney W. Grimes void
1107df8bae1dSRodney W. Grimes vm_object_remove(pager)
1108df8bae1dSRodney W. Grimes 	register vm_pager_t pager;
1109df8bae1dSRodney W. Grimes {
1110df8bae1dSRodney W. Grimes 	struct vm_object_hash_head *bucket;
1111df8bae1dSRodney W. Grimes 	register vm_object_hash_entry_t entry;
1112df8bae1dSRodney W. Grimes 	register vm_object_t object;
1113df8bae1dSRodney W. Grimes 
1114df8bae1dSRodney W. Grimes 	bucket = &vm_object_hashtable[vm_object_hash(pager)];
1115df8bae1dSRodney W. Grimes 
1116df8bae1dSRodney W. Grimes 	for (entry = bucket->tqh_first;
1117df8bae1dSRodney W. Grimes 	    entry != NULL;
1118df8bae1dSRodney W. Grimes 	    entry = entry->hash_links.tqe_next) {
1119df8bae1dSRodney W. Grimes 		object = entry->object;
1120df8bae1dSRodney W. Grimes 		if (object->pager == pager) {
1121df8bae1dSRodney W. Grimes 			TAILQ_REMOVE(bucket, entry, hash_links);
1122df8bae1dSRodney W. Grimes 			free((caddr_t) entry, M_VMOBJHASH);
1123df8bae1dSRodney W. Grimes 			break;
1124df8bae1dSRodney W. Grimes 		}
1125df8bae1dSRodney W. Grimes 	}
1126df8bae1dSRodney W. Grimes }
1127df8bae1dSRodney W. Grimes 
11282fe6e4d7SDavid Greenman static void
11292fe6e4d7SDavid Greenman vm_object_rcollapse(object, sobject)
11302fe6e4d7SDavid Greenman 	register vm_object_t object, sobject;
11312fe6e4d7SDavid Greenman {
11322fe6e4d7SDavid Greenman 	register vm_object_t backing_object;
11332fe6e4d7SDavid Greenman 	register vm_offset_t backing_offset, new_offset;
11342fe6e4d7SDavid Greenman 	register vm_page_t p, pp;
11352fe6e4d7SDavid Greenman 	register vm_size_t size;
11362fe6e4d7SDavid Greenman 	int s;
11372fe6e4d7SDavid Greenman 
11382fe6e4d7SDavid Greenman 	if (!object)
11392fe6e4d7SDavid Greenman 		return;
11402fe6e4d7SDavid Greenman 	backing_object = object->shadow;
11412fe6e4d7SDavid Greenman 	if (backing_object != sobject) {
11422fe6e4d7SDavid Greenman 		printf("backing obj != sobject!!!\n");
11432fe6e4d7SDavid Greenman 		return;
11442fe6e4d7SDavid Greenman 	}
11452fe6e4d7SDavid Greenman 	if (!backing_object)
11462fe6e4d7SDavid Greenman 		return;
11472fe6e4d7SDavid Greenman 	if ((backing_object->flags & OBJ_INTERNAL) == 0)
11482fe6e4d7SDavid Greenman 		return;
11492fe6e4d7SDavid Greenman 	if (backing_object->shadow != NULL &&
11502fe6e4d7SDavid Greenman 	    backing_object->shadow->copy == backing_object)
11512fe6e4d7SDavid Greenman 		return;
11522fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
11532fe6e4d7SDavid Greenman 		return;
11542fe6e4d7SDavid Greenman 
1155010cf3b9SDavid Greenman 	backing_object->ref_count += 2;
11562fe6e4d7SDavid Greenman 	s = splbio();
11572fe6e4d7SDavid Greenman 	while (backing_object->paging_in_progress) {
11582fe6e4d7SDavid Greenman 		tsleep(backing_object, PVM, "rcolow", 0);
11592fe6e4d7SDavid Greenman 	}
11602fe6e4d7SDavid Greenman 	splx(s);
11612fe6e4d7SDavid Greenman 
11622fe6e4d7SDavid Greenman 	backing_offset = object->shadow_offset;
11632fe6e4d7SDavid Greenman 	size = object->size;
116445cbbb29SDavid Greenman 	p = backing_object->memq.tqh_first;
116545cbbb29SDavid Greenman 	while (p) {
11662fe6e4d7SDavid Greenman 		vm_page_t next;
11672fe6e4d7SDavid Greenman 
11680d94caffSDavid Greenman 		next = p->listq.tqe_next;
11690d94caffSDavid Greenman 		pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
11702fe6e4d7SDavid Greenman 		new_offset = (p->offset - backing_offset);
11712fe6e4d7SDavid Greenman 		if (p->offset < backing_offset ||
11722fe6e4d7SDavid Greenman 		    new_offset >= size) {
11732fe6e4d7SDavid Greenman 			if (backing_object->pager)
11740d94caffSDavid Greenman 				swap_pager_freespace(backing_object->pager,
11750d94caffSDavid Greenman 				    backing_object->paging_offset + p->offset, PAGE_SIZE);
11760d94caffSDavid Greenman 			vm_page_lock_queues();
11772fe6e4d7SDavid Greenman 			vm_page_free(p);
11782fe6e4d7SDavid Greenman 			vm_page_unlock_queues();
11792fe6e4d7SDavid Greenman 		} else {
11802fe6e4d7SDavid Greenman 			pp = vm_page_lookup(object, new_offset);
11810d94caffSDavid Greenman 			if (pp != NULL ||
11820d94caffSDavid Greenman 			    (object->pager &&
11830d94caffSDavid Greenman 			    vm_pager_has_page(object->pager, object->paging_offset + new_offset))) {
11842fe6e4d7SDavid Greenman 				if (backing_object->pager)
11850d94caffSDavid Greenman 					swap_pager_freespace(backing_object->pager,
11860d94caffSDavid Greenman 					    backing_object->paging_offset + p->offset, PAGE_SIZE);
11870d94caffSDavid Greenman 				vm_page_lock_queues();
11882fe6e4d7SDavid Greenman 				vm_page_free(p);
11892fe6e4d7SDavid Greenman 				vm_page_unlock_queues();
11902fe6e4d7SDavid Greenman 			} else {
11910d94caffSDavid Greenman 				if (!backing_object->pager ||
11920d94caffSDavid Greenman 				    !vm_pager_has_page(backing_object->pager, backing_object->paging_offset + p->offset))
11932fe6e4d7SDavid Greenman 					vm_page_rename(p, object, new_offset);
11942fe6e4d7SDavid Greenman 			}
11952fe6e4d7SDavid Greenman 		}
119645cbbb29SDavid Greenman 		p = next;
11972fe6e4d7SDavid Greenman 	}
1198010cf3b9SDavid Greenman 	backing_object->ref_count -= 2;
11992fe6e4d7SDavid Greenman }
12002fe6e4d7SDavid Greenman 
12012fe6e4d7SDavid Greenman /*
12022fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
12032fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
12042fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
12052fe6e4d7SDavid Greenman  */
12062fe6e4d7SDavid Greenman static void
12072fe6e4d7SDavid Greenman vm_object_qcollapse(object)
12082fe6e4d7SDavid Greenman 	register vm_object_t object;
12092fe6e4d7SDavid Greenman {
12102fe6e4d7SDavid Greenman 	register vm_object_t backing_object;
12112fe6e4d7SDavid Greenman 	register vm_offset_t backing_offset, new_offset;
12122fe6e4d7SDavid Greenman 	register vm_page_t p, pp;
12132fe6e4d7SDavid Greenman 	register vm_size_t size;
12142fe6e4d7SDavid Greenman 
12152fe6e4d7SDavid Greenman 	backing_object = object->shadow;
12162fe6e4d7SDavid Greenman 	if (!backing_object)
12172fe6e4d7SDavid Greenman 		return;
12182fe6e4d7SDavid Greenman 	if ((backing_object->flags & OBJ_INTERNAL) == 0)
12192fe6e4d7SDavid Greenman 		return;
12202fe6e4d7SDavid Greenman 	if (backing_object->shadow != NULL &&
12212fe6e4d7SDavid Greenman 	    backing_object->shadow->copy == backing_object)
12222fe6e4d7SDavid Greenman 		return;
12232fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
12242fe6e4d7SDavid Greenman 		return;
12252fe6e4d7SDavid Greenman 
1226010cf3b9SDavid Greenman 	backing_object->ref_count += 2;
1227010cf3b9SDavid Greenman 
12282fe6e4d7SDavid Greenman 	backing_offset = object->shadow_offset;
12292fe6e4d7SDavid Greenman 	size = object->size;
12302fe6e4d7SDavid Greenman 	p = backing_object->memq.tqh_first;
12312fe6e4d7SDavid Greenman 	while (p) {
12322fe6e4d7SDavid Greenman 		vm_page_t next;
12330d94caffSDavid Greenman 
12342fe6e4d7SDavid Greenman 		next = p->listq.tqe_next;
12350d94caffSDavid Greenman 		if ((p->flags & (PG_BUSY | PG_FICTITIOUS | PG_CACHE)) ||
12360d94caffSDavid Greenman 		    !p->valid || p->hold_count || p->wire_count || p->busy || p->bmapped) {
12372fe6e4d7SDavid Greenman 			p = next;
12382fe6e4d7SDavid Greenman 			continue;
12392fe6e4d7SDavid Greenman 		}
12400d94caffSDavid Greenman 		pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
12412fe6e4d7SDavid Greenman 		new_offset = (p->offset - backing_offset);
12422fe6e4d7SDavid Greenman 		if (p->offset < backing_offset ||
12432fe6e4d7SDavid Greenman 		    new_offset >= size) {
12442fe6e4d7SDavid Greenman 			if (backing_object->pager)
12450d94caffSDavid Greenman 				swap_pager_freespace(backing_object->pager,
12460d94caffSDavid Greenman 				    backing_object->paging_offset + p->offset, PAGE_SIZE);
12470d94caffSDavid Greenman 			vm_page_lock_queues();
12482fe6e4d7SDavid Greenman 			vm_page_free(p);
12492fe6e4d7SDavid Greenman 			vm_page_unlock_queues();
12502fe6e4d7SDavid Greenman 		} else {
12512fe6e4d7SDavid Greenman 			pp = vm_page_lookup(object, new_offset);
12522fe6e4d7SDavid Greenman 			if (pp != NULL || (object->pager && vm_pager_has_page(object->pager,
12532fe6e4d7SDavid Greenman 				    object->paging_offset + new_offset))) {
12542fe6e4d7SDavid Greenman 				if (backing_object->pager)
12550d94caffSDavid Greenman 					swap_pager_freespace(backing_object->pager,
12560d94caffSDavid Greenman 					    backing_object->paging_offset + p->offset, PAGE_SIZE);
12570d94caffSDavid Greenman 				vm_page_lock_queues();
12582fe6e4d7SDavid Greenman 				vm_page_free(p);
12592fe6e4d7SDavid Greenman 				vm_page_unlock_queues();
12602fe6e4d7SDavid Greenman 			} else {
12610d94caffSDavid Greenman 				if (!backing_object->pager ||
12620d94caffSDavid Greenman 				    !vm_pager_has_page(backing_object->pager, backing_object->paging_offset + p->offset))
12632fe6e4d7SDavid Greenman 					vm_page_rename(p, object, new_offset);
12642fe6e4d7SDavid Greenman 			}
12652fe6e4d7SDavid Greenman 		}
12662fe6e4d7SDavid Greenman 		p = next;
12672fe6e4d7SDavid Greenman 	}
1268010cf3b9SDavid Greenman 	backing_object->ref_count -= 2;
12692fe6e4d7SDavid Greenman }
12702fe6e4d7SDavid Greenman 
1271df8bae1dSRodney W. Grimes boolean_t vm_object_collapse_allowed = TRUE;
12720d94caffSDavid Greenman 
1273df8bae1dSRodney W. Grimes /*
1274df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1275df8bae1dSRodney W. Grimes  *
1276df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1277df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1278df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1279df8bae1dSRodney W. Grimes  *
1280df8bae1dSRodney W. Grimes  *	Requires that the object be locked and the page
1281df8bae1dSRodney W. Grimes  *	queues be unlocked.
1282df8bae1dSRodney W. Grimes  *
128326f9a767SRodney W. Grimes  *	This routine has significant changes by John S. Dyson
128426f9a767SRodney W. Grimes  *	to fix some swap memory leaks.  18 Dec 93
128526f9a767SRodney W. Grimes  *
1286df8bae1dSRodney W. Grimes  */
128726f9a767SRodney W. Grimes void
128826f9a767SRodney W. Grimes vm_object_collapse(object)
1289df8bae1dSRodney W. Grimes 	register vm_object_t object;
1290df8bae1dSRodney W. Grimes 
1291df8bae1dSRodney W. Grimes {
1292df8bae1dSRodney W. Grimes 	register vm_object_t backing_object;
1293df8bae1dSRodney W. Grimes 	register vm_offset_t backing_offset;
1294df8bae1dSRodney W. Grimes 	register vm_size_t size;
1295df8bae1dSRodney W. Grimes 	register vm_offset_t new_offset;
1296df8bae1dSRodney W. Grimes 	register vm_page_t p, pp;
1297df8bae1dSRodney W. Grimes 
1298df8bae1dSRodney W. Grimes 	if (!vm_object_collapse_allowed)
1299df8bae1dSRodney W. Grimes 		return;
1300df8bae1dSRodney W. Grimes 
1301df8bae1dSRodney W. Grimes 	while (TRUE) {
1302df8bae1dSRodney W. Grimes 		/*
1303df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1304df8bae1dSRodney W. Grimes 		 *
13050d94caffSDavid Greenman 		 * The object exists and no pages in it are currently being paged
13060d94caffSDavid Greenman 		 * out.
1307df8bae1dSRodney W. Grimes 		 */
13082fe6e4d7SDavid Greenman 		if (object == NULL)
1309df8bae1dSRodney W. Grimes 			return;
13102fe6e4d7SDavid Greenman 		if (object->paging_in_progress != 0) {
13112fe6e4d7SDavid Greenman 			if (object->shadow)
13122fe6e4d7SDavid Greenman 				vm_object_qcollapse(object);
13132fe6e4d7SDavid Greenman 			return;
13142fe6e4d7SDavid Greenman 		}
1315df8bae1dSRodney W. Grimes 		/*
1316df8bae1dSRodney W. Grimes 		 * There is a backing object, and
1317df8bae1dSRodney W. Grimes 		 */
1318df8bae1dSRodney W. Grimes 
1319df8bae1dSRodney W. Grimes 		if ((backing_object = object->shadow) == NULL)
1320df8bae1dSRodney W. Grimes 			return;
1321df8bae1dSRodney W. Grimes 
1322df8bae1dSRodney W. Grimes 		vm_object_lock(backing_object);
1323df8bae1dSRodney W. Grimes 		/*
13240d94caffSDavid Greenman 		 * ... The backing object is not read_only, and no pages in
13250d94caffSDavid Greenman 		 * the backing object are currently being paged out. The
13260d94caffSDavid Greenman 		 * backing object is internal.
1327df8bae1dSRodney W. Grimes 		 */
1328df8bae1dSRodney W. Grimes 
1329df8bae1dSRodney W. Grimes 		if ((backing_object->flags & OBJ_INTERNAL) == 0 ||
1330df8bae1dSRodney W. Grimes 		    backing_object->paging_in_progress != 0) {
1331df8bae1dSRodney W. Grimes 			vm_object_unlock(backing_object);
13322fe6e4d7SDavid Greenman 			vm_object_qcollapse(object);
1333df8bae1dSRodney W. Grimes 			return;
1334df8bae1dSRodney W. Grimes 		}
1335df8bae1dSRodney W. Grimes 		/*
13360d94caffSDavid Greenman 		 * The backing object can't be a copy-object: the
13370d94caffSDavid Greenman 		 * shadow_offset for the copy-object must stay as 0.
13380d94caffSDavid Greenman 		 * Furthermore (for the 'we have all the pages' case), if we
13390d94caffSDavid Greenman 		 * bypass backing_object and just shadow the next object in
13400d94caffSDavid Greenman 		 * the chain, old pages from that object would then have to be
13410d94caffSDavid Greenman 		 * copied BOTH into the (former) backing_object and into the
1342df8bae1dSRodney W. Grimes 		 * parent object.
1343df8bae1dSRodney W. Grimes 		 */
1344df8bae1dSRodney W. Grimes 		if (backing_object->shadow != NULL &&
134526f9a767SRodney W. Grimes 		    backing_object->shadow->copy == backing_object) {
1346df8bae1dSRodney W. Grimes 			vm_object_unlock(backing_object);
1347df8bae1dSRodney W. Grimes 			return;
1348df8bae1dSRodney W. Grimes 		}
1349df8bae1dSRodney W. Grimes 		/*
135026f9a767SRodney W. Grimes 		 * we can deal only with the swap pager
135126f9a767SRodney W. Grimes 		 */
135226f9a767SRodney W. Grimes 		if ((object->pager &&
135326f9a767SRodney W. Grimes 			object->pager->pg_type != PG_SWAP) ||
135426f9a767SRodney W. Grimes 		    (backing_object->pager &&
135526f9a767SRodney W. Grimes 			backing_object->pager->pg_type != PG_SWAP)) {
135626f9a767SRodney W. Grimes 			vm_object_unlock(backing_object);
135726f9a767SRodney W. Grimes 			return;
135826f9a767SRodney W. Grimes 		}
135926f9a767SRodney W. Grimes 		/*
13600d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
13610d94caffSDavid Greenman 		 * the parent is the only reference to it) or (perhaps) remove
13620d94caffSDavid Greenman 		 * the parent's reference to it.
1363df8bae1dSRodney W. Grimes 		 */
1364df8bae1dSRodney W. Grimes 
1365df8bae1dSRodney W. Grimes 		backing_offset = object->shadow_offset;
1366df8bae1dSRodney W. Grimes 		size = object->size;
1367df8bae1dSRodney W. Grimes 
1368df8bae1dSRodney W. Grimes 		/*
13690d94caffSDavid Greenman 		 * If there is exactly one reference to the backing object, we
13700d94caffSDavid Greenman 		 * can collapse it into the parent.
1371df8bae1dSRodney W. Grimes 		 */
1372df8bae1dSRodney W. Grimes 
1373df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1374df8bae1dSRodney W. Grimes 
1375df8bae1dSRodney W. Grimes 			/*
1376df8bae1dSRodney W. Grimes 			 * We can collapse the backing object.
1377df8bae1dSRodney W. Grimes 			 *
13780d94caffSDavid Greenman 			 * Move all in-memory pages from backing_object to the
13790d94caffSDavid Greenman 			 * parent.  Pages that have been paged out will be
13800d94caffSDavid Greenman 			 * overwritten by any of the parent's pages that
13810d94caffSDavid Greenman 			 * shadow them.
1382df8bae1dSRodney W. Grimes 			 */
1383df8bae1dSRodney W. Grimes 
138405f0fdd2SPoul-Henning Kamp 			while ((p = backing_object->memq.tqh_first) != 0) {
138526f9a767SRodney W. Grimes 
1386df8bae1dSRodney W. Grimes 				new_offset = (p->offset - backing_offset);
1387df8bae1dSRodney W. Grimes 
1388df8bae1dSRodney W. Grimes 				/*
13890d94caffSDavid Greenman 				 * If the parent has a page here, or if this
13900d94caffSDavid Greenman 				 * page falls outside the parent, dispose of
13910d94caffSDavid Greenman 				 * it.
1392df8bae1dSRodney W. Grimes 				 *
1393df8bae1dSRodney W. Grimes 				 * Otherwise, move it as planned.
1394df8bae1dSRodney W. Grimes 				 */
1395df8bae1dSRodney W. Grimes 
1396df8bae1dSRodney W. Grimes 				if (p->offset < backing_offset ||
1397df8bae1dSRodney W. Grimes 				    new_offset >= size) {
1398df8bae1dSRodney W. Grimes 					vm_page_lock_queues();
13992fe6e4d7SDavid Greenman 					pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
14000d94caffSDavid Greenman 					PAGE_WAKEUP(p);
1401df8bae1dSRodney W. Grimes 					vm_page_free(p);
1402df8bae1dSRodney W. Grimes 					vm_page_unlock_queues();
1403df8bae1dSRodney W. Grimes 				} else {
1404df8bae1dSRodney W. Grimes 					pp = vm_page_lookup(object, new_offset);
140526f9a767SRodney W. Grimes 					if (pp != NULL || (object->pager && vm_pager_has_page(object->pager,
140626f9a767SRodney W. Grimes 					    object->paging_offset + new_offset))) {
1407df8bae1dSRodney W. Grimes 						vm_page_lock_queues();
14082fe6e4d7SDavid Greenman 						pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
14090d94caffSDavid Greenman 						PAGE_WAKEUP(p);
1410df8bae1dSRodney W. Grimes 						vm_page_free(p);
1411df8bae1dSRodney W. Grimes 						vm_page_unlock_queues();
141226f9a767SRodney W. Grimes 					} else {
1413df8bae1dSRodney W. Grimes 						vm_page_rename(p, object, new_offset);
1414df8bae1dSRodney W. Grimes 					}
1415df8bae1dSRodney W. Grimes 				}
1416df8bae1dSRodney W. Grimes 			}
1417df8bae1dSRodney W. Grimes 
1418df8bae1dSRodney W. Grimes 			/*
1419df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1420df8bae1dSRodney W. Grimes 			 */
1421df8bae1dSRodney W. Grimes 
1422df8bae1dSRodney W. Grimes 			if (backing_object->pager) {
142326f9a767SRodney W. Grimes 				backing_object->paging_in_progress++;
142426f9a767SRodney W. Grimes 				if (object->pager) {
142526f9a767SRodney W. Grimes 					vm_pager_t bopager;
14260d94caffSDavid Greenman 
142726f9a767SRodney W. Grimes 					object->paging_in_progress++;
142826f9a767SRodney W. Grimes 					/*
142926f9a767SRodney W. Grimes 					 * copy shadow object pages into ours
14300d94caffSDavid Greenman 					 * and destroy unneeded pages in
14310d94caffSDavid Greenman 					 * shadow object.
143226f9a767SRodney W. Grimes 					 */
143326f9a767SRodney W. Grimes 					bopager = backing_object->pager;
1434df8bae1dSRodney W. Grimes 					backing_object->pager = NULL;
143526f9a767SRodney W. Grimes 					vm_object_remove(backing_object->pager);
143626f9a767SRodney W. Grimes 					swap_pager_copy(
143726f9a767SRodney W. Grimes 					    bopager, backing_object->paging_offset,
143826f9a767SRodney W. Grimes 					    object->pager, object->paging_offset,
143926f9a767SRodney W. Grimes 					    object->shadow_offset);
144026f9a767SRodney W. Grimes 					object->paging_in_progress--;
144126f9a767SRodney W. Grimes 					if (object->paging_in_progress == 0)
144226f9a767SRodney W. Grimes 						wakeup((caddr_t) object);
144326f9a767SRodney W. Grimes 				} else {
144426f9a767SRodney W. Grimes 					object->paging_in_progress++;
144526f9a767SRodney W. Grimes 					/*
144626f9a767SRodney W. Grimes 					 * grab the shadow objects pager
144726f9a767SRodney W. Grimes 					 */
144826f9a767SRodney W. Grimes 					object->pager = backing_object->pager;
144926f9a767SRodney W. Grimes 					object->paging_offset = backing_object->paging_offset + backing_offset;
145026f9a767SRodney W. Grimes 					vm_object_remove(backing_object->pager);
145126f9a767SRodney W. Grimes 					backing_object->pager = NULL;
145226f9a767SRodney W. Grimes 					/*
145326f9a767SRodney W. Grimes 					 * free unnecessary blocks
145426f9a767SRodney W. Grimes 					 */
145526f9a767SRodney W. Grimes 					swap_pager_freespace(object->pager, 0, object->paging_offset);
145626f9a767SRodney W. Grimes 					object->paging_in_progress--;
145726f9a767SRodney W. Grimes 					if (object->paging_in_progress == 0)
145826f9a767SRodney W. Grimes 						wakeup((caddr_t) object);
1459df8bae1dSRodney W. Grimes 				}
146026f9a767SRodney W. Grimes 				backing_object->paging_in_progress--;
146126f9a767SRodney W. Grimes 				if (backing_object->paging_in_progress == 0)
146226f9a767SRodney W. Grimes 					wakeup((caddr_t) backing_object);
146326f9a767SRodney W. Grimes 			}
1464df8bae1dSRodney W. Grimes 			/*
1465df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
1466df8bae1dSRodney W. Grimes 			 * Note that the reference to backing_object->shadow
1467df8bae1dSRodney W. Grimes 			 * moves from within backing_object to within object.
1468df8bae1dSRodney W. Grimes 			 */
1469df8bae1dSRodney W. Grimes 
14700d94caffSDavid Greenman 			TAILQ_REMOVE(&object->shadow->reverse_shadow_head, object,
14710d94caffSDavid Greenman 			    reverse_shadow_list);
14722fe6e4d7SDavid Greenman 			if (backing_object->shadow)
14730d94caffSDavid Greenman 				TAILQ_REMOVE(&backing_object->shadow->reverse_shadow_head,
14740d94caffSDavid Greenman 				    backing_object, reverse_shadow_list);
1475df8bae1dSRodney W. Grimes 			object->shadow = backing_object->shadow;
14762fe6e4d7SDavid Greenman 			if (object->shadow)
14770d94caffSDavid Greenman 				TAILQ_INSERT_TAIL(&object->shadow->reverse_shadow_head,
14780d94caffSDavid Greenman 				    object, reverse_shadow_list);
14792fe6e4d7SDavid Greenman 
1480df8bae1dSRodney W. Grimes 			object->shadow_offset += backing_object->shadow_offset;
1481df8bae1dSRodney W. Grimes 			if (object->shadow != NULL &&
1482df8bae1dSRodney W. Grimes 			    object->shadow->copy != NULL) {
1483df8bae1dSRodney W. Grimes 				panic("vm_object_collapse: we collapsed a copy-object!");
1484df8bae1dSRodney W. Grimes 			}
1485df8bae1dSRodney W. Grimes 			/*
1486df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1487df8bae1dSRodney W. Grimes 			 *
14880d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
14890d94caffSDavid Greenman 			 * and no object references within it, all that is
14900d94caffSDavid Greenman 			 * necessary is to dispose of it.
1491df8bae1dSRodney W. Grimes 			 */
1492df8bae1dSRodney W. Grimes 
1493df8bae1dSRodney W. Grimes 			vm_object_unlock(backing_object);
1494df8bae1dSRodney W. Grimes 
1495df8bae1dSRodney W. Grimes 			simple_lock(&vm_object_list_lock);
1496df8bae1dSRodney W. Grimes 			TAILQ_REMOVE(&vm_object_list, backing_object,
1497df8bae1dSRodney W. Grimes 			    object_list);
1498df8bae1dSRodney W. Grimes 			vm_object_count--;
1499df8bae1dSRodney W. Grimes 			simple_unlock(&vm_object_list_lock);
1500df8bae1dSRodney W. Grimes 
1501df8bae1dSRodney W. Grimes 			free((caddr_t) backing_object, M_VMOBJ);
1502df8bae1dSRodney W. Grimes 
1503df8bae1dSRodney W. Grimes 			object_collapses++;
15040d94caffSDavid Greenman 		} else {
1505df8bae1dSRodney W. Grimes 			/*
1506df8bae1dSRodney W. Grimes 			 * If all of the pages in the backing object are
15070d94caffSDavid Greenman 			 * shadowed by the parent object, the parent object no
15080d94caffSDavid Greenman 			 * longer has to shadow the backing object; it can
15090d94caffSDavid Greenman 			 * shadow the next one in the chain.
1510df8bae1dSRodney W. Grimes 			 *
15110d94caffSDavid Greenman 			 * The backing object must not be paged out - we'd have
15120d94caffSDavid Greenman 			 * to check all of the paged-out pages, as well.
1513df8bae1dSRodney W. Grimes 			 */
1514df8bae1dSRodney W. Grimes 
1515df8bae1dSRodney W. Grimes 			if (backing_object->pager != NULL) {
1516df8bae1dSRodney W. Grimes 				vm_object_unlock(backing_object);
1517df8bae1dSRodney W. Grimes 				return;
1518df8bae1dSRodney W. Grimes 			}
1519df8bae1dSRodney W. Grimes 			/*
15200d94caffSDavid Greenman 			 * Should have a check for a 'small' number of pages
15210d94caffSDavid Greenman 			 * here.
1522df8bae1dSRodney W. Grimes 			 */
1523df8bae1dSRodney W. Grimes 
152426f9a767SRodney W. Grimes 			for (p = backing_object->memq.tqh_first; p; p = p->listq.tqe_next) {
1525df8bae1dSRodney W. Grimes 				new_offset = (p->offset - backing_offset);
1526df8bae1dSRodney W. Grimes 
1527df8bae1dSRodney W. Grimes 				/*
15280d94caffSDavid Greenman 				 * If the parent has a page here, or if this
15290d94caffSDavid Greenman 				 * page falls outside the parent, keep going.
1530df8bae1dSRodney W. Grimes 				 *
15310d94caffSDavid Greenman 				 * Otherwise, the backing_object must be left in
15320d94caffSDavid Greenman 				 * the chain.
1533df8bae1dSRodney W. Grimes 				 */
1534df8bae1dSRodney W. Grimes 
1535df8bae1dSRodney W. Grimes 				if (p->offset >= backing_offset &&
153626f9a767SRodney W. Grimes 				    new_offset <= size &&
15370d94caffSDavid Greenman 				    ((pp = vm_page_lookup(object, new_offset)) == NULL ||
15380d94caffSDavid Greenman 					!pp->valid) &&
153926f9a767SRodney W. Grimes 				    (!object->pager || !vm_pager_has_page(object->pager, object->paging_offset + new_offset))) {
1540df8bae1dSRodney W. Grimes 					/*
15410d94caffSDavid Greenman 					 * Page still needed. Can't go any
15420d94caffSDavid Greenman 					 * further.
1543df8bae1dSRodney W. Grimes 					 */
1544df8bae1dSRodney W. Grimes 					vm_object_unlock(backing_object);
1545df8bae1dSRodney W. Grimes 					return;
1546df8bae1dSRodney W. Grimes 				}
1547df8bae1dSRodney W. Grimes 			}
1548df8bae1dSRodney W. Grimes 
1549df8bae1dSRodney W. Grimes 			/*
15500d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
15510d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
15520d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1553df8bae1dSRodney W. Grimes 			 */
1554df8bae1dSRodney W. Grimes 
15550d94caffSDavid Greenman 			TAILQ_REMOVE(&object->shadow->reverse_shadow_head,
15560d94caffSDavid Greenman 			    object, reverse_shadow_list);
155726f9a767SRodney W. Grimes 			vm_object_reference(object->shadow = backing_object->shadow);
15582fe6e4d7SDavid Greenman 			if (object->shadow)
15590d94caffSDavid Greenman 				TAILQ_INSERT_TAIL(&object->shadow->reverse_shadow_head,
15600d94caffSDavid Greenman 				    object, reverse_shadow_list);
1561df8bae1dSRodney W. Grimes 			object->shadow_offset += backing_object->shadow_offset;
1562df8bae1dSRodney W. Grimes 
1563df8bae1dSRodney W. Grimes 			/*
15640d94caffSDavid Greenman 			 * Backing object might have had a copy pointer to us.
15650d94caffSDavid Greenman 			 * If it did, clear it.
1566df8bae1dSRodney W. Grimes 			 */
1567df8bae1dSRodney W. Grimes 			if (backing_object->copy == object) {
1568df8bae1dSRodney W. Grimes 				backing_object->copy = NULL;
1569df8bae1dSRodney W. Grimes 			}
15700d94caffSDavid Greenman 			/*
15710d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
15720d94caffSDavid Greenman 			 * its ref_count was at least 2, it will not vanish;
15730d94caffSDavid Greenman 			 * so we don't need to call vm_object_deallocate.
1574df8bae1dSRodney W. Grimes 			 */
157526f9a767SRodney W. Grimes 			if (backing_object->ref_count == 1)
157626f9a767SRodney W. Grimes 				printf("should have called obj deallocate\n");
1577df8bae1dSRodney W. Grimes 			backing_object->ref_count--;
1578df8bae1dSRodney W. Grimes 			vm_object_unlock(backing_object);
1579df8bae1dSRodney W. Grimes 
1580df8bae1dSRodney W. Grimes 			object_bypasses++;
1581df8bae1dSRodney W. Grimes 
1582df8bae1dSRodney W. Grimes 		}
1583df8bae1dSRodney W. Grimes 
1584df8bae1dSRodney W. Grimes 		/*
1585df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1586df8bae1dSRodney W. Grimes 		 */
1587df8bae1dSRodney W. Grimes 	}
1588df8bae1dSRodney W. Grimes }
1589df8bae1dSRodney W. Grimes 
1590df8bae1dSRodney W. Grimes /*
1591df8bae1dSRodney W. Grimes  *	vm_object_page_remove: [internal]
1592df8bae1dSRodney W. Grimes  *
1593df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
1594df8bae1dSRodney W. Grimes  *	object range from the object's list of pages.
1595df8bae1dSRodney W. Grimes  *
1596df8bae1dSRodney W. Grimes  *	The object must be locked.
1597df8bae1dSRodney W. Grimes  */
159826f9a767SRodney W. Grimes void
159926f9a767SRodney W. Grimes vm_object_page_remove(object, start, end)
1600df8bae1dSRodney W. Grimes 	register vm_object_t object;
1601df8bae1dSRodney W. Grimes 	register vm_offset_t start;
1602df8bae1dSRodney W. Grimes 	register vm_offset_t end;
1603df8bae1dSRodney W. Grimes {
1604df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
160526f9a767SRodney W. Grimes 	vm_offset_t size;
160626f9a767SRodney W. Grimes 	int s;
1607df8bae1dSRodney W. Grimes 
1608df8bae1dSRodney W. Grimes 	if (object == NULL)
1609df8bae1dSRodney W. Grimes 		return;
1610df8bae1dSRodney W. Grimes 
16112fe6e4d7SDavid Greenman 	object->paging_in_progress++;
161226f9a767SRodney W. Grimes 	start = trunc_page(start);
161326f9a767SRodney W. Grimes 	end = round_page(end);
161426f9a767SRodney W. Grimes again:
161526f9a767SRodney W. Grimes 	size = end - start;
161626f9a767SRodney W. Grimes 	if (size > 4 * PAGE_SIZE || size >= object->size / 4) {
16170d94caffSDavid Greenman 		for (p = object->memq.tqh_first; p != NULL; p = next) {
1618df8bae1dSRodney W. Grimes 			next = p->listq.tqe_next;
1619df8bae1dSRodney W. Grimes 			if ((start <= p->offset) && (p->offset < end)) {
1620a481f200SDavid Greenman 				s = splhigh();
16210d94caffSDavid Greenman 				if (p->bmapped) {
16220d94caffSDavid Greenman 					splx(s);
16230d94caffSDavid Greenman 					continue;
16240d94caffSDavid Greenman 				}
16250d94caffSDavid Greenman 				if ((p->flags & PG_BUSY) || p->busy) {
162626f9a767SRodney W. Grimes 					p->flags |= PG_WANTED;
162726f9a767SRodney W. Grimes 					tsleep((caddr_t) p, PVM, "vmopar", 0);
1628a481f200SDavid Greenman 					splx(s);
162926f9a767SRodney W. Grimes 					goto again;
163026f9a767SRodney W. Grimes 				}
1631a481f200SDavid Greenman 				splx(s);
1632df8bae1dSRodney W. Grimes 				pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
1633df8bae1dSRodney W. Grimes 				vm_page_lock_queues();
16340d94caffSDavid Greenman 				PAGE_WAKEUP(p);
1635df8bae1dSRodney W. Grimes 				vm_page_free(p);
1636df8bae1dSRodney W. Grimes 				vm_page_unlock_queues();
163726f9a767SRodney W. Grimes 			}
163826f9a767SRodney W. Grimes 		}
163926f9a767SRodney W. Grimes 	} else {
164026f9a767SRodney W. Grimes 		while (size > 0) {
164105f0fdd2SPoul-Henning Kamp 			while ((p = vm_page_lookup(object, start)) != 0) {
1642a481f200SDavid Greenman 				s = splhigh();
16430d94caffSDavid Greenman 				if (p->bmapped) {
16440d94caffSDavid Greenman 					splx(s);
16450d94caffSDavid Greenman 					break;
16460d94caffSDavid Greenman 				}
16470d94caffSDavid Greenman 				if ((p->flags & PG_BUSY) || p->busy) {
164826f9a767SRodney W. Grimes 					p->flags |= PG_WANTED;
164926f9a767SRodney W. Grimes 					tsleep((caddr_t) p, PVM, "vmopar", 0);
1650a481f200SDavid Greenman 					splx(s);
165126f9a767SRodney W. Grimes 					goto again;
165226f9a767SRodney W. Grimes 				}
1653a481f200SDavid Greenman 				splx(s);
165426f9a767SRodney W. Grimes 				pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
165526f9a767SRodney W. Grimes 				vm_page_lock_queues();
16560d94caffSDavid Greenman 				PAGE_WAKEUP(p);
165726f9a767SRodney W. Grimes 				vm_page_free(p);
165826f9a767SRodney W. Grimes 				vm_page_unlock_queues();
165926f9a767SRodney W. Grimes 			}
166026f9a767SRodney W. Grimes 			start += PAGE_SIZE;
166126f9a767SRodney W. Grimes 			size -= PAGE_SIZE;
1662df8bae1dSRodney W. Grimes 		}
1663df8bae1dSRodney W. Grimes 	}
16642fe6e4d7SDavid Greenman 	--object->paging_in_progress;
16652fe6e4d7SDavid Greenman 	if (object->paging_in_progress == 0)
16662fe6e4d7SDavid Greenman 		wakeup((caddr_t) object);
1667df8bae1dSRodney W. Grimes }
1668df8bae1dSRodney W. Grimes 
1669df8bae1dSRodney W. Grimes /*
1670df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1671df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1672df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1673df8bae1dSRodney W. Grimes  *
1674df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1675df8bae1dSRodney W. Grimes  *
1676df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1677df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1678df8bae1dSRodney W. Grimes  *
1679df8bae1dSRodney W. Grimes  *	Parameters:
1680df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1681df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1682df8bae1dSRodney W. Grimes  *		next_object	Second object into coalesce
1683df8bae1dSRodney W. Grimes  *		next_offset	Offset into next_object
1684df8bae1dSRodney W. Grimes  *
1685df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
1686df8bae1dSRodney W. Grimes  *		next_size	Size of reference to next_object
1687df8bae1dSRodney W. Grimes  *
1688df8bae1dSRodney W. Grimes  *	Conditions:
1689df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1690df8bae1dSRodney W. Grimes  */
16910d94caffSDavid Greenman boolean_t
16920d94caffSDavid Greenman vm_object_coalesce(prev_object, next_object,
1693df8bae1dSRodney W. Grimes     prev_offset, next_offset,
1694df8bae1dSRodney W. Grimes     prev_size, next_size)
1695df8bae1dSRodney W. Grimes 	register vm_object_t prev_object;
1696df8bae1dSRodney W. Grimes 	vm_object_t next_object;
1697df8bae1dSRodney W. Grimes 	vm_offset_t prev_offset, next_offset;
1698df8bae1dSRodney W. Grimes 	vm_size_t prev_size, next_size;
1699df8bae1dSRodney W. Grimes {
1700df8bae1dSRodney W. Grimes 	vm_size_t newsize;
1701df8bae1dSRodney W. Grimes 
1702df8bae1dSRodney W. Grimes 	if (next_object != NULL) {
1703df8bae1dSRodney W. Grimes 		return (FALSE);
1704df8bae1dSRodney W. Grimes 	}
1705df8bae1dSRodney W. Grimes 	if (prev_object == NULL) {
1706df8bae1dSRodney W. Grimes 		return (TRUE);
1707df8bae1dSRodney W. Grimes 	}
1708df8bae1dSRodney W. Grimes 	vm_object_lock(prev_object);
1709df8bae1dSRodney W. Grimes 
1710df8bae1dSRodney W. Grimes 	/*
1711df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1712df8bae1dSRodney W. Grimes 	 */
1713df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1714df8bae1dSRodney W. Grimes 
1715df8bae1dSRodney W. Grimes 	/*
17160d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
17170d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
17180d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1719df8bae1dSRodney W. Grimes 	 */
1720df8bae1dSRodney W. Grimes 
1721df8bae1dSRodney W. Grimes 	if (prev_object->ref_count > 1 ||
1722df8bae1dSRodney W. Grimes 	    prev_object->pager != NULL ||
1723df8bae1dSRodney W. Grimes 	    prev_object->shadow != NULL ||
1724df8bae1dSRodney W. Grimes 	    prev_object->copy != NULL) {
1725df8bae1dSRodney W. Grimes 		vm_object_unlock(prev_object);
1726df8bae1dSRodney W. Grimes 		return (FALSE);
1727df8bae1dSRodney W. Grimes 	}
1728df8bae1dSRodney W. Grimes 	/*
17290d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
17300d94caffSDavid Greenman 	 * deallocation.
1731df8bae1dSRodney W. Grimes 	 */
1732df8bae1dSRodney W. Grimes 
1733df8bae1dSRodney W. Grimes 	vm_object_page_remove(prev_object,
1734df8bae1dSRodney W. Grimes 	    prev_offset + prev_size,
1735df8bae1dSRodney W. Grimes 	    prev_offset + prev_size + next_size);
1736df8bae1dSRodney W. Grimes 
1737df8bae1dSRodney W. Grimes 	/*
1738df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1739df8bae1dSRodney W. Grimes 	 */
1740df8bae1dSRodney W. Grimes 	newsize = prev_offset + prev_size + next_size;
1741df8bae1dSRodney W. Grimes 	if (newsize > prev_object->size)
1742df8bae1dSRodney W. Grimes 		prev_object->size = newsize;
1743df8bae1dSRodney W. Grimes 
1744df8bae1dSRodney W. Grimes 	vm_object_unlock(prev_object);
1745df8bae1dSRodney W. Grimes 	return (TRUE);
1746df8bae1dSRodney W. Grimes }
1747df8bae1dSRodney W. Grimes 
1748df8bae1dSRodney W. Grimes /*
174926f9a767SRodney W. Grimes  * returns page after looking up in shadow chain
175026f9a767SRodney W. Grimes  */
175126f9a767SRodney W. Grimes 
175226f9a767SRodney W. Grimes vm_page_t
175326f9a767SRodney W. Grimes vm_object_page_lookup(object, offset)
175426f9a767SRodney W. Grimes 	vm_object_t object;
175526f9a767SRodney W. Grimes 	vm_offset_t offset;
175626f9a767SRodney W. Grimes {
175726f9a767SRodney W. Grimes 	vm_page_t m;
17580d94caffSDavid Greenman 
175926f9a767SRodney W. Grimes 	if (!(m = vm_page_lookup(object, offset))) {
176026f9a767SRodney W. Grimes 		if (!object->shadow)
176126f9a767SRodney W. Grimes 			return 0;
176226f9a767SRodney W. Grimes 		else
176326f9a767SRodney W. Grimes 			return vm_object_page_lookup(object->shadow, offset + object->shadow_offset);
176426f9a767SRodney W. Grimes 	}
176526f9a767SRodney W. Grimes 	return m;
176626f9a767SRodney W. Grimes }
176726f9a767SRodney W. Grimes 
176826f9a767SRodney W. Grimes #define DEBUG
17698a129caeSDavid Greenman #if defined(DEBUG) || defined(DDB)
177026f9a767SRodney W. Grimes /*
1771df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
1772df8bae1dSRodney W. Grimes  */
17730d94caffSDavid Greenman void
17740d94caffSDavid Greenman vm_object_print(object, full)
1775df8bae1dSRodney W. Grimes 	vm_object_t object;
1776df8bae1dSRodney W. Grimes 	boolean_t full;
1777df8bae1dSRodney W. Grimes {
1778df8bae1dSRodney W. Grimes 	register vm_page_t p;
1779df8bae1dSRodney W. Grimes 	extern indent;
1780df8bae1dSRodney W. Grimes 
1781df8bae1dSRodney W. Grimes 	register int count;
1782df8bae1dSRodney W. Grimes 
1783df8bae1dSRodney W. Grimes 	if (object == NULL)
1784df8bae1dSRodney W. Grimes 		return;
1785df8bae1dSRodney W. Grimes 
1786df8bae1dSRodney W. Grimes 	iprintf("Object 0x%x: size=0x%x, res=%d, ref=%d, ",
1787df8bae1dSRodney W. Grimes 	    (int) object, (int) object->size,
1788df8bae1dSRodney W. Grimes 	    object->resident_page_count, object->ref_count);
1789df8bae1dSRodney W. Grimes 	printf("pager=0x%x+0x%x, shadow=(0x%x)+0x%x\n",
1790df8bae1dSRodney W. Grimes 	    (int) object->pager, (int) object->paging_offset,
1791df8bae1dSRodney W. Grimes 	    (int) object->shadow, (int) object->shadow_offset);
179205f0fdd2SPoul-Henning Kamp 	printf("cache: next=%p, prev=%p\n",
1793df8bae1dSRodney W. Grimes 	    object->cached_list.tqe_next, object->cached_list.tqe_prev);
1794df8bae1dSRodney W. Grimes 
1795df8bae1dSRodney W. Grimes 	if (!full)
1796df8bae1dSRodney W. Grimes 		return;
1797df8bae1dSRodney W. Grimes 
1798df8bae1dSRodney W. Grimes 	indent += 2;
1799df8bae1dSRodney W. Grimes 	count = 0;
1800df8bae1dSRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) {
1801df8bae1dSRodney W. Grimes 		if (count == 0)
1802df8bae1dSRodney W. Grimes 			iprintf("memory:=");
1803df8bae1dSRodney W. Grimes 		else if (count == 6) {
1804df8bae1dSRodney W. Grimes 			printf("\n");
1805df8bae1dSRodney W. Grimes 			iprintf(" ...");
1806df8bae1dSRodney W. Grimes 			count = 0;
1807df8bae1dSRodney W. Grimes 		} else
1808df8bae1dSRodney W. Grimes 			printf(",");
1809df8bae1dSRodney W. Grimes 		count++;
1810df8bae1dSRodney W. Grimes 
181105f0fdd2SPoul-Henning Kamp 		printf("(off=0x%lx,page=0x%lx)",
181205f0fdd2SPoul-Henning Kamp 		    (u_long) p->offset, (u_long) VM_PAGE_TO_PHYS(p));
1813df8bae1dSRodney W. Grimes 	}
1814df8bae1dSRodney W. Grimes 	if (count != 0)
1815df8bae1dSRodney W. Grimes 		printf("\n");
1816df8bae1dSRodney W. Grimes 	indent -= 2;
1817df8bae1dSRodney W. Grimes }
18188a129caeSDavid Greenman #endif				/* defined(DEBUG) || defined(DDB) */
1819