treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 145
[linux-block.git] / fs / ocfs2 / cluster / netdebug.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
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.
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"
34 #define STATS_DEBUG_NAME        "stats"
35 #define NODES_DEBUG_NAME        "connected_nodes"
36
37 #define SHOW_SOCK_CONTAINERS    0
38 #define SHOW_SOCK_STATS         1
39
40 static struct dentry *o2net_dentry;
41 static struct dentry *sc_dentry;
42 static struct dentry *nst_dentry;
43 static struct dentry *stats_dentry;
44 static struct dentry *nodes_dentry;
45
46 static DEFINE_SPINLOCK(o2net_debug_lock);
47
48 static LIST_HEAD(sock_containers);
49 static LIST_HEAD(send_tracking);
50
51 void 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
58 void 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
66 static 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
89 static 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
100 static 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
115 static int nst_seq_show(struct seq_file *seq, void *v)
116 {
117         struct o2net_send_tracking *nst, *dummy_nst = seq->private;
118         ktime_t now;
119         s64 sock, send, status;
120
121         spin_lock(&o2net_debug_lock);
122         nst = next_nst(dummy_nst);
123         if (!nst)
124                 goto out;
125
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
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);
152
153 out:
154         spin_unlock(&o2net_debug_lock);
155
156         return 0;
157 }
158
159 static void nst_seq_stop(struct seq_file *seq, void *v)
160 {
161 }
162
163 static const struct seq_operations nst_seq_ops = {
164         .start = nst_seq_start,
165         .next = nst_seq_next,
166         .stop = nst_seq_stop,
167         .show = nst_seq_show,
168 };
169
170 static int nst_fop_open(struct inode *inode, struct file *file)
171 {
172         struct o2net_send_tracking *dummy_nst;
173
174         dummy_nst = __seq_open_private(file, &nst_seq_ops, sizeof(*dummy_nst));
175         if (!dummy_nst)
176                 return -ENOMEM;
177         o2net_debug_add_nst(dummy_nst);
178
179         return 0;
180 }
181
182 static 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
191 static const struct file_operations nst_seq_fops = {
192         .open = nst_fop_open,
193         .read = seq_read,
194         .llseek = seq_lseek,
195         .release = nst_fop_release,
196 };
197
198 void 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
205 void 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
212 struct o2net_sock_debug {
213         int dbg_ctxt;
214         struct o2net_sock_container *dbg_sock;
215 };
216
217 static 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
240 static void *sc_seq_start(struct seq_file *seq, loff_t *pos)
241 {
242         struct o2net_sock_debug *sd = seq->private;
243         struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
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
252 static void *sc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
253 {
254         struct o2net_sock_debug *sd = seq->private;
255         struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
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
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
285 static void sc_show_sock_stats(struct seq_file *seq,
286                                struct o2net_sock_container *sc)
287 {
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 }
299
300 static 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         }
318
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,
337                    kref_read(&sc->sc_kref),
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 }
352
353 static 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;
357
358         spin_lock(&o2net_debug_lock);
359         sc = next_sc(dummy_sc);
360
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);
366         }
367
368         spin_unlock(&o2net_debug_lock);
369
370         return 0;
371 }
372
373 static void sc_seq_stop(struct seq_file *seq, void *v)
374 {
375 }
376
377 static const struct seq_operations sc_seq_ops = {
378         .start = sc_seq_start,
379         .next = sc_seq_next,
380         .stop = sc_seq_stop,
381         .show = sc_seq_show,
382 };
383
384 static int sc_common_open(struct file *file, int ctxt)
385 {
386         struct o2net_sock_debug *sd;
387         struct o2net_sock_container *dummy_sc;
388
389         dummy_sc = kzalloc(sizeof(*dummy_sc), GFP_KERNEL);
390         if (!dummy_sc)
391                 return -ENOMEM;
392
393         sd = __seq_open_private(file, &sc_seq_ops, sizeof(*sd));
394         if (!sd) {
395                 kfree(dummy_sc);
396                 return -ENOMEM;
397         }
398
399         sd->dbg_ctxt = ctxt;
400         sd->dbg_sock = dummy_sc;
401
402         o2net_debug_add_sc(dummy_sc);
403
404         return 0;
405 }
406
407 static int sc_fop_release(struct inode *inode, struct file *file)
408 {
409         struct seq_file *seq = file->private_data;
410         struct o2net_sock_debug *sd = seq->private;
411         struct o2net_sock_container *dummy_sc = sd->dbg_sock;
412
413         o2net_debug_del_sc(dummy_sc);
414         kfree(dummy_sc);
415         return seq_release_private(inode, file);
416 }
417
418 static int stats_fop_open(struct inode *inode, struct file *file)
419 {
420         return sc_common_open(file, SHOW_SOCK_STATS);
421 }
422
423 static 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
430 static int sc_fop_open(struct inode *inode, struct file *file)
431 {
432         return sc_common_open(file, SHOW_SOCK_CONTAINERS);
433 }
434
435 static const struct file_operations sc_seq_fops = {
436         .open = sc_fop_open,
437         .read = seq_read,
438         .llseek = seq_lseek,
439         .release = sc_fop_release,
440 };
441
442 static int o2net_fill_bitmap(char *buf, int len)
443 {
444         unsigned long map[BITS_TO_LONGS(O2NM_MAX_NODES)];
445         int i = -1, out = 0;
446
447         o2net_fill_node_map(map, sizeof(map));
448
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");
452
453         return out;
454 }
455
456 static 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;
467
468         return 0;
469 }
470
471 static int o2net_debug_release(struct inode *inode, struct file *file)
472 {
473         kfree(file->private_data);
474         return 0;
475 }
476
477 static 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
484 static 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
491 void o2net_debugfs_exit(void)
492 {
493         debugfs_remove(nodes_dentry);
494         debugfs_remove(stats_dentry);
495         debugfs_remove(sc_dentry);
496         debugfs_remove(nst_dentry);
497         debugfs_remove(o2net_dentry);
498 }
499
500 int o2net_debugfs_init(void)
501 {
502         umode_t mode = S_IFREG|S_IRUSR;
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
525 #endif  /* CONFIG_DEBUG_FS */