xref: /freebsd/sys/vm/vm_object.c (revision 9fdfe602fc5fe4a9fbbff8589022a8bc068aeeab)
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  *
649fdfe602SMatthew Dillon  * $Id: vm_object.c,v 1.145 1999/02/07 08:44:53 dillon 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;
1371c7c3c6aSMatthew Dillon static int object_hash_rand;
13899448ed1SJohn Dyson #define VM_OBJECTS_INIT 256
139303b270bSEivind Eklund static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
1401c7c3c6aSMatthew Dillon #if 0
141c0877f10SJohn Dyson static int objidnumber;
1421c7c3c6aSMatthew Dillon #endif
143df8bae1dSRodney W. Grimes 
1443075778bSJohn Dyson void
14524a1cce3SDavid Greenman _vm_object_allocate(type, size, object)
14624a1cce3SDavid Greenman 	objtype_t type;
147df8bae1dSRodney W. Grimes 	vm_size_t size;
148df8bae1dSRodney W. Grimes 	register vm_object_t object;
149df8bae1dSRodney W. Grimes {
15099448ed1SJohn Dyson 	int incr;
151df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
15224a1cce3SDavid Greenman 	TAILQ_INIT(&object->shadow_head);
153a1f6d91cSDavid Greenman 
15424a1cce3SDavid Greenman 	object->type = type;
155df8bae1dSRodney W. Grimes 	object->size = size;
156a1f6d91cSDavid Greenman 	object->ref_count = 1;
15724a1cce3SDavid Greenman 	object->flags = 0;
1581c7c3c6aSMatthew Dillon #if 0
159c0877f10SJohn Dyson 	object->id = ++objidnumber;
1601c7c3c6aSMatthew Dillon #endif
161c0877f10SJohn Dyson 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
162069e9bc1SDoug Rabson 		vm_object_set_flag(object, OBJ_ONEMAPPING);
163867a482dSJohn Dyson 	object->behavior = OBJ_NORMAL;
164df8bae1dSRodney W. Grimes 	object->paging_in_progress = 0;
165a1f6d91cSDavid Greenman 	object->resident_page_count = 0;
166eaf13dd7SJohn Dyson 	object->cache_count = 0;
167eaf13dd7SJohn Dyson 	object->wire_count = 0;
168de5f6a77SJohn Dyson 	object->shadow_count = 0;
1695070c7f8SJohn Dyson 	object->pg_color = next_index;
17099448ed1SJohn Dyson 	if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
17199448ed1SJohn Dyson 		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
17299448ed1SJohn Dyson 	else
17399448ed1SJohn Dyson 		incr = size;
17499448ed1SJohn Dyson 	next_index = (next_index + incr) & PQ_L2_MASK;
17524a1cce3SDavid Greenman 	object->handle = NULL;
17624a1cce3SDavid Greenman 	object->backing_object = NULL;
177a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
1781c7c3c6aSMatthew Dillon #if 0
179a2f4a846SJohn Dyson 	object->page_hint = NULL;
1801c7c3c6aSMatthew Dillon #endif
1811c7c3c6aSMatthew Dillon 	/*
1821c7c3c6aSMatthew Dillon 	 * Try to generate a number that will spread objects out in the
1831c7c3c6aSMatthew Dillon 	 * hash table.  We 'wipe' new objects across the hash in 128 page
1841c7c3c6aSMatthew Dillon 	 * increments plus 1 more to offset it a little more by the time
1851c7c3c6aSMatthew Dillon 	 * it wraps around.
1861c7c3c6aSMatthew Dillon 	 */
1871c7c3c6aSMatthew Dillon 	object->hash_rand = object_hash_rand - 129;
188a1f6d91cSDavid Greenman 
189a1f6d91cSDavid Greenman 	object->last_read = 0;
1902d8acc0fSJohn Dyson 	object->generation++;
191df8bae1dSRodney W. Grimes 
192df8bae1dSRodney W. Grimes 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
193df8bae1dSRodney W. Grimes 	vm_object_count++;
1941c7c3c6aSMatthew Dillon 	object_hash_rand = object->hash_rand;
195df8bae1dSRodney W. Grimes }
196df8bae1dSRodney W. Grimes 
197df8bae1dSRodney W. Grimes /*
19826f9a767SRodney W. Grimes  *	vm_object_init:
19926f9a767SRodney W. Grimes  *
20026f9a767SRodney W. Grimes  *	Initialize the VM objects module.
20126f9a767SRodney W. Grimes  */
20226f9a767SRodney W. Grimes void
203a316d390SJohn Dyson vm_object_init()
20426f9a767SRodney W. Grimes {
20526f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
206996c772fSJohn Dyson 	simple_lock_init(&vm_object_list_lock);
20726f9a767SRodney W. Grimes 	vm_object_count = 0;
2080217125fSDavid Greenman 
20926f9a767SRodney W. Grimes 	kernel_object = &kernel_object_store;
210a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
21126f9a767SRodney W. Grimes 	    kernel_object);
21226f9a767SRodney W. Grimes 
21326f9a767SRodney W. Grimes 	kmem_object = &kmem_object_store;
214a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
21526f9a767SRodney W. Grimes 	    kmem_object);
21699448ed1SJohn Dyson 
21799448ed1SJohn Dyson 	obj_zone = &obj_zone_store;
21899448ed1SJohn Dyson 	zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
21999448ed1SJohn Dyson 		vm_objects_init, VM_OBJECTS_INIT);
22099448ed1SJohn Dyson }
22199448ed1SJohn Dyson 
22299448ed1SJohn Dyson void
22399448ed1SJohn Dyson vm_object_init2() {
2240a80f406SJohn Dyson 	zinitna(obj_zone, NULL, NULL, 0, 0, 0, 1);
22526f9a767SRodney W. Grimes }
22626f9a767SRodney W. Grimes 
22726f9a767SRodney W. Grimes /*
22826f9a767SRodney W. Grimes  *	vm_object_allocate:
22926f9a767SRodney W. Grimes  *
23026f9a767SRodney W. Grimes  *	Returns a new object with the given size.
23126f9a767SRodney W. Grimes  */
23226f9a767SRodney W. Grimes 
23326f9a767SRodney W. Grimes vm_object_t
23424a1cce3SDavid Greenman vm_object_allocate(type, size)
23524a1cce3SDavid Greenman 	objtype_t type;
23626f9a767SRodney W. Grimes 	vm_size_t size;
23726f9a767SRodney W. Grimes {
23826f9a767SRodney W. Grimes 	register vm_object_t result;
23999448ed1SJohn Dyson 	result = (vm_object_t) zalloc(obj_zone);
24067bf6868SJohn Dyson 
24124a1cce3SDavid Greenman 	_vm_object_allocate(type, size, result);
24226f9a767SRodney W. Grimes 
24326f9a767SRodney W. Grimes 	return (result);
24426f9a767SRodney W. Grimes }
24526f9a767SRodney W. Grimes 
24626f9a767SRodney W. Grimes 
24726f9a767SRodney W. Grimes /*
248df8bae1dSRodney W. Grimes  *	vm_object_reference:
249df8bae1dSRodney W. Grimes  *
250df8bae1dSRodney W. Grimes  *	Gets another reference to the given object.
251df8bae1dSRodney W. Grimes  */
2526476c0d2SJohn Dyson void
25326f9a767SRodney W. Grimes vm_object_reference(object)
254df8bae1dSRodney W. Grimes 	register vm_object_t object;
255df8bae1dSRodney W. Grimes {
256df8bae1dSRodney W. Grimes 	if (object == NULL)
257df8bae1dSRodney W. Grimes 		return;
25895e5e988SJohn Dyson 
2595526d2d9SEivind Eklund 	KASSERT(!(object->flags & OBJ_DEAD),
2605526d2d9SEivind Eklund 	    ("vm_object_reference: attempting to reference dead obj"));
26195e5e988SJohn Dyson 
262df8bae1dSRodney W. Grimes 	object->ref_count++;
26347221757SJohn Dyson 	if (object->type == OBJT_VNODE) {
26447221757SJohn Dyson 		while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curproc)) {
26595461b45SJohn Dyson #if !defined(MAX_PERF)
26647221757SJohn Dyson 			printf("vm_object_reference: delay in getting object\n");
26795461b45SJohn Dyson #endif
26847221757SJohn Dyson 		}
26947221757SJohn Dyson 	}
27095e5e988SJohn Dyson }
27195e5e988SJohn Dyson 
272bf27292bSJohn Dyson void
27395e5e988SJohn Dyson vm_object_vndeallocate(object)
27495e5e988SJohn Dyson 	vm_object_t object;
27595e5e988SJohn Dyson {
27695e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
277219cbf59SEivind Eklund 
2785526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
2795526d2d9SEivind Eklund 	    ("vm_object_vndeallocate: not a vnode object"));
280219cbf59SEivind Eklund 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
281219cbf59SEivind Eklund #ifdef INVARIANTS
28295e5e988SJohn Dyson 	if (object->ref_count == 0) {
28395e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
28495e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
28595e5e988SJohn Dyson 	}
28695e5e988SJohn Dyson #endif
28795e5e988SJohn Dyson 
28895e5e988SJohn Dyson 	object->ref_count--;
28947221757SJohn Dyson 	if (object->ref_count == 0) {
290bf27292bSJohn Dyson 		vp->v_flag &= ~VTEXT;
291069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_OPT);
2922be70f79SJohn Dyson 	}
29347221757SJohn Dyson 	vrele(vp);
294df8bae1dSRodney W. Grimes }
295df8bae1dSRodney W. Grimes 
296df8bae1dSRodney W. Grimes /*
297df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
298df8bae1dSRodney W. Grimes  *
299df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
300df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
301df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
302df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
303df8bae1dSRodney W. Grimes  *	may be relinquished.
304df8bae1dSRodney W. Grimes  *
305df8bae1dSRodney W. Grimes  *	No object may be locked.
306df8bae1dSRodney W. Grimes  */
30726f9a767SRodney W. Grimes void
30826f9a767SRodney W. Grimes vm_object_deallocate(object)
30926f9a767SRodney W. Grimes 	vm_object_t object;
310df8bae1dSRodney W. Grimes {
311df8bae1dSRodney W. Grimes 	vm_object_t temp;
312df8bae1dSRodney W. Grimes 
313df8bae1dSRodney W. Grimes 	while (object != NULL) {
314df8bae1dSRodney W. Grimes 
31595e5e988SJohn Dyson 		if (object->type == OBJT_VNODE) {
31695e5e988SJohn Dyson 			vm_object_vndeallocate(object);
31795e5e988SJohn Dyson 			return;
31895e5e988SJohn Dyson 		}
31995e5e988SJohn Dyson 
3202be70f79SJohn Dyson 		if (object->ref_count == 0) {
32147221757SJohn Dyson 			panic("vm_object_deallocate: object deallocated too many times: %d", object->type);
3222be70f79SJohn Dyson 		} else if (object->ref_count > 2) {
3232be70f79SJohn Dyson 			object->ref_count--;
3242be70f79SJohn Dyson 			return;
3252be70f79SJohn Dyson 		}
3262be70f79SJohn Dyson 
3272be70f79SJohn Dyson 		/*
3282be70f79SJohn Dyson 		 * Here on ref_count of one or two, which are special cases for
3292be70f79SJohn Dyson 		 * objects.
3302be70f79SJohn Dyson 		 */
331c0877f10SJohn Dyson 		if ((object->ref_count == 2) && (object->shadow_count == 0)) {
332069e9bc1SDoug Rabson 			vm_object_set_flag(object, OBJ_ONEMAPPING);
333c0877f10SJohn Dyson 			object->ref_count--;
334c0877f10SJohn Dyson 			return;
335c0877f10SJohn Dyson 		} else if ((object->ref_count == 2) && (object->shadow_count == 1)) {
336be6d5bfaSDavid Greenman 			object->ref_count--;
3372be70f79SJohn Dyson 			if ((object->handle == NULL) &&
33824a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
33924a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
340a1f6d91cSDavid Greenman 				vm_object_t robject;
34195e5e988SJohn Dyson 
342b18bfc3dSJohn Dyson 				robject = TAILQ_FIRST(&object->shadow_head);
3435526d2d9SEivind Eklund 				KASSERT(robject != NULL,
344219cbf59SEivind Eklund 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
3455526d2d9SEivind Eklund 					 object->ref_count,
3465526d2d9SEivind Eklund 					 object->shadow_count));
34795e5e988SJohn Dyson 				if ((robject->handle == NULL) &&
34824a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
34924a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
350a1f6d91cSDavid Greenman 
35195e5e988SJohn Dyson 					robject->ref_count++;
35295e5e988SJohn Dyson 
3531c7c3c6aSMatthew Dillon 					while (
3541c7c3c6aSMatthew Dillon 						robject->paging_in_progress ||
3551c7c3c6aSMatthew Dillon 						object->paging_in_progress
3561c7c3c6aSMatthew Dillon 					) {
35766095752SJohn Dyson 						vm_object_pip_sleep(robject, "objde1");
35866095752SJohn Dyson 						vm_object_pip_sleep(object, "objde2");
359a1f6d91cSDavid Greenman 					}
360a1f6d91cSDavid Greenman 
36195e5e988SJohn Dyson 					if (robject->ref_count == 1) {
36295e5e988SJohn Dyson 						robject->ref_count--;
363ba8da839SDavid Greenman 						object = robject;
36495e5e988SJohn Dyson 						goto doterm;
36595e5e988SJohn Dyson 					}
36695e5e988SJohn Dyson 
36795e5e988SJohn Dyson 					object = robject;
36895e5e988SJohn Dyson 					vm_object_collapse(object);
369ba8da839SDavid Greenman 					continue;
370a1f6d91cSDavid Greenman 				}
37195e5e988SJohn Dyson 			}
37295e5e988SJohn Dyson 
373a1f6d91cSDavid Greenman 			return;
374df8bae1dSRodney W. Grimes 
37595e5e988SJohn Dyson 		} else {
3761efb74fbSJohn Dyson 			object->ref_count--;
37795e5e988SJohn Dyson 			if (object->ref_count != 0)
37895e5e988SJohn Dyson 				return;
37995e5e988SJohn Dyson 		}
38095e5e988SJohn Dyson 
38195e5e988SJohn Dyson doterm:
3821efb74fbSJohn Dyson 
38324a1cce3SDavid Greenman 		temp = object->backing_object;
384de5f6a77SJohn Dyson 		if (temp) {
38524a1cce3SDavid Greenman 			TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
38695e5e988SJohn Dyson 			temp->shadow_count--;
3872d8acc0fSJohn Dyson 			if (temp->ref_count == 0)
388069e9bc1SDoug Rabson 				vm_object_clear_flag(temp, OBJ_OPT);
389eaf13dd7SJohn Dyson 			temp->generation++;
39095461b45SJohn Dyson 			object->backing_object = NULL;
391de5f6a77SJohn Dyson 		}
392df8bae1dSRodney W. Grimes 		vm_object_terminate(object);
393df8bae1dSRodney W. Grimes 		/* unlocks and deallocates object */
394df8bae1dSRodney W. Grimes 		object = temp;
395df8bae1dSRodney W. Grimes 	}
396df8bae1dSRodney W. Grimes }
397df8bae1dSRodney W. Grimes 
398df8bae1dSRodney W. Grimes /*
399df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
400df8bae1dSRodney W. Grimes  *	up all previously used resources.
401df8bae1dSRodney W. Grimes  *
402df8bae1dSRodney W. Grimes  *	The object must be locked.
4031c7c3c6aSMatthew Dillon  *	This routine may block.
404df8bae1dSRodney W. Grimes  */
40595e5e988SJohn Dyson void
40626f9a767SRodney W. Grimes vm_object_terminate(object)
407df8bae1dSRodney W. Grimes 	register vm_object_t object;
408df8bae1dSRodney W. Grimes {
4093af76890SPoul-Henning Kamp 	register vm_page_t p;
410e4b7635dSDavid Greenman 	int s;
411df8bae1dSRodney W. Grimes 
41295e5e988SJohn Dyson 	/*
41395e5e988SJohn Dyson 	 * Make sure no one uses us.
41495e5e988SJohn Dyson 	 */
415069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
4163c631446SJohn Dyson 
417df8bae1dSRodney W. Grimes 	/*
418f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
419df8bae1dSRodney W. Grimes 	 */
42066095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
421df8bae1dSRodney W. Grimes 
4225526d2d9SEivind Eklund 	KASSERT(!object->paging_in_progress,
4235526d2d9SEivind Eklund 		("vm_object_terminate: pageout in progress"));
42426f9a767SRodney W. Grimes 
42526f9a767SRodney W. Grimes 	/*
4260d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
4270d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
42826f9a767SRodney W. Grimes 	 */
42924a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
43095e5e988SJohn Dyson 		struct vnode *vp;
43195e5e988SJohn Dyson 
43295e5e988SJohn Dyson 		/*
43395e5e988SJohn Dyson 		 * Freeze optimized copies.
43495e5e988SJohn Dyson 		 */
43595e5e988SJohn Dyson 		vm_freeze_copyopts(object, 0, object->size);
43695e5e988SJohn Dyson 
43795e5e988SJohn Dyson 		/*
43895e5e988SJohn Dyson 		 * Clean pages and flush buffers.
43995e5e988SJohn Dyson 		 */
4408f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
44195e5e988SJohn Dyson 
44295e5e988SJohn Dyson 		vp = (struct vnode *) object->handle;
443f6b04d2bSDavid Greenman 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
444bef608bdSJohn Dyson 	}
445bef608bdSJohn Dyson 
4460b10ba98SDavid Greenman 	if (object->ref_count != 0)
4470b10ba98SDavid Greenman 		panic("vm_object_terminate: object with references, ref_count=%d", object->ref_count);
448996c772fSJohn Dyson 
4490d94caffSDavid Greenman 	/*
450356863ebSDavid Greenman 	 * Now free any remaining pages. For internal objects, this also
451356863ebSDavid Greenman 	 * removes them from paging queues. Don't free wired pages, just
452356863ebSDavid Greenman 	 * remove them from the object.
453df8bae1dSRodney W. Grimes 	 */
454e4b7635dSDavid Greenman 	s = splvm();
455b18bfc3dSJohn Dyson 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
45695461b45SJohn Dyson #if !defined(MAX_PERF)
457a2f4a846SJohn Dyson 		if (p->busy || (p->flags & PG_BUSY))
4581c7c3c6aSMatthew Dillon 			panic("vm_object_terminate: freeing busy page %p\n", p);
45995461b45SJohn Dyson #endif
4600b10ba98SDavid Greenman 		if (p->wire_count == 0) {
461e69763a3SDoug Rabson 			vm_page_busy(p);
462df8bae1dSRodney W. Grimes 			vm_page_free(p);
463df8bae1dSRodney W. Grimes 			cnt.v_pfree++;
4640b10ba98SDavid Greenman 		} else {
465356863ebSDavid Greenman 			vm_page_busy(p);
4660b10ba98SDavid Greenman 			vm_page_remove(p);
4670b10ba98SDavid Greenman 		}
468df8bae1dSRodney W. Grimes 	}
469e4b7635dSDavid Greenman 	splx(s);
470bef608bdSJohn Dyson 
4712d8acc0fSJohn Dyson 	/*
4729fcfb650SDavid Greenman 	 * Let the pager know object is dead.
4739fcfb650SDavid Greenman 	 */
4749fcfb650SDavid Greenman 	vm_pager_deallocate(object);
4759fcfb650SDavid Greenman 
4769fcfb650SDavid Greenman 	/*
4770b10ba98SDavid Greenman 	 * Remove the object from the global object list.
4782d8acc0fSJohn Dyson 	 */
479996c772fSJohn Dyson 	simple_lock(&vm_object_list_lock);
480df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
481996c772fSJohn Dyson 	simple_unlock(&vm_object_list_lock);
4820b10ba98SDavid Greenman 
4830b10ba98SDavid Greenman 	wakeup(object);
4840b10ba98SDavid Greenman 
485df8bae1dSRodney W. Grimes 	/*
486df8bae1dSRodney W. Grimes 	 * Free the space for the object.
487df8bae1dSRodney W. Grimes 	 */
48899448ed1SJohn Dyson 	zfree(obj_zone, object);
48947221757SJohn Dyson }
490df8bae1dSRodney W. Grimes 
491df8bae1dSRodney W. Grimes /*
492df8bae1dSRodney W. Grimes  *	vm_object_page_clean
493df8bae1dSRodney W. Grimes  *
494df8bae1dSRodney W. Grimes  *	Clean all dirty pages in the specified range of object.
49526f9a767SRodney W. Grimes  *	Leaves page on whatever queue it is currently on.
49626f9a767SRodney W. Grimes  *
49726f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
49826f9a767SRodney W. Grimes  *
49926f9a767SRodney W. Grimes  *	The object must be locked.
50026f9a767SRodney W. Grimes  */
501f6b04d2bSDavid Greenman 
502f6b04d2bSDavid Greenman void
5038f9110f6SJohn Dyson vm_object_page_clean(object, start, end, flags)
504f6b04d2bSDavid Greenman 	vm_object_t object;
505a316d390SJohn Dyson 	vm_pindex_t start;
506a316d390SJohn Dyson 	vm_pindex_t end;
5078f9110f6SJohn Dyson 	int flags;
508f6b04d2bSDavid Greenman {
509bd7e5f99SJohn Dyson 	register vm_page_t p, np, tp;
510f6b04d2bSDavid Greenman 	register vm_offset_t tstart, tend;
511bd7e5f99SJohn Dyson 	vm_pindex_t pi;
512aef922f5SJohn Dyson 	int s;
51324a1cce3SDavid Greenman 	struct vnode *vp;
514aef922f5SJohn Dyson 	int runlen;
515bd7e5f99SJohn Dyson 	int maxf;
516bd7e5f99SJohn Dyson 	int chkb;
517bd7e5f99SJohn Dyson 	int maxb;
518bd7e5f99SJohn Dyson 	int i;
5198f9110f6SJohn Dyson 	int pagerflags;
520bd7e5f99SJohn Dyson 	vm_page_t maf[vm_pageout_page_count];
521bd7e5f99SJohn Dyson 	vm_page_t mab[vm_pageout_page_count];
522aef922f5SJohn Dyson 	vm_page_t ma[vm_pageout_page_count];
5232d8acc0fSJohn Dyson 	int curgeneration;
524f6b04d2bSDavid Greenman 
525aef922f5SJohn Dyson 	if (object->type != OBJT_VNODE ||
526aef922f5SJohn Dyson 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
527f6b04d2bSDavid Greenman 		return;
528f6b04d2bSDavid Greenman 
5298f9110f6SJohn Dyson 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0;
5308f9110f6SJohn Dyson 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
5318f9110f6SJohn Dyson 
53224a1cce3SDavid Greenman 	vp = object->handle;
53324a1cce3SDavid Greenman 
534069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_CLEANING);
53524a1cce3SDavid Greenman 
536f6b04d2bSDavid Greenman 	tstart = start;
537f6b04d2bSDavid Greenman 	if (end == 0) {
538f6b04d2bSDavid Greenman 		tend = object->size;
539f6b04d2bSDavid Greenman 	} else {
540f6b04d2bSDavid Greenman 		tend = end;
541f6b04d2bSDavid Greenman 	}
542eaf13dd7SJohn Dyson 
543eaf13dd7SJohn Dyson 	for(p = TAILQ_FIRST(&object->memq); p; p = TAILQ_NEXT(p, listq)) {
544e69763a3SDoug Rabson 		vm_page_flag_set(p, PG_CLEANCHK);
545eaf13dd7SJohn Dyson 		vm_page_protect(p, VM_PROT_READ);
546eaf13dd7SJohn Dyson 	}
547eaf13dd7SJohn Dyson 
548a316d390SJohn Dyson 	if ((tstart == 0) && (tend == object->size)) {
549069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
550ec4f9fb0SDavid Greenman 	}
551f6b04d2bSDavid Greenman 
552bd7e5f99SJohn Dyson rescan:
5532d8acc0fSJohn Dyson 	curgeneration = object->generation;
5542d8acc0fSJohn Dyson 
555b18bfc3dSJohn Dyson 	for(p = TAILQ_FIRST(&object->memq); p; p = np) {
556b18bfc3dSJohn Dyson 		np = TAILQ_NEXT(p, listq);
557bd7e5f99SJohn Dyson 
558bd7e5f99SJohn Dyson 		pi = p->pindex;
559bd7e5f99SJohn Dyson 		if (((p->flags & PG_CLEANCHK) == 0) ||
560bd7e5f99SJohn Dyson 			(pi < tstart) || (pi >= tend) ||
5615070c7f8SJohn Dyson 			(p->valid == 0) ||
5625070c7f8SJohn Dyson 			((p->queue - p->pc) == PQ_CACHE)) {
563e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
564aef922f5SJohn Dyson 			continue;
565f6b04d2bSDavid Greenman 		}
566f6b04d2bSDavid Greenman 
567bd7e5f99SJohn Dyson 		vm_page_test_dirty(p);
568bd7e5f99SJohn Dyson 		if ((p->dirty & p->valid) == 0) {
569e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
570bd7e5f99SJohn Dyson 			continue;
571bd7e5f99SJohn Dyson 		}
572ec4f9fb0SDavid Greenman 
573b18bfc3dSJohn Dyson 		s = splvm();
5741c7c3c6aSMatthew Dillon 		while (vm_page_sleep_busy(p, TRUE, "vpcwai")) {
5752d8acc0fSJohn Dyson 			if (object->generation != curgeneration) {
576f6b04d2bSDavid Greenman 				splx(s);
577bd7e5f99SJohn Dyson 				goto rescan;
578f6b04d2bSDavid Greenman 			}
5792d8acc0fSJohn Dyson 		}
580f6b04d2bSDavid Greenman 
581bd7e5f99SJohn Dyson 		maxf = 0;
582bd7e5f99SJohn Dyson 		for(i=1;i<vm_pageout_page_count;i++) {
5838aef1712SMatthew Dillon 			if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
584bd7e5f99SJohn Dyson 				if ((tp->flags & PG_BUSY) ||
585ffc82b0aSJohn Dyson 					(tp->flags & PG_CLEANCHK) == 0 ||
586ffc82b0aSJohn Dyson 					(tp->busy != 0))
587bd7e5f99SJohn Dyson 					break;
5885070c7f8SJohn Dyson 				if((tp->queue - tp->pc) == PQ_CACHE) {
589e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
5903077a9c2SJohn Dyson 					break;
5913077a9c2SJohn Dyson 				}
592bd7e5f99SJohn Dyson 				vm_page_test_dirty(tp);
593bd7e5f99SJohn Dyson 				if ((tp->dirty & tp->valid) == 0) {
594e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
595bd7e5f99SJohn Dyson 					break;
596bd7e5f99SJohn Dyson 				}
597bd7e5f99SJohn Dyson 				maf[ i - 1 ] = tp;
598bd7e5f99SJohn Dyson 				maxf++;
599bd7e5f99SJohn Dyson 				continue;
600bd7e5f99SJohn Dyson 			}
601bd7e5f99SJohn Dyson 			break;
602bd7e5f99SJohn Dyson 		}
603aef922f5SJohn Dyson 
604bd7e5f99SJohn Dyson 		maxb = 0;
605bd7e5f99SJohn Dyson 		chkb = vm_pageout_page_count -  maxf;
606bd7e5f99SJohn Dyson 		if (chkb) {
607bd7e5f99SJohn Dyson 			for(i = 1; i < chkb;i++) {
6088aef1712SMatthew Dillon 				if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
609bd7e5f99SJohn Dyson 					if ((tp->flags & PG_BUSY) ||
610ffc82b0aSJohn Dyson 						(tp->flags & PG_CLEANCHK) == 0 ||
611ffc82b0aSJohn Dyson 						(tp->busy != 0))
612bd7e5f99SJohn Dyson 						break;
6135070c7f8SJohn Dyson 					if((tp->queue - tp->pc) == PQ_CACHE) {
614e69763a3SDoug Rabson 						vm_page_flag_clear(tp, PG_CLEANCHK);
6153077a9c2SJohn Dyson 						break;
6163077a9c2SJohn Dyson 					}
617bd7e5f99SJohn Dyson 					vm_page_test_dirty(tp);
618bd7e5f99SJohn Dyson 					if ((tp->dirty & tp->valid) == 0) {
619e69763a3SDoug Rabson 						vm_page_flag_clear(tp, PG_CLEANCHK);
620bd7e5f99SJohn Dyson 						break;
621bd7e5f99SJohn Dyson 					}
622bd7e5f99SJohn Dyson 					mab[ i - 1 ] = tp;
623bd7e5f99SJohn Dyson 					maxb++;
624bd7e5f99SJohn Dyson 					continue;
625bd7e5f99SJohn Dyson 				}
626bd7e5f99SJohn Dyson 				break;
627bd7e5f99SJohn Dyson 			}
628bd7e5f99SJohn Dyson 		}
629bd7e5f99SJohn Dyson 
630bd7e5f99SJohn Dyson 		for(i=0;i<maxb;i++) {
631bd7e5f99SJohn Dyson 			int index = (maxb - i) - 1;
632bd7e5f99SJohn Dyson 			ma[index] = mab[i];
633e69763a3SDoug Rabson 			vm_page_flag_clear(ma[index], PG_CLEANCHK);
634bd7e5f99SJohn Dyson 		}
635e69763a3SDoug Rabson 		vm_page_flag_clear(p, PG_CLEANCHK);
636bd7e5f99SJohn Dyson 		ma[maxb] = p;
637bd7e5f99SJohn Dyson 		for(i=0;i<maxf;i++) {
638bd7e5f99SJohn Dyson 			int index = (maxb + i) + 1;
639bd7e5f99SJohn Dyson 			ma[index] = maf[i];
640e69763a3SDoug Rabson 			vm_page_flag_clear(ma[index], PG_CLEANCHK);
641f6b04d2bSDavid Greenman 		}
642bd7e5f99SJohn Dyson 		runlen = maxb + maxf + 1;
643cf2819ccSJohn Dyson 
644f35329acSJohn Dyson 		splx(s);
6458f9110f6SJohn Dyson 		vm_pageout_flush(ma, runlen, pagerflags);
646cf2819ccSJohn Dyson 		for (i = 0; i<runlen; i++) {
647cf2819ccSJohn Dyson 			if (ma[i]->valid & ma[i]->dirty) {
648cf2819ccSJohn Dyson 				vm_page_protect(ma[i], VM_PROT_READ);
649e69763a3SDoug Rabson 				vm_page_flag_set(ma[i], PG_CLEANCHK);
650cf2819ccSJohn Dyson 			}
651cf2819ccSJohn Dyson 		}
6522d8acc0fSJohn Dyson 		if (object->generation != curgeneration)
653bd7e5f99SJohn Dyson 			goto rescan;
654f6b04d2bSDavid Greenman 	}
655aef922f5SJohn Dyson 
656cf2819ccSJohn Dyson 	VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
657aef922f5SJohn Dyson 
658069e9bc1SDoug Rabson 	vm_object_clear_flag(object, OBJ_CLEANING);
659f5cf85d4SDavid Greenman 	return;
66026f9a767SRodney W. Grimes }
661df8bae1dSRodney W. Grimes 
662f708ef1bSPoul-Henning Kamp #ifdef not_used
663f708ef1bSPoul-Henning Kamp /* XXX I cannot tell if this should be an exported symbol */
664df8bae1dSRodney W. Grimes /*
665df8bae1dSRodney W. Grimes  *	vm_object_deactivate_pages
666df8bae1dSRodney W. Grimes  *
667df8bae1dSRodney W. Grimes  *	Deactivate all pages in the specified object.  (Keep its pages
668df8bae1dSRodney W. Grimes  *	in memory even though it is no longer referenced.)
669df8bae1dSRodney W. Grimes  *
670df8bae1dSRodney W. Grimes  *	The object must be locked.
671df8bae1dSRodney W. Grimes  */
672f708ef1bSPoul-Henning Kamp static void
673df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object)
674df8bae1dSRodney W. Grimes 	register vm_object_t object;
675df8bae1dSRodney W. Grimes {
676df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
677df8bae1dSRodney W. Grimes 
678b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
679b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
680df8bae1dSRodney W. Grimes 		vm_page_deactivate(p);
681df8bae1dSRodney W. Grimes 	}
682df8bae1dSRodney W. Grimes }
683f708ef1bSPoul-Henning Kamp #endif
684df8bae1dSRodney W. Grimes 
6858e3ad7c9SMatthew Dillon #if 0
6868e3ad7c9SMatthew Dillon 
687df8bae1dSRodney W. Grimes /*
688df8bae1dSRodney W. Grimes  *	vm_object_pmap_copy:
689df8bae1dSRodney W. Grimes  *
690df8bae1dSRodney W. Grimes  *	Makes all physical pages in the specified
691df8bae1dSRodney W. Grimes  *	object range copy-on-write.  No writeable
692df8bae1dSRodney W. Grimes  *	references to these pages should remain.
693df8bae1dSRodney W. Grimes  *
694df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
695df8bae1dSRodney W. Grimes  */
6960d94caffSDavid Greenman void
6970d94caffSDavid Greenman vm_object_pmap_copy(object, start, end)
698df8bae1dSRodney W. Grimes 	register vm_object_t object;
699a316d390SJohn Dyson 	register vm_pindex_t start;
700a316d390SJohn Dyson 	register vm_pindex_t end;
701df8bae1dSRodney W. Grimes {
702df8bae1dSRodney W. Grimes 	register vm_page_t p;
703df8bae1dSRodney W. Grimes 
704aef922f5SJohn Dyson 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
705df8bae1dSRodney W. Grimes 		return;
706df8bae1dSRodney W. Grimes 
707b5b40fa6SJohn Dyson 	for (p = TAILQ_FIRST(&object->memq);
708b5b40fa6SJohn Dyson 		p != NULL;
709b5b40fa6SJohn Dyson 		p = TAILQ_NEXT(p, listq)) {
710f919ebdeSDavid Greenman 		vm_page_protect(p, VM_PROT_READ);
711df8bae1dSRodney W. Grimes 	}
712aef922f5SJohn Dyson 
713069e9bc1SDoug Rabson 	vm_object_clear_flag(object, OBJ_WRITEABLE);
714df8bae1dSRodney W. Grimes }
715df8bae1dSRodney W. Grimes 
7168e3ad7c9SMatthew Dillon #endif
7178e3ad7c9SMatthew Dillon 
718df8bae1dSRodney W. Grimes /*
7198e3ad7c9SMatthew Dillon  * Same as vm_object_pmap_copy, except range checking really
7201efb74fbSJohn Dyson  * works, and is meant for small sections of an object.
7218e3ad7c9SMatthew Dillon  *
7228e3ad7c9SMatthew Dillon  * This code protects resident pages by making them read-only
7238e3ad7c9SMatthew Dillon  * and is typically called on a fork or split when a page
7248e3ad7c9SMatthew Dillon  * is converted to copy-on-write.
7258e3ad7c9SMatthew Dillon  *
7268e3ad7c9SMatthew Dillon  * NOTE: If the page is already at VM_PROT_NONE, calling
7278e3ad7c9SMatthew Dillon  * vm_page_protect will have no effect.
7281efb74fbSJohn Dyson  */
7298e3ad7c9SMatthew Dillon 
7301efb74fbSJohn Dyson void
7311efb74fbSJohn Dyson vm_object_pmap_copy_1(object, start, end)
7321efb74fbSJohn Dyson 	register vm_object_t object;
7331efb74fbSJohn Dyson 	register vm_pindex_t start;
7341efb74fbSJohn Dyson 	register vm_pindex_t end;
7351efb74fbSJohn Dyson {
7361efb74fbSJohn Dyson 	vm_pindex_t idx;
7371efb74fbSJohn Dyson 	register vm_page_t p;
7381efb74fbSJohn Dyson 
7391efb74fbSJohn Dyson 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
7401efb74fbSJohn Dyson 		return;
7411efb74fbSJohn Dyson 
7421efb74fbSJohn Dyson 	for (idx = start; idx < end; idx++) {
7431efb74fbSJohn Dyson 		p = vm_page_lookup(object, idx);
7441efb74fbSJohn Dyson 		if (p == NULL)
7451efb74fbSJohn Dyson 			continue;
7461efb74fbSJohn Dyson 		vm_page_protect(p, VM_PROT_READ);
7471efb74fbSJohn Dyson 	}
7481efb74fbSJohn Dyson }
7491efb74fbSJohn Dyson 
7501efb74fbSJohn Dyson /*
751df8bae1dSRodney W. Grimes  *	vm_object_pmap_remove:
752df8bae1dSRodney W. Grimes  *
753df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
754df8bae1dSRodney W. Grimes  *	object range from all physical maps.
755df8bae1dSRodney W. Grimes  *
756df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
757df8bae1dSRodney W. Grimes  */
75826f9a767SRodney W. Grimes void
75926f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end)
760df8bae1dSRodney W. Grimes 	register vm_object_t object;
761a316d390SJohn Dyson 	register vm_pindex_t start;
762a316d390SJohn Dyson 	register vm_pindex_t end;
763df8bae1dSRodney W. Grimes {
764df8bae1dSRodney W. Grimes 	register vm_page_t p;
765df8bae1dSRodney W. Grimes 	if (object == NULL)
766df8bae1dSRodney W. Grimes 		return;
767b5b40fa6SJohn Dyson 	for (p = TAILQ_FIRST(&object->memq);
768b5b40fa6SJohn Dyson 		p != NULL;
769b5b40fa6SJohn Dyson 		p = TAILQ_NEXT(p, listq)) {
770bd7e5f99SJohn Dyson 		if (p->pindex >= start && p->pindex < end)
771f919ebdeSDavid Greenman 			vm_page_protect(p, VM_PROT_NONE);
77226f9a767SRodney W. Grimes 	}
7736e20a165SJohn Dyson 	if ((start == 0) && (object->size == end))
774069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_WRITEABLE);
77526f9a767SRodney W. Grimes }
776df8bae1dSRodney W. Grimes 
777df8bae1dSRodney W. Grimes /*
778867a482dSJohn Dyson  *	vm_object_madvise:
779867a482dSJohn Dyson  *
780867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
7811c7c3c6aSMatthew Dillon  *
7821c7c3c6aSMatthew Dillon  *	Currently, madvise() functions are limited to the default and
7831c7c3c6aSMatthew Dillon  *	swap object types only, and also limited to only the unshared portions
7841c7c3c6aSMatthew Dillon  *	of a process's address space.  MADV_FREE, certainly, could never be
7851c7c3c6aSMatthew Dillon  *	run on anything else.  The others are more flexible and the code could
7861c7c3c6aSMatthew Dillon  *	be adjusted in the future to handle expanded cases for them.
787867a482dSJohn Dyson  */
788867a482dSJohn Dyson void
789867a482dSJohn Dyson vm_object_madvise(object, pindex, count, advise)
790867a482dSJohn Dyson 	vm_object_t object;
791867a482dSJohn Dyson 	vm_pindex_t pindex;
792867a482dSJohn Dyson 	int count;
793867a482dSJohn Dyson 	int advise;
794867a482dSJohn Dyson {
7956e20a165SJohn Dyson 	vm_pindex_t end, tpindex;
7966e20a165SJohn Dyson 	vm_object_t tobject;
797867a482dSJohn Dyson 	vm_page_t m;
798867a482dSJohn Dyson 
799867a482dSJohn Dyson 	if (object == NULL)
800867a482dSJohn Dyson 		return;
801867a482dSJohn Dyson 
802867a482dSJohn Dyson 	end = pindex + count;
803867a482dSJohn Dyson 
8041c7c3c6aSMatthew Dillon 	/*
8051c7c3c6aSMatthew Dillon 	 * MADV_FREE special case - free any swap backing store (as well
8061c7c3c6aSMatthew Dillon 	 * as resident pages later on).
8071c7c3c6aSMatthew Dillon 	 */
8086e20a165SJohn Dyson 
8091c7c3c6aSMatthew Dillon 	if (advise == MADV_FREE) {
8101c7c3c6aSMatthew Dillon 		tobject = object;
8111c7c3c6aSMatthew Dillon 		tpindex = pindex;
8121c7c3c6aSMatthew Dillon 
8131c7c3c6aSMatthew Dillon 		while (
8141c7c3c6aSMatthew Dillon 		    (tobject->type == OBJT_DEFAULT ||
8151c7c3c6aSMatthew Dillon 		     tobject->type == OBJT_SWAP) &&
8161c7c3c6aSMatthew Dillon 		    (tobject->flags & OBJ_ONEMAPPING)
8171c7c3c6aSMatthew Dillon 		) {
8181c7c3c6aSMatthew Dillon 			if (tobject->type == OBJT_SWAP) {
8191c7c3c6aSMatthew Dillon 				swap_pager_freespace(tobject, tpindex, count);
8201c7c3c6aSMatthew Dillon 			}
8211c7c3c6aSMatthew Dillon 			if ((tobject = tobject->backing_object) == NULL)
8221c7c3c6aSMatthew Dillon 				break;
8231c7c3c6aSMatthew Dillon 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
8241c7c3c6aSMatthew Dillon 		}
8251c7c3c6aSMatthew Dillon 	}
8261c7c3c6aSMatthew Dillon 
8271c7c3c6aSMatthew Dillon 	/*
8281c7c3c6aSMatthew Dillon 	 * Locate and adjust resident pages
8291c7c3c6aSMatthew Dillon 	 */
8301c7c3c6aSMatthew Dillon 
8311c7c3c6aSMatthew Dillon 	for (; pindex < end; pindex += 1) {
8326e20a165SJohn Dyson relookup:
8336e20a165SJohn Dyson 		tobject = object;
8346e20a165SJohn Dyson 		tpindex = pindex;
8356e20a165SJohn Dyson shadowlookup:
8361c7c3c6aSMatthew Dillon 
8371c7c3c6aSMatthew Dillon 		if (tobject->type != OBJT_DEFAULT &&
8381c7c3c6aSMatthew Dillon 		    tobject->type != OBJT_SWAP
8391c7c3c6aSMatthew Dillon 		) {
8406e20a165SJohn Dyson 			continue;
8416e20a165SJohn Dyson 		}
8426e20a165SJohn Dyson 
8431c7c3c6aSMatthew Dillon 		if ((tobject->flags & OBJ_ONEMAPPING) == 0)
8441c7c3c6aSMatthew Dillon 			continue;
8451c7c3c6aSMatthew Dillon 
8461c7c3c6aSMatthew Dillon 		m = vm_page_lookup(tobject, tpindex);
8471c7c3c6aSMatthew Dillon 
8481c7c3c6aSMatthew Dillon 		if (m == NULL) {
8496e20a165SJohn Dyson 			tobject = tobject->backing_object;
8501c7c3c6aSMatthew Dillon 			if (tobject == NULL)
8511c7c3c6aSMatthew Dillon 				continue;
8521c7c3c6aSMatthew Dillon #if 0
8536e20a165SJohn Dyson 			if ((tobject == NULL) || (tobject->ref_count != 1)) {
8546e20a165SJohn Dyson 				continue;
8556e20a165SJohn Dyson 			}
8561c7c3c6aSMatthew Dillon #endif
8576e20a165SJohn Dyson 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
8586e20a165SJohn Dyson 			goto shadowlookup;
8596e20a165SJohn Dyson 		}
860867a482dSJohn Dyson 
861867a482dSJohn Dyson 		/*
862867a482dSJohn Dyson 		 * If the page is busy or not in a normal active state,
863867a482dSJohn Dyson 		 * we skip it.  Things can break if we mess with pages
864867a482dSJohn Dyson 		 * in any of the below states.
865867a482dSJohn Dyson 		 */
8661c7c3c6aSMatthew Dillon 		if (
8671c7c3c6aSMatthew Dillon 		    m->hold_count ||
8681c7c3c6aSMatthew Dillon 		    m->wire_count ||
8691c7c3c6aSMatthew Dillon 		    m->valid != VM_PAGE_BITS_ALL
8701c7c3c6aSMatthew Dillon 		) {
871867a482dSJohn Dyson 			continue;
8726e20a165SJohn Dyson 		}
8736e20a165SJohn Dyson 
8741c7c3c6aSMatthew Dillon  		if (vm_page_sleep_busy(m, TRUE, "madvpo"))
8756e20a165SJohn Dyson   			goto relookup;
876867a482dSJohn Dyson 
877867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
878867a482dSJohn Dyson 			vm_page_activate(m);
8796e20a165SJohn Dyson 		} else if (advise == MADV_DONTNEED) {
880867a482dSJohn Dyson 			vm_page_deactivate(m);
8810a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
8821c7c3c6aSMatthew Dillon 			/*
8831c7c3c6aSMatthew Dillon 			 * If MADV_FREE_FORCE_FREE is defined, we attempt to
8841c7c3c6aSMatthew Dillon 			 * immediately free the page.  Otherwise we just
8851c7c3c6aSMatthew Dillon 			 * destroy any swap backing store, mark it clean,
8861c7c3c6aSMatthew Dillon 			 * and stuff it into the cache.
8871c7c3c6aSMatthew Dillon 			 */
8886e20a165SJohn Dyson 			pmap_clear_modify(VM_PAGE_TO_PHYS(m));
8896e20a165SJohn Dyson 			m->dirty = 0;
8901c7c3c6aSMatthew Dillon 
8911c7c3c6aSMatthew Dillon #ifdef MADV_FREE_FORCE_FREE
8921c7c3c6aSMatthew Dillon 			if (tobject->resident_page_count > 1) {
8931c7c3c6aSMatthew Dillon 				vm_page_busy(m);
8941c7c3c6aSMatthew Dillon 				vm_page_protect(m, VM_PROT_NONE);
8951c7c3c6aSMatthew Dillon 				vm_page_free(m);
8961c7c3c6aSMatthew Dillon 			} else
8971c7c3c6aSMatthew Dillon #endif
8981c7c3c6aSMatthew Dillon 			{
8991c7c3c6aSMatthew Dillon 				vm_page_cache(m);
9001c7c3c6aSMatthew Dillon 			}
901867a482dSJohn Dyson 		}
902867a482dSJohn Dyson 	}
903867a482dSJohn Dyson }
904867a482dSJohn Dyson 
905867a482dSJohn Dyson /*
906df8bae1dSRodney W. Grimes  *	vm_object_shadow:
907df8bae1dSRodney W. Grimes  *
908df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
909df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
910df8bae1dSRodney W. Grimes  *	object reference is deallocated.
911df8bae1dSRodney W. Grimes  *
912df8bae1dSRodney W. Grimes  *	The new object and offset into that object
913df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
914df8bae1dSRodney W. Grimes  */
915df8bae1dSRodney W. Grimes 
91626f9a767SRodney W. Grimes void
91726f9a767SRodney W. Grimes vm_object_shadow(object, offset, length)
918df8bae1dSRodney W. Grimes 	vm_object_t *object;	/* IN/OUT */
919a316d390SJohn Dyson 	vm_ooffset_t *offset;	/* IN/OUT */
920df8bae1dSRodney W. Grimes 	vm_size_t length;
921df8bae1dSRodney W. Grimes {
922df8bae1dSRodney W. Grimes 	register vm_object_t source;
923df8bae1dSRodney W. Grimes 	register vm_object_t result;
924df8bae1dSRodney W. Grimes 
925df8bae1dSRodney W. Grimes 	source = *object;
926df8bae1dSRodney W. Grimes 
927df8bae1dSRodney W. Grimes 	/*
928df8bae1dSRodney W. Grimes 	 * Allocate a new object with the given length
929df8bae1dSRodney W. Grimes 	 */
930df8bae1dSRodney W. Grimes 
93124a1cce3SDavid Greenman 	if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
932df8bae1dSRodney W. Grimes 		panic("vm_object_shadow: no object for shadowing");
933df8bae1dSRodney W. Grimes 
934df8bae1dSRodney W. Grimes 	/*
9350d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
9360d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
9370d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
9380d94caffSDavid Greenman 	 * of reference count.
9399b09fe24SMatthew Dillon 	 *
9409b09fe24SMatthew Dillon 	 * Try to optimize the result object's page color when shadowing
9419b09fe24SMatthew Dillon 	 * in order to maintain page coloring consistancy in the combined
9429b09fe24SMatthew Dillon 	 * shadowed object.
943df8bae1dSRodney W. Grimes 	 */
94424a1cce3SDavid Greenman 	result->backing_object = source;
945de5f6a77SJohn Dyson 	if (source) {
946de5f6a77SJohn Dyson 		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
947069e9bc1SDoug Rabson 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
948eaf13dd7SJohn Dyson 		source->shadow_count++;
949eaf13dd7SJohn Dyson 		source->generation++;
9509b09fe24SMatthew Dillon 		result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & PQ_L2_MASK;
951de5f6a77SJohn Dyson 	}
952df8bae1dSRodney W. Grimes 
953df8bae1dSRodney W. Grimes 	/*
9540d94caffSDavid Greenman 	 * Store the offset into the source object, and fix up the offset into
9550d94caffSDavid Greenman 	 * the new object.
956df8bae1dSRodney W. Grimes 	 */
957df8bae1dSRodney W. Grimes 
95824a1cce3SDavid Greenman 	result->backing_object_offset = *offset;
959df8bae1dSRodney W. Grimes 
960df8bae1dSRodney W. Grimes 	/*
961df8bae1dSRodney W. Grimes 	 * Return the new things
962df8bae1dSRodney W. Grimes 	 */
963df8bae1dSRodney W. Grimes 
964df8bae1dSRodney W. Grimes 	*offset = 0;
965df8bae1dSRodney W. Grimes 	*object = result;
966df8bae1dSRodney W. Grimes }
967df8bae1dSRodney W. Grimes 
968df8bae1dSRodney W. Grimes 
969df8bae1dSRodney W. Grimes /*
9702fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
9712fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
9722fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
9732fe6e4d7SDavid Greenman  */
9742fe6e4d7SDavid Greenman static void
9752fe6e4d7SDavid Greenman vm_object_qcollapse(object)
9762fe6e4d7SDavid Greenman 	register vm_object_t object;
9772fe6e4d7SDavid Greenman {
9782fe6e4d7SDavid Greenman 	register vm_object_t backing_object;
9791c7c3c6aSMatthew Dillon 	register vm_pindex_t backing_offset_index;
9802fe6e4d7SDavid Greenman 	register vm_page_t p, pp;
9812fe6e4d7SDavid Greenman 	register vm_size_t size;
982588059beSMatthew Dillon 	int s;
9832fe6e4d7SDavid Greenman 
98424a1cce3SDavid Greenman 	backing_object = object->backing_object;
9852fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
9862fe6e4d7SDavid Greenman 		return;
9872fe6e4d7SDavid Greenman 
988010cf3b9SDavid Greenman 	backing_object->ref_count += 2;
989010cf3b9SDavid Greenman 
990a316d390SJohn Dyson 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
9912fe6e4d7SDavid Greenman 	size = object->size;
9921c7c3c6aSMatthew Dillon 
993588059beSMatthew Dillon 	/*
994588059beSMatthew Dillon 	 * Since paging is in progress, we have to run at splbio() to
995588059beSMatthew Dillon 	 * avoid busied pages from getting ripped out from under us
996588059beSMatthew Dillon 	 * and screwing up the list sequencing.
997588059beSMatthew Dillon 	 */
998588059beSMatthew Dillon 
999588059beSMatthew Dillon 	s = splbio();
1000588059beSMatthew Dillon 
1001b18bfc3dSJohn Dyson 	p = TAILQ_FIRST(&backing_object->memq);
10022fe6e4d7SDavid Greenman 	while (p) {
10032fe6e4d7SDavid Greenman 		vm_page_t next;
1004588059beSMatthew Dillon 		vm_pindex_t new_pindex;
1005588059beSMatthew Dillon 		vm_pindex_t old_pindex;
10060d94caffSDavid Greenman 
10071c7c3c6aSMatthew Dillon 		/*
10081c7c3c6aSMatthew Dillon 		 * setup for loop.
10091c7c3c6aSMatthew Dillon 		 * loop if the page isn't trivial.
10101c7c3c6aSMatthew Dillon 		 */
10111c7c3c6aSMatthew Dillon 
1012b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
1013bd7e5f99SJohn Dyson 		if ((p->flags & (PG_BUSY | PG_FICTITIOUS)) ||
10145070c7f8SJohn Dyson 		    !p->valid || p->hold_count || p->wire_count || p->busy) {
10152fe6e4d7SDavid Greenman 			p = next;
10162fe6e4d7SDavid Greenman 			continue;
10172fe6e4d7SDavid Greenman 		}
10181c7c3c6aSMatthew Dillon 
10191c7c3c6aSMatthew Dillon 		/*
10201c7c3c6aSMatthew Dillon 		 * busy the page and move it from the backing store to the
10211c7c3c6aSMatthew Dillon 		 * parent object.
10221c7c3c6aSMatthew Dillon 		 */
10231c7c3c6aSMatthew Dillon 
1024e69763a3SDoug Rabson 		vm_page_busy(p);
1025eaf13dd7SJohn Dyson 
1026588059beSMatthew Dillon 		KASSERT(p->object == backing_object, ("vm_object_qcollapse(): object mismatch"));
10271c7c3c6aSMatthew Dillon 
1028588059beSMatthew Dillon 		old_pindex = p->pindex;
1029588059beSMatthew Dillon 		new_pindex = old_pindex - backing_offset_index;
1030588059beSMatthew Dillon 
1031588059beSMatthew Dillon 		if (old_pindex < backing_offset_index || new_pindex >= size) {
1032588059beSMatthew Dillon 			/*
1033588059beSMatthew Dillon 			 * Page is out of the parent object's range, we
1034588059beSMatthew Dillon 			 * can simply destroy it.
1035588059beSMatthew Dillon 			 */
1036b18bfc3dSJohn Dyson 			vm_page_protect(p, VM_PROT_NONE);
10372fe6e4d7SDavid Greenman 			vm_page_free(p);
10382fe6e4d7SDavid Greenman 		} else {
1039a316d390SJohn Dyson 			pp = vm_page_lookup(object, new_pindex);
1040eaf13dd7SJohn Dyson 			if (pp != NULL ||
1041eaf13dd7SJohn Dyson 				(object->type == OBJT_SWAP && vm_pager_has_page(object,
10421c7c3c6aSMatthew Dillon 				    new_pindex, NULL, NULL))) {
1043588059beSMatthew Dillon 				/*
1044588059beSMatthew Dillon 				 * page already exists in parent OR swap exists
1045588059beSMatthew Dillon 				 * for this location in the parent.  Destroy
1046588059beSMatthew Dillon 				 * the original page from the backing object.
1047588059beSMatthew Dillon 				 */
1048b18bfc3dSJohn Dyson 				vm_page_protect(p, VM_PROT_NONE);
10492fe6e4d7SDavid Greenman 				vm_page_free(p);
10502fe6e4d7SDavid Greenman 			} else {
1051588059beSMatthew Dillon 				/*
1052588059beSMatthew Dillon 				 * Page does not exist in parent, rename the
1053588059beSMatthew Dillon 				 * page.
1054588059beSMatthew Dillon 				 */
1055edd97f3aSJohn Dyson 				if ((p->queue - p->pc) == PQ_CACHE)
1056edd97f3aSJohn Dyson 					vm_page_deactivate(p);
1057edd97f3aSJohn Dyson 				else
10586e20a165SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1059edd97f3aSJohn Dyson 
1060edd97f3aSJohn Dyson 				vm_page_rename(p, object, new_pindex);
10611c7c3c6aSMatthew Dillon 				/* page automatically made dirty by rename */
10622fe6e4d7SDavid Greenman 			}
10632fe6e4d7SDavid Greenman 		}
1064588059beSMatthew Dillon 
1065588059beSMatthew Dillon 		/*
1066588059beSMatthew Dillon 		 * Destroy any swap assigned to the backing object at this
1067588059beSMatthew Dillon 		 * location.  This is an optimization.
1068588059beSMatthew Dillon 		 */
1069588059beSMatthew Dillon 		if (backing_object->type == OBJT_SWAP) {
1070588059beSMatthew Dillon 			swap_pager_freespace(backing_object, old_pindex, 1);
1071588059beSMatthew Dillon 		}
10722fe6e4d7SDavid Greenman 		p = next;
10732fe6e4d7SDavid Greenman 	}
1074010cf3b9SDavid Greenman 	backing_object->ref_count -= 2;
1075588059beSMatthew Dillon 
1076588059beSMatthew Dillon 	splx(s);
10772fe6e4d7SDavid Greenman }
10782fe6e4d7SDavid Greenman 
1079df8bae1dSRodney W. Grimes /*
1080df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1081df8bae1dSRodney W. Grimes  *
1082df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1083df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1084df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1085df8bae1dSRodney W. Grimes  */
108626f9a767SRodney W. Grimes void
108726f9a767SRodney W. Grimes vm_object_collapse(object)
108824a1cce3SDavid Greenman 	vm_object_t object;
1089df8bae1dSRodney W. Grimes 
1090df8bae1dSRodney W. Grimes {
109124a1cce3SDavid Greenman 	vm_object_t backing_object;
1092a316d390SJohn Dyson 	vm_ooffset_t backing_offset;
109324a1cce3SDavid Greenman 	vm_size_t size;
1094a316d390SJohn Dyson 	vm_pindex_t new_pindex, backing_offset_index;
109524a1cce3SDavid Greenman 	vm_page_t p, pp;
1096df8bae1dSRodney W. Grimes 
1097df8bae1dSRodney W. Grimes 	while (TRUE) {
1098df8bae1dSRodney W. Grimes 		/*
1099df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1100df8bae1dSRodney W. Grimes 		 *
11010d94caffSDavid Greenman 		 * The object exists and no pages in it are currently being paged
11020d94caffSDavid Greenman 		 * out.
1103df8bae1dSRodney W. Grimes 		 */
11042fe6e4d7SDavid Greenman 		if (object == NULL)
1105df8bae1dSRodney W. Grimes 			return;
1106df8bae1dSRodney W. Grimes 
1107b9921222SDavid Greenman 		/*
1108b9921222SDavid Greenman 		 * Make sure there is a backing object.
1109b9921222SDavid Greenman 		 */
111024a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
1111df8bae1dSRodney W. Grimes 			return;
1112df8bae1dSRodney W. Grimes 
1113f919ebdeSDavid Greenman 		/*
1114f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
111524a1cce3SDavid Greenman 		 * not collapsable.
1116f919ebdeSDavid Greenman 		 */
111724a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
111824a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
111924a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1120f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
112124a1cce3SDavid Greenman 		    object->handle != NULL ||
112224a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
112324a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
112424a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
11259b4814bbSDavid Greenman 			return;
112624a1cce3SDavid Greenman 		}
11279b4814bbSDavid Greenman 
1128f919ebdeSDavid Greenman 		if (object->paging_in_progress != 0 ||
1129f919ebdeSDavid Greenman 		    backing_object->paging_in_progress != 0) {
1130b9921222SDavid Greenman 			vm_object_qcollapse(object);
1131df8bae1dSRodney W. Grimes 			return;
1132df8bae1dSRodney W. Grimes 		}
1133f919ebdeSDavid Greenman 
113426f9a767SRodney W. Grimes 		/*
11350d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
11360d94caffSDavid Greenman 		 * the parent is the only reference to it) or (perhaps) remove
11370d94caffSDavid Greenman 		 * the parent's reference to it.
1138df8bae1dSRodney W. Grimes 		 */
1139df8bae1dSRodney W. Grimes 
114024a1cce3SDavid Greenman 		backing_offset = object->backing_object_offset;
1141a316d390SJohn Dyson 		backing_offset_index = OFF_TO_IDX(backing_offset);
1142df8bae1dSRodney W. Grimes 		size = object->size;
1143df8bae1dSRodney W. Grimes 
1144df8bae1dSRodney W. Grimes 		/*
11450d94caffSDavid Greenman 		 * If there is exactly one reference to the backing object, we
11460d94caffSDavid Greenman 		 * can collapse it into the parent.
1147df8bae1dSRodney W. Grimes 		 */
1148df8bae1dSRodney W. Grimes 
1149df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1150df8bae1dSRodney W. Grimes 
1151069e9bc1SDoug Rabson 			vm_object_set_flag(backing_object, OBJ_DEAD);
1152df8bae1dSRodney W. Grimes 			/*
1153df8bae1dSRodney W. Grimes 			 * We can collapse the backing object.
1154df8bae1dSRodney W. Grimes 			 *
11550d94caffSDavid Greenman 			 * Move all in-memory pages from backing_object to the
11560d94caffSDavid Greenman 			 * parent.  Pages that have been paged out will be
11570d94caffSDavid Greenman 			 * overwritten by any of the parent's pages that
11580d94caffSDavid Greenman 			 * shadow them.
1159df8bae1dSRodney W. Grimes 			 */
1160df8bae1dSRodney W. Grimes 
1161b18bfc3dSJohn Dyson 			while ((p = TAILQ_FIRST(&backing_object->memq)) != 0) {
11621c7c3c6aSMatthew Dillon 				if (vm_page_sleep_busy(p, TRUE, "vmocol"))
11631c7c3c6aSMatthew Dillon 					continue;
1164e69763a3SDoug Rabson 				vm_page_busy(p);
11651c7c3c6aSMatthew Dillon 				new_pindex = p->pindex - backing_offset_index;
1166df8bae1dSRodney W. Grimes 
1167df8bae1dSRodney W. Grimes 				/*
11680d94caffSDavid Greenman 				 * If the parent has a page here, or if this
11690d94caffSDavid Greenman 				 * page falls outside the parent, dispose of
11700d94caffSDavid Greenman 				 * it.
1171df8bae1dSRodney W. Grimes 				 *
1172df8bae1dSRodney W. Grimes 				 * Otherwise, move it as planned.
1173df8bae1dSRodney W. Grimes 				 */
1174df8bae1dSRodney W. Grimes 
1175a316d390SJohn Dyson 				if (p->pindex < backing_offset_index ||
1176a316d390SJohn Dyson 				    new_pindex >= size) {
1177f919ebdeSDavid Greenman 					vm_page_protect(p, VM_PROT_NONE);
1178df8bae1dSRodney W. Grimes 					vm_page_free(p);
1179df8bae1dSRodney W. Grimes 				} else {
1180a316d390SJohn Dyson 					pp = vm_page_lookup(object, new_pindex);
118124a1cce3SDavid Greenman 					if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object,
11821c7c3c6aSMatthew Dillon 					    new_pindex, NULL, NULL))) {
1183f919ebdeSDavid Greenman 						vm_page_protect(p, VM_PROT_NONE);
1184df8bae1dSRodney W. Grimes 						vm_page_free(p);
118526f9a767SRodney W. Grimes 					} else {
1186edd97f3aSJohn Dyson 						if ((p->queue - p->pc) == PQ_CACHE)
1187edd97f3aSJohn Dyson 							vm_page_deactivate(p);
1188edd97f3aSJohn Dyson 						else
11896e20a165SJohn Dyson 							vm_page_protect(p, VM_PROT_NONE);
1190a316d390SJohn Dyson 						vm_page_rename(p, object, new_pindex);
11911c7c3c6aSMatthew Dillon 						/* page automatically made dirty by rename */
1192df8bae1dSRodney W. Grimes 					}
1193df8bae1dSRodney W. Grimes 				}
1194df8bae1dSRodney W. Grimes 			}
1195df8bae1dSRodney W. Grimes 
1196df8bae1dSRodney W. Grimes 			/*
1197df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1198df8bae1dSRodney W. Grimes 			 */
1199df8bae1dSRodney W. Grimes 
120024a1cce3SDavid Greenman 			if (backing_object->type == OBJT_SWAP) {
1201d474eaaaSDoug Rabson 				vm_object_pip_add(backing_object, 1);
120224a1cce3SDavid Greenman 
120324a1cce3SDavid Greenman 				/*
12041c7c3c6aSMatthew Dillon 				 * scrap the paging_offset junk and do a
12051c7c3c6aSMatthew Dillon 				 * discrete copy.  This also removes major
12061c7c3c6aSMatthew Dillon 				 * assumptions about how the swap-pager
12071c7c3c6aSMatthew Dillon 				 * works from where it doesn't belong.  The
12081c7c3c6aSMatthew Dillon 				 * new swapper is able to optimize the
12091c7c3c6aSMatthew Dillon 				 * destroy-source case.
121024a1cce3SDavid Greenman 				 */
12111c7c3c6aSMatthew Dillon 
12121c7c3c6aSMatthew Dillon 				vm_object_pip_add(object, 1);
12131c7c3c6aSMatthew Dillon 				swap_pager_copy(
12141c7c3c6aSMatthew Dillon 				    backing_object,
12151c7c3c6aSMatthew Dillon 				    object,
12161c7c3c6aSMatthew Dillon 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1217f919ebdeSDavid Greenman 				vm_object_pip_wakeup(object);
1218c0503609SDavid Greenman 
1219f919ebdeSDavid Greenman 				vm_object_pip_wakeup(backing_object);
1220c0503609SDavid Greenman 			}
1221df8bae1dSRodney W. Grimes 			/*
1222df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
122324a1cce3SDavid Greenman 			 * Note that the reference to backing_object->backing_object
1224df8bae1dSRodney W. Grimes 			 * moves from within backing_object to within object.
1225df8bae1dSRodney W. Grimes 			 */
1226df8bae1dSRodney W. Grimes 
122724a1cce3SDavid Greenman 			TAILQ_REMOVE(&object->backing_object->shadow_head, object,
122824a1cce3SDavid Greenman 			    shadow_list);
1229eaf13dd7SJohn Dyson 			object->backing_object->shadow_count--;
1230eaf13dd7SJohn Dyson 			object->backing_object->generation++;
1231de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
123224a1cce3SDavid Greenman 				TAILQ_REMOVE(&backing_object->backing_object->shadow_head,
123324a1cce3SDavid Greenman 				    backing_object, shadow_list);
1234eaf13dd7SJohn Dyson 				backing_object->backing_object->shadow_count--;
1235eaf13dd7SJohn Dyson 				backing_object->backing_object->generation++;
1236de5f6a77SJohn Dyson 			}
123724a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
1238de5f6a77SJohn Dyson 			if (object->backing_object) {
123924a1cce3SDavid Greenman 				TAILQ_INSERT_TAIL(&object->backing_object->shadow_head,
124024a1cce3SDavid Greenman 				    object, shadow_list);
1241eaf13dd7SJohn Dyson 				object->backing_object->shadow_count++;
1242eaf13dd7SJohn Dyson 				object->backing_object->generation++;
1243de5f6a77SJohn Dyson 			}
12442fe6e4d7SDavid Greenman 
124524a1cce3SDavid Greenman 			object->backing_object_offset += backing_object->backing_object_offset;
1246df8bae1dSRodney W. Grimes 			/*
1247df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1248df8bae1dSRodney W. Grimes 			 *
12490d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
12500d94caffSDavid Greenman 			 * and no object references within it, all that is
12510d94caffSDavid Greenman 			 * necessary is to dispose of it.
1252df8bae1dSRodney W. Grimes 			 */
1253df8bae1dSRodney W. Grimes 
1254df8bae1dSRodney W. Grimes 			TAILQ_REMOVE(&vm_object_list, backing_object,
1255df8bae1dSRodney W. Grimes 			    object_list);
1256df8bae1dSRodney W. Grimes 			vm_object_count--;
1257df8bae1dSRodney W. Grimes 
125899448ed1SJohn Dyson 			zfree(obj_zone, backing_object);
1259df8bae1dSRodney W. Grimes 
1260df8bae1dSRodney W. Grimes 			object_collapses++;
12610d94caffSDavid Greenman 		} else {
126295e5e988SJohn Dyson 			vm_object_t new_backing_object;
1263df8bae1dSRodney W. Grimes 			/*
1264df8bae1dSRodney W. Grimes 			 * If all of the pages in the backing object are
12650d94caffSDavid Greenman 			 * shadowed by the parent object, the parent object no
12660d94caffSDavid Greenman 			 * longer has to shadow the backing object; it can
12670d94caffSDavid Greenman 			 * shadow the next one in the chain.
1268df8bae1dSRodney W. Grimes 			 *
12690d94caffSDavid Greenman 			 * The backing object must not be paged out - we'd have
12700d94caffSDavid Greenman 			 * to check all of the paged-out pages, as well.
1271df8bae1dSRodney W. Grimes 			 */
1272df8bae1dSRodney W. Grimes 
127324a1cce3SDavid Greenman 			if (backing_object->type != OBJT_DEFAULT) {
1274df8bae1dSRodney W. Grimes 				return;
1275df8bae1dSRodney W. Grimes 			}
1276df8bae1dSRodney W. Grimes 			/*
12770d94caffSDavid Greenman 			 * Should have a check for a 'small' number of pages
12780d94caffSDavid Greenman 			 * here.
1279df8bae1dSRodney W. Grimes 			 */
1280df8bae1dSRodney W. Grimes 
1281eaf13dd7SJohn Dyson 			for (p = TAILQ_FIRST(&backing_object->memq); p;
1282eaf13dd7SJohn Dyson 					p = TAILQ_NEXT(p, listq)) {
1283eaf13dd7SJohn Dyson 
1284a316d390SJohn Dyson 				new_pindex = p->pindex - backing_offset_index;
1285e69763a3SDoug Rabson 				vm_page_busy(p);
1286df8bae1dSRodney W. Grimes 
1287df8bae1dSRodney W. Grimes 				/*
12880d94caffSDavid Greenman 				 * If the parent has a page here, or if this
12890d94caffSDavid Greenman 				 * page falls outside the parent, keep going.
1290df8bae1dSRodney W. Grimes 				 *
12910d94caffSDavid Greenman 				 * Otherwise, the backing_object must be left in
12920d94caffSDavid Greenman 				 * the chain.
1293df8bae1dSRodney W. Grimes 				 */
1294df8bae1dSRodney W. Grimes 
1295a316d390SJohn Dyson 				if (p->pindex >= backing_offset_index &&
1296a316d390SJohn Dyson 					new_pindex <= size) {
129724a1cce3SDavid Greenman 
1298a316d390SJohn Dyson 					pp = vm_page_lookup(object, new_pindex);
129924a1cce3SDavid Greenman 
1300eaf13dd7SJohn Dyson 					if ((pp == NULL) || (pp->flags & PG_BUSY) || pp->busy) {
1301e69763a3SDoug Rabson 						vm_page_wakeup(p);
1302eaf13dd7SJohn Dyson 						return;
1303eaf13dd7SJohn Dyson 					}
1304eaf13dd7SJohn Dyson 
1305e69763a3SDoug Rabson 					vm_page_busy(pp);
1306eaf13dd7SJohn Dyson 					if ((pp->valid == 0) &&
13071c7c3c6aSMatthew Dillon 				   	    !vm_pager_has_page(object, new_pindex, NULL, NULL)) {
1308df8bae1dSRodney W. Grimes 						/*
13090d94caffSDavid Greenman 						 * Page still needed. Can't go any
13100d94caffSDavid Greenman 						 * further.
1311df8bae1dSRodney W. Grimes 						 */
1312e69763a3SDoug Rabson 						vm_page_wakeup(pp);
1313e69763a3SDoug Rabson 						vm_page_wakeup(p);
1314df8bae1dSRodney W. Grimes 						return;
1315df8bae1dSRodney W. Grimes 					}
1316e69763a3SDoug Rabson 					vm_page_wakeup(pp);
1317df8bae1dSRodney W. Grimes 				}
1318e69763a3SDoug Rabson 				vm_page_wakeup(p);
131924a1cce3SDavid Greenman 			}
1320df8bae1dSRodney W. Grimes 
1321df8bae1dSRodney W. Grimes 			/*
13220d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
13230d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
13240d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1325df8bae1dSRodney W. Grimes 			 */
1326df8bae1dSRodney W. Grimes 
132795e5e988SJohn Dyson 			TAILQ_REMOVE(&backing_object->shadow_head,
132824a1cce3SDavid Greenman 			    object, shadow_list);
1329eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
1330eaf13dd7SJohn Dyson 			backing_object->generation++;
133195e5e988SJohn Dyson 
133295e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
13338aef1712SMatthew Dillon 			if ((object->backing_object = new_backing_object) != NULL) {
133495e5e988SJohn Dyson 				vm_object_reference(new_backing_object);
133595e5e988SJohn Dyson 				TAILQ_INSERT_TAIL(&new_backing_object->shadow_head,
133624a1cce3SDavid Greenman 				    object, shadow_list);
1337eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1338eaf13dd7SJohn Dyson 				new_backing_object->generation++;
133995e5e988SJohn Dyson 				object->backing_object_offset +=
134095e5e988SJohn Dyson 					backing_object->backing_object_offset;
1341de5f6a77SJohn Dyson 			}
1342df8bae1dSRodney W. Grimes 
1343df8bae1dSRodney W. Grimes 			/*
13440d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
13450d94caffSDavid Greenman 			 * its ref_count was at least 2, it will not vanish;
1346eaf13dd7SJohn Dyson 			 * so we don't need to call vm_object_deallocate, but
1347eaf13dd7SJohn Dyson 			 * we do anyway.
1348df8bae1dSRodney W. Grimes 			 */
134995e5e988SJohn Dyson 			vm_object_deallocate(backing_object);
1350df8bae1dSRodney W. Grimes 			object_bypasses++;
1351df8bae1dSRodney W. Grimes 		}
1352df8bae1dSRodney W. Grimes 
1353df8bae1dSRodney W. Grimes 		/*
1354df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1355df8bae1dSRodney W. Grimes 		 */
1356df8bae1dSRodney W. Grimes 	}
1357df8bae1dSRodney W. Grimes }
1358df8bae1dSRodney W. Grimes 
1359df8bae1dSRodney W. Grimes /*
1360df8bae1dSRodney W. Grimes  *	vm_object_page_remove: [internal]
1361df8bae1dSRodney W. Grimes  *
1362df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
1363df8bae1dSRodney W. Grimes  *	object range from the object's list of pages.
1364df8bae1dSRodney W. Grimes  *
1365df8bae1dSRodney W. Grimes  *	The object must be locked.
1366df8bae1dSRodney W. Grimes  */
136726f9a767SRodney W. Grimes void
13687c1f6cedSDavid Greenman vm_object_page_remove(object, start, end, clean_only)
1369df8bae1dSRodney W. Grimes 	register vm_object_t object;
1370a316d390SJohn Dyson 	register vm_pindex_t start;
1371a316d390SJohn Dyson 	register vm_pindex_t end;
13727c1f6cedSDavid Greenman 	boolean_t clean_only;
1373df8bae1dSRodney W. Grimes {
1374df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
1375a316d390SJohn Dyson 	unsigned int size;
1376f5ef029eSPoul-Henning Kamp 	int all;
1377df8bae1dSRodney W. Grimes 
1378df8bae1dSRodney W. Grimes 	if (object == NULL)
1379df8bae1dSRodney W. Grimes 		return;
1380df8bae1dSRodney W. Grimes 
138195e5e988SJohn Dyson 	all = ((end == 0) && (start == 0));
138295e5e988SJohn Dyson 
1383d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
138426f9a767SRodney W. Grimes again:
138526f9a767SRodney W. Grimes 	size = end - start;
138695e5e988SJohn Dyson 	if (all || size > 4 || size >= object->size / 4) {
1387b18bfc3dSJohn Dyson 		for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1388b18bfc3dSJohn Dyson 			next = TAILQ_NEXT(p, listq);
138995e5e988SJohn Dyson 			if (all || ((start <= p->pindex) && (p->pindex < end))) {
1390bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1391bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1392ce65e68cSDavid Greenman 					if (!clean_only)
1393bd7e5f99SJohn Dyson 						p->valid = 0;
13940d94caffSDavid Greenman 					continue;
13950d94caffSDavid Greenman 				}
13960891ef4cSJohn Dyson 
1397b18bfc3dSJohn Dyson 				/*
1398b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1399b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1400b18bfc3dSJohn Dyson 				 */
1401ffc82b0aSJohn Dyson 
14021c7c3c6aSMatthew Dillon  				if (vm_page_sleep_busy(p, TRUE, "vmopar"))
140326f9a767SRodney W. Grimes  					goto again;
14040891ef4cSJohn Dyson 
14058f9110f6SJohn Dyson 				if (clean_only && p->valid) {
14067c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
14077c1f6cedSDavid Greenman 					if (p->valid & p->dirty)
14087c1f6cedSDavid Greenman 						continue;
14097c1f6cedSDavid Greenman 				}
1410eaf13dd7SJohn Dyson 
1411e69763a3SDoug Rabson 				vm_page_busy(p);
1412f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
1413df8bae1dSRodney W. Grimes 				vm_page_free(p);
141426f9a767SRodney W. Grimes 			}
141526f9a767SRodney W. Grimes 		}
141626f9a767SRodney W. Grimes 	} else {
141726f9a767SRodney W. Grimes 		while (size > 0) {
1418bd7e5f99SJohn Dyson 			if ((p = vm_page_lookup(object, start)) != 0) {
1419eaf13dd7SJohn Dyson 
1420bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1421bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1422ce65e68cSDavid Greenman 					if (!clean_only)
1423ce65e68cSDavid Greenman 						p->valid = 0;
1424bd7e5f99SJohn Dyson 					start += 1;
1425bd7e5f99SJohn Dyson 					size -= 1;
1426bd7e5f99SJohn Dyson 					continue;
14270d94caffSDavid Greenman 				}
1428eaf13dd7SJohn Dyson 
1429b18bfc3dSJohn Dyson 				/*
1430b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1431b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1432b18bfc3dSJohn Dyson 				 */
14331c7c3c6aSMatthew Dillon  				if (vm_page_sleep_busy(p, TRUE, "vmopar"))
143426f9a767SRodney W. Grimes 					goto again;
1435eaf13dd7SJohn Dyson 
14368f9110f6SJohn Dyson 				if (clean_only && p->valid) {
14377c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
1438bd7e5f99SJohn Dyson 					if (p->valid & p->dirty) {
1439bd7e5f99SJohn Dyson 						start += 1;
1440bd7e5f99SJohn Dyson 						size -= 1;
14417c1f6cedSDavid Greenman 						continue;
14427c1f6cedSDavid Greenman 					}
1443bd7e5f99SJohn Dyson 				}
1444eaf13dd7SJohn Dyson 
1445e69763a3SDoug Rabson 				vm_page_busy(p);
1446f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
144726f9a767SRodney W. Grimes 				vm_page_free(p);
144826f9a767SRodney W. Grimes 			}
1449a316d390SJohn Dyson 			start += 1;
1450a316d390SJohn Dyson 			size -= 1;
1451df8bae1dSRodney W. Grimes 		}
1452df8bae1dSRodney W. Grimes 	}
1453f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
1454c0503609SDavid Greenman }
1455df8bae1dSRodney W. Grimes 
1456df8bae1dSRodney W. Grimes /*
1457df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1458df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1459df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1460df8bae1dSRodney W. Grimes  *
1461df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1462df8bae1dSRodney W. Grimes  *
1463df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1464df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1465df8bae1dSRodney W. Grimes  *
1466df8bae1dSRodney W. Grimes  *	Parameters:
1467df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1468df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1469df8bae1dSRodney W. Grimes  *		next_object	Second object into coalesce
1470df8bae1dSRodney W. Grimes  *		next_offset	Offset into next_object
1471df8bae1dSRodney W. Grimes  *
1472df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
1473df8bae1dSRodney W. Grimes  *		next_size	Size of reference to next_object
1474df8bae1dSRodney W. Grimes  *
1475df8bae1dSRodney W. Grimes  *	Conditions:
1476df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1477df8bae1dSRodney W. Grimes  */
14780d94caffSDavid Greenman boolean_t
1479a316d390SJohn Dyson vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size)
1480df8bae1dSRodney W. Grimes 	register vm_object_t prev_object;
1481a316d390SJohn Dyson 	vm_pindex_t prev_pindex;
1482df8bae1dSRodney W. Grimes 	vm_size_t prev_size, next_size;
1483df8bae1dSRodney W. Grimes {
1484df8bae1dSRodney W. Grimes 	vm_size_t newsize;
1485df8bae1dSRodney W. Grimes 
1486df8bae1dSRodney W. Grimes 	if (prev_object == NULL) {
1487df8bae1dSRodney W. Grimes 		return (TRUE);
1488df8bae1dSRodney W. Grimes 	}
1489df8bae1dSRodney W. Grimes 
14904112823fSMatthew Dillon 	if (prev_object->type != OBJT_DEFAULT &&
14914112823fSMatthew Dillon 	    prev_object->type != OBJT_SWAP) {
149230dcfc09SJohn Dyson 		return (FALSE);
149330dcfc09SJohn Dyson 	}
149430dcfc09SJohn Dyson 
1495df8bae1dSRodney W. Grimes 	/*
1496df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1497df8bae1dSRodney W. Grimes 	 */
1498df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1499df8bae1dSRodney W. Grimes 
1500df8bae1dSRodney W. Grimes 	/*
15010d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
15020d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
15030d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1504df8bae1dSRodney W. Grimes 	 */
1505df8bae1dSRodney W. Grimes 
15068cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
1507df8bae1dSRodney W. Grimes 		return (FALSE);
1508df8bae1dSRodney W. Grimes 	}
1509a316d390SJohn Dyson 
1510a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
1511a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
15128cc7e047SJohn Dyson 
15138cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
15148cc7e047SJohn Dyson 	    (prev_object->size != prev_pindex + prev_size)) {
15158cc7e047SJohn Dyson 		return (FALSE);
15168cc7e047SJohn Dyson 	}
15178cc7e047SJohn Dyson 
1518df8bae1dSRodney W. Grimes 	/*
15190d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
15200d94caffSDavid Greenman 	 * deallocation.
1521df8bae1dSRodney W. Grimes 	 */
1522df8bae1dSRodney W. Grimes 
1523df8bae1dSRodney W. Grimes 	vm_object_page_remove(prev_object,
1524a316d390SJohn Dyson 	    prev_pindex + prev_size,
1525a316d390SJohn Dyson 	    prev_pindex + prev_size + next_size, FALSE);
1526df8bae1dSRodney W. Grimes 
1527df8bae1dSRodney W. Grimes 	/*
1528df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1529df8bae1dSRodney W. Grimes 	 */
1530a316d390SJohn Dyson 	newsize = prev_pindex + prev_size + next_size;
1531df8bae1dSRodney W. Grimes 	if (newsize > prev_object->size)
1532df8bae1dSRodney W. Grimes 		prev_object->size = newsize;
1533df8bae1dSRodney W. Grimes 
1534df8bae1dSRodney W. Grimes 	return (TRUE);
1535df8bae1dSRodney W. Grimes }
1536df8bae1dSRodney W. Grimes 
1537c7c34a24SBruce Evans #include "opt_ddb.h"
1538c3cb3e12SDavid Greenman #ifdef DDB
1539c7c34a24SBruce Evans #include <sys/kernel.h>
1540c7c34a24SBruce Evans 
1541c7c34a24SBruce Evans #include <machine/cons.h>
1542c7c34a24SBruce Evans 
1543c7c34a24SBruce Evans #include <ddb/ddb.h>
1544c7c34a24SBruce Evans 
1545c7c34a24SBruce Evans static int	_vm_object_in_map __P((vm_map_t map, vm_object_t object,
1546c7c34a24SBruce Evans 				       vm_map_entry_t entry));
1547c7c34a24SBruce Evans static int	vm_object_in_map __P((vm_object_t object));
1548c3cb3e12SDavid Greenman 
1549cac597e4SBruce Evans static int
1550a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry)
1551a1f6d91cSDavid Greenman 	vm_map_t map;
1552a1f6d91cSDavid Greenman 	vm_object_t object;
1553a1f6d91cSDavid Greenman 	vm_map_entry_t entry;
1554a1f6d91cSDavid Greenman {
1555a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1556a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1557a1f6d91cSDavid Greenman 	vm_object_t obj;
1558a1f6d91cSDavid Greenman 	int entcount;
1559a1f6d91cSDavid Greenman 
1560a1f6d91cSDavid Greenman 	if (map == 0)
1561a1f6d91cSDavid Greenman 		return 0;
1562a1f6d91cSDavid Greenman 
1563a1f6d91cSDavid Greenman 	if (entry == 0) {
1564a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1565a1f6d91cSDavid Greenman 		entcount = map->nentries;
1566a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1567a1f6d91cSDavid Greenman 			if( _vm_object_in_map(map, object, tmpe)) {
1568a1f6d91cSDavid Greenman 				return 1;
1569a1f6d91cSDavid Greenman 			}
1570a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1571a1f6d91cSDavid Greenman 		}
15729fdfe602SMatthew Dillon 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
15739fdfe602SMatthew Dillon 		tmpm = entry->object.sub_map;
1574a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1575a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1576a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1577a1f6d91cSDavid Greenman 			if( _vm_object_in_map(tmpm, object, tmpe)) {
1578a1f6d91cSDavid Greenman 				return 1;
1579a1f6d91cSDavid Greenman 			}
1580a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1581a1f6d91cSDavid Greenman 		}
15828aef1712SMatthew Dillon 	} else if ((obj = entry->object.vm_object) != NULL) {
158324a1cce3SDavid Greenman 		for(; obj; obj=obj->backing_object)
1584a1f6d91cSDavid Greenman 			if( obj == object) {
1585a1f6d91cSDavid Greenman 				return 1;
1586a1f6d91cSDavid Greenman 			}
1587a1f6d91cSDavid Greenman 	}
1588a1f6d91cSDavid Greenman 	return 0;
1589a1f6d91cSDavid Greenman }
1590a1f6d91cSDavid Greenman 
1591cac597e4SBruce Evans static int
1592a1f6d91cSDavid Greenman vm_object_in_map( object)
1593a1f6d91cSDavid Greenman 	vm_object_t object;
1594a1f6d91cSDavid Greenman {
1595a1f6d91cSDavid Greenman 	struct proc *p;
15961b67ec6dSJeffrey Hsu 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1597a1f6d91cSDavid Greenman 		if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1598a1f6d91cSDavid Greenman 			continue;
1599a1f6d91cSDavid Greenman 		if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1600a1f6d91cSDavid Greenman 			return 1;
1601a1f6d91cSDavid Greenman 	}
1602a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kernel_map, object, 0))
1603a1f6d91cSDavid Greenman 		return 1;
1604a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kmem_map, object, 0))
1605a1f6d91cSDavid Greenman 		return 1;
1606a1f6d91cSDavid Greenman 	if( _vm_object_in_map( pager_map, object, 0))
1607a1f6d91cSDavid Greenman 		return 1;
1608a1f6d91cSDavid Greenman 	if( _vm_object_in_map( buffer_map, object, 0))
1609a1f6d91cSDavid Greenman 		return 1;
1610a1f6d91cSDavid Greenman 	if( _vm_object_in_map( io_map, object, 0))
1611a1f6d91cSDavid Greenman 		return 1;
1612a1f6d91cSDavid Greenman 	if( _vm_object_in_map( phys_map, object, 0))
1613a1f6d91cSDavid Greenman 		return 1;
1614a1f6d91cSDavid Greenman 	if( _vm_object_in_map( mb_map, object, 0))
1615a1f6d91cSDavid Greenman 		return 1;
1616a1f6d91cSDavid Greenman 	if( _vm_object_in_map( u_map, object, 0))
1617a1f6d91cSDavid Greenman 		return 1;
1618a1f6d91cSDavid Greenman 	return 0;
1619a1f6d91cSDavid Greenman }
1620a1f6d91cSDavid Greenman 
1621c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
1622f708ef1bSPoul-Henning Kamp {
1623a1f6d91cSDavid Greenman 	vm_object_t object;
1624a1f6d91cSDavid Greenman 
1625a1f6d91cSDavid Greenman 	/*
1626a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
1627a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
1628a1f6d91cSDavid Greenman 	 */
1629b18bfc3dSJohn Dyson 	for (object = TAILQ_FIRST(&vm_object_list);
1630a1f6d91cSDavid Greenman 			object != NULL;
1631b18bfc3dSJohn Dyson 			object = TAILQ_NEXT(object, object_list)) {
163224a1cce3SDavid Greenman 		if (object->handle == NULL &&
163324a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1634a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
1635c7c34a24SBruce Evans 				db_printf("vmochk: internal obj has zero ref count: %d\n",
1636a1f6d91cSDavid Greenman 					object->size);
1637a1f6d91cSDavid Greenman 			}
1638a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
1639fc62ef1fSBruce Evans 				db_printf(
1640fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
1641fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
1642fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
1643fc62ef1fSBruce Evans 				    (u_long)object->size,
1644fc62ef1fSBruce Evans 				    (void *)object->backing_object);
1645a1f6d91cSDavid Greenman 			}
1646a1f6d91cSDavid Greenman 		}
1647a1f6d91cSDavid Greenman 	}
1648a1f6d91cSDavid Greenman }
1649a1f6d91cSDavid Greenman 
165026f9a767SRodney W. Grimes /*
1651df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
1652df8bae1dSRodney W. Grimes  */
1653c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
1654df8bae1dSRodney W. Grimes {
1655c7c34a24SBruce Evans 	/* XXX convert args. */
1656c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
1657c7c34a24SBruce Evans 	boolean_t full = have_addr;
1658c7c34a24SBruce Evans 
1659df8bae1dSRodney W. Grimes 	register vm_page_t p;
1660df8bae1dSRodney W. Grimes 
1661c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
1662c7c34a24SBruce Evans #define	count	was_count
1663c7c34a24SBruce Evans 
1664df8bae1dSRodney W. Grimes 	register int count;
1665df8bae1dSRodney W. Grimes 
1666df8bae1dSRodney W. Grimes 	if (object == NULL)
1667df8bae1dSRodney W. Grimes 		return;
1668df8bae1dSRodney W. Grimes 
1669eb95adefSBruce Evans 	db_iprintf(
1670eb95adefSBruce Evans 	    "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
1671eb95adefSBruce Evans 	    object, (int)object->type, (u_long)object->size,
1672eb95adefSBruce Evans 	    object->resident_page_count, object->ref_count, object->flags);
1673eb95adefSBruce Evans 	/*
16741c7c3c6aSMatthew Dillon 	 * XXX no %qd in kernel.  Truncate object->backing_object_offset.
1675eb95adefSBruce Evans 	 */
16761c7c3c6aSMatthew Dillon 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
16771c7c3c6aSMatthew Dillon 	    object->shadow_count,
1678eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
1679eb95adefSBruce Evans 	    object->backing_object, (long)object->backing_object_offset);
1680df8bae1dSRodney W. Grimes 
1681df8bae1dSRodney W. Grimes 	if (!full)
1682df8bae1dSRodney W. Grimes 		return;
1683df8bae1dSRodney W. Grimes 
1684c7c34a24SBruce Evans 	db_indent += 2;
1685df8bae1dSRodney W. Grimes 	count = 0;
1686b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = TAILQ_NEXT(p, listq)) {
1687df8bae1dSRodney W. Grimes 		if (count == 0)
1688c7c34a24SBruce Evans 			db_iprintf("memory:=");
1689df8bae1dSRodney W. Grimes 		else if (count == 6) {
1690c7c34a24SBruce Evans 			db_printf("\n");
1691c7c34a24SBruce Evans 			db_iprintf(" ...");
1692df8bae1dSRodney W. Grimes 			count = 0;
1693df8bae1dSRodney W. Grimes 		} else
1694c7c34a24SBruce Evans 			db_printf(",");
1695df8bae1dSRodney W. Grimes 		count++;
1696df8bae1dSRodney W. Grimes 
1697c7c34a24SBruce Evans 		db_printf("(off=0x%lx,page=0x%lx)",
1698a316d390SJohn Dyson 		    (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
1699df8bae1dSRodney W. Grimes 	}
1700df8bae1dSRodney W. Grimes 	if (count != 0)
1701c7c34a24SBruce Evans 		db_printf("\n");
1702c7c34a24SBruce Evans 	db_indent -= 2;
1703df8bae1dSRodney W. Grimes }
17045070c7f8SJohn Dyson 
1705c7c34a24SBruce Evans /* XXX. */
1706c7c34a24SBruce Evans #undef count
1707c7c34a24SBruce Evans 
1708c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
17095070c7f8SJohn Dyson void
1710c7c34a24SBruce Evans vm_object_print(addr, have_addr, count, modif)
1711ecbb00a2SDoug Rabson         /* db_expr_t */ long addr;
1712c7c34a24SBruce Evans 	boolean_t have_addr;
1713ecbb00a2SDoug Rabson 	/* db_expr_t */ long count;
1714c7c34a24SBruce Evans 	char *modif;
1715c7c34a24SBruce Evans {
1716c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
1717c7c34a24SBruce Evans }
1718c7c34a24SBruce Evans 
1719c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
17205070c7f8SJohn Dyson {
17215070c7f8SJohn Dyson 	vm_object_t object;
17225070c7f8SJohn Dyson 	int nl = 0;
17235070c7f8SJohn Dyson 	int c;
17245070c7f8SJohn Dyson 	for (object = TAILQ_FIRST(&vm_object_list);
17255070c7f8SJohn Dyson 			object != NULL;
17265070c7f8SJohn Dyson 			object = TAILQ_NEXT(object, object_list)) {
17275070c7f8SJohn Dyson 		vm_pindex_t idx, fidx;
17285070c7f8SJohn Dyson 		vm_pindex_t osize;
17295070c7f8SJohn Dyson 		vm_offset_t pa = -1, padiff;
17305070c7f8SJohn Dyson 		int rcount;
17315070c7f8SJohn Dyson 		vm_page_t m;
17325070c7f8SJohn Dyson 
1733fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
17345070c7f8SJohn Dyson 		if ( nl > 18) {
17355070c7f8SJohn Dyson 			c = cngetc();
17365070c7f8SJohn Dyson 			if (c != ' ')
17375070c7f8SJohn Dyson 				return;
17385070c7f8SJohn Dyson 			nl = 0;
17395070c7f8SJohn Dyson 		}
17405070c7f8SJohn Dyson 		nl++;
17415070c7f8SJohn Dyson 		rcount = 0;
17425070c7f8SJohn Dyson 		fidx = 0;
17435070c7f8SJohn Dyson 		osize = object->size;
17445070c7f8SJohn Dyson 		if (osize > 128)
17455070c7f8SJohn Dyson 			osize = 128;
17465070c7f8SJohn Dyson 		for(idx=0;idx<osize;idx++) {
17475070c7f8SJohn Dyson 			m = vm_page_lookup(object, idx);
17485070c7f8SJohn Dyson 			if (m == NULL) {
17495070c7f8SJohn Dyson 				if (rcount) {
17505070c7f8SJohn Dyson 					db_printf(" index(%d)run(%d)pa(0x%x)\n",
17515070c7f8SJohn Dyson 						fidx, rcount, pa);
17525070c7f8SJohn Dyson 					if ( nl > 18) {
17535070c7f8SJohn Dyson 						c = cngetc();
17545070c7f8SJohn Dyson 						if (c != ' ')
17555070c7f8SJohn Dyson 							return;
17565070c7f8SJohn Dyson 						nl = 0;
17575070c7f8SJohn Dyson 					}
17585070c7f8SJohn Dyson 					nl++;
17595070c7f8SJohn Dyson 					rcount = 0;
17605070c7f8SJohn Dyson 				}
17615070c7f8SJohn Dyson 				continue;
17625070c7f8SJohn Dyson 			}
17635070c7f8SJohn Dyson 
17645070c7f8SJohn Dyson 
17655070c7f8SJohn Dyson 			if (rcount &&
17665070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
17675070c7f8SJohn Dyson 				++rcount;
17685070c7f8SJohn Dyson 				continue;
17695070c7f8SJohn Dyson 			}
17705070c7f8SJohn Dyson 			if (rcount) {
17715070c7f8SJohn Dyson 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
17725070c7f8SJohn Dyson 				padiff >>= PAGE_SHIFT;
17735070c7f8SJohn Dyson 				padiff &= PQ_L2_MASK;
17745070c7f8SJohn Dyson 				if (padiff == 0) {
17755070c7f8SJohn Dyson 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
17765070c7f8SJohn Dyson 					++rcount;
17775070c7f8SJohn Dyson 					continue;
17785070c7f8SJohn Dyson 				}
17795070c7f8SJohn Dyson 				db_printf(" index(%d)run(%d)pa(0x%x)", fidx, rcount, pa);
17805070c7f8SJohn Dyson 				db_printf("pd(%d)\n", padiff);
17815070c7f8SJohn Dyson 				if ( nl > 18) {
17825070c7f8SJohn Dyson 					c = cngetc();
17835070c7f8SJohn Dyson 					if (c != ' ')
17845070c7f8SJohn Dyson 						return;
17855070c7f8SJohn Dyson 					nl = 0;
17865070c7f8SJohn Dyson 				}
17875070c7f8SJohn Dyson 				nl++;
17885070c7f8SJohn Dyson 			}
17895070c7f8SJohn Dyson 			fidx = idx;
17905070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
17915070c7f8SJohn Dyson 			rcount = 1;
17925070c7f8SJohn Dyson 		}
17935070c7f8SJohn Dyson 		if (rcount) {
17945070c7f8SJohn Dyson 			db_printf(" index(%d)run(%d)pa(0x%x)\n", fidx, rcount, pa);
17955070c7f8SJohn Dyson 			if ( nl > 18) {
17965070c7f8SJohn Dyson 				c = cngetc();
17975070c7f8SJohn Dyson 				if (c != ' ')
17985070c7f8SJohn Dyson 					return;
17995070c7f8SJohn Dyson 				nl = 0;
18005070c7f8SJohn Dyson 			}
18015070c7f8SJohn Dyson 			nl++;
18025070c7f8SJohn Dyson 		}
18035070c7f8SJohn Dyson 	}
18045070c7f8SJohn Dyson }
1805c3cb3e12SDavid Greenman #endif /* DDB */
1806