1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2010-2018 B.A.T.M.A.N. contributors:
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include <linux/compiler.h>
23 #include <linux/debugfs.h>
24 #include <linux/errno.h>
25 #include <linux/eventpoll.h>
26 #include <linux/export.h>
27 #include <linux/fcntl.h>
29 #include <linux/gfp.h>
30 #include <linux/jiffies.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/poll.h>
34 #include <linux/sched.h> /* for linux/wait.h */
35 #include <linux/slab.h>
36 #include <linux/spinlock.h>
37 #include <linux/stddef.h>
38 #include <linux/types.h>
39 #include <linux/uaccess.h>
40 #include <linux/wait.h>
46 #define BATADV_LOG_BUFF_MASK (batadv_log_buff_len - 1)
48 static const int batadv_log_buff_len
= BATADV_LOG_BUF_LEN
;
50 static char *batadv_log_char_addr(struct batadv_priv_debug_log
*debug_log
,
53 return &debug_log
->log_buff
[idx
& BATADV_LOG_BUFF_MASK
];
56 static void batadv_emit_log_char(struct batadv_priv_debug_log
*debug_log
,
61 char_addr
= batadv_log_char_addr(debug_log
, debug_log
->log_end
);
65 if (debug_log
->log_end
- debug_log
->log_start
> batadv_log_buff_len
)
66 debug_log
->log_start
= debug_log
->log_end
- batadv_log_buff_len
;
70 static int batadv_fdebug_log(struct batadv_priv_debug_log
*debug_log
,
74 static char debug_log_buf
[256];
80 spin_lock_bh(&debug_log
->lock
);
82 vscnprintf(debug_log_buf
, sizeof(debug_log_buf
), fmt
, args
);
85 for (p
= debug_log_buf
; *p
!= 0; p
++)
86 batadv_emit_log_char(debug_log
, *p
);
88 spin_unlock_bh(&debug_log
->lock
);
90 wake_up(&debug_log
->queue_wait
);
96 * batadv_debug_log() - Add debug log entry
97 * @bat_priv: the bat priv with all the soft interface information
100 * Return: 0 on success or negative error number in case of failure
102 int batadv_debug_log(struct batadv_priv
*bat_priv
, const char *fmt
, ...)
104 struct va_format vaf
;
112 batadv_fdebug_log(bat_priv
->debug_log
, "[%10u] %pV",
113 jiffies_to_msecs(jiffies
), &vaf
);
115 trace_batadv_dbg(bat_priv
, &vaf
);
122 static int batadv_log_open(struct inode
*inode
, struct file
*file
)
124 if (!try_module_get(THIS_MODULE
))
127 batadv_debugfs_deprecated(file
,
128 "Use tracepoint batadv:batadv_dbg instead\n");
130 nonseekable_open(inode
, file
);
131 file
->private_data
= inode
->i_private
;
135 static int batadv_log_release(struct inode
*inode
, struct file
*file
)
137 module_put(THIS_MODULE
);
141 static bool batadv_log_empty(struct batadv_priv_debug_log
*debug_log
)
143 return !(debug_log
->log_start
- debug_log
->log_end
);
146 static ssize_t
batadv_log_read(struct file
*file
, char __user
*buf
,
147 size_t count
, loff_t
*ppos
)
149 struct batadv_priv
*bat_priv
= file
->private_data
;
150 struct batadv_priv_debug_log
*debug_log
= bat_priv
->debug_log
;
155 if ((file
->f_flags
& O_NONBLOCK
) && batadv_log_empty(debug_log
))
164 if (!access_ok(VERIFY_WRITE
, buf
, count
))
167 error
= wait_event_interruptible(debug_log
->queue_wait
,
168 (!batadv_log_empty(debug_log
)));
173 spin_lock_bh(&debug_log
->lock
);
175 while ((!error
) && (i
< count
) &&
176 (debug_log
->log_start
!= debug_log
->log_end
)) {
177 char_addr
= batadv_log_char_addr(debug_log
,
178 debug_log
->log_start
);
181 debug_log
->log_start
++;
183 spin_unlock_bh(&debug_log
->lock
);
185 error
= __put_user(c
, buf
);
187 spin_lock_bh(&debug_log
->lock
);
193 spin_unlock_bh(&debug_log
->lock
);
201 static __poll_t
batadv_log_poll(struct file
*file
, poll_table
*wait
)
203 struct batadv_priv
*bat_priv
= file
->private_data
;
204 struct batadv_priv_debug_log
*debug_log
= bat_priv
->debug_log
;
206 poll_wait(file
, &debug_log
->queue_wait
, wait
);
208 if (!batadv_log_empty(debug_log
))
209 return EPOLLIN
| EPOLLRDNORM
;
214 static const struct file_operations batadv_log_fops
= {
215 .open
= batadv_log_open
,
216 .release
= batadv_log_release
,
217 .read
= batadv_log_read
,
218 .poll
= batadv_log_poll
,
223 * batadv_debug_log_setup() - Initialize debug log
224 * @bat_priv: the bat priv with all the soft interface information
226 * Return: 0 on success or negative error number in case of failure
228 int batadv_debug_log_setup(struct batadv_priv
*bat_priv
)
232 if (!bat_priv
->debug_dir
)
235 bat_priv
->debug_log
= kzalloc(sizeof(*bat_priv
->debug_log
), GFP_ATOMIC
);
236 if (!bat_priv
->debug_log
)
239 spin_lock_init(&bat_priv
->debug_log
->lock
);
240 init_waitqueue_head(&bat_priv
->debug_log
->queue_wait
);
242 d
= debugfs_create_file("log", 0400, bat_priv
->debug_dir
, bat_priv
,
254 * batadv_debug_log_cleanup() - Destroy debug log
255 * @bat_priv: the bat priv with all the soft interface information
257 void batadv_debug_log_cleanup(struct batadv_priv
*bat_priv
)
259 kfree(bat_priv
->debug_log
);
260 bat_priv
->debug_log
= NULL
;