Module Name:    src
Committed By:   manu
Date:           Tue Nov  4 09:14:42 UTC 2014

Modified Files:
        src/sys/fs/puffs: puffs_vnops.c

Log Message:
PUFFS direct I/O cache fix

There are a few situations where we must take care of the cache if direct
I/O was enabled:
- if we do direct I/O for write but not for read, then any write must
  invalidate the cache so that a reader gets the written data and not
  the not-updated cache.
- if we used a vnode without direct I/O and it is enabled for writing,
  we must flush the cache before compeling the open operation, so that
  the cachec write are not lost.

And at inactive time, we wipe direct I/O flags so that a new open without
direct I/O does not inherit direct I/O.


To generate a diff of this commit:
cvs rdiff -u -r1.197 -r1.198 src/sys/fs/puffs/puffs_vnops.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/fs/puffs/puffs_vnops.c
diff -u src/sys/fs/puffs/puffs_vnops.c:1.197 src/sys/fs/puffs/puffs_vnops.c:1.198
--- src/sys/fs/puffs/puffs_vnops.c:1.197	Tue Nov  4 09:10:37 2014
+++ src/sys/fs/puffs/puffs_vnops.c	Tue Nov  4 09:14:42 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: puffs_vnops.c,v 1.197 2014/11/04 09:10:37 manu Exp $	*/
+/*	$NetBSD: puffs_vnops.c,v 1.198 2014/11/04 09:14:42 manu Exp $	*/
 
 /*
  * Copyright (c) 2005, 2006, 2007  Antti Kantee.  All Rights Reserved.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: puffs_vnops.c,v 1.197 2014/11/04 09:10:37 manu Exp $");
+__KERNEL_RCSID(0, "$NetBSD: puffs_vnops.c,v 1.198 2014/11/04 09:14:42 manu Exp $");
 
 #include <sys/param.h>
 #include <sys/buf.h>
@@ -908,6 +908,12 @@ puffs_vnop_open(void *v)
 	error = checkerr(pmp, error, __func__);
 
 	if (open_msg->pvnr_oflags & PUFFS_OPEN_IO_DIRECT) {
+		/*
+		 * Flush cache:
+		 * - we do not want to discard cached write by direct write
+		 * - read cache is now useless and should be freed
+		 */
+		flushvncache(vp, 0, 0, true);
 		if (mode & FREAD)
 			pn->pn_stat |= PNODE_RDIRECT;
 		if (mode & FWRITE)
@@ -1416,6 +1422,11 @@ puffs_vnop_inactive(void *v)
 		}
 	}
 
+	/*
+	 * Wipe direct I/O flags
+	 */
+	pnode->pn_stat &= ~(PNODE_RDIRECT|PNODE_WDIRECT);
+
 	*ap->a_recycle = recycle;
 
 	mutex_exit(&pnode->pn_sizemtx);
@@ -2381,19 +2392,20 @@ puffs_vnop_write(void *v)
 
 	mutex_enter(&pn->pn_sizemtx);
 
+	/*
+	 * userspace *should* be allowed to control this,
+	 * but with UBC it's a bit unclear how to handle it
+	 */
+	if (ap->a_ioflag & IO_APPEND)
+		uio->uio_offset = vp->v_size;
+
+	origoff = uio->uio_offset;
+
 	if (vp->v_type == VREG && 
 	    PUFFS_USE_PAGECACHE(pmp) &&
 	    !(pn->pn_stat & PNODE_WDIRECT)) {
 		ubcflags = UBC_WRITE | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp);
 
-		/*
-		 * userspace *should* be allowed to control this,
-		 * but with UBC it's a bit unclear how to handle it
-		 */
-		if (ap->a_ioflag & IO_APPEND)
-			uio->uio_offset = vp->v_size;
-
-		origoff = uio->uio_offset;
 		while (uio->uio_resid > 0) {
 			oldoff = uio->uio_offset;
 			bytelen = uio->uio_resid;
@@ -2500,6 +2512,22 @@ puffs_vnop_write(void *v)
 			}
 		}
 		puffs_msgmem_release(park_write);
+
+		/*
+		 * Direct I/O on write but not on read: we must
+		 * invlidate the written pages so that we read
+		 * the written data and not the stalled cache.
+		 */
+		if ((error == 0) && 
+		    (vp->v_type == VREG) && PUFFS_USE_PAGECACHE(pmp) &&
+		    (pn->pn_stat & PNODE_WDIRECT) &&
+		    !(pn->pn_stat & PNODE_RDIRECT)) {
+			voff_t off_lo = trunc_page(origoff);
+			voff_t off_hi = round_page(uio->uio_offset);
+
+			mutex_enter(vp->v_uobj.vmobjlock);
+			error = VOP_PUTPAGES(vp, off_lo, off_hi, PGO_FREE);
+		}
 	}
 
 	if (vp->v_mount->mnt_flag & MNT_RELATIME)

Reply via email to