]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/ocfs2/cluster/netdebug.c
Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-jammy-kernel.git] / fs / ocfs2 / cluster / netdebug.c
CommitLineData
328970de 1// SPDX-License-Identifier: GPL-2.0-or-later
2309e9e0
SM
2/* -*- mode: c; c-basic-offset: 8; -*-
3 * vim: noexpandtab sw=8 ts=8 sts=0:
4 *
5 * netdebug.c
6 *
7 * debug functionality for o2net
8 *
9 * Copyright (C) 2005, 2008 Oracle. All rights reserved.
2309e9e0
SM
10 */
11
12#ifdef CONFIG_DEBUG_FS
13
14#include <linux/module.h>
15#include <linux/types.h>
16#include <linux/slab.h>
17#include <linux/idr.h>
18#include <linux/kref.h>
19#include <linux/seq_file.h>
20#include <linux/debugfs.h>
21
22#include <linux/uaccess.h>
23
24#include "tcp.h"
25#include "nodemanager.h"
26#define MLOG_MASK_PREFIX ML_TCP
27#include "masklog.h"
28
29#include "tcp_internal.h"
30
31#define O2NET_DEBUG_DIR "o2net"
32#define SC_DEBUG_NAME "sock_containers"
33#define NST_DEBUG_NAME "send_tracking"
db02754c 34#define STATS_DEBUG_NAME "stats"
3ba169cc 35#define NODES_DEBUG_NAME "connected_nodes"
db02754c
SM
36
37#define SHOW_SOCK_CONTAINERS 0
38#define SHOW_SOCK_STATS 1
2309e9e0
SM
39
40static struct dentry *o2net_dentry;
41static struct dentry *sc_dentry;
42static struct dentry *nst_dentry;
db02754c 43static struct dentry *stats_dentry;
3ba169cc 44static struct dentry *nodes_dentry;
2309e9e0
SM
45
46static DEFINE_SPINLOCK(o2net_debug_lock);
47
48static LIST_HEAD(sock_containers);
49static LIST_HEAD(send_tracking);
50
51void o2net_debug_add_nst(struct o2net_send_tracking *nst)
52{
53 spin_lock(&o2net_debug_lock);
54 list_add(&nst->st_net_debug_item, &send_tracking);
55 spin_unlock(&o2net_debug_lock);
56}
57
58void o2net_debug_del_nst(struct o2net_send_tracking *nst)
59{
60 spin_lock(&o2net_debug_lock);
61 if (!list_empty(&nst->st_net_debug_item))
62 list_del_init(&nst->st_net_debug_item);
63 spin_unlock(&o2net_debug_lock);
64}
65
66static struct o2net_send_tracking
67 *next_nst(struct o2net_send_tracking *nst_start)
68{
69 struct o2net_send_tracking *nst, *ret = NULL;
70
71 assert_spin_locked(&o2net_debug_lock);
72
73 list_for_each_entry(nst, &nst_start->st_net_debug_item,
74 st_net_debug_item) {
75 /* discover the head of the list */
76 if (&nst->st_net_debug_item == &send_tracking)
77 break;
78
79 /* use st_task to detect real nsts in the list */
80 if (nst->st_task != NULL) {
81 ret = nst;
82 break;
83 }
84 }
85
86 return ret;
87}
88
89static void *nst_seq_start(struct seq_file *seq, loff_t *pos)
90{
91 struct o2net_send_tracking *nst, *dummy_nst = seq->private;
92
93 spin_lock(&o2net_debug_lock);
94 nst = next_nst(dummy_nst);
95 spin_unlock(&o2net_debug_lock);
96
97 return nst;
98}
99
100static void *nst_seq_next(struct seq_file *seq, void *v, loff_t *pos)
101{
102 struct o2net_send_tracking *nst, *dummy_nst = seq->private;
103
104 spin_lock(&o2net_debug_lock);
105 nst = next_nst(dummy_nst);
106 list_del_init(&dummy_nst->st_net_debug_item);
107 if (nst)
108 list_add(&dummy_nst->st_net_debug_item,
109 &nst->st_net_debug_item);
110 spin_unlock(&o2net_debug_lock);
111
112 return nst; /* unused, just needs to be null when done */
113}
114
115static int nst_seq_show(struct seq_file *seq, void *v)
116{
117 struct o2net_send_tracking *nst, *dummy_nst = seq->private;
3f9c14fa
SM
118 ktime_t now;
119 s64 sock, send, status;
2309e9e0
SM
120
121 spin_lock(&o2net_debug_lock);
122 nst = next_nst(dummy_nst);
cc548166
DC
123 if (!nst)
124 goto out;
2309e9e0 125
3f9c14fa
SM
126 now = ktime_get();
127 sock = ktime_to_us(ktime_sub(now, nst->st_sock_time));
128 send = ktime_to_us(ktime_sub(now, nst->st_send_time));
129 status = ktime_to_us(ktime_sub(now, nst->st_status_time));
130
cc548166
DC
131 /* get_task_comm isn't exported. oh well. */
132 seq_printf(seq, "%p:\n"
133 " pid: %lu\n"
134 " tgid: %lu\n"
135 " process name: %s\n"
136 " node: %u\n"
137 " sc: %p\n"
138 " message id: %d\n"
139 " message type: %u\n"
140 " message key: 0x%08x\n"
141 " sock acquiry: %lld usecs ago\n"
142 " send start: %lld usecs ago\n"
143 " wait start: %lld usecs ago\n",
144 nst, (unsigned long)task_pid_nr(nst->st_task),
145 (unsigned long)nst->st_task->tgid,
146 nst->st_task->comm, nst->st_node,
147 nst->st_sc, nst->st_id, nst->st_msg_type,
148 nst->st_msg_key,
149 (long long)sock,
150 (long long)send,
151 (long long)status);
2309e9e0 152
cc548166 153out:
2309e9e0
SM
154 spin_unlock(&o2net_debug_lock);
155
156 return 0;
157}
158
159static void nst_seq_stop(struct seq_file *seq, void *v)
160{
161}
162
88e9d34c 163static const struct seq_operations nst_seq_ops = {
2309e9e0
SM
164 .start = nst_seq_start,
165 .next = nst_seq_next,
166 .stop = nst_seq_stop,
167 .show = nst_seq_show,
168};
169
170static int nst_fop_open(struct inode *inode, struct file *file)
171{
172 struct o2net_send_tracking *dummy_nst;
2309e9e0 173
f3288338
RJ
174 dummy_nst = __seq_open_private(file, &nst_seq_ops, sizeof(*dummy_nst));
175 if (!dummy_nst)
176 return -ENOMEM;
2309e9e0
SM
177 o2net_debug_add_nst(dummy_nst);
178
f3288338 179 return 0;
2309e9e0
SM
180}
181
182static int nst_fop_release(struct inode *inode, struct file *file)
183{
184 struct seq_file *seq = file->private_data;
185 struct o2net_send_tracking *dummy_nst = seq->private;
186
187 o2net_debug_del_nst(dummy_nst);
188 return seq_release_private(inode, file);
189}
190
828c0950 191static const struct file_operations nst_seq_fops = {
2309e9e0
SM
192 .open = nst_fop_open,
193 .read = seq_read,
194 .llseek = seq_lseek,
195 .release = nst_fop_release,
196};
197
198void o2net_debug_add_sc(struct o2net_sock_container *sc)
199{
200 spin_lock(&o2net_debug_lock);
201 list_add(&sc->sc_net_debug_item, &sock_containers);
202 spin_unlock(&o2net_debug_lock);
203}
204
205void o2net_debug_del_sc(struct o2net_sock_container *sc)
206{
207 spin_lock(&o2net_debug_lock);
208 list_del_init(&sc->sc_net_debug_item);
209 spin_unlock(&o2net_debug_lock);
210}
211
db02754c
SM
212struct o2net_sock_debug {
213 int dbg_ctxt;
214 struct o2net_sock_container *dbg_sock;
215};
216
2309e9e0
SM
217static struct o2net_sock_container
218 *next_sc(struct o2net_sock_container *sc_start)
219{
220 struct o2net_sock_container *sc, *ret = NULL;
221
222 assert_spin_locked(&o2net_debug_lock);
223
224 list_for_each_entry(sc, &sc_start->sc_net_debug_item,
225 sc_net_debug_item) {
226 /* discover the head of the list miscast as a sc */
227 if (&sc->sc_net_debug_item == &sock_containers)
228 break;
229
230 /* use sc_page to detect real scs in the list */
231 if (sc->sc_page != NULL) {
232 ret = sc;
233 break;
234 }
235 }
236
237 return ret;
238}
239
240static void *sc_seq_start(struct seq_file *seq, loff_t *pos)
241{
db02754c
SM
242 struct o2net_sock_debug *sd = seq->private;
243 struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
2309e9e0
SM
244
245 spin_lock(&o2net_debug_lock);
246 sc = next_sc(dummy_sc);
247 spin_unlock(&o2net_debug_lock);
248
249 return sc;
250}
251
252static void *sc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
253{
db02754c
SM
254 struct o2net_sock_debug *sd = seq->private;
255 struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
2309e9e0
SM
256
257 spin_lock(&o2net_debug_lock);
258 sc = next_sc(dummy_sc);
259 list_del_init(&dummy_sc->sc_net_debug_item);
260 if (sc)
261 list_add(&dummy_sc->sc_net_debug_item, &sc->sc_net_debug_item);
262 spin_unlock(&o2net_debug_lock);
263
264 return sc; /* unused, just needs to be null when done */
265}
266
db02754c
SM
267#ifdef CONFIG_OCFS2_FS_STATS
268# define sc_send_count(_s) ((_s)->sc_send_count)
269# define sc_recv_count(_s) ((_s)->sc_recv_count)
270# define sc_tv_acquiry_total_ns(_s) (ktime_to_ns((_s)->sc_tv_acquiry_total))
271# define sc_tv_send_total_ns(_s) (ktime_to_ns((_s)->sc_tv_send_total))
272# define sc_tv_status_total_ns(_s) (ktime_to_ns((_s)->sc_tv_status_total))
273# define sc_tv_process_total_ns(_s) (ktime_to_ns((_s)->sc_tv_process_total))
274#else
275# define sc_send_count(_s) (0U)
276# define sc_recv_count(_s) (0U)
277# define sc_tv_acquiry_total_ns(_s) (0LL)
278# define sc_tv_send_total_ns(_s) (0LL)
279# define sc_tv_status_total_ns(_s) (0LL)
280# define sc_tv_process_total_ns(_s) (0LL)
281#endif
282
283/* So that debugfs.ocfs2 can determine which format is being used */
284#define O2NET_STATS_STR_VERSION 1
285static void sc_show_sock_stats(struct seq_file *seq,
286 struct o2net_sock_container *sc)
2309e9e0 287{
db02754c
SM
288 if (!sc)
289 return;
290
291 seq_printf(seq, "%d,%u,%lu,%lld,%lld,%lld,%lu,%lld\n", O2NET_STATS_STR_VERSION,
292 sc->sc_node->nd_num, (unsigned long)sc_send_count(sc),
293 (long long)sc_tv_acquiry_total_ns(sc),
294 (long long)sc_tv_send_total_ns(sc),
295 (long long)sc_tv_status_total_ns(sc),
296 (unsigned long)sc_recv_count(sc),
297 (long long)sc_tv_process_total_ns(sc));
298}
2309e9e0 299
db02754c
SM
300static void sc_show_sock_container(struct seq_file *seq,
301 struct o2net_sock_container *sc)
302{
303 struct inet_sock *inet = NULL;
304 __be32 saddr = 0, daddr = 0;
305 __be16 sport = 0, dport = 0;
306
307 if (!sc)
308 return;
309
310 if (sc->sc_sock) {
311 inet = inet_sk(sc->sc_sock->sk);
312 /* the stack's structs aren't sparse endian clean */
313 saddr = (__force __be32)inet->inet_saddr;
314 daddr = (__force __be32)inet->inet_daddr;
315 sport = (__force __be16)inet->inet_sport;
316 dport = (__force __be16)inet->inet_dport;
317 }
2309e9e0 318
db02754c
SM
319 /* XXX sigh, inet-> doesn't have sparse annotation so any
320 * use of it here generates a warning with -Wbitwise */
321 seq_printf(seq, "%p:\n"
322 " krefs: %d\n"
323 " sock: %pI4:%u -> "
324 "%pI4:%u\n"
325 " remote node: %s\n"
326 " page off: %zu\n"
327 " handshake ok: %u\n"
328 " timer: %lld usecs\n"
329 " data ready: %lld usecs\n"
330 " advance start: %lld usecs\n"
331 " advance stop: %lld usecs\n"
332 " func start: %lld usecs\n"
333 " func stop: %lld usecs\n"
334 " func key: 0x%08x\n"
335 " func type: %u\n",
336 sc,
2c935bc5 337 kref_read(&sc->sc_kref),
db02754c
SM
338 &saddr, inet ? ntohs(sport) : 0,
339 &daddr, inet ? ntohs(dport) : 0,
340 sc->sc_node->nd_name,
341 sc->sc_page_off,
342 sc->sc_handshake_ok,
343 (long long)ktime_to_us(sc->sc_tv_timer),
344 (long long)ktime_to_us(sc->sc_tv_data_ready),
345 (long long)ktime_to_us(sc->sc_tv_advance_start),
346 (long long)ktime_to_us(sc->sc_tv_advance_stop),
347 (long long)ktime_to_us(sc->sc_tv_func_start),
348 (long long)ktime_to_us(sc->sc_tv_func_stop),
349 sc->sc_msg_key,
350 sc->sc_msg_type);
351}
2309e9e0 352
db02754c
SM
353static int sc_seq_show(struct seq_file *seq, void *v)
354{
355 struct o2net_sock_debug *sd = seq->private;
356 struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
2309e9e0 357
db02754c
SM
358 spin_lock(&o2net_debug_lock);
359 sc = next_sc(dummy_sc);
2309e9e0 360
db02754c
SM
361 if (sc) {
362 if (sd->dbg_ctxt == SHOW_SOCK_CONTAINERS)
363 sc_show_sock_container(seq, sc);
364 else
365 sc_show_sock_stats(seq, sc);
2309e9e0
SM
366 }
367
2309e9e0
SM
368 spin_unlock(&o2net_debug_lock);
369
370 return 0;
371}
372
373static void sc_seq_stop(struct seq_file *seq, void *v)
374{
375}
376
88e9d34c 377static const struct seq_operations sc_seq_ops = {
2309e9e0
SM
378 .start = sc_seq_start,
379 .next = sc_seq_next,
380 .stop = sc_seq_stop,
381 .show = sc_seq_show,
382};
383
f3288338 384static int sc_common_open(struct file *file, int ctxt)
2309e9e0 385{
f3288338 386 struct o2net_sock_debug *sd;
2309e9e0 387 struct o2net_sock_container *dummy_sc;
2309e9e0 388
f3288338
RJ
389 dummy_sc = kzalloc(sizeof(*dummy_sc), GFP_KERNEL);
390 if (!dummy_sc)
391 return -ENOMEM;
2309e9e0 392
f3288338
RJ
393 sd = __seq_open_private(file, &sc_seq_ops, sizeof(*sd));
394 if (!sd) {
395 kfree(dummy_sc);
396 return -ENOMEM;
397 }
2309e9e0 398
f3288338 399 sd->dbg_ctxt = ctxt;
db02754c 400 sd->dbg_sock = dummy_sc;
2309e9e0 401
f3288338 402 o2net_debug_add_sc(dummy_sc);
2309e9e0 403
f3288338 404 return 0;
2309e9e0
SM
405}
406
407static int sc_fop_release(struct inode *inode, struct file *file)
408{
409 struct seq_file *seq = file->private_data;
db02754c
SM
410 struct o2net_sock_debug *sd = seq->private;
411 struct o2net_sock_container *dummy_sc = sd->dbg_sock;
2309e9e0
SM
412
413 o2net_debug_del_sc(dummy_sc);
25b1c72e 414 kfree(dummy_sc);
2309e9e0
SM
415 return seq_release_private(inode, file);
416}
417
db02754c
SM
418static int stats_fop_open(struct inode *inode, struct file *file)
419{
f3288338 420 return sc_common_open(file, SHOW_SOCK_STATS);
db02754c
SM
421}
422
423static const struct file_operations stats_seq_fops = {
424 .open = stats_fop_open,
425 .read = seq_read,
426 .llseek = seq_lseek,
427 .release = sc_fop_release,
428};
429
430static int sc_fop_open(struct inode *inode, struct file *file)
431{
f3288338 432 return sc_common_open(file, SHOW_SOCK_CONTAINERS);
db02754c
SM
433}
434
828c0950 435static const struct file_operations sc_seq_fops = {
2309e9e0
SM
436 .open = sc_fop_open,
437 .read = seq_read,
438 .llseek = seq_lseek,
439 .release = sc_fop_release,
440};
441
3ba169cc 442static int o2net_fill_bitmap(char *buf, int len)
2309e9e0 443{
3ba169cc
SM
444 unsigned long map[BITS_TO_LONGS(O2NM_MAX_NODES)];
445 int i = -1, out = 0;
2309e9e0 446
3ba169cc 447 o2net_fill_node_map(map, sizeof(map));
2309e9e0 448
3ba169cc
SM
449 while ((i = find_next_bit(map, O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES)
450 out += snprintf(buf + out, PAGE_SIZE - out, "%d ", i);
451 out += snprintf(buf + out, PAGE_SIZE - out, "\n");
2309e9e0 452
3ba169cc
SM
453 return out;
454}
455
456static int nodes_fop_open(struct inode *inode, struct file *file)
457{
458 char *buf;
459
460 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
461 if (!buf)
462 return -ENOMEM;
463
464 i_size_write(inode, o2net_fill_bitmap(buf, PAGE_SIZE));
465
466 file->private_data = buf;
db02754c 467
2309e9e0 468 return 0;
2309e9e0
SM
469}
470
3ba169cc
SM
471static int o2net_debug_release(struct inode *inode, struct file *file)
472{
473 kfree(file->private_data);
474 return 0;
475}
476
477static ssize_t o2net_debug_read(struct file *file, char __user *buf,
478 size_t nbytes, loff_t *ppos)
479{
480 return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
481 i_size_read(file->f_mapping->host));
482}
483
484static const struct file_operations nodes_fops = {
485 .open = nodes_fop_open,
486 .release = o2net_debug_release,
487 .read = o2net_debug_read,
488 .llseek = generic_file_llseek,
489};
490
2309e9e0
SM
491void o2net_debugfs_exit(void)
492{
3ba169cc 493 debugfs_remove(nodes_dentry);
db02754c 494 debugfs_remove(stats_dentry);
37096a79
SM
495 debugfs_remove(sc_dentry);
496 debugfs_remove(nst_dentry);
497 debugfs_remove(o2net_dentry);
2309e9e0
SM
498}
499
3ba169cc
SM
500int o2net_debugfs_init(void)
501{
f4ae40a6 502 umode_t mode = S_IFREG|S_IRUSR;
3ba169cc
SM
503
504 o2net_dentry = debugfs_create_dir(O2NET_DEBUG_DIR, NULL);
505 if (o2net_dentry)
506 nst_dentry = debugfs_create_file(NST_DEBUG_NAME, mode,
507 o2net_dentry, NULL, &nst_seq_fops);
508 if (nst_dentry)
509 sc_dentry = debugfs_create_file(SC_DEBUG_NAME, mode,
510 o2net_dentry, NULL, &sc_seq_fops);
511 if (sc_dentry)
512 stats_dentry = debugfs_create_file(STATS_DEBUG_NAME, mode,
513 o2net_dentry, NULL, &stats_seq_fops);
514 if (stats_dentry)
515 nodes_dentry = debugfs_create_file(NODES_DEBUG_NAME, mode,
516 o2net_dentry, NULL, &nodes_fops);
517 if (nodes_dentry)
518 return 0;
519
520 o2net_debugfs_exit();
521 mlog_errno(-ENOMEM);
522 return -ENOMEM;
523}
524
2309e9e0 525#endif /* CONFIG_DEBUG_FS */