Module Name:    src
Committed By:   rmind
Date:           Wed Nov 20 23:44:23 UTC 2013

Modified Files:
        src/sys/fs/v7fs: v7fs_io_kern.c v7fs_vfsops.c v7fs_vnops.c

Log Message:
v7fs: replace malloc(9) with kmem(9), fix a memory leak and few missing
mutex destruction cases in the error path.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/fs/v7fs/v7fs_io_kern.c
cvs rdiff -u -r1.7 -r1.8 src/sys/fs/v7fs/v7fs_vfsops.c
cvs rdiff -u -r1.12 -r1.13 src/sys/fs/v7fs/v7fs_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/v7fs/v7fs_io_kern.c
diff -u src/sys/fs/v7fs/v7fs_io_kern.c:1.1 src/sys/fs/v7fs/v7fs_io_kern.c:1.2
--- src/sys/fs/v7fs/v7fs_io_kern.c:1.1	Mon Jun 27 11:52:25 2011
+++ src/sys/fs/v7fs/v7fs_io_kern.c	Wed Nov 20 23:44:23 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: v7fs_io_kern.c,v 1.1 2011/06/27 11:52:25 uch Exp $	*/
+/*	$NetBSD: v7fs_io_kern.c,v 1.2 2013/11/20 23:44:23 rmind Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2011 The NetBSD Foundation, Inc.
@@ -30,19 +30,19 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: v7fs_io_kern.c,v 1.1 2011/06/27 11:52:25 uch Exp $");
+__KERNEL_RCSID(0, "$NetBSD: v7fs_io_kern.c,v 1.2 2013/11/20 23:44:23 rmind Exp $");
 #if defined _KERNEL_OPT
 #include "opt_v7fs.h"
 #endif
 #include <sys/cdefs.h>
 
-__KERNEL_RCSID(0, "$NetBSD: v7fs_io_kern.c,v 1.1 2011/06/27 11:52:25 uch Exp $");
+__KERNEL_RCSID(0, "$NetBSD: v7fs_io_kern.c,v 1.2 2013/11/20 23:44:23 rmind Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
 #include <sys/systm.h>
 #include <sys/buf.h>
-#include <sys/malloc.h>
+#include <sys/kmem.h>
 #include <sys/kauth.h>
 #include <sys/mutex.h>
 
@@ -69,8 +69,6 @@ static void v7fs_os_lock(void *);
 static void v7fs_os_unlock(void *);
 static bool lock_init(struct lock_ops *);
 
-MALLOC_JUSTDEFINE(M_V7FS, "v7fs core", "v7fs internal structures");
-
 int
 v7fs_io_init(struct v7fs_self **fs,
     const struct v7fs_mount_device *mount_device, size_t block_size)
@@ -80,11 +78,8 @@ v7fs_io_init(struct v7fs_self **fs,
 	struct local_io *local;
 	int error = 0;
 
-	/* Allocate myself */
-	if (!(p = (struct v7fs_self *)malloc(sizeof(*p), M_TEMP, M_WAITOK |
-	    M_ZERO)))
+	if ((p = kmem_zalloc(sizeof(*p), KM_SLEEP)) == NULL)
 		return ENOMEM;
-	memset(p, 0, sizeof(*p));
 
 	p->scratch_free = -1;
 	p->scratch_remain = V7FS_SELF_NSCRATCH;
@@ -95,8 +90,7 @@ v7fs_io_init(struct v7fs_self **fs,
 	v7fs_endian_init(p);
 #endif
 	/* IO */
-	if (!(local = (struct local_io *)malloc(sizeof(*local), M_TEMP,
-	    M_WAITOK | M_ZERO))) {
+	if ((local = kmem_zalloc(sizeof(*local), KM_SLEEP)) == NULL) {
 		error = ENOMEM;
 		goto errexit;
 	}
@@ -124,44 +118,41 @@ v7fs_io_init(struct v7fs_self **fs,
 	return 0;
 
 errexit:
-	if (p->io.cookie)
-		free(p->io.cookie, M_TEMP);
-	if (p->sb_lock.cookie)
-		free(p->sb_lock.cookie, M_TEMP);
-	if (p->ilist_lock.cookie)
-		free(p->ilist_lock.cookie, M_TEMP);
-	if (p->mem_lock.cookie)
-		free(p->mem_lock.cookie, M_TEMP);
-	free(p, M_TEMP);
-
+	v7fs_io_fini(p);
 	return error;
 }
 
 static bool
 lock_init(struct lock_ops *ops)
 {
-	if (!(ops->cookie = (kmutex_t *)malloc(sizeof(kmutex_t), M_TEMP,
-	    M_WAITOK | M_ZERO))) {
+	if ((ops->cookie = kmem_zalloc(sizeof(kmutex_t), KM_SLEEP)) == NULL) {
 		return false;
 	}
 	mutex_init(ops->cookie, MUTEX_DEFAULT, IPL_NONE);
 	ops->lock = v7fs_os_lock;
 	ops->unlock = v7fs_os_unlock;
-
 	return true;
 }
 
 void
 v7fs_io_fini(struct v7fs_self *fs)
 {
-	mutex_destroy(fs->sb_lock.cookie);
-	mutex_destroy(fs->ilist_lock.cookie);
-	mutex_destroy(fs->mem_lock.cookie);
-
-	free(fs->io.cookie, M_TEMP);
-	free(fs->sb_lock.cookie, M_TEMP);
-	free(fs->ilist_lock.cookie, M_TEMP);
-	free(fs, M_TEMP);
+	if (fs->io.cookie) {
+		kmem_free(fs->io.cookie, sizeof(struct local_io));
+	}
+	if (fs->sb_lock.cookie) {
+		mutex_destroy(fs->sb_lock.cookie);
+		kmem_free(fs->sb_lock.cookie, sizeof(kmutex_t));
+	}
+	if (fs->ilist_lock.cookie) {
+		mutex_destroy(fs->ilist_lock.cookie);
+		kmem_free(fs->ilist_lock.cookie, sizeof(kmutex_t));
+	}
+	if (fs->mem_lock.cookie) {
+		mutex_destroy(fs->mem_lock.cookie);
+		kmem_free(fs->mem_lock.cookie, sizeof(kmutex_t));
+	}
+	kmem_free(fs, sizeof(*fs));
 }
 
 static bool

Index: src/sys/fs/v7fs/v7fs_vfsops.c
diff -u src/sys/fs/v7fs/v7fs_vfsops.c:1.7 src/sys/fs/v7fs/v7fs_vfsops.c:1.8
--- src/sys/fs/v7fs/v7fs_vfsops.c:1.7	Wed Jun 13 22:56:51 2012
+++ src/sys/fs/v7fs/v7fs_vfsops.c	Wed Nov 20 23:44:23 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: v7fs_vfsops.c,v 1.7 2012/06/13 22:56:51 joerg Exp $	*/
+/*	$NetBSD: v7fs_vfsops.c,v 1.8 2013/11/20 23:44:23 rmind Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2011 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: v7fs_vfsops.c,v 1.7 2012/06/13 22:56:51 joerg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: v7fs_vfsops.c,v 1.8 2013/11/20 23:44:23 rmind Exp $");
 #if defined _KERNEL_OPT
 #include "opt_v7fs.h"
 #endif
@@ -45,7 +45,7 @@ __KERNEL_RCSID(0, "$NetBSD: v7fs_vfsops.
 #include <sys/disk.h>
 #include <sys/device.h>
 #include <sys/fcntl.h>
-#include <sys/malloc.h>
+#include <sys/kmem.h>
 #include <sys/kauth.h>
 #include <sys/proc.h>
 
@@ -67,8 +67,6 @@ __KERNEL_RCSID(0, "$NetBSD: v7fs_vfsops.
 #define	DPRINTF(arg...)		((void)0)
 #endif
 
-MALLOC_JUSTDEFINE(M_V7FS_VFS, "v7fs vfs", "v7fs vfs structures");
-
 struct pool v7fs_node_pool;
 
 static int v7fs_mountfs(struct vnode *, struct mount *, int);
@@ -254,7 +252,7 @@ v7fs_mountfs(struct vnode *devvp, struct
 
 	DPRINTF("%d\n",endian);
 
-	v7fsmount = malloc(sizeof(*v7fsmount), M_V7FS_VFS, M_WAITOK);
+	v7fsmount = kmem_zalloc(sizeof(*v7fsmount), KM_SLEEP);
 	if (v7fsmount == NULL) {
 		return ENOMEM;
 	}
@@ -288,7 +286,7 @@ v7fs_mountfs(struct vnode *devvp, struct
 	return 0;
 
 err_exit:
-	free(v7fsmount, M_V7FS_VFS);
+	kmem_free(v7fsmount, sizeof(*v7fsmount));
 	return error;
 }
 
@@ -319,7 +317,7 @@ v7fs_unmount(struct mount *mp, int mntfl
 
 	v7fs_io_fini(v7fsmount->core);
 
-	free(v7fsmount, M_V7FS_VFS);
+	kmem_free(v7fsmount, sizeof(*v7fsmount));
 	mp->mnt_data = NULL;
 	mp->mnt_flag &= ~MNT_LOCAL;
 
@@ -522,17 +520,11 @@ v7fs_vptofh(struct vnode *vpp, struct fi
 	return EOPNOTSUPP;
 }
 
-MALLOC_DECLARE(M_V7FS);
-MALLOC_DECLARE(M_V7FS_VNODE);
-
 void
 v7fs_init(void)
 {
 
 	DPRINTF("\n");
-	malloc_type_attach(M_V7FS_VFS);
-	malloc_type_attach(M_V7FS);
-	malloc_type_attach(M_V7FS_VNODE);
 	pool_init(&v7fs_node_pool, sizeof(struct v7fs_node), 0, 0, 0,
 	    "v7fs_node_pool", &pool_allocator_nointr, IPL_NONE);
 }
@@ -551,9 +543,6 @@ v7fs_done(void)
 
 	DPRINTF("\n");
 	pool_destroy(&v7fs_node_pool);
-	malloc_type_detach(M_V7FS);
-	malloc_type_detach(M_V7FS_VFS);
-	malloc_type_detach(M_V7FS_VNODE);
 }
 
 int

Index: src/sys/fs/v7fs/v7fs_vnops.c
diff -u src/sys/fs/v7fs/v7fs_vnops.c:1.12 src/sys/fs/v7fs/v7fs_vnops.c:1.13
--- src/sys/fs/v7fs/v7fs_vnops.c:1.12	Mon Mar 18 19:35:42 2013
+++ src/sys/fs/v7fs/v7fs_vnops.c	Wed Nov 20 23:44:23 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: v7fs_vnops.c,v 1.12 2013/03/18 19:35:42 plunky Exp $	*/
+/*	$NetBSD: v7fs_vnops.c,v 1.13 2013/11/20 23:44:23 rmind Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2011 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: v7fs_vnops.c,v 1.12 2013/03/18 19:35:42 plunky Exp $");
+__KERNEL_RCSID(0, "$NetBSD: v7fs_vnops.c,v 1.13 2013/11/20 23:44:23 rmind Exp $");
 #if defined _KERNEL_OPT
 #include "opt_v7fs.h"
 #endif
@@ -41,7 +41,7 @@ __KERNEL_RCSID(0, "$NetBSD: v7fs_vnops.c
 #include <sys/vnode.h>
 #include <sys/namei.h>
 #include <sys/dirent.h>
-#include <sys/malloc.h>
+#include <sys/kmem.h>
 #include <sys/lockf.h>
 #include <sys/unistd.h>
 #include <sys/fcntl.h>
@@ -64,9 +64,6 @@ __KERNEL_RCSID(0, "$NetBSD: v7fs_vnops.c
 #define	DPRINTF(arg...)		((void)0)
 #endif
 
-MALLOC_JUSTDEFINE(M_V7FS_VNODE, "v7fs vnode", "v7fs vnode structures");
-MALLOC_DECLARE(M_V7FS);
-
 int v7fs_vnode_reload(struct mount *, struct vnode *);
 
 static v7fs_mode_t vtype_to_v7fs_mode(enum vtype);
@@ -983,7 +980,7 @@ v7fs_readdir(void *v)
 		DPRINTF("uio buffer too small\n");
 		return ENOMEM;
 	}
-	dp = malloc(sizeof(*dp), M_V7FS, M_WAITOK | M_ZERO);
+	dp = kmem_zalloc(sizeof(*dp), KM_SLEEP);
 	arg.cnt = 0;
 	arg.dp = dp;
 	arg.uio = uio;
@@ -996,7 +993,7 @@ v7fs_readdir(void *v)
 	if (error < 0)
 		error = 0;
 
-	free(dp, M_V7FS);
+	kmem_free(dp, sizeof(*dp));
 
 	return error;
 }

Reply via email to