vm_object.c revision 98849
1178825Sdfr/*
2178825Sdfr * Copyright (c) 1991, 1993
3178825Sdfr *	The Regents of the University of California.  All rights reserved.
4178825Sdfr *
5178825Sdfr * This code is derived from software contributed to Berkeley by
6178825Sdfr * The Mach Operating System project at Carnegie-Mellon University.
7178825Sdfr *
8178825Sdfr * Redistribution and use in source and binary forms, with or without
9178825Sdfr * modification, are permitted provided that the following conditions
10178825Sdfr * are met:
11178825Sdfr * 1. Redistributions of source code must retain the above copyright
12178825Sdfr *    notice, this list of conditions and the following disclaimer.
13178825Sdfr * 2. Redistributions in binary form must reproduce the above copyright
14178825Sdfr *    notice, this list of conditions and the following disclaimer in the
15178825Sdfr *    documentation and/or other materials provided with the distribution.
16178825Sdfr * 3. All advertising materials mentioning features or use of this software
17178825Sdfr *    must display the following acknowledgement:
18178825Sdfr *	This product includes software developed by the University of
19178825Sdfr *	California, Berkeley and its contributors.
20178825Sdfr * 4. Neither the name of the University nor the names of its contributors
21178825Sdfr *    may be used to endorse or promote products derived from this software
22178825Sdfr *    without specific prior written permission.
23178825Sdfr *
24178825Sdfr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25178825Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26178825Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27178825Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28178825Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29178825Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30178825Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31178825Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32178825Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33178825Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34178825Sdfr * SUCH DAMAGE.
35178825Sdfr *
36178825Sdfr *	from: @(#)vm_object.c	8.5 (Berkeley) 3/22/94
37178825Sdfr *
38178825Sdfr *
39178825Sdfr * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40178825Sdfr * All rights reserved.
41178825Sdfr *
42178825Sdfr * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43178825Sdfr *
44178825Sdfr * Permission to use, copy, modify and distribute this software and
45178825Sdfr * its documentation is hereby granted, provided that both the copyright
46178825Sdfr * notice and this permission notice appear in all copies of the
47178825Sdfr * software, derivative works or modified versions, and any portions
48178825Sdfr * thereof, and that both notices appear in supporting documentation.
49178825Sdfr *
50178825Sdfr * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51178825Sdfr * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52178825Sdfr * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53178825Sdfr *
54178825Sdfr * Carnegie Mellon requests users of this software to return to
55178825Sdfr *
56178825Sdfr *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57178825Sdfr *  School of Computer Science
58178825Sdfr *  Carnegie Mellon University
59178825Sdfr *  Pittsburgh PA 15213-3890
60178825Sdfr *
61178825Sdfr * any improvements or extensions that they make and grant Carnegie the
62178825Sdfr * rights to redistribute these changes.
63178825Sdfr *
64178825Sdfr * $FreeBSD: head/sys/vm/vm_object.c 98849 2002-06-26 03:37:47Z ken $
65178825Sdfr */
66178825Sdfr
67178825Sdfr/*
68178825Sdfr *	Virtual memory object module.
69178825Sdfr */
70178825Sdfr
71178825Sdfr#include <sys/param.h>
72178825Sdfr#include <sys/systm.h>
73178825Sdfr#include <sys/lock.h>
74178825Sdfr#include <sys/mman.h>
75178825Sdfr#include <sys/mount.h>
76178825Sdfr#include <sys/kernel.h>
77178825Sdfr#include <sys/sysctl.h>
78178825Sdfr#include <sys/mutex.h>
79178825Sdfr#include <sys/proc.h>		/* for curproc, pageproc */
80178825Sdfr#include <sys/socket.h>
81178825Sdfr#include <sys/vnode.h>
82178825Sdfr#include <sys/vmmeter.h>
83178825Sdfr#include <sys/sx.h>
84178825Sdfr
85178825Sdfr#include <vm/vm.h>
86178825Sdfr#include <vm/vm_param.h>
87178825Sdfr#include <vm/pmap.h>
88178825Sdfr#include <vm/vm_map.h>
89178825Sdfr#include <vm/vm_object.h>
90178825Sdfr#include <vm/vm_page.h>
91178825Sdfr#include <vm/vm_pageout.h>
92178825Sdfr#include <vm/vm_pager.h>
93178825Sdfr#include <vm/swap_pager.h>
94178825Sdfr#include <vm/vm_kern.h>
95178825Sdfr#include <vm/vm_extern.h>
96178825Sdfr#include <vm/uma.h>
97178825Sdfr
98178825Sdfr#define EASY_SCAN_FACTOR       8
99178825Sdfr
100178825Sdfr#define MSYNC_FLUSH_HARDSEQ	0x01
101178825Sdfr#define MSYNC_FLUSH_SOFTSEQ	0x02
102178825Sdfr
103178825Sdfr/*
104178825Sdfr * msync / VM object flushing optimizations
105178825Sdfr */
106178825Sdfrstatic int msync_flush_flags = MSYNC_FLUSH_HARDSEQ | MSYNC_FLUSH_SOFTSEQ;
107178825SdfrSYSCTL_INT(_vm, OID_AUTO, msync_flush_flags,
108178825Sdfr        CTLFLAG_RW, &msync_flush_flags, 0, "");
109178825Sdfr
110178825Sdfrstatic void	vm_object_qcollapse(vm_object_t object);
111178825Sdfrstatic int	vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags);
112178825Sdfr
113178825Sdfr/*
114178825Sdfr *	Virtual memory objects maintain the actual data
115178825Sdfr *	associated with allocated virtual memory.  A given
116178825Sdfr *	page of memory exists within exactly one object.
117178825Sdfr *
118178825Sdfr *	An object is only deallocated when all "references"
119178825Sdfr *	are given up.  Only one "reference" to a given
120178825Sdfr *	region of an object should be writeable.
121178825Sdfr *
122178825Sdfr *	Associated with each object is a list of all resident
123178825Sdfr *	memory pages belonging to that object; this list is
124178825Sdfr *	maintained by the "vm_page" module, and locked by the object's
125178825Sdfr *	lock.
126178825Sdfr *
127178825Sdfr *	Each object also records a "pager" routine which is
128178825Sdfr *	used to retrieve (and store) pages to the proper backing
129178825Sdfr *	storage.  In addition, objects may be backed by other
130178825Sdfr *	objects from which they were virtual-copied.
131178825Sdfr *
132178825Sdfr *	The only items within the object structure which are
133178825Sdfr *	modified after time of creation are:
134178825Sdfr *		reference count		locked by object's lock
135178825Sdfr *		pager routine		locked by object's lock
136178825Sdfr *
137178825Sdfr */
138178825Sdfr
139178825Sdfrstruct object_q vm_object_list;
140178825Sdfrstruct mtx vm_object_list_mtx;	/* lock for object list and count */
141178825Sdfrvm_object_t kernel_object;
142178825Sdfrvm_object_t kmem_object;
143178825Sdfrstatic struct vm_object kernel_object_store;
144178825Sdfrstatic struct vm_object kmem_object_store;
145178825Sdfrextern int vm_pageout_page_count;
146178825Sdfr
147178825Sdfrstatic long object_collapses;
148178825Sdfrstatic long object_bypasses;
149178825Sdfrstatic int next_index;
150178825Sdfrstatic uma_zone_t obj_zone;
151178825Sdfr#define VM_OBJECTS_INIT 256
152178825Sdfr
153178825Sdfrstatic void vm_object_zinit(void *mem, int size);
154178825Sdfr
155178825Sdfr#ifdef INVARIANTS
156178825Sdfrstatic void vm_object_zdtor(void *mem, int size, void *arg);
157178825Sdfr
158178825Sdfrstatic void
159178825Sdfrvm_object_zdtor(void *mem, int size, void *arg)
160178825Sdfr{
161178825Sdfr	vm_object_t object;
162178825Sdfr
163178825Sdfr	object = (vm_object_t)mem;
164178825Sdfr	KASSERT(object->paging_in_progress == 0,
165178825Sdfr	    ("object %p paging_in_progress = %d",
166178825Sdfr	    object, object->paging_in_progress));
167178825Sdfr	KASSERT(object->resident_page_count == 0,
168178825Sdfr	    ("object %p resident_page_count = %d",
169178825Sdfr	    object, object->resident_page_count));
170178825Sdfr	KASSERT(object->shadow_count == 0,
171178825Sdfr	    ("object %p shadow_count = %d",
172178825Sdfr	    object, object->shadow_count));
173178825Sdfr}
174178825Sdfr#endif
175178825Sdfr
176178825Sdfrstatic void
177178825Sdfrvm_object_zinit(void *mem, int size)
178178825Sdfr{
179178825Sdfr	vm_object_t object;
180178825Sdfr
181178825Sdfr	object = (vm_object_t)mem;
182178825Sdfr
183178825Sdfr	/* These are true for any object that has been freed */
184178825Sdfr	object->paging_in_progress = 0;
185178825Sdfr	object->resident_page_count = 0;
186178825Sdfr	object->shadow_count = 0;
187178825Sdfr}
188178825Sdfr
189178825Sdfrvoid
190178825Sdfr_vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
191178825Sdfr{
192178825Sdfr	static int object_hash_rand;
193178825Sdfr	int exp, incr;
194178825Sdfr
195178825Sdfr	TAILQ_INIT(&object->memq);
196178825Sdfr	TAILQ_INIT(&object->shadow_head);
197178825Sdfr
198178825Sdfr	object->type = type;
199178825Sdfr	object->size = size;
200178825Sdfr	object->ref_count = 1;
201178825Sdfr	object->flags = 0;
202178825Sdfr	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
203178825Sdfr		vm_object_set_flag(object, OBJ_ONEMAPPING);
204178825Sdfr	if (size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
205178825Sdfr		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
206178825Sdfr	else
207178825Sdfr		incr = size;
208178825Sdfr	do
209178825Sdfr		object->pg_color = next_index;
210178825Sdfr	while (!atomic_cmpset_int(&next_index, object->pg_color,
211178825Sdfr				  (object->pg_color + incr) & PQ_L2_MASK));
212178825Sdfr	object->handle = NULL;
213178825Sdfr	object->backing_object = NULL;
214178825Sdfr	object->backing_object_offset = (vm_ooffset_t) 0;
215178825Sdfr	/*
216178825Sdfr	 * Try to generate a number that will spread objects out in the
217178825Sdfr	 * hash table.  We 'wipe' new objects across the hash in 128 page
218178825Sdfr	 * increments plus 1 more to offset it a little more by the time
219178825Sdfr	 * it wraps around.
220178825Sdfr	 */
221178825Sdfr	do {
222178825Sdfr		exp = object_hash_rand;
223178825Sdfr		object->hash_rand = exp - 129;
224178825Sdfr	} while (!atomic_cmpset_int(&object_hash_rand, exp, object->hash_rand));
225178825Sdfr
226178825Sdfr	object->generation++;		/* atomicity needed? XXX */
227178825Sdfr
228178825Sdfr	mtx_lock(&vm_object_list_mtx);
229178825Sdfr	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
230178825Sdfr	mtx_unlock(&vm_object_list_mtx);
231178825Sdfr}
232178825Sdfr
233178825Sdfr/*
234178825Sdfr *	vm_object_init:
235178825Sdfr *
236178825Sdfr *	Initialize the VM objects module.
237178825Sdfr */
238178825Sdfrvoid
239178825Sdfrvm_object_init(void)
240178825Sdfr{
241178825Sdfr	TAILQ_INIT(&vm_object_list);
242178825Sdfr	mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
243178825Sdfr
244178825Sdfr	kernel_object = &kernel_object_store;
245178825Sdfr	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
246178825Sdfr	    kernel_object);
247178825Sdfr
248178825Sdfr	kmem_object = &kmem_object_store;
249178825Sdfr	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
250178825Sdfr	    kmem_object);
251178825Sdfr	obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
252178825Sdfr#ifdef INVARIANTS
253178825Sdfr	    vm_object_zdtor,
254178825Sdfr#else
255178825Sdfr	    NULL,
256178825Sdfr#endif
257178825Sdfr	    vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
258178825Sdfr	uma_prealloc(obj_zone, VM_OBJECTS_INIT);
259178825Sdfr}
260178825Sdfr
261178825Sdfrvoid
262178825Sdfrvm_object_init2(void)
263178825Sdfr{
264178825Sdfr}
265178825Sdfr
266178825Sdfrvoid
267178825Sdfrvm_object_set_flag(vm_object_t object, u_short bits)
268178825Sdfr{
269178825Sdfr	object->flags |= bits;
270178825Sdfr}
271178825Sdfr
272178825Sdfrvoid
273178825Sdfrvm_object_clear_flag(vm_object_t object, u_short bits)
274178825Sdfr{
275178825Sdfr	GIANT_REQUIRED;
276178825Sdfr	object->flags &= ~bits;
277178825Sdfr}
278178825Sdfr
279178825Sdfrvoid
280178825Sdfrvm_object_pip_add(vm_object_t object, short i)
281178825Sdfr{
282178825Sdfr	GIANT_REQUIRED;
283178825Sdfr	object->paging_in_progress += i;
284178825Sdfr}
285178825Sdfr
286178825Sdfrvoid
287178825Sdfrvm_object_pip_subtract(vm_object_t object, short i)
288178825Sdfr{
289178825Sdfr	GIANT_REQUIRED;
290178825Sdfr	object->paging_in_progress -= i;
291178825Sdfr}
292178825Sdfr
293178825Sdfrvoid
294178825Sdfrvm_object_pip_wakeup(vm_object_t object)
295178825Sdfr{
296178825Sdfr	GIANT_REQUIRED;
297178825Sdfr	object->paging_in_progress--;
298178825Sdfr	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
299178825Sdfr		vm_object_clear_flag(object, OBJ_PIPWNT);
300178825Sdfr		wakeup(object);
301178825Sdfr	}
302178825Sdfr}
303178825Sdfr
304178825Sdfrvoid
305178825Sdfrvm_object_pip_wakeupn(vm_object_t object, short i)
306178825Sdfr{
307178825Sdfr	GIANT_REQUIRED;
308178825Sdfr	if (i)
309178825Sdfr		object->paging_in_progress -= i;
310178825Sdfr	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
311178825Sdfr		vm_object_clear_flag(object, OBJ_PIPWNT);
312178825Sdfr		wakeup(object);
313178825Sdfr	}
314178825Sdfr}
315178825Sdfr
316178825Sdfrvoid
317178825Sdfrvm_object_pip_sleep(vm_object_t object, char *waitid)
318178825Sdfr{
319178825Sdfr	GIANT_REQUIRED;
320178825Sdfr	if (object->paging_in_progress) {
321178825Sdfr		int s = splvm();
322178825Sdfr		if (object->paging_in_progress) {
323178825Sdfr			vm_object_set_flag(object, OBJ_PIPWNT);
324178825Sdfr			tsleep(object, PVM, waitid, 0);
325178825Sdfr		}
326178825Sdfr		splx(s);
327178825Sdfr	}
328178825Sdfr}
329178825Sdfr
330178825Sdfrvoid
331vm_object_pip_wait(vm_object_t object, char *waitid)
332{
333	GIANT_REQUIRED;
334	while (object->paging_in_progress)
335		vm_object_pip_sleep(object, waitid);
336}
337
338/*
339 *	vm_object_allocate_wait
340 *
341 *	Return a new object with the given size, and give the user the
342 *	option of waiting for it to complete or failing if the needed
343 *	memory isn't available.
344 */
345vm_object_t
346vm_object_allocate_wait(objtype_t type, vm_pindex_t size, int flags)
347{
348	vm_object_t result;
349
350	result = (vm_object_t) uma_zalloc(obj_zone, flags);
351
352	if (result != NULL)
353		_vm_object_allocate(type, size, result);
354
355	return (result);
356}
357
358/*
359 *	vm_object_allocate:
360 *
361 *	Returns a new object with the given size.
362 */
363vm_object_t
364vm_object_allocate(objtype_t type, vm_pindex_t size)
365{
366	return(vm_object_allocate_wait(type, size, M_WAITOK));
367}
368
369
370/*
371 *	vm_object_reference:
372 *
373 *	Gets another reference to the given object.
374 */
375void
376vm_object_reference(vm_object_t object)
377{
378	if (object == NULL)
379		return;
380
381	mtx_lock(&Giant);
382#if 0
383	/* object can be re-referenced during final cleaning */
384	KASSERT(!(object->flags & OBJ_DEAD),
385	    ("vm_object_reference: attempting to reference dead obj"));
386#endif
387
388	object->ref_count++;
389	if (object->type == OBJT_VNODE) {
390		while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curthread)) {
391			printf("vm_object_reference: delay in getting object\n");
392		}
393	}
394	mtx_unlock(&Giant);
395}
396
397/*
398 * handle deallocating a object of type OBJT_VNODE
399 */
400void
401vm_object_vndeallocate(vm_object_t object)
402{
403	struct vnode *vp = (struct vnode *) object->handle;
404
405	GIANT_REQUIRED;
406	KASSERT(object->type == OBJT_VNODE,
407	    ("vm_object_vndeallocate: not a vnode object"));
408	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
409#ifdef INVARIANTS
410	if (object->ref_count == 0) {
411		vprint("vm_object_vndeallocate", vp);
412		panic("vm_object_vndeallocate: bad object reference count");
413	}
414#endif
415
416	object->ref_count--;
417	if (object->ref_count == 0) {
418		vp->v_flag &= ~VTEXT;
419#ifdef ENABLE_VFS_IOOPT
420		vm_object_clear_flag(object, OBJ_OPT);
421#endif
422	}
423	/*
424	 * vrele may need a vop lock
425	 */
426	vrele(vp);
427}
428
429/*
430 *	vm_object_deallocate:
431 *
432 *	Release a reference to the specified object,
433 *	gained either through a vm_object_allocate
434 *	or a vm_object_reference call.  When all references
435 *	are gone, storage associated with this object
436 *	may be relinquished.
437 *
438 *	No object may be locked.
439 */
440void
441vm_object_deallocate(vm_object_t object)
442{
443	vm_object_t temp;
444
445	mtx_lock(&Giant);
446	while (object != NULL) {
447
448		if (object->type == OBJT_VNODE) {
449			vm_object_vndeallocate(object);
450			mtx_unlock(&Giant);
451			return;
452		}
453
454		KASSERT(object->ref_count != 0,
455			("vm_object_deallocate: object deallocated too many times: %d", object->type));
456
457		/*
458		 * If the reference count goes to 0 we start calling
459		 * vm_object_terminate() on the object chain.
460		 * A ref count of 1 may be a special case depending on the
461		 * shadow count being 0 or 1.
462		 */
463		object->ref_count--;
464		if (object->ref_count > 1) {
465			mtx_unlock(&Giant);
466			return;
467		} else if (object->ref_count == 1) {
468			if (object->shadow_count == 0) {
469				vm_object_set_flag(object, OBJ_ONEMAPPING);
470			} else if ((object->shadow_count == 1) &&
471			    (object->handle == NULL) &&
472			    (object->type == OBJT_DEFAULT ||
473			     object->type == OBJT_SWAP)) {
474				vm_object_t robject;
475
476				robject = TAILQ_FIRST(&object->shadow_head);
477				KASSERT(robject != NULL,
478				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
479					 object->ref_count,
480					 object->shadow_count));
481				if ((robject->handle == NULL) &&
482				    (robject->type == OBJT_DEFAULT ||
483				     robject->type == OBJT_SWAP)) {
484
485					robject->ref_count++;
486
487					while (
488						robject->paging_in_progress ||
489						object->paging_in_progress
490					) {
491						vm_object_pip_sleep(robject, "objde1");
492						vm_object_pip_sleep(object, "objde2");
493					}
494
495					if (robject->ref_count == 1) {
496						robject->ref_count--;
497						object = robject;
498						goto doterm;
499					}
500
501					object = robject;
502					vm_object_collapse(object);
503					continue;
504				}
505			}
506			mtx_unlock(&Giant);
507			return;
508		}
509doterm:
510		temp = object->backing_object;
511		if (temp) {
512			TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
513			temp->shadow_count--;
514#ifdef ENABLE_VFS_IOOPT
515			if (temp->ref_count == 0)
516				vm_object_clear_flag(temp, OBJ_OPT);
517#endif
518			temp->generation++;
519			object->backing_object = NULL;
520		}
521		/*
522		 * Don't double-terminate, we could be in a termination
523		 * recursion due to the terminate having to sync data
524		 * to disk.
525		 */
526		if ((object->flags & OBJ_DEAD) == 0)
527			vm_object_terminate(object);
528		object = temp;
529	}
530	mtx_unlock(&Giant);
531}
532
533/*
534 *	vm_object_terminate actually destroys the specified object, freeing
535 *	up all previously used resources.
536 *
537 *	The object must be locked.
538 *	This routine may block.
539 */
540void
541vm_object_terminate(vm_object_t object)
542{
543	vm_page_t p;
544	int s;
545
546	GIANT_REQUIRED;
547
548	/*
549	 * Make sure no one uses us.
550	 */
551	vm_object_set_flag(object, OBJ_DEAD);
552
553	/*
554	 * wait for the pageout daemon to be done with the object
555	 */
556	vm_object_pip_wait(object, "objtrm");
557
558	KASSERT(!object->paging_in_progress,
559		("vm_object_terminate: pageout in progress"));
560
561	/*
562	 * Clean and free the pages, as appropriate. All references to the
563	 * object are gone, so we don't need to lock it.
564	 */
565	if (object->type == OBJT_VNODE) {
566		struct vnode *vp;
567
568#ifdef ENABLE_VFS_IOOPT
569		/*
570		 * Freeze optimized copies.
571		 */
572		vm_freeze_copyopts(object, 0, object->size);
573#endif
574		/*
575		 * Clean pages and flush buffers.
576		 */
577		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
578
579		vp = (struct vnode *) object->handle;
580		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
581	}
582
583	KASSERT(object->ref_count == 0,
584		("vm_object_terminate: object with references, ref_count=%d",
585		object->ref_count));
586
587	/*
588	 * Now free any remaining pages. For internal objects, this also
589	 * removes them from paging queues. Don't free wired pages, just
590	 * remove them from the object.
591	 */
592	s = splvm();
593	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
594		KASSERT(!p->busy && (p->flags & PG_BUSY) == 0,
595			("vm_object_terminate: freeing busy page %p "
596			"p->busy = %d, p->flags %x\n", p, p->busy, p->flags));
597		if (p->wire_count == 0) {
598			vm_page_busy(p);
599			vm_page_free(p);
600			cnt.v_pfree++;
601		} else {
602			vm_page_busy(p);
603			vm_page_remove(p);
604		}
605	}
606	splx(s);
607
608	/*
609	 * Let the pager know object is dead.
610	 */
611	vm_pager_deallocate(object);
612
613	/*
614	 * Remove the object from the global object list.
615	 */
616	mtx_lock(&vm_object_list_mtx);
617	TAILQ_REMOVE(&vm_object_list, object, object_list);
618	mtx_unlock(&vm_object_list_mtx);
619
620	wakeup(object);
621
622	/*
623	 * Free the space for the object.
624	 */
625	uma_zfree(obj_zone, object);
626}
627
628/*
629 *	vm_object_page_clean
630 *
631 *	Clean all dirty pages in the specified range of object.  Leaves page
632 * 	on whatever queue it is currently on.   If NOSYNC is set then do not
633 *	write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
634 *	leaving the object dirty.
635 *
636 *	Odd semantics: if start == end, we clean everything.
637 *
638 *	The object must be locked.
639 */
640void
641vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags)
642{
643	vm_page_t p, np;
644	vm_pindex_t tstart, tend;
645	vm_pindex_t pi;
646	struct vnode *vp;
647	int clearobjflags;
648	int pagerflags;
649	int curgeneration;
650
651	GIANT_REQUIRED;
652
653	if (object->type != OBJT_VNODE ||
654		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
655		return;
656
657	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0;
658	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
659
660	vp = object->handle;
661
662	vm_object_set_flag(object, OBJ_CLEANING);
663
664	tstart = start;
665	if (end == 0) {
666		tend = object->size;
667	} else {
668		tend = end;
669	}
670
671	/*
672	 * If the caller is smart and only msync()s a range he knows is
673	 * dirty, we may be able to avoid an object scan.  This results in
674	 * a phenominal improvement in performance.  We cannot do this
675	 * as a matter of course because the object may be huge - e.g.
676	 * the size might be in the gigabytes or terrabytes.
677	 */
678	if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) {
679		vm_offset_t tscan;
680		int scanlimit;
681		int scanreset;
682
683		scanreset = object->resident_page_count / EASY_SCAN_FACTOR;
684		if (scanreset < 16)
685			scanreset = 16;
686
687		scanlimit = scanreset;
688		tscan = tstart;
689		while (tscan < tend) {
690			curgeneration = object->generation;
691			p = vm_page_lookup(object, tscan);
692			if (p == NULL || p->valid == 0 ||
693			    (p->queue - p->pc) == PQ_CACHE) {
694				if (--scanlimit == 0)
695					break;
696				++tscan;
697				continue;
698			}
699			vm_page_test_dirty(p);
700			if ((p->dirty & p->valid) == 0) {
701				if (--scanlimit == 0)
702					break;
703				++tscan;
704				continue;
705			}
706			/*
707			 * If we have been asked to skip nosync pages and
708			 * this is a nosync page, we can't continue.
709			 */
710			if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
711				if (--scanlimit == 0)
712					break;
713				++tscan;
714				continue;
715			}
716			scanlimit = scanreset;
717
718			/*
719			 * This returns 0 if it was unable to busy the first
720			 * page (i.e. had to sleep).
721			 */
722			tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags);
723		}
724
725		/*
726		 * If everything was dirty and we flushed it successfully,
727		 * and the requested range is not the entire object, we
728		 * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can
729		 * return immediately.
730		 */
731		if (tscan >= tend && (tstart || tend < object->size)) {
732			vm_object_clear_flag(object, OBJ_CLEANING);
733			return;
734		}
735	}
736
737	/*
738	 * Generally set CLEANCHK interlock and make the page read-only so
739	 * we can then clear the object flags.
740	 *
741	 * However, if this is a nosync mmap then the object is likely to
742	 * stay dirty so do not mess with the page and do not clear the
743	 * object flags.
744	 */
745	clearobjflags = 1;
746
747	TAILQ_FOREACH(p, &object->memq, listq) {
748		vm_page_flag_set(p, PG_CLEANCHK);
749		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC))
750			clearobjflags = 0;
751		else
752			vm_page_protect(p, VM_PROT_READ);
753	}
754
755	if (clearobjflags && (tstart == 0) && (tend == object->size)) {
756		struct vnode *vp;
757
758		vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
759		if (object->type == OBJT_VNODE &&
760		    (vp = (struct vnode *)object->handle) != NULL) {
761			if (vp->v_flag & VOBJDIRTY) {
762				mtx_lock(&vp->v_interlock);
763				vp->v_flag &= ~VOBJDIRTY;
764				mtx_unlock(&vp->v_interlock);
765			}
766		}
767	}
768
769rescan:
770	curgeneration = object->generation;
771
772	for (p = TAILQ_FIRST(&object->memq); p; p = np) {
773		int n;
774
775		np = TAILQ_NEXT(p, listq);
776
777again:
778		pi = p->pindex;
779		if (((p->flags & PG_CLEANCHK) == 0) ||
780			(pi < tstart) || (pi >= tend) ||
781			(p->valid == 0) ||
782			((p->queue - p->pc) == PQ_CACHE)) {
783			vm_page_flag_clear(p, PG_CLEANCHK);
784			continue;
785		}
786
787		vm_page_test_dirty(p);
788		if ((p->dirty & p->valid) == 0) {
789			vm_page_flag_clear(p, PG_CLEANCHK);
790			continue;
791		}
792
793		/*
794		 * If we have been asked to skip nosync pages and this is a
795		 * nosync page, skip it.  Note that the object flags were
796		 * not cleared in this case so we do not have to set them.
797		 */
798		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
799			vm_page_flag_clear(p, PG_CLEANCHK);
800			continue;
801		}
802
803		n = vm_object_page_collect_flush(object, p,
804			curgeneration, pagerflags);
805		if (n == 0)
806			goto rescan;
807
808		if (object->generation != curgeneration)
809			goto rescan;
810
811		/*
812		 * Try to optimize the next page.  If we can't we pick up
813		 * our (random) scan where we left off.
814		 */
815		if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) {
816			if ((p = vm_page_lookup(object, pi + n)) != NULL)
817				goto again;
818		}
819	}
820
821#if 0
822	VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
823#endif
824
825	vm_object_clear_flag(object, OBJ_CLEANING);
826	return;
827}
828
829static int
830vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags)
831{
832	int runlen;
833	int s;
834	int maxf;
835	int chkb;
836	int maxb;
837	int i;
838	vm_pindex_t pi;
839	vm_page_t maf[vm_pageout_page_count];
840	vm_page_t mab[vm_pageout_page_count];
841	vm_page_t ma[vm_pageout_page_count];
842
843	s = splvm();
844	pi = p->pindex;
845	while (vm_page_sleep_busy(p, TRUE, "vpcwai")) {
846		if (object->generation != curgeneration) {
847			splx(s);
848			return(0);
849		}
850	}
851
852	maxf = 0;
853	for(i = 1; i < vm_pageout_page_count; i++) {
854		vm_page_t tp;
855
856		if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
857			if ((tp->flags & PG_BUSY) ||
858				(tp->flags & PG_CLEANCHK) == 0 ||
859				(tp->busy != 0))
860				break;
861			if((tp->queue - tp->pc) == PQ_CACHE) {
862				vm_page_flag_clear(tp, PG_CLEANCHK);
863				break;
864			}
865			vm_page_test_dirty(tp);
866			if ((tp->dirty & tp->valid) == 0) {
867				vm_page_flag_clear(tp, PG_CLEANCHK);
868				break;
869			}
870			maf[ i - 1 ] = tp;
871			maxf++;
872			continue;
873		}
874		break;
875	}
876
877	maxb = 0;
878	chkb = vm_pageout_page_count -  maxf;
879	if (chkb) {
880		for(i = 1; i < chkb;i++) {
881			vm_page_t tp;
882
883			if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
884				if ((tp->flags & PG_BUSY) ||
885					(tp->flags & PG_CLEANCHK) == 0 ||
886					(tp->busy != 0))
887					break;
888				if ((tp->queue - tp->pc) == PQ_CACHE) {
889					vm_page_flag_clear(tp, PG_CLEANCHK);
890					break;
891				}
892				vm_page_test_dirty(tp);
893				if ((tp->dirty & tp->valid) == 0) {
894					vm_page_flag_clear(tp, PG_CLEANCHK);
895					break;
896				}
897				mab[ i - 1 ] = tp;
898				maxb++;
899				continue;
900			}
901			break;
902		}
903	}
904
905	for(i = 0; i < maxb; i++) {
906		int index = (maxb - i) - 1;
907		ma[index] = mab[i];
908		vm_page_flag_clear(ma[index], PG_CLEANCHK);
909	}
910	vm_page_flag_clear(p, PG_CLEANCHK);
911	ma[maxb] = p;
912	for(i = 0; i < maxf; i++) {
913		int index = (maxb + i) + 1;
914		ma[index] = maf[i];
915		vm_page_flag_clear(ma[index], PG_CLEANCHK);
916	}
917	runlen = maxb + maxf + 1;
918
919	splx(s);
920	vm_pageout_flush(ma, runlen, pagerflags);
921	for (i = 0; i < runlen; i++) {
922		if (ma[i]->valid & ma[i]->dirty) {
923			vm_page_protect(ma[i], VM_PROT_READ);
924			vm_page_flag_set(ma[i], PG_CLEANCHK);
925
926			/*
927			 * maxf will end up being the actual number of pages
928			 * we wrote out contiguously, non-inclusive of the
929			 * first page.  We do not count look-behind pages.
930			 */
931			if (i >= maxb + 1 && (maxf > i - maxb - 1))
932				maxf = i - maxb - 1;
933		}
934	}
935	return(maxf + 1);
936}
937
938#ifdef ENABLE_VFS_IOOPT
939/*
940 * Same as vm_object_pmap_copy, except range checking really
941 * works, and is meant for small sections of an object.
942 *
943 * This code protects resident pages by making them read-only
944 * and is typically called on a fork or split when a page
945 * is converted to copy-on-write.
946 *
947 * NOTE: If the page is already at VM_PROT_NONE, calling
948 * vm_page_protect will have no effect.
949 */
950void
951vm_object_pmap_copy_1(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
952{
953	vm_pindex_t idx;
954	vm_page_t p;
955
956	GIANT_REQUIRED;
957
958	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
959		return;
960
961	for (idx = start; idx < end; idx++) {
962		p = vm_page_lookup(object, idx);
963		if (p == NULL)
964			continue;
965		vm_page_protect(p, VM_PROT_READ);
966	}
967}
968#endif
969
970/*
971 *	vm_object_pmap_remove:
972 *
973 *	Removes all physical pages in the specified
974 *	object range from all physical maps.
975 *
976 *	The object must *not* be locked.
977 */
978void
979vm_object_pmap_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
980{
981	vm_page_t p;
982
983	GIANT_REQUIRED;
984	if (object == NULL)
985		return;
986	TAILQ_FOREACH(p, &object->memq, listq) {
987		if (p->pindex >= start && p->pindex < end)
988			vm_page_protect(p, VM_PROT_NONE);
989	}
990	if ((start == 0) && (object->size == end))
991		vm_object_clear_flag(object, OBJ_WRITEABLE);
992}
993
994/*
995 *	vm_object_madvise:
996 *
997 *	Implements the madvise function at the object/page level.
998 *
999 *	MADV_WILLNEED	(any object)
1000 *
1001 *	    Activate the specified pages if they are resident.
1002 *
1003 *	MADV_DONTNEED	(any object)
1004 *
1005 *	    Deactivate the specified pages if they are resident.
1006 *
1007 *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
1008 *			 OBJ_ONEMAPPING only)
1009 *
1010 *	    Deactivate and clean the specified pages if they are
1011 *	    resident.  This permits the process to reuse the pages
1012 *	    without faulting or the kernel to reclaim the pages
1013 *	    without I/O.
1014 */
1015void
1016vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
1017{
1018	vm_pindex_t end, tpindex;
1019	vm_object_t tobject;
1020	vm_page_t m;
1021
1022	if (object == NULL)
1023		return;
1024
1025	mtx_lock(&Giant);
1026
1027	end = pindex + count;
1028
1029	/*
1030	 * Locate and adjust resident pages
1031	 */
1032	for (; pindex < end; pindex += 1) {
1033relookup:
1034		tobject = object;
1035		tpindex = pindex;
1036shadowlookup:
1037		/*
1038		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1039		 * and those pages must be OBJ_ONEMAPPING.
1040		 */
1041		if (advise == MADV_FREE) {
1042			if ((tobject->type != OBJT_DEFAULT &&
1043			     tobject->type != OBJT_SWAP) ||
1044			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
1045				continue;
1046			}
1047		}
1048
1049		m = vm_page_lookup(tobject, tpindex);
1050
1051		if (m == NULL) {
1052			/*
1053			 * There may be swap even if there is no backing page
1054			 */
1055			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1056				swap_pager_freespace(tobject, tpindex, 1);
1057
1058			/*
1059			 * next object
1060			 */
1061			tobject = tobject->backing_object;
1062			if (tobject == NULL)
1063				continue;
1064			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
1065			goto shadowlookup;
1066		}
1067
1068		/*
1069		 * If the page is busy or not in a normal active state,
1070		 * we skip it.  If the page is not managed there are no
1071		 * page queues to mess with.  Things can break if we mess
1072		 * with pages in any of the below states.
1073		 */
1074		if (
1075		    m->hold_count ||
1076		    m->wire_count ||
1077		    (m->flags & PG_UNMANAGED) ||
1078		    m->valid != VM_PAGE_BITS_ALL
1079		) {
1080			continue;
1081		}
1082
1083 		if (vm_page_sleep_busy(m, TRUE, "madvpo"))
1084  			goto relookup;
1085
1086		if (advise == MADV_WILLNEED) {
1087			vm_page_activate(m);
1088		} else if (advise == MADV_DONTNEED) {
1089			vm_page_dontneed(m);
1090		} else if (advise == MADV_FREE) {
1091			/*
1092			 * Mark the page clean.  This will allow the page
1093			 * to be freed up by the system.  However, such pages
1094			 * are often reused quickly by malloc()/free()
1095			 * so we do not do anything that would cause
1096			 * a page fault if we can help it.
1097			 *
1098			 * Specifically, we do not try to actually free
1099			 * the page now nor do we try to put it in the
1100			 * cache (which would cause a page fault on reuse).
1101			 *
1102			 * But we do make the page is freeable as we
1103			 * can without actually taking the step of unmapping
1104			 * it.
1105			 */
1106			pmap_clear_modify(m);
1107			m->dirty = 0;
1108			m->act_count = 0;
1109			vm_page_dontneed(m);
1110			if (tobject->type == OBJT_SWAP)
1111				swap_pager_freespace(tobject, tpindex, 1);
1112		}
1113	}
1114	mtx_unlock(&Giant);
1115}
1116
1117/*
1118 *	vm_object_shadow:
1119 *
1120 *	Create a new object which is backed by the
1121 *	specified existing object range.  The source
1122 *	object reference is deallocated.
1123 *
1124 *	The new object and offset into that object
1125 *	are returned in the source parameters.
1126 */
1127void
1128vm_object_shadow(
1129	vm_object_t *object,	/* IN/OUT */
1130	vm_ooffset_t *offset,	/* IN/OUT */
1131	vm_size_t length)
1132{
1133	vm_object_t source;
1134	vm_object_t result;
1135
1136	source = *object;
1137
1138	mtx_lock(&Giant);
1139	/*
1140	 * Don't create the new object if the old object isn't shared.
1141	 */
1142	if (source != NULL &&
1143	    source->ref_count == 1 &&
1144	    source->handle == NULL &&
1145	    (source->type == OBJT_DEFAULT ||
1146	     source->type == OBJT_SWAP)) {
1147		mtx_unlock(&Giant);
1148		return;
1149	}
1150
1151	/*
1152	 * Allocate a new object with the given length
1153	 */
1154	result = vm_object_allocate(OBJT_DEFAULT, length);
1155	KASSERT(result != NULL, ("vm_object_shadow: no object for shadowing"));
1156
1157	/*
1158	 * The new object shadows the source object, adding a reference to it.
1159	 * Our caller changes his reference to point to the new object,
1160	 * removing a reference to the source object.  Net result: no change
1161	 * of reference count.
1162	 *
1163	 * Try to optimize the result object's page color when shadowing
1164	 * in order to maintain page coloring consistency in the combined
1165	 * shadowed object.
1166	 */
1167	result->backing_object = source;
1168	if (source) {
1169		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
1170		source->shadow_count++;
1171		source->generation++;
1172		if (length < source->size)
1173			length = source->size;
1174		if (length > PQ_L2_SIZE / 3 + PQ_PRIME1 ||
1175		    source->generation > 1)
1176			length = PQ_L2_SIZE / 3 + PQ_PRIME1;
1177		result->pg_color = (source->pg_color +
1178		    length * source->generation) & PQ_L2_MASK;
1179		next_index = (result->pg_color + PQ_L2_SIZE / 3 + PQ_PRIME1) &
1180		    PQ_L2_MASK;
1181	}
1182
1183	/*
1184	 * Store the offset into the source object, and fix up the offset into
1185	 * the new object.
1186	 */
1187	result->backing_object_offset = *offset;
1188
1189	/*
1190	 * Return the new things
1191	 */
1192	*offset = 0;
1193	*object = result;
1194
1195	mtx_unlock(&Giant);
1196}
1197
1198/*
1199 *	vm_object_split:
1200 *
1201 * Split the pages in a map entry into a new object.  This affords
1202 * easier removal of unused pages, and keeps object inheritance from
1203 * being a negative impact on memory usage.
1204 */
1205void
1206vm_object_split(vm_map_entry_t entry)
1207{
1208	vm_page_t m;
1209	vm_object_t orig_object, new_object, source;
1210	vm_offset_t s, e;
1211	vm_pindex_t offidxstart, offidxend, idx;
1212	vm_size_t size;
1213	vm_ooffset_t offset;
1214
1215	GIANT_REQUIRED;
1216
1217	orig_object = entry->object.vm_object;
1218	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1219		return;
1220	if (orig_object->ref_count <= 1)
1221		return;
1222
1223	offset = entry->offset;
1224	s = entry->start;
1225	e = entry->end;
1226
1227	offidxstart = OFF_TO_IDX(offset);
1228	offidxend = offidxstart + OFF_TO_IDX(e - s);
1229	size = offidxend - offidxstart;
1230
1231	new_object = vm_pager_allocate(orig_object->type,
1232		NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL);
1233	if (new_object == NULL)
1234		return;
1235
1236	source = orig_object->backing_object;
1237	if (source != NULL) {
1238		vm_object_reference(source);	/* Referenced by new_object */
1239		TAILQ_INSERT_TAIL(&source->shadow_head,
1240				  new_object, shadow_list);
1241		vm_object_clear_flag(source, OBJ_ONEMAPPING);
1242		new_object->backing_object_offset =
1243			orig_object->backing_object_offset + IDX_TO_OFF(offidxstart);
1244		new_object->backing_object = source;
1245		source->shadow_count++;
1246		source->generation++;
1247	}
1248	for (idx = 0; idx < size; idx++) {
1249	retry:
1250		m = vm_page_lookup(orig_object, offidxstart + idx);
1251		if (m == NULL)
1252			continue;
1253
1254		/*
1255		 * We must wait for pending I/O to complete before we can
1256		 * rename the page.
1257		 *
1258		 * We do not have to VM_PROT_NONE the page as mappings should
1259		 * not be changed by this operation.
1260		 */
1261		if (vm_page_sleep_busy(m, TRUE, "spltwt"))
1262			goto retry;
1263
1264		vm_page_busy(m);
1265		vm_page_rename(m, new_object, idx);
1266		/* page automatically made dirty by rename and cache handled */
1267		vm_page_busy(m);
1268	}
1269	if (orig_object->type == OBJT_SWAP) {
1270		vm_object_pip_add(orig_object, 1);
1271		/*
1272		 * copy orig_object pages into new_object
1273		 * and destroy unneeded pages in
1274		 * shadow object.
1275		 */
1276		swap_pager_copy(orig_object, new_object, offidxstart, 0);
1277		vm_object_pip_wakeup(orig_object);
1278	}
1279	for (idx = 0; idx < size; idx++) {
1280		m = vm_page_lookup(new_object, idx);
1281		if (m != NULL)
1282			vm_page_wakeup(m);
1283	}
1284	entry->object.vm_object = new_object;
1285	entry->offset = 0LL;
1286	vm_object_deallocate(orig_object);
1287}
1288
1289#define	OBSC_TEST_ALL_SHADOWED	0x0001
1290#define	OBSC_COLLAPSE_NOWAIT	0x0002
1291#define	OBSC_COLLAPSE_WAIT	0x0004
1292
1293static __inline int
1294vm_object_backing_scan(vm_object_t object, int op)
1295{
1296	int s;
1297	int r = 1;
1298	vm_page_t p;
1299	vm_object_t backing_object;
1300	vm_pindex_t backing_offset_index;
1301
1302	s = splvm();
1303	GIANT_REQUIRED;
1304
1305	backing_object = object->backing_object;
1306	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1307
1308	/*
1309	 * Initial conditions
1310	 */
1311	if (op & OBSC_TEST_ALL_SHADOWED) {
1312		/*
1313		 * We do not want to have to test for the existence of
1314		 * swap pages in the backing object.  XXX but with the
1315		 * new swapper this would be pretty easy to do.
1316		 *
1317		 * XXX what about anonymous MAP_SHARED memory that hasn't
1318		 * been ZFOD faulted yet?  If we do not test for this, the
1319		 * shadow test may succeed! XXX
1320		 */
1321		if (backing_object->type != OBJT_DEFAULT) {
1322			splx(s);
1323			return (0);
1324		}
1325	}
1326	if (op & OBSC_COLLAPSE_WAIT) {
1327		vm_object_set_flag(backing_object, OBJ_DEAD);
1328	}
1329
1330	/*
1331	 * Our scan
1332	 */
1333	p = TAILQ_FIRST(&backing_object->memq);
1334	while (p) {
1335		vm_page_t next = TAILQ_NEXT(p, listq);
1336		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
1337
1338		if (op & OBSC_TEST_ALL_SHADOWED) {
1339			vm_page_t pp;
1340
1341			/*
1342			 * Ignore pages outside the parent object's range
1343			 * and outside the parent object's mapping of the
1344			 * backing object.
1345			 *
1346			 * note that we do not busy the backing object's
1347			 * page.
1348			 */
1349			if (
1350			    p->pindex < backing_offset_index ||
1351			    new_pindex >= object->size
1352			) {
1353				p = next;
1354				continue;
1355			}
1356
1357			/*
1358			 * See if the parent has the page or if the parent's
1359			 * object pager has the page.  If the parent has the
1360			 * page but the page is not valid, the parent's
1361			 * object pager must have the page.
1362			 *
1363			 * If this fails, the parent does not completely shadow
1364			 * the object and we might as well give up now.
1365			 */
1366
1367			pp = vm_page_lookup(object, new_pindex);
1368			if (
1369			    (pp == NULL || pp->valid == 0) &&
1370			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
1371			) {
1372				r = 0;
1373				break;
1374			}
1375		}
1376
1377		/*
1378		 * Check for busy page
1379		 */
1380		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1381			vm_page_t pp;
1382
1383			if (op & OBSC_COLLAPSE_NOWAIT) {
1384				if (
1385				    (p->flags & PG_BUSY) ||
1386				    !p->valid ||
1387				    p->hold_count ||
1388				    p->wire_count ||
1389				    p->busy
1390				) {
1391					p = next;
1392					continue;
1393				}
1394			} else if (op & OBSC_COLLAPSE_WAIT) {
1395				if (vm_page_sleep_busy(p, TRUE, "vmocol")) {
1396					/*
1397					 * If we slept, anything could have
1398					 * happened.  Since the object is
1399					 * marked dead, the backing offset
1400					 * should not have changed so we
1401					 * just restart our scan.
1402					 */
1403					p = TAILQ_FIRST(&backing_object->memq);
1404					continue;
1405				}
1406			}
1407
1408			/*
1409			 * Busy the page
1410			 */
1411			vm_page_busy(p);
1412
1413			KASSERT(
1414			    p->object == backing_object,
1415			    ("vm_object_qcollapse(): object mismatch")
1416			);
1417
1418			/*
1419			 * Destroy any associated swap
1420			 */
1421			if (backing_object->type == OBJT_SWAP) {
1422				swap_pager_freespace(
1423				    backing_object,
1424				    p->pindex,
1425				    1
1426				);
1427			}
1428
1429			if (
1430			    p->pindex < backing_offset_index ||
1431			    new_pindex >= object->size
1432			) {
1433				/*
1434				 * Page is out of the parent object's range, we
1435				 * can simply destroy it.
1436				 */
1437				vm_page_protect(p, VM_PROT_NONE);
1438				vm_page_free(p);
1439				p = next;
1440				continue;
1441			}
1442
1443			pp = vm_page_lookup(object, new_pindex);
1444			if (
1445			    pp != NULL ||
1446			    vm_pager_has_page(object, new_pindex, NULL, NULL)
1447			) {
1448				/*
1449				 * page already exists in parent OR swap exists
1450				 * for this location in the parent.  Destroy
1451				 * the original page from the backing object.
1452				 *
1453				 * Leave the parent's page alone
1454				 */
1455				vm_page_protect(p, VM_PROT_NONE);
1456				vm_page_free(p);
1457				p = next;
1458				continue;
1459			}
1460
1461			/*
1462			 * Page does not exist in parent, rename the
1463			 * page from the backing object to the main object.
1464			 *
1465			 * If the page was mapped to a process, it can remain
1466			 * mapped through the rename.
1467			 */
1468			if ((p->queue - p->pc) == PQ_CACHE)
1469				vm_page_deactivate(p);
1470
1471			vm_page_rename(p, object, new_pindex);
1472			/* page automatically made dirty by rename */
1473		}
1474		p = next;
1475	}
1476	splx(s);
1477	return (r);
1478}
1479
1480
1481/*
1482 * this version of collapse allows the operation to occur earlier and
1483 * when paging_in_progress is true for an object...  This is not a complete
1484 * operation, but should plug 99.9% of the rest of the leaks.
1485 */
1486static void
1487vm_object_qcollapse(vm_object_t object)
1488{
1489	vm_object_t backing_object = object->backing_object;
1490
1491	GIANT_REQUIRED;
1492
1493	if (backing_object->ref_count != 1)
1494		return;
1495
1496	backing_object->ref_count += 2;
1497
1498	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1499
1500	backing_object->ref_count -= 2;
1501}
1502
1503/*
1504 *	vm_object_collapse:
1505 *
1506 *	Collapse an object with the object backing it.
1507 *	Pages in the backing object are moved into the
1508 *	parent, and the backing object is deallocated.
1509 */
1510void
1511vm_object_collapse(vm_object_t object)
1512{
1513	GIANT_REQUIRED;
1514
1515	while (TRUE) {
1516		vm_object_t backing_object;
1517
1518		/*
1519		 * Verify that the conditions are right for collapse:
1520		 *
1521		 * The object exists and the backing object exists.
1522		 */
1523		if (object == NULL)
1524			break;
1525
1526		if ((backing_object = object->backing_object) == NULL)
1527			break;
1528
1529		/*
1530		 * we check the backing object first, because it is most likely
1531		 * not collapsable.
1532		 */
1533		if (backing_object->handle != NULL ||
1534		    (backing_object->type != OBJT_DEFAULT &&
1535		     backing_object->type != OBJT_SWAP) ||
1536		    (backing_object->flags & OBJ_DEAD) ||
1537		    object->handle != NULL ||
1538		    (object->type != OBJT_DEFAULT &&
1539		     object->type != OBJT_SWAP) ||
1540		    (object->flags & OBJ_DEAD)) {
1541			break;
1542		}
1543
1544		if (
1545		    object->paging_in_progress != 0 ||
1546		    backing_object->paging_in_progress != 0
1547		) {
1548			vm_object_qcollapse(object);
1549			break;
1550		}
1551
1552		/*
1553		 * We know that we can either collapse the backing object (if
1554		 * the parent is the only reference to it) or (perhaps) have
1555		 * the parent bypass the object if the parent happens to shadow
1556		 * all the resident pages in the entire backing object.
1557		 *
1558		 * This is ignoring pager-backed pages such as swap pages.
1559		 * vm_object_backing_scan fails the shadowing test in this
1560		 * case.
1561		 */
1562		if (backing_object->ref_count == 1) {
1563			/*
1564			 * If there is exactly one reference to the backing
1565			 * object, we can collapse it into the parent.
1566			 */
1567			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1568
1569			/*
1570			 * Move the pager from backing_object to object.
1571			 */
1572			if (backing_object->type == OBJT_SWAP) {
1573				vm_object_pip_add(backing_object, 1);
1574
1575				/*
1576				 * scrap the paging_offset junk and do a
1577				 * discrete copy.  This also removes major
1578				 * assumptions about how the swap-pager
1579				 * works from where it doesn't belong.  The
1580				 * new swapper is able to optimize the
1581				 * destroy-source case.
1582				 */
1583				vm_object_pip_add(object, 1);
1584				swap_pager_copy(
1585				    backing_object,
1586				    object,
1587				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1588				vm_object_pip_wakeup(object);
1589
1590				vm_object_pip_wakeup(backing_object);
1591			}
1592			/*
1593			 * Object now shadows whatever backing_object did.
1594			 * Note that the reference to
1595			 * backing_object->backing_object moves from within
1596			 * backing_object to within object.
1597			 */
1598			TAILQ_REMOVE(
1599			    &object->backing_object->shadow_head,
1600			    object,
1601			    shadow_list
1602			);
1603			object->backing_object->shadow_count--;
1604			object->backing_object->generation++;
1605			if (backing_object->backing_object) {
1606				TAILQ_REMOVE(
1607				    &backing_object->backing_object->shadow_head,
1608				    backing_object,
1609				    shadow_list
1610				);
1611				backing_object->backing_object->shadow_count--;
1612				backing_object->backing_object->generation++;
1613			}
1614			object->backing_object = backing_object->backing_object;
1615			if (object->backing_object) {
1616				TAILQ_INSERT_TAIL(
1617				    &object->backing_object->shadow_head,
1618				    object,
1619				    shadow_list
1620				);
1621				object->backing_object->shadow_count++;
1622				object->backing_object->generation++;
1623			}
1624
1625			object->backing_object_offset +=
1626			    backing_object->backing_object_offset;
1627
1628			/*
1629			 * Discard backing_object.
1630			 *
1631			 * Since the backing object has no pages, no pager left,
1632			 * and no object references within it, all that is
1633			 * necessary is to dispose of it.
1634			 */
1635			KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object));
1636			KASSERT(TAILQ_FIRST(&backing_object->memq) == NULL, ("backing_object %p somehow has left over pages during collapse!", backing_object));
1637
1638			mtx_lock(&vm_object_list_mtx);
1639			TAILQ_REMOVE(
1640			    &vm_object_list,
1641			    backing_object,
1642			    object_list
1643			);
1644			mtx_unlock(&vm_object_list_mtx);
1645
1646			uma_zfree(obj_zone, backing_object);
1647
1648			object_collapses++;
1649		} else {
1650			vm_object_t new_backing_object;
1651
1652			/*
1653			 * If we do not entirely shadow the backing object,
1654			 * there is nothing we can do so we give up.
1655			 */
1656			if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) {
1657				break;
1658			}
1659
1660			/*
1661			 * Make the parent shadow the next object in the
1662			 * chain.  Deallocating backing_object will not remove
1663			 * it, since its reference count is at least 2.
1664			 */
1665			TAILQ_REMOVE(
1666			    &backing_object->shadow_head,
1667			    object,
1668			    shadow_list
1669			);
1670			backing_object->shadow_count--;
1671			backing_object->generation++;
1672
1673			new_backing_object = backing_object->backing_object;
1674			if ((object->backing_object = new_backing_object) != NULL) {
1675				vm_object_reference(new_backing_object);
1676				TAILQ_INSERT_TAIL(
1677				    &new_backing_object->shadow_head,
1678				    object,
1679				    shadow_list
1680				);
1681				new_backing_object->shadow_count++;
1682				new_backing_object->generation++;
1683				object->backing_object_offset +=
1684					backing_object->backing_object_offset;
1685			}
1686
1687			/*
1688			 * Drop the reference count on backing_object. Since
1689			 * its ref_count was at least 2, it will not vanish;
1690			 * so we don't need to call vm_object_deallocate, but
1691			 * we do anyway.
1692			 */
1693			vm_object_deallocate(backing_object);
1694			object_bypasses++;
1695		}
1696
1697		/*
1698		 * Try again with this object's new backing object.
1699		 */
1700	}
1701}
1702
1703/*
1704 *	vm_object_page_remove: [internal]
1705 *
1706 *	Removes all physical pages in the specified
1707 *	object range from the object's list of pages.
1708 *
1709 *	The object must be locked.
1710 */
1711void
1712vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, boolean_t clean_only)
1713{
1714	vm_page_t p, next;
1715	vm_pindex_t size;
1716	int all;
1717
1718	if (object == NULL)
1719		return;
1720
1721	mtx_lock(&Giant);
1722	if (object->resident_page_count == 0) {
1723		mtx_unlock(&Giant);
1724		return;
1725	}
1726	all = ((end == 0) && (start == 0));
1727
1728	/*
1729	 * Since physically-backed objects do not use managed pages, we can't
1730	 * remove pages from the object (we must instead remove the page
1731	 * references, and then destroy the object).
1732	 */
1733	KASSERT(object->type != OBJT_PHYS, ("attempt to remove pages from a physical object"));
1734
1735	vm_object_pip_add(object, 1);
1736again:
1737	size = end - start;
1738	if (all || size > object->resident_page_count / 4) {
1739		for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1740			next = TAILQ_NEXT(p, listq);
1741			if (all || ((start <= p->pindex) && (p->pindex < end))) {
1742				if (p->wire_count != 0) {
1743					vm_page_protect(p, VM_PROT_NONE);
1744					if (!clean_only)
1745						p->valid = 0;
1746					continue;
1747				}
1748
1749				/*
1750				 * The busy flags are only cleared at
1751				 * interrupt -- minimize the spl transitions
1752				 */
1753 				if (vm_page_sleep_busy(p, TRUE, "vmopar"))
1754 					goto again;
1755
1756				if (clean_only && p->valid) {
1757					vm_page_test_dirty(p);
1758					if (p->valid & p->dirty)
1759						continue;
1760				}
1761
1762				vm_page_busy(p);
1763				vm_page_protect(p, VM_PROT_NONE);
1764				vm_page_free(p);
1765			}
1766		}
1767	} else {
1768		while (size > 0) {
1769			if ((p = vm_page_lookup(object, start)) != 0) {
1770
1771				if (p->wire_count != 0) {
1772					vm_page_protect(p, VM_PROT_NONE);
1773					if (!clean_only)
1774						p->valid = 0;
1775					start += 1;
1776					size -= 1;
1777					continue;
1778				}
1779
1780				/*
1781				 * The busy flags are only cleared at
1782				 * interrupt -- minimize the spl transitions
1783				 */
1784 				if (vm_page_sleep_busy(p, TRUE, "vmopar"))
1785					goto again;
1786
1787				if (clean_only && p->valid) {
1788					vm_page_test_dirty(p);
1789					if (p->valid & p->dirty) {
1790						start += 1;
1791						size -= 1;
1792						continue;
1793					}
1794				}
1795
1796				vm_page_busy(p);
1797				vm_page_protect(p, VM_PROT_NONE);
1798				vm_page_free(p);
1799			}
1800			start += 1;
1801			size -= 1;
1802		}
1803	}
1804	vm_object_pip_wakeup(object);
1805	mtx_unlock(&Giant);
1806}
1807
1808/*
1809 *	Routine:	vm_object_coalesce
1810 *	Function:	Coalesces two objects backing up adjoining
1811 *			regions of memory into a single object.
1812 *
1813 *	returns TRUE if objects were combined.
1814 *
1815 *	NOTE:	Only works at the moment if the second object is NULL -
1816 *		if it's not, which object do we lock first?
1817 *
1818 *	Parameters:
1819 *		prev_object	First object to coalesce
1820 *		prev_offset	Offset into prev_object
1821 *		next_object	Second object into coalesce
1822 *		next_offset	Offset into next_object
1823 *
1824 *		prev_size	Size of reference to prev_object
1825 *		next_size	Size of reference to next_object
1826 *
1827 *	Conditions:
1828 *	The object must *not* be locked.
1829 */
1830boolean_t
1831vm_object_coalesce(vm_object_t prev_object, vm_pindex_t prev_pindex,
1832	vm_size_t prev_size, vm_size_t next_size)
1833{
1834	vm_pindex_t next_pindex;
1835
1836	if (prev_object == NULL)
1837		return (TRUE);
1838	mtx_lock(&Giant);
1839	if (prev_object->type != OBJT_DEFAULT &&
1840	    prev_object->type != OBJT_SWAP) {
1841		mtx_unlock(&Giant);
1842		return (FALSE);
1843	}
1844
1845	/*
1846	 * Try to collapse the object first
1847	 */
1848	vm_object_collapse(prev_object);
1849
1850	/*
1851	 * Can't coalesce if: . more than one reference . paged out . shadows
1852	 * another object . has a copy elsewhere (any of which mean that the
1853	 * pages not mapped to prev_entry may be in use anyway)
1854	 */
1855	if (prev_object->backing_object != NULL) {
1856		mtx_unlock(&Giant);
1857		return (FALSE);
1858	}
1859
1860	prev_size >>= PAGE_SHIFT;
1861	next_size >>= PAGE_SHIFT;
1862	next_pindex = prev_pindex + prev_size;
1863
1864	if ((prev_object->ref_count > 1) &&
1865	    (prev_object->size != next_pindex)) {
1866		mtx_unlock(&Giant);
1867		return (FALSE);
1868	}
1869
1870	/*
1871	 * Remove any pages that may still be in the object from a previous
1872	 * deallocation.
1873	 */
1874	if (next_pindex < prev_object->size) {
1875		vm_object_page_remove(prev_object,
1876				      next_pindex,
1877				      next_pindex + next_size, FALSE);
1878		if (prev_object->type == OBJT_SWAP)
1879			swap_pager_freespace(prev_object,
1880					     next_pindex, next_size);
1881	}
1882
1883	/*
1884	 * Extend the object if necessary.
1885	 */
1886	if (next_pindex + next_size > prev_object->size)
1887		prev_object->size = next_pindex + next_size;
1888
1889	mtx_unlock(&Giant);
1890	return (TRUE);
1891}
1892
1893void
1894vm_object_set_writeable_dirty(vm_object_t object)
1895{
1896	struct vnode *vp;
1897
1898	vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
1899	if (object->type == OBJT_VNODE &&
1900	    (vp = (struct vnode *)object->handle) != NULL) {
1901		if ((vp->v_flag & VOBJDIRTY) == 0) {
1902			mtx_lock(&vp->v_interlock);
1903			vp->v_flag |= VOBJDIRTY;
1904			mtx_unlock(&vp->v_interlock);
1905		}
1906	}
1907}
1908
1909#ifdef ENABLE_VFS_IOOPT
1910/*
1911 * Experimental support for zero-copy I/O
1912 *
1913 * Performs the copy_on_write operations necessary to allow the virtual copies
1914 * into user space to work.  This has to be called for write(2) system calls
1915 * from other processes, file unlinking, and file size shrinkage.
1916 */
1917void
1918vm_freeze_copyopts(vm_object_t object, vm_pindex_t froma, vm_pindex_t toa)
1919{
1920	int rv;
1921	vm_object_t robject;
1922	vm_pindex_t idx;
1923
1924	GIANT_REQUIRED;
1925	if ((object == NULL) ||
1926		((object->flags & OBJ_OPT) == 0))
1927		return;
1928
1929	if (object->shadow_count > object->ref_count)
1930		panic("vm_freeze_copyopts: sc > rc");
1931
1932	while ((robject = TAILQ_FIRST(&object->shadow_head)) != NULL) {
1933		vm_pindex_t bo_pindex;
1934		vm_page_t m_in, m_out;
1935
1936		bo_pindex = OFF_TO_IDX(robject->backing_object_offset);
1937
1938		vm_object_reference(robject);
1939
1940		vm_object_pip_wait(robject, "objfrz");
1941
1942		if (robject->ref_count == 1) {
1943			vm_object_deallocate(robject);
1944			continue;
1945		}
1946
1947		vm_object_pip_add(robject, 1);
1948
1949		for (idx = 0; idx < robject->size; idx++) {
1950
1951			m_out = vm_page_grab(robject, idx,
1952						VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1953
1954			if (m_out->valid == 0) {
1955				m_in = vm_page_grab(object, bo_pindex + idx,
1956						VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1957				if (m_in->valid == 0) {
1958					rv = vm_pager_get_pages(object, &m_in, 1, 0);
1959					if (rv != VM_PAGER_OK) {
1960						printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex);
1961						continue;
1962					}
1963					vm_page_deactivate(m_in);
1964				}
1965
1966				vm_page_protect(m_in, VM_PROT_NONE);
1967				pmap_copy_page(m_in, m_out);
1968				m_out->valid = m_in->valid;
1969				vm_page_dirty(m_out);
1970				vm_page_activate(m_out);
1971				vm_page_wakeup(m_in);
1972			}
1973			vm_page_wakeup(m_out);
1974		}
1975
1976		object->shadow_count--;
1977		object->ref_count--;
1978		TAILQ_REMOVE(&object->shadow_head, robject, shadow_list);
1979		robject->backing_object = NULL;
1980		robject->backing_object_offset = 0;
1981
1982		vm_object_pip_wakeup(robject);
1983		vm_object_deallocate(robject);
1984	}
1985
1986	vm_object_clear_flag(object, OBJ_OPT);
1987}
1988#endif
1989
1990#include "opt_ddb.h"
1991#ifdef DDB
1992#include <sys/kernel.h>
1993
1994#include <sys/cons.h>
1995
1996#include <ddb/ddb.h>
1997
1998static int
1999_vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2000{
2001	vm_map_t tmpm;
2002	vm_map_entry_t tmpe;
2003	vm_object_t obj;
2004	int entcount;
2005
2006	if (map == 0)
2007		return 0;
2008
2009	if (entry == 0) {
2010		tmpe = map->header.next;
2011		entcount = map->nentries;
2012		while (entcount-- && (tmpe != &map->header)) {
2013			if (_vm_object_in_map(map, object, tmpe)) {
2014				return 1;
2015			}
2016			tmpe = tmpe->next;
2017		}
2018	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2019		tmpm = entry->object.sub_map;
2020		tmpe = tmpm->header.next;
2021		entcount = tmpm->nentries;
2022		while (entcount-- && tmpe != &tmpm->header) {
2023			if (_vm_object_in_map(tmpm, object, tmpe)) {
2024				return 1;
2025			}
2026			tmpe = tmpe->next;
2027		}
2028	} else if ((obj = entry->object.vm_object) != NULL) {
2029		for (; obj; obj = obj->backing_object)
2030			if (obj == object) {
2031				return 1;
2032			}
2033	}
2034	return 0;
2035}
2036
2037static int
2038vm_object_in_map(vm_object_t object)
2039{
2040	struct proc *p;
2041
2042	/* sx_slock(&allproc_lock); */
2043	LIST_FOREACH(p, &allproc, p_list) {
2044		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2045			continue;
2046		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
2047			/* sx_sunlock(&allproc_lock); */
2048			return 1;
2049		}
2050	}
2051	/* sx_sunlock(&allproc_lock); */
2052	if (_vm_object_in_map(kernel_map, object, 0))
2053		return 1;
2054	if (_vm_object_in_map(kmem_map, object, 0))
2055		return 1;
2056	if (_vm_object_in_map(pager_map, object, 0))
2057		return 1;
2058	if (_vm_object_in_map(buffer_map, object, 0))
2059		return 1;
2060	return 0;
2061}
2062
2063DB_SHOW_COMMAND(vmochk, vm_object_check)
2064{
2065	vm_object_t object;
2066
2067	/*
2068	 * make sure that internal objs are in a map somewhere
2069	 * and none have zero ref counts.
2070	 */
2071	TAILQ_FOREACH(object, &vm_object_list, object_list) {
2072		if (object->handle == NULL &&
2073		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2074			if (object->ref_count == 0) {
2075				db_printf("vmochk: internal obj has zero ref count: %ld\n",
2076					(long)object->size);
2077			}
2078			if (!vm_object_in_map(object)) {
2079				db_printf(
2080			"vmochk: internal obj is not in a map: "
2081			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2082				    object->ref_count, (u_long)object->size,
2083				    (u_long)object->size,
2084				    (void *)object->backing_object);
2085			}
2086		}
2087	}
2088}
2089
2090/*
2091 *	vm_object_print:	[ debug ]
2092 */
2093DB_SHOW_COMMAND(object, vm_object_print_static)
2094{
2095	/* XXX convert args. */
2096	vm_object_t object = (vm_object_t)addr;
2097	boolean_t full = have_addr;
2098
2099	vm_page_t p;
2100
2101	/* XXX count is an (unused) arg.  Avoid shadowing it. */
2102#define	count	was_count
2103
2104	int count;
2105
2106	if (object == NULL)
2107		return;
2108
2109	db_iprintf(
2110	    "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
2111	    object, (int)object->type, (u_long)object->size,
2112	    object->resident_page_count, object->ref_count, object->flags);
2113	/*
2114	 * XXX no %qd in kernel.  Truncate object->backing_object_offset.
2115	 */
2116	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
2117	    object->shadow_count,
2118	    object->backing_object ? object->backing_object->ref_count : 0,
2119	    object->backing_object, (long)object->backing_object_offset);
2120
2121	if (!full)
2122		return;
2123
2124	db_indent += 2;
2125	count = 0;
2126	TAILQ_FOREACH(p, &object->memq, listq) {
2127		if (count == 0)
2128			db_iprintf("memory:=");
2129		else if (count == 6) {
2130			db_printf("\n");
2131			db_iprintf(" ...");
2132			count = 0;
2133		} else
2134			db_printf(",");
2135		count++;
2136
2137		db_printf("(off=0x%lx,page=0x%lx)",
2138		    (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
2139	}
2140	if (count != 0)
2141		db_printf("\n");
2142	db_indent -= 2;
2143}
2144
2145/* XXX. */
2146#undef count
2147
2148/* XXX need this non-static entry for calling from vm_map_print. */
2149void
2150vm_object_print(
2151        /* db_expr_t */ long addr,
2152	boolean_t have_addr,
2153	/* db_expr_t */ long count,
2154	char *modif)
2155{
2156	vm_object_print_static(addr, have_addr, count, modif);
2157}
2158
2159DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2160{
2161	vm_object_t object;
2162	int nl = 0;
2163	int c;
2164
2165	TAILQ_FOREACH(object, &vm_object_list, object_list) {
2166		vm_pindex_t idx, fidx;
2167		vm_pindex_t osize;
2168		vm_offset_t pa = -1, padiff;
2169		int rcount;
2170		vm_page_t m;
2171
2172		db_printf("new object: %p\n", (void *)object);
2173		if (nl > 18) {
2174			c = cngetc();
2175			if (c != ' ')
2176				return;
2177			nl = 0;
2178		}
2179		nl++;
2180		rcount = 0;
2181		fidx = 0;
2182		osize = object->size;
2183		if (osize > 128)
2184			osize = 128;
2185		for (idx = 0; idx < osize; idx++) {
2186			m = vm_page_lookup(object, idx);
2187			if (m == NULL) {
2188				if (rcount) {
2189					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2190						(long)fidx, rcount, (long)pa);
2191					if (nl > 18) {
2192						c = cngetc();
2193						if (c != ' ')
2194							return;
2195						nl = 0;
2196					}
2197					nl++;
2198					rcount = 0;
2199				}
2200				continue;
2201			}
2202
2203
2204			if (rcount &&
2205				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2206				++rcount;
2207				continue;
2208			}
2209			if (rcount) {
2210				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
2211				padiff >>= PAGE_SHIFT;
2212				padiff &= PQ_L2_MASK;
2213				if (padiff == 0) {
2214					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
2215					++rcount;
2216					continue;
2217				}
2218				db_printf(" index(%ld)run(%d)pa(0x%lx)",
2219					(long)fidx, rcount, (long)pa);
2220				db_printf("pd(%ld)\n", (long)padiff);
2221				if (nl > 18) {
2222					c = cngetc();
2223					if (c != ' ')
2224						return;
2225					nl = 0;
2226				}
2227				nl++;
2228			}
2229			fidx = idx;
2230			pa = VM_PAGE_TO_PHYS(m);
2231			rcount = 1;
2232		}
2233		if (rcount) {
2234			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2235				(long)fidx, rcount, (long)pa);
2236			if (nl > 18) {
2237				c = cngetc();
2238				if (c != ' ')
2239					return;
2240				nl = 0;
2241			}
2242			nl++;
2243		}
2244	}
2245}
2246#endif /* DDB */
2247