xref: /freebsd/sys/vm/vm_object.c (revision 73000556e86d11a7b30c264ef306f424c665075f)
160727d8bSWarner Losh /*-
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  * 4. Neither the name of the University nor the names of its contributors
17df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18df8bae1dSRodney W. Grimes  *    without specific prior written permission.
19df8bae1dSRodney W. Grimes  *
20df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
31df8bae1dSRodney W. Grimes  *
323c4dd356SDavid Greenman  *	from: @(#)vm_object.c	8.5 (Berkeley) 3/22/94
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *
35df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36df8bae1dSRodney W. Grimes  * All rights reserved.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
41df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
42df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
43df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
44df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53df8bae1dSRodney W. Grimes  *  School of Computer Science
54df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
55df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
56df8bae1dSRodney W. Grimes  *
57df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
58df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
59df8bae1dSRodney W. Grimes  */
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes /*
62df8bae1dSRodney W. Grimes  *	Virtual memory object module.
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes 
65874651b1SDavid E. O'Brien #include <sys/cdefs.h>
66874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
67874651b1SDavid E. O'Brien 
68df8bae1dSRodney W. Grimes #include <sys/param.h>
69df8bae1dSRodney W. Grimes #include <sys/systm.h>
70fb919e4dSMark Murray #include <sys/lock.h>
71867a482dSJohn Dyson #include <sys/mman.h>
72cf2819ccSJohn Dyson #include <sys/mount.h>
73b9b7a4beSMatthew Dillon #include <sys/kernel.h>
74b9b7a4beSMatthew Dillon #include <sys/sysctl.h>
751b367556SJason Evans #include <sys/mutex.h>
76fb919e4dSMark Murray #include <sys/proc.h>		/* for curproc, pageproc */
77fb919e4dSMark Murray #include <sys/socket.h>
78fb919e4dSMark Murray #include <sys/vnode.h>
79fb919e4dSMark Murray #include <sys/vmmeter.h>
801005a129SJohn Baldwin #include <sys/sx.h>
81df8bae1dSRodney W. Grimes 
82df8bae1dSRodney W. Grimes #include <vm/vm.h>
83efeaf95aSDavid Greenman #include <vm/vm_param.h>
84efeaf95aSDavid Greenman #include <vm/pmap.h>
85efeaf95aSDavid Greenman #include <vm/vm_map.h>
86efeaf95aSDavid Greenman #include <vm/vm_object.h>
87df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
8826f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
890d94caffSDavid Greenman #include <vm/vm_pager.h>
9005f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
91a1f6d91cSDavid Greenman #include <vm/vm_kern.h>
92efeaf95aSDavid Greenman #include <vm/vm_extern.h>
93670d17b5SJeff Roberson #include <vm/uma.h>
9426f9a767SRodney W. Grimes 
95b9b7a4beSMatthew Dillon #define EASY_SCAN_FACTOR       8
96b9b7a4beSMatthew Dillon 
97b9b7a4beSMatthew Dillon #define MSYNC_FLUSH_HARDSEQ	0x01
98b9b7a4beSMatthew Dillon #define MSYNC_FLUSH_SOFTSEQ	0x02
99b9b7a4beSMatthew Dillon 
100b9b7a4beSMatthew Dillon /*
101b9b7a4beSMatthew Dillon  * msync / VM object flushing optimizations
102b9b7a4beSMatthew Dillon  */
103b9b7a4beSMatthew Dillon static int msync_flush_flags = MSYNC_FLUSH_HARDSEQ | MSYNC_FLUSH_SOFTSEQ;
104b9b7a4beSMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, msync_flush_flags,
105b9b7a4beSMatthew Dillon         CTLFLAG_RW, &msync_flush_flags, 0, "");
106b9b7a4beSMatthew Dillon 
107c53f7aceSDag-Erling Smørgrav static int old_msync;
108c53f7aceSDag-Erling Smørgrav SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0,
109c53f7aceSDag-Erling Smørgrav     "Use old (insecure) msync behavior");
110c53f7aceSDag-Erling Smørgrav 
111b9b7a4beSMatthew Dillon static void	vm_object_qcollapse(vm_object_t object);
112b9b7a4beSMatthew Dillon static int	vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags);
11302dd8331SAlan Cox static void	vm_object_vndeallocate(vm_object_t object);
114f6b04d2bSDavid Greenman 
115df8bae1dSRodney W. Grimes /*
116df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
117df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
118df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
119df8bae1dSRodney W. Grimes  *
120df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
121df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
122df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
123df8bae1dSRodney W. Grimes  *
124df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
125df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
126df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
127df8bae1dSRodney W. Grimes  *	lock.
128df8bae1dSRodney W. Grimes  *
129df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
130df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
131df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
132df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
133df8bae1dSRodney W. Grimes  *
134df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
135df8bae1dSRodney W. Grimes  *	modified after time of creation are:
136df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
137df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
138df8bae1dSRodney W. Grimes  *
139df8bae1dSRodney W. Grimes  */
140df8bae1dSRodney W. Grimes 
14128f8db14SBruce Evans struct object_q vm_object_list;
142a5698387SAlan Cox struct mtx vm_object_list_mtx;	/* lock for object list and count */
143cccf11b8SAlan Cox 
144cccf11b8SAlan Cox struct vm_object kernel_object_store;
145cccf11b8SAlan Cox struct vm_object kmem_object_store;
146df8bae1dSRodney W. Grimes 
147604c2bbcSAlan Cox SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0, "VM object stats");
148604c2bbcSAlan Cox 
149f708ef1bSPoul-Henning Kamp static long object_collapses;
150604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD,
151604c2bbcSAlan Cox     &object_collapses, 0, "VM object collapses");
152604c2bbcSAlan Cox 
153f708ef1bSPoul-Henning Kamp static long object_bypasses;
154604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD,
155604c2bbcSAlan Cox     &object_bypasses, 0, "VM object bypasses");
156dad740e9SAlan Cox 
157dad740e9SAlan Cox /*
158dad740e9SAlan Cox  * next_index determines the page color that is assigned to the next
159dad740e9SAlan Cox  * allocated object.  Accesses to next_index are not synchronized
160dad740e9SAlan Cox  * because the effects of two or more object allocations using
161dad740e9SAlan Cox  * next_index simultaneously are inconsequential.  At any given time,
162dad740e9SAlan Cox  * numerous objects have the same page color.
163dad740e9SAlan Cox  */
164dcc5840eSAlan Cox static int next_index;
165dad740e9SAlan Cox 
166670d17b5SJeff Roberson static uma_zone_t obj_zone;
1678355f576SJeff Roberson 
168b23f72e9SBrian Feldman static int vm_object_zinit(void *mem, int size, int flags);
1698355f576SJeff Roberson 
1708355f576SJeff Roberson #ifdef INVARIANTS
1718355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg);
1728355f576SJeff Roberson 
1738355f576SJeff Roberson static void
1748355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg)
1758355f576SJeff Roberson {
1768355f576SJeff Roberson 	vm_object_t object;
1778355f576SJeff Roberson 
1788355f576SJeff Roberson 	object = (vm_object_t)mem;
17943186e53SAlan Cox 	KASSERT(TAILQ_EMPTY(&object->memq),
18043186e53SAlan Cox 	    ("object %p has resident pages",
18143186e53SAlan Cox 	    object));
1828355f576SJeff Roberson 	KASSERT(object->paging_in_progress == 0,
1838355f576SJeff Roberson 	    ("object %p paging_in_progress = %d",
1848355f576SJeff Roberson 	    object, object->paging_in_progress));
1858355f576SJeff Roberson 	KASSERT(object->resident_page_count == 0,
1868355f576SJeff Roberson 	    ("object %p resident_page_count = %d",
1878355f576SJeff Roberson 	    object, object->resident_page_count));
1888355f576SJeff Roberson 	KASSERT(object->shadow_count == 0,
1898355f576SJeff Roberson 	    ("object %p shadow_count = %d",
1908355f576SJeff Roberson 	    object, object->shadow_count));
1918355f576SJeff Roberson }
1928355f576SJeff Roberson #endif
1938355f576SJeff Roberson 
194b23f72e9SBrian Feldman static int
195b23f72e9SBrian Feldman vm_object_zinit(void *mem, int size, int flags)
1968355f576SJeff Roberson {
1978355f576SJeff Roberson 	vm_object_t object;
1988355f576SJeff Roberson 
1998355f576SJeff Roberson 	object = (vm_object_t)mem;
200e3a9e1b2SAlan Cox 	bzero(&object->mtx, sizeof(object->mtx));
2015285558aSAlan Cox 	VM_OBJECT_LOCK_INIT(object, "standard object");
2028355f576SJeff Roberson 
2038355f576SJeff Roberson 	/* These are true for any object that has been freed */
2048355f576SJeff Roberson 	object->paging_in_progress = 0;
2058355f576SJeff Roberson 	object->resident_page_count = 0;
2068355f576SJeff Roberson 	object->shadow_count = 0;
207b23f72e9SBrian Feldman 	return (0);
2088355f576SJeff Roberson }
209df8bae1dSRodney W. Grimes 
2103075778bSJohn Dyson void
2116395da54SIan Dowse _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
212df8bae1dSRodney W. Grimes {
213cf3e6e48SAlan Cox 	int incr;
2140cddd8f0SMatthew Dillon 
215df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
2161c500307SAlan Cox 	LIST_INIT(&object->shadow_head);
217a1f6d91cSDavid Greenman 
218b86ec922SMatthew Dillon 	object->root = NULL;
21924a1cce3SDavid Greenman 	object->type = type;
220df8bae1dSRodney W. Grimes 	object->size = size;
221b881da26SAlan Cox 	object->generation = 1;
222a1f6d91cSDavid Greenman 	object->ref_count = 1;
22324a1cce3SDavid Greenman 	object->flags = 0;
22460517fd1SJohn Baldwin 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
2253471677cSAlan Cox 		object->flags = OBJ_ONEMAPPING;
226ef39c05bSAlexander Leidinger 	incr = PQ_MAXLENGTH;
227ef39c05bSAlexander Leidinger 	if (size <= incr)
22899448ed1SJohn Dyson 		incr = size;
229dcc5840eSAlan Cox 	object->pg_color = next_index;
230ef39c05bSAlexander Leidinger 	next_index = (object->pg_color + incr) & PQ_COLORMASK;
23124a1cce3SDavid Greenman 	object->handle = NULL;
23224a1cce3SDavid Greenman 	object->backing_object = NULL;
233a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
234a1f6d91cSDavid Greenman 
235a5698387SAlan Cox 	mtx_lock(&vm_object_list_mtx);
23660517fd1SJohn Baldwin 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
237a5698387SAlan Cox 	mtx_unlock(&vm_object_list_mtx);
238df8bae1dSRodney W. Grimes }
239df8bae1dSRodney W. Grimes 
240df8bae1dSRodney W. Grimes /*
24126f9a767SRodney W. Grimes  *	vm_object_init:
24226f9a767SRodney W. Grimes  *
24326f9a767SRodney W. Grimes  *	Initialize the VM objects module.
24426f9a767SRodney W. Grimes  */
24526f9a767SRodney W. Grimes void
2461b40f8c0SMatthew Dillon vm_object_init(void)
24726f9a767SRodney W. Grimes {
24826f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
2496008862bSJohn Baldwin 	mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
2500217125fSDavid Greenman 
2515285558aSAlan Cox 	VM_OBJECT_LOCK_INIT(&kernel_object_store, "kernel object");
252a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
25326f9a767SRodney W. Grimes 	    kernel_object);
25426f9a767SRodney W. Grimes 
2555285558aSAlan Cox 	VM_OBJECT_LOCK_INIT(&kmem_object_store, "kmem object");
256a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
25726f9a767SRodney W. Grimes 	    kmem_object);
258ed6a7863SAlan Cox 
2598dbca793STor Egge 	/*
2608dbca793STor Egge 	 * The lock portion of struct vm_object must be type stable due
2618dbca793STor Egge 	 * to vm_pageout_fallback_object_lock locking a vm object
2628dbca793STor Egge 	 * without holding any references to it.
2638dbca793STor Egge 	 */
2648355f576SJeff Roberson 	obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
2658355f576SJeff Roberson #ifdef INVARIANTS
2668355f576SJeff Roberson 	    vm_object_zdtor,
2678355f576SJeff Roberson #else
2688355f576SJeff Roberson 	    NULL,
2698355f576SJeff Roberson #endif
270f3c625e4SJeff Roberson 	    vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE);
27199448ed1SJohn Dyson }
27299448ed1SJohn Dyson 
27399448ed1SJohn Dyson void
2741b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits)
2751b40f8c0SMatthew Dillon {
2765440b5a9SAlan Cox 
277d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
278b06805adSJake Burkholder 	object->flags &= ~bits;
2791b40f8c0SMatthew Dillon }
2801b40f8c0SMatthew Dillon 
2811b40f8c0SMatthew Dillon void
2821b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i)
2831b40f8c0SMatthew Dillon {
284f279b88dSAlan Cox 
285d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
286b06805adSJake Burkholder 	object->paging_in_progress += i;
2871b40f8c0SMatthew Dillon }
2881b40f8c0SMatthew Dillon 
2891b40f8c0SMatthew Dillon void
2901b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i)
2911b40f8c0SMatthew Dillon {
292d647a0edSAlan Cox 
2930fa05eaeSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
294b06805adSJake Burkholder 	object->paging_in_progress -= i;
2951b40f8c0SMatthew Dillon }
2961b40f8c0SMatthew Dillon 
2971b40f8c0SMatthew Dillon void
2981b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object)
2991b40f8c0SMatthew Dillon {
300f279b88dSAlan Cox 
301d647a0edSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
302b06805adSJake Burkholder 	object->paging_in_progress--;
3031b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
3041b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3051b40f8c0SMatthew Dillon 		wakeup(object);
3061b40f8c0SMatthew Dillon 	}
3071b40f8c0SMatthew Dillon }
3081b40f8c0SMatthew Dillon 
3091b40f8c0SMatthew Dillon void
3101b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i)
3111b40f8c0SMatthew Dillon {
312d647a0edSAlan Cox 
3130d420ad3SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3141b40f8c0SMatthew Dillon 	if (i)
315b06805adSJake Burkholder 		object->paging_in_progress -= i;
3161b40f8c0SMatthew Dillon 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
3171b40f8c0SMatthew Dillon 		vm_object_clear_flag(object, OBJ_PIPWNT);
3181b40f8c0SMatthew Dillon 		wakeup(object);
3191b40f8c0SMatthew Dillon 	}
3201b40f8c0SMatthew Dillon }
3211b40f8c0SMatthew Dillon 
3221b40f8c0SMatthew Dillon void
3231b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid)
3241b40f8c0SMatthew Dillon {
3251ca58953SAlan Cox 
3261ca58953SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3271ca58953SAlan Cox 	while (object->paging_in_progress) {
3281ca58953SAlan Cox 		object->flags |= OBJ_PIPWNT;
3291ca58953SAlan Cox 		msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0);
3301ca58953SAlan Cox 	}
3311b40f8c0SMatthew Dillon }
3321b40f8c0SMatthew Dillon 
33326f9a767SRodney W. Grimes /*
33426f9a767SRodney W. Grimes  *	vm_object_allocate:
33526f9a767SRodney W. Grimes  *
33626f9a767SRodney W. Grimes  *	Returns a new object with the given size.
33726f9a767SRodney W. Grimes  */
33826f9a767SRodney W. Grimes vm_object_t
3396395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size)
34026f9a767SRodney W. Grimes {
34190688d13SAlan Cox 	vm_object_t object;
34290688d13SAlan Cox 
34390688d13SAlan Cox 	object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK);
34490688d13SAlan Cox 	_vm_object_allocate(type, size, object);
34590688d13SAlan Cox 	return (object);
34626f9a767SRodney W. Grimes }
34726f9a767SRodney W. Grimes 
34826f9a767SRodney W. Grimes 
34926f9a767SRodney W. Grimes /*
350df8bae1dSRodney W. Grimes  *	vm_object_reference:
351df8bae1dSRodney W. Grimes  *
35215347817SAlan Cox  *	Gets another reference to the given object.  Note: OBJ_DEAD
35315347817SAlan Cox  *	objects can be referenced during final cleaning.
354df8bae1dSRodney W. Grimes  */
3556476c0d2SJohn Dyson void
3561b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object)
357df8bae1dSRodney W. Grimes {
35882f9defeSAlan Cox 	struct vnode *vp;
35982f9defeSAlan Cox 
360df8bae1dSRodney W. Grimes 	if (object == NULL)
361df8bae1dSRodney W. Grimes 		return;
36215347817SAlan Cox 	VM_OBJECT_LOCK(object);
363df8bae1dSRodney W. Grimes 	object->ref_count++;
36447221757SJohn Dyson 	if (object->type == OBJT_VNODE) {
365c05e22d4SJeff Roberson 		int vfslocked;
366c05e22d4SJeff Roberson 
36782f9defeSAlan Cox 		vp = object->handle;
36882f9defeSAlan Cox 		VM_OBJECT_UNLOCK(object);
369c05e22d4SJeff Roberson 		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
370c05e22d4SJeff Roberson 		vget(vp, LK_RETRY, curthread);
371c05e22d4SJeff Roberson 		VFS_UNLOCK_GIANT(vfslocked);
37282f9defeSAlan Cox 	} else
37382f9defeSAlan Cox 		VM_OBJECT_UNLOCK(object);
37495e5e988SJohn Dyson }
37595e5e988SJohn Dyson 
37623955314SAlfred Perlstein /*
377b921a12bSAlan Cox  *	vm_object_reference_locked:
378b921a12bSAlan Cox  *
379b921a12bSAlan Cox  *	Gets another reference to the given object.
380b921a12bSAlan Cox  *
381b921a12bSAlan Cox  *	The object must be locked.
382b921a12bSAlan Cox  */
383b921a12bSAlan Cox void
384b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object)
385b921a12bSAlan Cox {
386b921a12bSAlan Cox 	struct vnode *vp;
387b921a12bSAlan Cox 
388b921a12bSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
389b921a12bSAlan Cox 	KASSERT((object->flags & OBJ_DEAD) == 0,
390b921a12bSAlan Cox 	    ("vm_object_reference_locked: dead object referenced"));
391b921a12bSAlan Cox 	object->ref_count++;
392b921a12bSAlan Cox 	if (object->type == OBJT_VNODE) {
393b921a12bSAlan Cox 		vp = object->handle;
394b921a12bSAlan Cox 		vref(vp);
395b921a12bSAlan Cox 	}
396b921a12bSAlan Cox }
397b921a12bSAlan Cox 
398b921a12bSAlan Cox /*
3999d5abbddSJens Schweikhardt  * Handle deallocating an object of type OBJT_VNODE.
40023955314SAlfred Perlstein  */
40102dd8331SAlan Cox static void
4021b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object)
40395e5e988SJohn Dyson {
40495e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
405219cbf59SEivind Eklund 
406ae51ff11SJeff Roberson 	VFS_ASSERT_GIANT(vp->v_mount);
407ad682c48SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
4085526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
4095526d2d9SEivind Eklund 	    ("vm_object_vndeallocate: not a vnode object"));
410219cbf59SEivind Eklund 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
411219cbf59SEivind Eklund #ifdef INVARIANTS
41295e5e988SJohn Dyson 	if (object->ref_count == 0) {
41395e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
41495e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
41595e5e988SJohn Dyson 	}
41695e5e988SJohn Dyson #endif
41795e5e988SJohn Dyson 
41895e5e988SJohn Dyson 	object->ref_count--;
41947221757SJohn Dyson 	if (object->ref_count == 0) {
420e6e370a7SJeff Roberson 		mp_fixme("Unlocked vflag access.");
421e6e370a7SJeff Roberson 		vp->v_vflag &= ~VV_TEXT;
4222be70f79SJohn Dyson 	}
423ad682c48SAlan Cox 	VM_OBJECT_UNLOCK(object);
42423955314SAlfred Perlstein 	/*
42523955314SAlfred Perlstein 	 * vrele may need a vop lock
42623955314SAlfred Perlstein 	 */
42747221757SJohn Dyson 	vrele(vp);
428df8bae1dSRodney W. Grimes }
429df8bae1dSRodney W. Grimes 
430df8bae1dSRodney W. Grimes /*
431df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
432df8bae1dSRodney W. Grimes  *
433df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
434df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
435df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
436df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
437df8bae1dSRodney W. Grimes  *	may be relinquished.
438df8bae1dSRodney W. Grimes  *
439df8bae1dSRodney W. Grimes  *	No object may be locked.
440df8bae1dSRodney W. Grimes  */
44126f9a767SRodney W. Grimes void
4421b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object)
443df8bae1dSRodney W. Grimes {
444df8bae1dSRodney W. Grimes 	vm_object_t temp;
445df8bae1dSRodney W. Grimes 
446df8bae1dSRodney W. Grimes 	while (object != NULL) {
447ae51ff11SJeff Roberson 		int vfslocked;
448ca95b514SJohn Baldwin 
449ae51ff11SJeff Roberson 		vfslocked = 0;
450ca95b514SJohn Baldwin 	restart:
451ad682c48SAlan Cox 		VM_OBJECT_LOCK(object);
4523b68228cSAlan Cox 		if (object->type == OBJT_VNODE) {
453ca95b514SJohn Baldwin 			struct vnode *vp = (struct vnode *) object->handle;
454ca95b514SJohn Baldwin 
455ca95b514SJohn Baldwin 			/*
456ca95b514SJohn Baldwin 			 * Conditionally acquire Giant for a vnode-backed
457ca95b514SJohn Baldwin 			 * object.  We have to be careful since the type of
458ca95b514SJohn Baldwin 			 * a vnode object can change while the object is
459ca95b514SJohn Baldwin 			 * unlocked.
460ca95b514SJohn Baldwin 			 */
461ca95b514SJohn Baldwin 			if (VFS_NEEDSGIANT(vp->v_mount) && !vfslocked) {
462ca95b514SJohn Baldwin 				vfslocked = 1;
463ca95b514SJohn Baldwin 				if (!mtx_trylock(&Giant)) {
464ca95b514SJohn Baldwin 					VM_OBJECT_UNLOCK(object);
465ca95b514SJohn Baldwin 					mtx_lock(&Giant);
466ca95b514SJohn Baldwin 					goto restart;
467ca95b514SJohn Baldwin 				}
468ca95b514SJohn Baldwin 			}
46995e5e988SJohn Dyson 			vm_object_vndeallocate(object);
470ae51ff11SJeff Roberson 			VFS_UNLOCK_GIANT(vfslocked);
47123b186d3SAlan Cox 			return;
472ca95b514SJohn Baldwin 		} else
473ca95b514SJohn Baldwin 			/*
474ca95b514SJohn Baldwin 			 * This is to handle the case that the object
475ca95b514SJohn Baldwin 			 * changed type while we dropped its lock to
476ca95b514SJohn Baldwin 			 * obtain Giant.
477ca95b514SJohn Baldwin 			 */
478ca95b514SJohn Baldwin 			VFS_UNLOCK_GIANT(vfslocked);
47995e5e988SJohn Dyson 
4808125b1e6SAlfred Perlstein 		KASSERT(object->ref_count != 0,
4818125b1e6SAlfred Perlstein 			("vm_object_deallocate: object deallocated too many times: %d", object->type));
4822be70f79SJohn Dyson 
4832be70f79SJohn Dyson 		/*
4848125b1e6SAlfred Perlstein 		 * If the reference count goes to 0 we start calling
4858125b1e6SAlfred Perlstein 		 * vm_object_terminate() on the object chain.
4868125b1e6SAlfred Perlstein 		 * A ref count of 1 may be a special case depending on the
4878125b1e6SAlfred Perlstein 		 * shadow count being 0 or 1.
4882be70f79SJohn Dyson 		 */
489c0877f10SJohn Dyson 		object->ref_count--;
4908125b1e6SAlfred Perlstein 		if (object->ref_count > 1) {
4913b68228cSAlan Cox 			VM_OBJECT_UNLOCK(object);
49223b186d3SAlan Cox 			return;
4938125b1e6SAlfred Perlstein 		} else if (object->ref_count == 1) {
4948125b1e6SAlfred Perlstein 			if (object->shadow_count == 0) {
4958125b1e6SAlfred Perlstein 				vm_object_set_flag(object, OBJ_ONEMAPPING);
4968125b1e6SAlfred Perlstein 			} else if ((object->shadow_count == 1) &&
4978125b1e6SAlfred Perlstein 			    (object->handle == NULL) &&
49824a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
49924a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
500a1f6d91cSDavid Greenman 				vm_object_t robject;
50195e5e988SJohn Dyson 
5021c500307SAlan Cox 				robject = LIST_FIRST(&object->shadow_head);
5035526d2d9SEivind Eklund 				KASSERT(robject != NULL,
504219cbf59SEivind Eklund 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
5055526d2d9SEivind Eklund 					 object->ref_count,
5065526d2d9SEivind Eklund 					 object->shadow_count));
507b72b0115SAlan Cox 				if (!VM_OBJECT_TRYLOCK(robject)) {
508b72b0115SAlan Cox 					/*
509b72b0115SAlan Cox 					 * Avoid a potential deadlock.
510b72b0115SAlan Cox 					 */
511b72b0115SAlan Cox 					object->ref_count++;
512b72b0115SAlan Cox 					VM_OBJECT_UNLOCK(object);
513a7d86121SAlan Cox 					/*
514a7d86121SAlan Cox 					 * More likely than not the thread
515a7d86121SAlan Cox 					 * holding robject's lock has lower
516a7d86121SAlan Cox 					 * priority than the current thread.
517a7d86121SAlan Cox 					 * Let the lower priority thread run.
518a7d86121SAlan Cox 					 */
519a7d86121SAlan Cox 					tsleep(&proc0, PVM, "vmo_de", 1);
520b72b0115SAlan Cox 					continue;
521b72b0115SAlan Cox 				}
522d936694fSAlan Cox 				/*
523d936694fSAlan Cox 				 * Collapse object into its shadow unless its
524d936694fSAlan Cox 				 * shadow is dead.  In that case, object will
525d936694fSAlan Cox 				 * be deallocated by the thread that is
526d936694fSAlan Cox 				 * deallocating its shadow.
527d936694fSAlan Cox 				 */
528d936694fSAlan Cox 				if ((robject->flags & OBJ_DEAD) == 0 &&
529d936694fSAlan Cox 				    (robject->handle == NULL) &&
53024a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
53124a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
532a1f6d91cSDavid Greenman 
53395e5e988SJohn Dyson 					robject->ref_count++;
534138449dcSAlan Cox retry:
535138449dcSAlan Cox 					if (robject->paging_in_progress) {
536138449dcSAlan Cox 						VM_OBJECT_UNLOCK(object);
537138449dcSAlan Cox 						vm_object_pip_wait(robject,
538138449dcSAlan Cox 						    "objde1");
5392e9f4a69SAlan Cox 						temp = robject->backing_object;
5402e9f4a69SAlan Cox 						if (object == temp) {
541138449dcSAlan Cox 							VM_OBJECT_LOCK(object);
542138449dcSAlan Cox 							goto retry;
5432e9f4a69SAlan Cox 						}
544138449dcSAlan Cox 					} else if (object->paging_in_progress) {
545138449dcSAlan Cox 						VM_OBJECT_UNLOCK(robject);
546138449dcSAlan Cox 						object->flags |= OBJ_PIPWNT;
547138449dcSAlan Cox 						msleep(object,
548138449dcSAlan Cox 						    VM_OBJECT_MTX(object),
549138449dcSAlan Cox 						    PDROP | PVM, "objde2", 0);
550138449dcSAlan Cox 						VM_OBJECT_LOCK(robject);
5512e9f4a69SAlan Cox 						temp = robject->backing_object;
5522e9f4a69SAlan Cox 						if (object == temp) {
553138449dcSAlan Cox 							VM_OBJECT_LOCK(object);
554138449dcSAlan Cox 							goto retry;
555a1f6d91cSDavid Greenman 						}
5562e9f4a69SAlan Cox 					} else
5573b68228cSAlan Cox 						VM_OBJECT_UNLOCK(object);
5582e9f4a69SAlan Cox 
55995e5e988SJohn Dyson 					if (robject->ref_count == 1) {
56095e5e988SJohn Dyson 						robject->ref_count--;
561ba8da839SDavid Greenman 						object = robject;
56295e5e988SJohn Dyson 						goto doterm;
56395e5e988SJohn Dyson 					}
56495e5e988SJohn Dyson 					object = robject;
56595e5e988SJohn Dyson 					vm_object_collapse(object);
566d7fc2210SAlan Cox 					VM_OBJECT_UNLOCK(object);
567ba8da839SDavid Greenman 					continue;
568a1f6d91cSDavid Greenman 				}
569b72b0115SAlan Cox 				VM_OBJECT_UNLOCK(robject);
57095e5e988SJohn Dyson 			}
5713b68228cSAlan Cox 			VM_OBJECT_UNLOCK(object);
57223b186d3SAlan Cox 			return;
57395e5e988SJohn Dyson 		}
57495e5e988SJohn Dyson doterm:
57524a1cce3SDavid Greenman 		temp = object->backing_object;
576c9917419SAlan Cox 		if (temp != NULL) {
577c9917419SAlan Cox 			VM_OBJECT_LOCK(temp);
5781c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
57995e5e988SJohn Dyson 			temp->shadow_count--;
580eaf13dd7SJohn Dyson 			temp->generation++;
581c9917419SAlan Cox 			VM_OBJECT_UNLOCK(temp);
58295461b45SJohn Dyson 			object->backing_object = NULL;
583de5f6a77SJohn Dyson 		}
584245df27cSMatthew Dillon 		/*
585245df27cSMatthew Dillon 		 * Don't double-terminate, we could be in a termination
586245df27cSMatthew Dillon 		 * recursion due to the terminate having to sync data
587245df27cSMatthew Dillon 		 * to disk.
588245df27cSMatthew Dillon 		 */
589245df27cSMatthew Dillon 		if ((object->flags & OBJ_DEAD) == 0)
590df8bae1dSRodney W. Grimes 			vm_object_terminate(object);
591c829b9d0SAlan Cox 		else
592c829b9d0SAlan Cox 			VM_OBJECT_UNLOCK(object);
593df8bae1dSRodney W. Grimes 		object = temp;
594df8bae1dSRodney W. Grimes 	}
595df8bae1dSRodney W. Grimes }
596df8bae1dSRodney W. Grimes 
597df8bae1dSRodney W. Grimes /*
598df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
599df8bae1dSRodney W. Grimes  *	up all previously used resources.
600df8bae1dSRodney W. Grimes  *
601df8bae1dSRodney W. Grimes  *	The object must be locked.
6021c7c3c6aSMatthew Dillon  *	This routine may block.
603df8bae1dSRodney W. Grimes  */
60495e5e988SJohn Dyson void
6051b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object)
606df8bae1dSRodney W. Grimes {
607d031cff1SMatthew Dillon 	vm_page_t p;
608df8bae1dSRodney W. Grimes 
609c829b9d0SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
6100cddd8f0SMatthew Dillon 
61195e5e988SJohn Dyson 	/*
61295e5e988SJohn Dyson 	 * Make sure no one uses us.
61395e5e988SJohn Dyson 	 */
614069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
6153c631446SJohn Dyson 
616df8bae1dSRodney W. Grimes 	/*
617f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
618df8bae1dSRodney W. Grimes 	 */
61966095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
620df8bae1dSRodney W. Grimes 
6215526d2d9SEivind Eklund 	KASSERT(!object->paging_in_progress,
6225526d2d9SEivind Eklund 		("vm_object_terminate: pageout in progress"));
62326f9a767SRodney W. Grimes 
62426f9a767SRodney W. Grimes 	/*
6250d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
6260d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
62726f9a767SRodney W. Grimes 	 */
62824a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
629f7dd7b63SAlan Cox 		struct vnode *vp = (struct vnode *)object->handle;
63095e5e988SJohn Dyson 
63195e5e988SJohn Dyson 		/*
63295e5e988SJohn Dyson 		 * Clean pages and flush buffers.
63395e5e988SJohn Dyson 		 */
6348f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
635b6e48e03SAlan Cox 		VM_OBJECT_UNLOCK(object);
63695e5e988SJohn Dyson 
6377c0745eeSPoul-Henning Kamp 		vinvalbuf(vp, V_SAVE, NULL, 0, 0);
638f7dd7b63SAlan Cox 
639f7dd7b63SAlan Cox 		VM_OBJECT_LOCK(object);
640bef608bdSJohn Dyson 	}
641bef608bdSJohn Dyson 
642971dd342SAlfred Perlstein 	KASSERT(object->ref_count == 0,
643971dd342SAlfred Perlstein 		("vm_object_terminate: object with references, ref_count=%d",
644971dd342SAlfred Perlstein 		object->ref_count));
645996c772fSJohn Dyson 
6460d94caffSDavid Greenman 	/*
647356863ebSDavid Greenman 	 * Now free any remaining pages. For internal objects, this also
648356863ebSDavid Greenman 	 * removes them from paging queues. Don't free wired pages, just
649356863ebSDavid Greenman 	 * remove them from the object.
650df8bae1dSRodney W. Grimes 	 */
65156030358SAlan Cox 	vm_page_lock_queues();
652b18bfc3dSJohn Dyson 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
6539af80719SAlan Cox 		KASSERT(!p->busy && (p->oflags & VPO_BUSY) == 0,
654971dd342SAlfred Perlstein 			("vm_object_terminate: freeing busy page %p "
655971dd342SAlfred Perlstein 			"p->busy = %d, p->flags %x\n", p, p->busy, p->flags));
6560b10ba98SDavid Greenman 		if (p->wire_count == 0) {
657df8bae1dSRodney W. Grimes 			vm_page_free(p);
658df8bae1dSRodney W. Grimes 			cnt.v_pfree++;
6590b10ba98SDavid Greenman 		} else {
6600b10ba98SDavid Greenman 			vm_page_remove(p);
6610b10ba98SDavid Greenman 		}
662df8bae1dSRodney W. Grimes 	}
66356030358SAlan Cox 	vm_page_unlock_queues();
664bef608bdSJohn Dyson 
6652d8acc0fSJohn Dyson 	/*
6669fcfb650SDavid Greenman 	 * Let the pager know object is dead.
6679fcfb650SDavid Greenman 	 */
6689fcfb650SDavid Greenman 	vm_pager_deallocate(object);
669658ad5ffSAlan Cox 	VM_OBJECT_UNLOCK(object);
6709fcfb650SDavid Greenman 
6719fcfb650SDavid Greenman 	/*
6720b10ba98SDavid Greenman 	 * Remove the object from the global object list.
6732d8acc0fSJohn Dyson 	 */
6749ed346baSBosko Milekic 	mtx_lock(&vm_object_list_mtx);
675df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
6769ed346baSBosko Milekic 	mtx_unlock(&vm_object_list_mtx);
6770b10ba98SDavid Greenman 
678df8bae1dSRodney W. Grimes 	/*
679df8bae1dSRodney W. Grimes 	 * Free the space for the object.
680df8bae1dSRodney W. Grimes 	 */
681670d17b5SJeff Roberson 	uma_zfree(obj_zone, object);
68247221757SJohn Dyson }
683df8bae1dSRodney W. Grimes 
684df8bae1dSRodney W. Grimes /*
685df8bae1dSRodney W. Grimes  *	vm_object_page_clean
686df8bae1dSRodney W. Grimes  *
6874f79d873SMatthew Dillon  *	Clean all dirty pages in the specified range of object.  Leaves page
6884f79d873SMatthew Dillon  * 	on whatever queue it is currently on.   If NOSYNC is set then do not
689b146f9e5SAlan Cox  *	write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC),
6904f79d873SMatthew Dillon  *	leaving the object dirty.
69126f9a767SRodney W. Grimes  *
69243b7990eSMatthew Dillon  *	When stuffing pages asynchronously, allow clustering.  XXX we need a
69343b7990eSMatthew Dillon  *	synchronous clustering mode implementation.
69443b7990eSMatthew Dillon  *
69526f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
69626f9a767SRodney W. Grimes  *
69726f9a767SRodney W. Grimes  *	The object must be locked.
69826f9a767SRodney W. Grimes  */
699f6b04d2bSDavid Greenman void
7001b40f8c0SMatthew Dillon vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags)
701f6b04d2bSDavid Greenman {
702b9b7a4beSMatthew Dillon 	vm_page_t p, np;
7036395da54SIan Dowse 	vm_pindex_t tstart, tend;
704bd7e5f99SJohn Dyson 	vm_pindex_t pi;
7054f79d873SMatthew Dillon 	int clearobjflags;
7068f9110f6SJohn Dyson 	int pagerflags;
7072d8acc0fSJohn Dyson 	int curgeneration;
708f6b04d2bSDavid Greenman 
709b6e48e03SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
710aef922f5SJohn Dyson 	if (object->type != OBJT_VNODE ||
711aef922f5SJohn Dyson 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
712f6b04d2bSDavid Greenman 		return;
713f6b04d2bSDavid Greenman 
71443b7990eSMatthew Dillon 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
7158f9110f6SJohn Dyson 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
7168f9110f6SJohn Dyson 
717069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_CLEANING);
71824a1cce3SDavid Greenman 
719f6b04d2bSDavid Greenman 	tstart = start;
720f6b04d2bSDavid Greenman 	if (end == 0) {
721f6b04d2bSDavid Greenman 		tend = object->size;
722f6b04d2bSDavid Greenman 	} else {
723f6b04d2bSDavid Greenman 		tend = end;
724f6b04d2bSDavid Greenman 	}
725eaf13dd7SJohn Dyson 
72635c01631SAlan Cox 	vm_page_lock_queues();
7274f79d873SMatthew Dillon 	/*
728b9b7a4beSMatthew Dillon 	 * If the caller is smart and only msync()s a range he knows is
729b9b7a4beSMatthew Dillon 	 * dirty, we may be able to avoid an object scan.  This results in
730b9b7a4beSMatthew Dillon 	 * a phenominal improvement in performance.  We cannot do this
731b9b7a4beSMatthew Dillon 	 * as a matter of course because the object may be huge - e.g.
732b9b7a4beSMatthew Dillon 	 * the size might be in the gigabytes or terrabytes.
733b9b7a4beSMatthew Dillon 	 */
734b9b7a4beSMatthew Dillon 	if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) {
735300b96acSIan Dowse 		vm_pindex_t tscan;
736b9b7a4beSMatthew Dillon 		int scanlimit;
737b9b7a4beSMatthew Dillon 		int scanreset;
738b9b7a4beSMatthew Dillon 
739b9b7a4beSMatthew Dillon 		scanreset = object->resident_page_count / EASY_SCAN_FACTOR;
740b9b7a4beSMatthew Dillon 		if (scanreset < 16)
741b9b7a4beSMatthew Dillon 			scanreset = 16;
74243b7990eSMatthew Dillon 		pagerflags |= VM_PAGER_IGNORE_CLEANCHK;
743b9b7a4beSMatthew Dillon 
744b9b7a4beSMatthew Dillon 		scanlimit = scanreset;
745b9b7a4beSMatthew Dillon 		tscan = tstart;
746b9b7a4beSMatthew Dillon 		while (tscan < tend) {
747b9b7a4beSMatthew Dillon 			curgeneration = object->generation;
748b9b7a4beSMatthew Dillon 			p = vm_page_lookup(object, tscan);
749b9b7a4beSMatthew Dillon 			if (p == NULL || p->valid == 0 ||
750997e1c25SAlan Cox 			    VM_PAGE_INQUEUE1(p, PQ_CACHE)) {
751b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
752b9b7a4beSMatthew Dillon 					break;
753b9b7a4beSMatthew Dillon 				++tscan;
754b9b7a4beSMatthew Dillon 				continue;
755b9b7a4beSMatthew Dillon 			}
756b9b7a4beSMatthew Dillon 			vm_page_test_dirty(p);
757b9b7a4beSMatthew Dillon 			if ((p->dirty & p->valid) == 0) {
758b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
759b9b7a4beSMatthew Dillon 					break;
760b9b7a4beSMatthew Dillon 				++tscan;
761b9b7a4beSMatthew Dillon 				continue;
762b9b7a4beSMatthew Dillon 			}
763b9b7a4beSMatthew Dillon 			/*
764b9b7a4beSMatthew Dillon 			 * If we have been asked to skip nosync pages and
765b9b7a4beSMatthew Dillon 			 * this is a nosync page, we can't continue.
766b9b7a4beSMatthew Dillon 			 */
767b146f9e5SAlan Cox 			if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) {
768b9b7a4beSMatthew Dillon 				if (--scanlimit == 0)
769b9b7a4beSMatthew Dillon 					break;
770b9b7a4beSMatthew Dillon 				++tscan;
771b9b7a4beSMatthew Dillon 				continue;
772b9b7a4beSMatthew Dillon 			}
773b9b7a4beSMatthew Dillon 			scanlimit = scanreset;
774b9b7a4beSMatthew Dillon 
775b9b7a4beSMatthew Dillon 			/*
776b9b7a4beSMatthew Dillon 			 * This returns 0 if it was unable to busy the first
777b9b7a4beSMatthew Dillon 			 * page (i.e. had to sleep).
778b9b7a4beSMatthew Dillon 			 */
779b9b7a4beSMatthew Dillon 			tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags);
780b9b7a4beSMatthew Dillon 		}
781b9b7a4beSMatthew Dillon 
782b9b7a4beSMatthew Dillon 		/*
783b9b7a4beSMatthew Dillon 		 * If everything was dirty and we flushed it successfully,
784b9b7a4beSMatthew Dillon 		 * and the requested range is not the entire object, we
785b9b7a4beSMatthew Dillon 		 * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can
786b9b7a4beSMatthew Dillon 		 * return immediately.
787b9b7a4beSMatthew Dillon 		 */
788b9b7a4beSMatthew Dillon 		if (tscan >= tend && (tstart || tend < object->size)) {
78935c01631SAlan Cox 			vm_page_unlock_queues();
790b9b7a4beSMatthew Dillon 			vm_object_clear_flag(object, OBJ_CLEANING);
791b9b7a4beSMatthew Dillon 			return;
792b9b7a4beSMatthew Dillon 		}
79343b7990eSMatthew Dillon 		pagerflags &= ~VM_PAGER_IGNORE_CLEANCHK;
794b9b7a4beSMatthew Dillon 	}
795b9b7a4beSMatthew Dillon 
796b9b7a4beSMatthew Dillon 	/*
7974f79d873SMatthew Dillon 	 * Generally set CLEANCHK interlock and make the page read-only so
7984f79d873SMatthew Dillon 	 * we can then clear the object flags.
7994f79d873SMatthew Dillon 	 *
8004f79d873SMatthew Dillon 	 * However, if this is a nosync mmap then the object is likely to
8014f79d873SMatthew Dillon 	 * stay dirty so do not mess with the page and do not clear the
8024f79d873SMatthew Dillon 	 * object flags.
8034f79d873SMatthew Dillon 	 */
8044f79d873SMatthew Dillon 	clearobjflags = 1;
805fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
806e69763a3SDoug Rabson 		vm_page_flag_set(p, PG_CLEANCHK);
807b146f9e5SAlan Cox 		if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC))
8084f79d873SMatthew Dillon 			clearobjflags = 0;
8094f79d873SMatthew Dillon 		else
81078985e42SAlan Cox 			pmap_remove_write(p);
811eaf13dd7SJohn Dyson 	}
812eaf13dd7SJohn Dyson 
8134f79d873SMatthew Dillon 	if (clearobjflags && (tstart == 0) && (tend == object->size)) {
814245df27cSMatthew Dillon 		struct vnode *vp;
815245df27cSMatthew Dillon 
816af51d7bfSAlan Cox 		vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY);
817245df27cSMatthew Dillon 		if (object->type == OBJT_VNODE &&
818245df27cSMatthew Dillon 		    (vp = (struct vnode *)object->handle) != NULL) {
819e6e370a7SJeff Roberson 			VI_LOCK(vp);
820e6e370a7SJeff Roberson 			if (vp->v_iflag & VI_OBJDIRTY)
821e6e370a7SJeff Roberson 				vp->v_iflag &= ~VI_OBJDIRTY;
822e6e370a7SJeff Roberson 			VI_UNLOCK(vp);
823245df27cSMatthew Dillon 		}
824ec4f9fb0SDavid Greenman 	}
825f6b04d2bSDavid Greenman 
826bd7e5f99SJohn Dyson rescan:
8272d8acc0fSJohn Dyson 	curgeneration = object->generation;
8282d8acc0fSJohn Dyson 
829b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p; p = np) {
830b9b7a4beSMatthew Dillon 		int n;
831b9b7a4beSMatthew Dillon 
832b18bfc3dSJohn Dyson 		np = TAILQ_NEXT(p, listq);
833bd7e5f99SJohn Dyson 
834b9b7a4beSMatthew Dillon again:
835bd7e5f99SJohn Dyson 		pi = p->pindex;
836bd7e5f99SJohn Dyson 		if (((p->flags & PG_CLEANCHK) == 0) ||
837bd7e5f99SJohn Dyson 			(pi < tstart) || (pi >= tend) ||
8385070c7f8SJohn Dyson 			(p->valid == 0) ||
839997e1c25SAlan Cox 		    VM_PAGE_INQUEUE1(p, PQ_CACHE)) {
840e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
841aef922f5SJohn Dyson 			continue;
842f6b04d2bSDavid Greenman 		}
843f6b04d2bSDavid Greenman 
844bd7e5f99SJohn Dyson 		vm_page_test_dirty(p);
845bd7e5f99SJohn Dyson 		if ((p->dirty & p->valid) == 0) {
846e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
847bd7e5f99SJohn Dyson 			continue;
848bd7e5f99SJohn Dyson 		}
849ec4f9fb0SDavid Greenman 
8504f79d873SMatthew Dillon 		/*
8514f79d873SMatthew Dillon 		 * If we have been asked to skip nosync pages and this is a
8524f79d873SMatthew Dillon 		 * nosync page, skip it.  Note that the object flags were
8534f79d873SMatthew Dillon 		 * not cleared in this case so we do not have to set them.
8544f79d873SMatthew Dillon 		 */
855b146f9e5SAlan Cox 		if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) {
8564f79d873SMatthew Dillon 			vm_page_flag_clear(p, PG_CLEANCHK);
8574f79d873SMatthew Dillon 			continue;
8584f79d873SMatthew Dillon 		}
8594f79d873SMatthew Dillon 
860b9b7a4beSMatthew Dillon 		n = vm_object_page_collect_flush(object, p,
861b9b7a4beSMatthew Dillon 			curgeneration, pagerflags);
862b9b7a4beSMatthew Dillon 		if (n == 0)
863b9b7a4beSMatthew Dillon 			goto rescan;
864b9b7a4beSMatthew Dillon 
865b9b7a4beSMatthew Dillon 		if (object->generation != curgeneration)
866b9b7a4beSMatthew Dillon 			goto rescan;
867b9b7a4beSMatthew Dillon 
868b9b7a4beSMatthew Dillon 		/*
869b9b7a4beSMatthew Dillon 		 * Try to optimize the next page.  If we can't we pick up
870b9b7a4beSMatthew Dillon 		 * our (random) scan where we left off.
871b9b7a4beSMatthew Dillon 		 */
872b9b7a4beSMatthew Dillon 		if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) {
873b9b7a4beSMatthew Dillon 			if ((p = vm_page_lookup(object, pi + n)) != NULL)
874b9b7a4beSMatthew Dillon 				goto again;
875b9b7a4beSMatthew Dillon 		}
876b9b7a4beSMatthew Dillon 	}
87735c01631SAlan Cox 	vm_page_unlock_queues();
878b9b7a4beSMatthew Dillon #if 0
8798df6bac4SPoul-Henning Kamp 	VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
880b9b7a4beSMatthew Dillon #endif
881b9b7a4beSMatthew Dillon 
882b9b7a4beSMatthew Dillon 	vm_object_clear_flag(object, OBJ_CLEANING);
883b9b7a4beSMatthew Dillon 	return;
884b9b7a4beSMatthew Dillon }
885b9b7a4beSMatthew Dillon 
886b9b7a4beSMatthew Dillon static int
887b9b7a4beSMatthew Dillon vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags)
888b9b7a4beSMatthew Dillon {
889b9b7a4beSMatthew Dillon 	int runlen;
890b9b7a4beSMatthew Dillon 	int maxf;
891b9b7a4beSMatthew Dillon 	int chkb;
892b9b7a4beSMatthew Dillon 	int maxb;
893b9b7a4beSMatthew Dillon 	int i;
894b9b7a4beSMatthew Dillon 	vm_pindex_t pi;
895b9b7a4beSMatthew Dillon 	vm_page_t maf[vm_pageout_page_count];
896b9b7a4beSMatthew Dillon 	vm_page_t mab[vm_pageout_page_count];
897b9b7a4beSMatthew Dillon 	vm_page_t ma[vm_pageout_page_count];
898b9b7a4beSMatthew Dillon 
89935c01631SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
900b9b7a4beSMatthew Dillon 	pi = p->pindex;
901bd82dc74SAlan Cox 	while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
90235c01631SAlan Cox 		vm_page_lock_queues();
9032d8acc0fSJohn Dyson 		if (object->generation != curgeneration) {
904b9b7a4beSMatthew Dillon 			return(0);
905f6b04d2bSDavid Greenman 		}
906bd82dc74SAlan Cox 	}
907bd7e5f99SJohn Dyson 	maxf = 0;
908bd7e5f99SJohn Dyson 	for(i = 1; i < vm_pageout_page_count; i++) {
909b9b7a4beSMatthew Dillon 		vm_page_t tp;
910b9b7a4beSMatthew Dillon 
9118aef1712SMatthew Dillon 		if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
9129af80719SAlan Cox 			if ((tp->oflags & VPO_BUSY) ||
91343b7990eSMatthew Dillon 				((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
91443b7990eSMatthew Dillon 				 (tp->flags & PG_CLEANCHK) == 0) ||
915ffc82b0aSJohn Dyson 				(tp->busy != 0))
916bd7e5f99SJohn Dyson 				break;
917997e1c25SAlan Cox 			if (VM_PAGE_INQUEUE1(tp, PQ_CACHE)) {
918e69763a3SDoug Rabson 				vm_page_flag_clear(tp, PG_CLEANCHK);
9193077a9c2SJohn Dyson 				break;
9203077a9c2SJohn Dyson 			}
921bd7e5f99SJohn Dyson 			vm_page_test_dirty(tp);
922bd7e5f99SJohn Dyson 			if ((tp->dirty & tp->valid) == 0) {
923e69763a3SDoug Rabson 				vm_page_flag_clear(tp, PG_CLEANCHK);
924bd7e5f99SJohn Dyson 				break;
925bd7e5f99SJohn Dyson 			}
926bd7e5f99SJohn Dyson 			maf[ i - 1 ] = tp;
927bd7e5f99SJohn Dyson 			maxf++;
928bd7e5f99SJohn Dyson 			continue;
929bd7e5f99SJohn Dyson 		}
930bd7e5f99SJohn Dyson 		break;
931bd7e5f99SJohn Dyson 	}
932aef922f5SJohn Dyson 
933bd7e5f99SJohn Dyson 	maxb = 0;
934bd7e5f99SJohn Dyson 	chkb = vm_pageout_page_count -  maxf;
935bd7e5f99SJohn Dyson 	if (chkb) {
936bd7e5f99SJohn Dyson 		for(i = 1; i < chkb;i++) {
937b9b7a4beSMatthew Dillon 			vm_page_t tp;
938b9b7a4beSMatthew Dillon 
9398aef1712SMatthew Dillon 			if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
9409af80719SAlan Cox 				if ((tp->oflags & VPO_BUSY) ||
94143b7990eSMatthew Dillon 					((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
94243b7990eSMatthew Dillon 					 (tp->flags & PG_CLEANCHK) == 0) ||
943ffc82b0aSJohn Dyson 					(tp->busy != 0))
944bd7e5f99SJohn Dyson 					break;
945997e1c25SAlan Cox 				if (VM_PAGE_INQUEUE1(tp, PQ_CACHE)) {
946e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
9473077a9c2SJohn Dyson 					break;
9483077a9c2SJohn Dyson 				}
949bd7e5f99SJohn Dyson 				vm_page_test_dirty(tp);
950bd7e5f99SJohn Dyson 				if ((tp->dirty & tp->valid) == 0) {
951e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
952bd7e5f99SJohn Dyson 					break;
953bd7e5f99SJohn Dyson 				}
954bd7e5f99SJohn Dyson 				mab[ i - 1 ] = tp;
955bd7e5f99SJohn Dyson 				maxb++;
956bd7e5f99SJohn Dyson 				continue;
957bd7e5f99SJohn Dyson 			}
958bd7e5f99SJohn Dyson 			break;
959bd7e5f99SJohn Dyson 		}
960bd7e5f99SJohn Dyson 	}
961bd7e5f99SJohn Dyson 
962bd7e5f99SJohn Dyson 	for(i = 0; i < maxb; i++) {
963bd7e5f99SJohn Dyson 		int index = (maxb - i) - 1;
964bd7e5f99SJohn Dyson 		ma[index] = mab[i];
965e69763a3SDoug Rabson 		vm_page_flag_clear(ma[index], PG_CLEANCHK);
966bd7e5f99SJohn Dyson 	}
967e69763a3SDoug Rabson 	vm_page_flag_clear(p, PG_CLEANCHK);
968bd7e5f99SJohn Dyson 	ma[maxb] = p;
969bd7e5f99SJohn Dyson 	for(i = 0; i < maxf; i++) {
970bd7e5f99SJohn Dyson 		int index = (maxb + i) + 1;
971bd7e5f99SJohn Dyson 		ma[index] = maf[i];
972e69763a3SDoug Rabson 		vm_page_flag_clear(ma[index], PG_CLEANCHK);
973f6b04d2bSDavid Greenman 	}
974bd7e5f99SJohn Dyson 	runlen = maxb + maxf + 1;
975cf2819ccSJohn Dyson 
9767a935082SAlan Cox 	vm_pageout_flush(ma, runlen, pagerflags);
977cf2819ccSJohn Dyson 	for (i = 0; i < runlen; i++) {
978cf2819ccSJohn Dyson 		if (ma[i]->valid & ma[i]->dirty) {
97978985e42SAlan Cox 			pmap_remove_write(ma[i]);
980e69763a3SDoug Rabson 			vm_page_flag_set(ma[i], PG_CLEANCHK);
981aef922f5SJohn Dyson 
982b9b7a4beSMatthew Dillon 			/*
983b9b7a4beSMatthew Dillon 			 * maxf will end up being the actual number of pages
984b9b7a4beSMatthew Dillon 			 * we wrote out contiguously, non-inclusive of the
985b9b7a4beSMatthew Dillon 			 * first page.  We do not count look-behind pages.
986b9b7a4beSMatthew Dillon 			 */
987b9b7a4beSMatthew Dillon 			if (i >= maxb + 1 && (maxf > i - maxb - 1))
988b9b7a4beSMatthew Dillon 				maxf = i - maxb - 1;
989b9b7a4beSMatthew Dillon 		}
990b9b7a4beSMatthew Dillon 	}
991b9b7a4beSMatthew Dillon 	return(maxf + 1);
99226f9a767SRodney W. Grimes }
993df8bae1dSRodney W. Grimes 
9941efb74fbSJohn Dyson /*
995950f8459SAlan Cox  * Note that there is absolutely no sense in writing out
996950f8459SAlan Cox  * anonymous objects, so we track down the vnode object
997950f8459SAlan Cox  * to write out.
998950f8459SAlan Cox  * We invalidate (remove) all pages from the address space
999950f8459SAlan Cox  * for semantic correctness.
1000950f8459SAlan Cox  *
1001950f8459SAlan Cox  * Note: certain anonymous maps, such as MAP_NOSYNC maps,
1002950f8459SAlan Cox  * may start out with a NULL object.
1003950f8459SAlan Cox  */
1004950f8459SAlan Cox void
1005950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
1006950f8459SAlan Cox     boolean_t syncio, boolean_t invalidate)
1007950f8459SAlan Cox {
1008950f8459SAlan Cox 	vm_object_t backing_object;
1009950f8459SAlan Cox 	struct vnode *vp;
10103b582b4eSTor Egge 	struct mount *mp;
1011950f8459SAlan Cox 	int flags;
1012950f8459SAlan Cox 
1013950f8459SAlan Cox 	if (object == NULL)
1014950f8459SAlan Cox 		return;
1015950f8459SAlan Cox 	VM_OBJECT_LOCK(object);
1016950f8459SAlan Cox 	while ((backing_object = object->backing_object) != NULL) {
1017950f8459SAlan Cox 		VM_OBJECT_LOCK(backing_object);
101856e0670fSAlan Cox 		offset += object->backing_object_offset;
1019950f8459SAlan Cox 		VM_OBJECT_UNLOCK(object);
1020950f8459SAlan Cox 		object = backing_object;
1021950f8459SAlan Cox 		if (object->size < OFF_TO_IDX(offset + size))
1022950f8459SAlan Cox 			size = IDX_TO_OFF(object->size) - offset;
1023950f8459SAlan Cox 	}
1024950f8459SAlan Cox 	/*
1025950f8459SAlan Cox 	 * Flush pages if writing is allowed, invalidate them
1026950f8459SAlan Cox 	 * if invalidation requested.  Pages undergoing I/O
1027950f8459SAlan Cox 	 * will be ignored by vm_object_page_remove().
1028950f8459SAlan Cox 	 *
1029950f8459SAlan Cox 	 * We cannot lock the vnode and then wait for paging
1030950f8459SAlan Cox 	 * to complete without deadlocking against vm_fault.
1031950f8459SAlan Cox 	 * Instead we simply call vm_object_page_remove() and
1032950f8459SAlan Cox 	 * allow it to block internally on a page-by-page
1033950f8459SAlan Cox 	 * basis when it encounters pages undergoing async
1034950f8459SAlan Cox 	 * I/O.
1035950f8459SAlan Cox 	 */
1036950f8459SAlan Cox 	if (object->type == OBJT_VNODE &&
1037950f8459SAlan Cox 	    (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
1038ae51ff11SJeff Roberson 		int vfslocked;
1039950f8459SAlan Cox 		vp = object->handle;
1040950f8459SAlan Cox 		VM_OBJECT_UNLOCK(object);
10413b582b4eSTor Egge 		(void) vn_start_write(vp, &mp, V_WAIT);
1042ae51ff11SJeff Roberson 		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1043950f8459SAlan Cox 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1044950f8459SAlan Cox 		flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
1045950f8459SAlan Cox 		flags |= invalidate ? OBJPC_INVAL : 0;
1046950f8459SAlan Cox 		VM_OBJECT_LOCK(object);
1047950f8459SAlan Cox 		vm_object_page_clean(object,
1048950f8459SAlan Cox 		    OFF_TO_IDX(offset),
1049950f8459SAlan Cox 		    OFF_TO_IDX(offset + size + PAGE_MASK),
1050950f8459SAlan Cox 		    flags);
1051950f8459SAlan Cox 		VM_OBJECT_UNLOCK(object);
1052950f8459SAlan Cox 		VOP_UNLOCK(vp, 0, curthread);
1053ae51ff11SJeff Roberson 		VFS_UNLOCK_GIANT(vfslocked);
10543b582b4eSTor Egge 		vn_finished_write(mp);
1055950f8459SAlan Cox 		VM_OBJECT_LOCK(object);
1056950f8459SAlan Cox 	}
1057950f8459SAlan Cox 	if ((object->type == OBJT_VNODE ||
1058950f8459SAlan Cox 	     object->type == OBJT_DEVICE) && invalidate) {
1059874f0135SDoug Rabson 		boolean_t purge;
1060874f0135SDoug Rabson 		purge = old_msync || (object->type == OBJT_DEVICE);
1061950f8459SAlan Cox 		vm_object_page_remove(object,
1062950f8459SAlan Cox 		    OFF_TO_IDX(offset),
1063950f8459SAlan Cox 		    OFF_TO_IDX(offset + size + PAGE_MASK),
1064874f0135SDoug Rabson 		    purge ? FALSE : TRUE);
1065950f8459SAlan Cox 	}
1066950f8459SAlan Cox 	VM_OBJECT_UNLOCK(object);
1067950f8459SAlan Cox }
1068950f8459SAlan Cox 
1069950f8459SAlan Cox /*
1070867a482dSJohn Dyson  *	vm_object_madvise:
1071867a482dSJohn Dyson  *
1072867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
10731c7c3c6aSMatthew Dillon  *
1074193b9358SAlan Cox  *	MADV_WILLNEED	(any object)
1075193b9358SAlan Cox  *
1076193b9358SAlan Cox  *	    Activate the specified pages if they are resident.
1077193b9358SAlan Cox  *
1078193b9358SAlan Cox  *	MADV_DONTNEED	(any object)
1079193b9358SAlan Cox  *
1080193b9358SAlan Cox  *	    Deactivate the specified pages if they are resident.
1081193b9358SAlan Cox  *
1082193b9358SAlan Cox  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
1083193b9358SAlan Cox  *			 OBJ_ONEMAPPING only)
1084193b9358SAlan Cox  *
1085193b9358SAlan Cox  *	    Deactivate and clean the specified pages if they are
1086193b9358SAlan Cox  *	    resident.  This permits the process to reuse the pages
1087193b9358SAlan Cox  *	    without faulting or the kernel to reclaim the pages
1088193b9358SAlan Cox  *	    without I/O.
1089867a482dSJohn Dyson  */
1090867a482dSJohn Dyson void
10911b40f8c0SMatthew Dillon vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
1092867a482dSJohn Dyson {
10936e20a165SJohn Dyson 	vm_pindex_t end, tpindex;
109434567de7SAlan Cox 	vm_object_t backing_object, tobject;
1095867a482dSJohn Dyson 	vm_page_t m;
1096867a482dSJohn Dyson 
1097867a482dSJohn Dyson 	if (object == NULL)
1098867a482dSJohn Dyson 		return;
10999b98b796SAlan Cox 	VM_OBJECT_LOCK(object);
1100867a482dSJohn Dyson 	end = pindex + count;
11011c7c3c6aSMatthew Dillon 	/*
11021c7c3c6aSMatthew Dillon 	 * Locate and adjust resident pages
11031c7c3c6aSMatthew Dillon 	 */
11041c7c3c6aSMatthew Dillon 	for (; pindex < end; pindex += 1) {
11056e20a165SJohn Dyson relookup:
11066e20a165SJohn Dyson 		tobject = object;
11076e20a165SJohn Dyson 		tpindex = pindex;
11086e20a165SJohn Dyson shadowlookup:
110958b4e6ccSAlan Cox 		/*
111058b4e6ccSAlan Cox 		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
111158b4e6ccSAlan Cox 		 * and those pages must be OBJ_ONEMAPPING.
111258b4e6ccSAlan Cox 		 */
111358b4e6ccSAlan Cox 		if (advise == MADV_FREE) {
111458b4e6ccSAlan Cox 			if ((tobject->type != OBJT_DEFAULT &&
111558b4e6ccSAlan Cox 			     tobject->type != OBJT_SWAP) ||
111658b4e6ccSAlan Cox 			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
111734567de7SAlan Cox 				goto unlock_tobject;
11186e20a165SJohn Dyson 			}
111958b4e6ccSAlan Cox 		}
11201c7c3c6aSMatthew Dillon 		m = vm_page_lookup(tobject, tpindex);
11211c7c3c6aSMatthew Dillon 		if (m == NULL) {
11221ce137beSMatthew Dillon 			/*
11231ce137beSMatthew Dillon 			 * There may be swap even if there is no backing page
11241ce137beSMatthew Dillon 			 */
11251ce137beSMatthew Dillon 			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
11261ce137beSMatthew Dillon 				swap_pager_freespace(tobject, tpindex, 1);
11271ce137beSMatthew Dillon 			/*
11281ce137beSMatthew Dillon 			 * next object
11291ce137beSMatthew Dillon 			 */
113034567de7SAlan Cox 			backing_object = tobject->backing_object;
113134567de7SAlan Cox 			if (backing_object == NULL)
113234567de7SAlan Cox 				goto unlock_tobject;
113334567de7SAlan Cox 			VM_OBJECT_LOCK(backing_object);
113456e0670fSAlan Cox 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
11359b98b796SAlan Cox 			if (tobject != object)
113634567de7SAlan Cox 				VM_OBJECT_UNLOCK(tobject);
113734567de7SAlan Cox 			tobject = backing_object;
11386e20a165SJohn Dyson 			goto shadowlookup;
11396e20a165SJohn Dyson 		}
1140867a482dSJohn Dyson 		/*
1141867a482dSJohn Dyson 		 * If the page is busy or not in a normal active state,
11428b03c8edSMatthew Dillon 		 * we skip it.  If the page is not managed there are no
11438b03c8edSMatthew Dillon 		 * page queues to mess with.  Things can break if we mess
11448b03c8edSMatthew Dillon 		 * with pages in any of the below states.
1145867a482dSJohn Dyson 		 */
114632585dd6SAlan Cox 		vm_page_lock_queues();
114732585dd6SAlan Cox 		if (m->hold_count ||
11481c7c3c6aSMatthew Dillon 		    m->wire_count ||
11498b03c8edSMatthew Dillon 		    (m->flags & PG_UNMANAGED) ||
115032585dd6SAlan Cox 		    m->valid != VM_PAGE_BITS_ALL) {
115132585dd6SAlan Cox 			vm_page_unlock_queues();
115234567de7SAlan Cox 			goto unlock_tobject;
11536e20a165SJohn Dyson 		}
11549af80719SAlan Cox 		if ((m->oflags & VPO_BUSY) || m->busy) {
11555786be7cSAlan Cox 			vm_page_flag_set(m, PG_REFERENCED);
115691449ce9SAlan Cox 			vm_page_unlock_queues();
11579b98b796SAlan Cox 			if (object != tobject)
11589b98b796SAlan Cox 				VM_OBJECT_UNLOCK(object);
11595786be7cSAlan Cox 			m->oflags |= VPO_WANTED;
116091449ce9SAlan Cox 			msleep(m, VM_OBJECT_MTX(tobject), PDROP | PVM, "madvpo", 0);
11619b98b796SAlan Cox 			VM_OBJECT_LOCK(object);
11626e20a165SJohn Dyson   			goto relookup;
116334567de7SAlan Cox 		}
1164867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
1165867a482dSJohn Dyson 			vm_page_activate(m);
11666e20a165SJohn Dyson 		} else if (advise == MADV_DONTNEED) {
1167479112dfSMatthew Dillon 			vm_page_dontneed(m);
11680a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
11691c7c3c6aSMatthew Dillon 			/*
11702aaeadf8SMatthew Dillon 			 * Mark the page clean.  This will allow the page
11712aaeadf8SMatthew Dillon 			 * to be freed up by the system.  However, such pages
11722aaeadf8SMatthew Dillon 			 * are often reused quickly by malloc()/free()
11732aaeadf8SMatthew Dillon 			 * so we do not do anything that would cause
11742aaeadf8SMatthew Dillon 			 * a page fault if we can help it.
11752aaeadf8SMatthew Dillon 			 *
11762aaeadf8SMatthew Dillon 			 * Specifically, we do not try to actually free
11772aaeadf8SMatthew Dillon 			 * the page now nor do we try to put it in the
11782aaeadf8SMatthew Dillon 			 * cache (which would cause a page fault on reuse).
117941c67e12SMatthew Dillon 			 *
118041c67e12SMatthew Dillon 			 * But we do make the page is freeable as we
118141c67e12SMatthew Dillon 			 * can without actually taking the step of unmapping
118241c67e12SMatthew Dillon 			 * it.
11831c7c3c6aSMatthew Dillon 			 */
11840385347cSPeter Wemm 			pmap_clear_modify(m);
11856e20a165SJohn Dyson 			m->dirty = 0;
118641c67e12SMatthew Dillon 			m->act_count = 0;
1187479112dfSMatthew Dillon 			vm_page_dontneed(m);
1188867a482dSJohn Dyson 		}
11892999e9faSAlan Cox 		vm_page_unlock_queues();
11902999e9faSAlan Cox 		if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
11912999e9faSAlan Cox 			swap_pager_freespace(tobject, tpindex, 1);
119234567de7SAlan Cox unlock_tobject:
11939b98b796SAlan Cox 		if (tobject != object)
119434567de7SAlan Cox 			VM_OBJECT_UNLOCK(tobject);
1195867a482dSJohn Dyson 	}
11969b98b796SAlan Cox 	VM_OBJECT_UNLOCK(object);
1197867a482dSJohn Dyson }
1198867a482dSJohn Dyson 
1199867a482dSJohn Dyson /*
1200df8bae1dSRodney W. Grimes  *	vm_object_shadow:
1201df8bae1dSRodney W. Grimes  *
1202df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
1203df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
1204df8bae1dSRodney W. Grimes  *	object reference is deallocated.
1205df8bae1dSRodney W. Grimes  *
1206df8bae1dSRodney W. Grimes  *	The new object and offset into that object
1207df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
1208df8bae1dSRodney W. Grimes  */
120926f9a767SRodney W. Grimes void
12101b40f8c0SMatthew Dillon vm_object_shadow(
12111b40f8c0SMatthew Dillon 	vm_object_t *object,	/* IN/OUT */
12121b40f8c0SMatthew Dillon 	vm_ooffset_t *offset,	/* IN/OUT */
12131b40f8c0SMatthew Dillon 	vm_size_t length)
1214df8bae1dSRodney W. Grimes {
1215d031cff1SMatthew Dillon 	vm_object_t source;
1216d031cff1SMatthew Dillon 	vm_object_t result;
1217df8bae1dSRodney W. Grimes 
1218df8bae1dSRodney W. Grimes 	source = *object;
1219df8bae1dSRodney W. Grimes 
1220df8bae1dSRodney W. Grimes 	/*
12219a2f6362SAlan Cox 	 * Don't create the new object if the old object isn't shared.
12229a2f6362SAlan Cox 	 */
1223570a2f4aSAlan Cox 	if (source != NULL) {
1224570a2f4aSAlan Cox 		VM_OBJECT_LOCK(source);
1225570a2f4aSAlan Cox 		if (source->ref_count == 1 &&
12269a2f6362SAlan Cox 		    source->handle == NULL &&
12279a2f6362SAlan Cox 		    (source->type == OBJT_DEFAULT ||
12289917e010SAlan Cox 		     source->type == OBJT_SWAP)) {
1229570a2f4aSAlan Cox 			VM_OBJECT_UNLOCK(source);
12309a2f6362SAlan Cox 			return;
12319917e010SAlan Cox 		}
1232570a2f4aSAlan Cox 		VM_OBJECT_UNLOCK(source);
1233570a2f4aSAlan Cox 	}
12349a2f6362SAlan Cox 
12359a2f6362SAlan Cox 	/*
1236570a2f4aSAlan Cox 	 * Allocate a new object with the given length.
1237df8bae1dSRodney W. Grimes 	 */
1238971dd342SAlfred Perlstein 	result = vm_object_allocate(OBJT_DEFAULT, length);
1239df8bae1dSRodney W. Grimes 
1240df8bae1dSRodney W. Grimes 	/*
12410d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
12420d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
12430d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
12440d94caffSDavid Greenman 	 * of reference count.
12459b09fe24SMatthew Dillon 	 *
12469b09fe24SMatthew Dillon 	 * Try to optimize the result object's page color when shadowing
1247956f3135SPhilippe Charnier 	 * in order to maintain page coloring consistency in the combined
12489b09fe24SMatthew Dillon 	 * shadowed object.
1249df8bae1dSRodney W. Grimes 	 */
125024a1cce3SDavid Greenman 	result->backing_object = source;
12519174ca7bSTor Egge 	/*
12529174ca7bSTor Egge 	 * Store the offset into the source object, and fix up the offset into
12539174ca7bSTor Egge 	 * the new object.
12549174ca7bSTor Egge 	 */
12559174ca7bSTor Egge 	result->backing_object_offset = *offset;
1256570a2f4aSAlan Cox 	if (source != NULL) {
1257570a2f4aSAlan Cox 		VM_OBJECT_LOCK(source);
12581c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
1259eaf13dd7SJohn Dyson 		source->shadow_count++;
1260eaf13dd7SJohn Dyson 		source->generation++;
1261dcc5840eSAlan Cox 		if (length < source->size)
1262dcc5840eSAlan Cox 			length = source->size;
1263ef39c05bSAlexander Leidinger 		if (length > PQ_MAXLENGTH || source->generation > 1)
1264ef39c05bSAlexander Leidinger 			length = PQ_MAXLENGTH;
1265dcc5840eSAlan Cox 		result->pg_color = (source->pg_color +
1266ef39c05bSAlexander Leidinger 		    length * source->generation) & PQ_COLORMASK;
1267ed4fe4f4SJeff Roberson 		result->flags |= source->flags & OBJ_NEEDGIANT;
1268570a2f4aSAlan Cox 		VM_OBJECT_UNLOCK(source);
1269ef39c05bSAlexander Leidinger 		next_index = (result->pg_color + PQ_MAXLENGTH) & PQ_COLORMASK;
1270de5f6a77SJohn Dyson 	}
1271df8bae1dSRodney W. Grimes 
1272df8bae1dSRodney W. Grimes 
1273df8bae1dSRodney W. Grimes 	/*
1274df8bae1dSRodney W. Grimes 	 * Return the new things
1275df8bae1dSRodney W. Grimes 	 */
1276df8bae1dSRodney W. Grimes 	*offset = 0;
1277df8bae1dSRodney W. Grimes 	*object = result;
1278df8bae1dSRodney W. Grimes }
1279df8bae1dSRodney W. Grimes 
1280c5aaa06dSAlan Cox /*
1281c5aaa06dSAlan Cox  *	vm_object_split:
1282c5aaa06dSAlan Cox  *
1283c5aaa06dSAlan Cox  * Split the pages in a map entry into a new object.  This affords
1284c5aaa06dSAlan Cox  * easier removal of unused pages, and keeps object inheritance from
1285c5aaa06dSAlan Cox  * being a negative impact on memory usage.
1286c5aaa06dSAlan Cox  */
1287c5aaa06dSAlan Cox void
1288c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry)
1289c5aaa06dSAlan Cox {
129073000556SAlan Cox 	vm_page_t m, m_next;
1291c5aaa06dSAlan Cox 	vm_object_t orig_object, new_object, source;
129273000556SAlan Cox 	vm_pindex_t idx, offidxstart;
129373000556SAlan Cox 	vm_size_t size;
1294c5aaa06dSAlan Cox 
1295c5aaa06dSAlan Cox 	orig_object = entry->object.vm_object;
1296c5aaa06dSAlan Cox 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1297c5aaa06dSAlan Cox 		return;
1298c5aaa06dSAlan Cox 	if (orig_object->ref_count <= 1)
1299c5aaa06dSAlan Cox 		return;
13004da9f125SAlan Cox 	VM_OBJECT_UNLOCK(orig_object);
1301c5aaa06dSAlan Cox 
13024da9f125SAlan Cox 	offidxstart = OFF_TO_IDX(entry->offset);
130395442adfSAlan Cox 	size = atop(entry->end - entry->start);
1304c5aaa06dSAlan Cox 
13054da9f125SAlan Cox 	/*
13064da9f125SAlan Cox 	 * If swap_pager_copy() is later called, it will convert new_object
13074da9f125SAlan Cox 	 * into a swap object.
13084da9f125SAlan Cox 	 */
13094da9f125SAlan Cox 	new_object = vm_object_allocate(OBJT_DEFAULT, size);
1310c5aaa06dSAlan Cox 
131163f6cefcSAlan Cox 	VM_OBJECT_LOCK(new_object);
131263f6cefcSAlan Cox 	VM_OBJECT_LOCK(orig_object);
1313c5aaa06dSAlan Cox 	source = orig_object->backing_object;
1314c5aaa06dSAlan Cox 	if (source != NULL) {
13158e3a76fbSAlan Cox 		VM_OBJECT_LOCK(source);
13161c500307SAlan Cox 		LIST_INSERT_HEAD(&source->shadow_head,
1317c5aaa06dSAlan Cox 				  new_object, shadow_list);
13188e3a76fbSAlan Cox 		source->shadow_count++;
13198e3a76fbSAlan Cox 		source->generation++;
1320b921a12bSAlan Cox 		vm_object_reference_locked(source);	/* for new_object */
1321c5aaa06dSAlan Cox 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
1322e2479b4fSAlan Cox 		VM_OBJECT_UNLOCK(source);
1323c5aaa06dSAlan Cox 		new_object->backing_object_offset =
13244da9f125SAlan Cox 			orig_object->backing_object_offset + entry->offset;
1325c5aaa06dSAlan Cox 		new_object->backing_object = source;
1326c5aaa06dSAlan Cox 	}
1327b8a0b997SJeff Roberson 	new_object->flags |= orig_object->flags & OBJ_NEEDGIANT;
1328c5aaa06dSAlan Cox retry:
132973000556SAlan Cox 	if ((m = TAILQ_FIRST(&orig_object->memq)) != NULL) {
133073000556SAlan Cox 		if (m->pindex < offidxstart) {
133173000556SAlan Cox 			m = vm_page_splay(offidxstart, orig_object->root);
133273000556SAlan Cox 			if ((orig_object->root = m)->pindex < offidxstart)
133373000556SAlan Cox 				m = TAILQ_NEXT(m, listq);
133473000556SAlan Cox 		}
133573000556SAlan Cox 	}
133673000556SAlan Cox 	vm_page_lock_queues();
133773000556SAlan Cox 	for (; m != NULL && (idx = m->pindex - offidxstart) < size;
133873000556SAlan Cox 	    m = m_next) {
133973000556SAlan Cox 		m_next = TAILQ_NEXT(m, listq);
1340c5aaa06dSAlan Cox 
1341c5aaa06dSAlan Cox 		/*
1342c5aaa06dSAlan Cox 		 * We must wait for pending I/O to complete before we can
1343c5aaa06dSAlan Cox 		 * rename the page.
1344c5aaa06dSAlan Cox 		 *
1345c5aaa06dSAlan Cox 		 * We do not have to VM_PROT_NONE the page as mappings should
1346c5aaa06dSAlan Cox 		 * not be changed by this operation.
1347c5aaa06dSAlan Cox 		 */
13489af80719SAlan Cox 		if ((m->oflags & VPO_BUSY) || m->busy) {
13495786be7cSAlan Cox 			vm_page_flag_set(m, PG_REFERENCED);
135091449ce9SAlan Cox 			vm_page_unlock_queues();
1351de33beddSAlan Cox 			VM_OBJECT_UNLOCK(new_object);
13525786be7cSAlan Cox 			m->oflags |= VPO_WANTED;
135391449ce9SAlan Cox 			msleep(m, VM_OBJECT_MTX(orig_object), PDROP | PVM, "spltwt", 0);
1354de33beddSAlan Cox 			VM_OBJECT_LOCK(new_object);
1355de33beddSAlan Cox 			VM_OBJECT_LOCK(orig_object);
1356c5aaa06dSAlan Cox 			goto retry;
1357de33beddSAlan Cox 		}
1358c5aaa06dSAlan Cox 		vm_page_rename(m, new_object, idx);
1359c5aaa06dSAlan Cox 		/* page automatically made dirty by rename and cache handled */
1360c5aaa06dSAlan Cox 		vm_page_busy(m);
1361c5aaa06dSAlan Cox 	}
13625ba514bcSAlan Cox 	vm_page_unlock_queues();
1363d7a013c3SAlan Cox 	if (orig_object->type == OBJT_SWAP) {
1364c5aaa06dSAlan Cox 		/*
1365c7c8dd7eSAlan Cox 		 * swap_pager_copy() can sleep, in which case the orig_object's
1366c7c8dd7eSAlan Cox 		 * and new_object's locks are released and reacquired.
1367c5aaa06dSAlan Cox 		 */
1368c5aaa06dSAlan Cox 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
1369c5aaa06dSAlan Cox 	}
1370d7a013c3SAlan Cox 	VM_OBJECT_UNLOCK(orig_object);
1371c7118ed6SAlan Cox 	TAILQ_FOREACH(m, &new_object->memq, listq)
1372c5aaa06dSAlan Cox 		vm_page_wakeup(m);
1373c7c8dd7eSAlan Cox 	VM_OBJECT_UNLOCK(new_object);
1374c5aaa06dSAlan Cox 	entry->object.vm_object = new_object;
1375c5aaa06dSAlan Cox 	entry->offset = 0LL;
1376c5aaa06dSAlan Cox 	vm_object_deallocate(orig_object);
13774da9f125SAlan Cox 	VM_OBJECT_LOCK(new_object);
1378c5aaa06dSAlan Cox }
1379c5aaa06dSAlan Cox 
13802ad1a3f7SMatthew Dillon #define	OBSC_TEST_ALL_SHADOWED	0x0001
13812ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_NOWAIT	0x0002
13822ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_WAIT	0x0004
13832ad1a3f7SMatthew Dillon 
1384b4ae4780SPoul-Henning Kamp static int
13852ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op)
13862ad1a3f7SMatthew Dillon {
13872ad1a3f7SMatthew Dillon 	int r = 1;
13882ad1a3f7SMatthew Dillon 	vm_page_t p;
13892ad1a3f7SMatthew Dillon 	vm_object_t backing_object;
13902ad1a3f7SMatthew Dillon 	vm_pindex_t backing_offset_index;
13912ad1a3f7SMatthew Dillon 
13927ca33ad1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
13937ca33ad1SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED);
13942ad1a3f7SMatthew Dillon 
13952ad1a3f7SMatthew Dillon 	backing_object = object->backing_object;
13962ad1a3f7SMatthew Dillon 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
13972ad1a3f7SMatthew Dillon 
13982ad1a3f7SMatthew Dillon 	/*
13992ad1a3f7SMatthew Dillon 	 * Initial conditions
14002ad1a3f7SMatthew Dillon 	 */
14012ad1a3f7SMatthew Dillon 	if (op & OBSC_TEST_ALL_SHADOWED) {
14022ad1a3f7SMatthew Dillon 		/*
1403956f3135SPhilippe Charnier 		 * We do not want to have to test for the existence of
14042ad1a3f7SMatthew Dillon 		 * swap pages in the backing object.  XXX but with the
14052ad1a3f7SMatthew Dillon 		 * new swapper this would be pretty easy to do.
14062ad1a3f7SMatthew Dillon 		 *
14072ad1a3f7SMatthew Dillon 		 * XXX what about anonymous MAP_SHARED memory that hasn't
14082ad1a3f7SMatthew Dillon 		 * been ZFOD faulted yet?  If we do not test for this, the
14092ad1a3f7SMatthew Dillon 		 * shadow test may succeed! XXX
14102ad1a3f7SMatthew Dillon 		 */
14112ad1a3f7SMatthew Dillon 		if (backing_object->type != OBJT_DEFAULT) {
14122ad1a3f7SMatthew Dillon 			return (0);
14132ad1a3f7SMatthew Dillon 		}
14142ad1a3f7SMatthew Dillon 	}
14152ad1a3f7SMatthew Dillon 	if (op & OBSC_COLLAPSE_WAIT) {
14162ad1a3f7SMatthew Dillon 		vm_object_set_flag(backing_object, OBJ_DEAD);
14172ad1a3f7SMatthew Dillon 	}
14182ad1a3f7SMatthew Dillon 
14192ad1a3f7SMatthew Dillon 	/*
14202ad1a3f7SMatthew Dillon 	 * Our scan
14212ad1a3f7SMatthew Dillon 	 */
14222ad1a3f7SMatthew Dillon 	p = TAILQ_FIRST(&backing_object->memq);
14232ad1a3f7SMatthew Dillon 	while (p) {
14242ad1a3f7SMatthew Dillon 		vm_page_t next = TAILQ_NEXT(p, listq);
14252ad1a3f7SMatthew Dillon 		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
14262ad1a3f7SMatthew Dillon 
14272ad1a3f7SMatthew Dillon 		if (op & OBSC_TEST_ALL_SHADOWED) {
14282ad1a3f7SMatthew Dillon 			vm_page_t pp;
14292ad1a3f7SMatthew Dillon 
14302ad1a3f7SMatthew Dillon 			/*
14312ad1a3f7SMatthew Dillon 			 * Ignore pages outside the parent object's range
14322ad1a3f7SMatthew Dillon 			 * and outside the parent object's mapping of the
14332ad1a3f7SMatthew Dillon 			 * backing object.
14342ad1a3f7SMatthew Dillon 			 *
14352ad1a3f7SMatthew Dillon 			 * note that we do not busy the backing object's
14362ad1a3f7SMatthew Dillon 			 * page.
14372ad1a3f7SMatthew Dillon 			 */
14382ad1a3f7SMatthew Dillon 			if (
14392ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
14402ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
14412ad1a3f7SMatthew Dillon 			) {
14422ad1a3f7SMatthew Dillon 				p = next;
14432ad1a3f7SMatthew Dillon 				continue;
14442ad1a3f7SMatthew Dillon 			}
14452ad1a3f7SMatthew Dillon 
14462ad1a3f7SMatthew Dillon 			/*
14472ad1a3f7SMatthew Dillon 			 * See if the parent has the page or if the parent's
14482ad1a3f7SMatthew Dillon 			 * object pager has the page.  If the parent has the
14492ad1a3f7SMatthew Dillon 			 * page but the page is not valid, the parent's
14502ad1a3f7SMatthew Dillon 			 * object pager must have the page.
14512ad1a3f7SMatthew Dillon 			 *
14522ad1a3f7SMatthew Dillon 			 * If this fails, the parent does not completely shadow
14532ad1a3f7SMatthew Dillon 			 * the object and we might as well give up now.
14542ad1a3f7SMatthew Dillon 			 */
14552ad1a3f7SMatthew Dillon 
14562ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
14572ad1a3f7SMatthew Dillon 			if (
14582ad1a3f7SMatthew Dillon 			    (pp == NULL || pp->valid == 0) &&
14592ad1a3f7SMatthew Dillon 			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
14602ad1a3f7SMatthew Dillon 			) {
14612ad1a3f7SMatthew Dillon 				r = 0;
14622ad1a3f7SMatthew Dillon 				break;
14632ad1a3f7SMatthew Dillon 			}
14642ad1a3f7SMatthew Dillon 		}
14652ad1a3f7SMatthew Dillon 
14662ad1a3f7SMatthew Dillon 		/*
14672ad1a3f7SMatthew Dillon 		 * Check for busy page
14682ad1a3f7SMatthew Dillon 		 */
14692ad1a3f7SMatthew Dillon 		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
14702ad1a3f7SMatthew Dillon 			vm_page_t pp;
14712ad1a3f7SMatthew Dillon 
14722ad1a3f7SMatthew Dillon 			if (op & OBSC_COLLAPSE_NOWAIT) {
14739af80719SAlan Cox 				if ((p->oflags & VPO_BUSY) ||
14742ad1a3f7SMatthew Dillon 				    !p->valid ||
147500f9e8b4SAlan Cox 				    p->busy) {
14762ad1a3f7SMatthew Dillon 					p = next;
14772ad1a3f7SMatthew Dillon 					continue;
14782ad1a3f7SMatthew Dillon 				}
14792ad1a3f7SMatthew Dillon 			} else if (op & OBSC_COLLAPSE_WAIT) {
14809af80719SAlan Cox 				if ((p->oflags & VPO_BUSY) || p->busy) {
1481c6ec6a7cSAlan Cox 					vm_page_lock_queues();
14825786be7cSAlan Cox 					vm_page_flag_set(p, PG_REFERENCED);
148391449ce9SAlan Cox 					vm_page_unlock_queues();
14847ca33ad1SAlan Cox 					VM_OBJECT_UNLOCK(object);
14855786be7cSAlan Cox 					p->oflags |= VPO_WANTED;
148691449ce9SAlan Cox 					msleep(p, VM_OBJECT_MTX(backing_object),
14877ca33ad1SAlan Cox 					    PDROP | PVM, "vmocol", 0);
14887ca33ad1SAlan Cox 					VM_OBJECT_LOCK(object);
14897ca33ad1SAlan Cox 					VM_OBJECT_LOCK(backing_object);
14902ad1a3f7SMatthew Dillon 					/*
14912ad1a3f7SMatthew Dillon 					 * If we slept, anything could have
14922ad1a3f7SMatthew Dillon 					 * happened.  Since the object is
14932ad1a3f7SMatthew Dillon 					 * marked dead, the backing offset
14942ad1a3f7SMatthew Dillon 					 * should not have changed so we
14952ad1a3f7SMatthew Dillon 					 * just restart our scan.
14962ad1a3f7SMatthew Dillon 					 */
14972ad1a3f7SMatthew Dillon 					p = TAILQ_FIRST(&backing_object->memq);
14982ad1a3f7SMatthew Dillon 					continue;
14992ad1a3f7SMatthew Dillon 				}
15002ad1a3f7SMatthew Dillon 			}
15012ad1a3f7SMatthew Dillon 
15022ad1a3f7SMatthew Dillon 			KASSERT(
15032ad1a3f7SMatthew Dillon 			    p->object == backing_object,
15048e99783bSAlan Cox 			    ("vm_object_backing_scan: object mismatch")
15052ad1a3f7SMatthew Dillon 			);
15062ad1a3f7SMatthew Dillon 
15072ad1a3f7SMatthew Dillon 			/*
15082ad1a3f7SMatthew Dillon 			 * Destroy any associated swap
15092ad1a3f7SMatthew Dillon 			 */
15102ad1a3f7SMatthew Dillon 			if (backing_object->type == OBJT_SWAP) {
15112ad1a3f7SMatthew Dillon 				swap_pager_freespace(
15122ad1a3f7SMatthew Dillon 				    backing_object,
15132ad1a3f7SMatthew Dillon 				    p->pindex,
15142ad1a3f7SMatthew Dillon 				    1
15152ad1a3f7SMatthew Dillon 				);
15162ad1a3f7SMatthew Dillon 			}
15172ad1a3f7SMatthew Dillon 
15182ad1a3f7SMatthew Dillon 			if (
15192ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
15202ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
15212ad1a3f7SMatthew Dillon 			) {
15222ad1a3f7SMatthew Dillon 				/*
15232ad1a3f7SMatthew Dillon 				 * Page is out of the parent object's range, we
15242ad1a3f7SMatthew Dillon 				 * can simply destroy it.
15252ad1a3f7SMatthew Dillon 				 */
15266a684ecfSAlan Cox 				vm_page_lock_queues();
1527f6d89838SAlan Cox 				KASSERT(!pmap_page_is_mapped(p),
1528f6d89838SAlan Cox 				    ("freeing mapped page %p", p));
1529f6d89838SAlan Cox 				if (p->wire_count == 0)
15302ad1a3f7SMatthew Dillon 					vm_page_free(p);
1531f6d89838SAlan Cox 				else
1532f6d89838SAlan Cox 					vm_page_remove(p);
15336a684ecfSAlan Cox 				vm_page_unlock_queues();
15342ad1a3f7SMatthew Dillon 				p = next;
15352ad1a3f7SMatthew Dillon 				continue;
15362ad1a3f7SMatthew Dillon 			}
15372ad1a3f7SMatthew Dillon 
15382ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
15392ad1a3f7SMatthew Dillon 			if (
15402ad1a3f7SMatthew Dillon 			    pp != NULL ||
15412ad1a3f7SMatthew Dillon 			    vm_pager_has_page(object, new_pindex, NULL, NULL)
15422ad1a3f7SMatthew Dillon 			) {
15432ad1a3f7SMatthew Dillon 				/*
15442ad1a3f7SMatthew Dillon 				 * page already exists in parent OR swap exists
15452ad1a3f7SMatthew Dillon 				 * for this location in the parent.  Destroy
15462ad1a3f7SMatthew Dillon 				 * the original page from the backing object.
15472ad1a3f7SMatthew Dillon 				 *
15482ad1a3f7SMatthew Dillon 				 * Leave the parent's page alone
15492ad1a3f7SMatthew Dillon 				 */
15506a684ecfSAlan Cox 				vm_page_lock_queues();
1551f6d89838SAlan Cox 				KASSERT(!pmap_page_is_mapped(p),
1552f6d89838SAlan Cox 				    ("freeing mapped page %p", p));
1553f6d89838SAlan Cox 				if (p->wire_count == 0)
15542ad1a3f7SMatthew Dillon 					vm_page_free(p);
1555f6d89838SAlan Cox 				else
1556f6d89838SAlan Cox 					vm_page_remove(p);
15576a684ecfSAlan Cox 				vm_page_unlock_queues();
15582ad1a3f7SMatthew Dillon 				p = next;
15592ad1a3f7SMatthew Dillon 				continue;
15602ad1a3f7SMatthew Dillon 			}
15612ad1a3f7SMatthew Dillon 
15622ad1a3f7SMatthew Dillon 			/*
15632ad1a3f7SMatthew Dillon 			 * Page does not exist in parent, rename the
15642ad1a3f7SMatthew Dillon 			 * page from the backing object to the main object.
1565d1bf5d56SMatthew Dillon 			 *
1566d1bf5d56SMatthew Dillon 			 * If the page was mapped to a process, it can remain
1567d1bf5d56SMatthew Dillon 			 * mapped through the rename.
15682ad1a3f7SMatthew Dillon 			 */
1569a28cc55eSAlan Cox 			vm_page_lock_queues();
15702ad1a3f7SMatthew Dillon 			vm_page_rename(p, object, new_pindex);
1571a28cc55eSAlan Cox 			vm_page_unlock_queues();
15722ad1a3f7SMatthew Dillon 			/* page automatically made dirty by rename */
15732ad1a3f7SMatthew Dillon 		}
15742ad1a3f7SMatthew Dillon 		p = next;
15752ad1a3f7SMatthew Dillon 	}
15762ad1a3f7SMatthew Dillon 	return (r);
15772ad1a3f7SMatthew Dillon }
15782ad1a3f7SMatthew Dillon 
1579df8bae1dSRodney W. Grimes 
1580df8bae1dSRodney W. Grimes /*
15812fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
15822fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
15832fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
15842fe6e4d7SDavid Greenman  */
15852fe6e4d7SDavid Greenman static void
15861b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object)
15872fe6e4d7SDavid Greenman {
15882ad1a3f7SMatthew Dillon 	vm_object_t backing_object = object->backing_object;
15892fe6e4d7SDavid Greenman 
159006ecade7SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
159106ecade7SAlan Cox 	VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED);
15921b40f8c0SMatthew Dillon 
15932fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
15942fe6e4d7SDavid Greenman 		return;
15952fe6e4d7SDavid Greenman 
15962ad1a3f7SMatthew Dillon 	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
15972fe6e4d7SDavid Greenman }
15982fe6e4d7SDavid Greenman 
1599df8bae1dSRodney W. Grimes /*
1600df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1601df8bae1dSRodney W. Grimes  *
1602df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1603df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1604df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1605df8bae1dSRodney W. Grimes  */
160626f9a767SRodney W. Grimes void
16071b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object)
1608df8bae1dSRodney W. Grimes {
1609d7fc2210SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
161023955314SAlfred Perlstein 
1611df8bae1dSRodney W. Grimes 	while (TRUE) {
16122ad1a3f7SMatthew Dillon 		vm_object_t backing_object;
16132ad1a3f7SMatthew Dillon 
1614df8bae1dSRodney W. Grimes 		/*
1615df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1616df8bae1dSRodney W. Grimes 		 *
16172ad1a3f7SMatthew Dillon 		 * The object exists and the backing object exists.
1618df8bae1dSRodney W. Grimes 		 */
161924a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
16202ad1a3f7SMatthew Dillon 			break;
1621df8bae1dSRodney W. Grimes 
1622f919ebdeSDavid Greenman 		/*
1623f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
162424a1cce3SDavid Greenman 		 * not collapsable.
1625f919ebdeSDavid Greenman 		 */
162640b808a8SAlan Cox 		VM_OBJECT_LOCK(backing_object);
162724a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
162824a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
162924a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1630f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
163124a1cce3SDavid Greenman 		    object->handle != NULL ||
163224a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
163324a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
163424a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
163540b808a8SAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
16362ad1a3f7SMatthew Dillon 			break;
163724a1cce3SDavid Greenman 		}
16389b4814bbSDavid Greenman 
16392ad1a3f7SMatthew Dillon 		if (
16402ad1a3f7SMatthew Dillon 		    object->paging_in_progress != 0 ||
16412ad1a3f7SMatthew Dillon 		    backing_object->paging_in_progress != 0
16422ad1a3f7SMatthew Dillon 		) {
1643b9921222SDavid Greenman 			vm_object_qcollapse(object);
164440b808a8SAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
16452ad1a3f7SMatthew Dillon 			break;
1646df8bae1dSRodney W. Grimes 		}
164726f9a767SRodney W. Grimes 		/*
16480d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
16492ad1a3f7SMatthew Dillon 		 * the parent is the only reference to it) or (perhaps) have
16502ad1a3f7SMatthew Dillon 		 * the parent bypass the object if the parent happens to shadow
16512ad1a3f7SMatthew Dillon 		 * all the resident pages in the entire backing object.
16522ad1a3f7SMatthew Dillon 		 *
16532ad1a3f7SMatthew Dillon 		 * This is ignoring pager-backed pages such as swap pages.
16542ad1a3f7SMatthew Dillon 		 * vm_object_backing_scan fails the shadowing test in this
16552ad1a3f7SMatthew Dillon 		 * case.
1656df8bae1dSRodney W. Grimes 		 */
1657df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1658df8bae1dSRodney W. Grimes 			/*
16592ad1a3f7SMatthew Dillon 			 * If there is exactly one reference to the backing
16602ad1a3f7SMatthew Dillon 			 * object, we can collapse it into the parent.
1661df8bae1dSRodney W. Grimes 			 */
16622ad1a3f7SMatthew Dillon 			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1663df8bae1dSRodney W. Grimes 
1664df8bae1dSRodney W. Grimes 			/*
1665df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1666df8bae1dSRodney W. Grimes 			 */
16676be36525SAlan Cox 			if (backing_object->type == OBJT_SWAP) {
166824a1cce3SDavid Greenman 				/*
1669c7c8dd7eSAlan Cox 				 * swap_pager_copy() can sleep, in which case
1670c7c8dd7eSAlan Cox 				 * the backing_object's and object's locks are
1671c7c8dd7eSAlan Cox 				 * released and reacquired.
167224a1cce3SDavid Greenman 				 */
16731c7c3c6aSMatthew Dillon 				swap_pager_copy(
16741c7c3c6aSMatthew Dillon 				    backing_object,
16751c7c3c6aSMatthew Dillon 				    object,
16761c7c3c6aSMatthew Dillon 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1677c0503609SDavid Greenman 			}
1678df8bae1dSRodney W. Grimes 			/*
1679df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
16802ad1a3f7SMatthew Dillon 			 * Note that the reference to
16812ad1a3f7SMatthew Dillon 			 * backing_object->backing_object moves from within
16822ad1a3f7SMatthew Dillon 			 * backing_object to within object.
1683df8bae1dSRodney W. Grimes 			 */
16841c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
16854f7c7f6eSAlan Cox 			backing_object->shadow_count--;
16864f7c7f6eSAlan Cox 			backing_object->generation++;
1687de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
16886be36525SAlan Cox 				VM_OBJECT_LOCK(backing_object->backing_object);
16891c500307SAlan Cox 				LIST_REMOVE(backing_object, shadow_list);
169043186e53SAlan Cox 				LIST_INSERT_HEAD(
169143186e53SAlan Cox 				    &backing_object->backing_object->shadow_head,
169243186e53SAlan Cox 				    object, shadow_list);
169343186e53SAlan Cox 				/*
169443186e53SAlan Cox 				 * The shadow_count has not changed.
169543186e53SAlan Cox 				 */
1696eaf13dd7SJohn Dyson 				backing_object->backing_object->generation++;
16976be36525SAlan Cox 				VM_OBJECT_UNLOCK(backing_object->backing_object);
1698de5f6a77SJohn Dyson 			}
169924a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
17002ad1a3f7SMatthew Dillon 			object->backing_object_offset +=
17012ad1a3f7SMatthew Dillon 			    backing_object->backing_object_offset;
17022ad1a3f7SMatthew Dillon 
1703df8bae1dSRodney W. Grimes 			/*
1704df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1705df8bae1dSRodney W. Grimes 			 *
17060d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
17070d94caffSDavid Greenman 			 * and no object references within it, all that is
17080d94caffSDavid Greenman 			 * necessary is to dispose of it.
1709df8bae1dSRodney W. Grimes 			 */
1710245df27cSMatthew Dillon 			KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object));
17116be36525SAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
1712df8bae1dSRodney W. Grimes 
1713a5698387SAlan Cox 			mtx_lock(&vm_object_list_mtx);
17142ad1a3f7SMatthew Dillon 			TAILQ_REMOVE(
17152ad1a3f7SMatthew Dillon 			    &vm_object_list,
17162ad1a3f7SMatthew Dillon 			    backing_object,
17172ad1a3f7SMatthew Dillon 			    object_list
17182ad1a3f7SMatthew Dillon 			);
1719a5698387SAlan Cox 			mtx_unlock(&vm_object_list_mtx);
1720df8bae1dSRodney W. Grimes 
1721670d17b5SJeff Roberson 			uma_zfree(obj_zone, backing_object);
1722df8bae1dSRodney W. Grimes 
1723df8bae1dSRodney W. Grimes 			object_collapses++;
17240d94caffSDavid Greenman 		} else {
172595e5e988SJohn Dyson 			vm_object_t new_backing_object;
1726df8bae1dSRodney W. Grimes 
1727df8bae1dSRodney W. Grimes 			/*
17282ad1a3f7SMatthew Dillon 			 * If we do not entirely shadow the backing object,
17292ad1a3f7SMatthew Dillon 			 * there is nothing we can do so we give up.
1730df8bae1dSRodney W. Grimes 			 */
1731df59a0feSJeff Roberson 			if (object->resident_page_count != object->size &&
1732df59a0feSJeff Roberson 			    vm_object_backing_scan(object,
1733df59a0feSJeff Roberson 			    OBSC_TEST_ALL_SHADOWED) == 0) {
173440b808a8SAlan Cox 				VM_OBJECT_UNLOCK(backing_object);
17352ad1a3f7SMatthew Dillon 				break;
173624a1cce3SDavid Greenman 			}
1737df8bae1dSRodney W. Grimes 
1738df8bae1dSRodney W. Grimes 			/*
17390d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
17400d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
17410d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1742df8bae1dSRodney W. Grimes 			 */
17431c500307SAlan Cox 			LIST_REMOVE(object, shadow_list);
1744eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
1745eaf13dd7SJohn Dyson 			backing_object->generation++;
174695e5e988SJohn Dyson 
174795e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
17488aef1712SMatthew Dillon 			if ((object->backing_object = new_backing_object) != NULL) {
17496be36525SAlan Cox 				VM_OBJECT_LOCK(new_backing_object);
17501c500307SAlan Cox 				LIST_INSERT_HEAD(
17512ad1a3f7SMatthew Dillon 				    &new_backing_object->shadow_head,
17522ad1a3f7SMatthew Dillon 				    object,
17532ad1a3f7SMatthew Dillon 				    shadow_list
17542ad1a3f7SMatthew Dillon 				);
1755eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1756eaf13dd7SJohn Dyson 				new_backing_object->generation++;
1757b921a12bSAlan Cox 				vm_object_reference_locked(new_backing_object);
17586be36525SAlan Cox 				VM_OBJECT_UNLOCK(new_backing_object);
175995e5e988SJohn Dyson 				object->backing_object_offset +=
176095e5e988SJohn Dyson 					backing_object->backing_object_offset;
1761de5f6a77SJohn Dyson 			}
1762df8bae1dSRodney W. Grimes 
1763df8bae1dSRodney W. Grimes 			/*
17640d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
176522ec553fSAlan Cox 			 * its ref_count was at least 2, it will not vanish.
1766df8bae1dSRodney W. Grimes 			 */
176722ec553fSAlan Cox 			backing_object->ref_count--;
176822ec553fSAlan Cox 			VM_OBJECT_UNLOCK(backing_object);
1769df8bae1dSRodney W. Grimes 			object_bypasses++;
1770df8bae1dSRodney W. Grimes 		}
1771df8bae1dSRodney W. Grimes 
1772df8bae1dSRodney W. Grimes 		/*
1773df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1774df8bae1dSRodney W. Grimes 		 */
1775df8bae1dSRodney W. Grimes 	}
1776df8bae1dSRodney W. Grimes }
1777df8bae1dSRodney W. Grimes 
1778df8bae1dSRodney W. Grimes /*
1779bff99f0dSAlan Cox  *	vm_object_page_remove:
1780df8bae1dSRodney W. Grimes  *
1781bff99f0dSAlan Cox  *	Removes all physical pages in the given range from the
1782bff99f0dSAlan Cox  *	object's list of pages.  If the range's end is zero, all
1783bff99f0dSAlan Cox  *	physical pages from the range's start to the end of the object
1784bff99f0dSAlan Cox  *	are deleted.
1785df8bae1dSRodney W. Grimes  *
1786df8bae1dSRodney W. Grimes  *	The object must be locked.
1787df8bae1dSRodney W. Grimes  */
178826f9a767SRodney W. Grimes void
1789ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
1790ecde4b32SAlan Cox     boolean_t clean_only)
1791df8bae1dSRodney W. Grimes {
1792d031cff1SMatthew Dillon 	vm_page_t p, next;
1793df8bae1dSRodney W. Grimes 
1794ecde4b32SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1795ecde4b32SAlan Cox 	if (object->resident_page_count == 0)
1796df8bae1dSRodney W. Grimes 		return;
179795e5e988SJohn Dyson 
17988b03c8edSMatthew Dillon 	/*
17998b03c8edSMatthew Dillon 	 * Since physically-backed objects do not use managed pages, we can't
18008b03c8edSMatthew Dillon 	 * remove pages from the object (we must instead remove the page
18018b03c8edSMatthew Dillon 	 * references, and then destroy the object).
18028b03c8edSMatthew Dillon 	 */
1803ecde4b32SAlan Cox 	KASSERT(object->type != OBJT_PHYS,
1804ecde4b32SAlan Cox 	    ("attempt to remove pages from a physical object"));
18058b03c8edSMatthew Dillon 
1806d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
180726f9a767SRodney W. Grimes again:
180832585dd6SAlan Cox 	vm_page_lock_queues();
180975741c04SAlan Cox 	if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
181075741c04SAlan Cox 		if (p->pindex < start) {
181175741c04SAlan Cox 			p = vm_page_splay(start, object->root);
181275741c04SAlan Cox 			if ((object->root = p)->pindex < start)
181375741c04SAlan Cox 				p = TAILQ_NEXT(p, listq);
181475741c04SAlan Cox 		}
181575741c04SAlan Cox 	}
181675741c04SAlan Cox 	/*
181775741c04SAlan Cox 	 * Assert: the variable p is either (1) the page with the
181875741c04SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
181975741c04SAlan Cox 	 * or (2) NULL.
182075741c04SAlan Cox 	 */
182175741c04SAlan Cox 	for (;
1822bff99f0dSAlan Cox 	     p != NULL && (p->pindex < end || end == 0);
182375741c04SAlan Cox 	     p = next) {
1824b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
182575741c04SAlan Cox 
1826bd7e5f99SJohn Dyson 		if (p->wire_count != 0) {
18274fec79beSAlan Cox 			pmap_remove_all(p);
1828ce65e68cSDavid Greenman 			if (!clean_only)
1829bd7e5f99SJohn Dyson 				p->valid = 0;
18300d94caffSDavid Greenman 			continue;
18310d94caffSDavid Greenman 		}
183232585dd6SAlan Cox 		if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
183326f9a767SRodney W. Grimes 			goto again;
18348f9110f6SJohn Dyson 		if (clean_only && p->valid) {
183578985e42SAlan Cox 			pmap_remove_write(p);
18367c1f6cedSDavid Greenman 			if (p->valid & p->dirty)
18377c1f6cedSDavid Greenman 				continue;
18387c1f6cedSDavid Greenman 		}
18394fec79beSAlan Cox 		pmap_remove_all(p);
1840df8bae1dSRodney W. Grimes 		vm_page_free(p);
184126f9a767SRodney W. Grimes 	}
184232585dd6SAlan Cox 	vm_page_unlock_queues();
1843f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
1844c0503609SDavid Greenman }
1845df8bae1dSRodney W. Grimes 
1846df8bae1dSRodney W. Grimes /*
1847df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1848df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1849df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1850df8bae1dSRodney W. Grimes  *
1851df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1852df8bae1dSRodney W. Grimes  *
1853df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1854df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1855df8bae1dSRodney W. Grimes  *
1856df8bae1dSRodney W. Grimes  *	Parameters:
1857df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1858df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1859df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
186057a21abaSAlan Cox  *		next_size	Size of reference to the second object
1861df8bae1dSRodney W. Grimes  *
1862df8bae1dSRodney W. Grimes  *	Conditions:
1863df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1864df8bae1dSRodney W. Grimes  */
18650d94caffSDavid Greenman boolean_t
186657a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
186700e1854aSAlan Cox 	vm_size_t prev_size, vm_size_t next_size)
1868df8bae1dSRodney W. Grimes {
1869ea41812fSAlan Cox 	vm_pindex_t next_pindex;
1870df8bae1dSRodney W. Grimes 
187100e1854aSAlan Cox 	if (prev_object == NULL)
1872df8bae1dSRodney W. Grimes 		return (TRUE);
1873bdbfbaafSAlan Cox 	VM_OBJECT_LOCK(prev_object);
18744112823fSMatthew Dillon 	if (prev_object->type != OBJT_DEFAULT &&
18754112823fSMatthew Dillon 	    prev_object->type != OBJT_SWAP) {
1876bdbfbaafSAlan Cox 		VM_OBJECT_UNLOCK(prev_object);
187730dcfc09SJohn Dyson 		return (FALSE);
187830dcfc09SJohn Dyson 	}
187930dcfc09SJohn Dyson 
1880df8bae1dSRodney W. Grimes 	/*
1881df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1882df8bae1dSRodney W. Grimes 	 */
1883df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1884df8bae1dSRodney W. Grimes 
1885df8bae1dSRodney W. Grimes 	/*
18860d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
18870d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
18880d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1889df8bae1dSRodney W. Grimes 	 */
18908cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
1891bdbfbaafSAlan Cox 		VM_OBJECT_UNLOCK(prev_object);
1892df8bae1dSRodney W. Grimes 		return (FALSE);
1893df8bae1dSRodney W. Grimes 	}
1894a316d390SJohn Dyson 
1895a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
1896a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
189757a21abaSAlan Cox 	next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
18988cc7e047SJohn Dyson 
18998cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
1900ea41812fSAlan Cox 	    (prev_object->size != next_pindex)) {
1901bdbfbaafSAlan Cox 		VM_OBJECT_UNLOCK(prev_object);
19028cc7e047SJohn Dyson 		return (FALSE);
19038cc7e047SJohn Dyson 	}
19048cc7e047SJohn Dyson 
1905df8bae1dSRodney W. Grimes 	/*
19060d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
19070d94caffSDavid Greenman 	 * deallocation.
1908df8bae1dSRodney W. Grimes 	 */
1909ea41812fSAlan Cox 	if (next_pindex < prev_object->size) {
1910df8bae1dSRodney W. Grimes 		vm_object_page_remove(prev_object,
1911ea41812fSAlan Cox 				      next_pindex,
1912ea41812fSAlan Cox 				      next_pindex + next_size, FALSE);
1913ea41812fSAlan Cox 		if (prev_object->type == OBJT_SWAP)
1914ea41812fSAlan Cox 			swap_pager_freespace(prev_object,
1915ea41812fSAlan Cox 					     next_pindex, next_size);
1916ea41812fSAlan Cox 	}
1917df8bae1dSRodney W. Grimes 
1918df8bae1dSRodney W. Grimes 	/*
1919df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1920df8bae1dSRodney W. Grimes 	 */
1921ea41812fSAlan Cox 	if (next_pindex + next_size > prev_object->size)
1922ea41812fSAlan Cox 		prev_object->size = next_pindex + next_size;
1923df8bae1dSRodney W. Grimes 
1924bdbfbaafSAlan Cox 	VM_OBJECT_UNLOCK(prev_object);
1925df8bae1dSRodney W. Grimes 	return (TRUE);
1926df8bae1dSRodney W. Grimes }
1927df8bae1dSRodney W. Grimes 
19287a5a6352SMatthew Dillon void
19297a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object)
19307a5a6352SMatthew Dillon {
19317a5a6352SMatthew Dillon 	struct vnode *vp;
19327a5a6352SMatthew Dillon 
1933de33beddSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1934af51d7bfSAlan Cox 	if ((object->flags & OBJ_MIGHTBEDIRTY) != 0)
1935ee39666aSJeff Roberson 		return;
1936af51d7bfSAlan Cox 	vm_object_set_flag(object, OBJ_MIGHTBEDIRTY);
19377a5a6352SMatthew Dillon 	if (object->type == OBJT_VNODE &&
19387a5a6352SMatthew Dillon 	    (vp = (struct vnode *)object->handle) != NULL) {
1939e6e370a7SJeff Roberson 		VI_LOCK(vp);
1940e6e370a7SJeff Roberson 		vp->v_iflag |= VI_OBJDIRTY;
1941e6e370a7SJeff Roberson 		VI_UNLOCK(vp);
19427a5a6352SMatthew Dillon 	}
19437a5a6352SMatthew Dillon }
19447a5a6352SMatthew Dillon 
1945c7c34a24SBruce Evans #include "opt_ddb.h"
1946c3cb3e12SDavid Greenman #ifdef DDB
1947c7c34a24SBruce Evans #include <sys/kernel.h>
1948c7c34a24SBruce Evans 
1949ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
1950c7c34a24SBruce Evans 
1951c7c34a24SBruce Evans #include <ddb/ddb.h>
1952c7c34a24SBruce Evans 
1953cac597e4SBruce Evans static int
19541b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
1955a1f6d91cSDavid Greenman {
1956a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1957a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1958a1f6d91cSDavid Greenman 	vm_object_t obj;
1959a1f6d91cSDavid Greenman 	int entcount;
1960a1f6d91cSDavid Greenman 
1961a1f6d91cSDavid Greenman 	if (map == 0)
1962a1f6d91cSDavid Greenman 		return 0;
1963a1f6d91cSDavid Greenman 
1964a1f6d91cSDavid Greenman 	if (entry == 0) {
1965a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1966a1f6d91cSDavid Greenman 		entcount = map->nentries;
1967a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1968a1f6d91cSDavid Greenman 			if (_vm_object_in_map(map, object, tmpe)) {
1969a1f6d91cSDavid Greenman 				return 1;
1970a1f6d91cSDavid Greenman 			}
1971a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1972a1f6d91cSDavid Greenman 		}
19739fdfe602SMatthew Dillon 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
19749fdfe602SMatthew Dillon 		tmpm = entry->object.sub_map;
1975a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1976a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1977a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1978a1f6d91cSDavid Greenman 			if (_vm_object_in_map(tmpm, object, tmpe)) {
1979a1f6d91cSDavid Greenman 				return 1;
1980a1f6d91cSDavid Greenman 			}
1981a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1982a1f6d91cSDavid Greenman 		}
19838aef1712SMatthew Dillon 	} else if ((obj = entry->object.vm_object) != NULL) {
198424a1cce3SDavid Greenman 		for (; obj; obj = obj->backing_object)
1985a1f6d91cSDavid Greenman 			if (obj == object) {
1986a1f6d91cSDavid Greenman 				return 1;
1987a1f6d91cSDavid Greenman 			}
1988a1f6d91cSDavid Greenman 	}
1989a1f6d91cSDavid Greenman 	return 0;
1990a1f6d91cSDavid Greenman }
1991a1f6d91cSDavid Greenman 
1992cac597e4SBruce Evans static int
19931b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object)
1994a1f6d91cSDavid Greenman {
1995a1f6d91cSDavid Greenman 	struct proc *p;
19961005a129SJohn Baldwin 
199760517fd1SJohn Baldwin 	/* sx_slock(&allproc_lock); */
1998fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(p, &allproc, p_list) {
1999a1f6d91cSDavid Greenman 		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2000a1f6d91cSDavid Greenman 			continue;
2001553629ebSJake Burkholder 		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
200260517fd1SJohn Baldwin 			/* sx_sunlock(&allproc_lock); */
2003a1f6d91cSDavid Greenman 			return 1;
2004a1f6d91cSDavid Greenman 		}
2005553629ebSJake Burkholder 	}
200660517fd1SJohn Baldwin 	/* sx_sunlock(&allproc_lock); */
2007a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kernel_map, object, 0))
2008a1f6d91cSDavid Greenman 		return 1;
2009a1f6d91cSDavid Greenman 	if (_vm_object_in_map(kmem_map, object, 0))
2010a1f6d91cSDavid Greenman 		return 1;
2011a1f6d91cSDavid Greenman 	if (_vm_object_in_map(pager_map, object, 0))
2012a1f6d91cSDavid Greenman 		return 1;
2013a1f6d91cSDavid Greenman 	if (_vm_object_in_map(buffer_map, object, 0))
2014a1f6d91cSDavid Greenman 		return 1;
2015a1f6d91cSDavid Greenman 	return 0;
2016a1f6d91cSDavid Greenman }
2017a1f6d91cSDavid Greenman 
2018c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
2019f708ef1bSPoul-Henning Kamp {
2020a1f6d91cSDavid Greenman 	vm_object_t object;
2021a1f6d91cSDavid Greenman 
2022a1f6d91cSDavid Greenman 	/*
2023a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
2024a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
2025a1f6d91cSDavid Greenman 	 */
2026cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
202724a1cce3SDavid Greenman 		if (object->handle == NULL &&
202824a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2029a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
20303efc015bSPeter Wemm 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
20313efc015bSPeter Wemm 					(long)object->size);
2032a1f6d91cSDavid Greenman 			}
2033a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
2034fc62ef1fSBruce Evans 				db_printf(
2035fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
2036fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2037fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
2038fc62ef1fSBruce Evans 				    (u_long)object->size,
2039fc62ef1fSBruce Evans 				    (void *)object->backing_object);
2040a1f6d91cSDavid Greenman 			}
2041a1f6d91cSDavid Greenman 		}
2042a1f6d91cSDavid Greenman 	}
2043a1f6d91cSDavid Greenman }
2044a1f6d91cSDavid Greenman 
204526f9a767SRodney W. Grimes /*
2046df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
2047df8bae1dSRodney W. Grimes  */
2048c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
2049df8bae1dSRodney W. Grimes {
2050c7c34a24SBruce Evans 	/* XXX convert args. */
2051c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
2052c7c34a24SBruce Evans 	boolean_t full = have_addr;
2053c7c34a24SBruce Evans 
2054d031cff1SMatthew Dillon 	vm_page_t p;
2055df8bae1dSRodney W. Grimes 
2056c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
2057c7c34a24SBruce Evans #define	count	was_count
2058c7c34a24SBruce Evans 
2059d031cff1SMatthew Dillon 	int count;
2060df8bae1dSRodney W. Grimes 
2061df8bae1dSRodney W. Grimes 	if (object == NULL)
2062df8bae1dSRodney W. Grimes 		return;
2063df8bae1dSRodney W. Grimes 
2064eb95adefSBruce Evans 	db_iprintf(
2065e47cd172SMaxime Henrion 	    "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x\n",
2066e47cd172SMaxime Henrion 	    object, (int)object->type, (uintmax_t)object->size,
2067eb95adefSBruce Evans 	    object->resident_page_count, object->ref_count, object->flags);
2068e47cd172SMaxime Henrion 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
20691c7c3c6aSMatthew Dillon 	    object->shadow_count,
2070eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
2071e47cd172SMaxime Henrion 	    object->backing_object, (uintmax_t)object->backing_object_offset);
2072df8bae1dSRodney W. Grimes 
2073df8bae1dSRodney W. Grimes 	if (!full)
2074df8bae1dSRodney W. Grimes 		return;
2075df8bae1dSRodney W. Grimes 
2076c7c34a24SBruce Evans 	db_indent += 2;
2077df8bae1dSRodney W. Grimes 	count = 0;
2078fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
2079df8bae1dSRodney W. Grimes 		if (count == 0)
2080c7c34a24SBruce Evans 			db_iprintf("memory:=");
2081df8bae1dSRodney W. Grimes 		else if (count == 6) {
2082c7c34a24SBruce Evans 			db_printf("\n");
2083c7c34a24SBruce Evans 			db_iprintf(" ...");
2084df8bae1dSRodney W. Grimes 			count = 0;
2085df8bae1dSRodney W. Grimes 		} else
2086c7c34a24SBruce Evans 			db_printf(",");
2087df8bae1dSRodney W. Grimes 		count++;
2088df8bae1dSRodney W. Grimes 
2089e47cd172SMaxime Henrion 		db_printf("(off=0x%jx,page=0x%jx)",
2090e47cd172SMaxime Henrion 		    (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
2091df8bae1dSRodney W. Grimes 	}
2092df8bae1dSRodney W. Grimes 	if (count != 0)
2093c7c34a24SBruce Evans 		db_printf("\n");
2094c7c34a24SBruce Evans 	db_indent -= 2;
2095df8bae1dSRodney W. Grimes }
20965070c7f8SJohn Dyson 
2097c7c34a24SBruce Evans /* XXX. */
2098c7c34a24SBruce Evans #undef count
2099c7c34a24SBruce Evans 
2100c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
21015070c7f8SJohn Dyson void
21021b40f8c0SMatthew Dillon vm_object_print(
21031b40f8c0SMatthew Dillon         /* db_expr_t */ long addr,
21041b40f8c0SMatthew Dillon 	boolean_t have_addr,
21051b40f8c0SMatthew Dillon 	/* db_expr_t */ long count,
21061b40f8c0SMatthew Dillon 	char *modif)
2107c7c34a24SBruce Evans {
2108c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
2109c7c34a24SBruce Evans }
2110c7c34a24SBruce Evans 
2111c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
21125070c7f8SJohn Dyson {
21135070c7f8SJohn Dyson 	vm_object_t object;
21145070c7f8SJohn Dyson 	int nl = 0;
21155070c7f8SJohn Dyson 	int c;
2116cc64b484SAlfred Perlstein 
2117cc64b484SAlfred Perlstein 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
21185070c7f8SJohn Dyson 		vm_pindex_t idx, fidx;
21195070c7f8SJohn Dyson 		vm_pindex_t osize;
2120227f9a1cSJake Burkholder 		vm_paddr_t pa = -1, padiff;
21215070c7f8SJohn Dyson 		int rcount;
21225070c7f8SJohn Dyson 		vm_page_t m;
21235070c7f8SJohn Dyson 
2124fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
21255070c7f8SJohn Dyson 		if (nl > 18) {
21265070c7f8SJohn Dyson 			c = cngetc();
21275070c7f8SJohn Dyson 			if (c != ' ')
21285070c7f8SJohn Dyson 				return;
21295070c7f8SJohn Dyson 			nl = 0;
21305070c7f8SJohn Dyson 		}
21315070c7f8SJohn Dyson 		nl++;
21325070c7f8SJohn Dyson 		rcount = 0;
21335070c7f8SJohn Dyson 		fidx = 0;
21345070c7f8SJohn Dyson 		osize = object->size;
21355070c7f8SJohn Dyson 		if (osize > 128)
21365070c7f8SJohn Dyson 			osize = 128;
21375070c7f8SJohn Dyson 		for (idx = 0; idx < osize; idx++) {
21385070c7f8SJohn Dyson 			m = vm_page_lookup(object, idx);
21395070c7f8SJohn Dyson 			if (m == NULL) {
21405070c7f8SJohn Dyson 				if (rcount) {
21413efc015bSPeter Wemm 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
21423efc015bSPeter Wemm 						(long)fidx, rcount, (long)pa);
21435070c7f8SJohn Dyson 					if (nl > 18) {
21445070c7f8SJohn Dyson 						c = cngetc();
21455070c7f8SJohn Dyson 						if (c != ' ')
21465070c7f8SJohn Dyson 							return;
21475070c7f8SJohn Dyson 						nl = 0;
21485070c7f8SJohn Dyson 					}
21495070c7f8SJohn Dyson 					nl++;
21505070c7f8SJohn Dyson 					rcount = 0;
21515070c7f8SJohn Dyson 				}
21525070c7f8SJohn Dyson 				continue;
21535070c7f8SJohn Dyson 			}
21545070c7f8SJohn Dyson 
21555070c7f8SJohn Dyson 
21565070c7f8SJohn Dyson 			if (rcount &&
21575070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
21585070c7f8SJohn Dyson 				++rcount;
21595070c7f8SJohn Dyson 				continue;
21605070c7f8SJohn Dyson 			}
21615070c7f8SJohn Dyson 			if (rcount) {
21625070c7f8SJohn Dyson 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
21635070c7f8SJohn Dyson 				padiff >>= PAGE_SHIFT;
2164ef39c05bSAlexander Leidinger 				padiff &= PQ_COLORMASK;
21655070c7f8SJohn Dyson 				if (padiff == 0) {
21665070c7f8SJohn Dyson 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
21675070c7f8SJohn Dyson 					++rcount;
21685070c7f8SJohn Dyson 					continue;
21695070c7f8SJohn Dyson 				}
21703efc015bSPeter Wemm 				db_printf(" index(%ld)run(%d)pa(0x%lx)",
21713efc015bSPeter Wemm 					(long)fidx, rcount, (long)pa);
21723efc015bSPeter Wemm 				db_printf("pd(%ld)\n", (long)padiff);
21735070c7f8SJohn Dyson 				if (nl > 18) {
21745070c7f8SJohn Dyson 					c = cngetc();
21755070c7f8SJohn Dyson 					if (c != ' ')
21765070c7f8SJohn Dyson 						return;
21775070c7f8SJohn Dyson 					nl = 0;
21785070c7f8SJohn Dyson 				}
21795070c7f8SJohn Dyson 				nl++;
21805070c7f8SJohn Dyson 			}
21815070c7f8SJohn Dyson 			fidx = idx;
21825070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
21835070c7f8SJohn Dyson 			rcount = 1;
21845070c7f8SJohn Dyson 		}
21855070c7f8SJohn Dyson 		if (rcount) {
21863efc015bSPeter Wemm 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
21873efc015bSPeter Wemm 				(long)fidx, rcount, (long)pa);
21885070c7f8SJohn Dyson 			if (nl > 18) {
21895070c7f8SJohn Dyson 				c = cngetc();
21905070c7f8SJohn Dyson 				if (c != ' ')
21915070c7f8SJohn Dyson 					return;
21925070c7f8SJohn Dyson 				nl = 0;
21935070c7f8SJohn Dyson 			}
21945070c7f8SJohn Dyson 			nl++;
21955070c7f8SJohn Dyson 		}
21965070c7f8SJohn Dyson 	}
21975070c7f8SJohn Dyson }
2198c3cb3e12SDavid Greenman #endif /* DDB */
2199