Module Name:    src
Committed By:   slp
Date:           Tue Oct 21 10:39:26 UTC 2014

Modified Files:
        src/sys/ufs/ufs: ufs_extern.h ufs_inode.c ufs_vnops.c

Log Message:
Move and unify indirect block truncate algorithm into a separate function.

Reviewed by joerg.


To generate a diff of this commit:
cvs rdiff -u -r1.75 -r1.76 src/sys/ufs/ufs/ufs_extern.h
cvs rdiff -u -r1.90 -r1.91 src/sys/ufs/ufs/ufs_inode.c
cvs rdiff -u -r1.222 -r1.223 src/sys/ufs/ufs/ufs_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/ufs/ufs/ufs_extern.h
diff -u src/sys/ufs/ufs/ufs_extern.h:1.75 src/sys/ufs/ufs/ufs_extern.h:1.76
--- src/sys/ufs/ufs/ufs_extern.h:1.75	Sun May 25 13:48:40 2014
+++ src/sys/ufs/ufs/ufs_extern.h	Tue Oct 21 10:39:26 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: ufs_extern.h,v 1.75 2014/05/25 13:48:40 hannken Exp $	*/
+/*	$NetBSD: ufs_extern.h,v 1.76 2014/10/21 10:39:26 slp Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993, 1994
@@ -111,6 +111,7 @@ int	ufs_getlbns(struct vnode *, daddr_t,
 /* ufs_inode.c */
 int	ufs_reclaim(struct vnode *);
 int	ufs_balloc_range(struct vnode *, off_t, off_t, kauth_cred_t, int);
+int	ufs_wapbl_truncate(struct vnode *, uint64_t, uint64_t, uint64_t);
 
 /* ufs_lookup.c */
 void	ufs_dirbad(struct inode *, doff_t, const char *);

Index: src/sys/ufs/ufs/ufs_inode.c
diff -u src/sys/ufs/ufs/ufs_inode.c:1.90 src/sys/ufs/ufs/ufs_inode.c:1.91
--- src/sys/ufs/ufs/ufs_inode.c:1.90	Thu May  8 08:21:53 2014
+++ src/sys/ufs/ufs/ufs_inode.c	Tue Oct 21 10:39:26 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: ufs_inode.c,v 1.90 2014/05/08 08:21:53 hannken Exp $	*/
+/*	$NetBSD: ufs_inode.c,v 1.91 2014/10/21 10:39:26 slp Exp $	*/
 
 /*
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ufs_inode.c,v 1.90 2014/05/08 08:21:53 hannken Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ufs_inode.c,v 1.91 2014/10/21 10:39:26 slp Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_ffs.h"
@@ -102,40 +102,25 @@ ufs_inactive(void *v)
 #ifdef UFS_EXTATTR
 		ufs_extattr_vnode_inactive(vp, curlwp);
 #endif
-		error = UFS_WAPBL_BEGIN(vp->v_mount);
-		if (error)
-			goto out;
-		logged = 1;
 		if (ip->i_size != 0) {
+			uint64_t incr = MNINDIR(ip->i_ump) <<
+			    vp->v_mount->mnt_fs_bshift; /* Power of 2 */
+			uint64_t base = UFS_NDADDR <<
+			    vp->v_mount->mnt_fs_bshift;
 			/*
 			 * When journaling, only truncate one indirect block
 			 * at a time
 			 */
-			if (vp->v_mount->mnt_wapbl) {
-				uint64_t incr = MNINDIR(ip->i_ump) <<
-				    vp->v_mount->mnt_fs_bshift; /* Power of 2 */
-				uint64_t base = UFS_NDADDR <<
-				    vp->v_mount->mnt_fs_bshift;
-				while (!error && ip->i_size > base + incr) {
-					/*
-					 * round down to next full indirect
-					 * block boundary.
-					 */
-					uint64_t nsize = base +
-					    ((ip->i_size - base - 1) &
-					    ~(incr - 1));
-					error = UFS_TRUNCATE(vp, nsize, 0,
-					    NOCRED);
-					if (error)
-						break;
-					UFS_WAPBL_END(vp->v_mount);
-					error = UFS_WAPBL_BEGIN(vp->v_mount);
-					if (error)
-						goto out;
-				}
+			if (vp->v_mount->mnt_wapbl && ip->i_size > base + incr) {
+				error = ufs_wapbl_truncate(vp, incr, base, 0);
 			}
-			if (!error)
+			if (!error) {
+				error = UFS_WAPBL_BEGIN(vp->v_mount);
+				if (error)
+					goto out;
+				logged = 1;
 				error = UFS_TRUNCATE(vp, (off_t)0, 0, NOCRED);
+			}
 		}
 #if defined(QUOTA) || defined(QUOTA2)
 		(void)chkiq(ip, -1, NOCRED, 0);
@@ -309,3 +294,35 @@ ufs_balloc_range(struct vnode *vp, off_t
  	kmem_free(pgs, pgssize);
 	return error;
 }
+
+int
+ufs_wapbl_truncate(struct vnode *vp, uint64_t incr, uint64_t base,
+    uint64_t newsize)
+{
+	struct inode *ip = VTOI(vp);
+	int error;
+
+	error = UFS_WAPBL_BEGIN(vp->v_mount);
+	if (error)
+		return error;
+
+	while (ip->i_size > base + incr) {
+		/*
+		 * round down to next full indirect
+		 * block boundary.
+		 */
+		uint64_t nsize =
+		    base + ((ip->i_size - base - 1) & ~(incr - 1));
+		error = UFS_TRUNCATE(vp, nsize, 0, NOCRED);
+		if (error)
+			break;
+		UFS_WAPBL_END(vp->v_mount);
+		error = UFS_WAPBL_BEGIN(vp->v_mount);
+		if (error)
+			return error;
+	}
+	UFS_WAPBL_END(vp->v_mount);
+
+	return error;
+}
+

Index: src/sys/ufs/ufs/ufs_vnops.c
diff -u src/sys/ufs/ufs/ufs_vnops.c:1.222 src/sys/ufs/ufs/ufs_vnops.c:1.223
--- src/sys/ufs/ufs/ufs_vnops.c:1.222	Sat Oct 18 08:33:30 2014
+++ src/sys/ufs/ufs/ufs_vnops.c	Tue Oct 21 10:39:26 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: ufs_vnops.c,v 1.222 2014/10/18 08:33:30 snj Exp $	*/
+/*	$NetBSD: ufs_vnops.c,v 1.223 2014/10/21 10:39:26 slp Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -66,7 +66,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ufs_vnops.c,v 1.222 2014/10/18 08:33:30 snj Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ufs_vnops.c,v 1.223 2014/10/21 10:39:26 slp Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_ffs.h"
@@ -464,6 +464,8 @@ ufs_setattr(void *v)
 	int		error;
 	kauth_action_t	action;
 	bool		changing_sysflags;
+	uint64_t	incr;
+	uint64_t	base;
 
 	vap = ap->a_vap;
 	vp = ap->a_vp;
@@ -579,39 +581,25 @@ ufs_setattr(void *v)
 				error = EPERM;
 				goto out;
 			}
-			error = UFS_WAPBL_BEGIN(vp->v_mount);
-			if (error)
-				goto out;
+			incr = MNINDIR(ip->i_ump) <<
+			    vp->v_mount->mnt_fs_bshift; /* Power of 2 */
+			base = UFS_NDADDR <<
+			    vp->v_mount->mnt_fs_bshift;
 			/*
 			 * When journaling, only truncate one indirect block
 			 * at a time.
 			 */
-			if (vp->v_mount->mnt_wapbl) {
-				uint64_t incr = MNINDIR(ip->i_ump) <<
-				    vp->v_mount->mnt_fs_bshift; /* Power of 2 */
-				uint64_t base = UFS_NDADDR <<
-				    vp->v_mount->mnt_fs_bshift;
-				while (!error && ip->i_size > base + incr &&
-				    ip->i_size > vap->va_size + incr) {
-					/*
-					 * round down to next full indirect
-					 * block boundary.
-					 */
-					uint64_t nsize = base +
-					    ((ip->i_size - base - 1) &
-					    ~(incr - 1));
-					error = UFS_TRUNCATE(vp, nsize, 0,
-					    cred);
-					if (error == 0) {
-						UFS_WAPBL_END(vp->v_mount);
-						error =
-						   UFS_WAPBL_BEGIN(vp->v_mount);
-					}
-				}
+			if (vp->v_mount->mnt_wapbl && ip->i_size > base + incr) {
+				error = ufs_wapbl_truncate(vp, incr, base,
+				    vap->va_size);
 			}
-			if (!error)
+			if (!error) {
+				error = UFS_WAPBL_BEGIN(vp->v_mount);
+				if (error)
+					goto out;
 				error = UFS_TRUNCATE(vp, vap->va_size, 0, cred);
-			UFS_WAPBL_END(vp->v_mount);
+				UFS_WAPBL_END(vp->v_mount);
+			}
 			if (error)
 				goto out;
 			break;

Reply via email to