Module Name:    src
Committed By:   hannken
Date:           Mon Apr 17 08:29:58 UTC 2017

Modified Files:
        src/sys/kern: vfs_mount.c
        src/sys/sys: mount.h
        src/usr.sbin/pstat: pstat.c

Log Message:
Cleanup after mountlist iterator:
- remove now unused field mnt_list.
- rename mount_list to mountlist and make it local to vfs_mount.c.
- make mountlist_lock local to vfs_mount.c.

Change pstat.c to retrieve vnodes by lru lists.


To generate a diff of this commit:
cvs rdiff -u -r1.53 -r1.54 src/sys/kern/vfs_mount.c
cvs rdiff -u -r1.222 -r1.223 src/sys/sys/mount.h
cvs rdiff -u -r1.126 -r1.127 src/usr.sbin/pstat/pstat.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/kern/vfs_mount.c
diff -u src/sys/kern/vfs_mount.c:1.53 src/sys/kern/vfs_mount.c:1.54
--- src/sys/kern/vfs_mount.c:1.53	Wed Apr 12 10:35:10 2017
+++ src/sys/kern/vfs_mount.c	Mon Apr 17 08:29:58 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_mount.c,v 1.53 2017/04/12 10:35:10 hannken Exp $	*/
+/*	$NetBSD: vfs_mount.c,v 1.54 2017/04/17 08:29:58 hannken Exp $	*/
 
 /*-
  * Copyright (c) 1997-2011 The NetBSD Foundation, Inc.
@@ -67,7 +67,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vfs_mount.c,v 1.53 2017/04/12 10:35:10 hannken Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_mount.c,v 1.54 2017/04/17 08:29:58 hannken Exp $");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
@@ -108,8 +108,6 @@ struct mount_iterator {
 	struct mountlist_entry mi_entry;
 };
 
-static TAILQ_HEAD(mountlist, mountlist_entry) mount_list;
-
 static struct vnode *vfs_vnode_iterator_next1(struct vnode_iterator *,
     bool (*)(void *, struct vnode *), void *, bool);
 
@@ -117,10 +115,10 @@ static struct vnode *vfs_vnode_iterator_
 vnode_t *			rootvnode;
 
 /* Mounted filesystem list. */
-struct mntlist			mountlist;
-kmutex_t			mountlist_lock;
-int vnode_offset_next_by_mount	/* XXX: ugly hack for pstat.c */
-    = offsetof(vnode_impl_t, vi_mntvnodes.tqe_next);
+static TAILQ_HEAD(mountlist, mountlist_entry) mountlist;
+static kmutex_t			mountlist_lock;
+int vnode_offset_next_by_lru	/* XXX: ugly hack for pstat.c */
+    = offsetof(vnode_impl_t, vi_lrulist.tqe_next);
 
 kmutex_t			mntvnode_lock;
 kmutex_t			vfs_list_lock;
@@ -135,7 +133,6 @@ void
 vfs_mount_sysinit(void)
 {
 
-	TAILQ_INIT(&mount_list);
 	TAILQ_INIT(&mountlist);
 	mutex_init(&mountlist_lock, MUTEX_DEFAULT, IPL_NONE);
 	mutex_init(&mntvnode_lock, MUTEX_DEFAULT, IPL_NONE);
@@ -308,21 +305,16 @@ vfs_busy(struct mount *mp, struct mount 
 
 	KASSERT(mp->mnt_refcnt > 0);
 
+	KASSERT(nextp == NULL);
+
 	mutex_enter(&mp->mnt_unmounting);
 	if (__predict_false((mp->mnt_iflag & IMNT_GONE) != 0)) {
 		mutex_exit(&mp->mnt_unmounting);
-		if (nextp != NULL) {
-			KASSERT(mutex_owned(&mountlist_lock));
-			*nextp = TAILQ_NEXT(mp, mnt_list);
-		}
 		return ENOENT;
 	}
 	++mp->mnt_busynest;
 	KASSERT(mp->mnt_busynest != 0);
 	mutex_exit(&mp->mnt_unmounting);
-	if (nextp != NULL) {
-		mutex_exit(&mountlist_lock);
-	}
 	atomic_inc_uint(&mp->mnt_refcnt);
 	return 0;
 }
@@ -341,9 +333,8 @@ vfs_unbusy(struct mount *mp, bool keepre
 
 	KASSERT(mp->mnt_refcnt > 0);
 
-	if (nextp != NULL) {
-		mutex_enter(&mountlist_lock);
-	}
+	KASSERT(nextp == NULL);
+
 	mutex_enter(&mp->mnt_unmounting);
 	KASSERT(mp->mnt_busynest != 0);
 	mp->mnt_busynest--;
@@ -351,10 +342,6 @@ vfs_unbusy(struct mount *mp, bool keepre
 	if (!keepref) {
 		vfs_destroy(mp);
 	}
-	if (nextp != NULL) {
-		KASSERT(mutex_owned(&mountlist_lock));
-		*nextp = TAILQ_NEXT(mp, mnt_list);
-	}
 }
 
 struct vnode_iterator {
@@ -1508,7 +1495,7 @@ mountlist_iterator_init(mount_iterator_t
 
 	me = mountlist_alloc(ME_MARKER, NULL);
 	mutex_enter(&mountlist_lock);
-	TAILQ_INSERT_HEAD(&mount_list, me, me_list);
+	TAILQ_INSERT_HEAD(&mountlist, me, me_list);
 	mutex_exit(&mountlist_lock);
 	*mip = (mount_iterator_t *)me;
 }
@@ -1522,7 +1509,7 @@ mountlist_iterator_destroy(mount_iterato
 		vfs_unbusy(marker->me_mount, false, NULL);
 
 	mutex_enter(&mountlist_lock);
-	TAILQ_REMOVE(&mount_list, marker, me_list);
+	TAILQ_REMOVE(&mountlist, marker, me_list);
 	mutex_exit(&mountlist_lock);
 
 	mountlist_free(marker);
@@ -1554,8 +1541,8 @@ mountlist_iterator_next(mount_iterator_t
 			mutex_exit(&mountlist_lock);
 			return NULL;
 		}
-		TAILQ_REMOVE(&mount_list, marker, me_list);
-		TAILQ_INSERT_AFTER(&mount_list, me, marker, me_list);
+		TAILQ_REMOVE(&mountlist, marker, me_list);
+		TAILQ_INSERT_AFTER(&mountlist, me, marker, me_list);
 
 		/* Skip other markers. */
 		if (me->me_type != ME_MOUNT)
@@ -1588,8 +1575,7 @@ mountlist_append(struct mount *mp)
 
 	me = mountlist_alloc(ME_MOUNT, mp);
 	mutex_enter(&mountlist_lock);
-	TAILQ_INSERT_TAIL(&mount_list, me, me_list);
-	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
+	TAILQ_INSERT_TAIL(&mountlist, me, me_list);
 	mutex_exit(&mountlist_lock);
 }
 
@@ -1601,12 +1587,11 @@ mountlist_remove(struct mount *mp)
 	struct mountlist_entry *me;
 
 	mutex_enter(&mountlist_lock);
-	TAILQ_FOREACH(me, &mount_list, me_list)
+	TAILQ_FOREACH(me, &mountlist, me_list)
 		if (me->me_type == ME_MOUNT && me->me_mount == mp)
 			break;
 	KASSERT(me != NULL);
-	TAILQ_REMOVE(&mount_list, me, me_list);
-	TAILQ_REMOVE(&mountlist, mp, mnt_list);
+	TAILQ_REMOVE(&mountlist, me, me_list);
 	mutex_exit(&mountlist_lock);
 	mountlist_free(me);
 }
@@ -1621,9 +1606,9 @@ _mountlist_next(struct mount *mp)
 	struct mountlist_entry *me;
 
 	if (mp == NULL) {
-		me = TAILQ_FIRST(&mount_list);
+		me = TAILQ_FIRST(&mountlist);
 	} else {
-		TAILQ_FOREACH(me, &mount_list, me_list)
+		TAILQ_FOREACH(me, &mountlist, me_list)
 			if (me->me_type == ME_MOUNT && me->me_mount == mp)
 				break;
 		if (me != NULL)

Index: src/sys/sys/mount.h
diff -u src/sys/sys/mount.h:1.222 src/sys/sys/mount.h:1.223
--- src/sys/sys/mount.h:1.222	Tue Apr 11 07:46:37 2017
+++ src/sys/sys/mount.h	Mon Apr 17 08:29:58 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mount.h,v 1.222 2017/04/11 07:46:37 hannken Exp $	*/
+/*	$NetBSD: mount.h,v 1.223 2017/04/17 08:29:58 hannken Exp $	*/
 
 /*
  * Copyright (c) 1989, 1991, 1993
@@ -105,11 +105,9 @@ struct vattr;
 
 /*
  * Structure per mounted file system.  Each mounted file system has an
- * array of operations and an instance record.  The file systems are
- * put on a doubly linked list.
+ * array of operations and an instance record.
  */
 struct mount {
-	TAILQ_ENTRY(mount) mnt_list;		/* mount list */
 	TAILQ_HEAD(, vnode_impl) mnt_vnodelist;	/* list of vnodes this mount */
 	struct vfsops	*mnt_op;		/* operations on fs */
 	struct vnode	*mnt_vnodecovered;	/* vnode we mounted on */
@@ -466,10 +464,8 @@ extern time_t	metadelay;
 void	vfs_syncer_add_to_worklist(struct mount *);
 void	vfs_syncer_remove_from_worklist(struct mount *);
 
-extern	TAILQ_HEAD(mntlist, mount) mountlist;	/* mounted filesystem list */
 extern	struct vfsops *vfssw[];			/* filesystem type table */
 extern	int nvfssw;
-extern  kmutex_t mountlist_lock;
 extern	kmutex_t vfs_list_lock;
 
 void	vfs_mount_sysinit(void);

Index: src/usr.sbin/pstat/pstat.c
diff -u src/usr.sbin/pstat/pstat.c:1.126 src/usr.sbin/pstat/pstat.c:1.127
--- src/usr.sbin/pstat/pstat.c:1.126	Wed Jan 11 09:07:58 2017
+++ src/usr.sbin/pstat/pstat.c	Mon Apr 17 08:29:58 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: pstat.c,v 1.126 2017/01/11 09:07:58 hannken Exp $	*/
+/*	$NetBSD: pstat.c,v 1.127 2017/04/17 08:29:58 hannken Exp $	*/
 
 /*-
  * Copyright (c) 1980, 1991, 1993, 1994
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 19
 #if 0
 static char sccsid[] = "@(#)pstat.c	8.16 (Berkeley) 5/9/95";
 #else
-__RCSID("$NetBSD: pstat.c,v 1.126 2017/01/11 09:07:58 hannken Exp $");
+__RCSID("$NetBSD: pstat.c,v 1.127 2017/04/17 08:29:58 hannken Exp $");
 #endif
 #endif /* not lint */
 
@@ -85,19 +85,23 @@ __RCSID("$NetBSD: pstat.c,v 1.126 2017/0
 #include "swapctl.h"
 
 struct nlist nl[] = {
-#define	V_MOUNTLIST	0
-	{ "_mountlist", 0, 0, 0, 0 },	/* address of head of mount list. */
-#define	V_NUMV		1
+#define	V_LRU_FREE_LIST	0
+	{ "_lru_free_list", 0, 0, 0, 0 },	/* address of lru free list. */
+#define	V_LRU_HOLD_LIST	1
+	{ "_lru_hold_list", 0, 0, 0, 0 },	/* address of lru hold list. */
+#define	V_LRU_VRELE_LIST	2
+	{ "_lru_vrele_list", 0, 0, 0, 0 },	/* address of lru vrele list. */
+#define	V_NUMV		3
 	{ "_numvnodes", 0, 0, 0, 0 },
-#define	V_NEXT_OFFSET	2
-	{ "_vnode_offset_next_by_mount", 0, 0, 0, 0 },
-#define	FNL_NFILE	3
+#define	V_NEXT_OFFSET	4
+	{ "_vnode_offset_next_by_lru", 0, 0, 0, 0 },
+#define	FNL_NFILE	5
 	{ "_nfiles", 0, 0, 0, 0 },
-#define FNL_MAXFILE	4
+#define FNL_MAXFILE	6
 	{ "_maxfiles", 0, 0, 0, 0 },
-#define TTY_NTTY	5
+#define TTY_NTTY	7
 	{ "_tty_count", 0, 0, 0, 0 },
-#define TTY_TTYLIST	6
+#define TTY_TTYLIST	8
 	{ "_ttylist", 0, 0, 0, 0 },
 #define NLMANDATORY TTY_TTYLIST	/* names up to here are mandatory */
 	{ "", 0, 0, 0, 0 }
@@ -742,12 +746,21 @@ loadvnodes(int *avnodes)
 /*
  * simulate what a running kernel does in in kinfo_vnode
  */
+static int
+vnode_cmp(const void *p1, const void *p2)
+{
+	const char *s1 = (const char *)p1;
+	const char *s2 = (const char *)p2;
+	const struct vnode *v1 = (const struct vnode *)(s1 + VPTRSZ);
+	const struct vnode *v2 = (const struct vnode *)(s2 + VPTRSZ);
+
+	return (v2->v_mount - v1->v_mount);
+}
+
 char *
 kinfo_vnodes(int *avnodes)
 {
-	struct mntlist mlist;
-	struct mount *mp, mount;
-	struct vnode *vp, vnode;
+	int i;
 	char *beg, *bp, *ep;
 	int numvnodes, next_offset;
 
@@ -757,11 +770,13 @@ kinfo_vnodes(int *avnodes)
 	beg = bp;
 	ep = bp + (numvnodes + 20) * (VPTRSZ + VNODESZ);
 	KGET(V_NEXT_OFFSET, next_offset);
-	KGET(V_MOUNTLIST, mlist);
-	mp = TAILQ_FIRST(&mlist);
-	while (mp != NULL) {
-		KGET2(mp, &mount, sizeof(mount), "mount entry");
-		vp = (struct vnode *)TAILQ_FIRST(&mount.mnt_vnodelist);
+
+	for (i = V_LRU_FREE_LIST; i <= V_LRU_VRELE_LIST; i++) {
+		TAILQ_HEAD(vnodelst, vnode) lru_head;
+		struct vnode *vp, vnode;
+
+		KGET(i, lru_head);
+		vp = TAILQ_FIRST(&lru_head);
 		while (vp != NULL) {
 			KGET2(vp, &vnode, sizeof(vnode), "vnode");
 			if (bp + VPTRSZ + VNODESZ > ep)
@@ -773,9 +788,10 @@ kinfo_vnodes(int *avnodes)
 			bp += VNODESZ;
 			KGET2((char *)vp + next_offset, &vp, sizeof(vp), "nvp");
 		}
-		mp = TAILQ_NEXT(&mount, mnt_list);
 	}
 	*avnodes = (bp - beg) / (VPTRSZ + VNODESZ);
+	/* Sort by mount like we get it from sysctl. */
+	qsort(beg, *avnodes, VPTRSZ + VNODESZ, vnode_cmp);
 	return (beg);
 }
 

Reply via email to