xref: /freebsd/sys/vm/vm_object.c (revision 26f9a76710a312a951848542b9ca1f44100450e2)
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  *
36df8bae1dSRodney W. Grimes  *	@(#)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.
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes /*
66df8bae1dSRodney W. Grimes  *	Virtual memory object module.
67df8bae1dSRodney W. Grimes  */
68df8bae1dSRodney W. Grimes 
69df8bae1dSRodney W. Grimes #include <sys/param.h>
70df8bae1dSRodney W. Grimes #include <sys/systm.h>
71df8bae1dSRodney W. Grimes #include <sys/malloc.h>
72df8bae1dSRodney W. Grimes 
73df8bae1dSRodney W. Grimes #include <vm/vm.h>
74df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
7526f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
7626f9a767SRodney W. Grimes 
7726f9a767SRodney W. Grimes static void _vm_object_allocate(vm_size_t, vm_object_t);
7826f9a767SRodney W. Grimes void vm_object_deactivate_pages(vm_object_t);
7926f9a767SRodney W. Grimes void vm_object_cache_trim(void);
8026f9a767SRodney W. Grimes void vm_object_remove(vm_pager_t);
81df8bae1dSRodney W. Grimes 
82df8bae1dSRodney W. Grimes /*
83df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
84df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
85df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
86df8bae1dSRodney W. Grimes  *
87df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
88df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
89df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
90df8bae1dSRodney W. Grimes  *
91df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
92df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
93df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
94df8bae1dSRodney W. Grimes  *	lock.
95df8bae1dSRodney W. Grimes  *
96df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
97df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
98df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
99df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
100df8bae1dSRodney W. Grimes  *
101df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
102df8bae1dSRodney W. Grimes  *	modified after time of creation are:
103df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
104df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
105df8bae1dSRodney W. Grimes  *
106df8bae1dSRodney W. Grimes  */
107df8bae1dSRodney W. Grimes 
10826f9a767SRodney W. Grimes 
109df8bae1dSRodney W. Grimes struct vm_object	kernel_object_store;
110df8bae1dSRodney W. Grimes struct vm_object	kmem_object_store;
111df8bae1dSRodney W. Grimes 
11226f9a767SRodney W. Grimes extern int vm_cache_max;
113df8bae1dSRodney W. Grimes #define	VM_OBJECT_HASH_COUNT	157
114df8bae1dSRodney W. Grimes 
115df8bae1dSRodney W. Grimes struct vm_object_hash_head vm_object_hashtable[VM_OBJECT_HASH_COUNT];
116df8bae1dSRodney W. Grimes 
117df8bae1dSRodney W. Grimes long	object_collapses = 0;
118df8bae1dSRodney W. Grimes long	object_bypasses  = 0;
119df8bae1dSRodney W. Grimes 
120df8bae1dSRodney W. Grimes static void
121df8bae1dSRodney W. Grimes _vm_object_allocate(size, object)
122df8bae1dSRodney W. Grimes 	vm_size_t		size;
123df8bae1dSRodney W. Grimes 	register vm_object_t	object;
124df8bae1dSRodney W. Grimes {
12526f9a767SRodney W. Grimes 	bzero(object, sizeof *object);
126df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
127df8bae1dSRodney W. Grimes 	vm_object_lock_init(object);
128df8bae1dSRodney W. Grimes 	object->ref_count = 1;
129df8bae1dSRodney W. Grimes 	object->resident_page_count = 0;
130df8bae1dSRodney W. Grimes 	object->size = size;
131df8bae1dSRodney W. Grimes 	object->flags = OBJ_INTERNAL;	/* vm_allocate_with_pager will reset */
132df8bae1dSRodney W. Grimes 	object->paging_in_progress = 0;
133df8bae1dSRodney W. Grimes 	object->copy = NULL;
134df8bae1dSRodney W. Grimes 
135df8bae1dSRodney W. Grimes 	/*
136df8bae1dSRodney W. Grimes 	 *	Object starts out read-write, with no pager.
137df8bae1dSRodney W. Grimes 	 */
138df8bae1dSRodney W. Grimes 
139df8bae1dSRodney W. Grimes 	object->pager = NULL;
140df8bae1dSRodney W. Grimes 	object->paging_offset = 0;
141df8bae1dSRodney W. Grimes 	object->shadow = NULL;
142df8bae1dSRodney W. Grimes 	object->shadow_offset = (vm_offset_t) 0;
143df8bae1dSRodney W. Grimes 
144df8bae1dSRodney W. Grimes 	simple_lock(&vm_object_list_lock);
145df8bae1dSRodney W. Grimes 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
146df8bae1dSRodney W. Grimes 	vm_object_count++;
147df8bae1dSRodney W. Grimes 	cnt.v_nzfod += atop(size);
148df8bae1dSRodney W. Grimes 	simple_unlock(&vm_object_list_lock);
149df8bae1dSRodney W. Grimes }
150df8bae1dSRodney W. Grimes 
151df8bae1dSRodney W. Grimes /*
15226f9a767SRodney W. Grimes  *	vm_object_init:
15326f9a767SRodney W. Grimes  *
15426f9a767SRodney W. Grimes  *	Initialize the VM objects module.
15526f9a767SRodney W. Grimes  */
15626f9a767SRodney W. Grimes void
15726f9a767SRodney W. Grimes vm_object_init(vm_offset_t nothing)
15826f9a767SRodney W. Grimes {
15926f9a767SRodney W. Grimes 	register int	i;
16026f9a767SRodney W. Grimes 
16126f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_cached_list);
16226f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
16326f9a767SRodney W. Grimes 	vm_object_count = 0;
16426f9a767SRodney W. Grimes 	simple_lock_init(&vm_cache_lock);
16526f9a767SRodney W. Grimes 	simple_lock_init(&vm_object_list_lock);
16626f9a767SRodney W. Grimes 
16726f9a767SRodney W. Grimes 	for (i = 0; i < VM_OBJECT_HASH_COUNT; i++)
16826f9a767SRodney W. Grimes 		TAILQ_INIT(&vm_object_hashtable[i]);
16926f9a767SRodney W. Grimes 
17026f9a767SRodney W. Grimes 	kernel_object = &kernel_object_store;
17126f9a767SRodney W. Grimes 	_vm_object_allocate(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS,
17226f9a767SRodney W. Grimes 			kernel_object);
17326f9a767SRodney W. Grimes 
17426f9a767SRodney W. Grimes 	kmem_object = &kmem_object_store;
17526f9a767SRodney W. Grimes 	_vm_object_allocate(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS,
17626f9a767SRodney W. Grimes 			kmem_object);
17726f9a767SRodney W. Grimes }
17826f9a767SRodney W. Grimes 
17926f9a767SRodney W. Grimes /*
18026f9a767SRodney W. Grimes  *	vm_object_allocate:
18126f9a767SRodney W. Grimes  *
18226f9a767SRodney W. Grimes  *	Returns a new object with the given size.
18326f9a767SRodney W. Grimes  */
18426f9a767SRodney W. Grimes 
18526f9a767SRodney W. Grimes vm_object_t
18626f9a767SRodney W. Grimes vm_object_allocate(size)
18726f9a767SRodney W. Grimes 	vm_size_t	size;
18826f9a767SRodney W. Grimes {
18926f9a767SRodney W. Grimes 	register vm_object_t	result;
19026f9a767SRodney W. Grimes 	int s;
19126f9a767SRodney W. Grimes 
19226f9a767SRodney W. Grimes 	result = (vm_object_t)
19326f9a767SRodney W. Grimes 		malloc((u_long)sizeof *result, M_VMOBJ, M_WAITOK);
19426f9a767SRodney W. Grimes 
19526f9a767SRodney W. Grimes 
19626f9a767SRodney W. Grimes 	_vm_object_allocate(size, result);
19726f9a767SRodney W. Grimes 
19826f9a767SRodney W. Grimes 	return(result);
19926f9a767SRodney W. Grimes }
20026f9a767SRodney W. Grimes 
20126f9a767SRodney W. Grimes 
20226f9a767SRodney W. Grimes /*
203df8bae1dSRodney W. Grimes  *	vm_object_reference:
204df8bae1dSRodney W. Grimes  *
205df8bae1dSRodney W. Grimes  *	Gets another reference to the given object.
206df8bae1dSRodney W. Grimes  */
20726f9a767SRodney W. Grimes inline void
20826f9a767SRodney W. Grimes vm_object_reference(object)
209df8bae1dSRodney W. Grimes 	register vm_object_t	object;
210df8bae1dSRodney W. Grimes {
211df8bae1dSRodney W. Grimes 	if (object == NULL)
212df8bae1dSRodney W. Grimes 		return;
213df8bae1dSRodney W. Grimes 
214df8bae1dSRodney W. Grimes 	vm_object_lock(object);
215df8bae1dSRodney W. Grimes 	object->ref_count++;
216df8bae1dSRodney W. Grimes 	vm_object_unlock(object);
217df8bae1dSRodney W. Grimes }
218df8bae1dSRodney W. Grimes 
219df8bae1dSRodney W. Grimes /*
220df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
221df8bae1dSRodney W. Grimes  *
222df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
223df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
224df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
225df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
226df8bae1dSRodney W. Grimes  *	may be relinquished.
227df8bae1dSRodney W. Grimes  *
228df8bae1dSRodney W. Grimes  *	No object may be locked.
229df8bae1dSRodney W. Grimes  */
23026f9a767SRodney W. Grimes void
23126f9a767SRodney W. Grimes vm_object_deallocate(object)
23226f9a767SRodney W. Grimes 	vm_object_t	object;
233df8bae1dSRodney W. Grimes {
234df8bae1dSRodney W. Grimes 	vm_object_t	temp;
235df8bae1dSRodney W. Grimes 
236df8bae1dSRodney W. Grimes 	while (object != NULL) {
237df8bae1dSRodney W. Grimes 
238df8bae1dSRodney W. Grimes 		/*
239df8bae1dSRodney W. Grimes 		 *	The cache holds a reference (uncounted) to
240df8bae1dSRodney W. Grimes 		 *	the object; we must lock it before removing
241df8bae1dSRodney W. Grimes 		 *	the object.
242df8bae1dSRodney W. Grimes 		 */
243df8bae1dSRodney W. Grimes 
244df8bae1dSRodney W. Grimes 		vm_object_cache_lock();
245df8bae1dSRodney W. Grimes 
246df8bae1dSRodney W. Grimes 		/*
247df8bae1dSRodney W. Grimes 		 *	Lose the reference
248df8bae1dSRodney W. Grimes 		 */
249df8bae1dSRodney W. Grimes 		vm_object_lock(object);
250df8bae1dSRodney W. Grimes 		if (--(object->ref_count) != 0) {
251df8bae1dSRodney W. Grimes 
25226f9a767SRodney W. Grimes 			vm_object_unlock(object);
253df8bae1dSRodney W. Grimes 			/*
254df8bae1dSRodney W. Grimes 			 *	If there are still references, then
255df8bae1dSRodney W. Grimes 			 *	we are done.
256df8bae1dSRodney W. Grimes 			 */
257df8bae1dSRodney W. Grimes 			vm_object_cache_unlock();
258df8bae1dSRodney W. Grimes 			return;
259df8bae1dSRodney W. Grimes 		}
260df8bae1dSRodney W. Grimes 
261df8bae1dSRodney W. Grimes 		/*
262df8bae1dSRodney W. Grimes 		 *	See if this object can persist.  If so, enter
263df8bae1dSRodney W. Grimes 		 *	it in the cache, then deactivate all of its
264df8bae1dSRodney W. Grimes 		 *	pages.
265df8bae1dSRodney W. Grimes 		 */
266df8bae1dSRodney W. Grimes 
267df8bae1dSRodney W. Grimes 		if (object->flags & OBJ_CANPERSIST) {
268df8bae1dSRodney W. Grimes 
269df8bae1dSRodney W. Grimes 			TAILQ_INSERT_TAIL(&vm_object_cached_list, object,
270df8bae1dSRodney W. Grimes 				cached_list);
271df8bae1dSRodney W. Grimes 			vm_object_cached++;
272df8bae1dSRodney W. Grimes 			vm_object_cache_unlock();
273df8bae1dSRodney W. Grimes 
27426f9a767SRodney W. Grimes /*
27526f9a767SRodney W. Grimes  * this code segment was removed because it kills performance with
27626f9a767SRodney W. Grimes  * large -- repetively used binaries.  The functionality now resides
27726f9a767SRodney W. Grimes  * in the pageout daemon
27826f9a767SRodney W. Grimes  *			vm_object_deactivate_pages(object);
27926f9a767SRodney W. Grimes  */
280df8bae1dSRodney W. Grimes 			vm_object_unlock(object);
281df8bae1dSRodney W. Grimes 
282df8bae1dSRodney W. Grimes 			vm_object_cache_trim();
283df8bae1dSRodney W. Grimes 			return;
284df8bae1dSRodney W. Grimes 		}
285df8bae1dSRodney W. Grimes 
286df8bae1dSRodney W. Grimes 		/*
287df8bae1dSRodney W. Grimes 		 *	Make sure no one can look us up now.
288df8bae1dSRodney W. Grimes 		 */
289df8bae1dSRodney W. Grimes 		vm_object_remove(object->pager);
290df8bae1dSRodney W. Grimes 		vm_object_cache_unlock();
291df8bae1dSRodney W. Grimes 
292df8bae1dSRodney W. Grimes 		temp = object->shadow;
293df8bae1dSRodney W. Grimes 		vm_object_terminate(object);
294df8bae1dSRodney W. Grimes 			/* unlocks and deallocates object */
295df8bae1dSRodney W. Grimes 		object = temp;
296df8bae1dSRodney W. Grimes 	}
297df8bae1dSRodney W. Grimes }
298df8bae1dSRodney W. Grimes 
299df8bae1dSRodney W. Grimes /*
300df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
301df8bae1dSRodney W. Grimes  *	up all previously used resources.
302df8bae1dSRodney W. Grimes  *
303df8bae1dSRodney W. Grimes  *	The object must be locked.
304df8bae1dSRodney W. Grimes  */
30526f9a767SRodney W. Grimes void
30626f9a767SRodney W. Grimes vm_object_terminate(object)
307df8bae1dSRodney W. Grimes 	register vm_object_t	object;
308df8bae1dSRodney W. Grimes {
309df8bae1dSRodney W. Grimes 	register vm_page_t	p;
310df8bae1dSRodney W. Grimes 	vm_object_t		shadow_object;
31126f9a767SRodney W. Grimes 	int s;
312df8bae1dSRodney W. Grimes 
313df8bae1dSRodney W. Grimes 	/*
314df8bae1dSRodney W. Grimes 	 *	Detach the object from its shadow if we are the shadow's
315df8bae1dSRodney W. Grimes 	 *	copy.
316df8bae1dSRodney W. Grimes 	 */
317df8bae1dSRodney W. Grimes 	if ((shadow_object = object->shadow) != NULL) {
318df8bae1dSRodney W. Grimes 		vm_object_lock(shadow_object);
319df8bae1dSRodney W. Grimes 		if (shadow_object->copy == object)
320df8bae1dSRodney W. Grimes 			shadow_object->copy = NULL;
32126f9a767SRodney W. Grimes /*
322df8bae1dSRodney W. Grimes 		else if (shadow_object->copy != NULL)
323df8bae1dSRodney W. Grimes 			panic("vm_object_terminate: copy/shadow inconsistency");
32426f9a767SRodney W. Grimes */
325df8bae1dSRodney W. Grimes 		vm_object_unlock(shadow_object);
326df8bae1dSRodney W. Grimes 	}
327df8bae1dSRodney W. Grimes 
328df8bae1dSRodney W. Grimes 	/*
32926f9a767SRodney W. Grimes 	 *	Wait until the pageout daemon is through
33026f9a767SRodney W. Grimes 	 *	with the object.
331df8bae1dSRodney W. Grimes 	 */
33226f9a767SRodney W. Grimes 
333df8bae1dSRodney W. Grimes 	while (object->paging_in_progress) {
334df8bae1dSRodney W. Grimes 		vm_object_sleep((int)object, object, FALSE);
335df8bae1dSRodney W. Grimes 		vm_object_lock(object);
336df8bae1dSRodney W. Grimes 	}
337df8bae1dSRodney W. Grimes 
338df8bae1dSRodney W. Grimes 	/*
33926f9a767SRodney W. Grimes 	 *	While the paging system is locked,
34026f9a767SRodney W. Grimes 	 *	pull the object's pages off the active
34126f9a767SRodney W. Grimes 	 *	and inactive queues.  This keeps the
34226f9a767SRodney W. Grimes 	 *	pageout daemon from playing with them
34326f9a767SRodney W. Grimes 	 *	during vm_pager_deallocate.
344df8bae1dSRodney W. Grimes 	 *
34526f9a767SRodney W. Grimes 	 *	We can't free the pages yet, because the
34626f9a767SRodney W. Grimes 	 *	object's pager may have to write them out
34726f9a767SRodney W. Grimes 	 *	before deallocating the paging space.
348df8bae1dSRodney W. Grimes 	 */
34926f9a767SRodney W. Grimes 
35026f9a767SRodney W. Grimes 	for( p = object->memq.tqh_first; p; p=p->listq.tqe_next) {
35126f9a767SRodney W. Grimes 		VM_PAGE_CHECK(p);
35226f9a767SRodney W. Grimes 
35326f9a767SRodney W. Grimes 		vm_page_lock_queues();
35426f9a767SRodney W. Grimes 		s = splimp();
35526f9a767SRodney W. Grimes 		if (p->flags & PG_ACTIVE) {
35626f9a767SRodney W. Grimes 			TAILQ_REMOVE(&vm_page_queue_active, p, pageq);
35726f9a767SRodney W. Grimes 			p->flags &= ~PG_ACTIVE;
35826f9a767SRodney W. Grimes 			cnt.v_active_count--;
35926f9a767SRodney W. Grimes 		}
36026f9a767SRodney W. Grimes 
36126f9a767SRodney W. Grimes 		if (p->flags & PG_INACTIVE) {
36226f9a767SRodney W. Grimes 			TAILQ_REMOVE(&vm_page_queue_inactive, p, pageq);
36326f9a767SRodney W. Grimes 			p->flags &= ~PG_INACTIVE;
36426f9a767SRodney W. Grimes 			cnt.v_inactive_count--;
36526f9a767SRodney W. Grimes 		}
36626f9a767SRodney W. Grimes 		splx(s);
36726f9a767SRodney W. Grimes 		vm_page_unlock_queues();
36826f9a767SRodney W. Grimes 	}
36926f9a767SRodney W. Grimes 
37026f9a767SRodney W. Grimes 	vm_object_unlock(object);
37126f9a767SRodney W. Grimes 
37226f9a767SRodney W. Grimes 	if (object->paging_in_progress != 0)
37326f9a767SRodney W. Grimes 		panic("vm_object_deallocate: pageout in progress");
37426f9a767SRodney W. Grimes 
37526f9a767SRodney W. Grimes 	/*
37626f9a767SRodney W. Grimes 	 *	Clean and free the pages, as appropriate.
37726f9a767SRodney W. Grimes 	 *	All references to the object are gone,
37826f9a767SRodney W. Grimes 	 *	so we don't need to lock it.
37926f9a767SRodney W. Grimes 	 */
38026f9a767SRodney W. Grimes 
381df8bae1dSRodney W. Grimes 	if ((object->flags & OBJ_INTERNAL) == 0) {
38226f9a767SRodney W. Grimes 		vm_object_lock(object);
383df8bae1dSRodney W. Grimes 		(void) vm_object_page_clean(object, 0, 0, TRUE, TRUE);
384df8bae1dSRodney W. Grimes 		vm_object_unlock(object);
385df8bae1dSRodney W. Grimes 	}
386df8bae1dSRodney W. Grimes 
387df8bae1dSRodney W. Grimes 	/*
388df8bae1dSRodney W. Grimes 	 * Now free the pages.
389df8bae1dSRodney W. Grimes 	 * For internal objects, this also removes them from paging queues.
390df8bae1dSRodney W. Grimes 	 */
391df8bae1dSRodney W. Grimes 	while ((p = object->memq.tqh_first) != NULL) {
392df8bae1dSRodney W. Grimes 		VM_PAGE_CHECK(p);
393df8bae1dSRodney W. Grimes 		vm_page_lock_queues();
394df8bae1dSRodney W. Grimes 		vm_page_free(p);
395df8bae1dSRodney W. Grimes 		cnt.v_pfree++;
396df8bae1dSRodney W. Grimes 		vm_page_unlock_queues();
397df8bae1dSRodney W. Grimes 	}
398df8bae1dSRodney W. Grimes 
399df8bae1dSRodney W. Grimes 	/*
400df8bae1dSRodney W. Grimes 	 *	Let the pager know object is dead.
401df8bae1dSRodney W. Grimes 	 */
40226f9a767SRodney W. Grimes 
403df8bae1dSRodney W. Grimes 	if (object->pager != NULL)
404df8bae1dSRodney W. Grimes 		vm_pager_deallocate(object->pager);
405df8bae1dSRodney W. Grimes 
40626f9a767SRodney W. Grimes 
407df8bae1dSRodney W. Grimes 	simple_lock(&vm_object_list_lock);
408df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
409df8bae1dSRodney W. Grimes 	vm_object_count--;
410df8bae1dSRodney W. Grimes 	simple_unlock(&vm_object_list_lock);
411df8bae1dSRodney W. Grimes 
412df8bae1dSRodney W. Grimes 	/*
413df8bae1dSRodney W. Grimes 	 *	Free the space for the object.
414df8bae1dSRodney W. Grimes 	 */
41526f9a767SRodney W. Grimes 
416df8bae1dSRodney W. Grimes 	free((caddr_t)object, M_VMOBJ);
417df8bae1dSRodney W. Grimes }
418df8bae1dSRodney W. Grimes 
419df8bae1dSRodney W. Grimes /*
420df8bae1dSRodney W. Grimes  *	vm_object_page_clean
421df8bae1dSRodney W. Grimes  *
422df8bae1dSRodney W. Grimes  *	Clean all dirty pages in the specified range of object.
42326f9a767SRodney W. Grimes  *	Leaves page on whatever queue it is currently on.
42426f9a767SRodney W. Grimes  *
42526f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
42626f9a767SRodney W. Grimes  *
42726f9a767SRodney W. Grimes  *	The object must be locked.
42826f9a767SRodney W. Grimes  */
42926f9a767SRodney W. Grimes #if 1
43026f9a767SRodney W. Grimes boolean_t
43126f9a767SRodney W. Grimes vm_object_page_clean(object, start, end, syncio, de_queue)
43226f9a767SRodney W. Grimes 	register vm_object_t	object;
43326f9a767SRodney W. Grimes 	register vm_offset_t	start;
43426f9a767SRodney W. Grimes 	register vm_offset_t	end;
43526f9a767SRodney W. Grimes 	boolean_t		syncio;
43626f9a767SRodney W. Grimes 	boolean_t		de_queue;
43726f9a767SRodney W. Grimes {
43826f9a767SRodney W. Grimes 	register vm_page_t	p, nextp;
43926f9a767SRodney W. Grimes 	int s;
44026f9a767SRodney W. Grimes 	int size;
44126f9a767SRodney W. Grimes 
44226f9a767SRodney W. Grimes 	if (object->pager == NULL)
44326f9a767SRodney W. Grimes 		return 1;
44426f9a767SRodney W. Grimes 
44526f9a767SRodney W. Grimes 	if (start != end) {
44626f9a767SRodney W. Grimes 		start = trunc_page(start);
44726f9a767SRodney W. Grimes 		end = round_page(end);
44826f9a767SRodney W. Grimes 	}
44926f9a767SRodney W. Grimes 	size = end - start;
45026f9a767SRodney W. Grimes 
45126f9a767SRodney W. Grimes again:
45226f9a767SRodney W. Grimes 	/*
45326f9a767SRodney W. Grimes 	 * Wait until the pageout daemon is through with the object.
45426f9a767SRodney W. Grimes 	 */
45526f9a767SRodney W. Grimes 	while (object->paging_in_progress) {
45626f9a767SRodney W. Grimes 		vm_object_sleep((int)object, object, FALSE);
45726f9a767SRodney W. Grimes 	}
45826f9a767SRodney W. Grimes 
45926f9a767SRodney W. Grimes 	nextp = object->memq.tqh_first;
46026f9a767SRodney W. Grimes 	while ( (p = nextp) && ((start == end) || (size != 0) ) ) {
46126f9a767SRodney W. Grimes 		nextp = p->listq.tqe_next;
46226f9a767SRodney W. Grimes 		if (start == end || (p->offset >= start && p->offset < end)) {
46326f9a767SRodney W. Grimes 			if (p->flags & PG_BUSY)
46426f9a767SRodney W. Grimes 				continue;
46526f9a767SRodney W. Grimes 
46626f9a767SRodney W. Grimes 			size -= PAGE_SIZE;
46726f9a767SRodney W. Grimes 
46826f9a767SRodney W. Grimes 			if ((p->flags & PG_CLEAN)
46926f9a767SRodney W. Grimes 				 && pmap_is_modified(VM_PAGE_TO_PHYS(p)))
47026f9a767SRodney W. Grimes 				p->flags &= ~PG_CLEAN;
47126f9a767SRodney W. Grimes 
47226f9a767SRodney W. Grimes 			if ((p->flags & PG_CLEAN) == 0) {
47326f9a767SRodney W. Grimes 				vm_pageout_clean(p,VM_PAGEOUT_FORCE);
47426f9a767SRodney W. Grimes 				goto again;
47526f9a767SRodney W. Grimes 			}
47626f9a767SRodney W. Grimes 		}
47726f9a767SRodney W. Grimes 	}
47826f9a767SRodney W. Grimes 	wakeup((caddr_t)object);
47926f9a767SRodney W. Grimes 	return 1;
48026f9a767SRodney W. Grimes }
48126f9a767SRodney W. Grimes #endif
48226f9a767SRodney W. Grimes /*
48326f9a767SRodney W. Grimes  *	vm_object_page_clean
48426f9a767SRodney W. Grimes  *
48526f9a767SRodney W. Grimes  *	Clean all dirty pages in the specified range of object.
486df8bae1dSRodney W. Grimes  *	If syncio is TRUE, page cleaning is done synchronously.
487df8bae1dSRodney W. Grimes  *	If de_queue is TRUE, pages are removed from any paging queue
488df8bae1dSRodney W. Grimes  *	they were on, otherwise they are left on whatever queue they
489df8bae1dSRodney W. Grimes  *	were on before the cleaning operation began.
490df8bae1dSRodney W. Grimes  *
491df8bae1dSRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
492df8bae1dSRodney W. Grimes  *
493df8bae1dSRodney W. Grimes  *	The object must be locked.
494df8bae1dSRodney W. Grimes  *
495df8bae1dSRodney W. Grimes  *	Returns TRUE if all was well, FALSE if there was a pager error
496df8bae1dSRodney W. Grimes  *	somewhere.  We attempt to clean (and dequeue) all pages regardless
497df8bae1dSRodney W. Grimes  *	of where an error occurs.
498df8bae1dSRodney W. Grimes  */
49926f9a767SRodney W. Grimes #if 0
500df8bae1dSRodney W. Grimes boolean_t
501df8bae1dSRodney W. Grimes vm_object_page_clean(object, start, end, syncio, de_queue)
502df8bae1dSRodney W. Grimes 	register vm_object_t	object;
503df8bae1dSRodney W. Grimes 	register vm_offset_t	start;
504df8bae1dSRodney W. Grimes 	register vm_offset_t	end;
505df8bae1dSRodney W. Grimes 	boolean_t		syncio;
506df8bae1dSRodney W. Grimes 	boolean_t		de_queue;
507df8bae1dSRodney W. Grimes {
508df8bae1dSRodney W. Grimes 	register vm_page_t	p;
509df8bae1dSRodney W. Grimes 	int onqueue;
510df8bae1dSRodney W. Grimes 	boolean_t noerror = TRUE;
511df8bae1dSRodney W. Grimes 
512df8bae1dSRodney W. Grimes 	if (object == NULL)
513df8bae1dSRodney W. Grimes 		return (TRUE);
514df8bae1dSRodney W. Grimes 
515df8bae1dSRodney W. Grimes 	/*
516df8bae1dSRodney W. Grimes 	 * If it is an internal object and there is no pager, attempt to
517df8bae1dSRodney W. Grimes 	 * allocate one.  Note that vm_object_collapse may relocate one
518df8bae1dSRodney W. Grimes 	 * from a collapsed object so we must recheck afterward.
519df8bae1dSRodney W. Grimes 	 */
520df8bae1dSRodney W. Grimes 	if ((object->flags & OBJ_INTERNAL) && object->pager == NULL) {
521df8bae1dSRodney W. Grimes 		vm_object_collapse(object);
522df8bae1dSRodney W. Grimes 		if (object->pager == NULL) {
523df8bae1dSRodney W. Grimes 			vm_pager_t pager;
524df8bae1dSRodney W. Grimes 
525df8bae1dSRodney W. Grimes 			vm_object_unlock(object);
526df8bae1dSRodney W. Grimes 			pager = vm_pager_allocate(PG_DFLT, (caddr_t)0,
527df8bae1dSRodney W. Grimes 						  object->size, VM_PROT_ALL,
528df8bae1dSRodney W. Grimes 						  (vm_offset_t)0);
529df8bae1dSRodney W. Grimes 			if (pager)
530df8bae1dSRodney W. Grimes 				vm_object_setpager(object, pager, 0, FALSE);
531df8bae1dSRodney W. Grimes 			vm_object_lock(object);
532df8bae1dSRodney W. Grimes 		}
533df8bae1dSRodney W. Grimes 	}
534df8bae1dSRodney W. Grimes 	if (object->pager == NULL)
535df8bae1dSRodney W. Grimes 		return (FALSE);
536df8bae1dSRodney W. Grimes 
537df8bae1dSRodney W. Grimes again:
538df8bae1dSRodney W. Grimes 	/*
539df8bae1dSRodney W. Grimes 	 * Wait until the pageout daemon is through with the object.
540df8bae1dSRodney W. Grimes 	 */
541df8bae1dSRodney W. Grimes 	while (object->paging_in_progress) {
542df8bae1dSRodney W. Grimes 		vm_object_sleep((int)object, object, FALSE);
543df8bae1dSRodney W. Grimes 		vm_object_lock(object);
544df8bae1dSRodney W. Grimes 	}
545df8bae1dSRodney W. Grimes 	/*
546df8bae1dSRodney W. Grimes 	 * Loop through the object page list cleaning as necessary.
547df8bae1dSRodney W. Grimes 	 */
548df8bae1dSRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) {
54926f9a767SRodney W. Grimes 		onqueue = 0;
550df8bae1dSRodney W. Grimes 		if ((start == end || p->offset >= start && p->offset < end) &&
551df8bae1dSRodney W. Grimes 		    !(p->flags & PG_FICTITIOUS)) {
552df8bae1dSRodney W. Grimes 			if ((p->flags & PG_CLEAN) &&
553df8bae1dSRodney W. Grimes 			    pmap_is_modified(VM_PAGE_TO_PHYS(p)))
554df8bae1dSRodney W. Grimes 				p->flags &= ~PG_CLEAN;
555df8bae1dSRodney W. Grimes 			/*
556df8bae1dSRodney W. Grimes 			 * Remove the page from any paging queue.
557df8bae1dSRodney W. Grimes 			 * This needs to be done if either we have been
558df8bae1dSRodney W. Grimes 			 * explicitly asked to do so or it is about to
559df8bae1dSRodney W. Grimes 			 * be cleaned (see comment below).
560df8bae1dSRodney W. Grimes 			 */
561df8bae1dSRodney W. Grimes 			if (de_queue || !(p->flags & PG_CLEAN)) {
562df8bae1dSRodney W. Grimes 				vm_page_lock_queues();
563df8bae1dSRodney W. Grimes 				if (p->flags & PG_ACTIVE) {
564df8bae1dSRodney W. Grimes 					TAILQ_REMOVE(&vm_page_queue_active,
565df8bae1dSRodney W. Grimes 						     p, pageq);
566df8bae1dSRodney W. Grimes 					p->flags &= ~PG_ACTIVE;
567df8bae1dSRodney W. Grimes 					cnt.v_active_count--;
568df8bae1dSRodney W. Grimes 					onqueue = 1;
569df8bae1dSRodney W. Grimes 				} else if (p->flags & PG_INACTIVE) {
570df8bae1dSRodney W. Grimes 					TAILQ_REMOVE(&vm_page_queue_inactive,
571df8bae1dSRodney W. Grimes 						     p, pageq);
572df8bae1dSRodney W. Grimes 					p->flags &= ~PG_INACTIVE;
573df8bae1dSRodney W. Grimes 					cnt.v_inactive_count--;
574df8bae1dSRodney W. Grimes 					onqueue = -1;
575df8bae1dSRodney W. Grimes 				} else
576df8bae1dSRodney W. Grimes 					onqueue = 0;
577df8bae1dSRodney W. Grimes 				vm_page_unlock_queues();
578df8bae1dSRodney W. Grimes 			}
579df8bae1dSRodney W. Grimes 			/*
580df8bae1dSRodney W. Grimes 			 * To ensure the state of the page doesn't change
581df8bae1dSRodney W. Grimes 			 * during the clean operation we do two things.
582df8bae1dSRodney W. Grimes 			 * First we set the busy bit and write-protect all
583df8bae1dSRodney W. Grimes 			 * mappings to ensure that write accesses to the
584df8bae1dSRodney W. Grimes 			 * page block (in vm_fault).  Second, we remove
585df8bae1dSRodney W. Grimes 			 * the page from any paging queue to foil the
586df8bae1dSRodney W. Grimes 			 * pageout daemon (vm_pageout_scan).
587df8bae1dSRodney W. Grimes 			 */
588df8bae1dSRodney W. Grimes 			pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_READ);
589df8bae1dSRodney W. Grimes 			if (!(p->flags & PG_CLEAN)) {
590df8bae1dSRodney W. Grimes 				p->flags |= PG_BUSY;
591df8bae1dSRodney W. Grimes 				object->paging_in_progress++;
592df8bae1dSRodney W. Grimes 				vm_object_unlock(object);
593df8bae1dSRodney W. Grimes 				/*
594df8bae1dSRodney W. Grimes 				 * XXX if put fails we mark the page as
595df8bae1dSRodney W. Grimes 				 * clean to avoid an infinite loop.
596df8bae1dSRodney W. Grimes 				 * Will loose changes to the page.
597df8bae1dSRodney W. Grimes 				 */
598df8bae1dSRodney W. Grimes 				if (vm_pager_put(object->pager, p, syncio)) {
599df8bae1dSRodney W. Grimes 					printf("%s: pager_put error\n",
600df8bae1dSRodney W. Grimes 					       "vm_object_page_clean");
601df8bae1dSRodney W. Grimes 					p->flags |= PG_CLEAN;
602df8bae1dSRodney W. Grimes 					noerror = FALSE;
603df8bae1dSRodney W. Grimes 				}
604df8bae1dSRodney W. Grimes 				vm_object_lock(object);
605df8bae1dSRodney W. Grimes 				object->paging_in_progress--;
606df8bae1dSRodney W. Grimes 				if (!de_queue && onqueue) {
607df8bae1dSRodney W. Grimes 					vm_page_lock_queues();
608df8bae1dSRodney W. Grimes 					if (onqueue > 0)
609df8bae1dSRodney W. Grimes 						vm_page_activate(p);
610df8bae1dSRodney W. Grimes 					else
611df8bae1dSRodney W. Grimes 						vm_page_deactivate(p);
612df8bae1dSRodney W. Grimes 					vm_page_unlock_queues();
613df8bae1dSRodney W. Grimes 				}
614df8bae1dSRodney W. Grimes 				p->flags &= ~PG_BUSY;
615df8bae1dSRodney W. Grimes 				PAGE_WAKEUP(p);
616df8bae1dSRodney W. Grimes 				goto again;
617df8bae1dSRodney W. Grimes 			}
618df8bae1dSRodney W. Grimes 		}
619df8bae1dSRodney W. Grimes 	}
620df8bae1dSRodney W. Grimes 	return (noerror);
621df8bae1dSRodney W. Grimes }
62226f9a767SRodney W. Grimes #endif
623df8bae1dSRodney W. Grimes 
624df8bae1dSRodney W. Grimes /*
625df8bae1dSRodney W. Grimes  *	vm_object_deactivate_pages
626df8bae1dSRodney W. Grimes  *
627df8bae1dSRodney W. Grimes  *	Deactivate all pages in the specified object.  (Keep its pages
628df8bae1dSRodney W. Grimes  *	in memory even though it is no longer referenced.)
629df8bae1dSRodney W. Grimes  *
630df8bae1dSRodney W. Grimes  *	The object must be locked.
631df8bae1dSRodney W. Grimes  */
632df8bae1dSRodney W. Grimes void
633df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object)
634df8bae1dSRodney W. Grimes 	register vm_object_t	object;
635df8bae1dSRodney W. Grimes {
636df8bae1dSRodney W. Grimes 	register vm_page_t	p, next;
637df8bae1dSRodney W. Grimes 
638df8bae1dSRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = next) {
639df8bae1dSRodney W. Grimes 		next = p->listq.tqe_next;
640df8bae1dSRodney W. Grimes 		vm_page_lock_queues();
641df8bae1dSRodney W. Grimes 		vm_page_deactivate(p);
642df8bae1dSRodney W. Grimes 		vm_page_unlock_queues();
643df8bae1dSRodney W. Grimes 	}
644df8bae1dSRodney W. Grimes }
645df8bae1dSRodney W. Grimes 
646df8bae1dSRodney W. Grimes /*
647df8bae1dSRodney W. Grimes  *	Trim the object cache to size.
648df8bae1dSRodney W. Grimes  */
649df8bae1dSRodney W. Grimes void
650df8bae1dSRodney W. Grimes vm_object_cache_trim()
651df8bae1dSRodney W. Grimes {
652df8bae1dSRodney W. Grimes 	register vm_object_t	object;
653df8bae1dSRodney W. Grimes 
654df8bae1dSRodney W. Grimes 	vm_object_cache_lock();
655df8bae1dSRodney W. Grimes 	while (vm_object_cached > vm_cache_max) {
656df8bae1dSRodney W. Grimes 		object = vm_object_cached_list.tqh_first;
657df8bae1dSRodney W. Grimes 		vm_object_cache_unlock();
658df8bae1dSRodney W. Grimes 
659df8bae1dSRodney W. Grimes 		if (object != vm_object_lookup(object->pager))
660df8bae1dSRodney W. Grimes 			panic("vm_object_deactivate: I'm sooo confused.");
661df8bae1dSRodney W. Grimes 
662df8bae1dSRodney W. Grimes 		pager_cache(object, FALSE);
663df8bae1dSRodney W. Grimes 
664df8bae1dSRodney W. Grimes 		vm_object_cache_lock();
665df8bae1dSRodney W. Grimes 	}
666df8bae1dSRodney W. Grimes 	vm_object_cache_unlock();
667df8bae1dSRodney W. Grimes }
668df8bae1dSRodney W. Grimes 
66926f9a767SRodney W. Grimes 
670df8bae1dSRodney W. Grimes /*
671df8bae1dSRodney W. Grimes  *	vm_object_pmap_copy:
672df8bae1dSRodney W. Grimes  *
673df8bae1dSRodney W. Grimes  *	Makes all physical pages in the specified
674df8bae1dSRodney W. Grimes  *	object range copy-on-write.  No writeable
675df8bae1dSRodney W. Grimes  *	references to these pages should remain.
676df8bae1dSRodney W. Grimes  *
677df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
678df8bae1dSRodney W. Grimes  */
679df8bae1dSRodney W. Grimes void vm_object_pmap_copy(object, start, end)
680df8bae1dSRodney W. Grimes 	register vm_object_t	object;
681df8bae1dSRodney W. Grimes 	register vm_offset_t	start;
682df8bae1dSRodney W. Grimes 	register vm_offset_t	end;
683df8bae1dSRodney W. Grimes {
684df8bae1dSRodney W. Grimes 	register vm_page_t	p;
685df8bae1dSRodney W. Grimes 
686df8bae1dSRodney W. Grimes 	if (object == NULL)
687df8bae1dSRodney W. Grimes 		return;
688df8bae1dSRodney W. Grimes 
689df8bae1dSRodney W. Grimes 	vm_object_lock(object);
690df8bae1dSRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) {
691df8bae1dSRodney W. Grimes 		if ((start <= p->offset) && (p->offset < end)) {
692df8bae1dSRodney W. Grimes 			pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_READ);
693df8bae1dSRodney W. Grimes 			p->flags |= PG_COPYONWRITE;
694df8bae1dSRodney W. Grimes 		}
695df8bae1dSRodney W. Grimes 	}
696df8bae1dSRodney W. Grimes 	vm_object_unlock(object);
697df8bae1dSRodney W. Grimes }
698df8bae1dSRodney W. Grimes 
699df8bae1dSRodney W. Grimes /*
700df8bae1dSRodney W. Grimes  *	vm_object_pmap_remove:
701df8bae1dSRodney W. Grimes  *
702df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
703df8bae1dSRodney W. Grimes  *	object range from all physical maps.
704df8bae1dSRodney W. Grimes  *
705df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
706df8bae1dSRodney W. Grimes  */
70726f9a767SRodney W. Grimes void
70826f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end)
709df8bae1dSRodney W. Grimes 	register vm_object_t	object;
710df8bae1dSRodney W. Grimes 	register vm_offset_t	start;
711df8bae1dSRodney W. Grimes 	register vm_offset_t	end;
712df8bae1dSRodney W. Grimes {
713df8bae1dSRodney W. Grimes 	register vm_page_t	p;
714df8bae1dSRodney W. Grimes 
715df8bae1dSRodney W. Grimes 	if (object == NULL)
716df8bae1dSRodney W. Grimes 		return;
717df8bae1dSRodney W. Grimes 
718df8bae1dSRodney W. Grimes 	vm_object_lock(object);
71926f9a767SRodney W. Grimes again:
72026f9a767SRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) {
72126f9a767SRodney W. Grimes 		if ((start <= p->offset) && (p->offset < end)) {
72226f9a767SRodney W. Grimes 			if (p->flags & PG_BUSY) {
72326f9a767SRodney W. Grimes 				p->flags |= PG_WANTED;
72426f9a767SRodney W. Grimes 				tsleep((caddr_t) p, PVM, "vmopmr", 0);
72526f9a767SRodney W. Grimes 				goto again;
72626f9a767SRodney W. Grimes 			}
727df8bae1dSRodney W. Grimes 			pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
72826f9a767SRodney W. Grimes 			if ((p->flags & PG_CLEAN) == 0)
72926f9a767SRodney W. Grimes 				p->flags |= PG_LAUNDRY;
73026f9a767SRodney W. Grimes 		}
73126f9a767SRodney W. Grimes 	}
732df8bae1dSRodney W. Grimes 	vm_object_unlock(object);
733df8bae1dSRodney W. Grimes }
734df8bae1dSRodney W. Grimes 
735df8bae1dSRodney W. Grimes /*
736df8bae1dSRodney W. Grimes  *	vm_object_copy:
737df8bae1dSRodney W. Grimes  *
738df8bae1dSRodney W. Grimes  *	Create a new object which is a copy of an existing
739df8bae1dSRodney W. Grimes  *	object, and mark all of the pages in the existing
740df8bae1dSRodney W. Grimes  *	object 'copy-on-write'.  The new object has one reference.
741df8bae1dSRodney W. Grimes  *	Returns the new object.
742df8bae1dSRodney W. Grimes  *
743df8bae1dSRodney W. Grimes  *	May defer the copy until later if the object is not backed
744df8bae1dSRodney W. Grimes  *	up by a non-default pager.
745df8bae1dSRodney W. Grimes  */
746df8bae1dSRodney W. Grimes void vm_object_copy(src_object, src_offset, size,
747df8bae1dSRodney W. Grimes 		    dst_object, dst_offset, src_needs_copy)
748df8bae1dSRodney W. Grimes 	register vm_object_t	src_object;
749df8bae1dSRodney W. Grimes 	vm_offset_t		src_offset;
750df8bae1dSRodney W. Grimes 	vm_size_t		size;
751df8bae1dSRodney W. Grimes 	vm_object_t		*dst_object;	/* OUT */
752df8bae1dSRodney W. Grimes 	vm_offset_t		*dst_offset;	/* OUT */
753df8bae1dSRodney W. Grimes 	boolean_t		*src_needs_copy;	/* OUT */
754df8bae1dSRodney W. Grimes {
755df8bae1dSRodney W. Grimes 	register vm_object_t	new_copy;
756df8bae1dSRodney W. Grimes 	register vm_object_t	old_copy;
757df8bae1dSRodney W. Grimes 	vm_offset_t		new_start, new_end;
758df8bae1dSRodney W. Grimes 
759df8bae1dSRodney W. Grimes 	register vm_page_t	p;
760df8bae1dSRodney W. Grimes 
761df8bae1dSRodney W. Grimes 	if (src_object == NULL) {
762df8bae1dSRodney W. Grimes 		/*
763df8bae1dSRodney W. Grimes 		 *	Nothing to copy
764df8bae1dSRodney W. Grimes 		 */
765df8bae1dSRodney W. Grimes 		*dst_object = NULL;
766df8bae1dSRodney W. Grimes 		*dst_offset = 0;
767df8bae1dSRodney W. Grimes 		*src_needs_copy = FALSE;
768df8bae1dSRodney W. Grimes 		return;
769df8bae1dSRodney W. Grimes 	}
770df8bae1dSRodney W. Grimes 
77126f9a767SRodney W. Grimes 
772df8bae1dSRodney W. Grimes 	/*
773df8bae1dSRodney W. Grimes 	 *	If the object's pager is null_pager or the
774df8bae1dSRodney W. Grimes 	 *	default pager, we don't have to make a copy
775df8bae1dSRodney W. Grimes 	 *	of it.  Instead, we set the needs copy flag and
776df8bae1dSRodney W. Grimes 	 *	make a shadow later.
777df8bae1dSRodney W. Grimes 	 */
778df8bae1dSRodney W. Grimes 
779df8bae1dSRodney W. Grimes 	vm_object_lock(src_object);
78026f9a767SRodney W. Grimes 
78126f9a767SRodney W. Grimes 	/*
78226f9a767SRodney W. Grimes 	 *	Try to collapse the object before copying it.
78326f9a767SRodney W. Grimes 	 */
78426f9a767SRodney W. Grimes 
78526f9a767SRodney W. Grimes 	vm_object_collapse(src_object);
78626f9a767SRodney W. Grimes 
787df8bae1dSRodney W. Grimes 	if (src_object->pager == NULL ||
78826f9a767SRodney W. Grimes 	    src_object->pager->pg_type == PG_SWAP ||
789df8bae1dSRodney W. Grimes 	    (src_object->flags & OBJ_INTERNAL)) {
790df8bae1dSRodney W. Grimes 
791df8bae1dSRodney W. Grimes 		/*
792df8bae1dSRodney W. Grimes 		 *	Make another reference to the object
793df8bae1dSRodney W. Grimes 		 */
794df8bae1dSRodney W. Grimes 		src_object->ref_count++;
795df8bae1dSRodney W. Grimes 
796df8bae1dSRodney W. Grimes 		/*
797df8bae1dSRodney W. Grimes 		 *	Mark all of the pages copy-on-write.
798df8bae1dSRodney W. Grimes 		 */
799df8bae1dSRodney W. Grimes 		for (p = src_object->memq.tqh_first; p; p = p->listq.tqe_next)
800df8bae1dSRodney W. Grimes 			if (src_offset <= p->offset &&
801df8bae1dSRodney W. Grimes 			    p->offset < src_offset + size)
802df8bae1dSRodney W. Grimes 				p->flags |= PG_COPYONWRITE;
803df8bae1dSRodney W. Grimes 		vm_object_unlock(src_object);
804df8bae1dSRodney W. Grimes 
805df8bae1dSRodney W. Grimes 		*dst_object = src_object;
806df8bae1dSRodney W. Grimes 		*dst_offset = src_offset;
807df8bae1dSRodney W. Grimes 
808df8bae1dSRodney W. Grimes 		/*
809df8bae1dSRodney W. Grimes 		 *	Must make a shadow when write is desired
810df8bae1dSRodney W. Grimes 		 */
811df8bae1dSRodney W. Grimes 		*src_needs_copy = TRUE;
812df8bae1dSRodney W. Grimes 		return;
813df8bae1dSRodney W. Grimes 	}
814df8bae1dSRodney W. Grimes 
815df8bae1dSRodney W. Grimes 
816df8bae1dSRodney W. Grimes 	/*
817df8bae1dSRodney W. Grimes 	 *	If the object has a pager, the pager wants to
818df8bae1dSRodney W. Grimes 	 *	see all of the changes.  We need a copy-object
819df8bae1dSRodney W. Grimes 	 *	for the changed pages.
820df8bae1dSRodney W. Grimes 	 *
821df8bae1dSRodney W. Grimes 	 *	If there is a copy-object, and it is empty,
822df8bae1dSRodney W. Grimes 	 *	no changes have been made to the object since the
823df8bae1dSRodney W. Grimes 	 *	copy-object was made.  We can use the same copy-
824df8bae1dSRodney W. Grimes 	 *	object.
825df8bae1dSRodney W. Grimes 	 */
826df8bae1dSRodney W. Grimes 
827df8bae1dSRodney W. Grimes     Retry1:
828df8bae1dSRodney W. Grimes 	old_copy = src_object->copy;
829df8bae1dSRodney W. Grimes 	if (old_copy != NULL) {
830df8bae1dSRodney W. Grimes 		/*
831df8bae1dSRodney W. Grimes 		 *	Try to get the locks (out of order)
832df8bae1dSRodney W. Grimes 		 */
833df8bae1dSRodney W. Grimes 		if (!vm_object_lock_try(old_copy)) {
834df8bae1dSRodney W. Grimes 			vm_object_unlock(src_object);
835df8bae1dSRodney W. Grimes 
836df8bae1dSRodney W. Grimes 			/* should spin a bit here... */
837df8bae1dSRodney W. Grimes 			vm_object_lock(src_object);
838df8bae1dSRodney W. Grimes 			goto Retry1;
839df8bae1dSRodney W. Grimes 		}
840df8bae1dSRodney W. Grimes 
841df8bae1dSRodney W. Grimes 		if (old_copy->resident_page_count == 0 &&
842df8bae1dSRodney W. Grimes 		    old_copy->pager == NULL) {
843df8bae1dSRodney W. Grimes 			/*
844df8bae1dSRodney W. Grimes 			 *	Return another reference to
845df8bae1dSRodney W. Grimes 			 *	the existing copy-object.
846df8bae1dSRodney W. Grimes 			 */
847df8bae1dSRodney W. Grimes 			old_copy->ref_count++;
848df8bae1dSRodney W. Grimes 			vm_object_unlock(old_copy);
849df8bae1dSRodney W. Grimes 			vm_object_unlock(src_object);
850df8bae1dSRodney W. Grimes 			*dst_object = old_copy;
851df8bae1dSRodney W. Grimes 			*dst_offset = src_offset;
852df8bae1dSRodney W. Grimes 			*src_needs_copy = FALSE;
853df8bae1dSRodney W. Grimes 			return;
854df8bae1dSRodney W. Grimes 		}
855df8bae1dSRodney W. Grimes 		vm_object_unlock(old_copy);
856df8bae1dSRodney W. Grimes 	}
857df8bae1dSRodney W. Grimes 	vm_object_unlock(src_object);
858df8bae1dSRodney W. Grimes 
859df8bae1dSRodney W. Grimes 	/*
860df8bae1dSRodney W. Grimes 	 *	If the object has a pager, the pager wants
861df8bae1dSRodney W. Grimes 	 *	to see all of the changes.  We must make
862df8bae1dSRodney W. Grimes 	 *	a copy-object and put the changed pages there.
863df8bae1dSRodney W. Grimes 	 *
864df8bae1dSRodney W. Grimes 	 *	The copy-object is always made large enough to
865df8bae1dSRodney W. Grimes 	 *	completely shadow the original object, since
866df8bae1dSRodney W. Grimes 	 *	it may have several users who want to shadow
867df8bae1dSRodney W. Grimes 	 *	the original object at different points.
868df8bae1dSRodney W. Grimes 	 */
869df8bae1dSRodney W. Grimes 
870df8bae1dSRodney W. Grimes 	new_copy = vm_object_allocate(src_object->size);
871df8bae1dSRodney W. Grimes 
872df8bae1dSRodney W. Grimes     Retry2:
873df8bae1dSRodney W. Grimes 	vm_object_lock(src_object);
874df8bae1dSRodney W. Grimes 	/*
875df8bae1dSRodney W. Grimes 	 *	Copy object may have changed while we were unlocked
876df8bae1dSRodney W. Grimes 	 */
877df8bae1dSRodney W. Grimes 	old_copy = src_object->copy;
878df8bae1dSRodney W. Grimes 	if (old_copy != NULL) {
879df8bae1dSRodney W. Grimes 		/*
880df8bae1dSRodney W. Grimes 		 *	Try to get the locks (out of order)
881df8bae1dSRodney W. Grimes 		 */
882df8bae1dSRodney W. Grimes 		if (!vm_object_lock_try(old_copy)) {
883df8bae1dSRodney W. Grimes 			vm_object_unlock(src_object);
884df8bae1dSRodney W. Grimes 			goto Retry2;
885df8bae1dSRodney W. Grimes 		}
886df8bae1dSRodney W. Grimes 
887df8bae1dSRodney W. Grimes 		/*
888df8bae1dSRodney W. Grimes 		 *	Consistency check
889df8bae1dSRodney W. Grimes 		 */
890df8bae1dSRodney W. Grimes 		if (old_copy->shadow != src_object ||
891df8bae1dSRodney W. Grimes 		    old_copy->shadow_offset != (vm_offset_t) 0)
892df8bae1dSRodney W. Grimes 			panic("vm_object_copy: copy/shadow inconsistency");
893df8bae1dSRodney W. Grimes 
894df8bae1dSRodney W. Grimes 		/*
895df8bae1dSRodney W. Grimes 		 *	Make the old copy-object shadow the new one.
896df8bae1dSRodney W. Grimes 		 *	It will receive no more pages from the original
897df8bae1dSRodney W. Grimes 		 *	object.
898df8bae1dSRodney W. Grimes 		 */
899df8bae1dSRodney W. Grimes 
900df8bae1dSRodney W. Grimes 		src_object->ref_count--;	/* remove ref. from old_copy */
901df8bae1dSRodney W. Grimes 		old_copy->shadow = new_copy;
902df8bae1dSRodney W. Grimes 		new_copy->ref_count++;		/* locking not needed - we
903df8bae1dSRodney W. Grimes 						   have the only pointer */
904df8bae1dSRodney W. Grimes 		vm_object_unlock(old_copy);	/* done with old_copy */
905df8bae1dSRodney W. Grimes 	}
906df8bae1dSRodney W. Grimes 
907df8bae1dSRodney W. Grimes 	new_start = (vm_offset_t) 0;	/* always shadow original at 0 */
908df8bae1dSRodney W. Grimes 	new_end   = (vm_offset_t) new_copy->size; /* for the whole object */
909df8bae1dSRodney W. Grimes 
910df8bae1dSRodney W. Grimes 	/*
911df8bae1dSRodney W. Grimes 	 *	Point the new copy at the existing object.
912df8bae1dSRodney W. Grimes 	 */
913df8bae1dSRodney W. Grimes 
914df8bae1dSRodney W. Grimes 	new_copy->shadow = src_object;
915df8bae1dSRodney W. Grimes 	new_copy->shadow_offset = new_start;
916df8bae1dSRodney W. Grimes 	src_object->ref_count++;
917df8bae1dSRodney W. Grimes 	src_object->copy = new_copy;
918df8bae1dSRodney W. Grimes 
919df8bae1dSRodney W. Grimes 	/*
920df8bae1dSRodney W. Grimes 	 *	Mark all the affected pages of the existing object
921df8bae1dSRodney W. Grimes 	 *	copy-on-write.
922df8bae1dSRodney W. Grimes 	 */
923df8bae1dSRodney W. Grimes 	for (p = src_object->memq.tqh_first; p != NULL; p = p->listq.tqe_next)
924df8bae1dSRodney W. Grimes 		if ((new_start <= p->offset) && (p->offset < new_end))
925df8bae1dSRodney W. Grimes 			p->flags |= PG_COPYONWRITE;
926df8bae1dSRodney W. Grimes 
927df8bae1dSRodney W. Grimes 	vm_object_unlock(src_object);
928df8bae1dSRodney W. Grimes 
929df8bae1dSRodney W. Grimes 	*dst_object = new_copy;
930df8bae1dSRodney W. Grimes 	*dst_offset = src_offset - new_start;
931df8bae1dSRodney W. Grimes 	*src_needs_copy = FALSE;
932df8bae1dSRodney W. Grimes }
933df8bae1dSRodney W. Grimes 
934df8bae1dSRodney W. Grimes /*
935df8bae1dSRodney W. Grimes  *	vm_object_shadow:
936df8bae1dSRodney W. Grimes  *
937df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
938df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
939df8bae1dSRodney W. Grimes  *	object reference is deallocated.
940df8bae1dSRodney W. Grimes  *
941df8bae1dSRodney W. Grimes  *	The new object and offset into that object
942df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
943df8bae1dSRodney W. Grimes  */
944df8bae1dSRodney W. Grimes 
94526f9a767SRodney W. Grimes void
94626f9a767SRodney W. Grimes vm_object_shadow(object, offset, length)
947df8bae1dSRodney W. Grimes 	vm_object_t	*object;	/* IN/OUT */
948df8bae1dSRodney W. Grimes 	vm_offset_t	*offset;	/* IN/OUT */
949df8bae1dSRodney W. Grimes 	vm_size_t	length;
950df8bae1dSRodney W. Grimes {
951df8bae1dSRodney W. Grimes 	register vm_object_t	source;
952df8bae1dSRodney W. Grimes 	register vm_object_t	result;
953df8bae1dSRodney W. Grimes 
954df8bae1dSRodney W. Grimes 	source = *object;
955df8bae1dSRodney W. Grimes 
956df8bae1dSRodney W. Grimes 	/*
957df8bae1dSRodney W. Grimes 	 *	Allocate a new object with the given length
958df8bae1dSRodney W. Grimes 	 */
959df8bae1dSRodney W. Grimes 
960df8bae1dSRodney W. Grimes 	if ((result = vm_object_allocate(length)) == NULL)
961df8bae1dSRodney W. Grimes 		panic("vm_object_shadow: no object for shadowing");
962df8bae1dSRodney W. Grimes 
963df8bae1dSRodney W. Grimes 	/*
964df8bae1dSRodney W. Grimes 	 *	The new object shadows the source object, adding
965df8bae1dSRodney W. Grimes 	 *	a reference to it.  Our caller changes his reference
966df8bae1dSRodney W. Grimes 	 *	to point to the new object, removing a reference to
967df8bae1dSRodney W. Grimes 	 *	the source object.  Net result: no change of reference
968df8bae1dSRodney W. Grimes 	 *	count.
969df8bae1dSRodney W. Grimes 	 */
970df8bae1dSRodney W. Grimes 	result->shadow = source;
971df8bae1dSRodney W. Grimes 
972df8bae1dSRodney W. Grimes 	/*
973df8bae1dSRodney W. Grimes 	 *	Store the offset into the source object,
974df8bae1dSRodney W. Grimes 	 *	and fix up the offset into the new object.
975df8bae1dSRodney W. Grimes 	 */
976df8bae1dSRodney W. Grimes 
977df8bae1dSRodney W. Grimes 	result->shadow_offset = *offset;
978df8bae1dSRodney W. Grimes 
979df8bae1dSRodney W. Grimes 	/*
980df8bae1dSRodney W. Grimes 	 *	Return the new things
981df8bae1dSRodney W. Grimes 	 */
982df8bae1dSRodney W. Grimes 
983df8bae1dSRodney W. Grimes 	*offset = 0;
984df8bae1dSRodney W. Grimes 	*object = result;
985df8bae1dSRodney W. Grimes }
986df8bae1dSRodney W. Grimes 
987df8bae1dSRodney W. Grimes /*
988df8bae1dSRodney W. Grimes  *	Set the specified object's pager to the specified pager.
989df8bae1dSRodney W. Grimes  */
990df8bae1dSRodney W. Grimes 
99126f9a767SRodney W. Grimes void
99226f9a767SRodney W. Grimes vm_object_setpager(object, pager, paging_offset,
993df8bae1dSRodney W. Grimes 			read_only)
994df8bae1dSRodney W. Grimes 	vm_object_t	object;
995df8bae1dSRodney W. Grimes 	vm_pager_t	pager;
996df8bae1dSRodney W. Grimes 	vm_offset_t	paging_offset;
997df8bae1dSRodney W. Grimes 	boolean_t	read_only;
998df8bae1dSRodney W. Grimes {
999df8bae1dSRodney W. Grimes #ifdef	lint
1000df8bae1dSRodney W. Grimes 	read_only++;	/* No longer used */
100126f9a767SRodney W. Grimes #endif	lint
1002df8bae1dSRodney W. Grimes 
1003df8bae1dSRodney W. Grimes 	vm_object_lock(object);			/* XXX ? */
100426f9a767SRodney W. Grimes 	if (object->pager && object->pager != pager) {
100526f9a767SRodney W. Grimes 		panic("!!!pager already allocated!!!\n");
100626f9a767SRodney W. Grimes 	}
1007df8bae1dSRodney W. Grimes 	object->pager = pager;
1008df8bae1dSRodney W. Grimes 	object->paging_offset = paging_offset;
1009df8bae1dSRodney W. Grimes 	vm_object_unlock(object);			/* XXX ? */
1010df8bae1dSRodney W. Grimes }
1011df8bae1dSRodney W. Grimes 
1012df8bae1dSRodney W. Grimes /*
1013df8bae1dSRodney W. Grimes  *	vm_object_hash hashes the pager/id pair.
1014df8bae1dSRodney W. Grimes  */
1015df8bae1dSRodney W. Grimes 
1016df8bae1dSRodney W. Grimes #define vm_object_hash(pager) \
101726f9a767SRodney W. Grimes 	(((unsigned)pager >> 5)%VM_OBJECT_HASH_COUNT)
1018df8bae1dSRodney W. Grimes 
1019df8bae1dSRodney W. Grimes /*
1020df8bae1dSRodney W. Grimes  *	vm_object_lookup looks in the object cache for an object with the
1021df8bae1dSRodney W. Grimes  *	specified pager and paging id.
1022df8bae1dSRodney W. Grimes  */
1023df8bae1dSRodney W. Grimes 
1024df8bae1dSRodney W. Grimes vm_object_t vm_object_lookup(pager)
1025df8bae1dSRodney W. Grimes 	vm_pager_t	pager;
1026df8bae1dSRodney W. Grimes {
1027df8bae1dSRodney W. Grimes 	register vm_object_hash_entry_t	entry;
1028df8bae1dSRodney W. Grimes 	vm_object_t			object;
1029df8bae1dSRodney W. Grimes 
1030df8bae1dSRodney W. Grimes 	vm_object_cache_lock();
1031df8bae1dSRodney W. Grimes 
1032df8bae1dSRodney W. Grimes 	for (entry = vm_object_hashtable[vm_object_hash(pager)].tqh_first;
1033df8bae1dSRodney W. Grimes 	     entry != NULL;
1034df8bae1dSRodney W. Grimes 	     entry = entry->hash_links.tqe_next) {
1035df8bae1dSRodney W. Grimes 		object = entry->object;
1036df8bae1dSRodney W. Grimes 		if (object->pager == pager) {
1037df8bae1dSRodney W. Grimes 			vm_object_lock(object);
1038df8bae1dSRodney W. Grimes 			if (object->ref_count == 0) {
1039df8bae1dSRodney W. Grimes 				TAILQ_REMOVE(&vm_object_cached_list, object,
1040df8bae1dSRodney W. Grimes 					cached_list);
1041df8bae1dSRodney W. Grimes 				vm_object_cached--;
1042df8bae1dSRodney W. Grimes 			}
1043df8bae1dSRodney W. Grimes 			object->ref_count++;
1044df8bae1dSRodney W. Grimes 			vm_object_unlock(object);
1045df8bae1dSRodney W. Grimes 			vm_object_cache_unlock();
1046df8bae1dSRodney W. Grimes 			return(object);
1047df8bae1dSRodney W. Grimes 		}
1048df8bae1dSRodney W. Grimes 	}
1049df8bae1dSRodney W. Grimes 
1050df8bae1dSRodney W. Grimes 	vm_object_cache_unlock();
1051df8bae1dSRodney W. Grimes 	return(NULL);
1052df8bae1dSRodney W. Grimes }
1053df8bae1dSRodney W. Grimes 
1054df8bae1dSRodney W. Grimes /*
1055df8bae1dSRodney W. Grimes  *	vm_object_enter enters the specified object/pager/id into
1056df8bae1dSRodney W. Grimes  *	the hash table.
1057df8bae1dSRodney W. Grimes  */
1058df8bae1dSRodney W. Grimes 
1059df8bae1dSRodney W. Grimes void vm_object_enter(object, pager)
1060df8bae1dSRodney W. Grimes 	vm_object_t	object;
1061df8bae1dSRodney W. Grimes 	vm_pager_t	pager;
1062df8bae1dSRodney W. Grimes {
1063df8bae1dSRodney W. Grimes 	struct vm_object_hash_head	*bucket;
1064df8bae1dSRodney W. Grimes 	register vm_object_hash_entry_t	entry;
1065df8bae1dSRodney W. Grimes 
1066df8bae1dSRodney W. Grimes 	/*
1067df8bae1dSRodney W. Grimes 	 *	We don't cache null objects, and we can't cache
1068df8bae1dSRodney W. Grimes 	 *	objects with the null pager.
1069df8bae1dSRodney W. Grimes 	 */
1070df8bae1dSRodney W. Grimes 
1071df8bae1dSRodney W. Grimes 	if (object == NULL)
1072df8bae1dSRodney W. Grimes 		return;
1073df8bae1dSRodney W. Grimes 	if (pager == NULL)
1074df8bae1dSRodney W. Grimes 		return;
1075df8bae1dSRodney W. Grimes 
1076df8bae1dSRodney W. Grimes 	bucket = &vm_object_hashtable[vm_object_hash(pager)];
1077df8bae1dSRodney W. Grimes 	entry = (vm_object_hash_entry_t)
1078df8bae1dSRodney W. Grimes 		malloc((u_long)sizeof *entry, M_VMOBJHASH, M_WAITOK);
1079df8bae1dSRodney W. Grimes 	entry->object = object;
1080df8bae1dSRodney W. Grimes 	object->flags |= OBJ_CANPERSIST;
1081df8bae1dSRodney W. Grimes 
1082df8bae1dSRodney W. Grimes 	vm_object_cache_lock();
1083df8bae1dSRodney W. Grimes 	TAILQ_INSERT_TAIL(bucket, entry, hash_links);
1084df8bae1dSRodney W. Grimes 	vm_object_cache_unlock();
1085df8bae1dSRodney W. Grimes }
1086df8bae1dSRodney W. Grimes 
1087df8bae1dSRodney W. Grimes /*
1088df8bae1dSRodney W. Grimes  *	vm_object_remove:
1089df8bae1dSRodney W. Grimes  *
1090df8bae1dSRodney W. Grimes  *	Remove the pager from the hash table.
1091df8bae1dSRodney W. Grimes  *	Note:  This assumes that the object cache
1092df8bae1dSRodney W. Grimes  *	is locked.  XXX this should be fixed
1093df8bae1dSRodney W. Grimes  *	by reorganizing vm_object_deallocate.
1094df8bae1dSRodney W. Grimes  */
1095df8bae1dSRodney W. Grimes void
1096df8bae1dSRodney W. Grimes vm_object_remove(pager)
1097df8bae1dSRodney W. Grimes 	register vm_pager_t	pager;
1098df8bae1dSRodney W. Grimes {
1099df8bae1dSRodney W. Grimes 	struct vm_object_hash_head	*bucket;
1100df8bae1dSRodney W. Grimes 	register vm_object_hash_entry_t	entry;
1101df8bae1dSRodney W. Grimes 	register vm_object_t		object;
1102df8bae1dSRodney W. Grimes 
1103df8bae1dSRodney W. Grimes 	bucket = &vm_object_hashtable[vm_object_hash(pager)];
1104df8bae1dSRodney W. Grimes 
1105df8bae1dSRodney W. Grimes 	for (entry = bucket->tqh_first;
1106df8bae1dSRodney W. Grimes 	     entry != NULL;
1107df8bae1dSRodney W. Grimes 	     entry = entry->hash_links.tqe_next) {
1108df8bae1dSRodney W. Grimes 		object = entry->object;
1109df8bae1dSRodney W. Grimes 		if (object->pager == pager) {
1110df8bae1dSRodney W. Grimes 			TAILQ_REMOVE(bucket, entry, hash_links);
1111df8bae1dSRodney W. Grimes 			free((caddr_t)entry, M_VMOBJHASH);
1112df8bae1dSRodney W. Grimes 			break;
1113df8bae1dSRodney W. Grimes 		}
1114df8bae1dSRodney W. Grimes 	}
1115df8bae1dSRodney W. Grimes }
1116df8bae1dSRodney W. Grimes 
1117df8bae1dSRodney W. Grimes boolean_t	vm_object_collapse_allowed = TRUE;
1118df8bae1dSRodney W. Grimes /*
1119df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1120df8bae1dSRodney W. Grimes  *
1121df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1122df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1123df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1124df8bae1dSRodney W. Grimes  *
1125df8bae1dSRodney W. Grimes  *	Requires that the object be locked and the page
1126df8bae1dSRodney W. Grimes  *	queues be unlocked.
1127df8bae1dSRodney W. Grimes  *
112826f9a767SRodney W. Grimes  *	This routine has significant changes by John S. Dyson
112926f9a767SRodney W. Grimes  *	to fix some swap memory leaks.  18 Dec 93
113026f9a767SRodney W. Grimes  *
1131df8bae1dSRodney W. Grimes  */
113226f9a767SRodney W. Grimes void
113326f9a767SRodney W. Grimes vm_object_collapse(object)
1134df8bae1dSRodney W. Grimes 	register vm_object_t	object;
1135df8bae1dSRodney W. Grimes 
1136df8bae1dSRodney W. Grimes {
1137df8bae1dSRodney W. Grimes 	register vm_object_t	backing_object;
1138df8bae1dSRodney W. Grimes 	register vm_offset_t	backing_offset;
1139df8bae1dSRodney W. Grimes 	register vm_size_t	size;
1140df8bae1dSRodney W. Grimes 	register vm_offset_t	new_offset;
1141df8bae1dSRodney W. Grimes 	register vm_page_t	p, pp;
1142df8bae1dSRodney W. Grimes 
1143df8bae1dSRodney W. Grimes 	if (!vm_object_collapse_allowed)
1144df8bae1dSRodney W. Grimes 		return;
1145df8bae1dSRodney W. Grimes 
1146df8bae1dSRodney W. Grimes 	while (TRUE) {
1147df8bae1dSRodney W. Grimes 		/*
1148df8bae1dSRodney W. Grimes 		 *	Verify that the conditions are right for collapse:
1149df8bae1dSRodney W. Grimes 		 *
1150df8bae1dSRodney W. Grimes 		 *	The object exists and no pages in it are currently
115126f9a767SRodney W. Grimes 		 *	being paged out.
1152df8bae1dSRodney W. Grimes 		 */
1153df8bae1dSRodney W. Grimes 		if (object == NULL ||
115426f9a767SRodney W. Grimes 		    object->paging_in_progress != 0)
1155df8bae1dSRodney W. Grimes 			return;
1156df8bae1dSRodney W. Grimes 
1157df8bae1dSRodney W. Grimes 		/*
1158df8bae1dSRodney W. Grimes 		 *		There is a backing object, and
1159df8bae1dSRodney W. Grimes 		 */
1160df8bae1dSRodney W. Grimes 
1161df8bae1dSRodney W. Grimes 		if ((backing_object = object->shadow) == NULL)
1162df8bae1dSRodney W. Grimes 			return;
1163df8bae1dSRodney W. Grimes 
1164df8bae1dSRodney W. Grimes 		vm_object_lock(backing_object);
1165df8bae1dSRodney W. Grimes 		/*
1166df8bae1dSRodney W. Grimes 		 *	...
1167df8bae1dSRodney W. Grimes 		 *		The backing object is not read_only,
1168df8bae1dSRodney W. Grimes 		 *		and no pages in the backing object are
1169df8bae1dSRodney W. Grimes 		 *		currently being paged out.
1170df8bae1dSRodney W. Grimes 		 *		The backing object is internal.
1171df8bae1dSRodney W. Grimes 		 */
1172df8bae1dSRodney W. Grimes 
1173df8bae1dSRodney W. Grimes 		if ((backing_object->flags & OBJ_INTERNAL) == 0 ||
1174df8bae1dSRodney W. Grimes 		    backing_object->paging_in_progress != 0) {
1175df8bae1dSRodney W. Grimes 			vm_object_unlock(backing_object);
1176df8bae1dSRodney W. Grimes 			return;
1177df8bae1dSRodney W. Grimes 		}
1178df8bae1dSRodney W. Grimes 
1179df8bae1dSRodney W. Grimes 		/*
1180df8bae1dSRodney W. Grimes 		 *	The backing object can't be a copy-object:
1181df8bae1dSRodney W. Grimes 		 *	the shadow_offset for the copy-object must stay
1182df8bae1dSRodney W. Grimes 		 *	as 0.  Furthermore (for the 'we have all the
1183df8bae1dSRodney W. Grimes 		 *	pages' case), if we bypass backing_object and
1184df8bae1dSRodney W. Grimes 		 *	just shadow the next object in the chain, old
1185df8bae1dSRodney W. Grimes 		 *	pages from that object would then have to be copied
1186df8bae1dSRodney W. Grimes 		 *	BOTH into the (former) backing_object and into the
1187df8bae1dSRodney W. Grimes 		 *	parent object.
1188df8bae1dSRodney W. Grimes 		 */
1189df8bae1dSRodney W. Grimes 		if (backing_object->shadow != NULL &&
119026f9a767SRodney W. Grimes 		    backing_object->shadow->copy == backing_object) {
1191df8bae1dSRodney W. Grimes 			vm_object_unlock(backing_object);
1192df8bae1dSRodney W. Grimes 			return;
1193df8bae1dSRodney W. Grimes 		}
1194df8bae1dSRodney W. Grimes 
1195df8bae1dSRodney W. Grimes 		/*
119626f9a767SRodney W. Grimes 		 * we can deal only with the swap pager
119726f9a767SRodney W. Grimes 		 */
119826f9a767SRodney W. Grimes 		if ((object->pager &&
119926f9a767SRodney W. Grimes 		    	object->pager->pg_type != PG_SWAP) ||
120026f9a767SRodney W. Grimes 		    (backing_object->pager &&
120126f9a767SRodney W. Grimes 		    	backing_object->pager->pg_type != PG_SWAP)) {
120226f9a767SRodney W. Grimes 			vm_object_unlock(backing_object);
120326f9a767SRodney W. Grimes 			return;
120426f9a767SRodney W. Grimes 		}
120526f9a767SRodney W. Grimes 
120626f9a767SRodney W. Grimes 
120726f9a767SRodney W. Grimes 		/*
1208df8bae1dSRodney W. Grimes 		 *	We know that we can either collapse the backing
1209df8bae1dSRodney W. Grimes 		 *	object (if the parent is the only reference to
1210df8bae1dSRodney W. Grimes 		 *	it) or (perhaps) remove the parent's reference
1211df8bae1dSRodney W. Grimes 		 *	to it.
1212df8bae1dSRodney W. Grimes 		 */
1213df8bae1dSRodney W. Grimes 
1214df8bae1dSRodney W. Grimes 		backing_offset = object->shadow_offset;
1215df8bae1dSRodney W. Grimes 		size = object->size;
1216df8bae1dSRodney W. Grimes 
1217df8bae1dSRodney W. Grimes 		/*
1218df8bae1dSRodney W. Grimes 		 *	If there is exactly one reference to the backing
1219df8bae1dSRodney W. Grimes 		 *	object, we can collapse it into the parent.
1220df8bae1dSRodney W. Grimes 		 */
1221df8bae1dSRodney W. Grimes 
1222df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1223df8bae1dSRodney W. Grimes 
1224df8bae1dSRodney W. Grimes 			/*
1225df8bae1dSRodney W. Grimes 			 *	We can collapse the backing object.
1226df8bae1dSRodney W. Grimes 			 *
1227df8bae1dSRodney W. Grimes 			 *	Move all in-memory pages from backing_object
1228df8bae1dSRodney W. Grimes 			 *	to the parent.  Pages that have been paged out
1229df8bae1dSRodney W. Grimes 			 *	will be overwritten by any of the parent's
1230df8bae1dSRodney W. Grimes 			 *	pages that shadow them.
1231df8bae1dSRodney W. Grimes 			 */
1232df8bae1dSRodney W. Grimes 
123326f9a767SRodney W. Grimes 			while (p = backing_object->memq.tqh_first) {
123426f9a767SRodney W. Grimes 
1235df8bae1dSRodney W. Grimes 				new_offset = (p->offset - backing_offset);
1236df8bae1dSRodney W. Grimes 
1237df8bae1dSRodney W. Grimes 				/*
1238df8bae1dSRodney W. Grimes 				 *	If the parent has a page here, or if
1239df8bae1dSRodney W. Grimes 				 *	this page falls outside the parent,
1240df8bae1dSRodney W. Grimes 				 *	dispose of it.
1241df8bae1dSRodney W. Grimes 				 *
1242df8bae1dSRodney W. Grimes 				 *	Otherwise, move it as planned.
1243df8bae1dSRodney W. Grimes 				 */
1244df8bae1dSRodney W. Grimes 
1245df8bae1dSRodney W. Grimes 				if (p->offset < backing_offset ||
1246df8bae1dSRodney W. Grimes 				    new_offset >= size) {
1247df8bae1dSRodney W. Grimes 					vm_page_lock_queues();
1248df8bae1dSRodney W. Grimes 					vm_page_free(p);
1249df8bae1dSRodney W. Grimes 					vm_page_unlock_queues();
1250df8bae1dSRodney W. Grimes 				} else {
1251df8bae1dSRodney W. Grimes 				    pp = vm_page_lookup(object, new_offset);
125226f9a767SRodney W. Grimes 				    if (pp != NULL || (object->pager && vm_pager_has_page(object->pager,
125326f9a767SRodney W. Grimes 						object->paging_offset + new_offset))) {
1254df8bae1dSRodney W. Grimes 					vm_page_lock_queues();
1255df8bae1dSRodney W. Grimes 					vm_page_free(p);
1256df8bae1dSRodney W. Grimes 					vm_page_unlock_queues();
125726f9a767SRodney W. Grimes 				    } else {
1258df8bae1dSRodney W. Grimes 					vm_page_rename(p, object, new_offset);
1259df8bae1dSRodney W. Grimes 				    }
1260df8bae1dSRodney W. Grimes 				}
1261df8bae1dSRodney W. Grimes 			}
1262df8bae1dSRodney W. Grimes 
1263df8bae1dSRodney W. Grimes 			/*
1264df8bae1dSRodney W. Grimes 			 *	Move the pager from backing_object to object.
1265df8bae1dSRodney W. Grimes 			 */
1266df8bae1dSRodney W. Grimes 
1267df8bae1dSRodney W. Grimes 			if (backing_object->pager) {
126826f9a767SRodney W. Grimes 				backing_object->paging_in_progress++;
126926f9a767SRodney W. Grimes 				if (object->pager) {
127026f9a767SRodney W. Grimes 					vm_pager_t bopager;
127126f9a767SRodney W. Grimes 					object->paging_in_progress++;
127226f9a767SRodney W. Grimes 					/*
127326f9a767SRodney W. Grimes 					 * copy shadow object pages into ours
127426f9a767SRodney W. Grimes 					 * and destroy unneeded pages in shadow object.
127526f9a767SRodney W. Grimes 					 */
127626f9a767SRodney W. Grimes 					bopager = backing_object->pager;
1277df8bae1dSRodney W. Grimes 					backing_object->pager = NULL;
127826f9a767SRodney W. Grimes 					vm_object_remove(backing_object->pager);
127926f9a767SRodney W. Grimes 					swap_pager_copy(
128026f9a767SRodney W. Grimes 						bopager, backing_object->paging_offset,
128126f9a767SRodney W. Grimes 						object->pager, object->paging_offset,
128226f9a767SRodney W. Grimes 						object->shadow_offset);
128326f9a767SRodney W. Grimes 					object->paging_in_progress--;
128426f9a767SRodney W. Grimes 					if (object->paging_in_progress == 0)
128526f9a767SRodney W. Grimes 						wakeup((caddr_t)object);
128626f9a767SRodney W. Grimes 				} else {
128726f9a767SRodney W. Grimes 					object->paging_in_progress++;
128826f9a767SRodney W. Grimes 					/*
128926f9a767SRodney W. Grimes 					 * grab the shadow objects pager
129026f9a767SRodney W. Grimes 					 */
129126f9a767SRodney W. Grimes 					object->pager = backing_object->pager;
129226f9a767SRodney W. Grimes 					object->paging_offset = backing_object->paging_offset + backing_offset;
129326f9a767SRodney W. Grimes 					vm_object_remove(backing_object->pager);
129426f9a767SRodney W. Grimes 					backing_object->pager = NULL;
129526f9a767SRodney W. Grimes 					/*
129626f9a767SRodney W. Grimes 					 * free unnecessary blocks
129726f9a767SRodney W. Grimes 					 */
129826f9a767SRodney W. Grimes 					swap_pager_freespace(object->pager, 0, object->paging_offset);
129926f9a767SRodney W. Grimes 					object->paging_in_progress--;
130026f9a767SRodney W. Grimes 					if (object->paging_in_progress == 0)
130126f9a767SRodney W. Grimes 						wakeup((caddr_t)object);
1302df8bae1dSRodney W. Grimes 				}
130326f9a767SRodney W. Grimes 				backing_object->paging_in_progress--;
130426f9a767SRodney W. Grimes 				if (backing_object->paging_in_progress == 0)
130526f9a767SRodney W. Grimes 					wakeup((caddr_t)backing_object);
130626f9a767SRodney W. Grimes 			}
130726f9a767SRodney W. Grimes 
1308df8bae1dSRodney W. Grimes 
1309df8bae1dSRodney W. Grimes 			/*
1310df8bae1dSRodney W. Grimes 			 *	Object now shadows whatever backing_object did.
1311df8bae1dSRodney W. Grimes 			 *	Note that the reference to backing_object->shadow
1312df8bae1dSRodney W. Grimes 			 *	moves from within backing_object to within object.
1313df8bae1dSRodney W. Grimes 			 */
1314df8bae1dSRodney W. Grimes 
1315df8bae1dSRodney W. Grimes 			object->shadow = backing_object->shadow;
1316df8bae1dSRodney W. Grimes 			object->shadow_offset += backing_object->shadow_offset;
1317df8bae1dSRodney W. Grimes 			if (object->shadow != NULL &&
1318df8bae1dSRodney W. Grimes 			    object->shadow->copy != NULL) {
1319df8bae1dSRodney W. Grimes 				panic("vm_object_collapse: we collapsed a copy-object!");
1320df8bae1dSRodney W. Grimes 			}
1321df8bae1dSRodney W. Grimes 			/*
1322df8bae1dSRodney W. Grimes 			 *	Discard backing_object.
1323df8bae1dSRodney W. Grimes 			 *
1324df8bae1dSRodney W. Grimes 			 *	Since the backing object has no pages, no
1325df8bae1dSRodney W. Grimes 			 *	pager left, and no object references within it,
1326df8bae1dSRodney W. Grimes 			 *	all that is necessary is to dispose of it.
1327df8bae1dSRodney W. Grimes 			 */
1328df8bae1dSRodney W. Grimes 
1329df8bae1dSRodney W. Grimes 			vm_object_unlock(backing_object);
1330df8bae1dSRodney W. Grimes 
1331df8bae1dSRodney W. Grimes 			simple_lock(&vm_object_list_lock);
1332df8bae1dSRodney W. Grimes 			TAILQ_REMOVE(&vm_object_list, backing_object,
1333df8bae1dSRodney W. Grimes 				object_list);
1334df8bae1dSRodney W. Grimes 			vm_object_count--;
1335df8bae1dSRodney W. Grimes 			simple_unlock(&vm_object_list_lock);
1336df8bae1dSRodney W. Grimes 
1337df8bae1dSRodney W. Grimes 			free((caddr_t)backing_object, M_VMOBJ);
1338df8bae1dSRodney W. Grimes 
1339df8bae1dSRodney W. Grimes 			object_collapses++;
1340df8bae1dSRodney W. Grimes 		}
1341df8bae1dSRodney W. Grimes 		else {
1342df8bae1dSRodney W. Grimes 			/*
1343df8bae1dSRodney W. Grimes 			 *	If all of the pages in the backing object are
1344df8bae1dSRodney W. Grimes 			 *	shadowed by the parent object, the parent
1345df8bae1dSRodney W. Grimes 			 *	object no longer has to shadow the backing
1346df8bae1dSRodney W. Grimes 			 *	object; it can shadow the next one in the
1347df8bae1dSRodney W. Grimes 			 *	chain.
1348df8bae1dSRodney W. Grimes 			 *
1349df8bae1dSRodney W. Grimes 			 *	The backing object must not be paged out - we'd
1350df8bae1dSRodney W. Grimes 			 *	have to check all of the paged-out pages, as
1351df8bae1dSRodney W. Grimes 			 *	well.
1352df8bae1dSRodney W. Grimes 			 */
1353df8bae1dSRodney W. Grimes 
1354df8bae1dSRodney W. Grimes 			if (backing_object->pager != NULL) {
1355df8bae1dSRodney W. Grimes 				vm_object_unlock(backing_object);
1356df8bae1dSRodney W. Grimes 				return;
1357df8bae1dSRodney W. Grimes 			}
1358df8bae1dSRodney W. Grimes 
1359df8bae1dSRodney W. Grimes 			/*
1360df8bae1dSRodney W. Grimes 			 *	Should have a check for a 'small' number
1361df8bae1dSRodney W. Grimes 			 *	of pages here.
1362df8bae1dSRodney W. Grimes 			 */
1363df8bae1dSRodney W. Grimes 
136426f9a767SRodney W. Grimes 			for( p = backing_object->memq.tqh_first;p;p=p->listq.tqe_next) {
1365df8bae1dSRodney W. Grimes 				new_offset = (p->offset - backing_offset);
1366df8bae1dSRodney W. Grimes 
1367df8bae1dSRodney W. Grimes 				/*
1368df8bae1dSRodney W. Grimes 				 *	If the parent has a page here, or if
1369df8bae1dSRodney W. Grimes 				 *	this page falls outside the parent,
1370df8bae1dSRodney W. Grimes 				 *	keep going.
1371df8bae1dSRodney W. Grimes 				 *
1372df8bae1dSRodney W. Grimes 				 *	Otherwise, the backing_object must be
1373df8bae1dSRodney W. Grimes 				 *	left in the chain.
1374df8bae1dSRodney W. Grimes 				 */
1375df8bae1dSRodney W. Grimes 
1376df8bae1dSRodney W. Grimes 				if (p->offset >= backing_offset &&
137726f9a767SRodney W. Grimes 				    new_offset <= size &&
137826f9a767SRodney W. Grimes 				    ((pp = vm_page_lookup(object, new_offset)) == NULL || (pp->flags & PG_FAKE)) &&
137926f9a767SRodney W. Grimes 					(!object->pager || !vm_pager_has_page(object->pager, object->paging_offset+new_offset))) {
1380df8bae1dSRodney W. Grimes 					/*
1381df8bae1dSRodney W. Grimes 					 *	Page still needed.
1382df8bae1dSRodney W. Grimes 					 *	Can't go any further.
1383df8bae1dSRodney W. Grimes 					 */
1384df8bae1dSRodney W. Grimes 					vm_object_unlock(backing_object);
1385df8bae1dSRodney W. Grimes 					return;
1386df8bae1dSRodney W. Grimes 				}
1387df8bae1dSRodney W. Grimes 			}
1388df8bae1dSRodney W. Grimes 
1389df8bae1dSRodney W. Grimes 			/*
1390df8bae1dSRodney W. Grimes 			 *	Make the parent shadow the next object
1391df8bae1dSRodney W. Grimes 			 *	in the chain.  Deallocating backing_object
1392df8bae1dSRodney W. Grimes 			 *	will not remove it, since its reference
1393df8bae1dSRodney W. Grimes 			 *	count is at least 2.
1394df8bae1dSRodney W. Grimes 			 */
1395df8bae1dSRodney W. Grimes 
139626f9a767SRodney W. Grimes 			vm_object_reference(object->shadow = backing_object->shadow);
1397df8bae1dSRodney W. Grimes 			object->shadow_offset += backing_object->shadow_offset;
1398df8bae1dSRodney W. Grimes 
1399df8bae1dSRodney W. Grimes 			/*
1400df8bae1dSRodney W. Grimes 			 *      Backing object might have had a copy pointer
1401df8bae1dSRodney W. Grimes 			 *      to us.  If it did, clear it.
1402df8bae1dSRodney W. Grimes 			 */
1403df8bae1dSRodney W. Grimes 			if (backing_object->copy == object) {
1404df8bae1dSRodney W. Grimes 				backing_object->copy = NULL;
1405df8bae1dSRodney W. Grimes 			}
1406df8bae1dSRodney W. Grimes 
1407df8bae1dSRodney W. Grimes 			/*	Drop the reference count on backing_object.
1408df8bae1dSRodney W. Grimes 			 *	Since its ref_count was at least 2, it
1409df8bae1dSRodney W. Grimes 			 *	will not vanish; so we don't need to call
1410df8bae1dSRodney W. Grimes 			 *	vm_object_deallocate.
1411df8bae1dSRodney W. Grimes 			 */
141226f9a767SRodney W. Grimes 			if (backing_object->ref_count == 1)
141326f9a767SRodney W. Grimes 				printf("should have called obj deallocate\n");
1414df8bae1dSRodney W. Grimes 			backing_object->ref_count--;
1415df8bae1dSRodney W. Grimes 			vm_object_unlock(backing_object);
1416df8bae1dSRodney W. Grimes 
1417df8bae1dSRodney W. Grimes 			object_bypasses ++;
1418df8bae1dSRodney W. Grimes 
1419df8bae1dSRodney W. Grimes 		}
1420df8bae1dSRodney W. Grimes 
1421df8bae1dSRodney W. Grimes 		/*
1422df8bae1dSRodney W. Grimes 		 *	Try again with this object's new backing object.
1423df8bae1dSRodney W. Grimes 		 */
1424df8bae1dSRodney W. Grimes 	}
1425df8bae1dSRodney W. Grimes }
1426df8bae1dSRodney W. Grimes 
1427df8bae1dSRodney W. Grimes /*
1428df8bae1dSRodney W. Grimes  *	vm_object_page_remove: [internal]
1429df8bae1dSRodney W. Grimes  *
1430df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
1431df8bae1dSRodney W. Grimes  *	object range from the object's list of pages.
1432df8bae1dSRodney W. Grimes  *
1433df8bae1dSRodney W. Grimes  *	The object must be locked.
1434df8bae1dSRodney W. Grimes  */
143526f9a767SRodney W. Grimes void
143626f9a767SRodney W. Grimes vm_object_page_remove(object, start, end)
1437df8bae1dSRodney W. Grimes 	register vm_object_t	object;
1438df8bae1dSRodney W. Grimes 	register vm_offset_t	start;
1439df8bae1dSRodney W. Grimes 	register vm_offset_t	end;
1440df8bae1dSRodney W. Grimes {
1441df8bae1dSRodney W. Grimes 	register vm_page_t	p, next;
144226f9a767SRodney W. Grimes 	vm_offset_t size;
144326f9a767SRodney W. Grimes 	int cnt;
144426f9a767SRodney W. Grimes 	int s;
1445df8bae1dSRodney W. Grimes 
1446df8bae1dSRodney W. Grimes 	if (object == NULL)
1447df8bae1dSRodney W. Grimes 		return;
1448df8bae1dSRodney W. Grimes 
144926f9a767SRodney W. Grimes 	start = trunc_page(start);
145026f9a767SRodney W. Grimes 	end = round_page(end);
145126f9a767SRodney W. Grimes again:
145226f9a767SRodney W. Grimes 	size = end-start;
145326f9a767SRodney W. Grimes 	if (size > 4*PAGE_SIZE || size >= object->size/4) {
145426f9a767SRodney W. Grimes 		for (p = object->memq.tqh_first; (p != NULL && size > 0); p = next) {
1455df8bae1dSRodney W. Grimes 			next = p->listq.tqe_next;
1456df8bae1dSRodney W. Grimes 			if ((start <= p->offset) && (p->offset < end)) {
145726f9a767SRodney W. Grimes 				if (p->flags & PG_BUSY) {
145826f9a767SRodney W. Grimes 					p->flags |= PG_WANTED;
145926f9a767SRodney W. Grimes 					tsleep((caddr_t) p, PVM, "vmopar", 0);
146026f9a767SRodney W. Grimes 					goto again;
146126f9a767SRodney W. Grimes 				}
1462df8bae1dSRodney W. Grimes 				pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
1463df8bae1dSRodney W. Grimes 				vm_page_lock_queues();
1464df8bae1dSRodney W. Grimes 				vm_page_free(p);
1465df8bae1dSRodney W. Grimes 				vm_page_unlock_queues();
146626f9a767SRodney W. Grimes 				size -= PAGE_SIZE;
146726f9a767SRodney W. Grimes 			}
146826f9a767SRodney W. Grimes 		}
146926f9a767SRodney W. Grimes 	} else {
147026f9a767SRodney W. Grimes 		while (size > 0) {
147126f9a767SRodney W. Grimes 			while (p = vm_page_lookup(object, start)) {
147226f9a767SRodney W. Grimes 				if (p->flags & PG_BUSY) {
147326f9a767SRodney W. Grimes 					p->flags |= PG_WANTED;
147426f9a767SRodney W. Grimes 					tsleep((caddr_t) p, PVM, "vmopar", 0);
147526f9a767SRodney W. Grimes 					goto again;
147626f9a767SRodney W. Grimes 				}
147726f9a767SRodney W. Grimes 				pmap_page_protect(VM_PAGE_TO_PHYS(p), VM_PROT_NONE);
147826f9a767SRodney W. Grimes 				vm_page_lock_queues();
147926f9a767SRodney W. Grimes 				vm_page_free(p);
148026f9a767SRodney W. Grimes 				vm_page_unlock_queues();
148126f9a767SRodney W. Grimes 			}
148226f9a767SRodney W. Grimes 			start += PAGE_SIZE;
148326f9a767SRodney W. Grimes 			size -= PAGE_SIZE;
1484df8bae1dSRodney W. Grimes 		}
1485df8bae1dSRodney W. Grimes 	}
1486df8bae1dSRodney W. Grimes }
1487df8bae1dSRodney W. Grimes 
1488df8bae1dSRodney W. Grimes /*
1489df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1490df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1491df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1492df8bae1dSRodney W. Grimes  *
1493df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1494df8bae1dSRodney W. Grimes  *
1495df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1496df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1497df8bae1dSRodney W. Grimes  *
1498df8bae1dSRodney W. Grimes  *	Parameters:
1499df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1500df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1501df8bae1dSRodney W. Grimes  *		next_object	Second object into coalesce
1502df8bae1dSRodney W. Grimes  *		next_offset	Offset into next_object
1503df8bae1dSRodney W. Grimes  *
1504df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
1505df8bae1dSRodney W. Grimes  *		next_size	Size of reference to next_object
1506df8bae1dSRodney W. Grimes  *
1507df8bae1dSRodney W. Grimes  *	Conditions:
1508df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1509df8bae1dSRodney W. Grimes  */
1510df8bae1dSRodney W. Grimes boolean_t vm_object_coalesce(prev_object, next_object,
1511df8bae1dSRodney W. Grimes 			prev_offset, next_offset,
1512df8bae1dSRodney W. Grimes 			prev_size, next_size)
1513df8bae1dSRodney W. Grimes 
1514df8bae1dSRodney W. Grimes 	register vm_object_t	prev_object;
1515df8bae1dSRodney W. Grimes 	vm_object_t	next_object;
1516df8bae1dSRodney W. Grimes 	vm_offset_t	prev_offset, next_offset;
1517df8bae1dSRodney W. Grimes 	vm_size_t	prev_size, next_size;
1518df8bae1dSRodney W. Grimes {
1519df8bae1dSRodney W. Grimes 	vm_size_t	newsize;
1520df8bae1dSRodney W. Grimes 
1521df8bae1dSRodney W. Grimes #ifdef	lint
1522df8bae1dSRodney W. Grimes 	next_offset++;
1523df8bae1dSRodney W. Grimes #endif
1524df8bae1dSRodney W. Grimes 
1525df8bae1dSRodney W. Grimes 	if (next_object != NULL) {
1526df8bae1dSRodney W. Grimes 		return(FALSE);
1527df8bae1dSRodney W. Grimes 	}
1528df8bae1dSRodney W. Grimes 
1529df8bae1dSRodney W. Grimes 	if (prev_object == NULL) {
1530df8bae1dSRodney W. Grimes 		return(TRUE);
1531df8bae1dSRodney W. Grimes 	}
1532df8bae1dSRodney W. Grimes 
1533df8bae1dSRodney W. Grimes 	vm_object_lock(prev_object);
1534df8bae1dSRodney W. Grimes 
1535df8bae1dSRodney W. Grimes 	/*
1536df8bae1dSRodney W. Grimes 	 *	Try to collapse the object first
1537df8bae1dSRodney W. Grimes 	 */
1538df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1539df8bae1dSRodney W. Grimes 
1540df8bae1dSRodney W. Grimes 	/*
1541df8bae1dSRodney W. Grimes 	 *	Can't coalesce if:
1542df8bae1dSRodney W. Grimes 	 *	. more than one reference
1543df8bae1dSRodney W. Grimes 	 *	. paged out
1544df8bae1dSRodney W. Grimes 	 *	. shadows another object
1545df8bae1dSRodney W. Grimes 	 *	. has a copy elsewhere
1546df8bae1dSRodney W. Grimes 	 *	(any of which mean that the pages not mapped to
1547df8bae1dSRodney W. Grimes 	 *	prev_entry may be in use anyway)
1548df8bae1dSRodney W. Grimes 	 */
1549df8bae1dSRodney W. Grimes 
1550df8bae1dSRodney W. Grimes 	if (prev_object->ref_count > 1 ||
1551df8bae1dSRodney W. Grimes 		prev_object->pager != NULL ||
1552df8bae1dSRodney W. Grimes 		prev_object->shadow != NULL ||
1553df8bae1dSRodney W. Grimes 		prev_object->copy != NULL) {
1554df8bae1dSRodney W. Grimes 		vm_object_unlock(prev_object);
1555df8bae1dSRodney W. Grimes 		return(FALSE);
1556df8bae1dSRodney W. Grimes 	}
1557df8bae1dSRodney W. Grimes 
1558df8bae1dSRodney W. Grimes 	/*
1559df8bae1dSRodney W. Grimes 	 *	Remove any pages that may still be in the object from
1560df8bae1dSRodney W. Grimes 	 *	a previous deallocation.
1561df8bae1dSRodney W. Grimes 	 */
1562df8bae1dSRodney W. Grimes 
1563df8bae1dSRodney W. Grimes 	vm_object_page_remove(prev_object,
1564df8bae1dSRodney W. Grimes 			prev_offset + prev_size,
1565df8bae1dSRodney W. Grimes 			prev_offset + prev_size + next_size);
1566df8bae1dSRodney W. Grimes 
1567df8bae1dSRodney W. Grimes 	/*
1568df8bae1dSRodney W. Grimes 	 *	Extend the object if necessary.
1569df8bae1dSRodney W. Grimes 	 */
1570df8bae1dSRodney W. Grimes 	newsize = prev_offset + prev_size + next_size;
1571df8bae1dSRodney W. Grimes 	if (newsize > prev_object->size)
1572df8bae1dSRodney W. Grimes 		prev_object->size = newsize;
1573df8bae1dSRodney W. Grimes 
1574df8bae1dSRodney W. Grimes 	vm_object_unlock(prev_object);
1575df8bae1dSRodney W. Grimes 	return(TRUE);
1576df8bae1dSRodney W. Grimes }
1577df8bae1dSRodney W. Grimes 
1578df8bae1dSRodney W. Grimes /*
157926f9a767SRodney W. Grimes  * returns page after looking up in shadow chain
158026f9a767SRodney W. Grimes  */
158126f9a767SRodney W. Grimes 
158226f9a767SRodney W. Grimes vm_page_t
158326f9a767SRodney W. Grimes vm_object_page_lookup(object, offset)
158426f9a767SRodney W. Grimes 	vm_object_t object;
158526f9a767SRodney W. Grimes 	vm_offset_t offset;
158626f9a767SRodney W. Grimes {
158726f9a767SRodney W. Grimes 	vm_page_t m;
158826f9a767SRodney W. Grimes 	if (!(m=vm_page_lookup(object, offset))) {
158926f9a767SRodney W. Grimes 		if (!object->shadow)
159026f9a767SRodney W. Grimes 			return 0;
159126f9a767SRodney W. Grimes 		else
159226f9a767SRodney W. Grimes 			return vm_object_page_lookup(object->shadow, offset + object->shadow_offset);
159326f9a767SRodney W. Grimes 	}
159426f9a767SRodney W. Grimes 	return m;
159526f9a767SRodney W. Grimes }
159626f9a767SRodney W. Grimes 
159726f9a767SRodney W. Grimes #define DEBUG
159826f9a767SRodney W. Grimes #if defined(DEBUG) || (NDDB > 0)
159926f9a767SRodney W. Grimes /*
1600df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
1601df8bae1dSRodney W. Grimes  */
1602df8bae1dSRodney W. Grimes void vm_object_print(object, full)
1603df8bae1dSRodney W. Grimes 	vm_object_t	object;
1604df8bae1dSRodney W. Grimes 	boolean_t	full;
1605df8bae1dSRodney W. Grimes {
1606df8bae1dSRodney W. Grimes 	register vm_page_t	p;
1607df8bae1dSRodney W. Grimes 	extern indent;
1608df8bae1dSRodney W. Grimes 
1609df8bae1dSRodney W. Grimes 	register int count;
1610df8bae1dSRodney W. Grimes 
1611df8bae1dSRodney W. Grimes 	if (object == NULL)
1612df8bae1dSRodney W. Grimes 		return;
1613df8bae1dSRodney W. Grimes 
1614df8bae1dSRodney W. Grimes 	iprintf("Object 0x%x: size=0x%x, res=%d, ref=%d, ",
1615df8bae1dSRodney W. Grimes 		(int) object, (int) object->size,
1616df8bae1dSRodney W. Grimes 		object->resident_page_count, object->ref_count);
1617df8bae1dSRodney W. Grimes 	printf("pager=0x%x+0x%x, shadow=(0x%x)+0x%x\n",
1618df8bae1dSRodney W. Grimes 	       (int) object->pager, (int) object->paging_offset,
1619df8bae1dSRodney W. Grimes 	       (int) object->shadow, (int) object->shadow_offset);
1620df8bae1dSRodney W. Grimes 	printf("cache: next=0x%x, prev=0x%x\n",
1621df8bae1dSRodney W. Grimes 	       object->cached_list.tqe_next, object->cached_list.tqe_prev);
1622df8bae1dSRodney W. Grimes 
1623df8bae1dSRodney W. Grimes 	if (!full)
1624df8bae1dSRodney W. Grimes 		return;
1625df8bae1dSRodney W. Grimes 
1626df8bae1dSRodney W. Grimes 	indent += 2;
1627df8bae1dSRodney W. Grimes 	count = 0;
1628df8bae1dSRodney W. Grimes 	for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) {
1629df8bae1dSRodney W. Grimes 		if (count == 0)
1630df8bae1dSRodney W. Grimes 			iprintf("memory:=");
1631df8bae1dSRodney W. Grimes 		else if (count == 6) {
1632df8bae1dSRodney W. Grimes 			printf("\n");
1633df8bae1dSRodney W. Grimes 			iprintf(" ...");
1634df8bae1dSRodney W. Grimes 			count = 0;
1635df8bae1dSRodney W. Grimes 		} else
1636df8bae1dSRodney W. Grimes 			printf(",");
1637df8bae1dSRodney W. Grimes 		count++;
1638df8bae1dSRodney W. Grimes 
1639df8bae1dSRodney W. Grimes 		printf("(off=0x%x,page=0x%x)", p->offset, VM_PAGE_TO_PHYS(p));
1640df8bae1dSRodney W. Grimes 	}
1641df8bae1dSRodney W. Grimes 	if (count != 0)
1642df8bae1dSRodney W. Grimes 		printf("\n");
1643df8bae1dSRodney W. Grimes 	indent -= 2;
1644df8bae1dSRodney W. Grimes }
164526f9a767SRodney W. Grimes #endif /* defined(DEBUG) || (NDDB > 0) */
1646