xref: /linux/fs/ocfs2/cluster/netdebug.c (revision d293d3af2dee0c5f5d41b41ff0a0b091b2c45db8)
1328970deSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
22309e9e0SSunil Mushran /* -*- mode: c; c-basic-offset: 8; -*-
32309e9e0SSunil Mushran  * vim: noexpandtab sw=8 ts=8 sts=0:
42309e9e0SSunil Mushran  *
52309e9e0SSunil Mushran  * netdebug.c
62309e9e0SSunil Mushran  *
72309e9e0SSunil Mushran  * debug functionality for o2net
82309e9e0SSunil Mushran  *
92309e9e0SSunil Mushran  * Copyright (C) 2005, 2008 Oracle.  All rights reserved.
102309e9e0SSunil Mushran  */
112309e9e0SSunil Mushran 
122309e9e0SSunil Mushran #ifdef CONFIG_DEBUG_FS
132309e9e0SSunil Mushran 
142309e9e0SSunil Mushran #include <linux/module.h>
152309e9e0SSunil Mushran #include <linux/types.h>
162309e9e0SSunil Mushran #include <linux/slab.h>
172309e9e0SSunil Mushran #include <linux/idr.h>
182309e9e0SSunil Mushran #include <linux/kref.h>
192309e9e0SSunil Mushran #include <linux/seq_file.h>
202309e9e0SSunil Mushran #include <linux/debugfs.h>
212309e9e0SSunil Mushran 
222309e9e0SSunil Mushran #include <linux/uaccess.h>
232309e9e0SSunil Mushran 
242309e9e0SSunil Mushran #include "tcp.h"
252309e9e0SSunil Mushran #include "nodemanager.h"
262309e9e0SSunil Mushran #define MLOG_MASK_PREFIX ML_TCP
272309e9e0SSunil Mushran #include "masklog.h"
282309e9e0SSunil Mushran 
292309e9e0SSunil Mushran #include "tcp_internal.h"
302309e9e0SSunil Mushran 
312309e9e0SSunil Mushran #define O2NET_DEBUG_DIR		"o2net"
322309e9e0SSunil Mushran #define SC_DEBUG_NAME		"sock_containers"
332309e9e0SSunil Mushran #define NST_DEBUG_NAME		"send_tracking"
34db02754cSSunil Mushran #define STATS_DEBUG_NAME	"stats"
353ba169ccSSunil Mushran #define NODES_DEBUG_NAME	"connected_nodes"
36db02754cSSunil Mushran 
37db02754cSSunil Mushran #define SHOW_SOCK_CONTAINERS	0
38db02754cSSunil Mushran #define SHOW_SOCK_STATS		1
392309e9e0SSunil Mushran 
402309e9e0SSunil Mushran static struct dentry *o2net_dentry;
412309e9e0SSunil Mushran 
422309e9e0SSunil Mushran static DEFINE_SPINLOCK(o2net_debug_lock);
432309e9e0SSunil Mushran 
442309e9e0SSunil Mushran static LIST_HEAD(sock_containers);
452309e9e0SSunil Mushran static LIST_HEAD(send_tracking);
462309e9e0SSunil Mushran 
472309e9e0SSunil Mushran void o2net_debug_add_nst(struct o2net_send_tracking *nst)
482309e9e0SSunil Mushran {
492309e9e0SSunil Mushran 	spin_lock(&o2net_debug_lock);
502309e9e0SSunil Mushran 	list_add(&nst->st_net_debug_item, &send_tracking);
512309e9e0SSunil Mushran 	spin_unlock(&o2net_debug_lock);
522309e9e0SSunil Mushran }
532309e9e0SSunil Mushran 
542309e9e0SSunil Mushran void o2net_debug_del_nst(struct o2net_send_tracking *nst)
552309e9e0SSunil Mushran {
562309e9e0SSunil Mushran 	spin_lock(&o2net_debug_lock);
572309e9e0SSunil Mushran 	if (!list_empty(&nst->st_net_debug_item))
582309e9e0SSunil Mushran 		list_del_init(&nst->st_net_debug_item);
592309e9e0SSunil Mushran 	spin_unlock(&o2net_debug_lock);
602309e9e0SSunil Mushran }
612309e9e0SSunil Mushran 
622309e9e0SSunil Mushran static struct o2net_send_tracking
632309e9e0SSunil Mushran 			*next_nst(struct o2net_send_tracking *nst_start)
642309e9e0SSunil Mushran {
652309e9e0SSunil Mushran 	struct o2net_send_tracking *nst, *ret = NULL;
662309e9e0SSunil Mushran 
672309e9e0SSunil Mushran 	assert_spin_locked(&o2net_debug_lock);
682309e9e0SSunil Mushran 
692309e9e0SSunil Mushran 	list_for_each_entry(nst, &nst_start->st_net_debug_item,
702309e9e0SSunil Mushran 			    st_net_debug_item) {
712309e9e0SSunil Mushran 		/* discover the head of the list */
722309e9e0SSunil Mushran 		if (&nst->st_net_debug_item == &send_tracking)
732309e9e0SSunil Mushran 			break;
742309e9e0SSunil Mushran 
752309e9e0SSunil Mushran 		/* use st_task to detect real nsts in the list */
762309e9e0SSunil Mushran 		if (nst->st_task != NULL) {
772309e9e0SSunil Mushran 			ret = nst;
782309e9e0SSunil Mushran 			break;
792309e9e0SSunil Mushran 		}
802309e9e0SSunil Mushran 	}
812309e9e0SSunil Mushran 
822309e9e0SSunil Mushran 	return ret;
832309e9e0SSunil Mushran }
842309e9e0SSunil Mushran 
852309e9e0SSunil Mushran static void *nst_seq_start(struct seq_file *seq, loff_t *pos)
862309e9e0SSunil Mushran {
872309e9e0SSunil Mushran 	struct o2net_send_tracking *nst, *dummy_nst = seq->private;
882309e9e0SSunil Mushran 
892309e9e0SSunil Mushran 	spin_lock(&o2net_debug_lock);
902309e9e0SSunil Mushran 	nst = next_nst(dummy_nst);
912309e9e0SSunil Mushran 	spin_unlock(&o2net_debug_lock);
922309e9e0SSunil Mushran 
932309e9e0SSunil Mushran 	return nst;
942309e9e0SSunil Mushran }
952309e9e0SSunil Mushran 
962309e9e0SSunil Mushran static void *nst_seq_next(struct seq_file *seq, void *v, loff_t *pos)
972309e9e0SSunil Mushran {
982309e9e0SSunil Mushran 	struct o2net_send_tracking *nst, *dummy_nst = seq->private;
992309e9e0SSunil Mushran 
1002309e9e0SSunil Mushran 	spin_lock(&o2net_debug_lock);
1012309e9e0SSunil Mushran 	nst = next_nst(dummy_nst);
1022309e9e0SSunil Mushran 	list_del_init(&dummy_nst->st_net_debug_item);
1032309e9e0SSunil Mushran 	if (nst)
1042309e9e0SSunil Mushran 		list_add(&dummy_nst->st_net_debug_item,
1052309e9e0SSunil Mushran 			 &nst->st_net_debug_item);
1062309e9e0SSunil Mushran 	spin_unlock(&o2net_debug_lock);
1072309e9e0SSunil Mushran 
1082309e9e0SSunil Mushran 	return nst; /* unused, just needs to be null when done */
1092309e9e0SSunil Mushran }
1102309e9e0SSunil Mushran 
1112309e9e0SSunil Mushran static int nst_seq_show(struct seq_file *seq, void *v)
1122309e9e0SSunil Mushran {
1132309e9e0SSunil Mushran 	struct o2net_send_tracking *nst, *dummy_nst = seq->private;
1143f9c14faSSunil Mushran 	ktime_t now;
1153f9c14faSSunil Mushran 	s64 sock, send, status;
1162309e9e0SSunil Mushran 
1172309e9e0SSunil Mushran 	spin_lock(&o2net_debug_lock);
1182309e9e0SSunil Mushran 	nst = next_nst(dummy_nst);
119cc548166SDan Carpenter 	if (!nst)
120cc548166SDan Carpenter 		goto out;
1212309e9e0SSunil Mushran 
1223f9c14faSSunil Mushran 	now = ktime_get();
1233f9c14faSSunil Mushran 	sock = ktime_to_us(ktime_sub(now, nst->st_sock_time));
1243f9c14faSSunil Mushran 	send = ktime_to_us(ktime_sub(now, nst->st_send_time));
1253f9c14faSSunil Mushran 	status = ktime_to_us(ktime_sub(now, nst->st_status_time));
1263f9c14faSSunil Mushran 
1272309e9e0SSunil Mushran 	/* get_task_comm isn't exported.  oh well. */
1282309e9e0SSunil Mushran 	seq_printf(seq, "%p:\n"
1292309e9e0SSunil Mushran 		   "  pid:          %lu\n"
1302309e9e0SSunil Mushran 		   "  tgid:         %lu\n"
1312309e9e0SSunil Mushran 		   "  process name: %s\n"
1322309e9e0SSunil Mushran 		   "  node:         %u\n"
1332309e9e0SSunil Mushran 		   "  sc:           %p\n"
1342309e9e0SSunil Mushran 		   "  message id:   %d\n"
1352309e9e0SSunil Mushran 		   "  message type: %u\n"
1362309e9e0SSunil Mushran 		   "  message key:  0x%08x\n"
1373f9c14faSSunil Mushran 		   "  sock acquiry: %lld usecs ago\n"
1383f9c14faSSunil Mushran 		   "  send start:   %lld usecs ago\n"
1393f9c14faSSunil Mushran 		   "  wait start:   %lld usecs ago\n",
14037096a79SSunil Mushran 		   nst, (unsigned long)task_pid_nr(nst->st_task),
1412309e9e0SSunil Mushran 		   (unsigned long)nst->st_task->tgid,
1422309e9e0SSunil Mushran 		   nst->st_task->comm, nst->st_node,
1432309e9e0SSunil Mushran 		   nst->st_sc, nst->st_id, nst->st_msg_type,
1442309e9e0SSunil Mushran 		   nst->st_msg_key,
1453f9c14faSSunil Mushran 		   (long long)sock,
1463f9c14faSSunil Mushran 		   (long long)send,
1473f9c14faSSunil Mushran 		   (long long)status);
1482309e9e0SSunil Mushran 
149cc548166SDan Carpenter out:
1502309e9e0SSunil Mushran 	spin_unlock(&o2net_debug_lock);
1512309e9e0SSunil Mushran 
1522309e9e0SSunil Mushran 	return 0;
1532309e9e0SSunil Mushran }
1542309e9e0SSunil Mushran 
1552309e9e0SSunil Mushran static void nst_seq_stop(struct seq_file *seq, void *v)
1562309e9e0SSunil Mushran {
1572309e9e0SSunil Mushran }
1582309e9e0SSunil Mushran 
15988e9d34cSJames Morris static const struct seq_operations nst_seq_ops = {
1602309e9e0SSunil Mushran 	.start = nst_seq_start,
1612309e9e0SSunil Mushran 	.next = nst_seq_next,
1622309e9e0SSunil Mushran 	.stop = nst_seq_stop,
1632309e9e0SSunil Mushran 	.show = nst_seq_show,
1642309e9e0SSunil Mushran };
1652309e9e0SSunil Mushran 
1662309e9e0SSunil Mushran static int nst_fop_open(struct inode *inode, struct file *file)
1672309e9e0SSunil Mushran {
1682309e9e0SSunil Mushran 	struct o2net_send_tracking *dummy_nst;
1692309e9e0SSunil Mushran 
170f3288338SRob Jones 	dummy_nst = __seq_open_private(file, &nst_seq_ops, sizeof(*dummy_nst));
171f3288338SRob Jones 	if (!dummy_nst)
172f3288338SRob Jones 		return -ENOMEM;
1732309e9e0SSunil Mushran 	o2net_debug_add_nst(dummy_nst);
1742309e9e0SSunil Mushran 
175f3288338SRob Jones 	return 0;
1762309e9e0SSunil Mushran }
1772309e9e0SSunil Mushran 
1782309e9e0SSunil Mushran static int nst_fop_release(struct inode *inode, struct file *file)
1792309e9e0SSunil Mushran {
1802309e9e0SSunil Mushran 	struct seq_file *seq = file->private_data;
1812309e9e0SSunil Mushran 	struct o2net_send_tracking *dummy_nst = seq->private;
1822309e9e0SSunil Mushran 
1832309e9e0SSunil Mushran 	o2net_debug_del_nst(dummy_nst);
1842309e9e0SSunil Mushran 	return seq_release_private(inode, file);
1852309e9e0SSunil Mushran }
1862309e9e0SSunil Mushran 
187828c0950SAlexey Dobriyan static const struct file_operations nst_seq_fops = {
1882309e9e0SSunil Mushran 	.open = nst_fop_open,
1892309e9e0SSunil Mushran 	.read = seq_read,
1902309e9e0SSunil Mushran 	.llseek = seq_lseek,
1912309e9e0SSunil Mushran 	.release = nst_fop_release,
1922309e9e0SSunil Mushran };
1932309e9e0SSunil Mushran 
1942309e9e0SSunil Mushran void o2net_debug_add_sc(struct o2net_sock_container *sc)
1952309e9e0SSunil Mushran {
1962309e9e0SSunil Mushran 	spin_lock(&o2net_debug_lock);
1972309e9e0SSunil Mushran 	list_add(&sc->sc_net_debug_item, &sock_containers);
1982309e9e0SSunil Mushran 	spin_unlock(&o2net_debug_lock);
1992309e9e0SSunil Mushran }
2002309e9e0SSunil Mushran 
2012309e9e0SSunil Mushran void o2net_debug_del_sc(struct o2net_sock_container *sc)
2022309e9e0SSunil Mushran {
2032309e9e0SSunil Mushran 	spin_lock(&o2net_debug_lock);
2042309e9e0SSunil Mushran 	list_del_init(&sc->sc_net_debug_item);
2052309e9e0SSunil Mushran 	spin_unlock(&o2net_debug_lock);
2062309e9e0SSunil Mushran }
2072309e9e0SSunil Mushran 
208db02754cSSunil Mushran struct o2net_sock_debug {
209db02754cSSunil Mushran 	int dbg_ctxt;
210db02754cSSunil Mushran 	struct o2net_sock_container *dbg_sock;
211db02754cSSunil Mushran };
212db02754cSSunil Mushran 
2132309e9e0SSunil Mushran static struct o2net_sock_container
2142309e9e0SSunil Mushran 			*next_sc(struct o2net_sock_container *sc_start)
2152309e9e0SSunil Mushran {
2162309e9e0SSunil Mushran 	struct o2net_sock_container *sc, *ret = NULL;
2172309e9e0SSunil Mushran 
2182309e9e0SSunil Mushran 	assert_spin_locked(&o2net_debug_lock);
2192309e9e0SSunil Mushran 
2202309e9e0SSunil Mushran 	list_for_each_entry(sc, &sc_start->sc_net_debug_item,
2212309e9e0SSunil Mushran 			    sc_net_debug_item) {
2222309e9e0SSunil Mushran 		/* discover the head of the list miscast as a sc */
2232309e9e0SSunil Mushran 		if (&sc->sc_net_debug_item == &sock_containers)
2242309e9e0SSunil Mushran 			break;
2252309e9e0SSunil Mushran 
2262309e9e0SSunil Mushran 		/* use sc_page to detect real scs in the list */
2272309e9e0SSunil Mushran 		if (sc->sc_page != NULL) {
2282309e9e0SSunil Mushran 			ret = sc;
2292309e9e0SSunil Mushran 			break;
2302309e9e0SSunil Mushran 		}
2312309e9e0SSunil Mushran 	}
2322309e9e0SSunil Mushran 
2332309e9e0SSunil Mushran 	return ret;
2342309e9e0SSunil Mushran }
2352309e9e0SSunil Mushran 
2362309e9e0SSunil Mushran static void *sc_seq_start(struct seq_file *seq, loff_t *pos)
2372309e9e0SSunil Mushran {
238db02754cSSunil Mushran 	struct o2net_sock_debug *sd = seq->private;
239db02754cSSunil Mushran 	struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
2402309e9e0SSunil Mushran 
2412309e9e0SSunil Mushran 	spin_lock(&o2net_debug_lock);
2422309e9e0SSunil Mushran 	sc = next_sc(dummy_sc);
2432309e9e0SSunil Mushran 	spin_unlock(&o2net_debug_lock);
2442309e9e0SSunil Mushran 
2452309e9e0SSunil Mushran 	return sc;
2462309e9e0SSunil Mushran }
2472309e9e0SSunil Mushran 
2482309e9e0SSunil Mushran static void *sc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2492309e9e0SSunil Mushran {
250db02754cSSunil Mushran 	struct o2net_sock_debug *sd = seq->private;
251db02754cSSunil Mushran 	struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
2522309e9e0SSunil Mushran 
2532309e9e0SSunil Mushran 	spin_lock(&o2net_debug_lock);
2542309e9e0SSunil Mushran 	sc = next_sc(dummy_sc);
2552309e9e0SSunil Mushran 	list_del_init(&dummy_sc->sc_net_debug_item);
2562309e9e0SSunil Mushran 	if (sc)
2572309e9e0SSunil Mushran 		list_add(&dummy_sc->sc_net_debug_item, &sc->sc_net_debug_item);
2582309e9e0SSunil Mushran 	spin_unlock(&o2net_debug_lock);
2592309e9e0SSunil Mushran 
2602309e9e0SSunil Mushran 	return sc; /* unused, just needs to be null when done */
2612309e9e0SSunil Mushran }
2622309e9e0SSunil Mushran 
263db02754cSSunil Mushran #ifdef CONFIG_OCFS2_FS_STATS
264db02754cSSunil Mushran # define sc_send_count(_s)		((_s)->sc_send_count)
265db02754cSSunil Mushran # define sc_recv_count(_s)		((_s)->sc_recv_count)
266db02754cSSunil Mushran # define sc_tv_acquiry_total_ns(_s)	(ktime_to_ns((_s)->sc_tv_acquiry_total))
267db02754cSSunil Mushran # define sc_tv_send_total_ns(_s)	(ktime_to_ns((_s)->sc_tv_send_total))
268db02754cSSunil Mushran # define sc_tv_status_total_ns(_s)	(ktime_to_ns((_s)->sc_tv_status_total))
269db02754cSSunil Mushran # define sc_tv_process_total_ns(_s)	(ktime_to_ns((_s)->sc_tv_process_total))
270db02754cSSunil Mushran #else
271db02754cSSunil Mushran # define sc_send_count(_s)		(0U)
272db02754cSSunil Mushran # define sc_recv_count(_s)		(0U)
273db02754cSSunil Mushran # define sc_tv_acquiry_total_ns(_s)	(0LL)
274db02754cSSunil Mushran # define sc_tv_send_total_ns(_s)	(0LL)
275db02754cSSunil Mushran # define sc_tv_status_total_ns(_s)	(0LL)
276db02754cSSunil Mushran # define sc_tv_process_total_ns(_s)	(0LL)
277db02754cSSunil Mushran #endif
278db02754cSSunil Mushran 
279db02754cSSunil Mushran /* So that debugfs.ocfs2 can determine which format is being used */
280db02754cSSunil Mushran #define O2NET_STATS_STR_VERSION		1
281db02754cSSunil Mushran static void sc_show_sock_stats(struct seq_file *seq,
282db02754cSSunil Mushran 			       struct o2net_sock_container *sc)
2832309e9e0SSunil Mushran {
284db02754cSSunil Mushran 	if (!sc)
285db02754cSSunil Mushran 		return;
2862309e9e0SSunil Mushran 
287db02754cSSunil Mushran 	seq_printf(seq, "%d,%u,%lu,%lld,%lld,%lld,%lu,%lld\n", O2NET_STATS_STR_VERSION,
288db02754cSSunil Mushran 		   sc->sc_node->nd_num, (unsigned long)sc_send_count(sc),
289db02754cSSunil Mushran 		   (long long)sc_tv_acquiry_total_ns(sc),
290db02754cSSunil Mushran 		   (long long)sc_tv_send_total_ns(sc),
291db02754cSSunil Mushran 		   (long long)sc_tv_status_total_ns(sc),
292db02754cSSunil Mushran 		   (unsigned long)sc_recv_count(sc),
293db02754cSSunil Mushran 		   (long long)sc_tv_process_total_ns(sc));
294db02754cSSunil Mushran }
2952309e9e0SSunil Mushran 
296db02754cSSunil Mushran static void sc_show_sock_container(struct seq_file *seq,
297db02754cSSunil Mushran 				   struct o2net_sock_container *sc)
298db02754cSSunil Mushran {
2992309e9e0SSunil Mushran 	struct inet_sock *inet = NULL;
3002309e9e0SSunil Mushran 	__be32 saddr = 0, daddr = 0;
3012309e9e0SSunil Mushran 	__be16 sport = 0, dport = 0;
3022309e9e0SSunil Mushran 
303db02754cSSunil Mushran 	if (!sc)
304db02754cSSunil Mushran 		return;
305db02754cSSunil Mushran 
3062309e9e0SSunil Mushran 	if (sc->sc_sock) {
3072309e9e0SSunil Mushran 		inet = inet_sk(sc->sc_sock->sk);
3082309e9e0SSunil Mushran 		/* the stack's structs aren't sparse endian clean */
309c720c7e8SEric Dumazet 		saddr = (__force __be32)inet->inet_saddr;
310c720c7e8SEric Dumazet 		daddr = (__force __be32)inet->inet_daddr;
311c720c7e8SEric Dumazet 		sport = (__force __be16)inet->inet_sport;
312c720c7e8SEric Dumazet 		dport = (__force __be16)inet->inet_dport;
3132309e9e0SSunil Mushran 	}
3142309e9e0SSunil Mushran 
3152309e9e0SSunil Mushran 	/* XXX sigh, inet-> doesn't have sparse annotation so any
3162309e9e0SSunil Mushran 	 * use of it here generates a warning with -Wbitwise */
3172309e9e0SSunil Mushran 	seq_printf(seq, "%p:\n"
3182309e9e0SSunil Mushran 		   "  krefs:           %d\n"
319be859405SHarvey Harrison 		   "  sock:            %pI4:%u -> "
320be859405SHarvey Harrison 				      "%pI4:%u\n"
3212309e9e0SSunil Mushran 		   "  remote node:     %s\n"
3222309e9e0SSunil Mushran 		   "  page off:        %zu\n"
3232309e9e0SSunil Mushran 		   "  handshake ok:    %u\n"
324ff1becbfSSunil Mushran 		   "  timer:           %lld usecs\n"
325ff1becbfSSunil Mushran 		   "  data ready:      %lld usecs\n"
326ff1becbfSSunil Mushran 		   "  advance start:   %lld usecs\n"
327ff1becbfSSunil Mushran 		   "  advance stop:    %lld usecs\n"
328ff1becbfSSunil Mushran 		   "  func start:      %lld usecs\n"
329ff1becbfSSunil Mushran 		   "  func stop:       %lld usecs\n"
330ff1becbfSSunil Mushran 		   "  func key:        0x%08x\n"
3312309e9e0SSunil Mushran 		   "  func type:       %u\n",
3322309e9e0SSunil Mushran 		   sc,
3332c935bc5SPeter Zijlstra 		   kref_read(&sc->sc_kref),
334be859405SHarvey Harrison 		   &saddr, inet ? ntohs(sport) : 0,
335be859405SHarvey Harrison 		   &daddr, inet ? ntohs(dport) : 0,
3362309e9e0SSunil Mushran 		   sc->sc_node->nd_name,
3372309e9e0SSunil Mushran 		   sc->sc_page_off,
3382309e9e0SSunil Mushran 		   sc->sc_handshake_ok,
339ff1becbfSSunil Mushran 		   (long long)ktime_to_us(sc->sc_tv_timer),
340ff1becbfSSunil Mushran 		   (long long)ktime_to_us(sc->sc_tv_data_ready),
341ff1becbfSSunil Mushran 		   (long long)ktime_to_us(sc->sc_tv_advance_start),
342ff1becbfSSunil Mushran 		   (long long)ktime_to_us(sc->sc_tv_advance_stop),
343ff1becbfSSunil Mushran 		   (long long)ktime_to_us(sc->sc_tv_func_start),
344ff1becbfSSunil Mushran 		   (long long)ktime_to_us(sc->sc_tv_func_stop),
3452309e9e0SSunil Mushran 		   sc->sc_msg_key,
3462309e9e0SSunil Mushran 		   sc->sc_msg_type);
3472309e9e0SSunil Mushran }
3482309e9e0SSunil Mushran 
349db02754cSSunil Mushran static int sc_seq_show(struct seq_file *seq, void *v)
350db02754cSSunil Mushran {
351db02754cSSunil Mushran 	struct o2net_sock_debug *sd = seq->private;
352db02754cSSunil Mushran 	struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
353db02754cSSunil Mushran 
354db02754cSSunil Mushran 	spin_lock(&o2net_debug_lock);
355db02754cSSunil Mushran 	sc = next_sc(dummy_sc);
356db02754cSSunil Mushran 
357db02754cSSunil Mushran 	if (sc) {
358db02754cSSunil Mushran 		if (sd->dbg_ctxt == SHOW_SOCK_CONTAINERS)
359db02754cSSunil Mushran 			sc_show_sock_container(seq, sc);
360db02754cSSunil Mushran 		else
361db02754cSSunil Mushran 			sc_show_sock_stats(seq, sc);
362db02754cSSunil Mushran 	}
3632309e9e0SSunil Mushran 
3642309e9e0SSunil Mushran 	spin_unlock(&o2net_debug_lock);
3652309e9e0SSunil Mushran 
3662309e9e0SSunil Mushran 	return 0;
3672309e9e0SSunil Mushran }
3682309e9e0SSunil Mushran 
3692309e9e0SSunil Mushran static void sc_seq_stop(struct seq_file *seq, void *v)
3702309e9e0SSunil Mushran {
3712309e9e0SSunil Mushran }
3722309e9e0SSunil Mushran 
37388e9d34cSJames Morris static const struct seq_operations sc_seq_ops = {
3742309e9e0SSunil Mushran 	.start = sc_seq_start,
3752309e9e0SSunil Mushran 	.next = sc_seq_next,
3762309e9e0SSunil Mushran 	.stop = sc_seq_stop,
3772309e9e0SSunil Mushran 	.show = sc_seq_show,
3782309e9e0SSunil Mushran };
3792309e9e0SSunil Mushran 
380f3288338SRob Jones static int sc_common_open(struct file *file, int ctxt)
3812309e9e0SSunil Mushran {
382f3288338SRob Jones 	struct o2net_sock_debug *sd;
3832309e9e0SSunil Mushran 	struct o2net_sock_container *dummy_sc;
3842309e9e0SSunil Mushran 
385f3288338SRob Jones 	dummy_sc = kzalloc(sizeof(*dummy_sc), GFP_KERNEL);
386f3288338SRob Jones 	if (!dummy_sc)
387f3288338SRob Jones 		return -ENOMEM;
388f3288338SRob Jones 
389f3288338SRob Jones 	sd = __seq_open_private(file, &sc_seq_ops, sizeof(*sd));
390f3288338SRob Jones 	if (!sd) {
391f3288338SRob Jones 		kfree(dummy_sc);
392f3288338SRob Jones 		return -ENOMEM;
3932309e9e0SSunil Mushran 	}
3942309e9e0SSunil Mushran 
395f3288338SRob Jones 	sd->dbg_ctxt = ctxt;
396db02754cSSunil Mushran 	sd->dbg_sock = dummy_sc;
397f3288338SRob Jones 
3982309e9e0SSunil Mushran 	o2net_debug_add_sc(dummy_sc);
3992309e9e0SSunil Mushran 
400f3288338SRob Jones 	return 0;
4012309e9e0SSunil Mushran }
4022309e9e0SSunil Mushran 
4032309e9e0SSunil Mushran static int sc_fop_release(struct inode *inode, struct file *file)
4042309e9e0SSunil Mushran {
4052309e9e0SSunil Mushran 	struct seq_file *seq = file->private_data;
406db02754cSSunil Mushran 	struct o2net_sock_debug *sd = seq->private;
407db02754cSSunil Mushran 	struct o2net_sock_container *dummy_sc = sd->dbg_sock;
4082309e9e0SSunil Mushran 
4092309e9e0SSunil Mushran 	o2net_debug_del_sc(dummy_sc);
41025b1c72eSpiaojun 	kfree(dummy_sc);
4112309e9e0SSunil Mushran 	return seq_release_private(inode, file);
4122309e9e0SSunil Mushran }
4132309e9e0SSunil Mushran 
414db02754cSSunil Mushran static int stats_fop_open(struct inode *inode, struct file *file)
415db02754cSSunil Mushran {
416f3288338SRob Jones 	return sc_common_open(file, SHOW_SOCK_STATS);
417db02754cSSunil Mushran }
418db02754cSSunil Mushran 
419db02754cSSunil Mushran static const struct file_operations stats_seq_fops = {
420db02754cSSunil Mushran 	.open = stats_fop_open,
421db02754cSSunil Mushran 	.read = seq_read,
422db02754cSSunil Mushran 	.llseek = seq_lseek,
423db02754cSSunil Mushran 	.release = sc_fop_release,
424db02754cSSunil Mushran };
425db02754cSSunil Mushran 
426db02754cSSunil Mushran static int sc_fop_open(struct inode *inode, struct file *file)
427db02754cSSunil Mushran {
428f3288338SRob Jones 	return sc_common_open(file, SHOW_SOCK_CONTAINERS);
429db02754cSSunil Mushran }
430db02754cSSunil Mushran 
431828c0950SAlexey Dobriyan static const struct file_operations sc_seq_fops = {
4322309e9e0SSunil Mushran 	.open = sc_fop_open,
4332309e9e0SSunil Mushran 	.read = seq_read,
4342309e9e0SSunil Mushran 	.llseek = seq_lseek,
4352309e9e0SSunil Mushran 	.release = sc_fop_release,
4362309e9e0SSunil Mushran };
4372309e9e0SSunil Mushran 
4383ba169ccSSunil Mushran static int o2net_fill_bitmap(char *buf, int len)
4392309e9e0SSunil Mushran {
4403ba169ccSSunil Mushran 	unsigned long map[BITS_TO_LONGS(O2NM_MAX_NODES)];
4413ba169ccSSunil Mushran 	int i = -1, out = 0;
4423ba169ccSSunil Mushran 
4433ba169ccSSunil Mushran 	o2net_fill_node_map(map, sizeof(map));
4443ba169ccSSunil Mushran 
4453ba169ccSSunil Mushran 	while ((i = find_next_bit(map, O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES)
446*d293d3afSTakashi Iwai 		out += scnprintf(buf + out, PAGE_SIZE - out, "%d ", i);
447*d293d3afSTakashi Iwai 	out += scnprintf(buf + out, PAGE_SIZE - out, "\n");
4483ba169ccSSunil Mushran 
4493ba169ccSSunil Mushran 	return out;
4502309e9e0SSunil Mushran }
4512309e9e0SSunil Mushran 
4523ba169ccSSunil Mushran static int nodes_fop_open(struct inode *inode, struct file *file)
4533ba169ccSSunil Mushran {
4543ba169ccSSunil Mushran 	char *buf;
4552309e9e0SSunil Mushran 
4563ba169ccSSunil Mushran 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4573ba169ccSSunil Mushran 	if (!buf)
4583ba169ccSSunil Mushran 		return -ENOMEM;
4592309e9e0SSunil Mushran 
4603ba169ccSSunil Mushran 	i_size_write(inode, o2net_fill_bitmap(buf, PAGE_SIZE));
4613ba169ccSSunil Mushran 
4623ba169ccSSunil Mushran 	file->private_data = buf;
463db02754cSSunil Mushran 
4642309e9e0SSunil Mushran 	return 0;
4652309e9e0SSunil Mushran }
4662309e9e0SSunil Mushran 
4673ba169ccSSunil Mushran static int o2net_debug_release(struct inode *inode, struct file *file)
4683ba169ccSSunil Mushran {
4693ba169ccSSunil Mushran 	kfree(file->private_data);
4703ba169ccSSunil Mushran 	return 0;
4713ba169ccSSunil Mushran }
4723ba169ccSSunil Mushran 
4733ba169ccSSunil Mushran static ssize_t o2net_debug_read(struct file *file, char __user *buf,
4743ba169ccSSunil Mushran 				size_t nbytes, loff_t *ppos)
4753ba169ccSSunil Mushran {
4763ba169ccSSunil Mushran 	return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
4773ba169ccSSunil Mushran 				       i_size_read(file->f_mapping->host));
4783ba169ccSSunil Mushran }
4793ba169ccSSunil Mushran 
4803ba169ccSSunil Mushran static const struct file_operations nodes_fops = {
4813ba169ccSSunil Mushran 	.open		= nodes_fop_open,
4823ba169ccSSunil Mushran 	.release	= o2net_debug_release,
4833ba169ccSSunil Mushran 	.read		= o2net_debug_read,
4843ba169ccSSunil Mushran 	.llseek		= generic_file_llseek,
4853ba169ccSSunil Mushran };
4863ba169ccSSunil Mushran 
4872309e9e0SSunil Mushran void o2net_debugfs_exit(void)
4882309e9e0SSunil Mushran {
489e581595eSGreg Kroah-Hartman 	debugfs_remove_recursive(o2net_dentry);
4902309e9e0SSunil Mushran }
4912309e9e0SSunil Mushran 
492e581595eSGreg Kroah-Hartman void o2net_debugfs_init(void)
4933ba169ccSSunil Mushran {
494f4ae40a6SAl Viro 	umode_t mode = S_IFREG|S_IRUSR;
4953ba169ccSSunil Mushran 
4963ba169ccSSunil Mushran 	o2net_dentry = debugfs_create_dir(O2NET_DEBUG_DIR, NULL);
4973ba169ccSSunil Mushran 
498e581595eSGreg Kroah-Hartman 	debugfs_create_file(NST_DEBUG_NAME, mode, o2net_dentry, NULL,
499e581595eSGreg Kroah-Hartman 			    &nst_seq_fops);
500e581595eSGreg Kroah-Hartman 	debugfs_create_file(SC_DEBUG_NAME, mode, o2net_dentry, NULL,
501e581595eSGreg Kroah-Hartman 			    &sc_seq_fops);
502e581595eSGreg Kroah-Hartman 	debugfs_create_file(STATS_DEBUG_NAME, mode, o2net_dentry, NULL,
503e581595eSGreg Kroah-Hartman 			    &stats_seq_fops);
504e581595eSGreg Kroah-Hartman 	debugfs_create_file(NODES_DEBUG_NAME, mode, o2net_dentry, NULL,
505e581595eSGreg Kroah-Hartman 			    &nodes_fops);
5063ba169ccSSunil Mushran }
5073ba169ccSSunil Mushran 
5082309e9e0SSunil Mushran #endif	/* CONFIG_DEBUG_FS */
509