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