xref: /freebsd/sys/vm/vm_object.c (revision b52b9d56d4e96089873a75f9e29062eec19fabba)
1 /*
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * The Mach Operating System project at Carnegie-Mellon University.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	from: @(#)vm_object.c	8.5 (Berkeley) 3/22/94
37  *
38  *
39  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40  * All rights reserved.
41  *
42  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43  *
44  * Permission to use, copy, modify and distribute this software and
45  * its documentation is hereby granted, provided that both the copyright
46  * notice and this permission notice appear in all copies of the
47  * software, derivative works or modified versions, and any portions
48  * thereof, and that both notices appear in supporting documentation.
49  *
50  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53  *
54  * Carnegie Mellon requests users of this software to return to
55  *
56  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57  *  School of Computer Science
58  *  Carnegie Mellon University
59  *  Pittsburgh PA 15213-3890
60  *
61  * any improvements or extensions that they make and grant Carnegie the
62  * rights to redistribute these changes.
63  *
64  * $FreeBSD$
65  */
66 
67 /*
68  *	Virtual memory object module.
69  */
70 
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/lock.h>
74 #include <sys/mman.h>
75 #include <sys/mount.h>
76 #include <sys/kernel.h>
77 #include <sys/sysctl.h>
78 #include <sys/mutex.h>
79 #include <sys/proc.h>		/* for curproc, pageproc */
80 #include <sys/socket.h>
81 #include <sys/vnode.h>
82 #include <sys/vmmeter.h>
83 #include <sys/sx.h>
84 
85 #include <vm/vm.h>
86 #include <vm/vm_param.h>
87 #include <vm/pmap.h>
88 #include <vm/vm_map.h>
89 #include <vm/vm_object.h>
90 #include <vm/vm_page.h>
91 #include <vm/vm_pageout.h>
92 #include <vm/vm_pager.h>
93 #include <vm/swap_pager.h>
94 #include <vm/vm_kern.h>
95 #include <vm/vm_extern.h>
96 #include <vm/uma.h>
97 
98 #define EASY_SCAN_FACTOR       8
99 
100 #define MSYNC_FLUSH_HARDSEQ	0x01
101 #define MSYNC_FLUSH_SOFTSEQ	0x02
102 
103 /*
104  * msync / VM object flushing optimizations
105  */
106 static int msync_flush_flags = MSYNC_FLUSH_HARDSEQ | MSYNC_FLUSH_SOFTSEQ;
107 SYSCTL_INT(_vm, OID_AUTO, msync_flush_flags,
108         CTLFLAG_RW, &msync_flush_flags, 0, "");
109 
110 static void	vm_object_qcollapse(vm_object_t object);
111 static int	vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags);
112 
113 /*
114  *	Virtual memory objects maintain the actual data
115  *	associated with allocated virtual memory.  A given
116  *	page of memory exists within exactly one object.
117  *
118  *	An object is only deallocated when all "references"
119  *	are given up.  Only one "reference" to a given
120  *	region of an object should be writeable.
121  *
122  *	Associated with each object is a list of all resident
123  *	memory pages belonging to that object; this list is
124  *	maintained by the "vm_page" module, and locked by the object's
125  *	lock.
126  *
127  *	Each object also records a "pager" routine which is
128  *	used to retrieve (and store) pages to the proper backing
129  *	storage.  In addition, objects may be backed by other
130  *	objects from which they were virtual-copied.
131  *
132  *	The only items within the object structure which are
133  *	modified after time of creation are:
134  *		reference count		locked by object's lock
135  *		pager routine		locked by object's lock
136  *
137  */
138 
139 struct object_q vm_object_list;
140 struct mtx vm_object_list_mtx;	/* lock for object list and count */
141 vm_object_t kernel_object;
142 vm_object_t kmem_object;
143 static struct vm_object kernel_object_store;
144 static struct vm_object kmem_object_store;
145 extern int vm_pageout_page_count;
146 
147 static long object_collapses;
148 static long object_bypasses;
149 static int next_index;
150 static uma_zone_t obj_zone;
151 #define VM_OBJECTS_INIT 256
152 
153 static void vm_object_zinit(void *mem, int size);
154 
155 #ifdef INVARIANTS
156 static void vm_object_zdtor(void *mem, int size, void *arg);
157 
158 static void
159 vm_object_zdtor(void *mem, int size, void *arg)
160 {
161 	vm_object_t object;
162 
163 	object = (vm_object_t)mem;
164 	KASSERT(object->paging_in_progress == 0,
165 	    ("object %p paging_in_progress = %d",
166 	    object, object->paging_in_progress));
167 	KASSERT(object->resident_page_count == 0,
168 	    ("object %p resident_page_count = %d",
169 	    object, object->resident_page_count));
170 	KASSERT(object->shadow_count == 0,
171 	    ("object %p shadow_count = %d",
172 	    object, object->shadow_count));
173 }
174 #endif
175 
176 static void
177 vm_object_zinit(void *mem, int size)
178 {
179 	vm_object_t object;
180 
181 	object = (vm_object_t)mem;
182 
183 	/* These are true for any object that has been freed */
184 	object->paging_in_progress = 0;
185 	object->resident_page_count = 0;
186 	object->shadow_count = 0;
187 }
188 
189 void
190 _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
191 {
192 	static int object_hash_rand;
193 	int exp, incr;
194 
195 	TAILQ_INIT(&object->memq);
196 	TAILQ_INIT(&object->shadow_head);
197 
198 	object->type = type;
199 	object->size = size;
200 	object->ref_count = 1;
201 	object->flags = 0;
202 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
203 		vm_object_set_flag(object, OBJ_ONEMAPPING);
204 	if (size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
205 		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
206 	else
207 		incr = size;
208 	do
209 		object->pg_color = next_index;
210 	while (!atomic_cmpset_int(&next_index, object->pg_color,
211 				  (object->pg_color + incr) & PQ_L2_MASK));
212 	object->handle = NULL;
213 	object->backing_object = NULL;
214 	object->backing_object_offset = (vm_ooffset_t) 0;
215 	/*
216 	 * Try to generate a number that will spread objects out in the
217 	 * hash table.  We 'wipe' new objects across the hash in 128 page
218 	 * increments plus 1 more to offset it a little more by the time
219 	 * it wraps around.
220 	 */
221 	do {
222 		exp = object_hash_rand;
223 		object->hash_rand = exp - 129;
224 	} while (!atomic_cmpset_int(&object_hash_rand, exp, object->hash_rand));
225 
226 	object->generation++;		/* atomicity needed? XXX */
227 
228 	mtx_lock(&vm_object_list_mtx);
229 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
230 	mtx_unlock(&vm_object_list_mtx);
231 }
232 
233 /*
234  *	vm_object_init:
235  *
236  *	Initialize the VM objects module.
237  */
238 void
239 vm_object_init(void)
240 {
241 	TAILQ_INIT(&vm_object_list);
242 	mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
243 
244 	kernel_object = &kernel_object_store;
245 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
246 	    kernel_object);
247 
248 	kmem_object = &kmem_object_store;
249 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
250 	    kmem_object);
251 	obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
252 #ifdef INVARIANTS
253 	    vm_object_zdtor,
254 #else
255 	    NULL,
256 #endif
257 	    vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
258 	uma_prealloc(obj_zone, VM_OBJECTS_INIT);
259 }
260 
261 void
262 vm_object_init2(void)
263 {
264 }
265 
266 void
267 vm_object_set_flag(vm_object_t object, u_short bits)
268 {
269 	object->flags |= bits;
270 }
271 
272 void
273 vm_object_clear_flag(vm_object_t object, u_short bits)
274 {
275 	GIANT_REQUIRED;
276 	object->flags &= ~bits;
277 }
278 
279 void
280 vm_object_pip_add(vm_object_t object, short i)
281 {
282 	GIANT_REQUIRED;
283 	object->paging_in_progress += i;
284 }
285 
286 void
287 vm_object_pip_subtract(vm_object_t object, short i)
288 {
289 	GIANT_REQUIRED;
290 	object->paging_in_progress -= i;
291 }
292 
293 void
294 vm_object_pip_wakeup(vm_object_t object)
295 {
296 	GIANT_REQUIRED;
297 	object->paging_in_progress--;
298 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
299 		vm_object_clear_flag(object, OBJ_PIPWNT);
300 		wakeup(object);
301 	}
302 }
303 
304 void
305 vm_object_pip_wakeupn(vm_object_t object, short i)
306 {
307 	GIANT_REQUIRED;
308 	if (i)
309 		object->paging_in_progress -= i;
310 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
311 		vm_object_clear_flag(object, OBJ_PIPWNT);
312 		wakeup(object);
313 	}
314 }
315 
316 void
317 vm_object_pip_sleep(vm_object_t object, char *waitid)
318 {
319 	GIANT_REQUIRED;
320 	if (object->paging_in_progress) {
321 		int s = splvm();
322 		if (object->paging_in_progress) {
323 			vm_object_set_flag(object, OBJ_PIPWNT);
324 			tsleep(object, PVM, waitid, 0);
325 		}
326 		splx(s);
327 	}
328 }
329 
330 void
331 vm_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  */
345 vm_object_t
346 vm_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  */
363 vm_object_t
364 vm_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  */
375 void
376 vm_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  */
400 void
401 vm_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  */
440 void
441 vm_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 		}
509 doterm:
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  */
540 void
541 vm_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 	vm_page_lock_queues();
594 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
595 		KASSERT(!p->busy && (p->flags & PG_BUSY) == 0,
596 			("vm_object_terminate: freeing busy page %p "
597 			"p->busy = %d, p->flags %x\n", p, p->busy, p->flags));
598 		if (p->wire_count == 0) {
599 			vm_page_busy(p);
600 			vm_page_free(p);
601 			cnt.v_pfree++;
602 		} else {
603 			vm_page_busy(p);
604 			vm_page_remove(p);
605 		}
606 	}
607 	vm_page_unlock_queues();
608 	splx(s);
609 
610 	/*
611 	 * Let the pager know object is dead.
612 	 */
613 	vm_pager_deallocate(object);
614 
615 	/*
616 	 * Remove the object from the global object list.
617 	 */
618 	mtx_lock(&vm_object_list_mtx);
619 	TAILQ_REMOVE(&vm_object_list, object, object_list);
620 	mtx_unlock(&vm_object_list_mtx);
621 
622 	wakeup(object);
623 
624 	/*
625 	 * Free the space for the object.
626 	 */
627 	uma_zfree(obj_zone, object);
628 }
629 
630 /*
631  *	vm_object_page_clean
632  *
633  *	Clean all dirty pages in the specified range of object.  Leaves page
634  * 	on whatever queue it is currently on.   If NOSYNC is set then do not
635  *	write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
636  *	leaving the object dirty.
637  *
638  *	Odd semantics: if start == end, we clean everything.
639  *
640  *	The object must be locked.
641  */
642 void
643 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags)
644 {
645 	vm_page_t p, np;
646 	vm_pindex_t tstart, tend;
647 	vm_pindex_t pi;
648 	struct vnode *vp;
649 	int clearobjflags;
650 	int pagerflags;
651 	int curgeneration;
652 
653 	GIANT_REQUIRED;
654 
655 	if (object->type != OBJT_VNODE ||
656 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
657 		return;
658 
659 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0;
660 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
661 
662 	vp = object->handle;
663 
664 	vm_object_set_flag(object, OBJ_CLEANING);
665 
666 	tstart = start;
667 	if (end == 0) {
668 		tend = object->size;
669 	} else {
670 		tend = end;
671 	}
672 
673 	/*
674 	 * If the caller is smart and only msync()s a range he knows is
675 	 * dirty, we may be able to avoid an object scan.  This results in
676 	 * a phenominal improvement in performance.  We cannot do this
677 	 * as a matter of course because the object may be huge - e.g.
678 	 * the size might be in the gigabytes or terrabytes.
679 	 */
680 	if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) {
681 		vm_pindex_t tscan;
682 		int scanlimit;
683 		int scanreset;
684 
685 		scanreset = object->resident_page_count / EASY_SCAN_FACTOR;
686 		if (scanreset < 16)
687 			scanreset = 16;
688 
689 		scanlimit = scanreset;
690 		tscan = tstart;
691 		while (tscan < tend) {
692 			curgeneration = object->generation;
693 			p = vm_page_lookup(object, tscan);
694 			if (p == NULL || p->valid == 0 ||
695 			    (p->queue - p->pc) == PQ_CACHE) {
696 				if (--scanlimit == 0)
697 					break;
698 				++tscan;
699 				continue;
700 			}
701 			vm_page_test_dirty(p);
702 			if ((p->dirty & p->valid) == 0) {
703 				if (--scanlimit == 0)
704 					break;
705 				++tscan;
706 				continue;
707 			}
708 			/*
709 			 * If we have been asked to skip nosync pages and
710 			 * this is a nosync page, we can't continue.
711 			 */
712 			if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
713 				if (--scanlimit == 0)
714 					break;
715 				++tscan;
716 				continue;
717 			}
718 			scanlimit = scanreset;
719 
720 			/*
721 			 * This returns 0 if it was unable to busy the first
722 			 * page (i.e. had to sleep).
723 			 */
724 			tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags);
725 		}
726 
727 		/*
728 		 * If everything was dirty and we flushed it successfully,
729 		 * and the requested range is not the entire object, we
730 		 * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can
731 		 * return immediately.
732 		 */
733 		if (tscan >= tend && (tstart || tend < object->size)) {
734 			vm_object_clear_flag(object, OBJ_CLEANING);
735 			return;
736 		}
737 	}
738 
739 	/*
740 	 * Generally set CLEANCHK interlock and make the page read-only so
741 	 * we can then clear the object flags.
742 	 *
743 	 * However, if this is a nosync mmap then the object is likely to
744 	 * stay dirty so do not mess with the page and do not clear the
745 	 * object flags.
746 	 */
747 	clearobjflags = 1;
748 
749 	TAILQ_FOREACH(p, &object->memq, listq) {
750 		vm_page_flag_set(p, PG_CLEANCHK);
751 		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC))
752 			clearobjflags = 0;
753 		else
754 			vm_page_protect(p, VM_PROT_READ);
755 	}
756 
757 	if (clearobjflags && (tstart == 0) && (tend == object->size)) {
758 		struct vnode *vp;
759 
760 		vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
761 		if (object->type == OBJT_VNODE &&
762 		    (vp = (struct vnode *)object->handle) != NULL) {
763 			if (vp->v_flag & VOBJDIRTY) {
764 				mtx_lock(&vp->v_interlock);
765 				vp->v_flag &= ~VOBJDIRTY;
766 				mtx_unlock(&vp->v_interlock);
767 			}
768 		}
769 	}
770 
771 rescan:
772 	curgeneration = object->generation;
773 
774 	for (p = TAILQ_FIRST(&object->memq); p; p = np) {
775 		int n;
776 
777 		np = TAILQ_NEXT(p, listq);
778 
779 again:
780 		pi = p->pindex;
781 		if (((p->flags & PG_CLEANCHK) == 0) ||
782 			(pi < tstart) || (pi >= tend) ||
783 			(p->valid == 0) ||
784 			((p->queue - p->pc) == PQ_CACHE)) {
785 			vm_page_flag_clear(p, PG_CLEANCHK);
786 			continue;
787 		}
788 
789 		vm_page_test_dirty(p);
790 		if ((p->dirty & p->valid) == 0) {
791 			vm_page_flag_clear(p, PG_CLEANCHK);
792 			continue;
793 		}
794 
795 		/*
796 		 * If we have been asked to skip nosync pages and this is a
797 		 * nosync page, skip it.  Note that the object flags were
798 		 * not cleared in this case so we do not have to set them.
799 		 */
800 		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
801 			vm_page_flag_clear(p, PG_CLEANCHK);
802 			continue;
803 		}
804 
805 		n = vm_object_page_collect_flush(object, p,
806 			curgeneration, pagerflags);
807 		if (n == 0)
808 			goto rescan;
809 
810 		if (object->generation != curgeneration)
811 			goto rescan;
812 
813 		/*
814 		 * Try to optimize the next page.  If we can't we pick up
815 		 * our (random) scan where we left off.
816 		 */
817 		if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) {
818 			if ((p = vm_page_lookup(object, pi + n)) != NULL)
819 				goto again;
820 		}
821 	}
822 
823 #if 0
824 	VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
825 #endif
826 
827 	vm_object_clear_flag(object, OBJ_CLEANING);
828 	return;
829 }
830 
831 static int
832 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags)
833 {
834 	int runlen;
835 	int s;
836 	int maxf;
837 	int chkb;
838 	int maxb;
839 	int i;
840 	vm_pindex_t pi;
841 	vm_page_t maf[vm_pageout_page_count];
842 	vm_page_t mab[vm_pageout_page_count];
843 	vm_page_t ma[vm_pageout_page_count];
844 
845 	s = splvm();
846 	pi = p->pindex;
847 	while (vm_page_sleep_busy(p, TRUE, "vpcwai")) {
848 		if (object->generation != curgeneration) {
849 			splx(s);
850 			return(0);
851 		}
852 	}
853 	vm_page_lock_queues();
854 	maxf = 0;
855 	for(i = 1; i < vm_pageout_page_count; i++) {
856 		vm_page_t tp;
857 
858 		if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
859 			if ((tp->flags & PG_BUSY) ||
860 				(tp->flags & PG_CLEANCHK) == 0 ||
861 				(tp->busy != 0))
862 				break;
863 			if((tp->queue - tp->pc) == PQ_CACHE) {
864 				vm_page_flag_clear(tp, PG_CLEANCHK);
865 				break;
866 			}
867 			vm_page_test_dirty(tp);
868 			if ((tp->dirty & tp->valid) == 0) {
869 				vm_page_flag_clear(tp, PG_CLEANCHK);
870 				break;
871 			}
872 			maf[ i - 1 ] = tp;
873 			maxf++;
874 			continue;
875 		}
876 		break;
877 	}
878 
879 	maxb = 0;
880 	chkb = vm_pageout_page_count -  maxf;
881 	if (chkb) {
882 		for(i = 1; i < chkb;i++) {
883 			vm_page_t tp;
884 
885 			if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
886 				if ((tp->flags & PG_BUSY) ||
887 					(tp->flags & PG_CLEANCHK) == 0 ||
888 					(tp->busy != 0))
889 					break;
890 				if ((tp->queue - tp->pc) == PQ_CACHE) {
891 					vm_page_flag_clear(tp, PG_CLEANCHK);
892 					break;
893 				}
894 				vm_page_test_dirty(tp);
895 				if ((tp->dirty & tp->valid) == 0) {
896 					vm_page_flag_clear(tp, PG_CLEANCHK);
897 					break;
898 				}
899 				mab[ i - 1 ] = tp;
900 				maxb++;
901 				continue;
902 			}
903 			break;
904 		}
905 	}
906 
907 	for(i = 0; i < maxb; i++) {
908 		int index = (maxb - i) - 1;
909 		ma[index] = mab[i];
910 		vm_page_flag_clear(ma[index], PG_CLEANCHK);
911 	}
912 	vm_page_flag_clear(p, PG_CLEANCHK);
913 	ma[maxb] = p;
914 	for(i = 0; i < maxf; i++) {
915 		int index = (maxb + i) + 1;
916 		ma[index] = maf[i];
917 		vm_page_flag_clear(ma[index], PG_CLEANCHK);
918 	}
919 	runlen = maxb + maxf + 1;
920 
921 	splx(s);
922 	vm_pageout_flush(ma, runlen, pagerflags);
923 	for (i = 0; i < runlen; i++) {
924 		if (ma[i]->valid & ma[i]->dirty) {
925 			vm_page_protect(ma[i], VM_PROT_READ);
926 			vm_page_flag_set(ma[i], PG_CLEANCHK);
927 
928 			/*
929 			 * maxf will end up being the actual number of pages
930 			 * we wrote out contiguously, non-inclusive of the
931 			 * first page.  We do not count look-behind pages.
932 			 */
933 			if (i >= maxb + 1 && (maxf > i - maxb - 1))
934 				maxf = i - maxb - 1;
935 		}
936 	}
937 	vm_page_unlock_queues();
938 	return(maxf + 1);
939 }
940 
941 #ifdef ENABLE_VFS_IOOPT
942 /*
943  * Same as vm_object_pmap_copy, except range checking really
944  * works, and is meant for small sections of an object.
945  *
946  * This code protects resident pages by making them read-only
947  * and is typically called on a fork or split when a page
948  * is converted to copy-on-write.
949  *
950  * NOTE: If the page is already at VM_PROT_NONE, calling
951  * vm_page_protect will have no effect.
952  */
953 void
954 vm_object_pmap_copy_1(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
955 {
956 	vm_pindex_t idx;
957 	vm_page_t p;
958 
959 	GIANT_REQUIRED;
960 
961 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
962 		return;
963 
964 	for (idx = start; idx < end; idx++) {
965 		p = vm_page_lookup(object, idx);
966 		if (p == NULL)
967 			continue;
968 		vm_page_protect(p, VM_PROT_READ);
969 	}
970 }
971 #endif
972 
973 /*
974  *	vm_object_pmap_remove:
975  *
976  *	Removes all physical pages in the specified
977  *	object range from all physical maps.
978  *
979  *	The object must *not* be locked.
980  */
981 void
982 vm_object_pmap_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
983 {
984 	vm_page_t p;
985 
986 	GIANT_REQUIRED;
987 	if (object == NULL)
988 		return;
989 	TAILQ_FOREACH(p, &object->memq, listq) {
990 		if (p->pindex >= start && p->pindex < end)
991 			vm_page_protect(p, VM_PROT_NONE);
992 	}
993 	if ((start == 0) && (object->size == end))
994 		vm_object_clear_flag(object, OBJ_WRITEABLE);
995 }
996 
997 /*
998  *	vm_object_madvise:
999  *
1000  *	Implements the madvise function at the object/page level.
1001  *
1002  *	MADV_WILLNEED	(any object)
1003  *
1004  *	    Activate the specified pages if they are resident.
1005  *
1006  *	MADV_DONTNEED	(any object)
1007  *
1008  *	    Deactivate the specified pages if they are resident.
1009  *
1010  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
1011  *			 OBJ_ONEMAPPING only)
1012  *
1013  *	    Deactivate and clean the specified pages if they are
1014  *	    resident.  This permits the process to reuse the pages
1015  *	    without faulting or the kernel to reclaim the pages
1016  *	    without I/O.
1017  */
1018 void
1019 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
1020 {
1021 	vm_pindex_t end, tpindex;
1022 	vm_object_t tobject;
1023 	vm_page_t m;
1024 
1025 	if (object == NULL)
1026 		return;
1027 
1028 	mtx_lock(&Giant);
1029 
1030 	end = pindex + count;
1031 
1032 	/*
1033 	 * Locate and adjust resident pages
1034 	 */
1035 	for (; pindex < end; pindex += 1) {
1036 relookup:
1037 		tobject = object;
1038 		tpindex = pindex;
1039 shadowlookup:
1040 		/*
1041 		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1042 		 * and those pages must be OBJ_ONEMAPPING.
1043 		 */
1044 		if (advise == MADV_FREE) {
1045 			if ((tobject->type != OBJT_DEFAULT &&
1046 			     tobject->type != OBJT_SWAP) ||
1047 			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
1048 				continue;
1049 			}
1050 		}
1051 
1052 		m = vm_page_lookup(tobject, tpindex);
1053 
1054 		if (m == NULL) {
1055 			/*
1056 			 * There may be swap even if there is no backing page
1057 			 */
1058 			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1059 				swap_pager_freespace(tobject, tpindex, 1);
1060 
1061 			/*
1062 			 * next object
1063 			 */
1064 			tobject = tobject->backing_object;
1065 			if (tobject == NULL)
1066 				continue;
1067 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
1068 			goto shadowlookup;
1069 		}
1070 
1071 		/*
1072 		 * If the page is busy or not in a normal active state,
1073 		 * we skip it.  If the page is not managed there are no
1074 		 * page queues to mess with.  Things can break if we mess
1075 		 * with pages in any of the below states.
1076 		 */
1077 		if (
1078 		    m->hold_count ||
1079 		    m->wire_count ||
1080 		    (m->flags & PG_UNMANAGED) ||
1081 		    m->valid != VM_PAGE_BITS_ALL
1082 		) {
1083 			continue;
1084 		}
1085 
1086  		if (vm_page_sleep_busy(m, TRUE, "madvpo"))
1087   			goto relookup;
1088 		vm_page_lock_queues();
1089 		if (advise == MADV_WILLNEED) {
1090 			vm_page_activate(m);
1091 		} else if (advise == MADV_DONTNEED) {
1092 			vm_page_dontneed(m);
1093 		} else if (advise == MADV_FREE) {
1094 			/*
1095 			 * Mark the page clean.  This will allow the page
1096 			 * to be freed up by the system.  However, such pages
1097 			 * are often reused quickly by malloc()/free()
1098 			 * so we do not do anything that would cause
1099 			 * a page fault if we can help it.
1100 			 *
1101 			 * Specifically, we do not try to actually free
1102 			 * the page now nor do we try to put it in the
1103 			 * cache (which would cause a page fault on reuse).
1104 			 *
1105 			 * But we do make the page is freeable as we
1106 			 * can without actually taking the step of unmapping
1107 			 * it.
1108 			 */
1109 			pmap_clear_modify(m);
1110 			m->dirty = 0;
1111 			m->act_count = 0;
1112 			vm_page_dontneed(m);
1113 		}
1114 		vm_page_unlock_queues();
1115 		if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1116 			swap_pager_freespace(tobject, tpindex, 1);
1117 	}
1118 	mtx_unlock(&Giant);
1119 }
1120 
1121 /*
1122  *	vm_object_shadow:
1123  *
1124  *	Create a new object which is backed by the
1125  *	specified existing object range.  The source
1126  *	object reference is deallocated.
1127  *
1128  *	The new object and offset into that object
1129  *	are returned in the source parameters.
1130  */
1131 void
1132 vm_object_shadow(
1133 	vm_object_t *object,	/* IN/OUT */
1134 	vm_ooffset_t *offset,	/* IN/OUT */
1135 	vm_size_t length)
1136 {
1137 	vm_object_t source;
1138 	vm_object_t result;
1139 
1140 	source = *object;
1141 
1142 	mtx_lock(&Giant);
1143 	/*
1144 	 * Don't create the new object if the old object isn't shared.
1145 	 */
1146 	if (source != NULL &&
1147 	    source->ref_count == 1 &&
1148 	    source->handle == NULL &&
1149 	    (source->type == OBJT_DEFAULT ||
1150 	     source->type == OBJT_SWAP)) {
1151 		mtx_unlock(&Giant);
1152 		return;
1153 	}
1154 
1155 	/*
1156 	 * Allocate a new object with the given length
1157 	 */
1158 	result = vm_object_allocate(OBJT_DEFAULT, length);
1159 	KASSERT(result != NULL, ("vm_object_shadow: no object for shadowing"));
1160 
1161 	/*
1162 	 * The new object shadows the source object, adding a reference to it.
1163 	 * Our caller changes his reference to point to the new object,
1164 	 * removing a reference to the source object.  Net result: no change
1165 	 * of reference count.
1166 	 *
1167 	 * Try to optimize the result object's page color when shadowing
1168 	 * in order to maintain page coloring consistency in the combined
1169 	 * shadowed object.
1170 	 */
1171 	result->backing_object = source;
1172 	if (source) {
1173 		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
1174 		source->shadow_count++;
1175 		source->generation++;
1176 		if (length < source->size)
1177 			length = source->size;
1178 		if (length > PQ_L2_SIZE / 3 + PQ_PRIME1 ||
1179 		    source->generation > 1)
1180 			length = PQ_L2_SIZE / 3 + PQ_PRIME1;
1181 		result->pg_color = (source->pg_color +
1182 		    length * source->generation) & PQ_L2_MASK;
1183 		next_index = (result->pg_color + PQ_L2_SIZE / 3 + PQ_PRIME1) &
1184 		    PQ_L2_MASK;
1185 	}
1186 
1187 	/*
1188 	 * Store the offset into the source object, and fix up the offset into
1189 	 * the new object.
1190 	 */
1191 	result->backing_object_offset = *offset;
1192 
1193 	/*
1194 	 * Return the new things
1195 	 */
1196 	*offset = 0;
1197 	*object = result;
1198 
1199 	mtx_unlock(&Giant);
1200 }
1201 
1202 /*
1203  *	vm_object_split:
1204  *
1205  * Split the pages in a map entry into a new object.  This affords
1206  * easier removal of unused pages, and keeps object inheritance from
1207  * being a negative impact on memory usage.
1208  */
1209 void
1210 vm_object_split(vm_map_entry_t entry)
1211 {
1212 	vm_page_t m;
1213 	vm_object_t orig_object, new_object, source;
1214 	vm_offset_t s, e;
1215 	vm_pindex_t offidxstart, offidxend;
1216 	vm_size_t idx, size;
1217 	vm_ooffset_t offset;
1218 
1219 	GIANT_REQUIRED;
1220 
1221 	orig_object = entry->object.vm_object;
1222 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1223 		return;
1224 	if (orig_object->ref_count <= 1)
1225 		return;
1226 
1227 	offset = entry->offset;
1228 	s = entry->start;
1229 	e = entry->end;
1230 
1231 	offidxstart = OFF_TO_IDX(offset);
1232 	offidxend = offidxstart + OFF_TO_IDX(e - s);
1233 	size = offidxend - offidxstart;
1234 
1235 	new_object = vm_pager_allocate(orig_object->type,
1236 		NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL);
1237 	if (new_object == NULL)
1238 		return;
1239 
1240 	source = orig_object->backing_object;
1241 	if (source != NULL) {
1242 		vm_object_reference(source);	/* Referenced by new_object */
1243 		TAILQ_INSERT_TAIL(&source->shadow_head,
1244 				  new_object, shadow_list);
1245 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
1246 		new_object->backing_object_offset =
1247 			orig_object->backing_object_offset + offset;
1248 		new_object->backing_object = source;
1249 		source->shadow_count++;
1250 		source->generation++;
1251 	}
1252 	for (idx = 0; idx < size; idx++) {
1253 	retry:
1254 		m = vm_page_lookup(orig_object, offidxstart + idx);
1255 		if (m == NULL)
1256 			continue;
1257 
1258 		/*
1259 		 * We must wait for pending I/O to complete before we can
1260 		 * rename the page.
1261 		 *
1262 		 * We do not have to VM_PROT_NONE the page as mappings should
1263 		 * not be changed by this operation.
1264 		 */
1265 		if (vm_page_sleep_busy(m, TRUE, "spltwt"))
1266 			goto retry;
1267 
1268 		vm_page_busy(m);
1269 		vm_page_rename(m, new_object, idx);
1270 		/* page automatically made dirty by rename and cache handled */
1271 		vm_page_busy(m);
1272 	}
1273 	if (orig_object->type == OBJT_SWAP) {
1274 		vm_object_pip_add(orig_object, 1);
1275 		/*
1276 		 * copy orig_object pages into new_object
1277 		 * and destroy unneeded pages in
1278 		 * shadow object.
1279 		 */
1280 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
1281 		vm_object_pip_wakeup(orig_object);
1282 	}
1283 	TAILQ_FOREACH(m, &new_object->memq, listq)
1284 		vm_page_wakeup(m);
1285 	entry->object.vm_object = new_object;
1286 	entry->offset = 0LL;
1287 	vm_object_deallocate(orig_object);
1288 }
1289 
1290 #define	OBSC_TEST_ALL_SHADOWED	0x0001
1291 #define	OBSC_COLLAPSE_NOWAIT	0x0002
1292 #define	OBSC_COLLAPSE_WAIT	0x0004
1293 
1294 static __inline int
1295 vm_object_backing_scan(vm_object_t object, int op)
1296 {
1297 	int s;
1298 	int r = 1;
1299 	vm_page_t p;
1300 	vm_object_t backing_object;
1301 	vm_pindex_t backing_offset_index;
1302 
1303 	s = splvm();
1304 	GIANT_REQUIRED;
1305 
1306 	backing_object = object->backing_object;
1307 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1308 
1309 	/*
1310 	 * Initial conditions
1311 	 */
1312 	if (op & OBSC_TEST_ALL_SHADOWED) {
1313 		/*
1314 		 * We do not want to have to test for the existence of
1315 		 * swap pages in the backing object.  XXX but with the
1316 		 * new swapper this would be pretty easy to do.
1317 		 *
1318 		 * XXX what about anonymous MAP_SHARED memory that hasn't
1319 		 * been ZFOD faulted yet?  If we do not test for this, the
1320 		 * shadow test may succeed! XXX
1321 		 */
1322 		if (backing_object->type != OBJT_DEFAULT) {
1323 			splx(s);
1324 			return (0);
1325 		}
1326 	}
1327 	if (op & OBSC_COLLAPSE_WAIT) {
1328 		vm_object_set_flag(backing_object, OBJ_DEAD);
1329 	}
1330 
1331 	/*
1332 	 * Our scan
1333 	 */
1334 	p = TAILQ_FIRST(&backing_object->memq);
1335 	while (p) {
1336 		vm_page_t next = TAILQ_NEXT(p, listq);
1337 		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
1338 
1339 		if (op & OBSC_TEST_ALL_SHADOWED) {
1340 			vm_page_t pp;
1341 
1342 			/*
1343 			 * Ignore pages outside the parent object's range
1344 			 * and outside the parent object's mapping of the
1345 			 * backing object.
1346 			 *
1347 			 * note that we do not busy the backing object's
1348 			 * page.
1349 			 */
1350 			if (
1351 			    p->pindex < backing_offset_index ||
1352 			    new_pindex >= object->size
1353 			) {
1354 				p = next;
1355 				continue;
1356 			}
1357 
1358 			/*
1359 			 * See if the parent has the page or if the parent's
1360 			 * object pager has the page.  If the parent has the
1361 			 * page but the page is not valid, the parent's
1362 			 * object pager must have the page.
1363 			 *
1364 			 * If this fails, the parent does not completely shadow
1365 			 * the object and we might as well give up now.
1366 			 */
1367 
1368 			pp = vm_page_lookup(object, new_pindex);
1369 			if (
1370 			    (pp == NULL || pp->valid == 0) &&
1371 			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
1372 			) {
1373 				r = 0;
1374 				break;
1375 			}
1376 		}
1377 
1378 		/*
1379 		 * Check for busy page
1380 		 */
1381 		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1382 			vm_page_t pp;
1383 
1384 			if (op & OBSC_COLLAPSE_NOWAIT) {
1385 				if (
1386 				    (p->flags & PG_BUSY) ||
1387 				    !p->valid ||
1388 				    p->hold_count ||
1389 				    p->wire_count ||
1390 				    p->busy
1391 				) {
1392 					p = next;
1393 					continue;
1394 				}
1395 			} else if (op & OBSC_COLLAPSE_WAIT) {
1396 				if (vm_page_sleep_busy(p, TRUE, "vmocol")) {
1397 					/*
1398 					 * If we slept, anything could have
1399 					 * happened.  Since the object is
1400 					 * marked dead, the backing offset
1401 					 * should not have changed so we
1402 					 * just restart our scan.
1403 					 */
1404 					p = TAILQ_FIRST(&backing_object->memq);
1405 					continue;
1406 				}
1407 			}
1408 
1409 			/*
1410 			 * Busy the page
1411 			 */
1412 			vm_page_busy(p);
1413 
1414 			KASSERT(
1415 			    p->object == backing_object,
1416 			    ("vm_object_qcollapse(): object mismatch")
1417 			);
1418 
1419 			/*
1420 			 * Destroy any associated swap
1421 			 */
1422 			if (backing_object->type == OBJT_SWAP) {
1423 				swap_pager_freespace(
1424 				    backing_object,
1425 				    p->pindex,
1426 				    1
1427 				);
1428 			}
1429 
1430 			if (
1431 			    p->pindex < backing_offset_index ||
1432 			    new_pindex >= object->size
1433 			) {
1434 				/*
1435 				 * Page is out of the parent object's range, we
1436 				 * can simply destroy it.
1437 				 */
1438 				vm_page_lock_queues();
1439 				vm_page_protect(p, VM_PROT_NONE);
1440 				vm_page_free(p);
1441 				vm_page_unlock_queues();
1442 				p = next;
1443 				continue;
1444 			}
1445 
1446 			pp = vm_page_lookup(object, new_pindex);
1447 			if (
1448 			    pp != NULL ||
1449 			    vm_pager_has_page(object, new_pindex, NULL, NULL)
1450 			) {
1451 				/*
1452 				 * page already exists in parent OR swap exists
1453 				 * for this location in the parent.  Destroy
1454 				 * the original page from the backing object.
1455 				 *
1456 				 * Leave the parent's page alone
1457 				 */
1458 				vm_page_lock_queues();
1459 				vm_page_protect(p, VM_PROT_NONE);
1460 				vm_page_free(p);
1461 				vm_page_unlock_queues();
1462 				p = next;
1463 				continue;
1464 			}
1465 
1466 			/*
1467 			 * Page does not exist in parent, rename the
1468 			 * page from the backing object to the main object.
1469 			 *
1470 			 * If the page was mapped to a process, it can remain
1471 			 * mapped through the rename.
1472 			 */
1473 			vm_page_rename(p, object, new_pindex);
1474 			/* page automatically made dirty by rename */
1475 		}
1476 		p = next;
1477 	}
1478 	splx(s);
1479 	return (r);
1480 }
1481 
1482 
1483 /*
1484  * this version of collapse allows the operation to occur earlier and
1485  * when paging_in_progress is true for an object...  This is not a complete
1486  * operation, but should plug 99.9% of the rest of the leaks.
1487  */
1488 static void
1489 vm_object_qcollapse(vm_object_t object)
1490 {
1491 	vm_object_t backing_object = object->backing_object;
1492 
1493 	GIANT_REQUIRED;
1494 
1495 	if (backing_object->ref_count != 1)
1496 		return;
1497 
1498 	backing_object->ref_count += 2;
1499 
1500 	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1501 
1502 	backing_object->ref_count -= 2;
1503 }
1504 
1505 /*
1506  *	vm_object_collapse:
1507  *
1508  *	Collapse an object with the object backing it.
1509  *	Pages in the backing object are moved into the
1510  *	parent, and the backing object is deallocated.
1511  */
1512 void
1513 vm_object_collapse(vm_object_t object)
1514 {
1515 	GIANT_REQUIRED;
1516 
1517 	while (TRUE) {
1518 		vm_object_t backing_object;
1519 
1520 		/*
1521 		 * Verify that the conditions are right for collapse:
1522 		 *
1523 		 * The object exists and the backing object exists.
1524 		 */
1525 		if (object == NULL)
1526 			break;
1527 
1528 		if ((backing_object = object->backing_object) == NULL)
1529 			break;
1530 
1531 		/*
1532 		 * we check the backing object first, because it is most likely
1533 		 * not collapsable.
1534 		 */
1535 		if (backing_object->handle != NULL ||
1536 		    (backing_object->type != OBJT_DEFAULT &&
1537 		     backing_object->type != OBJT_SWAP) ||
1538 		    (backing_object->flags & OBJ_DEAD) ||
1539 		    object->handle != NULL ||
1540 		    (object->type != OBJT_DEFAULT &&
1541 		     object->type != OBJT_SWAP) ||
1542 		    (object->flags & OBJ_DEAD)) {
1543 			break;
1544 		}
1545 
1546 		if (
1547 		    object->paging_in_progress != 0 ||
1548 		    backing_object->paging_in_progress != 0
1549 		) {
1550 			vm_object_qcollapse(object);
1551 			break;
1552 		}
1553 
1554 		/*
1555 		 * We know that we can either collapse the backing object (if
1556 		 * the parent is the only reference to it) or (perhaps) have
1557 		 * the parent bypass the object if the parent happens to shadow
1558 		 * all the resident pages in the entire backing object.
1559 		 *
1560 		 * This is ignoring pager-backed pages such as swap pages.
1561 		 * vm_object_backing_scan fails the shadowing test in this
1562 		 * case.
1563 		 */
1564 		if (backing_object->ref_count == 1) {
1565 			/*
1566 			 * If there is exactly one reference to the backing
1567 			 * object, we can collapse it into the parent.
1568 			 */
1569 			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1570 
1571 			/*
1572 			 * Move the pager from backing_object to object.
1573 			 */
1574 			if (backing_object->type == OBJT_SWAP) {
1575 				vm_object_pip_add(backing_object, 1);
1576 
1577 				/*
1578 				 * scrap the paging_offset junk and do a
1579 				 * discrete copy.  This also removes major
1580 				 * assumptions about how the swap-pager
1581 				 * works from where it doesn't belong.  The
1582 				 * new swapper is able to optimize the
1583 				 * destroy-source case.
1584 				 */
1585 				vm_object_pip_add(object, 1);
1586 				swap_pager_copy(
1587 				    backing_object,
1588 				    object,
1589 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1590 				vm_object_pip_wakeup(object);
1591 
1592 				vm_object_pip_wakeup(backing_object);
1593 			}
1594 			/*
1595 			 * Object now shadows whatever backing_object did.
1596 			 * Note that the reference to
1597 			 * backing_object->backing_object moves from within
1598 			 * backing_object to within object.
1599 			 */
1600 			TAILQ_REMOVE(
1601 			    &object->backing_object->shadow_head,
1602 			    object,
1603 			    shadow_list
1604 			);
1605 			object->backing_object->shadow_count--;
1606 			object->backing_object->generation++;
1607 			if (backing_object->backing_object) {
1608 				TAILQ_REMOVE(
1609 				    &backing_object->backing_object->shadow_head,
1610 				    backing_object,
1611 				    shadow_list
1612 				);
1613 				backing_object->backing_object->shadow_count--;
1614 				backing_object->backing_object->generation++;
1615 			}
1616 			object->backing_object = backing_object->backing_object;
1617 			if (object->backing_object) {
1618 				TAILQ_INSERT_TAIL(
1619 				    &object->backing_object->shadow_head,
1620 				    object,
1621 				    shadow_list
1622 				);
1623 				object->backing_object->shadow_count++;
1624 				object->backing_object->generation++;
1625 			}
1626 
1627 			object->backing_object_offset +=
1628 			    backing_object->backing_object_offset;
1629 
1630 			/*
1631 			 * Discard backing_object.
1632 			 *
1633 			 * Since the backing object has no pages, no pager left,
1634 			 * and no object references within it, all that is
1635 			 * necessary is to dispose of it.
1636 			 */
1637 			KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object));
1638 			KASSERT(TAILQ_FIRST(&backing_object->memq) == NULL, ("backing_object %p somehow has left over pages during collapse!", backing_object));
1639 
1640 			mtx_lock(&vm_object_list_mtx);
1641 			TAILQ_REMOVE(
1642 			    &vm_object_list,
1643 			    backing_object,
1644 			    object_list
1645 			);
1646 			mtx_unlock(&vm_object_list_mtx);
1647 
1648 			uma_zfree(obj_zone, backing_object);
1649 
1650 			object_collapses++;
1651 		} else {
1652 			vm_object_t new_backing_object;
1653 
1654 			/*
1655 			 * If we do not entirely shadow the backing object,
1656 			 * there is nothing we can do so we give up.
1657 			 */
1658 			if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) {
1659 				break;
1660 			}
1661 
1662 			/*
1663 			 * Make the parent shadow the next object in the
1664 			 * chain.  Deallocating backing_object will not remove
1665 			 * it, since its reference count is at least 2.
1666 			 */
1667 			TAILQ_REMOVE(
1668 			    &backing_object->shadow_head,
1669 			    object,
1670 			    shadow_list
1671 			);
1672 			backing_object->shadow_count--;
1673 			backing_object->generation++;
1674 
1675 			new_backing_object = backing_object->backing_object;
1676 			if ((object->backing_object = new_backing_object) != NULL) {
1677 				vm_object_reference(new_backing_object);
1678 				TAILQ_INSERT_TAIL(
1679 				    &new_backing_object->shadow_head,
1680 				    object,
1681 				    shadow_list
1682 				);
1683 				new_backing_object->shadow_count++;
1684 				new_backing_object->generation++;
1685 				object->backing_object_offset +=
1686 					backing_object->backing_object_offset;
1687 			}
1688 
1689 			/*
1690 			 * Drop the reference count on backing_object. Since
1691 			 * its ref_count was at least 2, it will not vanish;
1692 			 * so we don't need to call vm_object_deallocate, but
1693 			 * we do anyway.
1694 			 */
1695 			vm_object_deallocate(backing_object);
1696 			object_bypasses++;
1697 		}
1698 
1699 		/*
1700 		 * Try again with this object's new backing object.
1701 		 */
1702 	}
1703 }
1704 
1705 /*
1706  *	vm_object_page_remove: [internal]
1707  *
1708  *	Removes all physical pages in the specified
1709  *	object range from the object's list of pages.
1710  *
1711  *	The object must be locked.
1712  */
1713 void
1714 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, boolean_t clean_only)
1715 {
1716 	vm_page_t p, next;
1717 	vm_pindex_t size;
1718 	int all;
1719 
1720 	if (object == NULL)
1721 		return;
1722 
1723 	mtx_lock(&Giant);
1724 	if (object->resident_page_count == 0) {
1725 		mtx_unlock(&Giant);
1726 		return;
1727 	}
1728 	all = ((end == 0) && (start == 0));
1729 
1730 	/*
1731 	 * Since physically-backed objects do not use managed pages, we can't
1732 	 * remove pages from the object (we must instead remove the page
1733 	 * references, and then destroy the object).
1734 	 */
1735 	KASSERT(object->type != OBJT_PHYS, ("attempt to remove pages from a physical object"));
1736 
1737 	vm_object_pip_add(object, 1);
1738 again:
1739 	size = end - start;
1740 	if (all || size > object->resident_page_count / 4) {
1741 		for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1742 			next = TAILQ_NEXT(p, listq);
1743 			if (all || ((start <= p->pindex) && (p->pindex < end))) {
1744 				if (p->wire_count != 0) {
1745 					vm_page_protect(p, VM_PROT_NONE);
1746 					if (!clean_only)
1747 						p->valid = 0;
1748 					continue;
1749 				}
1750 
1751 				/*
1752 				 * The busy flags are only cleared at
1753 				 * interrupt -- minimize the spl transitions
1754 				 */
1755  				if (vm_page_sleep_busy(p, TRUE, "vmopar"))
1756  					goto again;
1757 
1758 				if (clean_only && p->valid) {
1759 					vm_page_test_dirty(p);
1760 					if (p->valid & p->dirty)
1761 						continue;
1762 				}
1763 				vm_page_lock_queues();
1764 				vm_page_busy(p);
1765 				vm_page_protect(p, VM_PROT_NONE);
1766 				vm_page_free(p);
1767 				vm_page_unlock_queues();
1768 			}
1769 		}
1770 	} else {
1771 		while (size > 0) {
1772 			if ((p = vm_page_lookup(object, start)) != 0) {
1773 
1774 				if (p->wire_count != 0) {
1775 					vm_page_protect(p, VM_PROT_NONE);
1776 					if (!clean_only)
1777 						p->valid = 0;
1778 					start += 1;
1779 					size -= 1;
1780 					continue;
1781 				}
1782 
1783 				/*
1784 				 * The busy flags are only cleared at
1785 				 * interrupt -- minimize the spl transitions
1786 				 */
1787  				if (vm_page_sleep_busy(p, TRUE, "vmopar"))
1788 					goto again;
1789 
1790 				if (clean_only && p->valid) {
1791 					vm_page_test_dirty(p);
1792 					if (p->valid & p->dirty) {
1793 						start += 1;
1794 						size -= 1;
1795 						continue;
1796 					}
1797 				}
1798 				vm_page_lock_queues();
1799 				vm_page_busy(p);
1800 				vm_page_protect(p, VM_PROT_NONE);
1801 				vm_page_free(p);
1802 				vm_page_unlock_queues();
1803 			}
1804 			start += 1;
1805 			size -= 1;
1806 		}
1807 	}
1808 	vm_object_pip_wakeup(object);
1809 	mtx_unlock(&Giant);
1810 }
1811 
1812 /*
1813  *	Routine:	vm_object_coalesce
1814  *	Function:	Coalesces two objects backing up adjoining
1815  *			regions of memory into a single object.
1816  *
1817  *	returns TRUE if objects were combined.
1818  *
1819  *	NOTE:	Only works at the moment if the second object is NULL -
1820  *		if it's not, which object do we lock first?
1821  *
1822  *	Parameters:
1823  *		prev_object	First object to coalesce
1824  *		prev_offset	Offset into prev_object
1825  *		next_object	Second object into coalesce
1826  *		next_offset	Offset into next_object
1827  *
1828  *		prev_size	Size of reference to prev_object
1829  *		next_size	Size of reference to next_object
1830  *
1831  *	Conditions:
1832  *	The object must *not* be locked.
1833  */
1834 boolean_t
1835 vm_object_coalesce(vm_object_t prev_object, vm_pindex_t prev_pindex,
1836 	vm_size_t prev_size, vm_size_t next_size)
1837 {
1838 	vm_pindex_t next_pindex;
1839 
1840 	if (prev_object == NULL)
1841 		return (TRUE);
1842 	mtx_lock(&Giant);
1843 	if (prev_object->type != OBJT_DEFAULT &&
1844 	    prev_object->type != OBJT_SWAP) {
1845 		mtx_unlock(&Giant);
1846 		return (FALSE);
1847 	}
1848 
1849 	/*
1850 	 * Try to collapse the object first
1851 	 */
1852 	vm_object_collapse(prev_object);
1853 
1854 	/*
1855 	 * Can't coalesce if: . more than one reference . paged out . shadows
1856 	 * another object . has a copy elsewhere (any of which mean that the
1857 	 * pages not mapped to prev_entry may be in use anyway)
1858 	 */
1859 	if (prev_object->backing_object != NULL) {
1860 		mtx_unlock(&Giant);
1861 		return (FALSE);
1862 	}
1863 
1864 	prev_size >>= PAGE_SHIFT;
1865 	next_size >>= PAGE_SHIFT;
1866 	next_pindex = prev_pindex + prev_size;
1867 
1868 	if ((prev_object->ref_count > 1) &&
1869 	    (prev_object->size != next_pindex)) {
1870 		mtx_unlock(&Giant);
1871 		return (FALSE);
1872 	}
1873 
1874 	/*
1875 	 * Remove any pages that may still be in the object from a previous
1876 	 * deallocation.
1877 	 */
1878 	if (next_pindex < prev_object->size) {
1879 		vm_object_page_remove(prev_object,
1880 				      next_pindex,
1881 				      next_pindex + next_size, FALSE);
1882 		if (prev_object->type == OBJT_SWAP)
1883 			swap_pager_freespace(prev_object,
1884 					     next_pindex, next_size);
1885 	}
1886 
1887 	/*
1888 	 * Extend the object if necessary.
1889 	 */
1890 	if (next_pindex + next_size > prev_object->size)
1891 		prev_object->size = next_pindex + next_size;
1892 
1893 	mtx_unlock(&Giant);
1894 	return (TRUE);
1895 }
1896 
1897 void
1898 vm_object_set_writeable_dirty(vm_object_t object)
1899 {
1900 	struct vnode *vp;
1901 
1902 	vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
1903 	if (object->type == OBJT_VNODE &&
1904 	    (vp = (struct vnode *)object->handle) != NULL) {
1905 		if ((vp->v_flag & VOBJDIRTY) == 0) {
1906 			mtx_lock(&vp->v_interlock);
1907 			vp->v_flag |= VOBJDIRTY;
1908 			mtx_unlock(&vp->v_interlock);
1909 		}
1910 	}
1911 }
1912 
1913 #ifdef ENABLE_VFS_IOOPT
1914 /*
1915  * Experimental support for zero-copy I/O
1916  *
1917  * Performs the copy_on_write operations necessary to allow the virtual copies
1918  * into user space to work.  This has to be called for write(2) system calls
1919  * from other processes, file unlinking, and file size shrinkage.
1920  */
1921 void
1922 vm_freeze_copyopts(vm_object_t object, vm_pindex_t froma, vm_pindex_t toa)
1923 {
1924 	int rv;
1925 	vm_object_t robject;
1926 	vm_pindex_t idx;
1927 
1928 	GIANT_REQUIRED;
1929 	if ((object == NULL) ||
1930 		((object->flags & OBJ_OPT) == 0))
1931 		return;
1932 
1933 	if (object->shadow_count > object->ref_count)
1934 		panic("vm_freeze_copyopts: sc > rc");
1935 
1936 	while ((robject = TAILQ_FIRST(&object->shadow_head)) != NULL) {
1937 		vm_pindex_t bo_pindex;
1938 		vm_page_t m_in, m_out;
1939 
1940 		bo_pindex = OFF_TO_IDX(robject->backing_object_offset);
1941 
1942 		vm_object_reference(robject);
1943 
1944 		vm_object_pip_wait(robject, "objfrz");
1945 
1946 		if (robject->ref_count == 1) {
1947 			vm_object_deallocate(robject);
1948 			continue;
1949 		}
1950 
1951 		vm_object_pip_add(robject, 1);
1952 
1953 		for (idx = 0; idx < robject->size; idx++) {
1954 
1955 			m_out = vm_page_grab(robject, idx,
1956 						VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1957 
1958 			if (m_out->valid == 0) {
1959 				m_in = vm_page_grab(object, bo_pindex + idx,
1960 						VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1961 				if (m_in->valid == 0) {
1962 					rv = vm_pager_get_pages(object, &m_in, 1, 0);
1963 					if (rv != VM_PAGER_OK) {
1964 						printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex);
1965 						continue;
1966 					}
1967 					vm_page_deactivate(m_in);
1968 				}
1969 
1970 				vm_page_protect(m_in, VM_PROT_NONE);
1971 				pmap_copy_page(m_in, m_out);
1972 				m_out->valid = m_in->valid;
1973 				vm_page_dirty(m_out);
1974 				vm_page_activate(m_out);
1975 				vm_page_wakeup(m_in);
1976 			}
1977 			vm_page_wakeup(m_out);
1978 		}
1979 
1980 		object->shadow_count--;
1981 		object->ref_count--;
1982 		TAILQ_REMOVE(&object->shadow_head, robject, shadow_list);
1983 		robject->backing_object = NULL;
1984 		robject->backing_object_offset = 0;
1985 
1986 		vm_object_pip_wakeup(robject);
1987 		vm_object_deallocate(robject);
1988 	}
1989 
1990 	vm_object_clear_flag(object, OBJ_OPT);
1991 }
1992 #endif
1993 
1994 #include "opt_ddb.h"
1995 #ifdef DDB
1996 #include <sys/kernel.h>
1997 
1998 #include <sys/cons.h>
1999 
2000 #include <ddb/ddb.h>
2001 
2002 static int
2003 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2004 {
2005 	vm_map_t tmpm;
2006 	vm_map_entry_t tmpe;
2007 	vm_object_t obj;
2008 	int entcount;
2009 
2010 	if (map == 0)
2011 		return 0;
2012 
2013 	if (entry == 0) {
2014 		tmpe = map->header.next;
2015 		entcount = map->nentries;
2016 		while (entcount-- && (tmpe != &map->header)) {
2017 			if (_vm_object_in_map(map, object, tmpe)) {
2018 				return 1;
2019 			}
2020 			tmpe = tmpe->next;
2021 		}
2022 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2023 		tmpm = entry->object.sub_map;
2024 		tmpe = tmpm->header.next;
2025 		entcount = tmpm->nentries;
2026 		while (entcount-- && tmpe != &tmpm->header) {
2027 			if (_vm_object_in_map(tmpm, object, tmpe)) {
2028 				return 1;
2029 			}
2030 			tmpe = tmpe->next;
2031 		}
2032 	} else if ((obj = entry->object.vm_object) != NULL) {
2033 		for (; obj; obj = obj->backing_object)
2034 			if (obj == object) {
2035 				return 1;
2036 			}
2037 	}
2038 	return 0;
2039 }
2040 
2041 static int
2042 vm_object_in_map(vm_object_t object)
2043 {
2044 	struct proc *p;
2045 
2046 	/* sx_slock(&allproc_lock); */
2047 	LIST_FOREACH(p, &allproc, p_list) {
2048 		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2049 			continue;
2050 		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
2051 			/* sx_sunlock(&allproc_lock); */
2052 			return 1;
2053 		}
2054 	}
2055 	/* sx_sunlock(&allproc_lock); */
2056 	if (_vm_object_in_map(kernel_map, object, 0))
2057 		return 1;
2058 	if (_vm_object_in_map(kmem_map, object, 0))
2059 		return 1;
2060 	if (_vm_object_in_map(pager_map, object, 0))
2061 		return 1;
2062 	if (_vm_object_in_map(buffer_map, object, 0))
2063 		return 1;
2064 	return 0;
2065 }
2066 
2067 DB_SHOW_COMMAND(vmochk, vm_object_check)
2068 {
2069 	vm_object_t object;
2070 
2071 	/*
2072 	 * make sure that internal objs are in a map somewhere
2073 	 * and none have zero ref counts.
2074 	 */
2075 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
2076 		if (object->handle == NULL &&
2077 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2078 			if (object->ref_count == 0) {
2079 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
2080 					(long)object->size);
2081 			}
2082 			if (!vm_object_in_map(object)) {
2083 				db_printf(
2084 			"vmochk: internal obj is not in a map: "
2085 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2086 				    object->ref_count, (u_long)object->size,
2087 				    (u_long)object->size,
2088 				    (void *)object->backing_object);
2089 			}
2090 		}
2091 	}
2092 }
2093 
2094 /*
2095  *	vm_object_print:	[ debug ]
2096  */
2097 DB_SHOW_COMMAND(object, vm_object_print_static)
2098 {
2099 	/* XXX convert args. */
2100 	vm_object_t object = (vm_object_t)addr;
2101 	boolean_t full = have_addr;
2102 
2103 	vm_page_t p;
2104 
2105 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
2106 #define	count	was_count
2107 
2108 	int count;
2109 
2110 	if (object == NULL)
2111 		return;
2112 
2113 	db_iprintf(
2114 	    "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
2115 	    object, (int)object->type, (u_long)object->size,
2116 	    object->resident_page_count, object->ref_count, object->flags);
2117 	/*
2118 	 * XXX no %qd in kernel.  Truncate object->backing_object_offset.
2119 	 */
2120 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
2121 	    object->shadow_count,
2122 	    object->backing_object ? object->backing_object->ref_count : 0,
2123 	    object->backing_object, (long)object->backing_object_offset);
2124 
2125 	if (!full)
2126 		return;
2127 
2128 	db_indent += 2;
2129 	count = 0;
2130 	TAILQ_FOREACH(p, &object->memq, listq) {
2131 		if (count == 0)
2132 			db_iprintf("memory:=");
2133 		else if (count == 6) {
2134 			db_printf("\n");
2135 			db_iprintf(" ...");
2136 			count = 0;
2137 		} else
2138 			db_printf(",");
2139 		count++;
2140 
2141 		db_printf("(off=0x%lx,page=0x%lx)",
2142 		    (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
2143 	}
2144 	if (count != 0)
2145 		db_printf("\n");
2146 	db_indent -= 2;
2147 }
2148 
2149 /* XXX. */
2150 #undef count
2151 
2152 /* XXX need this non-static entry for calling from vm_map_print. */
2153 void
2154 vm_object_print(
2155         /* db_expr_t */ long addr,
2156 	boolean_t have_addr,
2157 	/* db_expr_t */ long count,
2158 	char *modif)
2159 {
2160 	vm_object_print_static(addr, have_addr, count, modif);
2161 }
2162 
2163 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2164 {
2165 	vm_object_t object;
2166 	int nl = 0;
2167 	int c;
2168 
2169 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
2170 		vm_pindex_t idx, fidx;
2171 		vm_pindex_t osize;
2172 		vm_offset_t pa = -1, padiff;
2173 		int rcount;
2174 		vm_page_t m;
2175 
2176 		db_printf("new object: %p\n", (void *)object);
2177 		if (nl > 18) {
2178 			c = cngetc();
2179 			if (c != ' ')
2180 				return;
2181 			nl = 0;
2182 		}
2183 		nl++;
2184 		rcount = 0;
2185 		fidx = 0;
2186 		osize = object->size;
2187 		if (osize > 128)
2188 			osize = 128;
2189 		for (idx = 0; idx < osize; idx++) {
2190 			m = vm_page_lookup(object, idx);
2191 			if (m == NULL) {
2192 				if (rcount) {
2193 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2194 						(long)fidx, rcount, (long)pa);
2195 					if (nl > 18) {
2196 						c = cngetc();
2197 						if (c != ' ')
2198 							return;
2199 						nl = 0;
2200 					}
2201 					nl++;
2202 					rcount = 0;
2203 				}
2204 				continue;
2205 			}
2206 
2207 
2208 			if (rcount &&
2209 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2210 				++rcount;
2211 				continue;
2212 			}
2213 			if (rcount) {
2214 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
2215 				padiff >>= PAGE_SHIFT;
2216 				padiff &= PQ_L2_MASK;
2217 				if (padiff == 0) {
2218 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
2219 					++rcount;
2220 					continue;
2221 				}
2222 				db_printf(" index(%ld)run(%d)pa(0x%lx)",
2223 					(long)fidx, rcount, (long)pa);
2224 				db_printf("pd(%ld)\n", (long)padiff);
2225 				if (nl > 18) {
2226 					c = cngetc();
2227 					if (c != ' ')
2228 						return;
2229 					nl = 0;
2230 				}
2231 				nl++;
2232 			}
2233 			fidx = idx;
2234 			pa = VM_PAGE_TO_PHYS(m);
2235 			rcount = 1;
2236 		}
2237 		if (rcount) {
2238 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2239 				(long)fidx, rcount, (long)pa);
2240 			if (nl > 18) {
2241 				c = cngetc();
2242 				if (c != ' ')
2243 					return;
2244 				nl = 0;
2245 			}
2246 			nl++;
2247 		}
2248 	}
2249 }
2250 #endif /* DDB */
2251