Index: vnd.c
===================================================================
RCS file: /cvsroot/src/sys/dev/vnd.c,v
retrieving revision 1.217
diff -p -u -2 -r1.217 vnd.c
--- vnd.c	12 Jun 2011 03:35:51 -0000	1.217
+++ vnd.c	24 Jun 2011 19:57:27 -0000
@@ -550,5 +550,4 @@ vndthread(void *arg)
 {
 	struct vnd_softc *vnd = arg;
-	bool usestrategy;
 	int s;
 
@@ -558,10 +557,12 @@ vndthread(void *arg)
 	 * Otherwise fall back to regular VOP_READ/VOP_WRITE operations
 	 * which are guaranteed to work with any file system. */
-	usestrategy = vnode_has_strategy(vnd);
+	if ((vnd->sc_flags & VNF_USE_VN_RDWR) == 0 &&
+	    ! vnode_has_strategy(vnd))
+		vnd->sc_flags |= VNF_USE_VN_RDWR;
 
 #ifdef DEBUG
 	if (vnddebug & VDB_INIT)
 		printf("vndthread: vp %p, %s\n", vnd->sc_vp,
-		    usestrategy ?
+		    (vnd->sc_flags & VNF_USE_VN_RDWR) == 0 ?
 		    "using bmap/strategy operations" :
 		    "using read/write operations");
@@ -645,5 +646,5 @@ vndthread(void *arg)
 
 		/* Handle the request using the appropriate operations. */
-		if (usestrategy)
+		if ((vnd->sc_flags & VNF_USE_VN_RDWR) == 0)
 			handle_with_strategy(vnd, obp, bp);
 		else
@@ -697,9 +698,10 @@ handle_with_rdwr(struct vnd_softc *vnd, 
 	bool doread;
 	off_t offset;
-	size_t resid;
+	size_t len, resid;
 	struct vnode *vp;
 
 	doread = bp->b_flags & B_READ;
 	offset = obp->b_rawblkno * vnd->sc_dkdev.dk_label->d_secsize;
+	len = bp->b_bcount;
 	vp = vnd->sc_vp;
 
@@ -717,8 +719,16 @@ handle_with_rdwr(struct vnd_softc *vnd, 
 	bp->b_error =
 	    vn_rdwr(doread ? UIO_READ : UIO_WRITE,
-	    vp, bp->b_data, bp->b_bcount, offset,
-	    UIO_SYSSPACE, 0, vnd->sc_cred, &resid, NULL);
+	    vp, bp->b_data, len, offset, UIO_SYSSPACE,
+	    IO_ADV_ENCODE(POSIX_FADV_NOREUSE), vnd->sc_cred, &resid, NULL);
 	bp->b_resid = resid;
 
+	/* Keep mapped pages below threshold. */
+	mutex_enter(vp->v_interlock);
+	if (vp->v_uobj.uo_npages > 1024*1024 / PAGE_SIZE)
+		(void) VOP_PUTPAGES(vp, 0, 0,
+		    PGO_ALLPAGES | PGO_CLEANIT | PGO_FREE | PGO_SYNCIO);
+	else
+		mutex_exit(vp->v_interlock);
+
 	/* We need to increase the number of outputs on the vnode if
 	 * there was any write to it. */
@@ -1077,6 +1087,6 @@ vndioctl(dev_t dev, u_long cmd, void *da
 			error = EOPNOTSUPP;
 		if (!error && vattr.va_bytes < vattr.va_size)
-			/* File is definitely sparse, reject here */
-			error = EINVAL;
+			/* File is definitely sparse, use vn_rdwr() */
+			vnd->sc_flags |= VNF_USE_VN_RDWR;
 		if (error) {
 			VOP_UNLOCK(nd.ni_vp);
Index: vndvar.h
===================================================================
RCS file: /cvsroot/src/sys/dev/vndvar.h,v
retrieving revision 1.30
diff -p -u -2 -r1.30 vndvar.h
--- vndvar.h	8 Feb 2011 20:20:26 -0000	1.30
+++ vndvar.h	24 Jun 2011 19:57:27 -0000
@@ -143,4 +143,5 @@ struct vnd_softc {
 #define VNF_COMP	0x400	/* file is compressed */
 #define VNF_CLEARING	0x800	/* unit is being torn down */
+#define VNF_USE_VN_RDWR	0x1000	/* have to use vn_rdwr() */
 
 /* structure of header in a compressed file */
