xref: /titanic_44/usr/src/uts/common/fs/ufs/ufs_directio.c (revision 1a5e258f5471356ca102c7176637cdce45bac147)
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
580d34432Sfrankho  * Common Development and Distribution License (the "License").
680d34432Sfrankho  * 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 /*
22408aef6aSbatschul  * Copyright 2009 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) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
317c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
367c478bd9Sstevel@tonic-gate #include <sys/param.h>
377c478bd9Sstevel@tonic-gate #include <sys/time.h>
387c478bd9Sstevel@tonic-gate #include <sys/systm.h>
397c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
407c478bd9Sstevel@tonic-gate #include <sys/resource.h>
417c478bd9Sstevel@tonic-gate #include <sys/signal.h>
427c478bd9Sstevel@tonic-gate #include <sys/cred.h>
437c478bd9Sstevel@tonic-gate #include <sys/user.h>
447c478bd9Sstevel@tonic-gate #include <sys/buf.h>
457c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
467c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
477c478bd9Sstevel@tonic-gate #include <sys/proc.h>
487c478bd9Sstevel@tonic-gate #include <sys/disp.h>
497c478bd9Sstevel@tonic-gate #include <sys/file.h>
507c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
517c478bd9Sstevel@tonic-gate #include <sys/flock.h>
527c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
537c478bd9Sstevel@tonic-gate #include <sys/uio.h>
547c478bd9Sstevel@tonic-gate #include <sys/dnlc.h>
557c478bd9Sstevel@tonic-gate #include <sys/conf.h>
567c478bd9Sstevel@tonic-gate #include <sys/mman.h>
577c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
587c478bd9Sstevel@tonic-gate #include <sys/debug.h>
597c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
607c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
617c478bd9Sstevel@tonic-gate #include <sys/filio.h>
627c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #include <sys/fssnap_if.h>
657c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
667c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_lockfs.h>
677c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_filio.h>
687c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
697c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
707c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_quota.h>
717c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_trans.h>
727c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_panic.h>
737c478bd9Sstevel@tonic-gate #include <sys/dirent.h>		/* must be AFTER <sys/fs/fsdir.h>! */
747c478bd9Sstevel@tonic-gate #include <sys/errno.h>
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #include <sys/filio.h>		/* _FIOIO */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #include <vm/hat.h>
797c478bd9Sstevel@tonic-gate #include <vm/page.h>
807c478bd9Sstevel@tonic-gate #include <vm/pvn.h>
817c478bd9Sstevel@tonic-gate #include <vm/as.h>
827c478bd9Sstevel@tonic-gate #include <vm/seg.h>
837c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
847c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
857c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
867c478bd9Sstevel@tonic-gate #include <vm/rm.h>
877c478bd9Sstevel@tonic-gate #include <sys/swap.h>
887c478bd9Sstevel@tonic-gate #include <sys/epm.h>
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate static void	*ufs_directio_zero_buf;
937c478bd9Sstevel@tonic-gate static int	ufs_directio_zero_len	= 8192;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate int	ufs_directio_enabled = 1;	/* feature is enabled */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * for kstats reader
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate struct ufs_directio_kstats {
101d6767ee3Srshoaib 	kstat_named_t	logical_reads;
102d6767ee3Srshoaib 	kstat_named_t	phys_reads;
103d6767ee3Srshoaib 	kstat_named_t	hole_reads;
104d6767ee3Srshoaib 	kstat_named_t	nread;
105d6767ee3Srshoaib 	kstat_named_t	logical_writes;
106d6767ee3Srshoaib 	kstat_named_t	phys_writes;
107d6767ee3Srshoaib 	kstat_named_t	nwritten;
108d6767ee3Srshoaib 	kstat_named_t	nflushes;
109d6767ee3Srshoaib } ufs_directio_kstats = {
110d6767ee3Srshoaib 	{ "logical_reads",	KSTAT_DATA_UINT64 },
111d6767ee3Srshoaib 	{ "phys_reads",		KSTAT_DATA_UINT64 },
112d6767ee3Srshoaib 	{ "hole_reads",		KSTAT_DATA_UINT64 },
113d6767ee3Srshoaib 	{ "nread",		KSTAT_DATA_UINT64 },
114d6767ee3Srshoaib 	{ "logical_writes",	KSTAT_DATA_UINT64 },
115d6767ee3Srshoaib 	{ "phys_writes",	KSTAT_DATA_UINT64 },
116d6767ee3Srshoaib 	{ "nwritten",		KSTAT_DATA_UINT64 },
117d6767ee3Srshoaib 	{ "nflushes",		KSTAT_DATA_UINT64 },
118d6767ee3Srshoaib };
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate kstat_t	*ufs_directio_kstatsp;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * use kmem_cache_create for direct-physio buffers. This has shown
1247c478bd9Sstevel@tonic-gate  * a better cache distribution compared to buffers on the
1257c478bd9Sstevel@tonic-gate  * stack. It also avoids semaphore construction/deconstruction
1267c478bd9Sstevel@tonic-gate  * per request
1277c478bd9Sstevel@tonic-gate  */
1287c478bd9Sstevel@tonic-gate struct directio_buf {
1297c478bd9Sstevel@tonic-gate 	struct directio_buf	*next;
1307c478bd9Sstevel@tonic-gate 	char		*addr;
1317c478bd9Sstevel@tonic-gate 	size_t		nbytes;
1327c478bd9Sstevel@tonic-gate 	struct buf	buf;
1337c478bd9Sstevel@tonic-gate };
1347c478bd9Sstevel@tonic-gate static struct kmem_cache *directio_buf_cache;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /* ARGSUSED */
1387c478bd9Sstevel@tonic-gate static int
directio_buf_constructor(void * dbp,void * cdrarg,int kmflags)1397c478bd9Sstevel@tonic-gate directio_buf_constructor(void *dbp, void *cdrarg, int kmflags)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 	bioinit((struct buf *)&((struct directio_buf *)dbp)->buf);
1427c478bd9Sstevel@tonic-gate 	return (0);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /* ARGSUSED */
1467c478bd9Sstevel@tonic-gate static void
directio_buf_destructor(void * dbp,void * cdrarg)1477c478bd9Sstevel@tonic-gate directio_buf_destructor(void *dbp, void *cdrarg)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	biofini((struct buf *)&((struct directio_buf *)dbp)->buf);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate void
directio_bufs_init(void)1537c478bd9Sstevel@tonic-gate directio_bufs_init(void)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	directio_buf_cache = kmem_cache_create("directio_buf_cache",
1567c478bd9Sstevel@tonic-gate 	    sizeof (struct directio_buf), 0,
1577c478bd9Sstevel@tonic-gate 	    directio_buf_constructor, directio_buf_destructor,
1587c478bd9Sstevel@tonic-gate 	    NULL, NULL, NULL, 0);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate void
ufs_directio_init(void)1627c478bd9Sstevel@tonic-gate ufs_directio_init(void)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	/*
1657c478bd9Sstevel@tonic-gate 	 * kstats
1667c478bd9Sstevel@tonic-gate 	 */
167d6767ee3Srshoaib 	ufs_directio_kstatsp = kstat_create("ufs", 0,
168d6767ee3Srshoaib 	    "directio", "ufs", KSTAT_TYPE_NAMED,
169d6767ee3Srshoaib 	    sizeof (ufs_directio_kstats) / sizeof (kstat_named_t),
1707c478bd9Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE);
1717c478bd9Sstevel@tonic-gate 	if (ufs_directio_kstatsp) {
1727c478bd9Sstevel@tonic-gate 		ufs_directio_kstatsp->ks_data = (void *)&ufs_directio_kstats;
1737c478bd9Sstevel@tonic-gate 		kstat_install(ufs_directio_kstatsp);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 	/*
1767c478bd9Sstevel@tonic-gate 	 * kzero is broken so we have to use a private buf of zeroes
1777c478bd9Sstevel@tonic-gate 	 */
1787c478bd9Sstevel@tonic-gate 	ufs_directio_zero_buf = kmem_zalloc(ufs_directio_zero_len, KM_SLEEP);
1797c478bd9Sstevel@tonic-gate 	directio_bufs_init();
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate  * Wait for the first direct IO operation to finish
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate static int
directio_wait_one(struct directio_buf * dbp,long * bytes_iop)1867c478bd9Sstevel@tonic-gate directio_wait_one(struct directio_buf *dbp, long *bytes_iop)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	buf_t	*bp;
1897c478bd9Sstevel@tonic-gate 	int	error;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	/*
1927c478bd9Sstevel@tonic-gate 	 * Wait for IO to finish
1937c478bd9Sstevel@tonic-gate 	 */
1947c478bd9Sstevel@tonic-gate 	bp = &dbp->buf;
1957c478bd9Sstevel@tonic-gate 	error = biowait(bp);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/*
1987c478bd9Sstevel@tonic-gate 	 * bytes_io will be used to figure out a resid
1997c478bd9Sstevel@tonic-gate 	 * for the caller. The resid is approximated by reporting
2007c478bd9Sstevel@tonic-gate 	 * the bytes following the first failed IO as the residual.
2017c478bd9Sstevel@tonic-gate 	 *
2027c478bd9Sstevel@tonic-gate 	 * I am cautious about using b_resid because I
2037c478bd9Sstevel@tonic-gate 	 * am not sure how well the disk drivers maintain it.
2047c478bd9Sstevel@tonic-gate 	 */
2057c478bd9Sstevel@tonic-gate 	if (error)
2067c478bd9Sstevel@tonic-gate 		if (bp->b_resid)
2077c478bd9Sstevel@tonic-gate 			*bytes_iop = bp->b_bcount - bp->b_resid;
2087c478bd9Sstevel@tonic-gate 		else
2097c478bd9Sstevel@tonic-gate 			*bytes_iop = 0;
2107c478bd9Sstevel@tonic-gate 	else
2117c478bd9Sstevel@tonic-gate 		*bytes_iop += bp->b_bcount;
2127c478bd9Sstevel@tonic-gate 	/*
2137c478bd9Sstevel@tonic-gate 	 * Release direct IO resources
2147c478bd9Sstevel@tonic-gate 	 */
2157c478bd9Sstevel@tonic-gate 	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_SHADOW);
2167c478bd9Sstevel@tonic-gate 	kmem_cache_free(directio_buf_cache, dbp);
2177c478bd9Sstevel@tonic-gate 	return (error);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate  * Wait for all of the direct IO operations to finish
2227c478bd9Sstevel@tonic-gate  */
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate uint32_t	ufs_directio_drop_kpri = 0;	/* enable kpri hack */
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate static int
directio_wait(struct directio_buf * tail,long * bytes_iop)2277c478bd9Sstevel@tonic-gate directio_wait(struct directio_buf *tail, long *bytes_iop)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	int	error = 0, newerror;
2307c478bd9Sstevel@tonic-gate 	struct directio_buf	*dbp;
2317c478bd9Sstevel@tonic-gate 	uint_t	kpri_req_save;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/*
2347c478bd9Sstevel@tonic-gate 	 * The linked list of directio buf structures is maintained
2357c478bd9Sstevel@tonic-gate 	 * in reverse order (tail->last request->penultimate request->...)
2367c478bd9Sstevel@tonic-gate 	 */
2377c478bd9Sstevel@tonic-gate 	/*
2387c478bd9Sstevel@tonic-gate 	 * This is the k_pri_req hack. Large numbers of threads
2397c478bd9Sstevel@tonic-gate 	 * sleeping with kernel priority will cause scheduler thrashing
2407c478bd9Sstevel@tonic-gate 	 * on an MP machine. This can be seen running Oracle using
2417c478bd9Sstevel@tonic-gate 	 * directio to ufs files. Sleep at normal priority here to
2427c478bd9Sstevel@tonic-gate 	 * more closely mimic physio to a device partition. This
2437c478bd9Sstevel@tonic-gate 	 * workaround is disabled by default as a niced thread could
2447c478bd9Sstevel@tonic-gate 	 * be starved from running while holding i_rwlock and i_contents.
2457c478bd9Sstevel@tonic-gate 	 */
2467c478bd9Sstevel@tonic-gate 	if (ufs_directio_drop_kpri) {
2477c478bd9Sstevel@tonic-gate 		kpri_req_save = curthread->t_kpri_req;
2487c478bd9Sstevel@tonic-gate 		curthread->t_kpri_req = 0;
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 	while ((dbp = tail) != NULL) {
2517c478bd9Sstevel@tonic-gate 		tail = dbp->next;
2527c478bd9Sstevel@tonic-gate 		newerror = directio_wait_one(dbp, bytes_iop);
2537c478bd9Sstevel@tonic-gate 		if (error == 0)
2547c478bd9Sstevel@tonic-gate 			error = newerror;
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 	if (ufs_directio_drop_kpri)
2577c478bd9Sstevel@tonic-gate 		curthread->t_kpri_req = kpri_req_save;
2587c478bd9Sstevel@tonic-gate 	return (error);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate  * Initiate direct IO request
2627c478bd9Sstevel@tonic-gate  */
2637c478bd9Sstevel@tonic-gate static void
directio_start(struct ufsvfs * ufsvfsp,struct inode * ip,size_t nbytes,offset_t offset,char * addr,enum seg_rw rw,struct proc * procp,struct directio_buf ** tailp,page_t ** pplist)264408aef6aSbatschul directio_start(struct ufsvfs *ufsvfsp, struct inode *ip, size_t nbytes,
2657c478bd9Sstevel@tonic-gate 	offset_t offset, char *addr, enum seg_rw rw, struct proc *procp,
2667c478bd9Sstevel@tonic-gate 	struct directio_buf **tailp, page_t **pplist)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	buf_t *bp;
2697c478bd9Sstevel@tonic-gate 	struct directio_buf *dbp;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/*
2727c478bd9Sstevel@tonic-gate 	 * Allocate a directio buf header
2737c478bd9Sstevel@tonic-gate 	 *   Note - list is maintained in reverse order.
2747c478bd9Sstevel@tonic-gate 	 *   directio_wait_one() depends on this fact when
2757c478bd9Sstevel@tonic-gate 	 *   adjusting the ``bytes_io'' param. bytes_io
2767c478bd9Sstevel@tonic-gate 	 *   is used to compute a residual in the case of error.
2777c478bd9Sstevel@tonic-gate 	 */
2787c478bd9Sstevel@tonic-gate 	dbp = kmem_cache_alloc(directio_buf_cache, KM_SLEEP);
2797c478bd9Sstevel@tonic-gate 	dbp->next = *tailp;
2807c478bd9Sstevel@tonic-gate 	*tailp = dbp;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	/*
2837c478bd9Sstevel@tonic-gate 	 * Initialize buf header
2847c478bd9Sstevel@tonic-gate 	 */
2857c478bd9Sstevel@tonic-gate 	dbp->addr = addr;
2867c478bd9Sstevel@tonic-gate 	dbp->nbytes = nbytes;
2877c478bd9Sstevel@tonic-gate 	bp = &dbp->buf;
288408aef6aSbatschul 	bp->b_edev = ip->i_dev;
2897c478bd9Sstevel@tonic-gate 	bp->b_lblkno = btodt(offset);
2907c478bd9Sstevel@tonic-gate 	bp->b_bcount = nbytes;
2917c478bd9Sstevel@tonic-gate 	bp->b_un.b_addr = addr;
2927c478bd9Sstevel@tonic-gate 	bp->b_proc = procp;
293408aef6aSbatschul 	bp->b_file = ip->i_vnode;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	/*
2967c478bd9Sstevel@tonic-gate 	 * Note that S_WRITE implies B_READ and vice versa: a read(2)
2977c478bd9Sstevel@tonic-gate 	 * will B_READ data from the filesystem and S_WRITE it into
2987c478bd9Sstevel@tonic-gate 	 * the user's buffer; a write(2) will S_READ data from the
2997c478bd9Sstevel@tonic-gate 	 * user's buffer and B_WRITE it to the filesystem.
3007c478bd9Sstevel@tonic-gate 	 */
3017c478bd9Sstevel@tonic-gate 	if (rw == S_WRITE) {
3027c478bd9Sstevel@tonic-gate 		bp->b_flags = B_BUSY | B_PHYS | B_READ;
303d6767ee3Srshoaib 		ufs_directio_kstats.phys_reads.value.ui64++;
304d6767ee3Srshoaib 		ufs_directio_kstats.nread.value.ui64 += nbytes;
3057c478bd9Sstevel@tonic-gate 	} else {
3067c478bd9Sstevel@tonic-gate 		bp->b_flags = B_BUSY | B_PHYS | B_WRITE;
307d6767ee3Srshoaib 		ufs_directio_kstats.phys_writes.value.ui64++;
308d6767ee3Srshoaib 		ufs_directio_kstats.nwritten.value.ui64 += nbytes;
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 	bp->b_shadow = pplist;
3117c478bd9Sstevel@tonic-gate 	if (pplist != NULL)
3127c478bd9Sstevel@tonic-gate 		bp->b_flags |= B_SHADOW;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	/*
3157c478bd9Sstevel@tonic-gate 	 * Issue I/O request.
3167c478bd9Sstevel@tonic-gate 	 */
317d3d50737SRafael Vanoni 	ufsvfsp->vfs_iotstamp = ddi_get_lbolt();
3187c478bd9Sstevel@tonic-gate 	if (ufsvfsp->vfs_snapshot)
3197c478bd9Sstevel@tonic-gate 		fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
3207c478bd9Sstevel@tonic-gate 	else
3217c478bd9Sstevel@tonic-gate 		(void) bdev_strategy(bp);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	if (rw == S_WRITE)
3247c478bd9Sstevel@tonic-gate 		lwp_stat_update(LWP_STAT_OUBLK, 1);
3257c478bd9Sstevel@tonic-gate 	else
3267c478bd9Sstevel@tonic-gate 		lwp_stat_update(LWP_STAT_INBLK, 1);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate uint32_t	ufs_shared_writes;	/* writes done w/ lock shared */
3317c478bd9Sstevel@tonic-gate uint32_t	ufs_cur_writes;		/* # concurrent writes */
3327c478bd9Sstevel@tonic-gate uint32_t	ufs_maxcur_writes;	/* high water concurrent writes */
3337c478bd9Sstevel@tonic-gate uint32_t	ufs_posix_hits;		/* writes done /w lock excl. */
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate /*
3367c478bd9Sstevel@tonic-gate  * Force POSIX syncronous data integrity on all writes for testing.
3377c478bd9Sstevel@tonic-gate  */
3387c478bd9Sstevel@tonic-gate uint32_t	ufs_force_posix_sdi = 0;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate /*
3417c478bd9Sstevel@tonic-gate  * Direct Write
3427c478bd9Sstevel@tonic-gate  */
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate int
ufs_directio_write(struct inode * ip,uio_t * arg_uio,int ioflag,int rewrite,cred_t * cr,int * statusp)3457c478bd9Sstevel@tonic-gate ufs_directio_write(struct inode *ip, uio_t *arg_uio, int ioflag, int rewrite,
3467c478bd9Sstevel@tonic-gate 	cred_t *cr, int *statusp)
3477c478bd9Sstevel@tonic-gate {
3487c478bd9Sstevel@tonic-gate 	long		resid, bytes_written;
3497c478bd9Sstevel@tonic-gate 	u_offset_t	size, uoff;
3507c478bd9Sstevel@tonic-gate 	uio_t		*uio = arg_uio;
3517c478bd9Sstevel@tonic-gate 	rlim64_t	limit = uio->uio_llimit;
3527c478bd9Sstevel@tonic-gate 	int		on, n, error, newerror, len, has_holes;
3537c478bd9Sstevel@tonic-gate 	daddr_t		bn;
3547c478bd9Sstevel@tonic-gate 	size_t		nbytes;
3557c478bd9Sstevel@tonic-gate 	struct fs	*fs;
3567c478bd9Sstevel@tonic-gate 	vnode_t		*vp;
3577c478bd9Sstevel@tonic-gate 	iovec_t		*iov;
3587c478bd9Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = ip->i_ufsvfs;
3597c478bd9Sstevel@tonic-gate 	struct proc	*procp;
3607c478bd9Sstevel@tonic-gate 	struct as	*as;
3617c478bd9Sstevel@tonic-gate 	struct directio_buf	*tail;
3627c478bd9Sstevel@tonic-gate 	int		exclusive, ncur, bmap_peek;
3637c478bd9Sstevel@tonic-gate 	uio_t		copy_uio;
3647c478bd9Sstevel@tonic-gate 	iovec_t		copy_iov;
3657c478bd9Sstevel@tonic-gate 	char		*copy_base;
3667c478bd9Sstevel@tonic-gate 	long		copy_resid;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	/*
3697c478bd9Sstevel@tonic-gate 	 * assume that directio isn't possible (normal case)
3707c478bd9Sstevel@tonic-gate 	 */
3717c478bd9Sstevel@tonic-gate 	*statusp = DIRECTIO_FAILURE;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	/*
3747c478bd9Sstevel@tonic-gate 	 * Don't go direct
3757c478bd9Sstevel@tonic-gate 	 */
3767c478bd9Sstevel@tonic-gate 	if (ufs_directio_enabled == 0)
3777c478bd9Sstevel@tonic-gate 		return (0);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	/*
3807c478bd9Sstevel@tonic-gate 	 * mapped file; nevermind
3817c478bd9Sstevel@tonic-gate 	 */
3827c478bd9Sstevel@tonic-gate 	if (ip->i_mapcnt)
3837c478bd9Sstevel@tonic-gate 		return (0);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	/*
3867c478bd9Sstevel@tonic-gate 	 * CAN WE DO DIRECT IO?
3877c478bd9Sstevel@tonic-gate 	 */
3887c478bd9Sstevel@tonic-gate 	uoff = uio->uio_loffset;
3897c478bd9Sstevel@tonic-gate 	resid = uio->uio_resid;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	/*
3927c478bd9Sstevel@tonic-gate 	 * beyond limit
3937c478bd9Sstevel@tonic-gate 	 */
3947c478bd9Sstevel@tonic-gate 	if (uoff + resid > limit)
3957c478bd9Sstevel@tonic-gate 		return (0);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	/*
3987c478bd9Sstevel@tonic-gate 	 * must be sector aligned
3997c478bd9Sstevel@tonic-gate 	 */
4007c478bd9Sstevel@tonic-gate 	if ((uoff & (u_offset_t)(DEV_BSIZE - 1)) || (resid & (DEV_BSIZE - 1)))
4017c478bd9Sstevel@tonic-gate 		return (0);
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	/*
4047c478bd9Sstevel@tonic-gate 	 * SHOULD WE DO DIRECT IO?
4057c478bd9Sstevel@tonic-gate 	 */
4067c478bd9Sstevel@tonic-gate 	size = ip->i_size;
4077c478bd9Sstevel@tonic-gate 	has_holes = -1;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	/*
4107c478bd9Sstevel@tonic-gate 	 * only on regular files; no metadata
4117c478bd9Sstevel@tonic-gate 	 */
4127c478bd9Sstevel@tonic-gate 	if (((ip->i_mode & IFMT) != IFREG) || ip->i_ufsvfs->vfs_qinod == ip)
4137c478bd9Sstevel@tonic-gate 		return (0);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	/*
4167c478bd9Sstevel@tonic-gate 	 * Synchronous, allocating writes run very slow in Direct-Mode
4177c478bd9Sstevel@tonic-gate 	 * 	XXX - can be fixed with bmap_write changes for large writes!!!
4187c478bd9Sstevel@tonic-gate 	 *	XXX - can be fixed for updates to "almost-full" files
4197c478bd9Sstevel@tonic-gate 	 *	XXX - WARNING - system hangs if bmap_write() has to
4207c478bd9Sstevel@tonic-gate 	 * 			allocate lots of pages since pageout
4217c478bd9Sstevel@tonic-gate 	 * 			suspends on locked inode
4227c478bd9Sstevel@tonic-gate 	 */
4237c478bd9Sstevel@tonic-gate 	if (!rewrite && (ip->i_flag & ISYNC)) {
4247c478bd9Sstevel@tonic-gate 		if ((uoff + resid) > size)
4257c478bd9Sstevel@tonic-gate 			return (0);
4267c478bd9Sstevel@tonic-gate 		has_holes = bmap_has_holes(ip);
4277c478bd9Sstevel@tonic-gate 		if (has_holes)
4287c478bd9Sstevel@tonic-gate 			return (0);
4297c478bd9Sstevel@tonic-gate 	}
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	/*
4327c478bd9Sstevel@tonic-gate 	 * Each iovec must be short aligned and sector aligned.  If
4337c478bd9Sstevel@tonic-gate 	 * one is not, then kmem_alloc a new buffer and copy all of
4347c478bd9Sstevel@tonic-gate 	 * the smaller buffers into the new buffer.  This new
4357c478bd9Sstevel@tonic-gate 	 * buffer will be short aligned and sector aligned.
4367c478bd9Sstevel@tonic-gate 	 */
4377c478bd9Sstevel@tonic-gate 	iov = uio->uio_iov;
4387c478bd9Sstevel@tonic-gate 	nbytes = uio->uio_iovcnt;
4397c478bd9Sstevel@tonic-gate 	while (nbytes--) {
4407c478bd9Sstevel@tonic-gate 		if (((uint_t)iov->iov_len & (DEV_BSIZE - 1)) != 0 ||
4417c478bd9Sstevel@tonic-gate 		    (intptr_t)(iov->iov_base) & 1) {
4427c478bd9Sstevel@tonic-gate 			copy_resid = uio->uio_resid;
4437c478bd9Sstevel@tonic-gate 			copy_base = kmem_alloc(copy_resid, KM_NOSLEEP);
4447c478bd9Sstevel@tonic-gate 			if (copy_base == NULL)
4457c478bd9Sstevel@tonic-gate 				return (0);
4467c478bd9Sstevel@tonic-gate 			copy_iov.iov_base = copy_base;
4477c478bd9Sstevel@tonic-gate 			copy_iov.iov_len = copy_resid;
4487c478bd9Sstevel@tonic-gate 			copy_uio.uio_iov = &copy_iov;
4497c478bd9Sstevel@tonic-gate 			copy_uio.uio_iovcnt = 1;
4507c478bd9Sstevel@tonic-gate 			copy_uio.uio_segflg = UIO_SYSSPACE;
4517c478bd9Sstevel@tonic-gate 			copy_uio.uio_extflg = UIO_COPY_DEFAULT;
4527c478bd9Sstevel@tonic-gate 			copy_uio.uio_loffset = uio->uio_loffset;
4537c478bd9Sstevel@tonic-gate 			copy_uio.uio_resid = uio->uio_resid;
4547c478bd9Sstevel@tonic-gate 			copy_uio.uio_llimit = uio->uio_llimit;
4557c478bd9Sstevel@tonic-gate 			error = uiomove(copy_base, copy_resid, UIO_WRITE, uio);
4567c478bd9Sstevel@tonic-gate 			if (error) {
4577c478bd9Sstevel@tonic-gate 				kmem_free(copy_base, copy_resid);
4587c478bd9Sstevel@tonic-gate 				return (0);
4597c478bd9Sstevel@tonic-gate 			}
4607c478bd9Sstevel@tonic-gate 			uio = &copy_uio;
4617c478bd9Sstevel@tonic-gate 			break;
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 		iov++;
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	/*
4677c478bd9Sstevel@tonic-gate 	 * From here on down, all error exits must go to errout and
4687c478bd9Sstevel@tonic-gate 	 * not simply return a 0.
4697c478bd9Sstevel@tonic-gate 	 */
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	/*
4727c478bd9Sstevel@tonic-gate 	 * DIRECTIO
4737c478bd9Sstevel@tonic-gate 	 */
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	fs = ip->i_fs;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/*
4787c478bd9Sstevel@tonic-gate 	 * POSIX check. If attempting a concurrent re-write, make sure
4797c478bd9Sstevel@tonic-gate 	 * that this will be a single request to the driver to meet
4807c478bd9Sstevel@tonic-gate 	 * POSIX synchronous data integrity requirements.
4817c478bd9Sstevel@tonic-gate 	 */
4827c478bd9Sstevel@tonic-gate 	bmap_peek = 0;
4837c478bd9Sstevel@tonic-gate 	if (rewrite && ((ioflag & FDSYNC) || ufs_force_posix_sdi)) {
4847c478bd9Sstevel@tonic-gate 		int upgrade = 0;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 		/* check easy conditions first */
4877c478bd9Sstevel@tonic-gate 		if (uio->uio_iovcnt != 1 || resid > ufsvfsp->vfs_ioclustsz) {
4887c478bd9Sstevel@tonic-gate 			upgrade = 1;
4897c478bd9Sstevel@tonic-gate 		} else {
4907c478bd9Sstevel@tonic-gate 			/* now look for contiguous allocation */
4917c478bd9Sstevel@tonic-gate 			len = (ssize_t)blkroundup(fs, resid);
4927c478bd9Sstevel@tonic-gate 			error = bmap_read(ip, uoff, &bn, &len);
4937c478bd9Sstevel@tonic-gate 			if (error || bn == UFS_HOLE || len == 0)
4947c478bd9Sstevel@tonic-gate 				goto errout;
4957c478bd9Sstevel@tonic-gate 			/* save a call to bmap_read later */
4967c478bd9Sstevel@tonic-gate 			bmap_peek = 1;
4977c478bd9Sstevel@tonic-gate 			if (len < resid)
4987c478bd9Sstevel@tonic-gate 				upgrade = 1;
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate 		if (upgrade) {
5017c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
5027c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_WRITER);
5037c478bd9Sstevel@tonic-gate 			ufs_posix_hits++;
5047c478bd9Sstevel@tonic-gate 		}
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	/*
5097c478bd9Sstevel@tonic-gate 	 * allocate space
5107c478bd9Sstevel@tonic-gate 	 */
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	/*
5137c478bd9Sstevel@tonic-gate 	 * If attempting a re-write, there is no allocation to do.
5147c478bd9Sstevel@tonic-gate 	 * bmap_write would trip an ASSERT if i_contents is held shared.
5157c478bd9Sstevel@tonic-gate 	 */
5167c478bd9Sstevel@tonic-gate 	if (rewrite)
5177c478bd9Sstevel@tonic-gate 		goto skip_alloc;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	do {
5207c478bd9Sstevel@tonic-gate 		on = (int)blkoff(fs, uoff);
5217c478bd9Sstevel@tonic-gate 		n = (int)MIN(fs->fs_bsize - on, resid);
5227c478bd9Sstevel@tonic-gate 		if ((uoff + n) > ip->i_size) {
5237c478bd9Sstevel@tonic-gate 			error = bmap_write(ip, uoff, (int)(on + n),
5247c478bd9Sstevel@tonic-gate 			    (int)(uoff & (offset_t)MAXBOFFSET) == 0,
525303bf60bSsdebnath 			    NULL, cr);
5267c478bd9Sstevel@tonic-gate 			/* Caller is responsible for updating i_seq if needed */
5277c478bd9Sstevel@tonic-gate 			if (error)
5287c478bd9Sstevel@tonic-gate 				break;
5297c478bd9Sstevel@tonic-gate 			ip->i_size = uoff + n;
5307c478bd9Sstevel@tonic-gate 			ip->i_flag |= IATTCHG;
5317c478bd9Sstevel@tonic-gate 		} else if (n == MAXBSIZE) {
532303bf60bSsdebnath 			error = bmap_write(ip, uoff, (int)(on + n),
533303bf60bSsdebnath 			    BI_ALLOC_ONLY, NULL, cr);
5347c478bd9Sstevel@tonic-gate 			/* Caller is responsible for updating i_seq if needed */
5357c478bd9Sstevel@tonic-gate 		} else {
5367c478bd9Sstevel@tonic-gate 			if (has_holes < 0)
5377c478bd9Sstevel@tonic-gate 				has_holes = bmap_has_holes(ip);
5387c478bd9Sstevel@tonic-gate 			if (has_holes) {
5397c478bd9Sstevel@tonic-gate 				uint_t	blk_size;
5407c478bd9Sstevel@tonic-gate 				u_offset_t offset;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 				offset = uoff & (offset_t)fs->fs_bmask;
5437c478bd9Sstevel@tonic-gate 				blk_size = (int)blksize(fs, ip,
5447c478bd9Sstevel@tonic-gate 				    (daddr_t)lblkno(fs, offset));
545303bf60bSsdebnath 				error = bmap_write(ip, uoff, blk_size,
546303bf60bSsdebnath 				    BI_NORMAL, NULL, cr);
5477c478bd9Sstevel@tonic-gate 				/*
5487c478bd9Sstevel@tonic-gate 				 * Caller is responsible for updating
5497c478bd9Sstevel@tonic-gate 				 * i_seq if needed
5507c478bd9Sstevel@tonic-gate 				 */
5517c478bd9Sstevel@tonic-gate 			} else
5527c478bd9Sstevel@tonic-gate 				error = 0;
5537c478bd9Sstevel@tonic-gate 		}
5547c478bd9Sstevel@tonic-gate 		if (error)
5557c478bd9Sstevel@tonic-gate 			break;
5567c478bd9Sstevel@tonic-gate 		uoff += n;
5577c478bd9Sstevel@tonic-gate 		resid -= n;
5587c478bd9Sstevel@tonic-gate 		/*
5597c478bd9Sstevel@tonic-gate 		 * if file has grown larger than 2GB, set flag
5607c478bd9Sstevel@tonic-gate 		 * in superblock if not already set
5617c478bd9Sstevel@tonic-gate 		 */
5627c478bd9Sstevel@tonic-gate 		if ((ip->i_size > MAXOFF32_T) &&
5637c478bd9Sstevel@tonic-gate 		    !(fs->fs_flags & FSLARGEFILES)) {
5647c478bd9Sstevel@tonic-gate 			ASSERT(ufsvfsp->vfs_lfflags & UFS_LARGEFILES);
5657c478bd9Sstevel@tonic-gate 			mutex_enter(&ufsvfsp->vfs_lock);
5667c478bd9Sstevel@tonic-gate 			fs->fs_flags |= FSLARGEFILES;
5677c478bd9Sstevel@tonic-gate 			ufs_sbwrite(ufsvfsp);
5687c478bd9Sstevel@tonic-gate 			mutex_exit(&ufsvfsp->vfs_lock);
5697c478bd9Sstevel@tonic-gate 		}
5707c478bd9Sstevel@tonic-gate 	} while (resid);
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	if (error) {
5737c478bd9Sstevel@tonic-gate 		/*
5747c478bd9Sstevel@tonic-gate 		 * restore original state
5757c478bd9Sstevel@tonic-gate 		 */
5767c478bd9Sstevel@tonic-gate 		if (resid) {
5777c478bd9Sstevel@tonic-gate 			if (size == ip->i_size)
5787c478bd9Sstevel@tonic-gate 				goto errout;
5797c478bd9Sstevel@tonic-gate 			(void) ufs_itrunc(ip, size, 0, cr);
5807c478bd9Sstevel@tonic-gate 		}
5817c478bd9Sstevel@tonic-gate 		/*
5827c478bd9Sstevel@tonic-gate 		 * try non-directio path
5837c478bd9Sstevel@tonic-gate 		 */
5847c478bd9Sstevel@tonic-gate 		goto errout;
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate skip_alloc:
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	/*
5897c478bd9Sstevel@tonic-gate 	 * get rid of cached pages
5907c478bd9Sstevel@tonic-gate 	 */
5917c478bd9Sstevel@tonic-gate 	vp = ITOV(ip);
5927c478bd9Sstevel@tonic-gate 	exclusive = rw_write_held(&ip->i_contents);
5937c478bd9Sstevel@tonic-gate 	if (vn_has_cached_data(vp)) {
5947c478bd9Sstevel@tonic-gate 		if (!exclusive) {
5957c478bd9Sstevel@tonic-gate 			/*
5967c478bd9Sstevel@tonic-gate 			 * Still holding i_rwlock, so no allocations
5977c478bd9Sstevel@tonic-gate 			 * can happen after dropping contents.
5987c478bd9Sstevel@tonic-gate 			 */
5997c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
6007c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_WRITER);
6017c478bd9Sstevel@tonic-gate 		}
602da6c28aaSamw 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0,
603da6c28aaSamw 		    B_INVAL, cr, NULL);
6047c478bd9Sstevel@tonic-gate 		if (vn_has_cached_data(vp))
6057c478bd9Sstevel@tonic-gate 			goto errout;
6067c478bd9Sstevel@tonic-gate 		if (!exclusive)
6077c478bd9Sstevel@tonic-gate 			rw_downgrade(&ip->i_contents);
608d6767ee3Srshoaib 		ufs_directio_kstats.nflushes.value.ui64++;
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	/*
6127c478bd9Sstevel@tonic-gate 	 * Direct Writes
6137c478bd9Sstevel@tonic-gate 	 */
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	if (!exclusive) {
6167c478bd9Sstevel@tonic-gate 		ufs_shared_writes++;
617*1a5e258fSJosef 'Jeff' Sipek 		ncur = atomic_inc_32_nv(&ufs_cur_writes);
6187c478bd9Sstevel@tonic-gate 		if (ncur > ufs_maxcur_writes)
6197c478bd9Sstevel@tonic-gate 			ufs_maxcur_writes = ncur;
6207c478bd9Sstevel@tonic-gate 	}
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	/*
6237c478bd9Sstevel@tonic-gate 	 * proc and as are for VM operations in directio_start()
6247c478bd9Sstevel@tonic-gate 	 */
6257c478bd9Sstevel@tonic-gate 	if (uio->uio_segflg == UIO_USERSPACE) {
6267c478bd9Sstevel@tonic-gate 		procp = ttoproc(curthread);
6277c478bd9Sstevel@tonic-gate 		as = procp->p_as;
6287c478bd9Sstevel@tonic-gate 	} else {
6297c478bd9Sstevel@tonic-gate 		procp = NULL;
6307c478bd9Sstevel@tonic-gate 		as = &kas;
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate 	*statusp = DIRECTIO_SUCCESS;
6337c478bd9Sstevel@tonic-gate 	error = 0;
6347c478bd9Sstevel@tonic-gate 	newerror = 0;
6357c478bd9Sstevel@tonic-gate 	resid = uio->uio_resid;
6367c478bd9Sstevel@tonic-gate 	bytes_written = 0;
637d6767ee3Srshoaib 	ufs_directio_kstats.logical_writes.value.ui64++;
6387c478bd9Sstevel@tonic-gate 	while (error == 0 && newerror == 0 && resid && uio->uio_iovcnt) {
6397c478bd9Sstevel@tonic-gate 		size_t pglck_len, pglck_size;
6407c478bd9Sstevel@tonic-gate 		caddr_t pglck_base;
6417c478bd9Sstevel@tonic-gate 		page_t **pplist, **spplist;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 		tail = NULL;
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		/*
6467c478bd9Sstevel@tonic-gate 		 * Adjust number of bytes
6477c478bd9Sstevel@tonic-gate 		 */
6487c478bd9Sstevel@tonic-gate 		iov = uio->uio_iov;
6497c478bd9Sstevel@tonic-gate 		pglck_len = (size_t)MIN(iov->iov_len, resid);
6507c478bd9Sstevel@tonic-gate 		pglck_base = iov->iov_base;
6517c478bd9Sstevel@tonic-gate 		if (pglck_len == 0) {
6527c478bd9Sstevel@tonic-gate 			uio->uio_iov++;
6537c478bd9Sstevel@tonic-gate 			uio->uio_iovcnt--;
6547c478bd9Sstevel@tonic-gate 			continue;
6557c478bd9Sstevel@tonic-gate 		}
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 		/*
6587c478bd9Sstevel@tonic-gate 		 * Try to Lock down the largest chunck of pages possible.
6597c478bd9Sstevel@tonic-gate 		 */
6607c478bd9Sstevel@tonic-gate 		pglck_len = (size_t)MIN(pglck_len,  ufsvfsp->vfs_ioclustsz);
6617c478bd9Sstevel@tonic-gate 		error = as_pagelock(as, &pplist, pglck_base, pglck_len, S_READ);
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 		if (error)
6647c478bd9Sstevel@tonic-gate 			break;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 		pglck_size = pglck_len;
6677c478bd9Sstevel@tonic-gate 		while (pglck_len) {
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 			nbytes = pglck_len;
6707c478bd9Sstevel@tonic-gate 			uoff = uio->uio_loffset;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 			if (!bmap_peek) {
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 				/*
6757c478bd9Sstevel@tonic-gate 				 * Re-adjust number of bytes to contiguous
6767c478bd9Sstevel@tonic-gate 				 * range. May have already called bmap_read
6777c478bd9Sstevel@tonic-gate 				 * in the case of a concurrent rewrite.
6787c478bd9Sstevel@tonic-gate 				 */
6797c478bd9Sstevel@tonic-gate 				len = (ssize_t)blkroundup(fs, nbytes);
6807c478bd9Sstevel@tonic-gate 				error = bmap_read(ip, uoff, &bn, &len);
6817c478bd9Sstevel@tonic-gate 				if (error)
6827c478bd9Sstevel@tonic-gate 					break;
6837c478bd9Sstevel@tonic-gate 				if (bn == UFS_HOLE || len == 0)
6847c478bd9Sstevel@tonic-gate 					break;
6857c478bd9Sstevel@tonic-gate 			}
6867c478bd9Sstevel@tonic-gate 			nbytes = (size_t)MIN(nbytes, len);
6877c478bd9Sstevel@tonic-gate 			bmap_peek = 0;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 			/*
6907c478bd9Sstevel@tonic-gate 			 * Get the pagelist pointer for this offset to be
6917c478bd9Sstevel@tonic-gate 			 * passed to directio_start.
6927c478bd9Sstevel@tonic-gate 			 */
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 			if (pplist != NULL)
6957c478bd9Sstevel@tonic-gate 				spplist = pplist +
6967c478bd9Sstevel@tonic-gate 				    btop((uintptr_t)iov->iov_base -
6977c478bd9Sstevel@tonic-gate 				    ((uintptr_t)pglck_base & PAGEMASK));
6987c478bd9Sstevel@tonic-gate 			else
6997c478bd9Sstevel@tonic-gate 				spplist = NULL;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 			/*
7027c478bd9Sstevel@tonic-gate 			 * Kick off the direct write requests
7037c478bd9Sstevel@tonic-gate 			 */
704408aef6aSbatschul 			directio_start(ufsvfsp, ip, nbytes, ldbtob(bn),
7057c478bd9Sstevel@tonic-gate 			    iov->iov_base, S_READ, procp, &tail, spplist);
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 			/*
7087c478bd9Sstevel@tonic-gate 			 * Adjust pointers and counters
7097c478bd9Sstevel@tonic-gate 			 */
7107c478bd9Sstevel@tonic-gate 			iov->iov_len -= nbytes;
7117c478bd9Sstevel@tonic-gate 			iov->iov_base += nbytes;
7127c478bd9Sstevel@tonic-gate 			uio->uio_loffset += nbytes;
7137c478bd9Sstevel@tonic-gate 			resid -= nbytes;
7147c478bd9Sstevel@tonic-gate 			pglck_len -= nbytes;
7157c478bd9Sstevel@tonic-gate 		}
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 		/*
7187c478bd9Sstevel@tonic-gate 		 * Wait for outstanding requests
7197c478bd9Sstevel@tonic-gate 		 */
7207c478bd9Sstevel@tonic-gate 		newerror = directio_wait(tail, &bytes_written);
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 		/*
7237c478bd9Sstevel@tonic-gate 		 * Release VM resources
7247c478bd9Sstevel@tonic-gate 		 */
7257c478bd9Sstevel@tonic-gate 		as_pageunlock(as, pplist, pglck_base, pglck_size, S_READ);
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	if (!exclusive) {
730*1a5e258fSJosef 'Jeff' Sipek 		atomic_dec_32(&ufs_cur_writes);
7317c478bd9Sstevel@tonic-gate 		/*
7327c478bd9Sstevel@tonic-gate 		 * If this write was done shared, readers may
7337c478bd9Sstevel@tonic-gate 		 * have pulled in unmodified pages. Get rid of
7347c478bd9Sstevel@tonic-gate 		 * these potentially stale pages.
7357c478bd9Sstevel@tonic-gate 		 */
7367c478bd9Sstevel@tonic-gate 		if (vn_has_cached_data(vp)) {
7377c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
7387c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_WRITER);
7397c478bd9Sstevel@tonic-gate 			(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0,
740da6c28aaSamw 			    B_INVAL, cr, NULL);
741d6767ee3Srshoaib 			ufs_directio_kstats.nflushes.value.ui64++;
7427c478bd9Sstevel@tonic-gate 			rw_downgrade(&ip->i_contents);
7437c478bd9Sstevel@tonic-gate 		}
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	/*
7477c478bd9Sstevel@tonic-gate 	 * If error, adjust resid to begin at the first
7487c478bd9Sstevel@tonic-gate 	 * un-writable byte.
7497c478bd9Sstevel@tonic-gate 	 */
7507c478bd9Sstevel@tonic-gate 	if (error == 0)
7517c478bd9Sstevel@tonic-gate 		error = newerror;
7527c478bd9Sstevel@tonic-gate 	if (error)
7537c478bd9Sstevel@tonic-gate 		resid = uio->uio_resid - bytes_written;
7547c478bd9Sstevel@tonic-gate 	arg_uio->uio_resid = resid;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	if (!rewrite) {
7577c478bd9Sstevel@tonic-gate 		ip->i_flag |= IUPD | ICHG;
7587c478bd9Sstevel@tonic-gate 		/* Caller will update i_seq */
7597c478bd9Sstevel@tonic-gate 		TRANS_INODE(ip->i_ufsvfs, ip);
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate 	/*
7627c478bd9Sstevel@tonic-gate 	 * If there is a residual; adjust the EOF if necessary
7637c478bd9Sstevel@tonic-gate 	 */
7647c478bd9Sstevel@tonic-gate 	if (resid) {
7657c478bd9Sstevel@tonic-gate 		if (size != ip->i_size) {
7667c478bd9Sstevel@tonic-gate 			if (uio->uio_loffset > size)
7677c478bd9Sstevel@tonic-gate 				size = uio->uio_loffset;
7687c478bd9Sstevel@tonic-gate 			(void) ufs_itrunc(ip, size, 0, cr);
7697c478bd9Sstevel@tonic-gate 		}
7707c478bd9Sstevel@tonic-gate 	}
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	if (uio == &copy_uio)
7737c478bd9Sstevel@tonic-gate 		kmem_free(copy_base, copy_resid);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	return (error);
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate errout:
7787c478bd9Sstevel@tonic-gate 	if (uio == &copy_uio)
7797c478bd9Sstevel@tonic-gate 		kmem_free(copy_base, copy_resid);
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	return (0);
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate /*
7847c478bd9Sstevel@tonic-gate  * Direct read of a hole
7857c478bd9Sstevel@tonic-gate  */
7867c478bd9Sstevel@tonic-gate static int
directio_hole(struct uio * uio,size_t nbytes)7877c478bd9Sstevel@tonic-gate directio_hole(struct uio *uio, size_t nbytes)
7887c478bd9Sstevel@tonic-gate {
7897c478bd9Sstevel@tonic-gate 	int		error = 0, nzero;
7907c478bd9Sstevel@tonic-gate 	uio_t		phys_uio;
7917c478bd9Sstevel@tonic-gate 	iovec_t		phys_iov;
7927c478bd9Sstevel@tonic-gate 
793d6767ee3Srshoaib 	ufs_directio_kstats.hole_reads.value.ui64++;
794d6767ee3Srshoaib 	ufs_directio_kstats.nread.value.ui64 += nbytes;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	phys_iov.iov_base = uio->uio_iov->iov_base;
7977c478bd9Sstevel@tonic-gate 	phys_iov.iov_len = nbytes;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	phys_uio.uio_iov = &phys_iov;
8007c478bd9Sstevel@tonic-gate 	phys_uio.uio_iovcnt = 1;
8017c478bd9Sstevel@tonic-gate 	phys_uio.uio_resid = phys_iov.iov_len;
8027c478bd9Sstevel@tonic-gate 	phys_uio.uio_segflg = uio->uio_segflg;
8037c478bd9Sstevel@tonic-gate 	phys_uio.uio_extflg = uio->uio_extflg;
8047c478bd9Sstevel@tonic-gate 	while (error == 0 && phys_uio.uio_resid) {
8057c478bd9Sstevel@tonic-gate 		nzero = (int)MIN(phys_iov.iov_len, ufs_directio_zero_len);
8067c478bd9Sstevel@tonic-gate 		error = uiomove(ufs_directio_zero_buf, nzero, UIO_READ,
8077c478bd9Sstevel@tonic-gate 		    &phys_uio);
8087c478bd9Sstevel@tonic-gate 	}
8097c478bd9Sstevel@tonic-gate 	return (error);
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate /*
8137c478bd9Sstevel@tonic-gate  * Direct Read
8147c478bd9Sstevel@tonic-gate  */
8157c478bd9Sstevel@tonic-gate int
ufs_directio_read(struct inode * ip,uio_t * uio,cred_t * cr,int * statusp)8167c478bd9Sstevel@tonic-gate ufs_directio_read(struct inode *ip, uio_t *uio, cred_t *cr, int *statusp)
8177c478bd9Sstevel@tonic-gate {
8187c478bd9Sstevel@tonic-gate 	ssize_t		resid, bytes_read;
8197c478bd9Sstevel@tonic-gate 	u_offset_t	size, uoff;
8207c478bd9Sstevel@tonic-gate 	int		error, newerror, len;
8217c478bd9Sstevel@tonic-gate 	size_t		nbytes;
8227c478bd9Sstevel@tonic-gate 	struct fs	*fs;
8237c478bd9Sstevel@tonic-gate 	vnode_t		*vp;
8247c478bd9Sstevel@tonic-gate 	daddr_t		bn;
8257c478bd9Sstevel@tonic-gate 	iovec_t		*iov;
8267c478bd9Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = ip->i_ufsvfs;
8277c478bd9Sstevel@tonic-gate 	struct proc	*procp;
8287c478bd9Sstevel@tonic-gate 	struct as	*as;
8297c478bd9Sstevel@tonic-gate 	struct directio_buf	*tail;
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	/*
8327c478bd9Sstevel@tonic-gate 	 * assume that directio isn't possible (normal case)
8337c478bd9Sstevel@tonic-gate 	 */
8347c478bd9Sstevel@tonic-gate 	*statusp = DIRECTIO_FAILURE;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	/*
8377c478bd9Sstevel@tonic-gate 	 * Don't go direct
8387c478bd9Sstevel@tonic-gate 	 */
8397c478bd9Sstevel@tonic-gate 	if (ufs_directio_enabled == 0)
8407c478bd9Sstevel@tonic-gate 		return (0);
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	/*
8437c478bd9Sstevel@tonic-gate 	 * mapped file; nevermind
8447c478bd9Sstevel@tonic-gate 	 */
8457c478bd9Sstevel@tonic-gate 	if (ip->i_mapcnt)
8467c478bd9Sstevel@tonic-gate 		return (0);
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	/*
8497c478bd9Sstevel@tonic-gate 	 * CAN WE DO DIRECT IO?
8507c478bd9Sstevel@tonic-gate 	 */
8517c478bd9Sstevel@tonic-gate 	/*
8527c478bd9Sstevel@tonic-gate 	 * must be sector aligned
8537c478bd9Sstevel@tonic-gate 	 */
8547c478bd9Sstevel@tonic-gate 	uoff = uio->uio_loffset;
8557c478bd9Sstevel@tonic-gate 	resid = uio->uio_resid;
8567c478bd9Sstevel@tonic-gate 	if ((uoff & (u_offset_t)(DEV_BSIZE - 1)) || (resid & (DEV_BSIZE - 1)))
8577c478bd9Sstevel@tonic-gate 		return (0);
8587c478bd9Sstevel@tonic-gate 	/*
8597c478bd9Sstevel@tonic-gate 	 * must be short aligned and sector aligned
8607c478bd9Sstevel@tonic-gate 	 */
8617c478bd9Sstevel@tonic-gate 	iov = uio->uio_iov;
8627c478bd9Sstevel@tonic-gate 	nbytes = uio->uio_iovcnt;
8637c478bd9Sstevel@tonic-gate 	while (nbytes--) {
8647c478bd9Sstevel@tonic-gate 		if (((size_t)iov->iov_len & (DEV_BSIZE - 1)) != 0)
8657c478bd9Sstevel@tonic-gate 			return (0);
8667c478bd9Sstevel@tonic-gate 		if ((intptr_t)(iov++->iov_base) & 1)
8677c478bd9Sstevel@tonic-gate 			return (0);
8687c478bd9Sstevel@tonic-gate 	}
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	/*
8717c478bd9Sstevel@tonic-gate 	 * DIRECTIO
8727c478bd9Sstevel@tonic-gate 	 */
8737c478bd9Sstevel@tonic-gate 	fs = ip->i_fs;
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	/*
8767c478bd9Sstevel@tonic-gate 	 * don't read past EOF
8777c478bd9Sstevel@tonic-gate 	 */
8787c478bd9Sstevel@tonic-gate 	size = ip->i_size;
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	/*
8817c478bd9Sstevel@tonic-gate 	 * The file offset is past EOF so bail out here; we don't want
8827c478bd9Sstevel@tonic-gate 	 * to update uio_resid and make it look like we read something.
8837c478bd9Sstevel@tonic-gate 	 * We say that direct I/O was a success to avoid having rdip()
8847c478bd9Sstevel@tonic-gate 	 * go through the same "read past EOF logic".
8857c478bd9Sstevel@tonic-gate 	 */
8867c478bd9Sstevel@tonic-gate 	if (uoff >= size) {
8877c478bd9Sstevel@tonic-gate 		*statusp = DIRECTIO_SUCCESS;
8887c478bd9Sstevel@tonic-gate 		return (0);
8897c478bd9Sstevel@tonic-gate 	}
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	/*
8927c478bd9Sstevel@tonic-gate 	 * The read would extend past EOF so make it smaller.
8937c478bd9Sstevel@tonic-gate 	 */
8947c478bd9Sstevel@tonic-gate 	if ((uoff + resid) > size) {
8957c478bd9Sstevel@tonic-gate 		resid = size - uoff;
8967c478bd9Sstevel@tonic-gate 		/*
8977c478bd9Sstevel@tonic-gate 		 * recheck sector alignment
8987c478bd9Sstevel@tonic-gate 		 */
8997c478bd9Sstevel@tonic-gate 		if (resid & (DEV_BSIZE - 1))
9007c478bd9Sstevel@tonic-gate 			return (0);
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	/*
9047c478bd9Sstevel@tonic-gate 	 * At this point, we know there is some real work to do.
9057c478bd9Sstevel@tonic-gate 	 */
9067c478bd9Sstevel@tonic-gate 	ASSERT(resid);
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	/*
9097c478bd9Sstevel@tonic-gate 	 * get rid of cached pages
9107c478bd9Sstevel@tonic-gate 	 */
9117c478bd9Sstevel@tonic-gate 	vp = ITOV(ip);
9127c478bd9Sstevel@tonic-gate 	if (vn_has_cached_data(vp)) {
9137c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
9147c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
915da6c28aaSamw 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0,
916da6c28aaSamw 		    B_INVAL, cr, NULL);
9177c478bd9Sstevel@tonic-gate 		if (vn_has_cached_data(vp))
9187c478bd9Sstevel@tonic-gate 			return (0);
9197c478bd9Sstevel@tonic-gate 		rw_downgrade(&ip->i_contents);
920d6767ee3Srshoaib 		ufs_directio_kstats.nflushes.value.ui64++;
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 	/*
9237c478bd9Sstevel@tonic-gate 	 * Direct Reads
9247c478bd9Sstevel@tonic-gate 	 */
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	/*
9277c478bd9Sstevel@tonic-gate 	 * proc and as are for VM operations in directio_start()
9287c478bd9Sstevel@tonic-gate 	 */
9297c478bd9Sstevel@tonic-gate 	if (uio->uio_segflg == UIO_USERSPACE) {
9307c478bd9Sstevel@tonic-gate 		procp = ttoproc(curthread);
9317c478bd9Sstevel@tonic-gate 		as = procp->p_as;
9327c478bd9Sstevel@tonic-gate 	} else {
9337c478bd9Sstevel@tonic-gate 		procp = NULL;
9347c478bd9Sstevel@tonic-gate 		as = &kas;
9357c478bd9Sstevel@tonic-gate 	}
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	*statusp = DIRECTIO_SUCCESS;
9387c478bd9Sstevel@tonic-gate 	error = 0;
9397c478bd9Sstevel@tonic-gate 	newerror = 0;
9407c478bd9Sstevel@tonic-gate 	bytes_read = 0;
941d6767ee3Srshoaib 	ufs_directio_kstats.logical_reads.value.ui64++;
9427c478bd9Sstevel@tonic-gate 	while (error == 0 && newerror == 0 && resid && uio->uio_iovcnt) {
9437c478bd9Sstevel@tonic-gate 		size_t pglck_len, pglck_size;
9447c478bd9Sstevel@tonic-gate 		caddr_t pglck_base;
9457c478bd9Sstevel@tonic-gate 		page_t **pplist, **spplist;
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 		tail = NULL;
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 		/*
9507c478bd9Sstevel@tonic-gate 		 * Adjust number of bytes
9517c478bd9Sstevel@tonic-gate 		 */
9527c478bd9Sstevel@tonic-gate 		iov = uio->uio_iov;
9537c478bd9Sstevel@tonic-gate 		pglck_len = (size_t)MIN(iov->iov_len, resid);
9547c478bd9Sstevel@tonic-gate 		pglck_base = iov->iov_base;
9557c478bd9Sstevel@tonic-gate 		if (pglck_len == 0) {
9567c478bd9Sstevel@tonic-gate 			uio->uio_iov++;
9577c478bd9Sstevel@tonic-gate 			uio->uio_iovcnt--;
9587c478bd9Sstevel@tonic-gate 			continue;
9597c478bd9Sstevel@tonic-gate 		}
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 		/*
9627c478bd9Sstevel@tonic-gate 		 * Try to Lock down the largest chunck of pages possible.
9637c478bd9Sstevel@tonic-gate 		 */
9647c478bd9Sstevel@tonic-gate 		pglck_len = (size_t)MIN(pglck_len,  ufsvfsp->vfs_ioclustsz);
9657c478bd9Sstevel@tonic-gate 		error = as_pagelock(as, &pplist, pglck_base,
9667c478bd9Sstevel@tonic-gate 		    pglck_len, S_WRITE);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 		if (error)
9697c478bd9Sstevel@tonic-gate 			break;
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 		pglck_size = pglck_len;
9727c478bd9Sstevel@tonic-gate 		while (pglck_len) {
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 			nbytes = pglck_len;
9757c478bd9Sstevel@tonic-gate 			uoff = uio->uio_loffset;
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 			/*
9787c478bd9Sstevel@tonic-gate 			 * Re-adjust number of bytes to contiguous range
9797c478bd9Sstevel@tonic-gate 			 */
9807c478bd9Sstevel@tonic-gate 			len = (ssize_t)blkroundup(fs, nbytes);
9817c478bd9Sstevel@tonic-gate 			error = bmap_read(ip, uoff, &bn, &len);
9827c478bd9Sstevel@tonic-gate 			if (error)
9837c478bd9Sstevel@tonic-gate 				break;
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 			if (bn == UFS_HOLE) {
9867c478bd9Sstevel@tonic-gate 				nbytes = (size_t)MIN(fs->fs_bsize -
9877c478bd9Sstevel@tonic-gate 				    (long)blkoff(fs, uoff), nbytes);
9887c478bd9Sstevel@tonic-gate 				error = directio_hole(uio, nbytes);
9897c478bd9Sstevel@tonic-gate 				/*
9907c478bd9Sstevel@tonic-gate 				 * Hole reads are not added to the list
9917c478bd9Sstevel@tonic-gate 				 * processed by directio_wait() below so
9927c478bd9Sstevel@tonic-gate 				 * account for bytes read here.
9937c478bd9Sstevel@tonic-gate 				 */
9947c478bd9Sstevel@tonic-gate 				if (!error)
9957c478bd9Sstevel@tonic-gate 					bytes_read += nbytes;
9967c478bd9Sstevel@tonic-gate 			} else {
9977c478bd9Sstevel@tonic-gate 				nbytes = (size_t)MIN(nbytes, len);
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 				/*
10007c478bd9Sstevel@tonic-gate 				 * Get the pagelist pointer for this offset
10017c478bd9Sstevel@tonic-gate 				 * to be passed to directio_start.
10027c478bd9Sstevel@tonic-gate 				 */
10037c478bd9Sstevel@tonic-gate 				if (pplist != NULL)
10047c478bd9Sstevel@tonic-gate 					spplist = pplist +
10057c478bd9Sstevel@tonic-gate 					    btop((uintptr_t)iov->iov_base -
10067c478bd9Sstevel@tonic-gate 					    ((uintptr_t)pglck_base & PAGEMASK));
10077c478bd9Sstevel@tonic-gate 				else
10087c478bd9Sstevel@tonic-gate 					spplist = NULL;
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 				/*
10117c478bd9Sstevel@tonic-gate 				 * Kick off the direct read requests
10127c478bd9Sstevel@tonic-gate 				 */
1013408aef6aSbatschul 				directio_start(ufsvfsp, ip, nbytes,
10147c478bd9Sstevel@tonic-gate 				    ldbtob(bn), iov->iov_base,
10157c478bd9Sstevel@tonic-gate 				    S_WRITE, procp, &tail, spplist);
10167c478bd9Sstevel@tonic-gate 			}
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 			if (error)
10197c478bd9Sstevel@tonic-gate 				break;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 			/*
10227c478bd9Sstevel@tonic-gate 			 * Adjust pointers and counters
10237c478bd9Sstevel@tonic-gate 			 */
10247c478bd9Sstevel@tonic-gate 			iov->iov_len -= nbytes;
10257c478bd9Sstevel@tonic-gate 			iov->iov_base += nbytes;
10267c478bd9Sstevel@tonic-gate 			uio->uio_loffset += nbytes;
10277c478bd9Sstevel@tonic-gate 			resid -= nbytes;
10287c478bd9Sstevel@tonic-gate 			pglck_len -= nbytes;
10297c478bd9Sstevel@tonic-gate 		}
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 		/*
10327c478bd9Sstevel@tonic-gate 		 * Wait for outstanding requests
10337c478bd9Sstevel@tonic-gate 		 */
10347c478bd9Sstevel@tonic-gate 		newerror = directio_wait(tail, &bytes_read);
10357c478bd9Sstevel@tonic-gate 		/*
10367c478bd9Sstevel@tonic-gate 		 * Release VM resources
10377c478bd9Sstevel@tonic-gate 		 */
10387c478bd9Sstevel@tonic-gate 		as_pageunlock(as, pplist, pglck_base, pglck_size, S_WRITE);
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	}
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	/*
10437c478bd9Sstevel@tonic-gate 	 * If error, adjust resid to begin at the first
10447c478bd9Sstevel@tonic-gate 	 * un-read byte.
10457c478bd9Sstevel@tonic-gate 	 */
10467c478bd9Sstevel@tonic-gate 	if (error == 0)
10477c478bd9Sstevel@tonic-gate 		error = newerror;
10487c478bd9Sstevel@tonic-gate 	uio->uio_resid -= bytes_read;
10497c478bd9Sstevel@tonic-gate 	return (error);
10507c478bd9Sstevel@tonic-gate }
1051