Module Name:    src
Committed By:   martin
Date:           Mon Oct 13 18:57:46 UTC 2014

Modified Files:
        src/sys/fs/puffs [netbsd-7]: puffs_vnops.c

Log Message:
Pull up following revision(s) (requested by manu in ticket #136):
        sys/fs/puffs/puffs_vnops.c: revision 1.189-1.191
If we truncate a file open for writing, make sure we zero-fill the end
of the last page, otherwise if the file is later truncated to a larger
size (creating a hole), that area will not return zeroes as it should.


To generate a diff of this commit:
cvs rdiff -u -r1.182.2.5 -r1.182.2.6 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.182.2.5 src/sys/fs/puffs/puffs_vnops.c:1.182.2.6
--- src/sys/fs/puffs/puffs_vnops.c:1.182.2.5	Tue Sep 30 18:14:22 2014
+++ src/sys/fs/puffs/puffs_vnops.c	Mon Oct 13 18:57:46 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: puffs_vnops.c,v 1.182.2.5 2014/09/30 18:14:22 martin Exp $	*/
+/*	$NetBSD: puffs_vnops.c,v 1.182.2.6 2014/10/13 18:57:46 martin 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.182.2.5 2014/09/30 18:14:22 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: puffs_vnops.c,v 1.182.2.6 2014/10/13 18:57:46 martin Exp $");
 
 #include <sys/param.h>
 #include <sys/buf.h>
@@ -1130,12 +1130,49 @@ puffs_vnop_getattr(void *v)
 	return error;
 }
 
+static void
+zerofill_lastpage(struct vnode *vp, voff_t off)
+{
+	char zbuf[PAGE_SIZE];
+	struct iovec iov;
+	struct uio uio;
+	vsize_t len;
+	int error;
+
+	if (trunc_page(off) == off)
+		return;
+ 
+	if (vp->v_writecount == 0)
+		return;
+
+	len = round_page(off) - off;
+	memset(zbuf, 0, len);
+
+	iov.iov_base = zbuf;
+	iov.iov_len = len;
+	UIO_SETUP_SYSSPACE(&uio);
+	uio.uio_iov = &iov;
+	uio.uio_iovcnt = 1;
+	uio.uio_offset = off;
+	uio.uio_resid = len;
+	uio.uio_rw = UIO_WRITE;
+
+	error = ubc_uiomove(&vp->v_uobj, &uio, len,
+			    UVM_ADV_SEQUENTIAL, UBC_WRITE|UBC_UNMAP_FLAG(vp));
+	if (error)
+		DPRINTF(("zero-fill 0x%lx@0x%" PRIx64 " failed: error = %d\n",
+			 len, off, error));
+
+	return;
+}
+
 static int
 dosetattr(struct vnode *vp, struct vattr *vap, kauth_cred_t cred, int flags)
 {
 	PUFFS_MSG_VARS(vn, setattr);
 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
 	struct puffs_node *pn = vp->v_data;
+	vsize_t oldsize = vp->v_size;
 	int error = 0;
 
 	KASSERT(!(flags & SETATTR_CHSIZE) || mutex_owned(&pn->pn_sizemtx));
@@ -1208,6 +1245,17 @@ dosetattr(struct vnode *vp, struct vattr
 	}
 
 	if (vap->va_size != VNOVAL) {
+		/*
+		 * If we truncated the file, make sure the data beyond 
+		 * EOF in last page does not remain in cache, otherwise 
+		 * if the file is later truncated to a larger size (creating
+		 * a hole), that area will not return zeroes as it
+		 * should. 
+		 */
+		if ((flags & SETATTR_CHSIZE) && PUFFS_USE_PAGECACHE(pmp) && 
+		    (vap->va_size < oldsize))
+			zerofill_lastpage(vp, vap->va_size);
+
 		pn->pn_serversize = vap->va_size;
 		if (flags & SETATTR_CHSIZE)
 			uvm_vnp_setsize(vp, vap->va_size);

Reply via email to