xref: /illumos-gate/usr/src/uts/common/vm/vm_pvn.c (revision f8bbc5716bd2f470114db82bab03cedb9ce9d72e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a71e32b6Sstans  * Common Development and Distribution License (the "License").
6a71e32b6Sstans  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*f8bbc571SPavel Filipensky  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * VM - paged vnode.
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * This file supplies vm support for the vnode operations that deal with pages.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
467c478bd9Sstevel@tonic-gate #include <sys/param.h>
477c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
487c478bd9Sstevel@tonic-gate #include <sys/systm.h>
497c478bd9Sstevel@tonic-gate #include <sys/time.h>
507c478bd9Sstevel@tonic-gate #include <sys/buf.h>
517c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
527c478bd9Sstevel@tonic-gate #include <sys/uio.h>
537c478bd9Sstevel@tonic-gate #include <sys/vmmeter.h>
547c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
557c478bd9Sstevel@tonic-gate #include <sys/mman.h>
567c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
577c478bd9Sstevel@tonic-gate #include <sys/cred.h>
587c478bd9Sstevel@tonic-gate #include <sys/user.h>
597c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
607c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
617c478bd9Sstevel@tonic-gate #include <sys/debug.h>
627c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
637c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
647c478bd9Sstevel@tonic-gate #include <sys/tnf_probe.h>
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include <vm/hat.h>
677c478bd9Sstevel@tonic-gate #include <vm/as.h>
687c478bd9Sstevel@tonic-gate #include <vm/seg.h>
697c478bd9Sstevel@tonic-gate #include <vm/rm.h>
707c478bd9Sstevel@tonic-gate #include <vm/pvn.h>
717c478bd9Sstevel@tonic-gate #include <vm/page.h>
727c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
737c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
747c478bd9Sstevel@tonic-gate #include <sys/fs/swapnode.h>
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate int pvn_nofodklust = 0;
777c478bd9Sstevel@tonic-gate int pvn_write_noklust = 0;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate uint_t pvn_vmodsort_supported = 0;	/* set if HAT supports VMODSORT */
807c478bd9Sstevel@tonic-gate uint_t pvn_vmodsort_disable = 0;	/* set in /etc/system to disable HAT */
817c478bd9Sstevel@tonic-gate 					/* support for vmodsort for testing */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate static struct kmem_cache *marker_cache = NULL;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * Find the largest contiguous block which contains `addr' for file offset
877c478bd9Sstevel@tonic-gate  * `offset' in it while living within the file system block sizes (`vp_off'
887c478bd9Sstevel@tonic-gate  * and `vp_len') and the address space limits for which no pages currently
897c478bd9Sstevel@tonic-gate  * exist and which map to consecutive file offsets.
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate page_t *
927c478bd9Sstevel@tonic-gate pvn_read_kluster(
937c478bd9Sstevel@tonic-gate 	struct vnode *vp,
947c478bd9Sstevel@tonic-gate 	u_offset_t off,
957c478bd9Sstevel@tonic-gate 	struct seg *seg,
967c478bd9Sstevel@tonic-gate 	caddr_t addr,
977c478bd9Sstevel@tonic-gate 	u_offset_t *offp,			/* return values */
987c478bd9Sstevel@tonic-gate 	size_t *lenp,				/* return values */
997c478bd9Sstevel@tonic-gate 	u_offset_t vp_off,
1007c478bd9Sstevel@tonic-gate 	size_t vp_len,
1017c478bd9Sstevel@tonic-gate 	int isra)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	ssize_t deltaf, deltab;
1047c478bd9Sstevel@tonic-gate 	page_t *pp;
1057c478bd9Sstevel@tonic-gate 	page_t *plist = NULL;
1067c478bd9Sstevel@tonic-gate 	spgcnt_t pagesavail;
1077c478bd9Sstevel@tonic-gate 	u_offset_t vp_end;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	ASSERT(off >= vp_off && off < vp_off + vp_len);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	/*
1127c478bd9Sstevel@tonic-gate 	 * We only want to do klustering/read ahead if there
1137c478bd9Sstevel@tonic-gate 	 * is more than minfree pages currently available.
1147c478bd9Sstevel@tonic-gate 	 */
1157c478bd9Sstevel@tonic-gate 	pagesavail = freemem - minfree;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (pagesavail <= 0)
1187c478bd9Sstevel@tonic-gate 		if (isra)
1197c478bd9Sstevel@tonic-gate 			return ((page_t *)NULL);    /* ra case - give up */
1207c478bd9Sstevel@tonic-gate 		else
1217c478bd9Sstevel@tonic-gate 			pagesavail = 1;		    /* must return a page */
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	/* We calculate in pages instead of bytes due to 32-bit overflows */
1247c478bd9Sstevel@tonic-gate 	if (pagesavail < (spgcnt_t)btopr(vp_len)) {
1257c478bd9Sstevel@tonic-gate 		/*
1267c478bd9Sstevel@tonic-gate 		 * Don't have enough free memory for the
1277c478bd9Sstevel@tonic-gate 		 * max request, try sizing down vp request.
1287c478bd9Sstevel@tonic-gate 		 */
1297c478bd9Sstevel@tonic-gate 		deltab = (ssize_t)(off - vp_off);
1307c478bd9Sstevel@tonic-gate 		vp_len -= deltab;
1317c478bd9Sstevel@tonic-gate 		vp_off += deltab;
1327c478bd9Sstevel@tonic-gate 		if (pagesavail < btopr(vp_len)) {
1337c478bd9Sstevel@tonic-gate 			/*
1347c478bd9Sstevel@tonic-gate 			 * Still not enough memory, just settle for
1357c478bd9Sstevel@tonic-gate 			 * pagesavail which is at least 1.
1367c478bd9Sstevel@tonic-gate 			 */
1377c478bd9Sstevel@tonic-gate 			vp_len = ptob(pagesavail);
1387c478bd9Sstevel@tonic-gate 		}
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	vp_end = vp_off + vp_len;
1427c478bd9Sstevel@tonic-gate 	ASSERT(off >= vp_off && off < vp_end);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (isra && SEGOP_KLUSTER(seg, addr, 0))
1457c478bd9Sstevel@tonic-gate 		return ((page_t *)NULL);	/* segment driver says no */
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if ((plist = page_create_va(vp, off,
1487c478bd9Sstevel@tonic-gate 	    PAGESIZE, PG_EXCL | PG_WAIT, seg, addr)) == NULL)
1497c478bd9Sstevel@tonic-gate 		return ((page_t *)NULL);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (vp_len <= PAGESIZE || pvn_nofodklust) {
1527c478bd9Sstevel@tonic-gate 		*offp = off;
1537c478bd9Sstevel@tonic-gate 		*lenp = MIN(vp_len, PAGESIZE);
1547c478bd9Sstevel@tonic-gate 	} else {
1557c478bd9Sstevel@tonic-gate 		/*
1567c478bd9Sstevel@tonic-gate 		 * Scan back from front by incrementing "deltab" and
1577c478bd9Sstevel@tonic-gate 		 * comparing "off" with "vp_off + deltab" to avoid
1587c478bd9Sstevel@tonic-gate 		 * "signed" versus "unsigned" conversion problems.
1597c478bd9Sstevel@tonic-gate 		 */
1607c478bd9Sstevel@tonic-gate 		for (deltab = PAGESIZE; off >= vp_off + deltab;
1617c478bd9Sstevel@tonic-gate 		    deltab += PAGESIZE) {
1627c478bd9Sstevel@tonic-gate 			/*
1637c478bd9Sstevel@tonic-gate 			 * Call back to the segment driver to verify that
1647c478bd9Sstevel@tonic-gate 			 * the klustering/read ahead operation makes sense.
1657c478bd9Sstevel@tonic-gate 			 */
1667c478bd9Sstevel@tonic-gate 			if (SEGOP_KLUSTER(seg, addr, -deltab))
1677c478bd9Sstevel@tonic-gate 				break;		/* page not eligible */
1687c478bd9Sstevel@tonic-gate 			if ((pp = page_create_va(vp, off - deltab,
1697c478bd9Sstevel@tonic-gate 			    PAGESIZE, PG_EXCL, seg, addr - deltab))
1707c478bd9Sstevel@tonic-gate 			    == NULL)
1717c478bd9Sstevel@tonic-gate 				break;		/* already have the page */
1727c478bd9Sstevel@tonic-gate 			/*
1737c478bd9Sstevel@tonic-gate 			 * Add page to front of page list.
1747c478bd9Sstevel@tonic-gate 			 */
1757c478bd9Sstevel@tonic-gate 			page_add(&plist, pp);
1767c478bd9Sstevel@tonic-gate 		}
1777c478bd9Sstevel@tonic-gate 		deltab -= PAGESIZE;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 		/* scan forward from front */
1807c478bd9Sstevel@tonic-gate 		for (deltaf = PAGESIZE; off + deltaf < vp_end;
1817c478bd9Sstevel@tonic-gate 		    deltaf += PAGESIZE) {
1827c478bd9Sstevel@tonic-gate 			/*
1837c478bd9Sstevel@tonic-gate 			 * Call back to the segment driver to verify that
1847c478bd9Sstevel@tonic-gate 			 * the klustering/read ahead operation makes sense.
1857c478bd9Sstevel@tonic-gate 			 */
1867c478bd9Sstevel@tonic-gate 			if (SEGOP_KLUSTER(seg, addr, deltaf))
1877c478bd9Sstevel@tonic-gate 				break;		/* page not file extension */
1887c478bd9Sstevel@tonic-gate 			if ((pp = page_create_va(vp, off + deltaf,
1897c478bd9Sstevel@tonic-gate 			    PAGESIZE, PG_EXCL, seg, addr + deltaf))
1907c478bd9Sstevel@tonic-gate 			    == NULL)
1917c478bd9Sstevel@tonic-gate 				break;		/* already have page */
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 			/*
1947c478bd9Sstevel@tonic-gate 			 * Add page to end of page list.
1957c478bd9Sstevel@tonic-gate 			 */
1967c478bd9Sstevel@tonic-gate 			page_add(&plist, pp);
1977c478bd9Sstevel@tonic-gate 			plist = plist->p_next;
1987c478bd9Sstevel@tonic-gate 		}
1997c478bd9Sstevel@tonic-gate 		*offp = off = off - deltab;
2007c478bd9Sstevel@tonic-gate 		*lenp = deltab + deltaf;
2017c478bd9Sstevel@tonic-gate 		ASSERT(off >= vp_off);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		/*
2047c478bd9Sstevel@tonic-gate 		 * If we ended up getting more than was actually
2057c478bd9Sstevel@tonic-gate 		 * requested, retract the returned length to only
2067c478bd9Sstevel@tonic-gate 		 * reflect what was requested.  This might happen
2077c478bd9Sstevel@tonic-gate 		 * if we were allowed to kluster pages across a
2087c478bd9Sstevel@tonic-gate 		 * span of (say) 5 frags, and frag size is less
2097c478bd9Sstevel@tonic-gate 		 * than PAGESIZE.  We need a whole number of
2107c478bd9Sstevel@tonic-gate 		 * pages to contain those frags, but the returned
2117c478bd9Sstevel@tonic-gate 		 * size should only allow the returned range to
2127c478bd9Sstevel@tonic-gate 		 * extend as far as the end of the frags.
2137c478bd9Sstevel@tonic-gate 		 */
2147c478bd9Sstevel@tonic-gate 		if ((vp_off + vp_len) < (off + *lenp)) {
2157c478bd9Sstevel@tonic-gate 			ASSERT(vp_end > off);
2167c478bd9Sstevel@tonic-gate 			*lenp = vp_end - off;
2177c478bd9Sstevel@tonic-gate 		}
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_VM, TR_PVN_READ_KLUSTER,
220*f8bbc571SPavel Filipensky 	    "pvn_read_kluster:seg %p addr %x isra %x",
221*f8bbc571SPavel Filipensky 	    seg, addr, isra);
2227c478bd9Sstevel@tonic-gate 	return (plist);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate  * Handle pages for this vnode on either side of the page "pp"
2277c478bd9Sstevel@tonic-gate  * which has been locked by the caller.  This routine will also
2287c478bd9Sstevel@tonic-gate  * do klustering in the range [vp_off, vp_off + vp_len] up
2297c478bd9Sstevel@tonic-gate  * until a page which is not found.  The offset and length
2307c478bd9Sstevel@tonic-gate  * of pages included is returned in "*offp" and "*lenp".
2317c478bd9Sstevel@tonic-gate  *
2327c478bd9Sstevel@tonic-gate  * Returns a list of dirty locked pages all ready to be
2337c478bd9Sstevel@tonic-gate  * written back.
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate page_t *
2367c478bd9Sstevel@tonic-gate pvn_write_kluster(
2377c478bd9Sstevel@tonic-gate 	struct vnode *vp,
2387c478bd9Sstevel@tonic-gate 	page_t *pp,
2397c478bd9Sstevel@tonic-gate 	u_offset_t *offp,		/* return values */
2407c478bd9Sstevel@tonic-gate 	size_t *lenp,			/* return values */
2417c478bd9Sstevel@tonic-gate 	u_offset_t vp_off,
2427c478bd9Sstevel@tonic-gate 	size_t vp_len,
2437c478bd9Sstevel@tonic-gate 	int flags)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	u_offset_t off;
2467c478bd9Sstevel@tonic-gate 	page_t *dirty;
2477c478bd9Sstevel@tonic-gate 	size_t deltab, deltaf;
2487c478bd9Sstevel@tonic-gate 	se_t se;
2497c478bd9Sstevel@tonic-gate 	u_offset_t vp_end;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	off = pp->p_offset;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	/*
2547c478bd9Sstevel@tonic-gate 	 * Kustering should not be done if we are invalidating
2557c478bd9Sstevel@tonic-gate 	 * pages since we could destroy pages that belong to
2567c478bd9Sstevel@tonic-gate 	 * some other process if this is a swap vnode.
2577c478bd9Sstevel@tonic-gate 	 */
2587c478bd9Sstevel@tonic-gate 	if (pvn_write_noklust || ((flags & B_INVAL) && IS_SWAPVP(vp))) {
2597c478bd9Sstevel@tonic-gate 		*offp = off;
2607c478bd9Sstevel@tonic-gate 		*lenp = PAGESIZE;
2617c478bd9Sstevel@tonic-gate 		return (pp);
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	if (flags & (B_FREE | B_INVAL))
2657c478bd9Sstevel@tonic-gate 		se = SE_EXCL;
2667c478bd9Sstevel@tonic-gate 	else
2677c478bd9Sstevel@tonic-gate 		se = SE_SHARED;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	dirty = pp;
2707c478bd9Sstevel@tonic-gate 	/*
2717c478bd9Sstevel@tonic-gate 	 * Scan backwards looking for pages to kluster by incrementing
2727c478bd9Sstevel@tonic-gate 	 * "deltab" and comparing "off" with "vp_off + deltab" to
2737c478bd9Sstevel@tonic-gate 	 * avoid "signed" versus "unsigned" conversion problems.
2747c478bd9Sstevel@tonic-gate 	 */
2757c478bd9Sstevel@tonic-gate 	for (deltab = PAGESIZE; off >= vp_off + deltab; deltab += PAGESIZE) {
2767c478bd9Sstevel@tonic-gate 		pp = page_lookup_nowait(vp, off - deltab, se);
2777c478bd9Sstevel@tonic-gate 		if (pp == NULL)
2787c478bd9Sstevel@tonic-gate 			break;		/* page not found */
2797c478bd9Sstevel@tonic-gate 		if (pvn_getdirty(pp, flags | B_DELWRI) == 0)
2807c478bd9Sstevel@tonic-gate 			break;
2817c478bd9Sstevel@tonic-gate 		page_add(&dirty, pp);
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 	deltab -= PAGESIZE;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	vp_end = vp_off + vp_len;
2867c478bd9Sstevel@tonic-gate 	/* now scan forwards looking for pages to kluster */
2877c478bd9Sstevel@tonic-gate 	for (deltaf = PAGESIZE; off + deltaf < vp_end; deltaf += PAGESIZE) {
2887c478bd9Sstevel@tonic-gate 		pp = page_lookup_nowait(vp, off + deltaf, se);
2897c478bd9Sstevel@tonic-gate 		if (pp == NULL)
2907c478bd9Sstevel@tonic-gate 			break;		/* page not found */
2917c478bd9Sstevel@tonic-gate 		if (pvn_getdirty(pp, flags | B_DELWRI) == 0)
2927c478bd9Sstevel@tonic-gate 			break;
2937c478bd9Sstevel@tonic-gate 		page_add(&dirty, pp);
2947c478bd9Sstevel@tonic-gate 		dirty = dirty->p_next;
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	*offp = off - deltab;
2987c478bd9Sstevel@tonic-gate 	*lenp = deltab + deltaf;
2997c478bd9Sstevel@tonic-gate 	return (dirty);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate /*
3037c478bd9Sstevel@tonic-gate  * Generic entry point used to release the "shared/exclusive" lock
3047c478bd9Sstevel@tonic-gate  * and the "p_iolock" on pages after i/o is complete.
3057c478bd9Sstevel@tonic-gate  */
3067c478bd9Sstevel@tonic-gate void
3077c478bd9Sstevel@tonic-gate pvn_io_done(page_t *plist)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	page_t *pp;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	while (plist != NULL) {
3127c478bd9Sstevel@tonic-gate 		pp = plist;
3137c478bd9Sstevel@tonic-gate 		page_sub(&plist, pp);
3147c478bd9Sstevel@tonic-gate 		page_io_unlock(pp);
3157c478bd9Sstevel@tonic-gate 		page_unlock(pp);
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate /*
3207c478bd9Sstevel@tonic-gate  * Entry point to be used by file system getpage subr's and
3217c478bd9Sstevel@tonic-gate  * other such routines which either want to unlock pages (B_ASYNC
3227c478bd9Sstevel@tonic-gate  * request) or destroy a list of pages if an error occurred.
3237c478bd9Sstevel@tonic-gate  */
3247c478bd9Sstevel@tonic-gate void
3257c478bd9Sstevel@tonic-gate pvn_read_done(page_t *plist, int flags)
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate 	page_t *pp;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	while (plist != NULL) {
3307c478bd9Sstevel@tonic-gate 		pp = plist;
3317c478bd9Sstevel@tonic-gate 		page_sub(&plist, pp);
3327c478bd9Sstevel@tonic-gate 		page_io_unlock(pp);
3337c478bd9Sstevel@tonic-gate 		if (flags & B_ERROR) {
3347c478bd9Sstevel@tonic-gate 			/*LINTED: constant in conditional context*/
3357c478bd9Sstevel@tonic-gate 			VN_DISPOSE(pp, B_INVAL, 0, kcred);
3367c478bd9Sstevel@tonic-gate 		} else {
3377c478bd9Sstevel@tonic-gate 			(void) page_release(pp, 0);
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate /*
3437c478bd9Sstevel@tonic-gate  * Automagic pageout.
3447c478bd9Sstevel@tonic-gate  * When memory gets tight, start freeing pages popping out of the
3457c478bd9Sstevel@tonic-gate  * write queue.
3467c478bd9Sstevel@tonic-gate  */
3477c478bd9Sstevel@tonic-gate int	write_free = 1;
3487c478bd9Sstevel@tonic-gate pgcnt_t	pages_before_pager = 200;	/* LMXXX */
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate /*
3517c478bd9Sstevel@tonic-gate  * Routine to be called when page-out's complete.
3527c478bd9Sstevel@tonic-gate  * The caller, typically VOP_PUTPAGE, has to explicity call this routine
3537c478bd9Sstevel@tonic-gate  * after waiting for i/o to complete (biowait) to free the list of
3547c478bd9Sstevel@tonic-gate  * pages associated with the buffer.  These pages must be locked
3557c478bd9Sstevel@tonic-gate  * before i/o is initiated.
3567c478bd9Sstevel@tonic-gate  *
3577c478bd9Sstevel@tonic-gate  * If a write error occurs, the pages are marked as modified
3587c478bd9Sstevel@tonic-gate  * so the write will be re-tried later.
3597c478bd9Sstevel@tonic-gate  */
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate void
3627c478bd9Sstevel@tonic-gate pvn_write_done(page_t *plist, int flags)
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	int dfree = 0;
3657c478bd9Sstevel@tonic-gate 	int pgrec = 0;
3667c478bd9Sstevel@tonic-gate 	int pgout = 0;
3677c478bd9Sstevel@tonic-gate 	int pgpgout = 0;
3687c478bd9Sstevel@tonic-gate 	int anonpgout = 0;
3697c478bd9Sstevel@tonic-gate 	int anonfree = 0;
3707c478bd9Sstevel@tonic-gate 	int fspgout = 0;
3717c478bd9Sstevel@tonic-gate 	int fsfree = 0;
3727c478bd9Sstevel@tonic-gate 	int execpgout = 0;
3737c478bd9Sstevel@tonic-gate 	int execfree = 0;
3747c478bd9Sstevel@tonic-gate 	page_t *pp;
3757c478bd9Sstevel@tonic-gate 	struct cpu *cpup;
3767c478bd9Sstevel@tonic-gate 	struct vnode *vp = NULL;	/* for probe */
3777c478bd9Sstevel@tonic-gate 	uint_t ppattr;
378a71e32b6Sstans 	kmutex_t *vphm = NULL;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	ASSERT((flags & B_READ) == 0);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	/*
3837c478bd9Sstevel@tonic-gate 	 * If we are about to start paging anyway, start freeing pages.
3847c478bd9Sstevel@tonic-gate 	 */
3857c478bd9Sstevel@tonic-gate 	if (write_free && freemem < lotsfree + pages_before_pager &&
3867c478bd9Sstevel@tonic-gate 	    (flags & B_ERROR) == 0) {
3877c478bd9Sstevel@tonic-gate 		flags |= B_FREE;
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	/*
3917c478bd9Sstevel@tonic-gate 	 * Handle each page involved in the i/o operation.
3927c478bd9Sstevel@tonic-gate 	 */
3937c478bd9Sstevel@tonic-gate 	while (plist != NULL) {
3947c478bd9Sstevel@tonic-gate 		pp = plist;
3957c478bd9Sstevel@tonic-gate 		ASSERT(PAGE_LOCKED(pp) && page_iolock_assert(pp));
3967c478bd9Sstevel@tonic-gate 		page_sub(&plist, pp);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		/* Kernel probe support */
3997c478bd9Sstevel@tonic-gate 		if (vp == NULL)
4007c478bd9Sstevel@tonic-gate 			vp = pp->p_vnode;
4017c478bd9Sstevel@tonic-gate 
402d9615970Sqiao 		if (((flags & B_ERROR) == 0) && IS_VMODSORT(vp)) {
403a71e32b6Sstans 			/*
404a71e32b6Sstans 			 * Move page to the top of the v_page list.
405a71e32b6Sstans 			 * Skip pages modified during IO.
406a71e32b6Sstans 			 */
407a71e32b6Sstans 			vphm = page_vnode_mutex(vp);
408a71e32b6Sstans 			mutex_enter(vphm);
409a71e32b6Sstans 			if ((pp->p_vpnext != pp) && !hat_ismod(pp)) {
410a71e32b6Sstans 				page_vpsub(&vp->v_pages, pp);
411a71e32b6Sstans 				page_vpadd(&vp->v_pages, pp);
412a71e32b6Sstans 			}
413a71e32b6Sstans 			mutex_exit(vphm);
414a71e32b6Sstans 		}
415a71e32b6Sstans 
4167c478bd9Sstevel@tonic-gate 		if (flags & B_ERROR) {
4177c478bd9Sstevel@tonic-gate 			/*
4187c478bd9Sstevel@tonic-gate 			 * Write operation failed.  We don't want
4197c478bd9Sstevel@tonic-gate 			 * to destroy (or free) the page unless B_FORCE
4207c478bd9Sstevel@tonic-gate 			 * is set. We set the mod bit again and release
4217c478bd9Sstevel@tonic-gate 			 * all locks on the page so that it will get written
4227c478bd9Sstevel@tonic-gate 			 * back again later when things are hopefully
4237c478bd9Sstevel@tonic-gate 			 * better again.
4247c478bd9Sstevel@tonic-gate 			 * If B_INVAL and B_FORCE is set we really have
4257c478bd9Sstevel@tonic-gate 			 * to destroy the page.
4267c478bd9Sstevel@tonic-gate 			 */
4277c478bd9Sstevel@tonic-gate 			if ((flags & (B_INVAL|B_FORCE)) == (B_INVAL|B_FORCE)) {
4287c478bd9Sstevel@tonic-gate 				page_io_unlock(pp);
4297c478bd9Sstevel@tonic-gate 				/*LINTED: constant in conditional context*/
4307c478bd9Sstevel@tonic-gate 				VN_DISPOSE(pp, B_INVAL, 0, kcred);
4317c478bd9Sstevel@tonic-gate 			} else {
432d9615970Sqiao 				hat_setmod_only(pp);
4337c478bd9Sstevel@tonic-gate 				page_io_unlock(pp);
4347c478bd9Sstevel@tonic-gate 				page_unlock(pp);
4357c478bd9Sstevel@tonic-gate 			}
4367c478bd9Sstevel@tonic-gate 		} else if (flags & B_INVAL) {
4377c478bd9Sstevel@tonic-gate 			/*
4387c478bd9Sstevel@tonic-gate 			 * XXX - Failed writes with B_INVAL set are
4397c478bd9Sstevel@tonic-gate 			 * not handled appropriately.
4407c478bd9Sstevel@tonic-gate 			 */
4417c478bd9Sstevel@tonic-gate 			page_io_unlock(pp);
4427c478bd9Sstevel@tonic-gate 			/*LINTED: constant in conditional context*/
4437c478bd9Sstevel@tonic-gate 			VN_DISPOSE(pp, B_INVAL, 0, kcred);
4447c478bd9Sstevel@tonic-gate 		} else if (flags & B_FREE ||!hat_page_is_mapped(pp)) {
4457c478bd9Sstevel@tonic-gate 			/*
4467c478bd9Sstevel@tonic-gate 			 * Update statistics for pages being paged out
4477c478bd9Sstevel@tonic-gate 			 */
4487c478bd9Sstevel@tonic-gate 			if (pp->p_vnode) {
4497c478bd9Sstevel@tonic-gate 				if (IS_SWAPFSVP(pp->p_vnode)) {
4507c478bd9Sstevel@tonic-gate 					anonpgout++;
4517c478bd9Sstevel@tonic-gate 				} else {
4527c478bd9Sstevel@tonic-gate 					if (pp->p_vnode->v_flag & VVMEXEC) {
4537c478bd9Sstevel@tonic-gate 						execpgout++;
4547c478bd9Sstevel@tonic-gate 					} else {
4557c478bd9Sstevel@tonic-gate 						fspgout++;
4567c478bd9Sstevel@tonic-gate 					}
4577c478bd9Sstevel@tonic-gate 				}
4587c478bd9Sstevel@tonic-gate 			}
4597c478bd9Sstevel@tonic-gate 			page_io_unlock(pp);
4607c478bd9Sstevel@tonic-gate 			pgout = 1;
4617c478bd9Sstevel@tonic-gate 			pgpgout++;
4627c478bd9Sstevel@tonic-gate 			TRACE_1(TR_FAC_VM, TR_PAGE_WS_OUT,
463*f8bbc571SPavel Filipensky 			    "page_ws_out:pp %p", pp);
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 			/*
4667c478bd9Sstevel@tonic-gate 			 * The page_struct_lock need not be acquired to
4677c478bd9Sstevel@tonic-gate 			 * examine "p_lckcnt" and "p_cowcnt" since we'll
4687c478bd9Sstevel@tonic-gate 			 * have an "exclusive" lock if the upgrade succeeds.
4697c478bd9Sstevel@tonic-gate 			 */
4707c478bd9Sstevel@tonic-gate 			if (page_tryupgrade(pp) &&
4717c478bd9Sstevel@tonic-gate 			    pp->p_lckcnt == 0 && pp->p_cowcnt == 0) {
4727c478bd9Sstevel@tonic-gate 				/*
4737c478bd9Sstevel@tonic-gate 				 * Check if someone has reclaimed the
4747c478bd9Sstevel@tonic-gate 				 * page.  If ref and mod are not set, no
4757c478bd9Sstevel@tonic-gate 				 * one is using it so we can free it.
4767c478bd9Sstevel@tonic-gate 				 * The rest of the system is careful
4777c478bd9Sstevel@tonic-gate 				 * to use the NOSYNC flag to unload
4787c478bd9Sstevel@tonic-gate 				 * translations set up for i/o w/o
4797c478bd9Sstevel@tonic-gate 				 * affecting ref and mod bits.
4807c478bd9Sstevel@tonic-gate 				 *
4817c478bd9Sstevel@tonic-gate 				 * Obtain a copy of the real hardware
4827c478bd9Sstevel@tonic-gate 				 * mod bit using hat_pagesync(pp, HAT_DONTZERO)
4837c478bd9Sstevel@tonic-gate 				 * to avoid having to flush the cache.
4847c478bd9Sstevel@tonic-gate 				 */
4857c478bd9Sstevel@tonic-gate 				ppattr = hat_pagesync(pp, HAT_SYNC_DONTZERO |
486*f8bbc571SPavel Filipensky 				    HAT_SYNC_STOPON_MOD);
4877c478bd9Sstevel@tonic-gate 			ck_refmod:
4887c478bd9Sstevel@tonic-gate 				if (!(ppattr & (P_REF | P_MOD))) {
4897c478bd9Sstevel@tonic-gate 					if (hat_page_is_mapped(pp)) {
4907c478bd9Sstevel@tonic-gate 						/*
4917c478bd9Sstevel@tonic-gate 						 * Doesn't look like the page
4927c478bd9Sstevel@tonic-gate 						 * was modified so now we
4937c478bd9Sstevel@tonic-gate 						 * really have to unload the
4947c478bd9Sstevel@tonic-gate 						 * translations.  Meanwhile
4957c478bd9Sstevel@tonic-gate 						 * another CPU could've
4967c478bd9Sstevel@tonic-gate 						 * modified it so we have to
4977c478bd9Sstevel@tonic-gate 						 * check again.  We don't loop
4987c478bd9Sstevel@tonic-gate 						 * forever here because now
4997c478bd9Sstevel@tonic-gate 						 * the translations are gone
5007c478bd9Sstevel@tonic-gate 						 * and no one can get a new one
5017c478bd9Sstevel@tonic-gate 						 * since we have the "exclusive"
5027c478bd9Sstevel@tonic-gate 						 * lock on the page.
5037c478bd9Sstevel@tonic-gate 						 */
5047c478bd9Sstevel@tonic-gate 						(void) hat_pageunload(pp,
505*f8bbc571SPavel Filipensky 						    HAT_FORCE_PGUNLOAD);
5067c478bd9Sstevel@tonic-gate 						ppattr = hat_page_getattr(pp,
507*f8bbc571SPavel Filipensky 						    P_REF | P_MOD);
5087c478bd9Sstevel@tonic-gate 						goto ck_refmod;
5097c478bd9Sstevel@tonic-gate 					}
5107c478bd9Sstevel@tonic-gate 					/*
5117c478bd9Sstevel@tonic-gate 					 * Update statistics for pages being
5127c478bd9Sstevel@tonic-gate 					 * freed
5137c478bd9Sstevel@tonic-gate 					 */
5147c478bd9Sstevel@tonic-gate 					if (pp->p_vnode) {
5157c478bd9Sstevel@tonic-gate 						if (IS_SWAPFSVP(pp->p_vnode)) {
5167c478bd9Sstevel@tonic-gate 							anonfree++;
5177c478bd9Sstevel@tonic-gate 						} else {
5187c478bd9Sstevel@tonic-gate 							if (pp->p_vnode->v_flag
5197c478bd9Sstevel@tonic-gate 							    & VVMEXEC) {
5207c478bd9Sstevel@tonic-gate 								execfree++;
5217c478bd9Sstevel@tonic-gate 							} else {
5227c478bd9Sstevel@tonic-gate 								fsfree++;
5237c478bd9Sstevel@tonic-gate 							}
5247c478bd9Sstevel@tonic-gate 						}
5257c478bd9Sstevel@tonic-gate 					}
5267c478bd9Sstevel@tonic-gate 					/*LINTED: constant in conditional ctx*/
5277c478bd9Sstevel@tonic-gate 					VN_DISPOSE(pp, B_FREE,
528*f8bbc571SPavel Filipensky 					    (flags & B_DONTNEED), kcred);
5297c478bd9Sstevel@tonic-gate 					dfree++;
5307c478bd9Sstevel@tonic-gate 				} else {
5317c478bd9Sstevel@tonic-gate 					page_unlock(pp);
5327c478bd9Sstevel@tonic-gate 					pgrec++;
5337c478bd9Sstevel@tonic-gate 					TRACE_1(TR_FAC_VM, TR_PAGE_WS_FREE,
5347c478bd9Sstevel@tonic-gate 					    "page_ws_free:pp %p", pp);
5357c478bd9Sstevel@tonic-gate 				}
5367c478bd9Sstevel@tonic-gate 			} else {
5377c478bd9Sstevel@tonic-gate 				/*
5387c478bd9Sstevel@tonic-gate 				 * Page is either `locked' in memory
5397c478bd9Sstevel@tonic-gate 				 * or was reclaimed and now has a
5407c478bd9Sstevel@tonic-gate 				 * "shared" lock, so release it.
5417c478bd9Sstevel@tonic-gate 				 */
5427c478bd9Sstevel@tonic-gate 				page_unlock(pp);
5437c478bd9Sstevel@tonic-gate 			}
5447c478bd9Sstevel@tonic-gate 		} else {
5457c478bd9Sstevel@tonic-gate 			/*
5467c478bd9Sstevel@tonic-gate 			 * Neither B_FREE nor B_INVAL nor B_ERROR.
5477c478bd9Sstevel@tonic-gate 			 * Just release locks.
5487c478bd9Sstevel@tonic-gate 			 */
5497c478bd9Sstevel@tonic-gate 			page_io_unlock(pp);
5507c478bd9Sstevel@tonic-gate 			page_unlock(pp);
5517c478bd9Sstevel@tonic-gate 		}
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	CPU_STATS_ENTER_K();
5557c478bd9Sstevel@tonic-gate 	cpup = CPU;		/* get cpup now that CPU cannot change */
5567c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cpup, vm, dfree, dfree);
5577c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cpup, vm, pgrec, pgrec);
5587c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cpup, vm, pgout, pgout);
5597c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cpup, vm, pgpgout, pgpgout);
5607c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cpup, vm, anonpgout, anonpgout);
5617c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cpup, vm, anonfree, anonfree);
5627c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cpup, vm, fspgout, fspgout);
5637c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cpup, vm, fsfree, fsfree);
5647c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cpup, vm, execpgout, execpgout);
5657c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cpup, vm, execfree, execfree);
5667c478bd9Sstevel@tonic-gate 	CPU_STATS_EXIT_K();
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	/* Kernel probe */
5697c478bd9Sstevel@tonic-gate 	TNF_PROBE_4(pageout, "vm pageio io", /* CSTYLED */,
570*f8bbc571SPavel Filipensky 	    tnf_opaque,	vnode,			vp,
571*f8bbc571SPavel Filipensky 	    tnf_ulong,	pages_pageout,		pgpgout,
572*f8bbc571SPavel Filipensky 	    tnf_ulong,	pages_freed,		dfree,
573*f8bbc571SPavel Filipensky 	    tnf_ulong,	pages_reclaimed,	pgrec);
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate /*
5777c478bd9Sstevel@tonic-gate  * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED, B_DELWRI,
5787c478bd9Sstevel@tonic-gate  * B_TRUNC, B_FORCE}.  B_DELWRI indicates that this page is part of a kluster
5797c478bd9Sstevel@tonic-gate  * operation and is only to be considered if it doesn't involve any
5807c478bd9Sstevel@tonic-gate  * waiting here.  B_TRUNC indicates that the file is being truncated
5817c478bd9Sstevel@tonic-gate  * and so no i/o needs to be done. B_FORCE indicates that the page
5827c478bd9Sstevel@tonic-gate  * must be destroyed so don't try wrting it out.
5837c478bd9Sstevel@tonic-gate  *
5847c478bd9Sstevel@tonic-gate  * The caller must ensure that the page is locked.  Returns 1, if
5857c478bd9Sstevel@tonic-gate  * the page should be written back (the "iolock" is held in this
5867c478bd9Sstevel@tonic-gate  * case), or 0 if the page has been dealt with or has been
5877c478bd9Sstevel@tonic-gate  * unlocked.
5887c478bd9Sstevel@tonic-gate  */
5897c478bd9Sstevel@tonic-gate int
5907c478bd9Sstevel@tonic-gate pvn_getdirty(page_t *pp, int flags)
5917c478bd9Sstevel@tonic-gate {
5927c478bd9Sstevel@tonic-gate 	ASSERT((flags & (B_INVAL | B_FREE)) ?
5937c478bd9Sstevel@tonic-gate 	    PAGE_EXCL(pp) : PAGE_SHARED(pp));
5947c478bd9Sstevel@tonic-gate 	ASSERT(PP_ISFREE(pp) == 0);
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	/*
5977c478bd9Sstevel@tonic-gate 	 * If trying to invalidate or free a logically `locked' page,
5987c478bd9Sstevel@tonic-gate 	 * forget it.  Don't need page_struct_lock to check p_lckcnt and
5997c478bd9Sstevel@tonic-gate 	 * p_cowcnt as the page is exclusively locked.
6007c478bd9Sstevel@tonic-gate 	 */
6017c478bd9Sstevel@tonic-gate 	if ((flags & (B_INVAL | B_FREE)) && !(flags & (B_TRUNC|B_FORCE)) &&
6027c478bd9Sstevel@tonic-gate 	    (pp->p_lckcnt != 0 || pp->p_cowcnt != 0)) {
6037c478bd9Sstevel@tonic-gate 		page_unlock(pp);
6047c478bd9Sstevel@tonic-gate 		return (0);
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	/*
6087c478bd9Sstevel@tonic-gate 	 * Now acquire the i/o lock so we can add it to the dirty
6097c478bd9Sstevel@tonic-gate 	 * list (if necessary).  We avoid blocking on the i/o lock
6107c478bd9Sstevel@tonic-gate 	 * in the following cases:
6117c478bd9Sstevel@tonic-gate 	 *
6127c478bd9Sstevel@tonic-gate 	 *	If B_DELWRI is set, which implies that this request is
6137c478bd9Sstevel@tonic-gate 	 *	due to a klustering operartion.
6147c478bd9Sstevel@tonic-gate 	 *
6157c478bd9Sstevel@tonic-gate 	 *	If this is an async (B_ASYNC) operation and we are not doing
6167c478bd9Sstevel@tonic-gate 	 *	invalidation (B_INVAL) [The current i/o or fsflush will ensure
6177c478bd9Sstevel@tonic-gate 	 *	that the the page is written out].
6187c478bd9Sstevel@tonic-gate 	 */
6197c478bd9Sstevel@tonic-gate 	if ((flags & B_DELWRI) || ((flags & (B_INVAL | B_ASYNC)) == B_ASYNC)) {
6207c478bd9Sstevel@tonic-gate 		if (!page_io_trylock(pp)) {
6217c478bd9Sstevel@tonic-gate 			page_unlock(pp);
6227c478bd9Sstevel@tonic-gate 			return (0);
6237c478bd9Sstevel@tonic-gate 		}
6247c478bd9Sstevel@tonic-gate 	} else {
6257c478bd9Sstevel@tonic-gate 		page_io_lock(pp);
6267c478bd9Sstevel@tonic-gate 	}
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	/*
6297c478bd9Sstevel@tonic-gate 	 * If we want to free or invalidate the page then
6307c478bd9Sstevel@tonic-gate 	 * we need to unload it so that anyone who wants
6317c478bd9Sstevel@tonic-gate 	 * it will have to take a minor fault to get it.
6327c478bd9Sstevel@tonic-gate 	 * Otherwise, we're just writing the page back so we
6337c478bd9Sstevel@tonic-gate 	 * need to sync up the hardwre and software mod bit to
6347c478bd9Sstevel@tonic-gate 	 * detect any future modifications.  We clear the
6357c478bd9Sstevel@tonic-gate 	 * software mod bit when we put the page on the dirty
6367c478bd9Sstevel@tonic-gate 	 * list.
6377c478bd9Sstevel@tonic-gate 	 */
6387c478bd9Sstevel@tonic-gate 	if (flags & (B_INVAL | B_FREE)) {
6397c478bd9Sstevel@tonic-gate 		(void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD);
6407c478bd9Sstevel@tonic-gate 	} else {
6417c478bd9Sstevel@tonic-gate 		(void) hat_pagesync(pp, HAT_SYNC_ZERORM);
6427c478bd9Sstevel@tonic-gate 	}
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	if (!hat_ismod(pp) || (flags & B_TRUNC)) {
6457c478bd9Sstevel@tonic-gate 		/*
6467c478bd9Sstevel@tonic-gate 		 * Don't need to add it to the
6477c478bd9Sstevel@tonic-gate 		 * list after all.
6487c478bd9Sstevel@tonic-gate 		 */
6497c478bd9Sstevel@tonic-gate 		page_io_unlock(pp);
6507c478bd9Sstevel@tonic-gate 		if (flags & B_INVAL) {
6517c478bd9Sstevel@tonic-gate 			/*LINTED: constant in conditional context*/
6527c478bd9Sstevel@tonic-gate 			VN_DISPOSE(pp, B_INVAL, 0, kcred);
6537c478bd9Sstevel@tonic-gate 		} else if (flags & B_FREE) {
6547c478bd9Sstevel@tonic-gate 			/*LINTED: constant in conditional context*/
6557c478bd9Sstevel@tonic-gate 			VN_DISPOSE(pp, B_FREE, (flags & B_DONTNEED), kcred);
6567c478bd9Sstevel@tonic-gate 		} else {
6577c478bd9Sstevel@tonic-gate 			/*
6587c478bd9Sstevel@tonic-gate 			 * This is advisory path for the callers
6597c478bd9Sstevel@tonic-gate 			 * of VOP_PUTPAGE() who prefer freeing the
6607c478bd9Sstevel@tonic-gate 			 * page _only_ if no one else is accessing it.
6617c478bd9Sstevel@tonic-gate 			 * E.g. segmap_release()
6627c478bd9Sstevel@tonic-gate 			 *
6637c478bd9Sstevel@tonic-gate 			 * The above hat_ismod() check is useless because:
6647c478bd9Sstevel@tonic-gate 			 * (1) we may not be holding SE_EXCL lock;
6657c478bd9Sstevel@tonic-gate 			 * (2) we've not unloaded _all_ translations
6667c478bd9Sstevel@tonic-gate 			 *
6677c478bd9Sstevel@tonic-gate 			 * Let page_release() do the heavy-lifting.
6687c478bd9Sstevel@tonic-gate 			 */
6697c478bd9Sstevel@tonic-gate 			(void) page_release(pp, 1);
6707c478bd9Sstevel@tonic-gate 		}
6717c478bd9Sstevel@tonic-gate 		return (0);
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	/*
6757c478bd9Sstevel@tonic-gate 	 * Page is dirty, get it ready for the write back
6767c478bd9Sstevel@tonic-gate 	 * and add page to the dirty list.
6777c478bd9Sstevel@tonic-gate 	 */
6787c478bd9Sstevel@tonic-gate 	hat_clrrefmod(pp);
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	/*
6817c478bd9Sstevel@tonic-gate 	 * If we're going to free the page when we're done
6827c478bd9Sstevel@tonic-gate 	 * then we can let others try to use it starting now.
6837c478bd9Sstevel@tonic-gate 	 * We'll detect the fact that they used it when the
6847c478bd9Sstevel@tonic-gate 	 * i/o is done and avoid freeing the page.
6857c478bd9Sstevel@tonic-gate 	 */
6867c478bd9Sstevel@tonic-gate 	if (flags & B_FREE)
6877c478bd9Sstevel@tonic-gate 		page_downgrade(pp);
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_VM, TR_PVN_GETDIRTY, "pvn_getdirty:pp %p", pp);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	return (1);
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6977c478bd9Sstevel@tonic-gate static int
6987c478bd9Sstevel@tonic-gate marker_constructor(void *buf, void *cdrarg, int kmflags)
6997c478bd9Sstevel@tonic-gate {
7007c478bd9Sstevel@tonic-gate 	page_t *mark = buf;
7017c478bd9Sstevel@tonic-gate 	bzero(mark, sizeof (page_t));
702*f8bbc571SPavel Filipensky 	mark->p_hash = PVN_VPLIST_HASH_TAG;
7037c478bd9Sstevel@tonic-gate 	return (0);
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate void
7077c478bd9Sstevel@tonic-gate pvn_init()
7087c478bd9Sstevel@tonic-gate {
7097c478bd9Sstevel@tonic-gate 	if (pvn_vmodsort_disable == 0)
7107c478bd9Sstevel@tonic-gate 		pvn_vmodsort_supported = hat_supported(HAT_VMODSORT, NULL);
7117c478bd9Sstevel@tonic-gate 	marker_cache = kmem_cache_create("marker_cache",
7127c478bd9Sstevel@tonic-gate 	    sizeof (page_t), 0, marker_constructor,
7137c478bd9Sstevel@tonic-gate 	    NULL, NULL, NULL, NULL, 0);
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate /*
7187c478bd9Sstevel@tonic-gate  * Process a vnode's page list for all pages whose offset is >= off.
7197c478bd9Sstevel@tonic-gate  * Pages are to either be free'd, invalidated, or written back to disk.
7207c478bd9Sstevel@tonic-gate  *
7217c478bd9Sstevel@tonic-gate  * An "exclusive" lock is acquired for each page if B_INVAL or B_FREE
7227c478bd9Sstevel@tonic-gate  * is specified, otherwise they are "shared" locked.
7237c478bd9Sstevel@tonic-gate  *
7247c478bd9Sstevel@tonic-gate  * Flags are {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED, B_TRUNC}
7257c478bd9Sstevel@tonic-gate  *
7267c478bd9Sstevel@tonic-gate  * Special marker page_t's are inserted in the list in order
7277c478bd9Sstevel@tonic-gate  * to keep track of where we are in the list when locks are dropped.
7287c478bd9Sstevel@tonic-gate  *
7297c478bd9Sstevel@tonic-gate  * Note the list is circular and insertions can happen only at the
7307c478bd9Sstevel@tonic-gate  * head and tail of the list. The algorithm ensures visiting all pages
7317c478bd9Sstevel@tonic-gate  * on the list in the following way:
7327c478bd9Sstevel@tonic-gate  *
7337c478bd9Sstevel@tonic-gate  *    Drop two marker pages at the end of the list.
7347c478bd9Sstevel@tonic-gate  *
7357c478bd9Sstevel@tonic-gate  *    Move one marker page backwards towards the start of the list until
7367c478bd9Sstevel@tonic-gate  *    it is at the list head, processing the pages passed along the way.
7377c478bd9Sstevel@tonic-gate  *
7387c478bd9Sstevel@tonic-gate  *    Due to race conditions when the vphm mutex is dropped, additional pages
7397c478bd9Sstevel@tonic-gate  *    can be added to either end of the list, so we'll continue to move
7407c478bd9Sstevel@tonic-gate  *    the marker and process pages until it is up against the end marker.
7417c478bd9Sstevel@tonic-gate  *
7427c478bd9Sstevel@tonic-gate  * There is one special exit condition. If we are processing a VMODSORT
7437c478bd9Sstevel@tonic-gate  * vnode and only writing back modified pages, we can stop as soon as
7447c478bd9Sstevel@tonic-gate  * we run into an unmodified page.  This makes fsync(3) operations fast.
7457c478bd9Sstevel@tonic-gate  */
7467c478bd9Sstevel@tonic-gate int
7477c478bd9Sstevel@tonic-gate pvn_vplist_dirty(
7487c478bd9Sstevel@tonic-gate 	vnode_t		*vp,
7497c478bd9Sstevel@tonic-gate 	u_offset_t	off,
7507c478bd9Sstevel@tonic-gate 	int		(*putapage)(vnode_t *, page_t *, u_offset_t *,
7517c478bd9Sstevel@tonic-gate 			size_t *, int, cred_t *),
7527c478bd9Sstevel@tonic-gate 	int		flags,
7537c478bd9Sstevel@tonic-gate 	cred_t		*cred)
7547c478bd9Sstevel@tonic-gate {
7557c478bd9Sstevel@tonic-gate 	page_t		*pp;
7567c478bd9Sstevel@tonic-gate 	page_t		*mark;		/* marker page that moves toward head */
7577c478bd9Sstevel@tonic-gate 	page_t		*end;		/* marker page at end of list */
7587c478bd9Sstevel@tonic-gate 	int		err = 0;
7597c478bd9Sstevel@tonic-gate 	int		error;
7607c478bd9Sstevel@tonic-gate 	kmutex_t	*vphm;
7617c478bd9Sstevel@tonic-gate 	se_t		se;
7627c478bd9Sstevel@tonic-gate 	page_t		**where_to_move;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type != VCHR);
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	if (vp->v_pages == NULL)
7677c478bd9Sstevel@tonic-gate 		return (0);
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	/*
7717c478bd9Sstevel@tonic-gate 	 * Serialize vplist_dirty operations on this vnode by setting VVMLOCK.
7727c478bd9Sstevel@tonic-gate 	 *
7737c478bd9Sstevel@tonic-gate 	 * Don't block on VVMLOCK if B_ASYNC is set. This prevents sync()
7747c478bd9Sstevel@tonic-gate 	 * from getting blocked while flushing pages to a dead NFS server.
7757c478bd9Sstevel@tonic-gate 	 */
7767c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
7777c478bd9Sstevel@tonic-gate 	if ((vp->v_flag & VVMLOCK) && (flags & B_ASYNC)) {
7787c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
7797c478bd9Sstevel@tonic-gate 		return (EAGAIN);
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	while (vp->v_flag & VVMLOCK)
7837c478bd9Sstevel@tonic-gate 		cv_wait(&vp->v_cv, &vp->v_lock);
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	if (vp->v_pages == NULL) {
7867c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
7877c478bd9Sstevel@tonic-gate 		return (0);
7887c478bd9Sstevel@tonic-gate 	}
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	vp->v_flag |= VVMLOCK;
7917c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	/*
7957c478bd9Sstevel@tonic-gate 	 * Set up the marker pages used to walk the list
7967c478bd9Sstevel@tonic-gate 	 */
7977c478bd9Sstevel@tonic-gate 	end = kmem_cache_alloc(marker_cache, KM_SLEEP);
7987c478bd9Sstevel@tonic-gate 	end->p_vnode = vp;
7997c478bd9Sstevel@tonic-gate 	end->p_offset = (u_offset_t)-2;
8007c478bd9Sstevel@tonic-gate 	mark = kmem_cache_alloc(marker_cache, KM_SLEEP);
8017c478bd9Sstevel@tonic-gate 	mark->p_vnode = vp;
8027c478bd9Sstevel@tonic-gate 	mark->p_offset = (u_offset_t)-1;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	/*
8057c478bd9Sstevel@tonic-gate 	 * Grab the lock protecting the vnode's page list
8067c478bd9Sstevel@tonic-gate 	 * note that this lock is dropped at times in the loop.
8077c478bd9Sstevel@tonic-gate 	 */
8087c478bd9Sstevel@tonic-gate 	vphm = page_vnode_mutex(vp);
8097c478bd9Sstevel@tonic-gate 	mutex_enter(vphm);
8107c478bd9Sstevel@tonic-gate 	if (vp->v_pages == NULL)
8117c478bd9Sstevel@tonic-gate 		goto leave;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	/*
8147c478bd9Sstevel@tonic-gate 	 * insert the markers and loop through the list of pages
8157c478bd9Sstevel@tonic-gate 	 */
8167c478bd9Sstevel@tonic-gate 	page_vpadd(&vp->v_pages->p_vpprev->p_vpnext, mark);
8177c478bd9Sstevel@tonic-gate 	page_vpadd(&mark->p_vpnext, end);
8187c478bd9Sstevel@tonic-gate 	for (;;) {
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 		/*
8217c478bd9Sstevel@tonic-gate 		 * If only doing an async write back, then we can
8227c478bd9Sstevel@tonic-gate 		 * stop as soon as we get to start of the list.
8237c478bd9Sstevel@tonic-gate 		 */
8247c478bd9Sstevel@tonic-gate 		if (flags == B_ASYNC && vp->v_pages == mark)
8257c478bd9Sstevel@tonic-gate 			break;
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 		/*
8287c478bd9Sstevel@tonic-gate 		 * otherwise stop when we've gone through all the pages
8297c478bd9Sstevel@tonic-gate 		 */
8307c478bd9Sstevel@tonic-gate 		if (mark->p_vpprev == end)
8317c478bd9Sstevel@tonic-gate 			break;
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 		pp = mark->p_vpprev;
8347c478bd9Sstevel@tonic-gate 		if (vp->v_pages == pp)
8357c478bd9Sstevel@tonic-gate 			where_to_move = &vp->v_pages;
8367c478bd9Sstevel@tonic-gate 		else
8377c478bd9Sstevel@tonic-gate 			where_to_move = &pp->p_vpprev->p_vpnext;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 		ASSERT(pp->p_vnode == vp);
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 		/*
8427c478bd9Sstevel@tonic-gate 		 * If just flushing dirty pages to disk and this vnode
8437c478bd9Sstevel@tonic-gate 		 * is using a sorted list of pages, we can stop processing
8447c478bd9Sstevel@tonic-gate 		 * as soon as we find an unmodified page. Since all the
8457c478bd9Sstevel@tonic-gate 		 * modified pages are visited first.
8467c478bd9Sstevel@tonic-gate 		 */
8477c478bd9Sstevel@tonic-gate 		if (IS_VMODSORT(vp) &&
848a71e32b6Sstans 		    !(flags & (B_INVAL | B_FREE | B_TRUNC))) {
849a71e32b6Sstans 			if (!hat_ismod(pp) && !page_io_locked(pp)) {
8507c478bd9Sstevel@tonic-gate #ifdef  DEBUG
851a71e32b6Sstans 				/*
852a71e32b6Sstans 				 * For debug kernels examine what should be
853a71e32b6Sstans 				 * all the remaining clean pages, asserting
854a71e32b6Sstans 				 * that they are not modified.
855a71e32b6Sstans 				 */
856a71e32b6Sstans 				page_t	*chk = pp;
857a71e32b6Sstans 				int	attr;
858a71e32b6Sstans 
859a71e32b6Sstans 				page_vpsub(&vp->v_pages, mark);
860a71e32b6Sstans 				page_vpadd(where_to_move, mark);
861a71e32b6Sstans 				do {
862a71e32b6Sstans 					chk = chk->p_vpprev;
863a71e32b6Sstans 					ASSERT(chk != end);
864a71e32b6Sstans 					if (chk == mark)
865a71e32b6Sstans 						continue;
866a71e32b6Sstans 					attr = hat_page_getattr(chk, P_MOD |
867a71e32b6Sstans 					    P_REF);
868a71e32b6Sstans 					if ((attr & P_MOD) == 0)
869a71e32b6Sstans 						continue;
870a71e32b6Sstans 					panic("v_pages list not all clean: "
871a71e32b6Sstans 					    "page_t*=%p vnode=%p off=%lx "
872a71e32b6Sstans 					    "attr=0x%x last clean page_t*=%p\n",
873a71e32b6Sstans 					    (void *)chk, (void *)chk->p_vnode,
874a71e32b6Sstans 					    (long)chk->p_offset, attr,
875a71e32b6Sstans 					    (void *)pp);
876a71e32b6Sstans 				} while (chk != vp->v_pages);
8777c478bd9Sstevel@tonic-gate #endif
878a71e32b6Sstans 				break;
879a71e32b6Sstans 			} else if (!(flags & B_ASYNC) && !hat_ismod(pp)) {
880a71e32b6Sstans 				/*
881a71e32b6Sstans 				 * Couldn't get io lock, wait until IO is done.
882a71e32b6Sstans 				 * Block only for sync IO since we don't want
883a71e32b6Sstans 				 * to block async IO.
884a71e32b6Sstans 				 */
885a71e32b6Sstans 				mutex_exit(vphm);
886a71e32b6Sstans 				page_io_wait(pp);
887a71e32b6Sstans 				mutex_enter(vphm);
888a71e32b6Sstans 				continue;
889a71e32b6Sstans 			}
8907c478bd9Sstevel@tonic-gate 		}
8917c478bd9Sstevel@tonic-gate 
892adbe22e1SPeter Telford 		/*
893adbe22e1SPeter Telford 		 * Skip this page if the offset is out of the desired range.
894adbe22e1SPeter Telford 		 * Just move the marker and continue.
895adbe22e1SPeter Telford 		 */
896adbe22e1SPeter Telford 		if (pp->p_offset < off) {
897adbe22e1SPeter Telford 			page_vpsub(&vp->v_pages, mark);
898adbe22e1SPeter Telford 			page_vpadd(where_to_move, mark);
899adbe22e1SPeter Telford 			continue;
900adbe22e1SPeter Telford 		}
901adbe22e1SPeter Telford 
9027c478bd9Sstevel@tonic-gate 		/*
9037c478bd9Sstevel@tonic-gate 		 * If we are supposed to invalidate or free this
9047c478bd9Sstevel@tonic-gate 		 * page, then we need an exclusive lock.
9057c478bd9Sstevel@tonic-gate 		 */
9067c478bd9Sstevel@tonic-gate 		se = (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED;
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 		/*
9097c478bd9Sstevel@tonic-gate 		 * We must acquire the page lock for all synchronous
9107c478bd9Sstevel@tonic-gate 		 * operations (invalidate, free and write).
9117c478bd9Sstevel@tonic-gate 		 */
9127c478bd9Sstevel@tonic-gate 		if ((flags & B_INVAL) != 0 || (flags & B_ASYNC) == 0) {
9137c478bd9Sstevel@tonic-gate 			/*
9147c478bd9Sstevel@tonic-gate 			 * If the page_lock() drops the mutex
9157c478bd9Sstevel@tonic-gate 			 * we must retry the loop.
9167c478bd9Sstevel@tonic-gate 			 */
9177c478bd9Sstevel@tonic-gate 			if (!page_lock(pp, se, vphm, P_NO_RECLAIM))
9187c478bd9Sstevel@tonic-gate 				continue;
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 			/*
9217c478bd9Sstevel@tonic-gate 			 * It's ok to move the marker page now.
9227c478bd9Sstevel@tonic-gate 			 */
9237c478bd9Sstevel@tonic-gate 			page_vpsub(&vp->v_pages, mark);
9247c478bd9Sstevel@tonic-gate 			page_vpadd(where_to_move, mark);
9257c478bd9Sstevel@tonic-gate 		} else {
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 			/*
9287c478bd9Sstevel@tonic-gate 			 * update the marker page for all remaining cases
9297c478bd9Sstevel@tonic-gate 			 */
9307c478bd9Sstevel@tonic-gate 			page_vpsub(&vp->v_pages, mark);
9317c478bd9Sstevel@tonic-gate 			page_vpadd(where_to_move, mark);
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 			/*
9347c478bd9Sstevel@tonic-gate 			 * For write backs, If we can't lock the page, it's
9357c478bd9Sstevel@tonic-gate 			 * invalid or in the process of being destroyed.  Skip
9367c478bd9Sstevel@tonic-gate 			 * it, assuming someone else is writing it.
9377c478bd9Sstevel@tonic-gate 			 */
9387c478bd9Sstevel@tonic-gate 			if (!page_trylock(pp, se))
9397c478bd9Sstevel@tonic-gate 				continue;
9407c478bd9Sstevel@tonic-gate 		}
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 		ASSERT(pp->p_vnode == vp);
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 		/*
9457c478bd9Sstevel@tonic-gate 		 * Successfully locked the page, now figure out what to
9467c478bd9Sstevel@tonic-gate 		 * do with it. Free pages are easily dealt with, invalidate
9477c478bd9Sstevel@tonic-gate 		 * if desired or just go on to the next page.
9487c478bd9Sstevel@tonic-gate 		 */
9497c478bd9Sstevel@tonic-gate 		if (PP_ISFREE(pp)) {
9507c478bd9Sstevel@tonic-gate 			if ((flags & B_INVAL) == 0) {
9517c478bd9Sstevel@tonic-gate 				page_unlock(pp);
9527c478bd9Sstevel@tonic-gate 				continue;
9537c478bd9Sstevel@tonic-gate 			}
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 			/*
9567c478bd9Sstevel@tonic-gate 			 * Invalidate (destroy) the page.
9577c478bd9Sstevel@tonic-gate 			 */
9587c478bd9Sstevel@tonic-gate 			mutex_exit(vphm);
9597c478bd9Sstevel@tonic-gate 			page_destroy_free(pp);
9607c478bd9Sstevel@tonic-gate 			mutex_enter(vphm);
9617c478bd9Sstevel@tonic-gate 			continue;
9627c478bd9Sstevel@tonic-gate 		}
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 		/*
9657c478bd9Sstevel@tonic-gate 		 * pvn_getdirty() figures out what do do with a dirty page.
9667c478bd9Sstevel@tonic-gate 		 * If the page is dirty, the putapage() routine will write it
9677c478bd9Sstevel@tonic-gate 		 * and will kluster any other adjacent dirty pages it can.
9687c478bd9Sstevel@tonic-gate 		 *
9697c478bd9Sstevel@tonic-gate 		 * pvn_getdirty() and `(*putapage)' unlock the page.
9707c478bd9Sstevel@tonic-gate 		 */
9717c478bd9Sstevel@tonic-gate 		mutex_exit(vphm);
9727c478bd9Sstevel@tonic-gate 		if (pvn_getdirty(pp, flags)) {
9737c478bd9Sstevel@tonic-gate 			error = (*putapage)(vp, pp, NULL, NULL, flags, cred);
9747c478bd9Sstevel@tonic-gate 			if (!err)
9757c478bd9Sstevel@tonic-gate 				err = error;
9767c478bd9Sstevel@tonic-gate 		}
9777c478bd9Sstevel@tonic-gate 		mutex_enter(vphm);
9787c478bd9Sstevel@tonic-gate 	}
9797c478bd9Sstevel@tonic-gate 	page_vpsub(&vp->v_pages, mark);
9807c478bd9Sstevel@tonic-gate 	page_vpsub(&vp->v_pages, end);
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate leave:
9837c478bd9Sstevel@tonic-gate 	/*
9847c478bd9Sstevel@tonic-gate 	 * Release v_pages mutex, also VVMLOCK and wakeup blocked thrds
9857c478bd9Sstevel@tonic-gate 	 */
9867c478bd9Sstevel@tonic-gate 	mutex_exit(vphm);
9877c478bd9Sstevel@tonic-gate 	kmem_cache_free(marker_cache, mark);
9887c478bd9Sstevel@tonic-gate 	kmem_cache_free(marker_cache, end);
9897c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
9907c478bd9Sstevel@tonic-gate 	vp->v_flag &= ~VVMLOCK;
9917c478bd9Sstevel@tonic-gate 	cv_broadcast(&vp->v_cv);
9927c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
9937c478bd9Sstevel@tonic-gate 	return (err);
9947c478bd9Sstevel@tonic-gate }
9957c478bd9Sstevel@tonic-gate 
996*f8bbc571SPavel Filipensky /*
997*f8bbc571SPavel Filipensky  * Walk the vp->v_pages list, for every page call the callback function
998*f8bbc571SPavel Filipensky  * pointed by *page_check. If page_check returns non-zero, then mark the
999*f8bbc571SPavel Filipensky  * page as modified and if VMODSORT is set, move it to the end of v_pages
1000*f8bbc571SPavel Filipensky  * list. Moving makes sense only if we have at least two pages - this also
1001*f8bbc571SPavel Filipensky  * avoids having v_pages temporarily being NULL after calling page_vpsub()
1002*f8bbc571SPavel Filipensky  * if there was just one page.
1003*f8bbc571SPavel Filipensky  */
1004*f8bbc571SPavel Filipensky void
1005*f8bbc571SPavel Filipensky pvn_vplist_setdirty(vnode_t *vp, int (*page_check)(page_t *))
1006*f8bbc571SPavel Filipensky {
1007*f8bbc571SPavel Filipensky 	page_t	*pp, *next, *end;
1008*f8bbc571SPavel Filipensky 	kmutex_t	*vphm;
1009*f8bbc571SPavel Filipensky 	int	shuffle;
1010*f8bbc571SPavel Filipensky 
1011*f8bbc571SPavel Filipensky 	vphm = page_vnode_mutex(vp);
1012*f8bbc571SPavel Filipensky 	mutex_enter(vphm);
1013*f8bbc571SPavel Filipensky 
1014*f8bbc571SPavel Filipensky 	if (vp->v_pages == NULL) {
1015*f8bbc571SPavel Filipensky 		mutex_exit(vphm);
1016*f8bbc571SPavel Filipensky 		return;
1017*f8bbc571SPavel Filipensky 	}
1018*f8bbc571SPavel Filipensky 
1019*f8bbc571SPavel Filipensky 	end = vp->v_pages->p_vpprev;
1020*f8bbc571SPavel Filipensky 	shuffle = IS_VMODSORT(vp) && (vp->v_pages != end);
1021*f8bbc571SPavel Filipensky 	pp = vp->v_pages;
1022*f8bbc571SPavel Filipensky 
1023*f8bbc571SPavel Filipensky 	for (;;) {
1024*f8bbc571SPavel Filipensky 		next = pp->p_vpnext;
1025*f8bbc571SPavel Filipensky 		if (pp->p_hash != PVN_VPLIST_HASH_TAG && page_check(pp)) {
1026*f8bbc571SPavel Filipensky 			/*
1027*f8bbc571SPavel Filipensky 			 * hat_setmod_only() in contrast to hat_setmod() does
1028*f8bbc571SPavel Filipensky 			 * not shuffle the pages and does not grab the mutex
1029*f8bbc571SPavel Filipensky 			 * page_vnode_mutex. Exactly what we need.
1030*f8bbc571SPavel Filipensky 			 */
1031*f8bbc571SPavel Filipensky 			hat_setmod_only(pp);
1032*f8bbc571SPavel Filipensky 			if (shuffle) {
1033*f8bbc571SPavel Filipensky 				page_vpsub(&vp->v_pages, pp);
1034*f8bbc571SPavel Filipensky 				ASSERT(vp->v_pages != NULL);
1035*f8bbc571SPavel Filipensky 				page_vpadd(&vp->v_pages->p_vpprev->p_vpnext,
1036*f8bbc571SPavel Filipensky 				    pp);
1037*f8bbc571SPavel Filipensky 			}
1038*f8bbc571SPavel Filipensky 		}
1039*f8bbc571SPavel Filipensky 		/* Stop if we have just processed the last page. */
1040*f8bbc571SPavel Filipensky 		if (pp == end)
1041*f8bbc571SPavel Filipensky 			break;
1042*f8bbc571SPavel Filipensky 		pp = next;
1043*f8bbc571SPavel Filipensky 	}
1044*f8bbc571SPavel Filipensky 
1045*f8bbc571SPavel Filipensky 	mutex_exit(vphm);
1046*f8bbc571SPavel Filipensky }
1047*f8bbc571SPavel Filipensky 
10487c478bd9Sstevel@tonic-gate /*
10497c478bd9Sstevel@tonic-gate  * Zero out zbytes worth of data. Caller should be aware that this
10507c478bd9Sstevel@tonic-gate  * routine may enter back into the fs layer (xxx_getpage). Locks
10517c478bd9Sstevel@tonic-gate  * that the xxx_getpage routine may need should not be held while
10527c478bd9Sstevel@tonic-gate  * calling this.
10537c478bd9Sstevel@tonic-gate  */
10547c478bd9Sstevel@tonic-gate void
10557c478bd9Sstevel@tonic-gate pvn_vpzero(struct vnode *vp, u_offset_t vplen, size_t zbytes)
10567c478bd9Sstevel@tonic-gate {
10577c478bd9Sstevel@tonic-gate 	caddr_t addr;
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type != VCHR);
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	if (vp->v_pages == NULL)
10627c478bd9Sstevel@tonic-gate 		return;
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	/*
10657c478bd9Sstevel@tonic-gate 	 * zbytes may be zero but there still may be some portion of
10667c478bd9Sstevel@tonic-gate 	 * a page which needs clearing (since zbytes is a function
10677c478bd9Sstevel@tonic-gate 	 * of filesystem block size, not pagesize.)
10687c478bd9Sstevel@tonic-gate 	 */
10697c478bd9Sstevel@tonic-gate 	if (zbytes == 0 && (PAGESIZE - (vplen & PAGEOFFSET)) == 0)
10707c478bd9Sstevel@tonic-gate 		return;
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	/*
10737c478bd9Sstevel@tonic-gate 	 * We get the last page and handle the partial
10747c478bd9Sstevel@tonic-gate 	 * zeroing via kernel mappings.  This will make the page
10757c478bd9Sstevel@tonic-gate 	 * dirty so that we know that when this page is written
10767c478bd9Sstevel@tonic-gate 	 * back, the zeroed information will go out with it.  If
10777c478bd9Sstevel@tonic-gate 	 * the page is not currently in memory, then the kzero
10787c478bd9Sstevel@tonic-gate 	 * operation will cause it to be brought it.  We use kzero
10797c478bd9Sstevel@tonic-gate 	 * instead of bzero so that if the page cannot be read in
10807c478bd9Sstevel@tonic-gate 	 * for any reason, the system will not panic.  We need
10817c478bd9Sstevel@tonic-gate 	 * to zero out a minimum of the fs given zbytes, but we
10827c478bd9Sstevel@tonic-gate 	 * might also have to do more to get the entire last page.
10837c478bd9Sstevel@tonic-gate 	 */
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	if ((zbytes + (vplen & MAXBOFFSET)) > MAXBSIZE)
10867c478bd9Sstevel@tonic-gate 		panic("pvn_vptrunc zbytes");
10877c478bd9Sstevel@tonic-gate 	addr = segmap_getmapflt(segkmap, vp, vplen,
10887c478bd9Sstevel@tonic-gate 	    MAX(zbytes, PAGESIZE - (vplen & PAGEOFFSET)), 1, S_WRITE);
10897c478bd9Sstevel@tonic-gate 	(void) kzero(addr + (vplen & MAXBOFFSET),
10907c478bd9Sstevel@tonic-gate 	    MAX(zbytes, PAGESIZE - (vplen & PAGEOFFSET)));
10917c478bd9Sstevel@tonic-gate 	(void) segmap_release(segkmap, addr, SM_WRITE | SM_ASYNC);
10927c478bd9Sstevel@tonic-gate }
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate /*
10957c478bd9Sstevel@tonic-gate  * Handles common work of the VOP_GETPAGE routines when more than
10967c478bd9Sstevel@tonic-gate  * one page must be returned by calling a file system specific operation
10977c478bd9Sstevel@tonic-gate  * to do most of the work.  Must be called with the vp already locked
10987c478bd9Sstevel@tonic-gate  * by the VOP_GETPAGE routine.
10997c478bd9Sstevel@tonic-gate  */
11007c478bd9Sstevel@tonic-gate int
11017c478bd9Sstevel@tonic-gate pvn_getpages(
11027c478bd9Sstevel@tonic-gate 	int (*getpage)(vnode_t *, u_offset_t, size_t, uint_t *, page_t *[],
11037c478bd9Sstevel@tonic-gate 		size_t, struct seg *, caddr_t, enum seg_rw, cred_t *),
11047c478bd9Sstevel@tonic-gate 	struct vnode *vp,
11057c478bd9Sstevel@tonic-gate 	u_offset_t off,
11067c478bd9Sstevel@tonic-gate 	size_t len,
11077c478bd9Sstevel@tonic-gate 	uint_t *protp,
11087c478bd9Sstevel@tonic-gate 	page_t *pl[],
11097c478bd9Sstevel@tonic-gate 	size_t plsz,
11107c478bd9Sstevel@tonic-gate 	struct seg *seg,
11117c478bd9Sstevel@tonic-gate 	caddr_t addr,
11127c478bd9Sstevel@tonic-gate 	enum seg_rw rw,
11137c478bd9Sstevel@tonic-gate 	struct cred *cred)
11147c478bd9Sstevel@tonic-gate {
11157c478bd9Sstevel@tonic-gate 	page_t **ppp;
11167c478bd9Sstevel@tonic-gate 	u_offset_t o, eoff;
11177c478bd9Sstevel@tonic-gate 	size_t sz, xlen;
11187c478bd9Sstevel@tonic-gate 	int err;
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	ASSERT(plsz >= len);		/* insure that we have enough space */
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	/*
11237c478bd9Sstevel@tonic-gate 	 * Loop one page at a time and let getapage function fill
11247c478bd9Sstevel@tonic-gate 	 * in the next page in array.  We only allow one page to be
11257c478bd9Sstevel@tonic-gate 	 * returned at a time (except for the last page) so that we
11267c478bd9Sstevel@tonic-gate 	 * don't have any problems with duplicates and other such
11277c478bd9Sstevel@tonic-gate 	 * painful problems.  This is a very simple minded algorithm,
11287c478bd9Sstevel@tonic-gate 	 * but it does the job correctly.  We hope that the cost of a
11297c478bd9Sstevel@tonic-gate 	 * getapage call for a resident page that we might have been
11307c478bd9Sstevel@tonic-gate 	 * able to get from an earlier call doesn't cost too much.
11317c478bd9Sstevel@tonic-gate 	 */
11327c478bd9Sstevel@tonic-gate 	ppp = pl;
11337c478bd9Sstevel@tonic-gate 	sz = PAGESIZE;
11347c478bd9Sstevel@tonic-gate 	eoff = off + len;
11357c478bd9Sstevel@tonic-gate 	xlen = len;
11367c478bd9Sstevel@tonic-gate 	for (o = off; o < eoff; o += PAGESIZE, addr += PAGESIZE,
11377c478bd9Sstevel@tonic-gate 	    xlen -= PAGESIZE) {
11387c478bd9Sstevel@tonic-gate 		if (o + PAGESIZE >= eoff) {
11397c478bd9Sstevel@tonic-gate 			/*
11407c478bd9Sstevel@tonic-gate 			 * Last time through - allow the all of
11417c478bd9Sstevel@tonic-gate 			 * what's left of the pl[] array to be used.
11427c478bd9Sstevel@tonic-gate 			 */
11437c478bd9Sstevel@tonic-gate 			sz = plsz - (o - off);
11447c478bd9Sstevel@tonic-gate 		}
11457c478bd9Sstevel@tonic-gate 		err = (*getpage)(vp, o, xlen, protp, ppp, sz, seg, addr,
11467c478bd9Sstevel@tonic-gate 		    rw, cred);
11477c478bd9Sstevel@tonic-gate 		if (err) {
11487c478bd9Sstevel@tonic-gate 			/*
11497c478bd9Sstevel@tonic-gate 			 * Release any pages we already got.
11507c478bd9Sstevel@tonic-gate 			 */
11517c478bd9Sstevel@tonic-gate 			if (o > off && pl != NULL) {
11527c478bd9Sstevel@tonic-gate 				for (ppp = pl; *ppp != NULL; *ppp++ = NULL)
11537c478bd9Sstevel@tonic-gate 					(void) page_release(*ppp, 1);
11547c478bd9Sstevel@tonic-gate 			}
11557c478bd9Sstevel@tonic-gate 			break;
11567c478bd9Sstevel@tonic-gate 		}
11577c478bd9Sstevel@tonic-gate 		if (pl != NULL)
11587c478bd9Sstevel@tonic-gate 			ppp++;
11597c478bd9Sstevel@tonic-gate 	}
11607c478bd9Sstevel@tonic-gate 	return (err);
11617c478bd9Sstevel@tonic-gate }
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate /*
11647c478bd9Sstevel@tonic-gate  * Initialize the page list array.
11657c478bd9Sstevel@tonic-gate  */
1166081a94b0Saguzovsk /*ARGSUSED*/
11677c478bd9Sstevel@tonic-gate void
11687c478bd9Sstevel@tonic-gate pvn_plist_init(page_t *pp, page_t *pl[], size_t plsz,
11697c478bd9Sstevel@tonic-gate     u_offset_t off, size_t io_len, enum seg_rw rw)
11707c478bd9Sstevel@tonic-gate {
11717c478bd9Sstevel@tonic-gate 	ssize_t sz;
11727c478bd9Sstevel@tonic-gate 	page_t *ppcur, **ppp;
11737c478bd9Sstevel@tonic-gate 
1174081a94b0Saguzovsk 	/*
1175081a94b0Saguzovsk 	 * Set up to load plsz worth
1176081a94b0Saguzovsk 	 * starting at the needed page.
1177081a94b0Saguzovsk 	 */
1178081a94b0Saguzovsk 	while (pp != NULL && pp->p_offset != off) {
11797c478bd9Sstevel@tonic-gate 		/*
1180081a94b0Saguzovsk 		 * Remove page from the i/o list,
1181081a94b0Saguzovsk 		 * release the i/o and the page lock.
11827c478bd9Sstevel@tonic-gate 		 */
1183081a94b0Saguzovsk 		ppcur = pp;
1184081a94b0Saguzovsk 		page_sub(&pp, ppcur);
1185081a94b0Saguzovsk 		page_io_unlock(ppcur);
1186081a94b0Saguzovsk 		(void) page_release(ppcur, 1);
11877c478bd9Sstevel@tonic-gate 	}
11887c478bd9Sstevel@tonic-gate 
1189081a94b0Saguzovsk 	if (pp == NULL) {
1190081a94b0Saguzovsk 		pl[0] = NULL;
1191081a94b0Saguzovsk 		return;
1192081a94b0Saguzovsk 	}
1193081a94b0Saguzovsk 
1194081a94b0Saguzovsk 	sz = plsz;
1195081a94b0Saguzovsk 
11967c478bd9Sstevel@tonic-gate 	/*
11977c478bd9Sstevel@tonic-gate 	 * Initialize the page list array.
11987c478bd9Sstevel@tonic-gate 	 */
11997c478bd9Sstevel@tonic-gate 	ppp = pl;
12007c478bd9Sstevel@tonic-gate 	do {
12017c478bd9Sstevel@tonic-gate 		ppcur = pp;
12027c478bd9Sstevel@tonic-gate 		*ppp++ = ppcur;
12037c478bd9Sstevel@tonic-gate 		page_sub(&pp, ppcur);
12047c478bd9Sstevel@tonic-gate 		page_io_unlock(ppcur);
12057c478bd9Sstevel@tonic-gate 		if (rw != S_CREATE)
12067c478bd9Sstevel@tonic-gate 			page_downgrade(ppcur);
12077c478bd9Sstevel@tonic-gate 		sz -= PAGESIZE;
12087c478bd9Sstevel@tonic-gate 	} while (sz > 0 && pp != NULL);
12097c478bd9Sstevel@tonic-gate 	*ppp = NULL;		/* terminate list */
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	/*
12127c478bd9Sstevel@tonic-gate 	 * Now free the remaining pages that weren't
12137c478bd9Sstevel@tonic-gate 	 * loaded in the page list.
12147c478bd9Sstevel@tonic-gate 	 */
12157c478bd9Sstevel@tonic-gate 	while (pp != NULL) {
12167c478bd9Sstevel@tonic-gate 		ppcur = pp;
12177c478bd9Sstevel@tonic-gate 		page_sub(&pp, ppcur);
12187c478bd9Sstevel@tonic-gate 		page_io_unlock(ppcur);
12197c478bd9Sstevel@tonic-gate 		(void) page_release(ppcur, 1);
12207c478bd9Sstevel@tonic-gate 	}
12217c478bd9Sstevel@tonic-gate }
1222