xref: /freebsd/sys/vm/vm_object.c (revision 9fcfb650d1733bc9d84a7d4ebd465d66ad2bff6c)
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  *
649fcfb650SDavid Greenman  * $Id: vm_object.c,v 1.131 1998/10/23 05:25:49 dg 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/proc.h>		/* for curproc, pageproc */
740d94caffSDavid Greenman #include <sys/vnode.h>
75efeaf95aSDavid Greenman #include <sys/vmmeter.h>
76867a482dSJohn Dyson #include <sys/mman.h>
77cf2819ccSJohn Dyson #include <sys/mount.h>
78df8bae1dSRodney W. Grimes 
79df8bae1dSRodney W. Grimes #include <vm/vm.h>
80efeaf95aSDavid Greenman #include <vm/vm_param.h>
81efeaf95aSDavid Greenman #include <vm/vm_prot.h>
82efeaf95aSDavid Greenman #include <vm/pmap.h>
83efeaf95aSDavid Greenman #include <vm/vm_map.h>
84efeaf95aSDavid Greenman #include <vm/vm_object.h>
85df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
8626f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
870d94caffSDavid Greenman #include <vm/vm_pager.h>
8805f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
89a1f6d91cSDavid Greenman #include <vm/vm_kern.h>
90efeaf95aSDavid Greenman #include <vm/vm_extern.h>
9199448ed1SJohn Dyson #include <vm/vm_zone.h>
9226f9a767SRodney W. Grimes 
93cac597e4SBruce Evans static void	vm_object_qcollapse __P((vm_object_t object));
94f6b04d2bSDavid Greenman 
95df8bae1dSRodney W. Grimes /*
96df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
97df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
98df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
99df8bae1dSRodney W. Grimes  *
100df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
101df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
102df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
103df8bae1dSRodney W. Grimes  *
104df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
105df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
106df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
107df8bae1dSRodney W. Grimes  *	lock.
108df8bae1dSRodney W. Grimes  *
109df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
110df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
111df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
112df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
113df8bae1dSRodney W. Grimes  *
114df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
115df8bae1dSRodney W. Grimes  *	modified after time of creation are:
116df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
117df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
118df8bae1dSRodney W. Grimes  *
119df8bae1dSRodney W. Grimes  */
120df8bae1dSRodney W. Grimes 
12128f8db14SBruce Evans struct object_q vm_object_list;
122303b270bSEivind Eklund static struct simplelock vm_object_list_lock;
1234de628deSBruce Evans static long vm_object_count;		/* count of all objects */
12428f8db14SBruce Evans vm_object_t kernel_object;
12528f8db14SBruce Evans vm_object_t kmem_object;
126f708ef1bSPoul-Henning Kamp static struct vm_object kernel_object_store;
127f708ef1bSPoul-Henning Kamp static struct vm_object kmem_object_store;
128aef922f5SJohn Dyson extern int vm_pageout_page_count;
129df8bae1dSRodney W. Grimes 
130f708ef1bSPoul-Henning Kamp static long object_collapses;
131f708ef1bSPoul-Henning Kamp static long object_bypasses;
1325070c7f8SJohn Dyson static int next_index;
13399448ed1SJohn Dyson static vm_zone_t obj_zone;
13499448ed1SJohn Dyson static struct vm_zone obj_zone_store;
13599448ed1SJohn Dyson #define VM_OBJECTS_INIT 256
136303b270bSEivind Eklund static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
137c0877f10SJohn Dyson static int objidnumber;
138df8bae1dSRodney W. Grimes 
1393075778bSJohn Dyson void
14024a1cce3SDavid Greenman _vm_object_allocate(type, size, object)
14124a1cce3SDavid Greenman 	objtype_t type;
142df8bae1dSRodney W. Grimes 	vm_size_t size;
143df8bae1dSRodney W. Grimes 	register vm_object_t object;
144df8bae1dSRodney W. Grimes {
14599448ed1SJohn Dyson 	int incr;
146df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
14724a1cce3SDavid Greenman 	TAILQ_INIT(&object->shadow_head);
148a1f6d91cSDavid Greenman 
14924a1cce3SDavid Greenman 	object->type = type;
150df8bae1dSRodney W. Grimes 	object->size = size;
151a1f6d91cSDavid Greenman 	object->ref_count = 1;
15224a1cce3SDavid Greenman 	object->flags = 0;
153c0877f10SJohn Dyson 	object->id = ++objidnumber;
154c0877f10SJohn Dyson 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
155069e9bc1SDoug Rabson 		vm_object_set_flag(object, OBJ_ONEMAPPING);
156867a482dSJohn Dyson 	object->behavior = OBJ_NORMAL;
157df8bae1dSRodney W. Grimes 	object->paging_in_progress = 0;
158a1f6d91cSDavid Greenman 	object->resident_page_count = 0;
159eaf13dd7SJohn Dyson 	object->cache_count = 0;
160eaf13dd7SJohn Dyson 	object->wire_count = 0;
161de5f6a77SJohn Dyson 	object->shadow_count = 0;
1625070c7f8SJohn Dyson 	object->pg_color = next_index;
16399448ed1SJohn Dyson 	if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
16499448ed1SJohn Dyson 		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
16599448ed1SJohn Dyson 	else
16699448ed1SJohn Dyson 		incr = size;
16799448ed1SJohn Dyson 	next_index = (next_index + incr) & PQ_L2_MASK;
16824a1cce3SDavid Greenman 	object->handle = NULL;
169a316d390SJohn Dyson 	object->paging_offset = (vm_ooffset_t) 0;
17024a1cce3SDavid Greenman 	object->backing_object = NULL;
171a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
172a2f4a846SJohn Dyson 	object->page_hint = NULL;
173a1f6d91cSDavid Greenman 
174a1f6d91cSDavid Greenman 	object->last_read = 0;
1752d8acc0fSJohn Dyson 	object->generation++;
176df8bae1dSRodney W. Grimes 
177df8bae1dSRodney W. Grimes 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
178df8bae1dSRodney W. Grimes 	vm_object_count++;
179df8bae1dSRodney W. Grimes }
180df8bae1dSRodney W. Grimes 
181df8bae1dSRodney W. Grimes /*
18226f9a767SRodney W. Grimes  *	vm_object_init:
18326f9a767SRodney W. Grimes  *
18426f9a767SRodney W. Grimes  *	Initialize the VM objects module.
18526f9a767SRodney W. Grimes  */
18626f9a767SRodney W. Grimes void
187a316d390SJohn Dyson vm_object_init()
18826f9a767SRodney W. Grimes {
18926f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
190996c772fSJohn Dyson 	simple_lock_init(&vm_object_list_lock);
19126f9a767SRodney W. Grimes 	vm_object_count = 0;
1920217125fSDavid Greenman 
19326f9a767SRodney W. Grimes 	kernel_object = &kernel_object_store;
194a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
19526f9a767SRodney W. Grimes 	    kernel_object);
19626f9a767SRodney W. Grimes 
19726f9a767SRodney W. Grimes 	kmem_object = &kmem_object_store;
198a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
19926f9a767SRodney W. Grimes 	    kmem_object);
20099448ed1SJohn Dyson 
20199448ed1SJohn Dyson 	obj_zone = &obj_zone_store;
20299448ed1SJohn Dyson 	zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
20399448ed1SJohn Dyson 		vm_objects_init, VM_OBJECTS_INIT);
20499448ed1SJohn Dyson }
20599448ed1SJohn Dyson 
20699448ed1SJohn Dyson void
20799448ed1SJohn Dyson vm_object_init2() {
2080a80f406SJohn Dyson 	zinitna(obj_zone, NULL, NULL, 0, 0, 0, 1);
20926f9a767SRodney W. Grimes }
21026f9a767SRodney W. Grimes 
21126f9a767SRodney W. Grimes /*
21226f9a767SRodney W. Grimes  *	vm_object_allocate:
21326f9a767SRodney W. Grimes  *
21426f9a767SRodney W. Grimes  *	Returns a new object with the given size.
21526f9a767SRodney W. Grimes  */
21626f9a767SRodney W. Grimes 
21726f9a767SRodney W. Grimes vm_object_t
21824a1cce3SDavid Greenman vm_object_allocate(type, size)
21924a1cce3SDavid Greenman 	objtype_t type;
22026f9a767SRodney W. Grimes 	vm_size_t size;
22126f9a767SRodney W. Grimes {
22226f9a767SRodney W. Grimes 	register vm_object_t result;
22399448ed1SJohn Dyson 	result = (vm_object_t) zalloc(obj_zone);
22467bf6868SJohn Dyson 
22524a1cce3SDavid Greenman 	_vm_object_allocate(type, size, result);
22626f9a767SRodney W. Grimes 
22726f9a767SRodney W. Grimes 	return (result);
22826f9a767SRodney W. Grimes }
22926f9a767SRodney W. Grimes 
23026f9a767SRodney W. Grimes 
23126f9a767SRodney W. Grimes /*
232df8bae1dSRodney W. Grimes  *	vm_object_reference:
233df8bae1dSRodney W. Grimes  *
234df8bae1dSRodney W. Grimes  *	Gets another reference to the given object.
235df8bae1dSRodney W. Grimes  */
2366476c0d2SJohn Dyson void
23726f9a767SRodney W. Grimes vm_object_reference(object)
238df8bae1dSRodney W. Grimes 	register vm_object_t object;
239df8bae1dSRodney W. Grimes {
240df8bae1dSRodney W. Grimes 	if (object == NULL)
241df8bae1dSRodney W. Grimes 		return;
24295e5e988SJohn Dyson 
24395e5e988SJohn Dyson #if defined(DIAGNOSTIC)
24495e5e988SJohn Dyson 	if (object->flags & OBJ_DEAD)
24595e5e988SJohn Dyson 		panic("vm_object_reference: attempting to reference dead obj");
24695e5e988SJohn Dyson #endif
24795e5e988SJohn Dyson 
248df8bae1dSRodney W. Grimes 	object->ref_count++;
24947221757SJohn Dyson 	if (object->type == OBJT_VNODE) {
25047221757SJohn Dyson 		while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curproc)) {
25195461b45SJohn Dyson #if !defined(MAX_PERF)
25247221757SJohn Dyson 			printf("vm_object_reference: delay in getting object\n");
25395461b45SJohn Dyson #endif
25447221757SJohn Dyson 		}
25547221757SJohn Dyson 	}
25695e5e988SJohn Dyson }
25795e5e988SJohn Dyson 
258bf27292bSJohn Dyson void
25995e5e988SJohn Dyson vm_object_vndeallocate(object)
26095e5e988SJohn Dyson 	vm_object_t object;
26195e5e988SJohn Dyson {
26295e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
26395e5e988SJohn Dyson #if defined(DIAGNOSTIC)
26495e5e988SJohn Dyson 	if (object->type != OBJT_VNODE)
26595e5e988SJohn Dyson 		panic("vm_object_vndeallocate: not a vnode object");
26695e5e988SJohn Dyson 	if (vp == NULL)
26795e5e988SJohn Dyson 		panic("vm_object_vndeallocate: missing vp");
26895e5e988SJohn Dyson 	if (object->ref_count == 0) {
26995e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
27095e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
27195e5e988SJohn Dyson 	}
27295e5e988SJohn Dyson #endif
27395e5e988SJohn Dyson 
27495e5e988SJohn Dyson 	object->ref_count--;
27547221757SJohn Dyson 	if (object->ref_count == 0) {
276bf27292bSJohn Dyson 		vp->v_flag &= ~VTEXT;
277069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_OPT);
2782be70f79SJohn Dyson 	}
27947221757SJohn Dyson 	vrele(vp);
280df8bae1dSRodney W. Grimes }
281df8bae1dSRodney W. Grimes 
282df8bae1dSRodney W. Grimes /*
283df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
284df8bae1dSRodney W. Grimes  *
285df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
286df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
287df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
288df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
289df8bae1dSRodney W. Grimes  *	may be relinquished.
290df8bae1dSRodney W. Grimes  *
291df8bae1dSRodney W. Grimes  *	No object may be locked.
292df8bae1dSRodney W. Grimes  */
29326f9a767SRodney W. Grimes void
29426f9a767SRodney W. Grimes vm_object_deallocate(object)
29526f9a767SRodney W. Grimes 	vm_object_t object;
296df8bae1dSRodney W. Grimes {
29795e5e988SJohn Dyson 	int s;
298df8bae1dSRodney W. Grimes 	vm_object_t temp;
299df8bae1dSRodney W. Grimes 
300df8bae1dSRodney W. Grimes 	while (object != NULL) {
301df8bae1dSRodney W. Grimes 
30295e5e988SJohn Dyson 		if (object->type == OBJT_VNODE) {
30395e5e988SJohn Dyson 			vm_object_vndeallocate(object);
30495e5e988SJohn Dyson 			return;
30595e5e988SJohn Dyson 		}
30695e5e988SJohn Dyson 
3072be70f79SJohn Dyson 		if (object->ref_count == 0) {
30847221757SJohn Dyson 			panic("vm_object_deallocate: object deallocated too many times: %d", object->type);
3092be70f79SJohn Dyson 		} else if (object->ref_count > 2) {
3102be70f79SJohn Dyson 			object->ref_count--;
3112be70f79SJohn Dyson 			return;
3122be70f79SJohn Dyson 		}
3132be70f79SJohn Dyson 
3142be70f79SJohn Dyson 		/*
3152be70f79SJohn Dyson 		 * Here on ref_count of one or two, which are special cases for
3162be70f79SJohn Dyson 		 * objects.
3172be70f79SJohn Dyson 		 */
318c0877f10SJohn Dyson 		if ((object->ref_count == 2) && (object->shadow_count == 0)) {
319069e9bc1SDoug Rabson 			vm_object_set_flag(object, OBJ_ONEMAPPING);
320c0877f10SJohn Dyson 			object->ref_count--;
321c0877f10SJohn Dyson 			return;
322c0877f10SJohn Dyson 		} else if ((object->ref_count == 2) && (object->shadow_count == 1)) {
323be6d5bfaSDavid Greenman 			object->ref_count--;
3242be70f79SJohn Dyson 			if ((object->handle == NULL) &&
32524a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
32624a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
327a1f6d91cSDavid Greenman 				vm_object_t robject;
32895e5e988SJohn Dyson 
329b18bfc3dSJohn Dyson 				robject = TAILQ_FIRST(&object->shadow_head);
33095e5e988SJohn Dyson #if defined(DIAGNOSTIC)
33195e5e988SJohn Dyson 				if (robject == NULL)
33295e5e988SJohn Dyson 					panic("vm_object_deallocate: ref_count: %d,"
33395e5e988SJohn Dyson 						  " shadow_count: %d",
33495e5e988SJohn Dyson 						  object->ref_count, object->shadow_count);
33595e5e988SJohn Dyson #endif
33695e5e988SJohn Dyson 				if ((robject->handle == NULL) &&
33724a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
33824a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
339a1f6d91cSDavid Greenman 
34095e5e988SJohn Dyson 					robject->ref_count++;
34195e5e988SJohn Dyson 
34295e5e988SJohn Dyson 			retry:
343ffc82b0aSJohn Dyson 					if (robject->paging_in_progress ||
344ffc82b0aSJohn Dyson 							object->paging_in_progress) {
34566095752SJohn Dyson 						vm_object_pip_sleep(robject, "objde1");
346ffc82b0aSJohn Dyson 						if (robject->paging_in_progress &&
347ffc82b0aSJohn Dyson 							robject->type == OBJT_SWAP) {
34866095752SJohn Dyson 							swap_pager_sync();
34995e5e988SJohn Dyson 							goto retry;
3500d94caffSDavid Greenman 						}
351a1f6d91cSDavid Greenman 
35266095752SJohn Dyson 						vm_object_pip_sleep(object, "objde2");
353ffc82b0aSJohn Dyson 						if (object->paging_in_progress &&
354ffc82b0aSJohn Dyson 							object->type == OBJT_SWAP) {
35566095752SJohn Dyson 							swap_pager_sync();
35666095752SJohn Dyson 						}
35795e5e988SJohn Dyson 						goto retry;
358a1f6d91cSDavid Greenman 					}
359a1f6d91cSDavid Greenman 
36095e5e988SJohn Dyson 					if( robject->ref_count == 1) {
36195e5e988SJohn Dyson 						robject->ref_count--;
362ba8da839SDavid Greenman 						object = robject;
36395e5e988SJohn Dyson 						goto doterm;
36495e5e988SJohn Dyson 					}
36595e5e988SJohn Dyson 
36695e5e988SJohn Dyson 					object = robject;
36795e5e988SJohn Dyson 					vm_object_collapse(object);
368ba8da839SDavid Greenman 					continue;
369a1f6d91cSDavid Greenman 				}
37095e5e988SJohn Dyson 			}
37195e5e988SJohn Dyson 
372a1f6d91cSDavid Greenman 			return;
373df8bae1dSRodney W. Grimes 
37495e5e988SJohn Dyson 		} else {
3751efb74fbSJohn Dyson 			object->ref_count--;
37695e5e988SJohn Dyson 			if (object->ref_count != 0)
37795e5e988SJohn Dyson 				return;
37895e5e988SJohn Dyson 		}
37995e5e988SJohn Dyson 
38095e5e988SJohn Dyson doterm:
3811efb74fbSJohn Dyson 
38224a1cce3SDavid Greenman 		temp = object->backing_object;
383de5f6a77SJohn Dyson 		if (temp) {
38424a1cce3SDavid Greenman 			TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
38595e5e988SJohn Dyson 			temp->shadow_count--;
3862d8acc0fSJohn Dyson 			if (temp->ref_count == 0)
387069e9bc1SDoug Rabson 				vm_object_clear_flag(temp, OBJ_OPT);
388eaf13dd7SJohn Dyson 			temp->generation++;
38995461b45SJohn Dyson 			object->backing_object = NULL;
390de5f6a77SJohn Dyson 		}
391df8bae1dSRodney W. Grimes 		vm_object_terminate(object);
392df8bae1dSRodney W. Grimes 		/* unlocks and deallocates object */
393df8bae1dSRodney W. Grimes 		object = temp;
394df8bae1dSRodney W. Grimes 	}
395df8bae1dSRodney W. Grimes }
396df8bae1dSRodney W. Grimes 
397df8bae1dSRodney W. Grimes /*
398df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
399df8bae1dSRodney W. Grimes  *	up all previously used resources.
400df8bae1dSRodney W. Grimes  *
401df8bae1dSRodney W. Grimes  *	The object must be locked.
402df8bae1dSRodney W. Grimes  */
40395e5e988SJohn Dyson void
40426f9a767SRodney W. Grimes vm_object_terminate(object)
405df8bae1dSRodney W. Grimes 	register vm_object_t object;
406df8bae1dSRodney W. Grimes {
4073af76890SPoul-Henning Kamp 	register vm_page_t p;
408df8bae1dSRodney W. Grimes 
40995e5e988SJohn Dyson 	/*
41095e5e988SJohn Dyson 	 * Make sure no one uses us.
41195e5e988SJohn Dyson 	 */
412069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
4133c631446SJohn Dyson 
414df8bae1dSRodney W. Grimes 	/*
415f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
416df8bae1dSRodney W. Grimes 	 */
41766095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
418df8bae1dSRodney W. Grimes 
41995e5e988SJohn Dyson #if defined(DIAGNOSTIC)
42026f9a767SRodney W. Grimes 	if (object->paging_in_progress != 0)
42195461b45SJohn Dyson 		panic("vm_object_terminate: pageout in progress");
42295e5e988SJohn Dyson #endif
42326f9a767SRodney W. Grimes 
42426f9a767SRodney W. Grimes 	/*
4250d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
4260d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
42726f9a767SRodney W. Grimes 	 */
42824a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
42995e5e988SJohn Dyson 		struct vnode *vp;
43095e5e988SJohn Dyson 
43195e5e988SJohn Dyson 		/*
43295e5e988SJohn Dyson 		 * Freeze optimized copies.
43395e5e988SJohn Dyson 		 */
43495e5e988SJohn Dyson 		vm_freeze_copyopts(object, 0, object->size);
43595e5e988SJohn Dyson 
43695e5e988SJohn Dyson 		/*
43795e5e988SJohn Dyson 		 * Clean pages and flush buffers.
43895e5e988SJohn Dyson 		 */
4398f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
44095e5e988SJohn Dyson 
44195e5e988SJohn Dyson 		vp = (struct vnode *) object->handle;
442f6b04d2bSDavid Greenman 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
443bef608bdSJohn Dyson 	}
444bef608bdSJohn Dyson 
4450b10ba98SDavid Greenman 	if (object->ref_count != 0)
4460b10ba98SDavid Greenman 		panic("vm_object_terminate: object with references, ref_count=%d", object->ref_count);
447996c772fSJohn Dyson 
4480d94caffSDavid Greenman 	/*
449356863ebSDavid Greenman 	 * Now free any remaining pages. For internal objects, this also
450356863ebSDavid Greenman 	 * removes them from paging queues. Don't free wired pages, just
451356863ebSDavid Greenman 	 * remove them from the object.
452df8bae1dSRodney W. Grimes 	 */
453b18bfc3dSJohn Dyson 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
45495461b45SJohn Dyson #if !defined(MAX_PERF)
455a2f4a846SJohn Dyson 		if (p->busy || (p->flags & PG_BUSY))
456be6d5bfaSDavid Greenman 			printf("vm_object_terminate: freeing busy page\n");
45795461b45SJohn Dyson #endif
4580b10ba98SDavid Greenman 		if (p->wire_count == 0) {
459e69763a3SDoug Rabson 			vm_page_busy(p);
460df8bae1dSRodney W. Grimes 			vm_page_free(p);
461df8bae1dSRodney W. Grimes 			cnt.v_pfree++;
4620b10ba98SDavid Greenman 		} else {
4639fcfb650SDavid Greenman 			if (!(p->flags & PG_FICTITIOUS))
4640b10ba98SDavid Greenman 				printf("vm_object_terminate: not freeing wired page; wire_count=%d\n", p->wire_count);
465356863ebSDavid Greenman 			vm_page_busy(p);
4660b10ba98SDavid Greenman 			vm_page_remove(p);
4670b10ba98SDavid Greenman 		}
468df8bae1dSRodney W. Grimes 	}
469bef608bdSJohn Dyson 
4702d8acc0fSJohn Dyson 	/*
4719fcfb650SDavid Greenman 	 * Let the pager know object is dead.
4729fcfb650SDavid Greenman 	 */
4739fcfb650SDavid Greenman 	vm_pager_deallocate(object);
4749fcfb650SDavid Greenman 
4759fcfb650SDavid Greenman 	/*
4760b10ba98SDavid Greenman 	 * Remove the object from the global object list.
4772d8acc0fSJohn Dyson 	 */
478996c772fSJohn Dyson 	simple_lock(&vm_object_list_lock);
479df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
480996c772fSJohn Dyson 	simple_unlock(&vm_object_list_lock);
4810b10ba98SDavid Greenman 
4820b10ba98SDavid Greenman 	wakeup(object);
4830b10ba98SDavid Greenman 
484df8bae1dSRodney W. Grimes 	/*
485df8bae1dSRodney W. Grimes 	 * Free the space for the object.
486df8bae1dSRodney W. Grimes 	 */
48799448ed1SJohn Dyson 	zfree(obj_zone, object);
48847221757SJohn Dyson }
489df8bae1dSRodney W. Grimes 
490df8bae1dSRodney W. Grimes /*
491df8bae1dSRodney W. Grimes  *	vm_object_page_clean
492df8bae1dSRodney W. Grimes  *
493df8bae1dSRodney W. Grimes  *	Clean all dirty pages in the specified range of object.
49426f9a767SRodney W. Grimes  *	Leaves page on whatever queue it is currently on.
49526f9a767SRodney W. Grimes  *
49626f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
49726f9a767SRodney W. Grimes  *
49826f9a767SRodney W. Grimes  *	The object must be locked.
49926f9a767SRodney W. Grimes  */
500f6b04d2bSDavid Greenman 
501f6b04d2bSDavid Greenman void
5028f9110f6SJohn Dyson vm_object_page_clean(object, start, end, flags)
503f6b04d2bSDavid Greenman 	vm_object_t object;
504a316d390SJohn Dyson 	vm_pindex_t start;
505a316d390SJohn Dyson 	vm_pindex_t end;
5068f9110f6SJohn Dyson 	int flags;
507f6b04d2bSDavid Greenman {
508bd7e5f99SJohn Dyson 	register vm_page_t p, np, tp;
509f6b04d2bSDavid Greenman 	register vm_offset_t tstart, tend;
510bd7e5f99SJohn Dyson 	vm_pindex_t pi;
511aef922f5SJohn Dyson 	int s;
51224a1cce3SDavid Greenman 	struct vnode *vp;
513aef922f5SJohn Dyson 	int runlen;
514bd7e5f99SJohn Dyson 	int maxf;
515bd7e5f99SJohn Dyson 	int chkb;
516bd7e5f99SJohn Dyson 	int maxb;
517bd7e5f99SJohn Dyson 	int i;
5188f9110f6SJohn Dyson 	int pagerflags;
519bd7e5f99SJohn Dyson 	vm_page_t maf[vm_pageout_page_count];
520bd7e5f99SJohn Dyson 	vm_page_t mab[vm_pageout_page_count];
521aef922f5SJohn Dyson 	vm_page_t ma[vm_pageout_page_count];
5222d8acc0fSJohn Dyson 	int curgeneration;
523996c772fSJohn Dyson 	struct proc *pproc = curproc;	/* XXX */
524f6b04d2bSDavid Greenman 
525aef922f5SJohn Dyson 	if (object->type != OBJT_VNODE ||
526aef922f5SJohn Dyson 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
527f6b04d2bSDavid Greenman 		return;
528f6b04d2bSDavid Greenman 
5298f9110f6SJohn Dyson 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0;
5308f9110f6SJohn Dyson 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
5318f9110f6SJohn Dyson 
53224a1cce3SDavid Greenman 	vp = object->handle;
53324a1cce3SDavid Greenman 
534069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_CLEANING);
53524a1cce3SDavid Greenman 
536f6b04d2bSDavid Greenman 	tstart = start;
537f6b04d2bSDavid Greenman 	if (end == 0) {
538f6b04d2bSDavid Greenman 		tend = object->size;
539f6b04d2bSDavid Greenman 	} else {
540f6b04d2bSDavid Greenman 		tend = end;
541f6b04d2bSDavid Greenman 	}
542eaf13dd7SJohn Dyson 
543eaf13dd7SJohn Dyson 	for(p = TAILQ_FIRST(&object->memq); p; p = TAILQ_NEXT(p, listq)) {
544e69763a3SDoug Rabson 		vm_page_flag_set(p, PG_CLEANCHK);
545eaf13dd7SJohn Dyson 		vm_page_protect(p, VM_PROT_READ);
546eaf13dd7SJohn Dyson 	}
547eaf13dd7SJohn Dyson 
548a316d390SJohn Dyson 	if ((tstart == 0) && (tend == object->size)) {
549069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
550ec4f9fb0SDavid Greenman 	}
551f6b04d2bSDavid Greenman 
552bd7e5f99SJohn Dyson rescan:
5532d8acc0fSJohn Dyson 	curgeneration = object->generation;
5542d8acc0fSJohn Dyson 
555b18bfc3dSJohn Dyson 	for(p = TAILQ_FIRST(&object->memq); p; p = np) {
556b18bfc3dSJohn Dyson 		np = TAILQ_NEXT(p, listq);
557bd7e5f99SJohn Dyson 
558bd7e5f99SJohn Dyson 		pi = p->pindex;
559bd7e5f99SJohn Dyson 		if (((p->flags & PG_CLEANCHK) == 0) ||
560bd7e5f99SJohn Dyson 			(pi < tstart) || (pi >= tend) ||
5615070c7f8SJohn Dyson 			(p->valid == 0) ||
5625070c7f8SJohn Dyson 			((p->queue - p->pc) == PQ_CACHE)) {
563e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
564aef922f5SJohn Dyson 			continue;
565f6b04d2bSDavid Greenman 		}
566f6b04d2bSDavid Greenman 
567bd7e5f99SJohn Dyson 		vm_page_test_dirty(p);
568bd7e5f99SJohn Dyson 		if ((p->dirty & p->valid) == 0) {
569e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
570bd7e5f99SJohn Dyson 			continue;
571bd7e5f99SJohn Dyson 		}
572ec4f9fb0SDavid Greenman 
573b18bfc3dSJohn Dyson 		s = splvm();
5742d8acc0fSJohn Dyson 		while ((p->flags & PG_BUSY) || p->busy) {
575e69763a3SDoug Rabson 			vm_page_flag_set(p, PG_WANTED | PG_REFERENCED);
576aef922f5SJohn Dyson 			tsleep(p, PVM, "vpcwai", 0);
5772d8acc0fSJohn Dyson 			if (object->generation != curgeneration) {
578f6b04d2bSDavid Greenman 				splx(s);
579bd7e5f99SJohn Dyson 				goto rescan;
580f6b04d2bSDavid Greenman 			}
5812d8acc0fSJohn Dyson 		}
582f6b04d2bSDavid Greenman 
583bd7e5f99SJohn Dyson 		maxf = 0;
584bd7e5f99SJohn Dyson 		for(i=1;i<vm_pageout_page_count;i++) {
585bd7e5f99SJohn Dyson 			if (tp = vm_page_lookup(object, pi + i)) {
586bd7e5f99SJohn Dyson 				if ((tp->flags & PG_BUSY) ||
587ffc82b0aSJohn Dyson 					(tp->flags & PG_CLEANCHK) == 0 ||
588ffc82b0aSJohn Dyson 					(tp->busy != 0))
589bd7e5f99SJohn Dyson 					break;
5905070c7f8SJohn Dyson 				if((tp->queue - tp->pc) == PQ_CACHE) {
591e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
5923077a9c2SJohn Dyson 					break;
5933077a9c2SJohn Dyson 				}
594bd7e5f99SJohn Dyson 				vm_page_test_dirty(tp);
595bd7e5f99SJohn Dyson 				if ((tp->dirty & tp->valid) == 0) {
596e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
597bd7e5f99SJohn Dyson 					break;
598bd7e5f99SJohn Dyson 				}
599bd7e5f99SJohn Dyson 				maf[ i - 1 ] = tp;
600bd7e5f99SJohn Dyson 				maxf++;
601bd7e5f99SJohn Dyson 				continue;
602bd7e5f99SJohn Dyson 			}
603bd7e5f99SJohn Dyson 			break;
604bd7e5f99SJohn Dyson 		}
605aef922f5SJohn Dyson 
606bd7e5f99SJohn Dyson 		maxb = 0;
607bd7e5f99SJohn Dyson 		chkb = vm_pageout_page_count -  maxf;
608bd7e5f99SJohn Dyson 		if (chkb) {
609bd7e5f99SJohn Dyson 			for(i = 1; i < chkb;i++) {
610bd7e5f99SJohn Dyson 				if (tp = vm_page_lookup(object, pi - i)) {
611bd7e5f99SJohn Dyson 					if ((tp->flags & PG_BUSY) ||
612ffc82b0aSJohn Dyson 						(tp->flags & PG_CLEANCHK) == 0 ||
613ffc82b0aSJohn Dyson 						(tp->busy != 0))
614bd7e5f99SJohn Dyson 						break;
6155070c7f8SJohn Dyson 					if((tp->queue - tp->pc) == PQ_CACHE) {
616e69763a3SDoug Rabson 						vm_page_flag_clear(tp, PG_CLEANCHK);
6173077a9c2SJohn Dyson 						break;
6183077a9c2SJohn Dyson 					}
619bd7e5f99SJohn Dyson 					vm_page_test_dirty(tp);
620bd7e5f99SJohn Dyson 					if ((tp->dirty & tp->valid) == 0) {
621e69763a3SDoug Rabson 						vm_page_flag_clear(tp, PG_CLEANCHK);
622bd7e5f99SJohn Dyson 						break;
623bd7e5f99SJohn Dyson 					}
624bd7e5f99SJohn Dyson 					mab[ i - 1 ] = tp;
625bd7e5f99SJohn Dyson 					maxb++;
626bd7e5f99SJohn Dyson 					continue;
627bd7e5f99SJohn Dyson 				}
628bd7e5f99SJohn Dyson 				break;
629bd7e5f99SJohn Dyson 			}
630bd7e5f99SJohn Dyson 		}
631bd7e5f99SJohn Dyson 
632bd7e5f99SJohn Dyson 		for(i=0;i<maxb;i++) {
633bd7e5f99SJohn Dyson 			int index = (maxb - i) - 1;
634bd7e5f99SJohn Dyson 			ma[index] = mab[i];
635e69763a3SDoug Rabson 			vm_page_flag_clear(ma[index], PG_CLEANCHK);
636bd7e5f99SJohn Dyson 		}
637e69763a3SDoug Rabson 		vm_page_flag_clear(p, PG_CLEANCHK);
638bd7e5f99SJohn Dyson 		ma[maxb] = p;
639bd7e5f99SJohn Dyson 		for(i=0;i<maxf;i++) {
640bd7e5f99SJohn Dyson 			int index = (maxb + i) + 1;
641bd7e5f99SJohn Dyson 			ma[index] = maf[i];
642e69763a3SDoug Rabson 			vm_page_flag_clear(ma[index], PG_CLEANCHK);
643f6b04d2bSDavid Greenman 		}
644bd7e5f99SJohn Dyson 		runlen = maxb + maxf + 1;
645cf2819ccSJohn Dyson 
646f35329acSJohn Dyson 		splx(s);
6478f9110f6SJohn Dyson 		vm_pageout_flush(ma, runlen, pagerflags);
648cf2819ccSJohn Dyson 		for (i = 0; i<runlen; i++) {
649cf2819ccSJohn Dyson 			if (ma[i]->valid & ma[i]->dirty) {
650cf2819ccSJohn Dyson 				vm_page_protect(ma[i], VM_PROT_READ);
651e69763a3SDoug Rabson 				vm_page_flag_set(ma[i], PG_CLEANCHK);
652cf2819ccSJohn Dyson 			}
653cf2819ccSJohn Dyson 		}
6542d8acc0fSJohn Dyson 		if (object->generation != curgeneration)
655bd7e5f99SJohn Dyson 			goto rescan;
656f6b04d2bSDavid Greenman 	}
657aef922f5SJohn Dyson 
658cf2819ccSJohn Dyson 	VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
659aef922f5SJohn Dyson 
660069e9bc1SDoug Rabson 	vm_object_clear_flag(object, OBJ_CLEANING);
661f5cf85d4SDavid Greenman 	return;
66226f9a767SRodney W. Grimes }
663df8bae1dSRodney W. Grimes 
664f708ef1bSPoul-Henning Kamp #ifdef not_used
665f708ef1bSPoul-Henning Kamp /* XXX I cannot tell if this should be an exported symbol */
666df8bae1dSRodney W. Grimes /*
667df8bae1dSRodney W. Grimes  *	vm_object_deactivate_pages
668df8bae1dSRodney W. Grimes  *
669df8bae1dSRodney W. Grimes  *	Deactivate all pages in the specified object.  (Keep its pages
670df8bae1dSRodney W. Grimes  *	in memory even though it is no longer referenced.)
671df8bae1dSRodney W. Grimes  *
672df8bae1dSRodney W. Grimes  *	The object must be locked.
673df8bae1dSRodney W. Grimes  */
674f708ef1bSPoul-Henning Kamp static void
675df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object)
676df8bae1dSRodney W. Grimes 	register vm_object_t object;
677df8bae1dSRodney W. Grimes {
678df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
679df8bae1dSRodney W. Grimes 
680b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
681b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
682df8bae1dSRodney W. Grimes 		vm_page_deactivate(p);
683df8bae1dSRodney W. Grimes 	}
684df8bae1dSRodney W. Grimes }
685f708ef1bSPoul-Henning Kamp #endif
686df8bae1dSRodney W. Grimes 
687df8bae1dSRodney W. Grimes /*
688df8bae1dSRodney W. Grimes  *	vm_object_pmap_copy:
689df8bae1dSRodney W. Grimes  *
690df8bae1dSRodney W. Grimes  *	Makes all physical pages in the specified
691df8bae1dSRodney W. Grimes  *	object range copy-on-write.  No writeable
692df8bae1dSRodney W. Grimes  *	references to these pages should remain.
693df8bae1dSRodney W. Grimes  *
694df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
695df8bae1dSRodney W. Grimes  */
6960d94caffSDavid Greenman void
6970d94caffSDavid Greenman vm_object_pmap_copy(object, start, end)
698df8bae1dSRodney W. Grimes 	register vm_object_t object;
699a316d390SJohn Dyson 	register vm_pindex_t start;
700a316d390SJohn Dyson 	register vm_pindex_t end;
701df8bae1dSRodney W. Grimes {
702df8bae1dSRodney W. Grimes 	register vm_page_t p;
703df8bae1dSRodney W. Grimes 
704aef922f5SJohn Dyson 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
705df8bae1dSRodney W. Grimes 		return;
706df8bae1dSRodney W. Grimes 
707b5b40fa6SJohn Dyson 	for (p = TAILQ_FIRST(&object->memq);
708b5b40fa6SJohn Dyson 		p != NULL;
709b5b40fa6SJohn Dyson 		p = TAILQ_NEXT(p, listq)) {
710f919ebdeSDavid Greenman 		vm_page_protect(p, VM_PROT_READ);
711df8bae1dSRodney W. Grimes 	}
712aef922f5SJohn Dyson 
713069e9bc1SDoug Rabson 	vm_object_clear_flag(object, OBJ_WRITEABLE);
714df8bae1dSRodney W. Grimes }
715df8bae1dSRodney W. Grimes 
716df8bae1dSRodney W. Grimes /*
7171efb74fbSJohn Dyson  * Same as vm_object_pmap_copy_1, except range checking really
7181efb74fbSJohn Dyson  * works, and is meant for small sections of an object.
7191efb74fbSJohn Dyson  */
7201efb74fbSJohn Dyson void
7211efb74fbSJohn Dyson vm_object_pmap_copy_1(object, start, end)
7221efb74fbSJohn Dyson 	register vm_object_t object;
7231efb74fbSJohn Dyson 	register vm_pindex_t start;
7241efb74fbSJohn Dyson 	register vm_pindex_t end;
7251efb74fbSJohn Dyson {
7261efb74fbSJohn Dyson 	vm_pindex_t idx;
7271efb74fbSJohn Dyson 	register vm_page_t p;
7281efb74fbSJohn Dyson 
7291efb74fbSJohn Dyson 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
7301efb74fbSJohn Dyson 		return;
7311efb74fbSJohn Dyson 
7321efb74fbSJohn Dyson 	for (idx = start; idx < end; idx++) {
7331efb74fbSJohn Dyson 		p = vm_page_lookup(object, idx);
7341efb74fbSJohn Dyson 		if (p == NULL)
7351efb74fbSJohn Dyson 			continue;
7361efb74fbSJohn Dyson 		vm_page_protect(p, VM_PROT_READ);
7371efb74fbSJohn Dyson 	}
7381efb74fbSJohn Dyson }
7391efb74fbSJohn Dyson 
7401efb74fbSJohn Dyson /*
741df8bae1dSRodney W. Grimes  *	vm_object_pmap_remove:
742df8bae1dSRodney W. Grimes  *
743df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
744df8bae1dSRodney W. Grimes  *	object range from all physical maps.
745df8bae1dSRodney W. Grimes  *
746df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
747df8bae1dSRodney W. Grimes  */
74826f9a767SRodney W. Grimes void
74926f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end)
750df8bae1dSRodney W. Grimes 	register vm_object_t object;
751a316d390SJohn Dyson 	register vm_pindex_t start;
752a316d390SJohn Dyson 	register vm_pindex_t end;
753df8bae1dSRodney W. Grimes {
754df8bae1dSRodney W. Grimes 	register vm_page_t p;
755df8bae1dSRodney W. Grimes 	if (object == NULL)
756df8bae1dSRodney W. Grimes 		return;
757b5b40fa6SJohn Dyson 	for (p = TAILQ_FIRST(&object->memq);
758b5b40fa6SJohn Dyson 		p != NULL;
759b5b40fa6SJohn Dyson 		p = TAILQ_NEXT(p, listq)) {
760bd7e5f99SJohn Dyson 		if (p->pindex >= start && p->pindex < end)
761f919ebdeSDavid Greenman 			vm_page_protect(p, VM_PROT_NONE);
76226f9a767SRodney W. Grimes 	}
7636e20a165SJohn Dyson 	if ((start == 0) && (object->size == end))
764069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_WRITEABLE);
76526f9a767SRodney W. Grimes }
766df8bae1dSRodney W. Grimes 
767df8bae1dSRodney W. Grimes /*
768867a482dSJohn Dyson  *	vm_object_madvise:
769867a482dSJohn Dyson  *
770867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
771867a482dSJohn Dyson  */
772867a482dSJohn Dyson void
773867a482dSJohn Dyson vm_object_madvise(object, pindex, count, advise)
774867a482dSJohn Dyson 	vm_object_t object;
775867a482dSJohn Dyson 	vm_pindex_t pindex;
776867a482dSJohn Dyson 	int count;
777867a482dSJohn Dyson 	int advise;
778867a482dSJohn Dyson {
7796e20a165SJohn Dyson 	int s;
7806e20a165SJohn Dyson 	vm_pindex_t end, tpindex;
7816e20a165SJohn Dyson 	vm_object_t tobject;
782867a482dSJohn Dyson 	vm_page_t m;
783867a482dSJohn Dyson 
784867a482dSJohn Dyson 	if (object == NULL)
785867a482dSJohn Dyson 		return;
786867a482dSJohn Dyson 
787867a482dSJohn Dyson 	end = pindex + count;
788867a482dSJohn Dyson 
789867a482dSJohn Dyson 	for (; pindex < end; pindex += 1) {
7906e20a165SJohn Dyson 
7916e20a165SJohn Dyson relookup:
7926e20a165SJohn Dyson 		tobject = object;
7936e20a165SJohn Dyson 		tpindex = pindex;
7946e20a165SJohn Dyson shadowlookup:
7956e20a165SJohn Dyson 		m = vm_page_lookup(tobject, tpindex);
7966e20a165SJohn Dyson 		if (m == NULL) {
7976e20a165SJohn Dyson 			if (tobject->type != OBJT_DEFAULT) {
7986e20a165SJohn Dyson 				continue;
7996e20a165SJohn Dyson 			}
8006e20a165SJohn Dyson 
8016e20a165SJohn Dyson 			tobject = tobject->backing_object;
8026e20a165SJohn Dyson 			if ((tobject == NULL) || (tobject->ref_count != 1)) {
8036e20a165SJohn Dyson 				continue;
8046e20a165SJohn Dyson 			}
8056e20a165SJohn Dyson 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
8066e20a165SJohn Dyson 			goto shadowlookup;
8076e20a165SJohn Dyson 		}
808867a482dSJohn Dyson 
809867a482dSJohn Dyson 		/*
810867a482dSJohn Dyson 		 * If the page is busy or not in a normal active state,
811867a482dSJohn Dyson 		 * we skip it.  Things can break if we mess with pages
812867a482dSJohn Dyson 		 * in any of the below states.
813867a482dSJohn Dyson 		 */
8146e20a165SJohn Dyson 		if (m->hold_count || m->wire_count ||
8156e20a165SJohn Dyson 			m->valid != VM_PAGE_BITS_ALL) {
816867a482dSJohn Dyson 			continue;
8176e20a165SJohn Dyson 		}
8186e20a165SJohn Dyson 
819ffc82b0aSJohn Dyson  		if (vm_page_sleep(m, "madvpo", &m->busy))
8206e20a165SJohn Dyson   			goto relookup;
821867a482dSJohn Dyson 
822867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
823867a482dSJohn Dyson 			vm_page_activate(m);
8246e20a165SJohn Dyson 		} else if (advise == MADV_DONTNEED) {
825867a482dSJohn Dyson 			vm_page_deactivate(m);
8260a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
8276e20a165SJohn Dyson 			pmap_clear_modify(VM_PAGE_TO_PHYS(m));
8286e20a165SJohn Dyson 			m->dirty = 0;
8290a47b48bSJohn Dyson 			/*
8306e20a165SJohn Dyson 			 * Force a demand zero if attempt to read from swap.
8316e20a165SJohn Dyson 			 * We currently don't handle vnode files correctly,
8326e20a165SJohn Dyson 			 * and will reread stale contents unnecessarily.
8330a47b48bSJohn Dyson 			 */
8340a47b48bSJohn Dyson 			if (object->type == OBJT_SWAP)
8356e20a165SJohn Dyson 				swap_pager_dmzspace(tobject, m->pindex, 1);
836867a482dSJohn Dyson 		}
837867a482dSJohn Dyson 	}
838867a482dSJohn Dyson }
839867a482dSJohn Dyson 
840867a482dSJohn Dyson /*
841df8bae1dSRodney W. Grimes  *	vm_object_shadow:
842df8bae1dSRodney W. Grimes  *
843df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
844df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
845df8bae1dSRodney W. Grimes  *	object reference is deallocated.
846df8bae1dSRodney W. Grimes  *
847df8bae1dSRodney W. Grimes  *	The new object and offset into that object
848df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
849df8bae1dSRodney W. Grimes  */
850df8bae1dSRodney W. Grimes 
85126f9a767SRodney W. Grimes void
85226f9a767SRodney W. Grimes vm_object_shadow(object, offset, length)
853df8bae1dSRodney W. Grimes 	vm_object_t *object;	/* IN/OUT */
854a316d390SJohn Dyson 	vm_ooffset_t *offset;	/* IN/OUT */
855df8bae1dSRodney W. Grimes 	vm_size_t length;
856df8bae1dSRodney W. Grimes {
857df8bae1dSRodney W. Grimes 	register vm_object_t source;
858df8bae1dSRodney W. Grimes 	register vm_object_t result;
859df8bae1dSRodney W. Grimes 
860df8bae1dSRodney W. Grimes 	source = *object;
861df8bae1dSRodney W. Grimes 
862df8bae1dSRodney W. Grimes 	/*
863df8bae1dSRodney W. Grimes 	 * Allocate a new object with the given length
864df8bae1dSRodney W. Grimes 	 */
865df8bae1dSRodney W. Grimes 
86624a1cce3SDavid Greenman 	if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
867df8bae1dSRodney W. Grimes 		panic("vm_object_shadow: no object for shadowing");
868df8bae1dSRodney W. Grimes 
869df8bae1dSRodney W. Grimes 	/*
8700d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
8710d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
8720d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
8730d94caffSDavid Greenman 	 * of reference count.
874df8bae1dSRodney W. Grimes 	 */
87524a1cce3SDavid Greenman 	result->backing_object = source;
876de5f6a77SJohn Dyson 	if (source) {
877de5f6a77SJohn Dyson 		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
878069e9bc1SDoug Rabson 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
879eaf13dd7SJohn Dyson 		source->shadow_count++;
880eaf13dd7SJohn Dyson 		source->generation++;
881de5f6a77SJohn Dyson 	}
882df8bae1dSRodney W. Grimes 
883df8bae1dSRodney W. Grimes 	/*
8840d94caffSDavid Greenman 	 * Store the offset into the source object, and fix up the offset into
8850d94caffSDavid Greenman 	 * the new object.
886df8bae1dSRodney W. Grimes 	 */
887df8bae1dSRodney W. Grimes 
88824a1cce3SDavid Greenman 	result->backing_object_offset = *offset;
889df8bae1dSRodney W. Grimes 
890df8bae1dSRodney W. Grimes 	/*
891df8bae1dSRodney W. Grimes 	 * Return the new things
892df8bae1dSRodney W. Grimes 	 */
893df8bae1dSRodney W. Grimes 
894df8bae1dSRodney W. Grimes 	*offset = 0;
895df8bae1dSRodney W. Grimes 	*object = result;
896df8bae1dSRodney W. Grimes }
897df8bae1dSRodney W. Grimes 
898df8bae1dSRodney W. Grimes 
899df8bae1dSRodney W. Grimes /*
9002fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
9012fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
9022fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
9032fe6e4d7SDavid Greenman  */
9042fe6e4d7SDavid Greenman static void
9052fe6e4d7SDavid Greenman vm_object_qcollapse(object)
9062fe6e4d7SDavid Greenman 	register vm_object_t object;
9072fe6e4d7SDavid Greenman {
9082fe6e4d7SDavid Greenman 	register vm_object_t backing_object;
909a316d390SJohn Dyson 	register vm_pindex_t backing_offset_index, paging_offset_index;
910a316d390SJohn Dyson 	vm_pindex_t backing_object_paging_offset_index;
911a316d390SJohn Dyson 	vm_pindex_t new_pindex;
9122fe6e4d7SDavid Greenman 	register vm_page_t p, pp;
9132fe6e4d7SDavid Greenman 	register vm_size_t size;
9142fe6e4d7SDavid Greenman 
91524a1cce3SDavid Greenman 	backing_object = object->backing_object;
9162fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
9172fe6e4d7SDavid Greenman 		return;
9182fe6e4d7SDavid Greenman 
919010cf3b9SDavid Greenman 	backing_object->ref_count += 2;
920010cf3b9SDavid Greenman 
921a316d390SJohn Dyson 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
922a316d390SJohn Dyson 	backing_object_paging_offset_index = OFF_TO_IDX(backing_object->paging_offset);
923a316d390SJohn Dyson 	paging_offset_index = OFF_TO_IDX(object->paging_offset);
9242fe6e4d7SDavid Greenman 	size = object->size;
925b18bfc3dSJohn Dyson 	p = TAILQ_FIRST(&backing_object->memq);
9262fe6e4d7SDavid Greenman 	while (p) {
9272fe6e4d7SDavid Greenman 		vm_page_t next;
9280d94caffSDavid Greenman 
929b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
930bd7e5f99SJohn Dyson 		if ((p->flags & (PG_BUSY | PG_FICTITIOUS)) ||
9315070c7f8SJohn Dyson 		    !p->valid || p->hold_count || p->wire_count || p->busy) {
9322fe6e4d7SDavid Greenman 			p = next;
9332fe6e4d7SDavid Greenman 			continue;
9342fe6e4d7SDavid Greenman 		}
935e69763a3SDoug Rabson 		vm_page_busy(p);
936eaf13dd7SJohn Dyson 
937a316d390SJohn Dyson 		new_pindex = p->pindex - backing_offset_index;
938a316d390SJohn Dyson 		if (p->pindex < backing_offset_index ||
939a316d390SJohn Dyson 		    new_pindex >= size) {
94024a1cce3SDavid Greenman 			if (backing_object->type == OBJT_SWAP)
94124a1cce3SDavid Greenman 				swap_pager_freespace(backing_object,
942a316d390SJohn Dyson 				    backing_object_paging_offset_index+p->pindex,
943a316d390SJohn Dyson 				    1);
944b18bfc3dSJohn Dyson 			vm_page_protect(p, VM_PROT_NONE);
9452fe6e4d7SDavid Greenman 			vm_page_free(p);
9462fe6e4d7SDavid Greenman 		} else {
947a316d390SJohn Dyson 			pp = vm_page_lookup(object, new_pindex);
948eaf13dd7SJohn Dyson 			if (pp != NULL ||
949eaf13dd7SJohn Dyson 				(object->type == OBJT_SWAP && vm_pager_has_page(object,
950a316d390SJohn Dyson 				    paging_offset_index + new_pindex, NULL, NULL))) {
95124a1cce3SDavid Greenman 				if (backing_object->type == OBJT_SWAP)
95224a1cce3SDavid Greenman 					swap_pager_freespace(backing_object,
953a316d390SJohn Dyson 					    backing_object_paging_offset_index + p->pindex, 1);
954b18bfc3dSJohn Dyson 				vm_page_protect(p, VM_PROT_NONE);
9552fe6e4d7SDavid Greenman 				vm_page_free(p);
9562fe6e4d7SDavid Greenman 			} else {
95724a1cce3SDavid Greenman 				if (backing_object->type == OBJT_SWAP)
95824a1cce3SDavid Greenman 					swap_pager_freespace(backing_object,
959a316d390SJohn Dyson 					    backing_object_paging_offset_index + p->pindex, 1);
960eaf13dd7SJohn Dyson 
961edd97f3aSJohn Dyson 				if ((p->queue - p->pc) == PQ_CACHE)
962edd97f3aSJohn Dyson 					vm_page_deactivate(p);
963edd97f3aSJohn Dyson 				else
9646e20a165SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
965edd97f3aSJohn Dyson 
966edd97f3aSJohn Dyson 				vm_page_rename(p, object, new_pindex);
9679b4814bbSDavid Greenman 				p->dirty = VM_PAGE_BITS_ALL;
9682fe6e4d7SDavid Greenman 			}
9692fe6e4d7SDavid Greenman 		}
9702fe6e4d7SDavid Greenman 		p = next;
9712fe6e4d7SDavid Greenman 	}
972010cf3b9SDavid Greenman 	backing_object->ref_count -= 2;
9732fe6e4d7SDavid Greenman }
9742fe6e4d7SDavid Greenman 
975df8bae1dSRodney W. Grimes /*
976df8bae1dSRodney W. Grimes  *	vm_object_collapse:
977df8bae1dSRodney W. Grimes  *
978df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
979df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
980df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
981df8bae1dSRodney W. Grimes  */
98226f9a767SRodney W. Grimes void
98326f9a767SRodney W. Grimes vm_object_collapse(object)
98424a1cce3SDavid Greenman 	vm_object_t object;
985df8bae1dSRodney W. Grimes 
986df8bae1dSRodney W. Grimes {
98724a1cce3SDavid Greenman 	vm_object_t backing_object;
988a316d390SJohn Dyson 	vm_ooffset_t backing_offset;
98924a1cce3SDavid Greenman 	vm_size_t size;
990a316d390SJohn Dyson 	vm_pindex_t new_pindex, backing_offset_index;
99124a1cce3SDavid Greenman 	vm_page_t p, pp;
992df8bae1dSRodney W. Grimes 
993df8bae1dSRodney W. Grimes 	while (TRUE) {
994df8bae1dSRodney W. Grimes 		/*
995df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
996df8bae1dSRodney W. Grimes 		 *
9970d94caffSDavid Greenman 		 * The object exists and no pages in it are currently being paged
9980d94caffSDavid Greenman 		 * out.
999df8bae1dSRodney W. Grimes 		 */
10002fe6e4d7SDavid Greenman 		if (object == NULL)
1001df8bae1dSRodney W. Grimes 			return;
1002df8bae1dSRodney W. Grimes 
1003b9921222SDavid Greenman 		/*
1004b9921222SDavid Greenman 		 * Make sure there is a backing object.
1005b9921222SDavid Greenman 		 */
100624a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
1007df8bae1dSRodney W. Grimes 			return;
1008df8bae1dSRodney W. Grimes 
1009f919ebdeSDavid Greenman 		/*
1010f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
101124a1cce3SDavid Greenman 		 * not collapsable.
1012f919ebdeSDavid Greenman 		 */
101324a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
101424a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
101524a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1016f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
101724a1cce3SDavid Greenman 		    object->handle != NULL ||
101824a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
101924a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
102024a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
10219b4814bbSDavid Greenman 			return;
102224a1cce3SDavid Greenman 		}
10239b4814bbSDavid Greenman 
1024f919ebdeSDavid Greenman 		if (object->paging_in_progress != 0 ||
1025f919ebdeSDavid Greenman 		    backing_object->paging_in_progress != 0) {
1026b9921222SDavid Greenman 			vm_object_qcollapse(object);
1027df8bae1dSRodney W. Grimes 			return;
1028df8bae1dSRodney W. Grimes 		}
1029f919ebdeSDavid Greenman 
103026f9a767SRodney W. Grimes 		/*
10310d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
10320d94caffSDavid Greenman 		 * the parent is the only reference to it) or (perhaps) remove
10330d94caffSDavid Greenman 		 * the parent's reference to it.
1034df8bae1dSRodney W. Grimes 		 */
1035df8bae1dSRodney W. Grimes 
103624a1cce3SDavid Greenman 		backing_offset = object->backing_object_offset;
1037a316d390SJohn Dyson 		backing_offset_index = OFF_TO_IDX(backing_offset);
1038df8bae1dSRodney W. Grimes 		size = object->size;
1039df8bae1dSRodney W. Grimes 
1040df8bae1dSRodney W. Grimes 		/*
10410d94caffSDavid Greenman 		 * If there is exactly one reference to the backing object, we
10420d94caffSDavid Greenman 		 * can collapse it into the parent.
1043df8bae1dSRodney W. Grimes 		 */
1044df8bae1dSRodney W. Grimes 
1045df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1046df8bae1dSRodney W. Grimes 
1047069e9bc1SDoug Rabson 			vm_object_set_flag(backing_object, OBJ_DEAD);
1048df8bae1dSRodney W. Grimes 			/*
1049df8bae1dSRodney W. Grimes 			 * We can collapse the backing object.
1050df8bae1dSRodney W. Grimes 			 *
10510d94caffSDavid Greenman 			 * Move all in-memory pages from backing_object to the
10520d94caffSDavid Greenman 			 * parent.  Pages that have been paged out will be
10530d94caffSDavid Greenman 			 * overwritten by any of the parent's pages that
10540d94caffSDavid Greenman 			 * shadow them.
1055df8bae1dSRodney W. Grimes 			 */
1056df8bae1dSRodney W. Grimes 
1057b18bfc3dSJohn Dyson 			while ((p = TAILQ_FIRST(&backing_object->memq)) != 0) {
105826f9a767SRodney W. Grimes 
1059a316d390SJohn Dyson 				new_pindex = p->pindex - backing_offset_index;
1060e69763a3SDoug Rabson 				vm_page_busy(p);
1061df8bae1dSRodney W. Grimes 
1062df8bae1dSRodney W. Grimes 				/*
10630d94caffSDavid Greenman 				 * If the parent has a page here, or if this
10640d94caffSDavid Greenman 				 * page falls outside the parent, dispose of
10650d94caffSDavid Greenman 				 * it.
1066df8bae1dSRodney W. Grimes 				 *
1067df8bae1dSRodney W. Grimes 				 * Otherwise, move it as planned.
1068df8bae1dSRodney W. Grimes 				 */
1069df8bae1dSRodney W. Grimes 
1070a316d390SJohn Dyson 				if (p->pindex < backing_offset_index ||
1071a316d390SJohn Dyson 				    new_pindex >= size) {
1072f919ebdeSDavid Greenman 					vm_page_protect(p, VM_PROT_NONE);
1073df8bae1dSRodney W. Grimes 					vm_page_free(p);
1074df8bae1dSRodney W. Grimes 				} else {
1075a316d390SJohn Dyson 					pp = vm_page_lookup(object, new_pindex);
107624a1cce3SDavid Greenman 					if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object,
1077a316d390SJohn Dyson 					    OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL))) {
1078f919ebdeSDavid Greenman 						vm_page_protect(p, VM_PROT_NONE);
1079df8bae1dSRodney W. Grimes 						vm_page_free(p);
108026f9a767SRodney W. Grimes 					} else {
1081edd97f3aSJohn Dyson 						if ((p->queue - p->pc) == PQ_CACHE)
1082edd97f3aSJohn Dyson 							vm_page_deactivate(p);
1083edd97f3aSJohn Dyson 						else
10846e20a165SJohn Dyson 							vm_page_protect(p, VM_PROT_NONE);
1085a316d390SJohn Dyson 						vm_page_rename(p, object, new_pindex);
10866e20a165SJohn Dyson 						p->dirty = VM_PAGE_BITS_ALL;
1087df8bae1dSRodney W. Grimes 					}
1088df8bae1dSRodney W. Grimes 				}
1089df8bae1dSRodney W. Grimes 			}
1090df8bae1dSRodney W. Grimes 
1091df8bae1dSRodney W. Grimes 			/*
1092df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1093df8bae1dSRodney W. Grimes 			 */
1094df8bae1dSRodney W. Grimes 
109524a1cce3SDavid Greenman 			if (backing_object->type == OBJT_SWAP) {
1096d474eaaaSDoug Rabson 				vm_object_pip_add(backing_object, 1);
109724a1cce3SDavid Greenman 				if (object->type == OBJT_SWAP) {
1098d474eaaaSDoug Rabson 					vm_object_pip_add(object, 1);
109926f9a767SRodney W. Grimes 					/*
110026f9a767SRodney W. Grimes 					 * copy shadow object pages into ours
11010d94caffSDavid Greenman 					 * and destroy unneeded pages in
11020d94caffSDavid Greenman 					 * shadow object.
110326f9a767SRodney W. Grimes 					 */
110426f9a767SRodney W. Grimes 					swap_pager_copy(
1105a316d390SJohn Dyson 					    backing_object,
1106a316d390SJohn Dyson 					    OFF_TO_IDX(backing_object->paging_offset),
1107a316d390SJohn Dyson 					    object,
1108a316d390SJohn Dyson 					    OFF_TO_IDX(object->paging_offset),
1109c0877f10SJohn Dyson 					    OFF_TO_IDX(object->backing_object_offset), TRUE);
1110f919ebdeSDavid Greenman 					vm_object_pip_wakeup(object);
111126f9a767SRodney W. Grimes 				} else {
1112d474eaaaSDoug Rabson 					vm_object_pip_add(object, 1);
111326f9a767SRodney W. Grimes 					/*
111424a1cce3SDavid Greenman 					 * move the shadow backing_object's pager data to
111524a1cce3SDavid Greenman 					 * "object" and convert "object" type to OBJT_SWAP.
111626f9a767SRodney W. Grimes 					 */
111724a1cce3SDavid Greenman 					object->type = OBJT_SWAP;
11182a4895f4SDavid Greenman 					object->un_pager.swp.swp_nblocks =
11192a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_nblocks;
11202a4895f4SDavid Greenman 					object->un_pager.swp.swp_allocsize =
11212a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_allocsize;
11222a4895f4SDavid Greenman 					object->un_pager.swp.swp_blocks =
11232a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_blocks;
11242a4895f4SDavid Greenman 					object->un_pager.swp.swp_poip =		/* XXX */
11252a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_poip;
112626f9a767SRodney W. Grimes 					object->paging_offset = backing_object->paging_offset + backing_offset;
112724a1cce3SDavid Greenman 					TAILQ_INSERT_TAIL(&swap_pager_un_object_list, object, pager_object_list);
112824a1cce3SDavid Greenman 
112924a1cce3SDavid Greenman 					/*
113024a1cce3SDavid Greenman 					 * Convert backing object from OBJT_SWAP to
113124a1cce3SDavid Greenman 					 * OBJT_DEFAULT. XXX - only the TAILQ_REMOVE is
113224a1cce3SDavid Greenman 					 * actually necessary.
113324a1cce3SDavid Greenman 					 */
113424a1cce3SDavid Greenman 					backing_object->type = OBJT_DEFAULT;
113524a1cce3SDavid Greenman 					TAILQ_REMOVE(&swap_pager_un_object_list, backing_object, pager_object_list);
113626f9a767SRodney W. Grimes 					/*
113726f9a767SRodney W. Grimes 					 * free unnecessary blocks
113826f9a767SRodney W. Grimes 					 */
1139a316d390SJohn Dyson 					swap_pager_freespace(object, 0,
1140a316d390SJohn Dyson 						OFF_TO_IDX(object->paging_offset));
1141f919ebdeSDavid Greenman 					vm_object_pip_wakeup(object);
1142c0503609SDavid Greenman 				}
1143c0503609SDavid Greenman 
1144f919ebdeSDavid Greenman 				vm_object_pip_wakeup(backing_object);
1145c0503609SDavid Greenman 			}
1146df8bae1dSRodney W. Grimes 			/*
1147df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
114824a1cce3SDavid Greenman 			 * Note that the reference to backing_object->backing_object
1149df8bae1dSRodney W. Grimes 			 * moves from within backing_object to within object.
1150df8bae1dSRodney W. Grimes 			 */
1151df8bae1dSRodney W. Grimes 
115224a1cce3SDavid Greenman 			TAILQ_REMOVE(&object->backing_object->shadow_head, object,
115324a1cce3SDavid Greenman 			    shadow_list);
1154eaf13dd7SJohn Dyson 			object->backing_object->shadow_count--;
1155eaf13dd7SJohn Dyson 			object->backing_object->generation++;
1156de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
115724a1cce3SDavid Greenman 				TAILQ_REMOVE(&backing_object->backing_object->shadow_head,
115824a1cce3SDavid Greenman 				    backing_object, shadow_list);
1159eaf13dd7SJohn Dyson 				backing_object->backing_object->shadow_count--;
1160eaf13dd7SJohn Dyson 				backing_object->backing_object->generation++;
1161de5f6a77SJohn Dyson 			}
116224a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
1163de5f6a77SJohn Dyson 			if (object->backing_object) {
116424a1cce3SDavid Greenman 				TAILQ_INSERT_TAIL(&object->backing_object->shadow_head,
116524a1cce3SDavid Greenman 				    object, shadow_list);
1166eaf13dd7SJohn Dyson 				object->backing_object->shadow_count++;
1167eaf13dd7SJohn Dyson 				object->backing_object->generation++;
1168de5f6a77SJohn Dyson 			}
11692fe6e4d7SDavid Greenman 
117024a1cce3SDavid Greenman 			object->backing_object_offset += backing_object->backing_object_offset;
1171df8bae1dSRodney W. Grimes 			/*
1172df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1173df8bae1dSRodney W. Grimes 			 *
11740d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
11750d94caffSDavid Greenman 			 * and no object references within it, all that is
11760d94caffSDavid Greenman 			 * necessary is to dispose of it.
1177df8bae1dSRodney W. Grimes 			 */
1178df8bae1dSRodney W. Grimes 
1179df8bae1dSRodney W. Grimes 			TAILQ_REMOVE(&vm_object_list, backing_object,
1180df8bae1dSRodney W. Grimes 			    object_list);
1181df8bae1dSRodney W. Grimes 			vm_object_count--;
1182df8bae1dSRodney W. Grimes 
118399448ed1SJohn Dyson 			zfree(obj_zone, backing_object);
1184df8bae1dSRodney W. Grimes 
1185df8bae1dSRodney W. Grimes 			object_collapses++;
11860d94caffSDavid Greenman 		} else {
118795e5e988SJohn Dyson 			vm_object_t new_backing_object;
1188df8bae1dSRodney W. Grimes 			/*
1189df8bae1dSRodney W. Grimes 			 * If all of the pages in the backing object are
11900d94caffSDavid Greenman 			 * shadowed by the parent object, the parent object no
11910d94caffSDavid Greenman 			 * longer has to shadow the backing object; it can
11920d94caffSDavid Greenman 			 * shadow the next one in the chain.
1193df8bae1dSRodney W. Grimes 			 *
11940d94caffSDavid Greenman 			 * The backing object must not be paged out - we'd have
11950d94caffSDavid Greenman 			 * to check all of the paged-out pages, as well.
1196df8bae1dSRodney W. Grimes 			 */
1197df8bae1dSRodney W. Grimes 
119824a1cce3SDavid Greenman 			if (backing_object->type != OBJT_DEFAULT) {
1199df8bae1dSRodney W. Grimes 				return;
1200df8bae1dSRodney W. Grimes 			}
1201df8bae1dSRodney W. Grimes 			/*
12020d94caffSDavid Greenman 			 * Should have a check for a 'small' number of pages
12030d94caffSDavid Greenman 			 * here.
1204df8bae1dSRodney W. Grimes 			 */
1205df8bae1dSRodney W. Grimes 
1206eaf13dd7SJohn Dyson 			for (p = TAILQ_FIRST(&backing_object->memq); p;
1207eaf13dd7SJohn Dyson 					p = TAILQ_NEXT(p, listq)) {
1208eaf13dd7SJohn Dyson 
1209a316d390SJohn Dyson 				new_pindex = p->pindex - backing_offset_index;
1210e69763a3SDoug Rabson 				vm_page_busy(p);
1211df8bae1dSRodney W. Grimes 
1212df8bae1dSRodney W. Grimes 				/*
12130d94caffSDavid Greenman 				 * If the parent has a page here, or if this
12140d94caffSDavid Greenman 				 * page falls outside the parent, keep going.
1215df8bae1dSRodney W. Grimes 				 *
12160d94caffSDavid Greenman 				 * Otherwise, the backing_object must be left in
12170d94caffSDavid Greenman 				 * the chain.
1218df8bae1dSRodney W. Grimes 				 */
1219df8bae1dSRodney W. Grimes 
1220a316d390SJohn Dyson 				if (p->pindex >= backing_offset_index &&
1221a316d390SJohn Dyson 					new_pindex <= size) {
122224a1cce3SDavid Greenman 
1223a316d390SJohn Dyson 					pp = vm_page_lookup(object, new_pindex);
122424a1cce3SDavid Greenman 
1225eaf13dd7SJohn Dyson 					if ((pp == NULL) || (pp->flags & PG_BUSY) || pp->busy) {
1226e69763a3SDoug Rabson 						vm_page_wakeup(p);
1227eaf13dd7SJohn Dyson 						return;
1228eaf13dd7SJohn Dyson 					}
1229eaf13dd7SJohn Dyson 
1230e69763a3SDoug Rabson 					vm_page_busy(pp);
1231eaf13dd7SJohn Dyson 					if ((pp->valid == 0) &&
1232a316d390SJohn Dyson 				   	    !vm_pager_has_page(object, OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL)) {
1233df8bae1dSRodney W. Grimes 						/*
12340d94caffSDavid Greenman 						 * Page still needed. Can't go any
12350d94caffSDavid Greenman 						 * further.
1236df8bae1dSRodney W. Grimes 						 */
1237e69763a3SDoug Rabson 						vm_page_wakeup(pp);
1238e69763a3SDoug Rabson 						vm_page_wakeup(p);
1239df8bae1dSRodney W. Grimes 						return;
1240df8bae1dSRodney W. Grimes 					}
1241e69763a3SDoug Rabson 					vm_page_wakeup(pp);
1242df8bae1dSRodney W. Grimes 				}
1243e69763a3SDoug Rabson 				vm_page_wakeup(p);
124424a1cce3SDavid Greenman 			}
1245df8bae1dSRodney W. Grimes 
1246df8bae1dSRodney W. Grimes 			/*
12470d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
12480d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
12490d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1250df8bae1dSRodney W. Grimes 			 */
1251df8bae1dSRodney W. Grimes 
125295e5e988SJohn Dyson 			TAILQ_REMOVE(&backing_object->shadow_head,
125324a1cce3SDavid Greenman 			    object, shadow_list);
1254eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
1255eaf13dd7SJohn Dyson 			backing_object->generation++;
125695e5e988SJohn Dyson 
125795e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
125895e5e988SJohn Dyson 			if (object->backing_object = new_backing_object) {
125995e5e988SJohn Dyson 				vm_object_reference(new_backing_object);
126095e5e988SJohn Dyson 				TAILQ_INSERT_TAIL(&new_backing_object->shadow_head,
126124a1cce3SDavid Greenman 				    object, shadow_list);
1262eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1263eaf13dd7SJohn Dyson 				new_backing_object->generation++;
126495e5e988SJohn Dyson 				object->backing_object_offset +=
126595e5e988SJohn Dyson 					backing_object->backing_object_offset;
1266de5f6a77SJohn Dyson 			}
1267df8bae1dSRodney W. Grimes 
1268df8bae1dSRodney W. Grimes 			/*
12690d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
12700d94caffSDavid Greenman 			 * its ref_count was at least 2, it will not vanish;
1271eaf13dd7SJohn Dyson 			 * so we don't need to call vm_object_deallocate, but
1272eaf13dd7SJohn Dyson 			 * we do anyway.
1273df8bae1dSRodney W. Grimes 			 */
127495e5e988SJohn Dyson 			vm_object_deallocate(backing_object);
1275df8bae1dSRodney W. Grimes 			object_bypasses++;
1276df8bae1dSRodney W. Grimes 		}
1277df8bae1dSRodney W. Grimes 
1278df8bae1dSRodney W. Grimes 		/*
1279df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1280df8bae1dSRodney W. Grimes 		 */
1281df8bae1dSRodney W. Grimes 	}
1282df8bae1dSRodney W. Grimes }
1283df8bae1dSRodney W. Grimes 
1284df8bae1dSRodney W. Grimes /*
1285df8bae1dSRodney W. Grimes  *	vm_object_page_remove: [internal]
1286df8bae1dSRodney W. Grimes  *
1287df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
1288df8bae1dSRodney W. Grimes  *	object range from the object's list of pages.
1289df8bae1dSRodney W. Grimes  *
1290df8bae1dSRodney W. Grimes  *	The object must be locked.
1291df8bae1dSRodney W. Grimes  */
129226f9a767SRodney W. Grimes void
12937c1f6cedSDavid Greenman vm_object_page_remove(object, start, end, clean_only)
1294df8bae1dSRodney W. Grimes 	register vm_object_t object;
1295a316d390SJohn Dyson 	register vm_pindex_t start;
1296a316d390SJohn Dyson 	register vm_pindex_t end;
12977c1f6cedSDavid Greenman 	boolean_t clean_only;
1298df8bae1dSRodney W. Grimes {
1299df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
1300a316d390SJohn Dyson 	unsigned int size;
130195e5e988SJohn Dyson 	int s, all;
1302df8bae1dSRodney W. Grimes 
1303df8bae1dSRodney W. Grimes 	if (object == NULL)
1304df8bae1dSRodney W. Grimes 		return;
1305df8bae1dSRodney W. Grimes 
130695e5e988SJohn Dyson 	all = ((end == 0) && (start == 0));
130795e5e988SJohn Dyson 
1308d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
130926f9a767SRodney W. Grimes again:
131026f9a767SRodney W. Grimes 	size = end - start;
131195e5e988SJohn Dyson 	if (all || size > 4 || size >= object->size / 4) {
1312b18bfc3dSJohn Dyson 		for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1313b18bfc3dSJohn Dyson 			next = TAILQ_NEXT(p, listq);
131495e5e988SJohn Dyson 			if (all || ((start <= p->pindex) && (p->pindex < end))) {
1315bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1316bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1317ce65e68cSDavid Greenman 					if (!clean_only)
1318bd7e5f99SJohn Dyson 						p->valid = 0;
13190d94caffSDavid Greenman 					continue;
13200d94caffSDavid Greenman 				}
13210891ef4cSJohn Dyson 
1322b18bfc3dSJohn Dyson 				/*
1323b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1324b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1325b18bfc3dSJohn Dyson 				 */
1326ffc82b0aSJohn Dyson 
1327ffc82b0aSJohn Dyson  				if (vm_page_sleep(p, "vmopar", &p->busy))
132826f9a767SRodney W. Grimes  					goto again;
13290891ef4cSJohn Dyson 
13308f9110f6SJohn Dyson 				if (clean_only && p->valid) {
13317c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
13327c1f6cedSDavid Greenman 					if (p->valid & p->dirty)
13337c1f6cedSDavid Greenman 						continue;
13347c1f6cedSDavid Greenman 				}
1335eaf13dd7SJohn Dyson 
1336e69763a3SDoug Rabson 				vm_page_busy(p);
1337f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
1338df8bae1dSRodney W. Grimes 				vm_page_free(p);
133926f9a767SRodney W. Grimes 			}
134026f9a767SRodney W. Grimes 		}
134126f9a767SRodney W. Grimes 	} else {
134226f9a767SRodney W. Grimes 		while (size > 0) {
1343bd7e5f99SJohn Dyson 			if ((p = vm_page_lookup(object, start)) != 0) {
1344eaf13dd7SJohn Dyson 
1345bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1346bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1347ce65e68cSDavid Greenman 					if (!clean_only)
1348ce65e68cSDavid Greenman 						p->valid = 0;
1349bd7e5f99SJohn Dyson 					start += 1;
1350bd7e5f99SJohn Dyson 					size -= 1;
1351bd7e5f99SJohn Dyson 					continue;
13520d94caffSDavid Greenman 				}
1353eaf13dd7SJohn Dyson 
1354b18bfc3dSJohn Dyson 				/*
1355b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1356b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1357b18bfc3dSJohn Dyson 				 */
1358ffc82b0aSJohn Dyson  				if (vm_page_sleep(p, "vmopar", &p->busy))
135926f9a767SRodney W. Grimes 					goto again;
1360eaf13dd7SJohn Dyson 
13618f9110f6SJohn Dyson 				if (clean_only && p->valid) {
13627c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
1363bd7e5f99SJohn Dyson 					if (p->valid & p->dirty) {
1364bd7e5f99SJohn Dyson 						start += 1;
1365bd7e5f99SJohn Dyson 						size -= 1;
13667c1f6cedSDavid Greenman 						continue;
13677c1f6cedSDavid Greenman 					}
1368bd7e5f99SJohn Dyson 				}
1369eaf13dd7SJohn Dyson 
1370e69763a3SDoug Rabson 				vm_page_busy(p);
1371f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
137226f9a767SRodney W. Grimes 				vm_page_free(p);
137326f9a767SRodney W. Grimes 			}
1374a316d390SJohn Dyson 			start += 1;
1375a316d390SJohn Dyson 			size -= 1;
1376df8bae1dSRodney W. Grimes 		}
1377df8bae1dSRodney W. Grimes 	}
1378f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
1379c0503609SDavid Greenman }
1380df8bae1dSRodney W. Grimes 
1381df8bae1dSRodney W. Grimes /*
1382df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1383df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1384df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1385df8bae1dSRodney W. Grimes  *
1386df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1387df8bae1dSRodney W. Grimes  *
1388df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1389df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1390df8bae1dSRodney W. Grimes  *
1391df8bae1dSRodney W. Grimes  *	Parameters:
1392df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1393df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1394df8bae1dSRodney W. Grimes  *		next_object	Second object into coalesce
1395df8bae1dSRodney W. Grimes  *		next_offset	Offset into next_object
1396df8bae1dSRodney W. Grimes  *
1397df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
1398df8bae1dSRodney W. Grimes  *		next_size	Size of reference to next_object
1399df8bae1dSRodney W. Grimes  *
1400df8bae1dSRodney W. Grimes  *	Conditions:
1401df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1402df8bae1dSRodney W. Grimes  */
14030d94caffSDavid Greenman boolean_t
1404a316d390SJohn Dyson vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size)
1405df8bae1dSRodney W. Grimes 	register vm_object_t prev_object;
1406a316d390SJohn Dyson 	vm_pindex_t prev_pindex;
1407df8bae1dSRodney W. Grimes 	vm_size_t prev_size, next_size;
1408df8bae1dSRodney W. Grimes {
1409df8bae1dSRodney W. Grimes 	vm_size_t newsize;
1410df8bae1dSRodney W. Grimes 
1411df8bae1dSRodney W. Grimes 	if (prev_object == NULL) {
1412df8bae1dSRodney W. Grimes 		return (TRUE);
1413df8bae1dSRodney W. Grimes 	}
1414df8bae1dSRodney W. Grimes 
141530dcfc09SJohn Dyson 	if (prev_object->type != OBJT_DEFAULT) {
141630dcfc09SJohn Dyson 		return (FALSE);
141730dcfc09SJohn Dyson 	}
141830dcfc09SJohn Dyson 
1419df8bae1dSRodney W. Grimes 	/*
1420df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1421df8bae1dSRodney W. Grimes 	 */
1422df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1423df8bae1dSRodney W. Grimes 
1424df8bae1dSRodney W. Grimes 	/*
14250d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
14260d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
14270d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1428df8bae1dSRodney W. Grimes 	 */
1429df8bae1dSRodney W. Grimes 
14308cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
1431df8bae1dSRodney W. Grimes 		return (FALSE);
1432df8bae1dSRodney W. Grimes 	}
1433a316d390SJohn Dyson 
1434a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
1435a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
14368cc7e047SJohn Dyson 
14378cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
14388cc7e047SJohn Dyson 	    (prev_object->size != prev_pindex + prev_size)) {
14398cc7e047SJohn Dyson 		return (FALSE);
14408cc7e047SJohn Dyson 	}
14418cc7e047SJohn Dyson 
1442df8bae1dSRodney W. Grimes 	/*
14430d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
14440d94caffSDavid Greenman 	 * deallocation.
1445df8bae1dSRodney W. Grimes 	 */
1446df8bae1dSRodney W. Grimes 
1447df8bae1dSRodney W. Grimes 	vm_object_page_remove(prev_object,
1448a316d390SJohn Dyson 	    prev_pindex + prev_size,
1449a316d390SJohn Dyson 	    prev_pindex + prev_size + next_size, FALSE);
1450df8bae1dSRodney W. Grimes 
1451df8bae1dSRodney W. Grimes 	/*
1452df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1453df8bae1dSRodney W. Grimes 	 */
1454a316d390SJohn Dyson 	newsize = prev_pindex + prev_size + next_size;
1455df8bae1dSRodney W. Grimes 	if (newsize > prev_object->size)
1456df8bae1dSRodney W. Grimes 		prev_object->size = newsize;
1457df8bae1dSRodney W. Grimes 
1458df8bae1dSRodney W. Grimes 	return (TRUE);
1459df8bae1dSRodney W. Grimes }
1460df8bae1dSRodney W. Grimes 
1461c7c34a24SBruce Evans #include "opt_ddb.h"
1462c3cb3e12SDavid Greenman #ifdef DDB
1463c7c34a24SBruce Evans #include <sys/kernel.h>
1464c7c34a24SBruce Evans 
1465c7c34a24SBruce Evans #include <machine/cons.h>
1466c7c34a24SBruce Evans 
1467c7c34a24SBruce Evans #include <ddb/ddb.h>
1468c7c34a24SBruce Evans 
1469c7c34a24SBruce Evans static int	_vm_object_in_map __P((vm_map_t map, vm_object_t object,
1470c7c34a24SBruce Evans 				       vm_map_entry_t entry));
1471c7c34a24SBruce Evans static int	vm_object_in_map __P((vm_object_t object));
1472c3cb3e12SDavid Greenman 
1473cac597e4SBruce Evans static int
1474a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry)
1475a1f6d91cSDavid Greenman 	vm_map_t map;
1476a1f6d91cSDavid Greenman 	vm_object_t object;
1477a1f6d91cSDavid Greenman 	vm_map_entry_t entry;
1478a1f6d91cSDavid Greenman {
1479a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1480a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1481a1f6d91cSDavid Greenman 	vm_object_t obj;
1482a1f6d91cSDavid Greenman 	int entcount;
1483a1f6d91cSDavid Greenman 
1484a1f6d91cSDavid Greenman 	if (map == 0)
1485a1f6d91cSDavid Greenman 		return 0;
1486a1f6d91cSDavid Greenman 
1487a1f6d91cSDavid Greenman 	if (entry == 0) {
1488a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1489a1f6d91cSDavid Greenman 		entcount = map->nentries;
1490a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1491a1f6d91cSDavid Greenman 			if( _vm_object_in_map(map, object, tmpe)) {
1492a1f6d91cSDavid Greenman 				return 1;
1493a1f6d91cSDavid Greenman 			}
1494a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1495a1f6d91cSDavid Greenman 		}
1496afa07f7eSJohn Dyson 	} else if (entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) {
1497a1f6d91cSDavid Greenman 		tmpm = entry->object.share_map;
1498a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1499a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1500a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1501a1f6d91cSDavid Greenman 			if( _vm_object_in_map(tmpm, object, tmpe)) {
1502a1f6d91cSDavid Greenman 				return 1;
1503a1f6d91cSDavid Greenman 			}
1504a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1505a1f6d91cSDavid Greenman 		}
1506a1f6d91cSDavid Greenman 	} else if (obj = entry->object.vm_object) {
150724a1cce3SDavid Greenman 		for(; obj; obj=obj->backing_object)
1508a1f6d91cSDavid Greenman 			if( obj == object) {
1509a1f6d91cSDavid Greenman 				return 1;
1510a1f6d91cSDavid Greenman 			}
1511a1f6d91cSDavid Greenman 	}
1512a1f6d91cSDavid Greenman 	return 0;
1513a1f6d91cSDavid Greenman }
1514a1f6d91cSDavid Greenman 
1515cac597e4SBruce Evans static int
1516a1f6d91cSDavid Greenman vm_object_in_map( object)
1517a1f6d91cSDavid Greenman 	vm_object_t object;
1518a1f6d91cSDavid Greenman {
1519a1f6d91cSDavid Greenman 	struct proc *p;
15201b67ec6dSJeffrey Hsu 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1521a1f6d91cSDavid Greenman 		if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1522a1f6d91cSDavid Greenman 			continue;
1523a1f6d91cSDavid Greenman 		if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1524a1f6d91cSDavid Greenman 			return 1;
1525a1f6d91cSDavid Greenman 	}
1526a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kernel_map, object, 0))
1527a1f6d91cSDavid Greenman 		return 1;
1528a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kmem_map, object, 0))
1529a1f6d91cSDavid Greenman 		return 1;
1530a1f6d91cSDavid Greenman 	if( _vm_object_in_map( pager_map, object, 0))
1531a1f6d91cSDavid Greenman 		return 1;
1532a1f6d91cSDavid Greenman 	if( _vm_object_in_map( buffer_map, object, 0))
1533a1f6d91cSDavid Greenman 		return 1;
1534a1f6d91cSDavid Greenman 	if( _vm_object_in_map( io_map, object, 0))
1535a1f6d91cSDavid Greenman 		return 1;
1536a1f6d91cSDavid Greenman 	if( _vm_object_in_map( phys_map, object, 0))
1537a1f6d91cSDavid Greenman 		return 1;
1538a1f6d91cSDavid Greenman 	if( _vm_object_in_map( mb_map, object, 0))
1539a1f6d91cSDavid Greenman 		return 1;
1540a1f6d91cSDavid Greenman 	if( _vm_object_in_map( u_map, object, 0))
1541a1f6d91cSDavid Greenman 		return 1;
1542a1f6d91cSDavid Greenman 	return 0;
1543a1f6d91cSDavid Greenman }
1544a1f6d91cSDavid Greenman 
1545c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
1546f708ef1bSPoul-Henning Kamp {
1547a1f6d91cSDavid Greenman 	vm_object_t object;
1548a1f6d91cSDavid Greenman 
1549a1f6d91cSDavid Greenman 	/*
1550a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
1551a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
1552a1f6d91cSDavid Greenman 	 */
1553b18bfc3dSJohn Dyson 	for (object = TAILQ_FIRST(&vm_object_list);
1554a1f6d91cSDavid Greenman 			object != NULL;
1555b18bfc3dSJohn Dyson 			object = TAILQ_NEXT(object, object_list)) {
155624a1cce3SDavid Greenman 		if (object->handle == NULL &&
155724a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1558a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
1559c7c34a24SBruce Evans 				db_printf("vmochk: internal obj has zero ref count: %d\n",
1560a1f6d91cSDavid Greenman 					object->size);
1561a1f6d91cSDavid Greenman 			}
1562a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
1563fc62ef1fSBruce Evans 				db_printf(
1564fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
1565fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
1566fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
1567fc62ef1fSBruce Evans 				    (u_long)object->size,
1568fc62ef1fSBruce Evans 				    (void *)object->backing_object);
1569a1f6d91cSDavid Greenman 			}
1570a1f6d91cSDavid Greenman 		}
1571a1f6d91cSDavid Greenman 	}
1572a1f6d91cSDavid Greenman }
1573a1f6d91cSDavid Greenman 
157426f9a767SRodney W. Grimes /*
1575df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
1576df8bae1dSRodney W. Grimes  */
1577c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
1578df8bae1dSRodney W. Grimes {
1579c7c34a24SBruce Evans 	/* XXX convert args. */
1580c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
1581c7c34a24SBruce Evans 	boolean_t full = have_addr;
1582c7c34a24SBruce Evans 
1583df8bae1dSRodney W. Grimes 	register vm_page_t p;
1584df8bae1dSRodney W. Grimes 
1585c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
1586c7c34a24SBruce Evans #define	count	was_count
1587c7c34a24SBruce Evans 
1588df8bae1dSRodney W. Grimes 	register int count;
1589df8bae1dSRodney W. Grimes 
1590df8bae1dSRodney W. Grimes 	if (object == NULL)
1591df8bae1dSRodney W. Grimes 		return;
1592df8bae1dSRodney W. Grimes 
1593eb95adefSBruce Evans 	db_iprintf(
1594eb95adefSBruce Evans 	    "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
1595eb95adefSBruce Evans 	    object, (int)object->type, (u_long)object->size,
1596eb95adefSBruce Evans 	    object->resident_page_count, object->ref_count, object->flags);
1597eb95adefSBruce Evans 	/*
1598eb95adefSBruce Evans 	 * XXX no %qd in kernel.  Truncate object->paging_offset and
1599eb95adefSBruce Evans 	 * object->backing_object_offset.
1600eb95adefSBruce Evans 	 */
1601eb95adefSBruce Evans 	db_iprintf(" sref=%d, offset=0x%lx, backing_object(%d)=(%p)+0x%lx\n",
1602eb95adefSBruce Evans 	    object->shadow_count, (long)object->paging_offset,
1603eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
1604eb95adefSBruce Evans 	    object->backing_object, (long)object->backing_object_offset);
1605df8bae1dSRodney W. Grimes 
1606df8bae1dSRodney W. Grimes 	if (!full)
1607df8bae1dSRodney W. Grimes 		return;
1608df8bae1dSRodney W. Grimes 
1609c7c34a24SBruce Evans 	db_indent += 2;
1610df8bae1dSRodney W. Grimes 	count = 0;
1611b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = TAILQ_NEXT(p, listq)) {
1612df8bae1dSRodney W. Grimes 		if (count == 0)
1613c7c34a24SBruce Evans 			db_iprintf("memory:=");
1614df8bae1dSRodney W. Grimes 		else if (count == 6) {
1615c7c34a24SBruce Evans 			db_printf("\n");
1616c7c34a24SBruce Evans 			db_iprintf(" ...");
1617df8bae1dSRodney W. Grimes 			count = 0;
1618df8bae1dSRodney W. Grimes 		} else
1619c7c34a24SBruce Evans 			db_printf(",");
1620df8bae1dSRodney W. Grimes 		count++;
1621df8bae1dSRodney W. Grimes 
1622c7c34a24SBruce Evans 		db_printf("(off=0x%lx,page=0x%lx)",
1623a316d390SJohn Dyson 		    (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
1624df8bae1dSRodney W. Grimes 	}
1625df8bae1dSRodney W. Grimes 	if (count != 0)
1626c7c34a24SBruce Evans 		db_printf("\n");
1627c7c34a24SBruce Evans 	db_indent -= 2;
1628df8bae1dSRodney W. Grimes }
16295070c7f8SJohn Dyson 
1630c7c34a24SBruce Evans /* XXX. */
1631c7c34a24SBruce Evans #undef count
1632c7c34a24SBruce Evans 
1633c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
16345070c7f8SJohn Dyson void
1635c7c34a24SBruce Evans vm_object_print(addr, have_addr, count, modif)
1636ecbb00a2SDoug Rabson         /* db_expr_t */ long addr;
1637c7c34a24SBruce Evans 	boolean_t have_addr;
1638ecbb00a2SDoug Rabson 	/* db_expr_t */ long count;
1639c7c34a24SBruce Evans 	char *modif;
1640c7c34a24SBruce Evans {
1641c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
1642c7c34a24SBruce Evans }
1643c7c34a24SBruce Evans 
1644c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
16455070c7f8SJohn Dyson {
16465070c7f8SJohn Dyson 	vm_object_t object;
16475070c7f8SJohn Dyson 	int nl = 0;
16485070c7f8SJohn Dyson 	int c;
16495070c7f8SJohn Dyson 	for (object = TAILQ_FIRST(&vm_object_list);
16505070c7f8SJohn Dyson 			object != NULL;
16515070c7f8SJohn Dyson 			object = TAILQ_NEXT(object, object_list)) {
16525070c7f8SJohn Dyson 		vm_pindex_t idx, fidx;
16535070c7f8SJohn Dyson 		vm_pindex_t osize;
16545070c7f8SJohn Dyson 		vm_offset_t pa = -1, padiff;
16555070c7f8SJohn Dyson 		int rcount;
16565070c7f8SJohn Dyson 		vm_page_t m;
16575070c7f8SJohn Dyson 
1658fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
16595070c7f8SJohn Dyson 		if ( nl > 18) {
16605070c7f8SJohn Dyson 			c = cngetc();
16615070c7f8SJohn Dyson 			if (c != ' ')
16625070c7f8SJohn Dyson 				return;
16635070c7f8SJohn Dyson 			nl = 0;
16645070c7f8SJohn Dyson 		}
16655070c7f8SJohn Dyson 		nl++;
16665070c7f8SJohn Dyson 		rcount = 0;
16675070c7f8SJohn Dyson 		fidx = 0;
16685070c7f8SJohn Dyson 		osize = object->size;
16695070c7f8SJohn Dyson 		if (osize > 128)
16705070c7f8SJohn Dyson 			osize = 128;
16715070c7f8SJohn Dyson 		for(idx=0;idx<osize;idx++) {
16725070c7f8SJohn Dyson 			m = vm_page_lookup(object, idx);
16735070c7f8SJohn Dyson 			if (m == NULL) {
16745070c7f8SJohn Dyson 				if (rcount) {
16755070c7f8SJohn Dyson 					db_printf(" index(%d)run(%d)pa(0x%x)\n",
16765070c7f8SJohn Dyson 						fidx, rcount, pa);
16775070c7f8SJohn Dyson 					if ( nl > 18) {
16785070c7f8SJohn Dyson 						c = cngetc();
16795070c7f8SJohn Dyson 						if (c != ' ')
16805070c7f8SJohn Dyson 							return;
16815070c7f8SJohn Dyson 						nl = 0;
16825070c7f8SJohn Dyson 					}
16835070c7f8SJohn Dyson 					nl++;
16845070c7f8SJohn Dyson 					rcount = 0;
16855070c7f8SJohn Dyson 				}
16865070c7f8SJohn Dyson 				continue;
16875070c7f8SJohn Dyson 			}
16885070c7f8SJohn Dyson 
16895070c7f8SJohn Dyson 
16905070c7f8SJohn Dyson 			if (rcount &&
16915070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
16925070c7f8SJohn Dyson 				++rcount;
16935070c7f8SJohn Dyson 				continue;
16945070c7f8SJohn Dyson 			}
16955070c7f8SJohn Dyson 			if (rcount) {
16965070c7f8SJohn Dyson 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
16975070c7f8SJohn Dyson 				padiff >>= PAGE_SHIFT;
16985070c7f8SJohn Dyson 				padiff &= PQ_L2_MASK;
16995070c7f8SJohn Dyson 				if (padiff == 0) {
17005070c7f8SJohn Dyson 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
17015070c7f8SJohn Dyson 					++rcount;
17025070c7f8SJohn Dyson 					continue;
17035070c7f8SJohn Dyson 				}
17045070c7f8SJohn Dyson 				db_printf(" index(%d)run(%d)pa(0x%x)", fidx, rcount, pa);
17055070c7f8SJohn Dyson 				db_printf("pd(%d)\n", padiff);
17065070c7f8SJohn Dyson 				if ( nl > 18) {
17075070c7f8SJohn Dyson 					c = cngetc();
17085070c7f8SJohn Dyson 					if (c != ' ')
17095070c7f8SJohn Dyson 						return;
17105070c7f8SJohn Dyson 					nl = 0;
17115070c7f8SJohn Dyson 				}
17125070c7f8SJohn Dyson 				nl++;
17135070c7f8SJohn Dyson 			}
17145070c7f8SJohn Dyson 			fidx = idx;
17155070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
17165070c7f8SJohn Dyson 			rcount = 1;
17175070c7f8SJohn Dyson 		}
17185070c7f8SJohn Dyson 		if (rcount) {
17195070c7f8SJohn Dyson 			db_printf(" index(%d)run(%d)pa(0x%x)\n", fidx, rcount, pa);
17205070c7f8SJohn Dyson 			if ( nl > 18) {
17215070c7f8SJohn Dyson 				c = cngetc();
17225070c7f8SJohn Dyson 				if (c != ' ')
17235070c7f8SJohn Dyson 					return;
17245070c7f8SJohn Dyson 				nl = 0;
17255070c7f8SJohn Dyson 			}
17265070c7f8SJohn Dyson 			nl++;
17275070c7f8SJohn Dyson 		}
17285070c7f8SJohn Dyson 	}
17295070c7f8SJohn Dyson }
1730c3cb3e12SDavid Greenman #endif /* DDB */
1731