xref: /freebsd/sys/vm/vm_object.c (revision bef608bd7ee7f08b6e3ff2aad04c62e5d756fd3a)
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  *
64bef608bdSJohn Dyson  * $Id: vm_object.c,v 1.118 1998/03/08 18:05:59 dyson 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>
77df8bae1dSRodney W. Grimes 
78df8bae1dSRodney W. Grimes #include <vm/vm.h>
79efeaf95aSDavid Greenman #include <vm/vm_param.h>
80efeaf95aSDavid Greenman #include <vm/vm_prot.h>
81996c772fSJohn Dyson #include <sys/lock.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];
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;
153867a482dSJohn Dyson 	object->behavior = OBJ_NORMAL;
154df8bae1dSRodney W. Grimes 	object->paging_in_progress = 0;
155a1f6d91cSDavid Greenman 	object->resident_page_count = 0;
156eaf13dd7SJohn Dyson 	object->cache_count = 0;
157eaf13dd7SJohn Dyson 	object->wire_count = 0;
158de5f6a77SJohn Dyson 	object->shadow_count = 0;
1595070c7f8SJohn Dyson 	object->pg_color = next_index;
16099448ed1SJohn Dyson 	if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
16199448ed1SJohn Dyson 		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
16299448ed1SJohn Dyson 	else
16399448ed1SJohn Dyson 		incr = size;
16499448ed1SJohn Dyson 	next_index = (next_index + incr) & PQ_L2_MASK;
16524a1cce3SDavid Greenman 	object->handle = NULL;
166a316d390SJohn Dyson 	object->paging_offset = (vm_ooffset_t) 0;
16724a1cce3SDavid Greenman 	object->backing_object = NULL;
168a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
169a2f4a846SJohn Dyson 	object->page_hint = NULL;
170a1f6d91cSDavid Greenman 
171a1f6d91cSDavid Greenman 	object->last_read = 0;
1722d8acc0fSJohn Dyson 	object->generation++;
173df8bae1dSRodney W. Grimes 
174df8bae1dSRodney W. Grimes 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
175df8bae1dSRodney W. Grimes 	vm_object_count++;
176df8bae1dSRodney W. Grimes }
177df8bae1dSRodney W. Grimes 
178df8bae1dSRodney W. Grimes /*
17926f9a767SRodney W. Grimes  *	vm_object_init:
18026f9a767SRodney W. Grimes  *
18126f9a767SRodney W. Grimes  *	Initialize the VM objects module.
18226f9a767SRodney W. Grimes  */
18326f9a767SRodney W. Grimes void
184a316d390SJohn Dyson vm_object_init()
18526f9a767SRodney W. Grimes {
18626f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
187996c772fSJohn Dyson 	simple_lock_init(&vm_object_list_lock);
18826f9a767SRodney W. Grimes 	vm_object_count = 0;
1890217125fSDavid Greenman 
19026f9a767SRodney W. Grimes 	kernel_object = &kernel_object_store;
191a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
19226f9a767SRodney W. Grimes 	    kernel_object);
19326f9a767SRodney W. Grimes 
19426f9a767SRodney W. Grimes 	kmem_object = &kmem_object_store;
195a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
19626f9a767SRodney W. Grimes 	    kmem_object);
19799448ed1SJohn Dyson 
19899448ed1SJohn Dyson 	obj_zone = &obj_zone_store;
19999448ed1SJohn Dyson 	zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
20099448ed1SJohn Dyson 		vm_objects_init, VM_OBJECTS_INIT);
20199448ed1SJohn Dyson }
20299448ed1SJohn Dyson 
20399448ed1SJohn Dyson void
20499448ed1SJohn Dyson vm_object_init2() {
2050a80f406SJohn Dyson 	zinitna(obj_zone, NULL, NULL, 0, 0, 0, 1);
20626f9a767SRodney W. Grimes }
20726f9a767SRodney W. Grimes 
20826f9a767SRodney W. Grimes /*
20926f9a767SRodney W. Grimes  *	vm_object_allocate:
21026f9a767SRodney W. Grimes  *
21126f9a767SRodney W. Grimes  *	Returns a new object with the given size.
21226f9a767SRodney W. Grimes  */
21326f9a767SRodney W. Grimes 
21426f9a767SRodney W. Grimes vm_object_t
21524a1cce3SDavid Greenman vm_object_allocate(type, size)
21624a1cce3SDavid Greenman 	objtype_t type;
21726f9a767SRodney W. Grimes 	vm_size_t size;
21826f9a767SRodney W. Grimes {
21926f9a767SRodney W. Grimes 	register vm_object_t result;
22099448ed1SJohn Dyson 	result = (vm_object_t) zalloc(obj_zone);
22167bf6868SJohn Dyson 
22224a1cce3SDavid Greenman 	_vm_object_allocate(type, size, result);
22326f9a767SRodney W. Grimes 
22426f9a767SRodney W. Grimes 	return (result);
22526f9a767SRodney W. Grimes }
22626f9a767SRodney W. Grimes 
22726f9a767SRodney W. Grimes 
22826f9a767SRodney W. Grimes /*
229df8bae1dSRodney W. Grimes  *	vm_object_reference:
230df8bae1dSRodney W. Grimes  *
231df8bae1dSRodney W. Grimes  *	Gets another reference to the given object.
232df8bae1dSRodney W. Grimes  */
2336476c0d2SJohn Dyson void
23426f9a767SRodney W. Grimes vm_object_reference(object)
235df8bae1dSRodney W. Grimes 	register vm_object_t object;
236df8bae1dSRodney W. Grimes {
237df8bae1dSRodney W. Grimes 	if (object == NULL)
238df8bae1dSRodney W. Grimes 		return;
23995e5e988SJohn Dyson 
24095e5e988SJohn Dyson #if defined(DIAGNOSTIC)
24195e5e988SJohn Dyson 	if (object->flags & OBJ_DEAD)
24295e5e988SJohn Dyson 		panic("vm_object_reference: attempting to reference dead obj");
24395e5e988SJohn Dyson #endif
24495e5e988SJohn Dyson 
245df8bae1dSRodney W. Grimes 	object->ref_count++;
24647221757SJohn Dyson 	if (object->type == OBJT_VNODE) {
24747221757SJohn Dyson 		while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curproc)) {
24895461b45SJohn Dyson #if !defined(MAX_PERF)
24947221757SJohn Dyson 			printf("vm_object_reference: delay in getting object\n");
25095461b45SJohn Dyson #endif
25147221757SJohn Dyson 		}
25247221757SJohn Dyson 	}
25395e5e988SJohn Dyson }
25495e5e988SJohn Dyson 
255bf27292bSJohn Dyson void
25695e5e988SJohn Dyson vm_object_vndeallocate(object)
25795e5e988SJohn Dyson 	vm_object_t object;
25895e5e988SJohn Dyson {
25995e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
26095e5e988SJohn Dyson #if defined(DIAGNOSTIC)
26195e5e988SJohn Dyson 	if (object->type != OBJT_VNODE)
26295e5e988SJohn Dyson 		panic("vm_object_vndeallocate: not a vnode object");
26395e5e988SJohn Dyson 	if (vp == NULL)
26495e5e988SJohn Dyson 		panic("vm_object_vndeallocate: missing vp");
26595e5e988SJohn Dyson 	if (object->ref_count == 0) {
26695e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
26795e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
26895e5e988SJohn Dyson 	}
26995e5e988SJohn Dyson #endif
27095e5e988SJohn Dyson 
27195e5e988SJohn Dyson 	object->ref_count--;
27247221757SJohn Dyson 	if (object->ref_count == 0) {
273bf27292bSJohn Dyson 		vp->v_flag &= ~VTEXT;
2742d8acc0fSJohn Dyson 		object->flags &= ~OBJ_OPT;
2752be70f79SJohn Dyson 	}
27647221757SJohn Dyson 	vrele(vp);
277df8bae1dSRodney W. Grimes }
278df8bae1dSRodney W. Grimes 
279df8bae1dSRodney W. Grimes /*
280df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
281df8bae1dSRodney W. Grimes  *
282df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
283df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
284df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
285df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
286df8bae1dSRodney W. Grimes  *	may be relinquished.
287df8bae1dSRodney W. Grimes  *
288df8bae1dSRodney W. Grimes  *	No object may be locked.
289df8bae1dSRodney W. Grimes  */
29026f9a767SRodney W. Grimes void
29126f9a767SRodney W. Grimes vm_object_deallocate(object)
29226f9a767SRodney W. Grimes 	vm_object_t object;
293df8bae1dSRodney W. Grimes {
29495e5e988SJohn Dyson 	int s;
295df8bae1dSRodney W. Grimes 	vm_object_t temp;
296df8bae1dSRodney W. Grimes 
297df8bae1dSRodney W. Grimes 	while (object != NULL) {
298df8bae1dSRodney W. Grimes 
29995e5e988SJohn Dyson 		if (object->type == OBJT_VNODE) {
30095e5e988SJohn Dyson 			vm_object_vndeallocate(object);
30195e5e988SJohn Dyson 			return;
30295e5e988SJohn Dyson 		}
30395e5e988SJohn Dyson 
3042be70f79SJohn Dyson 		if (object->ref_count == 0) {
30547221757SJohn Dyson 			panic("vm_object_deallocate: object deallocated too many times: %d", object->type);
3062be70f79SJohn Dyson 		} else if (object->ref_count > 2) {
3072be70f79SJohn Dyson 			object->ref_count--;
3082be70f79SJohn Dyson 			return;
3092be70f79SJohn Dyson 		}
3102be70f79SJohn Dyson 
3112be70f79SJohn Dyson 		/*
3122be70f79SJohn Dyson 		 * Here on ref_count of one or two, which are special cases for
3132be70f79SJohn Dyson 		 * objects.
3142be70f79SJohn Dyson 		 */
31595e5e988SJohn Dyson 		if ((object->ref_count == 2) && (object->shadow_count == 1)) {
3162be70f79SJohn Dyson 
317be6d5bfaSDavid Greenman 			object->ref_count--;
3182be70f79SJohn Dyson 			if ((object->handle == NULL) &&
31924a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
32024a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
321a1f6d91cSDavid Greenman 				vm_object_t robject;
32295e5e988SJohn Dyson 
323b18bfc3dSJohn Dyson 				robject = TAILQ_FIRST(&object->shadow_head);
32495e5e988SJohn Dyson #if defined(DIAGNOSTIC)
32595e5e988SJohn Dyson 				if (robject == NULL)
32695e5e988SJohn Dyson 					panic("vm_object_deallocate: ref_count: %d,"
32795e5e988SJohn Dyson 						  " shadow_count: %d",
32895e5e988SJohn Dyson 						  object->ref_count, object->shadow_count);
32995e5e988SJohn Dyson #endif
33095e5e988SJohn Dyson 				if ((robject->handle == NULL) &&
33124a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
33224a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
333a1f6d91cSDavid Greenman 
33495e5e988SJohn Dyson 					robject->ref_count++;
33595e5e988SJohn Dyson 
33695e5e988SJohn Dyson 			retry:
337ffc82b0aSJohn Dyson 					if (robject->paging_in_progress ||
338ffc82b0aSJohn Dyson 							object->paging_in_progress) {
33966095752SJohn Dyson 						vm_object_pip_sleep(robject, "objde1");
340ffc82b0aSJohn Dyson 						if (robject->paging_in_progress &&
341ffc82b0aSJohn Dyson 							robject->type == OBJT_SWAP) {
34266095752SJohn Dyson 							swap_pager_sync();
34395e5e988SJohn Dyson 							goto retry;
3440d94caffSDavid Greenman 						}
345a1f6d91cSDavid Greenman 
34666095752SJohn Dyson 						vm_object_pip_sleep(object, "objde2");
347ffc82b0aSJohn Dyson 						if (object->paging_in_progress &&
348ffc82b0aSJohn Dyson 							object->type == OBJT_SWAP) {
34966095752SJohn Dyson 							swap_pager_sync();
35066095752SJohn Dyson 						}
35195e5e988SJohn Dyson 						goto retry;
352a1f6d91cSDavid Greenman 					}
353a1f6d91cSDavid Greenman 
35495e5e988SJohn Dyson 					if( robject->ref_count == 1) {
35595e5e988SJohn Dyson 						robject->ref_count--;
356ba8da839SDavid Greenman 						object = robject;
35795e5e988SJohn Dyson 						goto doterm;
35895e5e988SJohn Dyson 					}
35995e5e988SJohn Dyson 
36095e5e988SJohn Dyson 					object = robject;
36195e5e988SJohn Dyson 					vm_object_collapse(object);
362ba8da839SDavid Greenman 					continue;
363a1f6d91cSDavid Greenman 				}
36495e5e988SJohn Dyson 			}
36595e5e988SJohn Dyson 
366a1f6d91cSDavid Greenman 			return;
367df8bae1dSRodney W. Grimes 
36895e5e988SJohn Dyson 		} else {
3691efb74fbSJohn Dyson 			object->ref_count--;
37095e5e988SJohn Dyson 			if (object->ref_count != 0)
37195e5e988SJohn Dyson 				return;
37295e5e988SJohn Dyson 		}
37395e5e988SJohn Dyson 
37495e5e988SJohn Dyson doterm:
3751efb74fbSJohn Dyson 
37624a1cce3SDavid Greenman 		temp = object->backing_object;
377de5f6a77SJohn Dyson 		if (temp) {
37824a1cce3SDavid Greenman 			TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
37995e5e988SJohn Dyson 			temp->shadow_count--;
3802d8acc0fSJohn Dyson 			if (temp->ref_count == 0)
381bf27292bSJohn Dyson 				temp->flags &= ~OBJ_OPT;
382eaf13dd7SJohn Dyson 			temp->generation++;
38395461b45SJohn Dyson 			object->backing_object = NULL;
384de5f6a77SJohn Dyson 		}
385df8bae1dSRodney W. Grimes 		vm_object_terminate(object);
386df8bae1dSRodney W. Grimes 		/* unlocks and deallocates object */
387df8bae1dSRodney W. Grimes 		object = temp;
388df8bae1dSRodney W. Grimes 	}
389df8bae1dSRodney W. Grimes }
390df8bae1dSRodney W. Grimes 
391df8bae1dSRodney W. Grimes /*
392df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
393df8bae1dSRodney W. Grimes  *	up all previously used resources.
394df8bae1dSRodney W. Grimes  *
395df8bae1dSRodney W. Grimes  *	The object must be locked.
396df8bae1dSRodney W. Grimes  */
39795e5e988SJohn Dyson void
39826f9a767SRodney W. Grimes vm_object_terminate(object)
399df8bae1dSRodney W. Grimes 	register vm_object_t object;
400df8bae1dSRodney W. Grimes {
4013af76890SPoul-Henning Kamp 	register vm_page_t p;
40226f9a767SRodney W. Grimes 	int s;
403df8bae1dSRodney W. Grimes 
40495e5e988SJohn Dyson 	/*
40595e5e988SJohn Dyson 	 * Make sure no one uses us.
40695e5e988SJohn Dyson 	 */
40795e5e988SJohn Dyson 	object->flags |= OBJ_DEAD;
4083c631446SJohn Dyson 
409df8bae1dSRodney W. Grimes 	/*
410f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
411df8bae1dSRodney W. Grimes 	 */
41266095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
413df8bae1dSRodney W. Grimes 
41495e5e988SJohn Dyson #if defined(DIAGNOSTIC)
41526f9a767SRodney W. Grimes 	if (object->paging_in_progress != 0)
41695461b45SJohn Dyson 		panic("vm_object_terminate: pageout in progress");
41795e5e988SJohn Dyson #endif
41826f9a767SRodney W. Grimes 
41926f9a767SRodney W. Grimes 	/*
4200d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
4210d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
42226f9a767SRodney W. Grimes 	 */
42324a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
42495e5e988SJohn Dyson 		struct vnode *vp;
42595e5e988SJohn Dyson 
42695e5e988SJohn Dyson 		/*
42795e5e988SJohn Dyson 		 * Freeze optimized copies.
42895e5e988SJohn Dyson 		 */
42995e5e988SJohn Dyson 		vm_freeze_copyopts(object, 0, object->size);
43095e5e988SJohn Dyson 
43195e5e988SJohn Dyson 		/*
43295e5e988SJohn Dyson 		 * Clean pages and flush buffers.
43395e5e988SJohn Dyson 		 */
4348f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
43595e5e988SJohn Dyson 
43695e5e988SJohn Dyson 		vp = (struct vnode *) object->handle;
437f6b04d2bSDavid Greenman 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
43895e5e988SJohn Dyson 
439bef608bdSJohn Dyson 		/*
440bef608bdSJohn Dyson 		 * Let the pager know object is dead.
441bef608bdSJohn Dyson 		 */
442bef608bdSJohn Dyson 		vm_pager_deallocate(object);
443bef608bdSJohn Dyson 
444bef608bdSJohn Dyson 	}
445bef608bdSJohn Dyson 
446bef608bdSJohn Dyson 	if ((object->type != OBJT_VNODE) && (object->ref_count == 0)) {
447996c772fSJohn Dyson 
4480d94caffSDavid Greenman 		/*
4490d94caffSDavid Greenman 		 * Now free the pages. For internal objects, this also removes them
4500d94caffSDavid Greenman 		 * from paging queues.
451df8bae1dSRodney W. Grimes 		 */
452b18bfc3dSJohn Dyson 		while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
45395461b45SJohn Dyson #if !defined(MAX_PERF)
454a2f4a846SJohn Dyson 			if (p->busy || (p->flags & PG_BUSY))
455be6d5bfaSDavid Greenman 				printf("vm_object_terminate: freeing busy page\n");
45695461b45SJohn Dyson #endif
457eaf13dd7SJohn Dyson 			p->flags |= PG_BUSY;
458df8bae1dSRodney W. Grimes 			vm_page_free(p);
459df8bae1dSRodney W. Grimes 			cnt.v_pfree++;
460df8bae1dSRodney W. Grimes 		}
461df8bae1dSRodney W. Grimes 		/*
462df8bae1dSRodney W. Grimes 		 * Let the pager know object is dead.
463df8bae1dSRodney W. Grimes 		 */
46424a1cce3SDavid Greenman 		vm_pager_deallocate(object);
465bef608bdSJohn Dyson 
46695461b45SJohn Dyson 	}
467df8bae1dSRodney W. Grimes 
468bef608bdSJohn Dyson 	if ((object->ref_count == 0) && (object->resident_page_count == 0))
4692d8acc0fSJohn Dyson 		vm_object_dispose(object);
4702d8acc0fSJohn Dyson }
4712d8acc0fSJohn Dyson 
4722d8acc0fSJohn Dyson /*
4732d8acc0fSJohn Dyson  * vm_object_dispose
4742d8acc0fSJohn Dyson  *
4752d8acc0fSJohn Dyson  * Dispose the object.
4762d8acc0fSJohn Dyson  */
47795461b45SJohn Dyson static void
4782d8acc0fSJohn Dyson vm_object_dispose(object)
4792d8acc0fSJohn Dyson 	vm_object_t object;
4802d8acc0fSJohn Dyson {
481996c772fSJohn Dyson 		simple_lock(&vm_object_list_lock);
482df8bae1dSRodney W. Grimes 		TAILQ_REMOVE(&vm_object_list, object, object_list);
483df8bae1dSRodney W. Grimes 		vm_object_count--;
484996c772fSJohn Dyson 		simple_unlock(&vm_object_list_lock);
485df8bae1dSRodney W. Grimes 		/*
486df8bae1dSRodney W. Grimes    		* Free the space for the object.
487df8bae1dSRodney W. Grimes    		*/
48899448ed1SJohn Dyson 		zfree(obj_zone, object);
48947221757SJohn Dyson 		wakeup(object);
49047221757SJohn Dyson }
491df8bae1dSRodney W. Grimes 
492df8bae1dSRodney W. Grimes /*
493df8bae1dSRodney W. Grimes  *	vm_object_page_clean
494df8bae1dSRodney W. Grimes  *
495df8bae1dSRodney W. Grimes  *	Clean all dirty pages in the specified range of object.
49626f9a767SRodney W. Grimes  *	Leaves page on whatever queue it is currently on.
49726f9a767SRodney W. Grimes  *
49826f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
49926f9a767SRodney W. Grimes  *
50026f9a767SRodney W. Grimes  *	The object must be locked.
50126f9a767SRodney W. Grimes  */
502f6b04d2bSDavid Greenman 
503f6b04d2bSDavid Greenman void
5048f9110f6SJohn Dyson vm_object_page_clean(object, start, end, flags)
505f6b04d2bSDavid Greenman 	vm_object_t object;
506a316d390SJohn Dyson 	vm_pindex_t start;
507a316d390SJohn Dyson 	vm_pindex_t end;
5088f9110f6SJohn Dyson 	int flags;
509f6b04d2bSDavid Greenman {
510bd7e5f99SJohn Dyson 	register vm_page_t p, np, tp;
511f6b04d2bSDavid Greenman 	register vm_offset_t tstart, tend;
512bd7e5f99SJohn Dyson 	vm_pindex_t pi;
513aef922f5SJohn Dyson 	int s;
51424a1cce3SDavid Greenman 	struct vnode *vp;
515aef922f5SJohn Dyson 	int runlen;
516bd7e5f99SJohn Dyson 	int maxf;
517bd7e5f99SJohn Dyson 	int chkb;
518bd7e5f99SJohn Dyson 	int maxb;
519bd7e5f99SJohn Dyson 	int i;
5208f9110f6SJohn Dyson 	int pagerflags;
521bd7e5f99SJohn Dyson 	vm_page_t maf[vm_pageout_page_count];
522bd7e5f99SJohn Dyson 	vm_page_t mab[vm_pageout_page_count];
523aef922f5SJohn Dyson 	vm_page_t ma[vm_pageout_page_count];
5242d8acc0fSJohn Dyson 	int curgeneration;
525996c772fSJohn Dyson 	struct proc *pproc = curproc;	/* XXX */
526f6b04d2bSDavid Greenman 
527aef922f5SJohn Dyson 	if (object->type != OBJT_VNODE ||
528aef922f5SJohn Dyson 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
529f6b04d2bSDavid Greenman 		return;
530f6b04d2bSDavid Greenman 
5318f9110f6SJohn Dyson 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0;
5328f9110f6SJohn Dyson 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
5338f9110f6SJohn Dyson 
53424a1cce3SDavid Greenman 	vp = object->handle;
53524a1cce3SDavid Greenman 
536aef922f5SJohn Dyson 	object->flags |= OBJ_CLEANING;
53724a1cce3SDavid Greenman 
538f6b04d2bSDavid Greenman 	tstart = start;
539f6b04d2bSDavid Greenman 	if (end == 0) {
540f6b04d2bSDavid Greenman 		tend = object->size;
541f6b04d2bSDavid Greenman 	} else {
542f6b04d2bSDavid Greenman 		tend = end;
543f6b04d2bSDavid Greenman 	}
544eaf13dd7SJohn Dyson 
545eaf13dd7SJohn Dyson 	for(p = TAILQ_FIRST(&object->memq); p; p = TAILQ_NEXT(p, listq)) {
546eaf13dd7SJohn Dyson 		p->flags |= PG_CLEANCHK;
547eaf13dd7SJohn Dyson 		vm_page_protect(p, VM_PROT_READ);
548eaf13dd7SJohn Dyson 	}
549eaf13dd7SJohn Dyson 
550a316d390SJohn Dyson 	if ((tstart == 0) && (tend == object->size)) {
551aef922f5SJohn Dyson 		object->flags &= ~(OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
552ec4f9fb0SDavid Greenman 	}
553f6b04d2bSDavid Greenman 
554bd7e5f99SJohn Dyson rescan:
5552d8acc0fSJohn Dyson 	curgeneration = object->generation;
5562d8acc0fSJohn Dyson 
557b18bfc3dSJohn Dyson 	for(p = TAILQ_FIRST(&object->memq); p; p = np) {
558b18bfc3dSJohn Dyson 		np = TAILQ_NEXT(p, listq);
559bd7e5f99SJohn Dyson 
560bd7e5f99SJohn Dyson 		pi = p->pindex;
561bd7e5f99SJohn Dyson 		if (((p->flags & PG_CLEANCHK) == 0) ||
562bd7e5f99SJohn Dyson 			(pi < tstart) || (pi >= tend) ||
5635070c7f8SJohn Dyson 			(p->valid == 0) ||
5645070c7f8SJohn Dyson 			((p->queue - p->pc) == PQ_CACHE)) {
565bd7e5f99SJohn Dyson 			p->flags &= ~PG_CLEANCHK;
566aef922f5SJohn Dyson 			continue;
567f6b04d2bSDavid Greenman 		}
568f6b04d2bSDavid Greenman 
569bd7e5f99SJohn Dyson 		vm_page_test_dirty(p);
570bd7e5f99SJohn Dyson 		if ((p->dirty & p->valid) == 0) {
571bd7e5f99SJohn Dyson 			p->flags &= ~PG_CLEANCHK;
572bd7e5f99SJohn Dyson 			continue;
573bd7e5f99SJohn Dyson 		}
574ec4f9fb0SDavid Greenman 
575b18bfc3dSJohn Dyson 		s = splvm();
5762d8acc0fSJohn Dyson 		while ((p->flags & PG_BUSY) || p->busy) {
577aef922f5SJohn Dyson 			p->flags |= PG_WANTED | PG_REFERENCED;
578aef922f5SJohn Dyson 			tsleep(p, PVM, "vpcwai", 0);
5792d8acc0fSJohn Dyson 			if (object->generation != curgeneration) {
580f6b04d2bSDavid Greenman 				splx(s);
581bd7e5f99SJohn Dyson 				goto rescan;
582f6b04d2bSDavid Greenman 			}
5832d8acc0fSJohn Dyson 		}
584f6b04d2bSDavid Greenman 
585bd7e5f99SJohn Dyson 		maxf = 0;
586bd7e5f99SJohn Dyson 		for(i=1;i<vm_pageout_page_count;i++) {
587bd7e5f99SJohn Dyson 			if (tp = vm_page_lookup(object, pi + i)) {
588bd7e5f99SJohn Dyson 				if ((tp->flags & PG_BUSY) ||
589ffc82b0aSJohn Dyson 					(tp->flags & PG_CLEANCHK) == 0 ||
590ffc82b0aSJohn Dyson 					(tp->busy != 0))
591bd7e5f99SJohn Dyson 					break;
5925070c7f8SJohn Dyson 				if((tp->queue - tp->pc) == PQ_CACHE) {
5933077a9c2SJohn Dyson 					tp->flags &= ~PG_CLEANCHK;
5943077a9c2SJohn Dyson 					break;
5953077a9c2SJohn Dyson 				}
596bd7e5f99SJohn Dyson 				vm_page_test_dirty(tp);
597bd7e5f99SJohn Dyson 				if ((tp->dirty & tp->valid) == 0) {
598bd7e5f99SJohn Dyson 					tp->flags &= ~PG_CLEANCHK;
599bd7e5f99SJohn Dyson 					break;
600bd7e5f99SJohn Dyson 				}
601bd7e5f99SJohn Dyson 				maf[ i - 1 ] = tp;
602bd7e5f99SJohn Dyson 				maxf++;
603bd7e5f99SJohn Dyson 				continue;
604bd7e5f99SJohn Dyson 			}
605bd7e5f99SJohn Dyson 			break;
606bd7e5f99SJohn Dyson 		}
607aef922f5SJohn Dyson 
608bd7e5f99SJohn Dyson 		maxb = 0;
609bd7e5f99SJohn Dyson 		chkb = vm_pageout_page_count -  maxf;
610bd7e5f99SJohn Dyson 		if (chkb) {
611bd7e5f99SJohn Dyson 			for(i = 1; i < chkb;i++) {
612bd7e5f99SJohn Dyson 				if (tp = vm_page_lookup(object, pi - i)) {
613bd7e5f99SJohn Dyson 					if ((tp->flags & PG_BUSY) ||
614ffc82b0aSJohn Dyson 						(tp->flags & PG_CLEANCHK) == 0 ||
615ffc82b0aSJohn Dyson 						(tp->busy != 0))
616bd7e5f99SJohn Dyson 						break;
6175070c7f8SJohn Dyson 					if((tp->queue - tp->pc) == PQ_CACHE) {
6183077a9c2SJohn Dyson 						tp->flags &= ~PG_CLEANCHK;
6193077a9c2SJohn Dyson 						break;
6203077a9c2SJohn Dyson 					}
621bd7e5f99SJohn Dyson 					vm_page_test_dirty(tp);
622bd7e5f99SJohn Dyson 					if ((tp->dirty & tp->valid) == 0) {
623bd7e5f99SJohn Dyson 						tp->flags &= ~PG_CLEANCHK;
624bd7e5f99SJohn Dyson 						break;
625bd7e5f99SJohn Dyson 					}
626bd7e5f99SJohn Dyson 					mab[ i - 1 ] = tp;
627bd7e5f99SJohn Dyson 					maxb++;
628bd7e5f99SJohn Dyson 					continue;
629bd7e5f99SJohn Dyson 				}
630bd7e5f99SJohn Dyson 				break;
631bd7e5f99SJohn Dyson 			}
632bd7e5f99SJohn Dyson 		}
633bd7e5f99SJohn Dyson 
634bd7e5f99SJohn Dyson 		for(i=0;i<maxb;i++) {
635bd7e5f99SJohn Dyson 			int index = (maxb - i) - 1;
636bd7e5f99SJohn Dyson 			ma[index] = mab[i];
637bd7e5f99SJohn Dyson 			ma[index]->flags &= ~PG_CLEANCHK;
638bd7e5f99SJohn Dyson 		}
639bd7e5f99SJohn Dyson 		p->flags &= ~PG_CLEANCHK;
640bd7e5f99SJohn Dyson 		ma[maxb] = p;
641bd7e5f99SJohn Dyson 		for(i=0;i<maxf;i++) {
642bd7e5f99SJohn Dyson 			int index = (maxb + i) + 1;
643bd7e5f99SJohn Dyson 			ma[index] = maf[i];
644bd7e5f99SJohn Dyson 			ma[index]->flags &= ~PG_CLEANCHK;
645f6b04d2bSDavid Greenman 		}
646bd7e5f99SJohn Dyson 		runlen = maxb + maxf + 1;
647f35329acSJohn Dyson 		splx(s);
6488f9110f6SJohn Dyson 		vm_pageout_flush(ma, runlen, pagerflags);
6492d8acc0fSJohn Dyson 		if (object->generation != curgeneration)
650bd7e5f99SJohn Dyson 			goto rescan;
651f6b04d2bSDavid Greenman 	}
652aef922f5SJohn Dyson 
6538f9110f6SJohn Dyson 	VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?1:0, curproc);
654aef922f5SJohn Dyson 
655aef922f5SJohn Dyson 	object->flags &= ~OBJ_CLEANING;
656f5cf85d4SDavid Greenman 	return;
65726f9a767SRodney W. Grimes }
658df8bae1dSRodney W. Grimes 
659f708ef1bSPoul-Henning Kamp #ifdef not_used
660f708ef1bSPoul-Henning Kamp /* XXX I cannot tell if this should be an exported symbol */
661df8bae1dSRodney W. Grimes /*
662df8bae1dSRodney W. Grimes  *	vm_object_deactivate_pages
663df8bae1dSRodney W. Grimes  *
664df8bae1dSRodney W. Grimes  *	Deactivate all pages in the specified object.  (Keep its pages
665df8bae1dSRodney W. Grimes  *	in memory even though it is no longer referenced.)
666df8bae1dSRodney W. Grimes  *
667df8bae1dSRodney W. Grimes  *	The object must be locked.
668df8bae1dSRodney W. Grimes  */
669f708ef1bSPoul-Henning Kamp static void
670df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object)
671df8bae1dSRodney W. Grimes 	register vm_object_t object;
672df8bae1dSRodney W. Grimes {
673df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
674df8bae1dSRodney W. Grimes 
675b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
676b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
677df8bae1dSRodney W. Grimes 		vm_page_deactivate(p);
678df8bae1dSRodney W. Grimes 	}
679df8bae1dSRodney W. Grimes }
680f708ef1bSPoul-Henning Kamp #endif
681df8bae1dSRodney W. Grimes 
682df8bae1dSRodney W. Grimes /*
683df8bae1dSRodney W. Grimes  *	vm_object_pmap_copy:
684df8bae1dSRodney W. Grimes  *
685df8bae1dSRodney W. Grimes  *	Makes all physical pages in the specified
686df8bae1dSRodney W. Grimes  *	object range copy-on-write.  No writeable
687df8bae1dSRodney W. Grimes  *	references to these pages should remain.
688df8bae1dSRodney W. Grimes  *
689df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
690df8bae1dSRodney W. Grimes  */
6910d94caffSDavid Greenman void
6920d94caffSDavid Greenman vm_object_pmap_copy(object, start, end)
693df8bae1dSRodney W. Grimes 	register vm_object_t object;
694a316d390SJohn Dyson 	register vm_pindex_t start;
695a316d390SJohn Dyson 	register vm_pindex_t end;
696df8bae1dSRodney W. Grimes {
697df8bae1dSRodney W. Grimes 	register vm_page_t p;
698df8bae1dSRodney W. Grimes 
699aef922f5SJohn Dyson 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
700df8bae1dSRodney W. Grimes 		return;
701df8bae1dSRodney W. Grimes 
702b5b40fa6SJohn Dyson 	for (p = TAILQ_FIRST(&object->memq);
703b5b40fa6SJohn Dyson 		p != NULL;
704b5b40fa6SJohn Dyson 		p = TAILQ_NEXT(p, listq)) {
705f919ebdeSDavid Greenman 		vm_page_protect(p, VM_PROT_READ);
706df8bae1dSRodney W. Grimes 	}
707aef922f5SJohn Dyson 
708aef922f5SJohn Dyson 	object->flags &= ~OBJ_WRITEABLE;
709df8bae1dSRodney W. Grimes }
710df8bae1dSRodney W. Grimes 
711df8bae1dSRodney W. Grimes /*
7121efb74fbSJohn Dyson  * Same as vm_object_pmap_copy_1, except range checking really
7131efb74fbSJohn Dyson  * works, and is meant for small sections of an object.
7141efb74fbSJohn Dyson  */
7151efb74fbSJohn Dyson void
7161efb74fbSJohn Dyson vm_object_pmap_copy_1(object, start, end)
7171efb74fbSJohn Dyson 	register vm_object_t object;
7181efb74fbSJohn Dyson 	register vm_pindex_t start;
7191efb74fbSJohn Dyson 	register vm_pindex_t end;
7201efb74fbSJohn Dyson {
7211efb74fbSJohn Dyson 	vm_pindex_t idx;
7221efb74fbSJohn Dyson 	register vm_page_t p;
7231efb74fbSJohn Dyson 
7241efb74fbSJohn Dyson 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
7251efb74fbSJohn Dyson 		return;
7261efb74fbSJohn Dyson 
7271efb74fbSJohn Dyson 	for (idx = start; idx < end; idx++) {
7281efb74fbSJohn Dyson 		p = vm_page_lookup(object, idx);
7291efb74fbSJohn Dyson 		if (p == NULL)
7301efb74fbSJohn Dyson 			continue;
7311efb74fbSJohn Dyson 		vm_page_protect(p, VM_PROT_READ);
7321efb74fbSJohn Dyson 	}
7331efb74fbSJohn Dyson }
7341efb74fbSJohn Dyson 
7351efb74fbSJohn Dyson /*
736df8bae1dSRodney W. Grimes  *	vm_object_pmap_remove:
737df8bae1dSRodney W. Grimes  *
738df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
739df8bae1dSRodney W. Grimes  *	object range from all physical maps.
740df8bae1dSRodney W. Grimes  *
741df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
742df8bae1dSRodney W. Grimes  */
74326f9a767SRodney W. Grimes void
74426f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end)
745df8bae1dSRodney W. Grimes 	register vm_object_t object;
746a316d390SJohn Dyson 	register vm_pindex_t start;
747a316d390SJohn Dyson 	register vm_pindex_t end;
748df8bae1dSRodney W. Grimes {
749df8bae1dSRodney W. Grimes 	register vm_page_t p;
750df8bae1dSRodney W. Grimes 	if (object == NULL)
751df8bae1dSRodney W. Grimes 		return;
752b5b40fa6SJohn Dyson 	for (p = TAILQ_FIRST(&object->memq);
753b5b40fa6SJohn Dyson 		p != NULL;
754b5b40fa6SJohn Dyson 		p = TAILQ_NEXT(p, listq)) {
755bd7e5f99SJohn Dyson 		if (p->pindex >= start && p->pindex < end)
756f919ebdeSDavid Greenman 			vm_page_protect(p, VM_PROT_NONE);
75726f9a767SRodney W. Grimes 	}
7586e20a165SJohn Dyson 	if ((start == 0) && (object->size == end))
7596e20a165SJohn Dyson 		object->flags &= ~OBJ_WRITEABLE;
76026f9a767SRodney W. Grimes }
761df8bae1dSRodney W. Grimes 
762df8bae1dSRodney W. Grimes /*
763867a482dSJohn Dyson  *	vm_object_madvise:
764867a482dSJohn Dyson  *
765867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
766867a482dSJohn Dyson  */
767867a482dSJohn Dyson void
768867a482dSJohn Dyson vm_object_madvise(object, pindex, count, advise)
769867a482dSJohn Dyson 	vm_object_t object;
770867a482dSJohn Dyson 	vm_pindex_t pindex;
771867a482dSJohn Dyson 	int count;
772867a482dSJohn Dyson 	int advise;
773867a482dSJohn Dyson {
7746e20a165SJohn Dyson 	int s;
7756e20a165SJohn Dyson 	vm_pindex_t end, tpindex;
7766e20a165SJohn Dyson 	vm_object_t tobject;
777867a482dSJohn Dyson 	vm_page_t m;
778867a482dSJohn Dyson 
779867a482dSJohn Dyson 	if (object == NULL)
780867a482dSJohn Dyson 		return;
781867a482dSJohn Dyson 
782867a482dSJohn Dyson 	end = pindex + count;
783867a482dSJohn Dyson 
784867a482dSJohn Dyson 	for (; pindex < end; pindex += 1) {
7856e20a165SJohn Dyson 
7866e20a165SJohn Dyson relookup:
7876e20a165SJohn Dyson 		tobject = object;
7886e20a165SJohn Dyson 		tpindex = pindex;
7896e20a165SJohn Dyson shadowlookup:
7906e20a165SJohn Dyson 		m = vm_page_lookup(tobject, tpindex);
7916e20a165SJohn Dyson 		if (m == NULL) {
7926e20a165SJohn Dyson 			if (tobject->type != OBJT_DEFAULT) {
7936e20a165SJohn Dyson 				continue;
7946e20a165SJohn Dyson 			}
7956e20a165SJohn Dyson 
7966e20a165SJohn Dyson 			tobject = tobject->backing_object;
7976e20a165SJohn Dyson 			if ((tobject == NULL) || (tobject->ref_count != 1)) {
7986e20a165SJohn Dyson 				continue;
7996e20a165SJohn Dyson 			}
8006e20a165SJohn Dyson 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
8016e20a165SJohn Dyson 			goto shadowlookup;
8026e20a165SJohn Dyson 		}
803867a482dSJohn Dyson 
804867a482dSJohn Dyson 		/*
805867a482dSJohn Dyson 		 * If the page is busy or not in a normal active state,
806867a482dSJohn Dyson 		 * we skip it.  Things can break if we mess with pages
807867a482dSJohn Dyson 		 * in any of the below states.
808867a482dSJohn Dyson 		 */
8096e20a165SJohn Dyson 		if (m->hold_count || m->wire_count ||
8106e20a165SJohn Dyson 			m->valid != VM_PAGE_BITS_ALL) {
811867a482dSJohn Dyson 			continue;
8126e20a165SJohn Dyson 		}
8136e20a165SJohn Dyson 
814ffc82b0aSJohn Dyson  		if (vm_page_sleep(m, "madvpo", &m->busy))
8156e20a165SJohn Dyson   			goto relookup;
816867a482dSJohn Dyson 
817867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
818867a482dSJohn Dyson 			vm_page_activate(m);
8196e20a165SJohn Dyson 		} else if (advise == MADV_DONTNEED) {
820867a482dSJohn Dyson 			vm_page_deactivate(m);
8210a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
8226e20a165SJohn Dyson 			pmap_clear_modify(VM_PAGE_TO_PHYS(m));
8236e20a165SJohn Dyson 			m->dirty = 0;
8240a47b48bSJohn Dyson 			/*
8256e20a165SJohn Dyson 			 * Force a demand zero if attempt to read from swap.
8266e20a165SJohn Dyson 			 * We currently don't handle vnode files correctly,
8276e20a165SJohn Dyson 			 * and will reread stale contents unnecessarily.
8280a47b48bSJohn Dyson 			 */
8290a47b48bSJohn Dyson 			if (object->type == OBJT_SWAP)
8306e20a165SJohn Dyson 				swap_pager_dmzspace(tobject, m->pindex, 1);
831867a482dSJohn Dyson 		}
832867a482dSJohn Dyson 	}
833867a482dSJohn Dyson }
834867a482dSJohn Dyson 
835867a482dSJohn Dyson /*
836df8bae1dSRodney W. Grimes  *	vm_object_shadow:
837df8bae1dSRodney W. Grimes  *
838df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
839df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
840df8bae1dSRodney W. Grimes  *	object reference is deallocated.
841df8bae1dSRodney W. Grimes  *
842df8bae1dSRodney W. Grimes  *	The new object and offset into that object
843df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
844df8bae1dSRodney W. Grimes  */
845df8bae1dSRodney W. Grimes 
84626f9a767SRodney W. Grimes void
84726f9a767SRodney W. Grimes vm_object_shadow(object, offset, length)
848df8bae1dSRodney W. Grimes 	vm_object_t *object;	/* IN/OUT */
849a316d390SJohn Dyson 	vm_ooffset_t *offset;	/* IN/OUT */
850df8bae1dSRodney W. Grimes 	vm_size_t length;
851df8bae1dSRodney W. Grimes {
852df8bae1dSRodney W. Grimes 	register vm_object_t source;
853df8bae1dSRodney W. Grimes 	register vm_object_t result;
854df8bae1dSRodney W. Grimes 
855df8bae1dSRodney W. Grimes 	source = *object;
856df8bae1dSRodney W. Grimes 
857df8bae1dSRodney W. Grimes 	/*
858df8bae1dSRodney W. Grimes 	 * Allocate a new object with the given length
859df8bae1dSRodney W. Grimes 	 */
860df8bae1dSRodney W. Grimes 
86124a1cce3SDavid Greenman 	if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
862df8bae1dSRodney W. Grimes 		panic("vm_object_shadow: no object for shadowing");
863df8bae1dSRodney W. Grimes 
864df8bae1dSRodney W. Grimes 	/*
8650d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
8660d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
8670d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
8680d94caffSDavid Greenman 	 * of reference count.
869df8bae1dSRodney W. Grimes 	 */
87024a1cce3SDavid Greenman 	result->backing_object = source;
871de5f6a77SJohn Dyson 	if (source) {
872de5f6a77SJohn Dyson 		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
873eaf13dd7SJohn Dyson 		source->shadow_count++;
874eaf13dd7SJohn Dyson 		source->generation++;
875de5f6a77SJohn Dyson 	}
876df8bae1dSRodney W. Grimes 
877df8bae1dSRodney W. Grimes 	/*
8780d94caffSDavid Greenman 	 * Store the offset into the source object, and fix up the offset into
8790d94caffSDavid Greenman 	 * the new object.
880df8bae1dSRodney W. Grimes 	 */
881df8bae1dSRodney W. Grimes 
88224a1cce3SDavid Greenman 	result->backing_object_offset = *offset;
883df8bae1dSRodney W. Grimes 
884df8bae1dSRodney W. Grimes 	/*
885df8bae1dSRodney W. Grimes 	 * Return the new things
886df8bae1dSRodney W. Grimes 	 */
887df8bae1dSRodney W. Grimes 
888df8bae1dSRodney W. Grimes 	*offset = 0;
889df8bae1dSRodney W. Grimes 	*object = result;
890df8bae1dSRodney W. Grimes }
891df8bae1dSRodney W. Grimes 
892df8bae1dSRodney W. Grimes 
893df8bae1dSRodney W. Grimes /*
8942fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
8952fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
8962fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
8972fe6e4d7SDavid Greenman  */
8982fe6e4d7SDavid Greenman static void
8992fe6e4d7SDavid Greenman vm_object_qcollapse(object)
9002fe6e4d7SDavid Greenman 	register vm_object_t object;
9012fe6e4d7SDavid Greenman {
9022fe6e4d7SDavid Greenman 	register vm_object_t backing_object;
903a316d390SJohn Dyson 	register vm_pindex_t backing_offset_index, paging_offset_index;
904a316d390SJohn Dyson 	vm_pindex_t backing_object_paging_offset_index;
905a316d390SJohn Dyson 	vm_pindex_t new_pindex;
9062fe6e4d7SDavid Greenman 	register vm_page_t p, pp;
9072fe6e4d7SDavid Greenman 	register vm_size_t size;
9082fe6e4d7SDavid Greenman 
90924a1cce3SDavid Greenman 	backing_object = object->backing_object;
9102fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
9112fe6e4d7SDavid Greenman 		return;
9122fe6e4d7SDavid Greenman 
913010cf3b9SDavid Greenman 	backing_object->ref_count += 2;
914010cf3b9SDavid Greenman 
915a316d390SJohn Dyson 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
916a316d390SJohn Dyson 	backing_object_paging_offset_index = OFF_TO_IDX(backing_object->paging_offset);
917a316d390SJohn Dyson 	paging_offset_index = OFF_TO_IDX(object->paging_offset);
9182fe6e4d7SDavid Greenman 	size = object->size;
919b18bfc3dSJohn Dyson 	p = TAILQ_FIRST(&backing_object->memq);
9202fe6e4d7SDavid Greenman 	while (p) {
9212fe6e4d7SDavid Greenman 		vm_page_t next;
9220d94caffSDavid Greenman 
923b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
924bd7e5f99SJohn Dyson 		if ((p->flags & (PG_BUSY | PG_FICTITIOUS)) ||
9255070c7f8SJohn Dyson 		    !p->valid || p->hold_count || p->wire_count || p->busy) {
9262fe6e4d7SDavid Greenman 			p = next;
9272fe6e4d7SDavid Greenman 			continue;
9282fe6e4d7SDavid Greenman 		}
929eaf13dd7SJohn Dyson 		p->flags |= PG_BUSY;
930eaf13dd7SJohn Dyson 
931a316d390SJohn Dyson 		new_pindex = p->pindex - backing_offset_index;
932a316d390SJohn Dyson 		if (p->pindex < backing_offset_index ||
933a316d390SJohn Dyson 		    new_pindex >= size) {
93424a1cce3SDavid Greenman 			if (backing_object->type == OBJT_SWAP)
93524a1cce3SDavid Greenman 				swap_pager_freespace(backing_object,
936a316d390SJohn Dyson 				    backing_object_paging_offset_index+p->pindex,
937a316d390SJohn Dyson 				    1);
938b18bfc3dSJohn Dyson 			vm_page_protect(p, VM_PROT_NONE);
9392fe6e4d7SDavid Greenman 			vm_page_free(p);
9402fe6e4d7SDavid Greenman 		} else {
941a316d390SJohn Dyson 			pp = vm_page_lookup(object, new_pindex);
942eaf13dd7SJohn Dyson 			if (pp != NULL ||
943eaf13dd7SJohn Dyson 				(object->type == OBJT_SWAP && vm_pager_has_page(object,
944a316d390SJohn Dyson 				    paging_offset_index + new_pindex, NULL, NULL))) {
94524a1cce3SDavid Greenman 				if (backing_object->type == OBJT_SWAP)
94624a1cce3SDavid Greenman 					swap_pager_freespace(backing_object,
947a316d390SJohn Dyson 					    backing_object_paging_offset_index + p->pindex, 1);
948b18bfc3dSJohn Dyson 				vm_page_protect(p, VM_PROT_NONE);
9492fe6e4d7SDavid Greenman 				vm_page_free(p);
9502fe6e4d7SDavid Greenman 			} else {
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);
954eaf13dd7SJohn Dyson 
955edd97f3aSJohn Dyson 				if ((p->queue - p->pc) == PQ_CACHE)
956edd97f3aSJohn Dyson 					vm_page_deactivate(p);
957edd97f3aSJohn Dyson 				else
9586e20a165SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
959edd97f3aSJohn Dyson 
960edd97f3aSJohn Dyson 				vm_page_rename(p, object, new_pindex);
9619b4814bbSDavid Greenman 				p->dirty = VM_PAGE_BITS_ALL;
9622fe6e4d7SDavid Greenman 			}
9632fe6e4d7SDavid Greenman 		}
9642fe6e4d7SDavid Greenman 		p = next;
9652fe6e4d7SDavid Greenman 	}
966010cf3b9SDavid Greenman 	backing_object->ref_count -= 2;
9672fe6e4d7SDavid Greenman }
9682fe6e4d7SDavid Greenman 
969df8bae1dSRodney W. Grimes /*
970df8bae1dSRodney W. Grimes  *	vm_object_collapse:
971df8bae1dSRodney W. Grimes  *
972df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
973df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
974df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
975df8bae1dSRodney W. Grimes  */
97626f9a767SRodney W. Grimes void
97726f9a767SRodney W. Grimes vm_object_collapse(object)
97824a1cce3SDavid Greenman 	vm_object_t object;
979df8bae1dSRodney W. Grimes 
980df8bae1dSRodney W. Grimes {
98124a1cce3SDavid Greenman 	vm_object_t backing_object;
982a316d390SJohn Dyson 	vm_ooffset_t backing_offset;
98324a1cce3SDavid Greenman 	vm_size_t size;
984a316d390SJohn Dyson 	vm_pindex_t new_pindex, backing_offset_index;
98524a1cce3SDavid Greenman 	vm_page_t p, pp;
986df8bae1dSRodney W. Grimes 
987df8bae1dSRodney W. Grimes 	while (TRUE) {
988df8bae1dSRodney W. Grimes 		/*
989df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
990df8bae1dSRodney W. Grimes 		 *
9910d94caffSDavid Greenman 		 * The object exists and no pages in it are currently being paged
9920d94caffSDavid Greenman 		 * out.
993df8bae1dSRodney W. Grimes 		 */
9942fe6e4d7SDavid Greenman 		if (object == NULL)
995df8bae1dSRodney W. Grimes 			return;
996df8bae1dSRodney W. Grimes 
997b9921222SDavid Greenman 		/*
998b9921222SDavid Greenman 		 * Make sure there is a backing object.
999b9921222SDavid Greenman 		 */
100024a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
1001df8bae1dSRodney W. Grimes 			return;
1002df8bae1dSRodney W. Grimes 
1003f919ebdeSDavid Greenman 		/*
1004f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
100524a1cce3SDavid Greenman 		 * not collapsable.
1006f919ebdeSDavid Greenman 		 */
100724a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
100824a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
100924a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1010f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
101124a1cce3SDavid Greenman 		    object->handle != NULL ||
101224a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
101324a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
101424a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
10159b4814bbSDavid Greenman 			return;
101624a1cce3SDavid Greenman 		}
10179b4814bbSDavid Greenman 
1018f919ebdeSDavid Greenman 		if (object->paging_in_progress != 0 ||
1019f919ebdeSDavid Greenman 		    backing_object->paging_in_progress != 0) {
1020b9921222SDavid Greenman 			vm_object_qcollapse(object);
1021df8bae1dSRodney W. Grimes 			return;
1022df8bae1dSRodney W. Grimes 		}
1023f919ebdeSDavid Greenman 
102426f9a767SRodney W. Grimes 		/*
10250d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
10260d94caffSDavid Greenman 		 * the parent is the only reference to it) or (perhaps) remove
10270d94caffSDavid Greenman 		 * the parent's reference to it.
1028df8bae1dSRodney W. Grimes 		 */
1029df8bae1dSRodney W. Grimes 
103024a1cce3SDavid Greenman 		backing_offset = object->backing_object_offset;
1031a316d390SJohn Dyson 		backing_offset_index = OFF_TO_IDX(backing_offset);
1032df8bae1dSRodney W. Grimes 		size = object->size;
1033df8bae1dSRodney W. Grimes 
1034df8bae1dSRodney W. Grimes 		/*
10350d94caffSDavid Greenman 		 * If there is exactly one reference to the backing object, we
10360d94caffSDavid Greenman 		 * can collapse it into the parent.
1037df8bae1dSRodney W. Grimes 		 */
1038df8bae1dSRodney W. Grimes 
1039df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1040df8bae1dSRodney W. Grimes 
1041a1f6d91cSDavid Greenman 			backing_object->flags |= OBJ_DEAD;
1042df8bae1dSRodney W. Grimes 			/*
1043df8bae1dSRodney W. Grimes 			 * We can collapse the backing object.
1044df8bae1dSRodney W. Grimes 			 *
10450d94caffSDavid Greenman 			 * Move all in-memory pages from backing_object to the
10460d94caffSDavid Greenman 			 * parent.  Pages that have been paged out will be
10470d94caffSDavid Greenman 			 * overwritten by any of the parent's pages that
10480d94caffSDavid Greenman 			 * shadow them.
1049df8bae1dSRodney W. Grimes 			 */
1050df8bae1dSRodney W. Grimes 
1051b18bfc3dSJohn Dyson 			while ((p = TAILQ_FIRST(&backing_object->memq)) != 0) {
105226f9a767SRodney W. Grimes 
1053a316d390SJohn Dyson 				new_pindex = p->pindex - backing_offset_index;
1054eaf13dd7SJohn Dyson 				p->flags |= PG_BUSY;
1055df8bae1dSRodney W. Grimes 
1056df8bae1dSRodney W. Grimes 				/*
10570d94caffSDavid Greenman 				 * If the parent has a page here, or if this
10580d94caffSDavid Greenman 				 * page falls outside the parent, dispose of
10590d94caffSDavid Greenman 				 * it.
1060df8bae1dSRodney W. Grimes 				 *
1061df8bae1dSRodney W. Grimes 				 * Otherwise, move it as planned.
1062df8bae1dSRodney W. Grimes 				 */
1063df8bae1dSRodney W. Grimes 
1064a316d390SJohn Dyson 				if (p->pindex < backing_offset_index ||
1065a316d390SJohn Dyson 				    new_pindex >= size) {
1066f919ebdeSDavid Greenman 					vm_page_protect(p, VM_PROT_NONE);
1067df8bae1dSRodney W. Grimes 					vm_page_free(p);
1068df8bae1dSRodney W. Grimes 				} else {
1069a316d390SJohn Dyson 					pp = vm_page_lookup(object, new_pindex);
107024a1cce3SDavid Greenman 					if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object,
1071a316d390SJohn Dyson 					    OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL))) {
1072f919ebdeSDavid Greenman 						vm_page_protect(p, VM_PROT_NONE);
1073df8bae1dSRodney W. Grimes 						vm_page_free(p);
107426f9a767SRodney W. Grimes 					} else {
1075edd97f3aSJohn Dyson 						if ((p->queue - p->pc) == PQ_CACHE)
1076edd97f3aSJohn Dyson 							vm_page_deactivate(p);
1077edd97f3aSJohn Dyson 						else
10786e20a165SJohn Dyson 							vm_page_protect(p, VM_PROT_NONE);
1079a316d390SJohn Dyson 						vm_page_rename(p, object, new_pindex);
10806e20a165SJohn Dyson 						p->dirty = VM_PAGE_BITS_ALL;
1081df8bae1dSRodney W. Grimes 					}
1082df8bae1dSRodney W. Grimes 				}
1083df8bae1dSRodney W. Grimes 			}
1084df8bae1dSRodney W. Grimes 
1085df8bae1dSRodney W. Grimes 			/*
1086df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1087df8bae1dSRodney W. Grimes 			 */
1088df8bae1dSRodney W. Grimes 
108924a1cce3SDavid Greenman 			if (backing_object->type == OBJT_SWAP) {
109026f9a767SRodney W. Grimes 				backing_object->paging_in_progress++;
109124a1cce3SDavid Greenman 				if (object->type == OBJT_SWAP) {
109226f9a767SRodney W. Grimes 					object->paging_in_progress++;
109326f9a767SRodney W. Grimes 					/*
109426f9a767SRodney W. Grimes 					 * copy shadow object pages into ours
10950d94caffSDavid Greenman 					 * and destroy unneeded pages in
10960d94caffSDavid Greenman 					 * shadow object.
109726f9a767SRodney W. Grimes 					 */
109826f9a767SRodney W. Grimes 					swap_pager_copy(
1099a316d390SJohn Dyson 					    backing_object,
1100a316d390SJohn Dyson 					    OFF_TO_IDX(backing_object->paging_offset),
1101a316d390SJohn Dyson 					    object,
1102a316d390SJohn Dyson 					    OFF_TO_IDX(object->paging_offset),
1103a316d390SJohn Dyson 					    OFF_TO_IDX(object->backing_object_offset));
1104f919ebdeSDavid Greenman 					vm_object_pip_wakeup(object);
110526f9a767SRodney W. Grimes 				} else {
110626f9a767SRodney W. Grimes 					object->paging_in_progress++;
110726f9a767SRodney W. Grimes 					/*
110824a1cce3SDavid Greenman 					 * move the shadow backing_object's pager data to
110924a1cce3SDavid Greenman 					 * "object" and convert "object" type to OBJT_SWAP.
111026f9a767SRodney W. Grimes 					 */
111124a1cce3SDavid Greenman 					object->type = OBJT_SWAP;
11122a4895f4SDavid Greenman 					object->un_pager.swp.swp_nblocks =
11132a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_nblocks;
11142a4895f4SDavid Greenman 					object->un_pager.swp.swp_allocsize =
11152a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_allocsize;
11162a4895f4SDavid Greenman 					object->un_pager.swp.swp_blocks =
11172a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_blocks;
11182a4895f4SDavid Greenman 					object->un_pager.swp.swp_poip =		/* XXX */
11192a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_poip;
112026f9a767SRodney W. Grimes 					object->paging_offset = backing_object->paging_offset + backing_offset;
112124a1cce3SDavid Greenman 					TAILQ_INSERT_TAIL(&swap_pager_un_object_list, object, pager_object_list);
112224a1cce3SDavid Greenman 
112324a1cce3SDavid Greenman 					/*
112424a1cce3SDavid Greenman 					 * Convert backing object from OBJT_SWAP to
112524a1cce3SDavid Greenman 					 * OBJT_DEFAULT. XXX - only the TAILQ_REMOVE is
112624a1cce3SDavid Greenman 					 * actually necessary.
112724a1cce3SDavid Greenman 					 */
112824a1cce3SDavid Greenman 					backing_object->type = OBJT_DEFAULT;
112924a1cce3SDavid Greenman 					TAILQ_REMOVE(&swap_pager_un_object_list, backing_object, pager_object_list);
113026f9a767SRodney W. Grimes 					/*
113126f9a767SRodney W. Grimes 					 * free unnecessary blocks
113226f9a767SRodney W. Grimes 					 */
1133a316d390SJohn Dyson 					swap_pager_freespace(object, 0,
1134a316d390SJohn Dyson 						OFF_TO_IDX(object->paging_offset));
1135f919ebdeSDavid Greenman 					vm_object_pip_wakeup(object);
1136c0503609SDavid Greenman 				}
1137c0503609SDavid Greenman 
1138f919ebdeSDavid Greenman 				vm_object_pip_wakeup(backing_object);
1139c0503609SDavid Greenman 			}
1140df8bae1dSRodney W. Grimes 			/*
1141df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
114224a1cce3SDavid Greenman 			 * Note that the reference to backing_object->backing_object
1143df8bae1dSRodney W. Grimes 			 * moves from within backing_object to within object.
1144df8bae1dSRodney W. Grimes 			 */
1145df8bae1dSRodney W. Grimes 
114624a1cce3SDavid Greenman 			TAILQ_REMOVE(&object->backing_object->shadow_head, object,
114724a1cce3SDavid Greenman 			    shadow_list);
1148eaf13dd7SJohn Dyson 			object->backing_object->shadow_count--;
1149eaf13dd7SJohn Dyson 			object->backing_object->generation++;
1150de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
115124a1cce3SDavid Greenman 				TAILQ_REMOVE(&backing_object->backing_object->shadow_head,
115224a1cce3SDavid Greenman 				    backing_object, shadow_list);
1153eaf13dd7SJohn Dyson 				backing_object->backing_object->shadow_count--;
1154eaf13dd7SJohn Dyson 				backing_object->backing_object->generation++;
1155de5f6a77SJohn Dyson 			}
115624a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
1157de5f6a77SJohn Dyson 			if (object->backing_object) {
115824a1cce3SDavid Greenman 				TAILQ_INSERT_TAIL(&object->backing_object->shadow_head,
115924a1cce3SDavid Greenman 				    object, shadow_list);
1160eaf13dd7SJohn Dyson 				object->backing_object->shadow_count++;
1161eaf13dd7SJohn Dyson 				object->backing_object->generation++;
1162de5f6a77SJohn Dyson 			}
11632fe6e4d7SDavid Greenman 
116424a1cce3SDavid Greenman 			object->backing_object_offset += backing_object->backing_object_offset;
1165df8bae1dSRodney W. Grimes 			/*
1166df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1167df8bae1dSRodney W. Grimes 			 *
11680d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
11690d94caffSDavid Greenman 			 * and no object references within it, all that is
11700d94caffSDavid Greenman 			 * necessary is to dispose of it.
1171df8bae1dSRodney W. Grimes 			 */
1172df8bae1dSRodney W. Grimes 
1173df8bae1dSRodney W. Grimes 			TAILQ_REMOVE(&vm_object_list, backing_object,
1174df8bae1dSRodney W. Grimes 			    object_list);
1175df8bae1dSRodney W. Grimes 			vm_object_count--;
1176df8bae1dSRodney W. Grimes 
117799448ed1SJohn Dyson 			zfree(obj_zone, backing_object);
1178df8bae1dSRodney W. Grimes 
1179df8bae1dSRodney W. Grimes 			object_collapses++;
11800d94caffSDavid Greenman 		} else {
118195e5e988SJohn Dyson 			vm_object_t new_backing_object;
1182df8bae1dSRodney W. Grimes 			/*
1183df8bae1dSRodney W. Grimes 			 * If all of the pages in the backing object are
11840d94caffSDavid Greenman 			 * shadowed by the parent object, the parent object no
11850d94caffSDavid Greenman 			 * longer has to shadow the backing object; it can
11860d94caffSDavid Greenman 			 * shadow the next one in the chain.
1187df8bae1dSRodney W. Grimes 			 *
11880d94caffSDavid Greenman 			 * The backing object must not be paged out - we'd have
11890d94caffSDavid Greenman 			 * to check all of the paged-out pages, as well.
1190df8bae1dSRodney W. Grimes 			 */
1191df8bae1dSRodney W. Grimes 
119224a1cce3SDavid Greenman 			if (backing_object->type != OBJT_DEFAULT) {
1193df8bae1dSRodney W. Grimes 				return;
1194df8bae1dSRodney W. Grimes 			}
1195df8bae1dSRodney W. Grimes 			/*
11960d94caffSDavid Greenman 			 * Should have a check for a 'small' number of pages
11970d94caffSDavid Greenman 			 * here.
1198df8bae1dSRodney W. Grimes 			 */
1199df8bae1dSRodney W. Grimes 
1200eaf13dd7SJohn Dyson 			for (p = TAILQ_FIRST(&backing_object->memq); p;
1201eaf13dd7SJohn Dyson 					p = TAILQ_NEXT(p, listq)) {
1202eaf13dd7SJohn Dyson 
1203a316d390SJohn Dyson 				new_pindex = p->pindex - backing_offset_index;
1204ffc82b0aSJohn Dyson 				p->flags |= PG_BUSY;
1205df8bae1dSRodney W. Grimes 
1206df8bae1dSRodney W. Grimes 				/*
12070d94caffSDavid Greenman 				 * If the parent has a page here, or if this
12080d94caffSDavid Greenman 				 * page falls outside the parent, keep going.
1209df8bae1dSRodney W. Grimes 				 *
12100d94caffSDavid Greenman 				 * Otherwise, the backing_object must be left in
12110d94caffSDavid Greenman 				 * the chain.
1212df8bae1dSRodney W. Grimes 				 */
1213df8bae1dSRodney W. Grimes 
1214a316d390SJohn Dyson 				if (p->pindex >= backing_offset_index &&
1215a316d390SJohn Dyson 					new_pindex <= size) {
121624a1cce3SDavid Greenman 
1217a316d390SJohn Dyson 					pp = vm_page_lookup(object, new_pindex);
121824a1cce3SDavid Greenman 
1219eaf13dd7SJohn Dyson 					if ((pp == NULL) || (pp->flags & PG_BUSY) || pp->busy) {
1220eaf13dd7SJohn Dyson 						PAGE_WAKEUP(p);
1221eaf13dd7SJohn Dyson 						return;
1222eaf13dd7SJohn Dyson 					}
1223eaf13dd7SJohn Dyson 
1224eaf13dd7SJohn Dyson 					pp->flags |= PG_BUSY;
1225eaf13dd7SJohn Dyson 					if ((pp->valid == 0) &&
1226a316d390SJohn Dyson 				   	    !vm_pager_has_page(object, OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL)) {
1227df8bae1dSRodney W. Grimes 						/*
12280d94caffSDavid Greenman 						 * Page still needed. Can't go any
12290d94caffSDavid Greenman 						 * further.
1230df8bae1dSRodney W. Grimes 						 */
1231eaf13dd7SJohn Dyson 						PAGE_WAKEUP(pp);
1232eaf13dd7SJohn Dyson 						PAGE_WAKEUP(p);
1233df8bae1dSRodney W. Grimes 						return;
1234df8bae1dSRodney W. Grimes 					}
1235eaf13dd7SJohn Dyson 					PAGE_WAKEUP(pp);
1236df8bae1dSRodney W. Grimes 				}
1237eaf13dd7SJohn Dyson 				PAGE_WAKEUP(p);
123824a1cce3SDavid Greenman 			}
1239df8bae1dSRodney W. Grimes 
1240df8bae1dSRodney W. Grimes 			/*
12410d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
12420d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
12430d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1244df8bae1dSRodney W. Grimes 			 */
1245df8bae1dSRodney W. Grimes 
124695e5e988SJohn Dyson 			TAILQ_REMOVE(&backing_object->shadow_head,
124724a1cce3SDavid Greenman 			    object, shadow_list);
1248eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
1249eaf13dd7SJohn Dyson 			backing_object->generation++;
125095e5e988SJohn Dyson 
125195e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
125295e5e988SJohn Dyson 			if (object->backing_object = new_backing_object) {
125395e5e988SJohn Dyson 				vm_object_reference(new_backing_object);
125495e5e988SJohn Dyson 				TAILQ_INSERT_TAIL(&new_backing_object->shadow_head,
125524a1cce3SDavid Greenman 				    object, shadow_list);
1256eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1257eaf13dd7SJohn Dyson 				new_backing_object->generation++;
125895e5e988SJohn Dyson 				object->backing_object_offset +=
125995e5e988SJohn Dyson 					backing_object->backing_object_offset;
1260de5f6a77SJohn Dyson 			}
1261df8bae1dSRodney W. Grimes 
1262df8bae1dSRodney W. Grimes 			/*
12630d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
12640d94caffSDavid Greenman 			 * its ref_count was at least 2, it will not vanish;
1265eaf13dd7SJohn Dyson 			 * so we don't need to call vm_object_deallocate, but
1266eaf13dd7SJohn Dyson 			 * we do anyway.
1267df8bae1dSRodney W. Grimes 			 */
126895e5e988SJohn Dyson 			vm_object_deallocate(backing_object);
1269df8bae1dSRodney W. Grimes 			object_bypasses++;
1270df8bae1dSRodney W. Grimes 		}
1271df8bae1dSRodney W. Grimes 
1272df8bae1dSRodney W. Grimes 		/*
1273df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1274df8bae1dSRodney W. Grimes 		 */
1275df8bae1dSRodney W. Grimes 	}
1276df8bae1dSRodney W. Grimes }
1277df8bae1dSRodney W. Grimes 
1278df8bae1dSRodney W. Grimes /*
1279df8bae1dSRodney W. Grimes  *	vm_object_page_remove: [internal]
1280df8bae1dSRodney W. Grimes  *
1281df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
1282df8bae1dSRodney W. Grimes  *	object range from the object's list of pages.
1283df8bae1dSRodney W. Grimes  *
1284df8bae1dSRodney W. Grimes  *	The object must be locked.
1285df8bae1dSRodney W. Grimes  */
128626f9a767SRodney W. Grimes void
12877c1f6cedSDavid Greenman vm_object_page_remove(object, start, end, clean_only)
1288df8bae1dSRodney W. Grimes 	register vm_object_t object;
1289a316d390SJohn Dyson 	register vm_pindex_t start;
1290a316d390SJohn Dyson 	register vm_pindex_t end;
12917c1f6cedSDavid Greenman 	boolean_t clean_only;
1292df8bae1dSRodney W. Grimes {
1293df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
1294a316d390SJohn Dyson 	unsigned int size;
129595e5e988SJohn Dyson 	int s, all;
1296df8bae1dSRodney W. Grimes 
1297df8bae1dSRodney W. Grimes 	if (object == NULL)
1298df8bae1dSRodney W. Grimes 		return;
1299df8bae1dSRodney W. Grimes 
130095e5e988SJohn Dyson 	all = ((end == 0) && (start == 0));
130195e5e988SJohn Dyson 
13022fe6e4d7SDavid Greenman 	object->paging_in_progress++;
130326f9a767SRodney W. Grimes again:
130426f9a767SRodney W. Grimes 	size = end - start;
130595e5e988SJohn Dyson 	if (all || size > 4 || size >= object->size / 4) {
1306b18bfc3dSJohn Dyson 		for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1307b18bfc3dSJohn Dyson 			next = TAILQ_NEXT(p, listq);
130895e5e988SJohn Dyson 			if (all || ((start <= p->pindex) && (p->pindex < end))) {
1309bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1310bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1311bd7e5f99SJohn Dyson 					p->valid = 0;
13120d94caffSDavid Greenman 					continue;
13130d94caffSDavid Greenman 				}
13140891ef4cSJohn Dyson 
1315b18bfc3dSJohn Dyson 				/*
1316b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1317b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1318b18bfc3dSJohn Dyson 				 */
1319ffc82b0aSJohn Dyson 
1320ffc82b0aSJohn Dyson  				if (vm_page_sleep(p, "vmopar", &p->busy))
132126f9a767SRodney W. Grimes  					goto again;
13220891ef4cSJohn Dyson 
13238f9110f6SJohn Dyson 				if (clean_only && p->valid) {
13247c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
13257c1f6cedSDavid Greenman 					if (p->valid & p->dirty)
13267c1f6cedSDavid Greenman 						continue;
13277c1f6cedSDavid Greenman 				}
1328eaf13dd7SJohn Dyson 
1329eaf13dd7SJohn Dyson 				p->flags |= PG_BUSY;
1330f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
1331df8bae1dSRodney W. Grimes 				vm_page_free(p);
133226f9a767SRodney W. Grimes 			}
133326f9a767SRodney W. Grimes 		}
133426f9a767SRodney W. Grimes 	} else {
133526f9a767SRodney W. Grimes 		while (size > 0) {
1336bd7e5f99SJohn Dyson 			if ((p = vm_page_lookup(object, start)) != 0) {
1337eaf13dd7SJohn Dyson 
1338bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1339bd7e5f99SJohn Dyson 					p->valid = 0;
1340bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1341bd7e5f99SJohn Dyson 					start += 1;
1342bd7e5f99SJohn Dyson 					size -= 1;
1343bd7e5f99SJohn Dyson 					continue;
13440d94caffSDavid Greenman 				}
1345eaf13dd7SJohn Dyson 
1346b18bfc3dSJohn Dyson 				/*
1347b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1348b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1349b18bfc3dSJohn Dyson 				 */
1350ffc82b0aSJohn Dyson  				if (vm_page_sleep(p, "vmopar", &p->busy))
135126f9a767SRodney W. Grimes 					goto again;
1352eaf13dd7SJohn Dyson 
13538f9110f6SJohn Dyson 				if (clean_only && p->valid) {
13547c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
1355bd7e5f99SJohn Dyson 					if (p->valid & p->dirty) {
1356bd7e5f99SJohn Dyson 						start += 1;
1357bd7e5f99SJohn Dyson 						size -= 1;
13587c1f6cedSDavid Greenman 						continue;
13597c1f6cedSDavid Greenman 					}
1360bd7e5f99SJohn Dyson 				}
1361eaf13dd7SJohn Dyson 
1362eaf13dd7SJohn Dyson 				p->flags |= PG_BUSY;
1363f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
136426f9a767SRodney W. Grimes 				vm_page_free(p);
136526f9a767SRodney W. Grimes 			}
1366a316d390SJohn Dyson 			start += 1;
1367a316d390SJohn Dyson 			size -= 1;
1368df8bae1dSRodney W. Grimes 		}
1369df8bae1dSRodney W. Grimes 	}
1370f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
1371c0503609SDavid Greenman }
1372df8bae1dSRodney W. Grimes 
1373df8bae1dSRodney W. Grimes /*
1374df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1375df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1376df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1377df8bae1dSRodney W. Grimes  *
1378df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1379df8bae1dSRodney W. Grimes  *
1380df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1381df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1382df8bae1dSRodney W. Grimes  *
1383df8bae1dSRodney W. Grimes  *	Parameters:
1384df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1385df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1386df8bae1dSRodney W. Grimes  *		next_object	Second object into coalesce
1387df8bae1dSRodney W. Grimes  *		next_offset	Offset into next_object
1388df8bae1dSRodney W. Grimes  *
1389df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
1390df8bae1dSRodney W. Grimes  *		next_size	Size of reference to next_object
1391df8bae1dSRodney W. Grimes  *
1392df8bae1dSRodney W. Grimes  *	Conditions:
1393df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1394df8bae1dSRodney W. Grimes  */
13950d94caffSDavid Greenman boolean_t
1396a316d390SJohn Dyson vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size)
1397df8bae1dSRodney W. Grimes 	register vm_object_t prev_object;
1398a316d390SJohn Dyson 	vm_pindex_t prev_pindex;
1399df8bae1dSRodney W. Grimes 	vm_size_t prev_size, next_size;
1400df8bae1dSRodney W. Grimes {
1401df8bae1dSRodney W. Grimes 	vm_size_t newsize;
1402df8bae1dSRodney W. Grimes 
1403df8bae1dSRodney W. Grimes 	if (prev_object == NULL) {
1404df8bae1dSRodney W. Grimes 		return (TRUE);
1405df8bae1dSRodney W. Grimes 	}
1406df8bae1dSRodney W. Grimes 
140730dcfc09SJohn Dyson 	if (prev_object->type != OBJT_DEFAULT) {
140830dcfc09SJohn Dyson 		return (FALSE);
140930dcfc09SJohn Dyson 	}
141030dcfc09SJohn Dyson 
1411df8bae1dSRodney W. Grimes 	/*
1412df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1413df8bae1dSRodney W. Grimes 	 */
1414df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1415df8bae1dSRodney W. Grimes 
1416df8bae1dSRodney W. Grimes 	/*
14170d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
14180d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
14190d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1420df8bae1dSRodney W. Grimes 	 */
1421df8bae1dSRodney W. Grimes 
14228cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
1423df8bae1dSRodney W. Grimes 		return (FALSE);
1424df8bae1dSRodney W. Grimes 	}
1425a316d390SJohn Dyson 
1426a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
1427a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
14288cc7e047SJohn Dyson 
14298cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
14308cc7e047SJohn Dyson 	    (prev_object->size != prev_pindex + prev_size)) {
14318cc7e047SJohn Dyson 		return (FALSE);
14328cc7e047SJohn Dyson 	}
14338cc7e047SJohn Dyson 
1434df8bae1dSRodney W. Grimes 	/*
14350d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
14360d94caffSDavid Greenman 	 * deallocation.
1437df8bae1dSRodney W. Grimes 	 */
1438df8bae1dSRodney W. Grimes 
1439df8bae1dSRodney W. Grimes 	vm_object_page_remove(prev_object,
1440a316d390SJohn Dyson 	    prev_pindex + prev_size,
1441a316d390SJohn Dyson 	    prev_pindex + prev_size + next_size, FALSE);
1442df8bae1dSRodney W. Grimes 
1443df8bae1dSRodney W. Grimes 	/*
1444df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1445df8bae1dSRodney W. Grimes 	 */
1446a316d390SJohn Dyson 	newsize = prev_pindex + prev_size + next_size;
1447df8bae1dSRodney W. Grimes 	if (newsize > prev_object->size)
1448df8bae1dSRodney W. Grimes 		prev_object->size = newsize;
1449df8bae1dSRodney W. Grimes 
1450df8bae1dSRodney W. Grimes 	return (TRUE);
1451df8bae1dSRodney W. Grimes }
1452df8bae1dSRodney W. Grimes 
1453c7c34a24SBruce Evans #include "opt_ddb.h"
1454c3cb3e12SDavid Greenman #ifdef DDB
1455c7c34a24SBruce Evans #include <sys/kernel.h>
1456c7c34a24SBruce Evans 
1457c7c34a24SBruce Evans #include <machine/cons.h>
1458c7c34a24SBruce Evans 
1459c7c34a24SBruce Evans #include <ddb/ddb.h>
1460c7c34a24SBruce Evans 
1461c7c34a24SBruce Evans static int	_vm_object_in_map __P((vm_map_t map, vm_object_t object,
1462c7c34a24SBruce Evans 				       vm_map_entry_t entry));
1463c7c34a24SBruce Evans static int	vm_object_in_map __P((vm_object_t object));
1464c3cb3e12SDavid Greenman 
1465cac597e4SBruce Evans static int
1466a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry)
1467a1f6d91cSDavid Greenman 	vm_map_t map;
1468a1f6d91cSDavid Greenman 	vm_object_t object;
1469a1f6d91cSDavid Greenman 	vm_map_entry_t entry;
1470a1f6d91cSDavid Greenman {
1471a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1472a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1473a1f6d91cSDavid Greenman 	vm_object_t obj;
1474a1f6d91cSDavid Greenman 	int entcount;
1475a1f6d91cSDavid Greenman 
1476a1f6d91cSDavid Greenman 	if (map == 0)
1477a1f6d91cSDavid Greenman 		return 0;
1478a1f6d91cSDavid Greenman 
1479a1f6d91cSDavid Greenman 	if (entry == 0) {
1480a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1481a1f6d91cSDavid Greenman 		entcount = map->nentries;
1482a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1483a1f6d91cSDavid Greenman 			if( _vm_object_in_map(map, object, tmpe)) {
1484a1f6d91cSDavid Greenman 				return 1;
1485a1f6d91cSDavid Greenman 			}
1486a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1487a1f6d91cSDavid Greenman 		}
1488afa07f7eSJohn Dyson 	} else if (entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) {
1489a1f6d91cSDavid Greenman 		tmpm = entry->object.share_map;
1490a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1491a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1492a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1493a1f6d91cSDavid Greenman 			if( _vm_object_in_map(tmpm, object, tmpe)) {
1494a1f6d91cSDavid Greenman 				return 1;
1495a1f6d91cSDavid Greenman 			}
1496a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1497a1f6d91cSDavid Greenman 		}
1498a1f6d91cSDavid Greenman 	} else if (obj = entry->object.vm_object) {
149924a1cce3SDavid Greenman 		for(; obj; obj=obj->backing_object)
1500a1f6d91cSDavid Greenman 			if( obj == object) {
1501a1f6d91cSDavid Greenman 				return 1;
1502a1f6d91cSDavid Greenman 			}
1503a1f6d91cSDavid Greenman 	}
1504a1f6d91cSDavid Greenman 	return 0;
1505a1f6d91cSDavid Greenman }
1506a1f6d91cSDavid Greenman 
1507cac597e4SBruce Evans static int
1508a1f6d91cSDavid Greenman vm_object_in_map( object)
1509a1f6d91cSDavid Greenman 	vm_object_t object;
1510a1f6d91cSDavid Greenman {
1511a1f6d91cSDavid Greenman 	struct proc *p;
15121b67ec6dSJeffrey Hsu 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1513a1f6d91cSDavid Greenman 		if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1514a1f6d91cSDavid Greenman 			continue;
1515a1f6d91cSDavid Greenman 		if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1516a1f6d91cSDavid Greenman 			return 1;
1517a1f6d91cSDavid Greenman 	}
1518a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kernel_map, object, 0))
1519a1f6d91cSDavid Greenman 		return 1;
1520a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kmem_map, object, 0))
1521a1f6d91cSDavid Greenman 		return 1;
1522a1f6d91cSDavid Greenman 	if( _vm_object_in_map( pager_map, object, 0))
1523a1f6d91cSDavid Greenman 		return 1;
1524a1f6d91cSDavid Greenman 	if( _vm_object_in_map( buffer_map, object, 0))
1525a1f6d91cSDavid Greenman 		return 1;
1526a1f6d91cSDavid Greenman 	if( _vm_object_in_map( io_map, object, 0))
1527a1f6d91cSDavid Greenman 		return 1;
1528a1f6d91cSDavid Greenman 	if( _vm_object_in_map( phys_map, object, 0))
1529a1f6d91cSDavid Greenman 		return 1;
1530a1f6d91cSDavid Greenman 	if( _vm_object_in_map( mb_map, object, 0))
1531a1f6d91cSDavid Greenman 		return 1;
1532a1f6d91cSDavid Greenman 	if( _vm_object_in_map( u_map, object, 0))
1533a1f6d91cSDavid Greenman 		return 1;
1534a1f6d91cSDavid Greenman 	return 0;
1535a1f6d91cSDavid Greenman }
1536a1f6d91cSDavid Greenman 
1537c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
1538f708ef1bSPoul-Henning Kamp {
1539a1f6d91cSDavid Greenman 	vm_object_t object;
1540a1f6d91cSDavid Greenman 
1541a1f6d91cSDavid Greenman 	/*
1542a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
1543a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
1544a1f6d91cSDavid Greenman 	 */
1545b18bfc3dSJohn Dyson 	for (object = TAILQ_FIRST(&vm_object_list);
1546a1f6d91cSDavid Greenman 			object != NULL;
1547b18bfc3dSJohn Dyson 			object = TAILQ_NEXT(object, object_list)) {
154824a1cce3SDavid Greenman 		if (object->handle == NULL &&
154924a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1550a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
1551c7c34a24SBruce Evans 				db_printf("vmochk: internal obj has zero ref count: %d\n",
1552a1f6d91cSDavid Greenman 					object->size);
1553a1f6d91cSDavid Greenman 			}
1554a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
1555c7c34a24SBruce Evans 				db_printf("vmochk: internal obj is not in a map: "
15563af76890SPoul-Henning Kamp 		"ref: %d, size: %d: 0x%x, backing_object: 0x%x\n",
15573af76890SPoul-Henning Kamp 				    object->ref_count, object->size,
15583af76890SPoul-Henning Kamp 				    object->size, object->backing_object);
1559a1f6d91cSDavid Greenman 			}
1560a1f6d91cSDavid Greenman 		}
1561a1f6d91cSDavid Greenman 	}
1562a1f6d91cSDavid Greenman }
1563a1f6d91cSDavid Greenman 
156426f9a767SRodney W. Grimes /*
1565df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
1566df8bae1dSRodney W. Grimes  */
1567c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
1568df8bae1dSRodney W. Grimes {
1569c7c34a24SBruce Evans 	/* XXX convert args. */
1570c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
1571c7c34a24SBruce Evans 	boolean_t full = have_addr;
1572c7c34a24SBruce Evans 
1573df8bae1dSRodney W. Grimes 	register vm_page_t p;
1574df8bae1dSRodney W. Grimes 
1575c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
1576c7c34a24SBruce Evans #define	count	was_count
1577c7c34a24SBruce Evans 
1578df8bae1dSRodney W. Grimes 	register int count;
1579df8bae1dSRodney W. Grimes 
1580df8bae1dSRodney W. Grimes 	if (object == NULL)
1581df8bae1dSRodney W. Grimes 		return;
1582df8bae1dSRodney W. Grimes 
158395e5e988SJohn Dyson 	db_iprintf("Object 0x%x: type=%d, size=0x%x, res=%d, ref=%d, flags=0x%x\n",
158495e5e988SJohn Dyson 	    (int) object, (int) object->type, (int) object->size,
158595e5e988SJohn Dyson 	    object->resident_page_count,
158695e5e988SJohn Dyson 		object->ref_count,
158795e5e988SJohn Dyson 		object->flags);
158895e5e988SJohn Dyson 	db_iprintf(" sref=%d, offset=0x%x, backing_object(%d)=(0x%x)+0x%x\n",
158995e5e988SJohn Dyson 		object->shadow_count,
15902a4895f4SDavid Greenman 	    (int) object->paging_offset,
159195e5e988SJohn Dyson 		(((int)object->backing_object)?object->backing_object->ref_count:0),
159295e5e988SJohn Dyson 	    (int) object->backing_object,
159395e5e988SJohn Dyson 		(int) object->backing_object_offset);
1594df8bae1dSRodney W. Grimes 
1595df8bae1dSRodney W. Grimes 	if (!full)
1596df8bae1dSRodney W. Grimes 		return;
1597df8bae1dSRodney W. Grimes 
1598c7c34a24SBruce Evans 	db_indent += 2;
1599df8bae1dSRodney W. Grimes 	count = 0;
1600b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = TAILQ_NEXT(p, listq)) {
1601df8bae1dSRodney W. Grimes 		if (count == 0)
1602c7c34a24SBruce Evans 			db_iprintf("memory:=");
1603df8bae1dSRodney W. Grimes 		else if (count == 6) {
1604c7c34a24SBruce Evans 			db_printf("\n");
1605c7c34a24SBruce Evans 			db_iprintf(" ...");
1606df8bae1dSRodney W. Grimes 			count = 0;
1607df8bae1dSRodney W. Grimes 		} else
1608c7c34a24SBruce Evans 			db_printf(",");
1609df8bae1dSRodney W. Grimes 		count++;
1610df8bae1dSRodney W. Grimes 
1611c7c34a24SBruce Evans 		db_printf("(off=0x%lx,page=0x%lx)",
1612a316d390SJohn Dyson 		    (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
1613df8bae1dSRodney W. Grimes 	}
1614df8bae1dSRodney W. Grimes 	if (count != 0)
1615c7c34a24SBruce Evans 		db_printf("\n");
1616c7c34a24SBruce Evans 	db_indent -= 2;
1617df8bae1dSRodney W. Grimes }
16185070c7f8SJohn Dyson 
1619c7c34a24SBruce Evans /* XXX. */
1620c7c34a24SBruce Evans #undef count
1621c7c34a24SBruce Evans 
1622c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
16235070c7f8SJohn Dyson void
1624c7c34a24SBruce Evans vm_object_print(addr, have_addr, count, modif)
1625c7c34a24SBruce Evans 	db_expr_t addr;
1626c7c34a24SBruce Evans 	boolean_t have_addr;
1627c7c34a24SBruce Evans 	db_expr_t count;
1628c7c34a24SBruce Evans 	char *modif;
1629c7c34a24SBruce Evans {
1630c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
1631c7c34a24SBruce Evans }
1632c7c34a24SBruce Evans 
1633c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
16345070c7f8SJohn Dyson {
16355070c7f8SJohn Dyson 	vm_object_t object;
16365070c7f8SJohn Dyson 	int nl = 0;
16375070c7f8SJohn Dyson 	int c;
16385070c7f8SJohn Dyson 	for (object = TAILQ_FIRST(&vm_object_list);
16395070c7f8SJohn Dyson 			object != NULL;
16405070c7f8SJohn Dyson 			object = TAILQ_NEXT(object, object_list)) {
16415070c7f8SJohn Dyson 		vm_pindex_t idx, fidx;
16425070c7f8SJohn Dyson 		vm_pindex_t osize;
16435070c7f8SJohn Dyson 		vm_offset_t pa = -1, padiff;
16445070c7f8SJohn Dyson 		int rcount;
16455070c7f8SJohn Dyson 		vm_page_t m;
16465070c7f8SJohn Dyson 
16475070c7f8SJohn Dyson 		db_printf("new object: 0x%x\n", object);
16485070c7f8SJohn Dyson 		if ( nl > 18) {
16495070c7f8SJohn Dyson 			c = cngetc();
16505070c7f8SJohn Dyson 			if (c != ' ')
16515070c7f8SJohn Dyson 				return;
16525070c7f8SJohn Dyson 			nl = 0;
16535070c7f8SJohn Dyson 		}
16545070c7f8SJohn Dyson 		nl++;
16555070c7f8SJohn Dyson 		rcount = 0;
16565070c7f8SJohn Dyson 		fidx = 0;
16575070c7f8SJohn Dyson 		osize = object->size;
16585070c7f8SJohn Dyson 		if (osize > 128)
16595070c7f8SJohn Dyson 			osize = 128;
16605070c7f8SJohn Dyson 		for(idx=0;idx<osize;idx++) {
16615070c7f8SJohn Dyson 			m = vm_page_lookup(object, idx);
16625070c7f8SJohn Dyson 			if (m == NULL) {
16635070c7f8SJohn Dyson 				if (rcount) {
16645070c7f8SJohn Dyson 					db_printf(" index(%d)run(%d)pa(0x%x)\n",
16655070c7f8SJohn Dyson 						fidx, rcount, pa);
16665070c7f8SJohn Dyson 					if ( nl > 18) {
16675070c7f8SJohn Dyson 						c = cngetc();
16685070c7f8SJohn Dyson 						if (c != ' ')
16695070c7f8SJohn Dyson 							return;
16705070c7f8SJohn Dyson 						nl = 0;
16715070c7f8SJohn Dyson 					}
16725070c7f8SJohn Dyson 					nl++;
16735070c7f8SJohn Dyson 					rcount = 0;
16745070c7f8SJohn Dyson 				}
16755070c7f8SJohn Dyson 				continue;
16765070c7f8SJohn Dyson 			}
16775070c7f8SJohn Dyson 
16785070c7f8SJohn Dyson 
16795070c7f8SJohn Dyson 			if (rcount &&
16805070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
16815070c7f8SJohn Dyson 				++rcount;
16825070c7f8SJohn Dyson 				continue;
16835070c7f8SJohn Dyson 			}
16845070c7f8SJohn Dyson 			if (rcount) {
16855070c7f8SJohn Dyson 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
16865070c7f8SJohn Dyson 				padiff >>= PAGE_SHIFT;
16875070c7f8SJohn Dyson 				padiff &= PQ_L2_MASK;
16885070c7f8SJohn Dyson 				if (padiff == 0) {
16895070c7f8SJohn Dyson 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
16905070c7f8SJohn Dyson 					++rcount;
16915070c7f8SJohn Dyson 					continue;
16925070c7f8SJohn Dyson 				}
16935070c7f8SJohn Dyson 				db_printf(" index(%d)run(%d)pa(0x%x)", fidx, rcount, pa);
16945070c7f8SJohn Dyson 				db_printf("pd(%d)\n", padiff);
16955070c7f8SJohn Dyson 				if ( nl > 18) {
16965070c7f8SJohn Dyson 					c = cngetc();
16975070c7f8SJohn Dyson 					if (c != ' ')
16985070c7f8SJohn Dyson 						return;
16995070c7f8SJohn Dyson 					nl = 0;
17005070c7f8SJohn Dyson 				}
17015070c7f8SJohn Dyson 				nl++;
17025070c7f8SJohn Dyson 			}
17035070c7f8SJohn Dyson 			fidx = idx;
17045070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
17055070c7f8SJohn Dyson 			rcount = 1;
17065070c7f8SJohn Dyson 		}
17075070c7f8SJohn Dyson 		if (rcount) {
17085070c7f8SJohn Dyson 			db_printf(" index(%d)run(%d)pa(0x%x)\n", fidx, rcount, pa);
17095070c7f8SJohn Dyson 			if ( nl > 18) {
17105070c7f8SJohn Dyson 				c = cngetc();
17115070c7f8SJohn Dyson 				if (c != ' ')
17125070c7f8SJohn Dyson 					return;
17135070c7f8SJohn Dyson 				nl = 0;
17145070c7f8SJohn Dyson 			}
17155070c7f8SJohn Dyson 			nl++;
17165070c7f8SJohn Dyson 		}
17175070c7f8SJohn Dyson 	}
17185070c7f8SJohn Dyson }
1719c3cb3e12SDavid Greenman #endif /* DDB */
1720