xref: /freebsd/sys/vm/vm_object.c (revision fc62ef1fb597fc0f390ede5f3a968a396c3d0df5)
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  *
64fc62ef1fSBruce Evans  * $Id: vm_object.c,v 1.123 1998/06/21 14:53:44 bde 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));
9495461b45SJohn Dyson static void vm_object_dispose __P((vm_object_t));
95f6b04d2bSDavid Greenman 
96df8bae1dSRodney W. Grimes /*
97df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
98df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
99df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
100df8bae1dSRodney W. Grimes  *
101df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
102df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
103df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
104df8bae1dSRodney W. Grimes  *
105df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
106df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
107df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
108df8bae1dSRodney W. Grimes  *	lock.
109df8bae1dSRodney W. Grimes  *
110df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
111df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
112df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
113df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
114df8bae1dSRodney W. Grimes  *
115df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
116df8bae1dSRodney W. Grimes  *	modified after time of creation are:
117df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
118df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
119df8bae1dSRodney W. Grimes  *
120df8bae1dSRodney W. Grimes  */
121df8bae1dSRodney W. Grimes 
12228f8db14SBruce Evans struct object_q vm_object_list;
123303b270bSEivind Eklund static struct simplelock vm_object_list_lock;
1244de628deSBruce Evans static long vm_object_count;		/* count of all objects */
12528f8db14SBruce Evans vm_object_t kernel_object;
12628f8db14SBruce Evans vm_object_t kmem_object;
127f708ef1bSPoul-Henning Kamp static struct vm_object kernel_object_store;
128f708ef1bSPoul-Henning Kamp static struct vm_object kmem_object_store;
129aef922f5SJohn Dyson extern int vm_pageout_page_count;
130df8bae1dSRodney W. Grimes 
131f708ef1bSPoul-Henning Kamp static long object_collapses;
132f708ef1bSPoul-Henning Kamp static long object_bypasses;
1335070c7f8SJohn Dyson static int next_index;
13499448ed1SJohn Dyson static vm_zone_t obj_zone;
13599448ed1SJohn Dyson static struct vm_zone obj_zone_store;
13699448ed1SJohn Dyson #define VM_OBJECTS_INIT 256
137303b270bSEivind Eklund static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
138c0877f10SJohn Dyson static int objidnumber;
139df8bae1dSRodney W. Grimes 
1403075778bSJohn Dyson void
14124a1cce3SDavid Greenman _vm_object_allocate(type, size, object)
14224a1cce3SDavid Greenman 	objtype_t type;
143df8bae1dSRodney W. Grimes 	vm_size_t size;
144df8bae1dSRodney W. Grimes 	register vm_object_t object;
145df8bae1dSRodney W. Grimes {
14699448ed1SJohn Dyson 	int incr;
147df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
14824a1cce3SDavid Greenman 	TAILQ_INIT(&object->shadow_head);
149a1f6d91cSDavid Greenman 
15024a1cce3SDavid Greenman 	object->type = type;
151df8bae1dSRodney W. Grimes 	object->size = size;
152a1f6d91cSDavid Greenman 	object->ref_count = 1;
15324a1cce3SDavid Greenman 	object->flags = 0;
154c0877f10SJohn Dyson 	object->id = ++objidnumber;
155c0877f10SJohn Dyson 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
156c0877f10SJohn Dyson 		object->flags |= OBJ_ONEMAPPING;
157867a482dSJohn Dyson 	object->behavior = OBJ_NORMAL;
158df8bae1dSRodney W. Grimes 	object->paging_in_progress = 0;
159a1f6d91cSDavid Greenman 	object->resident_page_count = 0;
160eaf13dd7SJohn Dyson 	object->cache_count = 0;
161eaf13dd7SJohn Dyson 	object->wire_count = 0;
162de5f6a77SJohn Dyson 	object->shadow_count = 0;
1635070c7f8SJohn Dyson 	object->pg_color = next_index;
16499448ed1SJohn Dyson 	if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
16599448ed1SJohn Dyson 		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
16699448ed1SJohn Dyson 	else
16799448ed1SJohn Dyson 		incr = size;
16899448ed1SJohn Dyson 	next_index = (next_index + incr) & PQ_L2_MASK;
16924a1cce3SDavid Greenman 	object->handle = NULL;
170a316d390SJohn Dyson 	object->paging_offset = (vm_ooffset_t) 0;
17124a1cce3SDavid Greenman 	object->backing_object = NULL;
172a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
173a2f4a846SJohn Dyson 	object->page_hint = NULL;
174a1f6d91cSDavid Greenman 
175a1f6d91cSDavid Greenman 	object->last_read = 0;
1762d8acc0fSJohn Dyson 	object->generation++;
177df8bae1dSRodney W. Grimes 
178df8bae1dSRodney W. Grimes 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
179df8bae1dSRodney W. Grimes 	vm_object_count++;
180df8bae1dSRodney W. Grimes }
181df8bae1dSRodney W. Grimes 
182df8bae1dSRodney W. Grimes /*
18326f9a767SRodney W. Grimes  *	vm_object_init:
18426f9a767SRodney W. Grimes  *
18526f9a767SRodney W. Grimes  *	Initialize the VM objects module.
18626f9a767SRodney W. Grimes  */
18726f9a767SRodney W. Grimes void
188a316d390SJohn Dyson vm_object_init()
18926f9a767SRodney W. Grimes {
19026f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
191996c772fSJohn Dyson 	simple_lock_init(&vm_object_list_lock);
19226f9a767SRodney W. Grimes 	vm_object_count = 0;
1930217125fSDavid Greenman 
19426f9a767SRodney W. Grimes 	kernel_object = &kernel_object_store;
195a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
19626f9a767SRodney W. Grimes 	    kernel_object);
19726f9a767SRodney W. Grimes 
19826f9a767SRodney W. Grimes 	kmem_object = &kmem_object_store;
199a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
20026f9a767SRodney W. Grimes 	    kmem_object);
20199448ed1SJohn Dyson 
20299448ed1SJohn Dyson 	obj_zone = &obj_zone_store;
20399448ed1SJohn Dyson 	zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
20499448ed1SJohn Dyson 		vm_objects_init, VM_OBJECTS_INIT);
20599448ed1SJohn Dyson }
20699448ed1SJohn Dyson 
20799448ed1SJohn Dyson void
20899448ed1SJohn Dyson vm_object_init2() {
2090a80f406SJohn Dyson 	zinitna(obj_zone, NULL, NULL, 0, 0, 0, 1);
21026f9a767SRodney W. Grimes }
21126f9a767SRodney W. Grimes 
21226f9a767SRodney W. Grimes /*
21326f9a767SRodney W. Grimes  *	vm_object_allocate:
21426f9a767SRodney W. Grimes  *
21526f9a767SRodney W. Grimes  *	Returns a new object with the given size.
21626f9a767SRodney W. Grimes  */
21726f9a767SRodney W. Grimes 
21826f9a767SRodney W. Grimes vm_object_t
21924a1cce3SDavid Greenman vm_object_allocate(type, size)
22024a1cce3SDavid Greenman 	objtype_t type;
22126f9a767SRodney W. Grimes 	vm_size_t size;
22226f9a767SRodney W. Grimes {
22326f9a767SRodney W. Grimes 	register vm_object_t result;
22499448ed1SJohn Dyson 	result = (vm_object_t) zalloc(obj_zone);
22567bf6868SJohn Dyson 
22624a1cce3SDavid Greenman 	_vm_object_allocate(type, size, result);
22726f9a767SRodney W. Grimes 
22826f9a767SRodney W. Grimes 	return (result);
22926f9a767SRodney W. Grimes }
23026f9a767SRodney W. Grimes 
23126f9a767SRodney W. Grimes 
23226f9a767SRodney W. Grimes /*
233df8bae1dSRodney W. Grimes  *	vm_object_reference:
234df8bae1dSRodney W. Grimes  *
235df8bae1dSRodney W. Grimes  *	Gets another reference to the given object.
236df8bae1dSRodney W. Grimes  */
2376476c0d2SJohn Dyson void
23826f9a767SRodney W. Grimes vm_object_reference(object)
239df8bae1dSRodney W. Grimes 	register vm_object_t object;
240df8bae1dSRodney W. Grimes {
241df8bae1dSRodney W. Grimes 	if (object == NULL)
242df8bae1dSRodney W. Grimes 		return;
24395e5e988SJohn Dyson 
24495e5e988SJohn Dyson #if defined(DIAGNOSTIC)
24595e5e988SJohn Dyson 	if (object->flags & OBJ_DEAD)
24695e5e988SJohn Dyson 		panic("vm_object_reference: attempting to reference dead obj");
24795e5e988SJohn Dyson #endif
24895e5e988SJohn Dyson 
249df8bae1dSRodney W. Grimes 	object->ref_count++;
25047221757SJohn Dyson 	if (object->type == OBJT_VNODE) {
25147221757SJohn Dyson 		while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curproc)) {
25295461b45SJohn Dyson #if !defined(MAX_PERF)
25347221757SJohn Dyson 			printf("vm_object_reference: delay in getting object\n");
25495461b45SJohn Dyson #endif
25547221757SJohn Dyson 		}
25647221757SJohn Dyson 	}
25795e5e988SJohn Dyson }
25895e5e988SJohn Dyson 
259bf27292bSJohn Dyson void
26095e5e988SJohn Dyson vm_object_vndeallocate(object)
26195e5e988SJohn Dyson 	vm_object_t object;
26295e5e988SJohn Dyson {
26395e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
26495e5e988SJohn Dyson #if defined(DIAGNOSTIC)
26595e5e988SJohn Dyson 	if (object->type != OBJT_VNODE)
26695e5e988SJohn Dyson 		panic("vm_object_vndeallocate: not a vnode object");
26795e5e988SJohn Dyson 	if (vp == NULL)
26895e5e988SJohn Dyson 		panic("vm_object_vndeallocate: missing vp");
26995e5e988SJohn Dyson 	if (object->ref_count == 0) {
27095e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
27195e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
27295e5e988SJohn Dyson 	}
27395e5e988SJohn Dyson #endif
27495e5e988SJohn Dyson 
27595e5e988SJohn Dyson 	object->ref_count--;
27647221757SJohn Dyson 	if (object->ref_count == 0) {
277bf27292bSJohn Dyson 		vp->v_flag &= ~VTEXT;
2782d8acc0fSJohn Dyson 		object->flags &= ~OBJ_OPT;
2792be70f79SJohn Dyson 	}
28047221757SJohn Dyson 	vrele(vp);
281df8bae1dSRodney W. Grimes }
282df8bae1dSRodney W. Grimes 
283df8bae1dSRodney W. Grimes /*
284df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
285df8bae1dSRodney W. Grimes  *
286df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
287df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
288df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
289df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
290df8bae1dSRodney W. Grimes  *	may be relinquished.
291df8bae1dSRodney W. Grimes  *
292df8bae1dSRodney W. Grimes  *	No object may be locked.
293df8bae1dSRodney W. Grimes  */
29426f9a767SRodney W. Grimes void
29526f9a767SRodney W. Grimes vm_object_deallocate(object)
29626f9a767SRodney W. Grimes 	vm_object_t object;
297df8bae1dSRodney W. Grimes {
29895e5e988SJohn Dyson 	int s;
299df8bae1dSRodney W. Grimes 	vm_object_t temp;
300df8bae1dSRodney W. Grimes 
301df8bae1dSRodney W. Grimes 	while (object != NULL) {
302df8bae1dSRodney W. Grimes 
30395e5e988SJohn Dyson 		if (object->type == OBJT_VNODE) {
30495e5e988SJohn Dyson 			vm_object_vndeallocate(object);
30595e5e988SJohn Dyson 			return;
30695e5e988SJohn Dyson 		}
30795e5e988SJohn Dyson 
3082be70f79SJohn Dyson 		if (object->ref_count == 0) {
30947221757SJohn Dyson 			panic("vm_object_deallocate: object deallocated too many times: %d", object->type);
3102be70f79SJohn Dyson 		} else if (object->ref_count > 2) {
3112be70f79SJohn Dyson 			object->ref_count--;
3122be70f79SJohn Dyson 			return;
3132be70f79SJohn Dyson 		}
3142be70f79SJohn Dyson 
3152be70f79SJohn Dyson 		/*
3162be70f79SJohn Dyson 		 * Here on ref_count of one or two, which are special cases for
3172be70f79SJohn Dyson 		 * objects.
3182be70f79SJohn Dyson 		 */
319c0877f10SJohn Dyson 		if ((object->ref_count == 2) && (object->shadow_count == 0)) {
320c0877f10SJohn Dyson 			object->flags |= OBJ_ONEMAPPING;
321c0877f10SJohn Dyson 			object->ref_count--;
322c0877f10SJohn Dyson 			return;
323c0877f10SJohn Dyson 		} else if ((object->ref_count == 2) && (object->shadow_count == 1)) {
324be6d5bfaSDavid Greenman 			object->ref_count--;
3252be70f79SJohn Dyson 			if ((object->handle == NULL) &&
32624a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
32724a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
328a1f6d91cSDavid Greenman 				vm_object_t robject;
32995e5e988SJohn Dyson 
330b18bfc3dSJohn Dyson 				robject = TAILQ_FIRST(&object->shadow_head);
33195e5e988SJohn Dyson #if defined(DIAGNOSTIC)
33295e5e988SJohn Dyson 				if (robject == NULL)
33395e5e988SJohn Dyson 					panic("vm_object_deallocate: ref_count: %d,"
33495e5e988SJohn Dyson 						  " shadow_count: %d",
33595e5e988SJohn Dyson 						  object->ref_count, object->shadow_count);
33695e5e988SJohn Dyson #endif
33795e5e988SJohn Dyson 				if ((robject->handle == NULL) &&
33824a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
33924a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
340a1f6d91cSDavid Greenman 
34195e5e988SJohn Dyson 					robject->ref_count++;
34295e5e988SJohn Dyson 
34395e5e988SJohn Dyson 			retry:
344ffc82b0aSJohn Dyson 					if (robject->paging_in_progress ||
345ffc82b0aSJohn Dyson 							object->paging_in_progress) {
34666095752SJohn Dyson 						vm_object_pip_sleep(robject, "objde1");
347ffc82b0aSJohn Dyson 						if (robject->paging_in_progress &&
348ffc82b0aSJohn Dyson 							robject->type == OBJT_SWAP) {
34966095752SJohn Dyson 							swap_pager_sync();
35095e5e988SJohn Dyson 							goto retry;
3510d94caffSDavid Greenman 						}
352a1f6d91cSDavid Greenman 
35366095752SJohn Dyson 						vm_object_pip_sleep(object, "objde2");
354ffc82b0aSJohn Dyson 						if (object->paging_in_progress &&
355ffc82b0aSJohn Dyson 							object->type == OBJT_SWAP) {
35666095752SJohn Dyson 							swap_pager_sync();
35766095752SJohn Dyson 						}
35895e5e988SJohn Dyson 						goto retry;
359a1f6d91cSDavid Greenman 					}
360a1f6d91cSDavid Greenman 
36195e5e988SJohn Dyson 					if( robject->ref_count == 1) {
36295e5e988SJohn Dyson 						robject->ref_count--;
363ba8da839SDavid Greenman 						object = robject;
36495e5e988SJohn Dyson 						goto doterm;
36595e5e988SJohn Dyson 					}
36695e5e988SJohn Dyson 
36795e5e988SJohn Dyson 					object = robject;
36895e5e988SJohn Dyson 					vm_object_collapse(object);
369ba8da839SDavid Greenman 					continue;
370a1f6d91cSDavid Greenman 				}
37195e5e988SJohn Dyson 			}
37295e5e988SJohn Dyson 
373a1f6d91cSDavid Greenman 			return;
374df8bae1dSRodney W. Grimes 
37595e5e988SJohn Dyson 		} else {
3761efb74fbSJohn Dyson 			object->ref_count--;
37795e5e988SJohn Dyson 			if (object->ref_count != 0)
37895e5e988SJohn Dyson 				return;
37995e5e988SJohn Dyson 		}
38095e5e988SJohn Dyson 
38195e5e988SJohn Dyson doterm:
3821efb74fbSJohn Dyson 
38324a1cce3SDavid Greenman 		temp = object->backing_object;
384de5f6a77SJohn Dyson 		if (temp) {
38524a1cce3SDavid Greenman 			TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
38695e5e988SJohn Dyson 			temp->shadow_count--;
3872d8acc0fSJohn Dyson 			if (temp->ref_count == 0)
388bf27292bSJohn Dyson 				temp->flags &= ~OBJ_OPT;
389eaf13dd7SJohn Dyson 			temp->generation++;
39095461b45SJohn Dyson 			object->backing_object = NULL;
391de5f6a77SJohn Dyson 		}
392df8bae1dSRodney W. Grimes 		vm_object_terminate(object);
393df8bae1dSRodney W. Grimes 		/* unlocks and deallocates object */
394df8bae1dSRodney W. Grimes 		object = temp;
395df8bae1dSRodney W. Grimes 	}
396df8bae1dSRodney W. Grimes }
397df8bae1dSRodney W. Grimes 
398df8bae1dSRodney W. Grimes /*
399df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
400df8bae1dSRodney W. Grimes  *	up all previously used resources.
401df8bae1dSRodney W. Grimes  *
402df8bae1dSRodney W. Grimes  *	The object must be locked.
403df8bae1dSRodney W. Grimes  */
40495e5e988SJohn Dyson void
40526f9a767SRodney W. Grimes vm_object_terminate(object)
406df8bae1dSRodney W. Grimes 	register vm_object_t object;
407df8bae1dSRodney W. Grimes {
4083af76890SPoul-Henning Kamp 	register vm_page_t p;
40926f9a767SRodney W. Grimes 	int s;
410df8bae1dSRodney W. Grimes 
41195e5e988SJohn Dyson 	/*
41295e5e988SJohn Dyson 	 * Make sure no one uses us.
41395e5e988SJohn Dyson 	 */
41495e5e988SJohn Dyson 	object->flags |= OBJ_DEAD;
4153c631446SJohn Dyson 
416df8bae1dSRodney W. Grimes 	/*
417f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
418df8bae1dSRodney W. Grimes 	 */
41966095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
420df8bae1dSRodney W. Grimes 
42195e5e988SJohn Dyson #if defined(DIAGNOSTIC)
42226f9a767SRodney W. Grimes 	if (object->paging_in_progress != 0)
42395461b45SJohn Dyson 		panic("vm_object_terminate: pageout in progress");
42495e5e988SJohn Dyson #endif
42526f9a767SRodney W. Grimes 
42626f9a767SRodney W. Grimes 	/*
4270d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
4280d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
42926f9a767SRodney W. Grimes 	 */
43024a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
43195e5e988SJohn Dyson 		struct vnode *vp;
43295e5e988SJohn Dyson 
43395e5e988SJohn Dyson 		/*
43495e5e988SJohn Dyson 		 * Freeze optimized copies.
43595e5e988SJohn Dyson 		 */
43695e5e988SJohn Dyson 		vm_freeze_copyopts(object, 0, object->size);
43795e5e988SJohn Dyson 
43895e5e988SJohn Dyson 		/*
43995e5e988SJohn Dyson 		 * Clean pages and flush buffers.
44095e5e988SJohn Dyson 		 */
4418f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
44295e5e988SJohn Dyson 
44395e5e988SJohn Dyson 		vp = (struct vnode *) object->handle;
444f6b04d2bSDavid Greenman 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
44595e5e988SJohn Dyson 
446bef608bdSJohn Dyson 		/*
447bef608bdSJohn Dyson 		 * Let the pager know object is dead.
448bef608bdSJohn Dyson 		 */
449bef608bdSJohn Dyson 		vm_pager_deallocate(object);
450bef608bdSJohn Dyson 
451bef608bdSJohn Dyson 	}
452bef608bdSJohn Dyson 
453bef608bdSJohn Dyson 	if ((object->type != OBJT_VNODE) && (object->ref_count == 0)) {
454996c772fSJohn Dyson 
4550d94caffSDavid Greenman 		/*
4560d94caffSDavid Greenman 		 * Now free the pages. For internal objects, this also removes them
4570d94caffSDavid Greenman 		 * from paging queues.
458df8bae1dSRodney W. Grimes 		 */
459b18bfc3dSJohn Dyson 		while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
46095461b45SJohn Dyson #if !defined(MAX_PERF)
461a2f4a846SJohn Dyson 			if (p->busy || (p->flags & PG_BUSY))
462be6d5bfaSDavid Greenman 				printf("vm_object_terminate: freeing busy page\n");
46395461b45SJohn Dyson #endif
464eaf13dd7SJohn Dyson 			p->flags |= PG_BUSY;
465df8bae1dSRodney W. Grimes 			vm_page_free(p);
466df8bae1dSRodney W. Grimes 			cnt.v_pfree++;
467df8bae1dSRodney W. Grimes 		}
468df8bae1dSRodney W. Grimes 		/*
469df8bae1dSRodney W. Grimes 		 * Let the pager know object is dead.
470df8bae1dSRodney W. Grimes 		 */
47124a1cce3SDavid Greenman 		vm_pager_deallocate(object);
472bef608bdSJohn Dyson 
47395461b45SJohn Dyson 	}
474df8bae1dSRodney W. Grimes 
475bef608bdSJohn Dyson 	if ((object->ref_count == 0) && (object->resident_page_count == 0))
4762d8acc0fSJohn Dyson 		vm_object_dispose(object);
4772d8acc0fSJohn Dyson }
4782d8acc0fSJohn Dyson 
4792d8acc0fSJohn Dyson /*
4802d8acc0fSJohn Dyson  * vm_object_dispose
4812d8acc0fSJohn Dyson  *
4822d8acc0fSJohn Dyson  * Dispose the object.
4832d8acc0fSJohn Dyson  */
48495461b45SJohn Dyson static void
4852d8acc0fSJohn Dyson vm_object_dispose(object)
4862d8acc0fSJohn Dyson 	vm_object_t object;
4872d8acc0fSJohn Dyson {
488996c772fSJohn Dyson 		simple_lock(&vm_object_list_lock);
489df8bae1dSRodney W. Grimes 		TAILQ_REMOVE(&vm_object_list, object, object_list);
490df8bae1dSRodney W. Grimes 		vm_object_count--;
491996c772fSJohn Dyson 		simple_unlock(&vm_object_list_lock);
492df8bae1dSRodney W. Grimes 		/*
493df8bae1dSRodney W. Grimes    		* Free the space for the object.
494df8bae1dSRodney W. Grimes    		*/
49599448ed1SJohn Dyson 		zfree(obj_zone, object);
49647221757SJohn Dyson 		wakeup(object);
49747221757SJohn Dyson }
498df8bae1dSRodney W. Grimes 
499df8bae1dSRodney W. Grimes /*
500df8bae1dSRodney W. Grimes  *	vm_object_page_clean
501df8bae1dSRodney W. Grimes  *
502df8bae1dSRodney W. Grimes  *	Clean all dirty pages in the specified range of object.
50326f9a767SRodney W. Grimes  *	Leaves page on whatever queue it is currently on.
50426f9a767SRodney W. Grimes  *
50526f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
50626f9a767SRodney W. Grimes  *
50726f9a767SRodney W. Grimes  *	The object must be locked.
50826f9a767SRodney W. Grimes  */
509f6b04d2bSDavid Greenman 
510f6b04d2bSDavid Greenman void
5118f9110f6SJohn Dyson vm_object_page_clean(object, start, end, flags)
512f6b04d2bSDavid Greenman 	vm_object_t object;
513a316d390SJohn Dyson 	vm_pindex_t start;
514a316d390SJohn Dyson 	vm_pindex_t end;
5158f9110f6SJohn Dyson 	int flags;
516f6b04d2bSDavid Greenman {
517bd7e5f99SJohn Dyson 	register vm_page_t p, np, tp;
518f6b04d2bSDavid Greenman 	register vm_offset_t tstart, tend;
519bd7e5f99SJohn Dyson 	vm_pindex_t pi;
520aef922f5SJohn Dyson 	int s;
52124a1cce3SDavid Greenman 	struct vnode *vp;
522aef922f5SJohn Dyson 	int runlen;
523bd7e5f99SJohn Dyson 	int maxf;
524bd7e5f99SJohn Dyson 	int chkb;
525bd7e5f99SJohn Dyson 	int maxb;
526bd7e5f99SJohn Dyson 	int i;
5278f9110f6SJohn Dyson 	int pagerflags;
528bd7e5f99SJohn Dyson 	vm_page_t maf[vm_pageout_page_count];
529bd7e5f99SJohn Dyson 	vm_page_t mab[vm_pageout_page_count];
530aef922f5SJohn Dyson 	vm_page_t ma[vm_pageout_page_count];
5312d8acc0fSJohn Dyson 	int curgeneration;
532996c772fSJohn Dyson 	struct proc *pproc = curproc;	/* XXX */
533f6b04d2bSDavid Greenman 
534aef922f5SJohn Dyson 	if (object->type != OBJT_VNODE ||
535aef922f5SJohn Dyson 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
536f6b04d2bSDavid Greenman 		return;
537f6b04d2bSDavid Greenman 
5388f9110f6SJohn Dyson 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0;
5398f9110f6SJohn Dyson 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
5408f9110f6SJohn Dyson 
54124a1cce3SDavid Greenman 	vp = object->handle;
54224a1cce3SDavid Greenman 
543aef922f5SJohn Dyson 	object->flags |= OBJ_CLEANING;
54424a1cce3SDavid Greenman 
545f6b04d2bSDavid Greenman 	tstart = start;
546f6b04d2bSDavid Greenman 	if (end == 0) {
547f6b04d2bSDavid Greenman 		tend = object->size;
548f6b04d2bSDavid Greenman 	} else {
549f6b04d2bSDavid Greenman 		tend = end;
550f6b04d2bSDavid Greenman 	}
551eaf13dd7SJohn Dyson 
552eaf13dd7SJohn Dyson 	for(p = TAILQ_FIRST(&object->memq); p; p = TAILQ_NEXT(p, listq)) {
553eaf13dd7SJohn Dyson 		p->flags |= PG_CLEANCHK;
554eaf13dd7SJohn Dyson 		vm_page_protect(p, VM_PROT_READ);
555eaf13dd7SJohn Dyson 	}
556eaf13dd7SJohn Dyson 
557a316d390SJohn Dyson 	if ((tstart == 0) && (tend == object->size)) {
558aef922f5SJohn Dyson 		object->flags &= ~(OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
559ec4f9fb0SDavid Greenman 	}
560f6b04d2bSDavid Greenman 
561bd7e5f99SJohn Dyson rescan:
5622d8acc0fSJohn Dyson 	curgeneration = object->generation;
5632d8acc0fSJohn Dyson 
564b18bfc3dSJohn Dyson 	for(p = TAILQ_FIRST(&object->memq); p; p = np) {
565b18bfc3dSJohn Dyson 		np = TAILQ_NEXT(p, listq);
566bd7e5f99SJohn Dyson 
567bd7e5f99SJohn Dyson 		pi = p->pindex;
568bd7e5f99SJohn Dyson 		if (((p->flags & PG_CLEANCHK) == 0) ||
569bd7e5f99SJohn Dyson 			(pi < tstart) || (pi >= tend) ||
5705070c7f8SJohn Dyson 			(p->valid == 0) ||
5715070c7f8SJohn Dyson 			((p->queue - p->pc) == PQ_CACHE)) {
572bd7e5f99SJohn Dyson 			p->flags &= ~PG_CLEANCHK;
573aef922f5SJohn Dyson 			continue;
574f6b04d2bSDavid Greenman 		}
575f6b04d2bSDavid Greenman 
576bd7e5f99SJohn Dyson 		vm_page_test_dirty(p);
577bd7e5f99SJohn Dyson 		if ((p->dirty & p->valid) == 0) {
578bd7e5f99SJohn Dyson 			p->flags &= ~PG_CLEANCHK;
579bd7e5f99SJohn Dyson 			continue;
580bd7e5f99SJohn Dyson 		}
581ec4f9fb0SDavid Greenman 
582b18bfc3dSJohn Dyson 		s = splvm();
5832d8acc0fSJohn Dyson 		while ((p->flags & PG_BUSY) || p->busy) {
584aef922f5SJohn Dyson 			p->flags |= PG_WANTED | PG_REFERENCED;
585aef922f5SJohn Dyson 			tsleep(p, PVM, "vpcwai", 0);
5862d8acc0fSJohn Dyson 			if (object->generation != curgeneration) {
587f6b04d2bSDavid Greenman 				splx(s);
588bd7e5f99SJohn Dyson 				goto rescan;
589f6b04d2bSDavid Greenman 			}
5902d8acc0fSJohn Dyson 		}
591f6b04d2bSDavid Greenman 
592bd7e5f99SJohn Dyson 		maxf = 0;
593bd7e5f99SJohn Dyson 		for(i=1;i<vm_pageout_page_count;i++) {
594bd7e5f99SJohn Dyson 			if (tp = vm_page_lookup(object, pi + i)) {
595bd7e5f99SJohn Dyson 				if ((tp->flags & PG_BUSY) ||
596ffc82b0aSJohn Dyson 					(tp->flags & PG_CLEANCHK) == 0 ||
597ffc82b0aSJohn Dyson 					(tp->busy != 0))
598bd7e5f99SJohn Dyson 					break;
5995070c7f8SJohn Dyson 				if((tp->queue - tp->pc) == PQ_CACHE) {
6003077a9c2SJohn Dyson 					tp->flags &= ~PG_CLEANCHK;
6013077a9c2SJohn Dyson 					break;
6023077a9c2SJohn Dyson 				}
603bd7e5f99SJohn Dyson 				vm_page_test_dirty(tp);
604bd7e5f99SJohn Dyson 				if ((tp->dirty & tp->valid) == 0) {
605bd7e5f99SJohn Dyson 					tp->flags &= ~PG_CLEANCHK;
606bd7e5f99SJohn Dyson 					break;
607bd7e5f99SJohn Dyson 				}
608bd7e5f99SJohn Dyson 				maf[ i - 1 ] = tp;
609bd7e5f99SJohn Dyson 				maxf++;
610bd7e5f99SJohn Dyson 				continue;
611bd7e5f99SJohn Dyson 			}
612bd7e5f99SJohn Dyson 			break;
613bd7e5f99SJohn Dyson 		}
614aef922f5SJohn Dyson 
615bd7e5f99SJohn Dyson 		maxb = 0;
616bd7e5f99SJohn Dyson 		chkb = vm_pageout_page_count -  maxf;
617bd7e5f99SJohn Dyson 		if (chkb) {
618bd7e5f99SJohn Dyson 			for(i = 1; i < chkb;i++) {
619bd7e5f99SJohn Dyson 				if (tp = vm_page_lookup(object, pi - i)) {
620bd7e5f99SJohn Dyson 					if ((tp->flags & PG_BUSY) ||
621ffc82b0aSJohn Dyson 						(tp->flags & PG_CLEANCHK) == 0 ||
622ffc82b0aSJohn Dyson 						(tp->busy != 0))
623bd7e5f99SJohn Dyson 						break;
6245070c7f8SJohn Dyson 					if((tp->queue - tp->pc) == PQ_CACHE) {
6253077a9c2SJohn Dyson 						tp->flags &= ~PG_CLEANCHK;
6263077a9c2SJohn Dyson 						break;
6273077a9c2SJohn Dyson 					}
628bd7e5f99SJohn Dyson 					vm_page_test_dirty(tp);
629bd7e5f99SJohn Dyson 					if ((tp->dirty & tp->valid) == 0) {
630bd7e5f99SJohn Dyson 						tp->flags &= ~PG_CLEANCHK;
631bd7e5f99SJohn Dyson 						break;
632bd7e5f99SJohn Dyson 					}
633bd7e5f99SJohn Dyson 					mab[ i - 1 ] = tp;
634bd7e5f99SJohn Dyson 					maxb++;
635bd7e5f99SJohn Dyson 					continue;
636bd7e5f99SJohn Dyson 				}
637bd7e5f99SJohn Dyson 				break;
638bd7e5f99SJohn Dyson 			}
639bd7e5f99SJohn Dyson 		}
640bd7e5f99SJohn Dyson 
641bd7e5f99SJohn Dyson 		for(i=0;i<maxb;i++) {
642bd7e5f99SJohn Dyson 			int index = (maxb - i) - 1;
643bd7e5f99SJohn Dyson 			ma[index] = mab[i];
644bd7e5f99SJohn Dyson 			ma[index]->flags &= ~PG_CLEANCHK;
645bd7e5f99SJohn Dyson 		}
646bd7e5f99SJohn Dyson 		p->flags &= ~PG_CLEANCHK;
647bd7e5f99SJohn Dyson 		ma[maxb] = p;
648bd7e5f99SJohn Dyson 		for(i=0;i<maxf;i++) {
649bd7e5f99SJohn Dyson 			int index = (maxb + i) + 1;
650bd7e5f99SJohn Dyson 			ma[index] = maf[i];
651bd7e5f99SJohn Dyson 			ma[index]->flags &= ~PG_CLEANCHK;
652f6b04d2bSDavid Greenman 		}
653bd7e5f99SJohn Dyson 		runlen = maxb + maxf + 1;
654cf2819ccSJohn Dyson 
655f35329acSJohn Dyson 		splx(s);
6568f9110f6SJohn Dyson 		vm_pageout_flush(ma, runlen, pagerflags);
657cf2819ccSJohn Dyson 		for (i = 0; i<runlen; i++) {
658cf2819ccSJohn Dyson 			if (ma[i]->valid & ma[i]->dirty) {
659cf2819ccSJohn Dyson 				vm_page_protect(ma[i], VM_PROT_READ);
660cf2819ccSJohn Dyson 				ma[i]->flags |= PG_CLEANCHK;
661cf2819ccSJohn Dyson 			}
662cf2819ccSJohn Dyson 		}
6632d8acc0fSJohn Dyson 		if (object->generation != curgeneration)
664bd7e5f99SJohn Dyson 			goto rescan;
665f6b04d2bSDavid Greenman 	}
666aef922f5SJohn Dyson 
667cf2819ccSJohn Dyson 	VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
668aef922f5SJohn Dyson 
669aef922f5SJohn Dyson 	object->flags &= ~OBJ_CLEANING;
670f5cf85d4SDavid Greenman 	return;
67126f9a767SRodney W. Grimes }
672df8bae1dSRodney W. Grimes 
673f708ef1bSPoul-Henning Kamp #ifdef not_used
674f708ef1bSPoul-Henning Kamp /* XXX I cannot tell if this should be an exported symbol */
675df8bae1dSRodney W. Grimes /*
676df8bae1dSRodney W. Grimes  *	vm_object_deactivate_pages
677df8bae1dSRodney W. Grimes  *
678df8bae1dSRodney W. Grimes  *	Deactivate all pages in the specified object.  (Keep its pages
679df8bae1dSRodney W. Grimes  *	in memory even though it is no longer referenced.)
680df8bae1dSRodney W. Grimes  *
681df8bae1dSRodney W. Grimes  *	The object must be locked.
682df8bae1dSRodney W. Grimes  */
683f708ef1bSPoul-Henning Kamp static void
684df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object)
685df8bae1dSRodney W. Grimes 	register vm_object_t object;
686df8bae1dSRodney W. Grimes {
687df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
688df8bae1dSRodney W. Grimes 
689b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
690b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
691df8bae1dSRodney W. Grimes 		vm_page_deactivate(p);
692df8bae1dSRodney W. Grimes 	}
693df8bae1dSRodney W. Grimes }
694f708ef1bSPoul-Henning Kamp #endif
695df8bae1dSRodney W. Grimes 
696df8bae1dSRodney W. Grimes /*
697df8bae1dSRodney W. Grimes  *	vm_object_pmap_copy:
698df8bae1dSRodney W. Grimes  *
699df8bae1dSRodney W. Grimes  *	Makes all physical pages in the specified
700df8bae1dSRodney W. Grimes  *	object range copy-on-write.  No writeable
701df8bae1dSRodney W. Grimes  *	references to these pages should remain.
702df8bae1dSRodney W. Grimes  *
703df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
704df8bae1dSRodney W. Grimes  */
7050d94caffSDavid Greenman void
7060d94caffSDavid Greenman vm_object_pmap_copy(object, start, end)
707df8bae1dSRodney W. Grimes 	register vm_object_t object;
708a316d390SJohn Dyson 	register vm_pindex_t start;
709a316d390SJohn Dyson 	register vm_pindex_t end;
710df8bae1dSRodney W. Grimes {
711df8bae1dSRodney W. Grimes 	register vm_page_t p;
712df8bae1dSRodney W. Grimes 
713aef922f5SJohn Dyson 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
714df8bae1dSRodney W. Grimes 		return;
715df8bae1dSRodney W. Grimes 
716b5b40fa6SJohn Dyson 	for (p = TAILQ_FIRST(&object->memq);
717b5b40fa6SJohn Dyson 		p != NULL;
718b5b40fa6SJohn Dyson 		p = TAILQ_NEXT(p, listq)) {
719f919ebdeSDavid Greenman 		vm_page_protect(p, VM_PROT_READ);
720df8bae1dSRodney W. Grimes 	}
721aef922f5SJohn Dyson 
722aef922f5SJohn Dyson 	object->flags &= ~OBJ_WRITEABLE;
723df8bae1dSRodney W. Grimes }
724df8bae1dSRodney W. Grimes 
725df8bae1dSRodney W. Grimes /*
7261efb74fbSJohn Dyson  * Same as vm_object_pmap_copy_1, except range checking really
7271efb74fbSJohn Dyson  * works, and is meant for small sections of an object.
7281efb74fbSJohn Dyson  */
7291efb74fbSJohn Dyson void
7301efb74fbSJohn Dyson vm_object_pmap_copy_1(object, start, end)
7311efb74fbSJohn Dyson 	register vm_object_t object;
7321efb74fbSJohn Dyson 	register vm_pindex_t start;
7331efb74fbSJohn Dyson 	register vm_pindex_t end;
7341efb74fbSJohn Dyson {
7351efb74fbSJohn Dyson 	vm_pindex_t idx;
7361efb74fbSJohn Dyson 	register vm_page_t p;
7371efb74fbSJohn Dyson 
7381efb74fbSJohn Dyson 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
7391efb74fbSJohn Dyson 		return;
7401efb74fbSJohn Dyson 
7411efb74fbSJohn Dyson 	for (idx = start; idx < end; idx++) {
7421efb74fbSJohn Dyson 		p = vm_page_lookup(object, idx);
7431efb74fbSJohn Dyson 		if (p == NULL)
7441efb74fbSJohn Dyson 			continue;
7451efb74fbSJohn Dyson 		vm_page_protect(p, VM_PROT_READ);
7461efb74fbSJohn Dyson 	}
7471efb74fbSJohn Dyson }
7481efb74fbSJohn Dyson 
7491efb74fbSJohn Dyson /*
750df8bae1dSRodney W. Grimes  *	vm_object_pmap_remove:
751df8bae1dSRodney W. Grimes  *
752df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
753df8bae1dSRodney W. Grimes  *	object range from all physical maps.
754df8bae1dSRodney W. Grimes  *
755df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
756df8bae1dSRodney W. Grimes  */
75726f9a767SRodney W. Grimes void
75826f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end)
759df8bae1dSRodney W. Grimes 	register vm_object_t object;
760a316d390SJohn Dyson 	register vm_pindex_t start;
761a316d390SJohn Dyson 	register vm_pindex_t end;
762df8bae1dSRodney W. Grimes {
763df8bae1dSRodney W. Grimes 	register vm_page_t p;
764df8bae1dSRodney W. Grimes 	if (object == NULL)
765df8bae1dSRodney W. Grimes 		return;
766b5b40fa6SJohn Dyson 	for (p = TAILQ_FIRST(&object->memq);
767b5b40fa6SJohn Dyson 		p != NULL;
768b5b40fa6SJohn Dyson 		p = TAILQ_NEXT(p, listq)) {
769bd7e5f99SJohn Dyson 		if (p->pindex >= start && p->pindex < end)
770f919ebdeSDavid Greenman 			vm_page_protect(p, VM_PROT_NONE);
77126f9a767SRodney W. Grimes 	}
7726e20a165SJohn Dyson 	if ((start == 0) && (object->size == end))
7736e20a165SJohn Dyson 		object->flags &= ~OBJ_WRITEABLE;
77426f9a767SRodney W. Grimes }
775df8bae1dSRodney W. Grimes 
776df8bae1dSRodney W. Grimes /*
777867a482dSJohn Dyson  *	vm_object_madvise:
778867a482dSJohn Dyson  *
779867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
780867a482dSJohn Dyson  */
781867a482dSJohn Dyson void
782867a482dSJohn Dyson vm_object_madvise(object, pindex, count, advise)
783867a482dSJohn Dyson 	vm_object_t object;
784867a482dSJohn Dyson 	vm_pindex_t pindex;
785867a482dSJohn Dyson 	int count;
786867a482dSJohn Dyson 	int advise;
787867a482dSJohn Dyson {
7886e20a165SJohn Dyson 	int s;
7896e20a165SJohn Dyson 	vm_pindex_t end, tpindex;
7906e20a165SJohn Dyson 	vm_object_t tobject;
791867a482dSJohn Dyson 	vm_page_t m;
792867a482dSJohn Dyson 
793867a482dSJohn Dyson 	if (object == NULL)
794867a482dSJohn Dyson 		return;
795867a482dSJohn Dyson 
796867a482dSJohn Dyson 	end = pindex + count;
797867a482dSJohn Dyson 
798867a482dSJohn Dyson 	for (; pindex < end; pindex += 1) {
7996e20a165SJohn Dyson 
8006e20a165SJohn Dyson relookup:
8016e20a165SJohn Dyson 		tobject = object;
8026e20a165SJohn Dyson 		tpindex = pindex;
8036e20a165SJohn Dyson shadowlookup:
8046e20a165SJohn Dyson 		m = vm_page_lookup(tobject, tpindex);
8056e20a165SJohn Dyson 		if (m == NULL) {
8066e20a165SJohn Dyson 			if (tobject->type != OBJT_DEFAULT) {
8076e20a165SJohn Dyson 				continue;
8086e20a165SJohn Dyson 			}
8096e20a165SJohn Dyson 
8106e20a165SJohn Dyson 			tobject = tobject->backing_object;
8116e20a165SJohn Dyson 			if ((tobject == NULL) || (tobject->ref_count != 1)) {
8126e20a165SJohn Dyson 				continue;
8136e20a165SJohn Dyson 			}
8146e20a165SJohn Dyson 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
8156e20a165SJohn Dyson 			goto shadowlookup;
8166e20a165SJohn Dyson 		}
817867a482dSJohn Dyson 
818867a482dSJohn Dyson 		/*
819867a482dSJohn Dyson 		 * If the page is busy or not in a normal active state,
820867a482dSJohn Dyson 		 * we skip it.  Things can break if we mess with pages
821867a482dSJohn Dyson 		 * in any of the below states.
822867a482dSJohn Dyson 		 */
8236e20a165SJohn Dyson 		if (m->hold_count || m->wire_count ||
8246e20a165SJohn Dyson 			m->valid != VM_PAGE_BITS_ALL) {
825867a482dSJohn Dyson 			continue;
8266e20a165SJohn Dyson 		}
8276e20a165SJohn Dyson 
828ffc82b0aSJohn Dyson  		if (vm_page_sleep(m, "madvpo", &m->busy))
8296e20a165SJohn Dyson   			goto relookup;
830867a482dSJohn Dyson 
831867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
832867a482dSJohn Dyson 			vm_page_activate(m);
8336e20a165SJohn Dyson 		} else if (advise == MADV_DONTNEED) {
834867a482dSJohn Dyson 			vm_page_deactivate(m);
8350a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
8366e20a165SJohn Dyson 			pmap_clear_modify(VM_PAGE_TO_PHYS(m));
8376e20a165SJohn Dyson 			m->dirty = 0;
8380a47b48bSJohn Dyson 			/*
8396e20a165SJohn Dyson 			 * Force a demand zero if attempt to read from swap.
8406e20a165SJohn Dyson 			 * We currently don't handle vnode files correctly,
8416e20a165SJohn Dyson 			 * and will reread stale contents unnecessarily.
8420a47b48bSJohn Dyson 			 */
8430a47b48bSJohn Dyson 			if (object->type == OBJT_SWAP)
8446e20a165SJohn Dyson 				swap_pager_dmzspace(tobject, m->pindex, 1);
845867a482dSJohn Dyson 		}
846867a482dSJohn Dyson 	}
847867a482dSJohn Dyson }
848867a482dSJohn Dyson 
849867a482dSJohn Dyson /*
850df8bae1dSRodney W. Grimes  *	vm_object_shadow:
851df8bae1dSRodney W. Grimes  *
852df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
853df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
854df8bae1dSRodney W. Grimes  *	object reference is deallocated.
855df8bae1dSRodney W. Grimes  *
856df8bae1dSRodney W. Grimes  *	The new object and offset into that object
857df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
858df8bae1dSRodney W. Grimes  */
859df8bae1dSRodney W. Grimes 
86026f9a767SRodney W. Grimes void
86126f9a767SRodney W. Grimes vm_object_shadow(object, offset, length)
862df8bae1dSRodney W. Grimes 	vm_object_t *object;	/* IN/OUT */
863a316d390SJohn Dyson 	vm_ooffset_t *offset;	/* IN/OUT */
864df8bae1dSRodney W. Grimes 	vm_size_t length;
865df8bae1dSRodney W. Grimes {
866df8bae1dSRodney W. Grimes 	register vm_object_t source;
867df8bae1dSRodney W. Grimes 	register vm_object_t result;
868df8bae1dSRodney W. Grimes 
869df8bae1dSRodney W. Grimes 	source = *object;
870df8bae1dSRodney W. Grimes 
871df8bae1dSRodney W. Grimes 	/*
872df8bae1dSRodney W. Grimes 	 * Allocate a new object with the given length
873df8bae1dSRodney W. Grimes 	 */
874df8bae1dSRodney W. Grimes 
87524a1cce3SDavid Greenman 	if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
876df8bae1dSRodney W. Grimes 		panic("vm_object_shadow: no object for shadowing");
877df8bae1dSRodney W. Grimes 
878df8bae1dSRodney W. Grimes 	/*
8790d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
8800d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
8810d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
8820d94caffSDavid Greenman 	 * of reference count.
883df8bae1dSRodney W. Grimes 	 */
88424a1cce3SDavid Greenman 	result->backing_object = source;
885de5f6a77SJohn Dyson 	if (source) {
886de5f6a77SJohn Dyson 		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
887c0877f10SJohn Dyson 		source->flags &= ~OBJ_ONEMAPPING;
888eaf13dd7SJohn Dyson 		source->shadow_count++;
889eaf13dd7SJohn Dyson 		source->generation++;
890de5f6a77SJohn Dyson 	}
891df8bae1dSRodney W. Grimes 
892df8bae1dSRodney W. Grimes 	/*
8930d94caffSDavid Greenman 	 * Store the offset into the source object, and fix up the offset into
8940d94caffSDavid Greenman 	 * the new object.
895df8bae1dSRodney W. Grimes 	 */
896df8bae1dSRodney W. Grimes 
89724a1cce3SDavid Greenman 	result->backing_object_offset = *offset;
898df8bae1dSRodney W. Grimes 
899df8bae1dSRodney W. Grimes 	/*
900df8bae1dSRodney W. Grimes 	 * Return the new things
901df8bae1dSRodney W. Grimes 	 */
902df8bae1dSRodney W. Grimes 
903df8bae1dSRodney W. Grimes 	*offset = 0;
904df8bae1dSRodney W. Grimes 	*object = result;
905df8bae1dSRodney W. Grimes }
906df8bae1dSRodney W. Grimes 
907df8bae1dSRodney W. Grimes 
908df8bae1dSRodney W. Grimes /*
9092fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
9102fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
9112fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
9122fe6e4d7SDavid Greenman  */
9132fe6e4d7SDavid Greenman static void
9142fe6e4d7SDavid Greenman vm_object_qcollapse(object)
9152fe6e4d7SDavid Greenman 	register vm_object_t object;
9162fe6e4d7SDavid Greenman {
9172fe6e4d7SDavid Greenman 	register vm_object_t backing_object;
918a316d390SJohn Dyson 	register vm_pindex_t backing_offset_index, paging_offset_index;
919a316d390SJohn Dyson 	vm_pindex_t backing_object_paging_offset_index;
920a316d390SJohn Dyson 	vm_pindex_t new_pindex;
9212fe6e4d7SDavid Greenman 	register vm_page_t p, pp;
9222fe6e4d7SDavid Greenman 	register vm_size_t size;
9232fe6e4d7SDavid Greenman 
92424a1cce3SDavid Greenman 	backing_object = object->backing_object;
9252fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
9262fe6e4d7SDavid Greenman 		return;
9272fe6e4d7SDavid Greenman 
928010cf3b9SDavid Greenman 	backing_object->ref_count += 2;
929010cf3b9SDavid Greenman 
930a316d390SJohn Dyson 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
931a316d390SJohn Dyson 	backing_object_paging_offset_index = OFF_TO_IDX(backing_object->paging_offset);
932a316d390SJohn Dyson 	paging_offset_index = OFF_TO_IDX(object->paging_offset);
9332fe6e4d7SDavid Greenman 	size = object->size;
934b18bfc3dSJohn Dyson 	p = TAILQ_FIRST(&backing_object->memq);
9352fe6e4d7SDavid Greenman 	while (p) {
9362fe6e4d7SDavid Greenman 		vm_page_t next;
9370d94caffSDavid Greenman 
938b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
939bd7e5f99SJohn Dyson 		if ((p->flags & (PG_BUSY | PG_FICTITIOUS)) ||
9405070c7f8SJohn Dyson 		    !p->valid || p->hold_count || p->wire_count || p->busy) {
9412fe6e4d7SDavid Greenman 			p = next;
9422fe6e4d7SDavid Greenman 			continue;
9432fe6e4d7SDavid Greenman 		}
944eaf13dd7SJohn Dyson 		p->flags |= PG_BUSY;
945eaf13dd7SJohn Dyson 
946a316d390SJohn Dyson 		new_pindex = p->pindex - backing_offset_index;
947a316d390SJohn Dyson 		if (p->pindex < backing_offset_index ||
948a316d390SJohn Dyson 		    new_pindex >= size) {
94924a1cce3SDavid Greenman 			if (backing_object->type == OBJT_SWAP)
95024a1cce3SDavid Greenman 				swap_pager_freespace(backing_object,
951a316d390SJohn Dyson 				    backing_object_paging_offset_index+p->pindex,
952a316d390SJohn Dyson 				    1);
953b18bfc3dSJohn Dyson 			vm_page_protect(p, VM_PROT_NONE);
9542fe6e4d7SDavid Greenman 			vm_page_free(p);
9552fe6e4d7SDavid Greenman 		} else {
956a316d390SJohn Dyson 			pp = vm_page_lookup(object, new_pindex);
957eaf13dd7SJohn Dyson 			if (pp != NULL ||
958eaf13dd7SJohn Dyson 				(object->type == OBJT_SWAP && vm_pager_has_page(object,
959a316d390SJohn Dyson 				    paging_offset_index + new_pindex, NULL, NULL))) {
96024a1cce3SDavid Greenman 				if (backing_object->type == OBJT_SWAP)
96124a1cce3SDavid Greenman 					swap_pager_freespace(backing_object,
962a316d390SJohn Dyson 					    backing_object_paging_offset_index + p->pindex, 1);
963b18bfc3dSJohn Dyson 				vm_page_protect(p, VM_PROT_NONE);
9642fe6e4d7SDavid Greenman 				vm_page_free(p);
9652fe6e4d7SDavid Greenman 			} else {
96624a1cce3SDavid Greenman 				if (backing_object->type == OBJT_SWAP)
96724a1cce3SDavid Greenman 					swap_pager_freespace(backing_object,
968a316d390SJohn Dyson 					    backing_object_paging_offset_index + p->pindex, 1);
969eaf13dd7SJohn Dyson 
970edd97f3aSJohn Dyson 				if ((p->queue - p->pc) == PQ_CACHE)
971edd97f3aSJohn Dyson 					vm_page_deactivate(p);
972edd97f3aSJohn Dyson 				else
9736e20a165SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
974edd97f3aSJohn Dyson 
975edd97f3aSJohn Dyson 				vm_page_rename(p, object, new_pindex);
9769b4814bbSDavid Greenman 				p->dirty = VM_PAGE_BITS_ALL;
9772fe6e4d7SDavid Greenman 			}
9782fe6e4d7SDavid Greenman 		}
9792fe6e4d7SDavid Greenman 		p = next;
9802fe6e4d7SDavid Greenman 	}
981010cf3b9SDavid Greenman 	backing_object->ref_count -= 2;
9822fe6e4d7SDavid Greenman }
9832fe6e4d7SDavid Greenman 
984df8bae1dSRodney W. Grimes /*
985df8bae1dSRodney W. Grimes  *	vm_object_collapse:
986df8bae1dSRodney W. Grimes  *
987df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
988df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
989df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
990df8bae1dSRodney W. Grimes  */
99126f9a767SRodney W. Grimes void
99226f9a767SRodney W. Grimes vm_object_collapse(object)
99324a1cce3SDavid Greenman 	vm_object_t object;
994df8bae1dSRodney W. Grimes 
995df8bae1dSRodney W. Grimes {
99624a1cce3SDavid Greenman 	vm_object_t backing_object;
997a316d390SJohn Dyson 	vm_ooffset_t backing_offset;
99824a1cce3SDavid Greenman 	vm_size_t size;
999a316d390SJohn Dyson 	vm_pindex_t new_pindex, backing_offset_index;
100024a1cce3SDavid Greenman 	vm_page_t p, pp;
1001df8bae1dSRodney W. Grimes 
1002df8bae1dSRodney W. Grimes 	while (TRUE) {
1003df8bae1dSRodney W. Grimes 		/*
1004df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1005df8bae1dSRodney W. Grimes 		 *
10060d94caffSDavid Greenman 		 * The object exists and no pages in it are currently being paged
10070d94caffSDavid Greenman 		 * out.
1008df8bae1dSRodney W. Grimes 		 */
10092fe6e4d7SDavid Greenman 		if (object == NULL)
1010df8bae1dSRodney W. Grimes 			return;
1011df8bae1dSRodney W. Grimes 
1012b9921222SDavid Greenman 		/*
1013b9921222SDavid Greenman 		 * Make sure there is a backing object.
1014b9921222SDavid Greenman 		 */
101524a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
1016df8bae1dSRodney W. Grimes 			return;
1017df8bae1dSRodney W. Grimes 
1018f919ebdeSDavid Greenman 		/*
1019f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
102024a1cce3SDavid Greenman 		 * not collapsable.
1021f919ebdeSDavid Greenman 		 */
102224a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
102324a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
102424a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1025f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
102624a1cce3SDavid Greenman 		    object->handle != NULL ||
102724a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
102824a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
102924a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
10309b4814bbSDavid Greenman 			return;
103124a1cce3SDavid Greenman 		}
10329b4814bbSDavid Greenman 
1033f919ebdeSDavid Greenman 		if (object->paging_in_progress != 0 ||
1034f919ebdeSDavid Greenman 		    backing_object->paging_in_progress != 0) {
1035b9921222SDavid Greenman 			vm_object_qcollapse(object);
1036df8bae1dSRodney W. Grimes 			return;
1037df8bae1dSRodney W. Grimes 		}
1038f919ebdeSDavid Greenman 
103926f9a767SRodney W. Grimes 		/*
10400d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
10410d94caffSDavid Greenman 		 * the parent is the only reference to it) or (perhaps) remove
10420d94caffSDavid Greenman 		 * the parent's reference to it.
1043df8bae1dSRodney W. Grimes 		 */
1044df8bae1dSRodney W. Grimes 
104524a1cce3SDavid Greenman 		backing_offset = object->backing_object_offset;
1046a316d390SJohn Dyson 		backing_offset_index = OFF_TO_IDX(backing_offset);
1047df8bae1dSRodney W. Grimes 		size = object->size;
1048df8bae1dSRodney W. Grimes 
1049df8bae1dSRodney W. Grimes 		/*
10500d94caffSDavid Greenman 		 * If there is exactly one reference to the backing object, we
10510d94caffSDavid Greenman 		 * can collapse it into the parent.
1052df8bae1dSRodney W. Grimes 		 */
1053df8bae1dSRodney W. Grimes 
1054df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1055df8bae1dSRodney W. Grimes 
1056a1f6d91cSDavid Greenman 			backing_object->flags |= OBJ_DEAD;
1057df8bae1dSRodney W. Grimes 			/*
1058df8bae1dSRodney W. Grimes 			 * We can collapse the backing object.
1059df8bae1dSRodney W. Grimes 			 *
10600d94caffSDavid Greenman 			 * Move all in-memory pages from backing_object to the
10610d94caffSDavid Greenman 			 * parent.  Pages that have been paged out will be
10620d94caffSDavid Greenman 			 * overwritten by any of the parent's pages that
10630d94caffSDavid Greenman 			 * shadow them.
1064df8bae1dSRodney W. Grimes 			 */
1065df8bae1dSRodney W. Grimes 
1066b18bfc3dSJohn Dyson 			while ((p = TAILQ_FIRST(&backing_object->memq)) != 0) {
106726f9a767SRodney W. Grimes 
1068a316d390SJohn Dyson 				new_pindex = p->pindex - backing_offset_index;
1069eaf13dd7SJohn Dyson 				p->flags |= PG_BUSY;
1070df8bae1dSRodney W. Grimes 
1071df8bae1dSRodney W. Grimes 				/*
10720d94caffSDavid Greenman 				 * If the parent has a page here, or if this
10730d94caffSDavid Greenman 				 * page falls outside the parent, dispose of
10740d94caffSDavid Greenman 				 * it.
1075df8bae1dSRodney W. Grimes 				 *
1076df8bae1dSRodney W. Grimes 				 * Otherwise, move it as planned.
1077df8bae1dSRodney W. Grimes 				 */
1078df8bae1dSRodney W. Grimes 
1079a316d390SJohn Dyson 				if (p->pindex < backing_offset_index ||
1080a316d390SJohn Dyson 				    new_pindex >= size) {
1081f919ebdeSDavid Greenman 					vm_page_protect(p, VM_PROT_NONE);
1082df8bae1dSRodney W. Grimes 					vm_page_free(p);
1083df8bae1dSRodney W. Grimes 				} else {
1084a316d390SJohn Dyson 					pp = vm_page_lookup(object, new_pindex);
108524a1cce3SDavid Greenman 					if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object,
1086a316d390SJohn Dyson 					    OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL))) {
1087f919ebdeSDavid Greenman 						vm_page_protect(p, VM_PROT_NONE);
1088df8bae1dSRodney W. Grimes 						vm_page_free(p);
108926f9a767SRodney W. Grimes 					} else {
1090edd97f3aSJohn Dyson 						if ((p->queue - p->pc) == PQ_CACHE)
1091edd97f3aSJohn Dyson 							vm_page_deactivate(p);
1092edd97f3aSJohn Dyson 						else
10936e20a165SJohn Dyson 							vm_page_protect(p, VM_PROT_NONE);
1094a316d390SJohn Dyson 						vm_page_rename(p, object, new_pindex);
10956e20a165SJohn Dyson 						p->dirty = VM_PAGE_BITS_ALL;
1096df8bae1dSRodney W. Grimes 					}
1097df8bae1dSRodney W. Grimes 				}
1098df8bae1dSRodney W. Grimes 			}
1099df8bae1dSRodney W. Grimes 
1100df8bae1dSRodney W. Grimes 			/*
1101df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1102df8bae1dSRodney W. Grimes 			 */
1103df8bae1dSRodney W. Grimes 
110424a1cce3SDavid Greenman 			if (backing_object->type == OBJT_SWAP) {
110526f9a767SRodney W. Grimes 				backing_object->paging_in_progress++;
110624a1cce3SDavid Greenman 				if (object->type == OBJT_SWAP) {
110726f9a767SRodney W. Grimes 					object->paging_in_progress++;
110826f9a767SRodney W. Grimes 					/*
110926f9a767SRodney W. Grimes 					 * copy shadow object pages into ours
11100d94caffSDavid Greenman 					 * and destroy unneeded pages in
11110d94caffSDavid Greenman 					 * shadow object.
111226f9a767SRodney W. Grimes 					 */
111326f9a767SRodney W. Grimes 					swap_pager_copy(
1114a316d390SJohn Dyson 					    backing_object,
1115a316d390SJohn Dyson 					    OFF_TO_IDX(backing_object->paging_offset),
1116a316d390SJohn Dyson 					    object,
1117a316d390SJohn Dyson 					    OFF_TO_IDX(object->paging_offset),
1118c0877f10SJohn Dyson 					    OFF_TO_IDX(object->backing_object_offset), TRUE);
1119f919ebdeSDavid Greenman 					vm_object_pip_wakeup(object);
112026f9a767SRodney W. Grimes 				} else {
112126f9a767SRodney W. Grimes 					object->paging_in_progress++;
112226f9a767SRodney W. Grimes 					/*
112324a1cce3SDavid Greenman 					 * move the shadow backing_object's pager data to
112424a1cce3SDavid Greenman 					 * "object" and convert "object" type to OBJT_SWAP.
112526f9a767SRodney W. Grimes 					 */
112624a1cce3SDavid Greenman 					object->type = OBJT_SWAP;
11272a4895f4SDavid Greenman 					object->un_pager.swp.swp_nblocks =
11282a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_nblocks;
11292a4895f4SDavid Greenman 					object->un_pager.swp.swp_allocsize =
11302a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_allocsize;
11312a4895f4SDavid Greenman 					object->un_pager.swp.swp_blocks =
11322a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_blocks;
11332a4895f4SDavid Greenman 					object->un_pager.swp.swp_poip =		/* XXX */
11342a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_poip;
113526f9a767SRodney W. Grimes 					object->paging_offset = backing_object->paging_offset + backing_offset;
113624a1cce3SDavid Greenman 					TAILQ_INSERT_TAIL(&swap_pager_un_object_list, object, pager_object_list);
113724a1cce3SDavid Greenman 
113824a1cce3SDavid Greenman 					/*
113924a1cce3SDavid Greenman 					 * Convert backing object from OBJT_SWAP to
114024a1cce3SDavid Greenman 					 * OBJT_DEFAULT. XXX - only the TAILQ_REMOVE is
114124a1cce3SDavid Greenman 					 * actually necessary.
114224a1cce3SDavid Greenman 					 */
114324a1cce3SDavid Greenman 					backing_object->type = OBJT_DEFAULT;
114424a1cce3SDavid Greenman 					TAILQ_REMOVE(&swap_pager_un_object_list, backing_object, pager_object_list);
114526f9a767SRodney W. Grimes 					/*
114626f9a767SRodney W. Grimes 					 * free unnecessary blocks
114726f9a767SRodney W. Grimes 					 */
1148a316d390SJohn Dyson 					swap_pager_freespace(object, 0,
1149a316d390SJohn Dyson 						OFF_TO_IDX(object->paging_offset));
1150f919ebdeSDavid Greenman 					vm_object_pip_wakeup(object);
1151c0503609SDavid Greenman 				}
1152c0503609SDavid Greenman 
1153f919ebdeSDavid Greenman 				vm_object_pip_wakeup(backing_object);
1154c0503609SDavid Greenman 			}
1155df8bae1dSRodney W. Grimes 			/*
1156df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
115724a1cce3SDavid Greenman 			 * Note that the reference to backing_object->backing_object
1158df8bae1dSRodney W. Grimes 			 * moves from within backing_object to within object.
1159df8bae1dSRodney W. Grimes 			 */
1160df8bae1dSRodney W. Grimes 
116124a1cce3SDavid Greenman 			TAILQ_REMOVE(&object->backing_object->shadow_head, object,
116224a1cce3SDavid Greenman 			    shadow_list);
1163eaf13dd7SJohn Dyson 			object->backing_object->shadow_count--;
1164eaf13dd7SJohn Dyson 			object->backing_object->generation++;
1165de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
116624a1cce3SDavid Greenman 				TAILQ_REMOVE(&backing_object->backing_object->shadow_head,
116724a1cce3SDavid Greenman 				    backing_object, shadow_list);
1168eaf13dd7SJohn Dyson 				backing_object->backing_object->shadow_count--;
1169eaf13dd7SJohn Dyson 				backing_object->backing_object->generation++;
1170de5f6a77SJohn Dyson 			}
117124a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
1172de5f6a77SJohn Dyson 			if (object->backing_object) {
117324a1cce3SDavid Greenman 				TAILQ_INSERT_TAIL(&object->backing_object->shadow_head,
117424a1cce3SDavid Greenman 				    object, shadow_list);
1175eaf13dd7SJohn Dyson 				object->backing_object->shadow_count++;
1176eaf13dd7SJohn Dyson 				object->backing_object->generation++;
1177de5f6a77SJohn Dyson 			}
11782fe6e4d7SDavid Greenman 
117924a1cce3SDavid Greenman 			object->backing_object_offset += backing_object->backing_object_offset;
1180df8bae1dSRodney W. Grimes 			/*
1181df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1182df8bae1dSRodney W. Grimes 			 *
11830d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
11840d94caffSDavid Greenman 			 * and no object references within it, all that is
11850d94caffSDavid Greenman 			 * necessary is to dispose of it.
1186df8bae1dSRodney W. Grimes 			 */
1187df8bae1dSRodney W. Grimes 
1188df8bae1dSRodney W. Grimes 			TAILQ_REMOVE(&vm_object_list, backing_object,
1189df8bae1dSRodney W. Grimes 			    object_list);
1190df8bae1dSRodney W. Grimes 			vm_object_count--;
1191df8bae1dSRodney W. Grimes 
119299448ed1SJohn Dyson 			zfree(obj_zone, backing_object);
1193df8bae1dSRodney W. Grimes 
1194df8bae1dSRodney W. Grimes 			object_collapses++;
11950d94caffSDavid Greenman 		} else {
119695e5e988SJohn Dyson 			vm_object_t new_backing_object;
1197df8bae1dSRodney W. Grimes 			/*
1198df8bae1dSRodney W. Grimes 			 * If all of the pages in the backing object are
11990d94caffSDavid Greenman 			 * shadowed by the parent object, the parent object no
12000d94caffSDavid Greenman 			 * longer has to shadow the backing object; it can
12010d94caffSDavid Greenman 			 * shadow the next one in the chain.
1202df8bae1dSRodney W. Grimes 			 *
12030d94caffSDavid Greenman 			 * The backing object must not be paged out - we'd have
12040d94caffSDavid Greenman 			 * to check all of the paged-out pages, as well.
1205df8bae1dSRodney W. Grimes 			 */
1206df8bae1dSRodney W. Grimes 
120724a1cce3SDavid Greenman 			if (backing_object->type != OBJT_DEFAULT) {
1208df8bae1dSRodney W. Grimes 				return;
1209df8bae1dSRodney W. Grimes 			}
1210df8bae1dSRodney W. Grimes 			/*
12110d94caffSDavid Greenman 			 * Should have a check for a 'small' number of pages
12120d94caffSDavid Greenman 			 * here.
1213df8bae1dSRodney W. Grimes 			 */
1214df8bae1dSRodney W. Grimes 
1215eaf13dd7SJohn Dyson 			for (p = TAILQ_FIRST(&backing_object->memq); p;
1216eaf13dd7SJohn Dyson 					p = TAILQ_NEXT(p, listq)) {
1217eaf13dd7SJohn Dyson 
1218a316d390SJohn Dyson 				new_pindex = p->pindex - backing_offset_index;
1219ffc82b0aSJohn Dyson 				p->flags |= PG_BUSY;
1220df8bae1dSRodney W. Grimes 
1221df8bae1dSRodney W. Grimes 				/*
12220d94caffSDavid Greenman 				 * If the parent has a page here, or if this
12230d94caffSDavid Greenman 				 * page falls outside the parent, keep going.
1224df8bae1dSRodney W. Grimes 				 *
12250d94caffSDavid Greenman 				 * Otherwise, the backing_object must be left in
12260d94caffSDavid Greenman 				 * the chain.
1227df8bae1dSRodney W. Grimes 				 */
1228df8bae1dSRodney W. Grimes 
1229a316d390SJohn Dyson 				if (p->pindex >= backing_offset_index &&
1230a316d390SJohn Dyson 					new_pindex <= size) {
123124a1cce3SDavid Greenman 
1232a316d390SJohn Dyson 					pp = vm_page_lookup(object, new_pindex);
123324a1cce3SDavid Greenman 
1234eaf13dd7SJohn Dyson 					if ((pp == NULL) || (pp->flags & PG_BUSY) || pp->busy) {
1235eaf13dd7SJohn Dyson 						PAGE_WAKEUP(p);
1236eaf13dd7SJohn Dyson 						return;
1237eaf13dd7SJohn Dyson 					}
1238eaf13dd7SJohn Dyson 
1239eaf13dd7SJohn Dyson 					pp->flags |= PG_BUSY;
1240eaf13dd7SJohn Dyson 					if ((pp->valid == 0) &&
1241a316d390SJohn Dyson 				   	    !vm_pager_has_page(object, OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL)) {
1242df8bae1dSRodney W. Grimes 						/*
12430d94caffSDavid Greenman 						 * Page still needed. Can't go any
12440d94caffSDavid Greenman 						 * further.
1245df8bae1dSRodney W. Grimes 						 */
1246eaf13dd7SJohn Dyson 						PAGE_WAKEUP(pp);
1247eaf13dd7SJohn Dyson 						PAGE_WAKEUP(p);
1248df8bae1dSRodney W. Grimes 						return;
1249df8bae1dSRodney W. Grimes 					}
1250eaf13dd7SJohn Dyson 					PAGE_WAKEUP(pp);
1251df8bae1dSRodney W. Grimes 				}
1252eaf13dd7SJohn Dyson 				PAGE_WAKEUP(p);
125324a1cce3SDavid Greenman 			}
1254df8bae1dSRodney W. Grimes 
1255df8bae1dSRodney W. Grimes 			/*
12560d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
12570d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
12580d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1259df8bae1dSRodney W. Grimes 			 */
1260df8bae1dSRodney W. Grimes 
126195e5e988SJohn Dyson 			TAILQ_REMOVE(&backing_object->shadow_head,
126224a1cce3SDavid Greenman 			    object, shadow_list);
1263eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
1264eaf13dd7SJohn Dyson 			backing_object->generation++;
126595e5e988SJohn Dyson 
126695e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
126795e5e988SJohn Dyson 			if (object->backing_object = new_backing_object) {
126895e5e988SJohn Dyson 				vm_object_reference(new_backing_object);
126995e5e988SJohn Dyson 				TAILQ_INSERT_TAIL(&new_backing_object->shadow_head,
127024a1cce3SDavid Greenman 				    object, shadow_list);
1271eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1272eaf13dd7SJohn Dyson 				new_backing_object->generation++;
127395e5e988SJohn Dyson 				object->backing_object_offset +=
127495e5e988SJohn Dyson 					backing_object->backing_object_offset;
1275de5f6a77SJohn Dyson 			}
1276df8bae1dSRodney W. Grimes 
1277df8bae1dSRodney W. Grimes 			/*
12780d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
12790d94caffSDavid Greenman 			 * its ref_count was at least 2, it will not vanish;
1280eaf13dd7SJohn Dyson 			 * so we don't need to call vm_object_deallocate, but
1281eaf13dd7SJohn Dyson 			 * we do anyway.
1282df8bae1dSRodney W. Grimes 			 */
128395e5e988SJohn Dyson 			vm_object_deallocate(backing_object);
1284df8bae1dSRodney W. Grimes 			object_bypasses++;
1285df8bae1dSRodney W. Grimes 		}
1286df8bae1dSRodney W. Grimes 
1287df8bae1dSRodney W. Grimes 		/*
1288df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1289df8bae1dSRodney W. Grimes 		 */
1290df8bae1dSRodney W. Grimes 	}
1291df8bae1dSRodney W. Grimes }
1292df8bae1dSRodney W. Grimes 
1293df8bae1dSRodney W. Grimes /*
1294df8bae1dSRodney W. Grimes  *	vm_object_page_remove: [internal]
1295df8bae1dSRodney W. Grimes  *
1296df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
1297df8bae1dSRodney W. Grimes  *	object range from the object's list of pages.
1298df8bae1dSRodney W. Grimes  *
1299df8bae1dSRodney W. Grimes  *	The object must be locked.
1300df8bae1dSRodney W. Grimes  */
130126f9a767SRodney W. Grimes void
13027c1f6cedSDavid Greenman vm_object_page_remove(object, start, end, clean_only)
1303df8bae1dSRodney W. Grimes 	register vm_object_t object;
1304a316d390SJohn Dyson 	register vm_pindex_t start;
1305a316d390SJohn Dyson 	register vm_pindex_t end;
13067c1f6cedSDavid Greenman 	boolean_t clean_only;
1307df8bae1dSRodney W. Grimes {
1308df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
1309a316d390SJohn Dyson 	unsigned int size;
131095e5e988SJohn Dyson 	int s, all;
1311df8bae1dSRodney W. Grimes 
1312df8bae1dSRodney W. Grimes 	if (object == NULL)
1313df8bae1dSRodney W. Grimes 		return;
1314df8bae1dSRodney W. Grimes 
131595e5e988SJohn Dyson 	all = ((end == 0) && (start == 0));
131695e5e988SJohn Dyson 
13172fe6e4d7SDavid Greenman 	object->paging_in_progress++;
131826f9a767SRodney W. Grimes again:
131926f9a767SRodney W. Grimes 	size = end - start;
132095e5e988SJohn Dyson 	if (all || size > 4 || size >= object->size / 4) {
1321b18bfc3dSJohn Dyson 		for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1322b18bfc3dSJohn Dyson 			next = TAILQ_NEXT(p, listq);
132395e5e988SJohn Dyson 			if (all || ((start <= p->pindex) && (p->pindex < end))) {
1324bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1325bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1326bd7e5f99SJohn Dyson 					p->valid = 0;
13270d94caffSDavid Greenman 					continue;
13280d94caffSDavid Greenman 				}
13290891ef4cSJohn Dyson 
1330b18bfc3dSJohn Dyson 				/*
1331b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1332b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1333b18bfc3dSJohn Dyson 				 */
1334ffc82b0aSJohn Dyson 
1335ffc82b0aSJohn Dyson  				if (vm_page_sleep(p, "vmopar", &p->busy))
133626f9a767SRodney W. Grimes  					goto again;
13370891ef4cSJohn Dyson 
13388f9110f6SJohn Dyson 				if (clean_only && p->valid) {
13397c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
13407c1f6cedSDavid Greenman 					if (p->valid & p->dirty)
13417c1f6cedSDavid Greenman 						continue;
13427c1f6cedSDavid Greenman 				}
1343eaf13dd7SJohn Dyson 
1344eaf13dd7SJohn Dyson 				p->flags |= PG_BUSY;
1345f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
1346df8bae1dSRodney W. Grimes 				vm_page_free(p);
134726f9a767SRodney W. Grimes 			}
134826f9a767SRodney W. Grimes 		}
134926f9a767SRodney W. Grimes 	} else {
135026f9a767SRodney W. Grimes 		while (size > 0) {
1351bd7e5f99SJohn Dyson 			if ((p = vm_page_lookup(object, start)) != 0) {
1352eaf13dd7SJohn Dyson 
1353bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1354bd7e5f99SJohn Dyson 					p->valid = 0;
1355bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1356bd7e5f99SJohn Dyson 					start += 1;
1357bd7e5f99SJohn Dyson 					size -= 1;
1358bd7e5f99SJohn Dyson 					continue;
13590d94caffSDavid Greenman 				}
1360eaf13dd7SJohn Dyson 
1361b18bfc3dSJohn Dyson 				/*
1362b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1363b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1364b18bfc3dSJohn Dyson 				 */
1365ffc82b0aSJohn Dyson  				if (vm_page_sleep(p, "vmopar", &p->busy))
136626f9a767SRodney W. Grimes 					goto again;
1367eaf13dd7SJohn Dyson 
13688f9110f6SJohn Dyson 				if (clean_only && p->valid) {
13697c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
1370bd7e5f99SJohn Dyson 					if (p->valid & p->dirty) {
1371bd7e5f99SJohn Dyson 						start += 1;
1372bd7e5f99SJohn Dyson 						size -= 1;
13737c1f6cedSDavid Greenman 						continue;
13747c1f6cedSDavid Greenman 					}
1375bd7e5f99SJohn Dyson 				}
1376eaf13dd7SJohn Dyson 
1377eaf13dd7SJohn Dyson 				p->flags |= PG_BUSY;
1378f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
137926f9a767SRodney W. Grimes 				vm_page_free(p);
138026f9a767SRodney W. Grimes 			}
1381a316d390SJohn Dyson 			start += 1;
1382a316d390SJohn Dyson 			size -= 1;
1383df8bae1dSRodney W. Grimes 		}
1384df8bae1dSRodney W. Grimes 	}
1385f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
1386c0503609SDavid Greenman }
1387df8bae1dSRodney W. Grimes 
1388df8bae1dSRodney W. Grimes /*
1389df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1390df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1391df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1392df8bae1dSRodney W. Grimes  *
1393df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1394df8bae1dSRodney W. Grimes  *
1395df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1396df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1397df8bae1dSRodney W. Grimes  *
1398df8bae1dSRodney W. Grimes  *	Parameters:
1399df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1400df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1401df8bae1dSRodney W. Grimes  *		next_object	Second object into coalesce
1402df8bae1dSRodney W. Grimes  *		next_offset	Offset into next_object
1403df8bae1dSRodney W. Grimes  *
1404df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
1405df8bae1dSRodney W. Grimes  *		next_size	Size of reference to next_object
1406df8bae1dSRodney W. Grimes  *
1407df8bae1dSRodney W. Grimes  *	Conditions:
1408df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1409df8bae1dSRodney W. Grimes  */
14100d94caffSDavid Greenman boolean_t
1411a316d390SJohn Dyson vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size)
1412df8bae1dSRodney W. Grimes 	register vm_object_t prev_object;
1413a316d390SJohn Dyson 	vm_pindex_t prev_pindex;
1414df8bae1dSRodney W. Grimes 	vm_size_t prev_size, next_size;
1415df8bae1dSRodney W. Grimes {
1416df8bae1dSRodney W. Grimes 	vm_size_t newsize;
1417df8bae1dSRodney W. Grimes 
1418df8bae1dSRodney W. Grimes 	if (prev_object == NULL) {
1419df8bae1dSRodney W. Grimes 		return (TRUE);
1420df8bae1dSRodney W. Grimes 	}
1421df8bae1dSRodney W. Grimes 
142230dcfc09SJohn Dyson 	if (prev_object->type != OBJT_DEFAULT) {
142330dcfc09SJohn Dyson 		return (FALSE);
142430dcfc09SJohn Dyson 	}
142530dcfc09SJohn Dyson 
1426df8bae1dSRodney W. Grimes 	/*
1427df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1428df8bae1dSRodney W. Grimes 	 */
1429df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1430df8bae1dSRodney W. Grimes 
1431df8bae1dSRodney W. Grimes 	/*
14320d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
14330d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
14340d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1435df8bae1dSRodney W. Grimes 	 */
1436df8bae1dSRodney W. Grimes 
14378cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
1438df8bae1dSRodney W. Grimes 		return (FALSE);
1439df8bae1dSRodney W. Grimes 	}
1440a316d390SJohn Dyson 
1441a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
1442a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
14438cc7e047SJohn Dyson 
14448cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
14458cc7e047SJohn Dyson 	    (prev_object->size != prev_pindex + prev_size)) {
14468cc7e047SJohn Dyson 		return (FALSE);
14478cc7e047SJohn Dyson 	}
14488cc7e047SJohn Dyson 
1449df8bae1dSRodney W. Grimes 	/*
14500d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
14510d94caffSDavid Greenman 	 * deallocation.
1452df8bae1dSRodney W. Grimes 	 */
1453df8bae1dSRodney W. Grimes 
1454df8bae1dSRodney W. Grimes 	vm_object_page_remove(prev_object,
1455a316d390SJohn Dyson 	    prev_pindex + prev_size,
1456a316d390SJohn Dyson 	    prev_pindex + prev_size + next_size, FALSE);
1457df8bae1dSRodney W. Grimes 
1458df8bae1dSRodney W. Grimes 	/*
1459df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1460df8bae1dSRodney W. Grimes 	 */
1461a316d390SJohn Dyson 	newsize = prev_pindex + prev_size + next_size;
1462df8bae1dSRodney W. Grimes 	if (newsize > prev_object->size)
1463df8bae1dSRodney W. Grimes 		prev_object->size = newsize;
1464df8bae1dSRodney W. Grimes 
1465df8bae1dSRodney W. Grimes 	return (TRUE);
1466df8bae1dSRodney W. Grimes }
1467df8bae1dSRodney W. Grimes 
1468c7c34a24SBruce Evans #include "opt_ddb.h"
1469c3cb3e12SDavid Greenman #ifdef DDB
1470c7c34a24SBruce Evans #include <sys/kernel.h>
1471c7c34a24SBruce Evans 
1472c7c34a24SBruce Evans #include <machine/cons.h>
1473c7c34a24SBruce Evans 
1474c7c34a24SBruce Evans #include <ddb/ddb.h>
1475c7c34a24SBruce Evans 
1476c7c34a24SBruce Evans static int	_vm_object_in_map __P((vm_map_t map, vm_object_t object,
1477c7c34a24SBruce Evans 				       vm_map_entry_t entry));
1478c7c34a24SBruce Evans static int	vm_object_in_map __P((vm_object_t object));
1479c3cb3e12SDavid Greenman 
1480cac597e4SBruce Evans static int
1481a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry)
1482a1f6d91cSDavid Greenman 	vm_map_t map;
1483a1f6d91cSDavid Greenman 	vm_object_t object;
1484a1f6d91cSDavid Greenman 	vm_map_entry_t entry;
1485a1f6d91cSDavid Greenman {
1486a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1487a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1488a1f6d91cSDavid Greenman 	vm_object_t obj;
1489a1f6d91cSDavid Greenman 	int entcount;
1490a1f6d91cSDavid Greenman 
1491a1f6d91cSDavid Greenman 	if (map == 0)
1492a1f6d91cSDavid Greenman 		return 0;
1493a1f6d91cSDavid Greenman 
1494a1f6d91cSDavid Greenman 	if (entry == 0) {
1495a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1496a1f6d91cSDavid Greenman 		entcount = map->nentries;
1497a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1498a1f6d91cSDavid Greenman 			if( _vm_object_in_map(map, object, tmpe)) {
1499a1f6d91cSDavid Greenman 				return 1;
1500a1f6d91cSDavid Greenman 			}
1501a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1502a1f6d91cSDavid Greenman 		}
1503afa07f7eSJohn Dyson 	} else if (entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) {
1504a1f6d91cSDavid Greenman 		tmpm = entry->object.share_map;
1505a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1506a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1507a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1508a1f6d91cSDavid Greenman 			if( _vm_object_in_map(tmpm, object, tmpe)) {
1509a1f6d91cSDavid Greenman 				return 1;
1510a1f6d91cSDavid Greenman 			}
1511a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1512a1f6d91cSDavid Greenman 		}
1513a1f6d91cSDavid Greenman 	} else if (obj = entry->object.vm_object) {
151424a1cce3SDavid Greenman 		for(; obj; obj=obj->backing_object)
1515a1f6d91cSDavid Greenman 			if( obj == object) {
1516a1f6d91cSDavid Greenman 				return 1;
1517a1f6d91cSDavid Greenman 			}
1518a1f6d91cSDavid Greenman 	}
1519a1f6d91cSDavid Greenman 	return 0;
1520a1f6d91cSDavid Greenman }
1521a1f6d91cSDavid Greenman 
1522cac597e4SBruce Evans static int
1523a1f6d91cSDavid Greenman vm_object_in_map( object)
1524a1f6d91cSDavid Greenman 	vm_object_t object;
1525a1f6d91cSDavid Greenman {
1526a1f6d91cSDavid Greenman 	struct proc *p;
15271b67ec6dSJeffrey Hsu 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1528a1f6d91cSDavid Greenman 		if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1529a1f6d91cSDavid Greenman 			continue;
1530a1f6d91cSDavid Greenman 		if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1531a1f6d91cSDavid Greenman 			return 1;
1532a1f6d91cSDavid Greenman 	}
1533a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kernel_map, object, 0))
1534a1f6d91cSDavid Greenman 		return 1;
1535a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kmem_map, object, 0))
1536a1f6d91cSDavid Greenman 		return 1;
1537a1f6d91cSDavid Greenman 	if( _vm_object_in_map( pager_map, object, 0))
1538a1f6d91cSDavid Greenman 		return 1;
1539a1f6d91cSDavid Greenman 	if( _vm_object_in_map( buffer_map, object, 0))
1540a1f6d91cSDavid Greenman 		return 1;
1541a1f6d91cSDavid Greenman 	if( _vm_object_in_map( io_map, object, 0))
1542a1f6d91cSDavid Greenman 		return 1;
1543a1f6d91cSDavid Greenman 	if( _vm_object_in_map( phys_map, object, 0))
1544a1f6d91cSDavid Greenman 		return 1;
1545a1f6d91cSDavid Greenman 	if( _vm_object_in_map( mb_map, object, 0))
1546a1f6d91cSDavid Greenman 		return 1;
1547a1f6d91cSDavid Greenman 	if( _vm_object_in_map( u_map, object, 0))
1548a1f6d91cSDavid Greenman 		return 1;
1549a1f6d91cSDavid Greenman 	return 0;
1550a1f6d91cSDavid Greenman }
1551a1f6d91cSDavid Greenman 
1552c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
1553f708ef1bSPoul-Henning Kamp {
1554a1f6d91cSDavid Greenman 	vm_object_t object;
1555a1f6d91cSDavid Greenman 
1556a1f6d91cSDavid Greenman 	/*
1557a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
1558a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
1559a1f6d91cSDavid Greenman 	 */
1560b18bfc3dSJohn Dyson 	for (object = TAILQ_FIRST(&vm_object_list);
1561a1f6d91cSDavid Greenman 			object != NULL;
1562b18bfc3dSJohn Dyson 			object = TAILQ_NEXT(object, object_list)) {
156324a1cce3SDavid Greenman 		if (object->handle == NULL &&
156424a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1565a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
1566c7c34a24SBruce Evans 				db_printf("vmochk: internal obj has zero ref count: %d\n",
1567a1f6d91cSDavid Greenman 					object->size);
1568a1f6d91cSDavid Greenman 			}
1569a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
1570fc62ef1fSBruce Evans 				db_printf(
1571fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
1572fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
1573fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
1574fc62ef1fSBruce Evans 				    (u_long)object->size,
1575fc62ef1fSBruce Evans 				    (void *)object->backing_object);
1576a1f6d91cSDavid Greenman 			}
1577a1f6d91cSDavid Greenman 		}
1578a1f6d91cSDavid Greenman 	}
1579a1f6d91cSDavid Greenman }
1580a1f6d91cSDavid Greenman 
158126f9a767SRodney W. Grimes /*
1582df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
1583df8bae1dSRodney W. Grimes  */
1584c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
1585df8bae1dSRodney W. Grimes {
1586c7c34a24SBruce Evans 	/* XXX convert args. */
1587c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
1588c7c34a24SBruce Evans 	boolean_t full = have_addr;
1589c7c34a24SBruce Evans 
1590df8bae1dSRodney W. Grimes 	register vm_page_t p;
1591df8bae1dSRodney W. Grimes 
1592c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
1593c7c34a24SBruce Evans #define	count	was_count
1594c7c34a24SBruce Evans 
1595df8bae1dSRodney W. Grimes 	register int count;
1596df8bae1dSRodney W. Grimes 
1597df8bae1dSRodney W. Grimes 	if (object == NULL)
1598df8bae1dSRodney W. Grimes 		return;
1599df8bae1dSRodney W. Grimes 
1600ecbb00a2SDoug Rabson 	db_iprintf("Object 0x%lx: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
1601ecbb00a2SDoug Rabson 	    (long) object, (int) object->type, (long) object->size,
160295e5e988SJohn Dyson 	    object->resident_page_count,
160395e5e988SJohn Dyson 		object->ref_count,
160495e5e988SJohn Dyson 		object->flags);
1605ecbb00a2SDoug Rabson 	db_iprintf(" sref=%d, offset=0x%x, backing_object(%d)=(0x%lx)+0x%x\n",
160695e5e988SJohn Dyson 		object->shadow_count,
16072a4895f4SDavid Greenman 	    (int) object->paging_offset,
1608ecbb00a2SDoug Rabson 		(((long)object->backing_object)?object->backing_object->ref_count:0),
1609ecbb00a2SDoug Rabson 	    (long) object->backing_object,
161095e5e988SJohn Dyson 		(int) object->backing_object_offset);
1611df8bae1dSRodney W. Grimes 
1612df8bae1dSRodney W. Grimes 	if (!full)
1613df8bae1dSRodney W. Grimes 		return;
1614df8bae1dSRodney W. Grimes 
1615c7c34a24SBruce Evans 	db_indent += 2;
1616df8bae1dSRodney W. Grimes 	count = 0;
1617b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = TAILQ_NEXT(p, listq)) {
1618df8bae1dSRodney W. Grimes 		if (count == 0)
1619c7c34a24SBruce Evans 			db_iprintf("memory:=");
1620df8bae1dSRodney W. Grimes 		else if (count == 6) {
1621c7c34a24SBruce Evans 			db_printf("\n");
1622c7c34a24SBruce Evans 			db_iprintf(" ...");
1623df8bae1dSRodney W. Grimes 			count = 0;
1624df8bae1dSRodney W. Grimes 		} else
1625c7c34a24SBruce Evans 			db_printf(",");
1626df8bae1dSRodney W. Grimes 		count++;
1627df8bae1dSRodney W. Grimes 
1628c7c34a24SBruce Evans 		db_printf("(off=0x%lx,page=0x%lx)",
1629a316d390SJohn Dyson 		    (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
1630df8bae1dSRodney W. Grimes 	}
1631df8bae1dSRodney W. Grimes 	if (count != 0)
1632c7c34a24SBruce Evans 		db_printf("\n");
1633c7c34a24SBruce Evans 	db_indent -= 2;
1634df8bae1dSRodney W. Grimes }
16355070c7f8SJohn Dyson 
1636c7c34a24SBruce Evans /* XXX. */
1637c7c34a24SBruce Evans #undef count
1638c7c34a24SBruce Evans 
1639c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
16405070c7f8SJohn Dyson void
1641c7c34a24SBruce Evans vm_object_print(addr, have_addr, count, modif)
1642ecbb00a2SDoug Rabson         /* db_expr_t */ long addr;
1643c7c34a24SBruce Evans 	boolean_t have_addr;
1644ecbb00a2SDoug Rabson 	/* db_expr_t */ long count;
1645c7c34a24SBruce Evans 	char *modif;
1646c7c34a24SBruce Evans {
1647c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
1648c7c34a24SBruce Evans }
1649c7c34a24SBruce Evans 
1650c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
16515070c7f8SJohn Dyson {
16525070c7f8SJohn Dyson 	vm_object_t object;
16535070c7f8SJohn Dyson 	int nl = 0;
16545070c7f8SJohn Dyson 	int c;
16555070c7f8SJohn Dyson 	for (object = TAILQ_FIRST(&vm_object_list);
16565070c7f8SJohn Dyson 			object != NULL;
16575070c7f8SJohn Dyson 			object = TAILQ_NEXT(object, object_list)) {
16585070c7f8SJohn Dyson 		vm_pindex_t idx, fidx;
16595070c7f8SJohn Dyson 		vm_pindex_t osize;
16605070c7f8SJohn Dyson 		vm_offset_t pa = -1, padiff;
16615070c7f8SJohn Dyson 		int rcount;
16625070c7f8SJohn Dyson 		vm_page_t m;
16635070c7f8SJohn Dyson 
1664fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
16655070c7f8SJohn Dyson 		if ( nl > 18) {
16665070c7f8SJohn Dyson 			c = cngetc();
16675070c7f8SJohn Dyson 			if (c != ' ')
16685070c7f8SJohn Dyson 				return;
16695070c7f8SJohn Dyson 			nl = 0;
16705070c7f8SJohn Dyson 		}
16715070c7f8SJohn Dyson 		nl++;
16725070c7f8SJohn Dyson 		rcount = 0;
16735070c7f8SJohn Dyson 		fidx = 0;
16745070c7f8SJohn Dyson 		osize = object->size;
16755070c7f8SJohn Dyson 		if (osize > 128)
16765070c7f8SJohn Dyson 			osize = 128;
16775070c7f8SJohn Dyson 		for(idx=0;idx<osize;idx++) {
16785070c7f8SJohn Dyson 			m = vm_page_lookup(object, idx);
16795070c7f8SJohn Dyson 			if (m == NULL) {
16805070c7f8SJohn Dyson 				if (rcount) {
16815070c7f8SJohn Dyson 					db_printf(" index(%d)run(%d)pa(0x%x)\n",
16825070c7f8SJohn Dyson 						fidx, rcount, pa);
16835070c7f8SJohn Dyson 					if ( nl > 18) {
16845070c7f8SJohn Dyson 						c = cngetc();
16855070c7f8SJohn Dyson 						if (c != ' ')
16865070c7f8SJohn Dyson 							return;
16875070c7f8SJohn Dyson 						nl = 0;
16885070c7f8SJohn Dyson 					}
16895070c7f8SJohn Dyson 					nl++;
16905070c7f8SJohn Dyson 					rcount = 0;
16915070c7f8SJohn Dyson 				}
16925070c7f8SJohn Dyson 				continue;
16935070c7f8SJohn Dyson 			}
16945070c7f8SJohn Dyson 
16955070c7f8SJohn Dyson 
16965070c7f8SJohn Dyson 			if (rcount &&
16975070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
16985070c7f8SJohn Dyson 				++rcount;
16995070c7f8SJohn Dyson 				continue;
17005070c7f8SJohn Dyson 			}
17015070c7f8SJohn Dyson 			if (rcount) {
17025070c7f8SJohn Dyson 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
17035070c7f8SJohn Dyson 				padiff >>= PAGE_SHIFT;
17045070c7f8SJohn Dyson 				padiff &= PQ_L2_MASK;
17055070c7f8SJohn Dyson 				if (padiff == 0) {
17065070c7f8SJohn Dyson 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
17075070c7f8SJohn Dyson 					++rcount;
17085070c7f8SJohn Dyson 					continue;
17095070c7f8SJohn Dyson 				}
17105070c7f8SJohn Dyson 				db_printf(" index(%d)run(%d)pa(0x%x)", fidx, rcount, pa);
17115070c7f8SJohn Dyson 				db_printf("pd(%d)\n", padiff);
17125070c7f8SJohn Dyson 				if ( nl > 18) {
17135070c7f8SJohn Dyson 					c = cngetc();
17145070c7f8SJohn Dyson 					if (c != ' ')
17155070c7f8SJohn Dyson 						return;
17165070c7f8SJohn Dyson 					nl = 0;
17175070c7f8SJohn Dyson 				}
17185070c7f8SJohn Dyson 				nl++;
17195070c7f8SJohn Dyson 			}
17205070c7f8SJohn Dyson 			fidx = idx;
17215070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
17225070c7f8SJohn Dyson 			rcount = 1;
17235070c7f8SJohn Dyson 		}
17245070c7f8SJohn Dyson 		if (rcount) {
17255070c7f8SJohn Dyson 			db_printf(" index(%d)run(%d)pa(0x%x)\n", fidx, rcount, pa);
17265070c7f8SJohn Dyson 			if ( nl > 18) {
17275070c7f8SJohn Dyson 				c = cngetc();
17285070c7f8SJohn Dyson 				if (c != ' ')
17295070c7f8SJohn Dyson 					return;
17305070c7f8SJohn Dyson 				nl = 0;
17315070c7f8SJohn Dyson 			}
17325070c7f8SJohn Dyson 			nl++;
17335070c7f8SJohn Dyson 		}
17345070c7f8SJohn Dyson 	}
17355070c7f8SJohn Dyson }
1736c3cb3e12SDavid Greenman #endif /* DDB */
1737