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