From f6020599aad34903d0a7e1b32181fcd719adbc44 Mon Sep 17 00:00:00 2001
From: Peter Wemm <peter@FreeBSD.org>
Date: Thu, 29 Oct 1998 09:30:52 +0000
Subject: [PATCH] Use vtruncbuf() to clean out cached blocks on a file shorten
 rather than the more expensive vinvalbuf(), based on the FFS version of the
 same routine.  I don't have any ext2fs filesystems to test this on.

---
 sys/gnu/ext2fs/ext2_inode.c    | 7 ++++---
 sys/gnu/fs/ext2fs/ext2_inode.c | 7 ++++---
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/sys/gnu/ext2fs/ext2_inode.c b/sys/gnu/ext2fs/ext2_inode.c
index 4699d4d51dc0..b76410b1a307 100644
--- a/sys/gnu/ext2fs/ext2_inode.c
+++ b/sys/gnu/ext2fs/ext2_inode.c
@@ -142,7 +142,7 @@ ext2_truncate(vp, length, flags, cred, p)
 	register struct ext2_sb_info *fs;
 	struct buf *bp;
 	int offset, size, level;
-	long count, nblocks, vflags, blocksreleased = 0;
+	long count, nblocks, blocksreleased = 0;
 	struct timeval tv;
 	register int i;
 	int aflags, error, allerror;
@@ -268,8 +268,9 @@ printf("ext2_truncate called %d to %d\n", VTOI(ovp)->i_number, length);
 	bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks);
 	bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks);
 	oip->i_size = osize;
-	vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
-	allerror = vinvalbuf(ovp, vflags, cred, p, 0, 0);
+	error = vtruncbuf(ovp, cred, p, length, (int)fs->s_blocksize);
+	if (error && (allerror == 0))
+		allerror = error;
 
 	/*
 	 * Indirect blocks first.
diff --git a/sys/gnu/fs/ext2fs/ext2_inode.c b/sys/gnu/fs/ext2fs/ext2_inode.c
index 4699d4d51dc0..b76410b1a307 100644
--- a/sys/gnu/fs/ext2fs/ext2_inode.c
+++ b/sys/gnu/fs/ext2fs/ext2_inode.c
@@ -142,7 +142,7 @@ ext2_truncate(vp, length, flags, cred, p)
 	register struct ext2_sb_info *fs;
 	struct buf *bp;
 	int offset, size, level;
-	long count, nblocks, vflags, blocksreleased = 0;
+	long count, nblocks, blocksreleased = 0;
 	struct timeval tv;
 	register int i;
 	int aflags, error, allerror;
@@ -268,8 +268,9 @@ printf("ext2_truncate called %d to %d\n", VTOI(ovp)->i_number, length);
 	bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks);
 	bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks);
 	oip->i_size = osize;
-	vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
-	allerror = vinvalbuf(ovp, vflags, cred, p, 0, 0);
+	error = vtruncbuf(ovp, cred, p, length, (int)fs->s_blocksize);
+	if (error && (allerror == 0))
+		allerror = error;
 
 	/*
 	 * Indirect blocks first.