xref: /freebsd/sys/vm/vm_object.c (revision 5526d2d920eb17b1507499f35b275b486f7fe8d0)
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  *
645526d2d9SEivind Eklund  * $Id: vm_object.c,v 1.136 1999/01/02 11:34:57 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));
94f6b04d2bSDavid Greenman 
95df8bae1dSRodney W. Grimes /*
96df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
97df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
98df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
99df8bae1dSRodney W. Grimes  *
100df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
101df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
102df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
103df8bae1dSRodney W. Grimes  *
104df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
105df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
106df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
107df8bae1dSRodney W. Grimes  *	lock.
108df8bae1dSRodney W. Grimes  *
109df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
110df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
111df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
112df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
113df8bae1dSRodney W. Grimes  *
114df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
115df8bae1dSRodney W. Grimes  *	modified after time of creation are:
116df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
117df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
118df8bae1dSRodney W. Grimes  *
119df8bae1dSRodney W. Grimes  */
120df8bae1dSRodney W. Grimes 
12128f8db14SBruce Evans struct object_q vm_object_list;
122289bdf33SBruce Evans #ifndef NULL_SIMPLELOCKS
123303b270bSEivind Eklund static struct simplelock vm_object_list_lock;
124289bdf33SBruce Evans #endif
1254de628deSBruce Evans static long vm_object_count;		/* count of all objects */
12628f8db14SBruce Evans vm_object_t kernel_object;
12728f8db14SBruce Evans vm_object_t kmem_object;
128f708ef1bSPoul-Henning Kamp static struct vm_object kernel_object_store;
129f708ef1bSPoul-Henning Kamp static struct vm_object kmem_object_store;
130aef922f5SJohn Dyson extern int vm_pageout_page_count;
131df8bae1dSRodney W. Grimes 
132f708ef1bSPoul-Henning Kamp static long object_collapses;
133f708ef1bSPoul-Henning Kamp static long object_bypasses;
1345070c7f8SJohn Dyson static int next_index;
13599448ed1SJohn Dyson static vm_zone_t obj_zone;
13699448ed1SJohn Dyson static struct vm_zone obj_zone_store;
13799448ed1SJohn Dyson #define VM_OBJECTS_INIT 256
138303b270bSEivind Eklund static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
139c0877f10SJohn Dyson static int objidnumber;
140df8bae1dSRodney W. Grimes 
1413075778bSJohn Dyson void
14224a1cce3SDavid Greenman _vm_object_allocate(type, size, object)
14324a1cce3SDavid Greenman 	objtype_t type;
144df8bae1dSRodney W. Grimes 	vm_size_t size;
145df8bae1dSRodney W. Grimes 	register vm_object_t object;
146df8bae1dSRodney W. Grimes {
14799448ed1SJohn Dyson 	int incr;
148df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
14924a1cce3SDavid Greenman 	TAILQ_INIT(&object->shadow_head);
150a1f6d91cSDavid Greenman 
15124a1cce3SDavid Greenman 	object->type = type;
152df8bae1dSRodney W. Grimes 	object->size = size;
153a1f6d91cSDavid Greenman 	object->ref_count = 1;
15424a1cce3SDavid Greenman 	object->flags = 0;
155c0877f10SJohn Dyson 	object->id = ++objidnumber;
156c0877f10SJohn Dyson 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
157069e9bc1SDoug Rabson 		vm_object_set_flag(object, OBJ_ONEMAPPING);
158867a482dSJohn Dyson 	object->behavior = OBJ_NORMAL;
159df8bae1dSRodney W. Grimes 	object->paging_in_progress = 0;
160a1f6d91cSDavid Greenman 	object->resident_page_count = 0;
161eaf13dd7SJohn Dyson 	object->cache_count = 0;
162eaf13dd7SJohn Dyson 	object->wire_count = 0;
163de5f6a77SJohn Dyson 	object->shadow_count = 0;
1645070c7f8SJohn Dyson 	object->pg_color = next_index;
16599448ed1SJohn Dyson 	if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
16699448ed1SJohn Dyson 		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
16799448ed1SJohn Dyson 	else
16899448ed1SJohn Dyson 		incr = size;
16999448ed1SJohn Dyson 	next_index = (next_index + incr) & PQ_L2_MASK;
17024a1cce3SDavid Greenman 	object->handle = NULL;
171a316d390SJohn Dyson 	object->paging_offset = (vm_ooffset_t) 0;
17224a1cce3SDavid Greenman 	object->backing_object = NULL;
173a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
174a2f4a846SJohn Dyson 	object->page_hint = NULL;
175a1f6d91cSDavid Greenman 
176a1f6d91cSDavid Greenman 	object->last_read = 0;
1772d8acc0fSJohn Dyson 	object->generation++;
178df8bae1dSRodney W. Grimes 
179df8bae1dSRodney W. Grimes 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
180df8bae1dSRodney W. Grimes 	vm_object_count++;
181df8bae1dSRodney W. Grimes }
182df8bae1dSRodney W. Grimes 
183df8bae1dSRodney W. Grimes /*
18426f9a767SRodney W. Grimes  *	vm_object_init:
18526f9a767SRodney W. Grimes  *
18626f9a767SRodney W. Grimes  *	Initialize the VM objects module.
18726f9a767SRodney W. Grimes  */
18826f9a767SRodney W. Grimes void
189a316d390SJohn Dyson vm_object_init()
19026f9a767SRodney W. Grimes {
19126f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
192996c772fSJohn Dyson 	simple_lock_init(&vm_object_list_lock);
19326f9a767SRodney W. Grimes 	vm_object_count = 0;
1940217125fSDavid Greenman 
19526f9a767SRodney W. Grimes 	kernel_object = &kernel_object_store;
196a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
19726f9a767SRodney W. Grimes 	    kernel_object);
19826f9a767SRodney W. Grimes 
19926f9a767SRodney W. Grimes 	kmem_object = &kmem_object_store;
200a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
20126f9a767SRodney W. Grimes 	    kmem_object);
20299448ed1SJohn Dyson 
20399448ed1SJohn Dyson 	obj_zone = &obj_zone_store;
20499448ed1SJohn Dyson 	zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
20599448ed1SJohn Dyson 		vm_objects_init, VM_OBJECTS_INIT);
20699448ed1SJohn Dyson }
20799448ed1SJohn Dyson 
20899448ed1SJohn Dyson void
20999448ed1SJohn Dyson vm_object_init2() {
2100a80f406SJohn Dyson 	zinitna(obj_zone, NULL, NULL, 0, 0, 0, 1);
21126f9a767SRodney W. Grimes }
21226f9a767SRodney W. Grimes 
21326f9a767SRodney W. Grimes /*
21426f9a767SRodney W. Grimes  *	vm_object_allocate:
21526f9a767SRodney W. Grimes  *
21626f9a767SRodney W. Grimes  *	Returns a new object with the given size.
21726f9a767SRodney W. Grimes  */
21826f9a767SRodney W. Grimes 
21926f9a767SRodney W. Grimes vm_object_t
22024a1cce3SDavid Greenman vm_object_allocate(type, size)
22124a1cce3SDavid Greenman 	objtype_t type;
22226f9a767SRodney W. Grimes 	vm_size_t size;
22326f9a767SRodney W. Grimes {
22426f9a767SRodney W. Grimes 	register vm_object_t result;
22599448ed1SJohn Dyson 	result = (vm_object_t) zalloc(obj_zone);
22667bf6868SJohn Dyson 
22724a1cce3SDavid Greenman 	_vm_object_allocate(type, size, result);
22826f9a767SRodney W. Grimes 
22926f9a767SRodney W. Grimes 	return (result);
23026f9a767SRodney W. Grimes }
23126f9a767SRodney W. Grimes 
23226f9a767SRodney W. Grimes 
23326f9a767SRodney W. Grimes /*
234df8bae1dSRodney W. Grimes  *	vm_object_reference:
235df8bae1dSRodney W. Grimes  *
236df8bae1dSRodney W. Grimes  *	Gets another reference to the given object.
237df8bae1dSRodney W. Grimes  */
2386476c0d2SJohn Dyson void
23926f9a767SRodney W. Grimes vm_object_reference(object)
240df8bae1dSRodney W. Grimes 	register vm_object_t object;
241df8bae1dSRodney W. Grimes {
242df8bae1dSRodney W. Grimes 	if (object == NULL)
243df8bae1dSRodney W. Grimes 		return;
24495e5e988SJohn Dyson 
2455526d2d9SEivind Eklund 	KASSERT(!(object->flags & OBJ_DEAD),
2465526d2d9SEivind Eklund 		("vm_object_reference: attempting to reference dead obj"));
24795e5e988SJohn Dyson 
248df8bae1dSRodney W. Grimes 	object->ref_count++;
24947221757SJohn Dyson 	if (object->type == OBJT_VNODE) {
25047221757SJohn Dyson 		while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curproc)) {
25195461b45SJohn Dyson #if !defined(MAX_PERF)
25247221757SJohn Dyson 			printf("vm_object_reference: delay in getting object\n");
25395461b45SJohn Dyson #endif
25447221757SJohn Dyson 		}
25547221757SJohn Dyson 	}
25695e5e988SJohn Dyson }
25795e5e988SJohn Dyson 
258bf27292bSJohn Dyson void
25995e5e988SJohn Dyson vm_object_vndeallocate(object)
26095e5e988SJohn Dyson 	vm_object_t object;
26195e5e988SJohn Dyson {
26295e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
2635526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
2645526d2d9SEivind Eklund 		("vm_object_vndeallocate: not a vnode object"));
2655526d2d9SEivind Eklund 	KASSERT(vp, ("vm_object_vndeallocate: missing vp"));
2665526d2d9SEivind Eklund #if defined(INVARIANTS)
26795e5e988SJohn Dyson 	if (object->ref_count == 0) {
26895e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
26995e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
27095e5e988SJohn Dyson 	}
27195e5e988SJohn Dyson #endif
27295e5e988SJohn Dyson 
27395e5e988SJohn Dyson 	object->ref_count--;
27447221757SJohn Dyson 	if (object->ref_count == 0) {
275bf27292bSJohn Dyson 		vp->v_flag &= ~VTEXT;
276069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_OPT);
2772be70f79SJohn Dyson 	}
27847221757SJohn Dyson 	vrele(vp);
279df8bae1dSRodney W. Grimes }
280df8bae1dSRodney W. Grimes 
281df8bae1dSRodney W. Grimes /*
282df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
283df8bae1dSRodney W. Grimes  *
284df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
285df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
286df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
287df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
288df8bae1dSRodney W. Grimes  *	may be relinquished.
289df8bae1dSRodney W. Grimes  *
290df8bae1dSRodney W. Grimes  *	No object may be locked.
291df8bae1dSRodney W. Grimes  */
29226f9a767SRodney W. Grimes void
29326f9a767SRodney W. Grimes vm_object_deallocate(object)
29426f9a767SRodney W. Grimes 	vm_object_t object;
295df8bae1dSRodney W. Grimes {
296df8bae1dSRodney W. Grimes 	vm_object_t temp;
297df8bae1dSRodney W. Grimes 
298df8bae1dSRodney W. Grimes 	while (object != NULL) {
299df8bae1dSRodney W. Grimes 
30095e5e988SJohn Dyson 		if (object->type == OBJT_VNODE) {
30195e5e988SJohn Dyson 			vm_object_vndeallocate(object);
30295e5e988SJohn Dyson 			return;
30395e5e988SJohn Dyson 		}
30495e5e988SJohn Dyson 
3052be70f79SJohn Dyson 		if (object->ref_count == 0) {
30647221757SJohn Dyson 			panic("vm_object_deallocate: object deallocated too many times: %d", object->type);
3072be70f79SJohn Dyson 		} else if (object->ref_count > 2) {
3082be70f79SJohn Dyson 			object->ref_count--;
3092be70f79SJohn Dyson 			return;
3102be70f79SJohn Dyson 		}
3112be70f79SJohn Dyson 
3122be70f79SJohn Dyson 		/*
3132be70f79SJohn Dyson 		 * Here on ref_count of one or two, which are special cases for
3142be70f79SJohn Dyson 		 * objects.
3152be70f79SJohn Dyson 		 */
316c0877f10SJohn Dyson 		if ((object->ref_count == 2) && (object->shadow_count == 0)) {
317069e9bc1SDoug Rabson 			vm_object_set_flag(object, OBJ_ONEMAPPING);
318c0877f10SJohn Dyson 			object->ref_count--;
319c0877f10SJohn Dyson 			return;
320c0877f10SJohn Dyson 		} else if ((object->ref_count == 2) && (object->shadow_count == 1)) {
321be6d5bfaSDavid Greenman 			object->ref_count--;
3222be70f79SJohn Dyson 			if ((object->handle == NULL) &&
32324a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
32424a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
325a1f6d91cSDavid Greenman 				vm_object_t robject;
32695e5e988SJohn Dyson 
327b18bfc3dSJohn Dyson 				robject = TAILQ_FIRST(&object->shadow_head);
3285526d2d9SEivind Eklund 				KASSERT(robject != NULL,
3295526d2d9SEivind Eklund 					("vm_object_deallocate: ref_count: %d,"
33095e5e988SJohn Dyson 					 " shadow_count: %d",
3315526d2d9SEivind Eklund 					 object->ref_count,
3325526d2d9SEivind Eklund 					 object->shadow_count));
33395e5e988SJohn Dyson 				if ((robject->handle == NULL) &&
33424a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
33524a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
336a1f6d91cSDavid Greenman 
33795e5e988SJohn Dyson 					robject->ref_count++;
33895e5e988SJohn Dyson 
33995e5e988SJohn Dyson 			retry:
340ffc82b0aSJohn Dyson 					if (robject->paging_in_progress ||
341ffc82b0aSJohn Dyson 							object->paging_in_progress) {
34266095752SJohn Dyson 						vm_object_pip_sleep(robject, "objde1");
343ffc82b0aSJohn Dyson 						if (robject->paging_in_progress &&
344ffc82b0aSJohn Dyson 							robject->type == OBJT_SWAP) {
34566095752SJohn Dyson 							swap_pager_sync();
34695e5e988SJohn Dyson 							goto retry;
3470d94caffSDavid Greenman 						}
348a1f6d91cSDavid Greenman 
34966095752SJohn Dyson 						vm_object_pip_sleep(object, "objde2");
350ffc82b0aSJohn Dyson 						if (object->paging_in_progress &&
351ffc82b0aSJohn Dyson 							object->type == OBJT_SWAP) {
35266095752SJohn Dyson 							swap_pager_sync();
35366095752SJohn Dyson 						}
35495e5e988SJohn Dyson 						goto retry;
355a1f6d91cSDavid Greenman 					}
356a1f6d91cSDavid Greenman 
35795e5e988SJohn Dyson 					if( robject->ref_count == 1) {
35895e5e988SJohn Dyson 						robject->ref_count--;
359ba8da839SDavid Greenman 						object = robject;
36095e5e988SJohn Dyson 						goto doterm;
36195e5e988SJohn Dyson 					}
36295e5e988SJohn Dyson 
36395e5e988SJohn Dyson 					object = robject;
36495e5e988SJohn Dyson 					vm_object_collapse(object);
365ba8da839SDavid Greenman 					continue;
366a1f6d91cSDavid Greenman 				}
36795e5e988SJohn Dyson 			}
36895e5e988SJohn Dyson 
369a1f6d91cSDavid Greenman 			return;
370df8bae1dSRodney W. Grimes 
37195e5e988SJohn Dyson 		} else {
3721efb74fbSJohn Dyson 			object->ref_count--;
37395e5e988SJohn Dyson 			if (object->ref_count != 0)
37495e5e988SJohn Dyson 				return;
37595e5e988SJohn Dyson 		}
37695e5e988SJohn Dyson 
37795e5e988SJohn Dyson doterm:
3781efb74fbSJohn Dyson 
37924a1cce3SDavid Greenman 		temp = object->backing_object;
380de5f6a77SJohn Dyson 		if (temp) {
38124a1cce3SDavid Greenman 			TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
38295e5e988SJohn Dyson 			temp->shadow_count--;
3832d8acc0fSJohn Dyson 			if (temp->ref_count == 0)
384069e9bc1SDoug Rabson 				vm_object_clear_flag(temp, OBJ_OPT);
385eaf13dd7SJohn Dyson 			temp->generation++;
38695461b45SJohn Dyson 			object->backing_object = NULL;
387de5f6a77SJohn Dyson 		}
388df8bae1dSRodney W. Grimes 		vm_object_terminate(object);
389df8bae1dSRodney W. Grimes 		/* unlocks and deallocates object */
390df8bae1dSRodney W. Grimes 		object = temp;
391df8bae1dSRodney W. Grimes 	}
392df8bae1dSRodney W. Grimes }
393df8bae1dSRodney W. Grimes 
394df8bae1dSRodney W. Grimes /*
395df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
396df8bae1dSRodney W. Grimes  *	up all previously used resources.
397df8bae1dSRodney W. Grimes  *
398df8bae1dSRodney W. Grimes  *	The object must be locked.
399df8bae1dSRodney W. Grimes  */
40095e5e988SJohn Dyson void
40126f9a767SRodney W. Grimes vm_object_terminate(object)
402df8bae1dSRodney W. Grimes 	register vm_object_t object;
403df8bae1dSRodney W. Grimes {
4043af76890SPoul-Henning Kamp 	register vm_page_t p;
405e4b7635dSDavid Greenman 	int s;
406df8bae1dSRodney W. Grimes 
40795e5e988SJohn Dyson 	/*
40895e5e988SJohn Dyson 	 * Make sure no one uses us.
40995e5e988SJohn Dyson 	 */
410069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
4113c631446SJohn Dyson 
412df8bae1dSRodney W. Grimes 	/*
413f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
414df8bae1dSRodney W. Grimes 	 */
41566095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
416df8bae1dSRodney W. Grimes 
4175526d2d9SEivind Eklund 	KASSERT(!object->paging_in_progress,
4185526d2d9SEivind Eklund 		("vm_object_terminate: pageout in progress"));
41926f9a767SRodney W. Grimes 
42026f9a767SRodney W. Grimes 	/*
4210d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
4220d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
42326f9a767SRodney W. Grimes 	 */
42424a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
42595e5e988SJohn Dyson 		struct vnode *vp;
42695e5e988SJohn Dyson 
42795e5e988SJohn Dyson 		/*
42895e5e988SJohn Dyson 		 * Freeze optimized copies.
42995e5e988SJohn Dyson 		 */
43095e5e988SJohn Dyson 		vm_freeze_copyopts(object, 0, object->size);
43195e5e988SJohn Dyson 
43295e5e988SJohn Dyson 		/*
43395e5e988SJohn Dyson 		 * Clean pages and flush buffers.
43495e5e988SJohn Dyson 		 */
4358f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
43695e5e988SJohn Dyson 
43795e5e988SJohn Dyson 		vp = (struct vnode *) object->handle;
438f6b04d2bSDavid Greenman 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
439bef608bdSJohn Dyson 	}
440bef608bdSJohn Dyson 
4410b10ba98SDavid Greenman 	if (object->ref_count != 0)
4420b10ba98SDavid Greenman 		panic("vm_object_terminate: object with references, ref_count=%d", object->ref_count);
443996c772fSJohn Dyson 
4440d94caffSDavid Greenman 	/*
445356863ebSDavid Greenman 	 * Now free any remaining pages. For internal objects, this also
446356863ebSDavid Greenman 	 * removes them from paging queues. Don't free wired pages, just
447356863ebSDavid Greenman 	 * remove them from the object.
448df8bae1dSRodney W. Grimes 	 */
449e4b7635dSDavid Greenman 	s = splvm();
450b18bfc3dSJohn Dyson 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
45195461b45SJohn Dyson #if !defined(MAX_PERF)
452a2f4a846SJohn Dyson 		if (p->busy || (p->flags & PG_BUSY))
453be6d5bfaSDavid Greenman 			printf("vm_object_terminate: freeing busy page\n");
45495461b45SJohn Dyson #endif
4550b10ba98SDavid Greenman 		if (p->wire_count == 0) {
456e69763a3SDoug Rabson 			vm_page_busy(p);
457df8bae1dSRodney W. Grimes 			vm_page_free(p);
458df8bae1dSRodney W. Grimes 			cnt.v_pfree++;
4590b10ba98SDavid Greenman 		} else {
460356863ebSDavid Greenman 			vm_page_busy(p);
4610b10ba98SDavid Greenman 			vm_page_remove(p);
4620b10ba98SDavid Greenman 		}
463df8bae1dSRodney W. Grimes 	}
464e4b7635dSDavid Greenman 	splx(s);
465bef608bdSJohn Dyson 
4662d8acc0fSJohn Dyson 	/*
4679fcfb650SDavid Greenman 	 * Let the pager know object is dead.
4689fcfb650SDavid Greenman 	 */
4699fcfb650SDavid Greenman 	vm_pager_deallocate(object);
4709fcfb650SDavid Greenman 
4719fcfb650SDavid Greenman 	/*
4720b10ba98SDavid Greenman 	 * Remove the object from the global object list.
4732d8acc0fSJohn Dyson 	 */
474996c772fSJohn Dyson 	simple_lock(&vm_object_list_lock);
475df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
476996c772fSJohn Dyson 	simple_unlock(&vm_object_list_lock);
4770b10ba98SDavid Greenman 
4780b10ba98SDavid Greenman 	wakeup(object);
4790b10ba98SDavid Greenman 
480df8bae1dSRodney W. Grimes 	/*
481df8bae1dSRodney W. Grimes 	 * Free the space for the object.
482df8bae1dSRodney W. Grimes 	 */
48399448ed1SJohn Dyson 	zfree(obj_zone, object);
48447221757SJohn Dyson }
485df8bae1dSRodney W. Grimes 
486df8bae1dSRodney W. Grimes /*
487df8bae1dSRodney W. Grimes  *	vm_object_page_clean
488df8bae1dSRodney W. Grimes  *
489df8bae1dSRodney W. Grimes  *	Clean all dirty pages in the specified range of object.
49026f9a767SRodney W. Grimes  *	Leaves page on whatever queue it is currently on.
49126f9a767SRodney W. Grimes  *
49226f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
49326f9a767SRodney W. Grimes  *
49426f9a767SRodney W. Grimes  *	The object must be locked.
49526f9a767SRodney W. Grimes  */
496f6b04d2bSDavid Greenman 
497f6b04d2bSDavid Greenman void
4988f9110f6SJohn Dyson vm_object_page_clean(object, start, end, flags)
499f6b04d2bSDavid Greenman 	vm_object_t object;
500a316d390SJohn Dyson 	vm_pindex_t start;
501a316d390SJohn Dyson 	vm_pindex_t end;
5028f9110f6SJohn Dyson 	int flags;
503f6b04d2bSDavid Greenman {
504bd7e5f99SJohn Dyson 	register vm_page_t p, np, tp;
505f6b04d2bSDavid Greenman 	register vm_offset_t tstart, tend;
506bd7e5f99SJohn Dyson 	vm_pindex_t pi;
507aef922f5SJohn Dyson 	int s;
50824a1cce3SDavid Greenman 	struct vnode *vp;
509aef922f5SJohn Dyson 	int runlen;
510bd7e5f99SJohn Dyson 	int maxf;
511bd7e5f99SJohn Dyson 	int chkb;
512bd7e5f99SJohn Dyson 	int maxb;
513bd7e5f99SJohn Dyson 	int i;
5148f9110f6SJohn Dyson 	int pagerflags;
515bd7e5f99SJohn Dyson 	vm_page_t maf[vm_pageout_page_count];
516bd7e5f99SJohn Dyson 	vm_page_t mab[vm_pageout_page_count];
517aef922f5SJohn Dyson 	vm_page_t ma[vm_pageout_page_count];
5182d8acc0fSJohn Dyson 	int curgeneration;
519f6b04d2bSDavid Greenman 
520aef922f5SJohn Dyson 	if (object->type != OBJT_VNODE ||
521aef922f5SJohn Dyson 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
522f6b04d2bSDavid Greenman 		return;
523f6b04d2bSDavid Greenman 
5248f9110f6SJohn Dyson 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0;
5258f9110f6SJohn Dyson 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
5268f9110f6SJohn Dyson 
52724a1cce3SDavid Greenman 	vp = object->handle;
52824a1cce3SDavid Greenman 
529069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_CLEANING);
53024a1cce3SDavid Greenman 
531f6b04d2bSDavid Greenman 	tstart = start;
532f6b04d2bSDavid Greenman 	if (end == 0) {
533f6b04d2bSDavid Greenman 		tend = object->size;
534f6b04d2bSDavid Greenman 	} else {
535f6b04d2bSDavid Greenman 		tend = end;
536f6b04d2bSDavid Greenman 	}
537eaf13dd7SJohn Dyson 
538eaf13dd7SJohn Dyson 	for(p = TAILQ_FIRST(&object->memq); p; p = TAILQ_NEXT(p, listq)) {
539e69763a3SDoug Rabson 		vm_page_flag_set(p, PG_CLEANCHK);
540eaf13dd7SJohn Dyson 		vm_page_protect(p, VM_PROT_READ);
541eaf13dd7SJohn Dyson 	}
542eaf13dd7SJohn Dyson 
543a316d390SJohn Dyson 	if ((tstart == 0) && (tend == object->size)) {
544069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
545ec4f9fb0SDavid Greenman 	}
546f6b04d2bSDavid Greenman 
547bd7e5f99SJohn Dyson rescan:
5482d8acc0fSJohn Dyson 	curgeneration = object->generation;
5492d8acc0fSJohn Dyson 
550b18bfc3dSJohn Dyson 	for(p = TAILQ_FIRST(&object->memq); p; p = np) {
551b18bfc3dSJohn Dyson 		np = TAILQ_NEXT(p, listq);
552bd7e5f99SJohn Dyson 
553bd7e5f99SJohn Dyson 		pi = p->pindex;
554bd7e5f99SJohn Dyson 		if (((p->flags & PG_CLEANCHK) == 0) ||
555bd7e5f99SJohn Dyson 			(pi < tstart) || (pi >= tend) ||
5565070c7f8SJohn Dyson 			(p->valid == 0) ||
5575070c7f8SJohn Dyson 			((p->queue - p->pc) == PQ_CACHE)) {
558e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
559aef922f5SJohn Dyson 			continue;
560f6b04d2bSDavid Greenman 		}
561f6b04d2bSDavid Greenman 
562bd7e5f99SJohn Dyson 		vm_page_test_dirty(p);
563bd7e5f99SJohn Dyson 		if ((p->dirty & p->valid) == 0) {
564e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
565bd7e5f99SJohn Dyson 			continue;
566bd7e5f99SJohn Dyson 		}
567ec4f9fb0SDavid Greenman 
568b18bfc3dSJohn Dyson 		s = splvm();
5692d8acc0fSJohn Dyson 		while ((p->flags & PG_BUSY) || p->busy) {
570e69763a3SDoug Rabson 			vm_page_flag_set(p, PG_WANTED | PG_REFERENCED);
571aef922f5SJohn Dyson 			tsleep(p, PVM, "vpcwai", 0);
5722d8acc0fSJohn Dyson 			if (object->generation != curgeneration) {
573f6b04d2bSDavid Greenman 				splx(s);
574bd7e5f99SJohn Dyson 				goto rescan;
575f6b04d2bSDavid Greenman 			}
5762d8acc0fSJohn Dyson 		}
577f6b04d2bSDavid Greenman 
578bd7e5f99SJohn Dyson 		maxf = 0;
579bd7e5f99SJohn Dyson 		for(i=1;i<vm_pageout_page_count;i++) {
580bd7e5f99SJohn Dyson 			if (tp = vm_page_lookup(object, pi + i)) {
581bd7e5f99SJohn Dyson 				if ((tp->flags & PG_BUSY) ||
582ffc82b0aSJohn Dyson 					(tp->flags & PG_CLEANCHK) == 0 ||
583ffc82b0aSJohn Dyson 					(tp->busy != 0))
584bd7e5f99SJohn Dyson 					break;
5855070c7f8SJohn Dyson 				if((tp->queue - tp->pc) == PQ_CACHE) {
586e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
5873077a9c2SJohn Dyson 					break;
5883077a9c2SJohn Dyson 				}
589bd7e5f99SJohn Dyson 				vm_page_test_dirty(tp);
590bd7e5f99SJohn Dyson 				if ((tp->dirty & tp->valid) == 0) {
591e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
592bd7e5f99SJohn Dyson 					break;
593bd7e5f99SJohn Dyson 				}
594bd7e5f99SJohn Dyson 				maf[ i - 1 ] = tp;
595bd7e5f99SJohn Dyson 				maxf++;
596bd7e5f99SJohn Dyson 				continue;
597bd7e5f99SJohn Dyson 			}
598bd7e5f99SJohn Dyson 			break;
599bd7e5f99SJohn Dyson 		}
600aef922f5SJohn Dyson 
601bd7e5f99SJohn Dyson 		maxb = 0;
602bd7e5f99SJohn Dyson 		chkb = vm_pageout_page_count -  maxf;
603bd7e5f99SJohn Dyson 		if (chkb) {
604bd7e5f99SJohn Dyson 			for(i = 1; i < chkb;i++) {
605bd7e5f99SJohn Dyson 				if (tp = vm_page_lookup(object, pi - i)) {
606bd7e5f99SJohn Dyson 					if ((tp->flags & PG_BUSY) ||
607ffc82b0aSJohn Dyson 						(tp->flags & PG_CLEANCHK) == 0 ||
608ffc82b0aSJohn Dyson 						(tp->busy != 0))
609bd7e5f99SJohn Dyson 						break;
6105070c7f8SJohn Dyson 					if((tp->queue - tp->pc) == PQ_CACHE) {
611e69763a3SDoug Rabson 						vm_page_flag_clear(tp, PG_CLEANCHK);
6123077a9c2SJohn Dyson 						break;
6133077a9c2SJohn Dyson 					}
614bd7e5f99SJohn Dyson 					vm_page_test_dirty(tp);
615bd7e5f99SJohn Dyson 					if ((tp->dirty & tp->valid) == 0) {
616e69763a3SDoug Rabson 						vm_page_flag_clear(tp, PG_CLEANCHK);
617bd7e5f99SJohn Dyson 						break;
618bd7e5f99SJohn Dyson 					}
619bd7e5f99SJohn Dyson 					mab[ i - 1 ] = tp;
620bd7e5f99SJohn Dyson 					maxb++;
621bd7e5f99SJohn Dyson 					continue;
622bd7e5f99SJohn Dyson 				}
623bd7e5f99SJohn Dyson 				break;
624bd7e5f99SJohn Dyson 			}
625bd7e5f99SJohn Dyson 		}
626bd7e5f99SJohn Dyson 
627bd7e5f99SJohn Dyson 		for(i=0;i<maxb;i++) {
628bd7e5f99SJohn Dyson 			int index = (maxb - i) - 1;
629bd7e5f99SJohn Dyson 			ma[index] = mab[i];
630e69763a3SDoug Rabson 			vm_page_flag_clear(ma[index], PG_CLEANCHK);
631bd7e5f99SJohn Dyson 		}
632e69763a3SDoug Rabson 		vm_page_flag_clear(p, PG_CLEANCHK);
633bd7e5f99SJohn Dyson 		ma[maxb] = p;
634bd7e5f99SJohn Dyson 		for(i=0;i<maxf;i++) {
635bd7e5f99SJohn Dyson 			int index = (maxb + i) + 1;
636bd7e5f99SJohn Dyson 			ma[index] = maf[i];
637e69763a3SDoug Rabson 			vm_page_flag_clear(ma[index], PG_CLEANCHK);
638f6b04d2bSDavid Greenman 		}
639bd7e5f99SJohn Dyson 		runlen = maxb + maxf + 1;
640cf2819ccSJohn Dyson 
641f35329acSJohn Dyson 		splx(s);
6428f9110f6SJohn Dyson 		vm_pageout_flush(ma, runlen, pagerflags);
643cf2819ccSJohn Dyson 		for (i = 0; i<runlen; i++) {
644cf2819ccSJohn Dyson 			if (ma[i]->valid & ma[i]->dirty) {
645cf2819ccSJohn Dyson 				vm_page_protect(ma[i], VM_PROT_READ);
646e69763a3SDoug Rabson 				vm_page_flag_set(ma[i], PG_CLEANCHK);
647cf2819ccSJohn Dyson 			}
648cf2819ccSJohn Dyson 		}
6492d8acc0fSJohn Dyson 		if (object->generation != curgeneration)
650bd7e5f99SJohn Dyson 			goto rescan;
651f6b04d2bSDavid Greenman 	}
652aef922f5SJohn Dyson 
653cf2819ccSJohn Dyson 	VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
654aef922f5SJohn Dyson 
655069e9bc1SDoug Rabson 	vm_object_clear_flag(object, 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 
708069e9bc1SDoug Rabson 	vm_object_clear_flag(object, 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))
759069e9bc1SDoug Rabson 		vm_object_clear_flag(object, 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 	vm_pindex_t end, tpindex;
7756e20a165SJohn Dyson 	vm_object_t tobject;
776867a482dSJohn Dyson 	vm_page_t m;
777867a482dSJohn Dyson 
778867a482dSJohn Dyson 	if (object == NULL)
779867a482dSJohn Dyson 		return;
780867a482dSJohn Dyson 
781867a482dSJohn Dyson 	end = pindex + count;
782867a482dSJohn Dyson 
783867a482dSJohn Dyson 	for (; pindex < end; pindex += 1) {
7846e20a165SJohn Dyson 
7856e20a165SJohn Dyson relookup:
7866e20a165SJohn Dyson 		tobject = object;
7876e20a165SJohn Dyson 		tpindex = pindex;
7886e20a165SJohn Dyson shadowlookup:
7896e20a165SJohn Dyson 		m = vm_page_lookup(tobject, tpindex);
7906e20a165SJohn Dyson 		if (m == NULL) {
7916e20a165SJohn Dyson 			if (tobject->type != OBJT_DEFAULT) {
7926e20a165SJohn Dyson 				continue;
7936e20a165SJohn Dyson 			}
7946e20a165SJohn Dyson 
7956e20a165SJohn Dyson 			tobject = tobject->backing_object;
7966e20a165SJohn Dyson 			if ((tobject == NULL) || (tobject->ref_count != 1)) {
7976e20a165SJohn Dyson 				continue;
7986e20a165SJohn Dyson 			}
7996e20a165SJohn Dyson 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
8006e20a165SJohn Dyson 			goto shadowlookup;
8016e20a165SJohn Dyson 		}
802867a482dSJohn Dyson 
803867a482dSJohn Dyson 		/*
804867a482dSJohn Dyson 		 * If the page is busy or not in a normal active state,
805867a482dSJohn Dyson 		 * we skip it.  Things can break if we mess with pages
806867a482dSJohn Dyson 		 * in any of the below states.
807867a482dSJohn Dyson 		 */
8086e20a165SJohn Dyson 		if (m->hold_count || m->wire_count ||
8096e20a165SJohn Dyson 			m->valid != VM_PAGE_BITS_ALL) {
810867a482dSJohn Dyson 			continue;
8116e20a165SJohn Dyson 		}
8126e20a165SJohn Dyson 
813ffc82b0aSJohn Dyson  		if (vm_page_sleep(m, "madvpo", &m->busy))
8146e20a165SJohn Dyson   			goto relookup;
815867a482dSJohn Dyson 
816867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
817867a482dSJohn Dyson 			vm_page_activate(m);
8186e20a165SJohn Dyson 		} else if (advise == MADV_DONTNEED) {
819867a482dSJohn Dyson 			vm_page_deactivate(m);
8200a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
8216e20a165SJohn Dyson 			pmap_clear_modify(VM_PAGE_TO_PHYS(m));
8226e20a165SJohn Dyson 			m->dirty = 0;
8230a47b48bSJohn Dyson 			/*
8246e20a165SJohn Dyson 			 * Force a demand zero if attempt to read from swap.
8256e20a165SJohn Dyson 			 * We currently don't handle vnode files correctly,
8266e20a165SJohn Dyson 			 * and will reread stale contents unnecessarily.
8270a47b48bSJohn Dyson 			 */
8280a47b48bSJohn Dyson 			if (object->type == OBJT_SWAP)
8296e20a165SJohn Dyson 				swap_pager_dmzspace(tobject, m->pindex, 1);
830867a482dSJohn Dyson 		}
831867a482dSJohn Dyson 	}
832867a482dSJohn Dyson }
833867a482dSJohn Dyson 
834867a482dSJohn Dyson /*
835df8bae1dSRodney W. Grimes  *	vm_object_shadow:
836df8bae1dSRodney W. Grimes  *
837df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
838df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
839df8bae1dSRodney W. Grimes  *	object reference is deallocated.
840df8bae1dSRodney W. Grimes  *
841df8bae1dSRodney W. Grimes  *	The new object and offset into that object
842df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
843df8bae1dSRodney W. Grimes  */
844df8bae1dSRodney W. Grimes 
84526f9a767SRodney W. Grimes void
84626f9a767SRodney W. Grimes vm_object_shadow(object, offset, length)
847df8bae1dSRodney W. Grimes 	vm_object_t *object;	/* IN/OUT */
848a316d390SJohn Dyson 	vm_ooffset_t *offset;	/* IN/OUT */
849df8bae1dSRodney W. Grimes 	vm_size_t length;
850df8bae1dSRodney W. Grimes {
851df8bae1dSRodney W. Grimes 	register vm_object_t source;
852df8bae1dSRodney W. Grimes 	register vm_object_t result;
853df8bae1dSRodney W. Grimes 
854df8bae1dSRodney W. Grimes 	source = *object;
855df8bae1dSRodney W. Grimes 
856df8bae1dSRodney W. Grimes 	/*
857df8bae1dSRodney W. Grimes 	 * Allocate a new object with the given length
858df8bae1dSRodney W. Grimes 	 */
859df8bae1dSRodney W. Grimes 
86024a1cce3SDavid Greenman 	if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
861df8bae1dSRodney W. Grimes 		panic("vm_object_shadow: no object for shadowing");
862df8bae1dSRodney W. Grimes 
863df8bae1dSRodney W. Grimes 	/*
8640d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
8650d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
8660d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
8670d94caffSDavid Greenman 	 * of reference count.
868df8bae1dSRodney W. Grimes 	 */
86924a1cce3SDavid Greenman 	result->backing_object = source;
870de5f6a77SJohn Dyson 	if (source) {
871de5f6a77SJohn Dyson 		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
872069e9bc1SDoug Rabson 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
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 		}
929e69763a3SDoug Rabson 		vm_page_busy(p);
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 
1041069e9bc1SDoug Rabson 			vm_object_set_flag(backing_object, 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;
1054e69763a3SDoug Rabson 				vm_page_busy(p);
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) {
1090d474eaaaSDoug Rabson 				vm_object_pip_add(backing_object, 1);
109124a1cce3SDavid Greenman 				if (object->type == OBJT_SWAP) {
1092d474eaaaSDoug Rabson 					vm_object_pip_add(object, 1);
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),
1103c0877f10SJohn Dyson 					    OFF_TO_IDX(object->backing_object_offset), TRUE);
1104f919ebdeSDavid Greenman 					vm_object_pip_wakeup(object);
110526f9a767SRodney W. Grimes 				} else {
1106d474eaaaSDoug Rabson 					vm_object_pip_add(object, 1);
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;
1204e69763a3SDoug Rabson 				vm_page_busy(p);
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) {
1220e69763a3SDoug Rabson 						vm_page_wakeup(p);
1221eaf13dd7SJohn Dyson 						return;
1222eaf13dd7SJohn Dyson 					}
1223eaf13dd7SJohn Dyson 
1224e69763a3SDoug Rabson 					vm_page_busy(pp);
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 						 */
1231e69763a3SDoug Rabson 						vm_page_wakeup(pp);
1232e69763a3SDoug Rabson 						vm_page_wakeup(p);
1233df8bae1dSRodney W. Grimes 						return;
1234df8bae1dSRodney W. Grimes 					}
1235e69763a3SDoug Rabson 					vm_page_wakeup(pp);
1236df8bae1dSRodney W. Grimes 				}
1237e69763a3SDoug Rabson 				vm_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;
1295f5ef029eSPoul-Henning Kamp 	int 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 
1302d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
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);
1311ce65e68cSDavid Greenman 					if (!clean_only)
1312bd7e5f99SJohn Dyson 						p->valid = 0;
13130d94caffSDavid Greenman 					continue;
13140d94caffSDavid Greenman 				}
13150891ef4cSJohn Dyson 
1316b18bfc3dSJohn Dyson 				/*
1317b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1318b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1319b18bfc3dSJohn Dyson 				 */
1320ffc82b0aSJohn Dyson 
1321ffc82b0aSJohn Dyson  				if (vm_page_sleep(p, "vmopar", &p->busy))
132226f9a767SRodney W. Grimes  					goto again;
13230891ef4cSJohn Dyson 
13248f9110f6SJohn Dyson 				if (clean_only && p->valid) {
13257c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
13267c1f6cedSDavid Greenman 					if (p->valid & p->dirty)
13277c1f6cedSDavid Greenman 						continue;
13287c1f6cedSDavid Greenman 				}
1329eaf13dd7SJohn Dyson 
1330e69763a3SDoug Rabson 				vm_page_busy(p);
1331f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
1332df8bae1dSRodney W. Grimes 				vm_page_free(p);
133326f9a767SRodney W. Grimes 			}
133426f9a767SRodney W. Grimes 		}
133526f9a767SRodney W. Grimes 	} else {
133626f9a767SRodney W. Grimes 		while (size > 0) {
1337bd7e5f99SJohn Dyson 			if ((p = vm_page_lookup(object, start)) != 0) {
1338eaf13dd7SJohn Dyson 
1339bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1340bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1341ce65e68cSDavid Greenman 					if (!clean_only)
1342ce65e68cSDavid Greenman 						p->valid = 0;
1343bd7e5f99SJohn Dyson 					start += 1;
1344bd7e5f99SJohn Dyson 					size -= 1;
1345bd7e5f99SJohn Dyson 					continue;
13460d94caffSDavid Greenman 				}
1347eaf13dd7SJohn Dyson 
1348b18bfc3dSJohn Dyson 				/*
1349b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1350b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1351b18bfc3dSJohn Dyson 				 */
1352ffc82b0aSJohn Dyson  				if (vm_page_sleep(p, "vmopar", &p->busy))
135326f9a767SRodney W. Grimes 					goto again;
1354eaf13dd7SJohn Dyson 
13558f9110f6SJohn Dyson 				if (clean_only && p->valid) {
13567c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
1357bd7e5f99SJohn Dyson 					if (p->valid & p->dirty) {
1358bd7e5f99SJohn Dyson 						start += 1;
1359bd7e5f99SJohn Dyson 						size -= 1;
13607c1f6cedSDavid Greenman 						continue;
13617c1f6cedSDavid Greenman 					}
1362bd7e5f99SJohn Dyson 				}
1363eaf13dd7SJohn Dyson 
1364e69763a3SDoug Rabson 				vm_page_busy(p);
1365f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
136626f9a767SRodney W. Grimes 				vm_page_free(p);
136726f9a767SRodney W. Grimes 			}
1368a316d390SJohn Dyson 			start += 1;
1369a316d390SJohn Dyson 			size -= 1;
1370df8bae1dSRodney W. Grimes 		}
1371df8bae1dSRodney W. Grimes 	}
1372f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
1373c0503609SDavid Greenman }
1374df8bae1dSRodney W. Grimes 
1375df8bae1dSRodney W. Grimes /*
1376df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1377df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1378df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1379df8bae1dSRodney W. Grimes  *
1380df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1381df8bae1dSRodney W. Grimes  *
1382df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1383df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1384df8bae1dSRodney W. Grimes  *
1385df8bae1dSRodney W. Grimes  *	Parameters:
1386df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1387df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1388df8bae1dSRodney W. Grimes  *		next_object	Second object into coalesce
1389df8bae1dSRodney W. Grimes  *		next_offset	Offset into next_object
1390df8bae1dSRodney W. Grimes  *
1391df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
1392df8bae1dSRodney W. Grimes  *		next_size	Size of reference to next_object
1393df8bae1dSRodney W. Grimes  *
1394df8bae1dSRodney W. Grimes  *	Conditions:
1395df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1396df8bae1dSRodney W. Grimes  */
13970d94caffSDavid Greenman boolean_t
1398a316d390SJohn Dyson vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size)
1399df8bae1dSRodney W. Grimes 	register vm_object_t prev_object;
1400a316d390SJohn Dyson 	vm_pindex_t prev_pindex;
1401df8bae1dSRodney W. Grimes 	vm_size_t prev_size, next_size;
1402df8bae1dSRodney W. Grimes {
1403df8bae1dSRodney W. Grimes 	vm_size_t newsize;
1404df8bae1dSRodney W. Grimes 
1405df8bae1dSRodney W. Grimes 	if (prev_object == NULL) {
1406df8bae1dSRodney W. Grimes 		return (TRUE);
1407df8bae1dSRodney W. Grimes 	}
1408df8bae1dSRodney W. Grimes 
140930dcfc09SJohn Dyson 	if (prev_object->type != OBJT_DEFAULT) {
141030dcfc09SJohn Dyson 		return (FALSE);
141130dcfc09SJohn Dyson 	}
141230dcfc09SJohn Dyson 
1413df8bae1dSRodney W. Grimes 	/*
1414df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1415df8bae1dSRodney W. Grimes 	 */
1416df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1417df8bae1dSRodney W. Grimes 
1418df8bae1dSRodney W. Grimes 	/*
14190d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
14200d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
14210d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1422df8bae1dSRodney W. Grimes 	 */
1423df8bae1dSRodney W. Grimes 
14248cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
1425df8bae1dSRodney W. Grimes 		return (FALSE);
1426df8bae1dSRodney W. Grimes 	}
1427a316d390SJohn Dyson 
1428a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
1429a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
14308cc7e047SJohn Dyson 
14318cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
14328cc7e047SJohn Dyson 	    (prev_object->size != prev_pindex + prev_size)) {
14338cc7e047SJohn Dyson 		return (FALSE);
14348cc7e047SJohn Dyson 	}
14358cc7e047SJohn Dyson 
1436df8bae1dSRodney W. Grimes 	/*
14370d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
14380d94caffSDavid Greenman 	 * deallocation.
1439df8bae1dSRodney W. Grimes 	 */
1440df8bae1dSRodney W. Grimes 
1441df8bae1dSRodney W. Grimes 	vm_object_page_remove(prev_object,
1442a316d390SJohn Dyson 	    prev_pindex + prev_size,
1443a316d390SJohn Dyson 	    prev_pindex + prev_size + next_size, FALSE);
1444df8bae1dSRodney W. Grimes 
1445df8bae1dSRodney W. Grimes 	/*
1446df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1447df8bae1dSRodney W. Grimes 	 */
1448a316d390SJohn Dyson 	newsize = prev_pindex + prev_size + next_size;
1449df8bae1dSRodney W. Grimes 	if (newsize > prev_object->size)
1450df8bae1dSRodney W. Grimes 		prev_object->size = newsize;
1451df8bae1dSRodney W. Grimes 
1452df8bae1dSRodney W. Grimes 	return (TRUE);
1453df8bae1dSRodney W. Grimes }
1454df8bae1dSRodney W. Grimes 
1455c7c34a24SBruce Evans #include "opt_ddb.h"
1456c3cb3e12SDavid Greenman #ifdef DDB
1457c7c34a24SBruce Evans #include <sys/kernel.h>
1458c7c34a24SBruce Evans 
1459c7c34a24SBruce Evans #include <machine/cons.h>
1460c7c34a24SBruce Evans 
1461c7c34a24SBruce Evans #include <ddb/ddb.h>
1462c7c34a24SBruce Evans 
1463c7c34a24SBruce Evans static int	_vm_object_in_map __P((vm_map_t map, vm_object_t object,
1464c7c34a24SBruce Evans 				       vm_map_entry_t entry));
1465c7c34a24SBruce Evans static int	vm_object_in_map __P((vm_object_t object));
1466c3cb3e12SDavid Greenman 
1467cac597e4SBruce Evans static int
1468a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry)
1469a1f6d91cSDavid Greenman 	vm_map_t map;
1470a1f6d91cSDavid Greenman 	vm_object_t object;
1471a1f6d91cSDavid Greenman 	vm_map_entry_t entry;
1472a1f6d91cSDavid Greenman {
1473a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1474a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1475a1f6d91cSDavid Greenman 	vm_object_t obj;
1476a1f6d91cSDavid Greenman 	int entcount;
1477a1f6d91cSDavid Greenman 
1478a1f6d91cSDavid Greenman 	if (map == 0)
1479a1f6d91cSDavid Greenman 		return 0;
1480a1f6d91cSDavid Greenman 
1481a1f6d91cSDavid Greenman 	if (entry == 0) {
1482a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1483a1f6d91cSDavid Greenman 		entcount = map->nentries;
1484a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1485a1f6d91cSDavid Greenman 			if( _vm_object_in_map(map, object, tmpe)) {
1486a1f6d91cSDavid Greenman 				return 1;
1487a1f6d91cSDavid Greenman 			}
1488a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1489a1f6d91cSDavid Greenman 		}
1490afa07f7eSJohn Dyson 	} else if (entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) {
1491a1f6d91cSDavid Greenman 		tmpm = entry->object.share_map;
1492a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1493a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1494a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1495a1f6d91cSDavid Greenman 			if( _vm_object_in_map(tmpm, object, tmpe)) {
1496a1f6d91cSDavid Greenman 				return 1;
1497a1f6d91cSDavid Greenman 			}
1498a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1499a1f6d91cSDavid Greenman 		}
1500a1f6d91cSDavid Greenman 	} else if (obj = entry->object.vm_object) {
150124a1cce3SDavid Greenman 		for(; obj; obj=obj->backing_object)
1502a1f6d91cSDavid Greenman 			if( obj == object) {
1503a1f6d91cSDavid Greenman 				return 1;
1504a1f6d91cSDavid Greenman 			}
1505a1f6d91cSDavid Greenman 	}
1506a1f6d91cSDavid Greenman 	return 0;
1507a1f6d91cSDavid Greenman }
1508a1f6d91cSDavid Greenman 
1509cac597e4SBruce Evans static int
1510a1f6d91cSDavid Greenman vm_object_in_map( object)
1511a1f6d91cSDavid Greenman 	vm_object_t object;
1512a1f6d91cSDavid Greenman {
1513a1f6d91cSDavid Greenman 	struct proc *p;
15141b67ec6dSJeffrey Hsu 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1515a1f6d91cSDavid Greenman 		if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1516a1f6d91cSDavid Greenman 			continue;
1517a1f6d91cSDavid Greenman 		if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1518a1f6d91cSDavid Greenman 			return 1;
1519a1f6d91cSDavid Greenman 	}
1520a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kernel_map, object, 0))
1521a1f6d91cSDavid Greenman 		return 1;
1522a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kmem_map, object, 0))
1523a1f6d91cSDavid Greenman 		return 1;
1524a1f6d91cSDavid Greenman 	if( _vm_object_in_map( pager_map, object, 0))
1525a1f6d91cSDavid Greenman 		return 1;
1526a1f6d91cSDavid Greenman 	if( _vm_object_in_map( buffer_map, object, 0))
1527a1f6d91cSDavid Greenman 		return 1;
1528a1f6d91cSDavid Greenman 	if( _vm_object_in_map( io_map, object, 0))
1529a1f6d91cSDavid Greenman 		return 1;
1530a1f6d91cSDavid Greenman 	if( _vm_object_in_map( phys_map, object, 0))
1531a1f6d91cSDavid Greenman 		return 1;
1532a1f6d91cSDavid Greenman 	if( _vm_object_in_map( mb_map, object, 0))
1533a1f6d91cSDavid Greenman 		return 1;
1534a1f6d91cSDavid Greenman 	if( _vm_object_in_map( u_map, object, 0))
1535a1f6d91cSDavid Greenman 		return 1;
1536a1f6d91cSDavid Greenman 	return 0;
1537a1f6d91cSDavid Greenman }
1538a1f6d91cSDavid Greenman 
1539c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
1540f708ef1bSPoul-Henning Kamp {
1541a1f6d91cSDavid Greenman 	vm_object_t object;
1542a1f6d91cSDavid Greenman 
1543a1f6d91cSDavid Greenman 	/*
1544a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
1545a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
1546a1f6d91cSDavid Greenman 	 */
1547b18bfc3dSJohn Dyson 	for (object = TAILQ_FIRST(&vm_object_list);
1548a1f6d91cSDavid Greenman 			object != NULL;
1549b18bfc3dSJohn Dyson 			object = TAILQ_NEXT(object, object_list)) {
155024a1cce3SDavid Greenman 		if (object->handle == NULL &&
155124a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1552a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
1553c7c34a24SBruce Evans 				db_printf("vmochk: internal obj has zero ref count: %d\n",
1554a1f6d91cSDavid Greenman 					object->size);
1555a1f6d91cSDavid Greenman 			}
1556a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
1557fc62ef1fSBruce Evans 				db_printf(
1558fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
1559fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
1560fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
1561fc62ef1fSBruce Evans 				    (u_long)object->size,
1562fc62ef1fSBruce Evans 				    (void *)object->backing_object);
1563a1f6d91cSDavid Greenman 			}
1564a1f6d91cSDavid Greenman 		}
1565a1f6d91cSDavid Greenman 	}
1566a1f6d91cSDavid Greenman }
1567a1f6d91cSDavid Greenman 
156826f9a767SRodney W. Grimes /*
1569df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
1570df8bae1dSRodney W. Grimes  */
1571c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
1572df8bae1dSRodney W. Grimes {
1573c7c34a24SBruce Evans 	/* XXX convert args. */
1574c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
1575c7c34a24SBruce Evans 	boolean_t full = have_addr;
1576c7c34a24SBruce Evans 
1577df8bae1dSRodney W. Grimes 	register vm_page_t p;
1578df8bae1dSRodney W. Grimes 
1579c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
1580c7c34a24SBruce Evans #define	count	was_count
1581c7c34a24SBruce Evans 
1582df8bae1dSRodney W. Grimes 	register int count;
1583df8bae1dSRodney W. Grimes 
1584df8bae1dSRodney W. Grimes 	if (object == NULL)
1585df8bae1dSRodney W. Grimes 		return;
1586df8bae1dSRodney W. Grimes 
1587eb95adefSBruce Evans 	db_iprintf(
1588eb95adefSBruce Evans 	    "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
1589eb95adefSBruce Evans 	    object, (int)object->type, (u_long)object->size,
1590eb95adefSBruce Evans 	    object->resident_page_count, object->ref_count, object->flags);
1591eb95adefSBruce Evans 	/*
1592eb95adefSBruce Evans 	 * XXX no %qd in kernel.  Truncate object->paging_offset and
1593eb95adefSBruce Evans 	 * object->backing_object_offset.
1594eb95adefSBruce Evans 	 */
1595eb95adefSBruce Evans 	db_iprintf(" sref=%d, offset=0x%lx, backing_object(%d)=(%p)+0x%lx\n",
1596eb95adefSBruce Evans 	    object->shadow_count, (long)object->paging_offset,
1597eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
1598eb95adefSBruce Evans 	    object->backing_object, (long)object->backing_object_offset);
1599df8bae1dSRodney W. Grimes 
1600df8bae1dSRodney W. Grimes 	if (!full)
1601df8bae1dSRodney W. Grimes 		return;
1602df8bae1dSRodney W. Grimes 
1603c7c34a24SBruce Evans 	db_indent += 2;
1604df8bae1dSRodney W. Grimes 	count = 0;
1605b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = TAILQ_NEXT(p, listq)) {
1606df8bae1dSRodney W. Grimes 		if (count == 0)
1607c7c34a24SBruce Evans 			db_iprintf("memory:=");
1608df8bae1dSRodney W. Grimes 		else if (count == 6) {
1609c7c34a24SBruce Evans 			db_printf("\n");
1610c7c34a24SBruce Evans 			db_iprintf(" ...");
1611df8bae1dSRodney W. Grimes 			count = 0;
1612df8bae1dSRodney W. Grimes 		} else
1613c7c34a24SBruce Evans 			db_printf(",");
1614df8bae1dSRodney W. Grimes 		count++;
1615df8bae1dSRodney W. Grimes 
1616c7c34a24SBruce Evans 		db_printf("(off=0x%lx,page=0x%lx)",
1617a316d390SJohn Dyson 		    (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
1618df8bae1dSRodney W. Grimes 	}
1619df8bae1dSRodney W. Grimes 	if (count != 0)
1620c7c34a24SBruce Evans 		db_printf("\n");
1621c7c34a24SBruce Evans 	db_indent -= 2;
1622df8bae1dSRodney W. Grimes }
16235070c7f8SJohn Dyson 
1624c7c34a24SBruce Evans /* XXX. */
1625c7c34a24SBruce Evans #undef count
1626c7c34a24SBruce Evans 
1627c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
16285070c7f8SJohn Dyson void
1629c7c34a24SBruce Evans vm_object_print(addr, have_addr, count, modif)
1630ecbb00a2SDoug Rabson         /* db_expr_t */ long addr;
1631c7c34a24SBruce Evans 	boolean_t have_addr;
1632ecbb00a2SDoug Rabson 	/* db_expr_t */ long count;
1633c7c34a24SBruce Evans 	char *modif;
1634c7c34a24SBruce Evans {
1635c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
1636c7c34a24SBruce Evans }
1637c7c34a24SBruce Evans 
1638c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
16395070c7f8SJohn Dyson {
16405070c7f8SJohn Dyson 	vm_object_t object;
16415070c7f8SJohn Dyson 	int nl = 0;
16425070c7f8SJohn Dyson 	int c;
16435070c7f8SJohn Dyson 	for (object = TAILQ_FIRST(&vm_object_list);
16445070c7f8SJohn Dyson 			object != NULL;
16455070c7f8SJohn Dyson 			object = TAILQ_NEXT(object, object_list)) {
16465070c7f8SJohn Dyson 		vm_pindex_t idx, fidx;
16475070c7f8SJohn Dyson 		vm_pindex_t osize;
16485070c7f8SJohn Dyson 		vm_offset_t pa = -1, padiff;
16495070c7f8SJohn Dyson 		int rcount;
16505070c7f8SJohn Dyson 		vm_page_t m;
16515070c7f8SJohn Dyson 
1652fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
16535070c7f8SJohn Dyson 		if ( nl > 18) {
16545070c7f8SJohn Dyson 			c = cngetc();
16555070c7f8SJohn Dyson 			if (c != ' ')
16565070c7f8SJohn Dyson 				return;
16575070c7f8SJohn Dyson 			nl = 0;
16585070c7f8SJohn Dyson 		}
16595070c7f8SJohn Dyson 		nl++;
16605070c7f8SJohn Dyson 		rcount = 0;
16615070c7f8SJohn Dyson 		fidx = 0;
16625070c7f8SJohn Dyson 		osize = object->size;
16635070c7f8SJohn Dyson 		if (osize > 128)
16645070c7f8SJohn Dyson 			osize = 128;
16655070c7f8SJohn Dyson 		for(idx=0;idx<osize;idx++) {
16665070c7f8SJohn Dyson 			m = vm_page_lookup(object, idx);
16675070c7f8SJohn Dyson 			if (m == NULL) {
16685070c7f8SJohn Dyson 				if (rcount) {
16695070c7f8SJohn Dyson 					db_printf(" index(%d)run(%d)pa(0x%x)\n",
16705070c7f8SJohn Dyson 						fidx, rcount, pa);
16715070c7f8SJohn Dyson 					if ( nl > 18) {
16725070c7f8SJohn Dyson 						c = cngetc();
16735070c7f8SJohn Dyson 						if (c != ' ')
16745070c7f8SJohn Dyson 							return;
16755070c7f8SJohn Dyson 						nl = 0;
16765070c7f8SJohn Dyson 					}
16775070c7f8SJohn Dyson 					nl++;
16785070c7f8SJohn Dyson 					rcount = 0;
16795070c7f8SJohn Dyson 				}
16805070c7f8SJohn Dyson 				continue;
16815070c7f8SJohn Dyson 			}
16825070c7f8SJohn Dyson 
16835070c7f8SJohn Dyson 
16845070c7f8SJohn Dyson 			if (rcount &&
16855070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
16865070c7f8SJohn Dyson 				++rcount;
16875070c7f8SJohn Dyson 				continue;
16885070c7f8SJohn Dyson 			}
16895070c7f8SJohn Dyson 			if (rcount) {
16905070c7f8SJohn Dyson 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
16915070c7f8SJohn Dyson 				padiff >>= PAGE_SHIFT;
16925070c7f8SJohn Dyson 				padiff &= PQ_L2_MASK;
16935070c7f8SJohn Dyson 				if (padiff == 0) {
16945070c7f8SJohn Dyson 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
16955070c7f8SJohn Dyson 					++rcount;
16965070c7f8SJohn Dyson 					continue;
16975070c7f8SJohn Dyson 				}
16985070c7f8SJohn Dyson 				db_printf(" index(%d)run(%d)pa(0x%x)", fidx, rcount, pa);
16995070c7f8SJohn Dyson 				db_printf("pd(%d)\n", padiff);
17005070c7f8SJohn Dyson 				if ( nl > 18) {
17015070c7f8SJohn Dyson 					c = cngetc();
17025070c7f8SJohn Dyson 					if (c != ' ')
17035070c7f8SJohn Dyson 						return;
17045070c7f8SJohn Dyson 					nl = 0;
17055070c7f8SJohn Dyson 				}
17065070c7f8SJohn Dyson 				nl++;
17075070c7f8SJohn Dyson 			}
17085070c7f8SJohn Dyson 			fidx = idx;
17095070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
17105070c7f8SJohn Dyson 			rcount = 1;
17115070c7f8SJohn Dyson 		}
17125070c7f8SJohn Dyson 		if (rcount) {
17135070c7f8SJohn Dyson 			db_printf(" index(%d)run(%d)pa(0x%x)\n", fidx, rcount, pa);
17145070c7f8SJohn Dyson 			if ( nl > 18) {
17155070c7f8SJohn Dyson 				c = cngetc();
17165070c7f8SJohn Dyson 				if (c != ' ')
17175070c7f8SJohn Dyson 					return;
17185070c7f8SJohn Dyson 				nl = 0;
17195070c7f8SJohn Dyson 			}
17205070c7f8SJohn Dyson 			nl++;
17215070c7f8SJohn Dyson 		}
17225070c7f8SJohn Dyson 	}
17235070c7f8SJohn Dyson }
1724c3cb3e12SDavid Greenman #endif /* DDB */
1725