xref: /freebsd/sys/vm/vm_object.c (revision a3e8fd0b7f663db7eafff527d5c3ca3bcfa8a537)
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->root = NULL;
199 	object->type = type;
200 	object->size = size;
201 	object->ref_count = 1;
202 	object->flags = 0;
203 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
204 		vm_object_set_flag(object, OBJ_ONEMAPPING);
205 	if (size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
206 		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
207 	else
208 		incr = size;
209 	do
210 		object->pg_color = next_index;
211 	while (!atomic_cmpset_int(&next_index, object->pg_color,
212 				  (object->pg_color + incr) & PQ_L2_MASK));
213 	object->handle = NULL;
214 	object->backing_object = NULL;
215 	object->backing_object_offset = (vm_ooffset_t) 0;
216 	/*
217 	 * Try to generate a number that will spread objects out in the
218 	 * hash table.  We 'wipe' new objects across the hash in 128 page
219 	 * increments plus 1 more to offset it a little more by the time
220 	 * it wraps around.
221 	 */
222 	do {
223 		exp = object_hash_rand;
224 		object->hash_rand = exp - 129;
225 	} while (!atomic_cmpset_int(&object_hash_rand, exp, object->hash_rand));
226 
227 	object->generation++;		/* atomicity needed? XXX */
228 
229 	mtx_lock(&vm_object_list_mtx);
230 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
231 	mtx_unlock(&vm_object_list_mtx);
232 }
233 
234 /*
235  *	vm_object_init:
236  *
237  *	Initialize the VM objects module.
238  */
239 void
240 vm_object_init(void)
241 {
242 	TAILQ_INIT(&vm_object_list);
243 	mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF);
244 
245 	kernel_object = &kernel_object_store;
246 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
247 	    kernel_object);
248 
249 	kmem_object = &kmem_object_store;
250 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
251 	    kmem_object);
252 	obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL,
253 #ifdef INVARIANTS
254 	    vm_object_zdtor,
255 #else
256 	    NULL,
257 #endif
258 	    vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
259 	uma_prealloc(obj_zone, VM_OBJECTS_INIT);
260 }
261 
262 void
263 vm_object_init2(void)
264 {
265 }
266 
267 void
268 vm_object_set_flag(vm_object_t object, u_short bits)
269 {
270 	object->flags |= bits;
271 }
272 
273 void
274 vm_object_clear_flag(vm_object_t object, u_short bits)
275 {
276 	GIANT_REQUIRED;
277 	object->flags &= ~bits;
278 }
279 
280 void
281 vm_object_pip_add(vm_object_t object, short i)
282 {
283 	GIANT_REQUIRED;
284 	object->paging_in_progress += i;
285 }
286 
287 void
288 vm_object_pip_subtract(vm_object_t object, short i)
289 {
290 	GIANT_REQUIRED;
291 	object->paging_in_progress -= i;
292 }
293 
294 void
295 vm_object_pip_wakeup(vm_object_t object)
296 {
297 	GIANT_REQUIRED;
298 	object->paging_in_progress--;
299 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
300 		vm_object_clear_flag(object, OBJ_PIPWNT);
301 		wakeup(object);
302 	}
303 }
304 
305 void
306 vm_object_pip_wakeupn(vm_object_t object, short i)
307 {
308 	GIANT_REQUIRED;
309 	if (i)
310 		object->paging_in_progress -= i;
311 	if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) {
312 		vm_object_clear_flag(object, OBJ_PIPWNT);
313 		wakeup(object);
314 	}
315 }
316 
317 void
318 vm_object_pip_sleep(vm_object_t object, char *waitid)
319 {
320 	GIANT_REQUIRED;
321 	if (object->paging_in_progress) {
322 		int s = splvm();
323 		if (object->paging_in_progress) {
324 			vm_object_set_flag(object, OBJ_PIPWNT);
325 			tsleep(object, PVM, waitid, 0);
326 		}
327 		splx(s);
328 	}
329 }
330 
331 void
332 vm_object_pip_wait(vm_object_t object, char *waitid)
333 {
334 	GIANT_REQUIRED;
335 	while (object->paging_in_progress)
336 		vm_object_pip_sleep(object, waitid);
337 }
338 
339 /*
340  *	vm_object_allocate_wait
341  *
342  *	Return a new object with the given size, and give the user the
343  *	option of waiting for it to complete or failing if the needed
344  *	memory isn't available.
345  */
346 vm_object_t
347 vm_object_allocate_wait(objtype_t type, vm_pindex_t size, int flags)
348 {
349 	vm_object_t result;
350 
351 	result = (vm_object_t) uma_zalloc(obj_zone, flags);
352 
353 	if (result != NULL)
354 		_vm_object_allocate(type, size, result);
355 
356 	return (result);
357 }
358 
359 /*
360  *	vm_object_allocate:
361  *
362  *	Returns a new object with the given size.
363  */
364 vm_object_t
365 vm_object_allocate(objtype_t type, vm_pindex_t size)
366 {
367 	return(vm_object_allocate_wait(type, size, M_WAITOK));
368 }
369 
370 
371 /*
372  *	vm_object_reference:
373  *
374  *	Gets another reference to the given object.
375  */
376 void
377 vm_object_reference(vm_object_t object)
378 {
379 	if (object == NULL)
380 		return;
381 
382 	vm_object_lock(object);
383 #if 0
384 	/* object can be re-referenced during final cleaning */
385 	KASSERT(!(object->flags & OBJ_DEAD),
386 	    ("vm_object_reference: attempting to reference dead obj"));
387 #endif
388 
389 	object->ref_count++;
390 	if (object->type == OBJT_VNODE) {
391 		while (vget((struct vnode *) object->handle, LK_RETRY, curthread)) {
392 			printf("vm_object_reference: delay in getting object\n");
393 		}
394 	}
395 	vm_object_unlock(object);
396 }
397 
398 /*
399  * handle deallocating a object of type OBJT_VNODE
400  */
401 void
402 vm_object_vndeallocate(vm_object_t object)
403 {
404 	struct vnode *vp = (struct vnode *) object->handle;
405 
406 	GIANT_REQUIRED;
407 	KASSERT(object->type == OBJT_VNODE,
408 	    ("vm_object_vndeallocate: not a vnode object"));
409 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
410 #ifdef INVARIANTS
411 	if (object->ref_count == 0) {
412 		vprint("vm_object_vndeallocate", vp);
413 		panic("vm_object_vndeallocate: bad object reference count");
414 	}
415 #endif
416 
417 	object->ref_count--;
418 	if (object->ref_count == 0) {
419 		mp_fixme("Unlocked vflag access.");
420 		vp->v_vflag &= ~VV_TEXT;
421 #ifdef ENABLE_VFS_IOOPT
422 		vm_object_clear_flag(object, OBJ_OPT);
423 #endif
424 	}
425 	/*
426 	 * vrele may need a vop lock
427 	 */
428 	vrele(vp);
429 }
430 
431 /*
432  *	vm_object_deallocate:
433  *
434  *	Release a reference to the specified object,
435  *	gained either through a vm_object_allocate
436  *	or a vm_object_reference call.  When all references
437  *	are gone, storage associated with this object
438  *	may be relinquished.
439  *
440  *	No object may be locked.
441  */
442 void
443 vm_object_deallocate(vm_object_t object)
444 {
445 	vm_object_t temp;
446 
447 	mtx_lock(&Giant);
448 	while (object != NULL) {
449 
450 		if (object->type == OBJT_VNODE) {
451 			vm_object_vndeallocate(object);
452 			mtx_unlock(&Giant);
453 			return;
454 		}
455 
456 		KASSERT(object->ref_count != 0,
457 			("vm_object_deallocate: object deallocated too many times: %d", object->type));
458 
459 		/*
460 		 * If the reference count goes to 0 we start calling
461 		 * vm_object_terminate() on the object chain.
462 		 * A ref count of 1 may be a special case depending on the
463 		 * shadow count being 0 or 1.
464 		 */
465 		object->ref_count--;
466 		if (object->ref_count > 1) {
467 			mtx_unlock(&Giant);
468 			return;
469 		} else if (object->ref_count == 1) {
470 			if (object->shadow_count == 0) {
471 				vm_object_set_flag(object, OBJ_ONEMAPPING);
472 			} else if ((object->shadow_count == 1) &&
473 			    (object->handle == NULL) &&
474 			    (object->type == OBJT_DEFAULT ||
475 			     object->type == OBJT_SWAP)) {
476 				vm_object_t robject;
477 
478 				robject = TAILQ_FIRST(&object->shadow_head);
479 				KASSERT(robject != NULL,
480 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
481 					 object->ref_count,
482 					 object->shadow_count));
483 				if ((robject->handle == NULL) &&
484 				    (robject->type == OBJT_DEFAULT ||
485 				     robject->type == OBJT_SWAP)) {
486 
487 					robject->ref_count++;
488 
489 					while (
490 						robject->paging_in_progress ||
491 						object->paging_in_progress
492 					) {
493 						vm_object_pip_sleep(robject, "objde1");
494 						vm_object_pip_sleep(object, "objde2");
495 					}
496 
497 					if (robject->ref_count == 1) {
498 						robject->ref_count--;
499 						object = robject;
500 						goto doterm;
501 					}
502 
503 					object = robject;
504 					vm_object_collapse(object);
505 					continue;
506 				}
507 			}
508 			mtx_unlock(&Giant);
509 			return;
510 		}
511 doterm:
512 		temp = object->backing_object;
513 		if (temp) {
514 			TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
515 			temp->shadow_count--;
516 #ifdef ENABLE_VFS_IOOPT
517 			if (temp->ref_count == 0)
518 				vm_object_clear_flag(temp, OBJ_OPT);
519 #endif
520 			temp->generation++;
521 			object->backing_object = NULL;
522 		}
523 		/*
524 		 * Don't double-terminate, we could be in a termination
525 		 * recursion due to the terminate having to sync data
526 		 * to disk.
527 		 */
528 		if ((object->flags & OBJ_DEAD) == 0)
529 			vm_object_terminate(object);
530 		object = temp;
531 	}
532 	mtx_unlock(&Giant);
533 }
534 
535 /*
536  *	vm_object_terminate actually destroys the specified object, freeing
537  *	up all previously used resources.
538  *
539  *	The object must be locked.
540  *	This routine may block.
541  */
542 void
543 vm_object_terminate(vm_object_t object)
544 {
545 	vm_page_t p;
546 	int s;
547 
548 	GIANT_REQUIRED;
549 
550 	/*
551 	 * Make sure no one uses us.
552 	 */
553 	vm_object_set_flag(object, OBJ_DEAD);
554 
555 	/*
556 	 * wait for the pageout daemon to be done with the object
557 	 */
558 	vm_object_pip_wait(object, "objtrm");
559 
560 	KASSERT(!object->paging_in_progress,
561 		("vm_object_terminate: pageout in progress"));
562 
563 	/*
564 	 * Clean and free the pages, as appropriate. All references to the
565 	 * object are gone, so we don't need to lock it.
566 	 */
567 	if (object->type == OBJT_VNODE) {
568 		struct vnode *vp;
569 
570 #ifdef ENABLE_VFS_IOOPT
571 		/*
572 		 * Freeze optimized copies.
573 		 */
574 		vm_freeze_copyopts(object, 0, object->size);
575 #endif
576 		/*
577 		 * Clean pages and flush buffers.
578 		 */
579 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
580 
581 		vp = (struct vnode *) object->handle;
582 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
583 	}
584 
585 	KASSERT(object->ref_count == 0,
586 		("vm_object_terminate: object with references, ref_count=%d",
587 		object->ref_count));
588 
589 	/*
590 	 * Now free any remaining pages. For internal objects, this also
591 	 * removes them from paging queues. Don't free wired pages, just
592 	 * remove them from the object.
593 	 */
594 	s = splvm();
595 	vm_page_lock_queues();
596 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
597 		KASSERT(!p->busy && (p->flags & PG_BUSY) == 0,
598 			("vm_object_terminate: freeing busy page %p "
599 			"p->busy = %d, p->flags %x\n", p, p->busy, p->flags));
600 		if (p->wire_count == 0) {
601 			vm_page_busy(p);
602 			vm_page_free(p);
603 			cnt.v_pfree++;
604 		} else {
605 			vm_page_busy(p);
606 			vm_page_remove(p);
607 		}
608 	}
609 	vm_page_unlock_queues();
610 	splx(s);
611 
612 	/*
613 	 * Let the pager know object is dead.
614 	 */
615 	vm_pager_deallocate(object);
616 
617 	/*
618 	 * Remove the object from the global object list.
619 	 */
620 	mtx_lock(&vm_object_list_mtx);
621 	TAILQ_REMOVE(&vm_object_list, object, object_list);
622 	mtx_unlock(&vm_object_list_mtx);
623 
624 	wakeup(object);
625 
626 	/*
627 	 * Free the space for the object.
628 	 */
629 	uma_zfree(obj_zone, object);
630 }
631 
632 /*
633  *	vm_object_page_clean
634  *
635  *	Clean all dirty pages in the specified range of object.  Leaves page
636  * 	on whatever queue it is currently on.   If NOSYNC is set then do not
637  *	write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
638  *	leaving the object dirty.
639  *
640  *	Odd semantics: if start == end, we clean everything.
641  *
642  *	The object must be locked.
643  */
644 void
645 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags)
646 {
647 	vm_page_t p, np;
648 	vm_pindex_t tstart, tend;
649 	vm_pindex_t pi;
650 	struct vnode *vp;
651 	int clearobjflags;
652 	int pagerflags;
653 	int curgeneration;
654 
655 	GIANT_REQUIRED;
656 
657 	if (object->type != OBJT_VNODE ||
658 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
659 		return;
660 
661 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0;
662 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
663 
664 	vp = object->handle;
665 
666 	vm_object_set_flag(object, OBJ_CLEANING);
667 
668 	tstart = start;
669 	if (end == 0) {
670 		tend = object->size;
671 	} else {
672 		tend = end;
673 	}
674 
675 	/*
676 	 * If the caller is smart and only msync()s a range he knows is
677 	 * dirty, we may be able to avoid an object scan.  This results in
678 	 * a phenominal improvement in performance.  We cannot do this
679 	 * as a matter of course because the object may be huge - e.g.
680 	 * the size might be in the gigabytes or terrabytes.
681 	 */
682 	if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) {
683 		vm_pindex_t tscan;
684 		int scanlimit;
685 		int scanreset;
686 
687 		scanreset = object->resident_page_count / EASY_SCAN_FACTOR;
688 		if (scanreset < 16)
689 			scanreset = 16;
690 
691 		scanlimit = scanreset;
692 		tscan = tstart;
693 		while (tscan < tend) {
694 			curgeneration = object->generation;
695 			p = vm_page_lookup(object, tscan);
696 			if (p == NULL || p->valid == 0 ||
697 			    (p->queue - p->pc) == PQ_CACHE) {
698 				if (--scanlimit == 0)
699 					break;
700 				++tscan;
701 				continue;
702 			}
703 			vm_page_test_dirty(p);
704 			if ((p->dirty & p->valid) == 0) {
705 				if (--scanlimit == 0)
706 					break;
707 				++tscan;
708 				continue;
709 			}
710 			/*
711 			 * If we have been asked to skip nosync pages and
712 			 * this is a nosync page, we can't continue.
713 			 */
714 			if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
715 				if (--scanlimit == 0)
716 					break;
717 				++tscan;
718 				continue;
719 			}
720 			scanlimit = scanreset;
721 
722 			/*
723 			 * This returns 0 if it was unable to busy the first
724 			 * page (i.e. had to sleep).
725 			 */
726 			tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags);
727 		}
728 
729 		/*
730 		 * If everything was dirty and we flushed it successfully,
731 		 * and the requested range is not the entire object, we
732 		 * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can
733 		 * return immediately.
734 		 */
735 		if (tscan >= tend && (tstart || tend < object->size)) {
736 			vm_object_clear_flag(object, OBJ_CLEANING);
737 			return;
738 		}
739 	}
740 
741 	/*
742 	 * Generally set CLEANCHK interlock and make the page read-only so
743 	 * we can then clear the object flags.
744 	 *
745 	 * However, if this is a nosync mmap then the object is likely to
746 	 * stay dirty so do not mess with the page and do not clear the
747 	 * object flags.
748 	 */
749 	clearobjflags = 1;
750 
751 	TAILQ_FOREACH(p, &object->memq, listq) {
752 		vm_page_flag_set(p, PG_CLEANCHK);
753 		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC))
754 			clearobjflags = 0;
755 		else
756 			vm_page_protect(p, VM_PROT_READ);
757 	}
758 
759 	if (clearobjflags && (tstart == 0) && (tend == object->size)) {
760 		struct vnode *vp;
761 
762 		vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
763 		if (object->type == OBJT_VNODE &&
764 		    (vp = (struct vnode *)object->handle) != NULL) {
765 			VI_LOCK(vp);
766 			if (vp->v_iflag & VI_OBJDIRTY)
767 				vp->v_iflag &= ~VI_OBJDIRTY;
768 			VI_UNLOCK(vp);
769 		}
770 	}
771 
772 rescan:
773 	curgeneration = object->generation;
774 
775 	for (p = TAILQ_FIRST(&object->memq); p; p = np) {
776 		int n;
777 
778 		np = TAILQ_NEXT(p, listq);
779 
780 again:
781 		pi = p->pindex;
782 		if (((p->flags & PG_CLEANCHK) == 0) ||
783 			(pi < tstart) || (pi >= tend) ||
784 			(p->valid == 0) ||
785 			((p->queue - p->pc) == PQ_CACHE)) {
786 			vm_page_flag_clear(p, PG_CLEANCHK);
787 			continue;
788 		}
789 
790 		vm_page_test_dirty(p);
791 		if ((p->dirty & p->valid) == 0) {
792 			vm_page_flag_clear(p, PG_CLEANCHK);
793 			continue;
794 		}
795 
796 		/*
797 		 * If we have been asked to skip nosync pages and this is a
798 		 * nosync page, skip it.  Note that the object flags were
799 		 * not cleared in this case so we do not have to set them.
800 		 */
801 		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
802 			vm_page_flag_clear(p, PG_CLEANCHK);
803 			continue;
804 		}
805 
806 		n = vm_object_page_collect_flush(object, p,
807 			curgeneration, pagerflags);
808 		if (n == 0)
809 			goto rescan;
810 
811 		if (object->generation != curgeneration)
812 			goto rescan;
813 
814 		/*
815 		 * Try to optimize the next page.  If we can't we pick up
816 		 * our (random) scan where we left off.
817 		 */
818 		if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) {
819 			if ((p = vm_page_lookup(object, pi + n)) != NULL)
820 				goto again;
821 		}
822 	}
823 
824 #if 0
825 	VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
826 #endif
827 
828 	vm_object_clear_flag(object, OBJ_CLEANING);
829 	return;
830 }
831 
832 static int
833 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags)
834 {
835 	int runlen;
836 	int s;
837 	int maxf;
838 	int chkb;
839 	int maxb;
840 	int i;
841 	vm_pindex_t pi;
842 	vm_page_t maf[vm_pageout_page_count];
843 	vm_page_t mab[vm_pageout_page_count];
844 	vm_page_t ma[vm_pageout_page_count];
845 
846 	s = splvm();
847 	pi = p->pindex;
848 	while (vm_page_sleep_busy(p, TRUE, "vpcwai")) {
849 		if (object->generation != curgeneration) {
850 			splx(s);
851 			return(0);
852 		}
853 	}
854 	vm_page_lock_queues();
855 	maxf = 0;
856 	for(i = 1; i < vm_pageout_page_count; i++) {
857 		vm_page_t tp;
858 
859 		if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
860 			if ((tp->flags & PG_BUSY) ||
861 				(tp->flags & PG_CLEANCHK) == 0 ||
862 				(tp->busy != 0))
863 				break;
864 			if((tp->queue - tp->pc) == PQ_CACHE) {
865 				vm_page_flag_clear(tp, PG_CLEANCHK);
866 				break;
867 			}
868 			vm_page_test_dirty(tp);
869 			if ((tp->dirty & tp->valid) == 0) {
870 				vm_page_flag_clear(tp, PG_CLEANCHK);
871 				break;
872 			}
873 			maf[ i - 1 ] = tp;
874 			maxf++;
875 			continue;
876 		}
877 		break;
878 	}
879 
880 	maxb = 0;
881 	chkb = vm_pageout_page_count -  maxf;
882 	if (chkb) {
883 		for(i = 1; i < chkb;i++) {
884 			vm_page_t tp;
885 
886 			if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
887 				if ((tp->flags & PG_BUSY) ||
888 					(tp->flags & PG_CLEANCHK) == 0 ||
889 					(tp->busy != 0))
890 					break;
891 				if ((tp->queue - tp->pc) == PQ_CACHE) {
892 					vm_page_flag_clear(tp, PG_CLEANCHK);
893 					break;
894 				}
895 				vm_page_test_dirty(tp);
896 				if ((tp->dirty & tp->valid) == 0) {
897 					vm_page_flag_clear(tp, PG_CLEANCHK);
898 					break;
899 				}
900 				mab[ i - 1 ] = tp;
901 				maxb++;
902 				continue;
903 			}
904 			break;
905 		}
906 	}
907 
908 	for(i = 0; i < maxb; i++) {
909 		int index = (maxb - i) - 1;
910 		ma[index] = mab[i];
911 		vm_page_flag_clear(ma[index], PG_CLEANCHK);
912 	}
913 	vm_page_flag_clear(p, PG_CLEANCHK);
914 	ma[maxb] = p;
915 	for(i = 0; i < maxf; i++) {
916 		int index = (maxb + i) + 1;
917 		ma[index] = maf[i];
918 		vm_page_flag_clear(ma[index], PG_CLEANCHK);
919 	}
920 	runlen = maxb + maxf + 1;
921 
922 	splx(s);
923 	vm_pageout_flush(ma, runlen, pagerflags);
924 	for (i = 0; i < runlen; i++) {
925 		if (ma[i]->valid & ma[i]->dirty) {
926 			vm_page_protect(ma[i], VM_PROT_READ);
927 			vm_page_flag_set(ma[i], PG_CLEANCHK);
928 
929 			/*
930 			 * maxf will end up being the actual number of pages
931 			 * we wrote out contiguously, non-inclusive of the
932 			 * first page.  We do not count look-behind pages.
933 			 */
934 			if (i >= maxb + 1 && (maxf > i - maxb - 1))
935 				maxf = i - maxb - 1;
936 		}
937 	}
938 	vm_page_unlock_queues();
939 	return(maxf + 1);
940 }
941 
942 #ifdef ENABLE_VFS_IOOPT
943 /*
944  * Same as vm_object_pmap_copy, except range checking really
945  * works, and is meant for small sections of an object.
946  *
947  * This code protects resident pages by making them read-only
948  * and is typically called on a fork or split when a page
949  * is converted to copy-on-write.
950  *
951  * NOTE: If the page is already at VM_PROT_NONE, calling
952  * vm_page_protect will have no effect.
953  */
954 void
955 vm_object_pmap_copy_1(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
956 {
957 	vm_pindex_t idx;
958 	vm_page_t p;
959 
960 	GIANT_REQUIRED;
961 
962 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
963 		return;
964 
965 	for (idx = start; idx < end; idx++) {
966 		p = vm_page_lookup(object, idx);
967 		if (p == NULL)
968 			continue;
969 		vm_page_protect(p, VM_PROT_READ);
970 	}
971 }
972 #endif
973 
974 /*
975  *	vm_object_pmap_remove:
976  *
977  *	Removes all physical pages in the specified
978  *	object range from all physical maps.
979  *
980  *	The object must *not* be locked.
981  */
982 void
983 vm_object_pmap_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
984 {
985 	vm_page_t p;
986 
987 	GIANT_REQUIRED;
988 	if (object == NULL)
989 		return;
990 	TAILQ_FOREACH(p, &object->memq, listq) {
991 		if (p->pindex >= start && p->pindex < end)
992 			vm_page_protect(p, VM_PROT_NONE);
993 	}
994 	if ((start == 0) && (object->size == end))
995 		vm_object_clear_flag(object, OBJ_WRITEABLE);
996 }
997 
998 /*
999  *	vm_object_madvise:
1000  *
1001  *	Implements the madvise function at the object/page level.
1002  *
1003  *	MADV_WILLNEED	(any object)
1004  *
1005  *	    Activate the specified pages if they are resident.
1006  *
1007  *	MADV_DONTNEED	(any object)
1008  *
1009  *	    Deactivate the specified pages if they are resident.
1010  *
1011  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
1012  *			 OBJ_ONEMAPPING only)
1013  *
1014  *	    Deactivate and clean the specified pages if they are
1015  *	    resident.  This permits the process to reuse the pages
1016  *	    without faulting or the kernel to reclaim the pages
1017  *	    without I/O.
1018  */
1019 void
1020 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
1021 {
1022 	vm_pindex_t end, tpindex;
1023 	vm_object_t tobject;
1024 	vm_page_t m;
1025 
1026 	if (object == NULL)
1027 		return;
1028 
1029 	vm_object_lock(object);
1030 
1031 	end = pindex + count;
1032 
1033 	/*
1034 	 * Locate and adjust resident pages
1035 	 */
1036 	for (; pindex < end; pindex += 1) {
1037 relookup:
1038 		tobject = object;
1039 		tpindex = pindex;
1040 shadowlookup:
1041 		/*
1042 		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
1043 		 * and those pages must be OBJ_ONEMAPPING.
1044 		 */
1045 		if (advise == MADV_FREE) {
1046 			if ((tobject->type != OBJT_DEFAULT &&
1047 			     tobject->type != OBJT_SWAP) ||
1048 			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
1049 				continue;
1050 			}
1051 		}
1052 
1053 		m = vm_page_lookup(tobject, tpindex);
1054 
1055 		if (m == NULL) {
1056 			/*
1057 			 * There may be swap even if there is no backing page
1058 			 */
1059 			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1060 				swap_pager_freespace(tobject, tpindex, 1);
1061 
1062 			/*
1063 			 * next object
1064 			 */
1065 			tobject = tobject->backing_object;
1066 			if (tobject == NULL)
1067 				continue;
1068 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
1069 			goto shadowlookup;
1070 		}
1071 
1072 		/*
1073 		 * If the page is busy or not in a normal active state,
1074 		 * we skip it.  If the page is not managed there are no
1075 		 * page queues to mess with.  Things can break if we mess
1076 		 * with pages in any of the below states.
1077 		 */
1078 		vm_page_lock_queues();
1079 		if (m->hold_count ||
1080 		    m->wire_count ||
1081 		    (m->flags & PG_UNMANAGED) ||
1082 		    m->valid != VM_PAGE_BITS_ALL) {
1083 			vm_page_unlock_queues();
1084 			continue;
1085 		}
1086  		if (vm_page_sleep_if_busy(m, TRUE, "madvpo"))
1087   			goto relookup;
1088 		if (advise == MADV_WILLNEED) {
1089 			vm_page_activate(m);
1090 		} else if (advise == MADV_DONTNEED) {
1091 			vm_page_dontneed(m);
1092 		} else if (advise == MADV_FREE) {
1093 			/*
1094 			 * Mark the page clean.  This will allow the page
1095 			 * to be freed up by the system.  However, such pages
1096 			 * are often reused quickly by malloc()/free()
1097 			 * so we do not do anything that would cause
1098 			 * a page fault if we can help it.
1099 			 *
1100 			 * Specifically, we do not try to actually free
1101 			 * the page now nor do we try to put it in the
1102 			 * cache (which would cause a page fault on reuse).
1103 			 *
1104 			 * But we do make the page is freeable as we
1105 			 * can without actually taking the step of unmapping
1106 			 * it.
1107 			 */
1108 			pmap_clear_modify(m);
1109 			m->dirty = 0;
1110 			m->act_count = 0;
1111 			vm_page_dontneed(m);
1112 		}
1113 		vm_page_unlock_queues();
1114 		if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
1115 			swap_pager_freespace(tobject, tpindex, 1);
1116 	}
1117 	vm_object_unlock(object);
1118 }
1119 
1120 /*
1121  *	vm_object_shadow:
1122  *
1123  *	Create a new object which is backed by the
1124  *	specified existing object range.  The source
1125  *	object reference is deallocated.
1126  *
1127  *	The new object and offset into that object
1128  *	are returned in the source parameters.
1129  */
1130 void
1131 vm_object_shadow(
1132 	vm_object_t *object,	/* IN/OUT */
1133 	vm_ooffset_t *offset,	/* IN/OUT */
1134 	vm_size_t length)
1135 {
1136 	vm_object_t source;
1137 	vm_object_t result;
1138 
1139 	source = *object;
1140 
1141 	vm_object_lock(source);
1142 	/*
1143 	 * Don't create the new object if the old object isn't shared.
1144 	 */
1145 	if (source != NULL &&
1146 	    source->ref_count == 1 &&
1147 	    source->handle == NULL &&
1148 	    (source->type == OBJT_DEFAULT ||
1149 	     source->type == OBJT_SWAP)) {
1150 		vm_object_unlock(source);
1151 		return;
1152 	}
1153 
1154 	/*
1155 	 * Allocate a new object with the given length
1156 	 */
1157 	result = vm_object_allocate(OBJT_DEFAULT, length);
1158 	KASSERT(result != NULL, ("vm_object_shadow: no object for shadowing"));
1159 
1160 	/*
1161 	 * The new object shadows the source object, adding a reference to it.
1162 	 * Our caller changes his reference to point to the new object,
1163 	 * removing a reference to the source object.  Net result: no change
1164 	 * of reference count.
1165 	 *
1166 	 * Try to optimize the result object's page color when shadowing
1167 	 * in order to maintain page coloring consistency in the combined
1168 	 * shadowed object.
1169 	 */
1170 	result->backing_object = source;
1171 	if (source) {
1172 		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
1173 		source->shadow_count++;
1174 		source->generation++;
1175 		if (length < source->size)
1176 			length = source->size;
1177 		if (length > PQ_L2_SIZE / 3 + PQ_PRIME1 ||
1178 		    source->generation > 1)
1179 			length = PQ_L2_SIZE / 3 + PQ_PRIME1;
1180 		result->pg_color = (source->pg_color +
1181 		    length * source->generation) & PQ_L2_MASK;
1182 		next_index = (result->pg_color + PQ_L2_SIZE / 3 + PQ_PRIME1) &
1183 		    PQ_L2_MASK;
1184 	}
1185 
1186 	/*
1187 	 * Store the offset into the source object, and fix up the offset into
1188 	 * the new object.
1189 	 */
1190 	result->backing_object_offset = *offset;
1191 
1192 	/*
1193 	 * Return the new things
1194 	 */
1195 	*offset = 0;
1196 	*object = result;
1197 
1198 	vm_object_unlock(source);
1199 }
1200 
1201 /*
1202  *	vm_object_split:
1203  *
1204  * Split the pages in a map entry into a new object.  This affords
1205  * easier removal of unused pages, and keeps object inheritance from
1206  * being a negative impact on memory usage.
1207  */
1208 void
1209 vm_object_split(vm_map_entry_t entry)
1210 {
1211 	vm_page_t m;
1212 	vm_object_t orig_object, new_object, source;
1213 	vm_offset_t s, e;
1214 	vm_pindex_t offidxstart, offidxend;
1215 	vm_size_t idx, size;
1216 	vm_ooffset_t offset;
1217 
1218 	GIANT_REQUIRED;
1219 
1220 	orig_object = entry->object.vm_object;
1221 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
1222 		return;
1223 	if (orig_object->ref_count <= 1)
1224 		return;
1225 
1226 	offset = entry->offset;
1227 	s = entry->start;
1228 	e = entry->end;
1229 
1230 	offidxstart = OFF_TO_IDX(offset);
1231 	offidxend = offidxstart + OFF_TO_IDX(e - s);
1232 	size = offidxend - offidxstart;
1233 
1234 	new_object = vm_pager_allocate(orig_object->type,
1235 		NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL);
1236 	if (new_object == NULL)
1237 		return;
1238 
1239 	source = orig_object->backing_object;
1240 	if (source != NULL) {
1241 		vm_object_reference(source);	/* Referenced by new_object */
1242 		TAILQ_INSERT_TAIL(&source->shadow_head,
1243 				  new_object, shadow_list);
1244 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
1245 		new_object->backing_object_offset =
1246 			orig_object->backing_object_offset + offset;
1247 		new_object->backing_object = source;
1248 		source->shadow_count++;
1249 		source->generation++;
1250 	}
1251 	for (idx = 0; idx < size; idx++) {
1252 	retry:
1253 		m = vm_page_lookup(orig_object, offidxstart + idx);
1254 		if (m == NULL)
1255 			continue;
1256 
1257 		/*
1258 		 * We must wait for pending I/O to complete before we can
1259 		 * rename the page.
1260 		 *
1261 		 * We do not have to VM_PROT_NONE the page as mappings should
1262 		 * not be changed by this operation.
1263 		 */
1264 		vm_page_lock_queues();
1265 		if (vm_page_sleep_if_busy(m, TRUE, "spltwt"))
1266 			goto retry;
1267 
1268 		vm_page_busy(m);
1269 		vm_page_unlock_queues();
1270 		vm_page_rename(m, new_object, idx);
1271 		/* page automatically made dirty by rename and cache handled */
1272 		vm_page_busy(m);
1273 	}
1274 	if (orig_object->type == OBJT_SWAP) {
1275 		vm_object_pip_add(orig_object, 1);
1276 		/*
1277 		 * copy orig_object pages into new_object
1278 		 * and destroy unneeded pages in
1279 		 * shadow object.
1280 		 */
1281 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
1282 		vm_object_pip_wakeup(orig_object);
1283 	}
1284 	TAILQ_FOREACH(m, &new_object->memq, listq)
1285 		vm_page_wakeup(m);
1286 	entry->object.vm_object = new_object;
1287 	entry->offset = 0LL;
1288 	vm_object_deallocate(orig_object);
1289 }
1290 
1291 #define	OBSC_TEST_ALL_SHADOWED	0x0001
1292 #define	OBSC_COLLAPSE_NOWAIT	0x0002
1293 #define	OBSC_COLLAPSE_WAIT	0x0004
1294 
1295 static __inline int
1296 vm_object_backing_scan(vm_object_t object, int op)
1297 {
1298 	int s;
1299 	int r = 1;
1300 	vm_page_t p;
1301 	vm_object_t backing_object;
1302 	vm_pindex_t backing_offset_index;
1303 
1304 	s = splvm();
1305 	GIANT_REQUIRED;
1306 
1307 	backing_object = object->backing_object;
1308 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
1309 
1310 	/*
1311 	 * Initial conditions
1312 	 */
1313 	if (op & OBSC_TEST_ALL_SHADOWED) {
1314 		/*
1315 		 * We do not want to have to test for the existence of
1316 		 * swap pages in the backing object.  XXX but with the
1317 		 * new swapper this would be pretty easy to do.
1318 		 *
1319 		 * XXX what about anonymous MAP_SHARED memory that hasn't
1320 		 * been ZFOD faulted yet?  If we do not test for this, the
1321 		 * shadow test may succeed! XXX
1322 		 */
1323 		if (backing_object->type != OBJT_DEFAULT) {
1324 			splx(s);
1325 			return (0);
1326 		}
1327 	}
1328 	if (op & OBSC_COLLAPSE_WAIT) {
1329 		vm_object_set_flag(backing_object, OBJ_DEAD);
1330 	}
1331 
1332 	/*
1333 	 * Our scan
1334 	 */
1335 	p = TAILQ_FIRST(&backing_object->memq);
1336 	while (p) {
1337 		vm_page_t next = TAILQ_NEXT(p, listq);
1338 		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
1339 
1340 		if (op & OBSC_TEST_ALL_SHADOWED) {
1341 			vm_page_t pp;
1342 
1343 			/*
1344 			 * Ignore pages outside the parent object's range
1345 			 * and outside the parent object's mapping of the
1346 			 * backing object.
1347 			 *
1348 			 * note that we do not busy the backing object's
1349 			 * page.
1350 			 */
1351 			if (
1352 			    p->pindex < backing_offset_index ||
1353 			    new_pindex >= object->size
1354 			) {
1355 				p = next;
1356 				continue;
1357 			}
1358 
1359 			/*
1360 			 * See if the parent has the page or if the parent's
1361 			 * object pager has the page.  If the parent has the
1362 			 * page but the page is not valid, the parent's
1363 			 * object pager must have the page.
1364 			 *
1365 			 * If this fails, the parent does not completely shadow
1366 			 * the object and we might as well give up now.
1367 			 */
1368 
1369 			pp = vm_page_lookup(object, new_pindex);
1370 			if (
1371 			    (pp == NULL || pp->valid == 0) &&
1372 			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
1373 			) {
1374 				r = 0;
1375 				break;
1376 			}
1377 		}
1378 
1379 		/*
1380 		 * Check for busy page
1381 		 */
1382 		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1383 			vm_page_t pp;
1384 
1385 			vm_page_lock_queues();
1386 			if (op & OBSC_COLLAPSE_NOWAIT) {
1387 				if ((p->flags & PG_BUSY) ||
1388 				    !p->valid ||
1389 				    p->hold_count ||
1390 				    p->wire_count ||
1391 				    p->busy) {
1392 					vm_page_unlock_queues();
1393 					p = next;
1394 					continue;
1395 				}
1396 			} else if (op & OBSC_COLLAPSE_WAIT) {
1397 				if (vm_page_sleep_if_busy(p, TRUE, "vmocol")) {
1398 					/*
1399 					 * If we slept, anything could have
1400 					 * happened.  Since the object is
1401 					 * marked dead, the backing offset
1402 					 * should not have changed so we
1403 					 * just restart our scan.
1404 					 */
1405 					p = TAILQ_FIRST(&backing_object->memq);
1406 					continue;
1407 				}
1408 			}
1409 
1410 			/*
1411 			 * Busy the page
1412 			 */
1413 			vm_page_busy(p);
1414 			vm_page_unlock_queues();
1415 
1416 			KASSERT(
1417 			    p->object == backing_object,
1418 			    ("vm_object_qcollapse(): object mismatch")
1419 			);
1420 
1421 			/*
1422 			 * Destroy any associated swap
1423 			 */
1424 			if (backing_object->type == OBJT_SWAP) {
1425 				swap_pager_freespace(
1426 				    backing_object,
1427 				    p->pindex,
1428 				    1
1429 				);
1430 			}
1431 
1432 			if (
1433 			    p->pindex < backing_offset_index ||
1434 			    new_pindex >= object->size
1435 			) {
1436 				/*
1437 				 * Page is out of the parent object's range, we
1438 				 * can simply destroy it.
1439 				 */
1440 				vm_page_lock_queues();
1441 				vm_page_protect(p, VM_PROT_NONE);
1442 				vm_page_free(p);
1443 				vm_page_unlock_queues();
1444 				p = next;
1445 				continue;
1446 			}
1447 
1448 			pp = vm_page_lookup(object, new_pindex);
1449 			if (
1450 			    pp != NULL ||
1451 			    vm_pager_has_page(object, new_pindex, NULL, NULL)
1452 			) {
1453 				/*
1454 				 * page already exists in parent OR swap exists
1455 				 * for this location in the parent.  Destroy
1456 				 * the original page from the backing object.
1457 				 *
1458 				 * Leave the parent's page alone
1459 				 */
1460 				vm_page_lock_queues();
1461 				vm_page_protect(p, VM_PROT_NONE);
1462 				vm_page_free(p);
1463 				vm_page_unlock_queues();
1464 				p = next;
1465 				continue;
1466 			}
1467 
1468 			/*
1469 			 * Page does not exist in parent, rename the
1470 			 * page from the backing object to the main object.
1471 			 *
1472 			 * If the page was mapped to a process, it can remain
1473 			 * mapped through the rename.
1474 			 */
1475 			vm_page_rename(p, object, new_pindex);
1476 			/* page automatically made dirty by rename */
1477 		}
1478 		p = next;
1479 	}
1480 	splx(s);
1481 	return (r);
1482 }
1483 
1484 
1485 /*
1486  * this version of collapse allows the operation to occur earlier and
1487  * when paging_in_progress is true for an object...  This is not a complete
1488  * operation, but should plug 99.9% of the rest of the leaks.
1489  */
1490 static void
1491 vm_object_qcollapse(vm_object_t object)
1492 {
1493 	vm_object_t backing_object = object->backing_object;
1494 
1495 	GIANT_REQUIRED;
1496 
1497 	if (backing_object->ref_count != 1)
1498 		return;
1499 
1500 	backing_object->ref_count += 2;
1501 
1502 	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1503 
1504 	backing_object->ref_count -= 2;
1505 }
1506 
1507 /*
1508  *	vm_object_collapse:
1509  *
1510  *	Collapse an object with the object backing it.
1511  *	Pages in the backing object are moved into the
1512  *	parent, and the backing object is deallocated.
1513  */
1514 void
1515 vm_object_collapse(vm_object_t object)
1516 {
1517 	GIANT_REQUIRED;
1518 
1519 	while (TRUE) {
1520 		vm_object_t backing_object;
1521 
1522 		/*
1523 		 * Verify that the conditions are right for collapse:
1524 		 *
1525 		 * The object exists and the backing object exists.
1526 		 */
1527 		if (object == NULL)
1528 			break;
1529 
1530 		if ((backing_object = object->backing_object) == NULL)
1531 			break;
1532 
1533 		/*
1534 		 * we check the backing object first, because it is most likely
1535 		 * not collapsable.
1536 		 */
1537 		if (backing_object->handle != NULL ||
1538 		    (backing_object->type != OBJT_DEFAULT &&
1539 		     backing_object->type != OBJT_SWAP) ||
1540 		    (backing_object->flags & OBJ_DEAD) ||
1541 		    object->handle != NULL ||
1542 		    (object->type != OBJT_DEFAULT &&
1543 		     object->type != OBJT_SWAP) ||
1544 		    (object->flags & OBJ_DEAD)) {
1545 			break;
1546 		}
1547 
1548 		if (
1549 		    object->paging_in_progress != 0 ||
1550 		    backing_object->paging_in_progress != 0
1551 		) {
1552 			vm_object_qcollapse(object);
1553 			break;
1554 		}
1555 
1556 		/*
1557 		 * We know that we can either collapse the backing object (if
1558 		 * the parent is the only reference to it) or (perhaps) have
1559 		 * the parent bypass the object if the parent happens to shadow
1560 		 * all the resident pages in the entire backing object.
1561 		 *
1562 		 * This is ignoring pager-backed pages such as swap pages.
1563 		 * vm_object_backing_scan fails the shadowing test in this
1564 		 * case.
1565 		 */
1566 		if (backing_object->ref_count == 1) {
1567 			/*
1568 			 * If there is exactly one reference to the backing
1569 			 * object, we can collapse it into the parent.
1570 			 */
1571 			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1572 
1573 			/*
1574 			 * Move the pager from backing_object to object.
1575 			 */
1576 			if (backing_object->type == OBJT_SWAP) {
1577 				vm_object_pip_add(backing_object, 1);
1578 
1579 				/*
1580 				 * scrap the paging_offset junk and do a
1581 				 * discrete copy.  This also removes major
1582 				 * assumptions about how the swap-pager
1583 				 * works from where it doesn't belong.  The
1584 				 * new swapper is able to optimize the
1585 				 * destroy-source case.
1586 				 */
1587 				vm_object_pip_add(object, 1);
1588 				swap_pager_copy(
1589 				    backing_object,
1590 				    object,
1591 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1592 				vm_object_pip_wakeup(object);
1593 
1594 				vm_object_pip_wakeup(backing_object);
1595 			}
1596 			/*
1597 			 * Object now shadows whatever backing_object did.
1598 			 * Note that the reference to
1599 			 * backing_object->backing_object moves from within
1600 			 * backing_object to within object.
1601 			 */
1602 			TAILQ_REMOVE(
1603 			    &object->backing_object->shadow_head,
1604 			    object,
1605 			    shadow_list
1606 			);
1607 			object->backing_object->shadow_count--;
1608 			object->backing_object->generation++;
1609 			if (backing_object->backing_object) {
1610 				TAILQ_REMOVE(
1611 				    &backing_object->backing_object->shadow_head,
1612 				    backing_object,
1613 				    shadow_list
1614 				);
1615 				backing_object->backing_object->shadow_count--;
1616 				backing_object->backing_object->generation++;
1617 			}
1618 			object->backing_object = backing_object->backing_object;
1619 			if (object->backing_object) {
1620 				TAILQ_INSERT_TAIL(
1621 				    &object->backing_object->shadow_head,
1622 				    object,
1623 				    shadow_list
1624 				);
1625 				object->backing_object->shadow_count++;
1626 				object->backing_object->generation++;
1627 			}
1628 
1629 			object->backing_object_offset +=
1630 			    backing_object->backing_object_offset;
1631 
1632 			/*
1633 			 * Discard backing_object.
1634 			 *
1635 			 * Since the backing object has no pages, no pager left,
1636 			 * and no object references within it, all that is
1637 			 * necessary is to dispose of it.
1638 			 */
1639 			KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object));
1640 			KASSERT(TAILQ_FIRST(&backing_object->memq) == NULL, ("backing_object %p somehow has left over pages during collapse!", backing_object));
1641 
1642 			mtx_lock(&vm_object_list_mtx);
1643 			TAILQ_REMOVE(
1644 			    &vm_object_list,
1645 			    backing_object,
1646 			    object_list
1647 			);
1648 			mtx_unlock(&vm_object_list_mtx);
1649 
1650 			uma_zfree(obj_zone, backing_object);
1651 
1652 			object_collapses++;
1653 		} else {
1654 			vm_object_t new_backing_object;
1655 
1656 			/*
1657 			 * If we do not entirely shadow the backing object,
1658 			 * there is nothing we can do so we give up.
1659 			 */
1660 			if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) {
1661 				break;
1662 			}
1663 
1664 			/*
1665 			 * Make the parent shadow the next object in the
1666 			 * chain.  Deallocating backing_object will not remove
1667 			 * it, since its reference count is at least 2.
1668 			 */
1669 			TAILQ_REMOVE(
1670 			    &backing_object->shadow_head,
1671 			    object,
1672 			    shadow_list
1673 			);
1674 			backing_object->shadow_count--;
1675 			backing_object->generation++;
1676 
1677 			new_backing_object = backing_object->backing_object;
1678 			if ((object->backing_object = new_backing_object) != NULL) {
1679 				vm_object_reference(new_backing_object);
1680 				TAILQ_INSERT_TAIL(
1681 				    &new_backing_object->shadow_head,
1682 				    object,
1683 				    shadow_list
1684 				);
1685 				new_backing_object->shadow_count++;
1686 				new_backing_object->generation++;
1687 				object->backing_object_offset +=
1688 					backing_object->backing_object_offset;
1689 			}
1690 
1691 			/*
1692 			 * Drop the reference count on backing_object. Since
1693 			 * its ref_count was at least 2, it will not vanish;
1694 			 * so we don't need to call vm_object_deallocate, but
1695 			 * we do anyway.
1696 			 */
1697 			vm_object_deallocate(backing_object);
1698 			object_bypasses++;
1699 		}
1700 
1701 		/*
1702 		 * Try again with this object's new backing object.
1703 		 */
1704 	}
1705 }
1706 
1707 /*
1708  *	vm_object_page_remove: [internal]
1709  *
1710  *	Removes all physical pages in the specified
1711  *	object range from the object's list of pages.
1712  *
1713  *	The object must be locked.
1714  */
1715 void
1716 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, boolean_t clean_only)
1717 {
1718 	vm_page_t p, next;
1719 	vm_pindex_t size;
1720 	int all;
1721 
1722 	if (object == NULL)
1723 		return;
1724 
1725 	mtx_lock(&Giant);
1726 	if (object->resident_page_count == 0) {
1727 		mtx_unlock(&Giant);
1728 		return;
1729 	}
1730 	all = ((end == 0) && (start == 0));
1731 
1732 	/*
1733 	 * Since physically-backed objects do not use managed pages, we can't
1734 	 * remove pages from the object (we must instead remove the page
1735 	 * references, and then destroy the object).
1736 	 */
1737 	KASSERT(object->type != OBJT_PHYS, ("attempt to remove pages from a physical object"));
1738 
1739 	vm_object_pip_add(object, 1);
1740 again:
1741 	vm_page_lock_queues();
1742 	size = end - start;
1743 	if (all || size > object->resident_page_count / 4) {
1744 		for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1745 			next = TAILQ_NEXT(p, listq);
1746 			if (all || ((start <= p->pindex) && (p->pindex < end))) {
1747 				if (p->wire_count != 0) {
1748 					vm_page_protect(p, VM_PROT_NONE);
1749 					if (!clean_only)
1750 						p->valid = 0;
1751 					continue;
1752 				}
1753 
1754 				/*
1755 				 * The busy flags are only cleared at
1756 				 * interrupt -- minimize the spl transitions
1757 				 */
1758  				if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
1759  					goto again;
1760 
1761 				if (clean_only && p->valid) {
1762 					vm_page_test_dirty(p);
1763 					if (p->valid & p->dirty)
1764 						continue;
1765 				}
1766 				vm_page_busy(p);
1767 				vm_page_protect(p, VM_PROT_NONE);
1768 				vm_page_free(p);
1769 			}
1770 		}
1771 	} else {
1772 		while (size > 0) {
1773 			if ((p = vm_page_lookup(object, start)) != NULL) {
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_if_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_busy(p);
1799 				vm_page_protect(p, VM_PROT_NONE);
1800 				vm_page_free(p);
1801 			}
1802 			start += 1;
1803 			size -= 1;
1804 		}
1805 	}
1806 	vm_page_unlock_queues();
1807 	vm_object_pip_wakeup(object);
1808 	mtx_unlock(&Giant);
1809 }
1810 
1811 /*
1812  *	Routine:	vm_object_coalesce
1813  *	Function:	Coalesces two objects backing up adjoining
1814  *			regions of memory into a single object.
1815  *
1816  *	returns TRUE if objects were combined.
1817  *
1818  *	NOTE:	Only works at the moment if the second object is NULL -
1819  *		if it's not, which object do we lock first?
1820  *
1821  *	Parameters:
1822  *		prev_object	First object to coalesce
1823  *		prev_offset	Offset into prev_object
1824  *		next_object	Second object into coalesce
1825  *		next_offset	Offset into next_object
1826  *
1827  *		prev_size	Size of reference to prev_object
1828  *		next_size	Size of reference to next_object
1829  *
1830  *	Conditions:
1831  *	The object must *not* be locked.
1832  */
1833 boolean_t
1834 vm_object_coalesce(vm_object_t prev_object, vm_pindex_t prev_pindex,
1835 	vm_size_t prev_size, vm_size_t next_size)
1836 {
1837 	vm_pindex_t next_pindex;
1838 
1839 	if (prev_object == NULL)
1840 		return (TRUE);
1841 	vm_object_lock(prev_object);
1842 	if (prev_object->type != OBJT_DEFAULT &&
1843 	    prev_object->type != OBJT_SWAP) {
1844 		vm_object_unlock(prev_object);
1845 		return (FALSE);
1846 	}
1847 
1848 	/*
1849 	 * Try to collapse the object first
1850 	 */
1851 	vm_object_collapse(prev_object);
1852 
1853 	/*
1854 	 * Can't coalesce if: . more than one reference . paged out . shadows
1855 	 * another object . has a copy elsewhere (any of which mean that the
1856 	 * pages not mapped to prev_entry may be in use anyway)
1857 	 */
1858 	if (prev_object->backing_object != NULL) {
1859 		vm_object_unlock(prev_object);
1860 		return (FALSE);
1861 	}
1862 
1863 	prev_size >>= PAGE_SHIFT;
1864 	next_size >>= PAGE_SHIFT;
1865 	next_pindex = prev_pindex + prev_size;
1866 
1867 	if ((prev_object->ref_count > 1) &&
1868 	    (prev_object->size != next_pindex)) {
1869 		vm_object_unlock(prev_object);
1870 		return (FALSE);
1871 	}
1872 
1873 	/*
1874 	 * Remove any pages that may still be in the object from a previous
1875 	 * deallocation.
1876 	 */
1877 	if (next_pindex < prev_object->size) {
1878 		vm_object_page_remove(prev_object,
1879 				      next_pindex,
1880 				      next_pindex + next_size, FALSE);
1881 		if (prev_object->type == OBJT_SWAP)
1882 			swap_pager_freespace(prev_object,
1883 					     next_pindex, next_size);
1884 	}
1885 
1886 	/*
1887 	 * Extend the object if necessary.
1888 	 */
1889 	if (next_pindex + next_size > prev_object->size)
1890 		prev_object->size = next_pindex + next_size;
1891 
1892 	vm_object_unlock(prev_object);
1893 	return (TRUE);
1894 }
1895 
1896 void
1897 vm_object_set_writeable_dirty(vm_object_t object)
1898 {
1899 	struct vnode *vp;
1900 
1901 	vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
1902 	if (object->type == OBJT_VNODE &&
1903 	    (vp = (struct vnode *)object->handle) != NULL) {
1904 		VI_LOCK(vp);
1905 		if ((vp->v_iflag & VI_OBJDIRTY) == 0)
1906 			vp->v_iflag |= VI_OBJDIRTY;
1907 		VI_UNLOCK(vp);
1908 	}
1909 }
1910 
1911 #ifdef ENABLE_VFS_IOOPT
1912 /*
1913  * Experimental support for zero-copy I/O
1914  *
1915  * Performs the copy_on_write operations necessary to allow the virtual copies
1916  * into user space to work.  This has to be called for write(2) system calls
1917  * from other processes, file unlinking, and file size shrinkage.
1918  */
1919 void
1920 vm_freeze_copyopts(vm_object_t object, vm_pindex_t froma, vm_pindex_t toa)
1921 {
1922 	int rv;
1923 	vm_object_t robject;
1924 	vm_pindex_t idx;
1925 
1926 	GIANT_REQUIRED;
1927 	if ((object == NULL) ||
1928 		((object->flags & OBJ_OPT) == 0))
1929 		return;
1930 
1931 	if (object->shadow_count > object->ref_count)
1932 		panic("vm_freeze_copyopts: sc > rc");
1933 
1934 	while ((robject = TAILQ_FIRST(&object->shadow_head)) != NULL) {
1935 		vm_pindex_t bo_pindex;
1936 		vm_page_t m_in, m_out;
1937 
1938 		bo_pindex = OFF_TO_IDX(robject->backing_object_offset);
1939 
1940 		vm_object_reference(robject);
1941 
1942 		vm_object_pip_wait(robject, "objfrz");
1943 
1944 		if (robject->ref_count == 1) {
1945 			vm_object_deallocate(robject);
1946 			continue;
1947 		}
1948 
1949 		vm_object_pip_add(robject, 1);
1950 
1951 		for (idx = 0; idx < robject->size; idx++) {
1952 
1953 			m_out = vm_page_grab(robject, idx,
1954 						VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1955 
1956 			if (m_out->valid == 0) {
1957 				m_in = vm_page_grab(object, bo_pindex + idx,
1958 						VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1959 				if (m_in->valid == 0) {
1960 					rv = vm_pager_get_pages(object, &m_in, 1, 0);
1961 					if (rv != VM_PAGER_OK) {
1962 						printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex);
1963 						continue;
1964 					}
1965 					vm_page_lock_queues();
1966 					vm_page_deactivate(m_in);
1967 					vm_page_unlock_queues();
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_lock_queues();
1975 				vm_page_activate(m_out);
1976 				vm_page_unlock_queues();
1977 				vm_page_wakeup(m_in);
1978 			}
1979 			vm_page_wakeup(m_out);
1980 		}
1981 
1982 		object->shadow_count--;
1983 		object->ref_count--;
1984 		TAILQ_REMOVE(&object->shadow_head, robject, shadow_list);
1985 		robject->backing_object = NULL;
1986 		robject->backing_object_offset = 0;
1987 
1988 		vm_object_pip_wakeup(robject);
1989 		vm_object_deallocate(robject);
1990 	}
1991 
1992 	vm_object_clear_flag(object, OBJ_OPT);
1993 }
1994 #endif
1995 
1996 #include "opt_ddb.h"
1997 #ifdef DDB
1998 #include <sys/kernel.h>
1999 
2000 #include <sys/cons.h>
2001 
2002 #include <ddb/ddb.h>
2003 
2004 static int
2005 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
2006 {
2007 	vm_map_t tmpm;
2008 	vm_map_entry_t tmpe;
2009 	vm_object_t obj;
2010 	int entcount;
2011 
2012 	if (map == 0)
2013 		return 0;
2014 
2015 	if (entry == 0) {
2016 		tmpe = map->header.next;
2017 		entcount = map->nentries;
2018 		while (entcount-- && (tmpe != &map->header)) {
2019 			if (_vm_object_in_map(map, object, tmpe)) {
2020 				return 1;
2021 			}
2022 			tmpe = tmpe->next;
2023 		}
2024 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2025 		tmpm = entry->object.sub_map;
2026 		tmpe = tmpm->header.next;
2027 		entcount = tmpm->nentries;
2028 		while (entcount-- && tmpe != &tmpm->header) {
2029 			if (_vm_object_in_map(tmpm, object, tmpe)) {
2030 				return 1;
2031 			}
2032 			tmpe = tmpe->next;
2033 		}
2034 	} else if ((obj = entry->object.vm_object) != NULL) {
2035 		for (; obj; obj = obj->backing_object)
2036 			if (obj == object) {
2037 				return 1;
2038 			}
2039 	}
2040 	return 0;
2041 }
2042 
2043 static int
2044 vm_object_in_map(vm_object_t object)
2045 {
2046 	struct proc *p;
2047 
2048 	/* sx_slock(&allproc_lock); */
2049 	LIST_FOREACH(p, &allproc, p_list) {
2050 		if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
2051 			continue;
2052 		if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
2053 			/* sx_sunlock(&allproc_lock); */
2054 			return 1;
2055 		}
2056 	}
2057 	/* sx_sunlock(&allproc_lock); */
2058 	if (_vm_object_in_map(kernel_map, object, 0))
2059 		return 1;
2060 	if (_vm_object_in_map(kmem_map, object, 0))
2061 		return 1;
2062 	if (_vm_object_in_map(pager_map, object, 0))
2063 		return 1;
2064 	if (_vm_object_in_map(buffer_map, object, 0))
2065 		return 1;
2066 	return 0;
2067 }
2068 
2069 DB_SHOW_COMMAND(vmochk, vm_object_check)
2070 {
2071 	vm_object_t object;
2072 
2073 	/*
2074 	 * make sure that internal objs are in a map somewhere
2075 	 * and none have zero ref counts.
2076 	 */
2077 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
2078 		if (object->handle == NULL &&
2079 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2080 			if (object->ref_count == 0) {
2081 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
2082 					(long)object->size);
2083 			}
2084 			if (!vm_object_in_map(object)) {
2085 				db_printf(
2086 			"vmochk: internal obj is not in a map: "
2087 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
2088 				    object->ref_count, (u_long)object->size,
2089 				    (u_long)object->size,
2090 				    (void *)object->backing_object);
2091 			}
2092 		}
2093 	}
2094 }
2095 
2096 /*
2097  *	vm_object_print:	[ debug ]
2098  */
2099 DB_SHOW_COMMAND(object, vm_object_print_static)
2100 {
2101 	/* XXX convert args. */
2102 	vm_object_t object = (vm_object_t)addr;
2103 	boolean_t full = have_addr;
2104 
2105 	vm_page_t p;
2106 
2107 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
2108 #define	count	was_count
2109 
2110 	int count;
2111 
2112 	if (object == NULL)
2113 		return;
2114 
2115 	db_iprintf(
2116 	    "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
2117 	    object, (int)object->type, (u_long)object->size,
2118 	    object->resident_page_count, object->ref_count, object->flags);
2119 	/*
2120 	 * XXX no %qd in kernel.  Truncate object->backing_object_offset.
2121 	 */
2122 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
2123 	    object->shadow_count,
2124 	    object->backing_object ? object->backing_object->ref_count : 0,
2125 	    object->backing_object, (long)object->backing_object_offset);
2126 
2127 	if (!full)
2128 		return;
2129 
2130 	db_indent += 2;
2131 	count = 0;
2132 	TAILQ_FOREACH(p, &object->memq, listq) {
2133 		if (count == 0)
2134 			db_iprintf("memory:=");
2135 		else if (count == 6) {
2136 			db_printf("\n");
2137 			db_iprintf(" ...");
2138 			count = 0;
2139 		} else
2140 			db_printf(",");
2141 		count++;
2142 
2143 		db_printf("(off=0x%lx,page=0x%lx)",
2144 		    (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
2145 	}
2146 	if (count != 0)
2147 		db_printf("\n");
2148 	db_indent -= 2;
2149 }
2150 
2151 /* XXX. */
2152 #undef count
2153 
2154 /* XXX need this non-static entry for calling from vm_map_print. */
2155 void
2156 vm_object_print(
2157         /* db_expr_t */ long addr,
2158 	boolean_t have_addr,
2159 	/* db_expr_t */ long count,
2160 	char *modif)
2161 {
2162 	vm_object_print_static(addr, have_addr, count, modif);
2163 }
2164 
2165 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
2166 {
2167 	vm_object_t object;
2168 	int nl = 0;
2169 	int c;
2170 
2171 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
2172 		vm_pindex_t idx, fidx;
2173 		vm_pindex_t osize;
2174 		vm_offset_t pa = -1, padiff;
2175 		int rcount;
2176 		vm_page_t m;
2177 
2178 		db_printf("new object: %p\n", (void *)object);
2179 		if (nl > 18) {
2180 			c = cngetc();
2181 			if (c != ' ')
2182 				return;
2183 			nl = 0;
2184 		}
2185 		nl++;
2186 		rcount = 0;
2187 		fidx = 0;
2188 		osize = object->size;
2189 		if (osize > 128)
2190 			osize = 128;
2191 		for (idx = 0; idx < osize; idx++) {
2192 			m = vm_page_lookup(object, idx);
2193 			if (m == NULL) {
2194 				if (rcount) {
2195 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2196 						(long)fidx, rcount, (long)pa);
2197 					if (nl > 18) {
2198 						c = cngetc();
2199 						if (c != ' ')
2200 							return;
2201 						nl = 0;
2202 					}
2203 					nl++;
2204 					rcount = 0;
2205 				}
2206 				continue;
2207 			}
2208 
2209 
2210 			if (rcount &&
2211 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
2212 				++rcount;
2213 				continue;
2214 			}
2215 			if (rcount) {
2216 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
2217 				padiff >>= PAGE_SHIFT;
2218 				padiff &= PQ_L2_MASK;
2219 				if (padiff == 0) {
2220 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
2221 					++rcount;
2222 					continue;
2223 				}
2224 				db_printf(" index(%ld)run(%d)pa(0x%lx)",
2225 					(long)fidx, rcount, (long)pa);
2226 				db_printf("pd(%ld)\n", (long)padiff);
2227 				if (nl > 18) {
2228 					c = cngetc();
2229 					if (c != ' ')
2230 						return;
2231 					nl = 0;
2232 				}
2233 				nl++;
2234 			}
2235 			fidx = idx;
2236 			pa = VM_PAGE_TO_PHYS(m);
2237 			rcount = 1;
2238 		}
2239 		if (rcount) {
2240 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
2241 				(long)fidx, rcount, (long)pa);
2242 			if (nl > 18) {
2243 				c = cngetc();
2244 				if (c != ' ')
2245 					return;
2246 				nl = 0;
2247 			}
2248 			nl++;
2249 		}
2250 	}
2251 }
2252 #endif /* DDB */
2253