xref: /linux/fs/ocfs2/cluster/netdebug.c (revision 2c935bc57221cc2edc787c72ea0e2d30cdcd3d5e)
12309e9e0SSunil Mushran /* -*- mode: c; c-basic-offset: 8; -*-
22309e9e0SSunil Mushran  * vim: noexpandtab sw=8 ts=8 sts=0:
32309e9e0SSunil Mushran  *
42309e9e0SSunil Mushran  * netdebug.c
52309e9e0SSunil Mushran  *
62309e9e0SSunil Mushran  * debug functionality for o2net
72309e9e0SSunil Mushran  *
82309e9e0SSunil Mushran  * Copyright (C) 2005, 2008 Oracle.  All rights reserved.
92309e9e0SSunil Mushran  *
102309e9e0SSunil Mushran  * This program is free software; you can redistribute it and/or
112309e9e0SSunil Mushran  * modify it under the terms of the GNU General Public
122309e9e0SSunil Mushran  * License as published by the Free Software Foundation; either
132309e9e0SSunil Mushran  * version 2 of the License, or (at your option) any later version.
142309e9e0SSunil Mushran  *
152309e9e0SSunil Mushran  * This program is distributed in the hope that it will be useful,
162309e9e0SSunil Mushran  * but WITHOUT ANY WARRANTY; without even the implied warranty of
172309e9e0SSunil Mushran  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
182309e9e0SSunil Mushran  * General Public License for more details.
192309e9e0SSunil Mushran  *
202309e9e0SSunil Mushran  * You should have received a copy of the GNU General Public
212309e9e0SSunil Mushran  * License along with this program; if not, write to the
222309e9e0SSunil Mushran  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
232309e9e0SSunil Mushran  * Boston, MA 021110-1307, USA.
242309e9e0SSunil Mushran  *
252309e9e0SSunil Mushran  */
262309e9e0SSunil Mushran 
272309e9e0SSunil Mushran #ifdef CONFIG_DEBUG_FS
282309e9e0SSunil Mushran 
292309e9e0SSunil Mushran #include <linux/module.h>
302309e9e0SSunil Mushran #include <linux/types.h>
312309e9e0SSunil Mushran #include <linux/slab.h>
322309e9e0SSunil Mushran #include <linux/idr.h>
332309e9e0SSunil Mushran #include <linux/kref.h>
342309e9e0SSunil Mushran #include <linux/seq_file.h>
352309e9e0SSunil Mushran #include <linux/debugfs.h>
362309e9e0SSunil Mushran 
372309e9e0SSunil Mushran #include <linux/uaccess.h>
382309e9e0SSunil Mushran 
392309e9e0SSunil Mushran #include "tcp.h"
402309e9e0SSunil Mushran #include "nodemanager.h"
412309e9e0SSunil Mushran #define MLOG_MASK_PREFIX ML_TCP
422309e9e0SSunil Mushran #include "masklog.h"
432309e9e0SSunil Mushran 
442309e9e0SSunil Mushran #include "tcp_internal.h"
452309e9e0SSunil Mushran 
462309e9e0SSunil Mushran #define O2NET_DEBUG_DIR		"o2net"
472309e9e0SSunil Mushran #define SC_DEBUG_NAME		"sock_containers"
482309e9e0SSunil Mushran #define NST_DEBUG_NAME		"send_tracking"
49db02754cSSunil Mushran #define STATS_DEBUG_NAME	"stats"
503ba169ccSSunil Mushran #define NODES_DEBUG_NAME	"connected_nodes"
51db02754cSSunil Mushran 
52db02754cSSunil Mushran #define SHOW_SOCK_CONTAINERS	0
53db02754cSSunil Mushran #define SHOW_SOCK_STATS		1
542309e9e0SSunil Mushran 
552309e9e0SSunil Mushran static struct dentry *o2net_dentry;
562309e9e0SSunil Mushran static struct dentry *sc_dentry;
572309e9e0SSunil Mushran static struct dentry *nst_dentry;
58db02754cSSunil Mushran static struct dentry *stats_dentry;
593ba169ccSSunil Mushran static struct dentry *nodes_dentry;
602309e9e0SSunil Mushran 
612309e9e0SSunil Mushran static DEFINE_SPINLOCK(o2net_debug_lock);
622309e9e0SSunil Mushran 
632309e9e0SSunil Mushran static LIST_HEAD(sock_containers);
642309e9e0SSunil Mushran static LIST_HEAD(send_tracking);
652309e9e0SSunil Mushran 
662309e9e0SSunil Mushran void o2net_debug_add_nst(struct o2net_send_tracking *nst)
672309e9e0SSunil Mushran {
682309e9e0SSunil Mushran 	spin_lock(&o2net_debug_lock);
692309e9e0SSunil Mushran 	list_add(&nst->st_net_debug_item, &send_tracking);
702309e9e0SSunil Mushran 	spin_unlock(&o2net_debug_lock);
712309e9e0SSunil Mushran }
722309e9e0SSunil Mushran 
732309e9e0SSunil Mushran void o2net_debug_del_nst(struct o2net_send_tracking *nst)
742309e9e0SSunil Mushran {
752309e9e0SSunil Mushran 	spin_lock(&o2net_debug_lock);
762309e9e0SSunil Mushran 	if (!list_empty(&nst->st_net_debug_item))
772309e9e0SSunil Mushran 		list_del_init(&nst->st_net_debug_item);
782309e9e0SSunil Mushran 	spin_unlock(&o2net_debug_lock);
792309e9e0SSunil Mushran }
802309e9e0SSunil Mushran 
812309e9e0SSunil Mushran static struct o2net_send_tracking
822309e9e0SSunil Mushran 			*next_nst(struct o2net_send_tracking *nst_start)
832309e9e0SSunil Mushran {
842309e9e0SSunil Mushran 	struct o2net_send_tracking *nst, *ret = NULL;
852309e9e0SSunil Mushran 
862309e9e0SSunil Mushran 	assert_spin_locked(&o2net_debug_lock);
872309e9e0SSunil Mushran 
882309e9e0SSunil Mushran 	list_for_each_entry(nst, &nst_start->st_net_debug_item,
892309e9e0SSunil Mushran 			    st_net_debug_item) {
902309e9e0SSunil Mushran 		/* discover the head of the list */
912309e9e0SSunil Mushran 		if (&nst->st_net_debug_item == &send_tracking)
922309e9e0SSunil Mushran 			break;
932309e9e0SSunil Mushran 
942309e9e0SSunil Mushran 		/* use st_task to detect real nsts in the list */
952309e9e0SSunil Mushran 		if (nst->st_task != NULL) {
962309e9e0SSunil Mushran 			ret = nst;
972309e9e0SSunil Mushran 			break;
982309e9e0SSunil Mushran 		}
992309e9e0SSunil Mushran 	}
1002309e9e0SSunil Mushran 
1012309e9e0SSunil Mushran 	return ret;
1022309e9e0SSunil Mushran }
1032309e9e0SSunil Mushran 
1042309e9e0SSunil Mushran static void *nst_seq_start(struct seq_file *seq, loff_t *pos)
1052309e9e0SSunil Mushran {
1062309e9e0SSunil Mushran 	struct o2net_send_tracking *nst, *dummy_nst = seq->private;
1072309e9e0SSunil Mushran 
1082309e9e0SSunil Mushran 	spin_lock(&o2net_debug_lock);
1092309e9e0SSunil Mushran 	nst = next_nst(dummy_nst);
1102309e9e0SSunil Mushran 	spin_unlock(&o2net_debug_lock);
1112309e9e0SSunil Mushran 
1122309e9e0SSunil Mushran 	return nst;
1132309e9e0SSunil Mushran }
1142309e9e0SSunil Mushran 
1152309e9e0SSunil Mushran static void *nst_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1162309e9e0SSunil Mushran {
1172309e9e0SSunil Mushran 	struct o2net_send_tracking *nst, *dummy_nst = seq->private;
1182309e9e0SSunil Mushran 
1192309e9e0SSunil Mushran 	spin_lock(&o2net_debug_lock);
1202309e9e0SSunil Mushran 	nst = next_nst(dummy_nst);
1212309e9e0SSunil Mushran 	list_del_init(&dummy_nst->st_net_debug_item);
1222309e9e0SSunil Mushran 	if (nst)
1232309e9e0SSunil Mushran 		list_add(&dummy_nst->st_net_debug_item,
1242309e9e0SSunil Mushran 			 &nst->st_net_debug_item);
1252309e9e0SSunil Mushran 	spin_unlock(&o2net_debug_lock);
1262309e9e0SSunil Mushran 
1272309e9e0SSunil Mushran 	return nst; /* unused, just needs to be null when done */
1282309e9e0SSunil Mushran }
1292309e9e0SSunil Mushran 
1302309e9e0SSunil Mushran static int nst_seq_show(struct seq_file *seq, void *v)
1312309e9e0SSunil Mushran {
1322309e9e0SSunil Mushran 	struct o2net_send_tracking *nst, *dummy_nst = seq->private;
1333f9c14faSSunil Mushran 	ktime_t now;
1343f9c14faSSunil Mushran 	s64 sock, send, status;
1352309e9e0SSunil Mushran 
1362309e9e0SSunil Mushran 	spin_lock(&o2net_debug_lock);
1372309e9e0SSunil Mushran 	nst = next_nst(dummy_nst);
138cc548166SDan Carpenter 	if (!nst)
139cc548166SDan Carpenter 		goto out;
1402309e9e0SSunil Mushran 
1413f9c14faSSunil Mushran 	now = ktime_get();
1423f9c14faSSunil Mushran 	sock = ktime_to_us(ktime_sub(now, nst->st_sock_time));
1433f9c14faSSunil Mushran 	send = ktime_to_us(ktime_sub(now, nst->st_send_time));
1443f9c14faSSunil Mushran 	status = ktime_to_us(ktime_sub(now, nst->st_status_time));
1453f9c14faSSunil Mushran 
1462309e9e0SSunil Mushran 	/* get_task_comm isn't exported.  oh well. */
1472309e9e0SSunil Mushran 	seq_printf(seq, "%p:\n"
1482309e9e0SSunil Mushran 		   "  pid:          %lu\n"
1492309e9e0SSunil Mushran 		   "  tgid:         %lu\n"
1502309e9e0SSunil Mushran 		   "  process name: %s\n"
1512309e9e0SSunil Mushran 		   "  node:         %u\n"
1522309e9e0SSunil Mushran 		   "  sc:           %p\n"
1532309e9e0SSunil Mushran 		   "  message id:   %d\n"
1542309e9e0SSunil Mushran 		   "  message type: %u\n"
1552309e9e0SSunil Mushran 		   "  message key:  0x%08x\n"
1563f9c14faSSunil Mushran 		   "  sock acquiry: %lld usecs ago\n"
1573f9c14faSSunil Mushran 		   "  send start:   %lld usecs ago\n"
1583f9c14faSSunil Mushran 		   "  wait start:   %lld usecs ago\n",
15937096a79SSunil Mushran 		   nst, (unsigned long)task_pid_nr(nst->st_task),
1602309e9e0SSunil Mushran 		   (unsigned long)nst->st_task->tgid,
1612309e9e0SSunil Mushran 		   nst->st_task->comm, nst->st_node,
1622309e9e0SSunil Mushran 		   nst->st_sc, nst->st_id, nst->st_msg_type,
1632309e9e0SSunil Mushran 		   nst->st_msg_key,
1643f9c14faSSunil Mushran 		   (long long)sock,
1653f9c14faSSunil Mushran 		   (long long)send,
1663f9c14faSSunil Mushran 		   (long long)status);
1672309e9e0SSunil Mushran 
168cc548166SDan Carpenter out:
1692309e9e0SSunil Mushran 	spin_unlock(&o2net_debug_lock);
1702309e9e0SSunil Mushran 
1712309e9e0SSunil Mushran 	return 0;
1722309e9e0SSunil Mushran }
1732309e9e0SSunil Mushran 
1742309e9e0SSunil Mushran static void nst_seq_stop(struct seq_file *seq, void *v)
1752309e9e0SSunil Mushran {
1762309e9e0SSunil Mushran }
1772309e9e0SSunil Mushran 
17888e9d34cSJames Morris static const struct seq_operations nst_seq_ops = {
1792309e9e0SSunil Mushran 	.start = nst_seq_start,
1802309e9e0SSunil Mushran 	.next = nst_seq_next,
1812309e9e0SSunil Mushran 	.stop = nst_seq_stop,
1822309e9e0SSunil Mushran 	.show = nst_seq_show,
1832309e9e0SSunil Mushran };
1842309e9e0SSunil Mushran 
1852309e9e0SSunil Mushran static int nst_fop_open(struct inode *inode, struct file *file)
1862309e9e0SSunil Mushran {
1872309e9e0SSunil Mushran 	struct o2net_send_tracking *dummy_nst;
1882309e9e0SSunil Mushran 
189f3288338SRob Jones 	dummy_nst = __seq_open_private(file, &nst_seq_ops, sizeof(*dummy_nst));
190f3288338SRob Jones 	if (!dummy_nst)
191f3288338SRob Jones 		return -ENOMEM;
1922309e9e0SSunil Mushran 	o2net_debug_add_nst(dummy_nst);
1932309e9e0SSunil Mushran 
194f3288338SRob Jones 	return 0;
1952309e9e0SSunil Mushran }
1962309e9e0SSunil Mushran 
1972309e9e0SSunil Mushran static int nst_fop_release(struct inode *inode, struct file *file)
1982309e9e0SSunil Mushran {
1992309e9e0SSunil Mushran 	struct seq_file *seq = file->private_data;
2002309e9e0SSunil Mushran 	struct o2net_send_tracking *dummy_nst = seq->private;
2012309e9e0SSunil Mushran 
2022309e9e0SSunil Mushran 	o2net_debug_del_nst(dummy_nst);
2032309e9e0SSunil Mushran 	return seq_release_private(inode, file);
2042309e9e0SSunil Mushran }
2052309e9e0SSunil Mushran 
206828c0950SAlexey Dobriyan static const struct file_operations nst_seq_fops = {
2072309e9e0SSunil Mushran 	.open = nst_fop_open,
2082309e9e0SSunil Mushran 	.read = seq_read,
2092309e9e0SSunil Mushran 	.llseek = seq_lseek,
2102309e9e0SSunil Mushran 	.release = nst_fop_release,
2112309e9e0SSunil Mushran };
2122309e9e0SSunil Mushran 
2132309e9e0SSunil Mushran void o2net_debug_add_sc(struct o2net_sock_container *sc)
2142309e9e0SSunil Mushran {
2152309e9e0SSunil Mushran 	spin_lock(&o2net_debug_lock);
2162309e9e0SSunil Mushran 	list_add(&sc->sc_net_debug_item, &sock_containers);
2172309e9e0SSunil Mushran 	spin_unlock(&o2net_debug_lock);
2182309e9e0SSunil Mushran }
2192309e9e0SSunil Mushran 
2202309e9e0SSunil Mushran void o2net_debug_del_sc(struct o2net_sock_container *sc)
2212309e9e0SSunil Mushran {
2222309e9e0SSunil Mushran 	spin_lock(&o2net_debug_lock);
2232309e9e0SSunil Mushran 	list_del_init(&sc->sc_net_debug_item);
2242309e9e0SSunil Mushran 	spin_unlock(&o2net_debug_lock);
2252309e9e0SSunil Mushran }
2262309e9e0SSunil Mushran 
227db02754cSSunil Mushran struct o2net_sock_debug {
228db02754cSSunil Mushran 	int dbg_ctxt;
229db02754cSSunil Mushran 	struct o2net_sock_container *dbg_sock;
230db02754cSSunil Mushran };
231db02754cSSunil Mushran 
2322309e9e0SSunil Mushran static struct o2net_sock_container
2332309e9e0SSunil Mushran 			*next_sc(struct o2net_sock_container *sc_start)
2342309e9e0SSunil Mushran {
2352309e9e0SSunil Mushran 	struct o2net_sock_container *sc, *ret = NULL;
2362309e9e0SSunil Mushran 
2372309e9e0SSunil Mushran 	assert_spin_locked(&o2net_debug_lock);
2382309e9e0SSunil Mushran 
2392309e9e0SSunil Mushran 	list_for_each_entry(sc, &sc_start->sc_net_debug_item,
2402309e9e0SSunil Mushran 			    sc_net_debug_item) {
2412309e9e0SSunil Mushran 		/* discover the head of the list miscast as a sc */
2422309e9e0SSunil Mushran 		if (&sc->sc_net_debug_item == &sock_containers)
2432309e9e0SSunil Mushran 			break;
2442309e9e0SSunil Mushran 
2452309e9e0SSunil Mushran 		/* use sc_page to detect real scs in the list */
2462309e9e0SSunil Mushran 		if (sc->sc_page != NULL) {
2472309e9e0SSunil Mushran 			ret = sc;
2482309e9e0SSunil Mushran 			break;
2492309e9e0SSunil Mushran 		}
2502309e9e0SSunil Mushran 	}
2512309e9e0SSunil Mushran 
2522309e9e0SSunil Mushran 	return ret;
2532309e9e0SSunil Mushran }
2542309e9e0SSunil Mushran 
2552309e9e0SSunil Mushran static void *sc_seq_start(struct seq_file *seq, loff_t *pos)
2562309e9e0SSunil Mushran {
257db02754cSSunil Mushran 	struct o2net_sock_debug *sd = seq->private;
258db02754cSSunil Mushran 	struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
2592309e9e0SSunil Mushran 
2602309e9e0SSunil Mushran 	spin_lock(&o2net_debug_lock);
2612309e9e0SSunil Mushran 	sc = next_sc(dummy_sc);
2622309e9e0SSunil Mushran 	spin_unlock(&o2net_debug_lock);
2632309e9e0SSunil Mushran 
2642309e9e0SSunil Mushran 	return sc;
2652309e9e0SSunil Mushran }
2662309e9e0SSunil Mushran 
2672309e9e0SSunil Mushran static void *sc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2682309e9e0SSunil Mushran {
269db02754cSSunil Mushran 	struct o2net_sock_debug *sd = seq->private;
270db02754cSSunil Mushran 	struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
2712309e9e0SSunil Mushran 
2722309e9e0SSunil Mushran 	spin_lock(&o2net_debug_lock);
2732309e9e0SSunil Mushran 	sc = next_sc(dummy_sc);
2742309e9e0SSunil Mushran 	list_del_init(&dummy_sc->sc_net_debug_item);
2752309e9e0SSunil Mushran 	if (sc)
2762309e9e0SSunil Mushran 		list_add(&dummy_sc->sc_net_debug_item, &sc->sc_net_debug_item);
2772309e9e0SSunil Mushran 	spin_unlock(&o2net_debug_lock);
2782309e9e0SSunil Mushran 
2792309e9e0SSunil Mushran 	return sc; /* unused, just needs to be null when done */
2802309e9e0SSunil Mushran }
2812309e9e0SSunil Mushran 
282db02754cSSunil Mushran #ifdef CONFIG_OCFS2_FS_STATS
283db02754cSSunil Mushran # define sc_send_count(_s)		((_s)->sc_send_count)
284db02754cSSunil Mushran # define sc_recv_count(_s)		((_s)->sc_recv_count)
285db02754cSSunil Mushran # define sc_tv_acquiry_total_ns(_s)	(ktime_to_ns((_s)->sc_tv_acquiry_total))
286db02754cSSunil Mushran # define sc_tv_send_total_ns(_s)	(ktime_to_ns((_s)->sc_tv_send_total))
287db02754cSSunil Mushran # define sc_tv_status_total_ns(_s)	(ktime_to_ns((_s)->sc_tv_status_total))
288db02754cSSunil Mushran # define sc_tv_process_total_ns(_s)	(ktime_to_ns((_s)->sc_tv_process_total))
289db02754cSSunil Mushran #else
290db02754cSSunil Mushran # define sc_send_count(_s)		(0U)
291db02754cSSunil Mushran # define sc_recv_count(_s)		(0U)
292db02754cSSunil Mushran # define sc_tv_acquiry_total_ns(_s)	(0LL)
293db02754cSSunil Mushran # define sc_tv_send_total_ns(_s)	(0LL)
294db02754cSSunil Mushran # define sc_tv_status_total_ns(_s)	(0LL)
295db02754cSSunil Mushran # define sc_tv_process_total_ns(_s)	(0LL)
296db02754cSSunil Mushran #endif
297db02754cSSunil Mushran 
298db02754cSSunil Mushran /* So that debugfs.ocfs2 can determine which format is being used */
299db02754cSSunil Mushran #define O2NET_STATS_STR_VERSION		1
300db02754cSSunil Mushran static void sc_show_sock_stats(struct seq_file *seq,
301db02754cSSunil Mushran 			       struct o2net_sock_container *sc)
3022309e9e0SSunil Mushran {
303db02754cSSunil Mushran 	if (!sc)
304db02754cSSunil Mushran 		return;
3052309e9e0SSunil Mushran 
306db02754cSSunil Mushran 	seq_printf(seq, "%d,%u,%lu,%lld,%lld,%lld,%lu,%lld\n", O2NET_STATS_STR_VERSION,
307db02754cSSunil Mushran 		   sc->sc_node->nd_num, (unsigned long)sc_send_count(sc),
308db02754cSSunil Mushran 		   (long long)sc_tv_acquiry_total_ns(sc),
309db02754cSSunil Mushran 		   (long long)sc_tv_send_total_ns(sc),
310db02754cSSunil Mushran 		   (long long)sc_tv_status_total_ns(sc),
311db02754cSSunil Mushran 		   (unsigned long)sc_recv_count(sc),
312db02754cSSunil Mushran 		   (long long)sc_tv_process_total_ns(sc));
313db02754cSSunil Mushran }
3142309e9e0SSunil Mushran 
315db02754cSSunil Mushran static void sc_show_sock_container(struct seq_file *seq,
316db02754cSSunil Mushran 				   struct o2net_sock_container *sc)
317db02754cSSunil Mushran {
3182309e9e0SSunil Mushran 	struct inet_sock *inet = NULL;
3192309e9e0SSunil Mushran 	__be32 saddr = 0, daddr = 0;
3202309e9e0SSunil Mushran 	__be16 sport = 0, dport = 0;
3212309e9e0SSunil Mushran 
322db02754cSSunil Mushran 	if (!sc)
323db02754cSSunil Mushran 		return;
324db02754cSSunil Mushran 
3252309e9e0SSunil Mushran 	if (sc->sc_sock) {
3262309e9e0SSunil Mushran 		inet = inet_sk(sc->sc_sock->sk);
3272309e9e0SSunil Mushran 		/* the stack's structs aren't sparse endian clean */
328c720c7e8SEric Dumazet 		saddr = (__force __be32)inet->inet_saddr;
329c720c7e8SEric Dumazet 		daddr = (__force __be32)inet->inet_daddr;
330c720c7e8SEric Dumazet 		sport = (__force __be16)inet->inet_sport;
331c720c7e8SEric Dumazet 		dport = (__force __be16)inet->inet_dport;
3322309e9e0SSunil Mushran 	}
3332309e9e0SSunil Mushran 
3342309e9e0SSunil Mushran 	/* XXX sigh, inet-> doesn't have sparse annotation so any
3352309e9e0SSunil Mushran 	 * use of it here generates a warning with -Wbitwise */
3362309e9e0SSunil Mushran 	seq_printf(seq, "%p:\n"
3372309e9e0SSunil Mushran 		   "  krefs:           %d\n"
338be859405SHarvey Harrison 		   "  sock:            %pI4:%u -> "
339be859405SHarvey Harrison 				      "%pI4:%u\n"
3402309e9e0SSunil Mushran 		   "  remote node:     %s\n"
3412309e9e0SSunil Mushran 		   "  page off:        %zu\n"
3422309e9e0SSunil Mushran 		   "  handshake ok:    %u\n"
343ff1becbfSSunil Mushran 		   "  timer:           %lld usecs\n"
344ff1becbfSSunil Mushran 		   "  data ready:      %lld usecs\n"
345ff1becbfSSunil Mushran 		   "  advance start:   %lld usecs\n"
346ff1becbfSSunil Mushran 		   "  advance stop:    %lld usecs\n"
347ff1becbfSSunil Mushran 		   "  func start:      %lld usecs\n"
348ff1becbfSSunil Mushran 		   "  func stop:       %lld usecs\n"
349ff1becbfSSunil Mushran 		   "  func key:        0x%08x\n"
3502309e9e0SSunil Mushran 		   "  func type:       %u\n",
3512309e9e0SSunil Mushran 		   sc,
352*2c935bc5SPeter Zijlstra 		   kref_read(&sc->sc_kref),
353be859405SHarvey Harrison 		   &saddr, inet ? ntohs(sport) : 0,
354be859405SHarvey Harrison 		   &daddr, inet ? ntohs(dport) : 0,
3552309e9e0SSunil Mushran 		   sc->sc_node->nd_name,
3562309e9e0SSunil Mushran 		   sc->sc_page_off,
3572309e9e0SSunil Mushran 		   sc->sc_handshake_ok,
358ff1becbfSSunil Mushran 		   (long long)ktime_to_us(sc->sc_tv_timer),
359ff1becbfSSunil Mushran 		   (long long)ktime_to_us(sc->sc_tv_data_ready),
360ff1becbfSSunil Mushran 		   (long long)ktime_to_us(sc->sc_tv_advance_start),
361ff1becbfSSunil Mushran 		   (long long)ktime_to_us(sc->sc_tv_advance_stop),
362ff1becbfSSunil Mushran 		   (long long)ktime_to_us(sc->sc_tv_func_start),
363ff1becbfSSunil Mushran 		   (long long)ktime_to_us(sc->sc_tv_func_stop),
3642309e9e0SSunil Mushran 		   sc->sc_msg_key,
3652309e9e0SSunil Mushran 		   sc->sc_msg_type);
3662309e9e0SSunil Mushran }
3672309e9e0SSunil Mushran 
368db02754cSSunil Mushran static int sc_seq_show(struct seq_file *seq, void *v)
369db02754cSSunil Mushran {
370db02754cSSunil Mushran 	struct o2net_sock_debug *sd = seq->private;
371db02754cSSunil Mushran 	struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
372db02754cSSunil Mushran 
373db02754cSSunil Mushran 	spin_lock(&o2net_debug_lock);
374db02754cSSunil Mushran 	sc = next_sc(dummy_sc);
375db02754cSSunil Mushran 
376db02754cSSunil Mushran 	if (sc) {
377db02754cSSunil Mushran 		if (sd->dbg_ctxt == SHOW_SOCK_CONTAINERS)
378db02754cSSunil Mushran 			sc_show_sock_container(seq, sc);
379db02754cSSunil Mushran 		else
380db02754cSSunil Mushran 			sc_show_sock_stats(seq, sc);
381db02754cSSunil Mushran 	}
3822309e9e0SSunil Mushran 
3832309e9e0SSunil Mushran 	spin_unlock(&o2net_debug_lock);
3842309e9e0SSunil Mushran 
3852309e9e0SSunil Mushran 	return 0;
3862309e9e0SSunil Mushran }
3872309e9e0SSunil Mushran 
3882309e9e0SSunil Mushran static void sc_seq_stop(struct seq_file *seq, void *v)
3892309e9e0SSunil Mushran {
3902309e9e0SSunil Mushran }
3912309e9e0SSunil Mushran 
39288e9d34cSJames Morris static const struct seq_operations sc_seq_ops = {
3932309e9e0SSunil Mushran 	.start = sc_seq_start,
3942309e9e0SSunil Mushran 	.next = sc_seq_next,
3952309e9e0SSunil Mushran 	.stop = sc_seq_stop,
3962309e9e0SSunil Mushran 	.show = sc_seq_show,
3972309e9e0SSunil Mushran };
3982309e9e0SSunil Mushran 
399f3288338SRob Jones static int sc_common_open(struct file *file, int ctxt)
4002309e9e0SSunil Mushran {
401f3288338SRob Jones 	struct o2net_sock_debug *sd;
4022309e9e0SSunil Mushran 	struct o2net_sock_container *dummy_sc;
4032309e9e0SSunil Mushran 
404f3288338SRob Jones 	dummy_sc = kzalloc(sizeof(*dummy_sc), GFP_KERNEL);
405f3288338SRob Jones 	if (!dummy_sc)
406f3288338SRob Jones 		return -ENOMEM;
407f3288338SRob Jones 
408f3288338SRob Jones 	sd = __seq_open_private(file, &sc_seq_ops, sizeof(*sd));
409f3288338SRob Jones 	if (!sd) {
410f3288338SRob Jones 		kfree(dummy_sc);
411f3288338SRob Jones 		return -ENOMEM;
4122309e9e0SSunil Mushran 	}
4132309e9e0SSunil Mushran 
414f3288338SRob Jones 	sd->dbg_ctxt = ctxt;
415db02754cSSunil Mushran 	sd->dbg_sock = dummy_sc;
416f3288338SRob Jones 
4172309e9e0SSunil Mushran 	o2net_debug_add_sc(dummy_sc);
4182309e9e0SSunil Mushran 
419f3288338SRob Jones 	return 0;
4202309e9e0SSunil Mushran }
4212309e9e0SSunil Mushran 
4222309e9e0SSunil Mushran static int sc_fop_release(struct inode *inode, struct file *file)
4232309e9e0SSunil Mushran {
4242309e9e0SSunil Mushran 	struct seq_file *seq = file->private_data;
425db02754cSSunil Mushran 	struct o2net_sock_debug *sd = seq->private;
426db02754cSSunil Mushran 	struct o2net_sock_container *dummy_sc = sd->dbg_sock;
4272309e9e0SSunil Mushran 
4282309e9e0SSunil Mushran 	o2net_debug_del_sc(dummy_sc);
4292309e9e0SSunil Mushran 	return seq_release_private(inode, file);
4302309e9e0SSunil Mushran }
4312309e9e0SSunil Mushran 
432db02754cSSunil Mushran static int stats_fop_open(struct inode *inode, struct file *file)
433db02754cSSunil Mushran {
434f3288338SRob Jones 	return sc_common_open(file, SHOW_SOCK_STATS);
435db02754cSSunil Mushran }
436db02754cSSunil Mushran 
437db02754cSSunil Mushran static const struct file_operations stats_seq_fops = {
438db02754cSSunil Mushran 	.open = stats_fop_open,
439db02754cSSunil Mushran 	.read = seq_read,
440db02754cSSunil Mushran 	.llseek = seq_lseek,
441db02754cSSunil Mushran 	.release = sc_fop_release,
442db02754cSSunil Mushran };
443db02754cSSunil Mushran 
444db02754cSSunil Mushran static int sc_fop_open(struct inode *inode, struct file *file)
445db02754cSSunil Mushran {
446f3288338SRob Jones 	return sc_common_open(file, SHOW_SOCK_CONTAINERS);
447db02754cSSunil Mushran }
448db02754cSSunil Mushran 
449828c0950SAlexey Dobriyan static const struct file_operations sc_seq_fops = {
4502309e9e0SSunil Mushran 	.open = sc_fop_open,
4512309e9e0SSunil Mushran 	.read = seq_read,
4522309e9e0SSunil Mushran 	.llseek = seq_lseek,
4532309e9e0SSunil Mushran 	.release = sc_fop_release,
4542309e9e0SSunil Mushran };
4552309e9e0SSunil Mushran 
4563ba169ccSSunil Mushran static int o2net_fill_bitmap(char *buf, int len)
4572309e9e0SSunil Mushran {
4583ba169ccSSunil Mushran 	unsigned long map[BITS_TO_LONGS(O2NM_MAX_NODES)];
4593ba169ccSSunil Mushran 	int i = -1, out = 0;
4603ba169ccSSunil Mushran 
4613ba169ccSSunil Mushran 	o2net_fill_node_map(map, sizeof(map));
4623ba169ccSSunil Mushran 
4633ba169ccSSunil Mushran 	while ((i = find_next_bit(map, O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES)
4643ba169ccSSunil Mushran 		out += snprintf(buf + out, PAGE_SIZE - out, "%d ", i);
4653ba169ccSSunil Mushran 	out += snprintf(buf + out, PAGE_SIZE - out, "\n");
4663ba169ccSSunil Mushran 
4673ba169ccSSunil Mushran 	return out;
4682309e9e0SSunil Mushran }
4692309e9e0SSunil Mushran 
4703ba169ccSSunil Mushran static int nodes_fop_open(struct inode *inode, struct file *file)
4713ba169ccSSunil Mushran {
4723ba169ccSSunil Mushran 	char *buf;
4732309e9e0SSunil Mushran 
4743ba169ccSSunil Mushran 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4753ba169ccSSunil Mushran 	if (!buf)
4763ba169ccSSunil Mushran 		return -ENOMEM;
4772309e9e0SSunil Mushran 
4783ba169ccSSunil Mushran 	i_size_write(inode, o2net_fill_bitmap(buf, PAGE_SIZE));
4793ba169ccSSunil Mushran 
4803ba169ccSSunil Mushran 	file->private_data = buf;
481db02754cSSunil Mushran 
4822309e9e0SSunil Mushran 	return 0;
4832309e9e0SSunil Mushran }
4842309e9e0SSunil Mushran 
4853ba169ccSSunil Mushran static int o2net_debug_release(struct inode *inode, struct file *file)
4863ba169ccSSunil Mushran {
4873ba169ccSSunil Mushran 	kfree(file->private_data);
4883ba169ccSSunil Mushran 	return 0;
4893ba169ccSSunil Mushran }
4903ba169ccSSunil Mushran 
4913ba169ccSSunil Mushran static ssize_t o2net_debug_read(struct file *file, char __user *buf,
4923ba169ccSSunil Mushran 				size_t nbytes, loff_t *ppos)
4933ba169ccSSunil Mushran {
4943ba169ccSSunil Mushran 	return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
4953ba169ccSSunil Mushran 				       i_size_read(file->f_mapping->host));
4963ba169ccSSunil Mushran }
4973ba169ccSSunil Mushran 
4983ba169ccSSunil Mushran static const struct file_operations nodes_fops = {
4993ba169ccSSunil Mushran 	.open		= nodes_fop_open,
5003ba169ccSSunil Mushran 	.release	= o2net_debug_release,
5013ba169ccSSunil Mushran 	.read		= o2net_debug_read,
5023ba169ccSSunil Mushran 	.llseek		= generic_file_llseek,
5033ba169ccSSunil Mushran };
5043ba169ccSSunil Mushran 
5052309e9e0SSunil Mushran void o2net_debugfs_exit(void)
5062309e9e0SSunil Mushran {
5073ba169ccSSunil Mushran 	debugfs_remove(nodes_dentry);
508db02754cSSunil Mushran 	debugfs_remove(stats_dentry);
5092309e9e0SSunil Mushran 	debugfs_remove(sc_dentry);
5102309e9e0SSunil Mushran 	debugfs_remove(nst_dentry);
5112309e9e0SSunil Mushran 	debugfs_remove(o2net_dentry);
5122309e9e0SSunil Mushran }
5132309e9e0SSunil Mushran 
5143ba169ccSSunil Mushran int o2net_debugfs_init(void)
5153ba169ccSSunil Mushran {
516f4ae40a6SAl Viro 	umode_t mode = S_IFREG|S_IRUSR;
5173ba169ccSSunil Mushran 
5183ba169ccSSunil Mushran 	o2net_dentry = debugfs_create_dir(O2NET_DEBUG_DIR, NULL);
5193ba169ccSSunil Mushran 	if (o2net_dentry)
5203ba169ccSSunil Mushran 		nst_dentry = debugfs_create_file(NST_DEBUG_NAME, mode,
5213ba169ccSSunil Mushran 					o2net_dentry, NULL, &nst_seq_fops);
5223ba169ccSSunil Mushran 	if (nst_dentry)
5233ba169ccSSunil Mushran 		sc_dentry = debugfs_create_file(SC_DEBUG_NAME, mode,
5243ba169ccSSunil Mushran 					o2net_dentry, NULL, &sc_seq_fops);
5253ba169ccSSunil Mushran 	if (sc_dentry)
5263ba169ccSSunil Mushran 		stats_dentry = debugfs_create_file(STATS_DEBUG_NAME, mode,
5273ba169ccSSunil Mushran 					o2net_dentry, NULL, &stats_seq_fops);
5283ba169ccSSunil Mushran 	if (stats_dentry)
5293ba169ccSSunil Mushran 		nodes_dentry = debugfs_create_file(NODES_DEBUG_NAME, mode,
5303ba169ccSSunil Mushran 					o2net_dentry, NULL, &nodes_fops);
5313ba169ccSSunil Mushran 	if (nodes_dentry)
5323ba169ccSSunil Mushran 		return 0;
5333ba169ccSSunil Mushran 
5343ba169ccSSunil Mushran 	o2net_debugfs_exit();
5353ba169ccSSunil Mushran 	mlog_errno(-ENOMEM);
5363ba169ccSSunil Mushran 	return -ENOMEM;
5373ba169ccSSunil Mushran }
5383ba169ccSSunil Mushran 
5392309e9e0SSunil Mushran #endif	/* CONFIG_DEBUG_FS */
540