1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2010-2020 B.A.T.M.A.N. contributors:
10 #include <linux/compiler.h>
11 #include <linux/debugfs.h>
12 #include <linux/errno.h>
13 #include <linux/eventpoll.h>
14 #include <linux/export.h>
15 #include <linux/fcntl.h>
17 #include <linux/gfp.h>
18 #include <linux/jiffies.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/poll.h>
22 #include <linux/sched.h> /* for linux/wait.h */
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
25 #include <linux/stddef.h>
26 #include <linux/types.h>
27 #include <linux/uaccess.h>
28 #include <linux/wait.h>
34 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
36 #define BATADV_LOG_BUFF_MASK (batadv_log_buff_len - 1)
38 static const int batadv_log_buff_len
= BATADV_LOG_BUF_LEN
;
40 static char *batadv_log_char_addr(struct batadv_priv_debug_log
*debug_log
,
43 return &debug_log
->log_buff
[idx
& BATADV_LOG_BUFF_MASK
];
46 static void batadv_emit_log_char(struct batadv_priv_debug_log
*debug_log
,
51 char_addr
= batadv_log_char_addr(debug_log
, debug_log
->log_end
);
55 if (debug_log
->log_end
- debug_log
->log_start
> batadv_log_buff_len
)
56 debug_log
->log_start
= debug_log
->log_end
- batadv_log_buff_len
;
60 static int batadv_fdebug_log(struct batadv_priv_debug_log
*debug_log
,
64 static char debug_log_buf
[256];
70 spin_lock_bh(&debug_log
->lock
);
72 vscnprintf(debug_log_buf
, sizeof(debug_log_buf
), fmt
, args
);
75 for (p
= debug_log_buf
; *p
!= 0; p
++)
76 batadv_emit_log_char(debug_log
, *p
);
78 spin_unlock_bh(&debug_log
->lock
);
80 wake_up(&debug_log
->queue_wait
);
85 static int batadv_log_open(struct inode
*inode
, struct file
*file
)
87 if (!try_module_get(THIS_MODULE
))
90 batadv_debugfs_deprecated(file
,
91 "Use tracepoint batadv:batadv_dbg instead\n");
93 stream_open(inode
, file
);
94 file
->private_data
= inode
->i_private
;
98 static int batadv_log_release(struct inode
*inode
, struct file
*file
)
100 module_put(THIS_MODULE
);
104 static bool batadv_log_empty(struct batadv_priv_debug_log
*debug_log
)
106 return !(debug_log
->log_start
- debug_log
->log_end
);
109 static ssize_t
batadv_log_read(struct file
*file
, char __user
*buf
,
110 size_t count
, loff_t
*ppos
)
112 struct batadv_priv
*bat_priv
= file
->private_data
;
113 struct batadv_priv_debug_log
*debug_log
= bat_priv
->debug_log
;
118 if ((file
->f_flags
& O_NONBLOCK
) && batadv_log_empty(debug_log
))
127 if (!access_ok(buf
, count
))
130 error
= wait_event_interruptible(debug_log
->queue_wait
,
131 (!batadv_log_empty(debug_log
)));
136 spin_lock_bh(&debug_log
->lock
);
138 while ((!error
) && (i
< count
) &&
139 (debug_log
->log_start
!= debug_log
->log_end
)) {
140 char_addr
= batadv_log_char_addr(debug_log
,
141 debug_log
->log_start
);
144 debug_log
->log_start
++;
146 spin_unlock_bh(&debug_log
->lock
);
148 error
= __put_user(c
, buf
);
150 spin_lock_bh(&debug_log
->lock
);
156 spin_unlock_bh(&debug_log
->lock
);
164 static __poll_t
batadv_log_poll(struct file
*file
, poll_table
*wait
)
166 struct batadv_priv
*bat_priv
= file
->private_data
;
167 struct batadv_priv_debug_log
*debug_log
= bat_priv
->debug_log
;
169 poll_wait(file
, &debug_log
->queue_wait
, wait
);
171 if (!batadv_log_empty(debug_log
))
172 return EPOLLIN
| EPOLLRDNORM
;
177 static const struct file_operations batadv_log_fops
= {
178 .open
= batadv_log_open
,
179 .release
= batadv_log_release
,
180 .read
= batadv_log_read
,
181 .poll
= batadv_log_poll
,
186 * batadv_debug_log_setup() - Initialize debug log
187 * @bat_priv: the bat priv with all the soft interface information
189 * Return: 0 on success or negative error number in case of failure
191 int batadv_debug_log_setup(struct batadv_priv
*bat_priv
)
193 bat_priv
->debug_log
= kzalloc(sizeof(*bat_priv
->debug_log
), GFP_ATOMIC
);
194 if (!bat_priv
->debug_log
)
197 spin_lock_init(&bat_priv
->debug_log
->lock
);
198 init_waitqueue_head(&bat_priv
->debug_log
->queue_wait
);
200 debugfs_create_file("log", 0400, bat_priv
->debug_dir
, bat_priv
,
206 * batadv_debug_log_cleanup() - Destroy debug log
207 * @bat_priv: the bat priv with all the soft interface information
209 void batadv_debug_log_cleanup(struct batadv_priv
*bat_priv
)
211 kfree(bat_priv
->debug_log
);
212 bat_priv
->debug_log
= NULL
;
215 #endif /* CONFIG_BATMAN_ADV_DEBUGFS */
218 * batadv_debug_log() - Add debug log entry
219 * @bat_priv: the bat priv with all the soft interface information
220 * @fmt: format string
222 * Return: 0 on success or negative error number in case of failure
224 int batadv_debug_log(struct batadv_priv
*bat_priv
, const char *fmt
, ...)
226 struct va_format vaf
;
234 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
235 batadv_fdebug_log(bat_priv
->debug_log
, "[%10u] %pV",
236 jiffies_to_msecs(jiffies
), &vaf
);
239 trace_batadv_dbg(bat_priv
, &vaf
);